Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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
55.0k
{
28
55.0k
    OSSL_DECODER_CTX *dctx = NULL;
29
55.0k
    size_t len = length;
30
55.0k
    EVP_PKEY *pkey = NULL, *bak_a = NULL;
31
55.0k
    EVP_PKEY **ppkey = &pkey;
32
55.0k
    const char *key_name = NULL;
33
55.0k
    char keytypebuf[OSSL_MAX_NAME_SIZE];
34
55.0k
    int ret;
35
55.0k
    const unsigned char *p = *pp;
36
55.0k
    const char *structure;
37
55.0k
    PKCS8_PRIV_KEY_INFO *p8info;
38
55.0k
    const ASN1_OBJECT *algoid;
39
40
55.0k
    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
55.0k
    ERR_set_mark();
48
55.0k
    p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, length);
49
55.0k
    ERR_pop_to_mark();
50
55.0k
    if (p8info != NULL) {
51
3.45k
        int64_t v;
52
53
        /* ascertain version is 0 or 1 as per RFC5958 */
54
3.45k
        if (!ASN1_INTEGER_get_int64(&v, p8info->version)
55
3.45k
            || (v != 0 && v != 1)) {
56
85
            *pp = p;
57
85
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR);
58
85
            PKCS8_PRIV_KEY_INFO_free(p8info);
59
85
            return NULL;
60
85
        }
61
3.37k
        if (key_name == NULL
62
3.37k
            && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)
63
3.37k
            && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))
64
3.37k
            key_name = keytypebuf;
65
3.37k
        structure = "PrivateKeyInfo";
66
3.37k
        PKCS8_PRIV_KEY_INFO_free(p8info);
67
51.5k
    } else {
68
51.5k
        structure = "type-specific";
69
51.5k
    }
70
54.9k
    *pp = p;
71
72
54.9k
    if (a != NULL && (bak_a = *a) != NULL)
73
0
        ppkey = a;
74
54.9k
    dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
75
54.9k
        EVP_PKEY_KEYPAIR, libctx, propq);
76
54.9k
    if (a != NULL)
77
0
        *a = bak_a;
78
54.9k
    if (dctx == NULL)
79
0
        goto err;
80
81
54.9k
    ret = OSSL_DECODER_from_data(dctx, pp, &len);
82
54.9k
    OSSL_DECODER_CTX_free(dctx);
83
54.9k
    if (ret
84
6.54k
        && *ppkey != NULL
85
6.54k
        && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
86
6.54k
        if (a != NULL)
87
0
            *a = *ppkey;
88
6.54k
        return *ppkey;
89
6.54k
    }
90
91
48.3k
err:
92
48.3k
    if (ppkey != a)
93
48.3k
        EVP_PKEY_free(*ppkey);
94
48.3k
    return NULL;
95
54.9k
}
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
52.4k
{
101
52.4k
    EVP_PKEY *ret;
102
52.4k
    const unsigned char *p = *pp;
103
104
52.4k
    if (a == NULL || *a == NULL) {
105
52.4k
        if ((ret = EVP_PKEY_new()) == NULL) {
106
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
107
0
            return NULL;
108
0
        }
109
52.4k
    } else {
110
0
        ret = *a;
111
0
    }
112
113
52.4k
    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
52.4k
    ERR_set_mark();
119
52.4k
    if (!ret->ameth->old_priv_decode || !ret->ameth->old_priv_decode(ret, &p, length)) {
120
51.3k
        if (ret->ameth->priv_decode != NULL
121
51.3k
            || ret->ameth->priv_decode_ex != NULL) {
122
51.3k
            EVP_PKEY *tmp;
123
51.3k
            PKCS8_PRIV_KEY_INFO *p8 = NULL;
124
51.3k
            p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
125
51.3k
            if (p8 == NULL) {
126
51.2k
                ERR_clear_last_mark();
127
51.2k
                goto err;
128
51.2k
            }
129
95
            tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
130
95
            PKCS8_PRIV_KEY_INFO_free(p8);
131
95
            if (tmp == NULL) {
132
63
                ERR_clear_last_mark();
133
63
                goto err;
134
63
            }
135
32
            EVP_PKEY_free(ret);
136
32
            ret = tmp;
137
32
            ERR_pop_to_mark();
138
32
            if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
139
5
                goto err;
140
32
        } 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
51.3k
    } else {
146
1.14k
        ERR_clear_last_mark();
147
1.14k
    }
148
1.16k
    *pp = p;
149
1.16k
    if (a != NULL)
150
0
        *a = ret;
151
1.16k
    return ret;
152
51.2k
err:
153
51.2k
    if (a == NULL || *a != ret)
154
51.2k
        EVP_PKEY_free(ret);
155
51.2k
    return NULL;
156
52.4k
}
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
59.2k
{
183
59.2k
    STACK_OF(ASN1_TYPE) *inkey;
184
59.2k
    const unsigned char *p;
185
59.2k
    int keytype;
186
187
59.2k
    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
59.2k
    inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
194
59.2k
    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
59.2k
    if (sk_ASN1_TYPE_num(inkey) == 6) {
200
889
        keytype = EVP_PKEY_DSA;
201
58.3k
    } else if (sk_ASN1_TYPE_num(inkey) == 4) {
202
3.30k
        keytype = EVP_PKEY_EC;
203
55.0k
    } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
204
                                                * traditional format */
205
6.77k
        PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
206
6.77k
        EVP_PKEY *ret;
207
208
6.77k
        sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
209
6.77k
        if (p8 == NULL) {
210
6.01k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
211
6.01k
            return NULL;
212
6.01k
        }
213
764
        ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
214
764
        PKCS8_PRIV_KEY_INFO_free(p8);
215
764
        if (ret == NULL)
216
702
            return NULL;
217
62
        *pp = p;
218
62
        if (a != NULL) {
219
0
            *a = ret;
220
0
        }
221
62
        return ret;
222
48.2k
    } else {
223
48.2k
        keytype = EVP_PKEY_RSA;
224
48.2k
    }
225
52.4k
    sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
226
52.4k
    return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
227
59.2k
}
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
67.3k
{
237
67.3k
    EVP_PKEY *ret;
238
239
67.3k
    ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
240
    /* try the legacy path if the decoder failed */
241
67.3k
    if (ret == NULL)
242
59.2k
        ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
243
67.3k
    return ret;
244
67.3k
}
245
246
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
247
    long length)
248
67.3k
{
249
67.3k
    return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
250
67.3k
}