Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/implementations/encode_decode/decode_msblob2key.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2023 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/pem.h> /* For public PVK functions */
24
#include <openssl/x509.h>
25
#include <openssl/err.h>
26
#include "internal/passphrase.h"
27
#include "crypto/pem.h" /* For internal PVK and "blob" headers */
28
#include "crypto/rsa.h"
29
#include "prov/bio.h"
30
#include "prov/implementations.h"
31
#include "endecoder_local.h"
32
33
struct msblob2key_ctx_st; /* Forward declaration */
34
typedef void *b2i_of_void_fn(const unsigned char **in, unsigned int bitlen,
35
    int ispub);
36
typedef void adjust_key_fn(void *, struct msblob2key_ctx_st *ctx);
37
typedef void free_key_fn(void *);
38
struct keytype_desc_st {
39
    int type; /* EVP key type */
40
    const char *name; /* Keytype */
41
    const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
42
43
    b2i_of_void_fn *read_private_key;
44
    b2i_of_void_fn *read_public_key;
45
    adjust_key_fn *adjust_key;
46
    free_key_fn *free_key;
47
};
48
49
static OSSL_FUNC_decoder_freectx_fn msblob2key_freectx;
50
static OSSL_FUNC_decoder_decode_fn msblob2key_decode;
51
static OSSL_FUNC_decoder_export_object_fn msblob2key_export_object;
52
53
/*
54
 * Context used for DER to key decoding.
55
 */
56
struct msblob2key_ctx_st {
57
    PROV_CTX *provctx;
58
    const struct keytype_desc_st *desc;
59
    /* The selection that is passed to msblob2key_decode() */
60
    int selection;
61
};
62
63
static struct msblob2key_ctx_st *
64
msblob2key_newctx(void *provctx, const struct keytype_desc_st *desc)
65
440k
{
66
440k
    struct msblob2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
67
68
440k
    if (ctx != NULL) {
69
440k
        ctx->provctx = provctx;
70
440k
        ctx->desc = desc;
71
440k
    }
72
440k
    return ctx;
73
440k
}
74
75
static void msblob2key_freectx(void *vctx)
76
440k
{
77
440k
    struct msblob2key_ctx_st *ctx = vctx;
78
79
440k
    OPENSSL_free(ctx);
80
440k
}
81
82
static int msblob2key_does_selection(void *provctx, int selection)
83
556k
{
84
556k
    if (selection == 0)
85
10
        return 1;
86
87
556k
    if ((selection & (OSSL_KEYMGMT_SELECT_PRIVATE_KEY | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) != 0)
88
556k
        return 1;
89
90
0
    return 0;
91
556k
}
92
93
static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
94
    OSSL_CALLBACK *data_cb, void *data_cbarg,
95
    OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
96
43.6k
{
97
43.6k
    struct msblob2key_ctx_st *ctx = vctx;
98
43.6k
    BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
99
43.6k
    const unsigned char *p;
100
43.6k
    unsigned char hdr_buf[16], *buf = NULL;
101
43.6k
    unsigned int bitlen, magic, length;
102
43.6k
    int isdss = -1;
103
43.6k
    int ispub = -1;
104
43.6k
    void *key = NULL;
105
43.6k
    int ok = 0;
106
107
43.6k
    if (in == NULL)
108
0
        return 0;
109
110
43.6k
    if (BIO_read(in, hdr_buf, 16) != 16) {
111
11.0k
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
112
11.0k
        goto next;
113
11.0k
    }
114
32.5k
    ERR_set_mark();
115
32.5k
    p = hdr_buf;
116
32.5k
    ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
117
32.5k
    ERR_pop_to_mark();
118
32.5k
    if (!ok)
119
24.0k
        goto next;
120
121
8.52k
    ctx->selection = selection;
122
8.52k
    ok = 0; /* Assume that we fail */
123
124
8.52k
    if ((isdss && ctx->desc->type != EVP_PKEY_DSA)
125
8.42k
        || (!isdss && ctx->desc->type != EVP_PKEY_RSA))
126
3.20k
        goto next;
127
128
5.32k
    length = ossl_blob_length(bitlen, isdss, ispub);
129
5.32k
    if (length > BLOB_MAX_LENGTH) {
130
70
        ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
131
70
        goto next;
132
70
    }
133
5.25k
    buf = OPENSSL_malloc(length);
134
5.25k
    if (buf == NULL) {
135
0
        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
136
0
        goto end;
137
0
    }
138
5.25k
    p = buf;
139
5.25k
    if (BIO_read(in, buf, length) != (int)length) {
140
119
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
141
119
        goto next;
142
119
    }
143
144
5.13k
    if ((selection == 0
145
0
            || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
146
5.13k
        && !ispub
147
2.33k
        && ctx->desc->read_private_key != NULL) {
148
2.33k
        struct ossl_passphrase_data_st pwdata;
149
150
2.33k
        memset(&pwdata, 0, sizeof(pwdata));
151
2.33k
        if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
152
0
            goto end;
153
2.33k
        p = buf;
154
2.33k
        key = ctx->desc->read_private_key(&p, bitlen, ispub);
155
2.33k
        if (selection != 0 && key == NULL)
156
0
            goto next;
157
2.33k
    }
158
5.13k
    if (key == NULL && (selection == 0 || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
159
2.80k
        && ispub
160
2.79k
        && ctx->desc->read_public_key != NULL) {
161
2.79k
        p = buf;
162
2.79k
        key = ctx->desc->read_public_key(&p, bitlen, ispub);
163
2.79k
        if (selection != 0 && key == NULL)
164
0
            goto next;
165
2.79k
    }
166
167
5.13k
    if (key != NULL && ctx->desc->adjust_key != NULL)
168
3.00k
        ctx->desc->adjust_key(key, ctx);
169
170
43.6k
next:
171
    /*
172
     * Indicated that we successfully decoded something, or not at all.
173
     * Ending up "empty handed" is not an error.
174
     */
175
43.6k
    ok = 1;
176
177
    /*
178
     * We free resources here so it's not held up during the callback, because
179
     * we know the process is recursive and the allocated chunks of memory
180
     * add up.
181
     */
182
43.6k
    OPENSSL_free(buf);
183
43.6k
    BIO_free(in);
184
43.6k
    buf = NULL;
185
43.6k
    in = NULL;
186
187
43.6k
    if (key != NULL) {
188
5.12k
        OSSL_PARAM params[4];
189
5.12k
        int object_type = OSSL_OBJECT_PKEY;
190
191
5.12k
        params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
192
5.12k
        params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
193
5.12k
            (char *)ctx->desc->name, 0);
194
        /* The address of the key becomes the octet string */
195
5.12k
        params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
196
5.12k
            &key, sizeof(key));
197
5.12k
        params[3] = OSSL_PARAM_construct_end();
198
199
5.12k
        ok = data_cb(params, data_cbarg);
200
5.12k
    }
201
202
43.6k
end:
203
43.6k
    BIO_free(in);
204
43.6k
    OPENSSL_free(buf);
205
43.6k
    ctx->desc->free_key(key);
206
207
43.6k
    return ok;
208
43.6k
}
209
210
static int
211
msblob2key_export_object(void *vctx,
212
    const void *reference, size_t reference_sz,
213
    OSSL_CALLBACK *export_cb, void *export_cbarg)
214
0
{
215
0
    struct msblob2key_ctx_st *ctx = vctx;
216
0
    OSSL_FUNC_keymgmt_export_fn *export = ossl_prov_get_keymgmt_export(ctx->desc->fns);
217
0
    void *keydata;
218
219
0
    if (reference_sz == sizeof(keydata) && export != NULL) {
220
0
        int selection = ctx->selection;
221
222
0
        if (selection == 0)
223
0
            selection = OSSL_KEYMGMT_SELECT_ALL;
224
        /* The contents of the reference is the address to our object */
225
0
        keydata = *(void **)reference;
226
227
0
        return export(keydata, selection, export_cb, export_cbarg);
228
0
    }
229
0
    return 0;
230
0
}
231
232
/* ---------------------------------------------------------------------- */
233
234
#define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
235
#define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header
236
#define dsa_adjust NULL
237
#define dsa_free (void (*)(void *)) DSA_free
238
239
/* ---------------------------------------------------------------------- */
240
241
#define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
242
#define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header
243
244
static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx)
245
3.00k
{
246
3.00k
    ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
247
3.00k
}
248
249
#define rsa_free (void (*)(void *)) RSA_free
250
251
/* ---------------------------------------------------------------------- */
252
253
#define IMPLEMENT_MSBLOB(KEYTYPE, keytype)                             \
254
    static const struct keytype_desc_st mstype##2##keytype##_desc = {  \
