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