Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/asn1/d2i_pr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 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
#include "internal/sizes.h"
26
27
static EVP_PKEY *
28
d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
29
                       long length, OSSL_LIB_CTX *libctx, const char *propq)
30
35.5k
{
31
35.5k
    OSSL_DECODER_CTX *dctx = NULL;
32
35.5k
    size_t len = length;
33
35.5k
    EVP_PKEY *pkey = NULL, *bak_a = NULL;
34
35.5k
    EVP_PKEY **ppkey = &pkey;
35
35.5k
    const char *key_name = NULL;
36
35.5k
    char keytypebuf[OSSL_MAX_NAME_SIZE];
37
35.5k
    int ret;
38
35.5k
    const unsigned char *p = *pp;
39
35.5k
    const char *structure;
40
35.5k
    PKCS8_PRIV_KEY_INFO *p8info;
41
35.5k
    const ASN1_OBJECT *algoid;
42
43
35.5k
    if (keytype != EVP_PKEY_NONE) {
44
0
        key_name = evp_pkey_type2name(keytype);
45
0
        if (key_name == NULL)
46
0
            return NULL;
47
0
    }
48
49
    /* This is just a probe. It might fail, so we ignore errors */
50
35.5k
    ERR_set_mark();
51
35.5k
    p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);
52
35.5k
    ERR_pop_to_mark();
53
35.5k
    if (p8info != NULL) {
54
1.98k
        if (key_name == NULL
55
1.98k
                && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)
56
1.98k
                && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))
57
1.98k
            key_name = keytypebuf;
58
1.98k
        structure = "PrivateKeyInfo";
59
1.98k
        PKCS8_PRIV_KEY_INFO_free(p8info);
60
33.5k
    } else {
61
33.5k
        structure = "type-specific";
62
33.5k
    }
63
35.5k
    *pp = p;
64
65
35.5k
    if (a != NULL && (bak_a = *a) != NULL)
66
0
        ppkey = a;
67
35.5k
    dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
68
35.5k
                                         EVP_PKEY_KEYPAIR, libctx, propq);
69
35.5k
    if (a != NULL)
70
0
        *a = bak_a;
71
35.5k
    if (dctx == NULL)
72
0
        goto err;
73
74
35.5k
    ret = OSSL_DECODER_from_data(dctx, pp, &len);
75
35.5k
    OSSL_DECODER_CTX_free(dctx);
76
35.5k
    if (ret
77
4.59k
        && *ppkey != NULL
78
4.59k
        && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
79
4.59k
        if (a != NULL)
80
0
            *a = *ppkey;
81
4.59k
        return *ppkey;
82
4.59k
    }
83
84
30.9k
 err:
85
30.9k
    if (ppkey != a)
86
30.9k
        EVP_PKEY_free(*ppkey);
87
30.9k
    return NULL;
88
35.5k
}
89
90
EVP_PKEY *
91
ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
92
                           long length, OSSL_LIB_CTX *libctx, const char *propq)
