dropbear: update to 2013.62
[tomato.git] / release / src / router / dropbear / gensignkey.c
blobcb66ffff7fef7038cec7918e64729b1867806055
1 #include "includes.h"
2 #include "dbutil.h"
3 #include "buffer.h"
4 #include "ecdsa.h"
5 #include "genrsa.h"
6 #include "gendss.h"
7 #include "signkey.h"
8 #include "dbrandom.h"
10 #define RSA_DEFAULT_SIZE 2048
11 #define DSS_DEFAULT_SIZE 1024
13 /* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
14 static int buf_writefile(buffer * buf, const char * filename) {
15 int ret = DROPBEAR_FAILURE;
16 int fd = -1;
18 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
19 if (fd < 0) {
20 dropbear_log(LOG_ERR, "Couldn't create new file %s: %s",
21 filename, strerror(errno));
22 goto out;
25 /* write the file now */
26 while (buf->pos != buf->len) {
27 int len = write(fd, buf_getptr(buf, buf->len - buf->pos),
28 buf->len - buf->pos);
29 if (errno == EINTR) {
30 continue;
32 if (len <= 0) {
33 dropbear_log(LOG_ERR, "Failed writing file %s: %s",
34 filename, strerror(errno));
35 goto out;
37 buf_incrpos(buf, len);
40 ret = DROPBEAR_SUCCESS;
42 out:
43 if (fd >= 0) {
44 m_close(fd);
46 return ret;
49 /* returns 0 on failure */
50 static int get_default_bits(enum signkey_type keytype)
52 switch (keytype) {
53 #ifdef DROPBEAR_RSA
54 case DROPBEAR_SIGNKEY_RSA:
55 return RSA_DEFAULT_SIZE;
56 #endif
57 #ifdef DROPBEAR_DSS
58 case DROPBEAR_SIGNKEY_DSS:
59 return DSS_DEFAULT_SIZE;
60 #endif
61 #ifdef DROPBEAR_ECDSA
62 case DROPBEAR_SIGNKEY_ECDSA_KEYGEN:
63 return ECDSA_DEFAULT_SIZE;
64 case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
65 return 521;
66 case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
67 return 384;
68 case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
69 return 256;
70 #endif
71 default:
72 return 0;
76 int signkey_generate(enum signkey_type keytype, int bits, const char* filename)
78 sign_key * key = NULL;
79 buffer *buf = NULL;
80 int ret = DROPBEAR_FAILURE;
81 if (bits == 0)
83 bits = get_default_bits(keytype);
86 /* now we can generate the key */
87 key = new_sign_key();
89 seedrandom();
91 switch(keytype) {
92 #ifdef DROPBEAR_RSA
93 case DROPBEAR_SIGNKEY_RSA:
94 key->rsakey = gen_rsa_priv_key(bits);
95 break;
96 #endif
97 #ifdef DROPBEAR_DSS
98 case DROPBEAR_SIGNKEY_DSS:
99 key->dsskey = gen_dss_priv_key(bits);
100 break;
101 #endif
102 #ifdef DROPBEAR_ECDSA
103 case DROPBEAR_SIGNKEY_ECDSA_KEYGEN:
104 case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
105 case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
106 case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
108 ecc_key *ecckey = gen_ecdsa_priv_key(bits);
109 keytype = ecdsa_signkey_type(ecckey);
110 *signkey_key_ptr(key, keytype) = ecckey;
112 break;
113 #endif
114 default:
115 dropbear_exit("Internal error");
118 seedrandom();
120 buf = buf_new(MAX_PRIVKEY_SIZE);
122 buf_put_priv_key(buf, key, keytype);
123 sign_key_free(key);
124 key = NULL;
125 buf_setpos(buf, 0);
126 ret = buf_writefile(buf, filename);
128 buf_burn(buf);
129 buf_free(buf);
130 buf = NULL;
131 return ret;