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