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