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