Coverage Report

Created: 2026-09-12 06:55

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
131k
{
39
131k
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
40
131k
    int ok;
41
42
131k
    if (in == NULL)
43
0
        return 0;
44
131k
    ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
45
46
131k
    BIO_free(in);
47
131k
    return ok;
48
131k
}
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.06M
{
65
1.06M
    struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
66
67
1.06M
    if (ctx != NULL)
68
1.06M
        ctx->provctx = provctx;
69
1.06M
    return ctx;
70
1.06M
}
71
72
static void pem2der_freectx(void *vctx)
73
1.06M
{
74
1.06M
    struct pem2der_ctx_st *ctx = vctx;
75
76
1.06M
    OPENSSL_free(ctx);
77
1.06M
}
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
1.58k
{
86
1.58k
    struct pem2der_ctx_st *ctx = vctx;
87
1.58k
    struct pem2der_set_ctx_params_st p;
88
1.58k
    char *str;
89
90
1.58k
    if (ctx == NULL || !pem2der_set_ctx_params_decoder(params, &p))
91
0
        return 0;
92
93
1.58k
    str = ctx->propq;
94
1.58k
    if (p.propq != NULL
95
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
96
0
        return 0;
97
98
1.58k
    str = ctx->data_structure;
99
1.58k
    if (p.ds != NULL
100
1.58k
        && !OSSL_PARAM_get_utf8_string(p.ds, &str, sizeof(ctx->data_structure)))
101
0
        return 0;
102
103
1.58k
    return 1;
104
1.58k
}
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
4
{
114
4
    struct pem2der_pass_data_st *pass_data = data;
115
4
    size_t plen;
116
117
4
    if (pass_data == NULL
118
4
        || pass_data->cb == NULL
119
4
        || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
120
4
        return -1;
121
0
    return (int)plen;
122
4
}
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
102k
{
128
    /*
129
     * PEM names we recognise.  Other PEM names should be recognised by
130
     * other decoder implementations.
131
     */
132
102k
    static const struct pem_name_map_st {
133
102k
        const char *pem_name;
134
102k
        int object_type;
135
102k
        const char *data_type;
136
102k
        const char *data_structure;
137
102k
    } pem_name_map[] = {
138
        /* PKCS#8 and SubjectPublicKeyInfo */
139
102k
        { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
140
102k
        { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
141
160k
#define PKCS8_LAST_IDX 1
142
102k
        { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
143
131k
#define SPKI_LAST_IDX 2
144
        /* Our set of type specific PEM types */
145
102k
        { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
146
102k
        { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
147
102k
        { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
148
102k
        { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
149
102k
        { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
150
102k
        { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
151
102k
        { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
152
102k
        { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
153
102k
        { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
154
102k
        { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
155
102k
        { 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
102k
        { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
163
102k
        { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
164
102k
        { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
165
102k
        { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
166
102k
    };
167
102k
    struct pem2der_ctx_st *ctx = vctx;
168
102k
    char *pem_name = NULL, *pem_header = NULL;
169
102k
    size_t i;
170
102k
    unsigned char *der = NULL;
171
102k
    long der_len = 0;
172
102k
    int ok = 0;
173
102k
    int objtype = OSSL_OBJECT_UNKNOWN;
174
175
102k
    ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
176
102k
             &der, &der_len)
177
102k
        > 0;
178
    /* We return "empty handed".  This is not an error. */
179
102k
    if (!ok)
180
21.5k
        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
80.7k
    if (strlen(pem_header) > 10) {
189
468
        EVP_CIPHER_INFO cipher;
190
468
        struct pem2der_pass_data_st pass_data;
191
192
468
        ok = 0; /* Assume that we fail */
193
468
        pass_data.cb = pw_cb;
194
468
        pass_data.cbarg = pw_cbarg;
195
468
        if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
196
3
            || !PEM_do_header(&cipher, der, &der_len,
197
3
                pem2der_pass_helper, &pass_data))
198
468
            goto end;
199
468
    }
200
201
    /*
202
     * Indicated that we successfully decoded something, or not at all.
203
     * Ending up "empty handed" is not an error.
204
     */
205
80.3k
    ok = 1;
206
207
    /* Have a look to see if we recognise anything */
208
524k
    for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
209
524k
        if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
210
80.1k
            break;
211
212
80.3k
    if (i < OSSL_NELEM(pem_name_map)) {
213
80.1k
        OSSL_PARAM params[5], *p = params;
214
        /* We expect these to be read only so casting away the const is ok */
215
80.1k
        char *data_type = (char *)pem_name_map[i].data_type;
216
80.1k
        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
80.1k
        if (i <= PKCS8_LAST_IDX
223
14.4k
            && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
224
39
                || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
225
14.4k
                || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
226
14.4k
            ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
227
14.4k
                data_cbarg, pw_cb, pw_cbarg,
228
14.4k
                PROV_LIBCTX_OF(ctx->provctx),
229
14.4k
                ctx->propq);
230
14.4k
            goto end;
231
14.4k
        }
232
233
65.7k
        if (i <= SPKI_LAST_IDX
234
57
            && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
235
57
                || 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
65.7k
        objtype = pem_name_map[i].object_type;
244
65.7k
        if (data_type != NULL)
245
65.7k
            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
246
65.7k
                data_type, 0);
247
248
        /* We expect this to be read only so casting away the const is ok */
249
65.7k
        if (data_structure != NULL)
250
65.7k
            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
251
65.7k
                data_structure, 0);
252
65.7k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
253
65.7k
            der, der_len);
254
65.7k
        *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
255
256
65.7k
        *p = OSSL_PARAM_construct_end();
257
258
65.7k
        ok = data_cb(params, data_cbarg);
259
65.7k
    }
260
261
80.7k
end:
262
80.7k
    OPENSSL_free(pem_name);
263
80.7k
    OPENSSL_free(pem_header);
264
80.7k
    OPENSSL_free(der);
265
80.7k
    return ok;
266
80.3k
}
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
};