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