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