1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2019-2021 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 * SHA256 low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.  Note, that due to symbols not being exported, only the
13e1051a39Sopenharmony_ci * #defines can be accessed.  In this case SHA256_CBLOCK.
14e1051a39Sopenharmony_ci */
15e1051a39Sopenharmony_ci#include "internal/deprecated.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci#include <string.h>
18e1051a39Sopenharmony_ci#include <openssl/sha.h>
19e1051a39Sopenharmony_ci#include <openssl/evp.h>
20e1051a39Sopenharmony_ci#include <openssl/provider.h>
21e1051a39Sopenharmony_ci#include "internal/sizes.h"
22e1051a39Sopenharmony_ci#include "testutil.h"
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_cistatic char *config_file = NULL;
25e1051a39Sopenharmony_cistatic char *alg = "digest";
26e1051a39Sopenharmony_cistatic int use_default_ctx = 0;
27e1051a39Sopenharmony_cistatic char *fetch_property = NULL;
28e1051a39Sopenharmony_cistatic int expected_fetch_result = 1;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_citypedef enum OPTION_choice {
31e1051a39Sopenharmony_ci    OPT_ERR = -1,
32e1051a39Sopenharmony_ci    OPT_EOF = 0,
33e1051a39Sopenharmony_ci    OPT_ALG_FETCH_TYPE,
34e1051a39Sopenharmony_ci    OPT_FETCH_PROPERTY,
35e1051a39Sopenharmony_ci    OPT_FETCH_FAILURE,
36e1051a39Sopenharmony_ci    OPT_USE_DEFAULTCTX,
37e1051a39Sopenharmony_ci    OPT_CONFIG_FILE,
38e1051a39Sopenharmony_ci    OPT_TEST_ENUM
39e1051a39Sopenharmony_ci} OPTION_CHOICE;
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciconst OPTIONS *test_get_options(void)
42e1051a39Sopenharmony_ci{
43e1051a39Sopenharmony_ci    static const OPTIONS test_options[] = {
44e1051a39Sopenharmony_ci        OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[provname...]\n"),
45e1051a39Sopenharmony_ci        { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" },
46e1051a39Sopenharmony_ci        { "type", OPT_ALG_FETCH_TYPE, 's', "The fetch type to test" },
47e1051a39Sopenharmony_ci        { "property", OPT_FETCH_PROPERTY, 's', "The fetch property e.g. provider=fips" },
48e1051a39Sopenharmony_ci        { "fetchfail", OPT_FETCH_FAILURE, '-', "fetch is expected to fail" },
49e1051a39Sopenharmony_ci        { "defaultctx", OPT_USE_DEFAULTCTX, '-',
50e1051a39Sopenharmony_ci          "Use the default context if this is set" },
51e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "file\tProvider names to explicitly load\n" },
52e1051a39Sopenharmony_ci        { NULL }
53e1051a39Sopenharmony_ci    };
54e1051a39Sopenharmony_ci    return test_options;
55e1051a39Sopenharmony_ci}
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_cistatic int calculate_digest(const EVP_MD *md, const char *msg, size_t len,
58e1051a39Sopenharmony_ci                            const unsigned char *exptd)
59e1051a39Sopenharmony_ci{
60e1051a39Sopenharmony_ci    unsigned char out[SHA256_DIGEST_LENGTH];
61e1051a39Sopenharmony_ci    EVP_MD_CTX *ctx;
62e1051a39Sopenharmony_ci    int ret = 0;
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_MD_CTX_new())
65e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestInit_ex(ctx, md, NULL))
66e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestUpdate(ctx, msg, len))
67e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestFinal_ex(ctx, out, NULL))
68e1051a39Sopenharmony_ci            || !TEST_mem_eq(out, SHA256_DIGEST_LENGTH, exptd,
69e1051a39Sopenharmony_ci                            SHA256_DIGEST_LENGTH)
70e1051a39Sopenharmony_ci            || !TEST_true(md == EVP_MD_CTX_get0_md(ctx)))
71e1051a39Sopenharmony_ci        goto err;
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    ret = 1;
74e1051a39Sopenharmony_ci err:
75e1051a39Sopenharmony_ci    EVP_MD_CTX_free(ctx);
76e1051a39Sopenharmony_ci    return ret;
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_cistatic int load_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[])
80e1051a39Sopenharmony_ci{
81e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = NULL;
82e1051a39Sopenharmony_ci    int ret = 0;
83e1051a39Sopenharmony_ci    size_t i;
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    ctx = OSSL_LIB_CTX_new();
86e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx))
87e1051a39Sopenharmony_ci        goto err;
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, config_file)))
90e1051a39Sopenharmony_ci        goto err;
91e1051a39Sopenharmony_ci    if (test_get_argument_count() > 2)
92e1051a39Sopenharmony_ci        goto err;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci    for (i = 0; i < test_get_argument_count(); ++i) {
95e1051a39Sopenharmony_ci        char *provname = test_get_argument(i);
96e1051a39Sopenharmony_ci        prov[i] = OSSL_PROVIDER_load(ctx, provname);
97e1051a39Sopenharmony_ci        if (!TEST_ptr(prov[i]))
98e1051a39Sopenharmony_ci            goto err;
99e1051a39Sopenharmony_ci    }
100e1051a39Sopenharmony_ci
101e1051a39Sopenharmony_ci    ret = 1;
102e1051a39Sopenharmony_ci    *libctx = ctx;
103e1051a39Sopenharmony_cierr:
104e1051a39Sopenharmony_ci    if (ret == 0)
105e1051a39Sopenharmony_ci        OSSL_LIB_CTX_free(ctx);
106e1051a39Sopenharmony_ci    return ret;
107e1051a39Sopenharmony_ci}
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_cistatic void unload_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[])
110e1051a39Sopenharmony_ci{
111e1051a39Sopenharmony_ci    if (prov[0] != NULL)
112e1051a39Sopenharmony_ci        OSSL_PROVIDER_unload(prov[0]);
113e1051a39Sopenharmony_ci    if (prov[1] != NULL)
114e1051a39Sopenharmony_ci        OSSL_PROVIDER_unload(prov[1]);
115e1051a39Sopenharmony_ci    /* Not normally needed, but we would like to test that
116e1051a39Sopenharmony_ci     * OPENSSL_thread_stop_ex() behaves as expected.
117e1051a39Sopenharmony_ci     */
118e1051a39Sopenharmony_ci    if (libctx != NULL && *libctx != NULL) {
119e1051a39Sopenharmony_ci        OPENSSL_thread_stop_ex(*libctx);
120e1051a39Sopenharmony_ci        OSSL_LIB_CTX_free(*libctx);
121e1051a39Sopenharmony_ci    }
122e1051a39Sopenharmony_ci}
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_cistatic X509_ALGOR *make_algor(int nid)
125e1051a39Sopenharmony_ci{
126e1051a39Sopenharmony_ci    X509_ALGOR *algor;
127e1051a39Sopenharmony_ci
128e1051a39Sopenharmony_ci    if (!TEST_ptr(algor = X509_ALGOR_new())
129e1051a39Sopenharmony_ci        || !TEST_true(X509_ALGOR_set0(algor, OBJ_nid2obj(nid),
130e1051a39Sopenharmony_ci                                      V_ASN1_UNDEF, NULL))) {
131e1051a39Sopenharmony_ci        X509_ALGOR_free(algor);
132e1051a39Sopenharmony_ci        return NULL;
133e1051a39Sopenharmony_ci    }
134e1051a39Sopenharmony_ci    return algor;
135e1051a39Sopenharmony_ci}
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci/*
138e1051a39Sopenharmony_ci * Test EVP_MD_fetch()
139e1051a39Sopenharmony_ci */
140e1051a39Sopenharmony_cistatic int test_md(const EVP_MD *md)
141e1051a39Sopenharmony_ci{
142e1051a39Sopenharmony_ci    const char testmsg[] = "Hello world";
143e1051a39Sopenharmony_ci    const unsigned char exptd[] = {
144e1051a39Sopenharmony_ci      0x27, 0x51, 0x8b, 0xa9, 0x68, 0x30, 0x11, 0xf6, 0xb3, 0x96, 0x07, 0x2c,
145e1051a39Sopenharmony_ci      0x05, 0xf6, 0x65, 0x6d, 0x04, 0xf5, 0xfb, 0xc3, 0x78, 0x7c, 0xf9, 0x24,
146e1051a39Sopenharmony_ci      0x90, 0xec, 0x60, 0x6e, 0x50, 0x92, 0xe3, 0x26
147e1051a39Sopenharmony_ci    };
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci    return TEST_ptr(md)
150e1051a39Sopenharmony_ci        && TEST_true(EVP_MD_is_a(md, "SHA256"))
151e1051a39Sopenharmony_ci        && TEST_true(calculate_digest(md, testmsg, sizeof(testmsg), exptd))
152e1051a39Sopenharmony_ci        && TEST_int_eq(EVP_MD_get_size(md), SHA256_DIGEST_LENGTH)
153e1051a39Sopenharmony_ci        && TEST_int_eq(EVP_MD_get_block_size(md), SHA256_CBLOCK);
154e1051a39Sopenharmony_ci}
155e1051a39Sopenharmony_ci
156e1051a39Sopenharmony_cistatic int test_implicit_EVP_MD_fetch(void)
157e1051a39Sopenharmony_ci{
158e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = NULL;
159e1051a39Sopenharmony_ci    OSSL_PROVIDER *prov[2] = {NULL, NULL};
160e1051a39Sopenharmony_ci    int ret = 0;
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci    ret = (use_default_ctx == 0 || load_providers(&ctx, prov))
163e1051a39Sopenharmony_ci        && test_md(EVP_sha256());
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_ci    unload_providers(&ctx, prov);
166e1051a39Sopenharmony_ci    return ret;
167e1051a39Sopenharmony_ci}
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_cistatic int test_explicit_EVP_MD_fetch(const char *id)
170e1051a39Sopenharmony_ci{
171e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = NULL;
172e1051a39Sopenharmony_ci    EVP_MD *md = NULL;
173e1051a39Sopenharmony_ci    OSSL_PROVIDER *prov[2] = {NULL, NULL};
174e1051a39Sopenharmony_ci    int ret = 0;
175e1051a39Sopenharmony_ci
176e1051a39Sopenharmony_ci    if (use_default_ctx == 0 && !load_providers(&ctx, prov))
177e1051a39Sopenharmony_ci        goto err;
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci    md = EVP_MD_fetch(ctx, id, fetch_property);
180e1051a39Sopenharmony_ci    if (expected_fetch_result != 0) {
181e1051a39Sopenharmony_ci        if (!test_md(md))
182e1051a39Sopenharmony_ci            goto err;
183e1051a39Sopenharmony_ci
184e1051a39Sopenharmony_ci        /* Also test EVP_MD_up_ref() while we're doing this */
185e1051a39Sopenharmony_ci        if (!TEST_true(EVP_MD_up_ref(md)))
186e1051a39Sopenharmony_ci            goto err;
187e1051a39Sopenharmony_ci        /* Ref count should now be 2. Release first one here */
188e1051a39Sopenharmony_ci        EVP_MD_free(md);
189e1051a39Sopenharmony_ci    } else {
190e1051a39Sopenharmony_ci        if (!TEST_ptr_null(md))
191e1051a39Sopenharmony_ci            goto err;
192e1051a39Sopenharmony_ci    }
193e1051a39Sopenharmony_ci    ret = 1;
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci err:
196e1051a39Sopenharmony_ci    EVP_MD_free(md);
197e1051a39Sopenharmony_ci    unload_providers(&ctx, prov);
198e1051a39Sopenharmony_ci    return ret;
199e1051a39Sopenharmony_ci}
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_cistatic int test_explicit_EVP_MD_fetch_by_name(void)
202e1051a39Sopenharmony_ci{
203e1051a39Sopenharmony_ci    return test_explicit_EVP_MD_fetch("SHA256");
204e1051a39Sopenharmony_ci}
205e1051a39Sopenharmony_ci
206e1051a39Sopenharmony_ci/*
207e1051a39Sopenharmony_ci * idx 0: Allow names from OBJ_obj2txt()
208e1051a39Sopenharmony_ci * idx 1: Force an OID in text form from OBJ_obj2txt()
209e1051a39Sopenharmony_ci */
210e1051a39Sopenharmony_cistatic int test_explicit_EVP_MD_fetch_by_X509_ALGOR(int idx)
211e1051a39Sopenharmony_ci{
212e1051a39Sopenharmony_ci    int ret = 0;
213e1051a39Sopenharmony_ci    X509_ALGOR *algor = make_algor(NID_sha256);
214e1051a39Sopenharmony_ci    const ASN1_OBJECT *obj;
215e1051a39Sopenharmony_ci    char id[OSSL_MAX_NAME_SIZE];
216e1051a39Sopenharmony_ci
217e1051a39Sopenharmony_ci    if (algor == NULL)
218e1051a39Sopenharmony_ci        return 0;
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ci    X509_ALGOR_get0(&obj, NULL, NULL, algor);
221e1051a39Sopenharmony_ci    switch (idx) {
222e1051a39Sopenharmony_ci    case 0:
223e1051a39Sopenharmony_ci        if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0))
224e1051a39Sopenharmony_ci            goto end;
225e1051a39Sopenharmony_ci        break;
226e1051a39Sopenharmony_ci    case 1:
227e1051a39Sopenharmony_ci        if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0))
228e1051a39Sopenharmony_ci            goto end;
229e1051a39Sopenharmony_ci        break;
230e1051a39Sopenharmony_ci    }
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci    ret = test_explicit_EVP_MD_fetch(id);
233e1051a39Sopenharmony_ci end:
234e1051a39Sopenharmony_ci    X509_ALGOR_free(algor);
235e1051a39Sopenharmony_ci    return ret;
236e1051a39Sopenharmony_ci}
237e1051a39Sopenharmony_ci
238e1051a39Sopenharmony_ci/*
239e1051a39Sopenharmony_ci * Test EVP_CIPHER_fetch()
240e1051a39Sopenharmony_ci */
241e1051a39Sopenharmony_cistatic int encrypt_decrypt(const EVP_CIPHER *cipher, const unsigned char *msg,
242e1051a39Sopenharmony_ci                           size_t len)
243e1051a39Sopenharmony_ci{
244e1051a39Sopenharmony_ci    int ret = 0, ctlen, ptlen;
245e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
246e1051a39Sopenharmony_ci    unsigned char key[128 / 8];
247e1051a39Sopenharmony_ci    unsigned char ct[64], pt[64];
248e1051a39Sopenharmony_ci
249e1051a39Sopenharmony_ci    memset(key, 0, sizeof(key));
250e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
251e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 1))
252e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherUpdate(ctx, ct, &ctlen, msg, len))
253e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherFinal_ex(ctx, ct, &ctlen))
254e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 0))
255e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherUpdate(ctx, pt, &ptlen, ct, ctlen))
256e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherFinal_ex(ctx, pt, &ptlen))
257e1051a39Sopenharmony_ci            || !TEST_mem_eq(pt, ptlen, msg, len))
258e1051a39Sopenharmony_ci        goto err;
259e1051a39Sopenharmony_ci
260e1051a39Sopenharmony_ci    ret = 1;
261e1051a39Sopenharmony_cierr:
262e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
263e1051a39Sopenharmony_ci    return ret;
264e1051a39Sopenharmony_ci}
265e1051a39Sopenharmony_ci
266e1051a39Sopenharmony_cistatic int test_cipher(const EVP_CIPHER *cipher)
267e1051a39Sopenharmony_ci{
268e1051a39Sopenharmony_ci    const unsigned char testmsg[] = "Hello world";
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci    return TEST_ptr(cipher)
271e1051a39Sopenharmony_ci        && TEST_true(encrypt_decrypt(cipher, testmsg, sizeof(testmsg)));
272e1051a39Sopenharmony_ci}
273e1051a39Sopenharmony_ci
274e1051a39Sopenharmony_cistatic int test_implicit_EVP_CIPHER_fetch(void)
275e1051a39Sopenharmony_ci{
276e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = NULL;
277e1051a39Sopenharmony_ci    OSSL_PROVIDER *prov[2] = {NULL, NULL};
278e1051a39Sopenharmony_ci    int ret = 0;
279e1051a39Sopenharmony_ci
280e1051a39Sopenharmony_ci    ret = (use_default_ctx == 0 || load_providers(&ctx, prov))
281e1051a39Sopenharmony_ci        && test_cipher(EVP_aes_128_cbc());
282e1051a39Sopenharmony_ci
283e1051a39Sopenharmony_ci    unload_providers(&ctx, prov);
284e1051a39Sopenharmony_ci    return ret;
285e1051a39Sopenharmony_ci}
286e1051a39Sopenharmony_ci
287e1051a39Sopenharmony_cistatic int test_explicit_EVP_CIPHER_fetch(const char *id)
288e1051a39Sopenharmony_ci{
289e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = NULL;
290e1051a39Sopenharmony_ci    EVP_CIPHER *cipher = NULL;
291e1051a39Sopenharmony_ci    OSSL_PROVIDER *prov[2] = {NULL, NULL};
292e1051a39Sopenharmony_ci    int ret = 0;
293e1051a39Sopenharmony_ci
294e1051a39Sopenharmony_ci    if (use_default_ctx == 0 && !load_providers(&ctx, prov))
295e1051a39Sopenharmony_ci        goto err;
296e1051a39Sopenharmony_ci
297e1051a39Sopenharmony_ci    cipher = EVP_CIPHER_fetch(ctx, id, fetch_property);
298e1051a39Sopenharmony_ci    if (expected_fetch_result != 0) {
299e1051a39Sopenharmony_ci        if (!test_cipher(cipher))
300e1051a39Sopenharmony_ci            goto err;
301e1051a39Sopenharmony_ci
302e1051a39Sopenharmony_ci        if (!TEST_true(EVP_CIPHER_up_ref(cipher)))
303e1051a39Sopenharmony_ci            goto err;
304e1051a39Sopenharmony_ci        /* Ref count should now be 2. Release first one here */
305e1051a39Sopenharmony_ci        EVP_CIPHER_free(cipher);
306e1051a39Sopenharmony_ci    } else {
307e1051a39Sopenharmony_ci        if (!TEST_ptr_null(cipher))
308e1051a39Sopenharmony_ci            goto err;
309e1051a39Sopenharmony_ci    }
310e1051a39Sopenharmony_ci    ret = 1;
311e1051a39Sopenharmony_cierr:
312e1051a39Sopenharmony_ci    EVP_CIPHER_free(cipher);
313e1051a39Sopenharmony_ci    unload_providers(&ctx, prov);
314e1051a39Sopenharmony_ci    return ret;
315e1051a39Sopenharmony_ci}
316e1051a39Sopenharmony_ci
317e1051a39Sopenharmony_cistatic int test_explicit_EVP_CIPHER_fetch_by_name(void)
318e1051a39Sopenharmony_ci{
319e1051a39Sopenharmony_ci    return test_explicit_EVP_CIPHER_fetch("AES-128-CBC");
320e1051a39Sopenharmony_ci}
321e1051a39Sopenharmony_ci
322e1051a39Sopenharmony_ci/*
323e1051a39Sopenharmony_ci * idx 0: Allow names from OBJ_obj2txt()
324e1051a39Sopenharmony_ci * idx 1: Force an OID in text form from OBJ_obj2txt()
325e1051a39Sopenharmony_ci */
326e1051a39Sopenharmony_cistatic int test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR(int idx)
327e1051a39Sopenharmony_ci{
328e1051a39Sopenharmony_ci    int ret = 0;
329e1051a39Sopenharmony_ci    X509_ALGOR *algor = make_algor(NID_aes_128_cbc);
330e1051a39Sopenharmony_ci    const ASN1_OBJECT *obj;
331e1051a39Sopenharmony_ci    char id[OSSL_MAX_NAME_SIZE];
332e1051a39Sopenharmony_ci
333e1051a39Sopenharmony_ci    if (algor == NULL)
334e1051a39Sopenharmony_ci        return 0;
335e1051a39Sopenharmony_ci
336e1051a39Sopenharmony_ci    X509_ALGOR_get0(&obj, NULL, NULL, algor);
337e1051a39Sopenharmony_ci    switch (idx) {
338e1051a39Sopenharmony_ci    case 0:
339e1051a39Sopenharmony_ci        if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0))
340e1051a39Sopenharmony_ci            goto end;
341e1051a39Sopenharmony_ci        break;
342e1051a39Sopenharmony_ci    case 1:
343e1051a39Sopenharmony_ci        if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0))
344e1051a39Sopenharmony_ci            goto end;
345e1051a39Sopenharmony_ci        break;
346e1051a39Sopenharmony_ci    }
347e1051a39Sopenharmony_ci
348e1051a39Sopenharmony_ci    ret = test_explicit_EVP_CIPHER_fetch(id);
349e1051a39Sopenharmony_ci end:
350e1051a39Sopenharmony_ci    X509_ALGOR_free(algor);
351e1051a39Sopenharmony_ci    return ret;
352e1051a39Sopenharmony_ci}
353e1051a39Sopenharmony_ci
354e1051a39Sopenharmony_ciint setup_tests(void)
355e1051a39Sopenharmony_ci{
356e1051a39Sopenharmony_ci    OPTION_CHOICE o;
357e1051a39Sopenharmony_ci
358e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
359e1051a39Sopenharmony_ci        switch (o) {
360e1051a39Sopenharmony_ci        case OPT_CONFIG_FILE:
361e1051a39Sopenharmony_ci            config_file = opt_arg();
362e1051a39Sopenharmony_ci            break;
363e1051a39Sopenharmony_ci        case OPT_ALG_FETCH_TYPE:
364e1051a39Sopenharmony_ci            alg = opt_arg();
365e1051a39Sopenharmony_ci            break;
366e1051a39Sopenharmony_ci        case OPT_FETCH_PROPERTY:
367e1051a39Sopenharmony_ci            fetch_property = opt_arg();
368e1051a39Sopenharmony_ci            break;
369e1051a39Sopenharmony_ci        case OPT_FETCH_FAILURE:
370e1051a39Sopenharmony_ci            expected_fetch_result = 0;
371e1051a39Sopenharmony_ci            break;
372e1051a39Sopenharmony_ci        case OPT_USE_DEFAULTCTX:
373e1051a39Sopenharmony_ci            use_default_ctx = 1;
374e1051a39Sopenharmony_ci            break;
375e1051a39Sopenharmony_ci        case OPT_TEST_CASES:
376e1051a39Sopenharmony_ci           break;
377e1051a39Sopenharmony_ci        default:
378e1051a39Sopenharmony_ci        case OPT_ERR:
379e1051a39Sopenharmony_ci            return 0;
380e1051a39Sopenharmony_ci        }
381e1051a39Sopenharmony_ci    }
382e1051a39Sopenharmony_ci    if (strcmp(alg, "digest") == 0) {
383e1051a39Sopenharmony_ci        ADD_TEST(test_implicit_EVP_MD_fetch);
384e1051a39Sopenharmony_ci        ADD_TEST(test_explicit_EVP_MD_fetch_by_name);
385e1051a39Sopenharmony_ci        ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_MD_fetch_by_X509_ALGOR, 2);
386e1051a39Sopenharmony_ci    } else {
387e1051a39Sopenharmony_ci        ADD_TEST(test_implicit_EVP_CIPHER_fetch);
388e1051a39Sopenharmony_ci        ADD_TEST(test_explicit_EVP_CIPHER_fetch_by_name);
389e1051a39Sopenharmony_ci        ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR, 2);
390e1051a39Sopenharmony_ci    }
391e1051a39Sopenharmony_ci    return 1;
392e1051a39Sopenharmony_ci}
393