Coverage Report

Created: 2025-06-13 06:58

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