Coverity 1458439: fix resource leak
[openssl.git] / providers / implementations / asymciphers / rsa_enc.c
1 /*
2  * Copyright 2019 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 <openssl/crypto.h>
11 #include <openssl/evp.h>
12 #include <openssl/core_numbers.h>
13 #include <openssl/core_names.h>
14 #include <openssl/rsa.h>
15 #include <openssl/params.h>
16 #include <openssl/err.h>
17 /* Just for SSL_MAX_MASTER_KEY_LENGTH */
18 #include <openssl/ssl.h>
19 #include "internal/constant_time.h"
20 #include "internal/sizes.h"
21 #include "crypto/rsa.h"
22 #include "prov/providercommonerr.h"
23 #include "prov/provider_ctx.h"
24 #include "prov/implementations.h"
25
26 #include <stdlib.h>
27
28 static OSSL_OP_asym_cipher_newctx_fn rsa_newctx;
29 static OSSL_OP_asym_cipher_encrypt_init_fn rsa_init;
30 static OSSL_OP_asym_cipher_encrypt_fn rsa_encrypt;
31 static OSSL_OP_asym_cipher_decrypt_init_fn rsa_init;
32 static OSSL_OP_asym_cipher_decrypt_fn rsa_decrypt;
33 static OSSL_OP_asym_cipher_freectx_fn rsa_freectx;
34 static OSSL_OP_asym_cipher_dupctx_fn rsa_dupctx;
35 static OSSL_OP_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
36 static OSSL_OP_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
37 static OSSL_OP_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
38 static OSSL_OP_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
39
40 static OSSL_ITEM padding_item[] = {
41     { RSA_PKCS1_PADDING,        "pkcs1"  },
42     { RSA_SSLV23_PADDING,       "sslv23" },
43     { RSA_NO_PADDING,           "none"   },
44     { RSA_PKCS1_OAEP_PADDING,   "oaep"   }, /* Correct spelling first */
45     { RSA_PKCS1_OAEP_PADDING,   "oeap"   },
46     { RSA_X931_PADDING,         "x931"   },
47     { RSA_PKCS1_PSS_PADDING,    "pss"    },
48     { 0,                        NULL     }
49 };
50
51 /*
52  * What's passed as an actual key is defined by the KEYMGMT interface.
53  * We happen to know that our KEYMGMT simply passes RSA structures, so
54  * we use that here too.
55  */
56
57 typedef struct {
58     OPENSSL_CTX *libctx;
59     RSA *rsa;
60     int pad_mode;
61     /* OAEP message digest */
62     EVP_MD *oaep_md;
63     /* message digest for MGF1 */
64     EVP_MD *mgf1_md;
65     /* OAEP label */
66     unsigned char *oaep_label;
67     size_t oaep_labellen;
68     /* TLS padding */
69     unsigned int client_version;
70     unsigned int alt_version;
71 } PROV_RSA_CTX;
72
73 static void *rsa_newctx(void *provctx)
74 {
75     PROV_RSA_CTX *prsactx =  OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
76
77     if (prsactx == NULL)
78         return NULL;
79     prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
80
81     return prsactx;
82 }
83
84 static int rsa_init(void *vprsactx, void *vrsa)
85 {
86     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
87
88     if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
89         return 0;
90     RSA_free(prsactx->rsa);
91     prsactx->rsa = vrsa;
92     prsactx->pad_mode = RSA_PKCS1_PADDING;
93     return 1;
94 }
95
96 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
97                        size_t outsize, const unsigned char *in, size_t inlen)
98 {
99     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
100     int ret;
101
102     if (out == NULL) {
103         size_t len = RSA_size(prsactx->rsa);
104
105         if (len == 0) {
106             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
107             return 0;
108         }
109         *outlen = len;
110         return 1;
111     }
112
113     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
114         int rsasize = RSA_size(prsactx->rsa);
115         unsigned char *tbuf;
116
117         if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
118             PROVerr(0, ERR_R_MALLOC_FAILURE);
119             return 0;
120         }
121         if (prsactx->oaep_md == NULL) {
122             OPENSSL_free(tbuf);
123             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
124             PROVerr(0, ERR_R_INTERNAL_ERROR);
125             return 0;
126         }
127         ret = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
128                                               prsactx->oaep_label,
129                                               prsactx->oaep_labellen,
130                                               prsactx->oaep_md,
131                                               prsactx->mgf1_md);
132
133         if (!ret) {
134             OPENSSL_free(tbuf);
135             return 0;
136         }
137         ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
138                                  RSA_NO_PADDING);
139         OPENSSL_free(tbuf);
140     } else {
141         ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
142                                  prsactx->pad_mode);
143     }
144     /* A ret value of 0 is not an error */
145     if (ret < 0)
146         return ret;
147     *outlen = ret;
148     return 1;
149 }
150
151 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
152                        size_t outsize, const unsigned char *in, size_t inlen)
153 {
154     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
155     int ret;
156     size_t len = RSA_size(prsactx->rsa);
157
158     if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
159         if (out == NULL) {
160             *outlen = SSL_MAX_MASTER_KEY_LENGTH;
161             return 1;
162         }
163         if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
164             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
165             return 0;
166         }
167     } else {
168         if (out == NULL) {
169             if (len == 0) {
170                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
171                 return 0;
172             }
173             *outlen = len;
174             return 1;
175         }
176
177         if (outsize < len) {
178             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
179             return 0;
180         }
181     }
182
183     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
184             || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
185         unsigned char *tbuf;
186
187         if ((tbuf = OPENSSL_malloc(len)) == NULL) {
188             PROVerr(0, ERR_R_MALLOC_FAILURE);
189             return 0;
190         }
191         ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
192                                   RSA_NO_PADDING);
193         /*
194          * With no padding then, on success ret should be len, otherwise an
195          * error occurred (non-constant time)
196          */
197         if (ret != (int)len) {
198             OPENSSL_free(tbuf);
199             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
200             return 0;
201         }
202         if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
203             if (prsactx->oaep_md == NULL) {
204                 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
205                 if (prsactx->oaep_md == NULL) {
206                     PROVerr(0, ERR_R_INTERNAL_ERROR);
207                     return 0;
208                 }
209             }
210             ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
211                                                     len, len,
212                                                     prsactx->oaep_label,
213                                                     prsactx->oaep_labellen,
214                                                     prsactx->oaep_md,
215                                                     prsactx->mgf1_md);
216         } else {
217             /* RSA_PKCS1_WITH_TLS_PADDING */
218             if (prsactx->client_version <= 0) {
219                 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
220                 return 0;
221             }
222             ret = rsa_padding_check_PKCS1_type_2_TLS(out, outsize,
223                                                      tbuf, len,
224                                                      prsactx->client_version,
225                                                      prsactx->alt_version);
226         }
227         OPENSSL_free(tbuf);
228     } else {
229         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
230                                   prsactx->pad_mode);
231     }
232     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
233     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
234     return ret;
235 }
236
237 static void rsa_freectx(void *vprsactx)
238 {
239     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
240
241     RSA_free(prsactx->rsa);
242
243     EVP_MD_free(prsactx->oaep_md);
244     EVP_MD_free(prsactx->mgf1_md);
245
246     OPENSSL_free(prsactx);
247 }
248
249 static void *rsa_dupctx(void *vprsactx)
250 {
251     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
252     PROV_RSA_CTX *dstctx;
253
254     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
255     if (dstctx == NULL)
256         return NULL;
257
258     *dstctx = *srcctx;
259     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
260         OPENSSL_free(dstctx);
261         return NULL;
262     }
263
264     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
265         RSA_free(dstctx->rsa);
266         OPENSSL_free(dstctx);
267         return NULL;
268     }
269
270     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
271         RSA_free(dstctx->rsa);
272         EVP_MD_free(dstctx->oaep_md);
273         OPENSSL_free(dstctx);
274         return NULL;
275     }
276
277     return dstctx;
278 }
279
280 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
281 {
282     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
283     OSSL_PARAM *p;
284
285     if (prsactx == NULL || params == NULL)
286         return 0;
287
288     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
289     if (p != NULL)
290         switch (p->data_type) {
291         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
292             if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
293                 return 0;
294             break;
295         case OSSL_PARAM_UTF8_STRING:
296             {
297                 int i;
298                 const char *word = NULL;
299
300                 for (i = 0; padding_item[i].id != 0; i++) {
301                     if (prsactx->pad_mode == (int)padding_item[i].id) {
302                         word = padding_item[i].ptr;
303                         break;
304                     }
305                 }
306
307                 if (word != NULL) {
308                     if (!OSSL_PARAM_set_utf8_string(p, word))
309                         return 0;
310                 } else {
311                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
312                 }
313             }
314             break;
315         default:
316             return 0;
317         }
318
319     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
320     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
321                                                     ? ""
322                                                     : EVP_MD_name(prsactx->oaep_md)))
323         return 0;
324
325     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
326     if (p != NULL) {
327         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
328                                                    : prsactx->mgf1_md;
329
330         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
331                                            ? ""
332                                            : EVP_MD_name(mgf1_md)))
333         return 0;
334     }
335
336     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
337     if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
338         return 0;
339
340     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
341     if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
342         return 0;
343
344     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
345     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
346         return 0;
347
348     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
349     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
350         return 0;
351
352     return 1;
353 }
354
355 static const OSSL_PARAM known_gettable_ctx_params[] = {
356     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
357     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
358     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
359     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
360                     NULL, 0),
361     OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
362     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
363     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
364     OSSL_PARAM_END
365 };
366
367 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
368 {
369     return known_gettable_ctx_params;
370 }
371
372 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
373 {
374     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
375     const OSSL_PARAM *p;
376     char mdname[OSSL_MAX_NAME_SIZE];
377     char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
378     char *str = mdname;
379
380     if (prsactx == NULL || params == NULL)
381         return 0;
382
383     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
384     if (p != NULL) {
385         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
386             return 0;
387
388         str = mdprops;
389         p = OSSL_PARAM_locate_const(params,
390                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
391         if (p != NULL) {
392             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
393                 return 0;
394         }
395
396         EVP_MD_free(prsactx->oaep_md);
397         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
398
399         if (prsactx->oaep_md == NULL)
400             return 0;
401     }
402
403     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
404     if (p != NULL) {
405         int pad_mode = 0;
406
407         switch (p->data_type) {
408         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
409             if (!OSSL_PARAM_get_int(p, &pad_mode))
410                 return 0;
411             break;
412         case OSSL_PARAM_UTF8_STRING:
413             {
414                 int i;
415
416                 if (p->data == NULL)
417                     return 0;
418
419                 for (i = 0; padding_item[i].id != 0; i++) {
420                     if (strcmp(p->data, padding_item[i].ptr) == 0) {
421                         pad_mode = padding_item[i].id;
422                         break;
423                     }
424                 }
425             }
426             break;
427         default:
428             return 0;
429         }
430
431         /*
432          * PSS padding is for signatures only so is not compatible with
433          * asymmetric cipher use.
434          */
435         if (pad_mode == RSA_PKCS1_PSS_PADDING)
436             return 0;
437         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
438             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
439             if (prsactx->oaep_md == NULL)
440                 return 0;
441         }
442         prsactx->pad_mode = pad_mode;
443     }
444
445     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
446     if (p != NULL) {
447         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
448             return 0;
449
450         str = mdprops;
451         p = OSSL_PARAM_locate_const(params,
452                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
453         if (p != NULL) {
454             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
455                 return 0;
456         } else {
457             str = NULL;
458         }
459
460         EVP_MD_free(prsactx->mgf1_md);
461         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
462
463         if (prsactx->mgf1_md == NULL)
464             return 0;
465     }
466
467     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
468     if (p != NULL) {
469         void *tmp_label = NULL;
470         size_t tmp_labellen;
471
472         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
473             return 0;
474         OPENSSL_free(prsactx->oaep_label);
475         prsactx->oaep_label = (unsigned char *)tmp_label;
476         prsactx->oaep_labellen = tmp_labellen;
477     }
478
479     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
480     if (p != NULL) {
481         unsigned int client_version;
482
483         if (!OSSL_PARAM_get_uint(p, &client_version))
484             return 0;
485         prsactx->client_version = client_version;
486     }
487
488     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
489     if (p != NULL) {
490         unsigned int alt_version;
491
492         if (!OSSL_PARAM_get_uint(p, &alt_version))
493             return 0;
494         prsactx->alt_version = alt_version;
495     }
496
497     return 1;
498 }
499
500 static const OSSL_PARAM known_settable_ctx_params[] = {
501     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
502     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
503     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
504     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
505     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
506     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
507     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
508     OSSL_PARAM_END
509 };
510
511 static const OSSL_PARAM *rsa_settable_ctx_params(void)
512 {
513     return known_settable_ctx_params;
514 }
515
516 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
517     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
518     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
519     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
520     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
521     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
522     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
523     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
524     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
525       (void (*)(void))rsa_get_ctx_params },
526     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
527       (void (*)(void))rsa_gettable_ctx_params },
528     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
529       (void (*)(void))rsa_set_ctx_params },
530     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
531       (void (*)(void))rsa_settable_ctx_params },
532     { 0, NULL }
533 };