xref: /freebsd/crypto/openssl/test/keymgmt_internal_test.c (revision 44096ebd22ddd0081a357011714eff8963614b65)
1e0c4386eSCy Schubert /*
2*44096ebdSEnji Cooper  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert  *
4e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert  */
9e0c4386eSCy Schubert 
10e0c4386eSCy Schubert /*
11e0c4386eSCy Schubert  * RSA low level APIs are deprecated for public use, but still ok for
12e0c4386eSCy Schubert  * internal use.
13e0c4386eSCy Schubert  */
14e0c4386eSCy Schubert #include "internal/deprecated.h"
15e0c4386eSCy Schubert 
16e0c4386eSCy Schubert #include <string.h>
17e0c4386eSCy Schubert 
18e0c4386eSCy Schubert #include <openssl/bio.h>
19e0c4386eSCy Schubert #include <openssl/bn.h>
20e0c4386eSCy Schubert #include <openssl/rsa.h>
21e0c4386eSCy Schubert #include <openssl/evp.h>
22e0c4386eSCy Schubert #include <openssl/pem.h>
23e0c4386eSCy Schubert #include <openssl/provider.h>
24e0c4386eSCy Schubert #include <openssl/core_names.h>
25e0c4386eSCy Schubert #include "internal/core.h"
26e0c4386eSCy Schubert #include "internal/nelem.h"
27e0c4386eSCy Schubert #include "crypto/evp.h"          /* For the internal API */
28e0c4386eSCy Schubert #include "testutil.h"
29e0c4386eSCy Schubert 
30e0c4386eSCy Schubert typedef struct {
31e0c4386eSCy Schubert     OSSL_LIB_CTX *ctx1;
32e0c4386eSCy Schubert     OSSL_PROVIDER *prov1;
33e0c4386eSCy Schubert     OSSL_LIB_CTX *ctx2;
34e0c4386eSCy Schubert     OSSL_PROVIDER *prov2;
35e0c4386eSCy Schubert } FIXTURE;
36e0c4386eSCy Schubert 
37e0c4386eSCy Schubert /* Collected arguments */
38e0c4386eSCy Schubert static const char *cert_filename = NULL;
39e0c4386eSCy Schubert 
40e0c4386eSCy Schubert static void tear_down(FIXTURE *fixture)
41e0c4386eSCy Schubert {
42e0c4386eSCy Schubert     if (fixture != NULL) {
43e0c4386eSCy Schubert         OSSL_PROVIDER_unload(fixture->prov1);
44e0c4386eSCy Schubert         OSSL_PROVIDER_unload(fixture->prov2);
45e0c4386eSCy Schubert         OSSL_LIB_CTX_free(fixture->ctx1);
46e0c4386eSCy Schubert         OSSL_LIB_CTX_free(fixture->ctx2);
47e0c4386eSCy Schubert         OPENSSL_free(fixture);
48e0c4386eSCy Schubert     }
49e0c4386eSCy Schubert }
50e0c4386eSCy Schubert 
51e0c4386eSCy Schubert static FIXTURE *set_up(const char *testcase_name)
52e0c4386eSCy Schubert {
53e0c4386eSCy Schubert     FIXTURE *fixture;
54e0c4386eSCy Schubert 
55e0c4386eSCy Schubert     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))
56e0c4386eSCy Schubert         || !TEST_ptr(fixture->ctx1 = OSSL_LIB_CTX_new())
57e0c4386eSCy Schubert         || !TEST_ptr(fixture->prov1 = OSSL_PROVIDER_load(fixture->ctx1,
58e0c4386eSCy Schubert                                                          "default"))
59e0c4386eSCy Schubert         || !TEST_ptr(fixture->ctx2 = OSSL_LIB_CTX_new())
60e0c4386eSCy Schubert         || !TEST_ptr(fixture->prov2 = OSSL_PROVIDER_load(fixture->ctx2,
61e0c4386eSCy Schubert                                                          "default"))) {
62e0c4386eSCy Schubert         tear_down(fixture);
63e0c4386eSCy Schubert         return NULL;
64e0c4386eSCy Schubert     }
65e0c4386eSCy Schubert     return fixture;
66e0c4386eSCy Schubert }
67e0c4386eSCy Schubert 
68e0c4386eSCy Schubert /* Array indexes */
69e0c4386eSCy Schubert #define N       0
70e0c4386eSCy Schubert #define E       1
71e0c4386eSCy Schubert #define D       2
72e0c4386eSCy Schubert #define P       3
73e0c4386eSCy Schubert #define Q       4
74e0c4386eSCy Schubert #define F3      5                /* Extra factor */
75e0c4386eSCy Schubert #define DP      6
76e0c4386eSCy Schubert #define DQ      7
77e0c4386eSCy Schubert #define E3      8                /* Extra exponent */
78e0c4386eSCy Schubert #define QINV    9
79e0c4386eSCy Schubert #define C2      10               /* Extra coefficient */
80e0c4386eSCy Schubert 
81e0c4386eSCy Schubert /*
82e0c4386eSCy Schubert  * We have to do this because OSSL_PARAM_get_ulong() can't handle params
83e0c4386eSCy Schubert  * holding data that isn't exactly sizeof(uint32_t) or sizeof(uint64_t),
84e0c4386eSCy Schubert  * and because the other end deals with BIGNUM, the resulting param might
85e0c4386eSCy Schubert  * be any size.  In this particular test, we know that the expected data
86e0c4386eSCy Schubert  * fits within an unsigned long, and we want to get the data in that form
87e0c4386eSCy Schubert  * to make testing of values easier.
88e0c4386eSCy Schubert  */
89e0c4386eSCy Schubert static int get_ulong_via_BN(const OSSL_PARAM *p, unsigned long *goal)
90e0c4386eSCy Schubert {
91e0c4386eSCy Schubert     BIGNUM *n = NULL;
92e0c4386eSCy Schubert     int ret = 1;                 /* Ever so hopeful */
93e0c4386eSCy Schubert 
94e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_BN(p, &n))
95e0c4386eSCy Schubert         || !TEST_int_ge(BN_bn2nativepad(n, (unsigned char *)goal, sizeof(*goal)), 0))
96e0c4386eSCy Schubert         ret = 0;
97e0c4386eSCy Schubert     BN_free(n);
98e0c4386eSCy Schubert     return ret;
99e0c4386eSCy Schubert }
100e0c4386eSCy Schubert 
101e0c4386eSCy Schubert static int export_cb(const OSSL_PARAM *params, void *arg)
102e0c4386eSCy Schubert {
103e0c4386eSCy Schubert     unsigned long *keydata = arg;
104e0c4386eSCy Schubert     const OSSL_PARAM *p = NULL;
105e0c4386eSCy Schubert 
106e0c4386eSCy Schubert     if (keydata == NULL)
107e0c4386eSCy Schubert         return 0;
108e0c4386eSCy Schubert 
109e0c4386eSCy Schubert     if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N))
110e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[N]))
111e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E))
112e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[E]))
113e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D))
114e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[D])))
115e0c4386eSCy Schubert         return 0;
116e0c4386eSCy Schubert 
117e0c4386eSCy Schubert     if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR1))
118e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[P]))
119e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR2))
120e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[Q]))
121e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR3))
122e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[F3])))
123e0c4386eSCy Schubert         return 0;
124e0c4386eSCy Schubert 
125e0c4386eSCy Schubert     if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT1))
126e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[DP]))
127e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT2))
128e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[DQ]))
129e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT3))
130e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[E3])))
131e0c4386eSCy Schubert         return 0;
132e0c4386eSCy Schubert 
133e0c4386eSCy Schubert     if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT1))
134e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[QINV]))
135e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT2))
136e0c4386eSCy Schubert         || !TEST_true(get_ulong_via_BN(p, &keydata[C2])))
137e0c4386eSCy Schubert         return 0;
138e0c4386eSCy Schubert 
139e0c4386eSCy Schubert     return 1;
140e0c4386eSCy Schubert }
141e0c4386eSCy Schubert 
142e0c4386eSCy Schubert static int test_pass_rsa(FIXTURE *fixture)
143e0c4386eSCy Schubert {
144e0c4386eSCy Schubert     size_t i;
145e0c4386eSCy Schubert     int ret = 0;
146e0c4386eSCy Schubert     RSA *rsa = NULL;
147e0c4386eSCy Schubert     BIGNUM *bn1 = NULL, *bn2 = NULL, *bn3 = NULL;
148e0c4386eSCy Schubert     EVP_PKEY *pk = NULL, *dup_pk = NULL;
149e0c4386eSCy Schubert     EVP_KEYMGMT *km = NULL, *km1 = NULL, *km2 = NULL, *km3 = NULL;
150e0c4386eSCy Schubert     void *provkey = NULL, *provkey2 = NULL;
151e0c4386eSCy Schubert     BIGNUM *bn_primes[1] = { NULL };
152e0c4386eSCy Schubert     BIGNUM *bn_exps[1] = { NULL };
153e0c4386eSCy Schubert     BIGNUM *bn_coeffs[1] = { NULL };
154e0c4386eSCy Schubert     /*
155e0c4386eSCy Schubert      * 32-bit RSA key, extracted from this command,
156e0c4386eSCy Schubert      * executed with OpenSSL 1.0.2:
157e0c4386eSCy Schubert      * An extra factor was added just for testing purposes.
158e0c4386eSCy Schubert      *
159e0c4386eSCy Schubert      * openssl genrsa 32 | openssl rsa -text
160e0c4386eSCy Schubert      */
161e0c4386eSCy Schubert     static BN_ULONG expected[] = {
162e0c4386eSCy Schubert         0xbc747fc5,              /* N */
163e0c4386eSCy Schubert         0x10001,                 /* E */
164e0c4386eSCy Schubert         0x7b133399,              /* D */
165e0c4386eSCy Schubert         0xe963,                  /* P */
166e0c4386eSCy Schubert         0xceb7,                  /* Q */
167e0c4386eSCy Schubert         1,                       /* F3 */
168e0c4386eSCy Schubert         0x8599,                  /* DP */
169e0c4386eSCy Schubert         0xbd87,                  /* DQ */
170e0c4386eSCy Schubert         2,                       /* E3 */
171e0c4386eSCy Schubert         0xcc3b,                  /* QINV */
172e0c4386eSCy Schubert         3,                       /* C3 */
173e0c4386eSCy Schubert         0                        /* Extra, should remain zero */
174e0c4386eSCy Schubert     };
175e0c4386eSCy Schubert     static unsigned long keydata[OSSL_NELEM(expected)] = { 0, };
176e0c4386eSCy Schubert 
177e0c4386eSCy Schubert     if (!TEST_ptr(rsa = RSA_new()))
178e0c4386eSCy Schubert         goto err;
179e0c4386eSCy Schubert 
180e0c4386eSCy Schubert     if (!TEST_ptr(bn1 = BN_new())
181e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn1, expected[N]))
182e0c4386eSCy Schubert         || !TEST_ptr(bn2 = BN_new())
183e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn2, expected[E]))
184e0c4386eSCy Schubert         || !TEST_ptr(bn3 = BN_new())
185e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn3, expected[D]))
186e0c4386eSCy Schubert         || !TEST_true(RSA_set0_key(rsa, bn1, bn2, bn3)))
187e0c4386eSCy Schubert         goto err;
188e0c4386eSCy Schubert 
189e0c4386eSCy Schubert     if (!TEST_ptr(bn1 = BN_new())
190e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn1, expected[P]))
191e0c4386eSCy Schubert         || !TEST_ptr(bn2 = BN_new())
192e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn2, expected[Q]))
193e0c4386eSCy Schubert         || !TEST_true(RSA_set0_factors(rsa, bn1, bn2)))
194e0c4386eSCy Schubert         goto err;
195e0c4386eSCy Schubert 
196e0c4386eSCy Schubert     if (!TEST_ptr(bn1 = BN_new())
197e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn1, expected[DP]))
198e0c4386eSCy Schubert         || !TEST_ptr(bn2 = BN_new())
199e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn2, expected[DQ]))
200e0c4386eSCy Schubert         || !TEST_ptr(bn3 = BN_new())
201e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn3, expected[QINV]))
202e0c4386eSCy Schubert         || !TEST_true(RSA_set0_crt_params(rsa, bn1, bn2, bn3)))
203e0c4386eSCy Schubert         goto err;
204e0c4386eSCy Schubert     bn1 = bn2 = bn3 = NULL;
205e0c4386eSCy Schubert 
206e0c4386eSCy Schubert     if (!TEST_ptr(bn_primes[0] = BN_new())
207e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn_primes[0], expected[F3]))
208e0c4386eSCy Schubert         || !TEST_ptr(bn_exps[0] = BN_new())
209e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn_exps[0], expected[E3]))
210e0c4386eSCy Schubert         || !TEST_ptr(bn_coeffs[0] = BN_new())
211e0c4386eSCy Schubert         || !TEST_true(BN_set_word(bn_coeffs[0], expected[C2]))
212e0c4386eSCy Schubert         || !TEST_true(RSA_set0_multi_prime_params(rsa, bn_primes, bn_exps,
213e0c4386eSCy Schubert                                                   bn_coeffs, 1)))
214e0c4386eSCy Schubert         goto err;
215e0c4386eSCy Schubert 
216e0c4386eSCy Schubert     if (!TEST_ptr(pk = EVP_PKEY_new())
217e0c4386eSCy Schubert         || !TEST_true(EVP_PKEY_assign_RSA(pk, rsa)))
218e0c4386eSCy Schubert         goto err;
219e0c4386eSCy Schubert     rsa = NULL;
220e0c4386eSCy Schubert 
221e0c4386eSCy Schubert     if (!TEST_ptr(km1 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA", NULL))
222e0c4386eSCy Schubert         || !TEST_ptr(km2 = EVP_KEYMGMT_fetch(fixture->ctx2, "RSA", NULL))
223e0c4386eSCy Schubert         || !TEST_ptr(km3 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA-PSS", NULL))
224e0c4386eSCy Schubert         || !TEST_ptr_ne(km1, km2))
225e0c4386eSCy Schubert         goto err;
226e0c4386eSCy Schubert 
227*44096ebdSEnji Cooper     for (;;) {
228e0c4386eSCy Schubert         ret = 0;
229e0c4386eSCy Schubert         km = km3;
230e0c4386eSCy Schubert         /* Check that we can't export an RSA key into an RSA-PSS keymanager */
231e0c4386eSCy Schubert         if (!TEST_ptr_null(provkey2 = evp_pkey_export_to_provider(pk, NULL,
232e0c4386eSCy Schubert                                                                   &km,
233e0c4386eSCy Schubert                                                                   NULL)))
234e0c4386eSCy Schubert             goto err;
235e0c4386eSCy Schubert 
236e0c4386eSCy Schubert         if (!TEST_ptr(provkey = evp_pkey_export_to_provider(pk, NULL, &km1,
237e0c4386eSCy Schubert                                                             NULL))
238e0c4386eSCy Schubert             || !TEST_true(evp_keymgmt_export(km2, provkey,
239e0c4386eSCy Schubert                                              OSSL_KEYMGMT_SELECT_KEYPAIR,
240e0c4386eSCy Schubert                                              &export_cb, keydata)))
241e0c4386eSCy Schubert             goto err;
242e0c4386eSCy Schubert 
243e0c4386eSCy Schubert         /*
244e0c4386eSCy Schubert          * At this point, the hope is that keydata will have all the numbers
245e0c4386eSCy Schubert          * from the key.
246e0c4386eSCy Schubert          */
247e0c4386eSCy Schubert 
248e0c4386eSCy Schubert         for (i = 0; i < OSSL_NELEM(expected); i++) {
249e0c4386eSCy Schubert             int rv = TEST_int_eq(expected[i], keydata[i]);
250e0c4386eSCy Schubert 
251e0c4386eSCy Schubert             if (!rv)
252e0c4386eSCy Schubert                 TEST_info("i = %zu", i);
253e0c4386eSCy Schubert             else
254e0c4386eSCy Schubert                 ret++;
255e0c4386eSCy Schubert         }
256e0c4386eSCy Schubert 
257e0c4386eSCy Schubert         ret = (ret == OSSL_NELEM(expected));
258*44096ebdSEnji Cooper 
259*44096ebdSEnji Cooper         if (!ret || dup_pk != NULL)
260*44096ebdSEnji Cooper             break;
261*44096ebdSEnji Cooper 
262*44096ebdSEnji Cooper         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
263e0c4386eSCy Schubert             goto err;
264e0c4386eSCy Schubert 
265e0c4386eSCy Schubert         ret = TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
266e0c4386eSCy Schubert         EVP_PKEY_free(pk);
267e0c4386eSCy Schubert         pk = dup_pk;
268e0c4386eSCy Schubert         if (!ret)
269e0c4386eSCy Schubert             goto err;
270e0c4386eSCy Schubert     }
271e0c4386eSCy Schubert 
272e0c4386eSCy Schubert  err:
273e0c4386eSCy Schubert     RSA_free(rsa);
274e0c4386eSCy Schubert     BN_free(bn1);
275e0c4386eSCy Schubert     BN_free(bn2);
276e0c4386eSCy Schubert     BN_free(bn3);
277e0c4386eSCy Schubert     EVP_PKEY_free(pk);
278e0c4386eSCy Schubert     EVP_KEYMGMT_free(km1);
279e0c4386eSCy Schubert     EVP_KEYMGMT_free(km2);
280e0c4386eSCy Schubert     EVP_KEYMGMT_free(km3);
281e0c4386eSCy Schubert 
282e0c4386eSCy Schubert     return ret;
283e0c4386eSCy Schubert }
284e0c4386eSCy Schubert 
285e0c4386eSCy Schubert static int (*tests[])(FIXTURE *) = {
286e0c4386eSCy Schubert     test_pass_rsa
287e0c4386eSCy Schubert };
288e0c4386eSCy Schubert 
289e0c4386eSCy Schubert static int test_pass_key(int n)
290e0c4386eSCy Schubert {
291e0c4386eSCy Schubert     SETUP_TEST_FIXTURE(FIXTURE, set_up);
292e0c4386eSCy Schubert     EXECUTE_TEST(tests[n], tear_down);
293e0c4386eSCy Schubert     return result;
294e0c4386eSCy Schubert }
295e0c4386eSCy Schubert 
296e0c4386eSCy Schubert static int test_evp_pkey_export_to_provider(int n)
297e0c4386eSCy Schubert {
298e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
299e0c4386eSCy Schubert     OSSL_PROVIDER *prov = NULL;
300e0c4386eSCy Schubert     X509 *cert = NULL;
301e0c4386eSCy Schubert     BIO *bio = NULL;
302e0c4386eSCy Schubert     X509_PUBKEY *pubkey = NULL;
303e0c4386eSCy Schubert     EVP_KEYMGMT *keymgmt = NULL;
304e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
305e0c4386eSCy Schubert     void *keydata = NULL;
306e0c4386eSCy Schubert     int ret = 0;
307e0c4386eSCy Schubert 
308e0c4386eSCy Schubert     if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())
309e0c4386eSCy Schubert          || !TEST_ptr(prov = OSSL_PROVIDER_load(libctx, "default")))
310e0c4386eSCy Schubert         goto end;
311e0c4386eSCy Schubert 
312e0c4386eSCy Schubert     if ((bio = BIO_new_file(cert_filename, "r")) == NULL) {
313e0c4386eSCy Schubert         TEST_error("Couldn't open '%s' for reading\n", cert_filename);
314e0c4386eSCy Schubert         TEST_openssl_errors();
315e0c4386eSCy Schubert         goto end;
316e0c4386eSCy Schubert     }
317e0c4386eSCy Schubert 
318e0c4386eSCy Schubert     if ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
319e0c4386eSCy Schubert         TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n",
320e0c4386eSCy Schubert                    cert_filename);
321e0c4386eSCy Schubert         TEST_openssl_errors();
322e0c4386eSCy Schubert         goto end;
323e0c4386eSCy Schubert     }
324e0c4386eSCy Schubert 
325e0c4386eSCy Schubert     pubkey = X509_get_X509_PUBKEY(cert);
326e0c4386eSCy Schubert     pkey = X509_PUBKEY_get0(pubkey);
327e0c4386eSCy Schubert 
328e0c4386eSCy Schubert     if (n == 0) {
329e0c4386eSCy Schubert         if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL,
330e0c4386eSCy Schubert                                                             NULL, NULL)))
331e0c4386eSCy Schubert             goto end;
332e0c4386eSCy Schubert     } else if (n == 1) {
333e0c4386eSCy Schubert         if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL,
334e0c4386eSCy Schubert                                                             &keymgmt, NULL)))
335e0c4386eSCy Schubert             goto end;
336e0c4386eSCy Schubert     } else {
337e0c4386eSCy Schubert         keymgmt = EVP_KEYMGMT_fetch(libctx, "RSA", NULL);
338e0c4386eSCy Schubert 
339e0c4386eSCy Schubert         if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL,
340e0c4386eSCy Schubert                                                             &keymgmt, NULL)))
341e0c4386eSCy Schubert             goto end;
342e0c4386eSCy Schubert     }
343e0c4386eSCy Schubert 
344e0c4386eSCy Schubert     ret = 1;
345e0c4386eSCy Schubert  end:
346e0c4386eSCy Schubert     BIO_free(bio);
347e0c4386eSCy Schubert     X509_free(cert);
348e0c4386eSCy Schubert     EVP_KEYMGMT_free(keymgmt);
349e0c4386eSCy Schubert     OSSL_PROVIDER_unload(prov);
350e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
351e0c4386eSCy Schubert     return ret;
352e0c4386eSCy Schubert }
353e0c4386eSCy Schubert 
354e0c4386eSCy Schubert int setup_tests(void)
355e0c4386eSCy Schubert {
356e0c4386eSCy Schubert     if (!TEST_ptr(cert_filename = test_get_argument(0)))
357e0c4386eSCy Schubert         return 0;
358e0c4386eSCy Schubert 
359e0c4386eSCy Schubert     ADD_ALL_TESTS(test_pass_key, 1);
360e0c4386eSCy Schubert     ADD_ALL_TESTS(test_evp_pkey_export_to_provider, 3);
361e0c4386eSCy Schubert     return 1;
362e0c4386eSCy Schubert }
363