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