Coverage Report

Created: 2025-11-11 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
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
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
0
{
86
0
    struct pem2der_ctx_st *ctx = vctx;
87
0
    struct pem2der_set_ctx_params_st p;
88
0
    char *str;
89
90
0
    if (ctx == NULL || !pem2der_set_ctx_params_decoder(params, &p))
91
0
        return 0;
92
93
0
    str = ctx->propq;
94
0
    if (p.propq != NULL
95
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
96
0
        return 0;
97
98
0
    str = ctx->data_structure;
99
0
    if (p.ds != NULL
100
0
        && !OSSL_PARAM_get_utf8_string(p.ds, &str, sizeof(ctx->data_structure)))
101
0
        return 0;
102
103
0
    return 1;
104
0
}
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
0
{
114
0
    struct pem2der_pass_data_st *pass_data = data;
115
0
    size_t plen;
116
117
0
    if (pass_data == NULL
118
0
        || pass_data->cb == NULL
119
0
        || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
120
0
        return -1;
121
0
    return (int)plen;
122
0
}
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
0
{
128
    /*
129
     * PEM names we recognise.  Other PEM names should be recognised by
130
     * other decoder implementations.
131
     */
132
0
    static struct pem_name_map_st {
133
0
        const char *pem_name;
134
0
        int object_type;
135
0
        const char *data_type;
136
0
        const char *data_structure;
137
0
    } pem_name_map[] = {
138
        /* PKCS#8 and SubjectPublicKeyInfo */
139
0
        { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
140
0
        { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
141
0
#define PKCS8_LAST_IDX 1
142
0
        { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
143
0
#define SPKI_LAST_IDX 2
144
        /* Our set of type specific PEM types */
145
0
        { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
146
0
        { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
147
0
        { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
148
0
        { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
149
0
        { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
150
0
        { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
151
0
        { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
152
0
        { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
153
0
        { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
154
0
        { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
155
0
        { 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
0
        { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
163
0
        { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
164
0
        { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
165
0
        { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
166
0
    };
167
0
    struct pem2der_ctx_st *ctx = vctx;
168
0
    char *pem_name = NULL, *pem_header = NULL;
169
0
    size_t i;
170
0
    unsigned char *der = NULL;
171
0
    long der_len = 0;
172
0
    int ok = 0;
173
0
    int objtype = OSSL_OBJECT_UNKNOWN;
174
175
0
    ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
176
0
                  &der, &der_len) > 0;
177
    /* We return "empty handed".  This is not an error. */
178
0
    if (!ok)
179
0
        return 1;
180
181
    /*
182
     * 10 is the number of characters in "Proc-Type:", which
183
     * PEM_get_EVP_CIPHER_INFO() requires to be present.
184
     * If the PEM header has less characters than that, it's
185
     * not worth spending cycles on it.
186
     */
187
0
    if (strlen(pem_header) > 10) {
188
0
        EVP_CIPHER_INFO cipher;
189
0
        struct pem2der_pass_data_st pass_data;
190
191
0
        ok = 0;                  /* Assume that we fail */
192
0
        pass_data.cb = pw_cb;
193
0
        pass_data.cbarg = pw_cbarg;
194
0
        if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
195
0
            || !PEM_do_header(&cipher, der, &der_len,
196
0
                              pem2der_pass_helper, &pass_data))
197
0
            goto end;
198
0
    }
199
200
    /*
201
     * Indicated that we successfully decoded something, or not at all.
202
     * Ending up "empty handed" is not an error.
203
     */
204
0
    ok = 1;
205
206
    /* Have a look to see if we recognise anything */
207
0
    for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
208
0
        if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
209
0
            break;
210
211
0
    if (i < OSSL_NELEM(pem_name_map)) {
212
0
        OSSL_PARAM params[5], *p = params;
213
        /* We expect these to be read only so casting away the const is ok */
214
0
        char *data_type = (char *)pem_name_map[i].data_type;
215
0
        char *data_structure = (char *)pem_name_map[i].data_structure;
216
217
        /*
218
         * Since this may perform decryption, we need to check the selection to
219
         * avoid password prompts for objects of no interest.
220
         */
221
0
        if (i <= PKCS8_LAST_IDX
222
0
            && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
223
0
                || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
224
0
                || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
225
0
            ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
226
0
                                          data_cbarg, pw_cb, pw_cbarg,
227
0
                                          PROV_LIBCTX_OF(ctx->provctx),
228
0
                                          ctx->propq);
229
0
            goto end;
230
0
        }
231
232
0
        if (i <= SPKI_LAST_IDX
233
0
            && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
234
0
                || OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {
235
0
            ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,
236
0
                                               data_cbarg, pw_cb, pw_cbarg,
237
0
                                               PROV_LIBCTX_OF(ctx->provctx),
238
0
                                               ctx->propq);
239
0
            goto end;
240
0
        }
241
242
0
        objtype = pem_name_map[i].object_type;
243
0
        if (data_type != NULL)
244
0
            *p++ =
245
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
246
0
                                                 data_type, 0);
247
248
        /* We expect this to be read only so casting away the const is ok */
249
0
        if (data_structure != NULL)
250
0
            *p++ =
251
0
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
252
0
                                                 data_structure, 0);
253
0
        *p++ =
254
0
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
255
0
                                              der, der_len);
256
0
        *p++ =
257
0
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
258
259
0
        *p = OSSL_PARAM_construct_end();
260
261
0
        ok = data_cb(params, data_cbarg);
262
0
    }
263
264
0
 end:
265
0
    OPENSSL_free(pem_name);
266
0
    OPENSSL_free(pem_header);
267
0
    OPENSSL_free(der);
268
0
    return ok;
269
0
}
270
271
const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
272
    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
273
    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
274
    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
275
    { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
276
      (void (*)(void))pem2der_settable_ctx_params },
277
    { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
278
      (void (*)(void))pem2der_set_ctx_params },
279
    OSSL_DISPATCH_END
280
};