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