Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
/*
12
 * RSA low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <string.h>
18
19
#include <openssl/core_dispatch.h>
20
#include <openssl/core_names.h>
21
#include <openssl/core_object.h>
22
#include <openssl/crypto.h>
23
#include <openssl/err.h>
24
#include <openssl/params.h>
25
#include <openssl/pem.h>
26
#include <openssl/proverr.h>
27
#include "internal/cryptlib.h"
28
#include "internal/nelem.h"
29
#include "internal/sizes.h"
30
#include "prov/bio.h"
31
#include "prov/decoders.h"
32
#include "prov/implementations.h"
33
#include "prov/endecoder_local.h"
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
163k
{
39
163k
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
40
163k
    int ok;
41
42
163k
    if (in == NULL)
43
0
        return 0;
44
163k
    ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
45
46
163k
    BIO_free(in);
47
163k
    return ok;
48
163k
}
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
2.47M
{
65
2.47M
    struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
66
67
2.47M
    if (ctx != NULL)
68
2.47M
        ctx->provctx = provctx;
69
2.47M
    return ctx;
70
2.47M
}
71
72
static void pem2der_freectx(void *vctx)
73
2.47M
{
74
2.47M
    struct pem2der_ctx_st *ctx = vctx;
75
76
2.47M
    OPENSSL_free(ctx);
77
2.47M
}
78
79
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
80
#ifndef pem2der_set_ctx_params_list
81
static const OSSL_PARAM pem2der_set_ctx_params_list[] = {
82
    OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
83
    OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, NULL, 0),
84
    OSSL_PARAM_END
85
};
86
#endif
87
88
#ifndef pem2der_set_ctx_params_st
89
struct pem2der_set_ctx_params_st {
90
    OSSL_PARAM *ds;
91
    OSSL_PARAM *propq;
92
};
93
#endif
94
95
#ifndef pem2der_set_ctx_params_decoder
96
static int pem2der_set_ctx_params_decoder
97
    (const OSSL_PARAM *p, struct pem2der_set_ctx_params_st *r)
98
850
{
99
850
    const char *s;
100
101
850
    memset(r, 0, sizeof(*r));
102
850
    if (p != NULL)
103
1.70k
        for (; (s = p->key) != NULL; p++)
104
850
            switch(s[0]) {
105
0
            default:
106
0
                break;
107
850
            case 'd':
108
850
                if (ossl_likely(strcmp("ata-structure", s + 1) == 0)) {
109
                    /* OSSL_OBJECT_PARAM_DATA_STRUCTURE */
110
850
                    if (ossl_unlikely(r->ds != NULL)) {
111
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
112
0
                                       "param %s is repeated", s);
113
0
                        return 0;
114
0
                    }
115
850
                    r->ds = (OSSL_PARAM *)p;
116
850
                }
117
850
                break;
118
850
            case 'p':
119
0
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
120
                    /* OSSL_DECODER_PARAM_PROPERTIES */
121
0
                    if (ossl_unlikely(r->propq != NULL)) {
122
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
123
0
                                       "param %s is repeated", s);
124
0
                        return 0;
125
0
                    }
126
0
                    r->propq = (OSSL_PARAM *)p;
127
0
                }
128
850
            }
129
850
    return 1;
