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