Coverage Report

Created: 2026-02-14 07:20

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