/src/openssl/providers/implementations/signature/rsa_sig.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 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/obj_mac.h> |
22 | | #include <openssl/rsa.h> |
23 | | #include <openssl/params.h> |
24 | | #include <openssl/evp.h> |
25 | | #include <openssl/proverr.h> |
26 | | #include "internal/cryptlib.h" |
27 | | #include "internal/nelem.h" |
28 | | #include "internal/sizes.h" |
29 | | #include "crypto/rsa.h" |
30 | | #include "prov/providercommon.h" |
31 | | #include "prov/implementations.h" |
32 | | #include "prov/provider_ctx.h" |
33 | | #include "prov/der_rsa.h" |
34 | | #include "prov/securitycheck.h" |
35 | | #include "internal/fips.h" |
36 | | |
37 | | #define rsa_set_ctx_params_no_digest_st rsa_set_ctx_params_st |
38 | | |
39 | | #include "providers/implementations/signature/rsa_sig.inc" |
40 | | |
41 | 0 | #define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1 |
42 | | |
43 | | static OSSL_FUNC_signature_newctx_fn rsa_newctx; |
44 | | static OSSL_FUNC_signature_sign_init_fn rsa_sign_init; |
45 | | static OSSL_FUNC_signature_verify_init_fn rsa_verify_init; |
46 | | static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init; |
47 | | static OSSL_FUNC_signature_sign_fn rsa_sign; |
48 | | static OSSL_FUNC_signature_sign_message_update_fn rsa_signverify_message_update; |
49 | | static OSSL_FUNC_signature_sign_message_final_fn rsa_sign_message_final; |
50 | | static OSSL_FUNC_signature_verify_fn rsa_verify; |
51 | | static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover; |
52 | | static OSSL_FUNC_signature_verify_message_update_fn rsa_signverify_message_update; |
53 | | static OSSL_FUNC_signature_verify_message_final_fn rsa_verify_message_final; |
54 | | static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init; |
55 | | static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_sign_update; |
56 | | static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final; |
57 | | static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init; |
58 | | static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_verify_update; |
59 | | static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final; |
60 | | static OSSL_FUNC_signature_freectx_fn rsa_freectx; |
61 | | static OSSL_FUNC_signature_dupctx_fn rsa_dupctx; |
62 | | static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types; |
63 | | static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params; |
64 | | static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params; |
65 | | static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params; |
66 | | static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params; |
67 | | static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params; |
68 | | static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params; |
69 | | static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params; |
70 | | static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params; |
71 | | static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params; |
72 | | static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params; |
73 | | |
74 | | static OSSL_ITEM padding_item[] = { |
75 | | { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 }, |
76 | | { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE }, |
77 | | { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 }, |
78 | | { RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS }, |
79 | | { 0, NULL } |
80 | | }; |
81 | | |
82 | | /* |
83 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
84 | | * We happen to know that our KEYMGMT simply passes RSA structures, so |
85 | | * we use that here too. |
86 | | */ |
87 | | |
88 | | typedef struct { |
89 | | OSSL_LIB_CTX *libctx; |
90 | | char *propq; |
91 | | RSA *rsa; |
92 | | int operation; |
93 | | |
94 | | /* |
95 | | * Flag to determine if a full sigalg is run (1) or if a composable |
96 | | * signature algorithm is run (0). |
97 | | * |
98 | | * When a full sigalg is run (1), this currently affects the following |
99 | | * other flags, which are to remain untouched after their initialization: |
100 | | * |
101 | | * - flag_allow_md (initialized to 0) |
102 | | */ |
103 | | unsigned int flag_sigalg : 1; |
104 | | /* |
105 | | * Flag to determine if the hash function can be changed (1) or not (0) |
106 | | * Because it's dangerous to change during a DigestSign or DigestVerify |
107 | | * operation, this flag is cleared by their Init function, and set again |
108 | | * by their Final function. |
109 | | * Implementations of full sigalgs (such as RSA-SHA256) hard-code this |
110 | | * flag to not allow changes (0). |
111 | | */ |
112 | | unsigned int flag_allow_md : 1; |
113 | | unsigned int mgf1_md_set : 1; |
114 | | /* |
115 | | * Flags to say what are the possible next external calls in what |
116 | | * constitutes the life cycle of an algorithm. The relevant calls are: |
117 | | * - init |
118 | | * - update |
119 | | * - final |
120 | | * - oneshot |
121 | | * All other external calls are regarded as utilitarian and are allowed |
122 | | * at any time (they may be affected by other flags, like flag_allow_md, |
123 | | * though). |
124 | | */ |
125 | | unsigned int flag_allow_update : 1; |
126 | | unsigned int flag_allow_final : 1; |
127 | | unsigned int flag_allow_oneshot : 1; |
128 | | |
129 | | /* main digest */ |
130 | | EVP_MD *md; |
131 | | EVP_MD_CTX *mdctx; |
132 | | int mdnid; |
133 | | char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */ |
134 | | |
135 | | /* RSA padding mode */ |
136 | | int pad_mode; |
137 | | /* message digest for MGF1 */ |
138 | | EVP_MD *mgf1_md; |
139 | | int mgf1_mdnid; |
140 | | char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */ |
141 | | /* PSS salt length */ |
142 | | int saltlen; |
143 | | /* Minimum salt length or -1 if no PSS parameter restriction */ |
144 | | int min_saltlen; |
145 | | |
146 | | /* Signature, for verification */ |
147 | | unsigned char *sig; |
148 | | size_t siglen; |
149 | | |
150 | | #ifdef FIPS_MODULE |
151 | | /* |
152 | | * FIPS 140-3 IG 2.4.B mandates that verification based on a digest of a |
153 | | * message is not permitted. However, signing based on a digest is still |
154 | | * permitted. |
155 | | */ |
156 | | int verify_message; |
157 | | #endif |
158 | | |
159 | | /* Temp buffer */ |
160 | | unsigned char *tbuf; |
161 | | |
162 | | OSSL_FIPS_IND_DECLARE |
163 | | } PROV_RSA_CTX; |
164 | | |
165 | | /* True if PSS parameters are restricted */ |
166 | 0 | #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1) |
167 | | |
168 | | static int rsa_get_md_size(const PROV_RSA_CTX *prsactx) |
169 | 0 | { |
170 | 0 | int md_size; |
171 | |
|
172 | 0 | if (prsactx->md != NULL) { |
173 | 0 | md_size = EVP_MD_get_size(prsactx->md); |
174 | 0 | if (md_size <= 0) |
175 | 0 | return 0; |
176 | 0 | return md_size; |
177 | 0 | } |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | | static int rsa_check_padding(const PROV_RSA_CTX *prsactx, |
182 | | const char *mdname, const char *mgf1_mdname, |
183 | | int mdnid) |
184 | 0 | { |
185 | 0 | switch (prsactx->pad_mode) { |
186 | 0 | case RSA_NO_PADDING: |
187 | 0 | if (mdname != NULL || mdnid != NID_undef) { |
188 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE); |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | break; |
192 | 0 | case RSA_X931_PADDING: |
193 | 0 | if (RSA_X931_hash_id(mdnid) == -1) { |
194 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST); |
195 | 0 | return 0; |
196 | 0 | } |
197 | 0 | break; |
198 | 0 | case RSA_PKCS1_PSS_PADDING: |
199 | 0 | if (rsa_pss_restricted(prsactx)) |
200 | 0 | if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname)) |
201 | 0 | || (mgf1_mdname != NULL |
202 | 0 | && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) { |
203 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
204 | 0 | return 0; |
205 | 0 | } |
206 | 0 | break; |
207 | 0 | default: |
208 | 0 | break; |
209 | 0 | } |
210 | | |
211 | 0 | return 1; |
212 | 0 | } |
213 | | |
214 | | static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen) |
215 | 0 | { |
216 | 0 | if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
217 | 0 | int max_saltlen; |
218 | | |
219 | | /* See if minimum salt length exceeds maximum possible */ |
220 | 0 | max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md); |
221 | 0 | if ((RSA_bits(prsactx->rsa) & 0x7) == 1) |
222 | 0 | max_saltlen--; |
223 | 0 | if (min_saltlen < 0 || min_saltlen > max_saltlen) { |
224 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); |
225 | 0 | return 0; |
226 | 0 | } |
227 | 0 | prsactx->min_saltlen = min_saltlen; |
228 | 0 | } |
229 | 0 | return 1; |
230 | 0 | } |
231 | | |
232 | | static void *rsa_newctx(void *provctx, const char *propq) |
233 | 0 | { |
234 | 0 | PROV_RSA_CTX *prsactx = NULL; |
235 | 0 | char *propq_copy = NULL; |
236 | |
|
237 | 0 | if (!ossl_prov_is_running()) |
238 | 0 | return NULL; |
239 | | |
240 | | #ifdef FIPS_MODULE |
241 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
242 | | ST_ID_SIG_RSA_SHA256)) |
243 | | return NULL; |
244 | | #endif |
245 | | |
246 | 0 | if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL |
247 | 0 | || (propq != NULL |
248 | 0 | && (propq_copy = OPENSSL_strdup(propq)) == NULL)) { |
249 | 0 | OPENSSL_free(prsactx); |
250 | 0 | return NULL; |
251 | 0 | } |
252 | | |
253 | 0 | OSSL_FIPS_IND_INIT(prsactx) |
254 | 0 | prsactx->libctx = PROV_LIBCTX_OF(provctx); |
255 | 0 | prsactx->flag_allow_md = 1; |
256 | | #ifdef FIPS_MODULE |
257 | | prsactx->verify_message = 1; |
258 | | #endif |
259 | 0 | prsactx->propq = propq_copy; |
260 | | /* Maximum up to digest length for sign, auto for verify */ |
261 | 0 | prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX; |
262 | 0 | prsactx->min_saltlen = -1; |
263 | 0 | return prsactx; |
264 | 0 | } |
265 | | |
266 | | static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx) |
267 | 0 | { |
268 | 0 | int saltlen = ctx->saltlen; |
269 | 0 | int saltlenMax = -1; |
270 | | |
271 | | /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection |
272 | | * 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the |
273 | | * salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of |
274 | | * the hash function output block (in bytes)." |
275 | | * |
276 | | * Provide a way to use at most the digest length, so that the default does |
277 | | * not violate FIPS 186-4. */ |
278 | 0 | if (saltlen == RSA_PSS_SALTLEN_DIGEST) { |
279 | 0 | if ((saltlen = EVP_MD_get_size(ctx->md)) <= 0) { |
280 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST); |
281 | 0 | return -1; |
282 | 0 | } |
283 | 0 | } else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) { |
284 | 0 | saltlen = RSA_PSS_SALTLEN_MAX; |
285 | 0 | if ((saltlenMax = EVP_MD_get_size(ctx->md)) <= 0) { |
286 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST); |
287 | 0 | return -1; |
288 | 0 | } |
289 | 0 | } |
290 | 0 | if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) { |
291 | 0 | int mdsize, rsasize; |
292 | |
|
293 | 0 | if ((mdsize = EVP_MD_get_size(ctx->md)) <= 0) { |
294 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST); |
295 | 0 | return -1; |
296 | 0 | } |
297 | 0 | if ((rsasize = RSA_size(ctx->rsa)) <= 2 || rsasize - 2 < mdsize) { |
298 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
299 | 0 | return -1; |
300 | 0 | } |
301 | 0 | saltlen = rsasize - mdsize - 2; |
302 | 0 | if ((RSA_bits(ctx->rsa) & 0x7) == 1) |
303 | 0 | saltlen--; |
304 | 0 | if (saltlenMax >= 0 && saltlen > saltlenMax) |
305 | 0 | saltlen = saltlenMax; |
306 | 0 | } |
307 | 0 | if (saltlen < 0) { |
308 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
309 | 0 | return -1; |
310 | 0 | } else if (saltlen < ctx->min_saltlen) { |
311 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL, |
312 | 0 | "minimum salt length: %d, actual salt length: %d", |
313 | 0 | ctx->min_saltlen, saltlen); |
314 | 0 | return -1; |
315 | 0 | } |
316 | 0 | return saltlen; |
317 | 0 | } |
318 | | |
319 | | static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx, |
320 | | unsigned char *aid_buf, |
321 | | size_t buf_len, |
322 | | size_t *aid_len) |
323 | 0 | { |
324 | 0 | WPACKET pkt; |
325 | 0 | unsigned char *aid = NULL; |
326 | 0 | int saltlen; |
327 | 0 | RSA_PSS_PARAMS_30 pss_params; |
328 | 0 | int ret; |
329 | |
|
330 | 0 | if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) { |
331 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | | |
335 | 0 | switch (ctx->pad_mode) { |
336 | 0 | case RSA_PKCS1_PADDING: |
337 | 0 | ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1, |
338 | 0 | ctx->mdnid); |
339 | |
|
340 | 0 | if (ret > 0) { |
341 | 0 | break; |
342 | 0 | } else if (ret == 0) { |
343 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
344 | 0 | goto cleanup; |
345 | 0 | } |
346 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED, |
347 | 0 | "Algorithm ID generation - md NID: %d", |
348 | 0 | ctx->mdnid); |
349 | 0 | goto cleanup; |
350 | 0 | case RSA_PKCS1_PSS_PADDING: |
351 | 0 | saltlen = rsa_pss_compute_saltlen(ctx); |
352 | 0 | if (saltlen < 0) |
353 | 0 | goto cleanup; |
354 | 0 | if (!ossl_rsa_pss_params_30_set_defaults(&pss_params) |
355 | 0 | || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid) |
356 | 0 | || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params, |
357 | 0 | ctx->mgf1_mdnid) |
358 | 0 | || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen) |
359 | 0 | || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1, |
360 | 0 | RSA_FLAG_TYPE_RSASSAPSS, |
361 | 0 | &pss_params)) { |
362 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
363 | 0 | goto cleanup; |
364 | 0 | } |
365 | 0 | break; |
366 | 0 | default: |
367 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED, |
368 | 0 | "Algorithm ID generation - pad mode: %d", |
369 | 0 | ctx->pad_mode); |
370 | 0 | goto cleanup; |
371 | 0 | } |
372 | 0 | if (WPACKET_finish(&pkt)) { |
373 | 0 | WPACKET_get_total_written(&pkt, aid_len); |
374 | 0 | aid = WPACKET_get_curr(&pkt); |
375 | 0 | } |
376 | 0 | cleanup: |
377 | 0 | WPACKET_cleanup(&pkt); |
378 | 0 | return aid; |
379 | 0 | } |
380 | | |
381 | | static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname, |
382 | | const char *mdprops, const char *desc) |
383 | 0 | { |
384 | 0 | EVP_MD *md = NULL; |
385 | |
|
386 | 0 | if (mdprops == NULL) |
387 | 0 | mdprops = ctx->propq; |
388 | |
|
389 | 0 | if (mdname != NULL) { |
390 | 0 | int md_nid; |
391 | 0 | size_t mdname_len = strlen(mdname); |
392 | |
|
393 | 0 | md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
394 | |
|
395 | 0 | if (md == NULL) { |
396 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
397 | 0 | "%s could not be fetched", mdname); |
398 | 0 | goto err; |
399 | 0 | } |
400 | 0 | md_nid = ossl_digest_rsa_sign_get_md_nid(md); |
401 | 0 | if (md_nid == NID_undef) { |
402 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
403 | 0 | "digest=%s", mdname); |
404 | 0 | goto err; |
405 | 0 | } |
406 | | /* |
407 | | * XOF digests are not allowed except for RSA PSS. |
408 | | * We don't support XOF digests with RSA PSS (yet), so just fail. |
409 | | * When we do support them, uncomment the second clause. |
410 | | */ |
411 | 0 | if (EVP_MD_xof(md) |
412 | 0 | /* && ctx->pad_mode != RSA_PKCS1_PSS_PADDING */) { |
413 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
414 | 0 | goto err; |
415 | 0 | } |
416 | | #ifdef FIPS_MODULE |
417 | | { |
418 | | int sha1_allowed |
419 | | = ((ctx->operation |
420 | | & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) |
421 | | == 0); |
422 | | |
423 | | if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx), |
424 | | OSSL_FIPS_IND_SETTABLE1, |
425 | | ctx->libctx, |
426 | | md_nid, sha1_allowed, 1, desc, |
427 | | ossl_fips_config_signature_digest_check)) |
428 | | goto err; |
429 | | } |
430 | | #endif |
431 | | |
432 | 0 | if (!rsa_check_padding(ctx, mdname, NULL, md_nid)) |
433 | 0 | goto err; |
434 | 0 | if (mdname_len >= sizeof(ctx->mdname)) { |
435 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
436 | 0 | "%s exceeds name buffer length", mdname); |
437 | 0 | goto err; |
438 | 0 | } |
439 | | |
440 | 0 | if (!ctx->flag_allow_md) { |
441 | 0 | if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { |
442 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
443 | 0 | "digest %s != %s", mdname, ctx->mdname); |
444 | 0 | goto err; |
445 | 0 | } |
446 | 0 | EVP_MD_free(md); |
447 | 0 | return 1; |
448 | 0 | } |
449 | | |
450 | 0 | if (!ctx->mgf1_md_set) { |
451 | 0 | if (!EVP_MD_up_ref(md)) { |
452 | 0 | goto err; |
453 | 0 | } |
454 | 0 | EVP_MD_free(ctx->mgf1_md); |
455 | 0 | ctx->mgf1_md = md; |
456 | 0 | ctx->mgf1_mdnid = md_nid; |
457 | 0 | OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname)); |
458 | 0 | } |
459 | | |
460 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
461 | 0 | EVP_MD_free(ctx->md); |
462 | |
|
463 | 0 | ctx->mdctx = NULL; |
464 | 0 | ctx->md = md; |
465 | 0 | ctx->mdnid = md_nid; |
466 | 0 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
467 | 0 | } |
468 | | |
469 | 0 | return 1; |
470 | 0 | err: |
471 | 0 | EVP_MD_free(md); |
472 | 0 | return 0; |
473 | 0 | } |
474 | | |
475 | | static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname, |
476 | | const char *mdprops) |
477 | 0 | { |
478 | 0 | size_t len; |
479 | 0 | EVP_MD *md = NULL; |
480 | 0 | int mdnid; |
481 | |
|
482 | 0 | if (mdprops == NULL) |
483 | 0 | mdprops = ctx->propq; |
484 | |
|
485 | 0 | if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) { |
486 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
487 | 0 | "%s could not be fetched", mdname); |
488 | 0 | return 0; |
489 | 0 | } |
490 | | /* The default for mgf1 is SHA1 - so allow SHA1 */ |
491 | 0 | if ((mdnid = ossl_digest_rsa_sign_get_md_nid(md)) <= 0 |
492 | 0 | || !rsa_check_padding(ctx, NULL, mdname, mdnid)) { |
493 | 0 | if (mdnid <= 0) |
494 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
495 | 0 | "digest=%s", mdname); |
496 | 0 | EVP_MD_free(md); |
497 | 0 | return 0; |
498 | 0 | } |
499 | 0 | len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname)); |
500 | 0 | if (len >= sizeof(ctx->mgf1_mdname)) { |
501 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
502 | 0 | "%s exceeds name buffer length", mdname); |
503 | 0 | EVP_MD_free(md); |
504 | 0 | return 0; |
505 | 0 | } |
506 | | |
507 | 0 | EVP_MD_free(ctx->mgf1_md); |
508 | 0 | ctx->mgf1_md = md; |
509 | 0 | ctx->mgf1_mdnid = mdnid; |
510 | 0 | ctx->mgf1_md_set = 1; |
511 | 0 | return 1; |
512 | 0 | } |
513 | | |
514 | | static int |
515 | | rsa_signverify_init(PROV_RSA_CTX *prsactx, void *vrsa, |
516 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
517 | | const OSSL_PARAM params[], int operation, |
518 | | const char *desc) |
519 | 0 | { |
520 | 0 | int protect; |
521 | |
|
522 | 0 | if (!ossl_prov_is_running() || prsactx == NULL) |
523 | 0 | return 0; |
524 | | |
525 | 0 | if (vrsa == NULL && prsactx->rsa == NULL) { |
526 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
527 | 0 | return 0; |
528 | 0 | } |
529 | | |
530 | 0 | if (vrsa != NULL) { |
531 | 0 | if (!RSA_up_ref(vrsa)) |
532 | 0 | return 0; |
533 | 0 | RSA_free(prsactx->rsa); |
534 | 0 | prsactx->rsa = vrsa; |
535 | 0 | } |
536 | 0 | if (!ossl_rsa_key_op_get_protect(prsactx->rsa, operation, &protect)) |
537 | 0 | return 0; |
538 | | |
539 | 0 | prsactx->operation = operation; |
540 | 0 | prsactx->flag_allow_update = 1; |
541 | 0 | prsactx->flag_allow_final = 1; |
542 | 0 | prsactx->flag_allow_oneshot = 1; |
543 | | |
544 | | /* Maximize up to digest length for sign, auto for verify */ |
545 | 0 | prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX; |
546 | 0 | prsactx->min_saltlen = -1; |
547 | |
|
548 | 0 | switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) { |
549 | 0 | case RSA_FLAG_TYPE_RSA: |
550 | 0 | prsactx->pad_mode = RSA_PKCS1_PADDING; |
551 | 0 | break; |
552 | 0 | case RSA_FLAG_TYPE_RSASSAPSS: |
553 | 0 | prsactx->pad_mode = RSA_PKCS1_PSS_PADDING; |
554 | |
|
555 | 0 | { |
556 | 0 | const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30(prsactx->rsa); |
557 | |
|
558 | 0 | if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) { |
559 | 0 | int md_nid = ossl_rsa_pss_params_30_hashalg(pss); |
560 | 0 | int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss); |
561 | 0 | int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss); |
562 | 0 | const char *mdname, *mgf1mdname; |
563 | 0 | size_t len; |
564 | |
|
565 | 0 | mdname = ossl_rsa_oaeppss_nid2name(md_nid); |
566 | 0 | mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid); |
567 | |
|
568 | 0 | if (mdname == NULL) { |
569 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
570 | 0 | "PSS restrictions lack hash algorithm"); |
571 | 0 | return 0; |
572 | 0 | } |
573 | 0 | if (mgf1mdname == NULL) { |
574 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
575 | 0 | "PSS restrictions lack MGF1 hash algorithm"); |
576 | 0 | return 0; |
577 | 0 | } |
578 | | |
579 | 0 | len = OPENSSL_strlcpy(prsactx->mdname, mdname, |
580 | 0 | sizeof(prsactx->mdname)); |
581 | 0 | if (len >= sizeof(prsactx->mdname)) { |
582 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
583 | 0 | "hash algorithm name too long"); |
584 | 0 | return 0; |
585 | 0 | } |
586 | 0 | len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname, |
587 | 0 | sizeof(prsactx->mgf1_mdname)); |
588 | 0 | if (len >= sizeof(prsactx->mgf1_mdname)) { |
589 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
590 | 0 | "MGF1 hash algorithm name too long"); |
591 | 0 | return 0; |
592 | 0 | } |
593 | 0 | prsactx->saltlen = min_saltlen; |
594 | | |
595 | | /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */ |
596 | 0 | if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq) |
597 | 0 | || !rsa_setup_md(prsactx, mdname, prsactx->propq, desc) |
598 | 0 | || !rsa_check_parameters(prsactx, min_saltlen)) |
599 | 0 | return 0; |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | 0 | break; |
604 | 0 | default: |
605 | 0 | ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
606 | 0 | return 0; |
607 | 0 | } |
608 | | |
609 | 0 | OSSL_FIPS_IND_SET_APPROVED(prsactx) |
610 | 0 | if (!set_ctx_params(prsactx, params)) |
611 | 0 | return 0; |
612 | | #ifdef FIPS_MODULE |
613 | | if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx), |
614 | | OSSL_FIPS_IND_SETTABLE0, prsactx->libctx, |
615 | | prsactx->rsa, desc, protect)) |
616 | | return 0; |
617 | | #endif |
618 | 0 | return 1; |
619 | 0 | } |
620 | | |
621 | | static int setup_tbuf(PROV_RSA_CTX *ctx) |
622 | 0 | { |
623 | 0 | if (ctx->tbuf != NULL) |
624 | 0 | return 1; |
625 | 0 | if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL) |
626 | 0 | return 0; |
627 | 0 | return 1; |
628 | 0 | } |
629 | | |
630 | | static void clean_tbuf(PROV_RSA_CTX *ctx) |
631 | 0 | { |
632 | 0 | if (ctx->tbuf != NULL) |
633 | 0 | OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa)); |
634 | 0 | } |
635 | | |
636 | | static void free_tbuf(PROV_RSA_CTX *ctx) |
637 | 0 | { |
638 | 0 | clean_tbuf(ctx); |
639 | 0 | OPENSSL_free(ctx->tbuf); |
640 | 0 | ctx->tbuf = NULL; |
641 | 0 | } |
642 | | |
643 | | #ifdef FIPS_MODULE |
644 | | static int rsa_pss_saltlen_check_passed(PROV_RSA_CTX *ctx, const char *algoname, int saltlen) |
645 | | { |
646 | | int mdsize = rsa_get_md_size(ctx); |
647 | | /* |
648 | | * Perform the check if the salt length is compliant to FIPS 186-5. |
649 | | * |
650 | | * According to FIPS 186-5 5.4 (g), the salt length shall be between zero |
651 | | * and the output block length of the digest function (inclusive). |
652 | | */ |
653 | | int approved = (saltlen >= 0 && saltlen <= mdsize); |
654 | | |
655 | | if (!approved) { |
656 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE3, |
657 | | ctx->libctx, |
658 | | algoname, "PSS Salt Length", |
659 | | ossl_fips_config_rsa_pss_saltlen_check)) { |
660 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); |
661 | | return 0; |
662 | | } |
663 | | } |
664 | | |
665 | | return 1; |
666 | | } |
667 | | #endif |
668 | | |
669 | | static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[]) |
670 | 0 | { |
671 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
672 | |
|
673 | | #ifdef FIPS_MODULE |
674 | | if (prsactx != NULL) |
675 | | prsactx->verify_message = 1; |
676 | | #endif |
677 | |
|
678 | 0 | return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params, |
679 | 0 | EVP_PKEY_OP_SIGN, "RSA Sign Init"); |
680 | 0 | } |
681 | | |
682 | | /* |
683 | | * Sign tbs without digesting it first. This is suitable for "primitive" |
684 | | * signing and signing the digest of a message, i.e. should be used with |
685 | | * implementations of the keytype related algorithms. |
686 | | */ |
687 | | static int rsa_sign_directly(PROV_RSA_CTX *prsactx, |
688 | | unsigned char *sig, size_t *siglen, size_t sigsize, |
689 | | const unsigned char *tbs, size_t tbslen) |
690 | 0 | { |
691 | 0 | int ret; |
692 | 0 | size_t rsasize = RSA_size(prsactx->rsa); |
693 | 0 | size_t mdsize = rsa_get_md_size(prsactx); |
694 | |
|
695 | 0 | if (!ossl_prov_is_running()) |
696 | 0 | return 0; |
697 | | |
698 | 0 | if (sig == NULL) { |
699 | 0 | *siglen = rsasize; |
700 | 0 | return 1; |
701 | 0 | } |
702 | | |
703 | 0 | if (sigsize < rsasize) { |
704 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE, |
705 | 0 | "is %zu, should be at least %zu", sigsize, rsasize); |
706 | 0 | return 0; |
707 | 0 | } |
708 | | |
709 | 0 | if (mdsize != 0) { |
710 | 0 | if (tbslen != mdsize) { |
711 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
712 | 0 | return 0; |
713 | 0 | } |
714 | | |
715 | 0 | #ifndef FIPS_MODULE |
716 | 0 | if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) { |
717 | 0 | unsigned int sltmp; |
718 | |
|
719 | 0 | if (prsactx->pad_mode != RSA_PKCS1_PADDING) { |
720 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE, |
721 | 0 | "only PKCS#1 padding supported with MDC2"); |
722 | 0 | return 0; |
723 | 0 | } |
724 | 0 | ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, (unsigned int)tbslen, sig, |
725 | 0 | &sltmp, prsactx->rsa); |
726 | |
|
727 | 0 | if (ret <= 0) { |
728 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
729 | 0 | return 0; |
730 | 0 | } |
731 | 0 | ret = sltmp; |
732 | 0 | goto end; |
733 | 0 | } |
734 | 0 | #endif |
735 | 0 | switch (prsactx->pad_mode) { |
736 | 0 | case RSA_X931_PADDING: |
737 | 0 | if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) { |
738 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL, |
739 | 0 | "RSA key size = %d, expected minimum = %d", |
740 | 0 | RSA_size(prsactx->rsa), tbslen + 1); |
741 | 0 | return 0; |
742 | 0 | } |
743 | 0 | if (!setup_tbuf(prsactx)) { |
744 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB); |
745 | 0 | return 0; |
746 | 0 | } |
747 | 0 | memcpy(prsactx->tbuf, tbs, tbslen); |
748 | 0 | prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid); |
749 | 0 | ret = RSA_private_encrypt((int)(tbslen + 1), prsactx->tbuf, |
750 | 0 | sig, prsactx->rsa, RSA_X931_PADDING); |
751 | 0 | clean_tbuf(prsactx); |
752 | 0 | break; |
753 | 0 | case RSA_PKCS1_PADDING: { |
754 | 0 | unsigned int sltmp; |
755 | |
|
756 | 0 | ret = RSA_sign(prsactx->mdnid, tbs, (unsigned int)tbslen, |
757 | 0 | sig, &sltmp, prsactx->rsa); |
758 | 0 | if (ret <= 0) { |
759 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
760 | 0 | return 0; |
761 | 0 | } |
762 | 0 | ret = sltmp; |
763 | 0 | } break; |
764 | | |
765 | 0 | case RSA_PKCS1_PSS_PADDING: { |
766 | 0 | int saltlen; |
767 | | |
768 | | /* Check PSS restrictions */ |
769 | 0 | if (rsa_pss_restricted(prsactx)) { |
770 | 0 | switch (prsactx->saltlen) { |
771 | 0 | case RSA_PSS_SALTLEN_DIGEST: |
772 | 0 | if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) { |
773 | 0 | ERR_raise_data(ERR_LIB_PROV, |
774 | 0 | PROV_R_PSS_SALTLEN_TOO_SMALL, |
775 | 0 | "minimum salt length set to %d, " |
776 | 0 | "but the digest only gives %d", |
777 | 0 | prsactx->min_saltlen, |
778 | 0 | EVP_MD_get_size(prsactx->md)); |
779 | 0 | return 0; |
780 | 0 | } |
781 | | /* FALLTHRU */ |
782 | 0 | default: |
783 | 0 | if (prsactx->saltlen >= 0 |
784 | 0 | && prsactx->saltlen < prsactx->min_saltlen) { |
785 | 0 | ERR_raise_data(ERR_LIB_PROV, |
786 | 0 | PROV_R_PSS_SALTLEN_TOO_SMALL, |
787 | 0 | "minimum salt length set to %d, but the" |
788 | 0 | "actual salt length is only set to %d", |
789 | 0 | prsactx->min_saltlen, |
790 | 0 | prsactx->saltlen); |
791 | 0 | return 0; |
792 | 0 | } |
793 | 0 | break; |
794 | 0 | } |
795 | 0 | } |
796 | 0 | if (!setup_tbuf(prsactx)) |
797 | 0 | return 0; |
798 | 0 | saltlen = prsactx->saltlen; |
799 | 0 | if (!ossl_rsa_padding_add_PKCS1_PSS_mgf1(prsactx->rsa, |
800 | 0 | prsactx->tbuf, tbs, |
801 | 0 | prsactx->md, prsactx->mgf1_md, |
802 | 0 | &saltlen)) { |
803 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
804 | 0 | return 0; |
805 | 0 | } |
806 | | #ifdef FIPS_MODULE |
807 | | if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Sign", saltlen)) |
808 | | return 0; |
809 | | #endif |
810 | 0 | ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf, |
811 | 0 | sig, prsactx->rsa, RSA_NO_PADDING); |
812 | 0 | clean_tbuf(prsactx); |
813 | 0 | } break; |
814 | | |
815 | 0 | default: |
816 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE, |
817 | 0 | "Only X.931, PKCS#1 v1.5 or PSS padding allowed"); |
818 | 0 | return 0; |
819 | 0 | } |
820 | 0 | } else { |
821 | 0 | ret = RSA_private_encrypt((int)tbslen, tbs, sig, prsactx->rsa, |
822 | 0 | prsactx->pad_mode); |
823 | 0 | } |
824 | | |
825 | 0 | #ifndef FIPS_MODULE |
826 | 0 | end: |
827 | 0 | #endif |
828 | 0 | if (ret <= 0) { |
829 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
830 | 0 | return 0; |
831 | 0 | } |
832 | | |
833 | 0 | *siglen = ret; |
834 | 0 | return 1; |
835 | 0 | } |
836 | | |
837 | | static int rsa_signverify_message_update(void *vprsactx, |
838 | | const unsigned char *data, |
839 | | size_t datalen) |
840 | 0 | { |
841 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
842 | |
|
843 | 0 | if (prsactx == NULL || prsactx->mdctx == NULL) |
844 | 0 | return 0; |
845 | | |
846 | 0 | if (!prsactx->flag_allow_update) { |
847 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UPDATE_CALL_OUT_OF_ORDER); |
848 | 0 | return 0; |
849 | 0 | } |
850 | 0 | prsactx->flag_allow_oneshot = 0; |
851 | |
|
852 | 0 | return EVP_DigestUpdate(prsactx->mdctx, data, datalen); |
853 | 0 | } |
854 | | |
855 | | static int rsa_sign_message_final(void *vprsactx, unsigned char *sig, |
856 | | size_t *siglen, size_t sigsize) |
857 | 0 | { |
858 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
859 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
860 | 0 | unsigned int dlen = 0; |
861 | |
|
862 | 0 | if (!ossl_prov_is_running() || prsactx == NULL) |
863 | 0 | return 0; |
864 | 0 | if (prsactx->mdctx == NULL) |
865 | 0 | return 0; |
866 | 0 | if (!prsactx->flag_allow_final) { |
867 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER); |
868 | 0 | return 0; |
869 | 0 | } |
870 | | |
871 | | /* |
872 | | * If sig is NULL then we're just finding out the sig size. Other fields |
873 | | * are ignored. Defer to rsa_sign. |
874 | | */ |
875 | 0 | if (sig != NULL) { |
876 | | /* |
877 | | * The digests used here are all known (see rsa_get_md_nid()), so they |
878 | | * should not exceed the internal buffer size of EVP_MAX_MD_SIZE. |
879 | | */ |
880 | 0 | if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen)) |
881 | 0 | return 0; |
882 | | |
883 | 0 | prsactx->flag_allow_update = 0; |
884 | 0 | prsactx->flag_allow_oneshot = 0; |
885 | 0 | prsactx->flag_allow_final = 0; |
886 | 0 | } |
887 | | |
888 | 0 | return rsa_sign_directly(prsactx, sig, siglen, sigsize, digest, dlen); |
889 | 0 | } |
890 | | |
891 | | /* |
892 | | * If signing a message, digest tbs and sign the result. |
893 | | * Otherwise, sign tbs directly. |
894 | | */ |
895 | | static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen, |
896 | | size_t sigsize, const unsigned char *tbs, size_t tbslen) |
897 | 0 | { |
898 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
899 | |
|
900 | 0 | if (!ossl_prov_is_running() || prsactx == NULL) |
901 | 0 | return 0; |
902 | 0 | if (!prsactx->flag_allow_oneshot) { |
903 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER); |
904 | 0 | return 0; |
905 | 0 | } |
906 | | |
907 | 0 | if (prsactx->operation == EVP_PKEY_OP_SIGNMSG) { |
908 | | /* |
909 | | * If |sig| is NULL, the caller is only looking for the sig length. |
910 | | * DO NOT update the input in this case. |
911 | | */ |
912 | 0 | if (sig == NULL) |
913 | 0 | return rsa_sign_message_final(prsactx, sig, siglen, sigsize); |
914 | | |
915 | 0 | return rsa_signverify_message_update(prsactx, tbs, tbslen) |
916 | 0 | && rsa_sign_message_final(prsactx, sig, siglen, sigsize); |
917 | 0 | } |
918 | 0 | return rsa_sign_directly(prsactx, sig, siglen, sigsize, tbs, tbslen); |
919 | 0 | } |
920 | | |
921 | | static int rsa_verify_recover_init(void *vprsactx, void *vrsa, |
922 | | const OSSL_PARAM params[]) |
923 | 0 | { |
924 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
925 | |
|
926 | | #ifdef FIPS_MODULE |
927 | | if (prsactx != NULL) |
928 | | prsactx->verify_message = 0; |
929 | | #endif |
930 | |
|
931 | 0 | return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params, |
932 | 0 | EVP_PKEY_OP_VERIFYRECOVER, "RSA VerifyRecover Init"); |
933 | 0 | } |
934 | | |
935 | | /* |
936 | | * There is no message variant of verify recover, so no need for |
937 | | * 'rsa_verify_recover_directly', just use this function, er, directly. |
938 | | */ |
939 | | static int rsa_verify_recover(void *vprsactx, |
940 | | unsigned char *rout, size_t *routlen, |
941 | | size_t routsize, |
942 | | const unsigned char *sig, size_t siglen) |
943 | 0 | { |
944 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
945 | 0 | int ret; |
946 | |
|
947 | 0 | if (!ossl_prov_is_running()) |
948 | 0 | return 0; |
949 | | |
950 | 0 | if (rout == NULL) { |
951 | 0 | *routlen = RSA_size(prsactx->rsa); |
952 | 0 | return 1; |
953 | 0 | } |
954 | | |
955 | 0 | if (prsactx->md != NULL) { |
956 | 0 | switch (prsactx->pad_mode) { |
957 | 0 | case RSA_X931_PADDING: |
958 | 0 | if (!setup_tbuf(prsactx)) |
959 | 0 | return 0; |
960 | 0 | ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa, |
961 | 0 | RSA_X931_PADDING); |
962 | 0 | if (ret <= 0) { |
963 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
964 | 0 | return 0; |
965 | 0 | } |
966 | 0 | ret--; |
967 | 0 | if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) { |
968 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH); |
969 | 0 | return 0; |
970 | 0 | } |
971 | 0 | if (ret != EVP_MD_get_size(prsactx->md)) { |
972 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH, |
973 | 0 | "Should be %d, but got %d", |
974 | 0 | EVP_MD_get_size(prsactx->md), ret); |
975 | 0 | return 0; |
976 | 0 | } |
977 | | |
978 | 0 | *routlen = ret; |
979 | 0 | if (rout != prsactx->tbuf) { |
980 | 0 | if (routsize < (size_t)ret) { |
981 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
982 | 0 | "buffer size is %d, should be %d", |
983 | 0 | routsize, ret); |
984 | 0 | return 0; |
985 | 0 | } |
986 | 0 | memcpy(rout, prsactx->tbuf, ret); |
987 | 0 | } |
988 | 0 | break; |
989 | | |
990 | 0 | case RSA_PKCS1_PADDING: { |
991 | 0 | size_t sltmp; |
992 | |
|
993 | 0 | ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp, |
994 | 0 | sig, siglen, prsactx->rsa); |
995 | 0 | if (ret <= 0) { |
996 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
997 | 0 | return 0; |
998 | 0 | } |
999 | 0 | ret = (int)sltmp; |
1000 | 0 | } break; |
1001 | | |
1002 | 0 | default: |
1003 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE, |
1004 | 0 | "Only X.931 or PKCS#1 v1.5 padding allowed"); |
1005 | 0 | return 0; |
1006 | 0 | } |
1007 | 0 | } else { |
1008 | 0 | ret = RSA_public_decrypt((int)siglen, sig, rout, prsactx->rsa, |
1009 | 0 | prsactx->pad_mode); |
1010 | 0 | if (ret <= 0) { |
1011 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
1012 | 0 | return 0; |
1013 | 0 | } |
1014 | 0 | } |
1015 | 0 | *routlen = ret; |
1016 | 0 | return 1; |
1017 | 0 | } |
1018 | | |
1019 | | static int rsa_verify_init(void *vprsactx, void *vrsa, |
1020 | | const OSSL_PARAM params[]) |
1021 | 0 | { |
1022 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1023 | |
|
1024 | | #ifdef FIPS_MODULE |
1025 | | if (prsactx != NULL) |
1026 | | prsactx->verify_message = 0; |
1027 | | #endif |
1028 | |
|
1029 | 0 | return rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params, |
1030 | 0 | EVP_PKEY_OP_VERIFY, "RSA Verify Init"); |
1031 | 0 | } |
1032 | | |
1033 | | static int rsa_verify_directly(PROV_RSA_CTX *prsactx, |
1034 | | const unsigned char *sig, size_t siglen, |
1035 | | const unsigned char *tbs, size_t tbslen) |
1036 | 0 | { |
1037 | 0 | size_t rslen; |
1038 | |
|
1039 | 0 | if (!ossl_prov_is_running()) |
1040 | 0 | return 0; |
1041 | 0 | if (prsactx->md != NULL) { |
1042 | 0 | switch (prsactx->pad_mode) { |
1043 | 0 | case RSA_PKCS1_PADDING: |
1044 | 0 | if (!RSA_verify(prsactx->mdnid, tbs, (unsigned int)tbslen, |
1045 | 0 | sig, (unsigned int)siglen, prsactx->rsa)) { |
1046 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
1047 | 0 | return 0; |
1048 | 0 | } |
1049 | 0 | return 1; |
1050 | 0 | case RSA_X931_PADDING: |
1051 | 0 | if (!setup_tbuf(prsactx)) |
1052 | 0 | return 0; |
1053 | 0 | if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0, |
1054 | 0 | sig, siglen) |
1055 | 0 | <= 0) |
1056 | 0 | return 0; |
1057 | 0 | break; |
1058 | 0 | case RSA_PKCS1_PSS_PADDING: { |
1059 | 0 | int ret; |
1060 | 0 | int saltlen; |
1061 | 0 | size_t mdsize; |
1062 | | |
1063 | | /* |
1064 | | * We need to check this for the RSA_verify_PKCS1_PSS_mgf1() |
1065 | | * call |
1066 | | */ |
1067 | 0 | mdsize = rsa_get_md_size(prsactx); |
1068 | 0 | if (tbslen != mdsize) { |
1069 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH, |
1070 | 0 | "Should be %d, but got %d", |
1071 | 0 | mdsize, tbslen); |
1072 | 0 | return 0; |
1073 | 0 | } |
1074 | | |
1075 | 0 | if (!setup_tbuf(prsactx)) |
1076 | 0 | return 0; |
1077 | 0 | ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, |
1078 | 0 | prsactx->rsa, RSA_NO_PADDING); |
1079 | 0 | if (ret <= 0) { |
1080 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
1081 | 0 | return 0; |
1082 | 0 | } |
1083 | 0 | saltlen = prsactx->saltlen; |
1084 | 0 | ret = ossl_rsa_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs, |
1085 | 0 | prsactx->md, prsactx->mgf1_md, |
1086 | 0 | prsactx->tbuf, |
1087 | 0 | &saltlen); |
1088 | 0 | if (ret <= 0) { |
1089 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
1090 | 0 | return 0; |
1091 | 0 | } |
1092 | | #ifdef FIPS_MODULE |
1093 | | if (!rsa_pss_saltlen_check_passed(prsactx, "RSA Verify", saltlen)) |
1094 | | return 0; |
1095 | | #endif |
1096 | 0 | return 1; |
1097 | 0 | } |
1098 | 0 | default: |
1099 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE, |
1100 | 0 | "Only X.931, PKCS#1 v1.5 or PSS padding allowed"); |
1101 | 0 | return 0; |
1102 | 0 | } |
1103 | 0 | } else { |
1104 | 0 | int ret; |
1105 | |
|
1106 | 0 | if (!setup_tbuf(prsactx)) |
1107 | 0 | return 0; |
1108 | 0 | ret = RSA_public_decrypt((int)siglen, sig, prsactx->tbuf, prsactx->rsa, |
1109 | 0 | prsactx->pad_mode); |
1110 | 0 | if (ret <= 0) { |
1111 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB); |
1112 | 0 | return 0; |
1113 | 0 | } |
1114 | 0 | rslen = (size_t)ret; |
1115 | 0 | } |
1116 | | |
1117 | 0 | if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen)) |
1118 | 0 | return 0; |
1119 | | |
1120 | 0 | return 1; |
1121 | 0 | } |
1122 | | |
1123 | | static int rsa_verify_set_sig(void *vprsactx, |
1124 | | const unsigned char *sig, size_t siglen) |
1125 | 0 | { |
1126 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1127 | 0 | OSSL_PARAM params[2]; |
1128 | |
|
1129 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, |
1130 | 0 | (unsigned char *)sig, siglen); |
1131 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1132 | 0 | return rsa_sigalg_set_ctx_params(prsactx, params); |
1133 | 0 | } |
1134 | | |
1135 | | static int rsa_verify_message_final(void *vprsactx) |
1136 | 0 | { |
1137 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1138 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
1139 | 0 | unsigned int dlen = 0; |
1140 | |
|
1141 | 0 | if (!ossl_prov_is_running() || prsactx == NULL) |
1142 | 0 | return 0; |
1143 | 0 | if (prsactx->mdctx == NULL) |
1144 | 0 | return 0; |
1145 | 0 | if (!prsactx->flag_allow_final) { |
1146 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FINAL_CALL_OUT_OF_ORDER); |
1147 | 0 | return 0; |
1148 | 0 | } |
1149 | | |
1150 | | /* |
1151 | | * The digests used here are all known (see rsa_get_md_nid()), so they |
1152 | | * should not exceed the internal buffer size of EVP_MAX_MD_SIZE. |
1153 | | */ |
1154 | 0 | if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen)) |
1155 | 0 | return 0; |
1156 | | |
1157 | 0 | prsactx->flag_allow_update = 0; |
1158 | 0 | prsactx->flag_allow_final = 0; |
1159 | 0 | prsactx->flag_allow_oneshot = 0; |
1160 | |
|
1161 | 0 | return rsa_verify_directly(prsactx, prsactx->sig, prsactx->siglen, |
1162 | 0 | digest, dlen); |
1163 | 0 | } |
1164 | | |
1165 | | /* |
1166 | | * If verifying a message, digest tbs and verify the result. |
1167 | | * Otherwise, verify tbs directly. |
1168 | | */ |
1169 | | static int rsa_verify(void *vprsactx, |
1170 | | const unsigned char *sig, size_t siglen, |
1171 | | const unsigned char *tbs, size_t tbslen) |
1172 | 0 | { |
1173 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1174 | |
|
1175 | 0 | if (!ossl_prov_is_running() || prsactx == NULL) |
1176 | 0 | return 0; |
1177 | 0 | if (!prsactx->flag_allow_oneshot) { |
1178 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ONESHOT_CALL_OUT_OF_ORDER); |
1179 | 0 | return 0; |
1180 | 0 | } |
1181 | | |
1182 | 0 | if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) |
1183 | 0 | return rsa_verify_set_sig(prsactx, sig, siglen) |
1184 | 0 | && rsa_signverify_message_update(prsactx, tbs, tbslen) |
1185 | 0 | && rsa_verify_message_final(prsactx); |
1186 | 0 | return rsa_verify_directly(prsactx, sig, siglen, tbs, tbslen); |
1187 | 0 | } |
1188 | | |
1189 | | /* DigestSign/DigestVerify wrappers */ |
1190 | | |
1191 | | static int rsa_digest_signverify_init(void *vprsactx, const char *mdname, |
1192 | | void *vrsa, const OSSL_PARAM params[], |
1193 | | int operation, const char *desc) |
1194 | 0 | { |
1195 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1196 | |
|
1197 | | #ifdef FIPS_MODULE |
1198 | | if (prsactx != NULL) |
1199 | | prsactx->verify_message = 1; |
1200 | | #endif |
1201 | |
|
1202 | 0 | if (!rsa_signverify_init(prsactx, vrsa, rsa_set_ctx_params, params, |
1203 | 0 | operation, desc)) |
1204 | 0 | return 0; |
1205 | | |
1206 | 0 | if (mdname != NULL |
1207 | | /* was rsa_setup_md already called in rsa_signverify_init()? */ |
1208 | 0 | && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0) |
1209 | 0 | && !rsa_setup_md(prsactx, mdname, prsactx->propq, desc)) |
1210 | 0 | return 0; |
1211 | | |
1212 | 0 | prsactx->flag_allow_md = 0; |
1213 | |
|
1214 | 0 | if (prsactx->mdctx == NULL) { |
1215 | 0 | prsactx->mdctx = EVP_MD_CTX_new(); |
1216 | 0 | if (prsactx->mdctx == NULL) |
1217 | 0 | goto error; |
1218 | 0 | } |
1219 | | |
1220 | 0 | if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params)) |
1221 | 0 | goto error; |
1222 | | |
1223 | 0 | return 1; |
1224 | | |
1225 | 0 | error: |
1226 | 0 | EVP_MD_CTX_free(prsactx->mdctx); |
1227 | 0 | prsactx->mdctx = NULL; |
1228 | 0 | return 0; |
1229 | 0 | } |
1230 | | |
1231 | | static int rsa_digest_sign_init(void *vprsactx, const char *mdname, |
1232 | | void *vrsa, const OSSL_PARAM params[]) |
1233 | 0 | { |
1234 | 0 | if (!ossl_prov_is_running()) |
1235 | 0 | return 0; |
1236 | 0 | return rsa_digest_signverify_init(vprsactx, mdname, vrsa, |
1237 | 0 | params, EVP_PKEY_OP_SIGNMSG, |
1238 | 0 | "RSA Digest Sign Init"); |
1239 | 0 | } |
1240 | | |
1241 | | static int rsa_digest_sign_update(void *vprsactx, const unsigned char *data, |
1242 | | size_t datalen) |
1243 | 0 | { |
1244 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1245 | |
|
1246 | 0 | if (prsactx == NULL) |
1247 | 0 | return 0; |
1248 | | /* Sigalg implementations shouldn't do digest_sign */ |
1249 | 0 | if (prsactx->flag_sigalg) |
1250 | 0 | return 0; |
1251 | | |
1252 | 0 | return rsa_signverify_message_update(prsactx, data, datalen); |
1253 | 0 | } |
1254 | | |
1255 | | static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig, |
1256 | | size_t *siglen, size_t sigsize) |
1257 | 0 | { |
1258 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1259 | 0 | int ok = 0; |
1260 | |
|
1261 | 0 | if (prsactx == NULL) |
1262 | 0 | return 0; |
1263 | | /* Sigalg implementations shouldn't do digest_sign */ |
1264 | 0 | if (prsactx->flag_sigalg) |
1265 | 0 | return 0; |
1266 | | |
1267 | 0 | if (rsa_sign_message_final(prsactx, sig, siglen, sigsize)) |
1268 | 0 | ok = 1; |
1269 | |
|
1270 | 0 | prsactx->flag_allow_md = 1; |
1271 | |
|
1272 | 0 | return ok; |
1273 | 0 | } |
1274 | | |
1275 | | static int rsa_digest_verify_init(void *vprsactx, const char *mdname, |
1276 | | void *vrsa, const OSSL_PARAM params[]) |
1277 | 0 | { |
1278 | 0 | if (!ossl_prov_is_running()) |
1279 | 0 | return 0; |
1280 | 0 | return rsa_digest_signverify_init(vprsactx, mdname, vrsa, |
1281 | 0 | params, EVP_PKEY_OP_VERIFYMSG, |
1282 | 0 | "RSA Digest Verify Init"); |
1283 | 0 | } |
1284 | | |
1285 | | static int rsa_digest_verify_update(void *vprsactx, const unsigned char *data, |
1286 | | size_t datalen) |
1287 | 0 | { |
1288 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1289 | |
|
1290 | 0 | if (prsactx == NULL) |
1291 | 0 | return 0; |
1292 | | /* Sigalg implementations shouldn't do digest_sign */ |
1293 | 0 | if (prsactx->flag_sigalg) |
1294 | 0 | return 0; |
1295 | | |
1296 | 0 | return rsa_signverify_message_update(prsactx, data, datalen); |
1297 | 0 | } |
1298 | | |
1299 | | int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig, |
1300 | | size_t siglen) |
1301 | 0 | { |
1302 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1303 | 0 | int ok = 0; |
1304 | |
|
1305 | 0 | if (prsactx == NULL) |
1306 | 0 | return 0; |
1307 | | /* Sigalg implementations shouldn't do digest_verify */ |
1308 | 0 | if (prsactx->flag_sigalg) |
1309 | 0 | return 0; |
1310 | | |
1311 | 0 | if (rsa_verify_set_sig(prsactx, sig, siglen) |
1312 | 0 | && rsa_verify_message_final(vprsactx)) |
1313 | 0 | ok = 1; |
1314 | |
|
1315 | 0 | prsactx->flag_allow_md = 1; |
1316 | |
|
1317 | 0 | return ok; |
1318 | 0 | } |
1319 | | |
1320 | | static void rsa_freectx(void *vprsactx) |
1321 | 0 | { |
1322 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1323 | |
|
1324 | 0 | if (prsactx == NULL) |
1325 | 0 | return; |
1326 | | |
1327 | 0 | EVP_MD_CTX_free(prsactx->mdctx); |
1328 | 0 | EVP_MD_free(prsactx->md); |
1329 | 0 | EVP_MD_free(prsactx->mgf1_md); |
1330 | 0 | OPENSSL_free(prsactx->sig); |
1331 | 0 | OPENSSL_free(prsactx->propq); |
1332 | 0 | free_tbuf(prsactx); |
1333 | 0 | RSA_free(prsactx->rsa); |
1334 | |
|
1335 | 0 | OPENSSL_clear_free(prsactx, sizeof(*prsactx)); |
1336 | 0 | } |
1337 | | |
1338 | | static void *rsa_dupctx(void *vprsactx) |
1339 | 0 | { |
1340 | 0 | PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; |
1341 | 0 | PROV_RSA_CTX *dstctx; |
1342 | |
|
1343 | 0 | if (!ossl_prov_is_running()) |
1344 | 0 | return NULL; |
1345 | | |
1346 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
1347 | 0 | if (dstctx == NULL) |
1348 | 0 | return NULL; |
1349 | | |
1350 | 0 | *dstctx = *srcctx; |
1351 | 0 | dstctx->rsa = NULL; |
1352 | 0 | dstctx->md = NULL; |
1353 | 0 | dstctx->mgf1_md = NULL; |
1354 | 0 | dstctx->mdctx = NULL; |
1355 | 0 | dstctx->tbuf = NULL; |
1356 | 0 | dstctx->propq = NULL; |
1357 | 0 | dstctx->sig = NULL; |
1358 | |
|
1359 | 0 | if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa)) |
1360 | 0 | goto err; |
1361 | 0 | dstctx->rsa = srcctx->rsa; |
1362 | |
|
1363 | 0 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
1364 | 0 | goto err; |
1365 | 0 | dstctx->md = srcctx->md; |
1366 | |
|
1367 | 0 | if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md)) |
1368 | 0 | goto err; |
1369 | 0 | dstctx->mgf1_md = srcctx->mgf1_md; |
1370 | |
|
1371 | 0 | if (srcctx->mdctx != NULL) { |
1372 | 0 | dstctx->mdctx = EVP_MD_CTX_new(); |
1373 | 0 | if (dstctx->mdctx == NULL |
1374 | 0 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
1375 | 0 | goto err; |
1376 | 0 | } |
1377 | | |
1378 | 0 | if (srcctx->propq != NULL) { |
1379 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
1380 | 0 | if (dstctx->propq == NULL) |
1381 | 0 | goto err; |
1382 | 0 | } |
1383 | | |
1384 | 0 | if (srcctx->sig != NULL) { |
1385 | 0 | dstctx->sig = OPENSSL_memdup(srcctx->sig, srcctx->siglen); |
1386 | 0 | if (dstctx->sig == NULL) |
1387 | 0 | goto err; |
1388 | 0 | } |
1389 | | |
1390 | 0 | return dstctx; |
1391 | 0 | err: |
1392 | 0 | rsa_freectx(dstctx); |
1393 | 0 | return NULL; |
1394 | 0 | } |
1395 | | |
1396 | | static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params) |
1397 | 0 | { |
1398 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1399 | 0 | struct rsa_get_ctx_params_st p; |
1400 | |
|
1401 | 0 | if (prsactx == NULL || !rsa_get_ctx_params_decoder(params, &p)) |
1402 | 0 | return 0; |
1403 | | |
1404 | 0 | if (p.algid != NULL) { |
1405 | | /* The Algorithm Identifier of the combined signature algorithm */ |
1406 | 0 | unsigned char aid_buf[128]; |
1407 | 0 | unsigned char *aid; |
1408 | 0 | size_t aid_len; |
1409 | |
|
1410 | 0 | aid = rsa_generate_signature_aid(prsactx, aid_buf, |
1411 | 0 | sizeof(aid_buf), &aid_len); |
1412 | 0 | if (aid == NULL || !OSSL_PARAM_set_octet_string(p.algid, aid, aid_len)) |
1413 | 0 | return 0; |
1414 | 0 | } |
1415 | | |
1416 | 0 | if (p.pad != NULL) { |
1417 | 0 | if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) { |
1418 | 0 | if (!OSSL_PARAM_set_int(p.pad, prsactx->pad_mode)) |
1419 | 0 | return 0; |
1420 | 0 | } else { |
1421 | 0 | int i; |
1422 | 0 | const char *word = NULL; |
1423 | |
|
1424 | 0 | for (i = 0; padding_item[i].id != 0; i++) { |
1425 | 0 | if (prsactx->pad_mode == (int)padding_item[i].id) { |
1426 | 0 | word = padding_item[i].ptr; |
1427 | 0 | break; |
1428 | 0 | } |
1429 | 0 | } |
1430 | |
|
1431 | 0 | if (word != NULL) { |
1432 | 0 | if (!OSSL_PARAM_set_utf8_string(p.pad, word)) |
1433 | 0 | return 0; |
1434 | 0 | } else { |
1435 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
1436 | 0 | } |
1437 | 0 | } |
1438 | 0 | } |
1439 | | |
1440 | 0 | if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, prsactx->mdname)) |
1441 | 0 | return 0; |
1442 | | |
1443 | 0 | if (p.mgf1 != NULL && !OSSL_PARAM_set_utf8_string(p.mgf1, prsactx->mgf1_mdname)) |
1444 | 0 | return 0; |
1445 | | |
1446 | 0 | if (p.slen != NULL) { |
1447 | 0 | if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) { |
1448 | 0 | if (!OSSL_PARAM_set_int(p.slen, prsactx->saltlen)) |
1449 | 0 | return 0; |
1450 | 0 | } else { |
1451 | 0 | const char *value = NULL; |
1452 | |
|
1453 | 0 | switch (prsactx->saltlen) { |
1454 | 0 | case RSA_PSS_SALTLEN_DIGEST: |
1455 | 0 | value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST; |
1456 | 0 | break; |
1457 | 0 | case RSA_PSS_SALTLEN_MAX: |
1458 | 0 | value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX; |
1459 | 0 | break; |
1460 | 0 | case RSA_PSS_SALTLEN_AUTO: |
1461 | 0 | value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO; |
1462 | 0 | break; |
1463 | 0 | case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX: |
1464 | 0 | value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX; |
1465 | 0 | break; |
1466 | 0 | default: { |
1467 | 0 | int len = BIO_snprintf(p.slen->data, p.slen->data_size, "%d", |
1468 | 0 | prsactx->saltlen); |
1469 | |
|
1470 | 0 | if (len <= 0) |
1471 | 0 | return 0; |
1472 | 0 | p.slen->return_size = len; |
1473 | 0 | break; |
1474 | 0 | } |
1475 | 0 | } |
1476 | 0 | if (value != NULL |
1477 | 0 | && !OSSL_PARAM_set_utf8_string(p.slen, value)) |
1478 | 0 | return 0; |
1479 | 0 | } |
1480 | 0 | } |
1481 | | |
1482 | | #ifdef FIPS_MODULE |
1483 | | if (p.verify != NULL && !OSSL_PARAM_set_uint(p.verify, prsactx->verify_message)) |
1484 | | return 0; |
1485 | | #endif |
1486 | | |
1487 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(prsactx, p.ind)) |
1488 | 0 | return 0; |
1489 | 0 | return 1; |
1490 | 0 | } |
1491 | | |
1492 | | static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx, |
1493 | | ossl_unused void *provctx) |
1494 | 0 | { |
1495 | 0 | return rsa_get_ctx_params_list; |
1496 | 0 | } |
1497 | | |
1498 | | #ifdef FIPS_MODULE |
1499 | | static int rsa_x931_padding_allowed(PROV_RSA_CTX *ctx) |
1500 | | { |
1501 | | if ((ctx->operation |
1502 | | & (EVP_PKEY_OP_SIGNMSG | EVP_PKEY_OP_SIGN)) |
1503 | | != 0) { |
1504 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2, |
1505 | | ctx->libctx, |
1506 | | "RSA Sign set ctx", "X931 Padding", |
1507 | | ossl_fips_config_rsa_sign_x931_disallowed)) { |
1508 | | ERR_raise(ERR_LIB_PROV, |
1509 | | PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); |
1510 | | return 0; |
1511 | | } |
1512 | | } |
1513 | | return 1; |
1514 | | } |
1515 | | #endif |
1516 | | |
1517 | | static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) |
1518 | 0 | { |
1519 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1520 | 0 | struct rsa_set_ctx_params_st p; |
1521 | 0 | int pad_mode; |
1522 | 0 | int saltlen; |
1523 | 0 | int count = 0; |
1524 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL; |
1525 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL; |
1526 | 0 | char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL; |
1527 | 0 | char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL; |
1528 | |
|
1529 | 0 | if (prsactx == NULL) |
1530 | 0 | return 0; |
1531 | | /* The processing code below doesn't handle no parameters properly */ |
1532 | 0 | if (ossl_param_is_empty(params)) |
1533 | 0 | return 1; |
1534 | | |
1535 | 0 | if (prsactx->flag_allow_md) { |
1536 | 0 | if (!rsa_set_ctx_params_decoder(params, &p, &count)) |
1537 | 0 | return 0; |
1538 | 0 | } else { |
1539 | 0 | if (!rsa_set_ctx_params_no_digest_decoder(params, &p, &count)) |
1540 | 0 | return 0; |
1541 | 0 | } |
1542 | 0 | if (count == 0) |
1543 | 0 | return 1; |
1544 | | |
1545 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, |
1546 | 0 | p.ind_k)) |
1547 | 0 | return 0; |
1548 | | |
1549 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1, |
1550 | 0 | p.ind_d)) |
1551 | 0 | return 0; |
1552 | | |
1553 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE2, |
1554 | 0 | p.ind_xpad)) |
1555 | 0 | return 0; |
1556 | | |
1557 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE3, |
1558 | 0 | p.ind_slen)) |
1559 | 0 | return 0; |
1560 | | |
1561 | 0 | pad_mode = prsactx->pad_mode; |
1562 | 0 | saltlen = prsactx->saltlen; |
1563 | |
|
1564 | 0 | if (p.digest != NULL) { |
1565 | 0 | pmdname = mdname; |
1566 | 0 | if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname))) |
1567 | 0 | return 0; |
1568 | | |
1569 | 0 | if (p.propq != NULL) { |
1570 | 0 | pmdprops = mdprops; |
1571 | 0 | if (!OSSL_PARAM_get_utf8_string(p.propq, |
1572 | 0 | &pmdprops, sizeof(mdprops))) |
1573 | 0 | return 0; |
1574 | 0 | } |
1575 | 0 | } |
1576 | | |
1577 | 0 | if (p.pad != NULL) { |
1578 | 0 | const char *err_extra_text = NULL; |
1579 | |
|
1580 | 0 | if (p.pad->data_type != OSSL_PARAM_UTF8_STRING) { |
1581 | | /* Support for legacy pad mode number */ |
1582 | 0 | if (!OSSL_PARAM_get_int(p.pad, &pad_mode)) |
1583 | 0 | return 0; |
1584 | 0 | } else { |
1585 | 0 | int i; |
1586 | |
|
1587 | 0 | if (p.pad->data == NULL) |
1588 | 0 | return 0; |
1589 | | |
1590 | 0 | for (i = 0; padding_item[i].id != 0; i++) { |
1591 | 0 | if (strcmp(p.pad->data, padding_item[i].ptr) == 0) { |
1592 | 0 | pad_mode = padding_item[i].id; |
1593 | 0 | break; |
1594 | 0 | } |
1595 | 0 | } |
1596 | 0 | } |
1597 | | |
1598 | 0 | switch (pad_mode) { |
1599 | 0 | case RSA_PKCS1_OAEP_PADDING: |
1600 | | /* |
1601 | | * OAEP padding is for asymmetric cipher only so is not compatible |
1602 | | * with signature use. |
1603 | | */ |
1604 | 0 | err_extra_text = "OAEP padding not allowed for signing / verifying"; |
1605 | 0 | goto bad_pad; |
1606 | 0 | case RSA_PKCS1_PSS_PADDING: |
1607 | 0 | if ((prsactx->operation |
1608 | 0 | & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG |
1609 | 0 | | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG)) |
1610 | 0 | == 0) { |
1611 | 0 | err_extra_text = "PSS padding only allowed for sign and verify operations"; |
1612 | 0 | goto bad_pad; |
1613 | 0 | } |
1614 | 0 | break; |
1615 | 0 | case RSA_PKCS1_PADDING: |
1616 | 0 | err_extra_text = "PKCS#1 padding not allowed with RSA-PSS"; |
1617 | 0 | goto cont; |
1618 | 0 | case RSA_NO_PADDING: |
1619 | 0 | err_extra_text = "No padding not allowed with RSA-PSS"; |
1620 | 0 | goto cont; |
1621 | 0 | case RSA_X931_PADDING: |
1622 | | #ifdef FIPS_MODULE |
1623 | | /* X9.31 only allows sizes of 1024 + 256 * s (bits) */ |
1624 | | if ((RSA_bits(prsactx->rsa) & 0xFF) != 0) { |
1625 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
1626 | | return 0; |
1627 | | } |
1628 | | /* RSA Signing with X9.31 padding is not allowed in FIPS 140-3 */ |
1629 | | if (!rsa_x931_padding_allowed(prsactx)) |
1630 | | return 0; |
1631 | | #endif |
1632 | 0 | err_extra_text = "X.931 padding not allowed with RSA-PSS"; |
1633 | 0 | cont: |
1634 | 0 | if (RSA_test_flags(prsactx->rsa, |
1635 | 0 | RSA_FLAG_TYPE_MASK) |
1636 | 0 | == RSA_FLAG_TYPE_RSA) |
1637 | 0 | break; |
1638 | | /* FALLTHRU */ |
1639 | 0 | default: |
1640 | 0 | bad_pad: |
1641 | 0 | if (err_extra_text == NULL) |
1642 | 0 | ERR_raise(ERR_LIB_PROV, |
1643 | 0 | PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); |
1644 | 0 | else |
1645 | 0 | ERR_raise_data(ERR_LIB_PROV, |
1646 | 0 | PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE, |
1647 | 0 | err_extra_text); |
1648 | 0 | return 0; |
1649 | 0 | } |
1650 | 0 | } |
1651 | | |
1652 | 0 | if (p.slen != NULL) { |
1653 | 0 | if (pad_mode != RSA_PKCS1_PSS_PADDING) { |
1654 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED, |
1655 | 0 | "PSS saltlen can only be specified if " |
1656 | 0 | "PSS padding has been specified first"); |
1657 | 0 | return 0; |
1658 | 0 | } |
1659 | | |
1660 | 0 | if (p.slen->data_type != OSSL_PARAM_UTF8_STRING) { |
1661 | | /* Support for legacy pad mode number */ |
1662 | 0 | if (!OSSL_PARAM_get_int(p.slen, &saltlen)) |
1663 | 0 | return 0; |
1664 | 0 | } else { |
1665 | 0 | if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0) |
1666 | 0 | saltlen = RSA_PSS_SALTLEN_DIGEST; |
1667 | 0 | else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0) |
1668 | 0 | saltlen = RSA_PSS_SALTLEN_MAX; |
1669 | 0 | else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0) |
1670 | 0 | saltlen = RSA_PSS_SALTLEN_AUTO; |
1671 | 0 | else if (strcmp(p.slen->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX) == 0) |
1672 | 0 | saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX; |
1673 | 0 | else |
1674 | 0 | saltlen = atoi(p.slen->data); |
1675 | 0 | } |
1676 | | |
1677 | | /* |
1678 | | * RSA_PSS_SALTLEN_AUTO_DIGEST_MAX seems curiously named in this check. |
1679 | | * Contrary to what it's name suggests, it's the currently lowest |
1680 | | * saltlen number possible. |
1681 | | */ |
1682 | 0 | if (saltlen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) { |
1683 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); |
1684 | 0 | return 0; |
1685 | 0 | } |
1686 | | |
1687 | 0 | if (rsa_pss_restricted(prsactx)) { |
1688 | 0 | switch (saltlen) { |
1689 | 0 | case RSA_PSS_SALTLEN_AUTO: |
1690 | 0 | case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX: |
1691 | 0 | if ((prsactx->operation |
1692 | 0 | & (EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG)) |
1693 | 0 | == 0) { |
1694 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, |
1695 | 0 | "Cannot use autodetected salt length"); |
1696 | 0 | return 0; |
1697 | 0 | } |
1698 | 0 | break; |
1699 | 0 | case RSA_PSS_SALTLEN_DIGEST: |
1700 | 0 | if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) { |
1701 | 0 | ERR_raise_data(ERR_LIB_PROV, |
1702 | 0 | PROV_R_PSS_SALTLEN_TOO_SMALL, |
1703 | 0 | "Should be more than %d, but would be " |
1704 | 0 | "set to match digest size (%d)", |
1705 | 0 | prsactx->min_saltlen, |
1706 | 0 | EVP_MD_get_size(prsactx->md)); |
1707 | 0 | return 0; |
1708 | 0 | } |
1709 | 0 | break; |
1710 | 0 | default: |
1711 | 0 | if (saltlen >= 0 && saltlen < prsactx->min_saltlen) { |
1712 | 0 | ERR_raise_data(ERR_LIB_PROV, |
1713 | 0 | PROV_R_PSS_SALTLEN_TOO_SMALL, |
1714 | 0 | "Should be more than %d, " |
1715 | 0 | "but would be set to %d", |
1716 | 0 | prsactx->min_saltlen, saltlen); |
1717 | 0 | return 0; |
1718 | 0 | } |
1719 | 0 | } |
1720 | 0 | } |
1721 | 0 | } |
1722 | | |
1723 | 0 | if (p.mgf1 != NULL) { |
1724 | 0 | pmgf1mdname = mgf1mdname; |
1725 | 0 | if (!OSSL_PARAM_get_utf8_string(p.mgf1, &pmgf1mdname, sizeof(mgf1mdname))) |
1726 | 0 | return 0; |
1727 | | |
1728 | 0 | if (p.mgf1pq != NULL) { |
1729 | 0 | pmgf1mdprops = mgf1mdprops; |
1730 | 0 | if (!OSSL_PARAM_get_utf8_string(p.mgf1pq, |
1731 | 0 | &pmgf1mdprops, sizeof(mgf1mdprops))) |
1732 | 0 | return 0; |
1733 | 0 | } |
1734 | | |
1735 | 0 | if (pad_mode != RSA_PKCS1_PSS_PADDING) { |
1736 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD); |
1737 | 0 | return 0; |
1738 | 0 | } |
1739 | 0 | } |
1740 | | |
1741 | 0 | prsactx->saltlen = saltlen; |
1742 | 0 | prsactx->pad_mode = pad_mode; |
1743 | |
|
1744 | 0 | if (prsactx->md == NULL && pmdname == NULL |
1745 | 0 | && pad_mode == RSA_PKCS1_PSS_PADDING) |
1746 | 0 | pmdname = RSA_DEFAULT_DIGEST_NAME; |
1747 | |
|
1748 | 0 | if (pmgf1mdname != NULL |
1749 | 0 | && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops)) |
1750 | 0 | return 0; |
1751 | | |
1752 | 0 | if (pmdname != NULL) { |
1753 | 0 | if (!rsa_setup_md(prsactx, pmdname, pmdprops, "RSA Sign Set Ctx")) |
1754 | 0 | return 0; |
1755 | 0 | } else { |
1756 | 0 | if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid)) |
1757 | 0 | return 0; |
1758 | 0 | } |
1759 | 0 | return 1; |
1760 | 0 | } |
1761 | | |
1762 | | static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx, |
1763 | | ossl_unused void *provctx) |
1764 | 0 | { |
1765 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1766 | |
|
1767 | 0 | if (prsactx != NULL && !prsactx->flag_allow_md) |
1768 | 0 | return rsa_set_ctx_params_no_digest_list; |
1769 | 0 | return rsa_set_ctx_params_list; |
1770 | 0 | } |
1771 | | |
1772 | | static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params) |
1773 | 0 | { |
1774 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1775 | |
|
1776 | 0 | if (prsactx->mdctx == NULL) |
1777 | 0 | return 0; |
1778 | | |
1779 | 0 | return EVP_MD_CTX_get_params(prsactx->mdctx, params); |
1780 | 0 | } |
1781 | | |
1782 | | static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx) |
1783 | 0 | { |
1784 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1785 | |
|
1786 | 0 | if (prsactx->md == NULL) |
1787 | 0 | return 0; |
1788 | | |
1789 | 0 | return EVP_MD_gettable_ctx_params(prsactx->md); |
1790 | 0 | } |
1791 | | |
1792 | | static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[]) |
1793 | 0 | { |
1794 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1795 | |
|
1796 | 0 | if (prsactx->mdctx == NULL) |
1797 | 0 | return 0; |
1798 | | |
1799 | 0 | return EVP_MD_CTX_set_params(prsactx->mdctx, params); |
1800 | 0 | } |
1801 | | |
1802 | | static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx) |
1803 | 0 | { |
1804 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1805 | |
|
1806 | 0 | if (prsactx->md == NULL) |
1807 | 0 | return 0; |
1808 | | |
1809 | 0 | return EVP_MD_settable_ctx_params(prsactx->md); |
1810 | 0 | } |
1811 | | |
1812 | | const OSSL_DISPATCH ossl_rsa_signature_functions[] = { |
1813 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx }, |
1814 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init }, |
1815 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign }, |
1816 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init }, |
1817 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify }, |
1818 | | { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT, |
1819 | | (void (*)(void))rsa_verify_recover_init }, |
1820 | | { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER, |
1821 | | (void (*)(void))rsa_verify_recover }, |
1822 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
1823 | | (void (*)(void))rsa_digest_sign_init }, |
1824 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
1825 | | (void (*)(void))rsa_digest_sign_update }, |
1826 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
1827 | | (void (*)(void))rsa_digest_sign_final }, |
1828 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
1829 | | (void (*)(void))rsa_digest_verify_init }, |
1830 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
1831 | | (void (*)(void))rsa_digest_verify_update }, |
1832 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
1833 | | (void (*)(void))rsa_digest_verify_final }, |
1834 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx }, |
1835 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx }, |
1836 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params }, |
1837 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
1838 | | (void (*)(void))rsa_gettable_ctx_params }, |
1839 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params }, |
1840 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
1841 | | (void (*)(void))rsa_settable_ctx_params }, |
1842 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
1843 | | (void (*)(void))rsa_get_ctx_md_params }, |
1844 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
1845 | | (void (*)(void))rsa_gettable_ctx_md_params }, |
1846 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
1847 | | (void (*)(void))rsa_set_ctx_md_params }, |
1848 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
1849 | | (void (*)(void))rsa_settable_ctx_md_params }, |
1850 | | OSSL_DISPATCH_END |
1851 | | }; |
1852 | | |
1853 | | /* ------------------------------------------------------------------ */ |
1854 | | |
1855 | | /* |
1856 | | * So called sigalgs (composite RSA+hash) implemented below. They |
1857 | | * are pretty much hard coded, and rely on the hash implementation |
1858 | | * being available as per what OPENSSL_NO_ macros allow. |
1859 | | */ |
1860 | | |
1861 | | static OSSL_FUNC_signature_query_key_types_fn rsa_sigalg_query_key_types; |
1862 | | static OSSL_FUNC_signature_settable_ctx_params_fn rsa_sigalg_settable_ctx_params; |
1863 | | static OSSL_FUNC_signature_set_ctx_params_fn rsa_sigalg_set_ctx_params; |
1864 | | |
1865 | | /* |
1866 | | * rsa_sigalg_signverify_init() is almost like rsa_digest_signverify_init(), |
1867 | | * just doesn't allow fetching an MD from whatever the user chooses. |
1868 | | */ |
1869 | | static int rsa_sigalg_signverify_init(void *vprsactx, void *vrsa, |
1870 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
1871 | | const OSSL_PARAM params[], |
1872 | | const char *mdname, |
1873 | | int operation, int pad_mode, |
1874 | | const char *desc) |
1875 | 0 | { |
1876 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1877 | |
|
1878 | 0 | if (!ossl_prov_is_running()) |
1879 | 0 | return 0; |
1880 | | |
1881 | 0 | if (!rsa_signverify_init(prsactx, vrsa, set_ctx_params, params, operation, |
1882 | 0 | desc)) |
1883 | 0 | return 0; |
1884 | | |
1885 | | /* PSS is currently not supported as a sigalg */ |
1886 | 0 | if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
1887 | 0 | ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
1888 | 0 | return 0; |
1889 | 0 | } |
1890 | | |
1891 | 0 | if (!rsa_setup_md(prsactx, mdname, NULL, desc)) |
1892 | 0 | return 0; |
1893 | | |
1894 | 0 | prsactx->pad_mode = pad_mode; |
1895 | 0 | prsactx->flag_sigalg = 1; |
1896 | 0 | prsactx->flag_allow_md = 0; |
1897 | |
|
1898 | 0 | if (prsactx->mdctx == NULL) { |
1899 | 0 | prsactx->mdctx = EVP_MD_CTX_new(); |
1900 | 0 | if (prsactx->mdctx == NULL) |
1901 | 0 | goto error; |
1902 | 0 | } |
1903 | | |
1904 | 0 | if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params)) |
1905 | 0 | goto error; |
1906 | | |
1907 | 0 | return 1; |
1908 | | |
1909 | 0 | error: |
1910 | 0 | EVP_MD_CTX_free(prsactx->mdctx); |
1911 | 0 | prsactx->mdctx = NULL; |
1912 | 0 | return 0; |
1913 | 0 | } |
1914 | | |
1915 | | static const char **rsa_sigalg_query_key_types(void) |
1916 | 0 | { |
1917 | 0 | static const char *keytypes[] = { "RSA", NULL }; |
1918 | |
|
1919 | 0 | return keytypes; |
1920 | 0 | } |
1921 | | |
1922 | | static const OSSL_PARAM *rsa_sigalg_settable_ctx_params(void *vprsactx, |
1923 | | ossl_unused void *provctx) |
1924 | 0 | { |
1925 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1926 | |
|
1927 | 0 | if (prsactx != NULL && prsactx->operation == EVP_PKEY_OP_VERIFYMSG) |
1928 | 0 | return rsa_sigalg_set_ctx_params_list; |
1929 | 0 | return NULL; |
1930 | 0 | } |
1931 | | |
1932 | | static int rsa_sigalg_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) |
1933 | 0 | { |
1934 | 0 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
1935 | 0 | struct rsa_sigalg_set_ctx_params_st p; |
1936 | |
|
1937 | 0 | if (prsactx == NULL || !rsa_sigalg_set_ctx_params_decoder(params, &p)) |
1938 | 0 | return 0; |
1939 | | |
1940 | 0 | if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) { |
1941 | 0 | if (p.sig != NULL) { |
1942 | 0 | OPENSSL_free(prsactx->sig); |
1943 | 0 | prsactx->sig = NULL; |
1944 | 0 | prsactx->siglen = 0; |
1945 | 0 | if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&prsactx->sig, |
1946 | 0 | 0, &prsactx->siglen)) |
1947 | 0 | return 0; |
1948 | 0 | } |
1949 | 0 | } |
1950 | 0 | return 1; |
1951 | 0 | } |
1952 | | |
1953 | | #define IMPL_RSA_SIGALG(md, MD) \ |
1954 | | static OSSL_FUNC_signature_sign_init_fn rsa_##md##_sign_init; \ |
1955 | | static OSSL_FUNC_signature_sign_message_init_fn \ |
1956 | | rsa_##md##_sign_message_init; \ |
1957 | | static OSSL_FUNC_signature_verify_init_fn rsa_##md##_verify_init; \ |
1958 | | static OSSL_FUNC_signature_verify_message_init_fn \ |
1959 | | rsa_##md##_verify_message_init; \ |
1960 | | \ |
1961 | | static int \ |
1962 | | rsa_##md##_sign_init(void *vprsactx, void *vrsa, \ |
1963 | | const OSSL_PARAM params[]) \ |
1964 | 0 | { \ |
1965 | 0 | static const char desc[] = "RSA Sigalg Sign Init"; \ |
1966 | 0 | \ |
1967 | 0 | return rsa_sigalg_signverify_init(vprsactx, vrsa, \ |
1968 | 0 | rsa_sigalg_set_ctx_params, \ |
1969 | 0 | params, MD, \ |
1970 | 0 | EVP_PKEY_OP_SIGN, \ |
1971 | 0 | RSA_PKCS1_PADDING, \ |
1972 | 0 | desc); \ |
1973 | 0 | } \ Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha1_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha224_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha256_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha384_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_sign_init Unexecuted instantiation: rsa_sig.c:rsa_sm3_sign_init |
1974 | | \ |
1975 | | static int \ |
1976 | | rsa_##md##_sign_message_init(void *vprsactx, void *vrsa, \ |
1977 | | const OSSL_PARAM params[]) \ |
1978 | 0 | { \ |
1979 | 0 | static const char desc[] = "RSA Sigalg Sign Message Init"; \ |
1980 | 0 | \ |
1981 | 0 | return rsa_sigalg_signverify_init(vprsactx, vrsa, \ |
1982 | 0 | rsa_sigalg_set_ctx_params, \ |
1983 | 0 | params, MD, \ |
1984 | 0 | EVP_PKEY_OP_SIGNMSG, \ |
1985 | 0 | RSA_PKCS1_PADDING, \ |
1986 | 0 | desc); \ |
1987 | 0 | } \ Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha1_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha224_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha256_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha384_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_sign_message_init Unexecuted instantiation: rsa_sig.c:rsa_sm3_sign_message_init |
1988 | | \ |
1989 | | static int \ |
1990 | | rsa_##md##_verify_init(void *vprsactx, void *vrsa, \ |
1991 | | const OSSL_PARAM params[]) \ |
1992 | 0 | { \ |
1993 | 0 | static const char desc[] = "RSA Sigalg Verify Init"; \ |
1994 | 0 | \ |
1995 | 0 | return rsa_sigalg_signverify_init(vprsactx, vrsa, \ |
1996 | 0 | rsa_sigalg_set_ctx_params, \ |
1997 | 0 | params, MD, \ |
1998 | 0 | EVP_PKEY_OP_VERIFY, \ |
1999 | 0 | RSA_PKCS1_PADDING, \ |
2000 | 0 | desc); \ |
2001 | 0 | } \ Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_init Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_init |
2002 | | \ |
2003 | | static int \ |
2004 | | rsa_##md##_verify_recover_init(void *vprsactx, void *vrsa, \ |
2005 | | const OSSL_PARAM params[]) \ |
2006 | 0 | { \ |
2007 | 0 | static const char desc[] = "RSA Sigalg Verify Recover Init"; \ |
2008 | 0 | \ |
2009 | 0 | return rsa_sigalg_signverify_init(vprsactx, vrsa, \ |
2010 | 0 | rsa_sigalg_set_ctx_params, \ |
2011 | 0 | params, MD, \ |
2012 | 0 | EVP_PKEY_OP_VERIFYRECOVER, \ |
2013 | 0 | RSA_PKCS1_PADDING, \ |
2014 | 0 | desc); \ |
2015 | 0 | } \ Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_recover_init Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_recover_init |
2016 | | \ |
2017 | | static int \ |
2018 | | rsa_##md##_verify_message_init(void *vprsactx, void *vrsa, \ |
2019 | | const OSSL_PARAM params[]) \ |
2020 | 0 | { \ |
2021 | 0 | static const char desc[] = "RSA Sigalg Verify Message Init"; \ |
2022 | 0 | \ |
2023 | 0 | return rsa_sigalg_signverify_init(vprsactx, vrsa, \ |
2024 | 0 | rsa_sigalg_set_ctx_params, \ |
2025 | 0 | params, MD, \ |
2026 | 0 | EVP_PKEY_OP_VERIFYMSG, \ |
2027 | 0 | RSA_PKCS1_PADDING, \ |
2028 | 0 | desc); \ |
2029 | 0 | } \ Unexecuted instantiation: rsa_sig.c:rsa_ripemd160_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha1_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha224_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha256_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha384_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_224_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha512_256_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_224_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_256_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_384_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sha3_512_verify_message_init Unexecuted instantiation: rsa_sig.c:rsa_sm3_verify_message_init |
2030 | | \ |
2031 | | const OSSL_DISPATCH ossl_rsa_##md##_signature_functions[] = { \ |
2032 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx }, \ |
2033 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, \ |
2034 | | (void (*)(void))rsa_##md##_sign_init }, \ |
2035 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign }, \ |
2036 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT, \ |
2037 | | (void (*)(void))rsa_##md##_sign_message_init }, \ |
2038 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE, \ |
2039 | | (void (*)(void))rsa_signverify_message_update }, \ |
2040 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL, \ |
2041 | | (void (*)(void))rsa_sign_message_final }, \ |
2042 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \ |
2043 | | (void (*)(void))rsa_##md##_verify_init }, \ |
2044 | | { OSSL_FUNC_SIGNATURE_VERIFY, \ |
2045 | | (void (*)(void))rsa_verify }, \ |
2046 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT, \ |
2047 | | (void (*)(void))rsa_##md##_verify_message_init }, \ |
2048 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE, \ |
2049 | | (void (*)(void))rsa_signverify_message_update }, \ |
2050 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL, \ |
2051 | | (void (*)(void))rsa_verify_message_final }, \ |
2052 | | { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT, \ |
2053 | | (void (*)(void))rsa_##md##_verify_recover_init }, \ |
2054 | | { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER, \ |
2055 | | (void (*)(void))rsa_verify_recover }, \ |
2056 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx }, \ |
2057 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx }, \ |
2058 | | { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES, \ |
2059 | | (void (*)(void))rsa_sigalg_query_key_types }, \ |
2060 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \ |
2061 | | (void (*)(void))rsa_get_ctx_params }, \ |
2062 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \ |
2063 | | (void (*)(void))rsa_gettable_ctx_params }, \ |
2064 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \ |
2065 | | (void (*)(void))rsa_sigalg_set_ctx_params }, \ |
2066 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \ |
2067 | | (void (*)(void))rsa_sigalg_settable_ctx_params }, \ |
2068 | | OSSL_DISPATCH_END \ |
2069 | | } |
2070 | | |
2071 | | #if !defined(OPENSSL_NO_RMD160) && !defined(FIPS_MODULE) |
2072 | | IMPL_RSA_SIGALG(ripemd160, "RIPEMD160"); |
2073 | | #endif |
2074 | | IMPL_RSA_SIGALG(sha1, "SHA1"); |
2075 | | IMPL_RSA_SIGALG(sha224, "SHA2-224"); |
2076 | | IMPL_RSA_SIGALG(sha256, "SHA2-256"); |
2077 | | IMPL_RSA_SIGALG(sha384, "SHA2-384"); |
2078 | | IMPL_RSA_SIGALG(sha512, "SHA2-512"); |
2079 | | IMPL_RSA_SIGALG(sha512_224, "SHA2-512/224"); |
2080 | | IMPL_RSA_SIGALG(sha512_256, "SHA2-512/256"); |
2081 | | IMPL_RSA_SIGALG(sha3_224, "SHA3-224"); |
2082 | | IMPL_RSA_SIGALG(sha3_256, "SHA3-256"); |
2083 | | IMPL_RSA_SIGALG(sha3_384, "SHA3-384"); |
2084 | | IMPL_RSA_SIGALG(sha3_512, "SHA3-512"); |
2085 | | #if !defined(OPENSSL_NO_SM3) && !defined(FIPS_MODULE) |
2086 | | IMPL_RSA_SIGALG(sm3, "SM3"); |
2087 | | #endif |