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