Coverage Report

Created: 2025-07-23 06:08

/src/openssl/crypto/evp/evp_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2023 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
/*
11
 * Needed for EVP_PKEY_get0_asn1 and EVP_PKEY_asn1_get0_info
12
 */
13
#define OPENSSL_SUPPRESS_DEPRECATED
14
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/x509.h>
19
#include <openssl/rand.h>
20
#include <openssl/encoder.h>
21
#include <openssl/decoder.h>
22
#include "internal/provider.h"
23
#include "internal/sizes.h"
24
#include "crypto/asn1.h"
25
#include "crypto/evp.h"
26
#include "crypto/x509.h"
27
28
/* Extract a private key from a PKCS8 structure */
29
30
EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
31
                                const char *propq)
32
0
{
33
0
    EVP_PKEY *pkey = NULL;
34
0
    const ASN1_OBJECT *algoid;
35
0
    char obj_tmp[80];
36
37
0
    if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
38
0
        return NULL;
39
40
0
    if ((pkey = EVP_PKEY_new()) == NULL) {
41
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
42
0
        return NULL;
43
0
    }
44
45
0
    if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
46
0
        i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
47
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
48
0
                       "TYPE=%s", obj_tmp);
49
0
        goto error;
50
0
    }
51
52
0
    if (pkey->ameth->priv_decode_ex != NULL) {
53
0
        if (!pkey->ameth->priv_decode_ex(pkey, p8, libctx, propq))
54
0
            goto error;
55
0
    } else if (pkey->ameth->priv_decode != NULL) {
56
0
        if (!pkey->ameth->priv_decode(pkey, p8)) {
57
0
            ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_DECODE_ERROR);
58
0
            goto error;
59
0
        }
60
0
    } else {
61
0
        ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
62
0
        goto error;
63
0
    }
64
65
0
    return pkey;
66
67
0
 error:
68
0
    EVP_PKEY_free(pkey);
69
0
    return NULL;
70
0
}
71
72
EVP_PKEY *EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
73
                            const char *propq)
74
0
{
75
0
    EVP_PKEY *pkey = NULL;
76
0
    const unsigned char *p8_data = NULL;
77
0
    unsigned char *encoded_data = NULL;
78
0
    int encoded_len;
79
0
    int selection;
80
0
    size_t len;
81
0
    OSSL_DECODER_CTX *dctx = NULL;
82
0
    const ASN1_OBJECT *algoid = NULL;
83
0
    char keytype[OSSL_MAX_NAME_SIZE];
84
85
0
    if (p8 == NULL
86
0
            || !PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8)
87
0
            || !OBJ_obj2txt(keytype, sizeof(keytype), algoid, 0))
88
0
        return NULL;
89
90
0
    if ((encoded_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &encoded_data)) <= 0
91
0
            || encoded_data == NULL)
92
0
        return NULL;
93
94
0
    p8_data = encoded_data;
95
0
    len = encoded_len;
96
0
    selection = EVP_PKEY_KEYPAIR | EVP_PKEY_KEY_PARAMETERS;
97
0
    dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
98
0
                                         keytype, selection, libctx, propq);
99
100
0
    if (dctx != NULL && OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) {
101
0
        OSSL_DECODER_CTX_free(dctx);
102
103
        /*
104
         * This could happen if OBJ_obj2txt() returned a text OID and the
105
         * decoder has not got that OID as an alias. We fall back to a NULL
106
         * keytype
107
         */
108
0
        dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
109
0
                                             NULL, selection, libctx, propq);
110
0
    }
111
112
0
    if (dctx == NULL
113
0
        || !OSSL_DECODER_from_data(dctx, &p8_data, &len))
114
        /* try legacy */
115
0
        pkey = evp_pkcs82pkey_legacy(p8, libctx, propq);
116
117
0
    OPENSSL_clear_free(encoded_data, encoded_len);
118
0
    OSSL_DECODER_CTX_free(dctx);
119
0
    return pkey;
