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
;
18 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
20 dropbear_log(LOG_ERR
, "Couldn't create new file %s: %s",
21 filename
, strerror(errno
));
25 /* write the file now */
26 while (buf
->pos
!= buf
->len
) {
27 int len
= write(fd
, buf_getptr(buf
, buf
->len
- buf
->pos
),
33 dropbear_log(LOG_ERR
, "Failed writing file %s: %s",
34 filename
, strerror(errno
));
37 buf_incrpos(buf
, len
);
40 ret
= DROPBEAR_SUCCESS
;
49 /* returns 0 on failure */
50 static int get_default_bits(enum signkey_type keytype
)
54 case DROPBEAR_SIGNKEY_RSA
:
55 return RSA_DEFAULT_SIZE
;
58 case DROPBEAR_SIGNKEY_DSS
:
59 return DSS_DEFAULT_SIZE
;
62 case DROPBEAR_SIGNKEY_ECDSA_KEYGEN
:
63 return ECDSA_DEFAULT_SIZE
;
64 case DROPBEAR_SIGNKEY_ECDSA_NISTP521
:
66 case DROPBEAR_SIGNKEY_ECDSA_NISTP384
:
68 case DROPBEAR_SIGNKEY_ECDSA_NISTP256
:
76 int signkey_generate(enum signkey_type keytype
, int bits
, const char* filename
)
78 sign_key
* key
= NULL
;
80 int ret
= DROPBEAR_FAILURE
;
83 bits
= get_default_bits(keytype
);
86 /* now we can generate the key */
93 case DROPBEAR_SIGNKEY_RSA
:
94 key
->rsakey
= gen_rsa_priv_key(bits
);
98 case DROPBEAR_SIGNKEY_DSS
:
99 key
->dsskey
= gen_dss_priv_key(bits
);
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
;
115 dropbear_exit("Internal error");
120 buf
= buf_new(MAX_PRIVKEY_SIZE
);
122 buf_put_priv_key(buf
, key
, keytype
);
126 ret
= buf_writefile(buf
, filename
);