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