120
0
}
121
122
EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
123
0
{
124
0
    return EVP_PKCS82PKEY_ex(p8, NULL, NULL);
125
0
}
126
127
/* Turn a private key into a PKCS8 structure */
128
129
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
130
0
{
131
0
    PKCS8_PRIV_KEY_INFO *p8 = NULL;
132
0
    OSSL_ENCODER_CTX *ctx = NULL;
133
134
    /*
135
     * The implementation for provider-native keys is to encode the
136
     * key to a DER encoded PKCS#8 structure, then convert it to a
137
     * PKCS8_PRIV_KEY_INFO with good old d2i functions.
138
     */
139
0
    if (evp_pkey_is_provided(pkey)) {
140
0
        int selection = OSSL_KEYMGMT_SELECT_ALL;
141
0
        unsigned char *der = NULL;
142
0
        size_t derlen = 0;
143
0
        const unsigned char *pp;
144
145
0
        if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
146
0
                                                 "DER", "PrivateKeyInfo",
147
0
                                                 NULL)) == NULL
148
0
            || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
149
0
            goto error;
150
151
0
        pp = der;
152
0
        p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
153
0
        OPENSSL_free(der);
154
0
        if (p8 == NULL)
155
0
            goto error;
156
0
    } else {
157
0
        p8 = PKCS8_PRIV_KEY_INFO_new();
158
0
        if (p8  == NULL) {
159
0
            ERR_raise(ERR_LIB_EVP, ERR_R_ASN1_LIB);
160
0
            return NULL;
161
0
        }
162
163
0
        if (pkey->ameth != NULL) {
164
0
            if (pkey->ameth->priv_encode != NULL) {
165
0
                if (!pkey->ameth->priv_encode(p8, pkey)) {
166
0
                    ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
167
0
                    goto error;
168
0
                }
169
0
            } else {
170
0
                ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
171
0
                goto error;
172
0
            }
173
0
        } else {
174
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
175
0
            goto error;
176
0
        }
177
0
    }
178
0
    goto end;
179
0
 error:
180
0
    PKCS8_PRIV_KEY_INFO_free(p8);
181
0
    p8 = NULL;
182
0
 end:
183
0
    OSSL_ENCODER_CTX_free(ctx);
184
0
    return p8;
185
186
0
}
187
188
/* EVP_PKEY attribute functions */
189
190
int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
191
0
{
192
0
    return X509at_get_attr_count(key->attributes);
193
0
}
194
195
int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
196
0
{
197
0
    return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
198
0
}
199
200
int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
201
                             int lastpos)
202
0
{
203
0
    return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
204
0
}
205
206
X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
207
0
{
208
0
    return X509at_get_attr(key->attributes, loc);
209
0
}
210
211
X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
212
0
{
213
0
    return X509at_delete_attr(key->attributes, loc);
214
0
}
215
216
int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
217
0
{
218
0
    if (X509at_add1_attr(&key->attributes, attr))
219
0
        return 1;
220
0
    return 0;
221
0
}
222
223
int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
224
                              const ASN1_OBJECT *obj, int type,
225
                              const unsigned char *bytes, int len)
226
0
{
227
0
    if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
228
0
        return 1;
229
0
    return 0;
230
0
}
231
232
int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
233
                              int nid, int type,
234
                              const unsigned char *bytes, int len)
235
0
{
236
0
    if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
237
0
        return 1;
238
0
    return 0;
239
0
}
240
241
int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
242
                              const char *attrname, int type,
243
                              const unsigned char *bytes, int len)
244
0
{
245
0
    if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
246
0
        return 1;
247
0
    return 0;
248
0
}
249
250
const char *EVP_PKEY_get0_type_name(const EVP_PKEY *key)
251
0
{
252
0
#ifndef OPENSSL_NO_DEPRECATED_3_6
253
0
    const EVP_PKEY_ASN1_METHOD *ameth;
254
0
#endif
255
0
    const char *name = NULL;
256
257
0
    if (key->keymgmt != NULL)
258
0
        return EVP_KEYMGMT_get0_name(key->keymgmt);
259
260
0
#ifndef OPENSSL_NO_DEPRECATED_3_6
261
    /* Otherwise fallback to legacy */
262
0
    ameth = EVP_PKEY_get0_asn1(key);
263
0
    if (ameth != NULL)
264
0
        EVP_PKEY_asn1_get0_info(NULL, NULL,
265
0
                                NULL, NULL, &name, ameth);
266
0
#endif
267
268
0
    return name;
269
0
}
270
271
const OSSL_PROVIDER *EVP_PKEY_get0_provider(const EVP_PKEY *key)
272
0
{
273
0
    if (evp_pkey_is_provided(key))
274
0
        return EVP_KEYMGMT_get0_provider(key->keymgmt);
275
0
    return NULL;
276
0
}