]> andersk Git - openssh.git/blob - ssh-keygen.c
- jakob@cvs.openbsd.org 2001/03/11 15:04:16
[openssh.git] / ssh-keygen.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Identity and host key generation and maintenance.
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13
14 #include "includes.h"
15 RCSID("$OpenBSD: ssh-keygen.c,v 1.47 2001/03/11 15:04:16 jakob Exp $");
16
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19
20 #include "xmalloc.h"
21 #include "key.h"
22 #include "authfile.h"
23 #include "uuencode.h"
24 #include "buffer.h"
25 #include "bufaux.h"
26 #include "pathnames.h"
27 #include "log.h"
28 #include "readpass.h"
29
30 /* Number of bits in the RSA/DSA key.  This value can be changed on the command line. */
31 int bits = 1024;
32
33 /*
34  * Flag indicating that we just want to change the passphrase.  This can be
35  * set on the command line.
36  */
37 int change_passphrase = 0;
38
39 /*
40  * Flag indicating that we just want to change the comment.  This can be set
41  * on the command line.
42  */
43 int change_comment = 0;
44
45 int quiet = 0;
46
47 /* Flag indicating that we just want to see the key fingerprint */
48 int print_fingerprint = 0;
49
50 /* The identity file name, given on the command line or entered by the user. */
51 char identity_file[1024];
52 int have_identity = 0;
53
54 /* This is set to the passphrase if given on the command line. */
55 char *identity_passphrase = NULL;
56
57 /* This is set to the new passphrase if given on the command line. */
58 char *identity_new_passphrase = NULL;
59
60 /* This is set to the new comment if given on the command line. */
61 char *identity_comment = NULL;
62
63 /* Dump public key file in format used by real and the original SSH 2 */
64 int convert_to_ssh2 = 0;
65 int convert_from_ssh2 = 0;
66 int print_public = 0;
67 int print_verbose = 0;
68
69 /* default to RSA for SSH-1 */
70 char *key_type_name = "rsa1";
71
72 /* argv0 */
73 #ifdef HAVE___PROGNAME
74 extern char *__progname;
75 #else
76 char *__progname;
77 #endif
78
79 char hostname[MAXHOSTNAMELEN];
80
81 void
82 ask_filename(struct passwd *pw, const char *prompt)
83 {
84         char buf[1024];
85         char *name = NULL;
86
87         switch (key_type_from_name(key_type_name)) {
88         case KEY_RSA1:
89                 name = _PATH_SSH_CLIENT_IDENTITY;
90                 break;
91         case KEY_DSA:
92                 name = _PATH_SSH_CLIENT_ID_DSA;
93                 break;
94         case KEY_RSA:
95                 name = _PATH_SSH_CLIENT_ID_RSA;
96                 break;
97         default:
98                 fprintf(stderr, "bad key type");
99                 exit(1);
100                 break;
101         }
102         snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
103         fprintf(stderr, "%s (%s): ", prompt, identity_file);
104         fflush(stderr);
105         if (fgets(buf, sizeof(buf), stdin) == NULL)
106                 exit(1);
107         if (strchr(buf, '\n'))
108                 *strchr(buf, '\n') = 0;
109         if (strcmp(buf, "") != 0)
110                 strlcpy(identity_file, buf, sizeof(identity_file));
111         have_identity = 1;
112 }
113
114 int
115 try_load_key(char *filename, Key *k)
116 {
117         int success = 1;
118         if (!load_private_key(filename, "", k, NULL)) {
119                 char *pass = read_passphrase("Enter passphrase: ", 1);
120                 if (!load_private_key(filename, pass, k, NULL)) {
121                         success = 0;
122                 }
123                 memset(pass, 0, strlen(pass));
124                 xfree(pass);
125         }
126         return success;
127 }
128
129 #define SSH_COM_PUBLIC_BEGIN            "---- BEGIN SSH2 PUBLIC KEY ----"
130 #define SSH_COM_PUBLIC_END              "---- END SSH2 PUBLIC KEY ----"
131 #define SSH_COM_PRIVATE_BEGIN           "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
132 #define SSH_COM_PRIVATE_KEY_MAGIC       0x3f6ff9eb
133
134 void
135 do_convert_to_ssh2(struct passwd *pw)
136 {
137         Key *k;
138         int len;
139         u_char *blob;
140         struct stat st;
141
142         if (!have_identity)
143                 ask_filename(pw, "Enter file in which the key is");
144         if (stat(identity_file, &st) < 0) {
145                 perror(identity_file);
146                 exit(1);
147         }
148         k = key_new(KEY_UNSPEC);
149         if (!try_load_key(identity_file, k)) {
150                 fprintf(stderr, "load failed\n");
151                 exit(1);
152         }
153         key_to_blob(k, &blob, &len);
154         fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
155         fprintf(stdout,
156             "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
157             key_size(k), key_type(k),
158             pw->pw_name, hostname);
159         dump_base64(stdout, blob, len);
160         fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
161         key_free(k);
162         xfree(blob);
163         exit(0);
164 }
165
166 void
167 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
168 {
169         int bits = buffer_get_int(b);
170         int bytes = (bits + 7) / 8;
171         if (buffer_len(b) < bytes)
172                 fatal("buffer_get_bignum_bits: input buffer too small");
173         BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
174         buffer_consume(b, bytes);
175 }
176
177 Key *
178 do_convert_private_ssh2_from_blob(char *blob, int blen)
179 {
180         Buffer b;
181         DSA *dsa;
182         Key *key = NULL;
183         int ignore, magic, rlen;
184         char *type, *cipher;
185
186         buffer_init(&b);
187         buffer_append(&b, blob, blen);
188
189         magic  = buffer_get_int(&b);
190         if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
191                 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
192                 buffer_free(&b);
193                 return NULL;
194         }
195         ignore = buffer_get_int(&b);
196         type   = buffer_get_string(&b, NULL);
197         cipher = buffer_get_string(&b, NULL);
198         ignore = buffer_get_int(&b);
199         ignore = buffer_get_int(&b);
200         ignore = buffer_get_int(&b);
201         xfree(type);
202
203         if (strcmp(cipher, "none") != 0) {
204                 error("unsupported cipher %s", cipher);
205                 xfree(cipher);
206                 buffer_free(&b);
207                 return NULL;
208         }
209         xfree(cipher);
210
211         key = key_new(KEY_DSA);
212         dsa = key->dsa;
213         dsa->priv_key = BN_new();
214         if (dsa->priv_key == NULL) {
215                 error("alloc priv_key failed");
216                 key_free(key);
217                 return NULL;
218         }
219         buffer_get_bignum_bits(&b, dsa->p);
220         buffer_get_bignum_bits(&b, dsa->g);
221         buffer_get_bignum_bits(&b, dsa->q);
222         buffer_get_bignum_bits(&b, dsa->pub_key);
223         buffer_get_bignum_bits(&b, dsa->priv_key);
224         rlen = buffer_len(&b);
225         if(rlen != 0)
226                 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
227         buffer_free(&b);
228         return key;
229 }
230
231 void
232 do_convert_from_ssh2(struct passwd *pw)
233 {
234         Key *k;
235         int blen;
236         char line[1024], *p;
237         char blob[8096];
238         char encoded[8096];
239         struct stat st;
240         int escaped = 0, private = 0, ok;
241         FILE *fp;
242
243         if (!have_identity)
244                 ask_filename(pw, "Enter file in which the key is");
245         if (stat(identity_file, &st) < 0) {
246                 perror(identity_file);
247                 exit(1);
248         }
249         fp = fopen(identity_file, "r");
250         if (fp == NULL) {
251                 perror(identity_file);
252                 exit(1);
253         }
254         encoded[0] = '\0';
255         while (fgets(line, sizeof(line), fp)) {
256                 if (!(p = strchr(line, '\n'))) {
257                         fprintf(stderr, "input line too long.\n");
258                         exit(1);
259                 }
260                 if (p > line && p[-1] == '\\')
261                         escaped++;
262                 if (strncmp(line, "----", 4) == 0 ||
263                     strstr(line, ": ") != NULL) {
264                         if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
265                                 private = 1;
266                         fprintf(stderr, "ignore: %s", line);
267                         continue;
268                 }
269                 if (escaped) {
270                         escaped--;
271                         fprintf(stderr, "escaped: %s", line);
272                         continue;
273                 }
274                 *p = '\0';
275                 strlcat(encoded, line, sizeof(encoded));
276         }
277         blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
278         if (blen < 0) {
279                 fprintf(stderr, "uudecode failed.\n");
280                 exit(1);
281         }
282         k = private ?
283             do_convert_private_ssh2_from_blob(blob, blen) :
284             key_from_blob(blob, blen);
285         if (k == NULL) {
286                 fprintf(stderr, "decode blob failed.\n");
287                 exit(1);
288         }
289         ok = private ?
290             PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
291             key_write(k, stdout);
292         if (!ok) {
293                 fprintf(stderr, "key write failed");
294                 exit(1);
295         }
296         key_free(k);
297         fprintf(stdout, "\n");
298         fclose(fp);
299         exit(0);
300 }
301
302 void
303 do_print_public(struct passwd *pw)
304 {
305         Key *k;
306         struct stat st;
307
308         if (!have_identity)
309                 ask_filename(pw, "Enter file in which the key is");
310         if (stat(identity_file, &st) < 0) {
311                 perror(identity_file);
312                 exit(1);
313         }
314         k = key_new(KEY_UNSPEC);
315         if (!try_load_key(identity_file, k)) {
316                 fprintf(stderr, "load failed\n");
317                 exit(1);
318         }
319         if (!key_write(k, stdout))
320                 fprintf(stderr, "key_write failed");
321         key_free(k);
322         fprintf(stdout, "\n");
323         exit(0);
324 }
325
326 void
327 do_fingerprint(struct passwd *pw)
328 {
329
330         FILE *f;
331         Key *public;
332         char *comment = NULL, *cp, *ep, line[16*1024];
333         int i, skip = 0, num = 1, invalid = 1, success = 0;
334         struct stat st;
335
336         if (!have_identity)
337                 ask_filename(pw, "Enter file in which the key is");
338         if (stat(identity_file, &st) < 0) {
339                 perror(identity_file);
340                 exit(1);
341         }
342         public = key_new(KEY_RSA1);
343         if (load_public_key(identity_file, public, &comment)) {
344                 success = 1;
345         } else {
346                 key_free(public);
347                 public = key_new(KEY_UNSPEC);
348                 if (try_load_public_key(identity_file, public, &comment))
349                         success = 1;
350                 else
351                         debug("try_load_public_key KEY_UNSPEC failed");
352         }
353         if (success) {
354                 char *digest_md5, *digest_sha1, *digest_bubblebabble;
355
356                 digest_md5 = key_fingerprint_ex(public, SSH_FP_MD5, SSH_FP_HEX);
357                 digest_sha1 = key_fingerprint_ex(public, SSH_FP_SHA1, SSH_FP_HEX);
358                 digest_bubblebabble = key_fingerprint_ex(public, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
359
360                 if(print_verbose) {
361                         printf("comment:      %s\n", comment);
362                         printf("size:         %d\n", key_size(public));
363                         printf("md5:          %s\n", digest_md5);
364                         printf("sha1:         %s\n", digest_sha1);
365                         printf("bubblebabble: %s\n", digest_bubblebabble);
366                 } else {
367                         printf("%d %s %s\n", key_size(public), digest_md5, comment);
368                 }
369
370                 key_free(public);
371                 xfree(comment);
372                 xfree(digest_md5);
373                 xfree(digest_sha1);
374                 xfree(digest_bubblebabble);
375
376                 exit(0);
377         }
378
379         f = fopen(identity_file, "r");
380         if (f != NULL) {
381                 while (fgets(line, sizeof(line), f)) {
382                         i = strlen(line) - 1;
383                         if (line[i] != '\n') {
384                                 error("line %d too long: %.40s...", num, line);
385                                 skip = 1;
386                                 continue;
387                         }
388                         num++;
389                         if (skip) {
390                                 skip = 0;
391                                 continue;
392                         }
393                         line[i] = '\0';
394
395                         /* Skip leading whitespace, empty and comment lines. */
396                         for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
397                                 ;
398                         if (!*cp || *cp == '\n' || *cp == '#')
399                                 continue ;
400                         i = strtol(cp, &ep, 10);
401                         if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
402                                 int quoted = 0;
403                                 comment = cp;
404                                 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
405                                         if (*cp == '\\' && cp[1] == '"')
406                                                 cp++;   /* Skip both */
407                                         else if (*cp == '"')
408                                                 quoted = !quoted;
409                                 }
410                                 if (!*cp)
411                                         continue;
412                                 *cp++ = '\0';
413                         }
414                         ep = cp;
415                         public = key_new(KEY_RSA1);
416                         if (key_read(public, &cp) != 1) {
417                                 cp = ep;
418                                 key_free(public);
419                                 public = key_new(KEY_UNSPEC);
420                                 if (key_read(public, &cp) != 1) {
421                                         key_free(public);
422                                         continue;
423                                 }
424                         }
425                         comment = *cp ? cp : comment;
426                         printf("%d %s %s\n", key_size(public),
427                             key_fingerprint(public),
428                             comment ? comment : "no comment");
429                         invalid = 0;
430                 }
431                 fclose(f);
432         }
433         key_free(public);
434         if (invalid) {
435                 printf("%s is not a valid key file.\n", identity_file);
436                 exit(1);
437         }
438         exit(0);
439 }
440
441 /*
442  * Perform changing a passphrase.  The argument is the passwd structure
443  * for the current user.
444  */
445 void
446 do_change_passphrase(struct passwd *pw)
447 {
448         char *comment;
449         char *old_passphrase, *passphrase1, *passphrase2;
450         struct stat st;
451         Key *private;
452         Key *public;
453         int type = KEY_RSA1;
454
455         if (!have_identity)
456                 ask_filename(pw, "Enter file in which the key is");
457         if (stat(identity_file, &st) < 0) {
458                 perror(identity_file);
459                 exit(1);
460         }
461         public = key_new(type);
462         if (!load_public_key(identity_file, public, NULL)) {
463                 type = KEY_UNSPEC;
464         } else {
465                 /* Clear the public key since we are just about to load the whole file. */
466                 key_free(public);
467         }
468         /* Try to load the file with empty passphrase. */
469         private = key_new(type);
470         if (!load_private_key(identity_file, "", private, &comment)) {
471                 if (identity_passphrase)
472                         old_passphrase = xstrdup(identity_passphrase);
473                 else
474                         old_passphrase = read_passphrase("Enter old passphrase: ", 1);
475                 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
476                         memset(old_passphrase, 0, strlen(old_passphrase));
477                         xfree(old_passphrase);
478                         printf("Bad passphrase.\n");
479                         exit(1);
480                 }
481                 memset(old_passphrase, 0, strlen(old_passphrase));
482                 xfree(old_passphrase);
483         }
484         printf("Key has comment '%s'\n", comment);
485
486         /* Ask the new passphrase (twice). */
487         if (identity_new_passphrase) {
488                 passphrase1 = xstrdup(identity_new_passphrase);
489                 passphrase2 = NULL;
490         } else {
491                 passphrase1 =
492                         read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
493                 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
494
495                 /* Verify that they are the same. */
496                 if (strcmp(passphrase1, passphrase2) != 0) {
497                         memset(passphrase1, 0, strlen(passphrase1));
498                         memset(passphrase2, 0, strlen(passphrase2));
499                         xfree(passphrase1);
500                         xfree(passphrase2);
501                         printf("Pass phrases do not match.  Try again.\n");
502                         exit(1);
503                 }
504                 /* Destroy the other copy. */
505                 memset(passphrase2, 0, strlen(passphrase2));
506                 xfree(passphrase2);
507         }
508
509         /* Save the file using the new passphrase. */
510         if (!save_private_key(identity_file, passphrase1, private, comment)) {
511                 printf("Saving the key failed: %s: %s.\n",
512                        identity_file, strerror(errno));
513                 memset(passphrase1, 0, strlen(passphrase1));
514                 xfree(passphrase1);
515                 key_free(private);
516                 xfree(comment);
517                 exit(1);
518         }
519         /* Destroy the passphrase and the copy of the key in memory. */
520         memset(passphrase1, 0, strlen(passphrase1));
521         xfree(passphrase1);
522         key_free(private);               /* Destroys contents */
523         xfree(comment);
524
525         printf("Your identification has been saved with the new passphrase.\n");
526         exit(0);
527 }
528
529 /*
530  * Change the comment of a private key file.
531  */
532 void
533 do_change_comment(struct passwd *pw)
534 {
535         char new_comment[1024], *comment, *passphrase;
536         Key *private, *public;
537         struct stat st;
538         FILE *f;
539         int fd;
540
541         if (!have_identity)
542                 ask_filename(pw, "Enter file in which the key is");
543         if (stat(identity_file, &st) < 0) {
544                 perror(identity_file);
545                 exit(1);
546         }
547         /*
548          * Try to load the public key from the file the verify that it is
549          * readable and of the proper format.
550          */
551         public = key_new(KEY_RSA1);
552         if (!load_public_key(identity_file, public, NULL)) {
553                 printf("%s is not a valid key file.\n", identity_file);
554                 printf("Comments are only supported in RSA1 keys\n");
555                 exit(1);
556         }
557
558         private = key_new(KEY_RSA1);
559         if (load_private_key(identity_file, "", private, &comment))
560                 passphrase = xstrdup("");
561         else {
562                 if (identity_passphrase)
563                         passphrase = xstrdup(identity_passphrase);
564                 else if (identity_new_passphrase)
565                         passphrase = xstrdup(identity_new_passphrase);
566                 else
567                         passphrase = read_passphrase("Enter passphrase: ", 1);
568                 /* Try to load using the passphrase. */
569                 if (!load_private_key(identity_file, passphrase, private, &comment)) {
570                         memset(passphrase, 0, strlen(passphrase));
571                         xfree(passphrase);
572                         printf("Bad passphrase.\n");
573                         exit(1);
574                 }
575         }
576         printf("Key now has comment '%s'\n", comment);
577
578         if (identity_comment) {
579                 strlcpy(new_comment, identity_comment, sizeof(new_comment));
580         } else {
581                 printf("Enter new comment: ");
582                 fflush(stdout);
583                 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
584                         memset(passphrase, 0, strlen(passphrase));
585                         key_free(private);
586                         exit(1);
587                 }
588                 if (strchr(new_comment, '\n'))
589                         *strchr(new_comment, '\n') = 0;
590         }
591
592         /* Save the file using the new passphrase. */
593         if (!save_private_key(identity_file, passphrase, private, new_comment)) {
594                 printf("Saving the key failed: %s: %s.\n",
595                        identity_file, strerror(errno));
596                 memset(passphrase, 0, strlen(passphrase));
597                 xfree(passphrase);
598                 key_free(private);
599                 xfree(comment);
600                 exit(1);
601         }
602         memset(passphrase, 0, strlen(passphrase));
603         xfree(passphrase);
604         key_free(private);
605
606         strlcat(identity_file, ".pub", sizeof(identity_file));
607         fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
608         if (fd == -1) {
609                 printf("Could not save your public key in %s\n", identity_file);
610                 exit(1);
611         }
612         f = fdopen(fd, "w");
613         if (f == NULL) {
614                 printf("fdopen %s failed", identity_file);
615                 exit(1);
616         }
617         if (!key_write(public, f))
618                 fprintf(stderr, "write key failed");
619         key_free(public);
620         fprintf(f, " %s\n", new_comment);
621         fclose(f);
622
623         xfree(comment);
624
625         printf("The comment in your key file has been changed.\n");
626         exit(0);
627 }
628
629 void
630 usage(void)
631 {
632         printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
633             "[-N new-pass] [-P pass]\n", __progname);
634         exit(1);
635 }
636
637 /*
638  * Main program for key management.
639  */
640 int
641 main(int ac, char **av)
642 {
643         char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
644         Key *private, *public;
645         struct passwd *pw;
646         int opt, type, fd;
647         struct stat st;
648         FILE *f;
649
650         extern int optind;
651         extern char *optarg;
652
653         __progname = get_progname(av[0]);
654         init_rng();
655
656         SSLeay_add_all_algorithms();
657
658         /* we need this for the home * directory.  */
659         pw = getpwuid(getuid());
660         if (!pw) {
661                 printf("You don't exist, go away!\n");
662                 exit(1);
663         }
664         if (gethostname(hostname, sizeof(hostname)) < 0) {
665                 perror("gethostname");
666                 exit(1);
667         }
668
669         while ((opt = getopt(ac, av, "dqpclRxXyvb:f:t:P:N:C:")) != -1) {
670                 switch (opt) {
671                 case 'b':
672                         bits = atoi(optarg);
673                         if (bits < 512 || bits > 32768) {
674                                 printf("Bits has bad value.\n");
675                                 exit(1);
676                         }
677                         break;
678
679                 case 'l':
680                         print_fingerprint = 1;
681                         break;
682
683                 case 'p':
684                         change_passphrase = 1;
685                         break;
686
687                 case 'c':
688                         change_comment = 1;
689                         break;
690
691                 case 'f':
692                         strlcpy(identity_file, optarg, sizeof(identity_file));
693                         have_identity = 1;
694                         break;
695
696                 case 'P':
697                         identity_passphrase = optarg;
698                         break;
699
700                 case 'N':
701                         identity_new_passphrase = optarg;
702                         break;
703
704                 case 'C':
705                         identity_comment = optarg;
706                         break;
707
708                 case 'q':
709                         quiet = 1;
710                         break;
711
712                 case 'R':
713                         /* unused */
714                         exit(0);
715                         break;
716
717                 case 'x':
718                         convert_to_ssh2 = 1;
719                         break;
720
721                 case 'X':
722                         convert_from_ssh2 = 1;
723                         break;
724
725                 case 'y':
726                         print_public = 1;
727                         break;
728
729                 case 'v':
730                         print_verbose = 1;
731                         break;
732
733                 case 'd':
734                         key_type_name = "dsa";
735                         break;
736
737                 case 't':
738                         key_type_name = optarg;
739                         break;
740
741                 case '?':
742                 default:
743                         usage();
744                 }
745         }
746         if (optind < ac) {
747                 printf("Too many arguments.\n");
748                 usage();
749         }
750         if (change_passphrase && change_comment) {
751                 printf("Can only have one of -p and -c.\n");
752                 usage();
753         }
754         if (print_fingerprint)
755                 do_fingerprint(pw);
756         if (change_passphrase)
757                 do_change_passphrase(pw);
758         if (change_comment)
759                 do_change_comment(pw);
760         if (convert_to_ssh2)
761                 do_convert_to_ssh2(pw);
762         if (convert_from_ssh2)
763                 do_convert_from_ssh2(pw);
764         if (print_public)
765                 do_print_public(pw);
766
767         arc4random_stir();
768
769         type = key_type_from_name(key_type_name);
770         if (type == KEY_UNSPEC) {
771                 fprintf(stderr, "unknown key type %s\n", key_type_name);
772                 exit(1);
773         }
774         if (!quiet)
775                 printf("Generating public/private %s key pair.\n", key_type_name);
776         private = key_generate(type, bits);
777         if (private == NULL) {
778                 fprintf(stderr, "key_generate failed");
779                 exit(1);
780         }
781         public  = key_from_private(private);
782
783         if (!have_identity)
784                 ask_filename(pw, "Enter file in which to save the key");
785
786         /* Create ~/.ssh directory if it doesn\'t already exist. */
787         snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
788         if (strstr(identity_file, dotsshdir) != NULL &&
789             stat(dotsshdir, &st) < 0) {
790                 if (mkdir(dotsshdir, 0700) < 0)
791                         error("Could not create directory '%s'.", dotsshdir);
792                 else if (!quiet)
793                         printf("Created directory '%s'.\n", dotsshdir);
794         }
795         /* If the file already exists, ask the user to confirm. */
796         if (stat(identity_file, &st) >= 0) {
797                 char yesno[3];
798                 printf("%s already exists.\n", identity_file);
799                 printf("Overwrite (y/n)? ");
800                 fflush(stdout);
801                 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
802                         exit(1);
803                 if (yesno[0] != 'y' && yesno[0] != 'Y')
804                         exit(1);
805         }
806         /* Ask for a passphrase (twice). */
807         if (identity_passphrase)
808                 passphrase1 = xstrdup(identity_passphrase);
809         else if (identity_new_passphrase)
810                 passphrase1 = xstrdup(identity_new_passphrase);
811         else {
812 passphrase_again:
813                 passphrase1 =
814                         read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
815                 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
816                 if (strcmp(passphrase1, passphrase2) != 0) {
817                         /* The passphrases do not match.  Clear them and retry. */
818                         memset(passphrase1, 0, strlen(passphrase1));
819                         memset(passphrase2, 0, strlen(passphrase2));
820                         xfree(passphrase1);
821                         xfree(passphrase2);
822                         printf("Passphrases do not match.  Try again.\n");
823                         goto passphrase_again;
824                 }
825                 /* Clear the other copy of the passphrase. */
826                 memset(passphrase2, 0, strlen(passphrase2));
827                 xfree(passphrase2);
828         }
829
830         if (identity_comment) {
831                 strlcpy(comment, identity_comment, sizeof(comment));
832         } else {
833                 /* Create default commend field for the passphrase. */
834                 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
835         }
836
837         /* Save the key with the given passphrase and comment. */
838         if (!save_private_key(identity_file, passphrase1, private, comment)) {
839                 printf("Saving the key failed: %s: %s.\n",
840                     identity_file, strerror(errno));
841                 memset(passphrase1, 0, strlen(passphrase1));
842                 xfree(passphrase1);
843                 exit(1);
844         }
845         /* Clear the passphrase. */
846         memset(passphrase1, 0, strlen(passphrase1));
847         xfree(passphrase1);
848
849         /* Clear the private key and the random number generator. */
850         key_free(private);
851         arc4random_stir();
852
853         if (!quiet)
854                 printf("Your identification has been saved in %s.\n", identity_file);
855
856         strlcat(identity_file, ".pub", sizeof(identity_file));
857         fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
858         if (fd == -1) {
859                 printf("Could not save your public key in %s\n", identity_file);
860                 exit(1);
861         }
862         f = fdopen(fd, "w");
863         if (f == NULL) {
864                 printf("fdopen %s failed", identity_file);
865                 exit(1);
866         }
867         if (!key_write(public, f))
868                 fprintf(stderr, "write key failed");
869         fprintf(f, " %s\n", comment);
870         fclose(f);
871
872         if (!quiet) {
873                 printf("Your public key has been saved in %s.\n",
874                     identity_file);
875                 printf("The key fingerprint is:\n");
876                 printf("%s %s\n", key_fingerprint(public), comment);
877         }
878
879         key_free(public);
880         exit(0);
881 }
This page took 0.108428 seconds and 5 git commands to generate.