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