Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/d2i_pr.c
Line
Count
Source
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.9k
{
30
12.9k
    OSSL_DECODER_CTX *dctx = NULL;
31
12.9k
    size_t len = length;
32
12.9k
    EVP_PKEY *pkey = NULL, *bak_a = NULL;
33
12.9k
    EVP_PKEY **ppkey = &pkey;
34
12.9k
    const char *key_name = NULL;
35
12.9k
    const char *input_structures[] = { "type-specific", "PrivateKeyInfo", NULL };
36
12.9k
    int i, ret;
37
38
12.9k
    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.9k
    for (i = 0; i < (int)OSSL_NELEM(input_structures); ++i) {
45
35.5k
        const unsigned char *p = *pp;
46
47
35.5k
        if (a != NULL && (bak_a = *a) != NULL)
48
0
            ppkey = a;
49
35.5k
        dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER",
50
35.5k
            input_structures[i], key_name,
51
35.5k
            EVP_PKEY_KEYPAIR, libctx, propq);
52
35.5k
        if (a != NULL)
53
0
            *a = bak_a;
54
35.5k
        if (dctx == NULL)
55
0
            continue;
56
57
35.5k
        ret = OSSL_DECODER_from_data(dctx, pp, &len);
58
35.5k
        OSSL_DECODER_CTX_free(dctx);
59
35.5k
        if (ret) {
60
1.57k
            if (*ppkey != NULL
61
1.57k
                && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
62
1.57k
                if (a != NULL)
63
0
                    *a = *ppkey;
64
1.57k
                return *ppkey;
65
1.57k
            }
66
0
            *pp = p;
67
0
            goto err;
68
1.57k
        }
69
35.5k
    }
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.9k
}
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
54.9k
{
81
54.9k
    EVP_PKEY *ret;
82
54.9k
    const unsigned char *p = *pp;
83
84
54.9k
    if (a == NULL || *a == NULL) {
85
54.9k
        if ((ret = EVP_PKEY_new()) == NULL) {
86
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
87
0
            return NULL;
88
0
        }
89
54.9k
    } 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
54.9k
    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
54.9k
    ERR_set_mark();
103
54.9k
    if (!ret->ameth->old_priv_decode || !ret->ameth->old_priv_decode(ret, &p, length)) {
104
53.6k
        if (ret->ameth->priv_decode != NULL
105
53.6k
            || ret->ameth->priv_decode_ex != NULL) {
106
53.6k
            EVP_PKEY *tmp;
107
53.6k
            PKCS8_PRIV_KEY_INFO *p8 = NULL;
108
53.6k
            p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
109
53.6k
            if (p8 == NULL) {
110
53.5k
                ERR_clear_last_mark();
111
53.5k
                goto err;
112
53.5k
            }
113
56
            tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
114
56
            PKCS8_PRIV_KEY_INFO_free(p8);
115
56
            if (tmp == NULL) {
116
42
                ERR_clear_last_mark();
117
42
                goto err;
118
42
            }
119
14
            EVP_PKEY_free(ret);
120
14
            ret = tmp;
121
14
            ERR_pop_to_mark();
122
14
            if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
123
6
                goto err;
124
14
        } else {
125
0
            ERR_clear_last_mark();
126
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
127
0
            goto err;
128
0
        }
129
53.6k
    } else {
130
1.26k
        ERR_clear_last_mark();
131
1.26k
    }
132
1.27k
    *pp = p;
133
1.27k
    if (a != NULL)
134
0
        *a = ret;
135
1.27k
    return ret;
136
53.6k
err:
137
53.6k
    if (a == NULL || *a != ret)
138
53.6k
        EVP_PKEY_free(ret);
139
53.6k
    return NULL;
140
54.9k
}
141
142
EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
143
    long length, OSSL_LIB_CTX *libctx,
144
    const char *propq)
145
0
{
146
0
    EVP_PKEY *ret;
147
148
0
    ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
149
    /* try the legacy path if the decoder failed */
150
0
    if (ret == NULL)
151
0
        ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
152
0
    return ret;
153
0
}
154
155
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
156
    long length)
157
0
{
158
0
    return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
159
0
}
160
161
static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
162
    const unsigned char **pp,
163
    long length,
164
    OSSL_LIB_CTX *libctx,
165
    const char *propq)
166
61.8k
{
167
61.8k
    STACK_OF(ASN1_TYPE) *inkey;
168
61.8k
    const unsigned char *p;
169
61.8k
    int keytype;
170
171
61.8k
    p = *pp;
172
    /*
173
     * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
174
     * analyzing it we can determine the passed structure: this assumes the
175
     * input is surrounded by an ASN1 SEQUENCE.
176
     */
177
61.8k
    inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
178
61.8k
    p = *pp;
179
    /*
180
     * Since we only need to discern "traditional format" RSA and DSA keys we
181
     * can just count the elements.
182
     */
183
61.8k
    if (sk_ASN1_TYPE_num(inkey) == 6) {
184
1.14k
        keytype = EVP_PKEY_DSA;
185
60.6k
    } else if (sk_ASN1_TYPE_num(inkey) == 4) {
186
2.93k
        keytype = EVP_PKEY_EC;
187
57.7k
    } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
188
                                                * traditional format */
189
6.88k
        PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
190
6.88k
        EVP_PKEY *ret;
191
192
6.88k
        sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
193
6.88k
        if (p8 == NULL) {
194
6.05k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
195
6.05k
            return NULL;
196
6.05k
        }
197
838
        ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
198
838
        PKCS8_PRIV_KEY_INFO_free(p8);
199
838
        if (ret == NULL)
200
684
            return NULL;
201
154
        *pp = p;
202
154
        if (a != NULL) {
203
0
            *a = ret;
204
0
        }
205
154
        return ret;
206
50.8k
    } else {
207
50.8k
        keytype = EVP_PKEY_RSA;
208
50.8k
    }
209
54.9k
    sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
210
54.9k
    return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
211
61.8k
}
212
213
/*
214
 * This works like d2i_PrivateKey() except it passes the keytype as
215
 * EVP_PKEY_NONE, which then figures out the type during decoding.
216
 */
217
EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
218
    long length, OSSL_LIB_CTX *libctx,
219
    const char *propq)
220
71.4k
{
221
71.4k
    EVP_PKEY *ret;
222
223
71.4k
    ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
224
    /* try the legacy path if the decoder failed */
225
71.4k
    if (ret == NULL)
226
61.8k
        ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
227
71.4k
    return ret;
228
71.4k
}
229
230
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
231
    long length)
232
71.4k
{
233
71.4k
    return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
234
71.4k
}