255
        EVP_PKEY_##KEYTYPE, #KEYTYPE,                                  \
256
        ossl_##keytype##_keymgmt_functions,                            \
257
        keytype##_decode_private_key,                                  \
258
        keytype##_decode_public_key,                                   \
259
        keytype##_adjust,                                              \
260
        keytype##_free                                                 \
261
    };                                                                 \
262
    static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx;      \
263
    static void *msblob2##keytype##_newctx(void *provctx)              \
264
440k
    {                                                                  \
265
440k
        return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
266
440k
    }                                                                  \
decode_msblob2key.c:msblob2dsa_newctx
Line
Count
Source
264
230k
    {                                                                  \
265
230k
        return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
266
230k
    }                                                                  \
decode_msblob2key.c:msblob2rsa_newctx
Line
Count
Source
264
209k
    {                                                                  \
265
209k
        return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \
266
209k
    }                                                                  \
267
    const OSSL_DISPATCH                                                \
268
        ossl_msblob_to_##keytype##_decoder_functions[]                 \
269
        = {                                                            \
270
              { OSSL_FUNC_DECODER_NEWCTX,                              \
271
                  (void (*)(void))msblob2##keytype##_newctx },         \
272
              { OSSL_FUNC_DECODER_FREECTX,                             \
273
                  (void (*)(void))msblob2key_freectx },                \
274
              { OSSL_FUNC_DECODER_DOES_SELECTION,                      \
275
                  (void (*)(void))msblob2key_does_selection },         \
276
              { OSSL_FUNC_DECODER_DECODE,                              \
277
                  (void (*)(void))msblob2key_decode },                 \
278
              { OSSL_FUNC_DECODER_EXPORT_OBJECT,                       \
279
                  (void (*)(void))msblob2key_export_object },          \
280
              { 0, NULL }                                              \
281
          }
282
283
#ifndef OPENSSL_NO_DSA
284
IMPLEMENT_MSBLOB(DSA, dsa);
285
#endif
286
IMPLEMENT_MSBLOB(RSA, rsa);