dropbar: Update to version 2013.60.1.
[tomato.git] / release / src / router / dropbear / dropbearkey.c
blob9b82a777b5e36caa832fd60aaddf004988497702
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 /* The format of the keyfiles is basically a raw dump of the buffer. Data types
26 * are specified in the transport rfc 4253 - string is a 32-bit len then the
27 * non-null-terminated string, mp_int is a 32-bit len then the bignum data.
28 * The actual functions are buf_put_rsa_priv_key() and buf_put_dss_priv_key()
30 * RSA:
31 * string "ssh-rsa"
32 * mp_int e
33 * mp_int n
34 * mp_int d
35 * mp_int p (newer versions only)
36 * mp_int q (newer versions only)
38 * DSS:
39 * string "ssh-dss"
40 * mp_int p
41 * mp_int q
42 * mp_int g
43 * mp_int y
44 * mp_int x
47 #include "includes.h"
48 #include "signkey.h"
49 #include "buffer.h"
50 #include "dbutil.h"
52 #include "genrsa.h"
53 #include "gendss.h"
55 static void printhelp(char * progname);
57 #define RSA_SIZE (1024/8) /* 1024 bit */
58 #define DSS_SIZE (1024/8) /* 1024 bit */
60 static void buf_writefile(buffer * buf, const char * filename);
61 static void printpubkey(sign_key * key, int keytype);
62 static void justprintpub(const char* filename);
64 /* Print a help message */
65 static void printhelp(char * progname) {
67 fprintf(stderr, "Usage: %s -t <type> -f <filename> [-s bits]\n"
68 "-t type Type of key to generate. One of:\n"
69 #ifdef DROPBEAR_RSA
70 " rsa\n"
71 #endif
72 #ifdef DROPBEAR_DSS
73 " dss\n"
74 #endif
75 "-f filename Use filename for the secret key\n"
76 "-s bits Key size in bits, should be a multiple of 8 (optional)\n"
77 " (DSS has a fixed size of 1024 bits)\n"
78 "-y Just print the publickey and fingerprint for the\n private key in <filename>.\n"
79 #ifdef DEBUG_TRACE
80 "-v verbose\n"
81 #endif
82 ,progname);
85 #if defined(DBMULTI_dropbearkey) || !defined(DROPBEAR_MULTI)
86 #if defined(DBMULTI_dropbearkey) && defined(DROPBEAR_MULTI)
87 int dropbearkey_main(int argc, char ** argv) {
88 #else
89 int main(int argc, char ** argv) {
90 #endif
92 int i;
93 char ** next = 0;
94 sign_key *key = NULL;
95 buffer *buf = NULL;
96 char * filename = NULL;
97 int keytype = -1;
98 char * typetext = NULL;
99 char * sizetext = NULL;
100 unsigned int bits;
101 unsigned int keysize;
102 int printpub = 0;
104 /* get the commandline options */
105 for (i = 1; i < argc; i++) {
106 if (argv[i] == NULL) {
107 continue; /* Whack */
109 if (next) {
110 *next = argv[i];
111 next = NULL;
112 continue;
115 if (argv[i][0] == '-') {
116 switch (argv[i][1]) {
117 case 'f':
118 next = &filename;
119 break;
120 case 't':
121 next = &typetext;
122 break;
123 case 's':
124 next = &sizetext;
125 break;
126 case 'y':
127 printpub = 1;
128 break;
129 case 'h':
130 printhelp(argv[0]);
131 exit(EXIT_SUCCESS);
132 break;
133 #ifdef DEBUG_TRACE
134 case 'v':
135 debug_trace = 1;
136 break;
137 #endif
138 default:
139 fprintf(stderr, "Unknown argument %s\n", argv[i]);
140 printhelp(argv[0]);
141 exit(EXIT_FAILURE);
142 break;
147 if (!filename) {
148 fprintf(stderr, "Must specify a key filename\n");
149 printhelp(argv[0]);
150 exit(EXIT_FAILURE);
153 if (printpub) {
154 justprintpub(filename);
155 /* Not reached */
158 /* check/parse args */
159 if (!typetext) {
160 fprintf(stderr, "Must specify key type\n");
161 printhelp(argv[0]);
162 exit(EXIT_FAILURE);
165 if (strlen(typetext) == 3) {
166 #ifdef DROPBEAR_RSA
167 if (strncmp(typetext, "rsa", 3) == 0) {
168 keytype = DROPBEAR_SIGNKEY_RSA;
169 TRACE(("type is rsa"))
171 #endif
172 #ifdef DROPBEAR_DSS
173 if (strncmp(typetext, "dss", 3) == 0) {
174 keytype = DROPBEAR_SIGNKEY_DSS;
175 TRACE(("type is dss"))
177 #endif
179 if (keytype == -1) {
180 fprintf(stderr, "Unknown key type '%s'\n", typetext);
181 printhelp(argv[0]);
182 exit(EXIT_FAILURE);
185 if (sizetext) {
186 if (sscanf(sizetext, "%u", &bits) != 1) {
187 fprintf(stderr, "Bits must be an integer\n");
188 exit(EXIT_FAILURE);
191 if (keytype == DROPBEAR_SIGNKEY_DSS && bits != 1024) {
192 fprintf(stderr, "DSS keys have a fixed size of 1024 bits\n");
193 exit(EXIT_FAILURE);
194 } else if (bits < 512 || bits > 4096 || (bits % 8 != 0)) {
195 fprintf(stderr, "Bits must satisfy 512 <= bits <= 4096, and be a"
196 " multiple of 8\n");
197 exit(EXIT_FAILURE);
200 keysize = bits / 8;
201 } else {
202 if (keytype == DROPBEAR_SIGNKEY_DSS) {
203 keysize = DSS_SIZE;
204 } else if (keytype == DROPBEAR_SIGNKEY_RSA) {
205 keysize = RSA_SIZE;
206 } else {
207 exit(EXIT_FAILURE); /* not reached */
212 fprintf(stderr, "Will output %d bit %s secret key to '%s'\n", keysize*8,
213 typetext, filename);
215 /* don't want the file readable by others */
216 umask(077);
218 /* now we can generate the key */
219 key = new_sign_key();
221 fprintf(stderr, "Generating key, this may take a while...\n");
222 switch(keytype) {
223 #ifdef DROPBEAR_RSA
224 case DROPBEAR_SIGNKEY_RSA:
225 key->rsakey = gen_rsa_priv_key(keysize); /* 128 bytes = 1024 bit */
226 break;
227 #endif
228 #ifdef DROPBEAR_DSS
229 case DROPBEAR_SIGNKEY_DSS:
230 key->dsskey = gen_dss_priv_key(keysize); /* 128 bytes = 1024 bit */
231 break;
232 #endif
233 default:
234 fprintf(stderr, "Internal error, bad key type\n");
235 exit(EXIT_FAILURE);
238 buf = buf_new(MAX_PRIVKEY_SIZE);
240 buf_put_priv_key(buf, key, keytype);
241 buf_setpos(buf, 0);
242 buf_writefile(buf, filename);
244 buf_burn(buf);
245 buf_free(buf);
247 printpubkey(key, keytype);
249 sign_key_free(key);
251 return EXIT_SUCCESS;
253 #endif
255 static void justprintpub(const char* filename) {
257 buffer *buf = NULL;
258 sign_key *key = NULL;
259 int keytype;
260 int ret;
261 int err = DROPBEAR_FAILURE;
263 buf = buf_new(MAX_PRIVKEY_SIZE);
264 ret = buf_readfile(buf, filename);
266 if (ret != DROPBEAR_SUCCESS) {
267 fprintf(stderr, "Failed reading '%s'\n", filename);
268 goto out;
271 key = new_sign_key();
272 keytype = DROPBEAR_SIGNKEY_ANY;
274 buf_setpos(buf, 0);
275 ret = buf_get_priv_key(buf, key, &keytype);
276 if (ret == DROPBEAR_FAILURE) {
277 fprintf(stderr, "Bad key in '%s'\n", filename);
278 goto out;
281 printpubkey(key, keytype);
283 err = DROPBEAR_SUCCESS;
285 out:
286 buf_burn(buf);
287 buf_free(buf);
288 buf = NULL;
289 if (key) {
290 sign_key_free(key);
291 key = NULL;
293 exit(err);
296 static void printpubkey(sign_key * key, int keytype) {
298 buffer * buf = NULL;
299 unsigned char base64key[MAX_PUBKEY_SIZE*2];
300 unsigned long base64len;
301 int err;
302 const char * typestring = NULL;
303 char *fp = NULL;
304 int len;
305 struct passwd * pw = NULL;
306 char * username = NULL;
307 char hostname[100];
309 buf = buf_new(MAX_PUBKEY_SIZE);
310 buf_put_pub_key(buf, key, keytype);
311 buf_setpos(buf, 4);
313 len = buf->len - buf->pos;
315 base64len = sizeof(base64key);
316 err = base64_encode(buf_getptr(buf, len), len, base64key, &base64len);
318 if (err != CRYPT_OK) {
319 fprintf(stderr, "base64 failed");
322 typestring = signkey_name_from_type(keytype, &err);
324 fp = sign_key_fingerprint(buf_getptr(buf, len), len);
326 /* a user@host comment is informative */
327 username = "";
328 pw = getpwuid(getuid());
329 if (pw) {
330 username = pw->pw_name;
333 gethostname(hostname, sizeof(hostname));
334 hostname[sizeof(hostname)-1] = '\0';
336 printf("Public key portion is:\n%s %s %s@%s\nFingerprint: %s\n",
337 typestring, base64key, username, hostname, fp);
339 m_free(fp);
340 buf_free(buf);
343 /* Write a buffer to a file specified, failing if the file exists */
344 static void buf_writefile(buffer * buf, const char * filename) {
346 int fd;
347 int len;
349 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
350 if (fd < 0) {
351 fprintf(stderr, "Couldn't create new file %s\n", filename);
352 perror("Reason");
353 buf_burn(buf);
354 exit(EXIT_FAILURE);
357 /* write the file now */
358 while (buf->pos != buf->len) {
359 len = write(fd, buf_getptr(buf, buf->len - buf->pos),
360 buf->len - buf->pos);
361 if (errno == EINTR) {
362 continue;
364 if (len <= 0) {
365 fprintf(stderr, "Failed writing file '%s'\n",filename);
366 perror("Reason");
367 exit(EXIT_FAILURE);
369 buf_incrpos(buf, len);
372 close(fd);