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