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