]> andersk Git - openssh.git/blob - ssh-keygen.c
- djm@cvs.openbsd.org 2004/05/09 00:06:47
[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.115 2004/05/09 00:06:47 djm 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 "misc.h"
30
31 #ifdef SMARTCARD
32 #include "scard.h"
33 #endif
34 #include "dns.h"
35
36 /* Number of bits in the RSA/DSA key.  This value can be changed on the command line. */
37 int bits = 1024;
38
39 /*
40  * Flag indicating that we just want to change the passphrase.  This can be
41  * set on the command line.
42  */
43 int change_passphrase = 0;
44
45 /*
46  * Flag indicating that we just want to change the comment.  This can be set
47  * on the command line.
48  */
49 int change_comment = 0;
50
51 int quiet = 0;
52
53 /* Flag indicating that we just want to see the key fingerprint */
54 int print_fingerprint = 0;
55 int print_bubblebabble = 0;
56
57 /* The identity file name, given on the command line or entered by the user. */
58 char identity_file[1024];
59 int have_identity = 0;
60
61 /* This is set to the passphrase if given on the command line. */
62 char *identity_passphrase = NULL;
63
64 /* This is set to the new passphrase if given on the command line. */
65 char *identity_new_passphrase = NULL;
66
67 /* This is set to the new comment if given on the command line. */
68 char *identity_comment = NULL;
69
70 /* Dump public key file in format used by real and the original SSH 2 */
71 int convert_to_ssh2 = 0;
72 int convert_from_ssh2 = 0;
73 int print_public = 0;
74 int print_generic = 0;
75
76 char *key_type_name = NULL;
77
78 /* argv0 */
79 #ifdef HAVE___PROGNAME
80 extern char *__progname;
81 #else
82 char *__progname;
83 #endif
84
85 char hostname[MAXHOSTNAMELEN];
86
87 /* moduli.c */
88 int gen_candidates(FILE *, int, int, BIGNUM *);
89 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
90
91 static void
92 ask_filename(struct passwd *pw, const char *prompt)
93 {
94         char buf[1024];
95         char *name = NULL;
96
97         if (key_type_name == NULL)
98                 name = _PATH_SSH_CLIENT_ID_RSA;
99         else
100                 switch (key_type_from_name(key_type_name)) {
101                 case KEY_RSA1:
102                         name = _PATH_SSH_CLIENT_IDENTITY;
103                         break;
104                 case KEY_DSA:
105                         name = _PATH_SSH_CLIENT_ID_DSA;
106                         break;
107                 case KEY_RSA:
108                         name = _PATH_SSH_CLIENT_ID_RSA;
109                         break;
110                 default:
111                         fprintf(stderr, "bad key type");
112                         exit(1);
113                         break;
114                 }
115
116         snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
117         fprintf(stderr, "%s (%s): ", prompt, identity_file);
118         if (fgets(buf, sizeof(buf), stdin) == NULL)
119                 exit(1);
120         if (strchr(buf, '\n'))
121                 *strchr(buf, '\n') = 0;
122         if (strcmp(buf, "") != 0)
123                 strlcpy(identity_file, buf, sizeof(identity_file));
124         have_identity = 1;
125 }
126
127 static Key *
128 load_identity(char *filename)
129 {
130         char *pass;
131         Key *prv;
132
133         prv = key_load_private(filename, "", NULL);
134         if (prv == NULL) {
135                 if (identity_passphrase)
136                         pass = xstrdup(identity_passphrase);
137                 else
138                         pass = read_passphrase("Enter passphrase: ",
139                             RP_ALLOW_STDIN);
140                 prv = key_load_private(filename, pass, NULL);
141                 memset(pass, 0, strlen(pass));
142                 xfree(pass);
143         }
144         return prv;
145 }
146
147 #define SSH_COM_PUBLIC_BEGIN            "---- BEGIN SSH2 PUBLIC KEY ----"
148 #define SSH_COM_PUBLIC_END              "---- END SSH2 PUBLIC KEY ----"
149 #define SSH_COM_PRIVATE_BEGIN           "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
150 #define SSH_COM_PRIVATE_KEY_MAGIC       0x3f6ff9eb
151
152 static void
153 do_convert_to_ssh2(struct passwd *pw)
154 {
155         Key *k;
156         u_int len;
157         u_char *blob;
158         struct stat st;
159
160         if (!have_identity)
161                 ask_filename(pw, "Enter file in which the key is");
162         if (stat(identity_file, &st) < 0) {
163                 perror(identity_file);
164                 exit(1);
165         }
166         if ((k = key_load_public(identity_file, NULL)) == NULL) {
167                 if ((k = load_identity(identity_file)) == NULL) {
168                         fprintf(stderr, "load failed\n");
169                         exit(1);
170                 }
171         }
172         if (k->type == KEY_RSA1) {
173                 fprintf(stderr, "version 1 keys are not supported\n");
174                 exit(1);
175         }
176         if (key_to_blob(k, &blob, &len) <= 0) {
177                 fprintf(stderr, "key_to_blob failed\n");
178                 exit(1);
179         }
180         fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
181         fprintf(stdout,
182             "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
183             key_size(k), key_type(k),
184             pw->pw_name, hostname);
185         dump_base64(stdout, blob, len);
186         fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
187         key_free(k);
188         xfree(blob);
189         exit(0);
190 }
191
192 static void
193 buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
194 {
195         u_int bits = buffer_get_int(b);
196         u_int bytes = (bits + 7) / 8;
197
198         if (buffer_len(b) < bytes)
199                 fatal("buffer_get_bignum_bits: input buffer too small: "
200                     "need %d have %d", bytes, buffer_len(b));
201         BN_bin2bn(buffer_ptr(b), bytes, value);
202         buffer_consume(b, bytes);
203 }
204
205 static Key *
206 do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
207 {
208         Buffer b;
209         Key *key = NULL;
210         char *type, *cipher;
211         u_char *sig, data[] = "abcde12345";
212         int magic, rlen, ktype, i1, i2, i3, i4;
213         u_int slen;
214         u_long e;
215
216         buffer_init(&b);
217         buffer_append(&b, blob, blen);
218
219         magic  = buffer_get_int(&b);
220         if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
221                 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
222                 buffer_free(&b);
223                 return NULL;
224         }
225         i1 = buffer_get_int(&b);
226         type   = buffer_get_string(&b, NULL);
227         cipher = buffer_get_string(&b, NULL);
228         i2 = buffer_get_int(&b);
229         i3 = buffer_get_int(&b);
230         i4 = buffer_get_int(&b);
231         debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
232         if (strcmp(cipher, "none") != 0) {
233                 error("unsupported cipher %s", cipher);
234                 xfree(cipher);
235                 buffer_free(&b);
236                 xfree(type);
237                 return NULL;
238         }
239         xfree(cipher);
240
241         if (strstr(type, "dsa")) {
242                 ktype = KEY_DSA;
243         } else if (strstr(type, "rsa")) {
244                 ktype = KEY_RSA;
245         } else {
246                 xfree(type);
247                 return NULL;
248         }
249         key = key_new_private(ktype);
250         xfree(type);
251
252         switch (key->type) {
253         case KEY_DSA:
254                 buffer_get_bignum_bits(&b, key->dsa->p);
255                 buffer_get_bignum_bits(&b, key->dsa->g);
256                 buffer_get_bignum_bits(&b, key->dsa->q);
257                 buffer_get_bignum_bits(&b, key->dsa->pub_key);
258                 buffer_get_bignum_bits(&b, key->dsa->priv_key);
259                 break;
260         case KEY_RSA:
261                 e  = buffer_get_char(&b);
262                 debug("e %lx", e);
263                 if (e < 30) {
264                         e <<= 8;
265                         e += buffer_get_char(&b);
266                         debug("e %lx", e);
267                         e <<= 8;
268                         e += buffer_get_char(&b);
269                         debug("e %lx", e);
270                 }
271                 if (!BN_set_word(key->rsa->e, e)) {
272                         buffer_free(&b);
273                         key_free(key);
274                         return NULL;
275                 }
276                 buffer_get_bignum_bits(&b, key->rsa->d);
277                 buffer_get_bignum_bits(&b, key->rsa->n);
278                 buffer_get_bignum_bits(&b, key->rsa->iqmp);
279                 buffer_get_bignum_bits(&b, key->rsa->q);
280                 buffer_get_bignum_bits(&b, key->rsa->p);
281                 rsa_generate_additional_parameters(key->rsa);
282                 break;
283         }
284         rlen = buffer_len(&b);
285         if (rlen != 0)
286                 error("do_convert_private_ssh2_from_blob: "
287                     "remaining bytes in key blob %d", rlen);
288         buffer_free(&b);
289
290         /* try the key */
291         key_sign(key, &sig, &slen, data, sizeof(data));
292         key_verify(key, sig, slen, data, sizeof(data));
293         xfree(sig);
294         return key;
295 }
296
297 static void
298 do_convert_from_ssh2(struct passwd *pw)
299 {
300         Key *k;
301         int blen;
302         u_int len;
303         char line[1024], *p;
304         u_char blob[8096];
305         char encoded[8096];
306         struct stat st;
307         int escaped = 0, private = 0, ok;
308         FILE *fp;
309
310         if (!have_identity)
311                 ask_filename(pw, "Enter file in which the key is");
312         if (stat(identity_file, &st) < 0) {
313                 perror(identity_file);
314                 exit(1);
315         }
316         fp = fopen(identity_file, "r");
317         if (fp == NULL) {
318                 perror(identity_file);
319                 exit(1);
320         }
321         encoded[0] = '\0';
322         while (fgets(line, sizeof(line), fp)) {
323                 if (!(p = strchr(line, '\n'))) {
324                         fprintf(stderr, "input line too long.\n");
325                         exit(1);
326                 }
327                 if (p > line && p[-1] == '\\')
328                         escaped++;
329                 if (strncmp(line, "----", 4) == 0 ||
330                     strstr(line, ": ") != NULL) {
331                         if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
332                                 private = 1;
333                         if (strstr(line, " END ") != NULL) {
334                                 break;
335                         }
336                         /* fprintf(stderr, "ignore: %s", line); */
337                         continue;
338                 }
339                 if (escaped) {
340                         escaped--;
341                         /* fprintf(stderr, "escaped: %s", line); */
342                         continue;
343                 }
344                 *p = '\0';
345                 strlcat(encoded, line, sizeof(encoded));
346         }
347         len = strlen(encoded);
348         if (((len % 4) == 3) &&
349             (encoded[len-1] == '=') &&
350             (encoded[len-2] == '=') &&
351             (encoded[len-3] == '='))
352                 encoded[len-3] = '\0';
353         blen = uudecode(encoded, blob, sizeof(blob));
354         if (blen < 0) {
355                 fprintf(stderr, "uudecode failed.\n");
356                 exit(1);
357         }
358         k = private ?
359             do_convert_private_ssh2_from_blob(blob, blen) :
360             key_from_blob(blob, blen);
361         if (k == NULL) {
362                 fprintf(stderr, "decode blob failed.\n");
363                 exit(1);
364         }
365         ok = private ?
366             (k->type == KEY_DSA ?
367                  PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
368                  PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
369             key_write(k, stdout);
370         if (!ok) {
371                 fprintf(stderr, "key write failed");
372                 exit(1);
373         }
374         key_free(k);
375         if (!private)
376                 fprintf(stdout, "\n");
377         fclose(fp);
378         exit(0);
379 }
380
381 static void
382 do_print_public(struct passwd *pw)
383 {
384         Key *prv;
385         struct stat st;
386
387         if (!have_identity)
388                 ask_filename(pw, "Enter file in which the key is");
389         if (stat(identity_file, &st) < 0) {
390                 perror(identity_file);
391                 exit(1);
392         }
393         prv = load_identity(identity_file);
394         if (prv == NULL) {
395                 fprintf(stderr, "load failed\n");
396                 exit(1);
397         }
398         if (!key_write(prv, stdout))
399                 fprintf(stderr, "key_write failed");
400         key_free(prv);
401         fprintf(stdout, "\n");
402         exit(0);
403 }
404
405 #ifdef SMARTCARD
406 static void
407 do_upload(struct passwd *pw, const char *sc_reader_id)
408 {
409         Key *prv = NULL;
410         struct stat st;
411         int ret;
412
413         if (!have_identity)
414                 ask_filename(pw, "Enter file in which the key is");
415         if (stat(identity_file, &st) < 0) {
416                 perror(identity_file);
417                 exit(1);
418         }
419         prv = load_identity(identity_file);
420         if (prv == NULL) {
421                 error("load failed");
422                 exit(1);
423         }
424         ret = sc_put_key(prv, sc_reader_id);
425         key_free(prv);
426         if (ret < 0)
427                 exit(1);
428         logit("loading key done");
429         exit(0);
430 }
431
432 static void
433 do_download(struct passwd *pw, const char *sc_reader_id)
434 {
435         Key **keys = NULL;
436         int i;
437
438         keys = sc_get_keys(sc_reader_id, NULL);
439         if (keys == NULL)
440                 fatal("cannot read public key from smartcard");
441         for (i = 0; keys[i]; i++) {
442                 key_write(keys[i], stdout);
443                 key_free(keys[i]);
444                 fprintf(stdout, "\n");
445         }
446         xfree(keys);
447         exit(0);
448 }
449 #endif /* SMARTCARD */
450
451 static void
452 do_fingerprint(struct passwd *pw)
453 {
454         FILE *f;
455         Key *public;
456         char *comment = NULL, *cp, *ep, line[16*1024], *fp;
457         int i, skip = 0, num = 1, invalid = 1;
458         enum fp_rep rep;
459         enum fp_type fptype;
460         struct stat st;
461
462         fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
463         rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
464
465         if (!have_identity)
466                 ask_filename(pw, "Enter file in which the key is");
467         if (stat(identity_file, &st) < 0) {
468                 perror(identity_file);
469                 exit(1);
470         }
471         public = key_load_public(identity_file, &comment);
472         if (public != NULL) {
473                 fp = key_fingerprint(public, fptype, rep);
474                 printf("%u %s %s\n", key_size(public), fp, comment);
475                 key_free(public);
476                 xfree(comment);
477                 xfree(fp);
478                 exit(0);
479         }
480         if (comment)
481                 xfree(comment);
482
483         f = fopen(identity_file, "r");
484         if (f != NULL) {
485                 while (fgets(line, sizeof(line), f)) {
486                         i = strlen(line) - 1;
487                         if (line[i] != '\n') {
488                                 error("line %d too long: %.40s...", num, line);
489                                 skip = 1;
490                                 continue;
491                         }
492                         num++;
493                         if (skip) {
494                                 skip = 0;
495                                 continue;
496                         }
497                         line[i] = '\0';
498
499                         /* Skip leading whitespace, empty and comment lines. */
500                         for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
501                                 ;
502                         if (!*cp || *cp == '\n' || *cp == '#')
503                                 continue ;
504                         i = strtol(cp, &ep, 10);
505                         if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
506                                 int quoted = 0;
507                                 comment = cp;
508                                 for (; *cp && (quoted || (*cp != ' ' &&
509                                     *cp != '\t')); cp++) {
510                                         if (*cp == '\\' && cp[1] == '"')
511                                                 cp++;   /* Skip both */
512                                         else if (*cp == '"')
513                                                 quoted = !quoted;
514                                 }
515                                 if (!*cp)
516                                         continue;
517                                 *cp++ = '\0';
518                         }
519                         ep = cp;
520                         public = key_new(KEY_RSA1);
521                         if (key_read(public, &cp) != 1) {
522                                 cp = ep;
523                                 key_free(public);
524                                 public = key_new(KEY_UNSPEC);
525                                 if (key_read(public, &cp) != 1) {
526                                         key_free(public);
527                                         continue;
528                                 }
529                         }
530                         comment = *cp ? cp : comment;
531                         fp = key_fingerprint(public, fptype, rep);
532                         printf("%u %s %s\n", key_size(public), fp,
533                             comment ? comment : "no comment");
534                         xfree(fp);
535                         key_free(public);
536                         invalid = 0;
537                 }
538                 fclose(f);
539         }
540         if (invalid) {
541                 printf("%s is not a public key file.\n", identity_file);
542                 exit(1);
543         }
544         exit(0);
545 }
546
547 /*
548  * Perform changing a passphrase.  The argument is the passwd structure
549  * for the current user.
550  */
551 static void
552 do_change_passphrase(struct passwd *pw)
553 {
554         char *comment;
555         char *old_passphrase, *passphrase1, *passphrase2;
556         struct stat st;
557         Key *private;
558
559         if (!have_identity)
560                 ask_filename(pw, "Enter file in which the key is");
561         if (stat(identity_file, &st) < 0) {
562                 perror(identity_file);
563                 exit(1);
564         }
565         /* Try to load the file with empty passphrase. */
566         private = key_load_private(identity_file, "", &comment);
567         if (private == NULL) {
568                 if (identity_passphrase)
569                         old_passphrase = xstrdup(identity_passphrase);
570                 else
571                         old_passphrase =
572                             read_passphrase("Enter old passphrase: ",
573                             RP_ALLOW_STDIN);
574                 private = key_load_private(identity_file, old_passphrase,
575                     &comment);
576                 memset(old_passphrase, 0, strlen(old_passphrase));
577                 xfree(old_passphrase);
578                 if (private == NULL) {
579                         printf("Bad passphrase.\n");
580                         exit(1);
581                 }
582         }
583         printf("Key has comment '%s'\n", comment);
584
585         /* Ask the new passphrase (twice). */
586         if (identity_new_passphrase) {
587                 passphrase1 = xstrdup(identity_new_passphrase);
588                 passphrase2 = NULL;
589         } else {
590                 passphrase1 =
591                         read_passphrase("Enter new passphrase (empty for no "
592                             "passphrase): ", RP_ALLOW_STDIN);
593                 passphrase2 = read_passphrase("Enter same passphrase again: ",
594                     RP_ALLOW_STDIN);
595
596                 /* Verify that they are the same. */
597                 if (strcmp(passphrase1, passphrase2) != 0) {
598                         memset(passphrase1, 0, strlen(passphrase1));
599                         memset(passphrase2, 0, strlen(passphrase2));
600                         xfree(passphrase1);
601                         xfree(passphrase2);
602                         printf("Pass phrases do not match.  Try again.\n");
603                         exit(1);
604                 }
605                 /* Destroy the other copy. */
606                 memset(passphrase2, 0, strlen(passphrase2));
607                 xfree(passphrase2);
608         }
609
610         /* Save the file using the new passphrase. */
611         if (!key_save_private(private, identity_file, passphrase1, comment)) {
612                 printf("Saving the key failed: %s.\n", identity_file);
613                 memset(passphrase1, 0, strlen(passphrase1));
614                 xfree(passphrase1);
615                 key_free(private);
616                 xfree(comment);
617                 exit(1);
618         }
619         /* Destroy the passphrase and the copy of the key in memory. */
620         memset(passphrase1, 0, strlen(passphrase1));
621         xfree(passphrase1);
622         key_free(private);               /* Destroys contents */
623         xfree(comment);
624
625         printf("Your identification has been saved with the new passphrase.\n");
626         exit(0);
627 }
628
629 /*
630  * Print the SSHFP RR.
631  */
632 static void
633 do_print_resource_record(struct passwd *pw, char *hostname)
634 {
635         Key *public;
636         char *comment = NULL;
637         struct stat st;
638
639         if (!have_identity)
640                 ask_filename(pw, "Enter file in which the key is");
641         if (stat(identity_file, &st) < 0) {
642                 perror(identity_file);
643                 exit(1);
644         }
645         public = key_load_public(identity_file, &comment);
646         if (public != NULL) {
647                 export_dns_rr(hostname, public, stdout, print_generic);
648                 key_free(public);
649                 xfree(comment);
650                 exit(0);
651         }
652         if (comment)
653                 xfree(comment);
654
655         printf("failed to read v2 public key from %s.\n", identity_file);
656         exit(1);
657 }
658
659 /*
660  * Change the comment of a private key file.
661  */
662 static void
663 do_change_comment(struct passwd *pw)
664 {
665         char new_comment[1024], *comment, *passphrase;
666         Key *private;
667         Key *public;
668         struct stat st;
669         FILE *f;
670         int fd;
671
672         if (!have_identity)
673                 ask_filename(pw, "Enter file in which the key is");
674         if (stat(identity_file, &st) < 0) {
675                 perror(identity_file);
676                 exit(1);
677         }
678         private = key_load_private(identity_file, "", &comment);
679         if (private == NULL) {
680                 if (identity_passphrase)
681                         passphrase = xstrdup(identity_passphrase);
682                 else if (identity_new_passphrase)
683                         passphrase = xstrdup(identity_new_passphrase);
684                 else
685                         passphrase = read_passphrase("Enter passphrase: ",
686                             RP_ALLOW_STDIN);
687                 /* Try to load using the passphrase. */
688                 private = key_load_private(identity_file, passphrase, &comment);
689                 if (private == NULL) {
690                         memset(passphrase, 0, strlen(passphrase));
691                         xfree(passphrase);
692                         printf("Bad passphrase.\n");
693                         exit(1);
694                 }
695         } else {
696                 passphrase = xstrdup("");
697         }
698         if (private->type != KEY_RSA1) {
699                 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
700                 key_free(private);
701                 exit(1);
702         }
703         printf("Key now has comment '%s'\n", comment);
704
705         if (identity_comment) {
706                 strlcpy(new_comment, identity_comment, sizeof(new_comment));
707         } else {
708                 printf("Enter new comment: ");
709                 fflush(stdout);
710                 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
711                         memset(passphrase, 0, strlen(passphrase));
712                         key_free(private);
713                         exit(1);
714                 }
715                 if (strchr(new_comment, '\n'))
716                         *strchr(new_comment, '\n') = 0;
717         }
718
719         /* Save the file using the new passphrase. */
720         if (!key_save_private(private, identity_file, passphrase, new_comment)) {
721                 printf("Saving the key failed: %s.\n", identity_file);
722                 memset(passphrase, 0, strlen(passphrase));
723                 xfree(passphrase);
724                 key_free(private);
725                 xfree(comment);
726                 exit(1);
727         }
728         memset(passphrase, 0, strlen(passphrase));
729         xfree(passphrase);
730         public = key_from_private(private);
731         key_free(private);
732
733         strlcat(identity_file, ".pub", sizeof(identity_file));
734         fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
735         if (fd == -1) {
736                 printf("Could not save your public key in %s\n", identity_file);
737                 exit(1);
738         }
739         f = fdopen(fd, "w");
740         if (f == NULL) {
741                 printf("fdopen %s failed", identity_file);
742                 exit(1);
743         }
744         if (!key_write(public, f))
745                 fprintf(stderr, "write key failed");
746         key_free(public);
747         fprintf(f, " %s\n", new_comment);
748         fclose(f);
749
750         xfree(comment);
751
752         printf("The comment in your key file has been changed.\n");
753         exit(0);
754 }
755
756 static void
757 usage(void)
758 {
759         fprintf(stderr, "Usage: %s [options]\n", __progname);
760         fprintf(stderr, "Options:\n");
761         fprintf(stderr, "  -b bits     Number of bits in the key to create.\n");
762         fprintf(stderr, "  -c          Change comment in private and public key files.\n");
763         fprintf(stderr, "  -e          Convert OpenSSH to IETF SECSH key file.\n");
764         fprintf(stderr, "  -f filename Filename of the key file.\n");
765         fprintf(stderr, "  -g          Use generic DNS resource record format.\n");
766         fprintf(stderr, "  -i          Convert IETF SECSH to OpenSSH key file.\n");
767         fprintf(stderr, "  -l          Show fingerprint of key file.\n");
768         fprintf(stderr, "  -p          Change passphrase of private key file.\n");
769         fprintf(stderr, "  -q          Quiet.\n");
770         fprintf(stderr, "  -y          Read private key file and print public key.\n");
771         fprintf(stderr, "  -t type     Specify type of key to create.\n");
772         fprintf(stderr, "  -B          Show bubblebabble digest of key file.\n");
773         fprintf(stderr, "  -C comment  Provide new comment.\n");
774         fprintf(stderr, "  -N phrase   Provide new passphrase.\n");
775         fprintf(stderr, "  -P phrase   Provide old passphrase.\n");
776         fprintf(stderr, "  -r hostname Print DNS resource record.\n");
777 #ifdef SMARTCARD
778         fprintf(stderr, "  -D reader   Download public key from smartcard.\n");
779         fprintf(stderr, "  -U reader   Upload private key to smartcard.\n");
780 #endif /* SMARTCARD */
781
782         fprintf(stderr, "  -G file     Generate candidates for DH-GEX moduli\n");
783         fprintf(stderr, "  -T file     Screen candidates for DH-GEX moduli\n");
784
785         exit(1);
786 }
787
788 /*
789  * Main program for key management.
790  */
791 int
792 main(int ac, char **av)
793 {
794         char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
795         char out_file[MAXPATHLEN], *reader_id = NULL;
796         char *resource_record_hostname = NULL;
797         Key *private, *public;
798         struct passwd *pw;
799         struct stat st;
800         int opt, type, fd, download = 0, memory = 0;
801         int generator_wanted = 0, trials = 100;
802         int do_gen_candidates = 0, do_screen_candidates = 0;
803         int log_level = SYSLOG_LEVEL_INFO;
804         BIGNUM *start = NULL;
805         FILE *f;
806
807         extern int optind;
808         extern char *optarg;
809
810         __progname = ssh_get_progname(av[0]);
811
812         SSLeay_add_all_algorithms();
813         log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
814
815         init_rng();
816         seed_rng();
817
818         /* we need this for the home * directory.  */
819         pw = getpwuid(getuid());
820         if (!pw) {
821                 printf("You don't exist, go away!\n");
822                 exit(1);
823         }
824         if (gethostname(hostname, sizeof(hostname)) < 0) {
825                 perror("gethostname");
826                 exit(1);
827         }
828
829         while ((opt = getopt(ac, av,
830             "degiqpclBRvxXyb:f:t:U:D:P:N:C:r:g:T:G:M:S:a:W:")) != -1) {
831                 switch (opt) {
832                 case 'b':
833                         bits = atoi(optarg);
834                         if (bits < 512 || bits > 32768) {
835                                 printf("Bits has bad value.\n");
836                                 exit(1);
837                         }
838                         break;
839                 case 'l':
840                         print_fingerprint = 1;
841                         break;
842                 case 'B':
843                         print_bubblebabble = 1;
844                         break;
845                 case 'p':
846                         change_passphrase = 1;
847                         break;
848                 case 'c':
849                         change_comment = 1;
850                         break;
851                 case 'f':
852                         strlcpy(identity_file, optarg, sizeof(identity_file));
853                         have_identity = 1;
854                         break;
855                 case 'g':
856                         print_generic = 1;
857                         break;
858                 case 'P':
859                         identity_passphrase = optarg;
860                         break;
861                 case 'N':
862                         identity_new_passphrase = optarg;
863                         break;
864                 case 'C':
865                         identity_comment = optarg;
866                         break;
867                 case 'q':
868                         quiet = 1;
869                         break;
870                 case 'R':
871                         /* unused */
872                         exit(0);
873                         break;
874                 case 'e':
875                 case 'x':
876                         /* export key */
877                         convert_to_ssh2 = 1;
878                         break;
879                 case 'i':
880                 case 'X':
881                         /* import key */
882                         convert_from_ssh2 = 1;
883                         break;
884                 case 'y':
885                         print_public = 1;
886                         break;
887                 case 'd':
888                         key_type_name = "dsa";
889                         break;
890                 case 't':
891                         key_type_name = optarg;
892                         break;
893                 case 'D':
894                         download = 1;
895                 case 'U':
896                         reader_id = optarg;
897                         break;
898                 case 'v':
899                         if (log_level == SYSLOG_LEVEL_INFO)
900                                 log_level = SYSLOG_LEVEL_DEBUG1;
901                         else {
902                                 if (log_level >= SYSLOG_LEVEL_DEBUG1 && 
903                                     log_level < SYSLOG_LEVEL_DEBUG3)
904                                         log_level++;
905                         }
906                         break;
907                 case 'r':
908                         resource_record_hostname = optarg;
909                         break;
910                 case 'W':
911                         generator_wanted = atoi(optarg);
912                         if (generator_wanted < 1)
913                                 fatal("Desired generator has bad value.");
914                         break;
915                 case 'a':
916                         trials = atoi(optarg);
917                         break;
918                 case 'M':
919                         memory = atoi(optarg);
920                         break;
921                 case 'G':
922                         do_gen_candidates = 1;
923                         strlcpy(out_file, optarg, sizeof(out_file));
924                         break;
925                 case 'T':
926                         do_screen_candidates = 1;
927                         strlcpy(out_file, optarg, sizeof(out_file));
928                         break;
929                 case 'S':
930                         /* XXX - also compare length against bits */
931                         if (BN_hex2bn(&start, optarg) == 0)
932                                 fatal("Invalid start point.");
933                         break;
934                 case '?':
935                 default:
936                         usage();
937                 }
938         }
939
940         /* reinit */
941         log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
942
943         if (optind < ac) {
944                 printf("Too many arguments.\n");
945                 usage();
946         }
947         if (change_passphrase && change_comment) {
948                 printf("Can only have one of -p and -c.\n");
949                 usage();
950         }
951         if (print_fingerprint || print_bubblebabble)
952                 do_fingerprint(pw);
953         if (change_passphrase)
954                 do_change_passphrase(pw);
955         if (change_comment)
956                 do_change_comment(pw);
957         if (convert_to_ssh2)
958                 do_convert_to_ssh2(pw);
959         if (convert_from_ssh2)
960                 do_convert_from_ssh2(pw);
961         if (print_public)
962                 do_print_public(pw);
963         if (resource_record_hostname != NULL) {
964                 do_print_resource_record(pw, resource_record_hostname);
965         }
966         if (reader_id != NULL) {
967 #ifdef SMARTCARD
968                 if (download)
969                         do_download(pw, reader_id);
970                 else
971                         do_upload(pw, reader_id);
972 #else /* SMARTCARD */
973                 fatal("no support for smartcards.");
974 #endif /* SMARTCARD */
975         }
976
977         if (do_gen_candidates) {
978                 FILE *out = fopen(out_file, "w");
979
980                 if (out == NULL) {
981                         error("Couldn't open modulus candidate file \"%s\": %s",
982                             out_file, strerror(errno));
983                         return (1);
984                 }
985                 if (gen_candidates(out, memory, bits, start) != 0)
986                         fatal("modulus candidate generation failed\n");
987
988                 return (0);
989         }
990
991         if (do_screen_candidates) {
992                 FILE *in;
993                 FILE *out = fopen(out_file, "w");
994
995                 if (have_identity && strcmp(identity_file, "-") != 0) {
996                         if ((in = fopen(identity_file, "r")) == NULL) {
997                                 fatal("Couldn't open modulus candidate "
998                                     "file \"%s\": %s", identity_file,
999                                     strerror(errno));
1000                         }
1001                 } else
1002                         in = stdin;
1003
1004                 if (out == NULL) {
1005                         fatal("Couldn't open moduli file \"%s\": %s",
1006                             out_file, strerror(errno));
1007                 }
1008                 if (prime_test(in, out, trials, generator_wanted) != 0)
1009                         fatal("modulus screening failed\n");
1010                 return (0);
1011         }
1012
1013         arc4random_stir();
1014
1015         if (key_type_name == NULL) {
1016                 printf("You must specify a key type (-t).\n");
1017                 usage();
1018         }
1019         type = key_type_from_name(key_type_name);
1020         if (type == KEY_UNSPEC) {
1021                 fprintf(stderr, "unknown key type %s\n", key_type_name);
1022                 exit(1);
1023         }
1024         if (!quiet)
1025                 printf("Generating public/private %s key pair.\n", key_type_name);
1026         private = key_generate(type, bits);
1027         if (private == NULL) {
1028                 fprintf(stderr, "key_generate failed");
1029                 exit(1);
1030         }
1031         public  = key_from_private(private);
1032
1033         if (!have_identity)
1034                 ask_filename(pw, "Enter file in which to save the key");
1035
1036         /* Create ~/.ssh directory if it doesn\'t already exist. */
1037         snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1038         if (strstr(identity_file, dotsshdir) != NULL &&
1039             stat(dotsshdir, &st) < 0) {
1040                 if (mkdir(dotsshdir, 0700) < 0)
1041                         error("Could not create directory '%s'.", dotsshdir);
1042                 else if (!quiet)
1043                         printf("Created directory '%s'.\n", dotsshdir);
1044         }
1045         /* If the file already exists, ask the user to confirm. */
1046         if (stat(identity_file, &st) >= 0) {
1047                 char yesno[3];
1048                 printf("%s already exists.\n", identity_file);
1049                 printf("Overwrite (y/n)? ");
1050                 fflush(stdout);
1051                 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1052                         exit(1);
1053                 if (yesno[0] != 'y' && yesno[0] != 'Y')
1054                         exit(1);
1055         }
1056         /* Ask for a passphrase (twice). */
1057         if (identity_passphrase)
1058                 passphrase1 = xstrdup(identity_passphrase);
1059         else if (identity_new_passphrase)
1060                 passphrase1 = xstrdup(identity_new_passphrase);
1061         else {
1062 passphrase_again:
1063                 passphrase1 =
1064                         read_passphrase("Enter passphrase (empty for no "
1065                             "passphrase): ", RP_ALLOW_STDIN);
1066                 passphrase2 = read_passphrase("Enter same passphrase again: ",
1067                     RP_ALLOW_STDIN);
1068                 if (strcmp(passphrase1, passphrase2) != 0) {
1069                         /*
1070                          * The passphrases do not match.  Clear them and
1071                          * retry.
1072                          */
1073                         memset(passphrase1, 0, strlen(passphrase1));
1074                         memset(passphrase2, 0, strlen(passphrase2));
1075                         xfree(passphrase1);
1076                         xfree(passphrase2);
1077                         printf("Passphrases do not match.  Try again.\n");
1078                         goto passphrase_again;
1079                 }
1080                 /* Clear the other copy of the passphrase. */
1081                 memset(passphrase2, 0, strlen(passphrase2));
1082                 xfree(passphrase2);
1083         }
1084
1085         if (identity_comment) {
1086                 strlcpy(comment, identity_comment, sizeof(comment));
1087         } else {
1088                 /* Create default commend field for the passphrase. */
1089                 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1090         }
1091
1092         /* Save the key with the given passphrase and comment. */
1093         if (!key_save_private(private, identity_file, passphrase1, comment)) {
1094                 printf("Saving the key failed: %s.\n", identity_file);
1095                 memset(passphrase1, 0, strlen(passphrase1));
1096                 xfree(passphrase1);
1097                 exit(1);
1098         }
1099         /* Clear the passphrase. */
1100         memset(passphrase1, 0, strlen(passphrase1));
1101         xfree(passphrase1);
1102
1103         /* Clear the private key and the random number generator. */
1104         key_free(private);
1105         arc4random_stir();
1106
1107         if (!quiet)
1108                 printf("Your identification has been saved in %s.\n", identity_file);
1109
1110         strlcat(identity_file, ".pub", sizeof(identity_file));
1111         fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1112         if (fd == -1) {
1113                 printf("Could not save your public key in %s\n", identity_file);
1114                 exit(1);
1115         }
1116         f = fdopen(fd, "w");
1117         if (f == NULL) {
1118                 printf("fdopen %s failed", identity_file);
1119                 exit(1);
1120         }
1121         if (!key_write(public, f))
1122                 fprintf(stderr, "write key failed");
1123         fprintf(f, " %s\n", comment);
1124         fclose(f);
1125
1126         if (!quiet) {
1127                 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1128                 printf("Your public key has been saved in %s.\n",
1129                     identity_file);
1130                 printf("The key fingerprint is:\n");
1131                 printf("%s %s\n", fp, comment);
1132                 xfree(fp);
1133         }
1134
1135         key_free(public);
1136         exit(0);
1137 }
This page took 0.140947 seconds and 5 git commands to generate.