Coverage Report

Created: 2025-08-28 06:41

/src/openssl/providers/implementations/encode_decode/encode_key2ms.c
Line
Count
Source (jump to first uncovered line)
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
/*
12
 * Low level APIs are deprecated for public use, but still ok for internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/core.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/params.h>
21
#include <openssl/err.h>
22
#include <openssl/proverr.h>
23
#include <openssl/pem.h>         /* Functions for writing MSBLOB and PVK */
24
#include <openssl/dsa.h>
25
#include "internal/cryptlib.h"
26
#include "internal/passphrase.h"
27
#include "crypto/rsa.h"
28
#include "prov/implementations.h"
29
#include "prov/bio.h"
30
#include "prov/provider_ctx.h"
31
#include "prov/endecoder_local.h"
32
33
struct key2ms_ctx_st {
34
    PROV_CTX *provctx;
35
36
    int pvk_encr_level;
37
38
    struct ossl_passphrase_data_st pwdata;
39
};
40
41
static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
42
                        EVP_PKEY *pkey, int ispub)
43
0
{
44
0
    BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
45
0
    int ret;
46
47
0
    if (out == NULL)
48
0
        return 0;
49
0
    ret = ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
50
51
0
    BIO_free(out);
52
0
    return ret;
53
0
}
54
55
static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
56
                     EVP_PKEY *pkey)
57
0
{
58
0
    BIO *out = NULL;
59
0
    int ret;
60
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
61
62
0
    out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
63
0
    if (out == NULL)
64
0
        return 0;
65
0
    ret = i2b_PVK_bio_ex(out, pkey, ctx->pvk_encr_level,
66
0
                         ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL);
67
0
    BIO_free(out);
68
0
    return ret;
69
0
}
70
71
static OSSL_FUNC_encoder_freectx_fn key2ms_freectx;
72
static OSSL_FUNC_encoder_does_selection_fn key2ms_does_selection;
73
74
static struct key2ms_ctx_st *key2ms_newctx(void *provctx)
75
0
{
76
0
    struct key2ms_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
77
78
0
    if (ctx != NULL) {
79
0
        ctx->provctx = provctx;
80
        /* This is the strongest encryption level */
81
0
        ctx->pvk_encr_level = 2;
82
0
    }
83
0
    return ctx;
84
0
}
85
86
static void key2ms_freectx(void *vctx)
87
0
{
88
0
    struct key2ms_ctx_st *ctx = vctx;
89
90
0
    ossl_pw_clear_passphrase_data(&ctx->pwdata);
91
0
    OPENSSL_free(ctx);
92
0
}
93
94
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
95
#ifndef key2pvk_set_ctx_params_list
96
static const OSSL_PARAM key2pvk_set_ctx_params_list[] = {
97
    OSSL_PARAM_int(OSSL_ENCODER_PARAM_ENCRYPT_LEVEL, NULL),
98
    OSSL_PARAM_END
99
};
100
#endif
101
102
#ifndef key2pvk_set_ctx_params_st
103
struct key2pvk_set_ctx_params_st {
104
    OSSL_PARAM *enclvl;
105
};
106
#endif
107
108
#ifndef key2pvk_set_ctx_params_decoder
109
static int key2pvk_set_ctx_params_decoder
110
    (const OSSL_PARAM *p, struct key2pvk_set_ctx_params_st *r)
111
0
{
112
0
    const char *s;
113
114
0
    memset(r, 0, sizeof(*r));
115
0
    if (p != NULL)
116
0
        for (; (s = p->key) != NULL; p++)
117
0
            if (ossl_likely(strcmp("encrypt-level", s + 0) == 0)) {
118
                /* ENCODER_PARAM_ENCRYPT_LEVEL */
119
0
                if (ossl_unlikely(r->enclvl != NULL)) {
120
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
121
0
                                   "param %s is repeated", s);
122
0
                    return 0;
123
0
                }
124
0
                r->enclvl = (OSSL_PARAM *)p;
125
0
            }
126
0
    return 1;
127
0
}
128
#endif
129
/* End of machine generated */
130
131
static const OSSL_PARAM *key2pvk_settable_ctx_params(ossl_unused void *provctx)
132
0
{
133
0
    return key2pvk_set_ctx_params_list;
134
0
}
135
136
static int key2pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
137
0
{
138
0
    struct key2ms_ctx_st *ctx = vctx;
139
0
    struct key2pvk_set_ctx_params_st p;
140
141
0
    if (ctx == NULL || !key2pvk_set_ctx_params_decoder(params, &p))
142
0
        return 0;
143
144
0
    if (p.enclvl != NULL && !OSSL_PARAM_get_int(p.enclvl, &ctx->pvk_encr_level))
145
0
        return 0;
146
0
    return 1;
147
0
}
148
149
static int key2ms_does_selection(void *vctx, int selection)
150
0
{
151
0
    return (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0;
152
0
}
153
154
/*
155
 * The real EVP_PKEY_set1_TYPE() functions take a non-const key, while the key
156
 * pointer in the encode function is a const pointer.  We violate the constness
157
 * knowingly, since we know that the key comes from the same provider, is never
158
 * actually const, and the implied reference count change is safe.
159
 *
160
 * EVP_PKEY_assign() can't be used, because there's no way to clear the internal
161
 * key using that function without freeing the existing internal key.
162
 */
163
typedef int evp_pkey_set1_fn(EVP_PKEY *, const void *key);
164
165
static int key2msblob_encode(void *vctx, const void *key, int selection,
166
                             OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
167
                             OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
168
0
{
169
0
    struct key2ms_ctx_st *ctx = vctx;
170
0
    int ispub = -1;
171
0
    EVP_PKEY *pkey = NULL;
172
0
    int ok = 0;
173
174
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
175
0
        ispub = 0;
176
0
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
177
0
        ispub = 1;
178
0
    else
179
0
        return 0;                /* Error */
180
181
0
    if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
182
0
        ok = write_msblob(ctx, cout, pkey, ispub);
183
0
    EVP_PKEY_free(pkey);
184
0
    return ok;
185
0
}
186
187
static int key2pvk_encode(void *vctx, const void *key, int selection,
188
                          OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
189
                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
190
0
{
191
0
    struct key2ms_ctx_st *ctx = vctx;
192
0
    EVP_PKEY *pkey = NULL;
193
0
    int ok = 0;
194
195
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
196
0
        return 0;                /* Error */
197
198
0
    if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key)
199
0
        && (pw_cb == NULL
200
0
            || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pw_cb, pw_cbarg)))
201
0
        ok = write_pvk(ctx, cout, pkey);
202
0
    EVP_PKEY_free(pkey);
203
0
    return ok;
204
0
}
205
206
0
#define dsa_set1        (evp_pkey_set1_fn *)EVP_PKEY_set1_DSA
207
0
#define rsa_set1        (evp_pkey_set1_fn *)EVP_PKEY_set1_RSA
208
209
#define msblob_set_params
210
#define pvk_set_params                                                        \
211
        { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS,                              \
212
          (void (*)(void))key2pvk_settable_ctx_params },                      \
