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