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