Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/providers/implementations/encode_decode/decode_pvk2key.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
 * 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/params.h>
23
#include <openssl/err.h>
24
#include <openssl/proverr.h>
25
#include <openssl/pem.h> /* For public PVK functions */
26
#include <openssl/x509.h>
27
#include "internal/cryptlib.h"
28
#include "internal/passphrase.h"
29
#include "internal/sizes.h"
30
#include "crypto/pem.h" /* For internal PVK and "blob" headers */
31
#include "crypto/rsa.h"
32
#include "prov/bio.h"
33
#include "prov/implementations.h"
34
#include "prov/endecoder_local.h"
35
#include "providers/implementations/encode_decode/decode_pvk2key.inc"
36
37
struct pvk2key_ctx_st; /* Forward declaration */
38
typedef int check_key_fn(void *, struct pvk2key_ctx_st *ctx);
39
typedef void adjust_key_fn(void *, struct pvk2key_ctx_st *ctx);
40
typedef void *b2i_PVK_of_bio_pw_fn(BIO *in, pem_password_cb *cb, void *u,
41
    OSSL_LIB_CTX *libctx, const char *propq);
42
typedef void free_key_fn(void *);
43
struct keytype_desc_st {
44
    int type; /* EVP key type */
45
    const char *name; /* Keytype */
46
    const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
47
48
    b2i_PVK_of_bio_pw_fn *read_private_key;
49
    adjust_key_fn *adjust_key;
50
    free_key_fn *free_key;
51
};
52
53
static OSSL_FUNC_decoder_freectx_fn pvk2key_freectx;
54
static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
55
static OSSL_FUNC_decoder_export_object_fn pvk2key_export_object;
56
static OSSL_FUNC_decoder_settable_ctx_params_fn pvk2key_settable_ctx_params;
57
static OSSL_FUNC_decoder_set_ctx_params_fn pvk2key_set_ctx_params;
58
59
/*
60
 * Context used for DER to key decoding.
61
 */
62
struct pvk2key_ctx_st {
63
    PROV_CTX *provctx;
64
    char propq[OSSL_MAX_PROPQUERY_SIZE];
65
    const struct keytype_desc_st *desc;
66
    /* The selection that is passed to der2key_decode() */
67
    int selection;
68
};
69
70
static struct pvk2key_ctx_st *
71
pvk2key_newctx(void *provctx, const struct keytype_desc_st *desc)
72
126k
{
73
126k
    struct pvk2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
74
75
126k
    if (ctx != NULL) {
76
126k
        ctx->provctx = provctx;
77
126k
        ctx->desc = desc;
78
126k
    }
79
126k
    return ctx;
80
126k
}
81
82
static void pvk2key_freectx(void *vctx)
83
126k
{
84
126k
    struct pvk2key_ctx_st *ctx = vctx;
85
86
126k
    OPENSSL_free(ctx);
87
126k
}
88
89
static const OSSL_PARAM *pvk2key_settable_ctx_params(ossl_unused void *provctx)
90
0
{
91
0
    return pvk2key_set_ctx_params_list;
92
0
}
93
94
static int pvk2key_set_ctx_params(void *vctx, const OSSL_PARAM params[])
95
0
{
96
0
    struct pvk2key_ctx_st *ctx = vctx;
97
0
    struct pvk2key_set_ctx_params_st p;
98
0
    char *str;
99
100
0
    if (ctx == NULL || !pvk2key_set_ctx_params_decoder(params, &p))
101
0
        return 0;
102
103
0
    str = ctx->propq;
104
0
    if (p.propq != NULL
105
0
        && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
106
0
        return 0;
107
108
0
    return 1;
109
0
}
110
111
static int pvk2key_does_selection(void *provctx, int selection)
112
34.9k
{
113
34.9k
    if (selection == 0)
114
8
        return 1;
115
116
34.9k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
117
344
        return 1;
118
119
34.6k
    return 0;
120
34.9k
}
121
122
static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
123
    OSSL_CALLBACK *data_cb, void *data_cbarg,
124
    OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
125
32.8k
{
126
32.8k
    struct pvk2key_ctx_st *ctx = vctx;
127
32.8k
    BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
128
32.8k
    void *key = NULL;
129
32.8k
    int ok = 0;
130
131
32.8k
    if (in == NULL)
132
0
        return 0;
133
134
32.8k
    ctx->selection = selection;
135
136
32.8k
    if ((selection == 0
137
0
            || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
138
32.8k
        && ctx->desc->read_private_key != NULL) {
139
32.8k
        struct ossl_passphrase_data_st pwdata;
140
32.8k
        int err, lib, reason;
141
142
32.8k
        memset(&pwdata, 0, sizeof(pwdata));
143
32.8k
        if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
144
0
            goto end;
145
146
32.8k
        key = ctx->desc->read_private_key(in, ossl_pw_pvk_password, &pwdata,
147
32.8k
            PROV_LIBCTX_OF(ctx->provctx),
148
32.8k
            ctx->propq);
149
150
        /*
151
         * Because the PVK API doesn't have a separate decrypt call, we need
152
         * to check the error queue for certain well known errors that are
153
         * considered fatal and which we pass through, while the rest gets
154
         * thrown away.
155
         */
156
32.8k
        err = ERR_peek_last_error();
157
32.8k
        lib = ERR_GET_LIB(err);
158
32.8k
        reason = ERR_GET_REASON(err);
159
32.8k
        if (lib == ERR_LIB_PEM
160
32.6k
            && (reason == PEM_R_BAD_PASSWORD_READ
161
32.6k
                || reason == PEM_R_BAD_DECRYPT)) {
162
10
            ERR_clear_last_mark();
163
10
            goto end;
164
10
        }
165
166
32.8k
        if (selection != 0 && key == NULL)
167
0
            goto next;
168
32.8k
    }
169
170
32.8k
    if (key != NULL && ctx->desc->adjust_key != NULL)
171
8
        ctx->desc->adjust_key(key, ctx);
172
173
32.8k
next:
174
    /*
175
     * Indicated that we successfully decoded something, or not at all.
176
     * Ending up "empty handed" is not an error.
177
     */
178
32.8k
    ok = 1;
179
180
    /*
181
     * We free resources here so it's not held up during the callback, because
182
     * we know the process is recursive and the allocated chunks of memory
183
     * add up.
184
     */
185
32.8k
    BIO_free(in);
186
32.8k
    in = NULL;
187
188
32.8k
    if (key != NULL) {
189
15
        OSSL_PARAM params[4];
190
15
        int object_type = OSSL_OBJECT_PKEY;
191
192
15
        params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
193
15
        params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
194
15
            (char *)ctx->desc->name, 0);
195
        /* The address of the key becomes the octet string */
196
15
        params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
197
15
            &key, sizeof(key));
198
15
        params[3] = OSSL_PARAM_construct_end();
199
200
15
        ok = data_cb(params, data_cbarg);
201
15
    }
202
203
32.8k
end:
204
32.8k
    BIO_free(in);
205
32.8k
    ctx->desc->free_key(key);
206
207
32.8k
    return ok;
208
32.8k
}
209
210
static int pvk2key_export_object(void *vctx,
211
    const void *reference, size_t reference_sz,
212
    OSSL_CALLBACK *export_cb, void *export_cbarg)
