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