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