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