Annotation of src/usr.bin/ssh/ssh-keygen.c, Revision 1.268
1.268 ! tobias 1: /* $OpenBSD: ssh-keygen.c,v 1.267 2015/03/23 06:06:38 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.19 markus 18:
19: #include <openssl/evp.h>
20: #include <openssl/pem.h>
1.145 stevesk 21:
1.148 stevesk 22: #include <errno.h>
1.147 stevesk 23: #include <fcntl.h>
1.248 djm 24: #include <netdb.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.253 deraadt 30: #include <limits.h>
1.1 deraadt 31:
32: #include "xmalloc.h"
1.252 djm 33: #include "sshkey.h"
1.53 markus 34: #include "rsa.h"
1.19 markus 35: #include "authfile.h"
36: #include "uuencode.h"
1.252 djm 37: #include "sshbuf.h"
1.40 markus 38: #include "pathnames.h"
1.41 markus 39: #include "log.h"
1.114 djm 40: #include "misc.h"
1.119 djm 41: #include "match.h"
42: #include "hostfile.h"
1.146 stevesk 43: #include "dns.h"
1.223 djm 44: #include "ssh.h"
1.179 djm 45: #include "ssh2.h"
1.252 djm 46: #include "ssherr.h"
1.223 djm 47: #include "atomicio.h"
48: #include "krl.h"
1.251 djm 49: #include "digest.h"
1.32 markus 50:
1.177 markus 51: #ifdef ENABLE_PKCS11
52: #include "ssh-pkcs11.h"
1.106 djm 53: #endif
1.66 markus 54:
1.130 markus 55: /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
56: #define DEFAULT_BITS 2048
57: #define DEFAULT_BITS_DSA 1024
1.203 naddy 58: #define DEFAULT_BITS_ECDSA 256
1.130 markus 59: u_int32_t bits = 0;
1.1 deraadt 60:
1.14 markus 61: /*
62: * Flag indicating that we just want to change the passphrase. This can be
63: * set on the command line.
64: */
1.1 deraadt 65: int change_passphrase = 0;
66:
1.14 markus 67: /*
68: * Flag indicating that we just want to change the comment. This can be set
69: * on the command line.
70: */
1.1 deraadt 71: int change_comment = 0;
72:
1.2 provos 73: int quiet = 0;
74:
1.169 grunk 75: int log_level = SYSLOG_LEVEL_INFO;
76:
1.119 djm 77: /* Flag indicating that we want to hash a known_hosts file */
78: int hash_hosts = 0;
79: /* Flag indicating that we want lookup a host in known_hosts file */
80: int find_host = 0;
81: /* Flag indicating that we want to delete a host from a known_hosts file */
82: int delete_host = 0;
83:
1.182 djm 84: /* Flag indicating that we want to show the contents of a certificate */
85: int show_cert = 0;
86:
1.8 markus 87: /* Flag indicating that we just want to see the key fingerprint */
88: int print_fingerprint = 0;
1.49 markus 89: int print_bubblebabble = 0;
1.8 markus 90:
1.251 djm 91: /* Hash algorithm to use for fingerprints. */
92: int fingerprint_hash = SSH_FP_HASH_DEFAULT;
93:
1.10 markus 94: /* The identity file name, given on the command line or entered by the user. */
95: char identity_file[1024];
96: int have_identity = 0;
1.1 deraadt 97:
98: /* This is set to the passphrase if given on the command line. */
99: char *identity_passphrase = NULL;
100:
101: /* This is set to the new passphrase if given on the command line. */
102: char *identity_new_passphrase = NULL;
103:
104: /* This is set to the new comment if given on the command line. */
105: char *identity_comment = NULL;
106:
1.179 djm 107: /* Path to CA key when certifying keys. */
108: char *ca_key_path = NULL;
109:
1.186 djm 110: /* Certificate serial number */
1.220 djm 111: unsigned long long cert_serial = 0;
1.186 djm 112:
1.179 djm 113: /* Key type when certifying */
114: u_int cert_key_type = SSH2_CERT_TYPE_USER;
115:
116: /* "key ID" of signed key */
117: char *cert_key_id = NULL;
118:
119: /* Comma-separated list of principal names for certifying keys */
120: char *cert_principals = NULL;
121:
122: /* Validity period for certificates */
123: u_int64_t cert_valid_from = 0;
124: u_int64_t cert_valid_to = ~0ULL;
125:
1.186 djm 126: /* Certificate options */
1.190 djm 127: #define CERTOPT_X_FWD (1)
128: #define CERTOPT_AGENT_FWD (1<<1)
129: #define CERTOPT_PORT_FWD (1<<2)
130: #define CERTOPT_PTY (1<<3)
131: #define CERTOPT_USER_RC (1<<4)
132: #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
133: CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
134: u_int32_t certflags_flags = CERTOPT_DEFAULT;
135: char *certflags_command = NULL;
136: char *certflags_src_addr = NULL;
1.179 djm 137:
1.193 djm 138: /* Conversion to/from various formats */
139: int convert_to = 0;
140: int convert_from = 0;
141: enum {
142: FMT_RFC4716,
143: FMT_PKCS8,
144: FMT_PEM
145: } convert_format = FMT_RFC4716;
1.19 markus 146: int print_public = 0;
1.105 jakob 147: int print_generic = 0;
1.33 markus 148:
1.87 djm 149: char *key_type_name = NULL;
1.19 markus 150:
1.197 djm 151: /* Load key from this PKCS#11 provider */
152: char *pkcs11provider = NULL;
1.193 djm 153:
1.237 markus 154: /* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
155: int use_new_format = 0;
156:
157: /* Cipher for new-format private keys */
158: char *new_format_cipher = NULL;
159:
160: /*
161: * Number of KDF rounds to derive new format keys /
162: * number of primality trials when screening moduli.
163: */
164: int rounds = 0;
165:
1.10 markus 166: /* argv0 */
167: extern char *__progname;
1.1 deraadt 168:
1.248 djm 169: char hostname[NI_MAXHOST];
1.19 markus 170:
1.115 djm 171: /* moduli.c */
1.124 avsm 172: int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
1.215 dtucker 173: int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
174: unsigned long);
1.115 djm 175:
1.63 itojun 176: static void
1.255 djm 177: type_bits_valid(int type, const char *name, u_int32_t *bitsp)
1.206 stevesk 178: {
1.255 djm 179: u_int maxbits, nid;
1.206 stevesk 180:
181: if (type == KEY_UNSPEC) {
182: fprintf(stderr, "unknown key type %s\n", key_type_name);
183: exit(1);
184: }
1.209 djm 185: if (*bitsp == 0) {
1.206 stevesk 186: if (type == KEY_DSA)
1.209 djm 187: *bitsp = DEFAULT_BITS_DSA;
1.255 djm 188: else if (type == KEY_ECDSA) {
189: if (name != NULL &&
190: (nid = sshkey_ecdsa_nid_from_name(name)) > 0)
191: *bitsp = sshkey_curve_nid_to_bits(nid);
192: if (*bitsp == 0)
193: *bitsp = DEFAULT_BITS_ECDSA;
194: }
1.206 stevesk 195: else
1.209 djm 196: *bitsp = DEFAULT_BITS;
1.206 stevesk 197: }
198: maxbits = (type == KEY_DSA) ?
199: OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
1.209 djm 200: if (*bitsp > maxbits) {
1.206 stevesk 201: fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
202: exit(1);
203: }
1.246 markus 204: #ifdef WITH_OPENSSL
1.209 djm 205: if (type == KEY_DSA && *bitsp != 1024)
1.206 stevesk 206: fatal("DSA keys must be 1024 bits");
1.238 markus 207: else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
1.206 stevesk 208: fatal("Key must at least be 768 bits");
1.252 djm 209: else if (type == KEY_ECDSA && sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
1.206 stevesk 210: fatal("Invalid ECDSA key length - valid lengths are "
211: "256, 384 or 521 bits");
1.246 markus 212: #endif
1.206 stevesk 213: }
214:
215: static void
1.10 markus 216: ask_filename(struct passwd *pw, const char *prompt)
1.1 deraadt 217: {
1.12 markus 218: char buf[1024];
1.35 markus 219: char *name = NULL;
220:
1.92 stevesk 221: if (key_type_name == NULL)
1.40 markus 222: name = _PATH_SSH_CLIENT_ID_RSA;
1.140 deraadt 223: else {
1.252 djm 224: switch (sshkey_type_from_name(key_type_name)) {
1.92 stevesk 225: case KEY_RSA1:
226: name = _PATH_SSH_CLIENT_IDENTITY;
227: break;
1.186 djm 228: case KEY_DSA_CERT:
229: case KEY_DSA_CERT_V00:
1.92 stevesk 230: case KEY_DSA:
231: name = _PATH_SSH_CLIENT_ID_DSA;
232: break;
1.200 djm 233: case KEY_ECDSA_CERT:
234: case KEY_ECDSA:
235: name = _PATH_SSH_CLIENT_ID_ECDSA;
236: break;
1.186 djm 237: case KEY_RSA_CERT:
238: case KEY_RSA_CERT_V00:
1.92 stevesk 239: case KEY_RSA:
240: name = _PATH_SSH_CLIENT_ID_RSA;
241: break;
1.238 markus 242: case KEY_ED25519:
243: case KEY_ED25519_CERT:
244: name = _PATH_SSH_CLIENT_ID_ED25519;
245: break;
1.92 stevesk 246: default:
1.173 tobias 247: fprintf(stderr, "bad key type\n");
1.92 stevesk 248: exit(1);
249: break;
250: }
1.140 deraadt 251: }
1.35 markus 252: snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
1.37 markus 253: fprintf(stderr, "%s (%s): ", prompt, identity_file);
1.12 markus 254: if (fgets(buf, sizeof(buf), stdin) == NULL)
255: exit(1);
1.162 gilles 256: buf[strcspn(buf, "\n")] = '\0';
1.12 markus 257: if (strcmp(buf, "") != 0)
258: strlcpy(identity_file, buf, sizeof(identity_file));
259: have_identity = 1;
1.7 markus 260: }
261:
1.252 djm 262: static struct sshkey *
1.61 markus 263: load_identity(char *filename)
1.19 markus 264: {
1.52 markus 265: char *pass;
1.252 djm 266: struct sshkey *prv;
267: int r;
1.52 markus 268:
1.252 djm 269: if ((r = sshkey_load_private(filename, "", &prv, NULL)) == 0)
270: return prv;
271: if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
272: fatal("Load key \"%s\": %s", filename, ssh_err(r));
273: if (identity_passphrase)
274: pass = xstrdup(identity_passphrase);
275: else
276: pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
277: r = sshkey_load_private(filename, pass, &prv, NULL);
278: explicit_bzero(pass, strlen(pass));
279: free(pass);
280: if (r != 0)
281: fatal("Load key \"%s\": %s", filename, ssh_err(r));
1.52 markus 282: return prv;
1.19 markus 283: }
284:
1.32 markus 285: #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
1.100 deraadt 286: #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
1.32 markus 287: #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1.42 stevesk 288: #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
1.19 markus 289:
1.246 markus 290: #ifdef WITH_OPENSSL
1.63 itojun 291: static void
1.252 djm 292: do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
1.19 markus 293: {
1.252 djm 294: size_t len;
1.36 markus 295: u_char *blob;
1.176 djm 296: char comment[61];
1.252 djm 297: int r;
1.19 markus 298:
1.213 djm 299: if (k->type == KEY_RSA1) {
300: fprintf(stderr, "version 1 keys are not supported\n");
301: exit(1);
302: }
1.252 djm 303: if ((r = sshkey_to_blob(k, &blob, &len)) != 0) {
304: fprintf(stderr, "key_to_blob failed: %s\n", ssh_err(r));
1.81 markus 305: exit(1);
306: }
1.176 djm 307: /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
308: snprintf(comment, sizeof(comment),
309: "%u-bit %s, converted by %s@%s from OpenSSH",
1.252 djm 310: sshkey_size(k), sshkey_type(k),
1.19 markus 311: pw->pw_name, hostname);
1.176 djm 312:
313: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
314: fprintf(stdout, "Comment: \"%s\"\n", comment);
1.19 markus 315: dump_base64(stdout, blob, len);
1.32 markus 316: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1.252 djm 317: sshkey_free(k);
1.227 djm 318: free(blob);
1.19 markus 319: exit(0);
320: }
321:
1.63 itojun 322: static void
1.252 djm 323: do_convert_to_pkcs8(struct sshkey *k)
1.193 djm 324: {
1.252 djm 325: switch (sshkey_type_plain(k->type)) {
1.213 djm 326: case KEY_RSA1:
1.193 djm 327: case KEY_RSA:
328: if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
329: fatal("PEM_write_RSA_PUBKEY failed");
330: break;
331: case KEY_DSA:
332: if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
333: fatal("PEM_write_DSA_PUBKEY failed");
334: break;
1.200 djm 335: case KEY_ECDSA:
336: if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
337: fatal("PEM_write_EC_PUBKEY failed");
338: break;
1.193 djm 339: default:
1.252 djm 340: fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
1.193 djm 341: }
342: exit(0);
343: }
344:
345: static void
1.252 djm 346: do_convert_to_pem(struct sshkey *k)
1.193 djm 347: {
1.252 djm 348: switch (sshkey_type_plain(k->type)) {
1.213 djm 349: case KEY_RSA1:
1.193 djm 350: case KEY_RSA:
351: if (!PEM_write_RSAPublicKey(stdout, k->rsa))
352: fatal("PEM_write_RSAPublicKey failed");
353: break;
354: #if notyet /* OpenSSH 0.9.8 lacks this function */
355: case KEY_DSA:
356: if (!PEM_write_DSAPublicKey(stdout, k->dsa))
357: fatal("PEM_write_DSAPublicKey failed");
358: break;
359: #endif
1.200 djm 360: /* XXX ECDSA? */
1.193 djm 361: default:
1.252 djm 362: fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
1.193 djm 363: }
364: exit(0);
365: }
366:
367: static void
368: do_convert_to(struct passwd *pw)
369: {
1.252 djm 370: struct sshkey *k;
1.193 djm 371: struct stat st;
1.252 djm 372: int r;
1.193 djm 373:
374: if (!have_identity)
375: ask_filename(pw, "Enter file in which the key is");
376: if (stat(identity_file, &st) < 0)
377: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.252 djm 378: if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
379: k = load_identity(identity_file);
1.193 djm 380: switch (convert_format) {
381: case FMT_RFC4716:
382: do_convert_to_ssh2(pw, k);
383: break;
384: case FMT_PKCS8:
385: do_convert_to_pkcs8(k);
386: break;
387: case FMT_PEM:
388: do_convert_to_pem(k);
389: break;
390: default:
391: fatal("%s: unknown key format %d", __func__, convert_format);
392: }
393: exit(0);
394: }
395:
1.252 djm 396: /*
397: * This is almost exactly the bignum1 encoding, but with 32 bit for length
398: * instead of 16.
399: */
1.193 djm 400: static void
1.252 djm 401: buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
1.32 markus 402: {
1.252 djm 403: u_int bytes, bignum_bits;
404: int r;
1.53 markus 405:
1.252 djm 406: if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
407: fatal("%s: buffer error: %s", __func__, ssh_err(r));
408: bytes = (bignum_bits + 7) / 8;
409: if (sshbuf_len(b) < bytes)
410: fatal("%s: input buffer too small: need %d have %zu",
411: __func__, bytes, sshbuf_len(b));
412: if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
413: fatal("%s: BN_bin2bn failed", __func__);
414: if ((r = sshbuf_consume(b, bytes)) != 0)
415: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.32 markus 416: }
417:
1.252 djm 418: static struct sshkey *
1.93 markus 419: do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
1.32 markus 420: {
1.252 djm 421: struct sshbuf *b;
422: struct sshkey *key = NULL;
1.64 markus 423: char *type, *cipher;
1.252 djm 424: u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
425: int r, rlen, ktype;
426: u_int magic, i1, i2, i3, i4;
427: size_t slen;
1.62 markus 428: u_long e;
1.32 markus 429:
1.252 djm 430: if ((b = sshbuf_from(blob, blen)) == NULL)
431: fatal("%s: sshbuf_from failed", __func__);
432: if ((r = sshbuf_get_u32(b, &magic)) != 0)
433: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.32 markus 434:
435: if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
1.252 djm 436: error("bad magic 0x%x != 0x%x", magic,
437: SSH_COM_PRIVATE_KEY_MAGIC);
438: sshbuf_free(b);
1.32 markus 439: return NULL;
440: }
1.252 djm 441: if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
442: (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
443: (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
444: (r = sshbuf_get_u32(b, &i2)) != 0 ||
445: (r = sshbuf_get_u32(b, &i3)) != 0 ||
446: (r = sshbuf_get_u32(b, &i4)) != 0)
447: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.158 stevesk 448: debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
1.32 markus 449: if (strcmp(cipher, "none") != 0) {
450: error("unsupported cipher %s", cipher);
1.227 djm 451: free(cipher);
1.252 djm 452: sshbuf_free(b);
1.227 djm 453: free(type);
1.32 markus 454: return NULL;
455: }
1.227 djm 456: free(cipher);
1.32 markus 457:
1.53 markus 458: if (strstr(type, "dsa")) {
459: ktype = KEY_DSA;
460: } else if (strstr(type, "rsa")) {
461: ktype = KEY_RSA;
462: } else {
1.252 djm 463: sshbuf_free(b);
1.227 djm 464: free(type);
1.32 markus 465: return NULL;
466: }
1.252 djm 467: if ((key = sshkey_new_private(ktype)) == NULL)
468: fatal("key_new_private failed");
1.227 djm 469: free(type);
1.53 markus 470:
471: switch (key->type) {
472: case KEY_DSA:
1.252 djm 473: buffer_get_bignum_bits(b, key->dsa->p);
474: buffer_get_bignum_bits(b, key->dsa->g);
475: buffer_get_bignum_bits(b, key->dsa->q);
476: buffer_get_bignum_bits(b, key->dsa->pub_key);
477: buffer_get_bignum_bits(b, key->dsa->priv_key);
1.53 markus 478: break;
479: case KEY_RSA:
1.252 djm 480: if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
481: (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
482: (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
483: fatal("%s: buffer error: %s", __func__, ssh_err(r));
484: e = e1;
1.62 markus 485: debug("e %lx", e);
486: if (e < 30) {
487: e <<= 8;
1.252 djm 488: e += e2;
1.62 markus 489: debug("e %lx", e);
490: e <<= 8;
1.252 djm 491: e += e3;
1.62 markus 492: debug("e %lx", e);
493: }
494: if (!BN_set_word(key->rsa->e, e)) {
1.252 djm 495: sshbuf_free(b);
496: sshkey_free(key);
1.53 markus 497: return NULL;
498: }
1.252 djm 499: buffer_get_bignum_bits(b, key->rsa->d);
500: buffer_get_bignum_bits(b, key->rsa->n);
501: buffer_get_bignum_bits(b, key->rsa->iqmp);
502: buffer_get_bignum_bits(b, key->rsa->q);
503: buffer_get_bignum_bits(b, key->rsa->p);
504: if ((r = rsa_generate_additional_parameters(key->rsa)) != 0)
505: fatal("generate RSA parameters failed: %s", ssh_err(r));
1.53 markus 506: break;
507: }
1.252 djm 508: rlen = sshbuf_len(b);
1.85 deraadt 509: if (rlen != 0)
1.53 markus 510: error("do_convert_private_ssh2_from_blob: "
511: "remaining bytes in key blob %d", rlen);
1.252 djm 512: sshbuf_free(b);
1.64 markus 513:
514: /* try the key */
1.252 djm 515: if (sshkey_sign(key, &sig, &slen, data, sizeof(data), 0) != 0 ||
516: sshkey_verify(key, sig, slen, data, sizeof(data), 0) != 0) {
517: sshkey_free(key);
518: free(sig);
519: return NULL;
520: }
1.227 djm 521: free(sig);
1.32 markus 522: return key;
523: }
524:
1.137 dtucker 525: static int
526: get_line(FILE *fp, char *line, size_t len)
527: {
528: int c;
529: size_t pos = 0;
530:
531: line[0] = '\0';
532: while ((c = fgetc(fp)) != EOF) {
533: if (pos >= len - 1) {
534: fprintf(stderr, "input line too long.\n");
535: exit(1);
536: }
1.140 deraadt 537: switch (c) {
1.137 dtucker 538: case '\r':
539: c = fgetc(fp);
540: if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
541: fprintf(stderr, "unget: %s\n", strerror(errno));
542: exit(1);
543: }
544: return pos;
545: case '\n':
546: return pos;
547: }
548: line[pos++] = c;
549: line[pos] = '\0';
550: }
1.157 stevesk 551: /* We reached EOF */
552: return -1;
1.137 dtucker 553: }
554:
1.63 itojun 555: static void
1.252 djm 556: do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
1.19 markus 557: {
1.252 djm 558: int r, blen, escaped = 0;
1.98 markus 559: u_int len;
1.137 dtucker 560: char line[1024];
1.80 stevesk 561: u_char blob[8096];
1.19 markus 562: char encoded[8096];
563: FILE *fp;
564:
1.191 djm 565: if ((fp = fopen(identity_file, "r")) == NULL)
566: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.19 markus 567: encoded[0] = '\0';
1.137 dtucker 568: while ((blen = get_line(fp, line, sizeof(line))) != -1) {
1.228 djm 569: if (blen > 0 && line[blen - 1] == '\\')
1.25 markus 570: escaped++;
1.19 markus 571: if (strncmp(line, "----", 4) == 0 ||
572: strstr(line, ": ") != NULL) {
1.32 markus 573: if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
1.193 djm 574: *private = 1;
1.64 markus 575: if (strstr(line, " END ") != NULL) {
576: break;
577: }
1.60 markus 578: /* fprintf(stderr, "ignore: %s", line); */
1.19 markus 579: continue;
580: }
1.25 markus 581: if (escaped) {
582: escaped--;
1.60 markus 583: /* fprintf(stderr, "escaped: %s", line); */
1.25 markus 584: continue;
1.19 markus 585: }
586: strlcat(encoded, line, sizeof(encoded));
587: }
1.98 markus 588: len = strlen(encoded);
589: if (((len % 4) == 3) &&
590: (encoded[len-1] == '=') &&
591: (encoded[len-2] == '=') &&
592: (encoded[len-3] == '='))
593: encoded[len-3] = '\0';
1.91 stevesk 594: blen = uudecode(encoded, blob, sizeof(blob));
1.19 markus 595: if (blen < 0) {
596: fprintf(stderr, "uudecode failed.\n");
597: exit(1);
598: }
1.252 djm 599: if (*private)
600: *k = do_convert_private_ssh2_from_blob(blob, blen);
601: else if ((r = sshkey_from_blob(blob, blen, k)) != 0) {
602: fprintf(stderr, "decode blob failed: %s\n", ssh_err(r));
1.32 markus 603: exit(1);
604: }
1.193 djm 605: fclose(fp);
606: }
607:
608: static void
1.252 djm 609: do_convert_from_pkcs8(struct sshkey **k, int *private)
1.193 djm 610: {
611: EVP_PKEY *pubkey;
612: FILE *fp;
613:
614: if ((fp = fopen(identity_file, "r")) == NULL)
615: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
616: if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
617: fatal("%s: %s is not a recognised public key format", __func__,
618: identity_file);
619: }
620: fclose(fp);
621: switch (EVP_PKEY_type(pubkey->type)) {
622: case EVP_PKEY_RSA:
1.252 djm 623: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
624: fatal("sshkey_new failed");
1.193 djm 625: (*k)->type = KEY_RSA;
626: (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
627: break;
628: case EVP_PKEY_DSA:
1.252 djm 629: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
630: fatal("sshkey_new failed");
1.193 djm 631: (*k)->type = KEY_DSA;
632: (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
633: break;
1.200 djm 634: case EVP_PKEY_EC:
1.252 djm 635: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
636: fatal("sshkey_new failed");
1.200 djm 637: (*k)->type = KEY_ECDSA;
638: (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
1.252 djm 639: (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
1.200 djm 640: break;
1.193 djm 641: default:
642: fatal("%s: unsupported pubkey type %d", __func__,
643: EVP_PKEY_type(pubkey->type));
644: }
645: EVP_PKEY_free(pubkey);
646: return;
647: }
648:
649: static void
1.252 djm 650: do_convert_from_pem(struct sshkey **k, int *private)
1.193 djm 651: {
652: FILE *fp;
653: RSA *rsa;
654: #ifdef notyet
655: DSA *dsa;
656: #endif
657:
658: if ((fp = fopen(identity_file, "r")) == NULL)
659: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
660: if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
1.252 djm 661: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
662: fatal("sshkey_new failed");
1.193 djm 663: (*k)->type = KEY_RSA;
664: (*k)->rsa = rsa;
665: fclose(fp);
666: return;
667: }
668: #if notyet /* OpenSSH 0.9.8 lacks this function */
669: rewind(fp);
670: if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
1.252 djm 671: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
672: fatal("sshkey_new failed");
1.193 djm 673: (*k)->type = KEY_DSA;
674: (*k)->dsa = dsa;
675: fclose(fp);
676: return;
677: }
1.200 djm 678: /* XXX ECDSA */
1.193 djm 679: #endif
680: fatal("%s: unrecognised raw private key format", __func__);
681: }
682:
683: static void
684: do_convert_from(struct passwd *pw)
685: {
1.252 djm 686: struct sshkey *k = NULL;
687: int r, private = 0, ok = 0;
1.193 djm 688: struct stat st;
689:
690: if (!have_identity)
691: ask_filename(pw, "Enter file in which the key is");
692: if (stat(identity_file, &st) < 0)
693: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
694:
695: switch (convert_format) {
696: case FMT_RFC4716:
697: do_convert_from_ssh2(pw, &k, &private);
698: break;
699: case FMT_PKCS8:
700: do_convert_from_pkcs8(&k, &private);
701: break;
702: case FMT_PEM:
703: do_convert_from_pem(&k, &private);
704: break;
705: default:
706: fatal("%s: unknown key format %d", __func__, convert_format);
707: }
708:
1.260 djm 709: if (!private) {
1.252 djm 710: if ((r = sshkey_write(k, stdout)) == 0)
711: ok = 1;
1.193 djm 712: if (ok)
713: fprintf(stdout, "\n");
1.260 djm 714: } else {
1.193 djm 715: switch (k->type) {
716: case KEY_DSA:
717: ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
718: NULL, 0, NULL, NULL);
719: break;
1.200 djm 720: case KEY_ECDSA:
721: ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
722: NULL, 0, NULL, NULL);
723: break;
1.193 djm 724: case KEY_RSA:
725: ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
726: NULL, 0, NULL, NULL);
727: break;
728: default:
729: fatal("%s: unsupported key type %s", __func__,
1.252 djm 730: sshkey_type(k));
1.193 djm 731: }
732: }
733:
1.32 markus 734: if (!ok) {
1.173 tobias 735: fprintf(stderr, "key write failed\n");
1.32 markus 736: exit(1);
737: }
1.252 djm 738: sshkey_free(k);
1.19 markus 739: exit(0);
740: }
1.246 markus 741: #endif
1.19 markus 742:
1.63 itojun 743: static void
1.19 markus 744: do_print_public(struct passwd *pw)
745: {
1.252 djm 746: struct sshkey *prv;
1.19 markus 747: struct stat st;
1.252 djm 748: int r;
1.19 markus 749:
750: if (!have_identity)
751: ask_filename(pw, "Enter file in which the key is");
752: if (stat(identity_file, &st) < 0) {
753: perror(identity_file);
754: exit(1);
755: }
1.61 markus 756: prv = load_identity(identity_file);
1.252 djm 757: if ((r = sshkey_write(prv, stdout)) != 0)
758: fprintf(stderr, "key_write failed: %s", ssh_err(r));
759: sshkey_free(prv);
1.19 markus 760: fprintf(stdout, "\n");
761: exit(0);
762: }
763:
1.66 markus 764: static void
1.197 djm 765: do_download(struct passwd *pw)
1.75 markus 766: {
1.177 markus 767: #ifdef ENABLE_PKCS11
1.252 djm 768: struct sshkey **keys = NULL;
1.177 markus 769: int i, nkeys;
1.252 djm 770: enum sshkey_fp_rep rep;
1.251 djm 771: int fptype;
1.221 djm 772: char *fp, *ra;
1.222 djm 773:
1.251 djm 774: fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
775: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1.75 markus 776:
1.177 markus 777: pkcs11_init(0);
778: nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
779: if (nkeys <= 0)
780: fatal("cannot read public key from pkcs11");
781: for (i = 0; i < nkeys; i++) {
1.221 djm 782: if (print_fingerprint) {
1.252 djm 783: fp = sshkey_fingerprint(keys[i], fptype, rep);
784: ra = sshkey_fingerprint(keys[i], fingerprint_hash,
1.221 djm 785: SSH_FP_RANDOMART);
1.259 djm 786: if (fp == NULL || ra == NULL)
787: fatal("%s: sshkey_fingerprint fail", __func__);
1.252 djm 788: printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
789: fp, sshkey_type(keys[i]));
1.221 djm 790: if (log_level >= SYSLOG_LEVEL_VERBOSE)
791: printf("%s\n", ra);
1.227 djm 792: free(ra);
793: free(fp);
1.221 djm 794: } else {
1.252 djm 795: (void) sshkey_write(keys[i], stdout); /* XXX check */
1.221 djm 796: fprintf(stdout, "\n");
797: }
1.252 djm 798: sshkey_free(keys[i]);
1.97 markus 799: }
1.227 djm 800: free(keys);
1.177 markus 801: pkcs11_terminate();
1.75 markus 802: exit(0);
1.177 markus 803: #else
804: fatal("no pkcs11 support");
805: #endif /* ENABLE_PKCS11 */
1.75 markus 806: }
1.66 markus 807:
1.63 itojun 808: static void
1.8 markus 809: do_fingerprint(struct passwd *pw)
810: {
1.15 markus 811: FILE *f;
1.252 djm 812: struct sshkey *public;
1.167 grunk 813: char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
1.252 djm 814: int r, i, skip = 0, num = 0, invalid = 1;
815: enum sshkey_fp_rep rep;
1.251 djm 816: int fptype;
1.12 markus 817: struct stat st;
818:
1.251 djm 819: fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
820: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1.12 markus 821: if (!have_identity)
822: ask_filename(pw, "Enter file in which the key is");
823: if (stat(identity_file, &st) < 0) {
824: perror(identity_file);
825: exit(1);
826: }
1.252 djm 827: if ((r = sshkey_load_public(identity_file, &public, &comment)) != 0)
1.264 djm 828: debug2("Error loading public key \"%s\": %s",
1.252 djm 829: identity_file, ssh_err(r));
830: else {
831: fp = sshkey_fingerprint(public, fptype, rep);
832: ra = sshkey_fingerprint(public, fingerprint_hash,
1.251 djm 833: SSH_FP_RANDOMART);
1.259 djm 834: if (fp == NULL || ra == NULL)
835: fatal("%s: sshkey_fingerprint fail", __func__);
1.252 djm 836: printf("%u %s %s (%s)\n", sshkey_size(public), fp, comment,
837: sshkey_type(public));
1.169 grunk 838: if (log_level >= SYSLOG_LEVEL_VERBOSE)
839: printf("%s\n", ra);
1.252 djm 840: sshkey_free(public);
1.227 djm 841: free(comment);
842: free(ra);
843: free(fp);
1.15 markus 844: exit(0);
845: }
1.144 markus 846: if (comment) {
1.227 djm 847: free(comment);
1.144 markus 848: comment = NULL;
849: }
1.15 markus 850:
1.191 djm 851: if ((f = fopen(identity_file, "r")) == NULL)
852: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
853:
854: while (fgets(line, sizeof(line), f)) {
855: if ((cp = strchr(line, '\n')) == NULL) {
856: error("line %d too long: %.40s...",
857: num + 1, line);
858: skip = 1;
859: continue;
860: }
861: num++;
862: if (skip) {
863: skip = 0;
864: continue;
865: }
866: *cp = '\0';
867:
868: /* Skip leading whitespace, empty and comment lines. */
869: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
870: ;
871: if (!*cp || *cp == '\n' || *cp == '#')
872: continue;
873: i = strtol(cp, &ep, 10);
874: if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
875: int quoted = 0;
876: comment = cp;
877: for (; *cp && (quoted || (*cp != ' ' &&
878: *cp != '\t')); cp++) {
879: if (*cp == '\\' && cp[1] == '"')
880: cp++; /* Skip both */
881: else if (*cp == '"')
882: quoted = !quoted;
1.15 markus 883: }
1.191 djm 884: if (!*cp)
1.15 markus 885: continue;
1.191 djm 886: *cp++ = '\0';
887: }
888: ep = cp;
1.252 djm 889: if ((public = sshkey_new(KEY_RSA1)) == NULL)
890: fatal("sshkey_new failed");
891: if ((r = sshkey_read(public, &cp)) != 0) {
1.191 djm 892: cp = ep;
1.252 djm 893: sshkey_free(public);
894: if ((public = sshkey_new(KEY_UNSPEC)) == NULL)
895: fatal("sshkey_new failed");
896: if ((r = sshkey_read(public, &cp)) != 0) {
897: sshkey_free(public);
1.191 djm 898: continue;
1.12 markus 899: }
900: }
1.191 djm 901: comment = *cp ? cp : comment;
1.252 djm 902: fp = sshkey_fingerprint(public, fptype, rep);
903: ra = sshkey_fingerprint(public, fingerprint_hash,
1.251 djm 904: SSH_FP_RANDOMART);
1.259 djm 905: if (fp == NULL || ra == NULL)
906: fatal("%s: sshkey_fingerprint fail", __func__);
1.252 djm 907: printf("%u %s %s (%s)\n", sshkey_size(public), fp,
908: comment ? comment : "no comment", sshkey_type(public));
1.191 djm 909: if (log_level >= SYSLOG_LEVEL_VERBOSE)
910: printf("%s\n", ra);
1.227 djm 911: free(ra);
912: free(fp);
1.252 djm 913: sshkey_free(public);
1.191 djm 914: invalid = 0;
1.15 markus 915: }
1.191 djm 916: fclose(f);
917:
1.15 markus 918: if (invalid) {
1.83 markus 919: printf("%s is not a public key file.\n", identity_file);
1.15 markus 920: exit(1);
1.12 markus 921: }
922: exit(0);
1.8 markus 923: }
924:
1.119 djm 925: static void
1.206 stevesk 926: do_gen_all_hostkeys(struct passwd *pw)
927: {
928: struct {
929: char *key_type;
930: char *key_type_display;
931: char *path;
932: } key_types[] = {
1.267 djm 933: #ifdef WITH_OPENSSL
934: #ifdef WITH_SSH1
1.206 stevesk 935: { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
1.267 djm 936: #endif /* WITH_SSH1 */
1.206 stevesk 937: { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
938: { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
939: { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
1.267 djm 940: #endif /* WITH_OPENSSL */
1.238 markus 941: { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
1.206 stevesk 942: { NULL, NULL, NULL }
943: };
944:
945: int first = 0;
946: struct stat st;
1.252 djm 947: struct sshkey *private, *public;
1.206 stevesk 948: char comment[1024];
1.252 djm 949: int i, type, fd, r;
1.206 stevesk 950: FILE *f;
951:
952: for (i = 0; key_types[i].key_type; i++) {
953: if (stat(key_types[i].path, &st) == 0)
954: continue;
955: if (errno != ENOENT) {
956: printf("Could not stat %s: %s", key_types[i].path,
957: strerror(errno));
958: first = 0;
959: continue;
960: }
961:
962: if (first == 0) {
963: first = 1;
964: printf("%s: generating new host keys: ", __progname);
965: }
966: printf("%s ", key_types[i].key_type_display);
967: fflush(stdout);
1.252 djm 968: type = sshkey_type_from_name(key_types[i].key_type);
1.206 stevesk 969: strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
970: bits = 0;
1.255 djm 971: type_bits_valid(type, NULL, &bits);
1.252 djm 972: if ((r = sshkey_generate(type, bits, &private)) != 0) {
973: fprintf(stderr, "key_generate failed: %s\n",
974: ssh_err(r));
1.206 stevesk 975: first = 0;
976: continue;
977: }
1.252 djm 978: if ((r = sshkey_from_private(private, &public)) != 0)
979: fatal("sshkey_from_private failed: %s", ssh_err(r));
1.206 stevesk 980: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
981: hostname);
1.252 djm 982: if ((r = sshkey_save_private(private, identity_file, "",
983: comment, use_new_format, new_format_cipher, rounds)) != 0) {
984: printf("Saving key \"%s\" failed: %s\n", identity_file,
985: ssh_err(r));
986: sshkey_free(private);
987: sshkey_free(public);
1.206 stevesk 988: first = 0;
989: continue;
990: }
1.252 djm 991: sshkey_free(private);
1.206 stevesk 992: strlcat(identity_file, ".pub", sizeof(identity_file));
993: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
994: if (fd == -1) {
995: printf("Could not save your public key in %s\n",
996: identity_file);
1.252 djm 997: sshkey_free(public);
1.206 stevesk 998: first = 0;
999: continue;
1000: }
1001: f = fdopen(fd, "w");
1002: if (f == NULL) {
1003: printf("fdopen %s failed\n", identity_file);
1.250 doug 1004: close(fd);
1.252 djm 1005: sshkey_free(public);
1.206 stevesk 1006: first = 0;
1007: continue;
1008: }
1.254 djm 1009: if ((r = sshkey_write(public, f)) != 0) {
1010: fprintf(stderr, "write key failed: %s\n", ssh_err(r));
1.250 doug 1011: fclose(f);
1.252 djm 1012: sshkey_free(public);
1.206 stevesk 1013: first = 0;
1014: continue;
1015: }
1016: fprintf(f, " %s\n", comment);
1017: fclose(f);
1.252 djm 1018: sshkey_free(public);
1.206 stevesk 1019:
1020: }
1021: if (first != 0)
1022: printf("\n");
1023: }
1024:
1.256 djm 1025: struct known_hosts_ctx {
1.257 djm 1026: const char *host; /* Hostname searched for in find/delete case */
1027: FILE *out; /* Output file, stdout for find_hosts case */
1028: int has_unhashed; /* When hashing, original had unhashed hosts */
1029: int found_key; /* For find/delete, host was found */
1030: int invalid; /* File contained invalid items; don't delete */
1.256 djm 1031: };
1032:
1033: static int
1034: known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
1.119 djm 1035: {
1.256 djm 1036: struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1037: char *hashed, *cp, *hosts, *ohosts;
1038: int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
1039:
1.262 djm 1040: switch (l->status) {
1041: case HKF_STATUS_OK:
1042: case HKF_STATUS_MATCHED:
1043: /*
1044: * Don't hash hosts already already hashed, with wildcard
1045: * characters or a CA/revocation marker.
1046: */
1047: if ((l->match & HKF_MATCH_HOST_HASHED) != 0 ||
1048: has_wild || l->marker != MRK_NONE) {
1049: fprintf(ctx->out, "%s\n", l->line);
1050: if (has_wild && !find_host) {
1051: fprintf(stderr, "%s:%ld: ignoring host name "
1052: "with wildcard: %.64s\n", l->path,
1053: l->linenum, l->hosts);
1054: }
1055: return 0;
1056: }
1057: /*
1058: * Split any comma-separated hostnames from the host list,
1059: * hash and store separately.
1060: */
1061: ohosts = hosts = xstrdup(l->hosts);
1062: while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
1063: if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1064: fatal("hash_host failed");
1065: fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1066: ctx->has_unhashed = 1;
1067: }
1068: free(ohosts);
1069: return 0;
1070: case HKF_STATUS_INVALID:
1071: /* Retain invalid lines, but mark file as invalid. */
1.256 djm 1072: ctx->invalid = 1;
1073: fprintf(stderr, "%s:%ld: invalid line\n", l->path, l->linenum);
1.262 djm 1074: /* FALLTHROUGH */
1075: default:
1.256 djm 1076: fprintf(ctx->out, "%s\n", l->line);
1077: return 0;
1078: }
1.262 djm 1079: /* NOTREACHED */
1080: return -1;
1.256 djm 1081: }
1082:
1083: static int
1084: known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1085: {
1086: struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1087:
1.262 djm 1088: if (l->status == HKF_STATUS_MATCHED) {
1.256 djm 1089: if (delete_host) {
1090: if (l->marker != MRK_NONE) {
1091: /* Don't remove CA and revocation lines */
1092: fprintf(ctx->out, "%s\n", l->line);
1093: } else {
1094: /*
1095: * Hostname matches and has no CA/revoke
1096: * marker, delete it by *not* writing the
1097: * line to ctx->out.
1098: */
1099: ctx->found_key = 1;
1100: if (!quiet)
1101: printf("# Host %s found: line %ld\n",
1102: ctx->host, l->linenum);
1103: }
1104: return 0;
1105: } else if (find_host) {
1106: ctx->found_key = 1;
1107: if (!quiet) {
1108: printf("# Host %s found: line %ld %s\n",
1109: ctx->host,
1110: l->linenum, l->marker == MRK_CA ? "CA" :
1111: (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1112: }
1113: if (hash_hosts)
1114: known_hosts_hash(l, ctx);
1115: else
1116: fprintf(ctx->out, "%s\n", l->line);
1117: return 0;
1118: }
1119: } else if (delete_host) {
1120: /* Retain non-matching hosts when deleting */
1121: if (l->status == HKF_STATUS_INVALID) {
1122: ctx->invalid = 1;
1123: fprintf(stderr, "%s:%ld: invalid line\n",
1124: l->path, l->linenum);
1125: }
1126: fprintf(ctx->out, "%s\n", l->line);
1.166 djm 1127: }
1.256 djm 1128: return 0;
1.119 djm 1129: }
1130:
1131: static void
1132: do_known_hosts(struct passwd *pw, const char *name)
1133: {
1.258 deraadt 1134: char *cp, tmp[PATH_MAX], old[PATH_MAX];
1.257 djm 1135: int r, fd, oerrno, inplace = 0;
1.256 djm 1136: struct known_hosts_ctx ctx;
1.119 djm 1137:
1138: if (!have_identity) {
1139: cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1140: if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1141: sizeof(identity_file))
1142: fatal("Specified known hosts path too long");
1.227 djm 1143: free(cp);
1.119 djm 1144: have_identity = 1;
1145: }
1146:
1.256 djm 1147: memset(&ctx, 0, sizeof(ctx));
1148: ctx.out = stdout;
1149: ctx.host = name;
1150:
1.119 djm 1151: /*
1152: * Find hosts goes to stdout, hash and deletions happen in-place
1153: * A corner case is ssh-keygen -HF foo, which should go to stdout
1154: */
1155: if (!find_host && (hash_hosts || delete_host)) {
1156: if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1157: strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1158: strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1159: strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1160: fatal("known_hosts path too long");
1161: umask(077);
1.256 djm 1162: if ((fd = mkstemp(tmp)) == -1)
1.119 djm 1163: fatal("mkstemp: %s", strerror(errno));
1.256 djm 1164: if ((ctx.out = fdopen(fd, "w")) == NULL) {
1165: oerrno = errno;
1.119 djm 1166: unlink(tmp);
1.256 djm 1167: fatal("fdopen: %s", strerror(oerrno));
1.119 djm 1168: }
1.257 djm 1169: inplace = 1;
1.119 djm 1170: }
1171:
1.256 djm 1172: /* XXX support identity_file == "-" for stdin */
1173: if ((r = hostkeys_foreach(identity_file,
1174: hash_hosts ? known_hosts_hash : known_hosts_find_delete, &ctx,
1.262 djm 1175: name, NULL, find_host ? HKF_WANT_MATCH : 0)) != 0)
1.256 djm 1176: fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
1.119 djm 1177:
1.257 djm 1178: if (inplace)
1.256 djm 1179: fclose(ctx.out);
1.119 djm 1180:
1.256 djm 1181: if (ctx.invalid) {
1.165 djm 1182: fprintf(stderr, "%s is not a valid known_hosts file.\n",
1.119 djm 1183: identity_file);
1.257 djm 1184: if (inplace) {
1.119 djm 1185: fprintf(stderr, "Not replacing existing known_hosts "
1.122 markus 1186: "file because of errors\n");
1.119 djm 1187: unlink(tmp);
1188: }
1189: exit(1);
1.256 djm 1190: } else if (delete_host && !ctx.found_key) {
1191: fprintf(stderr, "Host %s not found in %s\n",
1192: name, identity_file);
1193: unlink(tmp);
1.257 djm 1194: } else if (inplace) {
1.119 djm 1195: /* Backup existing file */
1196: if (unlink(old) == -1 && errno != ENOENT)
1197: fatal("unlink %.100s: %s", old, strerror(errno));
1198: if (link(identity_file, old) == -1)
1199: fatal("link %.100s to %.100s: %s", identity_file, old,
1200: strerror(errno));
1201: /* Move new one into place */
1202: if (rename(tmp, identity_file) == -1) {
1203: error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1204: strerror(errno));
1205: unlink(tmp);
1206: unlink(old);
1207: exit(1);
1208: }
1209:
1210: fprintf(stderr, "%s updated.\n", identity_file);
1211: fprintf(stderr, "Original contents retained as %s\n", old);
1.256 djm 1212: if (ctx.has_unhashed) {
1.119 djm 1213: fprintf(stderr, "WARNING: %s contains unhashed "
1214: "entries\n", old);
1215: fprintf(stderr, "Delete this file to ensure privacy "
1.128 djm 1216: "of hostnames\n");
1.119 djm 1217: }
1218: }
1219:
1.256 djm 1220: exit (find_host && !ctx.found_key);
1.119 djm 1221: }
1222:
1.13 deraadt 1223: /*
1224: * Perform changing a passphrase. The argument is the passwd structure
1225: * for the current user.
1226: */
1.63 itojun 1227: static void
1.7 markus 1228: do_change_passphrase(struct passwd *pw)
1229: {
1.12 markus 1230: char *comment;
1231: char *old_passphrase, *passphrase1, *passphrase2;
1232: struct stat st;
1.252 djm 1233: struct sshkey *private;
1234: int r;
1.12 markus 1235:
1236: if (!have_identity)
1237: ask_filename(pw, "Enter file in which the key is");
1238: if (stat(identity_file, &st) < 0) {
1239: perror(identity_file);
1240: exit(1);
1241: }
1242: /* Try to load the file with empty passphrase. */
1.252 djm 1243: r = sshkey_load_private(identity_file, "", &private, &comment);
1244: if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.12 markus 1245: if (identity_passphrase)
1246: old_passphrase = xstrdup(identity_passphrase);
1247: else
1.65 markus 1248: old_passphrase =
1249: read_passphrase("Enter old passphrase: ",
1250: RP_ALLOW_STDIN);
1.252 djm 1251: r = sshkey_load_private(identity_file, old_passphrase,
1252: &private, &comment);
1.240 djm 1253: explicit_bzero(old_passphrase, strlen(old_passphrase));
1.227 djm 1254: free(old_passphrase);
1.252 djm 1255: if (r != 0)
1256: goto badkey;
1257: } else if (r != 0) {
1258: badkey:
1259: fprintf(stderr, "Failed to load key \"%s\": %s\n",
1260: identity_file, ssh_err(r));
1261: exit(1);
1.12 markus 1262: }
1.266 djm 1263: if (comment)
1264: printf("Key has comment '%s'\n", comment);
1.12 markus 1265:
1266: /* Ask the new passphrase (twice). */
1267: if (identity_new_passphrase) {
1268: passphrase1 = xstrdup(identity_new_passphrase);
1269: passphrase2 = NULL;
1270: } else {
1271: passphrase1 =
1.65 markus 1272: read_passphrase("Enter new passphrase (empty for no "
1273: "passphrase): ", RP_ALLOW_STDIN);
1274: passphrase2 = read_passphrase("Enter same passphrase again: ",
1.86 deraadt 1275: RP_ALLOW_STDIN);
1.12 markus 1276:
1277: /* Verify that they are the same. */
1278: if (strcmp(passphrase1, passphrase2) != 0) {
1.240 djm 1279: explicit_bzero(passphrase1, strlen(passphrase1));
1280: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 1281: free(passphrase1);
1282: free(passphrase2);
1.12 markus 1283: printf("Pass phrases do not match. Try again.\n");
1284: exit(1);
1285: }
1286: /* Destroy the other copy. */
1.240 djm 1287: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 1288: free(passphrase2);
1.12 markus 1289: }
1290:
1291: /* Save the file using the new passphrase. */
1.252 djm 1292: if ((r = sshkey_save_private(private, identity_file, passphrase1,
1293: comment, use_new_format, new_format_cipher, rounds)) != 0) {
1294: printf("Saving key \"%s\" failed: %s.\n",
1295: identity_file, ssh_err(r));
1.240 djm 1296: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 1297: free(passphrase1);
1.252 djm 1298: sshkey_free(private);
1.227 djm 1299: free(comment);
1.12 markus 1300: exit(1);
1301: }
1302: /* Destroy the passphrase and the copy of the key in memory. */
1.240 djm 1303: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 1304: free(passphrase1);
1.252 djm 1305: sshkey_free(private); /* Destroys contents */
1.227 djm 1306: free(comment);
1.1 deraadt 1307:
1.12 markus 1308: printf("Your identification has been saved with the new passphrase.\n");
1309: exit(0);
1.1 deraadt 1310: }
1311:
1.105 jakob 1312: /*
1313: * Print the SSHFP RR.
1314: */
1.138 jakob 1315: static int
1316: do_print_resource_record(struct passwd *pw, char *fname, char *hname)
1.105 jakob 1317: {
1.252 djm 1318: struct sshkey *public;
1.105 jakob 1319: char *comment = NULL;
1320: struct stat st;
1.252 djm 1321: int r;
1.105 jakob 1322:
1.138 jakob 1323: if (fname == NULL)
1.229 djm 1324: fatal("%s: no filename", __func__);
1.138 jakob 1325: if (stat(fname, &st) < 0) {
1326: if (errno == ENOENT)
1327: return 0;
1328: perror(fname);
1.105 jakob 1329: exit(1);
1330: }
1.252 djm 1331: if ((r = sshkey_load_public(fname, &public, &comment)) != 0) {
1332: printf("Failed to read v2 public key from \"%s\": %s.\n",
1333: fname, ssh_err(r));
1334: exit(1);
1.105 jakob 1335: }
1.252 djm 1336: export_dns_rr(hname, public, stdout, print_generic);
1337: sshkey_free(public);
1338: free(comment);
1339: return 1;
1.105 jakob 1340: }
1341:
1.13 deraadt 1342: /*
1343: * Change the comment of a private key file.
1344: */
1.63 itojun 1345: static void
1.2 provos 1346: do_change_comment(struct passwd *pw)
1.1 deraadt 1347: {
1.46 deraadt 1348: char new_comment[1024], *comment, *passphrase;
1.252 djm 1349: struct sshkey *private;
1350: struct sshkey *public;
1.12 markus 1351: struct stat st;
1352: FILE *f;
1.252 djm 1353: int r, fd;
1.12 markus 1354:
1355: if (!have_identity)
1356: ask_filename(pw, "Enter file in which the key is");
1357: if (stat(identity_file, &st) < 0) {
1358: perror(identity_file);
1359: exit(1);
1360: }
1.252 djm 1361: if ((r = sshkey_load_private(identity_file, "",
1362: &private, &comment)) == 0)
1363: passphrase = xstrdup("");
1364: else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1365: printf("Cannot load private key \"%s\": %s.\n",
1366: identity_file, ssh_err(r));
1367: exit(1);
1368: } else {
1.12 markus 1369: if (identity_passphrase)
1370: passphrase = xstrdup(identity_passphrase);
1371: else if (identity_new_passphrase)
1372: passphrase = xstrdup(identity_new_passphrase);
1373: else
1.65 markus 1374: passphrase = read_passphrase("Enter passphrase: ",
1375: RP_ALLOW_STDIN);
1.12 markus 1376: /* Try to load using the passphrase. */
1.252 djm 1377: if ((r = sshkey_load_private(identity_file, passphrase,
1378: &private, &comment)) != 0) {
1.240 djm 1379: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1380: free(passphrase);
1.252 djm 1381: printf("Cannot load private key \"%s\": %s.\n",
1382: identity_file, ssh_err(r));
1.12 markus 1383: exit(1);
1384: }
1385: }
1.52 markus 1386: if (private->type != KEY_RSA1) {
1387: fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1.268 ! tobias 1388: explicit_bzero(passphrase, strlen(passphrase));
1.252 djm 1389: sshkey_free(private);
1.52 markus 1390: exit(1);
1.86 deraadt 1391: }
1.12 markus 1392: printf("Key now has comment '%s'\n", comment);
1393:
1394: if (identity_comment) {
1395: strlcpy(new_comment, identity_comment, sizeof(new_comment));
1396: } else {
1397: printf("Enter new comment: ");
1398: fflush(stdout);
1399: if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1.240 djm 1400: explicit_bzero(passphrase, strlen(passphrase));
1.252 djm 1401: sshkey_free(private);
1.12 markus 1402: exit(1);
1403: }
1.162 gilles 1404: new_comment[strcspn(new_comment, "\n")] = '\0';
1.12 markus 1405: }
1406:
1407: /* Save the file using the new passphrase. */
1.252 djm 1408: if ((r = sshkey_save_private(private, identity_file, passphrase,
1409: new_comment, use_new_format, new_format_cipher, rounds)) != 0) {
1410: printf("Saving key \"%s\" failed: %s\n",
1411: identity_file, ssh_err(r));
1.240 djm 1412: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1413: free(passphrase);
1.252 djm 1414: sshkey_free(private);
1.227 djm 1415: free(comment);
1.12 markus 1416: exit(1);
1417: }
1.240 djm 1418: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1419: free(passphrase);
1.252 djm 1420: if ((r = sshkey_from_private(private, &public)) != 0)
1421: fatal("key_from_private failed: %s", ssh_err(r));
1422: sshkey_free(private);
1.12 markus 1423:
1424: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 1425: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1426: if (fd == -1) {
1.12 markus 1427: printf("Could not save your public key in %s\n", identity_file);
1428: exit(1);
1429: }
1.46 deraadt 1430: f = fdopen(fd, "w");
1431: if (f == NULL) {
1.173 tobias 1432: printf("fdopen %s failed\n", identity_file);
1.46 deraadt 1433: exit(1);
1434: }
1.252 djm 1435: if ((r = sshkey_write(public, f)) != 0)
1436: fprintf(stderr, "write key failed: %s\n", ssh_err(r));
1437: sshkey_free(public);
1.19 markus 1438: fprintf(f, " %s\n", new_comment);
1.12 markus 1439: fclose(f);
1.1 deraadt 1440:
1.227 djm 1441: free(comment);
1.1 deraadt 1442:
1.12 markus 1443: printf("The comment in your key file has been changed.\n");
1444: exit(0);
1.1 deraadt 1445: }
1446:
1.179 djm 1447: static const char *
1.182 djm 1448: fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
1.179 djm 1449: {
1450: char from[32], to[32];
1451: static char ret[64];
1452: time_t tt;
1453: struct tm *tm;
1454:
1455: *from = *to = '\0';
1.182 djm 1456: if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
1.179 djm 1457: return "forever";
1458:
1.182 djm 1459: if (valid_from != 0) {
1.179 djm 1460: /* XXX revisit INT_MAX in 2038 :) */
1.182 djm 1461: tt = valid_from > INT_MAX ? INT_MAX : valid_from;
1.179 djm 1462: tm = localtime(&tt);
1463: strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1464: }
1.182 djm 1465: if (valid_to != 0xffffffffffffffffULL) {
1.179 djm 1466: /* XXX revisit INT_MAX in 2038 :) */
1.182 djm 1467: tt = valid_to > INT_MAX ? INT_MAX : valid_to;
1.179 djm 1468: tm = localtime(&tt);
1469: strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1470: }
1471:
1.182 djm 1472: if (valid_from == 0) {
1.179 djm 1473: snprintf(ret, sizeof(ret), "before %s", to);
1474: return ret;
1475: }
1.182 djm 1476: if (valid_to == 0xffffffffffffffffULL) {
1.179 djm 1477: snprintf(ret, sizeof(ret), "after %s", from);
1478: return ret;
1479: }
1480:
1481: snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1482: return ret;
1483: }
1484:
1485: static void
1.252 djm 1486: add_flag_option(struct sshbuf *c, const char *name)
1.179 djm 1487: {
1.252 djm 1488: int r;
1489:
1.179 djm 1490: debug3("%s: %s", __func__, name);
1.252 djm 1491: if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1492: (r = sshbuf_put_string(c, NULL, 0)) != 0)
1493: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.179 djm 1494: }
1495:
1496: static void
1.252 djm 1497: add_string_option(struct sshbuf *c, const char *name, const char *value)
1.179 djm 1498: {
1.252 djm 1499: struct sshbuf *b;
1500: int r;
1.179 djm 1501:
1502: debug3("%s: %s=%s", __func__, name, value);
1.252 djm 1503: if ((b = sshbuf_new()) == NULL)
1504: fatal("%s: sshbuf_new failed", __func__);
1505: if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1506: (r = sshbuf_put_cstring(c, name)) != 0 ||
1507: (r = sshbuf_put_stringb(c, b)) != 0)
1508: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.179 djm 1509:
1.252 djm 1510: sshbuf_free(b);
1.179 djm 1511: }
1512:
1.190 djm 1513: #define OPTIONS_CRITICAL 1
1514: #define OPTIONS_EXTENSIONS 2
1.179 djm 1515: static void
1.252 djm 1516: prepare_options_buf(struct sshbuf *c, int which)
1.179 djm 1517: {
1.252 djm 1518: sshbuf_reset(c);
1.196 djm 1519: if ((which & OPTIONS_CRITICAL) != 0 &&
1520: certflags_command != NULL)
1521: add_string_option(c, "force-command", certflags_command);
1.190 djm 1522: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1.210 djm 1523: (certflags_flags & CERTOPT_X_FWD) != 0)
1524: add_flag_option(c, "permit-X11-forwarding");
1525: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1.190 djm 1526: (certflags_flags & CERTOPT_AGENT_FWD) != 0)
1.186 djm 1527: add_flag_option(c, "permit-agent-forwarding");
1.190 djm 1528: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1529: (certflags_flags & CERTOPT_PORT_FWD) != 0)
1.186 djm 1530: add_flag_option(c, "permit-port-forwarding");
1.190 djm 1531: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1532: (certflags_flags & CERTOPT_PTY) != 0)
1.186 djm 1533: add_flag_option(c, "permit-pty");
1.190 djm 1534: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1535: (certflags_flags & CERTOPT_USER_RC) != 0)
1.186 djm 1536: add_flag_option(c, "permit-user-rc");
1.190 djm 1537: if ((which & OPTIONS_CRITICAL) != 0 &&
1538: certflags_src_addr != NULL)
1539: add_string_option(c, "source-address", certflags_src_addr);
1.179 djm 1540: }
1541:
1.252 djm 1542: static struct sshkey *
1.197 djm 1543: load_pkcs11_key(char *path)
1544: {
1545: #ifdef ENABLE_PKCS11
1.252 djm 1546: struct sshkey **keys = NULL, *public, *private = NULL;
1547: int r, i, nkeys;
1.197 djm 1548:
1.252 djm 1549: if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1550: fatal("Couldn't load CA public key \"%s\": %s",
1551: path, ssh_err(r));
1.197 djm 1552:
1553: nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1554: debug3("%s: %d keys", __func__, nkeys);
1555: if (nkeys <= 0)
1556: fatal("cannot read public key from pkcs11");
1557: for (i = 0; i < nkeys; i++) {
1.252 djm 1558: if (sshkey_equal_public(public, keys[i])) {
1.197 djm 1559: private = keys[i];
1560: continue;
1561: }
1.252 djm 1562: sshkey_free(keys[i]);
1.197 djm 1563: }
1.227 djm 1564: free(keys);
1.252 djm 1565: sshkey_free(public);
1.197 djm 1566: return private;
1567: #else
1568: fatal("no pkcs11 support");
1569: #endif /* ENABLE_PKCS11 */
1570: }
1571:
1.179 djm 1572: static void
1573: do_ca_sign(struct passwd *pw, int argc, char **argv)
1574: {
1.252 djm 1575: int r, i, fd;
1.179 djm 1576: u_int n;
1.252 djm 1577: struct sshkey *ca, *public;
1.179 djm 1578: char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1579: FILE *f;
1.186 djm 1580: int v00 = 0; /* legacy keys */
1.179 djm 1581:
1.186 djm 1582: if (key_type_name != NULL) {
1.252 djm 1583: switch (sshkey_type_from_name(key_type_name)) {
1.186 djm 1584: case KEY_RSA_CERT_V00:
1585: case KEY_DSA_CERT_V00:
1586: v00 = 1;
1587: break;
1588: case KEY_UNSPEC:
1589: if (strcasecmp(key_type_name, "v00") == 0) {
1590: v00 = 1;
1591: break;
1592: } else if (strcasecmp(key_type_name, "v01") == 0)
1593: break;
1594: /* FALLTHROUGH */
1595: default:
1596: fprintf(stderr, "unknown key type %s\n", key_type_name);
1597: exit(1);
1598: }
1599: }
1600:
1.246 markus 1601: #ifdef ENABLE_PKCS11
1.197 djm 1602: pkcs11_init(1);
1.246 markus 1603: #endif
1.197 djm 1604: tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1605: if (pkcs11provider != NULL) {
1606: if ((ca = load_pkcs11_key(tmp)) == NULL)
1607: fatal("No PKCS#11 key matching %s found", ca_key_path);
1.252 djm 1608: } else
1609: ca = load_identity(tmp);
1.227 djm 1610: free(tmp);
1.197 djm 1611:
1.179 djm 1612: for (i = 0; i < argc; i++) {
1613: /* Split list of principals */
1614: n = 0;
1615: if (cert_principals != NULL) {
1616: otmp = tmp = xstrdup(cert_principals);
1617: plist = NULL;
1618: for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1619: plist = xrealloc(plist, n + 1, sizeof(*plist));
1620: if (*(plist[n] = xstrdup(cp)) == '\0')
1621: fatal("Empty principal name");
1622: }
1.227 djm 1623: free(otmp);
1.179 djm 1624: }
1625:
1626: tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1.252 djm 1627: if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1628: fatal("%s: unable to open \"%s\": %s",
1629: __func__, tmp, ssh_err(r));
1.200 djm 1630: if (public->type != KEY_RSA && public->type != KEY_DSA &&
1.238 markus 1631: public->type != KEY_ECDSA && public->type != KEY_ED25519)
1.179 djm 1632: fatal("%s: key \"%s\" type %s cannot be certified",
1.252 djm 1633: __func__, tmp, sshkey_type(public));
1.179 djm 1634:
1635: /* Prepare certificate to sign */
1.252 djm 1636: if ((r = sshkey_to_certified(public, v00)) != 0)
1637: fatal("Could not upgrade key %s to certificate: %s",
1638: tmp, ssh_err(r));
1.179 djm 1639: public->cert->type = cert_key_type;
1.186 djm 1640: public->cert->serial = (u_int64_t)cert_serial;
1.179 djm 1641: public->cert->key_id = xstrdup(cert_key_id);
1642: public->cert->nprincipals = n;
1643: public->cert->principals = plist;
1644: public->cert->valid_after = cert_valid_from;
1645: public->cert->valid_before = cert_valid_to;
1.190 djm 1646: if (v00) {
1.247 djm 1647: prepare_options_buf(public->cert->critical,
1.190 djm 1648: OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1649: } else {
1.247 djm 1650: prepare_options_buf(public->cert->critical,
1.190 djm 1651: OPTIONS_CRITICAL);
1.247 djm 1652: prepare_options_buf(public->cert->extensions,
1.190 djm 1653: OPTIONS_EXTENSIONS);
1654: }
1.252 djm 1655: if ((r = sshkey_from_private(ca,
1656: &public->cert->signature_key)) != 0)
1657: fatal("key_from_private (ca key): %s", ssh_err(r));
1.179 djm 1658:
1.252 djm 1659: if (sshkey_certify(public, ca) != 0)
1.179 djm 1660: fatal("Couldn't not certify key %s", tmp);
1661:
1662: if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1663: *cp = '\0';
1664: xasprintf(&out, "%s-cert.pub", tmp);
1.227 djm 1665: free(tmp);
1.179 djm 1666:
1667: if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1668: fatal("Could not open \"%s\" for writing: %s", out,
1669: strerror(errno));
1670: if ((f = fdopen(fd, "w")) == NULL)
1671: fatal("%s: fdopen: %s", __func__, strerror(errno));
1.252 djm 1672: if ((r = sshkey_write(public, f)) != 0)
1673: fatal("Could not write certified key to %s: %s",
1674: out, ssh_err(r));
1.179 djm 1675: fprintf(f, " %s\n", comment);
1676: fclose(f);
1677:
1.186 djm 1678: if (!quiet) {
1679: logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1.252 djm 1680: "valid %s", sshkey_cert_type(public),
1.205 djm 1681: out, public->cert->key_id,
1682: (unsigned long long)public->cert->serial,
1.179 djm 1683: cert_principals != NULL ? " for " : "",
1684: cert_principals != NULL ? cert_principals : "",
1.182 djm 1685: fmt_validity(cert_valid_from, cert_valid_to));
1.186 djm 1686: }
1.179 djm 1687:
1.252 djm 1688: sshkey_free(public);
1.227 djm 1689: free(out);
1.179 djm 1690: }
1.246 markus 1691: #ifdef ENABLE_PKCS11
1.197 djm 1692: pkcs11_terminate();
1.246 markus 1693: #endif
1.179 djm 1694: exit(0);
1695: }
1696:
1697: static u_int64_t
1698: parse_relative_time(const char *s, time_t now)
1699: {
1700: int64_t mul, secs;
1701:
1702: mul = *s == '-' ? -1 : 1;
1703:
1704: if ((secs = convtime(s + 1)) == -1)
1705: fatal("Invalid relative certificate time %s", s);
1706: if (mul == -1 && secs > now)
1707: fatal("Certificate time %s cannot be represented", s);
1708: return now + (u_int64_t)(secs * mul);
1709: }
1710:
1711: static u_int64_t
1712: parse_absolute_time(const char *s)
1713: {
1714: struct tm tm;
1715: time_t tt;
1.180 djm 1716: char buf[32], *fmt;
1.179 djm 1717:
1.180 djm 1718: /*
1719: * POSIX strptime says "The application shall ensure that there
1720: * is white-space or other non-alphanumeric characters between
1721: * any two conversion specifications" so arrange things this way.
1722: */
1723: switch (strlen(s)) {
1724: case 8:
1.184 djm 1725: fmt = "%Y-%m-%d";
1726: snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
1.180 djm 1727: break;
1728: case 14:
1.184 djm 1729: fmt = "%Y-%m-%dT%H:%M:%S";
1730: snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
1.180 djm 1731: s, s + 4, s + 6, s + 8, s + 10, s + 12);
1732: break;
1733: default:
1.179 djm 1734: fatal("Invalid certificate time format %s", s);
1.180 djm 1735: }
1.179 djm 1736:
1.239 tedu 1737: memset(&tm, 0, sizeof(tm));
1.180 djm 1738: if (strptime(buf, fmt, &tm) == NULL)
1.179 djm 1739: fatal("Invalid certificate time %s", s);
1740: if ((tt = mktime(&tm)) < 0)
1741: fatal("Certificate time %s cannot be represented", s);
1742: return (u_int64_t)tt;
1743: }
1744:
1745: static void
1746: parse_cert_times(char *timespec)
1747: {
1748: char *from, *to;
1749: time_t now = time(NULL);
1750: int64_t secs;
1751:
1752: /* +timespec relative to now */
1753: if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1754: if ((secs = convtime(timespec + 1)) == -1)
1755: fatal("Invalid relative certificate life %s", timespec);
1756: cert_valid_to = now + secs;
1757: /*
1758: * Backdate certificate one minute to avoid problems on hosts
1759: * with poorly-synchronised clocks.
1760: */
1761: cert_valid_from = ((now - 59)/ 60) * 60;
1762: return;
1763: }
1764:
1765: /*
1766: * from:to, where
1767: * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1768: * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1769: */
1770: from = xstrdup(timespec);
1771: to = strchr(from, ':');
1772: if (to == NULL || from == to || *(to + 1) == '\0')
1.181 djm 1773: fatal("Invalid certificate life specification %s", timespec);
1.179 djm 1774: *to++ = '\0';
1775:
1776: if (*from == '-' || *from == '+')
1777: cert_valid_from = parse_relative_time(from, now);
1778: else
1779: cert_valid_from = parse_absolute_time(from);
1780:
1781: if (*to == '-' || *to == '+')
1.235 djm 1782: cert_valid_to = parse_relative_time(to, now);
1.179 djm 1783: else
1784: cert_valid_to = parse_absolute_time(to);
1785:
1786: if (cert_valid_to <= cert_valid_from)
1787: fatal("Empty certificate validity interval");
1.227 djm 1788: free(from);
1.179 djm 1789: }
1790:
1791: static void
1.186 djm 1792: add_cert_option(char *opt)
1.179 djm 1793: {
1794: char *val;
1795:
1.208 stevesk 1796: if (strcasecmp(opt, "clear") == 0)
1.190 djm 1797: certflags_flags = 0;
1.179 djm 1798: else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1.190 djm 1799: certflags_flags &= ~CERTOPT_X_FWD;
1.179 djm 1800: else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1.190 djm 1801: certflags_flags |= CERTOPT_X_FWD;
1.179 djm 1802: else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1.190 djm 1803: certflags_flags &= ~CERTOPT_AGENT_FWD;
1.179 djm 1804: else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1.190 djm 1805: certflags_flags |= CERTOPT_AGENT_FWD;
1.179 djm 1806: else if (strcasecmp(opt, "no-port-forwarding") == 0)
1.190 djm 1807: certflags_flags &= ~CERTOPT_PORT_FWD;
1.179 djm 1808: else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1.190 djm 1809: certflags_flags |= CERTOPT_PORT_FWD;
1.179 djm 1810: else if (strcasecmp(opt, "no-pty") == 0)
1.190 djm 1811: certflags_flags &= ~CERTOPT_PTY;
1.179 djm 1812: else if (strcasecmp(opt, "permit-pty") == 0)
1.190 djm 1813: certflags_flags |= CERTOPT_PTY;
1.179 djm 1814: else if (strcasecmp(opt, "no-user-rc") == 0)
1.190 djm 1815: certflags_flags &= ~CERTOPT_USER_RC;
1.179 djm 1816: else if (strcasecmp(opt, "permit-user-rc") == 0)
1.190 djm 1817: certflags_flags |= CERTOPT_USER_RC;
1.179 djm 1818: else if (strncasecmp(opt, "force-command=", 14) == 0) {
1819: val = opt + 14;
1820: if (*val == '\0')
1.186 djm 1821: fatal("Empty force-command option");
1.190 djm 1822: if (certflags_command != NULL)
1.179 djm 1823: fatal("force-command already specified");
1.190 djm 1824: certflags_command = xstrdup(val);
1.179 djm 1825: } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1826: val = opt + 15;
1827: if (*val == '\0')
1.186 djm 1828: fatal("Empty source-address option");
1.190 djm 1829: if (certflags_src_addr != NULL)
1.179 djm 1830: fatal("source-address already specified");
1831: if (addr_match_cidr_list(NULL, val) != 0)
1832: fatal("Invalid source-address list");
1.190 djm 1833: certflags_src_addr = xstrdup(val);
1.179 djm 1834: } else
1.186 djm 1835: fatal("Unsupported certificate option \"%s\"", opt);
1.179 djm 1836: }
1837:
1.63 itojun 1838: static void
1.252 djm 1839: show_options(struct sshbuf *optbuf, int v00, int in_critical)
1.192 djm 1840: {
1.245 djm 1841: char *name, *arg;
1.252 djm 1842: struct sshbuf *options, *option = NULL;
1843: int r;
1844:
1845: if ((options = sshbuf_fromb(optbuf)) == NULL)
1846: fatal("%s: sshbuf_fromb failed", __func__);
1847: while (sshbuf_len(options) != 0) {
1848: sshbuf_free(option);
1849: option = NULL;
1850: if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
1851: (r = sshbuf_froms(options, &option)) != 0)
1852: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.192 djm 1853: printf(" %s", name);
1854: if ((v00 || !in_critical) &&
1855: (strcmp(name, "permit-X11-forwarding") == 0 ||
1856: strcmp(name, "permit-agent-forwarding") == 0 ||
1857: strcmp(name, "permit-port-forwarding") == 0 ||
1858: strcmp(name, "permit-pty") == 0 ||
1859: strcmp(name, "permit-user-rc") == 0))
1860: printf("\n");
1861: else if ((v00 || in_critical) &&
1862: (strcmp(name, "force-command") == 0 ||
1863: strcmp(name, "source-address") == 0)) {
1.252 djm 1864: if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
1865: fatal("%s: buffer error: %s",
1866: __func__, ssh_err(r));
1.245 djm 1867: printf(" %s\n", arg);
1868: free(arg);
1.192 djm 1869: } else {
1.252 djm 1870: printf(" UNKNOWN OPTION (len %zu)\n",
1871: sshbuf_len(option));
1872: sshbuf_reset(option);
1.192 djm 1873: }
1.227 djm 1874: free(name);
1.252 djm 1875: if (sshbuf_len(option) != 0)
1.192 djm 1876: fatal("Option corrupt: extra data at end");
1877: }
1.252 djm 1878: sshbuf_free(option);
1879: sshbuf_free(options);
1.192 djm 1880: }
1881:
1882: static void
1.182 djm 1883: do_show_cert(struct passwd *pw)
1884: {
1.252 djm 1885: struct sshkey *key;
1.182 djm 1886: struct stat st;
1887: char *key_fp, *ca_fp;
1.192 djm 1888: u_int i, v00;
1.252 djm 1889: int r;
1.182 djm 1890:
1891: if (!have_identity)
1892: ask_filename(pw, "Enter file in which the key is");
1.191 djm 1893: if (stat(identity_file, &st) < 0)
1894: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.252 djm 1895: if ((r = sshkey_load_public(identity_file, &key, NULL)) != 0)
1896: fatal("Cannot load public key \"%s\": %s",
1897: identity_file, ssh_err(r));
1898: if (!sshkey_is_cert(key))
1.182 djm 1899: fatal("%s is not a certificate", identity_file);
1.186 djm 1900: v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1901:
1.252 djm 1902: key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
1903: ca_fp = sshkey_fingerprint(key->cert->signature_key,
1.251 djm 1904: fingerprint_hash, SSH_FP_DEFAULT);
1.259 djm 1905: if (key_fp == NULL || ca_fp == NULL)
1906: fatal("%s: sshkey_fingerprint fail", __func__);
1.182 djm 1907:
1908: printf("%s:\n", identity_file);
1.252 djm 1909: printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
1910: sshkey_cert_type(key));
1911: printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
1.186 djm 1912: printf(" Signing CA: %s %s\n",
1.252 djm 1913: sshkey_type(key->cert->signature_key), ca_fp);
1.186 djm 1914: printf(" Key ID: \"%s\"\n", key->cert->key_id);
1.205 djm 1915: if (!v00) {
1916: printf(" Serial: %llu\n",
1917: (unsigned long long)key->cert->serial);
1918: }
1.182 djm 1919: printf(" Valid: %s\n",
1920: fmt_validity(key->cert->valid_after, key->cert->valid_before));
1921: printf(" Principals: ");
1922: if (key->cert->nprincipals == 0)
1923: printf("(none)\n");
1924: else {
1925: for (i = 0; i < key->cert->nprincipals; i++)
1926: printf("\n %s",
1927: key->cert->principals[i]);
1928: printf("\n");
1929: }
1.186 djm 1930: printf(" Critical Options: ");
1.252 djm 1931: if (sshbuf_len(key->cert->critical) == 0)
1.182 djm 1932: printf("(none)\n");
1933: else {
1934: printf("\n");
1.247 djm 1935: show_options(key->cert->critical, v00, 1);
1.186 djm 1936: }
1937: if (!v00) {
1938: printf(" Extensions: ");
1.252 djm 1939: if (sshbuf_len(key->cert->extensions) == 0)
1.186 djm 1940: printf("(none)\n");
1941: else {
1942: printf("\n");
1.247 djm 1943: show_options(key->cert->extensions, v00, 0);
1.182 djm 1944: }
1945: }
1946: exit(0);
1947: }
1948:
1.246 markus 1949: #ifdef WITH_OPENSSL
1.182 djm 1950: static void
1.223 djm 1951: load_krl(const char *path, struct ssh_krl **krlp)
1952: {
1.252 djm 1953: struct sshbuf *krlbuf;
1954: int r, fd;
1.223 djm 1955:
1.252 djm 1956: if ((krlbuf = sshbuf_new()) == NULL)
1957: fatal("sshbuf_new failed");
1.223 djm 1958: if ((fd = open(path, O_RDONLY)) == -1)
1959: fatal("open %s: %s", path, strerror(errno));
1.252 djm 1960: if ((r = sshkey_load_file(fd, krlbuf)) != 0)
1961: fatal("Unable to load KRL: %s", ssh_err(r));
1.223 djm 1962: close(fd);
1963: /* XXX check sigs */
1.252 djm 1964: if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
1.223 djm 1965: *krlp == NULL)
1.252 djm 1966: fatal("Invalid KRL file: %s", ssh_err(r));
1967: sshbuf_free(krlbuf);
1.223 djm 1968: }
1969:
1970: static void
1.261 djm 1971: update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
1.252 djm 1972: const struct sshkey *ca, struct ssh_krl *krl)
1.223 djm 1973: {
1.252 djm 1974: struct sshkey *key = NULL;
1.223 djm 1975: u_long lnum = 0;
1976: char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES];
1977: unsigned long long serial, serial2;
1978: int i, was_explicit_key, was_sha1, r;
1979: FILE *krl_spec;
1980:
1981: path = tilde_expand_filename(file, pw->pw_uid);
1982: if (strcmp(path, "-") == 0) {
1983: krl_spec = stdin;
1984: free(path);
1985: path = xstrdup("(standard input)");
1986: } else if ((krl_spec = fopen(path, "r")) == NULL)
1987: fatal("fopen %s: %s", path, strerror(errno));
1988:
1989: if (!quiet)
1990: printf("Revoking from %s\n", path);
1991: while (read_keyfile_line(krl_spec, path, line, sizeof(line),
1992: &lnum) == 0) {
1993: was_explicit_key = was_sha1 = 0;
1994: cp = line + strspn(line, " \t");
1995: /* Trim trailing space, comments and strip \n */
1996: for (i = 0, r = -1; cp[i] != '\0'; i++) {
1997: if (cp[i] == '#' || cp[i] == '\n') {
1998: cp[i] = '\0';
1999: break;
2000: }
2001: if (cp[i] == ' ' || cp[i] == '\t') {
2002: /* Remember the start of a span of whitespace */
2003: if (r == -1)
2004: r = i;
2005: } else
2006: r = -1;
2007: }
2008: if (r != -1)
2009: cp[r] = '\0';
2010: if (*cp == '\0')
2011: continue;
2012: if (strncasecmp(cp, "serial:", 7) == 0) {
1.261 djm 2013: if (ca == NULL && !wild_ca) {
1.231 djm 2014: fatal("revoking certificates by serial number "
1.223 djm 2015: "requires specification of a CA key");
2016: }
2017: cp += 7;
2018: cp = cp + strspn(cp, " \t");
2019: errno = 0;
2020: serial = strtoull(cp, &ep, 0);
2021: if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2022: fatal("%s:%lu: invalid serial \"%s\"",
2023: path, lnum, cp);
2024: if (errno == ERANGE && serial == ULLONG_MAX)
2025: fatal("%s:%lu: serial out of range",
2026: path, lnum);
2027: serial2 = serial;
2028: if (*ep == '-') {
2029: cp = ep + 1;
2030: errno = 0;
2031: serial2 = strtoull(cp, &ep, 0);
2032: if (*cp == '\0' || *ep != '\0')
2033: fatal("%s:%lu: invalid serial \"%s\"",
2034: path, lnum, cp);
2035: if (errno == ERANGE && serial2 == ULLONG_MAX)
2036: fatal("%s:%lu: serial out of range",
2037: path, lnum);
2038: if (serial2 <= serial)
2039: fatal("%s:%lu: invalid serial range "
2040: "%llu:%llu", path, lnum,
2041: (unsigned long long)serial,
2042: (unsigned long long)serial2);
2043: }
2044: if (ssh_krl_revoke_cert_by_serial_range(krl,
2045: ca, serial, serial2) != 0) {
2046: fatal("%s: revoke serial failed",
2047: __func__);
2048: }
2049: } else if (strncasecmp(cp, "id:", 3) == 0) {
1.261 djm 2050: if (ca == NULL && !wild_ca) {
1.232 djm 2051: fatal("revoking certificates by key ID "
1.223 djm 2052: "requires specification of a CA key");
2053: }
2054: cp += 3;
2055: cp = cp + strspn(cp, " \t");
2056: if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2057: fatal("%s: revoke key ID failed", __func__);
2058: } else {
2059: if (strncasecmp(cp, "key:", 4) == 0) {
2060: cp += 4;
2061: cp = cp + strspn(cp, " \t");
2062: was_explicit_key = 1;
2063: } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2064: cp += 5;
2065: cp = cp + strspn(cp, " \t");
2066: was_sha1 = 1;
2067: } else {
2068: /*
2069: * Just try to process the line as a key.
2070: * Parsing will fail if it isn't.
2071: */
2072: }
1.252 djm 2073: if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
1.223 djm 2074: fatal("key_new");
1.252 djm 2075: if ((r = sshkey_read(key, &cp)) != 0)
2076: fatal("%s:%lu: invalid key: %s",
2077: path, lnum, ssh_err(r));
1.223 djm 2078: if (was_explicit_key)
2079: r = ssh_krl_revoke_key_explicit(krl, key);
2080: else if (was_sha1)
2081: r = ssh_krl_revoke_key_sha1(krl, key);
2082: else
2083: r = ssh_krl_revoke_key(krl, key);
2084: if (r != 0)
1.252 djm 2085: fatal("%s: revoke key failed: %s",
2086: __func__, ssh_err(r));
2087: sshkey_free(key);
1.223 djm 2088: }
2089: }
2090: if (strcmp(path, "-") != 0)
2091: fclose(krl_spec);
1.226 djm 2092: free(path);
1.223 djm 2093: }
2094:
2095: static void
2096: do_gen_krl(struct passwd *pw, int updating, int argc, char **argv)
2097: {
2098: struct ssh_krl *krl;
2099: struct stat sb;
1.252 djm 2100: struct sshkey *ca = NULL;
1.261 djm 2101: int fd, i, r, wild_ca = 0;
1.223 djm 2102: char *tmp;
1.252 djm 2103: struct sshbuf *kbuf;
1.223 djm 2104:
2105: if (*identity_file == '\0')
2106: fatal("KRL generation requires an output file");
2107: if (stat(identity_file, &sb) == -1) {
2108: if (errno != ENOENT)
2109: fatal("Cannot access KRL \"%s\": %s",
2110: identity_file, strerror(errno));
2111: if (updating)
2112: fatal("KRL \"%s\" does not exist", identity_file);
2113: }
2114: if (ca_key_path != NULL) {
1.261 djm 2115: if (strcasecmp(ca_key_path, "none") == 0)
2116: wild_ca = 1;
2117: else {
2118: tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2119: if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2120: fatal("Cannot load CA public key %s: %s",
2121: tmp, ssh_err(r));
2122: free(tmp);
2123: }
1.223 djm 2124: }
2125:
2126: if (updating)
2127: load_krl(identity_file, &krl);
2128: else if ((krl = ssh_krl_init()) == NULL)
2129: fatal("couldn't create KRL");
2130:
2131: if (cert_serial != 0)
2132: ssh_krl_set_version(krl, cert_serial);
2133: if (identity_comment != NULL)
2134: ssh_krl_set_comment(krl, identity_comment);
2135:
2136: for (i = 0; i < argc; i++)
1.261 djm 2137: update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
1.223 djm 2138:
1.252 djm 2139: if ((kbuf = sshbuf_new()) == NULL)
2140: fatal("sshbuf_new failed");
2141: if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
1.223 djm 2142: fatal("Couldn't generate KRL");
2143: if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2144: fatal("open %s: %s", identity_file, strerror(errno));
1.252 djm 2145: if (atomicio(vwrite, fd, (void *)sshbuf_ptr(kbuf), sshbuf_len(kbuf)) !=
2146: sshbuf_len(kbuf))
1.223 djm 2147: fatal("write %s: %s", identity_file, strerror(errno));
2148: close(fd);
1.252 djm 2149: sshbuf_free(kbuf);
1.223 djm 2150: ssh_krl_free(krl);
1.226 djm 2151: if (ca != NULL)
1.252 djm 2152: sshkey_free(ca);
1.223 djm 2153: }
2154:
2155: static void
2156: do_check_krl(struct passwd *pw, int argc, char **argv)
2157: {
2158: int i, r, ret = 0;
2159: char *comment;
2160: struct ssh_krl *krl;
1.252 djm 2161: struct sshkey *k;
1.223 djm 2162:
2163: if (*identity_file == '\0')
2164: fatal("KRL checking requires an input file");
2165: load_krl(identity_file, &krl);
2166: for (i = 0; i < argc; i++) {
1.252 djm 2167: if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2168: fatal("Cannot load public key %s: %s",
2169: argv[i], ssh_err(r));
1.223 djm 2170: r = ssh_krl_check_key(krl, k);
2171: printf("%s%s%s%s: %s\n", argv[i],
2172: *comment ? " (" : "", comment, *comment ? ")" : "",
2173: r == 0 ? "ok" : "REVOKED");
2174: if (r != 0)
2175: ret = 1;
1.252 djm 2176: sshkey_free(k);
1.223 djm 2177: free(comment);
2178: }
2179: ssh_krl_free(krl);
2180: exit(ret);
2181: }
1.246 markus 2182: #endif
1.223 djm 2183:
2184: static void
1.10 markus 2185: usage(void)
2186: {
1.243 deraadt 2187: fprintf(stderr,
2188: "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n"
2189: " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
2190: " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n"
2191: " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2192: " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2193: " ssh-keygen -y [-f input_keyfile]\n"
2194: " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
1.265 naddy 2195: " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
1.243 deraadt 2196: " ssh-keygen -B [-f input_keyfile]\n");
1.177 markus 2197: #ifdef ENABLE_PKCS11
1.243 deraadt 2198: fprintf(stderr,
2199: " ssh-keygen -D pkcs11\n");
1.177 markus 2200: #endif
1.243 deraadt 2201: fprintf(stderr,
2202: " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2203: " ssh-keygen -H [-f known_hosts_file]\n"
2204: " ssh-keygen -R hostname [-f known_hosts_file]\n"
2205: " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
2206: " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2207: " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2208: " [-j start_line] [-K checkpt] [-W generator]\n"
2209: " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n"
2210: " [-O option] [-V validity_interval] [-z serial_number] file ...\n"
2211: " ssh-keygen -L [-f input_keyfile]\n"
2212: " ssh-keygen -A\n"
2213: " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2214: " file ...\n"
2215: " ssh-keygen -Q -f krl_file file ...\n");
1.12 markus 2216: exit(1);
1.10 markus 2217: }
2218:
1.13 deraadt 2219: /*
2220: * Main program for key management.
2221: */
1.2 provos 2222: int
1.156 deraadt 2223: main(int argc, char **argv)
1.1 deraadt 2224: {
1.253 deraadt 2225: char dotsshdir[PATH_MAX], comment[1024], *passphrase1, *passphrase2;
1.211 dtucker 2226: char *checkpoint = NULL;
1.259 djm 2227: char out_file[PATH_MAX], *rr_hostname = NULL, *ep, *fp, *ra;
1.252 djm 2228: struct sshkey *private, *public;
1.12 markus 2229: struct passwd *pw;
2230: struct stat st;
1.252 djm 2231: int r, opt, type, fd;
1.237 markus 2232: u_int32_t memory = 0, generator_wanted = 0;
1.107 djm 2233: int do_gen_candidates = 0, do_screen_candidates = 0;
1.223 djm 2234: int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
1.215 dtucker 2235: unsigned long start_lineno = 0, lines_to_process = 0;
1.107 djm 2236: BIGNUM *start = NULL;
1.12 markus 2237: FILE *f;
1.125 avsm 2238: const char *errstr;
1.33 markus 2239:
1.12 markus 2240: extern int optind;
2241: extern char *optarg;
1.129 djm 2242:
2243: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2244: sanitise_stdfd();
1.12 markus 2245:
1.201 djm 2246: OpenSSL_add_all_algorithms();
1.156 deraadt 2247: log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1.19 markus 2248:
1.14 markus 2249: /* we need this for the home * directory. */
1.12 markus 2250: pw = getpwuid(getuid());
2251: if (!pw) {
1.230 djm 2252: printf("No user exists for uid %lu\n", (u_long)getuid());
1.12 markus 2253: exit(1);
2254: }
1.19 markus 2255: if (gethostname(hostname, sizeof(hostname)) < 0) {
2256: perror("gethostname");
2257: exit(1);
2258: }
1.14 markus 2259:
1.251 djm 2260: /* Remaining characters: UYdw */
1.237 markus 2261: while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy"
1.251 djm 2262: "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:"
2263: "a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
1.12 markus 2264: switch (opt) {
1.206 stevesk 2265: case 'A':
2266: gen_all_hostkeys = 1;
2267: break;
1.12 markus 2268: case 'b':
1.202 markus 2269: bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
1.125 avsm 2270: if (errstr)
2271: fatal("Bits has bad value %s (%s)",
2272: optarg, errstr);
1.12 markus 2273: break;
1.251 djm 2274: case 'E':
2275: fingerprint_hash = ssh_digest_alg_by_name(optarg);
2276: if (fingerprint_hash == -1)
2277: fatal("Invalid hash algorithm \"%s\"", optarg);
2278: break;
1.119 djm 2279: case 'F':
2280: find_host = 1;
2281: rr_hostname = optarg;
2282: break;
2283: case 'H':
2284: hash_hosts = 1;
2285: break;
1.179 djm 2286: case 'I':
2287: cert_key_id = optarg;
2288: break;
1.215 dtucker 2289: case 'J':
2290: lines_to_process = strtoul(optarg, NULL, 10);
2291: break;
2292: case 'j':
2293: start_lineno = strtoul(optarg, NULL, 10);
2294: break;
1.119 djm 2295: case 'R':
2296: delete_host = 1;
2297: rr_hostname = optarg;
2298: break;
1.182 djm 2299: case 'L':
2300: show_cert = 1;
2301: break;
1.12 markus 2302: case 'l':
2303: print_fingerprint = 1;
2304: break;
1.49 markus 2305: case 'B':
2306: print_bubblebabble = 1;
2307: break;
1.193 djm 2308: case 'm':
2309: if (strcasecmp(optarg, "RFC4716") == 0 ||
2310: strcasecmp(optarg, "ssh2") == 0) {
2311: convert_format = FMT_RFC4716;
2312: break;
2313: }
2314: if (strcasecmp(optarg, "PKCS8") == 0) {
2315: convert_format = FMT_PKCS8;
2316: break;
2317: }
2318: if (strcasecmp(optarg, "PEM") == 0) {
2319: convert_format = FMT_PEM;
2320: break;
2321: }
2322: fatal("Unsupported conversion format \"%s\"", optarg);
1.179 djm 2323: case 'n':
2324: cert_principals = optarg;
2325: break;
1.237 markus 2326: case 'o':
2327: use_new_format = 1;
2328: break;
1.12 markus 2329: case 'p':
2330: change_passphrase = 1;
2331: break;
2332: case 'c':
2333: change_comment = 1;
2334: break;
2335: case 'f':
1.124 avsm 2336: if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2337: sizeof(identity_file))
2338: fatal("Identity filename too long");
1.12 markus 2339: have_identity = 1;
2340: break;
1.105 jakob 2341: case 'g':
2342: print_generic = 1;
2343: break;
1.12 markus 2344: case 'P':
2345: identity_passphrase = optarg;
2346: break;
2347: case 'N':
2348: identity_new_passphrase = optarg;
2349: break;
1.223 djm 2350: case 'Q':
2351: check_krl = 1;
2352: break;
1.179 djm 2353: case 'O':
1.186 djm 2354: add_cert_option(optarg);
1.179 djm 2355: break;
1.237 markus 2356: case 'Z':
2357: new_format_cipher = optarg;
2358: break;
1.12 markus 2359: case 'C':
2360: identity_comment = optarg;
2361: break;
2362: case 'q':
2363: quiet = 1;
1.20 deraadt 2364: break;
1.57 markus 2365: case 'e':
1.19 markus 2366: case 'x':
1.57 markus 2367: /* export key */
1.193 djm 2368: convert_to = 1;
1.19 markus 2369: break;
1.179 djm 2370: case 'h':
2371: cert_key_type = SSH2_CERT_TYPE_HOST;
1.190 djm 2372: certflags_flags = 0;
1.179 djm 2373: break;
1.223 djm 2374: case 'k':
2375: gen_krl = 1;
2376: break;
1.57 markus 2377: case 'i':
1.19 markus 2378: case 'X':
1.57 markus 2379: /* import key */
1.193 djm 2380: convert_from = 1;
1.19 markus 2381: break;
2382: case 'y':
2383: print_public = 1;
2384: break;
1.179 djm 2385: case 's':
2386: ca_key_path = optarg;
2387: break;
1.33 markus 2388: case 't':
2389: key_type_name = optarg;
2390: break;
1.75 markus 2391: case 'D':
1.177 markus 2392: pkcs11provider = optarg;
1.66 markus 2393: break;
1.223 djm 2394: case 'u':
2395: update_krl = 1;
2396: break;
1.113 djm 2397: case 'v':
2398: if (log_level == SYSLOG_LEVEL_INFO)
2399: log_level = SYSLOG_LEVEL_DEBUG1;
2400: else {
1.117 deraadt 2401: if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1.113 djm 2402: log_level < SYSLOG_LEVEL_DEBUG3)
2403: log_level++;
2404: }
2405: break;
1.105 jakob 2406: case 'r':
1.119 djm 2407: rr_hostname = optarg;
1.105 jakob 2408: break;
1.107 djm 2409: case 'W':
1.142 deraadt 2410: generator_wanted = (u_int32_t)strtonum(optarg, 1,
2411: UINT_MAX, &errstr);
1.124 avsm 2412: if (errstr)
2413: fatal("Desired generator has bad value: %s (%s)",
2414: optarg, errstr);
1.107 djm 2415: break;
2416: case 'a':
1.237 markus 2417: rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
1.124 avsm 2418: if (errstr)
1.237 markus 2419: fatal("Invalid number: %s (%s)",
1.124 avsm 2420: optarg, errstr);
1.107 djm 2421: break;
2422: case 'M':
1.142 deraadt 2423: memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
1.186 djm 2424: if (errstr)
1.124 avsm 2425: fatal("Memory limit is %s: %s", errstr, optarg);
1.107 djm 2426: break;
2427: case 'G':
2428: do_gen_candidates = 1;
1.124 avsm 2429: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2430: sizeof(out_file))
2431: fatal("Output filename too long");
1.107 djm 2432: break;
2433: case 'T':
2434: do_screen_candidates = 1;
1.124 avsm 2435: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2436: sizeof(out_file))
2437: fatal("Output filename too long");
1.107 djm 2438: break;
1.211 dtucker 2439: case 'K':
1.253 deraadt 2440: if (strlen(optarg) >= PATH_MAX)
1.211 dtucker 2441: fatal("Checkpoint filename too long");
2442: checkpoint = xstrdup(optarg);
2443: break;
1.107 djm 2444: case 'S':
2445: /* XXX - also compare length against bits */
2446: if (BN_hex2bn(&start, optarg) == 0)
2447: fatal("Invalid start point.");
2448: break;
1.179 djm 2449: case 'V':
2450: parse_cert_times(optarg);
1.186 djm 2451: break;
2452: case 'z':
1.219 djm 2453: errno = 0;
2454: cert_serial = strtoull(optarg, &ep, 10);
2455: if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2456: (errno == ERANGE && cert_serial == ULLONG_MAX))
2457: fatal("Invalid serial number \"%s\"", optarg);
1.179 djm 2458: break;
1.12 markus 2459: case '?':
2460: default:
2461: usage();
2462: }
2463: }
1.113 djm 2464:
2465: /* reinit */
1.156 deraadt 2466: log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
1.113 djm 2467:
1.179 djm 2468: argv += optind;
2469: argc -= optind;
2470:
2471: if (ca_key_path != NULL) {
1.223 djm 2472: if (argc < 1 && !gen_krl) {
1.179 djm 2473: printf("Too few arguments.\n");
2474: usage();
2475: }
1.223 djm 2476: } else if (argc > 0 && !gen_krl && !check_krl) {
1.12 markus 2477: printf("Too many arguments.\n");
1.87 djm 2478: usage();
2479: }
1.12 markus 2480: if (change_passphrase && change_comment) {
2481: printf("Can only have one of -p and -c.\n");
1.166 djm 2482: usage();
2483: }
2484: if (print_fingerprint && (delete_host || hash_hosts)) {
1.221 djm 2485: printf("Cannot use -l with -H or -R.\n");
1.12 markus 2486: usage();
1.223 djm 2487: }
1.246 markus 2488: #ifdef WITH_OPENSSL
1.223 djm 2489: if (gen_krl) {
2490: do_gen_krl(pw, update_krl, argc, argv);
2491: return (0);
2492: }
2493: if (check_krl) {
2494: do_check_krl(pw, argc, argv);
2495: return (0);
1.179 djm 2496: }
1.246 markus 2497: #endif
1.179 djm 2498: if (ca_key_path != NULL) {
2499: if (cert_key_id == NULL)
2500: fatal("Must specify key id (-I) when certifying");
2501: do_ca_sign(pw, argc, argv);
1.12 markus 2502: }
1.182 djm 2503: if (show_cert)
2504: do_show_cert(pw);
1.119 djm 2505: if (delete_host || hash_hosts || find_host)
2506: do_known_hosts(pw, rr_hostname);
1.221 djm 2507: if (pkcs11provider != NULL)
2508: do_download(pw);
1.49 markus 2509: if (print_fingerprint || print_bubblebabble)
1.12 markus 2510: do_fingerprint(pw);
2511: if (change_passphrase)
2512: do_change_passphrase(pw);
2513: if (change_comment)
2514: do_change_comment(pw);
1.246 markus 2515: #ifdef WITH_OPENSSL
1.193 djm 2516: if (convert_to)
2517: do_convert_to(pw);
2518: if (convert_from)
2519: do_convert_from(pw);
1.246 markus 2520: #endif
1.19 markus 2521: if (print_public)
2522: do_print_public(pw);
1.119 djm 2523: if (rr_hostname != NULL) {
1.138 jakob 2524: unsigned int n = 0;
2525:
2526: if (have_identity) {
2527: n = do_print_resource_record(pw,
2528: identity_file, rr_hostname);
2529: if (n == 0) {
2530: perror(identity_file);
2531: exit(1);
2532: }
2533: exit(0);
2534: } else {
2535:
2536: n += do_print_resource_record(pw,
2537: _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2538: n += do_print_resource_record(pw,
2539: _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1.214 djm 2540: n += do_print_resource_record(pw,
2541: _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
1.244 logan 2542: n += do_print_resource_record(pw,
2543: _PATH_HOST_ED25519_KEY_FILE, rr_hostname);
1.138 jakob 2544: if (n == 0)
2545: fatal("no keys found.");
2546: exit(0);
2547: }
1.105 jakob 2548: }
1.107 djm 2549:
2550: if (do_gen_candidates) {
2551: FILE *out = fopen(out_file, "w");
1.111 djm 2552:
1.107 djm 2553: if (out == NULL) {
2554: error("Couldn't open modulus candidate file \"%s\": %s",
2555: out_file, strerror(errno));
2556: return (1);
2557: }
1.130 markus 2558: if (bits == 0)
2559: bits = DEFAULT_BITS;
1.107 djm 2560: if (gen_candidates(out, memory, bits, start) != 0)
1.131 stevesk 2561: fatal("modulus candidate generation failed");
1.107 djm 2562:
2563: return (0);
2564: }
2565:
2566: if (do_screen_candidates) {
2567: FILE *in;
1.225 djm 2568: FILE *out = fopen(out_file, "a");
1.107 djm 2569:
2570: if (have_identity && strcmp(identity_file, "-") != 0) {
2571: if ((in = fopen(identity_file, "r")) == NULL) {
2572: fatal("Couldn't open modulus candidate "
1.111 djm 2573: "file \"%s\": %s", identity_file,
1.107 djm 2574: strerror(errno));
2575: }
2576: } else
2577: in = stdin;
2578:
2579: if (out == NULL) {
2580: fatal("Couldn't open moduli file \"%s\": %s",
2581: out_file, strerror(errno));
2582: }
1.237 markus 2583: if (prime_test(in, out, rounds == 0 ? 100 : rounds,
2584: generator_wanted, checkpoint,
1.215 dtucker 2585: start_lineno, lines_to_process) != 0)
1.131 stevesk 2586: fatal("modulus screening failed");
1.108 markus 2587: return (0);
1.75 markus 2588: }
1.12 markus 2589:
1.206 stevesk 2590: if (gen_all_hostkeys) {
2591: do_gen_all_hostkeys(pw);
2592: return (0);
2593: }
2594:
1.133 djm 2595: if (key_type_name == NULL)
2596: key_type_name = "rsa";
2597:
1.252 djm 2598: type = sshkey_type_from_name(key_type_name);
1.255 djm 2599: type_bits_valid(type, key_type_name, &bits);
1.206 stevesk 2600:
1.33 markus 2601: if (!quiet)
1.252 djm 2602: printf("Generating public/private %s key pair.\n",
2603: key_type_name);
2604: if ((r = sshkey_generate(type, bits, &private)) != 0) {
1.173 tobias 2605: fprintf(stderr, "key_generate failed\n");
1.33 markus 2606: exit(1);
2607: }
1.252 djm 2608: if ((r = sshkey_from_private(private, &public)) != 0) {
2609: fprintf(stderr, "key_from_private failed: %s\n", ssh_err(r));
2610: exit(1);
2611: }
1.12 markus 2612:
2613: if (!have_identity)
2614: ask_filename(pw, "Enter file in which to save the key");
2615:
1.132 djm 2616: /* Create ~/.ssh directory if it doesn't already exist. */
1.188 djm 2617: snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2618: pw->pw_dir, _PATH_SSH_USER_DIR);
2619: if (strstr(identity_file, dotsshdir) != NULL) {
2620: if (stat(dotsshdir, &st) < 0) {
2621: if (errno != ENOENT) {
2622: error("Could not stat %s: %s", dotsshdir,
2623: strerror(errno));
2624: } else if (mkdir(dotsshdir, 0700) < 0) {
2625: error("Could not create directory '%s': %s",
2626: dotsshdir, strerror(errno));
2627: } else if (!quiet)
2628: printf("Created directory '%s'.\n", dotsshdir);
2629: }
1.12 markus 2630: }
2631: /* If the file already exists, ask the user to confirm. */
2632: if (stat(identity_file, &st) >= 0) {
2633: char yesno[3];
2634: printf("%s already exists.\n", identity_file);
2635: printf("Overwrite (y/n)? ");
2636: fflush(stdout);
2637: if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2638: exit(1);
2639: if (yesno[0] != 'y' && yesno[0] != 'Y')
2640: exit(1);
2641: }
2642: /* Ask for a passphrase (twice). */
2643: if (identity_passphrase)
2644: passphrase1 = xstrdup(identity_passphrase);
2645: else if (identity_new_passphrase)
2646: passphrase1 = xstrdup(identity_new_passphrase);
2647: else {
2648: passphrase_again:
2649: passphrase1 =
1.65 markus 2650: read_passphrase("Enter passphrase (empty for no "
2651: "passphrase): ", RP_ALLOW_STDIN);
2652: passphrase2 = read_passphrase("Enter same passphrase again: ",
2653: RP_ALLOW_STDIN);
1.12 markus 2654: if (strcmp(passphrase1, passphrase2) != 0) {
1.65 markus 2655: /*
2656: * The passphrases do not match. Clear them and
2657: * retry.
2658: */
1.240 djm 2659: explicit_bzero(passphrase1, strlen(passphrase1));
2660: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 2661: free(passphrase1);
2662: free(passphrase2);
1.12 markus 2663: printf("Passphrases do not match. Try again.\n");
2664: goto passphrase_again;
2665: }
2666: /* Clear the other copy of the passphrase. */
1.240 djm 2667: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 2668: free(passphrase2);
1.12 markus 2669: }
2670:
2671: if (identity_comment) {
2672: strlcpy(comment, identity_comment, sizeof(comment));
2673: } else {
1.172 stevesk 2674: /* Create default comment field for the passphrase. */
1.12 markus 2675: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2676: }
2677:
2678: /* Save the key with the given passphrase and comment. */
1.252 djm 2679: if ((r = sshkey_save_private(private, identity_file, passphrase1,
2680: comment, use_new_format, new_format_cipher, rounds)) != 0) {
2681: printf("Saving key \"%s\" failed: %s\n",
2682: identity_file, ssh_err(r));
1.240 djm 2683: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 2684: free(passphrase1);
1.12 markus 2685: exit(1);
2686: }
2687: /* Clear the passphrase. */
1.240 djm 2688: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 2689: free(passphrase1);
1.12 markus 2690:
2691: /* Clear the private key and the random number generator. */
1.252 djm 2692: sshkey_free(private);
1.12 markus 2693:
2694: if (!quiet)
2695: printf("Your identification has been saved in %s.\n", identity_file);
2696:
2697: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 2698: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2699: if (fd == -1) {
1.12 markus 2700: printf("Could not save your public key in %s\n", identity_file);
1.46 deraadt 2701: exit(1);
2702: }
2703: f = fdopen(fd, "w");
2704: if (f == NULL) {
1.173 tobias 2705: printf("fdopen %s failed\n", identity_file);
1.12 markus 2706: exit(1);
2707: }
1.252 djm 2708: if ((r = sshkey_write(public, f)) != 0)
2709: fprintf(stderr, "write key failed: %s\n", ssh_err(r));
1.19 markus 2710: fprintf(f, " %s\n", comment);
1.12 markus 2711: fclose(f);
2712:
2713: if (!quiet) {
1.259 djm 2714: fp = sshkey_fingerprint(public, fingerprint_hash,
1.251 djm 2715: SSH_FP_DEFAULT);
1.259 djm 2716: ra = sshkey_fingerprint(public, fingerprint_hash,
1.167 grunk 2717: SSH_FP_RANDOMART);
1.259 djm 2718: if (fp == NULL || ra == NULL)
2719: fatal("sshkey_fingerprint failed");
1.19 markus 2720: printf("Your public key has been saved in %s.\n",
2721: identity_file);
1.12 markus 2722: printf("The key fingerprint is:\n");
1.50 markus 2723: printf("%s %s\n", fp, comment);
1.167 grunk 2724: printf("The key's randomart image is:\n");
2725: printf("%s\n", ra);
1.227 djm 2726: free(ra);
2727: free(fp);
1.12 markus 2728: }
1.19 markus 2729:
1.252 djm 2730: sshkey_free(public);
1.12 markus 2731: exit(0);
1.1 deraadt 2732: }