Mercurial > dropbear
comparison gensignkey.c @ 849:754d7bee1068 ecc
Merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 08 Nov 2013 23:32:13 +0800 |
parents | f4bb964c8678 |
children | 7540c0822374 |
comparison
equal
deleted
inserted
replaced
848:6c69e7df3621 | 849:754d7bee1068 |
---|---|
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 | |
9 #define RSA_DEFAULT_SIZE 2048 | |
10 #define DSS_DEFAULT_SIZE 1024 | |
11 | |
12 // Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE | |
13 static int buf_writefile(buffer * buf, const char * filename) { | |
14 int ret = DROPBEAR_FAILURE; | |
15 int fd = -1; | |
16 | |
17 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); | |
18 if (fd < 0) { | |
19 dropbear_log(LOG_ERR, "Couldn't create new file %s: %s", | |
20 filename, strerror(errno)); | |
21 goto out; | |
22 } | |
23 | |
24 /* write the file now */ | |
25 while (buf->pos != buf->len) { | |
26 int len = write(fd, buf_getptr(buf, buf->len - buf->pos), | |
27 buf->len - buf->pos); | |
28 if (errno == EINTR) { | |
29 continue; | |
30 } | |
31 if (len <= 0) { | |
32 dropbear_log(LOG_ERR, "Failed writing file %s: %s", | |
33 filename, strerror(errno)); | |
34 goto out; | |
35 } | |
36 buf_incrpos(buf, len); | |
37 } | |
38 | |
39 ret = DROPBEAR_SUCCESS; | |
40 | |
41 out: | |
42 if (fd >= 0) { | |
43 m_close(fd); | |
44 } | |
45 return ret; | |
46 } | |
47 | |
48 /* returns 0 on failure */ | |
49 static int get_default_bits(enum signkey_type keytype) | |
50 { | |
51 switch (keytype) { | |
52 #ifdef DROPBEAR_RSA | |
53 case DROPBEAR_SIGNKEY_RSA: | |
54 return RSA_DEFAULT_SIZE; | |
55 #endif | |
56 #ifdef DROPBEAR_DSS | |
57 case DROPBEAR_SIGNKEY_DSS: | |
58 return DSS_DEFAULT_SIZE; | |
59 #endif | |
60 #ifdef DROPBEAR_ECDSA | |
61 case DROPBEAR_SIGNKEY_ECDSA_KEYGEN: | |
62 return ECDSA_DEFAULT_SIZE; | |
63 case DROPBEAR_SIGNKEY_ECDSA_NISTP521: | |
64 return 521; | |
65 case DROPBEAR_SIGNKEY_ECDSA_NISTP384: | |
66 return 384; | |
67 case DROPBEAR_SIGNKEY_ECDSA_NISTP256: | |
68 return 256; | |
69 #endif | |
70 default: | |
71 return 0; | |
72 } | |
73 } | |
74 | |
75 int signkey_generate(enum signkey_type keytype, int bits, const char* filename) | |
76 { | |
77 sign_key * key = NULL; | |
78 buffer *buf = NULL; | |
79 int ret = DROPBEAR_FAILURE; | |
80 if (bits == 0) | |
81 { | |
82 bits = get_default_bits(keytype); | |
83 } | |
84 | |
85 /* now we can generate the key */ | |
86 key = new_sign_key(); | |
87 | |
88 switch(keytype) { | |
89 #ifdef DROPBEAR_RSA | |
90 case DROPBEAR_SIGNKEY_RSA: | |
91 key->rsakey = gen_rsa_priv_key(bits); | |
92 break; | |
93 #endif | |
94 #ifdef DROPBEAR_DSS | |
95 case DROPBEAR_SIGNKEY_DSS: | |
96 key->dsskey = gen_dss_priv_key(bits); | |
97 break; | |
98 #endif | |
99 #ifdef DROPBEAR_ECDSA | |
100 case DROPBEAR_SIGNKEY_ECDSA_KEYGEN: | |
101 case DROPBEAR_SIGNKEY_ECDSA_NISTP521: | |
102 case DROPBEAR_SIGNKEY_ECDSA_NISTP384: | |
103 case DROPBEAR_SIGNKEY_ECDSA_NISTP256: | |
104 { | |
105 ecc_key *ecckey = gen_ecdsa_priv_key(bits); | |
106 keytype = ecdsa_signkey_type(ecckey); | |
107 *signkey_key_ptr(key, keytype) = ecckey; | |
108 } | |
109 break; | |
110 #endif | |
111 default: | |
112 dropbear_exit("Internal error"); | |
113 } | |
114 | |
115 buf = buf_new(MAX_PRIVKEY_SIZE); | |
116 | |
117 buf_put_priv_key(buf, key, keytype); | |
118 sign_key_free(key); | |
119 key = NULL; | |
120 buf_setpos(buf, 0); | |
121 ret = buf_writefile(buf, filename); | |
122 | |
123 buf_burn(buf); | |
124 buf_free(buf); | |
125 buf = NULL; | |
126 return ret; | |
127 } |