Annotation of src/usr.bin/ssh/ssh-keygen.c, Revision 1.71
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.71 ! markus 15: RCSID("$OpenBSD: ssh-keygen.c,v 1.70 2001/06/29 07:06:34 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;
1.69 stevesk 193: u_char *sig, data[] = "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);
1.68 markus 263: rsa_generate_additional_parameters(key->rsa);
1.53 markus 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); \
1.70 markus 384: debug("#bytes %d", len); \
1.66 markus 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;
1.70 markus 402: int r1 = 0, r2 = 0, cla = 0x00;
1.66 markus 403:
1.71 ! markus 404: for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
! 405: elements[i] = NULL;
1.66 markus 406: if (!have_identity)
407: ask_filename(pw, "Enter file in which the key is");
408: if (stat(identity_file, &st) < 0) {
409: perror(identity_file);
410: goto done;
411: }
412: prv = load_identity(identity_file);
413: if (prv == NULL) {
414: error("load failed");
415: goto done;
416: }
417: COPY_RSA_KEY(q, 0);
418: COPY_RSA_KEY(p, 1);
419: COPY_RSA_KEY(iqmp, 2);
420: COPY_RSA_KEY(dmq1, 3);
421: COPY_RSA_KEY(dmp1, 4);
422: COPY_RSA_KEY(n, 5);
423: len = BN_num_bytes(prv->rsa->n);
424: fd = scopen(reader, 0, NULL);
425: if (fd < 0) {
1.70 markus 426: error("scopen failed");
1.66 markus 427: goto done;
428: }
429: ret = screset(fd, atr, NULL);
430: if (ret <= 0) {
1.70 markus 431: error("screset failed");
1.66 markus 432: goto done;
433: }
1.67 markus 434: if ((cla = cyberflex_inq_class(fd)) < 0) {
435: error("cyberflex_inq_class failed");
436: goto done;
437: }
1.66 markus 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;
1.70 markus 444: if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
445: &r1, &r2) < 0) {
446: error("cyberflex_load_rsa_priv failed: %s", get_r1r2s(r1, r1));
447: goto done;
448: }
449: if (r1 != 0x90 && r1 != 0x61)
1.66 markus 450: goto done;
451: log("cyberflex_load_rsa_priv done");
452: key_fid[0] = 0x73;
453: key_fid[1] = 0x68;
1.70 markus 454: if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
455: &r1, &r2) < 0) {
456: error("cyberflex_load_rsa_pub failed: %s", get_r1r2s(r1, r1));
457: goto done;
458: }
459: if (r1 != 0x90 && r1 != 0x61)
1.66 markus 460: goto done;
461: log("cyberflex_load_rsa_pub done");
462: status = 0;
463: log("loading key done");
464: done:
465: if (prv)
466: key_free(prv);
467: for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
468: xfree(elements[i]);
469: if (fd != -1)
470: scclose(fd);
471: exit(status);
472: #endif
473: }
474:
1.63 itojun 475: static void
1.8 markus 476: do_fingerprint(struct passwd *pw)
477: {
1.15 markus 478: FILE *f;
1.19 markus 479: Key *public;
1.49 markus 480: char *comment = NULL, *cp, *ep, line[16*1024], *fp;
1.52 markus 481: int i, skip = 0, num = 1, invalid = 1, rep, fptype;
1.12 markus 482: struct stat st;
483:
1.52 markus 484: fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
485: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
1.49 markus 486:
1.12 markus 487: if (!have_identity)
488: ask_filename(pw, "Enter file in which the key is");
489: if (stat(identity_file, &st) < 0) {
490: perror(identity_file);
491: exit(1);
492: }
1.52 markus 493: public = key_load_public(identity_file, &comment);
494: if (public != NULL) {
495: fp = key_fingerprint(public, fptype, rep);
496: printf("%d %s %s\n", key_size(public), fp, comment);
1.33 markus 497: key_free(public);
498: xfree(comment);
1.49 markus 499: xfree(fp);
1.15 markus 500: exit(0);
501: }
1.52 markus 502: if (comment)
503: xfree(comment);
1.15 markus 504:
505: f = fopen(identity_file, "r");
506: if (f != NULL) {
507: while (fgets(line, sizeof(line), f)) {
508: i = strlen(line) - 1;
509: if (line[i] != '\n') {
510: error("line %d too long: %.40s...", num, line);
511: skip = 1;
512: continue;
513: }
514: num++;
515: if (skip) {
516: skip = 0;
517: continue;
518: }
519: line[i] = '\0';
520:
521: /* Skip leading whitespace, empty and comment lines. */
522: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
523: ;
524: if (!*cp || *cp == '\n' || *cp == '#')
525: continue ;
526: i = strtol(cp, &ep, 10);
527: if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
528: int quoted = 0;
529: comment = cp;
530: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
531: if (*cp == '\\' && cp[1] == '"')
532: cp++; /* Skip both */
533: else if (*cp == '"')
534: quoted = !quoted;
535: }
536: if (!*cp)
537: continue;
538: *cp++ = '\0';
539: }
540: ep = cp;
1.38 markus 541: public = key_new(KEY_RSA1);
542: if (key_read(public, &cp) != 1) {
543: cp = ep;
544: key_free(public);
545: public = key_new(KEY_UNSPEC);
546: if (key_read(public, &cp) != 1) {
547: key_free(public);
548: continue;
549: }
1.12 markus 550: }
1.38 markus 551: comment = *cp ? cp : comment;
1.52 markus 552: fp = key_fingerprint(public, fptype, rep);
1.49 markus 553: printf("%d %s %s\n", key_size(public), fp,
1.38 markus 554: comment ? comment : "no comment");
1.49 markus 555: xfree(fp);
1.52 markus 556: key_free(public);
1.38 markus 557: invalid = 0;
1.12 markus 558: }
1.15 markus 559: fclose(f);
560: }
561: if (invalid) {
562: printf("%s is not a valid key file.\n", identity_file);
563: exit(1);
1.12 markus 564: }
565: exit(0);
1.8 markus 566: }
567:
1.13 deraadt 568: /*
569: * Perform changing a passphrase. The argument is the passwd structure
570: * for the current user.
571: */
1.63 itojun 572: static void
1.7 markus 573: do_change_passphrase(struct passwd *pw)
574: {
1.12 markus 575: char *comment;
576: char *old_passphrase, *passphrase1, *passphrase2;
577: struct stat st;
1.19 markus 578: Key *private;
1.12 markus 579:
580: if (!have_identity)
581: ask_filename(pw, "Enter file in which the key is");
582: if (stat(identity_file, &st) < 0) {
583: perror(identity_file);
584: exit(1);
585: }
586: /* Try to load the file with empty passphrase. */
1.52 markus 587: private = key_load_private(identity_file, "", &comment);
588: if (private == NULL) {
1.12 markus 589: if (identity_passphrase)
590: old_passphrase = xstrdup(identity_passphrase);
591: else
1.65 markus 592: old_passphrase =
593: read_passphrase("Enter old passphrase: ",
594: RP_ALLOW_STDIN);
595: private = key_load_private(identity_file, old_passphrase,
596: &comment);
1.52 markus 597: memset(old_passphrase, 0, strlen(old_passphrase));
598: xfree(old_passphrase);
599: if (private == NULL) {
1.12 markus 600: printf("Bad passphrase.\n");
601: exit(1);
602: }
603: }
604: printf("Key has comment '%s'\n", comment);
605:
606: /* Ask the new passphrase (twice). */
607: if (identity_new_passphrase) {
608: passphrase1 = xstrdup(identity_new_passphrase);
609: passphrase2 = NULL;
610: } else {
611: passphrase1 =
1.65 markus 612: read_passphrase("Enter new passphrase (empty for no "
613: "passphrase): ", RP_ALLOW_STDIN);
614: passphrase2 = read_passphrase("Enter same passphrase again: ",
615: RP_ALLOW_STDIN);
1.12 markus 616:
617: /* Verify that they are the same. */
618: if (strcmp(passphrase1, passphrase2) != 0) {
619: memset(passphrase1, 0, strlen(passphrase1));
620: memset(passphrase2, 0, strlen(passphrase2));
621: xfree(passphrase1);
622: xfree(passphrase2);
623: printf("Pass phrases do not match. Try again.\n");
624: exit(1);
625: }
626: /* Destroy the other copy. */
627: memset(passphrase2, 0, strlen(passphrase2));
628: xfree(passphrase2);
629: }
630:
631: /* Save the file using the new passphrase. */
1.52 markus 632: if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56 markus 633: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 634: memset(passphrase1, 0, strlen(passphrase1));
635: xfree(passphrase1);
1.19 markus 636: key_free(private);
1.12 markus 637: xfree(comment);
638: exit(1);
639: }
640: /* Destroy the passphrase and the copy of the key in memory. */
641: memset(passphrase1, 0, strlen(passphrase1));
642: xfree(passphrase1);
1.19 markus 643: key_free(private); /* Destroys contents */
1.12 markus 644: xfree(comment);
1.1 deraadt 645:
1.12 markus 646: printf("Your identification has been saved with the new passphrase.\n");
647: exit(0);
1.1 deraadt 648: }
649:
1.13 deraadt 650: /*
651: * Change the comment of a private key file.
652: */
1.63 itojun 653: static void
1.2 provos 654: do_change_comment(struct passwd *pw)
1.1 deraadt 655: {
1.46 deraadt 656: char new_comment[1024], *comment, *passphrase;
1.52 markus 657: Key *private;
658: Key *public;
1.12 markus 659: struct stat st;
660: FILE *f;
1.46 deraadt 661: int fd;
1.12 markus 662:
663: if (!have_identity)
664: ask_filename(pw, "Enter file in which the key is");
665: if (stat(identity_file, &st) < 0) {
666: perror(identity_file);
667: exit(1);
668: }
1.52 markus 669: private = key_load_private(identity_file, "", &comment);
670: if (private == NULL) {
1.12 markus 671: if (identity_passphrase)
672: passphrase = xstrdup(identity_passphrase);
673: else if (identity_new_passphrase)
674: passphrase = xstrdup(identity_new_passphrase);
675: else
1.65 markus 676: passphrase = read_passphrase("Enter passphrase: ",
677: RP_ALLOW_STDIN);
1.12 markus 678: /* Try to load using the passphrase. */
1.52 markus 679: private = key_load_private(identity_file, passphrase, &comment);
680: if (private == NULL) {
1.12 markus 681: memset(passphrase, 0, strlen(passphrase));
682: xfree(passphrase);
683: printf("Bad passphrase.\n");
684: exit(1);
685: }
1.52 markus 686: } else {
687: passphrase = xstrdup("");
1.12 markus 688: }
1.52 markus 689: if (private->type != KEY_RSA1) {
690: fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
691: key_free(private);
692: exit(1);
693: }
1.12 markus 694: printf("Key now has comment '%s'\n", comment);
695:
696: if (identity_comment) {
697: strlcpy(new_comment, identity_comment, sizeof(new_comment));
698: } else {
699: printf("Enter new comment: ");
700: fflush(stdout);
701: if (!fgets(new_comment, sizeof(new_comment), stdin)) {
702: memset(passphrase, 0, strlen(passphrase));
1.19 markus 703: key_free(private);
1.12 markus 704: exit(1);
705: }
706: if (strchr(new_comment, '\n'))
707: *strchr(new_comment, '\n') = 0;
708: }
709:
710: /* Save the file using the new passphrase. */
1.52 markus 711: if (!key_save_private(private, identity_file, passphrase, new_comment)) {
1.56 markus 712: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 713: memset(passphrase, 0, strlen(passphrase));
714: xfree(passphrase);
1.19 markus 715: key_free(private);
1.12 markus 716: xfree(comment);
717: exit(1);
718: }
719: memset(passphrase, 0, strlen(passphrase));
720: xfree(passphrase);
1.52 markus 721: public = key_from_private(private);
1.19 markus 722: key_free(private);
1.12 markus 723:
724: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 725: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
726: if (fd == -1) {
1.12 markus 727: printf("Could not save your public key in %s\n", identity_file);
728: exit(1);
729: }
1.46 deraadt 730: f = fdopen(fd, "w");
731: if (f == NULL) {
732: printf("fdopen %s failed", identity_file);
733: exit(1);
734: }
1.19 markus 735: if (!key_write(public, f))
736: fprintf(stderr, "write key failed");
737: key_free(public);
738: fprintf(f, " %s\n", new_comment);
1.12 markus 739: fclose(f);
1.1 deraadt 740:
1.12 markus 741: xfree(comment);
1.1 deraadt 742:
1.12 markus 743: printf("The comment in your key file has been changed.\n");
744: exit(0);
1.1 deraadt 745: }
746:
1.63 itojun 747: static void
1.10 markus 748: usage(void)
749: {
1.58 markus 750: printf("Usage: %s [-ceilpqyB] [-t type] [-b bits] [-f file] [-C comment] "
1.44 deraadt 751: "[-N new-pass] [-P pass]\n", __progname);
1.12 markus 752: exit(1);
1.10 markus 753: }
754:
1.13 deraadt 755: /*
756: * Main program for key management.
757: */
1.2 provos 758: int
759: main(int ac, char **av)
1.1 deraadt 760: {
1.12 markus 761: char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
1.46 deraadt 762: Key *private, *public;
1.12 markus 763: struct passwd *pw;
1.66 markus 764: int opt, type, fd, reader = -1;
1.12 markus 765: struct stat st;
766: FILE *f;
1.33 markus 767:
1.12 markus 768: extern int optind;
769: extern char *optarg;
770:
1.26 markus 771: SSLeay_add_all_algorithms();
1.19 markus 772:
1.14 markus 773: /* we need this for the home * directory. */
1.12 markus 774: pw = getpwuid(getuid());
775: if (!pw) {
776: printf("You don't exist, go away!\n");
777: exit(1);
778: }
1.19 markus 779: if (gethostname(hostname, sizeof(hostname)) < 0) {
780: perror("gethostname");
781: exit(1);
782: }
1.14 markus 783:
1.66 markus 784: while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:u:P:N:C:")) != -1) {
1.12 markus 785: switch (opt) {
786: case 'b':
787: bits = atoi(optarg);
788: if (bits < 512 || bits > 32768) {
789: printf("Bits has bad value.\n");
790: exit(1);
791: }
792: break;
793: case 'l':
794: print_fingerprint = 1;
795: break;
1.49 markus 796: case 'B':
797: print_bubblebabble = 1;
798: break;
1.12 markus 799: case 'p':
800: change_passphrase = 1;
801: break;
802: case 'c':
803: change_comment = 1;
804: break;
805: case 'f':
806: strlcpy(identity_file, optarg, sizeof(identity_file));
807: have_identity = 1;
808: break;
809: case 'P':
810: identity_passphrase = optarg;
811: break;
812: case 'N':
813: identity_new_passphrase = optarg;
814: break;
815: case 'C':
816: identity_comment = optarg;
817: break;
818: case 'q':
819: quiet = 1;
1.20 deraadt 820: break;
821: case 'R':
1.33 markus 822: /* unused */
823: exit(0);
1.12 markus 824: break;
1.57 markus 825: case 'e':
1.19 markus 826: case 'x':
1.57 markus 827: /* export key */
1.19 markus 828: convert_to_ssh2 = 1;
829: break;
1.57 markus 830: case 'i':
1.19 markus 831: case 'X':
1.57 markus 832: /* import key */
1.19 markus 833: convert_from_ssh2 = 1;
834: break;
835: case 'y':
836: print_public = 1;
1.47 jakob 837: break;
1.19 markus 838: case 'd':
1.33 markus 839: key_type_name = "dsa";
1.19 markus 840: break;
1.33 markus 841: case 't':
842: key_type_name = optarg;
843: break;
1.66 markus 844: case 'u':
845: reader = atoi(optarg); /*XXX*/
846: break;
1.12 markus 847: case '?':
848: default:
849: usage();
850: }
851: }
852: if (optind < ac) {
853: printf("Too many arguments.\n");
854: usage();
855: }
856: if (change_passphrase && change_comment) {
857: printf("Can only have one of -p and -c.\n");
858: usage();
859: }
1.49 markus 860: if (print_fingerprint || print_bubblebabble)
1.12 markus 861: do_fingerprint(pw);
862: if (change_passphrase)
863: do_change_passphrase(pw);
864: if (change_comment)
865: do_change_comment(pw);
1.19 markus 866: if (convert_to_ssh2)
867: do_convert_to_ssh2(pw);
868: if (convert_from_ssh2)
869: do_convert_from_ssh2(pw);
870: if (print_public)
871: do_print_public(pw);
1.66 markus 872: if (reader != -1)
873: do_upload(pw, reader);
1.12 markus 874:
875: arc4random_stir();
876:
1.35 markus 877: type = key_type_from_name(key_type_name);
878: if (type == KEY_UNSPEC) {
879: fprintf(stderr, "unknown key type %s\n", key_type_name);
880: exit(1);
1.19 markus 881: }
1.33 markus 882: if (!quiet)
1.35 markus 883: printf("Generating public/private %s key pair.\n", key_type_name);
1.33 markus 884: private = key_generate(type, bits);
885: if (private == NULL) {
886: fprintf(stderr, "key_generate failed");
887: exit(1);
888: }
889: public = key_from_private(private);
1.12 markus 890:
891: if (!have_identity)
892: ask_filename(pw, "Enter file in which to save the key");
893:
894: /* Create ~/.ssh directory if it doesn\'t already exist. */
1.40 markus 895: snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1.12 markus 896: if (strstr(identity_file, dotsshdir) != NULL &&
897: stat(dotsshdir, &st) < 0) {
1.29 djm 898: if (mkdir(dotsshdir, 0700) < 0)
1.12 markus 899: error("Could not create directory '%s'.", dotsshdir);
900: else if (!quiet)
901: printf("Created directory '%s'.\n", dotsshdir);
902: }
903: /* If the file already exists, ask the user to confirm. */
904: if (stat(identity_file, &st) >= 0) {
905: char yesno[3];
906: printf("%s already exists.\n", identity_file);
907: printf("Overwrite (y/n)? ");
908: fflush(stdout);
909: if (fgets(yesno, sizeof(yesno), stdin) == NULL)
910: exit(1);
911: if (yesno[0] != 'y' && yesno[0] != 'Y')
912: exit(1);
913: }
914: /* Ask for a passphrase (twice). */
915: if (identity_passphrase)
916: passphrase1 = xstrdup(identity_passphrase);
917: else if (identity_new_passphrase)
918: passphrase1 = xstrdup(identity_new_passphrase);
919: else {
920: passphrase_again:
921: passphrase1 =
1.65 markus 922: read_passphrase("Enter passphrase (empty for no "
923: "passphrase): ", RP_ALLOW_STDIN);
924: passphrase2 = read_passphrase("Enter same passphrase again: ",
925: RP_ALLOW_STDIN);
1.12 markus 926: if (strcmp(passphrase1, passphrase2) != 0) {
1.65 markus 927: /*
928: * The passphrases do not match. Clear them and
929: * retry.
930: */
1.12 markus 931: memset(passphrase1, 0, strlen(passphrase1));
932: memset(passphrase2, 0, strlen(passphrase2));
933: xfree(passphrase1);
934: xfree(passphrase2);
935: printf("Passphrases do not match. Try again.\n");
936: goto passphrase_again;
937: }
938: /* Clear the other copy of the passphrase. */
939: memset(passphrase2, 0, strlen(passphrase2));
940: xfree(passphrase2);
941: }
942:
943: if (identity_comment) {
944: strlcpy(comment, identity_comment, sizeof(comment));
945: } else {
1.18 markus 946: /* Create default commend field for the passphrase. */
1.12 markus 947: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
948: }
949:
950: /* Save the key with the given passphrase and comment. */
1.52 markus 951: if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56 markus 952: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 953: memset(passphrase1, 0, strlen(passphrase1));
954: xfree(passphrase1);
955: exit(1);
956: }
957: /* Clear the passphrase. */
958: memset(passphrase1, 0, strlen(passphrase1));
959: xfree(passphrase1);
960:
961: /* Clear the private key and the random number generator. */
1.33 markus 962: key_free(private);
1.12 markus 963: arc4random_stir();
964:
965: if (!quiet)
966: printf("Your identification has been saved in %s.\n", identity_file);
967:
968: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 969: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
970: if (fd == -1) {
1.12 markus 971: printf("Could not save your public key in %s\n", identity_file);
1.46 deraadt 972: exit(1);
973: }
974: f = fdopen(fd, "w");
975: if (f == NULL) {
976: printf("fdopen %s failed", identity_file);
1.12 markus 977: exit(1);
978: }
1.19 markus 979: if (!key_write(public, f))
980: fprintf(stderr, "write key failed");
981: fprintf(f, " %s\n", comment);
1.12 markus 982: fclose(f);
983:
984: if (!quiet) {
1.50 markus 985: char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1.19 markus 986: printf("Your public key has been saved in %s.\n",
987: identity_file);
1.12 markus 988: printf("The key fingerprint is:\n");
1.50 markus 989: printf("%s %s\n", fp, comment);
990: xfree(fp);
1.12 markus 991: }
1.19 markus 992:
993: key_free(public);
1.12 markus 994: exit(0);
1.1 deraadt 995: }