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