213
0
{
214
0
    struct pvk2key_ctx_st *ctx = vctx;
215
0
    OSSL_FUNC_keymgmt_export_fn *export = ossl_prov_get_keymgmt_export(ctx->desc->fns);
216
0
    void *keydata;
217
218
0
    if (reference_sz == sizeof(keydata) && export != NULL) {
219
0
        int selection = ctx->selection;
220
221
0
        if (selection == 0)
222
0
            selection = OSSL_KEYMGMT_SELECT_ALL;
223
        /* The contents of the reference is the address to our object */
224
0
        keydata = *(void **)reference;
225
226
0
        return export(keydata, selection, export_cb, export_cbarg);
227
0
    }
228
0
    return 0;
229
0
}
230
231
/* ---------------------------------------------------------------------- */
232
233
#define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex
234
#define dsa_adjust NULL
235
#define dsa_free (void (*)(void *)) DSA_free
236
237
/* ---------------------------------------------------------------------- */
238
239
#define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex
240
241
static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx)
242
8
{
243
8
    ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
244
8
}
245
246
#define rsa_free (void (*)(void *)) RSA_free
247
248
/* ---------------------------------------------------------------------- */
249
250
#define IMPLEMENT_MS(KEYTYPE, keytype)                           \
251
    static const struct keytype_desc_st                          \
252
        pvk2##keytype##_desc                                     \
253
        = {                                                      \
254
              EVP_PKEY_##KEYTYPE, #KEYTYPE,                      \
255
              ossl_##keytype##_keymgmt_functions,                \
256
              keytype##_private_key_bio,                         \
257
              keytype##_adjust,                                  \
258
              keytype##_free                                     \
259
          };                                                     \
260
    static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx;   \
261
    static void *pvk2##keytype##_newctx(void *provctx)           \
262
126k
    {                                                            \
263
126k
        return pvk2key_newctx(provctx, &pvk2##keytype##_desc);   \
264
126k
    }                                                            \
decode_pvk2key.c:pvk2dsa_newctx
Line
Count
Source
262
63.6k
    {                                                            \
263
63.6k
        return pvk2key_newctx(provctx, &pvk2##keytype##_desc);   \
264
63.6k
    }                                                            \
decode_pvk2key.c:pvk2rsa_newctx
Line
Count
Source
262
63.1k
    {                                                            \
263
63.1k
        return pvk2key_newctx(provctx, &pvk2##keytype##_desc);   \
264
63.1k
    }                                                            \
265
    const OSSL_DISPATCH                                          \
266
        ossl_##pvk_to_##keytype##_decoder_functions[]            \
267
        = {                                                      \
268
              { OSSL_FUNC_DECODER_NEWCTX,                        \
269
                  (void (*)(void))pvk2##keytype##_newctx },      \
270
              { OSSL_FUNC_DECODER_FREECTX,                       \
271
                  (void (*)(void))pvk2key_freectx },             \
272
              { OSSL_FUNC_DECODER_DOES_SELECTION,                \
273
                  (void (*)(void))pvk2key_does_selection },      \
274
              { OSSL_FUNC_DECODER_DECODE,                        \
275
                  (void (*)(void))pvk2key_decode },              \
276
              { OSSL_FUNC_DECODER_EXPORT_OBJECT,                 \
277
                  (void (*)(void))pvk2key_export_object },       \
278
              { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,           \
279
                  (void (*)(void))pvk2key_settable_ctx_params }, \
280
              { OSSL_FUNC_DECODER_SET_CTX_PARAMS,                \
281
                  (void (*)(void))pvk2key_set_ctx_params },      \
282
              OSSL_DISPATCH_END                                  \
283
          }
284
285
#ifndef OPENSSL_NO_DSA
286
IMPLEMENT_MS(DSA, dsa);
287
#endif
288
IMPLEMENT_MS(RSA, rsa);