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