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