/src/openssl/providers/implementations/encode_decode/decode_pvk2key.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 |
13 | | * internal use. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <string.h> |
18 | | |
19 | | #include <openssl/core_dispatch.h> |
20 | | #include <openssl/core_names.h> |
21 | | #include <openssl/core_object.h> |
22 | | #include <openssl/crypto.h> |
23 | | #include <openssl/params.h> |
24 | | #include <openssl/err.h> |
25 | | #include <openssl/proverr.h> |
26 | | #include <openssl/pem.h> /* For public PVK functions */ |
27 | | #include <openssl/x509.h> |
28 | | #include "internal/cryptlib.h" |
29 | | #include "internal/passphrase.h" |
30 | | #include "internal/sizes.h" |
31 | | #include "crypto/pem.h" /* For internal PVK and "blob" headers */ |
32 | | #include "crypto/rsa.h" |
33 | | #include "prov/bio.h" |
34 | | #include "prov/implementations.h" |
35 | | #include "prov/endecoder_local.h" |
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 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
90 | | #ifndef pvk2key_set_ctx_params_list |
91 | | static const OSSL_PARAM pvk2key_set_ctx_params_list[] = { |
92 | | OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0), |
93 | | OSSL_PARAM_END |
94 | | }; |
95 | | #endif |
96 | | |
97 | | #ifndef pvk2key_set_ctx_params_st |
98 | | struct pvk2key_set_ctx_params_st { |
99 | | OSSL_PARAM *propq; |
100 | | }; |
101 | | #endif |
102 | | |
103 | | #ifndef pvk2key_set_ctx_params_decoder |
104 | | static int pvk2key_set_ctx_params_decoder |
105 | | (const OSSL_PARAM *p, struct pvk2key_set_ctx_params_st *r) |
106 | 0 | { |
107 | 0 | const char *s; |
108 | |
|
109 | 0 | memset(r, 0, sizeof(*r)); |
110 | 0 | if (p != NULL) |
111 | 0 | for (; (s = p->key) != NULL; p++) |
112 | 0 | if (ossl_likely(strcmp("properties", s + 0) == 0)) { |
113 | | /* DECODER_PARAM_PROPERTIES */ |
114 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
115 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
116 | 0 | "param %s is repeated", s); |
117 | 0 | return 0; |
118 | 0 | } |
119 | 0 | r->propq = (OSSL_PARAM *)p; |
120 | 0 | } |
121 | 0 | return 1; |
122 | 0 | } |
123 | | #endif |
124 | | /* End of machine generated */ |
125 | | |
126 | | static const OSSL_PARAM *pvk2key_settable_ctx_params(ossl_unused void *provctx) |
127 | 0 | { |
128 | 0 | return pvk2key_set_ctx_params_list; |
129 | 0 | } |
130 | | |
131 | | static int pvk2key_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
132 | 0 | { |
133 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
134 | 0 | struct pvk2key_set_ctx_params_st p; |
135 | 0 | char *str; |
136 | |
|
137 | 0 | if (ctx == NULL || !pvk2key_set_ctx_params_decoder(params, &p)) |
138 | 0 | return 0; |
139 | | |
140 | 0 | str = ctx->propq; |
141 | 0 | if (p.propq != NULL |
142 | 0 | && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq))) |
143 | 0 | return 0; |
144 | | |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | | static int pvk2key_does_selection(void *provctx, int selection) |
149 | 0 | { |
150 | 0 | if (selection == 0) |
151 | 0 | return 1; |
152 | | |
153 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
154 | 0 | return 1; |
155 | | |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, |
160 | | OSSL_CALLBACK *data_cb, void *data_cbarg, |
161 | | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
162 | 0 | { |
163 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
164 | 0 | BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); |
165 | 0 | void *key = NULL; |
166 | 0 | int ok = 0; |
167 | |
|
168 | 0 | if (in == NULL) |
169 | 0 | return 0; |
170 | | |
171 | 0 | ctx->selection = selection; |
172 | |
|
173 | 0 | if ((selection == 0 |
174 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
175 | 0 | && ctx->desc->read_private_key != NULL) { |
176 | 0 | struct ossl_passphrase_data_st pwdata; |
177 | 0 | int err, lib, reason; |
178 | |
|
179 | 0 | memset(&pwdata, 0, sizeof(pwdata)); |
180 | 0 | if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg)) |
181 | 0 | goto end; |
182 | | |
183 | 0 | key = ctx->desc->read_private_key(in, ossl_pw_pvk_password, &pwdata, |
184 | 0 | PROV_LIBCTX_OF(ctx->provctx), |
185 | 0 | ctx->propq); |
186 | | |
187 | | /* |
188 | | * Because the PVK API doesn't have a separate decrypt call, we need |
189 | | * to check the error queue for certain well known errors that are |
190 | | * considered fatal and which we pass through, while the rest gets |
191 | | * thrown away. |
192 | | */ |
193 | 0 | err = ERR_peek_last_error(); |
194 | 0 | lib = ERR_GET_LIB(err); |
195 | 0 | reason = ERR_GET_REASON(err); |
196 | 0 | if (lib == ERR_LIB_PEM |
197 | 0 | && (reason == PEM_R_BAD_PASSWORD_READ |
198 | 0 | || reason == PEM_R_BAD_DECRYPT)) { |
199 | 0 | ERR_clear_last_mark(); |
200 | 0 | goto end; |
201 | 0 | } |
202 | | |
203 | 0 | if (selection != 0 && key == NULL) |
204 | 0 | goto next; |
205 | 0 | } |
206 | | |
207 | 0 | if (key != NULL && ctx->desc->adjust_key != NULL) |
208 | 0 | ctx->desc->adjust_key(key, ctx); |
209 | |
|
210 | 0 | next: |
211 | | /* |
212 | | * Indicated that we successfully decoded something, or not at all. |
213 | | * Ending up "empty handed" is not an error. |
214 | | */ |
215 | 0 | ok = 1; |
216 | | |
217 | | /* |
218 | | * We free resources here so it's not held up during the callback, because |
219 | | * we know the process is recursive and the allocated chunks of memory |
220 | | * add up. |
221 | | */ |
222 | 0 | BIO_free(in); |
223 | 0 | in = NULL; |
224 | |
|
225 | 0 | if (key != NULL) { |
226 | 0 | OSSL_PARAM params[4]; |
227 | 0 | int object_type = OSSL_OBJECT_PKEY; |
228 | |
|
229 | 0 | params[0] = |
230 | 0 | OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); |
231 | 0 | params[1] = |
232 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, |
233 | 0 | (char *)ctx->desc->name, 0); |
234 | | /* The address of the key becomes the octet string */ |
235 | 0 | params[2] = |
236 | 0 | OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, |
237 | 0 | &key, sizeof(key)); |
238 | 0 | params[3] = OSSL_PARAM_construct_end(); |
239 | |
|
240 | 0 | ok = data_cb(params, data_cbarg); |
241 | 0 | } |
242 | |
|
243 | 0 | end: |
244 | 0 | BIO_free(in); |
245 | 0 | ctx->desc->free_key(key); |
246 | |
|
247 | 0 | return ok; |
248 | 0 | } |
249 | | |
250 | | static int pvk2key_export_object(void *vctx, |
251 | | const void *reference, size_t reference_sz, |
252 | | OSSL_CALLBACK *export_cb, void *export_cbarg) |
253 | 0 | { |
254 | 0 | struct pvk2key_ctx_st *ctx = vctx; |
255 | 0 | OSSL_FUNC_keymgmt_export_fn *export = |
256 | 0 | ossl_prov_get_keymgmt_export(ctx->desc->fns); |
257 | 0 | void *keydata; |
258 | |
|
259 | 0 | if (reference_sz == sizeof(keydata) && export != NULL) { |
260 | 0 | int selection = ctx->selection; |
261 | |
|
262 | 0 | if (selection == 0) |
263 | 0 | selection = OSSL_KEYMGMT_SELECT_ALL; |
264 | | /* The contents of the reference is the address to our object */ |
265 | 0 | keydata = *(void **)reference; |
266 | |
|
267 | 0 | return export(keydata, selection, export_cb, export_cbarg); |
268 | 0 | } |
269 | 0 | return 0; |
270 | 0 | } |
271 | | |
272 | | /* ---------------------------------------------------------------------- */ |
273 | | |
274 | | #define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex |
275 | | #define dsa_adjust NULL |
276 | | #define dsa_free (void (*)(void *))DSA_free |
277 | | |
278 | | /* ---------------------------------------------------------------------- */ |
279 | | |
280 | | #define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex |
281 | | |
282 | | static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx) |
283 | 0 | { |
284 | 0 | ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); |
285 | 0 | } |
286 | | |
287 | | #define rsa_free (void (*)(void *))RSA_free |
288 | | |
289 | | /* ---------------------------------------------------------------------- */ |
290 | | |
291 | | #define IMPLEMENT_MS(KEYTYPE, keytype) \ |
292 | | static const struct keytype_desc_st \ |
293 | | pvk2##keytype##_desc = { \ |
294 | | EVP_PKEY_##KEYTYPE, #KEYTYPE, \ |
295 | | ossl_##keytype##_keymgmt_functions, \ |
296 | | keytype##_private_key_bio, \ |
297 | | keytype##_adjust, \ |
298 | | keytype##_free \ |
299 | | }; \ |
300 | | static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx; \ |
301 | | static void *pvk2##keytype##_newctx(void *provctx) \ |
302 | 0 | { \ |
303 | 0 | return pvk2key_newctx(provctx, &pvk2##keytype##_desc); \ |
304 | 0 | } \ Unexecuted instantiation: decode_pvk2key.c:pvk2dsa_newctx Unexecuted instantiation: decode_pvk2key.c:pvk2rsa_newctx |
305 | | const OSSL_DISPATCH \ |
306 | | ossl_##pvk_to_##keytype##_decoder_functions[] = { \ |
307 | | { OSSL_FUNC_DECODER_NEWCTX, \ |
308 | | (void (*)(void))pvk2##keytype##_newctx }, \ |
309 | | { OSSL_FUNC_DECODER_FREECTX, \ |
310 | | (void (*)(void))pvk2key_freectx }, \ |
311 | | { OSSL_FUNC_DECODER_DOES_SELECTION, \ |
312 | | (void (*)(void))pvk2key_does_selection }, \ |
313 | | { OSSL_FUNC_DECODER_DECODE, \ |
314 | | (void (*)(void))pvk2key_decode }, \ |
315 | | { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ |
316 | | (void (*)(void))pvk2key_export_object }, \ |
317 | | { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \ |
318 | | (void (*)(void))pvk2key_settable_ctx_params }, \ |
319 | | { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \ |
320 | | (void (*)(void))pvk2key_set_ctx_params }, \ |
321 | | OSSL_DISPATCH_END \ |
322 | | } |
323 | | |
324 | | #ifndef OPENSSL_NO_DSA |
325 | | IMPLEMENT_MS(DSA, dsa); |
326 | | #endif |
327 | | IMPLEMENT_MS(RSA, rsa); |