Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/encode_decode/encoder_pkey.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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
#include <openssl/err.h>
11
#include <openssl/ui.h>
12
#include <openssl/params.h>
13
#include <openssl/encoder.h>
14
#include <openssl/core_names.h>
15
#include <openssl/provider.h>
16
#include <openssl/safestack.h>
17
#include <openssl/trace.h>
18
#include "internal/provider.h"
19
#include "internal/property.h"
20
#include "crypto/evp.h"
21
#include "encoder_local.h"
22
23
DEFINE_STACK_OF(OSSL_ENCODER)
24
25
int OSSL_ENCODER_CTX_set_cipher(OSSL_ENCODER_CTX *ctx,
26
    const char *cipher_name,
27
    const char *propquery)
28
0
{
29
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
30
31
0
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_CIPHER,
32
0
        (void *)cipher_name, 0);
33
0
    params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES,
34
0
        (void *)propquery, 0);
35
36
0
    return OSSL_ENCODER_CTX_set_params(ctx, params);
37
0
}
38
39
int OSSL_ENCODER_CTX_set_passphrase(OSSL_ENCODER_CTX *ctx,
40
    const unsigned char *kstr,
41
    size_t klen)
42
0
{
43
0
    return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
44
0
}
45
46
int OSSL_ENCODER_CTX_set_passphrase_ui(OSSL_ENCODER_CTX *ctx,
47
    const UI_METHOD *ui_method,
48
    void *ui_data)
49
0
{
50
0
    return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
51
0
}
52
53
int OSSL_ENCODER_CTX_set_pem_password_cb(OSSL_ENCODER_CTX *ctx,
54
    pem_password_cb *cb, void *cbarg)
55
0
{
56
0
    return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
57
0
}
58
59
int OSSL_ENCODER_CTX_set_passphrase_cb(OSSL_ENCODER_CTX *ctx,
60
    OSSL_PASSPHRASE_CALLBACK *cb,
61
    void *cbarg)
62
0
{
63
0
    return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
64
0
}
65
66
/*
67
 * Support for OSSL_ENCODER_CTX_new_for_type:
68
 * finding a suitable encoder
69
 */
70
71
struct collected_encoder_st {
72
    STACK_OF(OPENSSL_CSTRING) *names;
73
    const char *output_structure;
74
    const char *output_type;
75
76
    const OSSL_PROVIDER *keymgmt_prov;
77
    OSSL_ENCODER_CTX *ctx;
78
    unsigned int flag_find_same_provider : 1;
79
80
    int error_occurred;
81
};
82
83
static void collect_encoder(OSSL_ENCODER *encoder, void *arg)
84
1.20M
{
85
1.20M
    struct collected_encoder_st *data = arg;
86
1.20M
    size_t i, end_i;
87
88
1.20M
    if (data->error_occurred)
89
0
        return;
90
91
1.20M
    data->error_occurred = 1; /* Assume the worst */
92
93
1.20M
    if (data->names == NULL)
94
0
        return;
95
96
1.20M
    end_i = sk_OPENSSL_CSTRING_num(data->names);
97
7.31M
    for (i = 0; i < end_i; i++) {
98
6.15M
        const char *name = sk_OPENSSL_CSTRING_value(data->names, i);
99
6.15M
        const OSSL_PROVIDER *prov = OSSL_ENCODER_get0_provider(encoder);
100
6.15M
        void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
101
102
        /*
103
         * collect_encoder() is called in two passes, one where the encoders
104
         * from the same provider as the keymgmt are looked up, and one where
105
         * the other encoders are looked up.  |data->flag_find_same_provider|
106
         * tells us which pass we're in.
107
         */
108
6.15M
        if ((data->keymgmt_prov == prov) != data->flag_find_same_provider)
109
3.19M
            continue;
110
111
2.95M
        if (!OSSL_ENCODER_is_a(encoder, name)
112
139k
            || (encoder->does_selection != NULL
113
134k
                && !encoder->does_selection(provctx, data->ctx->selection))
114
52.0k
            || (data->keymgmt_prov != prov
115
0
                && encoder->import_object == NULL))
116
2.90M
            continue;
117
118
        /* Only add each encoder implementation once */
119
52.0k
        if (OSSL_ENCODER_CTX_add_encoder(data->ctx, encoder))
120
52.0k
            break;
121
52.0k
    }
122
123
1.20M
    data->error_occurred = 0; /* All is good now */
124
1.20M
}
125
126
struct collected_names_st {
127
    STACK_OF(OPENSSL_CSTRING) *names;
128
    unsigned int error_occurred : 1;
129
};
130
131
static void collect_name(const char *name, void *arg)
132
396k
{
133
396k
    struct collected_names_st *data = arg;
134
135
396k
    if (data->error_occurred)
136
0
        return;
137
138
396k
    data->error_occurred = 1; /* Assume the worst */
139
140
396k
    if (sk_OPENSSL_CSTRING_push(data->names, name) <= 0)
141
0
        return;
142
143
396k
    data->error_occurred = 0; /* All is good now */
144
396k
}
145
146
/*
147
 * Support for OSSL_ENCODER_to_bio:
148
 * writing callback for the OSSL_PARAM (the implementation doesn't have
149
 * intimate knowledge of the provider side object)
150
 */
