1b0d17251Schristos /*
24170684fSchristos * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6b0d17251Schristos * in the file LICENSE in the source distribution or at
7b0d17251Schristos * https://www.openssl.org/source/license.html
8b0d17251Schristos */
9b0d17251Schristos
10b0d17251Schristos /*
11b0d17251Schristos * RSA low level APIs are deprecated for public use, but still ok for
12b0d17251Schristos * internal use.
13b0d17251Schristos */
14b0d17251Schristos #include "internal/deprecated.h"
15b0d17251Schristos
16b0d17251Schristos #include <string.h>
17b0d17251Schristos #include <openssl/crypto.h>
18b0d17251Schristos #include <openssl/core_dispatch.h>
19b0d17251Schristos #include <openssl/core_names.h>
20b0d17251Schristos #include <openssl/err.h>
21b0d17251Schristos #include <openssl/rsa.h>
22b0d17251Schristos #include <openssl/params.h>
23b0d17251Schristos #include <openssl/evp.h>
24b0d17251Schristos #include <openssl/proverr.h>
25b0d17251Schristos #include "internal/cryptlib.h"
26b0d17251Schristos #include "internal/nelem.h"
27b0d17251Schristos #include "internal/sizes.h"
28b0d17251Schristos #include "crypto/rsa.h"
29b0d17251Schristos #include "prov/providercommon.h"
30b0d17251Schristos #include "prov/implementations.h"
31b0d17251Schristos #include "prov/provider_ctx.h"
32b0d17251Schristos #include "prov/der_rsa.h"
33b0d17251Schristos #include "prov/securitycheck.h"
34b0d17251Schristos
35b0d17251Schristos #define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
36b0d17251Schristos
37b0d17251Schristos static OSSL_FUNC_signature_newctx_fn rsa_newctx;
38b0d17251Schristos static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
39b0d17251Schristos static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
40b0d17251Schristos static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
41b0d17251Schristos static OSSL_FUNC_signature_sign_fn rsa_sign;
42b0d17251Schristos static OSSL_FUNC_signature_verify_fn rsa_verify;
43b0d17251Schristos static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
44b0d17251Schristos static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
45b0d17251Schristos static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_signverify_update;
46b0d17251Schristos static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
47b0d17251Schristos static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
48b0d17251Schristos static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_signverify_update;
49b0d17251Schristos static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
50b0d17251Schristos static OSSL_FUNC_signature_freectx_fn rsa_freectx;
51b0d17251Schristos static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
52b0d17251Schristos static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
53b0d17251Schristos static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
54b0d17251Schristos static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
55b0d17251Schristos static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
56b0d17251Schristos static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
57b0d17251Schristos static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
58b0d17251Schristos static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
59b0d17251Schristos static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
60b0d17251Schristos
61b0d17251Schristos static OSSL_ITEM padding_item[] = {
62b0d17251Schristos { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
63b0d17251Schristos { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
64b0d17251Schristos { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
65b0d17251Schristos { RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS },
66b0d17251Schristos { 0, NULL }
67b0d17251Schristos };
68b0d17251Schristos
69b0d17251Schristos /*
70b0d17251Schristos * What's passed as an actual key is defined by the KEYMGMT interface.
71b0d17251Schristos * We happen to know that our KEYMGMT simply passes RSA structures, so
72b0d17251Schristos * we use that here too.
73b0d17251Schristos */
74b0d17251Schristos
75b0d17251Schristos typedef struct {
76b0d17251Schristos OSSL_LIB_CTX *libctx;
77b0d17251Schristos char *propq;
78b0d17251Schristos RSA *rsa;
79b0d17251Schristos int operation;
80b0d17251Schristos
81b0d17251Schristos /*
82b0d17251Schristos * Flag to determine if the hash function can be changed (1) or not (0)
83b0d17251Schristos * Because it's dangerous to change during a DigestSign or DigestVerify
84b0d17251Schristos * operation, this flag is cleared by their Init function, and set again
85b0d17251Schristos * by their Final function.
86b0d17251Schristos */
87b0d17251Schristos unsigned int flag_allow_md : 1;
88b0d17251Schristos unsigned int mgf1_md_set : 1;
89b0d17251Schristos
90b0d17251Schristos /* main digest */
91b0d17251Schristos EVP_MD *md;
92b0d17251Schristos EVP_MD_CTX *mdctx;
93b0d17251Schristos int mdnid;
94b0d17251Schristos char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
95b0d17251Schristos
96b0d17251Schristos /* RSA padding mode */
97b0d17251Schristos int pad_mode;
98b0d17251Schristos /* message digest for MGF1 */
99b0d17251Schristos EVP_MD *mgf1_md;
100b0d17251Schristos int mgf1_mdnid;
101b0d17251Schristos char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
102b0d17251Schristos /* PSS salt length */
103b0d17251Schristos int saltlen;
104b0d17251Schristos /* Minimum salt length or -1 if no PSS parameter restriction */
105b0d17251Schristos int min_saltlen;
106b0d17251Schristos
107b0d17251Schristos /* Temp buffer */
108b0d17251Schristos unsigned char *tbuf;
109b0d17251Schristos
110b0d17251Schristos } PROV_RSA_CTX;
111b0d17251Schristos
112b0d17251Schristos /* True if PSS parameters are restricted */
113b0d17251Schristos #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
114b0d17251Schristos
rsa_get_md_size(const PROV_RSA_CTX * prsactx)115b0d17251Schristos static size_t rsa_get_md_size(const PROV_RSA_CTX *prsactx)
116b0d17251Schristos {
117b0d17251Schristos if (prsactx->md != NULL)
118b0d17251Schristos return EVP_MD_get_size(prsactx->md);
119b0d17251Schristos return 0;
120b0d17251Schristos }
121b0d17251Schristos
rsa_check_padding(const PROV_RSA_CTX * prsactx,const char * mdname,const char * mgf1_mdname,int mdnid)122b0d17251Schristos static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
123b0d17251Schristos const char *mdname, const char *mgf1_mdname,
124b0d17251Schristos int mdnid)
125b0d17251Schristos {
126b0d17251Schristos switch(prsactx->pad_mode) {
127b0d17251Schristos case RSA_NO_PADDING:
128b0d17251Schristos if (mdname != NULL || mdnid != NID_undef) {
129b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
130b0d17251Schristos return 0;
131b0d17251Schristos }
132b0d17251Schristos break;
133b0d17251Schristos case RSA_X931_PADDING:
134b0d17251Schristos if (RSA_X931_hash_id(mdnid) == -1) {
135b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
136b0d17251Schristos return 0;
137b0d17251Schristos }
138b0d17251Schristos break;
139b0d17251Schristos case RSA_PKCS1_PSS_PADDING:
140b0d17251Schristos if (rsa_pss_restricted(prsactx))
141b0d17251Schristos if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
142b0d17251Schristos || (mgf1_mdname != NULL
143b0d17251Schristos && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
144b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
145b0d17251Schristos return 0;
146b0d17251Schristos }
147b0d17251Schristos break;
148b0d17251Schristos default:
149b0d17251Schristos break;
150b0d17251Schristos }
151b0d17251Schristos
152b0d17251Schristos return 1;
153b0d17251Schristos }
154b0d17251Schristos
rsa_check_parameters(PROV_RSA_CTX * prsactx,int min_saltlen)155b0d17251Schristos static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
156b0d17251Schristos {
157b0d17251Schristos if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
158b0d17251Schristos int max_saltlen;
159b0d17251Schristos
160b0d17251Schristos /* See if minimum salt length exceeds maximum possible */
161b0d17251Schristos max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
162b0d17251Schristos if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
163b0d17251Schristos max_saltlen--;
164b0d17251Schristos if (min_saltlen < 0 || min_saltlen > max_saltlen) {
165b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
166b0d17251Schristos return 0;
167b0d17251Schristos }
168b0d17251Schristos prsactx->min_saltlen = min_saltlen;
169b0d17251Schristos }
170b0d17251Schristos return 1;
171b0d17251Schristos }
172b0d17251Schristos
rsa_newctx(void * provctx,const char * propq)173b0d17251Schristos static void *rsa_newctx(void *provctx, const char *propq)
174b0d17251Schristos {
175b0d17251Schristos PROV_RSA_CTX *prsactx = NULL;
176b0d17251Schristos char *propq_copy = NULL;
177b0d17251Schristos
178b0d17251Schristos if (!ossl_prov_is_running())
179b0d17251Schristos return NULL;
180b0d17251Schristos
181b0d17251Schristos if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
182b0d17251Schristos || (propq != NULL
183b0d17251Schristos && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
184b0d17251Schristos OPENSSL_free(prsactx);
185b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
186b0d17251Schristos return NULL;
187b0d17251Schristos }
188b0d17251Schristos
189b0d17251Schristos prsactx->libctx = PROV_LIBCTX_OF(provctx);
190b0d17251Schristos prsactx->flag_allow_md = 1;
191b0d17251Schristos prsactx->propq = propq_copy;
192b0d17251Schristos /* Maximum for sign, auto for verify */
193b0d17251Schristos prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
194b0d17251Schristos prsactx->min_saltlen = -1;
195b0d17251Schristos return prsactx;
196b0d17251Schristos }
197b0d17251Schristos
rsa_pss_compute_saltlen(PROV_RSA_CTX * ctx)198b0d17251Schristos static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
199b0d17251Schristos {
200b0d17251Schristos int saltlen = ctx->saltlen;
201b0d17251Schristos
202b0d17251Schristos if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
203b0d17251Schristos saltlen = EVP_MD_get_size(ctx->md);
204b0d17251Schristos } else if (saltlen == RSA_PSS_SALTLEN_AUTO || saltlen == RSA_PSS_SALTLEN_MAX) {
205b0d17251Schristos saltlen = RSA_size(ctx->rsa) - EVP_MD_get_size(ctx->md) - 2;
206b0d17251Schristos if ((RSA_bits(ctx->rsa) & 0x7) == 1)
207b0d17251Schristos saltlen--;
208b0d17251Schristos }
209b0d17251Schristos if (saltlen < 0) {
210b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
211b0d17251Schristos return -1;
212b0d17251Schristos } else if (saltlen < ctx->min_saltlen) {
213b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
214b0d17251Schristos "minimum salt length: %d, actual salt length: %d",
215b0d17251Schristos ctx->min_saltlen, saltlen);
216b0d17251Schristos return -1;
217b0d17251Schristos }
218b0d17251Schristos return saltlen;
219b0d17251Schristos }
220b0d17251Schristos
rsa_generate_signature_aid(PROV_RSA_CTX * ctx,unsigned char * aid_buf,size_t buf_len,size_t * aid_len)221b0d17251Schristos static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
222b0d17251Schristos unsigned char *aid_buf,
223b0d17251Schristos size_t buf_len,
224b0d17251Schristos size_t *aid_len)
225b0d17251Schristos {
226b0d17251Schristos WPACKET pkt;
227b0d17251Schristos unsigned char *aid = NULL;
228b0d17251Schristos int saltlen;
229b0d17251Schristos RSA_PSS_PARAMS_30 pss_params;
230b0d17251Schristos int ret;
231b0d17251Schristos
232b0d17251Schristos if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
233b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
234b0d17251Schristos return NULL;
235b0d17251Schristos }
236b0d17251Schristos
237b0d17251Schristos switch(ctx->pad_mode) {
238b0d17251Schristos case RSA_PKCS1_PADDING:
239b0d17251Schristos ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
240b0d17251Schristos ctx->mdnid);
241b0d17251Schristos
242b0d17251Schristos if (ret > 0) {
243b0d17251Schristos break;
244b0d17251Schristos } else if (ret == 0) {
245b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
246b0d17251Schristos goto cleanup;
247b0d17251Schristos }
248b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
249b0d17251Schristos "Algorithm ID generation - md NID: %d",
250b0d17251Schristos ctx->mdnid);
251b0d17251Schristos goto cleanup;
252b0d17251Schristos case RSA_PKCS1_PSS_PADDING:
253b0d17251Schristos saltlen = rsa_pss_compute_saltlen(ctx);
254b0d17251Schristos if (saltlen < 0)
255b0d17251Schristos goto cleanup;
256b0d17251Schristos if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
257b0d17251Schristos || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
258b0d17251Schristos || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
259b0d17251Schristos ctx->mgf1_mdnid)
260b0d17251Schristos || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
261b0d17251Schristos || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
262b0d17251Schristos RSA_FLAG_TYPE_RSASSAPSS,
263b0d17251Schristos &pss_params)) {
264b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
265b0d17251Schristos goto cleanup;
266b0d17251Schristos }
267b0d17251Schristos break;
268b0d17251Schristos default:
269b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
270b0d17251Schristos "Algorithm ID generation - pad mode: %d",
271b0d17251Schristos ctx->pad_mode);
272b0d17251Schristos goto cleanup;
273b0d17251Schristos }
274b0d17251Schristos if (WPACKET_finish(&pkt)) {
275b0d17251Schristos WPACKET_get_total_written(&pkt, aid_len);
276b0d17251Schristos aid = WPACKET_get_curr(&pkt);
277b0d17251Schristos }
278b0d17251Schristos cleanup:
279b0d17251Schristos WPACKET_cleanup(&pkt);
280b0d17251Schristos return aid;
281b0d17251Schristos }
282b0d17251Schristos
rsa_setup_md(PROV_RSA_CTX * ctx,const char * mdname,const char * mdprops)283b0d17251Schristos static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
284b0d17251Schristos const char *mdprops)
285b0d17251Schristos {
286b0d17251Schristos if (mdprops == NULL)
287b0d17251Schristos mdprops = ctx->propq;
288b0d17251Schristos
289b0d17251Schristos if (mdname != NULL) {
290b0d17251Schristos EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
291b0d17251Schristos int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
292b0d17251Schristos int md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
293b0d17251Schristos sha1_allowed);
294b0d17251Schristos size_t mdname_len = strlen(mdname);
295b0d17251Schristos
296b0d17251Schristos if (md == NULL
297b0d17251Schristos || md_nid <= 0
298b0d17251Schristos || !rsa_check_padding(ctx, mdname, NULL, md_nid)
299b0d17251Schristos || mdname_len >= sizeof(ctx->mdname)) {
300b0d17251Schristos if (md == NULL)
301b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
302b0d17251Schristos "%s could not be fetched", mdname);
303b0d17251Schristos if (md_nid <= 0)
304b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
305b0d17251Schristos "digest=%s", mdname);
306b0d17251Schristos if (mdname_len >= sizeof(ctx->mdname))
307b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
308b0d17251Schristos "%s exceeds name buffer length", mdname);
309b0d17251Schristos EVP_MD_free(md);
310b0d17251Schristos return 0;
311b0d17251Schristos }
312b0d17251Schristos
313b0d17251Schristos if (!ctx->flag_allow_md) {
314b0d17251Schristos if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
315b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
316b0d17251Schristos "digest %s != %s", mdname, ctx->mdname);
317b0d17251Schristos EVP_MD_free(md);
318b0d17251Schristos return 0;
319b0d17251Schristos }
320b0d17251Schristos EVP_MD_free(md);
321b0d17251Schristos return 1;
322b0d17251Schristos }
323b0d17251Schristos
324b0d17251Schristos if (!ctx->mgf1_md_set) {
325b0d17251Schristos if (!EVP_MD_up_ref(md)) {
326b0d17251Schristos EVP_MD_free(md);
327b0d17251Schristos return 0;
328b0d17251Schristos }
329b0d17251Schristos EVP_MD_free(ctx->mgf1_md);
330b0d17251Schristos ctx->mgf1_md = md;
331b0d17251Schristos ctx->mgf1_mdnid = md_nid;
332b0d17251Schristos OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
333b0d17251Schristos }
334b0d17251Schristos
335b0d17251Schristos EVP_MD_CTX_free(ctx->mdctx);
336b0d17251Schristos EVP_MD_free(ctx->md);
337b0d17251Schristos
338b0d17251Schristos ctx->mdctx = NULL;
339b0d17251Schristos ctx->md = md;
340b0d17251Schristos ctx->mdnid = md_nid;
341b0d17251Schristos OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
342b0d17251Schristos }
343b0d17251Schristos
344b0d17251Schristos return 1;
345b0d17251Schristos }
346b0d17251Schristos
rsa_setup_mgf1_md(PROV_RSA_CTX * ctx,const char * mdname,const char * mdprops)347b0d17251Schristos static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
348b0d17251Schristos const char *mdprops)
349b0d17251Schristos {
350b0d17251Schristos size_t len;
351b0d17251Schristos EVP_MD *md = NULL;
352b0d17251Schristos int mdnid;
353b0d17251Schristos
354b0d17251Schristos if (mdprops == NULL)
355b0d17251Schristos mdprops = ctx->propq;
356b0d17251Schristos
357b0d17251Schristos if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
358b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
359b0d17251Schristos "%s could not be fetched", mdname);
360b0d17251Schristos return 0;
361b0d17251Schristos }
362b0d17251Schristos /* The default for mgf1 is SHA1 - so allow SHA1 */
363b0d17251Schristos if ((mdnid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md, 1)) <= 0
364b0d17251Schristos || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
365b0d17251Schristos if (mdnid <= 0)
366b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
367b0d17251Schristos "digest=%s", mdname);
368b0d17251Schristos EVP_MD_free(md);
369b0d17251Schristos return 0;
370b0d17251Schristos }
371b0d17251Schristos len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
372b0d17251Schristos if (len >= sizeof(ctx->mgf1_mdname)) {
373b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
374b0d17251Schristos "%s exceeds name buffer length", mdname);
375b0d17251Schristos EVP_MD_free(md);
376b0d17251Schristos return 0;
377b0d17251Schristos }
378b0d17251Schristos
379b0d17251Schristos EVP_MD_free(ctx->mgf1_md);
380b0d17251Schristos ctx->mgf1_md = md;
381b0d17251Schristos ctx->mgf1_mdnid = mdnid;
382b0d17251Schristos ctx->mgf1_md_set = 1;
383b0d17251Schristos return 1;
384b0d17251Schristos }
385b0d17251Schristos
rsa_signverify_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[],int operation)386b0d17251Schristos static int rsa_signverify_init(void *vprsactx, void *vrsa,
387b0d17251Schristos const OSSL_PARAM params[], int operation)
388b0d17251Schristos {
389b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
390b0d17251Schristos
391b0d17251Schristos if (!ossl_prov_is_running() || prsactx == NULL)
392b0d17251Schristos return 0;
393b0d17251Schristos
394b0d17251Schristos if (vrsa == NULL && prsactx->rsa == NULL) {
395b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
396b0d17251Schristos return 0;
397b0d17251Schristos }
398b0d17251Schristos
399b0d17251Schristos if (vrsa != NULL) {
400b0d17251Schristos if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
401b0d17251Schristos return 0;
402b0d17251Schristos
403b0d17251Schristos if (!RSA_up_ref(vrsa))
404b0d17251Schristos return 0;
405b0d17251Schristos RSA_free(prsactx->rsa);
406b0d17251Schristos prsactx->rsa = vrsa;
407b0d17251Schristos }
408b0d17251Schristos
409b0d17251Schristos prsactx->operation = operation;
410b0d17251Schristos
411b0d17251Schristos /* Maximum for sign, auto for verify */
412b0d17251Schristos prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
413b0d17251Schristos prsactx->min_saltlen = -1;
414b0d17251Schristos
415b0d17251Schristos switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
416b0d17251Schristos case RSA_FLAG_TYPE_RSA:
417b0d17251Schristos prsactx->pad_mode = RSA_PKCS1_PADDING;
418b0d17251Schristos break;
419b0d17251Schristos case RSA_FLAG_TYPE_RSASSAPSS:
420b0d17251Schristos prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
421b0d17251Schristos
422b0d17251Schristos {
423b0d17251Schristos const RSA_PSS_PARAMS_30 *pss =
424b0d17251Schristos ossl_rsa_get0_pss_params_30(prsactx->rsa);
425b0d17251Schristos
426b0d17251Schristos if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
427b0d17251Schristos int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
428b0d17251Schristos int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
429b0d17251Schristos int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
430b0d17251Schristos const char *mdname, *mgf1mdname;
431b0d17251Schristos size_t len;
432b0d17251Schristos
433b0d17251Schristos mdname = ossl_rsa_oaeppss_nid2name(md_nid);
434b0d17251Schristos mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
435b0d17251Schristos
436b0d17251Schristos if (mdname == NULL) {
437b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
438b0d17251Schristos "PSS restrictions lack hash algorithm");
439b0d17251Schristos return 0;
440b0d17251Schristos }
441b0d17251Schristos if (mgf1mdname == NULL) {
442b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
443b0d17251Schristos "PSS restrictions lack MGF1 hash algorithm");
444b0d17251Schristos return 0;
445b0d17251Schristos }
446b0d17251Schristos
447b0d17251Schristos len = OPENSSL_strlcpy(prsactx->mdname, mdname,
448b0d17251Schristos sizeof(prsactx->mdname));
449b0d17251Schristos if (len >= sizeof(prsactx->mdname)) {
450b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
451b0d17251Schristos "hash algorithm name too long");
452b0d17251Schristos return 0;
453b0d17251Schristos }
454b0d17251Schristos len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
455b0d17251Schristos sizeof(prsactx->mgf1_mdname));
456b0d17251Schristos if (len >= sizeof(prsactx->mgf1_mdname)) {
457b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
458b0d17251Schristos "MGF1 hash algorithm name too long");
459b0d17251Schristos return 0;
460b0d17251Schristos }
461b0d17251Schristos prsactx->saltlen = min_saltlen;
462b0d17251Schristos
463b0d17251Schristos /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
464b0d17251Schristos if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
465b0d17251Schristos || !rsa_setup_md(prsactx, mdname, prsactx->propq)
466b0d17251Schristos || !rsa_check_parameters(prsactx, min_saltlen))
467b0d17251Schristos return 0;
468b0d17251Schristos }
469b0d17251Schristos }
470b0d17251Schristos
471b0d17251Schristos break;
472b0d17251Schristos default:
473b0d17251Schristos ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
474b0d17251Schristos return 0;
475b0d17251Schristos }
476b0d17251Schristos
477b0d17251Schristos if (!rsa_set_ctx_params(prsactx, params))
478b0d17251Schristos return 0;
479b0d17251Schristos
480b0d17251Schristos return 1;
481b0d17251Schristos }
482b0d17251Schristos
setup_tbuf(PROV_RSA_CTX * ctx)483b0d17251Schristos static int setup_tbuf(PROV_RSA_CTX *ctx)
484b0d17251Schristos {
485b0d17251Schristos if (ctx->tbuf != NULL)
486b0d17251Schristos return 1;
487b0d17251Schristos if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL) {
488b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
489b0d17251Schristos return 0;
490b0d17251Schristos }
491b0d17251Schristos return 1;
492b0d17251Schristos }
493b0d17251Schristos
clean_tbuf(PROV_RSA_CTX * ctx)494b0d17251Schristos static void clean_tbuf(PROV_RSA_CTX *ctx)
495b0d17251Schristos {
496b0d17251Schristos if (ctx->tbuf != NULL)
497b0d17251Schristos OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
498b0d17251Schristos }
499b0d17251Schristos
free_tbuf(PROV_RSA_CTX * ctx)500b0d17251Schristos static void free_tbuf(PROV_RSA_CTX *ctx)
501b0d17251Schristos {
502b0d17251Schristos clean_tbuf(ctx);
503b0d17251Schristos OPENSSL_free(ctx->tbuf);
504b0d17251Schristos ctx->tbuf = NULL;
505b0d17251Schristos }
506b0d17251Schristos
rsa_sign_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[])507b0d17251Schristos static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
508b0d17251Schristos {
509b0d17251Schristos if (!ossl_prov_is_running())
510b0d17251Schristos return 0;
511b0d17251Schristos return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_SIGN);
512b0d17251Schristos }
513b0d17251Schristos
rsa_sign(void * vprsactx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * tbs,size_t tbslen)514b0d17251Schristos static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
515b0d17251Schristos size_t sigsize, const unsigned char *tbs, size_t tbslen)
516b0d17251Schristos {
517b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
518b0d17251Schristos int ret;
519b0d17251Schristos size_t rsasize = RSA_size(prsactx->rsa);
520b0d17251Schristos size_t mdsize = rsa_get_md_size(prsactx);
521b0d17251Schristos
522b0d17251Schristos if (!ossl_prov_is_running())
523b0d17251Schristos return 0;
524b0d17251Schristos
525b0d17251Schristos if (sig == NULL) {
526b0d17251Schristos *siglen = rsasize;
527b0d17251Schristos return 1;
528b0d17251Schristos }
529b0d17251Schristos
530b0d17251Schristos if (sigsize < rsasize) {
531b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
532b0d17251Schristos "is %zu, should be at least %zu", sigsize, rsasize);
533b0d17251Schristos return 0;
534b0d17251Schristos }
535b0d17251Schristos
536b0d17251Schristos if (mdsize != 0) {
537b0d17251Schristos if (tbslen != mdsize) {
538b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
539b0d17251Schristos return 0;
540b0d17251Schristos }
541b0d17251Schristos
542b0d17251Schristos #ifndef FIPS_MODULE
543b0d17251Schristos if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
544b0d17251Schristos unsigned int sltmp;
545b0d17251Schristos
546b0d17251Schristos if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
547b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
548b0d17251Schristos "only PKCS#1 padding supported with MDC2");
549b0d17251Schristos return 0;
550b0d17251Schristos }
551b0d17251Schristos ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp,
552b0d17251Schristos prsactx->rsa);
553b0d17251Schristos
554b0d17251Schristos if (ret <= 0) {
555b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
556b0d17251Schristos return 0;
557b0d17251Schristos }
558b0d17251Schristos ret = sltmp;
559b0d17251Schristos goto end;
560b0d17251Schristos }
561b0d17251Schristos #endif
562b0d17251Schristos switch (prsactx->pad_mode) {
563b0d17251Schristos case RSA_X931_PADDING:
564b0d17251Schristos if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
565b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
566b0d17251Schristos "RSA key size = %d, expected minimum = %d",
567b0d17251Schristos RSA_size(prsactx->rsa), tbslen + 1);
568b0d17251Schristos return 0;
569b0d17251Schristos }
570b0d17251Schristos if (!setup_tbuf(prsactx)) {
571b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
572b0d17251Schristos return 0;
573b0d17251Schristos }
574b0d17251Schristos memcpy(prsactx->tbuf, tbs, tbslen);
575b0d17251Schristos prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
576b0d17251Schristos ret = RSA_private_encrypt(tbslen + 1, prsactx->tbuf,
577b0d17251Schristos sig, prsactx->rsa, RSA_X931_PADDING);
578b0d17251Schristos clean_tbuf(prsactx);
579b0d17251Schristos break;
580b0d17251Schristos
581b0d17251Schristos case RSA_PKCS1_PADDING:
582b0d17251Schristos {
583b0d17251Schristos unsigned int sltmp;
584b0d17251Schristos
585b0d17251Schristos ret = RSA_sign(prsactx->mdnid, tbs, tbslen, sig, &sltmp,
586b0d17251Schristos prsactx->rsa);
587b0d17251Schristos if (ret <= 0) {
588b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
589b0d17251Schristos return 0;
590b0d17251Schristos }
591b0d17251Schristos ret = sltmp;
592b0d17251Schristos }
593b0d17251Schristos break;
594b0d17251Schristos
595b0d17251Schristos case RSA_PKCS1_PSS_PADDING:
596b0d17251Schristos /* Check PSS restrictions */
597b0d17251Schristos if (rsa_pss_restricted(prsactx)) {
598b0d17251Schristos switch (prsactx->saltlen) {
599b0d17251Schristos case RSA_PSS_SALTLEN_DIGEST:
600b0d17251Schristos if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
601b0d17251Schristos ERR_raise_data(ERR_LIB_PROV,
602b0d17251Schristos PROV_R_PSS_SALTLEN_TOO_SMALL,
603b0d17251Schristos "minimum salt length set to %d, "
604b0d17251Schristos "but the digest only gives %d",
605b0d17251Schristos prsactx->min_saltlen,
606b0d17251Schristos EVP_MD_get_size(prsactx->md));
607b0d17251Schristos return 0;
608b0d17251Schristos }
609b0d17251Schristos /* FALLTHRU */
610b0d17251Schristos default:
611b0d17251Schristos if (prsactx->saltlen >= 0
612b0d17251Schristos && prsactx->saltlen < prsactx->min_saltlen) {
613b0d17251Schristos ERR_raise_data(ERR_LIB_PROV,
614b0d17251Schristos PROV_R_PSS_SALTLEN_TOO_SMALL,
615b0d17251Schristos "minimum salt length set to %d, but the"
616b0d17251Schristos "actual salt length is only set to %d",
617b0d17251Schristos prsactx->min_saltlen,
618b0d17251Schristos prsactx->saltlen);
619b0d17251Schristos return 0;
620b0d17251Schristos }
621b0d17251Schristos break;
622b0d17251Schristos }
623b0d17251Schristos }
624b0d17251Schristos if (!setup_tbuf(prsactx))
625b0d17251Schristos return 0;
626b0d17251Schristos if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
627b0d17251Schristos prsactx->tbuf, tbs,
628b0d17251Schristos prsactx->md, prsactx->mgf1_md,
629b0d17251Schristos prsactx->saltlen)) {
630b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
631b0d17251Schristos return 0;
632b0d17251Schristos }
633b0d17251Schristos ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
634b0d17251Schristos sig, prsactx->rsa, RSA_NO_PADDING);
635b0d17251Schristos clean_tbuf(prsactx);
636b0d17251Schristos break;
637b0d17251Schristos
638b0d17251Schristos default:
639b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
640b0d17251Schristos "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
641b0d17251Schristos return 0;
642b0d17251Schristos }
643b0d17251Schristos } else {
644b0d17251Schristos ret = RSA_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
645b0d17251Schristos prsactx->pad_mode);
646b0d17251Schristos }
647b0d17251Schristos
648b0d17251Schristos #ifndef FIPS_MODULE
649b0d17251Schristos end:
650b0d17251Schristos #endif
651b0d17251Schristos if (ret <= 0) {
652b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
653b0d17251Schristos return 0;
654b0d17251Schristos }
655b0d17251Schristos
656b0d17251Schristos *siglen = ret;
657b0d17251Schristos return 1;
658b0d17251Schristos }
659b0d17251Schristos
rsa_verify_recover_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[])660b0d17251Schristos static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
661b0d17251Schristos const OSSL_PARAM params[])
662b0d17251Schristos {
663b0d17251Schristos if (!ossl_prov_is_running())
664b0d17251Schristos return 0;
665b0d17251Schristos return rsa_signverify_init(vprsactx, vrsa, params,
666b0d17251Schristos EVP_PKEY_OP_VERIFYRECOVER);
667b0d17251Schristos }
668b0d17251Schristos
rsa_verify_recover(void * vprsactx,unsigned char * rout,size_t * routlen,size_t routsize,const unsigned char * sig,size_t siglen)669b0d17251Schristos static int rsa_verify_recover(void *vprsactx,
670b0d17251Schristos unsigned char *rout,
671b0d17251Schristos size_t *routlen,
672b0d17251Schristos size_t routsize,
673b0d17251Schristos const unsigned char *sig,
674b0d17251Schristos size_t siglen)
675b0d17251Schristos {
676b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
677b0d17251Schristos int ret;
678b0d17251Schristos
679b0d17251Schristos if (!ossl_prov_is_running())
680b0d17251Schristos return 0;
681b0d17251Schristos
682b0d17251Schristos if (rout == NULL) {
683b0d17251Schristos *routlen = RSA_size(prsactx->rsa);
684b0d17251Schristos return 1;
685b0d17251Schristos }
686b0d17251Schristos
687b0d17251Schristos if (prsactx->md != NULL) {
688b0d17251Schristos switch (prsactx->pad_mode) {
689b0d17251Schristos case RSA_X931_PADDING:
690b0d17251Schristos if (!setup_tbuf(prsactx))
691b0d17251Schristos return 0;
692b0d17251Schristos ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
693b0d17251Schristos RSA_X931_PADDING);
694b0d17251Schristos if (ret < 1) {
695b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
696b0d17251Schristos return 0;
697b0d17251Schristos }
698b0d17251Schristos ret--;
699b0d17251Schristos if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
700b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
701b0d17251Schristos return 0;
702b0d17251Schristos }
703b0d17251Schristos if (ret != EVP_MD_get_size(prsactx->md)) {
704b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
705b0d17251Schristos "Should be %d, but got %d",
706b0d17251Schristos EVP_MD_get_size(prsactx->md), ret);
707b0d17251Schristos return 0;
708b0d17251Schristos }
709b0d17251Schristos
710b0d17251Schristos *routlen = ret;
711b0d17251Schristos if (rout != prsactx->tbuf) {
712b0d17251Schristos if (routsize < (size_t)ret) {
713b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
714b0d17251Schristos "buffer size is %d, should be %d",
715b0d17251Schristos routsize, ret);
716b0d17251Schristos return 0;
717b0d17251Schristos }
718b0d17251Schristos memcpy(rout, prsactx->tbuf, ret);
719b0d17251Schristos }
720b0d17251Schristos break;
721b0d17251Schristos
722b0d17251Schristos case RSA_PKCS1_PADDING:
723b0d17251Schristos {
724b0d17251Schristos size_t sltmp;
725b0d17251Schristos
726b0d17251Schristos ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
727b0d17251Schristos sig, siglen, prsactx->rsa);
728b0d17251Schristos if (ret <= 0) {
729b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
730b0d17251Schristos return 0;
731b0d17251Schristos }
732b0d17251Schristos ret = sltmp;
733b0d17251Schristos }
734b0d17251Schristos break;
735b0d17251Schristos
736b0d17251Schristos default:
737b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
738b0d17251Schristos "Only X.931 or PKCS#1 v1.5 padding allowed");
739b0d17251Schristos return 0;
740b0d17251Schristos }
741b0d17251Schristos } else {
742b0d17251Schristos ret = RSA_public_decrypt(siglen, sig, rout, prsactx->rsa,
743b0d17251Schristos prsactx->pad_mode);
744b0d17251Schristos if (ret < 0) {
745b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
746b0d17251Schristos return 0;
747b0d17251Schristos }
748b0d17251Schristos }
749b0d17251Schristos *routlen = ret;
750b0d17251Schristos return 1;
751b0d17251Schristos }
752b0d17251Schristos
rsa_verify_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[])753b0d17251Schristos static int rsa_verify_init(void *vprsactx, void *vrsa,
754b0d17251Schristos const OSSL_PARAM params[])
755b0d17251Schristos {
756b0d17251Schristos if (!ossl_prov_is_running())
757b0d17251Schristos return 0;
758b0d17251Schristos return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_VERIFY);
759b0d17251Schristos }
760b0d17251Schristos
rsa_verify(void * vprsactx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)761b0d17251Schristos static int rsa_verify(void *vprsactx, const unsigned char *sig, size_t siglen,
762b0d17251Schristos const unsigned char *tbs, size_t tbslen)
763b0d17251Schristos {
764b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
765b0d17251Schristos size_t rslen;
766b0d17251Schristos
767b0d17251Schristos if (!ossl_prov_is_running())
768b0d17251Schristos return 0;
769b0d17251Schristos if (prsactx->md != NULL) {
770b0d17251Schristos switch (prsactx->pad_mode) {
771b0d17251Schristos case RSA_PKCS1_PADDING:
772b0d17251Schristos if (!RSA_verify(prsactx->mdnid, tbs, tbslen, sig, siglen,
773b0d17251Schristos prsactx->rsa)) {
774b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
775b0d17251Schristos return 0;
776b0d17251Schristos }
777b0d17251Schristos return 1;
778b0d17251Schristos case RSA_X931_PADDING:
779b0d17251Schristos if (!setup_tbuf(prsactx))
780b0d17251Schristos return 0;
781b0d17251Schristos if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
782b0d17251Schristos sig, siglen) <= 0)
783b0d17251Schristos return 0;
784b0d17251Schristos break;
785b0d17251Schristos case RSA_PKCS1_PSS_PADDING:
786b0d17251Schristos {
787b0d17251Schristos int ret;
788b0d17251Schristos size_t mdsize;
789b0d17251Schristos
790b0d17251Schristos /*
791b0d17251Schristos * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
792b0d17251Schristos * call
793b0d17251Schristos */
794b0d17251Schristos mdsize = rsa_get_md_size(prsactx);
795b0d17251Schristos if (tbslen != mdsize) {
796b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
797b0d17251Schristos "Should be %d, but got %d",
798b0d17251Schristos mdsize, tbslen);
799b0d17251Schristos return 0;
800b0d17251Schristos }
801b0d17251Schristos
802b0d17251Schristos if (!setup_tbuf(prsactx))
803b0d17251Schristos return 0;
804b0d17251Schristos ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf,
805b0d17251Schristos prsactx->rsa, RSA_NO_PADDING);
806b0d17251Schristos if (ret <= 0) {
807b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
808b0d17251Schristos return 0;
809b0d17251Schristos }
810b0d17251Schristos ret = RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
811b0d17251Schristos prsactx->md, prsactx->mgf1_md,
812b0d17251Schristos prsactx->tbuf,
813b0d17251Schristos prsactx->saltlen);
814b0d17251Schristos if (ret <= 0) {
815b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
816b0d17251Schristos return 0;
817b0d17251Schristos }
818b0d17251Schristos return 1;
819b0d17251Schristos }
820b0d17251Schristos default:
821b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
822b0d17251Schristos "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
823b0d17251Schristos return 0;
824b0d17251Schristos }
825b0d17251Schristos } else {
8264170684fSchristos int ret;
8274170684fSchristos
828b0d17251Schristos if (!setup_tbuf(prsactx))
829b0d17251Schristos return 0;
8304170684fSchristos ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
831b0d17251Schristos prsactx->pad_mode);
8324170684fSchristos if (ret <= 0) {
833b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
834b0d17251Schristos return 0;
835b0d17251Schristos }
8364170684fSchristos rslen = (size_t)ret;
837b0d17251Schristos }
838b0d17251Schristos
839b0d17251Schristos if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
840b0d17251Schristos return 0;
841b0d17251Schristos
842b0d17251Schristos return 1;
843b0d17251Schristos }
844b0d17251Schristos
rsa_digest_signverify_init(void * vprsactx,const char * mdname,void * vrsa,const OSSL_PARAM params[],int operation)845b0d17251Schristos static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
846b0d17251Schristos void *vrsa, const OSSL_PARAM params[],
847b0d17251Schristos int operation)
848b0d17251Schristos {
849b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
850b0d17251Schristos
851b0d17251Schristos if (!ossl_prov_is_running())
852b0d17251Schristos return 0;
853b0d17251Schristos
854b0d17251Schristos if (!rsa_signverify_init(vprsactx, vrsa, params, operation))
855b0d17251Schristos return 0;
856b0d17251Schristos
857b0d17251Schristos if (mdname != NULL
858b0d17251Schristos /* was rsa_setup_md already called in rsa_signverify_init()? */
859b0d17251Schristos && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
860b0d17251Schristos && !rsa_setup_md(prsactx, mdname, prsactx->propq))
861b0d17251Schristos return 0;
862b0d17251Schristos
863b0d17251Schristos prsactx->flag_allow_md = 0;
864b0d17251Schristos
865b0d17251Schristos if (prsactx->mdctx == NULL) {
866b0d17251Schristos prsactx->mdctx = EVP_MD_CTX_new();
867b0d17251Schristos if (prsactx->mdctx == NULL)
868b0d17251Schristos goto error;
869b0d17251Schristos }
870b0d17251Schristos
871b0d17251Schristos if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
872b0d17251Schristos goto error;
873b0d17251Schristos
874b0d17251Schristos return 1;
875b0d17251Schristos
876b0d17251Schristos error:
877b0d17251Schristos EVP_MD_CTX_free(prsactx->mdctx);
878b0d17251Schristos prsactx->mdctx = NULL;
879b0d17251Schristos return 0;
880b0d17251Schristos }
881b0d17251Schristos
rsa_digest_signverify_update(void * vprsactx,const unsigned char * data,size_t datalen)882b0d17251Schristos static int rsa_digest_signverify_update(void *vprsactx,
883b0d17251Schristos const unsigned char *data,
884b0d17251Schristos size_t datalen)
885b0d17251Schristos {
886b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
887b0d17251Schristos
888b0d17251Schristos if (prsactx == NULL || prsactx->mdctx == NULL)
889b0d17251Schristos return 0;
890b0d17251Schristos
891b0d17251Schristos return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
892b0d17251Schristos }
893b0d17251Schristos
rsa_digest_sign_init(void * vprsactx,const char * mdname,void * vrsa,const OSSL_PARAM params[])894b0d17251Schristos static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
895b0d17251Schristos void *vrsa, const OSSL_PARAM params[])
896b0d17251Schristos {
897b0d17251Schristos if (!ossl_prov_is_running())
898b0d17251Schristos return 0;
899b0d17251Schristos return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
900b0d17251Schristos params, EVP_PKEY_OP_SIGN);
901b0d17251Schristos }
902b0d17251Schristos
rsa_digest_sign_final(void * vprsactx,unsigned char * sig,size_t * siglen,size_t sigsize)903b0d17251Schristos static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
904b0d17251Schristos size_t *siglen, size_t sigsize)
905b0d17251Schristos {
906b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
907b0d17251Schristos unsigned char digest[EVP_MAX_MD_SIZE];
908b0d17251Schristos unsigned int dlen = 0;
909b0d17251Schristos
910b0d17251Schristos if (!ossl_prov_is_running() || prsactx == NULL)
911b0d17251Schristos return 0;
912b0d17251Schristos prsactx->flag_allow_md = 1;
913b0d17251Schristos if (prsactx->mdctx == NULL)
914b0d17251Schristos return 0;
915b0d17251Schristos /*
916b0d17251Schristos * If sig is NULL then we're just finding out the sig size. Other fields
917b0d17251Schristos * are ignored. Defer to rsa_sign.
918b0d17251Schristos */
919b0d17251Schristos if (sig != NULL) {
920b0d17251Schristos /*
921b0d17251Schristos * The digests used here are all known (see rsa_get_md_nid()), so they
922b0d17251Schristos * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
923b0d17251Schristos */
924b0d17251Schristos if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
925b0d17251Schristos return 0;
926b0d17251Schristos }
927b0d17251Schristos
928b0d17251Schristos return rsa_sign(vprsactx, sig, siglen, sigsize, digest, (size_t)dlen);
929b0d17251Schristos }
930b0d17251Schristos
rsa_digest_verify_init(void * vprsactx,const char * mdname,void * vrsa,const OSSL_PARAM params[])931b0d17251Schristos static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
932b0d17251Schristos void *vrsa, const OSSL_PARAM params[])
933b0d17251Schristos {
934b0d17251Schristos if (!ossl_prov_is_running())
935b0d17251Schristos return 0;
936b0d17251Schristos return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
937b0d17251Schristos params, EVP_PKEY_OP_VERIFY);
938b0d17251Schristos }
939b0d17251Schristos
rsa_digest_verify_final(void * vprsactx,const unsigned char * sig,size_t siglen)940b0d17251Schristos int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
941b0d17251Schristos size_t siglen)
942b0d17251Schristos {
943b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
944b0d17251Schristos unsigned char digest[EVP_MAX_MD_SIZE];
945b0d17251Schristos unsigned int dlen = 0;
946b0d17251Schristos
947b0d17251Schristos if (!ossl_prov_is_running())
948b0d17251Schristos return 0;
949b0d17251Schristos
950b0d17251Schristos if (prsactx == NULL)
951b0d17251Schristos return 0;
952b0d17251Schristos prsactx->flag_allow_md = 1;
953b0d17251Schristos if (prsactx->mdctx == NULL)
954b0d17251Schristos return 0;
955b0d17251Schristos
956b0d17251Schristos /*
957b0d17251Schristos * The digests used here are all known (see rsa_get_md_nid()), so they
958b0d17251Schristos * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
959b0d17251Schristos */
960b0d17251Schristos if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
961b0d17251Schristos return 0;
962b0d17251Schristos
963b0d17251Schristos return rsa_verify(vprsactx, sig, siglen, digest, (size_t)dlen);
964b0d17251Schristos }
965b0d17251Schristos
rsa_freectx(void * vprsactx)966b0d17251Schristos static void rsa_freectx(void *vprsactx)
967b0d17251Schristos {
968b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
969b0d17251Schristos
970b0d17251Schristos if (prsactx == NULL)
971b0d17251Schristos return;
972b0d17251Schristos
973b0d17251Schristos EVP_MD_CTX_free(prsactx->mdctx);
974b0d17251Schristos EVP_MD_free(prsactx->md);
975b0d17251Schristos EVP_MD_free(prsactx->mgf1_md);
976b0d17251Schristos OPENSSL_free(prsactx->propq);
977b0d17251Schristos free_tbuf(prsactx);
978b0d17251Schristos RSA_free(prsactx->rsa);
979b0d17251Schristos
980b0d17251Schristos OPENSSL_clear_free(prsactx, sizeof(*prsactx));
981b0d17251Schristos }
982b0d17251Schristos
rsa_dupctx(void * vprsactx)983b0d17251Schristos static void *rsa_dupctx(void *vprsactx)
984b0d17251Schristos {
985b0d17251Schristos PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
986b0d17251Schristos PROV_RSA_CTX *dstctx;
987b0d17251Schristos
988b0d17251Schristos if (!ossl_prov_is_running())
989b0d17251Schristos return NULL;
990b0d17251Schristos
991b0d17251Schristos dstctx = OPENSSL_zalloc(sizeof(*srcctx));
992b0d17251Schristos if (dstctx == NULL) {
993b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
994b0d17251Schristos return NULL;
995b0d17251Schristos }
996b0d17251Schristos
997b0d17251Schristos *dstctx = *srcctx;
998b0d17251Schristos dstctx->rsa = NULL;
999b0d17251Schristos dstctx->md = NULL;
1000*0e2e28bcSchristos dstctx->mgf1_md = NULL;
1001b0d17251Schristos dstctx->mdctx = NULL;
1002b0d17251Schristos dstctx->tbuf = NULL;
1003b0d17251Schristos dstctx->propq = NULL;
1004b0d17251Schristos
1005b0d17251Schristos if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
1006b0d17251Schristos goto err;
1007b0d17251Schristos dstctx->rsa = srcctx->rsa;
1008b0d17251Schristos
1009b0d17251Schristos if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
1010b0d17251Schristos goto err;
1011b0d17251Schristos dstctx->md = srcctx->md;
1012b0d17251Schristos
1013b0d17251Schristos if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
1014b0d17251Schristos goto err;
1015b0d17251Schristos dstctx->mgf1_md = srcctx->mgf1_md;
1016b0d17251Schristos
1017b0d17251Schristos if (srcctx->mdctx != NULL) {
1018b0d17251Schristos dstctx->mdctx = EVP_MD_CTX_new();
1019b0d17251Schristos if (dstctx->mdctx == NULL
1020b0d17251Schristos || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
1021b0d17251Schristos goto err;
1022b0d17251Schristos }
1023b0d17251Schristos
1024b0d17251Schristos if (srcctx->propq != NULL) {
1025b0d17251Schristos dstctx->propq = OPENSSL_strdup(srcctx->propq);
1026b0d17251Schristos if (dstctx->propq == NULL)
1027b0d17251Schristos goto err;
1028b0d17251Schristos }
1029b0d17251Schristos
1030b0d17251Schristos return dstctx;
1031b0d17251Schristos err:
1032b0d17251Schristos rsa_freectx(dstctx);
1033b0d17251Schristos return NULL;
1034b0d17251Schristos }
1035b0d17251Schristos
rsa_get_ctx_params(void * vprsactx,OSSL_PARAM * params)1036b0d17251Schristos static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
1037b0d17251Schristos {
1038b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1039b0d17251Schristos OSSL_PARAM *p;
1040b0d17251Schristos
1041b0d17251Schristos if (prsactx == NULL)
1042b0d17251Schristos return 0;
1043b0d17251Schristos
1044b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
1045b0d17251Schristos if (p != NULL) {
1046b0d17251Schristos /* The Algorithm Identifier of the combined signature algorithm */
1047b0d17251Schristos unsigned char aid_buf[128];
1048b0d17251Schristos unsigned char *aid;
1049b0d17251Schristos size_t aid_len;
1050b0d17251Schristos
1051b0d17251Schristos aid = rsa_generate_signature_aid(prsactx, aid_buf,
1052b0d17251Schristos sizeof(aid_buf), &aid_len);
1053b0d17251Schristos if (aid == NULL || !OSSL_PARAM_set_octet_string(p, aid, aid_len))
1054b0d17251Schristos return 0;
1055b0d17251Schristos }
1056b0d17251Schristos
1057b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
1058b0d17251Schristos if (p != NULL)
1059b0d17251Schristos switch (p->data_type) {
1060b0d17251Schristos case OSSL_PARAM_INTEGER:
1061b0d17251Schristos if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
1062b0d17251Schristos return 0;
1063b0d17251Schristos break;
1064b0d17251Schristos case OSSL_PARAM_UTF8_STRING:
1065b0d17251Schristos {
1066b0d17251Schristos int i;
1067b0d17251Schristos const char *word = NULL;
1068b0d17251Schristos
1069b0d17251Schristos for (i = 0; padding_item[i].id != 0; i++) {
1070b0d17251Schristos if (prsactx->pad_mode == (int)padding_item[i].id) {
1071b0d17251Schristos word = padding_item[i].ptr;
1072b0d17251Schristos break;
1073b0d17251Schristos }
1074b0d17251Schristos }
1075b0d17251Schristos
1076b0d17251Schristos if (word != NULL) {
1077b0d17251Schristos if (!OSSL_PARAM_set_utf8_string(p, word))
1078b0d17251Schristos return 0;
1079b0d17251Schristos } else {
1080b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
1081b0d17251Schristos }
1082b0d17251Schristos }
1083b0d17251Schristos break;
1084b0d17251Schristos default:
1085b0d17251Schristos return 0;
1086b0d17251Schristos }
1087b0d17251Schristos
1088b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
1089b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
1090b0d17251Schristos return 0;
1091b0d17251Schristos
1092b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
1093b0d17251Schristos if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
1094b0d17251Schristos return 0;
1095b0d17251Schristos
1096b0d17251Schristos p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
1097b0d17251Schristos if (p != NULL) {
1098b0d17251Schristos if (p->data_type == OSSL_PARAM_INTEGER) {
1099b0d17251Schristos if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
1100b0d17251Schristos return 0;
1101b0d17251Schristos } else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
1102b0d17251Schristos const char *value = NULL;
1103b0d17251Schristos
1104b0d17251Schristos switch (prsactx->saltlen) {
1105b0d17251Schristos case RSA_PSS_SALTLEN_DIGEST:
1106b0d17251Schristos value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
1107b0d17251Schristos break;
1108b0d17251Schristos case RSA_PSS_SALTLEN_MAX:
1109b0d17251Schristos value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
1110b0d17251Schristos break;
1111b0d17251Schristos case RSA_PSS_SALTLEN_AUTO:
1112b0d17251Schristos value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
1113b0d17251Schristos break;
1114b0d17251Schristos default:
1115b0d17251Schristos {
1116b0d17251Schristos int len = BIO_snprintf(p->data, p->data_size, "%d",
1117b0d17251Schristos prsactx->saltlen);
1118b0d17251Schristos
1119b0d17251Schristos if (len <= 0)
1120b0d17251Schristos return 0;
1121b0d17251Schristos p->return_size = len;
1122b0d17251Schristos break;
1123b0d17251Schristos }
1124b0d17251Schristos }
1125b0d17251Schristos if (value != NULL
1126b0d17251Schristos && !OSSL_PARAM_set_utf8_string(p, value))
1127b0d17251Schristos return 0;
1128b0d17251Schristos }
1129b0d17251Schristos }
1130b0d17251Schristos
1131b0d17251Schristos return 1;
1132b0d17251Schristos }
1133b0d17251Schristos
1134b0d17251Schristos static const OSSL_PARAM known_gettable_ctx_params[] = {
1135b0d17251Schristos OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
1136b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1137b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1138b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1139b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1140b0d17251Schristos OSSL_PARAM_END
1141b0d17251Schristos };
1142b0d17251Schristos
rsa_gettable_ctx_params(ossl_unused void * vprsactx,ossl_unused void * provctx)1143b0d17251Schristos static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
1144b0d17251Schristos ossl_unused void *provctx)
1145b0d17251Schristos {
1146b0d17251Schristos return known_gettable_ctx_params;
1147b0d17251Schristos }
1148b0d17251Schristos
rsa_set_ctx_params(void * vprsactx,const OSSL_PARAM params[])1149b0d17251Schristos static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
1150b0d17251Schristos {
1151b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1152b0d17251Schristos const OSSL_PARAM *p;
1153b0d17251Schristos int pad_mode;
1154b0d17251Schristos int saltlen;
1155b0d17251Schristos char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
1156b0d17251Schristos char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
1157b0d17251Schristos char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
1158b0d17251Schristos char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
1159b0d17251Schristos
1160b0d17251Schristos if (prsactx == NULL)
1161b0d17251Schristos return 0;
1162b0d17251Schristos if (params == NULL)
1163b0d17251Schristos return 1;
1164b0d17251Schristos
1165b0d17251Schristos pad_mode = prsactx->pad_mode;
1166b0d17251Schristos saltlen = prsactx->saltlen;
1167b0d17251Schristos
1168b0d17251Schristos p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
1169b0d17251Schristos if (p != NULL) {
1170b0d17251Schristos const OSSL_PARAM *propsp =
1171b0d17251Schristos OSSL_PARAM_locate_const(params,
1172b0d17251Schristos OSSL_SIGNATURE_PARAM_PROPERTIES);
1173b0d17251Schristos
1174b0d17251Schristos pmdname = mdname;
1175b0d17251Schristos if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
1176b0d17251Schristos return 0;
1177b0d17251Schristos
1178b0d17251Schristos if (propsp != NULL) {
1179b0d17251Schristos pmdprops = mdprops;
1180b0d17251Schristos if (!OSSL_PARAM_get_utf8_string(propsp,
1181b0d17251Schristos &pmdprops, sizeof(mdprops)))
1182b0d17251Schristos return 0;
1183b0d17251Schristos }
1184b0d17251Schristos }
1185b0d17251Schristos
1186b0d17251Schristos p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
1187b0d17251Schristos if (p != NULL) {
1188b0d17251Schristos const char *err_extra_text = NULL;
1189b0d17251Schristos
1190b0d17251Schristos switch (p->data_type) {
1191b0d17251Schristos case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1192b0d17251Schristos if (!OSSL_PARAM_get_int(p, &pad_mode))
1193b0d17251Schristos return 0;
1194b0d17251Schristos break;
1195b0d17251Schristos case OSSL_PARAM_UTF8_STRING:
1196b0d17251Schristos {
1197b0d17251Schristos int i;
1198b0d17251Schristos
1199b0d17251Schristos if (p->data == NULL)
1200b0d17251Schristos return 0;
1201b0d17251Schristos
1202b0d17251Schristos for (i = 0; padding_item[i].id != 0; i++) {
1203b0d17251Schristos if (strcmp(p->data, padding_item[i].ptr) == 0) {
1204b0d17251Schristos pad_mode = padding_item[i].id;
1205b0d17251Schristos break;
1206b0d17251Schristos }
1207b0d17251Schristos }
1208b0d17251Schristos }
1209b0d17251Schristos break;
1210b0d17251Schristos default:
1211b0d17251Schristos return 0;
1212b0d17251Schristos }
1213b0d17251Schristos
1214b0d17251Schristos switch (pad_mode) {
1215b0d17251Schristos case RSA_PKCS1_OAEP_PADDING:
1216b0d17251Schristos /*
1217b0d17251Schristos * OAEP padding is for asymmetric cipher only so is not compatible
1218b0d17251Schristos * with signature use.
1219b0d17251Schristos */
1220b0d17251Schristos err_extra_text = "OAEP padding not allowed for signing / verifying";
1221b0d17251Schristos goto bad_pad;
1222b0d17251Schristos case RSA_PKCS1_PSS_PADDING:
1223b0d17251Schristos if ((prsactx->operation
1224b0d17251Schristos & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
1225b0d17251Schristos err_extra_text =
1226b0d17251Schristos "PSS padding only allowed for sign and verify operations";
1227b0d17251Schristos goto bad_pad;
1228b0d17251Schristos }
1229b0d17251Schristos break;
1230b0d17251Schristos case RSA_PKCS1_PADDING:
1231b0d17251Schristos err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
1232b0d17251Schristos goto cont;
1233b0d17251Schristos case RSA_NO_PADDING:
1234b0d17251Schristos err_extra_text = "No padding not allowed with RSA-PSS";
1235b0d17251Schristos goto cont;
1236b0d17251Schristos case RSA_X931_PADDING:
1237b0d17251Schristos err_extra_text = "X.931 padding not allowed with RSA-PSS";
1238b0d17251Schristos cont:
1239b0d17251Schristos if (RSA_test_flags(prsactx->rsa,
1240b0d17251Schristos RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
1241b0d17251Schristos break;
1242b0d17251Schristos /* FALLTHRU */
1243b0d17251Schristos default:
1244b0d17251Schristos bad_pad:
1245b0d17251Schristos if (err_extra_text == NULL)
1246b0d17251Schristos ERR_raise(ERR_LIB_PROV,
1247b0d17251Schristos PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
1248b0d17251Schristos else
1249b0d17251Schristos ERR_raise_data(ERR_LIB_PROV,
1250b0d17251Schristos PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
1251b0d17251Schristos err_extra_text);
1252b0d17251Schristos return 0;
1253b0d17251Schristos }
1254b0d17251Schristos }
1255b0d17251Schristos
1256b0d17251Schristos p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
1257b0d17251Schristos if (p != NULL) {
1258b0d17251Schristos if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1259b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
1260b0d17251Schristos "PSS saltlen can only be specified if "
1261b0d17251Schristos "PSS padding has been specified first");
1262b0d17251Schristos return 0;
1263b0d17251Schristos }
1264b0d17251Schristos
1265b0d17251Schristos switch (p->data_type) {
1266b0d17251Schristos case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
1267b0d17251Schristos if (!OSSL_PARAM_get_int(p, &saltlen))
1268b0d17251Schristos return 0;
1269b0d17251Schristos break;
1270b0d17251Schristos case OSSL_PARAM_UTF8_STRING:
1271b0d17251Schristos if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
1272b0d17251Schristos saltlen = RSA_PSS_SALTLEN_DIGEST;
1273b0d17251Schristos else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
1274b0d17251Schristos saltlen = RSA_PSS_SALTLEN_MAX;
1275b0d17251Schristos else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
1276b0d17251Schristos saltlen = RSA_PSS_SALTLEN_AUTO;
1277b0d17251Schristos else
1278b0d17251Schristos saltlen = atoi(p->data);
1279b0d17251Schristos break;
1280b0d17251Schristos default:
1281b0d17251Schristos return 0;
1282b0d17251Schristos }
1283b0d17251Schristos
1284b0d17251Schristos /*
1285b0d17251Schristos * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
1286b0d17251Schristos * Contrary to what it's name suggests, it's the currently
1287b0d17251Schristos * lowest saltlen number possible.
1288b0d17251Schristos */
1289b0d17251Schristos if (saltlen < RSA_PSS_SALTLEN_MAX) {
1290b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
1291b0d17251Schristos return 0;
1292b0d17251Schristos }
1293b0d17251Schristos
1294b0d17251Schristos if (rsa_pss_restricted(prsactx)) {
1295b0d17251Schristos switch (saltlen) {
1296b0d17251Schristos case RSA_PSS_SALTLEN_AUTO:
1297b0d17251Schristos if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
1298b0d17251Schristos ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
1299b0d17251Schristos "Cannot use autodetected salt length");
1300b0d17251Schristos return 0;
1301b0d17251Schristos }
1302b0d17251Schristos break;
1303b0d17251Schristos case RSA_PSS_SALTLEN_DIGEST:
1304b0d17251Schristos if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
1305b0d17251Schristos ERR_raise_data(ERR_LIB_PROV,
1306b0d17251Schristos PROV_R_PSS_SALTLEN_TOO_SMALL,
1307b0d17251Schristos "Should be more than %d, but would be "
1308b0d17251Schristos "set to match digest size (%d)",
1309b0d17251Schristos prsactx->min_saltlen,
1310b0d17251Schristos EVP_MD_get_size(prsactx->md));
1311b0d17251Schristos return 0;
1312b0d17251Schristos }
1313b0d17251Schristos break;
1314b0d17251Schristos default:
1315b0d17251Schristos if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
1316b0d17251Schristos ERR_raise_data(ERR_LIB_PROV,
1317b0d17251Schristos PROV_R_PSS_SALTLEN_TOO_SMALL,
1318b0d17251Schristos "Should be more than %d, "
1319b0d17251Schristos "but would be set to %d",
1320b0d17251Schristos prsactx->min_saltlen, saltlen);
1321b0d17251Schristos return 0;
1322b0d17251Schristos }
1323b0d17251Schristos }
1324b0d17251Schristos }
1325b0d17251Schristos }
1326b0d17251Schristos
1327b0d17251Schristos p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
1328b0d17251Schristos if (p != NULL) {
1329b0d17251Schristos const OSSL_PARAM *propsp =
1330b0d17251Schristos OSSL_PARAM_locate_const(params,
1331b0d17251Schristos OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
1332b0d17251Schristos
1333b0d17251Schristos pmgf1mdname = mgf1mdname;
1334b0d17251Schristos if (!OSSL_PARAM_get_utf8_string(p, &pmgf1mdname, sizeof(mgf1mdname)))
1335b0d17251Schristos return 0;
1336b0d17251Schristos
1337b0d17251Schristos if (propsp != NULL) {
1338b0d17251Schristos pmgf1mdprops = mgf1mdprops;
1339b0d17251Schristos if (!OSSL_PARAM_get_utf8_string(propsp,
1340b0d17251Schristos &pmgf1mdprops, sizeof(mgf1mdprops)))
1341b0d17251Schristos return 0;
1342b0d17251Schristos }
1343b0d17251Schristos
1344b0d17251Schristos if (pad_mode != RSA_PKCS1_PSS_PADDING) {
1345b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
1346b0d17251Schristos return 0;
1347b0d17251Schristos }
1348b0d17251Schristos }
1349b0d17251Schristos
1350b0d17251Schristos prsactx->saltlen = saltlen;
1351b0d17251Schristos prsactx->pad_mode = pad_mode;
1352b0d17251Schristos
1353b0d17251Schristos if (prsactx->md == NULL && pmdname == NULL
1354b0d17251Schristos && pad_mode == RSA_PKCS1_PSS_PADDING)
1355b0d17251Schristos pmdname = RSA_DEFAULT_DIGEST_NAME;
1356b0d17251Schristos
1357b0d17251Schristos if (pmgf1mdname != NULL
1358b0d17251Schristos && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
1359b0d17251Schristos return 0;
1360b0d17251Schristos
1361b0d17251Schristos if (pmdname != NULL) {
1362b0d17251Schristos if (!rsa_setup_md(prsactx, pmdname, pmdprops))
1363b0d17251Schristos return 0;
1364b0d17251Schristos } else {
1365b0d17251Schristos if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
1366b0d17251Schristos return 0;
1367b0d17251Schristos }
1368b0d17251Schristos return 1;
1369b0d17251Schristos }
1370b0d17251Schristos
1371b0d17251Schristos static const OSSL_PARAM settable_ctx_params[] = {
1372b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
1373b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
1374b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1375b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1376b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1377b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1378b0d17251Schristos OSSL_PARAM_END
1379b0d17251Schristos };
1380b0d17251Schristos
1381b0d17251Schristos static const OSSL_PARAM settable_ctx_params_no_digest[] = {
1382b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
1383b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
1384b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
1385b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
1386b0d17251Schristos OSSL_PARAM_END
1387b0d17251Schristos };
1388b0d17251Schristos
rsa_settable_ctx_params(void * vprsactx,ossl_unused void * provctx)1389b0d17251Schristos static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
1390b0d17251Schristos ossl_unused void *provctx)
1391b0d17251Schristos {
1392b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1393b0d17251Schristos
1394b0d17251Schristos if (prsactx != NULL && !prsactx->flag_allow_md)
1395b0d17251Schristos return settable_ctx_params_no_digest;
1396b0d17251Schristos return settable_ctx_params;
1397b0d17251Schristos }
1398b0d17251Schristos
rsa_get_ctx_md_params(void * vprsactx,OSSL_PARAM * params)1399b0d17251Schristos static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
1400b0d17251Schristos {
1401b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1402b0d17251Schristos
1403b0d17251Schristos if (prsactx->mdctx == NULL)
1404b0d17251Schristos return 0;
1405b0d17251Schristos
1406b0d17251Schristos return EVP_MD_CTX_get_params(prsactx->mdctx, params);
1407b0d17251Schristos }
1408b0d17251Schristos
rsa_gettable_ctx_md_params(void * vprsactx)1409b0d17251Schristos static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
1410b0d17251Schristos {
1411b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1412b0d17251Schristos
1413b0d17251Schristos if (prsactx->md == NULL)
1414b0d17251Schristos return 0;
1415b0d17251Schristos
1416b0d17251Schristos return EVP_MD_gettable_ctx_params(prsactx->md);
1417b0d17251Schristos }
1418b0d17251Schristos
rsa_set_ctx_md_params(void * vprsactx,const OSSL_PARAM params[])1419b0d17251Schristos static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
1420b0d17251Schristos {
1421b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1422b0d17251Schristos
1423b0d17251Schristos if (prsactx->mdctx == NULL)
1424b0d17251Schristos return 0;
1425b0d17251Schristos
1426b0d17251Schristos return EVP_MD_CTX_set_params(prsactx->mdctx, params);
1427b0d17251Schristos }
1428b0d17251Schristos
rsa_settable_ctx_md_params(void * vprsactx)1429b0d17251Schristos static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
1430b0d17251Schristos {
1431b0d17251Schristos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
1432b0d17251Schristos
1433b0d17251Schristos if (prsactx->md == NULL)
1434b0d17251Schristos return 0;
1435b0d17251Schristos
1436b0d17251Schristos return EVP_MD_settable_ctx_params(prsactx->md);
1437b0d17251Schristos }
1438b0d17251Schristos
1439b0d17251Schristos const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
1440b0d17251Schristos { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
1441b0d17251Schristos { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
1442b0d17251Schristos { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
1443b0d17251Schristos { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
1444b0d17251Schristos { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
1445b0d17251Schristos { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
1446b0d17251Schristos (void (*)(void))rsa_verify_recover_init },
1447b0d17251Schristos { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
1448b0d17251Schristos (void (*)(void))rsa_verify_recover },
1449b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
1450b0d17251Schristos (void (*)(void))rsa_digest_sign_init },
1451b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
1452b0d17251Schristos (void (*)(void))rsa_digest_signverify_update },
1453b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
1454b0d17251Schristos (void (*)(void))rsa_digest_sign_final },
1455b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
1456b0d17251Schristos (void (*)(void))rsa_digest_verify_init },
1457b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
1458b0d17251Schristos (void (*)(void))rsa_digest_signverify_update },
1459b0d17251Schristos { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
1460b0d17251Schristos (void (*)(void))rsa_digest_verify_final },
1461b0d17251Schristos { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
1462b0d17251Schristos { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
1463b0d17251Schristos { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
1464b0d17251Schristos { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
1465b0d17251Schristos (void (*)(void))rsa_gettable_ctx_params },
1466b0d17251Schristos { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
1467b0d17251Schristos { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
1468b0d17251Schristos (void (*)(void))rsa_settable_ctx_params },
1469b0d17251Schristos { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
1470b0d17251Schristos (void (*)(void))rsa_get_ctx_md_params },
1471b0d17251Schristos { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
1472b0d17251Schristos (void (*)(void))rsa_gettable_ctx_md_params },
1473b0d17251Schristos { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
1474b0d17251Schristos (void (*)(void))rsa_set_ctx_md_params },
1475b0d17251Schristos { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
1476b0d17251Schristos (void (*)(void))rsa_settable_ctx_md_params },
1477b0d17251Schristos { 0, NULL }
1478b0d17251Schristos };
1479