Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/encode_decode/decode_epki2pki.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
#include <openssl/core.h>
14
#include <openssl/core_dispatch.h>
15
#include <openssl/core_names.h>
16
#include <openssl/core_object.h>
17
#include <openssl/asn1.h>
18
#include <openssl/err.h>
19
#include <openssl/objects.h>
20
#include <openssl/pkcs12.h>
21
#include <openssl/x509.h>
22
#include <openssl/proverr.h>
23
#include "internal/cryptlib.h"
24
#include "internal/asn1.h"
25
#include "internal/sizes.h"
26
#include "prov/bio.h"
27
#include "prov/decoders.h"
28
#include "prov/implementations.h"
29
#include "prov/endecoder_local.h"
30
31
static OSSL_FUNC_decoder_newctx_fn epki2pki_newctx;
32
static OSSL_FUNC_decoder_freectx_fn epki2pki_freectx;
33
static OSSL_FUNC_decoder_decode_fn epki2pki_decode;
34
static OSSL_FUNC_decoder_settable_ctx_params_fn epki2pki_settable_ctx_params;
35
static OSSL_FUNC_decoder_set_ctx_params_fn epki2pki_set_ctx_params;
36
37
/*
38
 * Context used for EncryptedPrivateKeyInfo to PrivateKeyInfo decoding.
39
 */
40
struct epki2pki_ctx_st {
41
    PROV_CTX *provctx;
42
    char propq[OSSL_MAX_PROPQUERY_SIZE];
43
};
44
45
static void *epki2pki_newctx(void *provctx)
46
1.30M
{
47
1.30M
    struct epki2pki_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
48
49
1.30M
    if (ctx != NULL)
50
1.30M
        ctx->provctx = provctx;
51
1.30M
    return ctx;
52
1.30M
}
53
54
static void epki2pki_freectx(void *vctx)
55
1.30M
{
56
1.30M
    struct epki2pki_ctx_st *ctx = vctx;
57
58
1.30M
    OPENSSL_free(ctx);
59
1.30M
}
60
61
/* clang-format off */
62
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
63
#ifndef epki2pki_set_ctx_params_list
64
static const OSSL_PARAM epki2pki_set_ctx_params_list[] = {
65
    OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
66
    OSSL_PARAM_END
67
};
68
#endif
69
70
#ifndef epki2pki_set_ctx_params_st
71
struct epki2pki_set_ctx_params_st {
72
    OSSL_PARAM *propq;
73
};
74
#endif
75
76
#ifndef epki2pki_set_ctx_params_decoder
77
static int epki2pki_set_ctx_params_decoder
78
    (const OSSL_PARAM *p, struct epki2pki_set_ctx_params_st *r)
79
429
{
80
429
    const char *s;
81
82
429
    memset(r, 0, sizeof(*r));
83
429
    if (p != NULL)
84
858
        for (; (s = p->key) != NULL; p++)
85
429
            if (ossl_likely(strcmp("properties", s + 0) == 0)) {
86
                /* OSSL_DECODER_PARAM_PROPERTIES */
87
0
                if (ossl_unlikely(r->propq != NULL)) {
88
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
89
0
                                   "param %s is repeated", s);
90
0
                    return 0;
91
0
                }
92
0
                r->propq = (OSSL_PARAM *)p;
93
0
            }
94
429
    return 1;