151
152
struct construct_data_st {
153
    const EVP_PKEY *pk;
154
    int selection;
155
156
    OSSL_ENCODER_INSTANCE *encoder_inst;
157
    const void *obj;
158
    void *constructed_obj;
159
};
160
161
static int encoder_import_cb(const OSSL_PARAM params[], void *arg)
162
0
{
163
0
    struct construct_data_st *construct_data = arg;
164
0
    OSSL_ENCODER_INSTANCE *encoder_inst = construct_data->encoder_inst;
165
0
    OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
166
0
    void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
167
168
0
    construct_data->constructed_obj = encoder->import_object(encoderctx, construct_data->selection, params);
169
170
0
    return (construct_data->constructed_obj != NULL);
171
0
}
172
173
static const void *
174
encoder_construct_pkey(OSSL_ENCODER_INSTANCE *encoder_inst, void *arg)
175
91.5k
{
176
91.5k
    struct construct_data_st *data = arg;
177
178
91.5k
    if (data->obj == NULL) {
179
91.5k
        OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
180
91.5k
        const EVP_PKEY *pk = data->pk;
181
91.5k
        const OSSL_PROVIDER *k_prov = EVP_KEYMGMT_get0_provider(pk->keymgmt);
182
91.5k
        const OSSL_PROVIDER *e_prov = OSSL_ENCODER_get0_provider(encoder);
183
184
91.5k
        if (k_prov != e_prov) {
185
0
            int selection = data->selection;
186
187
0
            if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
188
0
                selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
189
0
            data->encoder_inst = encoder_inst;
190
191
0
            if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, selection,
192
0
                    &encoder_import_cb, data))
193
0
                return NULL;
194
0
            data->obj = data->constructed_obj;
195
91.5k
        } else {
196
91.5k
            data->obj = pk->keydata;
197
91.5k
        }
198
91.5k
    }
199
200
91.5k
    return data->obj;
201
91.5k
}
202
203
static void encoder_destruct_pkey(void *arg)
204
91.5k
{
205
91.5k
    struct construct_data_st *data = arg;
206
91.5k
    int match = (data->obj == data->constructed_obj);
207
208
91.5k
    if (data->encoder_inst != NULL) {
209
0
        OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->encoder_inst);
210
211
0
        encoder->free_object(data->constructed_obj);
212
0
    }
213
91.5k
    data->constructed_obj = NULL;
214
91.5k
    if (match)
215
0
        data->obj = NULL;
216
91.5k
}
217
218
/*
219
 * OSSL_ENCODER_CTX_new_for_pkey() returns a ctx with no encoder if
220
 * it couldn't find a suitable encoder.  This allows a caller to detect if
221
 * a suitable encoder was found, with OSSL_ENCODER_CTX_get_num_encoder(),
222
 * and to use fallback methods if the result is NULL.
223
 */
224
static int ossl_encoder_ctx_setup_for_pkey(OSSL_ENCODER_CTX *ctx,
225
    const EVP_PKEY *pkey,
226
    int selection,
227
    const char *propquery)
228
5.34k
{
229
5.34k
    struct construct_data_st *data = NULL;
230
5.34k
    const OSSL_PROVIDER *prov = NULL;
231
5.34k
    OSSL_LIB_CTX *libctx = NULL;
232
5.34k
    int ok = 0;
233
234
5.34k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(pkey != NULL)) {
235
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
236
0
        return 0;
237
0
    }
238
239
5.34k
    if (evp_pkey_is_provided(pkey)) {
240
5.25k
        prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
241
5.25k
        libctx = ossl_provider_libctx(prov);
242
5.25k
    }
243
244
5.34k
    if (pkey->keymgmt != NULL) {
245
5.25k
        struct collected_encoder_st encoder_data;
246
5.25k
        struct collected_names_st keymgmt_data;
247
248
5.25k
        if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL) {
249
0
            ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
250
0
            goto err;
251
0
        }
252
253
        /*
254
         * Select the first encoder implementations in two steps.
255
         * First, collect the keymgmt names, then the encoders that match.
256
         */
257
5.25k
        keymgmt_data.names = sk_OPENSSL_CSTRING_new_null();
258
5.25k
        if (keymgmt_data.names == NULL) {
259
0
            ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
260
0
            goto err;
261
0
        }
262
263
5.25k
        keymgmt_data.error_occurred = 0;
264
5.25k
        EVP_KEYMGMT_names_do_all(pkey->keymgmt, collect_name, &keymgmt_data);
265
5.25k
        if (keymgmt_data.error_occurred) {
266
0
            sk_OPENSSL_CSTRING_free(keymgmt_data.names);
267
0
            goto err;
268
0
        }
269
270
5.25k
        encoder_data.names = keymgmt_data.names;
271
5.25k
        encoder_data.output_type = ctx->output_type;
