Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/asn1/d2i_pr.c
Line
Count
Source (jump to first uncovered line)
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
21.7k
{
32
21.7k
    OSSL_DECODER_CTX *dctx = NULL;
33
21.7k
    size_t len = length;
34
21.7k
    EVP_PKEY *pkey = NULL, *bak_a = NULL;
35
21.7k
    EVP_PKEY **ppkey = &pkey;
36
21.7k
    const char *key_name = NULL;
37
21.7k
    char keytypebuf[OSSL_MAX_NAME_SIZE];
38
21.7k
    int ret;
39
21.7k
    const unsigned char *p = *pp;
40
21.7k
    const char *structure;
41
21.7k
    PKCS8_PRIV_KEY_INFO *p8info;
42
21.7k
    const ASN1_OBJECT *algoid;
43
44
21.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
21.7k
    ERR_set_mark();
52
21.7k
    p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);
53
21.7k
    ERR_pop_to_mark();
54
21.7k
    if (p8info != NULL) {
55
1.39k
        int64_t v;
56
57
        /* ascertain version is 0 or 1 as per RFC5958 */
58
1.39k
        if (!ASN1_INTEGER_get_int64(&v, p8info->version)
59
1.39k
            || (v != 0 && v != 1)) {
60
43
            *pp = p;
61
43
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ASN1_PARSE_ERROR);
62
43
            PKCS8_PRIV_KEY_INFO_free(p8info);
63
43
            return NULL;
64
43
        }
65
1.34k
        if (key_name == NULL
66
1.34k
                && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)
67
1.34k
                && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))
68
1.34k
            key_name = keytypebuf;
69
1.34k
        structure = "PrivateKeyInfo";
70
1.34k
        PKCS8_PRIV_KEY_INFO_free(p8info);
71
20.3k
    } else {
72
20.3k
        structure = "type-specific";
73
20.3k
    }
74
21.7k
    *pp = p;
75
76
21.7k
    if (a != NULL && (bak_a = *a) != NULL)
77
0
        ppkey = a;
78
21.7k
    dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
79
21.7k
                                         EVP_PKEY_KEYPAIR, libctx, propq);
80
21.7k
    if (a != NULL)
81
0
        *a = bak_a;
82
21.7k
    if (dctx == NULL)
83
0
        goto err;
84
85
21.7k
    ret = OSSL_DECODER_from_data(dctx, pp, &len);
86
21.7k
    OSSL_DECODER_CTX_free(dctx);
87
21.7k
    if (ret
88
21.7k
        && *ppkey != NULL
89
21.7k
        && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
90
2.59k
        if (a != NULL)
91
0
            *a = *ppkey;
92
2.59k
        return *ppkey;
93
2.59k
    }
94
95
19.1k
 err:
96
19.1k
    if (ppkey != a)
97
19.1k
        EVP_PKEY_free(*ppkey);
98
19.1k
    return NULL;
