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