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