130
850
}
131
#endif
132
/* End of machine generated */
133
134
static const OSSL_PARAM *pem2der_settable_ctx_params(ossl_unused void *provctx)
135
0
{
136
0
    return pem2der_set_ctx_params_list;
137
0
}
138
139
static int pem2der_set_ctx_params(void *vctx, const OSSL_PARAM params[])
140
850
{
141
850
    struct pem2der_ctx_st *ctx = vctx;
142
850
    struct pem2der_set_ctx_params_st p;
143
850
    char *str;
144
145
850
    if (ctx == NULL || !pem2der_set_ctx_params_decoder(params, &p))
146
0
        return 0;
147
148
850
    str = ctx->propq;
149
850
    if (p.propq != NULL
150
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
151
0
        return 0;
152
153
850
    str = ctx->data_structure;
154
850
    if (p.ds != NULL
155
850
        && !OSSL_PARAM_get_utf8_string(p.ds, &str, sizeof(ctx->data_structure)))
156
0
        return 0;
157
158
850
    return 1;
159
850
}
160
161
/* pem_password_cb compatible function */
162
struct pem2der_pass_data_st {
163
    OSSL_PASSPHRASE_CALLBACK *cb;
164
    void *cbarg;
165
};
166
167
static int pem2der_pass_helper(char *buf, int num, int w, void *data)
168
2
{
169
2
    struct pem2der_pass_data_st *pass_data = data;
170
2
    size_t plen;
171
172
2
    if (pass_data == NULL
173
2
        || pass_data->cb == NULL
174
2
        || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
175
2
        return -1;
176
0
    return (int)plen;
177
2
}
178
179
static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
180
                          OSSL_CALLBACK *data_cb, void *data_cbarg,
181
                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
182
78.2k
{
183
    /*
184
     * PEM names we recognise.  Other PEM names should be recognised by
185
     * other decoder implementations.
186
     */
187
78.2k
    static struct pem_name_map_st {
188
78.2k
        const char *pem_name;
189
78.2k
        int object_type;
190
78.2k
        const char *data_type;
191
78.2k
        const char *data_structure;
192
78.2k
    } pem_name_map[] = {
193
        /* PKCS#8 and SubjectPublicKeyInfo */
194
78.2k
        { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
195
78.2k
        { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
196
114k
#define PKCS8_LAST_IDX 1
197
78.2k
        { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
198
93.7k
#define SPKI_LAST_IDX 2
199
        /* Our set of type specific PEM types */
200
78.2k
        { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
201
78.2k
        { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
202
78.2k
        { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
203
78.2k
        { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
204
78.2k
        { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
205
78.2k
        { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
206
78.2k
        { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
207
78.2k
        { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
208
78.2k
        { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
209
78.2k
        { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
210
78.2k
        { PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
211
212
        /*
213
         * A few others that there is at least have an object type for, even
214
         * though there is no provider interface to handle such objects, yet.
215
         * However, this is beneficial for the OSSL_STORE result handler.
216
         */
217
78.2k
        { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
218
78.2k
        { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
219
78.2k
        { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
220
78.2k
        { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
221
78.2k
    };
222
78.2k
    struct pem2der_ctx_st *ctx = vctx;
223
78.2k
    char *pem_name = NULL, *pem_header = NULL;
224
78.2k
    size_t i;
225
78.2k
    unsigned char *der = NULL;
226
78.2k
    long der_len = 0;
227
78.2k
    int ok = 0;
228
78.2k
    int objtype = OSSL_OBJECT_UNKNOWN;
229
230
78.2k
    ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
231
78.2k
                  &der, &der_len) > 0;
232
    /* We return "empty handed".  This is not an error. */
233
78.2k
    if (!ok)
234
20.7k
        return 1;
235
236
    /*
237
     * 10 is the number of characters in "Proc-Type:", which
238
     * PEM_get_EVP_CIPHER_INFO() requires to be present.
239
     * If the PEM header has less characters than that, it's
240
     * not worth spending cycles on it.
241
     */
242
57.4k
    if (strlen(pem_header) > 10) {
243
323
        EVP_CIPHER_INFO cipher;
244
323
        struct pem2der_pass_data_st pass_data;
245
246
323
        ok = 0;                  /* Assume that we fail */
247
323
        pass_data.cb = pw_cb;
248
323
        pass_data.cbarg = pw_cbarg;
249
323
        if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
250
2
            || !PEM_do_header(&cipher, der, &der_len,
251
2
                              pem2der_pass_helper, &pass_data))
252
323
            goto end;
253
323
    }
254
255
    /*
256
     * Indicated that we successfully decoded something, or not at all.
257
     * Ending up "empty handed" is not an error.
258
     */
259
57.1k
    ok = 1;
260
261
    /* Have a look to see if we recognise anything */
262
374k
    for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
263
374k
        if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
264
57.0k
            break;
265
266
57.1k
    if (i < OSSL_NELEM(pem_name_map)) {
267
57.0k
        OSSL_PARAM params[5], *p = params;
268
        /* We expect these to be read only so casting away the const is ok */
269
57.0k
        char *data_type = (char *)pem_name_map[i].data_type;
270
57.0k
        char *data_structure = (char *)pem_name_map[i].data_structure;
271
272
        /*
273
         * Since this may perform decryption, we need to check the selection to
274
         * avoid password prompts for objects of no interest.
275
         */
276
57.0k
        if (i <= PKCS8_LAST_IDX
277
10.1k
            && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
278
7
                || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
279
10.1k
                || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
280
10.1k
            ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
281
10.1k
                                          data_cbarg, pw_cb, pw_cbarg,
282
10.1k
                                          PROV_LIBCTX_OF(ctx->provctx),
283
10.1k
                                          ctx->propq);
284
10.1k
            goto end;
285
10.1k
        }
286
287
46.8k
        if (i <= SPKI_LAST_IDX
288
17
            && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
289
17
                || OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {
290
0
            ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,
291
0
                                               data_cbarg, pw_cb, pw_cbarg,
292
0
                                               PROV_LIBCTX_OF(ctx->provctx),
293
0
                                               ctx->propq);
294
0
            goto end;
295
0
        }
296
297
46.8k
        objtype = pem_name_map[i].object_type;
298
46.8k
        if (data_type != NULL)
299
46.8k
            *p++ =
300
46.8k
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
301
46.8k
                                                 data_type, 0);
302
303
        /* We expect this to be read only so casting away the const is ok */
304
46.8k
        if (data_structure != NULL)
305
46.8k
            *p++ =
306
46.8k
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
307
46.8k
                                                 data_structure, 0);
308
46.8k
        *p++ =
309
46.8k
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
310
46.8k
                                              der, der_len);
311
46.8k
        *p++ =
312
46.8k
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
313
314
46.8k
        *p = OSSL_PARAM_construct_end();
315
316
46.8k
        ok = data_cb(params, data_cbarg);
317
46.8k
    }
318
319
57.4k
 end:
320
57.4k
    OPENSSL_free(pem_name);
321
57.4k
    OPENSSL_free(pem_header);
322
57.4k
    OPENSSL_free(der);
323
57.4k
    return ok;
324
57.1k
}
325
326
const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
327
    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
328
    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
329
    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
330
    { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
331
      (void (*)(void))pem2der_settable_ctx_params },
332
    { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
333
      (void (*)(void))pem2der_set_ctx_params },
334
    OSSL_DISPATCH_END
335
};