Coverage Report

Created: 2025-08-28 07:07

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