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