Update core_names.h fields and document most fields.
[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,        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 = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
142                                               prsactx->oaep_label,
143                                               prsactx->oaep_labellen,
144                                               prsactx->oaep_md,
145                                               prsactx->mgf1_md);
146
147         if (!ret) {
148             OPENSSL_free(tbuf);
149             return 0;
150         }
151         ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
152                                  RSA_NO_PADDING);
153         OPENSSL_free(tbuf);
154     } else {
155         ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
156                                  prsactx->pad_mode);
157     }
158     /* A ret value of 0 is not an error */
159     if (ret < 0)
160         return ret;
161     *outlen = ret;
162     return 1;
163 }
164
165 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
166                        size_t outsize, const unsigned char *in, size_t inlen)
167 {
168     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
169     int ret;
170     size_t len = RSA_size(prsactx->rsa);
171
172     if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
173         if (out == NULL) {
174             *outlen = SSL_MAX_MASTER_KEY_LENGTH;
175             return 1;
176         }
177         if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
178             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
179             return 0;
180         }
181     } else {
182         if (out == NULL) {
183             if (len == 0) {
184                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
185                 return 0;
186             }
187             *outlen = len;
188             return 1;
189         }
190
191         if (outsize < len) {
192             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
193             return 0;
194         }
195     }
196
197     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
198             || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
199         unsigned char *tbuf;
200
201         if ((tbuf = OPENSSL_malloc(len)) == NULL) {
202             PROVerr(0, ERR_R_MALLOC_FAILURE);
203             return 0;
204         }
205         ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
206                                   RSA_NO_PADDING);
207         /*
208          * With no padding then, on success ret should be len, otherwise an
209          * error occurred (non-constant time)
210          */
211         if (ret != (int)len) {
212             OPENSSL_free(tbuf);
213             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
214             return 0;
215         }
216         if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
217             if (prsactx->oaep_md == NULL) {
218                 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
219                 if (prsactx->oaep_md == NULL) {
220                     PROVerr(0, ERR_R_INTERNAL_ERROR);
221                     return 0;
222                 }
223             }
224             ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
225                                                     len, len,
226                                                     prsactx->oaep_label,
227                                                     prsactx->oaep_labellen,
228                                                     prsactx->oaep_md,
229                                                     prsactx->mgf1_md);
230         } else {
231             /* RSA_PKCS1_WITH_TLS_PADDING */
232             if (prsactx->client_version <= 0) {
233                 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
234                 return 0;
235             }
236             ret = rsa_padding_check_PKCS1_type_2_TLS(prsactx->libctx, out,
237                                                      outsize,
238                                                      tbuf, len,
239                                                      prsactx->client_version,
240                                                      prsactx->alt_version);
241         }
242         OPENSSL_free(tbuf);
243     } else {
244         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
245                                   prsactx->pad_mode);
246     }
247     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
248     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
249     return ret;
250 }
251
252 static void rsa_freectx(void *vprsactx)
253 {
254     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
255
256     RSA_free(prsactx->rsa);
257
258     EVP_MD_free(prsactx->oaep_md);
259     EVP_MD_free(prsactx->mgf1_md);
260
261     OPENSSL_free(prsactx);
262 }
263
264 static void *rsa_dupctx(void *vprsactx)
265 {
266     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
267     PROV_RSA_CTX *dstctx;
268
269     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
270     if (dstctx == NULL)
271         return NULL;
272
273     *dstctx = *srcctx;
274     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
275         OPENSSL_free(dstctx);
276         return NULL;
277     }
278
279     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
280         RSA_free(dstctx->rsa);
281         OPENSSL_free(dstctx);
282         return NULL;
283     }
284
285     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
286         RSA_free(dstctx->rsa);
287         EVP_MD_free(dstctx->oaep_md);
288         OPENSSL_free(dstctx);
289         return NULL;
290     }
291
292     return dstctx;
293 }
294
295 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
296 {
297     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
298     OSSL_PARAM *p;
299
300     if (prsactx == NULL || params == NULL)
301         return 0;
302
303     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
304     if (p != NULL)
305         switch (p->data_type) {
306         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
307             if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
308                 return 0;
309             break;
310         case OSSL_PARAM_UTF8_STRING:
311             {
312                 int i;
313                 const char *word = NULL;
314
315                 for (i = 0; padding_item[i].id != 0; i++) {
316                     if (prsactx->pad_mode == (int)padding_item[i].id) {
317                         word = padding_item[i].ptr;
318                         break;
319                     }
320                 }
321
322                 if (word != NULL) {
323                     if (!OSSL_PARAM_set_utf8_string(p, word))
324                         return 0;
325                 } else {
326                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
327                 }
328             }
329             break;
330         default:
331             return 0;
332         }
333
334     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
335     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
336                                                     ? ""
337                                                     : EVP_MD_name(prsactx->oaep_md)))
338         return 0;
339
340     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
341     if (p != NULL) {
342         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
343                                                    : prsactx->mgf1_md;
344
345         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
346                                            ? ""
347                                            : EVP_MD_name(mgf1_md)))
348         return 0;
349     }
350
351     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
352     if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
353         return 0;
354
355     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
356     if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
357         return 0;
358
359     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
360     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
361         return 0;
362
363     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
364     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
365         return 0;
366
367     return 1;
368 }
369
370 static const OSSL_PARAM known_gettable_ctx_params[] = {
371     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
372     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
373     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
374     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
375                     NULL, 0),
376     OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
377     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
378     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
379     OSSL_PARAM_END
380 };
381
382 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
383 {
384     return known_gettable_ctx_params;
385 }
386
387 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
388 {
389     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
390     const OSSL_PARAM *p;
391     char mdname[OSSL_MAX_NAME_SIZE];
392     char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
393     char *str = mdname;
394
395     if (prsactx == NULL || params == NULL)
396         return 0;
397
398     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
399     if (p != NULL) {
400         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
401             return 0;
402
403         str = mdprops;
404         p = OSSL_PARAM_locate_const(params,
405                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
406         if (p != NULL) {
407             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
408                 return 0;
409         }
410
411         EVP_MD_free(prsactx->oaep_md);
412         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
413
414         if (prsactx->oaep_md == NULL)
415             return 0;
416     }
417
418     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
419     if (p != NULL) {
420         int pad_mode = 0;
421
422         switch (p->data_type) {
423         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
424             if (!OSSL_PARAM_get_int(p, &pad_mode))
425                 return 0;
426             break;
427         case OSSL_PARAM_UTF8_STRING:
428             {
429                 int i;
430
431                 if (p->data == NULL)
432                     return 0;
433
434                 for (i = 0; padding_item[i].id != 0; i++) {
435                     if (strcmp(p->data, padding_item[i].ptr) == 0) {
436                         pad_mode = padding_item[i].id;
437                         break;
438                     }
439                 }
440             }
441             break;
442         default:
443             return 0;
444         }
445
446         /*
447          * PSS padding is for signatures only so is not compatible with
448          * asymmetric cipher use.
449          */
450         if (pad_mode == RSA_PKCS1_PSS_PADDING)
451             return 0;
452         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
453             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
454             if (prsactx->oaep_md == NULL)
455                 return 0;
456         }
457         prsactx->pad_mode = pad_mode;
458     }
459
460     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
461     if (p != NULL) {
462         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
463             return 0;
464
465         str = mdprops;
466         p = OSSL_PARAM_locate_const(params,
467                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
468         if (p != NULL) {
469             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
470                 return 0;
471         } else {
472             str = NULL;
473         }
474
475         EVP_MD_free(prsactx->mgf1_md);
476         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
477
478         if (prsactx->mgf1_md == NULL)
479             return 0;
480     }
481
482     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
483     if (p != NULL) {
484         void *tmp_label = NULL;
485         size_t tmp_labellen;
486
487         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
488             return 0;
489         OPENSSL_free(prsactx->oaep_label);
490         prsactx->oaep_label = (unsigned char *)tmp_label;
491         prsactx->oaep_labellen = tmp_labellen;
492     }
493
494     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
495     if (p != NULL) {
496         unsigned int client_version;
497
498         if (!OSSL_PARAM_get_uint(p, &client_version))
499             return 0;
500         prsactx->client_version = client_version;
501     }
502
503     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
504     if (p != NULL) {
505         unsigned int alt_version;
506
507         if (!OSSL_PARAM_get_uint(p, &alt_version))
508             return 0;
509         prsactx->alt_version = alt_version;
510     }
511
512     return 1;
513 }
514
515 static const OSSL_PARAM known_settable_ctx_params[] = {
516     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
517     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
518     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
519     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
520     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
521     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
522     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
523     OSSL_PARAM_END
524 };
525
526 static const OSSL_PARAM *rsa_settable_ctx_params(void)
527 {
528     return known_settable_ctx_params;
529 }
530
531 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
532     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
533     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
534     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
535     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
536     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
537     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
538     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
539     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
540       (void (*)(void))rsa_get_ctx_params },
541     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
542       (void (*)(void))rsa_gettable_ctx_params },
543     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
544       (void (*)(void))rsa_set_ctx_params },
545     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
546       (void (*)(void))rsa_settable_ctx_params },
547     { 0, NULL }
548 };