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