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