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