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