/src/openssl30/crypto/asn1/d2i_pr.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | /* We need to use some engine deprecated APIs */ | 
| 11 |  | #define OPENSSL_SUPPRESS_DEPRECATED | 
| 12 |  |  | 
| 13 |  | #include <stdio.h> | 
| 14 |  | #include "internal/cryptlib.h" | 
| 15 |  | #include <openssl/bn.h> | 
| 16 |  | #include <openssl/evp.h> | 
| 17 |  | #include <openssl/objects.h> | 
| 18 |  | #include <openssl/decoder.h> | 
| 19 |  | #include <openssl/engine.h> | 
| 20 |  | #include <openssl/x509.h> | 
| 21 |  | #include <openssl/asn1.h> | 
| 22 |  | #include "crypto/asn1.h" | 
| 23 |  | #include "crypto/evp.h" | 
| 24 |  | #include "internal/asn1.h" | 
| 25 |  |  | 
| 26 |  | static EVP_PKEY * | 
| 27 |  | d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp, | 
| 28 |  |                        long length, OSSL_LIB_CTX *libctx, const char *propq) | 
| 29 | 12.2k | { | 
| 30 | 12.2k |     OSSL_DECODER_CTX *dctx = NULL; | 
| 31 | 12.2k |     size_t len = length; | 
| 32 | 12.2k |     EVP_PKEY *pkey = NULL, *bak_a = NULL; | 
| 33 | 12.2k |     EVP_PKEY **ppkey = &pkey; | 
| 34 | 12.2k |     const char *key_name = NULL; | 
| 35 | 12.2k |     const char *input_structures[] = { "type-specific", "PrivateKeyInfo", NULL }; | 
| 36 | 12.2k |     int i, ret; | 
| 37 |  |  | 
| 38 | 12.2k |     if (keytype != EVP_PKEY_NONE) { | 
| 39 | 0 |         key_name = evp_pkey_type2name(keytype); | 
| 40 | 0 |         if (key_name == NULL) | 
| 41 | 0 |             return NULL; | 
| 42 | 0 |     } | 
| 43 |  |  | 
| 44 | 46.2k |     for (i = 0;  i < (int)OSSL_NELEM(input_structures); ++i) { | 
| 45 | 34.9k |         const unsigned char *p = *pp; | 
| 46 |  |  | 
| 47 | 34.9k |         if (a != NULL && (bak_a = *a) != NULL) | 
| 48 | 0 |             ppkey = a; | 
| 49 | 34.9k |         dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", | 
| 50 | 34.9k |                                              input_structures[i], key_name, | 
| 51 | 34.9k |                                              EVP_PKEY_KEYPAIR, libctx, propq); | 
| 52 | 34.9k |         if (a != NULL) | 
| 53 | 0 |             *a = bak_a; | 
| 54 | 34.9k |         if (dctx == NULL) | 
| 55 | 0 |             continue; | 
| 56 |  |  | 
| 57 | 34.9k |         ret = OSSL_DECODER_from_data(dctx, pp, &len); | 
| 58 | 34.9k |         OSSL_DECODER_CTX_free(dctx); | 
| 59 | 34.9k |         if (ret) { | 
| 60 | 965 |             if (*ppkey != NULL | 
| 61 | 965 |                 && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) { | 
| 62 | 965 |                 if (a != NULL) | 
| 63 | 0 |                     *a = *ppkey; | 
| 64 | 965 |                 return *ppkey; | 
| 65 | 965 |             } | 
| 66 | 0 |             *pp = p; | 
| 67 | 0 |             goto err; | 
| 68 | 965 |         } | 
| 69 | 34.9k |     } | 
| 70 |  |     /* Fall through to error if all decodes failed */ | 
| 71 | 11.3k | err: | 
| 72 | 11.3k |     if (ppkey != a) | 
| 73 | 11.3k |         EVP_PKEY_free(*ppkey); | 
| 74 | 11.3k |     return NULL; | 
| 75 | 12.2k | } | 
| 76 |  |  | 
| 77 |  | EVP_PKEY * | 
| 78 |  | ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp, | 
| 79 |  |                            long length, OSSL_LIB_CTX *libctx, const char *propq) | 
| 80 | 19.3k | { | 
| 81 | 19.3k |     EVP_PKEY *ret; | 
| 82 | 19.3k |     const unsigned char *p = *pp; | 
| 83 |  |  | 
| 84 | 19.3k |     if (a == NULL || *a == NULL) { | 
| 85 | 19.3k |         if ((ret = EVP_PKEY_new()) == NULL) { | 
| 86 | 0 |             ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); | 
| 87 | 0 |             return NULL; | 
| 88 | 0 |         } | 
| 89 | 19.3k |     } else { | 
| 90 | 0 |         ret = *a; | 
| 91 | 0 | #ifndef OPENSSL_NO_ENGINE | 
| 92 | 0 |         ENGINE_finish(ret->engine); | 
| 93 | 0 |         ret->engine = NULL; | 
| 94 | 0 | #endif | 
| 95 | 0 |     } | 
| 96 |  |  | 
| 97 | 19.3k |     if (!EVP_PKEY_set_type(ret, keytype)) { | 
| 98 | 0 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE); | 
| 99 | 0 |         goto err; | 
| 100 | 0 |     } | 
| 101 |  |  | 
| 102 | 19.3k |     ERR_set_mark(); | 
| 103 | 19.3k |     if (!ret->ameth->old_priv_decode || | 
| 104 | 19.3k |         !ret->ameth->old_priv_decode(ret, &p, length)) { | 
| 105 | 19.2k |         if (ret->ameth->priv_decode != NULL | 
| 106 | 19.2k |                 || ret->ameth->priv_decode_ex != NULL) { | 
| 107 | 19.2k |             EVP_PKEY *tmp; | 
| 108 | 19.2k |             PKCS8_PRIV_KEY_INFO *p8 = NULL; | 
| 109 | 19.2k |             p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length); | 
| 110 | 19.2k |             if (p8 == NULL) { | 
| 111 | 19.2k |                 ERR_clear_last_mark(); | 
| 112 | 19.2k |                 goto err; | 
| 113 | 19.2k |             } | 
| 114 | 15 |             tmp = evp_pkcs82pkey_legacy(p8, libctx, propq); | 
| 115 | 15 |             PKCS8_PRIV_KEY_INFO_free(p8); | 
| 116 | 15 |             if (tmp == NULL) { | 
| 117 | 13 |                 ERR_clear_last_mark(); | 
| 118 | 13 |                 goto err; | 
| 119 | 13 |             } | 
| 120 | 2 |             EVP_PKEY_free(ret); | 
| 121 | 2 |             ret = tmp; | 
| 122 | 2 |             ERR_pop_to_mark(); | 
| 123 | 2 |             if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret)) | 
| 124 | 1 |                 goto err; | 
| 125 | 2 |         } else { | 
| 126 | 0 |             ERR_clear_last_mark(); | 
| 127 | 0 |             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); | 
| 128 | 0 |             goto err; | 
| 129 | 0 |         } | 
| 130 | 19.2k |     } else { | 
| 131 | 64 |       ERR_clear_last_mark(); | 
| 132 | 64 |     } | 
| 133 | 65 |     *pp = p; | 
| 134 | 65 |     if (a != NULL) | 
| 135 | 0 |         *a = ret; | 
| 136 | 65 |     return ret; | 
| 137 | 19.2k |  err: | 
| 138 | 19.2k |     if (a == NULL || *a != ret) | 
| 139 | 19.2k |         EVP_PKEY_free(ret); | 
| 140 | 19.2k |     return NULL; | 
| 141 | 19.3k | } | 
| 142 |  |  | 
| 143 |  | EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp, | 
| 144 |  |                             long length, OSSL_LIB_CTX *libctx, | 
| 145 |  |                             const char *propq) | 
| 146 | 0 | { | 
| 147 | 0 |     EVP_PKEY *ret; | 
| 148 |  | 
 | 
| 149 | 0 |     ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq); | 
| 150 |  |     /* try the legacy path if the decoder failed */ | 
| 151 | 0 |     if (ret == NULL) | 
| 152 | 0 |         ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq); | 
| 153 | 0 |     return ret; | 
| 154 | 0 | } | 
| 155 |  |  | 
| 156 |  | EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, | 
| 157 |  |                          long length) | 
| 158 |  | { | 
| 159 |  |     return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL); | 
| 160 |  | } | 
| 161 |  |  | 
| 162 |  | static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a, | 
| 163 |  |                                            const unsigned char **pp, | 
| 164 |  |                                            long length, | 
| 165 |  |                                            OSSL_LIB_CTX *libctx, | 
| 166 |  |                                            const char *propq) | 
| 167 | 21.5k | { | 
| 168 | 21.5k |     STACK_OF(ASN1_TYPE) *inkey; | 
| 169 | 21.5k |     const unsigned char *p; | 
| 170 | 21.5k |     int keytype; | 
| 171 |  |  | 
| 172 | 21.5k |     p = *pp; | 
| 173 |  |     /* | 
| 174 |  |      * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by | 
| 175 |  |      * analyzing it we can determine the passed structure: this assumes the | 
| 176 |  |      * input is surrounded by an ASN1 SEQUENCE. | 
| 177 |  |      */ | 
| 178 | 21.5k |     inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length); | 
| 179 | 21.5k |     p = *pp; | 
| 180 |  |     /* | 
| 181 |  |      * Since we only need to discern "traditional format" RSA and DSA keys we | 
| 182 |  |      * can just count the elements. | 
| 183 |  |      */ | 
| 184 | 21.5k |     if (sk_ASN1_TYPE_num(inkey) == 6) { | 
| 185 | 499 |         keytype = EVP_PKEY_DSA; | 
| 186 | 21.0k |     } else if (sk_ASN1_TYPE_num(inkey) == 4) { | 
| 187 | 753 |         keytype = EVP_PKEY_EC; | 
| 188 | 20.3k |     } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not | 
| 189 |  |                                               * traditional format */ | 
| 190 | 2.23k |         PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length); | 
| 191 | 2.23k |         EVP_PKEY *ret; | 
| 192 |  |  | 
| 193 | 2.23k |         sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free); | 
| 194 | 2.23k |         if (p8 == NULL) { | 
| 195 | 2.04k |             ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE); | 
| 196 | 2.04k |             return NULL; | 
| 197 | 2.04k |         } | 
| 198 | 188 |         ret = evp_pkcs82pkey_legacy(p8, libctx, propq); | 
| 199 | 188 |         PKCS8_PRIV_KEY_INFO_free(p8); | 
| 200 | 188 |         if (ret == NULL) | 
| 201 | 114 |             return NULL; | 
| 202 | 74 |         *pp = p; | 
| 203 | 74 |         if (a != NULL) { | 
| 204 | 0 |             *a = ret; | 
| 205 | 0 |         } | 
| 206 | 74 |         return ret; | 
| 207 | 18.1k |     } else { | 
| 208 | 18.1k |         keytype = EVP_PKEY_RSA; | 
| 209 | 18.1k |     } | 
| 210 | 19.3k |     sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free); | 
| 211 | 19.3k |     return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq); | 
| 212 | 21.5k | } | 
| 213 |  |  | 
| 214 |  | /* | 
| 215 |  |  * This works like d2i_PrivateKey() except it passes the keytype as | 
| 216 |  |  * EVP_PKEY_NONE, which then figures out the type during decoding. | 
| 217 |  |  */ | 
| 218 |  | EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp, | 
| 219 |  |                                 long length, OSSL_LIB_CTX *libctx, | 
| 220 |  |                                 const char *propq) | 
| 221 | 24.0k | { | 
| 222 | 24.0k |     EVP_PKEY *ret; | 
| 223 |  |  | 
| 224 | 24.0k |     ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq); | 
| 225 |  |     /* try the legacy path if the decoder failed */ | 
| 226 | 24.0k |     if (ret == NULL) | 
| 227 | 21.5k |         ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq); | 
| 228 | 24.0k |     return ret; | 
| 229 | 24.0k | } | 
| 230 |  |  | 
| 231 |  | EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp, | 
| 232 |  |                              long length) | 
| 233 | 24.0k | { | 
| 234 | 24.0k |     return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL); | 
| 235 | 24.0k | } |