1 /*
2  * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <string.h>
17 #include <openssl/core_names.h>
18 #include <openssl/params.h>
19 #include <openssl/err.h>
20 #include <openssl/evp.h>
21 #ifndef FIPS_MODULE
22 # include <openssl/x509.h>
23 # include "crypto/asn1.h"
24 #endif
25 #include "internal/sizes.h"
26 #include "internal/param_build_set.h"
27 #include "crypto/rsa.h"
28 #include "rsa_local.h"
29 
30 #include "e_os.h"                /* strcasecmp for Windows() */
31 
32 /*
33  * The intention with the "backend" source file is to offer backend support
34  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
35  * implementations alike.
36  */
37 
DEFINE_STACK_OF(BIGNUM)38 DEFINE_STACK_OF(BIGNUM)
39 
40 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
41                            const OSSL_PARAM params[], const char *names[])
42 {
43     const OSSL_PARAM *p = NULL;
44     int i;
45 
46     if (numbers == NULL)
47         return 0;
48 
49     for (i = 0; names[i] != NULL; i++){
50         p = OSSL_PARAM_locate_const(params, names[i]);
51         if (p != NULL) {
52             BIGNUM *tmp = NULL;
53 
54             if (!OSSL_PARAM_get_BN(p, &tmp)
55                 || sk_BIGNUM_push(numbers, tmp) == 0)
56                 return 0;
57         }
58     }
59 
60     return 1;
61 }
62 
ossl_rsa_fromdata(RSA * rsa,const OSSL_PARAM params[],int include_private)63 int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private)
64 {
65     const OSSL_PARAM *param_n, *param_e,  *param_d = NULL;
66     BIGNUM *n = NULL, *e = NULL, *d = NULL;
67     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
68     int is_private = 0;
69 
70     if (rsa == NULL)
71         return 0;
72 
73     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
74     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
75     if (include_private)
76         param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
77 
78     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
79         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
80         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
81         goto err;
82 
83     is_private = (d != NULL);
84 
85     if (!RSA_set0_key(rsa, n, e, d))
86         goto err;
87     n = e = d = NULL;
88 
89     if (is_private) {
90         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
91                              ossl_rsa_mp_factor_names)
92             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
93                                 ossl_rsa_mp_exp_names)
94             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
95                                 ossl_rsa_mp_coeff_names))
96             goto err;
97 
98         /* It's ok if this private key just has n, e and d */
99         if (sk_BIGNUM_num(factors) != 0
100             && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs))
101             goto err;
102     }
103 
104 
105     sk_BIGNUM_free(factors);
106     sk_BIGNUM_free(exps);
107     sk_BIGNUM_free(coeffs);
108     return 1;
109 
110  err:
111     BN_free(n);
112     BN_free(e);
113     BN_free(d);
114     sk_BIGNUM_pop_free(factors, BN_free);
115     sk_BIGNUM_pop_free(exps, BN_free);
116     sk_BIGNUM_pop_free(coeffs, BN_free);
117     return 0;
118 }
119 
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)120 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
121 
122 int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
123                     int include_private)
124 {
125     int ret = 0;
126     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
127     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
128     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
129     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
130 
131     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
132         goto err;
133 
134     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
135     ossl_rsa_get0_all_params(rsa, factors, exps, coeffs);
136 
137     if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
138         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e))
139         goto err;
140 
141     /* Check private key data integrity */
142     if (include_private && rsa_d != NULL) {
143         int numprimes = sk_BIGNUM_const_num(factors);
144         int numexps = sk_BIGNUM_const_num(exps);
145         int numcoeffs = sk_BIGNUM_const_num(coeffs);
146 
147         /*
148          * It's permissible to have zero primes, i.e. no CRT params.
149          * Otherwise, there must be at least two, as many exponents,
150          * and one coefficient less.
151          */
152         if (numprimes != 0
153             && (numprimes < 2 || numexps < 2 || numcoeffs < 1))
154             goto err;
155 
156         if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D,
157                                      rsa_d)
158             || !ossl_param_build_set_multi_key_bn(bld, params,
159                                                   ossl_rsa_mp_factor_names,
160                                                   factors)
161             || !ossl_param_build_set_multi_key_bn(bld, params,
162                                                   ossl_rsa_mp_exp_names, exps)
163             || !ossl_param_build_set_multi_key_bn(bld, params,
164                                                   ossl_rsa_mp_coeff_names,
165                                                   coeffs))
166         goto err;
167     }
168 
169 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
170     /* The acvp test results are not meant for export so check for bld == NULL */
171     if (bld == NULL)
172         ossl_rsa_acvp_test_get_params(rsa, params);
173 #endif
174     ret = 1;
175  err:
176     sk_BIGNUM_const_free(factors);
177     sk_BIGNUM_const_free(exps);
178     sk_BIGNUM_const_free(coeffs);
179     return ret;
180 }
181 
ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 * pss,OSSL_PARAM_BLD * bld,OSSL_PARAM params[])182 int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss,
183                                   OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
184 {
185     if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
186         int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss);
187         int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss);
188         int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
189         int saltlen = ossl_rsa_pss_params_30_saltlen(pss);
190         int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL);
191         int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
192         int default_maskgenhashalg_nid =
193                 ossl_rsa_pss_params_30_maskgenhashalg(NULL);
194         const char *mdname =
195             (hashalg_nid == default_hashalg_nid
196              ? NULL : ossl_rsa_oaeppss_nid2name(hashalg_nid));
197         const char *mgfname =
198             (maskgenalg_nid == default_maskgenalg_nid
199              ? NULL : ossl_rsa_oaeppss_nid2name(maskgenalg_nid));
200         const char *mgf1mdname =
201             (maskgenhashalg_nid == default_maskgenhashalg_nid
202              ? NULL : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid));
203         const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;
204         const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;
205         const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
206         const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
207 
208         /*
209          * To ensure that the key isn't seen as unrestricted by the recipient,
210          * we make sure that at least one PSS-related parameter is passed, even
211          * if it has a default value; saltlen.
212          */
213         if ((mdname != NULL
214              && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))
215             || (mgfname != NULL
216                 && !ossl_param_build_set_utf8_string(bld, params,
217                                                      key_mgf, mgfname))
218             || (mgf1mdname != NULL
219                 && !ossl_param_build_set_utf8_string(bld, params,
220                                                      key_mgf1_md, mgf1mdname))
221             || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))
222             return 0;
223     }
224     return 1;
225 }
226 
ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 * pss_params,int * defaults_set,const OSSL_PARAM params[],OSSL_LIB_CTX * libctx)227 int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params,
228                                     int *defaults_set,
229                                     const OSSL_PARAM params[],
230                                     OSSL_LIB_CTX *libctx)
231 {
232     const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md,  *param_saltlen;
233     const OSSL_PARAM *param_propq;
234     const char *propq = NULL;
235     EVP_MD *md = NULL, *mgf1md = NULL;
236     int saltlen;
237     int ret = 0;
238 
239     if (pss_params == NULL)
240         return 0;
241     param_propq =
242         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
243     param_md =
244         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
245     param_mgf =
246         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
247     param_mgf1md =
248         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
249     param_saltlen =
250         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
251 
252     if (param_propq != NULL) {
253         if (param_propq->data_type == OSSL_PARAM_UTF8_STRING)
254             propq = param_propq->data;
255     }
256     /*
257      * If we get any of the parameters, we know we have at least some
258      * restrictions, so we start by setting default values, and let each
259      * parameter override their specific restriction data.
260      */
261     if (!*defaults_set
262         && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL
263             || param_saltlen != NULL)) {
264         if (!ossl_rsa_pss_params_30_set_defaults(pss_params))
265             return 0;
266         *defaults_set = 1;
267     }
268 
269     if (param_mgf != NULL) {
270         int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
271         const char *mgfname = NULL;
272 
273         if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING)
274             mgfname = param_mgf->data;
275         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname))
276             return 0;
277 
278         if (strcasecmp(param_mgf->data,
279                        ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0)
280             return 0;
281     }
282 
283     /*
284      * We're only interested in the NIDs that correspond to the MDs, so the
285      * exact propquery is unimportant in the EVP_MD_fetch() calls below.
286      */
287 
288     if (param_md != NULL) {
289         const char *mdname = NULL;
290 
291         if (param_md->data_type == OSSL_PARAM_UTF8_STRING)
292             mdname = param_md->data;
293         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))
294             goto err;
295 
296         if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL
297             || !ossl_rsa_pss_params_30_set_hashalg(pss_params,
298                                                    ossl_rsa_oaeppss_md2nid(md)))
299             goto err;
300     }
301 
302     if (param_mgf1md != NULL) {
303         const char *mgf1mdname = NULL;
304 
305         if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)
306             mgf1mdname = param_mgf1md->data;
307         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))
308             goto err;
309 
310         if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL
311             || !ossl_rsa_pss_params_30_set_maskgenhashalg(
312                     pss_params, ossl_rsa_oaeppss_md2nid(mgf1md)))
313             goto err;
314     }
315 
316     if (param_saltlen != NULL) {
317         if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)
318             || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen))
319             goto err;
320     }
321 
322     ret = 1;
323 
324  err:
325     EVP_MD_free(md);
326     EVP_MD_free(mgf1md);
327     return ret;
328 }
329 
ossl_rsa_is_foreign(const RSA * rsa)330 int ossl_rsa_is_foreign(const RSA *rsa)
331 {
332 #ifndef FIPS_MODULE
333     if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
334         return 1;
335 #endif
336     return 0;
337 }
338 
rsa_bn_dup_check(BIGNUM ** out,const BIGNUM * f)339 static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
340 {
341     if (f != NULL && (*out = BN_dup(f)) == NULL)
342         return 0;
343     return 1;
344 }
345 
ossl_rsa_dup(const RSA * rsa,int selection)346 RSA *ossl_rsa_dup(const RSA *rsa, int selection)
347 {
348     RSA *dupkey = NULL;
349 #ifndef FIPS_MODULE
350     int pnum, i;
351 #endif
352 
353     /* Do not try to duplicate foreign RSA keys */
354     if (ossl_rsa_is_foreign(rsa))
355         return NULL;
356 
357     if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
358         return NULL;
359 
360     /* public key */
361     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
362         if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
363             goto err;
364         if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
365             goto err;
366     }
367 
368     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
369 
370         /* private key */
371         if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
372             goto err;
373 
374         /* factors and crt params */
375         if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
376             goto err;
377         if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
378             goto err;
379         if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
380             goto err;
381         if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
382             goto err;
383         if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
384             goto err;
385     }
386 
387     dupkey->version = rsa->version;
388     dupkey->flags = rsa->flags;
389     /* we always copy the PSS parameters regardless of selection */
390     dupkey->pss_params = rsa->pss_params;
391 
392 #ifndef FIPS_MODULE
393     /* multiprime */
394     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
395         && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) {
396         dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
397         if (dupkey->prime_infos == NULL)
398             goto err;
399         for (i = 0; i < pnum; i++) {
400             const RSA_PRIME_INFO *pinfo = NULL;
401             RSA_PRIME_INFO *duppinfo = NULL;
402 
403             if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL) {
404                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
405                 goto err;
406             }
407             /* push first so cleanup in error case works */
408             (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
409 
410             pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
411             if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
412                 goto err;
413             if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
414                 goto err;
415             if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
416                 goto err;
417         }
418         if (!ossl_rsa_multip_calc_product(dupkey))
419             goto err;
420     }
421 
422     if (rsa->pss != NULL) {
423         dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);
424         if (rsa->pss->maskGenAlgorithm != NULL
425             && dupkey->pss->maskGenAlgorithm == NULL) {
426             dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
427             if (dupkey->pss->maskHash == NULL)
428                 goto err;
429         }
430     }
431     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
432                             &dupkey->ex_data, &rsa->ex_data))
433         goto err;
434 #endif
435 
436     return dupkey;
437 
438  err:
439     RSA_free(dupkey);
440     return NULL;
441 }
442 
443 #ifndef FIPS_MODULE
ossl_rsa_pss_decode(const X509_ALGOR * alg)444 RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)
445 {
446     RSA_PSS_PARAMS *pss;
447 
448     pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
449                                     alg->parameter);
450 
451     if (pss == NULL)
452         return NULL;
453 
454     if (pss->maskGenAlgorithm != NULL) {
455         pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
456         if (pss->maskHash == NULL) {
457             RSA_PSS_PARAMS_free(pss);
458             return NULL;
459         }
460     }
461 
462     return pss;
463 }
464 
ossl_rsa_sync_to_pss_params_30(RSA * rsa)465 static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)
466 {
467     const RSA_PSS_PARAMS *legacy_pss = NULL;
468     RSA_PSS_PARAMS_30 *pss = NULL;
469 
470     if (rsa != NULL
471         && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL
472         && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) {
473         const EVP_MD *md = NULL, *mgf1md = NULL;
474         int md_nid, mgf1md_nid, saltlen, trailerField;
475         RSA_PSS_PARAMS_30 pss_params;
476 
477         /*
478          * We don't care about the validity of the fields here, we just
479          * want to synchronise values.  Verifying here makes it impossible
480          * to even read a key with invalid values, making it hard to test
481          * a bad situation.
482          *
483          * Other routines use ossl_rsa_pss_get_param(), so the values will
484          * be checked, eventually.
485          */
486         if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,
487                                                &saltlen, &trailerField))
488             return 0;
489         md_nid = EVP_MD_get_type(md);
490         mgf1md_nid = EVP_MD_get_type(mgf1md);
491         if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
492             || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
493             || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
494                                                           mgf1md_nid)
495             || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
496             || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
497                                                         trailerField))
498             return 0;
499         *pss = pss_params;
500     }
501     return 1;
502 }
503 
ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS * pss,const EVP_MD ** pmd,const EVP_MD ** pmgf1md,int * psaltlen,int * ptrailerField)504 int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
505                                       const EVP_MD **pmd, const EVP_MD **pmgf1md,
506                                       int *psaltlen, int *ptrailerField)
507 {
508     RSA_PSS_PARAMS_30 pss_params;
509 
510     /* Get the defaults from the ONE place */
511     (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
512 
513     if (pss == NULL)
514         return 0;
515     *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);
516     if (*pmd == NULL)
517         return 0;
518     *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);
519     if (*pmgf1md == NULL)
520         return 0;
521     if (pss->saltLength)
522         *psaltlen = ASN1_INTEGER_get(pss->saltLength);
523     else
524         *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
525     if (pss->trailerField)
526         *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
527     else
528         *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);;
529 
530     return 1;
531 }
532 
ossl_rsa_param_decode(RSA * rsa,const X509_ALGOR * alg)533 int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
534 {
535     RSA_PSS_PARAMS *pss;
536     const ASN1_OBJECT *algoid;
537     const void *algp;
538     int algptype;
539 
540     X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
541     if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
542         return 1;
543     if (algptype == V_ASN1_UNDEF)
544         return 1;
545     if (algptype != V_ASN1_SEQUENCE) {
546         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
547         return 0;
548     }
549     if ((pss = ossl_rsa_pss_decode(alg)) == NULL
550         || !ossl_rsa_set0_pss_params(rsa, pss)) {
551         RSA_PSS_PARAMS_free(pss);
552         return 0;
553     }
554     if (!ossl_rsa_sync_to_pss_params_30(rsa))
555         return 0;
556     return 1;
557 }
558 
ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO * p8inf,OSSL_LIB_CTX * libctx,const char * propq)559 RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
560                              OSSL_LIB_CTX *libctx, const char *propq)
561 {
562     const unsigned char *p;
563     RSA *rsa;
564     int pklen;
565     const X509_ALGOR *alg;
566 
567     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))
568         return 0;
569     rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
570     if (rsa == NULL) {
571         ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
572         return NULL;
573     }
574     if (!ossl_rsa_param_decode(rsa, alg)) {
575         RSA_free(rsa);
576         return NULL;
577     }
578 
579     RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
580     switch (OBJ_obj2nid(alg->algorithm)) {
581     case EVP_PKEY_RSA:
582         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
583         break;
584     case EVP_PKEY_RSA_PSS:
585         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
586         break;
587     default:
588         /* Leave the type bits zero */
589         break;
590     }
591 
592     return rsa;
593 }
594 #endif
595