| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 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 ([email protected]). |
| 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 ([email protected])" |
| 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 ([email protected])" |
| 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.] */ |
| 56 |
|
| 57 |
#include <openssl/evp.h> |
| 58 |
|
| 59 |
#include <string.h> |
| 60 |
|
| 61 |
#include <openssl/bytestring.h> |
| 62 |
#include <openssl/dsa.h> |
| 63 |
#include <openssl/ec_key.h> |
| 64 |
#include <openssl/err.h> |
| 65 |
#include <openssl/rsa.h> |
| 66 |
|
| 67 |
#include "internal.h" |
| 68 |
|
| 69 |
|
| 70 |
static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = { |
| 71 |
&rsa_asn1_meth, |
| 72 |
&ec_asn1_meth, |
| 73 |
&dsa_asn1_meth, |
| 74 |
}; |
| 75 |
|
| 76 |
static int parse_key_type(CBS *cbs, int *out_type) { |
| 77 |
CBS oid; |
| 78 |
if (!CBS_get_asn1(cbs, &oid, CBS_ASN1_OBJECT)) { |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
unsigned i; |
| 83 |
for (i = 0; i < sizeof(kASN1Methods)/sizeof(kASN1Methods[0]); i++) { |
| 84 |
const EVP_PKEY_ASN1_METHOD *method = kASN1Methods[i]; |
| 85 |
if (CBS_len(&oid) == method->oid_len && |
| 86 |
memcmp(CBS_data(&oid), method->oid, method->oid_len) == 0) { |
| 87 |
*out_type = method->pkey_id; |
| 88 |
return 1; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
EVP_PKEY *EVP_parse_public_key(CBS *cbs) { |
| 96 |
/* Parse the SubjectPublicKeyInfo. */ |
| 97 |
CBS spki, algorithm, key; |
| 98 |
int type; |
| 99 |
uint8_t padding; |
| 100 |
if (!CBS_get_asn1(cbs, &spki, CBS_ASN1_SEQUENCE) || |
| 101 |
!CBS_get_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
| 102 |
!parse_key_type(&algorithm, &type) || |
| 103 |
!CBS_get_asn1(&spki, &key, CBS_ASN1_BITSTRING) || |
| 104 |
CBS_len(&spki) != 0 || |
| 105 |
/* Every key type defined encodes the key as a byte string with the same |
| 106 |
* conversion to BIT STRING. */ |
| 107 |
!CBS_get_u8(&key, &padding) || |
| 108 |
padding != 0) { |
| 109 |
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
/* Set up an |EVP_PKEY| of the appropriate type. */ |
| 114 |
EVP_PKEY *ret = EVP_PKEY_new(); |
| 115 |
if (ret == NULL || |
| 116 |
!EVP_PKEY_set_type(ret, type)) { |
| 117 |
goto err; |
| 118 |
} |
| 119 |
|
| 120 |
/* Call into the type-specific SPKI decoding function. */ |
| 121 |
if (ret->ameth->pub_decode == NULL) { |
| 122 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 123 |
goto err; |
| 124 |
} |
| 125 |
if (!ret->ameth->pub_decode(ret, &algorithm, &key)) { |
| 126 |
goto err; |
| 127 |
} |
| 128 |
|
| 129 |
return ret; |
| 130 |
|
| 131 |
err: |
| 132 |
EVP_PKEY_free(ret); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key) { |
| 137 |
if (key->ameth == NULL || key->ameth->pub_encode == NULL) { |
| 138 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
return key->ameth->pub_encode(cbb, key); |
| 143 |
} |
| 144 |
|
| 145 |
EVP_PKEY *EVP_parse_private_key(CBS *cbs) { |
| 146 |
/* Parse the PrivateKeyInfo. */ |
| 147 |
CBS pkcs8, algorithm, key; |
| 148 |
uint64_t version; |
| 149 |
int type; |
| 150 |
if (!CBS_get_asn1(cbs, &pkcs8, CBS_ASN1_SEQUENCE) || |
| 151 |
!CBS_get_asn1_uint64(&pkcs8, &version) || |
| 152 |
version != 0 || |
| 153 |
!CBS_get_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || |
| 154 |
!parse_key_type(&algorithm, &type) || |
| 155 |
!CBS_get_asn1(&pkcs8, &key, CBS_ASN1_OCTETSTRING)) { |
| 156 |
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
/* A PrivateKeyInfo ends with a SET of Attributes which we ignore. */ |
| 161 |
|
| 162 |
/* Set up an |EVP_PKEY| of the appropriate type. */ |
| 163 |
EVP_PKEY *ret = EVP_PKEY_new(); |
| 164 |
if (ret == NULL || |
| 165 |
!EVP_PKEY_set_type(ret, type)) { |
| 166 |
goto err; |
| 167 |
} |
| 168 |
|
| 169 |
/* Call into the type-specific PrivateKeyInfo decoding function. */ |
| 170 |
if (ret->ameth->priv_decode == NULL) { |
| 171 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 172 |
goto err; |
| 173 |
} |
| 174 |
if (!ret->ameth->priv_decode(ret, &algorithm, &key)) { |
| 175 |
goto err; |
| 176 |
} |
| 177 |
|
| 178 |
return ret; |
| 179 |
|
| 180 |
err: |
| 181 |
EVP_PKEY_free(ret); |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key) { |
| 186 |
if (key->ameth == NULL || key->ameth->priv_encode == NULL) { |
| 187 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
return key->ameth->priv_encode(cbb, key); |
| 192 |
} |
| 193 |
|
| 194 |
static EVP_PKEY *old_priv_decode(CBS *cbs, int type) { |
| 195 |
EVP_PKEY *ret = EVP_PKEY_new(); |
| 196 |
if (ret == NULL) { |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
|
| 200 |
switch (type) { |
| 201 |
case EVP_PKEY_EC: { |
| 202 |
EC_KEY *ec_key = EC_KEY_parse_private_key(cbs, NULL); |
| 203 |
if (ec_key == NULL || !EVP_PKEY_assign_EC_KEY(ret, ec_key)) { |
| 204 |
EC_KEY_free(ec_key); |
| 205 |
goto err; |
| 206 |
} |
| 207 |
return ret; |
| 208 |
} |
| 209 |
case EVP_PKEY_DSA: { |
| 210 |
DSA *dsa = DSA_parse_private_key(cbs); |
| 211 |
if (dsa == NULL || !EVP_PKEY_assign_DSA(ret, dsa)) { |
| 212 |
DSA_free(dsa); |
| 213 |
goto err; |
| 214 |
} |
| 215 |
return ret; |
| 216 |
} |
| 217 |
case EVP_PKEY_RSA: { |
| 218 |
RSA *rsa = RSA_parse_private_key(cbs); |
| 219 |
if (rsa == NULL || !EVP_PKEY_assign_RSA(ret, rsa)) { |
| 220 |
RSA_free(rsa); |
| 221 |
goto err; |
| 222 |
} |
| 223 |
return ret; |
| 224 |
} |
| 225 |
default: |
| 226 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNKNOWN_PUBLIC_KEY_TYPE); |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
err: |
| 231 |
EVP_PKEY_free(ret); |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
|
| 235 |
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp, |
| 236 |
long len) { |
| 237 |
if (len < 0) { |
| 238 |
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 239 |
return NULL; |
| 240 |
} |
| 241 |
|
| 242 |
/* Parse with the legacy format. */ |
| 243 |
CBS cbs; |
| 244 |
CBS_init(&cbs, *inp, (size_t)len); |
| 245 |
EVP_PKEY *ret = old_priv_decode(&cbs, type); |
| 246 |
if (ret == NULL) { |
| 247 |
/* Try again with PKCS#8. */ |
| 248 |
ERR_clear_error(); |
| 249 |
CBS_init(&cbs, *inp, (size_t)len); |
| 250 |
ret = EVP_parse_private_key(&cbs); |
| 251 |
if (ret == NULL) { |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
if (ret->type != type) { |
| 255 |
OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 256 |
EVP_PKEY_free(ret); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (out != NULL) { |
| 262 |
EVP_PKEY_free(*out); |
| 263 |
*out = ret; |
| 264 |
} |
| 265 |
*inp = CBS_data(&cbs); |
| 266 |
return ret; |
| 267 |
} |
| 268 |
|
| 269 |
/* num_elements parses one SEQUENCE from |in| and returns the number of elements |
| 270 |
* in it. On parse error, it returns zero. */ |
| 271 |
static size_t num_elements(const uint8_t *in, size_t in_len) { |
| 272 |
CBS cbs, sequence; |
| 273 |
CBS_init(&cbs, in, (size_t)in_len); |
| 274 |
|
| 275 |
if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE)) { |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
size_t count = 0; |
| 280 |
while (CBS_len(&sequence) > 0) { |
| 281 |
if (!CBS_get_any_asn1_element(&sequence, NULL, NULL, NULL)) { |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
count++; |
| 286 |
} |
| 287 |
|
| 288 |
return count; |
| 289 |
} |
| 290 |
|
| 291 |
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) { |
| 292 |
if (len < 0) { |
| 293 |
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
/* Parse the input as a PKCS#8 PrivateKeyInfo. */ |
| 298 |
CBS cbs; |
| 299 |
CBS_init(&cbs, *inp, (size_t)len); |
| 300 |
EVP_PKEY *ret = EVP_parse_private_key(&cbs); |
| 301 |
if (ret != NULL) { |
| 302 |
if (out != NULL) { |
| 303 |
EVP_PKEY_free(*out); |
| 304 |
*out = ret; |
| 305 |
} |
| 306 |
*inp = CBS_data(&cbs); |
| 307 |
return ret; |
| 308 |
} |
| 309 |
ERR_clear_error(); |
| 310 |
|
| 311 |
/* Count the elements to determine the legacy key format. */ |
| 312 |
switch (num_elements(*inp, (size_t)len)) { |
| 313 |
case 4: |
| 314 |
return d2i_PrivateKey(EVP_PKEY_EC, out, inp, len); |
| 315 |
|
| 316 |
case 6: |
| 317 |
return d2i_PrivateKey(EVP_PKEY_DSA, out, inp, len); |
| 318 |
|
| 319 |
default: |
| 320 |
return d2i_PrivateKey(EVP_PKEY_RSA, out, inp, len); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp) { |
| 325 |
switch (key->type) { |
| 326 |
case EVP_PKEY_RSA: |
| 327 |
return i2d_RSAPublicKey(key->pkey.rsa, outp); |
| 328 |
case EVP_PKEY_DSA: |
| 329 |
return i2d_DSAPublicKey(key->pkey.dsa, outp); |
| 330 |
case EVP_PKEY_EC: |
| 331 |
return i2o_ECPublicKey(key->pkey.ec, outp); |
| 332 |
default: |
| 333 |
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 334 |
return -1; |
| 335 |
} |
| 336 |
} |