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