]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: ssh-keygen.c,v 1.176 2010/01/11 10:51:07 djm Exp $ */ | |
2 | /* | |
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
4 | * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
6 | * Identity and host key generation and maintenance. | |
7 | * | |
8 | * As far as I am concerned, the code I have written for this software | |
9 | * can be used freely for any purpose. Any derived versions of this | |
10 | * software must be clearly marked as such, and if the derived work is | |
11 | * incompatible with the protocol description in the RFC file, it must be | |
12 | * called by a name other than "ssh" or "Secure Shell". | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #include <sys/types.h> | |
18 | #include <sys/socket.h> | |
19 | #include <sys/stat.h> | |
20 | #include <sys/param.h> | |
21 | ||
22 | #include <openssl/evp.h> | |
23 | #include <openssl/pem.h> | |
24 | #include "openbsd-compat/openssl-compat.h" | |
25 | ||
26 | #include <errno.h> | |
27 | #include <fcntl.h> | |
28 | #include <netdb.h> | |
29 | #ifdef HAVE_PATHS_H | |
30 | # include <paths.h> | |
31 | #endif | |
32 | #include <pwd.h> | |
33 | #include <stdarg.h> | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #include <unistd.h> | |
38 | ||
39 | #include "xmalloc.h" | |
40 | #include "key.h" | |
41 | #include "rsa.h" | |
42 | #include "authfile.h" | |
43 | #include "uuencode.h" | |
44 | #include "buffer.h" | |
45 | #include "pathnames.h" | |
46 | #include "log.h" | |
47 | #include "misc.h" | |
48 | #include "match.h" | |
49 | #include "hostfile.h" | |
50 | #include "dns.h" | |
51 | ||
52 | #ifdef SMARTCARD | |
53 | #include "scard.h" | |
54 | #endif | |
55 | ||
56 | /* Number of bits in the RSA/DSA key. This value can be set on the command line. */ | |
57 | #define DEFAULT_BITS 2048 | |
58 | #define DEFAULT_BITS_DSA 1024 | |
59 | u_int32_t bits = 0; | |
60 | ||
61 | /* | |
62 | * Flag indicating that we just want to change the passphrase. This can be | |
63 | * set on the command line. | |
64 | */ | |
65 | int change_passphrase = 0; | |
66 | ||
67 | /* | |
68 | * Flag indicating that we just want to change the comment. This can be set | |
69 | * on the command line. | |
70 | */ | |
71 | int change_comment = 0; | |
72 | ||
73 | int quiet = 0; | |
74 | ||
75 | int log_level = SYSLOG_LEVEL_INFO; | |
76 | ||
77 | /* Flag indicating that we want to hash a known_hosts file */ | |
78 | int hash_hosts = 0; | |
79 | /* Flag indicating that we want lookup a host in known_hosts file */ | |
80 | int find_host = 0; | |
81 | /* Flag indicating that we want to delete a host from a known_hosts file */ | |
82 | int delete_host = 0; | |
83 | ||
84 | /* Flag indicating that we just want to see the key fingerprint */ | |
85 | int print_fingerprint = 0; | |
86 | int print_bubblebabble = 0; | |
87 | ||
88 | /* The identity file name, given on the command line or entered by the user. */ | |
89 | char identity_file[1024]; | |
90 | int have_identity = 0; | |
91 | ||
92 | /* This is set to the passphrase if given on the command line. */ | |
93 | char *identity_passphrase = NULL; | |
94 | ||
95 | /* This is set to the new passphrase if given on the command line. */ | |
96 | char *identity_new_passphrase = NULL; | |
97 | ||
98 | /* This is set to the new comment if given on the command line. */ | |
99 | char *identity_comment = NULL; | |
100 | ||
101 | /* Dump public key file in format used by real and the original SSH 2 */ | |
102 | int convert_to_ssh2 = 0; | |
103 | int convert_from_ssh2 = 0; | |
104 | int print_public = 0; | |
105 | int print_generic = 0; | |
106 | ||
107 | char *key_type_name = NULL; | |
108 | ||
109 | /* argv0 */ | |
110 | extern char *__progname; | |
111 | ||
112 | char hostname[MAXHOSTNAMELEN]; | |
113 | ||
114 | /* moduli.c */ | |
115 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); | |
116 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t); | |
117 | ||
118 | static void | |
119 | ask_filename(struct passwd *pw, const char *prompt) | |
120 | { | |
121 | char buf[1024]; | |
122 | char *name = NULL; | |
123 | ||
124 | if (key_type_name == NULL) | |
125 | name = _PATH_SSH_CLIENT_ID_RSA; | |
126 | else { | |
127 | switch (key_type_from_name(key_type_name)) { | |
128 | case KEY_RSA1: | |
129 | name = _PATH_SSH_CLIENT_IDENTITY; | |
130 | break; | |
131 | case KEY_DSA: | |
132 | name = _PATH_SSH_CLIENT_ID_DSA; | |
133 | break; | |
134 | case KEY_RSA: | |
135 | name = _PATH_SSH_CLIENT_ID_RSA; | |
136 | break; | |
137 | default: | |
138 | fprintf(stderr, "bad key type\n"); | |
139 | exit(1); | |
140 | break; | |
141 | } | |
142 | } | |
143 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); | |
144 | fprintf(stderr, "%s (%s): ", prompt, identity_file); | |
145 | if (fgets(buf, sizeof(buf), stdin) == NULL) | |
146 | exit(1); | |
147 | buf[strcspn(buf, "\n")] = '\0'; | |
148 | if (strcmp(buf, "") != 0) | |
149 | strlcpy(identity_file, buf, sizeof(identity_file)); | |
150 | have_identity = 1; | |
151 | } | |
152 | ||
153 | static Key * | |
154 | load_identity(char *filename) | |
155 | { | |
156 | char *pass; | |
157 | Key *prv; | |
158 | ||
159 | prv = key_load_private(filename, "", NULL); | |
160 | if (prv == NULL) { | |
161 | if (identity_passphrase) | |
162 | pass = xstrdup(identity_passphrase); | |
163 | else | |
164 | pass = read_passphrase("Enter passphrase: ", | |
165 | RP_ALLOW_STDIN); | |
166 | prv = key_load_private(filename, pass, NULL); | |
167 | memset(pass, 0, strlen(pass)); | |
168 | xfree(pass); | |
169 | } | |
170 | return prv; | |
171 | } | |
172 | ||
173 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" | |
174 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" | |
175 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" | |
176 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb | |
177 | ||
178 | static void | |
179 | do_convert_to_ssh2(struct passwd *pw) | |
180 | { | |
181 | Key *k; | |
182 | u_int len; | |
183 | u_char *blob; | |
184 | char comment[61]; | |
185 | struct stat st; | |
186 | ||
187 | if (!have_identity) | |
188 | ask_filename(pw, "Enter file in which the key is"); | |
189 | if (stat(identity_file, &st) < 0) { | |
190 | perror(identity_file); | |
191 | exit(1); | |
192 | } | |
193 | if ((k = key_load_public(identity_file, NULL)) == NULL) { | |
194 | if ((k = load_identity(identity_file)) == NULL) { | |
195 | fprintf(stderr, "load failed\n"); | |
196 | exit(1); | |
197 | } | |
198 | } | |
199 | if (k->type == KEY_RSA1) { | |
200 | fprintf(stderr, "version 1 keys are not supported\n"); | |
201 | exit(1); | |
202 | } | |
203 | if (key_to_blob(k, &blob, &len) <= 0) { | |
204 | fprintf(stderr, "key_to_blob failed\n"); | |
205 | exit(1); | |
206 | } | |
207 | /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */ | |
208 | snprintf(comment, sizeof(comment), | |
209 | "%u-bit %s, converted by %s@%s from OpenSSH", | |
210 | key_size(k), key_type(k), | |
211 | pw->pw_name, hostname); | |
212 | ||
213 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); | |
214 | fprintf(stdout, "Comment: \"%s\"\n", comment); | |
215 | dump_base64(stdout, blob, len); | |
216 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); | |
217 | key_free(k); | |
218 | xfree(blob); | |
219 | exit(0); | |
220 | } | |
221 | ||
222 | static void | |
223 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) | |
224 | { | |
225 | u_int bignum_bits = buffer_get_int(b); | |
226 | u_int bytes = (bignum_bits + 7) / 8; | |
227 | ||
228 | if (buffer_len(b) < bytes) | |
229 | fatal("buffer_get_bignum_bits: input buffer too small: " | |
230 | "need %d have %d", bytes, buffer_len(b)); | |
231 | if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL) | |
232 | fatal("buffer_get_bignum_bits: BN_bin2bn failed"); | |
233 | buffer_consume(b, bytes); | |
234 | } | |
235 | ||
236 | static Key * | |
237 | do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) | |
238 | { | |
239 | Buffer b; | |
240 | Key *key = NULL; | |
241 | char *type, *cipher; | |
242 | u_char *sig, data[] = "abcde12345"; | |
243 | int magic, rlen, ktype, i1, i2, i3, i4; | |
244 | u_int slen; | |
245 | u_long e; | |
246 | ||
247 | buffer_init(&b); | |
248 | buffer_append(&b, blob, blen); | |
249 | ||
250 | magic = buffer_get_int(&b); | |
251 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { | |
252 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); | |
253 | buffer_free(&b); | |
254 | return NULL; | |
255 | } | |
256 | i1 = buffer_get_int(&b); | |
257 | type = buffer_get_string(&b, NULL); | |
258 | cipher = buffer_get_string(&b, NULL); | |
259 | i2 = buffer_get_int(&b); | |
260 | i3 = buffer_get_int(&b); | |
261 | i4 = buffer_get_int(&b); | |
262 | debug("ignore (%d %d %d %d)", i1, i2, i3, i4); | |
263 | if (strcmp(cipher, "none") != 0) { | |
264 | error("unsupported cipher %s", cipher); | |
265 | xfree(cipher); | |
266 | buffer_free(&b); | |
267 | xfree(type); | |
268 | return NULL; | |
269 | } | |
270 | xfree(cipher); | |
271 | ||
272 | if (strstr(type, "dsa")) { | |
273 | ktype = KEY_DSA; | |
274 | } else if (strstr(type, "rsa")) { | |
275 | ktype = KEY_RSA; | |
276 | } else { | |
277 | buffer_free(&b); | |
278 | xfree(type); | |
279 | return NULL; | |
280 | } | |
281 | key = key_new_private(ktype); | |
282 | xfree(type); | |
283 | ||
284 | switch (key->type) { | |
285 | case KEY_DSA: | |
286 | buffer_get_bignum_bits(&b, key->dsa->p); | |
287 | buffer_get_bignum_bits(&b, key->dsa->g); | |
288 | buffer_get_bignum_bits(&b, key->dsa->q); | |
289 | buffer_get_bignum_bits(&b, key->dsa->pub_key); | |
290 | buffer_get_bignum_bits(&b, key->dsa->priv_key); | |
291 | break; | |
292 | case KEY_RSA: | |
293 | e = buffer_get_char(&b); | |
294 | debug("e %lx", e); | |
295 | if (e < 30) { | |
296 | e <<= 8; | |
297 | e += buffer_get_char(&b); | |
298 | debug("e %lx", e); | |
299 | e <<= 8; | |
300 | e += buffer_get_char(&b); | |
301 | debug("e %lx", e); | |
302 | } | |
303 | if (!BN_set_word(key->rsa->e, e)) { | |
304 | buffer_free(&b); | |
305 | key_free(key); | |
306 | return NULL; | |
307 | } | |
308 | buffer_get_bignum_bits(&b, key->rsa->d); | |
309 | buffer_get_bignum_bits(&b, key->rsa->n); | |
310 | buffer_get_bignum_bits(&b, key->rsa->iqmp); | |
311 | buffer_get_bignum_bits(&b, key->rsa->q); | |
312 | buffer_get_bignum_bits(&b, key->rsa->p); | |
313 | rsa_generate_additional_parameters(key->rsa); | |
314 | break; | |
315 | } | |
316 | rlen = buffer_len(&b); | |
317 | if (rlen != 0) | |
318 | error("do_convert_private_ssh2_from_blob: " | |
319 | "remaining bytes in key blob %d", rlen); | |
320 | buffer_free(&b); | |
321 | ||
322 | /* try the key */ | |
323 | key_sign(key, &sig, &slen, data, sizeof(data)); | |
324 | key_verify(key, sig, slen, data, sizeof(data)); | |
325 | xfree(sig); | |
326 | return key; | |
327 | } | |
328 | ||
329 | static int | |
330 | get_line(FILE *fp, char *line, size_t len) | |
331 | { | |
332 | int c; | |
333 | size_t pos = 0; | |
334 | ||
335 | line[0] = '\0'; | |
336 | while ((c = fgetc(fp)) != EOF) { | |
337 | if (pos >= len - 1) { | |
338 | fprintf(stderr, "input line too long.\n"); | |
339 | exit(1); | |
340 | } | |
341 | switch (c) { | |
342 | case '\r': | |
343 | c = fgetc(fp); | |
344 | if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) { | |
345 | fprintf(stderr, "unget: %s\n", strerror(errno)); | |
346 | exit(1); | |
347 | } | |
348 | return pos; | |
349 | case '\n': | |
350 | return pos; | |
351 | } | |
352 | line[pos++] = c; | |
353 | line[pos] = '\0'; | |
354 | } | |
355 | /* We reached EOF */ | |
356 | return -1; | |
357 | } | |
358 | ||
359 | static void | |
360 | do_convert_from_ssh2(struct passwd *pw) | |
361 | { | |
362 | Key *k; | |
363 | int blen; | |
364 | u_int len; | |
365 | char line[1024]; | |
366 | u_char blob[8096]; | |
367 | char encoded[8096]; | |
368 | struct stat st; | |
369 | int escaped = 0, private = 0, ok; | |
370 | FILE *fp; | |
371 | ||
372 | if (!have_identity) | |
373 | ask_filename(pw, "Enter file in which the key is"); | |
374 | if (stat(identity_file, &st) < 0) { | |
375 | perror(identity_file); | |
376 | exit(1); | |
377 | } | |
378 | fp = fopen(identity_file, "r"); | |
379 | if (fp == NULL) { | |
380 | perror(identity_file); | |
381 | exit(1); | |
382 | } | |
383 | encoded[0] = '\0'; | |
384 | while ((blen = get_line(fp, line, sizeof(line))) != -1) { | |
385 | if (line[blen - 1] == '\\') | |
386 | escaped++; | |
387 | if (strncmp(line, "----", 4) == 0 || | |
388 | strstr(line, ": ") != NULL) { | |
389 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) | |
390 | private = 1; | |
391 | if (strstr(line, " END ") != NULL) { | |
392 | break; | |
393 | } | |
394 | /* fprintf(stderr, "ignore: %s", line); */ | |
395 | continue; | |
396 | } | |
397 | if (escaped) { | |
398 | escaped--; | |
399 | /* fprintf(stderr, "escaped: %s", line); */ | |
400 | continue; | |
401 | } | |
402 | strlcat(encoded, line, sizeof(encoded)); | |
403 | } | |
404 | len = strlen(encoded); | |
405 | if (((len % 4) == 3) && | |
406 | (encoded[len-1] == '=') && | |
407 | (encoded[len-2] == '=') && | |
408 | (encoded[len-3] == '=')) | |
409 | encoded[len-3] = '\0'; | |
410 | blen = uudecode(encoded, blob, sizeof(blob)); | |
411 | if (blen < 0) { | |
412 | fprintf(stderr, "uudecode failed.\n"); | |
413 | exit(1); | |
414 | } | |
415 | k = private ? | |
416 | do_convert_private_ssh2_from_blob(blob, blen) : | |
417 | key_from_blob(blob, blen); | |
418 | if (k == NULL) { | |
419 | fprintf(stderr, "decode blob failed.\n"); | |
420 | exit(1); | |
421 | } | |
422 | ok = private ? | |
423 | (k->type == KEY_DSA ? | |
424 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : | |
425 | PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) : | |
426 | key_write(k, stdout); | |
427 | if (!ok) { | |
428 | fprintf(stderr, "key write failed\n"); | |
429 | exit(1); | |
430 | } | |
431 | key_free(k); | |
432 | if (!private) | |
433 | fprintf(stdout, "\n"); | |
434 | fclose(fp); | |
435 | exit(0); | |
436 | } | |
437 | ||
438 | static void | |
439 | do_print_public(struct passwd *pw) | |
440 | { | |
441 | Key *prv; | |
442 | struct stat st; | |
443 | ||
444 | if (!have_identity) | |
445 | ask_filename(pw, "Enter file in which the key is"); | |
446 | if (stat(identity_file, &st) < 0) { | |
447 | perror(identity_file); | |
448 | exit(1); | |
449 | } | |
450 | prv = load_identity(identity_file); | |
451 | if (prv == NULL) { | |
452 | fprintf(stderr, "load failed\n"); | |
453 | exit(1); | |
454 | } | |
455 | if (!key_write(prv, stdout)) | |
456 | fprintf(stderr, "key_write failed"); | |
457 | key_free(prv); | |
458 | fprintf(stdout, "\n"); | |
459 | exit(0); | |
460 | } | |
461 | ||
462 | #ifdef SMARTCARD | |
463 | static void | |
464 | do_upload(struct passwd *pw, const char *sc_reader_id) | |
465 | { | |
466 | Key *prv = NULL; | |
467 | struct stat st; | |
468 | int ret; | |
469 | ||
470 | if (!have_identity) | |
471 | ask_filename(pw, "Enter file in which the key is"); | |
472 | if (stat(identity_file, &st) < 0) { | |
473 | perror(identity_file); | |
474 | exit(1); | |
475 | } | |
476 | prv = load_identity(identity_file); | |
477 | if (prv == NULL) { | |
478 | error("load failed"); | |
479 | exit(1); | |
480 | } | |
481 | ret = sc_put_key(prv, sc_reader_id); | |
482 | key_free(prv); | |
483 | if (ret < 0) | |
484 | exit(1); | |
485 | logit("loading key done"); | |
486 | exit(0); | |
487 | } | |
488 | ||
489 | static void | |
490 | do_download(struct passwd *pw, const char *sc_reader_id) | |
491 | { | |
492 | Key **keys = NULL; | |
493 | int i; | |
494 | ||
495 | keys = sc_get_keys(sc_reader_id, NULL); | |
496 | if (keys == NULL) | |
497 | fatal("cannot read public key from smartcard"); | |
498 | for (i = 0; keys[i]; i++) { | |
499 | key_write(keys[i], stdout); | |
500 | key_free(keys[i]); | |
501 | fprintf(stdout, "\n"); | |
502 | } | |
503 | xfree(keys); | |
504 | exit(0); | |
505 | } | |
506 | #endif /* SMARTCARD */ | |
507 | ||
508 | static void | |
509 | do_fingerprint(struct passwd *pw) | |
510 | { | |
511 | FILE *f; | |
512 | Key *public; | |
513 | char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra; | |
514 | int i, skip = 0, num = 0, invalid = 1; | |
515 | enum fp_rep rep; | |
516 | enum fp_type fptype; | |
517 | struct stat st; | |
518 | ||
519 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; | |
520 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; | |
521 | ||
522 | if (!have_identity) | |
523 | ask_filename(pw, "Enter file in which the key is"); | |
524 | if (stat(identity_file, &st) < 0) { | |
525 | perror(identity_file); | |
526 | exit(1); | |
527 | } | |
528 | public = key_load_public(identity_file, &comment); | |
529 | if (public != NULL) { | |
530 | fp = key_fingerprint(public, fptype, rep); | |
531 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); | |
532 | printf("%u %s %s (%s)\n", key_size(public), fp, comment, | |
533 | key_type(public)); | |
534 | if (log_level >= SYSLOG_LEVEL_VERBOSE) | |
535 | printf("%s\n", ra); | |
536 | key_free(public); | |
537 | xfree(comment); | |
538 | xfree(ra); | |
539 | xfree(fp); | |
540 | exit(0); | |
541 | } | |
542 | if (comment) { | |
543 | xfree(comment); | |
544 | comment = NULL; | |
545 | } | |
546 | ||
547 | f = fopen(identity_file, "r"); | |
548 | if (f != NULL) { | |
549 | while (fgets(line, sizeof(line), f)) { | |
550 | if ((cp = strchr(line, '\n')) == NULL) { | |
551 | error("line %d too long: %.40s...", | |
552 | num + 1, line); | |
553 | skip = 1; | |
554 | continue; | |
555 | } | |
556 | num++; | |
557 | if (skip) { | |
558 | skip = 0; | |
559 | continue; | |
560 | } | |
561 | *cp = '\0'; | |
562 | ||
563 | /* Skip leading whitespace, empty and comment lines. */ | |
564 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
565 | ; | |
566 | if (!*cp || *cp == '\n' || *cp == '#') | |
567 | continue; | |
568 | i = strtol(cp, &ep, 10); | |
569 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { | |
570 | int quoted = 0; | |
571 | comment = cp; | |
572 | for (; *cp && (quoted || (*cp != ' ' && | |
573 | *cp != '\t')); cp++) { | |
574 | if (*cp == '\\' && cp[1] == '"') | |
575 | cp++; /* Skip both */ | |
576 | else if (*cp == '"') | |
577 | quoted = !quoted; | |
578 | } | |
579 | if (!*cp) | |
580 | continue; | |
581 | *cp++ = '\0'; | |
582 | } | |
583 | ep = cp; | |
584 | public = key_new(KEY_RSA1); | |
585 | if (key_read(public, &cp) != 1) { | |
586 | cp = ep; | |
587 | key_free(public); | |
588 | public = key_new(KEY_UNSPEC); | |
589 | if (key_read(public, &cp) != 1) { | |
590 | key_free(public); | |
591 | continue; | |
592 | } | |
593 | } | |
594 | comment = *cp ? cp : comment; | |
595 | fp = key_fingerprint(public, fptype, rep); | |
596 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); | |
597 | printf("%u %s %s (%s)\n", key_size(public), fp, | |
598 | comment ? comment : "no comment", key_type(public)); | |
599 | if (log_level >= SYSLOG_LEVEL_VERBOSE) | |
600 | printf("%s\n", ra); | |
601 | xfree(ra); | |
602 | xfree(fp); | |
603 | key_free(public); | |
604 | invalid = 0; | |
605 | } | |
606 | fclose(f); | |
607 | } | |
608 | if (invalid) { | |
609 | printf("%s is not a public key file.\n", identity_file); | |
610 | exit(1); | |
611 | } | |
612 | exit(0); | |
613 | } | |
614 | ||
615 | static void | |
616 | print_host(FILE *f, const char *name, Key *public, int hash) | |
617 | { | |
618 | if (print_fingerprint) { | |
619 | enum fp_rep rep; | |
620 | enum fp_type fptype; | |
621 | char *fp, *ra; | |
622 | ||
623 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; | |
624 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; | |
625 | fp = key_fingerprint(public, fptype, rep); | |
626 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); | |
627 | printf("%u %s %s (%s)\n", key_size(public), fp, name, | |
628 | key_type(public)); | |
629 | if (log_level >= SYSLOG_LEVEL_VERBOSE) | |
630 | printf("%s\n", ra); | |
631 | xfree(ra); | |
632 | xfree(fp); | |
633 | } else { | |
634 | if (hash && (name = host_hash(name, NULL, 0)) == NULL) | |
635 | fatal("hash_host failed"); | |
636 | fprintf(f, "%s ", name); | |
637 | if (!key_write(public, f)) | |
638 | fatal("key_write failed"); | |
639 | fprintf(f, "\n"); | |
640 | } | |
641 | } | |
642 | ||
643 | static void | |
644 | do_known_hosts(struct passwd *pw, const char *name) | |
645 | { | |
646 | FILE *in, *out = stdout; | |
647 | Key *public; | |
648 | char *cp, *cp2, *kp, *kp2; | |
649 | char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN]; | |
650 | int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0; | |
651 | ||
652 | if (!have_identity) { | |
653 | cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid); | |
654 | if (strlcpy(identity_file, cp, sizeof(identity_file)) >= | |
655 | sizeof(identity_file)) | |
656 | fatal("Specified known hosts path too long"); | |
657 | xfree(cp); | |
658 | have_identity = 1; | |
659 | } | |
660 | if ((in = fopen(identity_file, "r")) == NULL) | |
661 | fatal("fopen: %s", strerror(errno)); | |
662 | ||
663 | /* | |
664 | * Find hosts goes to stdout, hash and deletions happen in-place | |
665 | * A corner case is ssh-keygen -HF foo, which should go to stdout | |
666 | */ | |
667 | if (!find_host && (hash_hosts || delete_host)) { | |
668 | if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) || | |
669 | strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) || | |
670 | strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) || | |
671 | strlcat(old, ".old", sizeof(old)) >= sizeof(old)) | |
672 | fatal("known_hosts path too long"); | |
673 | umask(077); | |
674 | if ((c = mkstemp(tmp)) == -1) | |
675 | fatal("mkstemp: %s", strerror(errno)); | |
676 | if ((out = fdopen(c, "w")) == NULL) { | |
677 | c = errno; | |
678 | unlink(tmp); | |
679 | fatal("fdopen: %s", strerror(c)); | |
680 | } | |
681 | inplace = 1; | |
682 | } | |
683 | ||
684 | while (fgets(line, sizeof(line), in)) { | |
685 | if ((cp = strchr(line, '\n')) == NULL) { | |
686 | error("line %d too long: %.40s...", num + 1, line); | |
687 | skip = 1; | |
688 | invalid = 1; | |
689 | continue; | |
690 | } | |
691 | num++; | |
692 | if (skip) { | |
693 | skip = 0; | |
694 | continue; | |
695 | } | |
696 | *cp = '\0'; | |
697 | ||
698 | /* Skip leading whitespace, empty and comment lines. */ | |
699 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
700 | ; | |
701 | if (!*cp || *cp == '\n' || *cp == '#') { | |
702 | if (inplace) | |
703 | fprintf(out, "%s\n", cp); | |
704 | continue; | |
705 | } | |
706 | /* Find the end of the host name portion. */ | |
707 | for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++) | |
708 | ; | |
709 | if (*kp == '\0' || *(kp + 1) == '\0') { | |
710 | error("line %d missing key: %.40s...", | |
711 | num, line); | |
712 | invalid = 1; | |
713 | continue; | |
714 | } | |
715 | *kp++ = '\0'; | |
716 | kp2 = kp; | |
717 | ||
718 | public = key_new(KEY_RSA1); | |
719 | if (key_read(public, &kp) != 1) { | |
720 | kp = kp2; | |
721 | key_free(public); | |
722 | public = key_new(KEY_UNSPEC); | |
723 | if (key_read(public, &kp) != 1) { | |
724 | error("line %d invalid key: %.40s...", | |
725 | num, line); | |
726 | key_free(public); | |
727 | invalid = 1; | |
728 | continue; | |
729 | } | |
730 | } | |
731 | ||
732 | if (*cp == HASH_DELIM) { | |
733 | if (find_host || delete_host) { | |
734 | cp2 = host_hash(name, cp, strlen(cp)); | |
735 | if (cp2 == NULL) { | |
736 | error("line %d: invalid hashed " | |
737 | "name: %.64s...", num, line); | |
738 | invalid = 1; | |
739 | continue; | |
740 | } | |
741 | c = (strcmp(cp2, cp) == 0); | |
742 | if (find_host && c) { | |
743 | printf("# Host %s found: " | |
744 | "line %d type %s\n", name, | |
745 | num, key_type(public)); | |
746 | print_host(out, cp, public, 0); | |
747 | } | |
748 | if (delete_host && !c) | |
749 | print_host(out, cp, public, 0); | |
750 | } else if (hash_hosts) | |
751 | print_host(out, cp, public, 0); | |
752 | } else { | |
753 | if (find_host || delete_host) { | |
754 | c = (match_hostname(name, cp, | |
755 | strlen(cp)) == 1); | |
756 | if (find_host && c) { | |
757 | printf("# Host %s found: " | |
758 | "line %d type %s\n", name, | |
759 | num, key_type(public)); | |
760 | print_host(out, name, public, | |
761 | hash_hosts); | |
762 | } | |
763 | if (delete_host && !c) | |
764 | print_host(out, cp, public, 0); | |
765 | } else if (hash_hosts) { | |
766 | for (cp2 = strsep(&cp, ","); | |
767 | cp2 != NULL && *cp2 != '\0'; | |
768 | cp2 = strsep(&cp, ",")) { | |
769 | if (strcspn(cp2, "*?!") != strlen(cp2)) | |
770 | fprintf(stderr, "Warning: " | |
771 | "ignoring host name with " | |
772 | "metacharacters: %.64s\n", | |
773 | cp2); | |
774 | else | |
775 | print_host(out, cp2, public, 1); | |
776 | } | |
777 | has_unhashed = 1; | |
778 | } | |
779 | } | |
780 | key_free(public); | |
781 | } | |
782 | fclose(in); | |
783 | ||
784 | if (invalid) { | |
785 | fprintf(stderr, "%s is not a valid known_hosts file.\n", | |
786 | identity_file); | |
787 | if (inplace) { | |
788 | fprintf(stderr, "Not replacing existing known_hosts " | |
789 | "file because of errors\n"); | |
790 | fclose(out); | |
791 | unlink(tmp); | |
792 | } | |
793 | exit(1); | |
794 | } | |
795 | ||
796 | if (inplace) { | |
797 | fclose(out); | |
798 | ||
799 | /* Backup existing file */ | |
800 | if (unlink(old) == -1 && errno != ENOENT) | |
801 | fatal("unlink %.100s: %s", old, strerror(errno)); | |
802 | if (link(identity_file, old) == -1) | |
803 | fatal("link %.100s to %.100s: %s", identity_file, old, | |
804 | strerror(errno)); | |
805 | /* Move new one into place */ | |
806 | if (rename(tmp, identity_file) == -1) { | |
807 | error("rename\"%s\" to \"%s\": %s", tmp, identity_file, | |
808 | strerror(errno)); | |
809 | unlink(tmp); | |
810 | unlink(old); | |
811 | exit(1); | |
812 | } | |
813 | ||
814 | fprintf(stderr, "%s updated.\n", identity_file); | |
815 | fprintf(stderr, "Original contents retained as %s\n", old); | |
816 | if (has_unhashed) { | |
817 | fprintf(stderr, "WARNING: %s contains unhashed " | |
818 | "entries\n", old); | |
819 | fprintf(stderr, "Delete this file to ensure privacy " | |
820 | "of hostnames\n"); | |
821 | } | |
822 | } | |
823 | ||
824 | exit(0); | |
825 | } | |
826 | ||
827 | /* | |
828 | * Perform changing a passphrase. The argument is the passwd structure | |
829 | * for the current user. | |
830 | */ | |
831 | static void | |
832 | do_change_passphrase(struct passwd *pw) | |
833 | { | |
834 | char *comment; | |
835 | char *old_passphrase, *passphrase1, *passphrase2; | |
836 | struct stat st; | |
837 | Key *private; | |
838 | ||
839 | if (!have_identity) | |
840 | ask_filename(pw, "Enter file in which the key is"); | |
841 | if (stat(identity_file, &st) < 0) { | |
842 | perror(identity_file); | |
843 | exit(1); | |
844 | } | |
845 | /* Try to load the file with empty passphrase. */ | |
846 | private = key_load_private(identity_file, "", &comment); | |
847 | if (private == NULL) { | |
848 | if (identity_passphrase) | |
849 | old_passphrase = xstrdup(identity_passphrase); | |
850 | else | |
851 | old_passphrase = | |
852 | read_passphrase("Enter old passphrase: ", | |
853 | RP_ALLOW_STDIN); | |
854 | private = key_load_private(identity_file, old_passphrase, | |
855 | &comment); | |
856 | memset(old_passphrase, 0, strlen(old_passphrase)); | |
857 | xfree(old_passphrase); | |
858 | if (private == NULL) { | |
859 | printf("Bad passphrase.\n"); | |
860 | exit(1); | |
861 | } | |
862 | } | |
863 | printf("Key has comment '%s'\n", comment); | |
864 | ||
865 | /* Ask the new passphrase (twice). */ | |
866 | if (identity_new_passphrase) { | |
867 | passphrase1 = xstrdup(identity_new_passphrase); | |
868 | passphrase2 = NULL; | |
869 | } else { | |
870 | passphrase1 = | |
871 | read_passphrase("Enter new passphrase (empty for no " | |
872 | "passphrase): ", RP_ALLOW_STDIN); | |
873 | passphrase2 = read_passphrase("Enter same passphrase again: ", | |
874 | RP_ALLOW_STDIN); | |
875 | ||
876 | /* Verify that they are the same. */ | |
877 | if (strcmp(passphrase1, passphrase2) != 0) { | |
878 | memset(passphrase1, 0, strlen(passphrase1)); | |
879 | memset(passphrase2, 0, strlen(passphrase2)); | |
880 | xfree(passphrase1); | |
881 | xfree(passphrase2); | |
882 | printf("Pass phrases do not match. Try again.\n"); | |
883 | exit(1); | |
884 | } | |
885 | /* Destroy the other copy. */ | |
886 | memset(passphrase2, 0, strlen(passphrase2)); | |
887 | xfree(passphrase2); | |
888 | } | |
889 | ||
890 | /* Save the file using the new passphrase. */ | |
891 | if (!key_save_private(private, identity_file, passphrase1, comment)) { | |
892 | printf("Saving the key failed: %s.\n", identity_file); | |
893 | memset(passphrase1, 0, strlen(passphrase1)); | |
894 | xfree(passphrase1); | |
895 | key_free(private); | |
896 | xfree(comment); | |
897 | exit(1); | |
898 | } | |
899 | /* Destroy the passphrase and the copy of the key in memory. */ | |
900 | memset(passphrase1, 0, strlen(passphrase1)); | |
901 | xfree(passphrase1); | |
902 | key_free(private); /* Destroys contents */ | |
903 | xfree(comment); | |
904 | ||
905 | printf("Your identification has been saved with the new passphrase.\n"); | |
906 | exit(0); | |
907 | } | |
908 | ||
909 | /* | |
910 | * Print the SSHFP RR. | |
911 | */ | |
912 | static int | |
913 | do_print_resource_record(struct passwd *pw, char *fname, char *hname) | |
914 | { | |
915 | Key *public; | |
916 | char *comment = NULL; | |
917 | struct stat st; | |
918 | ||
919 | if (fname == NULL) | |
920 | ask_filename(pw, "Enter file in which the key is"); | |
921 | if (stat(fname, &st) < 0) { | |
922 | if (errno == ENOENT) | |
923 | return 0; | |
924 | perror(fname); | |
925 | exit(1); | |
926 | } | |
927 | public = key_load_public(fname, &comment); | |
928 | if (public != NULL) { | |
929 | export_dns_rr(hname, public, stdout, print_generic); | |
930 | key_free(public); | |
931 | xfree(comment); | |
932 | return 1; | |
933 | } | |
934 | if (comment) | |
935 | xfree(comment); | |
936 | ||
937 | printf("failed to read v2 public key from %s.\n", fname); | |
938 | exit(1); | |
939 | } | |
940 | ||
941 | /* | |
942 | * Change the comment of a private key file. | |
943 | */ | |
944 | static void | |
945 | do_change_comment(struct passwd *pw) | |
946 | { | |
947 | char new_comment[1024], *comment, *passphrase; | |
948 | Key *private; | |
949 | Key *public; | |
950 | struct stat st; | |
951 | FILE *f; | |
952 | int fd; | |
953 | ||
954 | if (!have_identity) | |
955 | ask_filename(pw, "Enter file in which the key is"); | |
956 | if (stat(identity_file, &st) < 0) { | |
957 | perror(identity_file); | |
958 | exit(1); | |
959 | } | |
960 | private = key_load_private(identity_file, "", &comment); | |
961 | if (private == NULL) { | |
962 | if (identity_passphrase) | |
963 | passphrase = xstrdup(identity_passphrase); | |
964 | else if (identity_new_passphrase) | |
965 | passphrase = xstrdup(identity_new_passphrase); | |
966 | else | |
967 | passphrase = read_passphrase("Enter passphrase: ", | |
968 | RP_ALLOW_STDIN); | |
969 | /* Try to load using the passphrase. */ | |
970 | private = key_load_private(identity_file, passphrase, &comment); | |
971 | if (private == NULL) { | |
972 | memset(passphrase, 0, strlen(passphrase)); | |
973 | xfree(passphrase); | |
974 | printf("Bad passphrase.\n"); | |
975 | exit(1); | |
976 | } | |
977 | } else { | |
978 | passphrase = xstrdup(""); | |
979 | } | |
980 | if (private->type != KEY_RSA1) { | |
981 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); | |
982 | key_free(private); | |
983 | exit(1); | |
984 | } | |
985 | printf("Key now has comment '%s'\n", comment); | |
986 | ||
987 | if (identity_comment) { | |
988 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); | |
989 | } else { | |
990 | printf("Enter new comment: "); | |
991 | fflush(stdout); | |
992 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { | |
993 | memset(passphrase, 0, strlen(passphrase)); | |
994 | key_free(private); | |
995 | exit(1); | |
996 | } | |
997 | new_comment[strcspn(new_comment, "\n")] = '\0'; | |
998 | } | |
999 | ||
1000 | /* Save the file using the new passphrase. */ | |
1001 | if (!key_save_private(private, identity_file, passphrase, new_comment)) { | |
1002 | printf("Saving the key failed: %s.\n", identity_file); | |
1003 | memset(passphrase, 0, strlen(passphrase)); | |
1004 | xfree(passphrase); | |
1005 | key_free(private); | |
1006 | xfree(comment); | |
1007 | exit(1); | |
1008 | } | |
1009 | memset(passphrase, 0, strlen(passphrase)); | |
1010 | xfree(passphrase); | |
1011 | public = key_from_private(private); | |
1012 | key_free(private); | |
1013 | ||
1014 | strlcat(identity_file, ".pub", sizeof(identity_file)); | |
1015 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
1016 | if (fd == -1) { | |
1017 | printf("Could not save your public key in %s\n", identity_file); | |
1018 | exit(1); | |
1019 | } | |
1020 | f = fdopen(fd, "w"); | |
1021 | if (f == NULL) { | |
1022 | printf("fdopen %s failed\n", identity_file); | |
1023 | exit(1); | |
1024 | } | |
1025 | if (!key_write(public, f)) | |
1026 | fprintf(stderr, "write key failed\n"); | |
1027 | key_free(public); | |
1028 | fprintf(f, " %s\n", new_comment); | |
1029 | fclose(f); | |
1030 | ||
1031 | xfree(comment); | |
1032 | ||
1033 | printf("The comment in your key file has been changed.\n"); | |
1034 | exit(0); | |
1035 | } | |
1036 | ||
1037 | static void | |
1038 | usage(void) | |
1039 | { | |
1040 | fprintf(stderr, "usage: %s [options]\n", __progname); | |
1041 | fprintf(stderr, "Options:\n"); | |
1042 | fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n"); | |
1043 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); | |
1044 | fprintf(stderr, " -b bits Number of bits in the key to create.\n"); | |
1045 | fprintf(stderr, " -C comment Provide new comment.\n"); | |
1046 | fprintf(stderr, " -c Change comment in private and public key files.\n"); | |
1047 | #ifdef SMARTCARD | |
1048 | fprintf(stderr, " -D reader Download public key from smartcard.\n"); | |
1049 | #endif /* SMARTCARD */ | |
1050 | fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n"); | |
1051 | fprintf(stderr, " -F hostname Find hostname in known hosts file.\n"); | |
1052 | fprintf(stderr, " -f filename Filename of the key file.\n"); | |
1053 | fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n"); | |
1054 | fprintf(stderr, " -g Use generic DNS resource record format.\n"); | |
1055 | fprintf(stderr, " -H Hash names in known_hosts file.\n"); | |
1056 | fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n"); | |
1057 | fprintf(stderr, " -l Show fingerprint of key file.\n"); | |
1058 | fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n"); | |
1059 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); | |
1060 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); | |
1061 | fprintf(stderr, " -p Change passphrase of private key file.\n"); | |
1062 | fprintf(stderr, " -q Quiet.\n"); | |
1063 | fprintf(stderr, " -R hostname Remove host from known_hosts file.\n"); | |
1064 | fprintf(stderr, " -r hostname Print DNS resource record.\n"); | |
1065 | fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n"); | |
1066 | fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n"); | |
1067 | fprintf(stderr, " -t type Specify type of key to create.\n"); | |
1068 | #ifdef SMARTCARD | |
1069 | fprintf(stderr, " -U reader Upload private key to smartcard.\n"); | |
1070 | #endif /* SMARTCARD */ | |
1071 | fprintf(stderr, " -v Verbose.\n"); | |
1072 | fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n"); | |
1073 | fprintf(stderr, " -y Read private key file and print public key.\n"); | |
1074 | ||
1075 | exit(1); | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * Main program for key management. | |
1080 | */ | |
1081 | int | |
1082 | main(int argc, char **argv) | |
1083 | { | |
1084 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; | |
1085 | char out_file[MAXPATHLEN], *reader_id = NULL; | |
1086 | char *rr_hostname = NULL; | |
1087 | Key *private, *public; | |
1088 | struct passwd *pw; | |
1089 | struct stat st; | |
1090 | int opt, type, fd, download = 0; | |
1091 | u_int32_t memory = 0, generator_wanted = 0, trials = 100; | |
1092 | int do_gen_candidates = 0, do_screen_candidates = 0; | |
1093 | BIGNUM *start = NULL; | |
1094 | FILE *f; | |
1095 | const char *errstr; | |
1096 | ||
1097 | extern int optind; | |
1098 | extern char *optarg; | |
1099 | ||
1100 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ | |
1101 | sanitise_stdfd(); | |
1102 | ||
1103 | __progname = ssh_get_progname(argv[0]); | |
1104 | ||
1105 | SSLeay_add_all_algorithms(); | |
1106 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); | |
1107 | ||
1108 | init_rng(); | |
1109 | seed_rng(); | |
1110 | ||
1111 | /* we need this for the home * directory. */ | |
1112 | pw = getpwuid(getuid()); | |
1113 | if (!pw) { | |
1114 | printf("You don't exist, go away!\n"); | |
1115 | exit(1); | |
1116 | } | |
1117 | if (gethostname(hostname, sizeof(hostname)) < 0) { | |
1118 | perror("gethostname"); | |
1119 | exit(1); | |
1120 | } | |
1121 | ||
1122 | while ((opt = getopt(argc, argv, | |
1123 | "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) { | |
1124 | switch (opt) { | |
1125 | case 'b': | |
1126 | bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr); | |
1127 | if (errstr) | |
1128 | fatal("Bits has bad value %s (%s)", | |
1129 | optarg, errstr); | |
1130 | break; | |
1131 | case 'F': | |
1132 | find_host = 1; | |
1133 | rr_hostname = optarg; | |
1134 | break; | |
1135 | case 'H': | |
1136 | hash_hosts = 1; | |
1137 | break; | |
1138 | case 'R': | |
1139 | delete_host = 1; | |
1140 | rr_hostname = optarg; | |
1141 | break; | |
1142 | case 'l': | |
1143 | print_fingerprint = 1; | |
1144 | break; | |
1145 | case 'B': | |
1146 | print_bubblebabble = 1; | |
1147 | break; | |
1148 | case 'p': | |
1149 | change_passphrase = 1; | |
1150 | break; | |
1151 | case 'c': | |
1152 | change_comment = 1; | |
1153 | break; | |
1154 | case 'f': | |
1155 | if (strlcpy(identity_file, optarg, sizeof(identity_file)) >= | |
1156 | sizeof(identity_file)) | |
1157 | fatal("Identity filename too long"); | |
1158 | have_identity = 1; | |
1159 | break; | |
1160 | case 'g': | |
1161 | print_generic = 1; | |
1162 | break; | |
1163 | case 'P': | |
1164 | identity_passphrase = optarg; | |
1165 | break; | |
1166 | case 'N': | |
1167 | identity_new_passphrase = optarg; | |
1168 | break; | |
1169 | case 'C': | |
1170 | identity_comment = optarg; | |
1171 | break; | |
1172 | case 'q': | |
1173 | quiet = 1; | |
1174 | break; | |
1175 | case 'e': | |
1176 | case 'x': | |
1177 | /* export key */ | |
1178 | convert_to_ssh2 = 1; | |
1179 | break; | |
1180 | case 'i': | |
1181 | case 'X': | |
1182 | /* import key */ | |
1183 | convert_from_ssh2 = 1; | |
1184 | break; | |
1185 | case 'y': | |
1186 | print_public = 1; | |
1187 | break; | |
1188 | case 'd': | |
1189 | key_type_name = "dsa"; | |
1190 | break; | |
1191 | case 't': | |
1192 | key_type_name = optarg; | |
1193 | break; | |
1194 | case 'D': | |
1195 | download = 1; | |
1196 | /*FALLTHROUGH*/ | |
1197 | case 'U': | |
1198 | reader_id = optarg; | |
1199 | break; | |
1200 | case 'v': | |
1201 | if (log_level == SYSLOG_LEVEL_INFO) | |
1202 | log_level = SYSLOG_LEVEL_DEBUG1; | |
1203 | else { | |
1204 | if (log_level >= SYSLOG_LEVEL_DEBUG1 && | |
1205 | log_level < SYSLOG_LEVEL_DEBUG3) | |
1206 | log_level++; | |
1207 | } | |
1208 | break; | |
1209 | case 'r': | |
1210 | rr_hostname = optarg; | |
1211 | break; | |
1212 | case 'W': | |
1213 | generator_wanted = (u_int32_t)strtonum(optarg, 1, | |
1214 | UINT_MAX, &errstr); | |
1215 | if (errstr) | |
1216 | fatal("Desired generator has bad value: %s (%s)", | |
1217 | optarg, errstr); | |
1218 | break; | |
1219 | case 'a': | |
1220 | trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); | |
1221 | if (errstr) | |
1222 | fatal("Invalid number of trials: %s (%s)", | |
1223 | optarg, errstr); | |
1224 | break; | |
1225 | case 'M': | |
1226 | memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); | |
1227 | if (errstr) { | |
1228 | fatal("Memory limit is %s: %s", errstr, optarg); | |
1229 | } | |
1230 | break; | |
1231 | case 'G': | |
1232 | do_gen_candidates = 1; | |
1233 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= | |
1234 | sizeof(out_file)) | |
1235 | fatal("Output filename too long"); | |
1236 | break; | |
1237 | case 'T': | |
1238 | do_screen_candidates = 1; | |
1239 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= | |
1240 | sizeof(out_file)) | |
1241 | fatal("Output filename too long"); | |
1242 | break; | |
1243 | case 'S': | |
1244 | /* XXX - also compare length against bits */ | |
1245 | if (BN_hex2bn(&start, optarg) == 0) | |
1246 | fatal("Invalid start point."); | |
1247 | break; | |
1248 | case '?': | |
1249 | default: | |
1250 | usage(); | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | /* reinit */ | |
1255 | log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1); | |
1256 | ||
1257 | if (optind < argc) { | |
1258 | printf("Too many arguments.\n"); | |
1259 | usage(); | |
1260 | } | |
1261 | if (change_passphrase && change_comment) { | |
1262 | printf("Can only have one of -p and -c.\n"); | |
1263 | usage(); | |
1264 | } | |
1265 | if (print_fingerprint && (delete_host || hash_hosts)) { | |
1266 | printf("Cannot use -l with -D or -R.\n"); | |
1267 | usage(); | |
1268 | } | |
1269 | if (delete_host || hash_hosts || find_host) | |
1270 | do_known_hosts(pw, rr_hostname); | |
1271 | if (print_fingerprint || print_bubblebabble) | |
1272 | do_fingerprint(pw); | |
1273 | if (change_passphrase) | |
1274 | do_change_passphrase(pw); | |
1275 | if (change_comment) | |
1276 | do_change_comment(pw); | |
1277 | if (convert_to_ssh2) | |
1278 | do_convert_to_ssh2(pw); | |
1279 | if (convert_from_ssh2) | |
1280 | do_convert_from_ssh2(pw); | |
1281 | if (print_public) | |
1282 | do_print_public(pw); | |
1283 | if (rr_hostname != NULL) { | |
1284 | unsigned int n = 0; | |
1285 | ||
1286 | if (have_identity) { | |
1287 | n = do_print_resource_record(pw, | |
1288 | identity_file, rr_hostname); | |
1289 | if (n == 0) { | |
1290 | perror(identity_file); | |
1291 | exit(1); | |
1292 | } | |
1293 | exit(0); | |
1294 | } else { | |
1295 | ||
1296 | n += do_print_resource_record(pw, | |
1297 | _PATH_HOST_RSA_KEY_FILE, rr_hostname); | |
1298 | n += do_print_resource_record(pw, | |
1299 | _PATH_HOST_DSA_KEY_FILE, rr_hostname); | |
1300 | ||
1301 | if (n == 0) | |
1302 | fatal("no keys found."); | |
1303 | exit(0); | |
1304 | } | |
1305 | } | |
1306 | if (reader_id != NULL) { | |
1307 | #ifdef SMARTCARD | |
1308 | if (download) | |
1309 | do_download(pw, reader_id); | |
1310 | else | |
1311 | do_upload(pw, reader_id); | |
1312 | #else /* SMARTCARD */ | |
1313 | fatal("no support for smartcards."); | |
1314 | #endif /* SMARTCARD */ | |
1315 | } | |
1316 | ||
1317 | if (do_gen_candidates) { | |
1318 | FILE *out = fopen(out_file, "w"); | |
1319 | ||
1320 | if (out == NULL) { | |
1321 | error("Couldn't open modulus candidate file \"%s\": %s", | |
1322 | out_file, strerror(errno)); | |
1323 | return (1); | |
1324 | } | |
1325 | if (bits == 0) | |
1326 | bits = DEFAULT_BITS; | |
1327 | if (gen_candidates(out, memory, bits, start) != 0) | |
1328 | fatal("modulus candidate generation failed"); | |
1329 | ||
1330 | return (0); | |
1331 | } | |
1332 | ||
1333 | if (do_screen_candidates) { | |
1334 | FILE *in; | |
1335 | FILE *out = fopen(out_file, "w"); | |
1336 | ||
1337 | if (have_identity && strcmp(identity_file, "-") != 0) { | |
1338 | if ((in = fopen(identity_file, "r")) == NULL) { | |
1339 | fatal("Couldn't open modulus candidate " | |
1340 | "file \"%s\": %s", identity_file, | |
1341 | strerror(errno)); | |
1342 | } | |
1343 | } else | |
1344 | in = stdin; | |
1345 | ||
1346 | if (out == NULL) { | |
1347 | fatal("Couldn't open moduli file \"%s\": %s", | |
1348 | out_file, strerror(errno)); | |
1349 | } | |
1350 | if (prime_test(in, out, trials, generator_wanted) != 0) | |
1351 | fatal("modulus screening failed"); | |
1352 | return (0); | |
1353 | } | |
1354 | ||
1355 | arc4random_stir(); | |
1356 | ||
1357 | if (key_type_name == NULL) | |
1358 | key_type_name = "rsa"; | |
1359 | ||
1360 | type = key_type_from_name(key_type_name); | |
1361 | if (type == KEY_UNSPEC) { | |
1362 | fprintf(stderr, "unknown key type %s\n", key_type_name); | |
1363 | exit(1); | |
1364 | } | |
1365 | if (bits == 0) | |
1366 | bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; | |
1367 | if (type == KEY_DSA && bits != 1024) | |
1368 | fatal("DSA keys must be 1024 bits"); | |
1369 | if (!quiet) | |
1370 | printf("Generating public/private %s key pair.\n", key_type_name); | |
1371 | private = key_generate(type, bits); | |
1372 | if (private == NULL) { | |
1373 | fprintf(stderr, "key_generate failed\n"); | |
1374 | exit(1); | |
1375 | } | |
1376 | public = key_from_private(private); | |
1377 | ||
1378 | if (!have_identity) | |
1379 | ask_filename(pw, "Enter file in which to save the key"); | |
1380 | ||
1381 | /* Create ~/.ssh directory if it doesn't already exist. */ | |
1382 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR); | |
1383 | if (strstr(identity_file, dotsshdir) != NULL && | |
1384 | stat(dotsshdir, &st) < 0) { | |
1385 | if (mkdir(dotsshdir, 0700) < 0) | |
1386 | error("Could not create directory '%s'.", dotsshdir); | |
1387 | else if (!quiet) | |
1388 | printf("Created directory '%s'.\n", dotsshdir); | |
1389 | } | |
1390 | /* If the file already exists, ask the user to confirm. */ | |
1391 | if (stat(identity_file, &st) >= 0) { | |
1392 | char yesno[3]; | |
1393 | printf("%s already exists.\n", identity_file); | |
1394 | printf("Overwrite (y/n)? "); | |
1395 | fflush(stdout); | |
1396 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) | |
1397 | exit(1); | |
1398 | if (yesno[0] != 'y' && yesno[0] != 'Y') | |
1399 | exit(1); | |
1400 | } | |
1401 | /* Ask for a passphrase (twice). */ | |
1402 | if (identity_passphrase) | |
1403 | passphrase1 = xstrdup(identity_passphrase); | |
1404 | else if (identity_new_passphrase) | |
1405 | passphrase1 = xstrdup(identity_new_passphrase); | |
1406 | else { | |
1407 | passphrase_again: | |
1408 | passphrase1 = | |
1409 | read_passphrase("Enter passphrase (empty for no " | |
1410 | "passphrase): ", RP_ALLOW_STDIN); | |
1411 | passphrase2 = read_passphrase("Enter same passphrase again: ", | |
1412 | RP_ALLOW_STDIN); | |
1413 | if (strcmp(passphrase1, passphrase2) != 0) { | |
1414 | /* | |
1415 | * The passphrases do not match. Clear them and | |
1416 | * retry. | |
1417 | */ | |
1418 | memset(passphrase1, 0, strlen(passphrase1)); | |
1419 | memset(passphrase2, 0, strlen(passphrase2)); | |
1420 | xfree(passphrase1); | |
1421 | xfree(passphrase2); | |
1422 | printf("Passphrases do not match. Try again.\n"); | |
1423 | goto passphrase_again; | |
1424 | } | |
1425 | /* Clear the other copy of the passphrase. */ | |
1426 | memset(passphrase2, 0, strlen(passphrase2)); | |
1427 | xfree(passphrase2); | |
1428 | } | |
1429 | ||
1430 | if (identity_comment) { | |
1431 | strlcpy(comment, identity_comment, sizeof(comment)); | |
1432 | } else { | |
1433 | /* Create default comment field for the passphrase. */ | |
1434 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); | |
1435 | } | |
1436 | ||
1437 | /* Save the key with the given passphrase and comment. */ | |
1438 | if (!key_save_private(private, identity_file, passphrase1, comment)) { | |
1439 | printf("Saving the key failed: %s.\n", identity_file); | |
1440 | memset(passphrase1, 0, strlen(passphrase1)); | |
1441 | xfree(passphrase1); | |
1442 | exit(1); | |
1443 | } | |
1444 | /* Clear the passphrase. */ | |
1445 | memset(passphrase1, 0, strlen(passphrase1)); | |
1446 | xfree(passphrase1); | |
1447 | ||
1448 | /* Clear the private key and the random number generator. */ | |
1449 | key_free(private); | |
1450 | arc4random_stir(); | |
1451 | ||
1452 | if (!quiet) | |
1453 | printf("Your identification has been saved in %s.\n", identity_file); | |
1454 | ||
1455 | strlcat(identity_file, ".pub", sizeof(identity_file)); | |
1456 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
1457 | if (fd == -1) { | |
1458 | printf("Could not save your public key in %s\n", identity_file); | |
1459 | exit(1); | |
1460 | } | |
1461 | f = fdopen(fd, "w"); | |
1462 | if (f == NULL) { | |
1463 | printf("fdopen %s failed\n", identity_file); | |
1464 | exit(1); | |
1465 | } | |
1466 | if (!key_write(public, f)) | |
1467 | fprintf(stderr, "write key failed\n"); | |
1468 | fprintf(f, " %s\n", comment); | |
1469 | fclose(f); | |
1470 | ||
1471 | if (!quiet) { | |
1472 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); | |
1473 | char *ra = key_fingerprint(public, SSH_FP_MD5, | |
1474 | SSH_FP_RANDOMART); | |
1475 | printf("Your public key has been saved in %s.\n", | |
1476 | identity_file); | |
1477 | printf("The key fingerprint is:\n"); | |
1478 | printf("%s %s\n", fp, comment); | |
1479 | printf("The key's randomart image is:\n"); | |
1480 | printf("%s\n", ra); | |
1481 | xfree(ra); | |
1482 | xfree(fp); | |
1483 | } | |
1484 | ||
1485 | key_free(public); | |
1486 | exit(0); | |
1487 | } |