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