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