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