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