file_path
stringlengths
19
75
code
stringlengths
279
1.37M
./openssl/test/asn1_string_table_test.c
/* * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Tests for the ASN1_STRING_TABLE_* functions */ #include <stdio.h> #include <string.h> #include <openssl/asn1.h> #include "testutil.h" static int test_string_tbl(void) { const ASN1_STRING_TABLE *tmp = NULL; int nid = 12345678, nid2 = 87654321, rv = 0, ret = 0; tmp = ASN1_STRING_TABLE_get(nid); if (!TEST_ptr_null(tmp)) { TEST_info("asn1 string table: ASN1_STRING_TABLE_get non-exist nid"); goto out; } ret = ASN1_STRING_TABLE_add(nid, -1, -1, MBSTRING_ASC, 0); if (!TEST_true(ret)) { TEST_info("asn1 string table: add NID(%d) failed", nid); goto out; } ret = ASN1_STRING_TABLE_add(nid2, -1, -1, MBSTRING_ASC, 0); if (!TEST_true(ret)) { TEST_info("asn1 string table: add NID(%d) failed", nid2); goto out; } tmp = ASN1_STRING_TABLE_get(nid); if (!TEST_ptr(tmp)) { TEST_info("asn1 string table: get NID(%d) failed", nid); goto out; } tmp = ASN1_STRING_TABLE_get(nid2); if (!TEST_ptr(tmp)) { TEST_info("asn1 string table: get NID(%d) failed", nid2); goto out; } ASN1_STRING_TABLE_cleanup(); /* check if all newly added NIDs are cleaned up */ tmp = ASN1_STRING_TABLE_get(nid); if (!TEST_ptr_null(tmp)) { TEST_info("asn1 string table: get NID(%d) failed", nid); goto out; } tmp = ASN1_STRING_TABLE_get(nid2); if (!TEST_ptr_null(tmp)) { TEST_info("asn1 string table: get NID(%d) failed", nid2); goto out; } rv = 1; out: return rv; } int setup_tests(void) { ADD_TEST(test_string_tbl); return 1; }
./openssl/test/provfetchtest.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/crypto.h> #include <openssl/provider.h> #include <openssl/decoder.h> #include <openssl/encoder.h> #include <openssl/store.h> #include <openssl/rand.h> #include <openssl/core_names.h> #include "testutil.h" static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection, OSSL_CALLBACK *object_cb, void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { return 0; } static const OSSL_DISPATCH dummy_decoder_functions[] = { { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_decoders[] = { { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions }, { NULL, NULL, NULL } }; static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out, const void *obj_raw, const OSSL_PARAM obj_abstract[], int selection, OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) { return 0; } static const OSSL_DISPATCH dummy_encoder_functions[] = { { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_encoders[] = { { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions }, { NULL, NULL, NULL } }; static void *dummy_store_open(void *provctx, const char *uri) { return NULL; } static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb, void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { return 0; } static int dumm_store_eof(void *loaderctx) { return 0; } static int dummy_store_close(void *loaderctx) { return 0; } static const OSSL_DISPATCH dummy_store_functions[] = { { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open }, { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load }, { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof }, { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_store[] = { { "DUMMY", "provider=dummy", dummy_store_functions }, { NULL, NULL, NULL } }; static void *dummy_rand_newctx(void *provctx, void *parent, const OSSL_DISPATCH *parent_calls) { return provctx; } static void dummy_rand_freectx(void *vctx) { } static int dummy_rand_instantiate(void *vdrbg, unsigned int strength, int prediction_resistance, const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[]) { return 1; } static int dummy_rand_uninstantiate(void *vdrbg) { return 1; } static int dummy_rand_generate(void *vctx, unsigned char *out, size_t outlen, unsigned int strength, int prediction_resistance, const unsigned char *addin, size_t addin_len) { size_t i; for (i = 0; i <outlen; i++) out[i] = (unsigned char)(i & 0xff); return 1; } static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx) { static const OSSL_PARAM known_gettable_ctx_params[] = { OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), OSSL_PARAM_END }; return known_gettable_ctx_params; } static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[]) { OSSL_PARAM *p; p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX)) return 0; return 1; } static int dummy_rand_enable_locking(void *vtest) { return 1; } static int dummy_rand_lock(void *vtest) { return 1; } static void dummy_rand_unlock(void *vtest) { } static const OSSL_DISPATCH dummy_rand_functions[] = { { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))dummy_rand_newctx }, { OSSL_FUNC_RAND_FREECTX, (void (*)(void))dummy_rand_freectx }, { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))dummy_rand_instantiate }, { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))dummy_rand_uninstantiate }, { OSSL_FUNC_RAND_GENERATE, (void (*)(void))dummy_rand_generate }, { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, (void(*)(void))dummy_rand_gettable_ctx_params }, { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))dummy_rand_get_ctx_params }, { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))dummy_rand_enable_locking }, { OSSL_FUNC_RAND_LOCK, (void(*)(void))dummy_rand_lock }, { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))dummy_rand_unlock }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_rand[] = { { "DUMMY", "provider=dummy", dummy_rand_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; switch (operation_id) { case OSSL_OP_DECODER: return dummy_decoders; case OSSL_OP_ENCODER: return dummy_encoders; case OSSL_OP_STORE: return dummy_store; case OSSL_OP_RAND: return dummy_rand; } return NULL; } static const OSSL_DISPATCH dummy_dispatch_table[] = { { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query }, { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, OSSL_DISPATCH_END }; static int dummy_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in); unsigned char buf[32]; *provctx = (void *)libctx; *out = dummy_dispatch_table; /* * Do some work using the child libctx, to make sure this is possible from * inside the init function. */ if (RAND_bytes_ex(libctx, buf, sizeof(buf), 0) <= 0) return 0; return 1; } /* * Try fetching and freeing various things. * Test 0: Decoder * Test 1: Encoder * Test 2: Store loader * Test 3: EVP_RAND * Test 4-7: As above, but additionally with a query string */ static int fetch_test(int tst) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); OSSL_PROVIDER *dummyprov = NULL; OSSL_PROVIDER *nullprov = NULL; OSSL_DECODER *decoder = NULL; OSSL_ENCODER *encoder = NULL; OSSL_STORE_LOADER *loader = NULL; int testresult = 0; unsigned char buf[32]; int query = tst > 3; if (!TEST_ptr(libctx)) goto err; if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov", dummy_provider_init)) || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default")) || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov"))) goto err; switch (tst % 4) { case 0: decoder = OSSL_DECODER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(decoder)) goto err; break; case 1: encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(encoder)) goto err; break; case 2: loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(loader)) goto err; break; case 3: if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY", query ? "provider=dummy" : NULL, NULL, NULL)) || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1)) goto err; break; default: goto err; } testresult = 1; err: OSSL_DECODER_free(decoder); OSSL_ENCODER_free(encoder); OSSL_STORE_LOADER_free(loader); OSSL_PROVIDER_unload(dummyprov); OSSL_PROVIDER_unload(nullprov); OSSL_LIB_CTX_free(libctx); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(fetch_test, 8); return 1; }
./openssl/test/hmactest.c
/* * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * HMAC low level APIs are deprecated for public use, but still ok for internal * use. */ #include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include "internal/nelem.h" # include <openssl/hmac.h> # include <openssl/sha.h> # ifndef OPENSSL_NO_MD5 # include <openssl/md5.h> # endif # ifdef CHARSET_EBCDIC # include <openssl/ebcdic.h> # endif #include "testutil.h" # ifndef OPENSSL_NO_MD5 static struct test_st { const char key[16]; int key_len; const unsigned char data[64]; int data_len; const char *digest; } test[8] = { { "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54, "e9139d1e6ee064ef8cf514fc7dc83e86", }, { "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16, "Hi There", 8, "9294727a3638bb1c13f48ef8158bfc9d", }, { "Jefe", 4, "what do ya want for nothing?", 28, "750c783e6ab0b503eaa86e310a5db738", }, { "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 16, { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd }, 50, "56be34521d144c88dbb8c733f0e8b3f6", }, { "", 0, "My test data", 12, "61afdecb95429ef494d61fdee15990cabf0826fc" }, { "", 0, "My test data", 12, "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776" }, { "123456", 6, "My test data", 12, "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd" }, { "12345", 5, "My test data again", 18, "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e" } }; # endif static char *pt(unsigned char *md, unsigned int len); # ifndef OPENSSL_NO_MD5 static int test_hmac_md5(int idx) { char *p; # ifdef CHARSET_EBCDIC ebcdic2ascii(test[0].data, test[0].data, test[0].data_len); ebcdic2ascii(test[1].data, test[1].data, test[1].data_len); ebcdic2ascii(test[2].key, test[2].key, test[2].key_len); ebcdic2ascii(test[2].data, test[2].data, test[2].data_len); # endif p = pt(HMAC(EVP_md5(), test[idx].key, test[idx].key_len, test[idx].data, test[idx].data_len, NULL, NULL), MD5_DIGEST_LENGTH); return TEST_ptr(p) && TEST_str_eq(p, test[idx].digest); } # endif static int test_hmac_bad(void) { HMAC_CTX *ctx = NULL; int ret = 0; ctx = HMAC_CTX_new(); if (!TEST_ptr(ctx) || !TEST_ptr_null(HMAC_CTX_get_md(ctx)) || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)) || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL)) || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))) goto err; ret = 1; err: HMAC_CTX_free(ctx); return ret; } static int test_hmac_run(void) { char *p; HMAC_CTX *ctx = NULL; unsigned char buf[EVP_MAX_MD_SIZE]; unsigned int len; int ret = 0; if (!TEST_ptr(ctx = HMAC_CTX_new())) return 0; HMAC_CTX_reset(ctx); if (!TEST_ptr(ctx) || !TEST_ptr_null(HMAC_CTX_get_md(ctx)) || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)) || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL))) goto err; if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL)) || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len)) || !TEST_true(HMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest)) goto err; if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))) goto err; if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256()) || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len)) || !TEST_true(HMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[5].digest)) goto err; if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL)) || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len)) || !TEST_true(HMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) goto err; /* Test reusing a key */ if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len)) || !TEST_true(HMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) goto err; /* * Test reusing a key where the digest is provided again but is the same as * last time */ if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)) || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len)) || !TEST_true(HMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) goto err; ret = 1; err: HMAC_CTX_free(ctx); return ret; } static int test_hmac_single_shot(void) { char *p; /* Test single-shot with NULL key. */ p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len, NULL, NULL), SHA_DIGEST_LENGTH); if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest)) return 0; return 1; } static int test_hmac_copy(void) { char *p; HMAC_CTX *ctx = NULL, *ctx2 = NULL; unsigned char buf[EVP_MAX_MD_SIZE]; unsigned int len; int ret = 0; ctx = HMAC_CTX_new(); ctx2 = HMAC_CTX_new(); if (!TEST_ptr(ctx) || !TEST_ptr(ctx2)) goto err; if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL)) || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len)) || !TEST_true(HMAC_CTX_copy(ctx2, ctx)) || !TEST_true(HMAC_Final(ctx2, buf, &len))) goto err; p = pt(buf, len); if (!TEST_ptr(p) || !TEST_str_eq(p, test[7].digest)) goto err; ret = 1; err: HMAC_CTX_free(ctx2); HMAC_CTX_free(ctx); return ret; } static int test_hmac_copy_uninited(void) { const unsigned char key[24] = {0}; const unsigned char ct[166] = {0}; EVP_PKEY *pkey = NULL; EVP_MD_CTX *ctx = NULL; EVP_MD_CTX *ctx_tmp = NULL; int res = 0; if (!TEST_ptr(ctx = EVP_MD_CTX_new()) || !TEST_ptr(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, sizeof(key))) || !TEST_true(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey)) || !TEST_ptr(ctx_tmp = EVP_MD_CTX_new()) || !TEST_true(EVP_MD_CTX_copy(ctx_tmp, ctx))) goto err; EVP_MD_CTX_free(ctx); ctx = ctx_tmp; ctx_tmp = NULL; if (!TEST_true(EVP_DigestSignUpdate(ctx, ct, sizeof(ct)))) goto err; res = 1; err: EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx_tmp); EVP_PKEY_free(pkey); return res; } # ifndef OPENSSL_NO_MD5 static char *pt(unsigned char *md, unsigned int len) { unsigned int i; static char buf[80]; if (md == NULL) return NULL; for (i = 0; i < len; i++) sprintf(&(buf[i * 2]), "%02x", md[i]); return buf; } # endif int setup_tests(void) { ADD_ALL_TESTS(test_hmac_md5, 4); ADD_TEST(test_hmac_single_shot); ADD_TEST(test_hmac_bad); ADD_TEST(test_hmac_run); ADD_TEST(test_hmac_copy); ADD_TEST(test_hmac_copy_uninited); return 1; }
./openssl/test/rc4test.c
/* * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * RC4 and SHA-1 low level APIs are deprecated for public use, but still ok for * internal use. */ #include "internal/deprecated.h" #include <string.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_RC4 # include <openssl/rc4.h> # include <openssl/sha.h> static unsigned char keys[6][30] = { {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, {8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {4, 0xef, 0x01, 0x23, 0x45}, {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, {4, 0xef, 0x01, 0x23, 0x45}, }; static unsigned char data_len[6] = { 8, 8, 8, 20, 28, 10 }; static unsigned char data[6][30] = { {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0xff}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, }; static unsigned char output[6][30] = { {0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00}, {0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00}, {0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00}, {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba, 0x36, 0xb6, 0x78, 0x58, 0x00}, {0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89, 0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c, 0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87, 0x40, 0x01, 0x1e, 0xcf, 0x00}, {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x00}, }; static int test_rc4_encrypt(const int i) { unsigned char obuf[512]; RC4_KEY key; RC4_set_key(&key, keys[i][0], &(keys[i][1])); memset(obuf, 0, sizeof(obuf)); RC4(&key, data_len[i], &(data[i][0]), obuf); return TEST_mem_eq(obuf, data_len[i] + 1, output[i], data_len[i] + 1); } static int test_rc4_end_processing(const int i) { unsigned char obuf[512]; RC4_KEY key; RC4_set_key(&key, keys[3][0], &(keys[3][1])); memset(obuf, 0, sizeof(obuf)); RC4(&key, i, &(data[3][0]), obuf); if (!TEST_mem_eq(obuf, i, output[3], i)) return 0; return TEST_uchar_eq(obuf[i], 0); } static int test_rc4_multi_call(const int i) { unsigned char obuf[512]; RC4_KEY key; RC4_set_key(&key, keys[3][0], &(keys[3][1])); memset(obuf, 0, sizeof(obuf)); RC4(&key, i, &(data[3][0]), obuf); RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i])); return TEST_mem_eq(obuf, data_len[3] + 1, output[3], data_len[3] + 1); } static int test_rc_bulk(void) { RC4_KEY key; unsigned char buf[513]; SHA_CTX c; unsigned char md[SHA_DIGEST_LENGTH]; int i; static unsigned char expected[] = { 0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f, 0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5 }; RC4_set_key(&key, keys[0][0], &(keys[3][1])); memset(buf, 0, sizeof(buf)); SHA1_Init(&c); for (i = 0; i < 2571; i++) { RC4(&key, sizeof(buf), buf, buf); SHA1_Update(&c, buf, sizeof(buf)); } SHA1_Final(md, &c); return TEST_mem_eq(md, sizeof(md), expected, sizeof(expected)); } #endif int setup_tests(void) { #ifndef OPENSSL_NO_RC4 ADD_ALL_TESTS(test_rc4_encrypt, OSSL_NELEM(data_len)); ADD_ALL_TESTS(test_rc4_end_processing, data_len[3]); ADD_ALL_TESTS(test_rc4_multi_call, data_len[3]); ADD_TEST(test_rc_bulk); #endif return 1; }
./openssl/test/fatalerrtest.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/ssl.h> #include <openssl/err.h> #include "helpers/ssltestlib.h" #include "testutil.h" #include <string.h> static char *cert = NULL; static char *privkey = NULL; static int test_fatalerr(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *sssl = NULL, *cssl = NULL; const char *msg = "Dummy"; BIO *wbio = NULL; int ret = 0, len; char buf[80]; unsigned char dummyrec[] = { 0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y' }; if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_method(), TLS_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto err; /* * Deliberately set the cipher lists for client and server to be different * to force a handshake failure. */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA")) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, "TLS_AES_128_GCM_SHA256")) || !TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384")) || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL))) goto err; wbio = SSL_get_wbio(cssl); if (!TEST_ptr(wbio)) { printf("Unexpected NULL bio received\n"); goto err; } /* Connection should fail */ if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE))) goto err; ERR_clear_error(); /* Inject a plaintext record from client to server */ if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0)) goto err; /* SSL_read()/SSL_write should fail because of a previous fatal error */ if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) { buf[len] = '\0'; TEST_error("Unexpected success reading data: %s\n", buf); goto err; } if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0)) goto err; ret = 1; err: SSL_free(sssl); SSL_free(cssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return ret; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(cert = test_get_argument(0)) || !TEST_ptr(privkey = test_get_argument(1))) return 0; ADD_TEST(test_fatalerr); return 1; }
./openssl/test/fips_version_test.c
/* * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/evp.h> #include <openssl/provider.h> #include "testutil.h" static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *libprov = NULL; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { NULL } }; return test_options; } static int test_fips_version(int n) { const char *version = test_get_argument(n); if (!TEST_ptr(version)) return 0; return TEST_int_eq(fips_provider_version_match(libctx, version), 1); } int setup_tests(void) { char *config_file = NULL; OPTION_CHOICE o; int n; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } if (!test_get_libctx(&libctx, NULL, config_file, &libprov, NULL)) return 0; n = test_get_argument_count(); if (n == 0) return 0; ADD_ALL_TESTS(test_fips_version, n); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(libprov); OSSL_LIB_CTX_free(libctx); }
./openssl/test/x509_load_cert_file_test.c
/* * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <openssl/err.h> #include <openssl/x509_vfy.h> #include "testutil.h" static const char *chain; static int test_load_cert_file(void) { int ret = 0, i; X509_STORE *store = NULL; X509_LOOKUP *lookup = NULL; STACK_OF(X509) *certs = NULL; STACK_OF(X509_OBJECT) *objs = NULL; if (!TEST_ptr(store = X509_STORE_new()) || !TEST_ptr(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file())) || !TEST_true(X509_load_cert_file(lookup, chain, X509_FILETYPE_PEM)) || !TEST_ptr(certs = X509_STORE_get1_all_certs(store)) || !TEST_int_eq(sk_X509_num(certs), 4) || !TEST_ptr(objs = X509_STORE_get1_objects(store)) || !TEST_int_eq(sk_X509_OBJECT_num(objs), 4)) goto err; for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { const X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i); if (!TEST_int_eq(X509_OBJECT_get_type(obj), X509_LU_X509)) goto err; } ret = 1; err: OSSL_STACK_OF_X509_free(certs); sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free); X509_STORE_free(store); return ret; } OPT_TEST_DECLARE_USAGE("cert.pem...\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } chain = test_get_argument(0); if (chain == NULL) return 0; ADD_TEST(test_load_cert_file); return 1; }
./openssl/test/bio_memleak_test.c
/* * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <openssl/buffer.h> #include <openssl/bio.h> #include "testutil.h" static int test_bio_memleak(void) { int ok = 0; BIO *bio; BUF_MEM bufmem; static const char str[] = "BIO test\n"; char buf[100]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; bufmem.length = sizeof(str); bufmem.data = (char *) str; bufmem.max = bufmem.length; BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE); BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str))) goto finish; if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str))) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_get_mem(void) { int ok = 0; BIO *bio = NULL; BUF_MEM *bufmem = NULL; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_get_mem_ptr(bio, &bufmem); if (!TEST_ptr(bufmem)) goto finish; if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0)) goto finish; BIO_free(bio); bio = NULL; if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12)) goto finish; ok = 1; finish: BIO_free(bio); BUF_MEM_free(bufmem); return ok; } static int test_bio_new_mem_buf(void) { int ok = 0; BIO *bio; BUF_MEM *bufmem; char data[16]; bio = BIO_new_mem_buf("Hello World\n", 12); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 5), 5)) goto finish; if (!TEST_mem_eq(data, 5, "Hello", 5)) goto finish; if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0)) goto finish; if (!TEST_int_lt(BIO_write(bio, "test", 4), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_rdonly_mem_buf(void) { int ok = 0; BIO *bio, *bio2 = NULL; BUF_MEM *bufmem; char data[16]; bio = BIO_new_mem_buf("Hello World\n", 12); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 5), 5)) goto finish; if (!TEST_mem_eq(data, 5, "Hello", 5)) goto finish; if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0)) goto finish; (void)BIO_set_close(bio, BIO_NOCLOSE); bio2 = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio2)) goto finish; BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE); BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio2, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; if (!TEST_int_gt(BIO_reset(bio2), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio2, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; ok = 1; finish: BIO_free(bio); BIO_free(bio2); return ok; } static int test_bio_rdwr_rdonly(void) { int ok = 0; BIO *bio = NULL; char data[16]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 16)) goto finish; if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_nonclear_rst(void) { int ok = 0; BIO *bio = NULL; char data[16]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST); if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST); if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_lt(BIO_read(bio, data, 16), 1)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int error_callback_fired; static long BIO_error_callback(BIO *bio, int cmd, const char *argp, size_t len, int argi, long argl, int ret, size_t *processed) { if ((cmd & (BIO_CB_READ | BIO_CB_RETURN)) != 0) { error_callback_fired = 1; ret = 0; /* fail for read operations to simulate error in input BIO */ } return ret; } /* Checks i2d_ASN1_bio_stream() is freeing all memory when input BIO ends unexpectedly. */ static int test_bio_i2d_ASN1_mime(void) { int ok = 0; BIO *bio = NULL, *out = NULL; BUF_MEM bufmem; static const char str[] = "BIO mime test\n"; PKCS7 *p7 = NULL; if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto finish; bufmem.length = sizeof(str); bufmem.data = (char *) str; bufmem.max = bufmem.length; BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE); BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); BIO_set_callback_ex(bio, BIO_error_callback); if (!TEST_ptr(out = BIO_new(BIO_s_mem()))) goto finish; if (!TEST_ptr(p7 = PKCS7_new())) goto finish; if (!TEST_true(PKCS7_set_type(p7, NID_pkcs7_data))) goto finish; error_callback_fired = 0; if (!TEST_false(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio, SMIME_STREAM | SMIME_BINARY, ASN1_ITEM_rptr(PKCS7)))) goto finish; if (!TEST_int_eq(error_callback_fired, 1)) goto finish; ok = 1; finish: BIO_free(bio); BIO_free(out); PKCS7_free(p7); return ok; } int setup_tests(void) { ADD_TEST(test_bio_memleak); ADD_TEST(test_bio_get_mem); ADD_TEST(test_bio_new_mem_buf); ADD_TEST(test_bio_rdonly_mem_buf); ADD_TEST(test_bio_rdwr_rdonly); ADD_TEST(test_bio_nonclear_rst); ADD_TEST(test_bio_i2d_ASN1_mime); return 1; }
./openssl/test/quic_newcid_test.c
/* * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/ssl.h> #include "helpers/quictestlib.h" #include "internal/quic_error.h" #include "testutil.h" static char *cert = NULL; static char *privkey = NULL; /* * Inject NEW_CONNECTION_ID frame */ static size_t ncid_injected; static int add_ncid_frame_cb(QTEST_FAULT *fault, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len, void *cbarg) { /* * We inject NEW_CONNECTION_ID frame to trigger change of the DCID. * The connection id length must be 8, otherwise the tserver won't be * able to receive packets with this new id. */ static unsigned char new_conn_id_frame[] = { 0x18, /* Type */ 0x01, /* Sequence Number */ 0x01, /* Retire Prior To */ 0x08, /* Connection ID Length */ 0x33, 0x44, 0x55, 0x66, 0xde, 0xad, 0xbe, 0xef, /* Connection ID */ 0xab, 0xcd, 0xef, 0x01, 0x12, 0x32, 0x23, 0x45, /* Stateless Reset Token */ 0x56, 0x06, 0x08, 0x89, 0xa1, 0xb2, 0xc3, 0xd4 }; /* We only ever add the unknown frame to one packet */ if (ncid_injected++) return 1; return qtest_fault_prepend_frame(fault, new_conn_id_frame, sizeof(new_conn_id_frame)); } static int test_ncid_frame(int fail) { int testresult = 0; SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method()); QUIC_TSERVER *qtserv = NULL; SSL *cssl = NULL; char *msg = "Hello World!"; size_t msglen = strlen(msg); unsigned char buf[80]; size_t byteswritten; size_t bytesread; QTEST_FAULT *fault = NULL; static const QUIC_CONN_ID conn_id = { 0x08, {0x33, 0x44, 0x55, 0x66, 0xde, 0xad, 0xbe, 0xef} }; ncid_injected = 0; if (!TEST_ptr(cctx)) goto err; if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, 0, &qtserv, &cssl, &fault, NULL))) goto err; if (!TEST_true(qtest_create_quic_connection(qtserv, cssl))) goto err; if (!TEST_int_eq(SSL_write(cssl, msg, msglen), msglen)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf), &bytesread))) goto err; /* * We assume the entire message is read from the server in one go. In * theory this could get fragmented but its a small message so we assume * not. */ if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; /* * Write a message from the server to the client and add * a NEW_CONNECTION_ID frame. */ if (!TEST_true(qtest_fault_set_packet_plain_listener(fault, add_ncid_frame_cb, NULL))) goto err; if (!fail && !TEST_true(ossl_quic_tserver_set_new_local_cid(qtserv, &conn_id))) goto err; if (!TEST_true(ossl_quic_tserver_write(qtserv, 0, (unsigned char *)msg, msglen, &byteswritten))) goto err; if (!TEST_true(ncid_injected)) goto err; if (!TEST_size_t_eq(msglen, byteswritten)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(SSL_handle_events(cssl))) goto err; if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), msglen)) goto err; if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; if (!TEST_int_eq(SSL_write(cssl, msg, msglen), msglen)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf), &bytesread))) goto err; if (fail) { if (!TEST_size_t_eq(bytesread, 0)) goto err; } else { if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; } testresult = 1; err: qtest_fault_free(fault); SSL_free(cssl); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } OPT_TEST_DECLARE_USAGE("certsdir\n") int setup_tests(void) { char *certsdir = NULL; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certsdir = test_get_argument(0))) return 0; cert = test_mk_file_path(certsdir, "servercert.pem"); if (cert == NULL) goto err; privkey = test_mk_file_path(certsdir, "serverkey.pem"); if (privkey == NULL) goto err; ADD_ALL_TESTS(test_ncid_frame, 2); return 1; err: OPENSSL_free(cert); OPENSSL_free(privkey); return 0; } void cleanup_tests(void) { OPENSSL_free(cert); OPENSSL_free(privkey); }
./openssl/test/rsa_mp_test.c
/* * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright 2017 BaishanCloud. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* This aims to test the setting functions, including internal ones */ /* * RSA low level APIs are deprecated for public use, but still ok for * internal use. */ #include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/bn.h> #include "testutil.h" #include <openssl/rsa.h> #include "crypto/rsa.h" #define NUM_EXTRA_PRIMES 1 DEFINE_STACK_OF(BIGNUM) /* C90 requires string should <= 509 bytes */ static const unsigned char n[] = "\x92\x60\xd0\x75\x0a\xe1\x17\xee\xe5\x5c\x3f\x3d\xea\xba\x74\x91" "\x75\x21\xa2\x62\xee\x76\x00\x7c\xdf\x8a\x56\x75\x5a\xd7\x3a\x15" "\x98\xa1\x40\x84\x10\xa0\x14\x34\xc3\xf5\xbc\x54\xa8\x8b\x57\xfa" "\x19\xfc\x43\x28\xda\xea\x07\x50\xa4\xc4\x4e\x88\xcf\xf3\xb2\x38" "\x26\x21\xb8\x0f\x67\x04\x64\x43\x3e\x43\x36\xe6\xd0\x03\xe8\xcd" "\x65\xbf\xf2\x11\xda\x14\x4b\x88\x29\x1c\x22\x59\xa0\x0a\x72\xb7" "\x11\xc1\x16\xef\x76\x86\xe8\xfe\xe3\x4e\x4d\x93\x3c\x86\x81\x87" "\xbd\xc2\x6f\x7b\xe0\x71\x49\x3c\x86\xf7\xa5\x94\x1c\x35\x10\x80" "\x6a\xd6\x7b\x0f\x94\xd8\x8f\x5c\xf5\xc0\x2a\x09\x28\x21\xd8\x62" "\x6e\x89\x32\xb6\x5c\x5b\xd8\xc9\x20\x49\xc2\x10\x93\x2b\x7a\xfa" "\x7a\xc5\x9c\x0e\x88\x6a\xe5\xc1\xed\xb0\x0d\x8c\xe2\xc5\x76\x33" "\xdb\x26\xbd\x66\x39\xbf\xf7\x3c\xee\x82\xbe\x92\x75\xc4\x02\xb4" "\xcf\x2a\x43\x88\xda\x8c\xf8\xc6\x4e\xef\xe1\xc5\xa0\xf5\xab\x80" "\x57\xc3\x9f\xa5\xc0\x58\x9c\x3e\x25\x3f\x09\x60\x33\x23\x00\xf9" "\x4b\xea\x44\x87\x7b\x58\x8e\x1e\xdb\xde\x97\xcf\x23\x60\x72\x7a" "\x09\xb7\x75\x26\x2d\x7e\xe5\x52\xb3\x31\x9b\x92\x66\xf0\x5a\x25"; static const unsigned char e[] = "\x01\x00\x01"; static const unsigned char d[] = "\x6a\x7d\xf2\xca\x63\xea\xd4\xdd\xa1\x91\xd6\x14\xb6\xb3\x85\xe0" "\xd9\x05\x6a\x3d\x6d\x5c\xfe\x07\xdb\x1d\xaa\xbe\xe0\x22\xdb\x08" "\x21\x2d\x97\x61\x3d\x33\x28\xe0\x26\x7c\x9d\xd2\x3d\x78\x7a\xbd" "\xe2\xaf\xcb\x30\x6a\xeb\x7d\xfc\xe6\x92\x46\xcc\x73\xf5\xc8\x7f" "\xdf\x06\x03\x01\x79\xa2\x11\x4b\x76\x7d\xb1\xf0\x83\xff\x84\x1c" "\x02\x5d\x7d\xc0\x0c\xd8\x24\x35\xb9\xa9\x0f\x69\x53\x69\xe9\x4d" "\xf2\x3d\x2c\xe4\x58\xbc\x3b\x32\x83\xad\x8b\xba\x2b\x8f\xa1\xba" "\x62\xe2\xdc\xe9\xac\xcf\xf3\x79\x9a\xae\x7c\x84\x00\x16\xf3\xba" "\x8e\x00\x48\xc0\xb6\xcc\x43\x39\xaf\x71\x61\x00\x3a\x5b\xeb\x86" "\x4a\x01\x64\xb2\xc1\xc9\x23\x7b\x64\xbc\x87\x55\x69\x94\x35\x1b" "\x27\x50\x6c\x33\xd4\xbc\xdf\xce\x0f\x9c\x49\x1a\x7d\x6b\x06\x28" "\xc7\xc8\x52\xbe\x4f\x0a\x9c\x31\x32\xb2\xed\x3a\x2c\x88\x81\xe9" "\xaa\xb0\x7e\x20\xe1\x7d\xeb\x07\x46\x91\xbe\x67\x77\x76\xa7\x8b" "\x5c\x50\x2e\x05\xd9\xbd\xde\x72\x12\x6b\x37\x38\x69\x5e\x2d\xd1" "\xa0\xa9\x8a\x14\x24\x7c\x65\xd8\xa7\xee\x79\x43\x2a\x09\x2c\xb0" "\x72\x1a\x12\xdf\x79\x8e\x44\xf7\xcf\xce\x0c\x49\x81\x47\xa9\xb1"; static const unsigned char p[] = "\x06\x77\xcd\xd5\x46\x9b\xc1\xd5\x58\x00\x81\xe2\xf3\x0a\x36\xb1" "\x6e\x29\x89\xd5\x2f\x31\x5f\x92\x22\x3b\x9b\x75\x30\x82\xfa\xc5" "\xf5\xde\x8a\x36\xdb\xc6\xe5\x8f\xef\x14\x37\xd6\x00\xf9\xab\x90" "\x9b\x5d\x57\x4c\xf5\x1f\x77\xc4\xbb\x8b\xdd\x9b\x67\x11\x45\xb2" "\x64\xe8\xac\xa8\x03\x0f\x16\x0d\x5d\x2d\x53\x07\x23\xfb\x62\x0d" "\xe6\x16\xd3\x23\xe8\xb3"; static const unsigned char q[] = "\x06\x66\x9a\x70\x53\xd6\x72\x74\xfd\xea\x45\xc3\xc0\x17\xae\xde" "\x79\x17\xae\x79\xde\xfc\x0e\xf7\xa4\x3a\x8c\x43\x8f\xc7\x8a\xa2" "\x2c\x51\xc4\xd0\x72\x89\x73\x5c\x61\xbe\xfd\x54\x3f\x92\x65\xde" "\x4d\x65\x71\x70\xf6\xf2\xe5\x98\xb9\x0f\xd1\x0b\xe6\x95\x09\x4a" "\x7a\xdf\xf3\x10\x16\xd0\x60\xfc\xa5\x10\x34\x97\x37\x6f\x0a\xd5" "\x5d\x8f\xd4\xc3\xa0\x5b"; static const unsigned char dmp1[] = "\x05\x7c\x9e\x1c\xbd\x90\x25\xe7\x40\x86\xf5\xa8\x3b\x7a\x3f\x99" "\x56\x95\x60\x3a\x7b\x95\x4b\xb8\xa0\xd7\xa5\xf1\xcc\xdc\x5f\xb5" "\x8c\xf4\x62\x95\x54\xed\x2e\x12\x62\xc2\xe8\xf6\xde\xce\xed\x8e" "\x77\x6d\xc0\x40\x25\x74\xb3\x5a\x2d\xaa\xe1\xac\x11\xcb\xe2\x2f" "\x0a\x51\x23\x1e\x47\xb2\x05\x88\x02\xb2\x0f\x4b\xf0\x67\x30\xf0" "\x0f\x6e\xef\x5f\xf7\xe7"; static const unsigned char dmq1[] = "\x01\xa5\x6b\xbc\xcd\xe3\x0e\x46\xc6\x72\xf5\x04\x56\x28\x01\x22" "\x58\x74\x5d\xbc\x1c\x3c\x29\x41\x49\x6c\x81\x5c\x72\xe2\xf7\xe5" "\xa3\x8e\x58\x16\xe0\x0e\x37\xac\x1f\xbb\x75\xfd\xaf\xe7\xdf\xe9" "\x1f\x70\xa2\x8f\x52\x03\xc0\x46\xd9\xf9\x96\x63\x00\x27\x7e\x5f" "\x38\x60\xd6\x6b\x61\xe2\xaf\xbe\xea\x58\xd3\x9d\xbc\x75\x03\x8d" "\x42\x65\xd6\x6b\x85\x97"; static const unsigned char iqmp[] = "\x03\xa1\x8b\x80\xe4\xd8\x87\x25\x17\x5d\xcc\x8d\xa9\x8a\x22\x2b" "\x6c\x15\x34\x6f\x80\xcc\x1c\x44\x04\x68\xbc\x03\xcd\x95\xbb\x69" "\x37\x61\x48\xb4\x23\x13\x08\x16\x54\x6a\xa1\x7c\xf5\xd4\x3a\xe1" "\x4f\xa4\x0c\xf5\xaf\x80\x85\x27\x06\x0d\x70\xc0\xc5\x19\x28\xfe" "\xee\x8e\x86\x21\x98\x8a\x37\xb7\xe5\x30\x25\x70\x93\x51\x2d\x49" "\x85\x56\xb3\x0c\x2b\x96"; static const unsigned char ex_prime[] = "\x03\x89\x22\xa0\xb7\x3a\x91\xcb\x5e\x0c\xfd\x73\xde\xa7\x38\xa9" "\x47\x43\xd6\x02\xbf\x2a\xb9\x3c\x48\xf3\x06\xd6\x58\x35\x50\x56" "\x16\x5c\x34\x9b\x61\x87\xc8\xaa\x0a\x5d\x8a\x0a\xcd\x9c\x41\xd9" "\x96\x24\xe0\xa9\x9b\x26\xb7\xa8\x08\xc9\xea\xdc\xa7\x15\xfb\x62" "\xa0\x2d\x90\xe6\xa7\x55\x6e\xc6\x6c\xff\xd6\x10\x6d\xfa\x2e\x04" "\x50\xec\x5c\x66\xe4\x05"; static const unsigned char ex_exponent[] = "\x02\x0a\xcd\xc3\x82\xd2\x03\xb0\x31\xac\xd3\x20\x80\x34\x9a\x57" "\xbc\x60\x04\x57\x25\xd0\x29\x9a\x16\x90\xb9\x1c\x49\x6a\xd1\xf2" "\x47\x8c\x0e\x9e\xc9\x20\xc2\xd8\xe4\x8f\xce\xd2\x1a\x9c\xec\xb4" "\x1f\x33\x41\xc8\xf5\x62\xd1\xa5\xef\x1d\xa1\xd8\xbd\x71\xc6\xf7" "\xda\x89\x37\x2e\xe2\xec\x47\xc5\xb8\xe3\xb4\xe3\x5c\x82\xaa\xdd" "\xb7\x58\x2e\xaf\x07\x79"; static const unsigned char ex_coefficient[] = "\x00\x9c\x09\x88\x9b\xc8\x57\x08\x69\x69\xab\x2d\x9e\x29\x1c\x3c" "\x6d\x59\x33\x12\x0d\x2b\x09\x2e\xaf\x01\x2c\x27\x01\xfc\xbd\x26" "\x13\xf9\x2d\x09\x22\x4e\x49\x11\x03\x82\x88\x87\xf4\x43\x1d\xac" "\xca\xec\x86\xf7\x23\xf1\x64\xf3\xf5\x81\xf0\x37\x36\xcf\x67\xff" "\x1a\xff\x7a\xc7\xf9\xf9\x67\x2d\xa0\x9d\x61\xf8\xf6\x47\x5c\x2f" "\xe7\x66\xe8\x3c\x3a\xe8"; static int key2048_key(RSA *key) { if (!TEST_int_eq(RSA_set0_key(key, BN_bin2bn(n, sizeof(n) - 1, NULL), BN_bin2bn(e, sizeof(e) - 1, NULL), BN_bin2bn(d, sizeof(d) - 1, NULL)), 1)) return 0; return RSA_size(key); } static int key2048p3_v1(RSA *key) { BIGNUM **pris = NULL, **exps = NULL, **coeffs = NULL; int rv = RSA_size(key); if (!TEST_int_eq(RSA_set0_factors(key, BN_bin2bn(p, sizeof(p) - 1, NULL), BN_bin2bn(q, sizeof(q) - 1, NULL)), 1)) goto err; if (!TEST_int_eq(RSA_set0_crt_params(key, BN_bin2bn(dmp1, sizeof(dmp1) - 1, NULL), BN_bin2bn(dmq1, sizeof(dmq1) - 1, NULL), BN_bin2bn(iqmp, sizeof(iqmp) - 1, NULL)), 1)) return 0; pris = OPENSSL_zalloc(sizeof(BIGNUM *)); exps = OPENSSL_zalloc(sizeof(BIGNUM *)); coeffs = OPENSSL_zalloc(sizeof(BIGNUM *)); if (!TEST_ptr(pris) || !TEST_ptr(exps) || !TEST_ptr(coeffs)) goto err; pris[0] = BN_bin2bn(ex_prime, sizeof(ex_prime) - 1, NULL); exps[0] = BN_bin2bn(ex_exponent, sizeof(ex_exponent) - 1, NULL); coeffs[0] = BN_bin2bn(ex_coefficient, sizeof(ex_coefficient) - 1, NULL); if (!TEST_ptr(pris[0]) || !TEST_ptr(exps[0]) || !TEST_ptr(coeffs[0])) goto err; if (!TEST_true(RSA_set0_multi_prime_params(key, pris, exps, coeffs, NUM_EXTRA_PRIMES))) goto err; ret: OPENSSL_free(pris); OPENSSL_free(exps); OPENSSL_free(coeffs); return rv; err: if (pris != NULL) BN_free(pris[0]); if (exps != NULL) BN_free(exps[0]); if (coeffs != NULL) BN_free(coeffs[0]); rv = 0; goto ret; } static int key2048p3_v2(RSA *key) { STACK_OF(BIGNUM) *primes = NULL, *exps = NULL, *coeffs = NULL; BIGNUM *num = NULL; int rv = RSA_size(key); if (!TEST_ptr(primes = sk_BIGNUM_new_null()) || !TEST_ptr(exps = sk_BIGNUM_new_null()) || !TEST_ptr(coeffs = sk_BIGNUM_new_null())) goto err; if (!TEST_ptr(num = BN_bin2bn(p, sizeof(p) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0) || !TEST_ptr(num = BN_bin2bn(q, sizeof(q) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0) || !TEST_ptr(num = BN_bin2bn(ex_prime, sizeof(ex_prime) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0)) goto err; if (!TEST_ptr(num = BN_bin2bn(dmp1, sizeof(dmp1) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0) || !TEST_ptr(num = BN_bin2bn(dmq1, sizeof(dmq1) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0) || !TEST_ptr(num = BN_bin2bn(ex_exponent, sizeof(ex_exponent) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0)) goto err; if (!TEST_ptr(num = BN_bin2bn(iqmp, sizeof(iqmp) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(coeffs, num), 0) || !TEST_ptr(num = BN_bin2bn(ex_coefficient, sizeof(ex_coefficient) - 1, NULL)) || !TEST_int_ne(sk_BIGNUM_push(coeffs, num), 0)) goto err; if (!TEST_true(ossl_rsa_set0_all_params(key, primes, exps, coeffs))) goto err; ret: sk_BIGNUM_free(primes); sk_BIGNUM_free(exps); sk_BIGNUM_free(coeffs); return rv; err: sk_BIGNUM_pop_free(primes, BN_free); sk_BIGNUM_pop_free(exps, BN_free); sk_BIGNUM_pop_free(coeffs, BN_free); primes = exps = coeffs = NULL; rv = 0; goto ret; } static int test_rsa_mp(int i) { int ret = 0; RSA *key; unsigned char ptext[256]; unsigned char ctext[256]; static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; int plen; int clen = 0; int num; static int (*param_set[])(RSA *) = { key2048p3_v1, key2048p3_v2, }; plen = sizeof(ptext_ex) - 1; key = RSA_new(); if (!TEST_ptr(key)) goto err; if (!TEST_int_eq((clen = key2048_key(key)), 256) || !TEST_int_eq((clen = param_set[i](key)), 256)) goto err; if (!TEST_true(RSA_check_key_ex(key, NULL))) goto err; num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING); if (!TEST_int_eq(num, clen)) goto err; num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING); if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) goto err; ret = 1; err: RSA_free(key); return ret; } static int test_rsa_mp_gen_bad_input(void) { int ret = 0; RSA *rsa = NULL; BIGNUM *ebn = NULL; if (!TEST_ptr(rsa = RSA_new())) goto err; if (!TEST_ptr(ebn = BN_new())) goto err; if (!TEST_true(BN_set_word(ebn, 65537))) goto err; /* Test that a NULL exponent fails and does not segfault */ if (!TEST_int_eq(RSA_generate_multi_prime_key(rsa, 1024, 2, NULL, NULL), 0)) goto err; /* Test invalid bitsize fails */ if (!TEST_int_eq(RSA_generate_multi_prime_key(rsa, 500, 2, ebn, NULL), 0)) goto err; /* Test invalid prime count fails */ if (!TEST_int_eq(RSA_generate_multi_prime_key(rsa, 1024, 1, ebn, NULL), 0)) goto err; ret = 1; err: BN_free(ebn); RSA_free(rsa); return ret; } int setup_tests(void) { ADD_TEST(test_rsa_mp_gen_bad_input); ADD_ALL_TESTS(test_rsa_mp, 2); return 1; }
./openssl/test/bio_readbuffer_test.c
/* * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/bio.h> #include "testutil.h" static const char *filename = NULL; /* * Test that a BIO_f_readbuffer() with a BIO_new_file() behaves nicely if * BIO_gets() and BIO_read_ex() are both called. * Since the BIO_gets() calls buffer the reads, the BIO_read_ex() should * still be able to read the buffered data if we seek back to the start. * * The following cases are tested using tstid: * 0 : Just use BIO_read_ex(). * 1 : Try a few reads using BIO_gets() before using BIO_read_ex() * 2 : Read the entire file using BIO_gets() before using BIO_read_ex(). */ static int test_readbuffer_file_bio(int tstid) { int ret = 0, len, partial; BIO *in = NULL, *in_bio = NULL, *readbuf_bio = NULL; char buf[255]; char expected[4096]; size_t readbytes = 0, bytes = 0, count = 0; /* Open a file BIO and read all the data */ if (!TEST_ptr(in = BIO_new_file(filename, "r")) || !TEST_int_eq(BIO_read_ex(in, expected, sizeof(expected), &readbytes), 1) || !TEST_int_lt(readbytes, sizeof(expected))) goto err; BIO_free(in); in = NULL; /* Create a new file bio that sits under a readbuffer BIO */ if (!TEST_ptr(readbuf_bio = BIO_new(BIO_f_readbuffer())) || !TEST_ptr(in_bio = BIO_new_file(filename, "r"))) goto err; in_bio = BIO_push(readbuf_bio, in_bio); readbuf_bio = NULL; if (!TEST_int_eq(BIO_tell(in_bio), 0)) goto err; if (tstid != 0) { partial = 4; while (!BIO_eof(in_bio)) { len = BIO_gets(in_bio, buf, sizeof(buf)); if (len == 0) { if (!TEST_true(BIO_eof(in_bio))) goto err; } else { if (!TEST_int_gt(len, 0) || !TEST_int_le(len, (int)sizeof(buf) - 1)) goto err; if (!TEST_true(buf[len] == 0)) goto err; if (len > 1 && !BIO_eof(in_bio) && len != ((int)sizeof(buf) - 1) && !TEST_true(buf[len - 1] == '\n')) goto err; } if (tstid == 1 && --partial == 0) break; } } if (!TEST_int_eq(BIO_seek(in_bio, 0), 1)) goto err; len = 8; /* Do a small partial read to start with */ while (!BIO_eof(in_bio)) { if (!TEST_int_eq(BIO_read_ex(in_bio, buf, len, &bytes), 1)) break; if (!TEST_mem_eq(buf, bytes, expected + count, bytes)) goto err; count += bytes; len = sizeof(buf); /* fill the buffer on subsequent reads */ } if (!TEST_int_eq(count, readbytes)) goto err; ret = 1; err: BIO_free(in); BIO_free_all(in_bio); BIO_free(readbuf_bio); return ret; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file\n"), { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_TEST_CASES: break; default: return 0; } } filename = test_get_argument(0); ADD_ALL_TESTS(test_readbuffer_file_bio, 3); return 1; }
./openssl/test/shlibloadtest.c
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/opensslv.h> #include <openssl/ssl.h> #include <openssl/types.h> #include "simpledynamic.h" typedef void DSO; typedef const SSL_METHOD * (*TLS_method_t)(void); typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth); typedef void (*SSL_CTX_free_t)(SSL_CTX *); typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *); typedef int (*OPENSSL_atexit_t)(void (*handler)(void)); typedef unsigned long (*ERR_get_error_t)(void); typedef unsigned long (*OPENSSL_version_major_t)(void); typedef unsigned long (*OPENSSL_version_minor_t)(void); typedef unsigned long (*OPENSSL_version_patch_t)(void); typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags); typedef int (*DSO_free_t)(DSO *dso); typedef enum test_types_en { CRYPTO_FIRST, SSL_FIRST, JUST_CRYPTO, DSO_REFTEST, NO_ATEXIT } TEST_TYPE; static TEST_TYPE test_type; static const char *path_crypto; static const char *path_ssl; static const char *path_atexit; #ifdef SD_INIT static int atexit_handler_done = 0; static void atexit_handler(void) { FILE *atexit_file = fopen(path_atexit, "w"); if (atexit_file == NULL) return; fprintf(atexit_file, "atexit() run\n"); fclose(atexit_file); atexit_handler_done++; } static int test_lib(void) { SD ssllib = SD_INIT; SD cryptolib = SD_INIT; SSL_CTX *ctx; union { void (*func)(void); SD_SYM sym; } symbols[5]; TLS_method_t myTLS_method; SSL_CTX_new_t mySSL_CTX_new; SSL_CTX_free_t mySSL_CTX_free; ERR_get_error_t myERR_get_error; OPENSSL_version_major_t myOPENSSL_version_major; OPENSSL_version_minor_t myOPENSSL_version_minor; OPENSSL_version_patch_t myOPENSSL_version_patch; OPENSSL_atexit_t myOPENSSL_atexit; int result = 0; switch (test_type) { case JUST_CRYPTO: case DSO_REFTEST: case NO_ATEXIT: case CRYPTO_FIRST: if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { fprintf(stderr, "Failed to load libcrypto\n"); goto end; } if (test_type != CRYPTO_FIRST) break; /* Fall through */ case SSL_FIRST: if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) { fprintf(stderr, "Failed to load libssl\n"); goto end; } if (test_type != SSL_FIRST) break; if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { fprintf(stderr, "Failed to load libcrypto\n"); goto end; } break; } if (test_type == NO_ATEXIT) { OPENSSL_init_crypto_t myOPENSSL_init_crypto; if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) { fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n"); goto end; } myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func; if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) { fprintf(stderr, "Failed to initialise libcrypto\n"); goto end; } } if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST && test_type != NO_ATEXIT) { if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym) || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym) || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) { fprintf(stderr, "Failed to load libssl symbols\n"); goto end; } myTLS_method = (TLS_method_t)symbols[0].func; mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func; mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func; ctx = mySSL_CTX_new(myTLS_method()); if (ctx == NULL) { fprintf(stderr, "Failed to create SSL_CTX\n"); goto end; } mySSL_CTX_free(ctx); } if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym) || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym) || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym) || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym) || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) { fprintf(stderr, "Failed to load libcrypto symbols\n"); goto end; } myERR_get_error = (ERR_get_error_t)symbols[0].func; if (myERR_get_error() != 0) { fprintf(stderr, "Unexpected ERR_get_error() response\n"); goto end; } /* Library and header version should be identical in this test */ myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func; myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func; myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func; if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) { fprintf(stderr, "Invalid library version number\n"); goto end; } myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func; if (!myOPENSSL_atexit(atexit_handler)) { fprintf(stderr, "Failed to register atexit handler\n"); goto end; } if (test_type == DSO_REFTEST) { # ifdef DSO_DLFCN DSO_dsobyaddr_t myDSO_dsobyaddr; DSO_free_t myDSO_free; /* * This is resembling the code used in ossl_init_base() and * OPENSSL_atexit() to block unloading the library after dlclose(). * We are not testing this on Windows, because it is done there in a * completely different way. Especially as a call to DSO_dsobyaddr() * will always return an error, because DSO_pathbyaddr() is not * implemented there. */ if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym) || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) { fprintf(stderr, "Unable to load DSO symbols\n"); goto end; } myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func; myDSO_free = (DSO_free_t)symbols[1].func; { DSO *hndl; /* use known symbol from crypto module */ hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0); if (hndl == NULL) { fprintf(stderr, "DSO_dsobyaddr() failed\n"); goto end; } myDSO_free(hndl); } # endif /* DSO_DLFCN */ } if (!sd_close(cryptolib)) { fprintf(stderr, "Failed to close libcrypto\n"); goto end; } cryptolib = SD_INIT; if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) { if (!sd_close(ssllib)) { fprintf(stderr, "Failed to close libssl\n"); goto end; } ssllib = SD_INIT; } # if defined(OPENSSL_NO_PINSHARED) \ && defined(__GLIBC__) \ && defined(__GLIBC_PREREQ) \ && defined(OPENSSL_SYS_LINUX) # if __GLIBC_PREREQ(2, 3) /* * If we didn't pin the so then we are hopefully on a platform that supports * running atexit() on so unload. If not we might crash. We know this is * true on linux since glibc 2.2.3 */ if (test_type != NO_ATEXIT && atexit_handler_done != 1) { fprintf(stderr, "atexit() handler did not run\n"); goto end; } # endif # endif result = 1; end: if (cryptolib != SD_INIT) sd_close(cryptolib); if (ssllib != SD_INIT) sd_close(ssllib); return result; } #endif /* * shlibloadtest should not use the normal test framework because we don't want * it to link against libcrypto (which the framework uses). The point of the * test is to check dynamic loading and unloading of libcrypto/libssl. */ int main(int argc, char *argv[]) { const char *p; if (argc != 5) { fprintf(stderr, "Incorrect number of arguments\n"); return 1; } p = argv[1]; if (strcmp(p, "-crypto_first") == 0) { test_type = CRYPTO_FIRST; } else if (strcmp(p, "-ssl_first") == 0) { test_type = SSL_FIRST; } else if (strcmp(p, "-just_crypto") == 0) { test_type = JUST_CRYPTO; } else if (strcmp(p, "-dso_ref") == 0) { test_type = DSO_REFTEST; } else if (strcmp(p, "-no_atexit") == 0) { test_type = NO_ATEXIT; } else { fprintf(stderr, "Unrecognised argument\n"); return 1; } path_crypto = argv[2]; path_ssl = argv[3]; path_atexit = argv[4]; if (path_crypto == NULL || path_ssl == NULL) { fprintf(stderr, "Invalid libcrypto/libssl path\n"); return 1; } #ifdef SD_INIT if (!test_lib()) return 1; #endif return 0; }
./openssl/test/dtlstest.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/ssl.h> #include <openssl/err.h> #include "helpers/ssltestlib.h" #include "testutil.h" static char *cert = NULL; static char *privkey = NULL; static unsigned int timer_cb_count; #define NUM_TESTS 2 #define DUMMY_CERT_STATUS_LEN 12 static unsigned char certstatus[] = { SSL3_RT_HANDSHAKE, /* Content type */ 0xfe, 0xfd, /* Record version */ 0, 1, /* Epoch */ 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */ 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2, SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */ 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */ 0, 5, /* Message sequence */ 0, 0, 0, /* Fragment offset */ 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */ }; #define RECORD_SEQUENCE 10 static const char dummy_cookie[] = "0123456"; static int generate_cookie_cb(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) { memcpy(cookie, dummy_cookie, sizeof(dummy_cookie)); *cookie_len = sizeof(dummy_cookie); return 1; } static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len) { return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie)); } static unsigned int timer_cb(SSL *s, unsigned int timer_us) { ++timer_cb_count; if (timer_us == 0) return 50000; else return 2 * timer_us; } static int test_dtls_unprocessed(int testidx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl1 = NULL, *clientssl1 = NULL; BIO *c_to_s_fbio, *c_to_s_mempacket; int testresult = 0; timer_cb_count = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; #ifndef OPENSSL_NO_DTLS1_2 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA"))) goto end; #else /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA:@SECLEVEL=0"))) goto end; #endif c_to_s_fbio = BIO_new(bio_f_tls_dump_filter()); if (!TEST_ptr(c_to_s_fbio)) goto end; /* BIO is freed by create_ssl_connection on error */ if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL, c_to_s_fbio))) goto end; DTLS_set_timer_cb(clientssl1, timer_cb); if (testidx == 1) certstatus[RECORD_SEQUENCE] = 0xff; /* * Inject a dummy record from the next epoch. In test 0, this should never * get used because the message sequence number is too big. In test 1 we set * the record sequence number to be way off in the future. */ c_to_s_mempacket = SSL_get_wbio(clientssl1); c_to_s_mempacket = BIO_next(c_to_s_mempacket); mempacket_test_inject(c_to_s_mempacket, (char *)certstatus, sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ); /* * Create the connection. We use "create_bare_ssl_connection" here so that * we can force the connection to not do "SSL_read" once partly connected. * We don't want to accidentally read the dummy records we injected because * they will fail to decrypt. */ if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1, SSL_ERROR_NONE, 0, 0))) goto end; if (timer_cb_count == 0) { printf("timer_callback was not called.\n"); goto end; } testresult = 1; end: SSL_free(serverssl1); SSL_free(clientssl1); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } /* One record for the cookieless initial ClientHello */ #define CLI_TO_SRV_COOKIE_EXCH 1 /* * In a resumption handshake we use 2 records for the initial ClientHello in * this test because we are using a very small MTU and the ClientHello is * bigger than in the non resumption case. */ #define CLI_TO_SRV_RESUME_COOKIE_EXCH 2 #define SRV_TO_CLI_COOKIE_EXCH 1 #define CLI_TO_SRV_EPOCH_0_RECS 3 #define CLI_TO_SRV_EPOCH_1_RECS 1 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) # define SRV_TO_CLI_EPOCH_0_RECS 10 #else /* * In this case we have no ServerKeyExchange message, because we don't have * ECDHE or DHE. When it is present it gets fragmented into 3 records in this * test. */ # define SRV_TO_CLI_EPOCH_0_RECS 9 #endif #define SRV_TO_CLI_EPOCH_1_RECS 1 #define TOTAL_FULL_HAND_RECORDS \ (CLI_TO_SRV_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \ CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \ SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS) #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3 #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1 #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2 #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1 #define TOTAL_RESUME_HAND_RECORDS \ (CLI_TO_SRV_RESUME_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \ CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \ SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS) #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS) /* * We are assuming a ServerKeyExchange message is sent in this test. If we don't * have either DH or EC, then it won't be */ #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) static int test_dtls_drop_records(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; BIO *c_to_s_fbio, *mempackbio; int testresult = 0; int epoch = 0; SSL_SESSION *sess = NULL; int cli_to_srv_cookie, cli_to_srv_epoch0, cli_to_srv_epoch1; int srv_to_cli_epoch0; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; #ifdef OPENSSL_NO_DTLS1_2 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))) goto end; #endif if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1))) goto end; SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE); SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb); SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb); if (idx >= TOTAL_FULL_HAND_RECORDS) { /* We're going to do a resumption handshake. Get a session first. */ if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_ptr(sess = SSL_get1_session(clientssl))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS; cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS; srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS; cli_to_srv_cookie = CLI_TO_SRV_RESUME_COOKIE_EXCH; idx -= TOTAL_FULL_HAND_RECORDS; } else { cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS; cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS; srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS; cli_to_srv_cookie = CLI_TO_SRV_COOKIE_EXCH; } c_to_s_fbio = BIO_new(bio_f_tls_dump_filter()); if (!TEST_ptr(c_to_s_fbio)) goto end; /* BIO is freed by create_ssl_connection on error */ if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, c_to_s_fbio))) goto end; if (sess != NULL) { if (!TEST_true(SSL_set_session(clientssl, sess))) goto end; } DTLS_set_timer_cb(clientssl, timer_cb); DTLS_set_timer_cb(serverssl, timer_cb); /* Work out which record to drop based on the test number */ if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1) { mempackbio = SSL_get_wbio(serverssl); idx -= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1; if (idx >= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0) { epoch = 1; idx -= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0; } } else { mempackbio = SSL_get_wbio(clientssl); if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0) { epoch = 1; idx -= cli_to_srv_cookie + cli_to_srv_epoch0; } mempackbio = BIO_next(mempackbio); } BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL); BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (sess != NULL && !TEST_true(SSL_session_reused(clientssl))) goto end; /* If the test did what we planned then it should have dropped a record */ if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0, NULL), -1)) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */ static int test_cookie(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE); SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb); SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb); #ifdef OPENSSL_NO_DTLS1_2 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))) goto end; #endif if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_dtls_duplicate_records(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; #ifdef OPENSSL_NO_DTLS1_2 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))) goto end; #endif if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; DTLS_set_timer_cb(clientssl, timer_cb); DTLS_set_timer_cb(serverssl, timer_cb); BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL); BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } /* * Test just sending a Finished message as the first message. Should fail due * to an unexpected message. */ static int test_just_finished(void) { int testresult = 0, ret; SSL_CTX *sctx = NULL; SSL *serverssl = NULL; BIO *rbio = NULL, *wbio = NULL, *sbio = NULL; unsigned char buf[] = { /* Record header */ SSL3_RT_HANDSHAKE, /* content type */ (DTLS1_2_VERSION >> 8) & 0xff, /* protocol version hi byte */ DTLS1_2_VERSION & 0xff, /* protocol version lo byte */ 0, 0, /* epoch */ 0, 0, 0, 0, 0, 0, /* record sequence */ 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, /* record length */ /* Message header */ SSL3_MT_FINISHED, /* message type */ 0, 0, SHA_DIGEST_LENGTH, /* message length */ 0, 0, /* message sequence */ 0, 0, 0, /* fragment offset */ 0, 0, SHA_DIGEST_LENGTH, /* fragment length */ /* Message body */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), NULL, 0, 0, &sctx, NULL, cert, privkey))) return 0; #ifdef OPENSSL_NO_DTLS1_2 /* DTLSv1 is not allowed at the default security level */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))) goto end; #endif serverssl = SSL_new(sctx); rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio)) goto end; sbio = rbio; SSL_set0_rbio(serverssl, rbio); SSL_set0_wbio(serverssl, wbio); rbio = wbio = NULL; DTLS_set_timer_cb(serverssl, timer_cb); if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf))) goto end; /* We expect the attempt to process the message to fail */ if (!TEST_int_le(ret = SSL_accept(serverssl), 0)) goto end; /* Check that we got the error we were expecting */ if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL)) goto end; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE)) goto end; testresult = 1; end: BIO_free(rbio); BIO_free(wbio); SSL_free(serverssl); SSL_CTX_free(sctx); return testresult; } /* * Test that swapping later records before Finished or CCS still works * Test 0: Test receiving a handshake record early from next epoch on server side * Test 1: Test receiving a handshake record early from next epoch on client side * Test 2: Test receiving an app data record early from next epoch on client side * Test 3: Test receiving an app data before Finished on client side */ static int test_swap_records(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *sssl = NULL, *cssl = NULL; int testresult = 0; BIO *bio; char msg[] = { 0x00, 0x01, 0x02, 0x03 }; char buf[10]; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; #ifndef OPENSSL_NO_DTLS1_2 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA"))) goto end; #else /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA:@SECLEVEL=0"))) goto end; #endif if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL))) goto end; /* Send flight 1: ClientHello */ if (!TEST_int_le(SSL_connect(cssl), 0)) goto end; /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */ if (!TEST_int_le(SSL_accept(sssl), 0)) goto end; /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */ if (!TEST_int_le(SSL_connect(cssl), 0)) goto end; if (idx == 0) { /* Swap Finished and CCS within the datagram */ bio = SSL_get_wbio(cssl); if (!TEST_ptr(bio) || !TEST_true(mempacket_swap_epoch(bio))) goto end; } /* Recv flight 3, send flight 4: datagram 0(NST, CCS) datagram 1(Finished) */ if (!TEST_int_gt(SSL_accept(sssl), 0)) goto end; /* Send flight 4 (cont'd): datagram 2(app data) */ if (!TEST_int_eq(SSL_write(sssl, msg, sizeof(msg)), (int)sizeof(msg))) goto end; bio = SSL_get_wbio(sssl); if (!TEST_ptr(bio)) goto end; if (idx == 1) { /* Finished comes before NST/CCS */ if (!TEST_true(mempacket_move_packet(bio, 0, 1))) goto end; } else if (idx == 2) { /* App data comes before NST/CCS */ if (!TEST_true(mempacket_move_packet(bio, 0, 2))) goto end; } else if (idx == 3) { /* App data comes before Finished */ bio = SSL_get_wbio(sssl); if (!TEST_true(mempacket_move_packet(bio, 1, 2))) goto end; } /* * Recv flight 4 (datagram 1): NST, CCS, + flight 5: app data * + flight 4 (datagram 2): Finished */ if (!TEST_int_gt(SSL_connect(cssl), 0)) goto end; if (idx == 0 || idx == 1) { /* App data was not received early, so it should not be pending */ if (!TEST_int_eq(SSL_pending(cssl), 0) || !TEST_false(SSL_has_pending(cssl))) goto end; } else { /* We received the app data early so it should be buffered already */ if (!TEST_int_eq(SSL_pending(cssl), (int)sizeof(msg)) || !TEST_true(SSL_has_pending(cssl))) goto end; } /* * Recv flight 5 (app data) */ if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), (int)sizeof(msg))) goto end; testresult = 1; end: SSL_free(cssl); SSL_free(sssl); SSL_CTX_free(cctx); SSL_CTX_free(sctx); return testresult; } /* Confirm that we can create a connections using DTLSv1_listen() */ static int test_listen(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; #ifdef OPENSSL_NO_DTLS1_2 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */ if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))) goto end; #endif SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb); SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; DTLS_set_timer_cb(clientssl, timer_cb); DTLS_set_timer_cb(serverssl, timer_cb); /* * The last parameter to create_bare_ssl_connection() requests that * DTLSv1_listen() is used. */ if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE, 1, 1))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(cert = test_get_argument(0)) || !TEST_ptr(privkey = test_get_argument(1))) return 0; ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS); #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS); #endif ADD_TEST(test_cookie); ADD_TEST(test_dtls_duplicate_records); ADD_TEST(test_just_finished); ADD_ALL_TESTS(test_swap_records, 4); ADD_TEST(test_listen); return 1; } void cleanup_tests(void) { bio_f_tls_dump_filter_free(); bio_s_mempacket_test_free(); }
./openssl/test/prov_config_test.c
/* * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/evp.h> #include <openssl/conf.h> #include "testutil.h" static char *configfile = NULL; static char *recurseconfigfile = NULL; /* * Test to make sure there are no leaks or failures from loading the config * file twice. */ static int test_double_config(void) { OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); int testresult = 0; EVP_MD *sha256 = NULL; if (!TEST_ptr(configfile)) return 0; if (!TEST_ptr(ctx)) return 0; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) return 0; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) return 0; /* Check we can actually fetch something */ sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL); if (!TEST_ptr(sha256)) goto err; testresult = 1; err: EVP_MD_free(sha256); OSSL_LIB_CTX_free(ctx); return testresult; } static int test_recursive_config(void) { OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); int testresult = 0; unsigned long err; if (!TEST_ptr(recurseconfigfile)) goto err; if (!TEST_ptr(ctx)) goto err; if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile))) goto err; err = ERR_peek_error(); /* We expect to get a recursion error here */ if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE) testresult = 1; err: OSSL_LIB_CTX_free(ctx); return testresult; } OPT_TEST_DECLARE_USAGE("configfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(configfile = test_get_argument(0))) return 0; if (!TEST_ptr(recurseconfigfile = test_get_argument(1))) return 0; ADD_TEST(test_recursive_config); ADD_TEST(test_double_config); return 1; }
./openssl/test/sparse_array_test.c
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <limits.h> #include <openssl/crypto.h> #include "internal/nelem.h" #include "crypto/sparse_array.h" #include "testutil.h" /* The macros below generate unused functions which error out one of the clang * builds. We disable this check here. */ #ifdef __clang__ #pragma clang diagnostic ignored "-Wunused-function" #endif DEFINE_SPARSE_ARRAY_OF(char); static int test_sparse_array(void) { static const struct { ossl_uintmax_t n; char *v; } cases[] = { { 22, "a" }, { 0, "z" }, { 1, "b" }, { 290, "c" }, { INT_MAX, "m" }, { 6666666, "d" }, { (ossl_uintmax_t)-1, "H" }, { 99, "e" } }; SPARSE_ARRAY_OF(char) *sa; size_t i, j; int res = 0; if (!TEST_ptr(sa = ossl_sa_char_new()) || !TEST_ptr_null(ossl_sa_char_get(sa, 3)) || !TEST_ptr_null(ossl_sa_char_get(sa, 0)) || !TEST_ptr_null(ossl_sa_char_get(sa, UINT_MAX))) goto err; for (i = 0; i < OSSL_NELEM(cases); i++) { if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) { TEST_note("iteration %zu", i + 1); goto err; } for (j = 0; j <= i; j++) if (!TEST_str_eq(ossl_sa_char_get(sa, cases[j].n), cases[j].v)) { TEST_note("iteration %zu / %zu", i + 1, j + 1); goto err; } } res = 1; err: ossl_sa_char_free(sa); return res; } static int test_sparse_array_num(void) { static const struct { size_t num; ossl_uintmax_t n; char *v; } cases[] = { { 1, 22, "a" }, { 2, 1021, "b" }, { 3, 3, "c" }, { 2, 22, NULL }, { 2, 3, "d" }, { 3, 22, "e" }, { 3, 666, NULL }, { 4, 666, "f" }, { 3, 3, NULL }, { 2, 22, NULL }, { 1, 666, NULL }, { 2, 64000, "g" }, { 1, 1021, NULL }, { 0, 64000, NULL }, { 1, 23, "h" }, { 0, 23, NULL } }; SPARSE_ARRAY_OF(char) *sa = NULL; size_t i; int res = 0; if (!TEST_size_t_eq(ossl_sa_char_num(NULL), 0) || !TEST_ptr(sa = ossl_sa_char_new()) || !TEST_size_t_eq(ossl_sa_char_num(sa), 0)) goto err; for (i = 0; i < OSSL_NELEM(cases); i++) if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v)) || !TEST_size_t_eq(ossl_sa_char_num(sa), cases[i].num)) goto err; res = 1; err: ossl_sa_char_free(sa); return res; } struct index_cases_st { ossl_uintmax_t n; char *v; int del; }; struct doall_st { SPARSE_ARRAY_OF(char) *sa; size_t num_cases; const struct index_cases_st *cases; int res; int all; }; static void leaf_check_all(ossl_uintmax_t n, char *value, void *arg) { struct doall_st *doall_data = (struct doall_st *)arg; const struct index_cases_st *cases = doall_data->cases; size_t i; doall_data->res = 0; for (i = 0; i < doall_data->num_cases; i++) if ((doall_data->all || !cases[i].del) && n == cases[i].n && strcmp(value, cases[i].v) == 0) { doall_data->res = 1; return; } TEST_error("Index %ju with value %s not found", n, value); } static void leaf_delete(ossl_uintmax_t n, char *value, void *arg) { struct doall_st *doall_data = (struct doall_st *)arg; const struct index_cases_st *cases = doall_data->cases; size_t i; doall_data->res = 0; for (i = 0; i < doall_data->num_cases; i++) if (n == cases[i].n && strcmp(value, cases[i].v) == 0) { doall_data->res = 1; ossl_sa_char_set(doall_data->sa, n, NULL); return; } TEST_error("Index %ju with value %s not found", n, value); } static int test_sparse_array_doall(void) { static const struct index_cases_st cases[] = { { 22, "A", 1 }, { 1021, "b", 0 }, { 3, "c", 0 }, { INT_MAX, "d", 1 }, { (ossl_uintmax_t)-1, "H", 0 }, { (ossl_uintmax_t)-2, "i", 1 }, { 666666666, "s", 1 }, { 1234567890, "t", 0 }, }; struct doall_st doall_data; size_t i; SPARSE_ARRAY_OF(char) *sa = NULL; int res = 0; if (!TEST_ptr(sa = ossl_sa_char_new())) goto err; doall_data.num_cases = OSSL_NELEM(cases); doall_data.cases = cases; doall_data.all = 1; doall_data.sa = NULL; for (i = 0; i < OSSL_NELEM(cases); i++) if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) { TEST_note("failed at iteration %zu", i + 1); goto err; } ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data); if (doall_data.res == 0) { TEST_info("while checking all elements"); goto err; } doall_data.all = 0; doall_data.sa = sa; ossl_sa_char_doall_arg(sa, &leaf_delete, &doall_data); if (doall_data.res == 0) { TEST_info("while deleting selected elements"); goto err; } ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data); if (doall_data.res == 0) { TEST_info("while checking for deleted elements"); goto err; } res = 1; err: ossl_sa_char_free(sa); return res; } int setup_tests(void) { ADD_TEST(test_sparse_array); ADD_TEST(test_sparse_array_num); ADD_TEST(test_sparse_array_doall); return 1; }
./openssl/test/cmp_vfy_test.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2019 * Copyright Siemens AG 2015-2019 * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "helpers/cmp_testlib.h" #include "../crypto/crmf/crmf_local.h" /* for manipulating POPO signature */ static const char *server_f; static const char *client_f; static const char *endentity1_f; static const char *endentity2_f; static const char *root_f; static const char *intermediate_f; static const char *ir_protected_f; static const char *ir_unprotected_f; static const char *ir_rmprotection_f; static const char *ip_waiting_f; static const char *instacert_f; static const char *instaca_f; static const char *ir_protected_0_extracerts; static const char *ir_protected_2_extracerts; typedef struct test_fixture { const char *test_case_name; int expected; OSSL_CMP_CTX *cmp_ctx; OSSL_CMP_MSG *msg; X509 *cert; ossl_cmp_allow_unprotected_cb_t allow_unprotected_cb; int additional_arg; } CMP_VFY_TEST_FIXTURE; static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; static void tear_down(CMP_VFY_TEST_FIXTURE *fixture) { OSSL_CMP_MSG_free(fixture->msg); OSSL_CMP_CTX_free(fixture->cmp_ctx); OPENSSL_free(fixture); } static time_t test_time_valid = 0, test_time_after_expiration = 0; static CMP_VFY_TEST_FIXTURE *set_up(const char *const test_case_name) { X509_STORE *ts; CMP_VFY_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; ts = X509_STORE_new(); fixture->test_case_name = test_case_name; if (ts == NULL || !TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL)) || !OSSL_CMP_CTX_set0_trusted(fixture->cmp_ctx, ts) || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)) { tear_down(fixture); X509_STORE_free(ts); return NULL; } X509_VERIFY_PARAM_set_time(X509_STORE_get0_param(ts), test_time_valid); X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb); return fixture; } static X509 *srvcert = NULL; static X509 *clcert = NULL; /* chain */ static X509 *endentity1 = NULL, *endentity2 = NULL, *intermediate = NULL, *root = NULL; /* INSTA chain */ static X509 *insta_cert = NULL, *instaca_cert = NULL; static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; static OSSL_CMP_MSG *ir_unprotected, *ir_rmprotection; /* secret value used for IP_waitingStatus_PBM.der */ static const unsigned char sec_1[] = { '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3', 'Q', '-', 'u', 'd', 'N', 'R' }; static int flip_bit(ASN1_BIT_STRING *bitstr) { int bit_num = 7; int bit = ASN1_BIT_STRING_get_bit(bitstr, bit_num); return ASN1_BIT_STRING_set_bit(bitstr, bit_num, !bit); } static int execute_verify_popo_test(CMP_VFY_TEST_FIXTURE *fixture) { if ((fixture->msg = load_pkimsg(ir_protected_f, libctx)) == NULL) return 0; if (fixture->expected == 0) { const OSSL_CRMF_MSGS *reqs = fixture->msg->body->value.ir; const OSSL_CRMF_MSG *req = sk_OSSL_CRMF_MSG_value(reqs, 0); if (req == NULL || !flip_bit(req->popo->value.signature->signature)) return 0; } return TEST_int_eq(fixture->expected, ossl_cmp_verify_popo(fixture->cmp_ctx, fixture->msg, fixture->additional_arg)); } static int test_verify_popo(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_verify_popo_test, tear_down); return result; } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_verify_popo_bad(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->expected = 0; EXECUTE_TEST(execute_verify_popo_test, tear_down); return result; } #endif /* indirectly checks also OSSL_CMP_validate_msg() */ static int execute_validate_msg_test(CMP_VFY_TEST_FIXTURE *fixture) { int res = TEST_int_eq(fixture->expected, ossl_cmp_msg_check_update(fixture->cmp_ctx, fixture->msg, NULL, 0)); X509 *validated = OSSL_CMP_CTX_get0_validatedSrvCert(fixture->cmp_ctx); return res && (!fixture->expected || TEST_ptr_eq(validated, fixture->cert)); } static int execute_validate_cert_path_test(CMP_VFY_TEST_FIXTURE *fixture) { X509_STORE *ts = OSSL_CMP_CTX_get0_trusted(fixture->cmp_ctx); int res = TEST_int_eq(fixture->expected, OSSL_CMP_validate_cert_path(fixture->cmp_ctx, ts, fixture->cert)); OSSL_CMP_CTX_print_errors(fixture->cmp_ctx); return res; } static int test_validate_msg_mac_alg_protection(int miss, int wrong) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = NULL; fixture->expected = !miss && !wrong; if (!TEST_true(miss ? OSSL_CMP_CTX_set0_trusted(fixture->cmp_ctx, NULL) : OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_1, wrong ? 4 : sizeof(sec_1))) || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } static int test_validate_msg_mac_alg_protection_ok(void) { return test_validate_msg_mac_alg_protection(0, 0); } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_mac_alg_protection_missing(void) { return test_validate_msg_mac_alg_protection(1, 0); } static int test_validate_msg_mac_alg_protection_wrong(void) { return test_validate_msg_mac_alg_protection(0, 1); } static int test_validate_msg_mac_alg_protection_bad(void) { const unsigned char sec_bad[] = { '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3', 'Q', '-', 'u', 'd', 'N', 'r' }; SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = NULL; fixture->expected = 0; if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_bad, sizeof(sec_bad))) || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } #endif static int add_trusted(OSSL_CMP_CTX *ctx, X509 *cert) { return X509_STORE_add_cert(OSSL_CMP_CTX_get0_trusted(ctx), cert); } static int add_untrusted(OSSL_CMP_CTX *ctx, X509 *cert) { return X509_add_cert(OSSL_CMP_CTX_get0_untrusted(ctx), cert, X509_ADD_FLAG_UP_REF); } static int test_validate_msg_signature_partial_chain(int expired) { X509_STORE *ts; SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = srvcert; ts = OSSL_CMP_CTX_get0_trusted(fixture->cmp_ctx); fixture->expected = !expired; if (ts == NULL || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) || !add_trusted(fixture->cmp_ctx, srvcert)) { tear_down(fixture); fixture = NULL; } else { X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); if (expired) X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration); } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } static int test_validate_msg_signature_trusted_ok(void) { return test_validate_msg_signature_partial_chain(0); } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_signature_trusted_expired(void) { return test_validate_msg_signature_partial_chain(1); } #endif static int test_validate_msg_signature_srvcert(int bad_sig, int miss, int wrong) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = srvcert; fixture->expected = !bad_sig && !wrong && !miss; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) || !TEST_true(miss ? OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_1, sizeof(sec_1)) : OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, wrong? clcert : srvcert)) || (bad_sig && !flip_bit(fixture->msg->protection))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_signature_srvcert_missing(void) { return test_validate_msg_signature_srvcert(0, 1, 0); } #endif static int test_validate_msg_signature_srvcert_wrong(void) { return test_validate_msg_signature_srvcert(0, 0, 1); } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_signature_bad(void) { return test_validate_msg_signature_srvcert(1, 0, 0); } #endif static int test_validate_msg_signature_sender_cert_srvcert(void) { return test_validate_msg_signature_srvcert(0, 0, 0); } static int test_validate_msg_signature_sender_cert_untrusted(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = insta_cert; fixture->expected = 1; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx)) || !add_trusted(fixture->cmp_ctx, instaca_cert) || !add_untrusted(fixture->cmp_ctx, insta_cert)) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } static int test_validate_msg_signature_sender_cert_trusted(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = insta_cert; fixture->expected = 1; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx)) || !add_trusted(fixture->cmp_ctx, instaca_cert) || !add_trusted(fixture->cmp_ctx, insta_cert)) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } static int test_validate_msg_signature_sender_cert_extracert(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_2_extracerts, libctx)) || !add_trusted(fixture->cmp_ctx, instaca_cert)) { tear_down(fixture); fixture = NULL; } fixture->cert = sk_X509_value(fixture->msg->extraCerts, 1); /* Insta CA */ EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_signature_sender_cert_absent(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->expected = 0; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } #endif static int test_validate_with_sender(const X509_NAME *name, int expected) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->cert = srvcert; fixture->expected = expected; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) || !TEST_true(OSSL_CMP_CTX_set1_expected_sender(fixture->cmp_ctx, name)) || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } static int test_validate_msg_signature_expected_sender(void) { return test_validate_with_sender(X509_get_subject_name(srvcert), 1); } static int test_validate_msg_signature_unexpected_sender(void) { return test_validate_with_sender(X509_get_subject_name(root), 0); } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_validate_msg_unprotected_request(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); fixture->expected = 0; if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_validate_msg_test, tear_down); return result; } #endif static void setup_path(CMP_VFY_TEST_FIXTURE **fixture, X509 *wrong, int expired) { (*fixture)->cert = endentity2; (*fixture)->expected = wrong == NULL && !expired; if (expired) { X509_STORE *ts = OSSL_CMP_CTX_get0_trusted((*fixture)->cmp_ctx); X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration); } if (!add_trusted((*fixture)->cmp_ctx, wrong == NULL ? root : wrong) || !add_untrusted((*fixture)->cmp_ctx, endentity1) || !add_untrusted((*fixture)->cmp_ctx, intermediate)) { tear_down((*fixture)); (*fixture) = NULL; } } static int test_validate_cert_path_ok(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_path(&fixture, NULL, 0); EXECUTE_TEST(execute_validate_cert_path_test, tear_down); return result; } static int test_validate_cert_path_wrong_anchor(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_path(&fixture, srvcert /* wrong/non-root cert */, 0); EXECUTE_TEST(execute_validate_cert_path_test, tear_down); return result; } static int test_validate_cert_path_expired(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_path(&fixture, NULL, 1); EXECUTE_TEST(execute_validate_cert_path_test, tear_down); return result; } static int execute_msg_check_test(CMP_VFY_TEST_FIXTURE *fixture) { const OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(fixture->msg); const ASN1_OCTET_STRING *tid = OSSL_CMP_HDR_get0_transactionID(hdr); if (!TEST_int_eq(fixture->expected, ossl_cmp_msg_check_update(fixture->cmp_ctx, fixture->msg, fixture->allow_unprotected_cb, fixture->additional_arg))) return 0; if (fixture->expected == 0) /* error expected already during above check */ return 1; return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(ossl_cmp_hdr_get0_senderNonce(hdr), fixture->cmp_ctx->recipNonce)) && TEST_int_eq(0, ASN1_OCTET_STRING_cmp(tid, fixture->cmp_ctx->transactionID)); } static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg, int invalid_protection, int allow) { return allow; } static void setup_check_update(CMP_VFY_TEST_FIXTURE **fixture, int expected, ossl_cmp_allow_unprotected_cb_t cb, int arg, const unsigned char *trid_data, const unsigned char *nonce_data) { OSSL_CMP_CTX *ctx = (*fixture)->cmp_ctx; int nonce_len = OSSL_CMP_SENDERNONCE_LENGTH; (*fixture)->expected = expected; (*fixture)->allow_unprotected_cb = cb; (*fixture)->additional_arg = arg; (*fixture)->msg = OSSL_CMP_MSG_dup(ir_rmprotection); if ((*fixture)->msg == NULL || (nonce_data != NULL && !ossl_cmp_asn1_octet_string_set1_bytes(&ctx->senderNonce, nonce_data, nonce_len))) { tear_down((*fixture)); (*fixture) = NULL; } else if (trid_data != NULL) { ASN1_OCTET_STRING *trid = ASN1_OCTET_STRING_new(); if (trid == NULL || !ASN1_OCTET_STRING_set(trid, trid_data, OSSL_CMP_TRANSACTIONID_LENGTH) || !OSSL_CMP_CTX_set1_transactionID(ctx, trid)) { tear_down((*fixture)); (*fixture) = NULL; } ASN1_OCTET_STRING_free(trid); } } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_msg_check_no_protection_no_cb(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 0, NULL, 0, NULL, NULL); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } static int test_msg_check_no_protection_restrictive_cb(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 0, allow_unprotected, 0, NULL, NULL); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } #endif static int test_msg_check_no_protection_permissive_cb(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, NULL); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } static int test_msg_check_transaction_id(void) { /* Transaction id belonging to CMP_IR_rmprotection.der */ const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = { 0x39, 0xB6, 0x90, 0x28, 0xC4, 0xBC, 0x7A, 0xF6, 0xBE, 0xC6, 0x4A, 0x88, 0x97, 0xA6, 0x95, 0x0B }; SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 1, allow_unprotected, 1, trans_id, NULL); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_msg_check_transaction_id_bad(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 0, allow_unprotected, 1, rand_data, NULL); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } #endif static int test_msg_check_recipient_nonce(void) { /* Recipient nonce belonging to CMP_IP_ir_rmprotection.der */ const unsigned char rec_nonce[OSSL_CMP_SENDERNONCE_LENGTH] = { 0x48, 0xF1, 0x71, 0x1F, 0xE5, 0xAF, 0x1C, 0x8B, 0x21, 0x97, 0x5C, 0x84, 0x74, 0x49, 0xBA, 0x32 }; SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, rec_nonce); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION static int test_msg_check_recipient_nonce_bad(void) { SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); setup_check_update(&fixture, 0, allow_unprotected, 1, NULL, rand_data); EXECUTE_TEST(execute_msg_check_test, tear_down); return result; } #endif void cleanup_tests(void) { X509_free(srvcert); X509_free(clcert); X509_free(endentity1); X509_free(endentity2); X509_free(intermediate); X509_free(root); X509_free(insta_cert); X509_free(instaca_cert); OSSL_CMP_MSG_free(ir_unprotected); OSSL_CMP_MSG_free(ir_rmprotection); OSSL_PROVIDER_unload(default_null_provider); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); return; } #define USAGE "server.crt client.crt " \ "EndEntity1.crt EndEntity2.crt " \ "Root_CA.crt Intermediate_CA.crt " \ "CMP_IR_protected.der CMP_IR_unprotected.der " \ "IP_waitingStatus_PBM.der IR_rmprotection.der " \ "insta.cert.pem insta_ca.cert.pem " \ "IR_protected_0_extraCerts.der " \ "IR_protected_2_extraCerts.der module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { /* Set test time stamps */ struct tm ts = { 0 }; ts.tm_year = 2018 - 1900; /* 2018 */ ts.tm_mon = 1; /* February */ ts.tm_mday = 18; /* 18th */ test_time_valid = mktime(&ts); /* February 18th 2018 */ ts.tm_year += 10; /* February 18th 2028 */ test_time_after_expiration = mktime(&ts); if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); if (!TEST_ptr(server_f = test_get_argument(0)) || !TEST_ptr(client_f = test_get_argument(1)) || !TEST_ptr(endentity1_f = test_get_argument(2)) || !TEST_ptr(endentity2_f = test_get_argument(3)) || !TEST_ptr(root_f = test_get_argument(4)) || !TEST_ptr(intermediate_f = test_get_argument(5)) || !TEST_ptr(ir_protected_f = test_get_argument(6)) || !TEST_ptr(ir_unprotected_f = test_get_argument(7)) || !TEST_ptr(ip_waiting_f = test_get_argument(8)) || !TEST_ptr(ir_rmprotection_f = test_get_argument(9)) || !TEST_ptr(instacert_f = test_get_argument(10)) || !TEST_ptr(instaca_f = test_get_argument(11)) || !TEST_ptr(ir_protected_0_extracerts = test_get_argument(12)) || !TEST_ptr(ir_protected_2_extracerts = test_get_argument(13))) { TEST_error("usage: cmp_vfy_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 14, USAGE)) return 0; /* Load certificates for cert chain */ if (!TEST_ptr(endentity1 = load_cert_pem(endentity1_f, libctx)) || !TEST_ptr(endentity2 = load_cert_pem(endentity2_f, libctx)) || !TEST_ptr(root = load_cert_pem(root_f, NULL)) || !TEST_ptr(intermediate = load_cert_pem(intermediate_f, libctx))) goto err; if (!TEST_ptr(insta_cert = load_cert_pem(instacert_f, libctx)) || !TEST_ptr(instaca_cert = load_cert_pem(instaca_f, libctx))) goto err; /* Load certificates for message validation */ if (!TEST_ptr(srvcert = load_cert_pem(server_f, libctx)) || !TEST_ptr(clcert = load_cert_pem(client_f, libctx))) goto err; if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH))) goto err; if (!TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f, libctx)) || !TEST_ptr(ir_rmprotection = load_pkimsg(ir_rmprotection_f, libctx))) goto err; /* Message validation tests */ ADD_TEST(test_verify_popo); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_verify_popo_bad); #endif ADD_TEST(test_validate_msg_signature_trusted_ok); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_validate_msg_signature_trusted_expired); ADD_TEST(test_validate_msg_signature_srvcert_missing); #endif ADD_TEST(test_validate_msg_signature_srvcert_wrong); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_validate_msg_signature_bad); #endif ADD_TEST(test_validate_msg_signature_sender_cert_srvcert); ADD_TEST(test_validate_msg_signature_sender_cert_untrusted); ADD_TEST(test_validate_msg_signature_sender_cert_trusted); ADD_TEST(test_validate_msg_signature_sender_cert_extracert); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_validate_msg_signature_sender_cert_absent); #endif ADD_TEST(test_validate_msg_signature_expected_sender); ADD_TEST(test_validate_msg_signature_unexpected_sender); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_validate_msg_unprotected_request); #endif ADD_TEST(test_validate_msg_mac_alg_protection_ok); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_validate_msg_mac_alg_protection_missing); ADD_TEST(test_validate_msg_mac_alg_protection_wrong); ADD_TEST(test_validate_msg_mac_alg_protection_bad); #endif /* Cert path validation tests */ ADD_TEST(test_validate_cert_path_ok); ADD_TEST(test_validate_cert_path_expired); ADD_TEST(test_validate_cert_path_wrong_anchor); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_msg_check_no_protection_no_cb); ADD_TEST(test_msg_check_no_protection_restrictive_cb); #endif ADD_TEST(test_msg_check_no_protection_permissive_cb); ADD_TEST(test_msg_check_transaction_id); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_msg_check_transaction_id_bad); #endif ADD_TEST(test_msg_check_recipient_nonce); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ADD_TEST(test_msg_check_recipient_nonce_bad); #endif return 1; err: cleanup_tests(); return 0; }
./openssl/test/simpledynamic.h
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_TEST_SIMPLEDYNAMIC_H # define OSSL_TEST_SIMPLEDYNAMIC_H # include "crypto/dso_conf.h" # if defined(DSO_DLFCN) || defined(DSO_VMS) # include <dlfcn.h> # define SD_INIT NULL # ifdef DSO_VMS # define SD_SHLIB 0 # define SD_MODULE 0 # else # define SD_SHLIB (RTLD_GLOBAL|RTLD_LAZY) # define SD_MODULE (RTLD_LOCAL|RTLD_NOW) # endif typedef void *SD; typedef void *SD_SYM; # elif defined(DSO_WIN32) # include <windows.h> # define SD_INIT 0 # define SD_SHLIB 0 # define SD_MODULE 0 typedef HINSTANCE SD; typedef void *SD_SYM; # endif # if defined(DSO_DLFCN) || defined(DSO_WIN32) || defined(DSO_VMS) int sd_load(const char *filename, SD *sd, int type); int sd_sym(SD sd, const char *symname, SD_SYM *sym); int sd_close(SD lib); const char *sd_error(void); # endif #endif
./openssl/test/p_minimal.c
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * This is the most minimal provider imaginable. It can be loaded, and does * absolutely nothing else. */ #include <openssl/core.h> OSSL_provider_init_fn OSSL_provider_init; /* Check the function signature */ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *oin, const OSSL_DISPATCH **out, void **provctx) { return 1; }
./openssl/test/quic_fc_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/quic_fc.h" #include "internal/quic_error.h" #include "testutil.h" static int test_txfc(int is_stream) { int testresult = 0; QUIC_TXFC conn_txfc, stream_txfc, *txfc, *parent_txfc; if (!TEST_true(ossl_quic_txfc_init(&conn_txfc, 0))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_init(&stream_txfc, &conn_txfc))) goto err; txfc = is_stream ? &stream_txfc : &conn_txfc; parent_txfc = is_stream ? &conn_txfc : NULL; if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2000))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2000))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 0)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 100), 1900)) goto err; if (is_stream) { if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 2000)) goto err; if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 100), 1900)) goto err; } if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 500))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1500)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1500)) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 500)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 100))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 600)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1400)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1400)) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1400))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 0)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 0)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_false(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_false(ossl_quic_txfc_bump_cwm(txfc, 2000))) goto err; if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2500))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2400))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2500)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 500)) goto err; if (is_stream) ossl_quic_txfc_has_become_blocked(parent_txfc, 1); if (is_stream) { if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 400), 0)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 399))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; } else { if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 499))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (is_stream && !TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; } testresult = 1; err: return testresult; } static OSSL_TIME cur_time; static OSSL_TIME fake_now(void *arg) { return cur_time; } #define RX_OPC_END 0 #define RX_OPC_INIT_CONN 1 /* arg0=initial window, arg1=max window */ #define RX_OPC_INIT_STREAM 2 /* arg0=initial window, arg1=max window */ #define RX_OPC_RX 3 /* arg0=end, arg1=is_fin */ #define RX_OPC_RETIRE 4 /* arg0=num_bytes, arg1=rtt in OSSL_TIME ticks, expect_fail */ #define RX_OPC_CHECK_CWM_CONN 5 /* arg0=expected */ #define RX_OPC_CHECK_CWM_STREAM 6 /* arg0=expected */ #define RX_OPC_CHECK_SWM_CONN 7 /* arg0=expected */ #define RX_OPC_CHECK_SWM_STREAM 8 /* arg0=expected */ #define RX_OPC_CHECK_RWM_CONN 9 /* arg0=expected */ #define RX_OPC_CHECK_RWM_STREAM 10 /* arg0=expected */ #define RX_OPC_CHECK_CHANGED_CONN 11 /* arg0=expected, arg1=clear */ #define RX_OPC_CHECK_CHANGED_STREAM 12 /* arg0=expected, arg1=clear */ #define RX_OPC_CHECK_ERROR_CONN 13 /* arg0=expected, arg1=clear */ #define RX_OPC_CHECK_ERROR_STREAM 14 /* arg0=expected, arg1=clear */ #define RX_OPC_STEP_TIME 15 /* arg0=OSSL_TIME ticks to advance */ #define RX_OPC_MSG 16 struct rx_test_op { unsigned char op; size_t stream_idx; uint64_t arg0, arg1; unsigned char expect_fail; const char *msg; }; #define RX_OP_END \ { RX_OPC_END } #define RX_OP_INIT_CONN(init_window_size, max_window_size) \ { RX_OPC_INIT_CONN, 0, (init_window_size), (max_window_size) }, #define RX_OP_INIT_STREAM(stream_idx, init_window_size, max_window_size) \ { RX_OPC_INIT_STREAM, (stream_idx), (init_window_size), (max_window_size) }, #define RX_OP_RX(stream_idx, end, is_fin) \ { RX_OPC_RX, (stream_idx), (end), (is_fin) }, #define RX_OP_RETIRE(stream_idx, num_bytes, rtt, expect_fail) \ { RX_OPC_RETIRE, (stream_idx), (num_bytes), (rtt), (expect_fail) }, #define RX_OP_CHECK_CWM_CONN(expected) \ { RX_OPC_CHECK_CWM_CONN, 0, (expected) }, #define RX_OP_CHECK_CWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_CWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_SWM_CONN(expected) \ { RX_OPC_CHECK_SWM_CONN, 0, (expected) }, #define RX_OP_CHECK_SWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_SWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_RWM_CONN(expected) \ { RX_OPC_CHECK_RWM_CONN, 0, (expected) }, #define RX_OP_CHECK_RWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_RWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_CHANGED_CONN(expected, clear) \ { RX_OPC_CHECK_CHANGED_CONN, 0, (expected), (clear) }, #define RX_OP_CHECK_CHANGED_STREAM(stream_id, expected, clear) \ { RX_OPC_CHECK_CHANGED_STREAM, (stream_id), (expected), (clear) }, #define RX_OP_CHECK_ERROR_CONN(expected, clear) \ { RX_OPC_CHECK_ERROR_CONN, 0, (expected), (clear) }, #define RX_OP_CHECK_ERROR_STREAM(stream_id, expected, clear) \ { RX_OPC_CHECK_ERROR_STREAM, (stream_id), (expected), (clear) }, #define RX_OP_STEP_TIME(t) \ { RX_OPC_STEP_TIME, 0, (t) }, #define RX_OP_MSG(msg) \ { RX_OPC_MSG, 0, 0, 0, 0, (msg) }, #define RX_OP_INIT(init_window_size, max_window_size) \ RX_OP_INIT_CONN(init_window_size, max_window_size) \ RX_OP_INIT_STREAM(0, init_window_size, max_window_size) #define RX_OP_CHECK_CWM(expected) \ RX_OP_CHECK_CWM_CONN(expected) \ RX_OP_CHECK_CWM_STREAM(0, expected) #define RX_OP_CHECK_SWM(expected) \ RX_OP_CHECK_SWM_CONN(expected) \ RX_OP_CHECK_SWM_STREAM(0, expected) #define RX_OP_CHECK_RWM(expected) \ RX_OP_CHECK_RWM_CONN(expected) \ RX_OP_CHECK_RWM_STREAM(0, expected) #define RX_OP_CHECK_CHANGED(expected, clear) \ RX_OP_CHECK_CHANGED_CONN(expected, clear) \ RX_OP_CHECK_CHANGED_STREAM(0, expected, clear) #define RX_OP_CHECK_ERROR(expected, clear) \ RX_OP_CHECK_ERROR_CONN(expected, clear) \ RX_OP_CHECK_ERROR_STREAM(0, expected, clear) #define INIT_WINDOW_SIZE (1 * 1024 * 1024) #define INIT_S_WINDOW_SIZE (384 * 1024) /* 1. Basic RXFC Tests (stream window == connection window) */ static const struct rx_test_op rx_script_1[] = { RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_INIT(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE) /* Check initial state. */ RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CHANGED(0, 0) /* We cannot retire what we have not received. */ RX_OP_RETIRE(0, 1, 0, 1) /* Zero bytes is a no-op and always valid. */ RX_OP_RETIRE(0, 0, 0, 0) /* Consume some window. */ RX_OP_RX(0, 50, 0) /* CWM has not changed. */ RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_SWM(50) /* RX, Partial retire */ RX_OP_RX(0, 60, 0) RX_OP_CHECK_SWM(60) RX_OP_RETIRE(0, 20, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM(20) RX_OP_CHECK_SWM(60) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) /* Fully retired */ RX_OP_RETIRE(0, 41, 0, 1) RX_OP_RETIRE(0, 40, 0, 0) RX_OP_CHECK_SWM(60) RX_OP_CHECK_RWM(60) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) /* Exhaustion of window - we do not enlarge the window this epoch */ RX_OP_STEP_TIME(201 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE - 60, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 2) /* Second epoch - we still do not enlarge the window this epoch */ RX_OP_RX(0, INIT_WINDOW_SIZE + 1, 0) RX_OP_STEP_TIME(201 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 2) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 3) /* Third epoch - we enlarge the window */ RX_OP_RX(0, INIT_WINDOW_SIZE * 2 + 1, 0) RX_OP_STEP_TIME(199 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 3, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 3) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5) /* Fourth epoch - peer violates flow control */ RX_OP_RX(0, INIT_WINDOW_SIZE * 5 - 5, 0) RX_OP_STEP_TIME(250 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 5 + 1, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 5) RX_OP_CHECK_ERROR(QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR(QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5) /* * No window expansion due to flow control violation; window expansion is * triggered by retirement only. */ RX_OP_CHECK_CHANGED(0, 0) RX_OP_END }; /* 2. Interaction between connection and stream-level flow control */ static const struct rx_test_op rx_script_2[] = { RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_INIT_CONN(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE) RX_OP_INIT_STREAM(0, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE) RX_OP_INIT_STREAM(1, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE) RX_OP_RX(0, 10, 0) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_CONN(10) RX_OP_CHECK_SWM_STREAM(0, 10) RX_OP_CHECK_SWM_STREAM(1, 0) RX_OP_CHECK_RWM_CONN(0) RX_OP_CHECK_RWM_STREAM(0, 0) RX_OP_CHECK_RWM_STREAM(1, 0) RX_OP_RX(1, 42, 0) RX_OP_RX(1, 42, 0) /* monotonic; equal or lower values ignored */ RX_OP_RX(1, 35, 0) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_CONN(52) RX_OP_CHECK_SWM_STREAM(0, 10) RX_OP_CHECK_SWM_STREAM(1, 42) RX_OP_CHECK_RWM_CONN(0) RX_OP_CHECK_RWM_STREAM(0, 0) RX_OP_CHECK_RWM_STREAM(1, 0) RX_OP_RETIRE(0, 10, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM_CONN(10) RX_OP_CHECK_RWM_STREAM(0, 10) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_RETIRE(1, 42, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM_CONN(52) RX_OP_CHECK_RWM_STREAM(1, 42) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CHANGED_CONN(0, 0) /* FC limited by stream but not connection */ RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_RX(0, INIT_S_WINDOW_SIZE, 0) RX_OP_CHECK_SWM_CONN(INIT_S_WINDOW_SIZE + 42) RX_OP_CHECK_SWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_STREAM(1, 42) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) /* We bump CWM when more than 1/4 of the window has been retired */ RX_OP_RETIRE(0, INIT_S_WINDOW_SIZE - 10, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE * 2) RX_OP_CHECK_CHANGED_STREAM(0, 1, 0) RX_OP_CHECK_CHANGED_STREAM(0, 1, 1) RX_OP_CHECK_CHANGED_STREAM(0, 0, 0) /* * This is more than 1/4 of the connection window, so CWM will * be bumped here too. */ RX_OP_CHECK_CWM_CONN(INIT_S_WINDOW_SIZE + INIT_WINDOW_SIZE + 42) RX_OP_CHECK_RWM_CONN(INIT_S_WINDOW_SIZE + 42) RX_OP_CHECK_RWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_RWM_STREAM(1, 42) RX_OP_CHECK_CHANGED_CONN(1, 0) RX_OP_CHECK_CHANGED_CONN(1, 1) RX_OP_CHECK_CHANGED_CONN(0, 0) RX_OP_CHECK_ERROR_CONN(0, 0) RX_OP_CHECK_ERROR_STREAM(0, 0, 0) RX_OP_CHECK_ERROR_STREAM(1, 0, 0) /* Test exceeding limit at stream level. */ RX_OP_RX(0, INIT_S_WINDOW_SIZE * 2 + 1, 0) RX_OP_CHECK_ERROR_STREAM(0, QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR_STREAM(0, QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR_STREAM(0, 0, 0) RX_OP_CHECK_ERROR_CONN(0, 0) /* doesn't affect conn */ /* Test exceeding limit at connection level. */ RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0) RX_OP_CHECK_ERROR_CONN(QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR_CONN(QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR_CONN(0, 0) RX_OP_END }; static const struct rx_test_op *rx_scripts[] = { rx_script_1, rx_script_2 }; static int run_rxfc_script(const struct rx_test_op *script) { #define MAX_STREAMS 3 int testresult = 0; const struct rx_test_op *op = script; QUIC_RXFC conn_rxfc = {0}, stream_rxfc[MAX_STREAMS] = {0}; /* coverity */ char stream_init_done[MAX_STREAMS] = {0}; int conn_init_done = 0; cur_time = ossl_time_zero(); for (; op->op != RX_OPC_END; ++op) { switch (op->op) { case RX_OPC_INIT_CONN: if (!TEST_true(ossl_quic_rxfc_init(&conn_rxfc, 0, op->arg0, op->arg1, fake_now, NULL))) goto err; conn_init_done = 1; break; case RX_OPC_INIT_STREAM: if (!TEST_size_t_lt(op->stream_idx, OSSL_NELEM(stream_rxfc)) || !TEST_true(conn_init_done)) goto err; if (!TEST_true(ossl_quic_rxfc_init(&stream_rxfc[op->stream_idx], &conn_rxfc, op->arg0, op->arg1, fake_now, NULL))) goto err; stream_init_done[op->stream_idx] = 1; break; case RX_OPC_RX: if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_true(ossl_quic_rxfc_on_rx_stream_frame(&stream_rxfc[op->stream_idx], op->arg0, (int)op->arg1))) goto err; break; case RX_OPC_RETIRE: if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_on_retire(&stream_rxfc[op->stream_idx], op->arg0, ossl_ticks2time(op->arg1)), !op->expect_fail)) goto err; break; case RX_OPC_CHECK_CWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_CWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_SWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_SWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_RWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_RWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_CHANGED_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&conn_rxfc, (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_CHANGED_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&stream_rxfc[op->stream_idx], (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_ERROR_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_int_eq(ossl_quic_rxfc_get_error(&conn_rxfc, (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_ERROR_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_get_error(&stream_rxfc[op->stream_idx], (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_STEP_TIME: cur_time = ossl_time_add(cur_time, ossl_ticks2time(op->arg0)); break; case RX_OPC_MSG: fprintf(stderr, "# %s\n", op->msg); break; default: goto err; } } testresult = 1; err: return testresult; } static int test_rxfc(int idx) { return run_rxfc_script(rx_scripts[idx]); } int setup_tests(void) { ADD_ALL_TESTS(test_txfc, 2); ADD_ALL_TESTS(test_rxfc, OSSL_NELEM(rx_scripts)); return 1; }
./openssl/test/defltfips_test.c
/* * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/evp.h> #include <openssl/provider.h> #include "testutil.h" static int is_fips; static int bad_fips; static int test_is_fips_enabled(void) { int is_fips_enabled, is_fips_loaded; EVP_MD *sha256 = NULL; /* * Check we're in FIPS mode when we're supposed to be. We do this early to * confirm that EVP_default_properties_is_fips_enabled() works even before * other function calls have auto-loaded the config file. */ is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL); is_fips_loaded = OSSL_PROVIDER_available(NULL, "fips"); /* * Check we're in an expected state. EVP_default_properties_is_fips_enabled * can return true even if the FIPS provider isn't loaded - it is only based * on the default properties. However we only set those properties if also * loading the FIPS provider. */ if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled) || !TEST_int_eq(is_fips && !bad_fips, is_fips_loaded)) return 0; /* * Fetching an algorithm shouldn't change the state and should come from * expected provider. */ sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL); if (bad_fips) { if (!TEST_ptr_null(sha256)) { EVP_MD_free(sha256); return 0; } } else { if (!TEST_ptr(sha256)) return 0; if (is_fips && !TEST_str_eq(OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(sha256)), "fips")) { EVP_MD_free(sha256); return 0; } EVP_MD_free(sha256); } /* State should still be consistent */ is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL); if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled)) return 0; return 1; } int setup_tests(void) { size_t argc; char *arg1; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } argc = test_get_argument_count(); switch (argc) { case 0: is_fips = 0; bad_fips = 0; break; case 1: arg1 = test_get_argument(0); if (strcmp(arg1, "fips") == 0) { is_fips = 1; bad_fips = 0; break; } else if (strcmp(arg1, "badfips") == 0) { /* Configured for FIPS, but the module fails to load */ is_fips = 0; bad_fips = 1; break; } /* fall through */ default: TEST_error("Invalid argument\n"); return 0; } /* Must be the first test before any other libcrypto calls are made */ ADD_TEST(test_is_fips_enabled); return 1; }
./openssl/test/dtlsv1listentest.c
/* * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/ssl.h> #include <openssl/bio.h> #include <openssl/err.h> #include <openssl/conf.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_SOCK /* Just a ClientHello without a cookie */ static const unsigned char clienthello_nocookie[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x3A, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x2E, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x2E, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x00, /* Cookie len */ 0x00, 0x04, /* Ciphersuites len */ 0x00, 0x2f, /* AES128-SHA */ 0x00, 0xff, /* Empty reneg info SCSV */ 0x01, /* Compression methods len */ 0x00, /* Null compression */ 0x00, 0x00 /* Extensions len */ }; /* First fragment of a ClientHello without a cookie */ static const unsigned char clienthello_nocookie_frag[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x30, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x2E, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x24, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x00 /* Cookie len */ }; /* First fragment of a ClientHello which is too short */ static const unsigned char clienthello_nocookie_short[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x2F, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x2E, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x23, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00 /* Session id len */ }; /* Second fragment of a ClientHello */ static const unsigned char clienthello_2ndfrag[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x38, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x2E, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x02, /* Fragment offset */ 0x00, 0x00, 0x2C, /* Fragment length */ /* Version skipped - sent in first fragment */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x00, /* Cookie len */ 0x00, 0x04, /* Ciphersuites len */ 0x00, 0x2f, /* AES128-SHA */ 0x00, 0xff, /* Empty reneg info SCSV */ 0x01, /* Compression methods len */ 0x00, /* Null compression */ 0x00, 0x00 /* Extensions len */ }; /* A ClientHello with a good cookie */ static const unsigned char clienthello_cookie[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x4E, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x42, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x42, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x14, /* Cookie len */ 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* Cookie */ 0x00, 0x04, /* Ciphersuites len */ 0x00, 0x2f, /* AES128-SHA */ 0x00, 0xff, /* Empty reneg info SCSV */ 0x01, /* Compression methods len */ 0x00, /* Null compression */ 0x00, 0x00 /* Extensions len */ }; /* A fragmented ClientHello with a good cookie */ static const unsigned char clienthello_cookie_frag[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x44, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x42, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x38, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x14, /* Cookie len */ 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 /* Cookie */ }; /* A ClientHello with a bad cookie */ static const unsigned char clienthello_badcookie[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x4E, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x42, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x42, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x14, /* Cookie len */ 0x01, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* Cookie */ 0x00, 0x04, /* Ciphersuites len */ 0x00, 0x2f, /* AES128-SHA */ 0x00, 0xff, /* Empty reneg info SCSV */ 0x01, /* Compression methods len */ 0x00, /* Null compression */ 0x00, 0x00 /* Extensions len */ }; /* A fragmented ClientHello with the fragment boundary mid cookie */ static const unsigned char clienthello_cookie_short[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x43, /* Record Length */ 0x01, /* ClientHello */ 0x00, 0x00, 0x42, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x37, /* Fragment length */ 0xFE, 0xFD, /* DTLSv1.2 */ 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */ 0x00, /* Session id len */ 0x14, /* Cookie len */ 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12 /* Cookie */ }; /* Bad record - too short */ static const unsigned char record_short[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* Record sequence number */ }; static const unsigned char verify[] = { 0x16, /* Handshake */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x00, 0x00, /* Epoch */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */ 0x00, 0x23, /* Record Length */ 0x03, /* HelloVerifyRequest */ 0x00, 0x00, 0x17, /* Message length */ 0x00, 0x00, /* Message sequence */ 0x00, 0x00, 0x00, /* Fragment offset */ 0x00, 0x00, 0x17, /* Fragment length */ 0xFE, 0xFF, /* DTLSv1.0 */ 0x14, /* Cookie len */ 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 /* Cookie */ }; typedef struct { const unsigned char *in; unsigned int inlen; /* * GOOD == positive return value from DTLSv1_listen, no output yet * VERIFY == 0 return value, HelloVerifyRequest sent * DROP == 0 return value, no output */ enum {GOOD, VERIFY, DROP} outtype; } tests; static tests testpackets[9] = { { clienthello_nocookie, sizeof(clienthello_nocookie), VERIFY }, { clienthello_nocookie_frag, sizeof(clienthello_nocookie_frag), VERIFY }, { clienthello_nocookie_short, sizeof(clienthello_nocookie_short), DROP }, { clienthello_2ndfrag, sizeof(clienthello_2ndfrag), DROP }, { clienthello_cookie, sizeof(clienthello_cookie), GOOD }, { clienthello_cookie_frag, sizeof(clienthello_cookie_frag), GOOD }, { clienthello_badcookie, sizeof(clienthello_badcookie), VERIFY }, { clienthello_cookie_short, sizeof(clienthello_cookie_short), DROP }, { record_short, sizeof(record_short), DROP } }; # define COOKIE_LEN 20 static int cookie_gen(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) { unsigned int i; for (i = 0; i < COOKIE_LEN; i++, cookie++) *cookie = i; *cookie_len = COOKIE_LEN; return 1; } static int cookie_verify(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len) { unsigned int i; if (cookie_len != COOKIE_LEN) return 0; for (i = 0; i < COOKIE_LEN; i++, cookie++) { if (*cookie != i) return 0; } return 1; } static int dtls_listen_test(int i) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; BIO *outbio = NULL; BIO *inbio = NULL; BIO_ADDR *peer = NULL; tests *tp = &testpackets[i]; char *data; long datalen; int ret, success = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_server_method())) || !TEST_ptr(peer = BIO_ADDR_new())) goto err; SSL_CTX_set_cookie_generate_cb(ctx, cookie_gen); SSL_CTX_set_cookie_verify_cb(ctx, cookie_verify); /* Create an SSL object and set the BIO */ if (!TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(outbio = BIO_new(BIO_s_mem()))) goto err; SSL_set0_wbio(ssl, outbio); /* Set Non-blocking IO behaviour */ if (!TEST_ptr(inbio = BIO_new_mem_buf((char *)tp->in, tp->inlen))) goto err; BIO_set_mem_eof_return(inbio, -1); SSL_set0_rbio(ssl, inbio); /* Process the incoming packet */ if (!TEST_int_ge(ret = DTLSv1_listen(ssl, peer), 0)) goto err; datalen = BIO_get_mem_data(outbio, &data); if (tp->outtype == VERIFY) { if (!TEST_int_eq(ret, 0) || !TEST_mem_eq(data, datalen, verify, sizeof(verify))) goto err; } else if (datalen == 0) { if (!TEST_true((ret == 0 && tp->outtype == DROP) || (ret == 1 && tp->outtype == GOOD))) goto err; } else { TEST_info("Test %d: unexpected data output", i); goto err; } (void)BIO_reset(outbio); inbio = NULL; SSL_set0_rbio(ssl, NULL); success = 1; err: /* Also frees up outbio */ SSL_free(ssl); SSL_CTX_free(ctx); BIO_free(inbio); OPENSSL_free(peer); return success; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_SOCK ADD_ALL_TESTS(dtls_listen_test, (int)OSSL_NELEM(testpackets)); #endif return 1; }
./openssl/test/sm3_internal_test.c
/* * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved. * Copyright 2021 UnionTech. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Internal tests for the SM3 module. */ #include <string.h> #include <openssl/opensslconf.h> #include "testutil.h" #ifndef OPENSSL_NO_SM3 # include "internal/sm3.h" static int test_sm3(void) { static const unsigned char input1[] = { 0x61, 0x62, 0x63 }; /* * This test vector comes from Example 1 (A.1) of GM/T 0004-2012 */ static const unsigned char expected1[SM3_DIGEST_LENGTH] = { 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2, 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0 }; static const unsigned char input2[] = { 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64 }; /* * This test vector comes from Example 2 (A.2) from GM/T 0004-2012 */ static const unsigned char expected2[SM3_DIGEST_LENGTH] = { 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d, 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65, 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32 }; SM3_CTX ctx1, ctx2; unsigned char md1[SM3_DIGEST_LENGTH], md2[SM3_DIGEST_LENGTH]; if (!TEST_true(ossl_sm3_init(&ctx1)) || !TEST_true(ossl_sm3_update(&ctx1, input1, sizeof(input1))) || !TEST_true(ossl_sm3_final(md1, &ctx1)) || !TEST_mem_eq(md1, SM3_DIGEST_LENGTH, expected1, SM3_DIGEST_LENGTH)) return 0; if (!TEST_true(ossl_sm3_init(&ctx2)) || !TEST_true(ossl_sm3_update(&ctx2, input2, sizeof(input2))) || !TEST_true(ossl_sm3_final(md2, &ctx2)) || !TEST_mem_eq(md2, SM3_DIGEST_LENGTH, expected2, SM3_DIGEST_LENGTH)) return 0; return 1; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_SM3 ADD_TEST(test_sm3); #endif return 1; }
./openssl/test/mdc2test.c
/* * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * MDC2 low level APIs are deprecated for public use, but still ok for * internal use. */ #include "internal/deprecated.h" #include <string.h> #include <openssl/provider.h> #include <openssl/params.h> #include <openssl/types.h> #include <openssl/core_names.h> #include "testutil.h" #if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2) # define OPENSSL_NO_MDC2 #endif #ifndef OPENSSL_NO_MDC2 # include <openssl/evp.h> # include <openssl/mdc2.h> # ifdef CHARSET_EBCDIC # include <openssl/ebcdic.h> # endif static unsigned char pad1[16] = { 0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA, 0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A }; static unsigned char pad2[16] = { 0x2E, 0x46, 0x79, 0xB5, 0xAD, 0xD9, 0xCA, 0x75, 0x35, 0xD8, 0x7A, 0xFE, 0xAB, 0x33, 0xBE, 0xE2 }; static int test_mdc2(void) { int testresult = 0; unsigned int pad_type = 2; unsigned char md[MDC2_DIGEST_LENGTH]; EVP_MD_CTX *c = NULL; static char text[] = "Now is the time for all "; size_t tlen = strlen(text), i = 0; OSSL_PROVIDER *prov = NULL; OSSL_PARAM params[2]; params[i++] = OSSL_PARAM_construct_uint(OSSL_DIGEST_PARAM_PAD_TYPE, &pad_type), params[i++] = OSSL_PARAM_construct_end(); prov = OSSL_PROVIDER_load(NULL, "legacy"); if (!TEST_ptr(prov)) goto end; # ifdef CHARSET_EBCDIC ebcdic2ascii(text, text, tlen); # endif c = EVP_MD_CTX_new(); if (!TEST_ptr(c) || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)) || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen)) || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL)) || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH) || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))) goto end; if (!TEST_int_gt(EVP_MD_CTX_set_params(c, params), 0) || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen)) || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL)) || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH)) goto end; testresult = 1; end: EVP_MD_CTX_free(c); OSSL_PROVIDER_unload(prov); return testresult; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_MDC2 ADD_TEST(test_mdc2); #endif return 1; }
./openssl/test/cmp_hdr_test.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2019 * Copyright Siemens AG 2015-2019 * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "helpers/cmp_testlib.h" static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; typedef struct test_fixture { const char *test_case_name; int expected; OSSL_CMP_CTX *cmp_ctx; OSSL_CMP_PKIHEADER *hdr; } CMP_HDR_TEST_FIXTURE; static void tear_down(CMP_HDR_TEST_FIXTURE *fixture) { OSSL_CMP_PKIHEADER_free(fixture->hdr); OSSL_CMP_CTX_free(fixture->cmp_ctx); OPENSSL_free(fixture); } static CMP_HDR_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_HDR_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(NULL, NULL))) goto err; if (!TEST_ptr(fixture->hdr = OSSL_CMP_PKIHEADER_new())) goto err; return fixture; err: tear_down(fixture); return NULL; } static int execute_HDR_set_get_pvno_test(CMP_HDR_TEST_FIXTURE *fixture) { int pvno = 77; if (!TEST_int_eq(ossl_cmp_hdr_set_pvno(fixture->hdr, pvno), 1)) return 0; if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), pvno)) return 0; return 1; } static int test_HDR_set_get_pvno(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_set_get_pvno_test, tear_down); return result; } #define X509_NAME_ADD(n, rd, s) \ X509_NAME_add_entry_by_txt((n), (rd), MBSTRING_ASC, (unsigned char *)(s), \ -1, -1, 0) static int execute_HDR_get0_senderNonce_test(CMP_HDR_TEST_FIXTURE *fixture) { X509_NAME *sender = X509_NAME_new(); ASN1_OCTET_STRING *sn; if (!TEST_ptr(sender)) return 0; X509_NAME_ADD(sender, "CN", "A common sender name"); if (!TEST_int_eq(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, sender), 1)) return 0; if (!TEST_int_eq(ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr), 1)) return 0; sn = ossl_cmp_hdr_get0_senderNonce(fixture->hdr); if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->cmp_ctx->senderNonce, sn), 0)) return 0; X509_NAME_free(sender); return 1; } static int test_HDR_get0_senderNonce(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_get0_senderNonce_test, tear_down); return result; } static int execute_HDR_set1_sender_test(CMP_HDR_TEST_FIXTURE *fixture) { X509_NAME *x509name = X509_NAME_new(); if (!TEST_ptr(x509name)) return 0; X509_NAME_ADD(x509name, "CN", "A common sender name"); if (!TEST_int_eq(ossl_cmp_hdr_set1_sender(fixture->hdr, x509name), 1)) return 0; if (!TEST_int_eq(fixture->hdr->sender->type, GEN_DIRNAME)) return 0; if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->sender->d.directoryName, x509name), 0)) return 0; X509_NAME_free(x509name); return 1; } static int test_HDR_set1_sender(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_set1_sender_test, tear_down); return result; } static int execute_HDR_set1_recipient_test(CMP_HDR_TEST_FIXTURE *fixture) { X509_NAME *x509name = X509_NAME_new(); if (!TEST_ptr(x509name)) return 0; X509_NAME_ADD(x509name, "CN", "A common recipient name"); if (!TEST_int_eq(ossl_cmp_hdr_set1_recipient(fixture->hdr, x509name), 1)) return 0; if (!TEST_int_eq(fixture->hdr->recipient->type, GEN_DIRNAME)) return 0; if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->recipient->d.directoryName, x509name), 0)) return 0; X509_NAME_free(x509name); return 1; } static int test_HDR_set1_recipient(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_set1_recipient_test, tear_down); return result; } static int execute_HDR_update_messageTime_test(CMP_HDR_TEST_FIXTURE *fixture) { struct tm hdrtm, tmptm; time_t hdrtime, before, after, now; now = time(NULL); /* * Trial and error reveals that passing the return value from gmtime * directly to mktime in a mingw 32 bit build gives unexpected results. To * work around this we take a copy of the return value first. */ tmptm = *gmtime(&now); before = mktime(&tmptm); if (!TEST_true(ossl_cmp_hdr_update_messageTime(fixture->hdr))) return 0; if (!TEST_true(ASN1_TIME_to_tm(fixture->hdr->messageTime, &hdrtm))) return 0; hdrtime = mktime(&hdrtm); if (!TEST_time_t_le(before, hdrtime)) return 0; now = time(NULL); tmptm = *gmtime(&now); after = mktime(&tmptm); return TEST_time_t_le(hdrtime, after); } static int test_HDR_update_messageTime(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_update_messageTime_test, tear_down); return result; } static int execute_HDR_set1_senderKID_test(CMP_HDR_TEST_FIXTURE *fixture) { ASN1_OCTET_STRING *senderKID = ASN1_OCTET_STRING_new(); int res = 0; if (!TEST_ptr(senderKID)) return 0; if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data, sizeof(rand_data)), 1)) goto err; if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1)) goto err; if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID, senderKID), 0)) goto err; res = 1; err: ASN1_OCTET_STRING_free(senderKID); return res; } static int test_HDR_set1_senderKID(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down); return result; } static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture) { ASN1_UTF8STRING *text = ASN1_UTF8STRING_new(); if (!TEST_ptr(text)) return 0; if (!ASN1_STRING_set(text, "A free text", -1)) goto err; if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1)) goto err; if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0))) goto err; return 1; err: ASN1_UTF8STRING_free(text); return 0; } static int test_HDR_push0_freeText(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down); return result; } static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture) { ASN1_UTF8STRING *text = ASN1_UTF8STRING_new(); ASN1_UTF8STRING *pushed_text; int res = 0; if (!TEST_ptr(text)) return 0; if (!ASN1_STRING_set(text, "A free text", -1)) goto err; if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1)) goto err; pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0); if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0)) goto err; res = 1; err: ASN1_UTF8STRING_free(text); return res; } static int test_HDR_push1_freeText(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down); return result; } static int execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture) { OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new(); if (!TEST_ptr(itav)) return 0; if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav), 1)) return 0; if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo, 0))) return 0; return 1; } static int test_HDR_generalInfo_push0_item(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down); return result; } static int execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture) { const char oid[] = "1.2.3.4"; char buf[20]; OSSL_CMP_ITAV *itav, *pushed_itav; STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo; ASN1_INTEGER *asn1int = ASN1_INTEGER_new(); ASN1_TYPE *val = ASN1_TYPE_new(); ASN1_TYPE *pushed_val; int res = 0; if (!TEST_ptr(asn1int)) return 0; if (!TEST_ptr(val) || !TEST_true(ASN1_INTEGER_set(asn1int, 88))) { ASN1_INTEGER_free(asn1int); return 0; } ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int); if (!TEST_ptr(itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val))) { ASN1_TYPE_free(val); return 0; } if (!TEST_true(OSSL_CMP_ITAV_push0_stack_item(&itavs, itav))) { OSSL_CMP_ITAV_free(itav); return 0; } if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs), 1)) goto err; ginfo = fixture->hdr->generalInfo; pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0); OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0); if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0)) goto err; pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0)); if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0)) goto err; res = 1; err: sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free); return res; } static int test_HDR_generalInfo_push1_items(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down); return result; } static int execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE * fixture) { return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr)) && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr)) && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr)); } static int test_HDR_set_and_check_implicit_confirm(void) { SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down); return result; } static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture) { ASN1_OCTET_STRING *header_nonce, *header_transactionID; ASN1_OCTET_STRING *ctx_nonce; if (!TEST_int_eq(fixture->expected, ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr))) return 0; if (fixture->expected == 0) return 1; if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO)) return 0; header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr); if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce, fixture->cmp_ctx->senderNonce))) return 0; header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr); if (!TEST_true(ASN1_OCTET_STRING_cmp(header_transactionID, fixture->cmp_ctx->transactionID) == 0)) return 0; header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr); ctx_nonce = fixture->cmp_ctx->recipNonce; if (ctx_nonce != NULL && (!TEST_ptr(header_nonce) || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce, ctx_nonce)))) return 0; return 1; } static int test_HDR_init_with_ref(void) { unsigned char ref[CMP_TEST_REFVALUE_LENGTH]; SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref))) || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, ref, sizeof(ref)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_HDR_init_test, tear_down); return result; } static int test_HDR_init_with_subject(void) { X509_NAME *subject = NULL; SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(subject = X509_NAME_new()) || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name")) || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, subject))) { tear_down(fixture); fixture = NULL; } X509_NAME_free(subject); EXECUTE_TEST(execute_HDR_init_test, tear_down); return result; } void cleanup_tests(void) { return; } int setup_tests(void) { RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); /* Message header tests */ ADD_TEST(test_HDR_set_get_pvno); ADD_TEST(test_HDR_get0_senderNonce); ADD_TEST(test_HDR_set1_sender); ADD_TEST(test_HDR_set1_recipient); ADD_TEST(test_HDR_update_messageTime); ADD_TEST(test_HDR_set1_senderKID); ADD_TEST(test_HDR_push0_freeText); /* indirectly tests ossl_cmp_pkifreetext_push_str(): */ ADD_TEST(test_HDR_push1_freeText); ADD_TEST(test_HDR_generalInfo_push0_item); ADD_TEST(test_HDR_generalInfo_push1_items); ADD_TEST(test_HDR_set_and_check_implicit_confirm); /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */ /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */ /* also tests internal function ossl_cmp_hdr_get_pvno(): */ ADD_TEST(test_HDR_init_with_ref); ADD_TEST(test_HDR_init_with_subject); return 1; }
./openssl/test/x509_test.c
/* * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/x509.h> #include "testutil.h" static EVP_PKEY *pubkey = NULL; static EVP_PKEY *privkey = NULL; static EVP_MD *signmd = NULL; /* EC key pair used for signing */ static const unsigned char privkeydata[] = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x7d, 0x2b, 0xfe, 0x5c, 0xcb, 0xcb, 0x27, 0xd6, 0x28, 0xfe, 0x98, 0x34, 0x84, 0x4a, 0x13, 0x6f, 0x70, 0xc4, 0x1a, 0x0b, 0xfc, 0xde, 0xb0, 0xb2, 0x32, 0xb1, 0xdd, 0x4f, 0x0e, 0xbc, 0xdf, 0x89, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d }; static const unsigned char pubkeydata[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d }; /* Self signed cert using ECDSA-SHA256 with the keypair listed above */ static const unsigned char certdata[] = { 0x30, 0x82, 0x01, 0x86, 0x30, 0x82, 0x01, 0x2d, 0x02, 0x14, 0x75, 0xd6, 0x04, 0xd2, 0x80, 0x61, 0xd3, 0x32, 0xbc, 0xae, 0x38, 0x58, 0xfe, 0x12, 0x42, 0x81, 0x7a, 0xdd, 0x0b, 0x99, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30, 0x37, 0x32, 0x37, 0x35, 0x35, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x35, 0x30, 0x30, 0x32, 0x32, 0x37, 0x30, 0x37, 0x32, 0x37, 0x35, 0x35, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x5f, 0x45, 0x7f, 0xa4, 0x6a, 0x03, 0xfd, 0xe7, 0xf3, 0x42, 0x43, 0x38, 0x5b, 0x81, 0x08, 0x1a, 0x47, 0x8e, 0x59, 0x3a, 0x28, 0x5b, 0x97, 0x67, 0x47, 0x66, 0x2a, 0x16, 0xf5, 0xce, 0xf5, 0x92, 0x02, 0x20, 0x22, 0x0e, 0xab, 0x35, 0xdf, 0x49, 0xb1, 0x86, 0xa3, 0x3b, 0x26, 0xda, 0x7e, 0x8b, 0x44, 0x45, 0xc6, 0x46, 0x14, 0x04, 0x22, 0x2b, 0xe5, 0x2a, 0x62, 0x84, 0xc5, 0x94, 0xa0, 0x1b, 0xaa, 0xa9 }; /* Some simple CRL data */ static const unsigned char crldata[] = { 0x30, 0x81, 0x8B, 0x30, 0x31, 0x02, 0x01, 0x01, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x05, 0x00, 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x04, 0x54, 0x65, 0x73, 0x74, 0x17, 0x0D, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30, 0x35, 0x33, 0x34, 0x30, 0x31, 0x5A, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x05, 0x00, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x75, 0xAC, 0xA9, 0xB5, 0xFE, 0x63, 0x09, 0x8B, 0x57, 0x4F, 0xBB, 0xC6, 0x0C, 0xA9, 0x9A, 0x7C, 0x55, 0x89, 0xF9, 0x9C, 0x48, 0xE9, 0xF3, 0xED, 0xE5, 0xC2, 0x88, 0xCE, 0xEC, 0xB1, 0x51, 0xF1, 0x02, 0x21, 0x00, 0x8B, 0x93, 0xC5, 0xA6, 0x28, 0x48, 0x5A, 0x4E, 0x10, 0x52, 0x82, 0x12, 0x2F, 0xC4, 0x62, 0x2D, 0x3F, 0x5A, 0x62, 0x7F, 0x9D, 0x1B, 0x12, 0xC5, 0x36, 0x25, 0x73, 0x03, 0xF4, 0xDE, 0x62, 0x24 }; /* * Test for Regression discussed in PR #19388 * In order for this simple test to fail, it requires the digest used for * signing to be different from the alg within the loaded cert. */ static int test_x509_tbs_cache(void) { int ret; X509 *x = NULL; const unsigned char *p = certdata; ret = TEST_ptr(x = d2i_X509(NULL, &p, sizeof(certdata))) && TEST_int_gt(X509_sign(x, privkey, signmd), 0) && TEST_int_eq(X509_verify(x, pubkey), 1); X509_free(x); return ret; } /* * Test for Regression discussed in PR #19388 * In order for this simple test to fail, it requires the digest used for * signing to be different from the alg within the loaded cert. */ static int test_x509_crl_tbs_cache(void) { int ret; X509_CRL *crl = NULL; const unsigned char *p = crldata; ret = TEST_ptr(crl = d2i_X509_CRL(NULL, &p, sizeof(crldata))) && TEST_int_gt(X509_CRL_sign(crl, privkey, signmd), 0) && TEST_int_eq(X509_CRL_verify(crl, pubkey), 1); X509_CRL_free(crl); return ret; } int setup_tests(void) { const unsigned char *p; p = pubkeydata; pubkey = d2i_PUBKEY(NULL, &p, sizeof(pubkeydata)); p = privkeydata; privkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(privkeydata)); if (pubkey == NULL || privkey == NULL) { BIO_printf(bio_err, "Failed to create keys\n"); return 0; } /* Note this digest is different from the certificate digest */ signmd = EVP_MD_fetch(NULL, "SHA384", NULL); if (signmd == NULL) { BIO_printf(bio_err, "Failed to fetch digest\n"); return 0; } ADD_TEST(test_x509_tbs_cache); ADD_TEST(test_x509_crl_tbs_cache); return 1; } void cleanup_tests(void) { EVP_MD_free(signmd); EVP_PKEY_free(pubkey); EVP_PKEY_free(privkey); }
./openssl/test/quic_cfq_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/packet.h" #include "internal/quic_cfq.h" #include "internal/quic_wire.h" #include "testutil.h" static const unsigned char ref_buf[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 }; static const uint32_t ref_priority[] = { 90, 80, 70, 60, 95, 40, 94, 20, 10, 0 }; static const uint32_t ref_pn_space[] = { QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_HANDSHAKE, QUIC_PN_SPACE_HANDSHAKE, QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_INITIAL, QUIC_PN_SPACE_APP, QUIC_PN_SPACE_APP, }; static const uint64_t ref_frame_type[] = { OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, }; static const uint32_t expect[QUIC_PN_SPACE_NUM][11] = { { 4, 6, 0, 3, 5, 7, UINT32_MAX }, { 1, 2, UINT32_MAX }, { 8, 9, UINT32_MAX }, }; static QUIC_CFQ_ITEM *items[QUIC_PN_SPACE_NUM][10]; static unsigned char *g_free; static size_t g_free_len; static void free_cb(unsigned char *buf, size_t buf_len, void *arg) { g_free = buf; g_free_len = buf_len; } static int check(QUIC_CFQ *cfq) { int testresult = 0; QUIC_CFQ_ITEM *item; size_t i; uint32_t pn_space; for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) for (i = 0, item = ossl_quic_cfq_get_priority_head(cfq, pn_space);; ++i, item = ossl_quic_cfq_item_get_priority_next(item, pn_space)) { if (expect[pn_space][i] == UINT32_MAX) { if (!TEST_ptr_null(item)) goto err; break; } items[pn_space][i] = item; if (!TEST_ptr(item) || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item), ref_buf + expect[pn_space][i]) || !TEST_int_eq(ossl_quic_cfq_item_get_pn_space(item), pn_space) || !TEST_int_eq(ossl_quic_cfq_item_get_state(item), QUIC_CFQ_STATE_NEW)) goto err; } testresult = 1; err: return testresult; } static int test_cfq(void) { int testresult = 0; QUIC_CFQ *cfq = NULL; QUIC_CFQ_ITEM *item, *inext; size_t i; uint32_t pn_space; if (!TEST_ptr(cfq = ossl_quic_cfq_new())) goto err; g_free = NULL; g_free_len = 0; for (i = 0; i < OSSL_NELEM(ref_buf); ++i) { if (!TEST_ptr(item = ossl_quic_cfq_add_frame(cfq, ref_priority[i], ref_pn_space[i], ref_frame_type[i], 0, ref_buf + i, 1, free_cb, NULL)) || !TEST_int_eq(ossl_quic_cfq_item_get_state(item), QUIC_CFQ_STATE_NEW) || !TEST_uint_eq(ossl_quic_cfq_item_get_pn_space(item), ref_pn_space[i]) || !TEST_uint64_t_eq(ossl_quic_cfq_item_get_frame_type(item), ref_frame_type[i]) || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item), ref_buf + i) || !TEST_size_t_eq(ossl_quic_cfq_item_get_encoded_len(item), 1)) goto err; } if (!check(cfq)) goto err; for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) for (item = ossl_quic_cfq_get_priority_head(cfq, pn_space); item != NULL; item = inext) { inext = ossl_quic_cfq_item_get_priority_next(item, pn_space); ossl_quic_cfq_mark_tx(cfq, item); } for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space))) goto err; for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) for (i = 0; i < OSSL_NELEM(items[0]); ++i) if (items[pn_space][i] != NULL) ossl_quic_cfq_mark_lost(cfq, items[pn_space][i], UINT32_MAX); if (!check(cfq)) goto err; for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) for (i = 0; i < OSSL_NELEM(items[0]); ++i) if (items[pn_space][i] != NULL) ossl_quic_cfq_release(cfq, items[pn_space][i]); for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space))) goto err; testresult = 1; err: ossl_quic_cfq_free(cfq); return testresult; } int setup_tests(void) { ADD_TEST(test_cfq); return 1; }
./openssl/test/gmdifftest.c
/* * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/crypto.h> #include "testutil.h" #define SECS_PER_DAY (24 * 60 * 60) /* * Time checking test code. Check times are identical for a wide range of * offsets. This should be run on a machine with 64 bit time_t or it will * trigger the very errors the routines fix. */ static int check_time(long offset) { struct tm tm1, tm2, o1; int off_day, off_sec; long toffset; time_t t1, t2; time(&t1); t2 = t1 + offset; OPENSSL_gmtime(&t2, &tm2); OPENSSL_gmtime(&t1, &tm1); o1 = tm1; if (!TEST_true(OPENSSL_gmtime_adj(&tm1, 0, offset)) || !TEST_int_eq(tm1.tm_year, tm2.tm_year) || !TEST_int_eq(tm1.tm_mon, tm2.tm_mon) || !TEST_int_eq(tm1.tm_mday, tm2.tm_mday) || !TEST_int_eq(tm1.tm_hour, tm2.tm_hour) || !TEST_int_eq(tm1.tm_min, tm2.tm_min) || !TEST_int_eq(tm1.tm_sec, tm2.tm_sec) || !TEST_true(OPENSSL_gmtime_diff(&off_day, &off_sec, &o1, &tm1))) return 0; toffset = (long)off_day * SECS_PER_DAY + off_sec; if (!TEST_long_eq(offset, toffset)) return 0; return 1; } static int test_gmtime(int offset) { return check_time(offset) && check_time(-offset) && check_time(offset * 1000L) && check_time(-offset * 1000L) && check_time(offset * 1000000L) && check_time(-offset * 1000000L); } int setup_tests(void) { if (sizeof(time_t) < 8) TEST_info("Skipping; time_t is less than 64-bits"); else ADD_ALL_TESTS_NOSUBTEST(test_gmtime, 1000); return 1; }
./openssl/test/context_internal_test.c
/* * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Internal tests for the OpenSSL library context */ #include "internal/cryptlib.h" #include "testutil.h" static int test_set0_default(void) { OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default(); OSSL_LIB_CTX *local = OSSL_LIB_CTX_new(); OSSL_LIB_CTX *prev; int testresult = 0; if (!TEST_ptr(global) || !TEST_ptr(local) || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))) goto err; /* Check we can change the local default context */ if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local)) || !TEST_ptr_eq(global, prev)) goto err; /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */ if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))) goto err; /* Global default should be unchanged */ if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default())) goto err; /* Check we can swap back to the global default */ if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global)) || !TEST_ptr_eq(local, prev)) goto err; testresult = 1; err: OSSL_LIB_CTX_free(local); return testresult; } int setup_tests(void) { ADD_TEST(test_set0_default); return 1; }
./openssl/test/quic_wire_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/packet.h" #include "internal/quic_wire.h" #include "internal/quic_wire_pkt.h" #include "testutil.h" struct encode_test_case { int (*serializer)(WPACKET *pkt); const unsigned char *expect_buf; size_t expect_buf_len; /* * fail: -1 if not truncated (function should test for success), else number * of bytes to which the input has been truncated (function should test that * decoding fails) */ int (*deserializer)(PACKET *pkt, ossl_ssize_t fail); }; /* 1. PADDING */ static int encode_case_1_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1)) return 0; return 1; } static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail) { if (fail >= 0) /* No failure modes for padding */ return 1; if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3)) return 0; return 1; } static const unsigned char encode_case_1_expect[] = { 0, 0, 0 }; /* 2. PING */ static int encode_case_2_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1)) return 0; return 1; } static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail) { if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0)) return 0; return 1; } static const unsigned char encode_case_2_expect[] = { 0x01 }; /* 3. ACK */ static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = { { 20, 30 }, { 0, 10 } }; static const OSSL_QUIC_FRAME_ACK encode_case_3_f = { (OSSL_QUIC_ACK_RANGE *)encode_case_3_ranges, OSSL_NELEM(encode_case_3_ranges), { OSSL_TIME_MS }, 60, 70, 80, 1 }; static int encode_case_3_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1)) return 0; return 1; } static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_ACK_RANGE ranges[4] = {0}; OSSL_QUIC_FRAME_ACK f = {0}; uint64_t total_ranges = 0, peek_total_ranges = 0; int ret; f.ack_ranges = ranges; f.num_ack_ranges = OSSL_NELEM(ranges); ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges); if (fail < 0 && !TEST_int_eq(ret, 1)) return 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0)) return 0; if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges)) return 0; if (!TEST_uint64_t_le(f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE), SIZE_MAX) || !TEST_uint64_t_le(encode_case_3_f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE), SIZE_MAX)) return 0; if (!TEST_mem_eq(f.ack_ranges, (size_t)f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE), encode_case_3_f.ack_ranges, (size_t)encode_case_3_f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE))) return 0; if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time), ossl_time2ticks(encode_case_3_f.delay_time))) return 0; if (!TEST_true(f.ecn_present)) return 0; if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0)) return 0; if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1)) return 0; if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce)) return 0; return 1; } static const unsigned char encode_case_3_expect[] = { 0x03, /* Type */ 0x1E, /* Largest Acknowledged */ 0x40, 0x7d, /* ACK Delay */ 1, /* ACK Range Count */ 10, /* First ACK Range */ 8, /* Gap */ 10, /* Length */ 0x3c, /* ECT0 Count */ 0x40, 0x46, /* ECT1 Count */ 0x40, 0x50, /* ECNCE Count */ }; /* 4. RESET_STREAM */ static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = { 0x1234, 0x9781, 0x11717 }; static int encode_case_4_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt, &encode_case_4_f), 1)) return 0; return 1; } static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_RESET_STREAM f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f))) return 0; return 1; } static const unsigned char encode_case_4_expect[] = { 0x04, /* Type */ 0x52, 0x34, /* Stream ID */ 0x80, 0x00, 0x97, 0x81, /* App Error Code */ 0x80, 0x01, 0x17, 0x17, /* Final Size */ }; /* 5. STOP_SENDING */ static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = { 0x1234, 0x9781 }; static int encode_case_5_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt, &encode_case_5_f), 1)) return 0; return 1; } static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_STOP_SENDING f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f))) return 0; return 1; } static const unsigned char encode_case_5_expect[] = { 0x05, /* Type */ 0x52, 0x34, /* Stream ID */ 0x80, 0x00, 0x97, 0x81 /* App Error Code */ }; /* 6. CRYPTO */ static const unsigned char encode_case_6_data[] = { 93, 18, 17, 102, 33 }; static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = { 0x1234, sizeof(encode_case_6_data), encode_case_6_data }; static int encode_case_6_enc(WPACKET *pkt) { if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt, &encode_case_6_f))) return 0; return 1; } static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_CRYPTO f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, 0, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(f.offset, 0x1234)) return 0; if (!TEST_uint64_t_le(f.len, SIZE_MAX)) return 0; if (!TEST_mem_eq(f.data, (size_t)f.len, encode_case_6_data, sizeof(encode_case_6_data))) return 0; return 1; } static const unsigned char encode_case_6_expect[] = { 0x06, /* Type */ 0x52, 0x34, /* Offset */ 0x05, /* Length */ 93, 18, 17, 102, 33 /* Data */ }; /* 7. NEW_TOKEN */ static const unsigned char encode_case_7_token[] = { 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a }; static int encode_case_7_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt, encode_case_7_token, sizeof(encode_case_7_token)), 1)) return 0; return 1; } static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail) { const unsigned char *token = NULL; size_t token_len = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_mem_eq(token, token_len, encode_case_7_token, sizeof(encode_case_7_token))) return 0; return 1; } static const unsigned char encode_case_7_expect[] = { 0x07, /* Type */ 0x10, /* Length */ 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */ 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a }; /* 8. STREAM (no length, no offset, no fin) */ static const unsigned char encode_case_8_data[] = { 0xde, 0x06, 0xcb, 0x76, 0x5d }; static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = { 0x1234, 0, 5, encode_case_8_data, 0, 0 }; static int encode_case_8_enc(WPACKET *pkt) { if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt, &encode_case_8_f))) return 0; return 1; } static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_STREAM f = {0}; if (fail >= 3) /* * This case uses implicit length signalling so truncation will not * cause it to fail unless the header (which is 3 bytes) is truncated. */ return 1; if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_le(f.len, SIZE_MAX)) return 0; if (!TEST_mem_eq(f.data, (size_t)f.len, encode_case_8_data, sizeof(encode_case_8_data))) return 0; if (!TEST_uint64_t_eq(f.stream_id, 0x1234)) return 0; if (!TEST_uint64_t_eq(f.offset, 0)) return 0; if (!TEST_int_eq(f.has_explicit_len, 0)) return 0; if (!TEST_int_eq(f.is_fin, 0)) return 0; return 1; } static const unsigned char encode_case_8_expect[] = { 0x08, /* Type (OFF=0, LEN=0, FIN=0) */ 0x52, 0x34, /* Stream ID */ 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */ }; /* 9. STREAM (length, offset, fin) */ static const unsigned char encode_case_9_data[] = { 0xde, 0x06, 0xcb, 0x76, 0x5d }; static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = { 0x1234, 0x39, 5, encode_case_9_data, 1, 1 }; static int encode_case_9_enc(WPACKET *pkt) { if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt, &encode_case_9_f))) return 0; return 1; } static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_STREAM f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_le(f.len, SIZE_MAX)) return 0; if (!TEST_mem_eq(f.data, (size_t)f.len, encode_case_9_data, sizeof(encode_case_9_data))) return 0; if (!TEST_uint64_t_eq(f.stream_id, 0x1234)) return 0; if (!TEST_uint64_t_eq(f.offset, 0x39)) return 0; if (!TEST_int_eq(f.has_explicit_len, 1)) return 0; if (!TEST_int_eq(f.is_fin, 1)) return 0; return 1; } static const unsigned char encode_case_9_expect[] = { 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */ 0x52, 0x34, /* Stream ID */ 0x39, /* Offset */ 0x05, /* Length */ 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */ }; /* 10. MAX_DATA */ static int encode_case_10_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1)) return 0; return 1; } static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t max_data = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(max_data, 0x1234)) return 0; return 1; } static const unsigned char encode_case_10_expect[] = { 0x10, /* Type */ 0x52, 0x34, /* Max Data */ }; /* 11. MAX_STREAM_DATA */ static int encode_case_11_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt, 0x1234, 0x9781), 1)) return 0; return 1; } static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t stream_id = 0, max_data = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id, &max_data), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(stream_id, 0x1234)) return 0; if (!TEST_uint64_t_eq(max_data, 0x9781)) return 0; return 1; } static const unsigned char encode_case_11_expect[] = { 0x11, /* Type */ 0x52, 0x34, /* Stream ID */ 0x80, 0x00, 0x97, 0x81, /* Max Data */ }; /* 12. MAX_STREAMS */ static int encode_case_12_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1)) return 0; if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1)) return 0; return 1; } static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t max_streams_1 = 0, max_streams_2 = 0, frame_type_1 = 0, frame_type_2 = 0; int is_minimal = 1, success_if; success_if = (fail < 0 || fail >= 1); if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1, &is_minimal), success_if)) return 0; if (!TEST_true(!success_if || is_minimal)) return 0; success_if = (fail < 0 || fail >= 3); if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams_1), success_if)) return 0; success_if = (fail < 0 || fail >= 4); if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2, &is_minimal), success_if)) return 0; if (!TEST_true(!success_if || is_minimal)) return 0; success_if = (fail < 0); if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams_2), success_if)) return 0; if ((fail < 0 || fail >= 3) && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)) return 0; if ((fail < 0 || fail >= 3) && !TEST_uint64_t_eq(max_streams_1, 0x1234)) return 0; if ((fail < 0 || fail >= 8) && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI)) return 0; if ((fail < 0 || fail >= 8) && !TEST_uint64_t_eq(max_streams_2, 0x9781)) return 0; return 1; } static const unsigned char encode_case_12_expect[] = { 0x12, /* Type (MAX_STREAMS Bidirectional) */ 0x52, 0x34, /* Max Streams */ 0x13, /* Type (MAX_STREAMS Unidirectional) */ 0x80, 0x00, 0x97, 0x81, /* Max Streams */ }; /* 13. DATA_BLOCKED */ static int encode_case_13_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1)) return 0; return 1; } static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t max_data = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(max_data, 0x1234)) return 0; return 1; } static const unsigned char encode_case_13_expect[] = { 0x14, /* Type */ 0x52, 0x34, /* Max Data */ }; /* 14. STREAM_DATA_BLOCKED */ static int encode_case_14_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt, 0x1234, 0x9781), 1)) return 0; return 1; } static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t stream_id = 0, max_data = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id, &max_data), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(stream_id, 0x1234)) return 0; if (!TEST_uint64_t_eq(max_data, 0x9781)) return 0; return 1; } static const unsigned char encode_case_14_expect[] = { 0x15, /* Type */ 0x52, 0x34, /* Stream ID */ 0x80, 0x00, 0x97, 0x81, /* Max Data */ }; /* 15. STREAMS_BLOCKED */ static int encode_case_15_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1)) return 0; if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1)) return 0; return 1; } static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t max_streams_1 = 0, max_streams_2 = 0, frame_type_1 = 0, frame_type_2 = 0; int is_minimal = 1, success_if; success_if = (fail < 0 || fail >= 1); if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1, &is_minimal), success_if)) return 0; if (!TEST_true(!success_if || is_minimal)) return 0; success_if = (fail < 0 || fail >= 3); if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_streams_1), success_if)) return 0; success_if = (fail < 0 || fail >= 4); if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2, &is_minimal), success_if)) return 0; if (!TEST_true(!success_if || is_minimal)) return 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_streams_2), fail < 0 || fail >= 8)) return 0; if ((fail < 0 || fail >= 1) && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)) return 0; if ((fail < 0 || fail >= 3) && !TEST_uint64_t_eq(max_streams_1, 0x1234)) return 0; if ((fail < 0 || fail >= 4) && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI)) return 0; if ((fail < 0 || fail >= 8) && !TEST_uint64_t_eq(max_streams_2, 0x9781)) return 0; return 1; } static const unsigned char encode_case_15_expect[] = { 0x16, /* Type (STREAMS_BLOCKED Bidirectional) */ 0x52, 0x34, /* Max Streams */ 0x17, /* Type (STREAMS_BLOCKED Unidirectional) */ 0x80, 0x00, 0x97, 0x81, /* Max Streams */ }; /* 16. NEW_CONNECTION_ID */ static const unsigned char encode_case_16_conn_id[] = { 0x33, 0x44, 0x55, 0x66 }; static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = { 0x9781, 0x1234, { 0x4, {0x33, 0x44, 0x55, 0x66} }, { { 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a } } }; static int encode_case_16_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt, &encode_case_16_f), 1)) return 0; return 1; } static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_NEW_CONN_ID f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(f.seq_num, 0x9781)) return 0; if (!TEST_uint64_t_eq(f.retire_prior_to, 0x1234)) return 0; if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id))) return 0; if (!TEST_mem_eq(f.conn_id.id, f.conn_id.id_len, encode_case_16_conn_id, sizeof(encode_case_16_conn_id))) return 0; if (!TEST_mem_eq(f.stateless_reset.token, sizeof(f.stateless_reset.token), encode_case_16_f.stateless_reset.token, sizeof(encode_case_16_f.stateless_reset.token))) return 0; return 1; } static const unsigned char encode_case_16_expect[] = { 0x18, /* Type */ 0x80, 0x00, 0x97, 0x81, /* Sequence Number */ 0x52, 0x34, /* Retire Prior To */ 0x04, /* Connection ID Length */ 0x33, 0x44, 0x55, 0x66, /* Connection ID */ 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */ 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a }; /* 16b. NEW_CONNECTION_ID seq_num < retire_prior_to */ static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16b_f = { 0x1234, 0x9781, { 0x4, {0x33, 0x44, 0x55, 0x66} }, { { 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a } } }; static int encode_case_16b_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt, &encode_case_16b_f), 1)) return 0; return 1; } static int encode_case_16b_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_NEW_CONN_ID f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), 0)) return 0; if (!TEST_true(PACKET_forward(pkt, PACKET_remaining(pkt)))) return 0; return 1; } static const unsigned char encode_case_16b_expect[] = { 0x18, /* Type */ 0x52, 0x34, /* Sequence Number */ 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */ 0x04, /* Connection ID Length */ 0x33, 0x44, 0x55, 0x66, /* Connection ID */ 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */ 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a }; /* 17. RETIRE_CONNECTION_ID */ static int encode_case_17_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1)) return 0; return 1; } static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t seq_num = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(seq_num, 0x1234)) return 0; return 1; } static const unsigned char encode_case_17_expect[] = { 0x19, /* Type */ 0x52, 0x34, /* Seq Num */ }; /* 18. PATH_CHALLENGE */ static const uint64_t encode_case_18_data = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL; static int encode_case_18_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt, encode_case_18_data), 1)) return 0; return 1; } static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t challenge = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(challenge, encode_case_18_data)) return 0; return 1; } static const unsigned char encode_case_18_expect[] = { 0x1A, /* Type */ 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */ }; /* 19. PATH_RESPONSE */ static const uint64_t encode_case_19_data = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL; static int encode_case_19_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt, encode_case_19_data), 1)) return 0; return 1; } static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t challenge = 0; if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_uint64_t_eq(challenge, encode_case_19_data)) return 0; return 1; } static const unsigned char encode_case_19_expect[] = { 0x1B, /* Type */ 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */ }; /* 20. CONNECTION_CLOSE (transport) */ static const char encode_case_20_reason[] = { /* "reason for closure" */ 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65 }; static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = { 0, 0x1234, 0x9781, (char *)encode_case_20_reason, sizeof(encode_case_20_reason) }; static int encode_case_20_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt, &encode_case_20_f), 1)) return 0; return 1; } static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail) { OSSL_QUIC_FRAME_CONN_CLOSE f = {0}; if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0)) return 0; if (fail >= 0) return 1; if (!TEST_int_eq(f.is_app, 0)) return 0; if (!TEST_uint64_t_eq(f.error_code, 0x1234)) return 0; if (!TEST_uint64_t_eq(f.frame_type, 0x9781)) return 0; if (!TEST_size_t_eq(f.reason_len, 18)) return 0; if (!TEST_mem_eq(f.reason, f.reason_len, encode_case_20_f.reason, encode_case_20_f.reason_len)) return 0; return 1; } static const unsigned char encode_case_20_expect[] = { 0x1C, /* Type */ 0x52, 0x34, /* Sequence Number */ 0x80, 0x00, 0x97, 0x81, /* Frame Type */ 0x12, /* Reason Length */ 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */ 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65 }; /* 21. HANDSHAKE_DONE */ static int encode_case_21_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1)) return 0; return 1; } static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail) { if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0)) return 0; return 1; } static const unsigned char encode_case_21_expect[] = { 0x1E }; /* 22. Buffer Transport Parameter */ static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99}; static int encode_case_22_enc(WPACKET *pkt) { unsigned char *p; if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234, encode_case_22_data, sizeof(encode_case_22_data)))) return 0; if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781, NULL, 2))) return 0; p[0] = 0x33; p[1] = 0x44; return 1; } static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t id = 0; size_t len = 0; const unsigned char *p; static const unsigned char data[] = {0x33, 0x44}; if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id), fail < 0 || fail >= 2)) return 0; if ((fail < 0 || fail >= 2) && !TEST_uint64_t_eq(id, 0x1234)) return 0; id = 0; p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len); if (fail < 0 || fail >= 8) { if (!TEST_ptr(p)) return 0; } else { if (!TEST_ptr_null(p)) return 0; } if ((fail < 0 || fail >= 8) && !TEST_uint64_t_eq(id, 0x1234)) return 0; if ((fail < 0 || fail >= 8) && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data))) return 0; if ((fail < 0 || fail >= 8) && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id), fail < 0 || fail >= 12)) return 0; if ((fail < 0 || fail >= 12) && !TEST_uint64_t_eq(id, 0x9781)) return 0; id = 0; p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len); if (fail < 0 || fail >= 15) { if (!TEST_ptr(p)) return 0; } else { if (!TEST_ptr_null(p)) return 0; } if ((fail < 0 || fail >= 15) && !TEST_uint64_t_eq(id, 0x9781)) return 0; if ((fail < 0 || fail >= 15) && !TEST_mem_eq(p, len, data, sizeof(data))) return 0; return 1; } static const unsigned char encode_case_22_expect[] = { 0x52, 0x34, /* ID */ 0x05, /* Length */ 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */ 0x80, 0x00, 0x97, 0x81, /* ID */ 0x02, /* Length */ 0x33, 0x44 /* Data */ }; /* 23. Integer Transport Parameter */ static int encode_case_23_enc(WPACKET *pkt) { if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1)) return 0; if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1)) return 0; return 1; } static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail) { uint64_t id = 0, value = 0; if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt, &id, &value), fail < 0 || fail >= 7)) return 0; if ((fail < 0 || fail >= 7) && !TEST_uint64_t_eq(id, 0x1234)) return 0; if ((fail < 0 || fail >= 7) && !TEST_uint64_t_eq(value, 0x9781)) return 0; if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt, &id, &value), fail < 0 || fail >= 14)) return 0; if ((fail < 0 || fail >= 14) && !TEST_uint64_t_eq(id, 0x2233)) return 0; if ((fail < 0 || fail >= 14) && !TEST_uint64_t_eq(value, 0x4545)) return 0; return 1; } static const unsigned char encode_case_23_expect[] = { 0x52, 0x34, 0x04, 0x80, 0x00, 0x97, 0x81, 0x62, 0x33, 0x04, 0x80, 0x00, 0x45, 0x45, }; #define ENCODE_CASE(n) \ { \ encode_case_##n##_enc, \ encode_case_##n##_expect, \ OSSL_NELEM(encode_case_##n##_expect), \ encode_case_##n##_dec \ }, static const struct encode_test_case encode_cases[] = { ENCODE_CASE(1) ENCODE_CASE(2) ENCODE_CASE(3) ENCODE_CASE(4) ENCODE_CASE(5) ENCODE_CASE(6) ENCODE_CASE(7) ENCODE_CASE(8) ENCODE_CASE(9) ENCODE_CASE(10) ENCODE_CASE(11) ENCODE_CASE(12) ENCODE_CASE(13) ENCODE_CASE(14) ENCODE_CASE(15) ENCODE_CASE(16) ENCODE_CASE(16b) ENCODE_CASE(17) ENCODE_CASE(18) ENCODE_CASE(19) ENCODE_CASE(20) ENCODE_CASE(21) ENCODE_CASE(22) ENCODE_CASE(23) }; static int test_wire_encode(int idx) { int testresult = 0; WPACKET wpkt; PACKET pkt; BUF_MEM *buf = NULL; size_t written; const struct encode_test_case *c = &encode_cases[idx]; int have_wpkt = 0; size_t i; if (!TEST_ptr(buf = BUF_MEM_new())) goto err; if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1)) goto err; have_wpkt = 1; if (!TEST_int_eq(c->serializer(&wpkt), 1)) goto err; if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1)) goto err; if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len)) goto err; if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1)) goto err; if (!TEST_int_eq(c->deserializer(&pkt, -1), 1)) goto err; if (!TEST_false(PACKET_remaining(&pkt))) goto err; for (i = 0; i < c->expect_buf_len; ++i) { PACKET pkt2; /* * Check parsing truncated (i.e., malformed) input is handled correctly. * Generate all possible truncations of our reference encoding and * verify that they are handled correctly. The number of bytes of the * truncated encoding is passed as an argument to the deserializer to * help it determine whether decoding should fail or not. */ if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1)) goto err; if (!TEST_int_eq(c->deserializer(&pkt2, i), 1)) goto err; } testresult = 1; err: if (have_wpkt) WPACKET_finish(&wpkt); BUF_MEM_free(buf); return testresult; } struct ack_test_case { const unsigned char *input_buf; size_t input_buf_len; int (*deserializer)(PACKET *pkt); int expect_fail; }; /* ACK Frame with Excessive First ACK Range Field */ static const unsigned char ack_case_1_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x00, /* ACK Range Count */ 0x09, /* First ACK Range */ }; /* ACK Frame with Valid ACK Range Field */ static const unsigned char ack_case_2_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x00, /* ACK Range Count */ 0x08, /* First ACK Range */ }; /* ACK Frame with Excessive ACK Range Gap */ static const unsigned char ack_case_3_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x01, /* ACK Range Count */ 0x01, /* First ACK Range */ 0x05, /* Gap */ 0x01, /* ACK Range Length */ }; /* ACK Frame with Valid ACK Range */ static const unsigned char ack_case_4_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x01, /* ACK Range Count */ 0x01, /* First ACK Range */ 0x04, /* Gap */ 0x01, /* ACK Range Length */ }; /* ACK Frame with Excessive ACK Range Length */ static const unsigned char ack_case_5_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x01, /* ACK Range Count */ 0x01, /* First ACK Range */ 0x04, /* Gap */ 0x02, /* ACK Range Length */ }; /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */ static const unsigned char ack_case_6_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x02, /* ACK Range Count */ 0x01, /* First ACK Range */ 0x01, /* Gap */ 0x02, /* ACK Range Length */ 0x00, /* Gap */ 0x01, /* ACK Range Length */ }; /* ACK Frame with Multiple ACK Ranges, Valid */ static const unsigned char ack_case_7_input[] = { 0x02, /* ACK Without ECN */ 0x08, /* Largest Acknowledged */ 0x01, /* ACK Delay */ 0x02, /* ACK Range Count */ 0x01, /* First ACK Range */ 0x01, /* Gap */ 0x02, /* ACK Range Length */ 0x00, /* Gap */ 0x00, /* ACK Range Length */ }; static int ack_generic_decode(PACKET *pkt) { OSSL_QUIC_ACK_RANGE ranges[8] = {0}; OSSL_QUIC_FRAME_ACK f = {0}; uint64_t total_ranges = 0, peek_total_ranges = 0; int r; size_t i; f.ack_ranges = ranges; f.num_ack_ranges = OSSL_NELEM(ranges); if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges), 1)) return 0; r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges); if (r == 0) return 0; if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges)) return 0; for (i = 0; i < f.num_ack_ranges; ++i) { if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end)) return 0; if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000)) return 0; } return 1; } #define ACK_CASE(n, expect_fail, dec) \ { \ ack_case_##n##_input, \ sizeof(ack_case_##n##_input), \ (dec), \ (expect_fail) \ }, static const struct ack_test_case ack_cases[] = { ACK_CASE(1, 1, ack_generic_decode) ACK_CASE(2, 0, ack_generic_decode) ACK_CASE(3, 1, ack_generic_decode) ACK_CASE(4, 0, ack_generic_decode) ACK_CASE(5, 1, ack_generic_decode) ACK_CASE(6, 1, ack_generic_decode) ACK_CASE(7, 0, ack_generic_decode) }; static int test_wire_ack(int idx) { int testresult = 0, r; PACKET pkt; const struct ack_test_case *c = &ack_cases[idx]; if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)c->input_buf, c->input_buf_len), 1)) goto err; r = c->deserializer(&pkt); if (c->expect_fail) { if (!TEST_int_eq(r, 0)) goto err; } else { if (!TEST_int_eq(r, 1)) goto err; if (!TEST_false(PACKET_remaining(&pkt))) goto err; } testresult = 1; err: return testresult; } /* Packet Header PN Encoding Tests */ struct pn_test { QUIC_PN pn, tx_largest_acked, rx_largest_pn; char expected_len; unsigned char expected_bytes[4]; }; static const struct pn_test pn_tests[] = { /* RFC 9000 Section A.2 */ { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, {0x5c,0x02} }, { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, {0xac,0xe8,0xfe} }, /* RFC 9000 Section A.3 */ { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, {0x9b,0x32} }, /* Boundary Cases */ { 1, 0, 0, 1, {0x01} }, { 256, 255, 255, 1, {0x00} }, { 257, 255, 255, 1, {0x01} }, { 256, 128, 128, 1, {0x00} }, { 256, 127, 127, 2, {0x01,0x00} }, { 65536, 32768, 32768, 2, {0x00,0x00} }, { 65537, 32769, 32769, 2, {0x00,0x01} }, { 65536, 32767, 32767, 3, {0x01,0x00,0x00} }, { 65537, 32768, 32768, 3, {0x01,0x00,0x01} }, { 16777216, 8388608, 8388608, 3, {0x00,0x00,0x00} }, { 16777217, 8388609, 8388609, 3, {0x00,0x00,0x01} }, { 16777216, 8388607, 8388607, 4, {0x01,0x00,0x00,0x00} }, { 16777217, 8388608, 8388608, 4, {0x01,0x00,0x00,0x01} }, { 4294967296, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x00} }, { 4294967297, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x01} }, }; static int test_wire_pkt_hdr_pn(int tidx) { int testresult = 0; const struct pn_test *t = &pn_tests[tidx]; unsigned char buf[4]; int pn_len; QUIC_PN res_pn; pn_len = ossl_quic_wire_determine_pn_len(t->pn, t->tx_largest_acked); if (!TEST_int_eq(pn_len, (int)t->expected_len)) goto err; if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t->pn, buf, pn_len))) goto err; if (!TEST_mem_eq(t->expected_bytes, t->expected_len, buf, pn_len)) goto err; if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf, pn_len, t->rx_largest_pn, &res_pn))) goto err; if (!TEST_uint64_t_eq(res_pn, t->pn)) goto err; testresult = 1; err: return testresult; } /* RFC 9001 s. A.4 */ static const QUIC_CONN_ID retry_orig_dcid = { 8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 } }; static const unsigned char retry_encoded[] = { 0xff, /* Long Header, Retry */ 0x00, 0x00, 0x00, 0x01, /* Version 1 */ 0x00, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID */ /* Retry Token */ 0x74, 0x6f, 0x6b, 0x65, 0x6e, /* Retry Integrity Tag */ 0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f, 0x24, 0x96, 0xba }; static int test_wire_retry_integrity_tag(void) { int testresult = 0; PACKET pkt = {0}; QUIC_PKT_HDR hdr = {0}; unsigned char got_tag[QUIC_RETRY_INTEGRITY_TAG_LEN] = {0}; if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded)))) goto err; if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL))) goto err; if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY)) goto err; if (!TEST_true(ossl_quic_calculate_retry_integrity_tag(NULL, NULL, &hdr, &retry_orig_dcid, got_tag))) goto err; if (!TEST_mem_eq(got_tag, sizeof(got_tag), retry_encoded + sizeof(retry_encoded) - QUIC_RETRY_INTEGRITY_TAG_LEN, QUIC_RETRY_INTEGRITY_TAG_LEN)) goto err; if (!TEST_true(ossl_quic_validate_retry_integrity_tag(NULL, NULL, &hdr, &retry_orig_dcid))) goto err; testresult = 1; err: return testresult; } /* is_minimal=0 test */ static const unsigned char non_minimal_1[] = { 0x40, 0x00, }; static const unsigned char non_minimal_2[] = { 0x40, 0x3F, }; static const unsigned char non_minimal_3[] = { 0x80, 0x00, 0x00, 0x00, }; static const unsigned char non_minimal_4[] = { 0x80, 0x00, 0x3F, 0xFF, }; static const unsigned char non_minimal_5[] = { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char non_minimal_6[] = { 0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF }; static const unsigned char *const non_minimal[] = { non_minimal_1, non_minimal_2, non_minimal_3, non_minimal_4, non_minimal_5, non_minimal_6, }; static const size_t non_minimal_len[] = { OSSL_NELEM(non_minimal_1), OSSL_NELEM(non_minimal_2), OSSL_NELEM(non_minimal_3), OSSL_NELEM(non_minimal_4), OSSL_NELEM(non_minimal_5), OSSL_NELEM(non_minimal_6), }; static int test_wire_minimal(int idx) { int testresult = 0; int is_minimal; uint64_t frame_type; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, non_minimal[idx], non_minimal_len[idx]))) goto err; if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type, &is_minimal))) goto err; if (!TEST_false(is_minimal)) goto err; testresult = 1; err: return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases)); ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases)); ADD_ALL_TESTS(test_wire_pkt_hdr_pn, OSSL_NELEM(pn_tests)); ADD_TEST(test_wire_retry_integrity_tag); ADD_ALL_TESTS(test_wire_minimal, OSSL_NELEM(non_minimal_len)); return 1; }
./openssl/test/evp_pkey_provided_test.c
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> /* memset */ #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/encoder.h> #include <openssl/provider.h> #include <openssl/param_build.h> #include <openssl/core_names.h> #include <openssl/sha.h> #include "crypto/ecx.h" #include "crypto/evp.h" /* For the internal API */ #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */ #include "internal/nelem.h" #include "testutil.h" static char *datadir = NULL; /* * Do not change the order of the following defines unless you also * update the for loop bounds used inside test_print_key_using_encoder() and * test_print_key_using_encoder_public(). */ #define PRIV_TEXT 0 #define PRIV_PEM 1 #define PRIV_DER 2 #define PUB_TEXT 3 #define PUB_PEM 4 #define PUB_DER 5 static void stripcr(char *buf, size_t *len) { size_t i; char *curr, *writ; for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) { if (*curr == '\r') { (*len)--; continue; } if (curr != writ) *writ = *curr; writ++; } } static int compare_with_file(const char *alg, int type, BIO *membio) { char filename[80]; BIO *file = NULL; char buf[4096]; char *memdata, *fullfile = NULL; const char *suffix; size_t readbytes; int ret = 0; int len; size_t slen; switch (type) { case PRIV_TEXT: suffix = "priv.txt"; break; case PRIV_PEM: suffix = "priv.pem"; break; case PRIV_DER: suffix = "priv.der"; break; case PUB_TEXT: suffix = "pub.txt"; break; case PUB_PEM: suffix = "pub.pem"; break; case PUB_DER: suffix = "pub.der"; break; default: TEST_error("Invalid file type"); goto err; } BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix); fullfile = test_mk_file_path(datadir, filename); if (!TEST_ptr(fullfile)) goto err; file = BIO_new_file(fullfile, "rb"); if (!TEST_ptr(file)) goto err; if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes)) || !TEST_true(BIO_eof(file)) || !TEST_size_t_lt(readbytes, sizeof(buf))) goto err; len = BIO_get_mem_data(membio, &memdata); if (!TEST_int_gt(len, 0)) goto err; slen = len; if (type != PRIV_DER && type != PUB_DER) { stripcr(memdata, &slen); stripcr(buf, &readbytes); } if (!TEST_mem_eq(memdata, slen, buf, readbytes)) goto err; ret = 1; err: OPENSSL_free(fullfile); (void)BIO_reset(membio); BIO_free(file); return ret; } static int pass_cb(char *buf, int size, int rwflag, void *u) { return 0; } static int pass_cb_error(char *buf, int size, int rwflag, void *u) { return -1; } static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk) { BIO *membio = BIO_new(BIO_s_mem()); int ret = 0; if (!TEST_ptr(membio)) goto err; if (/* Output Encrypted private key in PEM form */ !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(), (unsigned char *)"pass", 4, NULL, NULL)) /* Output zero-length passphrase encrypted private key in PEM form */ || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), (const char *)~0, 0, NULL, NULL)) || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, NULL, "")) || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, pass_cb, NULL)) || !TEST_false(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, pass_cb_error, NULL)) #ifndef OPENSSL_NO_DES || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, (const char *)~0, 0, NULL, NULL)) || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, NULL, "")) || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, pass_cb, NULL)) || !TEST_false(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, pass_cb_error, NULL)) #endif /* Private key in text form */ || !TEST_int_gt(EVP_PKEY_print_private(membio, pk, 0, NULL), 0) || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio)) /* Public key in PEM form */ || !TEST_true(PEM_write_bio_PUBKEY(membio, pk)) || !TEST_true(compare_with_file(alg, PUB_PEM, membio)) /* Unencrypted private key in PEM form */ || !TEST_true(PEM_write_bio_PrivateKey(membio, pk, NULL, NULL, 0, NULL, NULL)) || !TEST_true(compare_with_file(alg, PRIV_PEM, membio)) /* NULL key */ || !TEST_false(PEM_write_bio_PrivateKey(membio, NULL, NULL, NULL, 0, NULL, NULL)) || !TEST_false(PEM_write_bio_PrivateKey_traditional(membio, NULL, NULL, NULL, 0, NULL, NULL))) goto err; ret = 1; err: BIO_free(membio); return ret; } static int test_print_key_type_using_encoder(const char *alg, int type, const EVP_PKEY *pk) { const char *output_type, *output_structure; int selection; OSSL_ENCODER_CTX *ctx = NULL; BIO *membio = BIO_new(BIO_s_mem()); int ret = 0; switch (type) { case PRIV_TEXT: output_type = "TEXT"; output_structure = NULL; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PRIV_PEM: output_type = "PEM"; output_structure = "PrivateKeyInfo"; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PRIV_DER: output_type = "DER"; output_structure = "PrivateKeyInfo"; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_TEXT: output_type = "TEXT"; output_structure = NULL; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_PEM: output_type = "PEM"; output_structure = "SubjectPublicKeyInfo"; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_DER: output_type = "DER"; output_structure = "SubjectPublicKeyInfo"; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; default: TEST_error("Invalid encoding type"); goto err; } if (!TEST_ptr(membio)) goto err; /* Make a context, it's valid for several prints */ TEST_note("Setting up a OSSL_ENCODER context with passphrase"); if (!TEST_ptr(ctx = OSSL_ENCODER_CTX_new_for_pkey(pk, selection, output_type, output_structure, NULL)) /* Check that this operation is supported */ || !TEST_int_ne(OSSL_ENCODER_CTX_get_num_encoders(ctx), 0)) goto err; /* Use no cipher. This should give us an unencrypted PEM */ TEST_note("Testing with no encryption"); if (!TEST_true(OSSL_ENCODER_to_bio(ctx, membio)) || !TEST_true(compare_with_file(alg, type, membio))) goto err; if (type == PRIV_PEM) { /* Set a passphrase to be used later */ if (!TEST_true(OSSL_ENCODER_CTX_set_passphrase(ctx, (unsigned char *)"pass", 4))) goto err; /* Use a valid cipher name */ TEST_note("Displaying PEM encrypted with AES-256-CBC"); if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, "AES-256-CBC", NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ctx, bio_out))) goto err; /* Use an invalid cipher name, which should generate no output */ TEST_note("NOT Displaying PEM encrypted with (invalid) FOO"); if (!TEST_false(OSSL_ENCODER_CTX_set_cipher(ctx, "FOO", NULL)) || !TEST_false(OSSL_ENCODER_to_bio(ctx, bio_out))) goto err; /* Clear the cipher. This should give us an unencrypted PEM again */ TEST_note("Testing with encryption cleared (no encryption)"); if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, NULL, NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ctx, membio)) || !TEST_true(compare_with_file(alg, type, membio))) goto err; } ret = 1; err: BIO_free(membio); OSSL_ENCODER_CTX_free(ctx); return ret; } static int test_print_key_using_encoder(const char *alg, const EVP_PKEY *pk) { int i; int ret = 1; for (i = PRIV_TEXT; i <= PUB_DER; i++) ret = ret && test_print_key_type_using_encoder(alg, i, pk); return ret; } #ifndef OPENSSL_NO_ECX static int test_print_key_using_encoder_public(const char *alg, const EVP_PKEY *pk) { int i; int ret = 1; for (i = PUB_TEXT; i <= PUB_DER; i++) ret = ret && test_print_key_type_using_encoder(alg, i, pk); return ret; } #endif /* Array indexes used in test_fromdata_rsa */ #define N 0 #define E 1 #define D 2 #define P 3 #define Q 4 #define DP 5 #define DQ 6 #define QINV 7 static int test_fromdata_rsa(void) { int ret = 0, i; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; /* * 32-bit RSA key, extracted from this command, * executed with OpenSSL 1.0.2: * * openssl genrsa 32 | openssl rsa -text */ static unsigned long key_numbers[] = { 0xbc747fc5, /* N */ 0x10001, /* E */ 0x7b133399, /* D */ 0xe963, /* P */ 0xceb7, /* Q */ 0x8599, /* DP */ 0xbd87, /* DQ */ 0xcc3b, /* QINV */ }; OSSL_PARAM fromdata_params[] = { OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]), OSSL_PARAM_END }; BIGNUM *bn = BN_new(); BIGNUM *bn_from = BN_new(); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 32) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 8) || !TEST_int_eq(EVP_PKEY_get_size(pk), 4) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; EVP_PKEY_CTX_free(key_ctx); if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; /* EVP_PKEY_copy_parameters() should fail for RSA */ if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("RSA", pk) && test_print_key_using_encoder("RSA", pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: /* for better diagnostics always compare key params */ for (i = 0; fromdata_params[i].key != NULL; ++i) { if (!TEST_true(BN_set_word(bn_from, key_numbers[i])) || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key, &bn)) || !TEST_BN_eq(bn, bn_from)) ret = 0; } BN_free(bn_from); BN_free(bn); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(key_ctx); EVP_PKEY_CTX_free(ctx); return ret; } struct check_data { const char *pname; BIGNUM *comparebn; }; static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params, struct check_data check[], int expected_nbits, int expected_sbits, int expected_ksize) { const OSSL_PARAM *check_param = NULL; BIGNUM *check_bn = NULL; OSSL_PARAM *todata_params = NULL; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; int i; int ret = 0; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; /* * get the generated key parameters back and validate that the * exponents/coeffs are correct */ if (!TEST_int_eq(EVP_PKEY_todata(pk, EVP_PKEY_KEYPAIR, &todata_params), 1)) goto err; for (i = 0; check[i].pname != NULL; i++) { if (!TEST_ptr(check_param = OSSL_PARAM_locate_const(todata_params, check[i].pname))) goto err; if (!TEST_int_eq(OSSL_PARAM_get_BN(check_param, &check_bn), 1)) goto err; if (!TEST_BN_eq(check_bn, check[i].comparebn)) { TEST_info("Data mismatch for parameter %s", check[i].pname); goto err; } BN_free(check_bn); check_bn = NULL; } while (dup_pk == NULL) { if (!TEST_int_eq(EVP_PKEY_get_bits(pk), expected_nbits) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), expected_sbits) || !TEST_int_eq(EVP_PKEY_get_size(pk), expected_ksize) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; EVP_PKEY_CTX_free(key_ctx); if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; /* EVP_PKEY_copy_parameters() should fail for RSA */ if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; if (!TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1)) { EVP_PKEY_free(dup_pk); goto err; } EVP_PKEY_free(pk); pk = dup_pk; } ret = 1; err: BN_free(check_bn); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_free(todata_params); return ret; } static int test_fromdata_rsa_derive_from_pq_sp800(void) { OSSL_PARAM_BLD *bld = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL; BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; OSSL_PARAM *fromdata_params = NULL; struct check_data cdata[4]; int ret = 0; /* * 512-bit RSA key, extracted from this command, * openssl genrsa 512 | openssl rsa -text * Note: When generating a key with EVP_PKEY_fromdata, and using * crt derivation, openssl requires a minimum of 512 bits of n data, * and 2048 bits in the FIPS case */ static unsigned char n_data[] = {0x00, 0xc7, 0x06, 0xd8, 0x6b, 0x3c, 0x4f, 0xb7, 0x95, 0x42, 0x44, 0x90, 0xbd, 0xef, 0xf3, 0xc4, 0xb5, 0xa8, 0x55, 0x9e, 0x33, 0xa3, 0x04, 0x3a, 0x90, 0xe5, 0x13, 0xff, 0x87, 0x69, 0x15, 0xa4, 0x8a, 0x17, 0x10, 0xcc, 0xdf, 0xf9, 0xc5, 0x0f, 0xf1, 0x12, 0xff, 0x12, 0x11, 0xe5, 0x6b, 0x5c, 0x83, 0xd9, 0x43, 0xd1, 0x8a, 0x7e, 0xa6, 0x60, 0x07, 0x2e, 0xbb, 0x03, 0x17, 0x2d, 0xec, 0x17, 0x87}; static unsigned char e_data[] = {0x01, 0x00, 0x01}; static unsigned char d_data[] = {0x1e, 0x5e, 0x5d, 0x07, 0x7f, 0xdc, 0x6a, 0x16, 0xcc, 0x55, 0xca, 0x00, 0x31, 0x6c, 0xf0, 0xc7, 0x07, 0x38, 0x89, 0x3b, 0x37, 0xd4, 0x9d, 0x5b, 0x1e, 0x99, 0x3e, 0x94, 0x5a, 0xe4, 0x82, 0x86, 0x8a, 0x78, 0x34, 0x09, 0x37, 0xd5, 0xe7, 0xb4, 0xef, 0x5f, 0x83, 0x94, 0xff, 0xe5, 0x36, 0x79, 0x10, 0x0c, 0x38, 0xc5, 0x3a, 0x33, 0xa6, 0x7c, 0x3c, 0xcc, 0x98, 0xe0, 0xf5, 0xdb, 0xe6, 0x81}; static unsigned char p_data[] = {0x00, 0xf6, 0x61, 0x38, 0x0e, 0x1f, 0x82, 0x7c, 0xb8, 0xba, 0x00, 0xd3, 0xac, 0xdc, 0x4e, 0x6b, 0x7e, 0xf7, 0x58, 0xf3, 0xd9, 0xd8, 0x21, 0xed, 0x54, 0xa3, 0x36, 0xd2, 0x2c, 0x5f, 0x06, 0x7d, 0xc5}; static unsigned char q_data[] = {0x00, 0xce, 0xcc, 0x4a, 0xa5, 0x4f, 0xd6, 0x73, 0xd0, 0x20, 0xc3, 0x98, 0x64, 0x20, 0x9b, 0xc1, 0x23, 0xd8, 0x5c, 0x82, 0x4f, 0xe8, 0xa5, 0x32, 0xcd, 0x7e, 0x97, 0xb4, 0xde, 0xf6, 0x4c, 0x80, 0xdb}; static unsigned char dmp1_data[] = {0x00, 0xd1, 0x07, 0xb6, 0x79, 0x34, 0xfe, 0x8e, 0x36, 0x63, 0x88, 0xa4, 0x0e, 0x3a, 0x73, 0x45, 0xfc, 0x58, 0x7a, 0x5d, 0x98, 0xeb, 0x28, 0x0d, 0xa5, 0x0b, 0x3c, 0x4d, 0xa0, 0x5b, 0x96, 0xb4, 0x49}; static unsigned char dmq1_data[] = {0x5b, 0x47, 0x02, 0xdf, 0xaa, 0xb8, 0xae, 0x8f, 0xbc, 0x16, 0x79, 0x6a, 0x20, 0x96, 0x7f, 0x0e, 0x92, 0x4e, 0x6a, 0xda, 0x58, 0x86, 0xaa, 0x40, 0xd7, 0xd2, 0xa0, 0x6c, 0x15, 0x6c, 0xb9, 0x27}; static unsigned char iqmp_data[] = {0x00, 0xa0, 0xd6, 0xf0, 0xe8, 0x17, 0x9e, 0xe7, 0xe6, 0x99, 0x12, 0xd6, 0xd9, 0x43, 0xcf, 0xed, 0x37, 0x29, 0xf5, 0x6c, 0x3e, 0xc1, 0x7f, 0x2e, 0x31, 0x3f, 0x64, 0x34, 0x66, 0x68, 0x5c, 0x22, 0x08}; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL)) || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL)) || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2, q)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1; cdata[0].comparebn = dmp1; cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2; cdata[1].comparebn = dmq1; cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1; cdata[2].comparebn = iqmp; cdata[3].pname = NULL; cdata[3].comparebn = NULL; ret = do_fromdata_rsa_derive(fromdata_params, cdata, 512, 56, 64); err: BN_free(n); BN_free(e); BN_free(d); BN_free(p); BN_free(q); BN_free(dmp1); BN_free(dmq1); BN_free(iqmp); OSSL_PARAM_BLD_free(bld); return ret; } static int test_fromdata_rsa_derive_from_pq_multiprime(void) { OSSL_PARAM_BLD *bld = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL; BIGNUM *p = NULL, *q = NULL, *p2 = NULL; BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; BIGNUM *exp3 = NULL, *coeff2 = NULL; OSSL_PARAM *fromdata_params = NULL; struct check_data cdata[12]; int ret = 0; /* * multiprime RSA key, extracted from this command, * openssl genrsa -primes 3 | openssl rsa -text * Note: When generating a key with EVP_PKEY_fromdata, and using * crt derivation, openssl requires a minimum of 512 bits of n data, * and 2048 bits in the FIPS case */ static unsigned char n_data[] = {0x00, 0x95, 0x78, 0x21, 0xe0, 0xca, 0x94, 0x6c, 0x0b, 0x86, 0x2a, 0x01, 0xde, 0xd9, 0xab, 0xee, 0x88, 0x4a, 0x27, 0x4f, 0xcc, 0x5f, 0xf1, 0x71, 0xe1, 0x0b, 0xc3, 0xd1, 0x88, 0x76, 0xf0, 0x83, 0x03, 0x93, 0x7e, 0x39, 0xfa, 0x47, 0x89, 0x34, 0x27, 0x18, 0x19, 0x97, 0xfc, 0xd4, 0xfe, 0xe5, 0x8a, 0xa9, 0x11, 0x83, 0xb5, 0x15, 0x4a, 0x29, 0xa6, 0xa6, 0xd0, 0x6e, 0x0c, 0x7f, 0x61, 0x8f, 0x7e, 0x7c, 0xfb, 0xfc, 0x04, 0x8b, 0xca, 0x44, 0xf8, 0x59, 0x0b, 0x22, 0x6f, 0x3f, 0x92, 0x23, 0x98, 0xb5, 0xc8, 0xf7, 0xff, 0xf7, 0xac, 0x6b, 0x36, 0xb3, 0xaf, 0x39, 0xde, 0x66, 0x38, 0x51, 0x9f, 0xbe, 0xe2, 0xfc, 0xe4, 0x6f, 0x1a, 0x0f, 0x7a, 0xde, 0x7f, 0x0f, 0x4e, 0xbc, 0xed, 0xa2, 0x99, 0xc5, 0xd1, 0xbf, 0x8f, 0xba, 0x92, 0x91, 0xe4, 0x00, 0x91, 0xbb, 0x67, 0x36, 0x7d, 0x00, 0x50, 0xda, 0x28, 0x38, 0xdc, 0x9f, 0xfe, 0x3f, 0x24, 0x5a, 0x0d, 0xe1, 0x8d, 0xe9, 0x45, 0x2c, 0xd7, 0xf2, 0x67, 0x8c, 0x0c, 0x6e, 0xdb, 0xc8, 0x8b, 0x6b, 0x38, 0x30, 0x21, 0x94, 0xc0, 0xe3, 0xd7, 0xe0, 0x23, 0xd3, 0xd4, 0xfa, 0xdb, 0xb9, 0xfe, 0x1a, 0xcc, 0xc9, 0x79, 0x19, 0x35, 0x18, 0x42, 0x30, 0xc4, 0xb5, 0x92, 0x33, 0x1e, 0xd4, 0xc4, 0xc0, 0x9d, 0x55, 0x37, 0xd4, 0xef, 0x54, 0x71, 0x81, 0x09, 0x15, 0xdb, 0x11, 0x38, 0x6b, 0x35, 0x93, 0x11, 0xdc, 0xb1, 0x6c, 0xd6, 0xa4, 0x37, 0x84, 0xf3, 0xb2, 0x2f, 0x1b, 0xd6, 0x05, 0x9f, 0x0e, 0x5c, 0x98, 0x29, 0x2f, 0x95, 0xb6, 0x55, 0xbd, 0x24, 0x44, 0xc5, 0xc8, 0xa2, 0x76, 0x1e, 0xf8, 0x82, 0x8a, 0xdf, 0x34, 0x72, 0x7e, 0xdd, 0x65, 0x4b, 0xfc, 0x6c, 0x1c, 0x96, 0x70, 0xe2, 0x69, 0xb5, 0x12, 0x1b, 0x59, 0x67, 0x14, 0x9d}; static unsigned char e_data[] = {0x01, 0x00, 0x01}; static unsigned char d_data[] = {0x64, 0x57, 0x4d, 0x86, 0xf6, 0xf8, 0x44, 0xc0, 0x47, 0xc5, 0x13, 0x94, 0x63, 0x54, 0x84, 0xc1, 0x81, 0xe6, 0x7a, 0x2f, 0x9d, 0x89, 0x1d, 0x06, 0x13, 0x3b, 0xd6, 0x02, 0x62, 0xb6, 0x7b, 0x7d, 0x7f, 0x1a, 0x92, 0x19, 0x6e, 0xc4, 0xb0, 0xfa, 0x3d, 0xb7, 0x90, 0xcc, 0xee, 0xc0, 0x5f, 0xa0, 0x82, 0x77, 0x7b, 0x8f, 0xa9, 0x47, 0x2c, 0x46, 0xf0, 0x5d, 0xa4, 0x43, 0x47, 0x90, 0x5b, 0x20, 0x73, 0x0f, 0x46, 0xd4, 0x56, 0x73, 0xe7, 0x71, 0x41, 0x75, 0xb4, 0x1c, 0x32, 0xf5, 0x0c, 0x68, 0x8c, 0x40, 0xea, 0x1c, 0x30, 0x12, 0xa2, 0x65, 0x02, 0x27, 0x98, 0x4e, 0x0a, 0xbf, 0x2b, 0x72, 0xb2, 0x5c, 0xe3, 0xbe, 0x3e, 0xc7, 0xdb, 0x9b, 0xa2, 0x4a, 0x90, 0xc0, 0xa7, 0xb0, 0x00, 0xf1, 0x6a, 0xff, 0xa3, 0x77, 0xf7, 0x71, 0xa2, 0x41, 0xe9, 0x6e, 0x7c, 0x38, 0x24, 0x46, 0xd5, 0x5c, 0x49, 0x2a, 0xe6, 0xee, 0x27, 0x4b, 0x2e, 0x6f, 0x16, 0x54, 0x2d, 0x37, 0x36, 0x01, 0x39, 0x2b, 0x23, 0x4b, 0xb4, 0x65, 0x25, 0x4d, 0x7f, 0x72, 0x20, 0x7f, 0x5d, 0xec, 0x50, 0xba, 0xbb, 0xaa, 0x9c, 0x3c, 0x1d, 0xa1, 0x40, 0x2c, 0x6a, 0x8b, 0x5f, 0x2e, 0xe0, 0xa6, 0xf7, 0x9e, 0x03, 0xb5, 0x44, 0x5f, 0x74, 0xc7, 0x9f, 0x89, 0x2b, 0x71, 0x2f, 0x66, 0x9f, 0x03, 0x6c, 0x96, 0xd0, 0x23, 0x36, 0x4d, 0xa1, 0xf0, 0x82, 0xcc, 0x43, 0xe7, 0x08, 0x93, 0x40, 0x18, 0xc0, 0x39, 0x73, 0x83, 0xe2, 0xec, 0x9b, 0x81, 0x9d, 0x4c, 0x86, 0xaa, 0x59, 0xa8, 0x67, 0x1c, 0x80, 0xdc, 0x6f, 0x7f, 0x23, 0x6b, 0x7d, 0x2c, 0x56, 0x99, 0xa0, 0x89, 0x7e, 0xdb, 0x8b, 0x7a, 0xaa, 0x03, 0x8e, 0x8e, 0x8e, 0x3a, 0x58, 0xb4, 0x03, 0x6b, 0x65, 0xfa, 0x92, 0x0a, 0x96, 0x93, 0xa6, 0x07, 0x60, 0x01}; static unsigned char p_data[] = {0x06, 0x55, 0x7f, 0xbd, 0xfd, 0xa8, 0x4c, 0x94, 0x5e, 0x10, 0x8a, 0x54, 0x37, 0xf3, 0x64, 0x37, 0x3a, 0xca, 0x18, 0x1b, 0xdd, 0x71, 0xa5, 0x94, 0xc9, 0x31, 0x59, 0xa5, 0x89, 0xe9, 0xc4, 0xba, 0x55, 0x90, 0x6d, 0x9c, 0xcc, 0x52, 0x5d, 0x44, 0xa8, 0xbc, 0x2b, 0x3b, 0x8c, 0xbd, 0x96, 0xfa, 0xcd, 0x54, 0x63, 0xe3, 0xc8, 0xfe, 0x5e, 0xc6, 0x73, 0x98, 0x14, 0x7a, 0x54, 0x0e, 0xe7, 0x75, 0x49, 0x93, 0x20, 0x33, 0x17, 0xa9, 0x34, 0xa8, 0xee, 0xaf, 0x3a, 0xcc, 0xf5, 0x69, 0xfc, 0x30, 0x1a, 0xdf, 0x49, 0x61, 0xa4, 0xd1}; static unsigned char p2_data[] = {0x03, 0xe2, 0x41, 0x3d, 0xb1, 0xdd, 0xad, 0xd7, 0x3b, 0xf8, 0xab, 0x32, 0x27, 0x8b, 0xac, 0x95, 0xc0, 0x1a, 0x3f, 0x80, 0x8e, 0x21, 0xa9, 0xb8, 0xa2, 0xed, 0xcf, 0x97, 0x5c, 0x61, 0x10, 0x94, 0x1b, 0xd0, 0xbe, 0x88, 0xc2, 0xa7, 0x20, 0xe5, 0xa5, 0xc2, 0x7a, 0x7e, 0xf0, 0xd1, 0xe4, 0x13, 0x75, 0xb9, 0x62, 0x90, 0xf1, 0xc3, 0x5b, 0x8c, 0xe9, 0xa9, 0x5b, 0xb7, 0x6d, 0xdc, 0xcd, 0x12, 0xea, 0x97, 0x05, 0x04, 0x25, 0x2a, 0x93, 0xd1, 0x4e, 0x05, 0x1a, 0x50, 0xa2, 0x67, 0xb8, 0x4b, 0x09, 0x15, 0x65, 0x6c, 0x66, 0x2d}; static unsigned char q_data[] = {0x06, 0x13, 0x74, 0x6e, 0xde, 0x7c, 0x33, 0xc2, 0xe7, 0x05, 0x2c, 0xeb, 0x25, 0x7d, 0x4a, 0x07, 0x7e, 0x03, 0xcf, 0x6a, 0x23, 0x36, 0x25, 0x23, 0xf6, 0x5d, 0xde, 0xa3, 0x0f, 0x82, 0xe6, 0x4b, 0xec, 0x39, 0xbf, 0x37, 0x1f, 0x4f, 0x56, 0x1e, 0xd8, 0x62, 0x32, 0x5c, 0xf5, 0x37, 0x75, 0x20, 0xe2, 0x7e, 0x56, 0x82, 0xc6, 0x35, 0xd3, 0x4d, 0xfa, 0x6c, 0xc3, 0x93, 0xf0, 0x60, 0x53, 0x78, 0x95, 0xee, 0xf9, 0x8b, 0x2c, 0xaf, 0xb1, 0x47, 0x5c, 0x29, 0x0d, 0x2a, 0x47, 0x7f, 0xd0, 0x7a, 0x4e, 0x26, 0x7b, 0x47, 0xfb, 0x61}; static unsigned char dmp1_data[] = {0x01, 0x13, 0x3a, 0x1f, 0x91, 0x92, 0xa3, 0x8c, 0xfb, 0x7a, 0x6b, 0x40, 0x68, 0x4e, 0xd3, 0xcf, 0xdc, 0x16, 0xb9, 0x88, 0xe1, 0x49, 0x8d, 0x05, 0x78, 0x30, 0xfc, 0x3a, 0x70, 0xf2, 0x51, 0x06, 0x1f, 0xc7, 0xe8, 0x13, 0x19, 0x4b, 0x51, 0xb1, 0x79, 0xc2, 0x96, 0xc4, 0x00, 0xdb, 0x9d, 0x68, 0xec, 0xb9, 0x4a, 0x4b, 0x3b, 0xae, 0x91, 0x7f, 0xb5, 0xd7, 0x36, 0x82, 0x9d, 0x09, 0xfa, 0x97, 0x99, 0xe9, 0x73, 0x29, 0xb8, 0xf6, 0x6b, 0x8d, 0xd1, 0x15, 0xc5, 0x31, 0x4c, 0xe6, 0xb4, 0x7b, 0xa5, 0xd4, 0x08, 0xac, 0x9e, 0x41}; static unsigned char dmq1_data[] = {0x05, 0xcd, 0x33, 0xc2, 0xdd, 0x3b, 0xb8, 0xec, 0xe4, 0x4c, 0x03, 0xcc, 0xef, 0xba, 0x07, 0x22, 0xca, 0x47, 0x77, 0x18, 0x40, 0x50, 0xe5, 0xfb, 0xc5, 0xb5, 0x71, 0xed, 0x3e, 0xd5, 0x5d, 0x72, 0xa7, 0x37, 0xa8, 0x86, 0x48, 0xa6, 0x27, 0x74, 0x42, 0x66, 0xd8, 0xf1, 0xfb, 0xcf, 0x1d, 0x4e, 0xee, 0x15, 0x76, 0x23, 0x5e, 0x81, 0x6c, 0xa7, 0x2b, 0x74, 0x08, 0xf7, 0x4c, 0x71, 0x9d, 0xa2, 0x29, 0x7f, 0xca, 0xd5, 0x02, 0x31, 0x2c, 0x54, 0x18, 0x02, 0xb6, 0xa8, 0x65, 0x26, 0xfc, 0xf8, 0x9b, 0x80, 0x90, 0xfc, 0x75, 0x61}; static unsigned char iqmp_data[] = {0x05, 0x78, 0xf8, 0xdd, 0x1c, 0x6f, 0x3d, 0xaf, 0x53, 0x84, 0x32, 0xa9, 0x35, 0x52, 0xf3, 0xd0, 0x4d, 0xf8, 0x09, 0x85, 0x3d, 0x72, 0x20, 0x8b, 0x47, 0xba, 0xc8, 0xce, 0xac, 0xd9, 0x76, 0x90, 0x05, 0x88, 0x63, 0x8a, 0x10, 0x2b, 0xcd, 0xd3, 0xbe, 0x8c, 0x16, 0x60, 0x6a, 0xfd, 0xce, 0xc7, 0x9f, 0xfa, 0xbb, 0xe3, 0xa6, 0xde, 0xc2, 0x8f, 0x1d, 0x25, 0xdc, 0x41, 0xcb, 0xa4, 0xeb, 0x76, 0xc9, 0xdc, 0x8e, 0x49, 0x0e, 0xe4, 0x7c, 0xd2, 0xd5, 0x6e, 0x26, 0x3c, 0x0b, 0xd3, 0xc5, 0x20, 0x4e, 0x4b, 0xb6, 0xf7, 0xae, 0xef}; static unsigned char exp3_data[] = {0x02, 0x7d, 0x16, 0x24, 0xfc, 0x35, 0xf9, 0xd0, 0xb3, 0x02, 0xf2, 0x5f, 0xde, 0xeb, 0x27, 0x19, 0x85, 0xd0, 0xcb, 0xe4, 0x0a, 0x2f, 0x13, 0xdb, 0xd5, 0xba, 0xe0, 0x8c, 0x32, 0x8b, 0x97, 0xdd, 0xef, 0xbc, 0xe0, 0x7a, 0x2d, 0x90, 0x7e, 0x09, 0xe9, 0x1f, 0x26, 0xf2, 0xf4, 0x48, 0xea, 0x06, 0x76, 0x26, 0xe6, 0x3b, 0xce, 0x4e, 0xc9, 0xf9, 0x0f, 0x38, 0x90, 0x26, 0x87, 0x65, 0x36, 0x9a, 0xea, 0x6a, 0xfe, 0xb1, 0xdb, 0x46, 0xdf, 0x14, 0xfd, 0x13, 0x53, 0xfb, 0x5b, 0x35, 0x6e, 0xe7, 0xd5, 0xd8, 0x39, 0xf7, 0x2d, 0xb9}; static unsigned char coeff2_data[] = {0x01, 0xba, 0x66, 0x0a, 0xa2, 0x86, 0xc0, 0x57, 0x7f, 0x4e, 0x68, 0xb1, 0x86, 0x63, 0x23, 0x5b, 0x0e, 0xeb, 0x93, 0x42, 0xd1, 0xaa, 0x15, 0x13, 0xcc, 0x29, 0x71, 0x8a, 0xb0, 0xe0, 0xc9, 0x67, 0xde, 0x1a, 0x7c, 0x1a, 0xef, 0xa7, 0x08, 0x85, 0xb3, 0xae, 0x98, 0x99, 0xde, 0xaf, 0x09, 0x38, 0xfc, 0x46, 0x29, 0x5f, 0x4f, 0x7e, 0x01, 0x6c, 0x50, 0x13, 0x95, 0x91, 0x4c, 0x0f, 0x00, 0xba, 0xca, 0x40, 0xa3, 0xd0, 0x58, 0xb6, 0x62, 0x4c, 0xd1, 0xb6, 0xd3, 0x29, 0x5d, 0x82, 0xb3, 0x3d, 0x61, 0xbe, 0x5d, 0xf0, 0x4b, 0xf4}; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(p2 = BN_bin2bn(p2_data, sizeof(p2_data), NULL)) || !TEST_ptr(exp3 = BN_bin2bn(exp3_data, sizeof(exp3_data), NULL)) || !TEST_ptr(coeff2 = BN_bin2bn(coeff2_data, sizeof(coeff2_data), NULL)) || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL)) || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL)) || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2, q)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR3, p2)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1; cdata[0].comparebn = dmp1; cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2; cdata[1].comparebn = dmq1; cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1; cdata[2].comparebn = iqmp; cdata[3].pname = OSSL_PKEY_PARAM_RSA_EXPONENT3; cdata[3].comparebn = exp3; cdata[4].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT2; cdata[4].comparebn = coeff2; cdata[5].pname = OSSL_PKEY_PARAM_RSA_N; cdata[5].comparebn = n; cdata[6].pname = OSSL_PKEY_PARAM_RSA_E; cdata[6].comparebn = e; cdata[7].pname = OSSL_PKEY_PARAM_RSA_D; cdata[7].comparebn = d; cdata[8].pname = OSSL_PKEY_PARAM_RSA_FACTOR1; cdata[8].comparebn = p; cdata[9].pname = OSSL_PKEY_PARAM_RSA_FACTOR2; cdata[9].comparebn = q; cdata[10].pname = OSSL_PKEY_PARAM_RSA_FACTOR3; cdata[10].comparebn = p2; cdata[11].pname = NULL; cdata[11].comparebn = NULL; ret = do_fromdata_rsa_derive(fromdata_params, cdata, 2048, 112, 256); err: BN_free(n); BN_free(e); BN_free(d); BN_free(p); BN_free(p2); BN_free(q); BN_free(dmp1); BN_free(dmq1); BN_free(iqmp); BN_free(exp3); BN_free(coeff2); OSSL_PARAM_BLD_free(bld); return ret; } static int test_evp_pkey_get_bn_param_large(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL; /* * The buffer size chosen here for n_data larger than the buffer used * internally in EVP_PKEY_get_bn_param. */ static unsigned char n_data[2050]; static const unsigned char e_data[] = { 0x1, 0x00, 0x01 }; static const unsigned char d_data[] = { 0x99, 0x33, 0x13, 0x7b }; /* N is a large buffer */ memset(n_data, 0xCE, sizeof(n_data)); if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out)) || !TEST_BN_eq(n, n_out)) goto err; ret = 1; err: BN_free(n_out); BN_free(n); BN_free(e); BN_free(d); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(key_ctx); EVP_PKEY_CTX_free(ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } #ifndef OPENSSL_NO_DH static int test_fromdata_dh_named_group(void) { int ret = 0; int gindex = 0, pcounter = 0, hindex = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; size_t len; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; OSSL_PARAM *fromdata_params = NULL; OSSL_PARAM_BLD *bld = NULL; char name_out[80]; unsigned char seed_out[32]; /* * DH key data was generated using the following: * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048 * -pkeyopt priv_len:224 -text */ static const unsigned char priv_data[] = { 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, 0x87, 0xe8, 0xa9, 0x7b, }; static const unsigned char pub_data[] = { 0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf, 0x33, 0x42, 0x83, 0x42 }; static const char group_name[] = "ffdhe2048"; static const long priv_len = 224; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN, priv_len)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; /* * A few extra checks of EVP_PKEY_get_utf8_string_param() to see that * it behaves as expected with regards to string length and terminating * NUL byte. */ if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, NULL, sizeof(name_out), &len)) || !TEST_size_t_eq(len, sizeof(group_name) - 1) /* Just enough space to hold the group name and a terminating NUL */ || !TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name), &len)) || !TEST_size_t_eq(len, sizeof(group_name) - 1) /* Too small buffer to hold the terminating NUL byte */ || !TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name) - 1, &len)) /* Too small buffer to hold the whole group name, even! */ || !TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name) - 2, &len))) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 256) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_str_eq(name_out, group_name) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) || !TEST_ptr(q) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) || !TEST_BN_eq(&ossl_bignum_const_2, g) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j)) || !TEST_ptr_null(j) || !TEST_false(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex)) || !TEST_int_eq(gindex, -1) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) || !TEST_int_eq(hindex, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter)) || !TEST_int_eq(pcounter, -1)) goto err; BN_free(p); p = NULL; BN_free(q); q = NULL; BN_free(g); g = NULL; BN_free(j); j = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("DH", pk) && test_print_key_using_encoder("DH", pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: BN_free(p); BN_free(q); BN_free(g); BN_free(j); BN_free(pub); BN_free(priv); BN_free(pub_out); BN_free(priv_out); EVP_PKEY_free(copy_pk); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } static int test_fromdata_dh_fips186_4(void) { int ret = 0; int gindex = 0, pcounter = 0, hindex = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *dup_pk = NULL; size_t len; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; char name_out[80]; unsigned char seed_out[32]; /* * DH key data was generated using the following: * openssl genpkey -algorithm DH * -pkeyopt group:ffdhe2048 -pkeyopt priv_len:224 -text */ static const unsigned char priv_data[] = { 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, 0x87, 0xe8, 0xa9, 0x7b, }; static const unsigned char pub_data[] = { 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf, 0x33, 0x42, 0x83, 0x42 }; static const char group_name[] = "ffdhe2048"; static const long priv_len = 224; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN, priv_len)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 256) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_str_eq(name_out, group_name) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) || !TEST_ptr(q) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) || !TEST_BN_eq(&ossl_bignum_const_2, g) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j)) || !TEST_ptr_null(j) || !TEST_false(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex)) || !TEST_int_eq(gindex, -1) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) || !TEST_int_eq(hindex, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter)) || !TEST_int_eq(pcounter, -1)) goto err; BN_free(p); p = NULL; BN_free(q); q = NULL; BN_free(g); g = NULL; BN_free(j); j = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; ret = test_print_key_using_pem("DH", pk) && test_print_key_using_encoder("DH", pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: BN_free(p); BN_free(q); BN_free(g); BN_free(j); BN_free(pub); BN_free(priv); BN_free(pub_out); BN_free(priv_out); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } #endif #ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_ECX /* Array indexes used in test_fromdata_ecx */ # define PRIV_KEY 0 # define PUB_KEY 1 # define X25519_IDX 0 # define X448_IDX 1 # define ED25519_IDX 2 # define ED448_IDX 3 /* * tst uses indexes 0 ... (3 * 4 - 1) * For the 4 ECX key types (X25519_IDX..ED448_IDX) * 0..3 = public + private key. * 4..7 = private key (This will generate the public key from the private key) * 8..11 = public key */ static int test_fromdata_ecx(int tst) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *ctx2 = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; const char *alg = NULL; size_t len; unsigned char out_pub[ED448_KEYLEN]; unsigned char out_priv[ED448_KEYLEN]; OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */ static unsigned char key_numbers[4][2][ED448_KEYLEN] = { /* X25519: Keys from RFC 7748 6.1 */ { /* Private Key */ { 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a }, /* Public Key */ { 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a } }, /* X448: Keys from RFC 7748 6.2 */ { /* Private Key */ { 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf, 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba, 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91, 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2, 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b }, /* Public Key */ { 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22, 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8, 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4, 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f, 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0 } }, /* ED25519: Keys from RFC 8032 */ { /* Private Key */ { 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60 }, /* Public Key */ { 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a } }, /* ED448: Keys from RFC 8032 */ { /* Private Key */ { 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b }, /* Public Key */ { 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80 } } }; OSSL_PARAM x25519_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[X25519_IDX][PRIV_KEY], X25519_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[X25519_IDX][PUB_KEY], X25519_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM x448_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[X448_IDX][PRIV_KEY], X448_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[X448_IDX][PUB_KEY], X448_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM ed25519_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[ED25519_IDX][PRIV_KEY], ED25519_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[ED25519_IDX][PUB_KEY], ED25519_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM ed448_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[ED448_IDX][PRIV_KEY], ED448_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[ED448_IDX][PUB_KEY], ED448_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM *fromdata_params = NULL; int bits = 0, security_bits = 0, size = 0; OSSL_PARAM *orig_fromdata_params = NULL; switch (tst & 3) { case X25519_IDX: fromdata_params = x25519_fromdata_params; bits = X25519_BITS; security_bits = X25519_SECURITY_BITS; size = X25519_KEYLEN; alg = "X25519"; break; case X448_IDX: fromdata_params = x448_fromdata_params; bits = X448_BITS; security_bits = X448_SECURITY_BITS; size = X448_KEYLEN; alg = "X448"; break; case ED25519_IDX: fromdata_params = ed25519_fromdata_params; bits = ED25519_BITS; security_bits = ED25519_SECURITY_BITS; size = ED25519_SIGSIZE; alg = "ED25519"; break; case ED448_IDX: fromdata_params = ed448_fromdata_params; bits = ED448_BITS; security_bits = ED448_SECURITY_BITS; size = ED448_SIGSIZE; alg = "ED448"; break; default: goto err; } ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); if (!TEST_ptr(ctx)) goto err; orig_fromdata_params = fromdata_params; if (tst > 7) { /* public key only */ fromdata_params++; } else if (tst > 3) { /* private key only */ params[0] = fromdata_params[0]; params[1] = fromdata_params[2]; fromdata_params = params; } if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits) || !TEST_int_eq(EVP_PKEY_get_size(pk), size) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, pk, NULL))) goto err; if (tst <= 7) { if (!TEST_int_gt(EVP_PKEY_check(ctx2), 0)) goto err; if (!TEST_true(EVP_PKEY_get_octet_string_param( pk, orig_fromdata_params[PRIV_KEY].key, out_priv, sizeof(out_priv), &len)) || !TEST_mem_eq(out_priv, len, orig_fromdata_params[PRIV_KEY].data, orig_fromdata_params[PRIV_KEY].data_size) || !TEST_true(EVP_PKEY_get_octet_string_param( pk, orig_fromdata_params[PUB_KEY].key, out_pub, sizeof(out_pub), &len)) || !TEST_mem_eq(out_pub, len, orig_fromdata_params[PUB_KEY].data, orig_fromdata_params[PUB_KEY].data_size)) goto err; } else { /* The private key check should fail if there is only a public key */ if (!TEST_int_gt(EVP_PKEY_public_check(ctx2), 0) || !TEST_int_le(EVP_PKEY_private_check(ctx2), 0) || !TEST_int_le(EVP_PKEY_check(ctx2), 0)) goto err; } EVP_PKEY_CTX_free(ctx2); ctx2 = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) /* This should succeed because there are no parameters to copy */ || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, copy_pk, NULL)) /* This should fail because copy_pk has no pubkey */ || !TEST_int_le(EVP_PKEY_public_check(ctx2), 0)) goto err; EVP_PKEY_CTX_free(ctx2); ctx2 = NULL; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (tst > 7) ret = test_print_key_using_encoder_public(alg, pk); else ret = test_print_key_using_pem(alg, pk) && test_print_key_using_encoder(alg, pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(ctx2); return ret; } # endif /* OPENSSL_NO_ECX */ static int test_fromdata_ec(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; OSSL_PARAM_BLD *bld = NULL; BIGNUM *ec_priv_bn = NULL; BIGNUM *bn_priv = NULL; OSSL_PARAM *fromdata_params = NULL; const char *alg = "EC"; const char *curve = "prime256v1"; const char bad_curve[] = "nonexistent-curve"; OSSL_PARAM nokey_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; /* UNCOMPRESSED FORMAT */ static const unsigned char ec_pub_keydata[] = { POINT_CONVERSION_UNCOMPRESSED, 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63, 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d, 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73, 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2, 0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f, 0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d, 0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf, 0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29 }; /* SAME BUT COMPRESSED FORMAT */ static const unsigned char ec_pub_keydata_compressed[] = { POINT_CONVERSION_COMPRESSED+1, 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63, 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d, 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73, 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2 }; static const unsigned char ec_priv_keydata[] = { 0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03, 0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59, 0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e, 0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad }; unsigned char out_pub[sizeof(ec_pub_keydata)]; char out_curve_name[80]; const OSSL_PARAM *gettable = NULL; size_t len; EC_GROUP *group = NULL; BIGNUM *group_a = NULL; BIGNUM *group_b = NULL; BIGNUM *group_p = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *p = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata, sizeof(ec_priv_keydata), NULL))) goto err; if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, curve, 0) <= 0) goto err; /* * We intentionally provide the input point in compressed format, * and avoid setting `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT`. * * Later on we check what format is used when exporting the * `OSSL_PKEY_PARAM_PUB_KEY` and expect to default to uncompressed * format. */ if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, ec_pub_keydata_compressed, sizeof(ec_pub_keydata_compressed)) <= 0) goto err; if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0) goto err; if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); if (!TEST_ptr(ctx)) goto err; /* try importing parameters with bad curve first */ nokey_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, (char *)bad_curve, sizeof(bad_curve)); if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEY_PARAMETERS, nokey_params), 0) || !TEST_ptr_null(pk)) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128) || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 35 * 2) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PUB_KEY)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PRIV_KEY))) goto err; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(OBJ_sn2nid(curve))) || !TEST_ptr(group_p = BN_new()) || !TEST_ptr(group_a = BN_new()) || !TEST_ptr(group_b = BN_new()) || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL))) goto err; if (!TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_A, &a)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_B, &b)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_P, &p))) goto err; if (!TEST_BN_eq(group_p, p) || !TEST_BN_eq(group_a, a) || !TEST_BN_eq(group_b, b)) goto err; if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, out_curve_name, sizeof(out_curve_name), &len) || !TEST_str_eq(out_curve_name, curve) || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY, out_pub, sizeof(out_pub), &len) /* * Our providers use uncompressed format by default if * `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT` was not * explicitly set, irrespective of the format used for the * input point given as a param to create this key. */ || !TEST_true(out_pub[0] == POINT_CONVERSION_UNCOMPRESSED) || !TEST_mem_eq(out_pub + 1, len - 1, ec_pub_keydata + 1, sizeof(ec_pub_keydata) - 1) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &bn_priv)) || !TEST_BN_eq(ec_priv_bn, bn_priv)) goto err; BN_free(bn_priv); bn_priv = NULL; ret = test_print_key_using_pem(alg, pk) && test_print_key_using_encoder(alg, pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: EC_GROUP_free(group); BN_free(group_a); BN_free(group_b); BN_free(group_p); BN_free(a); BN_free(b); BN_free(p); BN_free(bn_priv); BN_free(ec_priv_bn); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); return ret; } static int test_ec_dup_no_operation(void) { int ret = 0; EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL; EVP_PKEY *param = NULL, *pkey = NULL; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1), 0) || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0) || !TEST_ptr(param)) goto err; EVP_PKEY_CTX_free(pctx); pctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL)) || !TEST_ptr(kctx = EVP_PKEY_CTX_dup(ctx)) || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0) || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(param); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(pctx); return ret; } /* Test that keygen doesn't support EVP_PKEY_CTX_dup */ static int test_ec_dup_keygen_operation(void) { int ret = 0; EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL; EVP_PKEY *param = NULL, *pkey = NULL; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1), 0) || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0) || !TEST_ptr(param)) goto err; EVP_PKEY_CTX_free(pctx); pctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_ptr_null(kctx = EVP_PKEY_CTX_dup(ctx))) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(param); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(pctx); return ret; } #endif /* OPENSSL_NO_EC */ #ifndef OPENSSL_NO_DSA static int test_fromdata_dsa_fips186_4(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL; int gindex_out = 0, pcounter_out = 0, hindex_out = 0; char name_out[80]; unsigned char seed_out[32]; size_t len; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; /* * DSA parameter data was generated using the following: * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \ * -pkeyopt qbits:256 -pkeyopt type:0 \ * -pkeyopt gindex:1 -out dsa_params.pem -text */ static const unsigned char p_data[] = { 0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b, 0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21, 0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8, 0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7, 0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc, 0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4, 0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23, 0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64, 0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e, 0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73, 0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a, 0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6, 0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde, 0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd, 0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c, 0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71, 0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18, 0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56, 0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85, 0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c, 0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79, 0x7c, 0xfe, 0xaf, 0x6a, 0x05 }; static const unsigned char q_data[] = { 0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04, 0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40, 0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7 }; static const unsigned char g_data[] = { 0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b, 0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b, 0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4, 0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6, 0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa, 0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57, 0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72, 0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c, 0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60, 0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1, 0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35, 0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e, 0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05, 0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25, 0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb, 0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a, 0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25, 0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0, 0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd, 0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53, 0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62, 0x02, 0x6f, 0x96, 0x36 }; static const unsigned char seed_data[] = { 0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62, 0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04, 0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84 }; const int gindex = 1; const int pcounter = 53; /* * The keypair was generated using * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \ * -pkeyopt gindex:1 \ * -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text */ static const unsigned char priv_data[] = { 0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73, 0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8, 0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90 }; static const unsigned char pub_data[] = { 0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29, 0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80, 0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02, 0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73, 0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf, 0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda, 0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda, 0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47, 0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08, 0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1, 0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2, 0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b, 0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b, 0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3, 0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05, 0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4, 0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27, 0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6, 0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49, 0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a, 0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60, 0x48, 0xd1, 0x8a, 0xbd }; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_FFC_SEED, seed_data, sizeof(seed_data))) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX, gindex)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_PCOUNTER, pcounter)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; while (dup_pk == NULL) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 2 * (3 + sizeof(q_data))) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p_out)) || !TEST_BN_eq(p, p_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q_out)) || !TEST_BN_eq(q, q_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g_out)) || !TEST_BN_eq(g, g_out) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j_out)) || !TEST_ptr_null(j_out) || !TEST_true(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex_out)) || !TEST_int_eq(gindex, gindex_out) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex_out)) || !TEST_int_eq(hindex_out, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter_out)) || !TEST_int_eq(pcounter, pcounter_out)) goto err; BN_free(p); p = NULL; BN_free(q); q = NULL; BN_free(g); g = NULL; BN_free(j_out); j_out = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("DSA", pk) && test_print_key_using_encoder("DSA", pk); if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); BN_free(p); BN_free(q); BN_free(g); BN_free(pub); BN_free(priv); BN_free(p_out); BN_free(q_out); BN_free(g_out); BN_free(pub_out); BN_free(priv_out); BN_free(j_out); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); return ret; } static int test_check_dsa(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) || !TEST_int_le(EVP_PKEY_check(ctx), 0) || !TEST_int_le(EVP_PKEY_public_check(ctx), 0) || !TEST_int_le(EVP_PKEY_private_check(ctx), 0) || !TEST_int_le(EVP_PKEY_pairwise_check(ctx), 0)) goto err; ret = 1; err: EVP_PKEY_CTX_free(ctx); return ret; } #endif /* OPENSSL_NO_DSA */ static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key, size_t keylen, char *salt) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5); OSSL_PARAM *p = params; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, strlen(salt)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (unsigned char *)key, keylen); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, "EXTRACT_ONLY", 0); *p = OSSL_PARAM_construct_end(); return params; } static int test_evp_pkey_ctx_dup_kdf(void) { int ret = 0; size_t len = 0, dlen = 0; EVP_PKEY_CTX *pctx = NULL, *dctx = NULL; OSSL_PARAM *params = NULL; if (!TEST_ptr(params = do_construct_hkdf_params("sha256", "secret", 6, "salt"))) goto err; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "HKDF", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_derive_init_ex(pctx, params), 1)) goto err; if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(pctx))) goto err; if (!TEST_int_eq(EVP_PKEY_derive(pctx, NULL, &len), 1) || !TEST_size_t_eq(len, SHA256_DIGEST_LENGTH) || !TEST_int_eq(EVP_PKEY_derive(dctx, NULL, &dlen), 1) || !TEST_size_t_eq(dlen, SHA256_DIGEST_LENGTH)) goto err; ret = 1; err: OPENSSL_free(params); EVP_PKEY_CTX_free(dctx); EVP_PKEY_CTX_free(pctx); return ret; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(datadir = test_get_argument(0))) return 0; ADD_TEST(test_evp_pkey_ctx_dup_kdf); ADD_TEST(test_evp_pkey_get_bn_param_large); ADD_TEST(test_fromdata_rsa); ADD_TEST(test_fromdata_rsa_derive_from_pq_sp800); ADD_TEST(test_fromdata_rsa_derive_from_pq_multiprime); #ifndef OPENSSL_NO_DH ADD_TEST(test_fromdata_dh_fips186_4); ADD_TEST(test_fromdata_dh_named_group); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_check_dsa); ADD_TEST(test_fromdata_dsa_fips186_4); #endif #ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_ECX ADD_ALL_TESTS(test_fromdata_ecx, 4 * 3); # endif ADD_TEST(test_fromdata_ec); ADD_TEST(test_ec_dup_no_operation); ADD_TEST(test_ec_dup_keygen_operation); #endif return 1; }
./openssl/test/afalgtest.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* We need to use some engine deprecated APIs */ #define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <openssl/opensslconf.h> #include <string.h> #include <openssl/engine.h> #include <openssl/evp.h> #include <openssl/rand.h> #include "testutil.h" /* Use a buffer size which is not aligned to block size */ #define BUFFER_SIZE 17 #ifndef OPENSSL_NO_ENGINE static ENGINE *e; static int test_afalg_aes_cbc(int keysize_idx) { EVP_CIPHER_CTX *ctx; const EVP_CIPHER *cipher; unsigned char ebuf[BUFFER_SIZE + 32]; unsigned char dbuf[BUFFER_SIZE + 32]; const unsigned char *enc_result = NULL; int encl, encf, decl, decf; int ret = 0; static const unsigned char key[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06" "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"; static const unsigned char iv[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41"; /* input = "Single block msg\n" 17 Bytes*/ static const unsigned char in[BUFFER_SIZE] = "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67" "\x0a"; static const unsigned char encresult_128[BUFFER_SIZE] = "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a" "\x2d"; static const unsigned char encresult_192[BUFFER_SIZE] = "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55" "\xeb"; static const unsigned char encresult_256[BUFFER_SIZE] = "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4" "\x13"; #ifdef OSSL_SANITIZE_MEMORY /* * Initialise the encryption & decryption buffers to pacify the memory * sanitiser. The sanitiser doesn't know that this memory is modified * by the engine, this tells it that all is good. */ OPENSSL_cleanse(ebuf, sizeof(ebuf)); OPENSSL_cleanse(dbuf, sizeof(dbuf)); #endif switch (keysize_idx) { case 0: cipher = EVP_aes_128_cbc(); enc_result = &encresult_128[0]; break; case 1: cipher = EVP_aes_192_cbc(); enc_result = &encresult_192[0]; break; case 2: cipher = EVP_aes_256_cbc(); enc_result = &encresult_256[0]; break; default: cipher = NULL; } if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) return 0; if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1)) || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE)) || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf))) goto end; encl += encf; if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE)) goto end; if (!TEST_true(EVP_CIPHER_CTX_reset(ctx)) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0)) || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl)) || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf))) goto end; decl += decf; if (!TEST_int_eq(decl, BUFFER_SIZE) || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE)) goto end; ret = 1; end: EVP_CIPHER_CTX_free(ctx); return ret; } static int test_pr16743(void) { int ret = 0; const EVP_CIPHER *cipher; EVP_CIPHER_CTX *ctx; if (!TEST_true(ENGINE_init(e))) return 0; cipher = ENGINE_get_cipher(e, NID_aes_128_cbc); ctx = EVP_CIPHER_CTX_new(); if (cipher != NULL && ctx != NULL) ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL); TEST_true(ret); EVP_CIPHER_CTX_free(ctx); ENGINE_finish(e); return ret; } int global_init(void) { ENGINE_load_builtin_engines(); # ifndef OPENSSL_NO_STATIC_ENGINE OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL); # endif return 1; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_ENGINE if ((e = ENGINE_by_id("afalg")) == NULL) { /* Probably a platform env issue, not a test failure. */ TEST_info("Can't load AFALG engine"); } else { ADD_ALL_TESTS(test_afalg_aes_cbc, 3); ADD_TEST(test_pr16743); } #endif return 1; } #ifndef OPENSSL_NO_ENGINE void cleanup_tests(void) { ENGINE_free(e); } #endif
./openssl/test/algorithmid_test.c
/* * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/asn1.h> #include <openssl/pem.h> #include "internal/sizes.h" #include "crypto/evp.h" #include "testutil.h" /* Collected arguments */ static const char *eecert_filename = NULL; /* For test_x509_file() */ static const char *cacert_filename = NULL; /* For test_x509_file() */ static const char *pubkey_filename = NULL; /* For test_spki_file() */ #define ALGORITHMID_NAME "algorithm-id" static int test_spki_aid(X509_PUBKEY *pubkey, const char *filename) { const ASN1_OBJECT *oid; X509_ALGOR *alg = NULL; EVP_PKEY *pkey = NULL; EVP_KEYMGMT *keymgmt = NULL; void *keydata = NULL; char name[OSSL_MAX_NAME_SIZE] = ""; unsigned char *algid_legacy = NULL; int algid_legacy_len = 0; static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE]; size_t algid_prov_len = 0; const OSSL_PARAM *gettable_params = NULL; OSSL_PARAM params[] = { OSSL_PARAM_octet_string(ALGORITHMID_NAME, &algid_prov, sizeof(algid_prov)), OSSL_PARAM_END }; int ret = 0; if (!TEST_true(X509_PUBKEY_get0_param(NULL, NULL, NULL, &alg, pubkey)) || !TEST_ptr(pkey = X509_PUBKEY_get0(pubkey))) goto end; if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0)) goto end; X509_ALGOR_get0(&oid, NULL, NULL, alg); if (!TEST_int_gt(OBJ_obj2txt(name, sizeof(name), oid, 0), 0)) goto end; /* * We use an internal functions to ensure we have a provided key. * Note that |keydata| should not be freed, as it's cached in |pkey|. * The |keymgmt|, however, should, as its reference count is incremented * in this function. */ if ((keydata = evp_pkey_export_to_provider(pkey, NULL, &keymgmt, NULL)) == NULL) { TEST_info("The public key found in '%s' doesn't have provider support." " Skipping...", filename); ret = 1; goto end; } if (!TEST_true(EVP_KEYMGMT_is_a(keymgmt, name))) { TEST_info("The AlgorithmID key type (%s) for the public key found in" " '%s' doesn't match the key type of the extracted public" " key.", name, filename); ret = 1; goto end; } if (!TEST_ptr(gettable_params = EVP_KEYMGMT_gettable_params(keymgmt)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) { TEST_info("The %s provider keymgmt appears to lack support for algorithm-id." " Skipping...", name); ret = 1; goto end; } algid_prov[0] = '\0'; if (!TEST_true(evp_keymgmt_get_params(keymgmt, keydata, params))) goto end; algid_prov_len = params[0].return_size; /* We now have all the algorithm IDs we need, let's compare them */ if (TEST_mem_eq(algid_legacy, algid_legacy_len, algid_prov, algid_prov_len)) ret = 1; end: EVP_KEYMGMT_free(keymgmt); OPENSSL_free(algid_legacy); return ret; } static int test_x509_spki_aid(X509 *cert, const char *filename) { X509_PUBKEY *pubkey = X509_get_X509_PUBKEY(cert); return test_spki_aid(pubkey, filename); } static int test_x509_sig_aid(X509 *eecert, const char *ee_filename, X509 *cacert, const char *ca_filename) { const ASN1_OBJECT *sig_oid = NULL; const X509_ALGOR *alg = NULL; int sig_nid = NID_undef, dig_nid = NID_undef, pkey_nid = NID_undef; EVP_MD_CTX *mdctx = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; unsigned char *algid_legacy = NULL; int algid_legacy_len = 0; static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE]; size_t algid_prov_len = 0; const OSSL_PARAM *gettable_params = NULL; OSSL_PARAM params[] = { OSSL_PARAM_octet_string("algorithm-id", &algid_prov, sizeof(algid_prov)), OSSL_PARAM_END }; int ret = 0; X509_get0_signature(NULL, &alg, eecert); X509_ALGOR_get0(&sig_oid, NULL, NULL, alg); if (!TEST_int_eq(X509_ALGOR_cmp(alg, X509_get0_tbs_sigalg(eecert)), 0)) goto end; if (!TEST_int_ne(sig_nid = OBJ_obj2nid(sig_oid), NID_undef) || !TEST_true(OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) || !TEST_ptr(pkey = X509_get0_pubkey(cacert))) goto end; if (!TEST_true(EVP_PKEY_is_a(pkey, OBJ_nid2sn(pkey_nid)))) { TEST_info("The '%s' pubkey can't be used to verify the '%s' signature", ca_filename, ee_filename); TEST_info("Signature algorithm is %s (pkey type %s, hash type %s)", OBJ_nid2sn(sig_nid), OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid)); TEST_info("Pkey key type is %s", EVP_PKEY_get0_type_name(pkey)); goto end; } if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0)) goto end; if (!TEST_ptr(mdctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestVerifyInit_ex(mdctx, &pctx, OBJ_nid2sn(dig_nid), NULL, NULL, pkey, NULL))) { TEST_info("Couldn't initialize a DigestVerify operation with " "pkey type %s and hash type %s", OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid)); goto end; } if (!TEST_ptr(gettable_params = EVP_PKEY_CTX_gettable_params(pctx)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) { TEST_info("The %s provider keymgmt appears to lack support for algorithm-id" " Skipping...", OBJ_nid2sn(pkey_nid)); ret = 1; goto end; } algid_prov[0] = '\0'; if (!TEST_true(EVP_PKEY_CTX_get_params(pctx, params))) goto end; algid_prov_len = params[0].return_size; /* We now have all the algorithm IDs we need, let's compare them */ if (TEST_mem_eq(algid_legacy, algid_legacy_len, algid_prov, algid_prov_len)) ret = 1; end: EVP_MD_CTX_free(mdctx); /* pctx is free by EVP_MD_CTX_free() */ OPENSSL_free(algid_legacy); return ret; } static int test_spki_file(void) { X509_PUBKEY *pubkey = NULL; BIO *b = BIO_new_file(pubkey_filename, "r"); int ret = 0; if (b == NULL) { TEST_error("Couldn't open '%s' for reading\n", pubkey_filename); TEST_openssl_errors(); goto end; } if ((pubkey = PEM_read_bio_X509_PUBKEY(b, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a SubjectPublicKeyInfo in PEM format\n", pubkey_filename); TEST_openssl_errors(); goto end; } ret = test_spki_aid(pubkey, pubkey_filename); end: BIO_free(b); X509_PUBKEY_free(pubkey); return ret; } static int test_x509_files(void) { X509 *eecert = NULL, *cacert = NULL; BIO *bee = NULL, *bca = NULL; int ret = 0; if ((bee = BIO_new_file(eecert_filename, "r")) == NULL) { TEST_error("Couldn't open '%s' for reading\n", eecert_filename); TEST_openssl_errors(); goto end; } if ((bca = BIO_new_file(cacert_filename, "r")) == NULL) { TEST_error("Couldn't open '%s' for reading\n", cacert_filename); TEST_openssl_errors(); goto end; } if ((eecert = PEM_read_bio_X509(bee, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n", eecert_filename); TEST_openssl_errors(); goto end; } if ((cacert = PEM_read_bio_X509(bca, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n", cacert_filename); TEST_openssl_errors(); goto end; } ret = test_x509_sig_aid(eecert, eecert_filename, cacert, cacert_filename) & test_x509_spki_aid(eecert, eecert_filename) & test_x509_spki_aid(cacert, cacert_filename); end: BIO_free(bee); BIO_free(bca); X509_free(eecert); X509_free(cacert); return ret; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_X509, OPT_SPKI, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file...\n"), { "x509", OPT_X509, '-', "Test X.509 certificates. Requires two files" }, { "spki", OPT_SPKI, '-', "Test public keys in SubjectPublicKeyInfo form. Requires one file" }, { OPT_HELP_STR, 1, '-', "file...\tFile(s) to run tests on. All files must be PEM encoded.\n" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; int n, x509 = 0, spki = 0, testcount = 0; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_X509: x509 = 1; break; case OPT_SPKI: spki = 1; break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } /* |testcount| adds all the given test types together */ testcount = x509 + spki; if (testcount < 1) BIO_printf(bio_err, "No test type given\n"); else if (testcount > 1) BIO_printf(bio_err, "Only one test type may be given\n"); if (testcount != 1) return 0; n = test_get_argument_count(); if (spki && n == 1) { pubkey_filename = test_get_argument(0); } else if (x509 && n == 2) { eecert_filename = test_get_argument(0); cacert_filename = test_get_argument(1); } if (spki && pubkey_filename == NULL) { BIO_printf(bio_err, "Missing -spki argument\n"); return 0; } else if (x509 && (eecert_filename == NULL || cacert_filename == NULL)) { BIO_printf(bio_err, "Missing -x509 argument(s)\n"); return 0; } if (x509) ADD_TEST(test_x509_files); if (spki) ADD_TEST(test_spki_file); return 1; }
./openssl/test/ctype_internal_test.c
/* * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "testutil.h" #include "crypto/ctype.h" #include "internal/nelem.h" #include <ctype.h> #include <stdio.h> /* * Even though the VMS C RTL claims to be C99 compatible, it's not entirely * so far (C RTL version 8.4). Same applies to OSF. For the sake of these * tests, we therefore define our own. */ #if (defined(__VMS) && __CRTL_VER <= 80400000) || defined(__osf__) static int isblank(int c) { return c == ' ' || c == '\t'; } #endif static int test_ctype_chars(int n) { if (!TEST_int_eq(isascii((unsigned char)n) != 0, ossl_isascii(n) != 0)) return 0; if (!ossl_isascii(n)) return 1; return TEST_int_eq(isalpha(n) != 0, ossl_isalpha(n) != 0) && TEST_int_eq(isalnum(n) != 0, ossl_isalnum(n) != 0) #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && TEST_int_eq(isblank(n) != 0, ossl_isblank(n) != 0) #endif && TEST_int_eq(iscntrl(n) != 0, ossl_iscntrl(n) != 0) && TEST_int_eq(isdigit(n) != 0, ossl_isdigit(n) != 0) && TEST_int_eq(isgraph(n) != 0, ossl_isgraph(n) != 0) && TEST_int_eq(islower(n) != 0, ossl_islower(n) != 0) && TEST_int_eq(isprint(n) != 0, ossl_isprint(n) != 0) && TEST_int_eq(ispunct(n) != 0, ossl_ispunct(n) != 0) && TEST_int_eq(isspace(n) != 0, ossl_isspace(n) != 0) && TEST_int_eq(isupper(n) != 0, ossl_isupper(n) != 0) && TEST_int_eq(isxdigit(n) != 0, ossl_isxdigit(n) != 0); } static struct { int u; int l; } case_change[] = { { 'A', 'a' }, { 'X', 'x' }, { 'Z', 'z' }, { '0', '0' }, { '%', '%' }, { '~', '~' }, { 0, 0 }, { EOF, EOF } }; static int test_ctype_toupper(int n) { return TEST_int_eq(ossl_toupper(case_change[n].l), case_change[n].u) && TEST_int_eq(ossl_toupper(case_change[n].u), case_change[n].u); } static int test_ctype_tolower(int n) { return TEST_int_eq(ossl_tolower(case_change[n].u), case_change[n].l) && TEST_int_eq(ossl_tolower(case_change[n].l), case_change[n].l); } static int test_ctype_eof(void) { return test_ctype_chars(EOF); } int setup_tests(void) { ADD_ALL_TESTS(test_ctype_chars, 256); ADD_ALL_TESTS(test_ctype_toupper, OSSL_NELEM(case_change)); ADD_ALL_TESTS(test_ctype_tolower, OSSL_NELEM(case_change)); ADD_TEST(test_ctype_eof); return 1; }
./openssl/test/bftest.c
/* * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * BF low level APIs are deprecated for public use, but still ok for internal * use. */ #include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_BF is defined */ #include "testutil.h" #include "internal/nelem.h" #ifndef OPENSSL_NO_BF # include <openssl/blowfish.h> # ifdef CHARSET_EBCDIC # include <openssl/ebcdic.h> # endif static char bf_key[2][30] = { "abcdefghijklmnopqrstuvwxyz", "Who is John Galt?" }; /* big endian */ static BF_LONG bf_plain[2][2] = { {0x424c4f57L, 0x46495348L}, {0xfedcba98L, 0x76543210L} }; static BF_LONG bf_cipher[2][2] = { {0x324ed0feL, 0xf413a203L}, {0xcc91732bL, 0x8022f684L} }; /************/ /* Lets use the DES test vectors :-) */ # define NUM_TESTS 34 static unsigned char ecb_data[NUM_TESTS][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}, {0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57}, {0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E}, {0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86}, {0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E}, {0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6}, {0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE}, {0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6}, {0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE}, {0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16}, {0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F}, {0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46}, {0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E}, {0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76}, {0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07}, {0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F}, {0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7}, {0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, {0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6}, {0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF}, {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, {0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E}, {0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10} }; static unsigned char plain_data[NUM_TESTS][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0x01, 0xA1, 0xD6, 0xD0, 0x39, 0x77, 0x67, 0x42}, {0x5C, 0xD5, 0x4C, 0xA8, 0x3D, 0xEF, 0x57, 0xDA}, {0x02, 0x48, 0xD4, 0x38, 0x06, 0xF6, 0x71, 0x72}, {0x51, 0x45, 0x4B, 0x58, 0x2D, 0xDF, 0x44, 0x0A}, {0x42, 0xFD, 0x44, 0x30, 0x59, 0x57, 0x7F, 0xA2}, {0x05, 0x9B, 0x5E, 0x08, 0x51, 0xCF, 0x14, 0x3A}, {0x07, 0x56, 0xD8, 0xE0, 0x77, 0x47, 0x61, 0xD2}, {0x76, 0x25, 0x14, 0xB8, 0x29, 0xBF, 0x48, 0x6A}, {0x3B, 0xDD, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02}, {0x26, 0x95, 0x5F, 0x68, 0x35, 0xAF, 0x60, 0x9A}, {0x16, 0x4D, 0x5E, 0x40, 0x4F, 0x27, 0x52, 0x32}, {0x6B, 0x05, 0x6E, 0x18, 0x75, 0x9F, 0x5C, 0xCA}, {0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62}, {0x48, 0x0D, 0x39, 0x00, 0x6E, 0xE7, 0x62, 0xF2}, {0x43, 0x75, 0x40, 0xC8, 0x69, 0x8F, 0x3C, 0xFA}, {0x07, 0x2D, 0x43, 0xA0, 0x77, 0x07, 0x52, 0x92}, {0x02, 0xFE, 0x55, 0x77, 0x81, 0x17, 0xF1, 0x2A}, {0x1D, 0x9D, 0x5C, 0x50, 0x18, 0xF7, 0x28, 0xC2}, {0x30, 0x55, 0x32, 0x28, 0x6D, 0x6F, 0x29, 0x5A}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} }; static unsigned char cipher_data[NUM_TESTS][8] = { {0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78}, {0x51, 0x86, 0x6F, 0xD5, 0xB8, 0x5E, 0xCB, 0x8A}, {0x7D, 0x85, 0x6F, 0x9A, 0x61, 0x30, 0x63, 0xF2}, {0x24, 0x66, 0xDD, 0x87, 0x8B, 0x96, 0x3C, 0x9D}, {0x61, 0xF9, 0xC3, 0x80, 0x22, 0x81, 0xB0, 0x96}, {0x7D, 0x0C, 0xC6, 0x30, 0xAF, 0xDA, 0x1E, 0xC7}, {0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78}, {0x0A, 0xCE, 0xAB, 0x0F, 0xC6, 0xA0, 0xA2, 0x8D}, {0x59, 0xC6, 0x82, 0x45, 0xEB, 0x05, 0x28, 0x2B}, {0xB1, 0xB8, 0xCC, 0x0B, 0x25, 0x0F, 0x09, 0xA0}, {0x17, 0x30, 0xE5, 0x77, 0x8B, 0xEA, 0x1D, 0xA4}, {0xA2, 0x5E, 0x78, 0x56, 0xCF, 0x26, 0x51, 0xEB}, {0x35, 0x38, 0x82, 0xB1, 0x09, 0xCE, 0x8F, 0x1A}, {0x48, 0xF4, 0xD0, 0x88, 0x4C, 0x37, 0x99, 0x18}, {0x43, 0x21, 0x93, 0xB7, 0x89, 0x51, 0xFC, 0x98}, {0x13, 0xF0, 0x41, 0x54, 0xD6, 0x9D, 0x1A, 0xE5}, {0x2E, 0xED, 0xDA, 0x93, 0xFF, 0xD3, 0x9C, 0x79}, {0xD8, 0x87, 0xE0, 0x39, 0x3C, 0x2D, 0xA6, 0xE3}, {0x5F, 0x99, 0xD0, 0x4F, 0x5B, 0x16, 0x39, 0x69}, {0x4A, 0x05, 0x7A, 0x3B, 0x24, 0xD3, 0x97, 0x7B}, {0x45, 0x20, 0x31, 0xC1, 0xE4, 0xFA, 0xDA, 0x8E}, {0x75, 0x55, 0xAE, 0x39, 0xF5, 0x9B, 0x87, 0xBD}, {0x53, 0xC5, 0x5F, 0x9C, 0xB4, 0x9F, 0xC0, 0x19}, {0x7A, 0x8E, 0x7B, 0xFA, 0x93, 0x7E, 0x89, 0xA3}, {0xCF, 0x9C, 0x5D, 0x7A, 0x49, 0x86, 0xAD, 0xB5}, {0xD1, 0xAB, 0xB2, 0x90, 0x65, 0x8B, 0xC7, 0x78}, {0x55, 0xCB, 0x37, 0x74, 0xD1, 0x3E, 0xF2, 0x01}, {0xFA, 0x34, 0xEC, 0x48, 0x47, 0xB2, 0x68, 0xB2}, {0xA7, 0x90, 0x79, 0x51, 0x08, 0xEA, 0x3C, 0xAE}, {0xC3, 0x9E, 0x07, 0x2D, 0x9F, 0xAC, 0x63, 0x1D}, {0x01, 0x49, 0x33, 0xE0, 0xCD, 0xAF, 0xF6, 0xE4}, {0xF2, 0x1E, 0x9A, 0x77, 0xB7, 0x1C, 0x49, 0xBC}, {0x24, 0x59, 0x46, 0x88, 0x57, 0x54, 0x36, 0x9A}, {0x6B, 0x5C, 0x5A, 0x9C, 0x5D, 0x9E, 0x0A, 0x5A}, }; static unsigned char cbc_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }; static unsigned char cbc_iv[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; static char cbc_data[40] = "7654321 Now is the time for "; static unsigned char cbc_ok[32] = { 0x6B, 0x77, 0xB4, 0xD6, 0x30, 0x06, 0xDE, 0xE6, 0x05, 0xB1, 0x56, 0xE2, 0x74, 0x03, 0x97, 0x93, 0x58, 0xDE, 0xB9, 0xE7, 0x15, 0x46, 0x16, 0xD9, 0x59, 0xF1, 0x65, 0x2B, 0xD5, 0xFF, 0x92, 0xCC }; static unsigned char cfb64_ok[] = { 0xE7, 0x32, 0x14, 0xA2, 0x82, 0x21, 0x39, 0xCA, 0xF2, 0x6E, 0xCF, 0x6D, 0x2E, 0xB9, 0xE7, 0x6E, 0x3D, 0xA3, 0xDE, 0x04, 0xD1, 0x51, 0x72, 0x00, 0x51, 0x9D, 0x57, 0xA6, 0xC3 }; static unsigned char ofb64_ok[] = { 0xE7, 0x32, 0x14, 0xA2, 0x82, 0x21, 0x39, 0xCA, 0x62, 0xB3, 0x43, 0xCC, 0x5B, 0x65, 0x58, 0x73, 0x10, 0xDD, 0x90, 0x8D, 0x0C, 0x24, 0x1B, 0x22, 0x63, 0xC2, 0xCF, 0x80, 0xDA }; # define KEY_TEST_NUM 25 static unsigned char key_test[KEY_TEST_NUM] = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; static unsigned char key_data[8] = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }; static unsigned char key_out[KEY_TEST_NUM][8] = { {0xF9, 0xAD, 0x59, 0x7C, 0x49, 0xDB, 0x00, 0x5E}, {0xE9, 0x1D, 0x21, 0xC1, 0xD9, 0x61, 0xA6, 0xD6}, {0xE9, 0xC2, 0xB7, 0x0A, 0x1B, 0xC6, 0x5C, 0xF3}, {0xBE, 0x1E, 0x63, 0x94, 0x08, 0x64, 0x0F, 0x05}, {0xB3, 0x9E, 0x44, 0x48, 0x1B, 0xDB, 0x1E, 0x6E}, {0x94, 0x57, 0xAA, 0x83, 0xB1, 0x92, 0x8C, 0x0D}, {0x8B, 0xB7, 0x70, 0x32, 0xF9, 0x60, 0x62, 0x9D}, {0xE8, 0x7A, 0x24, 0x4E, 0x2C, 0xC8, 0x5E, 0x82}, {0x15, 0x75, 0x0E, 0x7A, 0x4F, 0x4E, 0xC5, 0x77}, {0x12, 0x2B, 0xA7, 0x0B, 0x3A, 0xB6, 0x4A, 0xE0}, {0x3A, 0x83, 0x3C, 0x9A, 0xFF, 0xC5, 0x37, 0xF6}, {0x94, 0x09, 0xDA, 0x87, 0xA9, 0x0F, 0x6B, 0xF2}, {0x88, 0x4F, 0x80, 0x62, 0x50, 0x60, 0xB8, 0xB4}, {0x1F, 0x85, 0x03, 0x1C, 0x19, 0xE1, 0x19, 0x68}, {0x79, 0xD9, 0x37, 0x3A, 0x71, 0x4C, 0xA3, 0x4F}, {0x93, 0x14, 0x28, 0x87, 0xEE, 0x3B, 0xE1, 0x5C}, {0x03, 0x42, 0x9E, 0x83, 0x8C, 0xE2, 0xD1, 0x4B}, {0xA4, 0x29, 0x9E, 0x27, 0x46, 0x9F, 0xF6, 0x7B}, {0xAF, 0xD5, 0xAE, 0xD1, 0xC1, 0xBC, 0x96, 0xA8}, {0x10, 0x85, 0x1C, 0x0E, 0x38, 0x58, 0xDA, 0x9F}, {0xE6, 0xF5, 0x1E, 0xD7, 0x9B, 0x9D, 0xB2, 0x1F}, {0x64, 0xA6, 0xE1, 0x4A, 0xFD, 0x36, 0xB4, 0x6F}, {0x80, 0xC7, 0xD7, 0xD4, 0x5A, 0x54, 0x79, 0xAD}, {0x05, 0x04, 0x4B, 0x62, 0xFA, 0x52, 0xD0, 0x80}, }; static int print_test_data(void) { unsigned int i, j; printf("ecb test data\n"); printf("key bytes\t\tclear bytes\t\tcipher bytes\n"); for (i = 0; i < NUM_TESTS; i++) { for (j = 0; j < 8; j++) printf("%02X", ecb_data[i][j]); printf("\t"); for (j = 0; j < 8; j++) printf("%02X", plain_data[i][j]); printf("\t"); for (j = 0; j < 8; j++) printf("%02X", cipher_data[i][j]); printf("\n"); } printf("set_key test data\n"); printf("data[8]= "); for (j = 0; j < 8; j++) printf("%02X", key_data[j]); printf("\n"); for (i = 0; i < KEY_TEST_NUM - 1; i++) { printf("c="); for (j = 0; j < 8; j++) printf("%02X", key_out[i][j]); printf(" k[%2u]=", i + 1); for (j = 0; j < i + 1; j++) printf("%02X", key_test[j]); printf("\n"); } printf("\nchaining mode test data\n"); printf("key[16] = "); for (j = 0; j < 16; j++) printf("%02X", cbc_key[j]); printf("\niv[8] = "); for (j = 0; j < 8; j++) printf("%02X", cbc_iv[j]); printf("\ndata[%d] = '%s'", (int)strlen(cbc_data) + 1, cbc_data); printf("\ndata[%d] = ", (int)strlen(cbc_data) + 1); for (j = 0; j < strlen(cbc_data) + 1; j++) printf("%02X", cbc_data[j]); printf("\n"); printf("cbc cipher text\n"); printf("cipher[%d]= ", 32); for (j = 0; j < 32; j++) printf("%02X", cbc_ok[j]); printf("\n"); printf("cfb64 cipher text\n"); printf("cipher[%d]= ", (int)strlen(cbc_data) + 1); for (j = 0; j < strlen(cbc_data) + 1; j++) printf("%02X", cfb64_ok[j]); printf("\n"); printf("ofb64 cipher text\n"); printf("cipher[%d]= ", (int)strlen(cbc_data) + 1); for (j = 0; j < strlen(cbc_data) + 1; j++) printf("%02X", ofb64_ok[j]); printf("\n"); return 0; } static int test_bf_ecb_raw(int n) { int ret = 1; BF_KEY key; BF_LONG data[2]; BF_set_key(&key, strlen(bf_key[n]), (unsigned char *)bf_key[n]); data[0] = bf_plain[n][0]; data[1] = bf_plain[n][1]; BF_encrypt(data, &key); if (!TEST_mem_eq(&(bf_cipher[n][0]), BF_BLOCK, &(data[0]), BF_BLOCK)) ret = 0; BF_decrypt(&(data[0]), &key); if (!TEST_mem_eq(&(bf_plain[n][0]), BF_BLOCK, &(data[0]), BF_BLOCK)) ret = 0; return ret; } static int test_bf_ecb(int n) { int ret = 1; BF_KEY key; unsigned char out[8]; BF_set_key(&key, 8, ecb_data[n]); BF_ecb_encrypt(&(plain_data[n][0]), out, &key, BF_ENCRYPT); if (!TEST_mem_eq(&(cipher_data[n][0]), BF_BLOCK, out, BF_BLOCK)) ret = 0; BF_ecb_encrypt(out, out, &key, BF_DECRYPT); if (!TEST_mem_eq(&(plain_data[n][0]), BF_BLOCK, out, BF_BLOCK)) ret = 0; return ret; } static int test_bf_set_key(int n) { int ret = 1; BF_KEY key; unsigned char out[8]; BF_set_key(&key, n+1, key_test); BF_ecb_encrypt(key_data, out, &key, BF_ENCRYPT); /* mips-sgi-irix6.5-gcc vv -mabi=64 bug workaround */ if (!TEST_mem_eq(out, 8, &(key_out[n][0]), 8)) ret = 0; return ret; } static int test_bf_cbc(void) { unsigned char cbc_in[40], cbc_out[40], iv[8]; int ret = 1; BF_KEY key; BF_LONG len; len = strlen(cbc_data) + 1; BF_set_key(&key, 16, cbc_key); memset(cbc_in, 0, sizeof(cbc_in)); memset(cbc_out, 0, sizeof(cbc_out)); memcpy(iv, cbc_iv, sizeof(iv)); BF_cbc_encrypt((unsigned char *)cbc_data, cbc_out, len, &key, iv, BF_ENCRYPT); if (!TEST_mem_eq(cbc_out, 32, cbc_ok, 32)) ret = 0; memcpy(iv, cbc_iv, 8); BF_cbc_encrypt(cbc_out, cbc_in, len, &key, iv, BF_DECRYPT); if (!TEST_mem_eq(cbc_in, len, cbc_data, strlen(cbc_data) + 1)) ret = 0; return ret; } static int test_bf_cfb64(void) { unsigned char cbc_in[40], cbc_out[40], iv[8]; int n, ret = 1; BF_KEY key; BF_LONG len; len = strlen(cbc_data) + 1; BF_set_key(&key, 16, cbc_key); memset(cbc_in, 0, 40); memset(cbc_out, 0, 40); memcpy(iv, cbc_iv, 8); n = 0; BF_cfb64_encrypt((unsigned char *)cbc_data, cbc_out, (long)13, &key, iv, &n, BF_ENCRYPT); BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]), &(cbc_out[13]), len - 13, &key, iv, &n, BF_ENCRYPT); if (!TEST_mem_eq(cbc_out, (int)len, cfb64_ok, (int)len)) ret = 0; n = 0; memcpy(iv, cbc_iv, 8); BF_cfb64_encrypt(cbc_out, cbc_in, 17, &key, iv, &n, BF_DECRYPT); BF_cfb64_encrypt(&(cbc_out[17]), &(cbc_in[17]), len - 17, &key, iv, &n, BF_DECRYPT); if (!TEST_mem_eq(cbc_in, (int)len, cbc_data, (int)len)) ret = 0; return ret; } static int test_bf_ofb64(void) { unsigned char cbc_in[40], cbc_out[40], iv[8]; int n, ret = 1; BF_KEY key; BF_LONG len; len = strlen(cbc_data) + 1; BF_set_key(&key, 16, cbc_key); memset(cbc_in, 0, 40); memset(cbc_out, 0, 40); memcpy(iv, cbc_iv, 8); n = 0; BF_ofb64_encrypt((unsigned char *)cbc_data, cbc_out, (long)13, &key, iv, &n); BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]), &(cbc_out[13]), len - 13, &key, iv, &n); if (!TEST_mem_eq(cbc_out, (int)len, ofb64_ok, (int)len)) ret = 0; n = 0; memcpy(iv, cbc_iv, 8); BF_ofb64_encrypt(cbc_out, cbc_in, 17, &key, iv, &n); BF_ofb64_encrypt(&(cbc_out[17]), &(cbc_in[17]), len - 17, &key, iv, &n); if (!TEST_mem_eq(cbc_in, (int)len, cbc_data, (int)len)) ret = 0; return ret; } #endif typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_PRINT, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "print", OPT_PRINT, '-', "Output test tables instead of running tests"}, { NULL } }; return test_options; } int setup_tests(void) { #ifndef OPENSSL_NO_BF OPTION_CHOICE o; # ifdef CHARSET_EBCDIC int n; ebcdic2ascii(cbc_data, cbc_data, strlen(cbc_data)); for (n = 0; n < 2; n++) { ebcdic2ascii(bf_key[n], bf_key[n], strlen(bf_key[n])); } # endif while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_PRINT: print_test_data(); return 1; case OPT_TEST_CASES: break; default: return 0; } } ADD_ALL_TESTS(test_bf_ecb_raw, 2); ADD_ALL_TESTS(test_bf_ecb, NUM_TESTS); ADD_ALL_TESTS(test_bf_set_key, KEY_TEST_NUM-1); ADD_TEST(test_bf_cbc); ADD_TEST(test_bf_cfb64); ADD_TEST(test_bf_ofb64); #endif return 1; }
./openssl/test/aesgcmtest.c
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/evp.h> #include "testutil.h" static const unsigned char gcm_key[] = { 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f }; static const unsigned char gcm_iv[] = { 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84 }; static const unsigned char gcm_pt[] = { 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea, 0xcc, 0x2b, 0xf2, 0xa5 }; static const unsigned char gcm_aad[] = { 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43, 0x7f, 0xec, 0x78, 0xde }; static const unsigned char gcm_ct[] = { 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e, 0xb9, 0xf2, 0x17, 0x36 }; static const unsigned char gcm_tag[] = { 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62, 0x98, 0xf7, 0x7e, 0x0c }; static int do_encrypt(unsigned char *iv_gen, unsigned char *ct, int *ct_len, unsigned char *tag, int *tag_len) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; int outlen; unsigned char outbuf[64]; *tag_len = 16; ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new()) && TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) > 0) && TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key, iv_gen != NULL ? NULL : gcm_iv) > 0) && TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)) > 0) && TEST_true(EVP_EncryptUpdate(ctx, ct, ct_len, gcm_pt, sizeof(gcm_pt)) > 0) && TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &outlen) > 0) && TEST_int_eq(EVP_CIPHER_CTX_get_tag_length(ctx), 16) && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, tag) > 0) && TEST_true(iv_gen == NULL || EVP_CIPHER_CTX_get_original_iv(ctx, iv_gen, 12)); EVP_CIPHER_CTX_free(ctx); return ret; } static int do_decrypt(const unsigned char *iv, const unsigned char *ct, int ct_len, const unsigned char *tag, int tag_len) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; int outlen, ptlen; unsigned char pt[32]; unsigned char outbuf[32]; ret = TEST_ptr(ctx = EVP_CIPHER_CTX_new()) && TEST_true(EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) > 0) && TEST_true(EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, iv) > 0) && TEST_int_eq(EVP_CIPHER_CTX_get_tag_length(ctx), 16) && TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)) > 0) && TEST_true(EVP_DecryptUpdate(ctx, pt, &ptlen, ct, ct_len) > 0) && TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, (void *)tag) > 0) && TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen) > 0) && TEST_mem_eq(gcm_pt, sizeof(gcm_pt), pt, ptlen); EVP_CIPHER_CTX_free(ctx); return ret; } static int kat_test(void) { unsigned char tag[32]; unsigned char ct[32]; int ctlen = 0, taglen = 0; return do_encrypt(NULL, ct, &ctlen, tag, &taglen) && TEST_mem_eq(gcm_ct, sizeof(gcm_ct), ct, ctlen) && TEST_mem_eq(gcm_tag, sizeof(gcm_tag), tag, taglen) && do_decrypt(gcm_iv, ct, ctlen, tag, taglen); } static int badkeylen_test(void) { int ret; EVP_CIPHER_CTX *ctx = NULL; const EVP_CIPHER *cipher; ret = TEST_ptr(cipher = EVP_aes_192_gcm()) && TEST_ptr(ctx = EVP_CIPHER_CTX_new()) && TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL)) && TEST_int_le(EVP_CIPHER_CTX_set_key_length(ctx, 2), 0); EVP_CIPHER_CTX_free(ctx); return ret; } static int ivgen_test(void) { unsigned char iv_gen[16]; unsigned char tag[32]; unsigned char ct[32]; int ctlen = 0, taglen = 0; return do_encrypt(iv_gen, ct, &ctlen, tag, &taglen) && do_decrypt(iv_gen, ct, ctlen, tag, taglen); } int setup_tests(void) { ADD_TEST(kat_test); ADD_TEST(badkeylen_test); ADD_TEST(ivgen_test); return 1; }
./openssl/test/exptest.c
/* * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "internal/nelem.h" #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/rand.h> #include <openssl/err.h> #include "testutil.h" #define NUM_BITS (BN_BITS2 * 4) #define BN_print_var(v) test_output_bignum(#v, v) /* * Test that r == 0 in test_exp_mod_zero(). Returns one on success, * returns zero and prints debug output otherwise. */ static int a_is_zero_mod_one(const char *method, const BIGNUM *r, const BIGNUM *a) { if (!BN_is_zero(r)) { TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method); BN_print_var(a); BN_print_var(r); return 0; } return 1; } /* * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success. */ static int test_mod_exp_zero(void) { BIGNUM *a = NULL, *p = NULL, *m = NULL; BIGNUM *r = NULL; BN_ULONG one_word = 1; BN_CTX *ctx = BN_CTX_new(); int ret = 0, failed = 0; BN_MONT_CTX *mont = NULL; if (!TEST_ptr(m = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(r = BN_new())) goto err; BN_one(m); BN_one(a); BN_zero(p); if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_true(BN_mod_exp(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))) failed = 1; if (!TEST_ptr(mont = BN_MONT_CTX_new())) goto err; ERR_set_mark(); /* mont is not set but passed in */ if (!TEST_false(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont))) goto err; if (!TEST_false(BN_mod_exp_mont(r, p, a, m, ctx, mont))) goto err; ERR_pop_to_mark(); if (!TEST_true(BN_MONT_CTX_set(mont, m, ctx))) goto err; /* we compute 0 ** a mod 1 here, to execute code that uses mont */ if (!TEST_true(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont(r, p, a, m, ctx, mont))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a))) failed = 1; /* * A different codepath exists for single word multiplication * in non-constant-time only. */ if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))) goto err; if (!TEST_BN_eq_zero(r)) { TEST_error("BN_mod_exp_mont_word failed: " "1 ** 0 mod 1 = r (should be 0)"); BN_print_var(r); goto err; } ret = !failed; err: BN_free(r); BN_free(a); BN_free(p); BN_free(m); BN_MONT_CTX_free(mont); BN_CTX_free(ctx); return ret; } static int test_mod_exp(int round) { BN_CTX *ctx; unsigned char c; int ret = 0; BIGNUM *r_mont = NULL; BIGNUM *r_mont_const = NULL; BIGNUM *r_recp = NULL; BIGNUM *r_simple = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *m = NULL; if (!TEST_ptr(ctx = BN_CTX_new())) goto err; if (!TEST_ptr(r_mont = BN_new()) || !TEST_ptr(r_mont_const = BN_new()) || !TEST_ptr(r_recp = BN_new()) || !TEST_ptr(r_simple = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_ptr(m = BN_new())) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))) goto err; if (!TEST_true(BN_mod(a, a, m, ctx)) || !TEST_true(BN_mod(b, b, m, ctx)) || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL)) || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx)) || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL))) goto err; if (!TEST_BN_eq(r_simple, r_mont) || !TEST_BN_eq(r_simple, r_recp) || !TEST_BN_eq(r_simple, r_mont_const)) { if (BN_cmp(r_simple, r_mont) != 0) TEST_info("simple and mont results differ"); if (BN_cmp(r_simple, r_mont_const) != 0) TEST_info("simple and mont const time results differ"); if (BN_cmp(r_simple, r_recp) != 0) TEST_info("simple and recp results differ"); BN_print_var(a); BN_print_var(b); BN_print_var(m); BN_print_var(r_simple); BN_print_var(r_recp); BN_print_var(r_mont); BN_print_var(r_mont_const); goto err; } ret = 1; err: BN_free(r_mont); BN_free(r_mont_const); BN_free(r_recp); BN_free(r_simple); BN_free(a); BN_free(b); BN_free(m); BN_CTX_free(ctx); return ret; } static int test_mod_exp_x2(int idx) { BN_CTX *ctx; int ret = 0; BIGNUM *r_mont_const_x2_1 = NULL; BIGNUM *r_mont_const_x2_2 = NULL; BIGNUM *r_simple1 = NULL; BIGNUM *r_simple2 = NULL; BIGNUM *a1 = NULL; BIGNUM *b1 = NULL; BIGNUM *m1 = NULL; BIGNUM *a2 = NULL; BIGNUM *b2 = NULL; BIGNUM *m2 = NULL; int factor_size = 0; if (idx <= 100) factor_size = 1024; else if (idx <= 200) factor_size = 1536; else if (idx <= 300) factor_size = 2048; if (!TEST_ptr(ctx = BN_CTX_new())) goto err; if (!TEST_ptr(r_mont_const_x2_1 = BN_new()) || !TEST_ptr(r_mont_const_x2_2 = BN_new()) || !TEST_ptr(r_simple1 = BN_new()) || !TEST_ptr(r_simple2 = BN_new()) || !TEST_ptr(a1 = BN_new()) || !TEST_ptr(b1 = BN_new()) || !TEST_ptr(m1 = BN_new()) || !TEST_ptr(a2 = BN_new()) || !TEST_ptr(b2 = BN_new()) || !TEST_ptr(m2 = BN_new())) goto err; BN_rand(a1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(b1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(m1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD); BN_rand(a2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(b2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(m2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD); if (!TEST_true(BN_mod(a1, a1, m1, ctx)) || !TEST_true(BN_mod(b1, b1, m1, ctx)) || !TEST_true(BN_mod(a2, a2, m2, ctx)) || !TEST_true(BN_mod(b2, b2, m2, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple1, a1, b1, m1, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple2, a2, b2, m2, ctx)) || !TEST_true(BN_mod_exp_mont_consttime_x2(r_mont_const_x2_1, a1, b1, m1, NULL, r_mont_const_x2_2, a2, b2, m2, NULL, ctx))) goto err; if (!TEST_BN_eq(r_simple1, r_mont_const_x2_1) || !TEST_BN_eq(r_simple2, r_mont_const_x2_2)) { if (BN_cmp(r_simple1, r_mont_const_x2_1) != 0) TEST_info("simple and mont const time x2 (#1) results differ"); if (BN_cmp(r_simple2, r_mont_const_x2_2) != 0) TEST_info("simple and mont const time x2 (#2) results differ"); BN_print_var(a1); BN_print_var(b1); BN_print_var(m1); BN_print_var(a2); BN_print_var(b2); BN_print_var(m2); BN_print_var(r_simple1); BN_print_var(r_simple2); BN_print_var(r_mont_const_x2_1); BN_print_var(r_mont_const_x2_2); goto err; } ret = 1; err: BN_free(r_mont_const_x2_1); BN_free(r_mont_const_x2_2); BN_free(r_simple1); BN_free(r_simple2); BN_free(a1); BN_free(b1); BN_free(m1); BN_free(a2); BN_free(b2); BN_free(m2); BN_CTX_free(ctx); return ret; } int setup_tests(void) { ADD_TEST(test_mod_exp_zero); ADD_ALL_TESTS(test_mod_exp, 200); ADD_ALL_TESTS(test_mod_exp_x2, 300); return 1; }
./openssl/test/packettest.c
/* * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/packet_quic.h" #include "testutil.h" #define BUF_LEN 255 static unsigned char smbuf[BUF_LEN + 1]; static int test_PACKET_remaining(void) { PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1)) || !TEST_size_t_eq(PACKET_remaining(&pkt), 1) || !TEST_true(PACKET_forward(&pkt, 1)) || !TEST_size_t_eq(PACKET_remaining(&pkt), 0)) return 0; return 1; } static int test_PACKET_end(void) { PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1)) || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN) || !TEST_true(PACKET_forward(&pkt, 1)) || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN)) return 0; return 1; } static int test_PACKET_get_1(void) { unsigned int i = 0; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_1(&pkt, &i)) || !TEST_uint_eq(i, 0x02) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 2)) || !TEST_true(PACKET_get_1(&pkt, &i)) || !TEST_uint_eq(i, 0xfe) || !TEST_false(PACKET_get_1(&pkt, &i))) return 0; return 1; } static int test_PACKET_get_4(void) { unsigned long i = 0; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_4(&pkt, &i)) || !TEST_ulong_eq(i, 0x08060402UL) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8)) || !TEST_true(PACKET_get_4(&pkt, &i)) || !TEST_ulong_eq(i, 0xfefcfaf8UL) || !TEST_false(PACKET_get_4(&pkt, &i))) return 0; return 1; } static int test_PACKET_get_net_2(void) { unsigned int i = 0; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_net_2(&pkt, &i)) || !TEST_uint_eq(i, 0x0204) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 4)) || !TEST_true(PACKET_get_net_2(&pkt, &i)) || !TEST_uint_eq(i, 0xfcfe) || !TEST_false(PACKET_get_net_2(&pkt, &i))) return 0; return 1; } static int test_PACKET_get_net_3(void) { unsigned long i = 0; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_net_3(&pkt, &i)) || !TEST_ulong_eq(i, 0x020406UL) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 6)) || !TEST_true(PACKET_get_net_3(&pkt, &i)) || !TEST_ulong_eq(i, 0xfafcfeUL) || !TEST_false(PACKET_get_net_3(&pkt, &i))) return 0; return 1; } static int test_PACKET_get_net_4(void) { unsigned long i = 0; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_net_4(&pkt, &i)) || !TEST_ulong_eq(i, 0x02040608UL) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8)) || !TEST_true(PACKET_get_net_4(&pkt, &i)) || !TEST_ulong_eq(i, 0xf8fafcfeUL) || !TEST_false(PACKET_get_net_4(&pkt, &i))) return 0; return 1; } static int test_PACKET_get_sub_packet(void) { PACKET pkt, subpkt; unsigned long i = 0; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4)) || !TEST_true(PACKET_get_net_4(&subpkt, &i)) || !TEST_ulong_eq(i, 0x02040608UL) || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8)) || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4)) || !TEST_true(PACKET_get_net_4(&subpkt, &i)) || !TEST_ulong_eq(i, 0xf8fafcfeUL) || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0) || !TEST_false(PACKET_get_sub_packet(&pkt, &subpkt, 4))) return 0; return 1; } static int test_PACKET_get_bytes(void) { const unsigned char *bytes = NULL; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4)) || !TEST_uchar_eq(bytes[0], 2) || !TEST_uchar_eq(bytes[1], 4) || !TEST_uchar_eq(bytes[2], 6) || !TEST_uchar_eq(bytes[3], 8) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN -4) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8)) || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4)) || !TEST_uchar_eq(bytes[0], 0xf8) || !TEST_uchar_eq(bytes[1], 0xfa) || !TEST_uchar_eq(bytes[2], 0xfc) || !TEST_uchar_eq(bytes[3], 0xfe) || !TEST_false(PACKET_remaining(&pkt))) return 0; return 1; } static int test_PACKET_copy_bytes(void) { unsigned char bytes[4]; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4)) || !TEST_char_eq(bytes[0], 2) || !TEST_char_eq(bytes[1], 4) || !TEST_char_eq(bytes[2], 6) || !TEST_char_eq(bytes[3], 8) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN - 4) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8)) || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4)) || !TEST_uchar_eq(bytes[0], 0xf8) || !TEST_uchar_eq(bytes[1], 0xfa) || !TEST_uchar_eq(bytes[2], 0xfc) || !TEST_uchar_eq(bytes[3], 0xfe) || !TEST_false(PACKET_remaining(&pkt))) return 0; return 1; } static int test_PACKET_copy_all(void) { unsigned char tmp[BUF_LEN]; PACKET pkt; size_t len; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)) || !TEST_size_t_eq(len, BUF_LEN) || !TEST_mem_eq(smbuf, BUF_LEN, tmp, BUF_LEN) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_false(PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len))) return 0; return 1; } static int test_PACKET_memdup(void) { unsigned char *data = NULL; size_t len; PACKET pkt; int result = 0; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_memdup(&pkt, &data, &len)) || !TEST_size_t_eq(len, BUF_LEN) || !TEST_mem_eq(data, len, PACKET_data(&pkt), len) || !TEST_true(PACKET_forward(&pkt, 10)) || !TEST_true(PACKET_memdup(&pkt, &data, &len)) || !TEST_size_t_eq(len, BUF_LEN - 10) || !TEST_mem_eq(data, len, PACKET_data(&pkt), len)) goto end; result = 1; end: OPENSSL_free(data); return result; } static int test_PACKET_strndup(void) { char buf1[10], buf2[10]; char *data = NULL; PACKET pkt; int result = 0; memset(buf1, 'x', 10); memset(buf2, 'y', 10); buf2[5] = '\0'; if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10)) || !TEST_true(PACKET_strndup(&pkt, &data)) || !TEST_size_t_eq(strlen(data), 10) || !TEST_strn_eq(data, buf1, 10) || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)) || !TEST_true(PACKET_strndup(&pkt, &data)) || !TEST_size_t_eq(strlen(data), 5) || !TEST_str_eq(data, buf2)) goto end; result = 1; end: OPENSSL_free(data); return result; } static int test_PACKET_contains_zero_byte(void) { char buf1[10], buf2[10]; PACKET pkt; memset(buf1, 'x', 10); memset(buf2, 'y', 10); buf2[5] = '\0'; if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10)) || !TEST_false(PACKET_contains_zero_byte(&pkt)) || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)) || !TEST_true(PACKET_contains_zero_byte(&pkt))) return 0; return 1; } static int test_PACKET_forward(void) { const unsigned char *byte = NULL; PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_forward(&pkt, 1)) || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1)) || !TEST_uchar_eq(byte[0], 4) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 3)) || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1)) || !TEST_uchar_eq(byte[0], 0xfe)) return 0; return 1; } static int test_PACKET_buf_init(void) { unsigned char buf1[BUF_LEN] = { 0 }; PACKET pkt; /* Also tests PACKET_remaining() */ if (!TEST_true(PACKET_buf_init(&pkt, buf1, 4)) || !TEST_size_t_eq(PACKET_remaining(&pkt), 4) || !TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN)) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_false(PACKET_buf_init(&pkt, buf1, -1))) return 0; return 1; } static int test_PACKET_null_init(void) { PACKET pkt; PACKET_null_init(&pkt); if (!TEST_size_t_eq(PACKET_remaining(&pkt), 0) || !TEST_false(PACKET_forward(&pkt, 1))) return 0; return 1; } static int test_PACKET_equal(void) { PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, 4)) || !TEST_true(PACKET_equal(&pkt, smbuf, 4)) || !TEST_false(PACKET_equal(&pkt, smbuf + 1, 4)) || !TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN)) || !TEST_true(PACKET_equal(&pkt, smbuf, BUF_LEN)) || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN - 1)) || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN + 1)) || !TEST_false(PACKET_equal(&pkt, smbuf, 0))) return 0; return 1; } static int test_PACKET_get_length_prefixed_1(void) { unsigned char buf1[BUF_LEN]; const size_t len = 16; unsigned int i; PACKET pkt, short_pkt, subpkt; memset(&subpkt, 0, sizeof(subpkt)); buf1[0] = (unsigned char)len; for (i = 1; i < BUF_LEN; i++) buf1[i] = (i * 2) & 0xff; if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN)) || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&subpkt), len) || !TEST_true(PACKET_get_net_2(&subpkt, &i)) || !TEST_uint_eq(i, 0x0204) || !TEST_false(PACKET_get_length_prefixed_1(&short_pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len)) return 0; return 1; } static int test_PACKET_get_length_prefixed_2(void) { unsigned char buf1[1024]; const size_t len = 516; /* 0x0204 */ unsigned int i; PACKET pkt, short_pkt, subpkt; memset(&subpkt, 0, sizeof(subpkt)); for (i = 1; i <= 1024; i++) buf1[i - 1] = (i * 2) & 0xff; if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024)) || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&subpkt), len) || !TEST_true(PACKET_get_net_2(&subpkt, &i)) || !TEST_uint_eq(i, 0x0608) || !TEST_false(PACKET_get_length_prefixed_2(&short_pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len)) return 0; return 1; } static int test_PACKET_get_length_prefixed_3(void) { unsigned char buf1[1024]; const size_t len = 516; /* 0x000204 */ unsigned int i; PACKET pkt, short_pkt, subpkt; memset(&subpkt, 0, sizeof(subpkt)); for (i = 0; i < 1024; i++) buf1[i] = (i * 2) & 0xff; if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024)) || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len)) || !TEST_true(PACKET_get_length_prefixed_3(&pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&subpkt), len) || !TEST_true(PACKET_get_net_2(&subpkt, &i)) || !TEST_uint_eq(i, 0x0608) || !TEST_false(PACKET_get_length_prefixed_3(&short_pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len)) return 0; return 1; } static int test_PACKET_as_length_prefixed_1(void) { unsigned char buf1[BUF_LEN]; const size_t len = 16; unsigned int i; PACKET pkt, exact_pkt, subpkt; memset(&subpkt, 0, sizeof(subpkt)); buf1[0] = (unsigned char)len; for (i = 1; i < BUF_LEN; i++) buf1[i] = (i * 2) & 0xff; if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN)) || !TEST_true(PACKET_buf_init(&exact_pkt, buf1, len + 1)) || !TEST_false(PACKET_as_length_prefixed_1(&pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_true(PACKET_as_length_prefixed_1(&exact_pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0) || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)) return 0; return 1; } static int test_PACKET_as_length_prefixed_2(void) { unsigned char buf[1024]; const size_t len = 516; /* 0x0204 */ unsigned int i; PACKET pkt, exact_pkt, subpkt; memset(&subpkt, 0, sizeof(subpkt)); for (i = 1; i <= 1024; i++) buf[i-1] = (i * 2) & 0xff; if (!TEST_true(PACKET_buf_init(&pkt, buf, 1024)) || !TEST_true(PACKET_buf_init(&exact_pkt, buf, len + 2)) || !TEST_false(PACKET_as_length_prefixed_2(&pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&pkt), 1024) || !TEST_true(PACKET_as_length_prefixed_2(&exact_pkt, &subpkt)) || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0) || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)) return 0; return 1; } #ifndef OPENSSL_NO_QUIC static int test_PACKET_get_quic_vlint(void) { struct quic_test_case { unsigned char buf[16]; size_t expected_read_count; uint64_t value; }; static const struct quic_test_case cases[] = { { {0x00}, 1, 0 }, { {0x01}, 1, 1 }, { {0x3e}, 1, 62 }, { {0x3f}, 1, 63 }, { {0x40,0x00}, 2, 0 }, { {0x40,0x01}, 2, 1 }, { {0x40,0x02}, 2, 2 }, { {0x40,0xff}, 2, 255 }, { {0x41,0x00}, 2, 256 }, { {0x7f,0xfe}, 2, 16382 }, { {0x7f,0xff}, 2, 16383 }, { {0x80,0x00,0x00,0x00}, 4, 0 }, { {0x80,0x00,0x00,0x01}, 4, 1 }, { {0x80,0x00,0x01,0x02}, 4, 258 }, { {0x80,0x18,0x49,0x65}, 4, 1591653 }, { {0xbe,0x18,0x49,0x65}, 4, 1041779045 }, { {0xbf,0xff,0xff,0xff}, 4, 1073741823 }, { {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 8, 0 }, { {0xc0,0x00,0x00,0x00,0x00,0x00,0x01,0x02}, 8, 258 }, { {0xfd,0x1f,0x59,0x8d,0xc9,0xf8,0x71,0x8a}, 8, 4404337426105397642 }, }; PACKET pkt; size_t i; uint64_t v; for (i = 0; i < OSSL_NELEM(cases); ++i) { memset(&pkt, 0, sizeof(pkt)); v = 55; if (!TEST_true(PACKET_buf_init(&pkt, cases[i].buf, sizeof(cases[i].buf))) || !TEST_true(PACKET_get_quic_vlint(&pkt, &v)) || !TEST_uint64_t_eq(v, cases[i].value) || !TEST_size_t_eq(PACKET_remaining(&pkt), sizeof(cases[i].buf) - cases[i].expected_read_count) ) return 0; } return 1; } static int test_PACKET_get_quic_length_prefixed(void) { struct quic_test_case { unsigned char buf[16]; size_t enclen, len; int fail; }; static const struct quic_test_case cases[] = { /* success cases */ { {0x00}, 1, 0, 0 }, { {0x01}, 1, 1, 0 }, { {0x02}, 1, 2, 0 }, { {0x03}, 1, 3, 0 }, { {0x04}, 1, 4, 0 }, { {0x05}, 1, 5, 0 }, /* failure cases */ { {0x10}, 1, 0, 1 }, { {0x3f}, 1, 0, 1 }, }; size_t i; PACKET pkt, subpkt = {0}; for (i = 0; i < OSSL_NELEM(cases); ++i) { memset(&pkt, 0, sizeof(pkt)); if (!TEST_true(PACKET_buf_init(&pkt, cases[i].buf, cases[i].fail ? sizeof(cases[i].buf) : cases[i].enclen + cases[i].len))) return 0; if (!TEST_int_eq(PACKET_get_quic_length_prefixed(&pkt, &subpkt), !cases[i].fail)) return 0; if (cases[i].fail) { if (!TEST_ptr_eq(pkt.curr, cases[i].buf)) return 0; continue; } if (!TEST_ptr_eq(subpkt.curr, cases[i].buf + cases[i].enclen)) return 0; if (!TEST_size_t_eq(subpkt.remaining, cases[i].len)) return 0; } return 1; } #endif int setup_tests(void) { unsigned int i; for (i = 1; i <= BUF_LEN; i++) smbuf[i - 1] = (i * 2) & 0xff; ADD_TEST(test_PACKET_buf_init); ADD_TEST(test_PACKET_null_init); ADD_TEST(test_PACKET_remaining); ADD_TEST(test_PACKET_end); ADD_TEST(test_PACKET_equal); ADD_TEST(test_PACKET_get_1); ADD_TEST(test_PACKET_get_4); ADD_TEST(test_PACKET_get_net_2); ADD_TEST(test_PACKET_get_net_3); ADD_TEST(test_PACKET_get_net_4); ADD_TEST(test_PACKET_get_sub_packet); ADD_TEST(test_PACKET_get_bytes); ADD_TEST(test_PACKET_copy_bytes); ADD_TEST(test_PACKET_copy_all); ADD_TEST(test_PACKET_memdup); ADD_TEST(test_PACKET_strndup); ADD_TEST(test_PACKET_contains_zero_byte); ADD_TEST(test_PACKET_forward); ADD_TEST(test_PACKET_get_length_prefixed_1); ADD_TEST(test_PACKET_get_length_prefixed_2); ADD_TEST(test_PACKET_get_length_prefixed_3); ADD_TEST(test_PACKET_as_length_prefixed_1); ADD_TEST(test_PACKET_as_length_prefixed_2); #ifndef OPENSSL_NO_QUIC ADD_TEST(test_PACKET_get_quic_vlint); ADD_TEST(test_PACKET_get_quic_length_prefixed); #endif return 1; }
./openssl/test/hexstr_test.c
/* * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * https://www.openssl.org/source/license.html * or in the file LICENSE in the source distribution. */ /* * This program tests the use of OSSL_PARAM, currently in raw form. */ #include "internal/nelem.h" #include "internal/cryptlib.h" #include "testutil.h" struct testdata { const char *in; const unsigned char *expected; size_t expected_len; const char sep; }; static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 }; static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 }; static struct testdata tbl_testdata[] = { { "AB:CD:EF:F1", test_1, sizeof(test_1), ':', }, { "AB:CD:EF:76:00", test_2, sizeof(test_2), ':', }, { "AB_CD_EF_F1", test_1, sizeof(test_1), '_', }, { "AB_CD_EF_76_00", test_2, sizeof(test_2), '_', }, { "ABCDEFF1", test_1, sizeof(test_1), '\0', }, { "ABCDEF7600", test_2, sizeof(test_2), '\0', }, }; static int test_hexstr_sep_to_from(int test_index) { int ret = 0; long len = 0; unsigned char *buf = NULL; char *out = NULL; struct testdata *test = &tbl_testdata[test_index]; if (!TEST_ptr(buf = ossl_hexstr2buf_sep(test->in, &len, test->sep)) || !TEST_mem_eq(buf, len, test->expected, test->expected_len) || !TEST_ptr(out = ossl_buf2hexstr_sep(buf, len, test->sep)) || !TEST_str_eq(out, test->in)) goto err; ret = 1; err: OPENSSL_free(buf); OPENSSL_free(out); return ret; } static int test_hexstr_to_from(int test_index) { int ret = 0; long len = 0; unsigned char *buf = NULL; char *out = NULL; struct testdata *test = &tbl_testdata[test_index]; if (test->sep != '_') { if (!TEST_ptr(buf = OPENSSL_hexstr2buf(test->in, &len)) || !TEST_mem_eq(buf, len, test->expected, test->expected_len) || !TEST_ptr(out = OPENSSL_buf2hexstr(buf, len))) goto err; if (test->sep == ':') { if (!TEST_str_eq(out, test->in)) goto err; } else if (!TEST_str_ne(out, test->in)) { goto err; } } else { if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len))) goto err; } ret = 1; err: OPENSSL_free(buf); OPENSSL_free(out); return ret; } static int test_hexstr_ex_to_from(int test_index) { size_t len = 0; char out[64]; unsigned char buf[64]; struct testdata *test = &tbl_testdata[test_index]; return TEST_true(OPENSSL_hexstr2buf_ex(buf, sizeof(buf), &len, test->in, ':')) && TEST_mem_eq(buf, len, test->expected, test->expected_len) && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len, ':')) && TEST_str_eq(out, test->in); } int setup_tests(void) { ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata)); ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata)); ADD_ALL_TESTS(test_hexstr_ex_to_from, 2); return 1; }
./openssl/test/ciphername_test.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * Copyright 2017 BaishanCloud. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * https://www.openssl.org/source/license.html * or in the file LICENSE in the source distribution. */ #include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/e_os2.h> #include <openssl/ssl.h> #include <openssl/ssl3.h> #include <openssl/tls1.h> #include "internal/nelem.h" #include "testutil.h" typedef struct cipher_id_name { int id; const char *name; } CIPHER_ID_NAME; /* Cipher suites, copied from t1_trce.c */ static CIPHER_ID_NAME cipher_names[] = { {0x0000, "TLS_NULL_WITH_NULL_NULL"}, {0x0001, "TLS_RSA_WITH_NULL_MD5"}, {0x0002, "TLS_RSA_WITH_NULL_SHA"}, {0x0003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5"}, {0x0004, "TLS_RSA_WITH_RC4_128_MD5"}, {0x0005, "TLS_RSA_WITH_RC4_128_SHA"}, {0x0006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5"}, {0x0007, "TLS_RSA_WITH_IDEA_CBC_SHA"}, {0x0008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA"}, {0x0009, "TLS_RSA_WITH_DES_CBC_SHA"}, {0x000A, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"}, {0x000B, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA"}, {0x000C, "TLS_DH_DSS_WITH_DES_CBC_SHA"}, {0x000D, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA"}, {0x000E, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA"}, {0x000F, "TLS_DH_RSA_WITH_DES_CBC_SHA"}, {0x0010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA"}, {0x0011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"}, {0x0012, "TLS_DHE_DSS_WITH_DES_CBC_SHA"}, {0x0013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA"}, {0x0014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"}, {0x0015, "TLS_DHE_RSA_WITH_DES_CBC_SHA"}, {0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA"}, {0x0017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5"}, {0x0018, "TLS_DH_anon_WITH_RC4_128_MD5"}, {0x0019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA"}, {0x001A, "TLS_DH_anon_WITH_DES_CBC_SHA"}, {0x001B, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA"}, {0x001D, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA"}, {0x001E, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA"}, {0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"}, {0x0020, "TLS_KRB5_WITH_RC4_128_SHA"}, {0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA"}, {0x0022, "TLS_KRB5_WITH_DES_CBC_MD5"}, {0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5"}, {0x0024, "TLS_KRB5_WITH_RC4_128_MD5"}, {0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5"}, {0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA"}, {0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA"}, {0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA"}, {0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5"}, {0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5"}, {0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5"}, {0x002C, "TLS_PSK_WITH_NULL_SHA"}, {0x002D, "TLS_DHE_PSK_WITH_NULL_SHA"}, {0x002E, "TLS_RSA_PSK_WITH_NULL_SHA"}, {0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA"}, {0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA"}, {0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA"}, {0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA"}, {0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}, {0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA"}, {0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA"}, {0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA"}, {0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA"}, {0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA"}, {0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"}, {0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA"}, {0x003B, "TLS_RSA_WITH_NULL_SHA256"}, {0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256"}, {0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256"}, {0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256"}, {0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256"}, {0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256"}, {0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA"}, {0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA"}, {0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA"}, {0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA"}, {0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA"}, {0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA"}, {0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"}, {0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256"}, {0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256"}, {0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"}, {0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"}, {0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256"}, {0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256"}, {0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA"}, {0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA"}, {0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA"}, {0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA"}, {0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA"}, {0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA"}, {0x008A, "TLS_PSK_WITH_RC4_128_SHA"}, {0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA"}, {0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA"}, {0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA"}, {0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA"}, {0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA"}, {0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA"}, {0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA"}, {0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA"}, {0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA"}, {0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA"}, {0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA"}, {0x0096, "TLS_RSA_WITH_SEED_CBC_SHA"}, {0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA"}, {0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA"}, {0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA"}, {0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA"}, {0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA"}, {0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256"}, {0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384"}, {0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"}, {0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384"}, {0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256"}, {0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384"}, {0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256"}, {0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384"}, {0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256"}, {0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384"}, {0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256"}, {0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384"}, {0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256"}, {0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384"}, {0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256"}, {0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384"}, {0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256"}, {0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384"}, {0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256"}, {0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384"}, {0x00B0, "TLS_PSK_WITH_NULL_SHA256"}, {0x00B1, "TLS_PSK_WITH_NULL_SHA384"}, {0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256"}, {0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384"}, {0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256"}, {0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384"}, {0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256"}, {0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384"}, {0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256"}, {0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384"}, {0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256"}, {0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256"}, {0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"}, {0x5600, "TLS_FALLBACK_SCSV"}, {0xC001, "TLS_ECDH_ECDSA_WITH_NULL_SHA"}, {0xC002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA"}, {0xC003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA"}, {0xC004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA"}, {0xC005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA"}, {0xC006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA"}, {0xC007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"}, {0xC008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"}, {0xC009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"}, {0xC00A, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"}, {0xC00B, "TLS_ECDH_RSA_WITH_NULL_SHA"}, {0xC00C, "TLS_ECDH_RSA_WITH_RC4_128_SHA"}, {0xC00D, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA"}, {0xC00E, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA"}, {0xC00F, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA"}, {0xC010, "TLS_ECDHE_RSA_WITH_NULL_SHA"}, {0xC011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA"}, {0xC012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"}, {0xC013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"}, {0xC014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"}, {0xC015, "TLS_ECDH_anon_WITH_NULL_SHA"}, {0xC016, "TLS_ECDH_anon_WITH_RC4_128_SHA"}, {0xC017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA"}, {0xC018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA"}, {0xC019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA"}, {0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA"}, {0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA"}, {0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA"}, {0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA"}, {0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA"}, {0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA"}, {0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA"}, {0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA"}, {0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA"}, {0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"}, {0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"}, {0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256"}, {0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384"}, {0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}, {0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"}, {0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256"}, {0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384"}, {0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, {0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"}, {0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256"}, {0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384"}, {0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"}, {0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, {0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256"}, {0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384"}, {0xC033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA"}, {0xC034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA"}, {0xC035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA"}, {0xC036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA"}, {0xC037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256"}, {0xC038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384"}, {0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA"}, {0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256"}, {0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384"}, {0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256"}, {0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384"}, {0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256"}, {0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384"}, {0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256"}, {0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384"}, {0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256"}, {0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384"}, {0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256"}, {0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384"}, {0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256"}, {0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384"}, {0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256"}, {0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384"}, {0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256"}, {0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384"}, {0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256"}, {0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384"}, {0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256"}, {0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384"}, {0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256"}, {0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384"}, {0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256"}, {0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384"}, {0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256"}, {0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384"}, {0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256"}, {0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384"}, {0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256"}, {0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384"}, {0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256"}, {0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384"}, {0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256"}, {0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384"}, {0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256"}, {0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384"}, {0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256"}, {0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384"}, {0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256"}, {0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384"}, {0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256"}, {0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384"}, {0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256"}, {0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384"}, {0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256"}, {0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384"}, {0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256"}, {0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384"}, {0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256"}, {0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384"}, {0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256"}, {0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384"}, {0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256"}, {0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384"}, {0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256"}, {0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384"}, {0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256"}, {0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384"}, {0xC09C, "TLS_RSA_WITH_AES_128_CCM"}, {0xC09D, "TLS_RSA_WITH_AES_256_CCM"}, {0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM"}, {0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM"}, {0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8"}, {0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8"}, {0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8"}, {0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8"}, {0xC0A4, "TLS_PSK_WITH_AES_128_CCM"}, {0xC0A5, "TLS_PSK_WITH_AES_256_CCM"}, {0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM"}, {0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM"}, {0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8"}, {0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8"}, {0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8"}, {0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8"}, {0xC0AC, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"}, {0xC0AD, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM"}, {0xC0AE, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8"}, {0xC0AF, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8"}, {0xCCA8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCA9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCAA, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCAB, "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCAC, "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCAD, "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256"}, {0xCCAE, "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256"}, {0x1301, "TLS_AES_128_GCM_SHA256"}, {0x1302, "TLS_AES_256_GCM_SHA384"}, {0x1303, "TLS_CHACHA20_POLY1305_SHA256"}, {0x1304, "TLS_AES_128_CCM_SHA256"}, {0x1305, "TLS_AES_128_CCM_8_SHA256"}, {0xFEFE, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"}, {0xFEFF, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"}, }; static const char *get_std_name_by_id(int id) { size_t i; for (i = 0; i < OSSL_NELEM(cipher_names); i++) if (cipher_names[i].id == id) return cipher_names[i].name; return NULL; } static int test_cipher_name(void) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; const SSL_CIPHER *c; STACK_OF(SSL_CIPHER) *sk = NULL; const char *ciphers = "ALL:eNULL", *p, *q, *r; int i, id = 0, ret = 0; /* tests for invalid input */ p = SSL_CIPHER_standard_name(NULL); if (!TEST_str_eq(p, "(NONE)")) { TEST_info("test_cipher_name(std) failed: NULL input doesn't return \"(NONE)\"\n"); goto err; } p = OPENSSL_cipher_name(NULL); if (!TEST_str_eq(p, "(NONE)")) { TEST_info("test_cipher_name(ossl) failed: NULL input doesn't return \"(NONE)\"\n"); goto err; } p = OPENSSL_cipher_name("This is not a valid cipher"); if (!TEST_str_eq(p, "(NONE)")) { TEST_info("test_cipher_name(ossl) failed: invalid input doesn't return \"(NONE)\"\n"); goto err; } /* tests for valid input */ ctx = SSL_CTX_new(TLS_server_method()); if (ctx == NULL) { TEST_info("test_cipher_name failed: internal error\n"); goto err; } if (!SSL_CTX_set_cipher_list(ctx, ciphers)) { TEST_info("test_cipher_name failed: internal error\n"); goto err; } ssl = SSL_new(ctx); if (ssl == NULL) { TEST_info("test_cipher_name failed: internal error\n"); goto err; } sk = SSL_get_ciphers(ssl); if (sk == NULL) { TEST_info("test_cipher_name failed: internal error\n"); goto err; } for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { c = sk_SSL_CIPHER_value(sk, i); id = SSL_CIPHER_get_id(c) & 0xFFFF; if ((id == 0xC102) || (id == 0xFF85) ||(id == 0xFF87)) /* skip GOST2012-GOST8912-GOST891 and GOST2012-NULL-GOST12 */ continue; p = SSL_CIPHER_standard_name(c); q = get_std_name_by_id(id); if (!TEST_ptr(p)) { TEST_info("test_cipher_name failed: expected %s, got NULL, cipher %x\n", q, id); goto err; } /* check if p is a valid standard name */ if (!TEST_str_eq(p, q)) { TEST_info("test_cipher_name(std) failed: expected %s, got %s, cipher %x\n", q, p, id); goto err; } /* test OPENSSL_cipher_name */ q = SSL_CIPHER_get_name(c); r = OPENSSL_cipher_name(p); if (!TEST_str_eq(r, q)) { TEST_info("test_cipher_name(ossl) failed: expected %s, got %s, cipher %x\n", q, r, id); goto err; } } ret = 1; err: SSL_CTX_free(ctx); SSL_free(ssl); return ret; } int setup_tests(void) { ADD_TEST(test_cipher_name); return 1; }
./openssl/test/pkcs7_test.c
/* * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/pkcs7.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_EC static const unsigned char cert_der[] = { 0x30, 0x82, 0x01, 0x51, 0x30, 0x81, 0xf7, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03, 0x09, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x27, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x79, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x31, 0x30, 0x31, 0x31, 0x32, 0x30, 0x31, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x38, 0x31, 0x32, 0x33, 0x31, 0x30, 0x38, 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x27, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x79, 0x20, 0x43, 0x41, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x18, 0xff, 0xcf, 0xbb, 0xf9, 0x39, 0xb8, 0xf5, 0xdd, 0xc3, 0xee, 0xc0, 0x40, 0x8b, 0x06, 0x75, 0x06, 0xab, 0x4f, 0xcd, 0xd8, 0x2c, 0x52, 0x24, 0x4e, 0x1f, 0xe0, 0x10, 0x46, 0x67, 0xb5, 0x5f, 0x15, 0xb9, 0x62, 0xbd, 0x3b, 0xcf, 0x0c, 0x6f, 0xbe, 0x1a, 0xf7, 0xb4, 0xa1, 0x0f, 0xb4, 0xb9, 0xcb, 0x6e, 0x86, 0xb3, 0x50, 0xf9, 0x6c, 0x51, 0xbf, 0xc1, 0x82, 0xd7, 0xbe, 0xc5, 0xf9, 0x05, 0xa3, 0x13, 0x30, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xd1, 0x12, 0xef, 0x8d, 0x97, 0x5a, 0x6e, 0xb8, 0xb6, 0x41, 0xa7, 0xcf, 0xc0, 0xe7, 0xa4, 0x6e, 0xae, 0xda, 0x51, 0xe4, 0x64, 0x54, 0x2b, 0xde, 0x86, 0x95, 0xbc, 0xf7, 0x1e, 0x9a, 0xf9, 0x5b, 0x02, 0x21, 0x00, 0xd1, 0x61, 0x86, 0xce, 0x66, 0x31, 0xe4, 0x2f, 0x54, 0xbd, 0xf5, 0xc8, 0x2b, 0xb3, 0x44, 0xce, 0x24, 0xf8, 0xa5, 0x0b, 0x72, 0x11, 0x21, 0x34, 0xb9, 0x15, 0x4a, 0x5f, 0x0e, 0x27, 0x32, 0xa9 }; static int pkcs7_verify_test(void) { int ret = 0; size_t i; BIO *msg_bio = NULL, *x509_bio = NULL, *bio = NULL; X509 *cert = NULL; X509_STORE *store = NULL; PKCS7 *p7 = NULL; const char *sig[] = { "MIME-Version: 1.0\nContent-Type: multipart/signed; protocol=\"application/x-pkcs7-signature\"; micalg=\"sha-256\"; boundary=\"----9B5319FF2E4428B17CD26B69294E7F31\"\n\n", "This is an S/MIME signed message\n\n------9B5319FF2E4428B17CD26B69294E7F31\n", "Content-Type: text/plain\r\n\r\nhello world\n------9B5319FF2E4428B17CD26B69294E7F31\n", "Content-Type: application/x-pkcs7-signature; name=\"smime.p7s\"\n", "Content-Transfer-Encoding: base64\nContent-Disposition: attachment; filename=\"smime.p7s\"\n\n", "MIIDEgYJKoZIhvcNAQcCoIIDAzCCAv8CAQExDzANBglghkgBZQMEAgEFADALBgkq\nhkiG9w0BBwGgggFVMIIBUTCB96ADAgECAgIDCTAKBggqhkjOPQQDAjAnMQswCQYD\nVQQGEwJVUzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5IENBMB4XDTE3MDEwMTEyMDEw\nMFoXDTM4MTIzMTA4MzAwMFowJzELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD2NyeXB0\nb2dyYXBoeSBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABBj/z7v5Obj13cPu\nwECLBnUGq0/N2CxSJE4f4BBGZ7VfFblivTvPDG++Gve0oQ+0uctuhrNQ+WxRv8GC\n", "177F+QWjEzARMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSQAwRgIhANES\n742XWm64tkGnz8DnpG6u2lHkZFQr3oaVvPcemvlbAiEA0WGGzmYx5C9UvfXIK7NE\nziT4pQtyESE0uRVKXw4nMqkxggGBMIIBfQIBATAtMCcxCzAJBgNVBAYTAlVTMRgw\nFgYDVQQDDA9jcnlwdG9ncmFwaHkgQ0ECAgMJMA0GCWCGSAFlAwQCAQUAoIHkMBgG\nCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTIxMDUyMDE4\nNTA0OVowLwYJKoZIhvcNAQkEMSIEIOdwMRgQrqcnmMYvag+BVvErcc6bwUXI94Ds\n", "QkiyIU9pMHkGCSqGSIb3DQEJDzFsMGowCwYJYIZIAWUDBAEqMAsGCWCGSAFlAwQB\nFjALBglghkgBZQMEAQIwCgYIKoZIhvcNAwcwDgYIKoZIhvcNAwICAgCAMA0GCCqG\nSIb3DQMCAgFAMAcGBSsOAwIHMA0GCCqGSIb3DQMCAgEoMAoGCCqGSM49BAMCBEcw\nRQIhANYMJku1fW9T1MIEcAyREArz9kXCY4tWck5Pt0xzrYhaAiBDSP6e43zj4YtI\nuvQW+Lzv+dNF8EPuhgoPNe17RuUSLw==\n\n------9B5319FF2E4428B17CD26B69294E7F31--\n\n" }; const char *signed_data = "Content-Type: text/plain\r\n\r\nhello world"; if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto end; for (i = 0; i < OSSL_NELEM(sig); ++i) BIO_puts(bio, sig[i]); ret = TEST_ptr(msg_bio = BIO_new_mem_buf(signed_data, strlen(signed_data))) && TEST_ptr(x509_bio = BIO_new_mem_buf(cert_der, sizeof(cert_der))) && TEST_ptr(cert = d2i_X509_bio(x509_bio, NULL)) && TEST_int_eq(ERR_peek_error(), 0) && TEST_ptr(store = X509_STORE_new()) && TEST_true(X509_STORE_add_cert(store, cert)) && TEST_ptr(p7 = SMIME_read_PKCS7(bio, NULL)) && TEST_int_eq(ERR_peek_error(), 0) && TEST_true(PKCS7_verify(p7, NULL, store, msg_bio, NULL, PKCS7_TEXT)) && TEST_int_eq(ERR_peek_error(), 0); end: X509_STORE_free(store); X509_free(cert); PKCS7_free(p7); BIO_free(msg_bio); BIO_free(x509_bio); BIO_free(bio); return ret; } #endif /* OPENSSL_NO_EC */ int setup_tests(void) { #ifndef OPENSSL_NO_EC ADD_TEST(pkcs7_verify_test); #endif /* OPENSSL_NO_EC */ return 1; }
./openssl/test/cmsapitest.c
/* * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/cms.h> #include <openssl/bio.h> #include <openssl/x509.h> #include <openssl/pem.h> #include "../crypto/cms/cms_local.h" /* for d.signedData and d.envelopedData */ #include "testutil.h" static X509 *cert = NULL; static EVP_PKEY *privkey = NULL; static char *derin = NULL; static int test_encrypt_decrypt(const EVP_CIPHER *cipher) { int testresult = 0; STACK_OF(X509) *certstack = sk_X509_new_null(); const char *msg = "Hello world"; BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg)); BIO *outmsgbio = BIO_new(BIO_s_mem()); CMS_ContentInfo* content = NULL; BIO *contentbio = NULL; char buf[80]; if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio)) goto end; if (!TEST_int_gt(sk_X509_push(certstack, cert), 0)) goto end; content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT); if (!TEST_ptr(content)) goto end; if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio, CMS_TEXT))) goto end; if (!TEST_ptr(contentbio = CMS_EnvelopedData_decrypt(content->d.envelopedData, NULL, privkey, cert, NULL, CMS_TEXT, NULL, NULL))) goto end; /* Check we got the message we first started with */ if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg)) || !TEST_int_eq(strcmp(buf, msg), 0)) goto end; testresult = 1; end: BIO_free(contentbio); sk_X509_free(certstack); BIO_free(msgbio); BIO_free(outmsgbio); CMS_ContentInfo_free(content); return testresult && TEST_int_eq(ERR_peek_error(), 0); } static int test_encrypt_decrypt_aes_cbc(void) { return test_encrypt_decrypt(EVP_aes_128_cbc()); } static int test_encrypt_decrypt_aes_128_gcm(void) { return test_encrypt_decrypt(EVP_aes_128_gcm()); } static int test_encrypt_decrypt_aes_192_gcm(void) { return test_encrypt_decrypt(EVP_aes_192_gcm()); } static int test_encrypt_decrypt_aes_256_gcm(void) { return test_encrypt_decrypt(EVP_aes_256_gcm()); } static int test_CMS_add1_cert(void) { CMS_ContentInfo *cms = NULL; int ret = 0; ret = TEST_ptr(cms = CMS_ContentInfo_new()) && TEST_ptr(CMS_add1_signer(cms, cert, privkey, NULL, 0)) && TEST_true(CMS_add1_cert(cms, cert)); /* add cert again */ CMS_ContentInfo_free(cms); return ret; } static int test_d2i_CMS_bio_NULL(void) { BIO *bio, *content = NULL; CMS_ContentInfo *cms = NULL; unsigned int flags = CMS_NO_SIGNER_CERT_VERIFY; int ret = 0; /* * Test data generated using: * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \ * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \ * -nosmimecap */ static const unsigned char cms_data[] = { 0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0, 0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02, 0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f, 0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0, 0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30, 0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35, 0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31, 0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74, 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88, 0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92, 0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10, 0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b, 0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b, 0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97, 0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2, 0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d, 0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7, 0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e, 0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e, 0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00, 0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f, 0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e, 0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c, 0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91, 0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3, 0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb, 0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec, 0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9, 0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97, 0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46, 0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9, 0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0, 0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf, 0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21, 0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93, 0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc, 0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75, 0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f, 0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37, 0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c, 0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d, 0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56, 0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a, 0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce, 0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb, 0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69, 0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6, 0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe, 0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17, 0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95, 0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20, 0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08, 0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab, 0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3, 0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c, 0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79, 0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc, 0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a, 0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe, 0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d, 0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1, 0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba, 0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49, 0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87, 0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c, 0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97, 0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44, 0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5, 0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1, 0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b, 0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6, 0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5, 0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12, 0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d, 0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82, 0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01, 0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d, 0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39, 0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80, 0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26, 0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53, 0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49, 0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52, 0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c, 0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a, 0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05, 0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9, 0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e, 0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2, 0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71, 0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73, 0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5, 0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43, 0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c, 0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7, 0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99, 0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6, 0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe, 0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a, 0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37, 0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7, 0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb, 0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde, 0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49, 0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2, 0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85, 0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75, 0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01, 0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f, 0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30, 0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a, 0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5, 0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74, 0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3, 0x6a }; ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data))) && TEST_ptr(cms = d2i_CMS_bio(bio, NULL)) && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL, flags)) && TEST_ptr(content = CMS_SignedData_verify(cms->d.signedData, NULL, NULL, NULL, NULL, NULL, flags, NULL, NULL)); BIO_free(content); CMS_ContentInfo_free(cms); BIO_free(bio); return ret && TEST_int_eq(ERR_peek_error(), 0); } static unsigned char *read_all(BIO *bio, long *p_len) { const int step = 256; unsigned char *buf = NULL; unsigned char *tmp = NULL; int ret; *p_len = 0; for (;;) { tmp = OPENSSL_realloc(buf, *p_len + step); if (tmp == NULL) break; buf = tmp; ret = BIO_read(bio, buf + *p_len, step); if (ret < 0) break; *p_len += ret; if (ret < step) return buf; } /* Error */ OPENSSL_free(buf); *p_len = 0; return NULL; } static int test_d2i_CMS_decode(const int idx) { BIO *bio = NULL; CMS_ContentInfo *cms = NULL; unsigned char *buf = NULL; const unsigned char *tmp = NULL; long buf_len = 0; int ret = 0; if (!TEST_ptr(bio = BIO_new_file(derin, "r"))) goto end; switch (idx) { case 0: if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL))) goto end; break; case 1: if (!TEST_ptr(buf = read_all(bio, &buf_len))) goto end; tmp = buf; if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len))) goto end; break; } if (!TEST_int_eq(ERR_peek_error(), 0)) goto end; ret = 1; end: CMS_ContentInfo_free(cms); BIO_free(bio); OPENSSL_free(buf); return ret; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n") int setup_tests(void) { char *certin = NULL, *privkeyin = NULL; BIO *certbio = NULL, *privkeybio = NULL; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certin = test_get_argument(0)) || !TEST_ptr(privkeyin = test_get_argument(1)) || !TEST_ptr(derin = test_get_argument(2))) return 0; certbio = BIO_new_file(certin, "r"); if (!TEST_ptr(certbio)) return 0; if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) { BIO_free(certbio); return 0; } BIO_free(certbio); privkeybio = BIO_new_file(privkeyin, "r"); if (!TEST_ptr(privkeybio)) { X509_free(cert); cert = NULL; return 0; } if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) { BIO_free(privkeybio); X509_free(cert); cert = NULL; return 0; } BIO_free(privkeybio); ADD_TEST(test_encrypt_decrypt_aes_cbc); ADD_TEST(test_encrypt_decrypt_aes_128_gcm); ADD_TEST(test_encrypt_decrypt_aes_192_gcm); ADD_TEST(test_encrypt_decrypt_aes_256_gcm); ADD_TEST(test_CMS_add1_cert); ADD_TEST(test_d2i_CMS_bio_NULL); ADD_ALL_TESTS(test_d2i_CMS_decode, 2); return 1; } void cleanup_tests(void) { X509_free(cert); EVP_PKEY_free(privkey); }
./openssl/test/threadstest_fips.c
/* * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #if defined(_WIN32) # include <windows.h> #endif #include "testutil.h" #include "threadstest.h" static int success; static void thread_fips_rand_fetch(void) { EVP_MD *md; if (!TEST_true(md = EVP_MD_fetch(NULL, "SHA2-256", NULL))) success = 0; EVP_MD_free(md); } static int test_fips_rand_leak(void) { thread_t thread; success = 1; if (!TEST_true(run_thread(&thread, thread_fips_rand_fetch))) return 0; if (!TEST_true(wait_for_thread(thread))) return 0; return TEST_true(success); } int setup_tests(void) { /* * This test MUST be run first. Once the default library context is set * up, this test will always pass. */ ADD_TEST(test_fips_rand_leak); return 1; }
./openssl/test/pem_read_depr_test.c
/* * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * This file tests deprecated APIs. Therefore we need to suppress deprecation * warnings. */ #define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/pem.h> #include <openssl/bio.h> #include <openssl/dh.h> #include <openssl/dsa.h> #include <openssl/rsa.h> #include "testutil.h" static const char *datadir; static BIO *getfile(const char *filename) { char *paramsfile = test_mk_file_path(datadir, filename); BIO *infile = NULL; if (!TEST_ptr(paramsfile)) goto err; infile = BIO_new_file(paramsfile, "r"); err: OPENSSL_free(paramsfile); return infile; } #ifndef OPENSSL_NO_DH static int test_read_dh_params(void) { int testresult = 0; BIO *infile = getfile("dhparams.pem"); DH *dh = NULL; if (!TEST_ptr(infile)) goto err; dh = PEM_read_bio_DHparams(infile, NULL, NULL, NULL); if (!TEST_ptr(dh)) goto err; testresult = 1; err: BIO_free(infile); DH_free(dh); return testresult; } static int test_read_dh_x942_params(void) { int testresult = 0; BIO *infile = getfile("x942params.pem"); DH *dh = NULL; if (!TEST_ptr(infile)) goto err; dh = PEM_read_bio_DHparams(infile, NULL, NULL, NULL); if (!TEST_ptr(dh)) goto err; testresult = 1; err: BIO_free(infile); DH_free(dh); return testresult; } #endif #ifndef OPENSSL_NO_DSA static int test_read_dsa_params(void) { int testresult = 0; BIO *infile = getfile("dsaparams.pem"); DSA *dsa = NULL; if (!TEST_ptr(infile)) goto err; dsa = PEM_read_bio_DSAparams(infile, NULL, NULL, NULL); if (!TEST_ptr(dsa)) goto err; testresult = 1; err: BIO_free(infile); DSA_free(dsa); return testresult; } static int test_read_dsa_private(void) { int testresult = 0; BIO *infile = getfile("dsaprivatekey.pem"); DSA *dsa = NULL; if (!TEST_ptr(infile)) goto err; dsa = PEM_read_bio_DSAPrivateKey(infile, NULL, NULL, NULL); if (!TEST_ptr(dsa)) goto err; testresult = 1; err: BIO_free(infile); DSA_free(dsa); return testresult; } static int test_read_dsa_public(void) { int testresult = 0; BIO *infile = getfile("dsapublickey.pem"); DSA *dsa = NULL; if (!TEST_ptr(infile)) goto err; dsa = PEM_read_bio_DSA_PUBKEY(infile, NULL, NULL, NULL); if (!TEST_ptr(dsa)) goto err; testresult = 1; err: BIO_free(infile); DSA_free(dsa); return testresult; } #endif static int test_read_rsa_private(void) { int testresult = 0; BIO *infile = getfile("rsaprivatekey.pem"); RSA *rsa = NULL; if (!TEST_ptr(infile)) goto err; rsa = PEM_read_bio_RSAPrivateKey(infile, NULL, NULL, NULL); if (!TEST_ptr(rsa)) goto err; testresult = 1; err: BIO_free(infile); RSA_free(rsa); return testresult; } static int test_read_rsa_public(void) { int testresult = 0; BIO *infile = getfile("rsapublickey.pem"); RSA *rsa = NULL; if (!TEST_ptr(infile)) goto err; rsa = PEM_read_bio_RSA_PUBKEY(infile, NULL, NULL, NULL); if (!TEST_ptr(rsa)) goto err; testresult = 1; err: BIO_free(infile); RSA_free(rsa); return testresult; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(datadir = test_get_argument(0))) { TEST_error("Error getting data dir\n"); return 0; } #ifndef OPENSSL_NO_DH ADD_TEST(test_read_dh_params); ADD_TEST(test_read_dh_x942_params); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_read_dsa_params); ADD_TEST(test_read_dsa_private); ADD_TEST(test_read_dsa_public); #endif ADD_TEST(test_read_rsa_private); ADD_TEST(test_read_rsa_public); return 1; }
./openssl/test/ssl_old_test.c
/* * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved * Copyright 2005 Nokia. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/e_os.h" /* Or gethostname won't be declared properly on Linux and GNU platforms. */ #ifndef _BSD_SOURCE # define _BSD_SOURCE 1 #endif #ifndef _DEFAULT_SOURCE # define _DEFAULT_SOURCE 1 #endif #include <assert.h> #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "internal/nelem.h" #ifdef OPENSSL_SYS_VMS /* * Or isascii won't be declared properly on VMS (at least with DECompHP C). */ # define _XOPEN_SOURCE 500 #endif #include <ctype.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/rsa.h> #ifndef OPENSSL_NO_DSA # include <openssl/dsa.h> #endif #include <openssl/bn.h> #ifndef OPENSSL_NO_CT # include <openssl/ct.h> #endif #include <openssl/provider.h> #include "testutil.h" #include "testutil/output.h" /* * Or gethostname won't be declared properly * on Compaq platforms (at least with DEC C). * Do not try to put it earlier, or IPv6 includes * get screwed... */ #define _XOPEN_SOURCE_EXTENDED 1 #ifdef OPENSSL_SYS_WINDOWS # include <winsock.h> #else # include <unistd.h> #endif #include "helpers/predefined_dhparams.h" static SSL_CTX *s_ctx = NULL; static SSL_CTX *s_ctx2 = NULL; /* * There is really no standard for this, so let's assign something * only for this test */ #define COMP_ZLIB 1 static int verify_callback(int ok, X509_STORE_CTX *ctx); static int app_verify_callback(X509_STORE_CTX *ctx, void *arg); #define APP_CALLBACK_STRING "Test Callback Argument" struct app_verify_arg { char *string; int app_verify; }; static char *psk_key = NULL; /* by default PSK is not used */ #ifndef OPENSSL_NO_PSK static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len); static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len); #endif static BIO *bio_stdout = NULL; #ifndef OPENSSL_NO_NEXTPROTONEG /* Note that this code assumes that this is only a one element list: */ static const char NEXT_PROTO_STRING[] = "\x09testproto"; static int npn_client = 0; static int npn_server = 0; static int npn_server_reject = 0; static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { /* * This callback only returns the protocol string, rather than a length * prefixed set. We assume that NEXT_PROTO_STRING is a one element list * and remove the first byte to chop off the length prefix. */ *out = (unsigned char *)NEXT_PROTO_STRING + 1; *outlen = sizeof(NEXT_PROTO_STRING) - 2; return SSL_TLSEXT_ERR_OK; } static int cb_server_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { *data = (const unsigned char *)NEXT_PROTO_STRING; *len = sizeof(NEXT_PROTO_STRING) - 1; return SSL_TLSEXT_ERR_OK; } static int cb_server_rejects_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { return SSL_TLSEXT_ERR_NOACK; } static int verify_npn(SSL *client, SSL *server) { const unsigned char *client_s; unsigned client_len; const unsigned char *server_s; unsigned server_len; SSL_get0_next_proto_negotiated(client, &client_s, &client_len); SSL_get0_next_proto_negotiated(server, &server_s, &server_len); if (client_len) { BIO_printf(bio_stdout, "Client NPN: "); BIO_write(bio_stdout, client_s, client_len); BIO_printf(bio_stdout, "\n"); } if (server_len) { BIO_printf(bio_stdout, "Server NPN: "); BIO_write(bio_stdout, server_s, server_len); BIO_printf(bio_stdout, "\n"); } /* * If an NPN string was returned, it must be the protocol that we * expected to negotiate. */ if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(client_s, NEXT_PROTO_STRING + 1, client_len))) return -1; if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(server_s, NEXT_PROTO_STRING + 1, server_len))) return -1; if (!npn_client && client_len) return -1; if (!npn_server && server_len) return -1; if (npn_server_reject && server_len) return -1; if (npn_client && npn_server && (!client_len || !server_len)) return -1; return 0; } #endif static const char *alpn_client; static char *alpn_server; static char *alpn_server2; static const char *alpn_expected; static unsigned char *alpn_selected; static const char *server_min_proto; static const char *server_max_proto; static const char *client_min_proto; static const char *client_max_proto; static const char *should_negotiate; static const char *sn_client; static const char *sn_server1; static const char *sn_server2; static int sn_expect = 0; static const char *server_sess_out; static const char *server_sess_in; static const char *client_sess_out; static const char *client_sess_in; static SSL_SESSION *server_sess; static SSL_SESSION *client_sess; static int servername_cb(SSL *s, int *ad, void *arg) { const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (sn_server2 == NULL) { BIO_printf(bio_stdout, "Servername 2 is NULL\n"); return SSL_TLSEXT_ERR_NOACK; } if (servername) { if (s_ctx2 != NULL && sn_server2 != NULL && !OPENSSL_strcasecmp(servername, sn_server2)) { BIO_printf(bio_stdout, "Switching server context.\n"); SSL_set_SSL_CTX(s, s_ctx2); } } return SSL_TLSEXT_ERR_OK; } static int verify_servername(SSL *client, SSL *server) { /* just need to see if sn_context is what we expect */ SSL_CTX* ctx = SSL_get_SSL_CTX(server); if (sn_expect == 0) return 0; if (sn_expect == 1 && ctx == s_ctx) return 0; if (sn_expect == 2 && ctx == s_ctx2) return 0; BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect); if (ctx == s_ctx2) BIO_printf(bio_stdout, "Servername: context is 2\n"); else if (ctx == s_ctx) BIO_printf(bio_stdout, "Servername: context is 1\n"); else BIO_printf(bio_stdout, "Servername: context is unknown\n"); return -1; } /*- * next_protos_parse parses a comma separated list of strings into a string * in a format suitable for passing to SSL_CTX_set_next_protos_advertised. * outlen: (output) set to the length of the resulting buffer on success. * in: a NUL terminated string like "abc,def,ghi" * * returns: a malloced buffer or NULL on failure. */ static unsigned char *next_protos_parse(size_t *outlen, const char *in) { size_t len; unsigned char *out; size_t i, start = 0; len = strlen(in); if (len >= 65535) return NULL; out = OPENSSL_malloc(strlen(in) + 1); if (!out) return NULL; for (i = 0; i <= len; ++i) { if (i == len || in[i] == ',') { if (i - start > 255) { OPENSSL_free(out); return NULL; } out[start] = (unsigned char)(i - start); start = i + 1; } else out[i + 1] = in[i]; } *outlen = len + 1; return out; } static int cb_server_alpn(SSL *s, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { unsigned char *protos; size_t protos_len; char* alpn_str = arg; protos = next_protos_parse(&protos_len, alpn_str); if (protos == NULL) { fprintf(stderr, "failed to parser ALPN server protocol string: %s\n", alpn_str); abort(); } if (SSL_select_next_proto ((unsigned char **)out, outlen, protos, protos_len, in, inlen) != OPENSSL_NPN_NEGOTIATED) { OPENSSL_free(protos); return SSL_TLSEXT_ERR_NOACK; } /* * Make a copy of the selected protocol which will be freed in * verify_alpn. */ alpn_selected = OPENSSL_malloc(*outlen); if (alpn_selected == NULL) { fprintf(stderr, "failed to allocate memory\n"); OPENSSL_free(protos); abort(); } memcpy(alpn_selected, *out, *outlen); *out = alpn_selected; OPENSSL_free(protos); return SSL_TLSEXT_ERR_OK; } static int verify_alpn(SSL *client, SSL *server) { const unsigned char *client_proto, *server_proto; unsigned int client_proto_len = 0, server_proto_len = 0; SSL_get0_alpn_selected(client, &client_proto, &client_proto_len); SSL_get0_alpn_selected(server, &server_proto, &server_proto_len); OPENSSL_free(alpn_selected); alpn_selected = NULL; if (client_proto == NULL && client_proto_len != 0) { BIO_printf(bio_stdout, "Inconsistent SSL_get0_alpn_selected() for client!\n"); goto err; } if (server_proto == NULL && server_proto_len != 0) { BIO_printf(bio_stdout, "Inconsistent SSL_get0_alpn_selected() for server!\n"); goto err; } if (client_proto_len != server_proto_len) { BIO_printf(bio_stdout, "ALPN selected protocols differ!\n"); goto err; } if (client_proto != NULL && memcmp(client_proto, server_proto, client_proto_len) != 0) { BIO_printf(bio_stdout, "ALPN selected protocols differ!\n"); goto err; } if (client_proto_len > 0 && alpn_expected == NULL) { BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n"); goto err; } if (alpn_expected != NULL && (client_proto_len != strlen(alpn_expected) || memcmp(client_proto, alpn_expected, client_proto_len) != 0)) { BIO_printf(bio_stdout, "ALPN selected protocols not equal to expected protocol: %s\n", alpn_expected); goto err; } return 0; err: BIO_printf(bio_stdout, "ALPN results: client: '"); BIO_write(bio_stdout, client_proto, client_proto_len); BIO_printf(bio_stdout, "', server: '"); BIO_write(bio_stdout, server_proto, server_proto_len); BIO_printf(bio_stdout, "'\n"); BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '", alpn_client); if (SSL_get_SSL_CTX(server) == s_ctx2) { BIO_printf(bio_stdout, "%s'\n", alpn_server2); } else { BIO_printf(bio_stdout, "%s'\n", alpn_server); } return -1; } /* * WARNING : below extension types are *NOT* IETF assigned, and could * conflict if these types are reassigned and handled specially by OpenSSL * in the future */ #define TACK_EXT_TYPE 62208 #define CUSTOM_EXT_TYPE_0 1000 #define CUSTOM_EXT_TYPE_1 1001 #define CUSTOM_EXT_TYPE_2 1002 #define CUSTOM_EXT_TYPE_3 1003 static const char custom_ext_cli_string[] = "abc"; static const char custom_ext_srv_string[] = "defg"; /* These set from cmdline */ static char *serverinfo_file = NULL; static int serverinfo_sct = 0; static int serverinfo_tack = 0; /* These set based on extension callbacks */ static int serverinfo_sct_seen = 0; static int serverinfo_tack_seen = 0; static int serverinfo_other_seen = 0; /* This set from cmdline */ static int custom_ext = 0; /* This set based on extension callbacks */ static int custom_ext_error = 0; static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp) serverinfo_sct_seen++; else if (ext_type == TACK_EXT_TYPE) serverinfo_tack_seen++; else serverinfo_other_seen++; return 1; } static int verify_serverinfo(void) { if (serverinfo_sct != serverinfo_sct_seen) return -1; if (serverinfo_tack != serverinfo_tack_seen) return -1; if (serverinfo_other_seen) return -1; return 0; } /*- * Four test cases for custom extensions: * 0 - no ClientHello extension or ServerHello response * 1 - ClientHello with "abc", no response * 2 - ClientHello with "abc", empty response * 3 - ClientHello with "abc", "defg" response */ static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_0) custom_ext_error = 1; return 0; /* Don't send an extension */ } static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { return 1; } static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_1) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; /* Send "abc" */ } static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { return 1; } static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; /* Send "abc" */ } static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; if (inlen != 0) custom_ext_error = 1; /* Should be empty response */ return 1; } static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; /* Send "abc" */ } static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; if (inlen != strlen(custom_ext_srv_string)) custom_ext_error = 1; if (memcmp(custom_ext_srv_string, in, inlen) != 0) custom_ext_error = 1; /* Check for "defg" */ return 1; } /* * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback * for this extension */ static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { custom_ext_error = 1; return 1; } /* 'add' callbacks are only called if the 'parse' callback is called */ static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { /* Error: should not have been called */ custom_ext_error = 1; return 0; /* Don't send an extension */ } static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_1) custom_ext_error = 1; /* Check for "abc" */ if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { return 0; /* Don't send an extension */ } static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; /* Check for "abc" */ if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { *out = NULL; *outlen = 0; return 1; /* Send empty extension */ } static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; /* Check for "abc" */ if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { *out = (const unsigned char *)custom_ext_srv_string; *outlen = strlen(custom_ext_srv_string); return 1; /* Send "defg" */ } static char *cipher = NULL; static char *ciphersuites = NULL; static int verbose = 0; static int debug = 0; int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long bytes, clock_t *s_time, clock_t *c_time); int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time, clock_t *c_time); int doit(SSL *s_ssl, SSL *c_ssl, long bytes); static void sv_usage(void) { fprintf(stderr, "usage: ssltest [args ...]\n"); fprintf(stderr, "\n"); fprintf(stderr, " -server_auth - check server certificate\n"); fprintf(stderr, " -client_auth - do client authentication\n"); fprintf(stderr, " -v - more output\n"); fprintf(stderr, " -d - debug output\n"); fprintf(stderr, " -reuse - use session-id reuse\n"); fprintf(stderr, " -num <val> - number of connections to perform\n"); fprintf(stderr, " -bytes <val> - number of bytes to swap between client/server\n"); #ifndef OPENSSL_NO_DH fprintf(stderr, " -dhe512 - use 512 bit key for DHE (to test failure)\n"); fprintf(stderr, " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n"); fprintf(stderr, " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n"); fprintf(stderr, " -dhe4096 - use 4096 bit key (safe prime) for DHE\n"); #endif fprintf(stderr, " -no_dhe - disable DHE\n"); #ifndef OPENSSL_NO_EC fprintf(stderr, " -no_ecdhe - disable ECDHE\n"); #endif #ifndef OPENSSL_NO_PSK fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n"); #endif #ifndef OPENSSL_NO_SSL3 fprintf(stderr, " -ssl3 - use SSLv3\n"); #endif #ifndef OPENSSL_NO_TLS1 fprintf(stderr, " -tls1 - use TLSv1\n"); #endif #ifndef OPENSSL_NO_TLS1_1 fprintf(stderr, " -tls1_1 - use TLSv1.1\n"); #endif #ifndef OPENSSL_NO_TLS1_2 fprintf(stderr, " -tls1_2 - use TLSv1.2\n"); #endif #ifndef OPENSSL_NO_DTLS fprintf(stderr, " -dtls - use DTLS\n"); #ifndef OPENSSL_NO_DTLS1 fprintf(stderr, " -dtls1 - use DTLSv1\n"); #endif #ifndef OPENSSL_NO_DTLS1_2 fprintf(stderr, " -dtls12 - use DTLSv1.2\n"); #endif #endif fprintf(stderr, " -CApath arg - PEM format directory of CA's\n"); fprintf(stderr, " -CAfile arg - PEM format file of CA's\n"); fprintf(stderr, " -s_cert arg - Server certificate file\n"); fprintf(stderr, " -s_key arg - Server key file (default: same as -cert)\n"); fprintf(stderr, " -c_cert arg - Client certificate file\n"); fprintf(stderr, " -c_key arg - Client key file (default: same as -c_cert)\n"); fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n"); fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n"); fprintf(stderr, " -bio_pair - Use BIO pairs\n"); fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n"); fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n"); fprintf(stderr, " -f - Test even cases that can't work\n"); fprintf(stderr, " -time - measure processor time used by client and server\n"); fprintf(stderr, " -zlib - use zlib compression\n"); #ifndef OPENSSL_NO_NEXTPROTONEG fprintf(stderr, " -npn_client - have client side offer NPN\n"); fprintf(stderr, " -npn_server - have server side offer NPN\n"); fprintf(stderr, " -npn_server_reject - have server reject NPN\n"); #endif fprintf(stderr, " -serverinfo_file file - have server use this file\n"); fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n"); fprintf(stderr, " -serverinfo_tack - have client offer and expect TACK\n"); fprintf(stderr, " -custom_ext - try various custom extension callbacks\n"); fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n"); fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n"); fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n"); fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n"); fprintf(stderr, " -alpn_expected <string> - the ALPN protocol that should be negotiated\n"); fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n"); fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n"); fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n"); fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n"); fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n"); #ifndef OPENSSL_NO_CT fprintf(stderr, " -noct - no certificate transparency\n"); fprintf(stderr, " -requestct - request certificate transparency\n"); fprintf(stderr, " -requirect - require certificate transparency\n"); #endif fprintf(stderr, " -sn_client <string> - have client request this servername\n"); fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n"); fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n"); fprintf(stderr, " -sn_expect1 - expected server 1\n"); fprintf(stderr, " -sn_expect2 - expected server 2\n"); fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n"); fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n"); fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n"); fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n"); fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n"); fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n"); fprintf(stderr, " -client_ktls - try to enable client KTLS\n"); fprintf(stderr, " -server_ktls - try to enable server KTLS\n"); fprintf(stderr, " -provider <name> - Load the given provider into the library context\n"); fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n"); } static void print_key_details(BIO *out, EVP_PKEY *key) { int keyid = EVP_PKEY_get_id(key); #ifndef OPENSSL_NO_EC if (keyid == EVP_PKEY_EC) { char group[80]; size_t size; if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size)) strcpy(group, "unknown group"); BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group); } else #endif { const char *algname; switch (keyid) { case EVP_PKEY_RSA: algname = "RSA"; break; case EVP_PKEY_DSA: algname = "DSA"; break; case EVP_PKEY_DH: algname = "DH"; break; default: algname = OBJ_nid2sn(keyid); break; } BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname); } } static void print_details(SSL *c_ssl, const char *prefix) { const SSL_CIPHER *ciph; int mdnid; X509 *cert; EVP_PKEY *pkey; ciph = SSL_get_current_cipher(c_ssl); BIO_printf(bio_stdout, "%s%s, cipher %s %s", prefix, SSL_get_version(c_ssl), SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph)); cert = SSL_get0_peer_certificate(c_ssl); if (cert != NULL) { EVP_PKEY* pubkey = X509_get0_pubkey(cert); if (pubkey != NULL) { BIO_puts(bio_stdout, ", "); print_key_details(bio_stdout, pubkey); } } if (SSL_get_peer_tmp_key(c_ssl, &pkey)) { BIO_puts(bio_stdout, ", temp key: "); print_key_details(bio_stdout, pkey); EVP_PKEY_free(pkey); } if (SSL_get_peer_signature_nid(c_ssl, &mdnid)) BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid)); BIO_printf(bio_stdout, "\n"); } /* * protocol_from_string - converts a protocol version string to a number * * Returns -1 on failure or the version on success */ static int protocol_from_string(const char *value) { struct protocol_versions { const char *name; int version; }; static const struct protocol_versions versions[] = { {"ssl3", SSL3_VERSION}, {"tls1", TLS1_VERSION}, {"tls1.1", TLS1_1_VERSION}, {"tls1.2", TLS1_2_VERSION}, {"tls1.3", TLS1_3_VERSION}, {"dtls1", DTLS1_VERSION}, {"dtls1.2", DTLS1_2_VERSION}}; size_t i; size_t n = OSSL_NELEM(versions); for (i = 0; i < n; i++) if (strcmp(versions[i].name, value) == 0) return versions[i].version; return -1; } static SSL_SESSION *read_session(const char *filename) { SSL_SESSION *sess; BIO *f = BIO_new_file(filename, "r"); if (f == NULL) { BIO_printf(bio_err, "Can't open session file %s\n", filename); ERR_print_errors(bio_err); return NULL; } sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL); if (sess == NULL) { BIO_printf(bio_err, "Can't parse session file %s\n", filename); ERR_print_errors(bio_err); } BIO_free(f); return sess; } static int write_session(const char *filename, SSL_SESSION *sess) { BIO *f; if (sess == NULL) { BIO_printf(bio_err, "No session information\n"); return 0; } f = BIO_new_file(filename, "w"); if (f == NULL) { BIO_printf(bio_err, "Can't open session file %s\n", filename); ERR_print_errors(bio_err); return 0; } PEM_write_bio_SSL_SESSION(f, sess); BIO_free(f); return 1; } /* * set_protocol_version - Sets protocol version minimum or maximum * * Returns 0 on failure and 1 on success */ static int set_protocol_version(const char *version, SSL *ssl, int setting) { if (version != NULL) { int ver = protocol_from_string(version); if (ver < 0) { BIO_printf(bio_err, "Error parsing: %s\n", version); return 0; } return SSL_ctrl(ssl, setting, ver, NULL); } return 1; } int main(int argc, char *argv[]) { const char *CApath = NULL, *CAfile = NULL; int badop = 0; enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM; int force = 0; int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0; int ret = EXIT_FAILURE; int client_auth = 0; int server_auth = 0, i; struct app_verify_arg app_verify_arg = { APP_CALLBACK_STRING, 0 }; SSL_CTX *c_ctx = NULL; const SSL_METHOD *meth = NULL; SSL *c_ssl = NULL; SSL *s_ssl = NULL; int number = 1, reuse = 0; int should_reuse = -1; int no_ticket = 0; int client_ktls = 0, server_ktls = 0; long bytes = 256L; #ifndef OPENSSL_NO_DH EVP_PKEY *dhpkey; int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0; int no_dhe = 0; #endif int no_psk = 0; int print_time = 0; clock_t s_time = 0, c_time = 0; #ifndef OPENSSL_NO_COMP int n, comp = 0; COMP_METHOD *cm = NULL; STACK_OF(SSL_COMP) *ssl_comp_methods = NULL; #endif int no_protocol; int min_version = 0, max_version = 0; #ifndef OPENSSL_NO_CT /* * Disable CT validation by default, because it will interfere with * anything using custom extension handlers to deal with SCT extensions. */ int ct_validation = 0; #endif SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL; STACK_OF(OPENSSL_STRING) *conf_args = NULL; char *arg = NULL, *argn = NULL; const char *provider = NULL, *config = NULL; OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL; OSSL_LIB_CTX *libctx = NULL; verbose = 0; debug = 0; test_open_streams(); bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT); s_cctx = SSL_CONF_CTX_new(); s_cctx2 = SSL_CONF_CTX_new(); c_cctx = SSL_CONF_CTX_new(); if (!s_cctx || !c_cctx || !s_cctx2) { ERR_print_errors(bio_err); goto end; } SSL_CONF_CTX_set_flags(s_cctx, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); SSL_CONF_CTX_set_flags(s_cctx2, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) { ERR_print_errors(bio_err); goto end; } if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) { ERR_print_errors(bio_err); goto end; } SSL_CONF_CTX_set_flags(c_cctx, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) { ERR_print_errors(bio_err); goto end; } argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-F") == 0) { fprintf(stderr, "not compiled with FIPS support, so exiting without running.\n"); ret = EXIT_SUCCESS; goto end; } else if (strcmp(*argv, "-server_auth") == 0) server_auth = 1; else if (strcmp(*argv, "-client_auth") == 0) client_auth = 1; else if (strcmp(*argv, "-v") == 0) verbose = 1; else if (strcmp(*argv, "-d") == 0) debug = 1; else if (strcmp(*argv, "-reuse") == 0) reuse = 1; else if (strcmp(*argv, "-no_dhe") == 0) #ifdef OPENSSL_NO_DH /* unused in this case */; #else no_dhe = 1; else if (strcmp(*argv, "-dhe512") == 0) dhe512 = 1; else if (strcmp(*argv, "-dhe1024dsa") == 0) dhe1024dsa = 1; else if (strcmp(*argv, "-dhe4096") == 0) dhe4096 = 1; #endif else if (strcmp(*argv, "-no_ecdhe") == 0) /* obsolete */; else if (strcmp(*argv, "-psk") == 0) { if (--argc < 1) goto bad; psk_key = *(++argv); #ifndef OPENSSL_NO_PSK if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) { BIO_printf(bio_err, "Not a hex number '%s'\n", *argv); goto bad; } #else no_psk = 1; #endif } else if (strcmp(*argv, "-tls1_2") == 0) { tls1_2 = 1; } else if (strcmp(*argv, "-tls1_1") == 0) { tls1_1 = 1; } else if (strcmp(*argv, "-tls1") == 0) { tls1 = 1; } else if (strcmp(*argv, "-ssl3") == 0) { ssl3 = 1; } else if (strcmp(*argv, "-dtls1") == 0) { dtls1 = 1; } else if (strcmp(*argv, "-dtls12") == 0) { dtls12 = 1; } else if (strcmp(*argv, "-dtls") == 0) { dtls = 1; } else if (HAS_PREFIX(*argv, "-num")) { if (--argc < 1) goto bad; number = atoi(*(++argv)); if (number == 0) number = 1; } else if (strcmp(*argv, "-bytes") == 0) { if (--argc < 1) goto bad; bytes = atol(*(++argv)); if (bytes == 0L) bytes = 1L; i = strlen(argv[0]); if (argv[0][i - 1] == 'k') bytes *= 1024L; if (argv[0][i - 1] == 'm') bytes *= 1024L * 1024L; } else if (strcmp(*argv, "-cipher") == 0) { if (--argc < 1) goto bad; cipher = *(++argv); } else if (strcmp(*argv, "-ciphersuites") == 0) { if (--argc < 1) goto bad; ciphersuites = *(++argv); } else if (strcmp(*argv, "-CApath") == 0) { if (--argc < 1) goto bad; CApath = *(++argv); } else if (strcmp(*argv, "-CAfile") == 0) { if (--argc < 1) goto bad; CAfile = *(++argv); } else if (strcmp(*argv, "-bio_pair") == 0) { bio_type = BIO_PAIR; } #ifndef OPENSSL_NO_SOCK else if (strcmp(*argv, "-ipv4") == 0) { bio_type = BIO_IPV4; } else if (strcmp(*argv, "-ipv6") == 0) { bio_type = BIO_IPV6; } #endif else if (strcmp(*argv, "-f") == 0) { force = 1; } else if (strcmp(*argv, "-time") == 0) { print_time = 1; } #ifndef OPENSSL_NO_CT else if (strcmp(*argv, "-noct") == 0) { ct_validation = 0; } else if (strcmp(*argv, "-ct") == 0) { ct_validation = 1; } #endif #ifndef OPENSSL_NO_COMP else if (strcmp(*argv, "-zlib") == 0) { comp = COMP_ZLIB; } #endif else if (strcmp(*argv, "-app_verify") == 0) { app_verify_arg.app_verify = 1; } #ifndef OPENSSL_NO_NEXTPROTONEG else if (strcmp(*argv, "-npn_client") == 0) { npn_client = 1; } else if (strcmp(*argv, "-npn_server") == 0) { npn_server = 1; } else if (strcmp(*argv, "-npn_server_reject") == 0) { npn_server_reject = 1; } #endif else if (strcmp(*argv, "-serverinfo_sct") == 0) { serverinfo_sct = 1; } else if (strcmp(*argv, "-serverinfo_tack") == 0) { serverinfo_tack = 1; } else if (strcmp(*argv, "-serverinfo_file") == 0) { if (--argc < 1) goto bad; serverinfo_file = *(++argv); } else if (strcmp(*argv, "-custom_ext") == 0) { custom_ext = 1; } else if (strcmp(*argv, "-alpn_client") == 0) { if (--argc < 1) goto bad; alpn_client = *(++argv); } else if (strcmp(*argv, "-alpn_server") == 0 || strcmp(*argv, "-alpn_server1") == 0) { if (--argc < 1) goto bad; alpn_server = *(++argv); } else if (strcmp(*argv, "-alpn_server2") == 0) { if (--argc < 1) goto bad; alpn_server2 = *(++argv); } else if (strcmp(*argv, "-alpn_expected") == 0) { if (--argc < 1) goto bad; alpn_expected = *(++argv); } else if (strcmp(*argv, "-server_min_proto") == 0) { if (--argc < 1) goto bad; server_min_proto = *(++argv); } else if (strcmp(*argv, "-server_max_proto") == 0) { if (--argc < 1) goto bad; server_max_proto = *(++argv); } else if (strcmp(*argv, "-client_min_proto") == 0) { if (--argc < 1) goto bad; client_min_proto = *(++argv); } else if (strcmp(*argv, "-client_max_proto") == 0) { if (--argc < 1) goto bad; client_max_proto = *(++argv); } else if (strcmp(*argv, "-should_negotiate") == 0) { if (--argc < 1) goto bad; should_negotiate = *(++argv); } else if (strcmp(*argv, "-sn_client") == 0) { if (--argc < 1) goto bad; sn_client = *(++argv); } else if (strcmp(*argv, "-sn_server1") == 0) { if (--argc < 1) goto bad; sn_server1 = *(++argv); } else if (strcmp(*argv, "-sn_server2") == 0) { if (--argc < 1) goto bad; sn_server2 = *(++argv); } else if (strcmp(*argv, "-sn_expect1") == 0) { sn_expect = 1; } else if (strcmp(*argv, "-sn_expect2") == 0) { sn_expect = 2; } else if (strcmp(*argv, "-server_sess_out") == 0) { if (--argc < 1) goto bad; server_sess_out = *(++argv); } else if (strcmp(*argv, "-server_sess_in") == 0) { if (--argc < 1) goto bad; server_sess_in = *(++argv); } else if (strcmp(*argv, "-client_sess_out") == 0) { if (--argc < 1) goto bad; client_sess_out = *(++argv); } else if (strcmp(*argv, "-client_sess_in") == 0) { if (--argc < 1) goto bad; client_sess_in = *(++argv); } else if (strcmp(*argv, "-should_reuse") == 0) { if (--argc < 1) goto bad; should_reuse = !!atoi(*(++argv)); } else if (strcmp(*argv, "-no_ticket") == 0) { no_ticket = 1; } else if (strcmp(*argv, "-client_ktls") == 0) { client_ktls = 1; } else if (strcmp(*argv, "-server_ktls") == 0) { server_ktls = 1; } else if (strcmp(*argv, "-provider") == 0) { if (--argc < 1) goto bad; provider = *(++argv); } else if (strcmp(*argv, "-config") == 0) { if (--argc < 1) goto bad; config = *(++argv); } else { int rv; arg = argv[0]; argn = argv[1]; /* Try to process command using SSL_CONF */ rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv); /* If not processed try server */ if (rv == 0) rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv); /* Recognised: store it for later use */ if (rv > 0) { if (rv == 1) argn = NULL; if (!conf_args) { conf_args = sk_OPENSSL_STRING_new_null(); if (!conf_args) goto end; } if (!sk_OPENSSL_STRING_push(conf_args, arg)) goto end; if (!sk_OPENSSL_STRING_push(conf_args, argn)) goto end; continue; } if (rv == -3) BIO_printf(bio_err, "Missing argument for %s\n", arg); else if (rv < 0) BIO_printf(bio_err, "Error with command %s\n", arg); else if (rv == 0) BIO_printf(bio_err, "unknown option %s\n", arg); badop = 1; break; } argc--; argv++; } if (badop) { bad: sv_usage(); goto end; } if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) { fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should " "be requested.\n"); goto end; } #ifdef OPENSSL_NO_SSL3 if (ssl3) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1 if (tls1) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1_1 if (tls1_1) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1_2 if (tls1_2) no_protocol = 1; else #endif #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1) if (dtls1) no_protocol = 1; else #endif #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2) if (dtls12) no_protocol = 1; else #endif no_protocol = 0; /* * Testing was requested for a compiled-out protocol (e.g. SSLv3). * Ideally, we would error out, but the generic test wrapper can't know * when to expect failure. So we do nothing and return success. */ if (no_protocol) { fprintf(stderr, "Testing was requested for a disabled protocol. " "Skipping tests.\n"); ret = EXIT_SUCCESS; goto end; } if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1 && !reuse && !force) { fprintf(stderr, "This case cannot work. Use -f to perform " "the test anyway (and\n-d to see what happens), " "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n" "to avoid protocol mismatch.\n"); goto end; } if (print_time) { if (bio_type == BIO_MEM) { fprintf(stderr, "Using BIO pair (-bio_pair)\n"); bio_type = BIO_PAIR; } if (number < 50 && !force) fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n"); } #ifndef OPENSSL_NO_COMP if (comp == COMP_ZLIB) cm = COMP_zlib(); if (cm != NULL) { if (SSL_COMP_add_compression_method(comp, cm) != 0) { fprintf(stderr, "Failed to add compression method\n"); ERR_print_errors_fp(stderr); } } else { fprintf(stderr, "Warning: %s compression not supported\n", comp == COMP_ZLIB ? "zlib" : "unknown"); ERR_print_errors_fp(stderr); } ssl_comp_methods = SSL_COMP_get_compression_methods(); n = sk_SSL_COMP_num(ssl_comp_methods); if (n) { int j; printf("Available compression methods:"); for (j = 0; j < n; j++) { SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j); printf(" %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c)); } printf("\n"); } #endif #ifndef OPENSSL_NO_TLS meth = TLS_method(); if (ssl3) { min_version = SSL3_VERSION; max_version = SSL3_VERSION; } else if (tls1) { min_version = TLS1_VERSION; max_version = TLS1_VERSION; } else if (tls1_1) { min_version = TLS1_1_VERSION; max_version = TLS1_1_VERSION; } else if (tls1_2) { min_version = TLS1_2_VERSION; max_version = TLS1_2_VERSION; } else { min_version = 0; # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH) /* We only have ec and dh based built-in groups for TLSv1.3 */ max_version = TLS1_2_VERSION; # else max_version = 0; # endif } #endif #ifndef OPENSSL_NO_DTLS if (dtls || dtls1 || dtls12) { meth = DTLS_method(); if (dtls1) { min_version = DTLS1_VERSION; max_version = DTLS1_VERSION; } else if (dtls12) { min_version = DTLS1_2_VERSION; max_version = DTLS1_2_VERSION; } else { min_version = 0; max_version = 0; } } #endif if (provider != NULL && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider)) goto end; c_ctx = SSL_CTX_new_ex(libctx, NULL, meth); s_ctx = SSL_CTX_new_ex(libctx, NULL, meth); s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */ if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) { ERR_print_errors(bio_err); goto end; } /* * Since we will use low security ciphersuites and keys for testing set * security level to zero by default. Tests can override this by adding * "@SECLEVEL=n" to the cipher string. */ SSL_CTX_set_security_level(c_ctx, 0); SSL_CTX_set_security_level(s_ctx, 0); SSL_CTX_set_security_level(s_ctx2, 0); if (no_ticket) { SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET); SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET); } if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0) goto end; if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0) goto end; if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0) goto end; if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0) goto end; if (cipher != NULL) { if (strcmp(cipher, "") == 0) { if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { /* Should have failed when clearing all TLSv1.2 ciphers. */ fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { /* Should have failed when clearing all TLSv1.2 ciphers. */ fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { /* Should have failed when clearing all TLSv1.2 ciphers. */ fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } } else { if (!SSL_CTX_set_cipher_list(c_ctx, cipher) || !SSL_CTX_set_cipher_list(s_ctx, cipher) || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) { ERR_print_errors(bio_err); goto end; } } } if (ciphersuites != NULL) { if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites) || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites) || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) { ERR_print_errors(bio_err); goto end; } } #ifndef OPENSSL_NO_CT if (ct_validation && !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) { ERR_print_errors(bio_err); goto end; } #endif /* Process SSL_CONF arguments */ SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx); SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx); SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2); for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) { int rv; arg = sk_OPENSSL_STRING_value(conf_args, i); argn = sk_OPENSSL_STRING_value(conf_args, i + 1); rv = SSL_CONF_cmd(c_cctx, arg, argn); /* If not recognised use server context */ if (rv == -2) { rv = SSL_CONF_cmd(s_cctx2, arg, argn); if (rv > 0) rv = SSL_CONF_cmd(s_cctx, arg, argn); } if (rv <= 0) { BIO_printf(bio_err, "Error processing %s %s\n", arg, argn ? argn : ""); ERR_print_errors(bio_err); goto end; } } if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) { BIO_puts(bio_err, "Error finishing context\n"); ERR_print_errors(bio_err); goto end; } #ifndef OPENSSL_NO_DH if (!no_dhe) { if (dhe1024dsa) dhpkey = get_dh1024dsa(libctx); else if (dhe512) dhpkey = get_dh512(libctx); else if (dhe4096) dhpkey = get_dh4096(libctx); else dhpkey = get_dh2048(libctx); if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) { EVP_PKEY_free(dhpkey); BIO_puts(bio_err, "Error getting DH parameters\n"); ERR_print_errors(bio_err); goto end; } if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey)) EVP_PKEY_free(dhpkey); if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey)) EVP_PKEY_free(dhpkey); } #endif if (!(SSL_CTX_load_verify_file(s_ctx, CAfile) || SSL_CTX_load_verify_dir(s_ctx, CApath)) || !SSL_CTX_set_default_verify_paths(s_ctx) || !(SSL_CTX_load_verify_file(s_ctx2, CAfile) || SSL_CTX_load_verify_dir(s_ctx2, CApath)) || !SSL_CTX_set_default_verify_paths(s_ctx2) || !(SSL_CTX_load_verify_file(c_ctx, CAfile) || SSL_CTX_load_verify_dir(c_ctx, CApath)) || !SSL_CTX_set_default_verify_paths(c_ctx)) { ERR_print_errors(bio_err); } #ifndef OPENSSL_NO_CT if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) || !SSL_CTX_set_default_ctlog_list_file(s_ctx2) || !SSL_CTX_set_default_ctlog_list_file(c_ctx)) { ERR_print_errors(bio_err); } #endif if (client_auth) { printf("client authentication\n"); SSL_CTX_set_verify(s_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback); SSL_CTX_set_verify(s_ctx2, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback); SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg); SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback, &app_verify_arg); } if (server_auth) { printf("server authentication\n"); SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback); SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg); } { int session_id_context = 0; if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof(session_id_context)) || !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context, sizeof(session_id_context))) { ERR_print_errors(bio_err); goto end; } } /* Use PSK only if PSK key is given */ if (psk_key != NULL) { /* * no_psk is used to avoid putting psk command to openssl tool */ if (no_psk) { /* * if PSK is not compiled in and psk key is given, do nothing and * exit successfully */ ret = EXIT_SUCCESS; goto end; } #ifndef OPENSSL_NO_PSK SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback); SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback); SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback); if (debug) BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n"); if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") || !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) { BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n"); ERR_print_errors(bio_err); goto end; } #endif } #ifndef OPENSSL_NO_NEXTPROTONEG if (npn_client) { SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL); } if (npn_server) { if (npn_server_reject) { BIO_printf(bio_err, "Can't have both -npn_server and -npn_server_reject\n"); goto end; } SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL); SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL); } if (npn_server_reject) { SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL); SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL); } #endif if (serverinfo_sct) { if (!SSL_CTX_add_client_custom_ext(c_ctx, TLSEXT_TYPE_signed_certificate_timestamp, NULL, NULL, NULL, serverinfo_cli_parse_cb, NULL)) { BIO_printf(bio_err, "Error adding SCT extension\n"); goto end; } } if (serverinfo_tack) { if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE, NULL, NULL, NULL, serverinfo_cli_parse_cb, NULL)) { BIO_printf(bio_err, "Error adding TACK extension\n"); goto end; } } if (serverinfo_file) if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) || !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) { BIO_printf(bio_err, "missing serverinfo file\n"); goto end; } if (custom_ext) { if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0, custom_ext_0_cli_add_cb, NULL, NULL, custom_ext_0_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1, custom_ext_1_cli_add_cb, NULL, NULL, custom_ext_1_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2, custom_ext_2_cli_add_cb, NULL, NULL, custom_ext_2_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3, custom_ext_3_cli_add_cb, NULL, NULL, custom_ext_3_cli_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0, custom_ext_0_srv_add_cb, NULL, NULL, custom_ext_0_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0, custom_ext_0_srv_add_cb, NULL, NULL, custom_ext_0_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1, custom_ext_1_srv_add_cb, NULL, NULL, custom_ext_1_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1, custom_ext_1_srv_add_cb, NULL, NULL, custom_ext_1_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2, custom_ext_2_srv_add_cb, NULL, NULL, custom_ext_2_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2, custom_ext_2_srv_add_cb, NULL, NULL, custom_ext_2_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3, custom_ext_3_srv_add_cb, NULL, NULL, custom_ext_3_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3, custom_ext_3_srv_add_cb, NULL, NULL, custom_ext_3_srv_parse_cb, NULL)) { BIO_printf(bio_err, "Error setting custom extensions\n"); goto end; } } if (alpn_server) SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server); if (alpn_server2) SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2); if (alpn_client) { size_t alpn_len; unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client); if (alpn == NULL) { BIO_printf(bio_err, "Error parsing -alpn_client argument\n"); goto end; } /* Returns 0 on success!! */ if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) { BIO_printf(bio_err, "Error setting ALPN\n"); OPENSSL_free(alpn); goto end; } OPENSSL_free(alpn); } if (server_sess_in != NULL) { server_sess = read_session(server_sess_in); if (server_sess == NULL) goto end; } if (client_sess_in != NULL) { client_sess = read_session(client_sess_in); if (client_sess == NULL) goto end; } if (server_sess_out != NULL || server_sess_in != NULL) { char *keys; long size; /* Use a fixed key so that we can decrypt the ticket. */ size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0); keys = OPENSSL_zalloc(size); if (keys == NULL) goto end; SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size); OPENSSL_free(keys); } if (sn_server1 != NULL || sn_server2 != NULL) SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb); c_ssl = SSL_new(c_ctx); s_ssl = SSL_new(s_ctx); if (c_ssl == NULL || s_ssl == NULL) goto end; if (sn_client) SSL_set_tlsext_host_name(c_ssl, sn_client); if (client_ktls) SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS); if (server_ktls) SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS); if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION)) goto end; if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION)) goto end; if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION)) goto end; if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION)) goto end; if (server_sess) { if (SSL_CTX_add_session(s_ctx, server_sess) == 0) { BIO_printf(bio_err, "Can't add server session\n"); ERR_print_errors(bio_err); goto end; } } BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes); for (i = 0; i < number; i++) { if (!reuse) { if (!SSL_set_session(c_ssl, NULL)) { BIO_printf(bio_err, "Failed to set session\n"); goto end; } } if (client_sess_in != NULL) { if (SSL_set_session(c_ssl, client_sess) == 0) { BIO_printf(bio_err, "Can't set client session\n"); ERR_print_errors(bio_err); goto end; } } switch (bio_type) { case BIO_MEM: ret = doit(s_ssl, c_ssl, bytes); break; case BIO_PAIR: ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time); break; #ifndef OPENSSL_NO_SOCK case BIO_IPV4: ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4, bytes, &s_time, &c_time); break; case BIO_IPV6: ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6, bytes, &s_time, &c_time); break; #else case BIO_IPV4: case BIO_IPV6: ret = EXIT_FAILURE; goto end; #endif } if (ret != EXIT_SUCCESS) break; } if (should_negotiate && ret == EXIT_SUCCESS && strcmp(should_negotiate, "fail-server") != 0 && strcmp(should_negotiate, "fail-client") != 0) { int version = protocol_from_string(should_negotiate); if (version < 0) { BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate); ret = EXIT_FAILURE; goto end; } if (SSL_version(c_ssl) != version) { BIO_printf(bio_err, "Unexpected version negotiated. " "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl)); ret = EXIT_FAILURE; goto end; } } if (should_reuse != -1) { if (SSL_session_reused(s_ssl) != should_reuse || SSL_session_reused(c_ssl) != should_reuse) { BIO_printf(bio_err, "Unexpected session reuse state. " "Expected: %d, server: %d, client: %d\n", should_reuse, SSL_session_reused(s_ssl), SSL_session_reused(c_ssl)); ret = EXIT_FAILURE; goto end; } } if (server_sess_out != NULL) { if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) { ret = EXIT_FAILURE; goto end; } } if (client_sess_out != NULL) { if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) { ret = EXIT_FAILURE; goto end; } } if (!verbose) { print_details(c_ssl, ""); } if (print_time) { #ifdef CLOCKS_PER_SEC /* * "To determine the time in seconds, the value returned by the clock * function should be divided by the value of the macro * CLOCKS_PER_SEC." -- ISO/IEC 9899 */ BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n" "Approximate total client time: %6.2f s\n", (double)s_time / CLOCKS_PER_SEC, (double)c_time / CLOCKS_PER_SEC); #else BIO_printf(bio_stdout, "Approximate total server time: %6.2f units\n" "Approximate total client time: %6.2f units\n", (double)s_time, (double)c_time); #endif } end: SSL_free(s_ssl); SSL_free(c_ssl); SSL_CTX_free(s_ctx); SSL_CTX_free(s_ctx2); SSL_CTX_free(c_ctx); SSL_CONF_CTX_free(s_cctx); SSL_CONF_CTX_free(s_cctx2); SSL_CONF_CTX_free(c_cctx); sk_OPENSSL_STRING_free(conf_args); BIO_free(bio_stdout); SSL_SESSION_free(server_sess); SSL_SESSION_free(client_sess); OSSL_PROVIDER_unload(defctxnull); OSSL_PROVIDER_unload(thisprov); OSSL_LIB_CTX_free(libctx); test_close_streams(); EXIT(ret); } #ifndef OPENSSL_NO_SOCK int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count, clock_t *s_time, clock_t *c_time) { long cw_num = count, cr_num = count, sw_num = count, sr_num = count; BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; BIO *acpt = NULL, *server = NULL, *client = NULL; char addr_str[40]; int ret = EXIT_FAILURE; int err_in_client = 0; int err_in_server = 0; acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0" : "[::1]:0"); if (acpt == NULL) goto err; BIO_set_accept_ip_family(acpt, family); BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR); if (BIO_do_accept(acpt) <= 0) goto err; BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt)); client = BIO_new_connect(addr_str); if (!client) goto err; BIO_set_conn_ip_family(client, family); if (BIO_set_nbio(client, 1) <= 0) goto err; if (BIO_set_nbio(acpt, 1) <= 0) goto err; { int st_connect = 0, st_accept = 0; while (!st_connect || !st_accept) { if (!st_connect) { if (BIO_do_connect(client) <= 0) { if (!BIO_should_retry(client)) goto err; } else { st_connect = 1; } } if (!st_accept) { if (BIO_do_accept(acpt) <= 0) { if (!BIO_should_retry(acpt)) goto err; } else { st_accept = 1; } } } } /* We're not interested in accepting further connects */ server = BIO_pop(acpt); BIO_free_all(acpt); acpt = NULL; s_ssl_bio = BIO_new(BIO_f_ssl()); if (!s_ssl_bio) goto err; c_ssl_bio = BIO_new(BIO_f_ssl()); if (!c_ssl_bio) goto err; SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, client, client); (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, server, server); (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); do { /*- * c_ssl_bio: SSL filter BIO * * client: I/O for SSL library * * * server: I/O for SSL library * * s_ssl_bio: SSL filter BIO */ /* * We have non-blocking behaviour throughout this test program, but * can be sure that there is *some* progress in each iteration; so we * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE -- * we just try everything in each iteration */ { /* CLIENT */ char cbuf[1024 * 8]; int i, r; clock_t c_clock = clock(); memset(cbuf, 0, sizeof(cbuf)); if (debug) if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); if (cw_num > 0) { /* Write to server. */ if (cw_num > (long)sizeof(cbuf)) i = sizeof(cbuf); else i = (int)cw_num; r = BIO_write(c_ssl_bio, cbuf, i); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT (write)\n"); err_in_client = 1; goto err; } /* * BIO_should_retry(...) can just be ignored here. The * library expects us to call BIO_write with the same * arguments again, and that's what we will do in the * next iteration. */ } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", r); cw_num -= r; } } if (cr_num > 0) { /* Read from server. */ r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT (read)\n"); err_in_client = 1; goto err; } /* * Again, "BIO_should_retry" can be ignored. */ } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", r); cr_num -= r; } } /* * c_time and s_time increments will typically be very small * (depending on machine speed and clock tick intervals), but * sampling over a large number of connections should result in * fairly accurate figures. We cannot guarantee a lot, however * -- if each connection lasts for exactly one clock tick, it * will be counted only for the client or only for the server or * even not at all. */ *c_time += (clock() - c_clock); } { /* SERVER */ char sbuf[1024 * 8]; int i, r; clock_t s_clock = clock(); memset(sbuf, 0, sizeof(sbuf)); if (debug) if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); if (sw_num > 0) { /* Write to client. */ if (sw_num > (long)sizeof(sbuf)) i = sizeof(sbuf); else i = (int)sw_num; r = BIO_write(s_ssl_bio, sbuf, i); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER (write)\n"); err_in_server = 1; goto err; } /* Ignore "BIO_should_retry". */ } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server wrote %d\n", r); sw_num -= r; } } if (sr_num > 0) { /* Read from client. */ r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER (read)\n"); err_in_server = 1; goto err; } /* blah, blah */ } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server read %d\n", r); sr_num -= r; } } *s_time += (clock() - s_clock); } } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); if (verbose) { print_details(c_ssl, "DONE via TCP connect: "); if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)) && BIO_get_ktls_recv(SSL_get_rbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n"); else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n"); else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n"); if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)) && BIO_get_ktls_recv(SSL_get_rbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n"); else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n"); else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n"); } # ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto end; # endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (verify_alpn(c_ssl, s_ssl) < 0 || verify_servername(c_ssl, s_ssl) < 0) goto err; if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } # ifndef OPENSSL_NO_NEXTPROTONEG end: # endif ret = EXIT_SUCCESS; err: ERR_print_errors(bio_err); BIO_free_all(acpt); BIO_free(server); BIO_free(client); BIO_free(s_ssl_bio); BIO_free(c_ssl_bio); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } #endif int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count, clock_t *s_time, clock_t *c_time) { long cw_num = count, cr_num = count, sw_num = count, sr_num = count; BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL; int ret = EXIT_FAILURE; int err_in_client = 0; int err_in_server = 0; size_t bufsiz = 256; /* small buffer for testing */ if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz)) goto err; if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz)) goto err; s_ssl_bio = BIO_new(BIO_f_ssl()); if (!s_ssl_bio) goto err; c_ssl_bio = BIO_new(BIO_f_ssl()); if (!c_ssl_bio) goto err; SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, client, client); (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, server, server); (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); do { /*- * c_ssl_bio: SSL filter BIO * * client: pseudo-I/O for SSL library * * client_io: client's SSL communication; usually to be * relayed over some I/O facility, but in this * test program, we're the server, too: * * server_io: server's SSL communication * * server: pseudo-I/O for SSL library * * s_ssl_bio: SSL filter BIO * * The client and the server each employ a "BIO pair": * client + client_io, server + server_io. * BIO pairs are symmetric. A BIO pair behaves similar * to a non-blocking socketpair (but both endpoints must * be handled by the same thread). * [Here we could connect client and server to the ends * of a single BIO pair, but then this code would be less * suitable as an example for BIO pairs in general.] * * Useful functions for querying the state of BIO pair endpoints: * * BIO_ctrl_pending(bio) number of bytes we can read now * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill * other side's read attempt * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now * * ..._read_request is never more than ..._write_guarantee; * it depends on the application which one you should use. */ /* * We have non-blocking behaviour throughout this test program, but * can be sure that there is *some* progress in each iteration; so we * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE -- * we just try everything in each iteration */ { /* CLIENT */ char cbuf[1024 * 8]; int i, r; clock_t c_clock = clock(); memset(cbuf, 0, sizeof(cbuf)); if (debug) if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); if (cw_num > 0) { /* Write to server. */ if (cw_num > (long)sizeof(cbuf)) i = sizeof(cbuf); else i = (int)cw_num; r = BIO_write(c_ssl_bio, cbuf, i); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; goto err; } /* * BIO_should_retry(...) can just be ignored here. The * library expects us to call BIO_write with the same * arguments again, and that's what we will do in the * next iteration. */ } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", r); cw_num -= r; } } if (cr_num > 0) { /* Read from server. */ r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; goto err; } /* * Again, "BIO_should_retry" can be ignored. */ } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", r); cr_num -= r; } } /* * c_time and s_time increments will typically be very small * (depending on machine speed and clock tick intervals), but * sampling over a large number of connections should result in * fairly accurate figures. We cannot guarantee a lot, however * -- if each connection lasts for exactly one clock tick, it * will be counted only for the client or only for the server or * even not at all. */ *c_time += (clock() - c_clock); } { /* SERVER */ char sbuf[1024 * 8]; int i, r; clock_t s_clock = clock(); memset(sbuf, 0, sizeof(sbuf)); if (debug) if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); if (sw_num > 0) { /* Write to client. */ if (sw_num > (long)sizeof(sbuf)) i = sizeof(sbuf); else i = (int)sw_num; r = BIO_write(s_ssl_bio, sbuf, i); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; goto err; } /* Ignore "BIO_should_retry". */ } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server wrote %d\n", r); sw_num -= r; } } if (sr_num > 0) { /* Read from client. */ r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; goto err; } /* blah, blah */ } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server read %d\n", r); sr_num -= r; } } *s_time += (clock() - s_clock); } { /* "I/O" BETWEEN CLIENT AND SERVER. */ size_t r1, r2; BIO *io1 = server_io, *io2 = client_io; /* * we use the non-copying interface for io1 and the standard * BIO_write/BIO_read interface for io2 */ static int prev_progress = 1; int progress = 0; /* io1 to io2 */ do { size_t num; int r; r1 = BIO_ctrl_pending(io1); r2 = BIO_ctrl_get_write_guarantee(io2); num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) /* yeah, right */ num = INT_MAX; r = BIO_nread(io1, &dataptr, (int)num); assert(r > 0); assert(r <= (int)num); /* * possibly r < num (non-contiguous data) */ num = r; r = BIO_write(io2, dataptr, (int)num); if (r != (int)num) { /* can't happen */ fprintf(stderr, "ERROR: BIO_write could not write " "BIO_ctrl_get_write_guarantee() bytes"); goto err; } progress = 1; if (debug) printf((io1 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } while (r1 && r2); /* io2 to io1 */ { size_t num; int r; r1 = BIO_ctrl_pending(io2); r2 = BIO_ctrl_get_read_request(io1); /* * here we could use ..._get_write_guarantee instead of * ..._get_read_request, but by using the latter we test * restartability of the SSL implementation more thoroughly */ num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) num = INT_MAX; if (num > 1) --num; /* test restartability even more thoroughly */ r = BIO_nwrite0(io1, &dataptr); assert(r > 0); if (r < (int)num) num = r; r = BIO_read(io2, dataptr, (int)num); if (r != (int)num) { /* can't happen */ fprintf(stderr, "ERROR: BIO_read could not read " "BIO_ctrl_pending() bytes"); goto err; } progress = 1; r = BIO_nwrite(io1, &dataptr, (int)num); if (r != (int)num) { /* can't happen */ fprintf(stderr, "ERROR: BIO_nwrite() did not accept " "BIO_nwrite0() bytes"); goto err; } if (debug) printf((io2 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } /* no loop, BIO_ctrl_get_read_request now * returns 0 anyway */ if (!progress && !prev_progress) if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) { fprintf(stderr, "ERROR: got stuck\n"); fprintf(stderr, " ERROR.\n"); goto err; } prev_progress = progress; } } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); if (verbose) print_details(c_ssl, "DONE via BIO pair: "); #ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto end; #endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (verify_alpn(c_ssl, s_ssl) < 0 || verify_servername(c_ssl, s_ssl) < 0) goto err; if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } #ifndef OPENSSL_NO_NEXTPROTONEG end: #endif ret = EXIT_SUCCESS; err: ERR_print_errors(bio_err); BIO_free(server); BIO_free(server_io); BIO_free(client); BIO_free(client_io); BIO_free(s_ssl_bio); BIO_free(c_ssl_bio); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } #define W_READ 1 #define W_WRITE 2 #define C_DONE 1 #define S_DONE 2 int doit(SSL *s_ssl, SSL *c_ssl, long count) { char *cbuf = NULL, *sbuf = NULL; long bufsiz; long cw_num = count, cr_num = count; long sw_num = count, sr_num = count; int ret = EXIT_FAILURE; BIO *c_to_s = NULL; BIO *s_to_c = NULL; BIO *c_bio = NULL; BIO *s_bio = NULL; int c_r, c_w, s_r, s_w; int i, j; int done = 0; int c_write, s_write; int do_server = 0, do_client = 0; int max_frag = 5 * 1024; int err_in_client = 0; int err_in_server = 0; bufsiz = count > 40 * 1024 ? 40 * 1024 : count; if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL) goto err; if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL) goto err; c_to_s = BIO_new(BIO_s_mem()); s_to_c = BIO_new(BIO_s_mem()); if ((s_to_c == NULL) || (c_to_s == NULL)) { ERR_print_errors(bio_err); goto err; } c_bio = BIO_new(BIO_f_ssl()); s_bio = BIO_new(BIO_f_ssl()); if ((c_bio == NULL) || (s_bio == NULL)) { ERR_print_errors(bio_err); goto err; } SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, s_to_c, c_to_s); SSL_set_max_send_fragment(c_ssl, max_frag); BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE); /* * We've just given our ref to these BIOs to c_ssl. We need another one to * give to s_ssl */ if (!BIO_up_ref(c_to_s)) { /* c_to_s and s_to_c will get freed when we free c_ssl */ c_to_s = NULL; s_to_c = NULL; goto err; } if (!BIO_up_ref(s_to_c)) { /* s_to_c will get freed when we free c_ssl */ s_to_c = NULL; goto err; } SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, c_to_s, s_to_c); /* We've used up all our refs to these now */ c_to_s = NULL; s_to_c = NULL; SSL_set_max_send_fragment(s_ssl, max_frag); BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE); c_r = 0; s_r = 1; c_w = 1; s_w = 0; c_write = 1, s_write = 0; /* We can always do writes */ for (;;) { do_server = 0; do_client = 0; i = (int)BIO_pending(s_bio); if ((i && s_r) || s_w) do_server = 1; i = (int)BIO_pending(c_bio); if ((i && c_r) || c_w) do_client = 1; if (do_server && debug) { if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); } if (do_client && debug) { if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); } if (!do_client && !do_server) { fprintf(stdout, "ERROR IN STARTUP\n"); ERR_print_errors(bio_err); goto err; } if (do_client && !(done & C_DONE)) { if (c_write) { j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num; i = BIO_write(c_bio, cbuf, j); if (i < 0) { c_r = 0; c_w = 0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r = 1; if (BIO_should_write(c_bio)) c_w = 1; } else { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", i); /* ok */ s_r = 1; c_write = 0; cw_num -= i; if (max_frag > 1029) SSL_set_max_send_fragment(c_ssl, max_frag -= 5); } } else { i = BIO_read(c_bio, cbuf, bufsiz); if (i < 0) { c_r = 0; c_w = 0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r = 1; if (BIO_should_write(c_bio)) c_w = 1; } else { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", i); cr_num -= i; if (sw_num > 0) { s_write = 1; s_w = 1; } if (cr_num <= 0) { s_write = 1; s_w = 1; done = S_DONE | C_DONE; } } } } if (do_server && !(done & S_DONE)) { if (!s_write) { i = BIO_read(s_bio, sbuf, bufsiz); if (i < 0) { s_r = 0; s_w = 0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r = 1; if (BIO_should_write(s_bio)) s_w = 1; } else { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_read\n"); goto err; } else { if (debug) printf("server read %d\n", i); sr_num -= i; if (cw_num > 0) { c_write = 1; c_w = 1; } if (sr_num <= 0) { s_write = 1; s_w = 1; c_write = 0; } } } else { j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num; i = BIO_write(s_bio, sbuf, j); if (i < 0) { s_r = 0; s_w = 0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r = 1; if (BIO_should_write(s_bio)) s_w = 1; } else { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_write\n"); goto err; } else { if (debug) printf("server wrote %d\n", i); sw_num -= i; s_write = 0; c_r = 1; if (sw_num <= 0) done |= S_DONE; if (max_frag > 1029) SSL_set_max_send_fragment(s_ssl, max_frag -= 5); } } } if ((done & S_DONE) && (done & C_DONE)) break; } if (verbose) print_details(c_ssl, "DONE: "); #ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto err; #endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } ret = EXIT_SUCCESS; err: BIO_free(c_to_s); BIO_free(s_to_c); BIO_free_all(c_bio); BIO_free_all(s_bio); OPENSSL_free(cbuf); OPENSSL_free(sbuf); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } static int verify_callback(int ok, X509_STORE_CTX *ctx) { char *s, buf[256]; s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)), buf, sizeof(buf)); if (s != NULL) { if (ok) printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf); else { fprintf(stderr, "depth=%d error=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), X509_STORE_CTX_get_error(ctx), buf); } } if (ok == 0) { int i = X509_STORE_CTX_get_error(ctx); switch (i) { default: fprintf(stderr, "Error string: %s\n", X509_verify_cert_error_string(i)); break; case X509_V_ERR_CERT_NOT_YET_VALID: case X509_V_ERR_CERT_HAS_EXPIRED: case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: ok = 1; break; } } return ok; } static int app_verify_callback(X509_STORE_CTX *ctx, void *arg) { int ok = 1; struct app_verify_arg *cb_arg = arg; if (cb_arg->app_verify) { char *s = NULL, buf[256]; X509 *c = X509_STORE_CTX_get0_cert(ctx); printf("In app_verify_callback, allowing cert. "); printf("Arg is: %s\n", cb_arg->string); printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n", (void *)ctx, (void *)c); if (c) s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256); if (s != NULL) { printf("cert depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf); } return 1; } ok = X509_verify_cert(ctx); return ok; } #ifndef OPENSSL_NO_PSK /* convert the PSK key (psk_key) in ascii to binary (psk) */ static int psk_key2bn(const char *pskkey, unsigned char *psk, unsigned int max_psk_len) { int ret; BIGNUM *bn = NULL; ret = BN_hex2bn(&bn, pskkey); if (!ret) { BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", pskkey); BN_free(bn); return 0; } if (BN_num_bytes(bn) > (int)max_psk_len) { BIO_printf(bio_err, "psk buffer of callback is too small (%d) for key (%d)\n", max_psk_len, BN_num_bytes(bn)); BN_free(bn); return 0; } ret = BN_bn2bin(bn, psk); BN_free(bn); return ret; } static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len) { int ret; unsigned int psk_len = 0; ret = BIO_snprintf(identity, max_identity_len, "Client_identity"); if (ret < 0) goto out_err; if (debug) fprintf(stderr, "client: created identity '%s' len=%d\n", identity, ret); ret = psk_key2bn(psk_key, psk, max_psk_len); if (ret < 0) goto out_err; psk_len = ret; out_err: return psk_len; } static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { unsigned int psk_len = 0; if (strcmp(identity, "Client_identity") != 0) { BIO_printf(bio_err, "server: PSK error: client identity not found\n"); return 0; } psk_len = psk_key2bn(psk_key, psk, max_psk_len); return psk_len; } #endif
./openssl/test/quic_record_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "internal/quic_record_rx.h" #include "internal/quic_rx_depack.h" #include "internal/quic_record_tx.h" #include "internal/quic_ackm.h" #include "internal/quic_cc.h" #include "internal/quic_ssl.h" #include "testutil.h" #include "quic_record_test_util.h" static const QUIC_CONN_ID empty_conn_id = {0, {0}}; #define RX_TEST_OP_END 0 /* end of script */ #define RX_TEST_OP_SET_SCID_LEN 1 /* change SCID length */ #define RX_TEST_OP_SET_INIT_LARGEST_PN 2 /* set initial largest PN */ #define RX_TEST_OP_SET_RX_DCID 3 /* register an RX DCID */ #define RX_TEST_OP_INJECT 4 /* inject a datagram into demux */ #define RX_TEST_OP_PROVIDE_SECRET 5 /* provide RX secret */ #define RX_TEST_OP_PROVIDE_SECRET_INITIAL 6 /* provide RX secret for initial */ #define RX_TEST_OP_DISCARD_EL 7 /* discard an encryption level */ #define RX_TEST_OP_CHECK_PKT 8 /* read packet, compare to expected */ #define RX_TEST_OP_CHECK_NO_PKT 9 /* check no packet is available to read */ #define RX_TEST_OP_CHECK_KEY_EPOCH 10 /* check key epoch value matches */ #define RX_TEST_OP_KEY_UPDATE_TIMEOUT 11 /* complete key update process */ #define RX_TEST_OP_SET_INIT_KEY_PHASE 12 /* initial Key Phase bit value */ #define RX_TEST_OP_CHECK_PKT_EPOCH 13 /* check read key epoch matches */ #define RX_TEST_OP_ALLOW_1RTT 14 /* allow 1RTT packet processing */ struct rx_test_op { unsigned char op; unsigned char subop; const unsigned char *buf; size_t buf_len; const QUIC_PKT_HDR *hdr; uint32_t enc_level, suite_id; QUIC_PN largest_pn; const QUIC_CONN_ID *dcid; int (*new_qrx)(QUIC_DEMUX **demux, OSSL_QRX **qrx); /* For frame checking */ }; #define RX_OP_END \ { RX_TEST_OP_END } #define RX_OP_SET_SCID_LEN(scid_len) \ { RX_TEST_OP_SET_SCID_LEN, 0, NULL, 0, NULL, (scid_len), 0, 0, NULL, NULL }, #define RX_OP_SET_INIT_LARGEST_PN(largest_pn) \ { RX_TEST_OP_SET_INIT_LARGEST_PN, 0, NULL, 0, NULL, 0, 0, (largest_pn), NULL, NULL }, #define RX_OP_SET_RX_DCID(dcid) \ { RX_TEST_OP_SET_RX_DCID, 0, NULL, 0, NULL, 0, 0, 0, &(dcid), NULL }, #define RX_OP_INJECT(dgram) \ { RX_TEST_OP_INJECT, 0, (dgram), sizeof(dgram), NULL, 0, 0, 0, NULL }, #define RX_OP_PROVIDE_SECRET(el, suite, key) \ { \ RX_TEST_OP_PROVIDE_SECRET, 0, (key), sizeof(key), \ NULL, (el), (suite), 0, NULL, NULL \ }, #define RX_OP_PROVIDE_SECRET_INITIAL(dcid) \ { RX_TEST_OP_PROVIDE_SECRET_INITIAL, 0, NULL, 0, NULL, 0, 0, 0, &(dcid), NULL }, #define RX_OP_DISCARD_EL(el) \ { RX_TEST_OP_DISCARD_EL, 0, NULL, 0, NULL, (el), 0, 0, NULL, NULL }, #define RX_OP_CHECK_PKT(expect_hdr, expect_body) \ { \ RX_TEST_OP_CHECK_PKT, 0, (expect_body), sizeof(expect_body), \ &(expect_hdr), 0, 0, 0, NULL, NULL \ }, #define RX_OP_CHECK_NO_PKT() \ { RX_TEST_OP_CHECK_NO_PKT, 0, NULL, 0, NULL, 0, 0, 0, NULL, NULL }, #define RX_OP_CHECK_KEY_EPOCH(expected) \ { RX_TEST_OP_CHECK_KEY_EPOCH, 0, NULL, 0, NULL, 0, 0, (expected), NULL }, #define RX_OP_KEY_UPDATE_TIMEOUT(normal) \ { RX_TEST_OP_KEY_UPDATE_TIMEOUT, 0, NULL, 0, NULL, (normal), 0, 0, NULL }, #define RX_OP_SET_INIT_KEY_PHASE(kp_bit) \ { RX_TEST_OP_SET_INIT_KEY_PHASE, 0, NULL, 0, NULL, (kp_bit), 0, 0, NULL }, #define RX_OP_CHECK_PKT_EPOCH(expected) \ { RX_TEST_OP_CHECK_PKT_EPOCH, 0, NULL, 0, NULL, 0, 0, (expected), NULL }, #define RX_OP_ALLOW_1RTT() \ { RX_TEST_OP_ALLOW_1RTT, 0, NULL, 0, NULL, 0, 0, 0, NULL }, #define RX_OP_INJECT_N(n) \ RX_OP_INJECT(rx_script_##n##_in) #define RX_OP_CHECK_PKT_N(n) \ RX_OP_CHECK_PKT(rx_script_##n##_expect_hdr, rx_script_##n##_body) #define RX_OP_INJECT_CHECK(n) \ RX_OP_INJECT_N(n) \ RX_OP_CHECK_PKT_N(n) /* 1. RFC 9001 - A.3 Server Initial */ static const unsigned char rx_script_1_in[] = { 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x40, 0x75, 0xc0, 0xd9, 0x5a, 0x48, 0x2c, 0xd0, 0x99, 0x1c, 0xd2, 0x5b, 0x0a, 0xac, 0x40, 0x6a, 0x58, 0x16, 0xb6, 0x39, 0x41, 0x00, 0xf3, 0x7a, 0x1c, 0x69, 0x79, 0x75, 0x54, 0x78, 0x0b, 0xb3, 0x8c, 0xc5, 0xa9, 0x9f, 0x5e, 0xde, 0x4c, 0xf7, 0x3c, 0x3e, 0xc2, 0x49, 0x3a, 0x18, 0x39, 0xb3, 0xdb, 0xcb, 0xa3, 0xf6, 0xea, 0x46, 0xc5, 0xb7, 0x68, 0x4d, 0xf3, 0x54, 0x8e, 0x7d, 0xde, 0xb9, 0xc3, 0xbf, 0x9c, 0x73, 0xcc, 0x3f, 0x3b, 0xde, 0xd7, 0x4b, 0x56, 0x2b, 0xfb, 0x19, 0xfb, 0x84, 0x02, 0x2f, 0x8e, 0xf4, 0xcd, 0xd9, 0x37, 0x95, 0xd7, 0x7d, 0x06, 0xed, 0xbb, 0x7a, 0xaf, 0x2f, 0x58, 0x89, 0x18, 0x50, 0xab, 0xbd, 0xca, 0x3d, 0x20, 0x39, 0x8c, 0x27, 0x64, 0x56, 0xcb, 0xc4, 0x21, 0x58, 0x40, 0x7d, 0xd0, 0x74, 0xee }; static const unsigned char rx_script_1_body[] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xee, 0xfc, 0xe7, 0xf7, 0xb3, 0x7b, 0xa1, 0xd1, 0x63, 0x2e, 0x96, 0x67, 0x78, 0x25, 0xdd, 0xf7, 0x39, 0x88, 0xcf, 0xc7, 0x98, 0x25, 0xdf, 0x56, 0x6d, 0xc5, 0x43, 0x0b, 0x9a, 0x04, 0x5a, 0x12, 0x00, 0x13, 0x01, 0x00, 0x00, 0x2e, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x9d, 0x3c, 0x94, 0x0d, 0x89, 0x69, 0x0b, 0x84, 0xd0, 0x8a, 0x60, 0x99, 0x3c, 0x14, 0x4e, 0xca, 0x68, 0x4d, 0x10, 0x81, 0x28, 0x7c, 0x83, 0x4d, 0x53, 0x11, 0xbc, 0xf3, 0x2b, 0xb9, 0xda, 0x1a, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04 }; static const QUIC_CONN_ID rx_script_1_dcid = { 8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 } }; static const QUIC_PKT_HDR rx_script_1_expect_hdr = { QUIC_PKT_TYPE_INITIAL, 0, 0, 2, 0, 1, 0, 0, 1, { 0, {0} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0, 1, 0, 0 }, NULL, 0, 99, NULL }; static const struct rx_test_op rx_script_1[] = { RX_OP_SET_SCID_LEN(2) RX_OP_SET_INIT_LARGEST_PN(0) RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET_INITIAL(rx_script_1_dcid) RX_OP_INJECT_CHECK(1) RX_OP_CHECK_NO_PKT() RX_OP_END }; /* 2. RFC 9001 - A.5 ChaCha20-Poly1305 Short Header Packet */ #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) static const unsigned char rx_script_2_in[] = { 0x4c, 0xfe, 0x41, 0x89, 0x65, 0x5e, 0x5c, 0xd5, 0x5c, 0x41, 0xf6, 0x90, 0x80, 0x57, 0x5d, 0x79, 0x99, 0xc2, 0x5a, 0x5b, 0xfb }; static const unsigned char rx_script_2_secret[] = { 0x9a, 0xc3, 0x12, 0xa7, 0xf8, 0x77, 0x46, 0x8e, 0xbe, 0x69, 0x42, 0x27, 0x48, 0xad, 0x00, 0xa1, 0x54, 0x43, 0xf1, 0x82, 0x03, 0xa0, 0x7d, 0x60, 0x60, 0xf6, 0x88, 0xf3, 0x0f, 0x21, 0x63, 0x2b }; static const unsigned char rx_script_2_body[] = { 0x01 }; static const QUIC_PKT_HDR rx_script_2_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, 0, 3, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0x00, 0xbf, 0xf4, 0x00}, NULL, 0, 1, NULL }; static const struct rx_test_op rx_script_2[] = { RX_OP_ALLOW_1RTT() RX_OP_SET_INIT_LARGEST_PN(654360560) RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_CHACHA20POLY1305, rx_script_2_secret) RX_OP_INJECT_CHECK(2) RX_OP_CHECK_NO_PKT() RX_OP_END }; #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */ /* 3. Real World - Version Negotiation Response */ static const unsigned char rx_script_3_in[] = { 0xc7, /* Long; Random Bits */ 0x00, 0x00, 0x00, 0x00, /* Version 0 (Version Negotiation) */ 0x00, /* DCID */ 0x0c, 0x35, 0x3c, 0x1b, 0x97, 0xca, /* SCID */ 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f, 0x00, 0x00, 0x00, 0x01, /* Supported Version: 1 */ 0xaa, 0x9a, 0x3a, 0x9a /* Supported Version: Random (GREASE) */ }; static const QUIC_PKT_HDR rx_script_3_expect_hdr = { QUIC_PKT_TYPE_VERSION_NEG, 0, /* Spin Bit */ 0, /* Key Phase */ 0, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {12, {0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, /* SCID */ 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f}}, {0}, /* PN */ NULL, 0, /* Token/Token Len */ 8, NULL }; static const unsigned char rx_script_3_body[] = { 0x00, 0x00, 0x00, 0x01, 0xaa, 0x9a, 0x3a, 0x9a }; static const struct rx_test_op rx_script_3[] = { RX_OP_SET_RX_DCID(empty_conn_id) /* * This is a version negotiation packet, so doesn't have any frames. * However, the depacketizer still handles this sort of packet, so * we still pass the packet to it, to exercise what it does. */ RX_OP_INJECT_CHECK(3) RX_OP_CHECK_NO_PKT() RX_OP_END }; /* 4. Real World - Retry (S2C) */ static const unsigned char rx_script_4_in[] = { 0xf0, /* Long; Retry */ 0x00, 0x00, 0x00, 0x01, /* Version 1 */ 0x00, /* DCID */ 0x04, 0xad, 0x15, 0x3f, 0xae, /* SCID */ /* Retry Token, including 16-byte Retry Integrity Tag */ 0xf6, 0x8b, 0x6e, 0xa3, 0xdc, 0x40, 0x38, 0xc6, 0xa5, 0x99, 0x1c, 0xa9, 0x77, 0xe6, 0x1d, 0x4f, 0x09, 0x36, 0x12, 0x26, 0x00, 0x56, 0x0b, 0x29, 0x7d, 0x5e, 0xda, 0x39, 0xc6, 0x61, 0x57, 0x69, 0x15, 0xff, 0x93, 0x39, 0x95, 0xf0, 0x57, 0xf1, 0xe5, 0x36, 0x08, 0xad, 0xd2, 0x75, 0xa9, 0x68, 0x29, 0xed, 0xaa, 0x03, 0x0e, 0x5f, 0xac, 0xbd, 0x26, 0x07, 0x95, 0x4e, 0x48, 0x61, 0x26, 0xc5, 0xe2, 0x6c, 0x60, 0xbf, 0xa8, 0x6f, 0x51, 0xbb, 0x1d, 0xf7, 0x98, 0x95, 0x3b, 0x2c, 0x50, 0x79, 0xcc, 0xde, 0x27, 0x84, 0x44, 0x9b, 0xb2, 0x4a, 0x94, 0x4d, 0x4d, 0x3d, 0xbc, 0x00, 0x9d, 0x69, 0xad, 0x45, 0x89, 0x04, 0x48, 0xca, 0x04, 0xf6, 0x3a, 0x62, 0xc1, 0x38, 0x9d, 0x82, 0xb3, 0x45, 0x62, 0x4c, }; static const QUIC_PKT_HDR rx_script_4_expect_hdr = { QUIC_PKT_TYPE_RETRY, 0, /* Spin Bit */ 0, /* Key Phase */ 0, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0xad, 0x15, 0x3f, 0xae}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 114, NULL }; static const unsigned char rx_script_4_body[] = { 0xf6, 0x8b, 0x6e, 0xa3, 0xdc, 0x40, 0x38, 0xc6, 0xa5, 0x99, 0x1c, 0xa9, 0x77, 0xe6, 0x1d, 0x4f, 0x09, 0x36, 0x12, 0x26, 0x00, 0x56, 0x0b, 0x29, 0x7d, 0x5e, 0xda, 0x39, 0xc6, 0x61, 0x57, 0x69, 0x15, 0xff, 0x93, 0x39, 0x95, 0xf0, 0x57, 0xf1, 0xe5, 0x36, 0x08, 0xad, 0xd2, 0x75, 0xa9, 0x68, 0x29, 0xed, 0xaa, 0x03, 0x0e, 0x5f, 0xac, 0xbd, 0x26, 0x07, 0x95, 0x4e, 0x48, 0x61, 0x26, 0xc5, 0xe2, 0x6c, 0x60, 0xbf, 0xa8, 0x6f, 0x51, 0xbb, 0x1d, 0xf7, 0x98, 0x95, 0x3b, 0x2c, 0x50, 0x79, 0xcc, 0xde, 0x27, 0x84, 0x44, 0x9b, 0xb2, 0x4a, 0x94, 0x4d, 0x4d, 0x3d, 0xbc, 0x00, 0x9d, 0x69, 0xad, 0x45, 0x89, 0x04, 0x48, 0xca, 0x04, 0xf6, 0x3a, 0x62, 0xc1, 0x38, 0x9d, 0x82, 0xb3, 0x45, 0x62, 0x4c }; static const struct rx_test_op rx_script_4[] = { RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_INJECT_CHECK(4) RX_OP_CHECK_NO_PKT() RX_OP_END }; /* * 5. Real World - S2C Multiple Packets * - Initial, Handshake, 1-RTT (AES-128-GCM/SHA256) */ static const QUIC_CONN_ID rx_script_5_c2s_init_dcid = { 4, {0xad, 0x15, 0x3f, 0xae} }; static const unsigned char rx_script_5_handshake_secret[32] = { 0x5e, 0xc6, 0x4a, 0x4d, 0x0d, 0x40, 0x43, 0x3b, 0xd5, 0xbd, 0xe0, 0x19, 0x71, 0x47, 0x56, 0xf3, 0x59, 0x3a, 0xa6, 0xc9, 0x3e, 0xdc, 0x81, 0x1e, 0xc7, 0x72, 0x9d, 0x83, 0xd8, 0x8f, 0x88, 0x77 }; static const unsigned char rx_script_5_1rtt_secret[32] = { 0x53, 0xf2, 0x1b, 0x94, 0xa7, 0x65, 0xf7, 0x76, 0xfb, 0x06, 0x27, 0xaa, 0xd2, 0x3f, 0xe0, 0x9a, 0xbb, 0xcf, 0x99, 0x6f, 0x13, 0x2c, 0x6a, 0x37, 0x95, 0xf3, 0xda, 0x21, 0xcb, 0xcb, 0xa5, 0x26, }; static const unsigned char rx_script_5_in[] = { /* First Packet: Initial */ 0xc4, /* Long, Initial, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x83, 0xd0, 0x0a, 0x27, /* SCID */ 0x00, /* Token Length */ 0x41, 0xd2, /* Length (466) */ 0xe3, 0xab, /* PN (0) */ 0x22, 0x35, 0x34, 0x12, 0xcf, 0x20, 0x2b, 0x16, 0xaf, 0x08, 0xd4, 0xe0, 0x94, 0x8b, 0x1e, 0x62, 0xdf, 0x31, 0x61, 0xcc, 0xf9, 0xfa, 0x66, 0x4f, 0x18, 0x61, 0x07, 0xcb, 0x13, 0xd3, 0xf9, 0xbf, 0xe2, 0x8e, 0x25, 0x8d, 0xd1, 0xdf, 0x58, 0x9c, 0x05, 0x20, 0xf9, 0xf2, 0x01, 0x20, 0xe9, 0x39, 0xc3, 0x80, 0x77, 0xec, 0xa4, 0x57, 0xcf, 0x57, 0x8c, 0xdd, 0x68, 0x82, 0x91, 0xfe, 0x71, 0xa0, 0xfa, 0x56, 0x4c, 0xf2, 0xe7, 0x2b, 0xd0, 0xc0, 0xda, 0x81, 0xe2, 0x39, 0xb5, 0xf0, 0x0f, 0xd9, 0x07, 0xd5, 0x67, 0x09, 0x02, 0xf0, 0xff, 0x74, 0xb0, 0xa0, 0xd9, 0x3a, 0x7e, 0xb6, 0x57, 0x82, 0x47, 0x18, 0x66, 0xed, 0xe2, 0x18, 0x4d, 0xc2, 0x5c, 0x9f, 0x05, 0x09, 0x18, 0x24, 0x0e, 0x3f, 0x3d, 0xf9, 0x15, 0x8b, 0x08, 0xfd, 0x25, 0xe9, 0xc9, 0xb7, 0x8c, 0x18, 0x7b, 0xf3, 0x37, 0x58, 0xf0, 0xf0, 0xac, 0x33, 0x55, 0x3f, 0x39, 0xbc, 0x62, 0x03, 0x8a, 0xc0, 0xd6, 0xcc, 0x49, 0x47, 0xeb, 0x85, 0xb6, 0x72, 0xd7, 0xf8, 0xdc, 0x01, 0x32, 0xec, 0x1b, 0x4e, 0x38, 0x6e, 0x2c, 0xc5, 0x80, 0xf2, 0x43, 0x4a, 0xf5, 0xe5, 0xa2, 0xf8, 0x76, 0xa7, 0xa8, 0x57, 0x32, 0x67, 0x72, 0xeb, 0x82, 0xac, 0x3e, 0xc0, 0x15, 0x67, 0xac, 0x32, 0x19, 0x18, 0x0a, 0xef, 0x20, 0xa1, 0xe8, 0xaf, 0xac, 0x33, 0x87, 0x4c, 0x55, 0x05, 0x9b, 0x78, 0xf0, 0x3a, 0xce, 0x02, 0x28, 0x06, 0x84, 0x61, 0x97, 0xac, 0x87, 0x8f, 0x25, 0xe7, 0x1b, 0xa3, 0x02, 0x08, 0x4c, 0x2e, 0xef, 0xbd, 0x4f, 0x82, 0xe7, 0x37, 0x6c, 0x27, 0x6f, 0x85, 0xb4, 0xbc, 0x79, 0x38, 0x45, 0x80, 0x8a, 0xda, 0x2f, 0x11, 0x11, 0xac, 0x9c, 0xf3, 0x93, 0xc1, 0x49, 0x1b, 0x94, 0x12, 0x77, 0x07, 0xdc, 0xbf, 0xc2, 0xfd, 0x8b, 0xf6, 0xf1, 0x66, 0x1c, 0x7f, 0x07, 0xbf, 0x1f, 0xae, 0x27, 0x6c, 0x66, 0xe9, 0xa3, 0x64, 0x7a, 0x96, 0x78, 0x45, 0xfe, 0x4b, 0x8c, 0x6f, 0x7f, 0x03, 0x47, 0x3c, 0xd7, 0xf7, 0x63, 0x92, 0x58, 0x5b, 0x63, 0x83, 0x03, 0x05, 0xc3, 0x5d, 0x36, 0x62, 0x63, 0x5e, 0xcf, 0xfe, 0x0a, 0x29, 0xfa, 0xeb, 0xc8, 0xaf, 0xce, 0x31, 0x07, 0x6a, 0x09, 0x41, 0xc0, 0x2d, 0x98, 0x70, 0x05, 0x3b, 0x41, 0xfc, 0x7d, 0x61, 0xe0, 0x41, 0x7d, 0x13, 0x41, 0x51, 0x52, 0xb4, 0x78, 0xd5, 0x46, 0x51, 0x3b, 0xf1, 0xcd, 0xcc, 0x2e, 0x49, 0x30, 0x8b, 0x2a, 0xd2, 0xe6, 0x69, 0xb5, 0x6b, 0x7a, 0xf4, 0xbb, 0xd1, 0xf8, 0x4a, 0xe8, 0x53, 0x10, 0x46, 0x85, 0x8d, 0x66, 0x8e, 0x2b, 0xe8, 0x5d, 0xab, 0x7e, 0xfe, 0x5a, 0x79, 0xcf, 0xc5, 0x0c, 0x30, 0x9e, 0x98, 0x02, 0xb3, 0xa6, 0xd5, 0xfa, 0x25, 0xa8, 0xc8, 0xc1, 0xd9, 0x51, 0x60, 0x57, 0x5d, 0xfe, 0x75, 0x97, 0x05, 0xda, 0xbb, 0xc6, 0x6a, 0xbe, 0x5c, 0xa5, 0x65, 0x0a, 0x12, 0x33, 0x1c, 0xdf, 0xee, 0x08, 0xa9, 0x13, 0x13, 0x28, 0xce, 0x61, 0x59, 0xd1, 0x4e, 0xc7, 0x74, 0xfd, 0x64, 0xde, 0x08, 0xce, 0xda, 0x3f, 0xec, 0xad, 0xc9, 0xe1, 0xf9, 0x1f, 0x74, 0xf6, 0x86, 0x37, 0x6a, 0xa0, 0xc8, 0x0b, 0x1b, 0x94, 0x98, 0x86, 0x81, 0x3b, 0xfc, 0x47, 0x6c, 0xc9, 0x3e, 0x3c, 0x30, 0xc5, 0x9e, 0xb2, 0x32, 0x47, 0xf5, 0x0c, 0x6f, /* Second Packet: Handshake */ 0xe6, /* Long, Handshake, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x83, 0xd0, 0x0a, 0x27, /* SCID */ 0x42, 0x9c, /* Length (668) */ 0x9c, 0x55, /* PN (0) */ 0x55, 0xd4, 0x50, 0x02, 0x1a, 0x57, 0x84, 0x22, 0xcd, 0x01, 0xe5, 0x42, 0x1b, 0x1e, 0x06, 0xf1, 0x86, 0xe2, 0x90, 0xf8, 0x9c, 0x3d, 0xa2, 0x7c, 0xde, 0x2b, 0xc9, 0x2e, 0xcd, 0xa8, 0x4f, 0x5a, 0x20, 0xca, 0x96, 0xb6, 0x11, 0x4b, 0xc8, 0x71, 0x32, 0xb5, 0xc7, 0x1a, 0x69, 0x7f, 0x1e, 0x37, 0x49, 0xfb, 0x08, 0xce, 0x83, 0x5f, 0x02, 0x6d, 0x8a, 0x8f, 0xe7, 0x5d, 0xe1, 0x34, 0x31, 0x22, 0x53, 0x53, 0x32, 0xcb, 0x04, 0x21, 0xce, 0xbc, 0xa5, 0x1b, 0xdd, 0x4d, 0xd5, 0x1c, 0xd6, 0x5d, 0x88, 0x29, 0x5a, 0x19, 0x71, 0x6a, 0xc2, 0xfa, 0xb7, 0xb4, 0x7d, 0xd1, 0x72, 0x93, 0x8f, 0x7c, 0xb5, 0x36, 0x1b, 0xea, 0xf3, 0xf1, 0xd7, 0x6e, 0xd3, 0x91, 0x96, 0x62, 0x4d, 0xc6, 0xec, 0xb7, 0xb0, 0xb7, 0x9b, 0x95, 0x8b, 0x14, 0x8d, 0x1a, 0x0d, 0xb6, 0x3e, 0xec, 0xfe, 0x3b, 0x51, 0xea, 0x1a, 0x05, 0x14, 0x12, 0x93, 0x0e, 0x7e, 0xe6, 0xa2, 0xc5, 0x22, 0x87, 0x65, 0xf8, 0x5d, 0x3c, 0x55, 0x18, 0xcb, 0xe9, 0xef, 0x23, 0x43, 0xfe, 0xe8, 0x0d, 0xb2, 0x0f, 0xc5, 0xf4, 0xb3, 0xde, 0x0c, 0xea, 0xa4, 0x48, 0x8e, 0xbf, 0x1f, 0xc7, 0x99, 0x53, 0x8c, 0xc1, 0x3d, 0xba, 0xf4, 0x8e, 0x8e, 0x02, 0x52, 0xf6, 0x1f, 0xcf, 0x1d, 0xaa, 0xb3, 0xcb, 0x08, 0xc2, 0xe1, 0x70, 0x68, 0x74, 0x78, 0xa9, 0x30, 0x67, 0xba, 0x2b, 0xea, 0x35, 0x63, 0x47, 0xff, 0x29, 0x73, 0x29, 0xc6, 0xe8, 0x08, 0xa9, 0x1e, 0x8f, 0x28, 0x41, 0xa4, 0x24, 0x54, 0x26, 0x5f, 0x42, 0x77, 0xb1, 0x2b, 0x3d, 0x65, 0x67, 0x60, 0xa7, 0x23, 0x0d, 0xa7, 0xf4, 0xd6, 0xe9, 0x4e, 0x58, 0x43, 0x9f, 0x3c, 0x9e, 0x77, 0x61, 0xe5, 0x04, 0x4f, 0x73, 0xc9, 0x10, 0x79, 0xd0, 0xda, 0x3b, 0xc6, 0x19, 0x93, 0x9f, 0x48, 0x3b, 0x76, 0x38, 0xa1, 0x72, 0x49, 0x7d, 0x86, 0x7f, 0xe8, 0x1b, 0xa9, 0x5b, 0xc0, 0x47, 0xa0, 0x9c, 0x3f, 0x65, 0x60, 0x76, 0x59, 0xaf, 0x20, 0x2d, 0x40, 0xa6, 0x80, 0x49, 0x5a, 0x8f, 0x09, 0xf8, 0xf6, 0x97, 0xc1, 0xbd, 0xe1, 0x9f, 0x9b, 0xa2, 0x4c, 0x7b, 0x88, 0xac, 0xbe, 0x4b, 0x11, 0x28, 0xd7, 0x67, 0xe6, 0xad, 0xaf, 0xd0, 0xad, 0x01, 0x29, 0xa4, 0x4a, 0xc4, 0xb8, 0x2e, 0x42, 0x79, 0x24, 0x9e, 0xd5, 0x34, 0xae, 0x45, 0xf1, 0x0b, 0x38, 0x4a, 0x76, 0xfb, 0x50, 0xa2, 0x99, 0xc9, 0x5b, 0x6d, 0xc0, 0xb7, 0x55, 0xd8, 0x8d, 0x49, 0xdd, 0x1b, 0xb8, 0xec, 0x10, 0x57, 0x9e, 0x33, 0xb4, 0x10, 0x16, 0x19, 0xac, 0x69, 0xa2, 0x19, 0x1b, 0xd0, 0x77, 0x45, 0xeb, 0x49, 0x5c, 0xc5, 0x7c, 0xbe, 0x4b, 0x4a, 0x22, 0x5c, 0x3d, 0x0e, 0x6e, 0xe5, 0x4b, 0x36, 0x06, 0x63, 0x03, 0x97, 0xab, 0xed, 0xdc, 0xea, 0x64, 0xc2, 0x70, 0xb6, 0x7e, 0x35, 0xfb, 0x13, 0x66, 0x37, 0xa3, 0x3f, 0x28, 0x16, 0x6c, 0xe7, 0xd4, 0xe6, 0xca, 0x26, 0x0f, 0x19, 0xdd, 0x02, 0xae, 0xc1, 0xcf, 0x18, 0x7d, 0x56, 0xe6, 0x52, 0xf3, 0x37, 0xb5, 0x86, 0x9d, 0x1d, 0x55, 0xb3, 0x95, 0x19, 0x19, 0xa5, 0x44, 0x95, 0x81, 0xed, 0x02, 0x18, 0xf1, 0x85, 0x57, 0x78, 0x28, 0xc4, 0x9a, 0xba, 0xe8, 0x5e, 0x22, 0x8d, 0xc1, 0x7b, 0x2a, 0x8a, 0xc8, 0xb9, 0xdd, 0x82, 0xb2, 0x7b, 0x9f, 0x3d, 0xf5, 0x27, 0x2a, 0x48, 0x53, 0xc7, 0xa0, 0x70, 0x0e, 0x9d, 0x61, 0xaa, 0xe2, 0xad, 0x28, 0xf2, 0xb4, 0xfc, 0x56, 0x6b, 0x89, 0xe7, 0xf9, 0x51, 0xc9, 0xe9, 0xd3, 0x8a, 0x8c, 0x7e, 0x86, 0xdd, 0xba, 0x2f, 0x39, 0xbf, 0x26, 0x62, 0x23, 0xd6, 0x98, 0x6d, 0x3e, 0x72, 0xd7, 0x1b, 0xe1, 0x62, 0x94, 0x35, 0xe2, 0x18, 0x19, 0x46, 0xb8, 0x2c, 0xb5, 0x8f, 0x8f, 0xb0, 0x5b, 0x76, 0x7b, 0x7e, 0xb8, 0xc6, 0xb7, 0xe9, 0x4e, 0x9d, 0x30, 0x68, 0x03, 0x1e, 0x19, 0x73, 0xc5, 0x3e, 0x24, 0xe2, 0x95, 0x60, 0x1b, 0x27, 0x93, 0x7c, 0x17, 0xc2, 0xc6, 0xa3, 0xbd, 0xbd, 0x70, 0xc6, 0x60, 0x59, 0xc8, 0x5c, 0xd7, 0x9a, 0xc4, 0x29, 0xac, 0x0f, 0xaa, 0x0d, 0xa9, 0x92, 0xa3, 0x95, 0xd7, 0x0f, 0x6f, 0x74, 0x99, 0x9b, 0xc1, 0xd3, 0x68, 0x6d, 0xac, 0x82, 0x2d, 0x32, 0x41, 0x9e, 0x0c, 0xf7, 0x31, 0x59, 0x4c, 0x93, 0x1c, 0x3b, 0x71, 0x69, 0xcf, 0xc5, 0xca, 0x2b, 0xdf, 0xe7, 0xaa, 0xfd, 0x1d, 0x71, 0x01, 0x7e, 0x1c, 0x70, 0x62, 0x20, 0x61, 0xf8, 0x35, 0xc1, 0x71, 0xe7, 0x02, 0x0d, 0x88, 0x44, 0xd9, 0x00, 0xc5, 0xcc, 0x63, 0xe4, 0xf0, 0x86, 0xa7, 0xd0, 0xfe, 0xcc, 0xb7, 0x1d, 0xfc, 0x21, 0x61, 0x54, 0x15, 0xea, 0x81, 0x5e, 0xc0, 0x31, 0xfa, 0xbf, 0x7d, 0xb9, 0x3b, 0xa2, 0x1e, 0x42, 0x73, 0x05, 0x3c, 0xdb, 0x21, 0x59, 0x4f, 0x63, /* Third Packet: 1-RTT */ 0x5f, /* Short, 1-RTT, Spin=0, KP=0, PN Length=2 bytes */ 0x68, 0x47, /* PN (0) */ 0xa3, 0x3c, 0xa5, 0x27, 0x5e, 0xf9, 0x8d, 0xec, 0xea, 0x6c, 0x09, 0x18, 0x40, 0x80, 0xee, 0x9f, 0x6f, 0x73, 0x5c, 0x49, 0xe3, 0xec, 0xb7, 0x58, 0x05, 0x66, 0x8f, 0xa3, 0x52, 0x37, 0xa1, 0x22, 0x1f, 0xc6, 0x92, 0xd6, 0x59, 0x04, 0x99, 0xcb, 0x44, 0xef, 0x66, 0x05, 0x2d, 0xd0, 0x85, 0x24, 0xbb, 0xe3, 0xa1, 0xd1, 0xbe, 0xf7, 0x54, 0xad, 0x65, 0xf4, 0xd4, 0x59, 0x54, 0x87, 0x4e, 0x22, 0x4f, 0x06, 0x07, 0xa7, 0x8a, 0x14, 0x89, 0xd1, 0x3f, 0xd3, 0xe4, 0x6f, 0x71, 0x8f, 0x9a, 0xd2, 0x3b, 0x61, 0x0a, 0xba, 0x9a, 0x31, 0x56, 0xc7, }; static const QUIC_PKT_HDR rx_script_5a_expect_hdr = { QUIC_PKT_TYPE_INITIAL, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x83, 0xd0, 0x0a, 0x27}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 448, NULL }; static const unsigned char rx_script_5a_body[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xe2, 0xd2, 0x0a, 0x3b, 0xa2, 0xc4, 0xd2, 0x29, 0xc8, 0xe8, 0xba, 0x23, 0x31, 0x88, 0x2c, 0x71, 0xeb, 0xba, 0x42, 0x5f, 0x94, 0xe9, 0x0a, 0x90, 0x35, 0x31, 0x1e, 0xca, 0xed, 0xf8, 0x8a, 0x8d, 0x00, 0x13, 0x01, 0x00, 0x00, 0x2e, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x96, 0x0b, 0x4b, 0x30, 0x66, 0x3a, 0x75, 0x01, 0x4a, 0xdc, 0x2a, 0x75, 0x1f, 0xce, 0x7a, 0x30, 0x9d, 0x00, 0xca, 0x20, 0xb4, 0xe0, 0x6b, 0x81, 0x23, 0x18, 0x0b, 0x20, 0x1f, 0x54, 0x86, 0x1d, }; static const QUIC_PKT_HDR rx_script_5b_expect_hdr = { QUIC_PKT_TYPE_HANDSHAKE, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x83, 0xd0, 0x0a, 0x27}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 650, NULL }; static const unsigned char rx_script_5b_body[] = { 0x06, 0x00, 0x42, 0x86, 0x08, 0x00, 0x00, 0x7d, 0x00, 0x7b, 0x00, 0x10, 0x00, 0x08, 0x00, 0x06, 0x05, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x00, 0x39, 0x00, 0x6b, 0x4b, 0x20, 0x0b, 0x1b, 0xe1, 0x1f, 0xd0, 0x78, 0xc0, 0x69, 0x72, 0x9c, 0xe2, 0xf7, 0x05, 0x04, 0x80, 0x08, 0x00, 0x00, 0x06, 0x04, 0x80, 0x08, 0x00, 0x00, 0x07, 0x04, 0x80, 0x08, 0x00, 0x00, 0x04, 0x04, 0x80, 0x0c, 0x00, 0x00, 0x08, 0x02, 0x40, 0x64, 0x09, 0x02, 0x40, 0x64, 0x01, 0x04, 0x80, 0x00, 0x75, 0x30, 0x03, 0x02, 0x45, 0xac, 0x0b, 0x01, 0x1a, 0x0c, 0x00, 0x02, 0x10, 0x41, 0x94, 0x41, 0x8d, 0x0d, 0xfb, 0x60, 0x7b, 0xdc, 0xcc, 0xa2, 0x9c, 0x3e, 0xa5, 0xdf, 0x8d, 0x00, 0x08, 0x2d, 0x71, 0x8a, 0x38, 0xdf, 0xdd, 0xe0, 0x03, 0x0e, 0x01, 0x04, 0x0f, 0x04, 0x83, 0xd0, 0x0a, 0x27, 0x10, 0x04, 0xad, 0x15, 0x3f, 0xae, 0x20, 0x01, 0x00, 0x0b, 0x00, 0x01, 0x8f, 0x00, 0x00, 0x01, 0x8b, 0x00, 0x01, 0x86, 0x30, 0x82, 0x01, 0x82, 0x30, 0x82, 0x01, 0x29, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x0a, 0x73, 0x0f, 0x86, 0x18, 0xf2, 0xc3, 0x30, 0x01, 0xd2, 0xc0, 0xc1, 0x62, 0x52, 0x13, 0xf1, 0x9c, 0x13, 0x39, 0xb5, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x38, 0x30, 0x32, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x39, 0x30, 0x31, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x67, 0xf4, 0xd3, 0x8f, 0x15, 0x6d, 0xee, 0x85, 0xcc, 0x2a, 0x77, 0xfc, 0x0b, 0x8f, 0x9f, 0xcf, 0xa9, 0x95, 0x5d, 0x5b, 0xcd, 0xb7, 0x8b, 0xba, 0x31, 0x0a, 0x73, 0x62, 0xc5, 0xd0, 0x0e, 0x07, 0x90, 0xae, 0x38, 0x43, 0x79, 0xce, 0x5e, 0x33, 0xad, 0x31, 0xbf, 0x9f, 0x2a, 0x56, 0x83, 0xa5, 0x24, 0x16, 0xab, 0x0c, 0xf1, 0x64, 0xbe, 0xe4, 0x93, 0xb5, 0x89, 0xd6, 0x05, 0xe4, 0xf7, 0x7b, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x0a, 0x82, 0x92, 0x6e, 0xd3, 0xc6, 0x66, 0xd9, 0xd3, 0x75, 0xff, 0x71, 0x3b, 0x61, 0x46, 0x21, 0x00, 0xe6, 0x21, 0x5d, 0x9c, 0x86, 0xe9, 0x65, 0x40, 0x4f, 0xeb, 0x70, 0x4f, 0x2c, 0xad, 0x00, 0x02, 0x20, 0x08, 0xc2, 0x07, 0x5d, 0x16, 0xfc, 0x54, 0x34, 0x2b, 0xb4, 0x18, 0x67, 0x44, 0x81, 0xc9, 0xa9, 0x67, 0x2e, 0xce, 0xa1, 0x02, 0x9f, 0x3b, 0xe5, 0x61, 0x16, 0x0b, 0x50, 0xf6, 0xa1, 0x50, 0x94, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x4a, 0x04, 0x03, 0x00, 0x46, 0x30, 0x44, 0x02, 0x20, 0x7d, 0x57, 0x17, 0x14, 0x46, 0x09, 0x95, 0x70, 0x09, 0x45, 0xe8, 0x9e, 0x5c, 0x87, 0x55, 0xd9, 0x08, 0xc6, 0x5e, 0x47, 0x73, 0x5e, 0xb1, 0xc9, 0xef, 0xcb, 0xe5, 0x7f, 0xcc, 0xb0, 0x28, 0xbc, 0x02, 0x20, 0x5d, 0xe4, 0x2b, 0x83, 0xd9, 0x78, 0x75, 0x45, 0xf3, 0x22, 0x2b, 0x38, 0xeb, 0x68, 0xe5, 0x71, 0x5d, 0xcb, 0xc3, 0x68, 0xb3, 0x0e, 0x7d, 0x5e, 0x1d, 0xc2, 0x1b, 0x8a, 0x62, 0x80, 0x48, 0x3e, 0x14, 0x00, 0x00, 0x20, 0x37, 0xcd, 0x55, 0xca, 0x3f, 0x4b, 0xf0, 0x95, 0xf8, 0xe4, 0xfe, 0x59, 0xab, 0xbc, 0xc1, 0x8f, 0x0c, 0x3f, 0x41, 0x59, 0xf6, 0x96, 0xdb, 0x75, 0xae, 0xe7, 0x86, 0x1a, 0x92, 0xa7, 0x53, 0x0a, }; static const QUIC_PKT_HDR rx_script_5c_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 72, NULL }; static const unsigned char rx_script_5c_body[] = { 0x18, 0x03, 0x00, 0x04, 0x92, 0xec, 0xaa, 0xd6, 0x47, 0xd8, 0x8b, 0x56, 0x3b, 0x5f, 0x67, 0xe6, 0xb9, 0xb9, 0xca, 0x72, 0xca, 0xf2, 0x49, 0x7d, 0x18, 0x02, 0x00, 0x04, 0xa9, 0x6e, 0x9b, 0x84, 0x26, 0x43, 0x00, 0xc7, 0x55, 0x71, 0x67, 0x2e, 0x52, 0xdd, 0x47, 0xfd, 0x06, 0x51, 0x33, 0x08, 0x18, 0x01, 0x00, 0x04, 0x36, 0xd5, 0x1f, 0x06, 0x4e, 0xbf, 0xb4, 0xc9, 0xef, 0x97, 0x1e, 0x9a, 0x3c, 0xab, 0x1e, 0xfc, 0xb7, 0x90, 0xc3, 0x1a, }; static const struct rx_test_op rx_script_5[] = { RX_OP_ALLOW_1RTT() RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET_INITIAL(rx_script_5_c2s_init_dcid) RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5a) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, rx_script_5_handshake_secret) RX_OP_CHECK_PKT_N(5b) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, rx_script_5_1rtt_secret) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() /* Discard Initial EL and try injecting the packet again */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_INJECT_N(5) /* Initial packet is not output because we have discarded Initial keys */ RX_OP_CHECK_PKT_N(5b) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() /* Try again with discarded keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() /* Try again */ RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() /* Try again with discarded 1-RTT keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(5) RX_OP_CHECK_NO_PKT() /* Recreate QRL, test reading packets received before key */ RX_OP_SET_SCID_LEN(0) RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_INJECT_N(5) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET_INITIAL(rx_script_5_c2s_init_dcid) RX_OP_CHECK_PKT_N(5a) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, rx_script_5_handshake_secret) RX_OP_CHECK_PKT_N(5b) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, rx_script_5_1rtt_secret) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(5) RX_OP_CHECK_NO_PKT() RX_OP_END }; /* * 6. Real World - S2C Multiple Packets * - Initial, Handshake, 1-RTT (AES-256-GCM/SHA384) */ static const QUIC_CONN_ID rx_script_6_c2s_init_dcid = { 4, {0xac, 0x88, 0x95, 0xbd} }; static const unsigned char rx_script_6_handshake_secret[48] = { 0xd1, 0x41, 0xb0, 0xf6, 0x0d, 0x8b, 0xbd, 0xe8, 0x5b, 0xa8, 0xff, 0xd7, 0x18, 0x9a, 0x23, 0x7b, 0x13, 0x5c, 0x1e, 0x90, 0x1d, 0x08, 0x95, 0xcc, 0xc5, 0x8e, 0x73, 0x4e, 0x02, 0x6f, 0x3c, 0xb6, 0x26, 0x77, 0x8d, 0x53, 0xc5, 0x62, 0x9f, 0xb5, 0xf0, 0x88, 0xfb, 0xe5, 0x14, 0x71, 0xab, 0xe6, }; static const unsigned char rx_script_6_1rtt_secret[48] = { 0x2d, 0x6b, 0x9d, 0xd4, 0x39, 0xa0, 0xe7, 0xff, 0x17, 0xe2, 0xcb, 0x5c, 0x0d, 0x4a, 0xf6, 0x3f, 0xf4, 0xfe, 0xfc, 0xe5, 0x22, 0xfa, 0xf5, 0x5b, 0xc0, 0xb2, 0x18, 0xbb, 0x92, 0x4d, 0x35, 0xea, 0x67, 0xa6, 0xe7, 0xc1, 0x90, 0x10, 0xc9, 0x14, 0x46, 0xf5, 0x95, 0x57, 0x8b, 0x90, 0x88, 0x5d, }; static const unsigned char rx_script_6_in[] = { /* First Packet: Initial */ 0xc5, /* Long, Initial, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x36, 0xf4, 0x75, 0x2d, /* SCID */ 0x00, /* Token Length */ 0x41, 0xbe, /* Length (446) */ 0xa9, 0xe2, /* PN (0) */ 0x83, 0x39, 0x95, 0x8f, 0x8f, 0x8c, 0xa9, 0xaf, 0x10, 0x29, 0x3d, 0xfc, 0x56, 0x4a, 0x1c, 0x4b, 0xc9, 0x48, 0xb1, 0xaf, 0x36, 0xd5, 0xac, 0x95, 0xbf, 0xfd, 0x2c, 0x4d, 0x70, 0x2e, 0x5b, 0x7c, 0x22, 0x5f, 0x5f, 0xee, 0x10, 0x8f, 0xfb, 0x0b, 0x5f, 0x9d, 0x7e, 0x68, 0x2f, 0x94, 0x0b, 0xdb, 0xed, 0xef, 0xfa, 0x4e, 0xc6, 0xd5, 0xe7, 0xef, 0xe0, 0x78, 0x3c, 0xdc, 0xe9, 0xd8, 0xe8, 0x56, 0x71, 0xd7, 0xe7, 0x6c, 0x7f, 0x5d, 0xaa, 0x7a, 0x52, 0x1d, 0x95, 0x7a, 0x80, 0x70, 0x38, 0xc0, 0x8b, 0xa1, 0x2f, 0x09, 0x16, 0xd2, 0xec, 0xa3, 0x23, 0x72, 0x45, 0x3c, 0xbd, 0x8c, 0xda, 0xbb, 0x37, 0x5a, 0x8d, 0xb2, 0x00, 0x7e, 0x67, 0x0c, 0xa0, 0x32, 0xdd, 0x80, 0x07, 0x71, 0xb0, 0x95, 0x21, 0xbc, 0x1e, 0xbd, 0x63, 0x0a, 0x10, 0xe7, 0x4b, 0x6e, 0x2e, 0x85, 0x3a, 0x65, 0xf7, 0x06, 0x6e, 0x7e, 0x8f, 0x65, 0x8c, 0xb1, 0x93, 0xe9, 0x0d, 0xe8, 0x46, 0xe7, 0xcf, 0xa7, 0xd2, 0x8b, 0x15, 0x23, 0xec, 0xc3, 0xec, 0x44, 0xda, 0x62, 0x15, 0x35, 0x34, 0x2f, 0x62, 0x77, 0xc8, 0x1f, 0x83, 0x22, 0x00, 0xe5, 0xc0, 0x89, 0xb8, 0x97, 0xd2, 0x37, 0x02, 0xea, 0xa2, 0x35, 0xbf, 0x19, 0xf0, 0xba, 0x1d, 0xb7, 0xaa, 0x36, 0xbb, 0x11, 0x60, 0xc3, 0x45, 0x1f, 0xe5, 0x18, 0xde, 0x4c, 0x01, 0x23, 0x2d, 0x17, 0x78, 0xdd, 0x4c, 0x8a, 0x1e, 0x1b, 0xd4, 0xda, 0x56, 0x43, 0x13, 0xa4, 0x4f, 0xfd, 0xd5, 0x92, 0x6a, 0x05, 0x5f, 0x14, 0x63, 0x85, 0x7d, 0xf1, 0x31, 0xb8, 0x27, 0x0b, 0xa6, 0xb5, 0x50, 0xca, 0x8b, 0x0e, 0xa1, 0x0d, 0xf9, 0xc4, 0xea, 0x6a, 0x6e, 0x4b, 0x6d, 0xdf, 0x49, 0xe8, 0x32, 0xf6, 0x85, 0xc4, 0x29, 0x26, 0x32, 0xfb, 0x5e, 0xa8, 0x55, 0x6b, 0x67, 0xe9, 0xaa, 0x35, 0x33, 0x90, 0xd8, 0x2a, 0x71, 0x0b, 0x6a, 0x48, 0xc4, 0xa3, 0x8b, 0xe0, 0xe7, 0x00, 0x3d, 0xee, 0x30, 0x70, 0x84, 0xbd, 0xa3, 0x3c, 0x9e, 0xa3, 0x5c, 0x69, 0xab, 0x55, 0x7b, 0xe2, 0xe5, 0x86, 0x13, 0xcb, 0x93, 0x3f, 0xcb, 0x3e, 0x6d, 0xc9, 0xc2, 0x10, 0x2b, 0x00, 0x9b, 0x3f, 0x14, 0x4e, 0x04, 0x27, 0xc0, 0xae, 0x1d, 0x48, 0x89, 0x3a, 0xf4, 0xac, 0xe0, 0x05, 0x07, 0xc9, 0x74, 0x6e, 0x21, 0x01, 0xe9, 0x26, 0xfd, 0xb4, 0xb2, 0x2a, 0xda, 0x72, 0xda, 0xbf, 0x63, 0x9d, 0x37, 0xaf, 0x90, 0x05, 0xd6, 0x89, 0xc7, 0xa6, 0x81, 0x4e, 0x2a, 0x30, 0xe3, 0x05, 0x88, 0x9f, 0xd0, 0xba, 0x8d, 0xc4, 0x21, 0x52, 0x5a, 0x7a, 0xe1, 0xad, 0xd3, 0x88, 0xc2, 0x18, 0xad, 0x4c, 0xb1, 0x66, 0x73, 0x1b, 0xf2, 0xd1, 0xb9, 0x43, 0xaa, 0xc4, 0x66, 0xcd, 0x42, 0xfa, 0x80, 0xec, 0xa1, 0x7c, 0x45, 0x02, 0x53, 0x45, 0xd5, 0x07, 0xd4, 0x70, 0x12, 0x1b, 0x08, 0x05, 0x6e, 0x99, 0x0a, 0xd3, 0x5b, 0x99, 0x6b, 0x65, 0xc4, 0xc0, 0x04, 0x1b, 0x75, 0xf2, 0x86, 0x99, 0x09, 0x4a, 0x50, 0x70, 0x00, 0x7a, 0x93, 0xaa, 0xe6, 0xf4, 0x03, 0x29, 0x06, 0xa4, 0x30, 0x6d, 0x52, 0xbd, 0x60, 0xd1, 0x7e, 0xd6, 0x07, 0xc0, 0x41, 0x01, 0x12, 0x3e, 0x16, 0x94, /* Second Packet: Handshake */ 0xea, /* Long, Handshake, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x36, 0xf4, 0x75, 0x2d, /* SCID */ 0x42, 0xb0, /* Length (688) */ 0x3a, 0xc5, /* PN (0) */ 0x3b, 0x8e, 0x4c, 0x01, 0x72, 0x6b, 0xfa, 0xbb, 0xad, 0xf9, 0x9e, 0x21, 0xb1, 0xd0, 0x01, 0xf1, 0xd4, 0x67, 0x8d, 0x2c, 0xee, 0x04, 0x60, 0x4a, 0xe2, 0xe4, 0xc6, 0x89, 0x01, 0xae, 0x3c, 0x1f, 0xf7, 0xe6, 0xf7, 0xac, 0x26, 0xcf, 0x3c, 0x6d, 0x1d, 0xfd, 0x11, 0x02, 0x51, 0x73, 0xb5, 0xe1, 0xb2, 0x44, 0x42, 0x32, 0x0f, 0xf5, 0x3d, 0x55, 0x2d, 0x1f, 0x02, 0x29, 0x51, 0x35, 0xdb, 0xc7, 0x7a, 0x34, 0x4b, 0xec, 0x60, 0x49, 0xa2, 0x90, 0x11, 0xef, 0x5a, 0xa9, 0x1c, 0xf7, 0xd9, 0x21, 0x68, 0x1c, 0x2b, 0xc6, 0x57, 0xde, 0xb1, 0x0b, 0x31, 0xed, 0xef, 0x16, 0xba, 0x08, 0xb9, 0xe2, 0xd9, 0xd0, 0xd8, 0x1f, 0xc4, 0x32, 0xe8, 0x45, 0x2a, 0x86, 0xe4, 0xd3, 0xaf, 0x72, 0x4f, 0x30, 0x01, 0x71, 0x15, 0x9b, 0xa9, 0x55, 0x35, 0xf7, 0x39, 0x7e, 0x6a, 0x59, 0x18, 0x4f, 0xe6, 0xdf, 0xb5, 0x0d, 0xc2, 0xe7, 0xb2, 0xa1, 0xa6, 0xa3, 0x9c, 0xf0, 0x0d, 0x59, 0x05, 0x49, 0x95, 0xfa, 0xcc, 0x72, 0xd7, 0xc0, 0x84, 0x2e, 0xc4, 0x1c, 0xd4, 0xa0, 0xe3, 0x6c, 0x5a, 0x8c, 0x94, 0x4d, 0x37, 0x1a, 0x1c, 0x68, 0x93, 0x5f, 0xe5, 0x99, 0x27, 0xc6, 0x06, 0xaa, 0x1f, 0x29, 0x17, 0xc5, 0x8c, 0x3d, 0x53, 0xa7, 0x05, 0x3a, 0x44, 0x53, 0x86, 0xed, 0x56, 0x99, 0x4c, 0xe2, 0x7b, 0x3a, 0x1e, 0x5d, 0x6d, 0xac, 0x78, 0x1e, 0xfa, 0x55, 0x58, 0x6e, 0x72, 0xee, 0xf9, 0x33, 0x64, 0x7f, 0x93, 0x3c, 0xfe, 0x18, 0x97, 0x6b, 0x02, 0x74, 0x90, 0x0d, 0xba, 0x89, 0xc0, 0x22, 0x0a, 0x0a, 0x37, 0x4c, 0x28, 0x74, 0xa7, 0x3a, 0x44, 0x74, 0x42, 0xff, 0xf1, 0xd2, 0x8d, 0x0c, 0xc1, 0xed, 0x98, 0x98, 0x8e, 0xa8, 0x6b, 0x95, 0x6a, 0x86, 0x0b, 0xb4, 0x95, 0x58, 0x34, 0x12, 0xb0, 0xc0, 0xf8, 0x2d, 0x5b, 0x40, 0x51, 0x80, 0x07, 0x91, 0x31, 0x77, 0xd3, 0x06, 0xa5, 0xe5, 0x1f, 0xe2, 0xf8, 0x92, 0xe4, 0x23, 0x2b, 0xf0, 0x4c, 0xa9, 0xa5, 0x6c, 0x6f, 0xaf, 0xaf, 0xbf, 0x97, 0xcf, 0x46, 0xf2, 0x8d, 0x61, 0x0e, 0x73, 0xcd, 0xc5, 0xde, 0xda, 0x50, 0x82, 0x61, 0x6d, 0xb1, 0xa2, 0xbe, 0x6b, 0x99, 0xcd, 0x5b, 0x99, 0x8f, 0x66, 0xab, 0x11, 0x78, 0xcc, 0xdb, 0x66, 0x98, 0xca, 0x19, 0x92, 0xf4, 0x05, 0xae, 0xe6, 0xf3, 0xe7, 0xf0, 0x30, 0x28, 0x31, 0x74, 0xff, 0xe2, 0xb3, 0x3a, 0x4f, 0x79, 0xe7, 0x2a, 0x9f, 0xe3, 0x41, 0xb2, 0x88, 0xc8, 0x8f, 0x77, 0x57, 0x42, 0x65, 0xdb, 0x07, 0xf6, 0x5f, 0xb8, 0x34, 0x17, 0xe3, 0x8d, 0x22, 0x5b, 0x88, 0x94, 0x60, 0x97, 0x32, 0x3d, 0x8a, 0x51, 0x9d, 0xb5, 0xac, 0xd7, 0x99, 0x96, 0x23, 0x6d, 0xc9, 0xab, 0x61, 0x41, 0x8f, 0x72, 0x1b, 0xf8, 0x84, 0xd9, 0x57, 0x88, 0x68, 0x3d, 0x73, 0x5f, 0xb1, 0x18, 0x5c, 0x3a, 0x35, 0xd2, 0xc5, 0xb7, 0x29, 0xc7, 0x95, 0xdd, 0x21, 0xc0, 0x78, 0x49, 0xf3, 0x24, 0xe0, 0x4c, 0x5c, 0x32, 0x08, 0xb7, 0x00, 0x43, 0x70, 0x5a, 0x95, 0x23, 0x91, 0xf5, 0xb7, 0x61, 0x85, 0x6f, 0xb3, 0xa4, 0x6b, 0x05, 0x9d, 0x39, 0xa3, 0xb1, 0x1c, 0x61, 0xc5, 0xa5, 0xe7, 0x9a, 0xe9, 0x5d, 0xaa, 0xca, 0x11, 0xd8, 0x4b, 0xa4, 0x9c, 0x18, 0x4e, 0x2b, 0x2d, 0x75, 0xc1, 0x12, 0x20, 0xe4, 0x66, 0xa5, 0x59, 0x67, 0x4b, 0xcc, 0x52, 0x2d, 0xfa, 0xaa, 0xa4, 0xe9, 0xfc, 0x79, 0xd7, 0xff, 0x03, 0x3e, 0xec, 0xba, 0x97, 0x37, 0x52, 0xc1, 0x57, 0x31, 0x8e, 0x57, 0x0c, 0x54, 0x92, 0x9c, 0x25, 0x5c, 0xfa, 0x9f, 0xa5, 0x36, 0x18, 0xd0, 0xaa, 0xf3, 0x3b, 0x5b, 0x59, 0xbd, 0x33, 0x5e, 0x7d, 0x74, 0x7c, 0xaf, 0xe9, 0x54, 0x80, 0xc4, 0xb4, 0xa1, 0x24, 0x9e, 0x23, 0x0d, 0xbf, 0x4e, 0x0f, 0xaf, 0xa5, 0x16, 0xcb, 0x3b, 0xfa, 0x33, 0xa5, 0x68, 0xa6, 0x64, 0x48, 0x2f, 0x5e, 0xfa, 0x64, 0x4e, 0xe3, 0x27, 0x4f, 0x13, 0xe6, 0x37, 0xf6, 0xb9, 0x63, 0x4b, 0xdc, 0x49, 0x3c, 0x5e, 0x9e, 0x06, 0xea, 0xac, 0xa3, 0xdf, 0x6c, 0x49, 0xfb, 0xa1, 0x01, 0x4f, 0x6f, 0x74, 0x1f, 0xd3, 0x26, 0xa1, 0x92, 0x3e, 0xe0, 0x73, 0xd6, 0x3b, 0x67, 0x13, 0x53, 0x2e, 0xcb, 0xbc, 0x83, 0xd0, 0x6e, 0x28, 0xb1, 0xcb, 0xd9, 0x66, 0xe0, 0x33, 0x59, 0x45, 0xd3, 0x13, 0xc2, 0x48, 0xd5, 0x9e, 0x88, 0xba, 0x75, 0x7b, 0xb1, 0xfe, 0x6f, 0xec, 0xde, 0xff, 0x14, 0x59, 0x75, 0xbf, 0x1a, 0x74, 0x47, 0xc5, 0xd8, 0xe8, 0x1b, 0x3c, 0x86, 0xd7, 0x1f, 0x99, 0x11, 0xd3, 0x29, 0xfd, 0x5d, 0x22, 0x7e, 0x03, 0x78, 0xed, 0x62, 0x0e, 0xbe, 0x6d, 0x75, 0xf4, 0xa8, 0x6e, 0xc7, 0x21, 0x76, 0xc5, 0xa0, 0x0c, 0xaa, 0x58, 0x78, 0x7e, 0x6e, 0xfc, 0x1e, 0x2a, 0x1c, 0xdd, 0xe5, 0x78, 0x08, 0xbd, 0xdb, 0xea, 0x8f, 0x8a, 0xa5, 0xbf, 0x93, 0xfe, 0x0f, 0x03, 0xa1, 0xc8, 0x64, 0x9f, 0x4a, /* Third Packet: 1-RTT */ 0x48, /* Short, 1-RTT, Spin=0, KP=0, PN Length=2 bytes */ 0x3e, 0x28, /* PN (0) */ 0xb9, 0xdb, 0x61, 0xf8, 0x8b, 0x3a, 0xef, 0x26, 0x69, 0xf2, 0x57, 0xc6, 0x84, 0x25, 0x6b, 0x77, 0xbe, 0x8c, 0x43, 0x32, 0xf3, 0x9a, 0xd1, 0x85, 0x14, 0xbc, 0x89, 0x3b, 0x9c, 0xf3, 0xfc, 0x00, 0xa1, 0x3a, 0xc3, 0xc4, 0x1e, 0xdf, 0xd0, 0x11, 0x70, 0xd9, 0x02, 0x7a, 0xd4, 0xef, 0x86, 0x67, 0xb1, 0x1e, 0x5d, 0xe3, 0x7f, 0x82, 0x14, 0x52, 0xa5, 0x8a, 0x89, 0xa7, 0x98, 0x75, 0x2f, 0x8a, 0x00, 0xf3, 0xbd, 0x49, 0x26, 0x4d, 0x0c, 0xc7, 0x38, 0xe7, 0x91, 0x85, 0xc9, 0x21, 0x6a, 0x1c, 0xc4, 0xa3, 0x0e, 0xd8, 0xfe, 0xb1, 0x25, 0x1a, }; static const QUIC_PKT_HDR rx_script_6a_expect_hdr = { QUIC_PKT_TYPE_INITIAL, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x36, 0xf4, 0x75, 0x2d}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 428, NULL }; static const unsigned char rx_script_6a_body[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xc3, 0x45, 0xe8, 0xb8, 0xf9, 0x7c, 0x9f, 0x5d, 0xcf, 0x66, 0x25, 0xe4, 0x91, 0x0e, 0xb0, 0x5a, 0x14, 0xce, 0xaf, 0xea, 0x83, 0x12, 0xde, 0x68, 0xd9, 0x31, 0xf2, 0x23, 0x11, 0x3a, 0x15, 0xcb, 0x00, 0x13, 0x02, 0x00, 0x00, 0x2e, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0xab, 0xd3, 0xc6, 0x9f, 0x36, 0xd3, 0x52, 0x93, 0x87, 0xee, 0x92, 0x01, 0xa2, 0xd6, 0x9a, 0x5e, 0x61, 0x43, 0xcc, 0x4a, 0xcc, 0x7a, 0xcd, 0x83, 0xb2, 0xd9, 0xad, 0xd1, 0x14, 0xdc, 0x84, 0x61, }; static const QUIC_PKT_HDR rx_script_6b_expect_hdr = { QUIC_PKT_TYPE_HANDSHAKE, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x36, 0xf4, 0x75, 0x2d}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 670, NULL }; static const unsigned char rx_script_6b_body[] = { 0x06, 0x00, 0x42, 0x9a, 0x08, 0x00, 0x00, 0x80, 0x00, 0x7e, 0x00, 0x10, 0x00, 0x08, 0x00, 0x06, 0x05, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x00, 0x39, 0x00, 0x6e, 0x47, 0xfa, 0x05, 0x5a, 0xe0, 0xec, 0x4a, 0xf3, 0x05, 0x04, 0x80, 0x08, 0x00, 0x00, 0x06, 0x04, 0x80, 0x08, 0x00, 0x00, 0x07, 0x04, 0x80, 0x08, 0x00, 0x00, 0x04, 0x04, 0x80, 0x0c, 0x00, 0x00, 0x08, 0x02, 0x40, 0x64, 0x09, 0x02, 0x40, 0x64, 0x01, 0x04, 0x80, 0x00, 0x75, 0x30, 0x03, 0x02, 0x45, 0xac, 0x0b, 0x01, 0x1a, 0x0c, 0x00, 0x02, 0x10, 0x35, 0xd7, 0x7d, 0x8b, 0xc5, 0xb1, 0x89, 0xb1, 0x5c, 0x23, 0x74, 0x50, 0xfd, 0x47, 0xfe, 0xd2, 0x00, 0x11, 0x96, 0x38, 0x27, 0xde, 0x7d, 0xfb, 0x2b, 0x38, 0x56, 0xe5, 0x2a, 0xb8, 0x6b, 0xfa, 0xaa, 0xde, 0x81, 0x0e, 0x01, 0x04, 0x0f, 0x04, 0x36, 0xf4, 0x75, 0x2d, 0x10, 0x04, 0xac, 0x88, 0x95, 0xbd, 0x20, 0x01, 0x00, 0x0b, 0x00, 0x01, 0x8f, 0x00, 0x00, 0x01, 0x8b, 0x00, 0x01, 0x86, 0x30, 0x82, 0x01, 0x82, 0x30, 0x82, 0x01, 0x29, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x0a, 0x73, 0x0f, 0x86, 0x18, 0xf2, 0xc3, 0x30, 0x01, 0xd2, 0xc0, 0xc1, 0x62, 0x52, 0x13, 0xf1, 0x9c, 0x13, 0x39, 0xb5, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x38, 0x30, 0x32, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x39, 0x30, 0x31, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x67, 0xf4, 0xd3, 0x8f, 0x15, 0x6d, 0xee, 0x85, 0xcc, 0x2a, 0x77, 0xfc, 0x0b, 0x8f, 0x9f, 0xcf, 0xa9, 0x95, 0x5d, 0x5b, 0xcd, 0xb7, 0x8b, 0xba, 0x31, 0x0a, 0x73, 0x62, 0xc5, 0xd0, 0x0e, 0x07, 0x90, 0xae, 0x38, 0x43, 0x79, 0xce, 0x5e, 0x33, 0xad, 0x31, 0xbf, 0x9f, 0x2a, 0x56, 0x83, 0xa5, 0x24, 0x16, 0xab, 0x0c, 0xf1, 0x64, 0xbe, 0xe4, 0x93, 0xb5, 0x89, 0xd6, 0x05, 0xe4, 0xf7, 0x7b, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x0a, 0x82, 0x92, 0x6e, 0xd3, 0xc6, 0x66, 0xd9, 0xd3, 0x75, 0xff, 0x71, 0x3b, 0x61, 0x46, 0x21, 0x00, 0xe6, 0x21, 0x5d, 0x9c, 0x86, 0xe9, 0x65, 0x40, 0x4f, 0xeb, 0x70, 0x4f, 0x2c, 0xad, 0x00, 0x02, 0x20, 0x08, 0xc2, 0x07, 0x5d, 0x16, 0xfc, 0x54, 0x34, 0x2b, 0xb4, 0x18, 0x67, 0x44, 0x81, 0xc9, 0xa9, 0x67, 0x2e, 0xce, 0xa1, 0x02, 0x9f, 0x3b, 0xe5, 0x61, 0x16, 0x0b, 0x50, 0xf6, 0xa1, 0x50, 0x94, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x4b, 0x04, 0x03, 0x00, 0x47, 0x30, 0x45, 0x02, 0x20, 0x78, 0x9e, 0xe0, 0x6a, 0x7a, 0xbd, 0xc3, 0x84, 0x3d, 0x25, 0x6a, 0x59, 0x23, 0x97, 0x52, 0x64, 0x4e, 0xb6, 0x9f, 0xcc, 0xd3, 0xd7, 0xa9, 0x29, 0x44, 0x75, 0x6d, 0x50, 0xfc, 0x22, 0xde, 0xd3, 0x02, 0x21, 0x00, 0xe5, 0x28, 0xd6, 0x5a, 0xd1, 0xec, 0x4a, 0xcc, 0x20, 0xb4, 0xea, 0x15, 0xfb, 0x8e, 0x73, 0xa8, 0x6b, 0xbb, 0x42, 0x70, 0x90, 0x08, 0x6e, 0x74, 0x6f, 0x5a, 0x05, 0xb5, 0x39, 0xee, 0x01, 0x04, 0x14, 0x00, 0x00, 0x30, 0xff, 0x9f, 0xb2, 0x1d, 0xcb, 0x4f, 0xfc, 0x7a, 0xac, 0xf4, 0x75, 0x24, 0x83, 0x5f, 0x8d, 0xa3, 0x3e, 0x9d, 0xef, 0x43, 0x67, 0x89, 0x5d, 0x55, 0xc7, 0xce, 0x80, 0xab, 0xc3, 0xc7, 0x74, 0xc7, 0xb2, 0x91, 0x27, 0xce, 0xd8, 0x5e, 0xc4, 0x4e, 0x96, 0x19, 0x68, 0x2d, 0xbe, 0x6f, 0x49, 0xfa, }; static const QUIC_PKT_HDR rx_script_6c_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 72, NULL }; static const unsigned char rx_script_6c_body[] = { 0x18, 0x03, 0x00, 0x04, 0xf2, 0x94, 0x49, 0xc3, 0x34, 0xa1, 0xf4, 0x0f, 0xcb, 0xb8, 0x03, 0x04, 0x1f, 0xc8, 0x69, 0xb9, 0x3b, 0xd5, 0xc6, 0x93, 0x18, 0x02, 0x00, 0x04, 0x9a, 0x4f, 0xec, 0x52, 0xde, 0xd2, 0xc8, 0xb7, 0x1c, 0x0c, 0xf3, 0x4e, 0x46, 0xf0, 0x6c, 0x54, 0x34, 0x1b, 0x0d, 0x98, 0x18, 0x01, 0x00, 0x04, 0xe3, 0x33, 0x9e, 0x59, 0x00, 0x69, 0xc3, 0xac, 0xfc, 0x58, 0x0e, 0xa4, 0xf4, 0xf3, 0x23, 0x1b, 0xd6, 0x8e, 0x5b, 0x08, }; static const struct rx_test_op rx_script_6[] = { RX_OP_ALLOW_1RTT() RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET_INITIAL(rx_script_6_c2s_init_dcid) RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6a) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES256GCM, rx_script_6_handshake_secret) RX_OP_CHECK_PKT_N(6b) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES256GCM, rx_script_6_1rtt_secret) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() /* Discard Initial EL and try injecting the packet again */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_INJECT_N(6) /* Initial packet is not output because we have discarded Initial keys */ RX_OP_CHECK_PKT_N(6b) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() /* Try again with discarded keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() /* Try again */ RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() /* Try again with discarded 1-RTT keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(6) RX_OP_CHECK_NO_PKT() /* Recreate QRL, test reading packets received before key */ RX_OP_SET_SCID_LEN(0) RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_INJECT_N(6) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET_INITIAL(rx_script_6_c2s_init_dcid) RX_OP_CHECK_PKT_N(6a) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES256GCM, rx_script_6_handshake_secret) RX_OP_CHECK_PKT_N(6b) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES256GCM, rx_script_6_1rtt_secret) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() RX_OP_END }; /* * 7. Real World - S2C Multiple Packets * - Initial, Handshake, 1-RTT (ChaCha20-Poly1305) */ #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) static const QUIC_CONN_ID rx_script_7_c2s_init_dcid = { 4, {0xfa, 0x5d, 0xd6, 0x80} }; static const unsigned char rx_script_7_handshake_secret[32] = { 0x85, 0x44, 0xa4, 0x02, 0x46, 0x5b, 0x2a, 0x92, 0x80, 0x71, 0xfd, 0x11, 0x89, 0x73, 0x84, 0xeb, 0x3e, 0x0d, 0x89, 0x4f, 0x71, 0xdc, 0x9c, 0xdd, 0x55, 0x77, 0x9e, 0x79, 0x7b, 0xeb, 0xfa, 0x86, }; static const unsigned char rx_script_7_1rtt_secret[32] = { 0x4a, 0x77, 0xb6, 0x0e, 0xfd, 0x90, 0xca, 0xbf, 0xc0, 0x1a, 0x64, 0x9f, 0xc0, 0x03, 0xd3, 0x8d, 0xc5, 0x41, 0x04, 0x50, 0xb1, 0x5b, 0x74, 0xe7, 0xe3, 0x99, 0x0c, 0xdf, 0x74, 0x61, 0x35, 0xe6, }; static const unsigned char rx_script_7_in[] = { /* First Packet: Initial */ 0xc2, /* Long, Initial, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x03, 0x45, 0x0c, 0x7a, /* SCID */ 0x00, /* Token Length */ 0x41, 0xcb, /* Length (459) */ 0x3c, 0xe0, /* PN (0) */ 0x85, 0x05, 0xc2, 0x4d, 0x0f, 0xf3, 0x62, 0x51, 0x04, 0x33, 0xfa, 0xb5, 0xa3, 0x02, 0xbd, 0x5c, 0x22, 0x0c, 0x1d, 0xda, 0x06, 0xf1, 0xd7, 0xe0, 0xc8, 0x56, 0xb0, 0x3d, 0xc1, 0x49, 0x8c, 0xc2, 0x88, 0x5a, 0x0e, 0xd5, 0x67, 0x72, 0xec, 0xcc, 0x7a, 0x2b, 0x46, 0x17, 0x49, 0x4b, 0x28, 0x6a, 0x89, 0x71, 0xfd, 0x31, 0x9a, 0xa1, 0x97, 0x64, 0xe2, 0xbf, 0xa0, 0x6d, 0xf6, 0x76, 0x83, 0x28, 0xc4, 0xd5, 0x39, 0x87, 0x22, 0x7c, 0x11, 0x9a, 0x53, 0x66, 0xb4, 0x27, 0xf1, 0xab, 0x6f, 0x49, 0x43, 0x3f, 0x9a, 0x23, 0xd3, 0x53, 0x06, 0xe8, 0x14, 0xfd, 0xc0, 0x67, 0x1f, 0x88, 0x2a, 0xa8, 0xae, 0x5f, 0x05, 0x0a, 0xeb, 0x66, 0x72, 0x8c, 0x46, 0xcc, 0x54, 0x21, 0x5e, 0x14, 0xfe, 0x68, 0xc7, 0xf7, 0x60, 0x67, 0xb5, 0xa7, 0x0d, 0xf4, 0xe1, 0xff, 0x60, 0xe3, 0x11, 0x38, 0x92, 0x90, 0xc2, 0x48, 0x28, 0xbf, 0xf3, 0x85, 0x27, 0xfe, 0xbf, 0x42, 0x26, 0x1a, 0x4e, 0x78, 0xf1, 0xf0, 0x88, 0x16, 0x1b, 0x64, 0x5f, 0x66, 0x02, 0x0b, 0x45, 0x3d, 0x38, 0xd9, 0x09, 0xd5, 0xff, 0xc2, 0x68, 0x02, 0x2c, 0xc4, 0x3f, 0x60, 0x6e, 0x2f, 0x7f, 0x43, 0xf7, 0x1a, 0x37, 0xcc, 0xe0, 0xe0, 0x4b, 0x96, 0xc1, 0xb1, 0x8b, 0x1c, 0x7c, 0x6e, 0x80, 0xe3, 0x92, 0x9b, 0x86, 0x87, 0x1f, 0x9a, 0x6a, 0x62, 0x18, 0xf4, 0x86, 0xc2, 0x3e, 0x33, 0xa3, 0xbf, 0x43, 0x96, 0x6e, 0xff, 0x94, 0xaf, 0x6d, 0x23, 0x5c, 0x42, 0xed, 0xe7, 0xb9, 0x2c, 0x33, 0xb0, 0xc6, 0x3d, 0x44, 0x00, 0x0b, 0xa3, 0x39, 0xa8, 0xeb, 0x8c, 0x81, 0x1a, 0x99, 0x20, 0xbd, 0xfa, 0xf3, 0xf4, 0xf0, 0x11, 0xd8, 0x41, 0x31, 0x8d, 0xdc, 0x0d, 0x00, 0xa6, 0x31, 0x40, 0xc6, 0xc6, 0xad, 0x74, 0x93, 0x62, 0x1c, 0x55, 0xce, 0x5f, 0x8c, 0x5b, 0x3c, 0xcb, 0x25, 0x5e, 0xbf, 0xed, 0xbb, 0x3c, 0x97, 0x4b, 0x62, 0xe0, 0xba, 0xf1, 0xb0, 0x30, 0xbf, 0x35, 0x89, 0x7e, 0x25, 0x61, 0x54, 0x86, 0x52, 0x11, 0x86, 0x90, 0xc3, 0xf5, 0xad, 0xa0, 0x96, 0x30, 0xb2, 0xf0, 0xa6, 0x79, 0x39, 0x1c, 0x51, 0x42, 0xa1, 0x00, 0x6f, 0x55, 0x7d, 0xdc, 0xd0, 0x7c, 0xcf, 0x01, 0x88, 0x03, 0xd7, 0x2d, 0x65, 0x2b, 0x40, 0xee, 0xba, 0x10, 0xd8, 0x0c, 0x85, 0x14, 0xb7, 0x4d, 0x9e, 0x7d, 0x7c, 0xde, 0x7f, 0x0d, 0x0e, 0x3b, 0x3d, 0xe3, 0xd3, 0x63, 0xc2, 0xed, 0xc7, 0x41, 0xaf, 0x05, 0x85, 0x87, 0x46, 0x55, 0x7e, 0xbe, 0x14, 0x5b, 0x98, 0xae, 0x6e, 0x67, 0x1a, 0x65, 0xc6, 0xcf, 0xe1, 0x28, 0x50, 0x6b, 0xb4, 0xf6, 0xba, 0x63, 0xbc, 0xf1, 0xd7, 0xa4, 0x97, 0x2d, 0x4d, 0x04, 0x26, 0x96, 0xec, 0x0c, 0xd4, 0xae, 0x6a, 0xca, 0x7e, 0x65, 0xc5, 0x43, 0x7e, 0xf8, 0x77, 0x61, 0xd0, 0x2c, 0xe5, 0x37, 0x0a, 0xb3, 0x7a, 0x8c, 0x2a, 0xa1, 0xdc, 0x29, 0xdb, 0xec, 0xca, 0xdc, 0xfe, 0xdd, 0x38, 0xd2, 0x13, 0x9f, 0x94, 0x6d, 0x5b, 0x87, 0xf3, 0x15, 0xa8, 0xe5, 0xe9, 0x65, 0x1d, 0x4f, 0x92, 0x1b, 0xf4, 0xa6, 0xa4, 0xd6, 0x22, 0xfc, 0x26, 0x1b, 0x35, 0xa4, 0x1c, 0x88, 0x9f, 0x7d, 0xe0, 0x9a, 0x89, 0x0f, 0x6c, 0xc1, 0xda, 0x6e, 0x45, 0xce, 0x74, 0xb1, 0xff, /* Second Packet: Handshake */ 0xeb, /* Long, Handshake, PN Length=2 bytes */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID */ 0x04, 0x03, 0x45, 0x0c, 0x7a, /* SCID */ 0x42, 0xa3, /* Length (675) */ 0x43, 0x29, /* PN (0) */ 0xff, 0xdb, 0xcf, 0x3c, 0x17, 0xcf, 0xdc, 0x42, 0x3a, 0x59, 0x88, 0xdb, 0x13, 0xef, 0x09, 0x3d, 0xf2, 0x24, 0xf3, 0xeb, 0xca, 0xb0, 0xe1, 0xa4, 0x67, 0x64, 0x65, 0x80, 0x5f, 0x73, 0x29, 0x69, 0x29, 0xba, 0x03, 0x77, 0x22, 0xc8, 0xa8, 0xd5, 0x21, 0xf2, 0xa2, 0x30, 0x7f, 0x86, 0x3a, 0x8a, 0xdd, 0x92, 0x33, 0xa6, 0x57, 0x21, 0x39, 0xdd, 0x34, 0xb4, 0x39, 0xa7, 0x6f, 0x0a, 0x14, 0xba, 0x9e, 0x3b, 0x3a, 0x6a, 0x4b, 0xc5, 0xda, 0x44, 0x82, 0xca, 0x52, 0x86, 0x68, 0x8a, 0x0c, 0x5e, 0xeb, 0x1e, 0x81, 0x43, 0x3a, 0x59, 0x2c, 0x26, 0x63, 0xa3, 0x89, 0x92, 0x80, 0xe9, 0x75, 0xc2, 0xdb, 0xb9, 0x58, 0x6d, 0xab, 0xfd, 0x21, 0xe0, 0x35, 0x79, 0x2e, 0x56, 0x7b, 0xfb, 0xb3, 0x7a, 0x05, 0x33, 0x0f, 0x13, 0xe5, 0xef, 0x04, 0x41, 0x69, 0x85, 0x91, 0x24, 0xce, 0xb5, 0x21, 0x8d, 0x0a, 0x13, 0xda, 0xae, 0x86, 0x2f, 0x25, 0x1f, 0x9c, 0x70, 0x8a, 0xaa, 0x05, 0xeb, 0x30, 0x93, 0x50, 0xc1, 0x39, 0xab, 0x99, 0x8a, 0x31, 0xc1, 0xc1, 0x5e, 0x39, 0xcf, 0x64, 0x3f, 0x9f, 0x5c, 0xa5, 0xa1, 0x88, 0xb2, 0x5f, 0x23, 0xcb, 0x76, 0xe5, 0xf3, 0x2d, 0xa0, 0xed, 0xad, 0xcf, 0x30, 0x05, 0x44, 0xdc, 0xa5, 0x81, 0xb1, 0x7f, 0x78, 0x0d, 0x4d, 0x96, 0xa3, 0xcb, 0xcb, 0x45, 0xcf, 0x5f, 0x22, 0xb8, 0x93, 0x2b, 0x16, 0xe0, 0x1c, 0x53, 0x34, 0x76, 0x3b, 0x7b, 0x78, 0xa1, 0x46, 0x40, 0x43, 0x4b, 0x0e, 0x1c, 0xfd, 0xcf, 0x01, 0xf1, 0x2c, 0xee, 0xd0, 0xbd, 0x9f, 0x44, 0xd2, 0xd7, 0x13, 0xf9, 0x65, 0x82, 0xf5, 0x42, 0xec, 0x9f, 0x5d, 0x51, 0x5a, 0x7b, 0xf2, 0x39, 0xbb, 0xa6, 0x19, 0x5c, 0x73, 0x95, 0x65, 0x5b, 0x64, 0x2f, 0xda, 0x50, 0xd0, 0x02, 0x34, 0x3f, 0x35, 0xc1, 0xd6, 0x31, 0x3b, 0xcf, 0x3f, 0x81, 0x8d, 0xe0, 0x40, 0xfd, 0x6d, 0x32, 0x68, 0xa4, 0xf2, 0x4e, 0x3a, 0x4a, 0x42, 0x2c, 0x07, 0x2d, 0x27, 0xa3, 0x34, 0xe7, 0x27, 0x87, 0x80, 0x76, 0xc0, 0xa0, 0x72, 0x05, 0xf2, 0x88, 0x81, 0xe3, 0x32, 0x00, 0x76, 0x8d, 0x24, 0x5c, 0x97, 0x2d, 0xd6, 0xb8, 0x34, 0xf8, 0x1c, 0x1a, 0x6d, 0xc7, 0x3f, 0xcf, 0x56, 0xae, 0xec, 0x26, 0x74, 0x53, 0x69, 0xcd, 0x7a, 0x97, 0x29, 0xab, 0x12, 0x7d, 0x75, 0xf8, 0x8d, 0x5b, 0xc0, 0x77, 0x20, 0xb6, 0x6a, 0x0b, 0xce, 0x98, 0x50, 0xca, 0x47, 0x42, 0x1e, 0x5d, 0xc3, 0x24, 0x5a, 0x47, 0x48, 0x3b, 0xa0, 0x9e, 0x43, 0xe9, 0x8d, 0x18, 0x23, 0xda, 0x6f, 0x8c, 0xda, 0xd0, 0x3e, 0xdb, 0x37, 0xff, 0xfc, 0x7e, 0x17, 0xbe, 0x42, 0xfd, 0xdb, 0x51, 0xb1, 0xa4, 0xfd, 0x9a, 0x20, 0x27, 0x24, 0x17, 0x04, 0x70, 0xb6, 0x21, 0x87, 0x88, 0xe9, 0xda, 0x63, 0xcb, 0xcb, 0x1d, 0xaf, 0x4a, 0x46, 0x76, 0x88, 0xa1, 0xf8, 0x48, 0x6c, 0x06, 0xb4, 0x62, 0x1a, 0x67, 0x18, 0xb0, 0x1d, 0x58, 0x6a, 0xfe, 0x1f, 0xf1, 0x48, 0xff, 0xcb, 0xa4, 0xd1, 0xa8, 0x12, 0x1f, 0x45, 0x94, 0x2f, 0x55, 0x80, 0x6a, 0x06, 0xcc, 0x7b, 0xb0, 0xcc, 0xb8, 0x06, 0x52, 0x16, 0xe3, 0x6e, 0x7e, 0xb0, 0x42, 0xfd, 0x3b, 0x7e, 0x0a, 0x42, 0x7b, 0x73, 0xaf, 0x2c, 0xf3, 0xbd, 0xe5, 0x72, 0x8c, 0x16, 0xb2, 0xd7, 0x7a, 0x11, 0xb6, 0x9f, 0xd1, 0x69, 0xc1, 0x1a, 0xe0, 0x26, 0x26, 0x13, 0xe2, 0x75, 0xf5, 0x74, 0xae, 0x3f, 0xee, 0x1e, 0x09, 0x63, 0x5a, 0x30, 0x19, 0xa5, 0x59, 0x48, 0x90, 0x9b, 0x46, 0x56, 0xd8, 0x6f, 0x6b, 0x76, 0x82, 0x32, 0xc7, 0x29, 0x76, 0x2e, 0x32, 0xb6, 0x23, 0x99, 0xeb, 0x92, 0x5d, 0xc4, 0x4c, 0xa1, 0xe9, 0x26, 0x37, 0x9a, 0x7d, 0x4c, 0x16, 0x9c, 0x18, 0xe9, 0xc0, 0xff, 0x48, 0x79, 0xb1, 0x7b, 0x0b, 0x1e, 0x6f, 0xb1, 0x77, 0xa5, 0xd2, 0xc6, 0x9a, 0xa9, 0xfc, 0xd1, 0x0f, 0x69, 0xf3, 0xe0, 0x49, 0x70, 0x57, 0x80, 0x86, 0xa7, 0x3f, 0x54, 0xa8, 0x60, 0xfb, 0xe4, 0x06, 0xa3, 0x13, 0xb9, 0x2f, 0xa7, 0x37, 0x80, 0x0c, 0x43, 0xac, 0x2f, 0xae, 0x6e, 0x62, 0x2b, 0x53, 0xe4, 0xfe, 0x58, 0xd7, 0x8b, 0x96, 0xdc, 0xe6, 0xd3, 0x86, 0xb8, 0xd6, 0x42, 0x5b, 0x68, 0x03, 0x48, 0x3f, 0xcd, 0xee, 0x39, 0x8b, 0xc4, 0x53, 0x30, 0x87, 0x48, 0x2a, 0x01, 0x9d, 0x6f, 0x8e, 0x36, 0x75, 0x73, 0xef, 0x77, 0x3a, 0x82, 0xd8, 0x4c, 0x0e, 0x7f, 0xb3, 0x8f, 0x16, 0xd1, 0x10, 0xcf, 0x2f, 0xa3, 0xdf, 0x65, 0xba, 0x91, 0x79, 0xf6, 0x93, 0x60, 0x08, 0xe5, 0xdb, 0x73, 0x02, 0x7a, 0x0b, 0x0e, 0xcc, 0x3b, 0x1f, 0x08, 0x2d, 0x51, 0x3e, 0x87, 0x48, 0xd3, 0xd3, 0x75, 0xc2, 0x28, 0xa3, 0xf3, 0x02, 0xde, 0x8f, 0xa6, 0xbd, 0xb3, 0x19, 0xa0, 0xdb, 0x48, 0x51, 0x03, 0x5f, 0x98, 0xbe, /* Third Packet: 1-RTT */ 0x5c, /* Short, 1-RTT, Spin=0, KP=0, PN Length=2 bytes */ 0x4f, 0x33, /* PN (0) */ 0x16, 0x75, 0x98, 0x67, 0x04, 0x16, 0x61, 0xe3, 0x00, 0xb7, 0x9d, 0x5c, 0x53, 0x4c, 0x26, 0x90, 0x92, 0x8e, 0x0e, 0xc0, 0x9c, 0x6d, 0x8b, 0xac, 0x15, 0x6d, 0x89, 0x74, 0x2f, 0xe7, 0x84, 0xe3, 0x46, 0x46, 0x8c, 0xc1, 0x21, 0x7c, 0x44, 0xa5, 0x00, 0x29, 0xca, 0xf2, 0x11, 0x18, 0xe0, 0x04, 0x40, 0x55, 0xd2, 0xa7, 0xe5, 0x9d, 0x22, 0xa2, 0x2a, 0x6c, 0x03, 0x87, 0xa3, 0xa3, 0xfa, 0xf5, 0x6c, 0xd7, 0x7d, 0xae, 0x3f, 0x28, 0x01, 0xae, 0x06, 0x11, 0x69, 0x67, 0x90, 0x57, 0x5a, 0xd0, 0xeb, 0xdd, 0xac, 0xbd, 0x7f, 0x33, 0x86, 0xbb, }; static const QUIC_PKT_HDR rx_script_7a_expect_hdr = { QUIC_PKT_TYPE_INITIAL, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x03, 0x45, 0x0c, 0x7a}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 441, NULL }; static const unsigned char rx_script_7a_body[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xd5, 0xfb, 0x6a, 0x81, 0x1c, 0xdb, 0xa2, 0x5c, 0x11, 0x31, 0xda, 0x15, 0x28, 0x97, 0x94, 0x83, 0xfd, 0x9d, 0x91, 0x0e, 0x87, 0x71, 0x46, 0x64, 0xb4, 0xd9, 0x9e, 0xbd, 0xa8, 0x48, 0x32, 0xbf, 0x00, 0x13, 0x03, 0x00, 0x00, 0x2e, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0xef, 0xbb, 0x46, 0xe9, 0xb4, 0xf6, 0x54, 0xc4, 0x07, 0x71, 0xdc, 0x50, 0xd5, 0x69, 0x40, 0xbc, 0x85, 0x7f, 0xf9, 0x48, 0x14, 0xe3, 0xd6, 0x08, 0xa9, 0x0b, 0xfd, 0xbe, 0xf1, 0x57, 0x21, 0x34, }; static const QUIC_PKT_HDR rx_script_7b_expect_hdr = { QUIC_PKT_TYPE_HANDSHAKE, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 1, /* Version */ {0, {0}}, /* DCID */ {4, {0x03, 0x45, 0x0c, 0x7a}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 657, NULL }; static const unsigned char rx_script_7b_body[] = { 0x06, 0x00, 0x42, 0x8d, 0x08, 0x00, 0x00, 0x82, 0x00, 0x80, 0x00, 0x10, 0x00, 0x08, 0x00, 0x06, 0x05, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x00, 0x39, 0x00, 0x70, 0x46, 0x0a, 0x0d, 0xdc, 0x59, 0xf0, 0x4e, 0xb2, 0x2c, 0xac, 0x69, 0x6a, 0xc9, 0x77, 0xa9, 0x99, 0x05, 0x04, 0x80, 0x08, 0x00, 0x00, 0x06, 0x04, 0x80, 0x08, 0x00, 0x00, 0x07, 0x04, 0x80, 0x08, 0x00, 0x00, 0x04, 0x04, 0x80, 0x0c, 0x00, 0x00, 0x08, 0x02, 0x40, 0x64, 0x09, 0x02, 0x40, 0x64, 0x01, 0x04, 0x80, 0x00, 0x75, 0x30, 0x03, 0x02, 0x45, 0xac, 0x0b, 0x01, 0x1a, 0x0c, 0x00, 0x02, 0x10, 0x42, 0xf0, 0xed, 0x09, 0x07, 0x5b, 0xd9, 0x5a, 0xb2, 0x39, 0x5d, 0x73, 0x2c, 0x57, 0x1f, 0x50, 0x00, 0x0b, 0xe0, 0x3e, 0xf3, 0xd6, 0x91, 0x6f, 0x9c, 0xcc, 0x31, 0xf7, 0xa5, 0x0e, 0x01, 0x04, 0x0f, 0x04, 0x03, 0x45, 0x0c, 0x7a, 0x10, 0x04, 0xfa, 0x5d, 0xd6, 0x80, 0x20, 0x01, 0x00, 0x0b, 0x00, 0x01, 0x8f, 0x00, 0x00, 0x01, 0x8b, 0x00, 0x01, 0x86, 0x30, 0x82, 0x01, 0x82, 0x30, 0x82, 0x01, 0x29, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x0a, 0x73, 0x0f, 0x86, 0x18, 0xf2, 0xc3, 0x30, 0x01, 0xd2, 0xc0, 0xc1, 0x62, 0x52, 0x13, 0xf1, 0x9c, 0x13, 0x39, 0xb5, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x38, 0x30, 0x32, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x39, 0x30, 0x31, 0x31, 0x32, 0x30, 0x30, 0x31, 0x38, 0x5a, 0x30, 0x17, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0c, 0x6d, 0x61, 0x70, 0x61, 0x6b, 0x74, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x67, 0xf4, 0xd3, 0x8f, 0x15, 0x6d, 0xee, 0x85, 0xcc, 0x2a, 0x77, 0xfc, 0x0b, 0x8f, 0x9f, 0xcf, 0xa9, 0x95, 0x5d, 0x5b, 0xcd, 0xb7, 0x8b, 0xba, 0x31, 0x0a, 0x73, 0x62, 0xc5, 0xd0, 0x0e, 0x07, 0x90, 0xae, 0x38, 0x43, 0x79, 0xce, 0x5e, 0x33, 0xad, 0x31, 0xbf, 0x9f, 0x2a, 0x56, 0x83, 0xa5, 0x24, 0x16, 0xab, 0x0c, 0xf1, 0x64, 0xbe, 0xe4, 0x93, 0xb5, 0x89, 0xd6, 0x05, 0xe4, 0xf7, 0x7b, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x02, 0x64, 0x0f, 0x55, 0x69, 0x14, 0x91, 0x19, 0xed, 0xf9, 0x1a, 0xe9, 0x1d, 0xa5, 0x5a, 0xd0, 0x48, 0x96, 0x9f, 0x60, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x0a, 0x82, 0x92, 0x6e, 0xd3, 0xc6, 0x66, 0xd9, 0xd3, 0x75, 0xff, 0x71, 0x3b, 0x61, 0x46, 0x21, 0x00, 0xe6, 0x21, 0x5d, 0x9c, 0x86, 0xe9, 0x65, 0x40, 0x4f, 0xeb, 0x70, 0x4f, 0x2c, 0xad, 0x00, 0x02, 0x20, 0x08, 0xc2, 0x07, 0x5d, 0x16, 0xfc, 0x54, 0x34, 0x2b, 0xb4, 0x18, 0x67, 0x44, 0x81, 0xc9, 0xa9, 0x67, 0x2e, 0xce, 0xa1, 0x02, 0x9f, 0x3b, 0xe5, 0x61, 0x16, 0x0b, 0x50, 0xf6, 0xa1, 0x50, 0x94, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x4c, 0x04, 0x03, 0x00, 0x48, 0x30, 0x46, 0x02, 0x21, 0x00, 0xaa, 0x18, 0x61, 0x93, 0xdf, 0xbb, 0x79, 0xe7, 0x34, 0x7e, 0x2e, 0x61, 0x13, 0x8c, 0xa0, 0x33, 0xfb, 0x33, 0xca, 0xfc, 0xd2, 0x45, 0xb0, 0xc7, 0x89, 0x3d, 0xf1, 0xd6, 0x54, 0x94, 0x05, 0xb6, 0x02, 0x21, 0x00, 0xef, 0x6c, 0xb6, 0xf2, 0x00, 0xb2, 0x32, 0xb1, 0xf3, 0x3f, 0x59, 0xf5, 0xc8, 0x18, 0xbe, 0x39, 0xbb, 0x27, 0xf8, 0x67, 0xac, 0xcb, 0x63, 0xa4, 0x29, 0xfb, 0x8e, 0x88, 0x0f, 0xe5, 0xe9, 0x7e, 0x14, 0x00, 0x00, 0x20, 0xfc, 0x2c, 0x4c, 0xa7, 0x77, 0x24, 0x79, 0x29, 0xa8, 0x82, 0x1a, 0x4d, 0x58, 0x9d, 0x82, 0xe2, 0x09, 0x36, 0x63, 0x0e, 0x0b, 0x55, 0x51, 0x80, 0x93, 0x40, 0xda, 0x41, 0x33, 0x08, 0x10, 0x2c, }; static const QUIC_PKT_HDR rx_script_7c_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0}, /* PN */ NULL, 0, /* Token/Token Len */ 72, NULL }; static const unsigned char rx_script_7c_body[] = { 0x18, 0x03, 0x00, 0x04, 0xf7, 0x75, 0x72, 0xa2, 0xfd, 0x17, 0xd4, 0x82, 0x8e, 0xe9, 0x5b, 0xce, 0xed, 0xec, 0x88, 0xb9, 0x73, 0xbf, 0x36, 0x9f, 0x18, 0x02, 0x00, 0x04, 0x5f, 0x43, 0x96, 0xe4, 0x15, 0xdc, 0x56, 0x6b, 0x67, 0x4c, 0x36, 0xb2, 0xe2, 0x77, 0xdc, 0x6e, 0xb9, 0x2c, 0x0d, 0x79, 0x18, 0x01, 0x00, 0x04, 0xcb, 0x83, 0x4a, 0xf4, 0x8d, 0x7b, 0x69, 0x90, 0xaf, 0x0d, 0xd2, 0x38, 0xa4, 0xf1, 0x94, 0xff, 0x63, 0x24, 0xd3, 0x7a, }; static const struct rx_test_op rx_script_7[] = { RX_OP_ALLOW_1RTT() RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET_INITIAL(rx_script_7_c2s_init_dcid) RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7a) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_CHACHA20POLY1305, rx_script_7_handshake_secret) RX_OP_CHECK_PKT_N(7b) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_CHACHA20POLY1305, rx_script_7_1rtt_secret) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() /* Discard Initial EL and try injecting the packet again */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_INJECT_N(7) /* Initial packet is not output because we have discarded Initial keys */ RX_OP_CHECK_PKT_N(7b) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() /* Try again with discarded keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() /* Try again */ RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() /* Try again with discarded 1-RTT keys */ RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(7) RX_OP_CHECK_NO_PKT() /* Recreate QRL, test reading packets received before key */ RX_OP_SET_SCID_LEN(0) RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_INJECT_N(7) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET_INITIAL(rx_script_7_c2s_init_dcid) RX_OP_CHECK_PKT_N(7a) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_CHACHA20POLY1305, rx_script_7_handshake_secret) RX_OP_CHECK_PKT_N(7b) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_CHACHA20POLY1305, rx_script_7_1rtt_secret) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() RX_OP_END }; #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */ /* * 8. Real World - S2C Multiple Packets with Peer Initiated Key Phase Update */ static const unsigned char rx_script_8_1rtt_secret[32] = { 0x5f, 0x1f, 0x47, 0xea, 0xc3, 0xb2, 0xce, 0x73, 0xfb, 0xa2, 0x9f, 0xac, 0xc3, 0xa0, 0xfe, 0x9b, 0xf3, 0xc0, 0xde, 0x5d, 0x33, 0x11, 0x1c, 0x70, 0xdd, 0xb4, 0x06, 0xcc, 0xdf, 0x7d, 0xe9, 0x9a }; static const unsigned char rx_script_8a_in[] = { 0x51, /* Short, 1-RTT, PN Length=2 bytes, KP=0 */ 0xcb, 0xf4, /* PN (4) */ 0x3f, 0x68, 0x7b, 0xa8, 0x2b, 0xb9, 0xfa, 0x7d, 0xe4, 0x6b, 0x20, 0x48, 0xd1, 0x3c, 0xcb, 0x4b, 0xef, 0xb1, 0xfd, 0x5e, 0x1b, 0x19, 0x83, 0xa9, 0x47, 0x62, 0xc1, 0x6e, 0xef, 0x27, 0xc3, 0x9b, 0x8f, 0x3f, 0xce, 0x11, 0x68, 0xf5, 0x73, 0x0d, 0xf2, 0xdc, 0xe0, 0x28, 0x28, 0x79, 0xa6, 0x39, 0xc3, 0xb9, 0xd3, }; static const QUIC_PKT_HDR rx_script_8a_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 4}, /* PN */ NULL, 0, /* Token/Token Len */ 35, NULL }; static const unsigned char rx_script_8a_body[] = { 0x02, 0x03, 0x06, 0x00, 0x03, 0x0c, 0x00, 0x1b, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65 }; static const unsigned char rx_script_8b_in[] = { 0x52, /* Short, 1-RTT, PN Length=2 bytes, KP=1 */ 0x21, 0x8e, /* PN (5) */ 0xa2, 0x6a, 0x9c, 0x83, 0x24, 0x48, 0xae, 0x60, 0x1e, 0xc2, 0xa5, 0x91, 0xfa, 0xe5, 0xf2, 0x05, 0x14, 0x37, 0x04, 0x6a, 0xa8, 0xae, 0x06, 0x58, 0xd7, 0x85, 0x48, 0xd7, 0x3b, 0x85, 0x9e, 0x5a, 0xb3, 0x46, 0x89, 0x1b, 0x4b, 0x6e, 0x1d, 0xd1, 0xfc, 0xb7, 0x47, 0xda, 0x6a, 0x64, 0x4b, 0x8e, 0xf2, 0x69, 0x16, }; static const QUIC_PKT_HDR rx_script_8b_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 1, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 5}, /* PN */ NULL, 0, /* Token/Token Len */ 35, NULL }; static const unsigned char rx_script_8b_body[] = { 0x02, 0x04, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x36, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char rx_script_8c_in[] = { 0x5b, /* Short, 1-RTT, PN Length=2 bytes, KP=0 */ 0x98, 0xd6, /* PN (3) */ 0x3c, 0x6f, 0x94, 0x20, 0x5e, 0xfc, 0x5b, 0x3a, 0x4a, 0x65, 0x1a, 0x9a, 0x6c, 0x00, 0x52, 0xb6, 0x0c, 0x9b, 0x07, 0xf9, 0x6f, 0xbc, 0x3d, 0xb4, 0x57, 0xe0, 0x15, 0x74, 0xfe, 0x76, 0xea, 0x1f, 0x23, 0xae, 0x22, 0x62, 0xb7, 0x90, 0x94, 0x89, 0x38, 0x9b, 0x5b, 0x47, 0xed, }; static const QUIC_PKT_HDR rx_script_8c_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 3}, /* PN */ NULL, 0, /* Token/Token Len */ 29, NULL }; static const unsigned char rx_script_8c_body[] = { 0x08, 0x00, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char rx_script_8d_in[] = { 0x55, /* Short, 1-RTT, PN Length=2 bytes, KP=1 */ 0x98, 0x20, /* PN (6) */ 0x45, 0x53, 0x05, 0x29, 0x30, 0x42, 0x29, 0x02, 0xf2, 0xa7, 0x27, 0xd6, 0xb0, 0xb7, 0x30, 0xad, 0x45, 0xd8, 0x73, 0xd7, 0xe3, 0x65, 0xee, 0xd9, 0x35, 0x33, 0x03, 0x3a, 0x35, 0x0b, 0x59, 0xa7, 0xbc, 0x23, 0x37, 0xc2, 0x5e, 0x13, 0x88, 0x18, 0x79, 0x94, 0x6c, 0x15, 0xe3, 0x1f, 0x0d, 0xd1, 0xc3, 0xfa, 0x40, 0xff, }; static const QUIC_PKT_HDR rx_script_8d_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 1, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 6}, /* PN */ NULL, 0, /* Token/Token Len */ 36, NULL }; static const unsigned char rx_script_8d_body[] = { 0x02, 0x05, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x40, 0x51, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char rx_script_8e_in[] = { 0x55, /* Short, 1-RTTT, PN Length=2 bytes, KP=0 */ 0x76, 0x25, /* PN (10) */ 0x1c, 0x0d, 0x70, 0x4c, 0x2b, 0xc5, 0x7d, 0x7b, 0x77, 0x64, 0x03, 0x27, 0xb3, 0x5d, 0x83, 0x9e, 0x35, 0x05, 0x10, 0xd2, 0xa4, 0x5c, 0x83, 0xd6, 0x94, 0x12, 0x18, 0xc5, 0xb3, 0x0f, 0x0a, 0xb1, 0x8a, 0x82, 0x9f, 0xd6, 0xa9, 0xab, 0x40, 0xc1, 0x05, 0xe8, 0x1b, 0x74, 0xaa, 0x8e, 0xd6, 0x8b, 0xa5, 0xa3, 0x77, 0x79, }; static const QUIC_PKT_HDR rx_script_8e_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 0, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 10}, /* PN */ NULL, 0, /* Token/Token Len */ 36, NULL }; static const unsigned char rx_script_8e_body[] = { 0x02, 0x09, 0x04, 0x00, 0x00, 0x0c, 0x00, 0x40, 0xbd, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char rx_script_8f_in[] = { 0x48, /* Short, 1-RTT, PN Length=2 Bytes, KP=1 */ 0x4d, 0xf6, /* PN (15) */ 0x42, 0x86, 0xa1, 0xfa, 0x69, 0x6b, 0x1a, 0x45, 0xf2, 0xcd, 0xf6, 0x92, 0xe1, 0xe6, 0x1a, 0x49, 0x37, 0xd7, 0x10, 0xae, 0x09, 0xbd }; static const QUIC_PKT_HDR rx_script_8f_expect_hdr = { QUIC_PKT_TYPE_1RTT, 0, /* Spin Bit */ 1, /* Key Phase */ 2, /* PN Length */ 0, /* Partial */ 1, /* Fixed */ 0, /* Unused */ 0, /* Reserved */ 0, /* Version */ {0, {0}}, /* DCID */ {0, {0}}, /* SCID */ {0, 15}, /* PN */ NULL, 0, /* Token/Token Len */ 6, NULL }; static const unsigned char rx_script_8f_body[] = { 0x02, 0x0e, 0x4c, 0x54, 0x00, 0x02 }; static const struct rx_test_op rx_script_8[] = { RX_OP_ALLOW_1RTT() RX_OP_SET_RX_DCID(empty_conn_id) /* Inject before we get the keys */ RX_OP_INJECT_N(8a) /* Nothing yet */ RX_OP_CHECK_NO_PKT() /* Provide keys */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, rx_script_8_1rtt_secret) /* Now the injected packet is successfully returned */ RX_OP_CHECK_PKT_N(8a) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(0) RX_OP_CHECK_PKT_EPOCH(0) /* Packet with new key phase */ RX_OP_INJECT_N(8b) /* Packet is successfully decrypted and returned */ RX_OP_CHECK_PKT_N(8b) RX_OP_CHECK_NO_PKT() /* Key epoch has increased */ RX_OP_CHECK_KEY_EPOCH(1) RX_OP_CHECK_PKT_EPOCH(1) /* * Now inject an old packet with the old keys (perhaps reordered in * network). */ RX_OP_INJECT_N(8c) /* Should still be decrypted OK */ RX_OP_CHECK_PKT_N(8c) RX_OP_CHECK_NO_PKT() /* Epoch has not changed */ RX_OP_CHECK_KEY_EPOCH(1) RX_OP_CHECK_PKT_EPOCH(0) /* Another packet with the new keys. */ RX_OP_INJECT_N(8d) RX_OP_CHECK_PKT_N(8d) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) RX_OP_CHECK_PKT_EPOCH(1) /* We can inject the old packet multiple times and it still works */ RX_OP_INJECT_N(8c) RX_OP_CHECK_PKT_N(8c) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) RX_OP_CHECK_PKT_EPOCH(0) /* Until we move from UPDATING to COOLDOWN */ RX_OP_KEY_UPDATE_TIMEOUT(0) RX_OP_INJECT_N(8c) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) /* * Injecting a packet from the next epoch (epoch 2) while in COOLDOWN * doesn't work */ RX_OP_INJECT_N(8e) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) /* Move from COOLDOWN to NORMAL and try again */ RX_OP_KEY_UPDATE_TIMEOUT(1) RX_OP_INJECT_N(8e) RX_OP_CHECK_PKT_N(8e) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(2) RX_OP_CHECK_PKT_EPOCH(2) /* Can still receive old packet */ RX_OP_INJECT_N(8d) RX_OP_CHECK_PKT_N(8d) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(2) RX_OP_CHECK_PKT_EPOCH(1) /* Move straight from UPDATING to NORMAL */ RX_OP_KEY_UPDATE_TIMEOUT(1) /* Try a packet from epoch 3 */ RX_OP_INJECT_N(8f) RX_OP_CHECK_PKT_N(8f) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(3) RX_OP_CHECK_PKT_EPOCH(3) RX_OP_END }; /* 9. 1-RTT Deferral Test */ static const struct rx_test_op rx_script_9[] = { RX_OP_SET_RX_DCID(empty_conn_id) RX_OP_PROVIDE_SECRET_INITIAL(rx_script_5_c2s_init_dcid) RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5a) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, rx_script_5_handshake_secret) RX_OP_CHECK_PKT_N(5b) RX_OP_CHECK_NO_PKT() /* not got secret for next packet yet */ RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, rx_script_5_1rtt_secret) RX_OP_CHECK_NO_PKT() /* still nothing - 1-RTT not enabled */ RX_OP_ALLOW_1RTT() RX_OP_CHECK_PKT_N(5c) /* now we get the 1-RTT packet */ RX_OP_CHECK_NO_PKT() RX_OP_END }; static const struct rx_test_op *rx_scripts[] = { rx_script_1, #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) rx_script_2, #endif rx_script_3, rx_script_4, rx_script_5, rx_script_6, #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) rx_script_7, #endif rx_script_8, rx_script_9 }; struct rx_state { QUIC_DEMUX *demux; /* OSSL_QRX with necessary data */ OSSL_QRX *qrx; OSSL_QRX_ARGS args; /* Used for the RX depacketizer */ SSL_CTX *quic_ssl_ctx; QUIC_CONNECTION *quic_conn; QUIC_CONN_ID rx_dcid; int allow_1rtt; }; static void rx_state_teardown(struct rx_state *s) { if (s->quic_conn != NULL) { SSL_free((SSL *)s->quic_conn); s->quic_conn = NULL; } if (s->quic_ssl_ctx != NULL) { SSL_CTX_free(s->quic_ssl_ctx); s->quic_ssl_ctx = NULL; } if (s->qrx != NULL) { ossl_qrx_free(s->qrx); s->qrx = NULL; } if (s->demux != NULL) { ossl_quic_demux_free(s->demux); s->demux = NULL; } } static uint64_t time_counter = 0; static OSSL_TIME expected_time(uint64_t counter) { return ossl_time_multiply(ossl_ticks2time(OSSL_TIME_MS), counter); } static OSSL_TIME fake_time(void *arg) { return expected_time(++time_counter); } static void demux_default_handler(QUIC_URXE *e, void *arg, const QUIC_CONN_ID *dcid) { struct rx_state *s = arg; if (dcid == NULL || !ossl_quic_conn_id_eq(dcid, &s->rx_dcid)) return; ossl_qrx_inject_urxe(s->qrx, e); } static int rx_state_ensure(struct rx_state *s) { if (s->demux == NULL && !TEST_ptr(s->demux = ossl_quic_demux_new(NULL, s->args.short_conn_id_len, fake_time, NULL))) return 0; s->args.demux = s->demux; s->args.max_deferred = 32; /* Initialise OSSL_QRX */ if (s->qrx == NULL && !TEST_ptr(s->qrx = ossl_qrx_new(&s->args))) return 0; ossl_quic_demux_set_default_handler(s->demux, demux_default_handler, s); if (s->allow_1rtt) ossl_qrx_allow_1rtt_processing(s->qrx); return 1; } static int rx_run_script(const struct rx_test_op *script) { int testresult = 0; struct rx_state s = {0}; size_t i; OSSL_QRX_PKT *pkt = NULL; const struct rx_test_op *op = script; uint64_t last_key_epoch = UINT64_MAX; for (; op->op != RX_TEST_OP_END; ++op) switch (op->op) { case RX_TEST_OP_SET_SCID_LEN: rx_state_teardown(&s); s.args.short_conn_id_len = op->enc_level; break; case RX_TEST_OP_SET_INIT_LARGEST_PN: rx_state_teardown(&s); for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) s.args.init_largest_pn[i] = op->largest_pn; break; case RX_TEST_OP_SET_RX_DCID: if (!TEST_true(rx_state_ensure(&s))) goto err; s.rx_dcid = *op->dcid; break; case RX_TEST_OP_PROVIDE_SECRET: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_qrx_provide_secret(s.qrx, op->enc_level, op->suite_id, NULL, op->buf, op->buf_len))) goto err; break; case RX_TEST_OP_PROVIDE_SECRET_INITIAL: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_quic_provide_initial_secret(NULL, NULL, op->dcid, 0, s.qrx, NULL))) goto err; break; case RX_TEST_OP_DISCARD_EL: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_qrx_discard_enc_level(s.qrx, op->enc_level))) goto err; break; case RX_TEST_OP_INJECT: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_quic_demux_inject(s.demux, op->buf, op->buf_len, NULL, NULL))) goto err; break; case RX_TEST_OP_CHECK_PKT: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_qrx_read_pkt(s.qrx, &pkt))) goto err; if (!TEST_ptr(pkt) || !TEST_ptr(pkt->hdr)) goto err; if (!TEST_mem_eq(pkt->hdr->data, pkt->hdr->len, op->buf, op->buf_len)) goto err; if (!TEST_true(cmp_pkt_hdr(pkt->hdr, op->hdr, op->buf, op->buf_len, 1))) goto err; last_key_epoch = pkt->key_epoch; ossl_qrx_pkt_release(pkt); pkt = NULL; break; case RX_TEST_OP_CHECK_NO_PKT: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_false(ossl_qrx_read_pkt(s.qrx, &pkt))) goto err; break; case RX_TEST_OP_CHECK_KEY_EPOCH: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_uint64_t_eq(ossl_qrx_get_key_epoch(s.qrx), op->largest_pn)) goto err; break; case RX_TEST_OP_CHECK_PKT_EPOCH: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_uint64_t_eq(last_key_epoch, op->largest_pn)) goto err; break; case RX_TEST_OP_KEY_UPDATE_TIMEOUT: if (!TEST_true(rx_state_ensure(&s))) goto err; if (!TEST_true(ossl_qrx_key_update_timeout(s.qrx, op->enc_level))) goto err; break; case RX_TEST_OP_SET_INIT_KEY_PHASE: rx_state_teardown(&s); s.args.init_key_phase_bit = (unsigned char)op->enc_level; break; case RX_TEST_OP_ALLOW_1RTT: s.allow_1rtt = 1; if (!TEST_true(rx_state_ensure(&s))) goto err; break; default: OPENSSL_assert(0); goto err; } testresult = 1; err: ossl_qrx_pkt_release(pkt); rx_state_teardown(&s); return testresult; } static int test_rx_script(int idx) { return rx_run_script(rx_scripts[idx]); } /* Packet Header Tests */ struct pkt_hdr_test { QUIC_PKT_HDR hdr; const unsigned char *expected; size_t expected_len; const unsigned char *payload; size_t payload_len; size_t short_conn_id_len; /* * Minimum number of bytes which should be required for a successful decode. * SIZE_MAX if should never decode successfully. */ size_t min_success_len; size_t pn_offset, sample_offset; }; /* Packet Header Test 1: INITIAL With SCID */ static const unsigned char pkt_hdr_test_1_expected[] = { 0xc1, /* Long|Fixed, Type=Initial, PN Len=2 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID Length */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x00, /* Token Length */ 0x15, /* Length=21 */ 0x33, 0x44, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_1_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_1 = { { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 0, {0} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33, 0x44 }, /* PN */ NULL, 0, /* Token/Token Len */ 19, NULL /* Len/Data */ }, pkt_hdr_test_1_expected, OSSL_NELEM(pkt_hdr_test_1_expected), pkt_hdr_test_1_payload, OSSL_NELEM(pkt_hdr_test_1_payload), 0, sizeof(pkt_hdr_test_1_expected), 17, 21 }; /* Packet Header Test 2: INITIAL With SCID and Token */ static const unsigned char pkt_hdr_test_2_expected[] = { 0xc1, /* Long|Fixed, Type=Initial, PN Len=2 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID Length */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x07, /* Token Length */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x15, /* Length=21 */ 0x33, 0x44, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_2_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_2_token[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96 }; static const struct pkt_hdr_test pkt_hdr_test_2 = { { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 0, {0} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33, 0x44 }, /* PN */ pkt_hdr_test_2_token, sizeof(pkt_hdr_test_2_token), /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_2_expected, OSSL_NELEM(pkt_hdr_test_2_expected), pkt_hdr_test_2_payload, OSSL_NELEM(pkt_hdr_test_2_payload), 0, sizeof(pkt_hdr_test_2_expected), 24, 28 }; /* Packet Header Test 3: INITIAL With DCID and SCID and Token */ static const unsigned char pkt_hdr_test_3_expected[] = { 0xc1, /* Long|Fixed, Type=Initial, PN Len=2 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x06, /* Token Length */ 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x15, /* Length=21 */ 0x33, 0x44, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_3_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_3_token[] = { 0x91, 0x92, 0x93, 0x94, 0x95, 0x96 }; static const struct pkt_hdr_test pkt_hdr_test_3 = { { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33, 0x44 }, /* PN */ pkt_hdr_test_3_token, sizeof(pkt_hdr_test_3_token), /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_3_expected, OSSL_NELEM(pkt_hdr_test_3_expected), pkt_hdr_test_3_payload, OSSL_NELEM(pkt_hdr_test_3_payload), 0, sizeof(pkt_hdr_test_3_expected), 26, 30 }; /* Packet Header Test 4: 0-RTT */ static const unsigned char pkt_hdr_test_4_expected[] = { 0xd0, /* Long|Fixed, Type=0-RTT, PN Len=1 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_4_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_4 = { { QUIC_PKT_TYPE_0RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 1, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33 }, /* PN */ NULL, 0, /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_4_expected, OSSL_NELEM(pkt_hdr_test_4_expected), pkt_hdr_test_4_payload, OSSL_NELEM(pkt_hdr_test_4_payload), 0, sizeof(pkt_hdr_test_4_expected), 19, 23 }; /* Packet Header Test 5: Handshake */ static const unsigned char pkt_hdr_test_5_expected[] = { 0xe0, /* Long|Fixed, Type=Handshake, PN Len=1 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_5_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_5 = { { QUIC_PKT_TYPE_HANDSHAKE, /* type */ 0, /* spin bit */ 0, /* key phase */ 1, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33 }, /* PN */ NULL, 0, /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_5_expected, OSSL_NELEM(pkt_hdr_test_5_expected), pkt_hdr_test_5_payload, OSSL_NELEM(pkt_hdr_test_5_payload), 0, sizeof(pkt_hdr_test_5_expected), 19, 23 }; /* Packet Header Test 6: Retry */ static const unsigned char pkt_hdr_test_6_expected[] = { 0xf0, /* Long|Fixed, Type=Retry */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* Retry Token */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f /* Retry Integrity Tag */ }; static const unsigned char pkt_hdr_test_6_payload[] = { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* Retry Token */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f /* Retry Integrity Tag */ }; static const struct pkt_hdr_test pkt_hdr_test_6 = { { QUIC_PKT_TYPE_RETRY, /* type */ 0, /* spin bit */ 0, /* key phase */ 0, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 24, NULL /* Len/Data */ }, pkt_hdr_test_6_expected, OSSL_NELEM(pkt_hdr_test_6_expected), pkt_hdr_test_6_payload, OSSL_NELEM(pkt_hdr_test_6_payload), 0, 21, SIZE_MAX, SIZE_MAX }; /* Packet Header Test 7: 1-RTT */ static const unsigned char pkt_hdr_test_7_expected[] = { 0x42, /* Short|Fixed, Type=1-RTT, PN Len=3 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const unsigned char pkt_hdr_test_7_payload[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_7 = { { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 3, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 0, {0} }, /* SCID */ { 0x50, 0x51, 0x52 }, /* PN */ NULL, 0, /* Token */ 18, NULL /* Len/Data */ }, pkt_hdr_test_7_expected, OSSL_NELEM(pkt_hdr_test_7_expected), pkt_hdr_test_7_payload, OSSL_NELEM(pkt_hdr_test_7_payload), 3, 21, 4, 8 }; /* Packet Header Test 8: 1-RTT with Spin Bit */ static const unsigned char pkt_hdr_test_8_expected[] = { 0x62, /* Short|Fixed, Type=1-RTT, PN Len=3, Spin=1 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const unsigned char pkt_hdr_test_8_payload[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_8 = { { QUIC_PKT_TYPE_1RTT, /* type */ 1, /* spin bit */ 0, /* key phase */ 3, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 0, {0} }, /* SCID */ { 0x50, 0x51, 0x52 }, /* PN */ NULL, 0, /* Token */ 18, NULL /* Len/Data */ }, pkt_hdr_test_8_expected, OSSL_NELEM(pkt_hdr_test_8_expected), pkt_hdr_test_8_payload, OSSL_NELEM(pkt_hdr_test_8_payload), 3, 21, 4, 8 }; /* Packet Header Test 9: 1-RTT with Key Phase Bit */ static const unsigned char pkt_hdr_test_9_expected[] = { 0x46, /* Short|Fixed, Type=1-RTT, PN Len=3, Key Phase=1 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const unsigned char pkt_hdr_test_9_payload[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_9 = { { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 1, /* key phase */ 3, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 0, {0} }, /* SCID */ { 0x50, 0x51, 0x52 }, /* PN */ NULL, 0, /* Token */ 18, NULL /* Len/Data */ }, pkt_hdr_test_9_expected, OSSL_NELEM(pkt_hdr_test_9_expected), pkt_hdr_test_9_payload, OSSL_NELEM(pkt_hdr_test_9_payload), 3, 21, 4, 8 }; /* Packet Header Test 10: Handshake with 4-Byte PN */ static const unsigned char pkt_hdr_test_10_expected[] = { 0xe3, /* Long|Fixed, Type=Handshake, PN Len=4 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x17, /* Length=20 */ 0x33, 0x44, 0x55, 0x66, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_10_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_10 = { { QUIC_PKT_TYPE_HANDSHAKE, /* type */ 0, /* spin bit */ 0, /* key phase */ 4, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33, 0x44, 0x55, 0x66 }, /* PN */ NULL, 0, /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_10_expected, OSSL_NELEM(pkt_hdr_test_10_expected), pkt_hdr_test_10_payload, OSSL_NELEM(pkt_hdr_test_10_payload), 0, sizeof(pkt_hdr_test_10_expected), 19, 23 }; /* Packet Header Test 11: 1-RTT with 4-Byte PN */ static const unsigned char pkt_hdr_test_11_expected[] = { 0x43, /* Short|Fixed, Type=1-RTT, PN Len=4 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, 0x53, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const unsigned char pkt_hdr_test_11_payload[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_11 = { { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 4, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 0, {0} }, /* SCID */ { 0x50, 0x51, 0x52, 0x53 }, /* PN */ NULL, 0, /* Token */ 18, NULL /* Len/Data */ }, pkt_hdr_test_11_expected, OSSL_NELEM(pkt_hdr_test_11_expected), pkt_hdr_test_11_payload, OSSL_NELEM(pkt_hdr_test_11_payload), 3, 21, 4, 8 }; /* Packet Header Test 12: Version Negotiation */ static const unsigned char pkt_hdr_test_12_expected[] = { 0xc0, /* Long|Fixed, Type=Version Neg */ 0x00, 0x00, 0x00, 0x00, /* Version (0) */ 0x03, 0x70, 0x71, 0x72, /* DCID */ 0x02, 0x81, 0x82, /* SCID */ 0x11, 0x22, 0x33, 0x44 /* One Version */ }; static const unsigned char pkt_hdr_test_12_payload[] = { 0x11, 0x22, 0x33, 0x44 }; static const struct pkt_hdr_test pkt_hdr_test_12 = { { QUIC_PKT_TYPE_VERSION_NEG, /* type */ 0, /* spin bit */ 0, /* key phase */ 0, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 2, {0x81, 0x82} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 4, NULL /* Len/Data */ }, pkt_hdr_test_12_expected, OSSL_NELEM(pkt_hdr_test_12_expected), pkt_hdr_test_12_payload, OSSL_NELEM(pkt_hdr_test_12_payload), 0, 12, SIZE_MAX, SIZE_MAX }; /* Packet Header Test 13: Version Negotiation without Fixed Bit */ static const unsigned char pkt_hdr_test_13_expected[] = { 0x80, /* Long|Fixed, Type=Version Neg */ 0x00, 0x00, 0x00, 0x00, /* Version (0) */ 0x03, 0x70, 0x71, 0x72, /* DCID */ 0x02, 0x81, 0x82, /* SCID */ 0x11, 0x22, 0x33, 0x44 /* One Version */ }; static const unsigned char pkt_hdr_test_13_payload[] = { 0x11, 0x22, 0x33, 0x44 }; static const struct pkt_hdr_test pkt_hdr_test_13 = { { QUIC_PKT_TYPE_VERSION_NEG, /* type */ 0, /* spin bit */ 0, /* key phase */ 0, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 2, {0x81, 0x82} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 4, NULL /* Len/Data */ }, pkt_hdr_test_13_expected, OSSL_NELEM(pkt_hdr_test_13_expected), pkt_hdr_test_13_payload, OSSL_NELEM(pkt_hdr_test_13_payload), 0, 12, SIZE_MAX, SIZE_MAX }; /* Packet Header Test 14: 1-RTT - Malformed - No Fixed Bit */ static const unsigned char pkt_hdr_test_14_expected[] = { 0x02, /* Fixed, Type=1-RTT, PN Len=3 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_14 = { { 0 }, pkt_hdr_test_14_expected, OSSL_NELEM(pkt_hdr_test_14_expected), NULL, 0, 3, SIZE_MAX, 4, 8 }; /* Packet Header Test 15: Handshake - Malformed - No Fixed Bit */ static const unsigned char pkt_hdr_test_15_expected[] = { 0xa0, /* Long, Type=Handshake, PN Len=1 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_15 = { { 0 }, pkt_hdr_test_15_expected, OSSL_NELEM(pkt_hdr_test_15_expected), NULL, 0, 0, SIZE_MAX, 19, 23 }; /* Packet Header Test 16: Handshake - Malformed - Wrong Version */ static const unsigned char pkt_hdr_test_16_expected[] = { 0xe0, /* Long|Fixed, Type=Handshake, PN Len=1 */ 0x00, 0x00, 0x00, 0x02, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_16 = { { 0 }, pkt_hdr_test_16_expected, OSSL_NELEM(pkt_hdr_test_16_expected), NULL, 0, 0, SIZE_MAX, 19, 23 }; /* Packet Header Test 17: Initial - Non-Zero Reserved Bits */ static const unsigned char pkt_hdr_test_17_expected[] = { 0xcd, /* Long|Fixed, Type=Initial, PN Len=2 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x00, /* DCID Length */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x00, /* Token Length */ 0x15, /* Length=21 */ 0x33, 0x44, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_17_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_17 = { { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 3, /* reserved */ 1, /* version */ { 0, {0} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33, 0x44 }, /* PN */ NULL, 0, /* Token/Token Len */ 19, NULL /* Len/Data */ }, pkt_hdr_test_17_expected, OSSL_NELEM(pkt_hdr_test_17_expected), pkt_hdr_test_17_payload, OSSL_NELEM(pkt_hdr_test_17_payload), 0, sizeof(pkt_hdr_test_17_expected), 17, 21 }; /* Packet Header Test 18: 0-RTT - Non-Zero Reserved Bits */ static const unsigned char pkt_hdr_test_18_expected[] = { 0xd8, /* Long|Fixed, Type=0-RTT, PN Len=1 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_18_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_18 = { { QUIC_PKT_TYPE_0RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 1, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 2, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33 }, /* PN */ NULL, 0, /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_18_expected, OSSL_NELEM(pkt_hdr_test_18_expected), pkt_hdr_test_18_payload, OSSL_NELEM(pkt_hdr_test_18_payload), 0, sizeof(pkt_hdr_test_18_expected), 19, 23 }; /* Packet Header Test 19: Handshake - Non-Zero Reserved Bits */ static const unsigned char pkt_hdr_test_19_expected[] = { 0xe4, /* Long|Fixed, Type=Handshake, PN Len=1 */ 0x00, 0x00, 0x00, 0x01, /* Version */ 0x03, /* DCID Length */ 0x70, 0x71, 0x72, /* DCID */ 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID Length, SCID */ 0x14, /* Length=20 */ 0x33, /* Encoded PN */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* Payload */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const unsigned char pkt_hdr_test_19_payload[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22 }; static const struct pkt_hdr_test pkt_hdr_test_19 = { { QUIC_PKT_TYPE_HANDSHAKE, /* type */ 0, /* spin bit */ 0, /* key phase */ 1, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 1, /* reserved */ 1, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, /* SCID */ { 0x33 }, /* PN */ NULL, 0, /* Token */ 19, NULL /* Len/Data */ }, pkt_hdr_test_19_expected, OSSL_NELEM(pkt_hdr_test_19_expected), pkt_hdr_test_19_payload, OSSL_NELEM(pkt_hdr_test_19_payload), 0, sizeof(pkt_hdr_test_19_expected), 19, 23 }; /* Packet Header Test 20: 1-RTT with Non-Zero Reserved Bits */ static const unsigned char pkt_hdr_test_20_expected[] = { 0x5a, /* Short|Fixed, Type=1-RTT, PN Len=3 */ 0x70, 0x71, 0x72, /* DCID */ 0x50, 0x51, 0x52, /* PN */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const unsigned char pkt_hdr_test_20_payload[] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1 }; static const struct pkt_hdr_test pkt_hdr_test_20 = { { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 3, /* PN length */ 0, /* partial */ 1, /* fixed */ 0, /* unused */ 3, /* reserved */ 0, /* version */ { 3, {0x70, 0x71, 0x72} }, /* DCID */ { 0, {0} }, /* SCID */ { 0x50, 0x51, 0x52 }, /* PN */ NULL, 0, /* Token */ 18, NULL /* Len/Data */ }, pkt_hdr_test_20_expected, OSSL_NELEM(pkt_hdr_test_20_expected), pkt_hdr_test_20_payload, OSSL_NELEM(pkt_hdr_test_20_payload), 3, 21, 4, 8 }; static const struct pkt_hdr_test *const pkt_hdr_tests[] = { &pkt_hdr_test_1, &pkt_hdr_test_2, &pkt_hdr_test_3, &pkt_hdr_test_4, &pkt_hdr_test_5, &pkt_hdr_test_6, &pkt_hdr_test_7, &pkt_hdr_test_8, &pkt_hdr_test_9, &pkt_hdr_test_10, &pkt_hdr_test_11, &pkt_hdr_test_12, &pkt_hdr_test_13, &pkt_hdr_test_14, &pkt_hdr_test_15, &pkt_hdr_test_16, &pkt_hdr_test_17, &pkt_hdr_test_18, &pkt_hdr_test_19, &pkt_hdr_test_20 }; #define HPR_REPEAT_COUNT 4 #define HPR_CIPHER_COUNT 3 /* * Count of number of times we observed an unchanged (u) or changed (c) bit in * each header-protectable bit over all test suites. */ static unsigned int counts_u[HPR_CIPHER_COUNT][37] = {0}; static unsigned int counts_c[HPR_CIPHER_COUNT][37] = {0}; #define TEST_PKT_BUF_LEN 20000 static int test_wire_pkt_hdr_actual(int tidx, int repeat, int cipher, size_t trunc_len) { int testresult = 0; const struct pkt_hdr_test *t = pkt_hdr_tests[tidx]; QUIC_PKT_HDR hdr = {0}; QUIC_PKT_HDR_PTRS ptrs = {0}, wptrs = {0}; PACKET pkt = {0}; WPACKET wpkt = {0}; unsigned char *buf = NULL; size_t l = 0, i, j; QUIC_HDR_PROTECTOR hpr = {0}; unsigned char hpr_key[32] = {0,1,2,3,4,5,6,7}; int have_hpr = 0, hpr_cipher_id, hpr_key_len; unsigned char *hbuf = NULL; int is_trunc = trunc_len < t->expected_len; int expect_fail = trunc_len < t->min_success_len; hpr_key[8] = (unsigned char)tidx; hpr_key[9] = (unsigned char)repeat; if (is_trunc && trunc_len > t->min_success_len && t->hdr.type == QUIC_PKT_TYPE_VERSION_NEG && ((trunc_len - t->min_success_len) % 4) != 0) expect_fail = 1; switch (cipher) { case 0: hpr_cipher_id = QUIC_HDR_PROT_CIPHER_AES_128; hpr_key_len = 16; break; case 1: hpr_cipher_id = QUIC_HDR_PROT_CIPHER_AES_256; hpr_key_len = 32; break; case 2: /* * In a build without CHACHA, we rerun the AES 256 tests. * Removing all dependence on CHACHA is more difficult and these * tests are fast enough. */ #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) hpr_cipher_id = QUIC_HDR_PROT_CIPHER_CHACHA; #else hpr_cipher_id = QUIC_HDR_PROT_CIPHER_AES_256; #endif hpr_key_len = 32; break; default: goto err; } if (!TEST_ptr(buf = OPENSSL_malloc(TEST_PKT_BUF_LEN))) goto err; if (!TEST_true(WPACKET_init_static_len(&wpkt, buf, TEST_PKT_BUF_LEN, 0))) goto err; if (!TEST_true(PACKET_buf_init(&pkt, t->expected, trunc_len))) goto err; if (!TEST_int_eq(ossl_quic_wire_decode_pkt_hdr(&pkt, t->short_conn_id_len, 0, 0, &hdr, &ptrs), !expect_fail)) goto err; if (!expect_fail && !is_trunc) { if (!TEST_true(cmp_pkt_hdr(&hdr, &t->hdr, t->payload, t->payload_len, 1))) goto err; if (!TEST_ptr_eq(ptrs.raw_start, t->expected)) goto err; if (t->pn_offset == SIZE_MAX) { if (!TEST_ptr_null(ptrs.raw_pn)) goto err; } else { if (!TEST_ptr_eq(ptrs.raw_pn, t->expected + t->pn_offset)) goto err; } if (t->sample_offset != SIZE_MAX) { if (!TEST_ptr_eq(ptrs.raw_sample, t->expected + t->sample_offset)) goto err; if (!TEST_size_t_eq(ptrs.raw_sample_len, t->expected_len - t->sample_offset)) goto err; } if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(&wpkt, t->short_conn_id_len, &hdr, &wptrs))) goto err; if (!TEST_true(WPACKET_memcpy(&wpkt, t->payload, t->payload_len))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &l))) goto err; if (!TEST_mem_eq(buf, l, t->expected, t->expected_len)) goto err; /* Test header protection. */ if (t->sample_offset != SIZE_MAX) { /* if packet type has protection */ if (!TEST_true(ossl_quic_hdr_protector_init(&hpr, NULL, NULL, hpr_cipher_id, hpr_key, hpr_key_len))) goto err; have_hpr = 1; /* * Copy into a duplicate buffer to test header protection by * comparing it against the original. */ hbuf = OPENSSL_malloc(t->expected_len); if (!TEST_ptr(hbuf)) goto err; memcpy(hbuf, t->expected, t->expected_len); /* Fixup pointers to new buffer and encrypt. */ ptrs.raw_pn = hbuf + (ptrs.raw_pn - ptrs.raw_start); ptrs.raw_sample = hbuf + (ptrs.raw_sample - ptrs.raw_start); ptrs.raw_start = hbuf; if (!TEST_true(ossl_quic_hdr_protector_encrypt(&hpr, &ptrs))) goto err; /* Ensure that bytes which should not have changed did not change */ for (i = 0; i < t->expected_len; ++i) { unsigned char d = t->expected[i] ^ hbuf[i], rej_mask = 0xff; size_t jrel = 0; if (i == 0) { /* Bits in first byte which must not change */ rej_mask = (t->hdr.type == QUIC_PKT_TYPE_1RTT) ? ~0x1f : ~0xf; } else if (i >= t->pn_offset && i < t->pn_offset + t->hdr.pn_len) { /* PN bytes change */ rej_mask = 0; jrel = 5 + (i - t->pn_offset) * 8; } if (rej_mask != 0xff) for (j = 0; j < 8; ++j) { if (((1U << j) & rej_mask) != 0) /* * Bit unrelated to header protection, do not record * stats about it. */ continue; OPENSSL_assert(jrel + j < OSSL_NELEM(counts_u[cipher])); if ((d & (1U << j)) != 0) ++counts_c[cipher][jrel + j]; /* bit did change */ else ++counts_u[cipher][jrel + j]; /* bit did not change */ } /* Bits in rej_mask must not change */ if (!TEST_int_eq(d & rej_mask, 0)) goto err; } /* Decrypt and check matches original. */ if (!TEST_true(ossl_quic_hdr_protector_decrypt(&hpr, &ptrs))) goto err; if (!TEST_mem_eq(hbuf, t->expected_len, t->expected, t->expected_len)) goto err; } } testresult = 1; err: if (have_hpr) ossl_quic_hdr_protector_cleanup(&hpr); WPACKET_finish(&wpkt); OPENSSL_free(buf); OPENSSL_free(hbuf); return testresult; } static int test_wire_pkt_hdr_inner(int tidx, int repeat, int cipher) { int testresult = 0; const struct pkt_hdr_test *t = pkt_hdr_tests[tidx]; size_t i; /* Test with entire packet */ if (!TEST_true(test_wire_pkt_hdr_actual(tidx, repeat, cipher, t->expected_len))) goto err; /* Now repeat for every possible truncation of the packet */ for (i = 0; i < t->expected_len; ++i) if (!TEST_true(test_wire_pkt_hdr_actual(tidx, repeat, cipher, i))) goto err; testresult = 1; err: return testresult; } static int test_hdr_prot_stats(void) { int testresult = 0; size_t i, cipher; /* * Test that, across all previously executed tests for each header * protection cipher, every bit which can have header protection applied a) * was changed in at least one test of applying header protection, and b) * was unchanged in at least one test of applying header protection. */ for (cipher = 0; cipher < HPR_CIPHER_COUNT; ++cipher) for (i = 0; i < OSSL_NELEM(counts_u[0]); ++i) { if (!TEST_uint_gt(counts_u[cipher][i], 0)) goto err; if (!TEST_uint_gt(counts_c[cipher][i], 0)) goto err; } testresult = 1; err: return testresult; } #define NUM_WIRE_PKT_HDR_TESTS \ (OSSL_NELEM(pkt_hdr_tests) * HPR_REPEAT_COUNT * HPR_CIPHER_COUNT) static int test_wire_pkt_hdr(int idx) { int tidx, repeat, cipher; if (idx == NUM_WIRE_PKT_HDR_TESTS) return test_hdr_prot_stats(); cipher = idx % HPR_CIPHER_COUNT; idx /= HPR_CIPHER_COUNT; repeat = idx % HPR_REPEAT_COUNT; idx /= HPR_REPEAT_COUNT; tidx = idx; return test_wire_pkt_hdr_inner(tidx, repeat, cipher); } /* TX Tests */ #define TX_TEST_OP_END 0 /* end of script */ #define TX_TEST_OP_WRITE 1 /* write packet */ #define TX_TEST_OP_PROVIDE_SECRET 2 /* provide TX secret */ #define TX_TEST_OP_PROVIDE_SECRET_INITIAL 3 /* provide TX secret for initial */ #define TX_TEST_OP_DISCARD_EL 4 /* discard an encryption level */ #define TX_TEST_OP_CHECK_DGRAM 5 /* read datagram, compare to expected */ #define TX_TEST_OP_CHECK_NO_DGRAM 6 /* check no datagram is in queue */ #define TX_TEST_OP_KEY_UPDATE 7 /* perform key update for 1-RTT */ struct tx_test_op { unsigned char op; const unsigned char *buf; size_t buf_len; const OSSL_QTX_PKT *pkt; uint32_t enc_level, suite_id; const QUIC_CONN_ID *dcid; }; #define TX_OP_END \ { TX_TEST_OP_END } #define TX_OP_WRITE(pkt) \ { TX_TEST_OP_WRITE, NULL, 0, &(pkt), 0, 0, NULL }, #define TX_OP_PROVIDE_SECRET(el, suite, key) \ { \ TX_TEST_OP_PROVIDE_SECRET, (key), sizeof(key), \ NULL, (el), (suite), NULL \ }, #define TX_OP_PROVIDE_SECRET_INITIAL(dcid, is_server) \ { TX_TEST_OP_PROVIDE_SECRET_INITIAL, \ NULL, 0, NULL, 0, (is_server), &(dcid) }, #define TX_OP_DISCARD_EL(el) \ { TX_TEST_OP_DISCARD_EL, NULL, 0, NULL, (el), 0, NULL }, #define TX_OP_CHECK_DGRAM(expect_dgram) \ { \ TX_TEST_OP_CHECK_DGRAM, (expect_dgram), sizeof(expect_dgram), \ NULL, 0, 0, NULL \ }, #define TX_OP_CHECK_NO_DGRAM() \ { TX_TEST_OP_CHECK_NO_PKT, NULL, 0, NULL, 0, 0, NULL }, #define TX_OP_WRITE_N(n) \ TX_OP_WRITE(tx_script_##n##_pkt) #define TX_OP_CHECK_DGRAM_N(n) \ TX_OP_CHECK_DGRAM(tx_script_##n##_dgram) #define TX_OP_WRITE_CHECK(n) \ TX_OP_WRITE_N(n) \ TX_OP_CHECK_DGRAM_N(n) #define TX_OP_KEY_UPDATE() \ { TX_TEST_OP_KEY_UPDATE, NULL, 0, NULL, 0, 0, NULL }, /* 1. RFC 9001 - A.2 Client Initial */ static const unsigned char tx_script_1_body[1162] = { 0x06, 0x00, 0x40, 0xf1, 0x01, 0x00, 0x00, 0xed, 0x03, 0x03, 0xeb, 0xf8, 0xfa, 0x56, 0xf1, 0x29, 0x39, 0xb9, 0x58, 0x4a, 0x38, 0x96, 0x47, 0x2e, 0xc4, 0x0b, 0xb8, 0x63, 0xcf, 0xd3, 0xe8, 0x68, 0x04, 0xfe, 0x3a, 0x47, 0xf0, 0x6a, 0x2b, 0x69, 0x48, 0x4c, 0x00, 0x00, 0x04, 0x13, 0x01, 0x13, 0x02, 0x01, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x00, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, 0x10, 0x00, 0x07, 0x00, 0x05, 0x04, 0x61, 0x6c, 0x70, 0x6e, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x93, 0x70, 0xb2, 0xc9, 0xca, 0xa4, 0x7f, 0xba, 0xba, 0xf4, 0x55, 0x9f, 0xed, 0xba, 0x75, 0x3d, 0xe1, 0x71, 0xfa, 0x71, 0xf5, 0x0f, 0x1c, 0xe1, 0x5d, 0x43, 0xe9, 0x94, 0xec, 0x74, 0xd7, 0x48, 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00, 0x0d, 0x00, 0x10, 0x00, 0x0e, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x02, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x00, 0x2d, 0x00, 0x02, 0x01, 0x01, 0x00, 0x1c, 0x00, 0x02, 0x40, 0x01, 0x00, 0x39, 0x00, 0x32, 0x04, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x05, 0x04, 0x80, 0x00, 0xff, 0xff, 0x07, 0x04, 0x80, 0x00, 0xff, 0xff, 0x08, 0x01, 0x10, 0x01, 0x04, 0x80, 0x00, 0x75, 0x30, 0x09, 0x01, 0x10, 0x0f, 0x08, 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08, 0x06, 0x04, 0x80, 0x00, 0xff, 0xff /* followed by zero padding */ }; static const unsigned char tx_script_1_dgram[] = { 0xc0, 0x00, 0x00, 0x00, 0x01, 0x08, 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08, 0x00, 0x00, 0x44, 0x9e, 0x7b, 0x9a, 0xec, 0x34, 0xd1, 0xb1, 0xc9, 0x8d, 0xd7, 0x68, 0x9f, 0xb8, 0xec, 0x11, 0xd2, 0x42, 0xb1, 0x23, 0xdc, 0x9b, 0xd8, 0xba, 0xb9, 0x36, 0xb4, 0x7d, 0x92, 0xec, 0x35, 0x6c, 0x0b, 0xab, 0x7d, 0xf5, 0x97, 0x6d, 0x27, 0xcd, 0x44, 0x9f, 0x63, 0x30, 0x00, 0x99, 0xf3, 0x99, 0x1c, 0x26, 0x0e, 0xc4, 0xc6, 0x0d, 0x17, 0xb3, 0x1f, 0x84, 0x29, 0x15, 0x7b, 0xb3, 0x5a, 0x12, 0x82, 0xa6, 0x43, 0xa8, 0xd2, 0x26, 0x2c, 0xad, 0x67, 0x50, 0x0c, 0xad, 0xb8, 0xe7, 0x37, 0x8c, 0x8e, 0xb7, 0x53, 0x9e, 0xc4, 0xd4, 0x90, 0x5f, 0xed, 0x1b, 0xee, 0x1f, 0xc8, 0xaa, 0xfb, 0xa1, 0x7c, 0x75, 0x0e, 0x2c, 0x7a, 0xce, 0x01, 0xe6, 0x00, 0x5f, 0x80, 0xfc, 0xb7, 0xdf, 0x62, 0x12, 0x30, 0xc8, 0x37, 0x11, 0xb3, 0x93, 0x43, 0xfa, 0x02, 0x8c, 0xea, 0x7f, 0x7f, 0xb5, 0xff, 0x89, 0xea, 0xc2, 0x30, 0x82, 0x49, 0xa0, 0x22, 0x52, 0x15, 0x5e, 0x23, 0x47, 0xb6, 0x3d, 0x58, 0xc5, 0x45, 0x7a, 0xfd, 0x84, 0xd0, 0x5d, 0xff, 0xfd, 0xb2, 0x03, 0x92, 0x84, 0x4a, 0xe8, 0x12, 0x15, 0x46, 0x82, 0xe9, 0xcf, 0x01, 0x2f, 0x90, 0x21, 0xa6, 0xf0, 0xbe, 0x17, 0xdd, 0xd0, 0xc2, 0x08, 0x4d, 0xce, 0x25, 0xff, 0x9b, 0x06, 0xcd, 0xe5, 0x35, 0xd0, 0xf9, 0x20, 0xa2, 0xdb, 0x1b, 0xf3, 0x62, 0xc2, 0x3e, 0x59, 0x6d, 0x11, 0xa4, 0xf5, 0xa6, 0xcf, 0x39, 0x48, 0x83, 0x8a, 0x3a, 0xec, 0x4e, 0x15, 0xda, 0xf8, 0x50, 0x0a, 0x6e, 0xf6, 0x9e, 0xc4, 0xe3, 0xfe, 0xb6, 0xb1, 0xd9, 0x8e, 0x61, 0x0a, 0xc8, 0xb7, 0xec, 0x3f, 0xaf, 0x6a, 0xd7, 0x60, 0xb7, 0xba, 0xd1, 0xdb, 0x4b, 0xa3, 0x48, 0x5e, 0x8a, 0x94, 0xdc, 0x25, 0x0a, 0xe3, 0xfd, 0xb4, 0x1e, 0xd1, 0x5f, 0xb6, 0xa8, 0xe5, 0xeb, 0xa0, 0xfc, 0x3d, 0xd6, 0x0b, 0xc8, 0xe3, 0x0c, 0x5c, 0x42, 0x87, 0xe5, 0x38, 0x05, 0xdb, 0x05, 0x9a, 0xe0, 0x64, 0x8d, 0xb2, 0xf6, 0x42, 0x64, 0xed, 0x5e, 0x39, 0xbe, 0x2e, 0x20, 0xd8, 0x2d, 0xf5, 0x66, 0xda, 0x8d, 0xd5, 0x99, 0x8c, 0xca, 0xbd, 0xae, 0x05, 0x30, 0x60, 0xae, 0x6c, 0x7b, 0x43, 0x78, 0xe8, 0x46, 0xd2, 0x9f, 0x37, 0xed, 0x7b, 0x4e, 0xa9, 0xec, 0x5d, 0x82, 0xe7, 0x96, 0x1b, 0x7f, 0x25, 0xa9, 0x32, 0x38, 0x51, 0xf6, 0x81, 0xd5, 0x82, 0x36, 0x3a, 0xa5, 0xf8, 0x99, 0x37, 0xf5, 0xa6, 0x72, 0x58, 0xbf, 0x63, 0xad, 0x6f, 0x1a, 0x0b, 0x1d, 0x96, 0xdb, 0xd4, 0xfa, 0xdd, 0xfc, 0xef, 0xc5, 0x26, 0x6b, 0xa6, 0x61, 0x17, 0x22, 0x39, 0x5c, 0x90, 0x65, 0x56, 0xbe, 0x52, 0xaf, 0xe3, 0xf5, 0x65, 0x63, 0x6a, 0xd1, 0xb1, 0x7d, 0x50, 0x8b, 0x73, 0xd8, 0x74, 0x3e, 0xeb, 0x52, 0x4b, 0xe2, 0x2b, 0x3d, 0xcb, 0xc2, 0xc7, 0x46, 0x8d, 0x54, 0x11, 0x9c, 0x74, 0x68, 0x44, 0x9a, 0x13, 0xd8, 0xe3, 0xb9, 0x58, 0x11, 0xa1, 0x98, 0xf3, 0x49, 0x1d, 0xe3, 0xe7, 0xfe, 0x94, 0x2b, 0x33, 0x04, 0x07, 0xab, 0xf8, 0x2a, 0x4e, 0xd7, 0xc1, 0xb3, 0x11, 0x66, 0x3a, 0xc6, 0x98, 0x90, 0xf4, 0x15, 0x70, 0x15, 0x85, 0x3d, 0x91, 0xe9, 0x23, 0x03, 0x7c, 0x22, 0x7a, 0x33, 0xcd, 0xd5, 0xec, 0x28, 0x1c, 0xa3, 0xf7, 0x9c, 0x44, 0x54, 0x6b, 0x9d, 0x90, 0xca, 0x00, 0xf0, 0x64, 0xc9, 0x9e, 0x3d, 0xd9, 0x79, 0x11, 0xd3, 0x9f, 0xe9, 0xc5, 0xd0, 0xb2, 0x3a, 0x22, 0x9a, 0x23, 0x4c, 0xb3, 0x61, 0x86, 0xc4, 0x81, 0x9e, 0x8b, 0x9c, 0x59, 0x27, 0x72, 0x66, 0x32, 0x29, 0x1d, 0x6a, 0x41, 0x82, 0x11, 0xcc, 0x29, 0x62, 0xe2, 0x0f, 0xe4, 0x7f, 0xeb, 0x3e, 0xdf, 0x33, 0x0f, 0x2c, 0x60, 0x3a, 0x9d, 0x48, 0xc0, 0xfc, 0xb5, 0x69, 0x9d, 0xbf, 0xe5, 0x89, 0x64, 0x25, 0xc5, 0xba, 0xc4, 0xae, 0xe8, 0x2e, 0x57, 0xa8, 0x5a, 0xaf, 0x4e, 0x25, 0x13, 0xe4, 0xf0, 0x57, 0x96, 0xb0, 0x7b, 0xa2, 0xee, 0x47, 0xd8, 0x05, 0x06, 0xf8, 0xd2, 0xc2, 0x5e, 0x50, 0xfd, 0x14, 0xde, 0x71, 0xe6, 0xc4, 0x18, 0x55, 0x93, 0x02, 0xf9, 0x39, 0xb0, 0xe1, 0xab, 0xd5, 0x76, 0xf2, 0x79, 0xc4, 0xb2, 0xe0, 0xfe, 0xb8, 0x5c, 0x1f, 0x28, 0xff, 0x18, 0xf5, 0x88, 0x91, 0xff, 0xef, 0x13, 0x2e, 0xef, 0x2f, 0xa0, 0x93, 0x46, 0xae, 0xe3, 0x3c, 0x28, 0xeb, 0x13, 0x0f, 0xf2, 0x8f, 0x5b, 0x76, 0x69, 0x53, 0x33, 0x41, 0x13, 0x21, 0x19, 0x96, 0xd2, 0x00, 0x11, 0xa1, 0x98, 0xe3, 0xfc, 0x43, 0x3f, 0x9f, 0x25, 0x41, 0x01, 0x0a, 0xe1, 0x7c, 0x1b, 0xf2, 0x02, 0x58, 0x0f, 0x60, 0x47, 0x47, 0x2f, 0xb3, 0x68, 0x57, 0xfe, 0x84, 0x3b, 0x19, 0xf5, 0x98, 0x40, 0x09, 0xdd, 0xc3, 0x24, 0x04, 0x4e, 0x84, 0x7a, 0x4f, 0x4a, 0x0a, 0xb3, 0x4f, 0x71, 0x95, 0x95, 0xde, 0x37, 0x25, 0x2d, 0x62, 0x35, 0x36, 0x5e, 0x9b, 0x84, 0x39, 0x2b, 0x06, 0x10, 0x85, 0x34, 0x9d, 0x73, 0x20, 0x3a, 0x4a, 0x13, 0xe9, 0x6f, 0x54, 0x32, 0xec, 0x0f, 0xd4, 0xa1, 0xee, 0x65, 0xac, 0xcd, 0xd5, 0xe3, 0x90, 0x4d, 0xf5, 0x4c, 0x1d, 0xa5, 0x10, 0xb0, 0xff, 0x20, 0xdc, 0xc0, 0xc7, 0x7f, 0xcb, 0x2c, 0x0e, 0x0e, 0xb6, 0x05, 0xcb, 0x05, 0x04, 0xdb, 0x87, 0x63, 0x2c, 0xf3, 0xd8, 0xb4, 0xda, 0xe6, 0xe7, 0x05, 0x76, 0x9d, 0x1d, 0xe3, 0x54, 0x27, 0x01, 0x23, 0xcb, 0x11, 0x45, 0x0e, 0xfc, 0x60, 0xac, 0x47, 0x68, 0x3d, 0x7b, 0x8d, 0x0f, 0x81, 0x13, 0x65, 0x56, 0x5f, 0xd9, 0x8c, 0x4c, 0x8e, 0xb9, 0x36, 0xbc, 0xab, 0x8d, 0x06, 0x9f, 0xc3, 0x3b, 0xd8, 0x01, 0xb0, 0x3a, 0xde, 0xa2, 0xe1, 0xfb, 0xc5, 0xaa, 0x46, 0x3d, 0x08, 0xca, 0x19, 0x89, 0x6d, 0x2b, 0xf5, 0x9a, 0x07, 0x1b, 0x85, 0x1e, 0x6c, 0x23, 0x90, 0x52, 0x17, 0x2f, 0x29, 0x6b, 0xfb, 0x5e, 0x72, 0x40, 0x47, 0x90, 0xa2, 0x18, 0x10, 0x14, 0xf3, 0xb9, 0x4a, 0x4e, 0x97, 0xd1, 0x17, 0xb4, 0x38, 0x13, 0x03, 0x68, 0xcc, 0x39, 0xdb, 0xb2, 0xd1, 0x98, 0x06, 0x5a, 0xe3, 0x98, 0x65, 0x47, 0x92, 0x6c, 0xd2, 0x16, 0x2f, 0x40, 0xa2, 0x9f, 0x0c, 0x3c, 0x87, 0x45, 0xc0, 0xf5, 0x0f, 0xba, 0x38, 0x52, 0xe5, 0x66, 0xd4, 0x45, 0x75, 0xc2, 0x9d, 0x39, 0xa0, 0x3f, 0x0c, 0xda, 0x72, 0x19, 0x84, 0xb6, 0xf4, 0x40, 0x59, 0x1f, 0x35, 0x5e, 0x12, 0xd4, 0x39, 0xff, 0x15, 0x0a, 0xab, 0x76, 0x13, 0x49, 0x9d, 0xbd, 0x49, 0xad, 0xab, 0xc8, 0x67, 0x6e, 0xef, 0x02, 0x3b, 0x15, 0xb6, 0x5b, 0xfc, 0x5c, 0xa0, 0x69, 0x48, 0x10, 0x9f, 0x23, 0xf3, 0x50, 0xdb, 0x82, 0x12, 0x35, 0x35, 0xeb, 0x8a, 0x74, 0x33, 0xbd, 0xab, 0xcb, 0x90, 0x92, 0x71, 0xa6, 0xec, 0xbc, 0xb5, 0x8b, 0x93, 0x6a, 0x88, 0xcd, 0x4e, 0x8f, 0x2e, 0x6f, 0xf5, 0x80, 0x01, 0x75, 0xf1, 0x13, 0x25, 0x3d, 0x8f, 0xa9, 0xca, 0x88, 0x85, 0xc2, 0xf5, 0x52, 0xe6, 0x57, 0xdc, 0x60, 0x3f, 0x25, 0x2e, 0x1a, 0x8e, 0x30, 0x8f, 0x76, 0xf0, 0xbe, 0x79, 0xe2, 0xfb, 0x8f, 0x5d, 0x5f, 0xbb, 0xe2, 0xe3, 0x0e, 0xca, 0xdd, 0x22, 0x07, 0x23, 0xc8, 0xc0, 0xae, 0xa8, 0x07, 0x8c, 0xdf, 0xcb, 0x38, 0x68, 0x26, 0x3f, 0xf8, 0xf0, 0x94, 0x00, 0x54, 0xda, 0x48, 0x78, 0x18, 0x93, 0xa7, 0xe4, 0x9a, 0xd5, 0xaf, 0xf4, 0xaf, 0x30, 0x0c, 0xd8, 0x04, 0xa6, 0xb6, 0x27, 0x9a, 0xb3, 0xff, 0x3a, 0xfb, 0x64, 0x49, 0x1c, 0x85, 0x19, 0x4a, 0xab, 0x76, 0x0d, 0x58, 0xa6, 0x06, 0x65, 0x4f, 0x9f, 0x44, 0x00, 0xe8, 0xb3, 0x85, 0x91, 0x35, 0x6f, 0xbf, 0x64, 0x25, 0xac, 0xa2, 0x6d, 0xc8, 0x52, 0x44, 0x25, 0x9f, 0xf2, 0xb1, 0x9c, 0x41, 0xb9, 0xf9, 0x6f, 0x3c, 0xa9, 0xec, 0x1d, 0xde, 0x43, 0x4d, 0xa7, 0xd2, 0xd3, 0x92, 0xb9, 0x05, 0xdd, 0xf3, 0xd1, 0xf9, 0xaf, 0x93, 0xd1, 0xaf, 0x59, 0x50, 0xbd, 0x49, 0x3f, 0x5a, 0xa7, 0x31, 0xb4, 0x05, 0x6d, 0xf3, 0x1b, 0xd2, 0x67, 0xb6, 0xb9, 0x0a, 0x07, 0x98, 0x31, 0xaa, 0xf5, 0x79, 0xbe, 0x0a, 0x39, 0x01, 0x31, 0x37, 0xaa, 0xc6, 0xd4, 0x04, 0xf5, 0x18, 0xcf, 0xd4, 0x68, 0x40, 0x64, 0x7e, 0x78, 0xbf, 0xe7, 0x06, 0xca, 0x4c, 0xf5, 0xe9, 0xc5, 0x45, 0x3e, 0x9f, 0x7c, 0xfd, 0x2b, 0x8b, 0x4c, 0x8d, 0x16, 0x9a, 0x44, 0xe5, 0x5c, 0x88, 0xd4, 0xa9, 0xa7, 0xf9, 0x47, 0x42, 0x41, 0xe2, 0x21, 0xaf, 0x44, 0x86, 0x00, 0x18, 0xab, 0x08, 0x56, 0x97, 0x2e, 0x19, 0x4c, 0xd9, 0x34 }; static QUIC_PKT_HDR tx_script_1_hdr = { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 4, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ {8, {0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08}}, /* DCID */ { 0, {0} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_1_iovec[] = { { tx_script_1_body, sizeof(tx_script_1_body) } }; static const OSSL_QTX_PKT tx_script_1_pkt = { &tx_script_1_hdr, tx_script_1_iovec, OSSL_NELEM(tx_script_1_iovec), NULL, NULL, 2, 0 }; static const struct tx_test_op tx_script_1[] = { TX_OP_PROVIDE_SECRET_INITIAL(tx_script_1_hdr.dst_conn_id, 0) TX_OP_WRITE_CHECK(1) TX_OP_END }; /* 2. RFC 9001 - A.3 Server Initial */ static const unsigned char tx_script_2_body[] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xee, 0xfc, 0xe7, 0xf7, 0xb3, 0x7b, 0xa1, 0xd1, 0x63, 0x2e, 0x96, 0x67, 0x78, 0x25, 0xdd, 0xf7, 0x39, 0x88, 0xcf, 0xc7, 0x98, 0x25, 0xdf, 0x56, 0x6d, 0xc5, 0x43, 0x0b, 0x9a, 0x04, 0x5a, 0x12, 0x00, 0x13, 0x01, 0x00, 0x00, 0x2e, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x9d, 0x3c, 0x94, 0x0d, 0x89, 0x69, 0x0b, 0x84, 0xd0, 0x8a, 0x60, 0x99, 0x3c, 0x14, 0x4e, 0xca, 0x68, 0x4d, 0x10, 0x81, 0x28, 0x7c, 0x83, 0x4d, 0x53, 0x11, 0xbc, 0xf3, 0x2b, 0xb9, 0xda, 0x1a, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04 }; static const unsigned char tx_script_2_dgram[] = { 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x40, 0x75, 0xc0, 0xd9, 0x5a, 0x48, 0x2c, 0xd0, 0x99, 0x1c, 0xd2, 0x5b, 0x0a, 0xac, 0x40, 0x6a, 0x58, 0x16, 0xb6, 0x39, 0x41, 0x00, 0xf3, 0x7a, 0x1c, 0x69, 0x79, 0x75, 0x54, 0x78, 0x0b, 0xb3, 0x8c, 0xc5, 0xa9, 0x9f, 0x5e, 0xde, 0x4c, 0xf7, 0x3c, 0x3e, 0xc2, 0x49, 0x3a, 0x18, 0x39, 0xb3, 0xdb, 0xcb, 0xa3, 0xf6, 0xea, 0x46, 0xc5, 0xb7, 0x68, 0x4d, 0xf3, 0x54, 0x8e, 0x7d, 0xde, 0xb9, 0xc3, 0xbf, 0x9c, 0x73, 0xcc, 0x3f, 0x3b, 0xde, 0xd7, 0x4b, 0x56, 0x2b, 0xfb, 0x19, 0xfb, 0x84, 0x02, 0x2f, 0x8e, 0xf4, 0xcd, 0xd9, 0x37, 0x95, 0xd7, 0x7d, 0x06, 0xed, 0xbb, 0x7a, 0xaf, 0x2f, 0x58, 0x89, 0x18, 0x50, 0xab, 0xbd, 0xca, 0x3d, 0x20, 0x39, 0x8c, 0x27, 0x64, 0x56, 0xcb, 0xc4, 0x21, 0x58, 0x40, 0x7d, 0xd0, 0x74, 0xee }; static QUIC_PKT_HDR tx_script_2_hdr = { QUIC_PKT_TYPE_INITIAL, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 0, {0} }, /* DCID */ {8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5}}, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_2_iovec[] = { { tx_script_2_body, sizeof(tx_script_2_body) } }; static const OSSL_QTX_PKT tx_script_2_pkt = { &tx_script_2_hdr, tx_script_2_iovec, OSSL_NELEM(tx_script_2_iovec), NULL, NULL, 1, 0 }; static const struct tx_test_op tx_script_2[] = { TX_OP_PROVIDE_SECRET_INITIAL(tx_script_1_hdr.dst_conn_id, 1) TX_OP_WRITE_CHECK(2) TX_OP_END }; #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) /* 3. RFC 9001 - A.5 ChaCha20-Poly1305 Short Header Packet */ static const unsigned char tx_script_3_body[] = { 0x01 }; static const unsigned char tx_script_3_dgram[] = { 0x4c, 0xfe, 0x41, 0x89, 0x65, 0x5e, 0x5c, 0xd5, 0x5c, 0x41, 0xf6, 0x90, 0x80, 0x57, 0x5d, 0x79, 0x99, 0xc2, 0x5a, 0x5b, 0xfb }; static const unsigned char tx_script_3_secret[] = { 0x9a, 0xc3, 0x12, 0xa7, 0xf8, 0x77, 0x46, 0x8e, 0xbe, 0x69, 0x42, 0x27, 0x48, 0xad, 0x00, 0xa1, 0x54, 0x43, 0xf1, 0x82, 0x03, 0xa0, 0x7d, 0x60, 0x60, 0xf6, 0x88, 0xf3, 0x0f, 0x21, 0x63, 0x2b }; static QUIC_PKT_HDR tx_script_3_hdr = { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 3, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 0, {0} }, /* DCID */ { 0, {0} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_3_iovec[] = { { tx_script_3_body, sizeof(tx_script_3_body) } }; static const OSSL_QTX_PKT tx_script_3_pkt = { &tx_script_3_hdr, tx_script_3_iovec, OSSL_NELEM(tx_script_3_iovec), NULL, NULL, 654360564, 0 }; static const struct tx_test_op tx_script_3[] = { TX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_CHACHA20POLY1305, tx_script_3_secret) TX_OP_WRITE_CHECK(3) TX_OP_END }; #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */ /* 4. Real World - AES-128-GCM Key Update */ static const unsigned char tx_script_4_secret[] = { 0x70, 0x82, 0xc0, 0x45, 0x61, 0x4d, 0xfe, 0x04, 0x76, 0xa6, 0x4e, 0xf0, 0x38, 0xe6, 0x63, 0xd9, 0xdd, 0x4a, 0x75, 0x16, 0xa8, 0xa0, 0x06, 0x5a, 0xf2, 0x56, 0xfd, 0x84, 0x78, 0xfd, 0xf6, 0x5e }; static const unsigned char tx_script_4a_body[] = { 0x02, 0x03, 0x09, 0x00, 0x03, 0x0c, 0x00, 0x36, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char tx_script_4a_dgram[] = { 0x47, 0x6e, 0x4e, 0xbd, 0x49, 0x7e, 0xbd, 0x15, 0x1c, 0xd1, 0x3e, 0xc8, 0xcd, 0x43, 0x87, 0x6b, 0x84, 0xdb, 0xeb, 0x06, 0x8b, 0x8a, 0xae, 0x37, 0xed, 0x9c, 0xeb, 0xbc, 0xcf, 0x0d, 0x3c, 0xf0, 0xa1, 0x6f, 0xee, 0xd2, 0x7c, 0x07, 0x6e, 0xd1, 0xbe, 0x40, 0x6a, 0xd4, 0x53, 0x38, 0x9e, 0x63, 0xb5, 0xde, 0x35, 0x09, 0xb2, 0x78, 0x94, 0xe4, 0x2b, 0x37 }; static QUIC_PKT_HDR tx_script_4a_hdr = { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 4, {0x6e, 0x4e, 0xbd, 0x49} }, /* DCID */ { 0, {0} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_4a_iovec[] = { { tx_script_4a_body, sizeof(tx_script_4a_body) } }; static const OSSL_QTX_PKT tx_script_4a_pkt = { &tx_script_4a_hdr, tx_script_4a_iovec, OSSL_NELEM(tx_script_4a_iovec), NULL, NULL, 4, 0 }; static const unsigned char tx_script_4b_body[] = { 0x02, 0x04, 0x07, 0x00, 0x00, 0x0c, 0x00, 0x40, 0x51, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char tx_script_4b_dgram[] = { 0x58, 0x6e, 0x4e, 0xbd, 0x49, 0xa4, 0x43, 0x33, 0xea, 0x11, 0x3a, 0x6c, 0xf5, 0x20, 0xef, 0x55, 0x8d, 0x25, 0xe2, 0x3b, 0x0e, 0x8c, 0xea, 0x17, 0xfc, 0x2b, 0x7a, 0xab, 0xfa, 0x3d, 0x07, 0xda, 0xa7, 0x7c, 0xc7, 0x47, 0x82, 0x02, 0x46, 0x40, 0x4f, 0x01, 0xad, 0xb2, 0x9d, 0x97, 0xdb, 0xfc, 0x9c, 0x4b, 0x46, 0xb1, 0x5a, 0x7f, 0x0b, 0x12, 0xaf, 0x49, 0xdf, }; static QUIC_PKT_HDR tx_script_4b_hdr = { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 1, /* key phase */ 2, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 4, {0x6e, 0x4e, 0xbd, 0x49} }, /* DCID */ { 0, {0} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_4b_iovec[] = { { tx_script_4b_body, sizeof(tx_script_4b_body) } }; static const OSSL_QTX_PKT tx_script_4b_pkt = { &tx_script_4b_hdr, tx_script_4b_iovec, OSSL_NELEM(tx_script_4b_iovec), NULL, NULL, 5, 0 }; static const unsigned char tx_script_4c_body[] = { 0x02, 0x09, 0x0e, 0x00, 0x00, 0x0c, 0x00, 0x40, 0xd8, 0x49, 0x27, 0x6d, 0x20, 0x68, 0x61, 0x76, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x66, 0x75, 0x6c, 0x20, 0x74, 0x69, 0x6d, 0x65, }; static const unsigned char tx_script_4c_dgram[] = { 0x49, 0x6e, 0x4e, 0xbd, 0x49, 0x4d, 0xd9, 0x85, 0xba, 0x26, 0xfb, 0x68, 0x83, 0x9b, 0x94, 0x34, 0x7d, 0xc1, 0x7a, 0x05, 0xb7, 0x38, 0x43, 0x21, 0xe2, 0xec, 0x2b, 0xc1, 0x81, 0x74, 0x2d, 0xda, 0x24, 0xba, 0xbd, 0x99, 0x69, 0xd2, 0x56, 0xfa, 0xae, 0x29, 0x24, 0xb2, 0xaa, 0xda, 0xbd, 0x82, 0x80, 0xf1, 0xbb, 0x6a, 0xfd, 0xae, 0xda, 0x0e, 0x09, 0xcf, 0x09, }; static QUIC_PKT_HDR tx_script_4c_hdr = { QUIC_PKT_TYPE_1RTT, /* type */ 0, /* spin bit */ 0, /* key phase */ 2, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 4, {0x6e, 0x4e, 0xbd, 0x49} }, /* DCID */ { 0, {0} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_4c_iovec[] = { { tx_script_4c_body, sizeof(tx_script_4c_body) } }; static const OSSL_QTX_PKT tx_script_4c_pkt = { &tx_script_4c_hdr, tx_script_4c_iovec, OSSL_NELEM(tx_script_4c_iovec), NULL, NULL, 10, 0 }; static const struct tx_test_op tx_script_4[] = { TX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, tx_script_4_secret) TX_OP_WRITE_CHECK(4a) TX_OP_KEY_UPDATE() TX_OP_WRITE_CHECK(4b) TX_OP_KEY_UPDATE() TX_OP_WRITE_CHECK(4c) TX_OP_END }; /* 5. Real World - Retry Packet */ static const unsigned char tx_script_5_body[] = { /* Retry Token */ 0x92, 0xe7, 0xc6, 0xd8, 0x09, 0x65, 0x72, 0x55, 0xe5, 0xe2, 0x73, 0x04, 0xf3, 0x07, 0x5b, 0x21, 0x9f, 0x50, 0xcb, 0xbc, 0x79, 0xc5, 0x77, 0x5a, 0x29, 0x43, 0x65, 0x49, 0xf0, 0x6e, 0xc1, 0xc0, 0x3a, 0xe8, 0xca, 0xd2, 0x44, 0x69, 0xdd, 0x23, 0x31, 0x93, 0x52, 0x02, 0xf7, 0x42, 0x07, 0x78, 0xa1, 0x81, 0x61, 0x9c, 0x39, 0x07, 0x18, 0x69, 0x6e, 0x4f, 0xdc, 0xa0, 0xbe, 0x4b, 0xe5, 0xf2, 0xe9, 0xd2, 0xa4, 0xa7, 0x34, 0x55, 0x5e, 0xf3, 0xf8, 0x9c, 0x49, 0x8f, 0x0c, 0xc8, 0xb2, 0x75, 0x4b, 0x4d, 0x2f, 0xfe, 0x05, 0x5a, 0xdd, 0x4b, 0xe6, 0x14, 0xb4, 0xd2, 0xc0, 0x93, 0x6e, 0x0e, 0x84, 0x41, 0x4d, 0x31, /* Retry Integrity Tag */ 0x43, 0x8e, 0xab, 0xcd, 0xce, 0x24, 0x44, 0xc2, 0x20, 0xe1, 0xe2, 0xc8, 0xae, 0xa3, 0x8d, 0x4e, }; static const unsigned char tx_script_5_dgram[] = { 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0xa9, 0x20, 0xcc, 0xc2, 0x92, 0xe7, 0xc6, 0xd8, 0x09, 0x65, 0x72, 0x55, 0xe5, 0xe2, 0x73, 0x04, 0xf3, 0x07, 0x5b, 0x21, 0x9f, 0x50, 0xcb, 0xbc, 0x79, 0xc5, 0x77, 0x5a, 0x29, 0x43, 0x65, 0x49, 0xf0, 0x6e, 0xc1, 0xc0, 0x3a, 0xe8, 0xca, 0xd2, 0x44, 0x69, 0xdd, 0x23, 0x31, 0x93, 0x52, 0x02, 0xf7, 0x42, 0x07, 0x78, 0xa1, 0x81, 0x61, 0x9c, 0x39, 0x07, 0x18, 0x69, 0x6e, 0x4f, 0xdc, 0xa0, 0xbe, 0x4b, 0xe5, 0xf2, 0xe9, 0xd2, 0xa4, 0xa7, 0x34, 0x55, 0x5e, 0xf3, 0xf8, 0x9c, 0x49, 0x8f, 0x0c, 0xc8, 0xb2, 0x75, 0x4b, 0x4d, 0x2f, 0xfe, 0x05, 0x5a, 0xdd, 0x4b, 0xe6, 0x14, 0xb4, 0xd2, 0xc0, 0x93, 0x6e, 0x0e, 0x84, 0x41, 0x4d, 0x31, 0x43, 0x8e, 0xab, 0xcd, 0xce, 0x24, 0x44, 0xc2, 0x20, 0xe1, 0xe2, 0xc8, 0xae, 0xa3, 0x8d, 0x4e, }; static QUIC_PKT_HDR tx_script_5_hdr = { QUIC_PKT_TYPE_RETRY, /* type */ 0, /* spin bit */ 0, /* key phase */ 0, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 1, /* version */ { 0, {0} }, /* DCID */ { 4, {0xa9, 0x20, 0xcc, 0xc2} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_5_iovec[] = { { tx_script_5_body, sizeof(tx_script_5_body) } }; static const OSSL_QTX_PKT tx_script_5_pkt = { &tx_script_5_hdr, tx_script_5_iovec, OSSL_NELEM(tx_script_5_iovec), NULL, NULL, 0, 0 }; static const struct tx_test_op tx_script_5[] = { TX_OP_WRITE_CHECK(5) TX_OP_END }; /* 6. Real World - Version Negotiation Packet */ static const unsigned char tx_script_6_body[] = { 0x00, 0x00, 0x00, 0x01, /* Supported Version: 1 */ 0xaa, 0x9a, 0x3a, 0x9a /* Supported Version: Random (GREASE) */ }; static const unsigned char tx_script_6_dgram[] = { 0x80, /* Long */ 0x00, 0x00, 0x00, 0x00, /* Version 0 (Version Negotiation) */ 0x00, /* DCID */ 0x0c, 0x35, 0x3c, 0x1b, 0x97, 0xca, /* SCID */ 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f, 0x00, 0x00, 0x00, 0x01, /* Supported Version: 1 */ 0xaa, 0x9a, 0x3a, 0x9a /* Supported Version: Random (GREASE) */ }; static QUIC_PKT_HDR tx_script_6_hdr = { QUIC_PKT_TYPE_VERSION_NEG, /* type */ 0, /* spin bit */ 0, /* key phase */ 0, /* PN length */ 0, /* partial */ 0, /* fixed */ 0, /* unused */ 0, /* reserved */ 0, /* version */ { 0, {0} }, /* DCID */ { 12, {0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f} }, /* SCID */ { 0 }, /* PN */ NULL, 0, /* Token */ 5555, NULL /* Len/Data */ }; static const OSSL_QTX_IOVEC tx_script_6_iovec[] = { { tx_script_6_body, sizeof(tx_script_6_body) } }; static const OSSL_QTX_PKT tx_script_6_pkt = { &tx_script_6_hdr, tx_script_6_iovec, OSSL_NELEM(tx_script_6_iovec), NULL, NULL, 0, 0 }; static const struct tx_test_op tx_script_6[] = { TX_OP_WRITE_CHECK(6) TX_OP_END }; static const struct tx_test_op *const tx_scripts[] = { tx_script_1, tx_script_2, #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) tx_script_3, #endif tx_script_4, tx_script_5, tx_script_6 }; static int tx_run_script(const struct tx_test_op *script) { int testresult = 0; const struct tx_test_op *op = script; OSSL_QTX *qtx = NULL; BIO_MSG msg = {0}; OSSL_QTX_ARGS args = {0}; args.mdpl = 1472; if (!TEST_ptr(qtx = ossl_qtx_new(&args))) goto err; for (; op->op != TX_TEST_OP_END; ++op) switch (op->op) { case TX_TEST_OP_PROVIDE_SECRET: if (!TEST_true(ossl_qtx_provide_secret(qtx, op->enc_level, op->suite_id, NULL, op->buf, op->buf_len))) goto err; break; case TX_TEST_OP_PROVIDE_SECRET_INITIAL: if (!TEST_true(ossl_quic_provide_initial_secret(NULL, NULL, op->dcid, (int)op->suite_id, NULL, qtx))) goto err; break; case TX_TEST_OP_DISCARD_EL: if (!TEST_true(ossl_qtx_discard_enc_level(qtx, op->enc_level))) goto err; break; case TX_TEST_OP_WRITE: { uint32_t enc_level = ossl_quic_pkt_type_to_enc_level(op->pkt->hdr->type); uint64_t old_value = 0, new_value, max_value; if (enc_level < QUIC_ENC_LEVEL_NUM) { /* encrypted packet */ max_value = ossl_qtx_get_max_epoch_pkt_count(qtx, enc_level); if (!TEST_uint64_t_lt(max_value, UINT64_MAX)) goto err; old_value = ossl_qtx_get_cur_epoch_pkt_count(qtx, enc_level); if (!TEST_uint64_t_lt(old_value, UINT64_MAX)) goto err; } if (!TEST_true(ossl_qtx_write_pkt(qtx, op->pkt))) goto err; if (enc_level < QUIC_ENC_LEVEL_NUM) { new_value = ossl_qtx_get_cur_epoch_pkt_count(qtx, enc_level); if (!TEST_uint64_t_eq(old_value + 1, new_value)) goto err; } } break; case TX_TEST_OP_CHECK_DGRAM: if (!TEST_true(ossl_qtx_pop_net(qtx, &msg))) goto err; if (!TEST_mem_eq(msg.data, msg.data_len, op->buf, op->buf_len)) goto err; break; case TX_TEST_OP_CHECK_NO_DGRAM: if (!TEST_false(ossl_qtx_pop_net(qtx, &msg))) goto err; break; case TX_TEST_OP_KEY_UPDATE: if (!TEST_true(ossl_qtx_trigger_key_update(qtx))) goto err; break; default: OPENSSL_assert(0); goto err; } testresult = 1; err: if (qtx != NULL) ossl_qtx_free(qtx); return testresult; } static int test_tx_script(int idx) { return tx_run_script(tx_scripts[idx]); } int setup_tests(void) { ADD_ALL_TESTS(test_rx_script, OSSL_NELEM(rx_scripts)); /* * Each instance of this test is executed multiple times to get enough * statistical coverage for our statistical test, as well as for each * supported key type. * * We call the statistical test as the last index in the wire_pkt_hdr * test rather than as a separate case, as it needs to execute last * and otherwise random test ordering will cause itt to randomly fail. */ ADD_ALL_TESTS(test_wire_pkt_hdr, NUM_WIRE_PKT_HDR_TESTS + 1); ADD_ALL_TESTS(test_tx_script, OSSL_NELEM(tx_scripts)); return 1; }
./openssl/test/asn1_dsa_internal_test.c
/* * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <openssl/bn.h> #include "crypto/asn1_dsa.h" #include "testutil.h" static unsigned char t_dsa_sig[] = { 0x30, 0x06, /* SEQUENCE tag + length */ 0x02, 0x01, 0x01, /* INTEGER tag + length + content */ 0x02, 0x01, 0x02 /* INTEGER tag + length + content */ }; static unsigned char t_dsa_sig_extra[] = { 0x30, 0x06, /* SEQUENCE tag + length */ 0x02, 0x01, 0x01, /* INTEGER tag + length + content */ 0x02, 0x01, 0x02, /* INTEGER tag + length + content */ 0x05, 0x00 /* NULL tag + length */ }; static unsigned char t_dsa_sig_msb[] = { 0x30, 0x08, /* SEQUENCE tag + length */ 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length + content */ 0x02, 0x02, 0x00, 0x82 /* INTEGER tag + length + content */ }; static unsigned char t_dsa_sig_two[] = { 0x30, 0x08, /* SEQUENCE tag + length */ 0x02, 0x02, 0x01, 0x00, /* INTEGER tag + length + content */ 0x02, 0x02, 0x02, 0x00 /* INTEGER tag + length + content */ }; /* * Badly coded ASN.1 INTEGER zero wrapped in a sequence along with another * (valid) INTEGER. */ static unsigned char t_invalid_int_zero[] = { 0x30, 0x05, /* SEQUENCE tag + length */ 0x02, 0x00, /* INTEGER tag + length */ 0x02, 0x01, 0x2a /* INTEGER tag + length */ }; /* * Badly coded ASN.1 INTEGER (with leading zeros) wrapped in a sequence along * with another (valid) INTEGER. */ static unsigned char t_invalid_int[] = { 0x30, 0x07, /* SEQUENCE tag + length */ 0x02, 0x02, 0x00, 0x7f, /* INTEGER tag + length */ 0x02, 0x01, 0x2a /* INTEGER tag + length */ }; /* * Negative ASN.1 INTEGER wrapped in a sequence along with another * (valid) INTEGER. */ static unsigned char t_neg_int[] = { 0x30, 0x06, /* SEQUENCE tag + length */ 0x02, 0x01, 0xaa, /* INTEGER tag + length */ 0x02, 0x01, 0x2a /* INTEGER tag + length */ }; static unsigned char t_trunc_der[] = { 0x30, 0x08, /* SEQUENCE tag + length */ 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length */ 0x02, 0x02, 0x00 /* INTEGER tag + length */ }; static unsigned char t_trunc_seq[] = { 0x30, 0x07, /* SEQUENCE tag + length */ 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length */ 0x02, 0x02, 0x00, 0x82 /* INTEGER tag + length */ }; static int test_decode(void) { int rv = 0; BIGNUM *r; BIGNUM *s; const unsigned char *pder; r = BN_new(); s = BN_new(); /* Positive tests */ pder = t_dsa_sig; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig)) == 0 || !TEST_ptr_eq(pder, (t_dsa_sig + sizeof(t_dsa_sig))) || !TEST_BN_eq_word(r, 1) || !TEST_BN_eq_word(s, 2)) { TEST_info("asn1_dsa test_decode: t_dsa_sig failed"); goto fail; } BN_clear(r); BN_clear(s); pder = t_dsa_sig_extra; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_extra)) == 0 || !TEST_ptr_eq(pder, (t_dsa_sig_extra + sizeof(t_dsa_sig_extra) - 2)) || !TEST_BN_eq_word(r, 1) || !TEST_BN_eq_word(s, 2)) { TEST_info("asn1_dsa test_decode: t_dsa_sig_extra failed"); goto fail; } BN_clear(r); BN_clear(s); pder = t_dsa_sig_msb; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_msb)) == 0 || !TEST_ptr_eq(pder, (t_dsa_sig_msb + sizeof(t_dsa_sig_msb))) || !TEST_BN_eq_word(r, 0x81) || !TEST_BN_eq_word(s, 0x82)) { TEST_info("asn1_dsa test_decode: t_dsa_sig_msb failed"); goto fail; } BN_clear(r); BN_clear(s); pder = t_dsa_sig_two; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_two)) == 0 || !TEST_ptr_eq(pder, (t_dsa_sig_two + sizeof(t_dsa_sig_two))) || !TEST_BN_eq_word(r, 0x100) || !TEST_BN_eq_word(s, 0x200)) { TEST_info("asn1_dsa test_decode: t_dsa_sig_two failed"); goto fail; } /* Negative tests */ pder = t_invalid_int_zero; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_invalid_int_zero)) != 0) { TEST_info("asn1_dsa test_decode: Expected t_invalid_int_zero to fail"); goto fail; } BN_clear(r); BN_clear(s); pder = t_invalid_int; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_invalid_int)) != 0) { TEST_info("asn1_dsa test_decode: Expected t_invalid_int to fail"); goto fail; } BN_clear(r); BN_clear(s); pder = t_neg_int; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_neg_int)) != 0) { TEST_info("asn1_dsa test_decode: Expected t_neg_int to fail"); goto fail; } BN_clear(r); BN_clear(s); pder = t_trunc_der; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_trunc_der)) != 0) { TEST_info("asn1_dsa test_decode: Expected fail t_trunc_der"); goto fail; } BN_clear(r); BN_clear(s); pder = t_trunc_seq; if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_trunc_seq)) != 0) { TEST_info("asn1_dsa test_decode: Expected fail t_trunc_seq"); goto fail; } rv = 1; fail: BN_free(r); BN_free(s); return rv; } int setup_tests(void) { ADD_TEST(test_decode); return 1; }
./openssl/test/constant_time_test.c
/* * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include "internal/nelem.h" #include "internal/constant_time.h" #include "testutil.h" #include "internal/numbers.h" static const unsigned int CONSTTIME_TRUE = (unsigned)(~0); static const unsigned int CONSTTIME_FALSE = 0; static const unsigned char CONSTTIME_TRUE_8 = 0xff; static const unsigned char CONSTTIME_FALSE_8 = 0; static const size_t CONSTTIME_TRUE_S = ~((size_t)0); static const size_t CONSTTIME_FALSE_S = 0; static uint32_t CONSTTIME_TRUE_32 = (uint32_t)(~(uint32_t)0); static uint32_t CONSTTIME_FALSE_32 = 0; static uint64_t CONSTTIME_TRUE_64 = (uint64_t)(~(uint64_t)0); static uint64_t CONSTTIME_FALSE_64 = 0; static unsigned int test_values[] = { 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1, UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1, UINT_MAX }; static unsigned char test_values_8[] = { 0, 1, 2, 20, 32, 127, 128, 129, 255 }; static int signed_test_values[] = { 0, 1, -1, 1024, -1024, 12345, -12345, 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1 }; static size_t test_values_s[] = { 0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1, SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1, SIZE_MAX }; static uint32_t test_values_32[] = { 0, 1, 1024, 12345, 32000, UINT32_MAX / 2, UINT32_MAX / 2 + 1, UINT32_MAX - 1, UINT32_MAX }; static uint64_t test_values_64[] = { 0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2, UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX }; static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b), const char *op_name, unsigned int a, unsigned int b, int is_true) { if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE)) return 0; if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE)) return 0; return 1; } static int test_binary_op_8(unsigned char (*op) (unsigned int a, unsigned int b), const char *op_name, unsigned int a, unsigned int b, int is_true) { if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8)) return 0; if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8)) return 0; return 1; } static int test_binary_op_s(size_t (*op) (size_t a, size_t b), const char *op_name, size_t a, size_t b, int is_true) { if (is_true && !TEST_size_t_eq(op(a, b), CONSTTIME_TRUE_S)) return 0; if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_S)) return 0; return 1; } static int test_binary_op_64(uint64_t (*op)(uint64_t a, uint64_t b), const char *op_name, uint64_t a, uint64_t b, int is_true) { uint64_t c = op(a, b); if (is_true && c != CONSTTIME_TRUE_64) { TEST_error("TRUE %s op failed", op_name); BIO_printf(bio_err, "a=%jx b=%jx\n", a, b); return 0; } else if (!is_true && c != CONSTTIME_FALSE_64) { TEST_error("FALSE %s op failed", op_name); BIO_printf(bio_err, "a=%jx b=%jx\n", a, b); return 0; } return 1; } static int test_is_zero(int i) { unsigned int a = test_values[i]; if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE)) return 0; if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE)) return 0; return 1; } static int test_is_zero_8(int i) { unsigned int a = test_values_8[i]; if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8)) return 0; if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8)) return 0; return 1; } static int test_is_zero_32(int i) { uint32_t a = test_values_32[i]; if (a == 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_TRUE_32)) return 0; if (a != 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_FALSE_32)) return 0; return 1; } static int test_is_zero_s(int i) { size_t a = test_values_s[i]; if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S)) return 0; if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S)) return 0; return 1; } static int test_select(unsigned int a, unsigned int b) { if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a)) return 0; if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b)) return 0; return 1; } static int test_select_8(unsigned char a, unsigned char b) { if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a)) return 0; if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b)) return 0; return 1; } static int test_select_32(uint32_t a, uint32_t b) { if (!TEST_true(constant_time_select_32(CONSTTIME_TRUE_32, a, b) == a)) return 0; if (!TEST_true(constant_time_select_32(CONSTTIME_FALSE_32, a, b) == b)) return 0; return 1; } static int test_select_s(size_t a, size_t b) { if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a)) return 0; if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b)) return 0; return 1; } static int test_select_64(uint64_t a, uint64_t b) { uint64_t selected = constant_time_select_64(CONSTTIME_TRUE_64, a, b); if (selected != a) { TEST_error("test_select_64 TRUE failed"); BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted a\n", a, b, selected); return 0; } selected = constant_time_select_64(CONSTTIME_FALSE_64, a, b); if (selected != b) { BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted b\n", a, b, selected); return 0; } return 1; } static int test_select_int(int a, int b) { if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a)) return 0; if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b)) return 0; return 1; } static int test_eq_int_8(int a, int b) { if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8)) return 0; if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8)) return 0; return 1; } static int test_eq_s(size_t a, size_t b) { if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S)) return 0; if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S)) return 0; return 1; } static int test_eq_int(int a, int b) { if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE)) return 0; if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE)) return 0; return 1; } static int test_sizeofs(void) { if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s))) return 0; return 1; } static int test_binops(int i) { unsigned int a = test_values[i]; int j; int ret = 1; for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) { unsigned int b = test_values[j]; if (!test_select(a, b) || !test_binary_op(&constant_time_lt, "ct_lt", a, b, a < b) || !test_binary_op(&constant_time_lt, "constant_time_lt", b, a, b < a) || !test_binary_op(&constant_time_ge, "constant_time_ge", a, b, a >= b) || !test_binary_op(&constant_time_ge, "constant_time_ge", b, a, b >= a) || !test_binary_op(&constant_time_eq, "constant_time_eq", a, b, a == b) || !test_binary_op(&constant_time_eq, "constant_time_eq", b, a, b == a)) ret = 0; } return ret; } static int test_binops_8(int i) { unsigned int a = test_values_8[i]; int j; int ret = 1; for (j = 0; j < (int)OSSL_NELEM(test_values_8); ++j) { unsigned int b = test_values_8[j]; if (!test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8", a, b, a < b) || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8", b, a, b < a) || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", a, b, a >= b) || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", b, a, b >= a) || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", a, b, a == b) || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", b, a, b == a)) ret = 0; } return ret; } static int test_binops_s(int i) { size_t a = test_values_s[i]; int j; int ret = 1; for (j = 0; j < (int)OSSL_NELEM(test_values_s); ++j) { size_t b = test_values_s[j]; if (!test_select_s(a, b) || !test_eq_s(a, b) || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s", a, b, a < b) || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s", b, a, b < a) || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s", a, b, a >= b) || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s", b, a, b >= a) || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", a, b, a == b) || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", b, a, b == a)) ret = 0; } return ret; } static int test_signed(int i) { int c = signed_test_values[i]; unsigned int j; int ret = 1; for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) { int d = signed_test_values[j]; if (!test_select_int(c, d) || !test_eq_int(c, d) || !test_eq_int_8(c, d)) ret = 0; } return ret; } static int test_8values(int i) { unsigned char e = test_values_8[i]; unsigned int j; int ret = 1; for (j = 0; j < sizeof(test_values_8); ++j) { unsigned char f = test_values_8[j]; if (!test_select_8(e, f)) ret = 0; } return ret; } static int test_32values(int i) { uint32_t e = test_values_32[i]; size_t j; int ret = 1; for (j = 0; j < OSSL_NELEM(test_values_32); j++) { uint32_t f = test_values_32[j]; if (!test_select_32(e, f)) ret = 0; } return ret; } static int test_64values(int i) { uint64_t g = test_values_64[i]; int j, ret = 1; for (j = i + 1; j < (int)OSSL_NELEM(test_values_64); j++) { uint64_t h = test_values_64[j]; if (!test_binary_op_64(&constant_time_lt_64, "constant_time_lt_64", g, h, g < h) || !test_select_64(g, h)) { TEST_info("test_64values failed i=%d j=%d", i, j); ret = 0; } } return ret; } int setup_tests(void) { ADD_TEST(test_sizeofs); ADD_ALL_TESTS(test_is_zero, OSSL_NELEM(test_values)); ADD_ALL_TESTS(test_is_zero_8, OSSL_NELEM(test_values_8)); ADD_ALL_TESTS(test_is_zero_32, OSSL_NELEM(test_values_32)); ADD_ALL_TESTS(test_is_zero_s, OSSL_NELEM(test_values_s)); ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values)); ADD_ALL_TESTS(test_binops_8, OSSL_NELEM(test_values_8)); ADD_ALL_TESTS(test_binops_s, OSSL_NELEM(test_values_s)); ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values)); ADD_ALL_TESTS(test_8values, OSSL_NELEM(test_values_8)); ADD_ALL_TESTS(test_32values, OSSL_NELEM(test_values_32)); ADD_ALL_TESTS(test_64values, OSSL_NELEM(test_values_64)); return 1; }
./openssl/test/filterprov.c
/* * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * A filtering provider for test purposes. We pass all calls through to the * default provider except where we want other behaviour for a test. */ #include <string.h> #include <openssl/core.h> #include <openssl/provider.h> #include <openssl/crypto.h> #include "testutil.h" #include "filterprov.h" #define MAX_FILTERS 10 #define MAX_ALG_FILTERS 5 struct filter_prov_globals_st { OSSL_LIB_CTX *libctx; OSSL_PROVIDER *deflt; struct { int operation; OSSL_ALGORITHM alg[MAX_ALG_FILTERS + 1]; } dispatch[MAX_FILTERS]; int num_dispatch; int no_cache; unsigned long int query_count; int error; }; static struct filter_prov_globals_st ourglobals; static struct filter_prov_globals_st *get_globals(void) { /* * Ideally we'd like to store this in the OSSL_LIB_CTX so that we can have * more than one instance of the filter provider at a time. But for now we * just make it simple. */ return &ourglobals; } static OSSL_FUNC_provider_gettable_params_fn filter_gettable_params; static OSSL_FUNC_provider_get_params_fn filter_get_params; static OSSL_FUNC_provider_query_operation_fn filter_query; static OSSL_FUNC_provider_unquery_operation_fn filter_unquery; static OSSL_FUNC_provider_teardown_fn filter_teardown; static const OSSL_PARAM *filter_gettable_params(void *provctx) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_gettable_params(globs->deflt); } static int filter_get_params(void *provctx, OSSL_PARAM params[]) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_get_params(globs->deflt, params); } static int filter_get_capabilities(void *provctx, const char *capability, OSSL_CALLBACK *cb, void *arg) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_get_capabilities(globs->deflt, capability, cb, arg); } static const OSSL_ALGORITHM *filter_query(void *provctx, int operation_id, int *no_cache) { struct filter_prov_globals_st *globs = get_globals(); int i; globs->query_count++; for (i = 0; i < globs->num_dispatch; i++) { if (globs->dispatch[i].operation == operation_id) { *no_cache = globs->no_cache; return globs->dispatch[i].alg; } } /* No filter set, so pass it down to the chained provider */ return OSSL_PROVIDER_query_operation(globs->deflt, operation_id, no_cache); } static void filter_unquery(void *provctx, int operation_id, const OSSL_ALGORITHM *algs) { struct filter_prov_globals_st *globs = get_globals(); int i; if (!TEST_ulong_gt(globs->query_count, 0)) globs->error = 1; else globs->query_count--; for (i = 0; i < globs->num_dispatch; i++) if (globs->dispatch[i].alg == algs) return; OSSL_PROVIDER_unquery_operation(globs->deflt, operation_id, algs); } static void filter_teardown(void *provctx) { struct filter_prov_globals_st *globs = get_globals(); OSSL_PROVIDER_unload(globs->deflt); OSSL_LIB_CTX_free(globs->libctx); memset(globs, 0, sizeof(*globs)); } /* Functions we provide to the core */ static const OSSL_DISPATCH filter_dispatch_table[] = { { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))filter_gettable_params }, { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))filter_get_params }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))filter_query }, { OSSL_FUNC_PROVIDER_UNQUERY_OPERATION, (void (*)(void))filter_unquery }, { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))filter_get_capabilities }, { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))filter_teardown }, OSSL_DISPATCH_END }; int filter_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { memset(&ourglobals, 0, sizeof(ourglobals)); ourglobals.libctx = OSSL_LIB_CTX_new(); if (ourglobals.libctx == NULL) goto err; ourglobals.deflt = OSSL_PROVIDER_load(ourglobals.libctx, "default"); if (ourglobals.deflt == NULL) goto err; *provctx = OSSL_PROVIDER_get0_provider_ctx(ourglobals.deflt); *out = filter_dispatch_table; return 1; err: OSSL_PROVIDER_unload(ourglobals.deflt); OSSL_LIB_CTX_free(ourglobals.libctx); return 0; } /* * Set a filter for the given operation id. The filter string is a colon * separated list of algorithms that will be made available by this provider. * Anything not in the filter will be suppressed. If a filter is not set for * a given operation id then all algorithms are made available. */ int filter_provider_set_filter(int operation, const char *filterstr) { int no_cache = 0; int algnum = 0, last = 0, ret = 0; struct filter_prov_globals_st *globs = get_globals(); size_t namelen; char *filterstrtmp = OPENSSL_strdup(filterstr); char *name, *sep; const OSSL_ALGORITHM *provalgs = OSSL_PROVIDER_query_operation(globs->deflt, operation, &no_cache); const OSSL_ALGORITHM *algs; if (filterstrtmp == NULL) goto err; /* Nothing to filter */ if (provalgs == NULL) goto err; if (globs->num_dispatch >= MAX_FILTERS) goto err; for (name = filterstrtmp; !last; name = (sep == NULL ? NULL : sep + 1)) { sep = strstr(name, ":"); if (sep != NULL) *sep = '\0'; else last = 1; namelen = strlen(name); for (algs = provalgs; algs->algorithm_names != NULL; algs++) { const char *found = strstr(algs->algorithm_names, name); if (found == NULL) continue; if (found[namelen] != '\0' && found[namelen] != ':') continue; if (found != algs->algorithm_names && found[-1] != ':') continue; /* We found a match */ if (algnum >= MAX_ALG_FILTERS) goto err; globs->dispatch[globs->num_dispatch].alg[algnum++] = *algs; break; } if (algs->algorithm_names == NULL) { /* No match found */ goto err; } } globs->dispatch[globs->num_dispatch].operation = operation; globs->no_cache = no_cache; globs->num_dispatch++; ret = 1; err: OSSL_PROVIDER_unquery_operation(globs->deflt, operation, provalgs); OPENSSL_free(filterstrtmp); return ret; } /* * Test if a filter provider is in a clean finishing state. * If it is return 1, otherwise return 0. */ int filter_provider_check_clean_finish(void) { struct filter_prov_globals_st *globs = get_globals(); return TEST_ulong_eq(globs->query_count, 0) && !globs->error; }
./openssl/test/danetest.c
/* * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <limits.h> #include <errno.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/conf.h> #ifndef OPENSSL_NO_ENGINE # include <openssl/engine.h> #endif #include "testutil.h" #include "internal/nelem.h" #define _UC(c) ((unsigned char)(c)) static const char *basedomain; static const char *CAfile; static const char *tlsafile; /* * Forward declaration, of function that uses internal interfaces, from headers * included at the end of this module. */ static void store_ctx_dane_init(X509_STORE_CTX *, SSL *); static int saved_errno; static void save_errno(void) { saved_errno = errno; } static int restore_errno(void) { int ret = errno; errno = saved_errno; return ret; } static int verify_chain(SSL *ssl, STACK_OF(X509) *chain) { X509_STORE_CTX *store_ctx = NULL; SSL_CTX *ssl_ctx = NULL; X509_STORE *store = NULL; int ret = 0; int store_ctx_idx = SSL_get_ex_data_X509_STORE_CTX_idx(); if (!TEST_ptr(store_ctx = X509_STORE_CTX_new()) || !TEST_ptr(ssl_ctx = SSL_get_SSL_CTX(ssl)) || !TEST_ptr(store = SSL_CTX_get_cert_store(ssl_ctx)) || !TEST_true(X509_STORE_CTX_init(store_ctx, store, NULL, chain)) || !TEST_true(X509_STORE_CTX_set_ex_data(store_ctx, store_ctx_idx, ssl))) goto end; X509_STORE_CTX_set_default(store_ctx, SSL_is_server(ssl) ? "ssl_client" : "ssl_server"); X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(store_ctx), SSL_get0_param(ssl)); store_ctx_dane_init(store_ctx, ssl); if (SSL_get_verify_callback(ssl) != NULL) X509_STORE_CTX_set_verify_cb(store_ctx, SSL_get_verify_callback(ssl)); /* Mask "internal failures" (-1) from our return value. */ if (!TEST_int_ge(ret = X509_STORE_CTX_verify(store_ctx), 0)) ret = 0; SSL_set_verify_result(ssl, X509_STORE_CTX_get_error(store_ctx)); end: X509_STORE_CTX_free(store_ctx); return ret; } static STACK_OF(X509) *load_chain(BIO *fp, int nelem) { int count; char *name = 0; char *header = 0; unsigned char *data = 0; long len; char *errtype = 0; /* if error: cert or pkey? */ STACK_OF(X509) *chain; typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long); if (!TEST_ptr(chain = sk_X509_new_null())) goto err; for (count = 0; count < nelem && errtype == 0 && PEM_read_bio(fp, &name, &header, &data, &len) == 1; ++count) { if (strcmp(name, PEM_STRING_X509) == 0 || strcmp(name, PEM_STRING_X509_TRUSTED) == 0 || strcmp(name, PEM_STRING_X509_OLD) == 0) { d2i_X509_t d = strcmp(name, PEM_STRING_X509_TRUSTED) != 0 ? d2i_X509_AUX : d2i_X509; X509 *cert; const unsigned char *p = data; if (!TEST_ptr(cert = d(0, &p, len)) || !TEST_long_eq(p - data, len)) { TEST_info("Certificate parsing error"); goto err; } if (!TEST_true(sk_X509_push(chain, cert))) goto err; } else { TEST_info("Unknown chain file object %s", name); goto err; } OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); name = header = NULL; data = NULL; } if (count == nelem) { ERR_clear_error(); return chain; } err: OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); OSSL_STACK_OF_X509_free(chain); return NULL; } static char *read_to_eol(BIO *f) { static char buf[4096]; int n; if (BIO_gets(f, buf, sizeof(buf)) <= 0) return NULL; n = strlen(buf); if (buf[n - 1] != '\n') { if (n + 1 == sizeof(buf)) TEST_error("input too long"); else TEST_error("EOF before newline"); return NULL; } /* Trim trailing whitespace */ while (n > 0 && isspace(_UC(buf[n - 1]))) buf[--n] = '\0'; return buf; } /* * Hex decoder that tolerates optional whitespace */ static ossl_ssize_t hexdecode(const char *in, void *result) { unsigned char **out = (unsigned char **)result; unsigned char *ret; unsigned char *cp; uint8_t byte; int nibble = 0; if (!TEST_ptr(ret = OPENSSL_malloc(strlen(in) / 2))) return -1; cp = ret; for (byte = 0; *in; ++in) { int x; if (isspace(_UC(*in))) continue; x = OPENSSL_hexchar2int(*in); if (x < 0) { OPENSSL_free(ret); return 0; } byte |= (char)x; if ((nibble ^= 1) == 0) { *cp++ = byte; byte = 0; } else { byte <<= 4; } } if (nibble != 0) { OPENSSL_free(ret); return 0; } return cp - (*out = ret); } static ossl_ssize_t checked_uint8(const char *in, void *out) { uint8_t *result = (uint8_t *)out; const char *cp = in; char *endp; long v; int e; save_errno(); v = strtol(cp, &endp, 10); e = restore_errno(); if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) || endp == cp || !isspace(_UC(*endp)) || v != (*(uint8_t *)result = (uint8_t) v)) { return -1; } for (cp = endp; isspace(_UC(*cp)); ++cp) continue; return cp - in; } struct tlsa_field { void *var; const char *name; ossl_ssize_t (*parser)(const char *, void *); }; static int tlsa_import_rr(SSL *ssl, const char *rrdata) { static uint8_t usage; static uint8_t selector; static uint8_t mtype; static unsigned char *data = NULL; static struct tlsa_field tlsa_fields[] = { { &usage, "usage", checked_uint8 }, { &selector, "selector", checked_uint8 }, { &mtype, "mtype", checked_uint8 }, { &data, "data", hexdecode }, { NULL, } }; int ret; struct tlsa_field *f; const char *cp = rrdata; ossl_ssize_t len = 0; for (f = tlsa_fields; f->var; ++f) { if ((len = f->parser(cp += len, f->var)) <= 0) { TEST_info("bad TLSA %s field in: %s", f->name, rrdata); return 0; } } ret = SSL_dane_tlsa_add(ssl, usage, selector, mtype, data, len); OPENSSL_free(data); if (ret == 0) { TEST_info("unusable TLSA rrdata: %s", rrdata); return 0; } if (ret < 0) { TEST_info("error loading TLSA rrdata: %s", rrdata); return 0; } return ret; } static int allws(const char *cp) { while (*cp) if (!isspace(_UC(*cp++))) return 0; return 1; } static int test_tlsafile(SSL_CTX *ctx, const char *base_name, BIO *f, const char *path) { char *line; int testno = 0; int ret = 1; SSL *ssl; while (ret > 0 && (line = read_to_eol(f)) != NULL) { STACK_OF(X509) *chain; int ntlsa; int ncert; int noncheck; int want; int want_depth; int off; int i; int ok; int err; int mdpth; if (*line == '\0' || *line == '#') continue; ++testno; if (sscanf(line, "%d %d %d %d %d%n", &ntlsa, &ncert, &noncheck, &want, &want_depth, &off) != 5 || !allws(line + off)) { TEST_error("Malformed line for test %d", testno); return 0; } if (!TEST_ptr(ssl = SSL_new(ctx))) return 0; SSL_set_connect_state(ssl); if (SSL_dane_enable(ssl, base_name) <= 0) { SSL_free(ssl); return 0; } if (noncheck) SSL_dane_set_flags(ssl, DANE_FLAG_NO_DANE_EE_NAMECHECKS); for (i = 0; i < ntlsa; ++i) { if ((line = read_to_eol(f)) == NULL || !tlsa_import_rr(ssl, line)) { SSL_free(ssl); return 0; } } /* Don't report old news */ ERR_clear_error(); if (!TEST_ptr(chain = load_chain(f, ncert))) { SSL_free(ssl); return 0; } ok = verify_chain(ssl, chain); OSSL_STACK_OF_X509_free(chain); err = SSL_get_verify_result(ssl); /* * Peek under the hood, normally TLSA match data is hidden when * verification fails, we can obtain any suppressed data by setting the * verification result to X509_V_OK before looking. */ SSL_set_verify_result(ssl, X509_V_OK); mdpth = SSL_get0_dane_authority(ssl, NULL, NULL); /* Not needed any more, but lead by example and put the error back. */ SSL_set_verify_result(ssl, err); SSL_free(ssl); if (!TEST_int_eq(err, want)) { if (want == X509_V_OK) TEST_info("Verification failure in test %d: %d=%s", testno, err, X509_verify_cert_error_string(err)); else TEST_info("Unexpected error in test %d", testno); ret = 0; continue; } if (!TEST_false(want == 0 && ok == 0)) { TEST_info("Verification failure in test %d: ok=0", testno); ret = 0; continue; } if (!TEST_int_eq(mdpth, want_depth)) { TEST_info("In test test %d", testno); ret = 0; } } ERR_clear_error(); return ret; } static int run_tlsatest(void) { SSL_CTX *ctx = NULL; BIO *f = NULL; int ret = 0; if (!TEST_ptr(f = BIO_new_file(tlsafile, "r")) || !TEST_ptr(ctx = SSL_CTX_new(TLS_client_method())) || !TEST_int_gt(SSL_CTX_dane_enable(ctx), 0) || !TEST_true(SSL_CTX_load_verify_file(ctx, CAfile)) || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha512(), 2, 1), 0) || !TEST_int_gt(SSL_CTX_dane_mtype_set(ctx, EVP_sha256(), 1, 2), 0) || !TEST_int_gt(test_tlsafile(ctx, basedomain, f, tlsafile), 0)) goto end; ret = 1; end: BIO_free(f); SSL_CTX_free(ctx); return ret; } OPT_TEST_DECLARE_USAGE("basedomain CAfile tlsafile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(basedomain = test_get_argument(0)) || !TEST_ptr(CAfile = test_get_argument(1)) || !TEST_ptr(tlsafile = test_get_argument(2))) return 0; ADD_TEST(run_tlsatest); return 1; } #include "internal/dane.h" static void store_ctx_dane_init(X509_STORE_CTX *store_ctx, SSL *ssl) { X509_STORE_CTX_set0_dane(store_ctx, SSL_get0_dane(ssl)); }
./openssl/test/cmp_msg_test.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2019 * Copyright Siemens AG 2015-2019 * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "helpers/cmp_testlib.h" static const char *newkey_f; static const char *server_cert_f; static const char *pkcs10_f; typedef struct test_fixture { const char *test_case_name; OSSL_CMP_CTX *cmp_ctx; /* for msg create tests */ int bodytype; int err_code; /* for certConf */ int fail_info; /* for protection tests */ OSSL_CMP_MSG *msg; int expected; /* for error and response messages */ OSSL_CMP_PKISI *si; } CMP_MSG_TEST_FIXTURE; static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; static unsigned char ref[CMP_TEST_REFVALUE_LENGTH]; static void tear_down(CMP_MSG_TEST_FIXTURE *fixture) { OSSL_CMP_CTX_free(fixture->cmp_ctx); OSSL_CMP_MSG_free(fixture->msg); OSSL_CMP_PKISI_free(fixture->si); OPENSSL_free(fixture); } #define SET_OPT_UNPROTECTED_SEND(ctx, val) \ OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val)) static CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_MSG_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1)) || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, ref, sizeof(ref)))) { tear_down(fixture); return NULL; } return fixture; } static EVP_PKEY *newkey = NULL; static X509 *cert = NULL; #define EXECUTE_MSG_CREATION_TEST(expr) \ do { \ OSSL_CMP_MSG *msg = NULL; \ int good = fixture->expected != 0 ? \ TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \ TEST_ptr_null(msg = (expr)); \ \ OSSL_CMP_MSG_free(msg); \ ERR_print_errors_fp(stderr); \ return good; \ } while (0) /*- * The following tests call a cmp message creation function. * if fixture->expected != 0: * returns 1 if the message is created and syntactically correct. * if fixture->expected == 0 * returns 1 if message creation returns NULL */ static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx, fixture->bodytype, NULL)); } static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si, fixture->err_code, "details", 0)); } static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx)); } static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new (fixture->cmp_ctx, OSSL_CMP_CERTREQID, fixture->fail_info, NULL)); } static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx)); } static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711)); } static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture) { EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create (fixture->cmp_ctx, fixture->bodytype)); } static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey) { if (!EVP_PKEY_up_ref(pkey)) return 0; if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) { EVP_PKEY_free(pkey); return 0; } return 1; } static int test_cmp_create_ir_protection_set(void) { OSSL_CMP_CTX *ctx; unsigned char secret[16]; SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); ctx = fixture->cmp_ctx; fixture->bodytype = OSSL_CMP_PKIBODY_IR; fixture->err_code = -1; fixture->expected = 1; if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0)) || !TEST_true(set1_newPkey(ctx, newkey)) || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret, sizeof(secret)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_ir_protection_fails(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_IR; fixture->err_code = -1; fixture->expected = 0; if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)) /* newkey used by default for signing does not match cert: */ || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_cr_without_key(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_CR; fixture->err_code = -1; fixture->expected = 0; EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_cr(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_CR; fixture->err_code = -1; fixture->expected = 1; if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_certreq_with_invalid_bodytype(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_RR; fixture->err_code = -1; fixture->expected = 0; if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_p10cr(void) { OSSL_CMP_CTX *ctx; X509_REQ *p10cr = NULL; SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); ctx = fixture->cmp_ctx; fixture->bodytype = OSSL_CMP_PKIBODY_P10CR; fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ; fixture->expected = 1; if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx)) || !TEST_true(set1_newPkey(ctx, newkey)) || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) { tear_down(fixture); fixture = NULL; } X509_REQ_free(p10cr); EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_p10cr_null(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_P10CR; fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ; fixture->expected = 0; if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_kur(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_KUR; fixture->err_code = -1; fixture->expected = 1; if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey)) || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_kur_without_oldcert(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->bodytype = OSSL_CMP_PKIBODY_KUR; fixture->err_code = -1; fixture->expected = 0; if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certreq_create_test, tear_down); return result; } static int test_cmp_create_certconf(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->fail_info = 0; fixture->expected = 1; if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(cert)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certconf_create_test, tear_down); return result; } static int test_cmp_create_certconf_badAlg(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg; fixture->expected = 1; if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(cert)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certconf_create_test, tear_down); return result; } static int test_cmp_create_certconf_fail_info_max(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX; fixture->expected = 1; if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(cert)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_certconf_create_test, tear_down); return result; } static int test_cmp_create_error_msg(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, OSSL_CMP_PKIFAILUREINFO_systemFailure, NULL); fixture->err_code = -1; fixture->expected = 1; /* expected: message creation is successful */ if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_errormsg_create_test, tear_down); return result; } static int test_cmp_create_pollreq(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_pollreq_create_test, tear_down); return result; } static int test_cmp_create_rr(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_rr_create_test, tear_down); return result; } static int test_cmp_create_genm(void) { OSSL_CMP_ITAV *iv = NULL; SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); fixture->expected = 1; iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL); if (!TEST_ptr(iv) || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) { OSSL_CMP_ITAV_free(iv); tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_genm_create_test, tear_down); return result; } static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture) { OSSL_CMP_CTX *ctx = fixture->cmp_ctx; OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new(); OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new(); X509 *certfromresp = NULL; int res = 0; if (crepmsg == NULL || cresp == NULL) goto err; if (!ASN1_INTEGER_set(cresp->certReqId, 99)) goto err; if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL) goto err; cresp->certifiedKeyPair->certOrEncCert->type = OSSL_CMP_CERTORENCCERT_CERTIFICATE; if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate = X509_dup(cert)) == NULL || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp)) goto err; cresp = NULL; read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99); if (!TEST_ptr(read_cresp)) goto err; if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88))) goto err; certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp); if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0)) goto err; res = 1; err: X509_free(certfromresp); OSSL_CMP_CERTRESPONSE_free(cresp); OSSL_CMP_CERTREPMESSAGE_free(crepmsg); return res; } static int test_cmp_create_certrep(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); EXECUTE_TEST(execute_certrep_create, tear_down); return result; } static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture) { OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text"); X509_NAME *issuer = X509_NAME_new(); ASN1_INTEGER *serial = ASN1_INTEGER_new(); OSSL_CRMF_CERTID *cid = NULL; OSSL_CMP_MSG *rpmsg = NULL; int res = 0; if (si == NULL || issuer == NULL || serial == NULL) goto err; if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC, (unsigned char *)"The Issuer", -1, -1, 0) || !ASN1_INTEGER_set(serial, 99) || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL) goto err; if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0))) goto err; if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0))) goto err; res = 1; err: ASN1_INTEGER_free(serial); X509_NAME_free(issuer); OSSL_CRMF_CERTID_free(cid); OSSL_CMP_PKISI_free(si); OSSL_CMP_MSG_free(rpmsg); return res; } static int test_cmp_create_rp(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); EXECUTE_TEST(execute_rp_create, tear_down); return result; } static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture) { OSSL_CMP_MSG *pollrep; int res = 0; pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000); if (!TEST_ptr(pollrep)) return 0; if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body-> value.pollRep, 77))) goto err; if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body-> value.pollRep, 88))) goto err; res = 1; err: OSSL_CMP_MSG_free(pollrep); return res; } static int test_cmp_create_pollrep(void) { SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); EXECUTE_TEST(execute_pollrep_create, tear_down); return result; } static int test_cmp_pkimessage_create(int bodytype) { X509_REQ *p10cr = NULL; SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up); switch (fixture->bodytype = bodytype) { case OSSL_CMP_PKIBODY_P10CR: fixture->expected = 1; p10cr = load_csr_der(pkcs10_f, libctx); if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) { tear_down(fixture); fixture = NULL; } X509_REQ_free(p10cr); break; case OSSL_CMP_PKIBODY_IR: case OSSL_CMP_PKIBODY_IP: case OSSL_CMP_PKIBODY_CR: case OSSL_CMP_PKIBODY_CP: case OSSL_CMP_PKIBODY_KUR: case OSSL_CMP_PKIBODY_KUP: case OSSL_CMP_PKIBODY_RR: case OSSL_CMP_PKIBODY_RP: case OSSL_CMP_PKIBODY_PKICONF: case OSSL_CMP_PKIBODY_GENM: case OSSL_CMP_PKIBODY_GENP: case OSSL_CMP_PKIBODY_ERROR: case OSSL_CMP_PKIBODY_CERTCONF: case OSSL_CMP_PKIBODY_POLLREQ: case OSSL_CMP_PKIBODY_POLLREP: fixture->expected = 1; break; default: fixture->expected = 0; break; } EXECUTE_TEST(execute_pkimessage_create_test, tear_down); return result; } void cleanup_tests(void) { EVP_PKEY_free(newkey); X509_free(cert); OSSL_PROVIDER_unload(default_null_provider); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); } #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(newkey_f = test_get_argument(0)) || !TEST_ptr(server_cert_f = test_get_argument(1)) || !TEST_ptr(pkcs10_f = test_get_argument(2))) { TEST_error("usage: cmp_msg_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE)) return 0; if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx)) || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx)) || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) { cleanup_tests(); return 0; } /* Message creation tests */ ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype); ADD_TEST(test_cmp_create_ir_protection_fails); ADD_TEST(test_cmp_create_ir_protection_set); ADD_TEST(test_cmp_create_error_msg); ADD_TEST(test_cmp_create_certconf); ADD_TEST(test_cmp_create_certconf_badAlg); ADD_TEST(test_cmp_create_certconf_fail_info_max); ADD_TEST(test_cmp_create_kur); ADD_TEST(test_cmp_create_kur_without_oldcert); ADD_TEST(test_cmp_create_cr); ADD_TEST(test_cmp_create_cr_without_key); ADD_TEST(test_cmp_create_p10cr); ADD_TEST(test_cmp_create_p10cr_null); ADD_TEST(test_cmp_create_pollreq); ADD_TEST(test_cmp_create_rr); ADD_TEST(test_cmp_create_rp); ADD_TEST(test_cmp_create_genm); ADD_TEST(test_cmp_create_certrep); ADD_TEST(test_cmp_create_pollrep); ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create, OSSL_CMP_PKIBODY_POLLREP + 1); return 1; }
./openssl/test/timing_load_creds.c
/* * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/e_os2.h> #ifdef OPENSSL_SYS_UNIX # include <sys/stat.h> # include <sys/resource.h> # include <openssl/pem.h> # include <openssl/x509.h> # include <openssl/err.h> # include <openssl/bio.h> # include "internal/e_os.h" # if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L # ifndef timersub /* struct timeval * subtraction; a must be greater than or equal to b */ # define timersub(a, b, res) \ do { \ (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ if ((a)->tv_usec < (b)->tv_usec) { \ (res)->tv_usec = (a)->tv_usec + 1000000 - (b)->tv_usec; \ --(res)->tv_sec; \ } else { \ (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ } \ } while(0) # endif static char *prog; static void readx509(const char *contents, int size) { X509 *x = NULL; BIO *b = BIO_new_mem_buf(contents, size); if (b == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } PEM_read_bio_X509(b, &x, 0, NULL); if (x == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } X509_free(x); BIO_free(b); } static void readpkey(const char *contents, int size) { BIO *b = BIO_new_mem_buf(contents, size); EVP_PKEY *pkey; if (b == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL); if (pkey == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } EVP_PKEY_free(pkey); BIO_free(b); } static void print_timeval(const char *what, struct timeval *tp) { printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec); } static void usage(void) { fprintf(stderr, "Usage: %s [flags] pem-file\n", prog); fprintf(stderr, "Flags, with the default being '-wc':\n"); fprintf(stderr, " -c # Repeat count\n"); fprintf(stderr, " -d Debugging output (minimal)\n"); fprintf(stderr, " -w<T> What to load T is a single character:\n"); fprintf(stderr, " c for cert\n"); fprintf(stderr, " p for private key\n"); exit(EXIT_FAILURE); } # endif #endif int main(int ac, char **av) { #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L int i, debug = 0, count = 100, what = 'c'; struct stat sb; FILE *fp; char *contents; struct rusage start, end, elapsed; struct timeval e_start, e_end, e_elapsed; /* Parse JCL. */ prog = av[0]; while ((i = getopt(ac, av, "c:dw:")) != EOF) { switch (i) { default: usage(); break; case 'c': if ((count = atoi(optarg)) < 0) usage(); break; case 'd': debug = 1; break; case 'w': if (optarg[1] != '\0') usage(); switch (*optarg) { default: usage(); break; case 'c': case 'p': what = *optarg; break; } break; } } ac -= optind; av += optind; /* Read input file. */ if (av[0] == NULL) usage(); if (stat(av[0], &sb) < 0) { perror(av[0]); exit(EXIT_FAILURE); } contents = OPENSSL_malloc(sb.st_size + 1); if (contents == NULL) { perror("malloc"); exit(EXIT_FAILURE); } fp = fopen(av[0], "r"); if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) { perror("fread"); exit(EXIT_FAILURE); } contents[sb.st_size] = '\0'; fclose(fp); if (debug) printf(">%s<\n", contents); /* Try to prep system cache, etc. */ for (i = 10; i > 0; i--) { switch (what) { case 'c': readx509(contents, (int)sb.st_size); break; case 'p': readpkey(contents, (int)sb.st_size); break; } } if (gettimeofday(&e_start, NULL) < 0) { perror("elapsed start"); exit(EXIT_FAILURE); } if (getrusage(RUSAGE_SELF, &start) < 0) { perror("start"); exit(EXIT_FAILURE); } for (i = count; i > 0; i--) { switch (what) { case 'c': readx509(contents, (int)sb.st_size); break; case 'p': readpkey(contents, (int)sb.st_size); break; } } if (getrusage(RUSAGE_SELF, &end) < 0) { perror("getrusage"); exit(EXIT_FAILURE); } if (gettimeofday(&e_end, NULL) < 0) { perror("gettimeofday"); exit(EXIT_FAILURE); } timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime); timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime); timersub(&e_end, &e_start, &e_elapsed); print_timeval("user ", &elapsed.ru_utime); print_timeval("sys ", &elapsed.ru_stime); if (debug) print_timeval("elapsed??", &e_elapsed); OPENSSL_free(contents); return EXIT_SUCCESS; #else fprintf(stderr, "This tool is not supported on this platform for lack of POSIX1.2001 support\n"); exit(EXIT_FAILURE); #endif }
./openssl/test/cipherlist_test.c
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * https://www.openssl.org/source/license.html * or in the file LICENSE in the source distribution. */ #include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/e_os2.h> #include <openssl/ssl.h> #include <openssl/ssl3.h> #include <openssl/tls1.h> #include "internal/nelem.h" #include "testutil.h" typedef struct cipherlist_test_fixture { const char *test_case_name; SSL_CTX *server; SSL_CTX *client; } CIPHERLIST_TEST_FIXTURE; static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture) { if (fixture != NULL) { SSL_CTX_free(fixture->server); SSL_CTX_free(fixture->client); fixture->server = fixture->client = NULL; OPENSSL_free(fixture); } } static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name) { CIPHERLIST_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method())) || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) { tear_down(fixture); return NULL; } return fixture; } /* * All ciphers in the DEFAULT cipherlist meet the default security level. * However, default supported ciphers exclude SRP and PSK ciphersuites * for which no callbacks have been set up. * * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled, * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA * are currently broken and should be considered mission impossible in libssl. */ static const uint32_t default_ciphers_in_order[] = { #ifndef OPENSSL_NO_TLS1_3 TLS1_3_CK_AES_256_GCM_SHA384, # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) TLS1_3_CK_CHACHA20_POLY1305_SHA256, # endif TLS1_3_CK_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_2 # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, # endif # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305, # endif # endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */ # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, # endif #endif /* !OPENSSL_NO_TLS1_2 */ #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */ # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, # endif #ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_SHA, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_SHA, # endif #endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */ #ifndef OPENSSL_NO_TLS1_2 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_2 TLS1_CK_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256, #endif #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */ TLS1_CK_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, #endif }; static int test_default_cipherlist(SSL_CTX *ctx) { STACK_OF(SSL_CIPHER) *ciphers = NULL; SSL *ssl = NULL; int i, ret = 0, num_expected_ciphers, num_ciphers; uint32_t expected_cipher_id, cipher_id; if (ctx == NULL) return 0; if (!TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))) goto err; num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order); num_ciphers = sk_SSL_CIPHER_num(ciphers); if (!TEST_int_eq(num_ciphers, num_expected_ciphers)) goto err; for (i = 0; i < num_ciphers; i++) { expected_cipher_id = default_ciphers_in_order[i]; cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i)); if (!TEST_int_eq(cipher_id, expected_cipher_id)) { TEST_info("Wrong cipher at position %d", i); goto err; } } ret = 1; err: sk_SSL_CIPHER_free(ciphers); SSL_free(ssl); return ret; } static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture) { return fixture != NULL && test_default_cipherlist(fixture->server) && test_default_cipherlist(fixture->client); } #define SETUP_CIPHERLIST_TEST_FIXTURE() \ SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up) #define EXECUTE_CIPHERLIST_TEST() \ EXECUTE_TEST(execute_test, tear_down) static int test_default_cipherlist_implicit(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); EXECUTE_CIPHERLIST_TEST(); return result; } static int test_default_cipherlist_explicit(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT")) || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) { tear_down(fixture); fixture = NULL; } EXECUTE_CIPHERLIST_TEST(); return result; } /* SSL_CTX_set_cipher_list() should fail if it clears all TLSv1.2 ciphers. */ static int test_default_cipherlist_clear(void) { SSL *s = NULL; SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0)) goto end; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH)) goto end; s = SSL_new(fixture->client); if (!TEST_ptr(s)) goto end; if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0)) goto end; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH)) goto end; result = 1; end: SSL_free(s); tear_down(fixture); return result; } /* SSL_CTX_set_cipher_list matching with cipher standard name */ static int test_stdname_cipherlist(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, TLS1_RFC_RSA_WITH_AES_128_SHA)) || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, TLS1_RFC_RSA_WITH_AES_128_SHA))) { goto end; } result = 1; end: tear_down(fixture); fixture = NULL; return result; } int setup_tests(void) { ADD_TEST(test_default_cipherlist_implicit); ADD_TEST(test_default_cipherlist_explicit); ADD_TEST(test_default_cipherlist_clear); ADD_TEST(test_stdname_cipherlist); return 1; }
./openssl/test/ec_internal_test.c
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * Low level APIs are deprecated for public use, but still ok for internal use. */ #include "internal/deprecated.h" #include "internal/nelem.h" #include "testutil.h" #include <openssl/ec.h> #include "ec_local.h" #include <openssl/objects.h> static size_t crv_len = 0; static EC_builtin_curve *curves = NULL; /* sanity checks field_inv function pointer in EC_METHOD */ static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx) { BIGNUM *a = NULL, *b = NULL, *c = NULL; int ret = 0; if (group->meth->field_inv == NULL || group->meth->field_mul == NULL) return 1; BN_CTX_start(ctx); a = BN_CTX_get(ctx); b = BN_CTX_get(ctx); if (!TEST_ptr(c = BN_CTX_get(ctx)) /* 1/1 = 1 */ || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx)) || !TEST_true(BN_is_one(b)) /* (1/a)*a = 1 */ || !TEST_true(BN_rand(a, BN_num_bits(group->field) - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) || !TEST_true(group->meth->field_inv(group, b, a, ctx)) || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, a, a, ctx))) || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, b, b, ctx))) || !TEST_true(group->meth->field_mul(group, c, a, b, ctx)) || (group->meth->field_decode && !TEST_true(group->meth->field_decode(group, c, c, ctx))) || !TEST_true(BN_is_one(c))) goto err; /* 1/0 = error */ BN_zero(a); if (!TEST_false(group->meth->field_inv(group, b, a, ctx)) || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC) || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT) /* 1/p = error */ || !TEST_false(group->meth->field_inv(group, b, group->field, ctx)) || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC) || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT)) goto err; ERR_clear_error(); ret = 1; err: BN_CTX_end(ctx); return ret; } /* wrapper for group_field_tests for explicit curve params and EC_METHOD */ static int field_tests(const EC_METHOD *meth, const unsigned char *params, int len) { BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; EC_GROUP *group = NULL; int ret = 0; if (!TEST_ptr(ctx = BN_CTX_new())) return 0; BN_CTX_start(ctx); p = BN_CTX_get(ctx); a = BN_CTX_get(ctx); if (!TEST_ptr(b = BN_CTX_get(ctx)) || !TEST_ptr(group = EC_GROUP_new(meth)) || !TEST_true(BN_bin2bn(params, len, p)) || !TEST_true(BN_bin2bn(params + len, len, a)) || !TEST_true(BN_bin2bn(params + 2 * len, len, b)) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !group_field_tests(group, ctx)) goto err; ret = 1; err: BN_CTX_end(ctx); BN_CTX_free(ctx); if (group != NULL) EC_GROUP_free(group); return ret; } /* NIST prime curve P-256 */ static const unsigned char params_p256[] = { /* p */ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* a */ 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, /* b */ 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6, 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B }; #ifndef OPENSSL_NO_EC2M /* NIST binary curve B-283 */ static const unsigned char params_b283[] = { /* p */ 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1, /* a */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* b */ 0x02, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A, 0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2, 0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5 }; #endif /* test EC_GFp_simple_method directly */ static int field_tests_ecp_simple(void) { TEST_info("Testing EC_GFp_simple_method()\n"); return field_tests(EC_GFp_simple_method(), params_p256, sizeof(params_p256) / 3); } /* test EC_GFp_mont_method directly */ static int field_tests_ecp_mont(void) { TEST_info("Testing EC_GFp_mont_method()\n"); return field_tests(EC_GFp_mont_method(), params_p256, sizeof(params_p256) / 3); } #ifndef OPENSSL_NO_EC2M /* test EC_GF2m_simple_method directly */ static int field_tests_ec2_simple(void) { TEST_info("Testing EC_GF2m_simple_method()\n"); return field_tests(EC_GF2m_simple_method(), params_b283, sizeof(params_b283) / 3); } #endif /* test default method for a named curve */ static int field_tests_default(int n) { BN_CTX *ctx = NULL; EC_GROUP *group = NULL; int nid = curves[n].nid; int ret = 0; TEST_info("Testing curve %s\n", OBJ_nid2sn(nid)); if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(ctx = BN_CTX_new()) || !group_field_tests(group, ctx)) goto err; ret = 1; err: if (group != NULL) EC_GROUP_free(group); if (ctx != NULL) BN_CTX_free(ctx); return ret; } #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 /* * Tests a point known to cause an incorrect underflow in an old version of * ecp_nist521.c */ static int underflow_test(void) { BN_CTX *ctx = NULL; EC_GROUP *grp = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL; BIGNUM *x1 = NULL, *y1 = NULL, *z1 = NULL, *x2 = NULL, *y2 = NULL; BIGNUM *k = NULL; int testresult = 0; const char *x1str = "1534f0077fffffe87e9adcfe000000000000000000003e05a21d2400002e031b1f4" "b80000c6fafa4f3c1288798d624a247b5e2ffffffffffffffefe099241900004"; const char *p521m1 = "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"; ctx = BN_CTX_new(); if (!TEST_ptr(ctx)) return 0; BN_CTX_start(ctx); x1 = BN_CTX_get(ctx); y1 = BN_CTX_get(ctx); z1 = BN_CTX_get(ctx); x2 = BN_CTX_get(ctx); y2 = BN_CTX_get(ctx); k = BN_CTX_get(ctx); if (!TEST_ptr(k)) goto err; grp = EC_GROUP_new_by_curve_name(NID_secp521r1); P = EC_POINT_new(grp); Q = EC_POINT_new(grp); R = EC_POINT_new(grp); if (!TEST_ptr(grp) || !TEST_ptr(P) || !TEST_ptr(Q) || !TEST_ptr(R)) goto err; if (!TEST_int_gt(BN_hex2bn(&x1, x1str), 0) || !TEST_int_gt(BN_hex2bn(&y1, p521m1), 0) || !TEST_int_gt(BN_hex2bn(&z1, p521m1), 0) || !TEST_int_gt(BN_hex2bn(&k, "02"), 0) || !TEST_true(ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(grp, P, x1, y1, z1, ctx)) || !TEST_true(EC_POINT_mul(grp, Q, NULL, P, k, ctx)) || !TEST_true(EC_POINT_get_affine_coordinates(grp, Q, x1, y1, ctx)) || !TEST_true(EC_POINT_dbl(grp, R, P, ctx)) || !TEST_true(EC_POINT_get_affine_coordinates(grp, R, x2, y2, ctx))) goto err; if (!TEST_int_eq(BN_cmp(x1, x2), 0) || !TEST_int_eq(BN_cmp(y1, y2), 0)) goto err; testresult = 1; err: BN_CTX_end(ctx); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); EC_GROUP_free(grp); BN_CTX_free(ctx); return testresult; } #endif /* * Tests behavior of the EC_KEY_set_private_key */ static int set_private_key(void) { EC_KEY *key = NULL, *aux_key = NULL; int testresult = 0; key = EC_KEY_new_by_curve_name(NID_secp224r1); aux_key = EC_KEY_new_by_curve_name(NID_secp224r1); if (!TEST_ptr(key) || !TEST_ptr(aux_key) || !TEST_int_eq(EC_KEY_generate_key(key), 1) || !TEST_int_eq(EC_KEY_generate_key(aux_key), 1)) goto err; /* Test setting a valid private key */ if (!TEST_int_eq(EC_KEY_set_private_key(key, aux_key->priv_key), 1)) goto err; /* Test compliance with legacy behavior for NULL private keys */ if (!TEST_int_eq(EC_KEY_set_private_key(key, NULL), 0) || !TEST_ptr_null(key->priv_key)) goto err; testresult = 1; err: EC_KEY_free(key); EC_KEY_free(aux_key); return testresult; } /* * Tests behavior of the decoded_from_explicit_params flag and API */ static int decoded_flag_test(void) { EC_GROUP *grp; EC_GROUP *grp_copy = NULL; ECPARAMETERS *ecparams = NULL; ECPKPARAMETERS *ecpkparams = NULL; EC_KEY *key = NULL; unsigned char *encodedparams = NULL; const unsigned char *encp; int encodedlen; int testresult = 0; /* Test EC_GROUP_new not setting the flag */ grp = EC_GROUP_new(EC_GFp_simple_method()); if (!TEST_ptr(grp) || !TEST_int_eq(grp->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp); /* Test EC_GROUP_new_by_curve_name not setting the flag */ grp = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); if (!TEST_ptr(grp) || !TEST_int_eq(grp->decoded_from_explicit_params, 0)) goto err; /* Test EC_GROUP_new_from_ecparameters not setting the flag */ if (!TEST_ptr(ecparams = EC_GROUP_get_ecparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecparameters(ecparams)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; ECPARAMETERS_free(ecparams); ecparams = NULL; /* Test EC_GROUP_new_from_ecpkparameters not setting the flag */ if (!TEST_int_eq(EC_GROUP_get_asn1_flag(grp), OPENSSL_EC_NAMED_CURVE) || !TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0) || !TEST_ptr(key = EC_KEY_new()) /* Test EC_KEY_decoded_from_explicit_params on key without a group */ || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), -1) || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1) /* Test EC_KEY_decoded_from_explicit_params negative case */ || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; ECPKPARAMETERS_free(ecpkparams); ecpkparams = NULL; /* Test d2i_ECPKParameters with named params not setting the flag */ if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0) || !TEST_ptr(encp = encodedparams) || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; OPENSSL_free(encodedparams); encodedparams = NULL; /* Asn1 flag stays set to explicit with EC_GROUP_new_from_ecpkparameters */ EC_GROUP_set_asn1_flag(grp, OPENSSL_EC_EXPLICIT_CURVE); if (!TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; /* Test d2i_ECPKParameters with explicit params setting the flag */ if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0) || !TEST_ptr(encp = encodedparams) || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 1) || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1) /* Test EC_KEY_decoded_from_explicit_params positive case */ || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 1)) goto err; testresult = 1; err: EC_KEY_free(key); EC_GROUP_free(grp); EC_GROUP_free(grp_copy); ECPARAMETERS_free(ecparams); ECPKPARAMETERS_free(ecpkparams); OPENSSL_free(encodedparams); return testresult; } static int ecpkparams_i2d2i_test(int n) { EC_GROUP *g1 = NULL, *g2 = NULL; FILE *fp = NULL; int nid = curves[n].nid; int testresult = 0; /* create group */ if (!TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) goto end; /* encode params to file */ if (!TEST_ptr(fp = fopen("params.der", "wb")) || !TEST_true(i2d_ECPKParameters_fp(fp, g1))) goto end; /* flush and close file */ if (!TEST_int_eq(fclose(fp), 0)) { fp = NULL; goto end; } fp = NULL; /* decode params from file */ if (!TEST_ptr(fp = fopen("params.der", "rb")) || !TEST_ptr(g2 = d2i_ECPKParameters_fp(fp, NULL))) goto end; testresult = 1; /* PASS */ end: if (fp != NULL) fclose(fp); EC_GROUP_free(g1); EC_GROUP_free(g2); return testresult; } int setup_tests(void) { crv_len = EC_get_builtin_curves(NULL, 0); if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len)) || !TEST_true(EC_get_builtin_curves(curves, crv_len))) return 0; ADD_TEST(field_tests_ecp_simple); ADD_TEST(field_tests_ecp_mont); #ifndef OPENSSL_NO_EC2M ADD_TEST(field_tests_ec2_simple); #endif ADD_ALL_TESTS(field_tests_default, crv_len); #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 ADD_TEST(underflow_test); #endif ADD_TEST(set_private_key); ADD_TEST(decoded_flag_test); ADD_ALL_TESTS(ecpkparams_i2d2i_test, crv_len); return 1; } void cleanup_tests(void) { OPENSSL_free(curves); }
./openssl/test/v3nametest.c
/* * Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/e_os2.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include "internal/nelem.h" #include "testutil.h" static const char *const names[] = { "a", "b", ".", "*", "@", ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..", "-example.com", "example-.com", "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com", "*@example.com", "test@*.example.com", "example.com", "www.example.com", "test.www.example.com", "*.example.com", "*.www.example.com", "test.*.example.com", "www.*.com", ".www.example.com", "*www.example.com", "example.net", "xn--rger-koa.example.com", "*.xn--rger-koa.example.com", "www.xn--rger-koa.example.com", "*.good--example.com", "www.good--example.com", "*.xn--bar.com", "xn--foo.xn--bar.com", "a.example.com", "b.example.com", "postmaster@example.com", "Postmaster@example.com", "postmaster@EXAMPLE.COM", NULL }; static const char *const exceptions[] = { "set CN: host: [*.example.com] matches [a.example.com]", "set CN: host: [*.example.com] matches [b.example.com]", "set CN: host: [*.example.com] matches [www.example.com]", "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]", "set CN: host: [*.www.example.com] matches [test.www.example.com]", "set CN: host: [*.www.example.com] matches [.www.example.com]", "set CN: host: [*www.example.com] matches [www.example.com]", "set CN: host: [test.www.example.com] matches [.www.example.com]", "set CN: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]", "set CN: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]", "set CN: host: [*.good--example.com] matches [www.good--example.com]", "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]", "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]", "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]", "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]", "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@example.com]", "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]", "set dnsName: host: [*.example.com] matches [www.example.com]", "set dnsName: host: [*.example.com] matches [a.example.com]", "set dnsName: host: [*.example.com] matches [b.example.com]", "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]", "set dnsName: host: [*.www.example.com] matches [test.www.example.com]", "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]", "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]", "set dnsName: host: [*.www.example.com] matches [.www.example.com]", "set dnsName: host: [*www.example.com] matches [www.example.com]", "set dnsName: host: [test.www.example.com] matches [.www.example.com]", "set dnsName: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]", "set dnsName: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]", "set dnsName: host: [*.good--example.com] matches [www.good--example.com]", "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]", "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]", "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]", "set rfc822Name: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]", NULL }; static int is_exception(const char *msg) { const char *const *p; for (p = exceptions; *p; ++p) if (strcmp(msg, *p) == 0) return 1; return 0; } static int set_cn(X509 *crt, ...) { int ret = 0; X509_NAME *n = NULL; va_list ap; va_start(ap, crt); n = X509_NAME_new(); if (n == NULL) goto out; while (1) { int nid; const char *name; nid = va_arg(ap, int); if (nid == 0) break; name = va_arg(ap, const char *); if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC, (unsigned char *)name, -1, -1, 1)) goto out; } if (!X509_set_subject_name(crt, n)) goto out; ret = 1; out: X509_NAME_free(n); va_end(ap); return ret; } /*- int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc); X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid, int crit, ASN1_OCTET_STRING *data); int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc); */ static int set_altname(X509 *crt, ...) { int ret = 0; GENERAL_NAMES *gens = NULL; GENERAL_NAME *gen = NULL; ASN1_IA5STRING *ia5 = NULL; va_list ap; va_start(ap, crt); gens = sk_GENERAL_NAME_new_null(); if (gens == NULL) goto out; while (1) { int type; const char *name; type = va_arg(ap, int); if (type == 0) break; name = va_arg(ap, const char *); gen = GENERAL_NAME_new(); if (gen == NULL) goto out; ia5 = ASN1_IA5STRING_new(); if (ia5 == NULL) goto out; if (!ASN1_STRING_set(ia5, name, -1)) goto out; switch (type) { case GEN_EMAIL: case GEN_DNS: GENERAL_NAME_set0_value(gen, type, ia5); ia5 = NULL; break; default: abort(); } sk_GENERAL_NAME_push(gens, gen); gen = NULL; } if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0)) goto out; ret = 1; out: ASN1_IA5STRING_free(ia5); GENERAL_NAME_free(gen); GENERAL_NAMES_free(gens); va_end(ap); return ret; } static int set_cn1(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, 0); } static int set_cn_and_email(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, NID_pkcs9_emailAddress, "dummy@example.com", 0); } static int set_cn2(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, "dummy value", NID_commonName, name, 0); } static int set_cn3(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, NID_commonName, "dummy value", 0); } static int set_email1(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, 0); } static int set_email2(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com", NID_pkcs9_emailAddress, name, 0); } static int set_email3(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, NID_pkcs9_emailAddress, "dummy@example.com", 0); } static int set_email_and_cn(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, NID_commonName, "www.example.org", 0); } static int set_altname_dns(X509 *crt, const char *name) { return set_altname(crt, GEN_DNS, name, 0); } static int set_altname_email(X509 *crt, const char *name) { return set_altname(crt, GEN_EMAIL, name, 0); } struct set_name_fn { int (*fn) (X509 *, const char *); const char *name; int host; int email; }; static const struct set_name_fn name_fns[] = { {set_cn1, "set CN", 1, 0}, {set_cn2, "set CN", 1, 0}, {set_cn3, "set CN", 1, 0}, {set_cn_and_email, "set CN", 1, 0}, {set_email1, "set emailAddress", 0, 1}, {set_email2, "set emailAddress", 0, 1}, {set_email3, "set emailAddress", 0, 1}, {set_email_and_cn, "set emailAddress", 0, 1}, {set_altname_dns, "set dnsName", 1, 0}, {set_altname_email, "set rfc822Name", 0, 1}, }; static X509 *make_cert(void) { X509 *crt = NULL; if (!TEST_ptr(crt = X509_new())) return NULL; if (!TEST_true(X509_set_version(crt, X509_VERSION_3))) { X509_free(crt); return NULL; } return crt; } static int check_message(const struct set_name_fn *fn, const char *op, const char *nameincert, int match, const char *name) { char msg[1024]; if (match < 0) return 1; BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]", fn->name, op, nameincert, match ? "matches" : "does not match", name); if (is_exception(msg)) return 1; TEST_error("%s", msg); return 0; } static int run_cert(X509 *crt, const char *nameincert, const struct set_name_fn *fn) { const char *const *pname = names; int failed = 0; for (; *pname != NULL; ++pname) { int samename = OPENSSL_strcasecmp(nameincert, *pname) == 0; size_t namelen = strlen(*pname); char *name = OPENSSL_malloc(namelen + 1); int match, ret; if (!TEST_ptr(name)) return 0; memcpy(name, *pname, namelen + 1); match = -1; if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, 0, NULL), 0)) { failed = 1; } else if (fn->host) { if (ret == 1 && !samename) match = 1; if (ret == 0 && samename) match = 0; } else if (ret == 1) match = 1; if (!TEST_true(check_message(fn, "host", nameincert, match, *pname))) failed = 1; match = -1; if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, X509_CHECK_FLAG_NO_WILDCARDS, NULL), 0)) { failed = 1; } else if (fn->host) { if (ret == 1 && !samename) match = 1; if (ret == 0 && samename) match = 0; } else if (ret == 1) match = 1; if (!TEST_true(check_message(fn, "host-no-wildcards", nameincert, match, *pname))) failed = 1; match = -1; ret = X509_check_email(crt, name, namelen, 0); if (fn->email) { if (ret && !samename) match = 1; if (!ret && samename && strchr(nameincert, '@') != NULL) match = 0; } else if (ret) match = 1; if (!TEST_true(check_message(fn, "email", nameincert, match, *pname))) failed = 1; OPENSSL_free(name); } return failed == 0; } static int call_run_cert(int i) { int failed = 0; const struct set_name_fn *pfn = &name_fns[i]; X509 *crt; const char *const *pname; TEST_info("%s", pfn->name); for (pname = names; *pname != NULL; pname++) { if (!TEST_ptr(crt = make_cert()) || !TEST_true(pfn->fn(crt, *pname)) || !run_cert(crt, *pname, pfn)) failed = 1; X509_free(crt); } return failed == 0; } static struct gennamedata { const unsigned char der[22]; size_t derlen; } gennames[] = { { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * SEQUENCE {} * } * } */ { 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00 }, 21 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * [APPLICATION 0] {} * } * } */ { 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00 }, 21 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * UTF8String { "a" } * } * } */ { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61 }, 22 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.2 } * [0] { * UTF8String { "a" } * } * } */ { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61 }, 22 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * UTF8String { "b" } * } * } */ { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62 }, 22 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * BOOLEAN { TRUE } * } * } */ { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff }, 22 }, { /* * [0] { * OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2.1 } * [0] { * BOOLEAN { FALSE } * } * } */ { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00 }, 22 }, { /* [1 PRIMITIVE] { "a" } */ { 0x81, 0x01, 0x61 }, 3 }, { /* [1 PRIMITIVE] { "b" } */ { 0x81, 0x01, 0x62 }, 3 }, { /* [2 PRIMITIVE] { "a" } */ { 0x82, 0x01, 0x61 }, 3 }, { /* [2 PRIMITIVE] { "b" } */ { 0x82, 0x01, 0x62 }, 3 }, { /* * [4] { * SEQUENCE { * SET { * SEQUENCE { * # commonName * OBJECT_IDENTIFIER { 2.5.4.3 } * UTF8String { "a" } * } * } * } * } */ { 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x01, 0x61 }, 16 }, { /* * [4] { * SEQUENCE { * SET { * SEQUENCE { * # commonName * OBJECT_IDENTIFIER { 2.5.4.3 } * UTF8String { "b" } * } * } * } * } */ { 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x01, 0x62 }, 16 }, { /* * [5] { * [1] { * UTF8String { "a" } * } * } */ { 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 7 }, { /* * [5] { * [1] { * UTF8String { "b" } * } * } */ { 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62 }, 7 }, { /* * [5] { * [0] { * UTF8String {} * } * [1] { * UTF8String { "a" } * } * } */ { 0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 11 }, { /* * [5] { * [0] { * UTF8String { "a" } * } * [1] { * UTF8String { "a" } * } * } */ { 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 12 }, { /* * [5] { * [0] { * UTF8String { "b" } * } * [1] { * UTF8String { "a" } * } * } */ { 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 12 }, { /* [6 PRIMITIVE] { "a" } */ { 0x86, 0x01, 0x61 }, 3 }, { /* [6 PRIMITIVE] { "b" } */ { 0x86, 0x01, 0x62 }, 3 }, { /* [7 PRIMITIVE] { `11111111` } */ { 0x87, 0x04, 0x11, 0x11, 0x11, 0x11 }, 6 }, { /* [7 PRIMITIVE] { `22222222`} */ { 0x87, 0x04, 0x22, 0x22, 0x22, 0x22 }, 6 }, { /* [7 PRIMITIVE] { `11111111111111111111111111111111` } */ { 0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, 18 }, { /* [7 PRIMITIVE] { `22222222222222222222222222222222` } */ { 0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }, 18 }, { /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.1 } */ { 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01 }, 15 }, { /* [8 PRIMITIVE] { 1.2.840.113554.4.1.72585.2.2 } */ { 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02 }, 15 }, { /* * Regression test for CVE-2023-0286. */ { 0xa3, 0x00 }, 2 } }; static int test_GENERAL_NAME_cmp(void) { size_t i, j; GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa) * OSSL_NELEM(gennames)); GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb) * OSSL_NELEM(gennames)); int testresult = 0; if (!TEST_ptr(namesa) || !TEST_ptr(namesb)) goto end; for (i = 0; i < OSSL_NELEM(gennames); i++) { const unsigned char *derp = gennames[i].der; /* * We create two versions of each GENERAL_NAME so that we ensure when * we compare them they are always different pointers. */ namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); derp = gennames[i].der; namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); if (!TEST_ptr(namesa[i]) || !TEST_ptr(namesb[i])) goto end; } /* Every name should be equal to itself and not equal to any others. */ for (i = 0; i < OSSL_NELEM(gennames); i++) { for (j = 0; j < OSSL_NELEM(gennames); j++) { if (i == j) { if (!TEST_int_eq(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0)) goto end; } else { if (!TEST_int_ne(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0)) goto end; } } } testresult = 1; end: for (i = 0; i < OSSL_NELEM(gennames); i++) { if (namesa != NULL) GENERAL_NAME_free(namesa[i]); if (namesb != NULL) GENERAL_NAME_free(namesb[i]); } OPENSSL_free(namesa); OPENSSL_free(namesb); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns)); ADD_TEST(test_GENERAL_NAME_cmp); return 1; }
./openssl/test/errtest.c
/* * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/macros.h> #include "testutil.h" #if defined(OPENSSL_SYS_WINDOWS) # include <windows.h> #else # include <errno.h> #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F')) static int test_print_error_format(void) { /* Variables used to construct an error line */ char *lib; const char *func = OPENSSL_FUNC; char *reason; # ifdef OPENSSL_NO_ERR char reasonbuf[255]; # endif # ifndef OPENSSL_NO_FILENAMES const char *file = OPENSSL_FILE; const int line = OPENSSL_LINE; # else const char *file = ""; const int line = 0; # endif /* The format for OpenSSL error lines */ const char *expected_format = ":error:%08lX:%s:%s:%s:%s:%d"; /*- * ^^ ^^ ^^ ^^ ^^ * "library" name --------------------------++ || || || || * function name ------------------------------++ || || || * reason string (system error string) -----------++ || || * file name ----------------------------------------++ || * line number -----------------------------------------++ */ char expected[512]; char *out = NULL, *p = NULL; int ret = 0, len; BIO *bio = NULL; const int syserr = EPERM; unsigned long errorcode; unsigned long reasoncode; /* * We set a mark here so we can clear the system error that we generate * with ERR_PUT_error(). That is, after all, just a simulation to verify * ERR_print_errors() output, not a real error. */ ERR_set_mark(); ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line); errorcode = ERR_peek_error(); reasoncode = ERR_GET_REASON(errorcode); if (!TEST_int_eq(reasoncode, syserr)) { ERR_pop_to_mark(); goto err; } # if !defined(OPENSSL_NO_ERR) # if defined(OPENSSL_NO_AUTOERRINIT) lib = "lib(2)"; # else lib = "system library"; # endif reason = strerror(syserr); # else lib = "lib(2)"; BIO_snprintf(reasonbuf, sizeof(reasonbuf), "reason(%lu)", reasoncode); reason = reasonbuf; # endif BIO_snprintf(expected, sizeof(expected), expected_format, errorcode, lib, func, reason, file, line); if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto err; ERR_print_errors(bio); if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0)) goto err; /* Skip over the variable thread id at the start of the string */ for (p = out; *p != ':' && *p != 0; ++p) { if (!TEST_true(IS_HEX(*p))) goto err; } if (!TEST_true(*p != 0) || !TEST_strn_eq(expected, p, strlen(expected))) goto err; ret = 1; err: BIO_free(bio); return ret; } #endif /* Test that querying the error queue preserves the OS error. */ static int preserves_system_error(void) { #if defined(OPENSSL_SYS_WINDOWS) SetLastError(ERROR_INVALID_FUNCTION); ERR_get_error(); return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION); #else errno = EINVAL; ERR_get_error(); return TEST_int_eq(errno, EINVAL); #endif } /* Test that calls to ERR_add_error_[v]data append */ static int vdata_appends(void) { const char *data; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); ERR_add_error_data(1, "hello "); ERR_add_error_data(1, "world"); ERR_peek_error_data(&data, NULL); return TEST_str_eq(data, "hello world"); } static int raised_error(void) { const char *f, *data; int l; unsigned long e; /* * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line * number is saved, so no point checking them. */ #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) const char *file; int line; file = __FILE__; line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */ #endif ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR, "calling exit()"); if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0) || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR) #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) || !TEST_int_eq(l, line) || !TEST_str_eq(f, file) #endif || !TEST_str_eq(data, "calling exit()")) return 0; return 1; } static int test_marks(void) { unsigned long mallocfail, shouldnot; /* Set an initial error */ ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); mallocfail = ERR_peek_last_error(); if (!TEST_ulong_gt(mallocfail, 0)) return 0; /* Setting and clearing a mark should not affect the error */ if (!TEST_true(ERR_set_mark()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_set_mark()) || !TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; /* Test popping errors */ if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; /* Nested marks should also work */ if (!TEST_true(ERR_set_mark()) || !TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); shouldnot = ERR_peek_last_error(); if (!TEST_ulong_ne(mallocfail, shouldnot) || !TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(shouldnot, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; /* Setting and clearing a mark should not affect the errors on the stack */ if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); if (!TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())) return 0; /* * Popping where no mark has been set should pop everything - but return * a failure result */ if (!TEST_false(ERR_pop_to_mark()) || !TEST_ulong_eq(0, ERR_peek_last_error())) return 0; /* Clearing where there is no mark should fail */ ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); if (!TEST_false(ERR_clear_last_mark()) /* "get" the last error to remove it */ || !TEST_ulong_eq(mallocfail, ERR_get_error()) || !TEST_ulong_eq(0, ERR_peek_last_error())) return 0; /* * Setting a mark where there are no errors in the stack should fail. * NOTE: This is somewhat surprising behaviour but is historically how this * function behaves. In practice we typically set marks without first * checking whether there is anything on the stack - but we also don't * tend to check the success of this function. It turns out to work anyway * because although setting a mark with no errors fails, a subsequent call * to ERR_pop_to_mark() or ERR_clear_last_mark() will do the right thing * anyway (even though they will report a failure result). */ if (!TEST_false(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); /* Should be able to "pop" past 2 errors */ if (!TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); /* Should be able to "clear" past 2 errors */ if (!TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())) return 0; /* Clear remaining errors from last test */ ERR_clear_error(); return 1; } static int test_clear_error(void) { int flags = -1; const char *data = NULL; int res = 0; /* Raise an error with data and clear it */ ERR_raise_data(0, 0, "hello %s", "world"); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "hello world") || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); /* Raise a new error without data */ ERR_raise(0, 0); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "") || !TEST_int_eq(flags, ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); /* Raise a new error with data */ ERR_raise_data(0, 0, "goodbye %s world", "cruel"); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "goodbye cruel world") || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); /* * Raise a new error without data to check that the malloced storage * is freed properly */ ERR_raise(0, 0); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "") || !TEST_int_eq(flags, ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); res = 1; err: ERR_clear_error(); return res; } /* * Test saving and restoring error state. * Test 0: Save using OSSL_ERR_STATE_save() * Test 1: Save using OSSL_ERR_STATE_save_to_mark() */ static int test_save_restore(int idx) { ERR_STATE *es; int res = 0, i, flags = -1; unsigned long mallocfail, interr; static const char testdata[] = "test data"; const char *data = NULL; if (!TEST_ptr(es = OSSL_ERR_STATE_new())) goto err; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); mallocfail = ERR_peek_last_error(); if (!TEST_ulong_gt(mallocfail, 0)) goto err; if (idx == 1 && !TEST_int_eq(ERR_set_mark(), 1)) goto err; ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR, testdata); interr = ERR_peek_last_error(); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())) goto err; if (idx == 0) { OSSL_ERR_STATE_save(es); if (!TEST_ulong_eq(ERR_peek_last_error(), 0)) goto err; } else { OSSL_ERR_STATE_save_to_mark(es); if (!TEST_ulong_ne(ERR_peek_last_error(), 0)) goto err; } for (i = 0; i < 2; i++) { OSSL_ERR_STATE_restore(es); if (!TEST_ulong_eq(ERR_peek_last_error(), interr)) goto err; ERR_peek_last_error_data(&data, &flags); if (!TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; /* restore again to duplicate the entries */ OSSL_ERR_STATE_restore(es); /* verify them all */ if (idx == 0 || i == 0) { if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), mallocfail) || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; } if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), interr) || !TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; if (idx == 0) { if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), mallocfail) || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; } if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), interr) || !TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; if (!TEST_ulong_eq(ERR_get_error(), 0)) goto err; } res = 1; err: OSSL_ERR_STATE_free(es); return res; } int setup_tests(void) { ADD_TEST(preserves_system_error); ADD_TEST(vdata_appends); ADD_TEST(raised_error); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_print_error_format); #endif ADD_TEST(test_marks); ADD_ALL_TESTS(test_save_restore, 2); ADD_TEST(test_clear_error); return 1; }
./openssl/test/cmp_client_test.c
/* * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2019 * Copyright Siemens AG 2015-2019 * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "helpers/cmp_testlib.h" #include "cmp_mock_srv.h" static const char *server_key_f; static const char *server_cert_f; static const char *client_key_f; static const char *client_cert_f; static const char *pkcs10_f; typedef struct test_fixture { const char *test_case_name; OSSL_CMP_CTX *cmp_ctx; OSSL_CMP_SRV_CTX *srv_ctx; int req_type; int expected; STACK_OF(X509) *caPubs; } CMP_SES_TEST_FIXTURE; static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; static EVP_PKEY *server_key = NULL; static X509 *server_cert = NULL; static EVP_PKEY *client_key = NULL; static X509 *client_cert = NULL; static unsigned char ref[CMP_TEST_REFVALUE_LENGTH]; /* * For these unit tests, the client abandons message protection, and for * error messages the mock server does so as well. * Message protection and verification is tested in cmp_lib_test.c */ static void tear_down(CMP_SES_TEST_FIXTURE *fixture) { OSSL_CMP_CTX_free(fixture->cmp_ctx); ossl_cmp_mock_srv_free(fixture->srv_ctx); sk_X509_free(fixture->caPubs); OPENSSL_free(fixture); } static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_SES_TEST_FIXTURE *fixture; OSSL_CMP_CTX *srv_cmp_ctx = NULL; OSSL_CMP_CTX *ctx = NULL; /* for client */ if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL)) || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1) || !ossl_cmp_mock_srv_set1_refCert(fixture->srv_ctx, client_cert) || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert) || (srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert) || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key)) goto err; if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL)) || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out) || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform) || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx) || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1) || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1) || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert) || !OSSL_CMP_CTX_set1_pkey(ctx, client_key) /* client_key is by default used also for newPkey */ || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert) || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref))) goto err; fixture->req_type = -1; return fixture; err: tear_down(fixture); return NULL; } static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt) { return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), OSSL_CMP_PKISTATUS_unspecified) && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx), fixt->expected == OSSL_CMP_PKISTATUS_accepted) && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected); } static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture) { OSSL_CMP_CTX *ctx = fixture->cmp_ctx; ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1); OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL); STACK_OF(OSSL_CMP_ITAV) *itavs; OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav); itavs = OSSL_CMP_exec_GENM_ses(ctx); sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free); return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected) && fixture->expected == OSSL_CMP_PKISTATUS_accepted ? TEST_ptr(itavs) : TEST_ptr_null(itavs); } static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture) { return execute_exec_GENM_ses_test_single(fixture) && OSSL_CMP_CTX_reinit(fixture->cmp_ctx) && execute_exec_GENM_ses_test_single(fixture); } static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture) { OSSL_CMP_CTX *ctx = fixture->cmp_ctx; X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL); int status = OSSL_CMP_CTX_get_status(ctx); OSSL_CMP_CTX_print_errors(ctx); if (!TEST_int_eq(status, fixture->expected) && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans))) return 0; if (fixture->expected != OSSL_CMP_PKISTATUS_accepted) return TEST_ptr_null(res); if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0)) return 0; if (fixture->caPubs != NULL) { STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx); int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0); OSSL_STACK_OF_X509_free(caPubs); return ret; } return 1; } static int test_exec_RR_ses(int request_error) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); if (request_error) OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL); fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request : OSSL_CMP_PKISTATUS_accepted; EXECUTE_TEST(execute_exec_RR_ses_test, tear_down); return result; } static int test_exec_RR_ses_ok(void) { return test_exec_RR_ses(0); } static int test_exec_RR_ses_request_error(void) { return test_exec_RR_ses(1); } static int test_exec_RR_ses_receive_error(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx, OSSL_CMP_PKISTATUS_rejection, OSSL_CMP_CTX_FAILINFO_signerNotTrusted, "test string"); ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR); fixture->expected = OSSL_CMP_PKISTATUS_rejection; EXECUTE_TEST(execute_exec_RR_ses_test, tear_down); return result; } static int test_exec_IR_ses(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->req_type = OSSL_CMP_PKIBODY_IR; fixture->expected = OSSL_CMP_PKISTATUS_accepted; fixture->caPubs = sk_X509_new_null(); sk_X509_push(fixture->caPubs, server_cert); sk_X509_push(fixture->caPubs, server_cert); ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs); EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down); return result; } static int test_exec_REQ_ses_poll(int req_type, int check_after, int poll_count, int total_timeout, int expect) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->req_type = req_type; fixture->expected = expect; ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after); ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count); OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout); if (req_type == OSSL_CMP_PKIBODY_IR) { EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down); } else if (req_type == OSSL_CMP_PKIBODY_GENM) { EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down); } return result; } static int checkAfter = 1; static int test_exec_IR_ses_poll_ok(void) { return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted); } static int test_exec_IR_ses_poll_no_timeout(void) { return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter, 2 /* pollCount */, checkAfter + 4, OSSL_CMP_PKISTATUS_accepted); } static int test_exec_IR_ses_poll_total_timeout(void) { return !test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter + 1, 3 /* pollCount */, checkAfter + 6, OSSL_CMP_PKISTATUS_waiting); } static int test_exec_CR_ses(int implicit_confirm, int granted, int reject) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->req_type = OSSL_CMP_PKIBODY_CR; OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm); OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted); ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, reject ? OSSL_CMP_PKIBODY_CERTCONF : -1); fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted; EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down); return result; } static int test_exec_CR_ses_explicit_confirm(void) { return test_exec_CR_ses(0, 0, 0) && test_exec_CR_ses(0, 0, 1 /* reject */); } static int test_exec_CR_ses_implicit_confirm(void) { return test_exec_CR_ses(1, 0, 0) && test_exec_CR_ses(1, 1 /* granted */, 0); } static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->req_type = OSSL_CMP_PKIBODY_KUR; /* ctx->oldCert has already been set */ if (transfer_error) OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL); if (pubkey) { EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key; EVP_PKEY_up_ref(key); OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key); OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1); } if (pubkey || raverified) OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD, OSSL_CRMF_POPO_RAVERIFIED); fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans : raverified ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted; EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down); return result; } static int test_exec_KUR_ses_ok(void) { return test_exec_KUR_ses(0, 0, 0); } static int test_exec_KUR_ses_transfer_error(void) { return test_exec_KUR_ses(1, 0, 0); } static int test_exec_KUR_ses_wrong_popo(void) { #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */ return test_exec_KUR_ses(0, 0, 1); #else return 1; #endif } static int test_exec_KUR_ses_pub(void) { return test_exec_KUR_ses(0, 1, 0); } static int test_exec_KUR_ses_wrong_pub(void) { return test_exec_KUR_ses(0, 1, 1); } static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info, const char **txt) { int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx); if (*reject) { *txt = "not to my taste"; fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate; } return fail_info; } static int test_exec_P10CR_ses(int reject) { OSSL_CMP_CTX *ctx; X509_REQ *csr = NULL; SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->req_type = OSSL_CMP_PKIBODY_P10CR; fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted; ctx = fixture->cmp_ctx; if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx)) || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr)) || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb)) || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) { tear_down(fixture); fixture = NULL; } X509_REQ_free(csr); EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down); return result; } static int test_exec_P10CR_ses_ok(void) { return test_exec_P10CR_ses(0); } static int test_exec_P10CR_ses_reject(void) { return test_exec_P10CR_ses(1); } static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture) { OSSL_CMP_CTX *ctx = fixture->cmp_ctx; int check_after; const int CHECK_AFTER = 0; const int TYPE = OSSL_CMP_PKIBODY_KUR; ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3); ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER); return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after)) && check_after == CHECK_AFTER && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL) && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after)) && check_after == CHECK_AFTER && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL) && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL)) && TEST_int_eq(0, X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert)); } static int test_try_certreq_poll(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_try_certreq_poll_test, tear_down); return result; } static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture) { OSSL_CMP_CTX *ctx = fixture->cmp_ctx; int check_after; const int CHECK_AFTER = 99; const int TYPE = OSSL_CMP_PKIBODY_CR; ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3); ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER); return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after)) && check_after == CHECK_AFTER && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL) && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL)) && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL); } static int test_try_certreq_poll_abort(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down); return result; } static int test_exec_GENM_ses_poll_ok(void) { return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted); } static int test_exec_GENM_ses_poll_no_timeout(void) { return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter, 1 /* pollCount */, checkAfter + 1, OSSL_CMP_PKISTATUS_accepted); } static int test_exec_GENM_ses_poll_total_timeout(void) { return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter + 1, 3 /* pollCount */, checkAfter + 2, OSSL_CMP_PKISTATUS_waiting); } static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); if (transfer_error) OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL); /* * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT) * here because this will correct total_timeout to be >= 0 */ fixture->cmp_ctx->total_timeout = total_timeout; fixture->expected = expect; EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down); return result; } static int test_exec_GENM_ses_ok(void) { return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted); } static int test_exec_GENM_ses_transfer_error(void) { return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans); } static int test_exec_GENM_ses_total_timeout(void) { return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans); } static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture) { int res = ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID, OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable, "abcdefg"); return TEST_int_eq(fixture->expected, res); } static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture) { int res = ossl_cmp_exchange_error(fixture->cmp_ctx, OSSL_CMP_PKISTATUS_rejection, 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion, "foo_status", 999, "foo_details"); return TEST_int_eq(fixture->expected, res); } static int test_exchange_certConf(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->expected = 0; /* client should not send certConf immediately */ if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_exchange_certConf_test, tear_down); return result; } static int test_exchange_error(void) { SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up); fixture->expected = 1; /* client may send error any time */ EXECUTE_TEST(execute_exchange_error_test, tear_down); return result; } void cleanup_tests(void) { X509_free(server_cert); EVP_PKEY_free(server_key); X509_free(client_cert); EVP_PKEY_free(client_key); OSSL_PROVIDER_unload(default_null_provider); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); return; } #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(server_key_f = test_get_argument(0)) || !TEST_ptr(server_cert_f = test_get_argument(1)) || !TEST_ptr(client_key_f = test_get_argument(2)) || !TEST_ptr(client_cert_f = test_get_argument(3)) || !TEST_ptr(pkcs10_f = test_get_argument(4))) { TEST_error("usage: cmp_client_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE)) return 0; if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx)) || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx)) || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx)) || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx)) || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) { cleanup_tests(); return 0; } ADD_TEST(test_exec_RR_ses_ok); ADD_TEST(test_exec_RR_ses_request_error); ADD_TEST(test_exec_RR_ses_receive_error); ADD_TEST(test_exec_CR_ses_explicit_confirm); ADD_TEST(test_exec_CR_ses_implicit_confirm); ADD_TEST(test_exec_IR_ses); ADD_TEST(test_exec_IR_ses_poll_ok); ADD_TEST(test_exec_IR_ses_poll_no_timeout); ADD_TEST(test_exec_IR_ses_poll_total_timeout); ADD_TEST(test_exec_KUR_ses_ok); ADD_TEST(test_exec_KUR_ses_transfer_error); ADD_TEST(test_exec_KUR_ses_wrong_popo); ADD_TEST(test_exec_KUR_ses_pub); ADD_TEST(test_exec_KUR_ses_wrong_pub); ADD_TEST(test_exec_P10CR_ses_ok); ADD_TEST(test_exec_P10CR_ses_reject); ADD_TEST(test_try_certreq_poll); ADD_TEST(test_try_certreq_poll_abort); ADD_TEST(test_exec_GENM_ses_ok); ADD_TEST(test_exec_GENM_ses_transfer_error); ADD_TEST(test_exec_GENM_ses_total_timeout); ADD_TEST(test_exec_GENM_ses_poll_ok); ADD_TEST(test_exec_GENM_ses_poll_no_timeout); ADD_TEST(test_exec_GENM_ses_poll_total_timeout); ADD_TEST(test_exchange_certConf); ADD_TEST(test_exchange_error); return 1; }
./openssl/test/user_property_test.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/core.h> #include <openssl/core_dispatch.h> #include <openssl/core_names.h> #include <openssl/provider.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include "testutil.h" #define MYPROPERTIES "foo.bar=yes" static OSSL_FUNC_provider_query_operation_fn testprov_query; static OSSL_FUNC_digest_get_params_fn tmpmd_get_params; static OSSL_FUNC_digest_digest_fn tmpmd_digest; static int tmpmd_get_params(OSSL_PARAM params[]) { OSSL_PARAM *p = NULL; p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); if (p != NULL && !OSSL_PARAM_set_size_t(p, 1)) return 0; p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); if (p != NULL && !OSSL_PARAM_set_size_t(p, 1)) return 0; return 1; } static int tmpmd_digest(void *provctx, const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, size_t outsz) { return 0; } static const OSSL_DISPATCH testprovmd_functions[] = { { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))tmpmd_get_params }, { OSSL_FUNC_DIGEST_DIGEST, (void (*)(void))tmpmd_digest }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM testprov_digests[] = { { "testprovmd", MYPROPERTIES, testprovmd_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *testprov_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; return operation_id == OSSL_OP_DIGEST ? testprov_digests : NULL; } static const OSSL_DISPATCH testprov_dispatch_table[] = { { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))testprov_query }, OSSL_DISPATCH_END }; static int testprov_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { *provctx = (void *)handle; *out = testprov_dispatch_table; return 1; } enum { DEFAULT_PROPS_FIRST = 0, DEFAULT_PROPS_AFTER_LOAD, DEFAULT_PROPS_AFTER_FETCH, DEFAULT_PROPS_FINAL }; static int test_default_props_and_providers(int propsorder) { OSSL_LIB_CTX *libctx; OSSL_PROVIDER *testprov = NULL; EVP_MD *testprovmd = NULL; int res = 0; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()) || !TEST_true(OSSL_PROVIDER_add_builtin(libctx, "testprov", testprov_provider_init))) goto err; if (propsorder == DEFAULT_PROPS_FIRST && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; if (!TEST_ptr(testprov = OSSL_PROVIDER_load(libctx, "testprov"))) goto err; if (propsorder == DEFAULT_PROPS_AFTER_LOAD && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL))) goto err; if (propsorder == DEFAULT_PROPS_AFTER_FETCH) { if (!TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; EVP_MD_free(testprovmd); if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL))) goto err; } res = 1; err: EVP_MD_free(testprovmd); OSSL_PROVIDER_unload(testprov); OSSL_LIB_CTX_free(libctx); return res; } int setup_tests(void) { ADD_ALL_TESTS(test_default_props_and_providers, DEFAULT_PROPS_FINAL); return 1; }
./openssl/test/pkey_meth_kdf_test.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Tests of the EVP_PKEY_CTX_set_* macro family */ #include <stdio.h> #include <string.h> #include <openssl/evp.h> #include <openssl/kdf.h> #include "testutil.h" static int test_kdf_tls1_prf(void) { int ret = 0; EVP_PKEY_CTX *pctx; unsigned char out[16]; size_t outlen = sizeof(out); if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL)) == NULL) { TEST_error("EVP_PKEY_TLS1_PRF"); goto err; } if (EVP_PKEY_derive_init(pctx) <= 0) { TEST_error("EVP_PKEY_derive_init"); goto err; } if (EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_sha256()) <= 0) { TEST_error("EVP_PKEY_CTX_set_tls1_prf_md"); goto err; } if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, (unsigned char *)"secret", 6) <= 0) { TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret"); goto err; } if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, (unsigned char *)"seed", 4) <= 0) { TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed"); goto err; } if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { TEST_error("EVP_PKEY_derive"); goto err; } { const unsigned char expected[sizeof(out)] = { 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0, 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc }; if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { goto err; } } ret = 1; err: EVP_PKEY_CTX_free(pctx); return ret; } static int test_kdf_hkdf(void) { int ret = 0; EVP_PKEY_CTX *pctx; unsigned char out[10]; size_t outlen = sizeof(out); if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) { TEST_error("EVP_PKEY_HKDF"); goto err; } if (EVP_PKEY_derive_init(pctx) <= 0) { TEST_error("EVP_PKEY_derive_init"); goto err; } if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) { TEST_error("EVP_PKEY_CTX_set_hkdf_md"); goto err; } if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, (const unsigned char *)"salt", 4) <= 0) { TEST_error("EVP_PKEY_CTX_set1_hkdf_salt"); goto err; } if (EVP_PKEY_CTX_set1_hkdf_key(pctx, (const unsigned char *)"secret", 6) <= 0) { TEST_error("EVP_PKEY_CTX_set1_hkdf_key"); goto err; } if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"label", 5) <= 0) { TEST_error("EVP_PKEY_CTX_set1_hkdf_info"); goto err; } if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { TEST_error("EVP_PKEY_derive"); goto err; } { const unsigned char expected[sizeof(out)] = { 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13 }; if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { goto err; } } ret = 1; err: EVP_PKEY_CTX_free(pctx); return ret; } #ifndef OPENSSL_NO_SCRYPT static int test_kdf_scrypt(void) { int ret = 0; EVP_PKEY_CTX *pctx; unsigned char out[64]; size_t outlen = sizeof(out); if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) { TEST_error("EVP_PKEY_SCRYPT"); goto err; } if (EVP_PKEY_derive_init(pctx) <= 0) { TEST_error("EVP_PKEY_derive_init"); goto err; } if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) { TEST_error("EVP_PKEY_CTX_set1_pbe_pass"); goto err; } if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, (unsigned char *)"NaCl", 4) <= 0) { TEST_error("EVP_PKEY_CTX_set1_scrypt_salt"); goto err; } if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) { TEST_error("EVP_PKEY_CTX_set_scrypt_N"); goto err; } if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) { TEST_error("EVP_PKEY_CTX_set_scrypt_r"); goto err; } if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) { TEST_error("EVP_PKEY_CTX_set_scrypt_p"); goto err; } if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) { TEST_error("EVP_PKEY_CTX_set_maxmem_bytes"); goto err; } if (EVP_PKEY_derive(pctx, out, &outlen) > 0) { TEST_error("EVP_PKEY_derive should have failed"); goto err; } if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) { TEST_error("EVP_PKEY_CTX_set_maxmem_bytes"); goto err; } if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { TEST_error("EVP_PKEY_derive"); goto err; } { const unsigned char expected[sizeof(out)] = { 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40 }; if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { goto err; } } ret = 1; err: EVP_PKEY_CTX_free(pctx); return ret; } #endif int setup_tests(void) { ADD_TEST(test_kdf_tls1_prf); ADD_TEST(test_kdf_hkdf); #ifndef OPENSSL_NO_SCRYPT ADD_TEST(test_kdf_scrypt); #endif return 1; }
./openssl/test/param_build_test.c
/* * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/params.h> #include <openssl/param_build.h> #include "internal/nelem.h" #include "testutil.h" static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END }; static int template_public_single_zero_test(int idx) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; int res = 0; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(zbn = BN_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", idx == 0 ? zbn : NULL)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; params = params_blt; /* Check BN (zero BN becomes unsigned integer) */ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_BN_eq(zbn_res, zbn)) goto err; res = 1; err: if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); BN_free(zbn); BN_free(zbn_res); return res; } static int template_private_single_zero_test(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; int res = 0; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(zbn = BN_secure_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; params = params_blt; /* Check BN (zero BN becomes unsigned integer) */ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_int_eq(BN_get_flags(zbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(zbn_res, zbn)) goto err; res = 1; err: if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); BN_free(zbn); BN_free(zbn_res); return res; } static int template_public_test(int tstid) { OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; BIGNUM *pbn = NULL, *pbn_res = NULL; BIGNUM *nbn = NULL, *nbn_res = NULL; int i; long int l; int32_t i32; int64_t i64; double d; time_t t; char *utf = NULL; const char *cutf; int res = 0; if (!TEST_ptr(bld) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999)) || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224)) || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875)) || !TEST_ptr(zbn = BN_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(pbn = BN_new()) || !TEST_true(BN_set_word(pbn, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) || !TEST_ptr(nbn = BN_secure_new()) || !TEST_true(BN_set_word(nbn, 1733)) || !TEST_true((BN_set_negative(nbn, 1), 1)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo", sizeof("foo"))) || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom", 0)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; switch (tstid) { case 0: params = params_blt; break; case 1: params = OSSL_PARAM_merge(params_blt, params_empty); break; case 2: params = OSSL_PARAM_dup(params_blt); break; case 3: p1 = OSSL_PARAM_merge(params_blt, params_empty); params = OSSL_PARAM_dup(p1); break; default: p1 = OSSL_PARAM_dup(params_blt); params = OSSL_PARAM_merge(p1, params_empty); break; } /* Check int */ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_true(OSSL_PARAM_get_int(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_int_eq(i, -6) /* Check int32 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_true(OSSL_PARAM_get_int32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_int_eq((int)i32, 1532) /* Check int64 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_int64(p, &i64)) || !TEST_long_eq((long)i64, -9999999) /* Check long */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(long int)) || !TEST_true(OSSL_PARAM_get_long(p, &l)) || !TEST_long_eq(l, 42) /* Check time_t */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "t")) || !TEST_str_eq(p->key, "t") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(time_t)) || !TEST_true(OSSL_PARAM_get_time_t(p, &t)) || !TEST_time_t_eq(t, 11224) /* Check double */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "d")) || !TEST_true(OSSL_PARAM_get_double(p, &d)) || !TEST_str_eq(p->key, "d") || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL) || !TEST_size_t_eq(p->data_size, sizeof(double)) || !TEST_double_eq(d, 1.61803398875) /* Check UTF8 string */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s")) || !TEST_str_eq(p->data, "foo") || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0)) || !TEST_str_eq(utf, "foo") /* Check UTF8 pointer */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p")) || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf)) || !TEST_str_eq(cutf, "bar-boom") /* Check BN (zero BN becomes unsigned integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_BN_eq(zbn_res, zbn) /* Check BN (positive BN becomes unsigned integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) || !TEST_str_eq(p->key, "bignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) || !TEST_BN_eq(pbn_res, pbn) /* Check BN (negative BN becomes signed integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) || !TEST_str_eq(p->key, "negativebignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) || !TEST_BN_eq(nbn_res, nbn)) goto err; res = 1; err: OPENSSL_free(p1); if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); OPENSSL_free(utf); BN_free(zbn); BN_free(zbn_res); BN_free(pbn); BN_free(pbn_res); BN_free(nbn); BN_free(nbn_res); return res; } static int template_private_test(int tstid) { int *data1 = NULL, *data2 = NULL, j; const int data1_num = 12; const int data1_size = data1_num * sizeof(int); const int data2_num = 5; const int data2_size = data2_num * sizeof(int); OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; unsigned int i; unsigned long int l; uint32_t i32; uint64_t i64; size_t st; BIGNUM *zbn = NULL, *zbn_res = NULL; BIGNUM *pbn = NULL, *pbn_res = NULL; BIGNUM *nbn = NULL, *nbn_res = NULL; int res = 0; if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size)) || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size)) || !TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; for (j = 0; j < data1_num; j++) data1[j] = -16 * j; for (j = 0; j < data2_num; j++) data2[j] = 2 * j; if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) || !TEST_ptr(zbn = BN_secure_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(pbn = BN_secure_new()) || !TEST_true(BN_set_word(pbn, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) || !TEST_ptr(nbn = BN_secure_new()) || !TEST_true(BN_set_word(nbn, 1733)) || !TEST_true((BN_set_negative(nbn, 1), 1)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1, data1_size)) || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2, data2_size)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; switch (tstid) { case 0: params = params_blt; break; case 1: params = OSSL_PARAM_merge(params_blt, params_empty); break; case 2: params = OSSL_PARAM_dup(params_blt); break; case 3: p1 = OSSL_PARAM_merge(params_blt, params_empty); params = OSSL_PARAM_dup(p1); break; default: p1 = OSSL_PARAM_dup(params_blt); params = OSSL_PARAM_merge(p1, params_empty); break; } /* Check unsigned int */ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(OSSL_PARAM_get_uint(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_uint_eq(i, 6) /* Check unsigned int32 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_uint_eq((unsigned int)i32, 1532) /* Check unsigned int64 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) || !TEST_ulong_eq((unsigned long)i64, 9999999) /* Check unsigned long int */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) || !TEST_ulong_eq(l, 42) /* Check size_t */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "st") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(size_t)) || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) || !TEST_size_t_eq(st, 65537) /* Check octet string */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "oct_s") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) || !TEST_mem_eq(p->data, p->data_size, data1, data1_size) /* Check octet pointer */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data)) || !TEST_str_eq(p->key, "oct_p") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size) /* Check BN (zero BN becomes unsigned integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(zbn_res, zbn) /* Check BN (positive BN becomes unsigned integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "bignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(pbn_res, pbn) /* Check BN (negative BN becomes signed integer) */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "negativebignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) || !TEST_int_eq(BN_get_flags(nbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(nbn_res, nbn)) goto err; res = 1; err: OSSL_PARAM_free(p1); if (params != params_blt) OSSL_PARAM_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); OPENSSL_secure_free(data1); OPENSSL_secure_free(data2); BN_free(zbn); BN_free(zbn_res); BN_free(pbn); BN_free(pbn_res); BN_free(nbn); BN_free(nbn_res); return res; } static int builder_limit_test(void) { const int n = 100; char names[100][3]; OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL; int i, res = 0; if (!TEST_ptr(bld)) goto err; for (i = 0; i < n; i++) { names[i][0] = 'A' + (i / 26) - 1; names[i][1] = 'a' + (i % 26) - 1; names[i][2] = '\0'; if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1))) goto err; } if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; /* Count the elements in the params array, expecting n */ for (i = 0; params[i].key != NULL; i++); if (!TEST_int_eq(i, n)) goto err; /* Verify that the build, cleared the builder structure */ OSSL_PARAM_free(params); params = NULL; if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2)) || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; /* Count the elements in the params array, expecting 1 */ for (i = 0; params[i].key != NULL; i++); if (!TEST_int_eq(i, 1)) goto err; res = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); return res; } static int builder_merge_test(void) { static int data1[] = { 2, 3, 5, 7, 11, 15, 17 }; static unsigned char data2[] = { 2, 4, 6, 8, 10 }; OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p; unsigned int i; unsigned long int l; uint32_t i32; uint64_t i64; size_t st; BIGNUM *bn_priv = NULL, *bn_priv_res = NULL; BIGNUM *bn_pub = NULL, *bn_pub_res = NULL; int res = 0; if (!TEST_ptr(bld) || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) || !TEST_ptr(bn_priv = BN_secure_new()) || !TEST_true(BN_set_word(bn_priv, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(bld2) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1, sizeof(data1))) || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2, sizeof(data2))) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99)) || !TEST_ptr(bn_pub = BN_new()) || !TEST_true(BN_set_word(bn_pub, 0x42)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub)) || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2))) goto err; if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt))) goto err; if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_true(OSSL_PARAM_get_uint(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_uint_eq(i, 6) /* Check unsigned int32 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_uint_eq((unsigned int)i32, 99) /* Check unsigned int64 */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) || !TEST_ulong_eq((unsigned long)i64, 9999999) /* Check unsigned long int */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) || !TEST_ulong_eq(l, 42) /* Check size_t */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) || !TEST_str_eq(p->key, "st") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(size_t)) || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) || !TEST_size_t_eq(st, 65537) /* Check octet string */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) || !TEST_str_eq(p->key, "oct_s") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1)) /* Check octet pointer */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) || !TEST_str_eq(p->key, "oct_p") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2)) /* Check BN */ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub")) || !TEST_str_eq(p->key, "bignumber_pub") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res)) || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0) || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv")) || !TEST_str_eq(p->key, "bignumber_priv") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res)) || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0)) goto err; res = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_free(params2_blt); OSSL_PARAM_BLD_free(bld); OSSL_PARAM_BLD_free(bld2); BN_free(bn_priv); BN_free(bn_priv_res); BN_free(bn_pub); BN_free(bn_pub_res); return res; } int setup_tests(void) { ADD_ALL_TESTS(template_public_single_zero_test, 2); ADD_ALL_TESTS(template_public_test, 5); /* Only run the secure memory testing if we have secure memory available */ if (CRYPTO_secure_malloc_init(1<<16, 16)) { ADD_TEST(template_private_single_zero_test); ADD_ALL_TESTS(template_private_test, 5); } ADD_TEST(builder_limit_test); ADD_TEST(builder_merge_test); return 1; }
./openssl/test/modes_internal_test.c
/* * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* Internal tests for the modes module */ /* * This file uses the low level AES functions (which are deprecated for * non-internal use) in order to test the modes code */ #include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <openssl/aes.h> #include <openssl/modes.h> #include "testutil.h" #include "crypto/modes.h" #include "internal/nelem.h" typedef struct { size_t size; const unsigned char *data; } SIZED_DATA; /********************************************************************** * * Test of cts128 * ***/ /* cts128 test vectors from RFC 3962 */ static const unsigned char cts128_test_key[16] = "chicken teriyaki"; static const unsigned char cts128_test_input[64] = "I would like the" " General Gau's C" "hicken, please, " "and wonton soup."; static const unsigned char cts128_test_iv[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static const unsigned char vector_17[17] = { 0xc6, 0x35, 0x35, 0x68, 0xf2, 0xbf, 0x8c, 0xb4, 0xd8, 0xa5, 0x80, 0x36, 0x2d, 0xa7, 0xff, 0x7f, 0x97 }; static const unsigned char vector_31[31] = { 0xfc, 0x00, 0x78, 0x3e, 0x0e, 0xfd, 0xb2, 0xc1, 0xd4, 0x45, 0xd4, 0xc8, 0xef, 0xf7, 0xed, 0x22, 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0, 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5 }; static const unsigned char vector_32[32] = { 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5, 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8, 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0, 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84 }; static const unsigned char vector_47[47] = { 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0, 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84, 0xb3, 0xff, 0xfd, 0x94, 0x0c, 0x16, 0xa1, 0x8c, 0x1b, 0x55, 0x49, 0xd2, 0xf8, 0x38, 0x02, 0x9e, 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5, 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5 }; static const unsigned char vector_48[48] = { 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0, 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84, 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0, 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8, 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5, 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8 }; static const unsigned char vector_64[64] = { 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0, 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84, 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5, 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8, 0x48, 0x07, 0xef, 0xe8, 0x36, 0xee, 0x89, 0xa5, 0x26, 0x73, 0x0d, 0xbc, 0x2f, 0x7b, 0xc8, 0x40, 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0, 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8 }; #define CTS128_TEST_VECTOR(len) \ { \ sizeof(vector_##len), vector_##len \ } static const SIZED_DATA aes_cts128_vectors[] = { CTS128_TEST_VECTOR(17), CTS128_TEST_VECTOR(31), CTS128_TEST_VECTOR(32), CTS128_TEST_VECTOR(47), CTS128_TEST_VECTOR(48), CTS128_TEST_VECTOR(64), }; static AES_KEY *cts128_encrypt_key_schedule(void) { static int init_key = 1; static AES_KEY ks; if (init_key) { AES_set_encrypt_key(cts128_test_key, 128, &ks); init_key = 0; } return &ks; } static AES_KEY *cts128_decrypt_key_schedule(void) { static int init_key = 1; static AES_KEY ks; if (init_key) { AES_set_decrypt_key(cts128_test_key, 128, &ks); init_key = 0; } return &ks; } typedef struct { const char *case_name; size_t (*last_blocks_correction)(const unsigned char *in, unsigned char *out, size_t len); size_t (*encrypt_block)(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block); size_t (*encrypt_stream)(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc); size_t (*decrypt_block)(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block); size_t (*decrypt_stream)(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc); } CTS128_FIXTURE; static size_t last_blocks_correction(const unsigned char *in, unsigned char *out, size_t len) { size_t tail; memcpy(out, in, len); if ((tail = len % 16) == 0) tail = 16; tail += 16; return tail; } static size_t last_blocks_correction_nist(const unsigned char *in, unsigned char *out, size_t len) { size_t tail; if ((tail = len % 16) == 0) tail = 16; len -= 16 + tail; memcpy(out, in, len); /* flip two last blocks */ memcpy(out + len, in + len + 16, tail); memcpy(out + len + tail, in + len, 16); len += 16 + tail; tail = 16; return tail; } static int execute_cts128(const CTS128_FIXTURE *fixture, int num) { const unsigned char *test_iv = cts128_test_iv; size_t test_iv_len = sizeof(cts128_test_iv); const unsigned char *orig_vector = aes_cts128_vectors[num].data; size_t len = aes_cts128_vectors[num].size; const unsigned char *test_input = cts128_test_input; const AES_KEY *encrypt_key_schedule = cts128_encrypt_key_schedule(); const AES_KEY *decrypt_key_schedule = cts128_decrypt_key_schedule(); unsigned char iv[16]; /* The largest test inputs are = 64 bytes. */ unsigned char cleartext[64], ciphertext[64], vector[64]; size_t tail, size; TEST_info("%s_vector_%lu", fixture->case_name, (unsigned long)len); tail = fixture->last_blocks_correction(orig_vector, vector, len); /* test block-based encryption */ memcpy(iv, test_iv, test_iv_len); if (!TEST_size_t_eq(fixture->encrypt_block(test_input, ciphertext, len, encrypt_key_schedule, iv, (block128_f)AES_encrypt), len) || !TEST_mem_eq(ciphertext, len, vector, len) || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) return 0; /* test block-based decryption */ memcpy(iv, test_iv, test_iv_len); size = fixture->decrypt_block(ciphertext, cleartext, len, decrypt_key_schedule, iv, (block128_f)AES_decrypt); if (!TEST_true(len == size || len + 16 == size) || !TEST_mem_eq(cleartext, len, test_input, len) || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) return 0; /* test streamed encryption */ memcpy(iv, test_iv, test_iv_len); if (!TEST_size_t_eq(fixture->encrypt_stream(test_input, ciphertext, len, encrypt_key_schedule, iv, (cbc128_f) AES_cbc_encrypt), len) || !TEST_mem_eq(ciphertext, len, vector, len) || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) return 0; /* test streamed decryption */ memcpy(iv, test_iv, test_iv_len); if (!TEST_size_t_eq(fixture->decrypt_stream(ciphertext, cleartext, len, decrypt_key_schedule, iv, (cbc128_f)AES_cbc_encrypt), len) || !TEST_mem_eq(cleartext, len, test_input, len) || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) return 0; return 1; } static int test_aes_cts128(int idx) { static const CTS128_FIXTURE fixture_cts128 = { "aes_cts128", last_blocks_correction, CRYPTO_cts128_encrypt_block, CRYPTO_cts128_encrypt, CRYPTO_cts128_decrypt_block, CRYPTO_cts128_decrypt }; return execute_cts128(&fixture_cts128, idx); } static int test_aes_cts128_nist(int idx) { static const CTS128_FIXTURE fixture_cts128_nist = { "aes_cts128_nist", last_blocks_correction_nist, CRYPTO_nistcts128_encrypt_block, CRYPTO_nistcts128_encrypt, CRYPTO_nistcts128_decrypt_block, CRYPTO_nistcts128_decrypt }; return execute_cts128(&fixture_cts128_nist, idx); } /* * * Test of gcm128 * */ /* Test Case 1 */ static const u8 K1[16], P1[] = { 0 }, A1[] = { 0 }, IV1[12], C1[] = { 0 }; static const u8 T1[] = { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61, 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a }; /* Test Case 2 */ # define K2 K1 # define A2 A1 # define IV2 IV1 static const u8 P2[16]; static const u8 C2[] = { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78 }; static const u8 T2[] = { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd, 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf }; /* Test Case 3 */ # define A3 A2 static const u8 K3[] = { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 }; static const u8 P3[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }; static const u8 IV3[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88 }; static const u8 C3[] = { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 }; static const u8 T3[] = { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6, 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4 }; /* Test Case 4 */ # define K4 K3 # define IV4 IV3 static const u8 P4[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39 }; static const u8 A4[] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }; static const u8 C4[] = { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 0x3d, 0x58, 0xe0, 0x91 }; static const u8 T4[] = { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb, 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47 }; /* Test Case 5 */ # define K5 K4 # define P5 P4 # define A5 A4 static const u8 IV5[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad }; static const u8 C5[] = { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a, 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55, 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8, 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23, 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2, 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42, 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07, 0xc2, 0x3f, 0x45, 0x98 }; static const u8 T5[] = { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85, 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb }; /* Test Case 6 */ # define K6 K5 # define P6 P5 # define A6 A5 static const u8 IV6[] = { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5, 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa, 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1, 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28, 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39, 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54, 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57, 0xa6, 0x37, 0xb3, 0x9b }; static const u8 C6[] = { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6, 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94, 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8, 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7, 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90, 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f, 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03, 0x4c, 0x34, 0xae, 0xe5 }; static const u8 T6[] = { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa, 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50 }; /* Test Case 7 */ static const u8 K7[24], P7[] = { 0 }, A7[] = { 0 }, IV7[12], C7[] = { 0 }; static const u8 T7[] = { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b, 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35 }; /* Test Case 8 */ # define K8 K7 # define IV8 IV7 # define A8 A7 static const u8 P8[16]; static const u8 C8[] = { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41, 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00 }; static const u8 T8[] = { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab, 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb }; /* Test Case 9 */ # define A9 A8 static const u8 K9[] = { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c }; static const u8 P9[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }; static const u8 IV9[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88 }; static const u8 C9[] = { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57, 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84, 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25, 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47, 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9, 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 }; static const u8 T9[] = { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf, 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14 }; /* Test Case 10 */ # define K10 K9 # define IV10 IV9 static const u8 P10[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39 }; static const u8 A10[] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }; static const u8 C10[] = { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57, 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84, 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25, 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47, 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9, 0xcc, 0xda, 0x27, 0x10 }; static const u8 T10[] = { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f, 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c }; /* Test Case 11 */ # define K11 K10 # define P11 P10 # define A11 A10 static const u8 IV11[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad }; static const u8 C11[] = { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54, 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8, 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f, 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57, 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75, 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9, 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f, 0xa0, 0xf0, 0x62, 0xf7 }; static const u8 T11[] = { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24, 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8 }; /* Test Case 12 */ # define K12 K11 # define P12 P11 # define A12 A11 static const u8 IV12[] = { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5, 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa, 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1, 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28, 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39, 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54, 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57, 0xa6, 0x37, 0xb3, 0x9b }; static const u8 C12[] = { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c, 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff, 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef, 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45, 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9, 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3, 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7, 0xe9, 0xb7, 0x37, 0x3b }; static const u8 T12[] = { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb, 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9 }; /* Test Case 13 */ static const u8 K13[32], P13[] = { 0 }, A13[] = { 0 }, IV13[12], C13[] = { 0 }; static const u8 T13[] = { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b }; /* Test Case 14 */ # define K14 K13 # define A14 A13 static const u8 P14[16], IV14[12]; static const u8 C14[] = { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 }; static const u8 T14[] = { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 }; /* Test Case 15 */ # define A15 A14 static const u8 K15[] = { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 }; static const u8 P15[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }; static const u8 IV15[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88 }; static const u8 C15[] = { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad }; static const u8 T15[] = { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd, 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c }; /* Test Case 16 */ # define K16 K15 # define IV16 IV15 static const u8 P16[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39 }; static const u8 A16[] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }; static const u8 C16[] = { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62 }; static const u8 T16[] = { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b }; /* Test Case 17 */ # define K17 K16 # define P17 P16 # define A17 A16 static const u8 IV17[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad }; static const u8 C17[] = { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa, 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78, 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f }; static const u8 T17[] = { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 }; /* Test Case 18 */ # define K18 K17 # define P18 P17 # define A18 A17 static const u8 IV18[] = { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5, 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa, 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1, 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28, 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39, 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54, 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57, 0xa6, 0x37, 0xb3, 0x9b }; static const u8 C18[] = { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1, 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20, 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19, 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4, 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45, 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde, 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e, 0x44, 0xae, 0x7e, 0x3f }; static const u8 T18[] = { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0, 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a }; /* Test Case 19 */ # define K19 K1 # define P19 P1 # define IV19 IV1 # define C19 C1 static const u8 A19[] = { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55, 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad }; static const u8 T19[] = { 0x5f, 0xea, 0x79, 0x3a, 0x2d, 0x6f, 0x97, 0x4d, 0x37, 0xe6, 0x8e, 0x0c, 0xb8, 0xff, 0x94, 0x92 }; /* Test Case 20 */ # define K20 K1 # define A20 A1 /* this results in 0xff in counter LSB */ static const u8 IV20[64] = { 0xff, 0xff, 0xff, 0xff }; static const u8 P20[288]; static const u8 C20[] = { 0x56, 0xb3, 0x37, 0x3c, 0xa9, 0xef, 0x6e, 0x4a, 0x2b, 0x64, 0xfe, 0x1e, 0x9a, 0x17, 0xb6, 0x14, 0x25, 0xf1, 0x0d, 0x47, 0xa7, 0x5a, 0x5f, 0xce, 0x13, 0xef, 0xc6, 0xbc, 0x78, 0x4a, 0xf2, 0x4f, 0x41, 0x41, 0xbd, 0xd4, 0x8c, 0xf7, 0xc7, 0x70, 0x88, 0x7a, 0xfd, 0x57, 0x3c, 0xca, 0x54, 0x18, 0xa9, 0xae, 0xff, 0xcd, 0x7c, 0x5c, 0xed, 0xdf, 0xc6, 0xa7, 0x83, 0x97, 0xb9, 0xa8, 0x5b, 0x49, 0x9d, 0xa5, 0x58, 0x25, 0x72, 0x67, 0xca, 0xab, 0x2a, 0xd0, 0xb2, 0x3c, 0xa4, 0x76, 0xa5, 0x3c, 0xb1, 0x7f, 0xb4, 0x1c, 0x4b, 0x8b, 0x47, 0x5c, 0xb4, 0xf3, 0xf7, 0x16, 0x50, 0x94, 0xc2, 0x29, 0xc9, 0xe8, 0xc4, 0xdc, 0x0a, 0x2a, 0x5f, 0xf1, 0x90, 0x3e, 0x50, 0x15, 0x11, 0x22, 0x13, 0x76, 0xa1, 0xcd, 0xb8, 0x36, 0x4c, 0x50, 0x61, 0xa2, 0x0c, 0xae, 0x74, 0xbc, 0x4a, 0xcd, 0x76, 0xce, 0xb0, 0xab, 0xc9, 0xfd, 0x32, 0x17, 0xef, 0x9f, 0x8c, 0x90, 0xbe, 0x40, 0x2d, 0xdf, 0x6d, 0x86, 0x97, 0xf4, 0xf8, 0x80, 0xdf, 0xf1, 0x5b, 0xfb, 0x7a, 0x6b, 0x28, 0x24, 0x1e, 0xc8, 0xfe, 0x18, 0x3c, 0x2d, 0x59, 0xe3, 0xf9, 0xdf, 0xff, 0x65, 0x3c, 0x71, 0x26, 0xf0, 0xac, 0xb9, 0xe6, 0x42, 0x11, 0xf4, 0x2b, 0xae, 0x12, 0xaf, 0x46, 0x2b, 0x10, 0x70, 0xbe, 0xf1, 0xab, 0x5e, 0x36, 0x06, 0x87, 0x2c, 0xa1, 0x0d, 0xee, 0x15, 0xb3, 0x24, 0x9b, 0x1a, 0x1b, 0x95, 0x8f, 0x23, 0x13, 0x4c, 0x4b, 0xcc, 0xb7, 0xd0, 0x32, 0x00, 0xbc, 0xe4, 0x20, 0xa2, 0xf8, 0xeb, 0x66, 0xdc, 0xf3, 0x64, 0x4d, 0x14, 0x23, 0xc1, 0xb5, 0x69, 0x90, 0x03, 0xc1, 0x3e, 0xce, 0xf4, 0xbf, 0x38, 0xa3, 0xb6, 0x0e, 0xed, 0xc3, 0x40, 0x33, 0xba, 0xc1, 0x90, 0x27, 0x83, 0xdc, 0x6d, 0x89, 0xe2, 0xe7, 0x74, 0x18, 0x8a, 0x43, 0x9c, 0x7e, 0xbc, 0xc0, 0x67, 0x2d, 0xbd, 0xa4, 0xdd, 0xcf, 0xb2, 0x79, 0x46, 0x13, 0xb0, 0xbe, 0x41, 0x31, 0x5e, 0xf7, 0x78, 0x70, 0x8a, 0x70, 0xee, 0x7d, 0x75, 0x16, 0x5c }; static const u8 T20[] = { 0x8b, 0x30, 0x7f, 0x6b, 0x33, 0x28, 0x6d, 0x0a, 0xb0, 0x26, 0xa9, 0xed, 0x3f, 0xe1, 0xe8, 0x5f }; #define GCM128_TEST_VECTOR(n) \ { \ {sizeof(K##n), K##n}, \ {sizeof(IV##n), IV##n}, \ {sizeof(A##n), A##n}, \ {sizeof(P##n), P##n}, \ {sizeof(C##n), C##n}, \ {sizeof(T##n), T##n} \ } static struct gcm128_data { const SIZED_DATA K; const SIZED_DATA IV; const SIZED_DATA A; const SIZED_DATA P; const SIZED_DATA C; const SIZED_DATA T; } gcm128_vectors[] = { GCM128_TEST_VECTOR(1), GCM128_TEST_VECTOR(2), GCM128_TEST_VECTOR(3), GCM128_TEST_VECTOR(4), GCM128_TEST_VECTOR(5), GCM128_TEST_VECTOR(6), GCM128_TEST_VECTOR(7), GCM128_TEST_VECTOR(8), GCM128_TEST_VECTOR(9), GCM128_TEST_VECTOR(10), GCM128_TEST_VECTOR(11), GCM128_TEST_VECTOR(12), GCM128_TEST_VECTOR(13), GCM128_TEST_VECTOR(14), GCM128_TEST_VECTOR(15), GCM128_TEST_VECTOR(16), GCM128_TEST_VECTOR(17), GCM128_TEST_VECTOR(18), GCM128_TEST_VECTOR(19), GCM128_TEST_VECTOR(20) }; static int test_gcm128(int idx) { unsigned char out[512]; SIZED_DATA K = gcm128_vectors[idx].K; SIZED_DATA IV = gcm128_vectors[idx].IV; SIZED_DATA A = gcm128_vectors[idx].A; SIZED_DATA P = gcm128_vectors[idx].P; SIZED_DATA C = gcm128_vectors[idx].C; SIZED_DATA T = gcm128_vectors[idx].T; GCM128_CONTEXT ctx; AES_KEY key; /* Size 1 inputs are special-cased to signal NULL. */ if (A.size == 1) A.data = NULL; if (P.size == 1) P.data = NULL; if (C.size == 1) C.data = NULL; AES_set_encrypt_key(K.data, K.size * 8, &key); CRYPTO_gcm128_init(&ctx, &key, (block128_f)AES_encrypt); CRYPTO_gcm128_setiv(&ctx, IV.data, IV.size); memset(out, 0, P.size); if (A.data != NULL) CRYPTO_gcm128_aad(&ctx, A.data, A.size); if (P.data != NULL) if (!TEST_int_ge(CRYPTO_gcm128_encrypt(&ctx, P.data, out, P.size), 0)) return 0; if (!TEST_false(CRYPTO_gcm128_finish(&ctx, T.data, 16)) || (C.data != NULL && !TEST_mem_eq(out, P.size, C.data, P.size))) return 0; CRYPTO_gcm128_setiv(&ctx, IV.data, IV.size); memset(out, 0, P.size); if (A.data != NULL) CRYPTO_gcm128_aad(&ctx, A.data, A.size); if (C.data != NULL) CRYPTO_gcm128_decrypt(&ctx, C.data, out, P.size); if (!TEST_false(CRYPTO_gcm128_finish(&ctx, T.data, 16)) || (P.data != NULL && !TEST_mem_eq(out, P.size, P.data, P.size))) return 0; return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_aes_cts128, OSSL_NELEM(aes_cts128_vectors)); ADD_ALL_TESTS(test_aes_cts128_nist, OSSL_NELEM(aes_cts128_vectors)); ADD_ALL_TESTS(test_gcm128, OSSL_NELEM(gcm128_vectors)); return 1; }
./openssl/test/quicfaultstest.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/ssl.h> #include "helpers/quictestlib.h" #include "internal/quic_error.h" #include "testutil.h" static char *cert = NULL; static char *privkey = NULL; /* * Basic test that just creates a connection and sends some data without any * faults injected. */ static int test_basic(void) { int testresult = 0; SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method()); QUIC_TSERVER *qtserv = NULL; SSL *cssl = NULL; char *msg = "Hello World!"; size_t msglen = strlen(msg); unsigned char buf[80]; size_t bytesread; if (!TEST_ptr(cctx)) goto err; if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, 0, &qtserv, &cssl, NULL, NULL))) goto err; if (!TEST_true(qtest_create_quic_connection(qtserv, cssl))) goto err; if (!TEST_int_eq(SSL_write(cssl, msg, msglen), msglen)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf), &bytesread))) goto err; /* * We assume the entire message is read from the server in one go. In * theory this could get fragmented but its a small message so we assume * not. */ if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; testresult = 1; err: SSL_free(cssl); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } /* * Test that adding an unknown frame type is handled correctly */ static int add_unknown_frame_cb(QTEST_FAULT *fault, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len, void *cbarg) { static size_t done = 0; /* * There are no "reserved" frame types which are definitately safe for us * to use for testing purposes - but we just use the highest possible * value (8 byte length integer) and with no payload bytes */ unsigned char unknown_frame[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; /* We only ever add the unknown frame to one packet */ if (done++) return 1; return qtest_fault_prepend_frame(fault, unknown_frame, sizeof(unknown_frame)); } static int test_unknown_frame(void) { int testresult = 0, ret; SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method()); QUIC_TSERVER *qtserv = NULL; SSL *cssl = NULL; char *msg = "Hello World!"; size_t msglen = strlen(msg); unsigned char buf[80]; size_t byteswritten; QTEST_FAULT *fault = NULL; uint64_t sid = UINT64_MAX; if (!TEST_ptr(cctx)) goto err; if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, 0, &qtserv, &cssl, &fault, NULL))) goto err; if (!TEST_true(qtest_create_quic_connection(qtserv, cssl))) goto err; /* * Write a message from the server to the client and add an unknown frame * type */ if (!TEST_true(qtest_fault_set_packet_plain_listener(fault, add_unknown_frame_cb, NULL))) goto err; if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, /*is_uni=*/0, &sid)) || !TEST_uint64_t_eq(sid, 1)) goto err; if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, msglen, &byteswritten))) goto err; if (!TEST_size_t_eq(msglen, byteswritten)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(SSL_handle_events(cssl))) goto err; if (!TEST_int_le(ret = SSL_read(cssl, buf, sizeof(buf)), 0)) goto err; if (!TEST_int_eq(SSL_get_error(cssl, ret), SSL_ERROR_SSL)) goto err; if (!TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_QUIC_PROTOCOL_ERROR)) goto err; if (!TEST_true(qtest_check_server_frame_encoding_err(qtserv))) goto err; testresult = 1; err: qtest_fault_free(fault); SSL_free(cssl); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } /* * Test that a server that fails to provide transport params cannot be * connected to. */ static int drop_extensions_cb(QTEST_FAULT *fault, QTEST_ENCRYPTED_EXTENSIONS *ee, size_t eelen, void *encextcbarg) { int *ext = (int *)encextcbarg; if (!qtest_fault_delete_extension(fault, *ext, ee->extensions, &ee->extensionslen, NULL)) return 0; return 1; } static int test_drop_extensions(int idx) { int testresult = 0; SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method()); QUIC_TSERVER *qtserv = NULL; SSL *cssl = NULL; QTEST_FAULT *fault = NULL; int ext, err; if (!TEST_ptr(cctx)) goto err; if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, 0, &qtserv, &cssl, &fault, NULL))) goto err; if (idx == 0) { ext = TLSEXT_TYPE_quic_transport_parameters; err = QUIC_ERR_CRYPTO_MISSING_EXT; } else { ext = TLSEXT_TYPE_application_layer_protocol_negotiation; err = QUIC_ERR_CRYPTO_NO_APP_PROTO; } if (!TEST_true(qtest_fault_set_hand_enc_ext_listener(fault, drop_extensions_cb, &ext))) goto err; /* * We expect the connection to fail because the server failed to provide * transport parameters */ if (!TEST_false(qtest_create_quic_connection(qtserv, cssl))) goto err; if (!TEST_true(qtest_check_server_transport_err(qtserv, err))) goto err; testresult = 1; err: qtest_fault_free(fault); SSL_free(cssl); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } /* * Test that corrupted packets/datagrams are dropped and retransmitted */ static int docorrupt = 0; static int on_packet_cipher_cb(QTEST_FAULT *fault, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len, void *cbarg) { if (!docorrupt || len == 0) return 1; buf[(size_t)test_random() % len] ^= 0xff; docorrupt = 0; return 1; } static int on_datagram_cb(QTEST_FAULT *fault, BIO_MSG *m, size_t stride, void *cbarg) { if (!docorrupt || m->data_len == 0) return 1; if (!qtest_fault_resize_datagram(fault, m->data_len - 1)) return 1; docorrupt = 0; return 1; } /* * Test 1: Corrupt by flipping bits in an encrypted packet * Test 2: Corrupt by truncating an entire datagram */ static int test_corrupted_data(int idx) { QTEST_FAULT *fault = NULL; int testresult = 0; SSL_CTX *cctx = SSL_CTX_new(OSSL_QUIC_client_method()); QUIC_TSERVER *qtserv = NULL; SSL *cssl = NULL; char *msg = "Hello World!"; size_t msglen = strlen(msg); unsigned char buf[80]; size_t bytesread, byteswritten; uint64_t sid = UINT64_MAX; if (!TEST_ptr(cctx)) goto err; if (!TEST_true(qtest_create_quic_objects(NULL, cctx, NULL, cert, privkey, QTEST_FLAG_FAKE_TIME, &qtserv, &cssl, &fault, NULL))) goto err; if (idx == 0) { /* Listen for encrypted packets being sent */ if (!TEST_true(qtest_fault_set_packet_cipher_listener(fault, on_packet_cipher_cb, NULL))) goto err; } else { /* Listen for datagrams being sent */ if (!TEST_true(qtest_fault_set_datagram_listener(fault, on_datagram_cb, NULL))) goto err; } if (!TEST_true(qtest_create_quic_connection(qtserv, cssl))) goto err; /* Corrupt the next server packet*/ docorrupt = 1; if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, /*is_uni=*/0, &sid)) || !TEST_uint64_t_eq(sid, 1)) goto err; /* * Send first 5 bytes of message. This will get corrupted and is treated as * "lost" */ if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, 5, &byteswritten))) goto err; if (!TEST_size_t_eq(byteswritten, 5)) goto err; /* * Introduce a small delay so that the above packet has time to be detected * as lost. Loss detection times are based on RTT which should be very * fast for us since there isn't really a network. The loss delay timer is * always at least 1ms though. We skip forward 100ms */ qtest_add_time(100); /* Send rest of message */ if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg + 5, msglen - 5, &byteswritten))) goto err; if (!TEST_size_t_eq(byteswritten, msglen - 5)) goto err; /* * Receive the corrupted packet. This should get dropped and is effectively * "lost". We also process the second packet which should be decrypted * successfully. Therefore we ack the frames in it */ if (!TEST_true(SSL_handle_events(cssl))) goto err; /* * Process the ack. Detect that the first part of the message must have * been lost due to the time elapsed since it was sent and resend it */ ossl_quic_tserver_tick(qtserv); /* Receive and process the newly arrived message data resend */ if (!TEST_true(SSL_handle_events(cssl))) goto err; /* The whole message should now have arrived */ if (!TEST_true(SSL_read_ex(cssl, buf, sizeof(buf), &bytesread))) goto err; if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; /* * If the test was successful then we corrupted exactly one packet and * docorrupt was reset */ if (!TEST_false(docorrupt)) goto err; testresult = 1; err: qtest_fault_free(fault); SSL_free(cssl); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } OPT_TEST_DECLARE_USAGE("certsdir\n") int setup_tests(void) { char *certsdir = NULL; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certsdir = test_get_argument(0))) return 0; cert = test_mk_file_path(certsdir, "servercert.pem"); if (cert == NULL) goto err; privkey = test_mk_file_path(certsdir, "serverkey.pem"); if (privkey == NULL) goto err; ADD_TEST(test_basic); ADD_TEST(test_unknown_frame); ADD_ALL_TESTS(test_drop_extensions, 2); ADD_ALL_TESTS(test_corrupted_data, 2); return 1; err: OPENSSL_free(cert); OPENSSL_free(privkey); return 0; } void cleanup_tests(void) { OPENSSL_free(cert); OPENSSL_free(privkey); }
./openssl/test/wpackettest.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/buffer.h> #include <openssl/rand.h> #include "internal/packet_quic.h" #include "testutil.h" static const unsigned char simple1[] = { 0xff }; static const unsigned char simple2[] = { 0x01, 0xff }; static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff }; static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff }; static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff }; static const unsigned char empty[] = { 0x00 }; static const unsigned char alloc[] = { 0x02, 0xfe, 0xff }; static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff }; static const unsigned char fixed[] = { 0xff, 0xff, 0xff }; static const unsigned char simpleder[] = { 0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd }; #ifndef OPENSSL_NO_QUIC /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */ static const unsigned char quic1[] = { 0x80, 0x00, 0x00, 0x01, 0x09 }; /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */ static const unsigned char quic2[] = { 0x01, 0x09 }; /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */ static const unsigned char quic3[] = { 0x40, 0x02, 0x40, 0x41 }; /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */ static const unsigned char quic4[] = { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x80, 0x01, 0x3c, 0x6a }; /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */ static const unsigned char quic5[] = { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xef, 0x77, 0x21, 0x3f, 0x3f, 0x50, 0x5b, 0xa5 }; /* QUIC sub-packet, length known up-front */ static const unsigned char quic6[] = { 0x03, 0x55, 0x66, 0x77 }; /* Nested and sequential sub-packets with length prefixes */ static const unsigned char quic7[] = { 0x07, 0x80, 0x00, 0x00, 0x08, 0x65, 0x14, 0x40, 0x01, 0x05, 0x40, 0x01, 0x11, 0x40, 0x01, 0x12, 0x40, 0x01, 0x13 }; #endif static BUF_MEM *buf; static int cleanup(WPACKET *pkt) { WPACKET_cleanup(pkt); return 0; } static int test_WPACKET_init(void) { WPACKET pkt; int i; size_t written; unsigned char sbuf[3]; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) /* Closing a top level WPACKET should fail */ || !TEST_false(WPACKET_close(&pkt)) /* Finishing a top level WPACKET should succeed */ || !TEST_true(WPACKET_finish(&pkt)) /* * Can't call close or finish on a WPACKET that's already * finished. */ || !TEST_false(WPACKET_close(&pkt)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); /* Now try with a one byte length prefix */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); /* And a longer length prefix */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 4)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))) return cleanup(&pkt); for (i = 1; i < 257; i++) { /* * Putting more bytes in than fit for the size of the length prefix * should fail */ if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256)) return cleanup(&pkt); } if (!TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); /* Test initialising from a fixed size buffer */ if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0)) /* Adding 3 bytes should succeed */ || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff)) /* Adding 1 more byte should fail */ || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) /* Finishing the top level WPACKET should succeed */ || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf)) /* Initialise with 1 len byte */ || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1)) /* Adding 2 bytes should succeed */ || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff)) /* Adding 1 more byte should fail */ || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc))) return cleanup(&pkt); return 1; } static int test_WPACKET_set_max_size(void) { WPACKET pkt; size_t written; if (!TEST_true(WPACKET_init(&pkt, buf)) /* * No previous lenbytes set so we should be ok to set the max * possible max size */ || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX)) /* We should be able to set it smaller too */ || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1)) /* And setting it bigger again should be ok */ || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX)) || !TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) /* * Should fail because we already consumed 1 byte with the * length */ || !TEST_false(WPACKET_set_max_size(&pkt, 0)) /* * Max size can't be bigger than biggest that will fit in * lenbytes */ || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101)) /* It can be the same as the maximum possible size */ || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100)) /* Or it can be less */ || !TEST_true(WPACKET_set_max_size(&pkt, 0x01)) /* Should fail because packet is already filled */ || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) /* You can't put in more bytes than max size */ || !TEST_true(WPACKET_set_max_size(&pkt, 0x02)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); return 1; } static int test_WPACKET_start_sub_packet(void) { WPACKET pkt; size_t written; size_t len; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) /* Can't finish because we have a sub packet */ || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) /* Sub packet is closed so can't close again */ || !TEST_false(WPACKET_close(&pkt)) /* Now a top level so finish should succeed */ || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); /* Single sub-packet with length prefix */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); /* Nested sub-packets with length prefixes */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 3) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))) return cleanup(&pkt); /* Sequential sub-packets with length prefixes */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub))) return cleanup(&pkt); /* Nested sub-packets with lengths filled before finish */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 3) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_fill_lengths(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)) || !TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); return 1; } static int test_WPACKET_set_flags(void) { WPACKET pkt; size_t written; /* Set packet to be non-zero length */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) /* Should fail because of zero length */ || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); /* Repeat above test in a sub-packet */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) /* Should fail because of zero length */ || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); /* Set packet to abandon non-zero length */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_size_t_eq(written, 0)) return cleanup(&pkt); /* Repeat above test but only abandon a sub-packet */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, empty, sizeof(empty))) return cleanup(&pkt); /* And repeat with a non empty sub-packet */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); return 1; } static int test_WPACKET_allocate_bytes(void) { WPACKET pkt; size_t written; unsigned char *bytes; if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes))) return cleanup(&pkt); bytes[0] = 0xfe; bytes[1] = 0xff; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc))) return cleanup(&pkt); /* Repeat with WPACKET_sub_allocate_bytes */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes))) return cleanup(&pkt); bytes[0] = 0xfe; bytes[1] = 0xff; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, submem, sizeof(submem))) return cleanup(&pkt); return 1; } static int test_WPACKET_memcpy(void) { WPACKET pkt; size_t written; const unsigned char bytes[] = { 0xfe, 0xff }; if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes))) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc))) return cleanup(&pkt); /* Repeat with WPACKET_sub_memcpy() */ if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, submem, sizeof(submem))) return cleanup(&pkt); return 1; } static int test_WPACKET_init_der(void) { WPACKET pkt; unsigned char sbuf[1024]; unsigned char testdata[] = { 0x00, 0x01, 0x02, 0x03 }; unsigned char testdata2[259] = { 0x82, 0x01, 0x00 }; size_t written[2]; size_t size1, size2; int flags = WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH; int i; /* Test initialising for writing DER */ if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf))) || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xfffefd)) /* Test writing data in a length prefixed sub-packet */ || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_memcpy(&pkt, testdata, sizeof(testdata))) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xfc)) /* this sub-packet is empty, and should render zero bytes */ || (!TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, flags)) || !TEST_true(WPACKET_get_total_written(&pkt, &size1)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &size2)) || !TEST_size_t_eq(size1, size2)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written[0])) || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[0], simpleder, sizeof(simpleder))) return cleanup(&pkt); /* Generate random packet data for test */ if (!TEST_int_gt(RAND_bytes(&testdata2[3], sizeof(testdata2) - 3), 0)) return 0; /* * Test with a sub-packet that has 2 length bytes. We do 2 passes - first * with a NULL buffer, just to calculate lengths, and a second pass with a * real buffer to actually generate a packet */ for (i = 0; i < 2; i++) { if (i == 0) { if (!TEST_true(WPACKET_init_null_der(&pkt))) return 0; } else { if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf)))) return 0; } if (!TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_memcpy(&pkt, &testdata2[3], sizeof(testdata2) - 3)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written[i]))) return cleanup(&pkt); } /* * Check that the size calculated in the first pass equals the size of the * packet actually generated in the second pass. Also check the generated * packet looks as we expect it to. */ if (!TEST_size_t_eq(written[0], written[1]) || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[1], testdata2, sizeof(testdata2))) return 0; return 1; } #ifndef OPENSSL_NO_QUIC static int test_WPACKET_quic(void) { WPACKET pkt; size_t written, len; unsigned char *bytes; /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet(&pkt)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09)) /* Can't finish because we have a sub packet */ || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) /* Sub packet is closed so can't close again */ || !TEST_false(WPACKET_close(&pkt)) /* Now a top level so finish should succeed */ || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic1, sizeof(quic1))) return cleanup(&pkt); /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_1B_MAX)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic2, sizeof(quic2))) return cleanup(&pkt); /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x41)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic3, sizeof(quic3))) return cleanup(&pkt); /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13c6a)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic4, sizeof(quic4))) return cleanup(&pkt); /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2f77213f3f505ba5ULL)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic5, sizeof(quic5))) return cleanup(&pkt); /* QUIC sub-packet, length known up-front */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_sub_allocate_bytes(&pkt, 3, &bytes))) return cleanup(&pkt); bytes[0] = 0x55; bytes[1] = 0x66; bytes[2] = 0x77; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic6, sizeof(quic6))) return cleanup(&pkt); /* Nested and sequential sub-packets with length prefixes */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x07)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_4B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2514)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 2) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x05)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x11)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 8) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x12)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic7, sizeof(quic7))) return cleanup(&pkt); /* Trying to encode a value above OSSL_QUIC_VLINT_MAX should fail */ if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_false(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX+1)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX))) return cleanup(&pkt); WPACKET_cleanup(&pkt); return 1; } static int test_WPACKET_quic_vlint_random(void) { size_t i, written; uint64_t expected, actual = 0; unsigned char rand_data[9]; WPACKET pkt; PACKET read_pkt = {0}; for (i = 0; i < 10000; ++i) { if (!TEST_int_gt(RAND_bytes(rand_data, sizeof(rand_data)), 0)) return cleanup(&pkt); expected = *(uint64_t*)rand_data; /* * Ensure that all size classes get tested with equal probability. */ switch (rand_data[8] & 3) { case 0: expected &= OSSL_QUIC_VLINT_1B_MAX; break; case 1: expected &= OSSL_QUIC_VLINT_2B_MAX; break; case 2: expected &= OSSL_QUIC_VLINT_4B_MAX; break; case 3: expected &= OSSL_QUIC_VLINT_8B_MAX; break; } if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, expected)) || !TEST_true(WPACKET_get_total_written(&pkt, &written))) return cleanup(&pkt); if (!TEST_true(PACKET_buf_init(&read_pkt, (unsigned char *)buf->data, written)) || !TEST_true(PACKET_get_quic_vlint(&read_pkt, &actual)) || !TEST_uint64_t_eq(expected, actual)) return cleanup(&pkt); WPACKET_cleanup(&pkt); } WPACKET_cleanup(&pkt); return 1; } #endif int setup_tests(void) { if (!TEST_ptr(buf = BUF_MEM_new())) return 0; ADD_TEST(test_WPACKET_init); ADD_TEST(test_WPACKET_set_max_size); ADD_TEST(test_WPACKET_start_sub_packet); ADD_TEST(test_WPACKET_set_flags); ADD_TEST(test_WPACKET_allocate_bytes); ADD_TEST(test_WPACKET_memcpy); ADD_TEST(test_WPACKET_init_der); #ifndef OPENSSL_NO_QUIC ADD_TEST(test_WPACKET_quic); ADD_TEST(test_WPACKET_quic_vlint_random); #endif return 1; } void cleanup_tests(void) { BUF_MEM_free(buf); }
./openssl/test/pbetest.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include "testutil.h" #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/rc4.h> #include <openssl/md5.h> #include <openssl/configuration.h> #include <openssl/provider.h> #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \ || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1 static const char pbe_password[] = "MyVoiceIsMyPassport"; static unsigned char pbe_salt[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, }; static const int pbe_iter = 1000; static unsigned char pbe_plaintext[] = { 0x57, 0x65, 0x20, 0x61, 0x72, 0x65, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x72, 0x73, }; #endif /* Expected output generated using OpenSSL 1.1.1 */ #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 static const unsigned char pbe_ciphertext_rc4_md5[] = { 0x21, 0x90, 0xfa, 0xee, 0x95, 0x66, 0x59, 0x45, 0xfa, 0x1e, 0x9f, 0xe2, 0x25, 0xd2, 0xf9, 0x71, 0x94, 0xe4, 0x3d, 0xc9, 0x7c, 0xb0, 0x07, 0x23, }; #endif #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1 static const unsigned char pbe_ciphertext_des_sha1[] = { 0xce, 0x4b, 0xb0, 0x0a, 0x7b, 0x48, 0xd7, 0xe3, 0x9a, 0x9f, 0x46, 0xd6, 0x41, 0x42, 0x4b, 0x44, 0x36, 0x45, 0x5f, 0x60, 0x8f, 0x3c, 0xd0, 0x55, 0xd0, 0x8d, 0xa9, 0xab, 0x78, 0x5b, 0x63, 0xaf, }; #endif #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \ || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1 static int test_pkcs5_pbe(const EVP_CIPHER *cipher, const EVP_MD *md, const unsigned char *exp, const int exp_len) { int ret = 0; EVP_CIPHER_CTX *ctx; X509_ALGOR *algor = NULL; int i, outlen; unsigned char out[32]; ctx = EVP_CIPHER_CTX_new(); if (!TEST_ptr(ctx)) goto err; algor = X509_ALGOR_new(); if (!TEST_ptr(algor)) goto err; if (!TEST_true(PKCS5_pbe_set0_algor(algor, EVP_CIPHER_nid(cipher), pbe_iter, pbe_salt, sizeof(pbe_salt))) || !TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password), algor->parameter, cipher, md, 1)) || !TEST_true(EVP_CipherUpdate(ctx, out, &i, pbe_plaintext, sizeof(pbe_plaintext)))) goto err; outlen = i; if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i))) goto err; outlen += i; if (!TEST_mem_eq(out, outlen, exp, exp_len)) goto err; /* Decrypt */ if (!TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password), algor->parameter, cipher, md, 0)) || !TEST_true(EVP_CipherUpdate(ctx, out, &i, exp, exp_len))) goto err; outlen = i; if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i))) goto err; if (!TEST_mem_eq(out, outlen, pbe_plaintext, sizeof(pbe_plaintext))) goto err; ret = 1; err: EVP_CIPHER_CTX_free(ctx); X509_ALGOR_free(algor); return ret; } #endif #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 static int test_pkcs5_pbe_rc4_md5(void) { return test_pkcs5_pbe(EVP_rc4(), EVP_md5(), pbe_ciphertext_rc4_md5, sizeof(pbe_ciphertext_rc4_md5)); } #endif #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1 static int test_pkcs5_pbe_des_sha1(void) { return test_pkcs5_pbe(EVP_des_cbc(), EVP_sha1(), pbe_ciphertext_des_sha1, sizeof(pbe_ciphertext_des_sha1)); } #endif #ifdef OPENSSL_NO_AUTOLOAD_CONFIG /* * For configurations where we are not autoloading configuration, we need * to access the legacy provider. The easiest way is to load both the * legacy and default providers directly and unload them on termination. */ static OSSL_PROVIDER *legacy, *dflt; #endif int setup_tests(void) { #ifdef OPENSSL_NO_AUTOLOAD_CONFIG /* Load required providers if not done via configuration */ legacy = OSSL_PROVIDER_load(NULL, "legacy"); dflt = OSSL_PROVIDER_load(NULL, "default"); if (!TEST_ptr(legacy) || !TEST_ptr(dflt)) { cleanup_tests(); return -1; } #endif #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 ADD_TEST(test_pkcs5_pbe_rc4_md5); #endif #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1 ADD_TEST(test_pkcs5_pbe_des_sha1); #endif return 1; } #ifdef OPENSSL_NO_AUTOLOAD_CONFIG void cleanup_tests(void) { /* Dispose of providers */ OSSL_PROVIDER_unload(legacy); OSSL_PROVIDER_unload(dflt); legacy = dflt = NULL; } #endif
./openssl/test/hpke_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/evp.h> #include <openssl/core_names.h> #include <openssl/rand.h> #include <openssl/hpke.h> #include "testutil.h" /* a size to use for stack buffers */ #define OSSL_HPKE_TSTSIZE 512 static OSSL_LIB_CTX *testctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *deflprov = NULL; static char *testpropq = "provider=default"; static int verbose = 0; typedef struct { int mode; OSSL_HPKE_SUITE suite; const unsigned char *ikmE; size_t ikmElen; const unsigned char *expected_pkEm; size_t expected_pkEmlen; const unsigned char *ikmR; size_t ikmRlen; const unsigned char *expected_pkRm; size_t expected_pkRmlen; const unsigned char *expected_skRm; size_t expected_skRmlen; const unsigned char *expected_secret; size_t expected_secretlen; const unsigned char *ksinfo; size_t ksinfolen; const unsigned char *ikmAuth; size_t ikmAuthlen; const unsigned char *psk; size_t psklen; const char *pskid; /* want terminating NUL here */ } TEST_BASEDATA; typedef struct { int seq; const unsigned char *pt; size_t ptlen; const unsigned char *aad; size_t aadlen; const unsigned char *expected_ct; size_t expected_ctlen; } TEST_AEADDATA; typedef struct { const unsigned char *context; size_t contextlen; const unsigned char *expected_secret; size_t expected_secretlen; } TEST_EXPORTDATA; /** * @brief Test that an EVP_PKEY encoded public key matches the supplied buffer * @param pkey is the EVP_PKEY we want to check * @param pub is the expected public key buffer * @param publen is the length of the above * @return 1 for good, 0 for bad */ static int cmpkey(const EVP_PKEY *pkey, const unsigned char *pub, size_t publen) { unsigned char pubbuf[256]; size_t pubbuflen = 0; int erv = 0; if (!TEST_true(publen <= sizeof(pubbuf))) return 0; erv = EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, pubbuf, sizeof(pubbuf), &pubbuflen); if (!TEST_true(erv)) return 0; if (pub != NULL && !TEST_mem_eq(pubbuf, pubbuflen, pub, publen)) return 0; return 1; } static int do_testhpke(const TEST_BASEDATA *base, const TEST_AEADDATA *aead, size_t aeadsz, const TEST_EXPORTDATA *export, size_t exportsz) { OSSL_LIB_CTX *libctx = testctx; const char *propq = testpropq; OSSL_HPKE_CTX *sealctx = NULL, *openctx = NULL; unsigned char ct[256]; unsigned char enc[256]; unsigned char ptout[256]; size_t ptoutlen = sizeof(ptout); size_t enclen = sizeof(enc); size_t ctlen = sizeof(ct); unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t publen = sizeof(pub); EVP_PKEY *privE = NULL; unsigned char authpub[OSSL_HPKE_TSTSIZE]; size_t authpublen = sizeof(authpub); EVP_PKEY *authpriv = NULL; unsigned char rpub[OSSL_HPKE_TSTSIZE]; size_t rpublen = sizeof(pub); EVP_PKEY *privR = NULL; int ret = 0; size_t i; uint64_t lastseq = 0; if (!TEST_true(OSSL_HPKE_keygen(base->suite, pub, &publen, &privE, base->ikmE, base->ikmElen, libctx, propq))) goto end; if (!TEST_true(cmpkey(privE, base->expected_pkEm, base->expected_pkEmlen))) goto end; if (!TEST_ptr(sealctx = OSSL_HPKE_CTX_new(base->mode, base->suite, OSSL_HPKE_ROLE_SENDER, libctx, propq))) goto end; if (!TEST_true(OSSL_HPKE_CTX_set1_ikme(sealctx, base->ikmE, base->ikmElen))) goto end; if (base->mode == OSSL_HPKE_MODE_AUTH || base->mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(base->ikmAuth != NULL && base->ikmAuthlen > 0)) goto end; if (!TEST_true(OSSL_HPKE_keygen(base->suite, authpub, &authpublen, &authpriv, base->ikmAuth, base->ikmAuthlen, libctx, propq))) goto end; if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(sealctx, authpriv))) goto end; } if (!TEST_true(OSSL_HPKE_keygen(base->suite, rpub, &rpublen, &privR, base->ikmR, base->ikmRlen, libctx, propq))) goto end; if (!TEST_true(cmpkey(privR, base->expected_pkRm, base->expected_pkRmlen))) goto end; if (base->mode == OSSL_HPKE_MODE_PSK || base->mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(OSSL_HPKE_CTX_set1_psk(sealctx, base->pskid, base->psk, base->psklen))) goto end; } if (!TEST_true(OSSL_HPKE_encap(sealctx, enc, &enclen, rpub, rpublen, base->ksinfo, base->ksinfolen))) goto end; if (!TEST_true(cmpkey(privE, enc, enclen))) goto end; for (i = 0; i < aeadsz; ++i) { ctlen = sizeof(ct); memset(ct, 0, ctlen); if (!TEST_true(OSSL_HPKE_seal(sealctx, ct, &ctlen, aead[i].aad, aead[i].aadlen, aead[i].pt, aead[i].ptlen))) goto end; if (!TEST_mem_eq(ct, ctlen, aead[i].expected_ct, aead[i].expected_ctlen)) goto end; if (!TEST_true(OSSL_HPKE_CTX_get_seq(sealctx, &lastseq))) goto end; if (lastseq != (uint64_t)(i + 1)) goto end; } if (!TEST_ptr(openctx = OSSL_HPKE_CTX_new(base->mode, base->suite, OSSL_HPKE_ROLE_RECEIVER, libctx, propq))) goto end; if (base->mode == OSSL_HPKE_MODE_PSK || base->mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(base->pskid != NULL && base->psk != NULL && base->psklen > 0)) goto end; if (!TEST_true(OSSL_HPKE_CTX_set1_psk(openctx, base->pskid, base->psk, base->psklen))) goto end; } if (base->mode == OSSL_HPKE_MODE_AUTH || base->mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(openctx, authpub, authpublen))) goto end; } if (!TEST_true(OSSL_HPKE_decap(openctx, enc, enclen, privR, base->ksinfo, base->ksinfolen))) goto end; for (i = 0; i < aeadsz; ++i) { ptoutlen = sizeof(ptout); memset(ptout, 0, ptoutlen); if (!TEST_true(OSSL_HPKE_open(openctx, ptout, &ptoutlen, aead[i].aad, aead[i].aadlen, aead[i].expected_ct, aead[i].expected_ctlen))) goto end; if (!TEST_mem_eq(aead[i].pt, aead[i].ptlen, ptout, ptoutlen)) goto end; /* check the sequence is being incremented as expected */ if (!TEST_true(OSSL_HPKE_CTX_get_seq(openctx, &lastseq))) goto end; if (lastseq != (uint64_t)(i + 1)) goto end; } /* check exporters */ for (i = 0; i < exportsz; ++i) { size_t len = export[i].expected_secretlen; unsigned char eval[OSSL_HPKE_TSTSIZE]; if (len > sizeof(eval)) goto end; /* export with too long label should fail */ if (!TEST_false(OSSL_HPKE_export(sealctx, eval, len, export[i].context, -1))) goto end; /* good export call */ if (!TEST_true(OSSL_HPKE_export(sealctx, eval, len, export[i].context, export[i].contextlen))) goto end; if (!TEST_mem_eq(eval, len, export[i].expected_secret, export[i].expected_secretlen)) goto end; /* check seal fails if export only mode */ if (aeadsz == 0) { if (!TEST_false(OSSL_HPKE_seal(sealctx, ct, &ctlen, NULL, 0, ptout, ptoutlen))) goto end; } } ret = 1; end: OSSL_HPKE_CTX_free(sealctx); OSSL_HPKE_CTX_free(openctx); EVP_PKEY_free(privE); EVP_PKEY_free(privR); EVP_PKEY_free(authpriv); return ret; } static const unsigned char pt[] = { 0x42, 0x65, 0x61, 0x75, 0x74, 0x79, 0x20, 0x69, 0x73, 0x20, 0x74, 0x72, 0x75, 0x74, 0x68, 0x2c, 0x20, 0x74, 0x72, 0x75, 0x74, 0x68, 0x20, 0x62, 0x65, 0x61, 0x75, 0x74, 0x79 }; static const unsigned char ksinfo[] = { 0x4f, 0x64, 0x65, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x20, 0x47, 0x72, 0x65, 0x63, 0x69, 0x61, 0x6e, 0x20, 0x55, 0x72, 0x6e }; #ifndef OPENSSL_NO_ECX /* * static const char *pskid = "Ennyn Durin aran Moria"; */ static const unsigned char pskid[] = { 0x45, 0x6e, 0x6e, 0x79, 0x6e, 0x20, 0x44, 0x75, 0x72, 0x69, 0x6e, 0x20, 0x61, 0x72, 0x61, 0x6e, 0x20, 0x4d, 0x6f, 0x72, 0x69, 0x61, 0x00 }; static const unsigned char psk[] = { 0x02, 0x47, 0xfd, 0x33, 0xb9, 0x13, 0x76, 0x0f, 0xa1, 0xfa, 0x51, 0xe1, 0x89, 0x2d, 0x9f, 0x30, 0x7f, 0xbe, 0x65, 0xeb, 0x17, 0x1e, 0x81, 0x32, 0xc2, 0xaf, 0x18, 0x55, 0x5a, 0x73, 0x8b, 0x82 }; /* these need to be "outside" the function below to keep check-ansi CI happy */ static const unsigned char first_ikme[] = { 0x78, 0x62, 0x8c, 0x35, 0x4e, 0x46, 0xf3, 0xe1, 0x69, 0xbd, 0x23, 0x1b, 0xe7, 0xb2, 0xff, 0x1c, 0x77, 0xaa, 0x30, 0x24, 0x60, 0xa2, 0x6d, 0xbf, 0xa1, 0x55, 0x15, 0x68, 0x4c, 0x00, 0x13, 0x0b }; static const unsigned char first_ikmr[] = { 0xd4, 0xa0, 0x9d, 0x09, 0xf5, 0x75, 0xfe, 0xf4, 0x25, 0x90, 0x5d, 0x2a, 0xb3, 0x96, 0xc1, 0x44, 0x91, 0x41, 0x46, 0x3f, 0x69, 0x8f, 0x8e, 0xfd, 0xb7, 0xac, 0xcf, 0xaf, 0xf8, 0x99, 0x50, 0x98 }; static const unsigned char first_ikmepub[] = { 0x0a, 0xd0, 0x95, 0x0d, 0x9f, 0xb9, 0x58, 0x8e, 0x59, 0x69, 0x0b, 0x74, 0xf1, 0x23, 0x7e, 0xcd, 0xf1, 0xd7, 0x75, 0xcd, 0x60, 0xbe, 0x2e, 0xca, 0x57, 0xaf, 0x5a, 0x4b, 0x04, 0x71, 0xc9, 0x1b, }; static const unsigned char first_ikmrpub[] = { 0x9f, 0xed, 0x7e, 0x8c, 0x17, 0x38, 0x75, 0x60, 0xe9, 0x2c, 0xc6, 0x46, 0x2a, 0x68, 0x04, 0x96, 0x57, 0x24, 0x6a, 0x09, 0xbf, 0xa8, 0xad, 0xe7, 0xae, 0xfe, 0x58, 0x96, 0x72, 0x01, 0x63, 0x66 }; static const unsigned char first_ikmrpriv[] = { 0xc5, 0xeb, 0x01, 0xeb, 0x45, 0x7f, 0xe6, 0xc6, 0xf5, 0x75, 0x77, 0xc5, 0x41, 0x3b, 0x93, 0x15, 0x50, 0xa1, 0x62, 0xc7, 0x1a, 0x03, 0xac, 0x8d, 0x19, 0x6b, 0xab, 0xbd, 0x4e, 0x5c, 0xe0, 0xfd }; static const unsigned char first_expected_shared_secret[] = { 0x72, 0x76, 0x99, 0xf0, 0x09, 0xff, 0xe3, 0xc0, 0x76, 0x31, 0x50, 0x19, 0xc6, 0x96, 0x48, 0x36, 0x6b, 0x69, 0x17, 0x14, 0x39, 0xbd, 0x7d, 0xd0, 0x80, 0x77, 0x43, 0xbd, 0xe7, 0x69, 0x86, 0xcd }; static const unsigned char first_aad0[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30 }; static const unsigned char first_ct0[] = { 0xe5, 0x2c, 0x6f, 0xed, 0x7f, 0x75, 0x8d, 0x0c, 0xf7, 0x14, 0x56, 0x89, 0xf2, 0x1b, 0xc1, 0xbe, 0x6e, 0xc9, 0xea, 0x09, 0x7f, 0xef, 0x4e, 0x95, 0x94, 0x40, 0x01, 0x2f, 0x4f, 0xeb, 0x73, 0xfb, 0x61, 0x1b, 0x94, 0x61, 0x99, 0xe6, 0x81, 0xf4, 0xcf, 0xc3, 0x4d, 0xb8, 0xea }; static const unsigned char first_aad1[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31 }; static const unsigned char first_ct1[] = { 0x49, 0xf3, 0xb1, 0x9b, 0x28, 0xa9, 0xea, 0x9f, 0x43, 0xe8, 0xc7, 0x12, 0x04, 0xc0, 0x0d, 0x4a, 0x49, 0x0e, 0xe7, 0xf6, 0x13, 0x87, 0xb6, 0x71, 0x9d, 0xb7, 0x65, 0xe9, 0x48, 0x12, 0x3b, 0x45, 0xb6, 0x16, 0x33, 0xef, 0x05, 0x9b, 0xa2, 0x2c, 0xd6, 0x24, 0x37, 0xc8, 0xba }; static const unsigned char first_aad2[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x32 }; static const unsigned char first_ct2[] = { 0x25, 0x7c, 0xa6, 0xa0, 0x84, 0x73, 0xdc, 0x85, 0x1f, 0xde, 0x45, 0xaf, 0xd5, 0x98, 0xcc, 0x83, 0xe3, 0x26, 0xdd, 0xd0, 0xab, 0xe1, 0xef, 0x23, 0xba, 0xa3, 0xba, 0xa4, 0xdd, 0x8c, 0xde, 0x99, 0xfc, 0xe2, 0xc1, 0xe8, 0xce, 0x68, 0x7b, 0x0b, 0x47, 0xea, 0xd1, 0xad, 0xc9 }; static const unsigned char first_export1[] = { 0xdf, 0xf1, 0x7a, 0xf3, 0x54, 0xc8, 0xb4, 0x16, 0x73, 0x56, 0x7d, 0xb6, 0x25, 0x9f, 0xd6, 0x02, 0x99, 0x67, 0xb4, 0xe1, 0xaa, 0xd1, 0x30, 0x23, 0xc2, 0xae, 0x5d, 0xf8, 0xf4, 0xf4, 0x3b, 0xf6 }; static const unsigned char first_context2[] = { 0x00 }; static const unsigned char first_export2[] = { 0x6a, 0x84, 0x72, 0x61, 0xd8, 0x20, 0x7f, 0xe5, 0x96, 0xbe, 0xfb, 0x52, 0x92, 0x84, 0x63, 0x88, 0x1a, 0xb4, 0x93, 0xda, 0x34, 0x5b, 0x10, 0xe1, 0xdc, 0xc6, 0x45, 0xe3, 0xb9, 0x4e, 0x2d, 0x95 }; static const unsigned char first_context3[] = { 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74 }; static const unsigned char first_export3[] = { 0x8a, 0xff, 0x52, 0xb4, 0x5a, 0x1b, 0xe3, 0xa7, 0x34, 0xbc, 0x7a, 0x41, 0xe2, 0x0b, 0x4e, 0x05, 0x5a, 0xd4, 0xc4, 0xd2, 0x21, 0x04, 0xb0, 0xc2, 0x02, 0x85, 0xa7, 0xc4, 0x30, 0x24, 0x01, 0xcd }; static int x25519kdfsha256_hkdfsha256_aes128gcm_psk_test(void) { const TEST_BASEDATA pskdata = { /* "X25519", NULL, "SHA256", "SHA256", "AES-128-GCM", */ OSSL_HPKE_MODE_PSK, { OSSL_HPKE_KEM_ID_X25519, OSSL_HPKE_KDF_ID_HKDF_SHA256, OSSL_HPKE_AEAD_ID_AES_GCM_128 }, first_ikme, sizeof(first_ikme), first_ikmepub, sizeof(first_ikmepub), first_ikmr, sizeof(first_ikmr), first_ikmrpub, sizeof(first_ikmrpub), first_ikmrpriv, sizeof(first_ikmrpriv), first_expected_shared_secret, sizeof(first_expected_shared_secret), ksinfo, sizeof(ksinfo), NULL, 0, /* No Auth */ psk, sizeof(psk), (char *) pskid }; const TEST_AEADDATA aeaddata[] = { { 0, pt, sizeof(pt), first_aad0, sizeof(first_aad0), first_ct0, sizeof(first_ct0) }, { 1, pt, sizeof(pt), first_aad1, sizeof(first_aad1), first_ct1, sizeof(first_ct1) }, { 2, pt, sizeof(pt), first_aad2, sizeof(first_aad2), first_ct2, sizeof(first_ct2) } }; const TEST_EXPORTDATA exportdata[] = { { NULL, 0, first_export1, sizeof(first_export1) }, { first_context2, sizeof(first_context2), first_export2, sizeof(first_export2) }, { first_context3, sizeof(first_context3), first_export3, sizeof(first_export3) }, }; return do_testhpke(&pskdata, aeaddata, OSSL_NELEM(aeaddata), exportdata, OSSL_NELEM(exportdata)); } static const unsigned char second_ikme[] = { 0x72, 0x68, 0x60, 0x0d, 0x40, 0x3f, 0xce, 0x43, 0x15, 0x61, 0xae, 0xf5, 0x83, 0xee, 0x16, 0x13, 0x52, 0x7c, 0xff, 0x65, 0x5c, 0x13, 0x43, 0xf2, 0x98, 0x12, 0xe6, 0x67, 0x06, 0xdf, 0x32, 0x34 }; static const unsigned char second_ikmepub[] = { 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28, 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d, 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44, 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31, }; static const unsigned char second_ikmr[] = { 0x6d, 0xb9, 0xdf, 0x30, 0xaa, 0x07, 0xdd, 0x42, 0xee, 0x5e, 0x81, 0x81, 0xaf, 0xdb, 0x97, 0x7e, 0x53, 0x8f, 0x5e, 0x1f, 0xec, 0x8a, 0x06, 0x22, 0x3f, 0x33, 0xf7, 0x01, 0x3e, 0x52, 0x50, 0x37 }; static const unsigned char second_ikmrpub[] = { 0x39, 0x48, 0xcf, 0xe0, 0xad, 0x1d, 0xdb, 0x69, 0x5d, 0x78, 0x0e, 0x59, 0x07, 0x71, 0x95, 0xda, 0x6c, 0x56, 0x50, 0x6b, 0x02, 0x73, 0x29, 0x79, 0x4a, 0xb0, 0x2b, 0xca, 0x80, 0x81, 0x5c, 0x4d }; static const unsigned char second_ikmrpriv[] = { 0x46, 0x12, 0xc5, 0x50, 0x26, 0x3f, 0xc8, 0xad, 0x58, 0x37, 0x5d, 0xf3, 0xf5, 0x57, 0xaa, 0xc5, 0x31, 0xd2, 0x68, 0x50, 0x90, 0x3e, 0x55, 0xa9, 0xf2, 0x3f, 0x21, 0xd8, 0x53, 0x4e, 0x8a, 0xc8 }; static const unsigned char second_expected_shared_secret[] = { 0xfe, 0x0e, 0x18, 0xc9, 0xf0, 0x24, 0xce, 0x43, 0x79, 0x9a, 0xe3, 0x93, 0xc7, 0xe8, 0xfe, 0x8f, 0xce, 0x9d, 0x21, 0x88, 0x75, 0xe8, 0x22, 0x7b, 0x01, 0x87, 0xc0, 0x4e, 0x7d, 0x2e, 0xa1, 0xfc }; static const unsigned char second_aead0[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30 }; static const unsigned char second_ct0[] = { 0xf9, 0x38, 0x55, 0x8b, 0x5d, 0x72, 0xf1, 0xa2, 0x38, 0x10, 0xb4, 0xbe, 0x2a, 0xb4, 0xf8, 0x43, 0x31, 0xac, 0xc0, 0x2f, 0xc9, 0x7b, 0xab, 0xc5, 0x3a, 0x52, 0xae, 0x82, 0x18, 0xa3, 0x55, 0xa9, 0x6d, 0x87, 0x70, 0xac, 0x83, 0xd0, 0x7b, 0xea, 0x87, 0xe1, 0x3c, 0x51, 0x2a }; static const unsigned char second_aead1[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31 }; static const unsigned char second_ct1[] = { 0xaf, 0x2d, 0x7e, 0x9a, 0xc9, 0xae, 0x7e, 0x27, 0x0f, 0x46, 0xba, 0x1f, 0x97, 0x5b, 0xe5, 0x3c, 0x09, 0xf8, 0xd8, 0x75, 0xbd, 0xc8, 0x53, 0x54, 0x58, 0xc2, 0x49, 0x4e, 0x8a, 0x6e, 0xab, 0x25, 0x1c, 0x03, 0xd0, 0xc2, 0x2a, 0x56, 0xb8, 0xca, 0x42, 0xc2, 0x06, 0x3b, 0x84 }; static const unsigned char second_export1[] = { 0x38, 0x53, 0xfe, 0x2b, 0x40, 0x35, 0x19, 0x5a, 0x57, 0x3f, 0xfc, 0x53, 0x85, 0x6e, 0x77, 0x05, 0x8e, 0x15, 0xd9, 0xea, 0x06, 0x4d, 0xe3, 0xe5, 0x9f, 0x49, 0x61, 0xd0, 0x09, 0x52, 0x50, 0xee }; static const unsigned char second_context2[] = { 0x00 }; static const unsigned char second_export2[] = { 0x2e, 0x8f, 0x0b, 0x54, 0x67, 0x3c, 0x70, 0x29, 0x64, 0x9d, 0x4e, 0xb9, 0xd5, 0xe3, 0x3b, 0xf1, 0x87, 0x2c, 0xf7, 0x6d, 0x62, 0x3f, 0xf1, 0x64, 0xac, 0x18, 0x5d, 0xa9, 0xe8, 0x8c, 0x21, 0xa5 }; static const unsigned char second_context3[] = { 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74 }; static const unsigned char second_export3[] = { 0xe9, 0xe4, 0x30, 0x65, 0x10, 0x2c, 0x38, 0x36, 0x40, 0x1b, 0xed, 0x8c, 0x3c, 0x3c, 0x75, 0xae, 0x46, 0xbe, 0x16, 0x39, 0x86, 0x93, 0x91, 0xd6, 0x2c, 0x61, 0xf1, 0xec, 0x7a, 0xf5, 0x49, 0x31 }; static int x25519kdfsha256_hkdfsha256_aes128gcm_base_test(void) { const TEST_BASEDATA basedata = { OSSL_HPKE_MODE_BASE, { OSSL_HPKE_KEM_ID_X25519, OSSL_HPKE_KDF_ID_HKDF_SHA256, OSSL_HPKE_AEAD_ID_AES_GCM_128 }, second_ikme, sizeof(second_ikme), second_ikmepub, sizeof(second_ikmepub), second_ikmr, sizeof(second_ikmr), second_ikmrpub, sizeof(second_ikmrpub), second_ikmrpriv, sizeof(second_ikmrpriv), second_expected_shared_secret, sizeof(second_expected_shared_secret), ksinfo, sizeof(ksinfo), NULL, 0, /* no auth ikm */ NULL, 0, NULL /* no psk */ }; const TEST_AEADDATA aeaddata[] = { { 0, pt, sizeof(pt), second_aead0, sizeof(second_aead0), second_ct0, sizeof(second_ct0) }, { 1, pt, sizeof(pt), second_aead1, sizeof(second_aead1), second_ct1, sizeof(second_ct1) } }; const TEST_EXPORTDATA exportdata[] = { { NULL, 0, second_export1, sizeof(second_export1) }, { second_context2, sizeof(second_context2), second_export2, sizeof(second_export2) }, { second_context3, sizeof(second_context3), second_export3, sizeof(second_export3) }, }; return do_testhpke(&basedata, aeaddata, OSSL_NELEM(aeaddata), exportdata, OSSL_NELEM(exportdata)); } #endif static const unsigned char third_ikme[] = { 0x42, 0x70, 0xe5, 0x4f, 0xfd, 0x08, 0xd7, 0x9d, 0x59, 0x28, 0x02, 0x0a, 0xf4, 0x68, 0x6d, 0x8f, 0x6b, 0x7d, 0x35, 0xdb, 0xe4, 0x70, 0x26, 0x5f, 0x1f, 0x5a, 0xa2, 0x28, 0x16, 0xce, 0x86, 0x0e }; static const unsigned char third_ikmepub[] = { 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50, 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14, 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e, 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a, 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4, 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00, 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b, 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18, 0xc4, }; static const unsigned char third_ikmr[] = { 0x66, 0x8b, 0x37, 0x17, 0x1f, 0x10, 0x72, 0xf3, 0xcf, 0x12, 0xea, 0x8a, 0x23, 0x6a, 0x45, 0xdf, 0x23, 0xfc, 0x13, 0xb8, 0x2a, 0xf3, 0x60, 0x9a, 0xd1, 0xe3, 0x54, 0xf6, 0xef, 0x81, 0x75, 0x50 }; static const unsigned char third_ikmrpub[] = { 0x04, 0xfe, 0x8c, 0x19, 0xce, 0x09, 0x05, 0x19, 0x1e, 0xbc, 0x29, 0x8a, 0x92, 0x45, 0x79, 0x25, 0x31, 0xf2, 0x6f, 0x0c, 0xec, 0xe2, 0x46, 0x06, 0x39, 0xe8, 0xbc, 0x39, 0xcb, 0x7f, 0x70, 0x6a, 0x82, 0x6a, 0x77, 0x9b, 0x4c, 0xf9, 0x69, 0xb8, 0xa0, 0xe5, 0x39, 0xc7, 0xf6, 0x2f, 0xb3, 0xd3, 0x0a, 0xd6, 0xaa, 0x8f, 0x80, 0xe3, 0x0f, 0x1d, 0x12, 0x8a, 0xaf, 0xd6, 0x8a, 0x2c, 0xe7, 0x2e, 0xa0 }; static const unsigned char third_ikmrpriv[] = { 0xf3, 0xce, 0x7f, 0xda, 0xe5, 0x7e, 0x1a, 0x31, 0x0d, 0x87, 0xf1, 0xeb, 0xbd, 0xe6, 0xf3, 0x28, 0xbe, 0x0a, 0x99, 0xcd, 0xbc, 0xad, 0xf4, 0xd6, 0x58, 0x9c, 0xf2, 0x9d, 0xe4, 0xb8, 0xff, 0xd2 }; static const unsigned char third_expected_shared_secret[] = { 0xc0, 0xd2, 0x6a, 0xea, 0xb5, 0x36, 0x60, 0x9a, 0x57, 0x2b, 0x07, 0x69, 0x5d, 0x93, 0x3b, 0x58, 0x9d, 0xcf, 0x36, 0x3f, 0xf9, 0xd9, 0x3c, 0x93, 0xad, 0xea, 0x53, 0x7a, 0xea, 0xbb, 0x8c, 0xb8 }; static const unsigned char third_aead0[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x30 }; static const unsigned char third_ct0[] = { 0x5a, 0xd5, 0x90, 0xbb, 0x8b, 0xaa, 0x57, 0x7f, 0x86, 0x19, 0xdb, 0x35, 0xa3, 0x63, 0x11, 0x22, 0x6a, 0x89, 0x6e, 0x73, 0x42, 0xa6, 0xd8, 0x36, 0xd8, 0xb7, 0xbc, 0xd2, 0xf2, 0x0b, 0x6c, 0x7f, 0x90, 0x76, 0xac, 0x23, 0x2e, 0x3a, 0xb2, 0x52, 0x3f, 0x39, 0x51, 0x34, 0x34 }; static const unsigned char third_aead1[] = { 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x2d, 0x31 }; static const unsigned char third_ct1[] = { 0xfa, 0x6f, 0x03, 0x7b, 0x47, 0xfc, 0x21, 0x82, 0x6b, 0x61, 0x01, 0x72, 0xca, 0x96, 0x37, 0xe8, 0x2d, 0x6e, 0x58, 0x01, 0xeb, 0x31, 0xcb, 0xd3, 0x74, 0x82, 0x71, 0xaf, 0xfd, 0x4e, 0xcb, 0x06, 0x64, 0x6e, 0x03, 0x29, 0xcb, 0xdf, 0x3c, 0x3c, 0xd6, 0x55, 0xb2, 0x8e, 0x82 }; static const unsigned char third_export1[] = { 0x5e, 0x9b, 0xc3, 0xd2, 0x36, 0xe1, 0x91, 0x1d, 0x95, 0xe6, 0x5b, 0x57, 0x6a, 0x8a, 0x86, 0xd4, 0x78, 0xfb, 0x82, 0x7e, 0x8b, 0xdf, 0xe7, 0x7b, 0x74, 0x1b, 0x28, 0x98, 0x90, 0x49, 0x0d, 0x4d }; static const unsigned char third_context2[] = { 0x00 }; static const unsigned char third_export2[] = { 0x6c, 0xff, 0x87, 0x65, 0x89, 0x31, 0xbd, 0xa8, 0x3d, 0xc8, 0x57, 0xe6, 0x35, 0x3e, 0xfe, 0x49, 0x87, 0xa2, 0x01, 0xb8, 0x49, 0x65, 0x8d, 0x9b, 0x04, 0x7a, 0xab, 0x4c, 0xf2, 0x16, 0xe7, 0x96 }; static const unsigned char third_context3[] = { 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74 }; static const unsigned char third_export3[] = { 0xd8, 0xf1, 0xea, 0x79, 0x42, 0xad, 0xbb, 0xa7, 0x41, 0x2c, 0x6d, 0x43, 0x1c, 0x62, 0xd0, 0x13, 0x71, 0xea, 0x47, 0x6b, 0x82, 0x3e, 0xb6, 0x97, 0xe1, 0xf6, 0xe6, 0xca, 0xe1, 0xda, 0xb8, 0x5a }; static int P256kdfsha256_hkdfsha256_aes128gcm_base_test(void) { const TEST_BASEDATA basedata = { OSSL_HPKE_MODE_BASE, { OSSL_HPKE_KEM_ID_P256, OSSL_HPKE_KDF_ID_HKDF_SHA256, OSSL_HPKE_AEAD_ID_AES_GCM_128 }, third_ikme, sizeof(third_ikme), third_ikmepub, sizeof(third_ikmepub), third_ikmr, sizeof(third_ikmr), third_ikmrpub, sizeof(third_ikmrpub), third_ikmrpriv, sizeof(third_ikmrpriv), third_expected_shared_secret, sizeof(third_expected_shared_secret), ksinfo, sizeof(ksinfo), NULL, 0, /* no auth */ NULL, 0, NULL /* PSK stuff */ }; const TEST_AEADDATA aeaddata[] = { { 0, pt, sizeof(pt), third_aead0, sizeof(third_aead0), third_ct0, sizeof(third_ct0) }, { 1, pt, sizeof(pt), third_aead1, sizeof(third_aead1), third_ct1, sizeof(third_ct1) } }; const TEST_EXPORTDATA exportdata[] = { { NULL, 0, third_export1, sizeof(third_export1) }, { third_context2, sizeof(third_context2), third_export2, sizeof(third_export2) }, { third_context3, sizeof(third_context3), third_export3, sizeof(third_export3) }, }; return do_testhpke(&basedata, aeaddata, OSSL_NELEM(aeaddata), exportdata, OSSL_NELEM(exportdata)); } #ifndef OPENSSL_NO_ECX static const unsigned char fourth_ikme[] = { 0x55, 0xbc, 0x24, 0x5e, 0xe4, 0xef, 0xda, 0x25, 0xd3, 0x8f, 0x2d, 0x54, 0xd5, 0xbb, 0x66, 0x65, 0x29, 0x1b, 0x99, 0xf8, 0x10, 0x8a, 0x8c, 0x4b, 0x68, 0x6c, 0x2b, 0x14, 0x89, 0x3e, 0xa5, 0xd9 }; static const unsigned char fourth_ikmepub[] = { 0xe5, 0xe8, 0xf9, 0xbf, 0xff, 0x6c, 0x2f, 0x29, 0x79, 0x1f, 0xc3, 0x51, 0xd2, 0xc2, 0x5c, 0xe1, 0x29, 0x9a, 0xa5, 0xea, 0xca, 0x78, 0xa7, 0x57, 0xc0, 0xb4, 0xfb, 0x4b, 0xcd, 0x83, 0x09, 0x18 }; static const unsigned char fourth_ikmr[] = { 0x68, 0x3a, 0xe0, 0xda, 0x1d, 0x22, 0x18, 0x1e, 0x74, 0xed, 0x2e, 0x50, 0x3e, 0xbf, 0x82, 0x84, 0x0d, 0xeb, 0x1d, 0x5e, 0x87, 0x2c, 0xad, 0xe2, 0x0f, 0x4b, 0x45, 0x8d, 0x99, 0x78, 0x3e, 0x31 }; static const unsigned char fourth_ikmrpub[] = { 0x19, 0x41, 0x41, 0xca, 0x6c, 0x3c, 0x3b, 0xeb, 0x47, 0x92, 0xcd, 0x97, 0xba, 0x0e, 0xa1, 0xfa, 0xff, 0x09, 0xd9, 0x84, 0x35, 0x01, 0x23, 0x45, 0x76, 0x6e, 0xe3, 0x3a, 0xae, 0x2d, 0x76, 0x64 }; static const unsigned char fourth_ikmrpriv[] = { 0x33, 0xd1, 0x96, 0xc8, 0x30, 0xa1, 0x2f, 0x9a, 0xc6, 0x5d, 0x6e, 0x56, 0x5a, 0x59, 0x0d, 0x80, 0xf0, 0x4e, 0xe9, 0xb1, 0x9c, 0x83, 0xc8, 0x7f, 0x2c, 0x17, 0x0d, 0x97, 0x2a, 0x81, 0x28, 0x48 }; static const unsigned char fourth_expected_shared_secret[] = { 0xe8, 0x17, 0x16, 0xce, 0x8f, 0x73, 0x14, 0x1d, 0x4f, 0x25, 0xee, 0x90, 0x98, 0xef, 0xc9, 0x68, 0xc9, 0x1e, 0x5b, 0x8c, 0xe5, 0x2f, 0xff, 0xf5, 0x9d, 0x64, 0x03, 0x9e, 0x82, 0x91, 0x8b, 0x66 }; static const unsigned char fourth_export1[] = { 0x7a, 0x36, 0x22, 0x1b, 0xd5, 0x6d, 0x50, 0xfb, 0x51, 0xee, 0x65, 0xed, 0xfd, 0x98, 0xd0, 0x6a, 0x23, 0xc4, 0xdc, 0x87, 0x08, 0x5a, 0xa5, 0x86, 0x6c, 0xb7, 0x08, 0x72, 0x44, 0xbd, 0x2a, 0x36 }; static const unsigned char fourth_context2[] = { 0x00 }; static const unsigned char fourth_export2[] = { 0xd5, 0x53, 0x5b, 0x87, 0x09, 0x9c, 0x6c, 0x3c, 0xe8, 0x0d, 0xc1, 0x12, 0xa2, 0x67, 0x1c, 0x6e, 0xc8, 0xe8, 0x11, 0xa2, 0xf2, 0x84, 0xf9, 0x48, 0xce, 0xc6, 0xdd, 0x17, 0x08, 0xee, 0x33, 0xf0 }; static const unsigned char fourth_context3[] = { 0x54, 0x65, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74 }; static const unsigned char fourth_export3[] = { 0xff, 0xaa, 0xbc, 0x85, 0xa7, 0x76, 0x13, 0x6c, 0xa0, 0xc3, 0x78, 0xe5, 0xd0, 0x84, 0xc9, 0x14, 0x0a, 0xb5, 0x52, 0xb7, 0x8f, 0x03, 0x9d, 0x2e, 0x87, 0x75, 0xf2, 0x6e, 0xff, 0xf4, 0xc7, 0x0e }; static int export_only_test(void) { /* based on RFC9180 A.7 */ const TEST_BASEDATA basedata = { OSSL_HPKE_MODE_BASE, { OSSL_HPKE_KEM_ID_X25519, OSSL_HPKE_KDF_ID_HKDF_SHA256, OSSL_HPKE_AEAD_ID_EXPORTONLY }, fourth_ikme, sizeof(fourth_ikme), fourth_ikmepub, sizeof(fourth_ikmepub), fourth_ikmr, sizeof(fourth_ikmr), fourth_ikmrpub, sizeof(fourth_ikmrpub), fourth_ikmrpriv, sizeof(fourth_ikmrpriv), fourth_expected_shared_secret, sizeof(fourth_expected_shared_secret), ksinfo, sizeof(ksinfo), NULL, 0, /* no auth */ NULL, 0, NULL /* PSK stuff */ }; const TEST_EXPORTDATA exportdata[] = { { NULL, 0, fourth_export1, sizeof(fourth_export1) }, { fourth_context2, sizeof(fourth_context2), fourth_export2, sizeof(fourth_export2) }, { fourth_context3, sizeof(fourth_context3), fourth_export3, sizeof(fourth_export3) }, }; return do_testhpke(&basedata, NULL, 0, exportdata, OSSL_NELEM(exportdata)); } #endif /* * Randomly toss a coin */ #define COIN_IS_HEADS (test_random() % 2) /* tables of HPKE modes and suite values */ static int hpke_mode_list[] = { OSSL_HPKE_MODE_BASE, OSSL_HPKE_MODE_PSK, OSSL_HPKE_MODE_AUTH, OSSL_HPKE_MODE_PSKAUTH }; static uint16_t hpke_kem_list[] = { OSSL_HPKE_KEM_ID_P256, OSSL_HPKE_KEM_ID_P384, OSSL_HPKE_KEM_ID_P521, #ifndef OPENSSL_NO_ECX OSSL_HPKE_KEM_ID_X25519, OSSL_HPKE_KEM_ID_X448 #endif }; static uint16_t hpke_kdf_list[] = { OSSL_HPKE_KDF_ID_HKDF_SHA256, OSSL_HPKE_KDF_ID_HKDF_SHA384, OSSL_HPKE_KDF_ID_HKDF_SHA512 }; static uint16_t hpke_aead_list[] = { OSSL_HPKE_AEAD_ID_AES_GCM_128, OSSL_HPKE_AEAD_ID_AES_GCM_256, #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) OSSL_HPKE_AEAD_ID_CHACHA_POLY1305 #endif }; /* * Strings that can be used with names or IANA codepoints. * Note that the initial entries from these lists should * match the lists above, i.e. kem_str_list[0] and * hpke_kem_list[0] should refer to the same KEM. We use * that for verbose output via TEST_note() below. * Subsequent entries are only used for tests of * OSSL_HPKE_str2suite() */ static const char *mode_str_list[] = { "base", "psk", "auth", "pskauth" }; static const char *kem_str_list[] = { #ifndef OPENSSL_NO_ECX "P-256", "P-384", "P-521", "x25519", "x448", "0x10", "0x11", "0x12", "0x20", "0x21", "16", "17", "18", "32", "33" #else "P-256", "P-384", "P-521", "0x10", "0x11", "0x12", "16", "17", "18" #endif }; static const char *kdf_str_list[] = { "hkdf-sha256", "hkdf-sha384", "hkdf-sha512", "0x1", "0x01", "0x2", "0x02", "0x3", "0x03", "1", "2", "3" }; static const char *aead_str_list[] = { "aes-128-gcm", "aes-256-gcm", "chacha20-poly1305", "exporter", "0x1", "0x01", "0x2", "0x02", "0x3", "0x03", "1", "2", "3", "0xff", "255" }; /* table of bogus strings that better not work */ static const char *bogus_suite_strs[] = { "3,33,3", "bogus,bogus,bogus", "bogus,33,3,1,bogus", "bogus,33,3,1", "bogus,bogus", "bogus", /* one bad token */ "0x10,0x01,bogus", "0x10,bogus,0x01", "bogus,0x02,0x01", /* in reverse order */ "aes-256-gcm,hkdf-sha512,x25519", /* surplus separators */ ",,0x10,0x01,0x02", "0x10,,0x01,0x02", "0x10,0x01,,0x02", /* embedded NUL chars */ "0x10,\00x01,,0x02", "0x10,\0""0x01,0x02", "0x10\0,0x01,0x02", "0x10,0x01\0,0x02", "0x10,0x01,\0""0x02", /* embedded whitespace */ " aes-256-gcm,hkdf-sha512,x25519", "aes-256-gcm, hkdf-sha512,x25519", "aes-256-gcm ,hkdf-sha512,x25519", "aes-256-gcm,hkdf-sha512, x25519", "aes-256-gcm,hkdf-sha512 ,x25519", "aes-256-gcm,hkdf-sha512,x25519 ", /* good value followed by extra stuff */ "0x10,0x01,0x02,", "0x10,0x01,0x02,,,", "0x10,0x01,0x01,0x02", "0x10,0x01,0x01,blah", "0x10,0x01,0x01 0x02", /* too few but good tokens */ "0x10,0x01", "0x10", /* empty things */ NULL, "", ",", ",," }; /** * @brief round-trips, generating keys, encrypt and decrypt * * This iterates over all mode and ciphersuite options trying * a key gen, encrypt and decrypt for each. The aad, info, and * seq inputs are randomly set or omitted each time. EVP and * non-EVP key generation are randomly selected. * * @return 1 for success, other otherwise */ static int test_hpke_modes_suites(void) { int overallresult = 1; size_t mind = 0; /* index into hpke_mode_list */ size_t kemind = 0; /* index into hpke_kem_list */ size_t kdfind = 0; /* index into hpke_kdf_list */ size_t aeadind = 0; /* index into hpke_aead_list */ /* iterate over the different modes */ for (mind = 0; mind < OSSL_NELEM(hpke_mode_list); mind++) { int hpke_mode = hpke_mode_list[mind]; size_t aadlen = OSSL_HPKE_TSTSIZE; unsigned char aad[OSSL_HPKE_TSTSIZE]; unsigned char *aadp = NULL; size_t infolen = 32; unsigned char info[32]; unsigned char *infop = NULL; unsigned char lpsk[32]; unsigned char *pskp = NULL; char lpskid[32]; size_t psklen = 32; char *pskidp = NULL; EVP_PKEY *privp = NULL; OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; size_t plainlen = OSSL_HPKE_TSTSIZE; unsigned char plain[OSSL_HPKE_TSTSIZE]; OSSL_HPKE_CTX *rctx = NULL; OSSL_HPKE_CTX *ctx = NULL; memset(plain, 0x00, OSSL_HPKE_TSTSIZE); strcpy((char *)plain, "a message not in a bottle"); plainlen = strlen((char *)plain); /* * Randomly try with/without info, aad, seq. Given mode and suite * combos, and this being run even a few times, we'll exercise many * code paths fairly quickly. We don't really care what the values * are but it'll be easier to debug if they're known, so we set 'em. */ if (COIN_IS_HEADS) { aadp = aad; memset(aad, 'a', aadlen); } else { aadlen = 0; } if (COIN_IS_HEADS) { infop = info; memset(info, 'i', infolen); } else { infolen = 0; } if (hpke_mode == OSSL_HPKE_MODE_PSK || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { pskp = lpsk; memset(lpsk, 'P', psklen); pskidp = lpskid; memset(lpskid, 'I', psklen - 1); lpskid[psklen - 1] = '\0'; } else { psklen = 0; } for (kemind = 0; /* iterate over the kems, kdfs and aeads */ overallresult == 1 && kemind < OSSL_NELEM(hpke_kem_list); kemind++) { uint16_t kem_id = hpke_kem_list[kemind]; size_t authpublen = OSSL_HPKE_TSTSIZE; unsigned char authpub[OSSL_HPKE_TSTSIZE]; unsigned char *authpubp = NULL; EVP_PKEY *authpriv = NULL; hpke_suite.kem_id = kem_id; if (hpke_mode == OSSL_HPKE_MODE_AUTH || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { if (TEST_true(OSSL_HPKE_keygen(hpke_suite, authpub, &authpublen, &authpriv, NULL, 0, testctx, NULL)) != 1) { overallresult = 0; } authpubp = authpub; } else { authpublen = 0; } for (kdfind = 0; overallresult == 1 && kdfind < OSSL_NELEM(hpke_kdf_list); kdfind++) { uint16_t kdf_id = hpke_kdf_list[kdfind]; hpke_suite.kdf_id = kdf_id; for (aeadind = 0; overallresult == 1 && aeadind < OSSL_NELEM(hpke_aead_list); aeadind++) { uint16_t aead_id = hpke_aead_list[aeadind]; size_t publen = OSSL_HPKE_TSTSIZE; unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t senderpublen = OSSL_HPKE_TSTSIZE; unsigned char senderpub[OSSL_HPKE_TSTSIZE]; size_t cipherlen = OSSL_HPKE_TSTSIZE; unsigned char cipher[OSSL_HPKE_TSTSIZE]; size_t clearlen = OSSL_HPKE_TSTSIZE; unsigned char clear[OSSL_HPKE_TSTSIZE]; hpke_suite.aead_id = aead_id; if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) overallresult = 0; if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) overallresult = 0; if (hpke_mode == OSSL_HPKE_MODE_PSK || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(OSSL_HPKE_CTX_set1_psk(ctx, pskidp, pskp, psklen))) overallresult = 0; } if (hpke_mode == OSSL_HPKE_MODE_AUTH || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(ctx, authpriv))) overallresult = 0; } if (!TEST_true(OSSL_HPKE_encap(ctx, senderpub, &senderpublen, pub, publen, infop, infolen))) overallresult = 0; /* throw in a call with a too-short cipherlen */ cipherlen = 15; if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, aadp, aadlen, plain, plainlen))) overallresult = 0; /* fix back real cipherlen */ cipherlen = OSSL_HPKE_TSTSIZE; if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, aadp, aadlen, plain, plainlen))) overallresult = 0; OSSL_HPKE_CTX_free(ctx); memset(clear, 0, clearlen); rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL); if (!TEST_ptr(rctx)) overallresult = 0; if (hpke_mode == OSSL_HPKE_MODE_PSK || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { if (!TEST_true(OSSL_HPKE_CTX_set1_psk(rctx, pskidp, pskp, psklen))) overallresult = 0; } if (hpke_mode == OSSL_HPKE_MODE_AUTH || hpke_mode == OSSL_HPKE_MODE_PSKAUTH) { /* check a borked p256 key */ if (hpke_suite.kem_id == OSSL_HPKE_KEM_ID_P256) { /* set to fail decode of authpub this time */ if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(rctx, authpub, 10 ))) overallresult = 0; } if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(rctx, authpubp, authpublen))) overallresult = 0; } if (!TEST_true(OSSL_HPKE_decap(rctx, senderpub, senderpublen, privp, infop, infolen))) overallresult = 0; /* throw in a call with a too-short clearlen */ clearlen = 15; if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, aadp, aadlen, cipher, cipherlen))) overallresult = 0; /* fix up real clearlen again */ clearlen = OSSL_HPKE_TSTSIZE; if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, aadp, aadlen, cipher, cipherlen))) overallresult = 0; OSSL_HPKE_CTX_free(rctx); EVP_PKEY_free(privp); privp = NULL; /* check output */ if (!TEST_mem_eq(clear, clearlen, plain, plainlen)) { overallresult = 0; } if (verbose || overallresult != 1) { const char *res = NULL; res = (overallresult == 1 ? "worked" : "failed"); TEST_note("HPKE %s for mode: %s/0x%02x, "\ "kem: %s/0x%02x, kdf: %s/0x%02x, "\ "aead: %s/0x%02x", res, mode_str_list[mind], (int) mind, kem_str_list[kemind], kem_id, kdf_str_list[kdfind], kdf_id, aead_str_list[aeadind], aead_id); } } } EVP_PKEY_free(authpriv); } } return overallresult; } /** * @brief check roundtrip for export * @return 1 for success, other otherwise */ static int test_hpke_export(void) { int erv = 0; EVP_PKEY *privp = NULL; unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t publen = sizeof(pub); int hpke_mode = OSSL_HPKE_MODE_BASE; OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_CTX *ctx = NULL; OSSL_HPKE_CTX *rctx = NULL; unsigned char exp[32]; unsigned char exp2[32]; unsigned char rexp[32]; unsigned char rexp2[32]; unsigned char plain[] = "quick brown fox"; size_t plainlen = sizeof(plain); unsigned char enc[OSSL_HPKE_TSTSIZE]; size_t enclen = sizeof(enc); unsigned char cipher[OSSL_HPKE_TSTSIZE]; size_t cipherlen = sizeof(cipher); unsigned char clear[OSSL_HPKE_TSTSIZE]; size_t clearlen = sizeof(clear); char *estr = "foo"; if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* a few error cases 1st */ if (!TEST_false(OSSL_HPKE_export(NULL, exp, sizeof(exp), (unsigned char *)estr, strlen(estr)))) goto end; /* ctx before encap should fail too */ if (!TEST_false(OSSL_HPKE_export(ctx, exp, sizeof(exp), (unsigned char *)estr, strlen(estr)))) goto end; if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; /* now for real */ if (!TEST_true(OSSL_HPKE_export(ctx, exp, sizeof(exp), (unsigned char *)estr, strlen(estr)))) goto end; /* check a 2nd call with same input gives same output */ if (!TEST_true(OSSL_HPKE_export(ctx, exp2, sizeof(exp2), (unsigned char *)estr, strlen(estr)))) goto end; if (!TEST_mem_eq(exp, sizeof(exp), exp2, sizeof(exp2))) goto end; if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL))) goto end; if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; if (!TEST_true(OSSL_HPKE_export(rctx, rexp, sizeof(rexp), (unsigned char *)estr, strlen(estr)))) goto end; /* check a 2nd call with same input gives same output */ if (!TEST_true(OSSL_HPKE_export(rctx, rexp2, sizeof(rexp2), (unsigned char *)estr, strlen(estr)))) goto end; if (!TEST_mem_eq(rexp, sizeof(rexp), rexp2, sizeof(rexp2))) goto end; if (!TEST_mem_eq(exp, sizeof(exp), rexp, sizeof(rexp))) goto end; erv = 1; end: OSSL_HPKE_CTX_free(ctx); OSSL_HPKE_CTX_free(rctx); EVP_PKEY_free(privp); return erv; } /** * @brief Check mapping from strings to HPKE suites * @return 1 for success, other otherwise */ static int test_hpke_suite_strs(void) { int overallresult = 1; int kemind = 0; int kdfind = 0; int aeadind = 0; int sind = 0; char sstr[128]; OSSL_HPKE_SUITE stirred; char giant[2048]; for (kemind = 0; kemind != OSSL_NELEM(kem_str_list); kemind++) { for (kdfind = 0; kdfind != OSSL_NELEM(kdf_str_list); kdfind++) { for (aeadind = 0; aeadind != OSSL_NELEM(aead_str_list); aeadind++) { BIO_snprintf(sstr, 128, "%s,%s,%s", kem_str_list[kemind], kdf_str_list[kdfind], aead_str_list[aeadind]); if (TEST_true(OSSL_HPKE_str2suite(sstr, &stirred)) != 1) { if (verbose) TEST_note("Unexpected str2suite fail for :%s", bogus_suite_strs[sind]); overallresult = 0; } } } } for (sind = 0; sind != OSSL_NELEM(bogus_suite_strs); sind++) { if (TEST_false(OSSL_HPKE_str2suite(bogus_suite_strs[sind], &stirred)) != 1) { if (verbose) TEST_note("OSSL_HPKE_str2suite didn't fail for bogus[%d]:%s", sind, bogus_suite_strs[sind]); overallresult = 0; } } /* check a few errors */ if (!TEST_false(OSSL_HPKE_str2suite("", &stirred))) overallresult = 0; if (!TEST_false(OSSL_HPKE_str2suite(NULL, &stirred))) overallresult = 0; if (!TEST_false(OSSL_HPKE_str2suite("", NULL))) overallresult = 0; memset(giant, 'A', sizeof(giant) - 1); giant[sizeof(giant) - 1] = '\0'; if (!TEST_false(OSSL_HPKE_str2suite(giant, &stirred))) overallresult = 0; return overallresult; } /** * @brief try the various GREASEy APIs * @return 1 for success, other otherwise */ static int test_hpke_grease(void) { int overallresult = 1; OSSL_HPKE_SUITE g_suite; unsigned char g_pub[OSSL_HPKE_TSTSIZE]; size_t g_pub_len = OSSL_HPKE_TSTSIZE; unsigned char g_cipher[OSSL_HPKE_TSTSIZE]; size_t g_cipher_len = 266; size_t clearlen = 128; size_t expanded = 0; size_t enclen = 0; size_t ikmelen = 0; memset(&g_suite, 0, sizeof(OSSL_HPKE_SUITE)); /* GREASEing */ /* check too short for public value */ g_pub_len = 10; if (TEST_false(OSSL_HPKE_get_grease_value(NULL, &g_suite, g_pub, &g_pub_len, g_cipher, g_cipher_len, testctx, NULL)) != 1) { overallresult = 0; } /* reset to work */ g_pub_len = OSSL_HPKE_TSTSIZE; if (TEST_true(OSSL_HPKE_get_grease_value(NULL, &g_suite, g_pub, &g_pub_len, g_cipher, g_cipher_len, testctx, NULL)) != 1) { overallresult = 0; } /* expansion */ expanded = OSSL_HPKE_get_ciphertext_size(g_suite, clearlen); if (!TEST_size_t_gt(expanded, clearlen)) { overallresult = 0; } enclen = OSSL_HPKE_get_public_encap_size(g_suite); if (!TEST_size_t_ne(enclen, 0)) overallresult = 0; /* not really GREASE but we'll check ikmelen thing */ ikmelen = OSSL_HPKE_get_recommended_ikmelen(g_suite); if (!TEST_size_t_ne(ikmelen, 0)) overallresult = 0; return overallresult; } /* * Make a set of calls with odd parameters */ static int test_hpke_oddcalls(void) { int erv = 0; EVP_PKEY *privp = NULL; unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t publen = sizeof(pub); int hpke_mode = OSSL_HPKE_MODE_BASE; int bad_mode = 0xbad; OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_SUITE bad_suite = { 0xbad, 0xbad, 0xbad }; OSSL_HPKE_CTX *ctx = NULL; OSSL_HPKE_CTX *rctx = NULL; unsigned char plain[] = "quick brown fox"; size_t plainlen = sizeof(plain); unsigned char enc[OSSL_HPKE_TSTSIZE], smallenc[10]; size_t enclen = sizeof(enc), smallenclen = sizeof(smallenc); unsigned char cipher[OSSL_HPKE_TSTSIZE]; size_t cipherlen = sizeof(cipher); unsigned char clear[OSSL_HPKE_TSTSIZE]; size_t clearlen = sizeof(clear); unsigned char fake_ikm[OSSL_HPKE_TSTSIZE]; char *badpropq = "yeah, this won't work"; uint64_t lseq = 0; char giant_pskid[OSSL_HPKE_MAX_PARMLEN + 10]; unsigned char info[OSSL_HPKE_TSTSIZE]; /* many of the calls below are designed to get better test coverage */ /* NULL ctx calls */ OSSL_HPKE_CTX_free(NULL); if (!TEST_false(OSSL_HPKE_CTX_set_seq(NULL, 1))) goto end; if (!TEST_false(OSSL_HPKE_CTX_get_seq(NULL, &lseq))) goto end; if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(NULL, pub, publen))) goto end; if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(NULL, privp))) goto end; if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(NULL, NULL, 0))) goto end; if (!TEST_false(OSSL_HPKE_CTX_set1_psk(NULL, NULL, NULL, 0))) goto end; /* bad suite calls */ hpke_suite.aead_id = 0xbad; if (!TEST_false(OSSL_HPKE_suite_check(hpke_suite))) goto end; hpke_suite.aead_id = OSSL_HPKE_AEAD_ID_AES_GCM_128; if (!TEST_false(OSSL_HPKE_suite_check(bad_suite))) goto end; if (!TEST_false(OSSL_HPKE_get_recommended_ikmelen(bad_suite))) goto end; if (!TEST_false(OSSL_HPKE_get_public_encap_size(bad_suite))) goto end; if (!TEST_false(OSSL_HPKE_get_ciphertext_size(bad_suite, 0))) goto end; if (!TEST_false(OSSL_HPKE_keygen(bad_suite, pub, &publen, &privp, NULL, 0, testctx, badpropq))) goto end; if (!TEST_false(OSSL_HPKE_keygen(bad_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; /* dodgy keygen calls */ /* no pub */ if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, NULL, &publen, &privp, NULL, 0, testctx, NULL))) goto end; /* ikmlen but NULL ikm */ if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 80, testctx, NULL))) goto end; /* zero ikmlen but ikm */ if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, fake_ikm, 0, testctx, NULL))) goto end; /* GIANT ikmlen */ if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, fake_ikm, -1, testctx, NULL))) goto end; /* short publen */ publen = 10; if (!TEST_false(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; publen = sizeof(pub); /* encap/decap with NULLs */ if (!TEST_false(OSSL_HPKE_encap(NULL, NULL, NULL, NULL, 0, NULL, 0))) goto end; if (!TEST_false(OSSL_HPKE_decap(NULL, NULL, 0, NULL, NULL, 0))) goto end; /* * run through a sender/recipient set of calls but with * failing calls interspersed whenever possible */ /* good keygen */ if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; /* a psk context with no psk => encap fail */ if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_PSK, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* set bad length psk */ if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, "foo", (unsigned char *)"bar", -1))) goto end; /* set bad length pskid */ memset(giant_pskid, 'A', sizeof(giant_pskid) - 1); giant_pskid[sizeof(giant_pskid) - 1] = '\0'; if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, giant_pskid, (unsigned char *)"bar", 3))) goto end; /* still no psk really set so encap fails */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; OSSL_HPKE_CTX_free(ctx); /* bad suite */ if (!TEST_ptr_null(ctx = OSSL_HPKE_CTX_new(hpke_mode, bad_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* bad mode */ if (!TEST_ptr_null(ctx = OSSL_HPKE_CTX_new(bad_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* make good ctx */ if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* too long ikm */ if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(ctx, fake_ikm, -1))) goto end; /* zero length ikm */ if (!TEST_false(OSSL_HPKE_CTX_set1_ikme(ctx, fake_ikm, 0))) goto end; /* NULL authpub */ if (!TEST_false(OSSL_HPKE_CTX_set1_authpub(ctx, NULL, 0))) goto end; /* NULL auth priv */ if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(ctx, NULL))) goto end; /* priv good, but mode is bad */ if (!TEST_false(OSSL_HPKE_CTX_set1_authpriv(ctx, privp))) goto end; /* bad mode for psk */ if (!TEST_false(OSSL_HPKE_CTX_set1_psk(ctx, "foo", (unsigned char *)"bar", 3))) goto end; /* seal before encap */ if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; /* encap with dodgy public */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, NULL, 0))) goto end; /* encap with too big info */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, info, -1))) goto end; /* encap with NULL info & non-zero infolen */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, NULL, 1))) goto end; /* encap with non-NULL info & zero infolen */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, 1, info, 0))) goto end; /* encap with too small enc */ if (!TEST_false(OSSL_HPKE_encap(ctx, smallenc, &smallenclen, pub, 1, NULL, 0))) goto end; /* good encap */ if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; /* second encap fail */ if (!TEST_false(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; plainlen = 0; /* should fail for no plaintext */ if (!TEST_false(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; plainlen = sizeof(plain); /* working seal */ if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; /* receiver side */ /* decap fail with psk mode but no psk set */ if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_PSK, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL))) goto end; if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; /* done with PSK mode */ OSSL_HPKE_CTX_free(rctx); /* back good calls for base mode */ if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL))) goto end; /* open before decap */ if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; /* decap with info too long */ if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, info, -1))) goto end; /* good decap */ if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; /* second decap fail */ if (!TEST_false(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; /* no space for recovered clear */ clearlen = 0; if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; clearlen = OSSL_HPKE_TSTSIZE; /* seq wrap around test */ if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, -1))) goto end; if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, 0))) goto end; if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; if (!TEST_mem_eq(plain, plainlen, clear, clearlen)) goto end; erv = 1; end: OSSL_HPKE_CTX_free(ctx); OSSL_HPKE_CTX_free(rctx); EVP_PKEY_free(privp); return erv; } #ifndef OPENSSL_NO_ECX /* from RFC 9180 Appendix A.1.1 */ static const unsigned char ikm25519[] = { 0x72, 0x68, 0x60, 0x0d, 0x40, 0x3f, 0xce, 0x43, 0x15, 0x61, 0xae, 0xf5, 0x83, 0xee, 0x16, 0x13, 0x52, 0x7c, 0xff, 0x65, 0x5c, 0x13, 0x43, 0xf2, 0x98, 0x12, 0xe6, 0x67, 0x06, 0xdf, 0x32, 0x34 }; static const unsigned char pub25519[] = { 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28, 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d, 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44, 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31 }; #endif /* from RFC9180 Appendix A.3.1 */ static const unsigned char ikmp256[] = { 0x42, 0x70, 0xe5, 0x4f, 0xfd, 0x08, 0xd7, 0x9d, 0x59, 0x28, 0x02, 0x0a, 0xf4, 0x68, 0x6d, 0x8f, 0x6b, 0x7d, 0x35, 0xdb, 0xe4, 0x70, 0x26, 0x5f, 0x1f, 0x5a, 0xa2, 0x28, 0x16, 0xce, 0x86, 0x0e }; static const unsigned char pubp256[] = { 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50, 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14, 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e, 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a, 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4, 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00, 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b, 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18, 0xc4 }; /* * A test vector that exercises the counter iteration * for p256. This was contributed by Ilari L. on the * CFRG list, see the mail archive: * https://mailarchive.ietf.org/arch/msg/cfrg/4zwl_y5YN6OU9oeWZOMHNOlOa2w/ */ static const unsigned char ikmiter[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x38, 0xb5, 0xec }; static const unsigned char pubiter[] = { 0x04, 0x7d, 0x0c, 0x87, 0xff, 0xd5, 0xd1, 0x45, 0x54, 0xa7, 0x51, 0xdf, 0xa3, 0x99, 0x26, 0xa9, 0xe3, 0x0e, 0x7c, 0x3c, 0x65, 0x62, 0x4f, 0x4b, 0x5f, 0xb3, 0xad, 0x7a, 0xa4, 0xda, 0xc2, 0x4a, 0xd8, 0xf5, 0xbe, 0xd0, 0xe8, 0x6e, 0xb8, 0x84, 0x1c, 0xe4, 0x89, 0x2e, 0x0f, 0xc3, 0x87, 0xbb, 0xdb, 0xfe, 0x16, 0x0d, 0x58, 0x9c, 0x89, 0x2d, 0xd4, 0xb1, 0x46, 0x4a, 0xc3, 0x51, 0xc5, 0x6f, 0xb6 }; /* from RFC9180 Appendix A.6.1 */ static const unsigned char ikmp521[] = { 0x7f, 0x06, 0xab, 0x82, 0x15, 0x10, 0x5f, 0xc4, 0x6a, 0xce, 0xeb, 0x2e, 0x3d, 0xc5, 0x02, 0x8b, 0x44, 0x36, 0x4f, 0x96, 0x04, 0x26, 0xeb, 0x0d, 0x8e, 0x40, 0x26, 0xc2, 0xf8, 0xb5, 0xd7, 0xe7, 0xa9, 0x86, 0x68, 0x8f, 0x15, 0x91, 0xab, 0xf5, 0xab, 0x75, 0x3c, 0x35, 0x7a, 0x5d, 0x6f, 0x04, 0x40, 0x41, 0x4b, 0x4e, 0xd4, 0xed, 0xe7, 0x13, 0x17, 0x77, 0x2a, 0xc9, 0x8d, 0x92, 0x39, 0xf7, 0x09, 0x04 }; static const unsigned char pubp521[] = { 0x04, 0x01, 0x38, 0xb3, 0x85, 0xca, 0x16, 0xbb, 0x0d, 0x5f, 0xa0, 0xc0, 0x66, 0x5f, 0xbb, 0xd7, 0xe6, 0x9e, 0x3e, 0xe2, 0x9f, 0x63, 0x99, 0x1d, 0x3e, 0x9b, 0x5f, 0xa7, 0x40, 0xaa, 0xb8, 0x90, 0x0a, 0xae, 0xed, 0x46, 0xed, 0x73, 0xa4, 0x90, 0x55, 0x75, 0x84, 0x25, 0xa0, 0xce, 0x36, 0x50, 0x7c, 0x54, 0xb2, 0x9c, 0xc5, 0xb8, 0x5a, 0x5c, 0xee, 0x6b, 0xae, 0x0c, 0xf1, 0xc2, 0x1f, 0x27, 0x31, 0xec, 0xe2, 0x01, 0x3d, 0xc3, 0xfb, 0x7c, 0x8d, 0x21, 0x65, 0x4b, 0xb1, 0x61, 0xb4, 0x63, 0x96, 0x2c, 0xa1, 0x9e, 0x8c, 0x65, 0x4f, 0xf2, 0x4c, 0x94, 0xdd, 0x28, 0x98, 0xde, 0x12, 0x05, 0x1f, 0x1e, 0xd0, 0x69, 0x22, 0x37, 0xfb, 0x02, 0xb2, 0xf8, 0xd1, 0xdc, 0x1c, 0x73, 0xe9, 0xb3, 0x66, 0xb5, 0x29, 0xeb, 0x43, 0x6e, 0x98, 0xa9, 0x96, 0xee, 0x52, 0x2a, 0xef, 0x86, 0x3d, 0xd5, 0x73, 0x9d, 0x2f, 0x29, 0xb0 }; static int test_hpke_random_suites(void) { OSSL_HPKE_SUITE def_suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_SUITE suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_SUITE suite2 = { 0xff01, 0xff02, 0xff03 }; unsigned char enc[200]; size_t enclen = sizeof(enc); unsigned char ct[500]; size_t ctlen = sizeof(ct); /* test with NULL/0 inputs */ if (!TEST_false(OSSL_HPKE_get_grease_value(NULL, NULL, NULL, NULL, NULL, 0, testctx, NULL))) return 0; enclen = 10; if (!TEST_false(OSSL_HPKE_get_grease_value(&def_suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */ /* test with a should-be-good suite */ if (!TEST_true(OSSL_HPKE_get_grease_value(&def_suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; /* no suggested suite */ enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */ if (!TEST_true(OSSL_HPKE_get_grease_value(NULL, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; /* suggested suite with P-521, just to be sure we hit long values */ enclen = sizeof(enc); /* reset, 'cause get_grease() will have set */ suite.kem_id = OSSL_HPKE_KEM_ID_P521; if (!TEST_true(OSSL_HPKE_get_grease_value(&suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; enclen = sizeof(enc); ctlen = 2; /* too-short cttext (can't fit an aead tag) */ if (!TEST_false(OSSL_HPKE_get_grease_value(NULL, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; ctlen = sizeof(ct); enclen = sizeof(enc); suite.kem_id = OSSL_HPKE_KEM_ID_X25519; /* back to default */ suite.aead_id = 0x1234; /* bad aead */ if (!TEST_false(OSSL_HPKE_get_grease_value(&suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; enclen = sizeof(enc); suite.aead_id = def_suite.aead_id; /* good aead */ suite.kdf_id = 0x3451; /* bad kdf */ if (!TEST_false(OSSL_HPKE_get_grease_value(&suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; enclen = sizeof(enc); suite.kdf_id = def_suite.kdf_id; /* good kdf */ suite.kem_id = 0x4517; /* bad kem */ if (!TEST_false(OSSL_HPKE_get_grease_value(&suite, &suite2, enc, &enclen, ct, ctlen, testctx, NULL))) return 0; return 1; } /* * @brief generate a key pair from initial key material (ikm) and check public * @param kem_id the KEM to use (RFC9180 code point) * @ikm is the initial key material buffer * @ikmlen is the length of ikm * @pub is the public key buffer * @publen is the length of the public key * @return 1 for good, other otherwise * * This calls OSSL_HPKE_keygen specifying only the IKM, then * compares the key pair values with the already-known values * that were input. */ static int test_hpke_one_ikm_gen(uint16_t kem_id, const unsigned char *ikm, size_t ikmlen, const unsigned char *pub, size_t publen) { OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; unsigned char lpub[OSSL_HPKE_TSTSIZE]; size_t lpublen = OSSL_HPKE_TSTSIZE; EVP_PKEY *sk = NULL; hpke_suite.kem_id = kem_id; if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, lpub, &lpublen, &sk, ikm, ikmlen, testctx, NULL))) return 0; if (!TEST_ptr(sk)) return 0; EVP_PKEY_free(sk); if (!TEST_mem_eq(pub, publen, lpub, lpublen)) return 0; return 1; } /* * @brief test some uses of IKM produce the expected public keys */ static int test_hpke_ikms(void) { int res = 1; #ifndef OPENSSL_NO_ECX res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_X25519, ikm25519, sizeof(ikm25519), pub25519, sizeof(pub25519)); if (res != 1) return res; #endif res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P521, ikmp521, sizeof(ikmp521), pubp521, sizeof(pubp521)); if (res != 1) return res; res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P256, ikmp256, sizeof(ikmp256), pubp256, sizeof(pubp256)); if (res != 1) return res; res = test_hpke_one_ikm_gen(OSSL_HPKE_KEM_ID_P256, ikmiter, sizeof(ikmiter), pubiter, sizeof(pubiter)); if (res != 1) return res; return res; } /* * Test that use of a compressed format auth public key works * We'll do a typical round-trip for auth mode but provide the * auth public key in compressed form. That should work. */ static int test_hpke_compressed(void) { int erv = 0; EVP_PKEY *privp = NULL; unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t publen = sizeof(pub); EVP_PKEY *authpriv = NULL; unsigned char authpub[OSSL_HPKE_TSTSIZE]; size_t authpublen = sizeof(authpub); int hpke_mode = OSSL_HPKE_MODE_AUTH; OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_CTX *ctx = NULL; OSSL_HPKE_CTX *rctx = NULL; unsigned char plain[] = "quick brown fox"; size_t plainlen = sizeof(plain); unsigned char enc[OSSL_HPKE_TSTSIZE]; size_t enclen = sizeof(enc); unsigned char cipher[OSSL_HPKE_TSTSIZE]; size_t cipherlen = sizeof(cipher); unsigned char clear[OSSL_HPKE_TSTSIZE]; size_t clearlen = sizeof(clear); hpke_suite.kem_id = OSSL_HPKE_KEM_ID_P256; /* generate auth key pair */ if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, authpub, &authpublen, &authpriv, NULL, 0, testctx, NULL))) goto end; /* now get the compressed form public key */ if (!TEST_true(EVP_PKEY_set_utf8_string_param(authpriv, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED))) goto end; if (!TEST_true(EVP_PKEY_get_octet_string_param(authpriv, OSSL_PKEY_PARAM_PUB_KEY, authpub, sizeof(authpub), &authpublen))) goto end; /* sender side as usual */ if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; if (!TEST_true(OSSL_HPKE_CTX_set1_authpriv(ctx, authpriv))) goto end; if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; /* receiver side providing compressed form of auth public */ if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL))) goto end; if (!TEST_true(OSSL_HPKE_CTX_set1_authpub(rctx, authpub, authpublen))) goto end; if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; erv = 1; end: EVP_PKEY_free(privp); EVP_PKEY_free(authpriv); OSSL_HPKE_CTX_free(ctx); OSSL_HPKE_CTX_free(rctx); return erv; } /* * Test that nonce reuse calls are prevented as we expect */ static int test_hpke_noncereuse(void) { int erv = 0; EVP_PKEY *privp = NULL; unsigned char pub[OSSL_HPKE_TSTSIZE]; size_t publen = sizeof(pub); int hpke_mode = OSSL_HPKE_MODE_BASE; OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; OSSL_HPKE_CTX *ctx = NULL; OSSL_HPKE_CTX *rctx = NULL; unsigned char plain[] = "quick brown fox"; size_t plainlen = sizeof(plain); unsigned char enc[OSSL_HPKE_TSTSIZE]; size_t enclen = sizeof(enc); unsigned char cipher[OSSL_HPKE_TSTSIZE]; size_t cipherlen = sizeof(cipher); unsigned char clear[OSSL_HPKE_TSTSIZE]; size_t clearlen = sizeof(clear); uint64_t seq = 0xbad1dea; /* sender side is not allowed set seq once some crypto done */ if (!TEST_true(OSSL_HPKE_keygen(hpke_suite, pub, &publen, &privp, NULL, 0, testctx, NULL))) goto end; if (!TEST_ptr(ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_SENDER, testctx, NULL))) goto end; /* set seq will fail before any crypto done */ if (!TEST_false(OSSL_HPKE_CTX_set_seq(ctx, seq))) goto end; if (!TEST_true(OSSL_HPKE_encap(ctx, enc, &enclen, pub, publen, NULL, 0))) goto end; /* set seq will also fail after some crypto done */ if (!TEST_false(OSSL_HPKE_CTX_set_seq(ctx, seq + 1))) goto end; if (!TEST_true(OSSL_HPKE_seal(ctx, cipher, &cipherlen, NULL, 0, plain, plainlen))) goto end; /* receiver side is allowed control seq */ if (!TEST_ptr(rctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, OSSL_HPKE_ROLE_RECEIVER, testctx, NULL))) goto end; /* set seq will work before any crypto done */ if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, seq))) goto end; if (!TEST_true(OSSL_HPKE_decap(rctx, enc, enclen, privp, NULL, 0))) goto end; /* set seq will work for receivers even after crypto done */ if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, seq))) goto end; /* but that value isn't good so decap will fail */ if (!TEST_false(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; /* reset seq to correct value and _open() should work */ if (!TEST_true(OSSL_HPKE_CTX_set_seq(rctx, 0))) goto end; if (!TEST_true(OSSL_HPKE_open(rctx, clear, &clearlen, NULL, 0, cipher, cipherlen))) goto end; erv = 1; end: EVP_PKEY_free(privp); OSSL_HPKE_CTX_free(ctx); OSSL_HPKE_CTX_free(rctx); return erv; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_VERBOSE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "v", OPT_VERBOSE, '-', "Enable verbose mode" }, { OPT_HELP_STR, 1, '-', "Run HPKE tests\n" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_VERBOSE: verbose = 1; /* Print progress dots */ break; case OPT_TEST_CASES: break; default: return 0; } } if (!test_get_libctx(&testctx, &nullprov, NULL, &deflprov, "default")) return 0; #ifndef OPENSSL_NO_ECX ADD_TEST(export_only_test); ADD_TEST(x25519kdfsha256_hkdfsha256_aes128gcm_base_test); ADD_TEST(x25519kdfsha256_hkdfsha256_aes128gcm_psk_test); #endif ADD_TEST(P256kdfsha256_hkdfsha256_aes128gcm_base_test); ADD_TEST(test_hpke_export); ADD_TEST(test_hpke_modes_suites); ADD_TEST(test_hpke_suite_strs); ADD_TEST(test_hpke_grease); ADD_TEST(test_hpke_ikms); ADD_TEST(test_hpke_random_suites); ADD_TEST(test_hpke_oddcalls); ADD_TEST(test_hpke_compressed); ADD_TEST(test_hpke_noncereuse); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(deflprov); OSSL_PROVIDER_unload(nullprov); OSSL_LIB_CTX_free(testctx); }
./openssl/test/punycode_test.c
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/crypto.h> #include <string.h> #include "crypto/punycode.h" #include "internal/nelem.h" #include "internal/packet.h" #include "testutil.h" static const struct puny_test { unsigned int raw[50]; const char *encoded; } puny_cases[] = { { /* Test of 4 byte codepoint using smileyface emoji */ { 0x1F600 }, "e28h" }, /* Test cases from RFC 3492 */ { /* Arabic (Egyptian) */ { 0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644, 0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F }, "egbpdaj6bu4bxfgehfvwxn" }, { /* Chinese (simplified) */ { 0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587 }, "ihqwcrb4cv8a8dqg056pqjye" }, { /* Chinese (traditional) */ { 0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587 }, "ihqwctvzc91f659drss3x8bo0yb" }, { /* Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky */ { 0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074, 0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D, 0x0065, 0x0073, 0x006B, 0x0079 }, "Proprostnemluvesky-uyb24dma41a" }, { /* Hebrew */ { 0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8, 0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2, 0x05D1, 0x05E8, 0x05D9, 0x05EA }, "4dbcagdahymbxekheh6e0a7fei0b" }, { /* Hindi (Devanagari) */ { 0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D, 0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939, 0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947, 0x0939, 0x0948, 0x0902 }, "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd" }, { /* Japanese (kanji and hiragana) */ { 0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092, 0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B }, "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa" }, { /* Korean (Hangul syllables) */ { 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774, 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74, 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C }, "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c" }, { /* Russian (Cyrillic) */ { 0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E, 0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440, 0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A, 0x0438 }, "b1abfaaepdrnnbgefbaDotcwatmq2g4l" }, { /* Spanish */ { 0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070, 0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070, 0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061, 0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070, 0x0061, 0x00F1, 0x006F, 0x006C }, "PorqunopuedensimplementehablarenEspaol-fmd56a" }, { /* Vietnamese */ { 0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B, 0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068, 0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067, 0x0056, 0x0069, 0x1EC7, 0x0074 }, "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g" }, { /* Japanese: 3<nen>B<gumi><kinpachi><sensei> */ { 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F }, "3B-ww4c5e180e575a65lsy2b" }, { /* Japanese: <amuro><namie>-with-SUPER-MONKEYS */ { 0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074, 0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D, 0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053 }, "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n" }, { /* Japanese: Hello-Another-Way-<sorezore><no><basho> */ { 0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F, 0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D, 0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240 }, "Hello-Another-Way--fc4qua05auwb3674vfr0b" }, { /* Japanese: <hitotsu><yane><no><shita>2 */ { 0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032 }, "2-u9tlzr9756bt3uc0v" }, { /* Japanese: Maji<de>Koi<suru>5<byou><mae> */ { 0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059, 0x308B, 0x0035, 0x79D2, 0x524D }, "MajiKoi5-783gue6qz075azm5e" }, { /* Japanese: <pafii>de<runba> */ { 0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0 }, "de-jg4avhby1noc0d" }, { /* Japanese: <sono><supiido><de> */ { 0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067 }, "d9juau41awczczp" }, { /* -> $1.00 <- */ { 0x002D, 0x003E, 0x0020, 0x0024, 0x0031, 0x002E, 0x0030, 0x0030, 0x0020, 0x003C, 0x002D }, "-> $1.00 <--" } }; static int test_punycode(int n) { const struct puny_test *tc = puny_cases + n; unsigned int buffer[50]; unsigned int bsize = OSSL_NELEM(buffer); size_t i; if (!TEST_true(ossl_punycode_decode(tc->encoded, strlen(tc->encoded), buffer, &bsize))) return 0; for (i = 0; i < OSSL_NELEM(tc->raw); i++) if (tc->raw[i] == 0) break; if (!TEST_mem_eq(buffer, bsize * sizeof(*buffer), tc->raw, i * sizeof(*tc->raw))) return 0; return 1; } static const struct bad_decode_test { size_t outlen; const char input[20]; } bad_decode_tests[] = { { 20, "xn--e-*" }, /* bad digit '*' */ { 10, "xn--e-999" }, /* loop > enc_len */ { 20, "xn--e-999999999" }, /* Too big */ { 20, {'x', 'n', '-', '-', (char)0x80, '-' } }, /* Not basic */ { 20, "xn--e-Oy65t" }, /* codepoint > 0x10FFFF */ }; static int test_a2ulabel_bad_decode(int tst) { char out[20]; return TEST_int_eq(ossl_a2ulabel(bad_decode_tests[tst].input, out, bad_decode_tests[tst].outlen), -1); } static int test_a2ulabel(void) { char out[50]; char in[530] = { 0 }; /* * The punycode being passed in and parsed is malformed but we're not * verifying that behaviour here. */ if (!TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, 1), 0) || !TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, 7), 1)) return 0; /* Test for an off by one on the buffer size works */ if (!TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, 6), 0) || !TEST_int_eq(ossl_a2ulabel("xn--a.b.c", out, 7), 1) || !TEST_str_eq(out,"\xc2\x80.b.c")) return 0; /* Test 4 byte smiley face */ if (!TEST_int_eq(ossl_a2ulabel("xn--e28h.com", out, 10), 1)) return 0; /* Test that we dont overflow the fixed internal buffer of 512 bytes when the starting bytes are copied */ strcpy(in, "xn--"); memset(in + 4, 'e', 513); memcpy(in + 517, "-3ya", 4); if (!TEST_int_eq(ossl_a2ulabel(in, out, 50), -1)) return 0; return 1; } static int test_puny_overrun(void) { static const unsigned int out[] = { 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F }; static const char *in = "3B-ww4c5e180e575a65lsy2b"; unsigned int buf[OSSL_NELEM(out)]; unsigned int bsize = OSSL_NELEM(buf) - 1; if (!TEST_false(ossl_punycode_decode(in, strlen(in), buf, &bsize))) { if (TEST_mem_eq(buf, bsize * sizeof(*buf), out, sizeof(out))) TEST_error("CRITICAL: buffer overrun detected!"); return 0; } return 1; } static int test_dotted_overflow(void) { static const char string[] = "a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a"; const size_t num_reps = OSSL_NELEM(string) / 2; WPACKET p; BUF_MEM *in; char *out = NULL; size_t i; int res = 0; /* Create out input punycode string */ if (!TEST_ptr(in = BUF_MEM_new())) return 0; if (!TEST_true(WPACKET_init_len(&p, in, 0))) { BUF_MEM_free(in); return 0; } for (i = 0; i < num_reps; i++) { if (i > 1 && !TEST_true(WPACKET_put_bytes_u8(&p, '.'))) goto err; if (!TEST_true(WPACKET_memcpy(&p, "xn--a", sizeof("xn--a") - 1))) goto err; } if (!TEST_true(WPACKET_put_bytes_u8(&p, '\0'))) goto err; if (!TEST_ptr(out = OPENSSL_malloc(in->length))) goto err; /* Test the decode into an undersized buffer */ memset(out, 0x7f, in->length - 1); if (!TEST_int_le(ossl_a2ulabel(in->data, out, num_reps), 0) || !TEST_int_eq(out[num_reps], 0x7f)) goto err; /* Test the decode works into a full size buffer */ if (!TEST_int_gt(ossl_a2ulabel(in->data, out, in->length), 0) || !TEST_size_t_eq(strlen(out), num_reps * 3)) goto err; res = 1; err: WPACKET_cleanup(&p); BUF_MEM_free(in); OPENSSL_free(out); return res; } int setup_tests(void) { ADD_ALL_TESTS(test_punycode, OSSL_NELEM(puny_cases)); ADD_TEST(test_dotted_overflow); ADD_TEST(test_a2ulabel); ADD_TEST(test_puny_overrun); ADD_ALL_TESTS(test_a2ulabel_bad_decode, OSSL_NELEM(bad_decode_tests)); return 1; }
./openssl/test/dsa_no_digest_size_test.c
/* * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * DSA low level APIs are deprecated for public use, but still ok for * internal use. */ #include "internal/deprecated.h" #include <stdlib.h> #include <string.h> #include "testutil.h" #include <openssl/evp.h> #include <openssl/err.h> #include <openssl/rand.h> #ifndef OPENSSL_NO_DSA #include <openssl/dsa.h> static DSA *dsakey; /* * These parameters are from test/recipes/04-test_pem_data/dsaparam.pem, * converted using dsaparam -C */ static DSA *load_dsa_params(void) { static unsigned char dsap_2048[] = { 0xAE, 0x35, 0x7D, 0x4E, 0x1D, 0x96, 0xE2, 0x9F, 0x00, 0x96, 0x60, 0x5A, 0x6E, 0x4D, 0x07, 0x8D, 0xA5, 0x7C, 0xBC, 0xF9, 0xAD, 0xD7, 0x9F, 0xD5, 0xE9, 0xEE, 0xA6, 0x33, 0x51, 0xDE, 0x7B, 0x72, 0xD2, 0x75, 0xAA, 0x71, 0x77, 0xF1, 0x63, 0xFB, 0xB6, 0xEC, 0x5A, 0xBA, 0x0D, 0x72, 0xA2, 0x1A, 0x1C, 0x64, 0xB8, 0xE5, 0x89, 0x09, 0x6D, 0xC9, 0x6F, 0x0B, 0x7F, 0xD2, 0xCE, 0x9F, 0xEF, 0x87, 0x5A, 0xB6, 0x67, 0x2F, 0xEF, 0xEE, 0xEB, 0x59, 0xF5, 0x5E, 0xFF, 0xA8, 0x28, 0x84, 0x9E, 0x5B, 0x37, 0x09, 0x11, 0x80, 0x7C, 0x08, 0x5C, 0xD5, 0xE1, 0x48, 0x4B, 0xD2, 0x68, 0xFB, 0x3F, 0x9F, 0x2B, 0x6B, 0x6C, 0x0D, 0x48, 0x1B, 0x1A, 0x80, 0xC2, 0xEB, 0x11, 0x1B, 0x37, 0x79, 0xD6, 0x8C, 0x8B, 0x72, 0x3E, 0x67, 0xA5, 0x05, 0x0E, 0x41, 0x8A, 0x9E, 0x35, 0x50, 0xB4, 0xD2, 0x40, 0x27, 0x6B, 0xFD, 0xE0, 0x64, 0x6B, 0x5B, 0x38, 0x42, 0x94, 0xB5, 0x49, 0xDA, 0xEF, 0x6E, 0x78, 0x37, 0xCD, 0x30, 0x89, 0xC3, 0x45, 0x50, 0x7B, 0x9C, 0x8C, 0xE7, 0x1C, 0x98, 0x70, 0x71, 0x5D, 0x79, 0x5F, 0xEF, 0xE8, 0x94, 0x85, 0x53, 0x3E, 0xEF, 0xA3, 0x2C, 0xCE, 0x1A, 0xAB, 0x7D, 0xD6, 0x5E, 0x14, 0xCD, 0x51, 0x54, 0x89, 0x9D, 0x77, 0xE4, 0xF8, 0x22, 0xF0, 0x35, 0x10, 0x75, 0x05, 0x71, 0x51, 0x4F, 0x8C, 0x4C, 0x5C, 0x0D, 0x2C, 0x2C, 0xBE, 0x6C, 0x34, 0xEE, 0x12, 0x82, 0x87, 0x03, 0x19, 0x06, 0x12, 0xA8, 0xAA, 0xF4, 0x0D, 0x3C, 0x49, 0xCC, 0x70, 0x5A, 0xD8, 0x32, 0xEE, 0x32, 0x50, 0x85, 0x70, 0xE8, 0x18, 0xFD, 0x74, 0x80, 0x53, 0x32, 0x57, 0xEE, 0x50, 0xC9, 0xAE, 0xEB, 0xAE, 0xB6, 0x22, 0x32, 0x16, 0x6B, 0x8C, 0x59, 0xDA, 0xEE, 0x1D, 0x33, 0xDF, 0x4C, 0xA2, 0x3D }; static unsigned char dsaq_2048[] = { 0xAD, 0x2D, 0x6E, 0x17, 0xB0, 0xF3, 0xEB, 0xC7, 0xB8, 0xEE, 0x95, 0x78, 0xF2, 0x17, 0xF5, 0x33, 0x01, 0x67, 0xBC, 0xDE, 0x93, 0xFF, 0xEE, 0x40, 0xE8, 0x7F, 0xF1, 0x93, 0x6D, 0x4B, 0x87, 0x13 }; static unsigned char dsag_2048[] = { 0x66, 0x6F, 0xDA, 0x63, 0xA5, 0x8E, 0xD2, 0x4C, 0xD5, 0x45, 0x2D, 0x76, 0x5D, 0x5F, 0xCD, 0x4A, 0xB4, 0x1A, 0x42, 0x35, 0x86, 0x3A, 0x6F, 0xA9, 0xFA, 0x27, 0xAB, 0xDE, 0x03, 0x21, 0x36, 0x0A, 0x07, 0x29, 0xC9, 0x2F, 0x6D, 0x49, 0xA8, 0xF7, 0xC6, 0xF4, 0x92, 0xD7, 0x73, 0xC1, 0xD8, 0x76, 0x0E, 0x61, 0xA7, 0x0B, 0x6E, 0x96, 0xB8, 0xC8, 0xCB, 0x38, 0x35, 0x12, 0x20, 0x79, 0xA5, 0x08, 0x28, 0x35, 0x5C, 0xBC, 0x52, 0x16, 0xAF, 0x52, 0xBA, 0x0F, 0xC3, 0xB1, 0x63, 0x12, 0x27, 0x0B, 0x74, 0xA4, 0x47, 0x43, 0xD6, 0x30, 0xB8, 0x9C, 0x2E, 0x40, 0x14, 0xCD, 0x99, 0x7F, 0xE8, 0x8E, 0x37, 0xB0, 0xA9, 0x3F, 0x54, 0xE9, 0x66, 0x22, 0x61, 0x4C, 0xF8, 0x49, 0x03, 0x57, 0x14, 0x32, 0x1D, 0x37, 0x3D, 0xE2, 0x92, 0xF8, 0x8E, 0xA0, 0x6A, 0x66, 0x63, 0xF0, 0xB0, 0x6E, 0x07, 0x2B, 0x3D, 0xBF, 0xD0, 0x84, 0x6A, 0xAA, 0x1F, 0x30, 0x77, 0x65, 0xE5, 0xFC, 0xF5, 0xEC, 0x55, 0xCE, 0x73, 0xDB, 0xBE, 0xA7, 0x8D, 0x3A, 0x9F, 0x7A, 0xED, 0x4F, 0xAF, 0xA2, 0x80, 0x4C, 0x30, 0x9E, 0x28, 0x49, 0x65, 0x40, 0xF0, 0x03, 0x45, 0x56, 0x99, 0xA2, 0x93, 0x1B, 0x9C, 0x46, 0xDE, 0xBD, 0xA8, 0xAB, 0x5F, 0x90, 0x3F, 0xB7, 0x3F, 0xD4, 0x6F, 0x8D, 0x5A, 0x30, 0xE1, 0xD4, 0x63, 0x3A, 0x6A, 0x7C, 0x8F, 0x24, 0xFC, 0xD9, 0x14, 0x28, 0x09, 0xE4, 0x84, 0x4E, 0x17, 0x43, 0x56, 0xB8, 0xD4, 0x4B, 0xA2, 0x29, 0x45, 0xD3, 0x13, 0xF0, 0xC2, 0x76, 0x9B, 0x01, 0xA0, 0x80, 0x6E, 0x93, 0x63, 0x5E, 0x87, 0x24, 0x20, 0x2A, 0xFF, 0xBB, 0x9F, 0xA8, 0x99, 0x6C, 0xA7, 0x9A, 0x00, 0xB9, 0x7D, 0xDA, 0x66, 0xC9, 0xC0, 0x72, 0x72, 0x22, 0x0F, 0x1A, 0xCC, 0x23, 0xD9, 0xB7, 0x5F, 0x1B }; DSA *dsa = DSA_new(); BIGNUM *p, *q, *g; if (dsa == NULL) return NULL; if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_2048, sizeof(dsap_2048), NULL), q = BN_bin2bn(dsaq_2048, sizeof(dsaq_2048), NULL), g = BN_bin2bn(dsag_2048, sizeof(dsag_2048), NULL))) { DSA_free(dsa); BN_free(p); BN_free(q); BN_free(g); return NULL; } return dsa; } static int genkeys(void) { if (!TEST_ptr(dsakey = load_dsa_params())) return 0; if (!TEST_int_eq(DSA_generate_key(dsakey), 1)) return 0; return 1; } static int sign_and_verify(int len) { /* * Per FIPS 186-4, the hash is recommended to be the same length as q. * If the hash is longer than q, the leftmost N bits are used; if the hash * is shorter, then we left-pad (see appendix C.2.1). */ size_t sigLength; int digestlen = BN_num_bytes(DSA_get0_q(dsakey)); int ok = 0; unsigned char *dataToSign = OPENSSL_malloc(len); unsigned char *paddedData = OPENSSL_malloc(digestlen); unsigned char *signature = NULL; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; if (!TEST_ptr(dataToSign) || !TEST_ptr(paddedData) || !TEST_int_eq(RAND_bytes(dataToSign, len), 1)) goto end; memset(paddedData, 0, digestlen); if (len > digestlen) memcpy(paddedData, dataToSign, digestlen); else memcpy(paddedData + digestlen - len, dataToSign, len); if (!TEST_ptr(pkey = EVP_PKEY_new())) goto end; EVP_PKEY_set1_DSA(pkey, dsakey); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))) goto end; if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1)) goto end; if (EVP_PKEY_sign(ctx, NULL, &sigLength, dataToSign, len) != 1) { TEST_error("Failed to get signature length, len=%d", len); goto end; } if (!TEST_ptr(signature = OPENSSL_malloc(sigLength))) goto end; if (EVP_PKEY_sign(ctx, signature, &sigLength, dataToSign, len) != 1) { TEST_error("Failed to sign, len=%d", len); goto end; } /* Check that the signature is okay via the EVP interface */ if (!TEST_int_eq(EVP_PKEY_verify_init(ctx), 1)) goto end; /* ... using the same data we just signed */ if (EVP_PKEY_verify(ctx, signature, sigLength, dataToSign, len) != 1) { TEST_error("EVP verify with unpadded length %d failed\n", len); goto end; } /* ... padding/truncating the data to the appropriate digest size */ if (EVP_PKEY_verify(ctx, signature, sigLength, paddedData, digestlen) != 1) { TEST_error("EVP verify with length %d failed\n", len); goto end; } /* Verify again using the raw DSA interface */ if (DSA_verify(0, dataToSign, len, signature, sigLength, dsakey) != 1) { TEST_error("Verification with unpadded data failed, len=%d", len); goto end; } if (DSA_verify(0, paddedData, digestlen, signature, sigLength, dsakey) != 1) { TEST_error("verify with length %d failed\n", len); goto end; } ok = 1; end: EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); OPENSSL_free(signature); OPENSSL_free(paddedData); OPENSSL_free(dataToSign); return ok; } static int dsa_exact_size_test(void) { /* * For a 2048-bit p, q should be either 224 or 256 bits per the table in * FIPS 186-4 4.2. */ return sign_and_verify(224 / 8) && sign_and_verify(256 / 8); } static int dsa_small_digest_test(void) { return sign_and_verify(16) && sign_and_verify(1); } static int dsa_large_digest_test(void) { return sign_and_verify(33) && sign_and_verify(64); } void cleanup_tests(void) { DSA_free(dsakey); } #endif /* OPENSSL_NO_DSA */ int setup_tests(void) { #ifndef OPENSSL_NO_DSA if (!genkeys()) return 0; ADD_TEST(dsa_exact_size_test); ADD_TEST(dsa_small_digest_test); ADD_TEST(dsa_large_digest_test); #endif return 1; }
./openssl/test/rc5test.c
/* * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * RC5 low level APIs are deprecated for public use, but still ok for internal * use. */ #include "internal/deprecated.h" #include <string.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_RC5 # include <openssl/rc5.h> static unsigned char RC5key[5][16] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x91, 0x5f, 0x46, 0x19, 0xbe, 0x41, 0xb2, 0x51, 0x63, 0x55, 0xa5, 0x01, 0x10, 0xa9, 0xce, 0x91}, {0x78, 0x33, 0x48, 0xe7, 0x5a, 0xeb, 0x0f, 0x2f, 0xd7, 0xb1, 0x69, 0xbb, 0x8d, 0xc1, 0x67, 0x87}, {0xdc, 0x49, 0xdb, 0x13, 0x75, 0xa5, 0x58, 0x4f, 0x64, 0x85, 0xb4, 0x13, 0xb5, 0xf1, 0x2b, 0xaf}, {0x52, 0x69, 0xf1, 0x49, 0xd4, 0x1b, 0xa0, 0x15, 0x24, 0x97, 0x57, 0x4d, 0x7f, 0x15, 0x31, 0x25}, }; static unsigned char RC5plain[5][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x21, 0xA5, 0xDB, 0xEE, 0x15, 0x4B, 0x8F, 0x6D}, {0xF7, 0xC0, 0x13, 0xAC, 0x5B, 0x2B, 0x89, 0x52}, {0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92}, {0x65, 0xC1, 0x78, 0xB2, 0x84, 0xD1, 0x97, 0xCC}, }; static unsigned char RC5cipher[5][8] = { {0x21, 0xA5, 0xDB, 0xEE, 0x15, 0x4B, 0x8F, 0x6D}, {0xF7, 0xC0, 0x13, 0xAC, 0x5B, 0x2B, 0x89, 0x52}, {0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92}, {0x65, 0xC1, 0x78, 0xB2, 0x84, 0xD1, 0x97, 0xCC}, {0xEB, 0x44, 0xE4, 0x15, 0xDA, 0x31, 0x98, 0x24}, }; # define RC5_CBC_NUM 27 static unsigned char rc5_cbc_cipher[RC5_CBC_NUM][8] = { {0x7a, 0x7b, 0xba, 0x4d, 0x79, 0x11, 0x1d, 0x1e}, {0x79, 0x7b, 0xba, 0x4d, 0x78, 0x11, 0x1d, 0x1e}, {0x7a, 0x7b, 0xba, 0x4d, 0x79, 0x11, 0x1d, 0x1f}, {0x7a, 0x7b, 0xba, 0x4d, 0x79, 0x11, 0x1d, 0x1f}, {0x8b, 0x9d, 0xed, 0x91, 0xce, 0x77, 0x94, 0xa6}, {0x2f, 0x75, 0x9f, 0xe7, 0xad, 0x86, 0xa3, 0x78}, {0xdc, 0xa2, 0x69, 0x4b, 0xf4, 0x0e, 0x07, 0x88}, {0xdc, 0xa2, 0x69, 0x4b, 0xf4, 0x0e, 0x07, 0x88}, {0xdc, 0xfe, 0x09, 0x85, 0x77, 0xec, 0xa5, 0xff}, {0x96, 0x46, 0xfb, 0x77, 0x63, 0x8f, 0x9c, 0xa8}, {0xb2, 0xb3, 0x20, 0x9d, 0xb6, 0x59, 0x4d, 0xa4}, {0x54, 0x5f, 0x7f, 0x32, 0xa5, 0xfc, 0x38, 0x36}, {0x82, 0x85, 0xe7, 0xc1, 0xb5, 0xbc, 0x74, 0x02}, {0xfc, 0x58, 0x6f, 0x92, 0xf7, 0x08, 0x09, 0x34}, {0xcf, 0x27, 0x0e, 0xf9, 0x71, 0x7f, 0xf7, 0xc4}, {0xe4, 0x93, 0xf1, 0xc1, 0xbb, 0x4d, 0x6e, 0x8c}, {0x5c, 0x4c, 0x04, 0x1e, 0x0f, 0x21, 0x7a, 0xc3}, {0x92, 0x1f, 0x12, 0x48, 0x53, 0x73, 0xb4, 0xf7}, {0x5b, 0xa0, 0xca, 0x6b, 0xbe, 0x7f, 0x5f, 0xad}, {0xc5, 0x33, 0x77, 0x1c, 0xd0, 0x11, 0x0e, 0x63}, {0x29, 0x4d, 0xdb, 0x46, 0xb3, 0x27, 0x8d, 0x60}, {0xda, 0xd6, 0xbd, 0xa9, 0xdf, 0xe8, 0xf7, 0xe8}, {0x97, 0xe0, 0x78, 0x78, 0x37, 0xed, 0x31, 0x7f}, {0x78, 0x75, 0xdb, 0xf6, 0x73, 0x8c, 0x64, 0x78}, {0x8f, 0x34, 0xc3, 0xc6, 0x81, 0xc9, 0x96, 0x95}, {0x7c, 0xb3, 0xf1, 0xdf, 0x34, 0xf9, 0x48, 0x11}, {0x7f, 0xd1, 0xa0, 0x23, 0xa5, 0xbb, 0xa2, 0x17}, }; static unsigned char rc5_cbc_key[RC5_CBC_NUM][17] = { {1, 0x00}, {1, 0x00}, {1, 0x00}, {1, 0x00}, {1, 0x00}, {1, 0x11}, {1, 0x00}, {4, 0x00, 0x00, 0x00, 0x00}, {1, 0x00}, {1, 0x00}, {1, 0x00}, {1, 0x00}, {4, 0x01, 0x02, 0x03, 0x04}, {4, 0x01, 0x02, 0x03, 0x04}, {4, 0x01, 0x02, 0x03, 0x04}, {8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {16, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {16, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {16, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {5, 0x01, 0x02, 0x03, 0x04, 0x05}, {5, 0x01, 0x02, 0x03, 0x04, 0x05}, {5, 0x01, 0x02, 0x03, 0x04, 0x05}, {5, 0x01, 0x02, 0x03, 0x04, 0x05}, {5, 0x01, 0x02, 0x03, 0x04, 0x05}, }; static unsigned char rc5_cbc_plain[RC5_CBC_NUM][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x01}, }; static int rc5_cbc_rounds[RC5_CBC_NUM] = { 0, 0, 0, 0, 0, 1, 2, 2, 8, 8, 12, 16, 8, 12, 16, 12, 8, 12, 16, 8, 12, 16, 12, 8, 8, 8, 8, }; static unsigned char rc5_cbc_iv[RC5_CBC_NUM][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x78, 0x75, 0xdb, 0xf6, 0x73, 0x8c, 0x64, 0x78}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x7c, 0xb3, 0xf1, 0xdf, 0x34, 0xf9, 0x48, 0x11}, }; static int test_rc5_ecb(int n) { int testresult = 1; RC5_32_KEY key; unsigned char buf[8], buf2[8]; if (!TEST_true(RC5_32_set_key(&key, 16, &RC5key[n][0], 12))) return 0; RC5_32_ecb_encrypt(&RC5plain[n][0], buf, &key, RC5_ENCRYPT); if (!TEST_mem_eq(&RC5cipher[n][0], sizeof(RC5cipher[0]), buf, sizeof(buf))) testresult = 0; RC5_32_ecb_encrypt(buf, buf2, &key, RC5_DECRYPT); if (!TEST_mem_eq(&RC5plain[n][0], sizeof(RC5cipher[0]), buf2, sizeof(buf2))) testresult = 0; return testresult; } static int test_rc5_cbc(int n) { int testresult = 1; int i; RC5_32_KEY key; unsigned char buf[8], buf2[8], ivb[8]; i = rc5_cbc_rounds[n]; if (i >= 8) { if (!TEST_true(RC5_32_set_key(&key, rc5_cbc_key[n][0], &rc5_cbc_key[n][1], i))) return 0; memcpy(ivb, &rc5_cbc_iv[n][0], 8); RC5_32_cbc_encrypt(&rc5_cbc_plain[n][0], buf, 8, &key, &ivb[0], RC5_ENCRYPT); if (!TEST_mem_eq(&rc5_cbc_cipher[n][0], sizeof(rc5_cbc_cipher[0]), buf, sizeof(buf))) testresult = 0; memcpy(ivb, &rc5_cbc_iv[n][0], 8); RC5_32_cbc_encrypt(buf, buf2, 8, &key, &ivb[0], RC5_DECRYPT); if (!TEST_mem_eq(&rc5_cbc_plain[n][0], sizeof(rc5_cbc_plain[0]), buf2, sizeof(buf2))) testresult = 0; } return testresult; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_RC5 ADD_ALL_TESTS(test_rc5_ecb, OSSL_NELEM(RC5key)); ADD_ALL_TESTS(test_rc5_cbc, RC5_CBC_NUM); #endif return 1; }
./openssl/test/quic_srt_gen_test.c
/* * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include "internal/quic_srt_gen.h" #include "testutil.h" #include "testutil/output.h" struct test_case { const unsigned char *key; size_t key_len; QUIC_CONN_ID dcid; QUIC_STATELESS_RESET_TOKEN expected; }; static const unsigned char key_1[] = { 0x01, 0x02, 0x03 }; static const unsigned char key_2[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; static const struct test_case tests[] = { { key_1, sizeof(key_1), { 2, { 0x55, 0x66 } }, {{ 0x02,0x9e,0x8f,0x3d,0x1e,0xa9,0x06,0x23,0xb2,0x43,0xd2,0x19,0x59,0x8a,0xa1,0x66 }} }, { key_2, sizeof(key_2), { 0, { 0 } }, {{ 0x93,0x10,0x2f,0xc7,0xaf,0x9d,0x9b,0x28,0x3f,0x84,0x95,0x6b,0xa3,0xdc,0x07,0x6b }} }, { key_2, sizeof(key_2), { 20, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }, {{ 0x9a,0x98,0x98,0x61,0xbe,0xfd,0xe3,0x05,0x45,0xac,0x66,0xcf,0x3b,0x58,0xfb,0xab }} } }; static int test_srt_gen(int idx) { int testresult = 0; const struct test_case *t = &tests[idx]; QUIC_SRT_GEN *srt_gen = NULL; QUIC_STATELESS_RESET_TOKEN token; size_t i; if (!TEST_ptr(srt_gen = ossl_quic_srt_gen_new(NULL, NULL, t->key, t->key_len))) goto err; for (i = 0; i < 2; ++i) { memset(&token, 0xff, sizeof(token)); if (!TEST_true(ossl_quic_srt_gen_calculate_token(srt_gen, &t->dcid, &token))) goto err; if (!TEST_mem_eq(token.token, sizeof(token.token), &t->expected, sizeof(t->expected))) goto err; } testresult = 1; err: ossl_quic_srt_gen_free(srt_gen); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_srt_gen, OSSL_NELEM(tests)); return 1; }
./openssl/test/bio_callback_test.c
/* * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <string.h> #include <openssl/bio.h> #include "testutil.h" #define MAXCOUNT 5 static int my_param_count; static BIO *my_param_b[MAXCOUNT]; static int my_param_oper[MAXCOUNT]; static const char *my_param_argp[MAXCOUNT]; static int my_param_argi[MAXCOUNT]; static long my_param_argl[MAXCOUNT]; static long my_param_ret[MAXCOUNT]; static size_t my_param_len[MAXCOUNT]; static size_t my_param_processed[MAXCOUNT]; static long my_bio_cb_ex(BIO *b, int oper, const char *argp, size_t len, int argi, long argl, int ret, size_t *processed) { if (my_param_count >= MAXCOUNT) return -1; my_param_b[my_param_count] = b; my_param_oper[my_param_count] = oper; my_param_argp[my_param_count] = argp; my_param_argi[my_param_count] = argi; my_param_argl[my_param_count] = argl; my_param_ret[my_param_count] = ret; my_param_len[my_param_count] = len; my_param_processed[my_param_count] = processed != NULL ? *processed : 0; my_param_count++; return ret; } static int test_bio_callback_ex(void) { int ok = 0; BIO *bio; int i; char test1[] = "test"; const size_t test1len = sizeof(test1) - 1; char test2[] = "hello"; const size_t test2len = sizeof(test2) - 1; char buf[16]; my_param_count = 0; bio = BIO_new(BIO_s_mem()); if (bio == NULL) goto err; BIO_set_callback_ex(bio, my_bio_cb_ex); i = BIO_write(bio, test1, test1len); if (!TEST_int_eq(i, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE) || !TEST_ptr_eq(my_param_argp[0], test1) || !TEST_size_t_eq(my_param_len[0], test1len) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test1) || !TEST_size_t_eq(my_param_len[1], test1len) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test1len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_mem_eq(buf, i, test1, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test1len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; /* By default a mem bio returns -1 if it has run out of data */ my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, -1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], 0) || !TEST_int_eq((int)my_param_ret[1], -1)) goto err; /* Force the mem bio to return 0 if it has run out of data */ my_param_count = 0; i = BIO_set_mem_eof_return(bio, 0); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_CTRL) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], BIO_C_SET_BUF_MEM_EOF_RETURN) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_CTRL | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], NULL) || !TEST_int_eq(my_param_argi[1], BIO_C_SET_BUF_MEM_EOF_RETURN) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, 0) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], 0) || !TEST_int_eq((int)my_param_ret[1], 0)) goto err; my_param_count = 0; i = BIO_puts(bio, test2); if (!TEST_int_eq(i, 5) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS) || !TEST_ptr_eq(my_param_argp[0], test2) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test2) || !TEST_int_eq(my_param_argi[1], 0) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test2len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_free(bio); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 1) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1)) goto finish; ok = 1; goto finish; err: BIO_free(bio); finish: /* This helps finding memory leaks with ASAN */ memset(my_param_b, 0, sizeof(my_param_b)); memset(my_param_argp, 0, sizeof(my_param_argp)); return ok; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static long my_bio_callback(BIO *b, int oper, const char *argp, int argi, long argl, long ret) { if (my_param_count >= MAXCOUNT) return -1; my_param_b[my_param_count] = b; my_param_oper[my_param_count] = oper; my_param_argp[my_param_count] = argp; my_param_argi[my_param_count] = argi; my_param_argl[my_param_count] = argl; my_param_ret[my_param_count] = ret; my_param_count++; return ret; } static int test_bio_callback(void) { int ok = 0; BIO *bio; int i; char test1[] = "test"; const int test1len = sizeof(test1) - 1; char test2[] = "hello"; const int test2len = sizeof(test2) - 1; char buf[16]; my_param_count = 0; bio = BIO_new(BIO_s_mem()); if (bio == NULL) goto err; BIO_set_callback(bio, my_bio_callback); i = BIO_write(bio, test1, test1len); if (!TEST_int_eq(i, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE) || !TEST_ptr_eq(my_param_argp[0], test1) || !TEST_int_eq(my_param_argi[0], test1len) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test1) || !TEST_int_eq(my_param_argi[1], test1len) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test1len)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_mem_eq(buf, i, test1, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test1len)) goto err; /* By default a mem bio returns -1 if it has run out of data */ my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, -1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], -1L)) goto err; /* Force the mem bio to return 0 if it has run out of data */ BIO_set_mem_eof_return(bio, 0); my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, 0) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], 0L)) goto err; my_param_count = 0; i = BIO_puts(bio, test2); if (!TEST_int_eq(i, 5) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS) || !TEST_ptr_eq(my_param_argp[0], test2) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test2) || !TEST_int_eq(my_param_argi[1], 0) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test2len)) goto err; my_param_count = 0; i = BIO_free(bio); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 1) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L)) goto finish; ok = 1; goto finish; err: BIO_free(bio); finish: /* This helps finding memory leaks with ASAN */ memset(my_param_b, 0, sizeof(my_param_b)); memset(my_param_argp, 0, sizeof(my_param_argp)); return ok; } #endif int setup_tests(void) { ADD_TEST(test_bio_callback_ex); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_bio_callback); #endif return 1; }
./openssl/test/cert_comp_test.c
/* * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * We need access to the deprecated low level HMAC APIs for legacy purposes * when the deprecated calls are not hidden */ #ifndef OPENSSL_NO_DEPRECATED_3_0 # define OPENSSL_SUPPRESS_DEPRECATED #endif #include <openssl/ssl.h> #include "internal/nelem.h" #include "helpers/ssltestlib.h" #include "testutil.h" #include "../ssl/ssl_local.h" #undef OSSL_NO_USABLE_TLS1_3 #if defined(OPENSSL_NO_TLS1_3) \ || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)) /* * If we don't have ec or dh then there are no built-in groups that are usable * with TLSv1.3 */ # define OSSL_NO_USABLE_TLS1_3 #endif #if !defined(OSSL_NO_USEABLE_TLS1_3) static char *certsdir = NULL; static char *cert = NULL; static char *privkey = NULL; static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) { X509 *xcert; EVP_PKEY *privpkey; BIO *in = NULL; BIO *priv_in = NULL; /* Check that SSL_get0_peer_certificate() returns something sensible */ if (!TEST_ptr(SSL_get0_peer_certificate(ssl))) return 0; in = BIO_new_file(cert, "r"); if (!TEST_ptr(in)) return 0; if (!TEST_ptr(xcert = X509_new_ex(NULL, NULL)) || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL)) || !TEST_ptr(priv_in = BIO_new_file(privkey, "r")) || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL, NULL, NULL, NULL, NULL))) goto err; *x509 = xcert; *pkey = privpkey; BIO_free(in); BIO_free(priv_in); return 1; err: X509_free(xcert); BIO_free(in); BIO_free(priv_in); return 0; } static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) { return 1; } /* * Test 0 = app pre-compresses certificate in SSL * Test 1 = app pre-compresses certificate in SSL_CTX * Test 2 = app pre-compresses certificate in SSL_CTX, client authentication * Test 3 = app pre-compresses certificate in SSL_CTX, but it's unused due to prefs */ /* Compression helper */ static int ssl_comp_cert(SSL *ssl, int alg) { unsigned char *comp_data = NULL; size_t comp_len = 0; size_t orig_len = 0; int retval = 0; if (!TEST_size_t_gt(comp_len = SSL_get1_compressed_cert(ssl, alg, &comp_data, &orig_len), 0)) goto err; if (!TEST_true(SSL_set1_compressed_cert(ssl, alg, comp_data, comp_len, orig_len))) goto err; retval = alg; err: OPENSSL_free(comp_data); return retval; } static void cert_comp_info_cb(const SSL *s, int where, int ret) { int *seen = (int*)SSL_get_app_data(s); if (SSL_is_server(s)) { /* TLS_ST_SR_COMP_CERT */ if (!strcmp(SSL_state_string(s), "TRCCC") && seen != NULL) *seen = 1; } else { /* TLS_ST_CR_COMP_CERT */ if (!strcmp(SSL_state_string(s), "TRSCC") && seen != NULL) *seen = 1; } } static int test_ssl_cert_comp(int test) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; int expected_client = TLSEXT_comp_cert_none; int expected_server = TLSEXT_comp_cert_none; int client_seen = 0; int server_seen = 0; /* reverse default order */ int server_pref[] = { TLSEXT_comp_cert_zstd, TLSEXT_comp_cert_zlib, TLSEXT_comp_cert_brotli }; /* default order */ int client_pref[] = { TLSEXT_comp_cert_brotli, TLSEXT_comp_cert_zlib, TLSEXT_comp_cert_zstd }; /* one of these *must* be defined! */ #ifndef OPENSSL_NO_BROTLI expected_server = TLSEXT_comp_cert_brotli; expected_client = TLSEXT_comp_cert_brotli; #endif #ifndef OPENSSL_NO_ZLIB expected_server = TLSEXT_comp_cert_zlib; if (expected_client == TLSEXT_comp_cert_none) expected_client = TLSEXT_comp_cert_zlib; #endif #ifndef OPENSSL_NO_ZSTD expected_server = TLSEXT_comp_cert_zstd; if (expected_client == TLSEXT_comp_cert_none) expected_client = TLSEXT_comp_cert_zstd; #endif /* * If there's only one comp algorithm, pref won't do much * Coverity can get confused in this case, and consider test == 3 * to be DEADCODE */ if (test == 3 && expected_client == expected_server) { TEST_info("Only one compression algorithm configured"); return 1; } if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (test == 3) { /* coverity[deadcode] */ server_pref[0] = expected_server; server_pref[1] = expected_client; if (!TEST_true(SSL_CTX_set1_cert_comp_preference(sctx, server_pref, 2))) goto end; client_pref[0] = expected_client; if (!TEST_true(SSL_CTX_set1_cert_comp_preference(cctx, client_pref, 1))) goto end; } else { if (!TEST_true(SSL_CTX_set1_cert_comp_preference(sctx, server_pref, OSSL_NELEM(server_pref)))) goto end; if (!TEST_true(SSL_CTX_set1_cert_comp_preference(cctx, client_pref, OSSL_NELEM(client_pref)))) goto end; } if (test == 2) { /* Use callbacks from test_client_cert_cb() */ SSL_CTX_set_client_cert_cb(cctx, client_cert_cb); SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb); } if (test == 1 || test== 2 || test == 3) { if (!TEST_true(SSL_CTX_compress_certs(sctx, expected_server))) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set_app_data(clientssl, &client_seen))) goto end; if (!TEST_true(SSL_set_app_data(serverssl, &server_seen))) goto end; SSL_set_info_callback(clientssl, cert_comp_info_cb); SSL_set_info_callback(serverssl, cert_comp_info_cb); if (test == 0) { if (!TEST_int_eq(ssl_comp_cert(serverssl, expected_server), expected_server)) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (test == 3) { /* coverity[deadcode] */ SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(serverssl); /* expect that the pre-compressed cert won't be used */ if (!TEST_int_eq(sc->cert->key->cert_comp_used, 0)) goto end; if (!TEST_false(*(int*)SSL_get_app_data(clientssl))) goto end; } else { SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(serverssl); if (!TEST_int_gt(sc->cert->key->cert_comp_used, 0)) goto end; if (!TEST_true(*(int*)SSL_get_app_data(clientssl))) goto end; } if (test == 2) { /* Only for client auth */ if (!TEST_true(*(int*)SSL_get_app_data(serverssl))) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif OPT_TEST_DECLARE_USAGE("certdir\n") int setup_tests(void) { #if !defined(OSSL_NO_USEABLE_TLS1_3) if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certsdir = test_get_argument(0))) return 0; cert = test_mk_file_path(certsdir, "servercert.pem"); if (cert == NULL) goto err; privkey = test_mk_file_path(certsdir, "serverkey.pem"); if (privkey == NULL) goto err; ADD_ALL_TESTS(test_ssl_cert_comp, 4); return 1; err: OPENSSL_free(cert); OPENSSL_free(privkey); return 0; #else return 1; #endif } void cleanup_tests(void) { #if !defined(OSSL_NO_USEABLE_TLS1_3) OPENSSL_free(cert); OPENSSL_free(privkey); #endif }
./openssl/test/bio_addr_test.c
/* * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/bio.h> #include "internal/e_os.h" #include "internal/sockets.h" #include "testutil.h" static int families[] = { AF_INET, #if OPENSSL_USE_IPV6 AF_INET6, #endif #ifndef OPENSSL_NO_UNIX_SOCK AF_UNIX #endif }; static BIO_ADDR *make_dummy_addr(int family) { BIO_ADDR *addr; union { struct sockaddr_in sin; #if OPENSSL_USE_IPV6 struct sockaddr_in6 sin6; #endif #ifndef OPENSSL_NO_UNIX_SOCK struct sockaddr_un sunaddr; #endif } sa; void *where; size_t wherelen; /* Fill with a dummy address */ switch(family) { case AF_INET: where = &(sa.sin.sin_addr); wherelen = sizeof(sa.sin.sin_addr); break; #if OPENSSL_USE_IPV6 case AF_INET6: where = &(sa.sin6.sin6_addr); wherelen = sizeof(sa.sin6.sin6_addr); break; #endif #ifndef OPENSSL_NO_UNIX_SOCK case AF_UNIX: where = &(sa.sunaddr.sun_path); /* BIO_ADDR_rawmake needs an extra byte for a NUL-terminator*/ wherelen = sizeof(sa.sunaddr.sun_path) - 1; break; #endif default: TEST_error("Unsupported address family"); return 0; } /* * Could be any data, but we make it printable because BIO_ADDR_rawmake * expects the AF_UNIX address to be a string. */ memset(where, 'a', wherelen); addr = BIO_ADDR_new(); if (!TEST_ptr(addr)) return NULL; if (!TEST_true(BIO_ADDR_rawmake(addr, family, where, wherelen, 1000))) { BIO_ADDR_free(addr); return NULL; } return addr; } static int bio_addr_is_eq(const BIO_ADDR *a, const BIO_ADDR *b) { unsigned char *adata = NULL, *bdata = NULL; size_t alen, blen; int ret = 0; /* True even if a and b are NULL */ if (a == b) return 1; /* If one is NULL the other cannot be due to the test above */ if (a == NULL || b == NULL) return 0; if (BIO_ADDR_family(a) != BIO_ADDR_family(b)) return 0; /* Works even with AF_UNIX/AF_UNSPEC which just returns 0 */ if (BIO_ADDR_rawport(a) != BIO_ADDR_rawport(b)) return 0; if (!BIO_ADDR_rawaddress(a, NULL, &alen)) return 0; if (!BIO_ADDR_rawaddress(b, NULL, &blen)) goto err; if (alen != blen) return 0; if (alen == 0) return 1; adata = OPENSSL_malloc(alen); if (!TEST_ptr(adata) || !BIO_ADDR_rawaddress(a, adata, &alen)) goto err; bdata = OPENSSL_malloc(blen); if (!TEST_ptr(bdata) || !BIO_ADDR_rawaddress(b, bdata, &blen)) goto err; ret = (memcmp(adata, bdata, alen) == 0); err: OPENSSL_free(adata); OPENSSL_free(bdata); return ret; } static int test_bio_addr_copy_dup(int idx) { BIO_ADDR *src = NULL, *dst = NULL; int ret = 0; int docopy = idx & 1; idx >>= 1; src = make_dummy_addr(families[idx]); if (!TEST_ptr(src)) return 0; if (docopy) { dst = BIO_ADDR_new(); if (!TEST_ptr(dst)) goto err; if (!TEST_true(BIO_ADDR_copy(dst, src))) goto err; } else { dst = BIO_ADDR_dup(src); if (!TEST_ptr(dst)) goto err; } if (!TEST_true(bio_addr_is_eq(src, dst))) goto err; ret = 1; err: BIO_ADDR_free(src); BIO_ADDR_free(dst); return ret; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } ADD_ALL_TESTS(test_bio_addr_copy_dup, OSSL_NELEM(families) * 2); return 1; }
./openssl/test/evp_test.c
/* * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/err.h> #include <openssl/provider.h> #include <openssl/x509v3.h> #include <openssl/pkcs12.h> #include <openssl/kdf.h> #include <openssl/params.h> #include <openssl/core_names.h> #include <openssl/fips_names.h> #include <openssl/thread.h> #include "internal/numbers.h" #include "internal/nelem.h" #include "crypto/evp.h" #include "testutil.h" typedef struct evp_test_buffer_st EVP_TEST_BUFFER; DEFINE_STACK_OF(EVP_TEST_BUFFER) #define AAD_NUM 4 typedef struct evp_test_method_st EVP_TEST_METHOD; /* Structure holding test information */ typedef struct evp_test_st { STANZA s; /* Common test stanza */ char *name; int skip; /* Current test should be skipped */ const EVP_TEST_METHOD *meth; /* method for this test */ const char *err, *aux_err; /* Error string for test */ char *expected_err; /* Expected error value of test */ char *reason; /* Expected error reason string */ void *data; /* test specific data */ } EVP_TEST; /* Test method structure */ struct evp_test_method_st { /* Name of test as it appears in file */ const char *name; /* Initialise test for "alg" */ int (*init) (EVP_TEST *t, const char *alg); /* Clean up method */ void (*cleanup) (EVP_TEST *t); /* Test specific name value pair processing */ int (*parse) (EVP_TEST *t, const char *name, const char *value); /* Run the test itself */ int (*run_test) (EVP_TEST *t); }; /* Linked list of named keys. */ typedef struct key_list_st { char *name; EVP_PKEY *key; struct key_list_st *next; } KEY_LIST; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_IN_PLACE, OPT_PROVIDER_NAME, OPT_PROV_PROPQUERY, OPT_TEST_ENUM } OPTION_CHOICE; static OSSL_PROVIDER *prov_null = NULL; static OSSL_PROVIDER *libprov = NULL; static OSSL_LIB_CTX *libctx = NULL; /* List of public and private keys */ static KEY_LIST *private_keys; static KEY_LIST *public_keys; static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst); static int parse_bin(const char *value, unsigned char **buf, size_t *buflen); static int is_digest_disabled(const char *name); static int is_pkey_disabled(const char *name); static int is_mac_disabled(const char *name); static int is_cipher_disabled(const char *name); static int is_kdf_disabled(const char *name); /* * Compare two memory regions for equality, returning zero if they differ. * However, if there is expected to be an error and the actual error * matches then the memory is expected to be different so handle this * case without producing unnecessary test framework output. */ static int memory_err_compare(EVP_TEST *t, const char *err, const void *expected, size_t expected_len, const void *got, size_t got_len) { int r; if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0) r = !TEST_mem_ne(expected, expected_len, got, got_len); else r = TEST_mem_eq(expected, expected_len, got, got_len); if (!r) t->err = err; return r; } /* Option specific for evp test */ static int process_mode_in_place; static const char *propquery = NULL; static int evp_test_process_mode(char *mode) { if (strcmp(mode, "in_place") == 0) return 1; else if (strcmp(mode, "both") == 0) return 0; return -1; } /* * Structure used to hold a list of blocks of memory to test * calls to "update" like functions. */ struct evp_test_buffer_st { unsigned char *buf; size_t buflen; size_t count; int count_set; }; static void evp_test_buffer_free(EVP_TEST_BUFFER *db) { if (db != NULL) { OPENSSL_free(db->buf); OPENSSL_free(db); } } /* append buffer to a list */ static int evp_test_buffer_append(const char *value, STACK_OF(EVP_TEST_BUFFER) **sk) { EVP_TEST_BUFFER *db = NULL; if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db)))) goto err; if (!parse_bin(value, &db->buf, &db->buflen)) goto err; db->count = 1; db->count_set = 0; if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null())) goto err; if (!sk_EVP_TEST_BUFFER_push(*sk, db)) goto err; return 1; err: evp_test_buffer_free(db); return 0; } /* replace last buffer in list with copies of itself */ static int evp_test_buffer_ncopy(const char *value, STACK_OF(EVP_TEST_BUFFER) *sk) { EVP_TEST_BUFFER *db; unsigned char *tbuf, *p; size_t tbuflen; int ncopy = atoi(value); int i; if (ncopy <= 0) return 0; if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) return 0; db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); tbuflen = db->buflen * ncopy; if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen))) return 0; for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen) memcpy(p, db->buf, db->buflen); OPENSSL_free(db->buf); db->buf = tbuf; db->buflen = tbuflen; return 1; } /* set repeat count for last buffer in list */ static int evp_test_buffer_set_count(const char *value, STACK_OF(EVP_TEST_BUFFER) *sk) { EVP_TEST_BUFFER *db; int count = atoi(value); if (count <= 0) return 0; if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) return 0; db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); if (db->count_set != 0) return 0; db->count = (size_t)count; db->count_set = 1; return 1; } /* call "fn" with each element of the list in turn */ static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk, int (*fn)(void *ctx, const unsigned char *buf, size_t buflen), void *ctx) { int i; for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) { EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i); size_t j; for (j = 0; j < tb->count; j++) { if (fn(ctx, tb->buf, tb->buflen) <= 0) return 0; } } return 1; } /* * Unescape some sequences in string literals (only \n for now). * Return an allocated buffer, set |out_len|. If |input_len| * is zero, get an empty buffer but set length to zero. */ static unsigned char* unescape(const char *input, size_t input_len, size_t *out_len) { unsigned char *ret, *p; size_t i; if (input_len == 0) { *out_len = 0; return OPENSSL_zalloc(1); } /* Escaping is non-expanding; over-allocate original size for simplicity. */ if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len))) return NULL; for (i = 0; i < input_len; i++) { if (*input == '\\') { if (i == input_len - 1 || *++input != 'n') { TEST_error("Bad escape sequence in file"); goto err; } *p++ = '\n'; i++; input++; } else { *p++ = *input++; } } *out_len = p - ret; return ret; err: OPENSSL_free(ret); return NULL; } /* * For a hex string "value" convert to a binary allocated buffer. * Return 1 on success or 0 on failure. */ static int parse_bin(const char *value, unsigned char **buf, size_t *buflen) { long len; /* Check for NULL literal */ if (strcmp(value, "NULL") == 0) { *buf = NULL; *buflen = 0; return 1; } /* Check for empty value */ if (*value == '\0') { /* * Don't return NULL for zero length buffer. This is needed for * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key * buffer even if the key length is 0, in order to detect key reset. */ *buf = OPENSSL_malloc(1); if (*buf == NULL) return 0; **buf = 0; *buflen = 0; return 1; } /* Check for string literal */ if (value[0] == '"') { size_t vlen = strlen(++value); if (vlen == 0 || value[vlen - 1] != '"') return 0; vlen--; *buf = unescape(value, vlen, buflen); return *buf == NULL ? 0 : 1; } /* Otherwise assume as hex literal and convert it to binary buffer */ if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) { TEST_info("Can't convert %s", value); TEST_openssl_errors(); return -1; } /* Size of input buffer means we'll never overflow */ *buflen = len; return 1; } /** ** MESSAGE DIGEST TESTS **/ typedef struct digest_data_st { /* Digest this test is for */ const EVP_MD *digest; EVP_MD *fetched_digest; /* Input to digest */ STACK_OF(EVP_TEST_BUFFER) *input; /* Expected output */ unsigned char *output; size_t output_len; /* Padding type */ int pad_type; /* XOF mode? */ int xof; /* Size for variable output length but non-XOF */ size_t digest_size; } DIGEST_DATA; static int digest_test_init(EVP_TEST *t, const char *alg) { DIGEST_DATA *mdat; const EVP_MD *digest; EVP_MD *fetched_digest; if (is_digest_disabled(alg)) { TEST_info("skipping, '%s' is disabled", alg); t->skip = 1; return 1; } if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, propquery)) == NULL && (digest = EVP_get_digestbyname(alg)) == NULL) return 0; if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) return 0; t->data = mdat; mdat->digest = digest; mdat->fetched_digest = fetched_digest; mdat->pad_type = 0; mdat->xof = 0; if (fetched_digest != NULL) TEST_info("%s is fetched", alg); return 1; } static void digest_test_cleanup(EVP_TEST *t) { DIGEST_DATA *mdat = t->data; sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free); OPENSSL_free(mdat->output); EVP_MD_free(mdat->fetched_digest); } static int digest_test_parse(EVP_TEST *t, const char *keyword, const char *value) { DIGEST_DATA *mdata = t->data; if (strcmp(keyword, "Input") == 0) return evp_test_buffer_append(value, &mdata->input); if (strcmp(keyword, "Output") == 0) return parse_bin(value, &mdata->output, &mdata->output_len); if (strcmp(keyword, "Count") == 0) return evp_test_buffer_set_count(value, mdata->input); if (strcmp(keyword, "Ncopy") == 0) return evp_test_buffer_ncopy(value, mdata->input); if (strcmp(keyword, "Padding") == 0) return (mdata->pad_type = atoi(value)) > 0; if (strcmp(keyword, "XOF") == 0) return (mdata->xof = atoi(value)) > 0; if (strcmp(keyword, "OutputSize") == 0) { int sz; sz = atoi(value); if (sz < 0) return -1; mdata->digest_size = sz; return 1; } return 0; } static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen) { return EVP_DigestUpdate(ctx, buf, buflen); } static int test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx) { char dont[] = "touch"; if (!TEST_ptr(mctx)) return 0; if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) { EVP_MD_CTX_free(mctx); t->err = "DIGESTFINALXOF_ERROR"; return 0; } if (!TEST_str_eq(dont, "touch")) { EVP_MD_CTX_free(mctx); t->err = "DIGESTFINALXOF_ERROR"; return 0; } EVP_MD_CTX_free(mctx); return 1; } static int digest_test_run(EVP_TEST *t) { DIGEST_DATA *expected = t->data; EVP_TEST_BUFFER *inbuf; EVP_MD_CTX *mctx; unsigned char *got = NULL; unsigned int got_len; size_t size = 0; int xof = 0; OSSL_PARAM params[4], *p = &params[0]; t->err = "TEST_FAILURE"; if (!TEST_ptr(mctx = EVP_MD_CTX_new())) goto err; got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ? expected->output_len : EVP_MAX_MD_SIZE); if (!TEST_ptr(got)) goto err; if (expected->xof > 0) { xof |= 1; *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &expected->output_len); } if (expected->digest_size > 0) { *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &expected->digest_size); } if (expected->pad_type > 0) *p++ = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE, &expected->pad_type); *p++ = OSSL_PARAM_construct_end(); if (!EVP_DigestInit_ex2(mctx, expected->digest, params)) { t->err = "DIGESTINIT_ERROR"; goto err; } if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) { t->err = "DIGESTUPDATE_ERROR"; goto err; } xof |= (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0; if (xof) { EVP_MD_CTX *mctx_cpy; if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) { goto err; } if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) { EVP_MD_CTX_free(mctx_cpy); goto err; } else if (!test_duplicate_md_ctx(t, mctx_cpy)) { goto err; } if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx))) goto err; got_len = expected->output_len; if (!EVP_DigestFinalXOF(mctx, got, got_len)) { t->err = "DIGESTFINALXOF_ERROR"; goto err; } } else { if (!EVP_DigestFinal(mctx, got, &got_len)) { t->err = "DIGESTFINAL_ERROR"; goto err; } } if (!TEST_int_eq(expected->output_len, got_len)) { t->err = "DIGEST_LENGTH_MISMATCH"; goto err; } if (!memory_err_compare(t, "DIGEST_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; /* Test the EVP_Q_digest interface as well */ if (sk_EVP_TEST_BUFFER_num(expected->input) == 1 && !xof /* This should never fail but we need the returned pointer now */ && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0)) && !inbuf->count_set) { OPENSSL_cleanse(got, got_len); if (!TEST_true(EVP_Q_digest(libctx, EVP_MD_get0_name(expected->fetched_digest), NULL, inbuf->buf, inbuf->buflen, got, &size)) || !TEST_mem_eq(got, size, expected->output, expected->output_len)) { t->err = "EVP_Q_digest failed"; goto err; } } err: OPENSSL_free(got); EVP_MD_CTX_free(mctx); return 1; } static const EVP_TEST_METHOD digest_test_method = { "Digest", digest_test_init, digest_test_cleanup, digest_test_parse, digest_test_run }; /** *** CIPHER TESTS **/ typedef struct cipher_data_st { const EVP_CIPHER *cipher; EVP_CIPHER *fetched_cipher; int enc; /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */ int aead; unsigned char *key; size_t key_len; size_t key_bits; /* Used by RC2 */ unsigned char *iv; unsigned char *next_iv; /* Expected IV state after operation */ unsigned int rounds; size_t iv_len; unsigned char *plaintext; size_t plaintext_len; unsigned char *ciphertext; size_t ciphertext_len; /* AEAD ciphers only */ unsigned char *aad[AAD_NUM]; size_t aad_len[AAD_NUM]; int tls_aad; int tls_version; unsigned char *tag; const char *cts_mode; size_t tag_len; int tag_late; unsigned char *mac_key; size_t mac_key_len; const char *xts_standard; } CIPHER_DATA; static int cipher_test_init(EVP_TEST *t, const char *alg) { const EVP_CIPHER *cipher; EVP_CIPHER *fetched_cipher; CIPHER_DATA *cdat; int m; if (is_cipher_disabled(alg)) { t->skip = 1; TEST_info("skipping, '%s' is disabled", alg); return 1; } ERR_set_mark(); if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, propquery)) == NULL && (cipher = EVP_get_cipherbyname(alg)) == NULL) { /* a stitched cipher might not be available */ if (strstr(alg, "HMAC") != NULL) { ERR_pop_to_mark(); t->skip = 1; TEST_info("skipping, '%s' is not available", alg); return 1; } ERR_clear_last_mark(); return 0; } ERR_clear_last_mark(); if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat)))) return 0; cdat->cipher = cipher; cdat->fetched_cipher = fetched_cipher; cdat->enc = -1; m = EVP_CIPHER_get_mode(cipher); if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) cdat->aead = m != 0 ? m : -1; else cdat->aead = 0; t->data = cdat; if (fetched_cipher != NULL) TEST_info("%s is fetched", alg); return 1; } static void cipher_test_cleanup(EVP_TEST *t) { int i; CIPHER_DATA *cdat = t->data; OPENSSL_free(cdat->key); OPENSSL_free(cdat->iv); OPENSSL_free(cdat->next_iv); OPENSSL_free(cdat->ciphertext); OPENSSL_free(cdat->plaintext); for (i = 0; i < AAD_NUM; i++) OPENSSL_free(cdat->aad[i]); OPENSSL_free(cdat->tag); OPENSSL_free(cdat->mac_key); EVP_CIPHER_free(cdat->fetched_cipher); } static int cipher_test_parse(EVP_TEST *t, const char *keyword, const char *value) { CIPHER_DATA *cdat = t->data; int i; if (strcmp(keyword, "Key") == 0) return parse_bin(value, &cdat->key, &cdat->key_len); if (strcmp(keyword, "Rounds") == 0) { i = atoi(value); if (i < 0) return -1; cdat->rounds = (unsigned int)i; return 1; } if (strcmp(keyword, "IV") == 0) return parse_bin(value, &cdat->iv, &cdat->iv_len); if (strcmp(keyword, "NextIV") == 0) return parse_bin(value, &cdat->next_iv, &cdat->iv_len); if (strcmp(keyword, "Plaintext") == 0) return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len); if (strcmp(keyword, "Ciphertext") == 0) return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len); if (strcmp(keyword, "KeyBits") == 0) { i = atoi(value); if (i < 0) return -1; cdat->key_bits = (size_t)i; return 1; } if (cdat->aead) { int tls_aad = 0; if (strcmp(keyword, "TLSAAD") == 0) cdat->tls_aad = tls_aad = 1; if (strcmp(keyword, "AAD") == 0 || tls_aad) { for (i = 0; i < AAD_NUM; i++) { if (cdat->aad[i] == NULL) return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]); } return -1; } if (strcmp(keyword, "Tag") == 0) return parse_bin(value, &cdat->tag, &cdat->tag_len); if (strcmp(keyword, "SetTagLate") == 0) { if (strcmp(value, "TRUE") == 0) cdat->tag_late = 1; else if (strcmp(value, "FALSE") == 0) cdat->tag_late = 0; else return -1; return 1; } if (strcmp(keyword, "MACKey") == 0) return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len); if (strcmp(keyword, "TLSVersion") == 0) { char *endptr; cdat->tls_version = (int)strtol(value, &endptr, 0); return value[0] != '\0' && endptr[0] == '\0'; } } if (strcmp(keyword, "Operation") == 0) { if (strcmp(value, "ENCRYPT") == 0) cdat->enc = 1; else if (strcmp(value, "DECRYPT") == 0) cdat->enc = 0; else return -1; return 1; } if (strcmp(keyword, "CTSMode") == 0) { cdat->cts_mode = value; return 1; } if (strcmp(keyword, "XTSStandard") == 0) { cdat->xts_standard = value; return 1; } return 0; } static int cipher_test_enc(EVP_TEST *t, int enc, size_t out_misalign, size_t inp_misalign, int frag, int in_place) { CIPHER_DATA *expected = t->data; unsigned char *in, *expected_out, *tmp = NULL; size_t in_len, out_len, donelen = 0; int ok = 0, tmplen, chunklen, tmpflen, i; EVP_CIPHER_CTX *ctx_base = NULL; EVP_CIPHER_CTX *ctx = NULL, *duped; int fips_dupctx_supported = fips_provider_version_ge(libctx, 3, 2, 0); t->err = "TEST_FAILURE"; if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new())) goto err; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); if (enc) { in = expected->plaintext; in_len = expected->plaintext_len; expected_out = expected->ciphertext; out_len = expected->ciphertext_len; } else { in = expected->ciphertext; in_len = expected->ciphertext_len; expected_out = expected->plaintext; out_len = expected->plaintext_len; } if (in_place == 1) { /* Exercise in-place encryption */ tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH); if (!tmp) goto err; in = memcpy(tmp + out_misalign, in, in_len); } else { inp_misalign += 16 - ((out_misalign + in_len) & 15); /* * 'tmp' will store both output and copy of input. We make the copy * of input to specifically aligned part of 'tmp'. So we just * figured out how much padding would ensure the required alignment, * now we allocate extended buffer and finally copy the input just * past inp_misalign in expression below. Output will be written * past out_misalign... */ tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + inp_misalign + in_len); if (!tmp) goto err; in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + inp_misalign, in, in_len); } if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) { t->err = "CIPHERINIT_ERROR"; goto err; } if (expected->cts_mode != NULL) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE, (char *)expected->cts_mode, 0); params[1] = OSSL_PARAM_construct_end(); if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) { t->err = "INVALID_CTS_MODE"; goto err; } } if (expected->iv) { if (expected->aead) { if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN, expected->iv_len, 0) <= 0) { t->err = "INVALID_IV_LENGTH"; goto err; } } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) { t->err = "INVALID_IV_LENGTH"; goto err; } } if (expected->aead && !expected->tls_aad) { unsigned char *tag; /* * If encrypting or OCB just set tag length initially, otherwise * set tag length and value. */ if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) { t->err = "TAG_LENGTH_SET_ERROR"; tag = NULL; } else { t->err = "TAG_SET_ERROR"; tag = expected->tag; } if (tag || expected->aead != EVP_CIPH_GCM_MODE) { if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG, expected->tag_len, tag) <= 0) goto err; } } if (expected->rounds > 0) { int rounds = (int)expected->rounds; if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) { t->err = "INVALID_ROUNDS"; goto err; } } if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) { t->err = "INVALID_KEY_LENGTH"; goto err; } if (expected->key_bits > 0) { int bits = (int)expected->key_bits; if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) { t->err = "INVALID KEY BITS"; goto err; } } if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) { t->err = "KEY_SET_ERROR"; goto err; } /* Check that we get the same IV back */ if (expected->iv != NULL) { /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */ unsigned char iv[128]; if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv))) || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0 && !TEST_mem_eq(expected->iv, expected->iv_len, iv, expected->iv_len))) { t->err = "INVALID_IV"; goto err; } } /* Test that the cipher dup functions correctly if it is supported */ ERR_set_mark(); if (!EVP_CIPHER_CTX_copy(ctx, ctx_base)) { if (fips_dupctx_supported) { TEST_info("Doing a copy of Cipher %s Fails!\n", EVP_CIPHER_get0_name(expected->cipher)); ERR_print_errors_fp(stderr); goto err; } else { TEST_info("Allowing copy fail as an old fips provider is in use."); } EVP_CIPHER_CTX_free(ctx); ctx = ctx_base; } else { EVP_CIPHER_CTX_free(ctx_base); ctx_base = NULL; } /* Likewise for dup */ duped = EVP_CIPHER_CTX_dup(ctx); if (duped != NULL) { EVP_CIPHER_CTX_free(ctx); ctx = duped; } else { if (fips_dupctx_supported) { TEST_info("Doing a dup of Cipher %s Fails!\n", EVP_CIPHER_get0_name(expected->cipher)); ERR_print_errors_fp(stderr); goto err; } else { TEST_info("Allowing dup fail as an old fips provider is in use."); } } ERR_pop_to_mark(); if (expected->mac_key != NULL && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, (int)expected->mac_key_len, (void *)expected->mac_key) <= 0) { t->err = "SET_MAC_KEY_ERROR"; goto err; } if (expected->tls_version) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION, &expected->tls_version); params[1] = OSSL_PARAM_construct_end(); if (!EVP_CIPHER_CTX_set_params(ctx, params)) { t->err = "SET_TLS_VERSION_ERROR"; goto err; } } if (expected->aead == EVP_CIPH_CCM_MODE) { if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) { t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR"; goto err; } } if (expected->aad[0] != NULL && !expected->tls_aad) { t->err = "AAD_SET_ERROR"; if (!frag) { for (i = 0; expected->aad[i] != NULL; i++) { if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], expected->aad_len[i])) goto err; } } else { /* * Supply the AAD in chunks less than the block size where possible */ for (i = 0; expected->aad[i] != NULL; i++) { if (expected->aad_len[i] > 0) { if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1)) goto err; donelen++; } if (expected->aad_len[i] > 2) { if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i] + donelen, expected->aad_len[i] - 2)) goto err; donelen += expected->aad_len[i] - 2; } if (expected->aad_len[i] > 1 && !EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i] + donelen, 1)) goto err; } } } if (expected->tls_aad) { OSSL_PARAM params[2]; char *tls_aad; /* duplicate the aad as the implementation might modify it */ if ((tls_aad = OPENSSL_memdup(expected->aad[0], expected->aad_len[0])) == NULL) goto err; params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, tls_aad, expected->aad_len[0]); params[1] = OSSL_PARAM_construct_end(); if (!EVP_CIPHER_CTX_set_params(ctx, params)) { OPENSSL_free(tls_aad); t->err = "TLS1_AAD_ERROR"; goto err; } OPENSSL_free(tls_aad); } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) { if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, expected->tag_len, expected->tag) <= 0) { t->err = "TAG_SET_ERROR"; goto err; } } if (expected->xts_standard != NULL) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD, (char *)expected->xts_standard, 0); params[1] = OSSL_PARAM_construct_end(); if (!EVP_CIPHER_CTX_set_params(ctx, params)) { t->err = "SET_XTS_STANDARD_ERROR"; goto err; } } EVP_CIPHER_CTX_set_padding(ctx, 0); t->err = "CIPHERUPDATE_ERROR"; tmplen = 0; if (!frag) { /* We supply the data all in one go */ if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len)) goto err; } else { /* Supply the data in chunks less than the block size where possible */ if (in_len > 0) { if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1)) goto err; tmplen += chunklen; in++; in_len--; } if (in_len > 1) { if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, in, in_len - 1)) goto err; tmplen += chunklen; in += in_len - 1; in_len = 1; } if (in_len > 0) { if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, in, 1)) goto err; tmplen += chunklen; } } if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) { t->err = "CIPHERFINAL_ERROR"; goto err; } if (!enc && expected->tls_aad) { if (expected->tls_version >= TLS1_1_VERSION && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1") || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) { tmplen -= expected->iv_len; expected_out += expected->iv_len; out_misalign += expected->iv_len; } if ((int)out_len > tmplen + tmpflen) out_len = tmplen + tmpflen; } if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len, tmp + out_misalign, tmplen + tmpflen)) goto err; if (enc && expected->aead && !expected->tls_aad) { unsigned char rtag[16]; if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) { t->err = "TAG_LENGTH_INTERNAL_ERROR"; goto err; } if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, expected->tag_len, rtag) <= 0) { t->err = "TAG_RETRIEVE_ERROR"; goto err; } if (!memory_err_compare(t, "TAG_VALUE_MISMATCH", expected->tag, expected->tag_len, rtag, expected->tag_len)) goto err; } /* Check the updated IV */ if (expected->next_iv != NULL) { /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */ unsigned char iv[128]; if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))) || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv, expected->iv_len))) { t->err = "INVALID_NEXT_IV"; goto err; } } t->err = NULL; ok = 1; err: OPENSSL_free(tmp); if (ctx != ctx_base) EVP_CIPHER_CTX_free(ctx_base); EVP_CIPHER_CTX_free(ctx); return ok; } /* * XTS, SIV, CCM, stitched ciphers and Wrap modes have special * requirements about input lengths so we don't fragment for those */ static int cipher_test_valid_fragmentation(CIPHER_DATA *cdat) { return (cdat->aead == EVP_CIPH_CCM_MODE || cdat->aead == EVP_CIPH_CBC_MODE || (cdat->aead == -1 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER) || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0) || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_GCM_SIV_MODE || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) ? 0 : 1; } static int cipher_test_run(EVP_TEST *t) { CIPHER_DATA *cdat = t->data; int rv, frag, fragmax, in_place; size_t out_misalign, inp_misalign; TEST_info("RUNNING TEST FOR CIPHER %s\n", EVP_CIPHER_get0_name(cdat->cipher)); if (!cdat->key) { t->err = "NO_KEY"; return 0; } if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) { /* IV is optional and usually omitted in wrap mode */ if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) { t->err = "NO_IV"; return 0; } } if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) { t->err = "NO_TAG"; return 0; } fragmax = (cipher_test_valid_fragmentation(cdat) == 0) ? 0 : 1; for (in_place = 1; in_place >= 0; in_place--) { static char aux_err[64]; t->aux_err = aux_err; /* Test only in-place data processing */ if (process_mode_in_place == 1 && in_place == 0) break; for (frag = 0; frag <= fragmax; frag++) { for (out_misalign = 0; out_misalign <= 1; out_misalign++) { for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) { /* Skip input misalign tests for in-place processing */ if (inp_misalign == 1 && in_place == 1) break; if (in_place == 1) { BIO_snprintf(aux_err, sizeof(aux_err), "%s in-place, %sfragmented", out_misalign ? "misaligned" : "aligned", frag ? "" : "not "); } else { BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input, %sfragmented", out_misalign ? "misaligned" : "aligned", inp_misalign ? "misaligned" : "aligned", frag ? "" : "not "); } if (cdat->enc) { rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag, in_place); /* Not fatal errors: return */ if (rv != 1) { if (rv < 0) return 0; return 1; } } if (cdat->enc != 1) { rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag, in_place); /* Not fatal errors: return */ if (rv != 1) { if (rv < 0) return 0; return 1; } } } } } } t->aux_err = NULL; return 1; } static const EVP_TEST_METHOD cipher_test_method = { "Cipher", cipher_test_init, cipher_test_cleanup, cipher_test_parse, cipher_test_run }; /** ** MAC TESTS **/ typedef struct mac_data_st { /* MAC type in one form or another */ char *mac_name; EVP_MAC *mac; /* for mac_test_run_mac */ int type; /* for mac_test_run_pkey */ /* Algorithm string for this MAC */ char *alg; /* MAC key */ unsigned char *key; size_t key_len; /* MAC IV (GMAC) */ unsigned char *iv; size_t iv_len; /* Input to MAC */ unsigned char *input; size_t input_len; /* Expected output */ unsigned char *output; size_t output_len; unsigned char *custom; size_t custom_len; /* MAC salt (blake2) */ unsigned char *salt; size_t salt_len; /* XOF mode? */ int xof; /* Reinitialization fails */ int no_reinit; /* Collection of controls */ STACK_OF(OPENSSL_STRING) *controls; /* Output size */ int output_size; /* Block size */ int block_size; } MAC_DATA; static int mac_test_init(EVP_TEST *t, const char *alg) { EVP_MAC *mac = NULL; int type = NID_undef; MAC_DATA *mdat; if (is_mac_disabled(alg)) { TEST_info("skipping, '%s' is disabled", alg); t->skip = 1; return 1; } if ((mac = EVP_MAC_fetch(libctx, alg, propquery)) == NULL) { /* * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running * the EVP_PKEY method. */ size_t sz = strlen(alg); static const char epilogue[] = " by EVP_PKEY"; if (sz >= sizeof(epilogue) && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0) sz -= sizeof(epilogue) - 1; if (strncmp(alg, "HMAC", sz) == 0) type = EVP_PKEY_HMAC; else if (strncmp(alg, "CMAC", sz) == 0) type = EVP_PKEY_CMAC; else if (strncmp(alg, "Poly1305", sz) == 0) type = EVP_PKEY_POLY1305; else if (strncmp(alg, "SipHash", sz) == 0) type = EVP_PKEY_SIPHASH; else return 0; } if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) return 0; mdat->type = type; if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) { OPENSSL_free(mdat); return 0; } mdat->mac = mac; if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) { OPENSSL_free(mdat->mac_name); OPENSSL_free(mdat); return 0; } mdat->output_size = mdat->block_size = -1; t->data = mdat; return 1; } /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ static void openssl_free(char *m) { OPENSSL_free(m); } static void mac_test_cleanup(EVP_TEST *t) { MAC_DATA *mdat = t->data; EVP_MAC_free(mdat->mac); OPENSSL_free(mdat->mac_name); sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free); OPENSSL_free(mdat->alg); OPENSSL_free(mdat->key); OPENSSL_free(mdat->iv); OPENSSL_free(mdat->custom); OPENSSL_free(mdat->salt); OPENSSL_free(mdat->input); OPENSSL_free(mdat->output); } static int mac_test_parse(EVP_TEST *t, const char *keyword, const char *value) { MAC_DATA *mdata = t->data; if (strcmp(keyword, "Key") == 0) return parse_bin(value, &mdata->key, &mdata->key_len); if (strcmp(keyword, "IV") == 0) return parse_bin(value, &mdata->iv, &mdata->iv_len); if (strcmp(keyword, "Custom") == 0) return parse_bin(value, &mdata->custom, &mdata->custom_len); if (strcmp(keyword, "Salt") == 0) return parse_bin(value, &mdata->salt, &mdata->salt_len); if (strcmp(keyword, "Algorithm") == 0) { mdata->alg = OPENSSL_strdup(value); if (mdata->alg == NULL) return -1; return 1; } if (strcmp(keyword, "Input") == 0) return parse_bin(value, &mdata->input, &mdata->input_len); if (strcmp(keyword, "Output") == 0) return parse_bin(value, &mdata->output, &mdata->output_len); if (strcmp(keyword, "XOF") == 0) return mdata->xof = 1; if (strcmp(keyword, "NoReinit") == 0) return mdata->no_reinit = 1; if (strcmp(keyword, "Ctrl") == 0) { char *data = OPENSSL_strdup(value); if (data == NULL) return -1; return sk_OPENSSL_STRING_push(mdata->controls, data) != 0; } if (strcmp(keyword, "OutputSize") == 0) { mdata->output_size = atoi(value); if (mdata->output_size < 0) return -1; return 1; } if (strcmp(keyword, "BlockSize") == 0) { mdata->block_size = atoi(value); if (mdata->block_size < 0) return -1; return 1; } return 0; } static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx, const char *value) { int rv = 0; char *p, *tmpval; if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) return 0; p = strchr(tmpval, ':'); if (p != NULL) { *p++ = '\0'; rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); } if (rv == -2) t->err = "PKEY_CTRL_INVALID"; else if (rv <= 0) t->err = "PKEY_CTRL_ERROR"; else rv = 1; OPENSSL_free(tmpval); return rv > 0; } static int mac_test_run_pkey(EVP_TEST *t) { MAC_DATA *expected = t->data; EVP_MD_CTX *mctx = NULL; EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; EVP_PKEY *key = NULL; const char *mdname = NULL; EVP_CIPHER *cipher = NULL; unsigned char *got = NULL; size_t got_len; int i; /* We don't do XOF mode via PKEY */ if (expected->xof) return 1; if (expected->alg == NULL) TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type)); else TEST_info("Trying the EVP_PKEY %s test with %s", OBJ_nid2sn(expected->type), expected->alg); if (expected->type == EVP_PKEY_CMAC) { #ifdef OPENSSL_NO_DEPRECATED_3_0 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg); t->skip = 1; t->err = NULL; goto err; #else OSSL_LIB_CTX *tmpctx; if (expected->alg != NULL && is_cipher_disabled(expected->alg)) { TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg); t->skip = 1; t->err = NULL; goto err; } if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, propquery))) { t->err = "MAC_KEY_CREATE_ERROR"; goto err; } tmpctx = OSSL_LIB_CTX_set0_default(libctx); key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, cipher); OSSL_LIB_CTX_set0_default(tmpctx); #endif } else { key = EVP_PKEY_new_raw_private_key_ex(libctx, OBJ_nid2sn(expected->type), NULL, expected->key, expected->key_len); } if (key == NULL) { t->err = "MAC_KEY_CREATE_ERROR"; goto err; } if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) { if (is_digest_disabled(expected->alg)) { TEST_info("skipping, HMAC '%s' is disabled", expected->alg); t->skip = 1; t->err = NULL; goto err; } mdname = expected->alg; } if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { t->err = "INTERNAL_ERROR"; goto err; } if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) { t->err = "DIGESTSIGNINIT_ERROR"; goto err; } for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) if (!mac_test_ctrl_pkey(t, pctx, sk_OPENSSL_STRING_value(expected->controls, i))) { t->err = "EVPPKEYCTXCTRL_ERROR"; goto err; } if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { t->err = "DIGESTSIGNUPDATE_ERROR"; goto err; } if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; goto err; } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "TEST_FAILURE"; goto err; } if (!EVP_DigestSignFinal(mctx, got, &got_len) || !memory_err_compare(t, "TEST_MAC_ERR", expected->output, expected->output_len, got, got_len)) { t->err = "TEST_MAC_ERR"; goto err; } t->err = NULL; err: EVP_CIPHER_free(cipher); EVP_MD_CTX_free(mctx); OPENSSL_free(got); EVP_PKEY_CTX_free(genctx); EVP_PKEY_free(key); return 1; } static int mac_test_run_mac(EVP_TEST *t) { MAC_DATA *expected = t->data; EVP_MAC_CTX *ctx = NULL; unsigned char *got = NULL; size_t got_len = 0, size = 0; size_t size_before_init = 0, size_after_init, size_val = 0; int i, block_size = -1, output_size = -1; OSSL_PARAM params[21], sizes[3], *psizes = sizes; size_t params_n = 0; size_t params_n_allocstart = 0; const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(expected->mac); int xof; int reinit = 1; if (expected->alg == NULL) TEST_info("Trying the EVP_MAC %s test", expected->mac_name); else TEST_info("Trying the EVP_MAC %s test with %s", expected->mac_name, expected->alg); if (expected->alg != NULL) { int skip = 0; /* * The underlying algorithm may be a cipher or a digest. * We don't know which it is, but we can ask the MAC what it * should be and bet on that. */ if (OSSL_PARAM_locate_const(defined_params, OSSL_MAC_PARAM_CIPHER) != NULL) { if (is_cipher_disabled(expected->alg)) skip = 1; else params[params_n++] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, expected->alg, 0); } else if (OSSL_PARAM_locate_const(defined_params, OSSL_MAC_PARAM_DIGEST) != NULL) { if (is_digest_disabled(expected->alg)) skip = 1; else params[params_n++] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, expected->alg, 0); } else { t->err = "MAC_BAD_PARAMS"; goto err; } if (skip) { TEST_info("skipping, algorithm '%s' is disabled", expected->alg); t->skip = 1; t->err = NULL; goto err; } } if (expected->custom != NULL) params[params_n++] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, expected->custom, expected->custom_len); if (expected->salt != NULL) params[params_n++] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT, expected->salt, expected->salt_len); if (expected->iv != NULL) params[params_n++] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV, expected->iv, expected->iv_len); /* Unknown controls. They must match parameters that the MAC recognizes */ if (params_n + sk_OPENSSL_STRING_num(expected->controls) >= OSSL_NELEM(params)) { t->err = "MAC_TOO_MANY_PARAMETERS"; goto err; } params_n_allocstart = params_n; for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) { char *tmpkey, *tmpval; char *value = sk_OPENSSL_STRING_value(expected->controls, i); if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) { t->err = "MAC_PARAM_ERROR"; goto err; } tmpval = strchr(tmpkey, ':'); if (tmpval != NULL) *tmpval++ = '\0'; if (tmpval == NULL || !OSSL_PARAM_allocate_from_text(&params[params_n], defined_params, tmpkey, tmpval, strlen(tmpval), NULL)) { OPENSSL_free(tmpkey); t->err = "MAC_PARAM_ERROR"; goto err; } params_n++; if (strcmp(tmpkey, "size") == 0) size_val = (size_t)strtoul(tmpval, NULL, 0); OPENSSL_free(tmpkey); } params[params_n] = OSSL_PARAM_construct_end(); if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) { t->err = "MAC_CREATE_ERROR"; goto err; } if (fips_provider_version_gt(libctx, 3, 2, 0)) size_before_init = EVP_MAC_CTX_get_mac_size(ctx); if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) { t->err = "MAC_INIT_ERROR"; goto err; } size_after_init = EVP_MAC_CTX_get_mac_size(ctx); if (!TEST_false(size_before_init == 0 && size_after_init == 0)) { t->err = "MAC SIZE not set"; goto err; } if (size_before_init != 0) { /* mac-size not modified by init params */ if (size_val == 0 && !TEST_size_t_eq(size_before_init, size_after_init)) { t->err = "MAC SIZE check failed"; goto err; } /* mac-size modified by init params */ if (size_val != 0 && !TEST_size_t_eq(size_val, size_after_init)) { t->err = "MAC SIZE check failed"; goto err; } } if (expected->output_size >= 0) *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, &output_size); if (expected->block_size >= 0) *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE, &block_size); if (psizes != sizes) { *psizes = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) { t->err = "INTERNAL_ERROR"; goto err; } if (expected->output_size >= 0 && !TEST_int_eq(output_size, expected->output_size)) { t->err = "TEST_FAILURE"; goto err; } if (expected->block_size >= 0 && !TEST_int_eq(block_size, expected->block_size)) { t->err = "TEST_FAILURE"; goto err; } } retry: if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { t->err = "MAC_UPDATE_ERROR"; goto err; } xof = expected->xof; if (xof) { if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) { t->err = "TEST_FAILURE"; goto err; } if (!EVP_MAC_finalXOF(ctx, got, expected->output_len) || !memory_err_compare(t, "TEST_MAC_ERR", expected->output, expected->output_len, got, expected->output_len)) { t->err = "MAC_FINAL_ERROR"; goto err; } } else { if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) { t->err = "MAC_FINAL_LENGTH_ERROR"; goto err; } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "TEST_FAILURE"; goto err; } if (!EVP_MAC_final(ctx, got, &got_len, got_len) || !memory_err_compare(t, "TEST_MAC_ERR", expected->output, expected->output_len, got, got_len)) { t->err = "TEST_MAC_ERR"; goto err; } } /* FIPS(3.0.0): can't reinitialise MAC contexts #18100 */ if (reinit-- && fips_provider_version_gt(libctx, 3, 0, 0)) { OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; int ret; /* If the MAC uses IV, we have to set it again */ if (expected->iv != NULL) { ivparams[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV, expected->iv, expected->iv_len); ivparams[1] = OSSL_PARAM_construct_end(); } ERR_set_mark(); ret = EVP_MAC_init(ctx, NULL, 0, ivparams); if (expected->no_reinit) { if (ret) { ERR_clear_last_mark(); t->err = "MAC_REINIT_SHOULD_FAIL"; goto err; } } else if (ret) { ERR_clear_last_mark(); OPENSSL_free(got); got = NULL; goto retry; } else { ERR_clear_last_mark(); t->err = "MAC_REINIT_ERROR"; goto err; } /* If reinitialization fails, it is unsupported by the algorithm */ ERR_pop_to_mark(); } t->err = NULL; /* Test the EVP_Q_mac interface as well */ if (!xof) { OPENSSL_cleanse(got, got_len); if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL, expected->alg, params, expected->key, expected->key_len, expected->input, expected->input_len, got, got_len, &size)) || !TEST_mem_eq(got, size, expected->output, expected->output_len)) { t->err = "EVP_Q_mac failed"; goto err; } } err: while (params_n-- > params_n_allocstart) { OPENSSL_free(params[params_n].data); } EVP_MAC_CTX_free(ctx); OPENSSL_free(got); return 1; } static int mac_test_run(EVP_TEST *t) { MAC_DATA *expected = t->data; if (expected->mac != NULL) return mac_test_run_mac(t); return mac_test_run_pkey(t); } static const EVP_TEST_METHOD mac_test_method = { "MAC", mac_test_init, mac_test_cleanup, mac_test_parse, mac_test_run }; /** ** PUBLIC KEY TESTS ** These are all very similar and share much common code. **/ typedef struct pkey_data_st { /* Context for this operation */ EVP_PKEY_CTX *ctx; /* Key operation to perform */ int (*keyop) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); /* Input to MAC */ unsigned char *input; size_t input_len; /* Expected output */ unsigned char *output; size_t output_len; } PKEY_DATA; /* * Perform public key operation setup: lookup key, allocated ctx and call * the appropriate initialisation function */ static int pkey_test_init(EVP_TEST *t, const char *name, int use_public, int (*keyopinit) (EVP_PKEY_CTX *ctx), int (*keyop)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen)) { PKEY_DATA *kdata; EVP_PKEY *pkey = NULL; int rv = 0; if (use_public) rv = find_key(&pkey, name, public_keys); if (rv == 0) rv = find_key(&pkey, name, private_keys); if (rv == 0 || pkey == NULL) { TEST_info("skipping, key '%s' is disabled", name); t->skip = 1; return 1; } if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { EVP_PKEY_free(pkey); return 0; } kdata->keyop = keyop; if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propquery))) { EVP_PKEY_free(pkey); OPENSSL_free(kdata); return 0; } if (keyopinit(kdata->ctx) <= 0) t->err = "KEYOP_INIT_ERROR"; t->data = kdata; return 1; } static void pkey_test_cleanup(EVP_TEST *t) { PKEY_DATA *kdata = t->data; OPENSSL_free(kdata->input); OPENSSL_free(kdata->output); EVP_PKEY_CTX_free(kdata->ctx); } static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, const char *value) { int rv = 0; char *p, *tmpval; if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) return 0; p = strchr(tmpval, ':'); if (p != NULL) { *p++ = '\0'; rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); } if (rv == -2) { t->err = "PKEY_CTRL_INVALID"; rv = 1; } else if (p != NULL && rv <= 0) { if (is_digest_disabled(p) || is_cipher_disabled(p)) { TEST_info("skipping, '%s' is disabled", p); t->skip = 1; rv = 1; } else { t->err = "PKEY_CTRL_ERROR"; rv = 1; } } OPENSSL_free(tmpval); return rv > 0; } static int pkey_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PKEY_DATA *kdata = t->data; if (strcmp(keyword, "Input") == 0) return parse_bin(value, &kdata->input, &kdata->input_len); if (strcmp(keyword, "Output") == 0) return parse_bin(value, &kdata->output, &kdata->output_len); if (strcmp(keyword, "Ctrl") == 0) return pkey_test_ctrl(t, kdata->ctx, value); return 0; } static int pkey_test_run(EVP_TEST *t) { PKEY_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len; EVP_PKEY_CTX *copy = NULL; if (expected->keyop(expected->ctx, NULL, &got_len, expected->input, expected->input_len) <= 0 || !TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "KEYOP_LENGTH_ERROR"; goto err; } if (expected->keyop(expected->ctx, got, &got_len, expected->input, expected->input_len) <= 0) { t->err = "KEYOP_ERROR"; goto err; } if (!memory_err_compare(t, "KEYOP_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; OPENSSL_free(got); got = NULL; /* Repeat the test on a copy. */ if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) { t->err = "INTERNAL_ERROR"; goto err; } if (expected->keyop(copy, NULL, &got_len, expected->input, expected->input_len) <= 0 || !TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "KEYOP_LENGTH_ERROR"; goto err; } if (expected->keyop(copy, got, &got_len, expected->input, expected->input_len) <= 0) { t->err = "KEYOP_ERROR"; goto err; } if (!memory_err_compare(t, "KEYOP_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; err: OPENSSL_free(got); EVP_PKEY_CTX_free(copy); return 1; } static int sign_test_init(EVP_TEST *t, const char *name) { return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); } static const EVP_TEST_METHOD psign_test_method = { "Sign", sign_test_init, pkey_test_cleanup, pkey_test_parse, pkey_test_run }; static int verify_recover_test_init(EVP_TEST *t, const char *name) { return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, EVP_PKEY_verify_recover); } static const EVP_TEST_METHOD pverify_recover_test_method = { "VerifyRecover", verify_recover_test_init, pkey_test_cleanup, pkey_test_parse, pkey_test_run }; static int decrypt_test_init(EVP_TEST *t, const char *name) { return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, EVP_PKEY_decrypt); } static const EVP_TEST_METHOD pdecrypt_test_method = { "Decrypt", decrypt_test_init, pkey_test_cleanup, pkey_test_parse, pkey_test_run }; static int verify_test_init(EVP_TEST *t, const char *name) { return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); } static int verify_test_run(EVP_TEST *t) { PKEY_DATA *kdata = t->data; if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, kdata->input, kdata->input_len) <= 0) t->err = "VERIFY_ERROR"; return 1; } static const EVP_TEST_METHOD pverify_test_method = { "Verify", verify_test_init, pkey_test_cleanup, pkey_test_parse, verify_test_run }; static int pderive_test_init(EVP_TEST *t, const char *name) { return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); } static int pderive_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PKEY_DATA *kdata = t->data; int validate = 0; if (strcmp(keyword, "PeerKeyValidate") == 0) validate = 1; if (validate || strcmp(keyword, "PeerKey") == 0) { EVP_PKEY *peer; if (find_key(&peer, value, public_keys) == 0) return -1; if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) { t->err = "DERIVE_SET_PEER_ERROR"; return 1; } t->err = NULL; return 1; } if (strcmp(keyword, "SharedSecret") == 0) return parse_bin(value, &kdata->output, &kdata->output_len); if (strcmp(keyword, "Ctrl") == 0) return pkey_test_ctrl(t, kdata->ctx, value); if (strcmp(keyword, "KDFType") == 0) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, (char *)value, 0); params[1] = OSSL_PARAM_construct_end(); if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0) return -1; return 1; } if (strcmp(keyword, "KDFDigest") == 0) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, (char *)value, 0); params[1] = OSSL_PARAM_construct_end(); if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0) return -1; return 1; } if (strcmp(keyword, "CEKAlg") == 0) { OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG, (char *)value, 0); params[1] = OSSL_PARAM_construct_end(); if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0) return -1; return 1; } if (strcmp(keyword, "KDFOutlen") == 0) { OSSL_PARAM params[2]; char *endptr; size_t outlen = (size_t)strtoul(value, &endptr, 0); if (endptr[0] != '\0') return -1; params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, &outlen); params[1] = OSSL_PARAM_construct_end(); if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0) return -1; return 1; } return 0; } static int pderive_test_run(EVP_TEST *t) { EVP_PKEY_CTX *dctx = NULL; PKEY_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len; if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) { t->err = "DERIVE_ERROR"; goto err; } if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0 || !TEST_size_t_ne(got_len, 0)) { t->err = "DERIVE_ERROR"; goto err; } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "DERIVE_ERROR"; goto err; } if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) { t->err = "DERIVE_ERROR"; goto err; } if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; err: OPENSSL_free(got); EVP_PKEY_CTX_free(dctx); return 1; } static const EVP_TEST_METHOD pderive_test_method = { "Derive", pderive_test_init, pkey_test_cleanup, pderive_test_parse, pderive_test_run }; /** ** PBE TESTS **/ typedef enum pbe_type_enum { PBE_TYPE_INVALID = 0, PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 } PBE_TYPE; typedef struct pbe_data_st { PBE_TYPE pbe_type; /* scrypt parameters */ uint64_t N, r, p, maxmem; /* PKCS#12 parameters */ int id, iter; const EVP_MD *md; /* password */ unsigned char *pass; size_t pass_len; /* salt */ unsigned char *salt; size_t salt_len; /* Expected output */ unsigned char *key; size_t key_len; } PBE_DATA; #ifndef OPENSSL_NO_SCRYPT /* Parse unsigned decimal 64 bit integer value */ static int parse_uint64(const char *value, uint64_t *pr) { const char *p = value; if (!TEST_true(*p)) { TEST_info("Invalid empty integer value"); return -1; } for (*pr = 0; *p; ) { if (*pr > UINT64_MAX / 10) { TEST_error("Integer overflow in string %s", value); return -1; } *pr *= 10; if (!TEST_true(isdigit((unsigned char)*p))) { TEST_error("Invalid character in string %s", value); return -1; } *pr += *p - '0'; p++; } return 1; } static int scrypt_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PBE_DATA *pdata = t->data; if (strcmp(keyword, "N") == 0) return parse_uint64(value, &pdata->N); if (strcmp(keyword, "p") == 0) return parse_uint64(value, &pdata->p); if (strcmp(keyword, "r") == 0) return parse_uint64(value, &pdata->r); if (strcmp(keyword, "maxmem") == 0) return parse_uint64(value, &pdata->maxmem); return 0; } #endif static int pbkdf2_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PBE_DATA *pdata = t->data; if (strcmp(keyword, "iter") == 0) { pdata->iter = atoi(value); if (pdata->iter <= 0) return -1; return 1; } if (strcmp(keyword, "MD") == 0) { pdata->md = EVP_get_digestbyname(value); if (pdata->md == NULL) return -1; return 1; } return 0; } static int pkcs12_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PBE_DATA *pdata = t->data; if (strcmp(keyword, "id") == 0) { pdata->id = atoi(value); if (pdata->id <= 0) return -1; return 1; } return pbkdf2_test_parse(t, keyword, value); } static int pbe_test_init(EVP_TEST *t, const char *alg) { PBE_DATA *pdat; PBE_TYPE pbe_type = PBE_TYPE_INVALID; if (is_kdf_disabled(alg)) { TEST_info("skipping, '%s' is disabled", alg); t->skip = 1; return 1; } if (strcmp(alg, "scrypt") == 0) { pbe_type = PBE_TYPE_SCRYPT; } else if (strcmp(alg, "pbkdf2") == 0) { pbe_type = PBE_TYPE_PBKDF2; } else if (strcmp(alg, "pkcs12") == 0) { pbe_type = PBE_TYPE_PKCS12; } else { TEST_error("Unknown pbe algorithm %s", alg); return 0; } if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat)))) return 0; pdat->pbe_type = pbe_type; t->data = pdat; return 1; } static void pbe_test_cleanup(EVP_TEST *t) { PBE_DATA *pdat = t->data; OPENSSL_free(pdat->pass); OPENSSL_free(pdat->salt); OPENSSL_free(pdat->key); } static int pbe_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PBE_DATA *pdata = t->data; if (strcmp(keyword, "Password") == 0) return parse_bin(value, &pdata->pass, &pdata->pass_len); if (strcmp(keyword, "Salt") == 0) return parse_bin(value, &pdata->salt, &pdata->salt_len); if (strcmp(keyword, "Key") == 0) return parse_bin(value, &pdata->key, &pdata->key_len); if (pdata->pbe_type == PBE_TYPE_PBKDF2) return pbkdf2_test_parse(t, keyword, value); else if (pdata->pbe_type == PBE_TYPE_PKCS12) return pkcs12_test_parse(t, keyword, value); #ifndef OPENSSL_NO_SCRYPT else if (pdata->pbe_type == PBE_TYPE_SCRYPT) return scrypt_test_parse(t, keyword, value); #endif return 0; } static int pbe_test_run(EVP_TEST *t) { PBE_DATA *expected = t->data; unsigned char *key; EVP_MD *fetched_digest = NULL; OSSL_LIB_CTX *save_libctx; save_libctx = OSSL_LIB_CTX_set0_default(libctx); if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { t->err = "INTERNAL_ERROR"; goto err; } if (expected->pbe_type == PBE_TYPE_PBKDF2) { if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, expected->salt, expected->salt_len, expected->iter, expected->md, expected->key_len, key) == 0) { t->err = "PBKDF2_ERROR"; goto err; } #ifndef OPENSSL_NO_SCRYPT } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, expected->salt, expected->salt_len, expected->N, expected->r, expected->p, expected->maxmem, key, expected->key_len) == 0) { t->err = "SCRYPT_ERROR"; goto err; } #endif } else if (expected->pbe_type == PBE_TYPE_PKCS12) { fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md), propquery); if (fetched_digest == NULL) { t->err = "PKCS12_ERROR"; goto err; } if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, expected->salt, expected->salt_len, expected->id, expected->iter, expected->key_len, key, fetched_digest) == 0) { t->err = "PKCS12_ERROR"; goto err; } } if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len, key, expected->key_len)) goto err; t->err = NULL; err: EVP_MD_free(fetched_digest); OPENSSL_free(key); OSSL_LIB_CTX_set0_default(save_libctx); return 1; } static const EVP_TEST_METHOD pbe_test_method = { "PBE", pbe_test_init, pbe_test_cleanup, pbe_test_parse, pbe_test_run }; /** ** BASE64 TESTS **/ typedef enum { BASE64_CANONICAL_ENCODING = 0, BASE64_VALID_ENCODING = 1, BASE64_INVALID_ENCODING = 2 } base64_encoding_type; typedef struct encode_data_st { /* Input to encoding */ unsigned char *input; size_t input_len; /* Expected output */ unsigned char *output; size_t output_len; base64_encoding_type encoding; } ENCODE_DATA; static int encode_test_init(EVP_TEST *t, const char *encoding) { ENCODE_DATA *edata; if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) return 0; if (strcmp(encoding, "canonical") == 0) { edata->encoding = BASE64_CANONICAL_ENCODING; } else if (strcmp(encoding, "valid") == 0) { edata->encoding = BASE64_VALID_ENCODING; } else if (strcmp(encoding, "invalid") == 0) { edata->encoding = BASE64_INVALID_ENCODING; if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) goto err; } else { TEST_error("Bad encoding: %s." " Should be one of {canonical, valid, invalid}", encoding); goto err; } t->data = edata; return 1; err: OPENSSL_free(edata); return 0; } static void encode_test_cleanup(EVP_TEST *t) { ENCODE_DATA *edata = t->data; OPENSSL_free(edata->input); OPENSSL_free(edata->output); memset(edata, 0, sizeof(*edata)); } static int encode_test_parse(EVP_TEST *t, const char *keyword, const char *value) { ENCODE_DATA *edata = t->data; if (strcmp(keyword, "Input") == 0) return parse_bin(value, &edata->input, &edata->input_len); if (strcmp(keyword, "Output") == 0) return parse_bin(value, &edata->output, &edata->output_len); return 0; } static int encode_test_run(EVP_TEST *t) { ENCODE_DATA *expected = t->data; unsigned char *encode_out = NULL, *decode_out = NULL; int output_len, chunk_len; EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL; if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { t->err = "INTERNAL_ERROR"; goto err; } if (expected->encoding == BASE64_CANONICAL_ENCODING) { if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) || !TEST_ptr(encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) goto err; EVP_EncodeInit(encode_ctx); if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, expected->input, expected->input_len))) goto err; output_len = chunk_len; EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); output_len += chunk_len; if (!memory_err_compare(t, "BAD_ENCODING", expected->output, expected->output_len, encode_out, output_len)) goto err; } if (!TEST_ptr(decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) goto err; EVP_DecodeInit(decode_ctx); if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, expected->output_len) < 0) { t->err = "DECODE_ERROR"; goto err; } output_len = chunk_len; if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { t->err = "DECODE_ERROR"; goto err; } output_len += chunk_len; if (expected->encoding != BASE64_INVALID_ENCODING && !memory_err_compare(t, "BAD_DECODING", expected->input, expected->input_len, decode_out, output_len)) { t->err = "BAD_DECODING"; goto err; } t->err = NULL; err: OPENSSL_free(encode_out); OPENSSL_free(decode_out); EVP_ENCODE_CTX_free(decode_ctx); EVP_ENCODE_CTX_free(encode_ctx); return 1; } static const EVP_TEST_METHOD encode_test_method = { "Encoding", encode_test_init, encode_test_cleanup, encode_test_parse, encode_test_run, }; /** ** RAND TESTS **/ #define MAX_RAND_REPEATS 15 typedef struct rand_data_pass_st { unsigned char *entropy; unsigned char *reseed_entropy; unsigned char *nonce; unsigned char *pers; unsigned char *reseed_addin; unsigned char *addinA; unsigned char *addinB; unsigned char *pr_entropyA; unsigned char *pr_entropyB; unsigned char *output; size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len, pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len, reseed_addin_len; } RAND_DATA_PASS; typedef struct rand_data_st { /* Context for this operation */ EVP_RAND_CTX *ctx; EVP_RAND_CTX *parent; int n; int prediction_resistance; int use_df; unsigned int generate_bits; char *cipher; char *digest; /* Expected output */ RAND_DATA_PASS data[MAX_RAND_REPEATS]; } RAND_DATA; static int rand_test_init(EVP_TEST *t, const char *name) { RAND_DATA *rdata; EVP_RAND *rand; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; unsigned int strength = 256; if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata)))) return 0; /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */ rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips"); if (rand == NULL) goto err; rdata->parent = EVP_RAND_CTX_new(rand, NULL); EVP_RAND_free(rand); if (rdata->parent == NULL) goto err; *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength); if (!EVP_RAND_CTX_set_params(rdata->parent, params)) goto err; rand = EVP_RAND_fetch(libctx, name, propquery); if (rand == NULL) goto err; rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent); EVP_RAND_free(rand); if (rdata->ctx == NULL) goto err; rdata->n = -1; t->data = rdata; return 1; err: EVP_RAND_CTX_free(rdata->parent); OPENSSL_free(rdata); return 0; } static void rand_test_cleanup(EVP_TEST *t) { RAND_DATA *rdata = t->data; int i; OPENSSL_free(rdata->cipher); OPENSSL_free(rdata->digest); for (i = 0; i <= rdata->n; i++) { OPENSSL_free(rdata->data[i].entropy); OPENSSL_free(rdata->data[i].reseed_entropy); OPENSSL_free(rdata->data[i].nonce); OPENSSL_free(rdata->data[i].pers); OPENSSL_free(rdata->data[i].reseed_addin); OPENSSL_free(rdata->data[i].addinA); OPENSSL_free(rdata->data[i].addinB); OPENSSL_free(rdata->data[i].pr_entropyA); OPENSSL_free(rdata->data[i].pr_entropyB); OPENSSL_free(rdata->data[i].output); } EVP_RAND_CTX_free(rdata->ctx); EVP_RAND_CTX_free(rdata->parent); } static int rand_test_parse(EVP_TEST *t, const char *keyword, const char *value) { RAND_DATA *rdata = t->data; RAND_DATA_PASS *item; const char *p; int n; if ((p = strchr(keyword, '.')) != NULL) { n = atoi(++p); if (n >= MAX_RAND_REPEATS) return 0; if (n > rdata->n) rdata->n = n; item = rdata->data + n; if (HAS_PREFIX(keyword, "Entropy.")) return parse_bin(value, &item->entropy, &item->entropy_len); if (HAS_PREFIX(keyword, "ReseedEntropy.")) return parse_bin(value, &item->reseed_entropy, &item->reseed_entropy_len); if (HAS_PREFIX(keyword, "Nonce.")) return parse_bin(value, &item->nonce, &item->nonce_len); if (HAS_PREFIX(keyword, "PersonalisationString.")) return parse_bin(value, &item->pers, &item->pers_len); if (HAS_PREFIX(keyword, "ReseedAdditionalInput.")) return parse_bin(value, &item->reseed_addin, &item->reseed_addin_len); if (HAS_PREFIX(keyword, "AdditionalInputA.")) return parse_bin(value, &item->addinA, &item->addinA_len); if (HAS_PREFIX(keyword, "AdditionalInputB.")) return parse_bin(value, &item->addinB, &item->addinB_len); if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA.")) return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len); if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB.")) return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len); if (HAS_PREFIX(keyword, "Output.")) return parse_bin(value, &item->output, &item->output_len); } else { if (strcmp(keyword, "Cipher") == 0) return TEST_ptr(rdata->cipher = OPENSSL_strdup(value)); if (strcmp(keyword, "Digest") == 0) return TEST_ptr(rdata->digest = OPENSSL_strdup(value)); if (strcmp(keyword, "DerivationFunction") == 0) { rdata->use_df = atoi(value) != 0; return 1; } if (strcmp(keyword, "GenerateBits") == 0) { if ((n = atoi(value)) <= 0 || n % 8 != 0) return 0; rdata->generate_bits = (unsigned int)n; return 1; } if (strcmp(keyword, "PredictionResistance") == 0) { rdata->prediction_resistance = atoi(value) != 0; return 1; } } return 0; } static int rand_test_run(EVP_TEST *t) { RAND_DATA *expected = t->data; RAND_DATA_PASS *item; unsigned char *got; size_t got_len = expected->generate_bits / 8; OSSL_PARAM params[5], *p = params; int i = -1, ret = 0; unsigned int strength; unsigned char *z; if (!TEST_ptr(got = OPENSSL_malloc(got_len))) return 0; *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df); if (expected->cipher != NULL) *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, expected->cipher, 0); if (expected->digest != NULL) *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST, expected->digest, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0); *p = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params))) goto err; strength = EVP_RAND_get_strength(expected->ctx); for (i = 0; i <= expected->n; i++) { item = expected->data + i; p = params; z = item->entropy != NULL ? item->entropy : (unsigned char *)""; *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, z, item->entropy_len); z = item->nonce != NULL ? item->nonce : (unsigned char *)""; *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE, z, item->nonce_len); *p = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength, 0, NULL, 0, params))) goto err; z = item->pers != NULL ? item->pers : (unsigned char *)""; if (!TEST_true(EVP_RAND_instantiate (expected->ctx, strength, expected->prediction_resistance, z, item->pers_len, NULL))) goto err; if (item->reseed_entropy != NULL) { params[0] = OSSL_PARAM_construct_octet_string (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy, item->reseed_entropy_len); params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) goto err; if (!TEST_true(EVP_RAND_reseed (expected->ctx, expected->prediction_resistance, NULL, 0, item->reseed_addin, item->reseed_addin_len))) goto err; } if (item->pr_entropyA != NULL) { params[0] = OSSL_PARAM_construct_octet_string (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA, item->pr_entropyA_len); params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) goto err; } if (!TEST_true(EVP_RAND_generate (expected->ctx, got, got_len, strength, expected->prediction_resistance, item->addinA, item->addinA_len))) goto err; if (item->pr_entropyB != NULL) { params[0] = OSSL_PARAM_construct_octet_string (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB, item->pr_entropyB_len); params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) goto err; } if (!TEST_true(EVP_RAND_generate (expected->ctx, got, got_len, strength, expected->prediction_resistance, item->addinB, item->addinB_len))) goto err; if (!TEST_mem_eq(got, got_len, item->output, item->output_len)) goto err; if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx)) || !TEST_true(EVP_RAND_uninstantiate(expected->parent)) || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx)) || !TEST_int_eq(EVP_RAND_get_state(expected->ctx), EVP_RAND_STATE_UNINITIALISED)) goto err; } t->err = NULL; ret = 1; err: if (ret == 0 && i >= 0) TEST_info("Error in test case %d of %d\n", i, expected->n + 1); OPENSSL_free(got); return ret; } static const EVP_TEST_METHOD rand_test_method = { "RAND", rand_test_init, rand_test_cleanup, rand_test_parse, rand_test_run }; /** ** KDF TESTS **/ typedef struct kdf_data_st { /* Context for this operation */ EVP_KDF_CTX *ctx; /* Expected output */ unsigned char *output; size_t output_len; OSSL_PARAM params[20]; OSSL_PARAM *p; } KDF_DATA; /* * Perform public key operation setup: lookup key, allocated ctx and call * the appropriate initialisation function */ static int kdf_test_init(EVP_TEST *t, const char *name) { KDF_DATA *kdata; EVP_KDF *kdf; if (is_kdf_disabled(name)) { TEST_info("skipping, '%s' is disabled", name); t->skip = 1; return 1; } if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) return 0; kdata->p = kdata->params; *kdata->p = OSSL_PARAM_construct_end(); kdf = EVP_KDF_fetch(libctx, name, propquery); if (kdf == NULL) { OPENSSL_free(kdata); return 0; } kdata->ctx = EVP_KDF_CTX_new(kdf); EVP_KDF_free(kdf); if (kdata->ctx == NULL) { OPENSSL_free(kdata); return 0; } t->data = kdata; return 1; } static void kdf_test_cleanup(EVP_TEST *t) { KDF_DATA *kdata = t->data; OSSL_PARAM *p; for (p = kdata->params; p->key != NULL; p++) OPENSSL_free(p->data); OPENSSL_free(kdata->output); EVP_KDF_CTX_free(kdata->ctx); } static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx, const char *value) { KDF_DATA *kdata = t->data; int rv; char *p, *name; const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx)); if (!TEST_ptr(name = OPENSSL_strdup(value))) return 0; p = strchr(name, ':'); if (p != NULL) *p++ = '\0'; if (strcmp(name, "r") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'r' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "lanes") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'lanes' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "iter") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'iter' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "memcost") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'memcost' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "secret") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'secret' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "pass") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'pass' is unsupported"); t->skip = 1; goto end; } if (strcmp(name, "ad") == 0 && OSSL_PARAM_locate_const(defs, name) == NULL) { TEST_info("skipping, setting 'ad' is unsupported"); t->skip = 1; goto end; } rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p, p != NULL ? strlen(p) : 0, NULL); *++kdata->p = OSSL_PARAM_construct_end(); if (!rv) { t->err = "KDF_PARAM_ERROR"; OPENSSL_free(name); return 0; } if (p != NULL && strcmp(name, "digest") == 0) { if (is_digest_disabled(p)) { TEST_info("skipping, '%s' is disabled", p); t->skip = 1; } goto end; } if (p != NULL && (strcmp(name, "cipher") == 0 || strcmp(name, "cekalg") == 0) && is_cipher_disabled(p)) { TEST_info("skipping, '%s' is disabled", p); t->skip = 1; goto end; } if (p != NULL && (strcmp(name, "mac") == 0) && is_mac_disabled(p)) { TEST_info("skipping, '%s' is disabled", p); t->skip = 1; } end: OPENSSL_free(name); return 1; } static int kdf_test_parse(EVP_TEST *t, const char *keyword, const char *value) { KDF_DATA *kdata = t->data; if (strcmp(keyword, "Output") == 0) return parse_bin(value, &kdata->output, &kdata->output_len); if (HAS_PREFIX(keyword, "Ctrl")) return kdf_test_ctrl(t, kdata->ctx, value); return 0; } static int kdf_test_run(EVP_TEST *t) { KDF_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len = expected->output_len; EVP_KDF_CTX *ctx; if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) { t->err = "KDF_CTRL_ERROR"; return 1; } if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) { t->err = "INTERNAL_ERROR"; goto err; } /* FIPS(3.0.0): can't dup KDF contexts #17572 */ if (fips_provider_version_gt(libctx, 3, 0, 0) && (ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) { EVP_KDF_CTX_free(expected->ctx); expected->ctx = ctx; } if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) { t->err = "KDF_DERIVE_ERROR"; goto err; } if (!memory_err_compare(t, "KDF_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; err: OPENSSL_free(got); return 1; } static const EVP_TEST_METHOD kdf_test_method = { "KDF", kdf_test_init, kdf_test_cleanup, kdf_test_parse, kdf_test_run }; /** ** PKEY KDF TESTS **/ typedef struct pkey_kdf_data_st { /* Context for this operation */ EVP_PKEY_CTX *ctx; /* Expected output */ unsigned char *output; size_t output_len; } PKEY_KDF_DATA; /* * Perform public key operation setup: lookup key, allocated ctx and call * the appropriate initialisation function */ static int pkey_kdf_test_init(EVP_TEST *t, const char *name) { PKEY_KDF_DATA *kdata = NULL; if (is_kdf_disabled(name)) { TEST_info("skipping, '%s' is disabled", name); t->skip = 1; return 1; } if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) return 0; kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propquery); if (kdata->ctx == NULL || EVP_PKEY_derive_init(kdata->ctx) <= 0) goto err; t->data = kdata; return 1; err: EVP_PKEY_CTX_free(kdata->ctx); OPENSSL_free(kdata); return 0; } static void pkey_kdf_test_cleanup(EVP_TEST *t) { PKEY_KDF_DATA *kdata = t->data; OPENSSL_free(kdata->output); EVP_PKEY_CTX_free(kdata->ctx); } static int pkey_kdf_test_parse(EVP_TEST *t, const char *keyword, const char *value) { PKEY_KDF_DATA *kdata = t->data; if (strcmp(keyword, "Output") == 0) return parse_bin(value, &kdata->output, &kdata->output_len); if (HAS_PREFIX(keyword, "Ctrl")) return pkey_test_ctrl(t, kdata->ctx, value); return 0; } static int pkey_kdf_test_run(EVP_TEST *t) { PKEY_KDF_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len = 0; if (fips_provider_version_eq(libctx, 3, 0, 0)) { /* FIPS(3.0.0): can't deal with oversized output buffers #18533 */ got_len = expected->output_len; } else { /* Find out the KDF output size */ if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) { t->err = "INTERNAL_ERROR"; goto err; } /* * We may get an absurd output size, which signals that anything goes. * If not, we specify a too big buffer for the output, to test that * EVP_PKEY_derive() can cope with it. */ if (got_len == SIZE_MAX || got_len == 0) got_len = expected->output_len; else got_len = expected->output_len * 2; } if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) { t->err = "INTERNAL_ERROR"; goto err; } if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { t->err = "KDF_DERIVE_ERROR"; goto err; } if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { t->err = "KDF_MISMATCH"; goto err; } t->err = NULL; err: OPENSSL_free(got); return 1; } static const EVP_TEST_METHOD pkey_kdf_test_method = { "PKEYKDF", pkey_kdf_test_init, pkey_kdf_test_cleanup, pkey_kdf_test_parse, pkey_kdf_test_run }; /** ** KEYPAIR TESTS **/ typedef struct keypair_test_data_st { EVP_PKEY *privk; EVP_PKEY *pubk; } KEYPAIR_TEST_DATA; static int keypair_test_init(EVP_TEST *t, const char *pair) { KEYPAIR_TEST_DATA *data; int rv = 0; EVP_PKEY *pk = NULL, *pubk = NULL; char *pub, *priv = NULL; /* Split private and public names. */ if (!TEST_ptr(priv = OPENSSL_strdup(pair)) || !TEST_ptr(pub = strchr(priv, ':'))) { t->err = "PARSING_ERROR"; goto end; } *pub++ = '\0'; if (!TEST_true(find_key(&pk, priv, private_keys))) { TEST_info("Can't find private key: %s", priv); t->err = "MISSING_PRIVATE_KEY"; goto end; } if (!TEST_true(find_key(&pubk, pub, public_keys))) { TEST_info("Can't find public key: %s", pub); t->err = "MISSING_PUBLIC_KEY"; goto end; } if (pk == NULL && pubk == NULL) { /* Both keys are listed but unsupported: skip this test */ t->skip = 1; rv = 1; goto end; } if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) goto end; data->privk = pk; data->pubk = pubk; t->data = data; rv = 1; t->err = NULL; end: OPENSSL_free(priv); return rv; } static void keypair_test_cleanup(EVP_TEST *t) { OPENSSL_free(t->data); t->data = NULL; } /* * For tests that do not accept any custom keywords. */ static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) { return 0; } static int keypair_test_run(EVP_TEST *t) { int rv = 0; const KEYPAIR_TEST_DATA *pair = t->data; if (pair->privk == NULL || pair->pubk == NULL) { /* * this can only happen if only one of the keys is not set * which means that one of them was unsupported while the * other isn't: hence a key type mismatch. */ t->err = "KEYPAIR_TYPE_MISMATCH"; rv = 1; goto end; } if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) { if (0 == rv) { t->err = "KEYPAIR_MISMATCH"; } else if (-1 == rv) { t->err = "KEYPAIR_TYPE_MISMATCH"; } else if (-2 == rv) { t->err = "UNSUPPORTED_KEY_COMPARISON"; } else { TEST_error("Unexpected error in key comparison"); rv = 0; goto end; } rv = 1; goto end; } rv = 1; t->err = NULL; end: return rv; } static const EVP_TEST_METHOD keypair_test_method = { "PrivPubKeyPair", keypair_test_init, keypair_test_cleanup, void_test_parse, keypair_test_run }; /** ** KEYGEN TEST **/ typedef struct keygen_test_data_st { EVP_PKEY_CTX *genctx; /* Keygen context to use */ char *keyname; /* Key name to store key or NULL */ } KEYGEN_TEST_DATA; static int keygen_test_init(EVP_TEST *t, const char *alg) { KEYGEN_TEST_DATA *data; EVP_PKEY_CTX *genctx; int nid = OBJ_sn2nid(alg); if (nid == NID_undef) { nid = OBJ_ln2nid(alg); if (nid == NID_undef) return 0; } if (is_pkey_disabled(alg)) { t->skip = 1; return 1; } if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propquery))) goto err; if (EVP_PKEY_keygen_init(genctx) <= 0) { t->err = "KEYGEN_INIT_ERROR"; goto err; } if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) goto err; data->genctx = genctx; data->keyname = NULL; t->data = data; t->err = NULL; return 1; err: EVP_PKEY_CTX_free(genctx); return 0; } static void keygen_test_cleanup(EVP_TEST *t) { KEYGEN_TEST_DATA *keygen = t->data; EVP_PKEY_CTX_free(keygen->genctx); OPENSSL_free(keygen->keyname); OPENSSL_free(t->data); t->data = NULL; } static int keygen_test_parse(EVP_TEST *t, const char *keyword, const char *value) { KEYGEN_TEST_DATA *keygen = t->data; if (strcmp(keyword, "KeyName") == 0) return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); if (strcmp(keyword, "Ctrl") == 0) return pkey_test_ctrl(t, keygen->genctx, value); return 0; } static int keygen_test_run(EVP_TEST *t) { KEYGEN_TEST_DATA *keygen = t->data; EVP_PKEY *pkey = NULL; int rv = 1; if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { t->err = "KEYGEN_GENERATE_ERROR"; goto err; } if (!evp_pkey_is_provided(pkey)) { TEST_info("Warning: legacy key generated %s", keygen->keyname); goto err; } if (keygen->keyname != NULL) { KEY_LIST *key; rv = 0; if (find_key(NULL, keygen->keyname, private_keys)) { TEST_info("Duplicate key %s", keygen->keyname); goto err; } if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) goto err; key->name = keygen->keyname; keygen->keyname = NULL; key->key = pkey; key->next = private_keys; private_keys = key; rv = 1; } else { EVP_PKEY_free(pkey); } t->err = NULL; err: return rv; } static const EVP_TEST_METHOD keygen_test_method = { "KeyGen", keygen_test_init, keygen_test_cleanup, keygen_test_parse, keygen_test_run, }; /** ** DIGEST SIGN+VERIFY TESTS **/ typedef struct { int is_verify; /* Set to 1 if verifying */ int is_oneshot; /* Set to 1 for one shot operation */ const EVP_MD *md; /* Digest to use */ EVP_MD_CTX *ctx; /* Digest context */ EVP_PKEY_CTX *pctx; STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ unsigned char *osin; /* Input data if one shot */ size_t osin_len; /* Input length data if one shot */ unsigned char *output; /* Expected output */ size_t output_len; /* Expected output length */ const char *nonce_type; } DIGESTSIGN_DATA; static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, int is_oneshot) { const EVP_MD *md = NULL; DIGESTSIGN_DATA *mdat; if (strcmp(alg, "NULL") != 0) { if (is_digest_disabled(alg)) { t->skip = 1; return 1; } md = EVP_get_digestbyname(alg); if (md == NULL) return 0; } if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) return 0; mdat->md = md; if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { OPENSSL_free(mdat); return 0; } mdat->is_verify = is_verify; mdat->is_oneshot = is_oneshot; t->data = mdat; return 1; } static int digestsign_test_init(EVP_TEST *t, const char *alg) { return digestsigver_test_init(t, alg, 0, 0); } static void digestsigver_test_cleanup(EVP_TEST *t) { DIGESTSIGN_DATA *mdata = t->data; EVP_MD_CTX_free(mdata->ctx); sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); OPENSSL_free(mdata->osin); OPENSSL_free(mdata->output); OPENSSL_free(mdata); t->data = NULL; } static int digestsigver_test_parse(EVP_TEST *t, const char *keyword, const char *value) { DIGESTSIGN_DATA *mdata = t->data; if (strcmp(keyword, "Key") == 0) { EVP_PKEY *pkey = NULL; int rv = 0; const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md); if (mdata->is_verify) rv = find_key(&pkey, value, public_keys); if (rv == 0) rv = find_key(&pkey, value, private_keys); if (rv == 0 || pkey == NULL) { t->skip = 1; return 1; } if (mdata->is_verify) { if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL, pkey, NULL)) t->err = "DIGESTVERIFYINIT_ERROR"; return 1; } if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL, pkey, NULL)) t->err = "DIGESTSIGNINIT_ERROR"; return 1; } if (strcmp(keyword, "Input") == 0) { if (mdata->is_oneshot) return parse_bin(value, &mdata->osin, &mdata->osin_len); return evp_test_buffer_append(value, &mdata->input); } if (strcmp(keyword, "Output") == 0) return parse_bin(value, &mdata->output, &mdata->output_len); if (!mdata->is_oneshot) { if (strcmp(keyword, "Count") == 0) return evp_test_buffer_set_count(value, mdata->input); if (strcmp(keyword, "Ncopy") == 0) return evp_test_buffer_ncopy(value, mdata->input); } if (strcmp(keyword, "Ctrl") == 0) { if (mdata->pctx == NULL) return -1; return pkey_test_ctrl(t, mdata->pctx, value); } if (strcmp(keyword, "NonceType") == 0) { if (strcmp(value, "deterministic") == 0) { OSSL_PARAM params[2]; unsigned int nonce_type = 1; params[0] = OSSL_PARAM_construct_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, &nonce_type); params[1] = OSSL_PARAM_construct_end(); if (!EVP_PKEY_CTX_set_params(mdata->pctx, params)) t->err = "EVP_PKEY_CTX_set_params_ERROR"; else if (!EVP_PKEY_CTX_get_params(mdata->pctx, params)) t->err = "EVP_PKEY_CTX_get_params_ERROR"; else if (!OSSL_PARAM_modified(&params[0])) t->err = "nonce_type_not_modified_ERROR"; else if (nonce_type != 1) t->err = "nonce_type_value_ERROR"; } return 1; } return 0; } static int digestsign_update_fn(void *ctx, const unsigned char *buf, size_t buflen) { return EVP_DigestSignUpdate(ctx, buf, buflen); } static int digestsign_test_run(EVP_TEST *t) { DIGESTSIGN_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len; if (!evp_test_buffer_do(expected->input, digestsign_update_fn, expected->ctx)) { t->err = "DIGESTUPDATE_ERROR"; goto err; } if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; goto err; } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "MALLOC_FAILURE"; goto err; } got_len *= 2; if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { t->err = "DIGESTSIGNFINAL_ERROR"; goto err; } if (!memory_err_compare(t, "SIGNATURE_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; err: OPENSSL_free(got); return 1; } static const EVP_TEST_METHOD digestsign_test_method = { "DigestSign", digestsign_test_init, digestsigver_test_cleanup, digestsigver_test_parse, digestsign_test_run }; static int digestverify_test_init(EVP_TEST *t, const char *alg) { return digestsigver_test_init(t, alg, 1, 0); } static int digestverify_update_fn(void *ctx, const unsigned char *buf, size_t buflen) { return EVP_DigestVerifyUpdate(ctx, buf, buflen); } static int digestverify_test_run(EVP_TEST *t) { DIGESTSIGN_DATA *mdata = t->data; if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { t->err = "DIGESTUPDATE_ERROR"; return 1; } if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, mdata->output_len) <= 0) t->err = "VERIFY_ERROR"; return 1; } static const EVP_TEST_METHOD digestverify_test_method = { "DigestVerify", digestverify_test_init, digestsigver_test_cleanup, digestsigver_test_parse, digestverify_test_run }; static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) { return digestsigver_test_init(t, alg, 0, 1); } static int oneshot_digestsign_test_run(EVP_TEST *t) { DIGESTSIGN_DATA *expected = t->data; unsigned char *got = NULL; size_t got_len; if (!EVP_DigestSign(expected->ctx, NULL, &got_len, expected->osin, expected->osin_len)) { t->err = "DIGESTSIGN_LENGTH_ERROR"; goto err; } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "MALLOC_FAILURE"; goto err; } got_len *= 2; if (!EVP_DigestSign(expected->ctx, got, &got_len, expected->osin, expected->osin_len)) { t->err = "DIGESTSIGN_ERROR"; goto err; } if (!memory_err_compare(t, "SIGNATURE_MISMATCH", expected->output, expected->output_len, got, got_len)) goto err; t->err = NULL; err: OPENSSL_free(got); return 1; } static const EVP_TEST_METHOD oneshot_digestsign_test_method = { "OneShotDigestSign", oneshot_digestsign_test_init, digestsigver_test_cleanup, digestsigver_test_parse, oneshot_digestsign_test_run }; static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) { return digestsigver_test_init(t, alg, 1, 1); } static int oneshot_digestverify_test_run(EVP_TEST *t) { DIGESTSIGN_DATA *mdata = t->data; if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, mdata->osin, mdata->osin_len) <= 0) t->err = "VERIFY_ERROR"; return 1; } static const EVP_TEST_METHOD oneshot_digestverify_test_method = { "OneShotDigestVerify", oneshot_digestverify_test_init, digestsigver_test_cleanup, digestsigver_test_parse, oneshot_digestverify_test_run }; /** ** PARSING AND DISPATCH **/ static const EVP_TEST_METHOD *evp_test_list[] = { &rand_test_method, &cipher_test_method, &digest_test_method, &digestsign_test_method, &digestverify_test_method, &encode_test_method, &kdf_test_method, &pkey_kdf_test_method, &keypair_test_method, &keygen_test_method, &mac_test_method, &oneshot_digestsign_test_method, &oneshot_digestverify_test_method, &pbe_test_method, &pdecrypt_test_method, &pderive_test_method, &psign_test_method, &pverify_recover_test_method, &pverify_test_method, NULL }; static const EVP_TEST_METHOD *find_test(const char *name) { const EVP_TEST_METHOD **tt; for (tt = evp_test_list; *tt; tt++) { if (strcmp(name, (*tt)->name) == 0) return *tt; } return NULL; } static void clear_test(EVP_TEST *t) { test_clearstanza(&t->s); ERR_clear_error(); if (t->data != NULL) { if (t->meth != NULL) t->meth->cleanup(t); OPENSSL_free(t->data); t->data = NULL; } OPENSSL_free(t->expected_err); t->expected_err = NULL; OPENSSL_free(t->reason); t->reason = NULL; /* Text literal. */ t->err = NULL; t->skip = 0; t->meth = NULL; #if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL) OSSL_set_max_threads(libctx, 0); #endif } /* Check for errors in the test structure; return 1 if okay, else 0. */ static int check_test_error(EVP_TEST *t) { unsigned long err; const char *reason; if (t->err == NULL && t->expected_err == NULL) return 1; if (t->err != NULL && t->expected_err == NULL) { if (t->aux_err != NULL) { TEST_info("%s:%d: Source of above error (%s); unexpected error %s", t->s.test_file, t->s.start, t->aux_err, t->err); } else { TEST_info("%s:%d: Source of above error; unexpected error %s", t->s.test_file, t->s.start, t->err); } return 0; } if (t->err == NULL && t->expected_err != NULL) { TEST_info("%s:%d: Succeeded but was expecting %s", t->s.test_file, t->s.start, t->expected_err); return 0; } if (strcmp(t->err, t->expected_err) != 0) { TEST_info("%s:%d: Expected %s got %s", t->s.test_file, t->s.start, t->expected_err, t->err); return 0; } if (t->reason == NULL) return 1; if (t->reason == NULL) { TEST_info("%s:%d: Test is missing function or reason code", t->s.test_file, t->s.start); return 0; } err = ERR_peek_error(); if (err == 0) { TEST_info("%s:%d: Expected error \"%s\" not set", t->s.test_file, t->s.start, t->reason); return 0; } reason = ERR_reason_error_string(err); if (reason == NULL) { TEST_info("%s:%d: Expected error \"%s\", no strings available." " Assuming ok.", t->s.test_file, t->s.start, t->reason); return 1; } if (strcmp(reason, t->reason) == 0) return 1; TEST_info("%s:%d: Expected error \"%s\", got \"%s\"", t->s.test_file, t->s.start, t->reason, reason); return 0; } /* Run a parsed test. Log a message and return 0 on error. */ static int run_test(EVP_TEST *t) { if (t->meth == NULL) return 1; t->s.numtests++; if (t->skip) { t->s.numskip++; } else { /* run the test */ if (t->err == NULL && t->meth->run_test(t) != 1) { TEST_info("%s:%d %s error", t->s.test_file, t->s.start, t->meth->name); return 0; } if (!check_test_error(t)) { TEST_openssl_errors(); t->s.errors++; } } /* clean it up */ return 1; } static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) { for (; lst != NULL; lst = lst->next) { if (strcmp(lst->name, name) == 0) { if (ppk != NULL) *ppk = lst->key; return 1; } } return 0; } static void free_key_list(KEY_LIST *lst) { while (lst != NULL) { KEY_LIST *next = lst->next; EVP_PKEY_free(lst->key); OPENSSL_free(lst->name); OPENSSL_free(lst); lst = next; } } /* * Is the key type an unsupported algorithm? */ static int key_unsupported(void) { long err = ERR_peek_last_error(); int lib = ERR_GET_LIB(err); long reason = ERR_GET_REASON(err); if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM) || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR) || reason == ERR_R_UNSUPPORTED) { ERR_clear_error(); return 1; } #ifndef OPENSSL_NO_EC /* * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an * hint to an unsupported algorithm/curve (e.g. if binary EC support is * disabled). */ if (lib == ERR_LIB_EC && (reason == EC_R_UNKNOWN_GROUP || reason == EC_R_INVALID_CURVE)) { ERR_clear_error(); return 1; } #endif /* OPENSSL_NO_EC */ return 0; } /* NULL out the value from |pp| but return it. This "steals" a pointer. */ static char *take_value(PAIR *pp) { char *p = pp->value; pp->value = NULL; return p; } #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS) static int securitycheck_enabled(void) { static int enabled = -1; if (enabled == -1) { if (OSSL_PROVIDER_available(libctx, "fips")) { OSSL_PARAM params[2]; OSSL_PROVIDER *prov = NULL; int check = 1; prov = OSSL_PROVIDER_load(libctx, "fips"); if (prov != NULL) { params[0] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS, &check); params[1] = OSSL_PARAM_construct_end(); OSSL_PROVIDER_get_params(prov, params); OSSL_PROVIDER_unload(prov); } enabled = check; return enabled; } enabled = 0; } return enabled; } #endif /* * Return 1 if one of the providers named in the string is available. * The provider names are separated with whitespace. * NOTE: destructive function, it inserts '\0' after each provider name. */ static int prov_available(char *providers) { char *p; int more = 1; while (more) { for (; isspace((unsigned char)(*providers)); providers++) continue; if (*providers == '\0') break; /* End of the road */ for (p = providers; *p != '\0' && !isspace((unsigned char)(*p)); p++) continue; if (*p == '\0') more = 0; else *p = '\0'; if (OSSL_PROVIDER_available(libctx, providers)) return 1; /* Found one */ } return 0; } /* Read and parse one test. Return 0 if failure, 1 if okay. */ static int parse(EVP_TEST *t) { KEY_LIST *key, **klist; EVP_PKEY *pkey; PAIR *pp; int i, j, skipped = 0; top: do { if (BIO_eof(t->s.fp)) return EOF; clear_test(t); if (!test_readstanza(&t->s)) return 0; } while (t->s.numpairs == 0); pp = &t->s.pairs[0]; /* Are we adding a key? */ klist = NULL; pkey = NULL; start: if (strcmp(pp->key, "PrivateKey") == 0) { pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL); if (pkey == NULL && !key_unsupported()) { EVP_PKEY_free(pkey); TEST_info("Can't read private key %s", pp->value); TEST_openssl_errors(); return 0; } klist = &private_keys; } else if (strcmp(pp->key, "PublicKey") == 0) { pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL); if (pkey == NULL && !key_unsupported()) { EVP_PKEY_free(pkey); TEST_info("Can't read public key %s", pp->value); TEST_openssl_errors(); return 0; } klist = &public_keys; } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 || strcmp(pp->key, "PublicKeyRaw") == 0) { char *strnid = NULL, *keydata = NULL; unsigned char *keybin; size_t keylen; int nid; if (strcmp(pp->key, "PrivateKeyRaw") == 0) klist = &private_keys; else klist = &public_keys; strnid = strchr(pp->value, ':'); if (strnid != NULL) { *strnid++ = '\0'; keydata = strchr(strnid, ':'); if (keydata != NULL) *keydata++ = '\0'; } if (keydata == NULL) { TEST_info("Failed to parse %s value", pp->key); return 0; } nid = OBJ_txt2nid(strnid); if (nid == NID_undef) { TEST_info("Unrecognised algorithm NID"); return 0; } if (!parse_bin(keydata, &keybin, &keylen)) { TEST_info("Failed to create binary key"); return 0; } if (klist == &private_keys) pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin, keylen); else pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin, keylen); if (pkey == NULL && !key_unsupported()) { TEST_info("Can't read %s data", pp->key); OPENSSL_free(keybin); TEST_openssl_errors(); return 0; } OPENSSL_free(keybin); } else if (strcmp(pp->key, "Availablein") == 0) { if (!prov_available(pp->value)) { TEST_info("skipping, '%s' provider not available: %s:%d", pp->value, t->s.test_file, t->s.start); t->skip = 1; return 0; } skipped++; pp++; goto start; } else if (strcmp(pp->key, "FIPSversion") == 0) { if (prov_available("fips")) { j = fips_provider_version_match(libctx, pp->value); if (j < 0) { TEST_info("Line %d: error matching FIPS versions\n", t->s.curr); return 0; } else if (j == 0) { TEST_info("skipping, FIPS provider incompatible version: %s:%d", t->s.test_file, t->s.start); t->skip = 1; return 0; } } skipped++; pp++; goto start; } /* If we have a key add to list */ if (klist != NULL) { if (find_key(NULL, pp->value, *klist)) { TEST_info("Duplicate key %s", pp->value); return 0; } if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) return 0; key->name = take_value(pp); key->key = pkey; key->next = *klist; *klist = key; /* Go back and start a new stanza. */ if ((t->s.numpairs - skipped) != 1) TEST_info("Line %d: missing blank line\n", t->s.curr); goto top; } /* Find the test, based on first keyword. */ if (!TEST_ptr(t->meth = find_test(pp->key))) return 0; if (!t->meth->init(t, pp->value)) { TEST_error("unknown %s: %s\n", pp->key, pp->value); return 0; } if (t->skip == 1) { /* TEST_info("skipping %s %s", pp->key, pp->value); */ return 0; } for (pp++, i = 1; i < (t->s.numpairs - skipped); pp++, i++) { if (strcmp(pp->key, "Securitycheck") == 0) { #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS) #else if (!securitycheck_enabled()) #endif { TEST_info("skipping, Securitycheck is disabled: %s:%d", t->s.test_file, t->s.start); t->skip = 1; return 0; } } else if (strcmp(pp->key, "Availablein") == 0) { TEST_info("Line %d: 'Availablein' should be the first option", t->s.curr); return 0; } else if (strcmp(pp->key, "Result") == 0) { if (t->expected_err != NULL) { TEST_info("Line %d: multiple result lines", t->s.curr); return 0; } t->expected_err = take_value(pp); } else if (strcmp(pp->key, "Function") == 0) { /* Ignore old line. */ } else if (strcmp(pp->key, "Reason") == 0) { if (t->reason != NULL) { TEST_info("Line %d: multiple reason lines", t->s.curr); return 0; } t->reason = take_value(pp); } else if (strcmp(pp->key, "Threads") == 0) { if (OSSL_set_max_threads(libctx, atoi(pp->value)) == 0) { TEST_info("skipping, '%s' threads not available: %s:%d", pp->value, t->s.test_file, t->s.start); t->skip = 1; } } else { /* Must be test specific line: try to parse it */ int rv = t->meth->parse(t, pp->key, pp->value); if (rv == 0) { TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); return 0; } if (rv < 0) { TEST_info("Line %d: error processing keyword %s = %s\n", t->s.curr, pp->key, pp->value); return 0; } if (t->skip) return 0; } } return 1; } static int run_file_tests(int i) { EVP_TEST *t; const char *testfile = test_get_argument(i); int c; if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) return 0; if (!test_start_file(&t->s, testfile)) { OPENSSL_free(t); return 0; } while (!BIO_eof(t->s.fp)) { c = parse(t); if (t->skip) { t->s.numskip++; continue; } if (c == 0 || !run_test(t)) { t->s.errors++; break; } } test_end_file(&t->s); clear_test(t); free_key_list(public_keys); free_key_list(private_keys); BIO_free(t->s.key); c = t->s.errors; OPENSSL_free(t); return c == 0; } const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"), { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { "process", OPT_IN_PLACE, 's', "Mode for data processing by cipher tests [in_place/both], both by default"}, { "provider", OPT_PROVIDER_NAME, 's', "The provider to load (when no configuration file, the default value is 'default')" }, { "propquery", OPT_PROV_PROPQUERY, 's', "Property query used when fetching algorithms" }, { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" }, { NULL } }; return test_options; } int setup_tests(void) { size_t n; char *config_file = NULL; char *provider_name = NULL; OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_IN_PLACE: if ((process_mode_in_place = evp_test_process_mode(opt_arg())) == -1) return 0; break; case OPT_PROVIDER_NAME: provider_name = opt_arg(); break; case OPT_PROV_PROPQUERY: propquery = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } /* * Load the provider via configuration into the created library context. * Load the 'null' provider into the default library context to ensure that * the tests do not fallback to using the default provider. */ if (config_file == NULL && provider_name == NULL) provider_name = "default"; if (!test_get_libctx(&libctx, &prov_null, config_file, &libprov, provider_name)) return 0; n = test_get_argument_count(); if (n == 0) return 0; ADD_ALL_TESTS(run_file_tests, n); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(libprov); OSSL_PROVIDER_unload(prov_null); OSSL_LIB_CTX_free(libctx); } static int is_digest_disabled(const char *name) { #ifdef OPENSSL_NO_BLAKE2 if (HAS_CASE_PREFIX(name, "BLAKE")) return 1; #endif #ifdef OPENSSL_NO_MD2 if (OPENSSL_strcasecmp(name, "MD2") == 0) return 1; #endif #ifdef OPENSSL_NO_MDC2 if (OPENSSL_strcasecmp(name, "MDC2") == 0) return 1; #endif #ifdef OPENSSL_NO_MD4 if (OPENSSL_strcasecmp(name, "MD4") == 0) return 1; #endif #ifdef OPENSSL_NO_MD5 if (OPENSSL_strcasecmp(name, "MD5") == 0) return 1; #endif #ifdef OPENSSL_NO_RMD160 if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0) return 1; #endif #ifdef OPENSSL_NO_SM3 if (OPENSSL_strcasecmp(name, "SM3") == 0) return 1; #endif #ifdef OPENSSL_NO_WHIRLPOOL if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0) return 1; #endif return 0; } static int is_pkey_disabled(const char *name) { #ifdef OPENSSL_NO_EC if (HAS_CASE_PREFIX(name, "EC")) return 1; #endif #ifdef OPENSSL_NO_DH if (HAS_CASE_PREFIX(name, "DH")) return 1; #endif #ifdef OPENSSL_NO_DSA if (HAS_CASE_PREFIX(name, "DSA")) return 1; #endif return 0; } static int is_mac_disabled(const char *name) { #ifdef OPENSSL_NO_BLAKE2 if (HAS_CASE_PREFIX(name, "BLAKE2BMAC") || HAS_CASE_PREFIX(name, "BLAKE2SMAC")) return 1; #endif #ifdef OPENSSL_NO_CMAC if (HAS_CASE_PREFIX(name, "CMAC")) return 1; #endif #ifdef OPENSSL_NO_POLY1305 if (HAS_CASE_PREFIX(name, "Poly1305")) return 1; #endif #ifdef OPENSSL_NO_SIPHASH if (HAS_CASE_PREFIX(name, "SipHash")) return 1; #endif return 0; } static int is_kdf_disabled(const char *name) { #ifdef OPENSSL_NO_SCRYPT if (HAS_CASE_SUFFIX(name, "SCRYPT")) return 1; #endif #ifdef OPENSSL_NO_ARGON2 if (HAS_CASE_SUFFIX(name, "ARGON2")) return 1; #endif return 0; } static int is_cipher_disabled(const char *name) { #ifdef OPENSSL_NO_ARIA if (HAS_CASE_PREFIX(name, "ARIA")) return 1; #endif #ifdef OPENSSL_NO_BF if (HAS_CASE_PREFIX(name, "BF")) return 1; #endif #ifdef OPENSSL_NO_CAMELLIA if (HAS_CASE_PREFIX(name, "CAMELLIA")) return 1; #endif #ifdef OPENSSL_NO_CAST if (HAS_CASE_PREFIX(name, "CAST")) return 1; #endif #ifdef OPENSSL_NO_CHACHA if (HAS_CASE_PREFIX(name, "CHACHA")) return 1; #endif #ifdef OPENSSL_NO_POLY1305 if (HAS_CASE_SUFFIX(name, "Poly1305")) return 1; #endif #ifdef OPENSSL_NO_DES if (HAS_CASE_PREFIX(name, "DES")) return 1; if (HAS_CASE_SUFFIX(name, "3DESwrap")) return 1; #endif #ifdef OPENSSL_NO_OCB if (HAS_CASE_SUFFIX(name, "OCB")) return 1; #endif #ifdef OPENSSL_NO_IDEA if (HAS_CASE_PREFIX(name, "IDEA")) return 1; #endif #ifdef OPENSSL_NO_RC2 if (HAS_CASE_PREFIX(name, "RC2")) return 1; #endif #ifdef OPENSSL_NO_RC4 if (HAS_CASE_PREFIX(name, "RC4")) return 1; #endif #ifdef OPENSSL_NO_RC5 if (HAS_CASE_PREFIX(name, "RC5")) return 1; #endif #ifdef OPENSSL_NO_SEED if (HAS_CASE_PREFIX(name, "SEED")) return 1; #endif #ifdef OPENSSL_NO_SIV if (HAS_CASE_SUFFIX(name, "SIV")) return 1; #endif #ifdef OPENSSL_NO_SM4 if (HAS_CASE_PREFIX(name, "SM4")) return 1; #endif return 0; }
./openssl/test/dtls_mtu_test.c
/* * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <string.h> #include <openssl/dtls1.h> #include <openssl/ssl.h> #include <openssl/err.h> #include "helpers/ssltestlib.h" #include "testutil.h" /* for SSL_READ_ETM() */ #include "../ssl/ssl_local.h" static int debug = 0; static unsigned int clnt_psk_callback(SSL *ssl, const char *hint, char *ident, unsigned int max_ident_len, unsigned char *psk, unsigned int max_psk_len) { BIO_snprintf(ident, max_ident_len, "psk"); if (max_psk_len > 20) max_psk_len = 20; memset(psk, 0x5a, max_psk_len); return max_psk_len; } static unsigned int srvr_psk_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { if (max_psk_len > 20) max_psk_len = 20; memset(psk, 0x5a, max_psk_len); return max_psk_len; } static int mtu_test(SSL_CTX *ctx, const char *cs, int no_etm) { SSL *srvr_ssl = NULL, *clnt_ssl = NULL; BIO *sc_bio = NULL; int i; size_t s; size_t mtus[30]; unsigned char buf[600]; int rv = 0; SSL_CONNECTION *clnt_sc; memset(buf, 0x5a, sizeof(buf)); if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl, NULL, NULL))) goto end; if (no_etm) SSL_set_options(srvr_ssl, SSL_OP_NO_ENCRYPT_THEN_MAC); if (!TEST_true(SSL_set_cipher_list(srvr_ssl, cs)) || !TEST_true(SSL_set_cipher_list(clnt_ssl, cs)) || !TEST_ptr(sc_bio = SSL_get_rbio(srvr_ssl)) || !TEST_true(create_ssl_connection(clnt_ssl, srvr_ssl, SSL_ERROR_NONE))) goto end; if (debug) TEST_info("Channel established"); /* For record MTU values between 500 and 539, call DTLS_get_data_mtu() * to query the payload MTU which will fit. */ for (i = 0; i < 30; i++) { SSL_set_mtu(clnt_ssl, 500 + i); mtus[i] = DTLS_get_data_mtu(clnt_ssl); if (debug) TEST_info("%s%s MTU for record mtu %d = %lu", cs, no_etm ? "-noEtM" : "", 500 + i, (unsigned long)mtus[i]); if (!TEST_size_t_ne(mtus[i], 0)) { TEST_info("Cipher %s MTU %d", cs, 500 + i); goto end; } } /* Now get out of the way */ SSL_set_mtu(clnt_ssl, 1000); /* * Now for all values in the range of payload MTUs, send a payload of * that size and see what actual record size we end up with. */ for (s = mtus[0]; s <= mtus[29]; s++) { size_t reclen; if (!TEST_int_eq(SSL_write(clnt_ssl, buf, s), (int)s)) goto end; reclen = BIO_read(sc_bio, buf, sizeof(buf)); if (debug) TEST_info("record %zu for payload %zu", reclen, s); for (i = 0; i < 30; i++) { /* DTLS_get_data_mtu() with record MTU 500+i returned mtus[i] ... */ if (!TEST_false(s <= mtus[i] && reclen > (size_t)(500 + i))) { /* * We sent a packet smaller than or equal to mtus[j] and * that made a record *larger* than the record MTU 500+j! */ TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d", cs, (unsigned long)s, (unsigned long)mtus[i], (unsigned long)reclen, 500 + i); goto end; } if (!TEST_false(s > mtus[i] && reclen <= (size_t)(500 + i))) { /* * We sent a *larger* packet than mtus[i] and that *still* * fits within the record MTU 500+i, so DTLS_get_data_mtu() * was overly pessimistic. */ TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d", cs, (unsigned long)s, (unsigned long)mtus[i], (unsigned long)reclen, 500 + i); goto end; } } } if (!TEST_ptr(clnt_sc = SSL_CONNECTION_FROM_SSL_ONLY(clnt_ssl))) goto end; rv = 1; if (SSL_READ_ETM(clnt_sc)) rv = 2; end: SSL_free(clnt_ssl); SSL_free(srvr_ssl); return rv; } static int run_mtu_tests(void) { SSL_CTX *ctx = NULL; STACK_OF(SSL_CIPHER) *ciphers; int i, ret = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method()))) goto end; SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback); SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback); SSL_CTX_set_security_level(ctx, 0); /* * We only care about iterating over each enc/mac; we don't want to * repeat the test for each auth/kx variant. So keep life simple and * only do (non-DH) PSK. */ if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "PSK"))) goto end; ciphers = SSL_CTX_get_ciphers(ctx); for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); const char *cipher_name = SSL_CIPHER_get_name(cipher); /* As noted above, only one test for each enc/mac variant. */ if (!HAS_PREFIX(cipher_name, "PSK-")) continue; if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 0), 0)) break; TEST_info("%s OK", cipher_name); if (ret == 1) continue; /* mtu_test() returns 2 if it used Encrypt-then-MAC */ if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 1), 0)) break; TEST_info("%s without EtM OK", cipher_name); } end: SSL_CTX_free(ctx); return ret; } static int test_server_mtu_larger_than_max_fragment_length(void) { SSL_CTX *ctx = NULL; SSL *srvr_ssl = NULL, *clnt_ssl = NULL; int rv = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method()))) goto end; SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback); SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback); #ifndef OPENSSL_NO_DH if (!TEST_true(SSL_CTX_set_dh_auto(ctx, 1))) goto end; #endif if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl, NULL, NULL))) goto end; SSL_set_options(srvr_ssl, SSL_OP_NO_QUERY_MTU); if (!TEST_true(DTLS_set_link_mtu(srvr_ssl, 1500))) goto end; SSL_set_tlsext_max_fragment_length(clnt_ssl, TLSEXT_max_fragment_length_512); if (!TEST_true(create_ssl_connection(srvr_ssl, clnt_ssl, SSL_ERROR_NONE))) goto end; rv = 1; end: SSL_free(clnt_ssl); SSL_free(srvr_ssl); SSL_CTX_free(ctx); return rv; } int setup_tests(void) { ADD_TEST(run_mtu_tests); ADD_TEST(test_server_mtu_larger_than_max_fragment_length); return 1; } void cleanup_tests(void) { bio_s_mempacket_test_free(); }
./openssl/test/quic_record_test_util.h
/* * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_RECORD_TEST_UTIL_H # define OSSL_RECORD_TEST_UTIL_H static int cmp_pkt_hdr(const QUIC_PKT_HDR *a, const QUIC_PKT_HDR *b, const unsigned char *b_data, size_t b_len, int cmp_data) { int ok = 1; if (b_data == NULL) { b_data = b->data; b_len = b->len; } if (!TEST_int_eq(a->type, b->type) || !TEST_int_eq(a->spin_bit, b->spin_bit) || !TEST_int_eq(a->key_phase, b->key_phase) || !TEST_int_eq(a->pn_len, b->pn_len) || !TEST_int_eq(a->partial, b->partial) || !TEST_int_eq(a->fixed, b->fixed) || !TEST_int_eq(a->unused, b->unused) || !TEST_int_eq(a->reserved, b->reserved) || !TEST_uint_eq(a->version, b->version) || !TEST_true(ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id)) || !TEST_true(ossl_quic_conn_id_eq(&a->src_conn_id, &b->src_conn_id)) || !TEST_mem_eq(a->pn, sizeof(a->pn), b->pn, sizeof(b->pn)) || !TEST_size_t_eq(a->token_len, b->token_len) || !TEST_uint64_t_eq(a->len, b->len)) ok = 0; if (a->token_len > 0 && b->token_len > 0 && !TEST_mem_eq(a->token, a->token_len, b->token, b->token_len)) ok = 0; if ((a->token_len == 0 && !TEST_ptr_null(a->token)) || (b->token_len == 0 && !TEST_ptr_null(b->token))) ok = 0; if (cmp_data && !TEST_mem_eq(a->data, a->len, b_data, b_len)) ok = 0; return ok; } #endif
./openssl/test/testutil/format_output.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "output.h" #include "tu_local.h" #include <string.h> #include <ctype.h> /* The size of memory buffers to display on failure */ #define MEM_BUFFER_SIZE (2000) #define MAX_STRING_WIDTH (80) #define BN_OUTPUT_SIZE (8) /* Output a diff header */ static void test_diff_header(const char *left, const char *right) { test_printf_stderr("--- %s\n", left); test_printf_stderr("+++ %s\n", right); } /* Formatted string output routines */ static void test_string_null_empty(const char *m, char c) { if (m == NULL) test_printf_stderr("%4s %c NULL\n", "", c); else test_printf_stderr("%4u:%c ''\n", 0u, c); } static void test_fail_string_common(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *m1, size_t l1, const char *m2, size_t l2) { const size_t width = (MAX_STRING_WIDTH - BIO_get_indent(bio_err) - 12) / 16 * 16; char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1]; char bdiff[MAX_STRING_WIDTH + 1]; size_t n1, n2, i; unsigned int cnt = 0, diff; test_fail_message_prefix(prefix, file, line, type, left, right, op); if (m1 == NULL) l1 = 0; if (m2 == NULL) l2 = 0; if (l1 == 0 && l2 == 0) { if ((m1 == NULL) == (m2 == NULL)) { test_string_null_empty(m1, ' '); } else { test_diff_header(left, right); test_string_null_empty(m1, '-'); test_string_null_empty(m2, '+'); } goto fin; } if (l1 != l2 || strncmp(m1, m2, l1) != 0) test_diff_header(left, right); while (l1 > 0 || l2 > 0) { n1 = n2 = 0; if (l1 > 0) { b1[n1 = l1 > width ? width : l1] = 0; for (i = 0; i < n1; i++) b1[i] = isprint((unsigned char)m1[i]) ? m1[i] : '.'; } if (l2 > 0) { b2[n2 = l2 > width ? width : l2] = 0; for (i = 0; i < n2; i++) b2[i] = isprint((unsigned char)m2[i]) ? m2[i] : '.'; } diff = 0; i = 0; if (n1 > 0 && n2 > 0) { const size_t j = n1 < n2 ? n1 : n2; for (; i < j; i++) if (m1[i] == m2[i]) { bdiff[i] = ' '; } else { bdiff[i] = '^'; diff = 1; } bdiff[i] = '\0'; } if (n1 == n2 && !diff) { test_printf_stderr("%4u: '%s'\n", cnt, n2 > n1 ? b2 : b1); } else { if (cnt == 0 && (m1 == NULL || *m1 == '\0')) test_string_null_empty(m1, '-'); else if (n1 > 0) test_printf_stderr("%4u:- '%s'\n", cnt, b1); if (cnt == 0 && (m2 == NULL || *m2 == '\0')) test_string_null_empty(m2, '+'); else if (n2 > 0) test_printf_stderr("%4u:+ '%s'\n", cnt, b2); if (diff && i > 0) test_printf_stderr("%4s %s\n", "", bdiff); } if (m1 != NULL) m1 += n1; if (m2 != NULL) m2 += n2; l1 -= n1; l2 -= n2; cnt += width; } fin: test_flush_stderr(); } /* * Wrapper routines so that the underlying code can be shared. * The first is the call from inside the test utilities when a conditional * fails. The second is the user's call to dump a string. */ void test_fail_string_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *m1, size_t l1, const char *m2, size_t l2) { test_fail_string_common(prefix, file, line, type, left, right, op, m1, l1, m2, l2); test_printf_stderr("\n"); } void test_output_string(const char *name, const char *m, size_t l) { test_fail_string_common("string", NULL, 0, NULL, NULL, NULL, name, m, l, m, l); } /* BIGNUM formatted output routines */ /* * A basic memory byte to hex digit converter with allowance for spacing * every so often. */ static void hex_convert_memory(const unsigned char *m, size_t n, char *b, size_t width) { size_t i; for (i = 0; i < n; i++) { const unsigned char c = *m++; *b++ = "0123456789abcdef"[c >> 4]; *b++ = "0123456789abcdef"[c & 15]; if (i % width == width - 1 && i != n - 1) *b++ = ' '; } *b = '\0'; } /* * Constants to define the number of bytes to display per line and the number * of characters these take. */ static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1) * BN_OUTPUT_SIZE; static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1) * (BN_OUTPUT_SIZE * 2 + 1) - 1; /* * Output the header line for the bignum */ static void test_bignum_header_line(void) { test_printf_stderr(" %*s\n", bn_chars + 6, "bit position"); } static const char *test_bignum_zero_null(const BIGNUM *bn) { if (bn != NULL) return BN_is_negative(bn) ? "-0" : "0"; return "NULL"; } /* * Print a bignum zero taking care to include the correct sign. * This routine correctly deals with a NULL bignum pointer as input. */ static void test_bignum_zero_print(const BIGNUM *bn, char sep) { const char *v = test_bignum_zero_null(bn); const char *suf = bn != NULL ? ": 0" : ""; test_printf_stderr("%c%*s%s\n", sep, bn_chars, v, suf); } /* * Convert a section of memory from inside a bignum into a displayable * string with appropriate visual aid spaces inserted. */ static int convert_bn_memory(const unsigned char *in, size_t bytes, char *out, int *lz, const BIGNUM *bn) { int n = bytes * 2, i; char *p = out, *q = NULL; const char *r; if (bn != NULL && !BN_is_zero(bn)) { hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE); if (*lz) { for (; *p == '0' || *p == ' '; p++) if (*p == '0') { q = p; *p = ' '; n--; } if (*p == '\0') { /* * in[bytes] is defined because we're converting a non-zero * number and we've not seen a non-zero yet. */ if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) { *lz = 0; *q = '-'; n++; } } else { *lz = 0; if (BN_is_negative(bn)) { /* * This is valid because we always convert more digits than * the number holds. */ *q = '-'; n++; } } } return n; } for (i = 0; i < n; i++) { *p++ = ' '; if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1) *p++ = ' '; } *p = '\0'; if (bn == NULL) r = "NULL"; else r = BN_is_negative(bn) ? "-0" : "0"; strcpy(p - strlen(r), r); return 0; } /* * Common code to display either one or two bignums, including the diff * pointers for changes (only when there are two). */ static void test_fail_bignum_common(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const BIGNUM *bn1, const BIGNUM *bn2) { const size_t bytes = bn_bytes; char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1]; char *p, bdiff[MAX_STRING_WIDTH + 1]; size_t l1, l2, n1, n2, i, len; unsigned int cnt, diff, real_diff; unsigned char *m1 = NULL, *m2 = NULL; int lz1 = 1, lz2 = 1; unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer; test_fail_message_prefix(prefix, file, line, type, left, right, op); l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0)); l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0)); if (l1 == 0 && l2 == 0) { if ((bn1 == NULL) == (bn2 == NULL)) { test_bignum_header_line(); test_bignum_zero_print(bn1, ' '); } else { test_diff_header(left, right); test_bignum_header_line(); test_bignum_zero_print(bn1, '-'); test_bignum_zero_print(bn2, '+'); } goto fin; } if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0) test_diff_header(left, right); test_bignum_header_line(); len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes; if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) { bufp = buffer; len = MEM_BUFFER_SIZE; test_printf_stderr("WARNING: these BIGNUMs have been truncated\n"); } if (bn1 != NULL) { m1 = bufp; BN_bn2binpad(bn1, m1, len); } if (bn2 != NULL) { m2 = bufp + len; BN_bn2binpad(bn2, m2, len); } while (len > 0) { cnt = 8 * (len - bytes); n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1); n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2); diff = real_diff = 0; i = 0; p = bdiff; for (i=0; b1[i] != '\0'; i++) if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') { *p++ = ' '; diff |= b1[i] != b2[i]; } else { *p++ = '^'; real_diff = diff = 1; } *p++ = '\0'; if (!diff) { test_printf_stderr(" %s:% 5d\n", n2 > n1 ? b2 : b1, cnt); } else { if (cnt == 0 && bn1 == NULL) test_printf_stderr("-%s\n", b1); else if (cnt == 0 || n1 > 0) test_printf_stderr("-%s:% 5d\n", b1, cnt); if (cnt == 0 && bn2 == NULL) test_printf_stderr("+%s\n", b2); else if (cnt == 0 || n2 > 0) test_printf_stderr("+%s:% 5d\n", b2, cnt); if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0)) && bn1 != NULL && bn2 != NULL) test_printf_stderr(" %s\n", bdiff); } if (m1 != NULL) m1 += bytes; if (m2 != NULL) m2 += bytes; len -= bytes; } fin: test_flush_stderr(); if (bufp != buffer) OPENSSL_free(bufp); } /* * Wrapper routines so that the underlying code can be shared. * The first two are calls from inside the test utilities when a conditional * fails. The third is the user's call to dump a bignum. */ void test_fail_bignum_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const BIGNUM *bn1, const BIGNUM *bn2) { test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2); test_printf_stderr("\n"); } void test_fail_bignum_mono_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const BIGNUM *bn) { test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn); test_printf_stderr("\n"); } void test_output_bignum(const char *name, const BIGNUM *bn) { if (bn == NULL || BN_is_zero(bn)) { test_printf_stderr("bignum: '%s' = %s\n", name, test_bignum_zero_null(bn)); } else if (BN_num_bytes(bn) <= BN_OUTPUT_SIZE) { unsigned char buf[BN_OUTPUT_SIZE]; char out[2 * sizeof(buf) + 1]; char *p = out; int n = BN_bn2bin(bn, buf); hex_convert_memory(buf, n, p, BN_OUTPUT_SIZE); while (*p == '0' && *++p != '\0') ; test_printf_stderr("bignum: '%s' = %s0x%s\n", name, BN_is_negative(bn) ? "-" : "", p); } else { test_fail_bignum_common("bignum", NULL, 0, NULL, NULL, NULL, name, bn, bn); } } /* Memory output routines */ /* * Handle zero length blocks of memory or NULL pointers to memory */ static void test_memory_null_empty(const unsigned char *m, char c) { if (m == NULL) test_printf_stderr("%4s %c%s\n", "", c, "NULL"); else test_printf_stderr("%04x %c%s\n", 0u, c, "empty"); } /* * Common code to display one or two blocks of memory. */ static void test_fail_memory_common(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const unsigned char *m1, size_t l1, const unsigned char *m2, size_t l2) { const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8; char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1]; char *p, bdiff[MAX_STRING_WIDTH + 1]; size_t n1, n2, i; unsigned int cnt = 0, diff; test_fail_message_prefix(prefix, file, line, type, left, right, op); if (m1 == NULL) l1 = 0; if (m2 == NULL) l2 = 0; if (l1 == 0 && l2 == 0) { if ((m1 == NULL) == (m2 == NULL)) { test_memory_null_empty(m1, ' '); } else { test_diff_header(left, right); test_memory_null_empty(m1, '-'); test_memory_null_empty(m2, '+'); } goto fin; } if (l1 != l2 || (m1 != m2 && memcmp(m1, m2, l1) != 0)) test_diff_header(left, right); while (l1 > 0 || l2 > 0) { n1 = n2 = 0; if (l1 > 0) { n1 = l1 > bytes ? bytes : l1; hex_convert_memory(m1, n1, b1, 8); } if (l2 > 0) { n2 = l2 > bytes ? bytes : l2; hex_convert_memory(m2, n2, b2, 8); } diff = 0; i = 0; p = bdiff; if (n1 > 0 && n2 > 0) { const size_t j = n1 < n2 ? n1 : n2; for (; i < j; i++) { if (m1[i] == m2[i]) { *p++ = ' '; *p++ = ' '; } else { *p++ = '^'; *p++ = '^'; diff = 1; } if (i % 8 == 7 && i != j - 1) *p++ = ' '; } *p++ = '\0'; } if (n1 == n2 && !diff) { test_printf_stderr("%04x: %s\n", cnt, b1); } else { if (cnt == 0 && (m1 == NULL || l1 == 0)) test_memory_null_empty(m1, '-'); else if (n1 > 0) test_printf_stderr("%04x:-%s\n", cnt, b1); if (cnt == 0 && (m2 == NULL || l2 == 0)) test_memory_null_empty(m2, '+'); else if (n2 > 0) test_printf_stderr("%04x:+%s\n", cnt, b2); if (diff && i > 0) test_printf_stderr("%4s %s\n", "", bdiff); } if (m1 != NULL) m1 += n1; if (m2 != NULL) m2 += n2; l1 -= n1; l2 -= n2; cnt += bytes; } fin: test_flush_stderr(); } /* * Wrapper routines so that the underlying code can be shared. * The first is the call from inside the test utilities when a conditional * fails. The second is the user's call to dump memory. */ void test_fail_memory_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const unsigned char *m1, size_t l1, const unsigned char *m2, size_t l2) { test_fail_memory_common(prefix, file, line, type, left, right, op, m1, l1, m2, l2); test_printf_stderr("\n"); } void test_output_memory(const char *name, const unsigned char *m, size_t l) { test_fail_memory_common("memory", NULL, 0, NULL, NULL, NULL, name, m, l, m, l); }
./openssl/test/testutil/main.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "output.h" #include "tu_local.h" int main(int argc, char *argv[]) { int ret = EXIT_FAILURE; int setup_res; test_open_streams(); if (!global_init()) { test_printf_stderr("Global init failed - aborting\n"); return ret; } if (!setup_test_framework(argc, argv)) goto end; if ((setup_res = setup_tests()) > 0) { ret = run_tests(argv[0]); cleanup_tests(); opt_check_usage(); } else if (setup_res == 0) { opt_help(test_get_options()); } end: ret = pulldown_test_framework(ret); test_close_streams(); return ret; }
./openssl/test/testutil/basic_output.c
/* * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "output.h" #include "tu_local.h" #include <openssl/crypto.h> #include <openssl/bio.h> /* These are available for any test program */ BIO *bio_out = NULL; BIO *bio_err = NULL; /* These are available for TAP output only (internally) */ static BIO *tap_out = NULL; static BIO *tap_err = NULL; #if defined(OPENSSL_THREADS) static CRYPTO_RWLOCK *io_lock = NULL; #endif void test_open_streams(void) { tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT); tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); #ifdef __VMS tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out); tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err); #endif tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out); tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err); bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out); bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err); BIO_set_prefix(bio_out, "# "); BIO_set_prefix(bio_err, "# "); #if defined(OPENSSL_THREADS) io_lock = CRYPTO_THREAD_lock_new(); #endif OPENSSL_assert(bio_out != NULL); OPENSSL_assert(bio_err != NULL); #if defined(OPENSSL_THREADS) OPENSSL_assert(io_lock != NULL); #endif } void test_adjust_streams_tap_level(int level) { BIO_set_indent(tap_out, level); BIO_set_indent(tap_err, level); } void test_close_streams(void) { /* * The rest of the chain is freed by the BIO_free_all() calls below, so * we only need to free the last one in the bio_out and bio_err chains. */ BIO_free(bio_out); BIO_free(bio_err); BIO_free_all(tap_out); BIO_free_all(tap_err); #if defined(OPENSSL_THREADS) CRYPTO_THREAD_lock_free(io_lock); #endif } static ossl_inline void test_io_lock(void) { #if defined(OPENSSL_THREADS) OPENSSL_assert(CRYPTO_THREAD_write_lock(io_lock) > 0); #endif } static ossl_inline void test_io_unlock(void) { #if defined(OPENSSL_THREADS) CRYPTO_THREAD_unlock(io_lock); #endif } int test_vprintf_stdout(const char *fmt, va_list ap) { int r; test_io_lock(); r = BIO_vprintf(bio_out, fmt, ap); test_io_unlock(); return r; } int test_vprintf_stderr(const char *fmt, va_list ap) { int r; test_io_lock(); r = BIO_vprintf(bio_err, fmt, ap); test_io_unlock(); return r; } int test_flush_stdout(void) { int r; test_io_lock(); r = BIO_flush(bio_out); test_io_unlock(); return r; } int test_flush_stderr(void) { int r; test_io_lock(); r = BIO_flush(bio_err); test_io_unlock(); return r; } int test_vprintf_tapout(const char *fmt, va_list ap) { int r; test_io_lock(); r = BIO_vprintf(tap_out, fmt, ap); test_io_unlock(); return r; } int test_vprintf_taperr(const char *fmt, va_list ap) { int r; test_io_lock(); r = BIO_vprintf(tap_err, fmt, ap); test_io_unlock(); return r; } int test_flush_tapout(void) { int r; test_io_lock(); r = BIO_flush(tap_out); test_io_unlock(); return r; } int test_flush_taperr(void) { int r; test_io_lock(); r = BIO_flush(tap_err); test_io_unlock(); return r; }
./openssl/test/testutil/cb.c
/* * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "output.h" #include "tu_local.h" int openssl_error_cb(const char *str, size_t len, void *u) { return test_printf_stderr("%s", str); }
./openssl/test/testutil/test_options.c
/* * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "tu_local.h" /* An overridable list of command line options */ const OPTIONS *test_get_options(void) { static const OPTIONS default_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { NULL } }; return default_options; }
./openssl/test/testutil/provider.c
/* * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include <ctype.h> #include <openssl/provider.h> #include <openssl/core_names.h> #include <string.h> int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov, const char *config_file, OSSL_PROVIDER **provider, const char *module_name) { OSSL_LIB_CTX *new_libctx = NULL; if (libctx != NULL) { if ((new_libctx = *libctx = OSSL_LIB_CTX_new()) == NULL) { opt_printf_stderr("Failed to create libctx\n"); goto err; } } if (default_null_prov != NULL && (*default_null_prov = OSSL_PROVIDER_load(NULL, "null")) == NULL) { opt_printf_stderr("Failed to load null provider into default libctx\n"); goto err; } if (config_file != NULL && !OSSL_LIB_CTX_load_config(new_libctx, config_file)) { opt_printf_stderr("Error loading config from file %s\n", config_file); goto err; } if (provider != NULL && module_name != NULL && (*provider = OSSL_PROVIDER_load(new_libctx, module_name)) == NULL) { opt_printf_stderr("Failed to load provider %s\n", module_name); goto err; } return 1; err: ERR_print_errors_fp(stderr); return 0; } int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov, OSSL_PROVIDER **provider, int argn, const char *usage) { const char *module_name; if (!TEST_ptr(module_name = test_get_argument(argn))) { TEST_error("usage: <prog> %s", usage); return 0; } if (strcmp(module_name, "none") == 0) return 1; return test_get_libctx(libctx, default_null_prov, test_get_argument(argn + 1), provider, module_name); } typedef struct { int major, minor, patch; } FIPS_VERSION; /* * Query the FIPS provider to determine it's version number. * Returns 1 if the version is retrieved correctly, 0 if the FIPS provider isn't * loaded and -1 on error. */ static int fips_provider_version(OSSL_LIB_CTX *libctx, FIPS_VERSION *vers) { OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; OSSL_PROVIDER *fips_prov; char *vs; if (!OSSL_PROVIDER_available(libctx, "fips")) return 0; *params = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION, &vs, 0); if ((fips_prov = OSSL_PROVIDER_load(libctx, "fips")) == NULL) return -1; if (!OSSL_PROVIDER_get_params(fips_prov, params) || sscanf(vs, "%d.%d.%d", &vers->major, &vers->minor, &vers->patch) != 3) goto err; if (!OSSL_PROVIDER_unload(fips_prov)) return -1; return 1; err: OSSL_PROVIDER_unload(fips_prov); return -1; } int fips_provider_version_eq(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return major == prov.major && minor == prov.minor && patch == prov.patch; } int fips_provider_version_ne(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return major != prov.major || minor != prov.minor || patch != prov.patch; } int fips_provider_version_le(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return prov.major < major || (prov.major == major && (prov.minor < minor || (prov.minor == minor && prov.patch <= patch))); } int fips_provider_version_lt(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return prov.major < major || (prov.major == major && (prov.minor < minor || (prov.minor == minor && prov.patch < patch))); } int fips_provider_version_gt(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return prov.major > major || (prov.major == major && (prov.minor > minor || (prov.minor == minor && prov.patch > patch))); } int fips_provider_version_ge(OSSL_LIB_CTX *libctx, int major, int minor, int patch) { FIPS_VERSION prov; int res; if ((res = fips_provider_version(libctx, &prov)) <= 0) return res == 0; return prov.major > major || (prov.major == major && (prov.minor > minor || (prov.minor == minor && prov.patch >= patch))); } int fips_provider_version_match(OSSL_LIB_CTX *libctx, const char *versions) { const char *p; int major, minor, patch, r; enum { MODE_EQ, MODE_NE, MODE_LE, MODE_LT, MODE_GT, MODE_GE } mode; while (*versions != '\0') { for (; isspace((unsigned char)(*versions)); versions++) continue; if (*versions == '\0') break; for (p = versions; *versions != '\0' && !isspace((unsigned char)(*versions)); versions++) continue; if (*p == '!') { mode = MODE_NE; p++; } else if (*p == '=') { mode = MODE_EQ; p++; } else if (*p == '<' && p[1] == '=') { mode = MODE_LE; p += 2; } else if (*p == '>' && p[1] == '=') { mode = MODE_GE; p += 2; } else if (*p == '<') { mode = MODE_LT; p++; } else if (*p == '>') { mode = MODE_GT; p++; } else if (isdigit((unsigned char)*p)) { mode = MODE_EQ; } else { TEST_info("Error matching FIPS version: mode %s\n", p); return -1; } if (sscanf(p, "%d.%d.%d", &major, &minor, &patch) != 3) { TEST_info("Error matching FIPS version: version %s\n", p); return -1; } switch (mode) { case MODE_EQ: r = fips_provider_version_eq(libctx, major, minor, patch); break; case MODE_NE: r = fips_provider_version_ne(libctx, major, minor, patch); break; case MODE_LE: r = fips_provider_version_le(libctx, major, minor, patch); break; case MODE_LT: r = fips_provider_version_lt(libctx, major, minor, patch); break; case MODE_GT: r = fips_provider_version_gt(libctx, major, minor, patch); break; case MODE_GE: r = fips_provider_version_ge(libctx, major, minor, patch); break; } if (r < 0) { TEST_info("Error matching FIPS version: internal error\n"); return -1; } if (r == 0) return 0; } return 1; }
./openssl/test/testutil/tests.c
/* * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "output.h" #include "tu_local.h" #include <errno.h> #include <string.h> #include <ctype.h> #include <openssl/asn1.h> /* * Output a failed test first line. * All items are optional are generally not printed if passed as NULL. * The special cases are for prefix where "ERROR" is assumed and for left * and right where a non-failure message is produced if either is NULL. */ void test_fail_message_prefix(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op) { test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR"); if (type) test_printf_stderr("(%s) ", type); if (op != NULL) { if (left != NULL && right != NULL) test_printf_stderr("'%s %s %s' failed", left, op, right); else test_printf_stderr("'%s'", op); } if (file != NULL) { test_printf_stderr(" @ %s:%d", file, line); } test_printf_stderr("\n"); } /* * A common routine to output test failure messages. Generally this should not * be called directly, rather it should be called by the following functions. * * |desc| is a printf formatted description with arguments |args| that is * supplied by the user and |desc| can be NULL. |type| is the data type * that was tested (int, char, ptr, ...). |fmt| is a system provided * printf format with following arguments that spell out the failure * details i.e. the actual values compared and the operator used. * * The typical use for this is from an utility test function: * * int test6(const char *file, int line, int n) { * if (n != 6) { * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6); * return 0; * } * return 1; * } * * calling test6(3, "oops") will return 0 and produce out along the lines of: * FAIL oops: (int) value 3 is not 6\n */ static void test_fail_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *fmt, ...) PRINTF_FORMAT(8, 9); static void test_fail_message_va(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *fmt, va_list ap) { test_fail_message_prefix(prefix, file, line, type, left, right, op); if (fmt != NULL) { test_vprintf_stderr(fmt, ap); test_printf_stderr("\n"); } test_flush_stderr(); } static void test_fail_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *fmt, ...) { va_list ap; va_start(ap, fmt); test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap); va_end(ap); } void test_info_c90(const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); } void test_info(const char *file, int line, const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); } void test_error_c90(const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); test_printf_stderr("\n"); } void test_error(const char *file, int line, const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); test_printf_stderr("\n"); } void test_perror(const char *s) { /* * Using openssl_strerror_r causes linking issues since it isn't * exported from libcrypto.so */ TEST_error("%s: %s", s, strerror(errno)); } void test_note(const char *fmt, ...) { if (fmt != NULL) { va_list ap; va_start(ap, fmt); test_vprintf_stderr(fmt, ap); va_end(ap); test_printf_stderr("\n"); } test_flush_stderr(); } int test_skip(const char *file, int line, const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va("SKIP", file, line, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); return TEST_SKIP_CODE; } int test_skip_c90(const char *desc, ...) { va_list ap; va_start(ap, desc); test_fail_message_va("SKIP", NULL, -1, NULL, NULL, NULL, NULL, desc, ap); va_end(ap); test_printf_stderr("\n"); return TEST_SKIP_CODE; } void test_openssl_errors(void) { ERR_print_errors_cb(openssl_error_cb, NULL); ERR_clear_error(); } /* * Define some comparisons between pairs of various types. * These functions return 1 if the test is true. * Otherwise, they return 0 and pretty-print diagnostics. * * In each case the functions produced are: * int test_name_eq(const type t1, const type t2, const char *desc, ...); * int test_name_ne(const type t1, const type t2, const char *desc, ...); * int test_name_lt(const type t1, const type t2, const char *desc, ...); * int test_name_le(const type t1, const type t2, const char *desc, ...); * int test_name_gt(const type t1, const type t2, const char *desc, ...); * int test_name_ge(const type t1, const type t2, const char *desc, ...); * * The t1 and t2 arguments are to be compared for equality, inequality, * less than, less than or equal to, greater than and greater than or * equal to respectively. If the specified condition holds, the functions * return 1. If the condition does not hold, the functions print a diagnostic * message and return 0. * * The desc argument is a printf format string followed by its arguments and * this is included in the output if the condition being tested for is false. */ #define DEFINE_COMPARISON(type, name, opname, op, fmt, cast) \ int test_ ## name ## _ ## opname(const char *file, int line, \ const char *s1, const char *s2, \ const type t1, const type t2) \ { \ if (t1 op t2) \ return 1; \ test_fail_message(NULL, file, line, #type, s1, s2, #op, \ "[" fmt "] compared to [" fmt "]", \ (cast)t1, (cast)t2); \ return 0; \ } #define DEFINE_COMPARISONS(type, name, fmt, cast) \ DEFINE_COMPARISON(type, name, eq, ==, fmt, cast) \ DEFINE_COMPARISON(type, name, ne, !=, fmt, cast) \ DEFINE_COMPARISON(type, name, lt, <, fmt, cast) \ DEFINE_COMPARISON(type, name, le, <=, fmt, cast) \ DEFINE_COMPARISON(type, name, gt, >, fmt, cast) \ DEFINE_COMPARISON(type, name, ge, >=, fmt, cast) DEFINE_COMPARISONS(int, int, "%d", int) DEFINE_COMPARISONS(unsigned int, uint, "%u", unsigned int) DEFINE_COMPARISONS(char, char, "%c", char) DEFINE_COMPARISONS(unsigned char, uchar, "%u", unsigned char) DEFINE_COMPARISONS(long, long, "%ld", long) DEFINE_COMPARISONS(unsigned long, ulong, "%lu", unsigned long) DEFINE_COMPARISONS(int64_t, int64_t, "%lld", long long) DEFINE_COMPARISONS(uint64_t, uint64_t, "%llu", unsigned long long) DEFINE_COMPARISONS(size_t, size_t, "%zu", size_t) DEFINE_COMPARISONS(double, double, "%g", double) DEFINE_COMPARISON(void *, ptr, eq, ==, "%p", void *) DEFINE_COMPARISON(void *, ptr, ne, !=, "%p", void *) int test_ptr_null(const char *file, int line, const char *s, const void *p) { if (p == NULL) return 1; test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p); return 0; } int test_ptr(const char *file, int line, const char *s, const void *p) { if (p != NULL) return 1; test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p); return 0; } int test_true(const char *file, int line, const char *s, int b) { if (b) return 1; test_fail_message(NULL, file, line, "bool", s, "true", "==", "false"); return 0; } int test_false(const char *file, int line, const char *s, int b) { if (!b) return 1; test_fail_message(NULL, file, line, "bool", s, "false", "==", "true"); return 0; } int test_str_eq(const char *file, int line, const char *st1, const char *st2, const char *s1, const char *s2) { if (s1 == NULL && s2 == NULL) return 1; if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) { test_fail_string_message(NULL, file, line, "string", st1, st2, "==", s1, s1 == NULL ? 0 : strlen(s1), s2, s2 == NULL ? 0 : strlen(s2)); return 0; } return 1; } int test_str_ne(const char *file, int line, const char *st1, const char *st2, const char *s1, const char *s2) { if ((s1 == NULL) ^ (s2 == NULL)) return 1; if (s1 == NULL || strcmp(s1, s2) == 0) { test_fail_string_message(NULL, file, line, "string", st1, st2, "!=", s1, s1 == NULL ? 0 : strlen(s1), s2, s2 == NULL ? 0 : strlen(s2)); return 0; } return 1; } int test_strn_eq(const char *file, int line, const char *st1, const char *st2, const char *s1, size_t n1, const char *s2, size_t n2) { if (s1 == NULL && s2 == NULL) return 1; if (n1 != n2 || s1 == NULL || s2 == NULL || strncmp(s1, s2, n1) != 0) { test_fail_string_message(NULL, file, line, "string", st1, st2, "==", s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1), s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2)); return 0; } return 1; } int test_strn_ne(const char *file, int line, const char *st1, const char *st2, const char *s1, size_t n1, const char *s2, size_t n2) { if ((s1 == NULL) ^ (s2 == NULL)) return 1; if (n1 != n2 || s1 == NULL || strncmp(s1, s2, n1) == 0) { test_fail_string_message(NULL, file, line, "string", st1, st2, "!=", s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1), s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2)); return 0; } return 1; } int test_mem_eq(const char *file, int line, const char *st1, const char *st2, const void *s1, size_t n1, const void *s2, size_t n2) { if (s1 == NULL && s2 == NULL) return 1; if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) { test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==", s1, n1, s2, n2); return 0; } return 1; } int test_mem_ne(const char *file, int line, const char *st1, const char *st2, const void *s1, size_t n1, const void *s2, size_t n2) { if ((s1 == NULL) ^ (s2 == NULL)) return 1; if (n1 != n2) return 1; if (s1 == NULL || memcmp(s1, s2, n1) == 0) { test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=", s1, n1, s2, n2); return 0; } return 1; } #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \ int test_BN_ ## opname(const char *file, int line, \ const char *s1, const char *s2, \ const BIGNUM *t1, const BIGNUM *t2) \ { \ if (BN_cmp(t1, t2) op 0) \ return 1; \ test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \ #op, t1, t2); \ return 0; \ } \ int test_BN_ ## opname ## _zero(const char *file, int line, \ const char *s, const BIGNUM *a) \ { \ if (a != NULL &&(zero_cond)) \ return 1; \ test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \ s, "0", #op, a); \ return 0; \ } DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a)) DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a)) DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a)) DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a)) DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a)) DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a)) int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a) { if (a != NULL && BN_is_one(a)) return 1; test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a); return 0; } int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a) { if (a != NULL && BN_is_odd(a)) return 1; test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a); return 0; } int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a) { if (a != NULL && !BN_is_odd(a)) return 1; test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s, a); return 0; } int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws, const BIGNUM *a, BN_ULONG w) { BIGNUM *bw; if (a != NULL && BN_is_word(a, w)) return 1; if ((bw = BN_new()) != NULL) BN_set_word(bw, w); test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw); BN_free(bw); return 0; } int test_BN_abs_eq_word(const char *file, int line, const char *bns, const char *ws, const BIGNUM *a, BN_ULONG w) { BIGNUM *bw, *aa; if (a != NULL && BN_abs_is_word(a, w)) return 1; if ((aa = BN_dup(a)) != NULL) BN_set_negative(aa, 0); if ((bw = BN_new()) != NULL) BN_set_word(bw, w); test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==", aa, bw); BN_free(bw); BN_free(aa); return 0; } static const char *print_time(const ASN1_TIME *t) { return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t); } #define DEFINE_TIME_T_COMPARISON(opname, op) \ int test_time_t_ ## opname(const char *file, int line, \ const char *s1, const char *s2, \ const time_t t1, const time_t t2) \ { \ ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \ ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \ int r = at1 != NULL && at2 != NULL \ && ASN1_TIME_compare(at1, at2) op 0; \ if (!r) \ test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \ "[%s] compared to [%s]", \ print_time(at1), print_time(at2)); \ ASN1_STRING_free(at1); \ ASN1_STRING_free(at2); \ return r; \ } DEFINE_TIME_T_COMPARISON(eq, ==) DEFINE_TIME_T_COMPARISON(ne, !=) DEFINE_TIME_T_COMPARISON(gt, >) DEFINE_TIME_T_COMPARISON(ge, >=) DEFINE_TIME_T_COMPARISON(lt, <) DEFINE_TIME_T_COMPARISON(le, <=)
./openssl/test/testutil/fake_random.c
/* * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * https://www.openssl.org/source/license.html * or in the file LICENSE in the source distribution. */ #include <string.h> #include <openssl/core_names.h> #include <openssl/rand.h> #include <openssl/provider.h> #include "../include/crypto/evp.h" #include "../../crypto/evp/evp_local.h" #include "../testutil.h" typedef struct { fake_random_generate_cb *cb; int state; const char *name; EVP_RAND_CTX *ctx; } FAKE_RAND; static OSSL_FUNC_rand_newctx_fn fake_rand_newctx; static OSSL_FUNC_rand_freectx_fn fake_rand_freectx; static OSSL_FUNC_rand_instantiate_fn fake_rand_instantiate; static OSSL_FUNC_rand_uninstantiate_fn fake_rand_uninstantiate; static OSSL_FUNC_rand_generate_fn fake_rand_generate; static OSSL_FUNC_rand_gettable_ctx_params_fn fake_rand_gettable_ctx_params; static OSSL_FUNC_rand_get_ctx_params_fn fake_rand_get_ctx_params; static OSSL_FUNC_rand_enable_locking_fn fake_rand_enable_locking; static void *fake_rand_newctx(void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch) { FAKE_RAND *r = OPENSSL_zalloc(sizeof(*r)); if (r != NULL) r->state = EVP_RAND_STATE_UNINITIALISED; return r; } static void fake_rand_freectx(void *vrng) { OPENSSL_free(vrng); } static int fake_rand_instantiate(void *vrng, ossl_unused unsigned int strength, ossl_unused int prediction_resistance, ossl_unused const unsigned char *pstr, size_t pstr_len, ossl_unused const OSSL_PARAM params[]) { FAKE_RAND *frng = (FAKE_RAND *)vrng; frng->state = EVP_RAND_STATE_READY; return 1; } static int fake_rand_uninstantiate(void *vrng) { FAKE_RAND *frng = (FAKE_RAND *)vrng; frng->state = EVP_RAND_STATE_UNINITIALISED; return 1; } static int fake_rand_generate(void *vrng, unsigned char *out, size_t outlen, unsigned int strength, int prediction_resistance, const unsigned char *adin, size_t adinlen) { FAKE_RAND *frng = (FAKE_RAND *)vrng; size_t l; uint32_t r; if (frng->cb != NULL) return (*frng->cb)(out, outlen, frng->name, frng->ctx); while (outlen > 0) { r = test_random(); l = outlen < sizeof(r) ? outlen : sizeof(r); memcpy(out, &r, l); out += l; outlen -= l; } return 1; } static int fake_rand_enable_locking(void *vrng) { return 1; } static int fake_rand_get_ctx_params(ossl_unused void *vrng, OSSL_PARAM params[]) { FAKE_RAND *frng = (FAKE_RAND *)vrng; OSSL_PARAM *p; p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE); if (p != NULL && !OSSL_PARAM_set_int(p, frng->state)) return 0; p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH); if (p != NULL && !OSSL_PARAM_set_int(p, 256)) return 0; p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX)) return 0; return 1; } static const OSSL_PARAM *fake_rand_gettable_ctx_params(ossl_unused void *vrng, ossl_unused void *provctx) { static const OSSL_PARAM known_gettable_ctx_params[] = { OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), OSSL_PARAM_END }; return known_gettable_ctx_params; } static const OSSL_DISPATCH fake_rand_functions[] = { { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fake_rand_newctx }, { OSSL_FUNC_RAND_FREECTX, (void (*)(void))fake_rand_freectx }, { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fake_rand_instantiate }, { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fake_rand_uninstantiate }, { OSSL_FUNC_RAND_GENERATE, (void (*)(void))fake_rand_generate }, { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fake_rand_enable_locking }, { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, (void(*)(void))fake_rand_gettable_ctx_params }, { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))fake_rand_get_ctx_params }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM fake_rand_rand[] = { { "FAKE", "provider=fake", fake_rand_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *fake_rand_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; switch (operation_id) { case OSSL_OP_RAND: return fake_rand_rand; } return NULL; } /* Functions we provide to the core */ static const OSSL_DISPATCH fake_rand_method[] = { { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rand_query }, OSSL_DISPATCH_END }; static int fake_rand_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new())) return 0; *out = fake_rand_method; return 1; } static int check_rng(EVP_RAND_CTX *rng, const char *name) { FAKE_RAND *f; if (!TEST_ptr(rng)) { TEST_info("random: %s", name); return 0; } f = rng->algctx; f->name = name; f->ctx = rng; return 1; } OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx) { OSSL_PROVIDER *p; if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rand", fake_rand_provider_init)) || !TEST_true(RAND_set_DRBG_type(libctx, "fake", NULL, NULL, NULL)) || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rand", 1))) return NULL; /* Ensure that the fake rand is initialized. */ if (!TEST_true(check_rng(RAND_get0_primary(libctx), "primary")) || !TEST_true(check_rng(RAND_get0_private(libctx), "private")) || !TEST_true(check_rng(RAND_get0_public(libctx), "public"))) { OSSL_PROVIDER_unload(p); return NULL; } return p; } void fake_rand_finish(OSSL_PROVIDER *p) { OSSL_PROVIDER_unload(p); } void fake_rand_set_callback(EVP_RAND_CTX *rng, int (*cb)(unsigned char *out, size_t outlen, const char *name, EVP_RAND_CTX *ctx)) { if (rng != NULL) ((FAKE_RAND *)rng->algctx)->cb = cb; } void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx, int (*cb)(unsigned char *out, size_t outlen, const char *name, EVP_RAND_CTX *ctx)) { fake_rand_set_callback(RAND_get0_private(libctx), cb); fake_rand_set_callback(RAND_get0_public(libctx), cb); }
./openssl/test/testutil/options.c
/* * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "internal/nelem.h" #include "tu_local.h" #include "output.h" static int used[100] = { 0 }; int test_skip_common_options(void) { OPTION_CHOICE_DEFAULT o; while ((o = (OPTION_CHOICE_DEFAULT)opt_next()) != OPT_EOF) { switch (o) { case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } return 1; } size_t test_get_argument_count(void) { return opt_num_rest(); } char *test_get_argument(size_t n) { char **argv = opt_rest(); OPENSSL_assert(n < sizeof(used)); if ((int)n >= opt_num_rest() || argv == NULL) return NULL; used[n] = 1; return argv[n]; } void opt_check_usage(void) { int i; char **argv = opt_rest(); int n, arg_count = opt_num_rest(); if (arg_count > (int)OSSL_NELEM(used)) n = (int)OSSL_NELEM(used); else n = arg_count; for (i = 0; i < n; i++) { if (used[i] == 0) test_printf_stderr("Warning ignored command-line argument %d: %s\n", i, argv[i]); } if (i < arg_count) test_printf_stderr("Warning arguments %d and later unchecked\n", i); } int opt_printf_stderr(const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = test_vprintf_stderr(fmt, ap); va_end(ap); return ret; }
./openssl/test/testutil/driver.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" #include "output.h" #include "tu_local.h" #include <string.h> #include <assert.h> #include "internal/nelem.h" #include <openssl/bio.h> #include "platform.h" /* From libapps */ #if defined(_WIN32) && !defined(__BORLANDC__) # define strdup _strdup #endif /* * Declares the structures needed to register each test case function. */ typedef struct test_info { const char *test_case_name; int (*test_fn) (void); int (*param_test_fn)(int idx); int num; /* flags */ int subtest:1; } TEST_INFO; static TEST_INFO all_tests[1024]; static int num_tests = 0; static int show_list = 0; static int single_test = -1; static int single_iter = -1; static int level = 0; static int seed = 0; static int rand_order = 0; /* * A parameterised test runs a loop of test cases. * |num_test_cases| counts the total number of non-subtest test cases * across all tests. */ static int num_test_cases = 0; static int process_shared_options(void); void add_test(const char *test_case_name, int (*test_fn) (void)) { assert(num_tests != OSSL_NELEM(all_tests)); all_tests[num_tests].test_case_name = test_case_name; all_tests[num_tests].test_fn = test_fn; all_tests[num_tests].num = -1; ++num_tests; ++num_test_cases; } void add_all_tests(const char *test_case_name, int(*test_fn)(int idx), int num, int subtest) { assert(num_tests != OSSL_NELEM(all_tests)); all_tests[num_tests].test_case_name = test_case_name; all_tests[num_tests].param_test_fn = test_fn; all_tests[num_tests].num = num; all_tests[num_tests].subtest = subtest; ++num_tests; if (subtest) ++num_test_cases; else num_test_cases += num; } static int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } static void set_seed(int s) { seed = s; if (seed <= 0) seed = (int)time(NULL); test_random_seed(seed); } int setup_test_framework(int argc, char *argv[]) { char *test_rand_order = getenv("OPENSSL_TEST_RAND_ORDER"); char *test_rand_seed = getenv("OPENSSL_TEST_RAND_SEED"); char *TAP_levels = getenv("HARNESS_OSSL_LEVEL"); if (TAP_levels != NULL) level = 4 * atoi(TAP_levels); test_adjust_streams_tap_level(level); if (test_rand_order != NULL) { rand_order = 1; set_seed(atoi(test_rand_order)); } else if (test_rand_seed != NULL) { set_seed(atoi(test_rand_seed)); } else { set_seed(0); } #if defined(OPENSSL_SYS_VMS) && defined(__DECC) argv = copy_argv(&argc, argv); #elif defined(_WIN32) /* * Replace argv[] with UTF-8 encoded strings. */ win32_utf8argv(&argc, &argv); #endif if (!opt_init(argc, argv, test_get_options())) return 0; return 1; } /* * This can only be called after setup() has run, since num_tests and * all_tests[] are setup at this point */ static int check_single_test_params(char *name, char *testname, char *itname) { if (name != NULL) { int i; for (i = 0; i < num_tests; ++i) { if (strcmp(name, all_tests[i].test_case_name) == 0) { single_test = 1 + i; break; } } if (i >= num_tests) single_test = atoi(name); } /* if only iteration is specified, assume we want the first test */ if (single_test == -1 && single_iter != -1) single_test = 1; if (single_test != -1) { if (single_test < 1 || single_test > num_tests) { test_printf_stderr("Invalid -%s value " "(Value must be a valid test name OR a value between %d..%d)\n", testname, 1, num_tests); return 0; } } if (single_iter != -1) { if (all_tests[single_test - 1].num == -1) { test_printf_stderr("-%s option is not valid for test %d:%s\n", itname, single_test, all_tests[single_test - 1].test_case_name); return 0; } else if (single_iter < 1 || single_iter > all_tests[single_test - 1].num) { test_printf_stderr("Invalid -%s value for test %d:%s\t" "(Value must be in the range %d..%d)\n", itname, single_test, all_tests[single_test - 1].test_case_name, 1, all_tests[single_test - 1].num); return 0; } } return 1; } static int process_shared_options(void) { OPTION_CHOICE_DEFAULT o; int value; int ret = -1; char *flag_test = ""; char *flag_iter = ""; char *testname = NULL; opt_begin(); while ((o = opt_next()) != OPT_EOF) { switch (o) { /* Ignore any test options at this level */ default: break; case OPT_ERR: return ret; case OPT_TEST_HELP: opt_help(test_get_options()); return 0; case OPT_TEST_LIST: show_list = 1; break; case OPT_TEST_SINGLE: flag_test = opt_flag(); testname = opt_arg(); break; case OPT_TEST_ITERATION: flag_iter = opt_flag(); if (!opt_int(opt_arg(), &single_iter)) goto end; break; case OPT_TEST_INDENT: if (!opt_int(opt_arg(), &value)) goto end; level = 4 * value; test_adjust_streams_tap_level(level); break; case OPT_TEST_SEED: if (!opt_int(opt_arg(), &value)) goto end; set_seed(value); break; } } if (!check_single_test_params(testname, flag_test, flag_iter)) goto end; ret = 1; end: return ret; } int pulldown_test_framework(int ret) { set_test_title(NULL); return ret; } static void finalize(int success) { if (success) ERR_clear_error(); else ERR_print_errors_cb(openssl_error_cb, NULL); } static char *test_title = NULL; void set_test_title(const char *title) { free(test_title); test_title = title == NULL ? NULL : strdup(title); } PRINTF_FORMAT(2, 3) static void test_verdict(int verdict, const char *description, ...) { va_list ap; test_flush_stdout(); test_flush_stderr(); if (verdict == 0) { if (rand_order) test_printf_tapout("# OPENSSL_TEST_RAND_ORDER=%d\n", seed); else test_printf_tapout("# OPENSSL_TEST_RAND_SEED=%d\n", seed); } test_printf_tapout("%s ", verdict != 0 ? "ok" : "not ok"); va_start(ap, description); test_vprintf_tapout(description, ap); va_end(ap); if (verdict == TEST_SKIP_CODE) test_printf_tapout(" # skipped"); test_printf_tapout("\n"); test_flush_tapout(); } int run_tests(const char *test_prog_name) { int num_failed = 0; int verdict = 1; int ii, i, jj, j, jstep; int test_case_count = 0; int subtest_case_count = 0; int permute[OSSL_NELEM(all_tests)]; i = process_shared_options(); if (i == 0) return EXIT_SUCCESS; if (i == -1) return EXIT_FAILURE; if (num_tests < 1) { test_printf_tapout("1..0 # Skipped: %s\n", test_prog_name); } else if (show_list == 0 && single_test == -1) { if (level > 0) { test_printf_stdout("Subtest: %s\n", test_prog_name); test_flush_stdout(); } test_printf_tapout("1..%d\n", num_test_cases); } test_flush_tapout(); for (i = 0; i < num_tests; i++) permute[i] = i; if (rand_order != 0) for (i = num_tests - 1; i >= 1; i--) { j = test_random() % (1 + i); ii = permute[j]; permute[j] = permute[i]; permute[i] = ii; } for (ii = 0; ii != num_tests; ++ii) { i = permute[ii]; if (single_test != -1 && ((i+1) != single_test)) { continue; } else if (show_list) { if (all_tests[i].num != -1) { test_printf_tapout("%d - %s (%d..%d)\n", ii + 1, all_tests[i].test_case_name, 1, all_tests[i].num); } else { test_printf_tapout("%d - %s\n", ii + 1, all_tests[i].test_case_name); } test_flush_tapout(); } else if (all_tests[i].num == -1) { set_test_title(all_tests[i].test_case_name); ERR_clear_error(); verdict = all_tests[i].test_fn(); finalize(verdict != 0); test_verdict(verdict, "%d - %s", test_case_count + 1, test_title); if (verdict == 0) num_failed++; test_case_count++; } else { verdict = TEST_SKIP_CODE; set_test_title(all_tests[i].test_case_name); if (all_tests[i].subtest) { level += 4; test_adjust_streams_tap_level(level); if (single_iter == -1) { test_printf_stdout("Subtest: %s\n", test_title); test_printf_tapout("%d..%d\n", 1, all_tests[i].num); test_flush_stdout(); test_flush_tapout(); } } j = -1; if (rand_order == 0 || all_tests[i].num < 3) jstep = 1; else do jstep = test_random() % all_tests[i].num; while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1); for (jj = 0; jj < all_tests[i].num; jj++) { int v; j = (j + jstep) % all_tests[i].num; if (single_iter != -1 && ((jj + 1) != single_iter)) continue; ERR_clear_error(); v = all_tests[i].param_test_fn(j); if (v == 0) { verdict = 0; } else if (v != TEST_SKIP_CODE && verdict != 0) { verdict = 1; } finalize(v != 0); if (all_tests[i].subtest) test_verdict(v, "%d - iteration %d", subtest_case_count + 1, j + 1); else test_verdict(v, "%d - %s - iteration %d", test_case_count + subtest_case_count + 1, test_title, j + 1); subtest_case_count++; } if (all_tests[i].subtest) { level -= 4; test_adjust_streams_tap_level(level); } if (verdict == 0) ++num_failed; if (all_tests[i].num == -1 || all_tests[i].subtest) test_verdict(verdict, "%d - %s", test_case_count + 1, all_tests[i].test_case_name); test_case_count++; } } if (num_failed != 0) return EXIT_FAILURE; return EXIT_SUCCESS; } /* * Glue an array of strings together and return it as an allocated string. * Optionally return the whole length of this string in |out_len| */ char *glue_strings(const char *list[], size_t *out_len) { size_t len = 0; char *p, *ret; int i; for (i = 0; list[i] != NULL; i++) len += strlen(list[i]); if (out_len != NULL) *out_len = len; if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1))) return NULL; for (i = 0; list[i] != NULL; i++) p += strlen(strcpy(p, list[i])); return ret; } char *test_mk_file_path(const char *dir, const char *file) { # ifndef OPENSSL_SYS_VMS const char *sep = "/"; # else const char *sep = ""; char *dir_end; char dir_end_sep; # endif size_t dirlen = dir != NULL ? strlen(dir) : 0; size_t len = dirlen + strlen(sep) + strlen(file) + 1; char *full_file = OPENSSL_zalloc(len); if (full_file != NULL) { if (dir != NULL && dirlen > 0) { OPENSSL_strlcpy(full_file, dir, len); # ifdef OPENSSL_SYS_VMS /* * If |file| contains a directory spec, we need to do some * careful merging. * "vol:[dir.dir]" + "[.certs]sm2-root.crt" should become * "vol:[dir.dir.certs]sm2-root.crt" */ dir_end = &full_file[strlen(full_file) - 1]; dir_end_sep = *dir_end; if ((dir_end_sep == ']' || dir_end_sep == '>') && (file[0] == '[' || file[0] == '<')) { file++; if (file[0] == '.') *dir_end = '\0'; else *dir_end = '.'; } #else OPENSSL_strlcat(full_file, sep, len); #endif } OPENSSL_strlcat(full_file, file, len); } return full_file; }
./openssl/test/testutil/tu_local.h
/* * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdlib.h> /* size_t */ #include <openssl/bn.h> #include <openssl/bio.h> #include "../testutil.h" #define TEST_SKIP_CODE 123 int subtest_level(void); int openssl_error_cb(const char *str, size_t len, void *u); const BIO_METHOD *BIO_f_tap(void); void test_fail_message_prefix(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op); void test_fail_string_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const char *m1, size_t l1, const char *m2, size_t l2); void test_fail_bignum_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const BIGNUM *bn1, const BIGNUM *bn2); void test_fail_bignum_mono_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const BIGNUM *bn); void test_fail_memory_message(const char *prefix, const char *file, int line, const char *type, const char *left, const char *right, const char *op, const unsigned char *m1, size_t l1, const unsigned char *m2, size_t l2); __owur int setup_test_framework(int argc, char *argv[]); __owur int pulldown_test_framework(int ret); __owur int run_tests(const char *test_prog_name); void set_test_title(const char *title); typedef enum OPTION_choice_default { OPT_ERR = -1, OPT_EOF = 0, OPT_TEST_ENUM } OPTION_CHOICE_DEFAULT; void opt_check_usage(void);
./openssl/test/testutil/output.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "output.h" int test_printf_stdout(const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = test_vprintf_stdout(fmt, ap); va_end(ap); return ret; } int test_printf_stderr(const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = test_vprintf_stderr(fmt, ap); va_end(ap); return ret; } int test_printf_tapout(const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = test_vprintf_tapout(fmt, ap); va_end(ap); return ret; } int test_printf_taperr(const char *fmt, ...) { va_list ap; int ret; va_start(ap, fmt); ret = test_vprintf_taperr(fmt, ap); va_end(ap); return ret; }
./openssl/test/testutil/stanza.c
/* * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <assert.h> #include <errno.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include "internal/nelem.h" #include "../testutil.h" #include "tu_local.h" int test_start_file(STANZA *s, const char *testfile) { TEST_info("Reading %s", testfile); set_test_title(testfile); memset(s, 0, sizeof(*s)); if (!TEST_ptr(s->fp = BIO_new_file(testfile, "r"))) return 0; s->test_file = testfile; return 1; } int test_end_file(STANZA *s) { TEST_info("Completed %d tests with %d errors and %d skipped", s->numtests, s->errors, s->numskip); BIO_free(s->fp); return 1; } /* * Read a PEM block. Return 1 if okay, 0 on error. */ static int read_key(STANZA *s) { char tmpbuf[128]; if (s->key == NULL) { if (!TEST_ptr(s->key = BIO_new(BIO_s_mem()))) return 0; } else if (!TEST_int_gt(BIO_reset(s->key), 0)) { return 0; } /* Read to PEM end line and place content in memory BIO */ while (BIO_gets(s->fp, tmpbuf, sizeof(tmpbuf))) { s->curr++; if (!TEST_int_gt(BIO_puts(s->key, tmpbuf), 0)) return 0; if (HAS_PREFIX(tmpbuf, "-----END")) return 1; } TEST_error("Can't find key end"); return 0; } /* * Delete leading and trailing spaces from a string */ static char *strip_spaces(char *p) { char *q; /* Skip over leading spaces */ while (*p && isspace((unsigned char)*p)) p++; if (*p == '\0') return NULL; for (q = p + strlen(p) - 1; q != p && isspace((unsigned char)*q); ) *q-- = '\0'; return *p ? p : NULL; } /* * Read next test stanza; return 1 if found, 0 on EOF or error. */ int test_readstanza(STANZA *s) { PAIR *pp = s->pairs; char *p, *equals, *key; const char *value; for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) { s->curr++; if (!TEST_ptr(p = strchr(s->buff, '\n'))) { TEST_info("Line %d too long", s->curr); return 0; } *p = '\0'; /* Blank line marks end of tests. */ if (s->buff[0] == '\0') break; /* Lines starting with a pound sign are ignored. */ if (s->buff[0] == '#') continue; /* Parse into key=value */ if (!TEST_ptr(equals = strchr(s->buff, '='))) { TEST_info("Missing = at line %d\n", s->curr); return 0; } *equals++ = '\0'; if (!TEST_ptr(key = strip_spaces(s->buff))) { TEST_info("Empty field at line %d\n", s->curr); return 0; } if ((value = strip_spaces(equals)) == NULL) value = ""; if (strcmp(key, "Title") == 0) { TEST_info("Starting \"%s\" tests at line %d", value, s->curr); continue; } if (s->numpairs == 0) s->start = s->curr; if (strcmp(key, "PrivateKey") == 0) { if (!read_key(s)) return 0; } if (strcmp(key, "PublicKey") == 0) { if (!read_key(s)) return 0; } if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS) || !TEST_ptr(pp->key = OPENSSL_strdup(key)) || !TEST_ptr(pp->value = OPENSSL_strdup(value))) return 0; pp++; } /* If we read anything, return ok. */ return 1; } void test_clearstanza(STANZA *s) { PAIR *pp = s->pairs; int i = s->numpairs; for ( ; --i >= 0; pp++) { OPENSSL_free(pp->key); OPENSSL_free(pp->value); } s->numpairs = 0; }
./openssl/test/testutil/output.h
/* * Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_TESTUTIL_OUTPUT_H # define OSSL_TESTUTIL_OUTPUT_H # include <stdarg.h> # define ossl_test__attr__(x) # if defined(__GNUC__) && defined(__STDC_VERSION__) \ && !defined(__MINGW32__) && !defined(__MINGW64__) \ && !defined(__APPLE__) /* * Because we support the 'z' modifier, which made its appearance in C99, * we can't use __attribute__ with pre C99 dialects. */ # if __STDC_VERSION__ >= 199901L # undef ossl_test__attr__ # define ossl_test__attr__ __attribute__ # if __GNUC__*10 + __GNUC_MINOR__ >= 44 # define ossl_test__printf__ __gnu_printf__ # else # define ossl_test__printf__ __printf__ # endif # endif # endif /* * The basic I/O functions used internally by the test framework. These * can be overridden when needed. Note that if one is, then all must be. */ void test_open_streams(void); void test_close_streams(void); void test_adjust_streams_tap_level(int level); /* The following ALL return the number of characters written */ int test_vprintf_stdout(const char *fmt, va_list ap) ossl_test__attr__((__format__(ossl_test__printf__, 1, 0))); int test_vprintf_tapout(const char *fmt, va_list ap) ossl_test__attr__((__format__(ossl_test__printf__, 1, 0))); int test_vprintf_stderr(const char *fmt, va_list ap) ossl_test__attr__((__format__(ossl_test__printf__, 1, 0))); int test_vprintf_taperr(const char *fmt, va_list ap) ossl_test__attr__((__format__(ossl_test__printf__, 1, 0))); /* These return failure or success */ int test_flush_stdout(void); int test_flush_tapout(void); int test_flush_stderr(void); int test_flush_taperr(void); /* Commodity functions. There's no need to override these */ int test_printf_stdout(const char *fmt, ...) ossl_test__attr__((__format__(ossl_test__printf__, 1, 2))); int test_printf_tapout(const char *fmt, ...) ossl_test__attr__((__format__(ossl_test__printf__, 1, 2))); int test_printf_stderr(const char *fmt, ...) ossl_test__attr__((__format__(ossl_test__printf__, 1, 2))); int test_printf_taperr(const char *fmt, ...) ossl_test__attr__((__format__(ossl_test__printf__, 1, 2))); # undef ossl_test__printf__ # undef ossl_test__attr__ #endif /* OSSL_TESTUTIL_OUTPUT_H */
./openssl/test/testutil/apps_shims.c
/* * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdlib.h> #include "apps.h" #include "../testutil.h" /* shim that avoids sucking in too much from apps/apps.c */ void *app_malloc(size_t sz, const char *what) { void *vp; /* * This isn't ideal but it is what the app's app_malloc() does on failure. * Instead of exiting with a failure, abort() is called which makes sure * that there will be a good stack trace for debugging purposes. */ if (!TEST_ptr(vp = OPENSSL_malloc(sz))) { TEST_info("Could not allocate %zu bytes for %s\n", sz, what); abort(); } return vp; } /* shim to prevent sucking in too much from apps */ int opt_legacy_okay(void) { return 1; } /* * These three functions are defined here so that they don't need to come from * the apps source code and pull in a lot of additional things. */ int opt_provider_option_given(void) { return 0; } const char *app_get0_propq(void) { return NULL; } OSSL_LIB_CTX *app_get0_libctx(void) { return NULL; }
./openssl/test/testutil/random.c
/* * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" /* * This is an implementation of the algorithm used by the GNU C library's * random(3) pseudorandom number generator as described: * https://www.mscs.dal.ca/~selinger/random/ */ static uint32_t test_random_state[31]; uint32_t test_random(void) { static unsigned int pos = 3; if (pos == 31) pos = 0; test_random_state[pos] += test_random_state[(pos + 28) % 31]; return test_random_state[pos++] / 2; } void test_random_seed(uint32_t sd) { int i; int32_t s; const unsigned int mod = (1u << 31) - 1; test_random_state[0] = sd; for (i = 1; i < 31; i++) { s = (int32_t)test_random_state[i - 1]; test_random_state[i] = (uint32_t)((16807 * (int64_t)s) % mod); } for (i = 34; i < 344; i++) test_random(); }
./openssl/test/testutil/load.c
/* * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <stdio.h> #include <stdlib.h> #include <openssl/x509.h> #include <openssl/pem.h> #include "../testutil.h" X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx) { X509 *cert = NULL; BIO *bio = NULL; if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new(BIO_s_file()))) return NULL; if (TEST_int_gt(BIO_read_filename(bio, file), 0) && TEST_ptr(cert = X509_new_ex(libctx, NULL))) (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL)); BIO_free(bio); return cert; } STACK_OF(X509) *load_certs_pem(const char *file) { STACK_OF(X509) *certs; BIO *bio; X509 *x; if (!TEST_ptr(file) || (bio = BIO_new_file(file, "r")) == NULL) return NULL; certs = sk_X509_new_null(); if (certs == NULL) { BIO_free(bio); return NULL; } ERR_set_mark(); do { x = PEM_read_bio_X509(bio, NULL, 0, NULL); if (x != NULL && !sk_X509_push(certs, x)) { OSSL_STACK_OF_X509_free(certs); BIO_free(bio); return NULL; } else if (x == NULL) { /* * We probably just ran out of certs, so ignore any errors * generated */ ERR_pop_to_mark(); } } while (x != NULL); BIO_free(bio); return certs; } EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx) { EVP_PKEY *key = NULL; BIO *bio = NULL; if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new(BIO_s_file()))) return NULL; if (TEST_int_gt(BIO_read_filename(bio, file), 0)) { unsigned long err = ERR_peek_error(); if (TEST_ptr(key = PEM_read_bio_PrivateKey_ex(bio, NULL, NULL, NULL, libctx, NULL)) && err != ERR_peek_error()) { TEST_info("Spurious error from reading PEM"); EVP_PKEY_free(key); key = NULL; } } BIO_free(bio); return key; } X509_REQ *load_csr_der(const char *file, OSSL_LIB_CTX *libctx) { X509_REQ *csr = NULL; BIO *bio = NULL; if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb"))) return NULL; csr = X509_REQ_new_ex(libctx, NULL); if (TEST_ptr(csr)) (void)TEST_ptr(d2i_X509_REQ_bio(bio, &csr)); BIO_free(bio); return csr; }
./openssl/test/testutil/testutil_init.c
/* * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/opensslconf.h> #include <openssl/trace.h> #include "apps.h" #include "../testutil.h" #ifndef OPENSSL_NO_TRACE typedef struct tracedata_st { BIO *bio; unsigned int ingroup:1; } tracedata; static size_t internal_trace_cb(const char *buf, size_t cnt, int category, int cmd, void *vdata) { int ret = 0; tracedata *trace_data = vdata; char buffer[256], *hex; CRYPTO_THREAD_ID tid; switch (cmd) { case OSSL_TRACE_CTRL_BEGIN: trace_data->ingroup = 1; tid = CRYPTO_THREAD_get_current_id(); hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid)); BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ", hex, OSSL_trace_get_category_name(category)); OPENSSL_free(hex); BIO_set_prefix(trace_data->bio, buffer); break; case OSSL_TRACE_CTRL_WRITE: ret = BIO_write(trace_data->bio, buf, cnt); break; case OSSL_TRACE_CTRL_END: trace_data->ingroup = 0; BIO_set_prefix(trace_data->bio, NULL); break; } return ret < 0 ? 0 : ret; } DEFINE_STACK_OF(tracedata) static STACK_OF(tracedata) *trace_data_stack; static void tracedata_free(tracedata *data) { BIO_free_all(data->bio); OPENSSL_free(data); } static STACK_OF(tracedata) *trace_data_stack; static void cleanup_trace(void) { sk_tracedata_pop_free(trace_data_stack, tracedata_free); } static void setup_trace_category(int category) { BIO *channel; tracedata *trace_data; BIO *bio = NULL; if (OSSL_trace_enabled(category)) return; bio = BIO_new(BIO_f_prefix()); channel = BIO_push(bio, BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT)); trace_data = OPENSSL_zalloc(sizeof(*trace_data)); if (trace_data == NULL || bio == NULL || (trace_data->bio = channel) == NULL || OSSL_trace_set_callback(category, internal_trace_cb, trace_data) == 0 || sk_tracedata_push(trace_data_stack, trace_data) == 0) { fprintf(stderr, "warning: unable to setup trace callback for category '%s'.\n", OSSL_trace_get_category_name(category)); OSSL_trace_set_callback(category, NULL, NULL); BIO_free_all(channel); } } static void setup_trace(const char *str) { char *val; /* * We add this handler as early as possible to ensure it's executed * as late as possible, i.e. after the TRACE code has done its cleanup * (which happens last in OPENSSL_cleanup). */ atexit(cleanup_trace); trace_data_stack = sk_tracedata_new_null(); val = OPENSSL_strdup(str); if (val != NULL) { char *valp = val; char *item; for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) { int category = OSSL_trace_get_category_num(item); if (category == OSSL_TRACE_CATEGORY_ALL) { while (++category < OSSL_TRACE_CATEGORY_NUM) setup_trace_category(category); break; } else if (category > 0) { setup_trace_category(category); } else { fprintf(stderr, "warning: unknown trace category: '%s'.\n", item); } } } OPENSSL_free(val); } #endif /* OPENSSL_NO_TRACE */ int global_init(void) { #ifndef OPENSSL_NO_TRACE setup_trace(getenv("OPENSSL_TRACE")); #endif return 1; }
./openssl/test/testutil/test_cleanup.c
/* * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include "../testutil.h" void cleanup_tests(void) { }
./openssl/test/helpers/ssl_test_ctx.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <string.h> #include <openssl/e_os2.h> #include <openssl/crypto.h> #include "internal/nelem.h" #include "ssl_test_ctx.h" #include "../testutil.h" static const int default_app_data_size = 256; /* Default set to be as small as possible to exercise fragmentation. */ static const int default_max_fragment_size = 512; static int parse_boolean(const char *value, int *result) { if (OPENSSL_strcasecmp(value, "Yes") == 0) { *result = 1; return 1; } else if (OPENSSL_strcasecmp(value, "No") == 0) { *result = 0; return 1; } TEST_error("parse_boolean given: '%s'", value); return 0; } #define IMPLEMENT_SSL_TEST_BOOL_OPTION(struct_type, name, field) \ static int parse_##name##_##field(struct_type *ctx, const char *value) \ { \ return parse_boolean(value, &ctx->field); \ } #define IMPLEMENT_SSL_TEST_STRING_OPTION(struct_type, name, field) \ static int parse_##name##_##field(struct_type *ctx, const char *value) \ { \ OPENSSL_free(ctx->field); \ ctx->field = OPENSSL_strdup(value); \ return TEST_ptr(ctx->field); \ } #define IMPLEMENT_SSL_TEST_INT_OPTION(struct_type, name, field) \ static int parse_##name##_##field(struct_type *ctx, const char *value) \ { \ ctx->field = atoi(value); \ return 1; \ } /* True enums and other test configuration values that map to an int. */ typedef struct { const char *name; int value; } test_enum; __owur static int parse_enum(const test_enum *enums, size_t num_enums, int *value, const char *name) { size_t i; for (i = 0; i < num_enums; i++) { if (strcmp(enums[i].name, name) == 0) { *value = enums[i].value; return 1; } } return 0; } static const char *enum_name(const test_enum *enums, size_t num_enums, int value) { size_t i; for (i = 0; i < num_enums; i++) { if (enums[i].value == value) { return enums[i].name; } } return "InvalidValue"; } /* ExpectedResult */ static const test_enum ssl_test_results[] = { {"Success", SSL_TEST_SUCCESS}, {"ServerFail", SSL_TEST_SERVER_FAIL}, {"ClientFail", SSL_TEST_CLIENT_FAIL}, {"InternalError", SSL_TEST_INTERNAL_ERROR}, {"FirstHandshakeFailed", SSL_TEST_FIRST_HANDSHAKE_FAILED}, }; __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results), &ret_value, value)) { return 0; } test_ctx->expected_result = ret_value; return 1; } const char *ssl_test_result_name(ssl_test_result_t result) { return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result); } /* ExpectedClientAlert / ExpectedServerAlert */ static const test_enum ssl_alerts[] = { {"UnknownCA", SSL_AD_UNKNOWN_CA}, {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE}, {"UnrecognizedName", SSL_AD_UNRECOGNIZED_NAME}, {"NoRenegotiation", SSL_AD_NO_RENEGOTIATION}, {"BadCertificate", SSL_AD_BAD_CERTIFICATE}, {"NoApplicationProtocol", SSL_AD_NO_APPLICATION_PROTOCOL}, {"CertificateRequired", SSL_AD_CERTIFICATE_REQUIRED}, }; __owur static int parse_alert(int *alert, const char *value) { return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value); } __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value) { return parse_alert(&test_ctx->expected_client_alert, value); } __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value) { return parse_alert(&test_ctx->expected_server_alert, value); } const char *ssl_alert_name(int alert) { return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert); } /* ExpectedProtocol */ static const test_enum ssl_protocols[] = { {"TLSv1.3", TLS1_3_VERSION}, {"TLSv1.2", TLS1_2_VERSION}, {"TLSv1.1", TLS1_1_VERSION}, {"TLSv1", TLS1_VERSION}, {"SSLv3", SSL3_VERSION}, {"DTLSv1", DTLS1_VERSION}, {"DTLSv1.2", DTLS1_2_VERSION}, }; __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value) { return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols), &test_ctx->expected_protocol, value); } const char *ssl_protocol_name(int protocol) { return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol); } /* VerifyCallback */ static const test_enum ssl_verify_callbacks[] = { {"None", SSL_TEST_VERIFY_NONE}, {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL}, {"RetryOnce", SSL_TEST_VERIFY_RETRY_ONCE}, {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL}, }; __owur static int parse_client_verify_callback(SSL_TEST_CLIENT_CONF *client_conf, const char *value) { int ret_value; if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks), &ret_value, value)) { return 0; } client_conf->verify_callback = ret_value; return 1; } const char *ssl_verify_callback_name(ssl_verify_callback_t callback) { return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks), callback); } /* ServerName */ static const test_enum ssl_servername[] = { {"None", SSL_TEST_SERVERNAME_NONE}, {"server1", SSL_TEST_SERVERNAME_SERVER1}, {"server2", SSL_TEST_SERVERNAME_SERVER2}, {"invalid", SSL_TEST_SERVERNAME_INVALID}, }; __owur static int parse_servername(SSL_TEST_CLIENT_CONF *client_conf, const char *value) { int ret_value; if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername), &ret_value, value)) { return 0; } client_conf->servername = ret_value; return 1; } __owur static int parse_expected_servername(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_servername, OSSL_NELEM(ssl_servername), &ret_value, value)) { return 0; } test_ctx->expected_servername = ret_value; return 1; } const char *ssl_servername_name(ssl_servername_t server) { return enum_name(ssl_servername, OSSL_NELEM(ssl_servername), server); } /* ServerNameCallback */ static const test_enum ssl_servername_callbacks[] = { {"None", SSL_TEST_SERVERNAME_CB_NONE}, {"IgnoreMismatch", SSL_TEST_SERVERNAME_IGNORE_MISMATCH}, {"RejectMismatch", SSL_TEST_SERVERNAME_REJECT_MISMATCH}, {"ClientHelloIgnoreMismatch", SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH}, {"ClientHelloRejectMismatch", SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH}, {"ClientHelloNoV12", SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12}, }; __owur static int parse_servername_callback(SSL_TEST_SERVER_CONF *server_conf, const char *value) { int ret_value; if (!parse_enum(ssl_servername_callbacks, OSSL_NELEM(ssl_servername_callbacks), &ret_value, value)) { return 0; } server_conf->servername_callback = ret_value; return 1; } const char *ssl_servername_callback_name(ssl_servername_callback_t callback) { return enum_name(ssl_servername_callbacks, OSSL_NELEM(ssl_servername_callbacks), callback); } /* SessionTicketExpected */ static const test_enum ssl_session_ticket[] = { {"Ignore", SSL_TEST_SESSION_TICKET_IGNORE}, {"Yes", SSL_TEST_SESSION_TICKET_YES}, {"No", SSL_TEST_SESSION_TICKET_NO}, }; __owur static int parse_session_ticket(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_session_ticket, OSSL_NELEM(ssl_session_ticket), &ret_value, value)) { return 0; } test_ctx->session_ticket_expected = ret_value; return 1; } const char *ssl_session_ticket_name(ssl_session_ticket_t server) { return enum_name(ssl_session_ticket, OSSL_NELEM(ssl_session_ticket), server); } /* CompressionExpected */ IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, compression_expected) /* SessionIdExpected */ static const test_enum ssl_session_id[] = { {"Ignore", SSL_TEST_SESSION_ID_IGNORE}, {"Yes", SSL_TEST_SESSION_ID_YES}, {"No", SSL_TEST_SESSION_ID_NO}, }; __owur static int parse_session_id(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_session_id, OSSL_NELEM(ssl_session_id), &ret_value, value)) { return 0; } test_ctx->session_id_expected = ret_value; return 1; } const char *ssl_session_id_name(ssl_session_id_t server) { return enum_name(ssl_session_id, OSSL_NELEM(ssl_session_id), server); } /* Method */ static const test_enum ssl_test_methods[] = { {"TLS", SSL_TEST_METHOD_TLS}, {"DTLS", SSL_TEST_METHOD_DTLS}, {"QUIC", SSL_TEST_METHOD_QUIC} }; __owur static int parse_test_method(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_test_methods, OSSL_NELEM(ssl_test_methods), &ret_value, value)) { return 0; } test_ctx->method = ret_value; return 1; } const char *ssl_test_method_name(ssl_test_method_t method) { return enum_name(ssl_test_methods, OSSL_NELEM(ssl_test_methods), method); } /* NPN and ALPN options */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, npn_protocols) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, npn_protocols) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_npn_protocol) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, alpn_protocols) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, alpn_protocols) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_alpn_protocol) /* SRP options */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, srp_user) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, srp_user) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, srp_password) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, srp_password) /* Session Ticket App Data options */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_session_ticket_app_data) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_SERVER_CONF, server, session_ticket_app_data) /* Handshake mode */ static const test_enum ssl_handshake_modes[] = { {"Simple", SSL_TEST_HANDSHAKE_SIMPLE}, {"Resume", SSL_TEST_HANDSHAKE_RESUME}, {"RenegotiateServer", SSL_TEST_HANDSHAKE_RENEG_SERVER}, {"RenegotiateClient", SSL_TEST_HANDSHAKE_RENEG_CLIENT}, {"KeyUpdateServer", SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER}, {"KeyUpdateClient", SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT}, {"PostHandshakeAuth", SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH}, }; __owur static int parse_handshake_mode(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes), &ret_value, value)) { return 0; } test_ctx->handshake_mode = ret_value; return 1; } const char *ssl_handshake_mode_name(ssl_handshake_mode_t mode) { return enum_name(ssl_handshake_modes, OSSL_NELEM(ssl_handshake_modes), mode); } /* Renegotiation Ciphersuites */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, reneg_ciphers) /* KeyUpdateType */ static const test_enum ssl_key_update_types[] = { {"KeyUpdateRequested", SSL_KEY_UPDATE_REQUESTED}, {"KeyUpdateNotRequested", SSL_KEY_UPDATE_NOT_REQUESTED}, }; __owur static int parse_key_update_type(SSL_TEST_CTX *test_ctx, const char *value) { int ret_value; if (!parse_enum(ssl_key_update_types, OSSL_NELEM(ssl_key_update_types), &ret_value, value)) { return 0; } test_ctx->key_update_type = ret_value; return 1; } /* CT Validation */ static const test_enum ssl_ct_validation_modes[] = { {"None", SSL_TEST_CT_VALIDATION_NONE}, {"Permissive", SSL_TEST_CT_VALIDATION_PERMISSIVE}, {"Strict", SSL_TEST_CT_VALIDATION_STRICT}, }; __owur static int parse_ct_validation(SSL_TEST_CLIENT_CONF *client_conf, const char *value) { int ret_value; if (!parse_enum(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes), &ret_value, value)) { return 0; } client_conf->ct_validation = ret_value; return 1; } const char *ssl_ct_validation_name(ssl_ct_validation_t mode) { return enum_name(ssl_ct_validation_modes, OSSL_NELEM(ssl_ct_validation_modes), mode); } IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, resumption_expected) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_SERVER_CONF, server, broken_session_ticket) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, use_sctp) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, compress_certificates) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, enable_client_sctp_label_bug) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, enable_server_sctp_label_bug) /* CertStatus */ static const test_enum ssl_certstatus[] = { {"None", SSL_TEST_CERT_STATUS_NONE}, {"GoodResponse", SSL_TEST_CERT_STATUS_GOOD_RESPONSE}, {"BadResponse", SSL_TEST_CERT_STATUS_BAD_RESPONSE} }; __owur static int parse_certstatus(SSL_TEST_SERVER_CONF *server_conf, const char *value) { int ret_value; if (!parse_enum(ssl_certstatus, OSSL_NELEM(ssl_certstatus), &ret_value, value)) { return 0; } server_conf->cert_status = ret_value; return 1; } const char *ssl_certstatus_name(ssl_cert_status_t cert_status) { return enum_name(ssl_certstatus, OSSL_NELEM(ssl_certstatus), cert_status); } /* ApplicationData */ IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, app_data_size) /* MaxFragmentSize */ IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, max_fragment_size) /* Maximum-Fragment-Length TLS extension mode */ static const test_enum ssl_max_fragment_len_mode[] = { {"None", TLSEXT_max_fragment_length_DISABLED}, { "512", TLSEXT_max_fragment_length_512}, {"1024", TLSEXT_max_fragment_length_1024}, {"2048", TLSEXT_max_fragment_length_2048}, {"4096", TLSEXT_max_fragment_length_4096} }; __owur static int parse_max_fragment_len_mode(SSL_TEST_CLIENT_CONF *client_conf, const char *value) { int ret_value; if (!parse_enum(ssl_max_fragment_len_mode, OSSL_NELEM(ssl_max_fragment_len_mode), &ret_value, value)) { return 0; } client_conf->max_fragment_len_mode = ret_value; return 1; } const char *ssl_max_fragment_len_name(int MFL_mode) { return enum_name(ssl_max_fragment_len_mode, OSSL_NELEM(ssl_max_fragment_len_mode), MFL_mode); } /* Expected key and signature types */ __owur static int parse_expected_key_type(int *ptype, const char *value) { int nid; const EVP_PKEY_ASN1_METHOD *ameth; if (value == NULL) return 0; ameth = EVP_PKEY_asn1_find_str(NULL, value, -1); if (ameth != NULL) EVP_PKEY_asn1_get0_info(&nid, NULL, NULL, NULL, NULL, ameth); else nid = OBJ_sn2nid(value); if (nid == NID_undef) nid = OBJ_ln2nid(value); #ifndef OPENSSL_NO_EC if (nid == NID_undef) nid = EC_curve_nist2nid(value); #endif switch (nid) { case NID_brainpoolP256r1tls13: nid = NID_brainpoolP256r1; break; case NID_brainpoolP384r1tls13: nid = NID_brainpoolP384r1; break; case NID_brainpoolP512r1tls13: nid = NID_brainpoolP512r1; break; } if (nid == NID_undef) return 0; *ptype = nid; return 1; } __owur static int parse_expected_tmp_key_type(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_key_type(&test_ctx->expected_tmp_key_type, value); } __owur static int parse_expected_server_cert_type(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_key_type(&test_ctx->expected_server_cert_type, value); } __owur static int parse_expected_server_sign_type(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_key_type(&test_ctx->expected_server_sign_type, value); } __owur static int parse_expected_client_cert_type(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_key_type(&test_ctx->expected_client_cert_type, value); } __owur static int parse_expected_client_sign_type(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_key_type(&test_ctx->expected_client_sign_type, value); } /* Expected signing hash */ __owur static int parse_expected_sign_hash(int *ptype, const char *value) { int nid; if (value == NULL) return 0; nid = OBJ_sn2nid(value); if (nid == NID_undef) nid = OBJ_ln2nid(value); if (nid == NID_undef) return 0; *ptype = nid; return 1; } __owur static int parse_expected_server_sign_hash(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_sign_hash(&test_ctx->expected_server_sign_hash, value); } __owur static int parse_expected_client_sign_hash(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_sign_hash(&test_ctx->expected_client_sign_hash, value); } __owur static int parse_expected_ca_names(STACK_OF(X509_NAME) **pnames, const char *value, OSSL_LIB_CTX *libctx) { if (value == NULL) return 0; if (!strcmp(value, "empty")) *pnames = sk_X509_NAME_new_null(); else *pnames = SSL_load_client_CA_file_ex(value, libctx, NULL); return *pnames != NULL; } __owur static int parse_expected_server_ca_names(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_ca_names(&test_ctx->expected_server_ca_names, value, test_ctx->libctx); } __owur static int parse_expected_client_ca_names(SSL_TEST_CTX *test_ctx, const char *value) { return parse_expected_ca_names(&test_ctx->expected_client_ca_names, value, test_ctx->libctx); } /* ExpectedCipher */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_cipher) /* Client and Server PHA */ IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CLIENT_CONF, client, enable_pha) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_SERVER_CONF, server, force_pha) IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CLIENT_CONF, client, no_extms_on_reneg) /* FIPS provider version limiting */ IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, fips_version) /* Known test options and their corresponding parse methods. */ /* Top-level options. */ typedef struct { const char *name; int (*parse)(SSL_TEST_CTX *test_ctx, const char *value); } ssl_test_ctx_option; static const ssl_test_ctx_option ssl_test_ctx_options[] = { { "ExpectedResult", &parse_expected_result }, { "ExpectedClientAlert", &parse_client_alert }, { "ExpectedServerAlert", &parse_server_alert }, { "ExpectedProtocol", &parse_protocol }, { "ExpectedServerName", &parse_expected_servername }, { "SessionTicketExpected", &parse_session_ticket }, { "CompressionExpected", &parse_test_compression_expected }, { "SessionIdExpected", &parse_session_id }, { "Method", &parse_test_method }, { "ExpectedNPNProtocol", &parse_test_expected_npn_protocol }, { "ExpectedALPNProtocol", &parse_test_expected_alpn_protocol }, { "HandshakeMode", &parse_handshake_mode }, { "KeyUpdateType", &parse_key_update_type }, { "ResumptionExpected", &parse_test_resumption_expected }, { "ApplicationData", &parse_test_app_data_size }, { "MaxFragmentSize", &parse_test_max_fragment_size }, { "ExpectedTmpKeyType", &parse_expected_tmp_key_type }, { "ExpectedServerCertType", &parse_expected_server_cert_type }, { "ExpectedServerSignHash", &parse_expected_server_sign_hash }, { "ExpectedServerSignType", &parse_expected_server_sign_type }, { "ExpectedServerCANames", &parse_expected_server_ca_names }, { "ExpectedClientCertType", &parse_expected_client_cert_type }, { "ExpectedClientSignHash", &parse_expected_client_sign_hash }, { "ExpectedClientSignType", &parse_expected_client_sign_type }, { "ExpectedClientCANames", &parse_expected_client_ca_names }, { "UseSCTP", &parse_test_use_sctp }, { "CompressCertificates", &parse_test_compress_certificates }, { "EnableClientSCTPLabelBug", &parse_test_enable_client_sctp_label_bug }, { "EnableServerSCTPLabelBug", &parse_test_enable_server_sctp_label_bug }, { "ExpectedCipher", &parse_test_expected_cipher }, { "ExpectedSessionTicketAppData", &parse_test_expected_session_ticket_app_data }, { "FIPSversion", &parse_test_fips_version }, }; /* Nested client options. */ typedef struct { const char *name; int (*parse)(SSL_TEST_CLIENT_CONF *conf, const char *value); } ssl_test_client_option; static const ssl_test_client_option ssl_test_client_options[] = { { "VerifyCallback", &parse_client_verify_callback }, { "ServerName", &parse_servername }, { "NPNProtocols", &parse_client_npn_protocols }, { "ALPNProtocols", &parse_client_alpn_protocols }, { "CTValidation", &parse_ct_validation }, { "RenegotiateCiphers", &parse_client_reneg_ciphers}, { "SRPUser", &parse_client_srp_user }, { "SRPPassword", &parse_client_srp_password }, { "MaxFragmentLenExt", &parse_max_fragment_len_mode }, { "EnablePHA", &parse_client_enable_pha }, { "RenegotiateNoExtms", &parse_client_no_extms_on_reneg }, }; /* Nested server options. */ typedef struct { const char *name; int (*parse)(SSL_TEST_SERVER_CONF *conf, const char *value); } ssl_test_server_option; static const ssl_test_server_option ssl_test_server_options[] = { { "ServerNameCallback", &parse_servername_callback }, { "NPNProtocols", &parse_server_npn_protocols }, { "ALPNProtocols", &parse_server_alpn_protocols }, { "BrokenSessionTicket", &parse_server_broken_session_ticket }, { "CertStatus", &parse_certstatus }, { "SRPUser", &parse_server_srp_user }, { "SRPPassword", &parse_server_srp_password }, { "ForcePHA", &parse_server_force_pha }, { "SessionTicketAppData", &parse_server_session_ticket_app_data }, }; SSL_TEST_CTX *SSL_TEST_CTX_new(OSSL_LIB_CTX *libctx) { SSL_TEST_CTX *ret; /* The return code is checked by caller */ if ((ret = OPENSSL_zalloc(sizeof(*ret))) != NULL) { ret->libctx = libctx; ret->app_data_size = default_app_data_size; ret->max_fragment_size = default_max_fragment_size; } return ret; } static void ssl_test_extra_conf_free_data(SSL_TEST_EXTRA_CONF *conf) { OPENSSL_free(conf->client.npn_protocols); OPENSSL_free(conf->server.npn_protocols); OPENSSL_free(conf->server2.npn_protocols); OPENSSL_free(conf->client.alpn_protocols); OPENSSL_free(conf->server.alpn_protocols); OPENSSL_free(conf->server2.alpn_protocols); OPENSSL_free(conf->client.reneg_ciphers); OPENSSL_free(conf->server.srp_user); OPENSSL_free(conf->server.srp_password); OPENSSL_free(conf->server2.srp_user); OPENSSL_free(conf->server2.srp_password); OPENSSL_free(conf->client.srp_user); OPENSSL_free(conf->client.srp_password); OPENSSL_free(conf->server.session_ticket_app_data); OPENSSL_free(conf->server2.session_ticket_app_data); } static void ssl_test_ctx_free_extra_data(SSL_TEST_CTX *ctx) { ssl_test_extra_conf_free_data(&ctx->extra); ssl_test_extra_conf_free_data(&ctx->resume_extra); } void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx) { if (ctx == NULL) return; ssl_test_ctx_free_extra_data(ctx); OPENSSL_free(ctx->expected_npn_protocol); OPENSSL_free(ctx->expected_alpn_protocol); OPENSSL_free(ctx->expected_session_ticket_app_data); sk_X509_NAME_pop_free(ctx->expected_server_ca_names, X509_NAME_free); sk_X509_NAME_pop_free(ctx->expected_client_ca_names, X509_NAME_free); OPENSSL_free(ctx->expected_cipher); OPENSSL_free(ctx->fips_version); OPENSSL_free(ctx); } static int parse_client_options(SSL_TEST_CLIENT_CONF *client, const CONF *conf, const char *client_section) { STACK_OF(CONF_VALUE) *sk_conf; int i; size_t j; if (!TEST_ptr(sk_conf = NCONF_get_section(conf, client_section))) return 0; for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) { int found = 0; const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i); for (j = 0; j < OSSL_NELEM(ssl_test_client_options); j++) { if (strcmp(option->name, ssl_test_client_options[j].name) == 0) { if (!ssl_test_client_options[j].parse(client, option->value)) { TEST_info("Bad value %s for option %s", option->value, option->name); return 0; } found = 1; break; } } if (!found) { TEST_info("Unknown test option: %s", option->name); return 0; } } return 1; } static int parse_server_options(SSL_TEST_SERVER_CONF *server, const CONF *conf, const char *server_section) { STACK_OF(CONF_VALUE) *sk_conf; int i; size_t j; if (!TEST_ptr(sk_conf = NCONF_get_section(conf, server_section))) return 0; for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) { int found = 0; const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i); for (j = 0; j < OSSL_NELEM(ssl_test_server_options); j++) { if (strcmp(option->name, ssl_test_server_options[j].name) == 0) { if (!ssl_test_server_options[j].parse(server, option->value)) { TEST_info("Bad value %s for option %s", option->value, option->name); return 0; } found = 1; break; } } if (!found) { TEST_info("Unknown test option: %s", option->name); return 0; } } return 1; } SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section, OSSL_LIB_CTX *libctx) { STACK_OF(CONF_VALUE) *sk_conf = NULL; SSL_TEST_CTX *ctx = NULL; int i; size_t j; if (!TEST_ptr(sk_conf = NCONF_get_section(conf, test_section)) || !TEST_ptr(ctx = SSL_TEST_CTX_new(libctx))) goto err; for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) { int found = 0; const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i); /* Subsections */ if (strcmp(option->name, "client") == 0) { if (!parse_client_options(&ctx->extra.client, conf, option->value)) goto err; } else if (strcmp(option->name, "server") == 0) { if (!parse_server_options(&ctx->extra.server, conf, option->value)) goto err; } else if (strcmp(option->name, "server2") == 0) { if (!parse_server_options(&ctx->extra.server2, conf, option->value)) goto err; } else if (strcmp(option->name, "resume-client") == 0) { if (!parse_client_options(&ctx->resume_extra.client, conf, option->value)) goto err; } else if (strcmp(option->name, "resume-server") == 0) { if (!parse_server_options(&ctx->resume_extra.server, conf, option->value)) goto err; } else if (strcmp(option->name, "resume-server2") == 0) { if (!parse_server_options(&ctx->resume_extra.server2, conf, option->value)) goto err; } else { for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) { if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) { if (!ssl_test_ctx_options[j].parse(ctx, option->value)) { TEST_info("Bad value %s for option %s", option->value, option->name); goto err; } found = 1; break; } } if (!found) { TEST_info("Unknown test option: %s", option->name); goto err; } } } goto done; err: SSL_TEST_CTX_free(ctx); ctx = NULL; done: return ctx; }
./openssl/test/helpers/ssltestlib.h
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_TEST_SSLTESTLIB_H # define OSSL_TEST_SSLTESTLIB_H # include <openssl/ssl.h> #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01") #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02") #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03") #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04") #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05") int create_ssl_ctx_pair(OSSL_LIB_CTX *libctx, const SSL_METHOD *sm, const SSL_METHOD *cm, int min_proto_version, int max_proto_version, SSL_CTX **sctx, SSL_CTX **cctx, char *certfile, char *privkeyfile); int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl, SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio); int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want, int read, int listen); int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl, SSL **cssl, int sfd, int cfd); int wait_until_sock_readable(int sock); int create_test_sockets(int *cfdp, int *sfdp, int socktype, BIO_ADDR *saddr); int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want); void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl); /* Note: Not thread safe! */ const BIO_METHOD *bio_f_tls_dump_filter(void); void bio_f_tls_dump_filter_free(void); const BIO_METHOD *bio_s_mempacket_test(void); void bio_s_mempacket_test_free(void); const BIO_METHOD *bio_s_always_retry(void); void bio_s_always_retry_free(void); void set_always_retry_err_val(int err); /* * Maybe retry BIO ctrls. We make them large enough to not clash with standard * BIO ctrl codes. */ #define MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT (1 << 15) const BIO_METHOD *bio_s_maybe_retry(void); void bio_s_maybe_retry_free(void); /* Packet types - value 0 is reserved */ #define INJECT_PACKET 1 #define INJECT_PACKET_IGNORE_REC_SEQ 2 /* * Mempacket BIO ctrls. We make them large enough to not clash with standard BIO * ctrl codes. */ #define MEMPACKET_CTRL_SET_DROP_EPOCH (1 << 15) #define MEMPACKET_CTRL_SET_DROP_REC (2 << 15) #define MEMPACKET_CTRL_GET_DROP_REC (3 << 15) #define MEMPACKET_CTRL_SET_DUPLICATE_REC (4 << 15) int mempacket_swap_epoch(BIO *bio); int mempacket_move_packet(BIO *bio, int d, int s); int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum, int type); typedef struct mempacket_st MEMPACKET; DEFINE_STACK_OF(MEMPACKET) SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize); /* Add cert from `cert_file` multiple times to create large extra cert chain */ int ssl_ctx_add_large_cert_chain(OSSL_LIB_CTX *libctx, SSL_CTX *sctx, const char *cert_file); #endif /* OSSL_TEST_SSLTESTLIB_H */
./openssl/test/helpers/noisydgrambio.c
/* * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/bio.h> #include "quictestlib.h" #include "../testutil.h" #define MSG_DATA_LEN_MAX 1472 struct noisy_dgram_st { uint64_t this_dgram; BIO_MSG msg; uint64_t reinject_dgram; int backoff; }; static long noisy_dgram_ctrl(BIO *bio, int cmd, long num, void *ptr) { long ret; BIO *next = BIO_next(bio); if (next == NULL) return 0; switch (cmd) { case BIO_CTRL_DUP: ret = 0L; break; case BIO_CTRL_NOISE_BACK_OFF: { struct noisy_dgram_st *data; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; data->backoff = 1; ret = 1; break; } default: ret = BIO_ctrl(next, cmd, num, ptr); break; } return ret; } static int noisy_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msg, uint64_t flags, size_t *msgs_processed) { BIO *next = BIO_next(bio); if (next == NULL) return 0; /* * We only introduce noise when receiving messages. We just pass this on * to the underlying BIO. */ return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed); } /* 1 in NOISE_RATE datagrams will be noisy. With a value of 5 that is 20% */ #define NOISE_RATE 5 /* * We have 3 different types of noise: drop, duplicate and delay * Each of these have equal probability. */ #define NOISE_TYPE_DROP 0 #define NOISE_TYPE_DUPLICATE 1 #define NOISE_TYPE_DELAY 2 #define NOISE_TYPE_BITFLIPS 3 #define NUM_NOISE_TYPES 4 /* * When a duplicate occurs we reinject the new datagram after up to * MAX_DGRAM_REINJECT datagrams have been sent. A reinject of 1 means that the * duplicate follows immediately after the original datagram. A reinject of 4 * means that original datagram plus 3 other datagrams are sent before the * reinjected datagram is inserted. * This also controls when a delay (not a duplicate) occurs. In that case * we add 1 to the number because there is no point in skipping the current * datagram only to immediately reinject it in the next datagram. */ #define MAX_DGRAM_REINJECT 4 static void get_noise(int long_header, uint64_t *reinject, int *should_drop, uint16_t *flip, size_t *flip_offset) { uint32_t type; *flip = 0; if (test_random() % NOISE_RATE != 0) { *reinject = 0; *should_drop = 0; return; } type = test_random() % NUM_NOISE_TYPES; /* * Of noisy datagrams, 25% drop, 25% duplicate, 25% delay, 25% flip bits * A duplicated datagram keeps the current datagram and reinjects a new * identical one after up to MAX_DGRAM_DELAY datagrams have been sent. * A delayed datagram is implemented as both a reinject and a drop, i.e. an * identical datagram is reinjected after the given number of datagrams have * been sent and the current datagram is dropped. */ *should_drop = (type == NOISE_TYPE_DROP || type == NOISE_TYPE_DELAY); /* * Where a duplicate occurs we reinject the copy of the datagram up to * MAX_DGRAM_DELAY datagrams later */ *reinject = (type == NOISE_TYPE_DUPLICATE || type == NOISE_TYPE_DELAY) ? (uint64_t)((test_random() % MAX_DGRAM_REINJECT) + 1) : 0; /* * No point in reinjecting after 1 datagram if the current datagram is also * dropped (i.e. this is a delay not a duplicate), so we reinject after an * extra datagram in that case */ *reinject += type == NOISE_TYPE_DELAY; /* flip some bits in the header */ if (type == NOISE_TYPE_BITFLIPS) { /* we flip at most 8 bits of the 16 bit value at once */ *flip = (test_random() % 255 + 1) << (test_random() % 8); /* * 25/50 bytes of guesstimated header size (it depends on CID length) * It does not matter much if it is overestimated. */ *flip_offset = test_random() % (25 * (1 + long_header)); } } static void flip_bits(unsigned char *msg, size_t msg_len, uint16_t flip, size_t flip_offset) { if (flip == 0) return; /* None of these border conditions should happen but check them anyway */ if (msg_len < 2) return; if (msg_len < flip_offset + 2) flip_offset = msg_len - 2; #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**Flipping bits in a datagram at offset %u\n", (unsigned int)flip_offset); BIO_dump_fp(stdout, msg, msg_len); printf("\n"); #endif msg[flip_offset] ^= flip >> 8; msg[flip_offset + 1] ^= flip & 0xff; } static int noisy_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msg, uint64_t flags, size_t *msgs_processed) { BIO *next = BIO_next(bio); size_t i, j, data_len = 0, msg_cnt = 0; BIO_MSG *thismsg; struct noisy_dgram_st *data; if (!TEST_ptr(next)) return 0; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; /* * For simplicity we assume that all elements in the msg array have the * same data_len. They are not required to by the API, but it would be quite * strange for that not to be the case - and our code that calls * BIO_recvmmsg does do this (which is all that is important for this test * code). We test the invariant here. */ for (i = 0; i < num_msg; i++) { if (i == 0) { data_len = msg[i].data_len; if (!TEST_size_t_le(data_len, MSG_DATA_LEN_MAX)) return 0; } else if (!TEST_size_t_eq(msg[i].data_len, data_len)) { return 0; } } if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed)) return 0; #ifdef OSSL_NOISY_DGRAM_DEBUG printf("Pre-filter datagram list:\n"); for (i = 0; i < *msgs_processed; i++) { printf("Pre-filter Datagram:\n"); BIO_dump_fp(stdout, msg[i].data, msg[i].data_len); printf("\n"); } printf("End of pre-filter datagram list\nApplying noise filters:\n"); #endif msg_cnt = *msgs_processed; /* Introduce noise */ for (i = 0, thismsg = msg; i < msg_cnt; i++, thismsg++, data->this_dgram++) { uint64_t reinject; int should_drop; uint16_t flip; size_t flip_offset; /* If we have a message to reinject then insert it now */ if (data->reinject_dgram > 0 && data->reinject_dgram == data->this_dgram) { if (msg_cnt < num_msg) { /* Make space for the injected message */ for (j = msg_cnt; j > i; j--) { if (!bio_msg_copy(&msg[j], &msg[j - 1])) return 0; } if (!bio_msg_copy(thismsg, &data->msg)) return 0; msg_cnt++; data->reinject_dgram = 0; #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**Injecting a datagram\n"); BIO_dump_fp(stdout, thismsg->data, thismsg->data_len); printf("\n"); #endif continue; } /* else we have no space for the injection, so just drop it */ data->reinject_dgram = 0; } get_noise(/* long header */ (((uint8_t *)thismsg->data)[0] & 0x80) != 0, &reinject, &should_drop, &flip, &flip_offset); if (data->backoff) { /* * We might be asked to back off on introducing too much noise if * there is a danger that the connection will fail. In that case * we always ensure that the next datagram does not get dropped so * that the connection always survives. After that we can resume * with normal noise */ #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**Back off applied\n"); #endif should_drop = 0; flip = 0; data->backoff = 0; } flip_bits(thismsg->data, thismsg->data_len, flip, flip_offset); /* * We ignore reinjection if a message is already waiting to be * reinjected */ if (reinject > 0 && data->reinject_dgram == 0) { /* * Both duplicated and delayed datagrams get reintroduced after the * delay period. Datagrams that are delayed only (not duplicated) * will also have the current copy of the datagram dropped (i.e * should_drop below will be true). */ if (!bio_msg_copy(&data->msg, thismsg)) return 0; data->reinject_dgram = data->this_dgram + reinject; #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**Scheduling a reinject after %u messages%s\n", (unsigned int)reinject, should_drop ? "" : "(duplicating)"); BIO_dump_fp(stdout, thismsg->data, thismsg->data_len); printf("\n"); #endif } if (should_drop) { #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**Dropping a datagram\n"); BIO_dump_fp(stdout, thismsg->data, thismsg->data_len); printf("\n"); #endif for (j = i + 1; j < msg_cnt; j++) { if (!bio_msg_copy(&msg[j - 1], &msg[j])) return 0; } msg_cnt--; } } #ifdef OSSL_NOISY_DGRAM_DEBUG printf("End of noise filters\nPost-filter datagram list:\n"); for (i = 0; i < msg_cnt; i++) { printf("Post-filter Datagram:\n"); BIO_dump_fp(stdout, msg[i].data, msg[i].data_len); printf("\n"); } printf("End of post-filter datagram list\n"); #endif *msgs_processed = msg_cnt; if (msg_cnt == 0) { ERR_raise(ERR_LIB_BIO, BIO_R_NON_FATAL); return 0; } return 1; } static void data_free(struct noisy_dgram_st *data) { if (data == NULL) return; OPENSSL_free(data->msg.data); BIO_ADDR_free(data->msg.peer); BIO_ADDR_free(data->msg.local); OPENSSL_free(data); } static int noisy_dgram_new(BIO *bio) { struct noisy_dgram_st *data = OPENSSL_zalloc(sizeof(*data)); if (!TEST_ptr(data)) return 0; data->msg.data = OPENSSL_malloc(MSG_DATA_LEN_MAX); data->msg.peer = BIO_ADDR_new(); data->msg.local = BIO_ADDR_new(); if (data->msg.data == NULL || data->msg.peer == NULL || data->msg.local == NULL) { data_free(data); return 0; } BIO_set_data(bio, data); BIO_set_init(bio, 1); return 1; } static int noisy_dgram_free(BIO *bio) { data_free(BIO_get_data(bio)); BIO_set_data(bio, NULL); BIO_set_init(bio, 0); return 1; } /* Choose a sufficiently large type likely to be unused for this custom BIO */ #define BIO_TYPE_NOISY_DGRAM_FILTER (0x80 | BIO_TYPE_FILTER) static BIO_METHOD *method_noisy_dgram = NULL; /* Note: Not thread safe! */ const BIO_METHOD *bio_f_noisy_dgram_filter(void) { if (method_noisy_dgram == NULL) { method_noisy_dgram = BIO_meth_new(BIO_TYPE_NOISY_DGRAM_FILTER, "Nosiy datagram filter"); if (method_noisy_dgram == NULL || !BIO_meth_set_ctrl(method_noisy_dgram, noisy_dgram_ctrl) || !BIO_meth_set_sendmmsg(method_noisy_dgram, noisy_dgram_sendmmsg) || !BIO_meth_set_recvmmsg(method_noisy_dgram, noisy_dgram_recvmmsg) || !BIO_meth_set_create(method_noisy_dgram, noisy_dgram_new) || !BIO_meth_set_destroy(method_noisy_dgram, noisy_dgram_free)) return NULL; } return method_noisy_dgram; } void bio_f_noisy_dgram_filter_free(void) { BIO_meth_free(method_noisy_dgram); }
./openssl/test/helpers/cmp_testlib.h
/* * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2019 * Copyright Siemens AG 2015-2019 * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #ifndef OSSL_TEST_CMP_TESTLIB_H # define OSSL_TEST_CMP_TESTLIB_H # include <openssl/cmp.h> # include <openssl/pem.h> # include <openssl/rand.h> # include "../../crypto/cmp/cmp_local.h" # include "../testutil.h" # ifndef OPENSSL_NO_CMP # define CMP_TEST_REFVALUE_LENGTH 15 /* arbitrary value */ OSSL_CMP_MSG *load_pkimsg(const char *file, OSSL_LIB_CTX *libctx); int valid_asn1_encoding(const OSSL_CMP_MSG *msg); int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2); int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert); int print_to_bio_out(const char *func, const char *file, int line, OSSL_CMP_severity level, const char *msg); # endif #endif /* OSSL_TEST_CMP_TESTLIB_H */
./openssl/test/helpers/pktsplitbio.c
/* * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/bio.h> #include "quictestlib.h" #include "../testutil.h" static long pkt_split_dgram_ctrl(BIO *bio, int cmd, long num, void *ptr) { long ret; BIO *next = BIO_next(bio); if (next == NULL) return 0; switch (cmd) { case BIO_CTRL_DUP: ret = 0L; break; default: ret = BIO_ctrl(next, cmd, num, ptr); break; } return ret; } static int pkt_split_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msg, uint64_t flags, size_t *msgs_processed) { BIO *next = BIO_next(bio); if (next == NULL) return 0; /* * We only introduce noise when receiving messages. We just pass this on * to the underlying BIO. */ return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed); } static int pkt_split_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msg, uint64_t flags, size_t *msgs_processed) { BIO *next = BIO_next(bio); size_t i, j, data_len = 0, msg_cnt = 0; BIO_MSG *thismsg; if (!TEST_ptr(next)) return 0; /* * For simplicity we assume that all elements in the msg array have the * same data_len. They are not required to by the API, but it would be quite * strange for that not to be the case - and our code that calls * BIO_recvmmsg does do this (which is all that is important for this test * code). We test the invariant here. */ for (i = 0; i < num_msg; i++) { if (i == 0) data_len = msg[i].data_len; else if (!TEST_size_t_eq(msg[i].data_len, data_len)) return 0; } if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed)) return 0; msg_cnt = *msgs_processed; if (msg_cnt == num_msg) return 1; /* We've used all our slots and can't split any more */ assert(msg_cnt < num_msg); for (i = 0, thismsg = msg; i < msg_cnt; i++, thismsg++) { QUIC_PKT_HDR hdr; PACKET pkt; size_t remain; if (!PACKET_buf_init(&pkt, thismsg->data, thismsg->data_len)) return 0; /* Decode the packet header */ /* * TODO(QUIC SERVER): We need to query the short connection id len * here, e.g. via some API SSL_get_short_conn_id_len() */ if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL) != 1) return 0; remain = PACKET_remaining(&pkt); if (remain > 0) { for (j = msg_cnt; j > i; j--) { if (!bio_msg_copy(&msg[j], &msg[j - 1])) return 0; } thismsg->data_len -= remain; msg[i + 1].data_len = remain; memmove(msg[i + 1].data, (unsigned char *)msg[i + 1].data + thismsg->data_len, remain); msg_cnt++; } } *msgs_processed = msg_cnt; return 1; } /* Choose a sufficiently large type likely to be unused for this custom BIO */ #define BIO_TYPE_PKT_SPLIT_DGRAM_FILTER (0x81 | BIO_TYPE_FILTER) static BIO_METHOD *method_pkt_split_dgram = NULL; /* Note: Not thread safe! */ const BIO_METHOD *bio_f_pkt_split_dgram_filter(void) { if (method_pkt_split_dgram == NULL) { method_pkt_split_dgram = BIO_meth_new(BIO_TYPE_PKT_SPLIT_DGRAM_FILTER, "Packet splitting datagram filter"); if (method_pkt_split_dgram == NULL || !BIO_meth_set_ctrl(method_pkt_split_dgram, pkt_split_dgram_ctrl) || !BIO_meth_set_sendmmsg(method_pkt_split_dgram, pkt_split_dgram_sendmmsg) || !BIO_meth_set_recvmmsg(method_pkt_split_dgram, pkt_split_dgram_recvmmsg)) return NULL; } return method_pkt_split_dgram; } void bio_f_pkt_split_dgram_filter_free(void) { BIO_meth_free(method_pkt_split_dgram); }
./openssl/test/helpers/predefined_dhparams.c
/* * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ #include <openssl/evp.h> #include <openssl/core_names.h> #include <openssl/param_build.h> #include "predefined_dhparams.h" #ifndef OPENSSL_NO_DH static EVP_PKEY *get_dh_from_pg_bn(OSSL_LIB_CTX *libctx, const char *type, BIGNUM *p, BIGNUM *g, BIGNUM *q) { EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(libctx, type, NULL); OSSL_PARAM_BLD *tmpl = NULL; OSSL_PARAM *params = NULL; EVP_PKEY *dhpkey = NULL; if (pctx == NULL || EVP_PKEY_fromdata_init(pctx) <= 0) goto err; if ((tmpl = OSSL_PARAM_BLD_new()) == NULL || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) || (q != NULL && !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q))) goto err; params = OSSL_PARAM_BLD_to_param(tmpl); if (params == NULL || EVP_PKEY_fromdata(pctx, &dhpkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) goto err; err: EVP_PKEY_CTX_free(pctx); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(tmpl); return dhpkey; } static EVP_PKEY *get_dh_from_pg(OSSL_LIB_CTX *libctx, const char *type, unsigned char *pdata, size_t plen, unsigned char *gdata, size_t glen, unsigned char *qdata, size_t qlen) { EVP_PKEY *dhpkey = NULL; BIGNUM *p = NULL, *g = NULL, *q = NULL; p = BN_bin2bn(pdata, plen, NULL); g = BN_bin2bn(gdata, glen, NULL); if (p == NULL || g == NULL) goto err; if (qdata != NULL && (q = BN_bin2bn(qdata, qlen, NULL)) == NULL) goto err; dhpkey = get_dh_from_pg_bn(libctx, type, p, g, q); err: BN_free(p); BN_free(g); BN_free(q); return dhpkey; } EVP_PKEY *get_dh512(OSSL_LIB_CTX *libctx) { static unsigned char dh512_p[] = { 0xCB, 0xC8, 0xE1, 0x86, 0xD0, 0x1F, 0x94, 0x17, 0xA6, 0x99, 0xF0, 0xC6, 0x1F, 0x0D, 0xAC, 0xB6, 0x25, 0x3E, 0x06, 0x39, 0xCA, 0x72, 0x04, 0xB0, 0x6E, 0xDA, 0xC0, 0x61, 0xE6, 0x7A, 0x77, 0x25, 0xE8, 0x3B, 0xB9, 0x5F, 0x9A, 0xB6, 0xB5, 0xFE, 0x99, 0x0B, 0xA1, 0x93, 0x4E, 0x35, 0x33, 0xB8, 0xE1, 0xF1, 0x13, 0x4F, 0x59, 0x1A, 0xD2, 0x57, 0xC0, 0x26, 0x21, 0x33, 0x02, 0xC5, 0xAE, 0x23, }; static unsigned char dh512_g[] = { 0x02, }; return get_dh_from_pg(libctx, "DH", dh512_p, sizeof(dh512_p), dh512_g, sizeof(dh512_g), NULL, 0); } EVP_PKEY *get_dhx512(OSSL_LIB_CTX *libctx) { static unsigned char dhx512_p[] = { 0x00, 0xe8, 0x1a, 0xb7, 0x9a, 0x02, 0x65, 0x64, 0x94, 0x7b, 0xba, 0x09, 0x1c, 0x12, 0x27, 0x1e, 0xea, 0x89, 0x32, 0x64, 0x78, 0xf8, 0x1c, 0x78, 0x8e, 0x96, 0xc3, 0xc6, 0x9f, 0x41, 0x05, 0x41, 0x65, 0xae, 0xe3, 0x05, 0xea, 0x66, 0x21, 0xf7, 0x38, 0xb7, 0x2b, 0x32, 0x40, 0x5a, 0x14, 0x86, 0x51, 0x94, 0xb1, 0xcf, 0x01, 0xe3, 0x27, 0x28, 0xf6, 0x75, 0xa3, 0x15, 0xbb, 0x12, 0x4d, 0x99, 0xe7, }; static unsigned char dhx512_g[] = { 0x00, 0x91, 0xc1, 0x43, 0x6d, 0x0d, 0xb0, 0xa4, 0xde, 0x41, 0xb7, 0x93, 0xad, 0x51, 0x94, 0x1b, 0x43, 0xd8, 0x42, 0xf1, 0x5e, 0x46, 0x83, 0x5d, 0xf1, 0xd1, 0xf0, 0x41, 0x10, 0xd1, 0x1c, 0x5e, 0xad, 0x9b, 0x68, 0xb1, 0x6f, 0xf5, 0x8e, 0xaa, 0x6d, 0x71, 0x88, 0x37, 0xdf, 0x05, 0xf7, 0x6e, 0x7a, 0xb4, 0x25, 0x10, 0x6c, 0x7f, 0x38, 0xb4, 0xc8, 0xfc, 0xcc, 0x0c, 0x6a, 0x02, 0x08, 0x61, 0xf6, }; static unsigned char dhx512_q[] = { 0x00, 0xdd, 0xf6, 0x35, 0xad, 0xfa, 0x70, 0xc7, 0xe7, 0xa8, 0xf0, 0xe3, 0xda, 0x79, 0x34, 0x3f, 0x5b, 0xcf, 0x73, 0x82, 0x91, }; return get_dh_from_pg(libctx, "X9.42 DH", dhx512_p, sizeof(dhx512_p), dhx512_g, sizeof(dhx512_g), dhx512_q, sizeof(dhx512_q)); } EVP_PKEY *get_dh1024dsa(OSSL_LIB_CTX *libctx) { static unsigned char dh1024_p[] = { 0xC8, 0x00, 0xF7, 0x08, 0x07, 0x89, 0x4D, 0x90, 0x53, 0xF3, 0xD5, 0x00, 0x21, 0x1B, 0xF7, 0x31, 0xA6, 0xA2, 0xDA, 0x23, 0x9A, 0xC7, 0x87, 0x19, 0x3B, 0x47, 0xB6, 0x8C, 0x04, 0x6F, 0xFF, 0xC6, 0x9B, 0xB8, 0x65, 0xD2, 0xC2, 0x5F, 0x31, 0x83, 0x4A, 0xA7, 0x5F, 0x2F, 0x88, 0x38, 0xB6, 0x55, 0xCF, 0xD9, 0x87, 0x6D, 0x6F, 0x9F, 0xDA, 0xAC, 0xA6, 0x48, 0xAF, 0xFC, 0x33, 0x84, 0x37, 0x5B, 0x82, 0x4A, 0x31, 0x5D, 0xE7, 0xBD, 0x52, 0x97, 0xA1, 0x77, 0xBF, 0x10, 0x9E, 0x37, 0xEA, 0x64, 0xFA, 0xCA, 0x28, 0x8D, 0x9D, 0x3B, 0xD2, 0x6E, 0x09, 0x5C, 0x68, 0xC7, 0x45, 0x90, 0xFD, 0xBB, 0x70, 0xC9, 0x3A, 0xBB, 0xDF, 0xD4, 0x21, 0x0F, 0xC4, 0x6A, 0x3C, 0xF6, 0x61, 0xCF, 0x3F, 0xD6, 0x13, 0xF1, 0x5F, 0xBC, 0xCF, 0xBC, 0x26, 0x9E, 0xBC, 0x0B, 0xBD, 0xAB, 0x5D, 0xC9, 0x54, 0x39, }; static unsigned char dh1024_g[] = { 0x3B, 0x40, 0x86, 0xE7, 0xF3, 0x6C, 0xDE, 0x67, 0x1C, 0xCC, 0x80, 0x05, 0x5A, 0xDF, 0xFE, 0xBD, 0x20, 0x27, 0x74, 0x6C, 0x24, 0xC9, 0x03, 0xF3, 0xE1, 0x8D, 0xC3, 0x7D, 0x98, 0x27, 0x40, 0x08, 0xB8, 0x8C, 0x6A, 0xE9, 0xBB, 0x1A, 0x3A, 0xD6, 0x86, 0x83, 0x5E, 0x72, 0x41, 0xCE, 0x85, 0x3C, 0xD2, 0xB3, 0xFC, 0x13, 0xCE, 0x37, 0x81, 0x9E, 0x4C, 0x1C, 0x7B, 0x65, 0xD3, 0xE6, 0xA6, 0x00, 0xF5, 0x5A, 0x95, 0x43, 0x5E, 0x81, 0xCF, 0x60, 0xA2, 0x23, 0xFC, 0x36, 0xA7, 0x5D, 0x7A, 0x4C, 0x06, 0x91, 0x6E, 0xF6, 0x57, 0xEE, 0x36, 0xCB, 0x06, 0xEA, 0xF5, 0x3D, 0x95, 0x49, 0xCB, 0xA7, 0xDD, 0x81, 0xDF, 0x80, 0x09, 0x4A, 0x97, 0x4D, 0xA8, 0x22, 0x72, 0xA1, 0x7F, 0xC4, 0x70, 0x56, 0x70, 0xE8, 0x20, 0x10, 0x18, 0x8F, 0x2E, 0x60, 0x07, 0xE7, 0x68, 0x1A, 0x82, 0x5D, 0x32, 0xA2, }; return get_dh_from_pg(libctx, "DH", dh1024_p, sizeof(dh1024_p), dh1024_g, sizeof(dh1024_g), NULL, 0); } EVP_PKEY *get_dh2048(OSSL_LIB_CTX *libctx) { BIGNUM *p = NULL, *g = NULL; EVP_PKEY *dhpkey = NULL; g = BN_new(); if (g == NULL || !BN_set_word(g, 2)) goto err; p = BN_get_rfc3526_prime_2048(NULL); if (p == NULL) goto err; dhpkey = get_dh_from_pg_bn(libctx, "DH", p, g, NULL); err: BN_free(p); BN_free(g); return dhpkey; } EVP_PKEY *get_dh4096(OSSL_LIB_CTX *libctx) { BIGNUM *p = NULL, *g = NULL; EVP_PKEY *dhpkey = NULL; g = BN_new(); if (g == NULL || !BN_set_word(g, 2)) goto err; p = BN_get_rfc3526_prime_4096(NULL); if (p == NULL) goto err; dhpkey = get_dh_from_pg_bn(libctx, "DH", p, g, NULL); err: BN_free(p); BN_free(g); return dhpkey; } #endif
./openssl/test/helpers/handshake_srp.c
/* * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ /* * SRP is deprecated and there is no replacement. When SRP is removed, * the code in this file can be removed too. Until then we have to use * the deprecated APIs. */ #define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/srp.h> #include <openssl/ssl.h> #include "handshake.h" #include "../testutil.h" static char *client_srp_cb(SSL *s, void *arg) { CTX_DATA *ctx_data = (CTX_DATA*)(arg); return OPENSSL_strdup(ctx_data->srp_password); } static int server_srp_cb(SSL *s, int *ad, void *arg) { CTX_DATA *ctx_data = (CTX_DATA*)(arg); if (strcmp(ctx_data->srp_user, SSL_get_srp_username(s)) != 0) return SSL3_AL_FATAL; if (SSL_set_srp_server_param_pw(s, ctx_data->srp_user, ctx_data->srp_password, "2048" /* known group */) < 0) { *ad = SSL_AD_INTERNAL_ERROR; return SSL3_AL_FATAL; } return SSL_ERROR_NONE; } int configure_handshake_ctx_for_srp(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, const SSL_TEST_EXTRA_CONF *extra, CTX_DATA *server_ctx_data, CTX_DATA *server2_ctx_data, CTX_DATA *client_ctx_data) { if (extra->server.srp_user != NULL) { SSL_CTX_set_srp_username_callback(server_ctx, server_srp_cb); server_ctx_data->srp_user = OPENSSL_strdup(extra->server.srp_user); server_ctx_data->srp_password = OPENSSL_strdup(extra->server.srp_password); if (server_ctx_data->srp_user == NULL || server_ctx_data->srp_password == NULL) { OPENSSL_free(server_ctx_data->srp_user); OPENSSL_free(server_ctx_data->srp_password); server_ctx_data->srp_user = NULL; server_ctx_data->srp_password = NULL; return 0; } SSL_CTX_set_srp_cb_arg(server_ctx, server_ctx_data); } if (extra->server2.srp_user != NULL) { if (!TEST_ptr(server2_ctx)) return 0; SSL_CTX_set_srp_username_callback(server2_ctx, server_srp_cb); server2_ctx_data->srp_user = OPENSSL_strdup(extra->server2.srp_user); server2_ctx_data->srp_password = OPENSSL_strdup(extra->server2.srp_password); if (server2_ctx_data->srp_user == NULL || server2_ctx_data->srp_password == NULL) { OPENSSL_free(server2_ctx_data->srp_user); OPENSSL_free(server2_ctx_data->srp_password); server2_ctx_data->srp_user = NULL; server2_ctx_data->srp_password = NULL; return 0; } SSL_CTX_set_srp_cb_arg(server2_ctx, server2_ctx_data); } if (extra->client.srp_user != NULL) { if (!TEST_true(SSL_CTX_set_srp_username(client_ctx, extra->client.srp_user))) return 0; SSL_CTX_set_srp_client_pwd_callback(client_ctx, client_srp_cb); client_ctx_data->srp_password = OPENSSL_strdup(extra->client.srp_password); if (client_ctx_data->srp_password == NULL) return 0; SSL_CTX_set_srp_cb_arg(client_ctx, client_ctx_data); } return 1; }