1 /*
2 * Copyright 2006-2021 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 <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "internal/provider.h"
16 #include "internal/core.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
19
evp_pkey_asym_cipher_init(EVP_PKEY_CTX * ctx,int operation,const OSSL_PARAM params[])20 static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
21 const OSSL_PARAM params[])
22 {
23 int ret = 0;
24 void *provkey = NULL;
25 EVP_ASYM_CIPHER *cipher = NULL;
26 EVP_KEYMGMT *tmp_keymgmt = NULL;
27 const char *supported_ciph = NULL;
28
29 if (ctx == NULL) {
30 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
31 return -2;
32 }
33
34 evp_pkey_ctx_free_old_ops(ctx);
35 ctx->operation = operation;
36
37 ERR_set_mark();
38
39 if (evp_pkey_ctx_is_legacy(ctx))
40 goto legacy;
41
42 /*
43 * Ensure that the key is provided, either natively, or as a cached export.
44 * If not, go legacy
45 */
46 tmp_keymgmt = ctx->keymgmt;
47 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
48 &tmp_keymgmt, ctx->propquery);
49 if (provkey == NULL)
50 goto legacy;
51 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
52 ERR_clear_last_mark();
53 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
54 goto err;
55 }
56 EVP_KEYMGMT_free(ctx->keymgmt);
57 ctx->keymgmt = tmp_keymgmt;
58
59 if (ctx->keymgmt->query_operation_name != NULL)
60 supported_ciph =
61 ctx->keymgmt->query_operation_name(OSSL_OP_ASYM_CIPHER);
62
63 /*
64 * If we didn't get a supported ciph, assume there is one with the
65 * same name as the key type.
66 */
67 if (supported_ciph == NULL)
68 supported_ciph = ctx->keytype;
69
70 /*
71 * Because we cleared out old ops, we shouldn't need to worry about
72 * checking if cipher is already there.
73 */
74 cipher =
75 EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, ctx->propquery);
76
77 if (cipher == NULL
78 || (EVP_KEYMGMT_get0_provider(ctx->keymgmt)
79 != EVP_ASYM_CIPHER_get0_provider(cipher))) {
80 /*
81 * We don't need to free ctx->keymgmt here, as it's not necessarily
82 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
83 */
84 EVP_ASYM_CIPHER_free(cipher);
85 goto legacy;
86 }
87
88 /*
89 * If we don't have the full support we need with provided methods,
90 * let's go see if legacy does.
91 */
92 ERR_pop_to_mark();
93
94 /* No more legacy from here down to legacy: */
95
96 ctx->op.ciph.cipher = cipher;
97 ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
98 if (ctx->op.ciph.algctx == NULL) {
99 /* The provider key can stay in the cache */
100 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
101 goto err;
102 }
103
104 switch (operation) {
105 case EVP_PKEY_OP_ENCRYPT:
106 if (cipher->encrypt_init == NULL) {
107 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
108 ret = -2;
109 goto err;
110 }
111 ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
112 break;
113 case EVP_PKEY_OP_DECRYPT:
114 if (cipher->decrypt_init == NULL) {
115 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
116 ret = -2;
117 goto err;
118 }
119 ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
120 break;
121 default:
122 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
123 goto err;
124 }
125
126 if (ret <= 0)
127 goto err;
128 return 1;
129
130 legacy:
131 /*
132 * If we don't have the full support we need with provided methods,
133 * let's go see if legacy does.
134 */
135 ERR_pop_to_mark();
136
137 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
138 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
139 return -2;
140 }
141 switch(ctx->operation) {
142 case EVP_PKEY_OP_ENCRYPT:
143 if (ctx->pmeth->encrypt_init == NULL)
144 return 1;
145 ret = ctx->pmeth->encrypt_init(ctx);
146 break;
147 case EVP_PKEY_OP_DECRYPT:
148 if (ctx->pmeth->decrypt_init == NULL)
149 return 1;
150 ret = ctx->pmeth->decrypt_init(ctx);
151 break;
152 default:
153 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
154 ret = -1;
155 }
156
157 err:
158 if (ret <= 0) {
159 evp_pkey_ctx_free_old_ops(ctx);
160 ctx->operation = EVP_PKEY_OP_UNDEFINED;
161 }
162 return ret;
163 }
164
EVP_PKEY_encrypt_init(EVP_PKEY_CTX * ctx)165 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
166 {
167 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
168 }
169
EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX * ctx,const OSSL_PARAM params[])170 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
171 {
172 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
173 }
174
EVP_PKEY_encrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)175 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
176 unsigned char *out, size_t *outlen,
177 const unsigned char *in, size_t inlen)
178 {
179 int ret;
180
181 if (ctx == NULL) {
182 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
183 return -2;
184 }
185
186 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
187 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
188 return -1;
189 }
190
191 if (ctx->op.ciph.algctx == NULL)
192 goto legacy;
193
194 ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.algctx, out, outlen,
195 (out == NULL ? 0 : *outlen), in, inlen);
196 return ret;
197
198 legacy:
199 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
200 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
201 return -2;
202 }
203 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
204 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
205 }
206
EVP_PKEY_decrypt_init(EVP_PKEY_CTX * ctx)207 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
208 {
209 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
210 }
211
EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX * ctx,const OSSL_PARAM params[])212 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
213 {
214 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
215 }
216
EVP_PKEY_decrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)217 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
218 unsigned char *out, size_t *outlen,
219 const unsigned char *in, size_t inlen)
220 {
221 int ret;
222
223 if (ctx == NULL) {
224 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
225 return -2;
226 }
227
228 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
229 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
230 return -1;
231 }
232
233 if (ctx->op.ciph.algctx == NULL)
234 goto legacy;
235
236 ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.algctx, out, outlen,
237 (out == NULL ? 0 : *outlen), in, inlen);
238 return ret;
239
240 legacy:
241 if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
242 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
243 return -2;
244 }
245 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
246 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
247 }
248
249
evp_asym_cipher_new(OSSL_PROVIDER * prov)250 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
251 {
252 EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
253
254 if (cipher == NULL) {
255 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
256 return NULL;
257 }
258
259 cipher->lock = CRYPTO_THREAD_lock_new();
260 if (cipher->lock == NULL) {
261 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
262 OPENSSL_free(cipher);
263 return NULL;
264 }
265 cipher->prov = prov;
266 ossl_provider_up_ref(prov);
267 cipher->refcnt = 1;
268
269 return cipher;
270 }
271
evp_asym_cipher_from_algorithm(int name_id,const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov)272 static void *evp_asym_cipher_from_algorithm(int name_id,
273 const OSSL_ALGORITHM *algodef,
274 OSSL_PROVIDER *prov)
275 {
276 const OSSL_DISPATCH *fns = algodef->implementation;
277 EVP_ASYM_CIPHER *cipher = NULL;
278 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
279 int gparamfncnt = 0, sparamfncnt = 0;
280
281 if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
282 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
283 goto err;
284 }
285
286 cipher->name_id = name_id;
287 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
288 goto err;
289 cipher->description = algodef->algorithm_description;
290
291 for (; fns->function_id != 0; fns++) {
292 switch (fns->function_id) {
293 case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
294 if (cipher->newctx != NULL)
295 break;
296 cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
297 ctxfncnt++;
298 break;
299 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
300 if (cipher->encrypt_init != NULL)
301 break;
302 cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
303 encfncnt++;
304 break;
305 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
306 if (cipher->encrypt != NULL)
307 break;
308 cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
309 encfncnt++;
310 break;
311 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
312 if (cipher->decrypt_init != NULL)
313 break;
314 cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
315 decfncnt++;
316 break;
317 case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
318 if (cipher->decrypt != NULL)
319 break;
320 cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
321 decfncnt++;
322 break;
323 case OSSL_FUNC_ASYM_CIPHER_FREECTX:
324 if (cipher->freectx != NULL)
325 break;
326 cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
327 ctxfncnt++;
328 break;
329 case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
330 if (cipher->dupctx != NULL)
331 break;
332 cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
333 break;
334 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
335 if (cipher->get_ctx_params != NULL)
336 break;
337 cipher->get_ctx_params
338 = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
339 gparamfncnt++;
340 break;
341 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
342 if (cipher->gettable_ctx_params != NULL)
343 break;
344 cipher->gettable_ctx_params
345 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
346 gparamfncnt++;
347 break;
348 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
349 if (cipher->set_ctx_params != NULL)
350 break;
351 cipher->set_ctx_params
352 = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
353 sparamfncnt++;
354 break;
355 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
356 if (cipher->settable_ctx_params != NULL)
357 break;
358 cipher->settable_ctx_params
359 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
360 sparamfncnt++;
361 break;
362 }
363 }
364 if (ctxfncnt != 2
365 || (encfncnt != 0 && encfncnt != 2)
366 || (decfncnt != 0 && decfncnt != 2)
367 || (encfncnt != 2 && decfncnt != 2)
368 || (gparamfncnt != 0 && gparamfncnt != 2)
369 || (sparamfncnt != 0 && sparamfncnt != 2)) {
370 /*
371 * In order to be a consistent set of functions we must have at least
372 * a set of context functions (newctx and freectx) as well as a pair of
373 * "cipher" functions: (encrypt_init, encrypt) or
374 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
375 * optional, but if one of them is present then the other one must also
376 * be present. The same applies to get_ctx_params and
377 * gettable_ctx_params. The dupctx function is optional.
378 */
379 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
380 goto err;
381 }
382
383 return cipher;
384 err:
385 EVP_ASYM_CIPHER_free(cipher);
386 return NULL;
387 }
388
EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER * cipher)389 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
390 {
391 int i;
392
393 if (cipher == NULL)
394 return;
395 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
396 if (i > 0)
397 return;
398 OPENSSL_free(cipher->type_name);
399 ossl_provider_free(cipher->prov);
400 CRYPTO_THREAD_lock_free(cipher->lock);
401 OPENSSL_free(cipher);
402 }
403
EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER * cipher)404 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
405 {
406 int ref = 0;
407
408 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
409 return 1;
410 }
411
EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER * cipher)412 OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
413 {
414 return cipher->prov;
415 }
416
EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX * ctx,const char * algorithm,const char * properties)417 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
418 const char *properties)
419 {
420 return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
421 evp_asym_cipher_from_algorithm,
422 (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
423 (void (*)(void *))EVP_ASYM_CIPHER_free);
424 }
425
EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER * cipher,const char * name)426 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
427 {
428 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
429 }
430
evp_asym_cipher_get_number(const EVP_ASYM_CIPHER * cipher)431 int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
432 {
433 return cipher->name_id;
434 }
435
EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER * cipher)436 const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
437 {
438 return cipher->type_name;
439 }
440
EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER * cipher)441 const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
442 {
443 return cipher->description;
444 }
445
EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX * libctx,void (* fn)(EVP_ASYM_CIPHER * cipher,void * arg),void * arg)446 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
447 void (*fn)(EVP_ASYM_CIPHER *cipher,
448 void *arg),
449 void *arg)
450 {
451 evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
452 (void (*)(void *, void *))fn, arg,
453 evp_asym_cipher_from_algorithm,
454 (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
455 (void (*)(void *))EVP_ASYM_CIPHER_free);
456 }
457
458
EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER * cipher,void (* fn)(const char * name,void * data),void * data)459 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
460 void (*fn)(const char *name, void *data),
461 void *data)
462 {
463 if (cipher->prov != NULL)
464 return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
465
466 return 1;
467 }
468
EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER * cip)469 const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
470 {
471 void *provctx;
472
473 if (cip == NULL || cip->gettable_ctx_params == NULL)
474 return NULL;
475
476 provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
477 return cip->gettable_ctx_params(NULL, provctx);
478 }
479
EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER * cip)480 const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
481 {
482 void *provctx;
483
484 if (cip == NULL || cip->settable_ctx_params == NULL)
485 return NULL;
486
487 provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
488 return cip->settable_ctx_params(NULL, provctx);
489 }
490