]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
da1c088f | 2 | * Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved. |
448be743 | 3 | * |
2a7b6f39 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 RS |
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 | |
448be743 DSH |
8 | */ |
9 | ||
c5f87134 P |
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 | ||
448be743 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
448be743 DSH |
18 | #include <openssl/asn1t.h> |
19 | #include <openssl/x509.h> | |
1e26a8ba | 20 | #include <openssl/bn.h> |
29be6023 | 21 | #include <openssl/core_names.h> |
96ebe52e | 22 | #include <openssl/param_build.h> |
25f2138b DMSP |
23 | #include "crypto/asn1.h" |
24 | #include "crypto/evp.h" | |
29be6023 | 25 | #include "crypto/rsa.h" |
706457b7 | 26 | #include "rsa_local.h" |
448be743 | 27 | |
42009ae8 DSH |
28 | /* Set any parameters associated with pkey */ |
29 | static int rsa_param_encode(const EVP_PKEY *pkey, | |
30 | ASN1_STRING **pstr, int *pstrtype) | |
31 | { | |
32 | const RSA *rsa = pkey->pkey.rsa; | |
52ad523c | 33 | |
42009ae8 DSH |
34 | *pstr = NULL; |
35 | /* If RSA it's just NULL type */ | |
484d1a73 | 36 | if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) { |
42009ae8 DSH |
37 | *pstrtype = V_ASN1_NULL; |
38 | return 1; | |
39 | } | |
40 | /* If no PSS parameters we omit parameters entirely */ | |
41 | if (rsa->pss == NULL) { | |
42 | *pstrtype = V_ASN1_UNDEF; | |
43 | return 1; | |
44 | } | |
45 | /* Encode PSS parameters */ | |
f291138b | 46 | if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL) |
42009ae8 | 47 | return 0; |
42009ae8 DSH |
48 | |
49 | *pstrtype = V_ASN1_SEQUENCE; | |
50 | return 1; | |
51 | } | |
52 | /* Decode any parameters and set them in RSA structure */ | |
6f81892e | 53 | static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
0f113f3e MC |
54 | { |
55 | unsigned char *penc = NULL; | |
56 | int penclen; | |
42009ae8 DSH |
57 | ASN1_STRING *str; |
58 | int strtype; | |
52ad523c | 59 | |
42009ae8 DSH |
60 | if (!rsa_param_encode(pkey, &str, &strtype)) |
61 | return 0; | |
0f113f3e | 62 | penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc); |
285eb168 BE |
63 | if (penclen <= 0) { |
64 | ASN1_STRING_free(str); | |
0f113f3e | 65 | return 0; |
285eb168 | 66 | } |
42009ae8 DSH |
67 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id), |
68 | strtype, str, penc, penclen)) | |
0f113f3e MC |
69 | return 1; |
70 | ||
71 | OPENSSL_free(penc); | |
285eb168 | 72 | ASN1_STRING_free(str); |
0f113f3e MC |
73 | return 0; |
74 | } | |
448be743 | 75 | |
7674e923 | 76 | static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) |
0f113f3e MC |
77 | { |
78 | const unsigned char *p; | |
79 | int pklen; | |
42009ae8 | 80 | X509_ALGOR *alg; |
0f113f3e | 81 | RSA *rsa = NULL; |
75ebbd9a | 82 | |
42009ae8 | 83 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey)) |
0f113f3e | 84 | return 0; |
29844ea5 | 85 | if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) |
0f113f3e | 86 | return 0; |
cf333799 | 87 | if (!ossl_rsa_param_decode(rsa, alg)) { |
42009ae8 DSH |
88 | RSA_free(rsa); |
89 | return 0; | |
90 | } | |
a6495479 RL |
91 | |
92 | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); | |
93 | switch (pkey->ameth->pkey_id) { | |
94 | case EVP_PKEY_RSA: | |
95 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA); | |
96 | break; | |
97 | case EVP_PKEY_RSA_PSS: | |
98 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS); | |
99 | break; | |
100 | default: | |
101 | /* Leave the type bits zero */ | |
102 | break; | |
103 | } | |
104 | ||
1f483a69 BE |
105 | if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) { |
106 | RSA_free(rsa); | |
107 | return 0; | |
108 | } | |
0f113f3e MC |
109 | return 1; |
110 | } | |
448be743 | 111 | |
6f81892e | 112 | static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e | 113 | { |
36871717 NA |
114 | /* |
115 | * Don't check the public/private key, this is mostly for smart | |
116 | * cards. | |
117 | */ | |
118 | if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) | |
119 | || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) { | |
120 | return 1; | |
121 | } | |
122 | ||
0f113f3e MC |
123 | if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0 |
124 | || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0) | |
125 | return 0; | |
126 | return 1; | |
127 | } | |
6f81892e | 128 | |
e4263314 | 129 | static int old_rsa_priv_decode(EVP_PKEY *pkey, |
0f113f3e MC |
130 | const unsigned char **pder, int derlen) |
131 | { | |
132 | RSA *rsa; | |
75ebbd9a | 133 | |
29844ea5 | 134 | if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) |
0f113f3e | 135 | return 0; |
faa02fe2 | 136 | EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa); |
0f113f3e MC |
137 | return 1; |
138 | } | |
448be743 | 139 | |
e4263314 | 140 | static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) |
0f113f3e MC |
141 | { |
142 | return i2d_RSAPrivateKey(pkey->pkey.rsa, pder); | |
143 | } | |
e4263314 | 144 | |
6f81892e | 145 | static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
0f113f3e MC |
146 | { |
147 | unsigned char *rk = NULL; | |
148 | int rklen; | |
42009ae8 DSH |
149 | ASN1_STRING *str; |
150 | int strtype; | |
52ad523c | 151 | |
42009ae8 DSH |
152 | if (!rsa_param_encode(pkey, &str, &strtype)) |
153 | return 0; | |
0f113f3e MC |
154 | rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk); |
155 | ||
156 | if (rklen <= 0) { | |
e077455e | 157 | ERR_raise(ERR_LIB_RSA, ERR_R_ASN1_LIB); |
285c7d9c | 158 | ASN1_STRING_free(str); |
0f113f3e MC |
159 | return 0; |
160 | } | |
161 | ||
faa02fe2 | 162 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0, |
42009ae8 | 163 | strtype, str, rk, rklen)) { |
e077455e | 164 | ERR_raise(ERR_LIB_RSA, ERR_R_ASN1_LIB); |
285c7d9c | 165 | ASN1_STRING_free(str); |
16249341 | 166 | OPENSSL_clear_free(rk, rklen); |
0f113f3e MC |
167 | return 0; |
168 | } | |
169 | ||
170 | return 1; | |
171 | } | |
448be743 | 172 | |
245c6bc3 | 173 | static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) |
0f113f3e | 174 | { |
cf333799 RL |
175 | int ret = 0; |
176 | RSA *rsa = ossl_rsa_key_from_pkcs8(p8, NULL, NULL); | |
42009ae8 | 177 | |
cf333799 RL |
178 | if (rsa != NULL) { |
179 | ret = 1; | |
180 | EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa); | |
42009ae8 | 181 | } |
cf333799 | 182 | return ret; |
0f113f3e | 183 | } |
e4263314 | 184 | |
6f81892e | 185 | static int int_rsa_size(const EVP_PKEY *pkey) |
0f113f3e MC |
186 | { |
187 | return RSA_size(pkey->pkey.rsa); | |
188 | } | |
6f81892e DSH |
189 | |
190 | static int rsa_bits(const EVP_PKEY *pkey) | |
0f113f3e MC |
191 | { |
192 | return BN_num_bits(pkey->pkey.rsa->n); | |
193 | } | |
6f81892e | 194 | |
2514fa79 | 195 | static int rsa_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
196 | { |
197 | return RSA_security_bits(pkey->pkey.rsa); | |
198 | } | |
2514fa79 | 199 | |
6f81892e | 200 | static void int_rsa_free(EVP_PKEY *pkey) |
0f113f3e MC |
201 | { |
202 | RSA_free(pkey->pkey.rsa); | |
203 | } | |
35208f36 | 204 | |
9503ed8b DSH |
205 | static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss, |
206 | int indent) | |
207 | { | |
208 | int rv = 0; | |
209 | X509_ALGOR *maskHash = NULL; | |
52ad523c | 210 | |
9503ed8b DSH |
211 | if (!BIO_indent(bp, indent, 128)) |
212 | goto err; | |
213 | if (pss_key) { | |
214 | if (pss == NULL) { | |
215 | if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0) | |
216 | return 0; | |
217 | return 1; | |
218 | } else { | |
219 | if (BIO_puts(bp, "PSS parameter restrictions:") <= 0) | |
220 | return 0; | |
221 | } | |
222 | } else if (pss == NULL) { | |
1287dabd | 223 | if (BIO_puts(bp, "(INVALID PSS PARAMETERS)\n") <= 0) |
9503ed8b DSH |
224 | return 0; |
225 | return 1; | |
226 | } | |
227 | if (BIO_puts(bp, "\n") <= 0) | |
228 | goto err; | |
229 | if (pss_key) | |
230 | indent += 2; | |
231 | if (!BIO_indent(bp, indent, 128)) | |
232 | goto err; | |
233 | if (BIO_puts(bp, "Hash Algorithm: ") <= 0) | |
234 | goto err; | |
235 | ||
236 | if (pss->hashAlgorithm) { | |
237 | if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) | |
238 | goto err; | |
90862ab4 | 239 | } else if (BIO_puts(bp, "sha1 (default)") <= 0) { |
9503ed8b | 240 | goto err; |
90862ab4 | 241 | } |
9503ed8b DSH |
242 | |
243 | if (BIO_puts(bp, "\n") <= 0) | |
244 | goto err; | |
245 | ||
246 | if (!BIO_indent(bp, indent, 128)) | |
247 | goto err; | |
248 | ||
249 | if (BIO_puts(bp, "Mask Algorithm: ") <= 0) | |
250 | goto err; | |
251 | if (pss->maskGenAlgorithm) { | |
252 | if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0) | |
253 | goto err; | |
254 | if (BIO_puts(bp, " with ") <= 0) | |
255 | goto err; | |
adf7e6d1 | 256 | maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm); |
9503ed8b DSH |
257 | if (maskHash != NULL) { |
258 | if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) | |
259 | goto err; | |
90862ab4 | 260 | } else if (BIO_puts(bp, "INVALID") <= 0) { |
9503ed8b | 261 | goto err; |
90862ab4 PY |
262 | } |
263 | } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) { | |
9503ed8b | 264 | goto err; |
90862ab4 | 265 | } |
9503ed8b DSH |
266 | BIO_puts(bp, "\n"); |
267 | ||
268 | if (!BIO_indent(bp, indent, 128)) | |
269 | goto err; | |
270 | if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0) | |
271 | goto err; | |
272 | if (pss->saltLength) { | |
273 | if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) | |
274 | goto err; | |
90862ab4 | 275 | } else if (BIO_puts(bp, "14 (default)") <= 0) { |
9503ed8b | 276 | goto err; |
90862ab4 | 277 | } |
9503ed8b DSH |
278 | BIO_puts(bp, "\n"); |
279 | ||
280 | if (!BIO_indent(bp, indent, 128)) | |
281 | goto err; | |
282 | if (BIO_puts(bp, "Trailer Field: 0x") <= 0) | |
283 | goto err; | |
284 | if (pss->trailerField) { | |
285 | if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) | |
286 | goto err; | |
814581bb | 287 | } else if (BIO_puts(bp, "01 (default)") <= 0) { |
9503ed8b | 288 | goto err; |
90862ab4 | 289 | } |
9503ed8b DSH |
290 | BIO_puts(bp, "\n"); |
291 | ||
292 | rv = 1; | |
293 | ||
294 | err: | |
295 | X509_ALGOR_free(maskHash); | |
296 | return rv; | |
297 | ||
298 | } | |
299 | ||
300 | static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv) | |
0f113f3e | 301 | { |
9503ed8b | 302 | const RSA *x = pkey->pkey.rsa; |
0f113f3e MC |
303 | char *str; |
304 | const char *s; | |
665d899f | 305 | int ret = 0, mod_len = 0, ex_primes; |
0f113f3e MC |
306 | |
307 | if (x->n != NULL) | |
308 | mod_len = BN_num_bits(x->n); | |
665d899f | 309 | ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos); |
0f113f3e MC |
310 | |
311 | if (!BIO_indent(bp, off, 128)) | |
312 | goto err; | |
313 | ||
87ee7b22 | 314 | if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0) |
9503ed8b DSH |
315 | goto err; |
316 | ||
0f113f3e | 317 | if (priv && x->d) { |
665d899f PY |
318 | if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n", |
319 | mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0) | |
0f113f3e MC |
320 | goto err; |
321 | str = "modulus:"; | |
322 | s = "publicExponent:"; | |
323 | } else { | |
a773b52a | 324 | if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) |
0f113f3e MC |
325 | goto err; |
326 | str = "Modulus:"; | |
327 | s = "Exponent:"; | |
328 | } | |
a773b52a | 329 | if (!ASN1_bn_print(bp, str, x->n, NULL, off)) |
0f113f3e | 330 | goto err; |
a773b52a | 331 | if (!ASN1_bn_print(bp, s, x->e, NULL, off)) |
0f113f3e MC |
332 | goto err; |
333 | if (priv) { | |
665d899f PY |
334 | int i; |
335 | ||
a773b52a | 336 | if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off)) |
0f113f3e | 337 | goto err; |
a773b52a | 338 | if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off)) |
0f113f3e | 339 | goto err; |
a773b52a | 340 | if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off)) |
0f113f3e | 341 | goto err; |
a773b52a | 342 | if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off)) |
0f113f3e | 343 | goto err; |
a773b52a | 344 | if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off)) |
0f113f3e | 345 | goto err; |
a773b52a | 346 | if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off)) |
0f113f3e | 347 | goto err; |
665d899f PY |
348 | for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) { |
349 | /* print multi-prime info */ | |
350 | BIGNUM *bn = NULL; | |
351 | RSA_PRIME_INFO *pinfo; | |
352 | int j; | |
353 | ||
354 | pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i); | |
355 | for (j = 0; j < 3; j++) { | |
356 | if (!BIO_indent(bp, off, 128)) | |
357 | goto err; | |
358 | switch (j) { | |
359 | case 0: | |
360 | if (BIO_printf(bp, "prime%d:", i + 3) <= 0) | |
361 | goto err; | |
362 | bn = pinfo->r; | |
363 | break; | |
364 | case 1: | |
365 | if (BIO_printf(bp, "exponent%d:", i + 3) <= 0) | |
366 | goto err; | |
367 | bn = pinfo->d; | |
368 | break; | |
369 | case 2: | |
370 | if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0) | |
371 | goto err; | |
372 | bn = pinfo->t; | |
373 | break; | |
374 | default: | |
375 | break; | |
376 | } | |
377 | if (!ASN1_bn_print(bp, "", bn, NULL, off)) | |
378 | goto err; | |
379 | } | |
380 | } | |
0f113f3e | 381 | } |
87ee7b22 | 382 | if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off)) |
9503ed8b | 383 | goto err; |
0f113f3e MC |
384 | ret = 1; |
385 | err: | |
9503ed8b | 386 | return ret; |
0f113f3e | 387 | } |
35208f36 DSH |
388 | |
389 | static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
390 | ASN1_PCTX *ctx) |
391 | { | |
9503ed8b | 392 | return pkey_rsa_print(bp, pkey, indent, 0); |
0f113f3e | 393 | } |
35208f36 DSH |
394 | |
395 | static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
396 | ASN1_PCTX *ctx) |
397 | { | |
9503ed8b | 398 | return pkey_rsa_print(bp, pkey, indent, 1); |
0f113f3e | 399 | } |
0574cadf | 400 | |
ff04bbe3 | 401 | static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, |
0f113f3e MC |
402 | const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) |
403 | { | |
ffc6fad5 | 404 | if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) { |
0f113f3e | 405 | int rv; |
cf333799 | 406 | RSA_PSS_PARAMS *pss = ossl_rsa_pss_decode(sigalg); |
c82bafc5 | 407 | |
9503ed8b | 408 | rv = rsa_pss_param_print(bp, 0, pss, indent); |
25aaa98a | 409 | RSA_PSS_PARAMS_free(pss); |
0f113f3e MC |
410 | if (!rv) |
411 | return 0; | |
a4c467c9 | 412 | } else if (BIO_puts(bp, "\n") <= 0) { |
0f113f3e | 413 | return 0; |
52ad523c | 414 | } |
0f113f3e MC |
415 | if (sig) |
416 | return X509_signature_dump(bp, sig, indent); | |
417 | return 1; | |
418 | } | |
492a9e24 DSH |
419 | |
420 | static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) | |
0f113f3e | 421 | { |
9bcc9f97 MC |
422 | const EVP_MD *md; |
423 | const EVP_MD *mgf1md; | |
424 | int min_saltlen; | |
52ad523c | 425 | |
0f113f3e | 426 | switch (op) { |
0f113f3e | 427 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
9bcc9f97 | 428 | if (pkey->pkey.rsa->pss != NULL) { |
4158b0dc SL |
429 | if (!ossl_rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md, |
430 | &min_saltlen)) { | |
9311d0c4 | 431 | ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR); |
9bcc9f97 MC |
432 | return 0; |
433 | } | |
ed576acd | 434 | *(int *)arg2 = EVP_MD_get_type(md); |
9bcc9f97 MC |
435 | /* Return of 2 indicates this MD is mandatory */ |
436 | return 2; | |
437 | } | |
0f113f3e MC |
438 | *(int *)arg2 = NID_sha256; |
439 | return 1; | |
03919683 | 440 | |
0f113f3e MC |
441 | default: |
442 | return -2; | |
0f113f3e | 443 | } |
0f113f3e | 444 | } |
492a9e24 | 445 | |
0f113f3e | 446 | /* |
47e42b3c | 447 | * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter, |
0574cadf | 448 | * suitable for setting an AlgorithmIdentifier. |
31904ecd DSH |
449 | */ |
450 | ||
47e42b3c | 451 | static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx) |
0f113f3e MC |
452 | { |
453 | const EVP_MD *sigmd, *mgf1md; | |
0f113f3e | 454 | EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx); |
47e42b3c | 455 | int saltlen; |
6c73ca4a | 456 | int saltlenMax = -1; |
52ad523c | 457 | |
0f113f3e | 458 | if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0) |
47e42b3c | 459 | return NULL; |
0f113f3e | 460 | if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0) |
47e42b3c | 461 | return NULL; |
7263a7fc | 462 | if (EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen) <= 0) |
47e42b3c | 463 | return NULL; |
6c73ca4a | 464 | if (saltlen == RSA_PSS_SALTLEN_DIGEST) { |
ed576acd | 465 | saltlen = EVP_MD_get_size(sigmd); |
6c73ca4a CL |
466 | } else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) { |
467 | /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", | |
468 | * subsection 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in | |
469 | * bytes) of the salt (sLen) shall satisfy 0 <= sLen <= hLen, where | |
470 | * hLen is the length of the hash function output block (in bytes)." | |
471 | * | |
472 | * Provide a way to use at most the digest length, so that the default | |
473 | * does not violate FIPS 186-4. */ | |
474 | saltlen = RSA_PSS_SALTLEN_MAX; | |
475 | saltlenMax = EVP_MD_get_size(sigmd); | |
476 | } | |
477 | if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) { | |
ed576acd TM |
478 | saltlen = EVP_PKEY_get_size(pk) - EVP_MD_get_size(sigmd) - 2; |
479 | if ((EVP_PKEY_get_bits(pk) & 0x7) == 1) | |
0f113f3e | 480 | saltlen--; |
491360e7 BE |
481 | if (saltlen < 0) |
482 | return NULL; | |
6c73ca4a CL |
483 | if (saltlenMax >= 0 && saltlen > saltlenMax) |
484 | saltlen = saltlenMax; | |
0f113f3e | 485 | } |
47e42b3c | 486 | |
4158b0dc | 487 | return ossl_rsa_pss_params_create(sigmd, mgf1md, saltlen); |
47e42b3c DSH |
488 | } |
489 | ||
4158b0dc SL |
490 | RSA_PSS_PARAMS *ossl_rsa_pss_params_create(const EVP_MD *sigmd, |
491 | const EVP_MD *mgf1md, int saltlen) | |
47e42b3c DSH |
492 | { |
493 | RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new(); | |
52ad523c | 494 | |
90945fa3 | 495 | if (pss == NULL) |
0f113f3e MC |
496 | goto err; |
497 | if (saltlen != 20) { | |
498 | pss->saltLength = ASN1_INTEGER_new(); | |
90945fa3 | 499 | if (pss->saltLength == NULL) |
0f113f3e MC |
500 | goto err; |
501 | if (!ASN1_INTEGER_set(pss->saltLength, saltlen)) | |
502 | goto err; | |
503 | } | |
adf7e6d1 | 504 | if (!ossl_x509_algor_new_from_md(&pss->hashAlgorithm, sigmd)) |
0f113f3e | 505 | goto err; |
47e42b3c | 506 | if (mgf1md == NULL) |
b6b885c6 | 507 | mgf1md = sigmd; |
adf7e6d1 | 508 | if (!ossl_x509_algor_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) |
0f113f3e | 509 | goto err; |
adf7e6d1 | 510 | if (!ossl_x509_algor_new_from_md(&pss->maskHash, mgf1md)) |
74753357 | 511 | goto err; |
47e42b3c | 512 | return pss; |
0f113f3e | 513 | err: |
25aaa98a | 514 | RSA_PSS_PARAMS_free(pss); |
0f113f3e MC |
515 | return NULL; |
516 | } | |
517 | ||
9ab7fe48 | 518 | ASN1_STRING *ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx) |
47e42b3c DSH |
519 | { |
520 | RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx); | |
f291138b | 521 | ASN1_STRING *os; |
52ad523c | 522 | |
47e42b3c DSH |
523 | if (pss == NULL) |
524 | return NULL; | |
525 | ||
f291138b | 526 | os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL); |
47e42b3c DSH |
527 | RSA_PSS_PARAMS_free(pss); |
528 | return os; | |
529 | } | |
530 | ||
0f113f3e MC |
531 | /* |
532 | * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL | |
0d4fb843 | 533 | * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are |
0f113f3e | 534 | * passed to pkctx instead. |
0574cadf | 535 | */ |
31904ecd | 536 | |
9ab7fe48 MC |
537 | int ossl_rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx, |
538 | const X509_ALGOR *sigalg, EVP_PKEY *pkey) | |
0f113f3e MC |
539 | { |
540 | int rv = -1; | |
541 | int saltlen; | |
542 | const EVP_MD *mgf1md = NULL, *md = NULL; | |
543 | RSA_PSS_PARAMS *pss; | |
52ad523c | 544 | |
0f113f3e | 545 | /* Sanity check: make sure it is PSS */ |
ffc6fad5 | 546 | if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) { |
9311d0c4 | 547 | ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); |
0f113f3e MC |
548 | return -1; |
549 | } | |
550 | /* Decode PSS parameters */ | |
cf333799 | 551 | pss = ossl_rsa_pss_decode(sigalg); |
0f113f3e | 552 | |
4158b0dc | 553 | if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) { |
9311d0c4 | 554 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS); |
0f113f3e MC |
555 | goto err; |
556 | } | |
0f113f3e MC |
557 | |
558 | /* We have all parameters now set up context */ | |
0f113f3e MC |
559 | if (pkey) { |
560 | if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey)) | |
561 | goto err; | |
562 | } else { | |
563 | const EVP_MD *checkmd; | |
564 | if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0) | |
565 | goto err; | |
ed576acd | 566 | if (EVP_MD_get_type(md) != EVP_MD_get_type(checkmd)) { |
9311d0c4 | 567 | ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_DOES_NOT_MATCH); |
0f113f3e MC |
568 | goto err; |
569 | } | |
570 | } | |
571 | ||
572 | if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0) | |
573 | goto err; | |
574 | ||
575 | if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0) | |
576 | goto err; | |
577 | ||
578 | if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) | |
579 | goto err; | |
580 | /* Carry on */ | |
581 | rv = 1; | |
582 | ||
583 | err: | |
584 | RSA_PSS_PARAMS_free(pss); | |
0f113f3e MC |
585 | return rv; |
586 | } | |
492a9e24 | 587 | |
a6495479 RL |
588 | static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md, |
589 | int *psaltlen, int *ptrailerField) | |
590 | { | |
591 | if (psaltlen != NULL && *psaltlen < 0) { | |
592 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH); | |
593 | return 0; | |
594 | } | |
595 | /* | |
596 | * low-level routines support only trailer field 0xbc (value 1) and | |
597 | * PKCS#1 says we should reject any other value anyway. | |
598 | */ | |
599 | if (ptrailerField != NULL && *ptrailerField != 1) { | |
600 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER); | |
601 | return 0; | |
602 | } | |
603 | return 1; | |
604 | } | |
605 | ||
4158b0dc SL |
606 | int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd, |
607 | const EVP_MD **pmgf1md, int *psaltlen) | |
a6495479 | 608 | { |
cfd81c6d | 609 | /* |
a6495479 RL |
610 | * Callers do not care about the trailer field, and yet, we must |
611 | * pass it from get_param to verify_param, since the latter checks | |
612 | * its value. | |
613 | * | |
614 | * When callers start caring, it's a simple thing to add another | |
615 | * argument to this function. | |
cfd81c6d | 616 | */ |
a6495479 | 617 | int trailerField = 0; |
cfd81c6d | 618 | |
cf333799 RL |
619 | return ossl_rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen, |
620 | &trailerField) | |
a6495479 RL |
621 | && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField); |
622 | } | |
623 | ||
0f113f3e MC |
624 | /* |
625 | * Customised RSA item verification routine. This is called when a signature | |
626 | * is encountered requiring special handling. We currently only handle PSS. | |
0574cadf DSH |
627 | */ |
628 | ||
ded346fa DDO |
629 | static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, |
630 | const void *asn, const X509_ALGOR *sigalg, | |
631 | const ASN1_BIT_STRING *sig, EVP_PKEY *pkey) | |
0f113f3e MC |
632 | { |
633 | /* Sanity check: make sure it is PSS */ | |
ffc6fad5 | 634 | if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) { |
9311d0c4 | 635 | ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); |
0f113f3e MC |
636 | return -1; |
637 | } | |
9ab7fe48 | 638 | if (ossl_rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) { |
0f113f3e MC |
639 | /* Carry on */ |
640 | return 2; | |
09f06923 | 641 | } |
0f113f3e MC |
642 | return -1; |
643 | } | |
0574cadf | 644 | |
ded346fa | 645 | static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *asn, |
0f113f3e MC |
646 | X509_ALGOR *alg1, X509_ALGOR *alg2, |
647 | ASN1_BIT_STRING *sig) | |
648 | { | |
649 | int pad_mode; | |
ed576acd | 650 | EVP_PKEY_CTX *pkctx = EVP_MD_CTX_get_pkey_ctx(ctx); |
52ad523c | 651 | |
0f113f3e MC |
652 | if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) |
653 | return 0; | |
654 | if (pad_mode == RSA_PKCS1_PADDING) | |
655 | return 2; | |
656 | if (pad_mode == RSA_PKCS1_PSS_PADDING) { | |
5a3bbe17 CL |
657 | unsigned char aid[128]; |
658 | size_t aid_len = 0; | |
659 | OSSL_PARAM params[2]; | |
04bc3c12 | 660 | |
3410a72d TM |
661 | if (evp_pkey_ctx_is_legacy(pkctx)) { |
662 | /* No provider -> we cannot query it for algorithm ID. */ | |
663 | ASN1_STRING *os1 = NULL; | |
664 | ||
665 | os1 = ossl_rsa_ctx_to_pss_string(pkctx); | |
666 | if (os1 == NULL) | |
667 | return 0; | |
668 | /* Duplicate parameters if we have to */ | |
669 | if (alg2 != NULL) { | |
670 | ASN1_STRING *os2 = ASN1_STRING_dup(os1); | |
671 | ||
672 | if (os2 == NULL) { | |
673 | ASN1_STRING_free(os1); | |
674 | return 0; | |
675 | } | |
676 | if (!X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS), | |
677 | V_ASN1_SEQUENCE, os2)) { | |
678 | ASN1_STRING_free(os1); | |
679 | ASN1_STRING_free(os2); | |
680 | return 0; | |
681 | } | |
682 | } | |
683 | if (!X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS), | |
684 | V_ASN1_SEQUENCE, os1)) { | |
685 | ASN1_STRING_free(os1); | |
686 | return 0; | |
687 | } | |
688 | return 3; | |
689 | } | |
690 | ||
5a3bbe17 CL |
691 | params[0] = OSSL_PARAM_construct_octet_string( |
692 | OSSL_SIGNATURE_PARAM_ALGORITHM_ID, aid, sizeof(aid)); | |
693 | params[1] = OSSL_PARAM_construct_end(); | |
694 | ||
695 | if (EVP_PKEY_CTX_get_params(pkctx, params) <= 0) | |
696 | return 0; | |
697 | if ((aid_len = params[0].return_size) == 0) | |
0f113f3e | 698 | return 0; |
04bc3c12 | 699 | |
5a3bbe17 CL |
700 | if (alg1 != NULL) { |
701 | const unsigned char *pp = aid; | |
3410a72d | 702 | |
5a3bbe17 CL |
703 | if (d2i_X509_ALGOR(&alg1, &pp, aid_len) == NULL) |
704 | return 0; | |
0f113f3e | 705 | } |
5a3bbe17 CL |
706 | if (alg2 != NULL) { |
707 | const unsigned char *pp = aid; | |
3410a72d | 708 | |
5a3bbe17 CL |
709 | if (d2i_X509_ALGOR(&alg2, &pp, aid_len) == NULL) |
710 | return 0; | |
711 | } | |
712 | ||
0f113f3e MC |
713 | return 3; |
714 | } | |
715 | return 2; | |
716 | } | |
17c63d1c | 717 | |
629e369c DSH |
718 | static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg, |
719 | const ASN1_STRING *sig) | |
720 | { | |
721 | int rv = 0; | |
722 | int mdnid, saltlen; | |
723 | uint32_t flags; | |
724 | const EVP_MD *mgf1md = NULL, *md = NULL; | |
725 | RSA_PSS_PARAMS *pss; | |
b744f915 | 726 | int secbits; |
629e369c DSH |
727 | |
728 | /* Sanity check: make sure it is PSS */ | |
729 | if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) | |
730 | return 0; | |
731 | /* Decode PSS parameters */ | |
cf333799 | 732 | pss = ossl_rsa_pss_decode(sigalg); |
4158b0dc | 733 | if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) |
629e369c | 734 | goto err; |
ed576acd | 735 | mdnid = EVP_MD_get_type(md); |
629e369c DSH |
736 | /* |
737 | * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must | |
738 | * match and salt length must equal digest size | |
739 | */ | |
740 | if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512) | |
ed576acd TM |
741 | && mdnid == EVP_MD_get_type(mgf1md) |
742 | && saltlen == EVP_MD_get_size(md)) | |
629e369c DSH |
743 | flags = X509_SIG_INFO_TLS; |
744 | else | |
745 | flags = 0; | |
746 | /* Note: security bits half number of digest bits */ | |
ed576acd | 747 | secbits = EVP_MD_get_size(md) * 4; |
b744f915 KR |
748 | /* |
749 | * SHA1 and MD5 are known to be broken. Reduce security bits so that | |
750 | * they're no longer accepted at security level 1. The real values don't | |
751 | * really matter as long as they're lower than 80, which is our security | |
752 | * level 1. | |
753 | * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at | |
754 | * 2^63.4 | |
755 | * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf | |
756 | * puts a chosen-prefix attack for MD5 at 2^39. | |
757 | */ | |
758 | if (mdnid == NID_sha1) | |
759 | secbits = 64; | |
760 | else if (mdnid == NID_md5_sha1) | |
761 | secbits = 68; | |
762 | else if (mdnid == NID_md5) | |
763 | secbits = 39; | |
764 | X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits, | |
629e369c DSH |
765 | flags); |
766 | rv = 1; | |
767 | err: | |
768 | RSA_PSS_PARAMS_free(pss); | |
769 | return rv; | |
770 | } | |
771 | ||
2aee35d3 PY |
772 | static int rsa_pkey_check(const EVP_PKEY *pkey) |
773 | { | |
774 | return RSA_check_key_ex(pkey->pkey.rsa, NULL); | |
775 | } | |
776 | ||
29be6023 RL |
777 | static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey) |
778 | { | |
779 | return pkey->pkey.rsa->dirty_cnt; | |
780 | } | |
781 | ||
967cc3f9 | 782 | /* |
0fc39c90 SL |
783 | * There is no need to do RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS) |
784 | * checks in this method since the caller tests EVP_KEYMGMT_is_a() first. | |
967cc3f9 RL |
785 | */ |
786 | static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type, | |
bed7437b RL |
787 | void *to_keydata, |
788 | OSSL_FUNC_keymgmt_import_fn *importer, | |
b4250010 | 789 | OSSL_LIB_CTX *libctx, const char *propq) |
29be6023 | 790 | { |
b305452f | 791 | RSA *rsa = from->pkey.rsa; |
6d4e6009 | 792 | OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new(); |
29be6023 | 793 | OSSL_PARAM *params = NULL; |
0996cff9 | 794 | int selection = 0; |
b305452f | 795 | int rv = 0; |
29be6023 | 796 | |
6d4e6009 P |
797 | if (tmpl == NULL) |
798 | return 0; | |
b305452f | 799 | /* Public parameters must always be present */ |
645a541a | 800 | if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL) |
29be6023 RL |
801 | goto err; |
802 | ||
944f822a | 803 | if (!ossl_rsa_todata(rsa, tmpl, NULL, 1)) |
29be6023 | 804 | goto err; |
b305452f | 805 | |
645a541a RL |
806 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
807 | if (RSA_get0_d(rsa) != NULL) | |
0996cff9 | 808 | selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
29be6023 | 809 | |
967cc3f9 RL |
810 | if (rsa->pss != NULL) { |
811 | const EVP_MD *md = NULL, *mgf1md = NULL; | |
6ce6ad39 | 812 | int md_nid, mgf1md_nid, saltlen, trailerfield; |
967cc3f9 RL |
813 | RSA_PSS_PARAMS_30 pss_params; |
814 | ||
cf333799 RL |
815 | if (!ossl_rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md, |
816 | &saltlen, &trailerfield)) | |
967cc3f9 | 817 | goto err; |
ed576acd TM |
818 | md_nid = EVP_MD_get_type(md); |
819 | mgf1md_nid = EVP_MD_get_type(mgf1md); | |
23b2fc0b P |
820 | if (!ossl_rsa_pss_params_30_set_defaults(&pss_params) |
821 | || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid) | |
822 | || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params, | |
823 | mgf1md_nid) | |
824 | || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen) | |
825 | || !ossl_rsa_pss_params_30_todata(&pss_params, tmpl, NULL)) | |
967cc3f9 RL |
826 | goto err; |
827 | selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS; | |
29be6023 RL |
828 | } |
829 | ||
6d4e6009 | 830 | if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) |
29be6023 RL |
831 | goto err; |
832 | ||
833 | /* We export, the provider imports */ | |
bed7437b | 834 | rv = importer(to_keydata, selection, params); |
29be6023 RL |
835 | |
836 | err: | |
3f883c7c | 837 | OSSL_PARAM_free(params); |
6d4e6009 | 838 | OSSL_PARAM_BLD_free(tmpl); |
b305452f | 839 | return rv; |
29be6023 RL |
840 | } |
841 | ||
967cc3f9 RL |
842 | static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx, |
843 | int rsa_type) | |
0abae163 | 844 | { |
629c72db MC |
845 | EVP_PKEY_CTX *pctx = vpctx; |
846 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); | |
23b2fc0b | 847 | RSA *rsa = ossl_rsa_new_with_ctx(pctx->libctx); |
967cc3f9 | 848 | RSA_PSS_PARAMS_30 rsa_pss_params = { 0, }; |
bbde8566 | 849 | int pss_defaults_set = 0; |
967cc3f9 | 850 | int ok = 0; |
0abae163 RL |
851 | |
852 | if (rsa == NULL) { | |
e077455e | 853 | ERR_raise(ERR_LIB_DH, ERR_R_RSA_LIB); |
0abae163 RL |
854 | return 0; |
855 | } | |
856 | ||
967cc3f9 RL |
857 | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); |
858 | RSA_set_flags(rsa, rsa_type); | |
859 | ||
bbde8566 TM |
860 | if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params, &pss_defaults_set, |
861 | params, pctx->libctx)) | |
967cc3f9 RL |
862 | goto err; |
863 | ||
864 | switch (rsa_type) { | |
865 | case RSA_FLAG_TYPE_RSA: | |
866 | /* | |
867 | * Were PSS parameters filled in? | |
868 | * In that case, something's wrong | |
869 | */ | |
23b2fc0b | 870 | if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) |
967cc3f9 RL |
871 | goto err; |
872 | break; | |
873 | case RSA_FLAG_TYPE_RSASSAPSS: | |
874 | /* | |
875 | * Were PSS parameters filled in? In that case, create the old | |
876 | * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key. | |
877 | */ | |
23b2fc0b | 878 | if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) { |
967cc3f9 | 879 | /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */ |
23b2fc0b P |
880 | int mdnid = ossl_rsa_pss_params_30_hashalg(&rsa_pss_params); |
881 | int mgf1mdnid = ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params); | |
882 | int saltlen = ossl_rsa_pss_params_30_saltlen(&rsa_pss_params); | |
967cc3f9 RL |
883 | const EVP_MD *md = EVP_get_digestbynid(mdnid); |
884 | const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid); | |
885 | ||
4158b0dc SL |
886 | if ((rsa->pss = ossl_rsa_pss_params_create(md, mgf1md, |
887 | saltlen)) == NULL) | |
967cc3f9 RL |
888 | goto err; |
889 | } | |
890 | break; | |
891 | default: | |
892 | /* RSA key sub-types we don't know how to handle yet */ | |
893 | goto err; | |
0abae163 | 894 | } |
967cc3f9 | 895 | |
944f822a | 896 | if (!ossl_rsa_fromdata(rsa, params, 1)) |
967cc3f9 RL |
897 | goto err; |
898 | ||
899 | switch (rsa_type) { | |
900 | case RSA_FLAG_TYPE_RSA: | |
901 | ok = EVP_PKEY_assign_RSA(pkey, rsa); | |
902 | break; | |
903 | case RSA_FLAG_TYPE_RSASSAPSS: | |
904 | ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa); | |
905 | break; | |
906 | } | |
907 | ||
908 | err: | |
909 | if (!ok) | |
910 | RSA_free(rsa); | |
911 | return ok; | |
912 | } | |
913 | ||
914 | static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata, | |
bed7437b RL |
915 | OSSL_FUNC_keymgmt_import_fn *importer, |
916 | OSSL_LIB_CTX *libctx, const char *propq) | |
967cc3f9 RL |
917 | { |
918 | return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata, | |
bed7437b | 919 | importer, libctx, propq); |
967cc3f9 RL |
920 | } |
921 | ||
922 | static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata, | |
bed7437b RL |
923 | OSSL_FUNC_keymgmt_import_fn *importer, |
924 | OSSL_LIB_CTX *libctx, const char *propq) | |
967cc3f9 RL |
925 | { |
926 | return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata, | |
bed7437b | 927 | importer, libctx, propq); |
967cc3f9 RL |
928 | } |
929 | ||
930 | static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx) | |
931 | { | |
932 | return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA); | |
933 | } | |
934 | ||
935 | static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx) | |
936 | { | |
937 | return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS); | |
0abae163 RL |
938 | } |
939 | ||
2145ba5e TM |
940 | static int rsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from) |
941 | { | |
942 | RSA *rsa = from->pkey.rsa; | |
943 | RSA *dupkey = NULL; | |
944 | int ret; | |
945 | ||
946 | if (rsa != NULL) { | |
b4f447c0 | 947 | dupkey = ossl_rsa_dup(rsa, OSSL_KEYMGMT_SELECT_ALL); |
2145ba5e TM |
948 | if (dupkey == NULL) |
949 | return 0; | |
950 | } | |
951 | ||
952 | ret = EVP_PKEY_assign(to, from->type, dupkey); | |
953 | if (!ret) | |
954 | RSA_free(dupkey); | |
955 | return ret; | |
956 | } | |
957 | ||
adf7e6d1 | 958 | const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2] = { |
0f113f3e MC |
959 | { |
960 | EVP_PKEY_RSA, | |
961 | EVP_PKEY_RSA, | |
962 | ASN1_PKEY_SIGPARAM_NULL, | |
963 | ||
964 | "RSA", | |
965 | "OpenSSL RSA method", | |
966 | ||
967 | rsa_pub_decode, | |
968 | rsa_pub_encode, | |
969 | rsa_pub_cmp, | |
970 | rsa_pub_print, | |
971 | ||
972 | rsa_priv_decode, | |
973 | rsa_priv_encode, | |
974 | rsa_priv_print, | |
975 | ||
976 | int_rsa_size, | |
977 | rsa_bits, | |
978 | rsa_security_bits, | |
979 | ||
980 | 0, 0, 0, 0, 0, 0, | |
981 | ||
982 | rsa_sig_print, | |
983 | int_rsa_free, | |
984 | rsa_pkey_ctrl, | |
985 | old_rsa_priv_decode, | |
986 | old_rsa_priv_encode, | |
987 | rsa_item_verify, | |
629e369c | 988 | rsa_item_sign, |
2aee35d3 | 989 | rsa_sig_info_set, |
29be6023 RL |
990 | rsa_pkey_check, |
991 | ||
992 | 0, 0, | |
993 | 0, 0, 0, 0, | |
994 | ||
995 | rsa_pkey_dirty_cnt, | |
0abae163 | 996 | rsa_pkey_export_to, |
2145ba5e TM |
997 | rsa_pkey_import_from, |
998 | rsa_pkey_copy | |
629e369c | 999 | }, |
0f113f3e MC |
1000 | |
1001 | { | |
1002 | EVP_PKEY_RSA2, | |
1003 | EVP_PKEY_RSA, | |
1004 | ASN1_PKEY_ALIAS} | |
1005 | }; | |
4e8ba747 | 1006 | |
adf7e6d1 | 1007 | const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth = { |
4e8ba747 DSH |
1008 | EVP_PKEY_RSA_PSS, |
1009 | EVP_PKEY_RSA_PSS, | |
1010 | ASN1_PKEY_SIGPARAM_NULL, | |
1011 | ||
1012 | "RSA-PSS", | |
1013 | "OpenSSL RSA-PSS method", | |
1014 | ||
1015 | rsa_pub_decode, | |
1016 | rsa_pub_encode, | |
1017 | rsa_pub_cmp, | |
1018 | rsa_pub_print, | |
1019 | ||
1020 | rsa_priv_decode, | |
1021 | rsa_priv_encode, | |
1022 | rsa_priv_print, | |
1023 | ||
1024 | int_rsa_size, | |
1025 | rsa_bits, | |
1026 | rsa_security_bits, | |
1027 | ||
1028 | 0, 0, 0, 0, 0, 0, | |
1029 | ||
1030 | rsa_sig_print, | |
1031 | int_rsa_free, | |
1032 | rsa_pkey_ctrl, | |
1033 | 0, 0, | |
1034 | rsa_item_verify, | |
1035 | rsa_item_sign, | |
03f5c893 | 1036 | rsa_sig_info_set, |
29be6023 RL |
1037 | rsa_pkey_check, |
1038 | ||
1039 | 0, 0, | |
1040 | 0, 0, 0, 0, | |
1041 | ||
1042 | rsa_pkey_dirty_cnt, | |
967cc3f9 | 1043 | rsa_pss_pkey_export_to, |
2145ba5e TM |
1044 | rsa_pss_pkey_import_from, |
1045 | rsa_pkey_copy | |
4e8ba747 | 1046 | }; |