Coverage Report

Created: 2025-08-28 06:41

/src/openssl/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
/*
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
0
{
39
0
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
40
0
    int ok;
41
42
0
    if (in == NULL)
43
0
        return 0;
44
0
    ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
45
46
0
    BIO_free(in);
47
0
    return ok;
48
0
}
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
0
{
65
0
    struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
66
67
0
    if (ctx != NULL)
68
0
        ctx->provctx = provctx;
69
0
    return ctx;
70
0
}
71
72
static void pem2der_freectx(void *vctx)
73
0
{
74
0
    struct pem2der_ctx_st *ctx = vctx;
75
76
0
    OPENSSL_free(ctx);
77
0
}
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
0
{
99
0
    const char *s;
100
101
0
    memset(r, 0, sizeof(*r));
102
0
    if (p != NULL)
103
0
        for (; (s = p->key) != NULL; p++)
104
0
            switch(s[0]) {
105
0
            default:
106
0
                break;
107
0
            case 'd':
108
0
                if (ossl_likely(strcmp("ata-structure", s + 1) == 0)) {
109
                    /* OBJECT_PARAM_DATA_STRUCTURE */
110
0
                    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
0
                    r->ds = (OSSL_PARAM *)p;
116
0
                }
117
0
                break;
118
0
            case 'p':
119
0
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
120
                    /* 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
0
            }
129
0
    return 1;
130
0
}
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
0
{
141
0
    struct pem2der_ctx_st *ctx = vctx;
142
0
    struct pem2der_set_ctx_params_st p;
143
0
    char *str;
144
145
0
    if (ctx == NULL || !pem2der_set_ctx_params_decoder(params, &p))
146
0
        return 0;
147
148
0
    str = ctx->propq;
149
0
    if (p.propq != NULL
150
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
151
0
        return 0;
152
153
0
    str = ctx->data_structure;
154
0
    if (p.ds != NULL
155
0
        && !OSSL_PARAM_get_utf8_string(p.ds, &str, sizeof(ctx->data_structure)))
156
0
        return 0;
157
158
0
    return 1;
159
0
}
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
0
{
169
0
    struct pem2der_pass_data_st *pass_data = data;
170
0
    size_t plen;
171
172
0
    if (pass_data == NULL
173
0
        || pass_data->cb == NULL
174
0
        || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
175
0
        return -1;
176
0
    return (int)plen;
177
0
}
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
0
{
183
    /*
184
     * PEM names we recognise.  Other PEM names should be recognised by
185
     * other decoder implementations.
186
     */
187
0
    static struct pem_name_map_st {
188
0
        const char *pem_name;
189
0
        int object_type;
190
0
        const char *data_type;
191
0
        const char *data_structure;
192
0
    } pem_name_map[] = {
193
        /* PKCS#8 and SubjectPublicKeyInfo */
194
0
        { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
195
0
        { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
196
0
#define PKCS8_LAST_IDX 1
197
0
        { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
198
0
#define SPKI_LAST_IDX 2
199
        /* Our set of type specific PEM types */
200
0
        { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
201
0
        { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
202
0
        { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
203
0
        { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
204
0
        { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
205
0
        { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
206
0
        { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
207
0
        { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
208
0
        { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
209
0
        { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
210
0
        { 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
0
        { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
218
0
        { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
219
0
        { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
220
0
        { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
221
0
    };
222
0
    struct pem2der_ctx_st *ctx = vctx;
223
0
    char *pem_name = NULL, *pem_header = NULL;
224
0
    size_t i;
225
0
    unsigned char *der = NULL;
226
0
    long der_len = 0;
227
0
    int ok = 0;
228
0
    int objtype = OSSL_OBJECT_UNKNOWN;
229
230
0
    ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
231
0
                  &der, &der_len) > 0;
232
    /* We return "empty handed".  This is not an error. */
233
0
    if (!ok)
234
0
        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
0
    if (strlen(pem_header) > 10) {
243
0
        EVP_CIPHER_INFO cipher;
244
0
        struct pem2der_pass_data_st pass_data;
245
246
0
        ok = 0;                  /* Assume that we fail */
247
0
        pass_data.cb = pw_cb;
248
0
        pass_data.cbarg = pw_cbarg;
249
0
        if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
250
0
            || !PEM_do_header(&cipher, der, &der_len,
251
0
                              pem2der_pass_helper, &pass_data))
252
0
            goto end;
253
0
    }
254
255
    /*
256
     * Indicated that we successfully decoded something, or not at all.
257
     * Ending up "empty handed" is not an error.
258
     */
259
0
    ok = 1;
260
261
    /* Have a look to see if we recognise anything */
262
0
    for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
263
0
        if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
264
0
            break;
265
266
0
    if (i < OSSL_NELEM(pem_name_map)) {
267
0
        OSSL_PARAM params[5], *p = params;
268
        /* We expect these to be read only so casting away the const is ok */
269
0
        char *data_type = (char *)pem_name_map[i].data_type;
270
0
        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
0
        if (i <= PKCS8_LAST_IDX
277
0
            && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
278
0
                || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
279
0
                || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
280
0
            ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
281
0
                                          data_cbarg, pw_cb, pw_cbarg,
282
0
                                          PROV_LIBCTX_OF(ctx->provctx),
283
0
                                          ctx->propq);
284
0
            goto end;
285
0
        }
286
287
0
        if (i <= SPKI_LAST_IDX
288
0
            && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
289
0
                || 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
0
        objtype = pem_name_map[i].object_type;
298
0
        if (data_type != NULL)
299
0
            *p++ =
300
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
301
0
                                                 data_type, 0);
302
303
        /* We expect this to be read only so casting away the const is ok */
304
0
        if (data_structure != NULL)
305
0
            *p++ =
306
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
307
0
                                                 data_structure, 0);
308
0
        *p++ =
309
0
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
310
0
                                              der, der_len);
311
0
        *p++ =
312
0
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
313
314
0
        *p = OSSL_PARAM_construct_end();
315
316
0
        ok = data_cb(params, data_cbarg);
317
0
    }
318
319
0
 end:
320
0
    OPENSSL_free(pem_name);
321
0
    OPENSSL_free(pem_header);
322
0
    OPENSSL_free(der);
323
0
    return ok;
324
0
}
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
};