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