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