272
5.25k
        encoder_data.output_structure = ctx->output_structure;
273
5.25k
        encoder_data.error_occurred = 0;
274
5.25k
        encoder_data.keymgmt_prov = prov;
275
5.25k
        encoder_data.ctx = ctx;
276
277
        /*
278
         * Place the encoders with the a different provider as the keymgmt
279
         * last (the chain is processed in reverse order)
280
         */
281
5.25k
        encoder_data.flag_find_same_provider = 0;
282
5.25k
        OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data);
283
284
        /*
285
         * Place the encoders with the same provider as the keymgmt first
286
         * (the chain is processed in reverse order)
287
         */
288
5.25k
        encoder_data.flag_find_same_provider = 1;
289
5.25k
        OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data);
290
291
5.25k
        sk_OPENSSL_CSTRING_free(keymgmt_data.names);
292
5.25k
        if (encoder_data.error_occurred) {
293
0
            ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
294
0
            goto err;
295
0
        }
296
5.25k
    }
297
298
5.34k
    if (data != NULL && OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) {
299
5.25k
        if (!OSSL_ENCODER_CTX_set_construct(ctx, encoder_construct_pkey)
300
5.25k
            || !OSSL_ENCODER_CTX_set_construct_data(ctx, data)
301
5.25k
            || !OSSL_ENCODER_CTX_set_cleanup(ctx, encoder_destruct_pkey))
302
0
            goto err;
303
304
5.25k
        data->pk = pkey;
305
5.25k
        data->selection = selection;
306
307
5.25k
        data = NULL; /* Avoid it being freed */
308
5.25k
    }
309
310
5.34k
    ok = 1;
311
5.34k
err:
312
5.34k
    if (data != NULL) {
313
0
        OSSL_ENCODER_CTX_set_construct_data(ctx, NULL);
314
0
        OPENSSL_free(data);
315
0
    }
316
5.34k
    return ok;
317
5.34k
}
318
319
OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new_for_pkey(const EVP_PKEY *pkey,
320
    int selection,
321
    const char *output_type,
322
    const char *output_struct,
323
    const char *propquery)
324
93.3k
{
325
93.3k
    OSSL_ENCODER_CTX *ctx = NULL;
326
93.3k
    OSSL_LIB_CTX *libctx = NULL;
327
328
93.3k
    if (pkey == NULL) {
329
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
330
0
        return NULL;
331
0
    }
332
333
93.3k
    if (!evp_pkey_is_assigned(pkey)) {
334
0
        ERR_raise_data(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT,
335
0
            "The passed EVP_PKEY must be assigned a key");
336
0
        return NULL;
337
0
    }
338
339
93.3k
    if ((ctx = OSSL_ENCODER_CTX_new()) == NULL) {
340
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
341
0
        return NULL;
342
0
    }
343
344
93.3k
    if (evp_pkey_is_provided(pkey)) {
345
91.8k
        const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
346
347
91.8k
        libctx = ossl_provider_libctx(prov);
348
91.8k
    }
349
350
93.3k
    OSSL_TRACE_BEGIN(ENCODER)
351
0
    {
352
0
        BIO_printf(trc_out,
353
0
            "(ctx %p) Looking for %s encoders with selection %d\n",
354
0
            (void *)ctx, EVP_PKEY_get0_type_name(pkey), selection);
355
0
        BIO_printf(trc_out, "    output type: %s, output structure: %s\n",
356
0
            output_type, output_struct);
357
0
    }
358
93.3k
    OSSL_TRACE_END(ENCODER);
359
360
93.3k
    if (OSSL_ENCODER_CTX_set_output_type(ctx, output_type)
361
93.3k
        && (output_struct == NULL
362
11.3k
            || OSSL_ENCODER_CTX_set_output_structure(ctx, output_struct))
363
93.3k
        && OSSL_ENCODER_CTX_set_selection(ctx, selection)
364
93.3k
        && ossl_encoder_ctx_setup_for_pkey(ctx, pkey, selection, propquery)
365
93.3k
        && OSSL_ENCODER_CTX_add_extra(ctx, libctx, propquery)) {
366
93.3k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
367
93.3k
        int save_parameters = pkey->save_parameters;
368
369
93.3k
        params[0] = OSSL_PARAM_construct_int(OSSL_ENCODER_PARAM_SAVE_PARAMETERS,
370
93.3k
            &save_parameters);
371
        /* ignoring error as this is only auxiliary parameter */
372
93.3k
        (void)OSSL_ENCODER_CTX_set_params(ctx, params);
373
374
93.3k
        OSSL_TRACE_BEGIN(ENCODER)
375
0
        {
376
0
            BIO_printf(trc_out, "(ctx %p) Got %d encoders\n",
377
0
                (void *)ctx, OSSL_ENCODER_CTX_get_num_encoders(ctx));
378
0
        }
379
93.3k
        OSSL_TRACE_END(ENCODER);
380
93.3k
        return ctx;
381
93.3k
    }
382
383
0
    OSSL_ENCODER_CTX_free(ctx);
384
    return NULL;
385
93.3k
}