99
21.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
51.6k
{
105
51.6k
    EVP_PKEY *ret;
106
51.6k
    const unsigned char *p = *pp;
107
108
51.6k
    if (a == NULL || *a == NULL) {
109
51.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
51.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
51.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
51.6k
    ERR_set_mark();
127
51.6k
    if (!ret->ameth->old_priv_decode ||
128
51.6k
        !ret->ameth->old_priv_decode(ret, &p, length)) {
129
50.4k
        if (ret->ameth->priv_decode != NULL
130
50.4k
                || ret->ameth->priv_decode_ex != NULL) {
131
50.4k
            EVP_PKEY *tmp;
132
50.4k
            PKCS8_PRIV_KEY_INFO *p8 = NULL;
133
50.4k
            p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
134
50.4k
            if (p8 == NULL) {
135
50.4k
                ERR_clear_last_mark();
136
50.4k
                goto err;
137
50.4k
            }
138
46
            tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
139
46
            PKCS8_PRIV_KEY_INFO_free(p8);
140
46
            if (tmp == NULL) {
141
33
                ERR_clear_last_mark();
142
33
                goto err;
143
33
            }
144
13
            EVP_PKEY_free(ret);
145
13
            ret = tmp;
146
13
            ERR_pop_to_mark();
147
13
            if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
148
5
                goto err;
149
13
        } else {
150
0
            ERR_clear_last_mark();
151
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
152
0
            goto err;
153
0
        }
154
50.4k
    } else {
155
1.16k
      ERR_clear_last_mark();
156
1.16k
    }
157
1.17k
    *pp = p;
158
1.17k
    if (a != NULL)
159
0
        *a = ret;
160
1.17k
    return ret;
161
50.4k
 err:
162
50.4k
    if (a == NULL || *a != ret)
163
50.4k
        EVP_PKEY_free(ret);
164
50.4k
    return NULL;
165
51.6k
}
166
167
EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
168
                            long length, OSSL_LIB_CTX *libctx,
169
                            const char *propq)
170
0
{
171
0
    EVP_PKEY *ret;
172
173
0
    ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
174
    /* try the legacy path if the decoder failed */
175
0
    if (ret == NULL)
176
0
        ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
177
0
    return ret;
178
0
}
179
180
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
181
                         long length)
182
0
{
183
0
    return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
184
0
}
185
186
static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
187
                                           const unsigned char **pp,
188
                                           long length,
189
                                           OSSL_LIB_CTX *libctx,
190
                                           const char *propq)
191
57.8k
{
192
57.8k
    STACK_OF(ASN1_TYPE) *inkey;
193
57.8k
    const unsigned char *p;
194
57.8k
    int keytype;
195
196
57.8k
    p = *pp;
197
    /*
198
     * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
199
     * analyzing it we can determine the passed structure: this assumes the
200
     * input is surrounded by an ASN1 SEQUENCE.
201
     */
202
57.8k
    inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
203
57.8k
    p = *pp;
204
    /*
205
     * Since we only need to discern "traditional format" RSA and DSA keys we
206
     * can just count the elements.
207
     */
208
57.8k
    if (sk_ASN1_TYPE_num(inkey) == 6) {
209
1.13k
        keytype = EVP_PKEY_DSA;
210
56.7k
    } else if (sk_ASN1_TYPE_num(inkey) == 4) {
211
2.45k
        keytype = EVP_PKEY_EC;
212
54.2k
    } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
213
                                              * traditional format */
214
6.18k
        PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
215
6.18k
        EVP_PKEY *ret;
216
217
6.18k
        sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
218
6.18k
        if (p8 == NULL) {
219
5.30k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
220
5.30k
            return NULL;
221
5.30k
        }
222
884
        ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
223
884
        PKCS8_PRIV_KEY_INFO_free(p8);
224
884
        if (ret == NULL)
225
543
            return NULL;
226
341
        *pp = p;
227
341
        if (a != NULL) {
228
0
            *a = ret;
229
0
        }
230
341
        return ret;
231
48.0k
    } else {
232
48.0k
        keytype = EVP_PKEY_RSA;
233
48.0k
    }
234
51.6k
    sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
235
51.6k
    return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
236
57.8k
}
237
238
/*
239
 * This works like d2i_PrivateKey() except it passes the keytype as
240
 * EVP_PKEY_NONE, which then figures out the type during decoding.
241
 */
242
EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
243
                                long length, OSSL_LIB_CTX *libctx,
244
                                const char *propq)
245
66.3k
{
246
66.3k
    EVP_PKEY *ret;
247
248
66.3k
    ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
249
    /* try the legacy path if the decoder failed */
250
66.3k
    if (ret == NULL)
251
57.8k
        ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
252
66.3k
    return ret;
253
66.3k
}
254
255
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
256
                             long length)
257
66.3k
{
258
66.3k
    return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
259
66.3k
}