/src/openssl/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 | 0 | { |
73 | 0 | struct pvk2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
74 | |
|
75 | 0 | if (ctx != NULL) { |
76 | 0 | ctx->provctx = provctx; |
77 | 0 | ctx->desc = desc; |
78 | 0 | } |
79 | 0 | return ctx; |
80 | 0 | } |
81 | | |
82 | | static void pvk2key_freectx(void *vctx) |
83 | 0 | { |
84 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
85 | |
|
86 | 0 | OPENSSL_free(ctx); |
87 | 0 | } |
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 | 0 | { |
113 | 0 | if (selection == 0) |
114 | 0 | return 1; |
115 | | |
116 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
117 | 0 | return 1; |
118 | | |
119 | 0 | return 0; |
120 | 0 | } |
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 | 0 | { |
126 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
127 | 0 | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
128 | 0 | void *key = NULL; |
129 | 0 | int ok = 0; |
130 | |
|
131 | 0 | if (in == NULL) |
132 | 0 | return 0; |
133 | | |
134 | 0 | ctx->selection = selection; |
135 | |
|
136 | 0 | if ((selection == 0 |
137 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
138 | 0 | && ctx->desc->read_private_key != NULL) { |
139 | 0 | struct ossl_passphrase_data_st pwdata; |
140 | 0 | int err, lib, reason; |
141 | |
|
142 | 0 | memset(&pwdata, 0, sizeof(pwdata)); |
143 | 0 | if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg)) |
144 | 0 | goto end; |
145 | | |
146 | 0 | key = ctx->desc->read_private_key(in, ossl_pw_pvk_password, &pwdata, |
147 | 0 | PROV_LIBCTX_OF(ctx->provctx), |
148 | 0 | 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 | 0 | err = ERR_peek_last_error(); |
157 | 0 | lib = ERR_GET_LIB(err); |
158 | 0 | reason = ERR_GET_REASON(err); |
159 | 0 | if (lib == ERR_LIB_PEM |
160 | 0 | && (reason == PEM_R_BAD_PASSWORD_READ |
161 | 0 | || reason == PEM_R_BAD_DECRYPT)) { |
162 | 0 | ERR_clear_last_mark(); |
163 | 0 | goto end; |
164 | 0 | } |
165 | | |
166 | 0 | if (selection != 0 && key == NULL) |
167 | 0 | goto next; |
168 | 0 | } |
169 | | |
170 | 0 | if (key != NULL && ctx->desc->adjust_key != NULL) |
171 | 0 | ctx->desc->adjust_key(key, ctx); |
172 | |
|
173 | 0 | next: |
174 | | /* |
175 | | * Indicated that we successfully decoded something, or not at all. |
176 | | * Ending up "empty handed" is not an error. |
177 | | */ |
178 | 0 | 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 | 0 | BIO_free(in); |
186 | 0 | in = NULL; |
187 | |
|
188 | 0 | if (key != NULL) { |
189 | 0 | OSSL_PARAM params[4]; |
190 | 0 | int object_type = OSSL_OBJECT_PKEY; |
191 | |
|
192 | 0 | params[0] = |
193 | 0 | OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); |
194 | 0 | params[1] = |
195 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
196 | 0 | (char *)ctx->desc->name, 0); |
197 | | /* The address of the key becomes the octet string */ |
198 | 0 | params[2] = |
199 | 0 | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, |
200 | 0 | &key, sizeof(key)); |
201 | 0 | params[3] = OSSL_PARAM_construct_end(); |
202 | |
|
203 | 0 | ok = data_cb(params, data_cbarg); |
204 | 0 | } |
205 | |
|
206 | 0 | end: |
207 | 0 | BIO_free(in); |
208 | 0 | ctx->desc->free_key(key); |
209 | |
|
210 | 0 | return ok; |
211 | 0 | } |
212 | | |
213 | | static int pvk2key_export_object(void *vctx, |
214 | | const void *reference, size_t reference_sz, |
215 | | OSSL_CALLBACK *export_cb, void *export_cbarg) |
216 | 0 | { |
217 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
218 | 0 | OSSL_FUNC_keymgmt_export_fn *export = |
219 | 0 | ossl_prov_get_keymgmt_export(ctx->desc->fns); |
220 | 0 | void *keydata; |
221 | |
|
222 | 0 | if (reference_sz == sizeof(keydata) && export != NULL) { |
223 | 0 | int selection = ctx->selection; |
224 | |
|
225 | 0 | if (selection == 0) |
226 | 0 | selection = OSSL_KEYMGMT_SELECT_ALL; |
227 | | /* The contents of the reference is the address to our object */ |
228 | 0 | keydata = *(void **)reference; |
229 | |
|
230 | 0 | return export(keydata, selection, export_cb, export_cbarg); |
231 | 0 | } |
232 | 0 | return 0; |
233 | 0 | } |
234 | | |
235 | | /* ---------------------------------------------------------------------- */ |
236 | | |
237 | | #define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex |
238 | | #define dsa_adjust NULL |
239 | | #define dsa_free (void (*)(void *))DSA_free |
240 | | |
241 | | /* ---------------------------------------------------------------------- */ |
242 | | |
243 | | #define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex |
244 | | |
245 | | static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx) |
246 | 0 | { |
247 | 0 | ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); |
248 | 0 | } |
249 | | |
250 | | #define rsa_free (void (*)(void *))RSA_free |
251 | | |
252 | | /* ---------------------------------------------------------------------- */ |
253 | | |
254 | | #define IMPLEMENT_MS(KEYTYPE, keytype) \ |
255 | | static const struct keytype_desc_st \ |
256 | | pvk2##keytype##_desc = { \ |
257 | | EVP_PKEY_##KEYTYPE, #KEYTYPE, \ |
258 | | ossl_##keytype##_keymgmt_functions, \ |
259 | | keytype##_private_key_bio, \ |
260 | | keytype##_adjust, \ |
261 | | keytype##_free \ |
262 | | }; \ |
263 | | static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx; \ |
264 | | static void *pvk2##keytype##_newctx(void *provctx) \ |
265 | 0 | { \ |
266 | 0 | return pvk2key_newctx(provctx, &pvk2##keytype##_desc); \ |
267 | 0 | } \ Unexecuted instantiation: decode_pvk2key.c:pvk2dsa_newctx Unexecuted instantiation: decode_pvk2key.c:pvk2rsa_newctx |
268 | | const OSSL_DISPATCH \ |
269 | | ossl_##pvk_to_##keytype##_decoder_functions[] = { \ |
270 | | { OSSL_FUNC_DECODER_NEWCTX, \ |
271 | | (void (*)(void))pvk2##keytype##_newctx }, \ |
272 | | { OSSL_FUNC_DECODER_FREECTX, \ |
273 | | (void (*)(void))pvk2key_freectx }, \ |
274 | | { OSSL_FUNC_DECODER_DOES_SELECTION, \ |
275 | | (void (*)(void))pvk2key_does_selection }, \ |
276 | | { OSSL_FUNC_DECODER_DECODE, \ |
277 | | (void (*)(void))pvk2key_decode }, \ |
278 | | { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ |
279 | | (void (*)(void))pvk2key_export_object }, \ |
280 | | { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \ |
281 | | (void (*)(void))pvk2key_settable_ctx_params }, \ |
282 | | { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \ |
283 | | (void (*)(void))pvk2key_set_ctx_params }, \ |
284 | | OSSL_DISPATCH_END \ |
285 | | } |
286 | | |
287 | | #ifndef OPENSSL_NO_DSA |
288 | | IMPLEMENT_MS(DSA, dsa); |
289 | | #endif |
290 | | IMPLEMENT_MS(RSA, rsa); |