]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_fn.c
31422baa4c3e9b37220a8ef3e193f3b3f00bcd28
[thirdparty/openssl.git] / crypto / evp / pmeth_fn.c
1 /*
2 * Copyright 2006-2016 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 "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
18
19 static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation)
20 {
21 int ret = 0;
22 void *provkey = NULL;
23 EVP_ASYM_CIPHER *cipher = NULL;
24
25 if (ctx == NULL) {
26 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
27 return -2;
28 }
29
30 evp_pkey_ctx_free_old_ops(ctx);
31 ctx->operation = operation;
32
33 if (ctx->keytype == NULL || ctx->engine != NULL)
34 goto legacy;
35
36 if (ctx->keymgmt == NULL)
37 ctx->keymgmt =
38 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, ctx->propquery);
39 if (ctx->keymgmt != NULL) {
40 const char *supported_ciph = NULL;
41
42 if (ctx->keymgmt->query_operation_name != NULL)
43 supported_ciph =
44 ctx->keymgmt->query_operation_name(OSSL_OP_ASYM_CIPHER);
45
46 /*
47 * If we didn't get a supported ciph, assume there is one with the
48 * same name as the key type.
49 */
50 if (supported_ciph == NULL)
51 supported_ciph = ctx->keytype;
52
53 /*
54 * Because we cleared out old ops, we shouldn't need to worry about
55 * checking if cipher is already there.
56 */
57 cipher =
58 EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, ctx->propquery);
59 }
60
61 if (ctx->keymgmt == NULL
62 || cipher == NULL
63 || (EVP_KEYMGMT_provider(ctx->keymgmt)
64 != EVP_ASYM_CIPHER_provider(cipher))) {
65 /*
66 * We don't have the full support we need with provided methods,
67 * let's go see if legacy does. Also, we don't need to free
68 * ctx->keymgmt here, as it's not necessarily tied to this
69 * operation. It will be freed by EVP_PKEY_CTX_free().
70 */
71 EVP_ASYM_CIPHER_free(cipher);
72 goto legacy;
73 }
74
75 ctx->op.ciph.cipher = cipher;
76
77 if (ctx->pkey != NULL) {
78 provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
79 /* If export failed, legacy may be able to pick it up */
80 if (provkey == NULL)
81 goto legacy;
82 }
83 ctx->op.ciph.ciphprovctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
84 if (ctx->op.ciph.ciphprovctx == NULL) {
85 /* The provider key can stay in the cache */
86 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
87 goto err;
88 }
89
90 switch (operation) {
91 case EVP_PKEY_OP_ENCRYPT:
92 if (cipher->encrypt_init == NULL) {
93 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
94 ret = -2;
95 goto err;
96 }
97 ret = cipher->encrypt_init(ctx->op.ciph.ciphprovctx, provkey);
98 break;
99 case EVP_PKEY_OP_DECRYPT:
100 if (cipher->decrypt_init == NULL) {
101 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
102 ret = -2;
103 goto err;
104 }
105 ret = cipher->decrypt_init(ctx->op.ciph.ciphprovctx, provkey);
106 break;
107 default:
108 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
109 goto err;
110 }
111
112 if (ret <= 0) {
113 cipher->freectx(ctx->op.ciph.ciphprovctx);
114 ctx->op.ciph.ciphprovctx = NULL;
115 goto err;
116 }
117 return 1;
118
119 legacy:
120 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
121 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
122 return -2;
123 }
124 switch(ctx->operation) {
125 case EVP_PKEY_OP_ENCRYPT:
126 if (ctx->pmeth->encrypt_init == NULL)
127 return 1;
128 ret = ctx->pmeth->encrypt_init(ctx);
129 break;
130 case EVP_PKEY_OP_DECRYPT:
131 if (ctx->pmeth->decrypt_init == NULL)
132 return 1;
133 ret = ctx->pmeth->decrypt_init(ctx);
134 break;
135 default:
136 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
137 ret = -1;
138 }
139
140 err:
141 if (ret <= 0)
142 ctx->operation = EVP_PKEY_OP_UNDEFINED;
143 return ret;
144 }
145
146 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
147 {
148 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT);
149 }
150
151 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
152 unsigned char *out, size_t *outlen,
153 const unsigned char *in, size_t inlen)
154 {
155 int ret;
156
157 if (ctx == NULL) {
158 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
159 return -2;
160 }
161
162 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
163 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
164 return -1;
165 }
166
167 if (ctx->op.ciph.ciphprovctx == NULL)
168 goto legacy;
169
170 ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.ciphprovctx, out, outlen,
171 (out == NULL ? 0 : *outlen), in, inlen);
172 return ret;
173
174 legacy:
175 if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
176 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
177 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
178 return -2;
179 }
180 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
181 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
182 }
183
184 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
185 {
186 return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT);
187 }
188
189 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
190 unsigned char *out, size_t *outlen,
191 const unsigned char *in, size_t inlen)
192 {
193 int ret;
194
195 if (ctx == NULL) {
196 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
197 return -2;
198 }
199
200 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
201 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
202 return -1;
203 }
204
205 if (ctx->op.ciph.ciphprovctx == NULL)
206 goto legacy;
207
208 ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.ciphprovctx, out, outlen,
209 (out == NULL ? 0 : *outlen), in, inlen);
210 return ret;
211
212 legacy:
213 if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
214 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
215 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
216 return -2;
217 }
218 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
219 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
220 }
221
222
223 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
224 {
225 EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
226
227 if (cipher == NULL) {
228 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
229 return NULL;
230 }
231
232 cipher->lock = CRYPTO_THREAD_lock_new();
233 if (cipher->lock == NULL) {
234 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
235 OPENSSL_free(cipher);
236 return NULL;
237 }
238 cipher->prov = prov;
239 ossl_provider_up_ref(prov);
240 cipher->refcnt = 1;
241
242 return cipher;
243 }
244
245 static void *evp_asym_cipher_from_dispatch(int name_id,
246 const OSSL_DISPATCH *fns,
247 OSSL_PROVIDER *prov)
248 {
249 EVP_ASYM_CIPHER *cipher = NULL;
250 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
251 int gparamfncnt = 0, sparamfncnt = 0;
252
253 if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
254 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
255 goto err;
256 }
257
258 cipher->name_id = name_id;
259
260 for (; fns->function_id != 0; fns++) {
261 switch (fns->function_id) {
262 case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
263 if (cipher->newctx != NULL)
264 break;
265 cipher->newctx = OSSL_get_OP_asym_cipher_newctx(fns);
266 ctxfncnt++;
267 break;
268 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
269 if (cipher->encrypt_init != NULL)
270 break;
271 cipher->encrypt_init = OSSL_get_OP_asym_cipher_encrypt_init(fns);
272 encfncnt++;
273 break;
274 case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
275 if (cipher->encrypt != NULL)
276 break;
277 cipher->encrypt = OSSL_get_OP_asym_cipher_encrypt(fns);
278 encfncnt++;
279 break;
280 case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
281 if (cipher->decrypt_init != NULL)
282 break;
283 cipher->decrypt_init = OSSL_get_OP_asym_cipher_decrypt_init(fns);
284 decfncnt++;
285 break;
286 case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
287 if (cipher->decrypt != NULL)
288 break;
289 cipher->decrypt = OSSL_get_OP_asym_cipher_decrypt(fns);
290 decfncnt++;
291 break;
292 case OSSL_FUNC_ASYM_CIPHER_FREECTX:
293 if (cipher->freectx != NULL)
294 break;
295 cipher->freectx = OSSL_get_OP_asym_cipher_freectx(fns);
296 ctxfncnt++;
297 break;
298 case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
299 if (cipher->dupctx != NULL)
300 break;
301 cipher->dupctx = OSSL_get_OP_asym_cipher_dupctx(fns);
302 break;
303 case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
304 if (cipher->get_ctx_params != NULL)
305 break;
306 cipher->get_ctx_params
307 = OSSL_get_OP_asym_cipher_get_ctx_params(fns);
308 gparamfncnt++;
309 break;
310 case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
311 if (cipher->gettable_ctx_params != NULL)
312 break;
313 cipher->gettable_ctx_params
314 = OSSL_get_OP_asym_cipher_gettable_ctx_params(fns);
315 gparamfncnt++;
316 break;
317 case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
318 if (cipher->set_ctx_params != NULL)
319 break;
320 cipher->set_ctx_params
321 = OSSL_get_OP_asym_cipher_set_ctx_params(fns);
322 sparamfncnt++;
323 break;
324 case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
325 if (cipher->settable_ctx_params != NULL)
326 break;
327 cipher->settable_ctx_params
328 = OSSL_get_OP_asym_cipher_settable_ctx_params(fns);
329 sparamfncnt++;
330 break;
331 }
332 }
333 if (ctxfncnt != 2
334 || (encfncnt != 0 && encfncnt != 2)
335 || (decfncnt != 0 && decfncnt != 2)
336 || (encfncnt != 2 && decfncnt != 2)
337 || (gparamfncnt != 0 && gparamfncnt != 2)
338 || (sparamfncnt != 0 && sparamfncnt != 2)) {
339 /*
340 * In order to be a consistent set of functions we must have at least
341 * a set of context functions (newctx and freectx) as well as a pair of
342 * "cipher" functions: (encrypt_init, encrypt) or
343 * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
344 * optional, but if one of them is present then the other one must also
345 * be present. The same applies to get_ctx_params and
346 * gettable_ctx_params. The dupctx function is optional.
347 */
348 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
349 goto err;
350 }
351
352 return cipher;
353 err:
354 EVP_ASYM_CIPHER_free(cipher);
355 return NULL;
356 }
357
358 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
359 {
360 if (cipher != NULL) {
361 int i;
362
363 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
364 if (i > 0)
365 return;
366 ossl_provider_free(cipher->prov);
367 CRYPTO_THREAD_lock_free(cipher->lock);
368 OPENSSL_free(cipher);
369 }
370 }
371
372 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
373 {
374 int ref = 0;
375
376 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
377 return 1;
378 }
379
380 OSSL_PROVIDER *EVP_ASYM_CIPHER_provider(const EVP_ASYM_CIPHER *cipher)
381 {
382 return cipher->prov;
383 }
384
385 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
386 const char *properties)
387 {
388 return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
389 evp_asym_cipher_from_dispatch,
390 (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
391 (void (*)(void *))EVP_ASYM_CIPHER_free);
392 }
393
394 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
395 {
396 return evp_is_a(cipher->prov, cipher->name_id, name);
397 }
398
399 int EVP_ASYM_CIPHER_number(const EVP_ASYM_CIPHER *cipher)
400 {
401 return cipher->name_id;
402 }
403
404 void EVP_ASYM_CIPHER_do_all_provided(OPENSSL_CTX *libctx,
405 void (*fn)(EVP_ASYM_CIPHER *cipher,
406 void *arg),
407 void *arg)
408 {
409 evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
410 (void (*)(void *, void *))fn, arg,
411 evp_asym_cipher_from_dispatch,
412 (void (*)(void *))EVP_ASYM_CIPHER_free);
413 }
414
415
416 void EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
417 void (*fn)(const char *name, void *data),
418 void *data)
419 {
420 if (cipher->prov != NULL)
421 evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
422 }
423