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