Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/providers/implementations/encode_decode/decode_pem2der.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/core_object.h>
21
#include <openssl/crypto.h>
22
#include <openssl/err.h>
23
#include <openssl/params.h>
24
#include <openssl/pem.h>
25
#include <openssl/proverr.h>
26
#include "internal/cryptlib.h"
27
#include "internal/nelem.h"
28
#include "internal/sizes.h"
29
#include "prov/bio.h"
30
#include "prov/decoders.h"
31
#include "prov/implementations.h"
32
#include "prov/endecoder_local.h"
33
#include "providers/implementations/encode_decode/decode_pem2der.inc"
34
35
static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
36
    char **pem_name, char **pem_header,
37
    unsigned char **data, long *len)
38
170k
{
39
170k
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
40
170k
    int ok;
41
42
170k
    if (in == NULL)
43
0
        return 0;
44
170k
    ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
45
46
170k
    BIO_free(in);
47
170k
    return ok;
48
170k
}
49
50
static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;
51
static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;
52
static OSSL_FUNC_decoder_decode_fn pem2der_decode;
53
54
/*
55
 * Context used for PEM to DER decoding.
56
 */
57
struct pem2der_ctx_st {
58
    PROV_CTX *provctx;
59
    char data_structure[OSSL_MAX_CODEC_STRUCT_SIZE];
60
    char propq[OSSL_MAX_PROPQUERY_SIZE];
61
};
62
63
static void *pem2der_newctx(void *provctx)
64
1.31M
{
65
1.31M
    struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
66
67
1.31M
    if (ctx != NULL)
68
1.31M
        ctx->provctx = provctx;
69
1.31M
    return ctx;
70
1.31M
}
71
72
static void pem2der_freectx(void *vctx)
73
1.31M
{
74
1.31M
    struct pem2der_ctx_st *ctx = vctx;
75
76
1.31M
    OPENSSL_free(ctx);
77
1.31M
}
78
79
static const OSSL_PARAM *pem2der_settable_ctx_params(ossl_unused void *provctx)
80
0
{
81
0
    return pem2der_set_ctx_params_list;
82
0
}
83
84
static int pem2der_set_ctx_params(void *vctx, const OSSL_PARAM params[])
85
2.25k
{
86
2.25k
    struct pem2der_ctx_st *ctx = vctx;
87
2.25k
    struct pem2der_set_ctx_params_st p;
88
2.25k
    char *str;
89
90
2.25k
    if (ctx == NULL || !pem2der_set_ctx_params_decoder(params, &p))
91
0
        return 0;
92
93
2.25k
    str = ctx->propq;
94
2.25k
    if (p.propq != NULL
95
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
96
0
        return 0;
97
98
2.25k
    str = ctx->data_structure;
99
2.25k
    if (p.ds != NULL
100
2.25k
        && !OSSL_PARAM_get_utf8_string(p.ds, &str, sizeof(ctx->data_structure)))
101
0
        return 0;
102
103
2.25k
    return 1;
104
2.25k
}
105
106
/* pem_password_cb compatible function */
107
struct pem2der_pass_data_st {
108
    OSSL_PASSPHRASE_CALLBACK *cb;
109
    void *cbarg;
110
};
111
112
static int pem2der_pass_helper(char *buf, int num, int w, void *data)
113
6
{
114
6
    struct pem2der_pass_data_st *pass_data = data;
115
6
    size_t plen;
116
117
6
    if (pass_data == NULL
118
6
        || pass_data->cb == NULL
119
6
        || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
120
6
        return -1;
121
0
    return (int)plen;
122
6
}
123
124
static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
125
    OSSL_CALLBACK *data_cb, void *data_cbarg,
126
    OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
127
139k
{
128
    /*
129
     * PEM names we recognise.  Other PEM names should be recognised by
130
     * other decoder implementations.
131
     */
132
139k
    static const struct pem_name_map_st {
133
139k
        const char *pem_name;
134
139k
        int object_type;
135
139k
        const char *data_type;
136
139k
        const char *data_structure;
137
139k
    } pem_name_map[] = {
138
        /* PKCS#8 and SubjectPublicKeyInfo */
139
139k
        { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
140
139k
        { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
141
204k
#define PKCS8_LAST_IDX 1
142
139k
        { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
143
167k
#define SPKI_LAST_IDX 2
144
        /* Our set of type specific PEM types */
145
139k
        { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
146
139k
        { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
147
139k
        { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
148
139k
        { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
149
139k
        { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
150
139k
        { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
151
139k
        { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
152
139k
        { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
153
139k
        { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
154
139k
        { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
155
139k
        { PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
156
157
        /*
158
         * A few others that there is at least have an object type for, even
159
         * though there is no provider interface to handle such objects, yet.
160
         * However, this is beneficial for the OSSL_STORE result handler.
161
         */
162
139k
        { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
163
139k
        { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
164
139k
        { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
165
139k
        { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
166
139k
    };
167
139k
    struct pem2der_ctx_st *ctx = vctx;
168
139k
    char *pem_name = NULL, *pem_header = NULL;
169
139k
    size_t i;
170
139k
    unsigned char *der = NULL;
171
139k
    long der_len = 0;
172
139k
    int ok = 0;
173
139k
    int objtype = OSSL_OBJECT_UNKNOWN;
174
175
139k
    ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
176
139k
             &der, &der_len)
177
139k
        > 0;
178
    /* We return "empty handed".  This is not an error. */
179
139k
    if (!ok)
180
36.4k
        return 1;
181
182
    /*
183
     * 10 is the number of characters in "Proc-Type:", which
184
     * PEM_get_EVP_CIPHER_INFO() requires to be present.
185
     * If the PEM header has less characters than that, it's
186
     * not worth spending cycles on it.
187
     */
188
103k
    if (strlen(pem_header) > 10) {
189
525
        EVP_CIPHER_INFO cipher;
190
525
        struct pem2der_pass_data_st pass_data;
191
192
525
        ok = 0; /* Assume that we fail */
193
525
        pass_data.cb = pw_cb;
194
525
        pass_data.cbarg = pw_cbarg;
195
525
        if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
196
5
            || !PEM_do_header(&cipher, der, &der_len,
197
5
                pem2der_pass_helper, &pass_data))
198
525
            goto end;
199
525
    }
200
201
    /*
202
     * Indicated that we successfully decoded something, or not at all.
203
     * Ending up "empty handed" is not an error.
204
     */
205
102k
    ok = 1;
206
207
    /* Have a look to see if we recognise anything */
208
668k
    for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
209
668k
        if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
210
102k
            break;
211
212
102k
    if (i < OSSL_NELEM(pem_name_map)) {
213
102k
        OSSL_PARAM params[5], *p = params;
214
        /* We expect these to be read only so casting away the const is ok */
215
102k
        char *data_type = (char *)pem_name_map[i].data_type;
216
102k
        char *data_structure = (char *)pem_name_map[i].data_structure;
217
218
        /*
219
         * Since this may perform decryption, we need to check the selection to
220
         * avoid password prompts for objects of no interest.
221
         */
222
102k
        if (i <= PKCS8_LAST_IDX
223
18.7k
            && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
224
40
                || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
225
18.7k
                || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
226
18.7k
            ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
227
18.7k
                data_cbarg, pw_cb, pw_cbarg,
228
18.7k
                PROV_LIBCTX_OF(ctx->provctx),
229
18.7k
                ctx->propq);
230
18.7k
            goto end;
231
18.7k
        }
232
233
83.6k
        if (i <= SPKI_LAST_IDX
234
61
            && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
235
61
                || OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {
236
0
            ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,
237
0
                data_cbarg, pw_cb, pw_cbarg,
238
0
                PROV_LIBCTX_OF(ctx->provctx),
239
0
                ctx->propq);
240
0
            goto end;
241
0
        }
242
243
83.6k
        objtype = pem_name_map[i].object_type;
244
83.6k
        if (data_type != NULL)
245
83.5k
            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
246
83.5k
                data_type, 0);
247
248
        /* We expect this to be read only so casting away the const is ok */
249
83.6k
        if (data_structure != NULL)
250
83.6k
            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
251
83.6k
                data_structure, 0);
252
83.6k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
253
83.6k
            der, der_len);
254
83.6k
        *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
255
256
83.6k
        *p = OSSL_PARAM_construct_end();
257
258
83.6k
        ok = data_cb(params, data_cbarg);
259
83.6k
    }
260
261
103k
end:
262
103k
    OPENSSL_free(pem_name);
263
103k
    OPENSSL_free(pem_header);
264
103k
    OPENSSL_free(der);
265
103k
    return ok;
266
102k
}
267
268
const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
269
    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
270
    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
271
    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
272
    { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
273
        (void (*)(void))pem2der_settable_ctx_params },
274
    { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
275
        (void (*)(void))pem2der_set_ctx_params },
276
    OSSL_DISPATCH_END
277
};