213
        { OSSL_FUNC_ENCODER_SET_CTX_PARAMS,                                   \
214
          (void (*)(void))key2pvk_set_ctx_params },
215
216
#define MAKE_MS_ENCODER(impl, output, type)                                   \
217
    static OSSL_FUNC_encoder_import_object_fn                                 \
218
    impl##2##output##_import_object;                                          \
219
    static OSSL_FUNC_encoder_free_object_fn impl##2##output##_free_object;    \
220
    static OSSL_FUNC_encoder_encode_fn impl##2##output##_encode;              \
221
                                                                              \
222
    static void *                                                             \
223
    impl##2##output##_import_object(void *ctx, int selection,                 \
224
                                    const OSSL_PARAM params[])                \
225
0
    {                                                                         \
226
0
        return ossl_prov_import_key(ossl_##impl##_keymgmt_functions,          \
227
0
                                    ctx, selection, params);                  \
228
0
    }                                                                         \
Unexecuted instantiation: encode_key2ms.c:dsa2pvk_import_object
Unexecuted instantiation: encode_key2ms.c:dsa2msblob_import_object
Unexecuted instantiation: encode_key2ms.c:rsa2pvk_import_object
Unexecuted instantiation: encode_key2ms.c:rsa2msblob_import_object
229
    static void impl##2##output##_free_object(void *key)                      \
230
0
    {                                                                         \
231
0
        ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key);             \
232
0
    }                                                                         \
Unexecuted instantiation: encode_key2ms.c:dsa2pvk_free_object
Unexecuted instantiation: encode_key2ms.c:dsa2msblob_free_object
Unexecuted instantiation: encode_key2ms.c:rsa2pvk_free_object
Unexecuted instantiation: encode_key2ms.c:rsa2msblob_free_object
233
    static int impl##2##output##_encode(void *vctx, OSSL_CORE_BIO *cout,      \
234
                                        const void *key,                      \
235
                                        const OSSL_PARAM key_abstract[],      \
236
                                        int selection,                        \
237
                                        OSSL_PASSPHRASE_CALLBACK *cb,         \
238
                                        void *cbarg)                          \
239
0
    {                                                                         \
240
0
        /* We don't deal with abstract objects */                             \
241
0
        if (key_abstract != NULL) {                                           \
242
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);           \
243
0
            return 0;                                                         \
244
0
        }                                                                     \
245
0
        return key2##output##_encode(vctx, key, selection, cout, type##_set1, \
246
0
                                     cb, cbarg);                              \
247
0
    }                                                                         \
Unexecuted instantiation: encode_key2ms.c:dsa2pvk_encode
Unexecuted instantiation: encode_key2ms.c:dsa2msblob_encode
Unexecuted instantiation: encode_key2ms.c:rsa2pvk_encode
Unexecuted instantiation: encode_key2ms.c:rsa2msblob_encode
248
    const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = {   \
249
        { OSSL_FUNC_ENCODER_NEWCTX,                                           \
250
          (void (*)(void))key2ms_newctx },                                    \
251
        { OSSL_FUNC_ENCODER_FREECTX,                                          \
252
          (void (*)(void))key2ms_freectx },                                   \
253
        output##_set_params                                                   \
254
        { OSSL_FUNC_ENCODER_DOES_SELECTION,                                   \
255
          (void (*)(void))key2ms_does_selection },                            \
256
        { OSSL_FUNC_ENCODER_IMPORT_OBJECT,                                    \
257
          (void (*)(void))impl##2##output##_import_object },                  \
258
        { OSSL_FUNC_ENCODER_FREE_OBJECT,                                      \
259
          (void (*)(void))impl##2##output##_free_object },                    \
260
        { OSSL_FUNC_ENCODER_ENCODE,                                           \
261
          (void (*)(void))impl##2##output##_encode },                         \
262
        OSSL_DISPATCH_END                                                     \
263
    }
264
265
#ifndef OPENSSL_NO_DSA
266
MAKE_MS_ENCODER(dsa, pvk, dsa);
267
MAKE_MS_ENCODER(dsa, msblob, dsa);
268
#endif
269
270
MAKE_MS_ENCODER(rsa, pvk, rsa);
271
MAKE_MS_ENCODER(rsa, msblob, rsa);