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