Annotation of src/usr.bin/ssh/ssh-keygen.c, Revision 1.344
1.344 ! djm 1: /* $OpenBSD: ssh-keygen.c,v 1.343 2019/09/03 08:27:52 djm Exp $ */
1.1 deraadt 2: /*
1.13 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Identity and host key generation and maintenance.
1.31 deraadt 7: *
8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.13 deraadt 13: */
1.1 deraadt 14:
1.136 stevesk 15: #include <sys/types.h>
1.174 dtucker 16: #include <sys/socket.h>
1.136 stevesk 17: #include <sys/stat.h>
1.19 markus 18:
19: #include <openssl/evp.h>
20: #include <openssl/pem.h>
1.145 stevesk 21:
1.148 stevesk 22: #include <errno.h>
1.147 stevesk 23: #include <fcntl.h>
1.248 djm 24: #include <netdb.h>
1.145 stevesk 25: #include <pwd.h>
1.153 stevesk 26: #include <stdio.h>
1.152 stevesk 27: #include <stdlib.h>
1.150 stevesk 28: #include <string.h>
1.149 stevesk 29: #include <unistd.h>
1.253 deraadt 30: #include <limits.h>
1.294 djm 31: #include <locale.h>
1.1 deraadt 32:
33: #include "xmalloc.h"
1.252 djm 34: #include "sshkey.h"
1.19 markus 35: #include "authfile.h"
1.252 djm 36: #include "sshbuf.h"
1.40 markus 37: #include "pathnames.h"
1.41 markus 38: #include "log.h"
1.114 djm 39: #include "misc.h"
1.119 djm 40: #include "match.h"
41: #include "hostfile.h"
1.146 stevesk 42: #include "dns.h"
1.223 djm 43: #include "ssh.h"
1.179 djm 44: #include "ssh2.h"
1.252 djm 45: #include "ssherr.h"
1.223 djm 46: #include "atomicio.h"
47: #include "krl.h"
1.251 djm 48: #include "digest.h"
1.294 djm 49: #include "utf8.h"
1.305 djm 50: #include "authfd.h"
1.344 ! djm 51: #include "sshsig.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.328 dtucker 63: /*
1.329 dtucker 64: * Default number of bits in the RSA, DSA and ECDSA keys. These value can be
65: * overridden on the command line.
66: *
67: * These values, with the exception of DSA, provide security equivalent to at
68: * least 128 bits of security according to NIST Special Publication 800-57:
69: * Recommendation for Key Management Part 1 rev 4 section 5.6.1.
70: * For DSA it (and FIPS-186-4 section 4.2) specifies that the only size for
71: * which a 160bit hash is acceptable is 1kbit, and since ssh-dss specifies only
72: * SHA1 we limit the DSA key size 1k bits.
1.328 dtucker 73: */
74: #define DEFAULT_BITS 3072
1.130 markus 75: #define DEFAULT_BITS_DSA 1024
1.203 naddy 76: #define DEFAULT_BITS_ECDSA 256
1.1 deraadt 77:
1.325 djm 78: static int quiet = 0;
1.182 djm 79:
1.8 markus 80: /* Flag indicating that we just want to see the key fingerprint */
1.325 djm 81: static int print_fingerprint = 0;
82: static int print_bubblebabble = 0;
1.8 markus 83:
1.251 djm 84: /* Hash algorithm to use for fingerprints. */
1.325 djm 85: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
1.251 djm 86:
1.10 markus 87: /* The identity file name, given on the command line or entered by the user. */
1.325 djm 88: static char identity_file[1024];
89: static int have_identity = 0;
1.1 deraadt 90:
91: /* This is set to the passphrase if given on the command line. */
1.325 djm 92: static char *identity_passphrase = NULL;
1.1 deraadt 93:
94: /* This is set to the new passphrase if given on the command line. */
1.325 djm 95: static char *identity_new_passphrase = NULL;
1.186 djm 96:
1.179 djm 97: /* Key type when certifying */
1.325 djm 98: static u_int cert_key_type = SSH2_CERT_TYPE_USER;
1.179 djm 99:
100: /* "key ID" of signed key */
1.325 djm 101: static char *cert_key_id = NULL;
1.179 djm 102:
103: /* Comma-separated list of principal names for certifying keys */
1.325 djm 104: static char *cert_principals = NULL;
1.179 djm 105:
106: /* Validity period for certificates */
1.325 djm 107: static u_int64_t cert_valid_from = 0;
108: static u_int64_t cert_valid_to = ~0ULL;
1.179 djm 109:
1.186 djm 110: /* Certificate options */
1.190 djm 111: #define CERTOPT_X_FWD (1)
112: #define CERTOPT_AGENT_FWD (1<<1)
113: #define CERTOPT_PORT_FWD (1<<2)
114: #define CERTOPT_PTY (1<<3)
115: #define CERTOPT_USER_RC (1<<4)
116: #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
117: CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
1.325 djm 118: static u_int32_t certflags_flags = CERTOPT_DEFAULT;
119: static char *certflags_command = NULL;
120: static char *certflags_src_addr = NULL;
1.179 djm 121:
1.300 djm 122: /* Arbitrary extensions specified by user */
123: struct cert_userext {
124: char *key;
125: char *val;
126: int crit;
127: };
1.325 djm 128: static struct cert_userext *cert_userext;
129: static size_t ncert_userext;
1.300 djm 130:
1.193 djm 131: /* Conversion to/from various formats */
132: enum {
133: FMT_RFC4716,
134: FMT_PKCS8,
135: FMT_PEM
136: } convert_format = FMT_RFC4716;
1.33 markus 137:
1.325 djm 138: static char *key_type_name = NULL;
1.19 markus 139:
1.197 djm 140: /* Load key from this PKCS#11 provider */
1.325 djm 141: static char *pkcs11provider = NULL;
1.193 djm 142:
1.336 djm 143: /* Format for writing private keys */
144: static int private_key_format = SSHKEY_PRIVATE_OPENSSH;
1.237 markus 145:
146: /* Cipher for new-format private keys */
1.336 djm 147: static char *openssh_format_cipher = NULL;
1.237 markus 148:
149: /*
150: * Number of KDF rounds to derive new format keys /
151: * number of primality trials when screening moduli.
152: */
1.325 djm 153: static int rounds = 0;
1.237 markus 154:
1.10 markus 155: /* argv0 */
156: extern char *__progname;
1.1 deraadt 157:
1.325 djm 158: static char hostname[NI_MAXHOST];
1.19 markus 159:
1.274 djm 160: #ifdef WITH_OPENSSL
1.115 djm 161: /* moduli.c */
1.124 avsm 162: int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
1.215 dtucker 163: int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
164: unsigned long);
1.274 djm 165: #endif
1.115 djm 166:
1.63 itojun 167: static void
1.255 djm 168: type_bits_valid(int type, const char *name, u_int32_t *bitsp)
1.206 stevesk 169: {
1.269 djm 170: if (type == KEY_UNSPEC)
171: fatal("unknown key type %s", key_type_name);
1.209 djm 172: if (*bitsp == 0) {
1.271 djm 173: #ifdef WITH_OPENSSL
1.339 naddy 174: u_int nid;
175:
176: switch(type) {
177: case KEY_DSA:
1.209 djm 178: *bitsp = DEFAULT_BITS_DSA;
1.339 naddy 179: break;
180: case KEY_ECDSA:
1.255 djm 181: if (name != NULL &&
182: (nid = sshkey_ecdsa_nid_from_name(name)) > 0)
183: *bitsp = sshkey_curve_nid_to_bits(nid);
184: if (*bitsp == 0)
185: *bitsp = DEFAULT_BITS_ECDSA;
1.339 naddy 186: break;
187: case KEY_RSA:
188: *bitsp = DEFAULT_BITS;
189: break;
190: }
1.271 djm 191: #endif
1.206 stevesk 192: }
1.271 djm 193: #ifdef WITH_OPENSSL
1.303 djm 194: switch (type) {
195: case KEY_DSA:
196: if (*bitsp != 1024)
197: fatal("Invalid DSA key length: must be 1024 bits");
198: break;
199: case KEY_RSA:
200: if (*bitsp < SSH_RSA_MINIMUM_MODULUS_SIZE)
201: fatal("Invalid RSA key length: minimum is %d bits",
202: SSH_RSA_MINIMUM_MODULUS_SIZE);
1.339 naddy 203: else if (*bitsp > OPENSSL_RSA_MAX_MODULUS_BITS)
204: fatal("Invalid RSA key length: maximum is %d bits",
205: OPENSSL_RSA_MAX_MODULUS_BITS);
1.303 djm 206: break;
207: case KEY_ECDSA:
208: if (sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
209: fatal("Invalid ECDSA key length: valid lengths are "
210: "256, 384 or 521 bits");
211: }
1.246 markus 212: #endif
1.206 stevesk 213: }
214:
1.343 djm 215: /*
216: * Checks whether a file exists and, if so, asks the user whether they wish
217: * to overwrite it.
218: * Returns nonzero if the file does not already exist or if the user agrees to
219: * overwrite, or zero otherwise.
220: */
221: static int
222: confirm_overwrite(const char *filename)
223: {
224: char yesno[3];
225: struct stat st;
226:
227: if (stat(filename, &st) != 0)
228: return 1;
229: printf("%s already exists.\n", filename);
230: printf("Overwrite (y/n)? ");
231: fflush(stdout);
232: if (fgets(yesno, sizeof(yesno), stdin) == NULL)
233: return 0;
234: if (yesno[0] != 'y' && yesno[0] != 'Y')
235: return 0;
236: return 1;
237: }
238:
1.206 stevesk 239: static void
1.10 markus 240: ask_filename(struct passwd *pw, const char *prompt)
1.1 deraadt 241: {
1.12 markus 242: char buf[1024];
1.35 markus 243: char *name = NULL;
244:
1.92 stevesk 245: if (key_type_name == NULL)
1.40 markus 246: name = _PATH_SSH_CLIENT_ID_RSA;
1.140 deraadt 247: else {
1.252 djm 248: switch (sshkey_type_from_name(key_type_name)) {
1.186 djm 249: case KEY_DSA_CERT:
1.92 stevesk 250: case KEY_DSA:
251: name = _PATH_SSH_CLIENT_ID_DSA;
252: break;
1.200 djm 253: case KEY_ECDSA_CERT:
254: case KEY_ECDSA:
255: name = _PATH_SSH_CLIENT_ID_ECDSA;
256: break;
1.186 djm 257: case KEY_RSA_CERT:
1.92 stevesk 258: case KEY_RSA:
259: name = _PATH_SSH_CLIENT_ID_RSA;
260: break;
1.238 markus 261: case KEY_ED25519:
262: case KEY_ED25519_CERT:
263: name = _PATH_SSH_CLIENT_ID_ED25519;
264: break;
1.313 markus 265: case KEY_XMSS:
266: case KEY_XMSS_CERT:
267: name = _PATH_SSH_CLIENT_ID_XMSS;
268: break;
1.92 stevesk 269: default:
1.269 djm 270: fatal("bad key type");
1.92 stevesk 271: }
1.140 deraadt 272: }
1.269 djm 273: snprintf(identity_file, sizeof(identity_file),
274: "%s/%s", pw->pw_dir, name);
275: printf("%s (%s): ", prompt, identity_file);
276: fflush(stdout);
1.12 markus 277: if (fgets(buf, sizeof(buf), stdin) == NULL)
278: exit(1);
1.162 gilles 279: buf[strcspn(buf, "\n")] = '\0';
1.12 markus 280: if (strcmp(buf, "") != 0)
281: strlcpy(identity_file, buf, sizeof(identity_file));
282: have_identity = 1;
1.7 markus 283: }
284:
1.252 djm 285: static struct sshkey *
1.342 djm 286: load_identity(const char *filename, char **commentp)
1.19 markus 287: {
1.52 markus 288: char *pass;
1.252 djm 289: struct sshkey *prv;
290: int r;
1.52 markus 291:
1.341 djm 292: if (commentp != NULL)
293: *commentp = NULL;
294: if ((r = sshkey_load_private(filename, "", &prv, commentp)) == 0)
1.252 djm 295: return prv;
296: if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
297: fatal("Load key \"%s\": %s", filename, ssh_err(r));
298: if (identity_passphrase)
299: pass = xstrdup(identity_passphrase);
300: else
301: pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
1.341 djm 302: r = sshkey_load_private(filename, pass, &prv, commentp);
1.252 djm 303: explicit_bzero(pass, strlen(pass));
304: free(pass);
305: if (r != 0)
306: fatal("Load key \"%s\": %s", filename, ssh_err(r));
1.52 markus 307: return prv;
1.19 markus 308: }
309:
1.32 markus 310: #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
1.100 deraadt 311: #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
1.32 markus 312: #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1.42 stevesk 313: #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
1.19 markus 314:
1.246 markus 315: #ifdef WITH_OPENSSL
1.63 itojun 316: static void
1.252 djm 317: do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
1.19 markus 318: {
1.337 djm 319: struct sshbuf *b;
320: char comment[61], *b64;
1.252 djm 321: int r;
1.19 markus 322:
1.337 djm 323: if ((b = sshbuf_new()) == NULL)
324: fatal("%s: sshbuf_new failed", __func__);
325: if ((r = sshkey_putb(k, b)) != 0)
1.269 djm 326: fatal("key_to_blob failed: %s", ssh_err(r));
1.337 djm 327: if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL)
328: fatal("%s: sshbuf_dtob64_string failed", __func__);
329:
1.176 djm 330: /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
331: snprintf(comment, sizeof(comment),
332: "%u-bit %s, converted by %s@%s from OpenSSH",
1.252 djm 333: sshkey_size(k), sshkey_type(k),
1.19 markus 334: pw->pw_name, hostname);
1.176 djm 335:
1.337 djm 336: sshkey_free(k);
337: sshbuf_free(b);
338:
1.176 djm 339: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
1.337 djm 340: fprintf(stdout, "Comment: \"%s\"\n%s", comment, b64);
1.32 markus 341: fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1.337 djm 342: free(b64);
1.19 markus 343: exit(0);
344: }
345:
1.63 itojun 346: static void
1.252 djm 347: do_convert_to_pkcs8(struct sshkey *k)
1.193 djm 348: {
1.252 djm 349: switch (sshkey_type_plain(k->type)) {
1.193 djm 350: case KEY_RSA:
351: if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
352: fatal("PEM_write_RSA_PUBKEY failed");
353: break;
354: case KEY_DSA:
355: if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
356: fatal("PEM_write_DSA_PUBKEY failed");
357: break;
1.200 djm 358: case KEY_ECDSA:
359: if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
360: fatal("PEM_write_EC_PUBKEY failed");
361: break;
1.193 djm 362: default:
1.252 djm 363: fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
1.193 djm 364: }
365: exit(0);
366: }
367:
368: static void
1.252 djm 369: do_convert_to_pem(struct sshkey *k)
1.193 djm 370: {
1.252 djm 371: switch (sshkey_type_plain(k->type)) {
1.193 djm 372: case KEY_RSA:
373: if (!PEM_write_RSAPublicKey(stdout, k->rsa))
374: fatal("PEM_write_RSAPublicKey failed");
375: break;
376: default:
1.252 djm 377: fatal("%s: unsupported key type %s", __func__, sshkey_type(k));
1.193 djm 378: }
379: exit(0);
380: }
381:
382: static void
383: do_convert_to(struct passwd *pw)
384: {
1.252 djm 385: struct sshkey *k;
1.193 djm 386: struct stat st;
1.252 djm 387: int r;
1.193 djm 388:
389: if (!have_identity)
390: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 391: if (stat(identity_file, &st) == -1)
1.193 djm 392: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.252 djm 393: if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
1.341 djm 394: k = load_identity(identity_file, NULL);
1.193 djm 395: switch (convert_format) {
396: case FMT_RFC4716:
397: do_convert_to_ssh2(pw, k);
398: break;
399: case FMT_PKCS8:
400: do_convert_to_pkcs8(k);
401: break;
402: case FMT_PEM:
403: do_convert_to_pem(k);
404: break;
405: default:
406: fatal("%s: unknown key format %d", __func__, convert_format);
407: }
408: exit(0);
409: }
410:
1.252 djm 411: /*
412: * This is almost exactly the bignum1 encoding, but with 32 bit for length
413: * instead of 16.
414: */
1.193 djm 415: static void
1.252 djm 416: buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
1.32 markus 417: {
1.252 djm 418: u_int bytes, bignum_bits;
419: int r;
1.53 markus 420:
1.252 djm 421: if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
422: fatal("%s: buffer error: %s", __func__, ssh_err(r));
423: bytes = (bignum_bits + 7) / 8;
424: if (sshbuf_len(b) < bytes)
425: fatal("%s: input buffer too small: need %d have %zu",
426: __func__, bytes, sshbuf_len(b));
427: if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
428: fatal("%s: BN_bin2bn failed", __func__);
429: if ((r = sshbuf_consume(b, bytes)) != 0)
430: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.32 markus 431: }
432:
1.252 djm 433: static struct sshkey *
1.337 djm 434: do_convert_private_ssh2(struct sshbuf *b)
1.32 markus 435: {
1.252 djm 436: struct sshkey *key = NULL;
1.64 markus 437: char *type, *cipher;
1.252 djm 438: u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
439: int r, rlen, ktype;
440: u_int magic, i1, i2, i3, i4;
441: size_t slen;
1.62 markus 442: u_long e;
1.321 djm 443: BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
444: BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
445: BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
446: BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL;
1.337 djm 447:
1.252 djm 448: if ((r = sshbuf_get_u32(b, &magic)) != 0)
449: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.32 markus 450:
451: if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
1.252 djm 452: error("bad magic 0x%x != 0x%x", magic,
453: SSH_COM_PRIVATE_KEY_MAGIC);
1.32 markus 454: return NULL;
455: }
1.252 djm 456: if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
457: (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
458: (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
459: (r = sshbuf_get_u32(b, &i2)) != 0 ||
460: (r = sshbuf_get_u32(b, &i3)) != 0 ||
461: (r = sshbuf_get_u32(b, &i4)) != 0)
462: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.158 stevesk 463: debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
1.32 markus 464: if (strcmp(cipher, "none") != 0) {
465: error("unsupported cipher %s", cipher);
1.227 djm 466: free(cipher);
467: free(type);
1.32 markus 468: return NULL;
469: }
1.227 djm 470: free(cipher);
1.32 markus 471:
1.53 markus 472: if (strstr(type, "dsa")) {
473: ktype = KEY_DSA;
474: } else if (strstr(type, "rsa")) {
475: ktype = KEY_RSA;
476: } else {
1.227 djm 477: free(type);
1.32 markus 478: return NULL;
479: }
1.322 djm 480: if ((key = sshkey_new(ktype)) == NULL)
481: fatal("sshkey_new failed");
1.227 djm 482: free(type);
1.53 markus 483:
484: switch (key->type) {
485: case KEY_DSA:
1.321 djm 486: if ((dsa_p = BN_new()) == NULL ||
487: (dsa_q = BN_new()) == NULL ||
488: (dsa_g = BN_new()) == NULL ||
489: (dsa_pub_key = BN_new()) == NULL ||
490: (dsa_priv_key = BN_new()) == NULL)
491: fatal("%s: BN_new", __func__);
492: buffer_get_bignum_bits(b, dsa_p);
493: buffer_get_bignum_bits(b, dsa_g);
494: buffer_get_bignum_bits(b, dsa_q);
495: buffer_get_bignum_bits(b, dsa_pub_key);
496: buffer_get_bignum_bits(b, dsa_priv_key);
497: if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g))
498: fatal("%s: DSA_set0_pqg failed", __func__);
499: dsa_p = dsa_q = dsa_g = NULL; /* transferred */
500: if (!DSA_set0_key(key->dsa, dsa_pub_key, dsa_priv_key))
501: fatal("%s: DSA_set0_key failed", __func__);
502: dsa_pub_key = dsa_priv_key = NULL; /* transferred */
1.53 markus 503: break;
504: case KEY_RSA:
1.252 djm 505: if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
506: (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
507: (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
508: fatal("%s: buffer error: %s", __func__, ssh_err(r));
509: e = e1;
1.62 markus 510: debug("e %lx", e);
511: if (e < 30) {
512: e <<= 8;
1.252 djm 513: e += e2;
1.62 markus 514: debug("e %lx", e);
515: e <<= 8;
1.252 djm 516: e += e3;
1.62 markus 517: debug("e %lx", e);
518: }
1.321 djm 519: if ((rsa_e = BN_new()) == NULL)
520: fatal("%s: BN_new", __func__);
521: if (!BN_set_word(rsa_e, e)) {
522: BN_clear_free(rsa_e);
1.252 djm 523: sshkey_free(key);
1.53 markus 524: return NULL;
525: }
1.321 djm 526: if ((rsa_n = BN_new()) == NULL ||
527: (rsa_d = BN_new()) == NULL ||
528: (rsa_p = BN_new()) == NULL ||
529: (rsa_q = BN_new()) == NULL ||
530: (rsa_iqmp = BN_new()) == NULL)
531: fatal("%s: BN_new", __func__);
532: buffer_get_bignum_bits(b, rsa_d);
533: buffer_get_bignum_bits(b, rsa_n);
534: buffer_get_bignum_bits(b, rsa_iqmp);
535: buffer_get_bignum_bits(b, rsa_q);
536: buffer_get_bignum_bits(b, rsa_p);
537: if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, rsa_d))
538: fatal("%s: RSA_set0_key failed", __func__);
539: rsa_n = rsa_e = rsa_d = NULL; /* transferred */
540: if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q))
541: fatal("%s: RSA_set0_factors failed", __func__);
542: rsa_p = rsa_q = NULL; /* transferred */
543: if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)
1.252 djm 544: fatal("generate RSA parameters failed: %s", ssh_err(r));
1.321 djm 545: BN_clear_free(rsa_iqmp);
1.53 markus 546: break;
547: }
1.252 djm 548: rlen = sshbuf_len(b);
1.85 deraadt 549: if (rlen != 0)
1.337 djm 550: error("%s: remaining bytes in key blob %d", __func__, rlen);
1.64 markus 551:
552: /* try the key */
1.285 markus 553: if (sshkey_sign(key, &sig, &slen, data, sizeof(data), NULL, 0) != 0 ||
1.309 djm 554: sshkey_verify(key, sig, slen, data, sizeof(data), NULL, 0) != 0) {
1.252 djm 555: sshkey_free(key);
556: free(sig);
557: return NULL;
558: }
1.227 djm 559: free(sig);
1.32 markus 560: return key;
561: }
562:
1.137 dtucker 563: static int
564: get_line(FILE *fp, char *line, size_t len)
565: {
566: int c;
567: size_t pos = 0;
568:
569: line[0] = '\0';
570: while ((c = fgetc(fp)) != EOF) {
1.269 djm 571: if (pos >= len - 1)
572: fatal("input line too long.");
1.140 deraadt 573: switch (c) {
1.137 dtucker 574: case '\r':
575: c = fgetc(fp);
1.269 djm 576: if (c != EOF && c != '\n' && ungetc(c, fp) == EOF)
577: fatal("unget: %s", strerror(errno));
1.137 dtucker 578: return pos;
579: case '\n':
580: return pos;
581: }
582: line[pos++] = c;
583: line[pos] = '\0';
584: }
1.157 stevesk 585: /* We reached EOF */
586: return -1;
1.137 dtucker 587: }
588:
1.63 itojun 589: static void
1.252 djm 590: do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
1.19 markus 591: {
1.252 djm 592: int r, blen, escaped = 0;
1.98 markus 593: u_int len;
1.137 dtucker 594: char line[1024];
1.337 djm 595: struct sshbuf *buf;
1.19 markus 596: char encoded[8096];
597: FILE *fp;
598:
1.337 djm 599: if ((buf = sshbuf_new()) == NULL)
600: fatal("sshbuf_new failed");
1.191 djm 601: if ((fp = fopen(identity_file, "r")) == NULL)
602: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
1.19 markus 603: encoded[0] = '\0';
1.137 dtucker 604: while ((blen = get_line(fp, line, sizeof(line))) != -1) {
1.228 djm 605: if (blen > 0 && line[blen - 1] == '\\')
1.25 markus 606: escaped++;
1.19 markus 607: if (strncmp(line, "----", 4) == 0 ||
608: strstr(line, ": ") != NULL) {
1.32 markus 609: if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
1.193 djm 610: *private = 1;
1.64 markus 611: if (strstr(line, " END ") != NULL) {
612: break;
613: }
1.60 markus 614: /* fprintf(stderr, "ignore: %s", line); */
1.19 markus 615: continue;
616: }
1.25 markus 617: if (escaped) {
618: escaped--;
1.60 markus 619: /* fprintf(stderr, "escaped: %s", line); */
1.25 markus 620: continue;
1.19 markus 621: }
622: strlcat(encoded, line, sizeof(encoded));
623: }
1.98 markus 624: len = strlen(encoded);
625: if (((len % 4) == 3) &&
626: (encoded[len-1] == '=') &&
627: (encoded[len-2] == '=') &&
628: (encoded[len-3] == '='))
629: encoded[len-3] = '\0';
1.337 djm 630: if ((r = sshbuf_b64tod(buf, encoded)) != 0)
631: fatal("%s: base64 decoding failed: %s", __func__, ssh_err(r));
1.252 djm 632: if (*private)
1.337 djm 633: *k = do_convert_private_ssh2(buf);
634: else if ((r = sshkey_fromb(buf, k)) != 0)
1.269 djm 635: fatal("decode blob failed: %s", ssh_err(r));
1.193 djm 636: fclose(fp);
637: }
638:
639: static void
1.252 djm 640: do_convert_from_pkcs8(struct sshkey **k, int *private)
1.193 djm 641: {
642: EVP_PKEY *pubkey;
643: FILE *fp;
644:
645: if ((fp = fopen(identity_file, "r")) == NULL)
646: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
647: if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
648: fatal("%s: %s is not a recognised public key format", __func__,
649: identity_file);
650: }
651: fclose(fp);
1.321 djm 652: switch (EVP_PKEY_base_id(pubkey)) {
1.193 djm 653: case EVP_PKEY_RSA:
1.252 djm 654: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
655: fatal("sshkey_new failed");
1.193 djm 656: (*k)->type = KEY_RSA;
657: (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
658: break;
659: case EVP_PKEY_DSA:
1.252 djm 660: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
661: fatal("sshkey_new failed");
1.193 djm 662: (*k)->type = KEY_DSA;
663: (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
664: break;
1.200 djm 665: case EVP_PKEY_EC:
1.252 djm 666: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
667: fatal("sshkey_new failed");
1.200 djm 668: (*k)->type = KEY_ECDSA;
669: (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
1.252 djm 670: (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
1.200 djm 671: break;
1.193 djm 672: default:
673: fatal("%s: unsupported pubkey type %d", __func__,
1.321 djm 674: EVP_PKEY_base_id(pubkey));
1.193 djm 675: }
676: EVP_PKEY_free(pubkey);
677: return;
678: }
679:
680: static void
1.252 djm 681: do_convert_from_pem(struct sshkey **k, int *private)
1.193 djm 682: {
683: FILE *fp;
684: RSA *rsa;
685:
686: if ((fp = fopen(identity_file, "r")) == NULL)
687: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
688: if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
1.252 djm 689: if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
690: fatal("sshkey_new failed");
1.193 djm 691: (*k)->type = KEY_RSA;
692: (*k)->rsa = rsa;
693: fclose(fp);
694: return;
695: }
696: fatal("%s: unrecognised raw private key format", __func__);
697: }
698:
699: static void
700: do_convert_from(struct passwd *pw)
701: {
1.252 djm 702: struct sshkey *k = NULL;
703: int r, private = 0, ok = 0;
1.193 djm 704: struct stat st;
705:
706: if (!have_identity)
707: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 708: if (stat(identity_file, &st) == -1)
1.193 djm 709: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
710:
711: switch (convert_format) {
712: case FMT_RFC4716:
713: do_convert_from_ssh2(pw, &k, &private);
714: break;
715: case FMT_PKCS8:
716: do_convert_from_pkcs8(&k, &private);
717: break;
718: case FMT_PEM:
719: do_convert_from_pem(&k, &private);
720: break;
721: default:
722: fatal("%s: unknown key format %d", __func__, convert_format);
723: }
724:
1.260 djm 725: if (!private) {
1.252 djm 726: if ((r = sshkey_write(k, stdout)) == 0)
727: ok = 1;
1.193 djm 728: if (ok)
729: fprintf(stdout, "\n");
1.260 djm 730: } else {
1.193 djm 731: switch (k->type) {
732: case KEY_DSA:
733: ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
734: NULL, 0, NULL, NULL);
735: break;
1.200 djm 736: case KEY_ECDSA:
737: ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
738: NULL, 0, NULL, NULL);
739: break;
1.193 djm 740: case KEY_RSA:
741: ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
742: NULL, 0, NULL, NULL);
743: break;
744: default:
745: fatal("%s: unsupported key type %s", __func__,
1.252 djm 746: sshkey_type(k));
1.193 djm 747: }
748: }
749:
1.269 djm 750: if (!ok)
751: fatal("key write failed");
1.252 djm 752: sshkey_free(k);
1.19 markus 753: exit(0);
754: }
1.246 markus 755: #endif
1.19 markus 756:
1.63 itojun 757: static void
1.19 markus 758: do_print_public(struct passwd *pw)
759: {
1.252 djm 760: struct sshkey *prv;
1.19 markus 761: struct stat st;
1.252 djm 762: int r;
1.341 djm 763: char *comment = NULL;
1.19 markus 764:
765: if (!have_identity)
766: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 767: if (stat(identity_file, &st) == -1)
1.269 djm 768: fatal("%s: %s", identity_file, strerror(errno));
1.341 djm 769: prv = load_identity(identity_file, &comment);
1.252 djm 770: if ((r = sshkey_write(prv, stdout)) != 0)
1.304 markus 771: error("sshkey_write failed: %s", ssh_err(r));
1.252 djm 772: sshkey_free(prv);
1.341 djm 773: if (comment != NULL && *comment != '\0')
774: fprintf(stdout, " %s", comment);
1.19 markus 775: fprintf(stdout, "\n");
1.341 djm 776: free(comment);
1.19 markus 777: exit(0);
778: }
779:
1.66 markus 780: static void
1.197 djm 781: do_download(struct passwd *pw)
1.75 markus 782: {
1.177 markus 783: #ifdef ENABLE_PKCS11
1.252 djm 784: struct sshkey **keys = NULL;
1.177 markus 785: int i, nkeys;
1.252 djm 786: enum sshkey_fp_rep rep;
1.251 djm 787: int fptype;
1.221 djm 788: char *fp, *ra;
1.222 djm 789:
1.251 djm 790: fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
791: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1.75 markus 792:
1.327 benno 793: pkcs11_init(1);
1.177 markus 794: nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
795: if (nkeys <= 0)
796: fatal("cannot read public key from pkcs11");
797: for (i = 0; i < nkeys; i++) {
1.221 djm 798: if (print_fingerprint) {
1.252 djm 799: fp = sshkey_fingerprint(keys[i], fptype, rep);
800: ra = sshkey_fingerprint(keys[i], fingerprint_hash,
1.221 djm 801: SSH_FP_RANDOMART);
1.259 djm 802: if (fp == NULL || ra == NULL)
803: fatal("%s: sshkey_fingerprint fail", __func__);
1.252 djm 804: printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
805: fp, sshkey_type(keys[i]));
1.325 djm 806: if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1.221 djm 807: printf("%s\n", ra);
1.227 djm 808: free(ra);
809: free(fp);
1.221 djm 810: } else {
1.252 djm 811: (void) sshkey_write(keys[i], stdout); /* XXX check */
1.221 djm 812: fprintf(stdout, "\n");
813: }
1.252 djm 814: sshkey_free(keys[i]);
1.97 markus 815: }
1.227 djm 816: free(keys);
1.177 markus 817: pkcs11_terminate();
1.75 markus 818: exit(0);
1.177 markus 819: #else
820: fatal("no pkcs11 support");
821: #endif /* ENABLE_PKCS11 */
1.75 markus 822: }
1.66 markus 823:
1.279 djm 824: static struct sshkey *
825: try_read_key(char **cpp)
826: {
827: struct sshkey *ret;
828: int r;
829:
830: if ((ret = sshkey_new(KEY_UNSPEC)) == NULL)
831: fatal("sshkey_new failed");
832: if ((r = sshkey_read(ret, cpp)) == 0)
833: return ret;
834: /* Not a key */
835: sshkey_free(ret);
836: return NULL;
837: }
838:
1.63 itojun 839: static void
1.279 djm 840: fingerprint_one_key(const struct sshkey *public, const char *comment)
1.8 markus 841: {
1.279 djm 842: char *fp = NULL, *ra = NULL;
1.252 djm 843: enum sshkey_fp_rep rep;
1.251 djm 844: int fptype;
1.12 markus 845:
1.251 djm 846: fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
847: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1.279 djm 848: fp = sshkey_fingerprint(public, fptype, rep);
849: ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART);
850: if (fp == NULL || ra == NULL)
851: fatal("%s: sshkey_fingerprint failed", __func__);
1.294 djm 852: mprintf("%u %s %s (%s)\n", sshkey_size(public), fp,
1.279 djm 853: comment ? comment : "no comment", sshkey_type(public));
1.325 djm 854: if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1.279 djm 855: printf("%s\n", ra);
856: free(ra);
857: free(fp);
858: }
859:
860: static void
861: fingerprint_private(const char *path)
862: {
863: struct stat st;
864: char *comment = NULL;
865: struct sshkey *public = NULL;
866: int r;
867:
1.333 deraadt 868: if (stat(identity_file, &st) == -1)
1.279 djm 869: fatal("%s: %s", path, strerror(errno));
1.280 djm 870: if ((r = sshkey_load_public(path, &public, &comment)) != 0) {
871: debug("load public \"%s\": %s", path, ssh_err(r));
872: if ((r = sshkey_load_private(path, NULL,
873: &public, &comment)) != 0) {
874: debug("load private \"%s\": %s", path, ssh_err(r));
875: fatal("%s is not a key file.", path);
876: }
877: }
878:
1.279 djm 879: fingerprint_one_key(public, comment);
880: sshkey_free(public);
881: free(comment);
882: }
883:
884: static void
885: do_fingerprint(struct passwd *pw)
886: {
887: FILE *f;
888: struct sshkey *public = NULL;
1.317 markus 889: char *comment = NULL, *cp, *ep, *line = NULL;
890: size_t linesize = 0;
1.279 djm 891: int i, invalid = 1;
892: const char *path;
1.289 djm 893: u_long lnum = 0;
1.279 djm 894:
1.12 markus 895: if (!have_identity)
896: ask_filename(pw, "Enter file in which the key is");
1.279 djm 897: path = identity_file;
1.15 markus 898:
1.279 djm 899: if (strcmp(identity_file, "-") == 0) {
900: f = stdin;
901: path = "(stdin)";
902: } else if ((f = fopen(path, "r")) == NULL)
903: fatal("%s: %s: %s", __progname, path, strerror(errno));
904:
1.317 markus 905: while (getline(&line, &linesize, f) != -1) {
906: lnum++;
1.279 djm 907: cp = line;
908: cp[strcspn(cp, "\n")] = '\0';
909: /* Trim leading space and comments */
910: cp = line + strspn(line, " \t");
911: if (*cp == '#' || *cp == '\0')
912: continue;
1.191 djm 913:
1.279 djm 914: /*
915: * Input may be plain keys, private keys, authorized_keys
916: * or known_hosts.
917: */
918:
919: /*
920: * Try private keys first. Assume a key is private if
921: * "SSH PRIVATE KEY" appears on the first line and we're
922: * not reading from stdin (XXX support private keys on stdin).
923: */
924: if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
1.280 djm 925: strstr(cp, "PRIVATE KEY") != NULL) {
1.317 markus 926: free(line);
1.279 djm 927: fclose(f);
928: fingerprint_private(path);
929: exit(0);
930: }
931:
932: /*
933: * If it's not a private key, then this must be prepared to
934: * accept a public key prefixed with a hostname or options.
935: * Try a bare key first, otherwise skip the leading stuff.
936: */
937: if ((public = try_read_key(&cp)) == NULL) {
938: i = strtol(cp, &ep, 10);
939: if (i == 0 || ep == NULL ||
940: (*ep != ' ' && *ep != '\t')) {
941: int quoted = 0;
942:
943: comment = cp;
944: for (; *cp && (quoted || (*cp != ' ' &&
945: *cp != '\t')); cp++) {
946: if (*cp == '\\' && cp[1] == '"')
947: cp++; /* Skip both */
948: else if (*cp == '"')
949: quoted = !quoted;
950: }
951: if (!*cp)
952: continue;
953: *cp++ = '\0';
954: }
1.191 djm 955: }
1.279 djm 956: /* Retry after parsing leading hostname/key options */
957: if (public == NULL && (public = try_read_key(&cp)) == NULL) {
1.289 djm 958: debug("%s:%lu: not a public key", path, lnum);
1.191 djm 959: continue;
960: }
961:
1.279 djm 962: /* Find trailing comment, if any */
963: for (; *cp == ' ' || *cp == '\t'; cp++)
1.191 djm 964: ;
1.279 djm 965: if (*cp != '\0' && *cp != '#')
1.191 djm 966: comment = cp;
1.279 djm 967:
968: fingerprint_one_key(public, comment);
1.252 djm 969: sshkey_free(public);
1.279 djm 970: invalid = 0; /* One good key in the file is sufficient */
1.15 markus 971: }
1.191 djm 972: fclose(f);
1.317 markus 973: free(line);
1.191 djm 974:
1.269 djm 975: if (invalid)
1.279 djm 976: fatal("%s is not a public key file.", path);
1.12 markus 977: exit(0);
1.8 markus 978: }
979:
1.119 djm 980: static void
1.206 stevesk 981: do_gen_all_hostkeys(struct passwd *pw)
982: {
983: struct {
984: char *key_type;
985: char *key_type_display;
986: char *path;
987: } key_types[] = {
1.267 djm 988: #ifdef WITH_OPENSSL
1.206 stevesk 989: { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
990: { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
991: { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
1.267 djm 992: #endif /* WITH_OPENSSL */
1.238 markus 993: { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
1.313 markus 994: #ifdef WITH_XMSS
995: { "xmss", "XMSS",_PATH_HOST_XMSS_KEY_FILE },
996: #endif /* WITH_XMSS */
1.206 stevesk 997: { NULL, NULL, NULL }
998: };
999:
1.340 dtucker 1000: u_int32_t bits = 0;
1.206 stevesk 1001: int first = 0;
1002: struct stat st;
1.252 djm 1003: struct sshkey *private, *public;
1.307 djm 1004: char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
1.252 djm 1005: int i, type, fd, r;
1.206 stevesk 1006: FILE *f;
1007:
1008: for (i = 0; key_types[i].key_type; i++) {
1.307 djm 1009: public = private = NULL;
1010: prv_tmp = pub_tmp = prv_file = pub_file = NULL;
1011:
1012: xasprintf(&prv_file, "%s%s",
1013: identity_file, key_types[i].path);
1014:
1015: /* Check whether private key exists and is not zero-length */
1016: if (stat(prv_file, &st) == 0) {
1017: if (st.st_size != 0)
1018: goto next;
1019: } else if (errno != ENOENT) {
1.269 djm 1020: error("Could not stat %s: %s", key_types[i].path,
1.206 stevesk 1021: strerror(errno));
1.307 djm 1022: goto failnext;
1.206 stevesk 1023: }
1024:
1.307 djm 1025: /*
1026: * Private key doesn't exist or is invalid; proceed with
1027: * key generation.
1028: */
1029: xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX",
1030: identity_file, key_types[i].path);
1031: xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX",
1032: identity_file, key_types[i].path);
1033: xasprintf(&pub_file, "%s%s.pub",
1034: identity_file, key_types[i].path);
1035:
1.206 stevesk 1036: if (first == 0) {
1037: first = 1;
1038: printf("%s: generating new host keys: ", __progname);
1039: }
1040: printf("%s ", key_types[i].key_type_display);
1041: fflush(stdout);
1.252 djm 1042: type = sshkey_type_from_name(key_types[i].key_type);
1.307 djm 1043: if ((fd = mkstemp(prv_tmp)) == -1) {
1044: error("Could not save your public key in %s: %s",
1045: prv_tmp, strerror(errno));
1046: goto failnext;
1047: }
1048: close(fd); /* just using mkstemp() to generate/reserve a name */
1.206 stevesk 1049: bits = 0;
1.255 djm 1050: type_bits_valid(type, NULL, &bits);
1.252 djm 1051: if ((r = sshkey_generate(type, bits, &private)) != 0) {
1.304 markus 1052: error("sshkey_generate failed: %s", ssh_err(r));
1.307 djm 1053: goto failnext;
1.206 stevesk 1054: }
1.252 djm 1055: if ((r = sshkey_from_private(private, &public)) != 0)
1056: fatal("sshkey_from_private failed: %s", ssh_err(r));
1.206 stevesk 1057: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
1058: hostname);
1.307 djm 1059: if ((r = sshkey_save_private(private, prv_tmp, "",
1.336 djm 1060: comment, private_key_format, openssh_format_cipher,
1061: rounds)) != 0) {
1.269 djm 1062: error("Saving key \"%s\" failed: %s",
1.307 djm 1063: prv_tmp, ssh_err(r));
1064: goto failnext;
1.206 stevesk 1065: }
1.307 djm 1066: if ((fd = mkstemp(pub_tmp)) == -1) {
1067: error("Could not save your public key in %s: %s",
1068: pub_tmp, strerror(errno));
1069: goto failnext;
1.206 stevesk 1070: }
1.307 djm 1071: (void)fchmod(fd, 0644);
1.206 stevesk 1072: f = fdopen(fd, "w");
1073: if (f == NULL) {
1.307 djm 1074: error("fdopen %s failed: %s", pub_tmp, strerror(errno));
1.250 doug 1075: close(fd);
1.307 djm 1076: goto failnext;
1.206 stevesk 1077: }
1.254 djm 1078: if ((r = sshkey_write(public, f)) != 0) {
1.269 djm 1079: error("write key failed: %s", ssh_err(r));
1.250 doug 1080: fclose(f);
1.307 djm 1081: goto failnext;
1082: }
1083: fprintf(f, " %s\n", comment);
1084: if (ferror(f) != 0) {
1085: error("write key failed: %s", strerror(errno));
1086: fclose(f);
1087: goto failnext;
1088: }
1089: if (fclose(f) != 0) {
1090: error("key close failed: %s", strerror(errno));
1091: goto failnext;
1092: }
1093:
1094: /* Rename temporary files to their permanent locations. */
1095: if (rename(pub_tmp, pub_file) != 0) {
1096: error("Unable to move %s into position: %s",
1097: pub_file, strerror(errno));
1098: goto failnext;
1099: }
1100: if (rename(prv_tmp, prv_file) != 0) {
1101: error("Unable to move %s into position: %s",
1102: key_types[i].path, strerror(errno));
1103: failnext:
1.206 stevesk 1104: first = 0;
1.307 djm 1105: goto next;
1.206 stevesk 1106: }
1.307 djm 1107: next:
1108: sshkey_free(private);
1.252 djm 1109: sshkey_free(public);
1.307 djm 1110: free(prv_tmp);
1111: free(pub_tmp);
1112: free(prv_file);
1113: free(pub_file);
1.206 stevesk 1114: }
1115: if (first != 0)
1116: printf("\n");
1117: }
1118:
1.256 djm 1119: struct known_hosts_ctx {
1.257 djm 1120: const char *host; /* Hostname searched for in find/delete case */
1121: FILE *out; /* Output file, stdout for find_hosts case */
1122: int has_unhashed; /* When hashing, original had unhashed hosts */
1123: int found_key; /* For find/delete, host was found */
1124: int invalid; /* File contained invalid items; don't delete */
1.325 djm 1125: int hash_hosts; /* Hash hostnames as we go */
1126: int find_host; /* Search for specific hostname */
1127: int delete_host; /* Delete host from known_hosts */
1.256 djm 1128: };
1129:
1130: static int
1131: known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
1.119 djm 1132: {
1.256 djm 1133: struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1134: char *hashed, *cp, *hosts, *ohosts;
1135: int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
1.298 dtucker 1136: int was_hashed = l->hosts && l->hosts[0] == HASH_DELIM;
1.256 djm 1137:
1.262 djm 1138: switch (l->status) {
1139: case HKF_STATUS_OK:
1140: case HKF_STATUS_MATCHED:
1141: /*
1142: * Don't hash hosts already already hashed, with wildcard
1143: * characters or a CA/revocation marker.
1144: */
1.296 djm 1145: if (was_hashed || has_wild || l->marker != MRK_NONE) {
1.262 djm 1146: fprintf(ctx->out, "%s\n", l->line);
1.325 djm 1147: if (has_wild && !ctx->find_host) {
1.297 dtucker 1148: logit("%s:%lu: ignoring host name "
1.269 djm 1149: "with wildcard: %.64s", l->path,
1.262 djm 1150: l->linenum, l->hosts);
1151: }
1152: return 0;
1153: }
1154: /*
1155: * Split any comma-separated hostnames from the host list,
1156: * hash and store separately.
1157: */
1158: ohosts = hosts = xstrdup(l->hosts);
1159: while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
1.299 djm 1160: lowercase(cp);
1.262 djm 1161: if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1162: fatal("hash_host failed");
1163: fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1164: ctx->has_unhashed = 1;
1165: }
1166: free(ohosts);
1167: return 0;
1168: case HKF_STATUS_INVALID:
1169: /* Retain invalid lines, but mark file as invalid. */
1.256 djm 1170: ctx->invalid = 1;
1.297 dtucker 1171: logit("%s:%lu: invalid line", l->path, l->linenum);
1.262 djm 1172: /* FALLTHROUGH */
1173: default:
1.256 djm 1174: fprintf(ctx->out, "%s\n", l->line);
1175: return 0;
1176: }
1.262 djm 1177: /* NOTREACHED */
1178: return -1;
1.256 djm 1179: }
1180:
1181: static int
1182: known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1183: {
1184: struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1.272 djm 1185: enum sshkey_fp_rep rep;
1186: int fptype;
1.338 djm 1187: char *fp = NULL, *ra = NULL;
1.272 djm 1188:
1189: fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
1190: rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1.256 djm 1191:
1.262 djm 1192: if (l->status == HKF_STATUS_MATCHED) {
1.325 djm 1193: if (ctx->delete_host) {
1.256 djm 1194: if (l->marker != MRK_NONE) {
1195: /* Don't remove CA and revocation lines */
1196: fprintf(ctx->out, "%s\n", l->line);
1197: } else {
1198: /*
1199: * Hostname matches and has no CA/revoke
1200: * marker, delete it by *not* writing the
1201: * line to ctx->out.
1202: */
1203: ctx->found_key = 1;
1204: if (!quiet)
1.297 dtucker 1205: printf("# Host %s found: line %lu\n",
1.256 djm 1206: ctx->host, l->linenum);
1207: }
1208: return 0;
1.325 djm 1209: } else if (ctx->find_host) {
1.256 djm 1210: ctx->found_key = 1;
1211: if (!quiet) {
1.297 dtucker 1212: printf("# Host %s found: line %lu %s\n",
1.256 djm 1213: ctx->host,
1214: l->linenum, l->marker == MRK_CA ? "CA" :
1215: (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1216: }
1.325 djm 1217: if (ctx->hash_hosts)
1.256 djm 1218: known_hosts_hash(l, ctx);
1.272 djm 1219: else if (print_fingerprint) {
1220: fp = sshkey_fingerprint(l->key, fptype, rep);
1.338 djm 1221: ra = sshkey_fingerprint(l->key,
1222: fingerprint_hash, SSH_FP_RANDOMART);
1223: if (fp == NULL || ra == NULL)
1224: fatal("%s: sshkey_fingerprint failed",
1225: __func__);
1.294 djm 1226: mprintf("%s %s %s %s\n", ctx->host,
1.272 djm 1227: sshkey_type(l->key), fp, l->comment);
1.338 djm 1228: if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1229: printf("%s\n", ra);
1230: free(ra);
1.272 djm 1231: free(fp);
1232: } else
1.256 djm 1233: fprintf(ctx->out, "%s\n", l->line);
1234: return 0;
1235: }
1.325 djm 1236: } else if (ctx->delete_host) {
1.256 djm 1237: /* Retain non-matching hosts when deleting */
1238: if (l->status == HKF_STATUS_INVALID) {
1239: ctx->invalid = 1;
1.297 dtucker 1240: logit("%s:%lu: invalid line", l->path, l->linenum);
1.256 djm 1241: }
1242: fprintf(ctx->out, "%s\n", l->line);
1.166 djm 1243: }
1.256 djm 1244: return 0;
1.119 djm 1245: }
1246:
1247: static void
1.325 djm 1248: do_known_hosts(struct passwd *pw, const char *name, int find_host,
1249: int delete_host, int hash_hosts)
1.119 djm 1250: {
1.258 deraadt 1251: char *cp, tmp[PATH_MAX], old[PATH_MAX];
1.257 djm 1252: int r, fd, oerrno, inplace = 0;
1.256 djm 1253: struct known_hosts_ctx ctx;
1.272 djm 1254: u_int foreach_options;
1.119 djm 1255:
1256: if (!have_identity) {
1257: cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1258: if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1259: sizeof(identity_file))
1260: fatal("Specified known hosts path too long");
1.227 djm 1261: free(cp);
1.119 djm 1262: have_identity = 1;
1263: }
1264:
1.256 djm 1265: memset(&ctx, 0, sizeof(ctx));
1266: ctx.out = stdout;
1267: ctx.host = name;
1.325 djm 1268: ctx.hash_hosts = hash_hosts;
1269: ctx.find_host = find_host;
1270: ctx.delete_host = delete_host;
1.256 djm 1271:
1.119 djm 1272: /*
1273: * Find hosts goes to stdout, hash and deletions happen in-place
1274: * A corner case is ssh-keygen -HF foo, which should go to stdout
1275: */
1276: if (!find_host && (hash_hosts || delete_host)) {
1277: if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1278: strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1279: strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1280: strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1281: fatal("known_hosts path too long");
1282: umask(077);
1.256 djm 1283: if ((fd = mkstemp(tmp)) == -1)
1.119 djm 1284: fatal("mkstemp: %s", strerror(errno));
1.256 djm 1285: if ((ctx.out = fdopen(fd, "w")) == NULL) {
1286: oerrno = errno;
1.119 djm 1287: unlink(tmp);
1.256 djm 1288: fatal("fdopen: %s", strerror(oerrno));
1.119 djm 1289: }
1.257 djm 1290: inplace = 1;
1.119 djm 1291: }
1.256 djm 1292: /* XXX support identity_file == "-" for stdin */
1.272 djm 1293: foreach_options = find_host ? HKF_WANT_MATCH : 0;
1294: foreach_options |= print_fingerprint ? HKF_WANT_PARSE_KEY : 0;
1.316 djm 1295: if ((r = hostkeys_foreach(identity_file, (find_host || !hash_hosts) ?
1.315 djm 1296: known_hosts_find_delete : known_hosts_hash, &ctx, name, NULL,
1297: foreach_options)) != 0) {
1.284 deraadt 1298: if (inplace)
1299: unlink(tmp);
1.256 djm 1300: fatal("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
1.284 deraadt 1301: }
1.119 djm 1302:
1.257 djm 1303: if (inplace)
1.256 djm 1304: fclose(ctx.out);
1.119 djm 1305:
1.256 djm 1306: if (ctx.invalid) {
1.269 djm 1307: error("%s is not a valid known_hosts file.", identity_file);
1.257 djm 1308: if (inplace) {
1.269 djm 1309: error("Not replacing existing known_hosts "
1310: "file because of errors");
1.119 djm 1311: unlink(tmp);
1312: }
1313: exit(1);
1.256 djm 1314: } else if (delete_host && !ctx.found_key) {
1.269 djm 1315: logit("Host %s not found in %s", name, identity_file);
1.277 djm 1316: if (inplace)
1317: unlink(tmp);
1.257 djm 1318: } else if (inplace) {
1.119 djm 1319: /* Backup existing file */
1320: if (unlink(old) == -1 && errno != ENOENT)
1321: fatal("unlink %.100s: %s", old, strerror(errno));
1322: if (link(identity_file, old) == -1)
1323: fatal("link %.100s to %.100s: %s", identity_file, old,
1324: strerror(errno));
1325: /* Move new one into place */
1326: if (rename(tmp, identity_file) == -1) {
1327: error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1328: strerror(errno));
1329: unlink(tmp);
1330: unlink(old);
1331: exit(1);
1332: }
1333:
1.269 djm 1334: printf("%s updated.\n", identity_file);
1335: printf("Original contents retained as %s\n", old);
1.256 djm 1336: if (ctx.has_unhashed) {
1.269 djm 1337: logit("WARNING: %s contains unhashed entries", old);
1338: logit("Delete this file to ensure privacy "
1339: "of hostnames");
1.119 djm 1340: }
1341: }
1342:
1.256 djm 1343: exit (find_host && !ctx.found_key);
1.119 djm 1344: }
1345:
1.13 deraadt 1346: /*
1347: * Perform changing a passphrase. The argument is the passwd structure
1348: * for the current user.
1349: */
1.63 itojun 1350: static void
1.7 markus 1351: do_change_passphrase(struct passwd *pw)
1352: {
1.12 markus 1353: char *comment;
1354: char *old_passphrase, *passphrase1, *passphrase2;
1355: struct stat st;
1.252 djm 1356: struct sshkey *private;
1357: int r;
1.12 markus 1358:
1359: if (!have_identity)
1360: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 1361: if (stat(identity_file, &st) == -1)
1.269 djm 1362: fatal("%s: %s", identity_file, strerror(errno));
1.12 markus 1363: /* Try to load the file with empty passphrase. */
1.252 djm 1364: r = sshkey_load_private(identity_file, "", &private, &comment);
1365: if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.12 markus 1366: if (identity_passphrase)
1367: old_passphrase = xstrdup(identity_passphrase);
1368: else
1.65 markus 1369: old_passphrase =
1370: read_passphrase("Enter old passphrase: ",
1371: RP_ALLOW_STDIN);
1.252 djm 1372: r = sshkey_load_private(identity_file, old_passphrase,
1373: &private, &comment);
1.240 djm 1374: explicit_bzero(old_passphrase, strlen(old_passphrase));
1.227 djm 1375: free(old_passphrase);
1.252 djm 1376: if (r != 0)
1377: goto badkey;
1378: } else if (r != 0) {
1379: badkey:
1.269 djm 1380: fatal("Failed to load key %s: %s", identity_file, ssh_err(r));
1.12 markus 1381: }
1.266 djm 1382: if (comment)
1.294 djm 1383: mprintf("Key has comment '%s'\n", comment);
1.12 markus 1384:
1385: /* Ask the new passphrase (twice). */
1386: if (identity_new_passphrase) {
1387: passphrase1 = xstrdup(identity_new_passphrase);
1388: passphrase2 = NULL;
1389: } else {
1390: passphrase1 =
1.65 markus 1391: read_passphrase("Enter new passphrase (empty for no "
1392: "passphrase): ", RP_ALLOW_STDIN);
1393: passphrase2 = read_passphrase("Enter same passphrase again: ",
1.86 deraadt 1394: RP_ALLOW_STDIN);
1.12 markus 1395:
1396: /* Verify that they are the same. */
1397: if (strcmp(passphrase1, passphrase2) != 0) {
1.240 djm 1398: explicit_bzero(passphrase1, strlen(passphrase1));
1399: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 1400: free(passphrase1);
1401: free(passphrase2);
1.12 markus 1402: printf("Pass phrases do not match. Try again.\n");
1403: exit(1);
1404: }
1405: /* Destroy the other copy. */
1.240 djm 1406: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 1407: free(passphrase2);
1.12 markus 1408: }
1409:
1410: /* Save the file using the new passphrase. */
1.252 djm 1411: if ((r = sshkey_save_private(private, identity_file, passphrase1,
1.336 djm 1412: comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
1.269 djm 1413: error("Saving key \"%s\" failed: %s.",
1.252 djm 1414: identity_file, ssh_err(r));
1.240 djm 1415: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 1416: free(passphrase1);
1.252 djm 1417: sshkey_free(private);
1.227 djm 1418: free(comment);
1.12 markus 1419: exit(1);
1420: }
1421: /* Destroy the passphrase and the copy of the key in memory. */
1.240 djm 1422: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 1423: free(passphrase1);
1.252 djm 1424: sshkey_free(private); /* Destroys contents */
1.227 djm 1425: free(comment);
1.1 deraadt 1426:
1.12 markus 1427: printf("Your identification has been saved with the new passphrase.\n");
1428: exit(0);
1.1 deraadt 1429: }
1430:
1.105 jakob 1431: /*
1432: * Print the SSHFP RR.
1433: */
1.138 jakob 1434: static int
1.325 djm 1435: do_print_resource_record(struct passwd *pw, char *fname, char *hname,
1436: int print_generic)
1.105 jakob 1437: {
1.252 djm 1438: struct sshkey *public;
1.105 jakob 1439: char *comment = NULL;
1440: struct stat st;
1.252 djm 1441: int r;
1.105 jakob 1442:
1.138 jakob 1443: if (fname == NULL)
1.229 djm 1444: fatal("%s: no filename", __func__);
1.333 deraadt 1445: if (stat(fname, &st) == -1) {
1.138 jakob 1446: if (errno == ENOENT)
1447: return 0;
1.269 djm 1448: fatal("%s: %s", fname, strerror(errno));
1.105 jakob 1449: }
1.269 djm 1450: if ((r = sshkey_load_public(fname, &public, &comment)) != 0)
1451: fatal("Failed to read v2 public key from \"%s\": %s.",
1.252 djm 1452: fname, ssh_err(r));
1453: export_dns_rr(hname, public, stdout, print_generic);
1454: sshkey_free(public);
1455: free(comment);
1456: return 1;
1.105 jakob 1457: }
1458:
1.13 deraadt 1459: /*
1460: * Change the comment of a private key file.
1461: */
1.63 itojun 1462: static void
1.325 djm 1463: do_change_comment(struct passwd *pw, const char *identity_comment)
1.1 deraadt 1464: {
1.46 deraadt 1465: char new_comment[1024], *comment, *passphrase;
1.252 djm 1466: struct sshkey *private;
1467: struct sshkey *public;
1.12 markus 1468: struct stat st;
1469: FILE *f;
1.252 djm 1470: int r, fd;
1.12 markus 1471:
1472: if (!have_identity)
1473: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 1474: if (stat(identity_file, &st) == -1)
1.269 djm 1475: fatal("%s: %s", identity_file, strerror(errno));
1.252 djm 1476: if ((r = sshkey_load_private(identity_file, "",
1477: &private, &comment)) == 0)
1478: passphrase = xstrdup("");
1.269 djm 1479: else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
1480: fatal("Cannot load private key \"%s\": %s.",
1.252 djm 1481: identity_file, ssh_err(r));
1.269 djm 1482: else {
1.12 markus 1483: if (identity_passphrase)
1484: passphrase = xstrdup(identity_passphrase);
1485: else if (identity_new_passphrase)
1486: passphrase = xstrdup(identity_new_passphrase);
1487: else
1.65 markus 1488: passphrase = read_passphrase("Enter passphrase: ",
1489: RP_ALLOW_STDIN);
1.12 markus 1490: /* Try to load using the passphrase. */
1.252 djm 1491: if ((r = sshkey_load_private(identity_file, passphrase,
1492: &private, &comment)) != 0) {
1.240 djm 1493: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1494: free(passphrase);
1.269 djm 1495: fatal("Cannot load private key \"%s\": %s.",
1.252 djm 1496: identity_file, ssh_err(r));
1.12 markus 1497: }
1498: }
1.283 halex 1499:
1.313 markus 1500: if (private->type != KEY_ED25519 && private->type != KEY_XMSS &&
1.336 djm 1501: private_key_format != SSHKEY_PRIVATE_OPENSSH) {
1.302 djm 1502: error("Comments are only supported for keys stored in "
1.283 halex 1503: "the new format (-o).");
1.268 tobias 1504: explicit_bzero(passphrase, strlen(passphrase));
1.252 djm 1505: sshkey_free(private);
1.52 markus 1506: exit(1);
1.86 deraadt 1507: }
1.293 millert 1508: if (comment)
1.330 lum 1509: printf("Old comment: %s\n", comment);
1.293 millert 1510: else
1.330 lum 1511: printf("No existing comment\n");
1.12 markus 1512:
1513: if (identity_comment) {
1514: strlcpy(new_comment, identity_comment, sizeof(new_comment));
1515: } else {
1.330 lum 1516: printf("New comment: ");
1.12 markus 1517: fflush(stdout);
1518: if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1.240 djm 1519: explicit_bzero(passphrase, strlen(passphrase));
1.252 djm 1520: sshkey_free(private);
1.12 markus 1521: exit(1);
1522: }
1.162 gilles 1523: new_comment[strcspn(new_comment, "\n")] = '\0';
1.12 markus 1524: }
1.330 lum 1525: if (comment != NULL && strcmp(comment, new_comment) == 0) {
1526: printf("No change to comment\n");
1527: free(passphrase);
1528: sshkey_free(private);
1529: free(comment);
1530: exit(0);
1531: }
1.12 markus 1532:
1533: /* Save the file using the new passphrase. */
1.252 djm 1534: if ((r = sshkey_save_private(private, identity_file, passphrase,
1.336 djm 1535: new_comment, private_key_format, openssh_format_cipher,
1536: rounds)) != 0) {
1.269 djm 1537: error("Saving key \"%s\" failed: %s",
1.252 djm 1538: identity_file, ssh_err(r));
1.240 djm 1539: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1540: free(passphrase);
1.252 djm 1541: sshkey_free(private);
1.227 djm 1542: free(comment);
1.12 markus 1543: exit(1);
1544: }
1.240 djm 1545: explicit_bzero(passphrase, strlen(passphrase));
1.227 djm 1546: free(passphrase);
1.252 djm 1547: if ((r = sshkey_from_private(private, &public)) != 0)
1.304 markus 1548: fatal("sshkey_from_private failed: %s", ssh_err(r));
1.252 djm 1549: sshkey_free(private);
1.12 markus 1550:
1551: strlcat(identity_file, ".pub", sizeof(identity_file));
1.46 deraadt 1552: fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1.269 djm 1553: if (fd == -1)
1554: fatal("Could not save your public key in %s", identity_file);
1.46 deraadt 1555: f = fdopen(fd, "w");
1.269 djm 1556: if (f == NULL)
1557: fatal("fdopen %s failed: %s", identity_file, strerror(errno));
1.252 djm 1558: if ((r = sshkey_write(public, f)) != 0)
1.269 djm 1559: fatal("write key failed: %s", ssh_err(r));
1.252 djm 1560: sshkey_free(public);
1.19 markus 1561: fprintf(f, " %s\n", new_comment);
1.12 markus 1562: fclose(f);
1.1 deraadt 1563:
1.227 djm 1564: free(comment);
1.1 deraadt 1565:
1.330 lum 1566: if (strlen(new_comment) > 0)
1567: printf("Comment '%s' applied\n", new_comment);
1568: else
1569: printf("Comment removed\n");
1570:
1.12 markus 1571: exit(0);
1.1 deraadt 1572: }
1573:
1.179 djm 1574: static void
1.252 djm 1575: add_flag_option(struct sshbuf *c, const char *name)
1.179 djm 1576: {
1.252 djm 1577: int r;
1578:
1.179 djm 1579: debug3("%s: %s", __func__, name);
1.252 djm 1580: if ((r = sshbuf_put_cstring(c, name)) != 0 ||
1581: (r = sshbuf_put_string(c, NULL, 0)) != 0)
1582: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.179 djm 1583: }
1584:
1585: static void
1.252 djm 1586: add_string_option(struct sshbuf *c, const char *name, const char *value)
1.179 djm 1587: {
1.252 djm 1588: struct sshbuf *b;
1589: int r;
1.179 djm 1590:
1591: debug3("%s: %s=%s", __func__, name, value);
1.252 djm 1592: if ((b = sshbuf_new()) == NULL)
1593: fatal("%s: sshbuf_new failed", __func__);
1594: if ((r = sshbuf_put_cstring(b, value)) != 0 ||
1595: (r = sshbuf_put_cstring(c, name)) != 0 ||
1596: (r = sshbuf_put_stringb(c, b)) != 0)
1597: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.179 djm 1598:
1.252 djm 1599: sshbuf_free(b);
1.179 djm 1600: }
1601:
1.190 djm 1602: #define OPTIONS_CRITICAL 1
1603: #define OPTIONS_EXTENSIONS 2
1.179 djm 1604: static void
1.252 djm 1605: prepare_options_buf(struct sshbuf *c, int which)
1.179 djm 1606: {
1.300 djm 1607: size_t i;
1608:
1.252 djm 1609: sshbuf_reset(c);
1.196 djm 1610: if ((which & OPTIONS_CRITICAL) != 0 &&
1611: certflags_command != NULL)
1612: add_string_option(c, "force-command", certflags_command);
1.190 djm 1613: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1.210 djm 1614: (certflags_flags & CERTOPT_X_FWD) != 0)
1615: add_flag_option(c, "permit-X11-forwarding");
1616: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1.190 djm 1617: (certflags_flags & CERTOPT_AGENT_FWD) != 0)
1.186 djm 1618: add_flag_option(c, "permit-agent-forwarding");
1.190 djm 1619: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1620: (certflags_flags & CERTOPT_PORT_FWD) != 0)
1.186 djm 1621: add_flag_option(c, "permit-port-forwarding");
1.190 djm 1622: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1623: (certflags_flags & CERTOPT_PTY) != 0)
1.186 djm 1624: add_flag_option(c, "permit-pty");
1.190 djm 1625: if ((which & OPTIONS_EXTENSIONS) != 0 &&
1626: (certflags_flags & CERTOPT_USER_RC) != 0)
1.186 djm 1627: add_flag_option(c, "permit-user-rc");
1.190 djm 1628: if ((which & OPTIONS_CRITICAL) != 0 &&
1629: certflags_src_addr != NULL)
1630: add_string_option(c, "source-address", certflags_src_addr);
1.300 djm 1631: for (i = 0; i < ncert_userext; i++) {
1632: if ((cert_userext[i].crit && (which & OPTIONS_EXTENSIONS)) ||
1633: (!cert_userext[i].crit && (which & OPTIONS_CRITICAL)))
1634: continue;
1635: if (cert_userext[i].val == NULL)
1636: add_flag_option(c, cert_userext[i].key);
1637: else {
1638: add_string_option(c, cert_userext[i].key,
1639: cert_userext[i].val);
1640: }
1641: }
1.179 djm 1642: }
1643:
1.252 djm 1644: static struct sshkey *
1.197 djm 1645: load_pkcs11_key(char *path)
1646: {
1647: #ifdef ENABLE_PKCS11
1.252 djm 1648: struct sshkey **keys = NULL, *public, *private = NULL;
1649: int r, i, nkeys;
1.197 djm 1650:
1.252 djm 1651: if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1652: fatal("Couldn't load CA public key \"%s\": %s",
1653: path, ssh_err(r));
1.197 djm 1654:
1655: nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1656: debug3("%s: %d keys", __func__, nkeys);
1657: if (nkeys <= 0)
1658: fatal("cannot read public key from pkcs11");
1659: for (i = 0; i < nkeys; i++) {
1.252 djm 1660: if (sshkey_equal_public(public, keys[i])) {
1.197 djm 1661: private = keys[i];
1662: continue;
1663: }
1.252 djm 1664: sshkey_free(keys[i]);
1.197 djm 1665: }
1.227 djm 1666: free(keys);
1.252 djm 1667: sshkey_free(public);
1.197 djm 1668: return private;
1669: #else
1670: fatal("no pkcs11 support");
1671: #endif /* ENABLE_PKCS11 */
1672: }
1673:
1.305 djm 1674: /* Signer for sshkey_certify_custom that uses the agent */
1675: static int
1.332 djm 1676: agent_signer(struct sshkey *key, u_char **sigp, size_t *lenp,
1.305 djm 1677: const u_char *data, size_t datalen,
1678: const char *alg, u_int compat, void *ctx)
1679: {
1680: int *agent_fdp = (int *)ctx;
1681:
1682: return ssh_agent_sign(*agent_fdp, key, sigp, lenp,
1683: data, datalen, alg, compat);
1684: }
1685:
1.179 djm 1686: static void
1.325 djm 1687: do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
1.326 djm 1688: unsigned long long cert_serial, int cert_serial_autoinc,
1689: int argc, char **argv)
1.179 djm 1690: {
1.305 djm 1691: int r, i, fd, found, agent_fd = -1;
1.179 djm 1692: u_int n;
1.252 djm 1693: struct sshkey *ca, *public;
1.281 djm 1694: char valid[64], *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1.179 djm 1695: FILE *f;
1.305 djm 1696: struct ssh_identitylist *agent_ids;
1697: size_t j;
1.186 djm 1698:
1.246 markus 1699: #ifdef ENABLE_PKCS11
1.197 djm 1700: pkcs11_init(1);
1.246 markus 1701: #endif
1.197 djm 1702: tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1703: if (pkcs11provider != NULL) {
1.305 djm 1704: /* If a PKCS#11 token was specified then try to use it */
1.197 djm 1705: if ((ca = load_pkcs11_key(tmp)) == NULL)
1706: fatal("No PKCS#11 key matching %s found", ca_key_path);
1.305 djm 1707: } else if (prefer_agent) {
1708: /*
1709: * Agent signature requested. Try to use agent after making
1710: * sure the public key specified is actually present in the
1711: * agent.
1712: */
1713: if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
1714: fatal("Cannot load CA public key %s: %s",
1715: tmp, ssh_err(r));
1716: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
1717: fatal("Cannot use public key for CA signature: %s",
1718: ssh_err(r));
1719: if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0)
1720: fatal("Retrieve agent key list: %s", ssh_err(r));
1721: found = 0;
1722: for (j = 0; j < agent_ids->nkeys; j++) {
1723: if (sshkey_equal(ca, agent_ids->keys[j])) {
1724: found = 1;
1725: break;
1726: }
1727: }
1728: if (!found)
1729: fatal("CA key %s not found in agent", tmp);
1730: ssh_free_identitylist(agent_ids);
1731: ca->flags |= SSHKEY_FLAG_EXT;
1732: } else {
1733: /* CA key is assumed to be a private key on the filesystem */
1.341 djm 1734: ca = load_identity(tmp, NULL);
1.305 djm 1735: }
1.227 djm 1736: free(tmp);
1.197 djm 1737:
1.290 djm 1738: if (key_type_name != NULL &&
1739: sshkey_type_from_name(key_type_name) != ca->type) {
1740: fatal("CA key type %s doesn't match specified %s",
1741: sshkey_ssh_name(ca), key_type_name);
1742: }
1743:
1.179 djm 1744: for (i = 0; i < argc; i++) {
1745: /* Split list of principals */
1746: n = 0;
1747: if (cert_principals != NULL) {
1748: otmp = tmp = xstrdup(cert_principals);
1749: plist = NULL;
1750: for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1.270 deraadt 1751: plist = xreallocarray(plist, n + 1, sizeof(*plist));
1.179 djm 1752: if (*(plist[n] = xstrdup(cp)) == '\0')
1753: fatal("Empty principal name");
1754: }
1.227 djm 1755: free(otmp);
1.179 djm 1756: }
1.312 djm 1757: if (n > SSHKEY_CERT_MAX_PRINCIPALS)
1758: fatal("Too many certificate principals specified");
1.337 djm 1759:
1.179 djm 1760: tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1.252 djm 1761: if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1762: fatal("%s: unable to open \"%s\": %s",
1763: __func__, tmp, ssh_err(r));
1.200 djm 1764: if (public->type != KEY_RSA && public->type != KEY_DSA &&
1.313 markus 1765: public->type != KEY_ECDSA && public->type != KEY_ED25519 &&
1766: public->type != KEY_XMSS)
1.179 djm 1767: fatal("%s: key \"%s\" type %s cannot be certified",
1.252 djm 1768: __func__, tmp, sshkey_type(public));
1.179 djm 1769:
1770: /* Prepare certificate to sign */
1.275 djm 1771: if ((r = sshkey_to_certified(public)) != 0)
1.252 djm 1772: fatal("Could not upgrade key %s to certificate: %s",
1773: tmp, ssh_err(r));
1.179 djm 1774: public->cert->type = cert_key_type;
1.186 djm 1775: public->cert->serial = (u_int64_t)cert_serial;
1.179 djm 1776: public->cert->key_id = xstrdup(cert_key_id);
1777: public->cert->nprincipals = n;
1778: public->cert->principals = plist;
1779: public->cert->valid_after = cert_valid_from;
1780: public->cert->valid_before = cert_valid_to;
1.275 djm 1781: prepare_options_buf(public->cert->critical, OPTIONS_CRITICAL);
1782: prepare_options_buf(public->cert->extensions,
1783: OPTIONS_EXTENSIONS);
1.252 djm 1784: if ((r = sshkey_from_private(ca,
1785: &public->cert->signature_key)) != 0)
1.304 markus 1786: fatal("sshkey_from_private (ca key): %s", ssh_err(r));
1.179 djm 1787:
1.305 djm 1788: if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) {
1789: if ((r = sshkey_certify_custom(public, ca,
1790: key_type_name, agent_signer, &agent_fd)) != 0)
1791: fatal("Couldn't certify key %s via agent: %s",
1792: tmp, ssh_err(r));
1793: } else {
1794: if ((sshkey_certify(public, ca, key_type_name)) != 0)
1795: fatal("Couldn't certify key %s: %s",
1796: tmp, ssh_err(r));
1797: }
1.179 djm 1798:
1799: if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1800: *cp = '\0';
1801: xasprintf(&out, "%s-cert.pub", tmp);
1.227 djm 1802: free(tmp);
1.179 djm 1803:
1804: if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1805: fatal("Could not open \"%s\" for writing: %s", out,
1806: strerror(errno));
1807: if ((f = fdopen(fd, "w")) == NULL)
1808: fatal("%s: fdopen: %s", __func__, strerror(errno));
1.252 djm 1809: if ((r = sshkey_write(public, f)) != 0)
1810: fatal("Could not write certified key to %s: %s",
1811: out, ssh_err(r));
1.179 djm 1812: fprintf(f, " %s\n", comment);
1813: fclose(f);
1814:
1.186 djm 1815: if (!quiet) {
1.282 djm 1816: sshkey_format_cert_validity(public->cert,
1.281 djm 1817: valid, sizeof(valid));
1.186 djm 1818: logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1.282 djm 1819: "valid %s", sshkey_cert_type(public),
1.205 djm 1820: out, public->cert->key_id,
1821: (unsigned long long)public->cert->serial,
1.179 djm 1822: cert_principals != NULL ? " for " : "",
1823: cert_principals != NULL ? cert_principals : "",
1.281 djm 1824: valid);
1.186 djm 1825: }
1.179 djm 1826:
1.252 djm 1827: sshkey_free(public);
1.227 djm 1828: free(out);
1.326 djm 1829: if (cert_serial_autoinc)
1830: cert_serial++;
1.179 djm 1831: }
1.246 markus 1832: #ifdef ENABLE_PKCS11
1.197 djm 1833: pkcs11_terminate();
1.246 markus 1834: #endif
1.179 djm 1835: exit(0);
1836: }
1837:
1838: static u_int64_t
1839: parse_relative_time(const char *s, time_t now)
1840: {
1841: int64_t mul, secs;
1842:
1843: mul = *s == '-' ? -1 : 1;
1844:
1845: if ((secs = convtime(s + 1)) == -1)
1846: fatal("Invalid relative certificate time %s", s);
1847: if (mul == -1 && secs > now)
1848: fatal("Certificate time %s cannot be represented", s);
1849: return now + (u_int64_t)(secs * mul);
1850: }
1851:
1852: static void
1853: parse_cert_times(char *timespec)
1854: {
1855: char *from, *to;
1856: time_t now = time(NULL);
1857: int64_t secs;
1858:
1859: /* +timespec relative to now */
1860: if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1861: if ((secs = convtime(timespec + 1)) == -1)
1862: fatal("Invalid relative certificate life %s", timespec);
1863: cert_valid_to = now + secs;
1864: /*
1865: * Backdate certificate one minute to avoid problems on hosts
1866: * with poorly-synchronised clocks.
1867: */
1868: cert_valid_from = ((now - 59)/ 60) * 60;
1869: return;
1870: }
1871:
1872: /*
1873: * from:to, where
1.308 djm 1874: * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "always"
1875: * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | "forever"
1.179 djm 1876: */
1877: from = xstrdup(timespec);
1878: to = strchr(from, ':');
1879: if (to == NULL || from == to || *(to + 1) == '\0')
1.181 djm 1880: fatal("Invalid certificate life specification %s", timespec);
1.179 djm 1881: *to++ = '\0';
1882:
1883: if (*from == '-' || *from == '+')
1884: cert_valid_from = parse_relative_time(from, now);
1.308 djm 1885: else if (strcmp(from, "always") == 0)
1886: cert_valid_from = 0;
1.314 djm 1887: else if (parse_absolute_time(from, &cert_valid_from) != 0)
1888: fatal("Invalid from time \"%s\"", from);
1.179 djm 1889:
1890: if (*to == '-' || *to == '+')
1.235 djm 1891: cert_valid_to = parse_relative_time(to, now);
1.308 djm 1892: else if (strcmp(to, "forever") == 0)
1893: cert_valid_to = ~(u_int64_t)0;
1.314 djm 1894: else if (parse_absolute_time(to, &cert_valid_to) != 0)
1895: fatal("Invalid to time \"%s\"", to);
1.179 djm 1896:
1897: if (cert_valid_to <= cert_valid_from)
1898: fatal("Empty certificate validity interval");
1.227 djm 1899: free(from);
1.179 djm 1900: }
1901:
1902: static void
1.186 djm 1903: add_cert_option(char *opt)
1.179 djm 1904: {
1.300 djm 1905: char *val, *cp;
1906: int iscrit = 0;
1.179 djm 1907:
1.208 stevesk 1908: if (strcasecmp(opt, "clear") == 0)
1.190 djm 1909: certflags_flags = 0;
1.179 djm 1910: else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1.190 djm 1911: certflags_flags &= ~CERTOPT_X_FWD;
1.179 djm 1912: else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1.190 djm 1913: certflags_flags |= CERTOPT_X_FWD;
1.179 djm 1914: else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1.190 djm 1915: certflags_flags &= ~CERTOPT_AGENT_FWD;
1.179 djm 1916: else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1.190 djm 1917: certflags_flags |= CERTOPT_AGENT_FWD;
1.179 djm 1918: else if (strcasecmp(opt, "no-port-forwarding") == 0)
1.190 djm 1919: certflags_flags &= ~CERTOPT_PORT_FWD;
1.179 djm 1920: else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1.190 djm 1921: certflags_flags |= CERTOPT_PORT_FWD;
1.179 djm 1922: else if (strcasecmp(opt, "no-pty") == 0)
1.190 djm 1923: certflags_flags &= ~CERTOPT_PTY;
1.179 djm 1924: else if (strcasecmp(opt, "permit-pty") == 0)
1.190 djm 1925: certflags_flags |= CERTOPT_PTY;
1.179 djm 1926: else if (strcasecmp(opt, "no-user-rc") == 0)
1.190 djm 1927: certflags_flags &= ~CERTOPT_USER_RC;
1.179 djm 1928: else if (strcasecmp(opt, "permit-user-rc") == 0)
1.190 djm 1929: certflags_flags |= CERTOPT_USER_RC;
1.179 djm 1930: else if (strncasecmp(opt, "force-command=", 14) == 0) {
1931: val = opt + 14;
1932: if (*val == '\0')
1.186 djm 1933: fatal("Empty force-command option");
1.190 djm 1934: if (certflags_command != NULL)
1.179 djm 1935: fatal("force-command already specified");
1.190 djm 1936: certflags_command = xstrdup(val);
1.179 djm 1937: } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1938: val = opt + 15;
1939: if (*val == '\0')
1.186 djm 1940: fatal("Empty source-address option");
1.190 djm 1941: if (certflags_src_addr != NULL)
1.179 djm 1942: fatal("source-address already specified");
1943: if (addr_match_cidr_list(NULL, val) != 0)
1944: fatal("Invalid source-address list");
1.190 djm 1945: certflags_src_addr = xstrdup(val);
1.300 djm 1946: } else if (strncasecmp(opt, "extension:", 10) == 0 ||
1947: (iscrit = (strncasecmp(opt, "critical:", 9) == 0))) {
1948: val = xstrdup(strchr(opt, ':') + 1);
1949: if ((cp = strchr(val, '=')) != NULL)
1950: *cp++ = '\0';
1951: cert_userext = xreallocarray(cert_userext, ncert_userext + 1,
1952: sizeof(*cert_userext));
1953: cert_userext[ncert_userext].key = val;
1954: cert_userext[ncert_userext].val = cp == NULL ?
1955: NULL : xstrdup(cp);
1956: cert_userext[ncert_userext].crit = iscrit;
1957: ncert_userext++;
1.179 djm 1958: } else
1.186 djm 1959: fatal("Unsupported certificate option \"%s\"", opt);
1.179 djm 1960: }
1961:
1.63 itojun 1962: static void
1.275 djm 1963: show_options(struct sshbuf *optbuf, int in_critical)
1.192 djm 1964: {
1.245 djm 1965: char *name, *arg;
1.252 djm 1966: struct sshbuf *options, *option = NULL;
1967: int r;
1968:
1969: if ((options = sshbuf_fromb(optbuf)) == NULL)
1970: fatal("%s: sshbuf_fromb failed", __func__);
1971: while (sshbuf_len(options) != 0) {
1972: sshbuf_free(option);
1973: option = NULL;
1974: if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
1975: (r = sshbuf_froms(options, &option)) != 0)
1976: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.192 djm 1977: printf(" %s", name);
1.275 djm 1978: if (!in_critical &&
1.192 djm 1979: (strcmp(name, "permit-X11-forwarding") == 0 ||
1980: strcmp(name, "permit-agent-forwarding") == 0 ||
1981: strcmp(name, "permit-port-forwarding") == 0 ||
1982: strcmp(name, "permit-pty") == 0 ||
1983: strcmp(name, "permit-user-rc") == 0))
1984: printf("\n");
1.275 djm 1985: else if (in_critical &&
1.192 djm 1986: (strcmp(name, "force-command") == 0 ||
1987: strcmp(name, "source-address") == 0)) {
1.252 djm 1988: if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
1989: fatal("%s: buffer error: %s",
1990: __func__, ssh_err(r));
1.245 djm 1991: printf(" %s\n", arg);
1992: free(arg);
1.192 djm 1993: } else {
1.252 djm 1994: printf(" UNKNOWN OPTION (len %zu)\n",
1995: sshbuf_len(option));
1996: sshbuf_reset(option);
1.192 djm 1997: }
1.227 djm 1998: free(name);
1.252 djm 1999: if (sshbuf_len(option) != 0)
1.192 djm 2000: fatal("Option corrupt: extra data at end");
2001: }
1.252 djm 2002: sshbuf_free(option);
2003: sshbuf_free(options);
1.192 djm 2004: }
2005:
2006: static void
1.278 djm 2007: print_cert(struct sshkey *key)
1.182 djm 2008: {
1.281 djm 2009: char valid[64], *key_fp, *ca_fp;
1.275 djm 2010: u_int i;
1.186 djm 2011:
1.252 djm 2012: key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
2013: ca_fp = sshkey_fingerprint(key->cert->signature_key,
1.251 djm 2014: fingerprint_hash, SSH_FP_DEFAULT);
1.259 djm 2015: if (key_fp == NULL || ca_fp == NULL)
2016: fatal("%s: sshkey_fingerprint fail", __func__);
1.281 djm 2017: sshkey_format_cert_validity(key->cert, valid, sizeof(valid));
1.182 djm 2018:
1.252 djm 2019: printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
2020: sshkey_cert_type(key));
2021: printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
1.323 djm 2022: printf(" Signing CA: %s %s (using %s)\n",
2023: sshkey_type(key->cert->signature_key), ca_fp,
2024: key->cert->signature_type);
1.186 djm 2025: printf(" Key ID: \"%s\"\n", key->cert->key_id);
1.275 djm 2026: printf(" Serial: %llu\n", (unsigned long long)key->cert->serial);
1.281 djm 2027: printf(" Valid: %s\n", valid);
1.182 djm 2028: printf(" Principals: ");
2029: if (key->cert->nprincipals == 0)
2030: printf("(none)\n");
2031: else {
2032: for (i = 0; i < key->cert->nprincipals; i++)
2033: printf("\n %s",
2034: key->cert->principals[i]);
2035: printf("\n");
2036: }
1.186 djm 2037: printf(" Critical Options: ");
1.252 djm 2038: if (sshbuf_len(key->cert->critical) == 0)
1.182 djm 2039: printf("(none)\n");
2040: else {
2041: printf("\n");
1.275 djm 2042: show_options(key->cert->critical, 1);
1.186 djm 2043: }
1.275 djm 2044: printf(" Extensions: ");
2045: if (sshbuf_len(key->cert->extensions) == 0)
2046: printf("(none)\n");
2047: else {
2048: printf("\n");
2049: show_options(key->cert->extensions, 0);
1.182 djm 2050: }
1.278 djm 2051: }
2052:
2053: static void
2054: do_show_cert(struct passwd *pw)
2055: {
2056: struct sshkey *key = NULL;
2057: struct stat st;
2058: int r, is_stdin = 0, ok = 0;
2059: FILE *f;
1.317 markus 2060: char *cp, *line = NULL;
1.278 djm 2061: const char *path;
1.317 markus 2062: size_t linesize = 0;
1.289 djm 2063: u_long lnum = 0;
1.278 djm 2064:
2065: if (!have_identity)
2066: ask_filename(pw, "Enter file in which the key is");
1.333 deraadt 2067: if (strcmp(identity_file, "-") != 0 && stat(identity_file, &st) == -1)
1.278 djm 2068: fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
2069:
2070: path = identity_file;
2071: if (strcmp(path, "-") == 0) {
2072: f = stdin;
2073: path = "(stdin)";
2074: is_stdin = 1;
2075: } else if ((f = fopen(identity_file, "r")) == NULL)
2076: fatal("fopen %s: %s", identity_file, strerror(errno));
2077:
1.317 markus 2078: while (getline(&line, &linesize, f) != -1) {
2079: lnum++;
1.278 djm 2080: sshkey_free(key);
2081: key = NULL;
2082: /* Trim leading space and comments */
2083: cp = line + strspn(line, " \t");
2084: if (*cp == '#' || *cp == '\0')
2085: continue;
2086: if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
1.304 markus 2087: fatal("sshkey_new");
1.278 djm 2088: if ((r = sshkey_read(key, &cp)) != 0) {
2089: error("%s:%lu: invalid key: %s", path,
2090: lnum, ssh_err(r));
2091: continue;
2092: }
2093: if (!sshkey_is_cert(key)) {
2094: error("%s:%lu is not a certificate", path, lnum);
2095: continue;
2096: }
2097: ok = 1;
2098: if (!is_stdin && lnum == 1)
2099: printf("%s:\n", path);
2100: else
2101: printf("%s:%lu:\n", path, lnum);
2102: print_cert(key);
2103: }
1.317 markus 2104: free(line);
1.278 djm 2105: sshkey_free(key);
2106: fclose(f);
2107: exit(ok ? 0 : 1);
1.182 djm 2108: }
2109:
1.246 markus 2110: #ifdef WITH_OPENSSL
1.182 djm 2111: static void
1.223 djm 2112: load_krl(const char *path, struct ssh_krl **krlp)
2113: {
1.252 djm 2114: struct sshbuf *krlbuf;
2115: int r, fd;
1.223 djm 2116:
1.252 djm 2117: if ((krlbuf = sshbuf_new()) == NULL)
2118: fatal("sshbuf_new failed");
1.223 djm 2119: if ((fd = open(path, O_RDONLY)) == -1)
2120: fatal("open %s: %s", path, strerror(errno));
1.252 djm 2121: if ((r = sshkey_load_file(fd, krlbuf)) != 0)
2122: fatal("Unable to load KRL: %s", ssh_err(r));
1.223 djm 2123: close(fd);
2124: /* XXX check sigs */
1.252 djm 2125: if ((r = ssh_krl_from_blob(krlbuf, krlp, NULL, 0)) != 0 ||
1.223 djm 2126: *krlp == NULL)
1.252 djm 2127: fatal("Invalid KRL file: %s", ssh_err(r));
2128: sshbuf_free(krlbuf);
1.223 djm 2129: }
2130:
2131: static void
1.320 djm 2132: hash_to_blob(const char *cp, u_char **blobp, size_t *lenp,
2133: const char *file, u_long lnum)
2134: {
2135: char *tmp;
2136: size_t tlen;
2137: struct sshbuf *b;
2138: int r;
2139:
2140: if (strncmp(cp, "SHA256:", 7) != 0)
2141: fatal("%s:%lu: unsupported hash algorithm", file, lnum);
2142: cp += 7;
2143:
2144: /*
2145: * OpenSSH base64 hashes omit trailing '='
2146: * characters; put them back for decode.
2147: */
2148: tlen = strlen(cp);
2149: tmp = xmalloc(tlen + 4 + 1);
2150: strlcpy(tmp, cp, tlen + 1);
2151: while ((tlen % 4) != 0) {
2152: tmp[tlen++] = '=';
2153: tmp[tlen] = '\0';
2154: }
2155: if ((b = sshbuf_new()) == NULL)
2156: fatal("%s: sshbuf_new failed", __func__);
2157: if ((r = sshbuf_b64tod(b, tmp)) != 0)
2158: fatal("%s:%lu: decode hash failed: %s", file, lnum, ssh_err(r));
2159: free(tmp);
2160: *lenp = sshbuf_len(b);
2161: *blobp = xmalloc(*lenp);
2162: memcpy(*blobp, sshbuf_ptr(b), *lenp);
2163: sshbuf_free(b);
2164: }
2165:
2166: static void
1.261 djm 2167: update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
1.252 djm 2168: const struct sshkey *ca, struct ssh_krl *krl)
1.223 djm 2169: {
1.252 djm 2170: struct sshkey *key = NULL;
1.223 djm 2171: u_long lnum = 0;
1.317 markus 2172: char *path, *cp, *ep, *line = NULL;
1.320 djm 2173: u_char *blob = NULL;
2174: size_t blen = 0, linesize = 0;
1.223 djm 2175: unsigned long long serial, serial2;
1.320 djm 2176: int i, was_explicit_key, was_sha1, was_sha256, was_hash, r;
1.223 djm 2177: FILE *krl_spec;
2178:
2179: path = tilde_expand_filename(file, pw->pw_uid);
2180: if (strcmp(path, "-") == 0) {
2181: krl_spec = stdin;
2182: free(path);
2183: path = xstrdup("(standard input)");
2184: } else if ((krl_spec = fopen(path, "r")) == NULL)
2185: fatal("fopen %s: %s", path, strerror(errno));
2186:
2187: if (!quiet)
2188: printf("Revoking from %s\n", path);
1.317 markus 2189: while (getline(&line, &linesize, krl_spec) != -1) {
2190: lnum++;
1.320 djm 2191: was_explicit_key = was_sha1 = was_sha256 = was_hash = 0;
1.223 djm 2192: cp = line + strspn(line, " \t");
2193: /* Trim trailing space, comments and strip \n */
2194: for (i = 0, r = -1; cp[i] != '\0'; i++) {
2195: if (cp[i] == '#' || cp[i] == '\n') {
2196: cp[i] = '\0';
2197: break;
2198: }
2199: if (cp[i] == ' ' || cp[i] == '\t') {
2200: /* Remember the start of a span of whitespace */
2201: if (r == -1)
2202: r = i;
2203: } else
2204: r = -1;
2205: }
2206: if (r != -1)
2207: cp[r] = '\0';
2208: if (*cp == '\0')
2209: continue;
2210: if (strncasecmp(cp, "serial:", 7) == 0) {
1.261 djm 2211: if (ca == NULL && !wild_ca) {
1.231 djm 2212: fatal("revoking certificates by serial number "
1.223 djm 2213: "requires specification of a CA key");
2214: }
2215: cp += 7;
2216: cp = cp + strspn(cp, " \t");
2217: errno = 0;
2218: serial = strtoull(cp, &ep, 0);
2219: if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2220: fatal("%s:%lu: invalid serial \"%s\"",
2221: path, lnum, cp);
2222: if (errno == ERANGE && serial == ULLONG_MAX)
2223: fatal("%s:%lu: serial out of range",
2224: path, lnum);
2225: serial2 = serial;
2226: if (*ep == '-') {
2227: cp = ep + 1;
2228: errno = 0;
2229: serial2 = strtoull(cp, &ep, 0);
2230: if (*cp == '\0' || *ep != '\0')
2231: fatal("%s:%lu: invalid serial \"%s\"",
2232: path, lnum, cp);
2233: if (errno == ERANGE && serial2 == ULLONG_MAX)
2234: fatal("%s:%lu: serial out of range",
2235: path, lnum);
2236: if (serial2 <= serial)
2237: fatal("%s:%lu: invalid serial range "
2238: "%llu:%llu", path, lnum,
2239: (unsigned long long)serial,
2240: (unsigned long long)serial2);
2241: }
2242: if (ssh_krl_revoke_cert_by_serial_range(krl,
2243: ca, serial, serial2) != 0) {
2244: fatal("%s: revoke serial failed",
2245: __func__);
2246: }
2247: } else if (strncasecmp(cp, "id:", 3) == 0) {
1.261 djm 2248: if (ca == NULL && !wild_ca) {
1.232 djm 2249: fatal("revoking certificates by key ID "
1.223 djm 2250: "requires specification of a CA key");
2251: }
2252: cp += 3;
2253: cp = cp + strspn(cp, " \t");
2254: if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2255: fatal("%s: revoke key ID failed", __func__);
1.320 djm 2256: } else if (strncasecmp(cp, "hash:", 5) == 0) {
2257: cp += 5;
2258: cp = cp + strspn(cp, " \t");
2259: hash_to_blob(cp, &blob, &blen, file, lnum);
2260: r = ssh_krl_revoke_key_sha256(krl, blob, blen);
1.223 djm 2261: } else {
2262: if (strncasecmp(cp, "key:", 4) == 0) {
2263: cp += 4;
2264: cp = cp + strspn(cp, " \t");
2265: was_explicit_key = 1;
2266: } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2267: cp += 5;
2268: cp = cp + strspn(cp, " \t");
2269: was_sha1 = 1;
1.320 djm 2270: } else if (strncasecmp(cp, "sha256:", 7) == 0) {
2271: cp += 7;
2272: cp = cp + strspn(cp, " \t");
2273: was_sha256 = 1;
1.223 djm 2274: /*
2275: * Just try to process the line as a key.
2276: * Parsing will fail if it isn't.
2277: */
2278: }
1.252 djm 2279: if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
1.304 markus 2280: fatal("sshkey_new");
1.252 djm 2281: if ((r = sshkey_read(key, &cp)) != 0)
2282: fatal("%s:%lu: invalid key: %s",
2283: path, lnum, ssh_err(r));
1.223 djm 2284: if (was_explicit_key)
2285: r = ssh_krl_revoke_key_explicit(krl, key);
1.320 djm 2286: else if (was_sha1) {
2287: if (sshkey_fingerprint_raw(key,
2288: SSH_DIGEST_SHA1, &blob, &blen) != 0) {
2289: fatal("%s:%lu: fingerprint failed",
2290: file, lnum);
2291: }
2292: r = ssh_krl_revoke_key_sha1(krl, blob, blen);
2293: } else if (was_sha256) {
2294: if (sshkey_fingerprint_raw(key,
2295: SSH_DIGEST_SHA256, &blob, &blen) != 0) {
2296: fatal("%s:%lu: fingerprint failed",
2297: file, lnum);
2298: }
2299: r = ssh_krl_revoke_key_sha256(krl, blob, blen);
2300: } else
1.223 djm 2301: r = ssh_krl_revoke_key(krl, key);
2302: if (r != 0)
1.252 djm 2303: fatal("%s: revoke key failed: %s",
2304: __func__, ssh_err(r));
1.320 djm 2305: freezero(blob, blen);
2306: blob = NULL;
2307: blen = 0;
1.252 djm 2308: sshkey_free(key);
1.223 djm 2309: }
2310: }
2311: if (strcmp(path, "-") != 0)
2312: fclose(krl_spec);
1.317 markus 2313: free(line);
1.226 djm 2314: free(path);
1.223 djm 2315: }
2316:
2317: static void
1.325 djm 2318: do_gen_krl(struct passwd *pw, int updating, const char *ca_key_path,
2319: unsigned long long krl_version, const char *krl_comment,
2320: int argc, char **argv)
1.223 djm 2321: {
2322: struct ssh_krl *krl;
2323: struct stat sb;
1.252 djm 2324: struct sshkey *ca = NULL;
1.261 djm 2325: int fd, i, r, wild_ca = 0;
1.223 djm 2326: char *tmp;
1.252 djm 2327: struct sshbuf *kbuf;
1.223 djm 2328:
2329: if (*identity_file == '\0')
2330: fatal("KRL generation requires an output file");
2331: if (stat(identity_file, &sb) == -1) {
2332: if (errno != ENOENT)
2333: fatal("Cannot access KRL \"%s\": %s",
2334: identity_file, strerror(errno));
2335: if (updating)
2336: fatal("KRL \"%s\" does not exist", identity_file);
2337: }
2338: if (ca_key_path != NULL) {
1.261 djm 2339: if (strcasecmp(ca_key_path, "none") == 0)
2340: wild_ca = 1;
2341: else {
2342: tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2343: if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2344: fatal("Cannot load CA public key %s: %s",
2345: tmp, ssh_err(r));
2346: free(tmp);
2347: }
1.223 djm 2348: }
2349:
2350: if (updating)
2351: load_krl(identity_file, &krl);
2352: else if ((krl = ssh_krl_init()) == NULL)
2353: fatal("couldn't create KRL");
2354:
1.325 djm 2355: if (krl_version != 0)
2356: ssh_krl_set_version(krl, krl_version);
2357: if (krl_comment != NULL)
2358: ssh_krl_set_comment(krl, krl_comment);
1.223 djm 2359:
2360: for (i = 0; i < argc; i++)
1.261 djm 2361: update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
1.223 djm 2362:
1.252 djm 2363: if ((kbuf = sshbuf_new()) == NULL)
2364: fatal("sshbuf_new failed");
2365: if (ssh_krl_to_blob(krl, kbuf, NULL, 0) != 0)
1.223 djm 2366: fatal("Couldn't generate KRL");
2367: if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
2368: fatal("open %s: %s", identity_file, strerror(errno));
1.318 markus 2369: if (atomicio(vwrite, fd, sshbuf_mutable_ptr(kbuf), sshbuf_len(kbuf)) !=
1.252 djm 2370: sshbuf_len(kbuf))
1.223 djm 2371: fatal("write %s: %s", identity_file, strerror(errno));
2372: close(fd);
1.252 djm 2373: sshbuf_free(kbuf);
1.223 djm 2374: ssh_krl_free(krl);
1.286 mmcc 2375: sshkey_free(ca);
1.223 djm 2376: }
2377:
2378: static void
2379: do_check_krl(struct passwd *pw, int argc, char **argv)
2380: {
2381: int i, r, ret = 0;
2382: char *comment;
2383: struct ssh_krl *krl;
1.252 djm 2384: struct sshkey *k;
1.223 djm 2385:
2386: if (*identity_file == '\0')
2387: fatal("KRL checking requires an input file");
2388: load_krl(identity_file, &krl);
2389: for (i = 0; i < argc; i++) {
1.252 djm 2390: if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2391: fatal("Cannot load public key %s: %s",
2392: argv[i], ssh_err(r));
1.223 djm 2393: r = ssh_krl_check_key(krl, k);
2394: printf("%s%s%s%s: %s\n", argv[i],
2395: *comment ? " (" : "", comment, *comment ? ")" : "",
2396: r == 0 ? "ok" : "REVOKED");
2397: if (r != 0)
2398: ret = 1;
1.252 djm 2399: sshkey_free(k);
1.223 djm 2400: free(comment);
2401: }
2402: ssh_krl_free(krl);
2403: exit(ret);
2404: }
1.246 markus 2405: #endif
1.223 djm 2406:
1.344 ! djm 2407: static struct sshkey *
! 2408: load_sign_key(const char *keypath, const struct sshkey *pubkey)
! 2409: {
! 2410: size_t i, slen, plen = strlen(keypath);
! 2411: char *privpath = xstrdup(keypath);
! 2412: const char *suffixes[] = { "-cert.pub", ".pub", NULL };
! 2413: struct sshkey *ret = NULL, *privkey = NULL;
! 2414: int r;
! 2415:
! 2416: /*
! 2417: * If passed a public key filename, then try to locate the correponding
! 2418: * private key. This lets us specify certificates on the command-line
! 2419: * and have ssh-keygen find the appropriate private key.
! 2420: */
! 2421: for (i = 0; suffixes[i]; i++) {
! 2422: slen = strlen(suffixes[i]);
! 2423: if (plen <= slen ||
! 2424: strcmp(privpath + plen - slen, suffixes[i]) != 0)
! 2425: continue;
! 2426: privpath[plen - slen] = '\0';
! 2427: debug("%s: %s looks like a public key, using private key "
! 2428: "path %s instead", __func__, keypath, privpath);
! 2429: }
! 2430: if ((privkey = load_identity(privpath, NULL)) == NULL) {
! 2431: error("Couldn't load identity %s", keypath);
! 2432: goto done;
! 2433: }
! 2434: if (!sshkey_equal_public(pubkey, privkey)) {
! 2435: error("Public key %s doesn't match private %s",
! 2436: keypath, privpath);
! 2437: goto done;
! 2438: }
! 2439: if (sshkey_is_cert(pubkey) && !sshkey_is_cert(privkey)) {
! 2440: /*
! 2441: * Graft the certificate onto the private key to make
! 2442: * it capable of signing.
! 2443: */
! 2444: if ((r = sshkey_to_certified(privkey)) != 0) {
! 2445: error("%s: sshkey_to_certified: %s", __func__,
! 2446: ssh_err(r));
! 2447: goto done;
! 2448: }
! 2449: if ((r = sshkey_cert_copy(pubkey, privkey)) != 0) {
! 2450: error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
! 2451: goto done;
! 2452: }
! 2453: }
! 2454: /* success */
! 2455: ret = privkey;
! 2456: privkey = NULL;
! 2457: done:
! 2458: sshkey_free(privkey);
! 2459: free(privpath);
! 2460: return ret;
! 2461: }
! 2462:
! 2463: static int
! 2464: sign_one(struct sshkey *signkey, const char *filename, int fd,
! 2465: const char *sig_namespace, sshsig_signer *signer, void *signer_ctx)
! 2466: {
! 2467: struct sshbuf *sigbuf = NULL, *abuf = NULL;
! 2468: int r = SSH_ERR_INTERNAL_ERROR, wfd = -1, oerrno;
! 2469: char *wfile = NULL;
! 2470: char *asig = NULL;
! 2471:
! 2472: if (!quiet) {
! 2473: if (fd == STDIN_FILENO)
! 2474: fprintf(stderr, "Signing data on standard input\n");
! 2475: else
! 2476: fprintf(stderr, "Signing file %s\n", filename);
! 2477: }
! 2478: if ((r = sshsig_sign_fd(signkey, NULL, fd, sig_namespace,
! 2479: &sigbuf, signer, signer_ctx)) != 0) {
! 2480: error("Signing %s failed: %s", filename, ssh_err(r));
! 2481: goto out;
! 2482: }
! 2483: if ((r = sshsig_armor(sigbuf, &abuf)) != 0) {
! 2484: error("%s: sshsig_armor: %s", __func__, ssh_err(r));
! 2485: goto out;
! 2486: }
! 2487: if ((asig = sshbuf_dup_string(abuf)) == NULL) {
! 2488: error("%s: buffer error", __func__);
! 2489: r = SSH_ERR_ALLOC_FAIL;
! 2490: goto out;
! 2491: }
! 2492:
! 2493: if (fd == STDIN_FILENO) {
! 2494: fputs(asig, stdout);
! 2495: fflush(stdout);
! 2496: } else {
! 2497: xasprintf(&wfile, "%s.sig", filename);
! 2498: if (confirm_overwrite(wfile)) {
! 2499: if ((wfd = open(wfile, O_WRONLY|O_CREAT|O_TRUNC,
! 2500: 0666)) == -1) {
! 2501: oerrno = errno;
! 2502: error("Cannot open %s: %s",
! 2503: wfile, strerror(errno));
! 2504: errno = oerrno;
! 2505: r = SSH_ERR_SYSTEM_ERROR;
! 2506: goto out;
! 2507: }
! 2508: if (atomicio(vwrite, wfd, asig,
! 2509: strlen(asig)) != strlen(asig)) {
! 2510: oerrno = errno;
! 2511: error("Cannot write to %s: %s",
! 2512: wfile, strerror(errno));
! 2513: errno = oerrno;
! 2514: r = SSH_ERR_SYSTEM_ERROR;
! 2515: goto out;
! 2516: }
! 2517: if (!quiet) {
! 2518: fprintf(stderr, "Write signature to %s\n",
! 2519: wfile);
! 2520: }
! 2521: }
! 2522: }
! 2523: /* success */
! 2524: r = 0;
! 2525: out:
! 2526: free(wfile);
! 2527: free(asig);
! 2528: sshbuf_free(abuf);
! 2529: sshbuf_free(sigbuf);
! 2530: if (wfd != -1)
! 2531: close(wfd);
! 2532: return r;
! 2533: }
! 2534:
! 2535: static int
! 2536: sign(const char *keypath, const char *sig_namespace, int argc, char **argv)
! 2537: {
! 2538: int i, fd = -1, r, ret = -1;
! 2539: int agent_fd = -1;
! 2540: struct sshkey *pubkey = NULL, *privkey = NULL, *signkey = NULL;
! 2541: sshsig_signer *signer = NULL;
! 2542:
! 2543: /* Check file arguments. */
! 2544: for (i = 0; i < argc; i++) {
! 2545: if (strcmp(argv[i], "-") != 0)
! 2546: continue;
! 2547: if (i > 0 || argc > 1)
! 2548: fatal("Cannot sign mix of paths and standard input");
! 2549: }
! 2550:
! 2551: if ((r = sshkey_load_public(keypath, &pubkey, NULL)) != 0) {
! 2552: error("Couldn't load public key %s: %s", keypath, ssh_err(r));
! 2553: goto done;
! 2554: }
! 2555:
! 2556: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
! 2557: debug("Couldn't get agent socket: %s", ssh_err(r));
! 2558: else {
! 2559: if ((r = ssh_agent_has_key(agent_fd, pubkey)) == 0)
! 2560: signer = agent_signer;
! 2561: else
! 2562: debug("Couldn't find key in agent: %s", ssh_err(r));
! 2563: }
! 2564:
! 2565: if (signer == NULL) {
! 2566: /* Not using agent - try to load private key */
! 2567: if ((privkey = load_sign_key(keypath, pubkey)) == NULL)
! 2568: goto done;
! 2569: signkey = privkey;
! 2570: } else {
! 2571: /* Will use key in agent */
! 2572: signkey = pubkey;
! 2573: }
! 2574:
! 2575: if (argc == 0) {
! 2576: if ((r = sign_one(signkey, "(stdin)", STDIN_FILENO,
! 2577: sig_namespace, signer, &agent_fd)) != 0)
! 2578: goto done;
! 2579: } else {
! 2580: for (i = 0; i < argc; i++) {
! 2581: if (strcmp(argv[i], "-") == 0)
! 2582: fd = STDIN_FILENO;
! 2583: else if ((fd = open(argv[i], O_RDONLY)) == -1) {
! 2584: error("Cannot open %s for signing: %s",
! 2585: argv[i], strerror(errno));
! 2586: goto done;
! 2587: }
! 2588: if ((r = sign_one(signkey, argv[i], fd, sig_namespace,
! 2589: signer, &agent_fd)) != 0)
! 2590: goto done;
! 2591: if (fd != STDIN_FILENO)
! 2592: close(fd);
! 2593: fd = -1;
! 2594: }
! 2595: }
! 2596:
! 2597: ret = 0;
! 2598: done:
! 2599: if (fd != -1 && fd != STDIN_FILENO)
! 2600: close(fd);
! 2601: sshkey_free(pubkey);
! 2602: sshkey_free(privkey);
! 2603: return ret;
! 2604: }
! 2605:
! 2606: static int
! 2607: verify(const char *signature, const char *sig_namespace, const char *principal,
! 2608: const char *allowed_keys, const char *revoked_keys)
! 2609: {
! 2610: int r, ret = -1, sigfd = -1;
! 2611: struct sshbuf *sigbuf = NULL, *abuf = NULL;
! 2612: struct sshkey *sign_key = NULL;
! 2613: char *fp = NULL;
! 2614:
! 2615: if ((abuf = sshbuf_new()) == NULL)
! 2616: fatal("%s: sshbuf_new() failed", __func__);
! 2617:
! 2618: if ((sigfd = open(signature, O_RDONLY)) < 0) {
! 2619: error("Couldn't open signature file %s", signature);
! 2620: goto done;
! 2621: }
! 2622:
! 2623: if ((r = sshkey_load_file(sigfd, abuf)) != 0) {
! 2624: error("Couldn't read signature file: %s", ssh_err(r));
! 2625: goto done;
! 2626: }
! 2627: if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
! 2628: error("%s: sshsig_armor: %s", __func__, ssh_err(r));
! 2629: return r;
! 2630: }
! 2631: if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace,
! 2632: &sign_key)) != 0)
! 2633: goto done; /* sshsig_verify() prints error */
! 2634:
! 2635: if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
! 2636: SSH_FP_DEFAULT)) == NULL)
! 2637: fatal("%s: sshkey_fingerprint failed", __func__);
! 2638: debug("Valid (unverified) signature from key %s", fp);
! 2639: free(fp);
! 2640: fp = NULL;
! 2641:
! 2642: if (revoked_keys != NULL) {
! 2643: if ((r = sshkey_check_revoked(sign_key, revoked_keys)) != 0) {
! 2644: debug3("sshkey_check_revoked failed: %s", ssh_err(r));
! 2645: goto done;
! 2646: }
! 2647: }
! 2648:
! 2649: if ((r = sshsig_check_allowed_keys(allowed_keys, sign_key,
! 2650: principal, sig_namespace)) != 0) {
! 2651: debug3("sshsig_check_allowed_keys failed: %s", ssh_err(r));
! 2652: goto done;
! 2653: }
! 2654: /* success */
! 2655: ret = 0;
! 2656: done:
! 2657: if (!quiet) {
! 2658: if (ret == 0) {
! 2659: if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
! 2660: SSH_FP_DEFAULT)) == NULL) {
! 2661: fatal("%s: sshkey_fingerprint failed",
! 2662: __func__);
! 2663: }
! 2664: printf("Good \"%s\" signature for %s with %s key %s\n",
! 2665: sig_namespace, principal,
! 2666: sshkey_type(sign_key), fp);
! 2667: } else {
! 2668: printf("Could not verify signature.\n");
! 2669: }
! 2670: }
! 2671: if (sigfd != -1)
! 2672: close(sigfd);
! 2673: sshbuf_free(sigbuf);
! 2674: sshbuf_free(abuf);
! 2675: sshkey_free(sign_key);
! 2676: free(fp);
! 2677: return ret;
! 2678: }
! 2679:
1.223 djm 2680: static void
1.10 markus 2681: usage(void)
2682: {
1.243 deraadt 2683: fprintf(stderr,
1.324 djm 2684: "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa] [-m format]\n"
1.243 deraadt 2685: " [-N new_passphrase] [-C comment] [-f output_keyfile]\n"
1.324 djm 2686: " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-m format]\n"
2687: " [-f keyfile]\n"
1.243 deraadt 2688: " ssh-keygen -i [-m key_format] [-f input_keyfile]\n"
2689: " ssh-keygen -e [-m key_format] [-f input_keyfile]\n"
2690: " ssh-keygen -y [-f input_keyfile]\n"
2691: " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n"
1.265 naddy 2692: " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
1.302 djm 2693: " ssh-keygen -B [-f input_keyfile]\n");
1.177 markus 2694: #ifdef ENABLE_PKCS11
1.243 deraadt 2695: fprintf(stderr,
2696: " ssh-keygen -D pkcs11\n");
1.177 markus 2697: #endif
1.243 deraadt 2698: fprintf(stderr,
2699: " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n"
2700: " ssh-keygen -H [-f known_hosts_file]\n"
2701: " ssh-keygen -R hostname [-f known_hosts_file]\n"
2702: " ssh-keygen -r hostname [-f input_keyfile] [-g]\n"
1.274 djm 2703: #ifdef WITH_OPENSSL
1.243 deraadt 2704: " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n"
2705: " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n"
2706: " [-j start_line] [-K checkpt] [-W generator]\n"
1.274 djm 2707: #endif
1.305 djm 2708: " ssh-keygen -s ca_key -I certificate_identity [-h] [-U]\n"
2709: " [-D pkcs11_provider] [-n principals] [-O option]\n"
2710: " [-V validity_interval] [-z serial_number] file ...\n"
1.243 deraadt 2711: " ssh-keygen -L [-f input_keyfile]\n"
2712: " ssh-keygen -A\n"
2713: " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
2714: " file ...\n"
1.344 ! djm 2715: " ssh-keygen -Q -f le file ...\n"
! 2716: " ssh-keygen -Y sign -f sign_key -n namespace\n"
! 2717: " ssh-keygen -Y verify -I signer_identity -s signature_file\n"
! 2718: " -n namespace -f allowed_keys [-r revoked_keys]\n");
1.12 markus 2719: exit(1);
1.10 markus 2720: }
2721:
1.13 deraadt 2722: /*
2723: * Main program for key management.
2724: */
1.2 provos 2725: int
1.156 deraadt 2726: main(int argc, char **argv)
1.1 deraadt 2727: {
1.253 deraadt 2728: char dotsshdir[PATH_MAX], comment[1024], *passphrase1, *passphrase2;
1.274 djm 2729: char *rr_hostname = NULL, *ep, *fp, *ra;
1.252 djm 2730: struct sshkey *private, *public;
1.12 markus 2731: struct passwd *pw;
2732: struct stat st;
1.252 djm 2733: int r, opt, type, fd;
1.325 djm 2734: int change_passphrase = 0, change_comment = 0, show_cert = 0;
2735: int find_host = 0, delete_host = 0, hash_hosts = 0;
1.274 djm 2736: int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
1.325 djm 2737: int prefer_agent = 0, convert_to = 0, convert_from = 0;
1.326 djm 2738: int print_public = 0, print_generic = 0, cert_serial_autoinc = 0;
1.325 djm 2739: unsigned long long cert_serial = 0;
2740: char *identity_comment = NULL, *ca_key_path = NULL;
1.340 dtucker 2741: u_int32_t bits = 0;
1.274 djm 2742: FILE *f;
2743: const char *errstr;
1.325 djm 2744: int log_level = SYSLOG_LEVEL_INFO;
1.344 ! djm 2745: char *sign_op = NULL;
1.274 djm 2746: #ifdef WITH_OPENSSL
2747: /* Moduli generation/screening */
2748: char out_file[PATH_MAX], *checkpoint = NULL;
1.237 markus 2749: u_int32_t memory = 0, generator_wanted = 0;
1.107 djm 2750: int do_gen_candidates = 0, do_screen_candidates = 0;
1.215 dtucker 2751: unsigned long start_lineno = 0, lines_to_process = 0;
1.107 djm 2752: BIGNUM *start = NULL;
1.274 djm 2753: #endif
1.33 markus 2754:
1.12 markus 2755: extern int optind;
2756: extern char *optarg;
1.129 djm 2757:
2758: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2759: sanitise_stdfd();
1.12 markus 2760:
1.201 djm 2761: OpenSSL_add_all_algorithms();
1.156 deraadt 2762: log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
1.294 djm 2763:
2764: setlocale(LC_CTYPE, "");
1.19 markus 2765:
1.14 markus 2766: /* we need this for the home * directory. */
1.12 markus 2767: pw = getpwuid(getuid());
1.269 djm 2768: if (!pw)
2769: fatal("No user exists for uid %lu", (u_long)getuid());
1.333 deraadt 2770: if (gethostname(hostname, sizeof(hostname)) == -1)
1.269 djm 2771: fatal("gethostname: %s", strerror(errno));
1.14 markus 2772:
1.344 ! djm 2773: /* Remaining characters: dw */
1.305 djm 2774: while ((opt = getopt(argc, argv, "ABHLQUXceghiklopquvxy"
1.344 ! djm 2775: "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Y:Z:"
1.251 djm 2776: "a:b:f:g:j:m:n:r:s:t:z:")) != -1) {
1.12 markus 2777: switch (opt) {
1.206 stevesk 2778: case 'A':
2779: gen_all_hostkeys = 1;
2780: break;
1.12 markus 2781: case 'b':
1.340 dtucker 2782: bits = (u_int32_t)strtonum(optarg, 1, UINT32_MAX,
2783: &errstr);
1.125 avsm 2784: if (errstr)
2785: fatal("Bits has bad value %s (%s)",
2786: optarg, errstr);
1.12 markus 2787: break;
1.251 djm 2788: case 'E':
2789: fingerprint_hash = ssh_digest_alg_by_name(optarg);
2790: if (fingerprint_hash == -1)
2791: fatal("Invalid hash algorithm \"%s\"", optarg);
2792: break;
1.119 djm 2793: case 'F':
2794: find_host = 1;
2795: rr_hostname = optarg;
2796: break;
2797: case 'H':
2798: hash_hosts = 1;
2799: break;
1.179 djm 2800: case 'I':
2801: cert_key_id = optarg;
2802: break;
1.119 djm 2803: case 'R':
2804: delete_host = 1;
2805: rr_hostname = optarg;
2806: break;
1.182 djm 2807: case 'L':
2808: show_cert = 1;
2809: break;
1.12 markus 2810: case 'l':
2811: print_fingerprint = 1;
2812: break;
1.49 markus 2813: case 'B':
2814: print_bubblebabble = 1;
2815: break;
1.193 djm 2816: case 'm':
2817: if (strcasecmp(optarg, "RFC4716") == 0 ||
2818: strcasecmp(optarg, "ssh2") == 0) {
2819: convert_format = FMT_RFC4716;
2820: break;
2821: }
2822: if (strcasecmp(optarg, "PKCS8") == 0) {
2823: convert_format = FMT_PKCS8;
1.336 djm 2824: private_key_format = SSHKEY_PRIVATE_PKCS8;
1.193 djm 2825: break;
2826: }
2827: if (strcasecmp(optarg, "PEM") == 0) {
2828: convert_format = FMT_PEM;
1.336 djm 2829: private_key_format = SSHKEY_PRIVATE_PEM;
1.193 djm 2830: break;
2831: }
2832: fatal("Unsupported conversion format \"%s\"", optarg);
1.179 djm 2833: case 'n':
2834: cert_principals = optarg;
2835: break;
1.237 markus 2836: case 'o':
1.319 djm 2837: /* no-op; new format is already the default */
1.237 markus 2838: break;
1.12 markus 2839: case 'p':
2840: change_passphrase = 1;
2841: break;
2842: case 'c':
2843: change_comment = 1;
2844: break;
2845: case 'f':
1.274 djm 2846: if (strlcpy(identity_file, optarg,
2847: sizeof(identity_file)) >= sizeof(identity_file))
1.124 avsm 2848: fatal("Identity filename too long");
1.12 markus 2849: have_identity = 1;
2850: break;
1.105 jakob 2851: case 'g':
2852: print_generic = 1;
2853: break;
1.12 markus 2854: case 'P':
2855: identity_passphrase = optarg;
2856: break;
2857: case 'N':
2858: identity_new_passphrase = optarg;
2859: break;
1.223 djm 2860: case 'Q':
2861: check_krl = 1;
2862: break;
1.179 djm 2863: case 'O':
1.186 djm 2864: add_cert_option(optarg);
1.179 djm 2865: break;
1.237 markus 2866: case 'Z':
1.336 djm 2867: openssh_format_cipher = optarg;
1.237 markus 2868: break;
1.12 markus 2869: case 'C':
2870: identity_comment = optarg;
2871: break;
2872: case 'q':
2873: quiet = 1;
1.20 deraadt 2874: break;
1.57 markus 2875: case 'e':
1.19 markus 2876: case 'x':
1.57 markus 2877: /* export key */
1.193 djm 2878: convert_to = 1;
1.19 markus 2879: break;
1.179 djm 2880: case 'h':
2881: cert_key_type = SSH2_CERT_TYPE_HOST;
1.190 djm 2882: certflags_flags = 0;
1.179 djm 2883: break;
1.223 djm 2884: case 'k':
2885: gen_krl = 1;
2886: break;
1.57 markus 2887: case 'i':
1.19 markus 2888: case 'X':
1.57 markus 2889: /* import key */
1.193 djm 2890: convert_from = 1;
1.19 markus 2891: break;
2892: case 'y':
2893: print_public = 1;
2894: break;
1.179 djm 2895: case 's':
2896: ca_key_path = optarg;
2897: break;
1.33 markus 2898: case 't':
2899: key_type_name = optarg;
2900: break;
1.75 markus 2901: case 'D':
1.177 markus 2902: pkcs11provider = optarg;
1.305 djm 2903: break;
2904: case 'U':
2905: prefer_agent = 1;
1.66 markus 2906: break;
1.223 djm 2907: case 'u':
2908: update_krl = 1;
2909: break;
1.113 djm 2910: case 'v':
2911: if (log_level == SYSLOG_LEVEL_INFO)
2912: log_level = SYSLOG_LEVEL_DEBUG1;
2913: else {
1.117 deraadt 2914: if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
1.113 djm 2915: log_level < SYSLOG_LEVEL_DEBUG3)
2916: log_level++;
2917: }
2918: break;
1.105 jakob 2919: case 'r':
1.119 djm 2920: rr_hostname = optarg;
1.105 jakob 2921: break;
1.107 djm 2922: case 'a':
1.237 markus 2923: rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
1.124 avsm 2924: if (errstr)
1.237 markus 2925: fatal("Invalid number: %s (%s)",
1.124 avsm 2926: optarg, errstr);
1.107 djm 2927: break;
1.274 djm 2928: case 'V':
2929: parse_cert_times(optarg);
2930: break;
1.344 ! djm 2931: case 'Y':
! 2932: sign_op = optarg;
! 2933: break;
1.274 djm 2934: case 'z':
2935: errno = 0;
1.326 djm 2936: if (*optarg == '+') {
2937: cert_serial_autoinc = 1;
2938: optarg++;
2939: }
1.274 djm 2940: cert_serial = strtoull(optarg, &ep, 10);
2941: if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2942: (errno == ERANGE && cert_serial == ULLONG_MAX))
2943: fatal("Invalid serial number \"%s\"", optarg);
1.107 djm 2944: break;
1.274 djm 2945: #ifdef WITH_OPENSSL
2946: /* Moduli generation/screening */
1.107 djm 2947: case 'G':
2948: do_gen_candidates = 1;
1.124 avsm 2949: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2950: sizeof(out_file))
2951: fatal("Output filename too long");
1.107 djm 2952: break;
1.274 djm 2953: case 'J':
2954: lines_to_process = strtoul(optarg, NULL, 10);
1.292 dtucker 2955: break;
1.274 djm 2956: case 'j':
2957: start_lineno = strtoul(optarg, NULL, 10);
1.292 dtucker 2958: break;
1.211 dtucker 2959: case 'K':
1.253 deraadt 2960: if (strlen(optarg) >= PATH_MAX)
1.211 dtucker 2961: fatal("Checkpoint filename too long");
2962: checkpoint = xstrdup(optarg);
2963: break;
1.274 djm 2964: case 'M':
2965: memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX,
2966: &errstr);
2967: if (errstr)
2968: fatal("Memory limit is %s: %s", errstr, optarg);
2969: break;
1.107 djm 2970: case 'S':
2971: /* XXX - also compare length against bits */
2972: if (BN_hex2bn(&start, optarg) == 0)
2973: fatal("Invalid start point.");
2974: break;
1.274 djm 2975: case 'T':
2976: do_screen_candidates = 1;
2977: if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2978: sizeof(out_file))
2979: fatal("Output filename too long");
1.186 djm 2980: break;
1.274 djm 2981: case 'W':
2982: generator_wanted = (u_int32_t)strtonum(optarg, 1,
2983: UINT_MAX, &errstr);
2984: if (errstr != NULL)
2985: fatal("Desired generator invalid: %s (%s)",
2986: optarg, errstr);
1.179 djm 2987: break;
1.274 djm 2988: #endif /* WITH_OPENSSL */
1.12 markus 2989: case '?':
2990: default:
2991: usage();
2992: }
2993: }
1.113 djm 2994:
2995: /* reinit */
1.156 deraadt 2996: log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
1.113 djm 2997:
1.179 djm 2998: argv += optind;
2999: argc -= optind;
1.344 ! djm 3000:
! 3001: if (sign_op != NULL) {
! 3002: if (cert_principals == NULL) {
! 3003: error("Too few arguments for sign/verify: "
! 3004: "missing namespace");
! 3005: exit(1);
! 3006: }
! 3007: if (strncmp(sign_op, "sign", 4) == 0) {
! 3008: if (!have_identity) {
! 3009: error("Too few arguments for sign: "
! 3010: "missing key");
! 3011: exit(1);
! 3012: }
! 3013: return sign(identity_file, cert_principals, argc, argv);
! 3014: } else if (strncmp(sign_op, "verify", 6) == 0) {
! 3015: if (ca_key_path == NULL) {
! 3016: error("Too few arguments for verify: "
! 3017: "missing signature file");
! 3018: exit(1);
! 3019: }
! 3020: if (!have_identity) {
! 3021: error("Too few arguments for sign: "
! 3022: "missing allowed keys file");
! 3023: exit(1);
! 3024: }
! 3025: if (cert_key_id == NULL) {
! 3026: error("Too few arguments for verify: "
! 3027: "missing principal ID");
! 3028: exit(1);
! 3029: }
! 3030: return verify(ca_key_path, cert_principals,
! 3031: cert_key_id, identity_file, rr_hostname);
! 3032: }
! 3033: usage();
! 3034: /* NOTREACHED */
! 3035: }
1.179 djm 3036:
3037: if (ca_key_path != NULL) {
1.223 djm 3038: if (argc < 1 && !gen_krl) {
1.269 djm 3039: error("Too few arguments.");
1.179 djm 3040: usage();
3041: }
1.223 djm 3042: } else if (argc > 0 && !gen_krl && !check_krl) {
1.269 djm 3043: error("Too many arguments.");
1.87 djm 3044: usage();
3045: }
1.12 markus 3046: if (change_passphrase && change_comment) {
1.269 djm 3047: error("Can only have one of -p and -c.");
1.166 djm 3048: usage();
3049: }
3050: if (print_fingerprint && (delete_host || hash_hosts)) {
1.269 djm 3051: error("Cannot use -l with -H or -R.");
1.12 markus 3052: usage();
1.223 djm 3053: }
1.246 markus 3054: #ifdef WITH_OPENSSL
1.223 djm 3055: if (gen_krl) {
1.325 djm 3056: do_gen_krl(pw, update_krl, ca_key_path,
3057: cert_serial, identity_comment, argc, argv);
1.223 djm 3058: return (0);
3059: }
3060: if (check_krl) {
3061: do_check_krl(pw, argc, argv);
3062: return (0);
1.179 djm 3063: }
1.246 markus 3064: #endif
1.179 djm 3065: if (ca_key_path != NULL) {
3066: if (cert_key_id == NULL)
3067: fatal("Must specify key id (-I) when certifying");
1.326 djm 3068: do_ca_sign(pw, ca_key_path, prefer_agent,
3069: cert_serial, cert_serial_autoinc, argc, argv);
1.12 markus 3070: }
1.182 djm 3071: if (show_cert)
3072: do_show_cert(pw);
1.325 djm 3073: if (delete_host || hash_hosts || find_host) {
3074: do_known_hosts(pw, rr_hostname, find_host,
3075: delete_host, hash_hosts);
3076: }
1.221 djm 3077: if (pkcs11provider != NULL)
3078: do_download(pw);
1.49 markus 3079: if (print_fingerprint || print_bubblebabble)
1.12 markus 3080: do_fingerprint(pw);
3081: if (change_passphrase)
3082: do_change_passphrase(pw);
3083: if (change_comment)
1.325 djm 3084: do_change_comment(pw, identity_comment);
1.246 markus 3085: #ifdef WITH_OPENSSL
1.193 djm 3086: if (convert_to)
3087: do_convert_to(pw);
3088: if (convert_from)
3089: do_convert_from(pw);
1.246 markus 3090: #endif
1.19 markus 3091: if (print_public)
3092: do_print_public(pw);
1.119 djm 3093: if (rr_hostname != NULL) {
1.138 jakob 3094: unsigned int n = 0;
3095:
3096: if (have_identity) {
1.325 djm 3097: n = do_print_resource_record(pw, identity_file,
3098: rr_hostname, print_generic);
1.269 djm 3099: if (n == 0)
3100: fatal("%s: %s", identity_file, strerror(errno));
1.138 jakob 3101: exit(0);
3102: } else {
3103:
3104: n += do_print_resource_record(pw,
1.325 djm 3105: _PATH_HOST_RSA_KEY_FILE, rr_hostname,
3106: print_generic);
1.138 jakob 3107: n += do_print_resource_record(pw,
1.325 djm 3108: _PATH_HOST_DSA_KEY_FILE, rr_hostname,
3109: print_generic);
1.214 djm 3110: n += do_print_resource_record(pw,
1.325 djm 3111: _PATH_HOST_ECDSA_KEY_FILE, rr_hostname,
3112: print_generic);
1.244 logan 3113: n += do_print_resource_record(pw,
1.325 djm 3114: _PATH_HOST_ED25519_KEY_FILE, rr_hostname,
3115: print_generic);
1.313 markus 3116: n += do_print_resource_record(pw,
1.325 djm 3117: _PATH_HOST_XMSS_KEY_FILE, rr_hostname,
3118: print_generic);
1.138 jakob 3119: if (n == 0)
3120: fatal("no keys found.");
3121: exit(0);
3122: }
1.105 jakob 3123: }
1.107 djm 3124:
1.274 djm 3125: #ifdef WITH_OPENSSL
1.107 djm 3126: if (do_gen_candidates) {
3127: FILE *out = fopen(out_file, "w");
1.111 djm 3128:
1.107 djm 3129: if (out == NULL) {
3130: error("Couldn't open modulus candidate file \"%s\": %s",
3131: out_file, strerror(errno));
3132: return (1);
3133: }
1.130 markus 3134: if (bits == 0)
3135: bits = DEFAULT_BITS;
1.107 djm 3136: if (gen_candidates(out, memory, bits, start) != 0)
1.131 stevesk 3137: fatal("modulus candidate generation failed");
1.107 djm 3138:
3139: return (0);
3140: }
3141:
3142: if (do_screen_candidates) {
3143: FILE *in;
1.225 djm 3144: FILE *out = fopen(out_file, "a");
1.107 djm 3145:
3146: if (have_identity && strcmp(identity_file, "-") != 0) {
3147: if ((in = fopen(identity_file, "r")) == NULL) {
3148: fatal("Couldn't open modulus candidate "
1.111 djm 3149: "file \"%s\": %s", identity_file,
1.107 djm 3150: strerror(errno));
3151: }
3152: } else
3153: in = stdin;
3154:
3155: if (out == NULL) {
3156: fatal("Couldn't open moduli file \"%s\": %s",
3157: out_file, strerror(errno));
3158: }
1.237 markus 3159: if (prime_test(in, out, rounds == 0 ? 100 : rounds,
3160: generator_wanted, checkpoint,
1.215 dtucker 3161: start_lineno, lines_to_process) != 0)
1.131 stevesk 3162: fatal("modulus screening failed");
1.108 markus 3163: return (0);
1.75 markus 3164: }
1.274 djm 3165: #endif
1.12 markus 3166:
1.206 stevesk 3167: if (gen_all_hostkeys) {
3168: do_gen_all_hostkeys(pw);
3169: return (0);
3170: }
3171:
1.133 djm 3172: if (key_type_name == NULL)
1.273 djm 3173: key_type_name = DEFAULT_KEY_TYPE_NAME;
1.133 djm 3174:
1.252 djm 3175: type = sshkey_type_from_name(key_type_name);
1.255 djm 3176: type_bits_valid(type, key_type_name, &bits);
1.206 stevesk 3177:
1.33 markus 3178: if (!quiet)
1.252 djm 3179: printf("Generating public/private %s key pair.\n",
3180: key_type_name);
1.269 djm 3181: if ((r = sshkey_generate(type, bits, &private)) != 0)
1.304 markus 3182: fatal("sshkey_generate failed");
1.269 djm 3183: if ((r = sshkey_from_private(private, &public)) != 0)
1.304 markus 3184: fatal("sshkey_from_private failed: %s\n", ssh_err(r));
1.12 markus 3185:
3186: if (!have_identity)
3187: ask_filename(pw, "Enter file in which to save the key");
3188:
1.132 djm 3189: /* Create ~/.ssh directory if it doesn't already exist. */
1.188 djm 3190: snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
3191: pw->pw_dir, _PATH_SSH_USER_DIR);
3192: if (strstr(identity_file, dotsshdir) != NULL) {
1.333 deraadt 3193: if (stat(dotsshdir, &st) == -1) {
1.188 djm 3194: if (errno != ENOENT) {
3195: error("Could not stat %s: %s", dotsshdir,
3196: strerror(errno));
1.333 deraadt 3197: } else if (mkdir(dotsshdir, 0700) == -1) {
1.188 djm 3198: error("Could not create directory '%s': %s",
3199: dotsshdir, strerror(errno));
3200: } else if (!quiet)
3201: printf("Created directory '%s'.\n", dotsshdir);
3202: }
1.12 markus 3203: }
3204: /* If the file already exists, ask the user to confirm. */
1.343 djm 3205: if (!confirm_overwrite(identity_file))
3206: exit(1);
1.12 markus 3207: /* Ask for a passphrase (twice). */
3208: if (identity_passphrase)
3209: passphrase1 = xstrdup(identity_passphrase);
3210: else if (identity_new_passphrase)
3211: passphrase1 = xstrdup(identity_new_passphrase);
3212: else {
3213: passphrase_again:
3214: passphrase1 =
1.65 markus 3215: read_passphrase("Enter passphrase (empty for no "
3216: "passphrase): ", RP_ALLOW_STDIN);
3217: passphrase2 = read_passphrase("Enter same passphrase again: ",
3218: RP_ALLOW_STDIN);
1.12 markus 3219: if (strcmp(passphrase1, passphrase2) != 0) {
1.65 markus 3220: /*
3221: * The passphrases do not match. Clear them and
3222: * retry.
3223: */
1.240 djm 3224: explicit_bzero(passphrase1, strlen(passphrase1));
3225: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 3226: free(passphrase1);
3227: free(passphrase2);
1.12 markus 3228: printf("Passphrases do not match. Try again.\n");
3229: goto passphrase_again;
3230: }
3231: /* Clear the other copy of the passphrase. */
1.240 djm 3232: explicit_bzero(passphrase2, strlen(passphrase2));
1.227 djm 3233: free(passphrase2);
1.12 markus 3234: }
3235:
3236: if (identity_comment) {
3237: strlcpy(comment, identity_comment, sizeof(comment));
3238: } else {
1.172 stevesk 3239: /* Create default comment field for the passphrase. */
1.12 markus 3240: snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
3241: }
3242:
3243: /* Save the key with the given passphrase and comment. */
1.252 djm 3244: if ((r = sshkey_save_private(private, identity_file, passphrase1,
1.336 djm 3245: comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
1.269 djm 3246: error("Saving key \"%s\" failed: %s",
1.252 djm 3247: identity_file, ssh_err(r));
1.240 djm 3248: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 3249: free(passphrase1);
1.12 markus 3250: exit(1);
3251: }
3252: /* Clear the passphrase. */
1.240 djm 3253: explicit_bzero(passphrase1, strlen(passphrase1));
1.227 djm 3254: free(passphrase1);
1.12 markus 3255:
3256: /* Clear the private key and the random number generator. */
1.252 djm 3257: sshkey_free(private);
1.12 markus 3258:
3259: if (!quiet)
3260: printf("Your identification has been saved in %s.\n", identity_file);
3261:
3262: strlcat(identity_file, ".pub", sizeof(identity_file));
1.269 djm 3263: if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
3264: fatal("Unable to save public key to %s: %s",
3265: identity_file, strerror(errno));
3266: if ((f = fdopen(fd, "w")) == NULL)
3267: fatal("fdopen %s failed: %s", identity_file, strerror(errno));
1.252 djm 3268: if ((r = sshkey_write(public, f)) != 0)
1.269 djm 3269: error("write key failed: %s", ssh_err(r));
1.19 markus 3270: fprintf(f, " %s\n", comment);
1.311 djm 3271: if (ferror(f) || fclose(f) != 0)
3272: fatal("write public failed: %s", strerror(errno));
1.12 markus 3273:
3274: if (!quiet) {
1.259 djm 3275: fp = sshkey_fingerprint(public, fingerprint_hash,
1.251 djm 3276: SSH_FP_DEFAULT);
1.259 djm 3277: ra = sshkey_fingerprint(public, fingerprint_hash,
1.167 grunk 3278: SSH_FP_RANDOMART);
1.259 djm 3279: if (fp == NULL || ra == NULL)
3280: fatal("sshkey_fingerprint failed");
1.19 markus 3281: printf("Your public key has been saved in %s.\n",
3282: identity_file);
1.12 markus 3283: printf("The key fingerprint is:\n");
1.50 markus 3284: printf("%s %s\n", fp, comment);
1.167 grunk 3285: printf("The key's randomart image is:\n");
3286: printf("%s\n", ra);
1.227 djm 3287: free(ra);
3288: free(fp);
1.12 markus 3289: }
1.19 markus 3290:
1.252 djm 3291: sshkey_free(public);
1.12 markus 3292: exit(0);
1.1 deraadt 3293: }