Add internal maxsize macros
[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
41 /*
42  * What's passed as an actual key is defined by the KEYMGMT interface.
43  * We happen to know that our KEYMGMT simply passes RSA structures, so
44  * we use that here too.
45  */
46
47 typedef struct {
48     OPENSSL_CTX *libctx;
49     RSA *rsa;
50     int pad_mode;
51     /* OAEP message digest */
52     EVP_MD *oaep_md;
53     /* message digest for MGF1 */
54     EVP_MD *mgf1_md;
55     /* OAEP label */
56     unsigned char *oaep_label;
57     size_t oaep_labellen;
58     /* TLS padding */
59     unsigned int client_version;
60     unsigned int alt_version;
61 } PROV_RSA_CTX;
62
63 static void *rsa_newctx(void *provctx)
64 {
65     PROV_RSA_CTX *prsactx =  OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
66
67     if (prsactx == NULL)
68         return NULL;
69     prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
70
71     return prsactx;
72 }
73
74 static int rsa_init(void *vprsactx, void *vrsa)
75 {
76     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
77
78     if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
79         return 0;
80     RSA_free(prsactx->rsa);
81     prsactx->rsa = vrsa;
82     prsactx->pad_mode = RSA_PKCS1_PADDING;
83     return 1;
84 }
85
86 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
87                        size_t outsize, const unsigned char *in, size_t inlen)
88 {
89     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
90     int ret;
91
92     if (out == NULL) {
93         size_t len = RSA_size(prsactx->rsa);
94
95         if (len == 0) {
96             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
97             return 0;
98         }
99         *outlen = len;
100         return 1;
101     }
102
103     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
104         int rsasize = RSA_size(prsactx->rsa);
105         unsigned char *tbuf;
106
107         if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
108             PROVerr(0, ERR_R_MALLOC_FAILURE);
109             return 0;
110         }
111         ret = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
112                                               prsactx->oaep_label,
113                                               prsactx->oaep_labellen,
114                                               prsactx->oaep_md,
115                                               prsactx->mgf1_md);
116
117         if (!ret) {
118             OPENSSL_free(tbuf);
119             return 0;
120         }
121         ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
122                                  RSA_NO_PADDING);
123         OPENSSL_free(tbuf);
124     } else {
125         ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
126                                  prsactx->pad_mode);
127     }
128     /* A ret value of 0 is not an error */
129     if (ret < 0)
130         return ret;
131     *outlen = ret;
132     return 1;
133 }
134
135 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
136                        size_t outsize, const unsigned char *in, size_t inlen)
137 {
138     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
139     int ret;
140     size_t len = RSA_size(prsactx->rsa);
141
142     if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
143         if (out == NULL) {
144             *outlen = SSL_MAX_MASTER_KEY_LENGTH;
145             return 1;
146         }
147         if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
148             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
149             return 0;
150         }
151     } else {
152         if (out == NULL) {
153             if (len == 0) {
154                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
155                 return 0;
156             }
157             *outlen = len;
158             return 1;
159         }
160
161         if (outsize < len) {
162             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
163             return 0;
164         }
165     }
166
167     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
168             || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
169         unsigned char *tbuf;
170
171         if ((tbuf = OPENSSL_malloc(len)) == NULL) {
172             PROVerr(0, ERR_R_MALLOC_FAILURE);
173             return 0;
174         }
175         ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
176                                   RSA_NO_PADDING);
177         /*
178          * With no padding then, on success ret should be len, otherwise an
179          * error occurred (non-constant time)
180          */
181         if (ret != (int)len) {
182             OPENSSL_free(tbuf);
183             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
184             return 0;
185         }
186         if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
187             ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
188                                                     len, len,
189                                                     prsactx->oaep_label,
190                                                     prsactx->oaep_labellen,
191                                                     prsactx->oaep_md,
192                                                     prsactx->mgf1_md);
193         } else {
194             /* RSA_PKCS1_WITH_TLS_PADDING */
195             if (prsactx->client_version <= 0) {
196                 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
197                 return 0;
198             }
199             ret = rsa_padding_check_PKCS1_type_2_TLS(out, outsize,
200                                                      tbuf, len,
201                                                      prsactx->client_version,
202                                                      prsactx->alt_version);
203         }
204         OPENSSL_free(tbuf);
205     } else {
206         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
207                                   prsactx->pad_mode);
208     }
209     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
210     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
211     return ret;
212 }
213
214 static void rsa_freectx(void *vprsactx)
215 {
216     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
217
218     RSA_free(prsactx->rsa);
219
220     EVP_MD_free(prsactx->oaep_md);
221     EVP_MD_free(prsactx->mgf1_md);
222
223     OPENSSL_free(prsactx);
224 }
225
226 static void *rsa_dupctx(void *vprsactx)
227 {
228     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
229     PROV_RSA_CTX *dstctx;
230
231     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
232     if (dstctx == NULL)
233         return NULL;
234
235     *dstctx = *srcctx;
236     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
237         OPENSSL_free(dstctx);
238         return NULL;
239     }
240
241     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
242         RSA_free(dstctx->rsa);
243         OPENSSL_free(dstctx);
244         return NULL;
245     }
246
247     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
248         RSA_free(dstctx->rsa);
249         EVP_MD_free(dstctx->oaep_md);
250         OPENSSL_free(dstctx);
251         return NULL;
252     }
253
254     return dstctx;
255 }
256
257 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
258 {
259     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
260     OSSL_PARAM *p;
261
262     if (prsactx == NULL || params == NULL)
263         return 0;
264
265     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
266     if (p != NULL && !OSSL_PARAM_set_int(p, prsactx->pad_mode))
267         return 0;
268
269     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
270     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
271                                                     ? ""
272                                                     : EVP_MD_name(prsactx->oaep_md)))
273         return 0;
274
275     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
276     if (p != NULL) {
277         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
278                                                    : prsactx->mgf1_md;
279
280         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
281                                            ? ""
282                                            : EVP_MD_name(mgf1_md)))
283         return 0;
284     }
285
286     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
287     if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
288         return 0;
289
290     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
291     if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
292         return 0;
293
294     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
295     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
296         return 0;
297
298     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
299     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
300         return 0;
301
302     return 1;
303 }
304
305 static const OSSL_PARAM known_gettable_ctx_params[] = {
306     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
307     OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
308     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
309     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
310                     NULL, 0),
311     OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
312     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
313     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
314     OSSL_PARAM_END
315 };
316
317 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
318 {
319     return known_gettable_ctx_params;
320 }
321
322 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
323 {
324     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
325     const OSSL_PARAM *p;
326     char mdname[OSSL_MAX_NAME_SIZE];
327     char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
328     char *str = mdname;
329     int pad_mode;
330
331     if (prsactx == NULL || params == NULL)
332         return 0;
333
334     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
335     if (p != NULL) {
336         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
337             return 0;
338
339         str = mdprops;
340         p = OSSL_PARAM_locate_const(params,
341                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
342         if (p != NULL) {
343             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
344                 return 0;
345         }
346
347         EVP_MD_free(prsactx->oaep_md);
348         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
349
350         if (prsactx->oaep_md == NULL)
351             return 0;
352     }
353
354     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
355     if (p != NULL) {
356         if (!OSSL_PARAM_get_int(p, &pad_mode))
357             return 0;
358         /*
359          * PSS padding is for signatures only so is not compatible with
360          * asymmetric cipher use.
361          */
362         if (pad_mode == RSA_PKCS1_PSS_PADDING)
363             return 0;
364         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
365             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
366             if (prsactx->oaep_md == NULL)
367                 return 0;
368         }
369         prsactx->pad_mode = pad_mode;
370     }
371
372     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
373     if (p != NULL) {
374         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
375             return 0;
376
377         str = mdprops;
378         p = OSSL_PARAM_locate_const(params,
379                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
380         if (p != NULL) {
381             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
382                 return 0;
383         } else {
384             str = NULL;
385         }
386
387         EVP_MD_free(prsactx->mgf1_md);
388         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
389
390         if (prsactx->mgf1_md == NULL)
391             return 0;
392     }
393
394     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
395     if (p != NULL) {
396         void *tmp_label = NULL;
397         size_t tmp_labellen;
398
399         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
400             return 0;
401         OPENSSL_free(prsactx->oaep_label);
402         prsactx->oaep_label = (unsigned char *)tmp_label;
403         prsactx->oaep_labellen = tmp_labellen;
404     }
405
406     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
407     if (p != NULL) {
408         unsigned int client_version;
409
410         if (!OSSL_PARAM_get_uint(p, &client_version))
411             return 0;
412         prsactx->client_version = client_version;
413     }
414
415     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
416     if (p != NULL) {
417         unsigned int alt_version;
418
419         if (!OSSL_PARAM_get_uint(p, &alt_version))
420             return 0;
421         prsactx->alt_version = alt_version;
422     }
423
424     return 1;
425 }
426
427 static const OSSL_PARAM known_settable_ctx_params[] = {
428     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
429     OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
430     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
431     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
432     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
433     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
434     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
435     OSSL_PARAM_END
436 };
437
438 static const OSSL_PARAM *rsa_settable_ctx_params(void)
439 {
440     return known_settable_ctx_params;
441 }
442
443 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
444     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
445     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
446     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
447     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
448     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
449     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
450     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
451     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
452       (void (*)(void))rsa_get_ctx_params },
453     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
454       (void (*)(void))rsa_gettable_ctx_params },
455     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
456       (void (*)(void))rsa_set_ctx_params },
457     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
458       (void (*)(void))rsa_settable_ctx_params },
459     { 0, NULL }
460 };