djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sshkey.c,v 1.93 2019/11/15 06:00:20 djm Exp $ */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
| 5 | * Copyright (c) 2010,2011 Damien Miller. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "includes.h" |
| 29 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 31 | #include <netinet/in.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 32 | |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 33 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 34 | #include <openssl/evp.h> |
| 35 | #include <openssl/err.h> |
| 36 | #include <openssl/pem.h> |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 37 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 38 | |
| 39 | #include "crypto_api.h" |
| 40 | |
| 41 | #include <errno.h> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 42 | #include <limits.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <string.h> |
Damien Miller | d16bdd8 | 2014-12-22 10:18:09 +1100 | [diff] [blame] | 45 | #include <resolv.h> |
Damien Miller | 6a710d3 | 2019-09-08 14:48:11 +1000 | [diff] [blame] | 46 | #include <time.h> |
Damien Miller | 82b2482 | 2014-07-02 17:43:41 +1000 | [diff] [blame] | 47 | #ifdef HAVE_UTIL_H |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 48 | #include <util.h> |
Damien Miller | 82b2482 | 2014-07-02 17:43:41 +1000 | [diff] [blame] | 49 | #endif /* HAVE_UTIL_H */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 50 | |
| 51 | #include "ssh2.h" |
| 52 | #include "ssherr.h" |
| 53 | #include "misc.h" |
| 54 | #include "sshbuf.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 55 | #include "cipher.h" |
| 56 | #include "digest.h" |
| 57 | #define SSHKEY_INTERNAL |
| 58 | #include "sshkey.h" |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 59 | #include "match.h" |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 60 | #include "ssh-sk.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 61 | |
dtucker@openbsd.org | 99043bd | 2019-05-03 03:25:18 +0000 | [diff] [blame] | 62 | #ifdef WITH_XMSS |
| 63 | #include "sshkey-xmss.h" |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 64 | #include "xmss_fast.h" |
dtucker@openbsd.org | 99043bd | 2019-05-03 03:25:18 +0000 | [diff] [blame] | 65 | #endif |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 66 | |
Damien Miller | 48f54b9 | 2018-09-13 12:13:50 +1000 | [diff] [blame] | 67 | #include "openbsd-compat/openssl-compat.h" |
| 68 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 69 | /* openssh private key file format */ |
| 70 | #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n" |
| 71 | #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n" |
| 72 | #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1) |
| 73 | #define MARK_END_LEN (sizeof(MARK_END) - 1) |
| 74 | #define KDFNAME "bcrypt" |
| 75 | #define AUTH_MAGIC "openssh-key-v1" |
| 76 | #define SALT_LEN 16 |
djm@openbsd.org | 0f34553 | 2017-08-12 06:42:52 +0000 | [diff] [blame] | 77 | #define DEFAULT_CIPHERNAME "aes256-ctr" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 78 | #define DEFAULT_ROUNDS 16 |
| 79 | |
| 80 | /* Version identification string for SSH v1 identity files. */ |
| 81 | #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n" |
| 82 | |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 83 | /* |
| 84 | * Constants relating to "shielding" support; protection of keys expected |
| 85 | * to remain in memory for long durations |
| 86 | */ |
| 87 | #define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024) |
| 88 | #define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */ |
| 89 | #define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512 |
| 90 | |
| 91 | int sshkey_private_serialize_opt(struct sshkey *key, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 92 | struct sshbuf *buf, enum sshkey_serialize_rep); |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 93 | static int sshkey_from_blob_internal(struct sshbuf *buf, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 94 | struct sshkey **keyp, int allow_cert); |
| 95 | |
| 96 | /* Supported key types */ |
| 97 | struct keytype { |
| 98 | const char *name; |
| 99 | const char *shortname; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 100 | const char *sigalg; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 101 | int type; |
| 102 | int nid; |
| 103 | int cert; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 104 | int sigonly; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 105 | }; |
| 106 | static const struct keytype keytypes[] = { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 107 | { "ssh-ed25519", "ED25519", NULL, KEY_ED25519, 0, 0, 0 }, |
| 108 | { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", NULL, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 109 | KEY_ED25519_CERT, 0, 1, 0 }, |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 110 | { "sk-ssh-ed25519@openssh.com", "ED25519-SK", NULL, |
| 111 | KEY_ED25519_SK, 0, 0, 0 }, |
| 112 | { "sk-ssh-ed25519-cert-v01@openssh.com", "ED25519-SK-CERT", NULL, |
| 113 | KEY_ED25519_SK_CERT, 0, 1, 0 }, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 114 | #ifdef WITH_XMSS |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 115 | { "ssh-xmss@openssh.com", "XMSS", NULL, KEY_XMSS, 0, 0, 0 }, |
| 116 | { "ssh-xmss-cert-v01@openssh.com", "XMSS-CERT", NULL, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 117 | KEY_XMSS_CERT, 0, 1, 0 }, |
| 118 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 119 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 120 | { "ssh-rsa", "RSA", NULL, KEY_RSA, 0, 0, 0 }, |
| 121 | { "rsa-sha2-256", "RSA", NULL, KEY_RSA, 0, 0, 1 }, |
| 122 | { "rsa-sha2-512", "RSA", NULL, KEY_RSA, 0, 0, 1 }, |
| 123 | { "ssh-dss", "DSA", NULL, KEY_DSA, 0, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 124 | # ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 125 | { "ecdsa-sha2-nistp256", "ECDSA", NULL, |
| 126 | KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 }, |
| 127 | { "ecdsa-sha2-nistp384", "ECDSA", NULL, |
| 128 | KEY_ECDSA, NID_secp384r1, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 129 | # ifdef OPENSSL_HAS_NISTP521 |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 130 | { "ecdsa-sha2-nistp521", "ECDSA", NULL, |
| 131 | KEY_ECDSA, NID_secp521r1, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 132 | # endif /* OPENSSL_HAS_NISTP521 */ |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 133 | { "sk-ecdsa-sha2-nistp256@openssh.com", "ECDSA-SK", NULL, |
| 134 | KEY_ECDSA_SK, NID_X9_62_prime256v1, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 135 | # endif /* OPENSSL_HAS_ECC */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 136 | { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", NULL, |
| 137 | KEY_RSA_CERT, 0, 1, 0 }, |
| 138 | { "rsa-sha2-256-cert-v01@openssh.com", "RSA-CERT", |
djm@openbsd.org | ebfafd9 | 2018-10-11 00:52:46 +0000 | [diff] [blame] | 139 | "rsa-sha2-256", KEY_RSA_CERT, 0, 1, 1 }, |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 140 | { "rsa-sha2-512-cert-v01@openssh.com", "RSA-CERT", |
djm@openbsd.org | ebfafd9 | 2018-10-11 00:52:46 +0000 | [diff] [blame] | 141 | "rsa-sha2-512", KEY_RSA_CERT, 0, 1, 1 }, |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 142 | { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", NULL, |
| 143 | KEY_DSA_CERT, 0, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 144 | # ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 145 | { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", NULL, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 146 | KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 }, |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 147 | { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", NULL, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 148 | KEY_ECDSA_CERT, NID_secp384r1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 149 | # ifdef OPENSSL_HAS_NISTP521 |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 150 | { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", NULL, |
| 151 | KEY_ECDSA_CERT, NID_secp521r1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 152 | # endif /* OPENSSL_HAS_NISTP521 */ |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 153 | { "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-SK-CERT", NULL, |
| 154 | KEY_ECDSA_SK_CERT, NID_X9_62_prime256v1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 155 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 156 | #endif /* WITH_OPENSSL */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 157 | { NULL, NULL, NULL, -1, -1, 0, 0 } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | const char * |
| 161 | sshkey_type(const struct sshkey *k) |
| 162 | { |
| 163 | const struct keytype *kt; |
| 164 | |
| 165 | for (kt = keytypes; kt->type != -1; kt++) { |
| 166 | if (kt->type == k->type) |
| 167 | return kt->shortname; |
| 168 | } |
| 169 | return "unknown"; |
| 170 | } |
| 171 | |
| 172 | static const char * |
| 173 | sshkey_ssh_name_from_type_nid(int type, int nid) |
| 174 | { |
| 175 | const struct keytype *kt; |
| 176 | |
| 177 | for (kt = keytypes; kt->type != -1; kt++) { |
| 178 | if (kt->type == type && (kt->nid == 0 || kt->nid == nid)) |
| 179 | return kt->name; |
| 180 | } |
| 181 | return "ssh-unknown"; |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | sshkey_type_is_cert(int type) |
| 186 | { |
| 187 | const struct keytype *kt; |
| 188 | |
| 189 | for (kt = keytypes; kt->type != -1; kt++) { |
| 190 | if (kt->type == type) |
| 191 | return kt->cert; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | const char * |
| 197 | sshkey_ssh_name(const struct sshkey *k) |
| 198 | { |
| 199 | return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid); |
| 200 | } |
| 201 | |
| 202 | const char * |
| 203 | sshkey_ssh_name_plain(const struct sshkey *k) |
| 204 | { |
| 205 | return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type), |
| 206 | k->ecdsa_nid); |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | sshkey_type_from_name(const char *name) |
| 211 | { |
| 212 | const struct keytype *kt; |
| 213 | |
| 214 | for (kt = keytypes; kt->type != -1; kt++) { |
| 215 | /* Only allow shortname matches for plain key types */ |
| 216 | if ((kt->name != NULL && strcmp(name, kt->name) == 0) || |
| 217 | (!kt->cert && strcasecmp(kt->shortname, name) == 0)) |
| 218 | return kt->type; |
| 219 | } |
| 220 | return KEY_UNSPEC; |
| 221 | } |
| 222 | |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 223 | static int |
| 224 | key_type_is_ecdsa_variant(int type) |
| 225 | { |
| 226 | switch (type) { |
| 227 | case KEY_ECDSA: |
| 228 | case KEY_ECDSA_CERT: |
| 229 | case KEY_ECDSA_SK: |
| 230 | case KEY_ECDSA_SK_CERT: |
| 231 | return 1; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 236 | int |
| 237 | sshkey_ecdsa_nid_from_name(const char *name) |
| 238 | { |
| 239 | const struct keytype *kt; |
| 240 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 241 | for (kt = keytypes; kt->type != -1; kt++) { |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 242 | if (!key_type_is_ecdsa_variant(kt->type)) |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 243 | continue; |
| 244 | if (kt->name != NULL && strcmp(name, kt->name) == 0) |
| 245 | return kt->nid; |
| 246 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | char * |
djm@openbsd.org | 183ba55 | 2017-03-10 04:07:20 +0000 | [diff] [blame] | 251 | sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 252 | { |
| 253 | char *tmp, *ret = NULL; |
| 254 | size_t nlen, rlen = 0; |
| 255 | const struct keytype *kt; |
| 256 | |
| 257 | for (kt = keytypes; kt->type != -1; kt++) { |
djm@openbsd.org | 183ba55 | 2017-03-10 04:07:20 +0000 | [diff] [blame] | 258 | if (kt->name == NULL) |
| 259 | continue; |
| 260 | if (!include_sigonly && kt->sigonly) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 261 | continue; |
| 262 | if ((certs_only && !kt->cert) || (plain_only && kt->cert)) |
| 263 | continue; |
| 264 | if (ret != NULL) |
djm@openbsd.org | 130f5df | 2016-09-12 23:31:27 +0000 | [diff] [blame] | 265 | ret[rlen++] = sep; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 266 | nlen = strlen(kt->name); |
| 267 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 268 | free(ret); |
| 269 | return NULL; |
| 270 | } |
| 271 | ret = tmp; |
| 272 | memcpy(ret + rlen, kt->name, nlen + 1); |
| 273 | rlen += nlen; |
| 274 | } |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | int |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 279 | sshkey_names_valid2(const char *names, int allow_wildcard) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 280 | { |
| 281 | char *s, *cp, *p; |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 282 | const struct keytype *kt; |
| 283 | int type; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 284 | |
| 285 | if (names == NULL || strcmp(names, "") == 0) |
| 286 | return 0; |
| 287 | if ((s = cp = strdup(names)) == NULL) |
| 288 | return 0; |
| 289 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
| 290 | (p = strsep(&cp, ","))) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 291 | type = sshkey_type_from_name(p); |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 292 | if (type == KEY_UNSPEC) { |
| 293 | if (allow_wildcard) { |
| 294 | /* |
| 295 | * Try matching key types against the string. |
| 296 | * If any has a positive or negative match then |
| 297 | * the component is accepted. |
| 298 | */ |
| 299 | for (kt = keytypes; kt->type != -1; kt++) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 300 | if (match_pattern_list(kt->name, |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 301 | p, 0) != 0) |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | if (kt->type != -1) |
| 305 | continue; |
| 306 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 307 | free(s); |
| 308 | return 0; |
| 309 | } |
| 310 | } |
| 311 | free(s); |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | u_int |
| 316 | sshkey_size(const struct sshkey *k) |
| 317 | { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 318 | #ifdef WITH_OPENSSL |
| 319 | const BIGNUM *rsa_n, *dsa_p; |
| 320 | #endif /* WITH_OPENSSL */ |
| 321 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 322 | switch (k->type) { |
| 323 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 324 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 325 | case KEY_RSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 326 | if (k->rsa == NULL) |
| 327 | return 0; |
| 328 | RSA_get0_key(k->rsa, &rsa_n, NULL, NULL); |
| 329 | return BN_num_bits(rsa_n); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 330 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 331 | case KEY_DSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 332 | if (k->dsa == NULL) |
| 333 | return 0; |
| 334 | DSA_get0_pqg(k->dsa, &dsa_p, NULL, NULL); |
| 335 | return BN_num_bits(dsa_p); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 336 | case KEY_ECDSA: |
| 337 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 338 | case KEY_ECDSA_SK: |
| 339 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 340 | return sshkey_curve_nid_to_bits(k->ecdsa_nid); |
| 341 | #endif /* WITH_OPENSSL */ |
| 342 | case KEY_ED25519: |
| 343 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 344 | case KEY_ED25519_SK: |
| 345 | case KEY_ED25519_SK_CERT: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 346 | case KEY_XMSS: |
| 347 | case KEY_XMSS_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 348 | return 256; /* XXX */ |
| 349 | } |
| 350 | return 0; |
| 351 | } |
| 352 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 353 | static int |
| 354 | sshkey_type_is_valid_ca(int type) |
| 355 | { |
| 356 | switch (type) { |
| 357 | case KEY_RSA: |
| 358 | case KEY_DSA: |
| 359 | case KEY_ECDSA: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 360 | case KEY_ECDSA_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 361 | case KEY_ED25519: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 362 | case KEY_ED25519_SK: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 363 | case KEY_XMSS: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 364 | return 1; |
| 365 | default: |
| 366 | return 0; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | sshkey_is_cert(const struct sshkey *k) |
| 372 | { |
| 373 | if (k == NULL) |
| 374 | return 0; |
| 375 | return sshkey_type_is_cert(k->type); |
| 376 | } |
| 377 | |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 378 | int |
| 379 | sshkey_is_sk(const struct sshkey *k) |
| 380 | { |
| 381 | if (k == NULL) |
| 382 | return 0; |
| 383 | switch (sshkey_type_plain(k->type)) { |
| 384 | case KEY_ECDSA_SK: |
| 385 | case KEY_ED25519_SK: |
| 386 | return 1; |
| 387 | default: |
| 388 | return 0; |
| 389 | } |
| 390 | } |
| 391 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 392 | /* Return the cert-less equivalent to a certified key type */ |
| 393 | int |
| 394 | sshkey_type_plain(int type) |
| 395 | { |
| 396 | switch (type) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 397 | case KEY_RSA_CERT: |
| 398 | return KEY_RSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 399 | case KEY_DSA_CERT: |
| 400 | return KEY_DSA; |
| 401 | case KEY_ECDSA_CERT: |
| 402 | return KEY_ECDSA; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 403 | case KEY_ECDSA_SK_CERT: |
| 404 | return KEY_ECDSA_SK; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 405 | case KEY_ED25519_CERT: |
| 406 | return KEY_ED25519; |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 407 | case KEY_ED25519_SK_CERT: |
| 408 | return KEY_ED25519_SK; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 409 | case KEY_XMSS_CERT: |
| 410 | return KEY_XMSS; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 411 | default: |
| 412 | return type; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | #ifdef WITH_OPENSSL |
| 417 | /* XXX: these are really begging for a table-driven approach */ |
| 418 | int |
| 419 | sshkey_curve_name_to_nid(const char *name) |
| 420 | { |
| 421 | if (strcmp(name, "nistp256") == 0) |
| 422 | return NID_X9_62_prime256v1; |
| 423 | else if (strcmp(name, "nistp384") == 0) |
| 424 | return NID_secp384r1; |
| 425 | # ifdef OPENSSL_HAS_NISTP521 |
| 426 | else if (strcmp(name, "nistp521") == 0) |
| 427 | return NID_secp521r1; |
| 428 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 429 | else |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | u_int |
| 434 | sshkey_curve_nid_to_bits(int nid) |
| 435 | { |
| 436 | switch (nid) { |
| 437 | case NID_X9_62_prime256v1: |
| 438 | return 256; |
| 439 | case NID_secp384r1: |
| 440 | return 384; |
| 441 | # ifdef OPENSSL_HAS_NISTP521 |
| 442 | case NID_secp521r1: |
| 443 | return 521; |
| 444 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 445 | default: |
| 446 | return 0; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | int |
| 451 | sshkey_ecdsa_bits_to_nid(int bits) |
| 452 | { |
| 453 | switch (bits) { |
| 454 | case 256: |
| 455 | return NID_X9_62_prime256v1; |
| 456 | case 384: |
| 457 | return NID_secp384r1; |
| 458 | # ifdef OPENSSL_HAS_NISTP521 |
| 459 | case 521: |
| 460 | return NID_secp521r1; |
| 461 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 462 | default: |
| 463 | return -1; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | const char * |
| 468 | sshkey_curve_nid_to_name(int nid) |
| 469 | { |
| 470 | switch (nid) { |
| 471 | case NID_X9_62_prime256v1: |
| 472 | return "nistp256"; |
| 473 | case NID_secp384r1: |
| 474 | return "nistp384"; |
| 475 | # ifdef OPENSSL_HAS_NISTP521 |
| 476 | case NID_secp521r1: |
| 477 | return "nistp521"; |
| 478 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 479 | default: |
| 480 | return NULL; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | int |
| 485 | sshkey_ec_nid_to_hash_alg(int nid) |
| 486 | { |
| 487 | int kbits = sshkey_curve_nid_to_bits(nid); |
| 488 | |
| 489 | if (kbits <= 0) |
| 490 | return -1; |
| 491 | |
| 492 | /* RFC5656 section 6.2.1 */ |
| 493 | if (kbits <= 256) |
| 494 | return SSH_DIGEST_SHA256; |
| 495 | else if (kbits <= 384) |
| 496 | return SSH_DIGEST_SHA384; |
| 497 | else |
| 498 | return SSH_DIGEST_SHA512; |
| 499 | } |
| 500 | #endif /* WITH_OPENSSL */ |
| 501 | |
| 502 | static void |
| 503 | cert_free(struct sshkey_cert *cert) |
| 504 | { |
| 505 | u_int i; |
| 506 | |
| 507 | if (cert == NULL) |
| 508 | return; |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 509 | sshbuf_free(cert->certblob); |
| 510 | sshbuf_free(cert->critical); |
| 511 | sshbuf_free(cert->extensions); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 512 | free(cert->key_id); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 513 | for (i = 0; i < cert->nprincipals; i++) |
| 514 | free(cert->principals[i]); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 515 | free(cert->principals); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 516 | sshkey_free(cert->signature_key); |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 517 | free(cert->signature_type); |
jsing@openbsd.org | 4270efa | 2018-02-14 16:03:32 +0000 | [diff] [blame] | 518 | freezero(cert, sizeof(*cert)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static struct sshkey_cert * |
| 522 | cert_new(void) |
| 523 | { |
| 524 | struct sshkey_cert *cert; |
| 525 | |
| 526 | if ((cert = calloc(1, sizeof(*cert))) == NULL) |
| 527 | return NULL; |
| 528 | if ((cert->certblob = sshbuf_new()) == NULL || |
| 529 | (cert->critical = sshbuf_new()) == NULL || |
| 530 | (cert->extensions = sshbuf_new()) == NULL) { |
| 531 | cert_free(cert); |
| 532 | return NULL; |
| 533 | } |
| 534 | cert->key_id = NULL; |
| 535 | cert->principals = NULL; |
| 536 | cert->signature_key = NULL; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 537 | cert->signature_type = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 538 | return cert; |
| 539 | } |
| 540 | |
| 541 | struct sshkey * |
| 542 | sshkey_new(int type) |
| 543 | { |
| 544 | struct sshkey *k; |
| 545 | #ifdef WITH_OPENSSL |
| 546 | RSA *rsa; |
| 547 | DSA *dsa; |
| 548 | #endif /* WITH_OPENSSL */ |
| 549 | |
| 550 | if ((k = calloc(1, sizeof(*k))) == NULL) |
| 551 | return NULL; |
| 552 | k->type = type; |
| 553 | k->ecdsa = NULL; |
| 554 | k->ecdsa_nid = -1; |
| 555 | k->dsa = NULL; |
| 556 | k->rsa = NULL; |
| 557 | k->cert = NULL; |
| 558 | k->ed25519_sk = NULL; |
| 559 | k->ed25519_pk = NULL; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 560 | k->xmss_sk = NULL; |
| 561 | k->xmss_pk = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 562 | switch (k->type) { |
| 563 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 564 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 565 | case KEY_RSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 566 | if ((rsa = RSA_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 567 | free(k); |
| 568 | return NULL; |
| 569 | } |
| 570 | k->rsa = rsa; |
| 571 | break; |
| 572 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 573 | case KEY_DSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 574 | if ((dsa = DSA_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 575 | free(k); |
| 576 | return NULL; |
| 577 | } |
| 578 | k->dsa = dsa; |
| 579 | break; |
| 580 | case KEY_ECDSA: |
| 581 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 582 | case KEY_ECDSA_SK: |
| 583 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 584 | /* Cannot do anything until we know the group */ |
| 585 | break; |
| 586 | #endif /* WITH_OPENSSL */ |
| 587 | case KEY_ED25519: |
| 588 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 589 | case KEY_ED25519_SK: |
| 590 | case KEY_ED25519_SK_CERT: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 591 | case KEY_XMSS: |
| 592 | case KEY_XMSS_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 593 | /* no need to prealloc */ |
| 594 | break; |
| 595 | case KEY_UNSPEC: |
| 596 | break; |
| 597 | default: |
| 598 | free(k); |
| 599 | return NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (sshkey_is_cert(k)) { |
| 603 | if ((k->cert = cert_new()) == NULL) { |
| 604 | sshkey_free(k); |
| 605 | return NULL; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return k; |
| 610 | } |
| 611 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 612 | void |
| 613 | sshkey_free(struct sshkey *k) |
| 614 | { |
| 615 | if (k == NULL) |
| 616 | return; |
| 617 | switch (k->type) { |
| 618 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 619 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 620 | case KEY_RSA_CERT: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 621 | RSA_free(k->rsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 622 | k->rsa = NULL; |
| 623 | break; |
| 624 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 625 | case KEY_DSA_CERT: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 626 | DSA_free(k->dsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 627 | k->dsa = NULL; |
| 628 | break; |
| 629 | # ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 630 | case KEY_ECDSA_SK: |
| 631 | case KEY_ECDSA_SK_CERT: |
| 632 | free(k->sk_application); |
| 633 | sshbuf_free(k->sk_key_handle); |
| 634 | sshbuf_free(k->sk_reserved); |
| 635 | /* FALLTHROUGH */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 636 | case KEY_ECDSA: |
| 637 | case KEY_ECDSA_CERT: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 638 | EC_KEY_free(k->ecdsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 639 | k->ecdsa = NULL; |
| 640 | break; |
| 641 | # endif /* OPENSSL_HAS_ECC */ |
| 642 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 643 | case KEY_ED25519_SK: |
| 644 | case KEY_ED25519_SK_CERT: |
| 645 | free(k->sk_application); |
| 646 | sshbuf_free(k->sk_key_handle); |
| 647 | sshbuf_free(k->sk_reserved); |
| 648 | /* FALLTHROUGH */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 649 | case KEY_ED25519: |
| 650 | case KEY_ED25519_CERT: |
jsing@openbsd.org | 4270efa | 2018-02-14 16:03:32 +0000 | [diff] [blame] | 651 | freezero(k->ed25519_pk, ED25519_PK_SZ); |
| 652 | k->ed25519_pk = NULL; |
| 653 | freezero(k->ed25519_sk, ED25519_SK_SZ); |
| 654 | k->ed25519_sk = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 655 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 656 | #ifdef WITH_XMSS |
| 657 | case KEY_XMSS: |
| 658 | case KEY_XMSS_CERT: |
| 659 | freezero(k->xmss_pk, sshkey_xmss_pklen(k)); |
| 660 | k->xmss_pk = NULL; |
| 661 | freezero(k->xmss_sk, sshkey_xmss_sklen(k)); |
| 662 | k->xmss_sk = NULL; |
| 663 | sshkey_xmss_free_state(k); |
| 664 | free(k->xmss_name); |
| 665 | k->xmss_name = NULL; |
| 666 | free(k->xmss_filename); |
| 667 | k->xmss_filename = NULL; |
| 668 | break; |
| 669 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 670 | case KEY_UNSPEC: |
| 671 | break; |
| 672 | default: |
| 673 | break; |
| 674 | } |
| 675 | if (sshkey_is_cert(k)) |
| 676 | cert_free(k->cert); |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 677 | freezero(k->shielded_private, k->shielded_len); |
| 678 | freezero(k->shield_prekey, k->shield_prekey_len); |
jsing@openbsd.org | 4270efa | 2018-02-14 16:03:32 +0000 | [diff] [blame] | 679 | freezero(k, sizeof(*k)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int |
| 683 | cert_compare(struct sshkey_cert *a, struct sshkey_cert *b) |
| 684 | { |
| 685 | if (a == NULL && b == NULL) |
| 686 | return 1; |
| 687 | if (a == NULL || b == NULL) |
| 688 | return 0; |
| 689 | if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob)) |
| 690 | return 0; |
| 691 | if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob), |
| 692 | sshbuf_len(a->certblob)) != 0) |
| 693 | return 0; |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Compare public portions of key only, allowing comparisons between |
| 699 | * certificates and plain keys too. |
| 700 | */ |
| 701 | int |
| 702 | sshkey_equal_public(const struct sshkey *a, const struct sshkey *b) |
| 703 | { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 704 | #if defined(WITH_OPENSSL) |
| 705 | const BIGNUM *rsa_e_a, *rsa_n_a; |
| 706 | const BIGNUM *rsa_e_b, *rsa_n_b; |
| 707 | const BIGNUM *dsa_p_a, *dsa_q_a, *dsa_g_a, *dsa_pub_key_a; |
| 708 | const BIGNUM *dsa_p_b, *dsa_q_b, *dsa_g_b, *dsa_pub_key_b; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 709 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 710 | |
| 711 | if (a == NULL || b == NULL || |
| 712 | sshkey_type_plain(a->type) != sshkey_type_plain(b->type)) |
| 713 | return 0; |
| 714 | |
| 715 | switch (a->type) { |
| 716 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 717 | case KEY_RSA_CERT: |
| 718 | case KEY_RSA: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 719 | if (a->rsa == NULL || b->rsa == NULL) |
| 720 | return 0; |
| 721 | RSA_get0_key(a->rsa, &rsa_n_a, &rsa_e_a, NULL); |
| 722 | RSA_get0_key(b->rsa, &rsa_n_b, &rsa_e_b, NULL); |
| 723 | return BN_cmp(rsa_e_a, rsa_e_b) == 0 && |
| 724 | BN_cmp(rsa_n_a, rsa_n_b) == 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 725 | case KEY_DSA_CERT: |
| 726 | case KEY_DSA: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 727 | if (a->dsa == NULL || b->dsa == NULL) |
| 728 | return 0; |
| 729 | DSA_get0_pqg(a->dsa, &dsa_p_a, &dsa_q_a, &dsa_g_a); |
| 730 | DSA_get0_pqg(b->dsa, &dsa_p_b, &dsa_q_b, &dsa_g_b); |
| 731 | DSA_get0_key(a->dsa, &dsa_pub_key_a, NULL); |
| 732 | DSA_get0_key(b->dsa, &dsa_pub_key_b, NULL); |
| 733 | return BN_cmp(dsa_p_a, dsa_p_b) == 0 && |
| 734 | BN_cmp(dsa_q_a, dsa_q_b) == 0 && |
| 735 | BN_cmp(dsa_g_a, dsa_g_b) == 0 && |
| 736 | BN_cmp(dsa_pub_key_a, dsa_pub_key_b) == 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 737 | # ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 738 | case KEY_ECDSA_SK: |
| 739 | case KEY_ECDSA_SK_CERT: |
| 740 | if (a->sk_application == NULL || b->sk_application == NULL) |
| 741 | return 0; |
| 742 | if (strcmp(a->sk_application, b->sk_application) != 0) |
| 743 | return 0; |
| 744 | /* FALLTHROUGH */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 745 | case KEY_ECDSA_CERT: |
| 746 | case KEY_ECDSA: |
| 747 | if (a->ecdsa == NULL || b->ecdsa == NULL || |
| 748 | EC_KEY_get0_public_key(a->ecdsa) == NULL || |
| 749 | EC_KEY_get0_public_key(b->ecdsa) == NULL) |
| 750 | return 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 751 | if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa), |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 752 | EC_KEY_get0_group(b->ecdsa), NULL) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 753 | EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa), |
| 754 | EC_KEY_get0_public_key(a->ecdsa), |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 755 | EC_KEY_get0_public_key(b->ecdsa), NULL) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 756 | return 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 757 | return 1; |
| 758 | # endif /* OPENSSL_HAS_ECC */ |
| 759 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 760 | case KEY_ED25519_SK: |
| 761 | case KEY_ED25519_SK_CERT: |
| 762 | if (a->sk_application == NULL || b->sk_application == NULL) |
| 763 | return 0; |
| 764 | if (strcmp(a->sk_application, b->sk_application) != 0) |
| 765 | return 0; |
| 766 | /* FALLTHROUGH */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 767 | case KEY_ED25519: |
| 768 | case KEY_ED25519_CERT: |
| 769 | return a->ed25519_pk != NULL && b->ed25519_pk != NULL && |
| 770 | memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 771 | #ifdef WITH_XMSS |
| 772 | case KEY_XMSS: |
| 773 | case KEY_XMSS_CERT: |
| 774 | return a->xmss_pk != NULL && b->xmss_pk != NULL && |
| 775 | sshkey_xmss_pklen(a) == sshkey_xmss_pklen(b) && |
| 776 | memcmp(a->xmss_pk, b->xmss_pk, sshkey_xmss_pklen(a)) == 0; |
| 777 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 778 | default: |
| 779 | return 0; |
| 780 | } |
| 781 | /* NOTREACHED */ |
| 782 | } |
| 783 | |
| 784 | int |
| 785 | sshkey_equal(const struct sshkey *a, const struct sshkey *b) |
| 786 | { |
| 787 | if (a == NULL || b == NULL || a->type != b->type) |
| 788 | return 0; |
| 789 | if (sshkey_is_cert(a)) { |
| 790 | if (!cert_compare(a->cert, b->cert)) |
| 791 | return 0; |
| 792 | } |
| 793 | return sshkey_equal_public(a, b); |
| 794 | } |
| 795 | |
| 796 | static int |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 797 | to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain, |
| 798 | enum sshkey_serialize_rep opts) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 799 | { |
| 800 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
| 801 | const char *typename; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 802 | #ifdef WITH_OPENSSL |
| 803 | const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key; |
| 804 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 805 | |
| 806 | if (key == NULL) |
| 807 | return SSH_ERR_INVALID_ARGUMENT; |
| 808 | |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 809 | if (sshkey_is_cert(key)) { |
| 810 | if (key->cert == NULL) |
| 811 | return SSH_ERR_EXPECTED_CERT; |
| 812 | if (sshbuf_len(key->cert->certblob) == 0) |
| 813 | return SSH_ERR_KEY_LACKS_CERTBLOB; |
| 814 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 815 | type = force_plain ? sshkey_type_plain(key->type) : key->type; |
| 816 | typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid); |
| 817 | |
| 818 | switch (type) { |
| 819 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 820 | case KEY_DSA_CERT: |
| 821 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 822 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 823 | case KEY_RSA_CERT: |
| 824 | #endif /* WITH_OPENSSL */ |
| 825 | case KEY_ED25519_CERT: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 826 | #ifdef WITH_XMSS |
| 827 | case KEY_XMSS_CERT: |
| 828 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 829 | /* Use the existing blob */ |
| 830 | /* XXX modified flag? */ |
| 831 | if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0) |
| 832 | return ret; |
| 833 | break; |
| 834 | #ifdef WITH_OPENSSL |
| 835 | case KEY_DSA: |
| 836 | if (key->dsa == NULL) |
| 837 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 838 | DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g); |
| 839 | DSA_get0_key(key->dsa, &dsa_pub_key, NULL); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 840 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 841 | (ret = sshbuf_put_bignum2(b, dsa_p)) != 0 || |
| 842 | (ret = sshbuf_put_bignum2(b, dsa_q)) != 0 || |
| 843 | (ret = sshbuf_put_bignum2(b, dsa_g)) != 0 || |
| 844 | (ret = sshbuf_put_bignum2(b, dsa_pub_key)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 845 | return ret; |
| 846 | break; |
Darren Tucker | d1a0421 | 2014-07-19 07:23:55 +1000 | [diff] [blame] | 847 | # ifdef OPENSSL_HAS_ECC |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 848 | case KEY_ECDSA: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 849 | case KEY_ECDSA_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 850 | if (key->ecdsa == NULL) |
| 851 | return SSH_ERR_INVALID_ARGUMENT; |
| 852 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 853 | (ret = sshbuf_put_cstring(b, |
| 854 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
| 855 | (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0) |
| 856 | return ret; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 857 | if (type == KEY_ECDSA_SK) { |
| 858 | if ((ret = sshbuf_put_cstring(b, |
| 859 | key->sk_application)) != 0) |
| 860 | return ret; |
| 861 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 862 | break; |
Darren Tucker | d1a0421 | 2014-07-19 07:23:55 +1000 | [diff] [blame] | 863 | # endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 864 | case KEY_RSA: |
| 865 | if (key->rsa == NULL) |
| 866 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 867 | RSA_get0_key(key->rsa, &rsa_n, &rsa_e, NULL); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 868 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 869 | (ret = sshbuf_put_bignum2(b, rsa_e)) != 0 || |
| 870 | (ret = sshbuf_put_bignum2(b, rsa_n)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 871 | return ret; |
| 872 | break; |
| 873 | #endif /* WITH_OPENSSL */ |
| 874 | case KEY_ED25519: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 875 | case KEY_ED25519_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 876 | if (key->ed25519_pk == NULL) |
| 877 | return SSH_ERR_INVALID_ARGUMENT; |
| 878 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 879 | (ret = sshbuf_put_string(b, |
| 880 | key->ed25519_pk, ED25519_PK_SZ)) != 0) |
| 881 | return ret; |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 882 | if (type == KEY_ED25519_SK) { |
| 883 | if ((ret = sshbuf_put_cstring(b, |
| 884 | key->sk_application)) != 0) |
| 885 | return ret; |
| 886 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 887 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 888 | #ifdef WITH_XMSS |
| 889 | case KEY_XMSS: |
| 890 | if (key->xmss_name == NULL || key->xmss_pk == NULL || |
| 891 | sshkey_xmss_pklen(key) == 0) |
| 892 | return SSH_ERR_INVALID_ARGUMENT; |
| 893 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 894 | (ret = sshbuf_put_cstring(b, key->xmss_name)) != 0 || |
| 895 | (ret = sshbuf_put_string(b, |
| 896 | key->xmss_pk, sshkey_xmss_pklen(key))) != 0 || |
| 897 | (ret = sshkey_xmss_serialize_pk_info(key, b, opts)) != 0) |
| 898 | return ret; |
| 899 | break; |
| 900 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 901 | default: |
| 902 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 903 | } |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 908 | sshkey_putb(const struct sshkey *key, struct sshbuf *b) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 909 | { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 910 | return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | int |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 914 | sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b, |
| 915 | enum sshkey_serialize_rep opts) |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 916 | { |
| 917 | struct sshbuf *tmp; |
| 918 | int r; |
| 919 | |
| 920 | if ((tmp = sshbuf_new()) == NULL) |
| 921 | return SSH_ERR_ALLOC_FAIL; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 922 | r = to_blob_buf(key, tmp, 0, opts); |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 923 | if (r == 0) |
| 924 | r = sshbuf_put_stringb(b, tmp); |
| 925 | sshbuf_free(tmp); |
| 926 | return r; |
| 927 | } |
| 928 | |
| 929 | int |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 930 | sshkey_puts(const struct sshkey *key, struct sshbuf *b) |
| 931 | { |
| 932 | return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT); |
| 933 | } |
| 934 | |
| 935 | int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 936 | sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 937 | { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 938 | return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static int |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 942 | to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain, |
| 943 | enum sshkey_serialize_rep opts) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 944 | { |
| 945 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 946 | size_t len; |
| 947 | struct sshbuf *b = NULL; |
| 948 | |
| 949 | if (lenp != NULL) |
| 950 | *lenp = 0; |
| 951 | if (blobp != NULL) |
| 952 | *blobp = NULL; |
| 953 | if ((b = sshbuf_new()) == NULL) |
| 954 | return SSH_ERR_ALLOC_FAIL; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 955 | if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 956 | goto out; |
| 957 | len = sshbuf_len(b); |
| 958 | if (lenp != NULL) |
| 959 | *lenp = len; |
| 960 | if (blobp != NULL) { |
| 961 | if ((*blobp = malloc(len)) == NULL) { |
| 962 | ret = SSH_ERR_ALLOC_FAIL; |
| 963 | goto out; |
| 964 | } |
| 965 | memcpy(*blobp, sshbuf_ptr(b), len); |
| 966 | } |
| 967 | ret = 0; |
| 968 | out: |
| 969 | sshbuf_free(b); |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | int |
| 974 | sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
| 975 | { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 976 | return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | int |
| 980 | sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
| 981 | { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 982 | return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | int |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 986 | sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 987 | u_char **retp, size_t *lenp) |
| 988 | { |
| 989 | u_char *blob = NULL, *ret = NULL; |
| 990 | size_t blob_len = 0; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 991 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 992 | |
| 993 | if (retp != NULL) |
| 994 | *retp = NULL; |
| 995 | if (lenp != NULL) |
| 996 | *lenp = 0; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 997 | if (ssh_digest_bytes(dgst_alg) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 998 | r = SSH_ERR_INVALID_ARGUMENT; |
| 999 | goto out; |
| 1000 | } |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1001 | if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT)) |
| 1002 | != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1003 | goto out; |
| 1004 | if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) { |
| 1005 | r = SSH_ERR_ALLOC_FAIL; |
| 1006 | goto out; |
| 1007 | } |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1008 | if ((r = ssh_digest_memory(dgst_alg, blob, blob_len, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1009 | ret, SSH_DIGEST_MAX_LENGTH)) != 0) |
| 1010 | goto out; |
| 1011 | /* success */ |
| 1012 | if (retp != NULL) { |
| 1013 | *retp = ret; |
| 1014 | ret = NULL; |
| 1015 | } |
| 1016 | if (lenp != NULL) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1017 | *lenp = ssh_digest_bytes(dgst_alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1018 | r = 0; |
| 1019 | out: |
| 1020 | free(ret); |
| 1021 | if (blob != NULL) { |
| 1022 | explicit_bzero(blob, blob_len); |
| 1023 | free(blob); |
| 1024 | } |
| 1025 | return r; |
| 1026 | } |
| 1027 | |
| 1028 | static char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1029 | fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1030 | { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1031 | char *ret; |
| 1032 | size_t plen = strlen(alg) + 1; |
| 1033 | size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1034 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1035 | if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1036 | return NULL; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1037 | strlcpy(ret, alg, rlen); |
| 1038 | strlcat(ret, ":", rlen); |
| 1039 | if (dgst_raw_len == 0) |
| 1040 | return ret; |
dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 1041 | if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) { |
jsing@openbsd.org | 4270efa | 2018-02-14 16:03:32 +0000 | [diff] [blame] | 1042 | freezero(ret, rlen); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1043 | return NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1044 | } |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1045 | /* Trim padding characters from end */ |
| 1046 | ret[strcspn(ret, "=")] = '\0'; |
| 1047 | return ret; |
| 1048 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1049 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1050 | static char * |
| 1051 | fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
| 1052 | { |
| 1053 | char *retval, hex[5]; |
| 1054 | size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2; |
| 1055 | |
| 1056 | if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL) |
| 1057 | return NULL; |
| 1058 | strlcpy(retval, alg, rlen); |
| 1059 | strlcat(retval, ":", rlen); |
| 1060 | for (i = 0; i < dgst_raw_len; i++) { |
| 1061 | snprintf(hex, sizeof(hex), "%s%02x", |
| 1062 | i > 0 ? ":" : "", dgst_raw[i]); |
| 1063 | strlcat(retval, hex, rlen); |
| 1064 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1065 | return retval; |
| 1066 | } |
| 1067 | |
| 1068 | static char * |
| 1069 | fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len) |
| 1070 | { |
| 1071 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
| 1072 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
| 1073 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
| 1074 | u_int i, j = 0, rounds, seed = 1; |
| 1075 | char *retval; |
| 1076 | |
| 1077 | rounds = (dgst_raw_len / 2) + 1; |
| 1078 | if ((retval = calloc(rounds, 6)) == NULL) |
| 1079 | return NULL; |
| 1080 | retval[j++] = 'x'; |
| 1081 | for (i = 0; i < rounds; i++) { |
| 1082 | u_int idx0, idx1, idx2, idx3, idx4; |
| 1083 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 1084 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
| 1085 | seed) % 6; |
| 1086 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 1087 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
| 1088 | (seed / 6)) % 6; |
| 1089 | retval[j++] = vowels[idx0]; |
| 1090 | retval[j++] = consonants[idx1]; |
| 1091 | retval[j++] = vowels[idx2]; |
| 1092 | if ((i + 1) < rounds) { |
| 1093 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
| 1094 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
| 1095 | retval[j++] = consonants[idx3]; |
| 1096 | retval[j++] = '-'; |
| 1097 | retval[j++] = consonants[idx4]; |
| 1098 | seed = ((seed * 5) + |
| 1099 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 1100 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
| 1101 | } |
| 1102 | } else { |
| 1103 | idx0 = seed % 6; |
| 1104 | idx1 = 16; |
| 1105 | idx2 = seed / 6; |
| 1106 | retval[j++] = vowels[idx0]; |
| 1107 | retval[j++] = consonants[idx1]; |
| 1108 | retval[j++] = vowels[idx2]; |
| 1109 | } |
| 1110 | } |
| 1111 | retval[j++] = 'x'; |
| 1112 | retval[j++] = '\0'; |
| 1113 | return retval; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Draw an ASCII-Art representing the fingerprint so human brain can |
| 1118 | * profit from its built-in pattern recognition ability. |
| 1119 | * This technique is called "random art" and can be found in some |
| 1120 | * scientific publications like this original paper: |
| 1121 | * |
| 1122 | * "Hash Visualization: a New Technique to improve Real-World Security", |
| 1123 | * Perrig A. and Song D., 1999, International Workshop on Cryptographic |
| 1124 | * Techniques and E-Commerce (CrypTEC '99) |
| 1125 | * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf |
| 1126 | * |
| 1127 | * The subject came up in a talk by Dan Kaminsky, too. |
| 1128 | * |
| 1129 | * If you see the picture is different, the key is different. |
| 1130 | * If the picture looks the same, you still know nothing. |
| 1131 | * |
| 1132 | * The algorithm used here is a worm crawling over a discrete plane, |
| 1133 | * leaving a trace (augmenting the field) everywhere it goes. |
| 1134 | * Movement is taken from dgst_raw 2bit-wise. Bumping into walls |
| 1135 | * makes the respective movement vector be ignored for this turn. |
| 1136 | * Graphs are not unambiguous, because circles in graphs can be |
| 1137 | * walked in either direction. |
| 1138 | */ |
| 1139 | |
| 1140 | /* |
| 1141 | * Field sizes for the random art. Have to be odd, so the starting point |
| 1142 | * can be in the exact middle of the picture, and FLDBASE should be >=8 . |
| 1143 | * Else pictures would be too dense, and drawing the frame would |
| 1144 | * fail, too, because the key type would not fit in anymore. |
| 1145 | */ |
| 1146 | #define FLDBASE 8 |
| 1147 | #define FLDSIZE_Y (FLDBASE + 1) |
| 1148 | #define FLDSIZE_X (FLDBASE * 2 + 1) |
| 1149 | static char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1150 | fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1151 | const struct sshkey *k) |
| 1152 | { |
| 1153 | /* |
| 1154 | * Chars to be used after each other every time the worm |
| 1155 | * intersects with itself. Matter of taste. |
| 1156 | */ |
| 1157 | char *augmentation_string = " .o+=*BOX@%&#/^SE"; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1158 | char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1159 | u_char field[FLDSIZE_X][FLDSIZE_Y]; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1160 | size_t i, tlen, hlen; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1161 | u_int b; |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1162 | int x, y, r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1163 | size_t len = strlen(augmentation_string) - 1; |
| 1164 | |
| 1165 | if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL) |
| 1166 | return NULL; |
| 1167 | |
| 1168 | /* initialize field */ |
| 1169 | memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char)); |
| 1170 | x = FLDSIZE_X / 2; |
| 1171 | y = FLDSIZE_Y / 2; |
| 1172 | |
| 1173 | /* process raw key */ |
| 1174 | for (i = 0; i < dgst_raw_len; i++) { |
| 1175 | int input; |
| 1176 | /* each byte conveys four 2-bit move commands */ |
| 1177 | input = dgst_raw[i]; |
| 1178 | for (b = 0; b < 4; b++) { |
| 1179 | /* evaluate 2 bit, rest is shifted later */ |
| 1180 | x += (input & 0x1) ? 1 : -1; |
| 1181 | y += (input & 0x2) ? 1 : -1; |
| 1182 | |
| 1183 | /* assure we are still in bounds */ |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1184 | x = MAXIMUM(x, 0); |
| 1185 | y = MAXIMUM(y, 0); |
| 1186 | x = MINIMUM(x, FLDSIZE_X - 1); |
| 1187 | y = MINIMUM(y, FLDSIZE_Y - 1); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1188 | |
| 1189 | /* augment the field */ |
| 1190 | if (field[x][y] < len - 2) |
| 1191 | field[x][y]++; |
| 1192 | input = input >> 2; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* mark starting point and end point*/ |
| 1197 | field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1; |
| 1198 | field[x][y] = len; |
| 1199 | |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1200 | /* assemble title */ |
| 1201 | r = snprintf(title, sizeof(title), "[%s %u]", |
| 1202 | sshkey_type(k), sshkey_size(k)); |
| 1203 | /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */ |
| 1204 | if (r < 0 || r > (int)sizeof(title)) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1205 | r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k)); |
| 1206 | tlen = (r <= 0) ? 0 : strlen(title); |
| 1207 | |
| 1208 | /* assemble hash ID. */ |
| 1209 | r = snprintf(hash, sizeof(hash), "[%s]", alg); |
| 1210 | hlen = (r <= 0) ? 0 : strlen(hash); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1211 | |
| 1212 | /* output upper border */ |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1213 | p = retval; |
| 1214 | *p++ = '+'; |
| 1215 | for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++) |
| 1216 | *p++ = '-'; |
| 1217 | memcpy(p, title, tlen); |
| 1218 | p += tlen; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1219 | for (i += tlen; i < FLDSIZE_X; i++) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1220 | *p++ = '-'; |
| 1221 | *p++ = '+'; |
| 1222 | *p++ = '\n'; |
| 1223 | |
| 1224 | /* output content */ |
| 1225 | for (y = 0; y < FLDSIZE_Y; y++) { |
| 1226 | *p++ = '|'; |
| 1227 | for (x = 0; x < FLDSIZE_X; x++) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1228 | *p++ = augmentation_string[MINIMUM(field[x][y], len)]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1229 | *p++ = '|'; |
| 1230 | *p++ = '\n'; |
| 1231 | } |
| 1232 | |
| 1233 | /* output lower border */ |
| 1234 | *p++ = '+'; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1235 | for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++) |
| 1236 | *p++ = '-'; |
| 1237 | memcpy(p, hash, hlen); |
| 1238 | p += hlen; |
| 1239 | for (i += hlen; i < FLDSIZE_X; i++) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1240 | *p++ = '-'; |
| 1241 | *p++ = '+'; |
| 1242 | |
| 1243 | return retval; |
| 1244 | } |
| 1245 | |
| 1246 | char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1247 | sshkey_fingerprint(const struct sshkey *k, int dgst_alg, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1248 | enum sshkey_fp_rep dgst_rep) |
| 1249 | { |
| 1250 | char *retval = NULL; |
| 1251 | u_char *dgst_raw; |
| 1252 | size_t dgst_raw_len; |
| 1253 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1254 | if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1255 | return NULL; |
| 1256 | switch (dgst_rep) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1257 | case SSH_FP_DEFAULT: |
| 1258 | if (dgst_alg == SSH_DIGEST_MD5) { |
| 1259 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
| 1260 | dgst_raw, dgst_raw_len); |
| 1261 | } else { |
| 1262 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
| 1263 | dgst_raw, dgst_raw_len); |
| 1264 | } |
| 1265 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1266 | case SSH_FP_HEX: |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1267 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
| 1268 | dgst_raw, dgst_raw_len); |
| 1269 | break; |
| 1270 | case SSH_FP_BASE64: |
| 1271 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
| 1272 | dgst_raw, dgst_raw_len); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1273 | break; |
| 1274 | case SSH_FP_BUBBLEBABBLE: |
| 1275 | retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
| 1276 | break; |
| 1277 | case SSH_FP_RANDOMART: |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1278 | retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg), |
| 1279 | dgst_raw, dgst_raw_len, k); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1280 | break; |
| 1281 | default: |
| 1282 | explicit_bzero(dgst_raw, dgst_raw_len); |
| 1283 | free(dgst_raw); |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | explicit_bzero(dgst_raw, dgst_raw_len); |
| 1287 | free(dgst_raw); |
| 1288 | return retval; |
| 1289 | } |
| 1290 | |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1291 | static int |
| 1292 | peek_type_nid(const char *s, size_t l, int *nid) |
| 1293 | { |
| 1294 | const struct keytype *kt; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1295 | |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1296 | for (kt = keytypes; kt->type != -1; kt++) { |
| 1297 | if (kt->name == NULL || strlen(kt->name) != l) |
| 1298 | continue; |
| 1299 | if (memcmp(s, kt->name, l) == 0) { |
| 1300 | *nid = -1; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1301 | if (key_type_is_ecdsa_variant(kt->type)) |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1302 | *nid = kt->nid; |
| 1303 | return kt->type; |
| 1304 | } |
| 1305 | } |
| 1306 | return KEY_UNSPEC; |
| 1307 | } |
| 1308 | |
| 1309 | /* XXX this can now be made const char * */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1310 | int |
| 1311 | sshkey_read(struct sshkey *ret, char **cpp) |
| 1312 | { |
| 1313 | struct sshkey *k; |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1314 | char *cp, *blobcopy; |
| 1315 | size_t space; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1316 | int r, type, curve_nid = -1; |
| 1317 | struct sshbuf *blob; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1318 | |
dtucker@openbsd.org | 7fadbb6 | 2017-03-10 03:48:57 +0000 | [diff] [blame] | 1319 | if (ret == NULL) |
| 1320 | return SSH_ERR_INVALID_ARGUMENT; |
| 1321 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1322 | switch (ret->type) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1323 | case KEY_UNSPEC: |
| 1324 | case KEY_RSA: |
| 1325 | case KEY_DSA: |
| 1326 | case KEY_ECDSA: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1327 | case KEY_ECDSA_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1328 | case KEY_ED25519: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 1329 | case KEY_ED25519_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1330 | case KEY_DSA_CERT: |
| 1331 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1332 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1333 | case KEY_RSA_CERT: |
| 1334 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 1335 | case KEY_ED25519_SK_CERT: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1336 | #ifdef WITH_XMSS |
| 1337 | case KEY_XMSS: |
| 1338 | case KEY_XMSS_CERT: |
| 1339 | #endif /* WITH_XMSS */ |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1340 | break; /* ok */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1341 | default: |
| 1342 | return SSH_ERR_INVALID_ARGUMENT; |
| 1343 | } |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* Decode type */ |
| 1346 | cp = *cpp; |
| 1347 | space = strcspn(cp, " \t"); |
| 1348 | if (space == strlen(cp)) |
| 1349 | return SSH_ERR_INVALID_FORMAT; |
| 1350 | if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC) |
| 1351 | return SSH_ERR_INVALID_FORMAT; |
| 1352 | |
| 1353 | /* skip whitespace */ |
| 1354 | for (cp += space; *cp == ' ' || *cp == '\t'; cp++) |
| 1355 | ; |
| 1356 | if (*cp == '\0') |
| 1357 | return SSH_ERR_INVALID_FORMAT; |
| 1358 | if (ret->type != KEY_UNSPEC && ret->type != type) |
| 1359 | return SSH_ERR_KEY_TYPE_MISMATCH; |
| 1360 | if ((blob = sshbuf_new()) == NULL) |
| 1361 | return SSH_ERR_ALLOC_FAIL; |
| 1362 | |
| 1363 | /* find end of keyblob and decode */ |
| 1364 | space = strcspn(cp, " \t"); |
| 1365 | if ((blobcopy = strndup(cp, space)) == NULL) { |
| 1366 | sshbuf_free(blob); |
| 1367 | return SSH_ERR_ALLOC_FAIL; |
| 1368 | } |
| 1369 | if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) { |
| 1370 | free(blobcopy); |
| 1371 | sshbuf_free(blob); |
| 1372 | return r; |
| 1373 | } |
| 1374 | free(blobcopy); |
| 1375 | if ((r = sshkey_fromb(blob, &k)) != 0) { |
| 1376 | sshbuf_free(blob); |
| 1377 | return r; |
| 1378 | } |
| 1379 | sshbuf_free(blob); |
| 1380 | |
| 1381 | /* skip whitespace and leave cp at start of comment */ |
| 1382 | for (cp += space; *cp == ' ' || *cp == '\t'; cp++) |
| 1383 | ; |
| 1384 | |
| 1385 | /* ensure type of blob matches type at start of line */ |
| 1386 | if (k->type != type) { |
| 1387 | sshkey_free(k); |
| 1388 | return SSH_ERR_KEY_TYPE_MISMATCH; |
| 1389 | } |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1390 | if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) { |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1391 | sshkey_free(k); |
| 1392 | return SSH_ERR_EC_CURVE_MISMATCH; |
| 1393 | } |
| 1394 | |
| 1395 | /* Fill in ret from parsed key */ |
| 1396 | ret->type = type; |
| 1397 | if (sshkey_is_cert(ret)) { |
| 1398 | if (!sshkey_is_cert(k)) { |
| 1399 | sshkey_free(k); |
| 1400 | return SSH_ERR_EXPECTED_CERT; |
| 1401 | } |
| 1402 | if (ret->cert != NULL) |
| 1403 | cert_free(ret->cert); |
| 1404 | ret->cert = k->cert; |
| 1405 | k->cert = NULL; |
| 1406 | } |
| 1407 | switch (sshkey_type_plain(ret->type)) { |
| 1408 | #ifdef WITH_OPENSSL |
| 1409 | case KEY_RSA: |
| 1410 | RSA_free(ret->rsa); |
| 1411 | ret->rsa = k->rsa; |
| 1412 | k->rsa = NULL; |
| 1413 | #ifdef DEBUG_PK |
| 1414 | RSA_print_fp(stderr, ret->rsa, 8); |
| 1415 | #endif |
| 1416 | break; |
| 1417 | case KEY_DSA: |
| 1418 | DSA_free(ret->dsa); |
| 1419 | ret->dsa = k->dsa; |
| 1420 | k->dsa = NULL; |
| 1421 | #ifdef DEBUG_PK |
| 1422 | DSA_print_fp(stderr, ret->dsa, 8); |
| 1423 | #endif |
| 1424 | break; |
| 1425 | # ifdef OPENSSL_HAS_ECC |
| 1426 | case KEY_ECDSA: |
| 1427 | EC_KEY_free(ret->ecdsa); |
| 1428 | ret->ecdsa = k->ecdsa; |
| 1429 | ret->ecdsa_nid = k->ecdsa_nid; |
| 1430 | k->ecdsa = NULL; |
| 1431 | k->ecdsa_nid = -1; |
| 1432 | #ifdef DEBUG_PK |
| 1433 | sshkey_dump_ec_key(ret->ecdsa); |
| 1434 | #endif |
| 1435 | break; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1436 | case KEY_ECDSA_SK: |
| 1437 | EC_KEY_free(ret->ecdsa); |
| 1438 | ret->ecdsa = k->ecdsa; |
| 1439 | ret->ecdsa_nid = k->ecdsa_nid; |
| 1440 | ret->sk_application = k->sk_application; |
| 1441 | k->ecdsa = NULL; |
| 1442 | k->ecdsa_nid = -1; |
| 1443 | k->sk_application = NULL; |
| 1444 | #ifdef DEBUG_PK |
| 1445 | sshkey_dump_ec_key(ret->ecdsa); |
| 1446 | fprintf(stderr, "App: %s\n", ret->sk_application); |
| 1447 | #endif |
| 1448 | break; |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1449 | # endif /* OPENSSL_HAS_ECC */ |
| 1450 | #endif /* WITH_OPENSSL */ |
| 1451 | case KEY_ED25519: |
| 1452 | freezero(ret->ed25519_pk, ED25519_PK_SZ); |
| 1453 | ret->ed25519_pk = k->ed25519_pk; |
| 1454 | k->ed25519_pk = NULL; |
| 1455 | #ifdef DEBUG_PK |
| 1456 | /* XXX */ |
| 1457 | #endif |
| 1458 | break; |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 1459 | case KEY_ED25519_SK: |
| 1460 | freezero(ret->ed25519_pk, ED25519_PK_SZ); |
| 1461 | ret->ed25519_pk = k->ed25519_pk; |
| 1462 | ret->sk_application = k->sk_application; |
| 1463 | k->ed25519_pk = NULL; |
| 1464 | k->sk_application = NULL; |
| 1465 | break; |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1466 | #ifdef WITH_XMSS |
| 1467 | case KEY_XMSS: |
| 1468 | free(ret->xmss_pk); |
| 1469 | ret->xmss_pk = k->xmss_pk; |
| 1470 | k->xmss_pk = NULL; |
| 1471 | free(ret->xmss_state); |
| 1472 | ret->xmss_state = k->xmss_state; |
| 1473 | k->xmss_state = NULL; |
| 1474 | free(ret->xmss_name); |
| 1475 | ret->xmss_name = k->xmss_name; |
| 1476 | k->xmss_name = NULL; |
| 1477 | free(ret->xmss_filename); |
| 1478 | ret->xmss_filename = k->xmss_filename; |
| 1479 | k->xmss_filename = NULL; |
| 1480 | #ifdef DEBUG_PK |
| 1481 | /* XXX */ |
| 1482 | #endif |
| 1483 | break; |
| 1484 | #endif /* WITH_XMSS */ |
| 1485 | default: |
| 1486 | sshkey_free(k); |
| 1487 | return SSH_ERR_INTERNAL_ERROR; |
| 1488 | } |
| 1489 | sshkey_free(k); |
| 1490 | |
| 1491 | /* success */ |
| 1492 | *cpp = cp; |
| 1493 | return 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1494 | } |
| 1495 | |
djm@openbsd.org | 94b4e2d | 2018-03-02 02:08:03 +0000 | [diff] [blame] | 1496 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1497 | int |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1498 | sshkey_to_base64(const struct sshkey *key, char **b64p) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1499 | { |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1500 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1501 | struct sshbuf *b = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1502 | char *uu = NULL; |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1503 | |
| 1504 | if (b64p != NULL) |
| 1505 | *b64p = NULL; |
| 1506 | if ((b = sshbuf_new()) == NULL) |
| 1507 | return SSH_ERR_ALLOC_FAIL; |
| 1508 | if ((r = sshkey_putb(key, b)) != 0) |
| 1509 | goto out; |
djm@openbsd.org | 16dd8b2 | 2019-07-16 13:18:39 +0000 | [diff] [blame] | 1510 | if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) { |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1511 | r = SSH_ERR_ALLOC_FAIL; |
| 1512 | goto out; |
| 1513 | } |
| 1514 | /* Success */ |
| 1515 | if (b64p != NULL) { |
| 1516 | *b64p = uu; |
| 1517 | uu = NULL; |
| 1518 | } |
| 1519 | r = 0; |
| 1520 | out: |
| 1521 | sshbuf_free(b); |
| 1522 | free(uu); |
| 1523 | return r; |
| 1524 | } |
| 1525 | |
djm@openbsd.org | 2076e4a | 2017-06-09 06:40:24 +0000 | [diff] [blame] | 1526 | int |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1527 | sshkey_format_text(const struct sshkey *key, struct sshbuf *b) |
| 1528 | { |
| 1529 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1530 | char *uu = NULL; |
| 1531 | |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 1532 | if ((r = sshkey_to_base64(key, &uu)) != 0) |
| 1533 | goto out; |
| 1534 | if ((r = sshbuf_putf(b, "%s %s", |
| 1535 | sshkey_ssh_name(key), uu)) != 0) |
| 1536 | goto out; |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1537 | r = 0; |
| 1538 | out: |
| 1539 | free(uu); |
| 1540 | return r; |
| 1541 | } |
| 1542 | |
| 1543 | int |
| 1544 | sshkey_write(const struct sshkey *key, FILE *f) |
| 1545 | { |
| 1546 | struct sshbuf *b = NULL; |
| 1547 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1548 | |
| 1549 | if ((b = sshbuf_new()) == NULL) |
| 1550 | return SSH_ERR_ALLOC_FAIL; |
| 1551 | if ((r = sshkey_format_text(key, b)) != 0) |
| 1552 | goto out; |
| 1553 | if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) { |
| 1554 | if (feof(f)) |
| 1555 | errno = EPIPE; |
| 1556 | r = SSH_ERR_SYSTEM_ERROR; |
| 1557 | goto out; |
| 1558 | } |
| 1559 | /* Success */ |
| 1560 | r = 0; |
| 1561 | out: |
| 1562 | sshbuf_free(b); |
| 1563 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | const char * |
| 1567 | sshkey_cert_type(const struct sshkey *k) |
| 1568 | { |
| 1569 | switch (k->cert->type) { |
| 1570 | case SSH2_CERT_TYPE_USER: |
| 1571 | return "user"; |
| 1572 | case SSH2_CERT_TYPE_HOST: |
| 1573 | return "host"; |
| 1574 | default: |
| 1575 | return "unknown"; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | #ifdef WITH_OPENSSL |
| 1580 | static int |
| 1581 | rsa_generate_private_key(u_int bits, RSA **rsap) |
| 1582 | { |
| 1583 | RSA *private = NULL; |
| 1584 | BIGNUM *f4 = NULL; |
| 1585 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1586 | |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 1587 | if (rsap == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1588 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 1589 | if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE || |
| 1590 | bits > SSHBUF_MAX_BIGNUM * 8) |
| 1591 | return SSH_ERR_KEY_LENGTH; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1592 | *rsap = NULL; |
| 1593 | if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) { |
| 1594 | ret = SSH_ERR_ALLOC_FAIL; |
| 1595 | goto out; |
| 1596 | } |
| 1597 | if (!BN_set_word(f4, RSA_F4) || |
| 1598 | !RSA_generate_key_ex(private, bits, f4, NULL)) { |
| 1599 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1600 | goto out; |
| 1601 | } |
| 1602 | *rsap = private; |
| 1603 | private = NULL; |
| 1604 | ret = 0; |
| 1605 | out: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1606 | RSA_free(private); |
| 1607 | BN_free(f4); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1608 | return ret; |
| 1609 | } |
| 1610 | |
| 1611 | static int |
| 1612 | dsa_generate_private_key(u_int bits, DSA **dsap) |
| 1613 | { |
| 1614 | DSA *private; |
| 1615 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1616 | |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 1617 | if (dsap == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1618 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 1619 | if (bits != 1024) |
| 1620 | return SSH_ERR_KEY_LENGTH; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1621 | if ((private = DSA_new()) == NULL) { |
| 1622 | ret = SSH_ERR_ALLOC_FAIL; |
| 1623 | goto out; |
| 1624 | } |
| 1625 | *dsap = NULL; |
| 1626 | if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL, |
| 1627 | NULL, NULL) || !DSA_generate_key(private)) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1628 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1629 | goto out; |
| 1630 | } |
| 1631 | *dsap = private; |
| 1632 | private = NULL; |
| 1633 | ret = 0; |
| 1634 | out: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1635 | DSA_free(private); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1636 | return ret; |
| 1637 | } |
| 1638 | |
| 1639 | # ifdef OPENSSL_HAS_ECC |
| 1640 | int |
| 1641 | sshkey_ecdsa_key_to_nid(EC_KEY *k) |
| 1642 | { |
| 1643 | EC_GROUP *eg; |
| 1644 | int nids[] = { |
| 1645 | NID_X9_62_prime256v1, |
| 1646 | NID_secp384r1, |
| 1647 | # ifdef OPENSSL_HAS_NISTP521 |
| 1648 | NID_secp521r1, |
| 1649 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 1650 | -1 |
| 1651 | }; |
| 1652 | int nid; |
| 1653 | u_int i; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1654 | const EC_GROUP *g = EC_KEY_get0_group(k); |
| 1655 | |
| 1656 | /* |
| 1657 | * The group may be stored in a ASN.1 encoded private key in one of two |
| 1658 | * ways: as a "named group", which is reconstituted by ASN.1 object ID |
| 1659 | * or explicit group parameters encoded into the key blob. Only the |
| 1660 | * "named group" case sets the group NID for us, but we can figure |
| 1661 | * it out for the other case by comparing against all the groups that |
| 1662 | * are supported. |
| 1663 | */ |
| 1664 | if ((nid = EC_GROUP_get_curve_name(g)) > 0) |
| 1665 | return nid; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1666 | for (i = 0; nids[i] != -1; i++) { |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 1667 | if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1668 | return -1; |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 1669 | if (EC_GROUP_cmp(g, eg, NULL) == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1670 | break; |
| 1671 | EC_GROUP_free(eg); |
| 1672 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1673 | if (nids[i] != -1) { |
| 1674 | /* Use the group with the NID attached */ |
| 1675 | EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE); |
| 1676 | if (EC_KEY_set_group(k, eg) != 1) { |
| 1677 | EC_GROUP_free(eg); |
| 1678 | return -1; |
| 1679 | } |
| 1680 | } |
| 1681 | return nids[i]; |
| 1682 | } |
| 1683 | |
| 1684 | static int |
| 1685 | ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap) |
| 1686 | { |
| 1687 | EC_KEY *private; |
| 1688 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1689 | |
djm@openbsd.org | 5f02bb1 | 2017-05-08 06:11:06 +0000 | [diff] [blame] | 1690 | if (nid == NULL || ecdsap == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1691 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 5f02bb1 | 2017-05-08 06:11:06 +0000 | [diff] [blame] | 1692 | if ((*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1) |
| 1693 | return SSH_ERR_KEY_LENGTH; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1694 | *ecdsap = NULL; |
| 1695 | if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) { |
| 1696 | ret = SSH_ERR_ALLOC_FAIL; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | if (EC_KEY_generate_key(private) != 1) { |
| 1700 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1701 | goto out; |
| 1702 | } |
| 1703 | EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE); |
| 1704 | *ecdsap = private; |
| 1705 | private = NULL; |
| 1706 | ret = 0; |
| 1707 | out: |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 1708 | EC_KEY_free(private); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1709 | return ret; |
| 1710 | } |
| 1711 | # endif /* OPENSSL_HAS_ECC */ |
| 1712 | #endif /* WITH_OPENSSL */ |
| 1713 | |
| 1714 | int |
| 1715 | sshkey_generate(int type, u_int bits, struct sshkey **keyp) |
| 1716 | { |
| 1717 | struct sshkey *k; |
| 1718 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1719 | |
| 1720 | if (keyp == NULL) |
| 1721 | return SSH_ERR_INVALID_ARGUMENT; |
| 1722 | *keyp = NULL; |
| 1723 | if ((k = sshkey_new(KEY_UNSPEC)) == NULL) |
| 1724 | return SSH_ERR_ALLOC_FAIL; |
| 1725 | switch (type) { |
| 1726 | case KEY_ED25519: |
| 1727 | if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL || |
| 1728 | (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) { |
| 1729 | ret = SSH_ERR_ALLOC_FAIL; |
| 1730 | break; |
| 1731 | } |
| 1732 | crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk); |
| 1733 | ret = 0; |
| 1734 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1735 | #ifdef WITH_XMSS |
| 1736 | case KEY_XMSS: |
| 1737 | ret = sshkey_xmss_generate_private_key(k, bits); |
| 1738 | break; |
| 1739 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1740 | #ifdef WITH_OPENSSL |
| 1741 | case KEY_DSA: |
| 1742 | ret = dsa_generate_private_key(bits, &k->dsa); |
| 1743 | break; |
| 1744 | # ifdef OPENSSL_HAS_ECC |
| 1745 | case KEY_ECDSA: |
| 1746 | ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid, |
| 1747 | &k->ecdsa); |
| 1748 | break; |
| 1749 | # endif /* OPENSSL_HAS_ECC */ |
| 1750 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1751 | ret = rsa_generate_private_key(bits, &k->rsa); |
| 1752 | break; |
| 1753 | #endif /* WITH_OPENSSL */ |
| 1754 | default: |
| 1755 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 1756 | } |
| 1757 | if (ret == 0) { |
| 1758 | k->type = type; |
| 1759 | *keyp = k; |
| 1760 | } else |
| 1761 | sshkey_free(k); |
| 1762 | return ret; |
| 1763 | } |
| 1764 | |
| 1765 | int |
| 1766 | sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key) |
| 1767 | { |
| 1768 | u_int i; |
| 1769 | const struct sshkey_cert *from; |
| 1770 | struct sshkey_cert *to; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1771 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1772 | |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1773 | if (to_key == NULL || (from = from_key->cert) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1774 | return SSH_ERR_INVALID_ARGUMENT; |
| 1775 | |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1776 | if ((to = cert_new()) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1777 | return SSH_ERR_ALLOC_FAIL; |
| 1778 | |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1779 | if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 || |
| 1780 | (r = sshbuf_putb(to->critical, from->critical)) != 0 || |
| 1781 | (r = sshbuf_putb(to->extensions, from->extensions)) != 0) |
| 1782 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1783 | |
| 1784 | to->serial = from->serial; |
| 1785 | to->type = from->type; |
| 1786 | if (from->key_id == NULL) |
| 1787 | to->key_id = NULL; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1788 | else if ((to->key_id = strdup(from->key_id)) == NULL) { |
| 1789 | r = SSH_ERR_ALLOC_FAIL; |
| 1790 | goto out; |
| 1791 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1792 | to->valid_after = from->valid_after; |
| 1793 | to->valid_before = from->valid_before; |
| 1794 | if (from->signature_key == NULL) |
| 1795 | to->signature_key = NULL; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1796 | else if ((r = sshkey_from_private(from->signature_key, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1797 | &to->signature_key)) != 0) |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1798 | goto out; |
| 1799 | if (from->signature_type != NULL && |
| 1800 | (to->signature_type = strdup(from->signature_type)) == NULL) { |
| 1801 | r = SSH_ERR_ALLOC_FAIL; |
| 1802 | goto out; |
| 1803 | } |
| 1804 | if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) { |
| 1805 | r = SSH_ERR_INVALID_ARGUMENT; |
| 1806 | goto out; |
| 1807 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1808 | if (from->nprincipals > 0) { |
| 1809 | if ((to->principals = calloc(from->nprincipals, |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1810 | sizeof(*to->principals))) == NULL) { |
| 1811 | r = SSH_ERR_ALLOC_FAIL; |
| 1812 | goto out; |
| 1813 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1814 | for (i = 0; i < from->nprincipals; i++) { |
| 1815 | to->principals[i] = strdup(from->principals[i]); |
| 1816 | if (to->principals[i] == NULL) { |
| 1817 | to->nprincipals = i; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1818 | r = SSH_ERR_ALLOC_FAIL; |
| 1819 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | to->nprincipals = from->nprincipals; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* success */ |
| 1826 | cert_free(to_key->cert); |
| 1827 | to_key->cert = to; |
| 1828 | to = NULL; |
| 1829 | r = 0; |
| 1830 | out: |
| 1831 | cert_free(to); |
| 1832 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | int |
| 1836 | sshkey_from_private(const struct sshkey *k, struct sshkey **pkp) |
| 1837 | { |
| 1838 | struct sshkey *n = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1839 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1840 | #ifdef WITH_OPENSSL |
| 1841 | const BIGNUM *rsa_n, *rsa_e; |
| 1842 | BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL; |
| 1843 | const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key; |
| 1844 | BIGNUM *dsa_p_dup = NULL, *dsa_q_dup = NULL, *dsa_g_dup = NULL; |
| 1845 | BIGNUM *dsa_pub_key_dup = NULL; |
| 1846 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1847 | |
djm@openbsd.org | 1a2663a | 2015-10-15 23:08:23 +0000 | [diff] [blame] | 1848 | *pkp = NULL; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1849 | if ((n = sshkey_new(k->type)) == NULL) { |
| 1850 | r = SSH_ERR_ALLOC_FAIL; |
| 1851 | goto out; |
| 1852 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1853 | switch (k->type) { |
| 1854 | #ifdef WITH_OPENSSL |
| 1855 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1856 | case KEY_DSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1857 | DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g); |
| 1858 | DSA_get0_key(k->dsa, &dsa_pub_key, NULL); |
| 1859 | if ((dsa_p_dup = BN_dup(dsa_p)) == NULL || |
| 1860 | (dsa_q_dup = BN_dup(dsa_q)) == NULL || |
| 1861 | (dsa_g_dup = BN_dup(dsa_g)) == NULL || |
| 1862 | (dsa_pub_key_dup = BN_dup(dsa_pub_key)) == NULL) { |
| 1863 | r = SSH_ERR_ALLOC_FAIL; |
| 1864 | goto out; |
| 1865 | } |
| 1866 | if (!DSA_set0_pqg(n->dsa, dsa_p_dup, dsa_q_dup, dsa_g_dup)) { |
| 1867 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 1868 | goto out; |
| 1869 | } |
| 1870 | dsa_p_dup = dsa_q_dup = dsa_g_dup = NULL; /* transferred */ |
| 1871 | if (!DSA_set0_key(n->dsa, dsa_pub_key_dup, NULL)) { |
| 1872 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 1873 | goto out; |
| 1874 | } |
| 1875 | dsa_pub_key_dup = NULL; /* transferred */ |
| 1876 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1877 | break; |
| 1878 | # ifdef OPENSSL_HAS_ECC |
| 1879 | case KEY_ECDSA: |
| 1880 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1881 | case KEY_ECDSA_SK: |
| 1882 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1883 | n->ecdsa_nid = k->ecdsa_nid; |
| 1884 | n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid); |
| 1885 | if (n->ecdsa == NULL) { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1886 | r = SSH_ERR_ALLOC_FAIL; |
| 1887 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1888 | } |
| 1889 | if (EC_KEY_set_public_key(n->ecdsa, |
| 1890 | EC_KEY_get0_public_key(k->ecdsa)) != 1) { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1891 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 1892 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1893 | } |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 1894 | if (k->type != KEY_ECDSA_SK && k->type != KEY_ECDSA_SK_CERT) |
| 1895 | break; |
| 1896 | /* Append security-key application string */ |
| 1897 | if ((n->sk_application = strdup(k->sk_application)) == NULL) |
| 1898 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1899 | break; |
| 1900 | # endif /* OPENSSL_HAS_ECC */ |
| 1901 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1902 | case KEY_RSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1903 | RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL); |
| 1904 | if ((rsa_n_dup = BN_dup(rsa_n)) == NULL || |
| 1905 | (rsa_e_dup = BN_dup(rsa_e)) == NULL) { |
| 1906 | r = SSH_ERR_ALLOC_FAIL; |
| 1907 | goto out; |
| 1908 | } |
| 1909 | if (!RSA_set0_key(n->rsa, rsa_n_dup, rsa_e_dup, NULL)) { |
| 1910 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 1911 | goto out; |
| 1912 | } |
| 1913 | rsa_n_dup = rsa_e_dup = NULL; /* transferred */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1914 | break; |
| 1915 | #endif /* WITH_OPENSSL */ |
| 1916 | case KEY_ED25519: |
| 1917 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 1918 | case KEY_ED25519_SK: |
| 1919 | case KEY_ED25519_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1920 | if (k->ed25519_pk != NULL) { |
| 1921 | if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1922 | r = SSH_ERR_ALLOC_FAIL; |
| 1923 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1924 | } |
| 1925 | memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ); |
| 1926 | } |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 1927 | if (k->type != KEY_ED25519_SK && |
| 1928 | k->type != KEY_ED25519_SK_CERT) |
| 1929 | break; |
| 1930 | /* Append security-key application string */ |
| 1931 | if ((n->sk_application = strdup(k->sk_application)) == NULL) |
| 1932 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1933 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1934 | #ifdef WITH_XMSS |
| 1935 | case KEY_XMSS: |
| 1936 | case KEY_XMSS_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1937 | if ((r = sshkey_xmss_init(n, k->xmss_name)) != 0) |
| 1938 | goto out; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1939 | if (k->xmss_pk != NULL) { |
markus@openbsd.org | 4f5e331 | 2019-11-13 22:00:21 +0000 | [diff] [blame] | 1940 | u_int32_t left; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1941 | size_t pklen = sshkey_xmss_pklen(k); |
| 1942 | if (pklen == 0 || sshkey_xmss_pklen(n) != pklen) { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1943 | r = SSH_ERR_INTERNAL_ERROR; |
| 1944 | goto out; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1945 | } |
| 1946 | if ((n->xmss_pk = malloc(pklen)) == NULL) { |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1947 | r = SSH_ERR_ALLOC_FAIL; |
| 1948 | goto out; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1949 | } |
| 1950 | memcpy(n->xmss_pk, k->xmss_pk, pklen); |
markus@openbsd.org | 4f5e331 | 2019-11-13 22:00:21 +0000 | [diff] [blame] | 1951 | /* simulate number of signatures left on pubkey */ |
| 1952 | left = sshkey_xmss_signatures_left(k); |
| 1953 | if (left) |
| 1954 | sshkey_xmss_enable_maxsign(n, left); |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1955 | } |
| 1956 | break; |
| 1957 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1958 | default: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1959 | r = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 1960 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1961 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1962 | if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0) |
| 1963 | goto out; |
| 1964 | /* success */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1965 | *pkp = n; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1966 | n = NULL; |
| 1967 | r = 0; |
| 1968 | out: |
| 1969 | sshkey_free(n); |
Darren Tucker | cce8cbe | 2018-09-15 19:44:06 +1000 | [diff] [blame] | 1970 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1971 | BN_clear_free(rsa_n_dup); |
| 1972 | BN_clear_free(rsa_e_dup); |
| 1973 | BN_clear_free(dsa_p_dup); |
| 1974 | BN_clear_free(dsa_q_dup); |
| 1975 | BN_clear_free(dsa_g_dup); |
| 1976 | BN_clear_free(dsa_pub_key_dup); |
Darren Tucker | cce8cbe | 2018-09-15 19:44:06 +1000 | [diff] [blame] | 1977 | #endif |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 1978 | |
| 1979 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1980 | } |
| 1981 | |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 1982 | int |
| 1983 | sshkey_is_shielded(struct sshkey *k) |
| 1984 | { |
| 1985 | return k != NULL && k->shielded_private != NULL; |
| 1986 | } |
| 1987 | |
| 1988 | int |
| 1989 | sshkey_shield_private(struct sshkey *k) |
| 1990 | { |
| 1991 | struct sshbuf *prvbuf = NULL; |
| 1992 | u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH]; |
| 1993 | struct sshcipher_ctx *cctx = NULL; |
| 1994 | const struct sshcipher *cipher; |
| 1995 | size_t i, enclen = 0; |
| 1996 | struct sshkey *kswap = NULL, tmp; |
| 1997 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1998 | |
| 1999 | #ifdef DEBUG_PK |
| 2000 | fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k)); |
| 2001 | #endif |
| 2002 | if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) { |
| 2003 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2004 | goto out; |
| 2005 | } |
| 2006 | if (cipher_keylen(cipher) + cipher_ivlen(cipher) > |
| 2007 | ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) { |
| 2008 | r = SSH_ERR_INTERNAL_ERROR; |
| 2009 | goto out; |
| 2010 | } |
| 2011 | |
| 2012 | /* Prepare a random pre-key, and from it an ephemeral key */ |
| 2013 | if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) { |
| 2014 | r = SSH_ERR_ALLOC_FAIL; |
| 2015 | goto out; |
| 2016 | } |
| 2017 | arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN); |
| 2018 | if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH, |
| 2019 | prekey, SSHKEY_SHIELD_PREKEY_LEN, |
| 2020 | keyiv, SSH_DIGEST_MAX_LENGTH)) != 0) |
| 2021 | goto out; |
| 2022 | #ifdef DEBUG_PK |
| 2023 | fprintf(stderr, "%s: key+iv\n", __func__); |
| 2024 | sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH), |
| 2025 | stderr); |
| 2026 | #endif |
| 2027 | if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher), |
| 2028 | keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0) |
| 2029 | goto out; |
| 2030 | |
| 2031 | /* Serialise and encrypt the private key using the ephemeral key */ |
| 2032 | if ((prvbuf = sshbuf_new()) == NULL) { |
| 2033 | r = SSH_ERR_ALLOC_FAIL; |
| 2034 | goto out; |
| 2035 | } |
| 2036 | if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0) |
| 2037 | goto out; |
| 2038 | if ((r = sshkey_private_serialize_opt(k, prvbuf, |
markus@openbsd.org | bf21992 | 2019-11-13 07:53:10 +0000 | [diff] [blame] | 2039 | SSHKEY_SERIALIZE_SHIELD)) != 0) |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2040 | goto out; |
| 2041 | /* pad to cipher blocksize */ |
| 2042 | i = 0; |
| 2043 | while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) { |
| 2044 | if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0) |
| 2045 | goto out; |
| 2046 | } |
| 2047 | #ifdef DEBUG_PK |
| 2048 | fprintf(stderr, "%s: serialised\n", __func__); |
| 2049 | sshbuf_dump(prvbuf, stderr); |
| 2050 | #endif |
| 2051 | /* encrypt */ |
| 2052 | enclen = sshbuf_len(prvbuf); |
| 2053 | if ((enc = malloc(enclen)) == NULL) { |
| 2054 | r = SSH_ERR_ALLOC_FAIL; |
| 2055 | goto out; |
| 2056 | } |
| 2057 | if ((r = cipher_crypt(cctx, 0, enc, |
| 2058 | sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0) |
| 2059 | goto out; |
| 2060 | #ifdef DEBUG_PK |
| 2061 | fprintf(stderr, "%s: encrypted\n", __func__); |
| 2062 | sshbuf_dump_data(enc, enclen, stderr); |
| 2063 | #endif |
| 2064 | |
| 2065 | /* Make a scrubbed, public-only copy of our private key argument */ |
| 2066 | if ((r = sshkey_from_private(k, &kswap)) != 0) |
| 2067 | goto out; |
| 2068 | |
| 2069 | /* Swap the private key out (it will be destroyed below) */ |
| 2070 | tmp = *kswap; |
| 2071 | *kswap = *k; |
| 2072 | *k = tmp; |
| 2073 | |
| 2074 | /* Insert the shielded key into our argument */ |
| 2075 | k->shielded_private = enc; |
| 2076 | k->shielded_len = enclen; |
| 2077 | k->shield_prekey = prekey; |
| 2078 | k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN; |
| 2079 | enc = prekey = NULL; /* transferred */ |
| 2080 | enclen = 0; |
| 2081 | |
| 2082 | /* success */ |
| 2083 | r = 0; |
| 2084 | |
| 2085 | out: |
| 2086 | /* XXX behaviour on error - invalidate original private key? */ |
| 2087 | cipher_free(cctx); |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2088 | explicit_bzero(keyiv, sizeof(keyiv)); |
| 2089 | explicit_bzero(&tmp, sizeof(tmp)); |
djm@openbsd.org | b2e3e57 | 2019-06-27 06:29:35 +0000 | [diff] [blame] | 2090 | freezero(enc, enclen); |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2091 | freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN); |
| 2092 | sshkey_free(kswap); |
| 2093 | sshbuf_free(prvbuf); |
| 2094 | return r; |
| 2095 | } |
| 2096 | |
| 2097 | int |
| 2098 | sshkey_unshield_private(struct sshkey *k) |
| 2099 | { |
| 2100 | struct sshbuf *prvbuf = NULL; |
| 2101 | u_char pad, *cp, keyiv[SSH_DIGEST_MAX_LENGTH]; |
| 2102 | struct sshcipher_ctx *cctx = NULL; |
| 2103 | const struct sshcipher *cipher; |
| 2104 | size_t i; |
| 2105 | struct sshkey *kswap = NULL, tmp; |
| 2106 | int r = SSH_ERR_INTERNAL_ERROR; |
| 2107 | |
| 2108 | #ifdef DEBUG_PK |
| 2109 | fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k)); |
| 2110 | #endif |
| 2111 | if (!sshkey_is_shielded(k)) |
| 2112 | return 0; /* nothing to do */ |
| 2113 | |
| 2114 | if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) { |
| 2115 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2116 | goto out; |
| 2117 | } |
| 2118 | if (cipher_keylen(cipher) + cipher_ivlen(cipher) > |
| 2119 | ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) { |
| 2120 | r = SSH_ERR_INTERNAL_ERROR; |
| 2121 | goto out; |
| 2122 | } |
| 2123 | /* check size of shielded key blob */ |
| 2124 | if (k->shielded_len < cipher_blocksize(cipher) || |
| 2125 | (k->shielded_len % cipher_blocksize(cipher)) != 0) { |
| 2126 | r = SSH_ERR_INVALID_FORMAT; |
| 2127 | goto out; |
| 2128 | } |
| 2129 | |
| 2130 | /* Calculate the ephemeral key from the prekey */ |
| 2131 | if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH, |
| 2132 | k->shield_prekey, k->shield_prekey_len, |
| 2133 | keyiv, SSH_DIGEST_MAX_LENGTH)) != 0) |
| 2134 | goto out; |
| 2135 | if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher), |
| 2136 | keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0) |
| 2137 | goto out; |
| 2138 | #ifdef DEBUG_PK |
| 2139 | fprintf(stderr, "%s: key+iv\n", __func__); |
| 2140 | sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH), |
| 2141 | stderr); |
| 2142 | #endif |
| 2143 | |
| 2144 | /* Decrypt and parse the shielded private key using the ephemeral key */ |
| 2145 | if ((prvbuf = sshbuf_new()) == NULL) { |
| 2146 | r = SSH_ERR_ALLOC_FAIL; |
| 2147 | goto out; |
| 2148 | } |
| 2149 | if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0) |
| 2150 | goto out; |
| 2151 | /* decrypt */ |
| 2152 | #ifdef DEBUG_PK |
| 2153 | fprintf(stderr, "%s: encrypted\n", __func__); |
| 2154 | sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr); |
| 2155 | #endif |
| 2156 | if ((r = cipher_crypt(cctx, 0, cp, |
| 2157 | k->shielded_private, k->shielded_len, 0, 0)) != 0) |
| 2158 | goto out; |
| 2159 | #ifdef DEBUG_PK |
| 2160 | fprintf(stderr, "%s: serialised\n", __func__); |
| 2161 | sshbuf_dump(prvbuf, stderr); |
| 2162 | #endif |
| 2163 | /* Parse private key */ |
| 2164 | if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0) |
| 2165 | goto out; |
| 2166 | /* Check deterministic padding */ |
| 2167 | i = 0; |
| 2168 | while (sshbuf_len(prvbuf)) { |
| 2169 | if ((r = sshbuf_get_u8(prvbuf, &pad)) != 0) |
| 2170 | goto out; |
| 2171 | if (pad != (++i & 0xff)) { |
| 2172 | r = SSH_ERR_INVALID_FORMAT; |
| 2173 | goto out; |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | /* Swap the parsed key back into place */ |
| 2178 | tmp = *kswap; |
| 2179 | *kswap = *k; |
| 2180 | *k = tmp; |
| 2181 | |
| 2182 | /* success */ |
| 2183 | r = 0; |
| 2184 | |
| 2185 | out: |
| 2186 | cipher_free(cctx); |
| 2187 | explicit_bzero(keyiv, sizeof(keyiv)); |
| 2188 | explicit_bzero(&tmp, sizeof(tmp)); |
| 2189 | sshkey_free(kswap); |
| 2190 | sshbuf_free(prvbuf); |
| 2191 | return r; |
| 2192 | } |
| 2193 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2194 | static int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2195 | cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2196 | { |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2197 | struct sshbuf *principals = NULL, *crit = NULL; |
| 2198 | struct sshbuf *exts = NULL, *ca = NULL; |
| 2199 | u_char *sig = NULL; |
| 2200 | size_t signed_len = 0, slen = 0, kidlen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2201 | int ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2202 | |
| 2203 | /* Copy the entire key blob for verification and later serialisation */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2204 | if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2205 | return ret; |
| 2206 | |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2207 | /* Parse body of certificate up to signature */ |
| 2208 | if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2209 | (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 || |
| 2210 | (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 || |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2211 | (ret = sshbuf_froms(b, &principals)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2212 | (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 || |
| 2213 | (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 || |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2214 | (ret = sshbuf_froms(b, &crit)) != 0 || |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2215 | (ret = sshbuf_froms(b, &exts)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2216 | (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 || |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2217 | (ret = sshbuf_froms(b, &ca)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2218 | /* XXX debug print error for ret */ |
| 2219 | ret = SSH_ERR_INVALID_FORMAT; |
| 2220 | goto out; |
| 2221 | } |
| 2222 | |
| 2223 | /* Signature is left in the buffer so we can calculate this length */ |
| 2224 | signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b); |
| 2225 | |
| 2226 | if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) { |
| 2227 | ret = SSH_ERR_INVALID_FORMAT; |
| 2228 | goto out; |
| 2229 | } |
| 2230 | |
| 2231 | if (key->cert->type != SSH2_CERT_TYPE_USER && |
| 2232 | key->cert->type != SSH2_CERT_TYPE_HOST) { |
| 2233 | ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE; |
| 2234 | goto out; |
| 2235 | } |
| 2236 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2237 | /* Parse principals section */ |
| 2238 | while (sshbuf_len(principals) > 0) { |
| 2239 | char *principal = NULL; |
| 2240 | char **oprincipals = NULL; |
| 2241 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2242 | if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) { |
| 2243 | ret = SSH_ERR_INVALID_FORMAT; |
| 2244 | goto out; |
| 2245 | } |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2246 | if ((ret = sshbuf_get_cstring(principals, &principal, |
| 2247 | NULL)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2248 | ret = SSH_ERR_INVALID_FORMAT; |
| 2249 | goto out; |
| 2250 | } |
| 2251 | oprincipals = key->cert->principals; |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 2252 | key->cert->principals = recallocarray(key->cert->principals, |
| 2253 | key->cert->nprincipals, key->cert->nprincipals + 1, |
| 2254 | sizeof(*key->cert->principals)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2255 | if (key->cert->principals == NULL) { |
| 2256 | free(principal); |
| 2257 | key->cert->principals = oprincipals; |
| 2258 | ret = SSH_ERR_ALLOC_FAIL; |
| 2259 | goto out; |
| 2260 | } |
| 2261 | key->cert->principals[key->cert->nprincipals++] = principal; |
| 2262 | } |
| 2263 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2264 | /* |
| 2265 | * Stash a copies of the critical options and extensions sections |
| 2266 | * for later use. |
| 2267 | */ |
| 2268 | if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 || |
| 2269 | (exts != NULL && |
| 2270 | (ret = sshbuf_putb(key->cert->extensions, exts)) != 0)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2271 | goto out; |
| 2272 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2273 | /* |
| 2274 | * Validate critical options and extensions sections format. |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2275 | */ |
| 2276 | while (sshbuf_len(crit) != 0) { |
| 2277 | if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 || |
| 2278 | (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) { |
| 2279 | sshbuf_reset(key->cert->critical); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2280 | ret = SSH_ERR_INVALID_FORMAT; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | } |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2284 | while (exts != NULL && sshbuf_len(exts) != 0) { |
| 2285 | if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 || |
| 2286 | (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) { |
| 2287 | sshbuf_reset(key->cert->extensions); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2288 | ret = SSH_ERR_INVALID_FORMAT; |
| 2289 | goto out; |
| 2290 | } |
| 2291 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2292 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2293 | /* Parse CA key and check signature */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2294 | if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2295 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2296 | goto out; |
| 2297 | } |
| 2298 | if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) { |
| 2299 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2300 | goto out; |
| 2301 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2302 | if ((ret = sshkey_verify(key->cert->signature_key, sig, slen, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 2303 | sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2304 | goto out; |
djm@openbsd.org | f8df041 | 2019-09-03 08:31:20 +0000 | [diff] [blame] | 2305 | if ((ret = sshkey_get_sigtype(sig, slen, |
| 2306 | &key->cert->signature_type)) != 0) |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 2307 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2308 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2309 | /* Success */ |
| 2310 | ret = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2311 | out: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2312 | sshbuf_free(ca); |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 2313 | sshbuf_free(crit); |
| 2314 | sshbuf_free(exts); |
| 2315 | sshbuf_free(principals); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2316 | free(sig); |
| 2317 | return ret; |
| 2318 | } |
| 2319 | |
Darren Tucker | cce8cbe | 2018-09-15 19:44:06 +1000 | [diff] [blame] | 2320 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2321 | static int |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2322 | check_rsa_length(const RSA *rsa) |
| 2323 | { |
| 2324 | const BIGNUM *rsa_n; |
| 2325 | |
| 2326 | RSA_get0_key(rsa, &rsa_n, NULL, NULL); |
| 2327 | if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE) |
| 2328 | return SSH_ERR_KEY_LENGTH; |
| 2329 | return 0; |
| 2330 | } |
Darren Tucker | cce8cbe | 2018-09-15 19:44:06 +1000 | [diff] [blame] | 2331 | #endif |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2332 | |
| 2333 | static int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2334 | sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp, |
| 2335 | int allow_cert) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2336 | { |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 2337 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2338 | char *ktype = NULL, *curve = NULL, *xmss_name = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2339 | struct sshkey *key = NULL; |
| 2340 | size_t len; |
| 2341 | u_char *pk = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2342 | struct sshbuf *copy; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2343 | #if defined(WITH_OPENSSL) |
| 2344 | BIGNUM *rsa_n = NULL, *rsa_e = NULL; |
| 2345 | BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL, *dsa_pub_key = NULL; |
| 2346 | # if defined(OPENSSL_HAS_ECC) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2347 | EC_POINT *q = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2348 | # endif /* OPENSSL_HAS_ECC */ |
| 2349 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2350 | |
| 2351 | #ifdef DEBUG_PK /* XXX */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2352 | sshbuf_dump(b, stderr); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2353 | #endif |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 2354 | if (keyp != NULL) |
| 2355 | *keyp = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2356 | if ((copy = sshbuf_fromb(b)) == NULL) { |
| 2357 | ret = SSH_ERR_ALLOC_FAIL; |
| 2358 | goto out; |
| 2359 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2360 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0) { |
| 2361 | ret = SSH_ERR_INVALID_FORMAT; |
| 2362 | goto out; |
| 2363 | } |
| 2364 | |
| 2365 | type = sshkey_type_from_name(ktype); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2366 | if (!allow_cert && sshkey_type_is_cert(type)) { |
| 2367 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2368 | goto out; |
| 2369 | } |
| 2370 | switch (type) { |
| 2371 | #ifdef WITH_OPENSSL |
| 2372 | case KEY_RSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2373 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2374 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2375 | ret = SSH_ERR_INVALID_FORMAT; |
| 2376 | goto out; |
| 2377 | } |
| 2378 | /* FALLTHROUGH */ |
| 2379 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2380 | if ((key = sshkey_new(type)) == NULL) { |
| 2381 | ret = SSH_ERR_ALLOC_FAIL; |
| 2382 | goto out; |
| 2383 | } |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 2384 | if (sshbuf_get_bignum2(b, &rsa_e) != 0 || |
| 2385 | sshbuf_get_bignum2(b, &rsa_n) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2386 | ret = SSH_ERR_INVALID_FORMAT; |
| 2387 | goto out; |
| 2388 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2389 | if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, NULL)) { |
| 2390 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 2391 | goto out; |
| 2392 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2393 | rsa_n = rsa_e = NULL; /* transferred */ |
| 2394 | if ((ret = check_rsa_length(key->rsa)) != 0) |
| 2395 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2396 | #ifdef DEBUG_PK |
| 2397 | RSA_print_fp(stderr, key->rsa, 8); |
| 2398 | #endif |
| 2399 | break; |
| 2400 | case KEY_DSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2401 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2402 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2403 | ret = SSH_ERR_INVALID_FORMAT; |
| 2404 | goto out; |
| 2405 | } |
| 2406 | /* FALLTHROUGH */ |
| 2407 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2408 | if ((key = sshkey_new(type)) == NULL) { |
| 2409 | ret = SSH_ERR_ALLOC_FAIL; |
| 2410 | goto out; |
| 2411 | } |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 2412 | if (sshbuf_get_bignum2(b, &dsa_p) != 0 || |
| 2413 | sshbuf_get_bignum2(b, &dsa_q) != 0 || |
| 2414 | sshbuf_get_bignum2(b, &dsa_g) != 0 || |
| 2415 | sshbuf_get_bignum2(b, &dsa_pub_key) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2416 | ret = SSH_ERR_INVALID_FORMAT; |
| 2417 | goto out; |
| 2418 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2419 | if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g)) { |
| 2420 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2421 | goto out; |
| 2422 | } |
| 2423 | dsa_p = dsa_q = dsa_g = NULL; /* transferred */ |
| 2424 | if (!DSA_set0_key(key->dsa, dsa_pub_key, NULL)) { |
| 2425 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2426 | goto out; |
| 2427 | } |
| 2428 | dsa_pub_key = NULL; /* transferred */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2429 | #ifdef DEBUG_PK |
| 2430 | DSA_print_fp(stderr, key->dsa, 8); |
| 2431 | #endif |
| 2432 | break; |
| 2433 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2434 | case KEY_ECDSA_SK_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2435 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2436 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2437 | ret = SSH_ERR_INVALID_FORMAT; |
| 2438 | goto out; |
| 2439 | } |
| 2440 | /* FALLTHROUGH */ |
| 2441 | # ifdef OPENSSL_HAS_ECC |
| 2442 | case KEY_ECDSA: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2443 | case KEY_ECDSA_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2444 | if ((key = sshkey_new(type)) == NULL) { |
| 2445 | ret = SSH_ERR_ALLOC_FAIL; |
| 2446 | goto out; |
| 2447 | } |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 2448 | key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2449 | if (sshbuf_get_cstring(b, &curve, NULL) != 0) { |
| 2450 | ret = SSH_ERR_INVALID_FORMAT; |
| 2451 | goto out; |
| 2452 | } |
| 2453 | if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
| 2454 | ret = SSH_ERR_EC_CURVE_MISMATCH; |
| 2455 | goto out; |
| 2456 | } |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 2457 | EC_KEY_free(key->ecdsa); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2458 | if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) |
| 2459 | == NULL) { |
| 2460 | ret = SSH_ERR_EC_CURVE_INVALID; |
| 2461 | goto out; |
| 2462 | } |
| 2463 | if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) { |
| 2464 | ret = SSH_ERR_ALLOC_FAIL; |
| 2465 | goto out; |
| 2466 | } |
| 2467 | if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) { |
| 2468 | ret = SSH_ERR_INVALID_FORMAT; |
| 2469 | goto out; |
| 2470 | } |
| 2471 | if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa), |
| 2472 | q) != 0) { |
| 2473 | ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 2474 | goto out; |
| 2475 | } |
| 2476 | if (EC_KEY_set_public_key(key->ecdsa, q) != 1) { |
| 2477 | /* XXX assume it is a allocation error */ |
| 2478 | ret = SSH_ERR_ALLOC_FAIL; |
| 2479 | goto out; |
| 2480 | } |
| 2481 | #ifdef DEBUG_PK |
| 2482 | sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q); |
| 2483 | #endif |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2484 | if (type == KEY_ECDSA_SK || type == KEY_ECDSA_SK_CERT) { |
| 2485 | /* Parse additional security-key application string */ |
| 2486 | if (sshbuf_get_cstring(b, &key->sk_application, |
| 2487 | NULL) != 0) { |
| 2488 | ret = SSH_ERR_INVALID_FORMAT; |
| 2489 | goto out; |
| 2490 | } |
| 2491 | #ifdef DEBUG_PK |
| 2492 | fprintf(stderr, "App: %s\n", key->sk_application); |
| 2493 | #endif |
| 2494 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2495 | break; |
| 2496 | # endif /* OPENSSL_HAS_ECC */ |
| 2497 | #endif /* WITH_OPENSSL */ |
| 2498 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 2499 | case KEY_ED25519_SK_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2500 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2501 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2502 | ret = SSH_ERR_INVALID_FORMAT; |
| 2503 | goto out; |
| 2504 | } |
| 2505 | /* FALLTHROUGH */ |
| 2506 | case KEY_ED25519: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 2507 | case KEY_ED25519_SK: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2508 | if ((ret = sshbuf_get_string(b, &pk, &len)) != 0) |
| 2509 | goto out; |
| 2510 | if (len != ED25519_PK_SZ) { |
| 2511 | ret = SSH_ERR_INVALID_FORMAT; |
| 2512 | goto out; |
| 2513 | } |
| 2514 | if ((key = sshkey_new(type)) == NULL) { |
| 2515 | ret = SSH_ERR_ALLOC_FAIL; |
| 2516 | goto out; |
| 2517 | } |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 2518 | if (type == KEY_ED25519_SK || type == KEY_ED25519_SK_CERT) { |
| 2519 | /* Parse additional security-key application string */ |
| 2520 | if (sshbuf_get_cstring(b, &key->sk_application, |
| 2521 | NULL) != 0) { |
| 2522 | ret = SSH_ERR_INVALID_FORMAT; |
| 2523 | goto out; |
| 2524 | } |
| 2525 | #ifdef DEBUG_PK |
| 2526 | fprintf(stderr, "App: %s\n", key->sk_application); |
| 2527 | #endif |
| 2528 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2529 | key->ed25519_pk = pk; |
| 2530 | pk = NULL; |
| 2531 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2532 | #ifdef WITH_XMSS |
| 2533 | case KEY_XMSS_CERT: |
| 2534 | /* Skip nonce */ |
| 2535 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2536 | ret = SSH_ERR_INVALID_FORMAT; |
| 2537 | goto out; |
| 2538 | } |
| 2539 | /* FALLTHROUGH */ |
| 2540 | case KEY_XMSS: |
| 2541 | if ((ret = sshbuf_get_cstring(b, &xmss_name, NULL)) != 0) |
| 2542 | goto out; |
| 2543 | if ((key = sshkey_new(type)) == NULL) { |
| 2544 | ret = SSH_ERR_ALLOC_FAIL; |
| 2545 | goto out; |
| 2546 | } |
| 2547 | if ((ret = sshkey_xmss_init(key, xmss_name)) != 0) |
| 2548 | goto out; |
| 2549 | if ((ret = sshbuf_get_string(b, &pk, &len)) != 0) |
| 2550 | goto out; |
| 2551 | if (len == 0 || len != sshkey_xmss_pklen(key)) { |
| 2552 | ret = SSH_ERR_INVALID_FORMAT; |
| 2553 | goto out; |
| 2554 | } |
| 2555 | key->xmss_pk = pk; |
| 2556 | pk = NULL; |
| 2557 | if (type != KEY_XMSS_CERT && |
| 2558 | (ret = sshkey_xmss_deserialize_pk_info(key, b)) != 0) |
| 2559 | goto out; |
| 2560 | break; |
| 2561 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2562 | case KEY_UNSPEC: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2563 | default: |
| 2564 | ret = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2565 | goto out; |
| 2566 | } |
| 2567 | |
| 2568 | /* Parse certificate potion */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2569 | if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2570 | goto out; |
| 2571 | |
| 2572 | if (key != NULL && sshbuf_len(b) != 0) { |
| 2573 | ret = SSH_ERR_INVALID_FORMAT; |
| 2574 | goto out; |
| 2575 | } |
| 2576 | ret = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 2577 | if (keyp != NULL) { |
| 2578 | *keyp = key; |
| 2579 | key = NULL; |
| 2580 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2581 | out: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2582 | sshbuf_free(copy); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2583 | sshkey_free(key); |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2584 | free(xmss_name); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2585 | free(ktype); |
| 2586 | free(curve); |
| 2587 | free(pk); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2588 | #if defined(WITH_OPENSSL) |
| 2589 | BN_clear_free(rsa_n); |
| 2590 | BN_clear_free(rsa_e); |
| 2591 | BN_clear_free(dsa_p); |
| 2592 | BN_clear_free(dsa_q); |
| 2593 | BN_clear_free(dsa_g); |
| 2594 | BN_clear_free(dsa_pub_key); |
| 2595 | # if defined(OPENSSL_HAS_ECC) |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 2596 | EC_POINT_free(q); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2597 | # endif /* OPENSSL_HAS_ECC */ |
| 2598 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2599 | return ret; |
| 2600 | } |
| 2601 | |
| 2602 | int |
| 2603 | sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp) |
| 2604 | { |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2605 | struct sshbuf *b; |
| 2606 | int r; |
| 2607 | |
| 2608 | if ((b = sshbuf_from(blob, blen)) == NULL) |
| 2609 | return SSH_ERR_ALLOC_FAIL; |
| 2610 | r = sshkey_from_blob_internal(b, keyp, 1); |
| 2611 | sshbuf_free(b); |
| 2612 | return r; |
| 2613 | } |
| 2614 | |
| 2615 | int |
| 2616 | sshkey_fromb(struct sshbuf *b, struct sshkey **keyp) |
| 2617 | { |
| 2618 | return sshkey_from_blob_internal(b, keyp, 1); |
| 2619 | } |
| 2620 | |
| 2621 | int |
| 2622 | sshkey_froms(struct sshbuf *buf, struct sshkey **keyp) |
| 2623 | { |
| 2624 | struct sshbuf *b; |
| 2625 | int r; |
| 2626 | |
| 2627 | if ((r = sshbuf_froms(buf, &b)) != 0) |
| 2628 | return r; |
| 2629 | r = sshkey_from_blob_internal(b, keyp, 1); |
| 2630 | sshbuf_free(b); |
| 2631 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2632 | } |
| 2633 | |
djm@openbsd.org | f8df041 | 2019-09-03 08:31:20 +0000 | [diff] [blame] | 2634 | int |
| 2635 | sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep) |
djm@openbsd.org | 931c78d | 2017-12-18 02:22:29 +0000 | [diff] [blame] | 2636 | { |
| 2637 | int r; |
| 2638 | struct sshbuf *b = NULL; |
| 2639 | char *sigtype = NULL; |
| 2640 | |
| 2641 | if (sigtypep != NULL) |
| 2642 | *sigtypep = NULL; |
| 2643 | if ((b = sshbuf_from(sig, siglen)) == NULL) |
| 2644 | return SSH_ERR_ALLOC_FAIL; |
| 2645 | if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0) |
| 2646 | goto out; |
| 2647 | /* success */ |
| 2648 | if (sigtypep != NULL) { |
| 2649 | *sigtypep = sigtype; |
| 2650 | sigtype = NULL; |
| 2651 | } |
| 2652 | r = 0; |
| 2653 | out: |
| 2654 | free(sigtype); |
| 2655 | sshbuf_free(b); |
| 2656 | return r; |
| 2657 | } |
| 2658 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 2659 | /* |
djm@openbsd.org | ba9e788 | 2018-09-12 01:32:54 +0000 | [diff] [blame] | 2660 | * |
| 2661 | * Checks whether a certificate's signature type is allowed. |
| 2662 | * Returns 0 (success) if the certificate signature type appears in the |
| 2663 | * "allowed" pattern-list, or the key is not a certificate to begin with. |
| 2664 | * Otherwise returns a ssherr.h code. |
| 2665 | */ |
| 2666 | int |
| 2667 | sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed) |
| 2668 | { |
| 2669 | if (key == NULL || allowed == NULL) |
| 2670 | return SSH_ERR_INVALID_ARGUMENT; |
| 2671 | if (!sshkey_type_is_cert(key->type)) |
| 2672 | return 0; |
| 2673 | if (key->cert == NULL || key->cert->signature_type == NULL) |
| 2674 | return SSH_ERR_INVALID_ARGUMENT; |
| 2675 | if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1) |
| 2676 | return SSH_ERR_SIGN_ALG_UNSUPPORTED; |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
| 2680 | /* |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 2681 | * Returns the expected signature algorithm for a given public key algorithm. |
| 2682 | */ |
djm@openbsd.org | b4d4eda | 2018-07-03 13:20:25 +0000 | [diff] [blame] | 2683 | const char * |
| 2684 | sshkey_sigalg_by_name(const char *name) |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 2685 | { |
| 2686 | const struct keytype *kt; |
| 2687 | |
| 2688 | for (kt = keytypes; kt->type != -1; kt++) { |
| 2689 | if (strcmp(kt->name, name) != 0) |
| 2690 | continue; |
| 2691 | if (kt->sigalg != NULL) |
| 2692 | return kt->sigalg; |
| 2693 | if (!kt->cert) |
| 2694 | return kt->name; |
| 2695 | return sshkey_ssh_name_from_type_nid( |
| 2696 | sshkey_type_plain(kt->type), kt->nid); |
| 2697 | } |
| 2698 | return NULL; |
| 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * Verifies that the signature algorithm appearing inside the signature blob |
| 2703 | * matches that which was requested. |
| 2704 | */ |
| 2705 | int |
| 2706 | sshkey_check_sigtype(const u_char *sig, size_t siglen, |
| 2707 | const char *requested_alg) |
| 2708 | { |
| 2709 | const char *expected_alg; |
| 2710 | char *sigtype = NULL; |
| 2711 | int r; |
| 2712 | |
| 2713 | if (requested_alg == NULL) |
| 2714 | return 0; |
djm@openbsd.org | b4d4eda | 2018-07-03 13:20:25 +0000 | [diff] [blame] | 2715 | if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL) |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 2716 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | f8df041 | 2019-09-03 08:31:20 +0000 | [diff] [blame] | 2717 | if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 2718 | return r; |
| 2719 | r = strcmp(expected_alg, sigtype) == 0; |
| 2720 | free(sigtype); |
| 2721 | return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED; |
| 2722 | } |
| 2723 | |
djm@openbsd.org | 931c78d | 2017-12-18 02:22:29 +0000 | [diff] [blame] | 2724 | int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2725 | sshkey_sign(struct sshkey *key, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2726 | u_char **sigp, size_t *lenp, |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 2727 | const u_char *data, size_t datalen, |
| 2728 | const char *alg, const char *sk_provider, u_int compat) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2729 | { |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2730 | int was_shielded = sshkey_is_shielded(key); |
| 2731 | int r2, r = SSH_ERR_INTERNAL_ERROR; |
| 2732 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2733 | if (sigp != NULL) |
| 2734 | *sigp = NULL; |
| 2735 | if (lenp != NULL) |
| 2736 | *lenp = 0; |
| 2737 | if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
| 2738 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2739 | if ((r = sshkey_unshield_private(key)) != 0) |
| 2740 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2741 | switch (key->type) { |
| 2742 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2743 | case KEY_DSA_CERT: |
| 2744 | case KEY_DSA: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2745 | r = ssh_dss_sign(key, sigp, lenp, data, datalen, compat); |
| 2746 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2747 | # ifdef OPENSSL_HAS_ECC |
| 2748 | case KEY_ECDSA_CERT: |
| 2749 | case KEY_ECDSA: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2750 | r = ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat); |
| 2751 | break; |
Damien Miller | 03f9205 | 2019-11-01 14:49:25 +1100 | [diff] [blame] | 2752 | # ifdef ENABLE_SK |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 2753 | case KEY_ECDSA_SK_CERT: |
| 2754 | case KEY_ECDSA_SK: |
markus@openbsd.org | e03a29e | 2019-11-12 19:30:50 +0000 | [diff] [blame] | 2755 | r = sshsk_sign(sk_provider, key, sigp, lenp, data, datalen, |
| 2756 | compat); |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 2757 | break; |
Damien Miller | 03f9205 | 2019-11-01 14:49:25 +1100 | [diff] [blame] | 2758 | # endif /* ENABLE_SK */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2759 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2760 | case KEY_RSA_CERT: |
| 2761 | case KEY_RSA: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2762 | r = ssh_rsa_sign(key, sigp, lenp, data, datalen, alg); |
| 2763 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2764 | #endif /* WITH_OPENSSL */ |
| 2765 | case KEY_ED25519: |
| 2766 | case KEY_ED25519_CERT: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2767 | r = ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat); |
| 2768 | break; |
Darren Tucker | 1e0b248 | 2019-11-14 16:08:17 +1100 | [diff] [blame] | 2769 | #ifdef ENABLE_SK |
markus@openbsd.org | fe05a36 | 2019-11-12 19:31:18 +0000 | [diff] [blame] | 2770 | case KEY_ED25519_SK: |
| 2771 | case KEY_ED25519_SK_CERT: |
| 2772 | r = sshsk_sign(sk_provider, key, sigp, lenp, data, datalen, |
| 2773 | compat); |
| 2774 | break; |
Darren Tucker | 1e0b248 | 2019-11-14 16:08:17 +1100 | [diff] [blame] | 2775 | #endif /* ENABLE_SK */ |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2776 | #ifdef WITH_XMSS |
| 2777 | case KEY_XMSS: |
| 2778 | case KEY_XMSS_CERT: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2779 | r = ssh_xmss_sign(key, sigp, lenp, data, datalen, compat); |
| 2780 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2781 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2782 | default: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2783 | r = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2784 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2785 | } |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 2786 | if (was_shielded && (r2 = sshkey_shield_private(key)) != 0) |
| 2787 | return r2; |
| 2788 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | /* |
| 2792 | * ssh_key_verify returns 0 for a correct signature and < 0 on error. |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 2793 | * If "alg" specified, then the signature must use that algorithm. |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2794 | */ |
| 2795 | int |
| 2796 | sshkey_verify(const struct sshkey *key, |
| 2797 | const u_char *sig, size_t siglen, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 2798 | const u_char *data, size_t dlen, const char *alg, u_int compat) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2799 | { |
djm@openbsd.org | 4cf87f4 | 2014-12-10 01:24:09 +0000 | [diff] [blame] | 2800 | if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2801 | return SSH_ERR_INVALID_ARGUMENT; |
| 2802 | switch (key->type) { |
| 2803 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2804 | case KEY_DSA_CERT: |
| 2805 | case KEY_DSA: |
| 2806 | return ssh_dss_verify(key, sig, siglen, data, dlen, compat); |
| 2807 | # ifdef OPENSSL_HAS_ECC |
| 2808 | case KEY_ECDSA_CERT: |
| 2809 | case KEY_ECDSA: |
| 2810 | return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat); |
Damien Miller | 03f9205 | 2019-11-01 14:49:25 +1100 | [diff] [blame] | 2811 | # ifdef ENABLE_SK |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2812 | case KEY_ECDSA_SK_CERT: |
| 2813 | case KEY_ECDSA_SK: |
| 2814 | return ssh_ecdsa_sk_verify(key, sig, siglen, data, dlen, |
| 2815 | compat); |
Damien Miller | 03f9205 | 2019-11-01 14:49:25 +1100 | [diff] [blame] | 2816 | # endif /* ENABLE_SK */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2817 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2818 | case KEY_RSA_CERT: |
| 2819 | case KEY_RSA: |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 2820 | return ssh_rsa_verify(key, sig, siglen, data, dlen, alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2821 | #endif /* WITH_OPENSSL */ |
| 2822 | case KEY_ED25519: |
| 2823 | case KEY_ED25519_CERT: |
| 2824 | return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat); |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 2825 | case KEY_ED25519_SK: |
| 2826 | case KEY_ED25519_SK_CERT: |
| 2827 | return ssh_ed25519_sk_verify(key, sig, siglen, data, dlen, |
| 2828 | compat); |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2829 | #ifdef WITH_XMSS |
| 2830 | case KEY_XMSS: |
| 2831 | case KEY_XMSS_CERT: |
| 2832 | return ssh_xmss_verify(key, sig, siglen, data, dlen, compat); |
| 2833 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2834 | default: |
| 2835 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2836 | } |
| 2837 | } |
| 2838 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2839 | /* Convert a plain key to their _CERT equivalent */ |
| 2840 | int |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2841 | sshkey_to_certified(struct sshkey *k) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2842 | { |
| 2843 | int newtype; |
| 2844 | |
| 2845 | switch (k->type) { |
| 2846 | #ifdef WITH_OPENSSL |
| 2847 | case KEY_RSA: |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2848 | newtype = KEY_RSA_CERT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2849 | break; |
| 2850 | case KEY_DSA: |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2851 | newtype = KEY_DSA_CERT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2852 | break; |
| 2853 | case KEY_ECDSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2854 | newtype = KEY_ECDSA_CERT; |
| 2855 | break; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2856 | case KEY_ECDSA_SK: |
| 2857 | newtype = KEY_ECDSA_SK_CERT; |
| 2858 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2859 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 2860 | case KEY_ED25519_SK: |
| 2861 | newtype = KEY_ED25519_SK_CERT; |
| 2862 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2863 | case KEY_ED25519: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2864 | newtype = KEY_ED25519_CERT; |
| 2865 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2866 | #ifdef WITH_XMSS |
| 2867 | case KEY_XMSS: |
| 2868 | newtype = KEY_XMSS_CERT; |
| 2869 | break; |
| 2870 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2871 | default: |
| 2872 | return SSH_ERR_INVALID_ARGUMENT; |
| 2873 | } |
| 2874 | if ((k->cert = cert_new()) == NULL) |
| 2875 | return SSH_ERR_ALLOC_FAIL; |
| 2876 | k->type = newtype; |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | /* Convert a certificate to its raw key equivalent */ |
| 2881 | int |
| 2882 | sshkey_drop_cert(struct sshkey *k) |
| 2883 | { |
| 2884 | if (!sshkey_type_is_cert(k->type)) |
| 2885 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2886 | cert_free(k->cert); |
| 2887 | k->cert = NULL; |
| 2888 | k->type = sshkey_type_plain(k->type); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
| 2892 | /* Sign a certified key, (re-)generating the signed certblob. */ |
| 2893 | int |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 2894 | sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg, |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 2895 | const char *sk_provider, sshkey_certify_signer *signer, void *signer_ctx) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2896 | { |
| 2897 | struct sshbuf *principals = NULL; |
| 2898 | u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32]; |
| 2899 | size_t i, ca_len, sig_len; |
| 2900 | int ret = SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 2901 | struct sshbuf *cert = NULL; |
| 2902 | char *sigtype = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2903 | #ifdef WITH_OPENSSL |
| 2904 | const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key; |
| 2905 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2906 | |
| 2907 | if (k == NULL || k->cert == NULL || |
| 2908 | k->cert->certblob == NULL || ca == NULL) |
| 2909 | return SSH_ERR_INVALID_ARGUMENT; |
| 2910 | if (!sshkey_is_cert(k)) |
| 2911 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2912 | if (!sshkey_type_is_valid_ca(ca->type)) |
| 2913 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2914 | |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 2915 | /* |
| 2916 | * If no alg specified as argument but a signature_type was set, |
| 2917 | * then prefer that. If both were specified, then they must match. |
| 2918 | */ |
| 2919 | if (alg == NULL) |
| 2920 | alg = k->cert->signature_type; |
| 2921 | else if (k->cert->signature_type != NULL && |
| 2922 | strcmp(alg, k->cert->signature_type) != 0) |
| 2923 | return SSH_ERR_INVALID_ARGUMENT; |
| 2924 | |
djm@openbsd.org | 476e355 | 2019-05-20 00:20:35 +0000 | [diff] [blame] | 2925 | /* |
| 2926 | * If no signing algorithm or signature_type was specified and we're |
| 2927 | * using a RSA key, then default to a good signature algorithm. |
| 2928 | */ |
| 2929 | if (alg == NULL && ca->type == KEY_RSA) |
| 2930 | alg = "rsa-sha2-512"; |
| 2931 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2932 | if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0) |
| 2933 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2934 | |
| 2935 | cert = k->cert->certblob; /* for readability */ |
| 2936 | sshbuf_reset(cert); |
| 2937 | if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0) |
| 2938 | goto out; |
| 2939 | |
| 2940 | /* -v01 certs put nonce first */ |
| 2941 | arc4random_buf(&nonce, sizeof(nonce)); |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2942 | if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0) |
| 2943 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2944 | |
| 2945 | /* XXX this substantially duplicates to_blob(); refactor */ |
| 2946 | switch (k->type) { |
| 2947 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2948 | case KEY_DSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2949 | DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g); |
| 2950 | DSA_get0_key(k->dsa, &dsa_pub_key, NULL); |
| 2951 | if ((ret = sshbuf_put_bignum2(cert, dsa_p)) != 0 || |
| 2952 | (ret = sshbuf_put_bignum2(cert, dsa_q)) != 0 || |
| 2953 | (ret = sshbuf_put_bignum2(cert, dsa_g)) != 0 || |
| 2954 | (ret = sshbuf_put_bignum2(cert, dsa_pub_key)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2955 | goto out; |
| 2956 | break; |
| 2957 | # ifdef OPENSSL_HAS_ECC |
| 2958 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2959 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2960 | if ((ret = sshbuf_put_cstring(cert, |
| 2961 | sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 || |
| 2962 | (ret = sshbuf_put_ec(cert, |
| 2963 | EC_KEY_get0_public_key(k->ecdsa), |
| 2964 | EC_KEY_get0_group(k->ecdsa))) != 0) |
| 2965 | goto out; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 2966 | if (k->type == KEY_ECDSA_SK_CERT) { |
| 2967 | if ((ret = sshbuf_put_cstring(cert, |
| 2968 | k->sk_application)) != 0) |
| 2969 | goto out; |
| 2970 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2971 | break; |
| 2972 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2973 | case KEY_RSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 2974 | RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL); |
| 2975 | if ((ret = sshbuf_put_bignum2(cert, rsa_e)) != 0 || |
| 2976 | (ret = sshbuf_put_bignum2(cert, rsa_n)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2977 | goto out; |
| 2978 | break; |
| 2979 | #endif /* WITH_OPENSSL */ |
| 2980 | case KEY_ED25519_CERT: |
| 2981 | if ((ret = sshbuf_put_string(cert, |
| 2982 | k->ed25519_pk, ED25519_PK_SZ)) != 0) |
| 2983 | goto out; |
| 2984 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 2985 | #ifdef WITH_XMSS |
| 2986 | case KEY_XMSS_CERT: |
| 2987 | if (k->xmss_name == NULL) { |
| 2988 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 2989 | goto out; |
| 2990 | } |
| 2991 | if ((ret = sshbuf_put_cstring(cert, k->xmss_name)) || |
| 2992 | (ret = sshbuf_put_string(cert, |
| 2993 | k->xmss_pk, sshkey_xmss_pklen(k))) != 0) |
| 2994 | goto out; |
| 2995 | break; |
| 2996 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2997 | default: |
| 2998 | ret = SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 55e5bde | 2015-03-06 01:40:56 +0000 | [diff] [blame] | 2999 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3000 | } |
| 3001 | |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 3002 | if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 || |
| 3003 | (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3004 | (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0) |
| 3005 | goto out; |
| 3006 | |
| 3007 | if ((principals = sshbuf_new()) == NULL) { |
| 3008 | ret = SSH_ERR_ALLOC_FAIL; |
| 3009 | goto out; |
| 3010 | } |
| 3011 | for (i = 0; i < k->cert->nprincipals; i++) { |
| 3012 | if ((ret = sshbuf_put_cstring(principals, |
| 3013 | k->cert->principals[i])) != 0) |
| 3014 | goto out; |
| 3015 | } |
| 3016 | if ((ret = sshbuf_put_stringb(cert, principals)) != 0 || |
| 3017 | (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 || |
| 3018 | (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 || |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 3019 | (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 || |
| 3020 | (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 || |
| 3021 | (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3022 | (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0) |
| 3023 | goto out; |
| 3024 | |
| 3025 | /* Sign the whole mess */ |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3026 | if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert), |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 3027 | sshbuf_len(cert), alg, sk_provider, 0, signer_ctx)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3028 | goto out; |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 3029 | /* Check and update signature_type against what was actually used */ |
djm@openbsd.org | f8df041 | 2019-09-03 08:31:20 +0000 | [diff] [blame] | 3030 | if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0) |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 3031 | goto out; |
| 3032 | if (alg != NULL && strcmp(alg, sigtype) != 0) { |
| 3033 | ret = SSH_ERR_SIGN_ALG_UNSUPPORTED; |
| 3034 | goto out; |
| 3035 | } |
| 3036 | if (k->cert->signature_type == NULL) { |
| 3037 | k->cert->signature_type = sigtype; |
| 3038 | sigtype = NULL; |
| 3039 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3040 | /* Append signature and we are done */ |
| 3041 | if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0) |
| 3042 | goto out; |
| 3043 | ret = 0; |
| 3044 | out: |
| 3045 | if (ret != 0) |
| 3046 | sshbuf_reset(cert); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 3047 | free(sig_blob); |
| 3048 | free(ca_blob); |
djm@openbsd.org | a70fd4a | 2018-09-12 01:31:30 +0000 | [diff] [blame] | 3049 | free(sigtype); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 3050 | sshbuf_free(principals); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3051 | return ret; |
| 3052 | } |
| 3053 | |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3054 | static int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3055 | default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp, |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3056 | const u_char *data, size_t datalen, |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 3057 | const char *alg, const char *sk_provider, u_int compat, void *ctx) |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3058 | { |
| 3059 | if (ctx != NULL) |
| 3060 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 3061 | return sshkey_sign(key, sigp, lenp, data, datalen, alg, |
| 3062 | sk_provider, compat); |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | int |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 3066 | sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg, |
| 3067 | const char *sk_provider) |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3068 | { |
djm@openbsd.org | 9a14c64 | 2019-10-31 21:23:19 +0000 | [diff] [blame] | 3069 | return sshkey_certify_custom(k, ca, alg, sk_provider, |
| 3070 | default_key_sign, NULL); |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3073 | int |
| 3074 | sshkey_cert_check_authority(const struct sshkey *k, |
| 3075 | int want_host, int require_principal, |
| 3076 | const char *name, const char **reason) |
| 3077 | { |
| 3078 | u_int i, principal_matches; |
| 3079 | time_t now = time(NULL); |
| 3080 | |
| 3081 | if (reason != NULL) |
| 3082 | *reason = NULL; |
| 3083 | |
| 3084 | if (want_host) { |
| 3085 | if (k->cert->type != SSH2_CERT_TYPE_HOST) { |
| 3086 | *reason = "Certificate invalid: not a host certificate"; |
| 3087 | return SSH_ERR_KEY_CERT_INVALID; |
| 3088 | } |
| 3089 | } else { |
| 3090 | if (k->cert->type != SSH2_CERT_TYPE_USER) { |
| 3091 | *reason = "Certificate invalid: not a user certificate"; |
| 3092 | return SSH_ERR_KEY_CERT_INVALID; |
| 3093 | } |
| 3094 | } |
| 3095 | if (now < 0) { |
| 3096 | /* yikes - system clock before epoch! */ |
| 3097 | *reason = "Certificate invalid: not yet valid"; |
| 3098 | return SSH_ERR_KEY_CERT_INVALID; |
| 3099 | } |
| 3100 | if ((u_int64_t)now < k->cert->valid_after) { |
| 3101 | *reason = "Certificate invalid: not yet valid"; |
| 3102 | return SSH_ERR_KEY_CERT_INVALID; |
| 3103 | } |
| 3104 | if ((u_int64_t)now >= k->cert->valid_before) { |
| 3105 | *reason = "Certificate invalid: expired"; |
| 3106 | return SSH_ERR_KEY_CERT_INVALID; |
| 3107 | } |
| 3108 | if (k->cert->nprincipals == 0) { |
| 3109 | if (require_principal) { |
| 3110 | *reason = "Certificate lacks principal list"; |
| 3111 | return SSH_ERR_KEY_CERT_INVALID; |
| 3112 | } |
| 3113 | } else if (name != NULL) { |
| 3114 | principal_matches = 0; |
| 3115 | for (i = 0; i < k->cert->nprincipals; i++) { |
| 3116 | if (strcmp(name, k->cert->principals[i]) == 0) { |
| 3117 | principal_matches = 1; |
| 3118 | break; |
| 3119 | } |
| 3120 | } |
| 3121 | if (!principal_matches) { |
| 3122 | *reason = "Certificate invalid: name is not a listed " |
| 3123 | "principal"; |
| 3124 | return SSH_ERR_KEY_CERT_INVALID; |
| 3125 | } |
| 3126 | } |
| 3127 | return 0; |
| 3128 | } |
| 3129 | |
djm@openbsd.org | 499cf36 | 2015-11-19 01:08:55 +0000 | [diff] [blame] | 3130 | size_t |
| 3131 | sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l) |
| 3132 | { |
| 3133 | char from[32], to[32], ret[64]; |
| 3134 | time_t tt; |
| 3135 | struct tm *tm; |
| 3136 | |
| 3137 | *from = *to = '\0'; |
| 3138 | if (cert->valid_after == 0 && |
| 3139 | cert->valid_before == 0xffffffffffffffffULL) |
| 3140 | return strlcpy(s, "forever", l); |
| 3141 | |
| 3142 | if (cert->valid_after != 0) { |
| 3143 | /* XXX revisit INT_MAX in 2038 :) */ |
| 3144 | tt = cert->valid_after > INT_MAX ? |
| 3145 | INT_MAX : cert->valid_after; |
| 3146 | tm = localtime(&tt); |
| 3147 | strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm); |
| 3148 | } |
| 3149 | if (cert->valid_before != 0xffffffffffffffffULL) { |
| 3150 | /* XXX revisit INT_MAX in 2038 :) */ |
| 3151 | tt = cert->valid_before > INT_MAX ? |
| 3152 | INT_MAX : cert->valid_before; |
| 3153 | tm = localtime(&tt); |
| 3154 | strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm); |
| 3155 | } |
| 3156 | |
| 3157 | if (cert->valid_after == 0) |
| 3158 | snprintf(ret, sizeof(ret), "before %s", to); |
| 3159 | else if (cert->valid_before == 0xffffffffffffffffULL) |
| 3160 | snprintf(ret, sizeof(ret), "after %s", from); |
| 3161 | else |
| 3162 | snprintf(ret, sizeof(ret), "from %s to %s", from, to); |
| 3163 | |
| 3164 | return strlcpy(s, ret, l); |
| 3165 | } |
| 3166 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3167 | int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3168 | sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3169 | enum sshkey_serialize_rep opts) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3170 | { |
| 3171 | int r = SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3172 | int was_shielded = sshkey_is_shielded(key); |
| 3173 | struct sshbuf *b = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3174 | #ifdef WITH_OPENSSL |
| 3175 | const BIGNUM *rsa_n, *rsa_e, *rsa_d, *rsa_iqmp, *rsa_p, *rsa_q; |
| 3176 | const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key, *dsa_priv_key; |
| 3177 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3178 | |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3179 | if ((r = sshkey_unshield_private(key)) != 0) |
| 3180 | return r; |
| 3181 | if ((b = sshbuf_new()) == NULL) |
| 3182 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3183 | if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0) |
| 3184 | goto out; |
| 3185 | switch (key->type) { |
| 3186 | #ifdef WITH_OPENSSL |
| 3187 | case KEY_RSA: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3188 | RSA_get0_key(key->rsa, &rsa_n, &rsa_e, &rsa_d); |
| 3189 | RSA_get0_factors(key->rsa, &rsa_p, &rsa_q); |
| 3190 | RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp); |
| 3191 | if ((r = sshbuf_put_bignum2(b, rsa_n)) != 0 || |
| 3192 | (r = sshbuf_put_bignum2(b, rsa_e)) != 0 || |
| 3193 | (r = sshbuf_put_bignum2(b, rsa_d)) != 0 || |
| 3194 | (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 || |
| 3195 | (r = sshbuf_put_bignum2(b, rsa_p)) != 0 || |
| 3196 | (r = sshbuf_put_bignum2(b, rsa_q)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3197 | goto out; |
| 3198 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3199 | case KEY_RSA_CERT: |
| 3200 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3201 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3202 | goto out; |
| 3203 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3204 | RSA_get0_key(key->rsa, NULL, NULL, &rsa_d); |
| 3205 | RSA_get0_factors(key->rsa, &rsa_p, &rsa_q); |
| 3206 | RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3207 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3208 | (r = sshbuf_put_bignum2(b, rsa_d)) != 0 || |
| 3209 | (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 || |
| 3210 | (r = sshbuf_put_bignum2(b, rsa_p)) != 0 || |
| 3211 | (r = sshbuf_put_bignum2(b, rsa_q)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3212 | goto out; |
| 3213 | break; |
| 3214 | case KEY_DSA: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3215 | DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g); |
| 3216 | DSA_get0_key(key->dsa, &dsa_pub_key, &dsa_priv_key); |
| 3217 | if ((r = sshbuf_put_bignum2(b, dsa_p)) != 0 || |
| 3218 | (r = sshbuf_put_bignum2(b, dsa_q)) != 0 || |
| 3219 | (r = sshbuf_put_bignum2(b, dsa_g)) != 0 || |
| 3220 | (r = sshbuf_put_bignum2(b, dsa_pub_key)) != 0 || |
| 3221 | (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3222 | goto out; |
| 3223 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3224 | case KEY_DSA_CERT: |
| 3225 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3226 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3227 | goto out; |
| 3228 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3229 | DSA_get0_key(key->dsa, NULL, &dsa_priv_key); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3230 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3231 | (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3232 | goto out; |
| 3233 | break; |
| 3234 | # ifdef OPENSSL_HAS_ECC |
| 3235 | case KEY_ECDSA: |
| 3236 | if ((r = sshbuf_put_cstring(b, |
| 3237 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
| 3238 | (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 || |
| 3239 | (r = sshbuf_put_bignum2(b, |
| 3240 | EC_KEY_get0_private_key(key->ecdsa))) != 0) |
| 3241 | goto out; |
| 3242 | break; |
| 3243 | case KEY_ECDSA_CERT: |
| 3244 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3245 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3246 | goto out; |
| 3247 | } |
| 3248 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 3249 | (r = sshbuf_put_bignum2(b, |
| 3250 | EC_KEY_get0_private_key(key->ecdsa))) != 0) |
| 3251 | goto out; |
| 3252 | break; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 3253 | case KEY_ECDSA_SK: |
| 3254 | if ((r = sshbuf_put_cstring(b, |
| 3255 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
| 3256 | (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 || |
| 3257 | (r = sshbuf_put_cstring(b, key->sk_application)) != 0 || |
| 3258 | (r = sshbuf_put_u8(b, key->sk_flags)) != 0 || |
| 3259 | (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 || |
| 3260 | (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0) |
| 3261 | goto out; |
| 3262 | break; |
| 3263 | case KEY_ECDSA_SK_CERT: |
| 3264 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3265 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3266 | goto out; |
| 3267 | } |
| 3268 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 3269 | (r = sshbuf_put_cstring(b, key->sk_application)) != 0 || |
| 3270 | (r = sshbuf_put_u8(b, key->sk_flags)) != 0 || |
| 3271 | (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 || |
| 3272 | (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0) |
| 3273 | goto out; |
| 3274 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3275 | # endif /* OPENSSL_HAS_ECC */ |
| 3276 | #endif /* WITH_OPENSSL */ |
| 3277 | case KEY_ED25519: |
| 3278 | if ((r = sshbuf_put_string(b, key->ed25519_pk, |
| 3279 | ED25519_PK_SZ)) != 0 || |
| 3280 | (r = sshbuf_put_string(b, key->ed25519_sk, |
| 3281 | ED25519_SK_SZ)) != 0) |
| 3282 | goto out; |
| 3283 | break; |
| 3284 | case KEY_ED25519_CERT: |
| 3285 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3286 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3287 | goto out; |
| 3288 | } |
| 3289 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 3290 | (r = sshbuf_put_string(b, key->ed25519_pk, |
| 3291 | ED25519_PK_SZ)) != 0 || |
| 3292 | (r = sshbuf_put_string(b, key->ed25519_sk, |
| 3293 | ED25519_SK_SZ)) != 0) |
| 3294 | goto out; |
| 3295 | break; |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 3296 | case KEY_ED25519_SK: |
| 3297 | if ((r = sshbuf_put_string(b, key->ed25519_pk, |
| 3298 | ED25519_PK_SZ)) != 0 || |
| 3299 | (r = sshbuf_put_cstring(b, key->sk_application)) != 0 || |
| 3300 | (r = sshbuf_put_u8(b, key->sk_flags)) != 0 || |
| 3301 | (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 || |
| 3302 | (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0) |
| 3303 | goto out; |
| 3304 | break; |
| 3305 | case KEY_ED25519_SK_CERT: |
| 3306 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 3307 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3308 | goto out; |
| 3309 | } |
| 3310 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 3311 | (r = sshbuf_put_string(b, key->ed25519_pk, |
| 3312 | ED25519_PK_SZ)) != 0 || |
| 3313 | (r = sshbuf_put_cstring(b, key->sk_application)) != 0 || |
| 3314 | (r = sshbuf_put_u8(b, key->sk_flags)) != 0 || |
| 3315 | (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 || |
| 3316 | (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0) |
| 3317 | goto out; |
| 3318 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3319 | #ifdef WITH_XMSS |
| 3320 | case KEY_XMSS: |
| 3321 | if (key->xmss_name == NULL) { |
| 3322 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3323 | goto out; |
| 3324 | } |
| 3325 | if ((r = sshbuf_put_cstring(b, key->xmss_name)) != 0 || |
| 3326 | (r = sshbuf_put_string(b, key->xmss_pk, |
| 3327 | sshkey_xmss_pklen(key))) != 0 || |
| 3328 | (r = sshbuf_put_string(b, key->xmss_sk, |
| 3329 | sshkey_xmss_sklen(key))) != 0 || |
| 3330 | (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0) |
| 3331 | goto out; |
| 3332 | break; |
| 3333 | case KEY_XMSS_CERT: |
| 3334 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0 || |
| 3335 | key->xmss_name == NULL) { |
| 3336 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3337 | goto out; |
| 3338 | } |
| 3339 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 3340 | (r = sshbuf_put_cstring(b, key->xmss_name)) != 0 || |
| 3341 | (r = sshbuf_put_string(b, key->xmss_pk, |
| 3342 | sshkey_xmss_pklen(key))) != 0 || |
| 3343 | (r = sshbuf_put_string(b, key->xmss_sk, |
| 3344 | sshkey_xmss_sklen(key))) != 0 || |
| 3345 | (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0) |
| 3346 | goto out; |
| 3347 | break; |
| 3348 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3349 | default: |
| 3350 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3351 | goto out; |
| 3352 | } |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3353 | /* |
| 3354 | * success (but we still need to append the output to buf after |
| 3355 | * possibly re-shielding the private key) |
| 3356 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3357 | r = 0; |
| 3358 | out: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3359 | if (was_shielded) |
| 3360 | r = sshkey_shield_private(key); |
| 3361 | if (r == 0) |
| 3362 | r = sshbuf_putb(buf, b); |
| 3363 | sshbuf_free(b); |
| 3364 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3365 | return r; |
| 3366 | } |
| 3367 | |
| 3368 | int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3369 | sshkey_private_serialize(struct sshkey *key, struct sshbuf *b) |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3370 | { |
| 3371 | return sshkey_private_serialize_opt(key, b, |
| 3372 | SSHKEY_SERIALIZE_DEFAULT); |
| 3373 | } |
| 3374 | |
| 3375 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3376 | sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) |
| 3377 | { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3378 | char *tname = NULL, *curve = NULL, *xmss_name = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3379 | struct sshkey *k = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 3380 | size_t pklen = 0, sklen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3381 | int type, r = SSH_ERR_INTERNAL_ERROR; |
| 3382 | u_char *ed25519_pk = NULL, *ed25519_sk = NULL; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3383 | u_char *xmss_pk = NULL, *xmss_sk = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3384 | #ifdef WITH_OPENSSL |
| 3385 | BIGNUM *exponent = NULL; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3386 | BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL; |
| 3387 | BIGNUM *rsa_iqmp = NULL, *rsa_p = NULL, *rsa_q = NULL; |
| 3388 | BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL; |
| 3389 | BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3390 | #endif /* WITH_OPENSSL */ |
| 3391 | |
| 3392 | if (kp != NULL) |
| 3393 | *kp = NULL; |
| 3394 | if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0) |
| 3395 | goto out; |
| 3396 | type = sshkey_type_from_name(tname); |
| 3397 | switch (type) { |
| 3398 | #ifdef WITH_OPENSSL |
| 3399 | case KEY_DSA: |
djm@openbsd.org | 6da046f | 2018-09-14 04:17:44 +0000 | [diff] [blame] | 3400 | if ((k = sshkey_new(type)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3401 | r = SSH_ERR_ALLOC_FAIL; |
| 3402 | goto out; |
| 3403 | } |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3404 | if ((r = sshbuf_get_bignum2(buf, &dsa_p)) != 0 || |
| 3405 | (r = sshbuf_get_bignum2(buf, &dsa_q)) != 0 || |
| 3406 | (r = sshbuf_get_bignum2(buf, &dsa_g)) != 0 || |
| 3407 | (r = sshbuf_get_bignum2(buf, &dsa_pub_key)) != 0 || |
| 3408 | (r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0) |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3409 | goto out; |
| 3410 | if (!DSA_set0_pqg(k->dsa, dsa_p, dsa_q, dsa_g)) { |
| 3411 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3412 | goto out; |
| 3413 | } |
| 3414 | dsa_p = dsa_q = dsa_g = NULL; /* transferred */ |
| 3415 | if (!DSA_set0_key(k->dsa, dsa_pub_key, dsa_priv_key)) { |
| 3416 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3417 | goto out; |
| 3418 | } |
| 3419 | dsa_pub_key = dsa_priv_key = NULL; /* transferred */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3420 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3421 | case KEY_DSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3422 | if ((r = sshkey_froms(buf, &k)) != 0 || |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3423 | (r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0) |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3424 | goto out; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3425 | if (k->type != type) { |
| 3426 | r = SSH_ERR_INVALID_FORMAT; |
| 3427 | goto out; |
| 3428 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3429 | if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) { |
| 3430 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3431 | goto out; |
| 3432 | } |
| 3433 | dsa_priv_key = NULL; /* transferred */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3434 | break; |
| 3435 | # ifdef OPENSSL_HAS_ECC |
| 3436 | case KEY_ECDSA: |
djm@openbsd.org | 6da046f | 2018-09-14 04:17:44 +0000 | [diff] [blame] | 3437 | if ((k = sshkey_new(type)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3438 | r = SSH_ERR_ALLOC_FAIL; |
| 3439 | goto out; |
| 3440 | } |
| 3441 | if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) { |
| 3442 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3443 | goto out; |
| 3444 | } |
| 3445 | if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0) |
| 3446 | goto out; |
| 3447 | if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
| 3448 | r = SSH_ERR_EC_CURVE_MISMATCH; |
| 3449 | goto out; |
| 3450 | } |
| 3451 | k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid); |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3452 | if (k->ecdsa == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3453 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3454 | goto out; |
| 3455 | } |
| 3456 | if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 || |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3457 | (r = sshbuf_get_bignum2(buf, &exponent))) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3458 | goto out; |
| 3459 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { |
| 3460 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3461 | goto out; |
| 3462 | } |
| 3463 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 3464 | EC_KEY_get0_public_key(k->ecdsa))) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3465 | (r = sshkey_ec_validate_private(k->ecdsa)) != 0) |
| 3466 | goto out; |
| 3467 | break; |
| 3468 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 3469 | if ((r = sshkey_froms(buf, &k)) != 0 || |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3470 | (r = sshbuf_get_bignum2(buf, &exponent)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3471 | goto out; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3472 | if (k->type != type || |
| 3473 | k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) { |
| 3474 | r = SSH_ERR_INVALID_FORMAT; |
| 3475 | goto out; |
| 3476 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3477 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { |
| 3478 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3479 | goto out; |
| 3480 | } |
| 3481 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 3482 | EC_KEY_get0_public_key(k->ecdsa))) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3483 | (r = sshkey_ec_validate_private(k->ecdsa)) != 0) |
| 3484 | goto out; |
| 3485 | break; |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 3486 | case KEY_ECDSA_SK: |
| 3487 | if ((k = sshkey_new(type)) == NULL) { |
| 3488 | r = SSH_ERR_ALLOC_FAIL; |
| 3489 | goto out; |
| 3490 | } |
| 3491 | if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) { |
| 3492 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0) |
| 3496 | goto out; |
| 3497 | if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
| 3498 | r = SSH_ERR_EC_CURVE_MISMATCH; |
| 3499 | goto out; |
| 3500 | } |
| 3501 | if ((k->sk_key_handle = sshbuf_new()) == NULL || |
| 3502 | (k->sk_reserved = sshbuf_new()) == NULL) { |
| 3503 | r = SSH_ERR_ALLOC_FAIL; |
| 3504 | goto out; |
| 3505 | } |
| 3506 | k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid); |
| 3507 | if (k->ecdsa == NULL) { |
| 3508 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3509 | goto out; |
| 3510 | } |
| 3511 | if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 || |
| 3512 | (r = sshbuf_get_cstring(buf, &k->sk_application, |
| 3513 | NULL)) != 0 || |
| 3514 | (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 || |
| 3515 | (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 || |
| 3516 | (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0) |
| 3517 | goto out; |
| 3518 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
| 3519 | EC_KEY_get0_public_key(k->ecdsa))) != 0) |
| 3520 | goto out; |
| 3521 | break; |
| 3522 | case KEY_ECDSA_SK_CERT: |
| 3523 | if ((r = sshkey_froms(buf, &k)) != 0) |
| 3524 | goto out; |
| 3525 | if ((k->sk_key_handle = sshbuf_new()) == NULL || |
| 3526 | (k->sk_reserved = sshbuf_new()) == NULL) { |
| 3527 | r = SSH_ERR_ALLOC_FAIL; |
| 3528 | goto out; |
| 3529 | } |
| 3530 | if ((r = sshbuf_get_cstring(buf, &k->sk_application, |
| 3531 | NULL)) != 0 || |
| 3532 | (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 || |
| 3533 | (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 || |
| 3534 | (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0) |
| 3535 | goto out; |
| 3536 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
| 3537 | EC_KEY_get0_public_key(k->ecdsa))) != 0) |
| 3538 | goto out; |
| 3539 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3540 | # endif /* OPENSSL_HAS_ECC */ |
| 3541 | case KEY_RSA: |
djm@openbsd.org | 6da046f | 2018-09-14 04:17:44 +0000 | [diff] [blame] | 3542 | if ((k = sshkey_new(type)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3543 | r = SSH_ERR_ALLOC_FAIL; |
| 3544 | goto out; |
| 3545 | } |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3546 | if ((r = sshbuf_get_bignum2(buf, &rsa_n)) != 0 || |
| 3547 | (r = sshbuf_get_bignum2(buf, &rsa_e)) != 0 || |
| 3548 | (r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 || |
| 3549 | (r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 || |
| 3550 | (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 || |
| 3551 | (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0) |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3552 | goto out; |
| 3553 | if (!RSA_set0_key(k->rsa, rsa_n, rsa_e, rsa_d)) { |
| 3554 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3555 | goto out; |
| 3556 | } |
| 3557 | rsa_n = rsa_e = rsa_d = NULL; /* transferred */ |
| 3558 | if (!RSA_set0_factors(k->rsa, rsa_p, rsa_q)) { |
| 3559 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3560 | goto out; |
| 3561 | } |
| 3562 | rsa_p = rsa_q = NULL; /* transferred */ |
| 3563 | if ((r = check_rsa_length(k->rsa)) != 0) |
| 3564 | goto out; |
| 3565 | if ((r = ssh_rsa_complete_crt_parameters(k, rsa_iqmp)) != 0) |
| 3566 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3567 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3568 | case KEY_RSA_CERT: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3569 | if ((r = sshkey_froms(buf, &k)) != 0 || |
djm@openbsd.org | 7be8572 | 2019-01-21 09:54:11 +0000 | [diff] [blame] | 3570 | (r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 || |
| 3571 | (r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 || |
| 3572 | (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 || |
| 3573 | (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0) |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3574 | goto out; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3575 | if (k->type != type) { |
| 3576 | r = SSH_ERR_INVALID_FORMAT; |
| 3577 | goto out; |
| 3578 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3579 | if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) { |
| 3580 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3581 | goto out; |
| 3582 | } |
| 3583 | rsa_d = NULL; /* transferred */ |
| 3584 | if (!RSA_set0_factors(k->rsa, rsa_p, rsa_q)) { |
| 3585 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3586 | goto out; |
| 3587 | } |
| 3588 | rsa_p = rsa_q = NULL; /* transferred */ |
| 3589 | if ((r = check_rsa_length(k->rsa)) != 0) |
| 3590 | goto out; |
| 3591 | if ((r = ssh_rsa_complete_crt_parameters(k, rsa_iqmp)) != 0) |
| 3592 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3593 | break; |
| 3594 | #endif /* WITH_OPENSSL */ |
| 3595 | case KEY_ED25519: |
djm@openbsd.org | 6da046f | 2018-09-14 04:17:44 +0000 | [diff] [blame] | 3596 | if ((k = sshkey_new(type)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3597 | r = SSH_ERR_ALLOC_FAIL; |
| 3598 | goto out; |
| 3599 | } |
| 3600 | if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || |
| 3601 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) |
| 3602 | goto out; |
| 3603 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { |
| 3604 | r = SSH_ERR_INVALID_FORMAT; |
| 3605 | goto out; |
| 3606 | } |
| 3607 | k->ed25519_pk = ed25519_pk; |
| 3608 | k->ed25519_sk = ed25519_sk; |
| 3609 | ed25519_pk = ed25519_sk = NULL; |
| 3610 | break; |
| 3611 | case KEY_ED25519_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 3612 | if ((r = sshkey_froms(buf, &k)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3613 | (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || |
| 3614 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) |
| 3615 | goto out; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3616 | if (k->type != type) { |
| 3617 | r = SSH_ERR_INVALID_FORMAT; |
| 3618 | goto out; |
| 3619 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3620 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { |
| 3621 | r = SSH_ERR_INVALID_FORMAT; |
| 3622 | goto out; |
| 3623 | } |
| 3624 | k->ed25519_pk = ed25519_pk; |
| 3625 | k->ed25519_sk = ed25519_sk; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3626 | ed25519_pk = ed25519_sk = NULL; /* transferred */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3627 | break; |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 3628 | case KEY_ED25519_SK: |
| 3629 | if ((k = sshkey_new(type)) == NULL) { |
| 3630 | r = SSH_ERR_ALLOC_FAIL; |
| 3631 | goto out; |
| 3632 | } |
| 3633 | if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0) |
| 3634 | goto out; |
| 3635 | if (pklen != ED25519_PK_SZ) { |
| 3636 | r = SSH_ERR_INVALID_FORMAT; |
| 3637 | goto out; |
| 3638 | } |
| 3639 | if ((k->sk_key_handle = sshbuf_new()) == NULL || |
| 3640 | (k->sk_reserved = sshbuf_new()) == NULL) { |
| 3641 | r = SSH_ERR_ALLOC_FAIL; |
| 3642 | goto out; |
| 3643 | } |
| 3644 | if ((r = sshbuf_get_cstring(buf, &k->sk_application, |
| 3645 | NULL)) != 0 || |
| 3646 | (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 || |
| 3647 | (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 || |
| 3648 | (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0) |
| 3649 | goto out; |
| 3650 | k->ed25519_pk = ed25519_pk; |
| 3651 | ed25519_pk = NULL; |
| 3652 | break; |
| 3653 | case KEY_ED25519_SK_CERT: |
| 3654 | if ((r = sshkey_froms(buf, &k)) != 0 || |
| 3655 | (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0) |
| 3656 | goto out; |
| 3657 | if (k->type != type) { |
| 3658 | r = SSH_ERR_INVALID_FORMAT; |
| 3659 | goto out; |
| 3660 | } |
| 3661 | if (pklen != ED25519_PK_SZ) { |
| 3662 | r = SSH_ERR_INVALID_FORMAT; |
| 3663 | goto out; |
| 3664 | } |
| 3665 | if ((k->sk_key_handle = sshbuf_new()) == NULL || |
| 3666 | (k->sk_reserved = sshbuf_new()) == NULL) { |
| 3667 | r = SSH_ERR_ALLOC_FAIL; |
| 3668 | goto out; |
| 3669 | } |
| 3670 | if ((r = sshbuf_get_cstring(buf, &k->sk_application, |
| 3671 | NULL)) != 0 || |
| 3672 | (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 || |
| 3673 | (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 || |
| 3674 | (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0) |
| 3675 | goto out; |
| 3676 | k->ed25519_pk = ed25519_pk; |
| 3677 | ed25519_pk = NULL; /* transferred */ |
| 3678 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3679 | #ifdef WITH_XMSS |
| 3680 | case KEY_XMSS: |
djm@openbsd.org | 6da046f | 2018-09-14 04:17:44 +0000 | [diff] [blame] | 3681 | if ((k = sshkey_new(type)) == NULL) { |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3682 | r = SSH_ERR_ALLOC_FAIL; |
| 3683 | goto out; |
| 3684 | } |
| 3685 | if ((r = sshbuf_get_cstring(buf, &xmss_name, NULL)) != 0 || |
| 3686 | (r = sshkey_xmss_init(k, xmss_name)) != 0 || |
| 3687 | (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 || |
| 3688 | (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0) |
| 3689 | goto out; |
| 3690 | if (pklen != sshkey_xmss_pklen(k) || |
| 3691 | sklen != sshkey_xmss_sklen(k)) { |
| 3692 | r = SSH_ERR_INVALID_FORMAT; |
| 3693 | goto out; |
| 3694 | } |
| 3695 | k->xmss_pk = xmss_pk; |
| 3696 | k->xmss_sk = xmss_sk; |
| 3697 | xmss_pk = xmss_sk = NULL; |
| 3698 | /* optional internal state */ |
| 3699 | if ((r = sshkey_xmss_deserialize_state_opt(k, buf)) != 0) |
| 3700 | goto out; |
| 3701 | break; |
| 3702 | case KEY_XMSS_CERT: |
| 3703 | if ((r = sshkey_froms(buf, &k)) != 0 || |
markus@openbsd.org | 27979da | 2018-03-22 07:05:48 +0000 | [diff] [blame] | 3704 | (r = sshbuf_get_cstring(buf, &xmss_name, NULL)) != 0 || |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3705 | (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 || |
| 3706 | (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0) |
| 3707 | goto out; |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 3708 | if (k->type != type || strcmp(xmss_name, k->xmss_name) != 0) { |
markus@openbsd.org | 27979da | 2018-03-22 07:05:48 +0000 | [diff] [blame] | 3709 | r = SSH_ERR_INVALID_FORMAT; |
| 3710 | goto out; |
| 3711 | } |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3712 | if (pklen != sshkey_xmss_pklen(k) || |
| 3713 | sklen != sshkey_xmss_sklen(k)) { |
| 3714 | r = SSH_ERR_INVALID_FORMAT; |
| 3715 | goto out; |
| 3716 | } |
| 3717 | k->xmss_pk = xmss_pk; |
| 3718 | k->xmss_sk = xmss_sk; |
| 3719 | xmss_pk = xmss_sk = NULL; |
| 3720 | /* optional internal state */ |
| 3721 | if ((r = sshkey_xmss_deserialize_state_opt(k, buf)) != 0) |
| 3722 | goto out; |
| 3723 | break; |
| 3724 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3725 | default: |
| 3726 | r = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 3727 | goto out; |
| 3728 | } |
| 3729 | #ifdef WITH_OPENSSL |
| 3730 | /* enable blinding */ |
| 3731 | switch (k->type) { |
| 3732 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3733 | case KEY_RSA_CERT: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3734 | if (RSA_blinding_on(k->rsa, NULL) != 1) { |
| 3735 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3736 | goto out; |
| 3737 | } |
| 3738 | break; |
| 3739 | } |
| 3740 | #endif /* WITH_OPENSSL */ |
| 3741 | /* success */ |
| 3742 | r = 0; |
| 3743 | if (kp != NULL) { |
| 3744 | *kp = k; |
| 3745 | k = NULL; |
| 3746 | } |
| 3747 | out: |
| 3748 | free(tname); |
| 3749 | free(curve); |
| 3750 | #ifdef WITH_OPENSSL |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 3751 | BN_clear_free(exponent); |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 3752 | BN_clear_free(dsa_p); |
| 3753 | BN_clear_free(dsa_q); |
| 3754 | BN_clear_free(dsa_g); |
| 3755 | BN_clear_free(dsa_pub_key); |
| 3756 | BN_clear_free(dsa_priv_key); |
| 3757 | BN_clear_free(rsa_n); |
| 3758 | BN_clear_free(rsa_e); |
| 3759 | BN_clear_free(rsa_d); |
| 3760 | BN_clear_free(rsa_p); |
| 3761 | BN_clear_free(rsa_q); |
| 3762 | BN_clear_free(rsa_iqmp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3763 | #endif /* WITH_OPENSSL */ |
| 3764 | sshkey_free(k); |
jsing@openbsd.org | 4270efa | 2018-02-14 16:03:32 +0000 | [diff] [blame] | 3765 | freezero(ed25519_pk, pklen); |
| 3766 | freezero(ed25519_sk, sklen); |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 3767 | free(xmss_name); |
| 3768 | freezero(xmss_pk, pklen); |
| 3769 | freezero(xmss_sk, sklen); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3770 | return r; |
| 3771 | } |
| 3772 | |
| 3773 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 3774 | int |
| 3775 | sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public) |
| 3776 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3777 | EC_POINT *nq = NULL; |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3778 | BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3779 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 3780 | |
djm@openbsd.org | a571dbc | 2016-10-04 21:34:40 +0000 | [diff] [blame] | 3781 | /* |
| 3782 | * NB. This assumes OpenSSL has already verified that the public |
| 3783 | * point lies on the curve. This is done by EC_POINT_oct2point() |
| 3784 | * implicitly calling EC_POINT_is_on_curve(). If this code is ever |
| 3785 | * reachable with public points not unmarshalled using |
| 3786 | * EC_POINT_oct2point then the caller will need to explicitly check. |
| 3787 | */ |
| 3788 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3789 | /* |
| 3790 | * We shouldn't ever hit this case because bignum_get_ecpoint() |
| 3791 | * refuses to load GF2m points. |
| 3792 | */ |
| 3793 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 3794 | NID_X9_62_prime_field) |
| 3795 | goto out; |
| 3796 | |
| 3797 | /* Q != infinity */ |
| 3798 | if (EC_POINT_is_at_infinity(group, public)) |
| 3799 | goto out; |
| 3800 | |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3801 | if ((x = BN_new()) == NULL || |
| 3802 | (y = BN_new()) == NULL || |
| 3803 | (order = BN_new()) == NULL || |
| 3804 | (tmp = BN_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3805 | ret = SSH_ERR_ALLOC_FAIL; |
| 3806 | goto out; |
| 3807 | } |
| 3808 | |
| 3809 | /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */ |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3810 | if (EC_GROUP_get_order(group, order, NULL) != 1 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3811 | EC_POINT_get_affine_coordinates_GFp(group, public, |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3812 | x, y, NULL) != 1) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3813 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 3814 | goto out; |
| 3815 | } |
| 3816 | if (BN_num_bits(x) <= BN_num_bits(order) / 2 || |
| 3817 | BN_num_bits(y) <= BN_num_bits(order) / 2) |
| 3818 | goto out; |
| 3819 | |
| 3820 | /* nQ == infinity (n == order of subgroup) */ |
| 3821 | if ((nq = EC_POINT_new(group)) == NULL) { |
| 3822 | ret = SSH_ERR_ALLOC_FAIL; |
| 3823 | goto out; |
| 3824 | } |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3825 | if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3826 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 3827 | goto out; |
| 3828 | } |
| 3829 | if (EC_POINT_is_at_infinity(group, nq) != 1) |
| 3830 | goto out; |
| 3831 | |
| 3832 | /* x < order - 1, y < order - 1 */ |
| 3833 | if (!BN_sub(tmp, order, BN_value_one())) { |
| 3834 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 3835 | goto out; |
| 3836 | } |
| 3837 | if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0) |
| 3838 | goto out; |
| 3839 | ret = 0; |
| 3840 | out: |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3841 | BN_clear_free(x); |
| 3842 | BN_clear_free(y); |
| 3843 | BN_clear_free(order); |
| 3844 | BN_clear_free(tmp); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 3845 | EC_POINT_free(nq); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3846 | return ret; |
| 3847 | } |
| 3848 | |
| 3849 | int |
| 3850 | sshkey_ec_validate_private(const EC_KEY *key) |
| 3851 | { |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3852 | BIGNUM *order = NULL, *tmp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3853 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 3854 | |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3855 | if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3856 | ret = SSH_ERR_ALLOC_FAIL; |
| 3857 | goto out; |
| 3858 | } |
| 3859 | |
| 3860 | /* log2(private) > log2(order)/2 */ |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3861 | if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3862 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 3863 | goto out; |
| 3864 | } |
| 3865 | if (BN_num_bits(EC_KEY_get0_private_key(key)) <= |
| 3866 | BN_num_bits(order) / 2) |
| 3867 | goto out; |
| 3868 | |
| 3869 | /* private < order - 1 */ |
| 3870 | if (!BN_sub(tmp, order, BN_value_one())) { |
| 3871 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 3872 | goto out; |
| 3873 | } |
| 3874 | if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) |
| 3875 | goto out; |
| 3876 | ret = 0; |
| 3877 | out: |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3878 | BN_clear_free(order); |
| 3879 | BN_clear_free(tmp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3880 | return ret; |
| 3881 | } |
| 3882 | |
| 3883 | void |
| 3884 | sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point) |
| 3885 | { |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3886 | BIGNUM *x = NULL, *y = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3887 | |
| 3888 | if (point == NULL) { |
| 3889 | fputs("point=(NULL)\n", stderr); |
| 3890 | return; |
| 3891 | } |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3892 | if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) { |
| 3893 | fprintf(stderr, "%s: BN_new failed\n", __func__); |
| 3894 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3895 | } |
| 3896 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 3897 | NID_X9_62_prime_field) { |
| 3898 | fprintf(stderr, "%s: group is not a prime field\n", __func__); |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3899 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3900 | } |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3901 | if (EC_POINT_get_affine_coordinates_GFp(group, point, |
| 3902 | x, y, NULL) != 1) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3903 | fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n", |
| 3904 | __func__); |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3905 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3906 | } |
| 3907 | fputs("x=", stderr); |
| 3908 | BN_print_fp(stderr, x); |
| 3909 | fputs("\ny=", stderr); |
| 3910 | BN_print_fp(stderr, y); |
| 3911 | fputs("\n", stderr); |
djm@openbsd.org | fd1a964 | 2019-11-15 06:00:20 +0000 | [diff] [blame] | 3912 | out: |
| 3913 | BN_clear_free(x); |
| 3914 | BN_clear_free(y); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | void |
| 3918 | sshkey_dump_ec_key(const EC_KEY *key) |
| 3919 | { |
| 3920 | const BIGNUM *exponent; |
| 3921 | |
| 3922 | sshkey_dump_ec_point(EC_KEY_get0_group(key), |
| 3923 | EC_KEY_get0_public_key(key)); |
| 3924 | fputs("exponent=", stderr); |
| 3925 | if ((exponent = EC_KEY_get0_private_key(key)) == NULL) |
| 3926 | fputs("(NULL)", stderr); |
| 3927 | else |
| 3928 | BN_print_fp(stderr, EC_KEY_get0_private_key(key)); |
| 3929 | fputs("\n", stderr); |
| 3930 | } |
| 3931 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
| 3932 | |
| 3933 | static int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 3934 | sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3935 | const char *passphrase, const char *comment, const char *ciphername, |
| 3936 | int rounds) |
| 3937 | { |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 3938 | u_char *cp, *key = NULL, *pubkeyblob = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3939 | u_char salt[SALT_LEN]; |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 3940 | char *b64 = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3941 | size_t i, pubkeylen, keylen, ivlen, blocksize, authlen; |
| 3942 | u_int check; |
| 3943 | int r = SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3944 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3945 | const struct sshcipher *cipher; |
| 3946 | const char *kdfname = KDFNAME; |
| 3947 | struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL; |
| 3948 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3949 | if (rounds <= 0) |
| 3950 | rounds = DEFAULT_ROUNDS; |
| 3951 | if (passphrase == NULL || !strlen(passphrase)) { |
| 3952 | ciphername = "none"; |
| 3953 | kdfname = "none"; |
| 3954 | } else if (ciphername == NULL) |
| 3955 | ciphername = DEFAULT_CIPHERNAME; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3956 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 3957 | r = SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3958 | goto out; |
| 3959 | } |
| 3960 | |
| 3961 | if ((kdf = sshbuf_new()) == NULL || |
| 3962 | (encoded = sshbuf_new()) == NULL || |
| 3963 | (encrypted = sshbuf_new()) == NULL) { |
| 3964 | r = SSH_ERR_ALLOC_FAIL; |
| 3965 | goto out; |
| 3966 | } |
| 3967 | blocksize = cipher_blocksize(cipher); |
| 3968 | keylen = cipher_keylen(cipher); |
| 3969 | ivlen = cipher_ivlen(cipher); |
| 3970 | authlen = cipher_authlen(cipher); |
| 3971 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
| 3972 | r = SSH_ERR_ALLOC_FAIL; |
| 3973 | goto out; |
| 3974 | } |
| 3975 | if (strcmp(kdfname, "bcrypt") == 0) { |
| 3976 | arc4random_buf(salt, SALT_LEN); |
| 3977 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), |
| 3978 | salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) { |
| 3979 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3980 | goto out; |
| 3981 | } |
| 3982 | if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 || |
| 3983 | (r = sshbuf_put_u32(kdf, rounds)) != 0) |
| 3984 | goto out; |
| 3985 | } else if (strcmp(kdfname, "none") != 0) { |
| 3986 | /* Unsupported KDF type */ |
| 3987 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 3988 | goto out; |
| 3989 | } |
| 3990 | if ((r = cipher_init(&ciphercontext, cipher, key, keylen, |
| 3991 | key + keylen, ivlen, 1)) != 0) |
| 3992 | goto out; |
| 3993 | |
| 3994 | if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 || |
| 3995 | (r = sshbuf_put_cstring(encoded, ciphername)) != 0 || |
| 3996 | (r = sshbuf_put_cstring(encoded, kdfname)) != 0 || |
| 3997 | (r = sshbuf_put_stringb(encoded, kdf)) != 0 || |
| 3998 | (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */ |
| 3999 | (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 || |
| 4000 | (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0) |
| 4001 | goto out; |
| 4002 | |
| 4003 | /* set up the buffer that will be encrypted */ |
| 4004 | |
| 4005 | /* Random check bytes */ |
| 4006 | check = arc4random(); |
| 4007 | if ((r = sshbuf_put_u32(encrypted, check)) != 0 || |
| 4008 | (r = sshbuf_put_u32(encrypted, check)) != 0) |
| 4009 | goto out; |
| 4010 | |
| 4011 | /* append private key and comment*/ |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4012 | if ((r = sshkey_private_serialize_opt(prv, encrypted, |
| 4013 | SSHKEY_SERIALIZE_FULL)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4014 | (r = sshbuf_put_cstring(encrypted, comment)) != 0) |
| 4015 | goto out; |
| 4016 | |
| 4017 | /* padding */ |
| 4018 | i = 0; |
| 4019 | while (sshbuf_len(encrypted) % blocksize) { |
| 4020 | if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0) |
| 4021 | goto out; |
| 4022 | } |
| 4023 | |
| 4024 | /* length in destination buffer */ |
| 4025 | if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0) |
| 4026 | goto out; |
| 4027 | |
| 4028 | /* encrypt */ |
| 4029 | if ((r = sshbuf_reserve(encoded, |
| 4030 | sshbuf_len(encrypted) + authlen, &cp)) != 0) |
| 4031 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 4032 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4033 | sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) |
| 4034 | goto out; |
| 4035 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4036 | sshbuf_reset(blob); |
djm@openbsd.org | 16dd8b2 | 2019-07-16 13:18:39 +0000 | [diff] [blame] | 4037 | |
| 4038 | /* assemble uuencoded key */ |
| 4039 | if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 || |
| 4040 | (r = sshbuf_dtob64(encoded, blob, 1)) != 0 || |
| 4041 | (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4042 | goto out; |
| 4043 | |
| 4044 | /* success */ |
| 4045 | r = 0; |
| 4046 | |
| 4047 | out: |
| 4048 | sshbuf_free(kdf); |
| 4049 | sshbuf_free(encoded); |
| 4050 | sshbuf_free(encrypted); |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 4051 | cipher_free(ciphercontext); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4052 | explicit_bzero(salt, sizeof(salt)); |
| 4053 | if (key != NULL) { |
| 4054 | explicit_bzero(key, keylen + ivlen); |
| 4055 | free(key); |
| 4056 | } |
| 4057 | if (pubkeyblob != NULL) { |
| 4058 | explicit_bzero(pubkeyblob, pubkeylen); |
| 4059 | free(pubkeyblob); |
| 4060 | } |
| 4061 | if (b64 != NULL) { |
| 4062 | explicit_bzero(b64, strlen(b64)); |
| 4063 | free(b64); |
| 4064 | } |
| 4065 | return r; |
| 4066 | } |
| 4067 | |
| 4068 | static int |
| 4069 | sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, |
| 4070 | struct sshkey **keyp, char **commentp) |
| 4071 | { |
| 4072 | char *comment = NULL, *ciphername = NULL, *kdfname = NULL; |
| 4073 | const struct sshcipher *cipher = NULL; |
| 4074 | const u_char *cp; |
| 4075 | int r = SSH_ERR_INTERNAL_ERROR; |
| 4076 | size_t encoded_len; |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4077 | size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4078 | struct sshbuf *encoded = NULL, *decoded = NULL; |
| 4079 | struct sshbuf *kdf = NULL, *decrypted = NULL; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 4080 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4081 | struct sshkey *k = NULL; |
| 4082 | u_char *key = NULL, *salt = NULL, *dp, pad, last; |
| 4083 | u_int blocksize, rounds, nkeys, encrypted_len, check1, check2; |
| 4084 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4085 | if (keyp != NULL) |
| 4086 | *keyp = NULL; |
| 4087 | if (commentp != NULL) |
| 4088 | *commentp = NULL; |
| 4089 | |
| 4090 | if ((encoded = sshbuf_new()) == NULL || |
| 4091 | (decoded = sshbuf_new()) == NULL || |
| 4092 | (decrypted = sshbuf_new()) == NULL) { |
| 4093 | r = SSH_ERR_ALLOC_FAIL; |
| 4094 | goto out; |
| 4095 | } |
| 4096 | |
| 4097 | /* check preamble */ |
| 4098 | cp = sshbuf_ptr(blob); |
| 4099 | encoded_len = sshbuf_len(blob); |
| 4100 | if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) || |
| 4101 | memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) { |
| 4102 | r = SSH_ERR_INVALID_FORMAT; |
| 4103 | goto out; |
| 4104 | } |
| 4105 | cp += MARK_BEGIN_LEN; |
| 4106 | encoded_len -= MARK_BEGIN_LEN; |
| 4107 | |
| 4108 | /* Look for end marker, removing whitespace as we go */ |
| 4109 | while (encoded_len > 0) { |
| 4110 | if (*cp != '\n' && *cp != '\r') { |
| 4111 | if ((r = sshbuf_put_u8(encoded, *cp)) != 0) |
| 4112 | goto out; |
| 4113 | } |
| 4114 | last = *cp; |
| 4115 | encoded_len--; |
| 4116 | cp++; |
| 4117 | if (last == '\n') { |
| 4118 | if (encoded_len >= MARK_END_LEN && |
| 4119 | memcmp(cp, MARK_END, MARK_END_LEN) == 0) { |
| 4120 | /* \0 terminate */ |
| 4121 | if ((r = sshbuf_put_u8(encoded, 0)) != 0) |
| 4122 | goto out; |
| 4123 | break; |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | if (encoded_len == 0) { |
| 4128 | r = SSH_ERR_INVALID_FORMAT; |
| 4129 | goto out; |
| 4130 | } |
| 4131 | |
| 4132 | /* decode base64 */ |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 4133 | if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4134 | goto out; |
| 4135 | |
| 4136 | /* check magic */ |
| 4137 | if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) || |
| 4138 | memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) { |
| 4139 | r = SSH_ERR_INVALID_FORMAT; |
| 4140 | goto out; |
| 4141 | } |
| 4142 | /* parse public portion of key */ |
| 4143 | if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 || |
| 4144 | (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 || |
| 4145 | (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 || |
| 4146 | (r = sshbuf_froms(decoded, &kdf)) != 0 || |
| 4147 | (r = sshbuf_get_u32(decoded, &nkeys)) != 0 || |
| 4148 | (r = sshbuf_skip_string(decoded)) != 0 || /* pubkey */ |
| 4149 | (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0) |
| 4150 | goto out; |
| 4151 | |
| 4152 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
| 4153 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 4154 | goto out; |
| 4155 | } |
| 4156 | if ((passphrase == NULL || strlen(passphrase) == 0) && |
| 4157 | strcmp(ciphername, "none") != 0) { |
| 4158 | /* passphrase required */ |
| 4159 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4160 | goto out; |
| 4161 | } |
| 4162 | if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) { |
| 4163 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 4164 | goto out; |
| 4165 | } |
| 4166 | if (!strcmp(kdfname, "none") && strcmp(ciphername, "none") != 0) { |
| 4167 | r = SSH_ERR_INVALID_FORMAT; |
| 4168 | goto out; |
| 4169 | } |
| 4170 | if (nkeys != 1) { |
| 4171 | /* XXX only one key supported */ |
| 4172 | r = SSH_ERR_INVALID_FORMAT; |
| 4173 | goto out; |
| 4174 | } |
| 4175 | |
| 4176 | /* check size of encrypted key blob */ |
| 4177 | blocksize = cipher_blocksize(cipher); |
| 4178 | if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) { |
| 4179 | r = SSH_ERR_INVALID_FORMAT; |
| 4180 | goto out; |
| 4181 | } |
| 4182 | |
| 4183 | /* setup key */ |
| 4184 | keylen = cipher_keylen(cipher); |
| 4185 | ivlen = cipher_ivlen(cipher); |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4186 | authlen = cipher_authlen(cipher); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4187 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
| 4188 | r = SSH_ERR_ALLOC_FAIL; |
| 4189 | goto out; |
| 4190 | } |
| 4191 | if (strcmp(kdfname, "bcrypt") == 0) { |
| 4192 | if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 || |
| 4193 | (r = sshbuf_get_u32(kdf, &rounds)) != 0) |
| 4194 | goto out; |
| 4195 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen, |
| 4196 | key, keylen + ivlen, rounds) < 0) { |
| 4197 | r = SSH_ERR_INVALID_FORMAT; |
| 4198 | goto out; |
| 4199 | } |
| 4200 | } |
| 4201 | |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4202 | /* check that an appropriate amount of auth data is present */ |
djm@openbsd.org | 29e0ecd | 2019-10-09 00:04:42 +0000 | [diff] [blame] | 4203 | if (sshbuf_len(decoded) < authlen || |
| 4204 | sshbuf_len(decoded) - authlen < encrypted_len) { |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4205 | r = SSH_ERR_INVALID_FORMAT; |
| 4206 | goto out; |
| 4207 | } |
| 4208 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4209 | /* decrypt private portion of key */ |
| 4210 | if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 || |
| 4211 | (r = cipher_init(&ciphercontext, cipher, key, keylen, |
| 4212 | key + keylen, ivlen, 0)) != 0) |
| 4213 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 4214 | if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded), |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4215 | encrypted_len, 0, authlen)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4216 | /* an integrity error here indicates an incorrect passphrase */ |
| 4217 | if (r == SSH_ERR_MAC_INVALID) |
| 4218 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4219 | goto out; |
| 4220 | } |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 4221 | if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4222 | goto out; |
| 4223 | /* there should be no trailing data */ |
| 4224 | if (sshbuf_len(decoded) != 0) { |
| 4225 | r = SSH_ERR_INVALID_FORMAT; |
| 4226 | goto out; |
| 4227 | } |
| 4228 | |
| 4229 | /* check check bytes */ |
| 4230 | if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 || |
| 4231 | (r = sshbuf_get_u32(decrypted, &check2)) != 0) |
| 4232 | goto out; |
| 4233 | if (check1 != check2) { |
| 4234 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4235 | goto out; |
| 4236 | } |
| 4237 | |
| 4238 | /* Load the private key and comment */ |
| 4239 | if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 || |
| 4240 | (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0) |
| 4241 | goto out; |
| 4242 | |
| 4243 | /* Check deterministic padding */ |
| 4244 | i = 0; |
| 4245 | while (sshbuf_len(decrypted)) { |
| 4246 | if ((r = sshbuf_get_u8(decrypted, &pad)) != 0) |
| 4247 | goto out; |
| 4248 | if (pad != (++i & 0xff)) { |
| 4249 | r = SSH_ERR_INVALID_FORMAT; |
| 4250 | goto out; |
| 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | /* XXX decode pubkey and check against private */ |
| 4255 | |
| 4256 | /* success */ |
| 4257 | r = 0; |
| 4258 | if (keyp != NULL) { |
| 4259 | *keyp = k; |
| 4260 | k = NULL; |
| 4261 | } |
| 4262 | if (commentp != NULL) { |
| 4263 | *commentp = comment; |
| 4264 | comment = NULL; |
| 4265 | } |
| 4266 | out: |
| 4267 | pad = 0; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 4268 | cipher_free(ciphercontext); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4269 | free(ciphername); |
| 4270 | free(kdfname); |
| 4271 | free(comment); |
| 4272 | if (salt != NULL) { |
| 4273 | explicit_bzero(salt, slen); |
| 4274 | free(salt); |
| 4275 | } |
| 4276 | if (key != NULL) { |
| 4277 | explicit_bzero(key, keylen + ivlen); |
| 4278 | free(key); |
| 4279 | } |
| 4280 | sshbuf_free(encoded); |
| 4281 | sshbuf_free(decoded); |
| 4282 | sshbuf_free(kdf); |
| 4283 | sshbuf_free(decrypted); |
| 4284 | sshkey_free(k); |
| 4285 | return r; |
| 4286 | } |
| 4287 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4288 | |
| 4289 | #ifdef WITH_OPENSSL |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4290 | /* convert SSH v2 key to PEM or PKCS#8 format */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4291 | static int |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4292 | sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf, |
| 4293 | int format, const char *_passphrase, const char *comment) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4294 | { |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4295 | int was_shielded = sshkey_is_shielded(key); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4296 | int success, r; |
| 4297 | int blen, len = strlen(_passphrase); |
| 4298 | u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL; |
Darren Tucker | 8fed0a5 | 2017-03-29 10:16:15 +1100 | [diff] [blame] | 4299 | const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL; |
djm@openbsd.org | 224f193 | 2017-10-13 06:24:51 +0000 | [diff] [blame] | 4300 | char *bptr; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4301 | BIO *bio = NULL; |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4302 | struct sshbuf *blob; |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4303 | EVP_PKEY *pkey = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4304 | |
| 4305 | if (len > 0 && len <= 4) |
| 4306 | return SSH_ERR_PASSPHRASE_TOO_SHORT; |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4307 | if ((blob = sshbuf_new()) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4308 | return SSH_ERR_ALLOC_FAIL; |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4309 | if ((bio = BIO_new(BIO_s_mem())) == NULL) { |
| 4310 | r = SSH_ERR_ALLOC_FAIL; |
| 4311 | goto out; |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4312 | } |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4313 | if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) { |
| 4314 | r = SSH_ERR_ALLOC_FAIL; |
| 4315 | goto out; |
| 4316 | } |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4317 | if ((r = sshkey_unshield_private(key)) != 0) |
| 4318 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4319 | |
| 4320 | switch (key->type) { |
| 4321 | case KEY_DSA: |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4322 | if (format == SSHKEY_PRIVATE_PEM) { |
| 4323 | success = PEM_write_bio_DSAPrivateKey(bio, key->dsa, |
| 4324 | cipher, passphrase, len, NULL, NULL); |
| 4325 | } else { |
| 4326 | success = EVP_PKEY_set1_DSA(pkey, key->dsa); |
| 4327 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4328 | break; |
| 4329 | #ifdef OPENSSL_HAS_ECC |
| 4330 | case KEY_ECDSA: |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4331 | if (format == SSHKEY_PRIVATE_PEM) { |
| 4332 | success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa, |
| 4333 | cipher, passphrase, len, NULL, NULL); |
| 4334 | } else { |
| 4335 | success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa); |
| 4336 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4337 | break; |
| 4338 | #endif |
| 4339 | case KEY_RSA: |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4340 | if (format == SSHKEY_PRIVATE_PEM) { |
| 4341 | success = PEM_write_bio_RSAPrivateKey(bio, key->rsa, |
| 4342 | cipher, passphrase, len, NULL, NULL); |
| 4343 | } else { |
| 4344 | success = EVP_PKEY_set1_RSA(pkey, key->rsa); |
| 4345 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4346 | break; |
| 4347 | default: |
| 4348 | success = 0; |
| 4349 | break; |
| 4350 | } |
| 4351 | if (success == 0) { |
| 4352 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 4353 | goto out; |
| 4354 | } |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4355 | if (format == SSHKEY_PRIVATE_PKCS8) { |
| 4356 | if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher, |
| 4357 | passphrase, len, NULL, NULL)) == 0) { |
| 4358 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 4359 | goto out; |
| 4360 | } |
| 4361 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4362 | if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) { |
| 4363 | r = SSH_ERR_INTERNAL_ERROR; |
| 4364 | goto out; |
| 4365 | } |
| 4366 | if ((r = sshbuf_put(blob, bptr, blen)) != 0) |
| 4367 | goto out; |
| 4368 | r = 0; |
| 4369 | out: |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4370 | if (was_shielded) |
| 4371 | r = sshkey_shield_private(key); |
| 4372 | if (r == 0) |
| 4373 | r = sshbuf_putb(buf, blob); |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4374 | |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4375 | EVP_PKEY_free(pkey); |
| 4376 | sshbuf_free(blob); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4377 | BIO_free(bio); |
| 4378 | return r; |
| 4379 | } |
| 4380 | #endif /* WITH_OPENSSL */ |
| 4381 | |
| 4382 | /* Serialise "key" to buffer "blob" */ |
| 4383 | int |
| 4384 | sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob, |
| 4385 | const char *passphrase, const char *comment, |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4386 | int format, const char *openssh_format_cipher, int openssh_format_rounds) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4387 | { |
| 4388 | switch (key->type) { |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 4389 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4390 | case KEY_DSA: |
| 4391 | case KEY_ECDSA: |
| 4392 | case KEY_RSA: |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4393 | break; /* see below */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4394 | #endif /* WITH_OPENSSL */ |
| 4395 | case KEY_ED25519: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 4396 | case KEY_ED25519_SK: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4397 | #ifdef WITH_XMSS |
| 4398 | case KEY_XMSS: |
| 4399 | #endif /* WITH_XMSS */ |
djm@openbsd.org | 02bb076 | 2019-10-31 21:15:14 +0000 | [diff] [blame] | 4400 | #ifdef WITH_OPENSSL |
| 4401 | case KEY_ECDSA_SK: |
| 4402 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4403 | return sshkey_private_to_blob2(key, blob, passphrase, |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4404 | comment, openssh_format_cipher, openssh_format_rounds); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4405 | default: |
| 4406 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 4407 | } |
djm@openbsd.org | eb0d8e7 | 2019-07-15 13:16:29 +0000 | [diff] [blame] | 4408 | |
| 4409 | #ifdef WITH_OPENSSL |
| 4410 | switch (format) { |
| 4411 | case SSHKEY_PRIVATE_OPENSSH: |
| 4412 | return sshkey_private_to_blob2(key, blob, passphrase, |
| 4413 | comment, openssh_format_cipher, openssh_format_rounds); |
| 4414 | case SSHKEY_PRIVATE_PEM: |
| 4415 | case SSHKEY_PRIVATE_PKCS8: |
| 4416 | return sshkey_private_to_blob_pem_pkcs8(key, blob, |
| 4417 | format, passphrase, comment); |
| 4418 | default: |
| 4419 | return SSH_ERR_INVALID_ARGUMENT; |
| 4420 | } |
| 4421 | #endif /* WITH_OPENSSL */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4422 | } |
| 4423 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4424 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 4425 | static int |
djm@openbsd.org | 2076e4a | 2017-06-09 06:40:24 +0000 | [diff] [blame] | 4426 | translate_libcrypto_error(unsigned long pem_err) |
| 4427 | { |
| 4428 | int pem_reason = ERR_GET_REASON(pem_err); |
| 4429 | |
| 4430 | switch (ERR_GET_LIB(pem_err)) { |
| 4431 | case ERR_LIB_PEM: |
| 4432 | switch (pem_reason) { |
| 4433 | case PEM_R_BAD_PASSWORD_READ: |
| 4434 | case PEM_R_PROBLEMS_GETTING_PASSWORD: |
| 4435 | case PEM_R_BAD_DECRYPT: |
| 4436 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4437 | default: |
| 4438 | return SSH_ERR_INVALID_FORMAT; |
| 4439 | } |
| 4440 | case ERR_LIB_EVP: |
| 4441 | switch (pem_reason) { |
| 4442 | case EVP_R_BAD_DECRYPT: |
| 4443 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4444 | #ifdef EVP_R_BN_DECODE_ERROR |
djm@openbsd.org | 2076e4a | 2017-06-09 06:40:24 +0000 | [diff] [blame] | 4445 | case EVP_R_BN_DECODE_ERROR: |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4446 | #endif |
djm@openbsd.org | 2076e4a | 2017-06-09 06:40:24 +0000 | [diff] [blame] | 4447 | case EVP_R_DECODE_ERROR: |
| 4448 | #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR |
| 4449 | case EVP_R_PRIVATE_KEY_DECODE_ERROR: |
| 4450 | #endif |
| 4451 | return SSH_ERR_INVALID_FORMAT; |
| 4452 | default: |
| 4453 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 4454 | } |
| 4455 | case ERR_LIB_ASN1: |
| 4456 | return SSH_ERR_INVALID_FORMAT; |
| 4457 | } |
| 4458 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 4459 | } |
| 4460 | |
| 4461 | static void |
| 4462 | clear_libcrypto_errors(void) |
| 4463 | { |
| 4464 | while (ERR_get_error() != 0) |
| 4465 | ; |
| 4466 | } |
| 4467 | |
| 4468 | /* |
| 4469 | * Translate OpenSSL error codes to determine whether |
| 4470 | * passphrase is required/incorrect. |
| 4471 | */ |
| 4472 | static int |
| 4473 | convert_libcrypto_error(void) |
| 4474 | { |
| 4475 | /* |
| 4476 | * Some password errors are reported at the beginning |
| 4477 | * of the error queue. |
| 4478 | */ |
| 4479 | if (translate_libcrypto_error(ERR_peek_error()) == |
| 4480 | SSH_ERR_KEY_WRONG_PASSPHRASE) |
| 4481 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4482 | return translate_libcrypto_error(ERR_peek_last_error()); |
| 4483 | } |
| 4484 | |
| 4485 | static int |
Damien Miller | 1273115 | 2018-10-11 10:29:29 +1100 | [diff] [blame] | 4486 | pem_passphrase_cb(char *buf, int size, int rwflag, void *u) |
| 4487 | { |
| 4488 | char *p = (char *)u; |
| 4489 | size_t len; |
| 4490 | |
| 4491 | if (p == NULL || (len = strlen(p)) == 0) |
| 4492 | return -1; |
| 4493 | if (size < 0 || len > (size_t)size) |
| 4494 | return -1; |
| 4495 | memcpy(buf, p, len); |
| 4496 | return (int)len; |
| 4497 | } |
| 4498 | |
| 4499 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4500 | sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type, |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 4501 | const char *passphrase, struct sshkey **keyp) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4502 | { |
| 4503 | EVP_PKEY *pk = NULL; |
| 4504 | struct sshkey *prv = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4505 | BIO *bio = NULL; |
| 4506 | int r; |
| 4507 | |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 4508 | if (keyp != NULL) |
| 4509 | *keyp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4510 | |
| 4511 | if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX) |
| 4512 | return SSH_ERR_ALLOC_FAIL; |
| 4513 | if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) != |
| 4514 | (int)sshbuf_len(blob)) { |
| 4515 | r = SSH_ERR_ALLOC_FAIL; |
| 4516 | goto out; |
| 4517 | } |
| 4518 | |
djm@openbsd.org | 2076e4a | 2017-06-09 06:40:24 +0000 | [diff] [blame] | 4519 | clear_libcrypto_errors(); |
Damien Miller | 1273115 | 2018-10-11 10:29:29 +1100 | [diff] [blame] | 4520 | if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4521 | (char *)passphrase)) == NULL) { |
djm@openbsd.org | edbb6fe | 2018-10-09 05:42:23 +0000 | [diff] [blame] | 4522 | /* |
| 4523 | * libcrypto may return various ASN.1 errors when attempting |
| 4524 | * to parse a key with an incorrect passphrase. |
| 4525 | * Treat all format errors as "incorrect passphrase" if a |
| 4526 | * passphrase was supplied. |
| 4527 | */ |
| 4528 | if (passphrase != NULL && *passphrase != '\0') |
| 4529 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 4530 | else |
| 4531 | r = convert_libcrypto_error(); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4532 | goto out; |
| 4533 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4534 | if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4535 | (type == KEY_UNSPEC || type == KEY_RSA)) { |
| 4536 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 4537 | r = SSH_ERR_ALLOC_FAIL; |
| 4538 | goto out; |
| 4539 | } |
| 4540 | prv->rsa = EVP_PKEY_get1_RSA(pk); |
| 4541 | prv->type = KEY_RSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4542 | #ifdef DEBUG_PK |
| 4543 | RSA_print_fp(stderr, prv->rsa, 8); |
| 4544 | #endif |
| 4545 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
| 4546 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 4547 | goto out; |
| 4548 | } |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4549 | if ((r = check_rsa_length(prv->rsa)) != 0) |
djm@openbsd.org | bd636f4 | 2017-05-07 23:15:59 +0000 | [diff] [blame] | 4550 | goto out; |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4551 | } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4552 | (type == KEY_UNSPEC || type == KEY_DSA)) { |
| 4553 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 4554 | r = SSH_ERR_ALLOC_FAIL; |
| 4555 | goto out; |
| 4556 | } |
| 4557 | prv->dsa = EVP_PKEY_get1_DSA(pk); |
| 4558 | prv->type = KEY_DSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4559 | #ifdef DEBUG_PK |
| 4560 | DSA_print_fp(stderr, prv->dsa, 8); |
| 4561 | #endif |
| 4562 | #ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 482d23b | 2018-09-13 02:08:33 +0000 | [diff] [blame] | 4563 | } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4564 | (type == KEY_UNSPEC || type == KEY_ECDSA)) { |
| 4565 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 4566 | r = SSH_ERR_ALLOC_FAIL; |
| 4567 | goto out; |
| 4568 | } |
| 4569 | prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk); |
| 4570 | prv->type = KEY_ECDSA; |
| 4571 | prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa); |
| 4572 | if (prv->ecdsa_nid == -1 || |
| 4573 | sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL || |
| 4574 | sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa), |
| 4575 | EC_KEY_get0_public_key(prv->ecdsa)) != 0 || |
| 4576 | sshkey_ec_validate_private(prv->ecdsa) != 0) { |
| 4577 | r = SSH_ERR_INVALID_FORMAT; |
| 4578 | goto out; |
| 4579 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4580 | # ifdef DEBUG_PK |
| 4581 | if (prv != NULL && prv->ecdsa != NULL) |
| 4582 | sshkey_dump_ec_key(prv->ecdsa); |
| 4583 | # endif |
| 4584 | #endif /* OPENSSL_HAS_ECC */ |
| 4585 | } else { |
| 4586 | r = SSH_ERR_INVALID_FORMAT; |
| 4587 | goto out; |
| 4588 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4589 | r = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 4590 | if (keyp != NULL) { |
| 4591 | *keyp = prv; |
| 4592 | prv = NULL; |
| 4593 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4594 | out: |
| 4595 | BIO_free(bio); |
jsing@openbsd.org | 7cd3163 | 2018-02-07 02:06:50 +0000 | [diff] [blame] | 4596 | EVP_PKEY_free(pk); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 4597 | sshkey_free(prv); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4598 | return r; |
| 4599 | } |
| 4600 | #endif /* WITH_OPENSSL */ |
| 4601 | |
| 4602 | int |
| 4603 | sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type, |
| 4604 | const char *passphrase, struct sshkey **keyp, char **commentp) |
| 4605 | { |
djm@openbsd.org | 155d540 | 2017-02-10 04:34:50 +0000 | [diff] [blame] | 4606 | int r = SSH_ERR_INTERNAL_ERROR; |
| 4607 | |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 4608 | if (keyp != NULL) |
| 4609 | *keyp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4610 | if (commentp != NULL) |
| 4611 | *commentp = NULL; |
| 4612 | |
| 4613 | switch (type) { |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 4614 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4615 | case KEY_DSA: |
| 4616 | case KEY_ECDSA: |
| 4617 | case KEY_RSA: |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 4618 | return sshkey_parse_private_pem_fileblob(blob, type, |
| 4619 | passphrase, keyp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4620 | #endif /* WITH_OPENSSL */ |
| 4621 | case KEY_ED25519: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4622 | #ifdef WITH_XMSS |
| 4623 | case KEY_XMSS: |
| 4624 | #endif /* WITH_XMSS */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4625 | return sshkey_parse_private2(blob, type, passphrase, |
| 4626 | keyp, commentp); |
| 4627 | case KEY_UNSPEC: |
djm@openbsd.org | 155d540 | 2017-02-10 04:34:50 +0000 | [diff] [blame] | 4628 | r = sshkey_parse_private2(blob, type, passphrase, keyp, |
| 4629 | commentp); |
| 4630 | /* Do not fallback to PEM parser if only passphrase is wrong. */ |
| 4631 | if (r == 0 || r == SSH_ERR_KEY_WRONG_PASSPHRASE) |
| 4632 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4633 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 4634 | return sshkey_parse_private_pem_fileblob(blob, type, |
| 4635 | passphrase, keyp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4636 | #else |
| 4637 | return SSH_ERR_INVALID_FORMAT; |
| 4638 | #endif /* WITH_OPENSSL */ |
| 4639 | default: |
| 4640 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | int |
| 4645 | sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase, |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 4646 | struct sshkey **keyp, char **commentp) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4647 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4648 | if (keyp != NULL) |
| 4649 | *keyp = NULL; |
| 4650 | if (commentp != NULL) |
| 4651 | *commentp = NULL; |
| 4652 | |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 4653 | return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC, |
| 4654 | passphrase, keyp, commentp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 4655 | } |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4656 | |
| 4657 | #ifdef WITH_XMSS |
| 4658 | /* |
| 4659 | * serialize the key with the current state and forward the state |
| 4660 | * maxsign times. |
| 4661 | */ |
| 4662 | int |
djm@openbsd.org | 8de52eb | 2019-06-23 12:21:46 +0000 | [diff] [blame] | 4663 | sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4664 | u_int32_t maxsign, sshkey_printfn *pr) |
| 4665 | { |
| 4666 | int r, rupdate; |
| 4667 | |
| 4668 | if (maxsign == 0 || |
| 4669 | sshkey_type_plain(k->type) != KEY_XMSS) |
| 4670 | return sshkey_private_serialize_opt(k, b, |
| 4671 | SSHKEY_SERIALIZE_DEFAULT); |
| 4672 | if ((r = sshkey_xmss_get_state(k, pr)) != 0 || |
| 4673 | (r = sshkey_private_serialize_opt(k, b, |
| 4674 | SSHKEY_SERIALIZE_STATE)) != 0 || |
| 4675 | (r = sshkey_xmss_forward_state(k, maxsign)) != 0) |
| 4676 | goto out; |
| 4677 | r = 0; |
| 4678 | out: |
| 4679 | if ((rupdate = sshkey_xmss_update_state(k, pr)) != 0) { |
| 4680 | if (r == 0) |
| 4681 | r = rupdate; |
| 4682 | } |
| 4683 | return r; |
| 4684 | } |
| 4685 | |
| 4686 | u_int32_t |
| 4687 | sshkey_signatures_left(const struct sshkey *k) |
| 4688 | { |
| 4689 | if (sshkey_type_plain(k->type) == KEY_XMSS) |
| 4690 | return sshkey_xmss_signatures_left(k); |
| 4691 | return 0; |
| 4692 | } |
| 4693 | |
| 4694 | int |
| 4695 | sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign) |
| 4696 | { |
| 4697 | if (sshkey_type_plain(k->type) != KEY_XMSS) |
| 4698 | return SSH_ERR_INVALID_ARGUMENT; |
| 4699 | return sshkey_xmss_enable_maxsign(k, maxsign); |
| 4700 | } |
| 4701 | |
| 4702 | int |
| 4703 | sshkey_set_filename(struct sshkey *k, const char *filename) |
| 4704 | { |
| 4705 | if (k == NULL) |
| 4706 | return SSH_ERR_INVALID_ARGUMENT; |
| 4707 | if (sshkey_type_plain(k->type) != KEY_XMSS) |
| 4708 | return 0; |
| 4709 | if (filename == NULL) |
| 4710 | return SSH_ERR_INVALID_ARGUMENT; |
| 4711 | if ((k->xmss_filename = strdup(filename)) == NULL) |
| 4712 | return SSH_ERR_ALLOC_FAIL; |
| 4713 | return 0; |
| 4714 | } |
| 4715 | #else |
| 4716 | int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 4717 | sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 4718 | u_int32_t maxsign, sshkey_printfn *pr) |
| 4719 | { |
| 4720 | return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT); |
| 4721 | } |
| 4722 | |
| 4723 | u_int32_t |
| 4724 | sshkey_signatures_left(const struct sshkey *k) |
| 4725 | { |
| 4726 | return 0; |
| 4727 | } |
| 4728 | |
| 4729 | int |
| 4730 | sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign) |
| 4731 | { |
| 4732 | return SSH_ERR_INVALID_ARGUMENT; |
| 4733 | } |
| 4734 | |
| 4735 | int |
| 4736 | sshkey_set_filename(struct sshkey *k, const char *filename) |
| 4737 | { |
| 4738 | if (k == NULL) |
| 4739 | return SSH_ERR_INVALID_ARGUMENT; |
| 4740 | return 0; |
| 4741 | } |
| 4742 | #endif /* WITH_XMSS */ |