93
59.9k
{
94
59.9k
    EVP_PKEY *ret;
95
59.9k
    const unsigned char *p = *pp;
96
97
59.9k
    if (a == NULL || *a == NULL) {
98
59.9k
        if ((ret = EVP_PKEY_new()) == NULL) {
99
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
100
0
            return NULL;
101
0
        }
102
59.9k
    } else {
103
0
        ret = *a;
104
0
#ifndef OPENSSL_NO_ENGINE
105
0
        ENGINE_finish(ret->engine);
106
0
        ret->engine = NULL;
107
0
#endif
108
0
    }
109
110
59.9k
    if (!EVP_PKEY_set_type(ret, keytype)) {
111
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
112
0
        goto err;
113
0
    }
114
115
59.9k
    ERR_set_mark();
116
59.9k
    if (!ret->ameth->old_priv_decode ||
117
59.9k
        !ret->ameth->old_priv_decode(ret, &p, length)) {
118
58.4k
        if (ret->ameth->priv_decode != NULL
119
58.4k
                || ret->ameth->priv_decode_ex != NULL) {
120
58.4k
            EVP_PKEY *tmp;
121
58.4k
            PKCS8_PRIV_KEY_INFO *p8 = NULL;
122
58.4k
            p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
123
58.4k
            if (p8 == NULL) {
124
58.4k
                ERR_clear_last_mark();
125
58.4k
                goto err;
126
58.4k
            }
127
69
            tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
128
69
            PKCS8_PRIV_KEY_INFO_free(p8);
129
69
            if (tmp == NULL) {
130
50
                ERR_clear_last_mark();
131
50
                goto err;
132
50
            }
133
19
            EVP_PKEY_free(ret);
134
19
            ret = tmp;
135
19
            ERR_pop_to_mark();
136
19
            if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
137
6
                goto err;
138
19
        } else {
139
0
            ERR_clear_last_mark();
140
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
141
0
            goto err;
142
0
        }
143
58.4k
    } else {
144
1.42k
      ERR_clear_last_mark();
145
1.42k
    }
146
1.44k
    *pp = p;
147
1.44k
    if (a != NULL)
148
0
        *a = ret;
149
1.44k
    return ret;
150
58.4k
 err:
151
58.4k
    if (a == NULL || *a != ret)
152
58.4k
        EVP_PKEY_free(ret);
153
58.4k
    return NULL;
154
59.9k
}
155
156
EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
157
                            long length, OSSL_LIB_CTX *libctx,
158
                            const char *propq)
159
0
{
160
0
    EVP_PKEY *ret;
161
162
0
    ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
163
    /* try the legacy path if the decoder failed */
164
0
    if (ret == NULL)
165
0
        ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
166
0
    return ret;
167
0
}
168
169
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
170
                         long length)
171
0
{
172
0
    return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
173
0
}
174
175
static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
176
                                           const unsigned char **pp,
177
                                           long length,
178
                                           OSSL_LIB_CTX *libctx,
179
                                           const char *propq)
180
67.5k
{
181
67.5k
    STACK_OF(ASN1_TYPE) *inkey;
182
67.5k
    const unsigned char *p;
183
67.5k
    int keytype;
184
185
67.5k
    p = *pp;
186
    /*
187
     * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
188
     * analyzing it we can determine the passed structure: this assumes the
189
     * input is surrounded by an ASN1 SEQUENCE.
190
     */
191
67.5k
    inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
192
67.5k
    p = *pp;
193
    /*
194
     * Since we only need to discern "traditional format" RSA and DSA keys we
195
     * can just count the elements.
196
     */
197
67.5k
    if (sk_ASN1_TYPE_num(inkey) == 6) {
198
1.24k
        keytype = EVP_PKEY_DSA;
199
66.3k
    } else if (sk_ASN1_TYPE_num(inkey) == 4) {
200
3.01k
        keytype = EVP_PKEY_EC;
201
63.3k
    } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
202
                                              * traditional format */
203
7.67k
        PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
204
7.67k
        EVP_PKEY *ret;
205
206
7.67k
        sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
207
7.67k
        if (p8 == NULL) {
208
6.67k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
209
6.67k
            return NULL;
210
6.67k
        }
211
999
        ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
212
999
        PKCS8_PRIV_KEY_INFO_free(p8);
213
999
        if (ret == NULL)
214
678
            return NULL;
215
321
        *pp = p;
216
321
        if (a != NULL) {
217
0
            *a = ret;
218
0
        }
219
321
        return ret;
220
55.6k
    } else {
221
55.6k
        keytype = EVP_PKEY_RSA;
222
55.6k
    }
223
59.9k
    sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
224
59.9k
    return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
225
67.5k
}
226
227
/*
228
 * This works like d2i_PrivateKey() except it passes the keytype as
229
 * EVP_PKEY_NONE, which then figures out the type during decoding.
230
 */
231
EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
232
                                long length, OSSL_LIB_CTX *libctx,
233
                                const char *propq)
234
77.2k
{
235
77.2k
    EVP_PKEY *ret;
236
237
77.2k
    ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
238
    /* try the legacy path if the decoder failed */
239
77.2k
    if (ret == NULL)
240
67.5k
        ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
241
77.2k
    return ret;
242
77.2k
}
243
244
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
245
                             long length)
246
77.2k
{
247
77.2k
    return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
248
77.2k
}