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