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