Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / crypto / evp / asymcipher.c
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
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 OSSL_PROVIDER *tmp_prov = NULL;
28     const char *supported_ciph = NULL;
29     int iter;
30
31     if (ctx == NULL) {
32         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
33         return -2;
34     }
35
36     evp_pkey_ctx_free_old_ops(ctx);
37     ctx->operation = operation;
38
39     ERR_set_mark();
40
41     if (evp_pkey_ctx_is_legacy(ctx))
42         goto legacy;
43
44     if (ctx->pkey == NULL) {
45         ERR_clear_last_mark();
46         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
47         goto err;
48     }
49
50     /*
51      * Try to derive the supported asym cipher from |ctx->keymgmt|.
52      */
53     if (!ossl_assert(ctx->pkey->keymgmt == NULL
54                      || ctx->pkey->keymgmt == ctx->keymgmt)) {
55         ERR_clear_last_mark();
56         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
57         goto err;
58     }
59     supported_ciph
60         = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
61                                                 OSSL_OP_ASYM_CIPHER);
62     if (supported_ciph == NULL) {
63         ERR_clear_last_mark();
64         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
65         goto err;
66     }
67
68     /*
69      * We perform two iterations:
70      *
71      * 1.  Do the normal asym cipher fetch, using the fetching data given by
72      *     the EVP_PKEY_CTX.
73      * 2.  Do the provider specific asym cipher fetch, from the same provider
74      *     as |ctx->keymgmt|
75      *
76      * We then try to fetch the keymgmt from the same provider as the
77      * asym cipher, and try to export |ctx->pkey| to that keymgmt (when
78      * this keymgmt happens to be the same as |ctx->keymgmt|, the export
79      * is a no-op, but we call it anyway to not complicate the code even
80      * more).
81      * If the export call succeeds (returns a non-NULL provider key pointer),
82      * we're done and can perform the operation itself.  If not, we perform
83      * the second iteration, or jump to legacy.
84      */
85     for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
86         EVP_KEYMGMT *tmp_keymgmt_tofree;
87
88         /*
89          * If we're on the second iteration, free the results from the first.
90          * They are NULL on the first iteration, so no need to check what
91          * iteration we're on.
92          */
93         EVP_ASYM_CIPHER_free(cipher);
94         EVP_KEYMGMT_free(tmp_keymgmt);
95
96         switch (iter) {
97         case 1:
98             cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph,
99                                            ctx->propquery);
100             if (cipher != NULL)
101                 tmp_prov = EVP_ASYM_CIPHER_get0_provider(cipher);
102             break;
103         case 2:
104             tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
105             cipher =
106                 evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
107                                                 supported_ciph, ctx->propquery);
108             if (cipher == NULL)
109                 goto legacy;
110             break;
111         }
112         if (cipher == NULL)
113             continue;
114
115         /*
116          * Ensure that the key is provided, either natively, or as a cached
117          * export.  We start by fetching the keymgmt with the same name as
118          * |ctx->pkey|, but from the provider of the asym cipher method, using
119          * the same property query as when fetching the asym cipher method.
120          * With the keymgmt we found (if we did), we try to export |ctx->pkey|
121          * to it (evp_pkey_export_to_provider() is smart enough to only actually
122          * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
123          */
124         tmp_keymgmt_tofree = tmp_keymgmt
125             = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
126                                           EVP_KEYMGMT_get0_name(ctx->keymgmt),
127                                           ctx->propquery);
128         if (tmp_keymgmt != NULL)
129             provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
130                                                   &tmp_keymgmt, ctx->propquery);
131         if (tmp_keymgmt == NULL)
132             EVP_KEYMGMT_free(tmp_keymgmt_tofree);
133     }
134
135     if (provkey == NULL) {
136         EVP_ASYM_CIPHER_free(cipher);
137         goto legacy;
138     }
139
140     ERR_pop_to_mark();
141
142     /* No more legacy from here down to legacy: */
143
144     ctx->op.ciph.cipher = cipher;
145     ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
146     if (ctx->op.ciph.algctx == NULL) {
147         /* The provider key can stay in the cache */
148         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
149         goto err;
150     }
151
152     switch (operation) {
153     case EVP_PKEY_OP_ENCRYPT:
154         if (cipher->encrypt_init == NULL) {
155             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
156             ret = -2;
157             goto err;
158         }
159         ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
160         break;
161     case EVP_PKEY_OP_DECRYPT:
162         if (cipher->decrypt_init == NULL) {
163             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
164             ret = -2;
165             goto err;
166         }
167         ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
168         break;
169     default:
170         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
171         goto err;
172     }
173
174     if (ret <= 0)
175         goto err;
176     EVP_KEYMGMT_free(tmp_keymgmt);
177     return 1;
178
179  legacy:
180     /*
181      * If we don't have the full support we need with provided methods,
182      * let's go see if legacy does.
183      */
184     ERR_pop_to_mark();
185     EVP_KEYMGMT_free(tmp_keymgmt);
186     tmp_keymgmt = NULL;
187
188     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
189         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
190         return -2;
191     }
192     switch (ctx->operation) {
193     case EVP_PKEY_OP_ENCRYPT:
194         if (ctx->pmeth->encrypt_init == NULL)
195             return 1;
196         ret = ctx->pmeth->encrypt_init(ctx);
197         break;
198     case EVP_PKEY_OP_DECRYPT:
199         if (ctx->pmeth->decrypt_init == NULL)
200             return 1;
201         ret = ctx->pmeth->decrypt_init(ctx);
202         break;
203     default:
204         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
205         ret = -1;
206     }
207
208  err:
209     if (ret <= 0) {
210         evp_pkey_ctx_free_old_ops(ctx);
211         ctx->operation = EVP_PKEY_OP_UNDEFINED;
212     }
213     EVP_KEYMGMT_free(tmp_keymgmt);
214     return ret;
215 }
216
217 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
218 {
219     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
220 }
221
222 int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
223 {
224     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
225 }
226
227 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
228                      unsigned char *out, size_t *outlen,
229                      const unsigned char *in, size_t inlen)
230 {
231     int ret;
232
233     if (ctx == NULL) {
234         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
235         return -2;
236     }
237
238     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
239         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
240         return -1;
241     }
242
243     if (ctx->op.ciph.algctx == NULL)
244         goto legacy;
245
246     ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.algctx, out, outlen,
247                                        (out == NULL ? 0 : *outlen), in, inlen);
248     return ret;
249
250  legacy:
251     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
252         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
253         return -2;
254     }
255     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
256         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
257 }
258
259 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
260 {
261     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
262 }
263
264 int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
265 {
266     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
267 }
268
269 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
270                      unsigned char *out, size_t *outlen,
271                      const unsigned char *in, size_t inlen)
272 {
273     int ret;
274
275     if (ctx == NULL) {
276         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
277         return -2;
278     }
279
280     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
281         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
282         return -1;
283     }
284
285     if (ctx->op.ciph.algctx == NULL)
286         goto legacy;
287
288     ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.algctx, out, outlen,
289                                        (out == NULL ? 0 : *outlen), in, inlen);
290     return ret;
291
292  legacy:
293     if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
294         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
295         return -2;
296     }
297     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
298         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
299 }
300
301
302 static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
303 {
304     EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
305
306     if (cipher == NULL)
307         return NULL;
308
309     cipher->lock = CRYPTO_THREAD_lock_new();
310     if (cipher->lock == NULL) {
311         ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
312         OPENSSL_free(cipher);
313         return NULL;
314     }
315     cipher->prov = prov;
316     ossl_provider_up_ref(prov);
317     cipher->refcnt = 1;
318
319     return cipher;
320 }
321
322 static void *evp_asym_cipher_from_algorithm(int name_id,
323                                             const OSSL_ALGORITHM *algodef,
324                                             OSSL_PROVIDER *prov)
325 {
326     const OSSL_DISPATCH *fns = algodef->implementation;
327     EVP_ASYM_CIPHER *cipher = NULL;
328     int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
329     int gparamfncnt = 0, sparamfncnt = 0;
330
331     if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
332         ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
333         goto err;
334     }
335
336     cipher->name_id = name_id;
337     if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
338         goto err;
339     cipher->description = algodef->algorithm_description;
340
341     for (; fns->function_id != 0; fns++) {
342         switch (fns->function_id) {
343         case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
344             if (cipher->newctx != NULL)
345                 break;
346             cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
347             ctxfncnt++;
348             break;
349         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
350             if (cipher->encrypt_init != NULL)
351                 break;
352             cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
353             encfncnt++;
354             break;
355         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
356             if (cipher->encrypt != NULL)
357                 break;
358             cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
359             encfncnt++;
360             break;
361         case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
362             if (cipher->decrypt_init != NULL)
363                 break;
364             cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
365             decfncnt++;
366             break;
367         case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
368             if (cipher->decrypt != NULL)
369                 break;
370             cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
371             decfncnt++;
372             break;
373         case OSSL_FUNC_ASYM_CIPHER_FREECTX:
374             if (cipher->freectx != NULL)
375                 break;
376             cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
377             ctxfncnt++;
378             break;
379         case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
380             if (cipher->dupctx != NULL)
381                 break;
382             cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
383             break;
384         case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
385             if (cipher->get_ctx_params != NULL)
386                 break;
387             cipher->get_ctx_params
388                 = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
389             gparamfncnt++;
390             break;
391         case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
392             if (cipher->gettable_ctx_params != NULL)
393                 break;
394             cipher->gettable_ctx_params
395                 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
396             gparamfncnt++;
397             break;
398         case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
399             if (cipher->set_ctx_params != NULL)
400                 break;
401             cipher->set_ctx_params
402                 = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
403             sparamfncnt++;
404             break;
405         case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
406             if (cipher->settable_ctx_params != NULL)
407                 break;
408             cipher->settable_ctx_params
409                 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
410             sparamfncnt++;
411             break;
412         }
413     }
414     if (ctxfncnt != 2
415         || (encfncnt != 0 && encfncnt != 2)
416         || (decfncnt != 0 && decfncnt != 2)
417         || (encfncnt != 2 && decfncnt != 2)
418         || (gparamfncnt != 0 && gparamfncnt != 2)
419         || (sparamfncnt != 0 && sparamfncnt != 2)) {
420         /*
421          * In order to be a consistent set of functions we must have at least
422          * a set of context functions (newctx and freectx) as well as a pair of
423          * "cipher" functions: (encrypt_init, encrypt) or
424          * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
425          * optional, but if one of them is present then the other one must also
426          * be present. The same applies to get_ctx_params and
427          * gettable_ctx_params. The dupctx function is optional.
428          */
429         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
430         goto err;
431     }
432
433     return cipher;
434  err:
435     EVP_ASYM_CIPHER_free(cipher);
436     return NULL;
437 }
438
439 void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
440 {
441     int i;
442
443     if (cipher == NULL)
444         return;
445     CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
446     if (i > 0)
447         return;
448     OPENSSL_free(cipher->type_name);
449     ossl_provider_free(cipher->prov);
450     CRYPTO_THREAD_lock_free(cipher->lock);
451     OPENSSL_free(cipher);
452 }
453
454 int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
455 {
456     int ref = 0;
457
458     CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
459     return 1;
460 }
461
462 OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
463 {
464     return cipher->prov;
465 }
466
467 EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
468                                        const char *properties)
469 {
470     return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
471                              evp_asym_cipher_from_algorithm,
472                              (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
473                              (void (*)(void *))EVP_ASYM_CIPHER_free);
474 }
475
476 EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
477                                                  const char *algorithm,
478                                                  const char *properties)
479 {
480     return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER,
481                                        algorithm, properties,
482                                        evp_asym_cipher_from_algorithm,
483                                        (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
484                                        (void (*)(void *))EVP_ASYM_CIPHER_free);
485 }
486
487 int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
488 {
489     return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
490 }
491
492 int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
493 {
494     return cipher->name_id;
495 }
496
497 const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
498 {
499     return cipher->type_name;
500 }
501
502 const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
503 {
504     return cipher->description;
505 }
506
507 void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
508                                      void (*fn)(EVP_ASYM_CIPHER *cipher,
509                                                 void *arg),
510                                      void *arg)
511 {
512     evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
513                        (void (*)(void *, void *))fn, arg,
514                        evp_asym_cipher_from_algorithm,
515                        (int (*)(void *))EVP_ASYM_CIPHER_up_ref,
516                        (void (*)(void *))EVP_ASYM_CIPHER_free);
517 }
518
519
520 int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
521                                  void (*fn)(const char *name, void *data),
522                                  void *data)
523 {
524     if (cipher->prov != NULL)
525         return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
526
527     return 1;
528 }
529
530 const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
531 {
532     void *provctx;
533
534     if (cip == NULL || cip->gettable_ctx_params == NULL)
535         return NULL;
536
537     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
538     return cip->gettable_ctx_params(NULL, provctx);
539 }
540
541 const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
542 {
543     void *provctx;
544
545     if (cip == NULL || cip->settable_ctx_params == NULL)
546         return NULL;
547
548     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
549     return cip->settable_ctx_params(NULL, provctx);
550 }