Annotation of src/usr.bin/ssh/ssh-keygen.c, Revision 1.204
1.204 ! djm 1: /* $OpenBSD: ssh-keygen.c,v 1.203 2010/09/02 17:21:50 naddy Exp $ */
1.1 deraadt 2: /*
1.13 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Identity and host key generation and maintenance.
1.31 deraadt 7: *
8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.13 deraadt 13: */
1.1 deraadt 14:
1.136 stevesk 15: #include <sys/types.h>
1.174 dtucker 16: #include <sys/socket.h>
1.136 stevesk 17: #include <sys/stat.h>
1.151 stevesk 18: #include <sys/param.h>
1.19 markus 19:
20: #include <openssl/evp.h>
21: #include <openssl/pem.h>
1.145 stevesk 22:
1.148 stevesk 23: #include <errno.h>
1.147 stevesk 24: #include <fcntl.h>
1.145 stevesk 25: #include <pwd.h>
1.153 stevesk 26: #include <stdio.h>
1.152 stevesk 27: #include <stdlib.h>
1.150 stevesk 28: #include <string.h>
1.149 stevesk 29: #include <unistd.h>
1.1 deraadt 30:
31: #include "xmalloc.h"
1.19 markus 32: #include "key.h"
1.53 markus 33: #include "rsa.h"
1.19 markus 34: #include "authfile.h"
35: #include "uuencode.h"
1.32 markus 36: #include "buffer.h"
1.40 markus 37: #include "pathnames.h"
1.41 markus 38: #include "log.h"
1.114 djm 39: #include "misc.h"
1.119 djm 40: #include "match.h"
41: #include "hostfile.h"
1.146 stevesk 42: #include "dns.h"
1.179 djm 43: #include "ssh2.h"
1.32 markus 44:
1.177 markus 45: #ifdef ENABLE_PKCS11
46: #include "ssh-pkcs11.h"
1.106 djm 47: #endif
1.66 markus 48:
1.130 markus 49: /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
50: #define DEFAULT_BITS 2048
51: #define DEFAULT_BITS_DSA 1024
1.203 naddy 52: #define DEFAULT_BITS_ECDSA 256
1.130 markus 53: u_int32_t bits = 0;
1.1 deraadt 54:
1.14 markus 55: /*
56: * Flag indicating that we just want to change the passphrase. This can be
57: * set on the command line.
58: */
1.1 deraadt 59: int change_passphrase = 0;
60:
1.14 markus 61: /*
62: * Flag indicating that we just want to change the comment. This can be set
63: * on the command line.
64: */
1.1 deraadt 65: int change_comment = 0;
66:
1.2 provos 67: int quiet = 0;
68:
1.169 grunk 69: int log_level = SYSLOG_LEVEL_INFO;
70:
1.119 djm 71: /* Flag indicating that we want to hash a known_hosts file */
72: int hash_hosts = 0;
73: /* Flag indicating that we want lookup a host in known_hosts file */
74: int find_host = 0;
75: /* Flag indicating that we want to delete a host from a known_hosts file */
76: int delete_host = 0;
77:
1.182 djm 78: /* Flag indicating that we want to show the contents of a certificate */
79: int show_cert = 0;
80:
1.8 markus 81: /* Flag indicating that we just want to see the key fingerprint */
82: int print_fingerprint = 0;
1.49 markus 83: int print_bubblebabble = 0;
1.8 markus 84:
1.10 markus 85: /* The identity file name, given on the command line or entered by the user. */
86: char identity_file[1024];
87: int have_identity = 0;
1.1 deraadt 88:
89: /* This is set to the passphrase if given on the command line. */
90: char *identity_passphrase = NULL;
91:
92: /* This is set to the new passphrase if given on the command line. */
93: char *identity_new_passphrase = NULL;
94:
95: /* This is set to the new comment if given on the command line. */
96: char *identity_comment = NULL;
97:
1.179 djm 98: /* Path to CA key when certifying keys. */
99: char *ca_key_path = NULL;
100:
1.186 djm 101: /* Certificate serial number */
102: long long cert_serial = 0;
103:
1.179 djm 104: /* Key type when certifying */
105: u_int cert_key_type = SSH2_CERT_TYPE_USER;
106:
107: /* "key ID" of signed key */
108: char *cert_key_id = NULL;
109:
110: /* Comma-separated list of principal names for certifying keys */
111: char *cert_principals = NULL;
112:
113: /* Validity period for certificates */
114: u_int64_t cert_valid_from = 0;
115: u_int64_t cert_valid_to = ~0ULL;
116:
1.186 djm 117: /* Certificate options */
1.190 djm 118: #define CERTOPT_X_FWD (1)
119: #define CERTOPT_AGENT_FWD (1<<1)
120: #define CERTOPT_PORT_FWD (1<<2)
121: #define CERTOPT_PTY (1<<3)
122: #define CERTOPT_USER_RC (1<<4)
123: #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
124: CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
125: u_int32_t certflags_flags = CERTOPT_DEFAULT;
126: char *certflags_command = NULL;
127: char *certflags_src_addr = NULL;
1.179 djm 128:
1.193 djm 129: /* Conversion to/from various formats */
130: int convert_to = 0;
131: int convert_from = 0;
132: enum {
133: FMT_RFC4716,
134: FMT_PKCS8,
135: FMT_PEM
136: } convert_format = FMT_RFC4716;
1.19 markus 137: int print_public = 0;
1.105 jakob 138: int print_generic = 0;
1.33 markus 139:
1.87 djm 140: char *key_type_name = NULL;
1.19 markus 141:
1.197 djm 142: /* Load key from this PKCS#11 provider */
143: char *pkcs11provider = NULL;
1.193 djm 144:
1.10 markus 145: /* argv0 */
146: extern char *__progname;
1.1 deraadt 147:
1.19 markus 148: char hostname[MAXHOSTNAMELEN];
149:
1.115 djm 150: /* moduli.c */
1.124 avsm 151: int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
1.115 djm 152: int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
153:
1.63 itojun 154: static void
1.10 markus 155: ask_filename(struct passwd *pw, const char *prompt)
1.1 deraadt 156: {
1.12 markus 157: char buf[1024];
1.35 markus 158: char *name = NULL;
159:
1.92 stevesk 160: if (key_type_name == NULL)
1.40 markus 161: name = _PATH_SSH_CLIENT_ID_RSA;
1.140 deraadt 162: else {
1.92 stevesk 163: switch (key_type_from_name(key_type_name)) {
164: case KEY_RSA1:
165: name = _PATH_SSH_CLIENT_IDENTITY;
166: break;
1.186 djm 167: case KEY_DSA_CERT:
168: case KEY_DSA_CERT_V00:
1.92 stevesk 169: case KEY_DSA:
170: name = _PATH_SSH_CLIENT_ID_DSA;
171: break;
1.200 djm 172: case KEY_ECDSA_CERT:
173: case KEY_ECDSA:
174: name = _PATH_SSH_CLIENT_ID_ECDSA;
175: break;
1.186 djm 176: case KEY_RSA_CERT:
177: case KEY_RSA_CERT_V00:
1.92 stevesk 178: case KEY_RSA:
179: name = _PATH_SSH_CLIENT_ID_RSA;
180: break;
181: default:
1.173 tobias 182: fprintf(stderr, "bad key type\n");
1.92 stevesk 183: exit(1);
184: break;
185: }
1.140 deraadt 186: }
1.35 markus 187: snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
1.37 markus 188: fprintf(stderr, "%s (%s): ", prompt, identity_file);
1.12 markus 189: if (fgets(buf, sizeof(buf), stdin) == NULL)
190: exit(1);
1.162 gilles 191: buf[strcspn(buf, "\n")] = '\0';
1.12 markus 192: if (strcmp(buf, "") != 0)
193: strlcpy(identity_file, buf, sizeof(identity_file));
194: have_identity = 1;
1.7 markus 195: }
196:
1.63 itojun 197: static Key *
1.61 markus 198: load_identity(char *filename)
1.19 markus 199: {
1.52 markus 200: char *pass;
201: Key *prv;
202:
1.55 markus 203: prv = key_load_private(filename, "", NULL);
1.52 markus 204: if (prv == NULL) {
1.61 markus 205: if (identity_passphrase)
206: pass = xstrdup(identity_passphrase);
207: else
1.65 markus 208: pass = read_passphrase("Enter passphrase: ",
209: RP_ALLOW_STDIN);
1.52 markus 210: prv = key_load_private(filename, pass, NULL);
1.19 markus 211: memset(pass, 0, strlen(pass));
212: xfree(pass);
213: }
1.52 markus 214: return prv;
1.19 markus 215: }
216:
1.32 markus 217: #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
1.100 deraadt 218: #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
1.32 markus 219: #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1.42 stevesk 220: #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
1.19 markus 221:
1.63 itojun 222: static void
1.193 djm 223: do_convert_to_ssh2(struct passwd *pw, Key *k)
1.19 markus 224: {
1.94 markus 225: u_int len;
1.36 markus 226: u_char *blob;
1.176 djm 227: char comment[61];
1.19 markus 228:
1.81 markus 229: if (key_to_blob(k, &blob, &len) <= 0) {
230: fprintf(stderr, "key_to_blob failed\n");
231: exit(1);
232: }
1.176 djm 233: /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
234: snprintf(comment, sizeof(comment),
235: "%u-bit %s, converted by %s@%s from OpenSSH",
1.59 markus 236: key_size(k), key_type(k),
1.19 markus 237: pw->pw_name, hostname);
1.176 djm 238:
239: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
240: fprintf(stdout, "Comment: \"%s\"\n", comment);
1.19 markus 241: dump_base64(stdout, blob, len);
1.32 markus 242: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1.59 markus 243: key_free(k);
1.21 markus 244: xfree(blob);
1.19 markus 245: exit(0);
246: }
247:
1.63 itojun 248: static void
1.193 djm 249: do_convert_to_pkcs8(Key *k)
250: {
251: switch (key_type_plain(k->type)) {
252: case KEY_RSA:
253: if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
254: fatal("PEM_write_RSA_PUBKEY failed");
255: break;
256: case KEY_DSA:
257: if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
258: fatal("PEM_write_DSA_PUBKEY failed");
259: break;
1.200 djm 260: case KEY_ECDSA:
261: if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
262: fatal("PEM_write_EC_PUBKEY failed");
263: break;
1.193 djm 264: default:
265: fatal("%s: unsupported key type %s", __func__, key_type(k));
266: }
267: exit(0);
268: }
269:
270: static void
271: do_convert_to_pem(Key *k)
272: {
273: switch (key_type_plain(k->type)) {
274: case KEY_RSA:
275: if (!PEM_write_RSAPublicKey(stdout, k->rsa))
276: fatal("PEM_write_RSAPublicKey failed");
277: break;
278: #if notyet /* OpenSSH 0.9.8 lacks this function */
279: case KEY_DSA:
280: if (!PEM_write_DSAPublicKey(stdout, k->dsa))
281: fatal("PEM_write_DSAPublicKey failed");
282: break;
283: #endif
1.200 djm 284: /* XXX ECDSA? */
1.193 djm 285: default:
286: fatal("%s: unsupported key type %s", __func__, key_type(k));
287: }
288: exit(0);
289: }
290:
291: static void
292: do_convert_to(struct passwd *pw)
293: {
294: Key *k;
295: struct stat st;
296:
297: if (!have_identity)
298: ask_filename(pw, "Enter file in which the key is");
299: if (stat(identity_file, &st) < 0)
300: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
301: if ((k = key_load_public(identity_file, NULL)) == NULL) {
302: if ((k = load_identity(identity_file)) == NULL) {
303: fprintf(stderr, "load failed\n");
304: exit(1);
305: }
306: }
307: if (k->type == KEY_RSA1) {
308: fprintf(stderr, "version 1 keys are not supported\n");
309: exit(1);
310: }
311:
312: switch (convert_format) {
313: case FMT_RFC4716:
314: do_convert_to_ssh2(pw, k);
315: break;
316: case FMT_PKCS8:
317: do_convert_to_pkcs8(k);
318: break;
319: case FMT_PEM:
320: do_convert_to_pem(k);
321: break;
322: default:
323: fatal("%s: unknown key format %d", __func__, convert_format);
324: }
325: exit(0);
326: }
327:
328: static void
1.32 markus 329: buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
330: {
1.116 avsm 331: u_int bignum_bits = buffer_get_int(b);
332: u_int bytes = (bignum_bits + 7) / 8;
1.53 markus 333:
1.32 markus 334: if (buffer_len(b) < bytes)
1.53 markus 335: fatal("buffer_get_bignum_bits: input buffer too small: "
336: "need %d have %d", bytes, buffer_len(b));
1.155 markus 337: if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
338: fatal("buffer_get_bignum_bits: BN_bin2bn failed");
1.32 markus 339: buffer_consume(b, bytes);
340: }
341:
1.63 itojun 342: static Key *
1.93 markus 343: do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
1.32 markus 344: {
345: Buffer b;
346: Key *key = NULL;
1.64 markus 347: char *type, *cipher;
1.69 stevesk 348: u_char *sig, data[] = "abcde12345";
1.62 markus 349: int magic, rlen, ktype, i1, i2, i3, i4;
1.64 markus 350: u_int slen;
1.62 markus 351: u_long e;
1.32 markus 352:
353: buffer_init(&b);
354: buffer_append(&b, blob, blen);
355:
1.160 stevesk 356: magic = buffer_get_int(&b);
1.32 markus 357: if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
358: error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
359: buffer_free(&b);
360: return NULL;
361: }
1.62 markus 362: i1 = buffer_get_int(&b);
1.32 markus 363: type = buffer_get_string(&b, NULL);
364: cipher = buffer_get_string(&b, NULL);
1.62 markus 365: i2 = buffer_get_int(&b);
366: i3 = buffer_get_int(&b);
367: i4 = buffer_get_int(&b);
1.158 stevesk 368: debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
1.32 markus 369: if (strcmp(cipher, "none") != 0) {
370: error("unsupported cipher %s", cipher);
371: xfree(cipher);
372: buffer_free(&b);
1.53 markus 373: xfree(type);
1.32 markus 374: return NULL;
375: }
376: xfree(cipher);
377:
1.53 markus 378: if (strstr(type, "dsa")) {
379: ktype = KEY_DSA;
380: } else if (strstr(type, "rsa")) {
381: ktype = KEY_RSA;
382: } else {
1.118 markus 383: buffer_free(&b);
1.53 markus 384: xfree(type);
1.32 markus 385: return NULL;
386: }
1.53 markus 387: key = key_new_private(ktype);
388: xfree(type);
389:
390: switch (key->type) {
391: case KEY_DSA:
392: buffer_get_bignum_bits(&b, key->dsa->p);
393: buffer_get_bignum_bits(&b, key->dsa->g);
394: buffer_get_bignum_bits(&b, key->dsa->q);
395: buffer_get_bignum_bits(&b, key->dsa->pub_key);
396: buffer_get_bignum_bits(&b, key->dsa->priv_key);
397: break;
398: case KEY_RSA:
1.160 stevesk 399: e = buffer_get_char(&b);
1.62 markus 400: debug("e %lx", e);
401: if (e < 30) {
402: e <<= 8;
403: e += buffer_get_char(&b);
404: debug("e %lx", e);
405: e <<= 8;
406: e += buffer_get_char(&b);
407: debug("e %lx", e);
408: }
409: if (!BN_set_word(key->rsa->e, e)) {
1.53 markus 410: buffer_free(&b);
411: key_free(key);
412: return NULL;
413: }
414: buffer_get_bignum_bits(&b, key->rsa->d);
415: buffer_get_bignum_bits(&b, key->rsa->n);
416: buffer_get_bignum_bits(&b, key->rsa->iqmp);
417: buffer_get_bignum_bits(&b, key->rsa->q);
418: buffer_get_bignum_bits(&b, key->rsa->p);
1.68 markus 419: rsa_generate_additional_parameters(key->rsa);
1.53 markus 420: break;
421: }
1.32 markus 422: rlen = buffer_len(&b);
1.85 deraadt 423: if (rlen != 0)
1.53 markus 424: error("do_convert_private_ssh2_from_blob: "
425: "remaining bytes in key blob %d", rlen);
1.32 markus 426: buffer_free(&b);
1.64 markus 427:
428: /* try the key */
429: key_sign(key, &sig, &slen, data, sizeof(data));
430: key_verify(key, sig, slen, data, sizeof(data));
431: xfree(sig);
1.32 markus 432: return key;
433: }
434:
1.137 dtucker 435: static int
436: get_line(FILE *fp, char *line, size_t len)
437: {
438: int c;
439: size_t pos = 0;
440:
441: line[0] = '\0';
442: while ((c = fgetc(fp)) != EOF) {
443: if (pos >= len - 1) {
444: fprintf(stderr, "input line too long.\n");
445: exit(1);
446: }
1.140 deraadt 447: switch (c) {
1.137 dtucker 448: case '\r':
449: c = fgetc(fp);
450: if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
451: fprintf(stderr, "unget: %s\n", strerror(errno));
452: exit(1);
453: }
454: return pos;
455: case '\n':
456: return pos;
457: }
458: line[pos++] = c;
459: line[pos] = '\0';
460: }
1.157 stevesk 461: /* We reached EOF */
462: return -1;
1.137 dtucker 463: }
464:
1.63 itojun 465: static void
1.193 djm 466: do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
1.19 markus 467: {
468: int blen;
1.98 markus 469: u_int len;
1.137 dtucker 470: char line[1024];
1.80 stevesk 471: u_char blob[8096];
1.19 markus 472: char encoded[8096];
1.193 djm 473: int escaped = 0;
1.19 markus 474: FILE *fp;
475:
1.191 djm 476: if ((fp = fopen(identity_file, "r")) == NULL)
477: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.19 markus 478: encoded[0] = '\0';
1.137 dtucker 479: while ((blen = get_line(fp, line, sizeof(line))) != -1) {
480: if (line[blen - 1] == '\\')
1.25 markus 481: escaped++;
1.19 markus 482: if (strncmp(line, "----", 4) == 0 ||
483: strstr(line, ": ") != NULL) {
1.32 markus 484: if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
1.193 djm 485: *private = 1;
1.64 markus 486: if (strstr(line, " END ") != NULL) {
487: break;
488: }
1.60 markus 489: /* fprintf(stderr, "ignore: %s", line); */
1.19 markus 490: continue;
491: }
1.25 markus 492: if (escaped) {
493: escaped--;
1.60 markus 494: /* fprintf(stderr, "escaped: %s", line); */
1.25 markus 495: continue;
1.19 markus 496: }
497: strlcat(encoded, line, sizeof(encoded));
498: }
1.98 markus 499: len = strlen(encoded);
500: if (((len % 4) == 3) &&
501: (encoded[len-1] == '=') &&
502: (encoded[len-2] == '=') &&
503: (encoded[len-3] == '='))
504: encoded[len-3] = '\0';
1.91 stevesk 505: blen = uudecode(encoded, blob, sizeof(blob));
1.19 markus 506: if (blen < 0) {
507: fprintf(stderr, "uudecode failed.\n");
508: exit(1);
509: }
1.193 djm 510: *k = *private ?
1.32 markus 511: do_convert_private_ssh2_from_blob(blob, blen) :
1.33 markus 512: key_from_blob(blob, blen);
1.193 djm 513: if (*k == NULL) {
1.32 markus 514: fprintf(stderr, "decode blob failed.\n");
515: exit(1);
516: }
1.193 djm 517: fclose(fp);
518: }
519:
520: static void
521: do_convert_from_pkcs8(Key **k, int *private)
522: {
523: EVP_PKEY *pubkey;
524: FILE *fp;
525:
526: if ((fp = fopen(identity_file, "r")) == NULL)
527: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
528: if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
529: fatal("%s: %s is not a recognised public key format", __func__,
530: identity_file);
531: }
532: fclose(fp);
533: switch (EVP_PKEY_type(pubkey->type)) {
534: case EVP_PKEY_RSA:
535: *k = key_new(KEY_UNSPEC);
536: (*k)->type = KEY_RSA;
537: (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
538: break;
539: case EVP_PKEY_DSA:
540: *k = key_new(KEY_UNSPEC);
541: (*k)->type = KEY_DSA;
542: (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
543: break;
1.200 djm 544: case EVP_PKEY_EC:
545: *k = key_new(KEY_UNSPEC);
546: (*k)->type = KEY_ECDSA;
547: (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
1.204 ! djm 548: (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
1.200 djm 549: break;
1.193 djm 550: default:
551: fatal("%s: unsupported pubkey type %d", __func__,
552: EVP_PKEY_type(pubkey->type));
553: }
554: EVP_PKEY_free(pubkey);
555: return;
556: }
557:
558: static void
559: do_convert_from_pem(Key **k, int *private)
560: {
561: FILE *fp;
562: RSA *rsa;
563: #ifdef notyet
564: DSA *dsa;
565: #endif
566:
567: if ((fp = fopen(identity_file, "r")) == NULL)
568: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
569: if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
570: *k = key_new(KEY_UNSPEC);
571: (*k)->type = KEY_RSA;
572: (*k)->rsa = rsa;
573: fclose(fp);
574: return;
575: }
576: #if notyet /* OpenSSH 0.9.8 lacks this function */
577: rewind(fp);
578: if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
579: *k = key_new(KEY_UNSPEC);
580: (*k)->type = KEY_DSA;
581: (*k)->dsa = dsa;
582: fclose(fp);
583: return;
584: }
1.200 djm 585: /* XXX ECDSA */
1.193 djm 586: #endif
587: fatal("%s: unrecognised raw private key format", __func__);
588: }
589:
590: static void
591: do_convert_from(struct passwd *pw)
592: {
593: Key *k = NULL;
1.195 djm 594: int private = 0, ok = 0;
1.193 djm 595: struct stat st;
596:
597: if (!have_identity)
598: ask_filename(pw, "Enter file in which the key is");
599: if (stat(identity_file, &st) < 0)
600: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
601:
602: switch (convert_format) {
603: case FMT_RFC4716:
604: do_convert_from_ssh2(pw, &k, &private);
605: break;
606: case FMT_PKCS8:
607: do_convert_from_pkcs8(&k, &private);
608: break;
609: case FMT_PEM:
610: do_convert_from_pem(&k, &private);
611: break;
612: default:
613: fatal("%s: unknown key format %d", __func__, convert_format);
614: }
615:
616: if (!private)
617: ok = key_write(k, stdout);
618: if (ok)
619: fprintf(stdout, "\n");
620: else {
621: switch (k->type) {
622: case KEY_DSA:
623: ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
624: NULL, 0, NULL, NULL);
625: break;
1.200 djm 626: case KEY_ECDSA:
627: ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
628: NULL, 0, NULL, NULL);
629: break;
1.193 djm 630: case KEY_RSA:
631: ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
632: NULL, 0, NULL, NULL);
633: break;
634: default:
635: fatal("%s: unsupported key type %s", __func__,
636: key_type(k));
637: }
638: }
639:
1.32 markus 640: if (!ok) {
1.173 tobias 641: fprintf(stderr, "key write failed\n");
1.32 markus 642: exit(1);
643: }
1.19 markus 644: key_free(k);
645: exit(0);
646: }
647:
1.63 itojun 648: static void
1.19 markus 649: do_print_public(struct passwd *pw)
650: {
1.52 markus 651: Key *prv;
1.19 markus 652: struct stat st;
653:
654: if (!have_identity)
655: ask_filename(pw, "Enter file in which the key is");
656: if (stat(identity_file, &st) < 0) {
657: perror(identity_file);
658: exit(1);
659: }
1.61 markus 660: prv = load_identity(identity_file);
1.52 markus 661: if (prv == NULL) {
1.19 markus 662: fprintf(stderr, "load failed\n");
663: exit(1);
664: }
1.52 markus 665: if (!key_write(prv, stdout))
1.19 markus 666: fprintf(stderr, "key_write failed");
1.52 markus 667: key_free(prv);
1.19 markus 668: fprintf(stdout, "\n");
669: exit(0);
670: }
671:
1.66 markus 672: static void
1.197 djm 673: do_download(struct passwd *pw)
1.75 markus 674: {
1.177 markus 675: #ifdef ENABLE_PKCS11
1.97 markus 676: Key **keys = NULL;
1.177 markus 677: int i, nkeys;
1.75 markus 678:
1.177 markus 679: pkcs11_init(0);
680: nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
681: if (nkeys <= 0)
682: fatal("cannot read public key from pkcs11");
683: for (i = 0; i < nkeys; i++) {
1.97 markus 684: key_write(keys[i], stdout);
685: key_free(keys[i]);
686: fprintf(stdout, "\n");
687: }
688: xfree(keys);
1.177 markus 689: pkcs11_terminate();
1.75 markus 690: exit(0);
1.177 markus 691: #else
692: fatal("no pkcs11 support");
693: #endif /* ENABLE_PKCS11 */
1.75 markus 694: }
1.66 markus 695:
1.63 itojun 696: static void
1.8 markus 697: do_fingerprint(struct passwd *pw)
698: {
1.15 markus 699: FILE *f;
1.19 markus 700: Key *public;
1.167 grunk 701: char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
1.165 djm 702: int i, skip = 0, num = 0, invalid = 1;
1.84 stevesk 703: enum fp_rep rep;
704: enum fp_type fptype;
1.12 markus 705: struct stat st;
706:
1.52 markus 707: fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
708: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
1.49 markus 709:
1.12 markus 710: if (!have_identity)
711: ask_filename(pw, "Enter file in which the key is");
712: if (stat(identity_file, &st) < 0) {
713: perror(identity_file);
714: exit(1);
715: }
1.52 markus 716: public = key_load_public(identity_file, &comment);
717: if (public != NULL) {
718: fp = key_fingerprint(public, fptype, rep);
1.175 djm 719: ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
1.170 grunk 720: printf("%u %s %s (%s)\n", key_size(public), fp, comment,
721: key_type(public));
1.169 grunk 722: if (log_level >= SYSLOG_LEVEL_VERBOSE)
723: printf("%s\n", ra);
1.33 markus 724: key_free(public);
725: xfree(comment);
1.167 grunk 726: xfree(ra);
1.49 markus 727: xfree(fp);
1.15 markus 728: exit(0);
729: }
1.144 markus 730: if (comment) {
1.52 markus 731: xfree(comment);
1.144 markus 732: comment = NULL;
733: }
1.15 markus 734:
1.191 djm 735: if ((f = fopen(identity_file, "r")) == NULL)
736: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
737:
738: while (fgets(line, sizeof(line), f)) {
739: if ((cp = strchr(line, '\n')) == NULL) {
740: error("line %d too long: %.40s...",
741: num + 1, line);
742: skip = 1;
743: continue;
744: }
745: num++;
746: if (skip) {
747: skip = 0;
748: continue;
749: }
750: *cp = '\0';
751:
752: /* Skip leading whitespace, empty and comment lines. */
753: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
754: ;
755: if (!*cp || *cp == '\n' || *cp == '#')
756: continue;
757: i = strtol(cp, &ep, 10);
758: if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
759: int quoted = 0;
760: comment = cp;
761: for (; *cp && (quoted || (*cp != ' ' &&
762: *cp != '\t')); cp++) {
763: if (*cp == '\\' && cp[1] == '"')
764: cp++; /* Skip both */
765: else if (*cp == '"')
766: quoted = !quoted;
1.15 markus 767: }
1.191 djm 768: if (!*cp)
1.15 markus 769: continue;
1.191 djm 770: *cp++ = '\0';
771: }
772: ep = cp;
773: public = key_new(KEY_RSA1);
774: if (key_read(public, &cp) != 1) {
775: cp = ep;
776: key_free(public);
777: public = key_new(KEY_UNSPEC);
1.38 markus 778: if (key_read(public, &cp) != 1) {
779: key_free(public);
1.191 djm 780: continue;
1.12 markus 781: }
782: }
1.191 djm 783: comment = *cp ? cp : comment;
784: fp = key_fingerprint(public, fptype, rep);
785: ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
786: printf("%u %s %s (%s)\n", key_size(public), fp,
787: comment ? comment : "no comment", key_type(public));
788: if (log_level >= SYSLOG_LEVEL_VERBOSE)
789: printf("%s\n", ra);
790: xfree(ra);
791: xfree(fp);
792: key_free(public);
793: invalid = 0;
1.15 markus 794: }
1.191 djm 795: fclose(f);
796:
1.15 markus 797: if (invalid) {
1.83 markus 798: printf("%s is not a public key file.\n", identity_file);
1.15 markus 799: exit(1);
1.12 markus 800: }
801: exit(0);
1.8 markus 802: }
803:
1.119 djm 804: static void
1.179 djm 805: printhost(FILE *f, const char *name, Key *public, int ca, int hash)
1.119 djm 806: {
1.166 djm 807: if (print_fingerprint) {
808: enum fp_rep rep;
809: enum fp_type fptype;
1.167 grunk 810: char *fp, *ra;
1.166 djm 811:
812: fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
813: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
814: fp = key_fingerprint(public, fptype, rep);
1.175 djm 815: ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
1.171 sthen 816: printf("%u %s %s (%s)\n", key_size(public), fp, name,
817: key_type(public));
818: if (log_level >= SYSLOG_LEVEL_VERBOSE)
819: printf("%s\n", ra);
1.167 grunk 820: xfree(ra);
1.166 djm 821: xfree(fp);
822: } else {
823: if (hash && (name = host_hash(name, NULL, 0)) == NULL)
824: fatal("hash_host failed");
1.179 djm 825: fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
1.166 djm 826: if (!key_write(public, f))
827: fatal("key_write failed");
828: fprintf(f, "\n");
829: }
1.119 djm 830: }
831:
832: static void
833: do_known_hosts(struct passwd *pw, const char *name)
834: {
835: FILE *in, *out = stdout;
1.179 djm 836: Key *pub;
1.119 djm 837: char *cp, *cp2, *kp, *kp2;
838: char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
1.165 djm 839: int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
1.179 djm 840: int ca;
1.119 djm 841:
842: if (!have_identity) {
843: cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
844: if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
845: sizeof(identity_file))
846: fatal("Specified known hosts path too long");
847: xfree(cp);
848: have_identity = 1;
849: }
850: if ((in = fopen(identity_file, "r")) == NULL)
1.191 djm 851: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.119 djm 852:
853: /*
854: * Find hosts goes to stdout, hash and deletions happen in-place
855: * A corner case is ssh-keygen -HF foo, which should go to stdout
856: */
857: if (!find_host && (hash_hosts || delete_host)) {
858: if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
859: strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
860: strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
861: strlcat(old, ".old", sizeof(old)) >= sizeof(old))
862: fatal("known_hosts path too long");
863: umask(077);
864: if ((c = mkstemp(tmp)) == -1)
865: fatal("mkstemp: %s", strerror(errno));
866: if ((out = fdopen(c, "w")) == NULL) {
867: c = errno;
868: unlink(tmp);
869: fatal("fdopen: %s", strerror(c));
870: }
871: inplace = 1;
872: }
873:
874: while (fgets(line, sizeof(line), in)) {
1.163 chl 875: if ((cp = strchr(line, '\n')) == NULL) {
1.165 djm 876: error("line %d too long: %.40s...", num + 1, line);
1.119 djm 877: skip = 1;
878: invalid = 1;
879: continue;
880: }
1.163 chl 881: num++;
1.119 djm 882: if (skip) {
883: skip = 0;
884: continue;
885: }
1.163 chl 886: *cp = '\0';
1.119 djm 887:
888: /* Skip leading whitespace, empty and comment lines. */
889: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
890: ;
891: if (!*cp || *cp == '\n' || *cp == '#') {
892: if (inplace)
893: fprintf(out, "%s\n", cp);
894: continue;
895: }
1.179 djm 896: /* Check whether this is a CA key */
897: if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
898: (cp[sizeof(CA_MARKER) - 1] == ' ' ||
899: cp[sizeof(CA_MARKER) - 1] == '\t')) {
900: ca = 1;
901: cp += sizeof(CA_MARKER);
902: } else
903: ca = 0;
904:
1.119 djm 905: /* Find the end of the host name portion. */
906: for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
907: ;
1.179 djm 908:
1.119 djm 909: if (*kp == '\0' || *(kp + 1) == '\0') {
910: error("line %d missing key: %.40s...",
911: num, line);
912: invalid = 1;
913: continue;
914: }
915: *kp++ = '\0';
916: kp2 = kp;
917:
1.179 djm 918: pub = key_new(KEY_RSA1);
919: if (key_read(pub, &kp) != 1) {
1.119 djm 920: kp = kp2;
1.179 djm 921: key_free(pub);
922: pub = key_new(KEY_UNSPEC);
923: if (key_read(pub, &kp) != 1) {
1.119 djm 924: error("line %d invalid key: %.40s...",
925: num, line);
1.179 djm 926: key_free(pub);
1.119 djm 927: invalid = 1;
928: continue;
929: }
930: }
931:
932: if (*cp == HASH_DELIM) {
933: if (find_host || delete_host) {
934: cp2 = host_hash(name, cp, strlen(cp));
935: if (cp2 == NULL) {
936: error("line %d: invalid hashed "
937: "name: %.64s...", num, line);
938: invalid = 1;
939: continue;
940: }
941: c = (strcmp(cp2, cp) == 0);
942: if (find_host && c) {
943: printf("# Host %s found: "
1.179 djm 944: "line %d type %s%s\n", name,
945: num, key_type(pub),
946: ca ? " (CA key)" : "");
947: printhost(out, cp, pub, ca, 0);
1.119 djm 948: }
1.179 djm 949: if (delete_host && !c && !ca)
950: printhost(out, cp, pub, ca, 0);
1.119 djm 951: } else if (hash_hosts)
1.179 djm 952: printhost(out, cp, pub, ca, 0);
1.119 djm 953: } else {
954: if (find_host || delete_host) {
955: c = (match_hostname(name, cp,
956: strlen(cp)) == 1);
957: if (find_host && c) {
958: printf("# Host %s found: "
1.179 djm 959: "line %d type %s%s\n", name,
960: num, key_type(pub),
961: ca ? " (CA key)" : "");
962: printhost(out, name, pub,
963: ca, hash_hosts && !ca);
1.119 djm 964: }
1.179 djm 965: if (delete_host && !c && !ca)
966: printhost(out, cp, pub, ca, 0);
1.119 djm 967: } else if (hash_hosts) {
1.121 deraadt 968: for (cp2 = strsep(&cp, ",");
1.119 djm 969: cp2 != NULL && *cp2 != '\0';
1.120 djm 970: cp2 = strsep(&cp, ",")) {
1.179 djm 971: if (ca) {
972: fprintf(stderr, "Warning: "
973: "ignoring CA key for host: "
974: "%.64s\n", cp2);
975: printhost(out, cp2, pub, ca, 0);
976: } else if (strcspn(cp2, "*?!") !=
977: strlen(cp2)) {
1.120 djm 978: fprintf(stderr, "Warning: "
979: "ignoring host name with "
980: "metacharacters: %.64s\n",
981: cp2);
1.179 djm 982: printhost(out, cp2, pub, ca, 0);
983: } else
984: printhost(out, cp2, pub, ca, 1);
1.120 djm 985: }
1.119 djm 986: has_unhashed = 1;
987: }
988: }
1.179 djm 989: key_free(pub);
1.119 djm 990: }
991: fclose(in);
992:
993: if (invalid) {
1.165 djm 994: fprintf(stderr, "%s is not a valid known_hosts file.\n",
1.119 djm 995: identity_file);
996: if (inplace) {
997: fprintf(stderr, "Not replacing existing known_hosts "
1.122 markus 998: "file because of errors\n");
1.119 djm 999: fclose(out);
1000: unlink(tmp);
1001: }
1002: exit(1);
1003: }
1004:
1005: if (inplace) {
1006: fclose(out);
1007:
1008: /* Backup existing file */
1009: if (unlink(old) == -1 && errno != ENOENT)
1010: fatal("unlink %.100s: %s", old, strerror(errno));
1011: if (link(identity_file, old) == -1)
1012: fatal("link %.100s to %.100s: %s", identity_file, old,
1013: strerror(errno));
1014: /* Move new one into place */
1015: if (rename(tmp, identity_file) == -1) {
1016: error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1017: strerror(errno));
1018: unlink(tmp);
1019: unlink(old);
1020: exit(1);
1021: }
1022:
1023: fprintf(stderr, "%s updated.\n", identity_file);
1024: fprintf(stderr, "Original contents retained as %s\n", old);
1025: if (has_unhashed) {
1026: fprintf(stderr, "WARNING: %s contains unhashed "
1027: "entries\n", old);
1028: fprintf(stderr, "Delete this file to ensure privacy "
1.128 djm 1029: "of hostnames\n");
1.119 djm 1030: }
1031: }
1032:
1033: exit(0);
1034: }
1035:
1.13 deraadt 1036: /*
1037: * Perform changing a passphrase. The argument is the passwd structure
1038: * for the current user.
1039: */
1.63 itojun 1040: static void
1.7 markus 1041: do_change_passphrase(struct passwd *pw)
1042: {
1.12 markus 1043: char *comment;
1044: char *old_passphrase, *passphrase1, *passphrase2;
1045: struct stat st;
1.19 markus 1046: Key *private;
1.12 markus 1047:
1048: if (!have_identity)
1049: ask_filename(pw, "Enter file in which the key is");
1050: if (stat(identity_file, &st) < 0) {
1051: perror(identity_file);
1052: exit(1);
1053: }
1054: /* Try to load the file with empty passphrase. */
1.52 markus 1055: private = key_load_private(identity_file, "", &comment);
1056: if (private == NULL) {
1.12 markus 1057: if (identity_passphrase)
1058: old_passphrase = xstrdup(identity_passphrase);
1059: else
1.65 markus 1060: old_passphrase =
1061: read_passphrase("Enter old passphrase: ",
1062: RP_ALLOW_STDIN);
1063: private = key_load_private(identity_file, old_passphrase,
1064: &comment);
1.52 markus 1065: memset(old_passphrase, 0, strlen(old_passphrase));
1066: xfree(old_passphrase);
1067: if (private == NULL) {
1.12 markus 1068: printf("Bad passphrase.\n");
1069: exit(1);
1070: }
1071: }
1072: printf("Key has comment '%s'\n", comment);
1073:
1074: /* Ask the new passphrase (twice). */
1075: if (identity_new_passphrase) {
1076: passphrase1 = xstrdup(identity_new_passphrase);
1077: passphrase2 = NULL;
1078: } else {
1079: passphrase1 =
1.65 markus 1080: read_passphrase("Enter new passphrase (empty for no "
1081: "passphrase): ", RP_ALLOW_STDIN);
1082: passphrase2 = read_passphrase("Enter same passphrase again: ",
1.86 deraadt 1083: RP_ALLOW_STDIN);
1.12 markus 1084:
1085: /* Verify that they are the same. */
1086: if (strcmp(passphrase1, passphrase2) != 0) {
1087: memset(passphrase1, 0, strlen(passphrase1));
1088: memset(passphrase2, 0, strlen(passphrase2));
1089: xfree(passphrase1);
1090: xfree(passphrase2);
1091: printf("Pass phrases do not match. Try again.\n");
1092: exit(1);
1093: }
1094: /* Destroy the other copy. */
1095: memset(passphrase2, 0, strlen(passphrase2));
1096: xfree(passphrase2);
1097: }
1098:
1099: /* Save the file using the new passphrase. */
1.52 markus 1100: if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56 markus 1101: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 1102: memset(passphrase1, 0, strlen(passphrase1));
1103: xfree(passphrase1);
1.19 markus 1104: key_free(private);
1.12 markus 1105: xfree(comment);
1106: exit(1);
1107: }
1108: /* Destroy the passphrase and the copy of the key in memory. */
1109: memset(passphrase1, 0, strlen(passphrase1));
1110: xfree(passphrase1);
1.19 markus 1111: key_free(private); /* Destroys contents */
1.12 markus 1112: xfree(comment);
1.1 deraadt 1113:
1.12 markus 1114: printf("Your identification has been saved with the new passphrase.\n");
1115: exit(0);
1.1 deraadt 1116: }
1117:
1.105 jakob 1118: /*
1119: * Print the SSHFP RR.
1120: */
1.138 jakob 1121: static int
1122: do_print_resource_record(struct passwd *pw, char *fname, char *hname)
1.105 jakob 1123: {
1124: Key *public;
1125: char *comment = NULL;
1126: struct stat st;
1127:
1.138 jakob 1128: if (fname == NULL)
1.105 jakob 1129: ask_filename(pw, "Enter file in which the key is");
1.138 jakob 1130: if (stat(fname, &st) < 0) {
1131: if (errno == ENOENT)
1132: return 0;
1133: perror(fname);
1.105 jakob 1134: exit(1);
1135: }
1.138 jakob 1136: public = key_load_public(fname, &comment);
1.105 jakob 1137: if (public != NULL) {
1.116 avsm 1138: export_dns_rr(hname, public, stdout, print_generic);
1.105 jakob 1139: key_free(public);
1140: xfree(comment);
1.138 jakob 1141: return 1;
1.105 jakob 1142: }
1143: if (comment)
1144: xfree(comment);
1145:
1.138 jakob 1146: printf("failed to read v2 public key from %s.\n", fname);
1.105 jakob 1147: exit(1);
1148: }
1149:
1.13 deraadt 1150: /*
1151: * Change the comment of a private key file.
1152: */
1.63 itojun 1153: static void
1.2 provos 1154: do_change_comment(struct passwd *pw)
1.1 deraadt 1155: {
1.46 deraadt 1156: char new_comment[1024], *comment, *passphrase;
1.52 markus 1157: Key *private;
1158: Key *public;
1.12 markus 1159: struct stat st;
1160: FILE *f;
1.46 deraadt 1161: int fd;
1.12 markus 1162:
1163: if (!have_identity)
1164: ask_filename(pw, "Enter file in which the key is");
1165: if (stat(identity_file, &st) < 0) {
1166: perror(identity_file);
1167: exit(1);
1168: }
1.52 markus 1169: private = key_load_private(identity_file, "", &comment);
1170: if (private == NULL) {
1.12 markus 1171: if (identity_passphrase)
1172: passphrase = xstrdup(identity_passphrase);
1173: else if (identity_new_passphrase)
1174: passphrase = xstrdup(identity_new_passphrase);
1175: else
1.65 markus 1176: passphrase = read_passphrase("Enter passphrase: ",
1177: RP_ALLOW_STDIN);
1.12 markus 1178: /* Try to load using the passphrase. */
1.52 markus 1179: private = key_load_private(identity_file, passphrase, &comment);
1180: if (private == NULL) {
1.12 markus 1181: memset(passphrase, 0, strlen(passphrase));
1182: xfree(passphrase);
1183: printf("Bad passphrase.\n");
1184: exit(1);
1185: }
1.52 markus 1186: } else {
1187: passphrase = xstrdup("");
1.12 markus 1188: }
1.52 markus 1189: if (private->type != KEY_RSA1) {
1190: fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1191: key_free(private);
1192: exit(1);
1.86 deraadt 1193: }
1.12 markus 1194: printf("Key now has comment '%s'\n", comment);
1195:
1196: if (identity_comment) {
1197: strlcpy(new_comment, identity_comment, sizeof(new_comment));
1198: } else {
1199: printf("Enter new comment: ");
1200: fflush(stdout);
1201: if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1202: memset(passphrase, 0, strlen(passphrase));
1.19 markus 1203: key_free(private);
1.12 markus 1204: exit(1);
1205: }
1.162 gilles 1206: new_comment[strcspn(new_comment, "\n")] = '\0';
1.12 markus 1207: }
1208:
1209: /* Save the file using the new passphrase. */
1.52 markus 1210: if (!key_save_private(private, identity_file, passphrase, new_comment)) {
1.56 markus 1211: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 1212: memset(passphrase, 0, strlen(passphrase));
1213: xfree(passphrase);
1.19 markus 1214: key_free(private);
1.12 markus 1215: xfree(comment);
1216: exit(1);
1217: }
1218: memset(passphrase, 0, strlen(passphrase));
1219: xfree(passphrase);
1.52 markus 1220: public = key_from_private(private);
1.19 markus 1221: key_free(private);
1.12 markus 1222:
1223: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 1224: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1225: if (fd == -1) {
1.12 markus 1226: printf("Could not save your public key in %s\n", identity_file);
1227: exit(1);
1228: }
1.46 deraadt 1229: f = fdopen(fd, "w");
1230: if (f == NULL) {
1.173 tobias 1231: printf("fdopen %s failed\n", identity_file);
1.46 deraadt 1232: exit(1);
1233: }
1.19 markus 1234: if (!key_write(public, f))
1.173 tobias 1235: fprintf(stderr, "write key failed\n");
1.19 markus 1236: key_free(public);
1237: fprintf(f, " %s\n", new_comment);
1.12 markus 1238: fclose(f);
1.1 deraadt 1239:
1.12 markus 1240: xfree(comment);
1.1 deraadt 1241:
1.12 markus 1242: printf("The comment in your key file has been changed.\n");
1243: exit(0);
1.1 deraadt 1244: }
1245:
1.179 djm 1246: static const char *
1.182 djm 1247: fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
1.179 djm 1248: {
1249: char from[32], to[32];
1250: static char ret[64];
1251: time_t tt;
1252: struct tm *tm;
1253:
1254: *from = *to = '\0';
1.182 djm 1255: if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
1.179 djm 1256: return "forever";
1257:
1.182 djm 1258: if (valid_from != 0) {
1.179 djm 1259: /* XXX revisit INT_MAX in 2038 :) */
1.182 djm 1260: tt = valid_from > INT_MAX ? INT_MAX : valid_from;
1.179 djm 1261: tm = localtime(&tt);
1262: strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1263: }
1.182 djm 1264: if (valid_to != 0xffffffffffffffffULL) {
1.179 djm 1265: /* XXX revisit INT_MAX in 2038 :) */
1.182 djm 1266: tt = valid_to > INT_MAX ? INT_MAX : valid_to;
1.179 djm 1267: tm = localtime(&tt);
1268: strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1269: }
1270:
1.182 djm 1271: if (valid_from == 0) {
1.179 djm 1272: snprintf(ret, sizeof(ret), "before %s", to);
1273: return ret;
1274: }
1.182 djm 1275: if (valid_to == 0xffffffffffffffffULL) {
1.179 djm 1276: snprintf(ret, sizeof(ret), "after %s", from);
1277: return ret;
1278: }
1279:
1280: snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1281: return ret;
1282: }
1283:
1284: static void
1.186 djm 1285: add_flag_option(Buffer *c, const char *name)
1.179 djm 1286: {
1287: debug3("%s: %s", __func__, name);
1288: buffer_put_cstring(c, name);
1289: buffer_put_string(c, NULL, 0);
1290: }
1291:
1292: static void
1.186 djm 1293: add_string_option(Buffer *c, const char *name, const char *value)
1.179 djm 1294: {
1295: Buffer b;
1296:
1297: debug3("%s: %s=%s", __func__, name, value);
1298: buffer_init(&b);
1299: buffer_put_cstring(&b, value);
1300:
1301: buffer_put_cstring(c, name);
1302: buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1303:
1304: buffer_free(&b);
1305: }
1306:
1.190 djm 1307: #define OPTIONS_CRITICAL 1
1308: #define OPTIONS_EXTENSIONS 2
1.179 djm 1309: static void
1.190 djm 1310: prepare_options_buf(Buffer *c, int which)
1.179 djm 1311: {
1312: buffer_clear(c);
1.196 djm 1313: if ((which & OPTIONS_CRITICAL) != 0 &&
1314: certflags_command != NULL)
1315: add_string_option(c, "force-command", certflags_command);
1.190 djm 1316: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1317: (certflags_flags & CERTOPT_AGENT_FWD) != 0)
1.186 djm 1318: add_flag_option(c, "permit-agent-forwarding");
1.190 djm 1319: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1320: (certflags_flags & CERTOPT_PORT_FWD) != 0)
1.186 djm 1321: add_flag_option(c, "permit-port-forwarding");
1.190 djm 1322: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1323: (certflags_flags & CERTOPT_PTY) != 0)
1.186 djm 1324: add_flag_option(c, "permit-pty");
1.190 djm 1325: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1326: (certflags_flags & CERTOPT_USER_RC) != 0)
1.186 djm 1327: add_flag_option(c, "permit-user-rc");
1.196 djm 1328: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1329: (certflags_flags & CERTOPT_X_FWD) != 0)
1330: add_flag_option(c, "permit-X11-forwarding");
1.190 djm 1331: if ((which & OPTIONS_CRITICAL) != 0 &&
1332: certflags_src_addr != NULL)
1333: add_string_option(c, "source-address", certflags_src_addr);
1.179 djm 1334: }
1335:
1.197 djm 1336: static Key *
1337: load_pkcs11_key(char *path)
1338: {
1339: #ifdef ENABLE_PKCS11
1340: Key **keys = NULL, *public, *private = NULL;
1341: int i, nkeys;
1342:
1343: if ((public = key_load_public(path, NULL)) == NULL)
1344: fatal("Couldn't load CA public key \"%s\"", path);
1345:
1346: nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1347: debug3("%s: %d keys", __func__, nkeys);
1348: if (nkeys <= 0)
1349: fatal("cannot read public key from pkcs11");
1350: for (i = 0; i < nkeys; i++) {
1351: if (key_equal_public(public, keys[i])) {
1352: private = keys[i];
1353: continue;
1354: }
1355: key_free(keys[i]);
1356: }
1357: xfree(keys);
1358: key_free(public);
1359: return private;
1360: #else
1361: fatal("no pkcs11 support");
1362: #endif /* ENABLE_PKCS11 */
1363: }
1364:
1.179 djm 1365: static void
1366: do_ca_sign(struct passwd *pw, int argc, char **argv)
1367: {
1368: int i, fd;
1369: u_int n;
1370: Key *ca, *public;
1371: char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1372: FILE *f;
1.186 djm 1373: int v00 = 0; /* legacy keys */
1.179 djm 1374:
1.186 djm 1375: if (key_type_name != NULL) {
1376: switch (key_type_from_name(key_type_name)) {
1377: case KEY_RSA_CERT_V00:
1378: case KEY_DSA_CERT_V00:
1379: v00 = 1;
1380: break;
1381: case KEY_UNSPEC:
1382: if (strcasecmp(key_type_name, "v00") == 0) {
1383: v00 = 1;
1384: break;
1385: } else if (strcasecmp(key_type_name, "v01") == 0)
1386: break;
1387: /* FALLTHROUGH */
1388: default:
1389: fprintf(stderr, "unknown key type %s\n", key_type_name);
1390: exit(1);
1391: }
1392: }
1393:
1.197 djm 1394: pkcs11_init(1);
1395: tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1396: if (pkcs11provider != NULL) {
1397: if ((ca = load_pkcs11_key(tmp)) == NULL)
1398: fatal("No PKCS#11 key matching %s found", ca_key_path);
1399: } else if ((ca = load_identity(tmp)) == NULL)
1400: fatal("Couldn't load CA key \"%s\"", tmp);
1401: xfree(tmp);
1402:
1.179 djm 1403: for (i = 0; i < argc; i++) {
1404: /* Split list of principals */
1405: n = 0;
1406: if (cert_principals != NULL) {
1407: otmp = tmp = xstrdup(cert_principals);
1408: plist = NULL;
1409: for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1410: plist = xrealloc(plist, n + 1, sizeof(*plist));
1411: if (*(plist[n] = xstrdup(cp)) == '\0')
1412: fatal("Empty principal name");
1413: }
1414: xfree(otmp);
1415: }
1416:
1417: tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1418: if ((public = key_load_public(tmp, &comment)) == NULL)
1419: fatal("%s: unable to open \"%s\"", __func__, tmp);
1.200 djm 1420: if (public->type != KEY_RSA && public->type != KEY_DSA &&
1421: public->type != KEY_ECDSA)
1.179 djm 1422: fatal("%s: key \"%s\" type %s cannot be certified",
1423: __func__, tmp, key_type(public));
1424:
1425: /* Prepare certificate to sign */
1.186 djm 1426: if (key_to_certified(public, v00) != 0)
1.179 djm 1427: fatal("Could not upgrade key %s to certificate", tmp);
1428: public->cert->type = cert_key_type;
1.186 djm 1429: public->cert->serial = (u_int64_t)cert_serial;
1.179 djm 1430: public->cert->key_id = xstrdup(cert_key_id);
1431: public->cert->nprincipals = n;
1432: public->cert->principals = plist;
1433: public->cert->valid_after = cert_valid_from;
1434: public->cert->valid_before = cert_valid_to;
1.190 djm 1435: if (v00) {
1436: prepare_options_buf(&public->cert->critical,
1437: OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1438: } else {
1439: prepare_options_buf(&public->cert->critical,
1440: OPTIONS_CRITICAL);
1441: prepare_options_buf(&public->cert->extensions,
1442: OPTIONS_EXTENSIONS);
1443: }
1.179 djm 1444: public->cert->signature_key = key_from_private(ca);
1445:
1446: if (key_certify(public, ca) != 0)
1447: fatal("Couldn't not certify key %s", tmp);
1448:
1449: if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1450: *cp = '\0';
1451: xasprintf(&out, "%s-cert.pub", tmp);
1452: xfree(tmp);
1453:
1454: if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1455: fatal("Could not open \"%s\" for writing: %s", out,
1456: strerror(errno));
1457: if ((f = fdopen(fd, "w")) == NULL)
1458: fatal("%s: fdopen: %s", __func__, strerror(errno));
1459: if (!key_write(public, f))
1460: fatal("Could not write certified key to %s", out);
1461: fprintf(f, " %s\n", comment);
1462: fclose(f);
1463:
1.186 djm 1464: if (!quiet) {
1465: logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1466: "valid %s", key_cert_type(public),
1467: out, public->cert->key_id, public->cert->serial,
1.179 djm 1468: cert_principals != NULL ? " for " : "",
1469: cert_principals != NULL ? cert_principals : "",
1.182 djm 1470: fmt_validity(cert_valid_from, cert_valid_to));
1.186 djm 1471: }
1.179 djm 1472:
1473: key_free(public);
1474: xfree(out);
1475: }
1.197 djm 1476: pkcs11_terminate();
1.179 djm 1477: exit(0);
1478: }
1479:
1480: static u_int64_t
1481: parse_relative_time(const char *s, time_t now)
1482: {
1483: int64_t mul, secs;
1484:
1485: mul = *s == '-' ? -1 : 1;
1486:
1487: if ((secs = convtime(s + 1)) == -1)
1488: fatal("Invalid relative certificate time %s", s);
1489: if (mul == -1 && secs > now)
1490: fatal("Certificate time %s cannot be represented", s);
1491: return now + (u_int64_t)(secs * mul);
1492: }
1493:
1494: static u_int64_t
1495: parse_absolute_time(const char *s)
1496: {
1497: struct tm tm;
1498: time_t tt;
1.180 djm 1499: char buf[32], *fmt;
1.179 djm 1500:
1.180 djm 1501: /*
1502: * POSIX strptime says "The application shall ensure that there
1503: * is white-space or other non-alphanumeric characters between
1504: * any two conversion specifications" so arrange things this way.
1505: */
1506: switch (strlen(s)) {
1507: case 8:
1.184 djm 1508: fmt = "%Y-%m-%d";
1509: snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
1.180 djm 1510: break;
1511: case 14:
1.184 djm 1512: fmt = "%Y-%m-%dT%H:%M:%S";
1513: snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
1.180 djm 1514: s, s + 4, s + 6, s + 8, s + 10, s + 12);
1515: break;
1516: default:
1.179 djm 1517: fatal("Invalid certificate time format %s", s);
1.180 djm 1518: }
1.179 djm 1519:
1520: bzero(&tm, sizeof(tm));
1.180 djm 1521: if (strptime(buf, fmt, &tm) == NULL)
1.179 djm 1522: fatal("Invalid certificate time %s", s);
1523: if ((tt = mktime(&tm)) < 0)
1524: fatal("Certificate time %s cannot be represented", s);
1525: return (u_int64_t)tt;
1526: }
1527:
1528: static void
1529: parse_cert_times(char *timespec)
1530: {
1531: char *from, *to;
1532: time_t now = time(NULL);
1533: int64_t secs;
1534:
1535: /* +timespec relative to now */
1536: if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1537: if ((secs = convtime(timespec + 1)) == -1)
1538: fatal("Invalid relative certificate life %s", timespec);
1539: cert_valid_to = now + secs;
1540: /*
1541: * Backdate certificate one minute to avoid problems on hosts
1542: * with poorly-synchronised clocks.
1543: */
1544: cert_valid_from = ((now - 59)/ 60) * 60;
1545: return;
1546: }
1547:
1548: /*
1549: * from:to, where
1550: * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1551: * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1552: */
1553: from = xstrdup(timespec);
1554: to = strchr(from, ':');
1555: if (to == NULL || from == to || *(to + 1) == '\0')
1.181 djm 1556: fatal("Invalid certificate life specification %s", timespec);
1.179 djm 1557: *to++ = '\0';
1558:
1559: if (*from == '-' || *from == '+')
1560: cert_valid_from = parse_relative_time(from, now);
1561: else
1562: cert_valid_from = parse_absolute_time(from);
1563:
1564: if (*to == '-' || *to == '+')
1565: cert_valid_to = parse_relative_time(to, cert_valid_from);
1566: else
1567: cert_valid_to = parse_absolute_time(to);
1568:
1569: if (cert_valid_to <= cert_valid_from)
1570: fatal("Empty certificate validity interval");
1571: xfree(from);
1572: }
1573:
1574: static void
1.186 djm 1575: add_cert_option(char *opt)
1.179 djm 1576: {
1577: char *val;
1578:
1579: if (strcmp(opt, "clear") == 0)
1.190 djm 1580: certflags_flags = 0;
1.179 djm 1581: else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1.190 djm 1582: certflags_flags &= ~CERTOPT_X_FWD;
1.179 djm 1583: else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1.190 djm 1584: certflags_flags |= CERTOPT_X_FWD;
1.179 djm 1585: else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1.190 djm 1586: certflags_flags &= ~CERTOPT_AGENT_FWD;
1.179 djm 1587: else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1.190 djm 1588: certflags_flags |= CERTOPT_AGENT_FWD;
1.179 djm 1589: else if (strcasecmp(opt, "no-port-forwarding") == 0)
1.190 djm 1590: certflags_flags &= ~CERTOPT_PORT_FWD;
1.179 djm 1591: else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1.190 djm 1592: certflags_flags |= CERTOPT_PORT_FWD;
1.179 djm 1593: else if (strcasecmp(opt, "no-pty") == 0)
1.190 djm 1594: certflags_flags &= ~CERTOPT_PTY;
1.179 djm 1595: else if (strcasecmp(opt, "permit-pty") == 0)
1.190 djm 1596: certflags_flags |= CERTOPT_PTY;
1.179 djm 1597: else if (strcasecmp(opt, "no-user-rc") == 0)
1.190 djm 1598: certflags_flags &= ~CERTOPT_USER_RC;
1.179 djm 1599: else if (strcasecmp(opt, "permit-user-rc") == 0)
1.190 djm 1600: certflags_flags |= CERTOPT_USER_RC;
1.179 djm 1601: else if (strncasecmp(opt, "force-command=", 14) == 0) {
1602: val = opt + 14;
1603: if (*val == '\0')
1.186 djm 1604: fatal("Empty force-command option");
1.190 djm 1605: if (certflags_command != NULL)
1.179 djm 1606: fatal("force-command already specified");
1.190 djm 1607: certflags_command = xstrdup(val);
1.179 djm 1608: } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1609: val = opt + 15;
1610: if (*val == '\0')
1.186 djm 1611: fatal("Empty source-address option");
1.190 djm 1612: if (certflags_src_addr != NULL)
1.179 djm 1613: fatal("source-address already specified");
1614: if (addr_match_cidr_list(NULL, val) != 0)
1615: fatal("Invalid source-address list");
1.190 djm 1616: certflags_src_addr = xstrdup(val);
1.179 djm 1617: } else
1.186 djm 1618: fatal("Unsupported certificate option \"%s\"", opt);
1.179 djm 1619: }
1620:
1.63 itojun 1621: static void
1.192 djm 1622: show_options(const Buffer *optbuf, int v00, int in_critical)
1623: {
1624: u_char *name, *data;
1625: u_int dlen;
1626: Buffer options, option;
1627:
1628: buffer_init(&options);
1629: buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1630:
1631: buffer_init(&option);
1632: while (buffer_len(&options) != 0) {
1633: name = buffer_get_string(&options, NULL);
1634: data = buffer_get_string_ptr(&options, &dlen);
1635: buffer_append(&option, data, dlen);
1636: printf(" %s", name);
1637: if ((v00 || !in_critical) &&
1638: (strcmp(name, "permit-X11-forwarding") == 0 ||
1639: strcmp(name, "permit-agent-forwarding") == 0 ||
1640: strcmp(name, "permit-port-forwarding") == 0 ||
1641: strcmp(name, "permit-pty") == 0 ||
1642: strcmp(name, "permit-user-rc") == 0))
1643: printf("\n");
1644: else if ((v00 || in_critical) &&
1645: (strcmp(name, "force-command") == 0 ||
1646: strcmp(name, "source-address") == 0)) {
1647: data = buffer_get_string(&option, NULL);
1648: printf(" %s\n", data);
1649: xfree(data);
1650: } else {
1651: printf(" UNKNOWN OPTION (len %u)\n",
1652: buffer_len(&option));
1653: buffer_clear(&option);
1654: }
1655: xfree(name);
1656: if (buffer_len(&option) != 0)
1657: fatal("Option corrupt: extra data at end");
1658: }
1659: buffer_free(&option);
1660: buffer_free(&options);
1661: }
1662:
1663: static void
1.182 djm 1664: do_show_cert(struct passwd *pw)
1665: {
1666: Key *key;
1667: struct stat st;
1668: char *key_fp, *ca_fp;
1.192 djm 1669: u_int i, v00;
1.182 djm 1670:
1671: if (!have_identity)
1672: ask_filename(pw, "Enter file in which the key is");
1.191 djm 1673: if (stat(identity_file, &st) < 0)
1674: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.182 djm 1675: if ((key = key_load_public(identity_file, NULL)) == NULL)
1676: fatal("%s is not a public key", identity_file);
1677: if (!key_is_cert(key))
1678: fatal("%s is not a certificate", identity_file);
1.186 djm 1679: v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1680:
1.182 djm 1681: key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1682: ca_fp = key_fingerprint(key->cert->signature_key,
1683: SSH_FP_MD5, SSH_FP_HEX);
1684:
1685: printf("%s:\n", identity_file);
1.186 djm 1686: printf(" Type: %s %s certificate\n", key_ssh_name(key),
1687: key_cert_type(key));
1688: printf(" Public key: %s %s\n", key_type(key), key_fp);
1689: printf(" Signing CA: %s %s\n",
1.182 djm 1690: key_type(key->cert->signature_key), ca_fp);
1.186 djm 1691: printf(" Key ID: \"%s\"\n", key->cert->key_id);
1692: if (!v00)
1693: printf(" Serial: %llu\n", key->cert->serial);
1.182 djm 1694: printf(" Valid: %s\n",
1695: fmt_validity(key->cert->valid_after, key->cert->valid_before));
1696: printf(" Principals: ");
1697: if (key->cert->nprincipals == 0)
1698: printf("(none)\n");
1699: else {
1700: for (i = 0; i < key->cert->nprincipals; i++)
1701: printf("\n %s",
1702: key->cert->principals[i]);
1703: printf("\n");
1704: }
1.186 djm 1705: printf(" Critical Options: ");
1706: if (buffer_len(&key->cert->critical) == 0)
1.182 djm 1707: printf("(none)\n");
1708: else {
1709: printf("\n");
1.192 djm 1710: show_options(&key->cert->critical, v00, 1);
1.186 djm 1711: }
1712: if (!v00) {
1713: printf(" Extensions: ");
1714: if (buffer_len(&key->cert->extensions) == 0)
1715: printf("(none)\n");
1716: else {
1717: printf("\n");
1.192 djm 1718: show_options(&key->cert->extensions, v00, 0);
1.182 djm 1719: }
1720: }
1721: exit(0);
1722: }
1723:
1724: static void
1.10 markus 1725: usage(void)
1726: {
1.161 sobrado 1727: fprintf(stderr, "usage: %s [options]\n", __progname);
1.77 jakob 1728: fprintf(stderr, "Options:\n");
1.123 otto 1729: fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
1730: fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
1.77 jakob 1731: fprintf(stderr, " -b bits Number of bits in the key to create.\n");
1.123 otto 1732: fprintf(stderr, " -C comment Provide new comment.\n");
1.77 jakob 1733: fprintf(stderr, " -c Change comment in private and public key files.\n");
1.177 markus 1734: #ifdef ENABLE_PKCS11
1735: fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1736: #endif
1.193 djm 1737: fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
1.123 otto 1738: fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1.77 jakob 1739: fprintf(stderr, " -f filename Filename of the key file.\n");
1.123 otto 1740: fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1.105 jakob 1741: fprintf(stderr, " -g Use generic DNS resource record format.\n");
1.123 otto 1742: fprintf(stderr, " -H Hash names in known_hosts file.\n");
1.179 djm 1743: fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1744: fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
1.193 djm 1745: fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
1.182 djm 1746: fprintf(stderr, " -L Print the contents of a certificate.\n");
1.77 jakob 1747: fprintf(stderr, " -l Show fingerprint of key file.\n");
1.194 jmc 1748: fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1.193 djm 1749: fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
1.194 jmc 1750: fprintf(stderr, " -N phrase Provide new passphrase.\n");
1.179 djm 1751: fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
1.187 jmc 1752: fprintf(stderr, " -O option Specify a certificate option.\n");
1.123 otto 1753: fprintf(stderr, " -P phrase Provide old passphrase.\n");
1.77 jakob 1754: fprintf(stderr, " -p Change passphrase of private key file.\n");
1755: fprintf(stderr, " -q Quiet.\n");
1.123 otto 1756: fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1757: fprintf(stderr, " -r hostname Print DNS resource record.\n");
1.194 jmc 1758: fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1.179 djm 1759: fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
1.123 otto 1760: fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1.77 jakob 1761: fprintf(stderr, " -t type Specify type of key to create.\n");
1.179 djm 1762: fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
1.123 otto 1763: fprintf(stderr, " -v Verbose.\n");
1764: fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1765: fprintf(stderr, " -y Read private key file and print public key.\n");
1.187 jmc 1766: fprintf(stderr, " -z serial Specify a serial number.\n");
1.107 djm 1767:
1.12 markus 1768: exit(1);
1.10 markus 1769: }
1770:
1.13 deraadt 1771: /*
1772: * Main program for key management.
1773: */
1.2 provos 1774: int
1.156 deraadt 1775: main(int argc, char **argv)
1.1 deraadt 1776: {
1.87 djm 1777: char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
1.197 djm 1778: char out_file[MAXPATHLEN], *rr_hostname = NULL;
1.46 deraadt 1779: Key *private, *public;
1.12 markus 1780: struct passwd *pw;
1781: struct stat st;
1.177 markus 1782: int opt, type, fd;
1.189 djm 1783: u_int maxbits;
1.126 dtucker 1784: u_int32_t memory = 0, generator_wanted = 0, trials = 100;
1.107 djm 1785: int do_gen_candidates = 0, do_screen_candidates = 0;
1786: BIGNUM *start = NULL;
1.12 markus 1787: FILE *f;
1.125 avsm 1788: const char *errstr;
1.33 markus 1789:
1.12 markus 1790: extern int optind;
1791: extern char *optarg;
1.129 djm 1792:
1793: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1794: sanitise_stdfd();
1.12 markus 1795:
1.201 djm 1796: OpenSSL_add_all_algorithms();
1.156 deraadt 1797: log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1.19 markus 1798:
1.14 markus 1799: /* we need this for the home * directory. */
1.12 markus 1800: pw = getpwuid(getuid());
1801: if (!pw) {
1802: printf("You don't exist, go away!\n");
1803: exit(1);
1804: }
1.19 markus 1805: if (gethostname(hostname, sizeof(hostname)) < 0) {
1806: perror("gethostname");
1807: exit(1);
1808: }
1.14 markus 1809:
1.193 djm 1810: while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
1.186 djm 1811: "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
1.12 markus 1812: switch (opt) {
1813: case 'b':
1.202 markus 1814: bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
1.125 avsm 1815: if (errstr)
1816: fatal("Bits has bad value %s (%s)",
1817: optarg, errstr);
1.12 markus 1818: break;
1.119 djm 1819: case 'F':
1820: find_host = 1;
1821: rr_hostname = optarg;
1822: break;
1823: case 'H':
1824: hash_hosts = 1;
1825: break;
1.179 djm 1826: case 'I':
1827: cert_key_id = optarg;
1828: break;
1.119 djm 1829: case 'R':
1830: delete_host = 1;
1831: rr_hostname = optarg;
1832: break;
1.182 djm 1833: case 'L':
1834: show_cert = 1;
1835: break;
1.12 markus 1836: case 'l':
1837: print_fingerprint = 1;
1838: break;
1.49 markus 1839: case 'B':
1840: print_bubblebabble = 1;
1841: break;
1.193 djm 1842: case 'm':
1843: if (strcasecmp(optarg, "RFC4716") == 0 ||
1844: strcasecmp(optarg, "ssh2") == 0) {
1845: convert_format = FMT_RFC4716;
1846: break;
1847: }
1848: if (strcasecmp(optarg, "PKCS8") == 0) {
1849: convert_format = FMT_PKCS8;
1850: break;
1851: }
1852: if (strcasecmp(optarg, "PEM") == 0) {
1853: convert_format = FMT_PEM;
1854: break;
1855: }
1856: fatal("Unsupported conversion format \"%s\"", optarg);
1.179 djm 1857: case 'n':
1858: cert_principals = optarg;
1859: break;
1.12 markus 1860: case 'p':
1861: change_passphrase = 1;
1862: break;
1863: case 'c':
1864: change_comment = 1;
1865: break;
1866: case 'f':
1.124 avsm 1867: if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1868: sizeof(identity_file))
1869: fatal("Identity filename too long");
1.12 markus 1870: have_identity = 1;
1871: break;
1.105 jakob 1872: case 'g':
1873: print_generic = 1;
1874: break;
1.12 markus 1875: case 'P':
1876: identity_passphrase = optarg;
1877: break;
1878: case 'N':
1879: identity_new_passphrase = optarg;
1880: break;
1.179 djm 1881: case 'O':
1.186 djm 1882: add_cert_option(optarg);
1.179 djm 1883: break;
1.12 markus 1884: case 'C':
1885: identity_comment = optarg;
1886: break;
1887: case 'q':
1888: quiet = 1;
1.20 deraadt 1889: break;
1.57 markus 1890: case 'e':
1.19 markus 1891: case 'x':
1.57 markus 1892: /* export key */
1.193 djm 1893: convert_to = 1;
1.19 markus 1894: break;
1.179 djm 1895: case 'h':
1896: cert_key_type = SSH2_CERT_TYPE_HOST;
1.190 djm 1897: certflags_flags = 0;
1.179 djm 1898: break;
1.57 markus 1899: case 'i':
1.19 markus 1900: case 'X':
1.57 markus 1901: /* import key */
1.193 djm 1902: convert_from = 1;
1.19 markus 1903: break;
1904: case 'y':
1905: print_public = 1;
1.47 jakob 1906: break;
1.19 markus 1907: case 'd':
1.33 markus 1908: key_type_name = "dsa";
1.19 markus 1909: break;
1.179 djm 1910: case 's':
1911: ca_key_path = optarg;
1912: break;
1.33 markus 1913: case 't':
1914: key_type_name = optarg;
1915: break;
1.75 markus 1916: case 'D':
1.177 markus 1917: pkcs11provider = optarg;
1.66 markus 1918: break;
1.113 djm 1919: case 'v':
1920: if (log_level == SYSLOG_LEVEL_INFO)
1921: log_level = SYSLOG_LEVEL_DEBUG1;
1922: else {
1.117 deraadt 1923: if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1.113 djm 1924: log_level < SYSLOG_LEVEL_DEBUG3)
1925: log_level++;
1926: }
1927: break;
1.105 jakob 1928: case 'r':
1.119 djm 1929: rr_hostname = optarg;
1.105 jakob 1930: break;
1.107 djm 1931: case 'W':
1.142 deraadt 1932: generator_wanted = (u_int32_t)strtonum(optarg, 1,
1933: UINT_MAX, &errstr);
1.124 avsm 1934: if (errstr)
1935: fatal("Desired generator has bad value: %s (%s)",
1936: optarg, errstr);
1.107 djm 1937: break;
1938: case 'a':
1.142 deraadt 1939: trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1.124 avsm 1940: if (errstr)
1941: fatal("Invalid number of trials: %s (%s)",
1942: optarg, errstr);
1.107 djm 1943: break;
1944: case 'M':
1.142 deraadt 1945: memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1.186 djm 1946: if (errstr)
1.124 avsm 1947: fatal("Memory limit is %s: %s", errstr, optarg);
1.107 djm 1948: break;
1949: case 'G':
1950: do_gen_candidates = 1;
1.124 avsm 1951: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1952: sizeof(out_file))
1953: fatal("Output filename too long");
1.107 djm 1954: break;
1955: case 'T':
1956: do_screen_candidates = 1;
1.124 avsm 1957: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1958: sizeof(out_file))
1959: fatal("Output filename too long");
1.107 djm 1960: break;
1961: case 'S':
1962: /* XXX - also compare length against bits */
1963: if (BN_hex2bn(&start, optarg) == 0)
1964: fatal("Invalid start point.");
1965: break;
1.179 djm 1966: case 'V':
1967: parse_cert_times(optarg);
1.186 djm 1968: break;
1969: case 'z':
1970: cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1971: if (errstr)
1972: fatal("Invalid serial number: %s", errstr);
1.179 djm 1973: break;
1.12 markus 1974: case '?':
1975: default:
1976: usage();
1977: }
1978: }
1.113 djm 1979:
1980: /* reinit */
1.156 deraadt 1981: log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
1.113 djm 1982:
1.179 djm 1983: argv += optind;
1984: argc -= optind;
1985:
1986: if (ca_key_path != NULL) {
1987: if (argc < 1) {
1988: printf("Too few arguments.\n");
1989: usage();
1990: }
1991: } else if (argc > 0) {
1.12 markus 1992: printf("Too many arguments.\n");
1.87 djm 1993: usage();
1994: }
1.12 markus 1995: if (change_passphrase && change_comment) {
1996: printf("Can only have one of -p and -c.\n");
1.166 djm 1997: usage();
1998: }
1999: if (print_fingerprint && (delete_host || hash_hosts)) {
2000: printf("Cannot use -l with -D or -R.\n");
1.12 markus 2001: usage();
1.179 djm 2002: }
2003: if (ca_key_path != NULL) {
2004: if (cert_key_id == NULL)
2005: fatal("Must specify key id (-I) when certifying");
2006: do_ca_sign(pw, argc, argv);
1.12 markus 2007: }
1.182 djm 2008: if (show_cert)
2009: do_show_cert(pw);
1.119 djm 2010: if (delete_host || hash_hosts || find_host)
2011: do_known_hosts(pw, rr_hostname);
1.49 markus 2012: if (print_fingerprint || print_bubblebabble)
1.12 markus 2013: do_fingerprint(pw);
2014: if (change_passphrase)
2015: do_change_passphrase(pw);
2016: if (change_comment)
2017: do_change_comment(pw);
1.193 djm 2018: if (convert_to)
2019: do_convert_to(pw);
2020: if (convert_from)
2021: do_convert_from(pw);
1.19 markus 2022: if (print_public)
2023: do_print_public(pw);
1.119 djm 2024: if (rr_hostname != NULL) {
1.138 jakob 2025: unsigned int n = 0;
2026:
2027: if (have_identity) {
2028: n = do_print_resource_record(pw,
2029: identity_file, rr_hostname);
2030: if (n == 0) {
2031: perror(identity_file);
2032: exit(1);
2033: }
2034: exit(0);
2035: } else {
2036:
2037: n += do_print_resource_record(pw,
2038: _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2039: n += do_print_resource_record(pw,
2040: _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2041:
2042: if (n == 0)
2043: fatal("no keys found.");
2044: exit(0);
2045: }
1.105 jakob 2046: }
1.177 markus 2047: if (pkcs11provider != NULL)
1.197 djm 2048: do_download(pw);
1.107 djm 2049:
2050: if (do_gen_candidates) {
2051: FILE *out = fopen(out_file, "w");
1.111 djm 2052:
1.107 djm 2053: if (out == NULL) {
2054: error("Couldn't open modulus candidate file \"%s\": %s",
2055: out_file, strerror(errno));
2056: return (1);
2057: }
1.130 markus 2058: if (bits == 0)
2059: bits = DEFAULT_BITS;
1.107 djm 2060: if (gen_candidates(out, memory, bits, start) != 0)
1.131 stevesk 2061: fatal("modulus candidate generation failed");
1.107 djm 2062:
2063: return (0);
2064: }
2065:
2066: if (do_screen_candidates) {
2067: FILE *in;
2068: FILE *out = fopen(out_file, "w");
2069:
2070: if (have_identity && strcmp(identity_file, "-") != 0) {
2071: if ((in = fopen(identity_file, "r")) == NULL) {
2072: fatal("Couldn't open modulus candidate "
1.111 djm 2073: "file \"%s\": %s", identity_file,
1.107 djm 2074: strerror(errno));
2075: }
2076: } else
2077: in = stdin;
2078:
2079: if (out == NULL) {
2080: fatal("Couldn't open moduli file \"%s\": %s",
2081: out_file, strerror(errno));
2082: }
2083: if (prime_test(in, out, trials, generator_wanted) != 0)
1.131 stevesk 2084: fatal("modulus screening failed");
1.108 markus 2085: return (0);
1.75 markus 2086: }
1.12 markus 2087:
2088: arc4random_stir();
2089:
1.133 djm 2090: if (key_type_name == NULL)
2091: key_type_name = "rsa";
2092:
1.35 markus 2093: type = key_type_from_name(key_type_name);
2094: if (type == KEY_UNSPEC) {
2095: fprintf(stderr, "unknown key type %s\n", key_type_name);
2096: exit(1);
1.19 markus 2097: }
1.200 djm 2098: if (bits == 0) {
2099: if (type == KEY_DSA)
2100: bits = DEFAULT_BITS_DSA;
2101: else if (type == KEY_ECDSA)
2102: bits = DEFAULT_BITS_ECDSA;
2103: else
2104: bits = DEFAULT_BITS;
2105: }
1.189 djm 2106: maxbits = (type == KEY_DSA) ?
2107: OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
2108: if (bits > maxbits) {
2109: fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
2110: exit(1);
2111: }
1.134 dtucker 2112: if (type == KEY_DSA && bits != 1024)
2113: fatal("DSA keys must be 1024 bits");
1.202 markus 2114: else if (type != KEY_ECDSA && bits < 768)
2115: fatal("Key must at least be 768 bits");
1.200 djm 2116: else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(bits) == -1)
2117: fatal("Invalid ECDSA key length - valid lengths are "
2118: "256, 384 or 521 bits");
1.33 markus 2119: if (!quiet)
1.35 markus 2120: printf("Generating public/private %s key pair.\n", key_type_name);
1.33 markus 2121: private = key_generate(type, bits);
2122: if (private == NULL) {
1.173 tobias 2123: fprintf(stderr, "key_generate failed\n");
1.33 markus 2124: exit(1);
2125: }
2126: public = key_from_private(private);
1.12 markus 2127:
2128: if (!have_identity)
2129: ask_filename(pw, "Enter file in which to save the key");
2130:
1.132 djm 2131: /* Create ~/.ssh directory if it doesn't already exist. */
1.188 djm 2132: snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2133: pw->pw_dir, _PATH_SSH_USER_DIR);
2134: if (strstr(identity_file, dotsshdir) != NULL) {
2135: if (stat(dotsshdir, &st) < 0) {
2136: if (errno != ENOENT) {
2137: error("Could not stat %s: %s", dotsshdir,
2138: strerror(errno));
2139: } else if (mkdir(dotsshdir, 0700) < 0) {
2140: error("Could not create directory '%s': %s",
2141: dotsshdir, strerror(errno));
2142: } else if (!quiet)
2143: printf("Created directory '%s'.\n", dotsshdir);
2144: }
1.12 markus 2145: }
2146: /* If the file already exists, ask the user to confirm. */
2147: if (stat(identity_file, &st) >= 0) {
2148: char yesno[3];
2149: printf("%s already exists.\n", identity_file);
2150: printf("Overwrite (y/n)? ");
2151: fflush(stdout);
2152: if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2153: exit(1);
2154: if (yesno[0] != 'y' && yesno[0] != 'Y')
2155: exit(1);
2156: }
2157: /* Ask for a passphrase (twice). */
2158: if (identity_passphrase)
2159: passphrase1 = xstrdup(identity_passphrase);
2160: else if (identity_new_passphrase)
2161: passphrase1 = xstrdup(identity_new_passphrase);
2162: else {
2163: passphrase_again:
2164: passphrase1 =
1.65 markus 2165: read_passphrase("Enter passphrase (empty for no "
2166: "passphrase): ", RP_ALLOW_STDIN);
2167: passphrase2 = read_passphrase("Enter same passphrase again: ",
2168: RP_ALLOW_STDIN);
1.12 markus 2169: if (strcmp(passphrase1, passphrase2) != 0) {
1.65 markus 2170: /*
2171: * The passphrases do not match. Clear them and
2172: * retry.
2173: */
1.12 markus 2174: memset(passphrase1, 0, strlen(passphrase1));
2175: memset(passphrase2, 0, strlen(passphrase2));
2176: xfree(passphrase1);
2177: xfree(passphrase2);
2178: printf("Passphrases do not match. Try again.\n");
2179: goto passphrase_again;
2180: }
2181: /* Clear the other copy of the passphrase. */
2182: memset(passphrase2, 0, strlen(passphrase2));
2183: xfree(passphrase2);
2184: }
2185:
2186: if (identity_comment) {
2187: strlcpy(comment, identity_comment, sizeof(comment));
2188: } else {
1.172 stevesk 2189: /* Create default comment field for the passphrase. */
1.12 markus 2190: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2191: }
2192:
2193: /* Save the key with the given passphrase and comment. */
1.52 markus 2194: if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56 markus 2195: printf("Saving the key failed: %s.\n", identity_file);
1.12 markus 2196: memset(passphrase1, 0, strlen(passphrase1));
2197: xfree(passphrase1);
2198: exit(1);
2199: }
2200: /* Clear the passphrase. */
2201: memset(passphrase1, 0, strlen(passphrase1));
2202: xfree(passphrase1);
2203:
2204: /* Clear the private key and the random number generator. */
1.33 markus 2205: key_free(private);
1.12 markus 2206: arc4random_stir();
2207:
2208: if (!quiet)
2209: printf("Your identification has been saved in %s.\n", identity_file);
2210:
2211: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 2212: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2213: if (fd == -1) {
1.12 markus 2214: printf("Could not save your public key in %s\n", identity_file);
1.46 deraadt 2215: exit(1);
2216: }
2217: f = fdopen(fd, "w");
2218: if (f == NULL) {
1.173 tobias 2219: printf("fdopen %s failed\n", identity_file);
1.12 markus 2220: exit(1);
2221: }
1.19 markus 2222: if (!key_write(public, f))
1.173 tobias 2223: fprintf(stderr, "write key failed\n");
1.19 markus 2224: fprintf(f, " %s\n", comment);
1.12 markus 2225: fclose(f);
2226:
2227: if (!quiet) {
1.50 markus 2228: char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1.167 grunk 2229: char *ra = key_fingerprint(public, SSH_FP_MD5,
2230: SSH_FP_RANDOMART);
1.19 markus 2231: printf("Your public key has been saved in %s.\n",
2232: identity_file);
1.12 markus 2233: printf("The key fingerprint is:\n");
1.50 markus 2234: printf("%s %s\n", fp, comment);
1.167 grunk 2235: printf("The key's randomart image is:\n");
2236: printf("%s\n", ra);
2237: xfree(ra);
1.50 markus 2238: xfree(fp);
1.12 markus 2239: }
1.19 markus 2240:
2241: key_free(public);
1.12 markus 2242: exit(0);
1.1 deraadt 2243: }