]>
Commit | Line | Data |
---|---|---|
e538294f MC |
1 | /* |
2 | * Copyright 2020 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 | #include <string.h> | |
11 | #include <openssl/core_dispatch.h> | |
12 | #include <openssl/core_names.h> | |
13 | #include <openssl/params.h> | |
14 | #include <openssl/err.h> | |
15 | #include <openssl/evp.h> | |
16 | #include "openssl/param_build.h" | |
17 | #include "internal/param_build_set.h" | |
18 | #include "prov/implementations.h" | |
19 | #include "prov/providercommon.h" | |
20 | #include "prov/provider_ctx.h" | |
21 | #include "prov/macsignature.h" | |
22 | ||
23 | ||
24 | static OSSL_FUNC_keymgmt_new_fn mac_new; | |
25 | static OSSL_FUNC_keymgmt_free_fn mac_free; | |
26 | static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init; | |
27 | static OSSL_FUNC_keymgmt_gen_fn mac_gen; | |
28 | static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup; | |
29 | static OSSL_FUNC_keymgmt_get_params_fn mac_get_params; | |
30 | static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params; | |
31 | static OSSL_FUNC_keymgmt_set_params_fn mac_set_params; | |
32 | static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params; | |
33 | static OSSL_FUNC_keymgmt_has_fn mac_has; | |
34 | static OSSL_FUNC_keymgmt_match_fn mac_match; | |
35 | static OSSL_FUNC_keymgmt_import_fn mac_import; | |
36 | static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types; | |
37 | static OSSL_FUNC_keymgmt_export_fn mac_export; | |
38 | static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types; | |
39 | ||
40 | struct mac_gen_ctx { | |
41 | OPENSSL_CTX *libctx; | |
42 | int selection; | |
43 | unsigned char *priv_key; | |
44 | size_t priv_key_len; | |
45 | }; | |
46 | ||
47 | MAC_KEY *mac_key_new(OPENSSL_CTX *libctx) | |
48 | { | |
49 | MAC_KEY *mackey = OPENSSL_zalloc(sizeof(*mackey)); | |
50 | ||
51 | if (mackey == NULL) | |
52 | return NULL; | |
53 | ||
54 | mackey->lock = CRYPTO_THREAD_lock_new(); | |
55 | if (mackey->lock == NULL) { | |
56 | OPENSSL_free(mackey); | |
57 | return NULL; | |
58 | } | |
59 | mackey->libctx = libctx; | |
60 | mackey->refcnt = 1; | |
61 | ||
62 | return mackey; | |
63 | } | |
64 | ||
65 | void mac_key_free(MAC_KEY *mackey) | |
66 | { | |
67 | int ref = 0; | |
68 | ||
69 | if (mackey == NULL) | |
70 | return; | |
71 | ||
72 | CRYPTO_DOWN_REF(&mackey->refcnt, &ref, mackey->lock); | |
73 | if (ref > 0) | |
74 | return; | |
75 | ||
76 | OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len); | |
77 | CRYPTO_THREAD_lock_free(mackey->lock); | |
78 | OPENSSL_free(mackey); | |
79 | } | |
80 | ||
81 | int mac_key_up_ref(MAC_KEY *mackey) | |
82 | { | |
83 | int ref = 0; | |
84 | ||
85 | CRYPTO_UP_REF(&mackey->refcnt, &ref, mackey->lock); | |
86 | return 1; | |
87 | } | |
88 | ||
89 | static void *mac_new(void *provctx) | |
90 | { | |
91 | return mac_key_new(PROV_LIBRARY_CONTEXT_OF(provctx)); | |
92 | } | |
93 | ||
94 | static void mac_free(void *mackey) | |
95 | { | |
96 | mac_key_free(mackey); | |
97 | } | |
98 | ||
99 | static int mac_has(void *keydata, int selection) | |
100 | { | |
101 | MAC_KEY *key = keydata; | |
102 | int ok = 0; | |
103 | ||
104 | if (key != NULL) { | |
105 | /* | |
106 | * MAC keys always have all the parameters they need (i.e. none). | |
107 | * Therefore we always return with 1, if asked about parameters. | |
108 | * Similarly for public keys. | |
109 | */ | |
110 | ok = 1; | |
111 | ||
112 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
113 | ok = key->priv_key != NULL; | |
114 | } | |
115 | return ok; | |
116 | } | |
117 | ||
118 | static int mac_match(const void *keydata1, const void *keydata2, int selection) | |
119 | { | |
120 | const MAC_KEY *key1 = keydata1; | |
121 | const MAC_KEY *key2 = keydata2; | |
122 | int ok = 1; | |
123 | ||
124 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | |
125 | if ((key1->priv_key == NULL && key2->priv_key != NULL) | |
126 | || (key1->priv_key != NULL && key2->priv_key == NULL) | |
127 | || key1->priv_key_len != key2->priv_key_len) | |
128 | ok = 0; | |
129 | else | |
130 | ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */ | |
131 | || CRYPTO_memcmp(key1->priv_key, key2->priv_key, | |
132 | key1->priv_key_len) == 0); | |
133 | } | |
134 | return ok; | |
135 | } | |
136 | ||
137 | static int mac_key_fromdata(MAC_KEY *key, const OSSL_PARAM params[]) | |
138 | { | |
139 | const OSSL_PARAM *p; | |
140 | ||
141 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
142 | if (p != NULL) { | |
143 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
144 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
145 | return 0; | |
146 | } | |
147 | OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len); | |
148 | key->priv_key = OPENSSL_secure_malloc(p->data_size); | |
149 | if (key->priv_key == NULL) { | |
150 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
151 | return 0; | |
152 | } | |
153 | memcpy(key->priv_key, p->data, p->data_size); | |
154 | key->priv_key_len = p->data_size; | |
155 | ||
156 | return 1; | |
157 | } | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static int mac_import(void *keydata, int selection, const OSSL_PARAM params[]) | |
162 | { | |
163 | MAC_KEY *key = keydata; | |
164 | ||
165 | if (key == NULL) | |
166 | return 0; | |
167 | ||
168 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0) | |
169 | return 0; | |
170 | ||
171 | return mac_key_fromdata(key, params); | |
172 | } | |
173 | ||
174 | static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl, | |
175 | OSSL_PARAM params[]) | |
176 | { | |
177 | if (key == NULL) | |
178 | return 0; | |
179 | ||
180 | if (key->priv_key != NULL | |
181 | && !ossl_param_build_set_octet_string(tmpl, params, | |
182 | OSSL_PKEY_PARAM_PRIV_KEY, | |
183 | key->priv_key, key->priv_key_len)) | |
184 | return 0; | |
185 | ||
186 | return 1; | |
187 | } | |
188 | ||
189 | static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, | |
190 | void *cbarg) | |
191 | { | |
192 | MAC_KEY *key = keydata; | |
193 | OSSL_PARAM_BLD *tmpl; | |
194 | OSSL_PARAM *params = NULL; | |
195 | int ret = 0; | |
196 | ||
197 | if (key == NULL) | |
198 | return 0; | |
199 | ||
200 | tmpl = OSSL_PARAM_BLD_new(); | |
201 | if (tmpl == NULL) | |
202 | return 0; | |
203 | ||
204 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 | |
205 | && !key_to_params(key, tmpl, NULL)) | |
206 | goto err; | |
207 | ||
208 | params = OSSL_PARAM_BLD_to_param(tmpl); | |
209 | if (params == NULL) | |
210 | goto err; | |
211 | ||
212 | ret = param_cb(params, cbarg); | |
213 | OSSL_PARAM_BLD_free_params(params); | |
214 | err: | |
215 | OSSL_PARAM_BLD_free(tmpl); | |
216 | return ret; | |
217 | } | |
218 | ||
219 | static const OSSL_PARAM mac_key_types[] = { | |
220 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
221 | OSSL_PARAM_END | |
222 | }; | |
223 | static const OSSL_PARAM *mac_imexport_types(int selection) | |
224 | { | |
225 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
226 | return mac_key_types; | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | static int mac_get_params(void *key, OSSL_PARAM params[]) | |
231 | { | |
232 | return key_to_params(key, NULL, params); | |
233 | } | |
234 | ||
235 | static const OSSL_PARAM *mac_gettable_params(void *provctx) | |
236 | { | |
237 | static const OSSL_PARAM gettable_params[] = { | |
238 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
239 | OSSL_PARAM_END | |
240 | }; | |
241 | return gettable_params; | |
242 | } | |
243 | ||
244 | static int mac_set_params(void *keydata, const OSSL_PARAM params[]) | |
245 | { | |
246 | MAC_KEY *key = keydata; | |
247 | const OSSL_PARAM *p; | |
248 | ||
249 | if (key == NULL) | |
250 | return 0; | |
251 | ||
252 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
253 | if (p != NULL) | |
254 | return mac_key_fromdata(key, params); | |
255 | ||
256 | return 1; | |
257 | } | |
258 | ||
259 | static const OSSL_PARAM *mac_settable_params(void *provctx) | |
260 | { | |
261 | static const OSSL_PARAM settable_params[] = { | |
262 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
263 | OSSL_PARAM_END | |
264 | }; | |
265 | return settable_params; | |
266 | } | |
267 | ||
268 | static void *mac_gen_init(void *provctx, int selection) | |
269 | { | |
270 | OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
271 | struct mac_gen_ctx *gctx = NULL; | |
272 | ||
273 | if ((gctx = OPENSSL_malloc(sizeof(*gctx))) != NULL) { | |
274 | gctx->libctx = libctx; | |
275 | gctx->selection = selection; | |
276 | } | |
277 | return gctx; | |
278 | } | |
279 | ||
280 | ||
281 | static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[]) | |
282 | { | |
283 | struct mac_gen_ctx *gctx = genctx; | |
284 | const OSSL_PARAM *p; | |
285 | ||
286 | if (gctx == NULL) | |
287 | return 0; | |
288 | ||
289 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); | |
290 | if (p != NULL) { | |
291 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
292 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
293 | return 0; | |
294 | } | |
295 | gctx->priv_key = OPENSSL_secure_malloc(p->data_size); | |
296 | if (gctx->priv_key == NULL) { | |
297 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
298 | return 0; | |
299 | } | |
300 | memcpy(gctx->priv_key, p->data, p->data_size); | |
301 | gctx->priv_key_len = p->data_size; | |
302 | } | |
303 | ||
304 | return 1; | |
305 | } | |
306 | ||
307 | static const OSSL_PARAM *mac_gen_settable_params(void *provctx) | |
308 | { | |
309 | static OSSL_PARAM settable[] = { | |
310 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), | |
311 | OSSL_PARAM_END | |
312 | }; | |
313 | return settable; | |
314 | } | |
315 | ||
316 | static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg) | |
317 | { | |
318 | struct mac_gen_ctx *gctx = genctx; | |
319 | MAC_KEY *key; | |
320 | ||
321 | if (gctx == NULL) | |
322 | return NULL; | |
323 | ||
324 | if ((key = mac_key_new(gctx->libctx)) == NULL) { | |
325 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
326 | return NULL; | |
327 | } | |
328 | ||
329 | /* If we're doing parameter generation then we just return a blank key */ | |
330 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) | |
331 | return key; | |
332 | ||
333 | if (gctx->priv_key == NULL) { | |
334 | ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY); | |
335 | mac_key_free(key); | |
336 | return NULL; | |
337 | } | |
338 | ||
339 | /* | |
340 | * This is horrible but required for backwards compatibility. We don't | |
341 | * actually do real key generation at all. We simply copy the key that was | |
342 | * previously set in the gctx. Hopefully at some point in the future all | |
343 | * of this can be removed and we will only support the EVP_KDF APIs. | |
344 | */ | |
345 | key->priv_key = gctx->priv_key; | |
346 | key->priv_key_len = gctx->priv_key_len; | |
347 | gctx->priv_key_len = 0; | |
348 | gctx->priv_key = NULL; | |
349 | ||
350 | return key; | |
351 | } | |
352 | ||
353 | static void mac_gen_cleanup(void *genctx) | |
354 | { | |
355 | struct mac_gen_ctx *gctx = genctx; | |
356 | ||
357 | OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len); | |
358 | OPENSSL_free(gctx); | |
359 | } | |
360 | ||
361 | const OSSL_DISPATCH mac_keymgmt_functions[] = { | |
362 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new }, | |
363 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, | |
364 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params }, | |
365 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))mac_gettable_params }, | |
366 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params }, | |
367 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params }, | |
368 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, | |
369 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, | |
370 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import }, | |
371 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types }, | |
372 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, | |
373 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types }, | |
374 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init }, | |
375 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params }, | |
376 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
377 | (void (*)(void))mac_gen_settable_params }, | |
378 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, | |
379 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, | |
380 | { 0, NULL } | |
381 | }; |