5f071a56caffeb4c4da4fc917effdd207f9ff64d
[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 /*
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(out, outsize,
229                                                      tbuf, len,
230                                                      prsactx->client_version,
231                                                      prsactx->alt_version);
232         }
233         OPENSSL_free(tbuf);
234     } else {
235         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
236                                   prsactx->pad_mode);
237     }
238     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
239     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
240     return ret;
241 }
242
243 static void rsa_freectx(void *vprsactx)
244 {
245     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
246
247     RSA_free(prsactx->rsa);
248
249     EVP_MD_free(prsactx->oaep_md);
250     EVP_MD_free(prsactx->mgf1_md);
251
252     OPENSSL_free(prsactx);
253 }
254
255 static void *rsa_dupctx(void *vprsactx)
256 {
257     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
258     PROV_RSA_CTX *dstctx;
259
260     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
261     if (dstctx == NULL)
262         return NULL;
263
264     *dstctx = *srcctx;
265     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
266         OPENSSL_free(dstctx);
267         return NULL;
268     }
269
270     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
271         RSA_free(dstctx->rsa);
272         OPENSSL_free(dstctx);
273         return NULL;
274     }
275
276     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
277         RSA_free(dstctx->rsa);
278         EVP_MD_free(dstctx->oaep_md);
279         OPENSSL_free(dstctx);
280         return NULL;
281     }
282
283     return dstctx;
284 }
285
286 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
287 {
288     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
289     OSSL_PARAM *p;
290
291     if (prsactx == NULL || params == NULL)
292         return 0;
293
294     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
295     if (p != NULL)
296         switch (p->data_type) {
297         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
298             if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
299                 return 0;
300             break;
301         case OSSL_PARAM_UTF8_STRING:
302             {
303                 int i;
304                 const char *word = NULL;
305
306                 for (i = 0; padding_item[i].id != 0; i++) {
307                     if (prsactx->pad_mode == (int)padding_item[i].id) {
308                         word = padding_item[i].ptr;
309                         break;
310                     }
311                 }
312
313                 if (word != NULL) {
314                     if (!OSSL_PARAM_set_utf8_string(p, word))
315                         return 0;
316                 } else {
317                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
318                 }
319             }
320             break;
321         default:
322             return 0;
323         }
324
325     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
326     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
327                                                     ? ""
328                                                     : EVP_MD_name(prsactx->oaep_md)))
329         return 0;
330
331     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
332     if (p != NULL) {
333         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
334                                                    : prsactx->mgf1_md;
335
336         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
337                                            ? ""
338                                            : EVP_MD_name(mgf1_md)))
339         return 0;
340     }
341
342     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
343     if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
344         return 0;
345
346     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
347     if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
348         return 0;
349
350     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
351     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
352         return 0;
353
354     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
355     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
356         return 0;
357
358     return 1;
359 }
360
361 static const OSSL_PARAM known_gettable_ctx_params[] = {
362     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
363     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
364     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
365     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
366                     NULL, 0),
367     OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
368     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
369     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
370     OSSL_PARAM_END
371 };
372
373 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
374 {
375     return known_gettable_ctx_params;
376 }
377
378 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
379 {
380     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
381     const OSSL_PARAM *p;
382     char mdname[OSSL_MAX_NAME_SIZE];
383     char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
384     char *str = mdname;
385
386     if (prsactx == NULL || params == NULL)
387         return 0;
388
389     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
390     if (p != NULL) {
391         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
392             return 0;
393
394         str = mdprops;
395         p = OSSL_PARAM_locate_const(params,
396                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
397         if (p != NULL) {
398             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
399                 return 0;
400         }
401
402         EVP_MD_free(prsactx->oaep_md);
403         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
404
405         if (prsactx->oaep_md == NULL)
406             return 0;
407     }
408
409     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
410     if (p != NULL) {
411         int pad_mode = 0;
412
413         switch (p->data_type) {
414         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
415             if (!OSSL_PARAM_get_int(p, &pad_mode))
416                 return 0;
417             break;
418         case OSSL_PARAM_UTF8_STRING:
419             {
420                 int i;
421
422                 if (p->data == NULL)
423                     return 0;
424
425                 for (i = 0; padding_item[i].id != 0; i++) {
426                     if (strcmp(p->data, padding_item[i].ptr) == 0) {
427                         pad_mode = padding_item[i].id;
428                         break;
429                     }
430                 }
431             }
432             break;
433         default:
434             return 0;
435         }
436
437         /*
438          * PSS padding is for signatures only so is not compatible with
439          * asymmetric cipher use.
440          */
441         if (pad_mode == RSA_PKCS1_PSS_PADDING)
442             return 0;
443         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
444             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
445             if (prsactx->oaep_md == NULL)
446                 return 0;
447         }
448         prsactx->pad_mode = pad_mode;
449     }
450
451     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
452     if (p != NULL) {
453         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
454             return 0;
455
456         str = mdprops;
457         p = OSSL_PARAM_locate_const(params,
458                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
459         if (p != NULL) {
460             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
461                 return 0;
462         } else {
463             str = NULL;
464         }
465
466         EVP_MD_free(prsactx->mgf1_md);
467         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
468
469         if (prsactx->mgf1_md == NULL)
470             return 0;
471     }
472
473     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
474     if (p != NULL) {
475         void *tmp_label = NULL;
476         size_t tmp_labellen;
477
478         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
479             return 0;
480         OPENSSL_free(prsactx->oaep_label);
481         prsactx->oaep_label = (unsigned char *)tmp_label;
482         prsactx->oaep_labellen = tmp_labellen;
483     }
484
485     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
486     if (p != NULL) {
487         unsigned int client_version;
488
489         if (!OSSL_PARAM_get_uint(p, &client_version))
490             return 0;
491         prsactx->client_version = client_version;
492     }
493
494     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
495     if (p != NULL) {
496         unsigned int alt_version;
497
498         if (!OSSL_PARAM_get_uint(p, &alt_version))
499             return 0;
500         prsactx->alt_version = alt_version;
501     }
502
503     return 1;
504 }
505
506 static const OSSL_PARAM known_settable_ctx_params[] = {
507     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
508     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
509     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
510     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
511     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
512     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
513     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
514     OSSL_PARAM_END
515 };
516
517 static const OSSL_PARAM *rsa_settable_ctx_params(void)
518 {
519     return known_settable_ctx_params;
520 }
521
522 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
523     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
524     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
525     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
526     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
527     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
528     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
529     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
530     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
531       (void (*)(void))rsa_get_ctx_params },
532     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
533       (void (*)(void))rsa_gettable_ctx_params },
534     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
535       (void (*)(void))rsa_set_ctx_params },
536     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
537       (void (*)(void))rsa_settable_ctx_params },
538     { 0, NULL }
539 };