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