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