Coverage Report

Created: 2026-07-12 07:21

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