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