Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 56 | #include <openssl/opensslconf.h> |
| 57 | #if !defined(OPENSSL_SYS_STARBOARD) |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 58 | #include <string.h> |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 59 | #endif // !defined(OPENSSL_SYS_STARBOARD) |
| 60 | #include <openssl/mem.h> |
| 61 | #include <openssl/evp.h> |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 62 | #include <openssl/bytestring.h> |
| 63 | #include <openssl/dsa.h> |
| 64 | #include <openssl/ec_key.h> |
| 65 | #include <openssl/err.h> |
| 66 | #include <openssl/rsa.h> |
| 67 | |
| 68 | #include "internal.h" |
| 69 | #include "../internal.h" |
| 70 | |
| 71 | |
| 72 | static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = { |
| 73 | &rsa_asn1_meth, |
| 74 | &ec_asn1_meth, |
| 75 | &dsa_asn1_meth, |
| 76 | &ed25519_asn1_meth, |
| 77 | }; |
| 78 | |
| 79 | static int parse_key_type(CBS *cbs, int *out_type) { |
| 80 | CBS oid; |
| 81 | if (!CBS_get_asn1(cbs, &oid, CBS_ASN1_OBJECT)) { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kASN1Methods); i++) { |
| 86 | const EVP_PKEY_ASN1_METHOD *method = kASN1Methods[i]; |
| 87 | if (CBS_len(&oid) == method->oid_len && |
| 88 | OPENSSL_memcmp(CBS_data(&oid), method->oid, method->oid_len) == 0) { |
| 89 | *out_type = method->pkey_id; |
| 90 | return 1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | EVP_PKEY *EVP_parse_public_key(CBS *cbs) { |
| 98 | // Parse the SubjectPublicKeyInfo. |
| 99 | CBS spki, algorithm, key; |
| 100 | int type; |
| 101 | uint8_t padding; |
| 102 | if (!CBS_get_asn1(cbs, &spki, CBS_ASN1_SEQUENCE) || |
| 103 | !CBS_get_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
| 104 | !CBS_get_asn1(&spki, &key, CBS_ASN1_BITSTRING) || |
| 105 | CBS_len(&spki) != 0) { |
| 106 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 107 | return NULL; |
| 108 | } |
| 109 | if (!parse_key_type(&algorithm, &type)) { |
| 110 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 111 | return NULL; |
| 112 | } |
| 113 | if (// Every key type defined encodes the key as a byte string with the same |
| 114 | // conversion to BIT STRING. |
| 115 | !CBS_get_u8(&key, &padding) || |
| 116 | padding != 0) { |
| 117 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | // Set up an |EVP_PKEY| of the appropriate type. |
| 122 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 123 | if (ret == NULL || |
| 124 | !EVP_PKEY_set_type(ret, type)) { |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | // Call into the type-specific SPKI decoding function. |
| 129 | if (ret->ameth->pub_decode == NULL) { |
| 130 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 131 | goto err; |
| 132 | } |
| 133 | if (!ret->ameth->pub_decode(ret, &algorithm, &key)) { |
| 134 | goto err; |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | |
| 139 | err: |
| 140 | EVP_PKEY_free(ret); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key) { |
| 145 | if (key->ameth == NULL || key->ameth->pub_encode == NULL) { |
| 146 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | return key->ameth->pub_encode(cbb, key); |
| 151 | } |
| 152 | |
| 153 | EVP_PKEY *EVP_parse_private_key(CBS *cbs) { |
| 154 | // Parse the PrivateKeyInfo. |
| 155 | CBS pkcs8, algorithm, key; |
| 156 | uint64_t version; |
| 157 | int type; |
| 158 | if (!CBS_get_asn1(cbs, &pkcs8, CBS_ASN1_SEQUENCE) || |
| 159 | !CBS_get_asn1_uint64(&pkcs8, &version) || |
| 160 | version != 0 || |
| 161 | !CBS_get_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || |
| 162 | !CBS_get_asn1(&pkcs8, &key, CBS_ASN1_OCTETSTRING)) { |
| 163 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 164 | return NULL; |
| 165 | } |
| 166 | if (!parse_key_type(&algorithm, &type)) { |
| 167 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | // A PrivateKeyInfo ends with a SET of Attributes which we ignore. |
| 172 | |
| 173 | // Set up an |EVP_PKEY| of the appropriate type. |
| 174 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 175 | if (ret == NULL || |
| 176 | !EVP_PKEY_set_type(ret, type)) { |
| 177 | goto err; |
| 178 | } |
| 179 | |
| 180 | // Call into the type-specific PrivateKeyInfo decoding function. |
| 181 | if (ret->ameth->priv_decode == NULL) { |
| 182 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 183 | goto err; |
| 184 | } |
| 185 | if (!ret->ameth->priv_decode(ret, &algorithm, &key)) { |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | return ret; |
| 190 | |
| 191 | err: |
| 192 | EVP_PKEY_free(ret); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key) { |
| 197 | if (key->ameth == NULL || key->ameth->priv_encode == NULL) { |
| 198 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | return key->ameth->priv_encode(cbb, key); |
| 203 | } |
| 204 | |
| 205 | static EVP_PKEY *old_priv_decode(CBS *cbs, int type) { |
| 206 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 207 | if (ret == NULL) { |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | switch (type) { |
| 212 | case EVP_PKEY_EC: { |
| 213 | EC_KEY *ec_key = EC_KEY_parse_private_key(cbs, NULL); |
| 214 | if (ec_key == NULL || !EVP_PKEY_assign_EC_KEY(ret, ec_key)) { |
| 215 | EC_KEY_free(ec_key); |
| 216 | goto err; |
| 217 | } |
| 218 | return ret; |
| 219 | } |
| 220 | case EVP_PKEY_DSA: { |
| 221 | DSA *dsa = DSA_parse_private_key(cbs); |
| 222 | if (dsa == NULL || !EVP_PKEY_assign_DSA(ret, dsa)) { |
| 223 | DSA_free(dsa); |
| 224 | goto err; |
| 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | case EVP_PKEY_RSA: { |
| 229 | RSA *rsa = RSA_parse_private_key(cbs); |
| 230 | if (rsa == NULL || !EVP_PKEY_assign_RSA(ret, rsa)) { |
| 231 | RSA_free(rsa); |
| 232 | goto err; |
| 233 | } |
| 234 | return ret; |
| 235 | } |
| 236 | default: |
| 237 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNKNOWN_PUBLIC_KEY_TYPE); |
| 238 | goto err; |
| 239 | } |
| 240 | |
| 241 | err: |
| 242 | EVP_PKEY_free(ret); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp, |
| 247 | long len) { |
| 248 | if (len < 0) { |
| 249 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | // Parse with the legacy format. |
| 254 | CBS cbs; |
| 255 | CBS_init(&cbs, *inp, (size_t)len); |
| 256 | EVP_PKEY *ret = old_priv_decode(&cbs, type); |
| 257 | if (ret == NULL) { |
| 258 | // Try again with PKCS#8. |
| 259 | ERR_clear_error(); |
| 260 | CBS_init(&cbs, *inp, (size_t)len); |
| 261 | ret = EVP_parse_private_key(&cbs); |
| 262 | if (ret == NULL) { |
| 263 | return NULL; |
| 264 | } |
| 265 | if (ret->type != type) { |
| 266 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 267 | EVP_PKEY_free(ret); |
| 268 | return NULL; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (out != NULL) { |
| 273 | EVP_PKEY_free(*out); |
| 274 | *out = ret; |
| 275 | } |
| 276 | *inp = CBS_data(&cbs); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | // num_elements parses one SEQUENCE from |in| and returns the number of elements |
| 281 | // in it. On parse error, it returns zero. |
| 282 | static size_t num_elements(const uint8_t *in, size_t in_len) { |
| 283 | CBS cbs, sequence; |
| 284 | CBS_init(&cbs, in, (size_t)in_len); |
| 285 | |
| 286 | if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE)) { |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | size_t count = 0; |
| 291 | while (CBS_len(&sequence) > 0) { |
| 292 | if (!CBS_get_any_asn1_element(&sequence, NULL, NULL, NULL)) { |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | count++; |
| 297 | } |
| 298 | |
| 299 | return count; |
| 300 | } |
| 301 | |
| 302 | EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) { |
| 303 | if (len < 0) { |
| 304 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | // Parse the input as a PKCS#8 PrivateKeyInfo. |
| 309 | CBS cbs; |
| 310 | CBS_init(&cbs, *inp, (size_t)len); |
| 311 | EVP_PKEY *ret = EVP_parse_private_key(&cbs); |
| 312 | if (ret != NULL) { |
| 313 | if (out != NULL) { |
| 314 | EVP_PKEY_free(*out); |
| 315 | *out = ret; |
| 316 | } |
| 317 | *inp = CBS_data(&cbs); |
| 318 | return ret; |
| 319 | } |
| 320 | ERR_clear_error(); |
| 321 | |
| 322 | // Count the elements to determine the legacy key format. |
| 323 | switch (num_elements(*inp, (size_t)len)) { |
| 324 | case 4: |
| 325 | return d2i_PrivateKey(EVP_PKEY_EC, out, inp, len); |
| 326 | |
| 327 | case 6: |
| 328 | return d2i_PrivateKey(EVP_PKEY_DSA, out, inp, len); |
| 329 | |
| 330 | default: |
| 331 | return d2i_PrivateKey(EVP_PKEY_RSA, out, inp, len); |
| 332 | } |
| 333 | } |
| 334 | |
Andrew Top | 193dc3d | 2019-01-23 09:57:23 -0800 | [diff] [blame] | 335 | int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp) { |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 336 | switch (key->type) { |
| 337 | case EVP_PKEY_RSA: |
| 338 | return i2d_RSAPublicKey(key->pkey.rsa, outp); |
| 339 | case EVP_PKEY_DSA: |
| 340 | return i2d_DSAPublicKey(key->pkey.dsa, outp); |
| 341 | case EVP_PKEY_EC: |
| 342 | return i2o_ECPublicKey(key->pkey.ec, outp); |
| 343 | default: |
| 344 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 345 | return -1; |
| 346 | } |
| 347 | } |