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