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