ca0790fcd627984eb7d618e17cdf14efa8e8e004
[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     EVP_KEYMGMT *tmp_keymgmt = NULL;
25     const char *supported_ciph = NULL;
26
27     if (ctx == NULL) {
28         EVPerr(0, 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 (ctx->keytype == NULL || ctx->engine != NULL)
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         EVPerr(0, 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             EVPerr(0, 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             EVPerr(0, 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         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
126         goto err;
127     }
128
129     if (ret <= 0) {
130         cipher->freectx(ctx->op.ciph.ciphprovctx);
131         ctx->op.ciph.ciphprovctx = NULL;
132         goto err;
133     }
134     return 1;
135
136  legacy:
137     /*
138      * TODO remove this when legacy is gone
139      * If we don't have the full support we need with provided methods,
140      * let's go see if legacy does.
141      */
142     ERR_pop_to_mark();
143
144     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
145         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
146         return -2;
147     }
148     switch(ctx->operation) {
149     case EVP_PKEY_OP_ENCRYPT:
150         if (ctx->pmeth->encrypt_init == NULL)
151             return 1;
152         ret = ctx->pmeth->encrypt_init(ctx);
153         break;
154     case EVP_PKEY_OP_DECRYPT:
155         if (ctx->pmeth->decrypt_init == NULL)
156             return 1;
157         ret = ctx->pmeth->decrypt_init(ctx);
158         break;
159     default:
160         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
161         ret = -1;
162     }
163
164  err:
165     if (ret <= 0)
166         ctx->operation = EVP_PKEY_OP_UNDEFINED;
167     return ret;
168 }
169
170 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
171 {
172     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT);
173 }
174
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         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
183         return -2;
184     }
185
186     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
187         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
188         return -1;
189     }
190
191     if (ctx->op.ciph.ciphprovctx == NULL)
192         goto legacy;
193
194     ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.ciphprovctx, 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         EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
201                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
202         return -2;
203     }
204     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
205         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
206 }
207
208 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
209 {
210     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT);
211 }
212
213 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
214                      unsigned char *out, size_t *outlen,
215                      const unsigned char *in, size_t inlen)
216 {
217     int ret;
218
219     if (ctx == NULL) {
220         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
221         return -2;
222     }
223
224     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
225         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
226         return -1;
227     }
228
229     if (ctx->op.ciph.ciphprovctx == NULL)
230         goto legacy;
231
232     ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.ciphprovctx, out, outlen,
233                                        (out == NULL ? 0 : *outlen), in, inlen);
234     return ret;
235
236  legacy:
237     if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
238         EVPerr(EVP_F_EVP_PKEY_DECRYPT,
239                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
240         return -2;
241     }
242     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
243         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
244 }
245
246
247 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
248 {
249     EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
250
251     if (cipher == NULL) {
252         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
253         return NULL;
254     }
255
256     cipher->lock = CRYPTO_THREAD_lock_new();
257     if (cipher->lock == NULL) {
258         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
259         OPENSSL_free(cipher);
260         return NULL;
261     }
262     cipher->prov = prov;
263     ossl_provider_up_ref(prov);
264     cipher->refcnt = 1;
265
266     return cipher;
267 }
268
269 static void *evp_asym_cipher_from_dispatch(int name_id,
270                                            const OSSL_DISPATCH *fns,
271                                            OSSL_PROVIDER *prov)
272 {
273     EVP_ASYM_CIPHER *cipher = NULL;
274     int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
275     int gparamfncnt = 0, sparamfncnt = 0;
276
277     if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
278         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
279         goto err;
280     }
281
282     cipher->name_id = name_id;
283
284     for (; fns->function_id != 0; fns++) {
285         switch (fns->function_id) {
286         case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
287             if (cipher->newctx != NULL)
288                 break;
289             cipher->newctx = OSSL_get_OP_asym_cipher_newctx(fns);
290             ctxfncnt++;
291             break;
292         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
293             if (cipher->encrypt_init != NULL)
294                 break;
295             cipher->encrypt_init = OSSL_get_OP_asym_cipher_encrypt_init(fns);
296             encfncnt++;
297             break;
298         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
299             if (cipher->encrypt != NULL)
300                 break;
301             cipher->encrypt = OSSL_get_OP_asym_cipher_encrypt(fns);
302             encfncnt++;
303             break;
304         case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
305             if (cipher->decrypt_init != NULL)
306                 break;
307             cipher->decrypt_init = OSSL_get_OP_asym_cipher_decrypt_init(fns);
308             decfncnt++;
309             break;
310         case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
311             if (cipher->decrypt != NULL)
312                 break;
313             cipher->decrypt = OSSL_get_OP_asym_cipher_decrypt(fns);
314             decfncnt++;
315             break;
316         case OSSL_FUNC_ASYM_CIPHER_FREECTX:
317             if (cipher->freectx != NULL)
318                 break;
319             cipher->freectx = OSSL_get_OP_asym_cipher_freectx(fns);
320             ctxfncnt++;
321             break;
322         case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
323             if (cipher->dupctx != NULL)
324                 break;
325             cipher->dupctx = OSSL_get_OP_asym_cipher_dupctx(fns);
326             break;
327         case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
328             if (cipher->get_ctx_params != NULL)
329                 break;
330             cipher->get_ctx_params
331                 = OSSL_get_OP_asym_cipher_get_ctx_params(fns);
332             gparamfncnt++;
333             break;
334         case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
335             if (cipher->gettable_ctx_params != NULL)
336                 break;
337             cipher->gettable_ctx_params
338                 = OSSL_get_OP_asym_cipher_gettable_ctx_params(fns);
339             gparamfncnt++;
340             break;
341         case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
342             if (cipher->set_ctx_params != NULL)
343                 break;
344             cipher->set_ctx_params
345                 = OSSL_get_OP_asym_cipher_set_ctx_params(fns);
346             sparamfncnt++;
347             break;
348         case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
349             if (cipher->settable_ctx_params != NULL)
350                 break;
351             cipher->settable_ctx_params
352                 = OSSL_get_OP_asym_cipher_settable_ctx_params(fns);
353             sparamfncnt++;
354             break;
355         }
356     }
357     if (ctxfncnt != 2
358         || (encfncnt != 0 && encfncnt != 2)
359         || (decfncnt != 0 && decfncnt != 2)
360         || (encfncnt != 2 && decfncnt != 2)
361         || (gparamfncnt != 0 && gparamfncnt != 2)
362         || (sparamfncnt != 0 && sparamfncnt != 2)) {
363         /*
364          * In order to be a consistent set of functions we must have at least
365          * a set of context functions (newctx and freectx) as well as a pair of
366          * "cipher" functions: (encrypt_init, encrypt) or
367          * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
368          * optional, but if one of them is present then the other one must also
369          * be present. The same applies to get_ctx_params and
370          * gettable_ctx_params. The dupctx function is optional.
371          */
372         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
373         goto err;
374     }
375
376     return cipher;
377  err:
378     EVP_ASYM_CIPHER_free(cipher);
379     return NULL;
380 }
381
382 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
383 {
384     if (cipher != NULL) {
385         int i;
386
387         CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
388         if (i > 0)
389             return;
390         ossl_provider_free(cipher->prov);
391         CRYPTO_THREAD_lock_free(cipher->lock);
392         OPENSSL_free(cipher);
393     }
394 }
395
396 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
397 {
398     int ref = 0;
399
400     CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
401     return 1;
402 }
403
404 OSSL_PROVIDER *EVP_ASYM_CIPHER_provider(const EVP_ASYM_CIPHER *cipher)
405 {
406     return cipher->prov;
407 }
408
409 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
410                                        const char *properties)
411 {
412     return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
413                              evp_asym_cipher_from_dispatch,
414                              (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
415                              (void (*)(void *))EVP_ASYM_CIPHER_free);
416 }
417
418 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
419 {
420     return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
421 }
422
423 int EVP_ASYM_CIPHER_number(const EVP_ASYM_CIPHER *cipher)
424 {
425     return cipher->name_id;
426 }
427
428 void EVP_ASYM_CIPHER_do_all_provided(OPENSSL_CTX *libctx,
429                                      void (*fn)(EVP_ASYM_CIPHER *cipher,
430                                                 void *arg),
431                                      void *arg)
432 {
433     evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
434                        (void (*)(void *, void *))fn, arg,
435                        evp_asym_cipher_from_dispatch,
436                        (void (*)(void *))EVP_ASYM_CIPHER_free);
437 }
438
439
440 void EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
441                                   void (*fn)(const char *name, void *data),
442                                   void *data)
443 {
444     if (cipher->prov != NULL)
445         evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
446 }
447