95
429
}
96
#endif
97
/* End of machine generated */
98
/* clang-format on */
99
100
static const OSSL_PARAM *epki2pki_settable_ctx_params(ossl_unused void *provctx)
101
0
{
102
0
    return epki2pki_set_ctx_params_list;
103
0
}
104
105
static int epki2pki_set_ctx_params(void *vctx, const OSSL_PARAM params[])
106
429
{
107
429
    struct epki2pki_ctx_st *ctx = vctx;
108
429
    struct epki2pki_set_ctx_params_st p;
109
429
    char *str;
110
111
429
    if (ctx == NULL || !epki2pki_set_ctx_params_decoder(params, &p))
112
0
        return 0;
113
114
429
    str = ctx->propq;
115
429
    if (p.propq != NULL
116
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
117
0
        return 0;
118
119
429
    return 1;
120
429
}
121
122
/*
123
 * The selection parameter in epki2pki_decode() is not used by this function
124
 * because it's not relevant just to decode EncryptedPrivateKeyInfo to
125
 * PrivateKeyInfo.
126
 */
127
static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
128
    OSSL_CALLBACK *data_cb, void *data_cbarg,
129
    OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
130
22.5k
{
131
22.5k
    struct epki2pki_ctx_st *ctx = vctx;
132
22.5k
    BUF_MEM *mem = NULL;
133
22.5k
    unsigned char *der = NULL;
134
22.5k
    long der_len = 0;
135
22.5k
    BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
136
22.5k
    int ok = 0;
137
138
22.5k
    if (in == NULL)
139
0
        return 0;
140
141
22.5k
    ok = (asn1_d2i_read_bio(in, &mem) >= 0);
142
22.5k
    BIO_free(in);
143
144
    /* We return "empty handed".  This is not an error. */
145
22.5k
    if (!ok)
146
2.21k
        return 1;
147
148
20.3k
    der = (unsigned char *)mem->data;
149
20.3k
    der_len = (long)mem->length;
150
20.3k
    OPENSSL_free(mem);
151
152
20.3k
    ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb, data_cbarg,
153
20.3k
        pw_cb, pw_cbarg, PROV_LIBCTX_OF(ctx->provctx),
154
20.3k
        ctx->propq);
155
20.3k
    OPENSSL_free(der);
156
20.3k
    return ok;
157
22.5k
}
158
159
int ossl_epki2pki_der_decode(unsigned char *der, long der_len, int selection,
160
    OSSL_CALLBACK *data_cb, void *data_cbarg,
161
    OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg,
162
    OSSL_LIB_CTX *libctx, const char *propq)
163
32.6k
{
164
32.6k
    const unsigned char *pder = der;
165
32.6k
    unsigned char *new_der = NULL;
166
32.6k
    X509_SIG *p8 = NULL;
167
32.6k
    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
168
32.6k
    const X509_ALGOR *alg = NULL;
169
32.6k
    int ok = 1; /* Assume good */
170
171
32.6k
    ERR_set_mark();
172
32.6k
    if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) {
173
128
        char pbuf[1024];
174
128
        size_t plen = 0;
175
176
128
        ERR_clear_last_mark();
177
178
128
        if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) {
179
128
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
180
128
            ok = 0;
181
128
        } else {
182
0
            const ASN1_OCTET_STRING *oct;
183
0
            int new_der_len = 0;
184
185
0
            X509_SIG_get0(p8, &alg, &oct);
186
0
            if (!PKCS12_pbe_crypt_ex(alg, pbuf, (int)plen,
187
0
                    oct->data, oct->length,
188
0
                    &new_der, &new_der_len, 0,
189
0
                    libctx, propq)) {
190
0
                ok = 0;
191
0
            } else {
192
0
                der = new_der;
193
0
                der_len = new_der_len;
194
0
            }
195
0
            alg = NULL;
196
0
        }
197
128
        X509_SIG_free(p8);
198
32.5k
    } else {
199
32.5k
        ERR_pop_to_mark();
200
32.5k
    }
201
202
32.6k
    ERR_set_mark();
203
32.6k
    pder = der;
204
32.6k
    p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pder, der_len);
205
32.6k
    ERR_pop_to_mark();
206
207
32.6k
    if (p8inf != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)) {
208
        /*
209
         * We have something and recognised it as PrivateKeyInfo, so let's
210
         * pass all the applicable data to the callback.
211
         */
212
13.3k
        char keytype[OSSL_MAX_NAME_SIZE];
213
13.3k
        OSSL_PARAM params[6], *p = params;
214
13.3k
        int objtype = OSSL_OBJECT_PKEY;
215
216
13.3k
        OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0);
217
218
13.3k
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
219
13.3k
            keytype, 0);
220
13.3k
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_INPUT_TYPE,
221
13.3k
            "DER", 0);
222
13.3k
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
223
13.3k
            "PrivateKeyInfo", 0);
224
13.3k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
225
13.3k
            der, der_len);
226
13.3k
        *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
227
13.3k
        *p = OSSL_PARAM_construct_end();
228
229
13.3k
        ok = data_cb(params, data_cbarg);
230
13.3k
    }
231
32.6k
    PKCS8_PRIV_KEY_INFO_free(p8inf);
232
32.6k
    OPENSSL_free(new_der);
233
32.6k
    return ok;
234
32.6k
}
235
236
const OSSL_DISPATCH ossl_EncryptedPrivateKeyInfo_der_to_der_decoder_functions[] = {
237
    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))epki2pki_newctx },
238
    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))epki2pki_freectx },
239
    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))epki2pki_decode },
240
    { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
241
        (void (*)(void))epki2pki_settable_ctx_params },
242
    { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
243
        (void (*)(void))epki2pki_set_ctx_params },
244
    OSSL_DISPATCH_END
245
};