Coverage Report

Created: 2025-12-31 06:58

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