code
stringlengths
49
1.37M
repo_name
stringclasses
117 values
path
stringlengths
17
73
from
stringclasses
1 value
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "internal/nelem.h" #include <openssl/pkcs12.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include "testutil.h" #include "helpers/pkcs12.h" static OSSL_LIB_CTX *testctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static int test_null_args(void) { return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL)); } static PKCS12 *PKCS12_load(const char *fpath) { BIO *bio = NULL; PKCS12 *p12 = NULL; bio = BIO_new_file(fpath, "rb"); if (!TEST_ptr(bio)) goto err; p12 = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default"); if (!TEST_ptr(p12)) goto err; if (!TEST_true(p12 == d2i_PKCS12_bio(bio, &p12))) goto err; BIO_free(bio); return p12; err: BIO_free(bio); PKCS12_free(p12); return NULL; } static const char *in_file = NULL; static const char *in_pass = ""; static int has_key = 0; static int has_cert = 0; static int has_ca = 0; static int changepass(PKCS12 *p12, EVP_PKEY *key, X509 *cert, STACK_OF(X509) *ca) { int ret = 0; PKCS12 *p12new = NULL; EVP_PKEY *key2 = NULL; X509 *cert2 = NULL; STACK_OF(X509) *ca2 = NULL; BIO *bio = NULL; if (!TEST_true(PKCS12_newpass(p12, in_pass, "NEWPASS"))) goto err; if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto err; if (!TEST_true(i2d_PKCS12_bio(bio, p12))) goto err; if (!TEST_ptr(p12new = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default"))) goto err; if (!TEST_ptr(d2i_PKCS12_bio(bio, &p12new))) goto err; if (!TEST_true(PKCS12_parse(p12new, "NEWPASS", &key2, &cert2, &ca2))) goto err; if (has_key) { if (!TEST_ptr(key2) || !TEST_int_eq(EVP_PKEY_eq(key, key2), 1)) goto err; } if (has_cert) { if (!TEST_ptr(cert2) || !TEST_int_eq(X509_cmp(cert, cert2), 0)) goto err; } ret = 1; err: BIO_free(bio); PKCS12_free(p12new); EVP_PKEY_free(key2); X509_free(cert2); OSSL_STACK_OF_X509_free(ca2); return ret; } static int pkcs12_parse_test(void) { int ret = 0; PKCS12 *p12 = NULL; EVP_PKEY *key = NULL; X509 *cert = NULL; STACK_OF(X509) *ca = NULL; if (in_file != NULL) { p12 = PKCS12_load(in_file); if (!TEST_ptr(p12)) goto err; if (!TEST_true(PKCS12_parse(p12, in_pass, &key, &cert, &ca))) goto err; if ((has_key && !TEST_ptr(key)) || (!has_key && !TEST_ptr_null(key))) goto err; if ((has_cert && !TEST_ptr(cert)) || (!has_cert && !TEST_ptr_null(cert))) goto err; if ((has_ca && !TEST_ptr(ca)) || (!has_ca && !TEST_ptr_null(ca))) goto err; if (has_key && !changepass(p12, key, cert, ca)) goto err; } ret = 1; err: PKCS12_free(p12); EVP_PKEY_free(key); X509_free(cert); OSSL_STACK_OF_X509_free(ca); return TEST_true(ret); } static int pkcs12_create_cb(PKCS12_SAFEBAG *bag, void *cbarg) { int cb_ret = *((int*)cbarg); return cb_ret; } static PKCS12 *pkcs12_create_ex2_setup(EVP_PKEY **key, X509 **cert, STACK_OF(X509) **ca) { PKCS12 *p12 = NULL; p12 = PKCS12_load("out6.p12"); if (!TEST_ptr(p12)) goto err; if (!TEST_true(PKCS12_parse(p12, "", key, cert, ca))) goto err; return p12; err: PKCS12_free(p12); return NULL; } static int pkcs12_create_ex2_test(int test) { int ret = 0, cb_ret = 0; PKCS12 *ptr = NULL, *p12 = NULL; EVP_PKEY *key = NULL; X509 *cert = NULL; STACK_OF(X509) *ca = NULL; p12 = pkcs12_create_ex2_setup(&key, &cert, &ca); if (!TEST_ptr(p12)) goto err; if (test == 0) { ptr = PKCS12_create_ex2(NULL, NULL, NULL, NULL, NULL, NID_undef, NID_undef, 0, 0, 0, testctx, NULL, NULL, NULL); if (TEST_ptr(ptr)) goto err; if (!TEST_ptr(cert)) goto err; cb_ret = 1; ptr = PKCS12_create_ex2(NULL, NULL, NULL, cert, NULL, NID_undef, NID_undef, 0, 0, 0, testctx, NULL, pkcs12_create_cb, (void*)&cb_ret); if (!TEST_ptr(ptr)) goto err; } else if (test == 1) { cb_ret = -1; ptr = PKCS12_create_ex2(NULL, NULL, NULL, cert, NULL, NID_undef, NID_undef, 0, 0, 0, testctx, NULL, pkcs12_create_cb, (void*)&cb_ret); if (TEST_ptr(ptr)) goto err; } else if (test == 2) { cb_ret = 0; ptr = PKCS12_create_ex2(NULL, NULL, NULL, cert, NULL, NID_undef, NID_undef, 0, 0, 0, testctx, NULL, pkcs12_create_cb, (void*)&cb_ret); if (!TEST_ptr(ptr)) goto err; } ret = 1; err: PKCS12_free(p12); PKCS12_free(ptr); EVP_PKEY_free(key); X509_free(cert); OSSL_STACK_OF_X509_free(ca); return TEST_true(ret); } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_IN_FILE, OPT_IN_PASS, OPT_IN_HAS_KEY, OPT_IN_HAS_CERT, OPT_IN_HAS_CA, OPT_LEGACY, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "in", OPT_IN_FILE, '<', "PKCS12 input file" }, { "pass", OPT_IN_PASS, 's', "PKCS12 input file password" }, { "has-key", OPT_IN_HAS_KEY, 'n', "Whether the input file does contain an user key" }, { "has-cert", OPT_IN_HAS_CERT, 'n', "Whether the input file does contain an user certificate" }, { "has-ca", OPT_IN_HAS_CA, 'n', "Whether the input file does contain other certificate" }, { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" }, { NULL } }; return options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_IN_FILE: in_file = opt_arg(); break; case OPT_IN_PASS: in_pass = opt_arg(); break; case OPT_LEGACY: break; case OPT_IN_HAS_KEY: has_key = opt_int_arg(); break; case OPT_IN_HAS_CERT: has_cert = opt_int_arg(); break; case OPT_IN_HAS_CA: has_ca = opt_int_arg(); break; case OPT_TEST_CASES: break; default: return 0; } } if (!test_get_libctx(&testctx, &nullprov, NULL, NULL, NULL)) { OSSL_LIB_CTX_free(testctx); testctx = NULL; return 0; } ADD_TEST(test_null_args); ADD_TEST(pkcs12_parse_test); ADD_ALL_TESTS(pkcs12_create_ex2_test, 3); return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(testctx); OSSL_PROVIDER_unload(nullprov); }
test
openssl/test/pkcs12_api_test.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <time.h> #include "internal/packet.h" #include "testutil.h" #define CLIENT_VERSION_LEN 2 #define TOTAL_NUM_TESTS 4 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0 #define TEST_ADD_PADDING 1 #define TEST_PADDING_NOT_NEEDED 2 #define TEST_ADD_PADDING_AND_PSK 3 #define F5_WORKAROUND_MIN_MSG_LEN 0x7f #define F5_WORKAROUND_MAX_MSG_LEN 0x200 static const char *sessionfile = NULL; #ifdef CHARSET_EBCDIC static const char alpn_prots[] = "|1234567890123456789012345678901234567890123456789012345678901234567890123456789" "|1234567890123456789012345678901234567890123456789012345678901234567890123456789"; #else static const char alpn_prots[] = "O1234567890123456789012345678901234567890123456789012345678901234567890123456789" "O1234567890123456789012345678901234567890123456789012345678901234567890123456789"; #endif static int test_client_hello(int currtest) { SSL_CTX *ctx; SSL *con = NULL; BIO *rbio; BIO *wbio; long len; unsigned char *data; PACKET pkt, pkt2, pkt3; char *dummytick = "Hello World!"; unsigned int type = 0; int testresult = 0; size_t msglen; BIO *sessbio = NULL; SSL_SESSION *sess = NULL; #ifdef OPENSSL_NO_TLS1_3 if (currtest == TEST_ADD_PADDING_AND_PSK) return 1; #endif memset(&pkt, 0, sizeof(pkt)); memset(&pkt2, 0, sizeof(pkt2)); memset(&pkt3, 0, sizeof(pkt3)); ctx = SSL_CTX_new(TLS_method()); if (!TEST_ptr(ctx)) goto end; if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, 0))) goto end; switch (currtest) { case TEST_SET_SESSION_TICK_DATA_VER_NEG: #if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2) SSL_CTX_free(ctx); return 1; #else if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))) goto end; #endif break; case TEST_ADD_PADDING_AND_PSK: if (!TEST_false(SSL_CTX_set_cipher_list(ctx, ""))) goto end; ERR_clear_error(); case TEST_ADD_PADDING: case TEST_PADDING_NOT_NEEDED: SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING); SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); if (currtest == TEST_ADD_PADDING) { if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, (unsigned char *)alpn_prots, sizeof(alpn_prots) - 1))) goto end; } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "AES128-SHA")) || !TEST_true(SSL_CTX_set_ciphersuites(ctx, "TLS_AES_128_GCM_SHA256"))) { goto end; } break; default: goto end; } con = SSL_new(ctx); if (!TEST_ptr(con)) goto end; if (currtest == TEST_ADD_PADDING_AND_PSK) { sessbio = BIO_new_file(sessionfile, "r"); if (!TEST_ptr(sessbio)) { TEST_info("Unable to open session.pem"); goto end; } sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL); if (!TEST_ptr(sess)) { TEST_info("Unable to load SSL_SESSION"); goto end; } if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL))) || !TEST_true(SSL_set_session(con, sess))) goto end; } rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { BIO_free(rbio); BIO_free(wbio); goto end; } SSL_set_bio(con, rbio, wbio); SSL_set_connect_state(con); if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))) goto end; } if (!TEST_int_le(SSL_connect(con), 0)) { goto end; } if (!TEST_long_ge(len = BIO_get_mem_data(wbio, (char **)&data), 0) || !TEST_true(PACKET_buf_init(&pkt, data, len)) || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)) goto end; msglen = PACKET_remaining(&pkt); if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) goto end; while (PACKET_remaining(&pkt2)) { if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) goto end; if (type == TLSEXT_TYPE_session_ticket) { if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { if (TEST_true(PACKET_equal(&pkt3, dummytick, strlen(dummytick)))) { testresult = 1; } goto end; } } if (type == TLSEXT_TYPE_padding) { if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED)) goto end; else if (TEST_true(currtest == TEST_ADD_PADDING || currtest == TEST_ADD_PADDING_AND_PSK)) testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN); } } if (currtest == TEST_PADDING_NOT_NEEDED) testresult = 1; end: SSL_free(con); SSL_CTX_free(ctx); SSL_SESSION_free(sess); BIO_free(sessbio); return testresult; } OPT_TEST_DECLARE_USAGE("sessionfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(sessionfile = test_get_argument(0))) return 0; ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS); return 1; }
test
openssl/test/clienthellotest.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/opensslconf.h> #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?" }; static BF_LONG bf_plain[2][2] = { {0x424c4f57L, 0x46495348L}, {0xfedcba98L, 0x76543210L} }; static BF_LONG bf_cipher[2][2] = { {0x324ed0feL, 0xf413a203L}, {0xcc91732bL, 0x8022f684L} }; # 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); 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; }
test
openssl/test/bftest.c
openssl
#include <string.h> #include <openssl/sha.h> #include "testutil.h" static int test_static_sha_common(const char *input, size_t length, const unsigned char *out, unsigned char *(*md)(const unsigned char *d, size_t n, unsigned char *md)) { unsigned char buf[EVP_MAX_MD_SIZE], *sbuf; const unsigned char *in = (unsigned char *)input; const size_t in_len = strlen(input); sbuf = (*md)(in, in_len, buf); if (!TEST_ptr(sbuf) || !TEST_ptr_eq(sbuf, buf) || !TEST_mem_eq(sbuf, length, out, length)) return 0; sbuf = (*md)(in, in_len, NULL); if (!TEST_ptr(sbuf) || !TEST_ptr_ne(sbuf, buf) || !TEST_mem_eq(sbuf, length, out, length)) return 0; return 1; } static int test_static_sha1(void) { static const unsigned char output[SHA_DIGEST_LENGTH] = { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d }; return test_static_sha_common("abc", SHA_DIGEST_LENGTH, output, &SHA1); } static int test_static_sha224(void) { static const unsigned char output[SHA224_DIGEST_LENGTH] = { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }; return test_static_sha_common("abc", SHA224_DIGEST_LENGTH, output, &SHA224); } static int test_static_sha256(void) { static const unsigned char output[SHA256_DIGEST_LENGTH] = { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }; return test_static_sha_common("abc", SHA256_DIGEST_LENGTH, output, &SHA256); } static int test_static_sha384(void) { static const unsigned char output[SHA384_DIGEST_LENGTH] = { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }; return test_static_sha_common("abc", SHA384_DIGEST_LENGTH, output, &SHA384); } static int test_static_sha512(void) { static const unsigned char output[SHA512_DIGEST_LENGTH] = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f }; return test_static_sha_common("abc", SHA512_DIGEST_LENGTH, output, &SHA512); } int setup_tests(void) { ADD_TEST(test_static_sha1); ADD_TEST(test_static_sha224); ADD_TEST(test_static_sha256); ADD_TEST(test_static_sha384); ADD_TEST(test_static_sha512); return 1; }
test
openssl/test/sha_test.c
openssl
#include <stdarg.h> #include <openssl/evp.h> #include "testutil.h" #include "internal/nelem.h" #include "internal/property.h" #include "../crypto/property/property_local.h" struct ossl_provider_st { int x; }; static int add_property_names(const char *n, ...) { va_list args; int res = 1; va_start(args, n); do { if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0)) res = 0; } while ((n = va_arg(args, const char *)) != NULL); va_end(args); return res; } static int up_ref(void *p) { return 1; } static void down_ref(void *p) { } static int test_property_string(void) { OSSL_LIB_CTX *ctx; OSSL_METHOD_STORE *store = NULL; int res = 0; OSSL_PROPERTY_IDX i, j; if (TEST_ptr(ctx = OSSL_LIB_CTX_new()) && TEST_ptr(store = ossl_method_store_new(ctx)) && TEST_int_eq(ossl_property_name(ctx, "fnord", 0), 0) && TEST_int_ne(ossl_property_name(ctx, "fnord", 1), 0) && TEST_int_ne(ossl_property_name(ctx, "name", 1), 0) && TEST_str_eq(ossl_property_name_str(ctx, 1), "provider") && TEST_str_eq(ossl_property_name_str(ctx, 2), "version") && TEST_str_eq(ossl_property_name_str(ctx, 3), "fips") && TEST_str_eq(ossl_property_name_str(ctx, 4), "output") && TEST_str_eq(ossl_property_name_str(ctx, 5), "input") && TEST_str_eq(ossl_property_name_str(ctx, 6), "structure") && TEST_str_eq(ossl_property_name_str(ctx, 7), "fnord") && TEST_str_eq(ossl_property_name_str(ctx, 8), "name") && TEST_ptr_null(ossl_property_name_str(ctx, 0)) && TEST_ptr_null(ossl_property_name_str(ctx, 9)) && TEST_int_eq(ossl_property_value(ctx, "fnord", 0), 0) && TEST_int_ne(i = ossl_property_value(ctx, "no", 0), 0) && TEST_int_ne(j = ossl_property_value(ctx, "yes", 0), 0) && TEST_int_ne(i, j) && TEST_int_eq(ossl_property_value(ctx, "yes", 1), j) && TEST_int_eq(ossl_property_value(ctx, "no", 1), i) && TEST_int_ne(i = ossl_property_value(ctx, "illuminati", 1), 0) && TEST_int_eq(j = ossl_property_value(ctx, "fnord", 1), i + 1) && TEST_int_eq(ossl_property_value(ctx, "fnord", 1), j) && TEST_str_eq(ossl_property_value_str(ctx, 1), "yes") && TEST_str_eq(ossl_property_value_str(ctx, 2), "no") && TEST_str_eq(ossl_property_value_str(ctx, 3), "illuminati") && TEST_str_eq(ossl_property_value_str(ctx, 4), "fnord") && TEST_ptr_null(ossl_property_value_str(ctx, 0)) && TEST_ptr_null(ossl_property_value_str(ctx, 5)) && TEST_int_eq(ossl_property_value(ctx, "cold", 0), 0) && TEST_int_ne(ossl_property_name(ctx, "fnord", 0), ossl_property_value(ctx, "fnord", 0))) res = 1; ossl_method_store_free(store); OSSL_LIB_CTX_free(ctx); return res; } static const struct { const char *defn; const char *query; int e; } parser_tests[] = { { "", "sky=blue", -1 }, { "", "sky!=blue", 1 }, { "groan", "", 0 }, { "cold=yes", "cold=yes", 1 }, { "cold=yes", "cold", 1 }, { "cold=yes", "cold!=no", 1 }, { "groan", "groan=yes", 1 }, { "groan", "groan=no", -1 }, { "groan", "groan!=yes", -1 }, { "cold=no", "cold", -1 }, { "cold=no", "?cold", 0 }, { "cold=no", "cold=no", 1 }, { "groan", "cold", -1 }, { "groan", "cold=no", 1 }, { "groan", "cold!=yes", 1 }, { "groan=blue", "groan=yellow", -1 }, { "groan=blue", "?groan=yellow", 0 }, { "groan=blue", "groan!=yellow", 1 }, { "groan=blue", "?groan!=yellow", 1 }, { "today=monday, tomorrow=3", "today!=2", 1 }, { "today=monday, tomorrow=3", "today!='monday'", -1 }, { "today=monday, tomorrow=3", "tomorrow=3", 1 }, { "n=0x3", "n=3", 1 }, { "n=0x3", "n=-3", -1 }, { "n=0x33", "n=51", 1 }, { "n=0x123456789abcdef", "n=0x123456789abcdef", 1 }, { "n=0x7fffffffffffffff", "n=0x7fffffffffffffff", 1 }, { "n=9223372036854775807", "n=9223372036854775807", 1 }, { "n=0777777777777777777777", "n=0777777777777777777777", 1 }, { "n=033", "n=27", 1 }, { "n=0", "n=00", 1 }, { "n=0x0", "n=0", 1 }, { "n=0, sky=blue", "?n=0, sky=blue", 2 }, { "n=1, sky=blue", "?n=0, sky=blue", 1 }, }; static int test_property_parse(int n) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *p = NULL, *q = NULL; int r = 0; if (TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n", NULL) && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn)) && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query, 0)) && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e)) r = 1; ossl_property_free(p); ossl_property_free(q); ossl_method_store_free(store); return r; } static int test_property_query_value_create(void) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *p = NULL, *q = NULL, *o = NULL; int r = 0; if (TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("wood", NULL) && TEST_ptr(p = ossl_parse_query(NULL, "wood=oak", 0)) && TEST_ptr(q = ossl_parse_query(NULL, "wood=oak", 1)) && TEST_ptr(o = ossl_parse_query(NULL, "wood=oak", 0)) && TEST_int_eq(ossl_property_match_count(q, p), -1) && TEST_int_eq(ossl_property_match_count(q, o), 1)) r = 1; ossl_property_free(o); ossl_property_free(p); ossl_property_free(q); ossl_method_store_free(store); return r; } static const struct { int query; const char *ps; } parse_error_tests[] = { { 0, "n=1, n=1" }, { 0, "n=1, a=hi, n=1" }, { 1, "n=1, a=bye, ?n=0" }, { 0, "a=abc,#@!, n=1" }, { 1, "a='Hello" }, { 0, "a=\"World" }, { 0, "a=_abd_" }, { 1, "a=2, n=012345678" }, { 0, "n=0x28FG, a=3" }, { 0, "n=145d, a=2" }, { 0, "n=0x8000000000000000, a=3" }, { 0, "n=922337203000000000d, a=2" }, { 0, "a=2, n=1000000000000000000000" }, { 1, "@='hello'" }, { 1, "n0123456789012345678901234567890123456789" "0123456789012345678901234567890123456789" "0123456789012345678901234567890123456789" "0123456789012345678901234567890123456789=yes" }, { 0, ".n=3" }, { 1, "fnord.fnord.=3" } }; static int test_property_parse_error(int n) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *p = NULL; int r = 0; const char *ps; if (!TEST_ptr(store = ossl_method_store_new(NULL)) || !add_property_names("a", "n", NULL)) goto err; ps = parse_error_tests[n].ps; if (parse_error_tests[n].query) { if (!TEST_ptr_null(p = ossl_parse_query(NULL, ps, 1))) goto err; } else if (!TEST_ptr_null(p = ossl_parse_property(NULL, ps))) { goto err; } r = 1; err: ossl_property_free(p); ossl_method_store_free(store); return r; } static const struct { const char *q_global; const char *q_local; const char *prop; } merge_tests[] = { { "", "colour=blue", "colour=blue" }, { "colour=blue", "", "colour=blue" }, { "colour=red", "colour=blue", "colour=blue" }, { "clouds=pink, urn=red", "urn=blue, colour=green", "urn=blue, colour=green, clouds=pink" }, { "pot=gold", "urn=blue", "pot=gold, urn=blue" }, { "night", "day", "day=yes, night=yes" }, { "day", "night", "day=yes, night=yes" }, { "", "", "" }, { "day=yes", "-day", "day=no" }, { "day=yes", "-day", "day=yes" }, { "day=yes", "-day", "day=arglebargle" }, { "day=yes", "-day", "pot=sesquioxidizing" }, { "day, night", "-night, day", "day=yes, night=no" }, { "-day", "day=yes", "day=yes" }, }; static int test_property_merge(int n) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL; OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL; int r = 0; if (TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("colour", "urn", "clouds", "pot", "day", "night", NULL) && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop)) && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global, 0)) && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0)) && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global)) && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0)) r = 1; ossl_property_free(q_global); ossl_property_free(q_local); ossl_property_free(q_combined); ossl_property_free(prop); ossl_method_store_free(store); return r; } static int test_property_defn_cache(void) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *red = NULL, *blue = NULL, *blue2 = NULL; int r; r = TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("red", "blue", NULL) && TEST_ptr(red = ossl_parse_property(NULL, "red")) && TEST_ptr(blue = ossl_parse_property(NULL, "blue")) && TEST_ptr_ne(red, blue) && TEST_true(ossl_prop_defn_set(NULL, "red", &red)); if (!r) { ossl_property_free(red); red = NULL; ossl_property_free(blue); blue = NULL; } r = r && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue)); if (!r) { ossl_property_free(blue); blue = NULL; } r = r && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red) && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue) && TEST_ptr(blue2 = ossl_parse_property(NULL, "blue")) && TEST_ptr_ne(blue2, blue) && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue2)); if (!r) { ossl_property_free(blue2); blue2 = NULL; } r = r && TEST_ptr_eq(blue2, blue) && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue); ossl_method_store_free(store); return r; } static const struct { const char *defn; const char *query; int e; } definition_tests[] = { { "alpha", "alpha=yes", 1 }, { "alpha=no", "alpha", -1 }, { "alpha=1", "alpha=1", 1 }, { "alpha=2", "alpha=1",-1 }, { "alpha", "omega", -1 }, { "alpha", "?omega", 0 }, { "alpha", "?omega=1", 0 }, { "alpha", "?omega=no", 1 }, { "alpha", "?omega=yes", 0 }, { "alpha, omega", "?omega=yes", 1 }, { "alpha, omega", "?omega=no", 0 } }; static int test_definition_compares(int n) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *d = NULL, *q = NULL; int r; r = TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("alpha", "omega", NULL) && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn)) && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0)) && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e); ossl_property_free(d); ossl_property_free(q); ossl_method_store_free(store); return r; } static int test_register_deregister(void) { static const struct { int nid; const char *prop; char *impl; } impls[] = { { 6, "position=1", "a" }, { 6, "position=2", "b" }, { 6, "position=3", "c" }, { 6, "position=4", "d" }, }; size_t i; int ret = 0; OSSL_METHOD_STORE *store; OSSL_PROVIDER prov = { 1 }; if (!TEST_ptr(store = ossl_method_store_new(NULL)) || !add_property_names("position", NULL)) goto err; for (i = 0; i < OSSL_NELEM(impls); i++) if (!TEST_true(ossl_method_store_add(store, &prov, impls[i].nid, impls[i].prop, impls[i].impl, &up_ref, &down_ref))) { TEST_note("iteration %zd", i + 1); goto err; } for (i = 0; i < OSSL_NELEM(impls); i++) { const size_t j = (1 + i * 3) % OSSL_NELEM(impls); int nid = impls[j].nid; void *impl = impls[j].impl; if (!TEST_true(ossl_method_store_remove(store, nid, impl)) || !TEST_false(ossl_method_store_remove(store, nid, impl))) { TEST_note("iteration %zd, position %zd", i + 1, j + 1); goto err; } } if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl))) ret = 1; err: ossl_method_store_free(store); return ret; } static int test_property(void) { static OSSL_PROVIDER fake_provider1 = { 1 }; static OSSL_PROVIDER fake_provider2 = { 2 }; static const OSSL_PROVIDER *fake_prov1 = &fake_provider1; static const OSSL_PROVIDER *fake_prov2 = &fake_provider2; static const struct { const OSSL_PROVIDER **prov; int nid; const char *prop; char *impl; } impls[] = { { &fake_prov1, 1, "fast=no, colour=green", "a" }, { &fake_prov1, 1, "fast, colour=blue", "b" }, { &fake_prov1, 1, "", "-" }, { &fake_prov2, 9, "sky=blue, furry", "c" }, { &fake_prov2, 3, NULL, "d" }, { &fake_prov2, 6, "sky.colour=blue, sky=green, old.data", "e" }, }; static struct { const OSSL_PROVIDER **prov; int nid; const char *prop; char *expected; } queries[] = { { &fake_prov1, 1, "fast", "b" }, { &fake_prov1, 1, "fast=yes", "b" }, { &fake_prov1, 1, "fast=no, colour=green", "a" }, { &fake_prov1, 1, "colour=blue, fast", "b" }, { &fake_prov1, 1, "colour=blue", "b" }, { &fake_prov2, 9, "furry", "c" }, { &fake_prov2, 6, "sky.colour=blue", "e" }, { &fake_prov2, 6, "old.data", "e" }, { &fake_prov2, 9, "furry=yes, sky=blue", "c" }, { &fake_prov1, 1, "", "a" }, { &fake_prov2, 3, "", "d" }, }; OSSL_METHOD_STORE *store; size_t i; int ret = 0; void *result; if (!TEST_ptr(store = ossl_method_store_new(NULL)) || !add_property_names("fast", "colour", "sky", "furry", NULL)) goto err; for (i = 0; i < OSSL_NELEM(impls); i++) if (!TEST_true(ossl_method_store_add(store, *impls[i].prov, impls[i].nid, impls[i].prop, impls[i].impl, &up_ref, &down_ref))) { TEST_note("iteration %zd", i + 1); goto err; } for (i = 0; i < OSSL_NELEM(queries); i++) { const OSSL_PROVIDER *nullprov = NULL; OSSL_PROPERTY_LIST *pq = NULL; if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid, queries[i].prop, &nullprov, &result)) || !TEST_str_eq((char *)result, queries[i].expected)) { TEST_note("iteration %zd", i + 1); ossl_property_free(pq); goto err; } ossl_property_free(pq); } for (i = 0; i < OSSL_NELEM(queries); i++) { OSSL_PROPERTY_LIST *pq = NULL; result = NULL; if (queries[i].prov == &fake_prov1) { if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid, queries[i].prop, &fake_prov1, &result)) || !TEST_ptr_eq(fake_prov1, &fake_provider1) || !TEST_str_eq((char *)result, queries[i].expected)) { TEST_note("iteration %zd", i + 1); ossl_property_free(pq); goto err; } } else { if (!TEST_false(ossl_method_store_fetch(store, queries[i].nid, queries[i].prop, &fake_prov1, &result)) || !TEST_ptr_eq(fake_prov1, &fake_provider1) || !TEST_ptr_null(result)) { TEST_note("iteration %zd", i + 1); ossl_property_free(pq); goto err; } } ossl_property_free(pq); } for (i = 0; i < OSSL_NELEM(queries); i++) { OSSL_PROPERTY_LIST *pq = NULL; result = NULL; if (queries[i].prov == &fake_prov2) { if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid, queries[i].prop, &fake_prov2, &result)) || !TEST_ptr_eq(fake_prov2, &fake_provider2) || !TEST_str_eq((char *)result, queries[i].expected)) { TEST_note("iteration %zd", i + 1); ossl_property_free(pq); goto err; } } else { if (!TEST_false(ossl_method_store_fetch(store, queries[i].nid, queries[i].prop, &fake_prov2, &result)) || !TEST_ptr_eq(fake_prov2, &fake_provider2) || !TEST_ptr_null(result)) { TEST_note("iteration %zd", i + 1); ossl_property_free(pq); goto err; } } ossl_property_free(pq); } ret = 1; err: ossl_method_store_free(store); return ret; } static int test_query_cache_stochastic(void) { const int max = 10000, tail = 10; OSSL_METHOD_STORE *store; int i, res = 0; char buf[50]; void *result; int errors = 0; int v[10001]; OSSL_PROVIDER prov = { 1 }; if (!TEST_ptr(store = ossl_method_store_new(NULL)) || !add_property_names("n", NULL)) goto err; for (i = 1; i <= max; i++) { v[i] = 2 * i; BIO_snprintf(buf, sizeof(buf), "n=%d\n", i); if (!TEST_true(ossl_method_store_add(store, &prov, i, buf, "abc", &up_ref, &down_ref)) || !TEST_true(ossl_method_store_cache_set(store, &prov, i, buf, v + i, &up_ref, &down_ref)) || !TEST_true(ossl_method_store_cache_set(store, &prov, i, "n=1234", "miss", &up_ref, &down_ref))) { TEST_note("iteration %d", i); goto err; } } for (i = 1; i <= max; i++) { BIO_snprintf(buf, sizeof(buf), "n=%d\n", i); if (!ossl_method_store_cache_get(store, NULL, i, buf, &result) || result != v + i) errors++; } res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail); err: ossl_method_store_free(store); return res; } static int test_fips_mode(void) { int ret = 0; OSSL_LIB_CTX *ctx = NULL; if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())) goto err; ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes")) && TEST_true(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes")) && TEST_false(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_set_default_properties(ctx, "fips=no")) && TEST_false(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_set_default_properties(ctx, "fips!=no")) && TEST_true(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_set_default_properties(ctx, "fips=no")) && TEST_false(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes")) && TEST_true(EVP_default_properties_enable_fips(ctx, 1)) && TEST_true(EVP_default_properties_is_fips_enabled(ctx)) && TEST_true(EVP_default_properties_enable_fips(ctx, 0)) && TEST_false(EVP_default_properties_is_fips_enabled(ctx)); err: OSSL_LIB_CTX_free(ctx); return ret; } static struct { const char *in; const char *out; } to_string_tests[] = { { "fips=yes", "fips=yes" }, { "fips!=yes", "fips!=yes" }, { "fips = yes", "fips=yes" }, { "fips", "fips=yes" }, { "fips=no", "fips=no" }, { "-fips", "-fips" }, { "?fips=yes", "?fips=yes" }, { "fips=yes,provider=fips", "fips=yes,provider=fips" }, { "fips = yes , provider = fips", "fips=yes,provider=fips" }, { "fips=yes,provider!=fips", "fips=yes,provider!=fips" }, { "fips=yes,?provider=fips", "fips=yes,?provider=fips" }, { "fips=yes,-provider", "fips=yes,-provider" }, { "foo=yes,fips=yes", "fips=yes"}, { "", "" }, { "fips=3", "fips=3" }, { "fips=-3", "fips=-3" }, { "provider='foo bar'", "provider='foo bar'" }, { "provider=\"foo bar'\"", "provider=\"foo bar'\"" }, { "provider=abc***", "provider='abc***'" }, { NULL, "" } }; static int test_property_list_to_string(int i) { OSSL_PROPERTY_LIST *pl = NULL; int ret = 0; size_t bufsize; char *buf = NULL; if (to_string_tests[i].in != NULL && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1))) goto err; bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0); if (!TEST_size_t_gt(bufsize, 0)) goto err; buf = OPENSSL_malloc(bufsize); if (!TEST_ptr(buf) || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf, bufsize), bufsize) || !TEST_str_eq(to_string_tests[i].out, buf) || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1)) goto err; ret = 1; err: OPENSSL_free(buf); ossl_property_free(pl); return ret; } int setup_tests(void) { ADD_TEST(test_property_string); ADD_TEST(test_property_query_value_create); ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests)); ADD_ALL_TESTS(test_property_parse_error, OSSL_NELEM(parse_error_tests)); ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests)); ADD_TEST(test_property_defn_cache); ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests)); ADD_TEST(test_register_deregister); ADD_TEST(test_property); ADD_TEST(test_query_cache_stochastic); ADD_TEST(test_fips_mode); ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests)); return 1; }
test
openssl/test/property_test.c
openssl
#include <stdio.h> #include <string.h> #include <time.h> #include <openssl/asn1.h> #include <openssl/x509.h> #include "testutil.h" #include "internal/nelem.h" typedef struct { const char *data; int time_result; int type; } TESTDATA; static TESTDATA tests[] = { { "20001201000000Z", 0, V_ASN1_GENERALIZEDTIME }, { "20001201010000+0100", 0, V_ASN1_GENERALIZEDTIME }, { "20001201050000+0500", 0, V_ASN1_GENERALIZEDTIME }, { "20001130230000-0100", 0, V_ASN1_GENERALIZEDTIME }, { "20001130190000-0500", 0, V_ASN1_GENERALIZEDTIME }, { "20001130190001-0500", 1, V_ASN1_GENERALIZEDTIME }, { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, { "001201000000Z", 0, V_ASN1_UTCTIME }, { "001201010000+0100", 0, V_ASN1_UTCTIME }, { "001201050000+0500", 0, V_ASN1_UTCTIME }, { "001130230000-0100", 0, V_ASN1_UTCTIME }, { "001130190000-0500", 0, V_ASN1_UTCTIME }, { "001201000000-0000", 0, V_ASN1_UTCTIME }, { "001201000001-0000", 1, V_ASN1_UTCTIME }, { "001130235959-0000", -1, V_ASN1_UTCTIME }, { "20001201000000+0000", 0, V_ASN1_GENERALIZEDTIME }, { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME }, { "001201000000+0100", -1, V_ASN1_UTCTIME }, { "20001201000000-0100", 1, V_ASN1_GENERALIZEDTIME }, { "001201000000-0100", 1, V_ASN1_UTCTIME }, { "20001201123400+1234", 0, V_ASN1_GENERALIZEDTIME }, { "20001130112600-1234", 0, V_ASN1_GENERALIZEDTIME }, }; static time_t the_time = 975628800; static ASN1_TIME the_asn1_time = { 15, V_ASN1_GENERALIZEDTIME, (unsigned char*)"20001201000000Z", 0 }; static int test_offset(int idx) { ASN1_TIME at; const TESTDATA *testdata = &tests[idx]; int ret = -2; int day, sec; at.data = (unsigned char*)testdata->data; at.length = strlen(testdata->data); at.type = testdata->type; at.flags = 0; if (!TEST_true(ASN1_TIME_diff(&day, &sec, &the_asn1_time, &at))) { TEST_info("ASN1_TIME_diff() failed for %s\n", at.data); return 0; } if (day > 0) ret = 1; else if (day < 0) ret = -1; else if (sec > 0) ret = 1; else if (sec < 0) ret = -1; else ret = 0; if (!TEST_int_eq(testdata->time_result, ret)) { TEST_info("ASN1_TIME_diff() test failed for %s day=%d sec=%d\n", at.data, day, sec); return 0; } ret = ASN1_TIME_cmp_time_t(&at, the_time); if (!TEST_int_eq(testdata->time_result, ret)) { TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data); return 0; } return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests)); return 1; }
test
openssl/test/time_offset_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include "internal/nelem.h" #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/bn.h> #include "testutil.h" #include "rsa_local.h" #include <openssl/rsa.h> static const unsigned char cav_e[] = { 0x01,0x00,0x01 }; static const unsigned char cav_p[] = { 0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0, 0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02, 0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d, 0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41, 0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xbc,0x4c, 0x01,0xa5,0x4b,0xbd,0xa4,0x20,0xb5,0x20,0xd5,0x59,0x6f,0x82,0x5c,0x8f,0x4f, 0xe0,0x3a,0x4e,0x7e,0xfe,0x44,0xf3,0x3c,0xc0,0x0e,0x14,0x2b,0x32,0xe6,0x28, 0x8b,0x63,0x87,0x00,0xc3,0x53,0x4a,0x5b,0x71,0x7a,0x5b,0x28,0x40,0xc4,0x18, 0xb6,0x77,0x0b,0xab,0x59,0xa4,0x96,0x7d }; static const unsigned char cav_q[] = { 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb, 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf, 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68, 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e, 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xc4,0x44,0xdd, 0xf7,0x7e,0xda,0x47,0x4a,0x67,0x44,0x5d,0x4e,0x75,0xf0,0x4d,0x00,0x68,0xe1, 0x4a,0xec,0x1f,0x45,0xf9,0xe6,0xca,0x38,0x95,0x48,0x6f,0xdc,0x9d,0x1b,0xa3, 0x4b,0xfd,0x08,0x4b,0x54,0xcd,0xeb,0x3d,0xef,0x33,0x11,0x6e,0xce,0xe4,0x5d, 0xef,0xa9,0x58,0x5c,0x87,0x4d,0xc8,0xcf }; static const unsigned char cav_n[] = { 0xce,0x5e,0x8d,0x1a,0xa3,0x08,0x7a,0x2d,0xb4,0x49,0x48,0xf0,0x06,0xb6,0xfe, 0xba,0x2f,0x39,0x7c,0x7b,0xe0,0x5d,0x09,0x2d,0x57,0x4e,0x54,0x60,0x9c,0xe5, 0x08,0x4b,0xe1,0x1a,0x73,0xc1,0x5e,0x2f,0xb6,0x46,0xd7,0x81,0xca,0xbc,0x98, 0xd2,0xf9,0xef,0x1c,0x92,0x8c,0x8d,0x99,0x85,0x28,0x52,0xd6,0xd5,0xab,0x70, 0x7e,0x9e,0xa9,0x87,0x82,0xc8,0x95,0x64,0xeb,0xf0,0x6c,0x0f,0x3f,0xe9,0x02, 0x29,0x2e,0x6d,0xa1,0xec,0xbf,0xdc,0x23,0xdf,0x82,0x4f,0xab,0x39,0x8d,0xcc, 0xac,0x21,0x51,0x14,0xf8,0xef,0xec,0x73,0x80,0x86,0xa3,0xcf,0x8f,0xd5,0xcf, 0x22,0x1f,0xcc,0x23,0x2f,0xba,0xcb,0xf6,0x17,0xcd,0x3a,0x1f,0xd9,0x84,0xb9, 0x88,0xa7,0x78,0x0f,0xaa,0xc9,0x04,0x01,0x20,0x72,0x5d,0x2a,0xfe,0x5b,0xdd, 0x16,0x5a,0xed,0x83,0x02,0x96,0x39,0x46,0x37,0x30,0xc1,0x0d,0x87,0xc2,0xc8, 0x33,0x38,0xed,0x35,0x72,0xe5,0x29,0xf8,0x1f,0x23,0x60,0xe1,0x2a,0x5b,0x1d, 0x6b,0x53,0x3f,0x07,0xc4,0xd9,0xbb,0x04,0x0c,0x5c,0x3f,0x0b,0xc4,0xd4,0x61, 0x96,0x94,0xf1,0x0f,0x4a,0x49,0xac,0xde,0xd2,0xe8,0x42,0xb3,0x4a,0x0b,0x64, 0x7a,0x32,0x5f,0x2b,0x5b,0x0f,0x8b,0x8b,0xe0,0x33,0x23,0x34,0x64,0xf8,0xb5, 0x7f,0x69,0x60,0xb8,0x71,0xe9,0xff,0x92,0x42,0xb1,0xf7,0x23,0xa8,0xa7,0x92, 0x04,0x3d,0x6b,0xff,0xf7,0xab,0xbb,0x14,0x1f,0x4c,0x10,0x97,0xd5,0x6b,0x71, 0x12,0xfd,0x93,0xa0,0x4a,0x3b,0x75,0x72,0x40,0x96,0x1c,0x5f,0x40,0x40,0x57, 0x13 }; static const unsigned char cav_d[] = { 0x47,0x47,0x49,0x1d,0x66,0x2a,0x4b,0x68,0xf5,0xd8,0x4a,0x24,0xfd,0x6c,0xbf, 0x56,0xb7,0x70,0xf7,0x9a,0x21,0xc8,0x80,0x9e,0xf4,0x84,0xcd,0x88,0x01,0x28, 0xea,0x50,0xab,0x13,0x63,0xdf,0xea,0x14,0x38,0xb5,0x07,0x42,0x81,0x2f,0xda, 0xe9,0x24,0x02,0x7e,0xaf,0xef,0x74,0x09,0x0e,0x80,0xfa,0xfb,0xd1,0x19,0x41, 0xe5,0xba,0x0f,0x7c,0x0a,0xa4,0x15,0x55,0xa2,0x58,0x8c,0x3a,0x48,0x2c,0xc6, 0xde,0x4a,0x76,0xfb,0x72,0xb6,0x61,0xe6,0xd2,0x10,0x44,0x4c,0x33,0xb8,0xd2, 0x74,0xb1,0x9d,0x3b,0xcd,0x2f,0xb1,0x4f,0xc3,0x98,0xbd,0x83,0xb7,0x7e,0x75, 0xe8,0xa7,0x6a,0xee,0xcc,0x51,0x8c,0x99,0x17,0x67,0x7f,0x27,0xf9,0x0d,0x6a, 0xb7,0xd4,0x80,0x17,0x89,0x39,0x9c,0xf3,0xd7,0x0f,0xdf,0xb0,0x55,0x80,0x1d, 0xaf,0x57,0x2e,0xd0,0xf0,0x4f,0x42,0x69,0x55,0xbc,0x83,0xd6,0x97,0x83,0x7a, 0xe6,0xc6,0x30,0x6d,0x3d,0xb5,0x21,0xa7,0xc4,0x62,0x0a,0x20,0xce,0x5e,0x5a, 0x17,0x98,0xb3,0x6f,0x6b,0x9a,0xeb,0x6b,0xa3,0xc4,0x75,0xd8,0x2b,0xdc,0x5c, 0x6f,0xec,0x5d,0x49,0xac,0xa8,0xa4,0x2f,0xb8,0x8c,0x4f,0x2e,0x46,0x21,0xee, 0x72,0x6a,0x0e,0x22,0x80,0x71,0xc8,0x76,0x40,0x44,0x61,0x16,0xbf,0xa5,0xf8, 0x89,0xc7,0xe9,0x87,0xdf,0xbd,0x2e,0x4b,0x4e,0xc2,0x97,0x53,0xe9,0x49,0x1c, 0x05,0xb0,0x0b,0x9b,0x9f,0x21,0x19,0x41,0xe9,0xf5,0x61,0xd7,0x33,0x2e,0x2c, 0x94,0xb8,0xa8,0x9a,0x3a,0xcc,0x6a,0x24,0x8d,0x19,0x13,0xee,0xb9,0xb0,0x48, 0x61 }; static BIGNUM *bn_load_new(const unsigned char *data, int sz) { BIGNUM *ret = BN_new(); if (ret != NULL) BN_bin2bn(data, sz, ret); return ret; } static int test_check_public_exponent(void) { int ret = 0; BIGNUM *e = NULL; ret = TEST_ptr(e = BN_new()) && TEST_true(BN_set_word(e, 1)) && TEST_false(ossl_rsa_check_public_exponent(e)) && TEST_true(BN_set_word(e, 65536)) && TEST_false(ossl_rsa_check_public_exponent(e)) && TEST_true(BN_set_word(e, 3)) && TEST_true(ossl_rsa_check_public_exponent(e)) && TEST_true(BN_set_word(e, 17)) && TEST_true(ossl_rsa_check_public_exponent(e)) && TEST_true(BN_set_word(e, 65537)) && TEST_true(ossl_rsa_check_public_exponent(e)) && TEST_true(BN_lshift(e, BN_value_one(), 256)) && TEST_true(BN_add(e, e, BN_value_one())) && TEST_true(ossl_rsa_check_public_exponent(e)); BN_free(e); return ret; } static int test_check_prime_factor_range(void) { int ret = 0; BN_CTX *ctx = NULL; BIGNUM *p = NULL; BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL; static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F }; static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 }; static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF }; ret = TEST_ptr(p = BN_new()) && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1))) && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2))) && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3))) && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4))) && TEST_ptr(ctx = BN_CTX_new()) && TEST_true(BN_set_word(p, 0xA)) && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx)) && TEST_true(BN_set_word(p, 0x10)) && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx)) && TEST_true(BN_set_word(p, 0xB)) && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx)) && TEST_true(BN_set_word(p, 0xC)) && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx)) && TEST_true(BN_set_word(p, 0xF)) && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx)) && TEST_false(ossl_rsa_check_prime_factor_range(bn_p1, 72, ctx)) && TEST_false(ossl_rsa_check_prime_factor_range(bn_p2, 72, ctx)) && TEST_true(ossl_rsa_check_prime_factor_range(bn_p3, 72, ctx)) && TEST_true(ossl_rsa_check_prime_factor_range(bn_p4, 72, ctx)); BN_free(bn_p4); BN_free(bn_p3); BN_free(bn_p2); BN_free(bn_p1); BN_free(p); BN_CTX_free(ctx); return ret; } static int test_check_prime_factor(void) { int ret = 0; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *e = NULL; BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL; static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 }; static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 }; static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 }; ret = TEST_ptr(p = BN_new()) && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1))) && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2))) && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3))) && TEST_ptr(e = BN_new()) && TEST_ptr(ctx = BN_CTX_new()) && TEST_true(BN_set_word(e, 0x1)) && TEST_false(ossl_rsa_check_prime_factor(bn_p1, e, 72, ctx)) && TEST_true(ossl_rsa_check_prime_factor(bn_p2, e, 72, ctx)) && TEST_true(BN_set_word(e, 0x2)) && TEST_false(ossl_rsa_check_prime_factor(p, e, 72, ctx)) && TEST_true(BN_set_word(e, 0x1)) && TEST_false(ossl_rsa_check_prime_factor(bn_p3, e, 72, ctx)); BN_free(bn_p3); BN_free(bn_p2); BN_free(bn_p1); BN_free(e); BN_free(p); BN_CTX_free(ctx); return ret; } static int test_check_private_exponent(void) { int ret = 0; RSA *key = NULL; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new()) && TEST_ptr(p = BN_new()) && TEST_ptr(q = BN_new()) && TEST_true(BN_set_word(p, 15)) && TEST_true(BN_set_word(q, 17)) && TEST_true(RSA_set0_factors(key, p, q)); if (!ret) { BN_free(p); BN_free(q); goto end; } ret = TEST_ptr(e = BN_new()) && TEST_ptr(d = BN_new()) && TEST_ptr(n = BN_new()) && TEST_true(BN_set_word(e, 5)) && TEST_true(BN_set_word(d, 157)) && TEST_true(BN_set_word(n, 15*17)) && TEST_true(RSA_set0_key(key, n, e, d)); if (!ret) { BN_free(e); BN_free(d); BN_free(n); goto end; } ret = TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx)) && TEST_true(BN_set_word(d, 45)) && TEST_true(ossl_rsa_check_private_exponent(key, 8, ctx)) && TEST_false(ossl_rsa_check_private_exponent(key, 16, ctx)) && TEST_true(BN_set_word(d, 16)) && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx)) && TEST_true(BN_set_word(d, 46)) && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx)); end: RSA_free(key); BN_CTX_free(ctx); return ret; } static int test_check_crt_components(void) { const int P = 15; const int Q = 17; const int E = 5; const int N = P*Q; const int DP = 3; const int DQ = 13; const int QINV = 8; int ret = 0; RSA *key = NULL; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *q = NULL, *e = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new()) && TEST_ptr(p = BN_new()) && TEST_ptr(q = BN_new()) && TEST_ptr(e = BN_new()) && TEST_true(BN_set_word(p, P)) && TEST_true(BN_set_word(q, Q)) && TEST_true(BN_set_word(e, E)) && TEST_true(RSA_set0_factors(key, p, q)); if (!ret) { BN_free(p); BN_free(q); goto end; } ret = TEST_int_eq(ossl_rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx), 1) && TEST_BN_eq_word(key->n, N) && TEST_BN_eq_word(key->dmp1, DP) && TEST_BN_eq_word(key->dmq1, DQ) && TEST_BN_eq_word(key->iqmp, QINV) && TEST_true(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmp1, 1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmp1, P-1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmp1, DP)) && TEST_true(BN_set_word(key->dmq1, 1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmq1, Q-1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmq1, DQ)) && TEST_true(BN_set_word(key->iqmp, 1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->iqmp, P)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->iqmp, QINV)) && TEST_true(BN_set_word(key->dmp1, DP+1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmp1, DP)) && TEST_true(BN_set_word(key->dmq1, DQ-1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->dmq1, DQ)) && TEST_true(BN_set_word(key->iqmp, QINV+1)) && TEST_false(ossl_rsa_check_crt_components(key, ctx)) && TEST_true(BN_set_word(key->iqmp, QINV)) && TEST_true(ossl_rsa_check_crt_components(key, ctx)); end: BN_free(e); RSA_free(key); BN_CTX_free(ctx); return ret; } static const struct derive_from_pq_test { int p, q, e; } derive_from_pq_tests[] = { { 15, 17, 6 }, { 0, 17, 5 }, }; static int test_derive_params_from_pq_fail(int tst) { int ret = 0; RSA *key = NULL; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *q = NULL, *e = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new()) && TEST_ptr(p = BN_new()) && TEST_ptr(q = BN_new()) && TEST_ptr(e = BN_new()) && TEST_true(BN_set_word(p, derive_from_pq_tests[tst].p)) && TEST_true(BN_set_word(q, derive_from_pq_tests[tst].q)) && TEST_true(BN_set_word(e, derive_from_pq_tests[tst].e)) && TEST_true(RSA_set0_factors(key, p, q)); if (!ret) { BN_free(p); BN_free(q); goto end; } ret = TEST_int_le(ossl_rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx), 0); end: BN_free(e); RSA_free(key); BN_CTX_free(ctx); return ret; } static int test_pq_diff(void) { int ret = 0; BIGNUM *tmp = NULL, *p = NULL, *q = NULL; ret = TEST_ptr(tmp = BN_new()) && TEST_ptr(p = BN_new()) && TEST_ptr(q = BN_new()) && TEST_true(BN_set_word(p, 1)) && TEST_true(BN_set_word(q, 1+2)) && TEST_false(ossl_rsa_check_pminusq_diff(tmp, p, q, 202)) && TEST_true(BN_set_word(q, 1+3)) && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202)) && TEST_true(BN_set_word(p, 1+3)) && TEST_true(BN_set_word(q, 1)) && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202)); BN_free(p); BN_free(q); BN_free(tmp); return ret; } static int test_invalid_keypair(void) { int ret = 0; RSA *key = NULL; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new()) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048)) && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p))) && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q))) && TEST_true(RSA_set0_factors(key, p, q)); if (!ret) { BN_free(p); BN_free(q); goto end; } ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e))) && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n))) && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d))) && TEST_true(RSA_set0_key(key, n, e, d)); if (!ret) { BN_free(e); BN_free(n); BN_free(d); goto end; } ret = TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 100, 2048)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 112, 1024)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 128, 2048)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 140, 3072)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, BN_value_one(), -1, 2048)) && TEST_true(BN_add_word(e, 1)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048)) && TEST_true(BN_sub_word(e, 1)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 3072)) && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, e, 112, 2048)) && TEST_true(BN_add_word(n, 1)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048)) && TEST_true(BN_sub_word(n, 1)) && TEST_true(BN_lshift1(n, n)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2049)) && TEST_true(BN_rshift1(n, n)) && TEST_true(BN_sub_word(p, 2)) && TEST_true(BN_mul(n, p, q, ctx)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048)) && TEST_true(BN_add_word(p, 2)) && TEST_true(BN_mul(n, p, q, ctx)) && TEST_true(BN_sub_word(q, 2)) && TEST_true(BN_mul(n, p, q, ctx)) && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048)) && TEST_true(BN_add_word(q, 2)) && TEST_true(BN_mul(n, p, q, ctx)); end: RSA_free(key); BN_CTX_free(ctx); return ret; } static int keygen_size[] = { 2048, 3072 }; static int test_sp80056b_keygen(int id) { RSA *key = NULL; int ret; int sz = keygen_size[id]; ret = TEST_ptr(key = RSA_new()) && TEST_true(ossl_rsa_sp800_56b_generate_key(key, sz, NULL, NULL)) && TEST_true(ossl_rsa_sp800_56b_check_public(key)) && TEST_true(ossl_rsa_sp800_56b_check_private(key)) && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, sz)); RSA_free(key); return ret; } static int test_check_private_key(void) { int ret = 0; BIGNUM *n = NULL, *d = NULL, *e = NULL; RSA *key = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_false(ossl_rsa_sp800_56b_check_private(key)) && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n))) && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d))) && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e))) && TEST_true(RSA_set0_key(key, n, e, d)); if (!ret) { BN_free(n); BN_free(e); BN_free(d); goto end; } ret = TEST_true(ossl_rsa_sp800_56b_check_private(key)) && TEST_true(BN_set_word(d, 0)) && TEST_false(ossl_rsa_sp800_56b_check_private(key)) && TEST_ptr(BN_copy(d, n)) && TEST_false(ossl_rsa_sp800_56b_check_private(key)); end: RSA_free(key); return ret; } static int test_check_public_key(void) { int ret = 0; BIGNUM *n = NULL, *e = NULL; RSA *key = NULL; ret = TEST_ptr(key = RSA_new()) && TEST_false(ossl_rsa_sp800_56b_check_public(key)) && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e))) && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n))) && TEST_true(RSA_set0_key(key, n, e, NULL)); if (!ret) { BN_free(e); BN_free(n); goto end; } ret = TEST_true(ossl_rsa_sp800_56b_check_public(key)) && TEST_true(BN_add_word(n, 1)) && TEST_false(ossl_rsa_sp800_56b_check_public(key)) && TEST_true(BN_sub_word(n, 1)) && TEST_true(BN_lshift1(n, n)) && TEST_false(ossl_rsa_sp800_56b_check_public(key)) && TEST_true(BN_rshift1(n, n)) && TEST_true(BN_add_word(e, 1)) && TEST_false(ossl_rsa_sp800_56b_check_public(key)) && TEST_true(BN_sub_word(e, 1)) && TEST_true(BN_add_word(n, 2)) && TEST_false(ossl_rsa_sp800_56b_check_public(key)); end: RSA_free(key); return ret; } int setup_tests(void) { ADD_TEST(test_check_public_exponent); ADD_TEST(test_check_prime_factor_range); ADD_TEST(test_check_prime_factor); ADD_TEST(test_check_private_exponent); ADD_TEST(test_check_crt_components); ADD_ALL_TESTS(test_derive_params_from_pq_fail, (int)OSSL_NELEM(derive_from_pq_tests)); ADD_TEST(test_check_private_key); ADD_TEST(test_check_public_key); ADD_TEST(test_invalid_keypair); ADD_TEST(test_pq_diff); ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size)); return 1; }
test
openssl/test/rsa_sp800_56b_test.c
openssl
#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; int bodytype; int err_code; int fail_info; OSSL_CMP_MSG *msg; int expected; 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) 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)) || !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; 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; } 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; }
test
openssl/test/cmp_msg_test.c
openssl
#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; 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) { 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; } if (!TEST_int_eq(EVP_PKEY_verify_init(ctx), 1)) goto end; if (EVP_PKEY_verify(ctx, signature, sigLength, dataToSign, len) != 1) { TEST_error("EVP verify with unpadded length %d failed\n", len); goto end; } if (EVP_PKEY_verify(ctx, signature, sigLength, paddedData, digestlen) != 1) { TEST_error("EVP verify with length %d failed\n", len); goto end; } 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) { 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 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; }
test
openssl/test/dsa_no_digest_size_test.c
openssl
#include <stdlib.h> #include <string.h> #define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/asn1.h> #include <openssl/pem.h> #include <openssl/params.h> #include <openssl/encoder.h> #include <openssl/decoder.h> #include <openssl/dh.h> #include <openssl/dsa.h> #ifndef OPENSSL_NO_DEPRECATED_3_0 # include <openssl/rsa.h> #endif #include "internal/nelem.h" #include "crypto/evp.h" #include "testutil.h" typedef int PEM_write_bio_of_void_protected(BIO *out, const void *obj, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); typedef int PEM_write_bio_of_void_unprotected(BIO *out, const void *obj); typedef void *PEM_read_bio_of_void(BIO *out, void **obj, pem_password_cb *cb, void *u); typedef int EVP_PKEY_print_fn(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx); typedef int EVP_PKEY_eq_fn(const EVP_PKEY *a, const EVP_PKEY *b); static struct test_stanza_st { const char *keytype; const char *structure[2]; int evp_type; i2d_of_void *i2d_PrivateKey; i2d_of_void *i2d_PublicKey; i2d_of_void *i2d_params; i2d_of_void *i2d_PUBKEY; PEM_write_bio_of_void_protected *pem_write_bio_PrivateKey; PEM_write_bio_of_void_unprotected *pem_write_bio_PublicKey; PEM_write_bio_of_void_unprotected *pem_write_bio_params; PEM_write_bio_of_void_unprotected *pem_write_bio_PUBKEY; d2i_of_void *d2i_PrivateKey; d2i_of_void *d2i_PublicKey; d2i_of_void *d2i_params; d2i_of_void *d2i_PUBKEY; PEM_read_bio_of_void *pem_read_bio_PrivateKey; PEM_read_bio_of_void *pem_read_bio_PublicKey; PEM_read_bio_of_void *pem_read_bio_params; PEM_read_bio_of_void *pem_read_bio_PUBKEY; } test_stanzas[] = { #ifndef OPENSSL_NO_DH { "DH", { "DH", "type-specific" }, EVP_PKEY_DH, NULL, NULL, (i2d_of_void *)i2d_DHparams, NULL, NULL, NULL, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHparams, NULL, NULL, NULL, (d2i_of_void *)d2i_DHparams, NULL, NULL, NULL, (PEM_read_bio_of_void *)PEM_read_bio_DHparams, NULL }, { "DHX", { "DHX", "type-specific" }, EVP_PKEY_DHX, NULL, NULL, (i2d_of_void *)i2d_DHxparams, NULL, NULL, NULL, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHxparams, NULL, NULL, NULL, (d2i_of_void *)d2i_DHxparams, NULL, NULL, NULL, NULL, NULL }, #endif #ifndef OPENSSL_NO_DSA { "DSA", { "DSA", "type-specific" }, EVP_PKEY_DSA, (i2d_of_void *)i2d_DSAPrivateKey, (i2d_of_void *)i2d_DSAPublicKey, (i2d_of_void *)i2d_DSAparams, (i2d_of_void *)i2d_DSA_PUBKEY, (PEM_write_bio_of_void_protected *)PEM_write_bio_DSAPrivateKey, NULL, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSAparams, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSA_PUBKEY, (d2i_of_void *)d2i_DSAPrivateKey, (d2i_of_void *)d2i_DSAPublicKey, (d2i_of_void *)d2i_DSAparams, (d2i_of_void *)d2i_DSA_PUBKEY, (PEM_read_bio_of_void *)PEM_read_bio_DSAPrivateKey, NULL, (PEM_read_bio_of_void *)PEM_read_bio_DSAparams, (PEM_read_bio_of_void *)PEM_read_bio_DSA_PUBKEY }, #endif #ifndef OPENSSL_NO_EC { "EC", { "EC", "type-specific" }, EVP_PKEY_EC, (i2d_of_void *)i2d_ECPrivateKey, NULL, (i2d_of_void *)i2d_ECParameters, (i2d_of_void *)i2d_EC_PUBKEY, (PEM_write_bio_of_void_protected *)PEM_write_bio_ECPrivateKey, NULL, NULL, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_EC_PUBKEY, (d2i_of_void *)d2i_ECPrivateKey, NULL, (d2i_of_void *)d2i_ECParameters, (d2i_of_void *)d2i_EC_PUBKEY, (PEM_read_bio_of_void *)PEM_read_bio_ECPrivateKey, NULL, NULL, (PEM_read_bio_of_void *)PEM_read_bio_EC_PUBKEY, }, #endif { "RSA", { "RSA", "type-specific" }, EVP_PKEY_RSA, (i2d_of_void *)i2d_RSAPrivateKey, (i2d_of_void *)i2d_RSAPublicKey, NULL, (i2d_of_void *)i2d_RSA_PUBKEY, (PEM_write_bio_of_void_protected *)PEM_write_bio_RSAPrivateKey, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSAPublicKey, NULL, (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSA_PUBKEY, (d2i_of_void *)d2i_RSAPrivateKey, (d2i_of_void *)d2i_RSAPublicKey, NULL, (d2i_of_void *)d2i_RSA_PUBKEY, (PEM_read_bio_of_void *)PEM_read_bio_RSAPrivateKey, (PEM_read_bio_of_void *)PEM_read_bio_RSAPublicKey, NULL, (PEM_read_bio_of_void *)PEM_read_bio_RSA_PUBKEY } }; #ifndef OPENSSL_NO_DH static const OSSL_PARAM DH_params[] = { OSSL_PARAM_END }; static const OSSL_PARAM DHX_params[] = { OSSL_PARAM_END }; #endif #ifndef OPENSSL_NO_DSA static size_t qbits = 160; static size_t pbits = 1024; static const OSSL_PARAM DSA_params[] = { OSSL_PARAM_size_t("pbits", &pbits), OSSL_PARAM_size_t("qbits", &qbits), OSSL_PARAM_END }; #endif #ifndef OPENSSL_NO_EC static char groupname[] = "prime256v1"; static const OSSL_PARAM EC_params[] = { OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1), OSSL_PARAM_END }; #endif static struct key_st { const char *keytype; int evp_type; const OSSL_PARAM *template_params; EVP_PKEY *key; } keys[] = { #ifndef OPENSSL_NO_DH { "DH", EVP_PKEY_DH, DH_params, NULL }, { "DHX", EVP_PKEY_DHX, DHX_params, NULL }, #endif #ifndef OPENSSL_NO_DSA { "DSA", EVP_PKEY_DSA, DSA_params, NULL }, #endif #ifndef OPENSSL_NO_EC { "EC", EVP_PKEY_EC, EC_params, NULL }, #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 { "RSA", EVP_PKEY_RSA, NULL, NULL }, #endif }; static EVP_PKEY *make_key(const char *type, const OSSL_PARAM *gen_template_params) { EVP_PKEY *template = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM *gen_template_params_noconst = (OSSL_PARAM *)gen_template_params; if (gen_template_params != NULL && ((ctx = EVP_PKEY_CTX_new_from_name(NULL, type, NULL)) == NULL || EVP_PKEY_paramgen_init(ctx) <= 0 || (gen_template_params[0].key != NULL && EVP_PKEY_CTX_set_params(ctx, gen_template_params_noconst) <= 0) || EVP_PKEY_generate(ctx, &template) <= 0)) goto end; EVP_PKEY_CTX_free(ctx); ctx = template != NULL ? EVP_PKEY_CTX_new(template, NULL) : EVP_PKEY_CTX_new_from_name(NULL, type, NULL); (void)(ctx != NULL && EVP_PKEY_keygen_init(ctx) > 0 && EVP_PKEY_keygen(ctx, &pkey) > 0); end: EVP_PKEY_free(template); EVP_PKEY_CTX_free(ctx); return pkey; } static struct key_st *lookup_key(const char *type) { size_t i; for (i = 0; i < OSSL_NELEM(keys); i++) { if (strcmp(keys[i].keytype, type) == 0) return &keys[i]; } return NULL; } static int test_membio_str_eq(BIO *bio_provided, BIO *bio_legacy) { char *str_provided = NULL, *str_legacy = NULL; long len_provided = BIO_get_mem_data(bio_provided, &str_provided); long len_legacy = BIO_get_mem_data(bio_legacy, &str_legacy); return TEST_long_ge(len_legacy, 0) && TEST_long_ge(len_provided, 0) && TEST_strn2_eq(str_provided, len_provided, str_legacy, len_legacy); } static int test_protected_PEM(const char *keytype, int evp_type, const void *legacy_key, PEM_write_bio_of_void_protected *pem_write_bio, PEM_read_bio_of_void *pem_read_bio, EVP_PKEY_eq_fn *evp_pkey_eq, EVP_PKEY_print_fn *evp_pkey_print, EVP_PKEY *provided_pkey, int selection, const char *structure) { int ok = 0; BIO *membio_legacy = NULL; BIO *membio_provided = NULL; OSSL_ENCODER_CTX *ectx = NULL; OSSL_DECODER_CTX *dctx = NULL; void *decoded_legacy_key = NULL; EVP_PKEY *decoded_legacy_pkey = NULL; EVP_PKEY *decoded_provided_pkey = NULL; if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem())) || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem()))) goto end; if (!TEST_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection, "PEM", structure, NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided)) || !TEST_true(pem_write_bio(membio_legacy, legacy_key, NULL, NULL, 0, NULL, NULL)) || !test_membio_str_eq(membio_provided, membio_legacy)) goto end; if (pem_read_bio != NULL) { if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new()) || !TEST_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey, "PEM", structure, keytype, selection, NULL, NULL)) || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided)) || !TEST_ptr(decoded_legacy_key = pem_read_bio(membio_legacy, NULL, NULL, NULL)) || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type, decoded_legacy_key))) goto end; if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey, decoded_legacy_pkey), 0)) { TEST_info("decoded_provided_pkey:"); evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL); TEST_info("decoded_legacy_pkey:"); evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL); } } ok = 1; end: EVP_PKEY_free(decoded_legacy_pkey); EVP_PKEY_free(decoded_provided_pkey); OSSL_ENCODER_CTX_free(ectx); OSSL_DECODER_CTX_free(dctx); BIO_free(membio_provided); BIO_free(membio_legacy); return ok; } static int test_unprotected_PEM(const char *keytype, int evp_type, const void *legacy_key, PEM_write_bio_of_void_unprotected *pem_write_bio, PEM_read_bio_of_void *pem_read_bio, EVP_PKEY_eq_fn *evp_pkey_eq, EVP_PKEY_print_fn *evp_pkey_print, EVP_PKEY *provided_pkey, int selection, const char *structure) { int ok = 0; BIO *membio_legacy = NULL; BIO *membio_provided = NULL; OSSL_ENCODER_CTX *ectx = NULL; OSSL_DECODER_CTX *dctx = NULL; void *decoded_legacy_key = NULL; EVP_PKEY *decoded_legacy_pkey = NULL; EVP_PKEY *decoded_provided_pkey = NULL; if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem())) || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem()))) goto end; if (!TEST_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection, "PEM", structure, NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided)) || !TEST_true(pem_write_bio(membio_legacy, legacy_key)) || !test_membio_str_eq(membio_provided, membio_legacy)) goto end; if (pem_read_bio != NULL) { if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new()) || !TEST_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey, "PEM", structure, keytype, selection, NULL, NULL)) || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided)) || !TEST_ptr(decoded_legacy_key = pem_read_bio(membio_legacy, NULL, NULL, NULL)) || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type, decoded_legacy_key))) goto end; if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey, decoded_legacy_pkey), 0)) { TEST_info("decoded_provided_pkey:"); evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL); TEST_info("decoded_legacy_pkey:"); evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL); } } ok = 1; end: EVP_PKEY_free(decoded_legacy_pkey); EVP_PKEY_free(decoded_provided_pkey); OSSL_ENCODER_CTX_free(ectx); OSSL_DECODER_CTX_free(dctx); BIO_free(membio_provided); BIO_free(membio_legacy); return ok; } static int test_DER(const char *keytype, int evp_type, const void *legacy_key, i2d_of_void *i2d, d2i_of_void *d2i, EVP_PKEY_eq_fn *evp_pkey_eq, EVP_PKEY_print_fn *evp_pkey_print, EVP_PKEY *provided_pkey, int selection, const char *structure) { int ok = 0; unsigned char *der_legacy = NULL; const unsigned char *pder_legacy = NULL; size_t der_legacy_len = 0; unsigned char *der_provided = NULL; const unsigned char *pder_provided = NULL; size_t der_provided_len = 0; size_t tmp_size; OSSL_ENCODER_CTX *ectx = NULL; OSSL_DECODER_CTX *dctx = NULL; void *decoded_legacy_key = NULL; EVP_PKEY *decoded_legacy_pkey = NULL; EVP_PKEY *decoded_provided_pkey = NULL; if (!TEST_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection, "DER", structure, NULL)) || !TEST_true(OSSL_ENCODER_to_data(ectx, &der_provided, &der_provided_len)) || !TEST_size_t_gt(der_legacy_len = i2d(legacy_key, &der_legacy), 0) || !TEST_mem_eq(der_provided, der_provided_len, der_legacy, der_legacy_len)) goto end; if (d2i != NULL) { if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new()) || !TEST_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey, "DER", structure, keytype, selection, NULL, NULL)) || !TEST_true((pder_provided = der_provided, tmp_size = der_provided_len, OSSL_DECODER_from_data(dctx, &pder_provided, &tmp_size))) || !TEST_ptr((pder_legacy = der_legacy, decoded_legacy_key = d2i(NULL, &pder_legacy, (long)der_legacy_len))) || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type, decoded_legacy_key))) goto end; if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey, decoded_legacy_pkey), 0)) { TEST_info("decoded_provided_pkey:"); evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL); TEST_info("decoded_legacy_pkey:"); evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL); } } ok = 1; end: EVP_PKEY_free(decoded_legacy_pkey); EVP_PKEY_free(decoded_provided_pkey); OSSL_ENCODER_CTX_free(ectx); OSSL_DECODER_CTX_free(dctx); OPENSSL_free(der_provided); OPENSSL_free(der_legacy); return ok; } static int test_key(int idx) { struct test_stanza_st *test_stanza = NULL; struct key_st *key = NULL; int ok = 0; size_t i; EVP_PKEY *pkey = NULL, *downgraded_pkey = NULL; const void *legacy_obj = NULL; if (!TEST_ptr(test_stanza = &test_stanzas[idx]) || !TEST_ptr(key = lookup_key(test_stanza->keytype))) goto end; if (!TEST_ptr(pkey = key->key) || !TEST_true(evp_pkey_copy_downgraded(&downgraded_pkey, pkey)) || !TEST_ptr(downgraded_pkey) || !TEST_int_eq(EVP_PKEY_get_id(downgraded_pkey), key->evp_type) || !TEST_ptr(legacy_obj = EVP_PKEY_get0(downgraded_pkey))) goto end; ok = 1; if (test_stanza->pem_write_bio_PrivateKey != NULL) { int selection = OSSL_KEYMGMT_SELECT_ALL; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PrivateKey for %s, %s", test_stanza->keytype, structure); if (!test_protected_PEM(key->keytype, key->evp_type, legacy_obj, test_stanza->pem_write_bio_PrivateKey, test_stanza->pem_read_bio_PrivateKey, EVP_PKEY_eq, EVP_PKEY_print_private, pkey, selection, structure)) ok = 0; } } if (test_stanza->pem_write_bio_PublicKey != NULL) { int selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PublicKey for %s, %s", test_stanza->keytype, structure); if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj, test_stanza->pem_write_bio_PublicKey, test_stanza->pem_read_bio_PublicKey, EVP_PKEY_eq, EVP_PKEY_print_public, pkey, selection, structure)) ok = 0; } } if (test_stanza->pem_write_bio_params != NULL) { int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}params for %s, %s", test_stanza->keytype, structure); if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj, test_stanza->pem_write_bio_params, test_stanza->pem_read_bio_params, EVP_PKEY_parameters_eq, EVP_PKEY_print_params, pkey, selection, structure)) ok = 0; } } if (test_stanza->pem_write_bio_PUBKEY != NULL) { int selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; const char *structure = "SubjectPublicKeyInfo"; TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}_PUBKEY for %s, %s", test_stanza->keytype, structure); if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj, test_stanza->pem_write_bio_PUBKEY, test_stanza->pem_read_bio_PUBKEY, EVP_PKEY_eq, EVP_PKEY_print_public, pkey, selection, structure)) ok = 0; } if (test_stanza->i2d_PrivateKey != NULL) { int selection = OSSL_KEYMGMT_SELECT_ALL; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PrivateKey for %s, %s", test_stanza->keytype, structure); if (!test_DER(key->keytype, key->evp_type, legacy_obj, test_stanza->i2d_PrivateKey, test_stanza->d2i_PrivateKey, EVP_PKEY_eq, EVP_PKEY_print_private, pkey, selection, structure)) ok = 0; } } if (test_stanza->i2d_PublicKey != NULL) { int selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PublicKey for %s, %s", test_stanza->keytype, structure); if (!test_DER(key->keytype, key->evp_type, legacy_obj, test_stanza->i2d_PublicKey, test_stanza->d2i_PublicKey, EVP_PKEY_eq, EVP_PKEY_print_public, pkey, selection, structure)) ok = 0; } } if (test_stanza->i2d_params != NULL) { int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) { const char *structure = test_stanza->structure[i]; TEST_info("Test OSSL_ENCODER against i2d_{TYPE}params for %s, %s", test_stanza->keytype, structure); if (!test_DER(key->keytype, key->evp_type, legacy_obj, test_stanza->i2d_params, test_stanza->d2i_params, EVP_PKEY_parameters_eq, EVP_PKEY_print_params, pkey, selection, structure)) ok = 0; } } if (test_stanza->i2d_PUBKEY != NULL) { int selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS; const char *structure = "SubjectPublicKeyInfo"; TEST_info("Test OSSL_ENCODER against i2d_{TYPE}_PUBKEY for %s, %s", test_stanza->keytype, structure); if (!test_DER(key->keytype, key->evp_type, legacy_obj, test_stanza->i2d_PUBKEY, test_stanza->d2i_PUBKEY, EVP_PKEY_eq, EVP_PKEY_print_public, pkey, selection, structure)) ok = 0; } end: EVP_PKEY_free(downgraded_pkey); return ok; } #define USAGE "rsa-key.pem dh-key.pem\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { size_t i; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (test_get_argument_count() != 2) { TEST_error("usage: endecoder_legacy_test %s", USAGE); return 0; } TEST_info("Generating keys..."); for (i = 0; i < OSSL_NELEM(keys); i++) { #ifndef OPENSSL_NO_DH if (strcmp(keys[i].keytype, "DH") == 0) { if (!TEST_ptr(keys[i].key = load_pkey_pem(test_get_argument(1), NULL))) return 0; continue; } #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 if (strcmp(keys[i].keytype, "RSA") == 0) { if (!TEST_ptr(keys[i].key = load_pkey_pem(test_get_argument(0), NULL))) return 0; continue; } #endif TEST_info("Generating %s key...", keys[i].keytype); if (!TEST_ptr(keys[i].key = make_key(keys[i].keytype, keys[i].template_params))) return 0; } TEST_info("Generating keys done"); ADD_ALL_TESTS(test_key, OSSL_NELEM(test_stanzas)); return 1; } void cleanup_tests(void) { size_t i; for (i = 0; i < OSSL_NELEM(keys); i++) EVP_PKEY_free(keys[i].key); }
test
openssl/test/endecoder_legacy_test.c
openssl
#include <string.h> #include <openssl/bio.h> #include <openssl/pem.h> #include "testutil.h" #include "internal/nelem.h" typedef struct { const char *raw; const char *encoded; } TESTDATA; static TESTDATA b64_pem_data[] = { { "hello world", "aGVsbG8gd29ybGQ=" }, { "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input", "YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" } }; static const char *pemtype = "PEMTESTDATA"; static char *pemfile; static int test_b64(int idx) { BIO *b = BIO_new(BIO_s_mem()); char *name = NULL, *header = NULL; unsigned char *data = NULL; long len; int ret = 0; const char *raw = b64_pem_data[idx].raw; const char *encoded = b64_pem_data[idx].encoded; if (!TEST_ptr(b) || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype)) || !TEST_true(BIO_printf(b, "%s\n", encoded)) || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype)) || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len, PEM_FLAG_ONLY_B64))) goto err; if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0) || !TEST_int_eq(len, strlen(raw)) || !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0)) goto err; ret = 1; err: BIO_free(b); OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); return ret; } static int test_invalid(void) { BIO *b = BIO_new(BIO_s_mem()); char *name = NULL, *header = NULL; unsigned char *data = NULL; long len; const char *encoded = b64_pem_data[0].encoded; if (!TEST_ptr(b) || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype)) || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded)) || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype)) || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len, PEM_FLAG_ONLY_B64))) { BIO_free(b); return 0; } BIO_free(b); OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); return 1; } static int test_cert_key_cert(void) { EVP_PKEY *key; if (!TEST_ptr(key = load_pkey_pem(pemfile, NULL))) return 0; EVP_PKEY_free(key); return 1; } static int test_empty_payload(void) { BIO *b; static char *emptypay = "-----BEGIN CERTIFICATE-----\n" "-\n" "-----END CERTIFICATE-----"; char *name = NULL, *header = NULL; unsigned char *data = NULL; long len; int ret = 0; b = BIO_new_mem_buf(emptypay, strlen(emptypay)); if (!TEST_ptr(b)) return 0; if (!TEST_false(PEM_read_bio_ex(b, &name, &header, &data, &len, 0))) goto err; ret = 1; err: OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); BIO_free(b); return ret; } static int test_protected_params(void) { BIO *b; static char *protectedpay = "-----BEGIN RSA PRIVATE KEY-----\n" "Proc-Type: 4,ENCRYPTED\n" "DEK-Info: AES-256-CBC,4A44448ED28992710556549B35100CEA\n" "\n" "Xw3INxKeH+rUUF57mjATpvj6zknVhedwrlRmRvnwlLv5wqIy5Ae4UVLPh7SUswfC\n" "-----END RSA PRIVATE KEY-----\n"; EVP_PKEY *pkey = NULL; int ret = 0; b = BIO_new_mem_buf(protectedpay, strlen(protectedpay)); if (!TEST_ptr(b)) return 0; pkey = PEM_read_bio_Parameters(b, NULL); if (!TEST_ptr_null(pkey)) goto err; ret = 1; err: EVP_PKEY_free(pkey); BIO_free(b); return ret; } int setup_tests(void) { if (!TEST_ptr(pemfile = test_get_argument(0))) return 0; ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data)); ADD_TEST(test_invalid); ADD_TEST(test_cert_key_cert); ADD_TEST(test_empty_payload); ADD_TEST(test_protected_params); return 1; }
test
openssl/test/pemtest.c
openssl
#ifdef _WIN32 # include <windows.h> #endif #include <stdio.h> #include <string.h> #include <openssl/async.h> #include <openssl/crypto.h> static int ctr = 0; static ASYNC_JOB *currjob = NULL; static int custom_alloc_used = 0; static int custom_free_used = 0; static int only_pause(void *args) { ASYNC_pause_job(); return 1; } static int add_two(void *args) { ctr++; ASYNC_pause_job(); ctr++; return 2; } static int save_current(void *args) { currjob = ASYNC_get_current_job(); ASYNC_pause_job(); return 1; } static int change_deflt_libctx(void *args) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); OSSL_LIB_CTX *oldctx, *tmpctx; int ret = 0; if (libctx == NULL) return 0; oldctx = OSSL_LIB_CTX_set0_default(libctx); ASYNC_pause_job(); tmpctx = OSSL_LIB_CTX_set0_default(oldctx); if (tmpctx != libctx) goto err; oldctx = OSSL_LIB_CTX_set0_default(libctx); ASYNC_pause_job(); tmpctx = OSSL_LIB_CTX_set0_default(oldctx); if (tmpctx != libctx) goto err; ret = 1; err: OSSL_LIB_CTX_free(libctx); return ret; } #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99) static int waitfd(void *args) { ASYNC_JOB *job; ASYNC_WAIT_CTX *waitctx; job = ASYNC_get_current_job(); if (job == NULL) return 0; waitctx = ASYNC_get_wait_ctx(job); if (waitctx == NULL) return 0; ASYNC_pause_job(); if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL)) return 0; ASYNC_pause_job(); if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx)) return 0; ASYNC_pause_job(); if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL)) return 0; if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx)) return 0; return 1; } static int blockpause(void *args) { ASYNC_block_pause(); ASYNC_pause_job(); ASYNC_unblock_pause(); ASYNC_pause_job(); return 1; } static int test_ASYNC_init_thread(void) { ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL; int funcret1, funcret2, funcret3; ASYNC_WAIT_CTX *waitctx = NULL; if ( !ASYNC_init_thread(2, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0) != ASYNC_PAUSE || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0) != ASYNC_PAUSE || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) != ASYNC_NO_JOBS || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0) != ASYNC_FINISH || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) != ASYNC_PAUSE || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0) != ASYNC_FINISH || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) != ASYNC_FINISH || funcret1 != 1 || funcret2 != 1 || funcret3 != 1) { fprintf(stderr, "test_ASYNC_init_thread() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_callback(void *arg) { printf("callback test pass\n"); return 1; } static int test_ASYNC_callback_status(void) { ASYNC_WAIT_CTX *waitctx = NULL; int set_arg = 100; ASYNC_callback_fn get_callback; void *get_arg; int set_status = 1; if ( !ASYNC_init_thread(1, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg) != 1 || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg) != 1 || test_callback != get_callback || get_arg != (void*)&set_arg || (*get_callback)(get_arg) != 1 || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1 || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) { fprintf(stderr, "test_ASYNC_callback_status() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_ASYNC_start_job(void) { ASYNC_JOB *job = NULL; int funcret; ASYNC_WAIT_CTX *waitctx = NULL; ctr = 0; if ( !ASYNC_init_thread(1, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0) != ASYNC_PAUSE || ctr != 1 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0) != ASYNC_FINISH || ctr != 2 || funcret != 2) { fprintf(stderr, "test_ASYNC_start_job() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_ASYNC_get_current_job(void) { ASYNC_JOB *job = NULL; int funcret; ASYNC_WAIT_CTX *waitctx = NULL; currjob = NULL; if ( !ASYNC_init_thread(1, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0) != ASYNC_PAUSE || currjob != job || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0) != ASYNC_FINISH || funcret != 1) { fprintf(stderr, "test_ASYNC_get_current_job() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_ASYNC_WAIT_CTX_get_all_fds(void) { ASYNC_JOB *job = NULL; int funcret; ASYNC_WAIT_CTX *waitctx = NULL; OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD; size_t numfds, numdelfds; if ( !ASYNC_init_thread(1, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) != ASYNC_PAUSE || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) || numfds != 0 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, &numdelfds) || numfds != 0 || numdelfds != 0 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) != ASYNC_PAUSE || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) || numfds != 1 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds) || fd != MAGIC_WAIT_FD || (fd = OSSL_BAD_ASYNC_FD, 0) || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, &numdelfds) || numfds != 1 || numdelfds != 0 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL, &numdelfds) || fd != MAGIC_WAIT_FD || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) != ASYNC_PAUSE || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) || numfds != 0 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, &numdelfds) || numfds != 0 || numdelfds != 1 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd, &numdelfds) || delfd != MAGIC_WAIT_FD || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) != ASYNC_FINISH || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) || numfds != 0 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, &numdelfds) || numfds != 0 || numdelfds != 0 || funcret != 1) { fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_ASYNC_block_pause(void) { ASYNC_JOB *job = NULL; int funcret; ASYNC_WAIT_CTX *waitctx = NULL; if ( !ASYNC_init_thread(1, 0) || (waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0) != ASYNC_PAUSE || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0) != ASYNC_FINISH || funcret != 1) { fprintf(stderr, "test_ASYNC_block_pause() failed\n"); ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 0; } ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); return 1; } static int test_ASYNC_start_job_ex(void) { ASYNC_JOB *job = NULL; int funcret; ASYNC_WAIT_CTX *waitctx = NULL; OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); OSSL_LIB_CTX *oldctx, *tmpctx, *globalctx; int ret = 0; if (libctx == NULL) { fprintf(stderr, "test_ASYNC_start_job_ex() failed to create libctx\n"); goto err; } globalctx = oldctx = OSSL_LIB_CTX_set0_default(libctx); if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0) != ASYNC_PAUSE) { fprintf(stderr, "test_ASYNC_start_job_ex() failed to start job\n"); goto err; } tmpctx = OSSL_LIB_CTX_set0_default(oldctx); oldctx = OSSL_LIB_CTX_set0_default(tmpctx); if (tmpctx != libctx) { fprintf(stderr, "test_ASYNC_start_job_ex() failed - unexpected libctx\n"); goto err; } if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0) != ASYNC_PAUSE) { fprintf(stderr, "test_ASYNC_start_job_ex() - restarting job failed\n"); goto err; } tmpctx = OSSL_LIB_CTX_set0_default(oldctx); if (tmpctx != libctx) { fprintf(stderr, "test_ASYNC_start_job_ex() failed - unexpected libctx\n"); goto err; } if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0) != ASYNC_FINISH || funcret != 1) { fprintf(stderr, "test_ASYNC_start_job_ex() - finishing job failed\n"); goto err; } tmpctx = OSSL_LIB_CTX_set0_default(libctx); OSSL_LIB_CTX_set0_default(tmpctx); if (tmpctx != globalctx) { fprintf(stderr, "test_ASYNC_start_job_ex() failed - global libctx check failed\n"); goto err; } ret = 1; err: ASYNC_WAIT_CTX_free(waitctx); ASYNC_cleanup_thread(); OSSL_LIB_CTX_free(libctx); return ret; } static void *test_alloc_stack(size_t *num) { custom_alloc_used = 1; return OPENSSL_malloc(*num); } static void test_free_stack(void *addr) { custom_free_used = 1; OPENSSL_free(addr); } static int test_ASYNC_set_mem_functions(void) { ASYNC_stack_alloc_fn alloc_fn; ASYNC_stack_free_fn free_fn; if (ASYNC_set_mem_functions(test_alloc_stack, test_free_stack) == 0) return 1; ASYNC_get_mem_functions(&alloc_fn, &free_fn); if ((alloc_fn != test_alloc_stack) || (free_fn != test_free_stack)) { fprintf(stderr, "test_ASYNC_set_mem_functions() - setting and retrieving custom allocators failed\n"); return 0; } if (!ASYNC_init_thread(1, 1)) { fprintf(stderr, "test_ASYNC_set_mem_functions() - failed initialising ctx pool\n"); return 0; } ASYNC_cleanup_thread(); if (!custom_alloc_used || !custom_free_used) { fprintf(stderr, "test_ASYNC_set_mem_functions() - custom allocation functions not used\n"); return 0; } return 1; } int main(int argc, char **argv) { if (!ASYNC_is_capable()) { fprintf(stderr, "OpenSSL build is not ASYNC capable - skipping async tests\n"); } else { if (!test_ASYNC_init_thread() || !test_ASYNC_callback_status() || !test_ASYNC_start_job() || !test_ASYNC_get_current_job() || !test_ASYNC_WAIT_CTX_get_all_fds() || !test_ASYNC_block_pause() || !test_ASYNC_start_job_ex() || !test_ASYNC_set_mem_functions()) { return 1; } } printf("PASS\n"); return 0; }
test
openssl/test/asynctest.c
openssl
#include <openssl/evp.h> #include "testutil.h" static int test_pbelu(void) { int i, failed = 0; int pbe_type, pbe_nid, last_type = -1, last_nid = -1; for (i = 0; EVP_PBE_get(&pbe_type, &pbe_nid, i) != 0; i++) { if (!TEST_true(EVP_PBE_find(pbe_type, pbe_nid, NULL, NULL, 0))) { TEST_note("i=%d, pbe_type=%d, pbe_nid=%d", i, pbe_type, pbe_nid); failed = 1; break; } } if (!failed) return 1; for (i = 0; EVP_PBE_get(&pbe_type, &pbe_nid, i) != 0; i++) { failed = pbe_type < last_type || (pbe_type == last_type && pbe_nid < last_nid); TEST_note("PBE type=%d %d (%s): %s\n", pbe_type, pbe_nid, OBJ_nid2sn(pbe_nid), failed ? "ERROR" : "OK"); last_type = pbe_type; last_nid = pbe_nid; } return 0; } int setup_tests(void) { ADD_TEST(test_pbelu); return 1; }
test
openssl/test/pbelutest.c
openssl
#include "internal/quic_cc.h" #include "internal/quic_types.h" typedef struct ossl_cc_dummy_st { size_t max_dgram_len; size_t *p_diag_max_dgram_len; } OSSL_CC_DUMMY; static void dummy_update_diag(OSSL_CC_DUMMY *d); static OSSL_CC_DATA *dummy_new(OSSL_TIME (*now_cb)(void *arg), void *now_cb_arg) { OSSL_CC_DUMMY *d = OPENSSL_zalloc(sizeof(*d)); if (d == NULL) return NULL; d->max_dgram_len = QUIC_MIN_INITIAL_DGRAM_LEN; return (OSSL_CC_DATA *)d; } static void dummy_free(OSSL_CC_DATA *cc) { OPENSSL_free(cc); } static void dummy_reset(OSSL_CC_DATA *cc) { } static int dummy_set_input_params(OSSL_CC_DATA *cc, const OSSL_PARAM *params) { OSSL_CC_DUMMY *d = (OSSL_CC_DUMMY *)cc; const OSSL_PARAM *p; size_t value; p = OSSL_PARAM_locate_const(params, OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN); if (p != NULL) { if (!OSSL_PARAM_get_size_t(p, &value)) return 0; if (value < QUIC_MIN_INITIAL_DGRAM_LEN) return 0; d->max_dgram_len = value; dummy_update_diag(d); } return 1; } static int dummy_bind_diagnostic(OSSL_CC_DATA *cc, OSSL_PARAM *params) { OSSL_CC_DUMMY *d = (OSSL_CC_DUMMY *)cc; const OSSL_PARAM *p; p = OSSL_PARAM_locate_const(params, OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN); if (p != NULL) { if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER || p->data_size != sizeof(size_t)) return 0; d->p_diag_max_dgram_len = p->data; } dummy_update_diag(d); return 1; } static int dummy_unbind_diagnostic(OSSL_CC_DATA *cc, OSSL_PARAM *params) { OSSL_CC_DUMMY *d = (OSSL_CC_DUMMY *)cc; if (OSSL_PARAM_locate_const(params, OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN) != NULL) d->p_diag_max_dgram_len = NULL; return 1; } static void dummy_update_diag(OSSL_CC_DUMMY *d) { if (d->p_diag_max_dgram_len != NULL) *d->p_diag_max_dgram_len = d->max_dgram_len; } static uint64_t dummy_get_tx_allowance(OSSL_CC_DATA *cc) { return SIZE_MAX; } static OSSL_TIME dummy_get_wakeup_deadline(OSSL_CC_DATA *cc) { return ossl_time_infinite(); } static int dummy_on_data_sent(OSSL_CC_DATA *cc, uint64_t num_bytes) { return 1; } static int dummy_on_data_acked(OSSL_CC_DATA *cc, const OSSL_CC_ACK_INFO *info) { return 1; } static int dummy_on_data_lost(OSSL_CC_DATA *cc, const OSSL_CC_LOSS_INFO *info) { return 1; } static int dummy_on_data_lost_finished(OSSL_CC_DATA *cc, uint32_t flags) { return 1; } static int dummy_on_data_invalidated(OSSL_CC_DATA *cc, uint64_t num_bytes) { return 1; } const OSSL_CC_METHOD ossl_cc_dummy_method = { dummy_new, dummy_free, dummy_reset, dummy_set_input_params, dummy_bind_diagnostic, dummy_unbind_diagnostic, dummy_get_tx_allowance, dummy_get_wakeup_deadline, dummy_on_data_sent, dummy_on_data_acked, dummy_on_data_lost, dummy_on_data_lost_finished, dummy_on_data_invalidated, };
test
openssl/test/cc_dummy.c
openssl
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/pkcs12.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include "testutil.h" #include "helpers/pkcs12.h" static int default_libctx = 1; static OSSL_LIB_CTX *testctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *deflprov = NULL; static OSSL_PROVIDER *lgcyprov = NULL; static const unsigned char CERT1[] = { 0x30, 0x82, 0x01, 0xed, 0x30, 0x82, 0x01, 0x56, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, 0x8b, 0x4b, 0x5e, 0x6c, 0x03, 0x28, 0x4e, 0xe6, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x19, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0e, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x39, 0x33, 0x30, 0x30, 0x30, 0x34, 0x36, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x39, 0x30, 0x39, 0x32, 0x37, 0x30, 0x30, 0x34, 0x36, 0x35, 0x36, 0x5a, 0x30, 0x1b, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x10, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2d, 0x31, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xbc, 0xdc, 0x6f, 0x8c, 0x7a, 0x2a, 0x4b, 0xea, 0x66, 0x66, 0x04, 0xa9, 0x05, 0x92, 0x53, 0xd7, 0x13, 0x3c, 0x49, 0xe1, 0xc8, 0xbb, 0xdf, 0x3d, 0xcb, 0x88, 0x31, 0x07, 0x20, 0x59, 0x93, 0x24, 0x7f, 0x7d, 0xc6, 0x84, 0x81, 0x16, 0x64, 0x4a, 0x52, 0xa6, 0x30, 0x44, 0xdc, 0x1a, 0x30, 0xde, 0xae, 0x29, 0x18, 0xcf, 0xc7, 0xf3, 0xcf, 0x0c, 0xb7, 0x8e, 0x2b, 0x1e, 0x21, 0x01, 0x0b, 0xfb, 0xe5, 0xe6, 0xcf, 0x2b, 0x84, 0xe1, 0x33, 0xf8, 0xba, 0x02, 0xfc, 0x30, 0xfa, 0xc4, 0x33, 0xc7, 0x37, 0xc6, 0x7f, 0x72, 0x31, 0x92, 0x1d, 0x8f, 0xa0, 0xfb, 0xe5, 0x4a, 0x08, 0x31, 0x78, 0x80, 0x9c, 0x23, 0xb4, 0xe9, 0x19, 0x56, 0x04, 0xfa, 0x0d, 0x07, 0x04, 0xb7, 0x43, 0xac, 0x4c, 0x49, 0x7c, 0xc2, 0xa1, 0x44, 0xc1, 0x48, 0x7d, 0x28, 0xe5, 0x23, 0x66, 0x07, 0x22, 0xd5, 0xf0, 0xf1, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xdb, 0xbb, 0xb8, 0x92, 0x4e, 0x24, 0x0b, 0x1b, 0xbb, 0x78, 0x33, 0xf9, 0x01, 0x02, 0x23, 0x0d, 0x96, 0x18, 0x30, 0x47, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x1c, 0x13, 0xdc, 0x02, 0xf1, 0x44, 0x36, 0x65, 0xa9, 0xbe, 0x30, 0x1c, 0x66, 0x14, 0x20, 0x86, 0x5a, 0xa8, 0x69, 0x25, 0xf8, 0x1a, 0xb6, 0x9e, 0x5e, 0xe9, 0x89, 0xb8, 0x67, 0x70, 0x19, 0x87, 0x60, 0xeb, 0x4b, 0x11, 0x71, 0x85, 0xf8, 0xe9, 0xa7, 0x3e, 0x20, 0x42, 0xec, 0x43, 0x25, 0x01, 0x03, 0xe5, 0x4d, 0x83, 0x22, 0xf5, 0x8e, 0x3a, 0x1a, 0x1b, 0xd4, 0x1c, 0xda, 0x6b, 0x9d, 0x10, 0x1b, 0xee, 0x67, 0x4e, 0x1f, 0x69, 0xab, 0xbc, 0xaa, 0x62, 0x8e, 0x9e, 0xc6, 0xee, 0xd6, 0x09, 0xc0, 0xca, 0xe0, 0xaa, 0x9f, 0x07, 0xb2, 0xc2, 0xbb, 0x31, 0x96, 0xa2, 0x04, 0x62, 0xd3, 0x13, 0x32, 0x29, 0x67, 0x6e, 0xad, 0x2e, 0x0b, 0xea, 0x04, 0x7c, 0x8c, 0x5a, 0x5d, 0xac, 0x14, 0xaa, 0x61, 0x7f, 0x28, 0x6c, 0x2d, 0x64, 0x2d, 0xc3, 0xaf, 0x77, 0x52, 0x90, 0xb4, 0x37, 0xc0, 0x30, }; static const unsigned char CERT2[] = { 0x30, 0x82, 0x01, 0xed, 0x30, 0x82, 0x01, 0x56, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, 0x8b, 0x4b, 0x5e, 0x6c, 0x03, 0x28, 0x4e, 0xe7, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x19, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0e, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x39, 0x33, 0x30, 0x30, 0x30, 0x34, 0x36, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x39, 0x30, 0x39, 0x32, 0x37, 0x30, 0x30, 0x34, 0x36, 0x35, 0x36, 0x5a, 0x30, 0x1b, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x10, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2d, 0x31, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xa8, 0x6e, 0x40, 0x86, 0x9f, 0x98, 0x59, 0xfb, 0x57, 0xbf, 0xc1, 0x55, 0x12, 0x38, 0xeb, 0xb3, 0x46, 0x34, 0xc9, 0x35, 0x4d, 0xfd, 0x03, 0xe9, 0x3a, 0x88, 0x9e, 0x97, 0x8f, 0xf4, 0xec, 0x36, 0x7b, 0x3f, 0xba, 0xb8, 0xa5, 0x96, 0x30, 0x03, 0xc5, 0xc6, 0xd9, 0xa8, 0x4e, 0xbc, 0x23, 0x51, 0xa1, 0x96, 0xd2, 0x03, 0x98, 0x73, 0xb6, 0x17, 0x9c, 0x77, 0xd4, 0x95, 0x1e, 0x1b, 0xb3, 0x1b, 0xc8, 0x71, 0xd1, 0x2e, 0x31, 0xc7, 0x6a, 0x75, 0x57, 0x08, 0x7f, 0xba, 0x70, 0x76, 0xf7, 0x67, 0xf4, 0x4e, 0xbe, 0xfc, 0x70, 0x61, 0x41, 0x07, 0x2b, 0x7c, 0x3c, 0x3b, 0xb3, 0xbc, 0xd5, 0xa8, 0xbd, 0x28, 0xd8, 0x49, 0xd3, 0xe1, 0x78, 0xc8, 0xc1, 0x42, 0x5e, 0x18, 0x36, 0xa8, 0x41, 0xf7, 0xc8, 0xaa, 0x35, 0xfe, 0x2d, 0xd1, 0xb4, 0xcc, 0x00, 0x67, 0xae, 0x79, 0xd3, 0x28, 0xd5, 0x5b, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xdb, 0xbb, 0xb8, 0x92, 0x4e, 0x24, 0x0b, 0x1b, 0xbb, 0x78, 0x33, 0xf9, 0x01, 0x02, 0x23, 0x0d, 0x96, 0x18, 0x30, 0x47, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x3b, 0xa6, 0x73, 0xbe, 0xe0, 0x28, 0xed, 0x1f, 0x29, 0x78, 0x4c, 0xc0, 0x1f, 0xe9, 0x85, 0xc6, 0x8f, 0xe3, 0x87, 0x7c, 0xd9, 0xe7, 0x0a, 0x37, 0xe8, 0xaa, 0xb5, 0xd2, 0x7f, 0xf8, 0x90, 0x20, 0x80, 0x35, 0xa7, 0x79, 0x2b, 0x04, 0xa7, 0xbf, 0xe6, 0x7b, 0x58, 0xcb, 0xec, 0x0e, 0x58, 0xef, 0x2a, 0x70, 0x8a, 0x56, 0x8a, 0xcf, 0x6b, 0x7a, 0x74, 0x0c, 0xf4, 0x15, 0x37, 0x93, 0xcd, 0xe6, 0xb2, 0xa1, 0x83, 0x09, 0xdb, 0x9e, 0x4f, 0xff, 0x6a, 0x17, 0x4f, 0x33, 0xc9, 0xcc, 0x90, 0x2a, 0x67, 0xff, 0x16, 0x78, 0xa8, 0x2c, 0x10, 0xe0, 0x52, 0x8c, 0xe6, 0xe9, 0x90, 0x8d, 0xe0, 0x62, 0x04, 0x9a, 0x0f, 0x44, 0x01, 0x82, 0x14, 0x92, 0x44, 0x25, 0x69, 0x22, 0xb7, 0xb8, 0xc5, 0x94, 0x4c, 0x4b, 0x1c, 0x9b, 0x92, 0x60, 0x66, 0x90, 0x4e, 0xb9, 0xa8, 0x4c, 0x89, 0xbb, 0x0f, 0x0b, }; static const unsigned char KEY1[] = { 0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbc, 0xdc, 0x6f, 0x8c, 0x7a, 0x2a, 0x4b, 0xea, 0x66, 0x66, 0x04, 0xa9, 0x05, 0x92, 0x53, 0xd7, 0x13, 0x3c, 0x49, 0xe1, 0xc8, 0xbb, 0xdf, 0x3d, 0xcb, 0x88, 0x31, 0x07, 0x20, 0x59, 0x93, 0x24, 0x7f, 0x7d, 0xc6, 0x84, 0x81, 0x16, 0x64, 0x4a, 0x52, 0xa6, 0x30, 0x44, 0xdc, 0x1a, 0x30, 0xde, 0xae, 0x29, 0x18, 0xcf, 0xc7, 0xf3, 0xcf, 0x0c, 0xb7, 0x8e, 0x2b, 0x1e, 0x21, 0x01, 0x0b, 0xfb, 0xe5, 0xe6, 0xcf, 0x2b, 0x84, 0xe1, 0x33, 0xf8, 0xba, 0x02, 0xfc, 0x30, 0xfa, 0xc4, 0x33, 0xc7, 0x37, 0xc6, 0x7f, 0x72, 0x31, 0x92, 0x1d, 0x8f, 0xa0, 0xfb, 0xe5, 0x4a, 0x08, 0x31, 0x78, 0x80, 0x9c, 0x23, 0xb4, 0xe9, 0x19, 0x56, 0x04, 0xfa, 0x0d, 0x07, 0x04, 0xb7, 0x43, 0xac, 0x4c, 0x49, 0x7c, 0xc2, 0xa1, 0x44, 0xc1, 0x48, 0x7d, 0x28, 0xe5, 0x23, 0x66, 0x07, 0x22, 0xd5, 0xf0, 0xf1, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81, 0x00, 0xa5, 0x6d, 0xf9, 0x8f, 0xf5, 0x5a, 0xa3, 0x50, 0xd9, 0x0d, 0x37, 0xbb, 0xce, 0x13, 0x94, 0xb8, 0xea, 0x32, 0x7f, 0x0c, 0xf5, 0x46, 0x0b, 0x90, 0x17, 0x7e, 0x5e, 0x63, 0xbd, 0xa4, 0x78, 0xcd, 0x19, 0x97, 0xd4, 0x92, 0x30, 0x78, 0xaa, 0xb4, 0xa7, 0x9c, 0xc6, 0xdf, 0x2a, 0x65, 0x0e, 0xb5, 0x9f, 0x9c, 0x84, 0x0d, 0x4d, 0x3a, 0x74, 0xfc, 0xd0, 0xb4, 0x09, 0x74, 0xc4, 0xb8, 0x24, 0x03, 0xa8, 0xf0, 0xf8, 0x0d, 0x5c, 0x8e, 0xdf, 0x4b, 0xe1, 0x0a, 0x8f, 0x4f, 0xd5, 0xc7, 0x9b, 0x54, 0x55, 0x8f, 0x00, 0x5c, 0xea, 0x4c, 0x73, 0xf9, 0x1b, 0xbf, 0xb8, 0x93, 0x33, 0x20, 0xce, 0x45, 0xd9, 0x03, 0x02, 0xb2, 0x36, 0xc5, 0x0a, 0x30, 0x50, 0x78, 0x80, 0x66, 0x00, 0x22, 0x38, 0x86, 0xcf, 0x63, 0x4a, 0x5c, 0xbf, 0x2b, 0xd9, 0x6e, 0xe6, 0xf0, 0x39, 0xad, 0x12, 0x25, 0x41, 0xb9, 0x02, 0x41, 0x00, 0xf3, 0x7c, 0x07, 0x99, 0x64, 0x3a, 0x28, 0x8c, 0x8d, 0x05, 0xfe, 0x32, 0xb5, 0x4c, 0x8c, 0x6d, 0xde, 0x3d, 0x16, 0x08, 0xa0, 0x01, 0x61, 0x4f, 0x8e, 0xa0, 0xf7, 0x26, 0x26, 0xb5, 0x8e, 0xc0, 0x7a, 0xce, 0x86, 0x34, 0xde, 0xb8, 0xef, 0x86, 0x01, 0xbe, 0x24, 0xaa, 0x9b, 0x36, 0x93, 0x72, 0x9b, 0xf9, 0xc6, 0xcb, 0x76, 0x84, 0x67, 0x06, 0x06, 0x30, 0x50, 0xdf, 0x42, 0x17, 0xe0, 0xa7, 0x02, 0x41, 0x00, 0xc6, 0x91, 0xa0, 0x41, 0x34, 0x11, 0x67, 0x4b, 0x08, 0x0f, 0xda, 0xa7, 0x99, 0xec, 0x58, 0x11, 0xa5, 0x82, 0xdb, 0x50, 0xfe, 0x77, 0xe2, 0xd1, 0x53, 0x9c, 0x7d, 0xe8, 0xbf, 0xe7, 0x7c, 0xa9, 0x01, 0xb1, 0x87, 0xc3, 0x52, 0x79, 0x9e, 0x2c, 0xa7, 0x6f, 0x02, 0x37, 0x32, 0xef, 0x24, 0x31, 0x21, 0x0b, 0x86, 0x05, 0x32, 0x4a, 0x2e, 0x0b, 0x65, 0x05, 0xd3, 0xd6, 0x30, 0xb2, 0xfc, 0xa7, 0x02, 0x41, 0x00, 0xc2, 0xed, 0x31, 0xdc, 0x40, 0x9c, 0x3a, 0xe8, 0x42, 0xe2, 0x60, 0x5e, 0x52, 0x3c, 0xc5, 0x54, 0x14, 0x0e, 0x8d, 0x7c, 0x3c, 0x34, 0xbe, 0xa6, 0x05, 0x86, 0xa2, 0x36, 0x5d, 0xd9, 0x0e, 0x3e, 0xd4, 0x52, 0x50, 0xa9, 0x35, 0x01, 0x93, 0x68, 0x92, 0x2e, 0x9a, 0x86, 0x27, 0x1a, 0xab, 0x32, 0x9e, 0xe2, 0x79, 0x9f, 0x5b, 0xf3, 0xa5, 0xd2, 0xf1, 0xd3, 0x6e, 0x7b, 0x3e, 0x1b, 0x85, 0x93, 0x02, 0x40, 0x68, 0xb8, 0xb6, 0x7e, 0x8c, 0xba, 0x3c, 0xf2, 0x8a, 0x2e, 0xea, 0x4f, 0x07, 0xd3, 0x68, 0x62, 0xee, 0x1a, 0x04, 0x16, 0x44, 0x0d, 0xef, 0xf6, 0x1b, 0x95, 0x65, 0xa5, 0xd1, 0x47, 0x81, 0x2c, 0x14, 0xb3, 0x8e, 0xf9, 0x08, 0xcf, 0x11, 0x07, 0x55, 0xca, 0x2a, 0xad, 0xf7, 0xd3, 0xbd, 0x0f, 0x97, 0xf0, 0xde, 0xde, 0x70, 0xb6, 0x44, 0x70, 0x47, 0xf7, 0xf9, 0xcf, 0x75, 0x61, 0x7f, 0xf3, 0x02, 0x40, 0x38, 0x4a, 0x67, 0xaf, 0xae, 0xb6, 0xb2, 0x6a, 0x00, 0x25, 0x5a, 0xa4, 0x65, 0x20, 0xb1, 0x13, 0xbd, 0x83, 0xff, 0xb4, 0xbc, 0xf4, 0xdd, 0xa1, 0xbb, 0x1c, 0x96, 0x37, 0x35, 0xf4, 0xbf, 0xed, 0x4c, 0xed, 0x92, 0xe8, 0xac, 0xc9, 0xc1, 0xa5, 0xa3, 0x23, 0x66, 0x40, 0x8a, 0xa1, 0xe6, 0xe3, 0x95, 0xfe, 0xc4, 0x53, 0xf5, 0x7d, 0x6e, 0xca, 0x45, 0x42, 0xe4, 0xc2, 0x9f, 0xe5, 0x1e, 0xb5, }; static const unsigned char KEY2[] = { 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xa8, 0x6e, 0x40, 0x86, 0x9f, 0x98, 0x59, 0xfb, 0x57, 0xbf, 0xc1, 0x55, 0x12, 0x38, 0xeb, 0xb3, 0x46, 0x34, 0xc9, 0x35, 0x4d, 0xfd, 0x03, 0xe9, 0x3a, 0x88, 0x9e, 0x97, 0x8f, 0xf4, 0xec, 0x36, 0x7b, 0x3f, 0xba, 0xb8, 0xa5, 0x96, 0x30, 0x03, 0xc5, 0xc6, 0xd9, 0xa8, 0x4e, 0xbc, 0x23, 0x51, 0xa1, 0x96, 0xd2, 0x03, 0x98, 0x73, 0xb6, 0x17, 0x9c, 0x77, 0xd4, 0x95, 0x1e, 0x1b, 0xb3, 0x1b, 0xc8, 0x71, 0xd1, 0x2e, 0x31, 0xc7, 0x6a, 0x75, 0x57, 0x08, 0x7f, 0xba, 0x70, 0x76, 0xf7, 0x67, 0xf4, 0x4e, 0xbe, 0xfc, 0x70, 0x61, 0x41, 0x07, 0x2b, 0x7c, 0x3c, 0x3b, 0xb3, 0xbc, 0xd5, 0xa8, 0xbd, 0x28, 0xd8, 0x49, 0xd3, 0xe1, 0x78, 0xc8, 0xc1, 0x42, 0x5e, 0x18, 0x36, 0xa8, 0x41, 0xf7, 0xc8, 0xaa, 0x35, 0xfe, 0x2d, 0xd1, 0xb4, 0xcc, 0x00, 0x67, 0xae, 0x79, 0xd3, 0x28, 0xd5, 0x5b, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81, 0x00, 0xa6, 0x00, 0x83, 0xf8, 0x2b, 0x33, 0xac, 0xfb, 0xdb, 0xf0, 0x52, 0x4b, 0xd6, 0x39, 0xe3, 0x94, 0x3d, 0x8d, 0xa9, 0x01, 0xb0, 0x6b, 0xbe, 0x7f, 0x10, 0x01, 0xb6, 0xcd, 0x0a, 0x45, 0x0a, 0xca, 0x67, 0x8e, 0xd8, 0x29, 0x44, 0x8a, 0x51, 0xa8, 0x66, 0x35, 0x26, 0x30, 0x8b, 0xe9, 0x41, 0xa6, 0x22, 0xec, 0xd2, 0xf0, 0x58, 0x41, 0x33, 0x26, 0xf2, 0x3f, 0xe8, 0x75, 0x4f, 0xc7, 0x5d, 0x2e, 0x5a, 0xa8, 0x7a, 0xd2, 0xbf, 0x59, 0xa0, 0x86, 0x79, 0x0b, 0x92, 0x6c, 0x95, 0x5d, 0x87, 0x63, 0x5c, 0xd6, 0x1a, 0xc0, 0xf6, 0x7a, 0x15, 0x8d, 0xc7, 0x3c, 0xb6, 0x9e, 0xa6, 0x58, 0x46, 0x9b, 0xbf, 0x3e, 0x28, 0x8c, 0xdf, 0x1a, 0x87, 0xaa, 0x7e, 0xf5, 0xf2, 0xcb, 0x5e, 0x84, 0x2d, 0xf6, 0x82, 0x7e, 0x89, 0x4e, 0xf5, 0xe6, 0x3c, 0x92, 0x80, 0x1e, 0x98, 0x1c, 0x6a, 0x7b, 0x57, 0x01, 0x02, 0x41, 0x00, 0xdd, 0x60, 0x95, 0xd7, 0xa1, 0x9d, 0x0c, 0xa1, 0x84, 0xc5, 0x39, 0xca, 0x67, 0x4c, 0x1c, 0x06, 0x71, 0x5b, 0x5c, 0x2d, 0x8d, 0xce, 0xcd, 0xe2, 0x79, 0xc8, 0x33, 0xbe, 0x50, 0x37, 0x60, 0x9f, 0x3b, 0xb9, 0x59, 0x55, 0x22, 0x1f, 0xa5, 0x4b, 0x1d, 0xca, 0x38, 0xa0, 0xab, 0x87, 0x9c, 0x86, 0x0e, 0xdb, 0x1c, 0x4f, 0x4f, 0x07, 0xed, 0x18, 0x3f, 0x05, 0x3c, 0xec, 0x78, 0x11, 0xf6, 0x99, 0x02, 0x41, 0x00, 0xc2, 0xc5, 0xcf, 0xbe, 0x95, 0x91, 0xeb, 0xcf, 0x47, 0xf3, 0x33, 0x32, 0xc7, 0x7e, 0x93, 0x56, 0xf7, 0xd8, 0xf9, 0xd4, 0xb6, 0xd6, 0x20, 0xac, 0xba, 0x8a, 0x20, 0x19, 0x14, 0xab, 0xc5, 0x5d, 0xb2, 0x08, 0xcc, 0x77, 0x7c, 0x65, 0xa8, 0xdb, 0x66, 0x97, 0x36, 0x44, 0x2c, 0x63, 0xc0, 0x6a, 0x7e, 0xb0, 0x0b, 0x5c, 0x90, 0x12, 0x50, 0xb4, 0x36, 0x60, 0xc3, 0x1f, 0x22, 0x0c, 0xc8, 0x13, 0x02, 0x40, 0x33, 0xc8, 0x7e, 0x04, 0x7c, 0x97, 0x61, 0xf6, 0xfe, 0x39, 0xac, 0x34, 0xfe, 0x48, 0xbd, 0x5d, 0x7c, 0x72, 0xa4, 0x73, 0x3b, 0x72, 0x9e, 0x92, 0x55, 0x6e, 0x51, 0x3c, 0x39, 0x43, 0x5a, 0xe4, 0xa4, 0x71, 0xcc, 0xc5, 0xaf, 0x3f, 0xbb, 0xc8, 0x80, 0x65, 0x67, 0x2d, 0x9e, 0x32, 0x10, 0x99, 0x03, 0x2c, 0x99, 0xc8, 0xab, 0x71, 0xed, 0x31, 0xf8, 0xbb, 0xde, 0xee, 0x69, 0x7f, 0xba, 0x31, 0x02, 0x40, 0x7e, 0xbc, 0x60, 0x55, 0x4e, 0xd5, 0xc8, 0x6e, 0xf4, 0x0e, 0x57, 0xbe, 0x2e, 0xf9, 0x39, 0xbe, 0x59, 0x3f, 0xa2, 0x30, 0xbb, 0x57, 0xd1, 0xa3, 0x13, 0x2e, 0x55, 0x7c, 0x7c, 0x6a, 0xd8, 0xde, 0x02, 0xbe, 0x9e, 0xed, 0x10, 0xd0, 0xc5, 0x73, 0x1d, 0xea, 0x3e, 0xb1, 0x55, 0x81, 0x02, 0xef, 0x48, 0xc8, 0x1c, 0x5c, 0x7a, 0x92, 0xb0, 0x58, 0xd3, 0x19, 0x5b, 0x5d, 0xa2, 0xb6, 0x56, 0x69, 0x02, 0x40, 0x1e, 0x00, 0x6a, 0x9f, 0xba, 0xee, 0x46, 0x5a, 0xc5, 0xb5, 0x9f, 0x91, 0x33, 0xdd, 0xc9, 0x96, 0x75, 0xb7, 0x87, 0xcf, 0x18, 0x1c, 0xb7, 0xb9, 0x3f, 0x04, 0x10, 0xb8, 0x75, 0xa9, 0xb8, 0xa0, 0x31, 0x35, 0x03, 0x30, 0x89, 0xc8, 0x37, 0x68, 0x20, 0x30, 0x99, 0x39, 0x96, 0xd6, 0x2b, 0x3d, 0x5e, 0x45, 0x84, 0xf7, 0xd2, 0x61, 0x50, 0xc9, 0x50, 0xba, 0x8d, 0x08, 0xaa, 0xd0, 0x08, 0x1e, }; static const PKCS12_ATTR ATTRS1[] = { { "friendlyName", "george" }, { "localKeyID", "1234567890" }, { "1.2.3.4.5", "MyCustomAttribute" }, { NULL, NULL } }; static const PKCS12_ATTR ATTRS2[] = { { "friendlyName", "janet" }, { "localKeyID", "987654321" }, { "1.2.3.5.8.13", "AnotherCustomAttribute" }, { NULL, NULL } }; static const PKCS12_ATTR ATTRS3[] = { { "friendlyName", "wildduk" }, { "localKeyID", "1122334455" }, { "oracle-jdk-trustedkeyusage", "anyExtendedKeyUsage" }, { NULL, NULL } }; static const PKCS12_ATTR ATTRS4[] = { { "friendlyName", "wildduk" }, { "localKeyID", "1122334455" }, { NULL, NULL } }; static const PKCS12_ENC enc_default = { #ifndef OPENSSL_NO_DES NID_pbe_WithSHA1And3_Key_TripleDES_CBC, #else NID_aes_128_cbc, #endif "Password1", 1000 }; static const PKCS12_ENC mac_default = { NID_sha1, "Password1", 1000 }; static const int enc_nids_all[] = { NID_aes_128_cbc, NID_aes_256_cbc, #ifndef OPENSSL_NO_DES NID_des_ede3_cbc, NID_des_cbc, #endif #ifndef OPENSSL_NO_RC5 NID_rc5_cbc, #endif #ifndef OPENSSL_NO_RC4 NID_rc4, #endif #ifndef OPENSSL_NO_RC2 NID_rc2_cbc, #endif #ifndef OPENSSL_NO_MD2 # ifndef OPENSSL_NO_DES NID_pbeWithMD2AndDES_CBC, # endif # ifndef OPENSSL_NO_RC2 NID_pbeWithMD2AndRC2_CBC, # endif #endif #ifndef OPENSSL_NO_MD5 # ifndef OPENSSL_NO_DES NID_pbeWithMD5AndDES_CBC, # endif # ifndef OPENSSL_NO_RC2 NID_pbeWithMD5AndRC2_CBC, # endif #endif #ifndef OPENSSL_NO_DES NID_pbeWithSHA1AndDES_CBC, #endif #ifndef OPENSSL_NO_RC2 NID_pbe_WithSHA1And128BitRC2_CBC, NID_pbe_WithSHA1And40BitRC2_CBC, NID_pbeWithSHA1AndRC2_CBC, #endif #ifndef OPENSSL_NO_RC4 NID_pbe_WithSHA1And128BitRC4, NID_pbe_WithSHA1And40BitRC4, #endif #ifndef OPENSSL_NO_DES NID_pbe_WithSHA1And2_Key_TripleDES_CBC, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, #endif }; static const int enc_nids_no_legacy[] = { NID_aes_128_cbc, NID_aes_256_cbc, #ifndef OPENSSL_NO_DES NID_des_ede3_cbc, NID_pbe_WithSHA1And2_Key_TripleDES_CBC, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, #endif }; static const int mac_nids[] = { NID_sha1, NID_md5, NID_sha256, NID_sha512, NID_sha3_256, NID_sha3_512 }; static const int iters[] = { 1, 1000 }; static const char *passwords[] = { "Password1", "", }; static int get_custom_oid(void) { static int sec_nid = -1; if (sec_nid != -1) return sec_nid; if (!TEST_true(OBJ_create("1.3.5.7.9", "CustomSecretOID", "My custom secret OID"))) return -1; return sec_nid = OBJ_txt2nid("CustomSecretOID"); } static int test_single_cert_no_attrs(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("1cert.p12"); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), NULL); end_contentinfo(pb); end_pkcs12(pb); start_check_pkcs12(pb); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), NULL); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_key(PKCS12_ENC *enc) { char fname[80]; PKCS12_BUILDER *pb; sprintf(fname, "1key_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter); pb = new_pkcs12_builder(fname); start_pkcs12(pb); start_contentinfo(pb); add_keybag(pb, KEY1, sizeof(KEY1), NULL, enc); end_contentinfo(pb); end_pkcs12(pb); start_check_pkcs12(pb); start_check_contentinfo(pb); check_keybag(pb, KEY1, sizeof(KEY1), NULL, enc); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_key_enc_alg(int z) { PKCS12_ENC enc; if (lgcyprov == NULL) enc.nid = enc_nids_no_legacy[z]; else enc.nid = enc_nids_all[z]; enc.pass = enc_default.pass; enc.iter = enc_default.iter; return test_single_key(&enc); } static int test_single_key_enc_pass(int z) { PKCS12_ENC enc; enc.nid = enc_default.nid; enc.pass = passwords[z]; enc.iter = enc_default.iter; return test_single_key(&enc); } static int test_single_key_enc_iter(int z) { PKCS12_ENC enc; enc.nid = enc_default.nid; enc.pass = enc_default.pass; enc.iter = iters[z]; return test_single_key(&enc); } static int test_single_key_with_attrs(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("1keyattrs.p12"); start_pkcs12(pb); start_contentinfo(pb); add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_contentinfo(pb); end_pkcs12(pb); start_check_pkcs12(pb); start_check_contentinfo(pb); check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_cert_mac(PKCS12_ENC *mac) { char fname[80]; PKCS12_BUILDER *pb; sprintf(fname, "1cert_mac-%s_iter-%d.p12", OBJ_nid2sn(mac->nid), mac->iter); pb = new_pkcs12_builder(fname); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), NULL); end_contentinfo(pb); end_pkcs12_with_mac(pb, mac); start_check_pkcs12_with_mac(pb, mac); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), NULL); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_cert_mac_alg(int z) { PKCS12_ENC mac; mac.nid = mac_nids[z]; mac.pass = mac_default.pass; mac.iter = mac_default.iter; return test_single_cert_mac(&mac); } static int test_single_cert_mac_pass(int z) { PKCS12_ENC mac; mac.nid = mac_default.nid; mac.pass = passwords[z]; mac.iter = mac_default.iter; return test_single_cert_mac(&mac); } static int test_single_cert_mac_iter(int z) { PKCS12_ENC mac; mac.nid = mac_default.nid; mac.pass = mac_default.pass; mac.iter = iters[z]; return test_single_cert_mac(&mac); } static int test_cert_key_with_attrs_and_mac(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key.p12"); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_contentinfo(pb); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_cert_key_encrypted_content(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key_enc.p12"); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_contentinfo_encrypted(pb, &enc_default); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo_encrypted(pb, &enc_default); check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_secret_encrypted_content(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("1secret.p12"); int custom_nid = get_custom_oid(); start_pkcs12(pb); start_contentinfo(pb); add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1); end_contentinfo_encrypted(pb, &enc_default); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo_encrypted(pb, &enc_default); check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_secret(PKCS12_ENC *enc) { int custom_nid; char fname[80]; PKCS12_BUILDER *pb; sprintf(fname, "1secret_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter); pb = new_pkcs12_builder(fname); custom_nid = get_custom_oid(); start_pkcs12(pb); start_contentinfo(pb); add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1); end_contentinfo_encrypted(pb, enc); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo_encrypted(pb, enc); check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_single_secret_enc_alg(int z) { PKCS12_ENC enc; if (lgcyprov == NULL) enc.nid = enc_nids_no_legacy[z]; else enc.nid = enc_nids_all[z]; enc.pass = enc_default.pass; enc.iter = enc_default.iter; return test_single_secret(&enc); } static int test_multiple_contents(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("multi_contents.p12"); int custom_nid = get_custom_oid(); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); add_certbag(pb, CERT2, sizeof(CERT2), ATTRS2); add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); add_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default); end_contentinfo(pb); start_contentinfo(pb); add_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1); end_contentinfo_encrypted(pb, &enc_default); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1); check_certbag(pb, CERT2, sizeof(CERT2), ATTRS2); check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default); check_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default); end_check_contentinfo(pb); start_check_contentinfo_encrypted(pb, &enc_default); check_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_jdk_trusted_attr(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("jdk_trusted.p12"); start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), ATTRS3); end_contentinfo(pb); end_pkcs12_with_mac(pb, &mac_default); start_check_pkcs12_with_mac(pb, &mac_default); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), ATTRS3); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); } static int test_set0_attrs(void) { PKCS12_BUILDER *pb = new_pkcs12_builder("attrs.p12"); PKCS12_SAFEBAG *bag = NULL; STACK_OF(X509_ATTRIBUTE) *attrs = NULL; X509_ATTRIBUTE *attr = NULL; start_pkcs12(pb); start_contentinfo(pb); add_certbag(pb, CERT1, sizeof(CERT1), ATTRS4); bag = sk_PKCS12_SAFEBAG_value(pb->bags, 0); attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag); attr = X509_ATTRIBUTE_create(NID_oracle_jdk_trustedkeyusage, V_ASN1_OBJECT, OBJ_txt2obj("anyExtendedKeyUsage", 0)); X509at_add1_attr(&attrs, attr); PKCS12_SAFEBAG_set0_attrs(bag, attrs); attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag); X509_ATTRIBUTE_free(attr); if(!TEST_ptr(attrs)) { goto err; } end_contentinfo(pb); end_pkcs12(pb); start_check_pkcs12(pb); start_check_contentinfo(pb); check_certbag(pb, CERT1, sizeof(CERT1), ATTRS3); end_check_contentinfo(pb); end_check_pkcs12(pb); return end_pkcs12_builder(pb); err: (void)end_pkcs12_builder(pb); return 0; } #ifndef OPENSSL_NO_DES static int pkcs12_create_test(void) { int ret = 0; EVP_PKEY *pkey = NULL; PKCS12 *p12 = NULL; const unsigned char *p; static const unsigned char rsa_key[] = { 0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbb, 0x24, 0x7a, 0x09, 0x7e, 0x0e, 0xb2, 0x37, 0x32, 0xcc, 0x39, 0x67, 0xad, 0xf1, 0x9e, 0x3d, 0x6b, 0x82, 0x83, 0xd1, 0xd0, 0xac, 0xa4, 0xc0, 0x18, 0xbe, 0x8d, 0x98, 0x00, 0xc0, 0x7b, 0xff, 0x07, 0x44, 0xc9, 0xca, 0x1c, 0xba, 0x36, 0xe1, 0x27, 0x69, 0xff, 0xb1, 0xe3, 0x8d, 0x8b, 0xee, 0x57, 0xa9, 0x3a, 0xaa, 0x16, 0x43, 0x39, 0x54, 0x19, 0x7c, 0xae, 0x69, 0x24, 0x14, 0xf6, 0x64, 0xff, 0xbc, 0x74, 0xc6, 0x67, 0x6c, 0x4c, 0xf1, 0x02, 0x49, 0x69, 0xc7, 0x2b, 0xe1, 0xe1, 0xa1, 0xa3, 0x43, 0x14, 0xf4, 0x77, 0x8f, 0xc8, 0xd0, 0x85, 0x5a, 0x35, 0x95, 0xac, 0x62, 0xa9, 0xc1, 0x21, 0x00, 0x77, 0xa0, 0x8b, 0x97, 0x30, 0xb4, 0x5a, 0x2c, 0xb8, 0x90, 0x2f, 0x48, 0xa0, 0x05, 0x28, 0x4b, 0xf2, 0x0f, 0x8d, 0xec, 0x8b, 0x4d, 0x03, 0x42, 0x75, 0xd6, 0xad, 0x81, 0xc0, 0x11, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x00, 0xfc, 0xb9, 0x4a, 0x26, 0x07, 0x89, 0x51, 0x2b, 0x53, 0x72, 0x91, 0xe0, 0x18, 0x3e, 0xa6, 0x5e, 0x31, 0xef, 0x9c, 0x0c, 0x16, 0x24, 0x42, 0xd0, 0x28, 0x33, 0xf9, 0xfa, 0xd0, 0x3c, 0x54, 0x04, 0x06, 0xc0, 0x15, 0xf5, 0x1b, 0x9a, 0xb3, 0x24, 0x31, 0xab, 0x3c, 0x6b, 0x47, 0x43, 0xb0, 0xd2, 0xa9, 0xdc, 0x05, 0xe1, 0x81, 0x59, 0xb6, 0x04, 0xe9, 0x66, 0x61, 0xaa, 0xd7, 0x0b, 0x00, 0x8f, 0x3d, 0xe5, 0xbf, 0xa2, 0xf8, 0x5e, 0x25, 0x6c, 0x1e, 0x22, 0x0f, 0xb4, 0xfd, 0x41, 0xe2, 0x03, 0x31, 0x5f, 0xda, 0x20, 0xc5, 0xc0, 0xf3, 0x55, 0x0e, 0xe1, 0xc9, 0xec, 0xd7, 0x3e, 0x2a, 0x0c, 0x01, 0xca, 0x7b, 0x22, 0xcb, 0xac, 0xf4, 0x2b, 0x27, 0xf0, 0x78, 0x5f, 0xb5, 0xc2, 0xf9, 0xe8, 0x14, 0x5a, 0x6e, 0x7e, 0x86, 0xbd, 0x6a, 0x9b, 0x20, 0x0c, 0xba, 0xcc, 0x97, 0x20, 0x11, 0x02, 0x41, 0x00, 0xc9, 0x59, 0x9f, 0x29, 0x8a, 0x5b, 0x9f, 0xe3, 0x2a, 0xd8, 0x7e, 0xc2, 0x40, 0x9f, 0xa8, 0x45, 0xe5, 0x3e, 0x11, 0x8d, 0x3c, 0xed, 0x6e, 0xab, 0xce, 0xd0, 0x65, 0x46, 0xd8, 0xc7, 0x07, 0x63, 0xb5, 0x23, 0x34, 0xf4, 0x9f, 0x7e, 0x1c, 0xc7, 0xc7, 0xf9, 0x65, 0xd1, 0xf4, 0x04, 0x42, 0x38, 0xbe, 0x3a, 0x0c, 0x9d, 0x08, 0x25, 0xfc, 0xa3, 0x71, 0xd9, 0xae, 0x0c, 0x39, 0x61, 0xf4, 0x89, 0x02, 0x41, 0x00, 0xed, 0xef, 0xab, 0xa9, 0xd5, 0x39, 0x9c, 0xee, 0x59, 0x1b, 0xff, 0xcf, 0x48, 0x44, 0x1b, 0xb6, 0x32, 0xe7, 0x46, 0x24, 0xf3, 0x04, 0x7f, 0xde, 0x95, 0x08, 0x6d, 0x75, 0x9e, 0x67, 0x17, 0xba, 0x5c, 0xa4, 0xd4, 0xe2, 0xe2, 0x4d, 0x77, 0xce, 0xeb, 0x66, 0x29, 0xc5, 0x96, 0xe0, 0x62, 0xbb, 0xe5, 0xac, 0xdc, 0x44, 0x62, 0x54, 0x86, 0xed, 0x64, 0x0c, 0xce, 0xd0, 0x60, 0x03, 0x9d, 0x49, 0x02, 0x40, 0x54, 0xd9, 0x18, 0x72, 0x27, 0xe4, 0xbe, 0x76, 0xbb, 0x1a, 0x6a, 0x28, 0x2f, 0x95, 0x58, 0x12, 0xc4, 0x2c, 0xa8, 0xb6, 0xcc, 0xe2, 0xfd, 0x0d, 0x17, 0x64, 0xc8, 0x18, 0xd7, 0xc6, 0xdf, 0x3d, 0x4c, 0x1a, 0x9e, 0xf9, 0x2a, 0xb0, 0xb9, 0x2e, 0x12, 0xfd, 0xec, 0xc3, 0x51, 0xc1, 0xed, 0xa9, 0xfd, 0xb7, 0x76, 0x93, 0x41, 0xd8, 0xc8, 0x22, 0x94, 0x1a, 0x77, 0xf6, 0x9c, 0xc3, 0xc3, 0x89, 0x02, 0x41, 0x00, 0x8e, 0xf9, 0xa7, 0x08, 0xad, 0xb5, 0x2a, 0x04, 0xdb, 0x8d, 0x04, 0xa1, 0xb5, 0x06, 0x20, 0x34, 0xd2, 0xcf, 0xc0, 0x89, 0xb1, 0x72, 0x31, 0xb8, 0x39, 0x8b, 0xcf, 0xe2, 0x8e, 0xa5, 0xda, 0x4f, 0x45, 0x1e, 0x53, 0x42, 0x66, 0xc4, 0x30, 0x4b, 0x29, 0x8e, 0xc1, 0x69, 0x17, 0x29, 0x8c, 0x8a, 0xe6, 0x0f, 0x82, 0x68, 0xa1, 0x41, 0xb3, 0xb6, 0x70, 0x99, 0x75, 0xa9, 0x27, 0x18, 0xe4, 0xe9, 0x02, 0x41, 0x00, 0x89, 0xea, 0x6e, 0x6d, 0x70, 0xdf, 0x25, 0x5f, 0x18, 0x3f, 0x48, 0xda, 0x63, 0x10, 0x8b, 0xfe, 0xa8, 0x0c, 0x94, 0x0f, 0xde, 0x97, 0x56, 0x53, 0x89, 0x94, 0xe2, 0x1e, 0x2c, 0x74, 0x3c, 0x91, 0x81, 0x34, 0x0b, 0xa6, 0x40, 0xf8, 0xcb, 0x2a, 0x60, 0x8c, 0xe0, 0x02, 0xb7, 0x89, 0x93, 0xcf, 0x18, 0x9f, 0x49, 0x54, 0xfd, 0x7d, 0x3f, 0x9a, 0xef, 0xd4, 0xa4, 0x4f, 0xc1, 0x45, 0x99, 0x91, }; p = rsa_key; if (!TEST_ptr(pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &p, sizeof(rsa_key), NULL, NULL))) goto err; if (!TEST_int_eq(ERR_peek_error(), 0)) goto err; p12 = PKCS12_create(NULL, NULL, pkey, NULL, NULL, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, 2, 1, 0); if (!TEST_ptr(p12)) goto err; if (!TEST_int_eq(ERR_peek_error(), 0)) goto err; ret = 1; err: PKCS12_free(p12); EVP_PKEY_free(pkey); return ret; } #endif static int pkcs12_recreate_test(void) { int ret = 0; X509 *cert = NULL; X509 *cert_parsed = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY *pkey_parsed = NULL; PKCS12 *p12 = NULL; PKCS12 *p12_parsed = NULL; PKCS12 *p12_recreated = NULL; const unsigned char *cert_bytes = CERT1; const unsigned char *key_bytes = KEY1; BIO *bio = NULL; cert = d2i_X509(NULL, &cert_bytes, sizeof(CERT1)); if (!TEST_ptr(cert)) goto err; pkey = d2i_AutoPrivateKey(NULL, &key_bytes, sizeof(KEY1)); if (!TEST_ptr(pkey)) goto err; p12 = PKCS12_create("pass", NULL, pkey, cert, NULL, NID_aes_256_cbc, NID_aes_256_cbc, 2, 1, 0); if (!TEST_ptr(p12)) goto err; if (!TEST_int_eq(ERR_peek_error(), 0)) goto err; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto err; if (!TEST_int_eq(i2d_PKCS12_bio(bio, p12), 1)) goto err; p12_parsed = PKCS12_init_ex(NID_pkcs7_data, testctx, NULL); if (!TEST_ptr(p12_parsed)) goto err; p12_parsed = d2i_PKCS12_bio(bio, &p12_parsed); if (!TEST_ptr(p12_parsed)) goto err; if (!TEST_int_eq(PKCS12_parse(p12_parsed, "pass", &pkey_parsed, &cert_parsed, NULL), 1)) goto err; p12_recreated = PKCS12_create("new_pass", NULL, pkey_parsed, cert_parsed, NULL, NID_aes_256_cbc, NID_aes_256_cbc, 2, 1, 0); if (!TEST_ptr(p12_recreated)) goto err; if (!TEST_int_eq(ERR_peek_error(), 0)) goto err; ret = 1; err: BIO_free(bio); PKCS12_free(p12); PKCS12_free(p12_parsed); PKCS12_free(p12_recreated); EVP_PKEY_free(pkey); EVP_PKEY_free(pkey_parsed); X509_free(cert); X509_free(cert_parsed); return ret; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_WRITE, OPT_LEGACY, OPT_CONTEXT, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "write", OPT_WRITE, '-', "Write PKCS12 objects to file" }, { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" }, { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" }, { NULL } }; return options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_WRITE: PKCS12_helper_set_write_files(1); break; case OPT_LEGACY: PKCS12_helper_set_legacy(1); break; case OPT_CONTEXT: default_libctx = 0; break; case OPT_TEST_CASES: break; default: return 0; } } if (!default_libctx) { testctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(testctx)) return 0; nullprov = OSSL_PROVIDER_load(NULL, "null"); if (!TEST_ptr(nullprov)) return 0; } deflprov = OSSL_PROVIDER_load(testctx, "default"); if (!TEST_ptr(deflprov)) return 0; lgcyprov = OSSL_PROVIDER_load(testctx, "legacy"); PKCS12_helper_set_libctx(testctx); if (!default_libctx) { if (!TEST_false(OSSL_PROVIDER_available(NULL, "default")) || !TEST_false(OSSL_PROVIDER_available(NULL, "fips"))) return 0; } ADD_TEST(test_single_cert_no_attrs); if (lgcyprov == NULL) { ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_no_legacy)); ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_no_legacy)); } else { ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_all)); ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_all)); } #ifndef OPENSSL_NO_DES if (default_libctx) ADD_TEST(pkcs12_create_test); #endif if (default_libctx) ADD_TEST(pkcs12_recreate_test); ADD_ALL_TESTS(test_single_key_enc_pass, OSSL_NELEM(passwords)); ADD_ALL_TESTS(test_single_key_enc_iter, OSSL_NELEM(iters)); ADD_TEST(test_single_key_with_attrs); ADD_ALL_TESTS(test_single_cert_mac_alg, OSSL_NELEM(mac_nids)); ADD_ALL_TESTS(test_single_cert_mac_pass, OSSL_NELEM(passwords)); ADD_ALL_TESTS(test_single_cert_mac_iter, OSSL_NELEM(iters)); ADD_TEST(test_cert_key_with_attrs_and_mac); ADD_TEST(test_cert_key_encrypted_content); ADD_TEST(test_single_secret_encrypted_content); ADD_TEST(test_multiple_contents); ADD_TEST(test_jdk_trusted_attr); ADD_TEST(test_set0_attrs); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(nullprov); OSSL_PROVIDER_unload(deflprov); OSSL_PROVIDER_unload(lgcyprov); OSSL_LIB_CTX_free(testctx); }
test
openssl/test/pkcs12_format_test.c
openssl
#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; }
test
openssl/test/aesgcmtest.c
openssl
#include <string.h> #include <openssl/core_names.h> #include <openssl/params.h> #include <openssl/opensslconf.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/kdf.h> #include "internal/packet.h" #include "internal/nelem.h" #include "testutil.h" #define MAC_OFFSET (DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH) static unsigned char client_random[SSL3_RANDOM_SIZE]; static unsigned char server_random[SSL3_RANDOM_SIZE]; static unsigned char session_id[32]; static unsigned char master_secret[48]; static unsigned char cookie[20]; static unsigned char key_block[104]; #define mac_key (key_block + 20) #define dec_key (key_block + 40) #define enc_key (key_block + 56) static EVP_MD_CTX *handshake_md; static int do_PRF(const void *seed1, int seed1_len, const void *seed2, int seed2_len, const void *seed3, int seed3_len, unsigned char *out, int olen) { EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL); size_t outlen = olen; EVP_PKEY_derive_init(pctx); EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1()); EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, master_secret, sizeof(master_secret)); EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, seed1_len); EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, seed2_len); EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, seed3_len); EVP_PKEY_derive(pctx, out, &outlen); EVP_PKEY_CTX_free(pctx); return 1; } static SSL_SESSION *client_session(void) { static unsigned char session_asn1[] = { 0x30, 0x5F, 0x02, 0x01, 0x01, 0x02, 0x02, 0x01, 0x00, 0x04, 0x02, 0x00, 0x2F, 0x04, 0x20, #define SS_SESSID_OFS 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x30, #define SS_SECRET_OFS 49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; const unsigned char *p = session_asn1; memcpy(session_asn1 + SS_SESSID_OFS, session_id, sizeof(session_id)); memcpy(session_asn1 + SS_SECRET_OFS, master_secret, sizeof(master_secret)); return d2i_SSL_SESSION(NULL, &p, sizeof(session_asn1)); } static int validate_client_hello(BIO *wbio) { PACKET pkt, pkt2; long len; unsigned char *data; int cookie_found = 0; unsigned int u = 0; if ((len = BIO_get_mem_data(wbio, (char **)&data)) < 0) return 0; if (!PACKET_buf_init(&pkt, data, len)) return 0; if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER) return 0; if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3)) return 0; if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CLIENT_HELLO) return 0; if (!PACKET_forward(&pkt, DTLS1_HM_HEADER_LENGTH - 1)) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER) return 0; if (!PACKET_copy_bytes(&pkt, client_random, SSL3_RANDOM_SIZE)) return 0; if (!PACKET_get_length_prefixed_1(&pkt, &pkt2) || !PACKET_equal(&pkt2, session_id, sizeof(session_id))) return 0; if (!PACKET_get_length_prefixed_1(&pkt, &pkt2)) return 0; if (PACKET_remaining(&pkt2)) { if (!PACKET_equal(&pkt2, cookie, sizeof(cookie))) return 0; cookie_found = 1; } if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u)) return 0; if (!PACKET_get_1(&pkt, &u) || !PACKET_forward(&pkt, u)) return 0; if (!PACKET_get_net_2(&pkt, &u) || !PACKET_forward(&pkt, u)) return 0; if (PACKET_remaining(&pkt)) return 0; if (cookie_found && !EVP_DigestUpdate(handshake_md, data + MAC_OFFSET, len - MAC_OFFSET)) return 0; (void)BIO_reset(wbio); return 1 + cookie_found; } static int send_hello_verify(BIO *rbio) { static unsigned char hello_verify[] = { 0x16, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x03, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x01, 0x00, 0x14, #define HV_COOKIE_OFS 28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; memcpy(hello_verify + HV_COOKIE_OFS, cookie, sizeof(cookie)); BIO_write(rbio, hello_verify, sizeof(hello_verify)); return 1; } static int send_server_hello(BIO *rbio) { static unsigned char server_hello[] = { 0x16, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x52, 0x02, 0x00, 0x00, 0x46, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x01, 0x00, #define SH_RANDOM_OFS 27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, #define SH_SESSID_OFS 60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x00, }; static unsigned char change_cipher_spec[] = { 0x14, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x01, 0x00, 0x02, }; memcpy(server_hello + SH_RANDOM_OFS, server_random, sizeof(server_random)); memcpy(server_hello + SH_SESSID_OFS, session_id, sizeof(session_id)); if (!EVP_DigestUpdate(handshake_md, server_hello + MAC_OFFSET, sizeof(server_hello) - MAC_OFFSET)) return 0; BIO_write(rbio, server_hello, sizeof(server_hello)); BIO_write(rbio, change_cipher_spec, sizeof(change_cipher_spec)); return 1; } static int send_record(BIO *rbio, unsigned char type, uint64_t seqnr, const void *msg, size_t len) { static unsigned char epoch[2] = { 0x00, 0x01 }; static unsigned char seq[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static unsigned char ver[2] = { 0x01, 0x00 }; unsigned char lenbytes[2]; EVP_MAC *hmac = NULL; EVP_MAC_CTX *ctx = NULL; EVP_CIPHER_CTX *enc_ctx = NULL; unsigned char iv[16]; unsigned char pad; unsigned char *enc; OSSL_PARAM params[2]; int ret = 0; seq[0] = (seqnr >> 40) & 0xff; seq[1] = (seqnr >> 32) & 0xff; seq[2] = (seqnr >> 24) & 0xff; seq[3] = (seqnr >> 16) & 0xff; seq[4] = (seqnr >> 8) & 0xff; seq[5] = seqnr & 0xff; pad = 15 - ((len + SHA_DIGEST_LENGTH) % 16); enc = OPENSSL_malloc(len + SHA_DIGEST_LENGTH + 1 + pad); if (enc == NULL) return 0; memcpy(enc, msg, len); if (!TEST_ptr(hmac = EVP_MAC_fetch(NULL, "HMAC", NULL)) || !TEST_ptr(ctx = EVP_MAC_CTX_new(hmac))) goto end; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, "SHA1", 0); params[1] = OSSL_PARAM_construct_end(); lenbytes[0] = (unsigned char)(len >> 8); lenbytes[1] = (unsigned char)(len); if (!EVP_MAC_init(ctx, mac_key, 20, params) || !EVP_MAC_update(ctx, epoch, 2) || !EVP_MAC_update(ctx, seq, 6) || !EVP_MAC_update(ctx, &type, 1) || !EVP_MAC_update(ctx, ver, 2) || !EVP_MAC_update(ctx, lenbytes, 2) || !EVP_MAC_update(ctx, enc, len) || !EVP_MAC_final(ctx, enc + len, NULL, SHA_DIGEST_LENGTH)) goto end; len += SHA_DIGEST_LENGTH; do { enc[len++] = pad; } while (len % 16); if (!TEST_int_gt(RAND_bytes(iv, sizeof(iv)), 0) || !TEST_ptr(enc_ctx = EVP_CIPHER_CTX_new()) || !TEST_true(EVP_CipherInit_ex(enc_ctx, EVP_aes_128_cbc(), NULL, enc_key, iv, 1)) || !TEST_int_ge(EVP_Cipher(enc_ctx, enc, enc, len), 0)) goto end; BIO_write(rbio, &type, 1); BIO_write(rbio, ver, 2); BIO_write(rbio, epoch, 2); BIO_write(rbio, seq, 6); lenbytes[0] = (unsigned char)((len + sizeof(iv)) >> 8); lenbytes[1] = (unsigned char)(len + sizeof(iv)); BIO_write(rbio, lenbytes, 2); BIO_write(rbio, iv, sizeof(iv)); BIO_write(rbio, enc, len); ret = 1; end: EVP_MAC_free(hmac); EVP_MAC_CTX_free(ctx); EVP_CIPHER_CTX_free(enc_ctx); OPENSSL_free(enc); return ret; } static int send_finished(SSL *s, BIO *rbio) { static unsigned char finished_msg[DTLS1_HM_HEADER_LENGTH + TLS1_FINISH_MAC_LENGTH] = { 0x14, 0x00, 0x00, 0x0c, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, }; unsigned char handshake_hash[EVP_MAX_MD_SIZE]; do_PRF(TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE, server_random, SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE, key_block, sizeof(key_block)); if (!EVP_DigestFinal_ex(handshake_md, handshake_hash, NULL)) return 0; do_PRF(TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, handshake_hash, EVP_MD_CTX_get_size(handshake_md), NULL, 0, finished_msg + DTLS1_HM_HEADER_LENGTH, TLS1_FINISH_MAC_LENGTH); return send_record(rbio, SSL3_RT_HANDSHAKE, 0, finished_msg, sizeof(finished_msg)); } static int validate_ccs(BIO *wbio) { PACKET pkt; long len; unsigned char *data; unsigned int u; len = BIO_get_mem_data(wbio, (char **)&data); if (len < 0) return 0; if (!PACKET_buf_init(&pkt, data, len)) return 0; if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_CHANGE_CIPHER_SPEC) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER) return 0; if (!PACKET_forward(&pkt, DTLS1_RT_HEADER_LENGTH - 3)) return 0; if (!PACKET_get_1(&pkt, &u) || u != SSL3_MT_CCS) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != 0x0002) return 0; if (!PACKET_get_1(&pkt, &u) || u != SSL3_RT_HANDSHAKE) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != DTLS1_BAD_VER) return 0; if (!PACKET_get_net_2(&pkt, &u) || u != 0x0001) return 0; return 1; } #define NODROP(x) { x##UL, 0 } #define DROP(x) { x##UL, 1 } static struct { uint64_t seq; int drop; } tests[] = { NODROP(1), NODROP(3), NODROP(2), NODROP(0x1234), NODROP(0x1230), NODROP(0x1235), NODROP(0xffff), NODROP(0x10001), NODROP(0xfffe), NODROP(0x10000), DROP(0x10001), DROP(0xff), NODROP(0x100000), NODROP(0x800000), NODROP(0x7fffe1), NODROP(0xffffff), NODROP(0x1000000), NODROP(0xfffffe), DROP(0xffffff), NODROP(0x1000010), NODROP(0xfffffd), NODROP(0x1000011), DROP(0x12), NODROP(0x1000012), NODROP(0x1ffffff), NODROP(0x2000000), DROP(0x1ff00fe), NODROP(0x2000001), NODROP(0x20fffff), NODROP(0x2105500), DROP(0x20ffffe), NODROP(0x21054ff), NODROP(0x211ffff), DROP(0x2110000), NODROP(0x2120000) }; static int test_bad_dtls(void) { SSL_SESSION *sess = NULL; SSL_CTX *ctx = NULL; SSL *con = NULL; BIO *rbio = NULL; BIO *wbio = NULL; time_t now = 0; int testresult = 0; int ret; int i; RAND_bytes(session_id, sizeof(session_id)); RAND_bytes(master_secret, sizeof(master_secret)); RAND_bytes(cookie, sizeof(cookie)); RAND_bytes(server_random + 4, sizeof(server_random) - 4); now = time(NULL); memcpy(server_random, &now, sizeof(now)); sess = client_session(); if (!TEST_ptr(sess)) goto end; handshake_md = EVP_MD_CTX_new(); if (!TEST_ptr(handshake_md) || !TEST_true(EVP_DigestInit_ex(handshake_md, EVP_md5_sha1(), NULL))) goto end; ctx = SSL_CTX_new(DTLS_client_method()); if (!TEST_ptr(ctx) || !TEST_true(SSL_CTX_set_min_proto_version(ctx, DTLS1_BAD_VER)) || !TEST_true(SSL_CTX_set_max_proto_version(ctx, DTLS1_BAD_VER)) || !TEST_true(SSL_CTX_set_options(ctx, SSL_OP_LEGACY_SERVER_CONNECT)) || !TEST_true(SSL_CTX_set_cipher_list(ctx, "AES128-SHA"))) goto end; SSL_CTX_set_security_level(ctx, 0); con = SSL_new(ctx); if (!TEST_ptr(con) || !TEST_true(SSL_set_session(con, sess))) goto end; SSL_SESSION_free(sess); rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(rbio) || !TEST_ptr(wbio)) goto end; SSL_set_bio(con, rbio, wbio); if (!TEST_true(BIO_up_ref(rbio))) { rbio = wbio = NULL; goto end; } if (!TEST_true(BIO_up_ref(wbio))) { wbio = NULL; goto end; } SSL_set_connect_state(con); ret = SSL_do_handshake(con); if (!TEST_int_le(ret, 0) || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ) || !TEST_int_eq(validate_client_hello(wbio), 1) || !TEST_true(send_hello_verify(rbio))) goto end; ret = SSL_do_handshake(con); if (!TEST_int_le(ret, 0) || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ) || !TEST_int_eq(validate_client_hello(wbio), 2) || !TEST_true(send_server_hello(rbio))) goto end; ret = SSL_do_handshake(con); if (!TEST_int_le(ret, 0) || !TEST_int_eq(SSL_get_error(con, ret), SSL_ERROR_WANT_READ) || !TEST_true(send_finished(con, rbio))) goto end; ret = SSL_do_handshake(con); if (!TEST_int_gt(ret, 0) || !TEST_true(validate_ccs(wbio))) goto end; for (i = 0; i < (int)OSSL_NELEM(tests); i++) { uint64_t recv_buf[2]; if (!TEST_true(send_record(rbio, SSL3_RT_APPLICATION_DATA, tests[i].seq, &tests[i].seq, sizeof(uint64_t)))) { TEST_error("Failed to send data seq #0x%x%08x (%d)\n", (unsigned int)(tests[i].seq >> 32), (unsigned int)tests[i].seq, i); goto end; } if (tests[i].drop) continue; ret = SSL_read(con, recv_buf, 2 * sizeof(uint64_t)); if (!TEST_int_eq(ret, (int)sizeof(uint64_t))) { TEST_error("SSL_read failed or wrong size on seq#0x%x%08x (%d)\n", (unsigned int)(tests[i].seq >> 32), (unsigned int)tests[i].seq, i); goto end; } if (!TEST_true(recv_buf[0] == tests[i].seq)) goto end; } if (!TEST_false(tests[i-1].drop)) goto end; testresult = 1; end: BIO_free(rbio); BIO_free(wbio); SSL_free(con); SSL_CTX_free(ctx); EVP_MD_CTX_free(handshake_md); return testresult; } int setup_tests(void) { ADD_TEST(test_bad_dtls); return 1; }
test
openssl/test/bad_dtls_test.c
openssl
#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; }
test
openssl/test/rc5test.c
openssl
#include <string.h> #include "helpers/ssltestlib.h" #include "testutil.h" static int docorrupt = 0; static void copy_flags(BIO *bio) { int flags; BIO *next = BIO_next(bio); flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS); BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS); BIO_set_flags(bio, flags); } static int tls_corrupt_read(BIO *bio, char *out, int outl) { int ret; BIO *next = BIO_next(bio); ret = BIO_read(next, out, outl); copy_flags(bio); return ret; } static int tls_corrupt_write(BIO *bio, const char *in, int inl) { int ret; BIO *next = BIO_next(bio); char *copy; if (docorrupt) { if (!TEST_ptr(copy = OPENSSL_memdup(in, inl))) return 0; copy[inl-1] ^= 1; ret = BIO_write(next, copy, inl); OPENSSL_free(copy); } else { ret = BIO_write(next, in, inl); } copy_flags(bio); return ret; } static long tls_corrupt_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 tls_corrupt_gets(BIO *bio, char *buf, int size) { return -1; } static int tls_corrupt_puts(BIO *bio, const char *str) { return -1; } static int tls_corrupt_new(BIO *bio) { BIO_set_init(bio, 1); return 1; } static int tls_corrupt_free(BIO *bio) { BIO_set_init(bio, 0); return 1; } #define BIO_TYPE_CUSTOM_FILTER (0x80 | BIO_TYPE_FILTER) static BIO_METHOD *method_tls_corrupt = NULL; static const BIO_METHOD *bio_f_tls_corrupt_filter(void) { if (method_tls_corrupt == NULL) { method_tls_corrupt = BIO_meth_new(BIO_TYPE_CUSTOM_FILTER, "TLS corrupt filter"); if (method_tls_corrupt == NULL || !BIO_meth_set_write(method_tls_corrupt, tls_corrupt_write) || !BIO_meth_set_read(method_tls_corrupt, tls_corrupt_read) || !BIO_meth_set_puts(method_tls_corrupt, tls_corrupt_puts) || !BIO_meth_set_gets(method_tls_corrupt, tls_corrupt_gets) || !BIO_meth_set_ctrl(method_tls_corrupt, tls_corrupt_ctrl) || !BIO_meth_set_create(method_tls_corrupt, tls_corrupt_new) || !BIO_meth_set_destroy(method_tls_corrupt, tls_corrupt_free)) return NULL; } return method_tls_corrupt; } static void bio_f_tls_corrupt_filter_free(void) { BIO_meth_free(method_tls_corrupt); } static const char **cipher_list = NULL; static int setup_cipher_list(void) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; STACK_OF(SSL_CIPHER) *sk_ciphers = NULL; int i, j, numciphers = 0; if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method())) || !TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(sk_ciphers = SSL_get1_supported_ciphers(ssl))) goto err; cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) * sizeof(cipher_list[0])); if (!TEST_ptr(cipher_list)) goto err; for (j = 0, i = 0; i < sk_SSL_CIPHER_num(sk_ciphers); i++) { const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(sk_ciphers, i); if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa) cipher_list[j++] = SSL_CIPHER_get_name(cipher); } if (TEST_int_ne(j, 0)) numciphers = j; err: sk_SSL_CIPHER_free(sk_ciphers); SSL_free(ssl); SSL_CTX_free(ctx); return numciphers; } static char *cert = NULL; static char *privkey = NULL; static int test_ssl_corrupt(int testidx) { static unsigned char junk[16000] = { 0 }; SSL_CTX *sctx = NULL, *cctx = NULL; SSL *server = NULL, *client = NULL; BIO *c_to_s_fbio; int testresult = 0; STACK_OF(SSL_CIPHER) *ciphers; const SSL_CIPHER *currcipher; int err; docorrupt = 0; TEST_info("Starting #%d, %s", testidx, cipher_list[testidx]); if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)) || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher_list[testidx])) || !TEST_true(SSL_CTX_set_ciphersuites(cctx, "")) || !TEST_ptr(ciphers = SSL_CTX_get_ciphers(cctx)) || !TEST_int_eq(sk_SSL_CIPHER_num(ciphers), 1) || !TEST_ptr(currcipher = sk_SSL_CIPHER_value(ciphers, 0))) goto end; if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))) goto end; if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter()))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL, c_to_s_fbio))) goto end; if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE))) goto end; docorrupt = 1; if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0)) goto end; if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0)) goto end; do { err = ERR_get_error(); if (err == 0) { TEST_error("Decryption failed or bad record MAC not seen"); goto end; } } while (ERR_GET_REASON(err) != SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); testresult = 1; end: SSL_free(server); SSL_free(client); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") int setup_tests(void) { int n; 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; n = setup_cipher_list(); if (n > 0) ADD_ALL_TESTS(test_ssl_corrupt, n); return 1; } void cleanup_tests(void) { bio_f_tls_corrupt_filter_free(); OPENSSL_free(cipher_list); }
test
openssl/test/sslcorrupttest.c
openssl
#include <openssl/rand.h> #include "testutil.h" static int test_rand_status(void) { return TEST_true(RAND_status()); } int setup_tests(void) { ADD_TEST(test_rand_status); return 1; }
test
openssl/test/rand_status_test.c
openssl
#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 int setup_tests(void) { #ifndef OPENSSL_NO_EC ADD_TEST(pkcs7_verify_test); #endif return 1; }
test
openssl/test/pkcs7_test.c
openssl
#include <openssl/crypto.h> #include "testutil.h" #define SECS_PER_DAY (24 * 60 * 60) 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; }
test
openssl/test/gmdifftest.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/crypto.h> #include "internal/list.h" #include "internal/nelem.h" #include "testutil.h" typedef struct testl_st TESTL; struct testl_st { int n; OSSL_LIST_MEMBER(fizz, TESTL); OSSL_LIST_MEMBER(buzz, TESTL); }; DEFINE_LIST_OF(fizz, TESTL); DEFINE_LIST_OF(buzz, TESTL); static int test_fizzbuzz(void) { OSSL_LIST(fizz) a; OSSL_LIST(buzz) b; TESTL elem[20]; const int nelem = OSSL_NELEM(elem); int i, na = 0, nb = 0; ossl_list_fizz_init(&a); ossl_list_buzz_init(&b); if (!TEST_true(ossl_list_fizz_is_empty(&a))) return 0; for (i = 1; i < nelem; i++) { ossl_list_fizz_init_elem(elem + i); ossl_list_buzz_init_elem(elem + i); elem[i].n = i; if (i % 3 == 0) { ossl_list_fizz_insert_tail(&a, elem + i); na++; } if (i % 5 == 0) { ossl_list_buzz_insert_head(&b, elem + i); nb++; } } if (!TEST_false(ossl_list_fizz_is_empty(&a)) || !TEST_size_t_eq(ossl_list_fizz_num(&a), na) || !TEST_size_t_eq(ossl_list_buzz_num(&b), nb) || !TEST_ptr(ossl_list_fizz_head(&a)) || !TEST_ptr(ossl_list_fizz_tail(&a)) || !TEST_ptr(ossl_list_buzz_head(&b)) || !TEST_ptr(ossl_list_buzz_tail(&b)) || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 3) || !TEST_int_eq(ossl_list_fizz_tail(&a)->n, na * 3) || !TEST_int_eq(ossl_list_buzz_head(&b)->n, nb * 5) || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 5)) return 0; ossl_list_fizz_remove(&a, ossl_list_fizz_head(&a)); ossl_list_buzz_remove(&b, ossl_list_buzz_tail(&b)); if (!TEST_size_t_eq(ossl_list_fizz_num(&a), --na) || !TEST_size_t_eq(ossl_list_buzz_num(&b), --nb) || !TEST_ptr(ossl_list_fizz_head(&a)) || !TEST_ptr(ossl_list_buzz_tail(&b)) || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 6) || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 10) || !TEST_ptr(ossl_list_fizz_next(ossl_list_fizz_head(&a))) || !TEST_ptr(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))) || !TEST_int_eq(ossl_list_fizz_next(ossl_list_fizz_head(&a))->n, 9) || !TEST_int_eq(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))->n, 15)) return 0; return 1; } typedef struct int_st INTL; struct int_st { int n; OSSL_LIST_MEMBER(int, INTL); }; DEFINE_LIST_OF(int, INTL); static int test_insert(void) { INTL *c, *d; OSSL_LIST(int) l; INTL elem[20]; size_t i; int n = 1; ossl_list_int_init(&l); for (i = 0; i < OSSL_NELEM(elem); i++) { ossl_list_int_init_elem(elem + i); elem[i].n = i; } ossl_list_int_insert_head(&l, elem + 3); ossl_list_int_insert_tail(&l, elem + 6); ossl_list_int_insert_before(&l, elem + 6, elem + 5); ossl_list_int_insert_before(&l, elem + 3, elem + 1); ossl_list_int_insert_after(&l, elem + 1, elem + 2); ossl_list_int_insert_after(&l, elem + 6, elem + 7); ossl_list_int_insert_after(&l, elem + 3, elem + 4); if (!TEST_size_t_eq(ossl_list_int_num(&l), 7)) return 0; c = ossl_list_int_head(&l); d = ossl_list_int_tail(&l); while (c != NULL && d != NULL) { if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 8 - n)) return 0; c = ossl_list_int_next(c); d = ossl_list_int_prev(d); n++; } if (!TEST_ptr_null(c) || !TEST_ptr_null(d)) return 0; ossl_list_int_remove(&l, elem + 1); ossl_list_int_remove(&l, elem + 6); ossl_list_int_remove(&l, elem + 7); n = 2; c = ossl_list_int_head(&l); d = ossl_list_int_tail(&l); while (c != NULL && d != NULL) { if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 7 - n)) return 0; c = ossl_list_int_next(c); d = ossl_list_int_prev(d); n++; } if (!TEST_ptr_null(c) || !TEST_ptr_null(d)) return 0; ossl_list_int_remove(&l, elem + 2); ossl_list_int_remove(&l, elem + 4); ossl_list_int_remove(&l, elem + 3); if (!TEST_ptr(ossl_list_int_head(&l)) || !TEST_ptr(ossl_list_int_tail(&l)) || !TEST_int_eq(ossl_list_int_head(&l)->n, 5) || !TEST_int_eq(ossl_list_int_tail(&l)->n, 5)) return 0; ossl_list_int_insert_head(&l, elem); ossl_list_int_remove(&l, elem + 5); if (!TEST_ptr(ossl_list_int_head(&l)) || !TEST_ptr(ossl_list_int_tail(&l)) || !TEST_int_eq(ossl_list_int_head(&l)->n, 0) || !TEST_int_eq(ossl_list_int_tail(&l)->n, 0)) return 0; ossl_list_int_remove(&l, elem); if (!TEST_ptr_null(ossl_list_int_head(&l)) || !TEST_ptr_null(ossl_list_int_tail(&l))) return 0; return 1; } int setup_tests(void) { ADD_TEST(test_fizzbuzz); ADD_TEST(test_insert); return 1; }
test
openssl/test/list_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/quic.h> #include <openssl/rand.h> #include "helpers/ssltestlib.h" #include "helpers/quictestlib.h" #include "testutil.h" #include "testutil/output.h" #include "../ssl/ssl_local.h" #include "internal/quic_error.h" static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *defctxnull = NULL; static char *certsdir = NULL; static char *cert = NULL; static char *ccert = NULL; static char *cauthca = NULL; static char *privkey = NULL; static char *cprivkey = NULL; static char *datadir = NULL; static int is_fips = 0; #if !defined(OPENSSL_NO_SSL_TRACE) \ && defined(OPENSSL_NO_BROTLI) && defined(OPENSSL_NO_ZSTD) \ && !defined(OPENSSL_NO_ECX) && !defined(OPENSSL_NO_DH) # define DO_SSL_TRACE_TEST #endif static int test_quic_write_read(int idx) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL_CTX *sctx = NULL; SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int j, k, ret = 0; unsigned char buf[20], scratch[64]; static char *msg = "A test message"; size_t msglen = strlen(msg); size_t numbytes = 0; int ssock = 0, csock = 0; uint64_t sid = UINT64_MAX; SSL_SESSION *sess = NULL; if (idx >= 1 && !qtest_supports_blocking()) return TEST_skip("Blocking tests not supported in this build"); for (k = 0; k < 2; k++) { if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert, privkey, idx >= 1 ? QTEST_FLAG_BLOCK : 0, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost"))) goto end; if (sess != NULL && !TEST_true(SSL_set_session(clientquic, sess))) goto end; if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto end; if (idx >= 1) { if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv), &ssock))) goto end; if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0)) goto end; } sid = 0; for (j = 0; j < 2; j++) { if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes)) || !TEST_size_t_eq(numbytes, msglen)) goto end; if (idx >= 1) { do { if (!TEST_true(wait_until_sock_readable(ssock))) goto end; ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf), &numbytes))) goto end; } while (numbytes == 0); if (!TEST_mem_eq(buf, numbytes, msg, msglen)) goto end; } if (idx >= 2 && j > 0) BIO_closesocket(csock); ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, msglen, &numbytes))) goto end; ossl_quic_tserver_tick(qtserv); SSL_handle_events(clientquic); if (idx >= 2 && j > 0) { if (!TEST_false(SSL_read_ex(clientquic, buf, 1, &numbytes)) || !TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_SYSCALL) || !TEST_false(SSL_write_ex(clientquic, msg, msglen, &numbytes)) || !TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_SYSCALL)) goto end; break; } if (!TEST_true(SSL_read_ex(clientquic, buf, 1, &numbytes)) || !TEST_size_t_eq(numbytes, 1) || !TEST_true(SSL_has_pending(clientquic)) || !TEST_int_eq(SSL_pending(clientquic), msglen - 1) || !TEST_true(SSL_read_ex(clientquic, buf + 1, sizeof(buf) - 1, &numbytes)) || !TEST_mem_eq(buf, numbytes + 1, msg, msglen)) goto end; } if (!TEST_true(SSL_export_keying_material(clientquic, scratch, sizeof(scratch), "test", 4, (unsigned char *)"ctx", 3, 1))) goto end; if (sess == NULL) { if (!TEST_false(SSL_session_reused(clientquic))) goto end; sess = SSL_get1_session(clientquic); if (!TEST_ptr(sess)) goto end; } else { if (!TEST_true(SSL_session_reused(clientquic))) goto end; } if (!TEST_true(qtest_shutdown(qtserv, clientquic))) goto end; if (sctx == NULL) { sctx = ossl_quic_tserver_get0_ssl_ctx(qtserv); if (!TEST_true(SSL_CTX_up_ref(sctx))) { sctx = NULL; goto end; } } ossl_quic_tserver_free(qtserv); qtserv = NULL; SSL_free(clientquic); clientquic = NULL; if (idx >= 2) break; } ret = 1; end: SSL_SESSION_free(sess); ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); SSL_CTX_free(sctx); return ret; } static int test_fin_only_blocking(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL_CTX *sctx = NULL; SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; const char *msg = "Hello World"; uint64_t sid; size_t numbytes; unsigned char buf[32]; int ret = 0; OSSL_TIME timer, timediff; if (!qtest_supports_blocking()) return TEST_skip("Blocking tests not supported in this build"); if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert, privkey, QTEST_FLAG_BLOCK, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost"))) goto end; if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto end; if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)) || !TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, strlen(msg), &numbytes)) || !TEST_size_t_eq(strlen(msg), numbytes)) goto end; ossl_quic_tserver_tick(qtserv); if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)) || !TEST_mem_eq(msg, strlen(msg), buf, numbytes)) goto end; if (!TEST_true(ossl_quic_tserver_conclude(qtserv, sid))) goto end; timer = ossl_time_now(); if (!TEST_false(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))) goto end; timediff = ossl_time_subtract(ossl_time_now(), timer); if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_ZERO_RETURN) || !TEST_uint64_t_le(ossl_time2ms(timediff), 20)) goto end; if (!TEST_true(qtest_shutdown(qtserv, clientquic))) goto end; ret = 1; end: ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); SSL_CTX_free(sctx); return ret; } static int test_ciphersuites(void) { SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *ssl; int testresult = 0; const STACK_OF(SSL_CIPHER) *ciphers = NULL; const SSL_CIPHER *cipher; int cipherids[] = { 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 }; size_t i, j; if (!TEST_ptr(ctx)) return 0; ssl = SSL_new(ctx); if (!TEST_ptr(ssl)) goto err; ciphers = SSL_get_ciphers(ssl); for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) { if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips) continue; cipher = sk_SSL_CIPHER_value(ciphers, j++); if (!TEST_ptr(cipher)) goto err; if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i])) goto err; } if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j)) goto err; testresult = 1; err: SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_cipher_find(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; struct { const unsigned char *cipherbytes; int ok; } testciphers[] = { { TLS13_AES_128_GCM_SHA256_BYTES, 1 }, { TLS13_AES_256_GCM_SHA384_BYTES, 1 }, { TLS13_CHACHA20_POLY1305_SHA256_BYTES, 1 }, { TLS13_AES_128_CCM_SHA256_BYTES, 0 }, { TLS13_AES_128_CCM_8_SHA256_BYTES, 0 } }; size_t i; int testresult = 0; if (!TEST_ptr(cctx)) goto err; clientquic = SSL_new(cctx); if (!TEST_ptr(clientquic)) goto err; for (i = 0; i < OSSL_NELEM(testciphers); i++) if (testciphers[i].ok) { if (!TEST_ptr(SSL_CIPHER_find(clientquic, testciphers[i].cipherbytes))) goto err; } else { if (!TEST_ptr_null(SSL_CIPHER_find(clientquic, testciphers[i].cipherbytes))) goto err; } testresult = 1; err: SSL_free(clientquic); SSL_CTX_free(cctx); return testresult; } static int test_version(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, 0, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION) || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1")) goto err; if (!TEST_true(SSL_is_quic(clientquic)) || !TEST_false(SSL_is_tls(clientquic)) || !TEST_false(SSL_is_dtls(clientquic))) goto err; testresult = 1; err: ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); return testresult; } #if defined(DO_SSL_TRACE_TEST) static void strip_line_ends(char *str) { size_t i; for (i = strlen(str); i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r'); i--); str[i] = '\0'; } static int compare_with_file(BIO *membio) { BIO *file = NULL, *newfile = NULL; char buf1[512], buf2[512]; char *reffile; int ret = 0; size_t i; #ifdef OPENSSL_NO_ZLIB reffile = test_mk_file_path(datadir, "ssltraceref.txt"); #else reffile = test_mk_file_path(datadir, "ssltraceref-zlib.txt"); #endif if (!TEST_ptr(reffile)) goto err; file = BIO_new_file(reffile, "rb"); if (!TEST_ptr(file)) goto err; newfile = BIO_new_file("ssltraceref-new.txt", "wb"); if (!TEST_ptr(newfile)) goto err; while (BIO_gets(membio, buf2, sizeof(buf2)) > 0) if (BIO_puts(newfile, buf2) <= 0) { TEST_error("Failed writing new file data"); goto err; } if (!TEST_int_ge(BIO_seek(membio, 0), 0)) goto err; while (BIO_gets(file, buf1, sizeof(buf1)) > 0) { if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) { TEST_error("Failed reading mem data"); goto err; } strip_line_ends(buf1); strip_line_ends(buf2); if (strlen(buf1) != strlen(buf2)) { TEST_error("Actual and ref line data length mismatch"); TEST_info("%s", buf1); TEST_info("%s", buf2); goto err; } for (i = 0; i < strlen(buf1); i++) { if (buf1[i] == '?') buf2[i] = '?'; } if (!TEST_str_eq(buf1, buf2)) goto err; } if (!TEST_true(BIO_eof(file)) || !TEST_true(BIO_eof(membio))) goto err; ret = 1; err: OPENSSL_free(reffile); BIO_free(file); BIO_free(newfile); return ret; } static int test_ssl_trace(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; BIO *bio = BIO_new(BIO_s_mem()); if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) goto err; if (!TEST_ptr(cctx) || !TEST_ptr(bio) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, QTEST_FLAG_FAKE_TIME, &qtserv, &clientquic, NULL, NULL))) goto err; SSL_set_msg_callback(clientquic, SSL_trace); SSL_set_msg_callback_arg(clientquic, bio); if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; if (!TEST_true(compare_with_file(bio))) goto err; testresult = 1; err: ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); BIO_free(bio); return testresult; } #endif static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers) { size_t i; for (i = 0; i < (size_t)sk_SSL_CIPHER_num(ciphers); ++i) { const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); switch (SSL_CIPHER_get_id(cipher)) { case TLS1_3_CK_AES_128_GCM_SHA256: case TLS1_3_CK_AES_256_GCM_SHA384: case TLS1_3_CK_CHACHA20_POLY1305_SHA256: break; default: TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher)); return 0; } } return 1; } static int test_quic_forbidden_apis_ctx(void) { int testresult = 0; SSL_CTX *ctx = NULL; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) goto err; #ifndef OPENSSL_NO_SRTP if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx, "SRTP_AEAD_AES_128_GCM"))) goto err; #endif #define QUIC_CIPHERSUITES \ "TLS_AES_128_GCM_SHA256:" \ "TLS_AES_256_GCM_SHA384:" \ "TLS_CHACHA20_POLY1305_SHA256" #define NON_QUIC_CIPHERSUITES \ "TLS_AES_128_CCM_SHA256:" \ "TLS_AES_256_CCM_SHA384:" \ "TLS_AES_128_CCM_8_SHA256" if (!TEST_true(SSL_CTX_set_ciphersuites(ctx, QUIC_CIPHERSUITES ":" NON_QUIC_CIPHERSUITES))) goto err; testresult = 1; err: SSL_CTX_free(ctx); return testresult; } static int test_quic_forbidden_apis(void) { int testresult = 0; SSL_CTX *ctx = NULL; SSL *ssl = NULL; STACK_OF(SSL_CIPHER) *ciphers = NULL; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) goto err; if (!TEST_ptr(ssl = SSL_new(ctx))) goto err; #ifndef OPENSSL_NO_SRTP if (!TEST_true(SSL_set_tlsext_use_srtp(ssl, "SRTP_AEAD_AES_128_GCM"))) goto err; #endif if (!TEST_true(SSL_set_ciphersuites(ssl, QUIC_CIPHERSUITES ":" NON_QUIC_CIPHERSUITES))) goto err; if (!TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)) || !TEST_true(ensure_valid_ciphers(ciphers))) goto err; testresult = 1; err: sk_SSL_CIPHER_free(ciphers); SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_quic_forbidden_options(void) { int testresult = 0; SSL_CTX *ctx = NULL; SSL *ssl = NULL; char buf[16]; size_t len; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) goto err; SSL_CTX_set_options(ctx, UINT64_MAX); if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX)) goto err; SSL_CTX_set_read_ahead(ctx, 1); SSL_CTX_set_max_early_data(ctx, 1); SSL_CTX_set_recv_max_early_data(ctx, 1); SSL_CTX_set_quiet_shutdown(ctx, 1); if (!TEST_ptr(ssl = SSL_new(ctx))) goto err; if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS)) goto err; SSL_set_options(ssl, UINT64_MAX); if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS)) goto err; SSL_clear_options(ssl, UINT64_MAX); if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0)) goto err; if (!TEST_false(SSL_get_read_ahead(ssl))) goto err; SSL_set_read_ahead(ssl, 1); if (!TEST_false(SSL_get_read_ahead(ssl))) goto err; if (!TEST_true(SSL_set_block_padding(ssl, 0)) || !TEST_true(SSL_set_block_padding(ssl, 1)) || !TEST_false(SSL_set_block_padding(ssl, 2))) goto err; if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_DISABLED)) || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_512))) goto err; if (!TEST_false(SSL_set_recv_max_early_data(ssl, 1)) || !TEST_false(SSL_set_max_early_data(ssl, 1))) goto err; if (!TEST_false(SSL_read_early_data(ssl, buf, sizeof(buf), &len)) || !TEST_false(SSL_write_early_data(ssl, buf, sizeof(buf), &len))) goto err; if (!TEST_true(SSL_alloc_buffers(ssl)) || !TEST_false(SSL_free_buffers(ssl))) goto err; if (!TEST_false(SSL_set_max_send_fragment(ssl, 2)) || !TEST_false(SSL_set_split_send_fragment(ssl, 2)) || !TEST_false(SSL_set_max_pipelines(ssl, 2))) goto err; if (!TEST_false(SSL_stateless(ssl))) goto err; if (!TEST_false(SSL_get_quiet_shutdown(ssl))) goto err; if (!TEST_ptr_null(SSL_dup(ssl))) goto err; if (!TEST_false(SSL_clear(ssl))) goto err; testresult = 1; err: SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_quic_set_fd(int idx) { int testresult = 0; SSL_CTX *ctx = NULL; SSL *ssl = NULL; int fd = -1, resfd = -1; BIO *bio = NULL; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) goto err; if (!TEST_ptr(ssl = SSL_new(ctx))) goto err; if (!TEST_int_ge(fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0), 0)) goto err; if (idx == 0) { if (!TEST_true(SSL_set_fd(ssl, fd))) goto err; if (!TEST_ptr(bio = SSL_get_rbio(ssl))) goto err; if (!TEST_ptr_eq(bio, SSL_get_wbio(ssl))) goto err; } else if (idx == 1) { if (!TEST_true(SSL_set_rfd(ssl, fd))) goto err; if (!TEST_ptr(bio = SSL_get_rbio(ssl))) goto err; if (!TEST_ptr_null(SSL_get_wbio(ssl))) goto err; } else { if (!TEST_true(SSL_set_wfd(ssl, fd))) goto err; if (!TEST_ptr(bio = SSL_get_wbio(ssl))) goto err; if (!TEST_ptr_null(SSL_get_rbio(ssl))) goto err; } if (!TEST_int_eq(BIO_method_type(bio), BIO_TYPE_DGRAM)) goto err; if (!TEST_true(BIO_get_fd(bio, &resfd)) || !TEST_int_eq(resfd, fd)) goto err; testresult = 1; err: SSL_free(ssl); SSL_CTX_free(ctx); if (fd >= 0) BIO_closesocket(fd); return testresult; } #define MAXLOOPS 1000 static int test_bio_ssl(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL, *stream = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; BIO *cbio = NULL, *strbio = NULL, *thisbio; const char *msg = "Hello world"; int abortctr = 0, err, clienterr = 0, servererr = 0, retc = 0, rets = 0; size_t written, readbytes, msglen; int sid = 0, i; unsigned char buf[80]; if (!TEST_ptr(cctx)) goto err; cbio = BIO_new_ssl(cctx, 1); if (!TEST_ptr(cbio)) goto err; if (!TEST_int_eq(BIO_get_ssl(cbio, &clientquic), 1)) goto err; if (!TEST_true(qtest_create_quic_objects(libctx, NULL, NULL, cert, privkey, 0, &qtserv, &clientquic, NULL, NULL))) goto err; msglen = strlen(msg); do { err = BIO_FLAGS_WRITE; while (!clienterr && !retc && err == BIO_FLAGS_WRITE) { retc = BIO_write_ex(cbio, msg, msglen, &written); if (!retc) { if (BIO_should_retry(cbio)) err = BIO_retry_type(cbio); else err = 0; } } if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) { TEST_info("BIO_write_ex() failed %d, %d", retc, err); TEST_openssl_errors(); clienterr = 1; } if (!servererr && rets <= 0) { ossl_quic_tserver_tick(qtserv); servererr = ossl_quic_tserver_is_term_any(qtserv); if (!servererr) rets = ossl_quic_tserver_is_handshake_confirmed(qtserv); } if (clienterr && servererr) goto err; if (++abortctr == MAXLOOPS) { TEST_info("No progress made"); goto err; } } while ((!retc && !clienterr) || (rets <= 0 && !servererr)); for (i = 0, thisbio = cbio; i < 2; i++) { if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(msg, msglen, buf, readbytes)) goto err; if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, msglen, &written))) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(msg, msglen, buf, readbytes)) goto err; if (i == 1) break; if (!TEST_true(SSL_set_mode(clientquic, 0))) goto err; sid = 4; stream = SSL_new_stream(clientquic, 0); if (!TEST_ptr(stream)) goto err; if (!TEST_true(SSL_set_mode(stream, 0))) goto err; thisbio = strbio = BIO_new(BIO_f_ssl()); if (!TEST_ptr(strbio)) goto err; if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1)) goto err; stream = NULL; if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written))) goto err; ossl_quic_tserver_tick(qtserv); } testresult = 1; err: BIO_free_all(cbio); BIO_free_all(strbio); SSL_free(stream); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } #define BACK_PRESSURE_NUM_LOOPS 10000 static int test_back_pressure(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; unsigned char *msg = NULL; const size_t msglen = 1024; unsigned char buf[64]; size_t readbytes, written; int i; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, 0, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; msg = OPENSSL_malloc(msglen); if (!TEST_ptr(msg)) goto err; if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1)) goto err; for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) { if (!SSL_write_ex(clientquic, msg, msglen, &written)) { if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE) break; TEST_error("Unexpected client failure"); goto err; } ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf), &readbytes))) goto err; } if (i == BACK_PRESSURE_NUM_LOOPS) { TEST_error("No back pressure seen"); goto err; } testresult = 1; err: SSL_free(clientquic); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); OPENSSL_free(msg); return testresult; } static int dgram_ctr = 0; static void dgram_cb(int write_p, int version, int content_type, const void *buf, size_t msglen, SSL *ssl, void *arg) { if (!write_p) return; if (content_type != SSL3_RT_QUIC_DATAGRAM) return; dgram_ctr++; } static int test_multiple_dgrams(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; unsigned char *buf; const size_t buflen = 1400; size_t written; buf = OPENSSL_zalloc(buflen); if (!TEST_ptr(cctx) || !TEST_ptr(buf) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, 0, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; dgram_ctr = 0; SSL_set_msg_callback(clientquic, dgram_cb); if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written)) || !TEST_size_t_eq(written, buflen) || !TEST_int_eq(dgram_ctr, 2)) goto err; testresult = 1; err: OPENSSL_free(buf); SSL_free(clientquic); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg) { int idx = SSL_get_ex_data_X509_STORE_CTX_idx(); SSL *ssl; const int *allow = (int *)arg; if (idx < 0 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL) return 0; if (*allow == 0) return SSL_set_retry_verify(ssl); return 1; } static int test_non_io_retry(int idx) { SSL_CTX *cctx; SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; int flags = 0, allow = 0; if (idx >= 1 && !qtest_supports_blocking()) return TEST_skip("Blocking tests not supported in this build"); cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); if (!TEST_ptr(cctx)) goto err; SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &allow); flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0; if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, flags, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic, SSL_ERROR_WANT_RETRY_VERIFY)) || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY)) goto err; allow = 1; if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; testresult = 1; err: SSL_free(clientquic); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); return testresult; } static int use_session_cb_cnt = 0; static int find_session_cb_cnt = 0; static const char *pskid = "Identity"; static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL; static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, size_t *idlen, SSL_SESSION **sess) { use_session_cb_cnt++; if (clientpsk == NULL) return 0; SSL_SESSION_up_ref(clientpsk); *sess = clientpsk; *id = (const unsigned char *)pskid; *idlen = strlen(pskid); return 1; } static int find_session_cb(SSL *ssl, const unsigned char *identity, size_t identity_len, SSL_SESSION **sess) { find_session_cb_cnt++; if (serverpsk == NULL) return 0; if (strlen(pskid) != identity_len || strncmp(pskid, (const char *)identity, identity_len) != 0) return 0; SSL_SESSION_up_ref(serverpsk); *sess = serverpsk; return 1; } static int test_quic_psk(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL, NULL, 0, &qtserv, &clientquic, NULL, NULL))) goto end; SSL_set_psk_use_session_callback(clientquic, use_session_cb); ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb); use_session_cb_cnt = 0; find_session_cb_cnt = 0; clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH); if (!TEST_ptr(clientpsk)) goto end; SSL_SESSION_up_ref(clientpsk); if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)) || !TEST_int_eq(1, find_session_cb_cnt) || !TEST_int_eq(1, use_session_cb_cnt) || !TEST_true(SSL_session_reused(clientquic))) goto end; testresult = 1; end: SSL_free(clientquic); ossl_quic_tserver_free(qtserv); SSL_CTX_free(cctx); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; return testresult; } static int test_client_auth(int idx) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; unsigned char buf[20]; static char *msg = "A test message"; size_t msglen = strlen(msg); size_t numbytes = 0; if (!TEST_ptr(cctx) || !TEST_ptr(sctx)) goto err; SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE, NULL); if (!TEST_true(SSL_CTX_load_verify_file(sctx, cauthca))) goto err; if (idx > 0 && (!TEST_true(SSL_CTX_use_certificate_chain_file(cctx, ccert)) || !TEST_true(SSL_CTX_use_PrivateKey_file(cctx, cprivkey, SSL_FILETYPE_PEM)))) goto err; if (!TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert, privkey, 0, &qtserv, &clientquic, NULL, NULL))) goto err; if (idx > 1) { if (!TEST_true(ssl_ctx_add_large_cert_chain(libctx, cctx, ccert)) || !TEST_true(ssl_ctx_add_large_cert_chain(libctx, sctx, cert))) goto err; } if (idx == 0) { if (!TEST_false(qtest_create_quic_connection(qtserv, clientquic))) goto err; testresult = 1; goto err; } if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes)) || !TEST_size_t_eq(numbytes, msglen)) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(ossl_quic_tserver_write(qtserv, 0, (unsigned char *)msg, msglen, &numbytes))) goto err; ossl_quic_tserver_tick(qtserv); SSL_handle_events(clientquic); if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)) || !TEST_size_t_eq(numbytes, msglen) || !TEST_mem_eq(buf, numbytes, msg, msglen)) goto err; if (!TEST_true(qtest_shutdown(qtserv, clientquic))) goto err; testresult = 1; err: SSL_free(clientquic); ossl_quic_tserver_free(qtserv); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_alpn(int idx) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; int ret; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) goto err; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, QTEST_FLAG_FAKE_TIME, &qtserv, &clientquic, NULL, NULL))) goto err; if (idx == 0) { if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0))) goto err; } ret = SSL_connect(clientquic); if (!TEST_int_le(ret, 0)) goto err; if (idx == 0) { if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL)) goto err; } else { if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ) || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; } testresult = 1; err: ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); return testresult; } static int test_get_shutdown(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, QTEST_FLAG_FAKE_TIME, &qtserv, &clientquic, NULL, NULL)) || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; if (!TEST_int_eq(SSL_get_shutdown(clientquic), 0)) goto err; if (!TEST_int_eq(SSL_shutdown(clientquic), 0)) goto err; if (!TEST_int_eq(SSL_get_shutdown(clientquic), SSL_SENT_SHUTDOWN)) goto err; do { ossl_quic_tserver_tick(qtserv); qtest_add_time(100); } while (SSL_shutdown(clientquic) == 0); if (!TEST_int_eq(SSL_get_shutdown(clientquic), SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) goto err; testresult = 1; err: ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); return testresult; } #define MAX_LOOPS 2000 static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf, size_t buflen, size_t *readbytes, QUIC_TSERVER *qtserv) { int abortctr; for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) { if (*stream == NULL) { SSL_handle_events(clientquic); *stream = SSL_accept_stream(clientquic, 0); } if (*stream != NULL) { if (SSL_read_ex(*stream, buf, buflen, readbytes)) return 1; if (!TEST_int_eq(SSL_get_error(*stream, 0), SSL_ERROR_WANT_READ)) return 0; } ossl_quic_tserver_tick(qtserv); qtest_add_time(1); qtest_wait_for_timeout(clientquic, qtserv); } TEST_error("No progress made"); return 0; } static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid, void *buf, size_t buflen, size_t *readbytes, SSL *clientquic) { int abortctr; for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) { if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes) && *readbytes > 1) return 1; ossl_quic_tserver_tick(qtserv); SSL_handle_events(clientquic); qtest_add_time(1); qtest_wait_for_timeout(clientquic, qtserv); } TEST_error("No progress made"); return 0; } static int test_noisy_dgram(int idx) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL, *stream[2] = { NULL, NULL }; QUIC_TSERVER *qtserv = NULL; int testresult = 0; uint64_t sid = 0; char *msg = "Hello world!"; size_t msglen = strlen(msg), written, readbytes, i, j; unsigned char buf[80]; int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME; QTEST_FAULT *fault = NULL; if (idx == 1) flags |= QTEST_FLAG_PACKET_SPLIT; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, flags, &qtserv, &clientquic, &fault, NULL))) goto err; if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; if (!TEST_true(SSL_set_incoming_stream_policy(clientquic, SSL_INCOMING_STREAM_POLICY_ACCEPT, 0)) || !TEST_true(SSL_set_default_stream_mode(clientquic, SSL_DEFAULT_STREAM_MODE_NONE))) goto err; for (j = 0; j < 2; j++) { if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid))) goto err; ossl_quic_tserver_tick(qtserv); qtest_add_time(1); for (i = 0; i < 20; i++) { if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, msglen, &written)) || !TEST_size_t_eq(msglen, written)) goto err; ossl_quic_tserver_tick(qtserv); qtest_add_time(1); if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf, sizeof(buf), &readbytes, qtserv)) || !TEST_mem_eq(msg, msglen, buf, readbytes)) goto err; } for (i = 0; i < 20; i++) { if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg, msglen, &written)) || !TEST_size_t_eq(msglen, written)) goto err; ossl_quic_tserver_tick(qtserv); qtest_add_time(1); if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf), &readbytes, clientquic)) || !TEST_mem_eq(msg, msglen, buf, readbytes)) goto err; } } testresult = 1; err: ossl_quic_tserver_free(qtserv); SSL_free(stream[0]); SSL_free(stream[1]); SSL_free(clientquic); SSL_CTX_free(cctx); qtest_fault_free(fault); return testresult; } #define TEST_TRANSFER_DATA_SIZE (2*1024*1024) #define TEST_SINGLE_WRITE_SIZE (16*1024) #define TEST_BW_LIMIT 1000 static int test_bw_limit(void) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); SSL *clientquic = NULL; QUIC_TSERVER *qtserv = NULL; int testresult = 0; unsigned char *msg = NULL, *recvbuf = NULL; size_t sendlen = TEST_TRANSFER_DATA_SIZE; size_t recvlen = TEST_TRANSFER_DATA_SIZE; size_t written, readbytes; int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME; QTEST_FAULT *fault = NULL; uint64_t real_bw; if (!TEST_ptr(cctx) || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, flags, &qtserv, &clientquic, &fault, NULL))) goto err; if (!TEST_ptr(msg = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE)) || !TEST_ptr(recvbuf = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE))) goto err; if (!TEST_true(qtest_fault_set_bw_limit(fault, 1000, 1000, 0))) goto err; if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) goto err; qtest_start_stopwatch(); while (recvlen > 0) { qtest_add_time(1); if (sendlen > 0) { if (!SSL_write_ex(clientquic, msg, sendlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE : sendlen, &written)) { TEST_info("Retrying to send: %llu", (unsigned long long) sendlen); if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_WANT_WRITE)) goto err; } else { sendlen -= written; TEST_info("Remaining to send: %llu", (unsigned long long) sendlen); } } else { SSL_handle_events(clientquic); } if (ossl_quic_tserver_read(qtserv, 0, recvbuf, recvlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE : recvlen, &readbytes) && readbytes > 1) { recvlen -= readbytes; TEST_info("Remaining to recv: %llu", (unsigned long long) recvlen); } else { TEST_info("No progress on recv: %llu", (unsigned long long) recvlen); } ossl_quic_tserver_tick(qtserv); } real_bw = TEST_TRANSFER_DATA_SIZE / qtest_get_stopwatch_time(); TEST_info("BW limit: %d Bytes/ms Real bandwidth reached: %llu Bytes/ms", TEST_BW_LIMIT, (unsigned long long)real_bw); if (!TEST_uint64_t_lt(real_bw, TEST_BW_LIMIT)) goto err; testresult = 1; err: OPENSSL_free(msg); OPENSSL_free(recvbuf); ossl_quic_tserver_free(qtserv); SSL_free(clientquic); SSL_CTX_free(cctx); qtest_fault_free(fault); return testresult; } enum { TPARAM_OP_DUP, TPARAM_OP_DROP, TPARAM_OP_INJECT, TPARAM_OP_INJECT_TWICE, TPARAM_OP_INJECT_RAW, TPARAM_OP_DROP_INJECT, TPARAM_OP_MUTATE }; #define TPARAM_CHECK_DUP(name, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_DUP, (reason) }, #define TPARAM_CHECK_DROP(name, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_DROP, (reason) }, #define TPARAM_CHECK_INJECT(name, buf, buf_len, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_INJECT, (reason), \ (buf), (buf_len) }, #define TPARAM_CHECK_INJECT_A(name, buf, reason) \ TPARAM_CHECK_INJECT(name, buf, sizeof(buf), reason) #define TPARAM_CHECK_DROP_INJECT(name, buf, buf_len, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_DROP_INJECT, (reason), \ (buf), (buf_len) }, #define TPARAM_CHECK_DROP_INJECT_A(name, buf, reason) \ TPARAM_CHECK_DROP_INJECT(name, buf, sizeof(buf), reason) #define TPARAM_CHECK_INJECT_TWICE(name, buf, buf_len, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_INJECT_TWICE, (reason), \ (buf), (buf_len) }, #define TPARAM_CHECK_INJECT_TWICE_A(name, buf, reason) \ TPARAM_CHECK_INJECT_TWICE(name, buf, sizeof(buf), reason) #define TPARAM_CHECK_INJECT_RAW(buf, buf_len, reason) \ { 0, TPARAM_OP_INJECT_RAW, (reason), \ (buf), (buf_len) }, #define TPARAM_CHECK_INJECT_RAW_A(buf, reason) \ TPARAM_CHECK_INJECT_RAW(buf, sizeof(buf), reason) #define TPARAM_CHECK_MUTATE(name, reason) \ { QUIC_TPARAM_##name, TPARAM_OP_MUTATE, (reason) }, #define TPARAM_CHECK_INT(name, reason) \ TPARAM_CHECK_DROP_INJECT(name, NULL, 0, reason) \ TPARAM_CHECK_DROP_INJECT_A(name, bogus_int, reason) \ TPARAM_CHECK_DROP_INJECT_A(name, int_with_trailer, reason) struct tparam_test { uint64_t id; int op; const char *expect_fail; const void *buf; size_t buf_len; }; static const unsigned char retry_scid_1[8] = { 0 }; static const unsigned char disable_active_migration_1[] = { 0x00 }; static const unsigned char malformed_stateless_reset_token_1[] = { 0x02, 0xff }; static const unsigned char malformed_stateless_reset_token_2[] = { 0x01 }; static const unsigned char malformed_stateless_reset_token_3[15] = { 0 }; static const unsigned char malformed_stateless_reset_token_4[17] = { 0 }; static const unsigned char malformed_preferred_addr_1[] = { 0x0d, 0xff }; static const unsigned char malformed_preferred_addr_2[42] = { 0x0d, 0x28, }; static const unsigned char malformed_preferred_addr_3[64] = { 0x0d, 0x3e, }; static const unsigned char malformed_preferred_addr_4[] = { 0x0d, 0x29, 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, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; static const unsigned char malformed_unknown_1[] = { 0xff }; static const unsigned char malformed_unknown_2[] = { 0x55, 0x55, }; static const unsigned char malformed_unknown_3[] = { 0x55, 0x55, 0x01, }; static const unsigned char ack_delay_exp[] = { 0x03 }; static const unsigned char stateless_reset_token[16] = { 0x42 }; static const unsigned char preferred_addr[] = { 0x44, 0x44, 0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x02, 0xAA, 0xBB, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, }; static const unsigned char long_cid[21] = { 0x42 }; static const unsigned char excess_ack_delay_exp[] = { 0x15, }; static const unsigned char excess_max_ack_delay[] = { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, }; static const unsigned char excess_initial_max_streams[] = { 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }; static const unsigned char undersize_udp_payload_size[] = { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xaf, }; static const unsigned char undersize_active_conn_id_limit[] = { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }; static const unsigned char bogus_int[9] = { 0 }; static const unsigned char int_with_trailer[2] = { 0x01 }; #define QUIC_TPARAM_UNKNOWN_1 0xf1f1 static const struct tparam_test tparam_tests[] = { TPARAM_CHECK_DUP(ORIG_DCID, "ORIG_DCID appears multiple times") TPARAM_CHECK_DUP(INITIAL_SCID, "INITIAL_SCID appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_DATA, "INITIAL_MAX_DATA appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_UNI, "INITIAL_MAX_STREAM_DATA_UNI appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_BIDI, "INITIAL_MAX_STREAMS_BIDI appears multiple times") TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_UNI, "INITIAL_MAX_STREAMS_UNI appears multiple times") TPARAM_CHECK_DUP(MAX_IDLE_TIMEOUT, "MAX_IDLE_TIMEOUT appears multiple times") TPARAM_CHECK_DUP(MAX_UDP_PAYLOAD_SIZE, "MAX_UDP_PAYLOAD_SIZE appears multiple times") TPARAM_CHECK_DUP(ACTIVE_CONN_ID_LIMIT, "ACTIVE_CONN_ID_LIMIT appears multiple times") TPARAM_CHECK_DUP(DISABLE_ACTIVE_MIGRATION, "DISABLE_ACTIVE_MIGRATION appears multiple times") TPARAM_CHECK_DROP(INITIAL_SCID, "INITIAL_SCID was not sent but is required") TPARAM_CHECK_DROP(ORIG_DCID, "ORIG_DCID was not sent but is required") TPARAM_CHECK_INJECT_A(RETRY_SCID, retry_scid_1, "RETRY_SCID sent when not performing a retry") TPARAM_CHECK_DROP_INJECT_A(DISABLE_ACTIVE_MIGRATION, disable_active_migration_1, "DISABLE_ACTIVE_MIGRATION is malformed") TPARAM_CHECK_INJECT(UNKNOWN_1, NULL, 0, NULL) TPARAM_CHECK_INJECT_RAW_A(malformed_stateless_reset_token_1, "STATELESS_RESET_TOKEN is malformed") TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, malformed_stateless_reset_token_2, "STATELESS_RESET_TOKEN is malformed") TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, malformed_stateless_reset_token_3, "STATELESS_RESET_TOKEN is malformed") TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, malformed_stateless_reset_token_4, "STATELESS_RESET_TOKEN is malformed") TPARAM_CHECK_INJECT(STATELESS_RESET_TOKEN, NULL, 0, "STATELESS_RESET_TOKEN is malformed") TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_1, "PREFERRED_ADDR is malformed") TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_2, "PREFERRED_ADDR is malformed") TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_3, "PREFERRED_ADDR is malformed") TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_4, "PREFERRED_ADDR is malformed") TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_1, "bad transport parameter") TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_2, "bad transport parameter") TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_3, "bad transport parameter") TPARAM_CHECK_INJECT_A(ACK_DELAY_EXP, excess_ack_delay_exp, "ACK_DELAY_EXP is malformed") TPARAM_CHECK_INJECT_A(MAX_ACK_DELAY, excess_max_ack_delay, "MAX_ACK_DELAY is malformed") TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_BIDI, excess_initial_max_streams, "INITIAL_MAX_STREAMS_BIDI is malformed") TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_UNI, excess_initial_max_streams, "INITIAL_MAX_STREAMS_UNI is malformed") TPARAM_CHECK_DROP_INJECT_A(MAX_UDP_PAYLOAD_SIZE, undersize_udp_payload_size, "MAX_UDP_PAYLOAD_SIZE is malformed") TPARAM_CHECK_DROP_INJECT_A(ACTIVE_CONN_ID_LIMIT, undersize_active_conn_id_limit, "ACTIVE_CONN_ID_LIMIT is malformed") TPARAM_CHECK_INJECT_TWICE_A(ACK_DELAY_EXP, ack_delay_exp, "ACK_DELAY_EXP appears multiple times") TPARAM_CHECK_INJECT_TWICE_A(MAX_ACK_DELAY, ack_delay_exp, "MAX_ACK_DELAY appears multiple times") TPARAM_CHECK_INJECT_TWICE_A(STATELESS_RESET_TOKEN, stateless_reset_token, "STATELESS_RESET_TOKEN appears multiple times") TPARAM_CHECK_INJECT_TWICE_A(PREFERRED_ADDR, preferred_addr, "PREFERRED_ADDR appears multiple times") TPARAM_CHECK_MUTATE(ORIG_DCID, "ORIG_DCID does not match expected value") TPARAM_CHECK_MUTATE(INITIAL_SCID, "INITIAL_SCID does not match expected value") TPARAM_CHECK_DROP_INJECT_A(ORIG_DCID, long_cid, "ORIG_DCID is malformed") TPARAM_CHECK_DROP_INJECT_A(INITIAL_SCID, long_cid, "INITIAL_SCID is malformed") TPARAM_CHECK_INT(INITIAL_MAX_DATA, "INITIAL_MAX_DATA is malformed") TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL is malformed") TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE is malformed") TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_UNI, "INITIAL_MAX_STREAM_DATA_UNI is malformed") TPARAM_CHECK_INT(ACK_DELAY_EXP, "ACK_DELAY_EXP is malformed") TPARAM_CHECK_INT(MAX_ACK_DELAY, "MAX_ACK_DELAY is malformed") TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_BIDI, "INITIAL_MAX_STREAMS_BIDI is malformed") TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_UNI, "INITIAL_MAX_STREAMS_UNI is malformed") TPARAM_CHECK_INT(MAX_IDLE_TIMEOUT, "MAX_IDLE_TIMEOUT is malformed") TPARAM_CHECK_INT(MAX_UDP_PAYLOAD_SIZE, "MAX_UDP_PAYLOAD_SIZE is malformed") TPARAM_CHECK_INT(ACTIVE_CONN_ID_LIMIT, "ACTIVE_CONN_ID_LIMIT is malformed") }; struct tparam_ctx { const struct tparam_test *t; }; static int tparam_handle(struct tparam_ctx *ctx, uint64_t id, unsigned char *data, size_t data_len, WPACKET *wpkt) { const struct tparam_test *t = ctx->t; switch (t->op) { case TPARAM_OP_DUP: if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, data, data_len))) return 0; if (id == t->id && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, data, data_len))) return 0; return 1; case TPARAM_OP_DROP: case TPARAM_OP_DROP_INJECT: if (id != t->id && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, data, data_len))) return 0; return 1; case TPARAM_OP_INJECT: case TPARAM_OP_INJECT_TWICE: case TPARAM_OP_INJECT_RAW: if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, data, data_len))) return 0; return 1; case TPARAM_OP_MUTATE: if (id == t->id) { if (!TEST_size_t_gt(data_len, 0)) return 0; data[0] ^= 1; } if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, data, data_len))) return 0; if (id == t->id) data[0] ^= 1; return 1; default: return 0; } } static int tparam_on_enc_ext(QTEST_FAULT *qtf, QTEST_ENCRYPTED_EXTENSIONS *ee, size_t ee_len, void *arg) { int rc = 0; struct tparam_ctx *ctx = arg; PACKET pkt = {0}; WPACKET wpkt; int have_wpkt = 0; BUF_MEM *old_bufm = NULL, *new_bufm = NULL; unsigned char *tp_p; size_t tp_len, written, old_len, eb_len; uint64_t id; if (!TEST_ptr(old_bufm = BUF_MEM_new())) goto err; if (!TEST_true(qtest_fault_delete_extension(qtf, TLSEXT_TYPE_quic_transport_parameters, ee->extensions, &ee->extensionslen, old_bufm))) goto err; if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char *)old_bufm->data, old_bufm->length)) || !TEST_ptr(new_bufm = BUF_MEM_new()) || !TEST_true(WPACKET_init(&wpkt, new_bufm))) goto err; have_wpkt = 1; if (!TEST_true(WPACKET_put_bytes_u16(&wpkt, TLSEXT_TYPE_quic_transport_parameters)) || !TEST_true(WPACKET_start_sub_packet_u16(&wpkt))) goto err; for (; PACKET_remaining(&pkt) > 0; ) { tp_p = (unsigned char *)ossl_quic_wire_decode_transport_param_bytes(&pkt, &id, &tp_len); if (!TEST_ptr(tp_p)) { TEST_mem_eq(PACKET_data(&pkt), PACKET_remaining(&pkt), NULL, 0); goto err; } if (!TEST_true(tparam_handle(ctx, id, tp_p, tp_len, &wpkt))) goto err; } if (ctx->t->op == TPARAM_OP_INJECT || ctx->t->op == TPARAM_OP_DROP_INJECT || ctx->t->op == TPARAM_OP_INJECT_TWICE) { if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id, ctx->t->buf, ctx->t->buf_len))) goto err; if (ctx->t->op == TPARAM_OP_INJECT_TWICE && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id, ctx->t->buf, ctx->t->buf_len))) goto err; } else if (ctx->t->op == TPARAM_OP_INJECT_RAW) { if (!TEST_true(WPACKET_memcpy(&wpkt, ctx->t->buf, ctx->t->buf_len))) goto err; } if (!TEST_true(WPACKET_close(&wpkt))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; WPACKET_finish(&wpkt); have_wpkt = 0; old_len = ee->extensionslen; if (!qtest_fault_resize_message(qtf, ee->extensionslen + written)) goto err; memcpy(ee->extensions + old_len, new_bufm->data, written); eb_len = (((uint16_t)ee->extensions[0]) << 8) + (uint16_t)ee->extensions[1]; eb_len += written; ee->extensions[0] = (unsigned char)((eb_len >> 8) & 0xFF); ee->extensions[1] = (unsigned char)( eb_len & 0xFF); rc = 1; err: if (have_wpkt) WPACKET_cleanup(&wpkt); BUF_MEM_free(old_bufm); BUF_MEM_free(new_bufm); return rc; } static int test_tparam(int idx) { int testresult = 0; SSL_CTX *c_ctx = NULL; SSL *c_ssl = NULL; QUIC_TSERVER *s = NULL; QTEST_FAULT *qtf = NULL; struct tparam_ctx ctx = {0}; ctx.t = &tparam_tests[idx]; if (!TEST_ptr(c_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) goto err; if (!TEST_true(qtest_create_quic_objects(libctx, c_ctx, NULL, cert, privkey, 0, &s, &c_ssl, &qtf, NULL))) goto err; if (!TEST_true(qtest_fault_set_hand_enc_ext_listener(qtf, tparam_on_enc_ext, &ctx))) goto err; if (!TEST_true(qtest_create_quic_connection_ex(s, c_ssl, ctx.t->expect_fail != NULL))) goto err; if (ctx.t->expect_fail != NULL) { SSL_CONN_CLOSE_INFO info = {0}; if (!TEST_true(SSL_get_conn_close_info(c_ssl, &info, sizeof(info)))) goto err; if (!TEST_true((info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0) || !TEST_uint64_t_eq(info.error_code, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR) || !TEST_ptr(strstr(info.reason, ctx.t->expect_fail))) { TEST_error("expected connection closure information mismatch" " during TPARAM test: flags=%llu ec=%llu reason='%s'", (unsigned long long)info.flags, (unsigned long long)info.error_code, info.reason); goto err; } } testresult = 1; err: if (!testresult) { if (ctx.t->expect_fail != NULL) TEST_info("failed during test for id=%llu, op=%d, bl=%zu, " "expected failure='%s'", (unsigned long long)ctx.t->id, ctx.t->op, ctx.t->buf_len, ctx.t->expect_fail); else TEST_info("failed during test for id=%llu, op=%d, bl=%zu", (unsigned long long)ctx.t->id, ctx.t->op, ctx.t->buf_len); } ossl_quic_tserver_free(s); SSL_free(c_ssl); SSL_CTX_free(c_ctx); qtest_fault_free(qtf); return testresult; } OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n") int setup_tests(void) { char *modulename; char *configfile; libctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(libctx)) return 0; defctxnull = OSSL_PROVIDER_load(NULL, "null"); if (!TEST_false(OSSL_PROVIDER_available(NULL, "default")) || !TEST_false(OSSL_PROVIDER_available(NULL, "fips"))) goto err; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); goto err; } if (!TEST_ptr(modulename = test_get_argument(0)) || !TEST_ptr(configfile = test_get_argument(1)) || !TEST_ptr(certsdir = test_get_argument(2)) || !TEST_ptr(datadir = test_get_argument(3))) goto err; if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile))) goto err; if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename))) goto err; if (strcmp(modulename, "default") != 0 && !TEST_false(OSSL_PROVIDER_available(libctx, "default"))) goto err; if (strcmp(modulename, "fips") == 0) is_fips = 1; cert = test_mk_file_path(certsdir, "servercert.pem"); if (cert == NULL) goto err; ccert = test_mk_file_path(certsdir, "ee-client-chain.pem"); if (ccert == NULL) goto err; cauthca = test_mk_file_path(certsdir, "root-cert.pem"); if (cauthca == NULL) goto err; privkey = test_mk_file_path(certsdir, "serverkey.pem"); if (privkey == NULL) goto err; cprivkey = test_mk_file_path(certsdir, "ee-key.pem"); if (privkey == NULL) goto err; ADD_ALL_TESTS(test_quic_write_read, 3); ADD_TEST(test_fin_only_blocking); ADD_TEST(test_ciphersuites); ADD_TEST(test_cipher_find); ADD_TEST(test_version); #if defined(DO_SSL_TRACE_TEST) ADD_TEST(test_ssl_trace); #endif ADD_TEST(test_quic_forbidden_apis_ctx); ADD_TEST(test_quic_forbidden_apis); ADD_TEST(test_quic_forbidden_options); ADD_ALL_TESTS(test_quic_set_fd, 3); ADD_TEST(test_bio_ssl); ADD_TEST(test_back_pressure); ADD_TEST(test_multiple_dgrams); ADD_ALL_TESTS(test_non_io_retry, 2); ADD_TEST(test_quic_psk); ADD_ALL_TESTS(test_client_auth, 3); ADD_ALL_TESTS(test_alpn, 2); ADD_ALL_TESTS(test_noisy_dgram, 2); ADD_TEST(test_bw_limit); ADD_TEST(test_get_shutdown); ADD_ALL_TESTS(test_tparam, OSSL_NELEM(tparam_tests)); return 1; err: cleanup_tests(); return 0; } void cleanup_tests(void) { bio_f_noisy_dgram_filter_free(); bio_f_pkt_split_dgram_filter_free(); OPENSSL_free(cert); OPENSSL_free(privkey); OPENSSL_free(ccert); OPENSSL_free(cauthca); OPENSSL_free(cprivkey); OSSL_PROVIDER_unload(defctxnull); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/quicapitest.c
openssl
#include "internal/quic_srtm.h" #include "testutil.h" static char ptrs[8]; static const QUIC_STATELESS_RESET_TOKEN token_1 = {{ 0x01, 0x02, 0x03, 0x04 }}; static const QUIC_STATELESS_RESET_TOKEN token_2 = {{ 0x01, 0x02, 0x03, 0x05 }}; static int test_srtm(void) { int testresult = 0; QUIC_SRTM *srtm; void *opaque = NULL; uint64_t seq_num = 0; if (!TEST_ptr(srtm = ossl_quic_srtm_new(NULL, NULL))) goto err; if (!TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1)) || !TEST_false(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1)) || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 0, 1)) || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 3, 0)) || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3)) || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 1, &token_1)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 2, &token_1)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 3, &token_1)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 1, 0, &token_1)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 2, 0, &token_2)) || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 3, 3, &token_2)) || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 3, 3)) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 1) || !TEST_uint64_t_eq(seq_num, 0) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 1, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 0) || !TEST_uint64_t_eq(seq_num, 3) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 2, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 0) || !TEST_uint64_t_eq(seq_num, 2) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 3, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 0) || !TEST_uint64_t_eq(seq_num, 1) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 4, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 0) || !TEST_uint64_t_eq(seq_num, 0) || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_1, 5, &opaque, &seq_num)) || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 0)) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 1) || !TEST_uint64_t_eq(seq_num, 0) || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num)) || !TEST_ptr_eq(opaque, ptrs + 2) || !TEST_uint64_t_eq(seq_num, 0) || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 2, 0)) || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num)) ) goto err; testresult = 1; err: ossl_quic_srtm_free(srtm); return testresult; } int setup_tests(void) { ADD_TEST(test_srtm); return 1; }
test
openssl/test/quic_srtm_test.c
openssl
#include <string.h> #include <openssl/core_names.h> #include <openssl/core_dispatch.h> #include <openssl/rand.h> #include <openssl/params.h> #include <openssl/err.h> #include <openssl/proverr.h> #include <openssl/pkcs12.h> #include <openssl/provider.h> #include <assert.h> #include <openssl/asn1.h> #include <openssl/asn1t.h> #include <openssl/core_object.h> #include "internal/asn1.h" #include <openssl/ssl.h> #include "internal/nelem.h" #include "internal/refcount.h" #define XORPROV_R_INVALID_DIGEST 1 #define XORPROV_R_INVALID_SIZE 2 #define XORPROV_R_INVALID_KEY 3 #define XORPROV_R_UNSUPPORTED 4 #define XORPROV_R_MISSING_OID 5 #define XORPROV_R_OBJ_CREATE_ERR 6 #define XORPROV_R_INVALID_ENCODING 7 #define XORPROV_R_SIGN_ERROR 8 #define XORPROV_R_LIB_CREATE_ERR 9 #define XORPROV_R_NO_PRIVATE_KEY 10 #define XORPROV_R_BUFFER_LENGTH_WRONG 11 #define XORPROV_R_SIGNING_FAILED 12 #define XORPROV_R_WRONG_PARAMETERS 13 #define XORPROV_R_VERIFY_ERROR 14 #define XORPROV_R_EVPINFO_MISSING 15 static OSSL_FUNC_keymgmt_import_fn xor_import; static OSSL_FUNC_keymgmt_import_types_fn xor_import_types; static OSSL_FUNC_keymgmt_import_types_ex_fn xor_import_types_ex; static OSSL_FUNC_keymgmt_export_fn xor_export; static OSSL_FUNC_keymgmt_export_types_fn xor_export_types; static OSSL_FUNC_keymgmt_export_types_ex_fn xor_export_types_ex; int tls_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx); #define XOR_KEY_SIZE 32 static const unsigned char private_constant[XOR_KEY_SIZE] = { 0xd3, 0x6b, 0x54, 0xec, 0x5b, 0xac, 0x89, 0x96, 0x8c, 0x2c, 0x66, 0xa5, 0x67, 0x0d, 0xe3, 0xdd, 0x43, 0x69, 0xbc, 0x83, 0x3d, 0x60, 0xc7, 0xb8, 0x2b, 0x1c, 0x5a, 0xfd, 0xb5, 0xcd, 0xd0, 0xf8 }; typedef struct xorkey_st { unsigned char privkey[XOR_KEY_SIZE]; unsigned char pubkey[XOR_KEY_SIZE]; int hasprivkey; int haspubkey; char *tls_name; CRYPTO_REF_COUNT references; } XORKEY; static OSSL_FUNC_keymgmt_new_fn xor_newkey; static OSSL_FUNC_keymgmt_free_fn xor_freekey; static OSSL_FUNC_keymgmt_has_fn xor_has; static OSSL_FUNC_keymgmt_dup_fn xor_dup; static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init; static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params; static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params; static OSSL_FUNC_keymgmt_gen_fn xor_gen; static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup; static OSSL_FUNC_keymgmt_load_fn xor_load; static OSSL_FUNC_keymgmt_get_params_fn xor_get_params; static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params; static OSSL_FUNC_keymgmt_set_params_fn xor_set_params; static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params; static OSSL_FUNC_keyexch_newctx_fn xor_newkemkexctx; static OSSL_FUNC_keyexch_init_fn xor_init; static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer; static OSSL_FUNC_keyexch_derive_fn xor_derive; static OSSL_FUNC_keyexch_freectx_fn xor_freectx; static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx; static OSSL_FUNC_kem_newctx_fn xor_newkemkexctx; static OSSL_FUNC_kem_freectx_fn xor_freectx; static OSSL_FUNC_kem_dupctx_fn xor_dupctx; static OSSL_FUNC_kem_encapsulate_init_fn xor_init; static OSSL_FUNC_kem_encapsulate_fn xor_encapsulate; static OSSL_FUNC_kem_decapsulate_init_fn xor_init; static OSSL_FUNC_kem_decapsulate_fn xor_decapsulate; static OSSL_FUNC_keymgmt_new_fn * xor_prov_get_keymgmt_new(const OSSL_DISPATCH *fns) { for (; fns->function_id != 0; fns++) if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW) return OSSL_FUNC_keymgmt_new(fns); return NULL; } static OSSL_FUNC_keymgmt_free_fn * xor_prov_get_keymgmt_free(const OSSL_DISPATCH *fns) { for (; fns->function_id != 0; fns++) if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE) return OSSL_FUNC_keymgmt_free(fns); return NULL; } static OSSL_FUNC_keymgmt_import_fn * xor_prov_get_keymgmt_import(const OSSL_DISPATCH *fns) { for (; fns->function_id != 0; fns++) if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT) return OSSL_FUNC_keymgmt_import(fns); return NULL; } static OSSL_FUNC_keymgmt_export_fn * xor_prov_get_keymgmt_export(const OSSL_DISPATCH *fns) { for (; fns->function_id != 0; fns++) if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT) return OSSL_FUNC_keymgmt_export(fns); return NULL; } static void *xor_prov_import_key(const OSSL_DISPATCH *fns, void *provctx, int selection, const OSSL_PARAM params[]) { OSSL_FUNC_keymgmt_new_fn *kmgmt_new = xor_prov_get_keymgmt_new(fns); OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns); OSSL_FUNC_keymgmt_import_fn *kmgmt_import = xor_prov_get_keymgmt_import(fns); void *key = NULL; if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) { if ((key = kmgmt_new(provctx)) == NULL || !kmgmt_import(key, selection, params)) { kmgmt_free(key); key = NULL; } } return key; } static void xor_prov_free_key(const OSSL_DISPATCH *fns, void *key) { OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns); if (kmgmt_free != NULL) kmgmt_free(key); } struct tls_group_st { unsigned int group_id; unsigned int secbits; unsigned int mintls; unsigned int maxtls; unsigned int mindtls; unsigned int maxdtls; unsigned int is_kem; }; #define XORGROUP_NAME "xorgroup" #define XORGROUP_NAME_INTERNAL "xorgroup-int" static struct tls_group_st xor_group = { 0, 128, TLS1_3_VERSION, 0, -1, -1, 0 }; #define XORKEMGROUP_NAME "xorkemgroup" #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int" static struct tls_group_st xor_kemgroup = { 0, 128, TLS1_3_VERSION, 0, -1, -1, 1 }; #define ALGORITHM "XOR" static const OSSL_PARAM xor_group_params[] = { OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME, XORGROUP_NAME, sizeof(XORGROUP_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL, XORGROUP_NAME_INTERNAL, sizeof(XORGROUP_NAME_INTERNAL)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM, sizeof(ALGORITHM)), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_group.group_id), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS, &xor_group.secbits), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_group.mintls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_group.maxtls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_group.mindtls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_group.maxdtls), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_group.is_kem), OSSL_PARAM_END }; static const OSSL_PARAM xor_kemgroup_params[] = { OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME, XORKEMGROUP_NAME, sizeof(XORKEMGROUP_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL, XORKEMGROUP_NAME_INTERNAL, sizeof(XORKEMGROUP_NAME_INTERNAL)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM, sizeof(ALGORITHM)), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_kemgroup.group_id), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS, &xor_kemgroup.secbits), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_kemgroup.mintls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_kemgroup.maxtls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_kemgroup.mindtls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_kemgroup.maxdtls), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_kemgroup.is_kem), OSSL_PARAM_END }; #define NUM_DUMMY_GROUPS 50 static char *dummy_group_names[NUM_DUMMY_GROUPS]; struct tls_sigalg_st { unsigned int code_point; unsigned int secbits; unsigned int mintls; unsigned int maxtls; }; #define XORSIGALG_NAME "xorhmacsig" #define XORSIGALG_OID "1.3.6.1.4.1.16604.998888.1" #define XORSIGALG_HASH_NAME "xorhmacsha2sig" #define XORSIGALG_HASH "SHA256" #define XORSIGALG_HASH_OID "1.3.6.1.4.1.16604.998888.2" #define XORSIGALG12_NAME "xorhmacsig12" #define XORSIGALG12_OID "1.3.6.1.4.1.16604.998888.3" static struct tls_sigalg_st xor_sigalg = { 0, 128, TLS1_3_VERSION, 0, }; static struct tls_sigalg_st xor_sigalg_hash = { 0, 128, TLS1_3_VERSION, 0, }; static struct tls_sigalg_st xor_sigalg12 = { 0, 128, TLS1_2_VERSION, TLS1_2_VERSION, }; static const OSSL_PARAM xor_sig_nohash_params[] = { OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, XORSIGALG_NAME, sizeof(XORSIGALG_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, XORSIGALG_NAME, sizeof(XORSIGALG_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, XORSIGALG_OID, sizeof(XORSIGALG_OID)), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, &xor_sigalg.code_point), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, &xor_sigalg.secbits), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, &xor_sigalg.mintls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, &xor_sigalg.maxtls), OSSL_PARAM_END }; static const OSSL_PARAM xor_sig_hash_params[] = { OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, XORSIGALG_HASH_NAME, sizeof(XORSIGALG_HASH_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, XORSIGALG_HASH_NAME, sizeof(XORSIGALG_HASH_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME, XORSIGALG_HASH, sizeof(XORSIGALG_HASH)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, XORSIGALG_HASH_OID, sizeof(XORSIGALG_HASH_OID)), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, &xor_sigalg_hash.code_point), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, &xor_sigalg_hash.secbits), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, &xor_sigalg_hash.mintls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, &xor_sigalg_hash.maxtls), OSSL_PARAM_END }; static const OSSL_PARAM xor_sig_12_params[] = { OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, XORSIGALG12_NAME, sizeof(XORSIGALG12_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, XORSIGALG12_NAME, sizeof(XORSIGALG12_NAME)), OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, XORSIGALG12_OID, sizeof(XORSIGALG12_OID)), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, &xor_sigalg12.code_point), OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, &xor_sigalg12.secbits), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, &xor_sigalg12.mintls), OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, &xor_sigalg12.maxtls), OSSL_PARAM_END }; static int tls_prov_get_capabilities(void *provctx, const char *capability, OSSL_CALLBACK *cb, void *arg) { int ret = 0; int i; const char *dummy_base = "dummy"; const size_t dummy_name_max_size = strlen(dummy_base) + 3; if (strcmp(capability, "TLS-GROUP") == 0) { OPENSSL_assert(xor_group.group_id >= 65024 && xor_group.group_id < 65279 - NUM_DUMMY_GROUPS); ret = cb(xor_group_params, arg); ret &= cb(xor_kemgroup_params, arg); for (i = 0; i < NUM_DUMMY_GROUPS; i++) { OSSL_PARAM dummygroup[OSSL_NELEM(xor_group_params)]; unsigned int dummygroup_id; memcpy(dummygroup, xor_group_params, sizeof(xor_group_params)); if (dummy_group_names[i] == NULL) { dummy_group_names[i] = OPENSSL_zalloc(dummy_name_max_size); if (dummy_group_names[i] == NULL) return 0; BIO_snprintf(dummy_group_names[i], dummy_name_max_size, "%s%d", dummy_base, i); } dummygroup[0].data = dummy_group_names[i]; dummygroup[0].data_size = strlen(dummy_group_names[i]) + 1; dummygroup_id = 65279 - NUM_DUMMY_GROUPS + i; dummygroup[3].data = (unsigned char*)&dummygroup_id; ret &= cb(dummygroup, arg); } } if (strcmp(capability, "TLS-SIGALG") == 0) { ret = cb(xor_sig_nohash_params, arg); ret &= cb(xor_sig_hash_params, arg); ret &= cb(xor_sig_12_params, arg); } return ret; } typedef struct { OSSL_LIB_CTX *libctx; } PROV_XOR_CTX; static PROV_XOR_CTX *xor_newprovctx(OSSL_LIB_CTX *libctx) { PROV_XOR_CTX* prov_ctx = OPENSSL_malloc(sizeof(PROV_XOR_CTX)); if (prov_ctx == NULL) return NULL; if (libctx == NULL) { OPENSSL_free(prov_ctx); return NULL; } prov_ctx->libctx = libctx; return prov_ctx; } #define PROV_XOR_LIBCTX_OF(provctx) (((PROV_XOR_CTX *)provctx)->libctx) typedef struct { XORKEY *key; XORKEY *peerkey; void *provctx; } PROV_XORKEMKEX_CTX; static void *xor_newkemkexctx(void *provctx) { PROV_XORKEMKEX_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XORKEMKEX_CTX)); if (pxorctx == NULL) return NULL; pxorctx->provctx = provctx; return pxorctx; } static int xor_init(void *vpxorctx, void *vkey, ossl_unused const OSSL_PARAM params[]) { PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; if (pxorctx == NULL || vkey == NULL) return 0; pxorctx->key = vkey; return 1; } static int xor_set_peer(void *vpxorctx, void *vpeerkey) { PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; if (pxorctx == NULL || vpeerkey == NULL) return 0; pxorctx->peerkey = vpeerkey; return 1; } static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen, size_t outlen) { PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; int i; if (pxorctx->key == NULL || pxorctx->peerkey == NULL) return 0; *secretlen = XOR_KEY_SIZE; if (secret == NULL) return 1; if (outlen < XOR_KEY_SIZE) return 0; for (i = 0; i < XOR_KEY_SIZE; i++) secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i]; return 1; } static void xor_freectx(void *pxorctx) { OPENSSL_free(pxorctx); } static void *xor_dupctx(void *vpxorctx) { PROV_XORKEMKEX_CTX *srcctx = (PROV_XORKEMKEX_CTX *)vpxorctx; PROV_XORKEMKEX_CTX *dstctx; dstctx = OPENSSL_zalloc(sizeof(*srcctx)); if (dstctx == NULL) return NULL; *dstctx = *srcctx; return dstctx; } static const OSSL_DISPATCH xor_keyexch_functions[] = { { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newkemkexctx }, { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init }, { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive }, { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer }, { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx }, { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM tls_prov_keyexch[] = { { "XOR", "provider=tls-provider,fips=yes", xor_keyexch_functions }, { NULL, NULL, NULL } }; static int xor_encapsulate(void *vpxorctx, unsigned char *ct, size_t *ctlen, unsigned char *ss, size_t *sslen) { int rv = 0; void *genctx = NULL, *derivectx = NULL; XORKEY *ourkey = NULL; PROV_XORKEMKEX_CTX *pxorctx = vpxorctx; if (ct == NULL || ss == NULL) { if (ctlen == NULL && sslen == NULL) return 0; if (ctlen != NULL) *ctlen = XOR_KEY_SIZE; if (sslen != NULL) *sslen = XOR_KEY_SIZE; return 1; } genctx = xor_gen_init(pxorctx->provctx, OSSL_KEYMGMT_SELECT_KEYPAIR, NULL); if (genctx == NULL) goto end; ourkey = xor_gen(genctx, NULL, NULL); if (ourkey == NULL) goto end; memcpy(ct, ourkey->pubkey, XOR_KEY_SIZE); *ctlen = XOR_KEY_SIZE; derivectx = xor_newkemkexctx(pxorctx->provctx); if (derivectx == NULL || !xor_init(derivectx, ourkey, NULL) || !xor_set_peer(derivectx, pxorctx->key) || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE)) goto end; rv = 1; end: xor_gen_cleanup(genctx); xor_freekey(ourkey); xor_freectx(derivectx); return rv; } static int xor_decapsulate(void *vpxorctx, unsigned char *ss, size_t *sslen, const unsigned char *ct, size_t ctlen) { int rv = 0; void *derivectx = NULL; XORKEY *peerkey = NULL; PROV_XORKEMKEX_CTX *pxorctx = vpxorctx; if (ss == NULL) { if (sslen == NULL) return 0; *sslen = XOR_KEY_SIZE; return 1; } if (ctlen != XOR_KEY_SIZE) return 0; peerkey = xor_newkey(pxorctx->provctx); if (peerkey == NULL) goto end; memcpy(peerkey->pubkey, ct, XOR_KEY_SIZE); derivectx = xor_newkemkexctx(pxorctx->provctx); if (derivectx == NULL || !xor_init(derivectx, pxorctx->key, NULL) || !xor_set_peer(derivectx, peerkey) || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE)) goto end; rv = 1; end: xor_freekey(peerkey); xor_freectx(derivectx); return rv; } static const OSSL_DISPATCH xor_kem_functions[] = { { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))xor_newkemkexctx }, { OSSL_FUNC_KEM_FREECTX, (void (*)(void))xor_freectx }, { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))xor_dupctx }, { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (void (*)(void))xor_init }, { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))xor_encapsulate }, { OSSL_FUNC_KEM_DECAPSULATE_INIT, (void (*)(void))xor_init }, { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))xor_decapsulate }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM tls_prov_kem[] = { { "XOR", "provider=tls-provider,fips=yes", xor_kem_functions }, { NULL, NULL, NULL } }; static void *xor_newkey(void *provctx) { XORKEY *ret = OPENSSL_zalloc(sizeof(XORKEY)); if (ret == NULL) return NULL; if (!CRYPTO_NEW_REF(&ret->references, 1)) { OPENSSL_free(ret); return NULL; } return ret; } static void xor_freekey(void *keydata) { XORKEY* key = (XORKEY *)keydata; int refcnt; if (key == NULL) return; if (CRYPTO_DOWN_REF(&key->references, &refcnt) <= 0) return; if (refcnt > 0) return; assert(refcnt == 0); if (key != NULL) { OPENSSL_free(key->tls_name); key->tls_name = NULL; } CRYPTO_FREE_REF(&key->references); OPENSSL_free(key); } static int xor_key_up_ref(XORKEY *key) { int refcnt; if (CRYPTO_UP_REF(&key->references, &refcnt) <= 0) return 0; assert(refcnt > 1); return (refcnt > 1); } static int xor_has(const void *vkey, int selection) { const XORKEY *key = vkey; int ok = 0; if (key != NULL) { ok = 1; if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) ok = ok && key->haspubkey; if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) ok = ok && key->hasprivkey; } return ok; } static void *xor_dup(const void *vfromkey, int selection) { XORKEY *tokey = xor_newkey(NULL); const XORKEY *fromkey = vfromkey; int ok = 0; if (tokey != NULL && fromkey != NULL) { ok = 1; if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { if (fromkey->haspubkey) { memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE); tokey->haspubkey = 1; } else { tokey->haspubkey = 0; } } if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { if (fromkey->hasprivkey) { memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE); tokey->hasprivkey = 1; } else { tokey->hasprivkey = 0; } } if (fromkey->tls_name != NULL) tokey->tls_name = OPENSSL_strdup(fromkey->tls_name); } if (!ok) { xor_freekey(tokey); tokey = NULL; } return tokey; } static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[]) { XORKEY *key = vkey; OSSL_PARAM *p; if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE)) return 0; if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL && !OSSL_PARAM_set_int(p, xor_group.secbits)) return 0; if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) { if (p->data_type != OSSL_PARAM_OCTET_STRING) return 0; p->return_size = XOR_KEY_SIZE; if (p->data != NULL && p->data_size >= XOR_KEY_SIZE) memcpy(p->data, key->pubkey, XOR_KEY_SIZE); } return 1; } static const OSSL_PARAM xor_params[] = { OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *xor_gettable_params(void *provctx) { return xor_params; } static int xor_set_params(void *vkey, const OSSL_PARAM params[]) { XORKEY *key = vkey; const OSSL_PARAM *p; p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY); if (p != NULL) { if (p->data_type != OSSL_PARAM_OCTET_STRING || p->data_size != XOR_KEY_SIZE) return 0; memcpy(key->pubkey, p->data, XOR_KEY_SIZE); key->haspubkey = 1; } return 1; } static const OSSL_PARAM xor_known_settable_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), OSSL_PARAM_END }; static void *xor_load(const void *reference, size_t reference_sz) { XORKEY *key = NULL; if (reference_sz == sizeof(key)) { key = *(XORKEY **)reference; *(XORKEY **)reference = NULL; return key; } return NULL; } static int xor_recreate(const unsigned char *kd1, const unsigned char *kd2) { int i; for (i = 0; i < XOR_KEY_SIZE; i++) { if ((kd1[i] & 0xff) != ((kd2[i] ^ private_constant[i]) & 0xff)) return 0; } return 1; } static int xor_match(const void *keydata1, const void *keydata2, int selection) { const XORKEY *key1 = keydata1; const XORKEY *key2 = keydata2; int ok = 1; if (key1->tls_name != NULL && key2->tls_name != NULL) ok = ok & (strcmp(key1->tls_name, key2->tls_name) == 0); if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { if (key1->hasprivkey) { if (key2->hasprivkey) ok = ok & (CRYPTO_memcmp(key1->privkey, key2->privkey, XOR_KEY_SIZE) == 0); else ok = ok & xor_recreate(key1->privkey, key2->pubkey); } else { if (key2->hasprivkey) ok = ok & xor_recreate(key2->privkey, key1->pubkey); else ok = 0; } } if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { if (key1->haspubkey) { if (key2->haspubkey) ok = ok & (CRYPTO_memcmp(key1->pubkey, key2->pubkey, XOR_KEY_SIZE) == 0); else ok = ok & xor_recreate(key1->pubkey, key2->privkey); } else { if (key2->haspubkey) ok = ok & xor_recreate(key2->pubkey, key1->privkey); else ok = 0; } } return ok; } static const OSSL_PARAM *xor_settable_params(void *provctx) { return xor_known_settable_params; } struct xor_gen_ctx { int selection; OSSL_LIB_CTX *libctx; }; static void *xor_gen_init(void *provctx, int selection, const OSSL_PARAM params[]) { struct xor_gen_ctx *gctx = NULL; if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0) return NULL; if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) gctx->selection = selection; gctx->libctx = PROV_XOR_LIBCTX_OF(provctx); if (!xor_gen_set_params(gctx, params)) { OPENSSL_free(gctx); return NULL; } return gctx; } static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[]) { struct xor_gen_ctx *gctx = genctx; const OSSL_PARAM *p; if (gctx == NULL) return 0; p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); if (p != NULL) { if (p->data_type != OSSL_PARAM_UTF8_STRING || (strcmp(p->data, XORGROUP_NAME_INTERNAL) != 0 && strcmp(p->data, XORKEMGROUP_NAME_INTERNAL) != 0)) return 0; } return 1; } static const OSSL_PARAM *xor_gen_settable_params(ossl_unused void *genctx, ossl_unused void *provctx) { static OSSL_PARAM settable[] = { OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), OSSL_PARAM_END }; return settable; } static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { struct xor_gen_ctx *gctx = genctx; XORKEY *key = xor_newkey(NULL); size_t i; if (key == NULL) return NULL; if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE, 0) <= 0) { OPENSSL_free(key); return NULL; } for (i = 0; i < XOR_KEY_SIZE; i++) key->pubkey[i] = key->privkey[i] ^ private_constant[i]; key->hasprivkey = 1; key->haspubkey = 1; } return key; } static int xor_import(void *vkey, int select, const OSSL_PARAM params[]) { XORKEY *key = vkey; const OSSL_PARAM *param_priv_key, *param_pub_key; unsigned char privkey[XOR_KEY_SIZE]; unsigned char pubkey[XOR_KEY_SIZE]; void *pprivkey = privkey, *ppubkey = pubkey; size_t priv_len = 0, pub_len = 0; int res = 0; if (key == NULL || (select & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) return 0; memset(privkey, 0, sizeof(privkey)); memset(pubkey, 0, sizeof(pubkey)); param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY); if ((param_priv_key != NULL && !OSSL_PARAM_get_octet_string(param_priv_key, &pprivkey, sizeof(privkey), &priv_len)) || (param_pub_key != NULL && !OSSL_PARAM_get_octet_string(param_pub_key, &ppubkey, sizeof(pubkey), &pub_len))) goto err; if (priv_len > 0) { memcpy(key->privkey, privkey, priv_len); key->hasprivkey = 1; } if (pub_len > 0) { memcpy(key->pubkey, pubkey, pub_len); key->haspubkey = 1; } res = 1; err: return res; } static int xor_export(void *vkey, int select, OSSL_CALLBACK *param_cb, void *cbarg) { XORKEY *key = vkey; OSSL_PARAM params[3], *p = params; if (key == NULL || (select & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) return 0; *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key->privkey, sizeof(key->privkey)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key->pubkey, sizeof(key->pubkey)); *p++ = OSSL_PARAM_construct_end(); return param_cb(params, cbarg); } static const OSSL_PARAM xor_key_types[] = { OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *xor_import_types(int select) { return (select & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0 ? xor_key_types : NULL; } static const OSSL_PARAM *xor_import_types_ex(void *provctx, int select) { if (provctx == NULL) return NULL; return xor_import_types(select); } static const OSSL_PARAM *xor_export_types(int select) { return (select & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0 ? xor_key_types : NULL; } static const OSSL_PARAM *xor_export_types_ex(void *provctx, int select) { if (provctx == NULL) return NULL; return xor_export_types(select); } static void xor_gen_cleanup(void *genctx) { OPENSSL_free(genctx); } static const OSSL_DISPATCH xor_keymgmt_functions[] = { { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (void (*)(void))xor_gen_settable_params }, { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen }, { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup }, { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params }, { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params }, { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params }, { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES_EX, (void (*)(void))xor_import_types_ex }, { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types }, { OSSL_FUNC_KEYMGMT_EXPORT_TYPES_EX, (void (*)(void))xor_export_types_ex }, OSSL_DISPATCH_END }; static void *xor_xorhmacsig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { XORKEY *k = xor_gen(genctx, osslcb, cbarg); if (k == NULL) return NULL; k->tls_name = OPENSSL_strdup(XORSIGALG_NAME); if (k->tls_name == NULL) { xor_freekey(k); return NULL; } return k; } static void *xor_xorhmacsha2sig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { XORKEY* k = xor_gen(genctx, osslcb, cbarg); if (k == NULL) return NULL; k->tls_name = OPENSSL_strdup(XORSIGALG_HASH_NAME); if (k->tls_name == NULL) { xor_freekey(k); return NULL; } return k; } static const OSSL_DISPATCH xor_xorhmacsig_keymgmt_functions[] = { { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (void (*)(void))xor_gen_settable_params }, { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsig_gen }, { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup }, { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params }, { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params }, { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params }, { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types }, { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load }, { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match }, OSSL_DISPATCH_END }; static const OSSL_DISPATCH xor_xorhmacsha2sig_keymgmt_functions[] = { { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (void (*)(void))xor_gen_settable_params }, { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsha2sig_gen }, { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup }, { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params }, { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params }, { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params }, { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types }, { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load }, { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match }, OSSL_DISPATCH_END }; typedef enum { KEY_OP_PUBLIC, KEY_OP_PRIVATE, KEY_OP_KEYGEN } xor_key_op_t; static XORKEY *xor_key_op(const X509_ALGOR *palg, const unsigned char *p, int plen, xor_key_op_t op, OSSL_LIB_CTX *libctx, const char *propq) { XORKEY *key = NULL; int nid = NID_undef; if (palg != NULL) { int ptype; X509_ALGOR_get0(NULL, &ptype, NULL, palg); if (ptype != V_ASN1_UNDEF || palg->algorithm == NULL) { ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); return 0; } nid = OBJ_obj2nid(palg->algorithm); } if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) { ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); return 0; } key = xor_newkey(NULL); if (key == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); return 0; } if (XOR_KEY_SIZE != plen) { ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); goto err; } if (op == KEY_OP_PUBLIC) { memcpy(key->pubkey, p, plen); key->haspubkey = 1; } else { memcpy(key->privkey, p, plen); key->hasprivkey = 1; } key->tls_name = OPENSSL_strdup(OBJ_nid2sn(nid)); if (key->tls_name == NULL) goto err; return key; err: xor_freekey(key); return NULL; } static XORKEY *xor_key_from_x509pubkey(const X509_PUBKEY *xpk, OSSL_LIB_CTX *libctx, const char *propq) { const unsigned char *p; int plen; X509_ALGOR *palg; if (!xpk || (!X509_PUBKEY_get0_param(NULL, &p, &plen, &palg, xpk))) { return NULL; } return xor_key_op(palg, p, plen, KEY_OP_PUBLIC, libctx, propq); } static XORKEY *xor_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq) { XORKEY *xork = NULL; const unsigned char *p; int plen; ASN1_OCTET_STRING *oct = NULL; const X509_ALGOR *palg; if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf)) return 0; oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen); if (oct == NULL) { p = NULL; plen = 0; } else { p = ASN1_STRING_get0_data(oct); plen = ASN1_STRING_length(oct); } xork = xor_key_op(palg, p, plen, KEY_OP_PRIVATE, libctx, propq); ASN1_OCTET_STRING_free(oct); return xork; } static const OSSL_ALGORITHM tls_prov_keymgmt[] = { { "XOR", "provider=tls-provider,fips=yes", xor_keymgmt_functions }, { XORSIGALG_NAME, "provider=tls-provider,fips=yes", xor_xorhmacsig_keymgmt_functions }, { XORSIGALG_HASH_NAME, "provider=tls-provider,fips=yes", xor_xorhmacsha2sig_keymgmt_functions }, { NULL, NULL, NULL } }; struct key2any_ctx_st { PROV_XOR_CTX *provctx; int save_parameters; int cipher_intent; EVP_CIPHER *cipher; OSSL_PASSPHRASE_CALLBACK *pwcb; void *pwcbarg; }; typedef int check_key_type_fn(const void *key, int nid); typedef int key_to_paramstring_fn(const void *key, int nid, int save, void **str, int *strtype); typedef int key_to_der_fn(BIO *out, const void *key, int key_nid, const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx); typedef int write_bio_of_void_fn(BIO *bp, const void *x); static void free_asn1_data(int type, void *data) { switch(type) { case V_ASN1_OBJECT: ASN1_OBJECT_free(data); break; case V_ASN1_SEQUENCE: ASN1_STRING_free(data); break; } } static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, void *params, int params_type, i2d_of_void *k2d) { unsigned char *der = NULL; int derlen; PKCS8_PRIV_KEY_INFO *p8info = NULL; if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL || (derlen = k2d(key, &der)) <= 0 || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0, V_ASN1_UNDEF, NULL, der, derlen)) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); PKCS8_PRIV_KEY_INFO_free(p8info); OPENSSL_free(der); p8info = NULL; } return p8info; } static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, struct key2any_ctx_st *ctx) { X509_SIG *p8 = NULL; char kstr[PEM_BUFSIZE]; size_t klen = 0; OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx); if (ctx->cipher == NULL || ctx->pwcb == NULL) return NULL; if (!ctx->pwcb(kstr, PEM_BUFSIZE, &klen, NULL, ctx->pwcbarg)) { ERR_raise(ERR_LIB_USER, PROV_R_UNABLE_TO_GET_PASSPHRASE); return NULL; } p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info, libctx, NULL); OPENSSL_cleanse(kstr, klen); return p8; } static X509_SIG *key_to_encp8(const void *key, int key_nid, void *params, int params_type, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { PKCS8_PRIV_KEY_INFO *p8info = key_to_p8info(key, key_nid, params, params_type, k2d); X509_SIG *p8 = NULL; if (p8info == NULL) { free_asn1_data(params_type, params); } else { p8 = p8info_to_encp8(p8info, ctx); PKCS8_PRIV_KEY_INFO_free(p8info); } return p8; } static X509_PUBKEY *xorx_key_to_pubkey(const void *key, int key_nid, void *params, int params_type, i2d_of_void k2d) { unsigned char *der = NULL; int derlen; X509_PUBKEY *xpk = NULL; if ((xpk = X509_PUBKEY_new()) == NULL || (derlen = k2d(key, &der)) <= 0 || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid), V_ASN1_UNDEF, NULL, der, derlen)) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); X509_PUBKEY_free(xpk); OPENSSL_free(der); xpk = NULL; } return xpk; } static int key_to_epki_der_priv_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; X509_SIG *p8; if (!ctx->cipher_intent) return 0; if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); if (p8 != NULL) ret = i2d_PKCS8_bio(out, p8); X509_SIG_free(p8); return ret; } static int key_to_epki_pem_priv_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; X509_SIG *p8; if (!ctx->cipher_intent) return 0; if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); if (p8 != NULL) ret = PEM_write_bio_PKCS8(out, p8); X509_SIG_free(p8); return ret; } static int key_to_pki_der_priv_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; PKCS8_PRIV_KEY_INFO *p8info; if (ctx->cipher_intent) return key_to_epki_der_priv_bio(out, key, key_nid, pemname, p2s, k2d, ctx); if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8info = key_to_p8info(key, key_nid, str, strtype, k2d); if (p8info != NULL) ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info); else free_asn1_data(strtype, str); PKCS8_PRIV_KEY_INFO_free(p8info); return ret; } static int key_to_pki_pem_priv_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; PKCS8_PRIV_KEY_INFO *p8info; if (ctx->cipher_intent) return key_to_epki_pem_priv_bio(out, key, key_nid, pemname, p2s, k2d, ctx); if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8info = key_to_p8info(key, key_nid, str, strtype, k2d); if (p8info != NULL) ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info); else free_asn1_data(strtype, str); PKCS8_PRIV_KEY_INFO_free(p8info); return ret; } static int key_to_spki_der_pub_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; X509_PUBKEY *xpk = NULL; void *str = NULL; int strtype = V_ASN1_UNDEF; if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d); if (xpk != NULL) ret = i2d_X509_PUBKEY_bio(out, xpk); X509_PUBKEY_free(xpk); return ret; } static int key_to_spki_pem_pub_bio(BIO *out, const void *key, int key_nid, ossl_unused const char *pemname, key_to_paramstring_fn *p2s, i2d_of_void *k2d, struct key2any_ctx_st *ctx) { int ret = 0; X509_PUBKEY *xpk = NULL; void *str = NULL; int strtype = V_ASN1_UNDEF; if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d); if (xpk != NULL) ret = PEM_write_bio_X509_PUBKEY(out, xpk); else free_asn1_data(strtype, str); X509_PUBKEY_free(xpk); return ret; } static int prepare_xorx_params(const void *xorxkey, int nid, int save, void **pstr, int *pstrtype) { ASN1_OBJECT *params = NULL; XORKEY *k = (XORKEY*)xorxkey; if (k->tls_name && OBJ_sn2nid(k->tls_name) != nid) { ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY); return 0; } if (nid == NID_undef) { ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID); return 0; } params = OBJ_nid2obj(nid); if (params == NULL || OBJ_length(params) == 0) { ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID); ASN1_OBJECT_free(params); return 0; } *pstr = params; *pstrtype = V_ASN1_OBJECT; return 1; } static int xorx_spki_pub_to_der(const void *vecxkey, unsigned char **pder) { const XORKEY *xorxkey = vecxkey; unsigned char *keyblob; int retlen; if (xorxkey == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); return 0; } keyblob = OPENSSL_memdup(xorxkey->pubkey, retlen = XOR_KEY_SIZE); if (keyblob == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); return 0; } *pder = keyblob; return retlen; } static int xorx_pki_priv_to_der(const void *vecxkey, unsigned char **pder) { XORKEY *xorxkey = (XORKEY *)vecxkey; unsigned char* buf = NULL; ASN1_OCTET_STRING oct; int keybloblen; if (xorxkey == NULL) { ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); return 0; } buf = OPENSSL_secure_malloc(XOR_KEY_SIZE); memcpy(buf, xorxkey->privkey, XOR_KEY_SIZE); oct.data = buf; oct.length = XOR_KEY_SIZE; oct.flags = 0; keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder); if (keybloblen < 0) { ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); keybloblen = 0; } OPENSSL_secure_clear_free(buf, XOR_KEY_SIZE); return keybloblen; } # define xorx_epki_priv_to_der xorx_pki_priv_to_der # define xorx_check_key_type NULL # define xorhmacsig_evp_type 0 # define xorhmacsig_input_type XORSIGALG_NAME # define xorhmacsig_pem_type XORSIGALG_NAME # define xorhmacsha2sig_evp_type 0 # define xorhmacsha2sig_input_type XORSIGALG_HASH_NAME # define xorhmacsha2sig_pem_type XORSIGALG_HASH_NAME static OSSL_FUNC_decoder_newctx_fn key2any_newctx; static OSSL_FUNC_decoder_freectx_fn key2any_freectx; static void *key2any_newctx(void *provctx) { struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); if (ctx != NULL) { ctx->provctx = provctx; ctx->save_parameters = 1; } return ctx; } static void key2any_freectx(void *vctx) { struct key2any_ctx_st *ctx = vctx; EVP_CIPHER_free(ctx->cipher); OPENSSL_free(ctx); } static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx) { static const OSSL_PARAM settables[] = { OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0), OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0), OSSL_PARAM_END, }; return settables; } static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { struct key2any_ctx_st *ctx = vctx; OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx); const OSSL_PARAM *cipherp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER); const OSSL_PARAM *propsp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); const OSSL_PARAM *save_paramsp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS); if (cipherp != NULL) { const char *ciphername = NULL; const char *props = NULL; if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername)) return 0; if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props)) return 0; EVP_CIPHER_free(ctx->cipher); ctx->cipher = NULL; ctx->cipher_intent = ciphername != NULL; if (ciphername != NULL && ((ctx->cipher = EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) { return 0; } } if (save_paramsp != NULL) { if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters)) { return 0; } } return 1; } static int key2any_check_selection(int selection, int selection_mask) { int checks[] = { OSSL_KEYMGMT_SELECT_PRIVATE_KEY, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS }; size_t i; if (selection == 0) return 1; for (i = 0; i < OSSL_NELEM(checks); i++) { int check1 = (selection & checks[i]) != 0; int check2 = (selection_mask & checks[i]) != 0; if (check1) return check2; } return 0; } static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, const void *key, const char* typestr, const char *pemname, key_to_der_fn *writer, OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg, key_to_paramstring_fn *key2paramstring, i2d_of_void *key2der) { int ret = 0; int type = OBJ_sn2nid(typestr); if (key == NULL || type <= 0) { ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); } else if (writer != NULL) { BIO *out = BIO_new_from_core_bio(ctx->provctx->libctx, cout); if (out != NULL) { ctx->pwcb = pwcb; ctx->pwcbarg = pwcbarg; ret = writer(out, key, type, pemname, key2paramstring, key2der, ctx); } BIO_free(out); } else { ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); } return ret; } #define DO_ENC_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY #define DO_ENC_PRIVATE_KEY(impl, type, kind, output) \ if ((selection & DO_ENC_PRIVATE_KEY_selection_mask) != 0) \ return key2any_encode(ctx, cout, key, impl##_pem_type, \ impl##_pem_type " PRIVATE KEY", \ key_to_##kind##_##output##_priv_bio, \ cb, cbarg, prepare_##type##_params, \ type##_##kind##_priv_to_der); #define DO_ENC_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY #define DO_ENC_PUBLIC_KEY(impl, type, kind, output) \ if ((selection & DO_ENC_PUBLIC_KEY_selection_mask) != 0) \ return key2any_encode(ctx, cout, key, impl##_pem_type, \ impl##_pem_type " PUBLIC KEY", \ key_to_##kind##_##output##_pub_bio, \ cb, cbarg, prepare_##type##_params, \ type##_##kind##_pub_to_der); #define DO_ENC_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS #define DO_ENC_PARAMETERS(impl, type, kind, output) \ if ((selection & DO_ENC_PARAMETERS_selection_mask) != 0) \ return key2any_encode(ctx, cout, key, impl##_pem_type, \ impl##_pem_type " PARAMETERS", \ key_to_##kind##_##output##_param_bio, \ NULL, NULL, NULL, \ type##_##kind##_params_to_der); #define DO_ENC_PrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask #define DO_ENC_PrivateKeyInfo(impl, type, output) \ DO_ENC_PRIVATE_KEY(impl, type, pki, output) #define DO_ENC_EncryptedPrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask #define DO_ENC_EncryptedPrivateKeyInfo(impl, type, output) \ DO_ENC_PRIVATE_KEY(impl, type, epki, output) #define DO_ENC_SubjectPublicKeyInfo_selection_mask DO_ENC_PUBLIC_KEY_selection_mask #define DO_ENC_SubjectPublicKeyInfo(impl, type, output) \ DO_ENC_PUBLIC_KEY(impl, type, spki, output) #define MAKE_ENCODER(impl, type, kind, output) \ static OSSL_FUNC_encoder_import_object_fn \ impl##_to_##kind##_##output##_import_object; \ static OSSL_FUNC_encoder_free_object_fn \ impl##_to_##kind##_##output##_free_object; \ static OSSL_FUNC_encoder_encode_fn \ impl##_to_##kind##_##output##_encode; \ \ static void * \ impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \ const OSSL_PARAM params[]) \ { \ struct key2any_ctx_st *ctx = vctx; \ \ return xor_prov_import_key(xor_##impl##_keymgmt_functions, \ ctx->provctx, selection, params); \ } \ static void impl##_to_##kind##_##output##_free_object(void *key) \ { \ xor_prov_free_key(xor_##impl##_keymgmt_functions, key); \ } \ static int impl##_to_##kind##_##output##_does_selection(void *ctx, \ int selection) \ { \ return key2any_check_selection(selection, \ DO_ENC_##kind##_selection_mask); \ } \ static int \ impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \ const void *key, \ const OSSL_PARAM key_abstract[], \ int selection, \ OSSL_PASSPHRASE_CALLBACK *cb, \ void *cbarg) \ { \ \ if (key_abstract != NULL) { \ ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \ return 0; \ } \ DO_ENC_##kind(impl, type, output) \ \ ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \ return 0; \ } \ static const OSSL_DISPATCH \ xor_##impl##_to_##kind##_##output##_encoder_functions[] = { \ { OSSL_FUNC_ENCODER_NEWCTX, \ (void (*)(void))key2any_newctx }, \ { OSSL_FUNC_ENCODER_FREECTX, \ (void (*)(void))key2any_freectx }, \ { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ (void (*)(void))key2any_settable_ctx_params }, \ { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ (void (*)(void))key2any_set_ctx_params }, \ { OSSL_FUNC_ENCODER_DOES_SELECTION, \ (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \ { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ (void (*)(void))impl##_to_##kind##_##output##_import_object }, \ { OSSL_FUNC_ENCODER_FREE_OBJECT, \ (void (*)(void))impl##_to_##kind##_##output##_free_object }, \ { OSSL_FUNC_ENCODER_ENCODE, \ (void (*)(void))impl##_to_##kind##_##output##_encode }, \ OSSL_DISPATCH_END \ } MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, der); MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, pem); MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, der); MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, pem); MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, der); MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, pem); MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, der); MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, pem); MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, der); MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, pem); MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, der); MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, pem); static const OSSL_ALGORITHM tls_prov_encoder[] = { #define ENCODER_PROVIDER "tls-provider" #ifndef ENCODER_PROVIDER # error Macro ENCODER_PROVIDER undefined #endif #define ENCODER_STRUCTURE_PKCS8 "pkcs8" #define ENCODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo" #define ENCODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo" #define ENCODER_STRUCTURE_EncryptedPrivateKeyInfo "EncryptedPrivateKeyInfo" #define ENCODER_STRUCTURE_PKCS1 "pkcs1" #define ENCODER_STRUCTURE_PKCS3 "pkcs3" #define ENCODER_TEXT(_name, _sym) \ { _name, \ "provider=" ENCODER_PROVIDER ",fips=yes,output=text", \ (xor_##_sym##_to_text_encoder_functions) } #define ENCODER(_name, _sym, _fips, _output) \ { _name, \ "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output, \ (xor_##_sym##_to_##_output##_encoder_functions) } #define ENCODER_w_structure(_name, _sym, _output, _structure) \ { _name, \ "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output \ ",structure=" ENCODER_STRUCTURE_##_structure, \ (xor_##_sym##_to_##_structure##_##_output##_encoder_functions) } ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, PrivateKeyInfo), ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, PrivateKeyInfo), ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, EncryptedPrivateKeyInfo), ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, EncryptedPrivateKeyInfo), ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, SubjectPublicKeyInfo), ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, SubjectPublicKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, der, PrivateKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, pem, PrivateKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, der, EncryptedPrivateKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, pem, EncryptedPrivateKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, der, SubjectPublicKeyInfo), ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, pem, SubjectPublicKeyInfo), #undef ENCODER_PROVIDER { NULL, NULL, NULL } }; struct der2key_ctx_st; typedef int check_key_fn(void *, struct der2key_ctx_st *ctx); typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx); typedef void free_key_fn(void *); typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long, struct der2key_ctx_st *); struct keytype_desc_st { const char *keytype_name; const OSSL_DISPATCH *fns; const char *structure_name; int evp_type; int selection_mask; d2i_of_void *d2i_private_key; d2i_of_void *d2i_public_key; d2i_of_void *d2i_key_params; d2i_PKCS8_fn *d2i_PKCS8; d2i_of_void *d2i_PUBKEY; check_key_fn *check_key; adjust_key_fn *adjust_key; free_key_fn *free_key; }; struct X509_pubkey_st { X509_ALGOR *algor; ASN1_BIT_STRING *public_key; EVP_PKEY *pkey; OSSL_LIB_CTX *libctx; char *propq; }; ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = { ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING) } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL) static X509_PUBKEY *xorx_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp, long len, OSSL_LIB_CTX *libctx) { X509_PUBKEY *xpub = OPENSSL_zalloc(sizeof(*xpub)); if (xpub == NULL) return NULL; return (X509_PUBKEY *)ASN1_item_d2i_ex((ASN1_VALUE **)&xpub, pp, len, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), libctx, NULL); } struct der2key_ctx_st { PROV_XOR_CTX *provctx; struct keytype_desc_st *desc; int selection; unsigned int flag_fatal : 1; }; static int xor_read_der(PROV_XOR_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, long *len) { BUF_MEM *mem = NULL; BIO *in = BIO_new_from_core_bio(provctx->libctx, cin); int ok = (asn1_d2i_read_bio(in, &mem) >= 0); if (ok) { *data = (unsigned char *)mem->data; *len = (long)mem->length; OPENSSL_free(mem); } BIO_free(in); return ok; } typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq); static void *xor_der2key_decode_p8(const unsigned char **input_der, long input_der_len, struct der2key_ctx_st *ctx, key_from_pkcs8_t *key_from_pkcs8) { PKCS8_PRIV_KEY_INFO *p8inf = NULL; const X509_ALGOR *alg = NULL; void *key = NULL; if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf) && OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type) key = key_from_pkcs8(p8inf, PROV_XOR_LIBCTX_OF(ctx->provctx), NULL); PKCS8_PRIV_KEY_INFO_free(p8inf); return key; } static XORKEY *xor_d2i_PUBKEY(XORKEY **a, const unsigned char **pp, long length) { XORKEY *key = NULL; X509_PUBKEY *xpk; xpk = xorx_d2i_X509_PUBKEY_INTERNAL(pp, length, NULL); key = xor_key_from_x509pubkey(xpk, NULL, NULL); if (key == NULL) goto err_exit; if (a != NULL) { xor_freekey(*a); *a = key; } err_exit: X509_PUBKEY_free(xpk); return key; } static OSSL_FUNC_decoder_freectx_fn der2key_freectx; static OSSL_FUNC_decoder_decode_fn xor_der2key_decode; static OSSL_FUNC_decoder_export_object_fn der2key_export_object; static struct der2key_ctx_st * der2key_newctx(void *provctx, struct keytype_desc_st *desc, const char* tls_name) { struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); if (ctx != NULL) { ctx->provctx = provctx; ctx->desc = desc; if (desc->evp_type == 0) { ctx->desc->evp_type = OBJ_sn2nid(tls_name); } } return ctx; } static void der2key_freectx(void *vctx) { struct der2key_ctx_st *ctx = vctx; OPENSSL_free(ctx); } static int der2key_check_selection(int selection, const struct keytype_desc_st *desc) { int checks[] = { OSSL_KEYMGMT_SELECT_PRIVATE_KEY, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS }; size_t i; if (selection == 0) return 1; for (i = 0; i < OSSL_NELEM(checks); i++) { int check1 = (selection & checks[i]) != 0; int check2 = (desc->selection_mask & checks[i]) != 0; if (check1) return check2; } return 0; } static int xor_der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, OSSL_CALLBACK *data_cb, void *data_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct der2key_ctx_st *ctx = vctx; unsigned char *der = NULL; const unsigned char *derp; long der_len = 0; void *key = NULL; int ok = 0; ctx->selection = selection; if (selection == 0) selection = ctx->desc->selection_mask; if ((selection & ctx->desc->selection_mask) == 0) { ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } ok = xor_read_der(ctx->provctx, cin, &der, &der_len); if (!ok) goto next; ok = 0; if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { derp = der; if (ctx->desc->d2i_PKCS8 != NULL) { key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx); if (ctx->flag_fatal) goto end; } else if (ctx->desc->d2i_private_key != NULL) { key = ctx->desc->d2i_private_key(NULL, &derp, der_len); } if (key == NULL && ctx->selection != 0) goto next; } if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { derp = der; if (ctx->desc->d2i_PUBKEY != NULL) key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len); else key = ctx->desc->d2i_public_key(NULL, &derp, der_len); if (key == NULL && ctx->selection != 0) goto next; } if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) { derp = der; if (ctx->desc->d2i_key_params != NULL) key = ctx->desc->d2i_key_params(NULL, &derp, der_len); if (key == NULL && ctx->selection != 0) goto next; } if (key != NULL && ctx->desc->check_key != NULL && !ctx->desc->check_key(key, ctx)) { ctx->desc->free_key(key); key = NULL; } if (key != NULL && ctx->desc->adjust_key != NULL) ctx->desc->adjust_key(key, ctx); next: ok = 1; OPENSSL_free(der); der = NULL; if (key != NULL) { OSSL_PARAM params[4]; int object_type = OSSL_OBJECT_PKEY; params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, (char *)ctx->desc->keytype_name, 0); params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, &key, sizeof(key)); params[3] = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); } end: ctx->desc->free_key(key); OPENSSL_free(der); return ok; } static int der2key_export_object(void *vctx, const void *reference, size_t reference_sz, OSSL_CALLBACK *export_cb, void *export_cbarg) { struct der2key_ctx_st *ctx = vctx; OSSL_FUNC_keymgmt_export_fn *export = xor_prov_get_keymgmt_export(ctx->desc->fns); void *keydata; if (reference_sz == sizeof(keydata) && export != NULL) { keydata = *(void **)reference; return export(keydata, ctx->selection, export_cb, export_cbarg); } return 0; } static void *xorx_d2i_PKCS8(void **key, const unsigned char **der, long der_len, struct der2key_ctx_st *ctx) { return xor_der2key_decode_p8(der, der_len, ctx, (key_from_pkcs8_t *)xor_key_from_pkcs8); } static void xorx_key_adjust(void *key, struct der2key_ctx_st *ctx) { } #define DO_PrivateKeyInfo(keytype) \ "PrivateKeyInfo", 0, \ ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \ NULL, \ NULL, \ NULL, \ xorx_d2i_PKCS8, \ NULL, \ NULL, \ xorx_key_adjust, \ (free_key_fn *)xor_freekey #define DO_SubjectPublicKeyInfo(keytype) \ "SubjectPublicKeyInfo", 0, \ ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \ NULL, \ NULL, \ NULL, \ NULL, \ (d2i_of_void *)xor_d2i_PUBKEY, \ NULL, \ xorx_key_adjust, \ (free_key_fn *)xor_freekey #define MAKE_DECODER(keytype_name, keytype, type, kind) \ static struct keytype_desc_st kind##_##keytype##_desc = \ { keytype_name, xor_##keytype##_keymgmt_functions, \ DO_##kind(keytype) }; \ \ static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \ \ static void *kind##_der2##keytype##_newctx(void *provctx) \ { \ return der2key_newctx(provctx, &kind##_##keytype##_desc, keytype_name );\ } \ static int kind##_der2##keytype##_does_selection(void *provctx, \ int selection) \ { \ return der2key_check_selection(selection, \ &kind##_##keytype##_desc); \ } \ static const OSSL_DISPATCH \ xor_##kind##_der_to_##keytype##_decoder_functions[] = { \ { OSSL_FUNC_DECODER_NEWCTX, \ (void (*)(void))kind##_der2##keytype##_newctx }, \ { OSSL_FUNC_DECODER_FREECTX, \ (void (*)(void))der2key_freectx }, \ { OSSL_FUNC_DECODER_DOES_SELECTION, \ (void (*)(void))kind##_der2##keytype##_does_selection }, \ { OSSL_FUNC_DECODER_DECODE, \ (void (*)(void))xor_der2key_decode }, \ { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ (void (*)(void))der2key_export_object }, \ OSSL_DISPATCH_END \ } MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, PrivateKeyInfo); MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, SubjectPublicKeyInfo); MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, PrivateKeyInfo); MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, SubjectPublicKeyInfo); static const OSSL_ALGORITHM tls_prov_decoder[] = { #define DECODER_PROVIDER "tls-provider" #define DECODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo" #define DECODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo" #define DECODER(_name, _input, _output) \ { _name, \ "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input, \ (xor_##_input##_to_##_output##_decoder_functions) } #define DECODER_w_structure(_name, _input, _structure, _output) \ { _name, \ "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input \ ",structure=" DECODER_STRUCTURE_##_structure, \ (xor_##_structure##_##_input##_to_##_output##_decoder_functions) } DECODER_w_structure(XORSIGALG_NAME, der, PrivateKeyInfo, xorhmacsig), DECODER_w_structure(XORSIGALG_NAME, der, SubjectPublicKeyInfo, xorhmacsig), DECODER_w_structure(XORSIGALG_HASH_NAME, der, PrivateKeyInfo, xorhmacsha2sig), DECODER_w_structure(XORSIGALG_HASH_NAME, der, SubjectPublicKeyInfo, xorhmacsha2sig), #undef DECODER_PROVIDER { NULL, NULL, NULL } }; #define OSSL_MAX_NAME_SIZE 50 #define OSSL_MAX_PROPQUERY_SIZE 256 static OSSL_FUNC_signature_newctx_fn xor_sig_newctx; static OSSL_FUNC_signature_sign_init_fn xor_sig_sign_init; static OSSL_FUNC_signature_verify_init_fn xor_sig_verify_init; static OSSL_FUNC_signature_sign_fn xor_sig_sign; static OSSL_FUNC_signature_verify_fn xor_sig_verify; static OSSL_FUNC_signature_digest_sign_init_fn xor_sig_digest_sign_init; static OSSL_FUNC_signature_digest_sign_update_fn xor_sig_digest_signverify_update; static OSSL_FUNC_signature_digest_sign_final_fn xor_sig_digest_sign_final; static OSSL_FUNC_signature_digest_verify_init_fn xor_sig_digest_verify_init; static OSSL_FUNC_signature_digest_verify_update_fn xor_sig_digest_signverify_update; static OSSL_FUNC_signature_digest_verify_final_fn xor_sig_digest_verify_final; static OSSL_FUNC_signature_freectx_fn xor_sig_freectx; static OSSL_FUNC_signature_dupctx_fn xor_sig_dupctx; static OSSL_FUNC_signature_get_ctx_params_fn xor_sig_get_ctx_params; static OSSL_FUNC_signature_gettable_ctx_params_fn xor_sig_gettable_ctx_params; static OSSL_FUNC_signature_set_ctx_params_fn xor_sig_set_ctx_params; static OSSL_FUNC_signature_settable_ctx_params_fn xor_sig_settable_ctx_params; static OSSL_FUNC_signature_get_ctx_md_params_fn xor_sig_get_ctx_md_params; static OSSL_FUNC_signature_gettable_ctx_md_params_fn xor_sig_gettable_ctx_md_params; static OSSL_FUNC_signature_set_ctx_md_params_fn xor_sig_set_ctx_md_params; static OSSL_FUNC_signature_settable_ctx_md_params_fn xor_sig_settable_ctx_md_params; static int xor_get_aid(unsigned char** oidbuf, const char *tls_name) { X509_ALGOR *algor = X509_ALGOR_new(); int aidlen = 0; X509_ALGOR_set0(algor, OBJ_txt2obj(tls_name, 0), V_ASN1_UNDEF, NULL); aidlen = i2d_X509_ALGOR(algor, oidbuf); X509_ALGOR_free(algor); return(aidlen); } typedef struct { OSSL_LIB_CTX *libctx; char *propq; XORKEY *sig; unsigned int flag_allow_md : 1; char mdname[OSSL_MAX_NAME_SIZE]; unsigned char *aid; size_t aid_len; EVP_MD *md; EVP_MD_CTX *mdctx; int operation; } PROV_XORSIG_CTX; static void *xor_sig_newctx(void *provctx, const char *propq) { PROV_XORSIG_CTX *pxor_sigctx; pxor_sigctx = OPENSSL_zalloc(sizeof(PROV_XORSIG_CTX)); if (pxor_sigctx == NULL) return NULL; pxor_sigctx->libctx = ((PROV_XOR_CTX*)provctx)->libctx; pxor_sigctx->flag_allow_md = 0; if (propq != NULL && (pxor_sigctx->propq = OPENSSL_strdup(propq)) == NULL) { OPENSSL_free(pxor_sigctx); pxor_sigctx = NULL; ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); } return pxor_sigctx; } static int xor_sig_setup_md(PROV_XORSIG_CTX *ctx, const char *mdname, const char *mdprops) { EVP_MD *md; if (mdprops == NULL) mdprops = ctx->propq; md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); if ((md == NULL) || (EVP_MD_nid(md)==NID_undef)) { if (md == NULL) ERR_raise_data(ERR_LIB_USER, XORPROV_R_INVALID_DIGEST, "%s could not be fetched", mdname); EVP_MD_free(md); return 0; } EVP_MD_CTX_free(ctx->mdctx); ctx->mdctx = NULL; EVP_MD_free(ctx->md); ctx->md = NULL; OPENSSL_free(ctx->aid); ctx->aid = NULL; ctx->aid_len = xor_get_aid(&(ctx->aid), ctx->sig->tls_name); if (ctx->aid_len <= 0) { EVP_MD_free(md); return 0; } ctx->mdctx = NULL; ctx->md = md; OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); return 1; } static int xor_sig_signverify_init(void *vpxor_sigctx, void *vxorsig, int operation) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx == NULL || vxorsig == NULL) return 0; xor_freekey(pxor_sigctx->sig); if (!xor_key_up_ref(vxorsig)) return 0; pxor_sigctx->sig = vxorsig; pxor_sigctx->operation = operation; if ((operation==EVP_PKEY_OP_SIGN && pxor_sigctx->sig == NULL) || (operation==EVP_PKEY_OP_VERIFY && pxor_sigctx->sig == NULL)) { ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY); return 0; } return 1; } static int xor_sig_sign_init(void *vpxor_sigctx, void *vxorsig, const OSSL_PARAM params[]) { return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_SIGN); } static int xor_sig_verify_init(void *vpxor_sigctx, void *vxorsig, const OSSL_PARAM params[]) { return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_VERIFY); } static int xor_sig_sign(void *vpxor_sigctx, unsigned char *sig, size_t *siglen, size_t sigsize, const unsigned char *tbs, size_t tbslen) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; XORKEY *xorkey = pxor_sigctx->sig; size_t max_sig_len = EVP_MAX_MD_SIZE; size_t xor_sig_len = 0; int rv = 0; if (xorkey == NULL || !xorkey->hasprivkey) { ERR_raise(ERR_LIB_USER, XORPROV_R_NO_PRIVATE_KEY); return rv; } if (sig == NULL) { *siglen = max_sig_len; return 1; } if (*siglen < max_sig_len) { ERR_raise(ERR_LIB_USER, XORPROV_R_BUFFER_LENGTH_WRONG); return rv; } if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL, xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen, &sig[0], EVP_MAX_MD_SIZE, &xor_sig_len)) { ERR_raise(ERR_LIB_USER, XORPROV_R_SIGNING_FAILED); goto endsign; } *siglen = xor_sig_len; rv = 1; endsign: return rv; } static int xor_sig_verify(void *vpxor_sigctx, const unsigned char *sig, size_t siglen, const unsigned char *tbs, size_t tbslen) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; XORKEY *xorkey = pxor_sigctx->sig; unsigned char resignature[EVP_MAX_MD_SIZE]; size_t resiglen; int i; if (xorkey == NULL || sig == NULL || tbs == NULL) { ERR_raise(ERR_LIB_USER, XORPROV_R_WRONG_PARAMETERS); return 0; } for (i = 0; i < XOR_KEY_SIZE; i++) xorkey->privkey[i] = xorkey->pubkey[i] ^ private_constant[i]; if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL, xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen, &resignature[0], EVP_MAX_MD_SIZE, &resiglen)) { ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR); return 0; } if (siglen != resiglen || memcmp(resignature, sig, siglen) != 0) { ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR); return 0; } return 1; } static int xor_sig_digest_signverify_init(void *vpxor_sigctx, const char *mdname, void *vxorsig, int operation) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; char *rmdname = (char *)mdname; if (rmdname == NULL) rmdname = "sha256"; pxor_sigctx->flag_allow_md = 0; if (!xor_sig_signverify_init(vpxor_sigctx, vxorsig, operation)) return 0; if (!xor_sig_setup_md(pxor_sigctx, rmdname, NULL)) return 0; pxor_sigctx->mdctx = EVP_MD_CTX_new(); if (pxor_sigctx->mdctx == NULL) goto error; if (!EVP_DigestInit_ex(pxor_sigctx->mdctx, pxor_sigctx->md, NULL)) goto error; return 1; error: EVP_MD_CTX_free(pxor_sigctx->mdctx); EVP_MD_free(pxor_sigctx->md); pxor_sigctx->mdctx = NULL; pxor_sigctx->md = NULL; return 0; } static int xor_sig_digest_sign_init(void *vpxor_sigctx, const char *mdname, void *vxorsig, const OSSL_PARAM params[]) { return xor_sig_digest_signverify_init(vpxor_sigctx, mdname, vxorsig, EVP_PKEY_OP_SIGN); } static int xor_sig_digest_verify_init(void *vpxor_sigctx, const char *mdname, void *vxorsig, const OSSL_PARAM params[]) { return xor_sig_digest_signverify_init(vpxor_sigctx, mdname, vxorsig, EVP_PKEY_OP_VERIFY); } int xor_sig_digest_signverify_update(void *vpxor_sigctx, const unsigned char *data, size_t datalen) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) return 0; return EVP_DigestUpdate(pxor_sigctx->mdctx, data, datalen); } int xor_sig_digest_sign_final(void *vpxor_sigctx, unsigned char *sig, size_t *siglen, size_t sigsize) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int dlen = 0; if (sig != NULL) { if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) return 0; if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen)) return 0; pxor_sigctx->flag_allow_md = 1; } return xor_sig_sign(vpxor_sigctx, sig, siglen, sigsize, digest, (size_t)dlen); } int xor_sig_digest_verify_final(void *vpxor_sigctx, const unsigned char *sig, size_t siglen) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int dlen = 0; if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) return 0; if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen)) return 0; pxor_sigctx->flag_allow_md = 1; return xor_sig_verify(vpxor_sigctx, sig, siglen, digest, (size_t)dlen); } static void xor_sig_freectx(void *vpxor_sigctx) { PROV_XORSIG_CTX *ctx = (PROV_XORSIG_CTX *)vpxor_sigctx; OPENSSL_free(ctx->propq); EVP_MD_CTX_free(ctx->mdctx); EVP_MD_free(ctx->md); ctx->propq = NULL; ctx->mdctx = NULL; ctx->md = NULL; xor_freekey(ctx->sig); ctx->sig = NULL; OPENSSL_free(ctx->aid); OPENSSL_free(ctx); } static void *xor_sig_dupctx(void *vpxor_sigctx) { PROV_XORSIG_CTX *srcctx = (PROV_XORSIG_CTX *)vpxor_sigctx; PROV_XORSIG_CTX *dstctx; dstctx = OPENSSL_zalloc(sizeof(*srcctx)); if (dstctx == NULL) return NULL; *dstctx = *srcctx; dstctx->sig = NULL; dstctx->md = NULL; dstctx->mdctx = NULL; dstctx->aid = NULL; if ((srcctx->sig != NULL) && !xor_key_up_ref(srcctx->sig)) goto err; dstctx->sig = srcctx->sig; if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) goto err; dstctx->md = srcctx->md; if (srcctx->mdctx != NULL) { dstctx->mdctx = EVP_MD_CTX_new(); if (dstctx->mdctx == NULL || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) goto err; } return dstctx; err: xor_sig_freectx(dstctx); return NULL; } static int xor_sig_get_ctx_params(void *vpxor_sigctx, OSSL_PARAM *params) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; OSSL_PARAM *p; if (pxor_sigctx == NULL || params == NULL) return 0; p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); if (pxor_sigctx->aid == NULL) pxor_sigctx->aid_len = xor_get_aid(&(pxor_sigctx->aid), pxor_sigctx->sig->tls_name); if (p != NULL && !OSSL_PARAM_set_octet_string(p, pxor_sigctx->aid, pxor_sigctx->aid_len)) return 0; p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pxor_sigctx->mdname)) return 0; return 1; } static const OSSL_PARAM known_gettable_ctx_params[] = { OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *xor_sig_gettable_ctx_params(ossl_unused void *vpxor_sigctx, ossl_unused void *vctx) { return known_gettable_ctx_params; } static int xor_sig_set_ctx_params(void *vpxor_sigctx, const OSSL_PARAM params[]) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; const OSSL_PARAM *p; if (pxor_sigctx == NULL || params == NULL) return 0; p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); if (p != NULL && !pxor_sigctx->flag_allow_md) return 0; if (p != NULL) { char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; const OSSL_PARAM *propsp = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PROPERTIES); if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) return 0; if (propsp != NULL && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) return 0; if (!xor_sig_setup_md(pxor_sigctx, mdname, mdprops)) return 0; } return 1; } static const OSSL_PARAM known_settable_ctx_params[] = { OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *xor_sig_settable_ctx_params(ossl_unused void *vpsm2ctx, ossl_unused void *provctx) { return known_settable_ctx_params; } static int xor_sig_get_ctx_md_params(void *vpxor_sigctx, OSSL_PARAM *params) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx->mdctx == NULL) return 0; return EVP_MD_CTX_get_params(pxor_sigctx->mdctx, params); } static const OSSL_PARAM *xor_sig_gettable_ctx_md_params(void *vpxor_sigctx) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx->md == NULL) return 0; return EVP_MD_gettable_ctx_params(pxor_sigctx->md); } static int xor_sig_set_ctx_md_params(void *vpxor_sigctx, const OSSL_PARAM params[]) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx->mdctx == NULL) return 0; return EVP_MD_CTX_set_params(pxor_sigctx->mdctx, params); } static const OSSL_PARAM *xor_sig_settable_ctx_md_params(void *vpxor_sigctx) { PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; if (pxor_sigctx->md == NULL) return 0; return EVP_MD_settable_ctx_params(pxor_sigctx->md); } static const OSSL_DISPATCH xor_signature_functions[] = { { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))xor_sig_newctx }, { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))xor_sig_sign_init }, { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))xor_sig_sign }, { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))xor_sig_verify_init }, { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))xor_sig_verify }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void (*)(void))xor_sig_digest_sign_init }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void (*)(void))xor_sig_digest_signverify_update }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, (void (*)(void))xor_sig_digest_sign_final }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void (*)(void))xor_sig_digest_verify_init }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void (*)(void))xor_sig_digest_signverify_update }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, (void (*)(void))xor_sig_digest_verify_final }, { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))xor_sig_freectx }, { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))xor_sig_dupctx }, { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))xor_sig_get_ctx_params }, { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, (void (*)(void))xor_sig_gettable_ctx_params }, { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))xor_sig_set_ctx_params }, { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, (void (*)(void))xor_sig_settable_ctx_params }, { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, (void (*)(void))xor_sig_get_ctx_md_params }, { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, (void (*)(void))xor_sig_gettable_ctx_md_params }, { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, (void (*)(void))xor_sig_set_ctx_md_params }, { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, (void (*)(void))xor_sig_settable_ctx_md_params }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM tls_prov_signature[] = { { XORSIGALG_NAME, "provider=tls-provider,fips=yes", xor_signature_functions }, { XORSIGALG_HASH_NAME, "provider=tls-provider,fips=yes", xor_signature_functions }, { XORSIGALG12_NAME, "provider=tls-provider,fips=yes", xor_signature_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; switch (operation_id) { case OSSL_OP_KEYMGMT: return tls_prov_keymgmt; case OSSL_OP_KEYEXCH: return tls_prov_keyexch; case OSSL_OP_KEM: return tls_prov_kem; case OSSL_OP_ENCODER: return tls_prov_encoder; case OSSL_OP_DECODER: return tls_prov_decoder; case OSSL_OP_SIGNATURE: return tls_prov_signature; } return NULL; } static void tls_prov_teardown(void *provctx) { int i; PROV_XOR_CTX *pctx = (PROV_XOR_CTX*)provctx; OSSL_LIB_CTX_free(pctx->libctx); for (i = 0; i < NUM_DUMMY_GROUPS; i++) { OPENSSL_free(dummy_group_names[i]); dummy_group_names[i] = NULL; } OPENSSL_free(pctx); } static const OSSL_DISPATCH tls_prov_dispatch_table[] = { { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))tls_prov_teardown }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query }, { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities }, OSSL_DISPATCH_END }; static unsigned int randomize_tls_alg_id(OSSL_LIB_CTX *libctx) { unsigned int id; static unsigned int mem[10] = { 0 }; static int in_mem = 0; int i; retry: if (RAND_bytes_ex(libctx, (unsigned char *)&id, sizeof(id), 0) <= 0) return 0; id %= 65279 - NUM_DUMMY_GROUPS - 65024; id += 65024; for (i = 0; i < in_mem; i++) if (mem[i] == id) goto retry; mem[in_mem++] = id; return id; } int tls_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_from_dispatch(handle, in); OSSL_FUNC_core_obj_create_fn *c_obj_create= NULL; OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid= NULL; PROV_XOR_CTX *prov_ctx = xor_newprovctx(libctx); if (libctx == NULL || prov_ctx == NULL) return 0; *provctx = prov_ctx; xor_group.group_id = randomize_tls_alg_id(libctx); xor_kemgroup.group_id = randomize_tls_alg_id(libctx); xor_sigalg.code_point = randomize_tls_alg_id(libctx); xor_sigalg_hash.code_point = randomize_tls_alg_id(libctx); for (; in->function_id != 0; in++) { switch (in->function_id) { case OSSL_FUNC_CORE_OBJ_CREATE: c_obj_create = OSSL_FUNC_core_obj_create(in); break; case OSSL_FUNC_CORE_OBJ_ADD_SIGID: c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in); break; default: break; } } if (!c_obj_create(handle, XORSIGALG_OID, XORSIGALG_NAME, XORSIGALG_NAME)) { ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); return 0; } if (!c_obj_add_sigid(handle, XORSIGALG_OID, "", XORSIGALG_OID)) { ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); return 0; } if (!c_obj_create(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH_NAME, NULL)) { ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); return 0; } if (!c_obj_add_sigid(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH, XORSIGALG_HASH_OID)) { ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); return 0; } *out = tls_prov_dispatch_table; return 1; }
test
openssl/test/tls-provider.c
openssl
#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 #define RX_TEST_OP_SET_SCID_LEN 1 #define RX_TEST_OP_SET_INIT_LARGEST_PN 2 #define RX_TEST_OP_SET_RX_DCID 3 #define RX_TEST_OP_INJECT 4 #define RX_TEST_OP_PROVIDE_SECRET 5 #define RX_TEST_OP_PROVIDE_SECRET_INITIAL 6 #define RX_TEST_OP_DISCARD_EL 7 #define RX_TEST_OP_CHECK_PKT 8 #define RX_TEST_OP_CHECK_NO_PKT 9 #define RX_TEST_OP_CHECK_KEY_EPOCH 10 #define RX_TEST_OP_KEY_UPDATE_TIMEOUT 11 #define RX_TEST_OP_SET_INIT_KEY_PHASE 12 #define RX_TEST_OP_CHECK_PKT_EPOCH 13 #define RX_TEST_OP_ALLOW_1RTT 14 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); }; #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) 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 }; #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 static const unsigned char rx_script_3_in[] = { 0xc7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f, 0x00, 0x00, 0x00, 0x01, 0xaa, 0x9a, 0x3a, 0x9a }; static const QUIC_PKT_HDR rx_script_3_expect_hdr = { QUIC_PKT_TYPE_VERSION_NEG, 0, 0, 0, 0, 1, 0, 0, 0, {0, {0}}, {12, {0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f}}, {0}, NULL, 0, 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) RX_OP_INJECT_CHECK(3) RX_OP_CHECK_NO_PKT() RX_OP_END }; static const unsigned char rx_script_4_in[] = { 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0xad, 0x15, 0x3f, 0xae, 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, 0, 0, 0, 1, 0, 0, 1, {0, {0}}, {4, {0xad, 0x15, 0x3f, 0xae}}, {0}, NULL, 0, 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 }; 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[] = { 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x83, 0xd0, 0x0a, 0x27, 0x00, 0x41, 0xd2, 0xe3, 0xab, 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, 0xe6, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x83, 0xd0, 0x0a, 0x27, 0x42, 0x9c, 0x9c, 0x55, 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, 0x5f, 0x68, 0x47, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x83, 0xd0, 0x0a, 0x27}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x83, 0xd0, 0x0a, 0x27}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0}, NULL, 0, 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() 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_INJECT_N(5) RX_OP_CHECK_PKT_N(5b) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() RX_OP_INJECT_N(5) RX_OP_CHECK_PKT_N(5c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(5) RX_OP_CHECK_NO_PKT() 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 }; 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[] = { 0xc5, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x36, 0xf4, 0x75, 0x2d, 0x00, 0x41, 0xbe, 0xa9, 0xe2, 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, 0xea, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x36, 0xf4, 0x75, 0x2d, 0x42, 0xb0, 0x3a, 0xc5, 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, 0x48, 0x3e, 0x28, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x36, 0xf4, 0x75, 0x2d}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x36, 0xf4, 0x75, 0x2d}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0}, NULL, 0, 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() 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_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6b) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() RX_OP_INJECT_N(6) RX_OP_CHECK_PKT_N(6c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(6) RX_OP_CHECK_NO_PKT() 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 }; #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[] = { 0xc2, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x03, 0x45, 0x0c, 0x7a, 0x00, 0x41, 0xcb, 0x3c, 0xe0, 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, 0xeb, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x03, 0x45, 0x0c, 0x7a, 0x42, 0xa3, 0x43, 0x29, 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, 0x5c, 0x4f, 0x33, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x03, 0x45, 0x0c, 0x7a}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 1, {0, {0}}, {4, {0x03, 0x45, 0x0c, 0x7a}}, {0}, NULL, 0, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0}, NULL, 0, 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() 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_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7b) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() RX_OP_INJECT_N(7) RX_OP_CHECK_PKT_N(7c) RX_OP_CHECK_NO_PKT() RX_OP_DISCARD_EL(QUIC_ENC_LEVEL_1RTT) RX_OP_INJECT_N(7) RX_OP_CHECK_NO_PKT() 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 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, 0xcb, 0xf4, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 4}, NULL, 0, 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, 0x21, 0x8e, 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, 1, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 5}, NULL, 0, 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, 0x98, 0xd6, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 3}, NULL, 0, 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, 0x98, 0x20, 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, 1, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 6}, NULL, 0, 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, 0x76, 0x25, 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, 0, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 10}, NULL, 0, 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, 0x4d, 0xf6, 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, 1, 2, 0, 1, 0, 0, 0, {0, {0}}, {0, {0}}, {0, 15}, NULL, 0, 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) RX_OP_INJECT_N(8a) RX_OP_CHECK_NO_PKT() RX_OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, rx_script_8_1rtt_secret) RX_OP_CHECK_PKT_N(8a) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(0) RX_OP_CHECK_PKT_EPOCH(0) RX_OP_INJECT_N(8b) RX_OP_CHECK_PKT_N(8b) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) RX_OP_CHECK_PKT_EPOCH(1) 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) 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) 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) RX_OP_KEY_UPDATE_TIMEOUT(0) RX_OP_INJECT_N(8c) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) RX_OP_INJECT_N(8e) RX_OP_CHECK_NO_PKT() RX_OP_CHECK_KEY_EPOCH(1) 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) 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) RX_OP_KEY_UPDATE_TIMEOUT(1) 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 }; 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() 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_NO_PKT() RX_OP_ALLOW_1RTT() RX_OP_CHECK_PKT_N(5c) 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 *qrx; OSSL_QRX_ARGS args; 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; 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]); } 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; size_t min_success_len; size_t pn_offset, sample_offset; }; static const unsigned char pkt_hdr_test_1_expected[] = { 0xc1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x15, 0x33, 0x44, 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_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, 0, 0, 2, 0, 1, 0, 0, 1, { 0, {0} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33, 0x44 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_2_expected[] = { 0xc1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x07, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x15, 0x33, 0x44, 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_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, 0, 0, 2, 0, 1, 0, 0, 1, { 0, {0} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33, 0x44 }, pkt_hdr_test_2_token, sizeof(pkt_hdr_test_2_token), 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_3_expected[] = { 0xc1, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x06, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x15, 0x33, 0x44, 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_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, 0, 0, 2, 0, 1, 0, 0, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33, 0x44 }, pkt_hdr_test_3_token, sizeof(pkt_hdr_test_3_token), 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_4_expected[] = { 0xd0, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_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, 0, 0, 1, 0, 1, 0, 0, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_5_expected[] = { 0xe0, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_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, 0, 0, 1, 0, 1, 0, 0, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_6_expected[] = { 0xf0, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f }; static const unsigned char pkt_hdr_test_6_payload[] = { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f }; static const struct pkt_hdr_test pkt_hdr_test_6 = { { QUIC_PKT_TYPE_RETRY, 0, 0, 0, 0, 1, 0, 0, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0 }, NULL, 0, 24, NULL }, 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 }; static const unsigned char pkt_hdr_test_7_expected[] = { 0x42, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 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, 0, 0, 3, 0, 1, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 0, {0} }, { 0x50, 0x51, 0x52 }, NULL, 0, 18, NULL }, 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 }; static const unsigned char pkt_hdr_test_8_expected[] = { 0x62, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 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, 1, 0, 3, 0, 1, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 0, {0} }, { 0x50, 0x51, 0x52 }, NULL, 0, 18, NULL }, 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 }; static const unsigned char pkt_hdr_test_9_expected[] = { 0x46, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 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, 0, 1, 3, 0, 1, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 0, {0} }, { 0x50, 0x51, 0x52 }, NULL, 0, 18, NULL }, 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 }; static const unsigned char pkt_hdr_test_10_expected[] = { 0xe3, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x17, 0x33, 0x44, 0x55, 0x66, 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_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, 0, 0, 4, 0, 1, 0, 0, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33, 0x44, 0x55, 0x66 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_11_expected[] = { 0x43, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 0x53, 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, 0, 0, 4, 0, 1, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 0, {0} }, { 0x50, 0x51, 0x52, 0x53 }, NULL, 0, 18, NULL }, 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 }; static const unsigned char pkt_hdr_test_12_expected[] = { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x70, 0x71, 0x72, 0x02, 0x81, 0x82, 0x11, 0x22, 0x33, 0x44 }; 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, 0, 0, 0, 0, 1, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 2, {0x81, 0x82} }, { 0 }, NULL, 0, 4, NULL }, 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 }; static const unsigned char pkt_hdr_test_13_expected[] = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0x70, 0x71, 0x72, 0x02, 0x81, 0x82, 0x11, 0x22, 0x33, 0x44 }; 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, 0, 0, 0, 0, 0, 0, 0, 0, { 3, {0x70, 0x71, 0x72} }, { 2, {0x81, 0x82} }, { 0 }, NULL, 0, 4, NULL }, 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 }; static const unsigned char pkt_hdr_test_14_expected[] = { 0x02, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 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 }; static const unsigned char pkt_hdr_test_15_expected[] = { 0xa0, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_15 = { { 0 }, pkt_hdr_test_15_expected, OSSL_NELEM(pkt_hdr_test_15_expected), NULL, 0, 0, SIZE_MAX, 19, 23 }; static const unsigned char pkt_hdr_test_16_expected[] = { 0xe0, 0x00, 0x00, 0x00, 0x02, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_16 = { { 0 }, pkt_hdr_test_16_expected, OSSL_NELEM(pkt_hdr_test_16_expected), NULL, 0, 0, SIZE_MAX, 19, 23 }; static const unsigned char pkt_hdr_test_17_expected[] = { 0xcd, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x15, 0x33, 0x44, 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_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, 0, 0, 2, 0, 1, 0, 3, 1, { 0, {0} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33, 0x44 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_18_expected[] = { 0xd8, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_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, 0, 0, 1, 0, 1, 0, 2, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_19_expected[] = { 0xe4, 0x00, 0x00, 0x00, 0x01, 0x03, 0x70, 0x71, 0x72, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x14, 0x33, 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_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, 0, 0, 1, 0, 1, 0, 1, 1, { 3, {0x70, 0x71, 0x72} }, { 8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5 } }, { 0x33 }, NULL, 0, 19, NULL }, 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 }; static const unsigned char pkt_hdr_test_20_expected[] = { 0x5a, 0x70, 0x71, 0x72, 0x50, 0x51, 0x52, 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, 0, 0, 3, 0, 1, 0, 3, 0, { 3, {0x70, 0x71, 0x72} }, { 0, {0} }, { 0x50, 0x51, 0x52 }, NULL, 0, 18, NULL }, 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 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: #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; if (t->sample_offset != SIZE_MAX) { if (!TEST_true(ossl_quic_hdr_protector_init(&hpr, NULL, NULL, hpr_cipher_id, hpr_key, hpr_key_len))) goto err; have_hpr = 1; hbuf = OPENSSL_malloc(t->expected_len); if (!TEST_ptr(hbuf)) goto err; memcpy(hbuf, t->expected, t->expected_len); 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; 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) { 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) { 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) continue; OPENSSL_assert(jrel + j < OSSL_NELEM(counts_u[cipher])); if ((d & (1U << j)) != 0) ++counts_c[cipher][jrel + j]; else ++counts_u[cipher][jrel + j]; } if (!TEST_int_eq(d & rej_mask, 0)) goto err; } 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; if (!TEST_true(test_wire_pkt_hdr_actual(tidx, repeat, cipher, t->expected_len))) goto err; 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; 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); } #define TX_TEST_OP_END 0 #define TX_TEST_OP_WRITE 1 #define TX_TEST_OP_PROVIDE_SECRET 2 #define TX_TEST_OP_PROVIDE_SECRET_INITIAL 3 #define TX_TEST_OP_DISCARD_EL 4 #define TX_TEST_OP_CHECK_DGRAM 5 #define TX_TEST_OP_CHECK_NO_DGRAM 6 #define TX_TEST_OP_KEY_UPDATE 7 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 }, 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 }; 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, 0, 0, 4, 0, 0, 0, 0, 1, {8, {0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08}}, { 0, {0} }, { 0 }, NULL, 0, 5555, NULL }; 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 }; 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, 0, 0, 2, 0, 0, 0, 0, 1, { 0, {0} }, {8, {0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5}}, { 0 }, NULL, 0, 5555, NULL }; 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) 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, 0, 0, 3, 0, 0, 0, 0, 0, { 0, {0} }, { 0, {0} }, { 0 }, NULL, 0, 5555, NULL }; 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 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, 0, 0, 2, 0, 0, 0, 0, 0, { 4, {0x6e, 0x4e, 0xbd, 0x49} }, { 0, {0} }, { 0 }, NULL, 0, 5555, NULL }; 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, 0, 1, 2, 0, 0, 0, 0, 0, { 4, {0x6e, 0x4e, 0xbd, 0x49} }, { 0, {0} }, { 0 }, NULL, 0, 5555, NULL }; 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, 0, 0, 2, 0, 0, 0, 0, 0, { 4, {0x6e, 0x4e, 0xbd, 0x49} }, { 0, {0} }, { 0 }, NULL, 0, 5555, NULL }; 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 }; static const unsigned char tx_script_5_body[] = { 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 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, 0, 0, 0, 0, 0, 0, 0, 1, { 0, {0} }, { 4, {0xa9, 0x20, 0xcc, 0xc2} }, { 0 }, NULL, 0, 5555, NULL }; 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 }; static const unsigned char tx_script_6_body[] = { 0x00, 0x00, 0x00, 0x01, 0xaa, 0x9a, 0x3a, 0x9a }; static const unsigned char tx_script_6_dgram[] = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f, 0x00, 0x00, 0x00, 0x01, 0xaa, 0x9a, 0x3a, 0x9a }; static QUIC_PKT_HDR tx_script_6_hdr = { QUIC_PKT_TYPE_VERSION_NEG, 0, 0, 0, 0, 0, 0, 0, 0, { 0, {0} }, { 12, {0x35, 0x3c, 0x1b, 0x97, 0xca, 0xf8, 0x99, 0x11, 0x39, 0xad, 0x79, 0x1f} }, { 0 }, NULL, 0, 5555, NULL }; 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) { 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)); 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; }
test
openssl/test/quic_record_test.c
openssl
#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; 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); wherelen = sizeof(sa.sunaddr.sun_path) - 1; break; #endif default: TEST_error("Unsupported address family"); return 0; } 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; if (a == b) return 1; if (a == NULL || b == NULL) return 0; if (BIO_ADDR_family(a) != BIO_ADDR_family(b)) return 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; }
test
openssl/test/bio_addr_test.c
openssl
#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; }
test
openssl/test/constant_time_test.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <string.h> #include <openssl/evp.h> #include "testutil.h" static int test_asn1_meths(void) { int i; int prev = -1; int good = 1; int pkey_id; const EVP_PKEY_ASN1_METHOD *ameth; for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { ameth = EVP_PKEY_asn1_get0(i); EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); if (pkey_id < prev) good = 0; prev = pkey_id; } if (!good) { TEST_error("EVP_PKEY_ASN1_METHOD table out of order"); for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { const char *info; ameth = EVP_PKEY_asn1_get0(i); EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, &info, NULL, ameth); if (info == NULL) info = "<NO NAME>"; TEST_note("%d : %s : %s", pkey_id, OBJ_nid2ln(pkey_id), info); } } return good; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_pkey_meths(void) { size_t i; int prev = -1; int good = 1; int pkey_id; const EVP_PKEY_METHOD *pmeth; for (i = 0; i < EVP_PKEY_meth_get_count(); i++) { pmeth = EVP_PKEY_meth_get0(i); EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth); if (pkey_id < prev) good = 0; prev = pkey_id; } if (!good) { TEST_error("EVP_PKEY_METHOD table out of order"); for (i = 0; i < EVP_PKEY_meth_get_count(); i++) { pmeth = EVP_PKEY_meth_get0(i); EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth); TEST_note("%d : %s", pkey_id, OBJ_nid2ln(pkey_id)); } } return good; } #endif int setup_tests(void) { ADD_TEST(test_asn1_meths); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_pkey_meths); #endif return 1; }
test
openssl/test/pkey_meth_test.c
openssl
#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; }
test
openssl/test/pem_read_depr_test.c
openssl
#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; }
test
openssl/test/quic_srt_gen_test.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/e_os2.h> # include "testutil.h" #ifndef OPENSSL_NO_ENGINE # include <openssl/buffer.h> # include <openssl/crypto.h> # include <openssl/engine.h> # include <openssl/rsa.h> # include <openssl/err.h> # include <openssl/x509.h> # include <openssl/pem.h> static void display_engine_list(void) { ENGINE *h; int loop; loop = 0; for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) { TEST_info("#%d: id = \"%s\", name = \"%s\"", loop++, ENGINE_get_id(h), ENGINE_get_name(h)); } ENGINE_free(h); } #define NUMTOADD 512 static int test_engines(void) { ENGINE *block[NUMTOADD]; char *eid[NUMTOADD]; char *ename[NUMTOADD]; char buf[256]; ENGINE *ptr; int loop; int to_return = 0; ENGINE *new_h1 = NULL; ENGINE *new_h2 = NULL; ENGINE *new_h3 = NULL; ENGINE *new_h4 = NULL; memset(block, 0, sizeof(block)); if (!TEST_ptr(new_h1 = ENGINE_new()) || !TEST_true(ENGINE_set_id(new_h1, "test_id0")) || !TEST_true(ENGINE_set_name(new_h1, "First test item")) || !TEST_ptr(new_h2 = ENGINE_new()) || !TEST_true(ENGINE_set_id(new_h2, "test_id1")) || !TEST_true(ENGINE_set_name(new_h2, "Second test item")) || !TEST_ptr(new_h3 = ENGINE_new()) || !TEST_true(ENGINE_set_id(new_h3, "test_id2")) || !TEST_true(ENGINE_set_name(new_h3, "Third test item")) || !TEST_ptr(new_h4 = ENGINE_new()) || !TEST_true(ENGINE_set_id(new_h4, "test_id3")) || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item"))) goto end; TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_add(new_h1))) goto end; TEST_info("Engines:"); display_engine_list(); ptr = ENGINE_get_first(); if (!TEST_true(ENGINE_remove(ptr))) goto end; ENGINE_free(ptr); TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_add(new_h3)) || !TEST_true(ENGINE_add(new_h2))) goto end; TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_remove(new_h2))) goto end; TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_add(new_h4))) goto end; TEST_info("Engines:"); display_engine_list(); if (!TEST_false(ENGINE_add(new_h3))) goto end; ERR_clear_error(); if (!TEST_false(ENGINE_remove(new_h2))) goto end; ERR_clear_error(); if (!TEST_true(ENGINE_remove(new_h3))) goto end; TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_remove(new_h4))) goto end; TEST_info("Engines:"); display_engine_list(); if ((ptr = ENGINE_get_first()) != NULL) { if (!ENGINE_remove(ptr)) TEST_info("Remove failed - probably no hardware support present"); } ENGINE_free(ptr); TEST_info("Engines:"); display_engine_list(); if (!TEST_true(ENGINE_add(new_h1)) || !TEST_true(ENGINE_remove(new_h1))) goto end; TEST_info("About to beef up the engine-type list"); for (loop = 0; loop < NUMTOADD; loop++) { sprintf(buf, "id%d", loop); eid[loop] = OPENSSL_strdup(buf); sprintf(buf, "Fake engine type %d", loop); ename[loop] = OPENSSL_strdup(buf); if (!TEST_ptr(block[loop] = ENGINE_new()) || !TEST_true(ENGINE_set_id(block[loop], eid[loop])) || !TEST_true(ENGINE_set_name(block[loop], ename[loop]))) goto end; } for (loop = 0; loop < NUMTOADD; loop++) { if (!TEST_true(ENGINE_add(block[loop]))) { test_note("Adding stopped at %d, (%s,%s)", loop, ENGINE_get_id(block[loop]), ENGINE_get_name(block[loop])); goto cleanup_loop; } } cleanup_loop: TEST_info("About to empty the engine-type list"); while ((ptr = ENGINE_get_first()) != NULL) { if (!TEST_true(ENGINE_remove(ptr))) goto end; ENGINE_free(ptr); } for (loop = 0; loop < NUMTOADD; loop++) { OPENSSL_free(eid[loop]); OPENSSL_free(ename[loop]); } to_return = 1; end: ENGINE_free(new_h1); ENGINE_free(new_h2); ENGINE_free(new_h3); ENGINE_free(new_h4); for (loop = 0; loop < NUMTOADD; loop++) ENGINE_free(block[loop]); return to_return; } static EVP_PKEY_METHOD *test_rsa = NULL; static int called_encrypt = 0; static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen) { called_encrypt = 1; return 1; } static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth, const int **pnids, int nid) { static const int rnid = EVP_PKEY_RSA; if (pmeth == NULL) { *pnids = &rnid; return 1; } if (nid == EVP_PKEY_RSA) { *pmeth = test_rsa; return 1; } *pmeth = NULL; return 0; } static EVP_PKEY *get_test_pkey(void) { static unsigned char n[] = "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F" "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5" "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93" "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1" "\xF5"; static unsigned char e[] = "\x11"; RSA *rsa = RSA_new(); EVP_PKEY *pk = EVP_PKEY_new(); if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) { RSA_free(rsa); EVP_PKEY_free(pk); return NULL; } if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL), BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) { EVP_PKEY_free(pk); return NULL; } return pk; } static int test_redirect(void) { const unsigned char pt[] = "Hello World\n"; unsigned char *tmp = NULL; size_t len; EVP_PKEY_CTX *ctx = NULL; ENGINE *e = NULL; EVP_PKEY *pkey = NULL; int to_return = 0; if (!TEST_ptr(pkey = get_test_pkey())) goto err; len = EVP_PKEY_get_size(pkey); if (!TEST_ptr(tmp = OPENSSL_malloc(len))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))) goto err; TEST_info("EVP_PKEY_encrypt test: no redirection"); if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0) || !TEST_false(called_encrypt)) goto err; EVP_PKEY_CTX_free(ctx); ctx = NULL; if (!TEST_ptr(e = ENGINE_new()) || !TEST_true(ENGINE_set_id(e, "Test redirect engine")) || !TEST_true(ENGINE_set_name(e, "Test redirect engine"))) goto err; if (!TEST_ptr_null(ctx = EVP_PKEY_CTX_new(pkey, e)) || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0)) goto err; if (!TEST_ptr(test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0))) goto err; ENGINE_set_pkey_meths(e, test_pkey_meths); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e))) goto err; if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0)) goto err; EVP_PKEY_CTX_free(ctx); ctx = NULL; EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt); TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()"); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e))) goto err; if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0) || !TEST_true(called_encrypt)) goto err; EVP_PKEY_CTX_free(ctx); ctx = NULL; called_encrypt = 0; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)) || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0) || !TEST_false(called_encrypt)) goto err; EVP_PKEY_CTX_free(ctx); ctx = NULL; if (!TEST_true(EVP_PKEY_set1_engine(pkey, e))) goto err; TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()"); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)) || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0) || !TEST_true(called_encrypt)) goto err; to_return = 1; err: EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); ENGINE_free(e); OPENSSL_free(tmp); return to_return; } static int test_x509_dup_w_engine(void) { ENGINE *e = NULL; X509 *cert = NULL, *dupcert = NULL; X509_PUBKEY *pubkey, *duppubkey = NULL; int ret = 0; BIO *b = NULL; RSA_METHOD *rsameth = NULL; if (!TEST_ptr(b = BIO_new_file(test_get_argument(0), "r")) || !TEST_ptr(cert = PEM_read_bio_X509(b, NULL, NULL, NULL))) goto err; if (!TEST_ptr(dupcert = X509_dup(cert))) goto err; X509_free(dupcert); dupcert = NULL; if (!TEST_ptr(pubkey = X509_get_X509_PUBKEY(cert)) || !TEST_ptr(duppubkey = X509_PUBKEY_dup(pubkey)) || !TEST_ptr_ne(duppubkey, pubkey) || !TEST_ptr_ne(X509_PUBKEY_get0(duppubkey), X509_PUBKEY_get0(pubkey))) goto err; X509_PUBKEY_free(duppubkey); duppubkey = NULL; X509_free(cert); cert = NULL; if (!TEST_ptr(e = ENGINE_new()) || !TEST_true(ENGINE_set_id(e, "Test dummy engine")) || !TEST_true(ENGINE_set_name(e, "Test dummy engine"))) goto err; if (!TEST_ptr(rsameth = RSA_meth_dup(RSA_get_default_method()))) goto err; ENGINE_set_RSA(e, rsameth); if (!TEST_true(ENGINE_set_default_RSA(e))) goto err; if (!TEST_int_ge(BIO_seek(b, 0), 0) || !TEST_ptr(cert = PEM_read_bio_X509(b, NULL, NULL, NULL))) goto err; if (!TEST_ptr(dupcert = X509_dup(cert))) goto err; if (!TEST_ptr(pubkey = X509_get_X509_PUBKEY(cert)) || !TEST_ptr(duppubkey = X509_PUBKEY_dup(pubkey)) || !TEST_ptr_ne(duppubkey, pubkey) || !TEST_ptr_ne(X509_PUBKEY_get0(duppubkey), X509_PUBKEY_get0(pubkey))) goto err; ret = 1; err: X509_free(cert); X509_free(dupcert); X509_PUBKEY_free(duppubkey); if (e != NULL) { ENGINE_unregister_RSA(e); ENGINE_free(e); } RSA_meth_free(rsameth); BIO_free(b); return ret; } #endif int global_init(void) { return OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL); } OPT_TEST_DECLARE_USAGE("certfile\n") int setup_tests(void) { #ifdef OPENSSL_NO_ENGINE TEST_note("No ENGINE support"); #else int n; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } n = test_get_argument_count(); if (n == 0) return 0; ADD_TEST(test_engines); ADD_TEST(test_redirect); ADD_TEST(test_x509_dup_w_engine); #endif return 1; }
test
openssl/test/enginetest.c
openssl
#ifndef OPENSSL_NO_DEPRECATED_3_0 # define OPENSSL_SUPPRESS_DEPRECATED #endif #include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/ssl.h> #include <openssl/ocsp.h> #include <openssl/srp.h> #include <openssl/txt_db.h> #include <openssl/aes.h> #include <openssl/rand.h> #include <openssl/core_names.h> #include <openssl/core_dispatch.h> #include <openssl/provider.h> #include <openssl/param_build.h> #include <openssl/x509v3.h> #include <openssl/dh.h> #include <openssl/engine.h> #include "helpers/ssltestlib.h" #include "testutil.h" #include "testutil/output.h" #include "internal/nelem.h" #include "internal/tlsgroups.h" #include "internal/ktls.h" #include "../ssl/ssl_local.h" #include "../ssl/record/methods/recmethod_local.h" #include "filterprov.h" #undef OSSL_NO_USABLE_TLS1_3 #if defined(OPENSSL_NO_TLS1_3) \ || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)) # define OSSL_NO_USABLE_TLS1_3 #endif int tls_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx); static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *defctxnull = NULL; #ifndef OSSL_NO_USABLE_TLS1_3 static SSL_SESSION *clientpsk = NULL; static SSL_SESSION *serverpsk = NULL; static const char *pskid = "Identity"; static const char *srvid; static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, size_t *idlen, SSL_SESSION **sess); static int find_session_cb(SSL *ssl, const unsigned char *identity, size_t identity_len, SSL_SESSION **sess); static int use_session_cb_cnt = 0; static int find_session_cb_cnt = 0; #endif static char *certsdir = NULL; static char *cert = NULL; static char *privkey = NULL; static char *cert2 = NULL; static char *privkey2 = NULL; static char *cert1024 = NULL; static char *privkey1024 = NULL; static char *cert3072 = NULL; static char *privkey3072 = NULL; static char *cert4096 = NULL; static char *privkey4096 = NULL; static char *cert8192 = NULL; static char *privkey8192 = NULL; static char *srpvfile = NULL; static char *tmpfilename = NULL; static char *dhfile = NULL; static int is_fips = 0; static int fips_ems_check = 0; #define LOG_BUFFER_SIZE 2048 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; static size_t server_log_buffer_index = 0; static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; static size_t client_log_buffer_index = 0; static int error_writing_log = 0; #ifndef OPENSSL_NO_OCSP static const unsigned char orespder[] = "Dummy OCSP Response"; static int ocsp_server_called = 0; static int ocsp_client_called = 0; static int cdummyarg = 1; static X509 *ocspcert = NULL; #endif #define CLIENT_VERSION_LEN 2 struct sslapitest_log_counts { unsigned int rsa_key_exchange_count; unsigned int master_secret_count; unsigned int client_early_secret_count; unsigned int client_handshake_secret_count; unsigned int server_handshake_secret_count; unsigned int client_application_secret_count; unsigned int server_application_secret_count; unsigned int early_exporter_secret_count; unsigned int exporter_secret_count; }; static int hostname_cb(SSL *s, int *al, void *arg) { const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (hostname != NULL && (strcmp(hostname, "goodhost") == 0 || strcmp(hostname, "altgoodhost") == 0)) return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_NOACK; } static void client_keylog_callback(const SSL *ssl, const char *line) { int line_length = strlen(line); if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) { TEST_info("Client log too full"); error_writing_log = 1; return; } strcat(client_log_buffer, line); client_log_buffer_index += line_length; client_log_buffer[client_log_buffer_index++] = '\n'; } static void server_keylog_callback(const SSL *ssl, const char *line) { int line_length = strlen(line); if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) { TEST_info("Server log too full"); error_writing_log = 1; return; } strcat(server_log_buffer, line); server_log_buffer_index += line_length; server_log_buffer[server_log_buffer_index++] = '\n'; } static int compare_hex_encoded_buffer(const char *hex_encoded, size_t hex_length, const uint8_t *raw, size_t raw_length) { size_t i, j; char hexed[3]; if (!TEST_size_t_eq(raw_length * 2, hex_length)) return 1; for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) { sprintf(hexed, "%02x", raw[i]); if (!TEST_int_eq(hexed[0], hex_encoded[j]) || !TEST_int_eq(hexed[1], hex_encoded[j + 1])) return 1; } return 0; } static int test_keylog_output(char *buffer, const SSL *ssl, const SSL_SESSION *session, struct sslapitest_log_counts *expected) { char *token = NULL; unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0}; size_t client_random_size = SSL3_RANDOM_SIZE; unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0}; size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH; unsigned int rsa_key_exchange_count = 0; unsigned int master_secret_count = 0; unsigned int client_early_secret_count = 0; unsigned int client_handshake_secret_count = 0; unsigned int server_handshake_secret_count = 0; unsigned int client_application_secret_count = 0; unsigned int server_application_secret_count = 0; unsigned int early_exporter_secret_count = 0; unsigned int exporter_secret_count = 0; for (token = strtok(buffer, " \n"); token != NULL; token = strtok(NULL, " \n")) { if (strcmp(token, "RSA") == 0) { if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; if (!TEST_size_t_eq(strlen(token), 16)) return 0; if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; rsa_key_exchange_count++; } else if (strcmp(token, "CLIENT_RANDOM") == 0) { client_random_size = SSL_get_client_random(ssl, actual_client_random, SSL3_RANDOM_SIZE); if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) return 0; if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; if (!TEST_size_t_eq(strlen(token), 64)) return 0; if (!TEST_false(compare_hex_encoded_buffer(token, 64, actual_client_random, client_random_size))) return 0; if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; master_key_size = SSL_SESSION_get_master_key(session, actual_master_key, master_key_size); if (!TEST_size_t_ne(master_key_size, 0)) return 0; if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token), actual_master_key, master_key_size))) return 0; master_secret_count++; } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0 || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0 || strcmp(token, "EARLY_EXPORTER_SECRET") == 0 || strcmp(token, "EXPORTER_SECRET") == 0) { if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0) client_early_secret_count++; else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0) client_handshake_secret_count++; else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0) server_handshake_secret_count++; else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0) client_application_secret_count++; else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) server_application_secret_count++; else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0) early_exporter_secret_count++; else if (strcmp(token, "EXPORTER_SECRET") == 0) exporter_secret_count++; client_random_size = SSL_get_client_random(ssl, actual_client_random, SSL3_RANDOM_SIZE); if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) return 0; if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; if (!TEST_size_t_eq(strlen(token), 64)) return 0; if (!TEST_false(compare_hex_encoded_buffer(token, 64, actual_client_random, client_random_size))) return 0; if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; } else { TEST_info("Unexpected token %s\n", token); return 0; } } if (!TEST_size_t_eq(rsa_key_exchange_count, expected->rsa_key_exchange_count) || !TEST_size_t_eq(master_secret_count, expected->master_secret_count) || !TEST_size_t_eq(client_early_secret_count, expected->client_early_secret_count) || !TEST_size_t_eq(client_handshake_secret_count, expected->client_handshake_secret_count) || !TEST_size_t_eq(server_handshake_secret_count, expected->server_handshake_secret_count) || !TEST_size_t_eq(client_application_secret_count, expected->client_application_secret_count) || !TEST_size_t_eq(server_application_secret_count, expected->server_application_secret_count) || !TEST_size_t_eq(early_exporter_secret_count, expected->early_exporter_secret_count) || !TEST_size_t_eq(exporter_secret_count, expected->exporter_secret_count)) return 0; return 1; } #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3) static int test_keylog(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; struct sslapitest_log_counts expected; memset(&expected, 0, sizeof(expected)); memset(client_log_buffer, 0, sizeof(client_log_buffer)); memset(server_log_buffer, 0, sizeof(server_log_buffer)); client_log_buffer_index = 0; server_log_buffer_index = 0; error_writing_log = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3); SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3); if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA"))) goto end; if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL) || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL)) goto end; SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == client_keylog_callback)) goto end; SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); if (!TEST_true(SSL_CTX_get_keylog_callback(sctx) == server_keylog_callback)) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(error_writing_log) || !TEST_int_gt(client_log_buffer_index, 0) || !TEST_int_gt(server_log_buffer_index, 0)) goto end; expected.rsa_key_exchange_count = 1; expected.master_secret_count = 1; if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, SSL_get_session(clientssl), &expected))) goto end; expected.rsa_key_exchange_count = 0; if (!TEST_true(test_keylog_output(server_log_buffer, serverssl, SSL_get_session(serverssl), &expected))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif #ifndef OSSL_NO_USABLE_TLS1_3 static int test_keylog_no_master_key(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *sess = NULL; int testresult = 0; struct sslapitest_log_counts expected; unsigned char buf[1]; size_t readbytes, written; memset(&expected, 0, sizeof(expected)); memset(client_log_buffer, 0, sizeof(client_log_buffer)); memset(server_log_buffer, 0, sizeof(server_log_buffer)); client_log_buffer_index = 0; server_log_buffer_index = 0; error_writing_log = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(SSL_CTX_set_max_early_data(sctx, SSL3_RT_MAX_PLAIN_LENGTH))) return 0; if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL) || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL)) goto end; SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == client_keylog_callback)) goto end; SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); if (!TEST_true(SSL_CTX_get_keylog_callback(sctx) == server_keylog_callback)) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(error_writing_log)) goto end; expected.client_handshake_secret_count = 1; expected.server_handshake_secret_count = 1; expected.client_application_secret_count = 1; expected.server_application_secret_count = 1; expected.exporter_secret_count = 1; if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, SSL_get_session(clientssl), &expected)) || !TEST_true(test_keylog_output(server_log_buffer, serverssl, SSL_get_session(serverssl), &expected))) goto end; sess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; memset(client_log_buffer, 0, sizeof(client_log_buffer)); memset(server_log_buffer, 0, sizeof(server_log_buffer)); client_log_buffer_index = 0; server_log_buffer_index = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sess)) || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_ERROR) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl))) goto end; expected.client_early_secret_count = 1; expected.early_exporter_secret_count = 1; if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, SSL_get_session(clientssl), &expected)) || !TEST_true(test_keylog_output(server_log_buffer, serverssl, SSL_get_session(serverssl), &expected))) 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 static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg) { int res = X509_verify_cert(ctx); int idx = SSL_get_ex_data_X509_STORE_CTX_idx(); SSL *ssl; if (idx < 0 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL) return 0; if (res == 0 && X509_STORE_CTX_get_error(ctx) == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) return SSL_set_retry_verify(ssl); return res; } static int test_client_cert_verify_cb(void) { char *skey = test_mk_file_path(certsdir, "leaf.key"); char *leaf = test_mk_file_path(certsdir, "leaf.pem"); char *int2 = test_mk_file_path(certsdir, "subinterCA.pem"); char *int1 = test_mk_file_path(certsdir, "interCA.pem"); char *root = test_mk_file_path(certsdir, "rootCA.pem"); X509 *crt1 = NULL, *crt2 = NULL; STACK_OF(X509) *server_chain; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, NULL, NULL))) goto end; if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1) || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)) goto end; if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL))) goto end; SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL); SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_RETRY_VERIFY))) goto end; if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx))) || !TEST_ptr((crt2 = load_cert_pem(int2, libctx))) || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl)))) goto end; if (!TEST_true(sk_X509_push(server_chain, crt1))) goto end; crt1 = NULL; if (!TEST_true(sk_X509_push(server_chain, crt2))) goto end; crt2 = NULL; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; testresult = 1; end: X509_free(crt1); X509_free(crt2); if (clientssl != NULL) { SSL_shutdown(clientssl); SSL_free(clientssl); } if (serverssl != NULL) { SSL_shutdown(serverssl); SSL_free(serverssl); } SSL_CTX_free(sctx); SSL_CTX_free(cctx); OPENSSL_free(skey); OPENSSL_free(leaf); OPENSSL_free(int2); OPENSSL_free(int1); OPENSSL_free(root); return testresult; } static int test_ssl_build_cert_chain(void) { int ret = 0; SSL_CTX *ssl_ctx = NULL; SSL *ssl = NULL; char *skey = test_mk_file_path(certsdir, "leaf.key"); char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem"); if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()))) goto end; if (!TEST_ptr(ssl = SSL_new(ssl_ctx))) goto end; if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1) || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_check_private_key(ssl), 1)) goto end; if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT | SSL_BUILD_CHAIN_FLAG_CHECK))) goto end; ret = 1; end: SSL_free(ssl); SSL_CTX_free(ssl_ctx); OPENSSL_free(leaf_chain); OPENSSL_free(skey); return ret; } static int get_password_cb(char *buf, int size, int rw_flag, void *userdata) { static const char pass[] = "testpass"; if (!TEST_int_eq(size, PEM_BUFSIZE)) return -1; memcpy(buf, pass, sizeof(pass) - 1); return sizeof(pass) - 1; } static int test_ssl_ctx_build_cert_chain(void) { int ret = 0; SSL_CTX *ctx = NULL; char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key"); char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem"); if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()))) goto end; SSL_CTX_set_default_passwd_cb(ctx, get_password_cb); if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1) || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1)) goto end; if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT | SSL_BUILD_CHAIN_FLAG_CHECK))) goto end; ret = 1; end: SSL_CTX_free(ctx); OPENSSL_free(leaf_chain); OPENSSL_free(skey); return ret; } #ifndef OPENSSL_NO_TLS1_2 static int full_client_hello_callback(SSL *s, int *al, void *arg) { int *ctr = arg; const unsigned char *p; int *exts; #ifdef OPENSSL_NO_EC const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff}; #else const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0, 0x2c, 0x00, 0xff}; #endif const int expected_extensions[] = { #ifndef OPENSSL_NO_EC 11, 10, #endif 35, 22, 23, 13}; size_t len; if ((*ctr)++ == 0) return SSL_CLIENT_HELLO_RETRY; len = SSL_client_hello_get0_ciphers(s, &p); if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers)) || !TEST_size_t_eq( SSL_client_hello_get0_compression_methods(s, &p), 1) || !TEST_int_eq(*p, 0)) return SSL_CLIENT_HELLO_ERROR; if (!SSL_client_hello_get1_extensions_present(s, &exts, &len)) return SSL_CLIENT_HELLO_ERROR; if (len != OSSL_NELEM(expected_extensions) || memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) { printf("ClientHello callback expected extensions mismatch\n"); OPENSSL_free(exts); return SSL_CLIENT_HELLO_ERROR; } OPENSSL_free(exts); return SSL_CLIENT_HELLO_SUCCESS; } static int test_client_hello_cb(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testctr = 0, testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr); SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_CLIENT_HELLO_CB)) || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_CLIENT_HELLO_CB) || !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_no_ems(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, status; if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, TLS1_2_VERSION, &sctx, &cctx, cert, privkey)) { printf("Unable to create SSL_CTX pair\n"); goto end; } SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET); if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { printf("Unable to create SSL objects\n"); goto end; } status = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); if (fips_ems_check) { if (status == 1) { printf("When FIPS uses the EMS check a connection that doesn't use EMS should fail\n"); goto end; } } else { if (!status) { printf("Creating SSL connection failed\n"); goto end; } if (SSL_get_extms_support(serverssl)) { printf("Server reports Extended Master Secret support\n"); goto end; } if (SSL_get_extms_support(clientssl)) { printf("Client reports Extended Master Secret support\n"); goto end; } } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_ccs_change_cipher(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *sess = NULL, *sesspre, *sesspost; int testresult = 0; int i; unsigned char buf; size_t readbytes; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, TLS1_2_VERSION, &sctx, &cctx, cert, privkey)) || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256")) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_ptr(sesspre = SSL_get0_session(serverssl)) || !TEST_ptr(sess = SSL_get1_session(clientssl))) goto end; shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sess)) || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256")) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl)) || !TEST_true(SSL_session_reused(serverssl)) || !TEST_ptr(sesspost = SSL_get0_session(serverssl)) || !TEST_ptr_eq(sesspre, sesspost) || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl)))) goto end; shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256")) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_ptr(sesspre = SSL_get0_session(serverssl)) || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")) || !TEST_true(SSL_renegotiate(clientssl)) || !TEST_true(SSL_renegotiate_pending(clientssl))) goto end; for (i = 0; i < 3; i++) { if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) { if (!TEST_ulong_eq(readbytes, 0)) goto end; } else if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_READ)) { goto end; } if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) { if (!TEST_ulong_eq(readbytes, 0)) goto end; } else if (!TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_READ)) { goto end; } } if (!TEST_false(SSL_renegotiate_pending(clientssl)) || !TEST_false(SSL_session_reused(clientssl)) || !TEST_false(SSL_session_reused(serverssl)) || !TEST_ptr(sesspost = SSL_get0_session(serverssl)) || !TEST_ptr_ne(sesspre, sesspost) || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl)))) goto end; shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); SSL_SESSION_free(sess); return testresult; } #endif static int execute_test_large_message(const SSL_METHOD *smeth, const SSL_METHOD *cmeth, int min_version, int max_version, int read_ahead) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version, max_version, &sctx, &cctx, cert, privkey))) goto end; #ifdef OPENSSL_NO_DTLS1_2 if (smeth == DTLS_server_method()) { 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 (read_ahead) { SSL_CTX_set_read_ahead(cctx, 1); } if (!ssl_ctx_add_large_cert_chain(libctx, sctx, cert)) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_clear(serverssl))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \ !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2)) static int ktls_chk_platform(int sock) { if (!ktls_enable(sock)) return 0; return 1; } static int ping_pong_query(SSL *clientssl, SSL *serverssl) { static char count = 1; unsigned char cbuf[16000] = {0}; unsigned char sbuf[16000]; size_t err = 0; char crec_wseq_before[SEQ_NUM_SIZE]; char crec_wseq_after[SEQ_NUM_SIZE]; char crec_rseq_before[SEQ_NUM_SIZE]; char crec_rseq_after[SEQ_NUM_SIZE]; char srec_wseq_before[SEQ_NUM_SIZE]; char srec_wseq_after[SEQ_NUM_SIZE]; char srec_rseq_before[SEQ_NUM_SIZE]; char srec_rseq_after[SEQ_NUM_SIZE]; SSL_CONNECTION *clientsc, *serversc; if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl)) || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl))) goto end; cbuf[0] = count++; memcpy(crec_wseq_before, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE); memcpy(srec_wseq_before, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE); memcpy(crec_rseq_before, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE); memcpy(srec_rseq_before, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE); if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf))) goto end; while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) { if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) { goto end; } } if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf))) goto end; while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) { if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) { goto end; } } memcpy(crec_wseq_after, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE); memcpy(srec_wseq_after, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE); memcpy(crec_rseq_after, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE); memcpy(srec_rseq_after, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE); if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf))) goto end; if (!BIO_get_ktls_send(clientsc->wbio)) { if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE, crec_wseq_after, SEQ_NUM_SIZE)) goto end; } else { if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE, crec_wseq_after, SEQ_NUM_SIZE)) goto end; } if (!BIO_get_ktls_send(serversc->wbio)) { if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE, srec_wseq_after, SEQ_NUM_SIZE)) goto end; } else { if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE, srec_wseq_after, SEQ_NUM_SIZE)) goto end; } if (!BIO_get_ktls_recv(clientsc->wbio)) { if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE, crec_rseq_after, SEQ_NUM_SIZE)) goto end; } else { if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE, crec_rseq_after, SEQ_NUM_SIZE)) goto end; } if (!BIO_get_ktls_recv(serversc->wbio)) { if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE, srec_rseq_after, SEQ_NUM_SIZE)) goto end; } else { if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE, srec_rseq_after, SEQ_NUM_SIZE)) goto end; } return 1; end: return 0; } static int execute_test_ktls(int cis_ktls, int sis_ktls, int tls_version, const char *cipher) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int ktls_used = 0, testresult = 0; int cfd = -1, sfd = -1; int rx_supported; SSL_CONNECTION *clientsc, *serversc; unsigned char *buf = NULL; const size_t bufsz = SSL3_RT_MAX_PLAIN_LENGTH + 16; int ret; size_t offset = 0, i; if (!TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL))) goto end; if (!ktls_chk_platform(cfd)) { testresult = TEST_skip("Kernel does not support KTLS"); goto end; } if (is_fips && strstr(cipher, "CHACHA") != NULL) { testresult = TEST_skip("CHACHA is not supported in FIPS"); goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), tls_version, tls_version, &sctx, &cctx, cert, privkey))) goto end; if (tls_version == TLS1_3_VERSION) { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher)) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher))) goto end; } else { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher)) || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher))) goto end; } if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl, &clientssl, sfd, cfd))) goto end; if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl)) || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl))) goto end; if (cis_ktls) { if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS))) goto end; } if (sis_ktls) { if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS))) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!cis_ktls) { if (!TEST_false(BIO_get_ktls_send(clientsc->wbio))) goto end; } else { if (BIO_get_ktls_send(clientsc->wbio)) ktls_used = 1; } if (!sis_ktls) { if (!TEST_false(BIO_get_ktls_send(serversc->wbio))) goto end; } else { if (BIO_get_ktls_send(serversc->wbio)) ktls_used = 1; } #if defined(OPENSSL_NO_KTLS_RX) rx_supported = 0; #else rx_supported = 1; #endif if (!cis_ktls || !rx_supported) { if (!TEST_false(BIO_get_ktls_recv(clientsc->rbio))) goto end; } else { if (BIO_get_ktls_send(clientsc->rbio)) ktls_used = 1; } if (!sis_ktls || !rx_supported) { if (!TEST_false(BIO_get_ktls_recv(serversc->rbio))) goto end; } else { if (BIO_get_ktls_send(serversc->rbio)) ktls_used = 1; } if ((cis_ktls || sis_ktls) && !ktls_used) { testresult = TEST_skip("KTLS not supported for %s cipher %s", tls_version == TLS1_3_VERSION ? "TLS 1.3" : "TLS 1.2", cipher); goto end; } if (!TEST_true(ping_pong_query(clientssl, serverssl))) goto end; buf = OPENSSL_zalloc(bufsz); if (!TEST_ptr(buf)) goto end; while ((ret = SSL_write(clientssl, buf, bufsz)) != (int)bufsz) { if (!TEST_true(SSL_get_error(clientssl, ret) == SSL_ERROR_WANT_WRITE)) goto end; } do { ret = SSL_read(serverssl, buf + offset, bufsz - offset); if (ret <= 0) { if (!TEST_true(SSL_get_error(serverssl, ret) == SSL_ERROR_WANT_READ)) goto end; } else { offset += ret; } } while (offset < bufsz); if (!TEST_true(offset == bufsz)) goto end; for (i = 0; i < bufsz; i++) if (!TEST_true(buf[i] == 0)) goto end; testresult = 1; end: OPENSSL_free(buf); if (clientssl) { SSL_shutdown(clientssl); SSL_free(clientssl); } if (serverssl) { SSL_shutdown(serverssl); SSL_free(serverssl); } SSL_CTX_free(sctx); SSL_CTX_free(cctx); serverssl = clientssl = NULL; if (cfd != -1) close(cfd); if (sfd != -1) close(sfd); return testresult; } #define SENDFILE_SZ (16 * 4096) #define SENDFILE_CHUNK (4 * 4096) #define min(a,b) ((a) > (b) ? (b) : (a)) static int execute_test_ktls_sendfile(int tls_version, const char *cipher, int zerocopy) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; unsigned char *buf, *buf_dst; BIO *out = NULL, *in = NULL; int cfd = -1, sfd = -1, ffd, err; ssize_t chunk_size = 0; off_t chunk_off = 0; int testresult = 0; FILE *ffdp; SSL_CONNECTION *serversc; buf = OPENSSL_zalloc(SENDFILE_SZ); buf_dst = OPENSSL_zalloc(SENDFILE_SZ); if (!TEST_ptr(buf) || !TEST_ptr(buf_dst) || !TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL))) goto end; if (!ktls_chk_platform(sfd)) { testresult = TEST_skip("Kernel does not support KTLS"); goto end; } if (is_fips && strstr(cipher, "CHACHA") != NULL) { testresult = TEST_skip("CHACHA is not supported in FIPS"); goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), tls_version, tls_version, &sctx, &cctx, cert, privkey))) goto end; if (tls_version == TLS1_3_VERSION) { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher)) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher))) goto end; } else { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher)) || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher))) goto end; } if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl, &clientssl, sfd, cfd))) goto end; if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl))) goto end; if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS))) goto end; if (zerocopy) { if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE))) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!BIO_get_ktls_send(serversc->wbio)) { testresult = TEST_skip("Failed to enable KTLS for %s cipher %s", tls_version == TLS1_3_VERSION ? "TLS 1.3" : "TLS 1.2", cipher); goto end; } if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0)) goto end; out = BIO_new_file(tmpfilename, "wb"); if (!TEST_ptr(out)) goto end; if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ) goto end; BIO_free(out); out = NULL; in = BIO_new_file(tmpfilename, "rb"); BIO_get_fp(in, &ffdp); ffd = fileno(ffdp); while (chunk_off < SENDFILE_SZ) { chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off); while ((err = SSL_sendfile(serverssl, ffd, chunk_off, chunk_size, 0)) != chunk_size) { if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE) goto end; } while ((err = SSL_read(clientssl, buf_dst + chunk_off, chunk_size)) != chunk_size) { if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) goto end; } if (!TEST_mem_eq(buf_dst + chunk_off, chunk_size, buf + chunk_off, chunk_size)) goto end; chunk_off += chunk_size; } testresult = 1; end: if (clientssl) { SSL_shutdown(clientssl); SSL_free(clientssl); } if (serverssl) { SSL_shutdown(serverssl); SSL_free(serverssl); } SSL_CTX_free(sctx); SSL_CTX_free(cctx); serverssl = clientssl = NULL; BIO_free(out); BIO_free(in); if (cfd != -1) close(cfd); if (sfd != -1) close(sfd); OPENSSL_free(buf); OPENSSL_free(buf_dst); return testresult; } static struct ktls_test_cipher { int tls_version; const char *cipher; } ktls_test_ciphers[] = { # if !defined(OPENSSL_NO_TLS1_2) # ifdef OPENSSL_KTLS_AES_GCM_128 { TLS1_2_VERSION, "AES128-GCM-SHA256" }, # endif # ifdef OPENSSL_KTLS_AES_CCM_128 { TLS1_2_VERSION, "AES128-CCM"}, # endif # ifdef OPENSSL_KTLS_AES_GCM_256 { TLS1_2_VERSION, "AES256-GCM-SHA384"}, # endif # ifdef OPENSSL_KTLS_CHACHA20_POLY1305 # ifndef OPENSSL_NO_EC { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"}, # endif # endif # endif # if !defined(OSSL_NO_USABLE_TLS1_3) # ifdef OPENSSL_KTLS_AES_GCM_128 { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" }, # endif # ifdef OPENSSL_KTLS_AES_CCM_128 { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" }, # endif # ifdef OPENSSL_KTLS_AES_GCM_256 { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" }, # endif # ifdef OPENSSL_KTLS_CHACHA20_POLY1305 { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" }, # endif # endif }; #define NUM_KTLS_TEST_CIPHERS \ (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0])) static int test_ktls(int test) { struct ktls_test_cipher *cipher; int cis_ktls, sis_ktls; OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS); cipher = &ktls_test_ciphers[test / 4]; cis_ktls = (test & 1) != 0; sis_ktls = (test & 2) != 0; return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version, cipher->cipher); } static int test_ktls_sendfile(int test) { struct ktls_test_cipher *cipher; int tst = test >> 1; OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS); cipher = &ktls_test_ciphers[tst]; return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher, test & 1); } #endif static int test_large_message_tls(void) { return execute_test_large_message(TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, 0); } static int test_large_message_tls_read_ahead(void) { return execute_test_large_message(TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, 1); } #ifndef OPENSSL_NO_DTLS static int test_large_message_dtls(void) { # ifdef OPENSSL_NO_DTLS1_2 if (is_fips) return 1; # endif return execute_test_large_message(DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, 0); } #endif static int test_large_app_data(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, prot; unsigned char *msg, *buf = NULL; size_t written, readbytes; const SSL_METHOD *smeth = TLS_server_method(); const SSL_METHOD *cmeth = TLS_client_method(); switch (tst >> 2) { case 0: #ifndef OSSL_NO_USABLE_TLS1_3 prot = TLS1_3_VERSION; break; #else return 1; #endif case 1: #ifndef OPENSSL_NO_TLS1_2 prot = TLS1_2_VERSION; break; #else return 1; #endif case 2: #ifndef OPENSSL_NO_TLS1_1 prot = TLS1_1_VERSION; break; #else return 1; #endif case 3: #ifndef OPENSSL_NO_TLS1 prot = TLS1_VERSION; break; #else return 1; #endif case 4: #ifndef OPENSSL_NO_SSL3 prot = SSL3_VERSION; break; #else return 1; #endif case 5: #ifndef OPENSSL_NO_DTLS1_2 prot = DTLS1_2_VERSION; smeth = DTLS_server_method(); cmeth = DTLS_client_method(); break; #else return 1; #endif case 6: #ifndef OPENSSL_NO_DTLS1 prot = DTLS1_VERSION; smeth = DTLS_server_method(); cmeth = DTLS_client_method(); break; #else return 1; #endif default: return 0; } if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips) return 1; msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH); if (!TEST_ptr(msg)) goto end; buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1); if (!TEST_ptr(buf)) goto end; memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1); if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot, &sctx, &cctx, cert, privkey))) goto end; if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")) || !TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if ((tst & 1) != 0) { if (!TEST_true(SSL_set_options(serverssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) || !TEST_true(SSL_set_options(clientssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))) goto end; } if ((tst & 2) != 0) { if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC)) || !TEST_true(SSL_set_options(clientssl, SSL_OP_NO_ENCRYPT_THEN_MAC))) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH, &written)) || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH)) goto end; if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1, &readbytes))) goto end; if (!TEST_mem_eq(msg, written, buf, readbytes)) goto end; testresult = 1; end: OPENSSL_free(msg); OPENSSL_free(buf); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) \ || !defined(OPENSSL_NO_DTLS) static int execute_cleanse_plaintext(const SSL_METHOD *smeth, const SSL_METHOD *cmeth, int min_version, int max_version) { size_t i; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; const unsigned char *zbuf; SSL_CONNECTION *serversc; TLS_RECORD *rr; static unsigned char cbuf[16000]; static unsigned char sbuf[16000]; if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version, max_version, &sctx, &cctx, cert, privkey))) goto end; # ifdef OPENSSL_NO_DTLS1_2 if (smeth == DTLS_server_method()) { if (is_fips) { testresult = 1; goto end; }; 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; if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; for (i = 0; i < sizeof(cbuf); i++) { cbuf[i] = i & 0xff; } if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf))) goto end; if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf))) goto end; if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf))) goto end; if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl))) goto end; rr = serversc->rlayer.tlsrecs; zbuf = &rr->data[rr->off]; if (!TEST_int_eq(rr->length, sizeof(cbuf))) goto end; if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf))) goto end; memset(sbuf, 0, sizeof(sbuf)); if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf))) goto end; if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf))) goto end; memset(cbuf, 0, sizeof(cbuf)); if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int test_cleanse_plaintext(void) { #if !defined(OPENSSL_NO_TLS1_2) if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(), TLS_client_method(), TLS1_2_VERSION, TLS1_2_VERSION))) return 0; #endif #if !defined(OSSL_NO_USABLE_TLS1_3) if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, TLS1_3_VERSION))) return 0; #endif #if !defined(OPENSSL_NO_DTLS) if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0))) return 0; #endif return 1; } #ifndef OPENSSL_NO_OCSP static int ocsp_server_cb(SSL *s, void *arg) { int *argi = (int *)arg; unsigned char *copy = NULL; STACK_OF(OCSP_RESPID) *ids = NULL; OCSP_RESPID *id = NULL; if (*argi == 2) { SSL_get_tlsext_status_ids(s, &ids); if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1) return SSL_TLSEXT_ERR_ALERT_FATAL; id = sk_OCSP_RESPID_value(ids, 0); if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL)) return SSL_TLSEXT_ERR_ALERT_FATAL; } else if (*argi != 1) { return SSL_TLSEXT_ERR_ALERT_FATAL; } if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder)))) return SSL_TLSEXT_ERR_ALERT_FATAL; if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder)))) { OPENSSL_free(copy); return SSL_TLSEXT_ERR_ALERT_FATAL; } ocsp_server_called = 1; return SSL_TLSEXT_ERR_OK; } static int ocsp_client_cb(SSL *s, void *arg) { int *argi = (int *)arg; const unsigned char *respderin; size_t len; if (*argi != 1 && *argi != 2) return 0; len = SSL_get_tlsext_status_ocsp_resp(s, &respderin); if (!TEST_mem_eq(orespder, len, respderin, len)) return 0; ocsp_client_called = 1; return 1; } static int test_tlsext_status_type(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; STACK_OF(OCSP_RESPID) *ids = NULL; OCSP_RESPID *id = NULL; BIO *certbio = NULL; if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) return 0; if (SSL_CTX_get_tlsext_status_type(cctx) != -1) goto end; clientssl = SSL_new(cctx); if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1) || !TEST_true(SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl), TLSEXT_STATUSTYPE_ocsp)) goto end; SSL_free(clientssl); clientssl = NULL; if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp) || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) goto end; clientssl = SSL_new(cctx); if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) goto end; SSL_free(clientssl); clientssl = NULL; SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb); SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg); SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb); SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(ocsp_client_called) || !TEST_true(ocsp_server_called)) goto end; SSL_free(serverssl); SSL_free(clientssl); serverssl = NULL; clientssl = NULL; ocsp_client_called = 0; ocsp_server_called = 0; cdummyarg = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(ocsp_client_called) || !TEST_false(ocsp_server_called)) goto end; SSL_free(serverssl); SSL_free(clientssl); serverssl = NULL; clientssl = NULL; ocsp_client_called = 0; ocsp_server_called = 0; cdummyarg = 2; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_ptr(certbio = BIO_new_file(cert, "r")) || !TEST_ptr(id = OCSP_RESPID_new()) || !TEST_ptr(ids = sk_OCSP_RESPID_new_null()) || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL)) || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL)) || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL)) || !TEST_true(sk_OCSP_RESPID_push(ids, id))) goto end; id = NULL; SSL_set_tlsext_status_ids(clientssl, ids); ids = NULL; BIO_free(certbio); certbio = NULL; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(ocsp_client_called) || !TEST_true(ocsp_server_called)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free); OCSP_RESPID_free(id); BIO_free(certbio); X509_free(ocspcert); ocspcert = NULL; return testresult; } #endif #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) static int new_called, remove_called, get_called; static int new_session_cb(SSL *ssl, SSL_SESSION *sess) { new_called++; SSL_SESSION_free(sess); return 1; } static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess) { remove_called++; } static SSL_SESSION *get_sess_val = NULL; static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len, int *copy) { get_called++; *copy = 1; return get_sess_val; } static int execute_test_session(int maxprot, int use_int_cache, int use_ext_cache, long s_options) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl1 = NULL, *clientssl1 = NULL; SSL *serverssl2 = NULL, *clientssl2 = NULL; # ifndef OPENSSL_NO_TLS1_1 SSL *serverssl3 = NULL, *clientssl3 = NULL; # endif SSL_SESSION *sess1 = NULL, *sess2 = NULL; int testresult = 0, numnewsesstick = 1; new_called = remove_called = 0; if (maxprot == TLS1_3_VERSION) numnewsesstick = 2; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; SSL_CTX_set_min_proto_version(cctx, maxprot); SSL_CTX_set_max_proto_version(cctx, maxprot); if (use_ext_cache) { SSL_CTX_sess_set_new_cb(cctx, new_session_cb); SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb); } if (use_int_cache) { SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT); } else { SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); } if (s_options) { SSL_CTX_set_options(sctx, s_options); } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl1, clientssl1, SSL_ERROR_NONE)) || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))) goto end; if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1))) goto end; if (use_ext_cache && (!TEST_int_eq(new_called, numnewsesstick) || !TEST_int_eq(remove_called, 0))) goto end; new_called = remove_called = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl2, sess1)) || !TEST_true(create_ssl_connection(serverssl2, clientssl2, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl2))) goto end; if (maxprot == TLS1_3_VERSION) { if (use_ext_cache && (!TEST_int_eq(new_called, 1) || !TEST_int_eq(remove_called, 1))) goto end; } else { if (use_ext_cache && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 0))) goto end; } SSL_SESSION_free(sess1); if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2))) goto end; shutdown_ssl_connection(serverssl2, clientssl2); serverssl2 = clientssl2 = NULL; new_called = remove_called = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl2, clientssl2, SSL_ERROR_NONE))) goto end; if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2))) goto end; if (use_ext_cache && (!TEST_int_eq(new_called, numnewsesstick) || !TEST_int_eq(remove_called, 0))) goto end; new_called = remove_called = 0; if (!TEST_true(SSL_set_session(clientssl2, sess1))) goto end; if (use_ext_cache && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) goto end; if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1)) goto end; if (use_int_cache) { if (!TEST_true(SSL_CTX_add_session(cctx, sess2)) || !TEST_true(SSL_CTX_remove_session(cctx, sess2))) goto end; } new_called = remove_called = 0; if (!TEST_false(SSL_CTX_remove_session(cctx, sess2))) goto end; if (use_ext_cache && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) goto end; # if !defined(OPENSSL_NO_TLS1_1) new_called = remove_called = 0; SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl3, sess1)) || !TEST_false(create_ssl_connection(serverssl3, clientssl3, SSL_ERROR_NONE))) goto end; if (use_ext_cache && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) goto end; if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2))) goto end; # endif if (use_ext_cache) { SSL_CTX_sess_set_new_cb(cctx, NULL); SSL_CTX_sess_set_remove_cb(cctx, NULL); SSL_CTX_sess_set_new_cb(sctx, new_session_cb); SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb); SSL_CTX_sess_set_get_cb(sctx, get_session_cb); get_sess_val = NULL; } SSL_CTX_set_session_cache_mode(cctx, 0); if (!use_int_cache) SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_SERVER | SSL_SESS_CACHE_NO_INTERNAL_STORE); SSL_free(serverssl1); SSL_free(clientssl1); serverssl1 = clientssl1 = NULL; SSL_free(serverssl2); SSL_free(clientssl2); serverssl2 = clientssl2 = NULL; SSL_SESSION_free(sess1); sess1 = NULL; SSL_SESSION_free(sess2); sess2 = NULL; SSL_CTX_set_max_proto_version(sctx, maxprot); if (maxprot == TLS1_2_VERSION) SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET); new_called = remove_called = get_called = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl1, clientssl1, SSL_ERROR_NONE)) || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)) || !TEST_ptr(sess2 = SSL_get1_session(serverssl1))) goto end; if (use_int_cache) { if (maxprot == TLS1_3_VERSION && !use_ext_cache) { if (!TEST_false(SSL_CTX_remove_session(sctx, sess2))) goto end; } else { if (!TEST_false(SSL_CTX_add_session(sctx, sess2))) goto end; } } if (use_ext_cache) { SSL_SESSION *tmp = sess2; if (!TEST_int_eq(new_called, numnewsesstick) || !TEST_int_eq(remove_called, 0) || !TEST_int_eq(get_called, 0)) goto end; if (use_int_cache && maxprot != TLS1_3_VERSION) { if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2)) || !TEST_true(sess2->owner != NULL) || !TEST_true(tmp->owner == NULL) || !TEST_true(SSL_CTX_remove_session(sctx, sess2))) goto end; SSL_SESSION_free(sess2); } sess2 = tmp; } new_called = remove_called = get_called = 0; get_sess_val = sess2; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl2, sess1)) || !TEST_true(create_ssl_connection(serverssl2, clientssl2, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl2))) goto end; if (use_ext_cache) { if (!TEST_int_eq(remove_called, 0)) goto end; if (maxprot == TLS1_3_VERSION) { if (!TEST_int_eq(new_called, 1) || !TEST_int_eq(get_called, 0)) goto end; } else { if (!TEST_int_eq(new_called, 0) || !TEST_int_eq(get_called, 1)) goto end; } } if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0) || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0) || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0) || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0)) goto end; if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0)) goto end; SSL_CTX_add_session(sctx, sess1); SSL_CTX_add_session(sctx, sess2); if (!TEST_true(SSL_CTX_add_session(sctx, sess1)) || !TEST_ptr(sess1->owner) || !TEST_ptr_null(sess2->owner)) goto end; testresult = 1; end: SSL_free(serverssl1); SSL_free(clientssl1); SSL_free(serverssl2); SSL_free(clientssl2); # ifndef OPENSSL_NO_TLS1_1 SSL_free(serverssl3); SSL_free(clientssl3); # endif SSL_SESSION_free(sess1); SSL_SESSION_free(sess2); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int test_session_with_only_int_cache(void) { #ifndef OSSL_NO_USABLE_TLS1_3 if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0)) return 0; #endif #ifndef OPENSSL_NO_TLS1_2 return execute_test_session(TLS1_2_VERSION, 1, 0, 0); #else return 1; #endif } static int test_session_with_only_ext_cache(void) { #ifndef OSSL_NO_USABLE_TLS1_3 if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0)) return 0; #endif #ifndef OPENSSL_NO_TLS1_2 return execute_test_session(TLS1_2_VERSION, 0, 1, 0); #else return 1; #endif } static int test_session_with_both_cache(void) { #ifndef OSSL_NO_USABLE_TLS1_3 if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0)) return 0; #endif #ifndef OPENSSL_NO_TLS1_2 return execute_test_session(TLS1_2_VERSION, 1, 1, 0); #else return 1; #endif } static int test_session_wo_ca_names(void) { #ifndef OSSL_NO_USABLE_TLS1_3 if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES)) return 0; #endif #ifndef OPENSSL_NO_TLS1_2 return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES); #else return 1; #endif } #ifndef OSSL_NO_USABLE_TLS1_3 static SSL_SESSION *sesscache[6]; static int do_cache; static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess) { if (do_cache) { sesscache[new_called] = sess; } else { SSL_SESSION_free(sess); } new_called++; return 1; } static int post_handshake_verify(SSL *sssl, SSL *cssl) { SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL); if (!TEST_true(SSL_verify_client_post_handshake(sssl))) return 0; if (!TEST_int_eq(SSL_do_handshake(sssl), 1) || !TEST_int_le(SSL_read(cssl, NULL, 0), 0) || !TEST_int_le(SSL_read(sssl, NULL, 0), 0) || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE))) return 0; return 1; } static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx, SSL_CTX **cctx) { int sess_id_ctx = 1; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, sctx, cctx, cert, privkey)) || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx)) || !TEST_true(SSL_CTX_set_session_id_context(*sctx, (void *)&sess_id_ctx, sizeof(sess_id_ctx)))) return 0; if (stateful) SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET); SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb); return 1; } static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ) { SSL *serverssl = NULL, *clientssl = NULL; int i; for (i = 0; i < idx * 2; i++) { new_called = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sesscache[i]))) goto end; SSL_set_post_handshake_auth(clientssl, 1); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (succ) { if (!TEST_true(SSL_session_reused(clientssl)) || !TEST_int_eq(new_called, 1)) goto end; } else { if (!TEST_false(SSL_session_reused(clientssl)) || !TEST_int_eq(new_called, idx)) goto end; } new_called = 0; if (succ && (!post_handshake_verify(serverssl, clientssl) || !TEST_int_eq(new_called, 1))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; SSL_SESSION_free(sesscache[i]); sesscache[i] = NULL; } return 1; end: SSL_free(clientssl); SSL_free(serverssl); return 0; } static int test_tickets(int stateful, int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; size_t j; new_called = 0; do_cache = 1; if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(idx, new_called)) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); clientssl = serverssl = NULL; sctx = cctx = NULL; do_cache = 0; if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) goto end; if (!check_resumption(idx, sctx, cctx, 0)) goto end; new_called = 0; do_cache = 1; SSL_CTX_free(sctx); SSL_CTX_free(cctx); sctx = cctx = NULL; if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; SSL_set_post_handshake_auth(clientssl, 1); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(idx, new_called)) goto end; if (!post_handshake_verify(serverssl, clientssl) || !TEST_int_eq(idx * 2, new_called)) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; do_cache = 0; if (!check_resumption(idx, sctx, cctx, 1)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); for (j = 0; j < OSSL_NELEM(sesscache); j++) { SSL_SESSION_free(sesscache[j]); sesscache[j] = NULL; } SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_stateless_tickets(int idx) { return test_tickets(0, idx); } static int test_stateful_tickets(int idx) { return test_tickets(1, idx); } static int test_psk_tickets(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; int sess_id_ctx = 1; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, NULL, NULL)) || !TEST_true(SSL_CTX_set_session_id_context(sctx, (void *)&sess_id_ctx, sizeof(sess_id_ctx)))) goto end; SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb); SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb); SSL_CTX_sess_set_new_cb(cctx, new_session_cb); use_session_cb_cnt = 0; find_session_cb_cnt = 0; srvid = pskid; new_called = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH); if (!TEST_ptr(clientpsk)) goto end; SSL_SESSION_up_ref(clientpsk); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(1, find_session_cb_cnt) || !TEST_int_eq(1, use_session_cb_cnt) || !TEST_int_eq(1, new_called)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; return testresult; } static int test_extra_tickets(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; BIO *bretry = BIO_new(bio_s_always_retry()); BIO *tmp = NULL; int testresult = 0; int stateful = 0; size_t nbytes; unsigned char c, buf[1]; new_called = 0; do_cache = 1; if (idx >= 3) { idx -= 3; stateful = 1; } if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx)) goto end; SSL_CTX_sess_set_new_cb(sctx, new_session_cb); SSL_CTX_sess_set_new_cb(cctx, new_session_cb); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(idx * 2, new_called) || !TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_int_eq(idx * 2, new_called)) goto end; c = '1'; if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) || !TEST_size_t_eq(1, nbytes) || !TEST_int_eq(idx * 2 + 2, new_called) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(idx * 2 + 4, new_called) || !TEST_int_eq(sizeof(buf), nbytes) || !TEST_int_eq(c, buf[0]) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) goto end; c = '2'; new_called = 0; if (!TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes)) || !TEST_size_t_eq(sizeof(c), nbytes) || !TEST_int_eq(1, new_called) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(2, new_called) || !TEST_size_t_eq(sizeof(buf), nbytes) || !TEST_int_eq(c, buf[0])) goto end; c = '3'; new_called = 0; if (!TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes)) || !TEST_size_t_eq(0, nbytes) || !TEST_int_eq(2, new_called) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(4, new_called)) goto end; c = '4'; new_called = 0; if (!TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_do_handshake(serverssl)) || !TEST_int_eq(2, new_called) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(4, new_called)) goto end; c = '5'; new_called = 0; tmp = SSL_get_wbio(serverssl); if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { tmp = NULL; goto end; } SSL_set0_wbio(serverssl, bretry); bretry = NULL; if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes)) || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE) || !TEST_size_t_eq(nbytes, 0)) goto end; SSL_set0_wbio(serverssl, tmp); tmp = NULL; if (!TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_true(SSL_new_session_ticket(serverssl)) || !TEST_int_eq(0, new_called) || !TEST_true(SSL_do_handshake(serverssl)) || !TEST_int_eq(0, new_called)) goto end; if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) || !TEST_size_t_eq(1, nbytes) || !TEST_int_eq(0, new_called) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(0, new_called) || !TEST_int_eq(sizeof(buf), nbytes) || !TEST_int_eq(c, buf[0]) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) goto end; if (!TEST_true(SSL_do_handshake(serverssl)) || !TEST_int_eq(0, new_called)) goto end; c = '6'; if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) || !TEST_size_t_eq(1, nbytes) || !TEST_int_eq(2, new_called) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) || !TEST_int_eq(4, new_called) || !TEST_int_eq(sizeof(buf), nbytes) || !TEST_int_eq(c, buf[0]) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); testresult = 1; end: BIO_free(bretry); BIO_free(tmp); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); clientssl = serverssl = NULL; sctx = cctx = NULL; return testresult; } #endif #define USE_NULL 0 #define USE_BIO_1 1 #define USE_BIO_2 2 #define USE_DEFAULT 3 #define CONNTYPE_CONNECTION_SUCCESS 0 #define CONNTYPE_CONNECTION_FAIL 1 #define CONNTYPE_NO_CONNECTION 2 #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3) #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2) #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2) #else # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0 #endif #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \ + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \ + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type) { switch (type) { case USE_NULL: *res = NULL; break; case USE_BIO_1: *res = bio1; break; case USE_BIO_2: *res = bio2; break; } } static int test_ssl_set_bio(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; BIO *bio1 = NULL; BIO *bio2 = NULL; BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL; SSL *serverssl = NULL, *clientssl = NULL; int initrbio, initwbio, newrbio, newwbio, conntype; int testresult = 0; if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) { initrbio = idx % 3; idx /= 3; initwbio = idx % 3; idx /= 3; newrbio = idx % 3; idx /= 3; newwbio = idx % 3; conntype = CONNTYPE_NO_CONNECTION; } else { idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS; initrbio = initwbio = USE_DEFAULT; newrbio = idx % 2; idx /= 2; newwbio = idx % 2; idx /= 2; conntype = idx % 2; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (conntype == CONNTYPE_CONNECTION_FAIL) { SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION); SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1 || newwbio == USE_BIO_1) { if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem()))) goto end; } if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2 || newwbio == USE_BIO_2) { if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem()))) goto end; } if (initrbio != USE_DEFAULT) { setupbio(&irbio, bio1, bio2, initrbio); setupbio(&iwbio, bio1, bio2, initwbio); SSL_set_bio(clientssl, irbio, iwbio); if (irbio != NULL) BIO_up_ref(irbio); if (iwbio != NULL && iwbio != irbio) BIO_up_ref(iwbio); } if (conntype != CONNTYPE_NO_CONNECTION && !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE) == (conntype == CONNTYPE_CONNECTION_SUCCESS))) goto end; setupbio(&nrbio, bio1, bio2, newrbio); setupbio(&nwbio, bio1, bio2, newwbio); if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio)) BIO_up_ref(nrbio); if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio))) BIO_up_ref(nwbio); SSL_set_bio(clientssl, nrbio, nwbio); testresult = 1; end: BIO_free(bio1); BIO_free(bio2); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t; static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio) { BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL; SSL_CTX *ctx; SSL *ssl = NULL; int testresult = 0; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method())) || !TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl())) || !TEST_ptr(membio1 = BIO_new(BIO_s_mem()))) goto end; BIO_set_ssl(sslbio, ssl, BIO_CLOSE); BIO_push(sslbio, membio1); if (change_bio != NO_BIO_CHANGE) { if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) { ssl = NULL; goto end; } if (change_bio == CHANGE_RBIO) SSL_set0_rbio(ssl, membio2); else SSL_set0_wbio(ssl, membio2); } ssl = NULL; if (pop_ssl) BIO_pop(sslbio); else BIO_pop(membio1); testresult = 1; end: BIO_free(membio1); BIO_free(sslbio); SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_ssl_bio_pop_next_bio(void) { return execute_test_ssl_bio(0, NO_BIO_CHANGE); } static int test_ssl_bio_pop_ssl_bio(void) { return execute_test_ssl_bio(1, NO_BIO_CHANGE); } static int test_ssl_bio_change_rbio(void) { return execute_test_ssl_bio(0, CHANGE_RBIO); } static int test_ssl_bio_change_wbio(void) { return execute_test_ssl_bio(0, CHANGE_WBIO); } #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3) typedef struct { const int *list; size_t listlen; const char *liststr; int valid; int connsuccess; } sigalgs_list; static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA}; # ifndef OPENSSL_NO_EC static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC}; static const int validlist3[] = {NID_sha512, EVP_PKEY_EC}; # endif static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA}; static const int invalidlist2[] = {NID_sha256, NID_undef}; static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256}; static const int invalidlist4[] = {NID_sha256}; static const sigalgs_list testsigalgs[] = { {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1}, # ifndef OPENSSL_NO_EC {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1}, {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0}, # endif {NULL, 0, "RSA+SHA256", 1, 1}, {NULL, 0, "RSA+SHA256:?Invalid", 1, 1}, # ifndef OPENSSL_NO_EC {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1}, {NULL, 0, "ECDSA+SHA512", 1, 0}, # endif {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0}, {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0}, {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0}, {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0}, {NULL, 0, "RSA", 0, 0}, {NULL, 0, "SHA256", 0, 0}, {NULL, 0, "RSA+SHA256:SHA256", 0, 0}, {NULL, 0, "Invalid", 0, 0} }; static int test_set_sigalgs(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; const sigalgs_list *curr; int testctx; if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2)) return 0; testctx = ((size_t)idx < OSSL_NELEM(testsigalgs)); curr = testctx ? &testsigalgs[idx] : &testsigalgs[idx - OSSL_NELEM(testsigalgs)]; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); if (testctx) { int ret; if (curr->list != NULL) ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen); else ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr); if (!ret) { if (curr->valid) TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx); else testresult = 1; goto end; } if (!curr->valid) { TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx); goto end; } } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!testctx) { int ret; if (curr->list != NULL) ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen); else ret = SSL_set1_sigalgs_list(clientssl, curr->liststr); if (!ret) { if (curr->valid) TEST_info("Failure setting sigalgs in SSL (%d)\n", idx); else testresult = 1; goto end; } if (!curr->valid) goto end; } if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE), curr->connsuccess)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif #ifndef OSSL_NO_USABLE_TLS1_3 static int psk_client_cb_cnt = 0; static int psk_server_cb_cnt = 0; static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, size_t *idlen, SSL_SESSION **sess) { switch (++use_session_cb_cnt) { case 1: if (md != NULL) return 0; break; case 2: if (md == NULL) return 0; break; default: return 0; } if (clientpsk != NULL) SSL_SESSION_up_ref(clientpsk); *sess = clientpsk; *id = (const unsigned char *)pskid; *idlen = strlen(pskid); return 1; } #ifndef OPENSSL_NO_PSK static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id, unsigned int max_id_len, unsigned char *psk, unsigned int max_psk_len) { unsigned int psklen = 0; psk_client_cb_cnt++; if (strlen(pskid) + 1 > max_id_len) return 0; if (psk_client_cb_cnt > 2) return 0; if (clientpsk == NULL) return 0; if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len) return 0; psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len); strncpy(id, pskid, max_id_len); return psklen; } #endif static int find_session_cb(SSL *ssl, const unsigned char *identity, size_t identity_len, SSL_SESSION **sess) { find_session_cb_cnt++; if (find_session_cb_cnt > 2) return 0; if (serverpsk == NULL) return 0; if (strlen(srvid) != identity_len || strncmp(srvid, (const char *)identity, identity_len) != 0) { *sess = NULL; return 1; } SSL_SESSION_up_ref(serverpsk); *sess = serverpsk; return 1; } #ifndef OPENSSL_NO_PSK static unsigned int psk_server_cb(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { unsigned int psklen = 0; psk_server_cb_cnt++; if (find_session_cb_cnt > 2) return 0; if (serverpsk == NULL) return 0; if (strcmp(srvid, identity) != 0) { return 0; } if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len) return 0; psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len); return psklen; } #endif #define MSG1 "Hello" #define MSG2 "World." #define MSG3 "This" #define MSG4 "is" #define MSG5 "a" #define MSG6 "test" #define MSG7 "message." static int artificial_ticket_time = 0; static int ed_gen_cb(SSL *s, void *arg) { SSL_SESSION *sess = SSL_get0_session(s); if (sess == NULL) return 0; if (artificial_ticket_time == 0) return 1; artificial_ticket_time--; if (SSL_SESSION_set_time_ex(sess, SSL_SESSION_get_time_ex(sess) - 10) == 0) return 0; return 1; } static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl, SSL **serverssl, SSL_SESSION **sess, int idx, size_t mdsize) { int artificial = (artificial_ticket_time > 0); if (*sctx == NULL && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, sctx, cctx, cert, privkey))) return 0; if (artificial) SSL_CTX_set_session_ticket_cb(*sctx, ed_gen_cb, NULL, NULL); if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH))) return 0; if (idx == 1) { SSL_CTX_set_read_ahead(*cctx, 1); SSL_CTX_set_read_ahead(*sctx, 1); } else if (idx == 2) { SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb); SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb); use_session_cb_cnt = 0; find_session_cb_cnt = 0; srvid = pskid; } if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl, NULL, NULL))) return 0; if (idx == 1 && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost"))) return 0; if (idx == 2) { clientpsk = create_a_psk(*clientssl, mdsize); if (!TEST_ptr(clientpsk) || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk, 0x100)) || !TEST_true(SSL_SESSION_up_ref(clientpsk))) { SSL_SESSION_free(clientpsk); clientpsk = NULL; return 0; } serverpsk = clientpsk; if (sess != NULL) { if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) { SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; return 0; } *sess = clientpsk; } return 1; } if (sess == NULL) return 1; if (!TEST_true(create_ssl_connection(*serverssl, *clientssl, SSL_ERROR_NONE))) return 0; *sess = SSL_get1_session(*clientssl); SSL_shutdown(*clientssl); SSL_shutdown(*serverssl); SSL_free(*serverssl); SSL_free(*clientssl); *serverssl = *clientssl = NULL; if (artificial && !TEST_time_t_gt(SSL_SESSION_set_time_ex(*sess, SSL_SESSION_get_time_ex(*sess) - 10), 0)) return 0; if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(*clientssl, *sess))) return 0; return 1; } static int test_early_data_read_write(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char buf[20], data[1024]; size_t readbytes, written, eoedlen, rawread, rawwritten; BIO *rbio; if (idx != 2) artificial_ticket_time = 2; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, SHA384_DIGEST_LENGTH))) { artificial_ticket_time = 0; goto end; } artificial_ticket_time = 0; if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1)) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED)) goto end; if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2), &written)) || !TEST_size_t_eq(written, strlen(MSG2)) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3), &written)) || !TEST_size_t_eq(written, strlen(MSG3))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3))) goto end; if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4), &written)) || !TEST_size_t_eq(written, strlen(MSG4)) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4))) goto end; if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written)) || !TEST_size_t_eq(written, strlen(MSG5)) || !TEST_int_eq(SSL_get_early_data_status(clientssl), SSL_EARLY_DATA_ACCEPTED)) goto end; rbio = SSL_get_rbio(serverssl); if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread)) || !TEST_size_t_lt(rawread, sizeof(data)) || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH)) goto end; eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]); if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten)) || !TEST_size_t_eq(rawwritten, eoedlen)) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_size_t_eq(readbytes, 0)) goto end; if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6), &written)) || !TEST_size_t_eq(written, strlen(MSG6))) goto end; if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen, &rawwritten)) || !TEST_size_t_eq(rawwritten, rawread - eoedlen)) goto end; if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_size_t_eq(readbytes, strlen(MSG5))) goto end; if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6), &written))) goto end; ERR_clear_error(); if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_ERROR)) goto end; ERR_clear_error(); if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6))) goto end; if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))) goto end; if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written)) || !TEST_size_t_eq(written, strlen(MSG7)) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7))) goto end; SSL_SESSION_free(sess); sess = SSL_get1_session(clientssl); use_session_cb_cnt = 0; find_session_cb_cnt = 0; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sess))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))) goto end; if (!TEST_int_gt(SSL_connect(clientssl), 0) || !TEST_int_gt(SSL_accept(serverssl), 0)) goto end; if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6), &written))) goto end; ERR_clear_error(); if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_ERROR)) goto end; ERR_clear_error(); if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written)) || !TEST_size_t_eq(written, strlen(MSG5)) || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_size_t_eq(readbytes, strlen(MSG5))) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int allow_ed_cb_called = 0; static int allow_early_data_cb(SSL *s, void *arg) { int *usecb = (int *)arg; allow_ed_cb_called++; if (*usecb == 1) return 0; return 1; } static int test_early_data_replay_int(int idx, int usecb, int confopt) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; size_t readbytes, written; unsigned char buf[20]; allow_ed_cb_called = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; if (usecb > 0) { if (confopt == 0) { SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY); } else { SSL_CONF_CTX *confctx = SSL_CONF_CTX_new(); if (!TEST_ptr(confctx)) goto end; SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE | SSL_CONF_FLAG_SERVER); SSL_CONF_CTX_set_ssl_ctx(confctx, sctx); if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"), 2)) { SSL_CONF_CTX_free(confctx); goto end; } SSL_CONF_CTX_free(confctx); } SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb); } if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, SHA384_DIGEST_LENGTH))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sess))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1))) goto end; if (usecb <= 1) { if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_REJECTED)) goto end; } else { if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes) || !TEST_int_gt(SSL_connect(clientssl), 0) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED)) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0) || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0)) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_early_data_replay(int idx) { int ret = 1, usecb, confopt; for (usecb = 0; usecb < 3; usecb++) { for (confopt = 0; confopt < 2; confopt++) ret &= test_early_data_replay_int(idx, usecb, confopt); } return ret; } static const char *ciphersuites[] = { "TLS_AES_128_CCM_8_SHA256", "TLS_AES_128_GCM_SHA256", "TLS_AES_256_GCM_SHA384", "TLS_AES_128_CCM_SHA256", #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) "TLS_CHACHA20_POLY1305_SHA256" #endif }; static int early_data_skip_helper(int testtype, int cipher, int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char buf[20]; size_t readbytes, written; if (is_fips && cipher == 4) return 1; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (cipher == 0) { SSL_CTX_set_security_level(sctx, 0); SSL_CTX_set_security_level(cctx, 0); } if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher])) || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher]))) goto end; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, cipher == 2 ? SHA384_DIGEST_LENGTH : SHA256_DIGEST_LENGTH))) goto end; if (testtype == 1 || testtype == 2) { #if defined(OPENSSL_NO_EC) if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072"))) goto end; #else if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384"))) goto end; #endif } else if (idx == 2) { srvid = "Dummy Identity"; } else { if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20)))) goto end; } if (testtype == 3 && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_size_t_eq(readbytes, 0) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_REJECTED)) goto end; switch (testtype) { case 0: break; case 1: if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))) goto end; break; case 2: { BIO *wbio = SSL_get_wbio(clientssl); const unsigned char bad_early_data[] = { 0x17, 0x03, 0x03, 0x00, 0x01, 0x00 }; if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))) goto end; if (!TEST_true(BIO_write_ex(wbio, bad_early_data, sizeof(bad_early_data), &written))) goto end; } case 3: if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL)) goto end; testresult = 1; goto end; default: TEST_error("Invalid test type"); goto end; } ERR_clear_error(); if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) || !TEST_size_t_eq(written, strlen(MSG2)) || !TEST_int_eq(SSL_get_early_data_status(clientssl), SSL_EARLY_DATA_REJECTED) || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; if (!TEST_long_eq(ERR_peek_error(), 0)) goto end; testresult = 1; end: SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_SESSION_free(sess); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_early_data_skip(int idx) { return early_data_skip_helper(0, idx % OSSL_NELEM(ciphersuites), idx / OSSL_NELEM(ciphersuites)); } static int test_early_data_skip_hrr(int idx) { return early_data_skip_helper(1, idx % OSSL_NELEM(ciphersuites), idx / OSSL_NELEM(ciphersuites)); } static int test_early_data_skip_hrr_fail(int idx) { return early_data_skip_helper(2, idx % OSSL_NELEM(ciphersuites), idx / OSSL_NELEM(ciphersuites)); } static int test_early_data_skip_abort(int idx) { return early_data_skip_helper(3, idx % OSSL_NELEM(ciphersuites), idx / OSSL_NELEM(ciphersuites)); } static int test_early_data_not_sent(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char buf[20]; size_t readbytes, written; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, SHA384_DIGEST_LENGTH))) goto end; SSL_set_connect_state(clientssl); if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_size_t_eq(readbytes, 0) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_NOT_SENT) || !TEST_int_eq(SSL_get_early_data_status(clientssl), SSL_EARLY_DATA_NOT_SENT)) goto end; if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1)) || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written) || !TEST_size_t_eq(written, strlen(MSG2))) goto end; if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static const char *servalpn; static int alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { unsigned int protlen = 0; const unsigned char *prot; for (prot = in; prot < in + inlen; prot += protlen) { protlen = *prot++; if (in + inlen < prot + protlen) return SSL_TLSEXT_ERR_NOACK; if (protlen == strlen(servalpn) && memcmp(prot, servalpn, protlen) == 0) { *out = prot; *outlen = protlen; return SSL_TLSEXT_ERR_OK; } } return SSL_TLSEXT_ERR_NOACK; } static int test_early_data_psk(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char alpnlist[] = { 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a', 'l', 'p', 'n' }; #define GOODALPNLEN 9 #define BADALPNLEN 8 #define GOODALPN (alpnlist) #define BADALPN (alpnlist + GOODALPNLEN) int err = 0; unsigned char buf[20]; size_t readbytes, written; int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1; int edstatus = SSL_EARLY_DATA_ACCEPTED; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, 2, SHA384_DIGEST_LENGTH))) goto end; servalpn = "goodalpn"; switch (idx) { case 0: err = SSL_R_INCONSISTENT_EARLY_DATA_SNI; if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost")) || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost"))) goto end; break; case 1: err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN; if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN, GOODALPNLEN)) || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN, BADALPNLEN))) goto end; break; case 2: err = SSL_R_BAD_PSK; if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION))) goto end; break; case 3: SSL_SESSION_free(serverpsk); serverpsk = SSL_SESSION_dup(clientpsk); if (!TEST_ptr(serverpsk) || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost"))) goto end; case 4: if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost")) || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")) || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, hostname_cb))) goto end; break; case 5: servalpn = "badalpn"; edstatus = SSL_EARLY_DATA_REJECTED; readearlyres = SSL_READ_EARLY_DATA_FINISH; case 6: if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1, GOODALPNLEN - 1)) || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN, GOODALPNLEN))) goto end; SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL); break; case 7: SSL_SESSION_free(serverpsk); serverpsk = SSL_SESSION_dup(clientpsk); if (!TEST_ptr(serverpsk) || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk, BADALPN + 1, BADALPNLEN - 1)) || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk, GOODALPN + 1, GOODALPNLEN - 1)) || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist, sizeof(alpnlist)))) goto end; SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL); edstatus = SSL_EARLY_DATA_ACCEPTED; readearlyres = SSL_READ_EARLY_DATA_SUCCESS; connectres = -1; break; default: TEST_error("Bad test index"); goto end; } SSL_set_connect_state(clientssl); if (err != 0) { if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL) || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err)) goto end; } else { if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), readearlyres) || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))) || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus) || !TEST_int_eq(SSL_connect(clientssl), connectres)) goto end; } testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_early_data_psk_with_all_ciphers(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char buf[20]; size_t readbytes, written; const SSL_CIPHER *cipher; const char *cipher_str[] = { TLS1_3_RFC_AES_128_GCM_SHA256, TLS1_3_RFC_AES_256_GCM_SHA384, # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) TLS1_3_RFC_CHACHA20_POLY1305_SHA256, # else NULL, # endif TLS1_3_RFC_AES_128_CCM_SHA256, TLS1_3_RFC_AES_128_CCM_8_SHA256 }; const unsigned char *cipher_bytes[] = { TLS13_AES_128_GCM_SHA256_BYTES, TLS13_AES_256_GCM_SHA384_BYTES, # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) TLS13_CHACHA20_POLY1305_SHA256_BYTES, # else NULL, # endif TLS13_AES_128_CCM_SHA256_BYTES, TLS13_AES_128_CCM_8_SHA256_BYTES }; if (cipher_str[idx] == NULL) return 1; if (idx == 2 && is_fips == 1) return 1; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, 2, SHA384_DIGEST_LENGTH))) goto end; if (idx == 4) { SSL_set_security_level(clientssl, 0); SSL_set_security_level(serverssl, 0); } if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx])) || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx]))) goto end; cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]); if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))) goto end; SSL_set_connect_state(clientssl); if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED) || !TEST_int_eq(SSL_connect(clientssl), 1) || !TEST_int_eq(SSL_accept(serverssl), 1)) goto end; if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) || !TEST_size_t_eq(written, strlen(MSG2))) goto end; if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; if (clientssl != NULL) SSL_shutdown(clientssl); if (serverssl != NULL) SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_early_data_not_expected(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; unsigned char buf[20]; size_t readbytes, written; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, SHA384_DIGEST_LENGTH))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written))) goto end; if (!TEST_int_le(SSL_accept(serverssl), 0) || !TEST_int_gt(SSL_connect(clientssl), 0) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_REJECTED) || !TEST_int_gt(SSL_accept(serverssl), 0) || !TEST_int_eq(SSL_get_early_data_status(clientssl), SSL_EARLY_DATA_REJECTED)) goto end; if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) || !TEST_size_t_eq(written, strlen(MSG2))) goto end; if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } # ifndef OPENSSL_NO_TLS1_2 static int test_early_data_tls1_2(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; unsigned char buf[20]; size_t readbytes, written; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, NULL, idx, SHA384_DIGEST_LENGTH))) goto end; SSL_set_max_proto_version(clientssl, TLS1_2_VERSION); SSL_set_connect_state(clientssl); if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))) goto end; if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_ERROR)) goto end; if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH) || !TEST_size_t_eq(readbytes, 0) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_NOT_SENT)) goto end; if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1)) || !TEST_int_eq(SSL_get_early_data_status(clientssl), SSL_EARLY_DATA_NOT_SENT) || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)) || !TEST_size_t_eq(written, strlen(MSG2)) || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes) || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) goto end; testresult = 1; end: SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } # endif static int test_set_ciphersuite(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256"))) goto end; if (idx >=4 && idx <= 7) { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384"))) goto end; } if (idx == 0 || idx == 4) { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) goto end; } else if (idx == 1 || idx == 5) { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_CCM_SHA256"))) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (idx == 8 || idx == 9) { if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))) goto end; } if (idx == 2 || idx == 6 || idx == 8) { if (!TEST_true(SSL_set_ciphersuites(clientssl, "TLS_AES_128_GCM_SHA256"))) goto end; } else if (idx == 3 || idx == 7 || idx == 9) { if (!TEST_true(SSL_set_ciphersuites(clientssl, "TLS_AES_128_CCM_SHA256"))) goto end; } 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; } static int test_ciphersuite_change(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *clntsess = NULL; int testresult = 0; const SSL_CIPHER *aes_128_gcm_sha256 = NULL; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, "TLS_AES_128_GCM_SHA256:" "TLS_AES_256_GCM_SHA384:" "TLS_AES_128_CCM_SHA256")) || !TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; clntsess = SSL_get1_session(clientssl); aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_CCM_SHA256")) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, clntsess)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl))) goto end; SSL_SESSION_free(clntsess); clntsess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384")) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, clntsess)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_SSL)) || !TEST_false(SSL_session_reused(clientssl))) goto end; SSL_SESSION_free(clntsess); clntsess = NULL; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384")) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; clntsess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384")) || !TEST_true(SSL_CTX_set_ciphersuites(sctx, "TLS_AES_256_GCM_SHA384")) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, clntsess)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_READ))) goto end; clntsess->cipher = aes_128_gcm_sha256; clntsess->cipher_id = clntsess->cipher->id; if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_SSL)) || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED)) goto end; testresult = 1; end: SSL_SESSION_free(clntsess); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } # ifndef OPENSSL_NO_EC static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, # ifndef OPENSSL_NO_ECX NID_X25519, NID_X448 # endif }; # endif # ifndef OPENSSL_NO_DH static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096, NID_ffdhe6144, NID_ffdhe8192}; # endif static int test_key_exchange(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; int kexch_alg; int *kexch_groups = &kexch_alg; int kexch_groups_size = 1; int max_version = TLS1_3_VERSION; char *kexch_name0 = NULL; switch (idx) { # ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_TLS1_2 case 12: max_version = TLS1_2_VERSION; # endif case 0: kexch_groups = ecdhe_kexch_groups; kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups); kexch_name0 = "secp256r1"; break; case 1: kexch_alg = NID_X9_62_prime256v1; kexch_name0 = "secp256r1"; break; case 2: kexch_alg = NID_secp384r1; kexch_name0 = "secp384r1"; break; case 3: kexch_alg = NID_secp521r1; kexch_name0 = "secp521r1"; break; # ifndef OPENSSL_NO_ECX case 4: kexch_alg = NID_X25519; kexch_name0 = "x25519"; break; case 5: kexch_alg = NID_X448; kexch_name0 = "x448"; break; # endif # endif # ifndef OPENSSL_NO_DH # ifndef OPENSSL_NO_TLS1_2 case 13: max_version = TLS1_2_VERSION; kexch_name0 = "ffdhe2048"; # endif case 6: kexch_groups = ffdhe_kexch_groups; kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups); kexch_name0 = "ffdhe2048"; break; case 7: kexch_alg = NID_ffdhe2048; kexch_name0 = "ffdhe2048"; break; case 8: kexch_alg = NID_ffdhe3072; kexch_name0 = "ffdhe3072"; break; case 9: kexch_alg = NID_ffdhe4096; kexch_name0 = "ffdhe4096"; break; case 10: kexch_alg = NID_ffdhe6144; kexch_name0 = "ffdhe6144"; break; case 11: kexch_alg = NID_ffdhe8192; kexch_name0 = "ffdhe8192"; break; # endif default: return 1; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, max_version, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, TLS1_3_RFC_AES_128_GCM_SHA256))) goto end; if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, TLS1_3_RFC_AES_128_GCM_SHA256))) goto end; if (!TEST_true(SSL_CTX_set_cipher_list(sctx, TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":" TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)) || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1))) goto end; # ifndef OPENSSL_NO_TLS1_2 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":" TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))) goto end; # endif if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size)) || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0), idx == 13 ? 0 : kexch_groups[0])) goto end; if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]), kexch_name0)) goto end; if (idx != 13) { if (!TEST_str_eq(SSL_get0_group_name(serverssl), kexch_name0) || !TEST_str_eq(SSL_get0_group_name(clientssl), kexch_name0)) goto end; if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0])) goto end; if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0])) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } # if !defined(OPENSSL_NO_TLS1_2) \ && !defined(OPENSSL_NO_EC) \ && !defined(OPENSSL_NO_DH) static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti, int isecdhe, int idx) { int kexch_alg; int *kexch_groups = &kexch_alg; int numec, numff; numec = OSSL_NELEM(ecdhe_kexch_groups); numff = OSSL_NELEM(ffdhe_kexch_groups); if (isecdhe) kexch_alg = ecdhe_kexch_groups[idx]; else kexch_alg = ffdhe_kexch_groups[idx]; if (clientmulti) { if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1))) return 0; if (isecdhe) { if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups, numec))) return 0; } else { if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups, numff))) return 0; } } else { if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1))) return 0; if (isecdhe) { if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups, numec))) return 0; } else { if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups, numff))) return 0; } } return 1; } static int test_negotiated_group(int idx) { int clientmulti, istls13, isecdhe, numec, numff, numgroups; int expectednid; SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; SSL_SESSION *origsess = NULL; int testresult = 0; int kexch_alg; int max_version = TLS1_3_VERSION; numec = OSSL_NELEM(ecdhe_kexch_groups); numff = OSSL_NELEM(ffdhe_kexch_groups); numgroups = numec + numff; clientmulti = (idx < 2 * numgroups); idx = idx % (2 * numgroups); istls13 = (idx < numgroups); idx = idx % numgroups; isecdhe = (idx < numec); if (!isecdhe) idx -= numec; if (isecdhe) kexch_alg = ecdhe_kexch_groups[idx]; else kexch_alg = ffdhe_kexch_groups[idx]; if (!istls13 && !isecdhe) expectednid = NID_undef; else expectednid = kexch_alg; if (!istls13) max_version = TLS1_2_VERSION; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, max_version, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_cipher_list(sctx, TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":" TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)) || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1))) goto end; if (!TEST_true(SSL_CTX_set_cipher_list(cctx, TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":" TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe, idx))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid) || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid)) goto end; if (!TEST_ptr((origsess = SSL_get1_session(clientssl)))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, origsess)) || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe, idx))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl))) goto end; if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid) || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid)) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (idx == 0) idx = 1; else idx--; if (istls13) { if (isecdhe) expectednid = ecdhe_kexch_groups[idx]; else expectednid = ffdhe_kexch_groups[idx]; if (!TEST_int_ne(expectednid, kexch_alg)) goto end; } else { if (isecdhe) expectednid = kexch_alg; else expectednid = 0; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, origsess)) || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe, idx))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl))) goto end; if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid) || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); SSL_SESSION_free(origsess); return testresult; } # endif static int test_tls13_ciphersuite(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; static const struct { const char *ciphername; int fipscapable; int low_security; } t13_ciphers[] = { { TLS1_3_RFC_AES_128_GCM_SHA256, 1, 0 }, { TLS1_3_RFC_AES_256_GCM_SHA384, 1, 0 }, { TLS1_3_RFC_AES_128_CCM_SHA256, 1, 0 }, # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 }, { TLS1_3_RFC_AES_256_GCM_SHA384 ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 }, # endif { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1, 1 } }; const char *t13_cipher = NULL; const char *t12_cipher = NULL; const char *negotiated_scipher; const char *negotiated_ccipher; int set_at_ctx = 0; int set_at_ssl = 0; int testresult = 0; int max_ver; size_t i; switch (idx) { case 0: set_at_ctx = 1; break; case 1: set_at_ssl = 1; break; case 2: set_at_ctx = 1; t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256; break; case 3: set_at_ssl = 1; t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256; break; } for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) { # ifdef OPENSSL_NO_TLS1_2 if (max_ver == TLS1_2_VERSION) continue; # endif for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) { if (is_fips && !t13_ciphers[i].fipscapable) continue; t13_cipher = t13_ciphers[i].ciphername; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, max_ver, &sctx, &cctx, cert, privkey))) goto end; if (t13_ciphers[i].low_security) { SSL_CTX_set_security_level(sctx, 0); SSL_CTX_set_security_level(cctx, 0); } if (set_at_ctx) { if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher)) || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher))) goto end; if (t12_cipher != NULL) { if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher)) || !TEST_true(SSL_CTX_set_cipher_list(cctx, t12_cipher))) goto end; } } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (set_at_ssl) { if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher)) || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher))) goto end; if (t12_cipher != NULL) { if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher)) || !TEST_true(SSL_set_cipher_list(clientssl, t12_cipher))) goto end; } } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher( serverssl)); negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher( clientssl)); if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher)) goto end; if (max_ver == TLS1_3_VERSION && !TEST_strn_eq(t13_cipher, negotiated_scipher, strlen(negotiated_scipher))) goto end; # ifndef OPENSSL_NO_TLS1_2 if (max_ver == TLS1_2_VERSION && t12_cipher != NULL && !TEST_str_eq(t12_cipher, negotiated_scipher)) goto end; # endif SSL_free(serverssl); serverssl = NULL; SSL_free(clientssl); clientssl = NULL; SSL_CTX_free(sctx); sctx = NULL; SSL_CTX_free(cctx); cctx = NULL; } } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_tls13_psk(int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; const SSL_CIPHER *cipher = NULL; const unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, idx == 3 ? NULL : cert, idx == 3 ? NULL : privkey))) goto end; if (idx != 3) { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) goto end; } else { if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384:" "TLS_AES_128_GCM_SHA256"))) goto end; } if (idx == 0 || idx == 1) { SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb); SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb); } #ifndef OPENSSL_NO_PSK if (idx >= 1) { SSL_CTX_set_psk_client_callback(cctx, psk_client_cb); SSL_CTX_set_psk_server_callback(sctx, psk_server_cb); } #endif srvid = pskid; use_session_cb_cnt = 0; find_session_cb_cnt = 0; psk_client_cb_cnt = 0; psk_server_cb_cnt = 0; if (idx != 3) { if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(SSL_session_reused(clientssl)) || !TEST_false(SSL_session_reused(serverssl))) goto end; if (idx == 0 || idx == 1) { if (!TEST_true(use_session_cb_cnt == 1) || !TEST_true(find_session_cb_cnt == 0) || !TEST_true(psk_client_cb_cnt == idx) || !TEST_true(psk_server_cb_cnt == 0)) goto end; } else { if (!TEST_true(use_session_cb_cnt == 0) || !TEST_true(find_session_cb_cnt == 0) || !TEST_true(psk_client_cb_cnt == 1) || !TEST_true(psk_server_cb_cnt == 0)) goto end; } shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; use_session_cb_cnt = psk_client_cb_cnt = 0; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES); clientpsk = SSL_SESSION_new(); if (!TEST_ptr(clientpsk) || !TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key, sizeof(key))) || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher)) || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk, TLS1_3_VERSION)) || !TEST_true(SSL_SESSION_up_ref(clientpsk))) goto end; serverpsk = clientpsk; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl)) || !TEST_true(SSL_session_reused(serverssl))) goto end; if (idx == 0 || idx == 1) { if (!TEST_true(use_session_cb_cnt == 1) || !TEST_true(find_session_cb_cnt == 1) || !TEST_true(psk_client_cb_cnt == 0) || !TEST_true(psk_server_cb_cnt == 0)) goto end; } else { if (!TEST_true(use_session_cb_cnt == 0) || !TEST_true(find_session_cb_cnt == 0) || !TEST_true(psk_client_cb_cnt == 1) || !TEST_true(psk_server_cb_cnt == 1)) goto end; } shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; use_session_cb_cnt = find_session_cb_cnt = 0; psk_client_cb_cnt = psk_server_cb_cnt = 0; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; #if defined(OPENSSL_NO_EC) if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072"))) goto end; #else if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384"))) goto end; #endif if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl)) || !TEST_true(SSL_session_reused(serverssl))) goto end; if (idx == 0 || idx == 1) { if (!TEST_true(use_session_cb_cnt == 2) || !TEST_true(find_session_cb_cnt == 2) || !TEST_true(psk_client_cb_cnt == 0) || !TEST_true(psk_server_cb_cnt == 0)) goto end; } else { if (!TEST_true(use_session_cb_cnt == 0) || !TEST_true(find_session_cb_cnt == 0) || !TEST_true(psk_client_cb_cnt == 2) || !TEST_true(psk_server_cb_cnt == 2)) goto end; } shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; use_session_cb_cnt = find_session_cb_cnt = 0; psk_client_cb_cnt = psk_server_cb_cnt = 0; if (idx != 3) { srvid = "Dummy Identity"; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(SSL_session_reused(clientssl)) || !TEST_false(SSL_session_reused(serverssl))) goto end; if (idx == 0 || idx == 1) { if (!TEST_true(use_session_cb_cnt == 1) || !TEST_true(find_session_cb_cnt == 1) || !TEST_true(psk_client_cb_cnt == 0) || !TEST_true(psk_server_cb_cnt == idx)) goto end; } else { if (!TEST_true(use_session_cb_cnt == 0) || !TEST_true(find_session_cb_cnt == 0) || !TEST_true(psk_client_cb_cnt == 1) || !TEST_true(psk_server_cb_cnt == 1)) goto end; } shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; } testresult = 1; end: SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #ifndef OSSL_NO_USABLE_TLS1_3 static int test_tls13_no_dhe_kex(const int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; size_t j; SSL_SESSION *saved_session; int server_allow_no_dhe = (idx & 1) != 0; int server_prefer_no_dhe = (idx & 2) != 0; int client_allow_no_dhe = (idx & 4) != 0; uint64_t server_options = 0 | (server_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0) | (server_prefer_no_dhe ? SSL_OP_PREFER_NO_DHE_KEX : 0); uint64_t client_options = 0 | (client_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0); new_called = 0; do_cache = 1; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); SSL_CTX_set_options(sctx, server_options); SSL_CTX_set_options(cctx, client_options); SSL_CTX_sess_set_new_cb(cctx, new_cachesession_cb); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(2, new_called)) goto end; saved_session = sesscache[new_called - 1]; SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(cctx); clientssl = serverssl = NULL; cctx = NULL; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, NULL, &cctx, cert, privkey))) goto end; SSL_CTX_set_options(cctx, client_options); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, saved_session))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_session_reused(clientssl))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); for (j = 0; j < OSSL_NELEM(sesscache); j++) { SSL_SESSION_free(sesscache[j]); sesscache[j] = NULL; } SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static unsigned char cookie_magic_value[] = "cookie magic"; static int generate_cookie_callback(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) { memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1); *cookie_len = sizeof(cookie_magic_value) - 1; return 1; } static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len) { if (cookie_len == sizeof(cookie_magic_value) - 1 && memcmp(cookie, cookie_magic_value, cookie_len) == 0) return 1; return 0; } static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie, size_t *cookie_len) { unsigned int temp; int res = generate_cookie_callback(ssl, cookie, &temp); *cookie_len = temp; return res; } static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie, size_t cookie_len) { return verify_cookie_callback(ssl, cookie, cookie_len); } static int test_stateless(void) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_READ)) || !TEST_int_eq(SSL_stateless(serverssl), -1)) goto end; SSL_free(clientssl); clientssl = NULL; SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback); SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_READ)) || !TEST_int_eq(SSL_stateless(serverssl), 0)) goto end; SSL_free(clientssl); clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_READ)) || !TEST_int_eq(SSL_stateless(serverssl), 0) || !TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_READ)) || !TEST_int_eq(SSL_stateless(serverssl), 1) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; shutdown_ssl_connection(serverssl, clientssl); serverssl = clientssl = NULL; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int clntaddoldcb = 0; static int clntparseoldcb = 0; static int srvaddoldcb = 0; static int srvparseoldcb = 0; static int clntaddnewcb = 0; static int clntparsenewcb = 0; static int srvaddnewcb = 0; static int srvparsenewcb = 0; static int snicb = 0; #define TEST_EXT_TYPE1 0xff00 static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) { int *server = (int *)add_arg; unsigned char *data; if (SSL_is_server(s)) srvaddoldcb++; else clntaddoldcb++; if (*server != SSL_is_server(s) || (data = OPENSSL_malloc(sizeof(*data))) == NULL) return -1; *data = 1; *out = data; *outlen = sizeof(char); return 1; } static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out, void *add_arg) { OPENSSL_free((unsigned char *)out); } static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) { int *server = (int *)parse_arg; if (SSL_is_server(s)) srvparseoldcb++; else clntparseoldcb++; if (*server != SSL_is_server(s) || inlen != sizeof(char) || *in != 1) return -1; return 1; } static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char **out, size_t *outlen, X509 *x, size_t chainidx, int *al, void *add_arg) { int *server = (int *)add_arg; unsigned char *data; if (SSL_is_server(s)) srvaddnewcb++; else clntaddnewcb++; if (*server != SSL_is_server(s) || (data = OPENSSL_malloc(sizeof(*data))) == NULL) return -1; *data = 1; *out = data; *outlen = sizeof(*data); return 1; } static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char *out, void *add_arg) { OPENSSL_free((unsigned char *)out); } static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char *in, size_t inlen, X509 *x, size_t chainidx, int *al, void *parse_arg) { int *server = (int *)parse_arg; if (SSL_is_server(s)) srvparsenewcb++; else clntparsenewcb++; if (*server != SSL_is_server(s) || inlen != sizeof(char) || *in != 1) return -1; return 1; } static int sni_cb(SSL *s, int *al, void *arg) { SSL_CTX *ctx = (SSL_CTX *)arg; if (SSL_set_SSL_CTX(s, ctx) == NULL) { *al = SSL_AD_INTERNAL_ERROR; return SSL_TLSEXT_ERR_ALERT_FATAL; } snicb++; return SSL_TLSEXT_ERR_OK; } static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) { return 1; } static int test_custom_exts(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; static int server = 1; static int client = 0; SSL_SESSION *sess = NULL; unsigned int context; #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3) if (tst < 3) return 1; #endif clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0; clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0; snicb = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (tst == 2 && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL, TLS1_VERSION, 0, &sctx2, NULL, cert, privkey))) goto end; if (tst < 3) { SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3); SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3); if (sctx2 != NULL) SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3); } if (tst == 5) { context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_CERTIFICATE; SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb); if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1)) goto end; } else if (tst == 4) { context = SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_NEW_SESSION_TICKET; } else { context = SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS; } if (tst == 0) { if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, old_add_cb, old_free_cb, &client, old_parse_cb, &client))) goto end; } else { if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context, new_add_cb, new_free_cb, &client, new_parse_cb, &client))) goto end; } if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, old_add_cb, old_free_cb, &client, old_parse_cb, &client)) || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context, new_add_cb, new_free_cb, &client, new_parse_cb, &client))) goto end; if (tst == 0) { if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, old_add_cb, old_free_cb, &server, old_parse_cb, &server))) goto end; } else { if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context, new_add_cb, new_free_cb, &server, new_parse_cb, &server))) goto end; if (sctx2 != NULL && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1, context, new_add_cb, new_free_cb, &server, new_parse_cb, &server))) goto end; } if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, old_add_cb, old_free_cb, &server, old_parse_cb, &server)) || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context, new_add_cb, new_free_cb, &server, new_parse_cb, &server))) goto end; if (tst == 2) { if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb)) || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2))) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (tst == 0) { if (clntaddoldcb != 1 || clntparseoldcb != 1 || srvaddoldcb != 1 || srvparseoldcb != 1) goto end; } else if (tst == 1 || tst == 2 || tst == 3) { if (clntaddnewcb != 1 || clntparsenewcb != 1 || srvaddnewcb != 1 || srvparsenewcb != 1 || (tst != 2 && snicb != 0) || (tst == 2 && snicb != 1)) goto end; } else if (tst == 5) { if (clntaddnewcb != 1 || clntparsenewcb != 1 || srvaddnewcb != 1 || srvparsenewcb != 1) goto end; } else { if (clntaddnewcb != 1 || clntparsenewcb != 5 || srvaddnewcb != 5 || srvparsenewcb != 1) goto end; } sess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (tst == 3 || tst == 5) { testresult = 1; goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, sess)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (tst == 0) { if (clntaddoldcb != 2 || clntparseoldcb != 1 || srvaddoldcb != 1 || srvparseoldcb != 1) goto end; } else if (tst == 1 || tst == 2 || tst == 3) { if (clntaddnewcb != 2 || clntparsenewcb != 2 || srvaddnewcb != 2 || srvparsenewcb != 2) goto end; } else { if (clntaddnewcb != 2 || clntparsenewcb != 8 || srvaddnewcb != 8 || srvparsenewcb != 2) goto end; } testresult = 1; end: SSL_SESSION_free(sess); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx2); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3) #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \ | SSL_EXT_CLIENT_HELLO \ | SSL_EXT_TLS1_2_SERVER_HELLO \ | SSL_EXT_IGNORE_ON_RESUMPTION) #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \ | SSL_EXT_TLS1_2_SERVER_HELLO \ | SSL_EXT_CLIENT_HELLO) #define SERVERINFO_CUSTOM \ 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \ 0x00, 0x03, \ 0x04, 0x05, 0x06 \ static const unsigned char serverinfo_custom_tls13[] = { 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff, SERVERINFO_CUSTOM }; static const unsigned char serverinfo_custom_v2[] = { 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff, SERVERINFO_CUSTOM }; static const unsigned char serverinfo_custom_v1[] = { SERVERINFO_CUSTOM }; static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13); static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2); static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1); static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type, unsigned int context, const unsigned char *in, size_t inlen, X509 *x, size_t chainidx, int *al, void *parse_arg) { const size_t len = serverinfo_custom_v1_len; const unsigned char *si = &serverinfo_custom_v1[len - 3]; int *p_cb_result = (int*)parse_arg; *p_cb_result = TEST_mem_eq(in, inlen, si, 3); return 1; } static int test_serverinfo_custom(const int idx) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; int cb_result = 0; int serverinfo_version = 0; int protocol_version = 0; unsigned int extension_context = 0; const unsigned char *si = NULL; size_t si_len = 0; const int call_use_serverinfo_ex = idx > 0; switch (idx) { case 0: case 1: serverinfo_version = SSL_SERVERINFOV1; protocol_version = TLS1_2_VERSION; extension_context = SYNTHV1CONTEXT; si = serverinfo_custom_v1; si_len = serverinfo_custom_v1_len; break; case 2: serverinfo_version = SSL_SERVERINFOV2; protocol_version = TLS1_2_VERSION; extension_context = SYNTHV1CONTEXT; si = serverinfo_custom_v2; si_len = serverinfo_custom_v2_len; break; case 3: serverinfo_version = SSL_SERVERINFOV2; protocol_version = TLS1_3_VERSION; extension_context = TLS13CONTEXT; si = serverinfo_custom_tls13; si_len = serverinfo_custom_tls13_len; break; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_method(), TLS_method(), protocol_version, protocol_version, &sctx, &cctx, cert, privkey))) goto end; if (call_use_serverinfo_ex) { if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version, si, si_len))) goto end; } else { if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len))) goto end; } if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp, extension_context, NULL, NULL, NULL, serverinfo_custom_parse_cb, &cb_result)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_int_eq(SSL_do_handshake(clientssl), 1)) goto end; if (!TEST_true(cb_result)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif #define SMALL_LABEL_LEN 10 #define LONG_LABEL_LEN 249 static int test_export_key_mat(int tst) { int testresult = 0; SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL; SSL *clientssl = NULL, *serverssl = NULL; const char label[LONG_LABEL_LEN + 1] = "test label"; const unsigned char context[] = "context"; const unsigned char *emptycontext = NULL; unsigned char longcontext[1280]; int test_longcontext = fips_provider_version_ge(libctx, 3, 3, 0); unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80], ckeymat4[80]; unsigned char skeymat1[80], skeymat2[80], skeymat3[80], skeymat4[80]; size_t labellen; const int protocols[] = { TLS1_VERSION, TLS1_1_VERSION, TLS1_2_VERSION, TLS1_3_VERSION, TLS1_3_VERSION, TLS1_3_VERSION }; #ifdef OPENSSL_NO_TLS1 if (tst == 0) return 1; #endif #ifdef OPENSSL_NO_TLS1_1 if (tst == 1) return 1; #endif if (is_fips && (tst == 0 || tst == 1)) return 1; #ifdef OPENSSL_NO_TLS1_2 if (tst == 2) return 1; #endif #ifdef OSSL_NO_USABLE_TLS1_3 if (tst >= 3) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols)); SSL_CTX_set_max_proto_version(cctx, protocols[tst]); SSL_CTX_set_min_proto_version(cctx, protocols[tst]); if ((protocols[tst] < TLS1_2_VERSION) && (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0") || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1, sizeof(ckeymat1), label, SMALL_LABEL_LEN + 1, context, sizeof(context) - 1, 1), 0)) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (tst == 5) { if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1, sizeof(ckeymat1), label, LONG_LABEL_LEN + 1, context, sizeof(context) - 1, 1), 0)) goto end; testresult = 1; goto end; } else if (tst == 4) { labellen = LONG_LABEL_LEN; } else { labellen = SMALL_LABEL_LEN; } memset(longcontext, 1, sizeof(longcontext)); if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1, sizeof(ckeymat1), label, labellen, context, sizeof(context) - 1, 1), 1) || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2, sizeof(ckeymat2), label, labellen, emptycontext, 0, 1), 1) || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3, sizeof(ckeymat3), label, labellen, NULL, 0, 0), 1) || (test_longcontext && !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat4, sizeof(ckeymat4), label, labellen, longcontext, sizeof(longcontext), 1), 1)) || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1, sizeof(skeymat1), label, labellen, context, sizeof(context) -1, 1), 1) || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2, sizeof(skeymat2), label, labellen, emptycontext, 0, 1), 1) || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3, sizeof(skeymat3), label, labellen, NULL, 0, 0), 1) || (test_longcontext && !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat4, sizeof(skeymat4), label, labellen, longcontext, sizeof(longcontext), 1), 1)) || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1, sizeof(skeymat1)) || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2, sizeof(skeymat2)) || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3, sizeof(skeymat3)) || (test_longcontext && !TEST_mem_eq(ckeymat4, sizeof(ckeymat4), skeymat4, sizeof(skeymat4))) || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2, sizeof(ckeymat2))) goto end; if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3, sizeof(ckeymat3))) || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3, sizeof(ckeymat3)))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx2); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #ifndef OSSL_NO_USABLE_TLS1_3 static int test_export_key_mat_early(int idx) { static const char label[] = "test label"; static const unsigned char context[] = "context"; int testresult = 0; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *sess = NULL; const unsigned char *emptycontext = NULL; unsigned char ckeymat1[80], ckeymat2[80]; unsigned char skeymat1[80], skeymat2[80]; unsigned char buf[1]; size_t readbytes, written; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx, SHA384_DIGEST_LENGTH))) goto end; if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_ERROR) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED)) goto end; if (!TEST_int_eq(SSL_export_keying_material_early( clientssl, ckeymat1, sizeof(ckeymat1), label, sizeof(label) - 1, context, sizeof(context) - 1), 1) || !TEST_int_eq(SSL_export_keying_material_early( clientssl, ckeymat2, sizeof(ckeymat2), label, sizeof(label) - 1, emptycontext, 0), 1) || !TEST_int_eq(SSL_export_keying_material_early( serverssl, skeymat1, sizeof(skeymat1), label, sizeof(label) - 1, context, sizeof(context) - 1), 1) || !TEST_int_eq(SSL_export_keying_material_early( serverssl, skeymat2, sizeof(skeymat2), label, sizeof(label) - 1, emptycontext, 0), 1) || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1, sizeof(skeymat1)) || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2, sizeof(skeymat2)) || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2, sizeof(ckeymat2))) goto end; testresult = 1; end: SSL_SESSION_free(sess); SSL_SESSION_free(clientpsk); SSL_SESSION_free(serverpsk); clientpsk = serverpsk = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #define NUM_KEY_UPDATE_MESSAGES 40 static int test_key_update(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, i, j; char buf[20]; static char *mess = "A test message"; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; for (j = 0; j < 2; j++) { for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) { if (!TEST_true(SSL_key_update(clientssl, (j == 0) ? SSL_KEY_UPDATE_NOT_REQUESTED : SSL_KEY_UPDATE_REQUESTED)) || !TEST_true(SSL_do_handshake(clientssl))) goto end; } if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)), strlen(mess))) goto end; if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)), strlen(mess))) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_key_update_peer_in_write(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char buf[20]; static char *mess = "A test message"; BIO *bretry = BIO_new(bio_s_always_retry()); BIO *tmp = NULL; SSL *peerupdate = NULL, *peerwrite = NULL; if (!TEST_ptr(bretry) || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; peerupdate = tst == 0 ? clientssl : serverssl; peerwrite = tst == 0 ? serverssl : clientssl; if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED)) || !TEST_int_eq(SSL_do_handshake(peerupdate), 1)) goto end; tmp = SSL_get_wbio(peerwrite); if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { tmp = NULL; goto end; } SSL_set0_wbio(peerwrite, bretry); bretry = NULL; if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1) || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE) || !TEST_true(SSL_want_write(peerwrite)) || !TEST_true(SSL_net_write_desired(peerwrite))) goto end; SSL_set0_wbio(peerwrite, tmp); tmp = NULL; if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1) || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ) || !TEST_true(SSL_want_read(peerwrite)) || !TEST_true(SSL_net_read_desired(peerwrite))) goto end; if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess))) goto end; if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess))) goto end; if (!TEST_false(SSL_net_read_desired(peerwrite)) || !TEST_false(SSL_net_write_desired(peerwrite)) || !TEST_int_eq(SSL_want(peerwrite), SSL_NOTHING)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); BIO_free(bretry); BIO_free(tmp); return testresult; } static int test_key_update_peer_in_read(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char prbuf[515], lwbuf[515] = {0}; static char *mess = "A test message"; BIO *lbio = NULL, *pbio = NULL; SSL *local = NULL, *peer = NULL; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; local = tst == 0 ? clientssl : serverssl; peer = tst == 0 ? serverssl : clientssl; if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1)) goto end; SSL_set_bio(local, lbio, lbio); SSL_set_bio(peer, pbio, pbio); if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED)) || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1) || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE)) goto end; if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1) || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ)) goto end; if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))) goto end; if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf)) || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf))) goto end; if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_key_update_local_in_write(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char buf[20]; static char *mess = "A test message"; BIO *bretry = BIO_new(bio_s_always_retry()); BIO *tmp = NULL; SSL *local = NULL, *peer = NULL; if (!TEST_ptr(bretry) || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; local = tst == 0 ? clientssl : serverssl; peer = tst == 0 ? serverssl : clientssl; tmp = SSL_get_wbio(local); if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { tmp = NULL; goto end; } SSL_set0_wbio(local, bretry); bretry = NULL; if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1) || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE)) goto end; SSL_set0_wbio(local, tmp); tmp = NULL; if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED)) || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY)) goto end; ERR_clear_error(); if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))) goto end; if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED)) || !TEST_int_eq(SSL_do_handshake(local), 1)) goto end; if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess))) goto end; if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); BIO_free(bretry); BIO_free(tmp); return testresult; } static int test_key_update_local_in_read(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char lrbuf[515], pwbuf[515] = {0}, prbuf[20]; static char *mess = "A test message"; BIO *lbio = NULL, *pbio = NULL; SSL *local = NULL, *peer = NULL; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; local = tst == 0 ? clientssl : serverssl; peer = tst == 0 ? serverssl : clientssl; if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1)) goto end; SSL_set_bio(local, lbio, lbio); SSL_set_bio(peer, pbio, pbio); if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1) || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE)) goto end; if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1) || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ)) goto end; if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED)) || !TEST_int_eq(SSL_do_handshake(local), 1)) goto end; if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf)) || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf))) goto end; if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess))) goto end; if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)) || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int test_ssl_clear(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL *writer, *reader; int testresult = 0; int tls12test, servertest, cleartest; size_t written, readbytes; const char *msg = "Hello World"; unsigned char buf[5]; tls12test = idx & 1; idx >>= 1; servertest = idx & 1; idx >>= 1; cleartest = idx & 1; #ifdef OPENSSL_NO_TLS1_2 if (tls12test == 1) return TEST_skip("No TLSv1.2 in this build"); #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) || (tls12test && !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (servertest) { writer = clientssl; reader = serverssl; } else { writer = serverssl; reader = clientssl; } if (!TEST_true(SSL_write_ex(writer, msg, strlen(msg), &written)) || written != strlen(msg)) goto end; if (!TEST_true(SSL_read_ex(reader, buf, sizeof(buf), &readbytes)) || readbytes != sizeof(buf)) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); if (servertest) { if (cleartest) { if (!TEST_true(SSL_clear(serverssl))) goto end; } else { SSL_set_accept_state(serverssl); } if (!TEST_true(SSL_set_session(serverssl, NULL))) goto end; SSL_free(clientssl); clientssl = NULL; } else { if (cleartest) { if (!TEST_true(SSL_clear(clientssl))) goto end; } else { SSL_set_connect_state(clientssl); } SSL_free(serverssl); serverssl = NULL; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(servertest || SSL_session_reused(clientssl))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code) { long len; unsigned char *data; PACKET pkt, pkt2, pkt3; unsigned int MFL_code = 0, type = 0; if (!TEST_uint_gt(len = BIO_get_mem_data(bio, (char **) &data), 0)) goto end; memset(&pkt, 0, sizeof(pkt)); memset(&pkt2, 0, sizeof(pkt2)); memset(&pkt3, 0, sizeof(pkt3)); if (!TEST_long_gt(len, 0) || !TEST_true(PACKET_buf_init(&pkt, data, len)) || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH) || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) goto end; while (PACKET_remaining(&pkt2)) { if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) goto end; if (type == TLSEXT_TYPE_max_fragment_length) { if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0) || !TEST_true(PACKET_get_1(&pkt3, &MFL_code))) goto end; *mfl_codemfl_code = MFL_code; return 1; } } end: return 0; } static const unsigned char max_fragment_len_test[] = { TLSEXT_max_fragment_length_512, TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048, TLSEXT_max_fragment_length_4096 }; static int test_max_fragment_len_ext(int idx_tst) { SSL_CTX *ctx = NULL; SSL *con = NULL; int testresult = 0, MFL_mode = 0; BIO *rbio, *wbio; if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(), TLS1_VERSION, 0, NULL, &ctx, NULL, NULL))) return 0; if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length( ctx, max_fragment_len_test[idx_tst]))) goto end; con = SSL_new(ctx); if (!TEST_ptr(con)) goto end; rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { BIO_free(rbio); BIO_free(wbio); goto end; } SSL_set_bio(con, rbio, wbio); if (!TEST_int_le(SSL_connect(con), 0)) { goto end; } if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode))) goto end; if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode)) goto end; testresult = 1; end: SSL_free(con); SSL_CTX_free(ctx); return testresult; } #ifndef OSSL_NO_USABLE_TLS1_3 static int test_pha_key_update(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) return 0; if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION)) || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION)) || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION)) || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION))) goto end; SSL_CTX_set_post_handshake_auth(cctx, 1); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); if (!TEST_true(SSL_verify_client_post_handshake(serverssl))) goto end; if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED))) goto end; if (!TEST_int_eq(SSL_do_handshake(serverssl), 1)) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; SSL_shutdown(clientssl); SSL_shutdown(serverssl); testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2) static SRP_VBASE *vbase = NULL; static int ssl_srp_cb(SSL *s, int *ad, void *arg) { int ret = SSL3_AL_FATAL; char *username; SRP_user_pwd *user = NULL; username = SSL_get_srp_username(s); if (username == NULL) { *ad = SSL_AD_INTERNAL_ERROR; goto err; } user = SRP_VBASE_get1_by_user(vbase, username); if (user == NULL) { *ad = SSL_AD_INTERNAL_ERROR; goto err; } if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v, user->info) <= 0) { *ad = SSL_AD_INTERNAL_ERROR; goto err; } ret = 0; err: SRP_user_pwd_free(user); return ret; } static int create_new_vfile(char *userid, char *password, const char *filename) { char *gNid = NULL; OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1)); TXT_DB *db = NULL; int ret = 0; BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0); size_t i; if (!TEST_ptr(dummy) || !TEST_ptr(row)) goto end; gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt], &row[DB_srpverifier], NULL, NULL, libctx, NULL); if (!TEST_ptr(gNid)) goto end; db = TXT_DB_read(dummy, DB_NUMBER); if (!TEST_ptr(db)) goto end; out = BIO_new_file(filename, "w"); if (!TEST_ptr(out)) goto end; row[DB_srpid] = OPENSSL_strdup(userid); row[DB_srptype] = OPENSSL_strdup("V"); row[DB_srpgN] = OPENSSL_strdup(gNid); if (!TEST_ptr(row[DB_srpid]) || !TEST_ptr(row[DB_srptype]) || !TEST_ptr(row[DB_srpgN]) || !TEST_true(TXT_DB_insert(db, row))) goto end; row = NULL; if (TXT_DB_write(out, db) <= 0) goto end; ret = 1; end: if (row != NULL) { for (i = 0; i < DB_NUMBER; i++) OPENSSL_free(row[i]); } OPENSSL_free(row); BIO_free(dummy); BIO_free(out); TXT_DB_free(db); return ret; } static int create_new_vbase(char *userid, char *password) { BIGNUM *verifier = NULL, *salt = NULL; const SRP_gN *lgN = NULL; SRP_user_pwd *user_pwd = NULL; int ret = 0; lgN = SRP_get_default_gN(NULL); if (!TEST_ptr(lgN)) goto end; if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier, lgN->N, lgN->g, libctx, NULL))) goto end; user_pwd = OPENSSL_zalloc(sizeof(*user_pwd)); if (!TEST_ptr(user_pwd)) goto end; user_pwd->N = lgN->N; user_pwd->g = lgN->g; user_pwd->id = OPENSSL_strdup(userid); if (!TEST_ptr(user_pwd->id)) goto end; user_pwd->v = verifier; user_pwd->s = salt; verifier = salt = NULL; if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0) goto end; user_pwd = NULL; ret = 1; end: SRP_user_pwd_free(user_pwd); BN_free(salt); BN_free(verifier); return ret; } static int test_srp(int tst) { char *userid = "test", *password = "password", *tstsrpfile; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int ret, testresult = 0; vbase = SRP_VBASE_new(NULL); if (!TEST_ptr(vbase)) goto end; if (tst == 0 || tst == 1) { if (!TEST_true(create_new_vbase(userid, password))) goto end; } else { if (tst == 4 || tst == 5) { if (!TEST_true(create_new_vfile(userid, password, tmpfilename))) goto end; tstsrpfile = tmpfilename; } else { tstsrpfile = srpvfile; } if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR)) goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0) || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA")) || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION)) || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)) || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0)) goto end; if (tst % 2 == 1) { if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0)) goto end; } else { if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0)) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); if (ret) { if (!TEST_true(tst % 2 == 0)) goto end; } else { if (!TEST_true(tst % 2 == 1)) goto end; } testresult = 1; end: SRP_VBASE_free(vbase); vbase = NULL; SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int info_cb_failed = 0; static int info_cb_offset = 0; static int info_cb_this_state = -1; static struct info_cb_states_st { int where; const char *statestr; } info_cb_states[][60] = { { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"}, {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSCC"}, {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSCC"}, {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, }, { {0, NULL}, } }; static void sslapi_info_callback(const SSL *s, int where, int ret) { struct info_cb_states_st *state = info_cb_states[info_cb_offset]; if (!TEST_false(ret == 0)) { info_cb_failed = 1; return; } if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0)) || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0) || !TEST_int_ne(state[++info_cb_this_state].where, 0)) { info_cb_failed = 1; return; } if (!TEST_true((where & state[info_cb_this_state].where) != 0)) { info_cb_failed = 1; return; } if ((where & SSL_CB_LOOP) != 0 && !TEST_int_eq(strcmp(SSL_state_string(s), state[info_cb_this_state].statestr), 0)) { info_cb_failed = 1; return; } if ((where & SSL_CB_HANDSHAKE_DONE) && SSL_in_init((SSL *)s) != 0) { info_cb_failed = 1; return; } } static int test_info_callback(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *clntsess = NULL; int testresult = 0; int tlsvers; if (tst < 2) { #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \ || !defined(OPENSSL_NO_DH)) tlsvers = TLS1_2_VERSION; #else return 1; #endif } else { #ifndef OSSL_NO_USABLE_TLS1_3 tlsvers = TLS1_3_VERSION; #else return 1; #endif } info_cb_failed = 0; info_cb_this_state = -1; info_cb_offset = tst; #ifndef OSSL_NO_USABLE_TLS1_3 if (tst >= 4 && tst < 6) { SSL_SESSION *sess = NULL; size_t written, readbytes; unsigned char buf[80]; if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, 0, SHA384_DIGEST_LENGTH))) goto end; SSL_SESSION_free(sess); SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl, sslapi_info_callback); if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) || !TEST_size_t_eq(written, strlen(MSG1)) || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS) || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1)) || !TEST_int_eq(SSL_get_early_data_status(serverssl), SSL_EARLY_DATA_ACCEPTED) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(info_cb_failed)) goto end; testresult = 1; goto end; } #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), tlsvers, tlsvers, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1))) goto end; SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx, sslapi_info_callback); if (tst >= 6) { if (!SSL_CTX_compress_certs(sctx, 0)) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_false(info_cb_failed)) goto end; clntsess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, clntsess)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_true(SSL_session_reused(clientssl)) || !TEST_false(info_cb_failed)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_SESSION_free(clntsess); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_ssl_pending(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char msg[] = "A test message"; char buf[5]; size_t written, readbytes; if (tst == 0) { if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; } else { #ifndef OPENSSL_NO_DTLS if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(), DTLS_client_method(), DTLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; # ifdef OPENSSL_NO_DTLS1_2 if (is_fips) { testresult = 1; goto end; }; 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 #else return 1; #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; if (!TEST_int_eq(SSL_pending(clientssl), 0) || !TEST_false(SSL_has_pending(clientssl)) || !TEST_int_eq(SSL_pending(serverssl), 0) || !TEST_false(SSL_has_pending(serverssl)) || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) || !TEST_size_t_eq(written, sizeof(msg)) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_size_t_eq(readbytes, sizeof(buf)) || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes)) || !TEST_true(SSL_has_pending(clientssl))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static struct { unsigned int maxprot; const char *clntciphers; const char *clnttls13ciphers; const char *srvrciphers; const char *srvrtls13ciphers; const char *shared; const char *fipsshared; } shared_ciphers_data[] = { #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) { TLS1_2_VERSION, "AES128-SHA:AES256-SHA", NULL, "AES256-SHA:DHE-RSA-AES128-SHA", NULL, "AES256-SHA", "AES256-SHA" }, # if !defined(OPENSSL_NO_CHACHA) \ && !defined(OPENSSL_NO_POLY1305) \ && !defined(OPENSSL_NO_EC) { TLS1_2_VERSION, "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", NULL, "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", NULL, "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", "AES128-SHA" }, # endif { TLS1_2_VERSION, "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA", NULL, "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA", NULL, "AES128-SHA:AES256-SHA", "AES128-SHA:AES256-SHA" }, { TLS1_2_VERSION, "AES128-SHA:AES256-SHA", NULL, "AES128-SHA:DHE-RSA-AES128-SHA", NULL, "AES128-SHA", "AES128-SHA" }, #endif #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \ && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) { TLS1_3_VERSION, "AES128-SHA:AES256-SHA", NULL, "AES256-SHA:AES128-SHA256", NULL, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:" "TLS_AES_128_GCM_SHA256:AES256-SHA", "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA" }, #endif #ifndef OSSL_NO_USABLE_TLS1_3 { TLS1_3_VERSION, "AES128-SHA", "TLS_AES_256_GCM_SHA384", "AES256-SHA", "TLS_AES_256_GCM_SHA384", "TLS_AES_256_GCM_SHA384", "TLS_AES_256_GCM_SHA384" }, #endif }; static int int_test_ssl_get_shared_ciphers(int tst, int clnt) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char buf[1024]; OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(tmplibctx)) goto end; if (clnt) { cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method()); if (!TEST_ptr(cctx)) goto end; } else { sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method()); if (!TEST_ptr(sctx)) goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, shared_ciphers_data[tst].maxprot, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_cipher_list(cctx, shared_ciphers_data[tst].clntciphers)) || (shared_ciphers_data[tst].clnttls13ciphers != NULL && !TEST_true(SSL_CTX_set_ciphersuites(cctx, shared_ciphers_data[tst].clnttls13ciphers))) || !TEST_true(SSL_CTX_set_cipher_list(sctx, shared_ciphers_data[tst].srvrciphers)) || (shared_ciphers_data[tst].srvrtls13ciphers != NULL && !TEST_true(SSL_CTX_set_ciphersuites(sctx, shared_ciphers_data[tst].srvrtls13ciphers)))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf))) || !TEST_int_eq(strcmp(buf, is_fips ? shared_ciphers_data[tst].fipsshared : shared_ciphers_data[tst].shared), 0)) { TEST_info("Shared ciphers are: %s\n", buf); goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); OSSL_LIB_CTX_free(tmplibctx); return testresult; } static int test_ssl_get_shared_ciphers(int tst) { return int_test_ssl_get_shared_ciphers(tst, 0) && int_test_ssl_get_shared_ciphers(tst, 1); } static const char *appdata = "Hello World"; static int gen_tick_called, dec_tick_called, tick_key_cb_called; static int tick_key_renew = 0; static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT; static int gen_tick_cb(SSL *s, void *arg) { gen_tick_called = 1; return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata, strlen(appdata)); } static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss, const unsigned char *keyname, size_t keyname_length, SSL_TICKET_STATUS status, void *arg) { void *tickdata; size_t tickdlen; dec_tick_called = 1; if (status == SSL_TICKET_EMPTY) return SSL_TICKET_RETURN_IGNORE_RENEW; if (!TEST_true(status == SSL_TICKET_SUCCESS || status == SSL_TICKET_SUCCESS_RENEW)) return SSL_TICKET_RETURN_ABORT; if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata, &tickdlen)) || !TEST_size_t_eq(tickdlen, strlen(appdata)) || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0)) return SSL_TICKET_RETURN_ABORT; if (tick_key_cb_called) { switch (status) { case SSL_TICKET_NO_DECRYPT: return SSL_TICKET_RETURN_IGNORE_RENEW; case SSL_TICKET_SUCCESS: return SSL_TICKET_RETURN_USE; case SSL_TICKET_SUCCESS_RENEW: return SSL_TICKET_RETURN_USE_RENEW; default: return SSL_TICKET_RETURN_ABORT; } } return tick_dec_ret; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int tick_key_cb(SSL *s, unsigned char key_name[16], unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc) { const unsigned char tick_aes_key[16] = "0123456789abcdef"; const unsigned char tick_hmac_key[16] = "0123456789abcdef"; EVP_CIPHER *aes128cbc; EVP_MD *sha256; int ret; tick_key_cb_called = 1; if (tick_key_renew == -1) return 0; aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL); if (!TEST_ptr(aes128cbc)) return 0; sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL); if (!TEST_ptr(sha256)) { EVP_CIPHER_free(aes128cbc); return 0; } memset(iv, 0, AES_BLOCK_SIZE); memset(key_name, 0, 16); if (aes128cbc == NULL || sha256 == NULL || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc) || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256, NULL)) ret = -1; else ret = tick_key_renew ? 2 : 1; EVP_CIPHER_free(aes128cbc); EVP_MD_free(sha256); return ret; } #endif static int tick_key_evp_cb(SSL *s, unsigned char key_name[16], unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc) { const unsigned char tick_aes_key[16] = "0123456789abcdef"; unsigned char tick_hmac_key[16] = "0123456789abcdef"; OSSL_PARAM params[2]; EVP_CIPHER *aes128cbc; int ret; tick_key_cb_called = 1; if (tick_key_renew == -1) return 0; aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL); if (!TEST_ptr(aes128cbc)) return 0; memset(iv, 0, AES_BLOCK_SIZE); memset(key_name, 0, 16); params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, "SHA256", 0); params[1] = OSSL_PARAM_construct_end(); if (aes128cbc == NULL || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc) || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key), params)) ret = -1; else ret = tick_key_renew ? 2 : 1; EVP_CIPHER_free(aes128cbc); return ret; } static int test_ticket_callbacks(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; SSL_SESSION *clntsess = NULL; int testresult = 0; #ifdef OPENSSL_NO_TLS1_2 if (tst % 2 == 0) return 1; #endif #ifdef OSSL_NO_USABLE_TLS1_3 if (tst % 2 == 1) return 1; #endif #ifdef OPENSSL_NO_DEPRECATED_3_0 if (tst >= 8 && tst <= 13) return 1; #endif gen_tick_called = dec_tick_called = tick_key_cb_called = 0; if (tst == 10 || tst == 11 || tst == 16 || tst == 17) tick_key_renew = 1; else if (tst == 12 || tst == 13 || tst == 18 || tst == 19) tick_key_renew = -1; else tick_key_renew = 0; switch (tst) { case 0: case 1: tick_dec_ret = SSL_TICKET_RETURN_IGNORE; break; case 2: case 3: tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW; break; case 4: case 5: tick_dec_ret = SSL_TICKET_RETURN_USE; break; case 6: case 7: tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW; break; default: tick_dec_ret = SSL_TICKET_RETURN_ABORT; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, ((tst % 2) == 0) ? TLS1_2_VERSION : TLS1_3_VERSION, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF))) goto end; if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb, NULL))) goto end; if (tst >= 14) { if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb))) goto end; #ifndef OPENSSL_NO_DEPRECATED_3_0 } else if (tst >= 8) { if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb))) 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; if (!TEST_int_eq(gen_tick_called, 1) || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0)) goto end; gen_tick_called = dec_tick_called = 0; clntsess = SSL_get1_session(clientssl); SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); serverssl = clientssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(SSL_set_session(clientssl, clntsess)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW || tick_key_renew == -1) { if (!TEST_false(SSL_session_reused(clientssl))) goto end; } else { if (!TEST_true(SSL_session_reused(clientssl))) goto end; } if (!TEST_int_eq(gen_tick_called, (tick_key_renew || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW) ? 1 : 0) || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1)) goto end; testresult = 1; end: SSL_SESSION_free(clntsess); SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_incorrect_shutdown(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char buf[80]; BIO *c2s; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, 0, &sctx, &cctx, cert, privkey))) goto end; if (tst == 1) SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; c2s = SSL_get_rbio(serverssl); BIO_set_mem_eof_return(c2s, 0); if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf)))) goto end; if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) ) goto end; if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) ) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_shutdown(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char msg[] = "A test message"; char buf[80]; size_t written, readbytes; SSL_SESSION *sess; #ifdef OPENSSL_NO_TLS1_2 if (tst <= 1) return 1; #endif #ifdef OSSL_NO_USABLE_TLS1_3 if (tst >= 2) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, (tst <= 1) ? TLS1_2_VERSION : TLS1_3_VERSION, &sctx, &cctx, cert, privkey))) goto end; if (tst == 5) SSL_CTX_set_post_handshake_auth(cctx, 1); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (tst == 3) { if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE, 1, 0)) || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) || !TEST_false(SSL_SESSION_is_resumable(sess))) goto end; } else if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) || !TEST_true(SSL_SESSION_is_resumable(sess))) { goto end; } if (!TEST_int_eq(SSL_shutdown(clientssl), 0)) goto end; if (tst >= 4) { if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) || !TEST_int_eq(SSL_get_shutdown(serverssl), SSL_RECEIVED_SHUTDOWN) || !TEST_true(SSL_write(serverssl, msg, sizeof(msg)))) goto end; if (tst == 4 && !TEST_true(SSL_key_update(serverssl, SSL_KEY_UPDATE_REQUESTED))) goto end; if (tst == 5) { SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); if (!TEST_true(SSL_verify_client_post_handshake(serverssl))) goto end; } if ((tst == 4 || tst == 5) && !TEST_true(SSL_write(serverssl, msg, sizeof(msg)))) goto end; if (!TEST_int_eq(SSL_shutdown(serverssl), 1)) goto end; if (tst == 4 || tst == 5) { if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_size_t_eq(readbytes, sizeof(msg)) || !TEST_int_eq(memcmp(msg, buf, readbytes), 0) || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) || !TEST_size_t_eq(readbytes, sizeof(msg)) || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)) goto end; } } if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written))) goto end; if (tst < 4) { if (!TEST_int_eq(SSL_shutdown(serverssl), 0) || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) || !TEST_int_eq(SSL_shutdown(clientssl), 1) || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) || !TEST_true(SSL_SESSION_is_resumable(sess)) || !TEST_int_eq(SSL_shutdown(serverssl), 1)) goto end; } else if (tst == 4 || tst == 5) { if (!TEST_int_eq(SSL_shutdown(clientssl), 1) || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) || !TEST_true(SSL_SESSION_is_resumable(sess))) goto end; } else { if (!TEST_int_eq(SSL_shutdown(clientssl), -1) || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL)) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_async_shutdown(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; BIO *bretry = BIO_new(bio_s_always_retry()), *tmp = NULL; if (!TEST_ptr(bretry)) goto end; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, 0, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_int_eq(SSL_shutdown(clientssl), 0)) goto end; tmp = SSL_get_wbio(serverssl); if (!TEST_true(BIO_up_ref(tmp))) { tmp = NULL; goto end; } SSL_set0_wbio(serverssl, bretry); bretry = NULL; if (!TEST_int_eq(SSL_shutdown(serverssl), -1) || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE)) goto end; if (!TEST_int_eq(SSL_shutdown(serverssl), -1) || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE)) goto end; SSL_set0_wbio(serverssl, tmp); tmp = NULL; if (!TEST_int_eq(SSL_shutdown(serverssl), 0)) goto end; if (!TEST_int_eq(SSL_shutdown(serverssl), 1)) goto end; if (!TEST_int_eq(SSL_shutdown(clientssl), 1)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); BIO_free(bretry); BIO_free(tmp); return testresult; } #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) static int cert_cb_cnt; static int cert_cb(SSL *s, void *arg) { SSL_CTX *ctx = (SSL_CTX *)arg; BIO *in = NULL; EVP_PKEY *pkey = NULL; X509 *x509 = NULL, *rootx = NULL; STACK_OF(X509) *chain = NULL; char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL; int ret = 0; if (cert_cb_cnt == 0) { cert_cb_cnt++; return -1; } else if (cert_cb_cnt == 1) { if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx))) return 0; if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM)) || !TEST_true(SSL_use_PrivateKey_file(s, privkey, SSL_FILETYPE_PEM)) || !TEST_true(SSL_check_private_key(s))) return 0; cert_cb_cnt++; return 1; } else if (cert_cb_cnt == 3) { int rv; rootfile = test_mk_file_path(certsdir, "rootcert.pem"); ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem"); ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem"); if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey)) goto out; chain = sk_X509_new_null(); if (!TEST_ptr(chain)) goto out; if (!TEST_ptr(in = BIO_new(BIO_s_file())) || !TEST_int_gt(BIO_read_filename(in, rootfile), 0) || !TEST_ptr(rootx = X509_new_ex(libctx, NULL)) || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL)) || !TEST_true(sk_X509_push(chain, rootx))) goto out; rootx = NULL; BIO_free(in); if (!TEST_ptr(in = BIO_new(BIO_s_file())) || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0) || !TEST_ptr(x509 = X509_new_ex(libctx, NULL)) || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL))) goto out; BIO_free(in); if (!TEST_ptr(in = BIO_new(BIO_s_file())) || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0) || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL, NULL, NULL, libctx, NULL))) goto out; rv = SSL_check_chain(s, x509, pkey, chain); if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) { if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1)) goto out; } ret = 1; } out: OPENSSL_free(ecdsacert); OPENSSL_free(ecdsakey); OPENSSL_free(rootfile); BIO_free(in); EVP_PKEY_free(pkey); X509_free(x509); X509_free(rootx); OSSL_STACK_OF_X509_free(chain); return ret; } static int test_cert_cb_int(int prot, int tst) { SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, ret; #ifdef OPENSSL_NO_EC if (tst >= 3) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, prot, &sctx, &cctx, NULL, NULL))) goto end; if (tst == 0) cert_cb_cnt = -1; else if (tst >= 3) cert_cb_cnt = 3; else cert_cb_cnt = 0; if (tst == 2) { snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(snictx)) goto end; } SSL_CTX_set_cert_cb(sctx, cert_cb, snictx); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (tst == 4) { if (!TEST_true(SSL_set1_sigalgs_list(clientssl, "ecdsa_secp256r1_sha256"))) goto end; } else if (tst == 5) { if (!TEST_true(SSL_set1_sigalgs_list(clientssl, "rsa_pss_rsae_sha256:rsa_pkcs1_sha256"))) goto end; } ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret) || (tst > 0 && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) { goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); SSL_CTX_free(snictx); return testresult; } #endif static int test_cert_cb(int tst) { int testresult = 1; #ifndef OPENSSL_NO_TLS1_2 testresult &= test_cert_cb_int(TLS1_2_VERSION, tst); #endif #ifndef OSSL_NO_USABLE_TLS1_3 testresult &= test_cert_cb_int(TLS1_3_VERSION, tst); #endif return testresult; } static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) { X509 *xcert; EVP_PKEY *privpkey; BIO *in = NULL; BIO *priv_in = NULL; 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(libctx, 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, libctx, 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 test_client_cert_cb(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; #ifdef OPENSSL_NO_TLS1_2 if (tst == 0) return 1; #endif #ifdef OSSL_NO_USABLE_TLS1_3 if (tst == 1) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, tst == 0 ? TLS1_2_VERSION : TLS1_3_VERSION, &sctx, &cctx, cert, privkey))) goto end; 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_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; } #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) static int test_ca_names_int(int prot, int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; size_t i; X509_NAME *name[] = { NULL, NULL, NULL, NULL }; char *strnames[] = { "Jack", "Jill", "John", "Joanne" }; STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL; const STACK_OF(X509_NAME) *sktmp = NULL; for (i = 0; i < OSSL_NELEM(name); i++) { name[i] = X509_NAME_new(); if (!TEST_ptr(name[i]) || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN", MBSTRING_ASC, (unsigned char *) strnames[i], -1, -1, 0))) goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, prot, &sctx, &cctx, cert, privkey))) goto end; SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL); if (tst == 0 || tst == 1) { if (!TEST_ptr(sk1 = sk_X509_NAME_new_null()) || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0]))) || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1]))) || !TEST_ptr(sk2 = sk_X509_NAME_new_null()) || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0]))) || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1])))) goto end; SSL_CTX_set0_CA_list(sctx, sk1); SSL_CTX_set0_CA_list(cctx, sk2); sk1 = sk2 = NULL; } if (tst == 1 || tst == 2) { if (!TEST_ptr(sk1 = sk_X509_NAME_new_null()) || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2]))) || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3]))) || !TEST_ptr(sk2 = sk_X509_NAME_new_null()) || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2]))) || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3])))) goto end; SSL_CTX_set_client_CA_list(sctx, sk1); SSL_CTX_set_client_CA_list(cctx, sk2); sk1 = sk2 = NULL; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; sktmp = SSL_get0_peer_CA_list(serverssl); if (prot == TLS1_3_VERSION && (tst == 0 || tst == 1)) { if (!TEST_ptr(sktmp) || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2) || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0), name[0]), 0) || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1), name[1]), 0)) goto end; } else if (!TEST_ptr_null(sktmp)) { goto end; } sktmp = SSL_get0_peer_CA_list(clientssl); if (!TEST_ptr(sktmp) || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2) || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0), name[tst == 0 ? 0 : 2]), 0) || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1), name[tst == 0 ? 1 : 3]), 0)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); for (i = 0; i < OSSL_NELEM(name); i++) X509_NAME_free(name[i]); sk_X509_NAME_pop_free(sk1, X509_NAME_free); sk_X509_NAME_pop_free(sk2, X509_NAME_free); return testresult; } #endif static int test_ca_names(int tst) { int testresult = 1; #ifndef OPENSSL_NO_TLS1_2 testresult &= test_ca_names_int(TLS1_2_VERSION, tst); #endif #ifndef OSSL_NO_USABLE_TLS1_3 testresult &= test_ca_names_int(TLS1_3_VERSION, tst); #endif return testresult; } #ifndef OPENSSL_NO_TLS1_2 static const char *multiblock_cipherlist_data[]= { "AES128-SHA", "AES128-SHA256", "AES256-SHA", "AES256-SHA256", }; # define MULTIBLOCK_FRAGSIZE 512 static int test_multiblock_write(int test_index) { static const char *fetchable_ciphers[]= { "AES-128-CBC-HMAC-SHA1", "AES-128-CBC-HMAC-SHA256", "AES-256-CBC-HMAC-SHA1", "AES-256-CBC-HMAC-SHA256" }; const char *cipherlist = multiblock_cipherlist_data[test_index]; const SSL_METHOD *smeth = TLS_server_method(); const SSL_METHOD *cmeth = TLS_client_method(); int min_version = TLS1_VERSION; int max_version = TLS1_2_VERSION; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; unsigned char msg[MULTIBLOCK_FRAGSIZE * 9]; unsigned char buf[sizeof(msg)], *p = buf; size_t readbytes, written, len; EVP_CIPHER *ciph = NULL; ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], ""); if (ciph == NULL) { TEST_skip("Multiblock cipher is not available for %s", cipherlist); return 1; } EVP_CIPHER_free(ciph); RAND_bytes(msg, sizeof(msg)); if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version, max_version, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC); if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) || !TEST_size_t_eq(written, sizeof(msg))) goto end; len = written; while (len > 0) { if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes))) goto end; p += readbytes; len -= readbytes; } if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int test_session_timeout(int test) { SSL_SESSION *early = NULL; SSL_SESSION *middle = NULL; SSL_SESSION *late = NULL; SSL_CTX *ctx; int testresult = 0; long now = (long)time(NULL); #define TIMEOUT 10 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method())) || !TEST_ptr(early = SSL_SESSION_new()) || !TEST_ptr(middle = SSL_SESSION_new()) || !TEST_ptr(late = SSL_SESSION_new())) goto end; early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH); middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH); late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH); if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1) || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1) || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1)) goto end; if (!TEST_ptr(early->prev) || !TEST_ptr(middle->prev) || !TEST_ptr(late->prev)) goto end; if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0) || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0) || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0)) goto end; if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0) || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0) || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0)) goto end; if (!TEST_ptr(early->prev) || !TEST_ptr(middle->prev) || !TEST_ptr(late->prev)) goto end; if (!TEST_ptr_eq(late->next, middle) || !TEST_ptr_eq(middle->next, early) || !TEST_ptr_eq(early->prev, middle) || !TEST_ptr_eq(middle->prev, late)) goto end; SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1); if (!TEST_ptr_null(early->prev) || !TEST_ptr(middle->prev) || !TEST_ptr(late->prev)) goto end; SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1); if (!TEST_ptr_null(early->prev) || !TEST_ptr_null(middle->prev) || !TEST_ptr(late->prev)) goto end; SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11); if (!TEST_ptr_null(early->prev) || !TEST_ptr_null(middle->prev) || !TEST_ptr_null(late->prev)) goto end; if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1) || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1) || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1)) goto end; if (!TEST_ptr(early->prev) || !TEST_ptr(middle->prev) || !TEST_ptr(late->prev)) goto end; SSL_CTX_flush_sessions(ctx, 0); if (!TEST_ptr_null(early->prev) || !TEST_ptr_null(middle->prev) || !TEST_ptr_null(late->prev)) goto end; (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME | SSL_CTX_get_session_cache_mode(ctx)); now -= 10; if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0) || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1) || !TEST_long_ne(SSL_SESSION_get_time(early), now)) goto end; testresult = 1; end: SSL_CTX_free(ctx); SSL_SESSION_free(early); SSL_SESSION_free(middle); SSL_SESSION_free(late); return testresult; } static int test_servername(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_SESSION *sess = NULL; const char *sexpectedhost = NULL, *cexpectedhost = NULL; #ifdef OPENSSL_NO_TLS1_2 if (tst <= 4) return 1; #endif #ifdef OSSL_NO_USABLE_TLS1_3 if (tst >= 5) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, (tst <= 4) ? TLS1_2_VERSION : TLS1_3_VERSION, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (tst != 1 && tst != 6) { if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, hostname_cb))) goto end; } if (tst != 3 && tst != 8) { if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))) goto end; sexpectedhost = cexpectedhost = "goodhost"; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), cexpectedhost) || !TEST_str_eq(SSL_get_servername(serverssl, TLSEXT_NAMETYPE_host_name), sexpectedhost)) goto end; if (!TEST_int_eq(SSL_shutdown(clientssl), 0) || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL) || !TEST_true(SSL_SESSION_is_resumable(sess)) || !TEST_int_eq(SSL_shutdown(serverssl), 0)) goto end; SSL_free(clientssl); SSL_free(serverssl); clientssl = serverssl = NULL; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set_session(clientssl, sess))) goto end; sexpectedhost = cexpectedhost = "goodhost"; if (tst == 2 || tst == 7) { if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost"))) goto end; if (tst == 7) sexpectedhost = cexpectedhost = "altgoodhost"; if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), "altgoodhost")) goto end; } else if (tst == 4 || tst == 9) { if (tst == 9) sexpectedhost = cexpectedhost = NULL; if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), cexpectedhost)) goto end; } else { if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))) goto end; if (tst == 1 || tst == 3) sexpectedhost = NULL; if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), "goodhost")) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_session_reused(clientssl)) || !TEST_true(SSL_session_reused(serverssl)) || !TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), cexpectedhost) || !TEST_str_eq(SSL_get_servername(serverssl, TLSEXT_NAMETYPE_host_name), sexpectedhost)) 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; } static int test_unknown_sigalgs_groups(void) { int ret = 0; SSL_CTX *ctx = NULL; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()))) goto end; if (!TEST_int_gt(SSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256:?nonexistent:?RSA+SHA512"), 0)) goto end; if (!TEST_size_t_eq(ctx->cert->conf_sigalgslen, 2) || !TEST_int_eq(ctx->cert->conf_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256) || !TEST_int_eq(ctx->cert->conf_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512)) goto end; if (!TEST_int_gt(SSL_CTX_set1_client_sigalgs_list(ctx, "RSA+SHA256:?nonexistent:?RSA+SHA512"), 0)) goto end; if (!TEST_size_t_eq(ctx->cert->client_sigalgslen, 2) || !TEST_int_eq(ctx->cert->client_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256) || !TEST_int_eq(ctx->cert->client_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512)) goto end; if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx, "nonexistent"), 0)) goto end; if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx, "?nonexistent1:?nonexistent2:?nonexistent3"), 0)) goto end; #ifndef OPENSSL_NO_EC if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx, "P-256:nonexistent"), 0)) goto end; if (!TEST_int_gt(SSL_CTX_set1_groups_list(ctx, "P-384:?nonexistent:?P-521"), 0)) goto end; if (!TEST_size_t_eq(ctx->ext.supportedgroups_len, 2) || !TEST_int_eq(ctx->ext.supportedgroups[0], OSSL_TLS_GROUP_ID_secp384r1) || !TEST_int_eq(ctx->ext.supportedgroups[1], OSSL_TLS_GROUP_ID_secp521r1)) goto end; #endif ret = 1; end: SSL_CTX_free(ctx); return ret; } #if !defined(OPENSSL_NO_EC) \ && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)) static int test_sigalgs_available(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new(); OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx; OSSL_PROVIDER *filterprov = NULL; int sig, hash; if (!TEST_ptr(tmpctx)) goto end; if (idx != 0 && idx != 3) { if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter", filter_provider_init))) goto end; filterprov = OSSL_PROVIDER_load(tmpctx, "filter"); if (!TEST_ptr(filterprov)) goto end; if (idx < 3) { if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST, "SHA2-256:SHA1"))) goto end; } else { if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE, "ECDSA")) # ifdef OPENSSL_NO_ECX || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT, "EC")) # else || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT, "EC:X25519:X448")) # endif ) goto end; } if (idx == 1 || idx == 4) clientctx = tmpctx; else serverctx = tmpctx; } cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method()); sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method()); if (!TEST_ptr(cctx) || !TEST_ptr(sctx)) goto end; if (idx != 5) { if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; } else { if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert2, privkey2))) goto end; } if (idx < 4) { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "ECDHE-RSA-AES128-GCM-SHA256"))) goto end; } else { if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "ECDHE-ECDSA-AES128-GCM-SHA256"))) goto end; } if (idx < 3) { if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha384" ":rsa_pss_rsae_sha256") || !SSL_CTX_set1_sigalgs_list(sctx, "rsa_pss_rsae_sha384" ":rsa_pss_rsae_sha256")) goto end; } else { if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256") || !SSL_CTX_set1_sigalgs_list(sctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")) goto end; } if (idx != 5 && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, privkey2, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL, NULL, NULL), (idx == 0 || idx == 3) ? 2 : 1)) goto end; if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256)) goto end; if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC : NID_rsassaPss)) goto end; testresult = filter_provider_check_clean_finish(); end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); OSSL_PROVIDER_unload(filterprov); OSSL_LIB_CTX_free(tmpctx); return testresult; } #endif #ifndef OPENSSL_NO_TLS1_3 static int test_pluggable_group(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider"); OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy"); const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup"; if (!TEST_ptr(tlsprov)) goto end; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, TLS1_3_VERSION, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup:xorkemgroup:dummy1:dummy2:dummy3:dummy4:dummy5:dummy6:dummy7:dummy8:dummy9:dummy10:dummy11:dummy12:dummy13:dummy14:dummy15:dummy16:dummy17:dummy18:dummy19:dummy20:dummy21:dummy22:dummy23:dummy24:dummy25:dummy26:dummy27:dummy28:dummy29:dummy30:dummy31:dummy32:dummy33:dummy34:dummy35:dummy36:dummy37:dummy38:dummy39:dummy40:dummy41:dummy42:dummy43")) || !TEST_true(SSL_set1_groups_list(clientssl, group_name))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_str_eq(group_name, SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0)))) goto end; if (!TEST_str_eq(group_name, SSL_get0_group_name(serverssl)) || !TEST_str_eq(group_name, SSL_get0_group_name(clientssl))) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); OSSL_PROVIDER_unload(tlsprov); OSSL_PROVIDER_unload(legacyprov); return testresult; } static int create_cert_key(int idx, char *certfilename, char *privkeyfilename) { EVP_PKEY_CTX *evpctx = EVP_PKEY_CTX_new_from_name(libctx, (idx == 0) ? "xorhmacsig" : "xorhmacsha2sig", NULL); EVP_PKEY *pkey = NULL; X509 *x509 = X509_new(); X509_NAME *name = NULL; BIO *keybio = NULL, *certbio = NULL; int ret = 1; if (!TEST_ptr(evpctx) || !TEST_true(EVP_PKEY_keygen_init(evpctx)) || !TEST_true(EVP_PKEY_generate(evpctx, &pkey)) || !TEST_ptr(pkey) || !TEST_ptr(x509) || !TEST_true(ASN1_INTEGER_set(X509_get_serialNumber(x509), 1)) || !TEST_true(X509_gmtime_adj(X509_getm_notBefore(x509), 0)) || !TEST_true(X509_gmtime_adj(X509_getm_notAfter(x509), 31536000L)) || !TEST_true(X509_set_pubkey(x509, pkey)) || !TEST_ptr(name = X509_get_subject_name(x509)) || !TEST_true(X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"CH", -1, -1, 0)) || !TEST_true(X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *)"test.org", -1, -1, 0)) || !TEST_true(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"localhost", -1, -1, 0)) || !TEST_true(X509_set_issuer_name(x509, name)) || !TEST_true(X509_sign(x509, pkey, EVP_sha1())) || !TEST_ptr(keybio = BIO_new_file(privkeyfilename, "wb")) || !TEST_true(PEM_write_bio_PrivateKey(keybio, pkey, NULL, NULL, 0, NULL, NULL)) || !TEST_ptr(certbio = BIO_new_file(certfilename, "wb")) || !TEST_true(PEM_write_bio_X509(certbio, x509))) ret = 0; EVP_PKEY_free(pkey); X509_free(x509); EVP_PKEY_CTX_free(evpctx); BIO_free(keybio); BIO_free(certbio); return ret; } static int test_pluggable_signature(int idx) { static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 }; SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider"); OSSL_PROVIDER *defaultprov = OSSL_PROVIDER_load(libctx, "default"); char *certfilename = "tls-prov-cert.pem"; char *privkeyfilename = "tls-prov-key.pem"; int sigidx = idx % 2; int rpkidx = idx / 2; if (!TEST_ptr(tlsprov) || !TEST_true(create_cert_key(sigidx, certfilename, privkeyfilename))) goto end; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, TLS1_3_VERSION, &sctx, &cctx, certfilename, privkeyfilename)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (rpkidx) { if (!TEST_true(SSL_set1_server_cert_type(serverssl, cert_type_rpk, sizeof(cert_type_rpk))) || !TEST_true(SSL_set1_server_cert_type(clientssl, cert_type_rpk, sizeof(cert_type_rpk)))) goto end; } if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup")) || !TEST_true(SSL_set1_groups_list(clientssl, "xorgroup"))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (rpkidx && !TEST_long_eq(SSL_get_verify_result(clientssl), X509_V_ERR_RPK_UNTRUSTED)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); OSSL_PROVIDER_unload(tlsprov); OSSL_PROVIDER_unload(defaultprov); return testresult; } #endif #ifndef OPENSSL_NO_TLS1_2 static int test_ssl_dup(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL; int testresult = 0; BIO *rbio = NULL, *wbio = NULL; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, 0, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION)) || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION))) goto end; client2ssl = SSL_dup(clientssl); rbio = SSL_get_rbio(clientssl); if (!TEST_ptr(rbio) || !TEST_true(BIO_up_ref(rbio))) goto end; SSL_set0_rbio(client2ssl, rbio); rbio = NULL; wbio = SSL_get_wbio(clientssl); if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio))) goto end; SSL_set0_wbio(client2ssl, wbio); rbio = NULL; if (!TEST_ptr(client2ssl) || !TEST_ptr_ne(clientssl, client2ssl)) goto end; if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION) || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION)) goto end; if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE))) goto end; SSL_free(clientssl); clientssl = SSL_dup(client2ssl); if (!TEST_ptr(clientssl) || !TEST_ptr_eq(clientssl, client2ssl)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_free(client2ssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } # ifndef OPENSSL_NO_DH static EVP_PKEY *tmp_dh_params = NULL; static EVP_PKEY *get_tmp_dh_params(void) { if (tmp_dh_params == NULL) { BIGNUM *p = NULL; OSSL_PARAM_BLD *tmpl = NULL; EVP_PKEY_CTX *pctx = NULL; OSSL_PARAM *params = NULL; EVP_PKEY *dhpkey = NULL; p = BN_get_rfc3526_prime_2048(NULL); if (!TEST_ptr(p)) goto end; pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL); if (!TEST_ptr(pctx) || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1)) goto end; tmpl = OSSL_PARAM_BLD_new(); if (!TEST_ptr(tmpl) || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)) || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2))) goto end; params = OSSL_PARAM_BLD_to_param(tmpl); if (!TEST_ptr(params) || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey, EVP_PKEY_KEY_PARAMETERS, params), 1)) goto end; tmp_dh_params = dhpkey; end: BN_free(p); EVP_PKEY_CTX_free(pctx); OSSL_PARAM_BLD_free(tmpl); OSSL_PARAM_free(params); } if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params)) return NULL; return tmp_dh_params; } # ifndef OPENSSL_NO_DEPRECATED_3_0 static DH *tmp_dh_callback(SSL *s, int is_export, int keylen) { EVP_PKEY *dhpkey = get_tmp_dh_params(); DH *ret = NULL; if (!TEST_ptr(dhpkey)) return NULL; ret = EVP_PKEY_get1_DH(dhpkey); DH_free(ret); EVP_PKEY_free(dhpkey); return ret; } # endif static int test_set_tmp_dh(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; int dhauto = (idx == 3 || idx == 4) ? 1 : 0; int expected = (idx <= 2) ? 0 : 1; EVP_PKEY *dhpkey = NULL; # ifndef OPENSSL_NO_DEPRECATED_3_0 DH *dh = NULL; # else if (idx >= 7) return 1; # endif if (idx >= 5 && idx <= 8) { dhpkey = get_tmp_dh_params(); if (!TEST_ptr(dhpkey)) goto end; } # ifndef OPENSSL_NO_DEPRECATED_3_0 if (idx == 7 || idx == 8) { dh = EVP_PKEY_get1_DH(dhpkey); if (!TEST_ptr(dh)) goto end; } # endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, 0, &sctx, &cctx, cert, privkey))) goto end; if ((idx & 1) == 1) { if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto))) goto end; } if (idx == 5) { if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey))) goto end; dhpkey = NULL; } # ifndef OPENSSL_NO_DEPRECATED_3_0 else if (idx == 7) { if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh))) goto end; } else if (idx == 9) { SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback); } # endif if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if ((idx & 1) == 0 && idx != 0) { if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto))) goto end; } if (idx == 6) { if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey))) goto end; dhpkey = NULL; } # ifndef OPENSSL_NO_DEPRECATED_3_0 else if (idx == 8) { if (!TEST_true(SSL_set_tmp_dh(serverssl, dh))) goto end; } else if (idx == 10) { SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback); } # endif if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION)) || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION)) || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA"))) goto end; if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE), expected)) goto end; testresult = 1; end: # ifndef OPENSSL_NO_DEPRECATED_3_0 DH_free(dh); # endif SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); EVP_PKEY_free(dhpkey); return testresult; } static int test_dh_auto(int idx) { SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()); SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; EVP_PKEY *tmpkey = NULL; char *thiscert = NULL, *thiskey = NULL; size_t expdhsize = 0; const char *ciphersuite = "DHE-RSA-AES128-SHA"; if (!TEST_ptr(sctx) || !TEST_ptr(cctx)) goto end; switch (idx) { case 0: if (is_fips) { testresult = 1; goto end; } thiscert = cert1024; thiskey = privkey1024; expdhsize = 1024; SSL_CTX_set_security_level(sctx, 1); SSL_CTX_set_security_level(cctx, 1); break; case 1: thiscert = cert; thiskey = privkey; expdhsize = 2048; break; case 2: thiscert = cert3072; thiskey = privkey3072; expdhsize = 3072; break; case 3: thiscert = cert4096; thiskey = privkey4096; expdhsize = 4096; break; case 4: thiscert = cert8192; thiskey = privkey8192; expdhsize = 8192; break; case 5: if (is_fips) { testresult = 1; goto end; } ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0"; expdhsize = 1024; break; case 6: ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0"; expdhsize = 3072; break; default: TEST_error("Invalid text index"); goto end; } if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, NULL, 0, 0, &sctx, &cctx, thiscert, thiskey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set_dh_auto(serverssl, 1)) || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION)) || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION)) || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite)) || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite))) goto end; if (!TEST_int_le(SSL_connect(clientssl), 0) || !TEST_int_le(SSL_accept(serverssl), 0)) goto end; if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0)) goto end; if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize)) goto end; 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); EVP_PKEY_free(tmpkey); return testresult; } # endif #endif #ifndef OSSL_NO_USABLE_TLS1_3 static int test_sni_tls13(void) { SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; snicb = 0; sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(sctx)) goto end; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx2, &cctx, cert, privkey))) goto end; if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb)) || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_int_eq(snicb, 1)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx2); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_ticket_lifetime(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; int version = TLS1_3_VERSION; #define ONE_WEEK_SEC (7 * 24 * 60 * 60) #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC) if (idx == 0) { #ifdef OPENSSL_NO_TLS1_2 return TEST_skip("TLS 1.2 is disabled."); #else version = TLS1_2_VERSION; #endif } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), version, version, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC), SSL_get_default_timeout(serverssl))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (idx == 0) { if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC)) goto end; } else { if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC)) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif static int test_set_alpn(void) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; int testresult = 0; unsigned char bad0[] = { 0x00, 'b', 'a', 'd' }; unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' }; unsigned char bad1[] = { 0x01, 'b', 'a', 'd' }; unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00}; unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'}; unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'}; ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(ctx)) goto end; if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2))) goto end; if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0))) goto end; if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good)))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0)))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1)))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2)))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3)))) goto end; if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4)))) goto end; ssl = SSL_new(ctx); if (!TEST_ptr(ssl)) goto end; if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2))) goto end; if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0))) goto end; if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good)))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0)))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1)))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2)))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3)))) goto end; if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4)))) goto end; testresult = 1; end: SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_set_verify_cert_store_ssl_ctx(void) { SSL_CTX *ctx = NULL; int testresult = 0; X509_STORE *store = NULL, *new_store = NULL, *cstore = NULL, *new_cstore = NULL; ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(ctx)) goto end; if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store))) goto end; if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore))) goto end; if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore)) goto end; new_store = X509_STORE_new(); if (!TEST_ptr(new_store)) goto end; new_cstore = X509_STORE_new(); if (!TEST_ptr(new_cstore)) goto end; if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store))) goto end; if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore))) goto end; if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store))) goto end; if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore))) goto end; if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore)) goto end; if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL))) goto end; if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL))) goto end; if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store))) goto end; if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore))) goto end; if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore)) goto end; testresult = 1; end: X509_STORE_free(new_store); X509_STORE_free(new_cstore); SSL_CTX_free(ctx); return testresult; } static int test_set_verify_cert_store_ssl(void) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; int testresult = 0; X509_STORE *store = NULL, *new_store = NULL, *cstore = NULL, *new_cstore = NULL; ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(ctx)) goto end; ssl = SSL_new(ctx); if (!TEST_ptr(ssl)) goto end; if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store))) goto end; if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore))) goto end; if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore)) goto end; new_store = X509_STORE_new(); if (!TEST_ptr(new_store)) goto end; new_cstore = X509_STORE_new(); if (!TEST_ptr(new_cstore)) goto end; if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store))) goto end; if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore))) goto end; if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store))) goto end; if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore))) goto end; if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore)) goto end; if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL))) goto end; if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL))) goto end; if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store))) goto end; if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore))) goto end; if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore)) goto end; testresult = 1; end: X509_STORE_free(new_store); X509_STORE_free(new_cstore); SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_inherit_verify_param(void) { int testresult = 0; SSL_CTX *ctx = NULL; X509_VERIFY_PARAM *cp = NULL; SSL *ssl = NULL; X509_VERIFY_PARAM *sp = NULL; int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT; ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_ptr(ctx)) goto end; cp = SSL_CTX_get0_param(ctx); if (!TEST_ptr(cp)) goto end; if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0)) goto end; X509_VERIFY_PARAM_set_hostflags(cp, hostflags); ssl = SSL_new(ctx); if (!TEST_ptr(ssl)) goto end; sp = SSL_get0_param(ssl); if (!TEST_ptr(sp)) goto end; if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags)) goto end; testresult = 1; end: SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } static int test_load_dhfile(void) { #ifndef OPENSSL_NO_DH int testresult = 0; SSL_CTX *ctx = NULL; SSL_CONF_CTX *cctx = NULL; if (dhfile == NULL) return 1; if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method())) || !TEST_ptr(cctx = SSL_CONF_CTX_new())) goto end; SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_FILE); if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2)) goto end; testresult = 1; end: SSL_CONF_CTX_free(cctx); SSL_CTX_free(ctx); return testresult; #else return TEST_skip("DH not supported by this build"); #endif } #ifndef OSSL_NO_USABLE_TLS1_3 static int test_read_ahead_key_change(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char *msg = "Hello World"; size_t written, readbytes; char buf[80]; int i; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; SSL_CTX_set_read_ahead(sctx, 1); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written)) || !TEST_size_t_eq(written, strlen(msg))) goto end; if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED))) goto end; if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written)) || !TEST_size_t_eq(written, strlen(msg))) goto end; for (i = 0; i < 2; i++) { if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1, &readbytes))) goto end; buf[readbytes] = '\0'; if (!TEST_str_eq(buf, msg)) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static size_t record_pad_cb(SSL *s, int type, size_t len, void *arg) { int *called = arg; switch ((*called)++) { case 0: return 512; case 1: return SSL3_RT_MAX_PLAIN_LENGTH - len; case 2: return SSL3_RT_MAX_PLAIN_LENGTH + 1 - len; case 3: return SIZE_MAX; default: return 0; } } static int test_tls13_record_padding(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; char *msg = "Hello World"; size_t written, readbytes; char buf[80]; int i; int called = 0; if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), TLS1_3_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (idx == 0) { SSL_CTX_set_record_padding_callback(cctx, record_pad_cb); SSL_CTX_set_record_padding_callback_arg(cctx, &called); if (!TEST_ptr_eq(SSL_CTX_get_record_padding_callback_arg(cctx), &called)) goto end; } else if (idx == 2) { if (!TEST_false(SSL_CTX_set_block_padding(cctx, SSL3_RT_MAX_PLAIN_LENGTH + 1))) goto end; if (!TEST_true(SSL_CTX_set_block_padding(cctx, 512))) goto end; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (idx == 1) { SSL_set_record_padding_callback(clientssl, record_pad_cb); SSL_set_record_padding_callback_arg(clientssl, &called); if (!TEST_ptr_eq(SSL_get_record_padding_callback_arg(clientssl), &called)) goto end; } else if (idx == 3) { if (!TEST_false(SSL_set_block_padding(clientssl, SSL3_RT_MAX_PLAIN_LENGTH + 1))) goto end; if (!TEST_true(SSL_set_block_padding(clientssl, 512))) goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; called = 0; for (i = 0; i < 4; i++) { if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written)) || !TEST_size_t_eq(written, strlen(msg))) goto end; if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1, &readbytes)) || !TEST_size_t_eq(written, readbytes)) goto end; buf[readbytes] = '\0'; if (!TEST_str_eq(buf, msg)) goto end; } if ((idx == 0 || idx == 1) && !TEST_int_eq(called, 4)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } #endif #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) static ENGINE *load_dasync(void) { ENGINE *e; if (!TEST_ptr(e = ENGINE_by_id("dasync"))) return NULL; if (!TEST_true(ENGINE_init(e))) { ENGINE_free(e); return NULL; } if (!TEST_true(ENGINE_register_ciphers(e))) { ENGINE_free(e); return NULL; } return e; } static int test_pipelining(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb; int testresult = 0, numreads; unsigned char *msg = (unsigned char *) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123"; size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5; size_t expectedreads; unsigned char *buf = NULL; ENGINE *e = NULL; if (idx != 6) { e = load_dasync(); if (e == NULL) return 0; } if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, TLS1_2_VERSION, &sctx, &cctx, cert, privkey))) goto end; if (idx == 6) { e = load_dasync(); if (e == NULL) goto end; idx = 0; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA"))) goto end; if (idx == 1) { peera = serverssl; peerb = clientssl; } else { peera = clientssl; peerb = serverssl; } if (idx == 5) { numpipes = 2; fragsize = SSL3_RT_MAX_PLAIN_LENGTH; msglen = fragsize * numpipes; msg = OPENSSL_malloc(msglen); if (!TEST_ptr(msg)) goto end; if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0)) goto end; } else if (idx == 4) { msglen = 55; } else { msglen = 50; } if (idx == 2) msglen -= 2; else if (idx == 3) msglen -= 12; buf = OPENSSL_malloc(msglen); if (!TEST_ptr(buf)) goto end; if (idx == 5) { if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1))) goto end; } if (!TEST_true(SSL_set_max_pipelines(peera, numpipes)) || !TEST_true(SSL_set_split_send_fragment(peera, fragsize))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written)) || !TEST_size_t_eq(written, msglen)) goto end; for (offset = 0, numreads = 0; offset < msglen; offset += readbytes, numreads++) { if (!TEST_true(SSL_read_ex(peerb, buf + offset, msglen - offset, &readbytes))) goto end; } expectedreads = idx == 4 ? numpipes + 1 : (idx == 3 ? numpipes - 1 : numpipes); if (!TEST_mem_eq(msg, msglen, buf, offset) || !TEST_int_eq(numreads, expectedreads)) goto end; for (offset = 0; offset < msglen; offset += fragsize) { size_t sendlen = msglen - offset; if (sendlen > fragsize) sendlen = fragsize; if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written)) || !TEST_size_t_eq(written, sendlen)) goto end; } if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes)) || !TEST_size_t_le(readbytes, msglen)) goto end; if (idx == 4) { size_t readbytes2; if (!TEST_true(SSL_read_ex(peera, buf + readbytes, msglen - readbytes, &readbytes2))) goto end; readbytes += readbytes2; if (!TEST_size_t_le(readbytes, msglen)) goto end; } if (!TEST_mem_eq(msg, msglen, buf, readbytes)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); if (e != NULL) { ENGINE_unregister_ciphers(e); ENGINE_finish(e); ENGINE_free(e); } OPENSSL_free(buf); if (fragsize == SSL3_RT_MAX_PLAIN_LENGTH) OPENSSL_free(msg); return testresult; } #endif static int check_version_string(SSL *s, int version) { const char *verstr = NULL; switch (version) { case SSL3_VERSION: verstr = "SSLv3"; break; case TLS1_VERSION: verstr = "TLSv1"; break; case TLS1_1_VERSION: verstr = "TLSv1.1"; break; case TLS1_2_VERSION: verstr = "TLSv1.2"; break; case TLS1_3_VERSION: verstr = "TLSv1.3"; break; case DTLS1_VERSION: verstr = "DTLSv1"; break; case DTLS1_2_VERSION: verstr = "DTLSv1.2"; } return TEST_str_eq(verstr, SSL_get_version(s)); } static int test_version(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, version; const SSL_METHOD *servmeth = TLS_server_method(); const SSL_METHOD *clientmeth = TLS_client_method(); switch (idx) { #if !defined(OPENSSL_NO_SSL3) case 0: version = SSL3_VERSION; break; #endif #if !defined(OPENSSL_NO_TLS1) case 1: version = TLS1_VERSION; break; #endif #if !defined(OPENSSL_NO_TLS1_2) case 2: version = TLS1_2_VERSION; break; #endif #if !defined(OSSL_NO_USABLE_TLS1_3) case 3: version = TLS1_3_VERSION; break; #endif #if !defined(OPENSSL_NO_DTLS1) case 4: version = DTLS1_VERSION; break; #endif #if !defined(OPENSSL_NO_DTLS1_2) case 5: version = DTLS1_2_VERSION; break; #endif default: TEST_skip("Unsupported protocol version"); return 1; } if (is_fips && (version == SSL3_VERSION || version == TLS1_VERSION || version == DTLS1_VERSION)) { TEST_skip("Protocol version not supported with FIPS"); return 1; } #if !defined(OPENSSL_NO_DTLS) if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) { servmeth = DTLS_server_method(); clientmeth = DTLS_client_method(); } #endif if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, version, version, &sctx, &cctx, cert, privkey))) goto end; 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; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_int_eq(SSL_version(serverssl), version) || !TEST_int_eq(SSL_version(clientssl), version) || !TEST_true(check_version_string(serverssl, version)) || !TEST_true(check_version_string(clientssl, version))) goto end; if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) { if (!TEST_true(SSL_is_dtls(serverssl)) || !TEST_true(SSL_is_dtls(clientssl)) || !TEST_false(SSL_is_tls(serverssl)) || !TEST_false(SSL_is_tls(clientssl)) || !TEST_false(SSL_is_quic(serverssl)) || !TEST_false(SSL_is_quic(clientssl))) goto end; } else { if (!TEST_true(SSL_is_tls(serverssl)) || !TEST_true(SSL_is_tls(clientssl)) || !TEST_false(SSL_is_dtls(serverssl)) || !TEST_false(SSL_is_dtls(clientssl)) || !TEST_false(SSL_is_quic(serverssl)) || !TEST_false(SSL_is_quic(clientssl))) goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_rstate_string(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0, version; const SSL_METHOD *servmeth = TLS_server_method(); const SSL_METHOD *clientmeth = TLS_client_method(); size_t written, readbytes; unsigned char buf[2]; unsigned char dummyheader[SSL3_RT_HEADER_LENGTH] = { SSL3_RT_APPLICATION_DATA, TLS1_2_VERSION_MAJOR, 0, 0, 1 }; if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, 0, 0, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH") || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header")) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH") || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header")) goto end; version = SSL_version(serverssl); if (version == TLS1_3_VERSION) version = TLS1_2_VERSION; dummyheader[2] = version & 0xff; if (!TEST_true(BIO_write_ex(SSL_get_rbio(serverssl), dummyheader, sizeof(dummyheader), &written)) || !TEST_size_t_eq(written, SSL3_RT_HEADER_LENGTH)) goto end; if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))) goto end; if (!TEST_str_eq(SSL_rstate_string(serverssl), "RB") || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read body")) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } static int test_handshake_retry(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; BIO *tmp = NULL, *bretry = BIO_new(bio_s_always_retry()); int maxversion = 0; if (!TEST_ptr(bretry)) goto end; #ifndef OPENSSL_NO_TLS1_2 if ((idx & 8) == 8) maxversion = TLS1_2_VERSION; #else if ((idx & 8) == 8) return TEST_skip("No TLSv1.2"); #endif if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, maxversion, &sctx, &cctx, cert, privkey))) goto end; if ((idx & 1) == 1 && !ssl_ctx_add_large_cert_chain(libctx, sctx, cert)) goto end; if ((idx & 2) == 2) SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL); if ((idx & 4) == 4) set_always_retry_err_val(0); if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; tmp = SSL_get_wbio(serverssl); if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { tmp = NULL; goto end; } SSL_set0_wbio(serverssl, bretry); bretry = NULL; if (!TEST_int_eq(SSL_connect(clientssl), -1)) goto end; if (!TEST_int_eq(SSL_accept(serverssl), -1) || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE)) goto end; SSL_set0_wbio(serverssl, tmp); tmp = 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); BIO_free(bretry); BIO_free(tmp); set_always_retry_err_val(-1); return testresult; } static int test_data_retry(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; unsigned char inbuf[1200], outbuf[1200]; size_t i; BIO *tmp = NULL; BIO *bretry = BIO_new(bio_s_maybe_retry()); size_t written, readbytes, totread = 0; if (!TEST_ptr(bretry)) goto end; for (i = 0; i < sizeof(inbuf); i++) inbuf[i] = (unsigned char)(0xff & i); memset(outbuf, 0, sizeof(outbuf)); if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), 0, 0, &sctx, &cctx, cert, privkey))) goto end; if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_true(SSL_set_max_send_fragment(clientssl, 512))) goto end; tmp = SSL_get_wbio(clientssl); if (!TEST_ptr(tmp)) goto end; if (!TEST_true(BIO_up_ref(tmp))) goto end; BIO_push(bretry, tmp); tmp = NULL; SSL_set0_wbio(clientssl, bretry); if (!BIO_up_ref(bretry)) { bretry = NULL; goto end; } for (i = 0; i < 3; i++) { if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written))) goto end; if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE)) goto end; if (!TEST_true(BIO_ctrl(bretry, MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT, 1, NULL))) goto end; if (i == 2) break; if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written))) goto end; if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE)) goto end; } if (!TEST_true(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written))) goto end; while (SSL_read_ex(serverssl, outbuf + totread, sizeof(outbuf) - totread, &readbytes)) totread += readbytes; if (!TEST_mem_eq(inbuf, sizeof(inbuf), outbuf, totread)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); BIO_free_all(bretry); BIO_free(tmp); return testresult; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n") int setup_tests(void) { char *modulename; char *configfile; libctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(libctx)) return 0; defctxnull = OSSL_PROVIDER_load(NULL, "null"); if (!TEST_false(OSSL_PROVIDER_available(NULL, "default")) || !TEST_false(OSSL_PROVIDER_available(NULL, "fips"))) return 0; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certsdir = test_get_argument(0)) || !TEST_ptr(srpvfile = test_get_argument(1)) || !TEST_ptr(tmpfilename = test_get_argument(2)) || !TEST_ptr(modulename = test_get_argument(3)) || !TEST_ptr(configfile = test_get_argument(4)) || !TEST_ptr(dhfile = test_get_argument(5))) return 0; if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile))) return 0; if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename))) return 0; if (strcmp(modulename, "default") != 0 && !TEST_false(OSSL_PROVIDER_available(libctx, "default"))) return 0; if (strcmp(modulename, "fips") == 0) { OSSL_PROVIDER *prov = NULL; OSSL_PARAM params[2]; is_fips = 1; prov = OSSL_PROVIDER_load(libctx, "fips"); if (prov != NULL) { params[0] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK, &fips_ems_check); params[1] = OSSL_PARAM_construct_end(); OSSL_PROVIDER_get_params(prov, params); OSSL_PROVIDER_unload(prov); } } if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider", tls_provider_init))) return 0; if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) { #ifdef OPENSSL_NO_CRYPTO_MDEBUG TEST_error("not supported in this build"); return 0; #else int i, mcount, rcount, fcount; for (i = 0; i < 4; i++) test_export_key_mat(i); CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount); test_printf_stdout("malloc %d realloc %d free %d\n", mcount, rcount, fcount); return 1; #endif } 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; cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem"); if (cert2 == NULL) goto err; privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem"); if (privkey2 == NULL) goto err; cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem"); if (cert1024 == NULL) goto err; privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem"); if (privkey1024 == NULL) goto err; cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem"); if (cert3072 == NULL) goto err; privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem"); if (privkey3072 == NULL) goto err; cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem"); if (cert4096 == NULL) goto err; privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem"); if (privkey4096 == NULL) goto err; cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem"); if (cert8192 == NULL) goto err; privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem"); if (privkey8192 == NULL) goto err; if (fips_ems_check) { #ifndef OPENSSL_NO_TLS1_2 ADD_TEST(test_no_ems); #endif return 1; } #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK) # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4); ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS * 2); # endif #endif ADD_TEST(test_large_message_tls); ADD_TEST(test_large_message_tls_read_ahead); #ifndef OPENSSL_NO_DTLS ADD_TEST(test_large_message_dtls); #endif ADD_ALL_TESTS(test_large_app_data, 28); ADD_TEST(test_cleanse_plaintext); #ifndef OPENSSL_NO_OCSP ADD_TEST(test_tlsext_status_type); #endif ADD_TEST(test_session_with_only_int_cache); ADD_TEST(test_session_with_only_ext_cache); ADD_TEST(test_session_with_both_cache); ADD_TEST(test_session_wo_ca_names); #ifndef OSSL_NO_USABLE_TLS1_3 ADD_ALL_TESTS(test_stateful_tickets, 3); ADD_ALL_TESTS(test_stateless_tickets, 3); ADD_TEST(test_psk_tickets); ADD_ALL_TESTS(test_extra_tickets, 6); #endif ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS); ADD_TEST(test_ssl_bio_pop_next_bio); ADD_TEST(test_ssl_bio_pop_ssl_bio); ADD_TEST(test_ssl_bio_change_rbio); ADD_TEST(test_ssl_bio_change_wbio); #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3) ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2); ADD_TEST(test_keylog); #endif #ifndef OSSL_NO_USABLE_TLS1_3 ADD_TEST(test_keylog_no_master_key); #endif ADD_TEST(test_client_cert_verify_cb); ADD_TEST(test_ssl_build_cert_chain); ADD_TEST(test_ssl_ctx_build_cert_chain); #ifndef OPENSSL_NO_TLS1_2 ADD_TEST(test_client_hello_cb); ADD_TEST(test_no_ems); ADD_TEST(test_ccs_change_cipher); #endif #ifndef OSSL_NO_USABLE_TLS1_3 ADD_ALL_TESTS(test_early_data_read_write, 6); ADD_ALL_TESTS(test_early_data_replay, 2); ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3); ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3); ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3); ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3); ADD_ALL_TESTS(test_early_data_not_sent, 3); ADD_ALL_TESTS(test_early_data_psk, 8); ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5); ADD_ALL_TESTS(test_early_data_not_expected, 3); # ifndef OPENSSL_NO_TLS1_2 ADD_ALL_TESTS(test_early_data_tls1_2, 3); # endif #endif #ifndef OSSL_NO_USABLE_TLS1_3 ADD_ALL_TESTS(test_set_ciphersuite, 10); ADD_TEST(test_ciphersuite_change); ADD_ALL_TESTS(test_tls13_ciphersuite, 4); # ifdef OPENSSL_NO_PSK ADD_ALL_TESTS(test_tls13_psk, 1); # else ADD_ALL_TESTS(test_tls13_psk, 4); # endif #ifndef OSSL_NO_USABLE_TLS1_3 ADD_ALL_TESTS(test_tls13_no_dhe_kex, 8); #endif # ifndef OPENSSL_NO_TLS1_2 ADD_ALL_TESTS(test_key_exchange, 14); # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) ADD_ALL_TESTS(test_negotiated_group, 4 * (OSSL_NELEM(ecdhe_kexch_groups) + OSSL_NELEM(ffdhe_kexch_groups))); # endif # else ADD_ALL_TESTS(test_key_exchange, 12); # endif ADD_ALL_TESTS(test_custom_exts, 6); ADD_TEST(test_stateless); ADD_TEST(test_pha_key_update); #else ADD_ALL_TESTS(test_custom_exts, 3); #endif ADD_ALL_TESTS(test_export_key_mat, 6); #ifndef OSSL_NO_USABLE_TLS1_3 ADD_ALL_TESTS(test_export_key_mat_early, 3); ADD_TEST(test_key_update); ADD_ALL_TESTS(test_key_update_peer_in_write, 2); ADD_ALL_TESTS(test_key_update_peer_in_read, 2); ADD_ALL_TESTS(test_key_update_local_in_write, 2); ADD_ALL_TESTS(test_key_update_local_in_read, 2); #endif ADD_ALL_TESTS(test_ssl_clear, 8); ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test)); #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2) ADD_ALL_TESTS(test_srp, 6); #endif #if !defined(OPENSSL_NO_COMP_ALG) ADD_ALL_TESTS(test_info_callback, 8); #else ADD_ALL_TESTS(test_info_callback, 6); #endif ADD_ALL_TESTS(test_ssl_pending, 2); ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data)); ADD_ALL_TESTS(test_ticket_callbacks, 20); ADD_ALL_TESTS(test_shutdown, 7); ADD_TEST(test_async_shutdown); ADD_ALL_TESTS(test_incorrect_shutdown, 2); ADD_ALL_TESTS(test_cert_cb, 6); ADD_ALL_TESTS(test_client_cert_cb, 2); ADD_ALL_TESTS(test_ca_names, 3); #ifndef OPENSSL_NO_TLS1_2 ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data)); #endif ADD_ALL_TESTS(test_servername, 10); ADD_TEST(test_unknown_sigalgs_groups); #if !defined(OPENSSL_NO_EC) \ && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)) ADD_ALL_TESTS(test_sigalgs_available, 6); #endif #ifndef OPENSSL_NO_TLS1_3 ADD_ALL_TESTS(test_pluggable_group, 2); ADD_ALL_TESTS(test_pluggable_signature, 4); #endif #ifndef OPENSSL_NO_TLS1_2 ADD_TEST(test_ssl_dup); # ifndef OPENSSL_NO_DH ADD_ALL_TESTS(test_set_tmp_dh, 11); ADD_ALL_TESTS(test_dh_auto, 7); # endif #endif #ifndef OSSL_NO_USABLE_TLS1_3 ADD_TEST(test_sni_tls13); ADD_ALL_TESTS(test_ticket_lifetime, 2); #endif ADD_TEST(test_inherit_verify_param); ADD_TEST(test_set_alpn); ADD_TEST(test_set_verify_cert_store_ssl_ctx); ADD_TEST(test_set_verify_cert_store_ssl); ADD_ALL_TESTS(test_session_timeout, 1); ADD_TEST(test_load_dhfile); #ifndef OSSL_NO_USABLE_TLS1_3 ADD_TEST(test_read_ahead_key_change); ADD_ALL_TESTS(test_tls13_record_padding, 4); #endif #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3) ADD_ALL_TESTS(test_serverinfo_custom, 4); #endif #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) ADD_ALL_TESTS(test_pipelining, 7); #endif ADD_ALL_TESTS(test_version, 6); ADD_TEST(test_rstate_string); ADD_ALL_TESTS(test_handshake_retry, 16); ADD_TEST(test_data_retry); return 1; err: OPENSSL_free(cert); OPENSSL_free(privkey); OPENSSL_free(cert2); OPENSSL_free(privkey2); return 0; } void cleanup_tests(void) { # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH) EVP_PKEY_free(tmp_dh_params); #endif OPENSSL_free(cert); OPENSSL_free(privkey); OPENSSL_free(cert2); OPENSSL_free(privkey2); OPENSSL_free(cert1024); OPENSSL_free(privkey1024); OPENSSL_free(cert3072); OPENSSL_free(privkey3072); OPENSSL_free(cert4096); OPENSSL_free(privkey4096); OPENSSL_free(cert8192); OPENSSL_free(privkey8192); bio_s_mempacket_test_free(); bio_s_always_retry_free(); bio_s_maybe_retry_free(); OSSL_PROVIDER_unload(defctxnull); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/sslapitest.c
openssl
#include <string.h> #include "helpers/ssltestlib.h" #include "testutil.h" static char *cert = NULL; static char *privkey = NULL; #define TEST_PLAINTEXT_OVERFLOW_OK 0 #define TEST_PLAINTEXT_OVERFLOW_NOT_OK 1 #define TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK 2 #define TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK 3 #define TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK 4 #define TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK 5 #define TOTAL_RECORD_OVERFLOW_TESTS 6 static int write_record(BIO *b, size_t len, uint8_t rectype, int recversion) { unsigned char header[SSL3_RT_HEADER_LENGTH]; size_t written; unsigned char buf[256]; memset(buf, 0, sizeof(buf)); header[0] = rectype; header[1] = (recversion >> 8) & 0xff; header[2] = recversion & 0xff; header[3] = (len >> 8) & 0xff; header[4] = len & 0xff; if (!BIO_write_ex(b, header, SSL3_RT_HEADER_LENGTH, &written) || written != SSL3_RT_HEADER_LENGTH) return 0; while (len > 0) { size_t outlen; if (len > sizeof(buf)) outlen = sizeof(buf); else outlen = len; if (!BIO_write_ex(b, buf, outlen, &written) || written != outlen) return 0; len -= outlen; } return 1; } static int fail_due_to_record_overflow(int enc) { long err = ERR_peek_error(); int reason; if (enc) reason = SSL_R_ENCRYPTED_LENGTH_TOO_LONG; else reason = SSL_R_DATA_LENGTH_TOO_LONG; if (ERR_GET_LIB(err) == ERR_LIB_SSL && ERR_GET_REASON(err) == reason) return 1; return 0; } static int test_record_overflow(int idx) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; size_t len = 0; size_t written; int overf_expected; unsigned char buf; BIO *serverbio; int recversion; #ifdef OPENSSL_NO_TLS1_2 if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK) return 1; #endif #if defined(OPENSSL_NO_TLS1_3) \ || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)) if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK) return 1; #endif if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey))) goto end; if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_OK || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK) { len = SSL3_RT_MAX_ENCRYPTED_LENGTH; #ifndef OPENSSL_NO_COMP len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD; #endif SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION); } else if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_OK || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK) { len = SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH; } if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; serverbio = SSL_get_rbio(serverssl); if (idx == TEST_PLAINTEXT_OVERFLOW_OK || idx == TEST_PLAINTEXT_OVERFLOW_NOT_OK) { len = SSL3_RT_MAX_PLAIN_LENGTH; if (idx == TEST_PLAINTEXT_OVERFLOW_NOT_OK) len++; if (!TEST_true(write_record(serverbio, len, SSL3_RT_HANDSHAKE, TLS1_VERSION))) goto end; if (!TEST_int_le(SSL_accept(serverssl), 0)) goto end; overf_expected = (idx == TEST_PLAINTEXT_OVERFLOW_OK) ? 0 : 1; if (!TEST_int_eq(fail_due_to_record_overflow(0), overf_expected)) goto end; goto success; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (idx == TEST_ENCRYPTED_OVERFLOW_TLS1_2_NOT_OK || idx == TEST_ENCRYPTED_OVERFLOW_TLS1_3_NOT_OK) { overf_expected = 1; len++; } else { overf_expected = 0; } recversion = TLS1_2_VERSION; if (!TEST_true(write_record(serverbio, len, SSL3_RT_APPLICATION_DATA, recversion))) goto end; if (!TEST_false(SSL_read_ex(serverssl, &buf, sizeof(buf), &written))) goto end; if (!TEST_int_eq(fail_due_to_record_overflow(1), overf_expected)) goto end; success: 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_record_overflow, TOTAL_RECORD_OVERFLOW_TESTS); return 1; } void cleanup_tests(void) { bio_s_mempacket_test_free(); }
test
openssl/test/recordlentest.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <time.h> #include "internal/packet.h" #include "testutil.h" #include "internal/nelem.h" #include "helpers/ssltestlib.h" #define CLIENT_VERSION_LEN 2 static const char *host = "dummy-host"; static char *cert = NULL; static char *privkey = NULL; #if defined(OPENSSL_NO_TLS1_3) || \ (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)) static int maxversion = TLS1_2_VERSION; #else static int maxversion = 0; #endif static int get_sni_from_client_hello(BIO *bio, char **sni) { long len; unsigned char *data; PACKET pkt, pkt2, pkt3, pkt4, pkt5; unsigned int servname_type = 0, type = 0; int ret = 0; memset(&pkt, 0, sizeof(pkt)); memset(&pkt2, 0, sizeof(pkt2)); memset(&pkt3, 0, sizeof(pkt3)); memset(&pkt4, 0, sizeof(pkt4)); memset(&pkt5, 0, sizeof(pkt5)); if (!TEST_long_ge(len = BIO_get_mem_data(bio, (char **)&data), 0) || !TEST_true(PACKET_buf_init(&pkt, data, len)) || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH) || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) goto end; while (PACKET_remaining(&pkt2)) { if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) goto end; if (type == TLSEXT_TYPE_server_name) { if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3, &pkt4)) || !TEST_uint_ne(PACKET_remaining(&pkt4), 0) || !TEST_true(PACKET_get_1(&pkt4, &servname_type)) || !TEST_uint_eq(servname_type, TLSEXT_NAMETYPE_host_name) || !TEST_true(PACKET_get_length_prefixed_2(&pkt4, &pkt5)) || !TEST_uint_le(PACKET_remaining(&pkt5), TLSEXT_MAXLEN_host_name) || !TEST_false(PACKET_contains_zero_byte(&pkt5)) || !TEST_true(PACKET_strndup(&pkt5, sni))) goto end; ret = 1; goto end; } } end: return ret; } static int client_setup_sni_before_state(void) { SSL_CTX *ctx; SSL *con = NULL; BIO *rbio; BIO *wbio; char *hostname = NULL; int ret = 0; ctx = SSL_CTX_new(TLS_method()); if (!TEST_ptr(ctx)) goto end; if (maxversion > 0 && !TEST_true(SSL_CTX_set_max_proto_version(ctx, maxversion))) goto end; con = SSL_new(ctx); if (!TEST_ptr(con)) goto end; SSL_set_tlsext_host_name(con, host); rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { BIO_free(rbio); BIO_free(wbio); goto end; } SSL_set_bio(con, rbio, wbio); if (!TEST_int_le(SSL_connect(con), 0)) goto end; if (!TEST_true(get_sni_from_client_hello(wbio, &hostname))) goto end; if (!TEST_str_eq(hostname, host)) goto end; ret = 1; end: OPENSSL_free(hostname); SSL_free(con); SSL_CTX_free(ctx); return ret; } static int client_setup_sni_after_state(void) { SSL_CTX *ctx; SSL *con = NULL; BIO *rbio; BIO *wbio; char *hostname = NULL; int ret = 0; ctx = SSL_CTX_new(TLS_method()); if (!TEST_ptr(ctx)) goto end; if (maxversion > 0 && !TEST_true(SSL_CTX_set_max_proto_version(ctx, maxversion))) goto end; con = SSL_new(ctx); if (!TEST_ptr(con)) goto end; rbio = BIO_new(BIO_s_mem()); wbio = BIO_new(BIO_s_mem()); if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { BIO_free(rbio); BIO_free(wbio); goto end; } SSL_set_bio(con, rbio, wbio); SSL_set_connect_state(con); SSL_set_tlsext_host_name(con, host); if (!TEST_int_le(SSL_connect(con), 0)) goto end; if (!TEST_true(get_sni_from_client_hello(wbio, &hostname))) goto end; if (!TEST_str_eq(hostname, host)) goto end; ret = 1; end: OPENSSL_free(hostname); SSL_free(con); SSL_CTX_free(ctx); return ret; } static int server_setup_sni(void) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &sctx, &cctx, cert, privkey)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; SSL_set_tlsext_host_name(serverssl, host); if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; if (!TEST_ptr_null(SSL_get_servername(serverssl, TLSEXT_NAMETYPE_host_name))) { goto end; } testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } typedef int (*sni_test_fn)(void); static sni_test_fn sni_test_fns[3] = { client_setup_sni_before_state, client_setup_sni_after_state, server_setup_sni }; static int test_servername(int test) { return sni_test_fns[test](); } 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_servername, OSSL_NELEM(sni_test_fns)); return 1; }
test
openssl/test/servername_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/pem.h> #include <openssl/x509.h> #include "testutil.h" static const char *c; static const char *k; static const char *t; static const char *e; static int test_x509_check_cert_pkey(void) { BIO *bio = NULL; X509 *x509 = NULL; X509_REQ *x509_req = NULL; EVP_PKEY *pkey = NULL; int ret = 0, type = 0, expected = 0, result = 0; if (strcmp(t, "cert") == 0) { type = 1; } else if (strcmp(t, "req") == 0) { type = 2; } else { TEST_error("invalid 'type'"); goto failed; } if (strcmp(e, "ok") == 0) { expected = 1; } else if (strcmp(e, "failed") == 0) { expected = 0; } else { TEST_error("invalid 'expected'"); goto failed; } if (!TEST_ptr(bio = BIO_new_file(k, "r"))) goto failed; if (!TEST_ptr(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL))) goto failed; BIO_free(bio); if (!TEST_ptr(bio = BIO_new_file(c, "r"))) goto failed; switch (type) { case 1: x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); if (x509 == NULL) { TEST_error("read PEM x509 failed"); goto failed; } result = X509_check_private_key(x509, pkey); break; case 2: x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL); if (x509_req == NULL) { TEST_error("read PEM x509 req failed"); goto failed; } result = X509_REQ_check_private_key(x509_req, pkey); break; default: break; } if (!TEST_int_eq(result, expected)) { TEST_error("check private key: expected: %d, got: %d", expected, result); goto failed; } ret = 1; failed: BIO_free(bio); X509_free(x509); X509_REQ_free(x509_req); EVP_PKEY_free(pkey); return ret; } static const char *file; static int expected; static int test_PEM_X509_INFO_read_bio(void) { BIO *in; STACK_OF(X509_INFO) *sk; X509_INFO *it; int i, count = 0; if (!TEST_ptr((in = BIO_new_file(file, "r")))) return 0; sk = PEM_X509_INFO_read_bio(in, NULL, NULL, ""); BIO_free(in); for (i = 0; i < sk_X509_INFO_num(sk); i++) { it = sk_X509_INFO_value(sk, i); if (it->x509 != NULL) count++; if (it->crl != NULL) count++; if (it->x_pkey != NULL) count++; } sk_X509_INFO_pop_free(sk, X509_INFO_free); return TEST_int_eq(count, expected); } const OPTIONS *test_get_options(void) { enum { OPT_TEST_ENUM }; static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("cert key type expected\n" " or [options] file num\n"), { OPT_HELP_STR, 1, '-', "cert\tcertificate or CSR filename in PEM\n" }, { OPT_HELP_STR, 1, '-', "key\tprivate key filename in PEM\n" }, { OPT_HELP_STR, 1, '-', "type\t\tvalue must be 'cert' or 'req'\n" }, { OPT_HELP_STR, 1, '-', "expected\tthe expected return value, either 'ok' or 'failed'\n" }, { OPT_HELP_STR, 1, '-', "file\tPEM format file containing certs, keys, and/OR CRLs\n" }, { OPT_HELP_STR, 1, '-', "num\texpected number of credentials to be loaded from file\n" }, { NULL } }; return test_options; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (test_get_argument_count() == 2) { const char *num; if (!TEST_ptr(file = test_get_argument(0)) || !TEST_ptr(num = test_get_argument(1))) return 0; if (!TEST_int_eq(sscanf(num, "%d", &expected), 1)) return 0; ADD_TEST(test_PEM_X509_INFO_read_bio); return 1; } if (!TEST_ptr(c = test_get_argument(0)) || !TEST_ptr(k = test_get_argument(1)) || !TEST_ptr(t = test_get_argument(2)) || !TEST_ptr(e = test_get_argument(3))) { return 0; } ADD_TEST(test_x509_check_cert_pkey); return 1; }
test
openssl/test/x509_check_cert_pkey_test.c
openssl
#include "internal/nelem.h" #include "../ssl/ssl_local.h" #include "../ssl/statem/statem_local.h" #include "testutil.h" #define EXT_ENTRY(name) { TLSEXT_IDX_##name, TLSEXT_TYPE_##name, #name } #define EXT_EXCEPTION(name) { TLSEXT_IDX_##name, TLSEXT_TYPE_invalid, #name } #define EXT_END(name) { TLSEXT_IDX_##name, TLSEXT_TYPE_out_of_range, #name } typedef struct { size_t idx; unsigned int type; char *name; } EXT_LIST; static EXT_LIST ext_list[] = { EXT_ENTRY(renegotiate), EXT_ENTRY(server_name), EXT_ENTRY(max_fragment_length), #ifndef OPENSSL_NO_SRP EXT_ENTRY(srp), #else EXT_EXCEPTION(srp), #endif EXT_ENTRY(ec_point_formats), EXT_ENTRY(supported_groups), EXT_ENTRY(session_ticket), #ifndef OPENSSL_NO_OCSP EXT_ENTRY(status_request), #else EXT_EXCEPTION(status_request), #endif #ifndef OPENSSL_NO_NEXTPROTONEG EXT_ENTRY(next_proto_neg), #else EXT_EXCEPTION(next_proto_neg), #endif EXT_ENTRY(application_layer_protocol_negotiation), #ifndef OPENSSL_NO_SRTP EXT_ENTRY(use_srtp), #else EXT_EXCEPTION(use_srtp), #endif EXT_ENTRY(encrypt_then_mac), #ifndef OPENSSL_NO_CT EXT_ENTRY(signed_certificate_timestamp), #else EXT_EXCEPTION(signed_certificate_timestamp), #endif EXT_ENTRY(extended_master_secret), EXT_ENTRY(signature_algorithms_cert), EXT_ENTRY(post_handshake_auth), EXT_ENTRY(client_cert_type), EXT_ENTRY(server_cert_type), EXT_ENTRY(signature_algorithms), EXT_ENTRY(supported_versions), EXT_ENTRY(psk_kex_modes), EXT_ENTRY(key_share), EXT_ENTRY(cookie), EXT_ENTRY(cryptopro_bug), EXT_ENTRY(compress_certificate), EXT_ENTRY(early_data), EXT_ENTRY(certificate_authorities), EXT_ENTRY(padding), EXT_ENTRY(psk), EXT_END(num_builtins) }; static int test_extension_list(void) { size_t n = OSSL_NELEM(ext_list); size_t i; unsigned int type; int retval = 1; for (i = 0; i < n; i++) { if (!TEST_size_t_eq(i, ext_list[i].idx)) { retval = 0; TEST_error("TLSEXT_IDX_%s=%zd, found at=%zd\n", ext_list[i].name, ext_list[i].idx, i); } type = ossl_get_extension_type(ext_list[i].idx); if (!TEST_uint_eq(type, ext_list[i].type)) { retval = 0; TEST_error("TLSEXT_IDX_%s=%zd expected=0x%05X got=0x%05X", ext_list[i].name, ext_list[i].idx, ext_list[i].type, type); } } return retval; } int setup_tests(void) { ADD_TEST(test_extension_list); return 1; }
test
openssl/test/ext_internal_test.c
openssl
#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) 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; if (!TEST_int_eq(RSA_generate_multi_prime_key(rsa, 1024, 2, NULL, NULL), 0)) goto err; if (!TEST_int_eq(RSA_generate_multi_prime_key(rsa, 500, 2, ebn, NULL), 0)) goto err; 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; }
test
openssl/test/rsa_mp_test.c
openssl
#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; }
test
openssl/test/mdc2test.c
openssl
#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); 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); 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); 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); ADD_TEST(test_HDR_init_with_ref); ADD_TEST(test_HDR_init_with_subject); return 1; }
test
openssl/test/cmp_hdr_test.c
openssl
#include <stddef.h> #include <string.h> #include <openssl/provider.h> #include <openssl/params.h> #include <openssl/core_names.h> #include <openssl/evp.h> #include <openssl/store.h> #include <openssl/ui.h> #include "testutil.h" #include "fake_rsaprov.h" static OSSL_LIB_CTX *libctx = NULL; extern int key_deleted; static int fetch_sig(OSSL_LIB_CTX *ctx, const char *alg, const char *propq, OSSL_PROVIDER *expected_prov) { OSSL_PROVIDER *prov; EVP_SIGNATURE *sig = EVP_SIGNATURE_fetch(ctx, "RSA", propq); int ret = 0; if (!TEST_ptr(sig)) return 0; if (!TEST_ptr(prov = EVP_SIGNATURE_get0_provider(sig))) goto end; if (!TEST_ptr_eq(prov, expected_prov)) { TEST_info("Fetched provider: %s, Expected provider: %s", OSSL_PROVIDER_get0_name(prov), OSSL_PROVIDER_get0_name(expected_prov)); goto end; } ret = 1; end: EVP_SIGNATURE_free(sig); return ret; } static int test_pkey_sig(void) { OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; int i, ret = 0; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *ctx = NULL; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) return 0; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_true(fetch_sig(libctx, "RSA", "provider=fake-rsa", fake_rsa)) || !TEST_true(fetch_sig(libctx, "RSA", "?provider=fake-rsa", fake_rsa))) goto end; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", "provider=fake-rsa")) || !TEST_true(EVP_PKEY_fromdata_init(ctx)) || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, NULL)) || !TEST_ptr(pkey)) goto end; EVP_PKEY_CTX_free(ctx); ctx = NULL; for (i = 0; i < 3; i++) { size_t siglen; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "?provider=default"))) goto end; if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1)) goto end; if (!TEST_int_eq(EVP_PKEY_sign(ctx, NULL, &siglen, NULL, 0), 1) || !TEST_size_t_eq(siglen, 256)) goto end; EVP_PKEY_CTX_free(ctx); ctx = NULL; } ret = 1; end: fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); return ret; } static int test_alternative_keygen_init(void) { EVP_PKEY_CTX *ctx = NULL; OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; const OSSL_PROVIDER *provider; const char *provname; int ret = 0; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL))) goto end; if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)) goto end; if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx))) goto end; if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider))) goto end; if (!TEST_str_eq(provname, "default")) goto end; EVP_PKEY_CTX_free(ctx); ctx = NULL; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) return 0; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", "?provider=fake-rsa"))) goto end; if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)) goto end; if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx))) goto end; if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider))) goto end; if (!TEST_str_eq(provname, "fake-rsa")) goto end; ret = 1; end: fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); EVP_PKEY_CTX_free(ctx); return ret; } static int test_pkey_eq(void) { OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; EVP_PKEY *pkey_fake = NULL; EVP_PKEY *pkey_dflt = NULL; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM *params = NULL; int ret = 0; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) return 0; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_ptr(params = fake_rsa_key_params(0)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", "provider=fake-rsa")) || !TEST_true(EVP_PKEY_fromdata_init(ctx)) || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_fake, EVP_PKEY_PUBLIC_KEY, params)) || !TEST_ptr(pkey_fake)) goto end; EVP_PKEY_CTX_free(ctx); ctx = NULL; OSSL_PARAM_free(params); params = NULL; if (!TEST_ptr(params = fake_rsa_key_params(0)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", "provider=default")) || !TEST_true(EVP_PKEY_fromdata_init(ctx)) || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_dflt, EVP_PKEY_PUBLIC_KEY, params)) || !TEST_ptr(pkey_dflt)) goto end; EVP_PKEY_CTX_free(ctx); ctx = NULL; OSSL_PARAM_free(params); params = NULL; if (!TEST_int_eq(EVP_PKEY_eq(pkey_fake, pkey_dflt), 1)) goto end; ret = 1; end: fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey_fake); EVP_PKEY_free(pkey_dflt); OSSL_PARAM_free(params); return ret; } static int test_pkey_store(int idx) { OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; int ret = 0; EVP_PKEY *pkey = NULL; OSSL_STORE_LOADER *loader = NULL; OSSL_STORE_CTX *ctx = NULL; OSSL_STORE_INFO *info; const char *propq = idx == 0 ? "?provider=fake-rsa" : "?provider=default"; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) goto end; if (!TEST_ptr(loader = OSSL_STORE_LOADER_fetch(libctx, "fake_rsa", propq))) goto end; OSSL_STORE_LOADER_free(loader); if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:test", libctx, propq, NULL, NULL, NULL, NULL, NULL))) goto end; while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL && pkey == NULL) { if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) pkey = OSSL_STORE_INFO_get1_PKEY(info); OSSL_STORE_INFO_free(info); info = NULL; } if (!TEST_ptr(pkey) || !TEST_int_eq(EVP_PKEY_is_a(pkey, "RSA"), 1)) goto end; ret = 1; end: fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); OSSL_STORE_close(ctx); EVP_PKEY_free(pkey); return ret; } static int test_pkey_delete(void) { OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; int ret = 0; EVP_PKEY *pkey = NULL; OSSL_STORE_LOADER *loader = NULL; OSSL_STORE_CTX *ctx = NULL; OSSL_STORE_INFO *info; const char *propq = "?provider=fake-rsa"; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) goto end; if (!TEST_ptr(loader = OSSL_STORE_LOADER_fetch(libctx, "fake_rsa", propq))) goto end; OSSL_STORE_LOADER_free(loader); if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:test", libctx, propq, NULL, NULL, NULL, NULL, NULL))) goto end; while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL && pkey == NULL) { if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) pkey = OSSL_STORE_INFO_get1_PKEY(info); OSSL_STORE_INFO_free(info); info = NULL; } if (!TEST_ptr(pkey) || !TEST_int_eq(EVP_PKEY_is_a(pkey, "RSA"), 1)) goto end; EVP_PKEY_free(pkey); pkey = NULL; if (!TEST_int_eq(OSSL_STORE_delete("fake_rsa:test", libctx, propq, NULL, NULL, NULL), 1)) goto end; if (!TEST_int_eq(OSSL_STORE_close(ctx), 1)) goto end; if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:test", libctx, propq, NULL, NULL, NULL, NULL, NULL))) goto end; while (!OSSL_STORE_eof(ctx)) { info = OSSL_STORE_load(ctx); if (!TEST_ptr_null(info)) goto end; } ret = 1; end: fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); OSSL_STORE_close(ctx); fake_rsa_restore_store_state(); return ret; } static int fake_pw_read_string(UI *ui, UI_STRING *uis) { const char *passphrase = FAKE_PASSPHRASE; if (UI_get_string_type(uis) == UIT_PROMPT) { UI_set_result(ui, uis, passphrase); return 1; } return 0; } static int test_pkey_store_open_ex(void) { OSSL_PROVIDER *deflt = NULL; OSSL_PROVIDER *fake_rsa = NULL; int ret = 0; EVP_PKEY *pkey = NULL; OSSL_STORE_LOADER *loader = NULL; OSSL_STORE_CTX *ctx = NULL; const char *propq = "?provider=fake-rsa"; UI_METHOD *ui_method = NULL; if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default"))) goto end; if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx))) goto end; if (!TEST_ptr(loader = OSSL_STORE_LOADER_fetch(libctx, "fake_rsa", propq))) goto end; OSSL_STORE_LOADER_free(loader); if (!TEST_ptr(ui_method= UI_create_method("PW Callbacks"))) goto end; if (UI_method_set_reader(ui_method, fake_pw_read_string)) goto end; if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:openpwtest", libctx, propq, ui_method, NULL, NULL, NULL, NULL))) goto end; OSSL_STORE_close(ctx); if (!TEST_ptr_null(ctx = OSSL_STORE_open_ex("fake_rsa:openpwtest", libctx, propq, NULL, NULL, NULL, NULL, NULL))) goto end; ret = 1; end: UI_destroy_method(ui_method); fake_rsa_finish(fake_rsa); OSSL_PROVIDER_unload(deflt); OSSL_STORE_close(ctx); EVP_PKEY_free(pkey); return ret; } int setup_tests(void) { libctx = OSSL_LIB_CTX_new(); if (libctx == NULL) return 0; ADD_TEST(test_pkey_sig); ADD_TEST(test_alternative_keygen_init); ADD_TEST(test_pkey_eq); ADD_ALL_TESTS(test_pkey_store, 2); ADD_TEST(test_pkey_delete); ADD_TEST(test_pkey_store_open_ex); return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(libctx); }
test
openssl/test/provider_pkey_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/lhash.h> #include <openssl/err.h> #include <openssl/crypto.h> #include "internal/nelem.h" #include "testutil.h" #ifdef __clang__ #pragma clang diagnostic ignored "-Wunused-function" #endif DEFINE_LHASH_OF_EX(int); static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9, -17, 16, 17, -23, 35, 37, 173, 11 }; static const unsigned int n_int_tests = OSSL_NELEM(int_tests); static short int_found[OSSL_NELEM(int_tests)]; static short int_not_found; static unsigned long int int_hash(const int *p) { return 3 & *p; } static int int_cmp(const int *p, const int *q) { return *p != *q; } static int int_find(int n) { unsigned int i; for (i = 0; i < n_int_tests; i++) if (int_tests[i] == n) return i; return -1; } static void int_doall(int *v) { const int n = int_find(*v); if (n < 0) int_not_found++; else int_found[n]++; } static void int_doall_arg(int *p, short *f) { const int n = int_find(*p); if (n < 0) int_not_found++; else f[n]++; } IMPLEMENT_LHASH_DOALL_ARG(int, short); static int test_int_lhash(void) { static struct { int data; int null; } dels[] = { { 65537, 0 }, { 173, 0 }, { 999, 1 }, { 37, 0 }, { 1, 0 }, { 34, 1 } }; const unsigned int n_dels = OSSL_NELEM(dels); LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp); unsigned int i; int testresult = 0, j, *p; if (!TEST_ptr(h)) goto end; for (i = 0; i < n_int_tests; i++) if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) { TEST_info("int insert %d", i); goto end; } if (!TEST_int_eq(lh_int_num_items(h), n_int_tests)) goto end; for (i = 0; i < n_int_tests; i++) if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) { TEST_info("lhash int retrieve value %d", i); goto end; } for (i = 0; i < n_int_tests; i++) if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) { TEST_info("lhash int retrieve address %d", i); goto end; } j = 1; if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2)) goto end; j = 13; if (!TEST_ptr(p = lh_int_insert(h, &j))) goto end; if (!TEST_ptr_eq(p, int_tests + 1)) goto end; if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j)) goto end; memset(int_found, 0, sizeof(int_found)); int_not_found = 0; lh_int_doall(h, &int_doall); if (!TEST_int_eq(int_not_found, 0)) { TEST_info("lhash int doall encountered a not found condition"); goto end; } for (i = 0; i < n_int_tests; i++) if (!TEST_int_eq(int_found[i], 1)) { TEST_info("lhash int doall %d", i); goto end; } memset(int_found, 0, sizeof(int_found)); int_not_found = 0; lh_int_doall_short(h, int_doall_arg, int_found); if (!TEST_int_eq(int_not_found, 0)) { TEST_info("lhash int doall arg encountered a not found condition"); goto end; } for (i = 0; i < n_int_tests; i++) if (!TEST_int_eq(int_found[i], 1)) { TEST_info("lhash int doall arg %d", i); goto end; } for (i = 0; i < n_dels; i++) { const int b = lh_int_delete(h, &dels[i].data) == NULL; if (!TEST_int_eq(b ^ dels[i].null, 0)) { TEST_info("lhash int delete %d", i); goto end; } } if (!TEST_int_eq(lh_int_error(h), 0)) goto end; testresult = 1; end: lh_int_free(h); return testresult; } static unsigned long int stress_hash(const int *p) { return *p; } static int test_stress(void) { LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp); const unsigned int n = 2500000; unsigned int i; int testresult = 0, *p; if (!TEST_ptr(h)) goto end; for (i = 0; i < n; i++) { p = OPENSSL_malloc(sizeof(i)); if (!TEST_ptr(p)) { TEST_info("lhash stress out of memory %d", i); goto end; } *p = 3 * i + 1; lh_int_insert(h, p); } if (!TEST_int_eq(lh_int_num_items(h), n)) goto end; for (i = 0; i < n; i++) { const int j = (7 * i + 4) % n * 3 + 1; if (!TEST_ptr(p = lh_int_delete(h, &j))) { TEST_info("lhash stress delete %d\n", i); goto end; } if (!TEST_int_eq(*p, j)) { TEST_info("lhash stress bad value %d", i); goto end; } OPENSSL_free(p); } testresult = 1; end: lh_int_free(h); return testresult; } int setup_tests(void) { ADD_TEST(test_int_lhash); ADD_TEST(test_stress); return 1; }
test
openssl/test/lhash_test.c
openssl
#include <string.h> #include "apps.h" #include "testutil.h" #include "crypto/asn1.h" #define binname "ca_internals_test" char *default_config_file = NULL; static int test_do_updatedb(void) { CA_DB *db = NULL; time_t testdateutc; int rv; size_t argc = test_get_argument_count(); BIO *bio_tmp; char *testdate; char *indexfile; int need64bit; int have64bit; if (argc != 4) { TEST_error("Usage: %s: do_updatedb dbfile testdate need64bit\n", binname); TEST_error(" testdate format: ASN1-String\n"); return 0; } need64bit = (int)strtol(test_get_argument(3), NULL, 0); have64bit = sizeof(time_t) > sizeof(uint32_t); if (need64bit && !have64bit) { BIO_printf(bio_out, "skipping test (need64bit: %i, have64bit: %i)", need64bit, have64bit); return 1; } testdate = test_get_argument(2); testdateutc = ossl_asn1_string_to_time_t(testdate); if (TEST_time_t_lt(testdateutc, 0)) { return 0; } indexfile = test_get_argument(1); db = load_index(indexfile, NULL); if (TEST_ptr_null(db)) { return 0; } bio_tmp = bio_err; bio_err = bio_out; rv = do_updatedb(db, &testdateutc); bio_err = bio_tmp; if (rv > 0) { if (!TEST_true(save_index(indexfile, "new", db))) goto end; if (!TEST_true(rotate_index(indexfile, "new", "old"))) goto end; } end: free_index(db); return 1; } int setup_tests(void) { char *command = test_get_argument(0); if (test_get_argument_count() < 1) { TEST_error("%s: no command specified for testing\n", binname); return 0; } if (strcmp(command, "do_updatedb") == 0) return test_do_updatedb(); TEST_error("%s: command '%s' is not supported for testing\n", binname, command); return 0; }
test
openssl/test/ca_internals_test.c
openssl
#include <openssl/pem.h> #include <openssl/core_names.h> #include <openssl/self_test.h> #include "testutil.h" typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_PROVIDER_NAME, OPT_CONFIG_FILE, OPT_PAIRWISETEST, OPT_DSAPARAM, OPT_TEST_ENUM } OPTION_CHOICE; struct self_test_arg { const char *type; }; static OSSL_LIB_CTX *libctx = NULL; static char *pairwise_name = NULL; static char *dsaparam_file = NULL; static struct self_test_arg self_test_args = { 0 }; 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" }, { "pairwise", OPT_PAIRWISETEST, 's', "Test keygen pairwise test failures" }, { "dsaparam", OPT_DSAPARAM, 's', "DSA param file" }, { NULL } }; return test_options; } static int self_test_on_pairwise_fail(const OSSL_PARAM params[], void *arg) { struct self_test_arg *args = arg; const OSSL_PARAM *p = NULL; const char *type = NULL, *phase = NULL; p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE); if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) return 0; phase = (const char *)p->data; if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0) { p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE); if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) return 0; type = (const char *)p->data; if (strcmp(type, args->type) == 0) return 0; } return 1; } static int setup_selftest_pairwise_failure(const char *type) { int ret = 0; OSSL_PROVIDER *prov = NULL; if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, "fips"))) goto err; self_test_args.type = type; OSSL_SELF_TEST_set_callback(libctx, self_test_on_pairwise_fail, &self_test_args); ret = 1; err: OSSL_PROVIDER_unload(prov); return ret; } static int test_keygen_pairwise_failure(void) { BIO *bio = NULL; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pParams = NULL; EVP_PKEY *pkey = NULL; const char *type = OSSL_SELF_TEST_TYPE_PCT; int ret = 0; if (strcmp(pairwise_name, "rsa") == 0) { if (!TEST_true(setup_selftest_pairwise_failure(type))) goto err; if (!TEST_ptr_null(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", 2048))) goto err; } else if (strncmp(pairwise_name, "ec", 2) == 0) { if (strcmp(pairwise_name, "eckat") == 0) type = OSSL_SELF_TEST_TYPE_PCT_KAT; if (!TEST_true(setup_selftest_pairwise_failure(type))) goto err; if (!TEST_ptr_null(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "EC", "P-256"))) goto err; } else if (strncmp(pairwise_name, "dsa", 3) == 0) { if (strcmp(pairwise_name, "dsakat") == 0) type = OSSL_SELF_TEST_TYPE_PCT_KAT; if (!TEST_true(setup_selftest_pairwise_failure(type))) goto err; if (!TEST_ptr(bio = BIO_new_file(dsaparam_file, "r"))) goto err; if (!TEST_ptr(pParams = PEM_read_bio_Parameters_ex(bio, NULL, libctx, NULL))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pParams, NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_keygen_init(ctx), 1)) goto err; if (!TEST_int_le(EVP_PKEY_keygen(ctx, &pkey), 0)) goto err; if (!TEST_ptr_null(pkey)) goto err; } else if (strncmp(pairwise_name, "eddsa", 5) == 0) { if (!TEST_true(setup_selftest_pairwise_failure(type))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "ED25519", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_keygen_init(ctx), 1)) goto err; if (!TEST_int_le(EVP_PKEY_keygen(ctx, &pkey), 0)) goto err; if (!TEST_ptr_null(pkey)) goto err; } ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); BIO_free(bio); EVP_PKEY_free(pParams); return ret; } int setup_tests(void) { OPTION_CHOICE o; char *config_file = NULL; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_PAIRWISETEST: pairwise_name = opt_arg(); break; case OPT_DSAPARAM: dsaparam_file = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } libctx = OSSL_LIB_CTX_new(); if (libctx == NULL) return 0; if (!OSSL_LIB_CTX_load_config(libctx, config_file)) { opt_printf_stderr("Failed to load config\n"); return 0; } ADD_TEST(test_keygen_pairwise_failure); return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(libctx); }
test
openssl/test/pairwise_fail_test.c
openssl
#include <openssl/ssl.h> #include <openssl/quic.h> #include <openssl/bio.h> #include <openssl/lhash.h> #include <openssl/rand.h> #include "internal/quic_tserver.h" #include "internal/quic_ssl.h" #include "internal/quic_error.h" #include "internal/quic_stream_map.h" #include "internal/quic_engine.h" #include "testutil.h" #include "helpers/quictestlib.h" #if defined(OPENSSL_THREADS) # include "internal/thread_arch.h" #endif #include "internal/numbers.h" static const char *certfile, *keyfile; #if defined(OPENSSL_THREADS) struct child_thread_args { struct helper *h; const struct script_op *script; const char *script_name; int thread_idx; CRYPTO_THREAD *t; CRYPTO_MUTEX *m; int testresult; int done; int s_checked_out; }; #endif typedef struct stream_info { const char *name; SSL *c_stream; uint64_t s_stream_id; } STREAM_INFO; DEFINE_LHASH_OF_EX(STREAM_INFO); struct helper { int s_fd; BIO *s_net_bio, *s_net_bio_own, *s_qtf_wbio, *s_qtf_wbio_own; BIO_ADDR *s_net_bio_orig_addr; BIO_ADDR *s_net_bio_addr; QUIC_TSERVER *s, *s_priv; LHASH_OF(STREAM_INFO) *s_streams; int c_fd; BIO *c_net_bio, *c_net_bio_own; SSL_CTX *c_ctx; SSL *c_conn; LHASH_OF(STREAM_INFO) *c_streams; #if defined(OPENSSL_THREADS) struct child_thread_args *threads; size_t num_threads; CRYPTO_MUTEX *misc_m; CRYPTO_CONDVAR *misc_cv; #endif OSSL_TIME start_time; CRYPTO_RWLOCK *time_lock; OSSL_TIME time_slip; QTEST_FAULT *qtf; int init, blocking, check_spin_again; int free_order, need_injector; int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t buf_len); int (*qtf_handshake_cb)(struct helper *h, unsigned char *buf, size_t buf_len); int (*qtf_datagram_cb)(struct helper *h, BIO_MSG *m, size_t stride); uint64_t inject_word0, inject_word1; uint64_t scratch0, scratch1, fail_count; #if defined(OPENSSL_THREADS) struct { CRYPTO_THREAD *t; CRYPTO_MUTEX *m; CRYPTO_CONDVAR *c; int ready, stop; } server_thread; int s_checked_out; #endif }; struct helper_local { struct helper *h; LHASH_OF(STREAM_INFO) *c_streams; int thread_idx; const struct script_op *check_op; int explicit_event_handling; }; struct script_op { uint32_t op; const void *arg0; size_t arg1; int (*check_func)(struct helper *h, struct helper_local *hl); const char *stream_name; uint64_t arg2; int (*qtf_packet_plain_cb)(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t buf_len); int (*qtf_handshake_cb)(struct helper *h, unsigned char *buf, size_t buf_len); int (*qtf_datagram_cb)(struct helper *h, BIO_MSG *m, size_t stride); }; #define OPK_END 0 #define OPK_CHECK 1 #define OPK_C_SET_ALPN 2 #define OPK_C_CONNECT_WAIT 3 #define OPK_C_WRITE 4 #define OPK_S_WRITE 5 #define OPK_C_READ_EXPECT 6 #define OPK_S_READ_EXPECT 7 #define OPK_C_EXPECT_FIN 8 #define OPK_S_EXPECT_FIN 9 #define OPK_C_CONCLUDE 10 #define OPK_S_CONCLUDE 11 #define OPK_C_DETACH 12 #define OPK_C_ATTACH 13 #define OPK_C_NEW_STREAM 14 #define OPK_S_NEW_STREAM 15 #define OPK_C_ACCEPT_STREAM_WAIT 16 #define OPK_C_ACCEPT_STREAM_NONE 17 #define OPK_C_FREE_STREAM 18 #define OPK_C_SET_DEFAULT_STREAM_MODE 19 #define OPK_C_SET_INCOMING_STREAM_POLICY 20 #define OPK_C_SHUTDOWN_WAIT 21 #define OPK_C_EXPECT_CONN_CLOSE_INFO 22 #define OPK_S_EXPECT_CONN_CLOSE_INFO 23 #define OPK_S_BIND_STREAM_ID 24 #define OPK_C_WAIT_FOR_DATA 25 #define OPK_C_WRITE_FAIL 26 #define OPK_S_WRITE_FAIL 27 #define OPK_C_READ_FAIL 28 #define OPK_C_STREAM_RESET 29 #define OPK_S_ACCEPT_STREAM_WAIT 30 #define OPK_NEW_THREAD 31 #define OPK_BEGIN_REPEAT 32 #define OPK_END_REPEAT 33 #define OPK_S_UNBIND_STREAM_ID 34 #define OPK_C_READ_FAIL_WAIT 35 #define OPK_C_CLOSE_SOCKET 36 #define OPK_C_EXPECT_SSL_ERR 37 #define OPK_EXPECT_ERR_REASON 38 #define OPK_EXPECT_ERR_LIB 39 #define OPK_SLEEP 40 #define OPK_S_READ_FAIL 41 #define OPK_S_SET_INJECT_PLAIN 42 #define OPK_SET_INJECT_WORD 43 #define OPK_C_INHIBIT_TICK 44 #define OPK_C_SET_WRITE_BUF_SIZE 45 #define OPK_S_SET_INJECT_HANDSHAKE 46 #define OPK_S_NEW_TICKET 47 #define OPK_C_SKIP_IF_UNBOUND 48 #define OPK_S_SET_INJECT_DATAGRAM 49 #define OPK_S_SHUTDOWN 50 #define OPK_POP_ERR 51 #define OPK_C_WRITE_EX2 52 #define OPK_SKIP_IF_BLOCKING 53 #define EXPECT_CONN_CLOSE_APP (1U << 0) #define EXPECT_CONN_CLOSE_REMOTE (1U << 1) #define ALLOW_FAIL (1U << 16) #define C_BIDI_ID(ordinal) \ (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI) #define S_BIDI_ID(ordinal) \ (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI) #define C_UNI_ID(ordinal) \ (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI) #define S_UNI_ID(ordinal) \ (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI) #define ANY_ID UINT64_MAX #define OP_END \ {OPK_END} #define OP_CHECK(func, arg2) \ {OPK_CHECK, NULL, 0, (func), NULL, (arg2)}, #define OP_C_SET_ALPN(alpn) \ {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL}, #define OP_C_CONNECT_WAIT() \ {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL}, #define OP_C_CONNECT_WAIT_OR_FAIL() \ {OPK_C_CONNECT_WAIT, NULL, 1, NULL, NULL}, #define OP_C_WRITE(stream_name, buf, buf_len) \ {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name}, #define OP_S_WRITE(stream_name, buf, buf_len) \ {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name}, #define OP_C_READ_EXPECT(stream_name, buf, buf_len) \ {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name}, #define OP_S_READ_EXPECT(stream_name, buf, buf_len) \ {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name}, #define OP_C_EXPECT_FIN(stream_name) \ {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name}, #define OP_S_EXPECT_FIN(stream_name) \ {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name}, #define OP_C_CONCLUDE(stream_name) \ {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name}, #define OP_S_CONCLUDE(stream_name) \ {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name}, #define OP_C_DETACH(stream_name) \ {OPK_C_DETACH, NULL, 0, NULL, #stream_name}, #define OP_C_ATTACH(stream_name) \ {OPK_C_ATTACH, NULL, 0, NULL, #stream_name}, #define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \ {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)}, #define OP_C_NEW_STREAM_BIDI_EX(stream_name, expect_id, flags) \ {OPK_C_NEW_STREAM, NULL, (flags), NULL, #stream_name, (expect_id)}, #define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \ {OPK_C_NEW_STREAM, NULL, SSL_STREAM_FLAG_UNI, \ NULL, #stream_name, (expect_id)}, #define OP_C_NEW_STREAM_UNI_EX(stream_name, expect_id, flags) \ {OPK_C_NEW_STREAM, NULL, (flags) | SSL_STREAM_FLAG_UNI, \ NULL, #stream_name, (expect_id)}, #define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \ {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)}, #define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \ {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)}, #define OP_C_ACCEPT_STREAM_WAIT(stream_name) \ {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name}, #define OP_C_ACCEPT_STREAM_NONE() \ {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL}, #define OP_C_FREE_STREAM(stream_name) \ {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name}, #define OP_C_SET_DEFAULT_STREAM_MODE(mode) \ {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL}, #define OP_C_SET_INCOMING_STREAM_POLICY(policy) \ {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL}, #define OP_C_SHUTDOWN_WAIT(reason, flags) \ {OPK_C_SHUTDOWN_WAIT, (reason), (flags), NULL, NULL}, #define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \ {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL, \ ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \ ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \ NULL, NULL, (ec)}, #define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \ {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL, \ ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \ ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \ NULL, NULL, (ec)}, #define OP_S_BIND_STREAM_ID(stream_name, stream_id) \ {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)}, #define OP_C_WAIT_FOR_DATA(stream_name) \ {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name}, #define OP_C_WRITE_FAIL(stream_name) \ {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name}, #define OP_S_WRITE_FAIL(stream_name) \ {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name}, #define OP_C_READ_FAIL(stream_name) \ {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name}, #define OP_S_READ_FAIL(stream_name, allow_zero_len) \ {OPK_S_READ_FAIL, NULL, (allow_zero_len), NULL, #stream_name}, #define OP_C_STREAM_RESET(stream_name, aec) \ {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)}, #define OP_S_ACCEPT_STREAM_WAIT(stream_name) \ {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name}, #define OP_NEW_THREAD(num_threads, script) \ {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 }, #define OP_BEGIN_REPEAT(n) \ {OPK_BEGIN_REPEAT, NULL, (n)}, #define OP_END_REPEAT() \ {OPK_END_REPEAT}, #define OP_S_UNBIND_STREAM_ID(stream_name) \ {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name}, #define OP_C_READ_FAIL_WAIT(stream_name) \ {OPK_C_READ_FAIL_WAIT, NULL, 0, NULL, #stream_name}, #define OP_C_CLOSE_SOCKET() \ {OPK_C_CLOSE_SOCKET}, #define OP_C_EXPECT_SSL_ERR(stream_name, err) \ {OPK_C_EXPECT_SSL_ERR, NULL, (err), NULL, #stream_name}, #define OP_EXPECT_ERR_REASON(err) \ {OPK_EXPECT_ERR_REASON, NULL, (err)}, #define OP_EXPECT_ERR_LIB(lib) \ {OPK_EXPECT_ERR_LIB, NULL, (lib)}, #define OP_SLEEP(ms) \ {OPK_SLEEP, NULL, 0, NULL, NULL, (ms)}, #define OP_S_SET_INJECT_PLAIN(f) \ {OPK_S_SET_INJECT_PLAIN, NULL, 0, NULL, NULL, 0, (f)}, #define OP_SET_INJECT_WORD(w0, w1) \ {OPK_SET_INJECT_WORD, NULL, (w0), NULL, NULL, (w1), NULL}, #define OP_C_INHIBIT_TICK(inhibit) \ {OPK_C_INHIBIT_TICK, NULL, (inhibit), NULL, NULL, 0, NULL}, #define OP_C_SET_WRITE_BUF_SIZE(stream_name, size) \ {OPK_C_SET_WRITE_BUF_SIZE, NULL, (size), NULL, #stream_name}, #define OP_S_SET_INJECT_HANDSHAKE(f) \ {OPK_S_SET_INJECT_HANDSHAKE, NULL, 0, NULL, NULL, 0, NULL, (f)}, #define OP_S_NEW_TICKET() \ {OPK_S_NEW_TICKET}, #define OP_C_SKIP_IF_UNBOUND(stream_name, n) \ {OPK_C_SKIP_IF_UNBOUND, NULL, (n), NULL, #stream_name}, #define OP_S_SET_INJECT_DATAGRAM(f) \ {OPK_S_SET_INJECT_DATAGRAM, NULL, 0, NULL, NULL, 0, NULL, NULL, (f)}, #define OP_S_SHUTDOWN(error_code) \ {OPK_S_SHUTDOWN, NULL, (error_code)}, #define OP_POP_ERR() \ {OPK_POP_ERR}, #define OP_C_WRITE_EX2(stream_name, buf, buf_len, flags) \ {OPK_C_WRITE_EX2, (buf), (buf_len), NULL, #stream_name, (flags)}, #define OP_CHECK2(func, arg1, arg2) \ {OPK_CHECK, NULL, (arg1), (func), NULL, (arg2)}, #define OP_SKIP_IF_BLOCKING(n) \ {OPK_SKIP_IF_BLOCKING, NULL, (n), NULL, 0}, static OSSL_TIME get_time(void *arg) { struct helper *h = arg; OSSL_TIME t; if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock))) return ossl_time_zero(); t = ossl_time_add(ossl_time_now(), h->time_slip); CRYPTO_THREAD_unlock(h->time_lock); return t; } static int skip_time_ms(struct helper *h, struct helper_local *hl) { if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock))) return 0; h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(hl->check_op->arg2)); CRYPTO_THREAD_unlock(h->time_lock); return 1; } static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl); static void s_unlock(struct helper *h, struct helper_local *hl); #define ACQUIRE_S() s_lock(h, hl) #define ACQUIRE_S_NOHL() s_lock(h, NULL) static int check_rejected(struct helper *h, struct helper_local *hl) { uint64_t stream_id = hl->check_op->arg2; if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL) || !ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, NULL)) { h->check_spin_again = 1; return 0; } return 1; } static int check_stream_reset(struct helper *h, struct helper_local *hl) { uint64_t stream_id = hl->check_op->arg2, aec = 0; if (!ossl_quic_tserver_stream_has_peer_reset_stream(ACQUIRE_S(), stream_id, &aec)) { h->check_spin_again = 1; return 0; } return TEST_uint64_t_eq(aec, 42); } static int check_stream_stopped(struct helper *h, struct helper_local *hl) { uint64_t stream_id = hl->check_op->arg2; if (!ossl_quic_tserver_stream_has_peer_stop_sending(ACQUIRE_S(), stream_id, NULL)) { h->check_spin_again = 1; return 0; } return 1; } static int override_key_update(struct helper *h, struct helper_local *hl) { QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); ossl_quic_channel_set_txku_threshold_override(ch, hl->check_op->arg2); return 1; } static int trigger_key_update(struct helper *h, struct helper_local *hl) { if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED))) return 0; return 1; } static int check_key_update_ge(struct helper *h, struct helper_local *hl) { QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch); int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch); int64_t diff = txke - rxke; if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1)) return 0; if (!TEST_uint64_t_ge((uint64_t)rxke, hl->check_op->arg2)) return 0; return 1; } static int check_key_update_lt(struct helper *h, struct helper_local *hl) { QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch); if (!TEST_uint64_t_lt(txke, hl->check_op->arg2)) return 0; return 1; } static unsigned long stream_info_hash(const STREAM_INFO *info) { return OPENSSL_LH_strhash(info->name); } static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b) { return strcmp(a->name, b->name); } static void cleanup_stream(STREAM_INFO *info) { SSL_free(info->c_stream); OPENSSL_free(info); } static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh) { if (*lh == NULL) return; lh_STREAM_INFO_doall(*lh, cleanup_stream); lh_STREAM_INFO_free(*lh); *lh = NULL; } #if defined(OPENSSL_THREADS) static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg); static int join_threads(struct child_thread_args *threads, size_t num_threads) { int ok = 1; size_t i; CRYPTO_THREAD_RETVAL rv; for (i = 0; i < num_threads; ++i) { if (threads[i].t != NULL) { ossl_crypto_thread_native_join(threads[i].t, &rv); if (!threads[i].testresult) ok = 0; ossl_crypto_thread_native_clean(threads[i].t); threads[i].t = NULL; } ossl_crypto_mutex_free(&threads[i].m); } return ok; } static int join_server_thread(struct helper *h) { CRYPTO_THREAD_RETVAL rv; if (h->server_thread.t == NULL) return 1; ossl_crypto_mutex_lock(h->server_thread.m); h->server_thread.stop = 1; ossl_crypto_condvar_signal(h->server_thread.c); ossl_crypto_mutex_unlock(h->server_thread.m); ossl_crypto_thread_native_join(h->server_thread.t, &rv); ossl_crypto_thread_native_clean(h->server_thread.t); h->server_thread.t = NULL; return 1; } static int *s_checked_out_p(struct helper *h, int thread_idx) { return (thread_idx < 0) ? &h->s_checked_out : &h->threads[thread_idx].s_checked_out; } static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl) { int *p_checked_out = s_checked_out_p(h, hl == NULL ? -1 : hl->thread_idx); if (h->server_thread.m == NULL || *p_checked_out) return h->s; ossl_crypto_mutex_lock(h->server_thread.m); h->s = h->s_priv; *p_checked_out = 1; return h->s; } static void s_unlock(struct helper *h, struct helper_local *hl) { int *p_checked_out = s_checked_out_p(h, hl->thread_idx); if (h->server_thread.m == NULL || !*p_checked_out) return; *p_checked_out = 0; h->s = NULL; ossl_crypto_mutex_unlock(h->server_thread.m); } static unsigned int server_helper_thread(void *arg) { struct helper *h = arg; ossl_crypto_mutex_lock(h->server_thread.m); for (;;) { int ready, stop; ready = h->server_thread.ready; stop = h->server_thread.stop; if (stop) break; if (!ready) { ossl_crypto_condvar_wait(h->server_thread.c, h->server_thread.m); continue; } ossl_quic_tserver_tick(h->s_priv); ossl_crypto_mutex_unlock(h->server_thread.m); OSSL_sleep(1); ossl_crypto_mutex_lock(h->server_thread.m); } ossl_crypto_mutex_unlock(h->server_thread.m); return 1; } #else static QUIC_TSERVER *s_lock(struct helper *h, struct helper_local *hl) { return h->s; } static void s_unlock(struct helper *h, struct helper_local *hl) {} #endif static void helper_cleanup(struct helper *h) { #if defined(OPENSSL_THREADS) join_threads(h->threads, h->num_threads); join_server_thread(h); OPENSSL_free(h->threads); h->threads = NULL; h->num_threads = 0; #endif if (h->free_order == 0) { helper_cleanup_streams(&h->c_streams); SSL_free(h->c_conn); h->c_conn = NULL; } else { SSL_free(h->c_conn); h->c_conn = NULL; helper_cleanup_streams(&h->c_streams); } helper_cleanup_streams(&h->s_streams); ossl_quic_tserver_free(h->s_priv); h->s_priv = h->s = NULL; BIO_free(h->s_net_bio_own); h->s_net_bio_own = NULL; BIO_free(h->c_net_bio_own); h->c_net_bio_own = NULL; BIO_free(h->s_qtf_wbio_own); h->s_qtf_wbio_own = NULL; qtest_fault_free(h->qtf); h->qtf = NULL; if (h->s_fd >= 0) { BIO_closesocket(h->s_fd); h->s_fd = -1; } if (h->c_fd >= 0) { BIO_closesocket(h->c_fd); h->c_fd = -1; } BIO_ADDR_free(h->s_net_bio_addr); h->s_net_bio_addr = NULL; BIO_ADDR_free(h->s_net_bio_orig_addr); h->s_net_bio_orig_addr = NULL; SSL_CTX_free(h->c_ctx); h->c_ctx = NULL; CRYPTO_THREAD_lock_free(h->time_lock); h->time_lock = NULL; #if defined(OPENSSL_THREADS) ossl_crypto_mutex_free(&h->misc_m); ossl_crypto_condvar_free(&h->misc_cv); ossl_crypto_mutex_free(&h->server_thread.m); ossl_crypto_condvar_free(&h->server_thread.c); #endif } static int helper_init(struct helper *h, const char *script_name, int free_order, int blocking, int need_injector) { struct in_addr ina = {0}; QUIC_TSERVER_ARGS s_args = {0}; union BIO_sock_info_u info; char title[128]; memset(h, 0, sizeof(*h)); h->c_fd = -1; h->s_fd = -1; h->free_order = free_order; h->blocking = blocking; h->need_injector = need_injector; h->time_slip = ossl_time_zero(); if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new())) goto err; if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash, stream_info_cmp))) goto err; if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash, stream_info_cmp))) goto err; ina.s_addr = htonl(0x7f000001UL); h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); if (!TEST_int_ge(h->s_fd, 0)) goto err; if (!TEST_true(BIO_socket_nbio(h->s_fd, 1))) goto err; if (!TEST_ptr(h->s_net_bio_orig_addr = BIO_ADDR_new()) || !TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new())) goto err; if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_orig_addr, AF_INET, &ina, sizeof(ina), 0))) goto err; if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_orig_addr, 0))) goto err; info.addr = h->s_net_bio_addr; if (!TEST_true(BIO_sock_info(h->s_fd, BIO_SOCK_INFO_ADDRESS, &info))) goto err; if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0)) goto err; if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0))) goto err; if (!BIO_up_ref(h->s_net_bio)) goto err; if (need_injector) { h->s_qtf_wbio = h->s_qtf_wbio_own = BIO_new(qtest_get_bio_method()); if (!TEST_ptr(h->s_qtf_wbio)) goto err; if (!TEST_ptr(BIO_push(h->s_qtf_wbio, h->s_net_bio))) goto err; s_args.net_wbio = h->s_qtf_wbio; } else { s_args.net_wbio = h->s_net_bio; } s_args.net_rbio = h->s_net_bio; s_args.alpn = NULL; s_args.now_cb = get_time; s_args.now_cb_arg = h; s_args.ctx = NULL; if (!TEST_ptr(h->s_priv = ossl_quic_tserver_new(&s_args, certfile, keyfile))) goto err; if (!blocking) h->s = h->s_priv; if (need_injector) { h->qtf = qtest_create_injector(h->s_priv); if (!TEST_ptr(h->qtf)) goto err; BIO_set_data(h->s_qtf_wbio, h->qtf); } h->s_net_bio_own = NULL; h->s_qtf_wbio_own = NULL; h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); if (!TEST_int_ge(h->c_fd, 0)) goto err; if (!TEST_true(BIO_socket_nbio(h->c_fd, 1))) goto err; if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0))) goto err; if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr))) goto err; if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method()))) goto err; snprintf(title, sizeof(title), "quic_multistream_test: %s", script_name); if (!TEST_true(ossl_quic_set_diag_title(h->c_ctx, title))) goto err; if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx))) goto err; if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h))) goto err; SSL_set0_rbio(h->c_conn, h->c_net_bio); h->c_net_bio_own = NULL; if (!TEST_true(BIO_up_ref(h->c_net_bio))) goto err; SSL_set0_wbio(h->c_conn, h->c_net_bio); if (!TEST_true(SSL_set_blocking_mode(h->c_conn, h->blocking))) goto err; #if defined(OPENSSL_THREADS) if (!TEST_ptr(h->misc_m = ossl_crypto_mutex_new())) goto err; if (!TEST_ptr(h->misc_cv = ossl_crypto_condvar_new())) goto err; #endif if (h->blocking) { #if defined(OPENSSL_THREADS) if (!TEST_ptr(h->server_thread.m = ossl_crypto_mutex_new())) goto err; if (!TEST_ptr(h->server_thread.c = ossl_crypto_condvar_new())) goto err; h->server_thread.t = ossl_crypto_thread_native_start(server_helper_thread, h, 1); if (!TEST_ptr(h->server_thread.t)) goto err; #else TEST_error("cannot support blocking mode without threads"); goto err; #endif } h->start_time = ossl_time_now(); h->init = 1; return 1; err: helper_cleanup(h); return 0; } static int helper_local_init(struct helper_local *hl, struct helper *h, int thread_idx) { hl->h = h; hl->c_streams = NULL; hl->thread_idx = thread_idx; hl->explicit_event_handling = 0; if (!TEST_ptr(h)) return 0; if (thread_idx < 0) { hl->c_streams = h->c_streams; } else { if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash, stream_info_cmp))) return 0; } return 1; } static void helper_local_cleanup(struct helper_local *hl) { if (hl->h == NULL) return; if (hl->thread_idx >= 0) helper_cleanup_streams(&hl->c_streams); hl->h = NULL; } static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh, const char *stream_name) { STREAM_INFO key, *info; if (!TEST_ptr(stream_name)) return NULL; if (!strcmp(stream_name, "DEFAULT")) return NULL; key.name = stream_name; info = lh_STREAM_INFO_retrieve(lh, &key); if (info == NULL) { info = OPENSSL_zalloc(sizeof(*info)); if (info == NULL) return NULL; info->name = stream_name; info->s_stream_id = UINT64_MAX; lh_STREAM_INFO_insert(lh, info); } return info; } static int helper_local_set_c_stream(struct helper_local *hl, const char *stream_name, SSL *c_stream) { STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name); if (info == NULL) return 0; info->c_stream = c_stream; info->s_stream_id = UINT64_MAX; return 1; } static SSL *helper_local_get_c_stream(struct helper_local *hl, const char *stream_name) { STREAM_INFO *info; if (!strcmp(stream_name, "DEFAULT")) return hl->h->c_conn; info = get_stream_info(hl->c_streams, stream_name); if (info == NULL) return NULL; return info->c_stream; } static int helper_set_s_stream(struct helper *h, const char *stream_name, uint64_t s_stream_id) { STREAM_INFO *info; if (!strcmp(stream_name, "DEFAULT")) return 0; info = get_stream_info(h->s_streams, stream_name); if (info == NULL) return 0; info->c_stream = NULL; info->s_stream_id = s_stream_id; return 1; } static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name) { STREAM_INFO *info; if (!strcmp(stream_name, "DEFAULT")) return UINT64_MAX; info = get_stream_info(h->s_streams, stream_name); if (info == NULL) return UINT64_MAX; return info->s_stream_id; } static int helper_packet_plain_listener(QTEST_FAULT *qtf, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t buf_len, void *arg) { struct helper *h = arg; return h->qtf_packet_plain_cb(h, hdr, buf, buf_len); } static int helper_handshake_listener(QTEST_FAULT *fault, unsigned char *buf, size_t buf_len, void *arg) { struct helper *h = arg; return h->qtf_handshake_cb(h, buf, buf_len); } static int helper_datagram_listener(QTEST_FAULT *fault, BIO_MSG *msg, size_t stride, void *arg) { struct helper *h = arg; return h->qtf_datagram_cb(h, msg, stride); } static int is_want(SSL *s, int ret) { int ec = SSL_get_error(s, ret); return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE; } static int check_consistent_want(SSL *s, int ret) { int ec = SSL_get_error(s, ret); int w = SSL_want(s); int ok = TEST_true( (ec == SSL_ERROR_NONE && w == SSL_NOTHING) || (ec == SSL_ERROR_ZERO_RETURN && w == SSL_NOTHING) || (ec == SSL_ERROR_SSL && w == SSL_NOTHING) || (ec == SSL_ERROR_SYSCALL && w == SSL_NOTHING) || (ec == SSL_ERROR_WANT_READ && w == SSL_READING) || (ec == SSL_ERROR_WANT_WRITE && w == SSL_WRITING) || (ec == SSL_ERROR_WANT_CLIENT_HELLO_CB && w == SSL_CLIENT_HELLO_CB) || (ec == SSL_ERROR_WANT_X509_LOOKUP && w == SSL_X509_LOOKUP) || (ec == SSL_ERROR_WANT_RETRY_VERIFY && w == SSL_RETRY_VERIFY) ); if (!ok) TEST_error("got error=%d, want=%d", ec, w); return ok; } static int run_script_worker(struct helper *h, const struct script_op *script, const char *script_name, int thread_idx) { int testresult = 0; unsigned char *tmp_buf = NULL; int connect_started = 0; size_t offset = 0; size_t op_idx = 0; const struct script_op *op = NULL; int no_advance = 0, first = 1; #if defined(OPENSSL_THREADS) int end_wait_warning = 0; #endif OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero(); struct helper_local hl_, *hl = &hl_; #define REPEAT_SLOTS 8 size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS]; size_t repeat_stack_limit[REPEAT_SLOTS]; size_t repeat_stack_len = 0; if (!TEST_true(helper_local_init(hl, h, thread_idx))) goto out; #define COMMON_SPIN_AGAIN() \ { \ no_advance = 1; \ continue; \ } #define S_SPIN_AGAIN() \ { \ s_lock(h, hl); \ ossl_quic_tserver_tick(h->s); \ COMMON_SPIN_AGAIN(); \ } #define C_SPIN_AGAIN() \ { \ if (h->blocking) { \ TEST_error("spin again in blocking mode"); \ goto out; \ } \ COMMON_SPIN_AGAIN(); \ } for (;;) { SSL *c_tgt = h->c_conn; uint64_t s_stream_id = UINT64_MAX; s_unlock(h, hl); if (no_advance) { no_advance = 0; } else { if (!first) ++op_idx; first = 0; offset = 0; op_start_time = ossl_time_now(); op_deadline = ossl_time_add(op_start_time, ossl_ms2time(60000)); } if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) { TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx); goto out; } op = &script[op_idx]; if (op->stream_name != NULL) { c_tgt = helper_local_get_c_stream(hl, op->stream_name); if (thread_idx < 0) s_stream_id = helper_get_s_stream(h, op->stream_name); else s_stream_id = UINT64_MAX; } if (thread_idx < 0) { if (!h->blocking) { ossl_quic_tserver_tick(h->s); } #if defined(OPENSSL_THREADS) else if (h->blocking && !h->server_thread.ready) { ossl_crypto_mutex_lock(h->server_thread.m); h->server_thread.ready = 1; ossl_crypto_condvar_signal(h->server_thread.c); ossl_crypto_mutex_unlock(h->server_thread.m); } if (h->blocking) assert(h->s == NULL); #endif } if (!hl->explicit_event_handling && (thread_idx >= 0 || connect_started)) SSL_handle_events(h->c_conn); if (thread_idx >= 0) { switch (op->op) { case OPK_END: case OPK_CHECK: case OPK_C_ACCEPT_STREAM_WAIT: case OPK_C_NEW_STREAM: case OPK_C_READ_EXPECT: case OPK_C_EXPECT_FIN: case OPK_C_WRITE: case OPK_C_WRITE_EX2: case OPK_C_CONCLUDE: case OPK_C_FREE_STREAM: case OPK_BEGIN_REPEAT: case OPK_END_REPEAT: case OPK_C_READ_FAIL_WAIT: case OPK_C_EXPECT_SSL_ERR: case OPK_EXPECT_ERR_REASON: case OPK_EXPECT_ERR_LIB: case OPK_POP_ERR: case OPK_SLEEP: break; default: TEST_error("opcode %lu not allowed on child thread", (unsigned long)op->op); goto out; } } switch (op->op) { case OPK_END: if (!TEST_size_t_eq(repeat_stack_len, 0)) goto out; #if defined(OPENSSL_THREADS) if (thread_idx < 0) { int done; size_t i; for (i = 0; i < h->num_threads; ++i) { if (h->threads[i].m == NULL) continue; ossl_crypto_mutex_lock(h->threads[i].m); done = h->threads[i].done; ossl_crypto_mutex_unlock(h->threads[i].m); if (!done) { if (!end_wait_warning) { TEST_info("still waiting for other threads to finish (%zu)", i); end_wait_warning = 1; } S_SPIN_AGAIN(); } } } #endif TEST_info("script \"%s\" finished on thread %d", script_name, thread_idx); testresult = 1; goto out; case OPK_BEGIN_REPEAT: if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx))) goto out; if (!TEST_size_t_gt(op->arg1, 0)) goto out; repeat_stack_idx[repeat_stack_len] = op_idx + 1; repeat_stack_done[repeat_stack_len] = 0; repeat_stack_limit[repeat_stack_len] = op->arg1; ++repeat_stack_len; break; case OPK_C_SKIP_IF_UNBOUND: if (c_tgt != NULL) break; op_idx += op->arg1; break; case OPK_SKIP_IF_BLOCKING: if (!h->blocking) break; op_idx += op->arg1; break; case OPK_END_REPEAT: if (!TEST_size_t_gt(repeat_stack_len, 0)) goto out; if (++repeat_stack_done[repeat_stack_len - 1] == repeat_stack_limit[repeat_stack_len - 1]) { --repeat_stack_len; } else { op_idx = repeat_stack_idx[repeat_stack_len - 1]; no_advance = 1; continue; } break; case OPK_CHECK: { int ok; hl->check_op = op; ok = op->check_func(h, hl); hl->check_op = NULL; if (thread_idx < 0 && h->check_spin_again) { h->check_spin_again = 0; S_SPIN_AGAIN(); } if (!TEST_true(ok)) goto out; } break; case OPK_C_SET_ALPN: { const char *alpn = op->arg0; size_t alpn_len = strlen(alpn); if (!TEST_size_t_le(alpn_len, UINT8_MAX) || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1))) goto out; memcpy(tmp_buf + 1, alpn, alpn_len); tmp_buf[0] = (unsigned char)alpn_len; if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf, alpn_len + 1))) goto out; OPENSSL_free(tmp_buf); tmp_buf = NULL; } break; case OPK_C_CONNECT_WAIT: { int ret; connect_started = 1; ret = SSL_connect(h->c_conn); if (!check_consistent_want(c_tgt, ret)) goto out; if (ret != 1) { if (!h->blocking && is_want(h->c_conn, ret)) C_SPIN_AGAIN(); if (op->arg1 == 0 && !TEST_int_eq(ret, 1)) goto out; } } break; case OPK_C_WRITE: { size_t bytes_written = 0; int r; if (!TEST_ptr(c_tgt)) goto out; r = SSL_write_ex(c_tgt, op->arg0, op->arg1, &bytes_written); if (!TEST_true(r) || !check_consistent_want(c_tgt, r) || !TEST_size_t_eq(bytes_written, op->arg1)) goto out; } break; case OPK_C_WRITE_EX2: { size_t bytes_written = 0; int r; if (!TEST_ptr(c_tgt)) goto out; r = SSL_write_ex2(c_tgt, op->arg0, op->arg1, op->arg2, &bytes_written); if (!TEST_true(r) || !check_consistent_want(c_tgt, r) || !TEST_size_t_eq(bytes_written, op->arg1)) goto out; } break; case OPK_S_WRITE: { size_t bytes_written = 0; if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; if (!TEST_true(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id, op->arg0, op->arg1, &bytes_written)) || !TEST_size_t_eq(bytes_written, op->arg1)) goto out; } break; case OPK_C_CONCLUDE: { if (!TEST_true(SSL_stream_conclude(c_tgt, 0))) goto out; } break; case OPK_S_CONCLUDE: { if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; ossl_quic_tserver_conclude(ACQUIRE_S(), s_stream_id); } break; case OPK_C_WAIT_FOR_DATA: { char buf[1]; size_t bytes_read = 0; if (!TEST_ptr(c_tgt)) goto out; if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read) || bytes_read == 0) C_SPIN_AGAIN(); } break; case OPK_C_READ_EXPECT: { size_t bytes_read = 0; int r; if (op->arg1 > 0 && tmp_buf == NULL && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1))) goto out; r = SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset, &bytes_read); if (!check_consistent_want(c_tgt, r)) goto out; if (!r) C_SPIN_AGAIN(); if (bytes_read + offset != op->arg1) { offset += bytes_read; C_SPIN_AGAIN(); } if (op->arg1 > 0 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1)) goto out; OPENSSL_free(tmp_buf); tmp_buf = NULL; } break; case OPK_S_READ_EXPECT: { size_t bytes_read = 0; if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; if (op->arg1 > 0 && tmp_buf == NULL && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1))) goto out; if (!TEST_true(ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id, tmp_buf + offset, op->arg1 - offset, &bytes_read))) goto out; if (bytes_read + offset != op->arg1) { offset += bytes_read; S_SPIN_AGAIN(); } if (op->arg1 > 0 && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1)) goto out; OPENSSL_free(tmp_buf); tmp_buf = NULL; } break; case OPK_C_EXPECT_FIN: { char buf[1]; size_t bytes_read = 0; int r; r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read); if (!check_consistent_want(c_tgt, r) || !TEST_false(r) || !TEST_size_t_eq(bytes_read, 0)) goto out; if (is_want(c_tgt, 0)) C_SPIN_AGAIN(); if (!TEST_int_eq(SSL_get_error(c_tgt, 0), SSL_ERROR_ZERO_RETURN)) goto out; if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING)) goto out; } break; case OPK_S_EXPECT_FIN: { if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; if (!ossl_quic_tserver_has_read_ended(ACQUIRE_S(), s_stream_id)) S_SPIN_AGAIN(); } break; case OPK_C_DETACH: { SSL *c_stream; if (!TEST_ptr_null(c_tgt)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn))) goto out; if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream))) goto out; } break; case OPK_C_ATTACH: { if (!TEST_ptr(c_tgt)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt))) goto out; if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL))) goto out; } break; case OPK_C_NEW_STREAM: { SSL *c_stream; uint64_t flags = op->arg1; int allow_fail = ((flags & ALLOW_FAIL) != 0); flags &= ~(uint64_t)ALLOW_FAIL; if (!TEST_ptr_null(c_tgt)) goto out; if (!TEST_ptr(op->stream_name)) goto out; c_stream = SSL_new_stream(h->c_conn, flags); if (!allow_fail && !TEST_ptr(c_stream)) goto out; if (allow_fail && c_stream == NULL) { if (!TEST_size_t_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_STREAM_COUNT_LIMITED)) goto out; ++h->fail_count; break; } if (op->arg2 != UINT64_MAX && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream), op->arg2)) goto out; if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream))) goto out; } break; case OPK_S_NEW_STREAM: { uint64_t stream_id = UINT64_MAX; if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_true(ossl_quic_tserver_stream_new(ACQUIRE_S(), op->arg1 > 0, &stream_id))) goto out; if (op->arg2 != UINT64_MAX && !TEST_uint64_t_eq(stream_id, op->arg2)) goto out; if (!TEST_true(helper_set_s_stream(h, op->stream_name, stream_id))) goto out; } break; case OPK_C_ACCEPT_STREAM_WAIT: { SSL *c_stream; if (!TEST_ptr_null(c_tgt)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL) C_SPIN_AGAIN(); if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, c_stream))) goto out; } break; case OPK_S_ACCEPT_STREAM_WAIT: { uint64_t new_stream_id; if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) goto out; if (!TEST_ptr(op->stream_name)) goto out; new_stream_id = ossl_quic_tserver_pop_incoming_stream(ACQUIRE_S()); if (new_stream_id == UINT64_MAX) S_SPIN_AGAIN(); if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id))) goto out; } break; case OPK_C_ACCEPT_STREAM_NONE: { SSL *c_stream; if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn, SSL_ACCEPT_STREAM_NO_BLOCK))) { SSL_free(c_stream); goto out; } } break; case OPK_C_FREE_STREAM: { if (!TEST_ptr(c_tgt) || !TEST_true(!SSL_is_connection(c_tgt))) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_true(helper_local_set_c_stream(hl, op->stream_name, NULL))) goto out; SSL_free(c_tgt); c_tgt = NULL; } break; case OPK_C_SET_DEFAULT_STREAM_MODE: { if (!TEST_ptr(c_tgt)) goto out; if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1))) goto out; } break; case OPK_C_SET_INCOMING_STREAM_POLICY: { if (!TEST_ptr(c_tgt)) goto out; if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt, op->arg1, 0))) goto out; } break; case OPK_C_SHUTDOWN_WAIT: { int ret; QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); SSL_SHUTDOWN_EX_ARGS args = {0}; ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch), 0); if (!TEST_ptr(c_tgt)) goto out; args.quic_reason = (const char *)op->arg0; ret = SSL_shutdown_ex(c_tgt, op->arg1, &args, sizeof(args)); if (!TEST_int_ge(ret, 0)) goto out; if (ret == 0) C_SPIN_AGAIN(); } break; case OPK_S_SHUTDOWN: { ossl_quic_tserver_shutdown(ACQUIRE_S(), op->arg1); } break; case OPK_C_EXPECT_CONN_CLOSE_INFO: { SSL_CONN_CLOSE_INFO cc_info = {0}; int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0; int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0; uint64_t error_code = op->arg2; if (!TEST_ptr(c_tgt)) goto out; if (h->blocking && !TEST_true(SSL_shutdown_ex(c_tgt, SSL_SHUTDOWN_FLAG_WAIT_PEER, NULL, 0))) goto out; if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info))) C_SPIN_AGAIN(); if (!TEST_int_eq(expect_app, (cc_info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) == 0) || !TEST_int_eq(expect_remote, (cc_info.flags & SSL_CONN_CLOSE_FLAG_LOCAL) == 0) || !TEST_uint64_t_eq(error_code, cc_info.error_code)) { TEST_info("Connection close reason: %s", cc_info.reason); goto out; } } break; case OPK_S_EXPECT_CONN_CLOSE_INFO: { const QUIC_TERMINATE_CAUSE *tc; int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0; int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0; uint64_t error_code = op->arg2; if (!ossl_quic_tserver_is_term_any(ACQUIRE_S())) { ossl_quic_tserver_ping(ACQUIRE_S()); S_SPIN_AGAIN(); } if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S()))) goto out; if (!TEST_uint64_t_eq(error_code, tc->error_code) || !TEST_int_eq(expect_app, tc->app) || !TEST_int_eq(expect_remote, tc->remote)) goto out; } break; case OPK_S_BIND_STREAM_ID: { if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2))) goto out; } break; case OPK_S_UNBIND_STREAM_ID: { if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; if (!TEST_ptr(op->stream_name)) goto out; if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX))) goto out; } break; case OPK_C_WRITE_FAIL: { size_t bytes_written = 0; int r; if (!TEST_ptr(c_tgt)) goto out; r = SSL_write_ex(c_tgt, "apple", 5, &bytes_written); if (!TEST_false(r) || !check_consistent_want(c_tgt, r)) goto out; } break; case OPK_S_WRITE_FAIL: { size_t bytes_written = 0; if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; if (!TEST_false(ossl_quic_tserver_write(ACQUIRE_S(), s_stream_id, (const unsigned char *)"apple", 5, &bytes_written))) goto out; } break; case OPK_C_READ_FAIL: { size_t bytes_read = 0; char buf[1]; int r; if (!TEST_ptr(c_tgt)) goto out; r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read); if (!TEST_false(r)) goto out; if (!check_consistent_want(c_tgt, r)) goto out; } break; case OPK_C_READ_FAIL_WAIT: { size_t bytes_read = 0; char buf[1]; int r; if (!TEST_ptr(c_tgt)) goto out; r = SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read); if (!TEST_false(r)) goto out; if (!check_consistent_want(c_tgt, r)) goto out; if (is_want(c_tgt, 0)) C_SPIN_AGAIN(); } break; case OPK_S_READ_FAIL: { int ret; size_t bytes_read = 0; unsigned char buf[1]; if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) goto out; ret = ossl_quic_tserver_read(ACQUIRE_S(), s_stream_id, buf, sizeof(buf), &bytes_read); if (!TEST_true(ret == 0 || (op->arg1 && bytes_read == 0))) goto out; } break; case OPK_C_STREAM_RESET: { SSL_STREAM_RESET_ARGS args = {0}; if (!TEST_ptr(c_tgt)) goto out; args.quic_error_code = op->arg2; if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args)))) goto out; } break; case OPK_NEW_THREAD: { #if !defined(OPENSSL_THREADS) TEST_skip("threading not supported, skipping"); testresult = 1; goto out; #else size_t i; if (!TEST_ptr_null(h->threads)) { TEST_error("max one NEW_THREAD operation per script"); goto out; } h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args)); if (!TEST_ptr(h->threads)) goto out; h->num_threads = op->arg1; for (i = 0; i < op->arg1; ++i) { h->threads[i].h = h; h->threads[i].script = op->arg0; h->threads[i].script_name = script_name; h->threads[i].thread_idx = i; h->threads[i].m = ossl_crypto_mutex_new(); if (!TEST_ptr(h->threads[i].m)) goto out; h->threads[i].t = ossl_crypto_thread_native_start(run_script_child_thread, &h->threads[i], 1); if (!TEST_ptr(h->threads[i].t)) goto out; } #endif } break; case OPK_C_CLOSE_SOCKET: { BIO_closesocket(h->c_fd); h->c_fd = -1; } break; case OPK_C_EXPECT_SSL_ERR: { if (!TEST_size_t_eq((size_t)SSL_get_error(c_tgt, 0), op->arg1)) goto out; if (!TEST_int_eq(SSL_want(c_tgt), SSL_NOTHING)) goto out; } break; case OPK_EXPECT_ERR_REASON: { if (!TEST_size_t_eq((size_t)ERR_GET_REASON(ERR_peek_last_error()), op->arg1)) goto out; } break; case OPK_EXPECT_ERR_LIB: { if (!TEST_size_t_eq((size_t)ERR_GET_LIB(ERR_peek_last_error()), op->arg1)) goto out; } break; case OPK_POP_ERR: ERR_pop(); break; case OPK_SLEEP: { OSSL_sleep(op->arg2); } break; case OPK_S_SET_INJECT_PLAIN: h->qtf_packet_plain_cb = op->qtf_packet_plain_cb; if (!TEST_true(qtest_fault_set_packet_plain_listener(h->qtf, h->qtf_packet_plain_cb != NULL ? helper_packet_plain_listener : NULL, h))) goto out; break; case OPK_S_SET_INJECT_HANDSHAKE: h->qtf_handshake_cb = op->qtf_handshake_cb; if (!TEST_true(qtest_fault_set_handshake_listener(h->qtf, h->qtf_handshake_cb != NULL ? helper_handshake_listener : NULL, h))) goto out; break; case OPK_S_SET_INJECT_DATAGRAM: h->qtf_datagram_cb = op->qtf_datagram_cb; if (!TEST_true(qtest_fault_set_datagram_listener(h->qtf, h->qtf_datagram_cb != NULL ? helper_datagram_listener : NULL, h))) goto out; break; case OPK_SET_INJECT_WORD: ACQUIRE_S(); h->inject_word0 = op->arg1; h->inject_word1 = op->arg2; break; case OPK_C_INHIBIT_TICK: { QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); ossl_quic_engine_set_inhibit_tick(ossl_quic_channel_get0_engine(ch), op->arg1); } break; case OPK_C_SET_WRITE_BUF_SIZE: if (!TEST_ptr(c_tgt)) goto out; if (!TEST_true(ossl_quic_set_write_buffer_size(c_tgt, op->arg1))) goto out; break; case OPK_S_NEW_TICKET: if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S()))) goto out; break; default: TEST_error("unknown op"); goto out; } } out: s_unlock(h, hl); if (!testresult) { size_t i; const QUIC_TERMINATE_CAUSE *tcause; const char *e_str, *f_str; TEST_error("failed in script \"%s\" at op %zu, thread %d\n", script_name, op_idx + 1, thread_idx); for (i = 0; i < repeat_stack_len; ++i) TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu", repeat_stack_done[i], repeat_stack_limit[i], repeat_stack_idx[i]); ERR_print_errors_fp(stderr); if (h->c_conn != NULL) { SSL_CONN_CLOSE_INFO cc_info = {0}; if (SSL_get_conn_close_info(h->c_conn, &cc_info, sizeof(cc_info))) { e_str = ossl_quic_err_to_string(cc_info.error_code); f_str = ossl_quic_frame_type_to_string(cc_info.frame_type); if (e_str == NULL) e_str = "?"; if (f_str == NULL) f_str = "?"; TEST_info("client side is closed: %llu(%s)/%llu(%s), " "%s, %s, reason: \"%s\"", (unsigned long long)cc_info.error_code, e_str, (unsigned long long)cc_info.frame_type, f_str, (cc_info.flags & SSL_CONN_CLOSE_FLAG_LOCAL) != 0 ? "local" : "remote", (cc_info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0 ? "transport" : "app", cc_info.reason != NULL ? cc_info.reason : "-"); } } tcause = (h->s != NULL ? ossl_quic_tserver_get_terminate_cause(h->s) : NULL); if (tcause != NULL) { e_str = ossl_quic_err_to_string(tcause->error_code); f_str = ossl_quic_frame_type_to_string(tcause->frame_type); if (e_str == NULL) e_str = "?"; if (f_str == NULL) f_str = "?"; TEST_info("server side is closed: %llu(%s)/%llu(%s), " "%s, %s, reason: \"%s\"", (unsigned long long)tcause->error_code, e_str, (unsigned long long)tcause->frame_type, f_str, tcause->remote ? "remote" : "local", tcause->app ? "app" : "transport", tcause->reason != NULL ? tcause->reason : "-"); } } OPENSSL_free(tmp_buf); helper_local_cleanup(hl); return testresult; } static int run_script(const struct script_op *script, const char *script_name, int free_order, int blocking) { int testresult = 0; struct helper h; if (!TEST_true(helper_init(&h, script_name, free_order, blocking, 1))) goto out; if (!TEST_true(run_script_worker(&h, script, script_name, -1))) goto out; #if defined(OPENSSL_THREADS) if (!TEST_true(join_threads(h.threads, h.num_threads))) goto out; #endif testresult = 1; out: helper_cleanup(&h); return testresult; } #if defined(OPENSSL_THREADS) static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg) { int testresult; struct child_thread_args *args = arg; testresult = run_script_worker(args->h, args->script, args->script_name, args->thread_idx); ossl_crypto_mutex_lock(args->m); args->testresult = testresult; args->done = 1; ossl_crypto_mutex_unlock(args->m); return 1; } #endif static const struct script_op script_1[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_C_CONCLUDE (DEFAULT) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_EXPECT_FIN (a) OP_S_WRITE (a, "orange", 6) OP_S_CONCLUDE (a) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_EXPECT_FIN (DEFAULT) OP_END }; static const struct script_op script_2[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT) OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1)) OP_C_WRITE (b, "flamingo", 8) OP_C_CONCLUDE (b) OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) OP_S_READ_EXPECT (b, "flamingo", 8) OP_S_EXPECT_FIN (b) OP_S_WRITE (b, "gargoyle", 8) OP_S_CONCLUDE (b) OP_C_READ_EXPECT (b, "gargoyle", 8) OP_C_EXPECT_FIN (b) OP_C_NEW_STREAM_UNI (c, C_UNI_ID(0)) OP_C_WRITE (c, "elephant", 8) OP_C_CONCLUDE (c) OP_S_BIND_STREAM_ID (c, C_UNI_ID(0)) OP_S_READ_EXPECT (c, "elephant", 8) OP_S_EXPECT_FIN (c) OP_S_WRITE_FAIL (c) OP_C_ACCEPT_STREAM_NONE () OP_S_NEW_STREAM_BIDI (d, S_BIDI_ID(0)) OP_S_WRITE (d, "frog", 4) OP_S_CONCLUDE (d) OP_C_ACCEPT_STREAM_WAIT (d) OP_C_ACCEPT_STREAM_NONE () OP_C_READ_EXPECT (d, "frog", 4) OP_C_EXPECT_FIN (d) OP_S_NEW_STREAM_BIDI (e, S_BIDI_ID(1)) OP_S_WRITE (e, "mixture", 7) OP_S_CONCLUDE (e) OP_C_ACCEPT_STREAM_WAIT (e) OP_C_READ_EXPECT (e, "mixture", 7) OP_C_EXPECT_FIN (e) OP_C_WRITE (e, "ramble", 6) OP_S_READ_EXPECT (e, "ramble", 6) OP_C_CONCLUDE (e) OP_S_EXPECT_FIN (e) OP_S_NEW_STREAM_UNI (f, S_UNI_ID(0)) OP_S_WRITE (f, "yonder", 6) OP_S_CONCLUDE (f) OP_C_ACCEPT_STREAM_WAIT (f) OP_C_ACCEPT_STREAM_NONE () OP_C_READ_EXPECT (f, "yonder", 6) OP_C_EXPECT_FIN (f) OP_C_WRITE_FAIL (f) OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT) OP_S_NEW_STREAM_BIDI (g, S_BIDI_ID(2)) OP_S_WRITE (g, "unseen", 6) OP_S_CONCLUDE (g) OP_C_ACCEPT_STREAM_NONE () OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO) OP_S_NEW_STREAM_BIDI (h, S_BIDI_ID(3)) OP_S_WRITE (h, "UNSEEN", 6) OP_S_CONCLUDE (h) OP_C_ACCEPT_STREAM_NONE () OP_CHECK (check_rejected, S_BIDI_ID(2)) OP_CHECK (check_rejected, S_BIDI_ID(3)) OP_END }; static const struct script_op script_3[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_C_DETACH (a) OP_C_WRITE_FAIL (DEFAULT) OP_C_WRITE (a, "by", 2) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "appleby", 7) OP_S_WRITE (a, "hello", 5) OP_C_READ_EXPECT (a, "hello", 5) OP_C_WRITE_FAIL (DEFAULT) OP_C_ATTACH (a) OP_C_WRITE (DEFAULT, "is here", 7) OP_S_READ_EXPECT (a, "is here", 7) OP_C_DETACH (a) OP_C_CONCLUDE (a) OP_S_EXPECT_FIN (a) OP_END }; static const struct script_op script_4[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_WRITE_FAIL (DEFAULT) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_READ_FAIL (DEFAULT) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_C_ATTACH (a) OP_C_WRITE (DEFAULT, "orange", 6) OP_S_READ_EXPECT (a, "orange", 6) OP_END }; static const struct script_op script_5[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1)) OP_C_WRITE (a, "apple", 5) OP_C_STREAM_RESET (a, 42) OP_C_WRITE (b, "strawberry", 10) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) OP_S_READ_EXPECT (b, "strawberry", 10) OP_S_READ_FAIL (a, 0) OP_CHECK (check_stream_reset, C_BIDI_ID(0)) OP_END }; static const struct script_op script_6[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_FREE_STREAM (a) OP_C_ACCEPT_STREAM_NONE () OP_CHECK (check_stream_stopped, S_BIDI_ID(0)) OP_END }; static const struct script_op script_7[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_UNI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_WRITE_FAIL (a) OP_END }; static const struct script_op script_8[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_READ_EXPECT (DEFAULT, "apple", 5) OP_C_WRITE_FAIL (DEFAULT) OP_END }; static const struct script_op script_9[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_READ_EXPECT (DEFAULT, "apple", 5) OP_C_WRITE (DEFAULT, "orange", 6) OP_S_READ_EXPECT (a, "orange", 6) OP_END }; static const struct script_op script_10[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_C_SHUTDOWN_WAIT (NULL, 0) OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0) OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1) OP_END }; static const struct script_op script_11_child[] = { OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "foo", 3) OP_C_EXPECT_FIN (a) OP_END }; static const struct script_op script_11[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_NEW_THREAD (5, script_11_child) OP_S_NEW_STREAM_BIDI (a, ANY_ID) OP_S_WRITE (a, "foo", 3) OP_S_CONCLUDE (a) OP_S_NEW_STREAM_BIDI (b, ANY_ID) OP_S_WRITE (b, "foo", 3) OP_S_CONCLUDE (b) OP_S_NEW_STREAM_BIDI (c, ANY_ID) OP_S_WRITE (c, "foo", 3) OP_S_CONCLUDE (c) OP_S_NEW_STREAM_BIDI (d, ANY_ID) OP_S_WRITE (d, "foo", 3) OP_S_CONCLUDE (d) OP_S_NEW_STREAM_BIDI (e, ANY_ID) OP_S_WRITE (e, "foo", 3) OP_S_CONCLUDE (e) OP_END }; static const struct script_op script_12_child[] = { OP_C_NEW_STREAM_BIDI (a, ANY_ID) OP_C_WRITE (a, "foo", 3) OP_C_CONCLUDE (a) OP_C_FREE_STREAM (a) OP_END }; static const struct script_op script_12[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_NEW_THREAD (5, script_12_child) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "foo", 3) OP_S_EXPECT_FIN (a) OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) OP_S_READ_EXPECT (b, "foo", 3) OP_S_EXPECT_FIN (b) OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2)) OP_S_READ_EXPECT (c, "foo", 3) OP_S_EXPECT_FIN (c) OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3)) OP_S_READ_EXPECT (d, "foo", 3) OP_S_EXPECT_FIN (d) OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4)) OP_S_READ_EXPECT (e, "foo", 3) OP_S_EXPECT_FIN (e) OP_END }; static const struct script_op script_13_child[] = { OP_BEGIN_REPEAT (10) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "foo", 3) OP_C_EXPECT_FIN (a) OP_C_FREE_STREAM (a) OP_END_REPEAT () OP_END }; static const struct script_op script_13[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_NEW_THREAD (5, script_13_child) OP_BEGIN_REPEAT (50) OP_S_NEW_STREAM_BIDI (a, ANY_ID) OP_S_WRITE (a, "foo", 3) OP_S_CONCLUDE (a) OP_S_UNBIND_STREAM_ID (a) OP_END_REPEAT () OP_END }; static const struct script_op script_14_child[] = { OP_BEGIN_REPEAT (10) OP_C_NEW_STREAM_BIDI (a, ANY_ID) OP_C_WRITE (a, "foo", 3) OP_C_CONCLUDE (a) OP_C_FREE_STREAM (a) OP_END_REPEAT () OP_END }; static const struct script_op script_14[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_NEW_THREAD (5, script_14_child) OP_BEGIN_REPEAT (50) OP_S_ACCEPT_STREAM_WAIT (a) OP_S_READ_EXPECT (a, "foo", 3) OP_S_EXPECT_FIN (a) OP_S_UNBIND_STREAM_ID (a) OP_END_REPEAT () OP_END }; static const struct script_op script_15[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_BEGIN_REPEAT (200) OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, SSL_STREAM_FLAG_ADVANCE) OP_C_WRITE (a, "foo", 3) OP_C_CONCLUDE (a) OP_C_FREE_STREAM (a) OP_END_REPEAT () OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "bar", 3) OP_S_CONCLUDE (a) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "bar", 3) OP_C_EXPECT_FIN (a) OP_BEGIN_REPEAT (200) OP_S_ACCEPT_STREAM_WAIT (b) OP_S_READ_EXPECT (b, "foo", 3) OP_S_EXPECT_FIN (b) OP_S_UNBIND_STREAM_ID (b) OP_END_REPEAT () OP_END }; static const struct script_op script_16[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_BEGIN_REPEAT (200) OP_S_NEW_STREAM_BIDI (a, ANY_ID) OP_S_WRITE (a, "foo", 3) OP_S_CONCLUDE (a) OP_S_UNBIND_STREAM_ID (a) OP_END_REPEAT () OP_C_NEW_STREAM_BIDI (a, ANY_ID) OP_C_WRITE (a, "bar", 3) OP_C_CONCLUDE (a) OP_S_ACCEPT_STREAM_WAIT (b) OP_S_READ_EXPECT (b, "bar", 3) OP_S_EXPECT_FIN (b) OP_BEGIN_REPEAT (200) OP_C_ACCEPT_STREAM_WAIT (b) OP_C_READ_EXPECT (b, "foo", 3) OP_C_EXPECT_FIN (b) OP_C_FREE_STREAM (b) OP_END_REPEAT () OP_END }; static const struct script_op script_17[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (override_key_update, 1) OP_BEGIN_REPEAT (200) OP_C_WRITE (DEFAULT, "apple", 5) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (skip_time_ms, 100) OP_END_REPEAT () OP_CHECK (check_key_update_ge, 5) OP_C_WRITE (DEFAULT, "xyzzy", 5) OP_S_READ_EXPECT (a, "xyzzy", 5) OP_S_WRITE (a, "plugh", 5) OP_C_READ_EXPECT (DEFAULT, "plugh", 5) OP_END }; static const struct script_op script_18[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (override_key_update, 1) OP_BEGIN_REPEAT (200) OP_C_WRITE (DEFAULT, "apple", 5) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (skip_time_ms, 8) OP_END_REPEAT () OP_CHECK (check_key_update_lt, 240) OP_C_WRITE (DEFAULT, "xyzzy", 5) OP_S_READ_EXPECT (a, "xyzzy", 5) OP_S_WRITE (a, "plugh", 5) OP_C_READ_EXPECT (DEFAULT, "plugh", 5) OP_END }; static const struct script_op script_19[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_C_WRITE (DEFAULT, "orange", 6) OP_S_READ_EXPECT (a, "orange", 6) OP_S_WRITE (a, "strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "strawberry", 10) OP_CHECK (check_key_update_lt, 1) OP_CHECK (trigger_key_update, 0) OP_C_WRITE (DEFAULT, "orange", 6) OP_S_READ_EXPECT (a, "orange", 6) OP_S_WRITE (a, "ok", 2) OP_C_READ_EXPECT (DEFAULT, "ok", 2) OP_CHECK (check_key_update_ge, 1) OP_END }; static int script_20_trigger(struct helper *h, volatile uint64_t *counter) { #if defined(OPENSSL_THREADS) ossl_crypto_mutex_lock(h->misc_m); ++*counter; ossl_crypto_condvar_broadcast(h->misc_cv); ossl_crypto_mutex_unlock(h->misc_m); #endif return 1; } static int script_20_wait(struct helper *h, volatile uint64_t *counter, uint64_t threshold) { #if defined(OPENSSL_THREADS) int stop = 0; ossl_crypto_mutex_lock(h->misc_m); while (!stop) { stop = (*counter >= threshold); if (stop) break; ossl_crypto_condvar_wait(h->misc_cv, h->misc_m); } ossl_crypto_mutex_unlock(h->misc_m); #endif return 1; } static int script_20_trigger1(struct helper *h, struct helper_local *hl) { return script_20_trigger(h, &h->scratch0); } static int script_20_wait1(struct helper *h, struct helper_local *hl) { return script_20_wait(h, &h->scratch0, hl->check_op->arg2); } static int script_20_trigger2(struct helper *h, struct helper_local *hl) { return script_20_trigger(h, &h->scratch1); } static int script_20_wait2(struct helper *h, struct helper_local *hl) { return script_20_wait(h, &h->scratch1, hl->check_op->arg2); } static const struct script_op script_20_child[] = { OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "foo", 3) OP_CHECK (script_20_trigger1, 0) OP_CHECK (script_20_wait2, 1) OP_C_READ_FAIL_WAIT (a) OP_C_EXPECT_SSL_ERR (a, SSL_ERROR_SYSCALL) OP_EXPECT_ERR_LIB (ERR_LIB_SSL) OP_EXPECT_ERR_REASON (SSL_R_PROTOCOL_IS_SHUTDOWN) OP_POP_ERR () OP_EXPECT_ERR_LIB (ERR_LIB_SSL) OP_EXPECT_ERR_REASON (SSL_R_QUIC_NETWORK_ERROR) OP_C_FREE_STREAM (a) OP_END }; static const struct script_op script_20[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_NEW_THREAD (5, script_20_child) OP_BEGIN_REPEAT (5) OP_S_NEW_STREAM_BIDI (a, ANY_ID) OP_S_WRITE (a, "foo", 3) OP_S_UNBIND_STREAM_ID (a) OP_END_REPEAT () OP_CHECK (script_20_wait1, 5) OP_C_CLOSE_SOCKET () OP_CHECK (script_20_trigger2, 0) OP_END }; static int script_21_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[8]; size_t written; if (h->inject_word0 == 0 || hdr->type != h->inject_word0) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_21[] = { OP_S_SET_INJECT_PLAIN (script_21_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static int script_22_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { if (h->inject_word0 == 0) return 1; hdr->reserved = 1; return 1; } static const struct script_op script_22[] = { OP_S_SET_INJECT_PLAIN (script_22_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0) OP_END }; static int script_23_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[16]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_23[] = { OP_S_SET_INJECT_PLAIN (script_23_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static int script_24_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[16]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, (((uint64_t)1) << 60) + 1))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_24[] = { OP_S_SET_INJECT_PLAIN (script_24_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_25[] = { OP_S_SET_INJECT_PLAIN (script_24_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_26[] = { OP_S_SET_INJECT_PLAIN (script_24_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0) OP_END }; static const struct script_op script_27[] = { OP_S_SET_INJECT_PLAIN (script_24_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0) OP_END }; static int script_28_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[32]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0 - 1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123)) || (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 5)))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_28[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "orange", 6) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "orange", 6) OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0)) OP_C_WRITE (b, "apple", 5) OP_S_BIND_STREAM_ID (b, C_UNI_ID(0)) OP_S_READ_EXPECT (b, "apple", 5) OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM) OP_S_WRITE (a, "fruit", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_29[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "orange", 6) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "orange", 6) OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0)) OP_C_WRITE (b, "apple", 5) OP_S_BIND_STREAM_ID (b, C_UNI_ID(0)) OP_S_READ_EXPECT (b, "apple", 5) OP_SET_INJECT_WORD (C_UNI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM) OP_S_WRITE (a, "fruit", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_30[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_31[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static int script_32_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[64]; size_t written; uint64_t type = OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN, offset, flen, i; if (hdr->type != QUIC_PKT_TYPE_1RTT) return 1; switch (h->inject_word1) { default: return 0; case 0: return 1; case 1: offset = 0; flen = 0; break; case 2: offset = (((uint64_t)1)<<62) - 1; flen = 5; break; case 3: offset = 1 * 1024 * 1024 * 1024; flen = 5; break; case 4: offset = 0; flen = 1; break; } if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0 - 1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, flen))) goto err; for (i = 0; i < flen; ++i) if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_32[] = { OP_S_SET_INJECT_PLAIN (script_32_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, 1) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_33[] = { OP_S_SET_INJECT_PLAIN (script_32_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 2) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_34[] = { OP_S_SET_INJECT_PLAIN (script_32_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, 3) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR,0,0) OP_END }; static const struct script_op script_35[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (S_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_36[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_37[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_UNI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0)) OP_SET_INJECT_WORD (C_UNI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED) OP_S_WRITE (b, "orange", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_38[] = { OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_UNI (a, C_UNI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_UNI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (C_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED) OP_S_NEW_STREAM_UNI (b, S_UNI_ID(0)) OP_S_WRITE (b, "orange", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static int script_39_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[64]; size_t i, written; uint64_t seq_no = 0, retire_prior_to = 0; QUIC_CONN_ID new_cid = {0}; QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(h->s_priv); if (hdr->type != QUIC_PKT_TYPE_1RTT) return 1; switch (h->inject_word1) { case 0: return 1; case 1: new_cid.id_len = 0; break; case 2: new_cid.id_len = 21; break; case 3: new_cid.id_len = 1; new_cid.id[0] = 0x55; seq_no = 0; retire_prior_to = 1; break; case 4: ossl_quic_channel_get_diag_local_cid(ch, &new_cid); seq_no = 2; retire_prior_to = 2; break; case 5: new_cid.id_len = 8; new_cid.id[0] = 0x55; seq_no = 1; retire_prior_to = 1; break; } if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, seq_no)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, retire_prior_to)) || !TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id_len))) goto err; for (i = 0; i < new_cid.id_len && i < OSSL_NELEM(new_cid.id); ++i) if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, new_cid.id[i]))) goto err; for (; i < new_cid.id_len; ++i) if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x55))) goto err; for (i = 0; i < QUIC_STATELESS_RESET_TOKEN_LEN; ++i) if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_39[] = { OP_S_SET_INJECT_PLAIN (script_39_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (0, 1) OP_S_WRITE (a, "orange", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const unsigned char script_40_data[1024] = "strawberry"; static const struct script_op script_40[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_C_INHIBIT_TICK (1) OP_C_SET_WRITE_BUF_SIZE (a, 1024 * 100 * 3) OP_BEGIN_REPEAT (100) OP_C_WRITE (a, script_40_data, sizeof(script_40_data)) OP_END_REPEAT () OP_C_CONCLUDE (a) OP_C_SHUTDOWN_WAIT (NULL, 0) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_BEGIN_REPEAT (100) OP_S_READ_EXPECT (a, script_40_data, sizeof(script_40_data)) OP_END_REPEAT () OP_S_EXPECT_FIN (a) OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0) OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1) OP_END }; static const uint64_t path_challenge = UINT64_C(0xbdeb9451169c83aa); static int script_41_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[16]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)) || !TEST_true(WPACKET_put_bytes_u64(&wpkt, path_challenge))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)) || !TEST_size_t_eq(written, 9)) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; --h->inject_word0; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static void script_41_trace(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) { uint64_t frame_type, frame_data; int was_minimal; struct helper *h = arg; PACKET pkt; if (version != OSSL_QUIC1_VERSION || content_type != SSL3_RT_QUIC_FRAME_FULL || len < 1) return; if (!TEST_true(PACKET_buf_init(&pkt, buf, len))) { ++h->scratch1; return; } if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type, &was_minimal))) { ++h->scratch1; return; } if (frame_type != OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE) return; if (!TEST_true(ossl_quic_wire_decode_frame_path_response(&pkt, &frame_data)) || !TEST_uint64_t_eq(frame_data, path_challenge)) { ++h->scratch1; return; } ++h->scratch0; } static int script_41_setup(struct helper *h, struct helper_local *hl) { ossl_quic_tserver_set_msg_callback(ACQUIRE_S(), script_41_trace, h); return 1; } static int script_41_check(struct helper *h, struct helper_local *hl) { if (!TEST_uint64_t_gt(h->scratch0, 0)) return 0; if (!TEST_uint64_t_eq(h->scratch1, 0)) return 0; return 1; } static const struct script_op script_41[] = { OP_S_SET_INJECT_PLAIN (script_41_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_CHECK (script_41_setup, 0) OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "strawberry", 10) OP_S_READ_EXPECT (a, "strawberry", 10) OP_CHECK (script_41_check, 0) OP_END }; static int script_42_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; unsigned char frame_buf[64]; size_t written; WPACKET wpkt; if (h->inject_word0 == 0) return 1; --h->inject_word0; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 1)) || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_42[] = { OP_S_SET_INJECT_PLAIN (script_42_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, (((uint64_t)1) << 62) - 1) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_43[] = { OP_S_SET_INJECT_PLAIN (script_42_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0x100000 ) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0) OP_END }; static int script_44_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[16]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(ossl_quic_wire_encode_padding(&wpkt, 1))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_44[] = { OP_S_SET_INJECT_PLAIN (script_44_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10) OP_END }; static int force_ping(struct helper *h, struct helper_local *hl) { QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S()); h->scratch0 = ossl_quic_channel_get_diag_num_rx_ack(ch); if (!TEST_true(ossl_quic_tserver_ping(ACQUIRE_S()))) return 0; return 1; } static int wait_incoming_acks_increased(struct helper *h, struct helper_local *hl) { QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S()); uint16_t count; count = ossl_quic_channel_get_diag_num_rx_ack(ch); if (count == h->scratch0) { h->check_spin_again = 1; return 0; } return 1; } static const struct script_op script_45[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_BEGIN_REPEAT (2) OP_CHECK (force_ping, 0) OP_CHECK (wait_incoming_acks_increased, 0) OP_END_REPEAT () OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10) OP_END }; static int script_46_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[16]; size_t written; uint64_t type = 0, largest_acked = 0, first_range = 0, range_count = 0; uint64_t agap = 0, alen = 0; uint64_t ect0 = 0, ect1 = 0, ecnce = 0; if (h->inject_word0 == 0) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; type = OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN; switch (h->inject_word0) { case 1: largest_acked = 100; first_range = 101; range_count = 0; break; case 2: largest_acked = 100; first_range = 80; range_count = 1; agap = 0; alen = 19; break; case 3: largest_acked = 100; first_range = 80; range_count = 1; agap = 18; alen = 1; break; case 4: type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN; largest_acked = 100; first_range = 1; range_count = 0; break; case 5: type = OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN; largest_acked = 0; first_range = 0; range_count = 0; ect0 = 0; ect1 = 50; ecnce = 200; break; } h->inject_word0 = 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, largest_acked)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, range_count)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, first_range))) goto err; if (range_count > 0) if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, agap)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, alen))) goto err; if (type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, ect0)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ect1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, ecnce))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_46[] = { OP_S_SET_INJECT_PLAIN (script_46_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_47[] = { OP_S_SET_INJECT_PLAIN (script_46_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (2, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_48[] = { OP_S_SET_INJECT_PLAIN (script_46_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (3, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_49[] = { OP_S_SET_INJECT_PLAIN (script_46_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (4, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10) OP_END }; static const struct script_op script_50[] = { OP_S_SET_INJECT_PLAIN (script_46_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_BEGIN_REPEAT (2) OP_SET_INJECT_WORD (5, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10) OP_END_REPEAT () OP_END }; static const struct script_op script_51[] = { OP_S_SET_INJECT_PLAIN (script_41_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_END }; static int script_52_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; unsigned char frame_buf[64]; size_t written; WPACKET wpkt; uint64_t type = h->inject_word1; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; --h->inject_word0; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, type))) goto err; if (type == OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED) if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, C_BIDI_ID(0)))) goto err; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, 0xFFFFFF))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_52[] = { OP_S_SET_INJECT_PLAIN (script_52_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_END }; static int script_53_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; size_t written; WPACKET wpkt; uint64_t offset = 0, data_len = 100; unsigned char *frame_buf = NULL; size_t frame_len, i; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; h->inject_word0 = 0; switch (h->inject_word1) { case 0: offset = 100000; data_len = 1; break; } frame_len = 1 + 8 + 8 + (size_t)data_len; if (!TEST_ptr(frame_buf = OPENSSL_malloc(frame_len))) return 0; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, frame_len, 0))) goto err; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, offset)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, data_len))) goto err; for (i = 0; i < data_len; ++i) if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x42))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); OPENSSL_free(frame_buf); return ok; } static const struct script_op script_53[] = { OP_S_SET_INJECT_PLAIN (script_53_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,0,0) OP_END }; static int script_54_inject_handshake(struct helper *h, unsigned char *buf, size_t buf_len) { size_t i; for (i = 0; i < buf_len; ++i) buf[i] ^= 0xff; return 1; } static const struct script_op script_54[] = { OP_S_SET_INJECT_HANDSHAKE(script_54_inject_handshake) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT_OR_FAIL() OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_UNEXPECTED_MESSAGE,0,0) OP_END }; static const struct script_op script_55[] = { OP_S_SET_INJECT_PLAIN (script_39_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (0, 2) OP_S_WRITE (a, "orange", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_56[] = { OP_S_SET_INJECT_PLAIN (script_39_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (0, 3) OP_S_WRITE (a, "orange", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; static const struct script_op script_57[] = { OP_S_SET_INJECT_PLAIN (script_39_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (0, 4) OP_S_WRITE (a, "orange", 5) OP_C_READ_EXPECT (a, "orange", 5) OP_C_WRITE (a, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_SET_INJECT_WORD (0, 5) OP_S_WRITE (a, "raspberry", 9) OP_C_READ_EXPECT (a, "raspberry", 9) OP_C_WRITE (a, "peach", 5) OP_S_READ_EXPECT (a, "peach", 5) OP_END }; static int script_58_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; unsigned char frame_buf[64]; size_t written; WPACKET wpkt; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (h->inject_word0 == 1) { if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE))) goto err; } else { if (!TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x40)) || !TEST_true(WPACKET_put_bytes_u8(&wpkt, 0x1E))) goto err; } if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_58[] = { OP_S_SET_INJECT_PLAIN (script_58_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_WRITE (DEFAULT, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_END }; static const struct script_op script_59[] = { OP_S_SET_INJECT_PLAIN (script_58_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (2, 0) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION,0,0) OP_END }; static char long_reason[2048]; static int init_reason(struct helper *h, struct helper_local *hl) { memset(long_reason, '~', sizeof(long_reason)); memcpy(long_reason, "This is a long reason string.", 29); long_reason[OSSL_NELEM(long_reason) - 1] = '\0'; return 1; } static int check_shutdown_reason(struct helper *h, struct helper_local *hl) { const QUIC_TERMINATE_CAUSE *tc = ossl_quic_tserver_get_terminate_cause(ACQUIRE_S()); if (tc == NULL) { h->check_spin_again = 1; return 0; } if (!TEST_size_t_ge(tc->reason_len, 50) || !TEST_mem_eq(long_reason, tc->reason_len, tc->reason, tc->reason_len)) return 0; return 1; } static const struct script_op script_60[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (init_reason, 0) OP_C_SHUTDOWN_WAIT (long_reason, 0) OP_CHECK (check_shutdown_reason, 0) OP_END }; static int script_61_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[32]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 123)) || (h->inject_word0 == OSSL_QUIC_FRAME_TYPE_RESET_STREAM && !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_61[] = { OP_S_SET_INJECT_PLAIN (script_61_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "orange", 6) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "orange", 6) OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_RESET_STREAM, S_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4)) OP_S_WRITE (a, "fruit", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0) OP_END }; static const struct script_op script_62[] = { OP_S_SET_INJECT_PLAIN (script_61_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "orange", 6) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "orange", 6) OP_SET_INJECT_WORD (OSSL_QUIC_FRAME_TYPE_STOP_SENDING, C_BIDI_ID(OSSL_QUIC_VLINT_MAX / 4)) OP_S_WRITE (a, "fruit", 5) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_STATE_ERROR,0,0) OP_END }; static const struct script_op script_63[] = { OP_S_SET_INJECT_PLAIN (script_32_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (S_BIDI_ID(5000) + 1, 4) OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_STREAM_LIMIT_ERROR,0,0) OP_END }; static const struct script_op script_64[] = { OP_S_SET_INJECT_PLAIN (script_32_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (S_BIDI_ID(20) + 1, 1) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_END }; static int script_65_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; unsigned char frame_buf[64]; size_t written; WPACKET wpkt; if (h->inject_word0 == 0) return 1; --h->inject_word0; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0)) || !TEST_true(WPACKET_quic_write_vlint(&wpkt, 0))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_65[] = { OP_S_SET_INJECT_PLAIN (script_65_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_END }; static int script_66_inject_plain(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int ok = 0; WPACKET wpkt; unsigned char frame_buf[64]; size_t written; if (h->inject_word0 == 0 || hdr->type != QUIC_PKT_TYPE_1RTT) return 1; if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word1))) goto err; if (h->inject_word1 == OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, h->inject_word0 - 1))) goto err; if (!TEST_true(WPACKET_quic_write_vlint(&wpkt, OSSL_QUIC_VLINT_MAX))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, written)) goto err; ok = 1; err: if (ok) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return ok; } static const struct script_op script_66[] = { OP_S_SET_INJECT_PLAIN (script_66_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (S_BIDI_ID(0) + 1, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_C_WRITE (a, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_END }; static const struct script_op script_67[] = { OP_S_SET_INJECT_PLAIN (script_66_inject_plain) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "apple", 5) OP_C_ACCEPT_STREAM_WAIT (a) OP_C_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, OSSL_QUIC_FRAME_TYPE_MAX_DATA) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_C_WRITE (a, "Strawberry", 10) OP_S_READ_EXPECT (a, "Strawberry", 10) OP_END }; static int script_68_inject_handshake(struct helper *h, unsigned char *msg, size_t msglen) { const unsigned char *data; size_t datalen; const unsigned char certreq[] = { SSL3_MT_CERTIFICATE_REQUEST, 0, 0, 12, 1, 1, 0, 8, 0, TLSEXT_TYPE_signature_algorithms, 0, 4, 0, 2, 8, 4 }; const unsigned char keyupdate[] = { SSL3_MT_KEY_UPDATE, 0, 0, 1, SSL_KEY_UPDATE_NOT_REQUESTED }; switch(h->inject_word0) { case 0: return 1; case 1: data = certreq; datalen = sizeof(certreq); break; case 2: data = keyupdate; datalen = sizeof(keyupdate); break; default: return 0; } if (!TEST_true(qtest_fault_resize_message(h->qtf, datalen - SSL3_HM_HEADER_LENGTH))) return 0; memcpy(msg, data, datalen); return 1; } static const struct script_op script_68[] = { OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_NEW_TICKET () OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0) OP_END }; static const struct script_op script_69[] = { OP_S_SET_INJECT_HANDSHAKE(script_68_inject_handshake) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (2, 0) OP_S_NEW_TICKET () OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN + SSL_AD_UNEXPECTED_MESSAGE, 0, 0) OP_END }; static int set_max_early_data(struct helper *h, struct helper_local *hl) { if (!TEST_true(ossl_quic_tserver_set_max_early_data(ACQUIRE_S(), (uint32_t)hl->check_op->arg2))) return 0; return 1; } static const struct script_op script_70[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (set_max_early_data, 0xfffffffe) OP_S_NEW_TICKET () OP_S_WRITE (a, "orange", 6) OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_PROTOCOL_VIOLATION, 0, 0) OP_END }; static const struct script_op script_71[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (set_max_early_data, 0xffffffff) OP_S_NEW_TICKET () OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_END }; static int script_72_check(struct helper *h, struct helper_local *hl) { if (!TEST_uint64_t_ge(h->fail_count, 50)) return 0; return 1; } static const struct script_op script_72[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_BEGIN_REPEAT (200) OP_C_NEW_STREAM_BIDI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK) OP_C_SKIP_IF_UNBOUND (a, 2) OP_C_WRITE (a, "apple", 5) OP_C_FREE_STREAM (a) OP_END_REPEAT () OP_CHECK (script_72_check, 0) OP_END }; static const struct script_op script_73[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_BEGIN_REPEAT (200) OP_C_NEW_STREAM_UNI_EX (a, ANY_ID, ALLOW_FAIL | SSL_STREAM_FLAG_NO_BLOCK) OP_C_SKIP_IF_UNBOUND (a, 2) OP_C_WRITE (a, "apple", 5) OP_C_FREE_STREAM (a) OP_END_REPEAT () OP_CHECK (script_72_check, 0) OP_END }; static int generate_version_neg(WPACKET *wpkt, uint32_t version) { QUIC_PKT_HDR hdr = {0}; hdr.type = QUIC_PKT_TYPE_VERSION_NEG; hdr.fixed = 1; hdr.dst_conn_id.id_len = 0; hdr.src_conn_id.id_len = 8; memset(hdr.src_conn_id.id, 0x55, 8); if (!TEST_true(ossl_quic_wire_encode_pkt_hdr(wpkt, 0, &hdr, NULL))) return 0; if (!TEST_true(WPACKET_put_bytes_u32(wpkt, version))) return 0; return 1; } static int server_gen_version_neg(struct helper *h, BIO_MSG *msg, size_t stride) { int rc = 0, have_wpkt = 0; size_t l; WPACKET wpkt; BUF_MEM *buf = NULL; uint32_t version; switch (h->inject_word0) { case 0: return 1; case 1: version = QUIC_VERSION_1; break; default: version = 0x5432abcd; break; } if (!TEST_ptr(buf = BUF_MEM_new())) goto err; if (!TEST_true(WPACKET_init(&wpkt, buf))) goto err; have_wpkt = 1; generate_version_neg(&wpkt, version); if (!TEST_true(WPACKET_get_total_written(&wpkt, &l))) goto err; if (!TEST_true(qtest_fault_resize_datagram(h->qtf, l))) return 0; memcpy(msg->data, buf->data, l); h->inject_word0 = 0; rc = 1; err: if (have_wpkt) WPACKET_finish(&wpkt); BUF_MEM_free(buf); return rc; } static const struct script_op script_74[] = { OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg) OP_SET_INJECT_WORD (1, 0) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_END }; static const struct script_op script_75[] = { OP_S_SET_INJECT_DATAGRAM (server_gen_version_neg) OP_SET_INJECT_WORD (2, 0) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT_OR_FAIL() OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_CONNECTION_REFUSED,0,0) OP_END }; static int script_76_check(struct helper *h, struct helper_local *hl) { if (!TEST_false(SSL_shutdown_ex(h->c_conn, SSL_SHUTDOWN_FLAG_WAIT_PEER | SSL_SHUTDOWN_FLAG_NO_BLOCK, NULL, 0))) return 0; return 1; } static const struct script_op script_76[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_CHECK (script_76_check, 0) OP_S_SHUTDOWN (42) OP_C_SHUTDOWN_WAIT (NULL, SSL_SHUTDOWN_FLAG_WAIT_PEER) OP_C_EXPECT_CONN_CLOSE_INFO(42, 1, 1) OP_END }; static const struct script_op script_77[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT) OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (DEFAULT, "Strawberry", 10) OP_S_NEW_STREAM_BIDI (b, S_BIDI_ID(1)) OP_S_WRITE (b, "xyz", 3) OP_C_ACCEPT_STREAM_WAIT (b) OP_C_READ_EXPECT (b, "xyz", 3) OP_END }; static size_t new_session_count; static int on_new_session(SSL *s, SSL_SESSION *sess) { ++new_session_count; return 0; } static int setup_session(struct helper *h, struct helper_local *hl) { SSL_CTX_set_session_cache_mode(h->c_ctx, SSL_SESS_CACHE_BOTH); SSL_CTX_sess_set_new_cb(h->c_ctx, on_new_session); return 1; } static int trigger_late_session_ticket(struct helper *h, struct helper_local *hl) { new_session_count = 0; if (!TEST_true(ossl_quic_tserver_new_ticket(ACQUIRE_S()))) return 0; return 1; } static int check_got_session_ticket(struct helper *h, struct helper_local *hl) { if (!TEST_size_t_gt(new_session_count, 0)) return 0; return 1; } static int check_idle_timeout(struct helper *h, struct helper_local *hl); static const struct script_op script_78[] = { OP_C_SET_ALPN ("ossltest") OP_CHECK (setup_session, 0) OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_CHECK (trigger_late_session_ticket, 0) OP_S_WRITE (a, "Strawberry", 10) OP_C_READ_EXPECT (a, "Strawberry", 10) OP_CHECK (check_got_session_ticket, 0) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000) OP_END }; static const struct script_op script_79[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE_EX2 (DEFAULT, "apple", 5, SSL_WRITE_FLAG_CONCLUDE) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_EXPECT_FIN (a) OP_S_WRITE (a, "orange", 6) OP_S_CONCLUDE (a) OP_C_READ_EXPECT (DEFAULT, "orange", 6) OP_C_EXPECT_FIN (DEFAULT) OP_END }; static QUIC_STATELESS_RESET_TOKEN test_reset_token = { { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef }}; static int script_80_send_stateless_reset(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { unsigned char databuf[64]; if (h->inject_word1 == 0) return 1; h->inject_word1 = 0; fprintf(stderr, "Sending stateless reset\n"); RAND_bytes(databuf, 64); databuf[0] = 0x40; memcpy(&databuf[48], test_reset_token.token, sizeof(test_reset_token.token)); if (!TEST_int_eq(SSL_inject_net_dgram(h->c_conn, databuf, sizeof(databuf), NULL, h->s_net_bio_addr), 1)) return 0; return 1; } static int script_80_gen_new_conn_id(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { int rc = 0; size_t l; unsigned char frame_buf[64]; WPACKET wpkt; QUIC_CONN_ID new_cid = {0}; OSSL_QUIC_FRAME_NEW_CONN_ID ncid = {0}; QUIC_CHANNEL *ch = ossl_quic_tserver_get_channel(ACQUIRE_S_NOHL()); if (h->inject_word0 == 0) return 1; h->inject_word0 = 0; fprintf(stderr, "sending new conn id\n"); if (!TEST_true(WPACKET_init_static_len(&wpkt, frame_buf, sizeof(frame_buf), 0))) return 0; ossl_quic_channel_get_diag_local_cid(ch, &new_cid); ncid.seq_num = 2; ncid.retire_prior_to = 2; ncid.conn_id = new_cid; memcpy(ncid.stateless_reset.token, test_reset_token.token, sizeof(test_reset_token.token)); if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid))) goto err; if (!TEST_true(WPACKET_get_total_written(&wpkt, &l))) goto err; if (!qtest_fault_prepend_frame(h->qtf, frame_buf, l)) goto err; rc = 1; err: if (rc) WPACKET_finish(&wpkt); else WPACKET_cleanup(&wpkt); return rc; } static int script_80_inject_pkt(struct helper *h, QUIC_PKT_HDR *hdr, unsigned char *buf, size_t len) { if (h->inject_word1 == 1) return script_80_send_stateless_reset(h, hdr, buf, len); else if (h->inject_word0 == 1) return script_80_gen_new_conn_id(h, hdr, buf, len); return 1; } static const struct script_op script_80[] = { OP_S_SET_INJECT_PLAIN (script_80_inject_pkt) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_WRITE (DEFAULT, "apple", 5) OP_C_CONCLUDE (DEFAULT) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_SET_INJECT_WORD (1, 0) OP_S_WRITE (a, "apple", 5) OP_C_READ_EXPECT (DEFAULT, "apple", 5) OP_SET_INJECT_WORD (0, 1) OP_S_WRITE (a, "apple", 5) OP_C_EXPECT_CONN_CLOSE_INFO (0, 0, 1) OP_END }; static int modify_idle_timeout(struct helper *h, struct helper_local *hl) { uint64_t v = 0; if (!TEST_false(SSL_set_feature_request_uint(h->c_conn, SSL_VALUE_QUIC_IDLE_TIMEOUT, (1ULL << 62)))) return 0; if (!TEST_true(SSL_set_feature_request_uint(h->c_conn, SSL_VALUE_QUIC_IDLE_TIMEOUT, hl->check_op->arg2))) return 0; if (!TEST_true(SSL_get_feature_request_uint(h->c_conn, SSL_VALUE_QUIC_IDLE_TIMEOUT, &v))) return 0; if (!TEST_uint64_t_eq(v, hl->check_op->arg2)) return 0; return 1; } static int check_idle_timeout(struct helper *h, struct helper_local *hl) { uint64_t v = 0; if (!TEST_true(SSL_get_value_uint(h->c_conn, hl->check_op->arg1, SSL_VALUE_QUIC_IDLE_TIMEOUT, &v))) return 0; if (!TEST_uint64_t_eq(v, hl->check_op->arg2)) return 0; return 1; } static const struct script_op script_81[] = { OP_C_SET_ALPN ("ossltest") OP_CHECK (modify_idle_timeout, 25000) OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 25000) OP_END }; static const struct script_op script_82[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000) OP_END }; static int cannot_change_idle_timeout(struct helper *h, struct helper_local *hl) { uint64_t v = 0; if (!TEST_true(SSL_get_feature_request_uint(h->c_conn, SSL_VALUE_QUIC_IDLE_TIMEOUT, &v))) return 0; if (!TEST_uint64_t_eq(v, 30000)) return 0; if (!TEST_false(SSL_set_feature_request_uint(h->c_conn, SSL_VALUE_QUIC_IDLE_TIMEOUT, 5000))) return 0; return 1; } static const struct script_op script_83[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_CHECK (cannot_change_idle_timeout, 0) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_PEER_REQUEST, 30000) OP_CHECK2 (check_idle_timeout, SSL_VALUE_CLASS_FEATURE_NEGOTIATED, 30000) OP_END }; static int check_avail_streams(struct helper *h, struct helper_local *hl) { uint64_t v = 0; switch (hl->check_op->arg1) { case 0: if (!TEST_true(SSL_get_quic_stream_bidi_local_avail(h->c_conn, &v))) return 0; break; case 1: if (!TEST_true(SSL_get_quic_stream_bidi_remote_avail(h->c_conn, &v))) return 0; break; case 2: if (!TEST_true(SSL_get_quic_stream_uni_local_avail(h->c_conn, &v))) return 0; break; case 3: if (!TEST_true(SSL_get_quic_stream_uni_remote_avail(h->c_conn, &v))) return 0; break; default: return 0; } if (!TEST_uint64_t_eq(v, hl->check_op->arg2)) return 0; return 1; } static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl); static int reenable_test_event_handling(struct helper *h, struct helper_local *hl); static int check_write_buf_stat(struct helper *h, struct helper_local *hl) { SSL *c_a; uint64_t size, used, avail; if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a"))) return 0; if (!TEST_true(SSL_get_stream_write_buf_size(c_a, &size)) || !TEST_true(SSL_get_stream_write_buf_used(c_a, &used)) || !TEST_true(SSL_get_stream_write_buf_avail(c_a, &avail)) || !TEST_uint64_t_ge(size, avail) || !TEST_uint64_t_ge(size, used) || !TEST_uint64_t_eq(avail + used, size)) return 0; if (!TEST_uint64_t_eq(used, hl->check_op->arg1)) return 0; return 1; } static const struct script_op script_84[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_CHECK2 (check_avail_streams, 0, 100) OP_CHECK2 (check_avail_streams, 1, 100) OP_CHECK2 (check_avail_streams, 2, 100) OP_CHECK2 (check_avail_streams, 3, 100) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_CHECK2 (check_avail_streams, 0, 99) OP_CHECK2 (check_avail_streams, 1, 100) OP_CHECK2 (check_avail_streams, 2, 100) OP_CHECK2 (check_avail_streams, 3, 100) OP_C_NEW_STREAM_UNI (b, C_UNI_ID(0)) OP_CHECK2 (check_avail_streams, 0, 99) OP_CHECK2 (check_avail_streams, 1, 100) OP_CHECK2 (check_avail_streams, 2, 99) OP_CHECK2 (check_avail_streams, 3, 100) OP_S_NEW_STREAM_BIDI (c, S_BIDI_ID(0)) OP_S_WRITE (c, "x", 1) OP_C_ACCEPT_STREAM_WAIT (c) OP_C_READ_EXPECT (c, "x", 1) OP_CHECK2 (check_avail_streams, 0, 99) OP_CHECK2 (check_avail_streams, 1, 99) OP_CHECK2 (check_avail_streams, 2, 99) OP_CHECK2 (check_avail_streams, 3, 100) OP_S_NEW_STREAM_UNI (d, S_UNI_ID(0)) OP_S_WRITE (d, "x", 1) OP_C_ACCEPT_STREAM_WAIT (d) OP_C_READ_EXPECT (d, "x", 1) OP_CHECK2 (check_avail_streams, 0, 99) OP_CHECK2 (check_avail_streams, 1, 99) OP_CHECK2 (check_avail_streams, 2, 99) OP_CHECK2 (check_avail_streams, 3, 99) OP_CHECK2 (check_write_buf_stat, 0, 0) OP_CHECK (set_event_handling_mode_conn, SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT) OP_C_WRITE (a, "apple", 5) OP_CHECK2 (check_write_buf_stat, 5, 0) OP_CHECK (reenable_test_event_handling, 0) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_READ_EXPECT (a, "apple", 5) OP_S_WRITE (a, "orange", 6) OP_C_READ_EXPECT (a, "orange", 6) OP_CHECK2 (check_write_buf_stat, 0, 0) OP_END }; ossl_unused static int script_85_poll(struct helper *h, struct helper_local *hl) { int ok = 1, ret, expected_ret = 1; static const struct timeval timeout = {0}; static const struct timeval nz_timeout = {0, 1}; size_t result_count, expected_result_count = 0; SSL_POLL_ITEM items[5] = {0}, *item = items; SSL *c_a, *c_b, *c_c, *c_d; size_t i; uint64_t mode, expected_revents[5] = {0}; if (!TEST_ptr(c_a = helper_local_get_c_stream(hl, "a")) || !TEST_ptr(c_b = helper_local_get_c_stream(hl, "b")) || !TEST_ptr(c_c = helper_local_get_c_stream(hl, "c")) || !TEST_ptr(c_d = helper_local_get_c_stream(hl, "d"))) return 0; item->desc = SSL_as_poll_descriptor(c_a); item->events = UINT64_MAX; item->revents = UINT64_MAX; ++item; item->desc = SSL_as_poll_descriptor(c_b); item->events = UINT64_MAX; item->revents = UINT64_MAX; ++item; item->desc = SSL_as_poll_descriptor(c_c); item->events = UINT64_MAX; item->revents = UINT64_MAX; ++item; item->desc = SSL_as_poll_descriptor(c_d); item->events = UINT64_MAX; item->revents = UINT64_MAX; ++item; item->desc = SSL_as_poll_descriptor(h->c_conn); item->events = UINT64_MAX; item->revents = UINT64_MAX; ++item; result_count = SIZE_MAX; ERR_set_mark(); if (!TEST_false(SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM), &nz_timeout, 0, &result_count)) || !TEST_size_t_eq(result_count, 0)) return 0; ERR_pop_to_mark(); result_count = SIZE_MAX; ret = SSL_poll(items, OSSL_NELEM(items), sizeof(SSL_POLL_ITEM), &timeout, 0, &result_count); mode = hl->check_op->arg2; switch (mode) { case 0: expected_revents[0] = SSL_POLL_EVENT_W; expected_revents[1] = SSL_POLL_EVENT_W; expected_revents[2] = SSL_POLL_EVENT_W; expected_revents[3] = SSL_POLL_EVENT_W; expected_revents[4] = SSL_POLL_EVENT_OS; expected_result_count = 5; break; case 1: expected_revents[0] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_R; expected_revents[1] = SSL_POLL_EVENT_W | SSL_POLL_EVENT_ER; expected_revents[2] = SSL_POLL_EVENT_EW; expected_revents[3] = SSL_POLL_EVENT_W; expected_revents[4] = SSL_POLL_EVENT_OS | SSL_POLL_EVENT_ISB; expected_result_count = 5; break; default: return 0; } if (!TEST_int_eq(ret, expected_ret) || !TEST_size_t_eq(result_count, expected_result_count)) ok = 0; for (i = 0; i < OSSL_NELEM(items); ++i) if (!TEST_uint64_t_eq(items[i].revents, expected_revents[i])) { TEST_error("mismatch at index %zu in poll results, mode %d", i, (int)mode); ok = 0; } return ok; } static const struct script_op script_85[] = { OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "flamingo", 8) OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1)) OP_C_WRITE (b, "orange", 6) OP_C_NEW_STREAM_BIDI (c, C_BIDI_ID(2)) OP_C_WRITE (c, "Strawberry", 10) OP_C_NEW_STREAM_BIDI (d, C_BIDI_ID(3)) OP_C_WRITE (d, "sync", 4) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2)) OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3)) OP_CHECK (script_85_poll, 0) OP_S_READ_EXPECT (a, "flamingo", 8) OP_S_WRITE (a, "herringbone", 11) OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_SET_INJECT_WORD (C_BIDI_ID(1) + 1, OSSL_QUIC_FRAME_TYPE_RESET_STREAM) OP_S_READ_EXPECT (d, "sync", 4) OP_S_WRITE (d, "x", 1) OP_C_READ_EXPECT (d, "x", 1) OP_S_SET_INJECT_PLAIN (script_28_inject_plain) OP_SET_INJECT_WORD (C_BIDI_ID(2) + 1, OSSL_QUIC_FRAME_TYPE_STOP_SENDING) OP_S_NEW_STREAM_BIDI (z, S_BIDI_ID(0)) OP_S_WRITE (z, "z", 1) OP_S_WRITE (d, "x", 1) OP_C_READ_EXPECT (d, "x", 1) OP_CHECK (script_85_poll, 1) OP_END }; static int set_event_handling_mode_conn(struct helper *h, struct helper_local *hl) { hl->explicit_event_handling = 1; return SSL_set_event_handling_mode(h->c_conn, hl->check_op->arg2); } static int reenable_test_event_handling(struct helper *h, struct helper_local *hl) { hl->explicit_event_handling = 0; return 1; } static ossl_unused int set_event_handling_mode_stream(struct helper *h, struct helper_local *hl) { SSL *ssl = helper_local_get_c_stream(hl, "a"); if (!TEST_ptr(ssl)) return 0; return SSL_set_event_handling_mode(ssl, hl->check_op->arg2); } static const struct script_op script_86[] = { OP_SKIP_IF_BLOCKING (23) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT () OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) OP_CHECK (set_event_handling_mode_conn, SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT) OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) OP_C_WRITE (a, "apple", 5) OP_CHECK (set_event_handling_mode_conn, SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT) OP_CHECK (set_event_handling_mode_stream, SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT) OP_C_WRITE (a, "orange", 6) OP_C_CONCLUDE (a) OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) OP_BEGIN_REPEAT (20) OP_S_READ_FAIL (a, 1) OP_SLEEP (10) OP_END_REPEAT () OP_CHECK (reenable_test_event_handling, 0) OP_S_READ_EXPECT (a, "appleorange", 11) OP_S_EXPECT_FIN (a) OP_CHECK (set_event_handling_mode_conn, SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT) OP_S_WRITE (a, "ok", 2) OP_C_READ_FAIL (a) OP_CHECK (reenable_test_event_handling, 0) OP_C_READ_EXPECT (a, "ok", 2) OP_END }; static const struct script_op *const scripts[] = { script_1, script_2, script_3, script_4, script_5, script_6, script_7, script_8, script_9, script_10, script_11, script_12, script_13, script_14, script_15, script_16, script_17, script_18, script_19, script_20, script_21, script_22, script_23, script_24, script_25, script_26, script_27, script_28, script_29, script_30, script_31, script_32, script_33, script_34, script_35, script_36, script_37, script_38, script_39, script_40, script_41, script_42, script_43, script_44, script_45, script_46, script_47, script_48, script_49, script_50, script_51, script_52, script_53, script_54, script_55, script_56, script_57, script_58, script_59, script_60, script_61, script_62, script_63, script_64, script_65, script_66, script_67, script_68, script_69, script_70, script_71, script_72, script_73, script_74, script_75, script_76, script_77, script_78, script_79, script_80, script_81, script_82, script_83, script_84, script_85, script_86 }; static int test_script(int idx) { int script_idx, free_order, blocking; char script_name[64]; free_order = idx % 2; idx /= 2; blocking = idx % 2; idx /= 2; script_idx = idx; if (blocking && free_order) return 1; #if !defined(OPENSSL_THREADS) if (blocking) { TEST_skip("cannot test in blocking mode without threads"); return 1; } #endif snprintf(script_name, sizeof(script_name), "script %d", script_idx + 1); TEST_info("Running script %d (order=%d, blocking=%d)", script_idx + 1, free_order, blocking); return run_script(scripts[script_idx], script_name, free_order, blocking); } static struct script_op dyn_frame_types_script[] = { OP_S_SET_INJECT_PLAIN (script_21_inject_plain) OP_SET_INJECT_WORD (0, 0) OP_C_SET_ALPN ("ossltest") OP_C_CONNECT_WAIT_OR_FAIL() OP_C_EXPECT_CONN_CLOSE_INFO(OSSL_QUIC_ERR_FRAME_ENCODING_ERROR,0,0) OP_END }; struct forbidden_frame_type { uint64_t pkt_type, frame_type, expected_err; }; static const struct forbidden_frame_type forbidden_frame_types[] = { { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR }, { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_VLINT_MAX, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_INITIAL, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RESET_STREAM, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STOP_SENDING, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_HANDSHAKE, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, { QUIC_PKT_TYPE_1RTT, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID, OSSL_QUIC_ERR_PROTOCOL_VIOLATION }, }; static ossl_unused int test_dyn_frame_types(int idx) { size_t i; char script_name[64]; struct script_op *s = dyn_frame_types_script; for (i = 0; i < OSSL_NELEM(dyn_frame_types_script); ++i) if (s[i].op == OPK_SET_INJECT_WORD) { s[i].arg1 = (size_t)forbidden_frame_types[idx].pkt_type; s[i].arg2 = forbidden_frame_types[idx].frame_type; } else if (s[i].op == OPK_C_EXPECT_CONN_CLOSE_INFO) { s[i].arg2 = forbidden_frame_types[idx].expected_err; } snprintf(script_name, sizeof(script_name), "dyn script %d", idx); return run_script(dyn_frame_types_script, script_name, 0, 0); } 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(certfile = test_get_argument(0)) || !TEST_ptr(keyfile = test_get_argument(1))) return 0; ADD_ALL_TESTS(test_dyn_frame_types, OSSL_NELEM(forbidden_frame_types)); ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2 * 2); return 1; }
test
openssl/test/quic_multistream_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/bio.h> #include "testutil.h" #include "crypto/siphash.h" #include "internal/nelem.h" typedef struct { size_t size; unsigned char data[64]; } SIZED_DATA; typedef struct { int idx; SIZED_DATA expected; } TESTDATA; static TESTDATA tests[] = { { 0, { 8, { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, } } }, { 1, { 8, { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, } } }, { 2, { 8, { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, } } }, { 3, { 8, { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, } } }, { 4, { 8, { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, } } }, { 5, { 8, { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, } } }, { 6, { 8, { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, } } }, { 7, { 8, { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, } } }, { 8, { 8, { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, } } }, { 9, { 8, { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, } } }, { 10, { 8, { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, } } }, { 11, { 8, { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, } } }, { 12, { 8, { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, } } }, { 13, { 8, { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, } } }, { 14, { 8, { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, } } }, { 15, { 8, { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, } } }, { 16, { 8, { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, } } }, { 17, { 8, { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, } } }, { 18, { 8, { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, } } }, { 19, { 8, { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, } } }, { 20, { 8, { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, } } }, { 21, { 8, { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, } } }, { 22, { 8, { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, } } }, { 23, { 8, { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, } } }, { 24, { 8, { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, } } }, { 25, { 8, { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, } } }, { 26, { 8, { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, } } }, { 27, { 8, { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, } } }, { 28, { 8, { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, } } }, { 29, { 8, { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, } } }, { 30, { 8, { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, } } }, { 31, { 8, { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, } } }, { 32, { 8, { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, } } }, { 33, { 8, { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, } } }, { 34, { 8, { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, } } }, { 35, { 8, { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, } } }, { 36, { 8, { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, } } }, { 37, { 8, { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, } } }, { 38, { 8, { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, } } }, { 39, { 8, { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, } } }, { 40, { 8, { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, } } }, { 41, { 8, { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, } } }, { 42, { 8, { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, } } }, { 43, { 8, { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, } } }, { 44, { 8, { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, } } }, { 45, { 8, { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, } } }, { 46, { 8, { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, } } }, { 47, { 8, { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, } } }, { 48, { 8, { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, } } }, { 49, { 8, { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, } } }, { 50, { 8, { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, } } }, { 51, { 8, { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, } } }, { 52, { 8, { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, } } }, { 53, { 8, { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, } } }, { 54, { 8, { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, } } }, { 55, { 8, { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, } } }, { 56, { 8, { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, } } }, { 57, { 8, { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, } } }, { 58, { 8, { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, } } }, { 59, { 8, { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, } } }, { 60, { 8, { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, } } }, { 61, { 8, { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, } } }, { 62, { 8, { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, } } }, { 63, { 8, { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, } } }, { 0, { 16, { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6, 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, } } }, { 1, { 16, { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44, 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, } } }, { 2, { 16, { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7, 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, } } }, { 3, { 16, { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e, 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, } } }, { 4, { 16, { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf, 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, } } }, { 5, { 16, { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88, 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, } } }, { 6, { 16, { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13, 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, } } }, { 7, { 16, { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53, 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, } } }, { 8, { 16, { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61, 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, } } }, { 9, { 16, { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab, 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, } } }, { 10, { 16, { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56, 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, } } }, { 11, { 16, { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77, 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, } } }, { 12, { 16, { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58, 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, } } }, { 13, { 16, { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10, 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, } } }, { 14, { 16, { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c, 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, } } }, { 15, { 16, { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11, 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, } } }, { 16, { 16, { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb, 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, } } }, { 17, { 16, { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98, 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, } } }, { 18, { 16, { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85, 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, } } }, { 19, { 16, { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0, 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, } } }, { 20, { 16, { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73, 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, } } }, { 21, { 16, { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21, 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, } } }, { 22, { 16, { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a, 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, } } }, { 23, { 16, { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0, 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, } } }, { 24, { 16, { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8, 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, } } }, { 25, { 16, { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b, 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, } } }, { 26, { 16, { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8, 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, } } }, { 27, { 16, { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4, 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, } } }, { 28, { 16, { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88, 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, } } }, { 29, { 16, { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9, 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, } } }, { 30, { 16, { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde, 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, } } }, { 31, { 16, { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc, 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, } } }, { 32, { 16, { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e, 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, } } }, { 33, { 16, { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32, 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, } } }, { 34, { 16, { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf, 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, } } }, { 35, { 16, { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9, 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, } } }, { 36, { 16, { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46, 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, } } }, { 37, { 16, { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a, 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, } } }, { 38, { 16, { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e, 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, } } }, { 39, { 16, { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c, 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, } } }, { 40, { 16, { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad, 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, } } }, { 41, { 16, { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39, 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, } } }, { 42, { 16, { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb, 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, } } }, { 43, { 16, { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa, 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, } } }, { 44, { 16, { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78, 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, } } }, { 45, { 16, { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65, 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, } } }, { 46, { 16, { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89, 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, } } }, { 47, { 16, { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1, 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, } } }, { 48, { 16, { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf, 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, } } }, { 49, { 16, { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f, 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, } } }, { 50, { 16, { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3, 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, } } }, { 51, { 16, { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4, 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, } } }, { 52, { 16, { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e, 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, } } }, { 53, { 16, { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94, 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, } } }, { 54, { 16, { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c, 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, } } }, { 55, { 16, { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12, 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, } } }, { 56, { 16, { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9, 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, } } }, { 57, { 16, { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe, 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, } } }, { 58, { 16, { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8, 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, } } }, { 59, { 16, { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe, 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, } } }, { 60, { 16, { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53, 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, } } }, { 61, { 16, { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe, 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, } } }, { 62, { 16, { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0, 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, } } }, { 63, { 16, { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a, 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, } } } }; static int test_siphash(int idx) { SIPHASH siphash = { 0, }; TESTDATA test = tests[idx]; unsigned char key[SIPHASH_KEY_SIZE]; unsigned char in[64]; size_t inlen = test.idx; unsigned char *expected = test.expected.data; size_t expectedlen = test.expected.size; unsigned char out[SIPHASH_MAX_DIGEST_SIZE]; size_t i; if (expectedlen != SIPHASH_MIN_DIGEST_SIZE && expectedlen != SIPHASH_MAX_DIGEST_SIZE) { TEST_info("size %zu vs %d and %d", expectedlen, SIPHASH_MIN_DIGEST_SIZE, SIPHASH_MAX_DIGEST_SIZE); return 0; } if (!TEST_int_le(inlen, sizeof(in))) return 0; for (i = 0; i < sizeof(key); i++) key[i] = (unsigned char)i; for (i = 0; i < inlen; i++) in[i] = (unsigned char)i; if (!TEST_true(SipHash_set_hash_size(&siphash, expectedlen)) || !TEST_true(SipHash_Init(&siphash, key, 0, 0))) return 0; SipHash_Update(&siphash, in, inlen); if (!TEST_true(SipHash_Final(&siphash, out, expectedlen)) || !TEST_mem_eq(out, expectedlen, expected, expectedlen)) return 0; if (inlen > 16) { if (!TEST_true(SipHash_set_hash_size(&siphash, expectedlen)) || !TEST_true(SipHash_Init(&siphash, key, 0, 0))) return 0; SipHash_Update(&siphash, in, 1); SipHash_Update(&siphash, in+1, inlen-1); if (!TEST_true(SipHash_Final(&siphash, out, expectedlen))) return 0; if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("SipHash test #%d/1+(N-1) failed.", idx); return 0; } } if (inlen > 32) { size_t half = inlen / 2; if (!TEST_true(SipHash_set_hash_size(&siphash, expectedlen)) || !TEST_true(SipHash_Init(&siphash, key, 0, 0))) return 0; SipHash_Update(&siphash, in, half); SipHash_Update(&siphash, in+half, inlen-half); if (!TEST_true(SipHash_Final(&siphash, out, expectedlen))) return 0; if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("SipHash test #%d/2 failed.", idx); return 0; } for (half = 16; half < inlen; half += 16) { if (!TEST_true(SipHash_set_hash_size(&siphash, expectedlen)) || !TEST_true(SipHash_Init(&siphash, key, 0, 0))) return 0; SipHash_Update(&siphash, in, half); SipHash_Update(&siphash, in+half, inlen-half); if (!TEST_true(SipHash_Final(&siphash, out, expectedlen))) return 0; if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("SipHash test #%d/%zu+%zu failed.", idx, half, inlen-half); return 0; } } } return 1; } static int test_siphash_basic(void) { SIPHASH siphash = { 0, }; static const unsigned char key[SIPHASH_KEY_SIZE] = {0}; unsigned char output[SIPHASH_MAX_DIGEST_SIZE]; return TEST_int_eq(SipHash_set_hash_size(&siphash, 4), 0) && TEST_false(SipHash_Final(&siphash, output, 0)) && TEST_true(SipHash_set_hash_size(&siphash, 8)) && TEST_false(SipHash_Final(&siphash, output, 8)) && TEST_true(SipHash_Init(&siphash, key, 0, 0)) && TEST_true(SipHash_Final(&siphash, output, 8)) && TEST_int_eq(SipHash_Final(&siphash, output, 16), 0) && TEST_true(SipHash_set_hash_size(&siphash, 16)) && TEST_true(SipHash_Init(&siphash, key, 0, 0)) && TEST_int_eq(SipHash_Final(&siphash, output, 8), 0) && TEST_true(SipHash_Final(&siphash, output, 16)) && TEST_true(SipHash_set_hash_size(&siphash, 0)) && TEST_true(SipHash_Init(&siphash, key, 0, 0)) && TEST_int_eq(SipHash_Final(&siphash, output, 8), 0) && TEST_true(SipHash_Final(&siphash, output, 16)); } int setup_tests(void) { ADD_TEST(test_siphash_basic); ADD_ALL_TESTS(test_siphash, OSSL_NELEM(tests)); return 1; }
test
openssl/test/siphash_internal_test.c
openssl
#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; }
test
openssl/test/pkey_meth_kdf_test.c
openssl
#include <stdio.h> #include <stdlib.h> #include "internal/nelem.h" #include "internal/safe_math.h" #include "testutil.h" OSSL_SAFE_MATH_SIGNED(int, int) OSSL_SAFE_MATH_UNSIGNED(uint, unsigned int) OSSL_SAFE_MATH_UNSIGNED(size_t, size_t) static const struct { int a, b; int sum_err, sub_err, mul_err, div_err, mod_err, div_round_up_err; int neg_a_err, neg_b_err, abs_a_err, abs_b_err; } test_ints[] = { { 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { -1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { -1, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { -3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 2, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { -2, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { INT_MAX, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { INT_MAX, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { INT_MAX, 4, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { INT_MAX - 3 , 4, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { INT_MIN, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0 }, { 1, INT_MIN, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1 }, { INT_MIN, 2, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0 }, { 2, INT_MIN, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1 }, { INT_MIN, -1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0 }, { INT_MAX, INT_MIN, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1 }, { INT_MIN, INT_MAX, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0 }, { 3, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 }, }; static int test_int_ops(int n) { int err, r, s; const int a = test_ints[n].a, b = test_ints[n].b; err = 0; r = safe_add_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].sum_err) || (!err && !TEST_int_eq(r, a + b))) goto err; err = 0; r = safe_sub_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].sub_err) || (!err && !TEST_int_eq(r, a - b))) goto err; err = 0; r = safe_mul_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].mul_err) || (!err && !TEST_int_eq(r, a * b))) goto err; err = 0; r = safe_div_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].div_err) || (!err && !TEST_int_eq(r, a / b))) goto err; err = 0; r = safe_mod_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].mod_err) || (!err && !TEST_int_eq(r, a % b))) goto err; err = 0; r = safe_div_round_up_int(a, b, &err); if (!TEST_int_eq(err, test_ints[n].div_round_up_err)) goto err; s = safe_mod_int(a, b, &err); s = safe_add_int(safe_div_int(a, b, &err), s != 0, &err); if (!err && !TEST_int_eq(r, s)) goto err; err = 0; r = safe_neg_int(a, &err); if (!TEST_int_eq(err, test_ints[n].neg_a_err) || (!err && !TEST_int_eq(r, -a))) goto err; err = 0; r = safe_neg_int(b, &err); if (!TEST_int_eq(err, test_ints[n].neg_b_err) || (!err && !TEST_int_eq(r, -b))) goto err; err = 0; r = safe_abs_int(a, &err); if (!TEST_int_eq(err, test_ints[n].abs_a_err) || (!err && !TEST_int_eq(r, a < 0 ? -a : a))) goto err; err = 0; r = safe_abs_int(b, &err); if (!TEST_int_eq(err, test_ints[n].abs_b_err) || (!err && !TEST_int_eq(r, b < 0 ? -b : b))) goto err; return 1; err: TEST_info("a = %d b = %d r = %d err = %d", a, b, r, err); return 0; } static const struct { unsigned int a, b; int sum_err, sub_err, mul_err, div_err, mod_err, div_round_up_err; } test_uints[] = { { 3, 1, 0, 0, 0, 0, 0, 0 }, { 1, 3, 0, 1, 0, 0, 0, 0 }, { UINT_MAX, 1, 1, 0, 0, 0, 0, 0 }, { UINT_MAX, 2, 1, 0, 1, 0, 0, 0 }, { UINT_MAX, 16, 1, 0, 1, 0, 0, 0 }, { UINT_MAX - 13, 16, 1, 0, 1, 0, 0, 0 }, { 1, UINT_MAX, 1, 1, 0, 0, 0, 0 }, { 2, UINT_MAX, 1, 1, 1, 0, 0, 0 }, { UINT_MAX, 0, 0, 0, 0, 1, 1, 1 }, }; static int test_uint_ops(int n) { int err; unsigned int r; const unsigned int a = test_uints[n].a, b = test_uints[n].b; err = 0; r = safe_add_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].sum_err) || (!err && !TEST_uint_eq(r, a + b))) goto err; err = 0; r = safe_sub_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].sub_err) || (!err && !TEST_uint_eq(r, a - b))) goto err; err = 0; r = safe_mul_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].mul_err) || (!err && !TEST_uint_eq(r, a * b))) goto err; err = 0; r = safe_div_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].div_err) || (!err && !TEST_uint_eq(r, a / b))) goto err; err = 0; r = safe_mod_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].mod_err) || (!err && !TEST_uint_eq(r, a % b))) goto err; err = 0; r = safe_div_round_up_uint(a, b, &err); if (!TEST_int_eq(err, test_uints[n].div_round_up_err) || (!err && !TEST_uint_eq(r, a / b + (a % b != 0)))) goto err; err = 0; r = safe_neg_uint(a, &err); if (!TEST_int_eq(err, a != 0) || (!err && !TEST_uint_eq(r, 0))) goto err; err = 0; r = safe_neg_uint(b, &err); if (!TEST_int_eq(err, b != 0) || (!err && !TEST_uint_eq(r, 0))) goto err; err = 0; r = safe_abs_uint(a, &err); if (!TEST_int_eq(err, 0) || !TEST_uint_eq(r, a)) goto err; err = 0; r = safe_abs_uint(b, &err); if (!TEST_int_eq(err, 0) || !TEST_uint_eq(r, b)) goto err; return 1; err: TEST_info("a = %u b = %u r = %u err = %d", a, b, r, err); return 0; } static const struct { size_t a, b; int sum_err, sub_err, mul_err, div_err, mod_err, div_round_up_err; } test_size_ts[] = { { 3, 1, 0, 0, 0, 0, 0, 0 }, { 1, 3, 0, 1, 0, 0, 0, 0 }, { 36, 8, 0, 0, 0, 0, 0, 0 }, { SIZE_MAX, 1, 1, 0, 0, 0, 0, 0 }, { SIZE_MAX, 2, 1, 0, 1, 0, 0, 0 }, { SIZE_MAX, 8, 1, 0, 1, 0, 0, 0 }, { SIZE_MAX - 3, 8, 1, 0, 1, 0, 0, 0 }, { 1, SIZE_MAX, 1, 1, 0, 0, 0, 0 }, { 2, SIZE_MAX, 1, 1, 1, 0, 0, 0 }, { 11, 0, 0, 0, 0, 1, 1, 1 }, }; static int test_size_t_ops(int n) { int err; size_t r; const size_t a = test_size_ts[n].a, b = test_size_ts[n].b; err = 0; r = safe_add_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].sum_err) || (!err && !TEST_size_t_eq(r, a + b))) goto err; err = 0; r = safe_sub_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].sub_err) || (!err && !TEST_size_t_eq(r, a - b))) goto err; err = 0; r = safe_mul_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].mul_err) || (!err && !TEST_size_t_eq(r, a * b))) goto err; err = 0; r = safe_div_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].div_err) || (!err && !TEST_size_t_eq(r, a / b))) goto err; err = 0; r = safe_mod_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].mod_err) || (!err && !TEST_size_t_eq(r, a % b))) goto err; err = 0; r = safe_div_round_up_size_t(a, b, &err); if (!TEST_int_eq(err, test_size_ts[n].div_round_up_err) || (!err && !TEST_size_t_eq(r, a / b + (a % b != 0)))) goto err; err = 0; r = safe_neg_size_t(a, &err); if (!TEST_int_eq(err, a != 0) || (!err && !TEST_size_t_eq(r, 0))) goto err; err = 0; r = safe_neg_size_t(b, &err); if (!TEST_int_eq(err, b != 0) || (!err && !TEST_size_t_eq(r, 0))) goto err; err = 0; r = safe_abs_size_t(a, &err); if (!TEST_int_eq(err, 0) || !TEST_size_t_eq(r, a)) goto err; err = 0; r = safe_abs_size_t(b, &err); if (!TEST_int_eq(err, 0) || !TEST_size_t_eq(r, b)) goto err; return 1; err: TEST_info("a = %zu b = %zu r = %zu err = %d", a, b, r, err); return 0; } static const struct { int a, b, c; int err; } test_muldiv_ints[] = { { 3, 1, 2, 0 }, { 1, 3, 2, 0 }, { -3, 1, 2, 0 }, { 1, 3, -2, 0 }, { INT_MAX, INT_MAX, INT_MAX, 0 }, { INT_MIN, INT_MIN, INT_MAX, 1 }, { INT_MIN, INT_MIN, INT_MIN, 0 }, { INT_MAX, 2, 4, 0 }, { 8, INT_MAX, 4, 1 }, { INT_MAX, 8, 4, 1 }, { INT_MIN, 2, 4, 1 }, { 8, INT_MIN, 4, 1 }, { INT_MIN, 8, 4, 1 }, { 3, 4, 0, 1 }, }; static int test_int_muldiv(int n) { int err = 0; int r, real = 0; const int a = test_muldiv_ints[n].a; const int b = test_muldiv_ints[n].b; const int c = test_muldiv_ints[n].c; r = safe_muldiv_int(a, b, c, &err); if (c != 0) real = (int)((int64_t)a * (int64_t)b / (int64_t)c); if (!TEST_int_eq(err, test_muldiv_ints[n].err) || (!err && !TEST_int_eq(r, real))) { TEST_info("%d * %d / %d r = %d err = %d", a, b, c, r, err); return 0; } return 1; } static const struct { unsigned int a, b, c; int err; } test_muldiv_uints[] = { { 3, 1, 2, 0 }, { 1, 3, 2, 0 }, { UINT_MAX, UINT_MAX, UINT_MAX, 0 }, { UINT_MAX, 2, 4, 0 }, { 8, UINT_MAX, 4, 1 }, { UINT_MAX, 8, 4, 1 }, { 3, 4, 0, 1 }, }; static int test_uint_muldiv(int n) { int err = 0; unsigned int r, real = 0; const unsigned int a = test_muldiv_uints[n].a; const unsigned int b = test_muldiv_uints[n].b; const unsigned int c = test_muldiv_uints[n].c; r = safe_muldiv_uint(a, b, c, &err); if (c != 0) real = (unsigned int)((uint64_t)a * (uint64_t)b / (uint64_t)c); if (!TEST_int_eq(err, test_muldiv_uints[n].err) || (!err && !TEST_uint_eq(r, real))) { TEST_info("%u * %u / %u r = %u err = %d", a, b, c, r, err); return 0; } return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_int_ops, OSSL_NELEM(test_ints)); ADD_ALL_TESTS(test_uint_ops, OSSL_NELEM(test_uints)); ADD_ALL_TESTS(test_size_t_ops, OSSL_NELEM(test_size_ts)); ADD_ALL_TESTS(test_int_muldiv, OSSL_NELEM(test_muldiv_ints)); ADD_ALL_TESTS(test_uint_muldiv, OSSL_NELEM(test_muldiv_uints)); return 1; }
test
openssl/test/safe_math_test.c
openssl
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/crypto.h> #include "testutil.h" static long saved_argl; static void *saved_argp; static int saved_idx; static int saved_idx2; static int saved_idx3; static int gbl_result; static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) { if (!TEST_int_eq(idx, saved_idx) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp) || !TEST_ptr_null(ptr)) gbl_result = 0; } static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, void **from_d, int idx, long argl, void *argp) { if (!TEST_int_eq(idx, saved_idx) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp) || !TEST_ptr(from_d)) gbl_result = 0; return 1; } static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) { if (!TEST_int_eq(idx, saved_idx) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp)) gbl_result = 0; } typedef struct myobj_ex_data_st { char *hello; int new; int dup; } MYOBJ_EX_DATA; static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) { MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data)); if (!TEST_true(idx == saved_idx2 || idx == saved_idx3) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp) || !TEST_ptr_null(ptr) || !TEST_ptr(ex_data) || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) { gbl_result = 0; OPENSSL_free(ex_data); } else { ex_data->new = 1; } } static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, void **from_d, int idx, long argl, void *argp) { MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d; MYOBJ_EX_DATA *ex_data = NULL; if (!TEST_true(idx == saved_idx2 || idx == saved_idx3) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp) || !TEST_ptr(from_d) || !TEST_ptr(*update_ex_data) || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx)) || !TEST_true(ex_data->new)) { gbl_result = 0; } else { ex_data->hello = (*update_ex_data)->hello; ex_data->dup = 1; *update_ex_data = ex_data; } return 1; } static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) { MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx); if (!TEST_true(idx == saved_idx2 || idx == saved_idx3) || !TEST_long_eq(argl, saved_argl) || !TEST_ptr_eq(argp, saved_argp) || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL))) gbl_result = 0; OPENSSL_free(ex_data); } typedef struct myobj_st { CRYPTO_EX_DATA ex_data; int id; int st; } MYOBJ; static MYOBJ *MYOBJ_new(void) { static int count = 0; MYOBJ *obj = OPENSSL_malloc(sizeof(*obj)); if (obj != NULL) { obj->id = ++count; obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); } return obj; } static void MYOBJ_sethello(MYOBJ *obj, char *cp) { obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp); if (!TEST_int_eq(obj->st, 1)) gbl_result = 0; } static char *MYOBJ_gethello(MYOBJ *obj) { return CRYPTO_get_ex_data(&obj->ex_data, saved_idx); } static void MYOBJ_sethello2(MYOBJ *obj, char *cp) { MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2); if (TEST_ptr(ex_data)) ex_data->hello = cp; else obj->st = gbl_result = 0; } static char *MYOBJ_gethello2(MYOBJ *obj) { MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2); if (TEST_ptr(ex_data)) return ex_data->hello; obj->st = gbl_result = 0; return NULL; } static void MYOBJ_allochello3(MYOBJ *obj, char *cp) { MYOBJ_EX_DATA* ex_data = NULL; if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)) && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data, saved_idx3)) && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))) ex_data->hello = cp; else obj->st = gbl_result = 0; } static char *MYOBJ_gethello3(MYOBJ *obj) { MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3); if (TEST_ptr(ex_data)) return ex_data->hello; obj->st = gbl_result = 0; return NULL; } static void MYOBJ_free(MYOBJ *obj) { if (obj != NULL) { CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); OPENSSL_free(obj); } } static MYOBJ *MYOBJ_dup(MYOBJ *in) { MYOBJ *obj = MYOBJ_new(); if (obj != NULL) obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data, &in->ex_data); return obj; } static int test_exdata(void) { MYOBJ *t1 = NULL, *t2 = NULL, *t3 = NULL; MYOBJ_EX_DATA *ex_data = NULL; const char *cp; char *p; int res = 0; gbl_result = 1; if (!TEST_ptr(p = OPENSSL_strdup("hello world"))) return 0; saved_argl = 21; if (!TEST_ptr(saved_argp = OPENSSL_malloc(1))) goto err; saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, saved_argl, saved_argp, exnew, exdup, exfree); saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, saved_argl, saved_argp, exnew2, exdup2, exfree2); t1 = MYOBJ_new(); t2 = MYOBJ_new(); if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1)) goto err; if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2))) goto err; saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, saved_argl, saved_argp, exnew2, exdup2, exfree2); if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3))) goto err; MYOBJ_sethello(t1, p); cp = MYOBJ_gethello(t1); if (!TEST_ptr_eq(cp, p)) goto err; MYOBJ_sethello2(t1, p); cp = MYOBJ_gethello2(t1); if (!TEST_ptr_eq(cp, p)) goto err; MYOBJ_allochello3(t1, p); cp = MYOBJ_gethello3(t1); if (!TEST_ptr_eq(cp, p)) goto err; cp = MYOBJ_gethello(t2); if (!TEST_ptr_null(cp)) goto err; cp = MYOBJ_gethello2(t2); if (!TEST_ptr_null(cp)) goto err; t3 = MYOBJ_dup(t1); if (!TEST_int_eq(t3->st, 1)) goto err; ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2); if (!TEST_ptr(ex_data)) goto err; if (!TEST_int_eq(ex_data->dup, 1)) goto err; cp = MYOBJ_gethello(t3); if (!TEST_ptr_eq(cp, p)) goto err; cp = MYOBJ_gethello2(t3); if (!TEST_ptr_eq(cp, p)) goto err; cp = MYOBJ_gethello3(t3); if (!TEST_ptr_eq(cp, p)) goto err; if (gbl_result) res = 1; err: MYOBJ_free(t1); MYOBJ_free(t2); MYOBJ_free(t3); OPENSSL_free(saved_argp); saved_argp = NULL; OPENSSL_free(p); return res; } int setup_tests(void) { ADD_TEST(test_exdata); return 1; }
test
openssl/test/exdatatest.c
openssl
#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; ASN1_OCTET_STRING *src_string; ASN1_OCTET_STRING *tgt_string; } CMP_ASN_TEST_FIXTURE; static CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_ASN_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; return fixture; } static void tear_down(CMP_ASN_TEST_FIXTURE *fixture) { ASN1_OCTET_STRING_free(fixture->src_string); if (fixture->tgt_string != fixture->src_string) ASN1_OCTET_STRING_free(fixture->tgt_string); OPENSSL_free(fixture); } static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture) { int res = 0; ASN1_INTEGER *asn1integer = ASN1_INTEGER_new(); const int good_int = 77; const int64_t max_int = INT_MAX; if (!TEST_ptr(asn1integer)) return res; if (!TEST_true(ASN1_INTEGER_set(asn1integer, good_int))) { ASN1_INTEGER_free(asn1integer); return 0; } res = TEST_int_eq(good_int, ossl_cmp_asn1_get_int(asn1integer)); if (res == 0) goto err; res = 0; if (!TEST_true(ASN1_INTEGER_set_int64(asn1integer, max_int + 1))) goto err; res = TEST_int_eq(-2, ossl_cmp_asn1_get_int(asn1integer)); err: ASN1_INTEGER_free(asn1integer); return res; } static int test_cmp_asn1_get_int(void) { SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up); fixture->expected = 1; EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down); return result; } static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE * fixture) { if (!TEST_int_eq(fixture->expected, ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string, fixture->src_string))) return 0; if (fixture->expected != 0) return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string, fixture->src_string)); return 1; } static int test_ASN1_OCTET_STRING_set(void) { SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new()) || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new()) || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data, sizeof(rand_data)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down); return result; } static int test_ASN1_OCTET_STRING_set_tgt_is_src(void) { SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new()) || !(fixture->tgt_string = fixture->src_string) || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data, sizeof(rand_data)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down); return result; } void cleanup_tests(void) { return; } int setup_tests(void) { RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); ADD_TEST(test_cmp_asn1_get_int); ADD_TEST(test_ASN1_OCTET_STRING_set); ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src); return 1; }
test
openssl/test/cmp_asn_test.c
openssl
#include <openssl/ssl.h> #include <openssl/evp.h> #include "../ssl/ssl_local.h" #include "../ssl/record/record_local.h" #include "internal/recordmethod.h" #include "../ssl/record/methods/recmethod_local.h" #include "internal/nelem.h" #include "testutil.h" typedef struct { const char *plaintext[3]; const char *ciphertext[3]; const char *key; const char *iv; const char *seq; } RECORD_DATA; static RECORD_DATA refdata[] = { { { "080000240022000a00140012001d00170018001901000101010201030104001c" "00024001000000000b0001b9000001b50001b0308201ac30820115a003020102" "020102300d06092a864886f70d01010b0500300e310c300a0603550403130372" "7361301e170d3136303733303031323335395a170d3236303733303031323335" "395a300e310c300a0603550403130372736130819f300d06092a864886f70d01" "0101050003818d0030818902818100b4bb498f8279303d980836399b36c6988c" "0c68de55e1bdb826d3901a2461eafd2de49a91d015abbc9a95137ace6c1af19e", "aa6af98c7ced43120998e187a80ee0ccb0524b1b018c3e0b63264d449a6d38e2" "2a5fda430846748030530ef0461c8ca9d9efbfae8ea6d1d03e2bd193eff0ab9a" "8002c47428a6d35a8d88d79f7f1e3f0203010001a31a301830090603551d1304" "023000300b0603551d0f0404030205a0300d06092a864886f70d01010b050003" "81810085aad2a0e5b9276b908c65f73a7267170618a54c5f8a7b337d2df7a594" "365417f2eae8f8a58c8f8172f9319cf36b7fd6c55b80f21a03015156726096fd" "335e5e67f2dbf102702e608ccae6bec1fc63a42a99be5c3eb7107c3c54e9b9eb", "2bd5203b1c3b84e0a8b2f759409ba3eac9d91d402dcc0cc8f8961229ac9187b4" "2b4de100000f00008408040080754040d0ddab8cf0e2da2bc4995b868ad745c8" "e1564e33cde17880a42392cc624aeef6b67bb3f0ae71d9d54a2309731d87dc59" "f642d733be2eb27484ad8a8c8eb3516a7ac57f2625e2b5c0888a8541f4e734f7" "3d054761df1dd02f0e3e9a33cfa10b6e3eb4ebf7ac053b01fdabbddfc54133bc" "d24c8bbdceb223b2aa03452a2914000020ac86acbc9cd25a45b57ad5b64db15d" "4405cf8c80e314583ebf3283ef9a99310c16" }, { "f10b26d8fcaf67b5b828f712122216a1cd14187465b77637cbcd78539128bb93" "246dcca1af56f1eaa271666077455bc54965d85f05f9bd36d6996171eb536aff" "613eeddc42bad5a2d2227c4606f1215f980e7afaf56bd3b85a51be130003101a" "758d077b1c891d8e7a22947e5a229851fd42a9dd422608f868272abf92b3d43f" "b46ac420259346067f66322fd708885680f4b4433c29116f2dfa529e09bba53c" "7cd920121724809eaddcc84307ef46fc51a0b33d99d39db337fcd761ce0f2b02" "dc73dedb6fddb77c4f8099bde93d5bee08bcf2131f29a2a37ff07949e8f8bcdd", "3e8310b8bf8b3444c85aaf0d2aeb2d4f36fd14d5cb51fcebff418b3827136ab9" "529e9a3d3f35e4c0ae749ea2dbc94982a1281d3e6daab719aa4460889321a008" "bf10fa06ac0c61cc122cc90d5e22c0030c986ae84a33a0c47df174bcfbd50bf7" "8ffdf24051ab423db63d5815db2f830040f30521131c98c66f16c362addce2fb" "a0602cf0a7dddf22e8def7516cdfee95b4056cc9ad38c95352335421b5b1ffba" "df75e5212fdad7a75f52a2801486a1eec3539580bee0e4b337cda6085ac9eccd" "1a0f1a46cebfbb5cdfa3251ac28c3bc826148c6d8c1eb6a06f77f6ff632c6a83", "e283e8f9df7c6dbabf1c6ea40629a85b43ab0c73d34f9d5072832a104eda3f75" "f5d83da6e14822a18e14099d749eafd823ca2ac7542086501eca206ce7887920" "008573757ce2f230a890782b99cc682377beee812756d04f9025135fb599d746" "fefe7316c922ac265ca0d29021375adb63c1509c3e242dfb92b8dee891f7368c" "4058399b8db9075f2dcc8216194e503b6652d87d2cb41f99adfdcc5be5ec7e1e" "6326ac22d70bd3ba652827532d669aff005173597f8039c3ea4922d3ec757670" "222f6ac29b93e90d7ad3f6dd96328e429cfcfd5cca22707fe2d86ad1dcb0be75" "6e8e" }, "c66cb1aec519df44c91e10995511ac8b", "f7f6884c4981716c2d0d29a4", "0000000000000000" }, { { "14000020b9027a0204b972b52cdefa58950fa1580d68c9cb124dbe691a7178f2" "5c554b2316", "", "" }, { "9539b4ae2f87fd8e616b295628ea953d9e3858db274970d19813ec136cae7d96" "e0417775fcabd3d8858fdc60240912d218f5afb21c", "", "" }, "2679a43e1d76784034ea1797d5ad2649", "5482405290dd0d2f81c0d942", "0000000000000000" }, { { "040000c90000001e2fd3992f02000000b2ff099f9676cdff8b0bf8825d000000" "007905a9d28efeef4a47c6f9b06a0cecdb0070d920b898997c75b79636943ed4" "2046a96142bd084a04acfa0c490f452d756dea02c0f927259f1f3231ac0d541a" "769129b740ce38090842b828c27fd729f59737ba98aa7b42e043c5da28f8dca8" "590b2df410d5134fd6c4cacad8b30370602afa35d265bf4d127976bb36dbda6a" "626f0270e20eebc73d6fcae2b1a0da122ee9042f76be56ebf41aa469c3d2c9da" "9197d80008002a00040000040016", "", "" }, { "3680c2b2109d25caa26c3b06eea9fdc5cb31613ba702176596da2e886bf6af93" "507bd68161ad9cb4780653842e1041ecbf0088a65ac4ef438419dd1d95ddd9bd" "2ad4484e7e167d0e6c008448ae58a0418713b6fc6c51e4bb23a537fb75a74f73" "de31fe6aa0bc522515f8b25f8955428b5de5ac06762cec22b0aa78c94385ef8e" "70fa24945b7c1f268510871689bbbbfaf2e7f4a19277024f95f1143ab12a31ec" "63adb128cb390711fd6d06a498df3e98615d8eb102e23353b480efcca5e8e026" "7a6d0fe2441f14c8c9664aefb2cfff6ae9e0442728b6a0940c1e824fda06", "", "" }, "a688ebb5ac826d6f42d45c0cc44b9b7d", "c1cad4425a438b5de714830a", "0000000000000000" }, { { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" "202122232425262728292a2b2c2d2e2f303117", "", "" }, { "8c3497da00ae023e53c01b4324b665404c1b49e78fe2bf4d17f6348ae8340551" "e363a0cd05f2179c4fef5ad689b5cae0bae94adc63632e571fb79aa91544c639" "4d28a1", "", "" }, "88b96ad686c84be55ace18a59cce5c87", "b99dc58cd5ff5ab082fdad19", "0000000000000000" }, { { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" "202122232425262728292a2b2c2d2e2f303117", "", "" }, { "f65f49fd2df6cd2347c3d30166e3cfddb6308a5906c076112c6a37ff1dbd406b" "5813c0abd734883017a6b2833186b13c14da5d75f33d8760789994e27d82043a" "b88d65", "", "" }, "a688ebb5ac826d6f42d45c0cc44b9b7d", "c1cad4425a438b5de714830a", "0000000000000001" }, { { "010015", "", "" }, { "2c2148163d7938a35f6acf2a6606f8cbd1d9f2", "", "" }, "88b96ad686c84be55ace18a59cce5c87", "b99dc58cd5ff5ab082fdad19", "0000000000000001" }, { { "010015", "", "" }, { "f8141ebdb5eda511e0bce639a56ff9ea825a21", "", "" }, "a688ebb5ac826d6f42d45c0cc44b9b7d", "c1cad4425a438b5de714830a", "0000000000000002" } }; static unsigned char *multihexstr2buf(const char *str[3], size_t *len) { size_t outer, inner, curr = 0; unsigned char *outbuf; size_t totlen = 0; for (outer = 0; outer < 3; outer++) { totlen += strlen(str[outer]); if ((totlen & 1) != 0) return NULL; } totlen /= 2; outbuf = OPENSSL_malloc(totlen); if (outbuf == NULL) return NULL; for (outer = 0; outer < 3; outer++) { for (inner = 0; str[outer][inner] != 0; inner += 2) { int hi, lo; hi = OPENSSL_hexchar2int(str[outer][inner]); lo = OPENSSL_hexchar2int(str[outer][inner + 1]); if (hi < 0 || lo < 0) { OPENSSL_free(outbuf); return NULL; } outbuf[curr++] = (hi << 4) | lo; } } *len = totlen; return outbuf; } static int load_record(TLS_RL_RECORD *rec, RECORD_DATA *recd, unsigned char **key, unsigned char *iv, size_t ivlen, unsigned char *seq) { unsigned char *pt = NULL, *sq = NULL, *ivtmp = NULL; size_t ptlen; *key = OPENSSL_hexstr2buf(recd->key, NULL); ivtmp = OPENSSL_hexstr2buf(recd->iv, NULL); sq = OPENSSL_hexstr2buf(recd->seq, NULL); pt = multihexstr2buf(recd->plaintext, &ptlen); if (*key == NULL || ivtmp == NULL || sq == NULL || pt == NULL) goto err; rec->data = rec->input = OPENSSL_malloc(ptlen + EVP_GCM_TLS_TAG_LEN); if (rec->data == NULL) goto err; rec->length = ptlen; memcpy(rec->data, pt, ptlen); OPENSSL_free(pt); memcpy(seq, sq, SEQ_NUM_SIZE); OPENSSL_free(sq); memcpy(iv, ivtmp, ivlen); OPENSSL_free(ivtmp); return 1; err: OPENSSL_free(*key); *key = NULL; OPENSSL_free(ivtmp); OPENSSL_free(sq); OPENSSL_free(pt); return 0; } static int test_record(TLS_RL_RECORD *rec, RECORD_DATA *recd, int enc) { int ret = 0; unsigned char *refd; size_t refdatalen = 0; if (enc) refd = multihexstr2buf(recd->ciphertext, &refdatalen); else refd = multihexstr2buf(recd->plaintext, &refdatalen); if (!TEST_ptr(refd)) { TEST_info("Failed to get reference data"); goto err; } if (!TEST_mem_eq(rec->data, rec->length, refd, refdatalen)) goto err; ret = 1; err: OPENSSL_free(refd); return ret; } #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01") static int test_tls13_encryption(void) { TLS_RL_RECORD rec; unsigned char *key = NULL; const EVP_CIPHER *ciph = EVP_aes_128_gcm(); int ret = 0; size_t ivlen, ctr; unsigned char seqbuf[SEQ_NUM_SIZE]; unsigned char iv[EVP_MAX_IV_LENGTH]; OSSL_RECORD_LAYER *rrl = NULL, *wrl = NULL; rec.data = NULL; rec.type = SSL3_RT_APPLICATION_DATA; rec.rec_version = TLS1_2_VERSION; for (ctr = 0; ctr < OSSL_NELEM(refdata); ctr++) { ivlen = EVP_CIPHER_get_iv_length(ciph); if (!load_record(&rec, &refdata[ctr], &key, iv, ivlen, seqbuf)) { TEST_error("Failed loading key into EVP_CIPHER_CTX"); goto err; } if (!TEST_true(ossl_tls_record_method.new_record_layer( NULL, NULL, TLS1_3_VERSION, OSSL_RECORD_ROLE_SERVER, OSSL_RECORD_DIRECTION_WRITE, OSSL_RECORD_PROTECTION_LEVEL_APPLICATION, 0, NULL, 0, key, 16, iv, ivlen, NULL, 0, EVP_aes_128_gcm(), EVP_GCM_TLS_TAG_LEN, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &wrl))) goto err; memcpy(wrl->sequence, seqbuf, sizeof(seqbuf)); if (!TEST_size_t_eq(wrl->funcs->cipher(wrl, &rec, 1, 1, NULL, 0), 1)) { TEST_info("Failed to encrypt record %zu", ctr); goto err; } if (!TEST_true(test_record(&rec, &refdata[ctr], 1))) { TEST_info("Record %zu encryption test failed", ctr); goto err; } if (!TEST_true(ossl_tls_record_method.new_record_layer( NULL, NULL, TLS1_3_VERSION, OSSL_RECORD_ROLE_SERVER, OSSL_RECORD_DIRECTION_READ, OSSL_RECORD_PROTECTION_LEVEL_APPLICATION, 0, NULL, 0, key, 16, iv, ivlen, NULL, 0, EVP_aes_128_gcm(), EVP_GCM_TLS_TAG_LEN, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &rrl))) goto err; memcpy(rrl->sequence, seqbuf, sizeof(seqbuf)); if (!TEST_int_eq(rrl->funcs->cipher(rrl, &rec, 1, 0, NULL, 0), 1)) { TEST_info("Failed to decrypt record %zu", ctr); goto err; } if (!TEST_true(test_record(&rec, &refdata[ctr], 0))) { TEST_info("Record %zu decryption test failed", ctr); goto err; } ossl_tls_record_method.free(rrl); ossl_tls_record_method.free(wrl); rrl = wrl = NULL; OPENSSL_free(rec.data); OPENSSL_free(key); rec.data = NULL; key = NULL; } TEST_note("PASS: %zu records tested", ctr); ret = 1; err: ossl_tls_record_method.free(rrl); ossl_tls_record_method.free(wrl); OPENSSL_free(rec.data); OPENSSL_free(key); return ret; } int setup_tests(void) { ADD_TEST(test_tls13_encryption); return 1; }
test
openssl/test/tls13encryptiontest.c
openssl
#include <stddef.h> #include <openssl/provider.h> #include <openssl/evp.h> #include "testutil.h" static int test_provider(OSSL_LIB_CTX *ctx) { EVP_KEYMGMT *rsameth = NULL; const OSSL_PROVIDER *prov = NULL; int ok; ok = TEST_true(OSSL_PROVIDER_available(ctx, "default")) && TEST_ptr(rsameth = EVP_KEYMGMT_fetch(ctx, "RSA", NULL)) && TEST_ptr(prov = EVP_KEYMGMT_get0_provider(rsameth)) && TEST_str_eq(OSSL_PROVIDER_get0_name(prov), "default"); EVP_KEYMGMT_free(rsameth); return ok; } static int test_fallback_provider(void) { return test_provider(NULL); } static int test_explicit_provider(void) { OSSL_LIB_CTX *ctx = NULL; OSSL_PROVIDER *prov = NULL; int ok; ok = TEST_ptr(ctx = OSSL_LIB_CTX_new()) && TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default")) && test_provider(ctx) && TEST_true(OSSL_PROVIDER_unload(prov)); OSSL_LIB_CTX_free(ctx); return ok; } int setup_tests(void) { ADD_TEST(test_fallback_provider); ADD_TEST(test_explicit_provider); return 1; }
test
openssl/test/provider_fallback_test.c
openssl
#include "internal/deprecated.h" #include <string.h> #include "internal/nelem.h" #include "testutil.h" #include <openssl/ec.h> #ifndef OPENSSL_NO_ENGINE # include <openssl/engine.h> #endif #include <openssl/err.h> #include <openssl/obj_mac.h> #include <openssl/objects.h> #include <openssl/rand.h> #include <openssl/bn.h> #include <openssl/opensslconf.h> #include <openssl/core_names.h> #include <openssl/param_build.h> #include <openssl/evp.h> static size_t crv_len = 0; static EC_builtin_curve *curves = NULL; static int group_order_tests(EC_GROUP *group) { BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL; const EC_POINT *G = NULL; BN_CTX *ctx = NULL; int i = 0, r = 0; if (!TEST_ptr(n1 = BN_new()) || !TEST_ptr(n2 = BN_new()) || !TEST_ptr(order = BN_new()) || !TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(G = EC_GROUP_get0_generator(group)) || !TEST_ptr(P = EC_POINT_new(group)) || !TEST_ptr(Q = EC_POINT_new(group)) || !TEST_ptr(R = EC_POINT_new(group)) || !TEST_ptr(S = EC_POINT_new(group))) goto err; if (!TEST_true(EC_GROUP_get_order(group, order, ctx)) || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, Q)) #ifndef OPENSSL_NO_DEPRECATED_3_0 || !TEST_true(EC_GROUP_precompute_mult(group, ctx)) #endif || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, Q)) || !TEST_true(EC_POINT_copy(P, G)) || !TEST_true(BN_one(n1)) || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)) || !TEST_true(BN_sub(n1, order, n1)) || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx)) || !TEST_true(EC_POINT_invert(group, Q, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))) goto err; for (i = 1; i <= 2; i++) { #ifndef OPENSSL_NO_DEPRECATED_3_0 const BIGNUM *scalars[6]; const EC_POINT *points[6]; #endif if (!TEST_true(BN_set_word(n1, i)) || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx)) || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx))) || !TEST_true(BN_one(n1)) || !TEST_true(BN_sub(n1, n1, order)) || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)) || !TEST_true(BN_add(n2, order, BN_value_one())) || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)) || !TEST_true(BN_mul(n2, n1, n2, ctx)) || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))) goto err; BN_set_negative(n2, 0); if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, Q)) || !TEST_false(EC_POINT_is_at_infinity(group, P))) goto err; #ifndef OPENSSL_NO_DEPRECATED_3_0 scalars[0] = scalars[1] = BN_value_one(); points[0] = points[1] = P; if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx)) || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx))) goto err; scalars[0] = n1; points[0] = Q; scalars[1] = n2; points[1] = P; scalars[2] = n1; points[2] = Q; scalars[3] = n2; points[3] = Q; scalars[4] = n1; points[4] = P; scalars[5] = n2; points[5] = Q; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; #endif } r = 1; err: if (r == 0 && i != 0) TEST_info(i == 1 ? "allowing precomputation" : "without precomputation"); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); EC_POINT_free(S); BN_free(n1); BN_free(n2); BN_free(order); BN_CTX_free(ctx); return r; } static int prime_field_tests(void) { BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL; EC_GROUP *group = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL; BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL; #ifndef OPENSSL_NO_DEPRECATED_3_0 const EC_POINT *points[4]; const BIGNUM *scalars[4]; #endif unsigned char buf[100]; size_t len, r = 0; int k; if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_true(BN_hex2bn(&p, "17")) || !TEST_true(BN_hex2bn(&a, "1")) || !TEST_true(BN_hex2bn(&b, "1")) || !TEST_ptr(group = EC_GROUP_new_curve_GFp(p, a, b, ctx)) || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))) goto err; TEST_info("Curve defined by Weierstrass equation"); TEST_note(" y^2 = x^3 + a*x + b (mod p)"); test_output_bignum("a", a); test_output_bignum("b", b); test_output_bignum("p", p); buf[0] = 0; if (!TEST_ptr(P = EC_POINT_new(group)) || !TEST_ptr(Q = EC_POINT_new(group)) || !TEST_ptr(R = EC_POINT_new(group)) || !TEST_true(EC_POINT_set_to_infinity(group, P)) || !TEST_true(EC_POINT_is_at_infinity(group, P)) || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx)) || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P)) || !TEST_ptr(x = BN_new()) || !TEST_ptr(y = BN_new()) || !TEST_ptr(z = BN_new()) || !TEST_ptr(yplusone = BN_new()) || !TEST_true(BN_hex2bn(&x, "D")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))) goto err; if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) { if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx))) goto err; TEST_info("Point is not on curve"); test_output_bignum("x", x); test_output_bignum("y", y); goto err; } TEST_note("A cyclic subgroup:"); k = 100; do { if (!TEST_int_ne(k--, 0)) goto err; if (EC_POINT_is_at_infinity(group, P)) { TEST_note(" point at infinity"); } else { if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; test_output_bignum("x", x); test_output_bignum("y", y); } if (!TEST_true(EC_POINT_copy(R, P)) || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))) goto err; } while (!EC_POINT_is_at_infinity(group, P)); if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, compressed form:", buf, len); len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, uncompressed form:", buf, len); len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, hybrid form:", buf, len); if (!TEST_true(EC_POINT_invert(group, P, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) || !TEST_true(BN_hex2bn(&p, "FFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "FFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) || !TEST_true(BN_hex2bn(&b, "1C97BEFC" "54BD7A8B65ACF89F81D4D4ADC565FA45")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "4A96B568" "8EF573284664698968C38BB913CBFC82")) || !TEST_true(BN_hex2bn(&y, "23a62855" "3168947d59dcc912042351377ac5fb32")) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "0100000000" "000000000001F4C8F927AED3CA752257")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("SEC2 curve secp160r1 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "23a62855" "3168947d59dcc912042351377ac5fb32")) || !TEST_BN_eq(y, z) || !TEST_int_eq(EC_GROUP_get_degree(group), 160) || !group_order_tests(group) || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7" "0FA7E9AB72243049FEB8DEECC146B9B1")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6" "7CBF20EB43A18800F4FF0AFD82FF1012")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF" "FFFFFFFF99DEF836146BC9B1B4D22831")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("NIST curve P-192 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78" "631011ED6B24CDD573F977A11E794811")) || !TEST_BN_eq(y, z) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_int_eq(EC_GROUP_get_degree(group), 192) || !group_order_tests(group) || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFF000000000000000000000001")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256" "5044B0B7D7BFD8BA270B39432355FFB4")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9" "4A03C1D356C21122343280D6115C1D21")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF" "FFFF16A2E0B8F03E13DD29455C5C2A3D")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("NIST curve P-224 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6" "CD4375A05A07476444D5819985007E34")) || !TEST_BN_eq(y, z) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_int_eq(EC_GROUP_get_degree(group), 224) || !group_order_tests(group) || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000" "00000000FFFFFFFFFFFFFFFFFFFFFFFF")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000" "00000000FFFFFFFFFFFFFFFFFFFFFFFC")) || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC" "651D06B0CC53B0F63BCE3C3E27D2604B")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2" "77037D812DEB33A0F4A13945D898C296")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF" "BCE6FAADA7179E84F3B9CAC2FC632551")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("NIST curve P-256 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16" "2BCE33576B315ECECBB6406837BF51F5")) || !TEST_BN_eq(y, z) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_int_eq(EC_GROUP_get_degree(group), 256) || !group_order_tests(group) || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" "FFFFFFFF0000000000000000FFFFFFFF")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" "FFFFFFFF0000000000000000FFFFFFFC")) || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19" "181D9C6EFE8141120314088F5013875A" "C656398D8A2ED19D2A85C8EDD3EC2AEF")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74" "6E1D3B628BA79B9859F741E082542A38" "5502F25DBF55296C3A545E3872760AB7")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFC7634D81F4372DDF" "581A0DB248B0A77AECEC196ACCC52973")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("NIST curve P-384 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29" "F8F41DBD289A147CE9DA3113B5F0B8C0" "0A60B1CE1D7E819D7A431D7C90EA0E5F")) || !TEST_BN_eq(y, z) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_int_eq(EC_GROUP_get_degree(group), 384) || !group_order_tests(group) || !TEST_true(BN_hex2bn(&p, "1FF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, "1FF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC")) || !TEST_true(BN_hex2bn(&b, "051" "953EB9618E1C9A1F929A21A0B68540EE" "A2DA725B99B315F3B8B489918EF109E1" "56193951EC7E937B1652C0BD3BB1BF07" "3573DF883D2C34F1EF451FD46B503F00")) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !TEST_true(BN_hex2bn(&x, "C6" "858E06B70404E9CD9E3ECB662395B442" "9C648139053FB521F828AF606B4D3DBA" "A14B5E77EFE75928FE1DC127A2FFA8DE" "3348B3C1856A429BF97E7E31C2E5BD66")) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, "1FF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA" "51868783BF2F966B7FCC0148F709A5D0" "3BB5C9B8899C47AEBB6FB71E91386409")) || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("NIST curve P-521 -- Generator"); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, "118" "39296A789A3BC0045C8A5FB42C7D1BD9" "98F54449579B446817AFBD17273E662C" "97EE72995EF42640C550B9013FAD0761" "353C7086A272C24088BE94769FD16650")) || !TEST_BN_eq(y, z) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_int_eq(EC_GROUP_get_degree(group), 521) || !group_order_tests(group) || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) || !TEST_true(EC_POINT_copy(Q, P)) || !TEST_false(EC_POINT_is_at_infinity(group, Q)) || !TEST_true(EC_POINT_dbl(group, P, P, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(EC_POINT_invert(group, Q, ctx)) || !TEST_true(EC_POINT_add(group, R, P, Q, ctx)) || !TEST_true(EC_POINT_add(group, R, R, Q, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, R)) || !TEST_false(EC_POINT_is_at_infinity(group, Q))) goto err; #ifndef OPENSSL_NO_DEPRECATED_3_0 TEST_note("combined multiplication ..."); points[0] = Q; points[1] = Q; points[2] = Q; points[3] = Q; if (!TEST_true(EC_GROUP_get_order(group, z, ctx)) || !TEST_true(BN_add(y, z, BN_value_one())) || !TEST_BN_even(y) || !TEST_true(BN_rshift1(y, y))) goto err; scalars[0] = y; scalars[1] = y; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)) || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0)) || !TEST_true(BN_add(z, z, y))) goto err; BN_set_negative(z, 1); scalars[0] = y; scalars[1] = z; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P)) || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0)) || !TEST_true(BN_add(z, x, y))) goto err; BN_set_negative(z, 1); scalars[0] = x; scalars[1] = y; scalars[2] = z; if (!TEST_ptr(scalar3 = BN_new())) goto err; BN_zero(scalar3); scalars[3] = scalar3; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; #endif TEST_note(" ok\n"); r = 1; err: BN_CTX_free(ctx); BN_free(p); BN_free(a); BN_free(b); EC_GROUP_free(group); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); BN_free(x); BN_free(y); BN_free(z); BN_free(yplusone); BN_free(scalar3); return r; } #ifndef OPENSSL_NO_EC2M static struct c2_curve_test { const char *name; const char *p; const char *a; const char *b; const char *x; const char *y; int ybit; const char *order; const char *cof; int degree; } char2_curve_tests[] = { { "NIST curve K-163", "0800000000000000000000000000000000000000C9", "1", "1", "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8", "0289070FB05D38FF58321F2E800536D538CCDAA3D9", 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163 }, { "NIST curve B-163", "0800000000000000000000000000000000000000C9", "1", "020A601907B8C953CA1481EB10512F78744A3205FD", "03F0EBA16286A2D57EA0991168D4994637E8343E36", "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163 }, { "NIST curve K-233", "020000000000000000000000000000000000000004000000000000000001", "0", "1", "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126", "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3", 0, "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF", "4", 233 }, { "NIST curve B-233", "020000000000000000000000000000000000000004000000000000000001", "000000000000000000000000000000000000000000000000000000000001", "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD", "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B", "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052", 1, "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7", "2", 233 }, { "NIST curve K-283", "08000000" "00000000000000000000000000000000000000000000000000000000000010A1", "0", "1", "0503213F" "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836", "01CCDA38" "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259", 0, "01FFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61", "4", 283 }, { "NIST curve B-283", "08000000" "00000000000000000000000000000000000000000000000000000000000010A1", "00000000" "0000000000000000000000000000000000000000000000000000000000000001", "027B680A" "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5", "05F93925" "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053", "03676854" "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4", 1, "03FFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307", "2", 283 }, { "NIST curve K-409", "0200000000000000000000000000000000000000" "0000000000000000000000000000000000000000008000000000000000000001", "0", "1", "0060F05F658F49C1AD3AB1890F7184210EFD0987" "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746", "01E369050B7C4E42ACBA1DACBF04299C3460782F" "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B", 1, "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF", "4", 409 }, { "NIST curve B-409", "0200000000000000000000000000000000000000" "0000000000000000000000000000000000000000008000000000000000000001", "0000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000001", "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E" "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F", "015D4860D088DDB3496B0C6064756260441CDE4A" "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7", "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5" "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706", 1, "0100000000000000000000000000000000000000" "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173", "2", 409 }, { "NIST curve K-571", "800000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000425", "0", "1", "026EB7A859923FBC" "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6" "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972", "0349DC807F4FBF37" "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7" "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3", 0, "0200000000000000" "00000000000000000000000000000000000000000000000000000000131850E1" "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001", "4", 571 }, { "NIST curve B-571", "800000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000425", "0000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000001", "02F40E7E2221F295" "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933" "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A", "0303001D34B85629" "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293" "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19", "037BF27342DA639B" "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57" "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B", 1, "03FFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18" "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47", "2", 571 } }; static int char2_curve_test(int n) { int r = 0; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL; EC_GROUP *group = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL; # ifndef OPENSSL_NO_DEPRECATED_3_0 const EC_POINT *points[3]; const BIGNUM *scalars[3]; # endif struct c2_curve_test *const test = char2_curve_tests + n; if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_ptr(x = BN_new()) || !TEST_ptr(y = BN_new()) || !TEST_ptr(z = BN_new()) || !TEST_ptr(yplusone = BN_new()) || !TEST_true(BN_hex2bn(&p, test->p)) || !TEST_true(BN_hex2bn(&a, test->a)) || !TEST_true(BN_hex2bn(&b, test->b)) || !TEST_true(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx)) || !TEST_ptr(P = EC_POINT_new(group)) || !TEST_ptr(Q = EC_POINT_new(group)) || !TEST_ptr(R = EC_POINT_new(group)) || !TEST_true(BN_hex2bn(&x, test->x)) || !TEST_true(BN_hex2bn(&y, test->y)) || !TEST_true(BN_add(yplusone, y, BN_value_one()))) goto err; # ifdef OPENSSL_EC_BIN_PT_COMP if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, test->y_bit, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, test->order)) || !TEST_true(BN_hex2bn(&cof, test->cof)) || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)) || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; TEST_info("%s -- Generator", test->name); test_output_bignum("x", x); test_output_bignum("y", y); if (!TEST_true(BN_hex2bn(&z, test->y)) || !TEST_BN_eq(y, z)) goto err; # else if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx)) || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(BN_hex2bn(&z, test->order)) || !TEST_true(BN_hex2bn(&cof, test->cof)) || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))) goto err; TEST_info("%s -- Generator:", test->name); test_output_bignum("x", x); test_output_bignum("y", y); # endif if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree) || !group_order_tests(group)) goto err; if (n == OSSL_NELEM(char2_curve_tests) - 1) { if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) || !TEST_true(EC_POINT_copy(Q, P)) || !TEST_false(EC_POINT_is_at_infinity(group, Q)) || !TEST_true(EC_POINT_dbl(group, P, P, ctx)) || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) || !TEST_true(EC_POINT_invert(group, Q, ctx)) || !TEST_true(EC_POINT_add(group, R, P, Q, ctx)) || !TEST_true(EC_POINT_add(group, R, R, Q, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, R)) || !TEST_false(EC_POINT_is_at_infinity(group, Q))) goto err; # ifndef OPENSSL_NO_DEPRECATED_3_0 TEST_note("combined multiplication ..."); points[0] = Q; points[1] = Q; points[2] = Q; if (!TEST_true(BN_add(y, z, BN_value_one())) || !TEST_BN_even(y) || !TEST_true(BN_rshift1(y, y))) goto err; scalars[0] = y; scalars[1] = y; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))) goto err; if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0)) || !TEST_true(BN_add(z, z, y))) goto err; BN_set_negative(z, 1); scalars[0] = y; scalars[1] = z; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0)) || !TEST_true(BN_add(z, x, y))) goto err; BN_set_negative(z, 1); scalars[0] = x; scalars[1] = y; scalars[2] = z; if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; # endif } r = 1; err: BN_CTX_free(ctx); BN_free(p); BN_free(a); BN_free(b); BN_free(x); BN_free(y); BN_free(z); BN_free(yplusone); BN_free(cof); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); EC_GROUP_free(group); return r; } static int char2_field_tests(void) { BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; EC_GROUP *group = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL; BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL; unsigned char buf[100]; size_t len; int k, r = 0; if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_true(BN_hex2bn(&p, "13")) || !TEST_true(BN_hex2bn(&a, "3")) || !TEST_true(BN_hex2bn(&b, "1"))) goto err; if (!TEST_ptr(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx)) || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))) goto err; TEST_info("Curve defined by Weierstrass equation"); TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)"); test_output_bignum("a", a); test_output_bignum("b", b); test_output_bignum("p", p); if (!TEST_ptr(P = EC_POINT_new(group)) || !TEST_ptr(Q = EC_POINT_new(group)) || !TEST_ptr(R = EC_POINT_new(group)) || !TEST_true(EC_POINT_set_to_infinity(group, P)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; buf[0] = 0; if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx)) || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P)) || !TEST_ptr(x = BN_new()) || !TEST_ptr(y = BN_new()) || !TEST_ptr(z = BN_new()) || !TEST_ptr(cof = BN_new()) || !TEST_ptr(yplusone = BN_new()) || !TEST_true(BN_hex2bn(&x, "6")) # ifdef OPENSSL_EC_BIN_PT_COMP || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)) # else || !TEST_true(BN_hex2bn(&y, "8")) || !TEST_true(EC_POINT_set_affine_coordinates(group, Q, x, y, ctx)) # endif ) goto err; if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) { # ifdef OPENSSL_EC_BIN_PT_COMP if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx))) goto err; # endif TEST_info("Point is not on curve"); test_output_bignum("x", x); test_output_bignum("y", y); goto err; } TEST_note("A cyclic subgroup:"); k = 100; do { if (!TEST_int_ne(k--, 0)) goto err; if (EC_POINT_is_at_infinity(group, P)) TEST_note(" point at infinity"); else { if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx))) goto err; test_output_bignum("x", x); test_output_bignum("y", y); } if (!TEST_true(EC_POINT_copy(R, P)) || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))) goto err; } while (!EC_POINT_is_at_infinity(group, P)); if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx)) || !TEST_true(EC_POINT_is_at_infinity(group, P))) goto err; # ifdef OPENSSL_EC_BIN_PT_COMP len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, compressed form:", buf, len); # endif len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, uncompressed form:", buf, len); # ifdef OPENSSL_EC_BIN_PT_COMP len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf), ctx); if (!TEST_size_t_ne(len, 0) || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) goto err; test_output_memory("Generator as octet string, hybrid form:", buf, len); # endif if (!TEST_true(EC_POINT_invert(group, P, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))) goto err; TEST_note("\n"); r = 1; err: BN_CTX_free(ctx); BN_free(p); BN_free(a); BN_free(b); EC_GROUP_free(group); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); BN_free(x); BN_free(y); BN_free(z); BN_free(cof); BN_free(yplusone); return r; } static int hybrid_point_encoding_test(void) { BIGNUM *x = NULL, *y = NULL; EC_GROUP *group = NULL; EC_POINT *point = NULL; unsigned char *buf = NULL; size_t len; int r = 0; if (!TEST_true(BN_dec2bn(&x, "0")) || !TEST_true(BN_dec2bn(&y, "1")) || !TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_sect571k1)) || !TEST_ptr(point = EC_POINT_new(group)) || !TEST_true(EC_POINT_set_affine_coordinates(group, point, x, y, NULL)) || !TEST_size_t_ne(0, (len = EC_POINT_point2oct(group, point, POINT_CONVERSION_HYBRID, NULL, 0, NULL))) || !TEST_ptr(buf = OPENSSL_malloc(len)) || !TEST_size_t_eq(len, EC_POINT_point2oct(group, point, POINT_CONVERSION_HYBRID, buf, len, NULL))) goto err; r = 1; if (!TEST_true(EC_POINT_oct2point(group, point, buf, len, NULL))) r = 0; buf[0] ^= 1; if (!TEST_false(EC_POINT_oct2point(group, point, buf, len, NULL))) r = 0; err: BN_free(x); BN_free(y); EC_GROUP_free(group); EC_POINT_free(point); OPENSSL_free(buf); return r; } #endif static int internal_curve_test(int n) { EC_GROUP *group = NULL; int nid = curves[n].nid; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) { TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n", OBJ_nid2sn(nid)); return 0; } if (!TEST_true(EC_GROUP_check(group, NULL))) { TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid)); EC_GROUP_free(group); return 0; } EC_GROUP_free(group); return 1; } static int internal_curve_test_method(int n) { int r, nid = curves[n].nid; EC_GROUP *group; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) { TEST_info("Curve %s failed\n", OBJ_nid2sn(nid)); return 0; } r = group_order_tests(group); EC_GROUP_free(group); return r; } static int group_field_test(void) { int r = 1; BIGNUM *secp521r1_field = NULL; BIGNUM *sect163r2_field = NULL; EC_GROUP *secp521r1_group = NULL; EC_GROUP *sect163r2_group = NULL; BN_hex2bn(&secp521r1_field, "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFF"); BN_hex2bn(&sect163r2_field, "08000000000000000000000000000000" "00000000C9"); secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1); if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group))) r = 0; # ifndef OPENSSL_NO_EC2M sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2); if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group))) r = 0; # endif EC_GROUP_free(secp521r1_group); EC_GROUP_free(sect163r2_group); BN_free(secp521r1_field); BN_free(sect163r2_field); return r; } struct nistp_test_params { const int nid; int degree; const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d; }; static const struct nistp_test_params nistp_tests_params[] = { { NID_secp224r1, 224, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", }, { NID_X9_62_prime256v1, 256, "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", }, { NID_secp521r1, 521, "1ff" "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "1ff" "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", "051" "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1" "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", "0098" "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e" "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", "0164" "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8" "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", "c6" "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba" "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", "118" "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c" "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", "1ff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa" "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", "0100" "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee" "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", }, }; static int nistp_single_test(int idx) { const struct nistp_test_params *test = nistp_tests_params + idx; BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL; BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL; EC_GROUP *NISTP = NULL; EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL; int r = 0; TEST_note("NIST curve P-%d (optimised implementation):", test->degree); if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_ptr(x = BN_new()) || !TEST_ptr(y = BN_new()) || !TEST_ptr(m = BN_new()) || !TEST_ptr(n = BN_new()) || !TEST_ptr(order = BN_new()) || !TEST_ptr(yplusone = BN_new()) || !TEST_ptr(NISTP = EC_GROUP_new_by_curve_name(test->nid)) || !TEST_true(BN_hex2bn(&p, test->p)) || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL)) || !TEST_true(BN_hex2bn(&a, test->a)) || !TEST_true(BN_hex2bn(&b, test->b)) || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx)) || !TEST_ptr(G = EC_POINT_new(NISTP)) || !TEST_ptr(P = EC_POINT_new(NISTP)) || !TEST_ptr(Q = EC_POINT_new(NISTP)) || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP)) || !TEST_true(BN_hex2bn(&x, test->Qx)) || !TEST_true(BN_hex2bn(&y, test->Qy)) || !TEST_true(BN_add(yplusone, y, BN_value_one())) || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, yplusone, ctx)) || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y, ctx)) || !TEST_true(BN_hex2bn(&x, test->Gx)) || !TEST_true(BN_hex2bn(&y, test->Gy)) || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx)) || !TEST_true(BN_hex2bn(&order, test->order)) || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree)) goto err; TEST_note("NIST test vectors ... "); if (!TEST_true(BN_hex2bn(&n, test->d))) goto err; EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) goto err; EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx)) || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) || !TEST_true(BN_rshift(m, n, 1))) goto err; EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) goto err; EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) #ifndef OPENSSL_NO_DEPRECATED_3_0 || !TEST_false(EC_GROUP_have_precompute_mult(NISTP)) || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx)) #endif ) goto err; EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) goto err; EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))) goto err; EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) goto err; EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) goto err; if (!TEST_true(BN_set_word(m, 32)) || !TEST_true(BN_set_word(n, 31)) || !TEST_true(EC_POINT_copy(P, G)) || !TEST_true(EC_POINT_invert(NISTP, P, ctx)) || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx)) || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx))) goto err; r = 1; err: EC_GROUP_free(NISTP); EC_POINT_free(G); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(Q_CHECK); BN_free(n); BN_free(m); BN_free(p); BN_free(a); BN_free(b); BN_free(x); BN_free(y); BN_free(order); BN_free(yplusone); BN_CTX_free(ctx); return r; } static const unsigned char p521_named[] = { 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, }; static const unsigned char p521_explicit[] = { 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 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, 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, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 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, 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, 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00, 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17, 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04, 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f, 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff, 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50, 0x02, 0x42, 0x01, 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, 0xfa, 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae, 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01, }; static int check_named_curve_test(int id) { int ret = 0, nid, field_nid, has_seed; EC_GROUP *group = NULL, *gtest = NULL; const EC_POINT *group_gen = NULL; EC_POINT *other_gen = NULL; BIGNUM *group_p = NULL, *group_a = NULL, *group_b = NULL; BIGNUM *other_p = NULL, *other_a = NULL, *other_b = NULL; BIGNUM *group_cofactor = NULL, *other_cofactor = NULL; BIGNUM *other_order = NULL; const BIGNUM *group_order = NULL; BN_CTX *bn_ctx = NULL; static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED"; static size_t invalid_seed_len = sizeof(invalid_seed); nid = curves[id].nid; if (!TEST_ptr(bn_ctx = BN_CTX_new()) || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(gtest = EC_GROUP_dup(group)) || !TEST_ptr(group_p = BN_new()) || !TEST_ptr(group_a = BN_new()) || !TEST_ptr(group_b = BN_new()) || !TEST_ptr(group_cofactor = BN_new()) || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group)) || !TEST_ptr(group_order = EC_GROUP_get0_order(group)) || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL)) || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL)) || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group)) || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL)) || !TEST_ptr(other_order = BN_dup(group_order)) || !TEST_true(BN_add_word(other_order, 1)) || !TEST_ptr(other_a = BN_dup(group_a)) || !TEST_true(BN_add_word(other_a, 1)) || !TEST_ptr(other_b = BN_dup(group_b)) || !TEST_true(BN_add_word(other_b, 1)) || !TEST_ptr(other_cofactor = BN_dup(group_cofactor)) || !TEST_true(BN_add_word(other_cofactor, 1))) goto err; has_seed = (EC_GROUP_get_seed_len(group) > 0); field_nid = EC_GROUP_get_field_type(group); if (field_nid == NID_X9_62_characteristic_two_field) { if (!TEST_ptr(other_p = BN_dup(group_p)) || !TEST_true(BN_lshift1(other_p, other_p))) goto err; } else { if (!TEST_ptr(other_p = BN_dup(group_p))) goto err; if (!TEST_ptr(BN_copy(other_p, BN_ucmp(BN_get0_nist_prime_192(), other_p) == 0 ? BN_get0_nist_prime_256() : BN_get0_nist_prime_192()))) goto err; } if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid) || !TEST_int_eq(EC_GROUP_check_named_curve(group, 1, NULL), EC_curve_nid2nist(nid) != NULL ? nid : NID_undef)) goto err; EC_GROUP_set_curve_name(group, nid + 1); ERR_set_mark(); if (!TEST_int_le(EC_GROUP_check_named_curve(group, 0, NULL), 0)) goto err; ERR_pop_to_mark(); EC_GROUP_set_curve_name(group, nid); if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)) goto err; if (!TEST_int_eq(EC_GROUP_set_seed(group, invalid_seed, invalid_seed_len), invalid_seed_len)) goto err; if (has_seed) { if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), 0)) goto err; } else { if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)) goto err; } if (!TEST_int_eq(EC_GROUP_set_seed(group, NULL, 0), 1) || !TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)) goto err; if (!TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)) goto err; if (!TEST_true(EC_GROUP_set_generator(gtest, other_gen, group_order, group_cofactor)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0) || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, other_order, group_cofactor)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0) || !TEST_false(EC_GROUP_set_generator(gtest, group_gen, NULL, group_cofactor)) || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order, other_cofactor)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0) || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order, NULL)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid) || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order, group_cofactor)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)) goto err; ERR_set_mark(); if (EC_GROUP_set_curve(gtest, other_p, group_a, group_b, NULL)) { if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)) goto err; } else { ERR_pop_to_mark(); ERR_set_mark(); } if (EC_GROUP_set_curve(gtest, group_p, other_a, group_b, NULL)) { if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)) goto err; } else { ERR_pop_to_mark(); ERR_set_mark(); } if (EC_GROUP_set_curve(gtest, group_p, group_a, other_b, NULL)) { if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)) goto err; } else { ERR_pop_to_mark(); ERR_set_mark(); } ERR_pop_to_mark(); if (!TEST_true(EC_GROUP_set_curve(gtest, group_p, group_a, group_b, NULL)) || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)) goto err; ret = 1; err: BN_free(group_p); BN_free(other_p); BN_free(group_a); BN_free(other_a); BN_free(group_b); BN_free(other_b); BN_free(group_cofactor); BN_free(other_cofactor); BN_free(other_order); EC_POINT_free(other_gen); EC_GROUP_free(gtest); EC_GROUP_free(group); BN_CTX_free(bn_ctx); return ret; } static int check_named_curve_lookup_test(int id) { int ret = 0, nid, rv = 0; EC_GROUP *g = NULL , *ga = NULL; ECPARAMETERS *p = NULL, *pa = NULL; BN_CTX *ctx = NULL; nid = curves[id].nid; if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(g = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(p = EC_GROUP_get_ecparameters(g, NULL))) goto err; EC_GROUP_free(g); if (!TEST_ptr(g = EC_GROUP_new_from_ecparameters(p))) goto err; if (!TEST_int_gt(rv = EC_GROUP_check_named_curve(g, 0, NULL), 0)) goto err; if (rv != nid) { if (!TEST_ptr(ga = EC_GROUP_new_by_curve_name(rv)) || !TEST_ptr(pa = EC_GROUP_get_ecparameters(ga, NULL))) goto err; EC_GROUP_free(ga); if (!TEST_ptr(ga = EC_GROUP_new_from_ecparameters(pa)) || !TEST_int_eq(EC_GROUP_cmp(g, ga, ctx), 0)) goto err; } ret = 1; err: EC_GROUP_free(g); EC_GROUP_free(ga); ECPARAMETERS_free(p); ECPARAMETERS_free(pa); BN_CTX_free(ctx); return ret; } static ossl_inline int are_ec_nids_compatible(int n1d, int n2d) { int ret = 0; switch (n1d) { #ifndef OPENSSL_NO_EC2M case NID_sect113r1: case NID_wap_wsg_idm_ecid_wtls4: ret = (n2d == NID_sect113r1 || n2d == NID_wap_wsg_idm_ecid_wtls4); break; case NID_sect163k1: case NID_wap_wsg_idm_ecid_wtls3: ret = (n2d == NID_sect163k1 || n2d == NID_wap_wsg_idm_ecid_wtls3); break; case NID_sect233k1: case NID_wap_wsg_idm_ecid_wtls10: ret = (n2d == NID_sect233k1 || n2d == NID_wap_wsg_idm_ecid_wtls10); break; case NID_sect233r1: case NID_wap_wsg_idm_ecid_wtls11: ret = (n2d == NID_sect233r1 || n2d == NID_wap_wsg_idm_ecid_wtls11); break; case NID_X9_62_c2pnb163v1: case NID_wap_wsg_idm_ecid_wtls5: ret = (n2d == NID_X9_62_c2pnb163v1 || n2d == NID_wap_wsg_idm_ecid_wtls5); break; #endif case NID_secp112r1: case NID_wap_wsg_idm_ecid_wtls6: ret = (n2d == NID_secp112r1 || n2d == NID_wap_wsg_idm_ecid_wtls6); break; case NID_secp160r2: case NID_wap_wsg_idm_ecid_wtls7: ret = (n2d == NID_secp160r2 || n2d == NID_wap_wsg_idm_ecid_wtls7); break; #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128 case NID_secp224r1: case NID_wap_wsg_idm_ecid_wtls12: ret = (n2d == NID_secp224r1 || n2d == NID_wap_wsg_idm_ecid_wtls12); break; #else case NID_wap_wsg_idm_ecid_wtls12: ret = (n2d == NID_secp224r1); break; #endif default: ret = (n1d == n2d); } return ret; } static int check_named_curve_from_ecparameters(int id) { int ret = 0, nid, tnid; EC_GROUP *group = NULL, *tgroup = NULL, *tmpg = NULL; const EC_POINT *group_gen = NULL; EC_POINT *other_gen = NULL; BIGNUM *group_cofactor = NULL, *other_cofactor = NULL; BIGNUM *other_gen_x = NULL, *other_gen_y = NULL; const BIGNUM *group_order = NULL; BIGNUM *other_order = NULL; BN_CTX *bn_ctx = NULL; static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED"; static size_t invalid_seed_len = sizeof(invalid_seed); ECPARAMETERS *params = NULL, *other_params = NULL; EC_GROUP *g_ary[8] = {NULL}; EC_GROUP **g_next = &g_ary[0]; ECPARAMETERS *p_ary[8] = {NULL}; ECPARAMETERS **p_next = &p_ary[0]; nid = curves[id].nid; TEST_note("Curve %s", OBJ_nid2sn(nid)); if (!TEST_ptr(bn_ctx = BN_CTX_new())) return ret; BN_CTX_start(bn_ctx); if ( !TEST_ptr(group_cofactor = BN_CTX_get(bn_ctx)) || !TEST_ptr(other_gen_x = BN_CTX_get(bn_ctx)) || !TEST_ptr(other_gen_y = BN_CTX_get(bn_ctx)) || !TEST_ptr(other_order = BN_CTX_get(bn_ctx)) || !TEST_ptr(other_cofactor = BN_CTX_get(bn_ctx)) || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(params = EC_GROUP_get_ecparameters(group, NULL)) || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group)) || !TEST_ptr(group_order = EC_GROUP_get0_order(group)) || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL)) || !TEST_ptr(tmpg = EC_GROUP_dup(group)) || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group)) || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL)) || !TEST_true(EC_POINT_get_affine_coordinates(group, other_gen, other_gen_x, other_gen_y, bn_ctx)) || !TEST_true(BN_copy(other_order, group_order)) || !TEST_true(BN_add_word(other_order, 1)) || !TEST_true(BN_copy(other_cofactor, group_cofactor)) || !TEST_true(BN_add_word(other_cofactor, 1))) goto err; EC_POINT_free(other_gen); other_gen = NULL; if (!TEST_ptr(other_gen = EC_POINT_new(tmpg)) || !TEST_true(EC_POINT_set_affine_coordinates(tmpg, other_gen, other_gen_x, other_gen_y, bn_ctx))) goto err; if (!TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)) goto err; if (!TEST_true(are_ec_nids_compatible(nid, tnid))) { TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid)); goto err; } if (!TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE)) goto err; if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, invalid_seed, invalid_seed_len), invalid_seed_len) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(are_ec_nids_compatible(nid, tnid)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE)) { TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid)); goto err; } if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, NULL, 0), 1) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(are_ec_nids_compatible(nid, tnid)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE)) { TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid)); goto err; } if ( !TEST_true(EC_GROUP_set_generator(tmpg, other_gen, group_order, group_cofactor)) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, other_order, group_cofactor)) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_false(EC_GROUP_set_generator(tmpg, group_gen, NULL, group_cofactor)) || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order, other_cofactor)) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(are_ec_nids_compatible(nid, tnid)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order, NULL)) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(are_ec_nids_compatible(nid, tnid)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order, group_cofactor)) || !TEST_ptr(other_params = *p_next++ = EC_GROUP_get_ecparameters(tmpg, NULL)) || !TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(other_params)) || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef) || !TEST_true(are_ec_nids_compatible(nid, tnid)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE)) goto err; ret = 1; err: for (g_next = &g_ary[0]; g_next < g_ary + OSSL_NELEM(g_ary); g_next++) EC_GROUP_free(*g_next); for (p_next = &p_ary[0]; p_next < p_ary + OSSL_NELEM(g_ary); p_next++) ECPARAMETERS_free(*p_next); ECPARAMETERS_free(params); EC_POINT_free(other_gen); EC_GROUP_free(tmpg); EC_GROUP_free(group); BN_CTX_end(bn_ctx); BN_CTX_free(bn_ctx); return ret; } static int parameter_test(void) { EC_GROUP *group = NULL, *group2 = NULL; ECPARAMETERS *ecparameters = NULL; unsigned char *buf = NULL; int r = 0, len; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp384r1)) || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL)) || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters)) || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0)) goto err; EC_GROUP_free(group); group = NULL; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1)) || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0) || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named))) goto err; OPENSSL_free(buf); buf = NULL; EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE); if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0) || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit))) goto err; r = 1; err: EC_GROUP_free(group); EC_GROUP_free(group2); ECPARAMETERS_free(ecparameters); OPENSSL_free(buf); return r; } static int ossl_parameter_test(void) { EC_GROUP *group_nmd = NULL, *group_nmd2 = NULL, *group_nmd3 = NULL; EC_GROUP *group_exp = NULL, *group_exp2 = NULL; OSSL_PARAM *params_nmd = NULL, *params_nmd2 = NULL; OSSL_PARAM *params_exp = NULL, *params_exp2 = NULL; unsigned char *buf = NULL, *buf2 = NULL; BN_CTX *bn_ctx = NULL; OSSL_PARAM_BLD *bld = NULL; BIGNUM *p, *a, *b; const EC_POINT *group_gen = NULL; size_t bsize; int r = 0; if (!TEST_ptr(bn_ctx = BN_CTX_new())) goto err; if (!TEST_ptr(group_nmd = EC_GROUP_new_by_curve_name(NID_secp384r1)) || !TEST_ptr(params_nmd = EC_GROUP_to_params( group_nmd, NULL, NULL, NULL)) || !TEST_ptr(group_nmd2 = EC_GROUP_new_from_params( params_nmd, NULL, NULL)) || !TEST_int_eq(EC_GROUP_cmp(group_nmd, group_nmd2, NULL), 0) || !TEST_ptr(params_nmd2 = EC_GROUP_to_params( group_nmd, NULL, NULL, bn_ctx)) || !TEST_ptr(group_nmd3 = EC_GROUP_new_from_params( params_nmd2, NULL, NULL)) || !TEST_int_eq(EC_GROUP_cmp(group_nmd, group_nmd3, NULL), 0)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; BN_CTX_start(bn_ctx); p = BN_CTX_get(bn_ctx); a = BN_CTX_get(bn_ctx); b = BN_CTX_get(bn_ctx); if (!TEST_true(EC_GROUP_get_curve(group_nmd, p, a, b, bn_ctx)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string( bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE, SN_X9_62_prime_field, 0)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b))) goto err; if (EC_GROUP_get0_seed(group_nmd) != NULL) { if (!TEST_true(OSSL_PARAM_BLD_push_octet_string( bld, OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group_nmd), EC_GROUP_get_seed_len(group_nmd)))) goto err; } if (EC_GROUP_get0_cofactor(group_nmd) != NULL) { if (!TEST_true(OSSL_PARAM_BLD_push_BN( bld, OSSL_PKEY_PARAM_EC_COFACTOR, EC_GROUP_get0_cofactor(group_nmd)))) goto err; } if (!TEST_ptr(group_gen = EC_GROUP_get0_generator(group_nmd)) || !TEST_size_t_gt(bsize = EC_POINT_point2oct( group_nmd, EC_GROUP_get0_generator(group_nmd), POINT_CONVERSION_UNCOMPRESSED, NULL, 0, bn_ctx), 0) || !TEST_ptr(buf2 = OPENSSL_malloc(bsize)) || !TEST_size_t_eq(EC_POINT_point2oct( group_nmd, EC_GROUP_get0_generator(group_nmd), POINT_CONVERSION_UNCOMPRESSED, buf2, bsize, bn_ctx), bsize) || !TEST_true(OSSL_PARAM_BLD_push_octet_string( bld, OSSL_PKEY_PARAM_EC_GENERATOR, buf2, bsize)) || !TEST_true(OSSL_PARAM_BLD_push_BN( bld, OSSL_PKEY_PARAM_EC_ORDER, EC_GROUP_get0_order(group_nmd)))) goto err; if (!TEST_ptr(params_exp = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(group_exp = EC_GROUP_new_from_params(params_exp, NULL, NULL)) || !TEST_ptr(params_exp2 = EC_GROUP_to_params(group_exp, NULL, NULL, NULL)) || !TEST_ptr(group_exp2 = EC_GROUP_new_from_params(params_exp2, NULL, NULL)) || !TEST_int_eq(EC_GROUP_cmp(group_exp, group_exp2, NULL), 0)) goto err; r = 1; err: EC_GROUP_free(group_nmd); EC_GROUP_free(group_nmd2); EC_GROUP_free(group_nmd3); OSSL_PARAM_free(params_nmd); OSSL_PARAM_free(params_nmd2); OPENSSL_free(buf); EC_GROUP_free(group_exp); EC_GROUP_free(group_exp2); BN_CTX_end(bn_ctx); BN_CTX_free(bn_ctx); OPENSSL_free(buf2); OSSL_PARAM_BLD_free(bld); OSSL_PARAM_free(params_exp); OSSL_PARAM_free(params_exp2); return r; } static const unsigned char params_cf_pass[] = { 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xe5, 0x00, 0x1f, 0xc5, 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d, 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93, 0x44, 0x88, 0xe6, 0x91, 0x30, 0x44, 0x04, 0x20, 0xe5, 0x00, 0x1f, 0xc5, 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d, 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93, 0x44, 0x88, 0xe6, 0x8e, 0x04, 0x20, 0x18, 0x8c, 0x59, 0x57, 0xc4, 0xbc, 0x85, 0x57, 0xc3, 0x66, 0x9f, 0x89, 0xd5, 0x92, 0x0d, 0x7e, 0x42, 0x27, 0x07, 0x64, 0xaa, 0x26, 0xed, 0x89, 0xc4, 0x09, 0x05, 0x4d, 0xc7, 0x23, 0x47, 0xda, 0x04, 0x41, 0x04, 0x1b, 0x6b, 0x41, 0x0b, 0xf9, 0xfb, 0x77, 0xfd, 0x50, 0xb7, 0x3e, 0x23, 0xa3, 0xec, 0x9a, 0x3b, 0x09, 0x31, 0x6b, 0xfa, 0xf6, 0xce, 0x1f, 0xff, 0xeb, 0x57, 0x93, 0x24, 0x70, 0xf3, 0xf4, 0xba, 0x7e, 0xfa, 0x86, 0x6e, 0x19, 0x89, 0xe3, 0x55, 0x6d, 0x5a, 0xe9, 0xc0, 0x3d, 0xbc, 0xfb, 0xaf, 0xad, 0xd4, 0x7e, 0xa6, 0xe5, 0xfa, 0x1a, 0x58, 0x07, 0x9e, 0x8f, 0x0d, 0x3b, 0xf7, 0x38, 0xca, 0x02, 0x11, 0x0c, 0x38, 0xd9, 0x6a, 0x9f, 0x89, 0x2b, 0x88, 0x77, 0x2e, 0xc2, 0xe3, 0x96, 0x14, 0xa8, 0x2f, 0x4f }; static const unsigned char params_cf_fail[] = { 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xc8, 0x95, 0x27, 0x37, 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b, 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0, 0x33, 0xc2, 0xea, 0x13, 0x30, 0x44, 0x04, 0x20, 0xc8, 0x95, 0x27, 0x37, 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b, 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0, 0x33, 0xc2, 0xea, 0x10, 0x04, 0x20, 0xbf, 0xa6, 0xa8, 0x05, 0x1d, 0x09, 0xac, 0x70, 0x39, 0xbb, 0x4d, 0xb2, 0x90, 0x8a, 0x15, 0x41, 0x14, 0x1d, 0x11, 0x86, 0x9f, 0x13, 0xa2, 0x63, 0x1a, 0xda, 0x95, 0x22, 0x4d, 0x02, 0x15, 0x0a, 0x04, 0x41, 0x04, 0xaf, 0x16, 0x71, 0xf9, 0xc4, 0xc8, 0x59, 0x1d, 0xa3, 0x6f, 0xe7, 0xc3, 0x57, 0xa1, 0xfa, 0x9f, 0x49, 0x7c, 0x11, 0x27, 0x05, 0xa0, 0x7f, 0xff, 0xf9, 0xe0, 0xe7, 0x92, 0xdd, 0x9c, 0x24, 0x8e, 0xc7, 0xb9, 0x52, 0x71, 0x3f, 0xbc, 0x7f, 0x6a, 0x9f, 0x35, 0x70, 0xe1, 0x27, 0xd5, 0x35, 0x8a, 0x13, 0xfa, 0xa8, 0x33, 0x3e, 0xd4, 0x73, 0x1c, 0x14, 0x58, 0x9e, 0xc7, 0x0a, 0x87, 0x65, 0x8d, 0x02, 0x11, 0x04, 0x5a, 0x75, 0xc0, 0xc1, 0x72, 0x28, 0xeb, 0xd9, 0xb1, 0x69, 0xa1, 0x0e, 0x34, 0xa2, 0x21, 0x01 }; static int cofactor_range_test(void) { EC_GROUP *group = NULL; BIGNUM *cf = NULL; int ret = 0; const unsigned char *b1 = (const unsigned char *)params_cf_fail; const unsigned char *b2 = (const unsigned char *)params_cf_pass; if (!TEST_ptr(group = d2i_ECPKParameters(NULL, &b1, sizeof(params_cf_fail))) || !TEST_BN_eq_zero(EC_GROUP_get0_cofactor(group)) || !TEST_ptr(group = d2i_ECPKParameters(&group, &b2, sizeof(params_cf_pass))) || !TEST_int_gt(BN_hex2bn(&cf, "12bc94785251297abfafddf1565100da"), 0) || !TEST_BN_eq(cf, EC_GROUP_get0_cofactor(group))) goto err; ret = 1; err: BN_free(cf); EC_GROUP_free(group); return ret; } static int cardinality_test(int n) { int ret = 0, is_binary = 0; int nid = curves[n].nid; BN_CTX *ctx = NULL; EC_GROUP *g1 = NULL, *g2 = NULL; EC_POINT *g2_gen = NULL; BIGNUM *g1_p = NULL, *g1_a = NULL, *g1_b = NULL, *g1_x = NULL, *g1_y = NULL, *g1_order = NULL, *g1_cf = NULL, *g2_cf = NULL; TEST_info("Curve %s cardinality test", OBJ_nid2sn(nid)); if (!TEST_ptr(ctx = BN_CTX_new()) || !TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) { BN_CTX_free(ctx); return 0; } is_binary = (EC_GROUP_get_field_type(g1) == NID_X9_62_characteristic_two_field); BN_CTX_start(ctx); g1_p = BN_CTX_get(ctx); g1_a = BN_CTX_get(ctx); g1_b = BN_CTX_get(ctx); g1_x = BN_CTX_get(ctx); g1_y = BN_CTX_get(ctx); g1_order = BN_CTX_get(ctx); g1_cf = BN_CTX_get(ctx); if (!TEST_ptr(g2_cf = BN_CTX_get(ctx)) || !TEST_true(EC_GROUP_get_curve(g1, g1_p, g1_a, g1_b, ctx)) || !TEST_true(EC_POINT_get_affine_coordinates(g1, EC_GROUP_get0_generator(g1), g1_x, g1_y, ctx)) || !TEST_true(BN_copy(g1_order, EC_GROUP_get0_order(g1))) || !TEST_true(EC_GROUP_get_cofactor(g1, g1_cf, ctx)) #ifndef OPENSSL_NO_EC2M || !TEST_ptr(g2 = (is_binary) ? EC_GROUP_new_curve_GF2m(g1_p, g1_a, g1_b, ctx) : EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx)) #else || !TEST_int_eq(0, is_binary) || !TEST_ptr(g2 = EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx)) #endif || !TEST_ptr(g2_gen = EC_POINT_new(g2)) || !TEST_true(EC_POINT_set_affine_coordinates(g2, g2_gen, g1_x, g1_y, ctx)) || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)) || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx)) || !TEST_BN_eq(g1_cf, g2_cf) || !TEST_true(BN_set_word(g2_cf, 0)) || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf)) || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx)) || !TEST_BN_eq(g1_cf, g2_cf) || !TEST_true(BN_set_word(g2_cf, 0)) || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one())) || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf)) || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, NULL, NULL)) || !TEST_true(BN_set_word(g1_order, 0)) || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)) || !TEST_true(BN_set_word(g2_cf, 0)) || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one())) || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)) || !TEST_true(BN_lshift(g1_order, g1_p, 2)) || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))) goto err; ret = 1; err: EC_POINT_free(g2_gen); EC_GROUP_free(g1); EC_GROUP_free(g2); BN_CTX_end(ctx); BN_CTX_free(ctx); return ret; } static int check_ec_key_field_public_range_test(int id) { int ret = 0, type = 0; const EC_POINT *pub = NULL; const EC_GROUP *group = NULL; const BIGNUM *field = NULL; BIGNUM *x = NULL, *y = NULL; EC_KEY *key = NULL; if (!TEST_ptr(x = BN_new()) || !TEST_ptr(y = BN_new()) || !TEST_ptr(key = EC_KEY_new_by_curve_name(curves[id].nid)) || !TEST_ptr(group = EC_KEY_get0_group(key)) || !TEST_ptr(field = EC_GROUP_get0_field(group)) || !TEST_int_gt(EC_KEY_generate_key(key), 0) || !TEST_int_gt(EC_KEY_check_key(key), 0) || !TEST_ptr(pub = EC_KEY_get0_public_key(key)) || !TEST_int_gt(EC_POINT_get_affine_coordinates(group, pub, x, y, NULL), 0)) goto err; type = EC_GROUP_get_field_type(group); #ifndef OPENSSL_NO_EC2M if (type == NID_X9_62_characteristic_two_field) { if (!TEST_true(BN_GF2m_add(x, x, field))) goto err; } else #endif if (type == NID_X9_62_prime_field) { if (!TEST_true(BN_add(x, x, field))) goto err; } else { TEST_error("Unsupported EC_METHOD field_type"); goto err; } if (!TEST_int_le(EC_KEY_set_public_key_affine_coordinates(key, x, y), 0)) goto err; ret = 1; err: BN_free(x); BN_free(y); EC_KEY_free(key); return ret; } static ossl_inline int ec_point_hex2point_test_helper(const EC_GROUP *group, const EC_POINT *P, point_conversion_form_t form, BN_CTX *bnctx) { int ret = 0; EC_POINT *Q = NULL, *Pinf = NULL; char *hex = NULL; if (P == NULL) { if (!TEST_ptr(Pinf = EC_POINT_new(group)) || !TEST_true(EC_POINT_set_to_infinity(group, Pinf))) goto err; P = Pinf; } if (!TEST_ptr(hex = EC_POINT_point2hex(group, P, form, bnctx)) || !TEST_ptr(Q = EC_POINT_hex2point(group, hex, NULL, bnctx)) || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, bnctx))) goto err; if (Pinf != NULL && !TEST_true(EC_POINT_is_at_infinity(group, Q))) goto err; ret = 1; err: EC_POINT_free(Pinf); OPENSSL_free(hex); EC_POINT_free(Q); return ret; } static int ec_point_hex2point_test(int id) { int ret = 0, nid; EC_GROUP *group = NULL; const EC_POINT *G = NULL; EC_POINT *P = NULL; BN_CTX *bnctx = NULL; nid = curves[id].nid; if (!TEST_ptr(bnctx = BN_CTX_new()) || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(G = EC_GROUP_get0_generator(group)) || !TEST_ptr(P = EC_POINT_dup(G, group))) goto err; if (!TEST_true(ec_point_hex2point_test_helper(group, P, POINT_CONVERSION_COMPRESSED, bnctx)) || !TEST_true(ec_point_hex2point_test_helper(group, NULL, POINT_CONVERSION_COMPRESSED, bnctx)) || !TEST_true(ec_point_hex2point_test_helper(group, P, POINT_CONVERSION_UNCOMPRESSED, bnctx)) || !TEST_true(ec_point_hex2point_test_helper(group, NULL, POINT_CONVERSION_UNCOMPRESSED, bnctx)) || !TEST_true(ec_point_hex2point_test_helper(group, P, POINT_CONVERSION_HYBRID, bnctx)) || !TEST_true(ec_point_hex2point_test_helper(group, NULL, POINT_CONVERSION_HYBRID, bnctx))) goto err; ret = 1; err: EC_POINT_free(P); EC_GROUP_free(group); BN_CTX_free(bnctx); return ret; } static int do_test_custom_explicit_fromdata(EC_GROUP *group, BN_CTX *ctx, unsigned char *gen, int gen_size) { int ret = 0, i_out; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkeyparam = NULL; OSSL_PARAM_BLD *bld = NULL; const char *field_name; OSSL_PARAM *params = NULL; const OSSL_PARAM *gettable; BIGNUM *p, *a, *b; BIGNUM *p_out = NULL, *a_out = NULL, *b_out = NULL; BIGNUM *order_out = NULL, *cofactor_out = NULL; char name[80]; unsigned char buf[1024]; size_t buf_len, name_len; #ifndef OPENSSL_NO_EC2M unsigned int k1 = 0, k2 = 0, k3 = 0; const char *basis_name = NULL; #endif p = BN_CTX_get(ctx); a = BN_CTX_get(ctx); b = BN_CTX_get(ctx); if (!TEST_ptr(b) || !TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) { field_name = SN_X9_62_prime_field; } else { field_name = SN_X9_62_characteristic_two_field; #ifndef OPENSSL_NO_EC2M if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) { basis_name = SN_X9_62_tpBasis; if (!TEST_true(EC_GROUP_get_trinomial_basis(group, &k1))) goto err; } else { basis_name = SN_X9_62_ppBasis; if (!TEST_true(EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))) goto err; } #endif } if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE, field_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b))) goto err; if (EC_GROUP_get0_seed(group) != NULL) { if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group), EC_GROUP_get_seed_len(group)))) goto err; } if (EC_GROUP_get0_cofactor(group) != NULL) { if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, EC_GROUP_get0_cofactor(group)))) goto err; } if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_size)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, EC_GROUP_get0_order(group)))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkeyparam, EVP_PKEY_KEY_PARAMETERS, params), 0)) goto err; if (!TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam, OSSL_PKEY_PARAM_GROUP_NAME, name, sizeof(name), &name_len))) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_ENCODING, name, sizeof(name), &name_len)) || !TEST_str_eq(name, OSSL_PKEY_EC_ENCODING_EXPLICIT)) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_FIELD_TYPE, name, sizeof(name), &name_len)) || !TEST_str_eq(name, field_name)) goto err; if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_GENERATOR, buf, sizeof(buf), &buf_len)) || !TEST_mem_eq(buf, (int)buf_len, gen, gen_size)) goto err; if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_P, &p_out)) || !TEST_BN_eq(p_out, p) || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_A, &a_out)) || !TEST_BN_eq(a_out, a) || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_B, &b_out)) || !TEST_BN_eq(b_out, b) || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_ORDER, &order_out)) || !TEST_BN_eq(order_out, EC_GROUP_get0_order(group))) goto err; if (EC_GROUP_get0_cofactor(group) != NULL) { if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_COFACTOR, &cofactor_out)) || !TEST_BN_eq(cofactor_out, EC_GROUP_get0_cofactor(group))) goto err; } if (EC_GROUP_get0_seed(group) != NULL) { if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_SEED, buf, sizeof(buf), &buf_len)) || !TEST_mem_eq(buf, buf_len, EC_GROUP_get0_seed(group), EC_GROUP_get_seed_len(group))) goto err; } if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) { if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out)) || !TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name), &name_len))) goto err; } else { #ifndef OPENSSL_NO_EC2M if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out)) || !TEST_int_eq(EC_GROUP_get_degree(group), i_out) || !TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name), &name_len)) || !TEST_str_eq(name, basis_name)) goto err; if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) { if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out)) || !TEST_int_eq(k1, i_out) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out)) || !TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))) goto err; } else { if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out)) || !TEST_true(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out)) || !TEST_int_eq(k1, i_out) || !TEST_true(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out)) || !TEST_int_eq(k2, i_out) || !TEST_true(EVP_PKEY_get_int_param(pkeyparam, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out)) || !TEST_int_eq(k3, i_out)) goto err; } #endif } if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkeyparam)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ENCODING)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_FIELD_TYPE)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_P)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_A)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_B)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_GENERATOR)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ORDER)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_COFACTOR)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_SEED)) #ifndef OPENSSL_NO_EC2M || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_M)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TYPE)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3)) #endif ) goto err; ret = 1; err: BN_free(order_out); BN_free(cofactor_out); BN_free(a_out); BN_free(b_out); BN_free(p_out); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(pkeyparam); EVP_PKEY_CTX_free(pctx); return ret; } static int custom_generator_test(int id) { int ret = 0, nid, bsize; EC_GROUP *group = NULL; EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL; BN_CTX *ctx = NULL; BIGNUM *k = NULL; unsigned char *b1 = NULL, *b2 = NULL; nid = curves[id].nid; TEST_note("Curve %s", OBJ_nid2sn(nid)); if (!TEST_ptr(ctx = BN_CTX_new())) return 0; BN_CTX_start(ctx); if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) goto err; bsize = (EC_GROUP_get_degree(group) + 7) / 8; bsize = 1 + 2 * bsize; if (!TEST_ptr(k = BN_CTX_get(ctx)) || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) || !TEST_true(BN_clear_bit(k, 0)) || !TEST_ptr(G2 = EC_POINT_new(group)) || !TEST_ptr(Q1 = EC_POINT_new(group)) || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx)) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(b1 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, b1, bsize, ctx), bsize) || !TEST_true(EC_POINT_dbl(group, G2, EC_GROUP_get0_generator(group), ctx)) || !TEST_true(EC_GROUP_set_generator(group, G2, EC_GROUP_get0_order(group), EC_GROUP_get0_cofactor(group))) || !TEST_ptr(Q2 = EC_POINT_new(group)) || !TEST_true(BN_rshift1(k, k)) || !TEST_true(EC_POINT_mul(group, Q2, k, NULL, NULL, ctx)) || !TEST_int_eq(EC_POINT_point2oct(group, Q2, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(b2 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(group, Q2, POINT_CONVERSION_UNCOMPRESSED, b2, bsize, ctx), bsize) || !TEST_mem_eq(b1, bsize, b2, bsize)) goto err; if (!do_test_custom_explicit_fromdata(group, ctx, b1, bsize)) goto err; ret = 1; err: EC_POINT_free(Q1); EC_POINT_free(Q2); EC_POINT_free(G2); EC_GROUP_free(group); BN_CTX_end(ctx); BN_CTX_free(ctx); OPENSSL_free(b1); OPENSSL_free(b2); return ret; } static int custom_params_test(int id) { int ret = 0, nid, bsize; const char *curve_name = NULL; EC_GROUP *group = NULL, *altgroup = NULL; EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL; const EC_POINT *Q = NULL; BN_CTX *ctx = NULL; BIGNUM *k = NULL; unsigned char *buf1 = NULL, *buf2 = NULL; const BIGNUM *z = NULL, *cof = NULL, *priv1 = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; int is_prime = 0; EC_KEY *eckey1 = NULL, *eckey2 = NULL; EVP_PKEY *pkey1 = NULL, *pkey2 = NULL; EVP_PKEY_CTX *pctx1 = NULL, *pctx2 = NULL; size_t sslen, t; unsigned char *pub1 = NULL , *pub2 = NULL; OSSL_PARAM_BLD *param_bld = NULL; OSSL_PARAM *params1 = NULL, *params2 = NULL; nid = curves[id].nid; curve_name = OBJ_nid2sn(nid); TEST_note("Curve %s", curve_name); if (nid == NID_sm2) return TEST_skip("custom params not supported with SM2"); if (!TEST_ptr(ctx = BN_CTX_new())) return 0; BN_CTX_start(ctx); if (!TEST_ptr(p = BN_CTX_get(ctx)) || !TEST_ptr(a = BN_CTX_get(ctx)) || !TEST_ptr(b = BN_CTX_get(ctx)) || !TEST_ptr(k = BN_CTX_get(ctx))) goto err; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) goto err; is_prime = EC_GROUP_get_field_type(group) == NID_X9_62_prime_field; #ifdef OPENSSL_NO_EC2M if (!is_prime) { ret = TEST_skip("binary curves not supported in this build"); goto err; } #endif bsize = (EC_GROUP_get_degree(group) + 7) / 8; bsize = 1 + 2 * bsize; if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)) || !TEST_ptr(G2 = EC_POINT_new(group)) || !TEST_true(EC_POINT_dbl(group, G2, EC_GROUP_get0_generator(group), ctx)) || !TEST_int_eq(EC_POINT_point2oct(group, G2, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(buf1 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(group, G2, POINT_CONVERSION_UNCOMPRESSED, buf1, bsize, ctx), bsize) || !TEST_ptr(z = EC_GROUP_get0_order(group)) || !TEST_ptr(cof = EC_GROUP_get0_cofactor(group)) ) goto err; if (is_prime) { if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GFp(p, a, b, ctx))) goto err; } #ifndef OPENSSL_NO_EC2M else { if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GF2m(p, a, b, ctx))) goto err; } #endif EC_POINT_free(G2); if (!TEST_ptr(G2 = EC_POINT_new(altgroup)) || !TEST_true(EC_POINT_oct2point(altgroup, G2, buf1, bsize, ctx)) || !TEST_int_eq(EC_POINT_is_on_curve(altgroup, G2, ctx), 1) || !TEST_true(EC_GROUP_set_generator(altgroup, G2, z, cof)) ) goto err; if ( !TEST_ptr(Q1 = EC_POINT_new(group)) || !TEST_ptr(Q2 = EC_POINT_new(altgroup)) || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) || !TEST_true(BN_clear_bit(k, 0)) || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx)) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, buf1, bsize, ctx), bsize) || !TEST_true(BN_rshift1(k, k)) || !TEST_true(EC_POINT_mul(altgroup, Q2, k, NULL, NULL, ctx)) || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(buf2 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2, POINT_CONVERSION_UNCOMPRESSED, buf2, bsize, ctx), bsize) || !TEST_mem_eq(buf1, bsize, buf2, bsize)) goto err; if (!TEST_ptr(eckey1 = EC_KEY_new()) || !TEST_true(EC_KEY_set_group(eckey1, altgroup)) || !TEST_true(EC_KEY_generate_key(eckey1)) || !TEST_ptr(eckey2 = EC_KEY_new()) || !TEST_true(EC_KEY_set_group(eckey2, altgroup)) || !TEST_true(EC_KEY_generate_key(eckey2))) goto err; if (!TEST_ptr(priv1 = EC_KEY_get0_private_key(eckey1))) goto err; if (!TEST_true(EC_POINT_mul(group, Q1, priv1, NULL, NULL, ctx)) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(pub1 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(group, Q1, POINT_CONVERSION_UNCOMPRESSED, pub1, bsize, ctx), bsize)) goto err; if (!TEST_ptr(Q = EC_KEY_get0_public_key(eckey2)) || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), bsize) || !TEST_ptr(pub2 = OPENSSL_malloc(bsize)) || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q, POINT_CONVERSION_UNCOMPRESSED, pub2, bsize, ctx), bsize)) goto err; if (!TEST_ptr(pkey1 = EVP_PKEY_new()) || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey1, eckey1), 1)) goto err; eckey1 = NULL; if (!TEST_ptr(pkey2 = EVP_PKEY_new()) || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey2, eckey2), 1)) goto err; eckey2 = NULL; if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL)) || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1) || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1) || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &sslen), 1) || !TEST_int_gt(bsize, sslen) || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &sslen), 1)) goto err; if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new(pkey2, NULL)) || !TEST_int_eq(EVP_PKEY_derive_init(pctx2), 1) || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx2, pkey1), 1) || !TEST_int_eq(EVP_PKEY_derive(pctx2, NULL, &t), 1) || !TEST_int_gt(bsize, t) || !TEST_int_le(sslen, t) || !TEST_int_eq(EVP_PKEY_derive(pctx2, buf2, &t), 1)) goto err; if (!TEST_mem_eq(buf1, sslen, buf2, t)) goto err; if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_PKEY_PARAM_GROUP_NAME, curve_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld, OSSL_PKEY_PARAM_PUB_KEY, pub1, bsize)) || !TEST_true(OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_PRIV_KEY, priv1)) || !TEST_ptr(params1 = OSSL_PARAM_BLD_to_param(param_bld))) goto err; OSSL_PARAM_BLD_free(param_bld); if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld, OSSL_PKEY_PARAM_GROUP_NAME, curve_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld, OSSL_PKEY_PARAM_PUB_KEY, pub2, bsize)) || !TEST_ptr(params2 = OSSL_PARAM_BLD_to_param(param_bld))) goto err; EVP_PKEY_CTX_free(pctx2); if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx2), 1) || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey1, EVP_PKEY_KEYPAIR, params1), 1) || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey2, EVP_PKEY_PUBLIC_KEY, params2), 1)) goto err; EVP_PKEY_CTX_free(pctx1); if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL)) || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1) || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1) || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &t), 1) || !TEST_int_gt(bsize, t) || !TEST_int_le(sslen, t) || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &t), 1) || !TEST_mem_eq(buf1, t, buf2, sslen)) goto err; ret = 1; err: BN_CTX_end(ctx); BN_CTX_free(ctx); OSSL_PARAM_BLD_free(param_bld); OSSL_PARAM_free(params1); OSSL_PARAM_free(params2); EC_POINT_free(Q1); EC_POINT_free(Q2); EC_POINT_free(G2); EC_GROUP_free(group); EC_GROUP_free(altgroup); OPENSSL_free(buf1); OPENSSL_free(buf2); OPENSSL_free(pub1); OPENSSL_free(pub2); EC_KEY_free(eckey1); EC_KEY_free(eckey2); EVP_PKEY_free(pkey1); EVP_PKEY_free(pkey2); EVP_PKEY_CTX_free(pctx1); EVP_PKEY_CTX_free(pctx2); return ret; } static int ec_d2i_publickey_test(void) { unsigned char buf[1000]; unsigned char *pubkey_enc = buf; const unsigned char *pk_enc = pubkey_enc; EVP_PKEY *gen_key = NULL, *decoded_key = NULL; EVP_PKEY_CTX *pctx = NULL; int pklen, ret = 0; OSSL_PARAM params[2]; if (!TEST_ptr(gen_key = EVP_EC_gen("P-256"))) goto err; if (!TEST_int_gt(pklen = i2d_PublicKey(gen_key, &pubkey_enc), 0)) goto err; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0); params[1] = OSSL_PARAM_construct_end(); if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) || !TEST_true(EVP_PKEY_fromdata_init(pctx)) || !TEST_true(EVP_PKEY_fromdata(pctx, &decoded_key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, params)) || !TEST_ptr(decoded_key) || !TEST_ptr(decoded_key = d2i_PublicKey(EVP_PKEY_EC, &decoded_key, &pk_enc, pklen))) goto err; if (!TEST_true(EVP_PKEY_eq(gen_key, decoded_key))) goto err; ret = 1; err: EVP_PKEY_CTX_free(pctx); EVP_PKEY_free(gen_key); EVP_PKEY_free(decoded_key); return ret; } 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(parameter_test); ADD_TEST(ossl_parameter_test); ADD_TEST(cofactor_range_test); ADD_ALL_TESTS(cardinality_test, crv_len); ADD_TEST(prime_field_tests); #ifndef OPENSSL_NO_EC2M ADD_TEST(hybrid_point_encoding_test); ADD_TEST(char2_field_tests); ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests)); #endif ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params)); ADD_ALL_TESTS(internal_curve_test, crv_len); ADD_ALL_TESTS(internal_curve_test_method, crv_len); ADD_TEST(group_field_test); ADD_ALL_TESTS(check_named_curve_test, crv_len); ADD_ALL_TESTS(check_named_curve_lookup_test, crv_len); ADD_ALL_TESTS(check_ec_key_field_public_range_test, crv_len); ADD_ALL_TESTS(check_named_curve_from_ecparameters, crv_len); ADD_ALL_TESTS(ec_point_hex2point_test, crv_len); ADD_ALL_TESTS(custom_generator_test, crv_len); ADD_ALL_TESTS(custom_params_test, crv_len); ADD_TEST(ec_d2i_publickey_test); return 1; } void cleanup_tests(void) { OPENSSL_free(curves); }
test
openssl/test/ectest.c
openssl
#include <openssl/core.h> OSSL_provider_init_fn OSSL_provider_init; int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *oin, const OSSL_DISPATCH **out, void **provctx) { return 1; }
test
openssl/test/p_minimal.c
openssl
#include "helpers/cmp_testlib.h" #include "../crypto/crmf/crmf_local.h" 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; static X509 *endentity1 = NULL, *endentity2 = NULL, *intermediate = NULL, *root = NULL; 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; 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 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); 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 , 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) 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) { 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) { 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) { struct tm ts = { 0 }; ts.tm_year = 2018 - 1900; ts.tm_mon = 1; ts.tm_mday = 18; test_time_valid = mktime(&ts); ts.tm_year += 10; 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; 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; 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; 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 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; }
test
openssl/test/cmp_vfy_test.c
openssl
#include <limits.h> #include <stdio.h> #include <string.h> #include <crypto/asn1.h> #include <openssl/asn1.h> #include <openssl/evp.h> #include <openssl/objects.h> #include "testutil.h" #include "internal/nelem.h" struct testdata { char *data; int type; int expected_type; int check_result; time_t t; int cmp_result; int convert_result; }; struct TESTDATA_asn1_to_utc { char *input; time_t expected; }; static const struct TESTDATA_asn1_to_utc asn1_to_utc[] = { { "210328005959Z", 1616893199, }, { "210328010000Z", 1616893200, }, { "20210328015959+0100", 1616893199, }, { "20210328030000+0200", 1616893200, }, { "INVALID", -1, }, }; static struct testdata tbl_testdata_pos[] = { { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, }, { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, }, { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, }, { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "209912312359Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, { "9912312359Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, }, { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, }, { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, }, { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, }, { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, }, { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, }, { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, }; static struct testdata tbl_testdata_neg[] = { { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, }, { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, }, { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, }, }; static struct testdata tbl_testdata_pos_64bit[] = { { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, }, { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, }, { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, }, { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, }, }; static struct testdata tbl_testdata_neg_64bit[] = { { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, }, { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, }, }; static ASN1_TIME gtime = { 15, V_ASN1_GENERALIZEDTIME, (unsigned char*)"19991231000000Z", 0 }; static time_t gtime_t = 946598400; static int test_table(struct testdata *tbl, int idx) { int error = 0; ASN1_TIME atime; ASN1_TIME *ptime; struct testdata *td = &tbl[idx]; int day, sec; atime.data = (unsigned char*)td->data; atime.length = strlen((char*)atime.data); atime.type = td->type; atime.flags = 0; if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) { TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data); error = 1; } if (td->check_result == 0) return 1; if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) { TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t); error = 1; } if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) { TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data); error = 1; } if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) { TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data); error = 1; } if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) { TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data); error = 1; } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) || (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) || (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) { TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data); error = 1; } if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) { TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data); error = 1; } ptime = ASN1_TIME_set(NULL, td->t); if (!TEST_ptr(ptime)) { TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t); error = 1; } else { int local_error = 0; if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) { TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)", (long)td->t, td->data, ptime->data); local_error = error = 1; } if (!TEST_int_eq(ptime->type, td->expected_type)) { TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t); local_error = error = 1; } if (local_error) TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data); ASN1_TIME_free(ptime); } ptime = ASN1_TIME_new(); if (!TEST_ptr(ptime)) { TEST_info("ASN1_TIME_new() failed"); error = 1; } else { int local_error = 0; if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) { TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data); local_error = error = 1; } if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) { TEST_info("ASN1_TIME_normalize(%s) failed", td->data); local_error = error = 1; } if (!TEST_int_eq(ptime->type, td->expected_type)) { TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data); local_error = error = 1; } day = sec = 0; if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) { TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data); local_error = error = 1; } if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) { TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data); local_error = error = 1; } if (local_error) TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data); ASN1_TIME_free(ptime); } ptime = ASN1_TIME_new(); if (!TEST_ptr(ptime)) { TEST_info("ASN1_TIME_new() failed"); error = 1; } else { int local_error = 0; if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) { TEST_info("ASN1_TIME_set_string(%s) failed", td->data); local_error = error = 1; } day = sec = 0; if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) { TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data); local_error = error = 1; } if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) { TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data); local_error = error = 1; } if (local_error) TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data); ASN1_TIME_free(ptime); } if (td->type == V_ASN1_UTCTIME) { ptime = ASN1_TIME_to_generalizedtime(&atime, NULL); if (td->convert_result == 1 && !TEST_ptr(ptime)) { TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data); error = 1; } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) { TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data); error = 1; } if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) { TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data); error = 1; } ASN1_TIME_free(ptime); } if (error) TEST_error("atime=%s", atime.data); return !error; } static int test_table_pos(int idx) { return test_table(tbl_testdata_pos, idx); } static int test_table_neg(int idx) { return test_table(tbl_testdata_neg, idx); } static int test_table_pos_64bit(int idx) { return test_table(tbl_testdata_pos_64bit, idx); } static int test_table_neg_64bit(int idx) { return test_table(tbl_testdata_neg_64bit, idx); } struct compare_testdata { ASN1_TIME t1; ASN1_TIME t2; int result; }; static unsigned char TODAY_GEN_STR[] = "20170825000000Z"; static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z"; static unsigned char TODAY_UTC_STR[] = "170825000000Z"; static unsigned char TOMORROW_UTC_STR[] = "170826000000Z"; #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 } #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 } #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 } #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 } static struct compare_testdata tbl_compare_testdata[] = { { TODAY_GEN, TODAY_GEN, 0 }, { TODAY_GEN, TODAY_UTC, 0 }, { TODAY_GEN, TOMORROW_GEN, -1 }, { TODAY_GEN, TOMORROW_UTC, -1 }, { TODAY_UTC, TODAY_GEN, 0 }, { TODAY_UTC, TODAY_UTC, 0 }, { TODAY_UTC, TOMORROW_GEN, -1 }, { TODAY_UTC, TOMORROW_UTC, -1 }, { TOMORROW_GEN, TODAY_GEN, 1 }, { TOMORROW_GEN, TODAY_UTC, 1 }, { TOMORROW_GEN, TOMORROW_GEN, 0 }, { TOMORROW_GEN, TOMORROW_UTC, 0 }, { TOMORROW_UTC, TODAY_GEN, 1 }, { TOMORROW_UTC, TODAY_UTC, 1 }, { TOMORROW_UTC, TOMORROW_GEN, 0 }, { TOMORROW_UTC, TOMORROW_UTC, 0 } }; static int test_table_compare(int idx) { struct compare_testdata *td = &tbl_compare_testdata[idx]; return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result); } static int test_time_dup(void) { int ret = 0; ASN1_TIME *asn1_time = NULL; ASN1_TIME *asn1_time_dup = NULL; ASN1_TIME *asn1_gentime = NULL; asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0); if (asn1_time == NULL) { TEST_info("Internal error."); goto err; } asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL); if (asn1_gentime == NULL) { TEST_info("Internal error."); goto err; } asn1_time_dup = ASN1_TIME_dup(asn1_time); if (!TEST_ptr_ne(asn1_time_dup, NULL)) { TEST_info("ASN1_TIME_dup() failed."); goto err; } if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) { TEST_info("ASN1_TIME_dup() duplicated non-identical value."); goto err; } ASN1_STRING_free(asn1_time_dup); asn1_time_dup = ASN1_UTCTIME_dup(asn1_time); if (!TEST_ptr_ne(asn1_time_dup, NULL)) { TEST_info("ASN1_UTCTIME_dup() failed."); goto err; } if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) { TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value."); goto err; } ASN1_STRING_free(asn1_time_dup); asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime); if (!TEST_ptr_ne(asn1_time_dup, NULL)) { TEST_info("ASN1_GENERALIZEDTIME_dup() failed."); goto err; } if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) { TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value."); goto err; } ret = 1; err: ASN1_STRING_free(asn1_time); ASN1_STRING_free(asn1_gentime); ASN1_STRING_free(asn1_time_dup); return ret; } static int convert_asn1_to_time_t(int idx) { time_t testdateutc; testdateutc = ossl_asn1_string_to_time_t(asn1_to_utc[idx].input); if (!TEST_time_t_eq(testdateutc, asn1_to_utc[idx].expected)) { TEST_info("ossl_asn1_string_to_time_t (%s) failed: expected %lli, got %lli\n", asn1_to_utc[idx].input, (long long int)asn1_to_utc[idx].expected, (long long int)testdateutc); return 0; } return 1; } static int convert_tm_to_asn1_time(void) { #if ((ULONG_MAX >> 31) >> 31) >= 1 time_t t; ASN1_TIME *at; if (sizeof(time_t) * CHAR_BIT >= 64) { t = 67768011791126057ULL; at = ASN1_TIME_set(NULL, t); ASN1_STRING_free(at); } #endif return 1; } int setup_tests(void) { time_t t = -1; struct tm *ptm = localtime(&t); ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos)); if (!(t > 0) && ptm != NULL) { TEST_info("Adding negative-sign time_t tests"); ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg)); } if (sizeof(time_t) > sizeof(uint32_t)) { TEST_info("Adding 64-bit time_t tests"); ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit)); #ifndef __hpux if (!(t > 0) && ptm != NULL) { TEST_info("Adding negative-sign 64-bit time_t tests"); ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit)); } #endif } ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata)); ADD_TEST(test_time_dup); ADD_ALL_TESTS(convert_asn1_to_time_t, OSSL_NELEM(asn1_to_utc)); ADD_TEST(convert_tm_to_asn1_time); return 1; }
test
openssl/test/asn1_time_test.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include <openssl/core_names.h> #include <openssl/evp.h> #include <openssl/ec.h> #include <openssl/dh.h> #include <openssl/dsa.h> #include <openssl/rsa.h> #include <openssl/param_build.h> #include <openssl/provider.h> #include <openssl/self_test.h> #include "testutil.h" #include "testutil/output.h" #include "acvp_test.inc" #include "internal/nelem.h" typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; typedef struct st_args { int enable; int called; } SELF_TEST_ARGS; static OSSL_PROVIDER *prov_null = NULL; static OSSL_LIB_CTX *libctx = NULL; static SELF_TEST_ARGS self_test_args = { 0 }; static OSSL_CALLBACK self_test_events; 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 pkey_get_bn_bytes(EVP_PKEY *pkey, const char *name, unsigned char **out, size_t *out_len) { unsigned char *buf = NULL; BIGNUM *bn = NULL; int sz; if (!EVP_PKEY_get_bn_param(pkey, name, &bn)) goto err; sz = BN_num_bytes(bn); buf = OPENSSL_zalloc(sz); if (buf == NULL) goto err; if (BN_bn2binpad(bn, buf, sz) <= 0) goto err; *out_len = sz; *out = buf; BN_free(bn); return 1; err: OPENSSL_free(buf); BN_free(bn); return 0; } static int sig_gen(EVP_PKEY *pkey, OSSL_PARAM *params, const char *digest_name, const unsigned char *msg, size_t msg_len, unsigned char **sig_out, size_t *sig_out_len) { int ret = 0; EVP_MD_CTX *md_ctx = NULL; unsigned char *sig = NULL; size_t sig_len; size_t sz = EVP_PKEY_get_size(pkey); sig_len = sz; if (!TEST_ptr(sig = OPENSSL_malloc(sz)) || !TEST_ptr(md_ctx = EVP_MD_CTX_new()) || !TEST_int_eq(EVP_DigestSignInit_ex(md_ctx, NULL, digest_name, libctx, NULL, pkey, NULL), 1) || !TEST_int_gt(EVP_DigestSign(md_ctx, sig, &sig_len, msg, msg_len), 0)) goto err; *sig_out = sig; *sig_out_len = sig_len; sig = NULL; ret = 1; err: OPENSSL_free(sig); EVP_MD_CTX_free(md_ctx); return ret; } #ifndef OPENSSL_NO_EC static int ecdsa_keygen_test(int id) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *priv = NULL; unsigned char *pubx = NULL, *puby = NULL; size_t priv_len = 0, pubx_len = 0, puby_len = 0; const struct ecdsa_keygen_st *tst = &ecdsa_keygen_data[id]; self_test_args.called = 0; self_test_args.enable = 1; if (!TEST_ptr(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "EC", tst->curve_name)) || !TEST_int_ge(self_test_args.called, 3) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv, &priv_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &pubx, &pubx_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &puby, &puby_len))) goto err; test_output_memory("qy", puby, puby_len); test_output_memory("qx", pubx, pubx_len); test_output_memory("d", priv, priv_len); ret = 1; err: self_test_args.enable = 0; self_test_args.called = 0; OPENSSL_clear_free(priv, priv_len); OPENSSL_free(pubx); OPENSSL_free(puby); EVP_PKEY_free(pkey); return ret; } static int ecdsa_create_pkey(EVP_PKEY **pkey, const char *curve_name, const unsigned char *pub, size_t pub_len, int expected) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || (curve_name != NULL && !TEST_true(OSSL_PARAM_BLD_push_utf8_string( bld, OSSL_PKEY_PARAM_GROUP_NAME, curve_name, 0) > 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, pub, pub_len) > 0) || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_PUBLIC_KEY, params), expected)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_CTX_free(ctx); return ret; } static int ecdsa_pub_verify_test(int id) { const struct ecdsa_pub_verify_st *tst = &ecdsa_pv_data[id]; int ret = 0; EVP_PKEY_CTX *key_ctx = NULL; EVP_PKEY *pkey = NULL; if (!TEST_true(ecdsa_create_pkey(&pkey, tst->curve_name, tst->pub, tst->pub_len, tst->pass))) goto err; if (tst->pass) { if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "")) || !TEST_int_eq(EVP_PKEY_public_check(key_ctx), tst->pass)) goto err; } ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(key_ctx); return ret; } static int get_ecdsa_sig_rs_bytes(const unsigned char *sig, size_t sig_len, unsigned char **r, unsigned char **s, size_t *rlen, size_t *slen) { int ret = 0; unsigned char *rbuf = NULL, *sbuf = NULL; size_t r1_len, s1_len; const BIGNUM *r1, *s1; ECDSA_SIG *sign = d2i_ECDSA_SIG(NULL, &sig, sig_len); if (sign == NULL) return 0; r1 = ECDSA_SIG_get0_r(sign); s1 = ECDSA_SIG_get0_s(sign); if (r1 == NULL || s1 == NULL) goto err; r1_len = BN_num_bytes(r1); s1_len = BN_num_bytes(s1); rbuf = OPENSSL_zalloc(r1_len); sbuf = OPENSSL_zalloc(s1_len); if (rbuf == NULL || sbuf == NULL) goto err; if (BN_bn2binpad(r1, rbuf, r1_len) <= 0) goto err; if (BN_bn2binpad(s1, sbuf, s1_len) <= 0) goto err; *r = rbuf; *s = sbuf; *rlen = r1_len; *slen = s1_len; ret = 1; err: if (ret == 0) { OPENSSL_free(rbuf); OPENSSL_free(sbuf); } ECDSA_SIG_free(sign); return ret; } static int ecdsa_siggen_test(int id) { int ret = 0; EVP_PKEY *pkey = NULL; size_t sig_len = 0, rlen = 0, slen = 0; unsigned char *sig = NULL; unsigned char *r = NULL, *s = NULL; const struct ecdsa_siggen_st *tst = &ecdsa_siggen_data[id]; if (!TEST_ptr(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "EC", tst->curve_name))) goto err; if (!TEST_true(sig_gen(pkey, NULL, tst->digest_alg, tst->msg, tst->msg_len, &sig, &sig_len)) || !TEST_true(get_ecdsa_sig_rs_bytes(sig, sig_len, &r, &s, &rlen, &slen))) goto err; test_output_memory("r", r, rlen); test_output_memory("s", s, slen); ret = 1; err: OPENSSL_free(r); OPENSSL_free(s); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } static int ecdsa_sigver_test(int id) { int ret = 0; EVP_MD_CTX *md_ctx = NULL; EVP_PKEY *pkey = NULL; ECDSA_SIG *sign = NULL; size_t sig_len; unsigned char *sig = NULL; BIGNUM *rbn = NULL, *sbn = NULL; const struct ecdsa_sigver_st *tst = &ecdsa_sigver_data[id]; if (!TEST_true(ecdsa_create_pkey(&pkey, tst->curve_name, tst->pub, tst->pub_len, 1))) goto err; if (!TEST_ptr(sign = ECDSA_SIG_new()) || !TEST_ptr(rbn = BN_bin2bn(tst->r, tst->r_len, NULL)) || !TEST_ptr(sbn = BN_bin2bn(tst->s, tst->s_len, NULL)) || !TEST_true(ECDSA_SIG_set0(sign, rbn, sbn))) goto err; rbn = sbn = NULL; ret = TEST_int_gt((sig_len = i2d_ECDSA_SIG(sign, &sig)), 0) && TEST_ptr(md_ctx = EVP_MD_CTX_new()) && TEST_true(EVP_DigestVerifyInit_ex(md_ctx, NULL, tst->digest_alg, libctx, NULL, pkey, NULL) && TEST_int_eq(EVP_DigestVerify(md_ctx, sig, sig_len, tst->msg, tst->msg_len), tst->pass)); err: BN_free(rbn); BN_free(sbn); OPENSSL_free(sig); ECDSA_SIG_free(sign); EVP_PKEY_free(pkey); EVP_MD_CTX_free(md_ctx); return ret; } #endif #ifndef OPENSSL_NO_DSA static int pkey_get_octet_bytes(EVP_PKEY *pkey, const char *name, unsigned char **out, size_t *out_len) { size_t len = 0; unsigned char *buf = NULL; if (!EVP_PKEY_get_octet_string_param(pkey, name, NULL, 0, &len)) goto err; buf = OPENSSL_zalloc(len); if (buf == NULL) goto err; if (!EVP_PKEY_get_octet_string_param(pkey, name, buf, len, out_len)) goto err; *out = buf; return 1; err: OPENSSL_free(buf); return 0; } static EVP_PKEY *dsa_paramgen(int L, int N) { EVP_PKEY_CTX *paramgen_ctx = NULL; EVP_PKEY *param_key = NULL; if (!TEST_ptr(paramgen_ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(paramgen_ctx), 0) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(paramgen_ctx, L)) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_q_bits(paramgen_ctx, N)) || !TEST_true(EVP_PKEY_paramgen(paramgen_ctx, &param_key))) return NULL; EVP_PKEY_CTX_free(paramgen_ctx); return param_key; } static EVP_PKEY *dsa_keygen(int L, int N) { EVP_PKEY *param_key = NULL, *key = NULL; EVP_PKEY_CTX *keygen_ctx = NULL; if (!TEST_ptr(param_key = dsa_paramgen(L, N)) || !TEST_ptr(keygen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(keygen_ctx), 0) || !TEST_int_gt(EVP_PKEY_keygen(keygen_ctx, &key), 0)) goto err; err: EVP_PKEY_free(param_key); EVP_PKEY_CTX_free(keygen_ctx); return key; } static int dsa_keygen_test(int id) { int ret = 0, i; EVP_PKEY_CTX *paramgen_ctx = NULL, *keygen_ctx = NULL; EVP_PKEY *param_key = NULL, *key = NULL; unsigned char *priv = NULL, *pub = NULL; size_t priv_len = 0, pub_len = 0; const struct dsa_paramgen_st *tst = &dsa_keygen_data[id]; if (!TEST_ptr(param_key = dsa_paramgen(tst->L, tst->N)) || !TEST_ptr(keygen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(keygen_ctx), 0)) goto err; for (i = 0; i < 2; ++i) { if (!TEST_int_gt(EVP_PKEY_keygen(keygen_ctx, &key), 0) || !TEST_true(pkey_get_bn_bytes(key, OSSL_PKEY_PARAM_PRIV_KEY, &priv, &priv_len)) || !TEST_true(pkey_get_bn_bytes(key, OSSL_PKEY_PARAM_PUB_KEY, &pub, &pub_len))) goto err; test_output_memory("y", pub, pub_len); test_output_memory("x", priv, priv_len); EVP_PKEY_free(key); OPENSSL_clear_free(priv, priv_len); OPENSSL_free(pub); key = NULL; pub = priv = NULL; } ret = 1; err: OPENSSL_clear_free(priv, priv_len); OPENSSL_free(pub); EVP_PKEY_free(param_key); EVP_PKEY_free(key); EVP_PKEY_CTX_free(keygen_ctx); EVP_PKEY_CTX_free(paramgen_ctx); return ret; } static int dsa_paramgen_test(int id) { int ret = 0, counter = 0; EVP_PKEY_CTX *paramgen_ctx = NULL; EVP_PKEY *param_key = NULL; unsigned char *p = NULL, *q = NULL, *seed = NULL; size_t plen = 0, qlen = 0, seedlen = 0; const struct dsa_paramgen_st *tst = &dsa_paramgen_data[id]; if (!TEST_ptr(paramgen_ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(paramgen_ctx), 0) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(paramgen_ctx, tst->L)) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_q_bits(paramgen_ctx, tst->N)) || !TEST_true(EVP_PKEY_paramgen(paramgen_ctx, &param_key)) || !TEST_true(pkey_get_bn_bytes(param_key, OSSL_PKEY_PARAM_FFC_P, &p, &plen)) || !TEST_true(pkey_get_bn_bytes(param_key, OSSL_PKEY_PARAM_FFC_Q, &q, &qlen)) || !TEST_true(pkey_get_octet_bytes(param_key, OSSL_PKEY_PARAM_FFC_SEED, &seed, &seedlen)) || !TEST_true(EVP_PKEY_get_int_param(param_key, OSSL_PKEY_PARAM_FFC_PCOUNTER, &counter))) goto err; test_output_memory("p", p, plen); test_output_memory("q", q, qlen); test_output_memory("domainSeed", seed, seedlen); test_printf_stderr("%s: %d\n", "counter", counter); ret = 1; err: OPENSSL_free(p); OPENSSL_free(q); OPENSSL_free(seed); EVP_PKEY_free(param_key); EVP_PKEY_CTX_free(paramgen_ctx); return ret; } static int dsa_create_pkey(EVP_PKEY **pkey, const unsigned char *p, size_t p_len, const unsigned char *q, size_t q_len, const unsigned char *g, size_t g_len, const unsigned char *seed, size_t seed_len, int counter, int validate_pq, int validate_g, const unsigned char *pub, size_t pub_len, BN_CTX *bn_ctx) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; BIGNUM *p_bn = NULL, *q_bn = NULL, *g_bn = NULL, *pub_bn = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(p_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(p, p_len, p_bn)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, validate_pq)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_VALIDATE_G, validate_g)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p_bn)) || !TEST_ptr(q_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(q, q_len, q_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q_bn))) goto err; if (g != NULL) { if (!TEST_ptr(g_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(g, g_len, g_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g_bn))) goto err; } if (seed != NULL) { if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_FFC_SEED, seed, seed_len))) goto err; } if (counter != -1) { if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_PCOUNTER, counter))) goto err; } if (pub != NULL) { if (!TEST_ptr(pub_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(pub, pub_len, pub_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub_bn))) goto err; } if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_PUBLIC_KEY, params), 1)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_CTX_free(ctx); return ret; } static int dsa_pqver_test(int id) { int ret = 0; BN_CTX *bn_ctx = NULL; EVP_PKEY_CTX *key_ctx = NULL; EVP_PKEY *param_key = NULL; const struct dsa_pqver_st *tst = &dsa_pqver_data[id]; if (!TEST_ptr(bn_ctx = BN_CTX_new_ex(libctx)) || !TEST_true(dsa_create_pkey(&param_key, tst->p, tst->p_len, tst->q, tst->q_len, NULL, 0, tst->seed, tst->seed_len, tst->counter, 1, 0, NULL, 0, bn_ctx)) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key, NULL)) || !TEST_int_eq(EVP_PKEY_param_check(key_ctx), tst->pass)) goto err; ret = 1; err: BN_CTX_free(bn_ctx); EVP_PKEY_free(param_key); EVP_PKEY_CTX_free(key_ctx); return ret; } static int get_dsa_sig_rs_bytes(const unsigned char *sig, size_t sig_len, unsigned char **r, unsigned char **s, size_t *r_len, size_t *s_len) { int ret = 0; unsigned char *rbuf = NULL, *sbuf = NULL; size_t r1_len, s1_len; const BIGNUM *r1, *s1; DSA_SIG *sign = d2i_DSA_SIG(NULL, &sig, sig_len); if (sign == NULL) return 0; DSA_SIG_get0(sign, &r1, &s1); if (r1 == NULL || s1 == NULL) goto err; r1_len = BN_num_bytes(r1); s1_len = BN_num_bytes(s1); rbuf = OPENSSL_zalloc(r1_len); sbuf = OPENSSL_zalloc(s1_len); if (rbuf == NULL || sbuf == NULL) goto err; if (BN_bn2binpad(r1, rbuf, r1_len) <= 0) goto err; if (BN_bn2binpad(s1, sbuf, s1_len) <= 0) goto err; *r = rbuf; *s = sbuf; *r_len = r1_len; *s_len = s1_len; ret = 1; err: if (ret == 0) { OPENSSL_free(rbuf); OPENSSL_free(sbuf); } DSA_SIG_free(sign); return ret; } static int dsa_siggen_test(int id) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *sig = NULL, *r = NULL, *s = NULL; size_t sig_len = 0, rlen = 0, slen = 0; const struct dsa_siggen_st *tst = &dsa_siggen_data[id]; if (!TEST_ptr(pkey = dsa_keygen(tst->L, tst->N))) goto err; if (!TEST_true(sig_gen(pkey, NULL, tst->digest_alg, tst->msg, tst->msg_len, &sig, &sig_len)) || !TEST_true(get_dsa_sig_rs_bytes(sig, sig_len, &r, &s, &rlen, &slen))) goto err; test_output_memory("r", r, rlen); test_output_memory("s", s, slen); ret = 1; err: OPENSSL_free(r); OPENSSL_free(s); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } static int dsa_sigver_test(int id) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; DSA_SIG *sign = NULL; size_t sig_len; unsigned char *sig = NULL; BIGNUM *rbn = NULL, *sbn = NULL; EVP_MD *md = NULL; unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int digest_len; BN_CTX *bn_ctx = NULL; const struct dsa_sigver_st *tst = &dsa_sigver_data[id]; if (!TEST_ptr(bn_ctx = BN_CTX_new()) || !TEST_true(dsa_create_pkey(&pkey, tst->p, tst->p_len, tst->q, tst->q_len, tst->g, tst->g_len, NULL, 0, 0, 0, 0, tst->pub, tst->pub_len, bn_ctx))) goto err; if (!TEST_ptr(sign = DSA_SIG_new()) || !TEST_ptr(rbn = BN_bin2bn(tst->r, tst->r_len, NULL)) || !TEST_ptr(sbn = BN_bin2bn(tst->s, tst->s_len, NULL)) || !TEST_true(DSA_SIG_set0(sign, rbn, sbn))) goto err; rbn = sbn = NULL; if (!TEST_ptr(md = EVP_MD_fetch(libctx, tst->digest_alg, "")) || !TEST_true(EVP_Digest(tst->msg, tst->msg_len, digest, &digest_len, md, NULL))) goto err; if (!TEST_int_gt((sig_len = i2d_DSA_SIG(sign, &sig)), 0) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "")) || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0) || !TEST_int_eq(EVP_PKEY_verify(ctx, sig, sig_len, digest, digest_len), tst->pass)) goto err; ret = 1; err: EVP_PKEY_CTX_free(ctx); OPENSSL_free(sig); EVP_MD_free(md); DSA_SIG_free(sign); EVP_PKEY_free(pkey); BN_free(rbn); BN_free(sbn); BN_CTX_free(bn_ctx); return ret; } #endif static int cipher_enc(const char *alg, const unsigned char *pt, size_t pt_len, const unsigned char *key, size_t key_len, const unsigned char *iv, size_t iv_len, const unsigned char *ct, size_t ct_len, int enc) { int ret = 0, out_len = 0, len = 0; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *cipher = NULL; unsigned char out[256] = { 0 }; TEST_note("%s : %s", alg, enc ? "encrypt" : "decrypt"); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, "")) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc)) || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0)) || !TEST_true(EVP_CipherUpdate(ctx, out, &len, pt, pt_len)) || !TEST_true(EVP_CipherFinal_ex(ctx, out + len, &out_len))) goto err; out_len += len; if (!TEST_mem_eq(out, out_len, ct, ct_len)) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int cipher_enc_dec_test(int id) { const struct cipher_st *tst = &cipher_enc_data[id]; const int enc = 1; return TEST_true(cipher_enc(tst->alg, tst->pt, tst->pt_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->ct, tst->ct_len, enc)) && TEST_true(cipher_enc(tst->alg, tst->ct, tst->ct_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->pt, tst->pt_len, !enc)); } static int aes_ccm_enc_dec(const char *alg, const unsigned char *pt, size_t pt_len, const unsigned char *key, size_t key_len, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *ct, size_t ct_len, const unsigned char *tag, size_t tag_len, int enc, int pass) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int out_len, len; unsigned char out[1024]; TEST_note("%s : %s : expected to %s", alg, enc ? "encrypt" : "decrypt", pass ? "pass" : "fail"); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, "")) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) || !TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL), 0) || !TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, enc ? NULL : (void *)tag), 0) || !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0)) || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, NULL, pt_len)) || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, aad, aad_len)) || !TEST_int_eq(EVP_CipherUpdate(ctx, out, &len, pt, pt_len), pass)) goto err; if (!pass) { ret = 1; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, out + len, &out_len))) goto err; if (enc) { out_len += len; if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_len, out + out_len), 0) || !TEST_mem_eq(out, out_len, ct, ct_len) || !TEST_mem_eq(out + out_len, tag_len, tag, tag_len)) goto err; } else { if (!TEST_mem_eq(out, out_len + len, ct, ct_len)) goto err; } ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int aes_ccm_enc_dec_test(int id) { const struct cipher_ccm_st *tst = &aes_ccm_enc_data[id]; const size_t tag_len = tst->ct_len - tst->pt_len; const size_t ct_len = tst->ct_len - tag_len; const unsigned char *tag = tst->ct + ct_len; const int enc = 1; const int pass = 1; if (ct_len < 1) return 0; return aes_ccm_enc_dec(tst->alg, tst->pt, tst->pt_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->ct, ct_len, tag, tag_len, enc, pass) && aes_ccm_enc_dec(tst->alg, tst->ct, ct_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->pt, tst->pt_len, tag, tag_len, !enc, pass) && aes_ccm_enc_dec(tst->alg, tst->ct, ct_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->pt, tst->pt_len, tag - 1, tag_len, !enc, !pass); } static int aes_gcm_enc_dec(const char *alg, const unsigned char *pt, size_t pt_len, const unsigned char *key, size_t key_len, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *ct, size_t ct_len, const unsigned char *tag, size_t tag_len, int enc, int pass) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int out_len, len; unsigned char out[1024]; TEST_note("%s : %s : expected to %s", alg, enc ? "encrypt" : "decrypt", pass ? "pass" : "fail"); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, "")) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) || !TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL), 0)) goto err; if (!enc) { if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, (void *)tag), 0)) goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0)) || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, aad, aad_len)) || !TEST_true(EVP_CipherUpdate(ctx, out, &len, pt, pt_len))) goto err; if (!TEST_int_eq(EVP_CipherFinal_ex(ctx, out + len, &out_len), pass)) goto err; if (!pass) { ret = 1; goto err; } out_len += len; if (enc) { if (!TEST_mem_eq(out, out_len, ct, ct_len) || !TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_len, out + out_len), 0) || !TEST_mem_eq(out + out_len, tag_len, tag, tag_len)) goto err; } else { if (!TEST_mem_eq(out, out_len, ct, ct_len)) goto err; } ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int aes_gcm_enc_dec_test(int id) { const struct cipher_gcm_st *tst = &aes_gcm_enc_data[id]; int enc = 1; int pass = 1; return aes_gcm_enc_dec(tst->alg, tst->pt, tst->pt_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->ct, tst->ct_len, tst->tag, tst->tag_len, enc, pass) && aes_gcm_enc_dec(tst->alg, tst->ct, tst->ct_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->pt, tst->pt_len, tst->tag, tst->tag_len, !enc, pass) && aes_gcm_enc_dec(tst->alg, tst->ct, tst->ct_len, tst->key, tst->key_len, tst->iv, tst->iv_len, tst->aad, tst->aad_len, tst->pt, tst->pt_len, tst->aad, tst->tag_len, !enc, !pass); } #ifndef OPENSSL_NO_DH static int dh_create_pkey(EVP_PKEY **pkey, const char *group_name, const unsigned char *pub, size_t pub_len, const unsigned char *priv, size_t priv_len, BN_CTX *bn_ctx, int pass) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; BIGNUM *pub_bn = NULL, *priv_bn = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || (group_name != NULL && !TEST_int_gt(OSSL_PARAM_BLD_push_utf8_string( bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0), 0))) goto err; if (pub != NULL) { if (!TEST_ptr(pub_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(pub, pub_len, pub_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub_bn))) goto err; } if (priv != NULL) { if (!TEST_ptr(priv_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(priv, priv_len, priv_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv_bn))) goto err; } if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_KEYPAIR, params), pass)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_CTX_free(ctx); return ret; } static int dh_safe_prime_keygen_test(int id) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; unsigned char *priv = NULL; unsigned char *pub = NULL; size_t priv_len = 0, pub_len = 0; OSSL_PARAM params[2]; const struct dh_safe_prime_keygen_st *tst = &dh_safe_prime_keygen_data[id]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, (char *)tst->group_name, 0); params[1] = OSSL_PARAM_construct_end(); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_true(EVP_PKEY_CTX_set_params(ctx, params)) || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv, &priv_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PUB_KEY, &pub, &pub_len))) goto err; test_output_memory("x", priv, priv_len); test_output_memory("y", pub, pub_len); ret = 1; err: OPENSSL_clear_free(priv, priv_len); OPENSSL_free(pub); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); return ret; } static int dh_safe_prime_keyver_test(int id) { int ret = 0; BN_CTX *bn_ctx = NULL; EVP_PKEY_CTX *key_ctx = NULL; EVP_PKEY *pkey = NULL; const struct dh_safe_prime_keyver_st *tst = &dh_safe_prime_keyver_data[id]; if (!TEST_ptr(bn_ctx = BN_CTX_new_ex(libctx)) || !TEST_true(dh_create_pkey(&pkey, tst->group_name, tst->pub, tst->pub_len, tst->priv, tst->priv_len, bn_ctx, 1)) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "")) || !TEST_int_eq(EVP_PKEY_check(key_ctx), tst->pass)) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(key_ctx); BN_CTX_free(bn_ctx); return ret; } #endif static int rsa_create_pkey(EVP_PKEY **pkey, const unsigned char *n, size_t n_len, const unsigned char *e, size_t e_len, const unsigned char *d, size_t d_len, BN_CTX *bn_ctx) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; BIGNUM *e_bn = NULL, *d_bn = NULL, *n_bn = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(n, n_len, n_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n_bn))) goto err; if (e != NULL) { if (!TEST_ptr(e_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(e, e_len, e_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e_bn))) goto err; } if (d != NULL) { if (!TEST_ptr(d_bn = BN_CTX_get(bn_ctx)) || !TEST_ptr(BN_bin2bn(d, d_len, d_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d_bn))) goto err; } if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_KEYPAIR, params), 1)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_CTX_free(ctx); return ret; } static int rsa_keygen_test(int id) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; BIGNUM *e_bn = NULL; BIGNUM *xp1_bn = NULL, *xp2_bn = NULL, *xp_bn = NULL; BIGNUM *xq1_bn = NULL, *xq2_bn = NULL, *xq_bn = NULL; unsigned char *n = NULL, *d = NULL; unsigned char *p = NULL, *p1 = NULL, *p2 = NULL; unsigned char *q = NULL, *q1 = NULL, *q2 = NULL; size_t n_len = 0, d_len = 0; size_t p_len = 0, p1_len = 0, p2_len = 0; size_t q_len = 0, q1_len = 0, q2_len = 0; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; const struct rsa_keygen_st *tst = &rsa_keygen_data[id]; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(xp1_bn = BN_bin2bn(tst->xp1, tst->xp1_len, NULL)) || !TEST_ptr(xp2_bn = BN_bin2bn(tst->xp2, tst->xp2_len, NULL)) || !TEST_ptr(xp_bn = BN_bin2bn(tst->xp, tst->xp_len, NULL)) || !TEST_ptr(xq1_bn = BN_bin2bn(tst->xq1, tst->xq1_len, NULL)) || !TEST_ptr(xq2_bn = BN_bin2bn(tst->xq2, tst->xq2_len, NULL)) || !TEST_ptr(xq_bn = BN_bin2bn(tst->xq, tst->xq_len, NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP1, xp1_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP2, xp2_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP, xp_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ1, xq1_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ2, xq2_bn)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ, xq_bn)) || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)) || !TEST_ptr(e_bn = BN_bin2bn(tst->e, tst->e_len, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_params(ctx, params), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, tst->mod), 0) || !TEST_int_gt(EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, e_bn), 0) || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_P1, &p1, &p1_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_P2, &p2, &p2_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_Q1, &q1, &q1_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_Q2, &q2, &q2_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, &p, &p_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, &q, &q_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N, &n, &n_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_D, &d, &d_len))) goto err; if (!TEST_mem_eq(tst->p1, tst->p1_len, p1, p1_len) || !TEST_mem_eq(tst->p2, tst->p2_len, p2, p2_len) || !TEST_mem_eq(tst->p, tst->p_len, p, p_len) || !TEST_mem_eq(tst->q1, tst->q1_len, q1, q1_len) || !TEST_mem_eq(tst->q2, tst->q2_len, q2, q2_len) || !TEST_mem_eq(tst->q, tst->q_len, q, q_len) || !TEST_mem_eq(tst->n, tst->n_len, n, n_len) || !TEST_mem_eq(tst->d, tst->d_len, d, d_len)) goto err; test_output_memory("p1", p1, p1_len); test_output_memory("p2", p2, p2_len); test_output_memory("p", p, p_len); test_output_memory("q1", q1, q1_len); test_output_memory("q2", q2, q2_len); test_output_memory("q", q, q_len); test_output_memory("n", n, n_len); test_output_memory("d", d, d_len); ret = 1; err: BN_free(xp1_bn); BN_free(xp2_bn); BN_free(xp_bn); BN_free(xq1_bn); BN_free(xq2_bn); BN_free(xq_bn); BN_free(e_bn); OPENSSL_free(p1); OPENSSL_free(p2); OPENSSL_free(q1); OPENSSL_free(q2); OPENSSL_free(p); OPENSSL_free(q); OPENSSL_free(n); OPENSSL_free(d); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); return ret; } static int rsa_siggen_test(int id) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *sig = NULL, *n = NULL, *e = NULL; size_t sig_len = 0, n_len = 0, e_len = 0; OSSL_PARAM params[4], *p; const struct rsa_siggen_st *tst = &rsa_siggen_data[id]; int salt_len = tst->pss_salt_len; TEST_note("RSA %s signature generation", tst->sig_pad_mode); p = params; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, (char *)tst->sig_pad_mode, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (char *)tst->digest_alg, 0); if (salt_len >= 0) *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &salt_len); *p++ = OSSL_PARAM_construct_end(); if (!TEST_ptr(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", tst->mod)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N, &n, &n_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_E, &e, &e_len)) || !TEST_true(sig_gen(pkey, params, tst->digest_alg, tst->msg, tst->msg_len, &sig, &sig_len))) goto err; test_output_memory("n", n, n_len); test_output_memory("e", e, e_len); test_output_memory("sig", sig, sig_len); ret = 1; err: OPENSSL_free(n); OPENSSL_free(e); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } static int rsa_sigver_test(int id) { int ret = 0; EVP_PKEY_CTX *pkey_ctx = NULL; EVP_PKEY *pkey = NULL; EVP_MD_CTX *md_ctx = NULL; BN_CTX *bn_ctx = NULL; OSSL_PARAM params[4], *p; const struct rsa_sigver_st *tst = &rsa_sigver_data[id]; int salt_len = tst->pss_salt_len; TEST_note("RSA %s Signature Verify : expected to %s ", tst->sig_pad_mode, tst->pass == PASS ? "pass" : "fail"); p = params; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, (char *)tst->sig_pad_mode, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (char *)tst->digest_alg, 0); if (salt_len >= 0) *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &salt_len); *p++ = OSSL_PARAM_construct_end(); if (!TEST_ptr(bn_ctx = BN_CTX_new()) || !TEST_true(rsa_create_pkey(&pkey, tst->n, tst->n_len, tst->e, tst->e_len, NULL, 0, bn_ctx)) || !TEST_ptr(md_ctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestVerifyInit_ex(md_ctx, &pkey_ctx, tst->digest_alg, libctx, NULL, pkey, NULL)) || !TEST_true(EVP_PKEY_CTX_set_params(pkey_ctx, params)) || !TEST_int_eq(EVP_DigestVerify(md_ctx, tst->sig, tst->sig_len, tst->msg, tst->msg_len), tst->pass)) goto err; ret = 1; err: EVP_PKEY_free(pkey); BN_CTX_free(bn_ctx); EVP_MD_CTX_free(md_ctx); return ret; } static int rsa_decryption_primitive_test(int id) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; unsigned char pt[2048]; size_t pt_len = sizeof(pt); unsigned char *n = NULL, *e = NULL; size_t n_len = 0, e_len = 0; BN_CTX *bn_ctx = NULL; const struct rsa_decrypt_prim_st *tst = &rsa_decrypt_prim_data[id]; if (!TEST_ptr(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", 2048)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N, &n, &n_len)) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_E, &e, &e_len)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, "")) || !TEST_int_gt(EVP_PKEY_decrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING), 0)) goto err; test_output_memory("n", n, n_len); test_output_memory("e", e, e_len); if (EVP_PKEY_decrypt(ctx, pt, &pt_len, tst->ct, tst->ct_len) <= 0) TEST_note("Decryption Failed"); else test_output_memory("pt", pt, pt_len); ret = 1; err: OPENSSL_free(n); OPENSSL_free(e); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); BN_CTX_free(bn_ctx); return ret; } static int self_test_events(const OSSL_PARAM params[], void *varg) { SELF_TEST_ARGS *args = varg; const OSSL_PARAM *p = NULL; const char *phase = NULL, *type = NULL, *desc = NULL; int ret = 0; if (!args->enable) return 1; args->called++; p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE); if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) goto err; phase = (const char *)p->data; p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC); if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) goto err; desc = (const char *)p->data; p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE); if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) goto err; type = (const char *)p->data; BIO_printf(bio_out, "%s %s %s\n", phase, desc, type); ret = 1; err: return ret; } static int drbg_test(int id) { OSSL_PARAM params[3]; EVP_RAND *rand = NULL; EVP_RAND_CTX *ctx = NULL, *parent = NULL; unsigned char returned_bits[64]; const size_t returned_bits_len = sizeof(returned_bits); unsigned int strength = 256; const struct drbg_st *tst = &drbg_data[id]; int res = 0; if (!TEST_ptr(rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips")) || !TEST_ptr(parent = EVP_RAND_CTX_new(rand, NULL))) goto err; EVP_RAND_free(rand); rand = NULL; params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength); params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(parent, params))) goto err; if (!TEST_ptr(rand = EVP_RAND_fetch(libctx, tst->drbg_name, "")) || !TEST_ptr(ctx = EVP_RAND_CTX_new(rand, parent))) goto err; params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, (int *)&tst->use_df); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, (char *)tst->cipher, 0); params[2] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(ctx, params))) goto err; params[0] = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, (void *)tst->entropy_input, tst->entropy_input_len); params[1] = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE, (void *)tst->nonce, tst->nonce_len); params[2] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_RAND_CTX_set_params(parent, params))) goto err; if (!TEST_true(EVP_RAND_instantiate(ctx, 0, 0, (void *)"", 0, NULL)) || !TEST_true(EVP_RAND_generate(ctx, returned_bits, returned_bits_len, 0, 0, NULL, 0)) || !TEST_true(EVP_RAND_generate(ctx, returned_bits, returned_bits_len, 0, 0, NULL, 0))) goto err; test_output_memory("returned bits", returned_bits, returned_bits_len); if (!TEST_true(EVP_RAND_uninstantiate(ctx)) || !TEST_true(EVP_RAND_uninstantiate(parent))) goto err; if (!TEST_mem_eq(returned_bits, returned_bits_len, tst->returned_bits, tst->returned_bits_len)) goto err; res = 1; err: EVP_RAND_CTX_free(ctx); EVP_RAND_CTX_free(parent); EVP_RAND_free(rand); return res; } static int aes_cfb1_bits_test(void) { int ret = 0; EVP_CIPHER *cipher = NULL; EVP_CIPHER_CTX *ctx = NULL; unsigned char out[16] = { 0 }; int outlen; const OSSL_PARAM *params, *p; static const unsigned char key[] = { 0x12, 0x22, 0x58, 0x2F, 0x1C, 0x1A, 0x8A, 0x88, 0x30, 0xFC, 0x18, 0xB7, 0x24, 0x89, 0x7F, 0xC0 }; static const unsigned char iv[] = { 0x05, 0x28, 0xB5, 0x2B, 0x58, 0x27, 0x63, 0x5C, 0x81, 0x86, 0xD3, 0x63, 0x60, 0xB0, 0xAA, 0x2B }; static const unsigned char pt[] = { 0xB4 }; static const unsigned char expected[] = { 0x6C }; if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, "AES-128-CFB1", "fips=yes"))) goto err; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; if (!TEST_int_gt(EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1), 0)) goto err; if (!TEST_ptr(params = EVP_CIPHER_CTX_settable_params(ctx)) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_USE_BITS))) goto err; EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS); if (!TEST_int_gt(EVP_CipherUpdate(ctx, out, &outlen, pt, 7), 0)) goto err; if (!TEST_int_eq(outlen, 7)) goto err; if (!TEST_mem_eq(out, (outlen + 7) / 8, expected, sizeof(expected))) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } int setup_tests(void) { char *config_file = NULL; OPTION_CHOICE o; 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, &prov_null, config_file, NULL, NULL)) return 0; OSSL_SELF_TEST_set_callback(libctx, self_test_events, &self_test_args); ADD_TEST(aes_cfb1_bits_test); ADD_ALL_TESTS(cipher_enc_dec_test, OSSL_NELEM(cipher_enc_data)); ADD_ALL_TESTS(aes_ccm_enc_dec_test, OSSL_NELEM(aes_ccm_enc_data)); ADD_ALL_TESTS(aes_gcm_enc_dec_test, OSSL_NELEM(aes_gcm_enc_data)); ADD_ALL_TESTS(rsa_keygen_test, OSSL_NELEM(rsa_keygen_data)); ADD_ALL_TESTS(rsa_siggen_test, OSSL_NELEM(rsa_siggen_data)); ADD_ALL_TESTS(rsa_sigver_test, OSSL_NELEM(rsa_sigver_data)); ADD_ALL_TESTS(rsa_decryption_primitive_test, OSSL_NELEM(rsa_decrypt_prim_data)); #ifndef OPENSSL_NO_DH ADD_ALL_TESTS(dh_safe_prime_keygen_test, OSSL_NELEM(dh_safe_prime_keygen_data)); ADD_ALL_TESTS(dh_safe_prime_keyver_test, OSSL_NELEM(dh_safe_prime_keyver_data)); #endif #ifndef OPENSSL_NO_DSA ADD_ALL_TESTS(dsa_keygen_test, OSSL_NELEM(dsa_keygen_data)); ADD_ALL_TESTS(dsa_paramgen_test, OSSL_NELEM(dsa_paramgen_data)); ADD_ALL_TESTS(dsa_pqver_test, OSSL_NELEM(dsa_pqver_data)); ADD_ALL_TESTS(dsa_siggen_test, OSSL_NELEM(dsa_siggen_data)); ADD_ALL_TESTS(dsa_sigver_test, OSSL_NELEM(dsa_sigver_data)); #endif #ifndef OPENSSL_NO_EC ADD_ALL_TESTS(ecdsa_keygen_test, OSSL_NELEM(ecdsa_keygen_data)); ADD_ALL_TESTS(ecdsa_pub_verify_test, OSSL_NELEM(ecdsa_pv_data)); ADD_ALL_TESTS(ecdsa_siggen_test, OSSL_NELEM(ecdsa_siggen_data)); ADD_ALL_TESTS(ecdsa_sigver_test, OSSL_NELEM(ecdsa_sigver_data)); #endif ADD_ALL_TESTS(drbg_test, OSSL_NELEM(drbg_data)); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(prov_null); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/acvp_test.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/conf.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/kdf.h> #include <openssl/provider.h> #include <openssl/core_names.h> #include <openssl/params.h> #include <openssl/param_build.h> #include <openssl/dsa.h> #include <openssl/dh.h> #include <openssl/aes.h> #include <openssl/decoder.h> #include <openssl/rsa.h> #include <openssl/engine.h> #include <openssl/proverr.h> #include "testutil.h" #include "internal/nelem.h" #include "internal/sizes.h" #include "crypto/evp.h" #include "fake_rsaprov.h" #ifdef STATIC_LEGACY OSSL_provider_init_fn ossl_legacy_provider_init; #endif static OSSL_LIB_CTX *testctx = NULL; static char *testpropq = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *deflprov = NULL; static OSSL_PROVIDER *lgcyprov = NULL; static const unsigned char kExampleRSAKeyDER[] = { 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf, }; #ifndef OPENSSL_NO_DSA static const unsigned char kExampleDSAKeyDER[] = { 0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a, 0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d, 0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f, 0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42, 0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38, 0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e, 0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e, 0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04, 0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4, 0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b, 0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14, 0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e, 0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3, 0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef, 0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f, 0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef, 0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38, 0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f, 0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4, 0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d, 0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86, 0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6, 0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a, 0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19, 0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff, 0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f, 0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0, 0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69, 0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07, 0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9, 0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79, 0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f, 0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c, 0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12, 0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45, 0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8, 0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe, 0x40, 0x48 }; #endif static const unsigned char kExampleBadRSAKeyDER[] = { 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef, 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9, 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4, 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3, 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05, 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26, 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0, 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33, 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66, 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde, 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa, 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53, 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c, 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75, 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7, 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c, 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a, 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87, 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd, 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0, 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18, 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01, 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5, 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06, 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0, 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75, 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1, 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00, 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd, 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29, 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5, 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05, 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e, 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf, 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d, 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb, 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5, 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef, 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9, 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2, 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7, 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff, 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99, 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78, 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb, 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35, 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46, 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6, 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f, 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56, 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d, 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55, 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20, 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6, 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81, 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a, 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e, 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3, 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1, 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6, 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77, 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a, 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d, 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b, 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a, 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf, 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28, 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12, 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd, 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a, 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5, 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e, 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b, 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84, 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e, 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97, 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81, 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b, 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6, 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74, 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e, 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9, 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98, 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05, 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef, 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf, 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69, 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce, }; static const unsigned char kExampleBad2RSAKeyDER[] = { 0x30, 0x1b, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00 }; static const unsigned char kMsg[] = { 1, 2, 3, 4 }; static const unsigned char kSignature[] = { 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d, 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e, 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04, 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09, 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67, 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a, 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda, 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48, 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04, 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7, 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42, }; static const unsigned char kExampleRSAKeyPKCS8[] = { 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf, }; #ifndef OPENSSL_NO_EC static const unsigned char kExampleECKeyDER[] = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, }; static const unsigned char kExampleBadECKeyDER[] = { 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51 }; static const unsigned char kExampleECPubKeyDER[] = { 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, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53, 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8, 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7, 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b, 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20, 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5 }; static const unsigned char kExampleBadECPubKeyDER[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53, 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8, 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7, 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b, 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20, 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5 }; static const unsigned char pExampleECParamDER[] = { 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 }; # ifndef OPENSSL_NO_ECX static const unsigned char kExampleED25519KeyDER[] = { 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a, 0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46, 0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50 }; static const unsigned char kExampleED25519PubKeyDER[] = { 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, 0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45, 0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40, 0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29 }; # ifndef OPENSSL_NO_DEPRECATED_3_0 static const unsigned char kExampleX25519KeyDER[] = { 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20, 0xa0, 0x24, 0x3a, 0x31, 0x24, 0xc3, 0x3f, 0xf6, 0x7b, 0x96, 0x0b, 0xd4, 0x8f, 0xd1, 0xee, 0x67, 0xf2, 0x9b, 0x88, 0xac, 0x50, 0xce, 0x97, 0x36, 0xdd, 0xaf, 0x25, 0xf6, 0x10, 0x34, 0x96, 0x6e }; # endif # endif #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 # ifndef OPENSSL_NO_DH static const unsigned char kExampleDHKeyDER[] = { 0x30, 0x82, 0x01, 0x21, 0x02, 0x01, 0x00, 0x30, 0x81, 0x95, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0xf7, 0x52, 0xc2, 0x68, 0xcc, 0x66, 0xc4, 0x8d, 0x03, 0x3f, 0xfa, 0x9c, 0x52, 0xd0, 0xd8, 0x33, 0xf2, 0xe1, 0xc9, 0x9e, 0xb7, 0xe7, 0x6e, 0x90, 0x97, 0xeb, 0x92, 0x91, 0x6a, 0x9a, 0x85, 0x63, 0x92, 0x79, 0xab, 0xb6, 0x3d, 0x23, 0x58, 0x5a, 0xe8, 0x45, 0x06, 0x81, 0x97, 0x77, 0xe1, 0xcc, 0x34, 0x4e, 0xae, 0x36, 0x80, 0xf2, 0xc4, 0x7f, 0x8a, 0x52, 0xb8, 0xdb, 0x58, 0xc8, 0x4b, 0x12, 0x4c, 0xf1, 0x4c, 0x53, 0xc1, 0x89, 0x39, 0x8d, 0xb6, 0x06, 0xd8, 0xea, 0x7f, 0x2d, 0x36, 0x53, 0x96, 0x29, 0xbe, 0xb6, 0x75, 0xfc, 0xe7, 0xf3, 0x36, 0xd6, 0xf4, 0x8f, 0x16, 0xa6, 0xc7, 0xec, 0x7b, 0xce, 0x42, 0x8d, 0x48, 0x2e, 0xb7, 0x74, 0x00, 0x11, 0x52, 0x61, 0xb4, 0x19, 0x35, 0xec, 0x5c, 0xe4, 0xbe, 0x34, 0xc6, 0x59, 0x64, 0x5e, 0x42, 0x61, 0x70, 0x54, 0xf4, 0xe9, 0x6b, 0x53, 0x02, 0x01, 0x02, 0x04, 0x81, 0x83, 0x02, 0x81, 0x80, 0x64, 0xc2, 0xe3, 0x09, 0x69, 0x37, 0x3c, 0xd2, 0x4a, 0xba, 0xc3, 0x78, 0x6a, 0x9b, 0x8a, 0x2a, 0xdb, 0xe7, 0xe6, 0xc0, 0xfa, 0x3a, 0xbe, 0x39, 0x67, 0xc0, 0xa9, 0x2a, 0xf0, 0x0a, 0xc1, 0x53, 0x1c, 0xdb, 0xfa, 0x1a, 0x26, 0x98, 0xb0, 0x8c, 0xc6, 0x06, 0x4a, 0xa2, 0x48, 0xd3, 0xa4, 0x3b, 0xbd, 0x05, 0x48, 0xea, 0x59, 0xdb, 0x18, 0xa4, 0xca, 0x66, 0xd9, 0x5d, 0xb8, 0x95, 0xd1, 0xeb, 0x97, 0x3d, 0x66, 0x97, 0x5c, 0x86, 0x8f, 0x7e, 0x90, 0xd3, 0x43, 0xd1, 0xa2, 0x0d, 0xcb, 0xe7, 0xeb, 0x90, 0xea, 0x09, 0x40, 0xb1, 0x6f, 0xf7, 0x4c, 0xf2, 0x41, 0x83, 0x1d, 0xd0, 0x76, 0xef, 0xaf, 0x55, 0x6f, 0x5d, 0xa9, 0xa3, 0x55, 0x81, 0x2a, 0xd1, 0x5d, 0x9d, 0x22, 0x77, 0x97, 0x83, 0xde, 0xad, 0xb6, 0x5d, 0x19, 0xc1, 0x53, 0xec, 0xfb, 0xaf, 0x06, 0x2e, 0x87, 0x2a, 0x0b, 0x7a }; # endif #endif static const unsigned char kCFBDefaultKey[] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; static const unsigned char kGCMDefaultKey[32] = { 0 }; static const unsigned char kGCMResetKey[] = { 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 unsigned char iCFBIV[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; static const unsigned char iGCMDefaultIV[12] = { 0 }; static const unsigned char iGCMResetIV1[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad }; static const unsigned char iGCMResetIV2[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88 }; static const unsigned char cfbPlaintext[] = { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A }; static const unsigned char cfbPlaintext_partial[] = { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, }; static const unsigned char gcmDefaultPlaintext[16] = { 0 }; static const unsigned char gcmResetPlaintext[] = { 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 unsigned char cfbCiphertext[] = { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A }; static const unsigned char cfbCiphertext_partial[] = { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A, 0x0D, 0x4A, 0x71, 0x82, 0x90, 0xF0, 0x9A, 0x35 }; static const unsigned char ofbCiphertext_partial[] = { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A, 0xB2, 0x65, 0x64, 0x38, 0x26, 0xD2, 0xBC, 0x09 }; static const unsigned char gcmDefaultCiphertext[] = { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 }; static const unsigned char gcmResetCiphertext1[] = { 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 unsigned char gcmResetCiphertext2[] = { 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 unsigned char gcmAAD[] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }; static const unsigned char gcmDefaultTag[] = { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 }; static const unsigned char gcmResetTag1[] = { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 }; static const unsigned char gcmResetTag2[] = { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b }; typedef struct APK_DATA_st { const unsigned char *kder; size_t size; const char *keytype; int evptype; int check; int pub_check; int param_check; int type; } APK_DATA; static APK_DATA keydata[] = { {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA}, {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA}, #ifndef OPENSSL_NO_EC {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC} #endif }; static APK_DATA keycheckdata[] = { {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1, 0}, {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA, 0, 1, 1, 0}, {kExampleBad2RSAKeyDER, sizeof(kExampleBad2RSAKeyDER), "RSA", EVP_PKEY_RSA, 0, 0, 1 , 0}, #ifndef OPENSSL_NO_EC {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0}, {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1, 1, 1}, {pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1, 2}, # ifndef OPENSSL_NO_ECX {kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519", EVP_PKEY_ED25519, 1, 1, 1, 0}, {kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519", EVP_PKEY_ED25519, 0, 1, 1, 1}, # endif #endif }; static EVP_PKEY *load_example_key(const char *keytype, const unsigned char *data, size_t data_len) { const unsigned char **pdata = &data; EVP_PKEY *pkey = NULL; OSSL_DECODER_CTX *dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0, testctx, testpropq); (void)OSSL_DECODER_from_data(dctx, pdata, &data_len); OSSL_DECODER_CTX_free(dctx); return pkey; } static EVP_PKEY *load_example_rsa_key(void) { return load_example_key("RSA", kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER)); } #ifndef OPENSSL_NO_DSA static EVP_PKEY *load_example_dsa_key(void) { return load_example_key("DSA", kExampleDSAKeyDER, sizeof(kExampleDSAKeyDER)); } #endif #ifndef OPENSSL_NO_EC static EVP_PKEY *load_example_ec_key(void) { return load_example_key("EC", kExampleECKeyDER, sizeof(kExampleECKeyDER)); } #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 # ifndef OPENSSL_NO_DH static EVP_PKEY *load_example_dh_key(void) { return load_example_key("DH", kExampleDHKeyDER, sizeof(kExampleDHKeyDER)); } # endif # ifndef OPENSSL_NO_ECX static EVP_PKEY *load_example_ed25519_key(void) { return load_example_key("ED25519", kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER)); } static EVP_PKEY *load_example_x25519_key(void) { return load_example_key("X25519", kExampleX25519KeyDER, sizeof(kExampleX25519KeyDER)); } # endif #endif static EVP_PKEY *load_example_hmac_key(void) { EVP_PKEY *pkey = NULL; unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; pkey = EVP_PKEY_new_raw_private_key_ex(testctx, "HMAC", NULL, key, sizeof(key)); if (!TEST_ptr(pkey)) return NULL; return pkey; } static int test_EVP_set_default_properties(void) { OSSL_LIB_CTX *ctx; EVP_MD *md = NULL; int res = 0; if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()) || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL))) goto err; EVP_MD_free(md); md = NULL; if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang")) || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL)) || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider"))) goto err; EVP_MD_free(md); md = NULL; if (!TEST_true(EVP_set_default_properties(ctx, NULL)) || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL))) goto err; res = 1; err: EVP_MD_free(md); OSSL_LIB_CTX_free(ctx); return res; } #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC) static EVP_PKEY *make_key_fromdata(char *keytype, OSSL_PARAM *params) { EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *tmp_pkey = NULL, *pkey = NULL; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq))) goto err; if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &tmp_pkey, EVP_PKEY_KEYPAIR, params), 0)) goto err; if (!TEST_ptr(tmp_pkey)) goto err; pkey = tmp_pkey; tmp_pkey = NULL; err: EVP_PKEY_free(tmp_pkey); EVP_PKEY_CTX_free(pctx); return pkey; } static int test_selection(EVP_PKEY *pkey, int selection) { int testresult = 0; int ret; BIO *bio = BIO_new(BIO_s_mem()); ret = PEM_write_bio_PUBKEY(bio, pkey); if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { if (!TEST_true(ret)) goto err; } else { if (!TEST_false(ret)) goto err; } ret = PEM_write_bio_PrivateKey_ex(bio, pkey, NULL, NULL, 0, NULL, NULL, testctx, NULL); if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { if (!TEST_true(ret)) goto err; } else { if (!TEST_false(ret)) goto err; } testresult = 1; err: BIO_free(bio); return testresult; } #endif #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) static int test_EVP_PKEY_ffc_priv_pub(char *keytype) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; EVP_PKEY *just_params = NULL; EVP_PKEY *params_and_priv = NULL; EVP_PKEY *params_and_pub = NULL; EVP_PKEY *params_and_keypair = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL; int ret = 0; if (!TEST_ptr(p = BN_new()) || !TEST_ptr(q = BN_new()) || !TEST_ptr(g = BN_new()) || !TEST_ptr(pub = BN_new()) || !TEST_ptr(priv = BN_new())) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !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))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(just_params = make_key_fromdata(keytype, params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !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_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_priv = make_key_fromdata(keytype, params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PRIVATE_KEY) || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !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_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_pub = make_key_fromdata(keytype, params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY) || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !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_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_keypair = make_key_fromdata(keytype, params))) goto err; if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(just_params); EVP_PKEY_free(params_and_priv); EVP_PKEY_free(params_and_pub); EVP_PKEY_free(params_and_keypair); BN_free(p); BN_free(q); BN_free(g); BN_free(pub); BN_free(priv); return ret; } #endif #ifndef OPENSSL_NO_EC static unsigned char ec_priv[] = { 0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23, 0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5, 0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16 }; static unsigned char ec_pub[] = { 0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97, 0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81, 0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d, 0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e, 0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1, 0x08, 0x09, 0xb8, 0xdb, 0x03 }; static int test_EC_priv_pub(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; EVP_PKEY *just_params = NULL; EVP_PKEY *params_and_priv = NULL; EVP_PKEY *params_and_pub = NULL; EVP_PKEY *params_and_keypair = NULL; BIGNUM *priv = NULL; int ret = 0; unsigned char *encoded = NULL; size_t len = 0; unsigned char buffer[128]; if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(just_params = make_key_fromdata("EC", params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_priv = make_key_fromdata("EC", params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, ec_pub, sizeof(ec_pub)))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_pub = make_key_fromdata("EC", params))) goto err; OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); params = NULL; bld = NULL; if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY) || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, ec_pub, sizeof(ec_pub))) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(params_and_keypair = make_key_fromdata("EC", params))) goto err; if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR)) goto err; if (!TEST_int_gt(EVP_PKEY_parameters_eq(just_params, just_params), 0) || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_pub), 0) || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_priv), 0) || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_keypair), 0) || !TEST_int_gt(EVP_PKEY_eq(params_and_pub, params_and_pub), 0) || !TEST_int_gt(EVP_PKEY_eq(params_and_priv, params_and_priv), 0) || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_pub), 0) || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_priv), 0)) goto err; if (!TEST_int_gt(EVP_PKEY_get1_encoded_public_key(params_and_pub, &encoded), 0)) goto err; OPENSSL_free(encoded); encoded = NULL; if (!TEST_int_eq(EVP_PKEY_get1_encoded_public_key(just_params, &encoded), 0)) { OPENSSL_free(encoded); encoded = NULL; goto err; } if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, buffer, sizeof(buffer), &len), 1) || !TEST_int_eq(len, 65)) goto err; len = 0; if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0, &len), 1) || !TEST_int_eq(len, 65)) goto err; if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, buffer, 10, &len), 0)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(just_params); EVP_PKEY_free(params_and_priv); EVP_PKEY_free(params_and_pub); EVP_PKEY_free(params_and_keypair); BN_free(priv); return ret; } static int test_evp_get_ec_pub(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; unsigned char *pad = NULL; EVP_PKEY *keypair = NULL; BIGNUM *priv = NULL; BIGNUM *x = NULL; BIGNUM *y = NULL; int ret = 0; if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) goto err; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, ec_pub, sizeof(ec_pub))) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(keypair = make_key_fromdata("EC", params))) goto err; if (!test_selection(keypair, EVP_PKEY_KEYPAIR)) goto err; if (!EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_X, &x) || !EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_Y, &y)) goto err; if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub)))) goto err; pad[0] = ec_pub[0]; BN_bn2bin(x, &pad[1]); BN_bn2bin(y, &pad[33]); if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0)) goto err; ret = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(keypair); OPENSSL_free(pad); BN_free(priv); BN_free(x); BN_free(y); return ret; } # ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_EC_priv_only_legacy(void) { BIGNUM *priv = NULL; int ret = 0; EC_KEY *eckey = NULL; EVP_PKEY *pkey = NULL, *dup_pk = NULL; EVP_MD_CTX *ctx = NULL; if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) goto err; eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); if (!TEST_ptr(eckey)) goto err; if (!TEST_true(EC_KEY_set_private_key(eckey, priv))) goto err; pkey = EVP_PKEY_new(); if (!TEST_ptr(pkey)) goto err; if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))) goto err; eckey = NULL; for (;;) { ret = 0; ctx = EVP_MD_CTX_new(); if (!TEST_ptr(ctx)) goto err; if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx, testpropq, pkey, NULL))) goto err; EVP_MD_CTX_free(ctx); ctx = NULL; if (dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))) goto err; ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2); EVP_PKEY_free(pkey); pkey = dup_pk; if (!ret) goto err; } ret = 1; err: EVP_MD_CTX_free(ctx); EVP_PKEY_free(pkey); EC_KEY_free(eckey); BN_free(priv); return ret; } static int test_evp_get_ec_pub_legacy(void) { OSSL_LIB_CTX *libctx = NULL; unsigned char *pad = NULL; EVP_PKEY *pkey = NULL; EC_KEY *eckey = NULL; BIGNUM *priv = NULL; BIGNUM *x = NULL; BIGNUM *y = NULL; int ret = 0; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())) goto err; if (!TEST_ptr(eckey = EC_KEY_new_by_curve_name_ex(libctx, NULL, NID_X9_62_prime256v1))) goto err; if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) goto err; if (!TEST_true(EC_KEY_set_private_key(eckey, priv))) goto err; if (!TEST_ptr(x = BN_bin2bn(&ec_pub[1], 32, NULL))) goto err; if (!TEST_ptr(y = BN_bin2bn(&ec_pub[33], 32, NULL))) goto err; if (!TEST_true(EC_KEY_set_public_key_affine_coordinates(eckey, x, y))) goto err; if (!TEST_ptr(pkey = EVP_PKEY_new())) goto err; if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))) goto err; eckey = NULL; if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &x)) || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &y))) goto err; if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub)))) goto err; pad[0] = ec_pub[0]; BN_bn2bin(x, &pad[1]); BN_bn2bin(y, &pad[33]); if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0)) goto err; ret = 1; err: OSSL_LIB_CTX_free(libctx); EVP_PKEY_free(pkey); EC_KEY_free(eckey); OPENSSL_free(pad); BN_free(priv); BN_free(x); BN_free(y); return ret; } # endif #endif static int test_EVP_PKEY_sign(int tst) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *sig = NULL; size_t sig_len = 0, shortsig_len = 1; EVP_PKEY_CTX *ctx = NULL; unsigned char tbs[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }; if (tst == 0) { if (!TEST_ptr(pkey = load_example_rsa_key())) goto out; } else if (tst == 1) { #ifndef OPENSSL_NO_DSA if (!TEST_ptr(pkey = load_example_dsa_key())) goto out; #else ret = 1; goto out; #endif } else { #ifndef OPENSSL_NO_EC if (!TEST_ptr(pkey = load_example_ec_key())) goto out; #else ret = 1; goto out; #endif } ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL); if (!TEST_ptr(ctx) || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs, sizeof(tbs)), 0)) goto out; sig = OPENSSL_malloc(sig_len); if (!TEST_ptr(sig) || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs, sizeof(tbs)), 0) || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)), 0) || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)), 0)) goto out; ret = 1; out: EVP_PKEY_CTX_free(ctx); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_EVP_PKEY_sign_with_app_method(int tst) { int ret = 0; EVP_PKEY *pkey = NULL; RSA *rsa = NULL; RSA_METHOD *rsa_meth = NULL; #ifndef OPENSSL_NO_DSA DSA *dsa = NULL; DSA_METHOD *dsa_meth = NULL; #endif unsigned char *sig = NULL; size_t sig_len = 0, shortsig_len = 1; EVP_PKEY_CTX *ctx = NULL; unsigned char tbs[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }; if (tst == 0) { if (!TEST_ptr(pkey = load_example_rsa_key())) goto out; if (!TEST_ptr(rsa_meth = RSA_meth_dup(RSA_get_default_method()))) goto out; if (!TEST_ptr(rsa = EVP_PKEY_get1_RSA(pkey)) || !TEST_int_gt(RSA_set_method(rsa, rsa_meth), 0) || !TEST_int_gt(EVP_PKEY_assign_RSA(pkey, rsa), 0)) goto out; rsa = NULL; } else { #ifndef OPENSSL_NO_DSA if (!TEST_ptr(pkey = load_example_dsa_key())) goto out; if (!TEST_ptr(dsa_meth = DSA_meth_dup(DSA_get_default_method()))) goto out; if (!TEST_ptr(dsa = EVP_PKEY_get1_DSA(pkey)) || !TEST_int_gt(DSA_set_method(dsa, dsa_meth), 0) || !TEST_int_gt(EVP_PKEY_assign_DSA(pkey, dsa), 0)) goto out; dsa = NULL; #else ret = 1; goto out; #endif } ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL); if (!TEST_ptr(ctx) || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs, sizeof(tbs)), 0)) goto out; sig = OPENSSL_malloc(sig_len); if (!TEST_ptr(sig) || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs, sizeof(tbs)), 0) || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)), 0) || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)), 0)) goto out; ret = 1; out: EVP_PKEY_CTX_free(ctx); OPENSSL_free(sig); EVP_PKEY_free(pkey); RSA_free(rsa); RSA_meth_free(rsa_meth); #ifndef OPENSSL_NO_DSA DSA_free(dsa); DSA_meth_free(dsa_meth); #endif return ret; } #endif static int test_EVP_Enveloped(int n) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; EVP_PKEY *keypair = NULL; unsigned char *kek = NULL; unsigned char iv[EVP_MAX_IV_LENGTH]; static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int len, kek_len, ciphertext_len, plaintext_len; unsigned char ciphertext[32], plaintext[16]; EVP_CIPHER *type = NULL; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (n == 0) type = (EVP_CIPHER *)EVP_aes_256_cbc(); else if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "AES-256-CBC", testpropq))) goto err; if (!TEST_ptr(keypair = load_example_rsa_key()) || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_get_size(keypair))) || !TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv, &keypair, 1)) || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len, msg, sizeof(msg))) || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len, &len))) goto err; ciphertext_len += len; if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair)) || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len, ciphertext, ciphertext_len)) || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len))) goto err; plaintext_len += len; if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len)) goto err; ret = 1; err: if (n != 0) EVP_CIPHER_free(type); OPENSSL_free(kek); EVP_PKEY_free(keypair); EVP_CIPHER_CTX_free(ctx); return ret; } static int test_EVP_DigestSignInit(int tst) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *sig = NULL, *sig2 = NULL; size_t sig_len = 0, sig2_len = 0, shortsig_len = 1; EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL; EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL; BIO *mdbio = NULL, *membio = NULL; size_t written; const EVP_MD *md; EVP_MD *mdexp = NULL; int reinit = 0; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (tst >= 15) { reinit = 1; tst -= 15; } if (tst >= 6 && tst <= 8) { membio = BIO_new(BIO_s_mem()); mdbio = BIO_new(BIO_f_md()); if (!TEST_ptr(membio) || !TEST_ptr(mdbio)) goto out; BIO_push(mdbio, membio); if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0)) goto out; } else { if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new()) || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new())) goto out; } if (tst % 3 == 0) { if (!TEST_ptr(pkey = load_example_rsa_key())) goto out; } else if (tst % 3 == 1) { #ifndef OPENSSL_NO_DSA if (!TEST_ptr(pkey = load_example_dsa_key())) goto out; #else ret = 1; goto out; #endif } else { if (!TEST_ptr(pkey = load_example_hmac_key())) goto out; } if (tst >= 3 && tst <= 5) md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL); else md = EVP_sha256(); if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey))) goto out; if (reinit && !TEST_true(EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, NULL))) goto out; if (tst >= 6 && tst <= 8) { if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)) goto out; } else if (tst < 6) { if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg)))) goto out; } if (tst >= 9) { if (!TEST_true(EVP_DigestSign(md_ctx, NULL, &sig_len, kMsg, sizeof(kMsg))) || !TEST_ptr(sig = OPENSSL_malloc(sig_len))) goto out; if (tst <= 11) { if (!TEST_false(EVP_DigestSign(md_ctx, sig, &shortsig_len, kMsg, sizeof(kMsg)))) goto out; ret = 1; goto out; } if (!TEST_true(EVP_DigestSign(md_ctx, sig, &sig_len, kMsg, sizeof(kMsg)))) goto out; } else { if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) || !TEST_ptr(sig = OPENSSL_malloc(sig_len)) || !TEST_false(EVP_DigestSignFinal(md_ctx, sig, &shortsig_len)) || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len))) goto out; } if (tst % 3 != 2) { if (tst >= 6 && tst <= 8) { if (!TEST_int_gt(BIO_reset(mdbio), 0) || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0)) goto out; } if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md, NULL, pkey))) goto out; if (tst >= 6 && tst <= 8) { if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))) goto out; } else { if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg)))) goto out; } if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0)) goto out; if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0)) goto out; } else { if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig2_len)) || !TEST_ptr(sig2 = OPENSSL_malloc(sig2_len)) || !TEST_true(EVP_DigestSignFinal(md_ctx, sig2, &sig2_len))) goto out; if (!TEST_mem_eq(sig, sig_len, sig2, sig2_len)) goto out; } ret = 1; out: BIO_free(membio); BIO_free(mdbio); EVP_MD_CTX_free(a_md_ctx); EVP_MD_CTX_free(a_md_ctx_verify); EVP_PKEY_free(pkey); OPENSSL_free(sig); OPENSSL_free(sig2); EVP_MD_free(mdexp); return ret; } static int test_EVP_DigestVerifyInit(void) { int ret = 0; EVP_PKEY *pkey = NULL; EVP_MD_CTX *md_ctx = NULL; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()) || !TEST_ptr(pkey = load_example_rsa_key())) goto out; if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey)) || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg))) || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature, sizeof(kSignature)), 0)) goto out; if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, NULL)) || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg))) || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature, sizeof(kSignature)), 0)) goto out; ret = 1; out: EVP_MD_CTX_free(md_ctx); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_SIPHASH static int test_siphash_digestsign(void) { unsigned char key[16]; unsigned char buf[8], digest[8]; unsigned char expected[8] = { 0x6d, 0x3e, 0x54, 0xc2, 0x2f, 0xf1, 0xfe, 0xe2 }; EVP_PKEY *pkey = NULL; EVP_MD_CTX *mdctx = NULL; EVP_PKEY_CTX *ctx = NULL; int ret = 0; size_t len = 8; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); memset(buf, 0, 8); memset(key, 1, 16); if (!TEST_ptr(pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_SIPHASH, NULL, key, 16))) goto out; if (!TEST_ptr(mdctx = EVP_MD_CTX_create())) goto out; if (!TEST_true(EVP_DigestSignInit(mdctx, &ctx, NULL, NULL, pkey))) goto out; if (!TEST_int_eq(EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_SIGNCTX, EVP_PKEY_CTRL_SET_DIGEST_SIZE, 8, NULL), 1)) goto out; if (!TEST_true(EVP_DigestSignInit(mdctx, NULL, NULL, NULL, NULL))) goto out; if (!TEST_true(EVP_DigestSignUpdate(mdctx, buf, 8))) goto out; if (!TEST_true(EVP_DigestSignFinal(mdctx, digest, &len))) goto out; if (!TEST_mem_eq(digest, len, expected, sizeof(expected))) goto out; ret = 1; out: EVP_PKEY_free(pkey); EVP_MD_CTX_free(mdctx); return ret; } #endif static int test_EVP_Digest(void) { int ret = 0; EVP_MD_CTX *md_ctx = NULL; unsigned char md[EVP_MAX_MD_SIZE]; EVP_MD *sha256 = NULL; EVP_MD *shake256 = NULL; if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())) goto out; if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq)) || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq))) goto out; if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL)) || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg))) || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL)) || !TEST_ptr_eq(EVP_MD_CTX_get0_md(md_ctx), NULL)) goto out; if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL)) || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg))) || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL)) || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx)) || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL))) goto out; if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL)) || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg))) || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md))) || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx)) || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL))) goto out; ret = 1; out: EVP_MD_CTX_free(md_ctx); EVP_MD_free(sha256); EVP_MD_free(shake256); return ret; } static int test_EVP_md_null(void) { int ret = 0; EVP_MD_CTX *md_ctx = NULL; const EVP_MD *md_null = EVP_md_null(); unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned int md_len = sizeof(md_value); if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (!TEST_ptr(md_null) || !TEST_ptr(md_ctx = EVP_MD_CTX_new())) goto out; if (!TEST_true(EVP_DigestInit_ex(md_ctx, md_null, NULL)) || !TEST_true(EVP_DigestUpdate(md_ctx, "test", 4)) || !TEST_true(EVP_DigestFinal_ex(md_ctx, md_value, &md_len))) goto out; if (!TEST_uint_eq(md_len, 0)) goto out; ret = 1; out: EVP_MD_CTX_free(md_ctx); return ret; } static int test_d2i_AutoPrivateKey(int i) { int ret = 0; const unsigned char *p; EVP_PKEY *pkey = NULL; const APK_DATA *ak = &keydata[i]; const unsigned char *input = ak->kder; size_t input_len = ak->size; int expected_id = ak->evptype; p = input; if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len)) || !TEST_ptr_eq(p, input + input_len) || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id)) goto done; ret = 1; done: EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_EC static const unsigned char ec_public_sect163k1_validxy[] = { 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04, 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69, 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b, 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3, 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f }; static const unsigned char ec_public_sect163k1_badx[] = { 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04, 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69, 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b, 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3, 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f }; static const unsigned char ec_public_sect163k1_bady[] = { 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04, 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69, 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b, 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3, 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6 }; static struct ec_der_pub_keys_st { const unsigned char *der; size_t len; int valid; } ec_der_pub_keys[] = { { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 }, { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 }, { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 }, }; static int test_invalide_ec_char2_pub_range_decode(int id) { int ret = 0; EVP_PKEY *pkey; pkey = load_example_key("EC", ec_der_pub_keys[id].der, ec_der_pub_keys[id].len); ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey)) || TEST_ptr_null(pkey); EVP_PKEY_free(pkey); return ret; } static int test_EVP_PKCS82PKEY(void) { int ret = 0; const unsigned char *derp = kExampleBadECKeyDER; PKCS8_PRIV_KEY_INFO *p8inf = NULL; EVP_PKEY *pkey = NULL; if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER)))) goto done; if (!TEST_ptr_eq(derp, kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER))) goto done; if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf))) goto done; ret = 1; done: PKCS8_PRIV_KEY_INFO_free(p8inf); EVP_PKEY_free(pkey); return ret; } #endif static int test_EVP_PKCS82PKEY_wrong_tag(void) { EVP_PKEY *pkey = NULL; EVP_PKEY *pkey2 = NULL; BIO *membio = NULL; char *membuf = NULL; PKCS8_PRIV_KEY_INFO *p8inf = NULL; int ok = 0; if (testctx != NULL) return 1; if (!TEST_ptr(membio = BIO_new(BIO_s_mem())) || !TEST_ptr(pkey = load_example_rsa_key()) || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL, NULL, 0, NULL, NULL), 0) || !TEST_int_gt(BIO_get_mem_data(membio, &membuf), 0) || !TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(membio, NULL)) || !TEST_ptr(pkey2 = EVP_PKCS82PKEY(p8inf)) || !TEST_int_eq(ERR_peek_last_error(), 0)) { goto done; } ok = 1; done: EVP_PKEY_free(pkey); EVP_PKEY_free(pkey2); PKCS8_PRIV_KEY_INFO_free(p8inf); BIO_free_all(membio); return ok; } static int test_privatekey_to_pkcs8(void) { EVP_PKEY *pkey = NULL; BIO *membio = NULL; char *membuf = NULL; long membuf_len = 0; int ok = 0; if (!TEST_ptr(membio = BIO_new(BIO_s_mem())) || !TEST_ptr(pkey = load_example_rsa_key()) || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL, NULL, 0, NULL, NULL), 0) || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0) || !TEST_ptr(membuf) || !TEST_mem_eq(membuf, (size_t)membuf_len, kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8)) || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL, NULL, 0, NULL, NULL), 0)) goto done; ok = 1; done: EVP_PKEY_free(pkey); BIO_free_all(membio); return ok; } #ifndef OPENSSL_NO_EC static const struct { int encoding; const char *encoding_name; } ec_encodings[] = { { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT }, { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP } }; static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg) { const OSSL_PARAM *p; const char *enc_name = NULL; int *enc = arg; size_t i; *enc = -1; if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING)) || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name))) return 0; for (i = 0; i < OSSL_NELEM(ec_encodings); i++) { if (OPENSSL_strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) { *enc = ec_encodings[i].encoding; break; } } return (*enc != -1); } static int test_EC_keygen_with_enc(int idx) { EVP_PKEY *params = NULL, *key = NULL; EVP_PKEY_CTX *pctx = NULL, *kctx = NULL; int enc; int ret = 0; enc = ec_encodings[idx].encoding; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_group_name(pctx, "P-256"), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc), 0) || !TEST_true(EVP_PKEY_paramgen(pctx, &params)) || !TEST_ptr(params)) goto done; if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0) || !TEST_true(EVP_PKEY_keygen(kctx, &key)) || !TEST_ptr(key)) goto done; if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL, ec_export_get_encoding_cb, &enc)) || !TEST_int_eq(enc, ec_encodings[idx].encoding)) goto done; ret = 1; done: EVP_PKEY_free(key); EVP_PKEY_free(params); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(pctx); return ret; } #endif #if !defined(OPENSSL_NO_SM2) static int test_EVP_SM2_verify(void) { const char *pubkey = "-----BEGIN PUBLIC KEY-----\n" "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n" "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n" "-----END PUBLIC KEY-----\n"; const char *msg = "message digest"; const char *id = "ALICE123@YAHOO.COM"; const uint8_t signature[] = { 0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb, 0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06, 0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36, 0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d, 0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07, 0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24, 0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70 }; int rc = 0; BIO *bio = NULL; EVP_PKEY *pkey = NULL; EVP_MD_CTX *mctx = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_MD *sm3 = NULL; bio = BIO_new_mem_buf(pubkey, strlen(pubkey)); if (!TEST_true(bio != NULL)) goto done; pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq); if (!TEST_true(pkey != NULL)) goto done; if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2"))) goto done; if (!TEST_ptr(mctx = EVP_MD_CTX_new())) goto done; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq))) goto done; EVP_MD_CTX_set_pkey_ctx(mctx, pctx); if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq))) goto done; if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey))) goto done; if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0)) goto done; if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg)))) goto done; if (!TEST_int_gt(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature)), 0)) goto done; rc = 1; done: BIO_free(bio); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(pctx); EVP_MD_CTX_free(mctx); EVP_MD_free(sm3); return rc; } static int test_EVP_SM2(void) { int ret = 0; EVP_PKEY *pkey = NULL; EVP_PKEY *pkeyparams = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY_CTX *kctx = NULL; EVP_PKEY_CTX *sctx = NULL; size_t sig_len = 0; unsigned char *sig = NULL; EVP_MD_CTX *md_ctx = NULL; EVP_MD_CTX *md_ctx_verify = NULL; EVP_PKEY_CTX *cctx = NULL; EVP_MD *check_md = NULL; uint8_t ciphertext[128]; size_t ctext_len = sizeof(ciphertext); uint8_t plaintext[8]; size_t ptext_len = sizeof(plaintext); uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'}; OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END}; OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END}; int i; char mdname[OSSL_MAX_NAME_SIZE]; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "SM2", testpropq))) goto done; if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1)) goto done; if (!TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2), 0)) goto done; if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams))) goto done; if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkeyparams, testpropq))) goto done; if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)) goto done; if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey))) goto done; if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())) goto done; if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())) goto done; if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq))) goto done; EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx); EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx); if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, "sm3", testpropq))) goto done; if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey))) goto done; if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0)) goto done; if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg)))) goto done; if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))) goto done; if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))) goto done; if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len))) goto done; if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL, pkey))) goto done; if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0)) goto done; if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg)))) goto done; if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0)) goto done; if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL, pkey))) goto done; if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, NULL, 0), 0)) goto done; if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg)))) goto done; if (!TEST_int_eq(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0)) goto done; gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, mdname, sizeof(mdname)); for (i = 0; i < 2; i++) { const char *mdnames[] = { #ifndef OPENSSL_NO_SM3 "SM3", #else NULL, #endif "SHA2-256" }; EVP_PKEY_CTX_free(cctx); if (mdnames[i] == NULL) continue; sparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, (char *)mdnames[i], 0); if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq))) goto done; if (!TEST_true(EVP_PKEY_encrypt_init(cctx))) goto done; if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams))) goto done; if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg, sizeof(kMsg)))) goto done; if (!TEST_int_gt(EVP_PKEY_decrypt_init(cctx), 0)) goto done; if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams))) goto done; if (!TEST_int_gt(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext, ctext_len), 0)) goto done; if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams))) goto done; EVP_MD_free(check_md); if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, mdname, testpropq))) goto done; if (!TEST_true(EVP_MD_is_a(check_md, mdnames[i]))) { TEST_info("Fetched md %s isn't %s", mdname, mdnames[i]); goto done; } if (!TEST_true(ptext_len == sizeof(kMsg))) goto done; if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0)) goto done; } ret = 1; done: EVP_PKEY_CTX_free(pctx); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(sctx); EVP_PKEY_CTX_free(cctx); EVP_PKEY_free(pkey); EVP_PKEY_free(pkeyparams); EVP_MD_CTX_free(md_ctx); EVP_MD_CTX_free(md_ctx_verify); EVP_MD_free(check_md); OPENSSL_free(sig); return ret; } #endif static struct keys_st { int type; char *priv; char *pub; } keys[] = { { EVP_PKEY_HMAC, "0123456789", NULL }, { EVP_PKEY_HMAC, "", NULL #ifndef OPENSSL_NO_POLY1305 }, { EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL #endif #ifndef OPENSSL_NO_SIPHASH }, { EVP_PKEY_SIPHASH, "0123456789012345", NULL #endif }, #ifndef OPENSSL_NO_ECX { EVP_PKEY_X25519, "01234567890123456789012345678901", "abcdefghijklmnopqrstuvwxyzabcdef" }, { EVP_PKEY_ED25519, "01234567890123456789012345678901", "abcdefghijklmnopqrstuvwxyzabcdef" }, { EVP_PKEY_X448, "01234567890123456789012345678901234567890123456789012345", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd" }, { EVP_PKEY_ED448, "012345678901234567890123456789012345678901234567890123456", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde" } #endif }; static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx) { int ret = 0; unsigned char buf[80]; unsigned char *in; size_t inlen, len = 0, shortlen = 1; EVP_PKEY *pkey; if (pub && keys[tst].pub == NULL) return 1; memset(buf, 0, sizeof(buf)); if (pub) { #ifndef OPENSSL_NO_EC inlen = strlen(keys[tst].pub); in = (unsigned char *)keys[tst].pub; if (uselibctx) { pkey = EVP_PKEY_new_raw_public_key_ex( testctx, OBJ_nid2sn(keys[tst].type), NULL, in, inlen); } else { pkey = EVP_PKEY_new_raw_public_key(keys[tst].type, NULL, in, inlen); } #else return 1; #endif } else { inlen = strlen(keys[tst].priv); in = (unsigned char *)keys[tst].priv; if (uselibctx) { pkey = EVP_PKEY_new_raw_private_key_ex( testctx, OBJ_nid2sn(keys[tst].type), NULL, in, inlen); } else { pkey = EVP_PKEY_new_raw_private_key(keys[tst].type, NULL, in, inlen); } } if (!TEST_ptr(pkey) || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey), 1) || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len))) || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len))) || !TEST_true(len == inlen)) goto done; if (tst != 1) { if ((!pub && !TEST_false(EVP_PKEY_get_raw_private_key(pkey, buf, &shortlen))) || (pub && !TEST_false(EVP_PKEY_get_raw_public_key(pkey, buf, &shortlen)))) goto done; } if ((!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len))) || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len))) || !TEST_mem_eq(in, inlen, buf, len)) goto done; ret = 1; done: EVP_PKEY_free(pkey); return ret; } static int test_set_get_raw_keys(int tst) { return (nullprov != NULL || test_set_get_raw_keys_int(tst, 0, 0)) && test_set_get_raw_keys_int(tst, 0, 1) && (nullprov != NULL || test_set_get_raw_keys_int(tst, 1, 0)) && test_set_get_raw_keys_int(tst, 1, 1); } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int pkey_custom_check(EVP_PKEY *pkey) { return 0xbeef; } static int pkey_custom_pub_check(EVP_PKEY *pkey) { return 0xbeef; } static int pkey_custom_param_check(EVP_PKEY *pkey) { return 0xbeef; } static EVP_PKEY_METHOD *custom_pmeth; #endif static int test_EVP_PKEY_check(int i) { int ret = 0; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *ctx = NULL; #ifndef OPENSSL_NO_DEPRECATED_3_0 EVP_PKEY_CTX *ctx2 = NULL; #endif const APK_DATA *ak = &keycheckdata[i]; const unsigned char *input = ak->kder; size_t input_len = ak->size; int expected_id = ak->evptype; int expected_check = ak->check; int expected_pub_check = ak->pub_check; int expected_param_check = ak->param_check; int type = ak->type; if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len))) goto done; if (type == 0 && !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id)) goto done; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq))) goto done; if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check)) goto done; if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check)) goto done; if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check)) goto done; #ifndef OPENSSL_NO_DEPRECATED_3_0 ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL); EVP_PKEY_up_ref(pkey); ctx2->pkey = pkey; if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef)) goto done; if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef)) goto done; if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef)) goto done; #endif ret = 1; done: EVP_PKEY_CTX_free(ctx); #ifndef OPENSSL_NO_DEPRECATED_3_0 EVP_PKEY_CTX_free(ctx2); #endif EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_CMAC static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac) { EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); const char msg[] = "Hello World"; size_t maclen = AES_BLOCK_SIZE; int ret = 1; if (!TEST_ptr(mdctx) || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx, testpropq, pkey, NULL)) || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg))) || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen)) || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE)) ret = 0; EVP_MD_CTX_free(mdctx); return ret; } static int test_CMAC_keygen(void) { static unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; EVP_PKEY_CTX *kctx = NULL; int ret = 0; EVP_PKEY *pkey = NULL; unsigned char mac[AES_BLOCK_SIZE]; # if !defined(OPENSSL_NO_DEPRECATED_3_0) unsigned char mac2[AES_BLOCK_SIZE]; # endif if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL); if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN, EVP_PKEY_CTRL_CIPHER, 0, (void *)EVP_aes_256_cbc()), 0) || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN, EVP_PKEY_CTRL_SET_MAC_KEY, sizeof(key), (void *)key), 0) || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0) || !TEST_ptr(pkey) || !TEST_true(get_cmac_val(pkey, mac))) goto done; # if !defined(OPENSSL_NO_DEPRECATED_3_0) EVP_PKEY_free(pkey); pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_cbc()); if (!TEST_ptr(pkey) || !TEST_true(get_cmac_val(pkey, mac2)) || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2))) goto done; # endif ret = 1; done: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(kctx); return ret; } #endif static int test_HKDF(void) { EVP_PKEY_CTX *pctx; unsigned char out[20]; size_t outlen; int i, ret = 0; unsigned char salt[] = "0123456789"; unsigned char key[] = "012345678901234567890123456789"; unsigned char info[] = "infostring"; const unsigned char expected[] = { 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5, 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca }; size_t expectedlen = sizeof(expected); if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq))) goto done; for (i = 0; i < 2; i++) { outlen = sizeof(out); memset(out, 0, outlen); if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0) || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, sizeof(salt) - 1), 0) || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key, sizeof(key) - 1), 0) || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info, sizeof(info) - 1), 0) || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0) || !TEST_mem_eq(out, outlen, expected, expectedlen)) goto done; } ret = 1; done: EVP_PKEY_CTX_free(pctx); return ret; } static int test_emptyikm_HKDF(void) { EVP_PKEY_CTX *pctx; unsigned char out[20]; size_t outlen; int ret = 0; unsigned char salt[] = "9876543210"; unsigned char key[] = ""; unsigned char info[] = "stringinfo"; const unsigned char expected[] = { 0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47, 0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30 }; size_t expectedlen = sizeof(expected); if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq))) goto done; outlen = sizeof(out); memset(out, 0, outlen); if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0) || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, sizeof(salt) - 1), 0) || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key, sizeof(key) - 1), 0) || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info, sizeof(info) - 1), 0) || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0) || !TEST_mem_eq(out, outlen, expected, expectedlen)) goto done; ret = 1; done: EVP_PKEY_CTX_free(pctx); return ret; } #ifndef OPENSSL_NO_EC static int test_X509_PUBKEY_inplace(void) { int ret = 0; X509_PUBKEY *xp = X509_PUBKEY_new_ex(testctx, testpropq); const unsigned char *p = kExampleECPubKeyDER; size_t input_len = sizeof(kExampleECPubKeyDER); if (!TEST_ptr(xp)) goto done; if (!TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len))) goto done; if (!TEST_ptr(X509_PUBKEY_get0(xp))) goto done; p = kExampleBadECPubKeyDER; input_len = sizeof(kExampleBadECPubKeyDER); if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))) goto done; if (!TEST_true(X509_PUBKEY_get0(xp) == NULL)) goto done; ret = 1; done: X509_PUBKEY_free(xp); return ret; } static int test_X509_PUBKEY_dup(void) { int ret = 0; X509_PUBKEY *xp = NULL, *xq = NULL; const unsigned char *p = kExampleECPubKeyDER; size_t input_len = sizeof(kExampleECPubKeyDER); xp = X509_PUBKEY_new_ex(testctx, testpropq); if (!TEST_ptr(xp) || !TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len)) || !TEST_ptr(xq = X509_PUBKEY_dup(xp)) || !TEST_ptr_ne(xp, xq)) goto done; if (!TEST_ptr(X509_PUBKEY_get0(xq)) || !TEST_ptr(X509_PUBKEY_get0(xp)) || !TEST_ptr_ne(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp))) goto done; X509_PUBKEY_free(xq); xq = NULL; p = kExampleBadECPubKeyDER; input_len = sizeof(kExampleBadECPubKeyDER); if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)) || !TEST_ptr(xq = X509_PUBKEY_dup(xp))) goto done; X509_PUBKEY_free(xp); xp = NULL; if (!TEST_true(X509_PUBKEY_get0(xq) == NULL)) goto done; ret = 1; done: X509_PUBKEY_free(xp); X509_PUBKEY_free(xq); return ret; } #endif static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey) { EVP_MD_CTX *mdctx = NULL; EVP_PKEY_CTX *ctx = NULL; const OSSL_PARAM *params; OSSL_PARAM ourparams[2], *param = ourparams, *param_md; int ret = 0; const EVP_MD *md; char mdname[OSSL_MAX_NAME_SIZE]; char ssl3ms[48]; ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq); if (!TEST_ptr(ctx) || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)) goto err; params = EVP_PKEY_CTX_settable_params(ctx); if (!TEST_ptr(params) || !TEST_ptr(OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST))) goto err; params = EVP_PKEY_CTX_gettable_params(ctx); if (!TEST_ptr(params) || !TEST_ptr(OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID)) || !TEST_ptr(OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST))) goto err; strcpy(mdname, "SHA512"); param_md = param; *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, mdname, 0); *param++ = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams))) goto err; mdname[0] = '\0'; *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, mdname, sizeof(mdname)); if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams)) || !TEST_str_eq(mdname, "SHA512")) goto err; if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0) || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0) || !TEST_ptr_eq(md, EVP_sha256())) goto err; mdctx = EVP_MD_CTX_new(); if (!TEST_ptr(mdctx) || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq, pkey, NULL))) goto err; params = EVP_MD_CTX_settable_params(mdctx); if (!TEST_ptr(params) || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0) || !TEST_ptr_null(params[1].key)) goto err; param = ourparams; memset(ssl3ms, 0, sizeof(ssl3ms)); *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS, ssl3ms, sizeof(ssl3ms)); *param++ = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams))) goto err; ret = 1; err: EVP_MD_CTX_free(mdctx); EVP_PKEY_CTX_free(ctx); return ret; } #ifndef OPENSSL_NO_DSA static int test_DSA_get_set_params(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; int ret = 0; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL)) || !TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(q = BN_new()) || !TEST_ptr(g = BN_new()) || !TEST_ptr(pub = BN_new()) || !TEST_ptr(priv = BN_new())) goto err; if (!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_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params), 0)) goto err; if (!TEST_ptr(pkey)) goto err; ret = test_EVP_PKEY_CTX_get_set_params(pkey); err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(pctx); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); BN_free(p); BN_free(q); BN_free(g); BN_free(pub); BN_free(priv); return ret; } static int test_DSA_priv_pub(void) { return test_EVP_PKEY_ffc_priv_pub("DSA"); } #endif static int test_RSA_get_set_params(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; int ret = 0; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL)) || !TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_new()) || !TEST_ptr(e = BN_new()) || !TEST_ptr(d = BN_new())) goto err; if (!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))) goto err; if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params), 0)) goto err; if (!TEST_ptr(pkey)) goto err; ret = test_EVP_PKEY_CTX_get_set_params(pkey); err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(pctx); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); BN_free(n); BN_free(e); BN_free(d); return ret; } static int test_RSA_OAEP_set_get_params(void) { int ret = 0; EVP_PKEY *key = NULL; EVP_PKEY_CTX *key_ctx = NULL; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (!TEST_ptr(key = load_example_rsa_key()) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(0, key, 0))) goto err; { int padding = RSA_PKCS1_OAEP_PADDING; OSSL_PARAM params[4]; params[0] = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PAD_MODE, &padding); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_DIGEST_NAME_SHA2_256, 0); params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, OSSL_DIGEST_NAME_SHA1, 0); params[3] = OSSL_PARAM_construct_end(); if (!TEST_int_gt(EVP_PKEY_encrypt_init_ex(key_ctx, params),0)) goto err; } { OSSL_PARAM params[3]; char oaepmd[30] = { '\0' }; char mgf1md[30] = { '\0' }; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, oaepmd, sizeof(oaepmd)); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, mgf1md, sizeof(mgf1md)); params[2] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_PKEY_CTX_get_params(key_ctx, params))) goto err; if (!TEST_str_eq(oaepmd, OSSL_DIGEST_NAME_SHA2_256) || !TEST_str_eq(mgf1md, OSSL_DIGEST_NAME_SHA1)) goto err; } ret = 1; err: EVP_PKEY_free(key); EVP_PKEY_CTX_free(key_ctx); return ret; } static int test_RSA_OAEP_set_null_label(void) { int ret = 0; EVP_PKEY *key = NULL; EVP_PKEY_CTX *key_ctx = NULL; if (!TEST_ptr(key = load_example_rsa_key()) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(testctx, key, NULL)) || !TEST_true(EVP_PKEY_encrypt_init(key_ctx))) goto err; if (!TEST_true(EVP_PKEY_CTX_set_rsa_padding(key_ctx, RSA_PKCS1_OAEP_PADDING))) goto err; if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, OPENSSL_strdup("foo"), 0))) goto err; if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, NULL, 0))) goto err; ret = 1; err: EVP_PKEY_free(key); EVP_PKEY_CTX_free(key_ctx); return ret; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_RSA_legacy(void) { int ret = 0; BIGNUM *p = NULL; BIGNUM *q = NULL; BIGNUM *n = NULL; BIGNUM *e = NULL; BIGNUM *d = NULL; const EVP_MD *md = EVP_sha256(); EVP_MD_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; RSA *rsa = NULL; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); if (!TEST_ptr(p = BN_dup(BN_value_one())) || !TEST_ptr(q = BN_dup(BN_value_one())) || !TEST_ptr(n = BN_dup(BN_value_one())) || !TEST_ptr(e = BN_dup(BN_value_one())) || !TEST_ptr(d = BN_dup(BN_value_one()))) goto err; if (!TEST_ptr(rsa = RSA_new()) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_ptr(ctx = EVP_MD_CTX_new())) goto err; if (!TEST_true(RSA_set0_factors(rsa, p, q))) goto err; p = NULL; q = NULL; if (!TEST_true(RSA_set0_key(rsa, n, e, d))) goto err; n = NULL; e = NULL; d = NULL; if (!TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))) goto err; rsa = NULL; if (!TEST_true(EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))) goto err; ret = 1; err: RSA_free(rsa); EVP_MD_CTX_free(ctx); EVP_PKEY_free(pkey); BN_free(p); BN_free(q); BN_free(n); BN_free(e); BN_free(d); return ret; } #endif #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) static int test_decrypt_null_chunks(void) { EVP_CIPHER_CTX* ctx = NULL; EVP_CIPHER *cipher = NULL; const unsigned char key[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1 }; unsigned char iv[12] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b }; unsigned char msg[] = "It was the best of times, it was the worst of times"; unsigned char ciphertext[80]; unsigned char plaintext[80]; int ctlen, ptlen, tmp = 99; int ret = 0; const int enc_offset = 10, dec_offset = 20; if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq)) || !TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg, enc_offset)) || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL, 0)) || !TEST_int_eq(tmp, 0) || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, msg + enc_offset, sizeof(msg) - enc_offset)) || !TEST_int_eq(ctlen += tmp, sizeof(msg)) || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp)) || !TEST_int_eq(tmp, 0)) goto err; tmp = 99; if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext, dec_offset)) || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL, 0)) || !TEST_int_eq(tmp, 0) || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, ciphertext + dec_offset, ctlen - dec_offset)) || !TEST_int_eq(ptlen += tmp, sizeof(msg)) || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp)) || !TEST_int_eq(tmp, 0) || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen)) goto err; ret = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(cipher); return ret; } #endif #ifndef OPENSSL_NO_DH static int test_DH_priv_pub(void) { return test_EVP_PKEY_ffc_priv_pub("DH"); } # ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_EVP_PKEY_set1_DH(void) { DH *x942dh = NULL, *noqdh = NULL; EVP_PKEY *pkey1 = NULL, *pkey2 = NULL; int ret = 0; BIGNUM *p, *g = NULL; BIGNUM *pubkey = NULL; unsigned char pub[2048 / 8]; size_t len = 0; if (!TEST_ptr(p = BN_new()) || !TEST_ptr(g = BN_new()) || !TEST_ptr(pubkey = BN_new()) || !TEST_true(BN_set_word(p, 9999)) || !TEST_true(BN_set_word(g, 2)) || !TEST_true(BN_set_word(pubkey, 4321)) || !TEST_ptr(noqdh = DH_new()) || !TEST_true(DH_set0_pqg(noqdh, p, NULL, g)) || !TEST_true(DH_set0_key(noqdh, pubkey, NULL)) || !TEST_ptr(pubkey = BN_new()) || !TEST_true(BN_set_word(pubkey, 4321))) goto err; p = g = NULL; x942dh = DH_get_2048_256(); pkey1 = EVP_PKEY_new(); pkey2 = EVP_PKEY_new(); if (!TEST_ptr(x942dh) || !TEST_ptr(noqdh) || !TEST_ptr(pkey1) || !TEST_ptr(pkey2) || !TEST_true(DH_set0_key(x942dh, pubkey, NULL))) goto err; pubkey = NULL; if (!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh)) || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX)) goto err; if (!TEST_true(EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PUB_KEY, &pubkey)) || !TEST_ptr(pubkey)) goto err; if (!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh)) || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH)) goto err; if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey2, OSSL_PKEY_PARAM_PUB_KEY, pub, sizeof(pub), &len)) || !TEST_size_t_ne(len, 0)) goto err; ret = 1; err: BN_free(p); BN_free(g); BN_free(pubkey); EVP_PKEY_free(pkey1); EVP_PKEY_free(pkey2); DH_free(x942dh); DH_free(noqdh); return ret; } # endif #endif static int test_keygen_with_empty_template(int n) { EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY *tkey = NULL; int ret = 0; if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); switch (n) { case 0: if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))) goto err; break; case 1: if (!TEST_ptr(tkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL))) goto err; break; } if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0)) goto err; ret = 1; err: EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); EVP_PKEY_free(tkey); return ret; } static int test_pkey_ctx_fail_without_provider(int tst) { OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new(); OSSL_PROVIDER *tmpnullprov = NULL; EVP_PKEY_CTX *pctx = NULL; const char *keytype = NULL; int expect_null = 0; int ret = 0; if (!TEST_ptr(tmpctx)) goto err; tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null"); if (!TEST_ptr(tmpnullprov)) goto err; switch (tst) { case 0: keytype = "RSA"; expect_null = 1; break; case 1: keytype = "SM2"; expect_null = 1; #ifdef OPENSSL_NO_EC TEST_info("EC disable, skipping SM2 check..."); goto end; #endif #ifdef OPENSSL_NO_SM2 TEST_info("SM2 disable, skipping SM2 check..."); goto end; #endif break; default: TEST_error("No test for case %d", tst); goto err; } pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, ""); if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx)) goto err; #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2) end: #endif ret = 1; err: EVP_PKEY_CTX_free(pctx); OSSL_PROVIDER_unload(tmpnullprov); OSSL_LIB_CTX_free(tmpctx); return ret; } static int test_rand_agglomeration(void) { EVP_RAND *rand; EVP_RAND_CTX *ctx; OSSL_PARAM params[3], *p = params; int res; unsigned int step = 7; static unsigned char seed[] = "It does not matter how slowly you go " "as long as you do not stop."; unsigned char out[sizeof(seed)]; if (!TEST_int_ne(sizeof(seed) % step, 0) || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq))) return 0; ctx = EVP_RAND_CTX_new(rand, NULL); EVP_RAND_free(rand); if (!TEST_ptr(ctx)) return 0; memset(out, 0, sizeof(out)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, seed, sizeof(seed)); *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step); *p = OSSL_PARAM_construct_end(); res = TEST_true(EVP_RAND_CTX_set_params(ctx, params)) && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0)) && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out)); EVP_RAND_CTX_free(ctx); return res; } static int test_evp_iv_aes(int idx) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1, 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57}; unsigned char init_iv[EVP_MAX_IV_LENGTH] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34}; static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; unsigned char ciphertext[32], oiv[16], iv[16]; unsigned char *ref_iv; unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9, 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e}; unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64, 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd}; unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c, 0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed}; unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81}; unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98}; #ifndef OPENSSL_NO_OCB unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81}; #endif int len = sizeof(ciphertext); size_t ivlen, ref_len; const EVP_CIPHER *type = NULL; int iv_reset = 0; if (nullprov != NULL && idx < 6) return TEST_skip("Test does not support a non-default library context"); switch (idx) { case 0: type = EVP_aes_128_cbc(); case 6: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq); ref_iv = cbc_state; ref_len = sizeof(cbc_state); iv_reset = 1; break; case 1: type = EVP_aes_128_ofb(); case 7: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq); ref_iv = ofb_state; ref_len = sizeof(ofb_state); iv_reset = 1; break; case 2: type = EVP_aes_128_cfb(); case 8: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq); ref_iv = cfb_state; ref_len = sizeof(cfb_state); iv_reset = 1; break; case 3: type = EVP_aes_128_gcm(); case 9: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq); ref_iv = gcm_state; ref_len = sizeof(gcm_state); break; case 4: type = EVP_aes_128_ccm(); case 10: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq); ref_iv = ccm_state; ref_len = sizeof(ccm_state); break; #ifdef OPENSSL_NO_OCB case 5: case 11: return 1; #else case 5: type = EVP_aes_128_ocb(); case 11: type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq); ref_iv = ocb_state; ref_len = sizeof(ocb_state); break; #endif default: return 0; } if (!TEST_ptr(type) || !TEST_ptr((ctx = EVP_CIPHER_CTX_new())) || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv)) || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg, (int)sizeof(msg))) || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv))) || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))) || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len))) goto err; ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); if (!TEST_int_gt(ivlen, 0)) goto err; if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen) || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen)) goto err; if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL)) || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))) goto err; if (iv_reset) { if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen)) goto err; } else { if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen)) goto err; } ret = 1; err: EVP_CIPHER_CTX_free(ctx); if (idx >= 6) EVP_CIPHER_free((EVP_CIPHER *)type); return ret; } #ifndef OPENSSL_NO_DES static int test_evp_iv_des(int idx) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; static const unsigned char key[24] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; static const unsigned char init_iv[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; unsigned char ciphertext[32], oiv[8], iv[8]; unsigned const char *ref_iv; static const unsigned char cbc_state_des[8] = { 0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a }; static const unsigned char cbc_state_3des[8] = { 0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9 }; static const unsigned char ofb_state_des[8] = { 0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c }; static const unsigned char ofb_state_3des[8] = { 0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e }; static const unsigned char cfb_state_des[8] = { 0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73 }; static const unsigned char cfb_state_3des[8] = { 0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7 }; int len = sizeof(ciphertext); size_t ivlen, ref_len; EVP_CIPHER *type = NULL; if (lgcyprov == NULL && idx < 3) return TEST_skip("Test requires legacy provider to be loaded"); switch (idx) { case 0: type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq); ref_iv = cbc_state_des; ref_len = sizeof(cbc_state_des); break; case 1: type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq); ref_iv = ofb_state_des; ref_len = sizeof(ofb_state_des); break; case 2: type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq); ref_iv = cfb_state_des; ref_len = sizeof(cfb_state_des); break; case 3: type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq); ref_iv = cbc_state_3des; ref_len = sizeof(cbc_state_3des); break; case 4: type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq); ref_iv = ofb_state_3des; ref_len = sizeof(ofb_state_3des); break; case 5: type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq); ref_iv = cfb_state_3des; ref_len = sizeof(cfb_state_3des); break; default: return 0; } if (!TEST_ptr(type) || !TEST_ptr((ctx = EVP_CIPHER_CTX_new())) || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv)) || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg, (int)sizeof(msg))) || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv))) || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))) || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len))) goto err; ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); if (!TEST_int_gt(ivlen, 0)) goto err; if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen) || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen)) goto err; if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL)) || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))) goto err; if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen)) goto err; ret = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return ret; } #endif #ifndef OPENSSL_NO_BF static int test_evp_bf_default_keylen(int idx) { int ret = 0; static const char *algos[4] = { "bf-ecb", "bf-cbc", "bf-cfb", "bf-ofb" }; int ivlen[4] = { 0, 8, 8, 8 }; EVP_CIPHER *cipher = NULL; if (lgcyprov == NULL) return TEST_skip("Test requires legacy provider to be loaded"); if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, algos[idx], testpropq)) || !TEST_int_eq(EVP_CIPHER_get_key_length(cipher), 16) || !TEST_int_eq(EVP_CIPHER_get_iv_length(cipher), ivlen[idx])) goto err; ret = 1; err: EVP_CIPHER_free(cipher); return ret; } #endif #ifndef OPENSSL_NO_EC static int ecpub_nids[] = { NID_brainpoolP256r1, NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, # ifndef OPENSSL_NO_EC2M NID_sect233k1, NID_sect233r1, NID_sect283r1, NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1, # endif NID_brainpoolP384r1, NID_brainpoolP512r1 }; static int test_ecpub(int idx) { int ret = 0, len, savelen; int nid; unsigned char buf[1024]; unsigned char *p; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *ctx = NULL; # ifndef OPENSSL_NO_DEPRECATED_3_0 const unsigned char *q; EVP_PKEY *pkey2 = NULL; EC_KEY *ec = NULL; # endif if (nullprov != NULL) return TEST_skip("Test does not support a non-default library context"); nid = ecpub_nids[idx]; ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); if (!TEST_ptr(ctx) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0) || !TEST_true(EVP_PKEY_keygen(ctx, &pkey))) goto done; len = i2d_PublicKey(pkey, NULL); savelen = len; if (!TEST_int_ge(len, 1) || !TEST_int_lt(len, 1024)) goto done; p = buf; len = i2d_PublicKey(pkey, &p); if (!TEST_int_ge(len, 1) || !TEST_int_eq(len, savelen)) goto done; # ifndef OPENSSL_NO_DEPRECATED_3_0 q = buf; if (!TEST_ptr((pkey2 = EVP_PKEY_new())) || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid))) || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec))) goto done; ec = NULL; if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen))) goto done; if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1)) goto done; # endif ret = 1; done: EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); # ifndef OPENSSL_NO_DEPRECATED_3_0 EVP_PKEY_free(pkey2); EC_KEY_free(ec); # endif return ret; } #endif static int test_EVP_rsa_pss_with_keygen_bits(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; EVP_MD *md; md = EVP_MD_fetch(testctx, "sha256", testpropq); ret = TEST_ptr(md) && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq))) && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0) && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0) && TEST_true(EVP_PKEY_keygen(ctx, &pkey)); EVP_MD_free(md); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); return ret; } static int test_EVP_rsa_pss_set_saltlen(void) { int ret = 0; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pkey_ctx = NULL; EVP_MD *sha256 = NULL; EVP_MD_CTX *sha256_ctx = NULL; int saltlen = 9999; const int test_value = 32; ret = TEST_ptr(pkey = load_example_rsa_key()) && TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", NULL)) && TEST_ptr(sha256_ctx = EVP_MD_CTX_new()) && TEST_true(EVP_DigestSignInit(sha256_ctx, &pkey_ctx, sha256, NULL, pkey)) && TEST_true(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING)) && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, test_value), 0) && TEST_int_gt(EVP_PKEY_CTX_get_rsa_pss_saltlen(pkey_ctx, &saltlen), 0) && TEST_int_eq(saltlen, test_value); EVP_MD_CTX_free(sha256_ctx); EVP_PKEY_free(pkey); EVP_MD_free(sha256); return ret; } static int success = 1; static void md_names(const char *name, void *vctx) { OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx; EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL); if (!TEST_ptr(aes128)) success = 0; EVP_CIPHER_free(aes128); } static int test_names_do_all(void) { OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); EVP_MD *sha256 = NULL; int testresult = 0; if (!TEST_ptr(ctx)) goto err; sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL); if (!TEST_ptr(sha256)) goto err; if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx))) goto err; if (!TEST_true(success)) goto err; testresult = 1; err: EVP_MD_free(sha256); OSSL_LIB_CTX_free(ctx); return testresult; } typedef struct { const char *cipher; const unsigned char *key; const unsigned char *iv; const unsigned char *input; const unsigned char *expected; const unsigned char *tag; size_t ivlen; size_t inlen; size_t expectedlen; size_t taglen; int keyfirst; int initenc; int finalenc; } EVP_INIT_TEST_st; static const EVP_INIT_TEST_st evp_init_tests[] = { { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext, cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext), 0, 1, 0, 1 }, { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext, gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV), sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultTag), 1, 0, 1 }, { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext, cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext), 0, 0, 0, 1 }, { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext, gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV), sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultTag), 0, 0, 1 }, { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext, cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0, 1, 1, 0 }, { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext, gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV), sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultTag), 1, 1, 0 }, { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext, cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0, 0, 1, 0 }, { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext, gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV), sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultTag), 0, 1, 0 } }; static const EVP_INIT_TEST_st evp_reinit_tests[] = { { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial, cfbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial), sizeof(cfbCiphertext_partial), 0, 0, 1, 0 }, { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext_partial, cfbPlaintext_partial, NULL, 0, sizeof(cfbCiphertext_partial), sizeof(cfbPlaintext_partial), 0, 0, 0, 0 }, { "aes-128-ofb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial, ofbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial), sizeof(ofbCiphertext_partial), 0, 0, 1, 0 }, { "aes-128-ofb", kCFBDefaultKey, iCFBIV, ofbCiphertext_partial, cfbPlaintext_partial, NULL, 0, sizeof(ofbCiphertext_partial), sizeof(cfbPlaintext_partial), 0, 0, 0, 0 }, }; static int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t) { int res = 0; if (t->ivlen != 0) { if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL), 0)) goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1))) goto err; res = 1; err: return res; } static int test_evp_init_seq(int idx) { int outlen1, outlen2; int testresult = 0; unsigned char outbuf[1024]; unsigned char tag[16]; const EVP_INIT_TEST_st *t = &evp_init_tests[idx]; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *type = NULL; size_t taglen = sizeof(tag); char *errmsg = NULL; ctx = EVP_CIPHER_CTX_new(); if (ctx == NULL) { errmsg = "CTX_ALLOC"; goto err; } if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))) { errmsg = "CIPHER_FETCH"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) { errmsg = "EMPTY_ENC_INIT"; goto err; } if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) { errmsg = "PADDING"; goto err; } if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) { errmsg = "KEY_INIT (before iv)"; goto err; } if (!evp_init_seq_set_iv(ctx, t)) { errmsg = "IV_INIT"; goto err; } if (t->keyfirst == 0 && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) { errmsg = "KEY_INIT (after iv)"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) { errmsg = "FINAL_ENC_INIT"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) { errmsg = "CIPHER_UPDATE"; goto err; } if (t->finalenc == 0 && t->tag != NULL) { if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->taglen, (void *)t->tag), 0)) { errmsg = "SET_TAG"; goto err; } } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL"; goto err; } if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) { errmsg = "WRONG_RESULT"; goto err; } if (t->finalenc != 0 && t->tag != NULL) { if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) { errmsg = "GET_TAG"; goto err; } if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) { errmsg = "TAG_ERROR"; goto err; } } testresult = 1; err: if (errmsg != NULL) TEST_info("evp_init_test %d: %s", idx, errmsg); EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return testresult; } static int test_evp_reinit_seq(int idx) { int outlen1, outlen2, outlen_final; int testresult = 0; unsigned char outbuf1[1024]; unsigned char outbuf2[1024]; const EVP_INIT_TEST_st *t = &evp_reinit_tests[idx]; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *type = NULL; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) || !TEST_true(EVP_CipherInit_ex2(ctx, type, t->key, t->iv, t->initenc, NULL)) || !TEST_true(EVP_CipherUpdate(ctx, outbuf1, &outlen1, t->input, t->inlen)) || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf1, &outlen_final)) || !TEST_mem_eq(t->expected, t->expectedlen, outbuf1, outlen1 + outlen_final) || !TEST_true(EVP_CipherInit_ex2(ctx, NULL, NULL, NULL, -1, NULL)) || !TEST_true(EVP_CipherUpdate(ctx, outbuf2, &outlen2, t->input, t->inlen)) || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf2, &outlen_final)) || !TEST_mem_eq(t->expected, t->expectedlen, outbuf2, outlen2 + outlen_final)) goto err; testresult = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return testresult; } typedef struct { const unsigned char *input; const unsigned char *expected; size_t inlen; size_t expectedlen; int enc; } EVP_RESET_TEST_st; static const EVP_RESET_TEST_st evp_reset_tests[] = { { cfbPlaintext, cfbCiphertext, sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1 }, { cfbCiphertext, cfbPlaintext, sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0 } }; static int test_evp_reset(int idx) { const EVP_RESET_TEST_st *t = &evp_reset_tests[idx]; int outlen1, outlen2; int testresult = 0; unsigned char outbuf[1024]; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *type = NULL; char *errmsg = NULL; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) { errmsg = "CTX_ALLOC"; goto err; } if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq))) { errmsg = "CIPHER_FETCH"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) { errmsg = "CIPHER_INIT"; goto err; } if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) { errmsg = "PADDING"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) { errmsg = "CIPHER_UPDATE"; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL"; goto err; } if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) { errmsg = "WRONG_RESULT"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) { errmsg = "CIPHER_REINIT"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) { errmsg = "CIPHER_UPDATE (reinit)"; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL (reinit)"; goto err; } if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) { errmsg = "WRONG_RESULT (reinit)"; goto err; } testresult = 1; err: if (errmsg != NULL) TEST_info("test_evp_reset %d: %s", idx, errmsg); EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return testresult; } typedef struct { const char *cipher; int enc; } EVP_UPDATED_IV_TEST_st; static const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = { { "aes-128-cfb", 1 }, { "aes-128-cfb", 0 }, { "aes-128-cfb1", 1 }, { "aes-128-cfb1", 0 }, { "aes-128-cfb8", 1 }, { "aes-128-cfb8", 0 }, { "aes-128-ofb", 1 }, { "aes-128-ofb", 0 }, { "aes-128-ctr", 1 }, { "aes-128-ctr", 0 }, { "aes-128-cbc", 1 }, { "aes-128-cbc", 0 } }; static int test_evp_updated_iv(int idx) { const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx]; int outlen1, outlen2; int testresult = 0; unsigned char outbuf[1024]; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *type = NULL; unsigned char updated_iv[EVP_MAX_IV_LENGTH]; int iv_len; char *errmsg = NULL; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) { errmsg = "CTX_ALLOC"; goto err; } if ((type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) == NULL) { TEST_info("cipher %s not supported, skipping", t->cipher); goto ok; } if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) { errmsg = "CIPHER_INIT"; goto err; } if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) { errmsg = "PADDING"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) { errmsg = "CIPHER_UPDATE"; goto err; } if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, updated_iv, sizeof(updated_iv)))) { errmsg = "CIPHER_CTX_GET_UPDATED_IV"; goto err; } iv_len = EVP_CIPHER_CTX_get_iv_length(ctx); if (!TEST_int_ge(iv_len,0)) { errmsg = "CIPHER_CTX_GET_IV_LEN"; goto err; } if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) { errmsg = "IV_NOT_UPDATED"; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL"; goto err; } ok: testresult = 1; err: if (errmsg != NULL) TEST_info("test_evp_updated_iv %d: %s", idx, errmsg); EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return testresult; } typedef struct { const unsigned char *iv1; const unsigned char *iv2; const unsigned char *expected1; const unsigned char *expected2; const unsigned char *tag1; const unsigned char *tag2; size_t ivlen1; size_t ivlen2; size_t expectedlen1; size_t expectedlen2; } TEST_GCM_IV_REINIT_st; static const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = { { iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2, gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2), sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2) }, { iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1, gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1), sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1) } }; static int test_gcm_reinit(int idx) { int outlen1, outlen2, outlen3; int testresult = 0; unsigned char outbuf[1024]; unsigned char tag[16]; const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx]; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *type = NULL; size_t taglen = sizeof(tag); char *errmsg = NULL; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) { errmsg = "CTX_ALLOC"; goto err; } if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-256-gcm", testpropq))) { errmsg = "CIPHER_FETCH"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) { errmsg = "ENC_INIT"; goto err; } if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL), 0)) { errmsg = "SET_IVLEN1"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) { errmsg = "SET_IV1"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) { errmsg = "AAD1"; goto err; } EVP_CIPHER_CTX_set_padding(ctx, 0); if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext, sizeof(gcmResetPlaintext)))) { errmsg = "CIPHER_UPDATE1"; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL1"; goto err; } if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) { errmsg = "WRONG_RESULT1"; goto err; } if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) { errmsg = "GET_TAG1"; goto err; } if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) { errmsg = "TAG_ERROR1"; goto err; } if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL), 0)) { errmsg = "SET_IVLEN2"; goto err; } if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) { errmsg = "SET_IV2"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) { errmsg = "AAD2"; goto err; } if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext, sizeof(gcmResetPlaintext)))) { errmsg = "CIPHER_UPDATE2"; goto err; } if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) { errmsg = "CIPHER_FINAL2"; goto err; } if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) { errmsg = "WRONG_RESULT2"; goto err; } if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) { errmsg = "GET_TAG2"; goto err; } if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) { errmsg = "TAG_ERROR2"; goto err; } testresult = 1; err: if (errmsg != NULL) TEST_info("evp_init_test %d: %s", idx, errmsg); EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(type); return testresult; } static const char *ivlen_change_ciphers[] = { "AES-256-GCM", #ifndef OPENSSL_NO_OCB "AES-256-OCB", #endif "AES-256-CCM" }; static int test_ivlen_change(int idx) { int outlen; int res = 0; unsigned char outbuf[1024]; static const unsigned char iv[] = { 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34 }; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *ciph = NULL; OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; size_t ivlen = 13; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, ivlen_change_ciphers[idx], testpropq))) goto err; if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, kGCMDefaultKey, iv, 1))) goto err; if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext, sizeof(gcmDefaultPlaintext)))) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &ivlen); if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))) goto err; ERR_set_mark(); if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext, sizeof(gcmDefaultPlaintext)))) { ERR_clear_last_mark(); goto err; } ERR_pop_to_mark(); res = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(ciph); return res; } static const char *keylen_change_ciphers[] = { #ifndef OPENSSL_NO_BF "BF-ECB", #endif #ifndef OPENSSL_NO_CAST "CAST5-ECB", #endif #ifndef OPENSSL_NO_RC2 "RC2-ECB", #endif #ifndef OPENSSL_NO_RC4 "RC4", #endif #ifndef OPENSSL_NO_RC5 "RC5-ECB", #endif NULL }; static int test_keylen_change(int idx) { int outlen; int res = 0; unsigned char outbuf[1024]; static const unsigned char key[] = { 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34 }; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *ciph = NULL; OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; size_t keylen = 12; if (lgcyprov == NULL) return TEST_skip("Test requires legacy provider to be loaded"); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, keylen_change_ciphers[idx], testpropq))) goto err; if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, key, NULL, 1))) goto err; if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext, sizeof(gcmDefaultPlaintext)))) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen); if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))) goto err; ERR_set_mark(); if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext, sizeof(gcmDefaultPlaintext)))) { ERR_clear_last_mark(); goto err; } ERR_pop_to_mark(); res = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(ciph); return res; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static EVP_PKEY_METHOD *custom_pmeth = NULL; static const EVP_PKEY_METHOD *orig_pmeth = NULL; # define EVP_PKEY_CTRL_MY_COMMAND 9999 static int custom_pmeth_init(EVP_PKEY_CTX *ctx) { int (*pinit)(EVP_PKEY_CTX *ctx); EVP_PKEY_meth_get_init(orig_pmeth, &pinit); return pinit(ctx); } static void custom_pmeth_cleanup(EVP_PKEY_CTX *ctx) { void (*pcleanup)(EVP_PKEY_CTX *ctx); EVP_PKEY_meth_get_cleanup(orig_pmeth, &pcleanup); pcleanup(ctx); } static int custom_pmeth_sign(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) { int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); EVP_PKEY_meth_get_sign(orig_pmeth, NULL, &psign); return psign(ctx, out, outlen, in, inlen); } static int custom_pmeth_digestsign(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen) { int (*pdigestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); EVP_PKEY_meth_get_digestsign(orig_pmeth, &pdigestsign); return pdigestsign(ctx, sig, siglen, tbs, tbslen); } static int custom_pmeth_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen) { int (*pderive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); EVP_PKEY_meth_get_derive(orig_pmeth, NULL, &pderive); return pderive(ctx, key, keylen); } static int custom_pmeth_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src) { int (*pcopy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); EVP_PKEY_meth_get_copy(orig_pmeth, &pcopy); return pcopy(dst, src); } static int ctrl_called; static int custom_pmeth_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2); EVP_PKEY_meth_get_ctrl(orig_pmeth, &pctrl, NULL); if (type == EVP_PKEY_CTRL_MY_COMMAND) { ctrl_called = 1; return 1; } return pctrl(ctx, type, p1, p2); } static int test_custom_pmeth(int idx) { EVP_PKEY_CTX *pctx = NULL; EVP_MD_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; int id, orig_id, orig_flags; int testresult = 0; size_t reslen; unsigned char *res = NULL; unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o' }; const EVP_MD *md = EVP_sha256(); int doderive = 0; ctrl_called = 0; if (testctx != NULL) return 1; switch (idx) { case 0: case 6: id = EVP_PKEY_RSA; pkey = load_example_rsa_key(); break; case 1: case 7: # ifndef OPENSSL_NO_DSA id = EVP_PKEY_DSA; pkey = load_example_dsa_key(); break; # else return 1; # endif case 2: case 8: # ifndef OPENSSL_NO_EC id = EVP_PKEY_EC; pkey = load_example_ec_key(); break; # else return 1; # endif case 3: case 9: # ifndef OPENSSL_NO_ECX id = EVP_PKEY_ED25519; md = NULL; pkey = load_example_ed25519_key(); break; # else return 1; # endif case 4: case 10: # ifndef OPENSSL_NO_DH id = EVP_PKEY_DH; doderive = 1; pkey = load_example_dh_key(); break; # else return 1; # endif case 5: case 11: # ifndef OPENSSL_NO_ECX id = EVP_PKEY_X25519; doderive = 1; pkey = load_example_x25519_key(); break; # else return 1; # endif default: TEST_error("Should not happen"); goto err; } if (!TEST_ptr(pkey)) goto err; if (idx < 6) { if (!TEST_true(evp_pkey_is_provided(pkey))) goto err; } else { EVP_PKEY *tmp = pkey; pkey = EVP_PKEY_new(); if (!TEST_ptr(pkey)) { pkey = tmp; goto err; } if (!TEST_true(evp_pkey_copy_downgraded(&pkey, tmp))) { EVP_PKEY_free(tmp); goto err; } EVP_PKEY_free(tmp); if (!TEST_true(evp_pkey_is_legacy(pkey))) goto err; } if (!TEST_ptr(orig_pmeth = EVP_PKEY_meth_find(id)) || !TEST_ptr(pkey)) goto err; EVP_PKEY_meth_get0_info(&orig_id, &orig_flags, orig_pmeth); if (!TEST_int_eq(orig_id, id) || !TEST_ptr(custom_pmeth = EVP_PKEY_meth_new(id, orig_flags))) goto err; if (id == EVP_PKEY_ED25519) { EVP_PKEY_meth_set_digestsign(custom_pmeth, custom_pmeth_digestsign); } if (id == EVP_PKEY_DH || id == EVP_PKEY_X25519) { EVP_PKEY_meth_set_derive(custom_pmeth, NULL, custom_pmeth_derive); } else { EVP_PKEY_meth_set_sign(custom_pmeth, NULL, custom_pmeth_sign); } if (id != EVP_PKEY_ED25519 && id != EVP_PKEY_X25519) { EVP_PKEY_meth_set_init(custom_pmeth, custom_pmeth_init); EVP_PKEY_meth_set_cleanup(custom_pmeth, custom_pmeth_cleanup); EVP_PKEY_meth_set_copy(custom_pmeth, custom_pmeth_copy); } EVP_PKEY_meth_set_ctrl(custom_pmeth, custom_pmeth_ctrl, NULL); if (!TEST_true(EVP_PKEY_meth_add0(custom_pmeth))) goto err; if (doderive) { pctx = EVP_PKEY_CTX_new(pkey, NULL); if (!TEST_ptr(pctx) || !TEST_int_eq(EVP_PKEY_derive_init(pctx), 1) || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_MY_COMMAND, 0, NULL), 1) || !TEST_int_eq(ctrl_called, 1) || !TEST_int_ge(EVP_PKEY_derive_set_peer(pctx, pkey), 1) || !TEST_int_ge(EVP_PKEY_derive(pctx, NULL, &reslen), 1) || !TEST_ptr(res = OPENSSL_malloc(reslen)) || !TEST_int_ge(EVP_PKEY_derive(pctx, res, &reslen), 1)) goto err; } else { ctx = EVP_MD_CTX_new(); reslen = EVP_PKEY_size(pkey); res = OPENSSL_malloc(reslen); if (!TEST_ptr(ctx) || !TEST_ptr(res) || !TEST_true(EVP_DigestSignInit(ctx, &pctx, md, NULL, pkey)) || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_MY_COMMAND, 0, NULL), 1) || !TEST_int_eq(ctrl_called, 1)) goto err; if (id == EVP_PKEY_ED25519) { if (!TEST_true(EVP_DigestSign(ctx, res, &reslen, msg, sizeof(msg)))) goto err; } else { if (!TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg))) || !TEST_true(EVP_DigestSignFinal(ctx, res, &reslen))) goto err; } } testresult = 1; err: OPENSSL_free(res); EVP_MD_CTX_free(ctx); if (doderive) EVP_PKEY_CTX_free(pctx); EVP_PKEY_free(pkey); EVP_PKEY_meth_remove(custom_pmeth); EVP_PKEY_meth_free(custom_pmeth); custom_pmeth = NULL; return testresult; } static int test_evp_md_cipher_meth(void) { EVP_MD *md = EVP_MD_meth_dup(EVP_sha256()); EVP_CIPHER *ciph = EVP_CIPHER_meth_dup(EVP_aes_128_cbc()); int testresult = 0; if (!TEST_ptr(md) || !TEST_ptr(ciph)) goto err; testresult = 1; err: EVP_MD_meth_free(md); EVP_CIPHER_meth_free(ciph); return testresult; } typedef struct { int data; } custom_dgst_ctx; static int custom_md_init_called = 0; static int custom_md_cleanup_called = 0; static int custom_md_init(EVP_MD_CTX *ctx) { custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx); if (p == NULL) return 0; custom_md_init_called++; return 1; } static int custom_md_cleanup(EVP_MD_CTX *ctx) { custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx); if (p == NULL) return 1; custom_md_cleanup_called++; return 1; } static int test_custom_md_meth(void) { EVP_MD_CTX *mdctx = NULL; EVP_MD *tmp = NULL; char mess[] = "Test Message\n"; unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned int md_len; int testresult = 0; int nid; if (testctx != NULL) return TEST_skip("Non-default libctx"); custom_md_init_called = custom_md_cleanup_called = 0; nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md"); if (!TEST_int_ne(nid, NID_undef)) goto err; tmp = EVP_MD_meth_new(nid, NID_undef); if (!TEST_ptr(tmp)) goto err; if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init)) || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup)) || !TEST_true(EVP_MD_meth_set_app_datasize(tmp, sizeof(custom_dgst_ctx)))) goto err; mdctx = EVP_MD_CTX_new(); if (!TEST_ptr(mdctx) || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL)) || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess))) || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len)) || !TEST_int_eq(custom_md_init_called, 1) || !TEST_int_eq(custom_md_cleanup_called, 1)) goto err; testresult = 1; err: EVP_MD_CTX_free(mdctx); EVP_MD_meth_free(tmp); return testresult; } typedef struct { int data; } custom_ciph_ctx; static int custom_ciph_init_called = 0; static int custom_ciph_cleanup_called = 0; static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx); if (p == NULL) return 0; custom_ciph_init_called++; return 1; } static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx) { custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx); if (p == NULL) return 1; custom_ciph_cleanup_called++; return 1; } static int test_custom_ciph_meth(void) { EVP_CIPHER_CTX *ciphctx = NULL; EVP_CIPHER *tmp = NULL; int testresult = 0; int nid; if (testctx != NULL) return TEST_skip("Non-default libctx"); custom_ciph_init_called = custom_ciph_cleanup_called = 0; nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph"); if (!TEST_int_ne(nid, NID_undef)) goto err; tmp = EVP_CIPHER_meth_new(nid, 16, 16); if (!TEST_ptr(tmp)) goto err; if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init)) || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT)) || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup)) || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp, sizeof(custom_ciph_ctx)))) goto err; ciphctx = EVP_CIPHER_CTX_new(); if (!TEST_ptr(ciphctx) || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1)) || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL, NULL, NULL, 1)) || !TEST_int_eq(custom_ciph_init_called, 1) || !TEST_int_eq(custom_ciph_cleanup_called, 1)) goto err; testresult = 1; err: EVP_CIPHER_CTX_free(ciphctx); EVP_CIPHER_meth_free(tmp); return testresult; } # ifndef OPENSSL_NO_DYNAMIC_ENGINE static int test_signatures_with_engine(int tst) { ENGINE *e; const char *engine_id = "dasync"; EVP_PKEY *pkey = NULL; const unsigned char badcmackey[] = { 0x00, 0x01 }; const unsigned char cmackey[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const unsigned char ed25519key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 }; int testresult = 0; EVP_MD_CTX *ctx = NULL; unsigned char *mac = NULL; size_t maclen = 0; int ret; # ifdef OPENSSL_NO_CMAC if (tst <= 1) return 1; # endif # ifdef OPENSSL_NO_ECX if (tst == 2) return 1; # endif if (!TEST_ptr(e = ENGINE_by_id(engine_id))) return 0; if (!TEST_true(ENGINE_init(e))) { ENGINE_free(e); return 0; } switch (tst) { case 0: pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey), EVP_aes_128_cbc()); break; case 1: pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey), EVP_aes_128_cbc()); break; case 2: pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key, sizeof(ed25519key)); break; default: TEST_error("Invalid test case"); goto err; } if (!TEST_ptr(pkey)) goto err; if (!TEST_ptr(ctx = EVP_MD_CTX_new())) goto err; ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL, pkey); if (tst == 0) { if (!TEST_true(ret)) goto err; if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg))) || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen))) goto err; if (!TEST_ptr(mac = OPENSSL_malloc(maclen))) goto err; if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen))) goto err; } else { if (!TEST_false(ret)) goto err; } testresult = 1; err: EVP_MD_CTX_free(ctx); OPENSSL_free(mac); EVP_PKEY_free(pkey); ENGINE_finish(e); ENGINE_free(e); return testresult; } static int test_cipher_with_engine(void) { ENGINE *e; const char *engine_id = "dasync"; const unsigned char keyiv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 }; int testresult = 0; EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL; unsigned char buf[AES_BLOCK_SIZE]; int len = 0; if (!TEST_ptr(e = ENGINE_by_id(engine_id))) return 0; if (!TEST_true(ENGINE_init(e))) { ENGINE_free(e); return 0; } if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new())) goto err; if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv))) goto err; if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx))) goto err; if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg))) || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len))) goto err; testresult = 1; err: EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx2); ENGINE_finish(e); ENGINE_free(e); return testresult; } # endif #endif #ifndef OPENSSL_NO_ECX static int ecxnids[] = { NID_X25519, NID_X448, NID_ED25519, NID_ED448 }; static int test_ecx_short_keys(int tst) { unsigned char ecxkeydata = 1; EVP_PKEY *pkey; pkey = EVP_PKEY_new_raw_private_key_ex(testctx, OBJ_nid2sn(ecxnids[tst]), NULL, &ecxkeydata, 1); if (!TEST_ptr_null(pkey)) { EVP_PKEY_free(pkey); return 0; } return 1; } #endif typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONTEXT, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" }, { NULL } }; return options; } #ifndef OPENSSL_NO_ECX static int test_ecx_not_private_key(int tst) { EVP_PKEY *pkey = NULL; const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 }; int testresult = 0; EVP_MD_CTX *ctx = NULL; unsigned char *mac = NULL; size_t maclen = 0; unsigned char *pubkey; size_t pubkeylen; switch (keys[tst].type) { case NID_X25519: case NID_X448: return TEST_skip("signing not supported for X25519/X448"); } if (keys[tst].pub == NULL) return TEST_skip("no public key present"); pubkey = (unsigned char *)keys[tst].pub; pubkeylen = strlen(keys[tst].pub); pkey = EVP_PKEY_new_raw_public_key_ex(testctx, OBJ_nid2sn(keys[tst].type), NULL, pubkey, pubkeylen); if (!TEST_ptr(pkey)) goto err; if (!TEST_ptr(ctx = EVP_MD_CTX_new())) goto err; if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1) goto check_err; if (EVP_DigestSign(ctx, NULL, &maclen, msg, sizeof(msg)) != 1) goto check_err; if (!TEST_ptr(mac = OPENSSL_malloc(maclen))) goto err; if (!TEST_int_eq(EVP_DigestSign(ctx, mac, &maclen, msg, sizeof(msg)), 0)) goto err; check_err: if (ERR_GET_REASON(ERR_peek_error()) == PROV_R_NOT_A_PRIVATE_KEY) { testresult = 1; ERR_clear_error(); } err: EVP_MD_CTX_free(ctx); OPENSSL_free(mac); EVP_PKEY_free(pkey); return testresult; } #endif static int test_sign_continuation(void) { OSSL_PROVIDER *fake_rsa = NULL; int testresult = 0; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_MD_CTX *mctx = NULL; const char sigbuf[] = "To Be Signed"; unsigned char signature[256]; size_t siglen = 256; static int nodupnum = 1; static const OSSL_PARAM nodup_params[] = { OSSL_PARAM_int("NO_DUP", &nodupnum), OSSL_PARAM_END }; if (!TEST_ptr(fake_rsa = fake_rsa_start(testctx))) return 0; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", "provider=fake-rsa")) || !TEST_true(EVP_PKEY_fromdata_init(pctx)) || !TEST_true(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, NULL)) || !TEST_ptr(pkey)) goto end; if (!TEST_ptr(mctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx, NULL, pkey, NULL)) || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf))) || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen)) || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf))) || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))) goto end; EVP_MD_CTX_free(mctx); if (!TEST_ptr(mctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx, NULL, pkey, nodup_params)) || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf))) || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen)) || !TEST_false(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf))) || !TEST_false(EVP_DigestSignFinal(mctx, signature, &siglen))) goto end; testresult = 1; end: EVP_MD_CTX_free(mctx); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(pctx); fake_rsa_finish(fake_rsa); return testresult; } static int aes_gcm_encrypt(const unsigned char *gcm_key, size_t gcm_key_s, const unsigned char *gcm_iv, size_t gcm_ivlen, const unsigned char *gcm_pt, size_t gcm_pt_s, const unsigned char *gcm_aad, size_t gcm_aad_s, const unsigned char *gcm_ct, size_t gcm_ct_s, const unsigned char *gcm_tag, size_t gcm_tag_s) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; unsigned char outtag[16]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", ""))) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &gcm_ivlen); if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) || (gcm_aad != NULL && !TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, gcm_aad_s))) || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, gcm_pt_s)) || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen))) goto err; params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, outtag, sizeof(outtag)); if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params)) || !TEST_mem_eq(outbuf, outlen, gcm_ct, gcm_ct_s) || !TEST_mem_eq(outtag, gcm_tag_s, gcm_tag, gcm_tag_s)) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int aes_gcm_decrypt(const unsigned char *gcm_key, size_t gcm_key_s, const unsigned char *gcm_iv, size_t gcm_ivlen, const unsigned char *gcm_pt, size_t gcm_pt_s, const unsigned char *gcm_aad, size_t gcm_aad_s, const unsigned char *gcm_ct, size_t gcm_ct_s, const unsigned char *gcm_tag, size_t gcm_tag_s) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen; unsigned char outbuf[1024]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; if ((cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")) == NULL) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &gcm_ivlen); if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) || (gcm_aad != NULL && !TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, gcm_aad_s))) || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, gcm_ct_s)) || !TEST_mem_eq(outbuf, outlen, gcm_pt, gcm_pt_s)) goto err; params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, (void*)gcm_tag, gcm_tag_s); if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)) ||!TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen))) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int test_aes_gcm_ivlen_change_cve_2023_5363(void) { static const unsigned char gcm_key[] = { 0xd0, 0xc2, 0x67, 0xc1, 0x9f, 0x30, 0xd8, 0x0b, 0x89, 0x14, 0xbb, 0xbf, 0xb7, 0x2f, 0x73, 0xb8, 0xd3, 0xcd, 0x5f, 0x6a, 0x78, 0x70, 0x15, 0x84, 0x8a, 0x7b, 0x30, 0xe3, 0x8f, 0x16, 0xf1, 0x8b, }; static const unsigned char gcm_iv[] = { 0xb6, 0xdc, 0xda, 0x95, 0xac, 0x99, 0x77, 0x76, 0x25, 0xae, 0x87, 0xf8, 0xa3, 0xa9, 0xdd, 0x64, 0xd7, 0x9b, 0xbd, 0x5f, 0x4a, 0x0e, 0x54, 0xca, 0x1a, 0x9f, 0xa2, 0xe3, 0xf4, 0x5f, 0x5f, 0xc2, 0xce, 0xa7, 0xb6, 0x14, 0x12, 0x6f, 0xf0, 0xaf, 0xfd, 0x3e, 0x17, 0x35, 0x6e, 0xa0, 0x16, 0x09, 0xdd, 0xa1, 0x3f, 0xd8, 0xdd, 0xf3, 0xdf, 0x4f, 0xcb, 0x18, 0x49, 0xb8, 0xb3, 0x69, 0x2c, 0x5d, 0x4f, 0xad, 0x30, 0x91, 0x08, 0xbc, 0xbe, 0x24, 0x01, 0x0f, 0xbe, 0x9c, 0xfb, 0x4f, 0x5d, 0x19, 0x7f, 0x4c, 0x53, 0xb0, 0x95, 0x90, 0xac, 0x7b, 0x1f, 0x7b, 0xa0, 0x99, 0xe1, 0xf3, 0x48, 0x54, 0xd0, 0xfc, 0xa9, 0xcc, 0x91, 0xf8, 0x1f, 0x9b, 0x6c, 0x9a, 0xe0, 0xdc, 0x63, 0xea, 0x7d, 0x2a, 0x4a, 0x7d, 0xa5, 0xed, 0x68, 0x57, 0x27, 0x6b, 0x68, 0xe0, 0xf2, 0xb8, 0x51, 0x50, 0x8d, 0x3d, }; static const unsigned char gcm_pt[] = { 0xb8, 0xb6, 0x88, 0x36, 0x44, 0xe2, 0x34, 0xdf, 0x24, 0x32, 0x91, 0x07, 0x4f, 0xe3, 0x6f, 0x81, }; static const unsigned char gcm_ct[] = { 0xff, 0x4f, 0xb3, 0xf3, 0xf9, 0xa2, 0x51, 0xd4, 0x82, 0xc2, 0xbe, 0xf3, 0xe2, 0xd0, 0xec, 0xed, }; static const unsigned char gcm_tag[] = { 0xbd, 0x06, 0x38, 0x09, 0xf7, 0xe1, 0xc4, 0x72, 0x0e, 0xf2, 0xea, 0x63, 0xdb, 0x99, 0x6c, 0x21, }; return aes_gcm_encrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv), gcm_pt, sizeof(gcm_pt), NULL, 0, gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag)) && aes_gcm_decrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv), gcm_pt, sizeof(gcm_pt), NULL, 0, gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag)); } #ifndef OPENSSL_NO_RC4 static int rc4_encrypt(const unsigned char *rc4_key, size_t rc4_key_s, const unsigned char *rc4_pt, size_t rc4_pt_s, const unsigned char *rc4_ct, size_t rc4_ct_s) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen, tmplen; unsigned char outbuf[1024]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "RC4", ""))) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &rc4_key_s); if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, rc4_key, NULL, params)) || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen, rc4_pt, rc4_pt_s)) || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen))) goto err; if (!TEST_mem_eq(outbuf, outlen, rc4_ct, rc4_ct_s)) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int rc4_decrypt(const unsigned char *rc4_key, size_t rc4_key_s, const unsigned char *rc4_pt, size_t rc4_pt_s, const unsigned char *rc4_ct, size_t rc4_ct_s) { int ret = 0; EVP_CIPHER_CTX *ctx; EVP_CIPHER *cipher = NULL; int outlen; unsigned char outbuf[1024]; OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; if ((ctx = EVP_CIPHER_CTX_new()) == NULL) goto err; if ((cipher = EVP_CIPHER_fetch(testctx, "RC4", "")) == NULL) goto err; params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &rc4_key_s); if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, rc4_key, NULL, params)) || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen, rc4_ct, rc4_ct_s)) || !TEST_mem_eq(outbuf, outlen, rc4_pt, rc4_pt_s)) goto err; ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int test_aes_rc4_keylen_change_cve_2023_5363(void) { static const struct { unsigned char key[5]; unsigned char padding[11]; } rc4_key = { { 0x83, 0x32, 0x22, 0x77, 0x2a, }, { 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0xaa, 0x32, 0x91 } }; static const unsigned char rc4_pt[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char rc4_ct[] = { 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda }; if (lgcyprov == NULL) return TEST_skip("Test requires legacy provider to be loaded"); return rc4_encrypt(rc4_key.key, sizeof(rc4_key.key), rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct)) && rc4_decrypt(rc4_key.key, sizeof(rc4_key.key), rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct)); } #endif int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONTEXT: testctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(testctx)) return 0; #ifdef STATIC_LEGACY if (!OSSL_PROVIDER_add_builtin(testctx, "legacy", ossl_legacy_provider_init)) return 0; #endif nullprov = OSSL_PROVIDER_load(NULL, "null"); deflprov = OSSL_PROVIDER_load(testctx, "default"); #ifndef OPENSSL_SYS_TANDEM lgcyprov = OSSL_PROVIDER_load(testctx, "legacy"); #endif break; case OPT_TEST_CASES: break; default: return 0; } } ADD_TEST(test_EVP_set_default_properties); ADD_ALL_TESTS(test_EVP_DigestSignInit, 30); ADD_TEST(test_EVP_DigestVerifyInit); #ifndef OPENSSL_NO_SIPHASH ADD_TEST(test_siphash_digestsign); #endif ADD_TEST(test_EVP_Digest); ADD_TEST(test_EVP_md_null); ADD_ALL_TESTS(test_EVP_PKEY_sign, 3); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_ALL_TESTS(test_EVP_PKEY_sign_with_app_method, 2); #endif ADD_ALL_TESTS(test_EVP_Enveloped, 2); ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata)); ADD_TEST(test_privatekey_to_pkcs8); ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag); #ifndef OPENSSL_NO_EC ADD_TEST(test_EVP_PKCS82PKEY); #endif #ifndef OPENSSL_NO_EC ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings)); #endif #if !defined(OPENSSL_NO_SM2) ADD_TEST(test_EVP_SM2); ADD_TEST(test_EVP_SM2_verify); #endif ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys)); #ifndef OPENSSL_NO_DEPRECATED_3_0 custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0); if (!TEST_ptr(custom_pmeth)) return 0; EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check); EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check); EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check); if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1)) return 0; #endif ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata)); #ifndef OPENSSL_NO_CMAC ADD_TEST(test_CMAC_keygen); #endif ADD_TEST(test_HKDF); ADD_TEST(test_emptyikm_HKDF); #ifndef OPENSSL_NO_EC ADD_TEST(test_X509_PUBKEY_inplace); ADD_TEST(test_X509_PUBKEY_dup); ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode, OSSL_NELEM(ec_der_pub_keys)); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_DSA_get_set_params); ADD_TEST(test_DSA_priv_pub); #endif ADD_TEST(test_RSA_get_set_params); ADD_TEST(test_RSA_OAEP_set_get_params); ADD_TEST(test_RSA_OAEP_set_null_label); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_RSA_legacy); #endif #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) ADD_TEST(test_decrypt_null_chunks); #endif #ifndef OPENSSL_NO_DH ADD_TEST(test_DH_priv_pub); # ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_EVP_PKEY_set1_DH); # endif #endif #ifndef OPENSSL_NO_EC ADD_TEST(test_EC_priv_pub); ADD_TEST(test_evp_get_ec_pub); # ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_EC_priv_only_legacy); ADD_TEST(test_evp_get_ec_pub_legacy); # endif #endif ADD_ALL_TESTS(test_keygen_with_empty_template, 2); ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2); ADD_TEST(test_rand_agglomeration); ADD_ALL_TESTS(test_evp_iv_aes, 12); #ifndef OPENSSL_NO_DES ADD_ALL_TESTS(test_evp_iv_des, 6); #endif #ifndef OPENSSL_NO_BF ADD_ALL_TESTS(test_evp_bf_default_keylen, 4); #endif ADD_TEST(test_EVP_rsa_pss_with_keygen_bits); ADD_TEST(test_EVP_rsa_pss_set_saltlen); #ifndef OPENSSL_NO_EC ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids)); #endif ADD_TEST(test_names_do_all); ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests)); ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests)); ADD_ALL_TESTS(test_evp_reinit_seq, OSSL_NELEM(evp_reinit_tests)); ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests)); ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests)); ADD_ALL_TESTS(test_ivlen_change, OSSL_NELEM(ivlen_change_ciphers)); if (OSSL_NELEM(keylen_change_ciphers) - 1 > 0) ADD_ALL_TESTS(test_keylen_change, OSSL_NELEM(keylen_change_ciphers) - 1); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_ALL_TESTS(test_custom_pmeth, 12); ADD_TEST(test_evp_md_cipher_meth); ADD_TEST(test_custom_md_meth); ADD_TEST(test_custom_ciph_meth); # ifndef OPENSSL_NO_DYNAMIC_ENGINE if (testctx == NULL) { # ifndef OPENSSL_NO_EC ADD_ALL_TESTS(test_signatures_with_engine, 3); # else ADD_ALL_TESTS(test_signatures_with_engine, 2); # endif ADD_TEST(test_cipher_with_engine); } # endif #endif #ifndef OPENSSL_NO_ECX ADD_ALL_TESTS(test_ecx_short_keys, OSSL_NELEM(ecxnids)); ADD_ALL_TESTS(test_ecx_not_private_key, OSSL_NELEM(keys)); #endif ADD_TEST(test_sign_continuation); ADD_TEST(test_aes_gcm_ivlen_change_cve_2023_5363); #ifndef OPENSSL_NO_RC4 ADD_TEST(test_aes_rc4_keylen_change_cve_2023_5363); #endif return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(nullprov); OSSL_PROVIDER_unload(deflprov); #ifndef OPENSSL_SYS_TANDEM OSSL_PROVIDER_unload(lgcyprov); #endif OSSL_LIB_CTX_free(testctx); }
test
openssl/test/evp_extra_test.c
openssl
#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; 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; 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; } 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; unsigned char unknown_frame[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 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; 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, 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; } 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 = OSSL_QUIC_ERR_CRYPTO_MISSING_EXT; } else { ext = TLSEXT_TYPE_application_layer_protocol_negotiation; err = OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO; } if (!TEST_true(qtest_fault_set_hand_enc_ext_listener(fault, drop_extensions_cb, &ext))) goto err; 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; } 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; } 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) { if (!TEST_true(qtest_fault_set_packet_cipher_listener(fault, on_packet_cipher_cb, NULL))) goto err; } else { 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; docorrupt = 1; if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)) || !TEST_uint64_t_eq(sid, 1)) goto err; 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; qtest_add_time(100); 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; if (!TEST_true(SSL_handle_events(cssl))) goto err; ossl_quic_tserver_tick(qtserv); if (!TEST_true(SSL_handle_events(cssl))) goto err; if (!TEST_true(SSL_read_ex(cssl, buf, sizeof(buf), &bytesread))) goto err; if (!TEST_mem_eq(msg, msglen, buf, bytesread)) goto err; 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); }
test
openssl/test/quicfaultstest.c
openssl
#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" #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; 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)); 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; 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; } 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); }
test
openssl/test/cmsapitest.c
openssl
#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, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02 }; static unsigned char t_dsa_sig_extra[] = { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x05, 0x00 }; static unsigned char t_dsa_sig_msb[] = { 0x30, 0x08, 0x02, 0x02, 0x00, 0x81, 0x02, 0x02, 0x00, 0x82 }; static unsigned char t_dsa_sig_two[] = { 0x30, 0x08, 0x02, 0x02, 0x01, 0x00, 0x02, 0x02, 0x02, 0x00 }; static unsigned char t_invalid_int_zero[] = { 0x30, 0x05, 0x02, 0x00, 0x02, 0x01, 0x2a }; static unsigned char t_invalid_int[] = { 0x30, 0x07, 0x02, 0x02, 0x00, 0x7f, 0x02, 0x01, 0x2a }; static unsigned char t_neg_int[] = { 0x30, 0x06, 0x02, 0x01, 0xaa, 0x02, 0x01, 0x2a }; static unsigned char t_trunc_der[] = { 0x30, 0x08, 0x02, 0x02, 0x00, 0x81, 0x02, 0x02, 0x00 }; static unsigned char t_trunc_seq[] = { 0x30, 0x07, 0x02, 0x02, 0x00, 0x81, 0x02, 0x02, 0x00, 0x82 }; static int test_decode(void) { int rv = 0; BIGNUM *r; BIGNUM *s; const unsigned char *pder; r = BN_new(); s = BN_new(); 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; } 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; }
test
openssl/test/asn1_dsa_internal_test.c
openssl
#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/ct.h> #include <openssl/err.h> #include <openssl/pem.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include "testutil.h" #include <openssl/crypto.h> #ifndef OPENSSL_NO_CT # define CT_TEST_MAX_FILE_SIZE 8096 static char *certs_dir = NULL; static char *ct_dir = NULL; typedef struct ct_test_fixture { const char *test_case_name; uint64_t epoch_time_in_ms; CTLOG_STORE* ctlog_store; const char *certs_dir; char *certificate_file; char *issuer_file; int expected_sct_count; int expected_valid_sct_count; const unsigned char *tls_sct_list; size_t tls_sct_list_len; STACK_OF(SCT) *sct_list; const char *sct_dir; const char *sct_text_file; int test_validity; } CT_TEST_FIXTURE; static CT_TEST_FIXTURE *set_up(const char *const test_case_name) { CT_TEST_FIXTURE *fixture = NULL; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) goto end; fixture->test_case_name = test_case_name; fixture->epoch_time_in_ms = 1580335307000ULL; if (!TEST_ptr(fixture->ctlog_store = CTLOG_STORE_new()) || !TEST_int_eq( CTLOG_STORE_load_default_file(fixture->ctlog_store), 1)) goto end; return fixture; end: if (fixture != NULL) CTLOG_STORE_free(fixture->ctlog_store); OPENSSL_free(fixture); TEST_error("Failed to setup"); return NULL; } static void tear_down(CT_TEST_FIXTURE *fixture) { if (fixture != NULL) { CTLOG_STORE_free(fixture->ctlog_store); SCT_LIST_free(fixture->sct_list); } OPENSSL_free(fixture); } static X509 *load_pem_cert(const char *dir, const char *file) { X509 *cert = NULL; char *file_path = test_mk_file_path(dir, file); if (file_path != NULL) { BIO *cert_io = BIO_new_file(file_path, "r"); if (cert_io != NULL) cert = PEM_read_bio_X509(cert_io, NULL, NULL, NULL); BIO_free(cert_io); } OPENSSL_free(file_path); return cert; } static int read_text_file(const char *dir, const char *file, char *buffer, int buffer_length) { int len = -1; char *file_path = test_mk_file_path(dir, file); if (file_path != NULL) { BIO *file_io = BIO_new_file(file_path, "r"); if (file_io != NULL) len = BIO_read(file_io, buffer, buffer_length); BIO_free(file_io); } OPENSSL_free(file_path); return len; } static int compare_sct_list_printout(STACK_OF(SCT) *sct, const char *expected_output) { BIO *text_buffer = NULL; char *actual_output = NULL; int result = 0; if (!TEST_ptr(text_buffer = BIO_new(BIO_s_mem()))) goto end; SCT_LIST_print(sct, text_buffer, 0, "\n", NULL); if (!TEST_true(BIO_write(text_buffer, "\0", 1))) goto end; BIO_get_mem_data(text_buffer, &actual_output); if (!TEST_str_eq(actual_output, expected_output)) goto end; result = 1; end: BIO_free(text_buffer); return result; } static int compare_extension_printout(X509_EXTENSION *extension, const char *expected_output) { BIO *text_buffer = NULL; char *actual_output = NULL; int result = 0; if (!TEST_ptr(text_buffer = BIO_new(BIO_s_mem())) || !TEST_true(X509V3_EXT_print(text_buffer, extension, X509V3_EXT_DEFAULT, 0))) goto end; if (!TEST_true(BIO_write(text_buffer, "\n", 1))) goto end; if (!TEST_true(BIO_write(text_buffer, "\0", 1))) goto end; BIO_get_mem_data(text_buffer, &actual_output); if (!TEST_str_eq(actual_output, expected_output)) goto end; result = 1; end: BIO_free(text_buffer); return result; } static int assert_validity(CT_TEST_FIXTURE *fixture, STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *policy_ctx) { int invalid_sct_count = 0; int valid_sct_count = 0; int i; if (!TEST_int_ge(SCT_LIST_validate(scts, policy_ctx), 0)) return 0; for (i = 0; i < sk_SCT_num(scts); ++i) { SCT *sct_i = sk_SCT_value(scts, i); switch (SCT_get_validation_status(sct_i)) { case SCT_VALIDATION_STATUS_VALID: ++valid_sct_count; break; case SCT_VALIDATION_STATUS_INVALID: ++invalid_sct_count; break; case SCT_VALIDATION_STATUS_NOT_SET: case SCT_VALIDATION_STATUS_UNKNOWN_LOG: case SCT_VALIDATION_STATUS_UNVERIFIED: case SCT_VALIDATION_STATUS_UNKNOWN_VERSION: break; } } if (!TEST_int_eq(valid_sct_count, fixture->expected_valid_sct_count)) { int unverified_sct_count = sk_SCT_num(scts) - invalid_sct_count - valid_sct_count; TEST_info("%d SCTs failed, %d SCTs unverified", invalid_sct_count, unverified_sct_count); return 0; } return 1; } static int execute_cert_test(CT_TEST_FIXTURE *fixture) { int success = 0; X509 *cert = NULL, *issuer = NULL; STACK_OF(SCT) *scts = NULL; SCT *sct = NULL; char expected_sct_text[CT_TEST_MAX_FILE_SIZE]; int sct_text_len = 0; unsigned char *tls_sct_list = NULL; size_t tls_sct_list_len = 0; CT_POLICY_EVAL_CTX *ct_policy_ctx = CT_POLICY_EVAL_CTX_new(); if (fixture->sct_text_file != NULL) { sct_text_len = read_text_file(fixture->sct_dir, fixture->sct_text_file, expected_sct_text, CT_TEST_MAX_FILE_SIZE - 1); if (!TEST_int_ge(sct_text_len, 0)) goto end; expected_sct_text[sct_text_len] = '\0'; } CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE( ct_policy_ctx, fixture->ctlog_store); CT_POLICY_EVAL_CTX_set_time(ct_policy_ctx, fixture->epoch_time_in_ms); if (fixture->certificate_file != NULL) { int sct_extension_index; int i; X509_EXTENSION *sct_extension = NULL; if (!TEST_ptr(cert = load_pem_cert(fixture->certs_dir, fixture->certificate_file))) goto end; CT_POLICY_EVAL_CTX_set1_cert(ct_policy_ctx, cert); if (fixture->issuer_file != NULL) { if (!TEST_ptr(issuer = load_pem_cert(fixture->certs_dir, fixture->issuer_file))) goto end; CT_POLICY_EVAL_CTX_set1_issuer(ct_policy_ctx, issuer); } sct_extension_index = X509_get_ext_by_NID(cert, NID_ct_precert_scts, -1); sct_extension = X509_get_ext(cert, sct_extension_index); if (fixture->expected_sct_count > 0) { if (!TEST_ptr(sct_extension)) goto end; if (fixture->sct_text_file && !compare_extension_printout(sct_extension, expected_sct_text)) goto end; scts = X509V3_EXT_d2i(sct_extension); for (i = 0; i < sk_SCT_num(scts); ++i) { SCT *sct_i = sk_SCT_value(scts, i); if (!TEST_int_eq(SCT_get_source(sct_i), SCT_SOURCE_X509V3_EXTENSION)) { goto end; } } if (fixture->test_validity) { if (!assert_validity(fixture, scts, ct_policy_ctx)) goto end; } } else if (!TEST_ptr_null(sct_extension)) { goto end; } } if (fixture->tls_sct_list != NULL) { const unsigned char *p = fixture->tls_sct_list; if (!TEST_ptr(o2i_SCT_LIST(&scts, &p, fixture->tls_sct_list_len))) goto end; if (fixture->test_validity && cert != NULL) { if (!assert_validity(fixture, scts, ct_policy_ctx)) goto end; } if (fixture->sct_text_file && !compare_sct_list_printout(scts, expected_sct_text)) { goto end; } tls_sct_list_len = i2o_SCT_LIST(scts, &tls_sct_list); if (!TEST_mem_eq(fixture->tls_sct_list, fixture->tls_sct_list_len, tls_sct_list, tls_sct_list_len)) goto end; } success = 1; end: X509_free(cert); X509_free(issuer); SCT_LIST_free(scts); SCT_free(sct); CT_POLICY_EVAL_CTX_free(ct_policy_ctx); OPENSSL_free(tls_sct_list); return success; } # define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE, set_up) # define EXECUTE_CT_TEST() EXECUTE_TEST(execute_cert_test, tear_down) static int test_no_scts_in_certificate(void) { SETUP_CT_TEST_FIXTURE(); fixture->certs_dir = certs_dir; fixture->certificate_file = "leaf.pem"; fixture->issuer_file = "subinterCA.pem"; fixture->expected_sct_count = 0; EXECUTE_CT_TEST(); return result; } static int test_one_sct_in_certificate(void) { SETUP_CT_TEST_FIXTURE(); fixture->certs_dir = certs_dir; fixture->certificate_file = "embeddedSCTs1.pem"; fixture->issuer_file = "embeddedSCTs1_issuer.pem"; fixture->expected_sct_count = 1; fixture->sct_dir = certs_dir; fixture->sct_text_file = "embeddedSCTs1.sct"; EXECUTE_CT_TEST(); return result; } static int test_multiple_scts_in_certificate(void) { SETUP_CT_TEST_FIXTURE(); fixture->certs_dir = certs_dir; fixture->certificate_file = "embeddedSCTs3.pem"; fixture->issuer_file = "embeddedSCTs3_issuer.pem"; fixture->expected_sct_count = 3; fixture->sct_dir = certs_dir; fixture->sct_text_file = "embeddedSCTs3.sct"; EXECUTE_CT_TEST(); return result; } static int test_verify_one_sct(void) { SETUP_CT_TEST_FIXTURE(); fixture->certs_dir = certs_dir; fixture->certificate_file = "embeddedSCTs1.pem"; fixture->issuer_file = "embeddedSCTs1_issuer.pem"; fixture->expected_sct_count = fixture->expected_valid_sct_count = 1; fixture->test_validity = 1; EXECUTE_CT_TEST(); return result; } static int test_verify_multiple_scts(void) { SETUP_CT_TEST_FIXTURE(); fixture->certs_dir = certs_dir; fixture->certificate_file = "embeddedSCTs3.pem"; fixture->issuer_file = "embeddedSCTs3_issuer.pem"; fixture->expected_sct_count = fixture->expected_valid_sct_count = 3; fixture->test_validity = 1; EXECUTE_CT_TEST(); return result; } static int test_verify_fails_for_future_sct(void) { SETUP_CT_TEST_FIXTURE(); fixture->epoch_time_in_ms = 1365094800000ULL; fixture->certs_dir = certs_dir; fixture->certificate_file = "embeddedSCTs1.pem"; fixture->issuer_file = "embeddedSCTs1_issuer.pem"; fixture->expected_sct_count = 1; fixture->expected_valid_sct_count = 0; fixture->test_validity = 1; EXECUTE_CT_TEST(); return result; } static int test_decode_tls_sct(void) { const unsigned char tls_sct_list[] = "\x00\x78" "\x00\x76" "\x00" "\xDF\x1C\x2E\xC1\x15\x00\x94\x52\x47\xA9\x61\x68\x32\x5D\xDC\x5C\x79" "\x59\xE8\xF7\xC6\xD3\x88\xFC\x00\x2E\x0B\xBD\x3F\x74\xD7\x64" "\x00\x00\x01\x3D\xDB\x27\xDF\x93" "\x00\x00" "" "\x04\x03" "\x00\x47" "\x30\x45\x02\x20\x48\x2F\x67\x51\xAF\x35\xDB\xA6\x54\x36\xBE\x1F\xD6" "\x64\x0F\x3D\xBF\x9A\x41\x42\x94\x95\x92\x45\x30\x28\x8F\xA3\xE5\xE2" "\x3E\x06\x02\x21\x00\xE4\xED\xC0\xDB\x3A\xC5\x72\xB1\xE2\xF5\xE8\xAB" "\x6A\x68\x06\x53\x98\x7D\xCF\x41\x02\x7D\xFE\xFF\xA1\x05\x51\x9D\x89" "\xED\xBF\x08"; SETUP_CT_TEST_FIXTURE(); fixture->tls_sct_list = tls_sct_list; fixture->tls_sct_list_len = 0x7a; fixture->sct_dir = ct_dir; fixture->sct_text_file = "tls1.sct"; EXECUTE_CT_TEST(); return result; } static int test_encode_tls_sct(void) { const char log_id[] = "3xwuwRUAlFJHqWFoMl3cXHlZ6PfG04j8AC4LvT9012Q="; const uint64_t timestamp = 1; const char extensions[] = ""; const char signature[] = "BAMARzBAMiBIL2dRrzXbplQ2vh/WZA89v5pBQpSVkkUwKI+j5" "eI+BgIhAOTtwNs6xXKx4vXoq2poBlOYfc9BAn3+/6EFUZ2J7b8I"; SCT *sct = NULL; SETUP_CT_TEST_FIXTURE(); fixture->sct_list = sk_SCT_new_null(); if (fixture->sct_list == NULL) return 0; if (!TEST_ptr(sct = SCT_new_from_base64(SCT_VERSION_V1, log_id, CT_LOG_ENTRY_TYPE_X509, timestamp, extensions, signature))) return 0; sk_SCT_push(fixture->sct_list, sct); fixture->sct_dir = ct_dir; fixture->sct_text_file = "tls1.sct"; EXECUTE_CT_TEST(); return result; } static int test_default_ct_policy_eval_ctx_time_is_now(void) { int success = 0; CT_POLICY_EVAL_CTX *ct_policy_ctx = CT_POLICY_EVAL_CTX_new(); const time_t default_time = (time_t)(CT_POLICY_EVAL_CTX_get_time(ct_policy_ctx) / 1000); const time_t time_tolerance = 600; if (!TEST_time_t_le(abs((int)difftime(time(NULL), default_time)), time_tolerance)) goto end; success = 1; end: CT_POLICY_EVAL_CTX_free(ct_policy_ctx); return success; } static int test_ctlog_from_base64(void) { CTLOG *ctlogp = NULL; const char notb64[] = "\01\02\03\04"; const char pad[] = "===="; const char name[] = "name"; if (!TEST_true(!CTLOG_new_from_base64(&ctlogp, notb64, name)) || !TEST_true(!CTLOG_new_from_base64(&ctlogp, pad, name))) return 0; return 1; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_CT if ((ct_dir = getenv("CT_DIR")) == NULL) ct_dir = "ct"; if ((certs_dir = getenv("CERTS_DIR")) == NULL) certs_dir = "certs"; ADD_TEST(test_no_scts_in_certificate); ADD_TEST(test_one_sct_in_certificate); ADD_TEST(test_multiple_scts_in_certificate); ADD_TEST(test_verify_one_sct); ADD_TEST(test_verify_multiple_scts); ADD_TEST(test_verify_fails_for_future_sct); ADD_TEST(test_decode_tls_sct); ADD_TEST(test_encode_tls_sct); ADD_TEST(test_default_ct_policy_eval_ctx_time_is_now); ADD_TEST(test_ctlog_from_base64); #else printf("No CT support\n"); #endif return 1; }
test
openssl/test/ct_test.c
openssl
#include <stdio.h> #include <openssl/err.h> #include <openssl/x509_vfy.h> #include "testutil.h" static int test_509_dup_cert(int n) { int ret = 0; X509_STORE *store = NULL; X509_LOOKUP *lookup = NULL; const char *cert_f = test_get_argument(n); 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, cert_f, X509_FILETYPE_PEM)) && TEST_true(X509_load_cert_file(lookup, cert_f, X509_FILETYPE_PEM))) ret = 1; X509_STORE_free(store); return ret; } OPT_TEST_DECLARE_USAGE("cert.pem...\n") int setup_tests(void) { size_t n; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } n = test_get_argument_count(); if (!TEST_int_gt(n, 0)) return 0; ADD_ALL_TESTS(test_509_dup_cert, n); return 1; }
test
openssl/test/x509_dup_cert_test.c
openssl
#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; }
test
openssl/test/hexstr_test.c
openssl
#include <string.h> #include <openssl/bio.h> #include "testutil.h" struct ossl_core_bio_st { int dummy; BIO *bio; }; static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len, size_t *bytes_read) { return BIO_read_ex(bio->bio, data, data_len, bytes_read); } static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data, size_t data_len, size_t *written) { return BIO_write_ex(bio->bio, data, data_len, written); } static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size) { return BIO_gets(bio->bio, buf, size); } static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str) { return BIO_puts(bio->bio, str); } static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr) { return BIO_ctrl(bio->bio, cmd, num, ptr); } static int tst_bio_core_up_ref(OSSL_CORE_BIO *bio) { return BIO_up_ref(bio->bio); } static int tst_bio_core_free(OSSL_CORE_BIO *bio) { return BIO_free(bio->bio); } static const OSSL_DISPATCH biocbs[] = { { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex }, { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex }, { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets }, { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts }, { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl }, { OSSL_FUNC_BIO_UP_REF, (void (*)(void))tst_bio_core_up_ref }, { OSSL_FUNC_BIO_FREE, (void (*)(void))tst_bio_core_free }, OSSL_DISPATCH_END }; static int test_bio_core(void) { BIO *cbio = NULL, *cbiobad = NULL; OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs); int testresult = 0; OSSL_CORE_BIO corebio; const char *msg = "Hello world"; char buf[80]; corebio.bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(corebio.bio) || !TEST_ptr(libctx) || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio))) || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio)))) goto err; if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0) || !TEST_false(BIO_eof(cbio)) || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0) || !TEST_true(BIO_eof(cbio)) || !TEST_str_eq(buf, msg)) goto err; buf[0] = '\0'; if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0) || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0) || !TEST_str_eq(buf, msg)) goto err; testresult = 1; err: BIO_free(cbiobad); BIO_free(cbio); BIO_free(corebio.bio); OSSL_LIB_CTX_free(libctx); return testresult; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } ADD_TEST(test_bio_core); return 1; }
test
openssl/test/bio_core_test.c
openssl
#include "testutil.h" #include "crypto/ctype.h" #include "internal/nelem.h" #include <ctype.h> #include <stdio.h> #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; }
test
openssl/test/ctype_internal_test.c
openssl
#include "internal/nelem.h" #include <string.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/pem.h> #include <openssl/x509.h> #include "testutil.h" #define PARAM_TIME 1474934400 static const char *kCRLTestRoot[] = { "-----BEGIN CERTIFICATE-----\n", "MIIDbzCCAlegAwIBAgIJAODri7v0dDUFMA0GCSqGSIb3DQEBCwUAME4xCzAJBgNV\n", "BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1Nb3VudGFpbiBW\n", "aWV3MRIwEAYDVQQKDAlCb3JpbmdTU0wwHhcNMTYwOTI2MTUwNjI2WhcNMjYwOTI0\n", "MTUwNjI2WjBOMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG\n", "A1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJQm9yaW5nU1NMMIIBIjANBgkq\n", "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo16WiLWZuaymsD8n5SKPmxV1y6jjgr3B\n", "S/dUBpbrzd1aeFzNlI8l2jfAnzUyp+I21RQ+nh/MhqjGElkTtK9xMn1Y+S9GMRh+\n", "5R/Du0iCb1tCZIPY07Tgrb0KMNWe0v2QKVVruuYSgxIWodBfxlKO64Z8AJ5IbnWp\n", "uRqO6rctN9qUoMlTIAB6dL4G0tDJ/PGFWOJYwOMEIX54bly2wgyYJVBKiRRt4f7n\n", "8H922qmvPNA9idmX9G1VAtgV6x97XXi7ULORIQvn9lVQF6nTYDBJhyuPB+mLThbL\n", "P2o9orxGx7aCtnnBZUIxUvHNOI0FaSaZH7Fi0xsZ/GkG2HZe7ImPJwIDAQABo1Aw\n", "TjAdBgNVHQ4EFgQUWPt3N5cZ/CRvubbrkqfBnAqhq94wHwYDVR0jBBgwFoAUWPt3\n", "N5cZ/CRvubbrkqfBnAqhq94wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n", "AQEAORu6M0MOwXy+3VEBwNilfTxyqDfruQsc1jA4PT8Oe8zora1WxE1JB4q2FJOz\n", "EAuM3H/NXvEnBuN+ITvKZAJUfm4NKX97qmjMJwLKWe1gVv+VQTr63aR7mgWJReQN\n", "XdMztlVeZs2dppV6uEg3ia1X0G7LARxGpA9ETbMyCpb39XxlYuTClcbA5ftDN99B\n", "3Xg9KNdd++Ew22O3HWRDvdDpTO/JkzQfzi3sYwUtzMEonENhczJhGf7bQMmvL/w5\n", "24Wxj4Z7KzzWIHsNqE/RIs6RV3fcW61j/mRgW2XyoWnMVeBzvcJr9NXp4VQYmFPw\n", "amd8GKMZQvP0ufGnUn7D7uartA==\n", "-----END CERTIFICATE-----\n", NULL }; static const char *kCRLTestLeaf[] = { "-----BEGIN CERTIFICATE-----\n", "MIIDkDCCAnigAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwTjELMAkGA1UEBhMCVVMx\n", "EzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxEjAQ\n", "BgNVBAoMCUJvcmluZ1NTTDAeFw0xNjA5MjYxNTA4MzFaFw0xNzA5MjYxNTA4MzFa\n", "MEsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRIwEAYDVQQKDAlC\n", "b3JpbmdTU0wxEzARBgNVBAMMCmJvcmluZy5zc2wwggEiMA0GCSqGSIb3DQEBAQUA\n", "A4IBDwAwggEKAoIBAQDc5v1S1M0W+QWM+raWfO0LH8uvqEwuJQgODqMaGnSlWUx9\n", "8iQcnWfjyPja3lWg9K62hSOFDuSyEkysKHDxijz5R93CfLcfnVXjWQDJe7EJTTDP\n", "ozEvxN6RjAeYv7CF000euYr3QT5iyBjg76+bon1p0jHZBJeNPP1KqGYgyxp+hzpx\n", "e0gZmTlGAXd8JQK4v8kpdYwD6PPifFL/jpmQpqOtQmH/6zcLjY4ojmqpEdBqIKIX\n", "+saA29hMq0+NK3K+wgg31RU+cVWxu3tLOIiesETkeDgArjWRS1Vkzbi4v9SJxtNu\n", "OZuAxWiynRJw3JwH/OFHYZIvQqz68ZBoj96cepjPAgMBAAGjezB5MAkGA1UdEwQC\n", "MAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRl\n", "MB0GA1UdDgQWBBTGn0OVVh/aoYt0bvEKG+PIERqnDzAfBgNVHSMEGDAWgBRY+3c3\n", "lxn8JG+5tuuSp8GcCqGr3jANBgkqhkiG9w0BAQsFAAOCAQEAd2nM8gCQN2Dc8QJw\n", "XSZXyuI3DBGGCHcay/3iXu0JvTC3EiQo8J6Djv7WLI0N5KH8mkm40u89fJAB2lLZ\n", "ShuHVtcC182bOKnePgwp9CNwQ21p0rDEu/P3X46ZvFgdxx82E9xLa0tBB8PiPDWh\n", "lV16jbaKTgX5AZqjnsyjR5o9/mbZVupZJXx5Syq+XA8qiJfstSYJs4KyKK9UOjql\n", "ICkJVKpi2ahDBqX4MOH4SLfzVk8pqSpviS6yaA1RXqjpkxiN45WWaXDldVHMSkhC\n", "5CNXsXi4b1nAntu89crwSLA3rEwzCWeYj+BX7e1T9rr3oJdwOU/2KQtW1js1yQUG\n", "tjJMFw==\n", "-----END CERTIFICATE-----\n", NULL }; static const char *kBasicCRL[] = { "-----BEGIN X509 CRL-----\n", "MIIBpzCBkAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoA4wDDAKBgNV\n", "HRQEAwIBATANBgkqhkiG9w0BAQsFAAOCAQEAnrBKKgvd9x9zwK9rtUvVeFeJ7+LN\n", "ZEAc+a5oxpPNEsJx6hXoApYEbzXMxuWBQoCs5iEBycSGudct21L+MVf27M38KrWo\n", "eOkq0a2siqViQZO2Fb/SUFR0k9zb8xl86Zf65lgPplALun0bV/HT7MJcl04Tc4os\n", "dsAReBs5nqTGNEd5AlC1iKHvQZkM "diyu0fZ/bPAM3VAGawatf/SyWfBMyKpoPXEG39oAzmjjOj8en82psn7m474IGaho\n", "/vBbhl1ms5qQiLYPjm4YELtnXQoFyC72tBjbdFd/ZE9k4CNKDbxFUXFbkw==\n", "-----END X509 CRL-----\n", NULL }; static const char *kRevokedCRL[] = { "-----BEGIN X509 CRL-----\n", "MIIBvjCBpwIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", "Qm9yaW5nU1NMFw0xNjA5MjYxNTEyNDRaFw0xNjEwMjYxNTEyNDRaMBUwEwICEAAX\n", "DTE2MDkyNjE1MTIyNlqgDjAMMAoGA1UdFAQDAgECMA0GCSqGSIb3DQEBCwUAA4IB\n", "AQCUGaM4DcWzlQKrcZvI8TMeR8BpsvQeo5BoI/XZu2a8h "sjgCT8b3C1FPgT+P2Lkowv7rJ+FHJRNQkogr+RuqCSPTq65ha4WKlRGWkMFybzVH\n", "NloxC+aU3lgp/NlX9yUtfqYmJek1CDrOOGPrAEAwj1l/BUeYKNGqfBWYJQtPJu+5\n", "OaSvIYGpETCZJscUWODmLEb/O3DM438vLvxonwGqXqS0KX37+CHpUlyhnSovxXxp\n", "Pz4aF+L7OtczxL0GYtD2fR9B7TDMqsNmHXgQrixvvOY7MUdLGbd4RfJL3yA53hyO\n", "xzfKY2TzxLiOmctG0hXFkH5J\n", "-----END X509 CRL-----\n", NULL }; static const char *kBadIssuerCRL[] = { "-----BEGIN X509 CRL-----\n", "MIIBwjCBqwIBATANBgkqhkiG9w0BAQsFADBSMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEWMBQGA1UECgwN\n", "Tm90IEJvcmluZ1NTTBcNMTYwOTI2MTUxMjQ0WhcNMTYxMDI2MTUxMjQ0WjAVMBMC\n", "AhAAFw0xNjA5MjYxNTEyMjZaoA4wDDAKBgNVHRQEAwIBAjANBgkqhkiG9w0BAQsF\n", "AAOCAQEAlBmjOA3Fs5UCq3GbyPEzHkfAabL0HqOQaCP12btmvIf/z8kcjMGHmjjP\n", "t85dHbI4Ak/G9wtRT4E/j9i5KML+6yfhRyUTUJKIK/kbqgkj06uuYWuFipURlpDB\n", "cm81RzZaMQvmlN5YKfzZV/clLX6mJiXpNQg6zjhj6wBAMI9ZfwVHmCjRqnwVmCUL\n", "TybvuTmkryGBqREwmSbHFFjg5ixG/ztwzON/Ly78aJ8Bql6ktCl9+/gh6VJcoZ0q\n", "L8V8aT8+Ghfi+zrXM8S9BmLQ9n0fQe0wzKrDZh14EK4sb7zmOzFHSxm3eEXyS98g\n", "Od4cjsc3ymNk88S4jpnLRtIVxZB+SQ==\n", "-----END X509 CRL-----\n", NULL }; static const char *kKnownCriticalCRL[] = { "-----BEGIN X509 CRL-----\n", "MIIBujCBowIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCEwHzAKBgNV\n", "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wDQYJKoZIhvcNAQELBQADggEBAA+3\n", "i+5e5Ub8sccfgOBs6WVJFI9c8gvJjrJ8/dYfFIAuCyeocs7DFXn1n13CRZ+URR/Q\n", "mVWgU28+xeusuSPYFpd9cyYTcVyNUGNTI3lwgcE/yVjPaOmzSZKdPakApRxtpKKQ\n", "NN/56aQz3bnT/ZSHQNciRB8U6jiD9V30t0w+FDTpGaG+7bzzUH3UVF9xf9Ctp60A\n", "3mfLe0scas7owSt4AEFuj2SPvcE7yvdOXbu+IEv21cEJUVExJAbhvIweHXh6yRW+\n", "7VVeiNzdIjkZjyTmAzoXGha4+wbxXyBRbfH+XWcO/H+8nwyG8Gktdu2QB9S9nnIp\n", "o/1TpfOMSGhMyMoyPrk=\n", "-----END X509 CRL-----\n", NULL }; static const char *kUnknownCriticalCRL[] = { "-----BEGIN X509 CRL-----\n", "MIIBvDCBpQIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCMwITAKBgNV\n", "HRQEAwIBATATBgwqhkiG9xIEAYS3CQABAf8EADANBgkqhkiG9w0BAQsFAAOCAQEA\n", "GvBP0xqL509InMj/3493YVRV+ldTpBv5uTD6jewzf5XdaxEQ/VjTNe5zKnxbpAib\n", "Kf7cwX0PMSkZjx7k7kKdDlEucwVvDoqC+O9aJcqVmM6GDyNb9xENxd0XCXja6MZC\n", "yVgP4AwLauB2vSiEprYJyI1APph3iAEeDm60lTXX/wBM/tupQDDujKh2GPyvBRfJ\n", "+wEDwGg3ICwvu4gO4zeC5qnFR+bpL9t5tOMAQnVZ0NWv+k7mkd2LbHdD44dxrfXC\n", "nhtfERx99SDmC/jtUAJrGhtCO8acr7exCeYcduN7KKCm91OeCJKK6OzWst0Og1DB\n", "kwzzU2rL3G65CrZ7H0SZsQ==\n", "-----END X509 CRL-----\n", NULL }; static const char *kUnknownCriticalCRL2[] = { "-----BEGIN X509 CRL-----\n", "MIIBzzCBuAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n", "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n", "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoDYwNDAKBgNV\n", "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wEwYMKoZIhvcSBAGEtwkAAQH/BAAw\n", "DQYJKoZIhvcNAQELBQADggEBACTcpQC8jXL12JN5YzOcQ64ubQIe0XxRAd30p7qB\n", "BTXGpgqBjrjxRfLms7EBYodEXB2oXMsDq3km0vT1MfYdsDD05S+SQ9CDsq/pUfaC\n", "E2WNI5p8WircRnroYvbN2vkjlRbMd1+yNITohXYXCJwjEOAWOx3XIM10bwPYBv4R\n", "rDobuLHoMgL3yHgMHmAkP7YpkBucNqeBV8cCdeAZLuhXFWi6yfr3r/X18yWbC/r2\n", "2xXdkrSqXLFo7ToyP8YKTgiXpya4x6m53biEYwa2ULlas0igL6DK7wjYZX95Uy7H\n", "GKljn9weIYiMPV/BzGymwfv2EW0preLwtyJNJPaxbdin6Jc=\n", "-----END X509 CRL-----\n", NULL }; static const char **unknown_critical_crls[] = { kUnknownCriticalCRL, kUnknownCriticalCRL2 }; static X509 *test_root = NULL; static X509 *test_leaf = NULL; static BIO *glue2bio(const char **pem, char **out) { size_t s = 0; *out = glue_strings(pem, &s); return BIO_new_mem_buf(*out, s); } static X509_CRL *CRL_from_strings(const char **pem) { X509_CRL *crl; char *p; BIO *b = glue2bio(pem, &p); if (b == NULL) { OPENSSL_free(p); return NULL; } crl = PEM_read_bio_X509_CRL(b, NULL, NULL, NULL); OPENSSL_free(p); BIO_free(b); return crl; } static X509 *X509_from_strings(const char **pem) { X509 *x; char *p; BIO *b = glue2bio(pem, &p); if (b == NULL) { OPENSSL_free(p); return NULL; } x = PEM_read_bio_X509(b, NULL, NULL, NULL); OPENSSL_free(p); BIO_free(b); return x; } static int verify(X509 *leaf, X509 *root, STACK_OF(X509_CRL) *crls, unsigned long flags) { X509_STORE_CTX *ctx = X509_STORE_CTX_new(); X509_STORE *store = X509_STORE_new(); X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new(); STACK_OF(X509) *roots = sk_X509_new_null(); int status = X509_V_ERR_UNSPECIFIED; if (!TEST_ptr(ctx) || !TEST_ptr(store) || !TEST_ptr(param) || !TEST_ptr(roots)) goto err; X509_up_ref(root); if (!TEST_true(sk_X509_push(roots, root)) || !TEST_true(X509_STORE_CTX_init(ctx, store, leaf, NULL))) goto err; X509_STORE_CTX_set0_trusted_stack(ctx, roots); X509_STORE_CTX_set0_crls(ctx, crls); X509_VERIFY_PARAM_set_time(param, PARAM_TIME); if (!TEST_long_eq((long)X509_VERIFY_PARAM_get_time(param), PARAM_TIME)) goto err; X509_VERIFY_PARAM_set_depth(param, 16); if (flags) X509_VERIFY_PARAM_set_flags(param, flags); X509_STORE_CTX_set0_param(ctx, param); param = NULL; ERR_clear_error(); status = X509_verify_cert(ctx) == 1 ? X509_V_OK : X509_STORE_CTX_get_error(ctx); err: OSSL_STACK_OF_X509_free(roots); sk_X509_CRL_pop_free(crls, X509_CRL_free); X509_VERIFY_PARAM_free(param); X509_STORE_CTX_free(ctx); X509_STORE_free(store); return status; } static STACK_OF(X509_CRL) *make_CRL_stack(X509_CRL *x1, X509_CRL *x2) { STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); sk_X509_CRL_push(sk, x1); X509_CRL_up_ref(x1); if (x2 != NULL) { sk_X509_CRL_push(sk, x2); X509_CRL_up_ref(x2); } return sk; } static int test_basic_crl(void) { X509_CRL *basic_crl = CRL_from_strings(kBasicCRL); X509_CRL *revoked_crl = CRL_from_strings(kRevokedCRL); int r; r = TEST_ptr(basic_crl) && TEST_ptr(revoked_crl) && TEST_int_eq(verify(test_leaf, test_root, make_CRL_stack(basic_crl, NULL), X509_V_FLAG_CRL_CHECK), X509_V_OK) && TEST_int_eq(verify(test_leaf, test_root, make_CRL_stack(basic_crl, revoked_crl), X509_V_FLAG_CRL_CHECK), X509_V_ERR_CERT_REVOKED); X509_CRL_free(basic_crl); X509_CRL_free(revoked_crl); return r; } static int test_no_crl(void) { return TEST_int_eq(verify(test_leaf, test_root, NULL, X509_V_FLAG_CRL_CHECK), X509_V_ERR_UNABLE_TO_GET_CRL); } static int test_bad_issuer_crl(void) { X509_CRL *bad_issuer_crl = CRL_from_strings(kBadIssuerCRL); int r; r = TEST_ptr(bad_issuer_crl) && TEST_int_eq(verify(test_leaf, test_root, make_CRL_stack(bad_issuer_crl, NULL), X509_V_FLAG_CRL_CHECK), X509_V_ERR_UNABLE_TO_GET_CRL); X509_CRL_free(bad_issuer_crl); return r; } static int test_known_critical_crl(void) { X509_CRL *known_critical_crl = CRL_from_strings(kKnownCriticalCRL); int r; r = TEST_ptr(known_critical_crl) && TEST_int_eq(verify(test_leaf, test_root, make_CRL_stack(known_critical_crl, NULL), X509_V_FLAG_CRL_CHECK), X509_V_OK); X509_CRL_free(known_critical_crl); return r; } static int test_unknown_critical_crl(int n) { X509_CRL *unknown_critical_crl = CRL_from_strings(unknown_critical_crls[n]); int r; r = TEST_ptr(unknown_critical_crl) && TEST_int_eq(verify(test_leaf, test_root, make_CRL_stack(unknown_critical_crl, NULL), X509_V_FLAG_CRL_CHECK), X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION); X509_CRL_free(unknown_critical_crl); return r; } static int test_reuse_crl(void) { X509_CRL *reused_crl = CRL_from_strings(kBasicCRL); char *p; BIO *b = glue2bio(kRevokedCRL, &p); if (b == NULL) { OPENSSL_free(p); X509_CRL_free(reused_crl); return 0; } reused_crl = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL); OPENSSL_free(p); BIO_free(b); X509_CRL_free(reused_crl); return 1; } int setup_tests(void) { if (!TEST_ptr(test_root = X509_from_strings(kCRLTestRoot)) || !TEST_ptr(test_leaf = X509_from_strings(kCRLTestLeaf))) return 0; ADD_TEST(test_no_crl); ADD_TEST(test_basic_crl); ADD_TEST(test_bad_issuer_crl); ADD_TEST(test_known_critical_crl); ADD_ALL_TESTS(test_unknown_critical_crl, OSSL_NELEM(unknown_critical_crls)); ADD_TEST(test_reuse_crl); return 1; } void cleanup_tests(void) { X509_free(test_root); X509_free(test_leaf); }
test
openssl/test/crltest.c
openssl
#include <stdio.h> #include <string.h> #include "testutil.h" #include "crypto/poly1305.h" #include "internal/nelem.h" typedef struct { size_t size; const unsigned char data[1024]; } SIZED_DATA; typedef struct { SIZED_DATA input; SIZED_DATA key; SIZED_DATA expected; } TESTDATA; static TESTDATA tests[] = { { { 34, { 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x46, 0x6f, 0x72, 0x75, 0x6d, 0x20, 0x52, 0x65, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68, 0x20, 0x47, 0x72, 0x6f, 0x75, 0x70 } }, { 32, { 0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33, 0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8, 0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd, 0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b } }, { 16, { 0xa8, 0x06, 0x1d, 0xc1, 0x30, 0x51, 0x36, 0xc6, 0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01, 0x27, 0xa9 } } }, { { 2, { 0xf3, 0xf6 } }, { 32, { 0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b, 0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00, 0x58, 0x0b, 0x3b, 0x0f, 0x94, 0x47, 0xbb, 0x1e, 0x69, 0xd0, 0x95, 0xb5, 0x92, 0x8b, 0x6d, 0xbc } }, { 16, { 0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45, 0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde } } }, { { 0, { 0 } }, { 32, { 0xa0, 0xf3, 0x08, 0x00, 0x00, 0xf4, 0x64, 0x00, 0xd0, 0xc7, 0xe9, 0x07, 0x6c, 0x83, 0x44, 0x03, 0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7, 0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7 } }, { 16, { 0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7, 0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7 } } }, { { 32, { 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36 } }, { 32, { 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef } }, { 16, { 0x0e, 0xe1, 0xc1, 0x6b, 0xb7, 0x3f, 0x0f, 0x4f, 0xd1, 0x98, 0x81, 0x75, 0x3c, 0x01, 0xcd, 0xbe } } }, { { 63, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0x51, 0x54, 0xad, 0x0d, 0x2c, 0xb2, 0x6e, 0x01, 0x27, 0x4f, 0xc5, 0x11, 0x48, 0x49, 0x1f, 0x1b } }, }, { { 64, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66 } }, }, { { 48, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0x5b, 0x88, 0xd7, 0xf6, 0x22, 0x8b, 0x11, 0xe2, 0xe2, 0x85, 0x79, 0xa5, 0xc0, 0xc1, 0xf7, 0x61 } }, }, { { 96, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0xbb, 0xb6, 0x13, 0xb2, 0xb6, 0xd7, 0x53, 0xba, 0x07, 0x39, 0x5b, 0x91, 0x6a, 0xae, 0xce, 0x15 } }, }, { { 112, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0xc7, 0x94, 0xd7, 0x05, 0x7d, 0x17, 0x78, 0xc4, 0xbb, 0xee, 0x0a, 0x39, 0xb3, 0xd9, 0x73, 0x42 } }, }, { { 128, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0xff, 0xbc, 0xb9, 0xb3, 0x71, 0x42, 0x31, 0x52, 0xd7, 0xfc, 0xa5, 0xad, 0x04, 0x2f, 0xba, 0xa9 } }, }, { { 144, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36, 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0x06, 0x9e, 0xd6, 0xb8, 0xef, 0x0f, 0x20, 0x7b, 0x3e, 0x24, 0x3b, 0xb1, 0x01, 0x9f, 0xe6, 0x32 } }, }, { { 160, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36, 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66, 0x5b, 0x88, 0xd7, 0xf6, 0x22, 0x8b, 0x11, 0xe2, 0xe2, 0x85, 0x79, 0xa5, 0xc0, 0xc1, 0xf7, 0x61 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0xcc, 0xa3, 0x39, 0xd9, 0xa4, 0x5f, 0xa2, 0x36, 0x8c, 0x2c, 0x68, 0xb3, 0xa4, 0x17, 0x91, 0x33 } }, }, { { 288, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36, 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66, 0x5b, 0x88, 0xd7, 0xf6, 0x22, 0x8b, 0x11, 0xe2, 0xe2, 0x85, 0x79, 0xa5, 0xc0, 0xc1, 0xf7, 0x61, 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0x53, 0xf6, 0xe8, 0x28, 0xa2, 0xf0, 0xfe, 0x0e, 0xe8, 0x15, 0xbf, 0x0b, 0xd5, 0x84, 0x1a, 0x34 } }, }, { { 320, { 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36, 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66, 0x5b, 0x88, 0xd7, 0xf6, 0x22, 0x8b, 0x11, 0xe2, 0xe2, 0x85, 0x79, 0xa5, 0xc0, 0xc1, 0xf7, 0x61, 0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42, 0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79, 0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91, 0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2, 0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83, 0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09, 0x5c, 0x1b, 0xf9, 0xaf, 0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09, 0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08, 0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88, 0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef, 0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8, 0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24, 0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb, 0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36, 0x81, 0x20, 0x59, 0xa5, 0xda, 0x19, 0x86, 0x37, 0xca, 0xc7, 0xc4, 0xa6, 0x31, 0xbe, 0xe4, 0x66, 0x5b, 0x88, 0xd7, 0xf6, 0x22, 0x8b, 0x11, 0xe2, 0xe2, 0x85, 0x79, 0xa5, 0xc0, 0xc1, 0xf7, 0x61 } }, { 32, { 0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c, 0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07, 0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1, 0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57 } }, { 16, { 0xb8, 0x46, 0xd4, 0x4e, 0x9b, 0xbd, 0x53, 0xce, 0xdf, 0xfb, 0xfb, 0xb6, 0xb7, 0xfa, 0x49, 0x33 } }, }, { { 256, { 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, 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, 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, 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, 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, 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, 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, 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 } }, { 32, { 0xad, 0x62, 0x81, 0x07, 0xe8, 0x35, 0x1d, 0x0f, 0x2c, 0x23, 0x1a, 0x05, 0xdc, 0x4a, 0x41, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x07, 0x14, 0x5a, 0x4c, 0x02, 0xfe, 0x5f, 0xa3, 0x20, 0x36, 0xde, 0x68, 0xfa, 0xbe, 0x90, 0x66 } }, }, { { 252, { 0x84, 0x23, 0x64, 0xe1, 0x56, 0x33, 0x6c, 0x09, 0x98, 0xb9, 0x33, 0xa6, 0x23, 0x77, 0x26, 0x18, 0x0d, 0x9e, 0x3f, 0xdc, 0xbd, 0xe4, 0xcd, 0x5d, 0x17, 0x08, 0x0f, 0xc3, 0xbe, 0xb4, 0x96, 0x14, 0xd7, 0x12, 0x2c, 0x03, 0x74, 0x63, 0xff, 0x10, 0x4d, 0x73, 0xf1, 0x9c, 0x12, 0x70, 0x46, 0x28, 0xd4, 0x17, 0xc4, 0xc5, 0x4a, 0x3f, 0xe3, 0x0d, 0x3c, 0x3d, 0x77, 0x14, 0x38, 0x2d, 0x43, 0xb0, 0x38, 0x2a, 0x50, 0xa5, 0xde, 0xe5, 0x4b, 0xe8, 0x44, 0xb0, 0x76, 0xe8, 0xdf, 0x88, 0x20, 0x1a, 0x1c, 0xd4, 0x3b, 0x90, 0xeb, 0x21, 0x64, 0x3f, 0xa9, 0x6f, 0x39, 0xb5, 0x18, 0xaa, 0x83, 0x40, 0xc9, 0x42, 0xff, 0x3c, 0x31, 0xba, 0xf7, 0xc9, 0xbd, 0xbf, 0x0f, 0x31, 0xae, 0x3f, 0xa0, 0x96, 0xbf, 0x8c, 0x63, 0x03, 0x06, 0x09, 0x82, 0x9f, 0xe7, 0x2e, 0x17, 0x98, 0x24, 0x89, 0x0b, 0xc8, 0xe0, 0x8c, 0x31, 0x5c, 0x1c, 0xce, 0x2a, 0x83, 0x14, 0x4d, 0xbb, 0xff, 0x09, 0xf7, 0x4e, 0x3e, 0xfc, 0x77, 0x0b, 0x54, 0xd0, 0x98, 0x4a, 0x8f, 0x19, 0xb1, 0x47, 0x19, 0xe6, 0x36, 0x35, 0x64, 0x1d, 0x6b, 0x1e, 0xed, 0xf6, 0x3e, 0xfb, 0xf0, 0x80, 0xe1, 0x78, 0x3d, 0x32, 0x44, 0x54, 0x12, 0x11, 0x4c, 0x20, 0xde, 0x0b, 0x83, 0x7a, 0x0d, 0xfa, 0x33, 0xd6, 0xb8, 0x28, 0x25, 0xff, 0xf4, 0x4c, 0x9a, 0x70, 0xea, 0x54, 0xce, 0x47, 0xf0, 0x7d, 0xf6, 0x98, 0xe6, 0xb0, 0x33, 0x23, 0xb5, 0x30, 0x79, 0x36, 0x4a, 0x5f, 0xc3, 0xe9, 0xdd, 0x03, 0x43, 0x92, 0xbd, 0xde, 0x86, 0xdc, 0xcd, 0xda, 0x94, 0x32, 0x1c, 0x5e, 0x44, 0x06, 0x04, 0x89, 0x33, 0x6c, 0xb6, 0x5b, 0xf3, 0x98, 0x9c, 0x36, 0xf7, 0x28, 0x2c, 0x2f, 0x5d, 0x2b, 0x88, 0x2c, 0x17, 0x1e, 0x74 } }, { 32, { 0x95, 0xd5, 0xc0, 0x05, 0x50, 0x3e, 0x51, 0x0d, 0x8c, 0xd0, 0xaa, 0x07, 0x2c, 0x4a, 0x4d, 0x06, 0x6e, 0xab, 0xc5, 0x2d, 0x11, 0x65, 0x3d, 0xf4, 0x7f, 0xbf, 0x63, 0xab, 0x19, 0x8b, 0xcc, 0x26 } }, { 16, { 0xf2, 0x48, 0x31, 0x2e, 0x57, 0x8d, 0x9d, 0x58, 0xf8, 0xb7, 0xbb, 0x4d, 0x19, 0x10, 0x54, 0x31 } }, }, { { 208, { 0x24, 0x8a, 0xc3, 0x10, 0x85, 0xb6, 0xc2, 0xad, 0xaa, 0xa3, 0x82, 0x59, 0xa0, 0xd7, 0x19, 0x2c, 0x5c, 0x35, 0xd1, 0xbb, 0x4e, 0xf3, 0x9a, 0xd9, 0x4c, 0x38, 0xd1, 0xc8, 0x24, 0x79, 0xe2, 0xdd, 0x21, 0x59, 0xa0, 0x77, 0x02, 0x4b, 0x05, 0x89, 0xbc, 0x8a, 0x20, 0x10, 0x1b, 0x50, 0x6f, 0x0a, 0x1a, 0xd0, 0xbb, 0xab, 0x76, 0xe8, 0x3a, 0x83, 0xf1, 0xb9, 0x4b, 0xe6, 0xbe, 0xae, 0x74, 0xe8, 0x74, 0xca, 0xb6, 0x92, 0xc5, 0x96, 0x3a, 0x75, 0x43, 0x6b, 0x77, 0x61, 0x21, 0xec, 0x9f, 0x62, 0x39, 0x9a, 0x3e, 0x66, 0xb2, 0xd2, 0x27, 0x07, 0xda, 0xe8, 0x19, 0x33, 0xb6, 0x27, 0x7f, 0x3c, 0x85, 0x16, 0xbc, 0xbe, 0x26, 0xdb, 0xbd, 0x86, 0xf3, 0x73, 0x10, 0x3d, 0x7c, 0xf4, 0xca, 0xd1, 0x88, 0x8c, 0x95, 0x21, 0x18, 0xfb, 0xfb, 0xd0, 0xd7, 0xb4, 0xbe, 0xdc, 0x4a, 0xe4, 0x93, 0x6a, 0xff, 0x91, 0x15, 0x7e, 0x7a, 0xa4, 0x7c, 0x54, 0x44, 0x2e, 0xa7, 0x8d, 0x6a, 0xc2, 0x51, 0xd3, 0x24, 0xa0, 0xfb, 0xe4, 0x9d, 0x89, 0xcc, 0x35, 0x21, 0xb6, 0x6d, 0x16, 0xe9, 0xc6, 0x6a, 0x37, 0x09, 0x89, 0x4e, 0x4e, 0xb0, 0xa4, 0xee, 0xdc, 0x4a, 0xe1, 0x94, 0x68, 0xe6, 0x6b, 0x81, 0xf2, 0x71, 0x35, 0x1b, 0x1d, 0x92, 0x1e, 0xa5, 0x51, 0x04, 0x7a, 0xbc, 0xc6, 0xb8, 0x7a, 0x90, 0x1f, 0xde, 0x7d, 0xb7, 0x9f, 0xa1, 0x81, 0x8c, 0x11, 0x33, 0x6d, 0xbc, 0x07, 0x24, 0x4a, 0x40, 0xeb } }, { 32, { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0xbc, 0x93, 0x9b, 0xc5, 0x28, 0x14, 0x80, 0xfa, 0x99, 0xc6, 0xd6, 0x8c, 0x25, 0x8e, 0xc4, 0x2f } }, }, { { 0, { 0x00, } }, { 32, { 0xc8, 0xaf, 0xaa, 0xc3, 0x31, 0xee, 0x37, 0x2c, 0xd6, 0x08, 0x2d, 0xe1, 0x34, 0x94, 0x3b, 0x17, 0x47, 0x10, 0x13, 0x0e, 0x9f, 0x6f, 0xea, 0x8d, 0x72, 0x29, 0x38, 0x50, 0xa6, 0x67, 0xd8, 0x6c } }, { 16, { 0x47, 0x10, 0x13, 0x0e, 0x9f, 0x6f, 0xea, 0x8d, 0x72, 0x29, 0x38, 0x50, 0xa6, 0x67, 0xd8, 0x6c } }, }, { { 12, { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21 } }, { 32, { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x33, 0x32, 0x2d, 0x62, 0x79, 0x74, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x50, 0x6f, 0x6c, 0x79, 0x31, 0x33, 0x30, 0x35 } }, { 16, { 0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0 } }, }, { { 32, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 32, { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x33, 0x32, 0x2d, 0x62, 0x79, 0x74, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x50, 0x6f, 0x6c, 0x79, 0x31, 0x33, 0x30, 0x35 } }, { 16, { 0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07 } }, }, { { 128, { 0x89, 0xda, 0xb8, 0x0b, 0x77, 0x17, 0xc1, 0xdb, 0x5d, 0xb4, 0x37, 0x86, 0x0a, 0x3f, 0x70, 0x21, 0x8e, 0x93, 0xe1, 0xb8, 0xf4, 0x61, 0xfb, 0x67, 0x7f, 0x16, 0xf3, 0x5f, 0x6f, 0x87, 0xe2, 0xa9, 0x1c, 0x99, 0xbc, 0x3a, 0x47, 0xac, 0xe4, 0x76, 0x40, 0xcc, 0x95, 0xc3, 0x45, 0xbe, 0x5e, 0xcc, 0xa5, 0xa3, 0x52, 0x3c, 0x35, 0xcc, 0x01, 0x89, 0x3a, 0xf0, 0xb6, 0x4a, 0x62, 0x03, 0x34, 0x27, 0x03, 0x72, 0xec, 0x12, 0x48, 0x2d, 0x1b, 0x1e, 0x36, 0x35, 0x61, 0x69, 0x8a, 0x57, 0x8b, 0x35, 0x98, 0x03, 0x49, 0x5b, 0xb4, 0xe2, 0xef, 0x19, 0x30, 0xb1, 0x7a, 0x51, 0x90, 0xb5, 0x80, 0xf1, 0x41, 0x30, 0x0d, 0xf3, 0x0a, 0xdb, 0xec, 0xa2, 0x8f, 0x64, 0x27, 0xa8, 0xbc, 0x1a, 0x99, 0x9f, 0xd5, 0x1c, 0x55, 0x4a, 0x01, 0x7d, 0x09, 0x5d, 0x8c, 0x3e, 0x31, 0x27, 0xda, 0xf9, 0xf5, 0x95 } }, { 32, { 0x2d, 0x77, 0x3b, 0xe3, 0x7a, 0xdb, 0x1e, 0x4d, 0x68, 0x3b, 0xf0, 0x07, 0x5e, 0x79, 0xc4, 0xee, 0x03, 0x79, 0x18, 0x53, 0x5a, 0x7f, 0x99, 0xcc, 0xb7, 0x04, 0x0f, 0xb5, 0xf5, 0xf4, 0x3a, 0xea } }, { 16, { 0xc8, 0x5d, 0x15, 0xed, 0x44, 0xc3, 0x78, 0xd6, 0xb0, 0x0e, 0x23, 0x06, 0x4c, 0x7b, 0xcd, 0x51 } }, }, { { 528, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x17, 0x03, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0xdb, 0x1f, 0x1f, 0x36, 0x8d, 0x69, 0x6a, 0x81, 0x0a, 0x34, 0x9c, 0x0c, 0x71, 0x4c, 0x9a, 0x5e, 0x78, 0x50, 0xc2, 0x40, 0x7d, 0x72, 0x1a, 0xcd, 0xed, 0x95, 0xe0, 0x18, 0xd7, 0xa8, 0x52, 0x66, 0xa6, 0xe1, 0x28, 0x9c, 0xdb, 0x4a, 0xeb, 0x18, 0xda, 0x5a, 0xc8, 0xa2, 0xb0, 0x02, 0x6d, 0x24, 0xa5, 0x9a, 0xd4, 0x85, 0x22, 0x7f, 0x3e, 0xae, 0xdb, 0xb2, 0xe7, 0xe3, 0x5e, 0x1c, 0x66, 0xcd, 0x60, 0xf9, 0xab, 0xf7, 0x16, 0xdc, 0xc9, 0xac, 0x42, 0x68, 0x2d, 0xd7, 0xda, 0xb2, 0x87, 0xa7, 0x02, 0x4c, 0x4e, 0xef, 0xc3, 0x21, 0xcc, 0x05, 0x74, 0xe1, 0x67, 0x93, 0xe3, 0x7c, 0xec, 0x03, 0xc5, 0xbd, 0xa4, 0x2b, 0x54, 0xc1, 0x14, 0xa8, 0x0b, 0x57, 0xaf, 0x26, 0x41, 0x6c, 0x7b, 0xe7, 0x42, 0x00, 0x5e, 0x20, 0x85, 0x5c, 0x73, 0xe2, 0x1d, 0xc8, 0xe2, 0xed, 0xc9, 0xd4, 0x35, 0xcb, 0x6f, 0x60, 0x59, 0x28, 0x00, 0x11, 0xc2, 0x70, 0xb7, 0x15, 0x70, 0x05, 0x1c, 0x1c, 0x9b, 0x30, 0x52, 0x12, 0x66, 0x20, 0xbc, 0x1e, 0x27, 0x30, 0xfa, 0x06, 0x6c, 0x7a, 0x50, 0x9d, 0x53, 0xc6, 0x0e, 0x5a, 0xe1, 0xb4, 0x0a, 0xa6, 0xe3, 0x9e, 0x49, 0x66, 0x92, 0x28, 0xc9, 0x0e, 0xec, 0xb4, 0xa5, 0x0d, 0xb3, 0x2a, 0x50, 0xbc, 0x49, 0xe9, 0x0b, 0x4f, 0x4b, 0x35, 0x9a, 0x1d, 0xfd, 0x11, 0x74, 0x9c, 0xd3, 0x86, 0x7f, 0xcf, 0x2f, 0xb7, 0xbb, 0x6c, 0xd4, 0x73, 0x8f, 0x6a, 0x4a, 0xd6, 0xf7, 0xca, 0x50, 0x58, 0xf7, 0x61, 0x88, 0x45, 0xaf, 0x9f, 0x02, 0x0f, 0x6c, 0x3b, 0x96, 0x7b, 0x8f, 0x4c, 0xd4, 0xa9, 0x1e, 0x28, 0x13, 0xb5, 0x07, 0xae, 0x66, 0xf2, 0xd3, 0x5c, 0x18, 0x28, 0x4f, 0x72, 0x92, 0x18, 0x60, 0x62, 0xe1, 0x0f, 0xd5, 0x51, 0x0d, 0x18, 0x77, 0x53, 0x51, 0xef, 0x33, 0x4e, 0x76, 0x34, 0xab, 0x47, 0x43, 0xf5, 0xb6, 0x8f, 0x49, 0xad, 0xca, 0xb3, 0x84, 0xd3, 0xfd, 0x75, 0xf7, 0x39, 0x0f, 0x40, 0x06, 0xef, 0x2a, 0x29, 0x5c, 0x8c, 0x7a, 0x07, 0x6a, 0xd5, 0x45, 0x46, 0xcd, 0x25, 0xd2, 0x10, 0x7f, 0xbe, 0x14, 0x36, 0xc8, 0x40, 0x92, 0x4a, 0xae, 0xbe, 0x5b, 0x37, 0x08, 0x93, 0xcd, 0x63, 0xd1, 0x32, 0x5b, 0x86, 0x16, 0xfc, 0x48, 0x10, 0x88, 0x6b, 0xc1, 0x52, 0xc5, 0x32, 0x21, 0xb6, 0xdf, 0x37, 0x31, 0x19, 0x39, 0x32, 0x55, 0xee, 0x72, 0xbc, 0xaa, 0x88, 0x01, 0x74, 0xf1, 0x71, 0x7f, 0x91, 0x84, 0xfa, 0x91, 0x64, 0x6f, 0x17, 0xa2, 0x4a, 0xc5, 0x5d, 0x16, 0xbf, 0xdd, 0xca, 0x95, 0x81, 0xa9, 0x2e, 0xda, 0x47, 0x92, 0x01, 0xf0, 0xed, 0xbf, 0x63, 0x36, 0x00, 0xd6, 0x06, 0x6d, 0x1a, 0xb3, 0x6d, 0x5d, 0x24, 0x15, 0xd7, 0x13, 0x51, 0xbb, 0xcd, 0x60, 0x8a, 0x25, 0x10, 0x8d, 0x25, 0x64, 0x19, 0x92, 0xc1, 0xf2, 0x6c, 0x53, 0x1c, 0xf9, 0xf9, 0x02, 0x03, 0xbc, 0x4c, 0xc1, 0x9f, 0x59, 0x27, 0xd8, 0x34, 0xb0, 0xa4, 0x71, 0x16, 0xd3, 0x88, 0x4b, 0xbb, 0x16, 0x4b, 0x8e, 0xc8, 0x83, 0xd1, 0xac, 0x83, 0x2e, 0x56, 0xb3, 0x91, 0x8a, 0x98, 0x60, 0x1a, 0x08, 0xd1, 0x71, 0x88, 0x15, 0x41, 0xd5, 0x94, 0xdb, 0x39, 0x9c, 0x6a, 0xe6, 0x15, 0x12, 0x21, 0x74, 0x5a, 0xec, 0x81, 0x4c, 0x45, 0xb0, 0xb0, 0x5b, 0x56, 0x54, 0x36, 0xfd, 0x6f, 0x13, 0x7a, 0xa1, 0x0a, 0x0c, 0x0b, 0x64, 0x37, 0x61, 0xdb, 0xd6, 0xf9, 0xa9, 0xdc, 0xb9, 0x9b, 0x1a, 0x6e, 0x69, 0x08, 0x54, 0xce, 0x07, 0x69, 0xcd, 0xe3, 0x97, 0x61, 0xd8, 0x2f, 0xcd, 0xec, 0x15, 0xf0, 0xd9, 0x2d, 0x7d, 0x8e, 0x94, 0xad, 0xe8, 0xeb, 0x83, 0xfb, 0xe0 } }, { 32, { 0x99, 0xe5, 0x82, 0x2d, 0xd4, 0x17, 0x3c, 0x99, 0x5e, 0x3d, 0xae, 0x0d, 0xde, 0xfb, 0x97, 0x74, 0x3f, 0xde, 0x3b, 0x08, 0x01, 0x34, 0xb3, 0x9f, 0x76, 0xe9, 0xbf, 0x8d, 0x0e, 0x88, 0xd5, 0x46 } }, { 16, { 0x26, 0x37, 0x40, 0x8f, 0xe1, 0x30, 0x86, 0xea, 0x73, 0xf9, 0x71, 0xe3, 0x42, 0x5e, 0x28, 0x20 } }, }, { { 257, { 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x80, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xce, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xc5, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xe3, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xac, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xe6, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x00, 0x00, 0x00, 0xaf, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xff, 0xff, 0xff, 0xf5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x92, 0x05, 0xa8, 0x52, 0x1d, 0xfc } }, { 32, { 0x7f, 0x1b, 0x02, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc } }, { 16, { 0x85, 0x59, 0xb8, 0x76, 0xec, 0xee, 0xd6, 0x6e, 0xb3, 0x77, 0x98, 0xc0, 0x45, 0x7b, 0xaf, 0xf9 } }, }, { { 39, { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x64 } }, { 32, { 0xe0, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa } }, { 16, { 0x00, 0xbd, 0x12, 0x58, 0x97, 0x8e, 0x20, 0x54, 0x44, 0xc9, 0xaa, 0xaa, 0x82, 0x00, 0x6f, 0xed } }, }, { { 2, { 0x02, 0xfc } }, { 32, { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c } }, { 16, { 0x06, 0x12, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c } }, }, { { 415, { 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7a, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x5c, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x6e, 0x7b, 0x00, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7a, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x5c, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x6e, 0x7b, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0xb3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0xef, 0xff, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0xb3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0xef, 0xff, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7a, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc } }, { 32, { 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x7b } }, { 16, { 0x33, 0x20, 0x5b, 0xbf, 0x9e, 0x9f, 0x8f, 0x72, 0x12, 0xab, 0x9e, 0x2a, 0xb9, 0xb7, 0xe4, 0xa5 } }, }, { { 118, { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0xff, 0xff, 0xff, 0xe9, 0xe9, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0x00, 0x00, 0xac, 0xac, 0xec, 0x01, 0x00, 0xac, 0xac, 0xac, 0x2c, 0xac, 0xa2, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0x64, 0xf2 } }, { 32, { 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x7f, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77 } }, { 16, { 0x02, 0xee, 0x7c, 0x8c, 0x54, 0x6d, 0xde, 0xb1, 0xa4, 0x67, 0xe4, 0xc3, 0x98, 0x11, 0x58, 0xb9 } }, }, { { 131, { 0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73, 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce, 0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4, 0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a, 0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b, 0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72, 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2, 0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38, 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7, 0xcc, 0x8a, 0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae, 0x90, 0x22, 0x43, 0x68, 0x51, 0x7a, 0xcf, 0xea, 0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda, 0x99, 0x83, 0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde, 0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3, 0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6, 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4, 0x5a, 0x74, 0xe3, 0x55, 0xa5 } }, { 32, { 0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d, 0x3c, 0x25, 0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4, 0xc8, 0xcf, 0xf8, 0x80 } }, { 16, { 0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9 } }, }, { { 16, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }, { 32, { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, }, { { 16, { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 32, { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }, { 16, { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, }, { { 48, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 32, { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, }, { { 48, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } }, { 32, { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, }, { { 16, { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }, { 32, { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }, }, { { 64, { 0xe3, 0x35, 0x94, 0xd7, 0x50, 0x5e, 0x43, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x94, 0xd7, 0x50, 0x5e, 0x43, 0x79, 0xcd, 0x01, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 32, { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, }, { { 48, { 0xe3, 0x35, 0x94, 0xd7, 0x50, 0x5e, 0x43, 0xb9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x94, 0xd7, 0x50, 0x5e, 0x43, 0x79, 0xcd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 32, { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, { 16, { 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } } }; static int test_poly1305(int idx) { POLY1305 poly1305; const TESTDATA test = tests[idx]; const unsigned char *in = test.input.data; size_t inlen = test.input.size; const unsigned char *key = test.key.data; const unsigned char *expected = test.expected.data; size_t expectedlen = test.expected.size; unsigned char out[16]; if (!TEST_size_t_eq(expectedlen, sizeof(out))) return 0; Poly1305_Init(&poly1305, key); Poly1305_Update(&poly1305, in, inlen); Poly1305_Final(&poly1305, out); if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("Poly1305 test #%d failed.", idx); return 0; } if (inlen > 16) { Poly1305_Init(&poly1305, key); Poly1305_Update(&poly1305, in, 1); Poly1305_Update(&poly1305, in+1, inlen-1); Poly1305_Final(&poly1305, out); if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("Poly1305 test #%d/1+(N-1) failed.", idx); return 0; } } if (inlen > 32) { size_t half = inlen / 2; Poly1305_Init(&poly1305, key); Poly1305_Update(&poly1305, in, half); Poly1305_Update(&poly1305, in+half, inlen-half); Poly1305_Final(&poly1305, out); if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("Poly1305 test #%d/2 failed.", idx); return 0; } for (half = 16; half < inlen; half += 16) { Poly1305_Init(&poly1305, key); Poly1305_Update(&poly1305, in, half); Poly1305_Update(&poly1305, in+half, inlen-half); Poly1305_Final(&poly1305, out); if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) { TEST_info("Poly1305 test #%d/%zu+%zu failed.", idx, half, inlen-half); return 0; } } } return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_poly1305, OSSL_NELEM(tests)); return 1; }
test
openssl/test/poly1305_internal_test.c
openssl
#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, 0xfe, 0xfd, 0, 1, 0, 0, 0, 0, 0, 0x0f, 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2, SSL3_MT_CERTIFICATE_STATUS, 0, 0, DUMMY_CERT_STATUS_LEN, 0, 5, 0, 0, 0, 0, 0, DUMMY_CERT_STATUS_LEN - 2, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; #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 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; 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; 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); 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; } #define CLI_TO_SRV_COOKIE_EXCH 1 #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 # 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) #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 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) { 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; 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); 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 (!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 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 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 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; } 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[] = { SSL3_RT_HANDSHAKE, (DTLS1_2_VERSION >> 8) & 0xff, DTLS1_2_VERSION & 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, SSL3_MT_FINISHED, 0, 0, SHA_DIGEST_LENGTH, 0, 0, 0, 0, 0, 0, 0, SHA_DIGEST_LENGTH, 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 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; if (!TEST_int_le(ret = SSL_accept(serverssl), 0)) goto end; 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; } 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 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; if (!TEST_int_le(SSL_connect(cssl), 0)) goto end; if (!TEST_int_le(SSL_accept(sssl), 0)) goto end; if (!TEST_int_le(SSL_connect(cssl), 0)) goto end; if (idx == 0) { bio = SSL_get_wbio(cssl); if (!TEST_ptr(bio) || !TEST_true(mempacket_swap_epoch(bio))) goto end; } if (!TEST_int_gt(SSL_accept(sssl), 0)) goto end; 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) { if (!TEST_true(mempacket_move_packet(bio, 0, 1))) goto end; } else if (idx == 2) { if (!TEST_true(mempacket_move_packet(bio, 0, 2))) goto end; } else if (idx == 3) { bio = SSL_get_wbio(sssl); if (!TEST_true(mempacket_move_packet(bio, 1, 2))) goto end; } if (!TEST_int_gt(SSL_connect(cssl), 0)) goto end; if (idx == 0 || idx == 1) { if (!TEST_int_eq(SSL_pending(cssl), 0) || !TEST_false(SSL_has_pending(cssl))) goto end; } else { if (!TEST_int_eq(SSL_pending(cssl), (int)sizeof(msg)) || !TEST_true(SSL_has_pending(cssl))) goto end; } 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; } 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 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); 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(); }
test
openssl/test/dtlstest.c
openssl
#include "internal/deprecated.h" #include <string.h> #include <openssl/sha.h> #include <openssl/evp.h> #include <openssl/provider.h> #include "internal/sizes.h" #include "testutil.h" static char *config_file = NULL; static char *alg = "digest"; static int use_default_ctx = 0; static char *fetch_property = NULL; static int expected_fetch_result = 1; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_ALG_FETCH_TYPE, OPT_FETCH_PROPERTY, OPT_FETCH_FAILURE, OPT_USE_DEFAULTCTX, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[provname...]\n"), { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { "type", OPT_ALG_FETCH_TYPE, 's', "The fetch type to test" }, { "property", OPT_FETCH_PROPERTY, 's', "The fetch property e.g. provider=fips" }, { "fetchfail", OPT_FETCH_FAILURE, '-', "fetch is expected to fail" }, { "defaultctx", OPT_USE_DEFAULTCTX, '-', "Use the default context if this is set" }, { OPT_HELP_STR, 1, '-', "file\tProvider names to explicitly load\n" }, { NULL } }; return test_options; } static int calculate_digest(const EVP_MD *md, const char *msg, size_t len, const unsigned char *exptd) { unsigned char out[SHA256_DIGEST_LENGTH]; EVP_MD_CTX *ctx; int ret = 0; if (!TEST_ptr(ctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestInit_ex(ctx, md, NULL)) || !TEST_true(EVP_DigestUpdate(ctx, msg, len)) || !TEST_true(EVP_DigestFinal_ex(ctx, out, NULL)) || !TEST_mem_eq(out, SHA256_DIGEST_LENGTH, exptd, SHA256_DIGEST_LENGTH) || !TEST_true(md == EVP_MD_CTX_get0_md(ctx))) goto err; ret = 1; err: EVP_MD_CTX_free(ctx); return ret; } static int load_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[]) { OSSL_LIB_CTX *ctx = NULL; int ret = 0; size_t i; ctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(ctx)) goto err; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, config_file))) goto err; if (test_get_argument_count() > 2) goto err; for (i = 0; i < test_get_argument_count(); ++i) { char *provname = test_get_argument(i); prov[i] = OSSL_PROVIDER_load(ctx, provname); if (!TEST_ptr(prov[i])) goto err; } ret = 1; *libctx = ctx; err: if (ret == 0) OSSL_LIB_CTX_free(ctx); return ret; } static void unload_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[]) { if (prov[0] != NULL) OSSL_PROVIDER_unload(prov[0]); if (prov[1] != NULL) OSSL_PROVIDER_unload(prov[1]); if (libctx != NULL && *libctx != NULL) { OPENSSL_thread_stop_ex(*libctx); OSSL_LIB_CTX_free(*libctx); } } static int test_legacy_provider_unloaded(void) { OSSL_LIB_CTX *ctx = NULL; int rc = 0; ctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(ctx)) goto err; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, config_file))) goto err; if (!TEST_int_eq(OSSL_PROVIDER_available(ctx, "legacy"), 0)) goto err; rc = 1; err: OSSL_LIB_CTX_free(ctx); return rc; } static X509_ALGOR *make_algor(int nid) { X509_ALGOR *algor; if (!TEST_ptr(algor = X509_ALGOR_new()) || !TEST_true(X509_ALGOR_set0(algor, OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL))) { X509_ALGOR_free(algor); return NULL; } return algor; } static int test_md(const EVP_MD *md) { const char testmsg[] = "Hello world"; const unsigned char exptd[] = { 0x27, 0x51, 0x8b, 0xa9, 0x68, 0x30, 0x11, 0xf6, 0xb3, 0x96, 0x07, 0x2c, 0x05, 0xf6, 0x65, 0x6d, 0x04, 0xf5, 0xfb, 0xc3, 0x78, 0x7c, 0xf9, 0x24, 0x90, 0xec, 0x60, 0x6e, 0x50, 0x92, 0xe3, 0x26 }; return TEST_ptr(md) && TEST_true(EVP_MD_is_a(md, "SHA256")) && TEST_true(calculate_digest(md, testmsg, sizeof(testmsg), exptd)) && TEST_int_eq(EVP_MD_get_size(md), SHA256_DIGEST_LENGTH) && TEST_int_eq(EVP_MD_get_block_size(md), SHA256_CBLOCK); } static int test_implicit_EVP_MD_fetch(void) { OSSL_LIB_CTX *ctx = NULL; OSSL_PROVIDER *prov[2] = {NULL, NULL}; int ret = 0; ret = (use_default_ctx == 0 || load_providers(&ctx, prov)) && test_md(EVP_sha256()); unload_providers(&ctx, prov); return ret; } static int test_explicit_EVP_MD_fetch(const char *id) { OSSL_LIB_CTX *ctx = NULL; EVP_MD *md = NULL; OSSL_PROVIDER *prov[2] = {NULL, NULL}; int ret = 0; if (use_default_ctx == 0 && !load_providers(&ctx, prov)) goto err; md = EVP_MD_fetch(ctx, id, fetch_property); if (expected_fetch_result != 0) { if (!test_md(md)) goto err; if (!TEST_true(EVP_MD_up_ref(md))) goto err; EVP_MD_free(md); } else { if (!TEST_ptr_null(md)) goto err; } ret = 1; err: EVP_MD_free(md); unload_providers(&ctx, prov); return ret; } static int test_explicit_EVP_MD_fetch_by_name(void) { return test_explicit_EVP_MD_fetch("SHA256"); } static int test_explicit_EVP_MD_fetch_by_X509_ALGOR(int idx) { int ret = 0; X509_ALGOR *algor = make_algor(NID_sha256); const ASN1_OBJECT *obj; char id[OSSL_MAX_NAME_SIZE] = { 0 }; if (algor == NULL) return 0; X509_ALGOR_get0(&obj, NULL, NULL, algor); switch (idx) { case 0: if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0)) goto end; break; case 1: if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0)) goto end; break; } ret = test_explicit_EVP_MD_fetch(id); end: X509_ALGOR_free(algor); return ret; } static int encrypt_decrypt(const EVP_CIPHER *cipher, const unsigned char *msg, size_t len) { int ret = 0, ctlen, ptlen; EVP_CIPHER_CTX *ctx = NULL; unsigned char key[128 / 8]; unsigned char ct[64], pt[64]; memset(key, 0, sizeof(key)); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 1)) || !TEST_true(EVP_CipherUpdate(ctx, ct, &ctlen, msg, len)) || !TEST_true(EVP_CipherFinal_ex(ctx, ct, &ctlen)) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 0)) || !TEST_true(EVP_CipherUpdate(ctx, pt, &ptlen, ct, ctlen)) || !TEST_true(EVP_CipherFinal_ex(ctx, pt, &ptlen)) || !TEST_mem_eq(pt, ptlen, msg, len)) goto err; ret = 1; err: EVP_CIPHER_CTX_free(ctx); return ret; } static int test_cipher(const EVP_CIPHER *cipher) { const unsigned char testmsg[] = "Hello world"; return TEST_ptr(cipher) && TEST_true(encrypt_decrypt(cipher, testmsg, sizeof(testmsg))); } static int test_implicit_EVP_CIPHER_fetch(void) { OSSL_LIB_CTX *ctx = NULL; OSSL_PROVIDER *prov[2] = {NULL, NULL}; int ret = 0; ret = (use_default_ctx == 0 || load_providers(&ctx, prov)) && test_cipher(EVP_aes_128_cbc()); unload_providers(&ctx, prov); return ret; } static int test_explicit_EVP_CIPHER_fetch(const char *id) { OSSL_LIB_CTX *ctx = NULL; EVP_CIPHER *cipher = NULL; OSSL_PROVIDER *prov[2] = {NULL, NULL}; int ret = 0; if (use_default_ctx == 0 && !load_providers(&ctx, prov)) goto err; cipher = EVP_CIPHER_fetch(ctx, id, fetch_property); if (expected_fetch_result != 0) { if (!test_cipher(cipher)) goto err; if (!TEST_true(EVP_CIPHER_up_ref(cipher))) goto err; EVP_CIPHER_free(cipher); } else { if (!TEST_ptr_null(cipher)) goto err; } ret = 1; err: EVP_CIPHER_free(cipher); unload_providers(&ctx, prov); return ret; } static int test_explicit_EVP_CIPHER_fetch_by_name(void) { return test_explicit_EVP_CIPHER_fetch("AES-128-CBC"); } static int test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR(int idx) { int ret = 0; X509_ALGOR *algor = make_algor(NID_aes_128_cbc); const ASN1_OBJECT *obj; char id[OSSL_MAX_NAME_SIZE] = { 0 }; if (algor == NULL) return 0; X509_ALGOR_get0(&obj, NULL, NULL, algor); switch (idx) { case 0: if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0)) goto end; break; case 1: if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0)) goto end; break; } ret = test_explicit_EVP_CIPHER_fetch(id); end: X509_ALGOR_free(algor); return ret; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_ALG_FETCH_TYPE: alg = opt_arg(); break; case OPT_FETCH_PROPERTY: fetch_property = opt_arg(); break; case OPT_FETCH_FAILURE: expected_fetch_result = 0; break; case OPT_USE_DEFAULTCTX: use_default_ctx = 1; break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } ADD_TEST(test_legacy_provider_unloaded); if (strcmp(alg, "digest") == 0) { ADD_TEST(test_implicit_EVP_MD_fetch); ADD_TEST(test_explicit_EVP_MD_fetch_by_name); ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_MD_fetch_by_X509_ALGOR, 2); } else { ADD_TEST(test_implicit_EVP_CIPHER_fetch); ADD_TEST(test_explicit_EVP_CIPHER_fetch_by_name); ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR, 2); } return 1; }
test
openssl/test/evp_fetch_prov_test.c
openssl
#include <stddef.h> #include <openssl/provider.h> #include <openssl/param_build.h> #include "testutil.h" extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME; static char buf[256]; static OSSL_PARAM greeting_request[] = { { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf) }, { NULL, 0, NULL, 0, 0 } }; static unsigned int digestsuccess = 0; static OSSL_PARAM digest_check[] = { { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, &digestsuccess, sizeof(digestsuccess) }, { NULL, 0, NULL, 0, 0 } }; static unsigned int stopsuccess = 0; static OSSL_PARAM stop_property_mirror[] = { { "stop-property-mirror", OSSL_PARAM_UNSIGNED_INTEGER, &stopsuccess, sizeof(stopsuccess) }, { NULL, 0, NULL, 0, 0 } }; static int test_provider(OSSL_LIB_CTX **libctx, const char *name, OSSL_PROVIDER *legacy) { OSSL_PROVIDER *prov = NULL; const char *greeting = NULL; char expected_greeting[256]; int ok = 0; long err; int dolegacycheck = (legacy != NULL); OSSL_PROVIDER *deflt = NULL, *base = NULL; BIO_snprintf(expected_greeting, sizeof(expected_greeting), "Hello OpenSSL %.20s, greetings from %s!", OPENSSL_VERSION_STR, name); EVP_set_default_properties(*libctx, "fips=yes"); if (!TEST_ptr(base = OSSL_PROVIDER_load(*libctx, "base"))) goto err; if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name))) goto err; EVP_set_default_properties(*libctx, ""); if (dolegacycheck) { if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check)) || !TEST_true(digestsuccess)) goto err; if (!TEST_true(OSSL_PROVIDER_get_params(prov, stop_property_mirror)) || !TEST_true(stopsuccess)) goto err; EVP_set_default_properties(*libctx, "fips=yes"); if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check)) || !TEST_true(digestsuccess)) goto err; EVP_set_default_properties(*libctx, ""); } if (!TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request)) || !TEST_ptr(greeting = greeting_request[0].data) || !TEST_size_t_gt(greeting_request[0].data_size, 0) || !TEST_str_eq(greeting, expected_greeting)) goto err; err = ERR_peek_last_error(); if (!TEST_int_gt(err, 0) || !TEST_int_eq(ERR_GET_REASON(err), 1)) goto err; OSSL_PROVIDER_unload(legacy); legacy = NULL; if (dolegacycheck) { if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check)) || !TEST_false(digestsuccess)) goto err; legacy = OSSL_PROVIDER_load(*libctx, "legacy"); deflt = OSSL_PROVIDER_load(*libctx, "default"); if (!TEST_ptr(deflt) || !TEST_true(OSSL_PROVIDER_available(*libctx, "default"))) goto err; OSSL_PROVIDER_unload(deflt); deflt = NULL; if (!TEST_ptr(legacy) || !TEST_false(OSSL_PROVIDER_available(*libctx, "default")) || !TEST_true(OSSL_PROVIDER_get_params(prov, digest_check)) || !TEST_true(digestsuccess)) goto err; OSSL_PROVIDER_unload(legacy); legacy = NULL; } if (!TEST_true(OSSL_PROVIDER_unload(base))) goto err; base = NULL; if (!TEST_true(OSSL_PROVIDER_unload(prov))) goto err; prov = NULL; OSSL_LIB_CTX_free(*libctx); *libctx = NULL; ERR_print_errors_fp(stderr); ok = 1; err: OSSL_PROVIDER_unload(base); OSSL_PROVIDER_unload(deflt); OSSL_PROVIDER_unload(legacy); legacy = NULL; OSSL_PROVIDER_unload(prov); OSSL_LIB_CTX_free(*libctx); *libctx = NULL; return ok; } #ifndef NO_PROVIDER_MODULE static int test_provider_ex(OSSL_LIB_CTX **libctx, const char *name) { OSSL_PROVIDER *prov = NULL; const char *greeting = NULL; int ok = 0; long err; const char custom_buf[] = "Custom greeting"; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "greeting", custom_buf, strlen(custom_buf))) || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) { goto err; } if (!TEST_ptr(prov = OSSL_PROVIDER_load_ex(*libctx, name, params))) goto err; if (!TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request)) || !TEST_ptr(greeting = greeting_request[0].data) || !TEST_size_t_gt(greeting_request[0].data_size, 0) || !TEST_str_eq(greeting, custom_buf)) goto err; err = ERR_peek_last_error(); if (!TEST_int_gt(err, 0) || !TEST_int_eq(ERR_GET_REASON(err), 1)) goto err; if (!TEST_true(OSSL_PROVIDER_unload(prov))) goto err; prov = NULL; OSSL_LIB_CTX_free(*libctx); *libctx = NULL; ERR_print_errors_fp(stderr); ok = 1; err: OSSL_PARAM_BLD_free(bld); OSSL_PARAM_free(params); OSSL_PROVIDER_unload(prov); OSSL_LIB_CTX_free(*libctx); *libctx = NULL; return ok; } #endif static int test_builtin_provider(void) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); const char *name = "p_test_builtin"; int ok; ok = TEST_ptr(libctx) && TEST_true(OSSL_PROVIDER_add_builtin(libctx, name, PROVIDER_INIT_FUNCTION_NAME)) && test_provider(&libctx, name, NULL); OSSL_LIB_CTX_free(libctx); return ok; } #ifndef OPENSSL_NO_MD4 static int test_builtin_provider_with_child(void) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); const char *name = "p_test"; OSSL_PROVIDER *legacy; if (!TEST_ptr(libctx)) return 0; legacy = OSSL_PROVIDER_load(libctx, "legacy"); if (legacy == NULL) { OSSL_LIB_CTX_free(libctx); return 1; } if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, name, PROVIDER_INIT_FUNCTION_NAME))) { OSSL_LIB_CTX_free(libctx); return 0; } return test_provider(&libctx, name, legacy); } #endif #ifndef NO_PROVIDER_MODULE static int test_loaded_provider(void) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); const char *name = "p_test"; int res = 0; if (!TEST_ptr(libctx)) return 0; res = test_provider(&libctx, name, NULL); libctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(libctx)) return 0; res = res && test_provider_ex(&libctx, name); return res; } #endif typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_LOADED, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "loaded", OPT_LOADED, '-', "Run test with a loaded provider" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; int loaded = 0; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_TEST_CASES: break; case OPT_LOADED: loaded = 1; break; default: return 0; } } if (!loaded) { ADD_TEST(test_builtin_provider); #ifndef OPENSSL_NO_MD4 ADD_TEST(test_builtin_provider_with_child); #endif } #ifndef NO_PROVIDER_MODULE else { ADD_TEST(test_loaded_provider); } #endif return 1; }
test
openssl/test/provider_test.c
openssl
#ifndef OPENSSL_NO_DEPRECATED_3_0 # define OPENSSL_SUPPRESS_DEPRECATED #endif #if defined(_WIN32) # include <windows.h> #endif #include <string.h> #include <openssl/crypto.h> #include <openssl/rsa.h> #include <openssl/aes.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/pem.h> #include <openssl/evp.h> #include "internal/tsan_assist.h" #include "internal/nelem.h" #include "internal/time.h" #include "internal/rcu.h" #include "testutil.h" #include "threadstest.h" #ifdef __SANITIZE_THREAD__ #include <sanitizer/tsan_interface.h> #define TSAN_ACQUIRE(s) __tsan_acquire(s) #else #define TSAN_ACQUIRE(s) #endif #define MAXIMUM_THREADS 10 #define MAXIMUM_PROVIDERS 4 static int do_fips = 0; static char *privkey; static char *config_file = NULL; static int multidefault_run = 0; static const char *default_provider[] = { "default", NULL }; static const char *fips_provider[] = { "fips", NULL }; static const char *fips_and_default_providers[] = { "default", "fips", NULL }; static CRYPTO_RWLOCK *global_lock; #ifdef TSAN_REQUIRES_LOCKING static CRYPTO_RWLOCK *tsan_lock; #endif static int get_new_uid(void) { static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2); #ifdef TSAN_REQUIRES_LOCKING int r; if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock))) return 0; r = ++current_uid; if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock))) return 0; return r; #else return tsan_counter(&current_uid); #endif } static int test_lock(void) { CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new(); int res; res = TEST_true(CRYPTO_THREAD_read_lock(lock)) && TEST_true(CRYPTO_THREAD_unlock(lock)) && TEST_true(CRYPTO_THREAD_write_lock(lock)) && TEST_true(CRYPTO_THREAD_unlock(lock)); CRYPTO_THREAD_lock_free(lock); return res; } #if defined(OPENSSL_THREADS) static int contention = 0; static int rwwriter1_done = 0; static int rwwriter2_done = 0; static int rwreader1_iterations = 0; static int rwreader2_iterations = 0; static int rwwriter1_iterations = 0; static int rwwriter2_iterations = 0; static int *rwwriter_ptr = NULL; static int rw_torture_result = 1; static CRYPTO_RWLOCK *rwtorturelock = NULL; static void rwwriter_fn(int id, int *iterations) { int count; int *old, *new; OSSL_TIME t1, t2; t1 = ossl_time_now(); for (count = 0; ; count++) { new = CRYPTO_zalloc(sizeof (int), NULL, 0); if (contention == 0) OSSL_sleep(1000); if (!CRYPTO_THREAD_write_lock(rwtorturelock)) abort(); if (rwwriter_ptr != NULL) { *new = *rwwriter_ptr + 1; } else { *new = 0; } old = rwwriter_ptr; rwwriter_ptr = new; if (!CRYPTO_THREAD_unlock(rwtorturelock)) abort(); if (old != NULL) CRYPTO_free(old, __FILE__, __LINE__); t2 = ossl_time_now(); if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4) break; } *iterations = count; return; } static void rwwriter1_fn(void) { int local; TEST_info("Starting writer1"); rwwriter_fn(1, &rwwriter1_iterations); CRYPTO_atomic_add(&rwwriter1_done, 1, &local, NULL); } static void rwwriter2_fn(void) { int local; TEST_info("Starting writer 2"); rwwriter_fn(2, &rwwriter2_iterations); CRYPTO_atomic_add(&rwwriter2_done, 1, &local, NULL); } static void rwreader_fn(int *iterations) { unsigned int count = 0; int old = 0; int lw1 = 0; int lw2 = 0; if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0) abort(); while (lw1 != 1 || lw2 != 1) { CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, NULL); CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, NULL); count++; if (rwwriter_ptr != NULL && old > *rwwriter_ptr) { TEST_info("rwwriter pointer went backwards\n"); rw_torture_result = 0; } if (CRYPTO_THREAD_unlock(rwtorturelock) == 0) abort(); *iterations = count; if (rw_torture_result == 0) { *iterations = count; return; } if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0) abort(); } *iterations = count; if (CRYPTO_THREAD_unlock(rwtorturelock) == 0) abort(); } static void rwreader1_fn(void) { TEST_info("Starting reader 1"); rwreader_fn(&rwreader1_iterations); } static void rwreader2_fn(void) { TEST_info("Starting reader 2"); rwreader_fn(&rwreader2_iterations); } static thread_t rwwriter1; static thread_t rwwriter2; static thread_t rwreader1; static thread_t rwreader2; static int _torture_rw(void) { double tottime = 0; int ret = 0; double avr, avw; OSSL_TIME t1, t2; struct timeval dtime; rwtorturelock = CRYPTO_THREAD_lock_new(); rwwriter1_iterations = 0; rwwriter2_iterations = 0; rwreader1_iterations = 0; rwreader2_iterations = 0; rwwriter1_done = 0; rwwriter2_done = 0; rw_torture_result = 1; memset(&rwwriter1, 0, sizeof(thread_t)); memset(&rwwriter2, 0, sizeof(thread_t)); memset(&rwreader1, 0, sizeof(thread_t)); memset(&rwreader2, 0, sizeof(thread_t)); TEST_info("Staring rw torture"); t1 = ossl_time_now(); if (!TEST_true(run_thread(&rwreader1, rwreader1_fn)) || !TEST_true(run_thread(&rwreader2, rwreader2_fn)) || !TEST_true(run_thread(&rwwriter1, rwwriter1_fn)) || !TEST_true(run_thread(&rwwriter2, rwwriter2_fn)) || !TEST_true(wait_for_thread(rwwriter1)) || !TEST_true(wait_for_thread(rwwriter2)) || !TEST_true(wait_for_thread(rwreader1)) || !TEST_true(wait_for_thread(rwreader2))) goto out; t2 = ossl_time_now(); dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1)); tottime = dtime.tv_sec + (dtime.tv_usec / 1e6); TEST_info("rw_torture_result is %d\n", rw_torture_result); TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds", rwreader1_iterations + rwreader2_iterations, rwwriter1_iterations + rwwriter2_iterations, tottime); avr = tottime / (rwreader1_iterations + rwreader2_iterations); avw = (tottime / (rwwriter1_iterations + rwwriter2_iterations)); TEST_info("Average read time %e/read", avr); TEST_info("Averate write time %e/write", avw); if (TEST_int_eq(rw_torture_result, 1)) ret = 1; out: CRYPTO_THREAD_lock_free(rwtorturelock); rwtorturelock = NULL; return ret; } static int torture_rw_low(void) { contention = 0; return _torture_rw(); } static int torture_rw_high(void) { contention = 1; return _torture_rw(); } # ifndef OPENSSL_SYS_MACOSX static CRYPTO_RCU_LOCK *rcu_lock = NULL; static int writer1_done = 0; static int writer2_done = 0; static int reader1_iterations = 0; static int reader2_iterations = 0; static int writer1_iterations = 0; static int writer2_iterations = 0; static uint64_t *writer_ptr = NULL; static uint64_t global_ctr = 0; static int rcu_torture_result = 1; static void free_old_rcu_data(void *data) { CRYPTO_free(data, NULL, 0); } static void writer_fn(int id, int *iterations) { int count; OSSL_TIME t1, t2; uint64_t *old, *new; t1 = ossl_time_now(); for (count = 0; ; count++) { new = CRYPTO_zalloc(sizeof(uint64_t), NULL, 0); if (contention == 0) OSSL_sleep(1000); ossl_rcu_write_lock(rcu_lock); old = ossl_rcu_deref(&writer_ptr); TSAN_ACQUIRE(&writer_ptr); *new = global_ctr++; ossl_rcu_assign_ptr(&writer_ptr, &new); if (contention == 0) ossl_rcu_call(rcu_lock, free_old_rcu_data, old); ossl_rcu_write_unlock(rcu_lock); if (contention != 0) { ossl_synchronize_rcu(rcu_lock); CRYPTO_free(old, NULL, 0); } t2 = ossl_time_now(); if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4) break; } *iterations = count; return; } static void writer1_fn(void) { int local; TEST_info("Starting writer1"); writer_fn(1, &writer1_iterations); CRYPTO_atomic_add(&writer1_done, 1, &local, NULL); } static void writer2_fn(void) { int local; TEST_info("Starting writer2"); writer_fn(2, &writer2_iterations); CRYPTO_atomic_add(&writer2_done, 1, &local, NULL); } static void reader_fn(int *iterations) { unsigned int count = 0; uint64_t *valp; uint64_t val; uint64_t oldval = 0; int lw1 = 0; int lw2 = 0; while (lw1 != 1 || lw2 != 1) { CRYPTO_atomic_add(&writer1_done, 0, &lw1, NULL); CRYPTO_atomic_add(&writer2_done, 0, &lw2, NULL); count++; ossl_rcu_read_lock(rcu_lock); valp = ossl_rcu_deref(&writer_ptr); val = (valp == NULL) ? 0 : *valp; if (oldval > val) { TEST_info("rcu torture value went backwards! %llu : %llu", (unsigned long long)oldval, (unsigned long long)val); rcu_torture_result = 0; } oldval = val; ossl_rcu_read_unlock(rcu_lock); if (rcu_torture_result == 0) { *iterations = count; return; } } *iterations = count; } static void reader1_fn(void) { TEST_info("Starting reader 1"); reader_fn(&reader1_iterations); } static void reader2_fn(void) { TEST_info("Starting reader 2"); reader_fn(&reader2_iterations); } static thread_t writer1; static thread_t writer2; static thread_t reader1; static thread_t reader2; static int _torture_rcu(void) { OSSL_TIME t1, t2; struct timeval dtime; double tottime; double avr, avw; memset(&writer1, 0, sizeof(thread_t)); memset(&writer2, 0, sizeof(thread_t)); memset(&reader1, 0, sizeof(thread_t)); memset(&reader2, 0, sizeof(thread_t)); writer1_iterations = 0; writer2_iterations = 0; reader1_iterations = 0; reader2_iterations = 0; writer1_done = 0; writer2_done = 0; rcu_torture_result = 1; rcu_lock = ossl_rcu_lock_new(1); TEST_info("Staring rcu torture"); t1 = ossl_time_now(); if (!TEST_true(run_thread(&reader1, reader1_fn)) || !TEST_true(run_thread(&reader2, reader2_fn)) || !TEST_true(run_thread(&writer1, writer1_fn)) || !TEST_true(run_thread(&writer2, writer2_fn)) || !TEST_true(wait_for_thread(writer1)) || !TEST_true(wait_for_thread(writer2)) || !TEST_true(wait_for_thread(reader1)) || !TEST_true(wait_for_thread(reader2))) return 0; t2 = ossl_time_now(); dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1)); tottime = dtime.tv_sec + (dtime.tv_usec / 1e6); TEST_info("rcu_torture_result is %d\n", rcu_torture_result); TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds", reader1_iterations + reader2_iterations, writer1_iterations + writer2_iterations, tottime); avr = tottime / (reader1_iterations + reader2_iterations); avw = tottime / (writer1_iterations + writer2_iterations); TEST_info("Average read time %e/read", avr); TEST_info("Average write time %e/write", avw); ossl_rcu_lock_free(rcu_lock); if (!TEST_int_eq(rcu_torture_result, 1)) return 0; return 1; } static int torture_rcu_low(void) { contention = 0; return _torture_rcu(); } static int torture_rcu_high(void) { contention = 1; return _torture_rcu(); } # endif #endif static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT; static unsigned once_run_count = 0; static void once_do_run(void) { once_run_count++; } static void once_run_thread_cb(void) { CRYPTO_THREAD_run_once(&once_run, once_do_run); } static int test_once(void) { thread_t thread; if (!TEST_true(run_thread(&thread, once_run_thread_cb)) || !TEST_true(wait_for_thread(thread)) || !CRYPTO_THREAD_run_once(&once_run, once_do_run) || !TEST_int_eq(once_run_count, 1)) return 0; return 1; } static CRYPTO_THREAD_LOCAL thread_local_key; static unsigned destructor_run_count = 0; static int thread_local_thread_cb_ok = 0; static void thread_local_destructor(void *arg) { unsigned *count; if (arg == NULL) return; count = arg; (*count)++; } static void thread_local_thread_cb(void) { void *ptr; ptr = CRYPTO_THREAD_get_local(&thread_local_key); if (!TEST_ptr_null(ptr) || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key, &destructor_run_count))) return; ptr = CRYPTO_THREAD_get_local(&thread_local_key); if (!TEST_ptr_eq(ptr, &destructor_run_count)) return; thread_local_thread_cb_ok = 1; } static int test_thread_local(void) { thread_t thread; void *ptr = NULL; if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key, thread_local_destructor))) return 0; ptr = CRYPTO_THREAD_get_local(&thread_local_key); if (!TEST_ptr_null(ptr) || !TEST_true(run_thread(&thread, thread_local_thread_cb)) || !TEST_true(wait_for_thread(thread)) || !TEST_int_eq(thread_local_thread_cb_ok, 1)) return 0; #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) ptr = CRYPTO_THREAD_get_local(&thread_local_key); if (!TEST_ptr_null(ptr)) return 0; # if !defined(OPENSSL_SYS_WINDOWS) if (!TEST_int_eq(destructor_run_count, 1)) return 0; # endif #endif if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key))) return 0; return 1; } static int test_atomic(void) { int val = 0, ret = 0, testresult = 0; uint64_t val64 = 1, ret64 = 0; CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new(); if (!TEST_ptr(lock)) return 0; if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) { if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret)) goto err; } else { if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret)) goto err; } val = 0; ret = 0; if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock))) goto err; if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret)) goto err; if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) { if (!TEST_uint_eq((unsigned int)val64, 3) || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) goto err; } else { if (!TEST_uint_eq((unsigned int)val64, 1) || !TEST_int_eq((unsigned int)ret64, 0)) goto err; } val64 = 1; ret64 = 0; if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock))) goto err; if (!TEST_uint_eq((unsigned int)val64, 3) || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) goto err; ret64 = 0; if (CRYPTO_atomic_load(&val64, &ret64, NULL)) { if (!TEST_uint_eq((unsigned int)val64, 3) || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) goto err; } else { if (!TEST_uint_eq((unsigned int)val64, 3) || !TEST_int_eq((unsigned int)ret64, 0)) goto err; } ret64 = 0; if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock))) goto err; if (!TEST_uint_eq((unsigned int)val64, 3) || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64)) goto err; testresult = 1; err: CRYPTO_THREAD_lock_free(lock); return testresult; } static OSSL_LIB_CTX *multi_libctx = NULL; static int multi_success; static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1]; static size_t multi_num_threads; static thread_t multi_threads[MAXIMUM_THREADS]; static void multi_intialise(void) { multi_success = 1; multi_libctx = NULL; multi_num_threads = 0; memset(multi_threads, 0, sizeof(multi_threads)); memset(multi_provider, 0, sizeof(multi_provider)); } static void multi_set_success(int ok) { if (CRYPTO_THREAD_write_lock(global_lock) == 0) { multi_success = ok; return; } multi_success = ok; CRYPTO_THREAD_unlock(global_lock); } static void thead_teardown_libctx(void) { OSSL_PROVIDER **p; for (p = multi_provider; *p != NULL; p++) OSSL_PROVIDER_unload(*p); OSSL_LIB_CTX_free(multi_libctx); multi_intialise(); } static int thread_setup_libctx(int libctx, const char *providers[]) { size_t n; if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file, NULL, NULL))) return 0; if (providers != NULL) for (n = 0; providers[n] != NULL; n++) if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS) || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx, providers[n]))) { thead_teardown_libctx(); return 0; } return 1; } static int teardown_threads(void) { size_t i; for (i = 0; i < multi_num_threads; i++) if (!TEST_true(wait_for_thread(multi_threads[i]))) return 0; return 1; } static int start_threads(size_t n, void (*thread_func)(void)) { size_t i; if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS)) return 0; for (i = 0 ; i < n; i++) if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func))) return 0; return 1; } static int thread_run_test(void (*main_func)(void), size_t num_threads, void (*thread_func)(void), int libctx, const char *providers[]) { int testresult = 0; multi_intialise(); if (!thread_setup_libctx(libctx, providers) || !start_threads(num_threads, thread_func)) goto err; if (main_func != NULL) main_func(); if (!teardown_threads() || !TEST_true(multi_success)) goto err; testresult = 1; err: thead_teardown_libctx(); return testresult; } static void thread_general_worker(void) { EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL); EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new(); EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL); const char *message = "Hello World"; size_t messlen = strlen(message); unsigned char out[EVP_MAX_MD_SIZE]; const unsigned char key[AES_BLOCK_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const unsigned char iv[AES_BLOCK_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; unsigned int mdoutl; int ciphoutl; EVP_PKEY *pkey = NULL; int testresult = 0; int i, isfips; isfips = OSSL_PROVIDER_available(multi_libctx, "fips"); if (!TEST_ptr(mdctx) || !TEST_ptr(md) || !TEST_ptr(cipherctx) || !TEST_ptr(ciph)) goto err; for (i = 0; i < 5; i++) { if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL)) || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen)) || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl))) goto err; } for (i = 0; i < 5; i++) { if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv)) || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl, (unsigned char *)message, messlen)) || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl))) goto err; } pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512); if (!TEST_ptr(pkey)) goto err; testresult = 1; err: EVP_MD_CTX_free(mdctx); EVP_MD_free(md); EVP_CIPHER_CTX_free(cipherctx); EVP_CIPHER_free(ciph); EVP_PKEY_free(pkey); if (!testresult) multi_set_success(0); } static void thread_multi_simple_fetch(void) { EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL); if (md != NULL) EVP_MD_free(md); else multi_set_success(0); } static EVP_PKEY *shared_evp_pkey = NULL; static void thread_shared_evp_pkey(void) { char *msg = "Hello World"; unsigned char ctbuf[256]; unsigned char ptbuf[256]; size_t ptlen, ctlen = sizeof(ctbuf); EVP_PKEY_CTX *ctx = NULL; int success = 0; int i; for (i = 0; i < 1 + do_fips; i++) { if (i > 0) EVP_PKEY_CTX_free(ctx); ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, i == 0 ? "provider=default" : "provider=fips"); if (!TEST_ptr(ctx)) goto err; if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0) || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen, (unsigned char *)msg, strlen(msg)), 0)) goto err; EVP_PKEY_CTX_free(ctx); ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL); if (!TEST_ptr(ctx)) goto err; ptlen = sizeof(ptbuf); if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0) || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen), 0) || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen)) goto err; } success = 1; err: EVP_PKEY_CTX_free(ctx); if (!success) multi_set_success(0); } static void thread_provider_load_unload(void) { OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default"); if (!TEST_ptr(deflt) || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default"))) multi_set_success(0); OSSL_PROVIDER_unload(deflt); } static int test_multi_general_worker_default_provider(void) { return thread_run_test(&thread_general_worker, 2, &thread_general_worker, 1, default_provider); } static int test_multi_general_worker_fips_provider(void) { if (!do_fips) return TEST_skip("FIPS not supported"); return thread_run_test(&thread_general_worker, 2, &thread_general_worker, 1, fips_provider); } static int test_multi_fetch_worker(void) { return thread_run_test(&thread_multi_simple_fetch, 2, &thread_multi_simple_fetch, 1, default_provider); } static int test_multi_shared_pkey_common(void (*worker)(void)) { int testresult = 0; multi_intialise(); if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers : default_provider) || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx)) || !start_threads(1, &thread_shared_evp_pkey) || !start_threads(1, worker)) goto err; thread_shared_evp_pkey(); if (!teardown_threads() || !TEST_true(multi_success)) goto err; testresult = 1; err: EVP_PKEY_free(shared_evp_pkey); thead_teardown_libctx(); return testresult; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static void thread_downgrade_shared_evp_pkey(void) { if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL) multi_set_success(0); } static int test_multi_downgrade_shared_pkey(void) { return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey); } #endif static int test_multi_shared_pkey(void) { return test_multi_shared_pkey_common(&thread_shared_evp_pkey); } static int test_multi_load_unload_provider(void) { EVP_MD *sha256 = NULL; OSSL_PROVIDER *prov = NULL; int testresult = 0; multi_intialise(); if (!thread_setup_libctx(1, NULL) || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default")) || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL)) || !TEST_true(OSSL_PROVIDER_unload(prov))) goto err; prov = NULL; if (!start_threads(2, &thread_provider_load_unload)) goto err; thread_provider_load_unload(); if (!teardown_threads() || !TEST_true(multi_success)) goto err; testresult = 1; err: OSSL_PROVIDER_unload(prov); EVP_MD_free(sha256); thead_teardown_libctx(); return testresult; } static char *multi_load_provider = "legacy"; static void test_multi_load_worker(void) { OSSL_PROVIDER *prov; if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider)) || !TEST_true(OSSL_PROVIDER_unload(prov))) multi_set_success(0); } static int test_multi_default(void) { if (multidefault_run) { TEST_skip("multi default test already run"); return 1; } multidefault_run = 1; return thread_run_test(&thread_multi_simple_fetch, 2, &thread_multi_simple_fetch, 0, default_provider); } static int test_multi_load(void) { int res = 1; OSSL_PROVIDER *prov; if (!multidefault_run) { TEST_info("Running multi default test first"); res = test_multi_default(); } prov = OSSL_PROVIDER_load(NULL, "legacy"); if (prov == NULL) { TEST_info("Cannot load legacy provider - assuming this is a no-legacy build"); multi_load_provider = "default"; } OSSL_PROVIDER_unload(prov); return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0, NULL) && res; } static void test_obj_create_one(void) { char tids[12], oid[40], sn[30], ln[30]; int id = get_new_uid(); BIO_snprintf(tids, sizeof(tids), "%d", id); BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids); BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids); BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids); if (!TEST_int_ne(id, 0) || !TEST_true(id = OBJ_create(oid, sn, ln)) || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa))) multi_set_success(0); } static int test_obj_add(void) { return thread_run_test(&test_obj_create_one, MAXIMUM_THREADS, &test_obj_create_one, 1, default_provider); } static void test_lib_ctx_load_config_worker(void) { if (!TEST_int_eq(OSSL_LIB_CTX_load_config(multi_libctx, config_file), 1)) multi_set_success(0); } static int test_lib_ctx_load_config(void) { return thread_run_test(&test_lib_ctx_load_config_worker, MAXIMUM_THREADS, &test_lib_ctx_load_config_worker, 1, default_provider); } #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) static BIO *multi_bio1, *multi_bio2; static void test_bio_dgram_pair_worker(void) { ossl_unused int r; int ok = 0; uint8_t ch = 0; uint8_t scratch[64]; BIO_MSG msg = {0}; size_t num_processed = 0; if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1)) goto err; msg.data = scratch; msg.data_len = sizeof(scratch); if (ch & 2) r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg, sizeof(BIO_MSG), 1, 0, &num_processed); else r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg, sizeof(BIO_MSG), 1, 0, &num_processed); ok = 1; err: if (ok == 0) multi_set_success(0); } static int test_bio_dgram_pair(void) { int r; BIO *bio1 = NULL, *bio2 = NULL; r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0); if (!TEST_int_eq(r, 1)) goto err; multi_bio1 = bio1; multi_bio2 = bio2; r = thread_run_test(&test_bio_dgram_pair_worker, MAXIMUM_THREADS, &test_bio_dgram_pair_worker, 1, default_provider); err: BIO_free(bio1); BIO_free(bio2); return r; } #endif static const char *pemdataraw[] = { "-----BEGIN RSA PRIVATE KEY-----\n", "MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n", "oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n", "kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n", "AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n", "74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n", "dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n", "HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n", "-----END RSA PRIVATE KEY-----\n", NULL }; static void test_pem_read_one(void) { EVP_PKEY *key = NULL; BIO *pem = NULL; char *pemdata; size_t len; pemdata = glue_strings(pemdataraw, &len); if (pemdata == NULL) { multi_set_success(0); goto err; } pem = BIO_new_mem_buf(pemdata, len); if (pem == NULL) { multi_set_success(0); goto err; } key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL); if (key == NULL) multi_set_success(0); err: EVP_PKEY_free(key); BIO_free(pem); OPENSSL_free(pemdata); } static int test_pem_read(void) { return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS, &test_pem_read_one, 1, default_provider); } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_FIPS, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "fips", OPT_FIPS, '-', "Test the FIPS provider" }, { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { NULL } }; return options; } int setup_tests(void) { OPTION_CHOICE o; char *datadir; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_FIPS: do_fips = 1; break; case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_TEST_CASES: break; default: return 0; } } if (!TEST_ptr(datadir = test_get_argument(0))) return 0; privkey = test_mk_file_path(datadir, "rsakey.pem"); if (!TEST_ptr(privkey)) return 0; if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new())) return 0; #ifdef TSAN_REQUIRES_LOCKING if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new())) return 0; #endif ADD_TEST(test_multi_default); ADD_TEST(test_lock); #if defined(OPENSSL_THREADS) ADD_TEST(torture_rw_low); ADD_TEST(torture_rw_high); # ifndef OPENSSL_SYS_MACOSX ADD_TEST(torture_rcu_low); ADD_TEST(torture_rcu_high); # endif #endif ADD_TEST(test_once); ADD_TEST(test_thread_local); ADD_TEST(test_atomic); ADD_TEST(test_multi_load); ADD_TEST(test_multi_general_worker_default_provider); ADD_TEST(test_multi_general_worker_fips_provider); ADD_TEST(test_multi_fetch_worker); ADD_TEST(test_multi_shared_pkey); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_multi_downgrade_shared_pkey); #endif ADD_TEST(test_multi_load_unload_provider); ADD_TEST(test_obj_add); ADD_TEST(test_lib_ctx_load_config); #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) ADD_TEST(test_bio_dgram_pair); #endif ADD_TEST(test_pem_read); return 1; } void cleanup_tests(void) { OPENSSL_free(privkey); #ifdef TSAN_REQUIRES_LOCKING CRYPTO_THREAD_lock_free(tsan_lock); #endif CRYPTO_THREAD_lock_free(global_lock); }
test
openssl/test/threadstest.c
openssl
#include <stdio.h> #include <openssl/opensslv.h> #include <openssl/crypto.h> int main(void) { printf("Build version: %s\n", OPENSSL_FULL_VERSION_STR); printf("Library version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING)); return 0; }
test
openssl/test/versions.c
openssl
#include <string.h> #include <openssl/params.h> #include "testutil.h" #if !defined(OPENSSL_NO_INTTYPES_H) # ifdef OPENSSL_SYS_VMS # define strtoumax strtoull # define strtoimax strtoll # endif typedef struct { OSSL_PARAM *param; int32_t i32; int64_t i64; uint32_t u32; uint64_t u64; double d; int valid_i32, valid_i64, valid_u32, valid_u64, valid_d; void *ref, *datum; size_t size; } PARAM_CONVERSION; static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s) { static int32_t datum_i32, ref_i32; static int64_t datum_i64, ref_i64; static uint32_t datum_u32, ref_u32; static uint64_t datum_u64, ref_u64; static double datum_d, ref_d; static OSSL_PARAM params[] = { OSSL_PARAM_int32("int32", &datum_i32), OSSL_PARAM_int64("int64", &datum_i64), OSSL_PARAM_uint32("uint32", &datum_u32), OSSL_PARAM_uint64("uint64", &datum_u64), OSSL_PARAM_double("double", &datum_d), OSSL_PARAM_END }; int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0; const PAIR *pp = s->pairs; const char *type = NULL; char *p; int i; memset(pc, 0, sizeof(*pc)); for (i = 0; i < s->numpairs; i++, pp++) { p = ""; if (OPENSSL_strcasecmp(pp->key, "type") == 0) { if (type != NULL) { TEST_info("Line %d: multiple type lines", s->curr); return 0; } pc->param = OSSL_PARAM_locate(params, type = pp->value); if (pc->param == NULL) { TEST_info("Line %d: unknown type line", s->curr); return 0; } } else if (OPENSSL_strcasecmp(pp->key, "int32") == 0) { if (def_i32++) { TEST_info("Line %d: multiple int32 lines", s->curr); return 0; } if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) { pc->valid_i32 = 1; pc->i32 = (int32_t)strtoimax(pp->value, &p, 10); } } else if (OPENSSL_strcasecmp(pp->key, "int64") == 0) { if (def_i64++) { TEST_info("Line %d: multiple int64 lines", s->curr); return 0; } if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) { pc->valid_i64 = 1; pc->i64 = (int64_t)strtoimax(pp->value, &p, 10); } } else if (OPENSSL_strcasecmp(pp->key, "uint32") == 0) { if (def_u32++) { TEST_info("Line %d: multiple uint32 lines", s->curr); return 0; } if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) { pc->valid_u32 = 1; pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10); } } else if (OPENSSL_strcasecmp(pp->key, "uint64") == 0) { if (def_u64++) { TEST_info("Line %d: multiple uint64 lines", s->curr); return 0; } if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) { pc->valid_u64 = 1; pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10); } } else if (OPENSSL_strcasecmp(pp->key, "double") == 0) { if (def_d++) { TEST_info("Line %d: multiple double lines", s->curr); return 0; } if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) { pc->valid_d = 1; pc->d = strtod(pp->value, &p); } } else { TEST_info("Line %d: unknown keyword %s", s->curr, pp->key); return 0; } if (*p != '\0') { TEST_info("Line %d: extra characters at end '%s' for %s", s->curr, p, pp->key); return 0; } } if (!TEST_ptr(type)) { TEST_info("Line %d: type not found", s->curr); return 0; } if (OPENSSL_strcasecmp(type, "int32") == 0) { if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) { TEST_note("errant int32 on line %d", s->curr); return 0; } datum_i32 = ref_i32 = pc->i32; pc->datum = &datum_i32; pc->ref = &ref_i32; pc->size = sizeof(ref_i32); } else if (OPENSSL_strcasecmp(type, "int64") == 0) { if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) { TEST_note("errant int64 on line %d", s->curr); return 0; } datum_i64 = ref_i64 = pc->i64; pc->datum = &datum_i64; pc->ref = &ref_i64; pc->size = sizeof(ref_i64); } else if (OPENSSL_strcasecmp(type, "uint32") == 0) { if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) { TEST_note("errant uint32 on line %d", s->curr); return 0; } datum_u32 = ref_u32 = pc->u32; pc->datum = &datum_u32; pc->ref = &ref_u32; pc->size = sizeof(ref_u32); } else if (OPENSSL_strcasecmp(type, "uint64") == 0) { if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) { TEST_note("errant uint64 on line %d", s->curr); return 0; } datum_u64 = ref_u64 = pc->u64; pc->datum = &datum_u64; pc->ref = &ref_u64; pc->size = sizeof(ref_u64); } else if (OPENSSL_strcasecmp(type, "double") == 0) { if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) { TEST_note("errant double on line %d", s->curr); return 0; } datum_d = ref_d = pc->d; pc->datum = &datum_d; pc->ref = &ref_d; pc->size = sizeof(ref_d); } else { TEST_error("type unknown at line %d", s->curr); return 0; } return 1; } static int param_conversion_test(const PARAM_CONVERSION *pc, int line) { int32_t i32; int64_t i64; uint32_t u32; uint64_t u64; double d; if (!pc->valid_i32) { if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32)) || !TEST_ulong_ne(ERR_get_error(), 0)) { TEST_note("unexpected valid conversion to int32 on line %d", line); return 0; } } else { if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32)) || !TEST_true(i32 == pc->i32)) { TEST_note("unexpected conversion to int32 on line %d", line); return 0; } memset(pc->datum, 44, pc->size); if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32)) || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) { TEST_note("unexpected valid conversion from int32 on line %d", line); return 0; } } if (!pc->valid_i64) { if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64)) || !TEST_ulong_ne(ERR_get_error(), 0)) { TEST_note("unexpected valid conversion to int64 on line %d", line); return 0; } } else { if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64)) || !TEST_true(i64 == pc->i64)) { TEST_note("unexpected conversion to int64 on line %d", line); return 0; } memset(pc->datum, 44, pc->size); if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64)) || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) { TEST_note("unexpected valid conversion from int64 on line %d", line); return 0; } } if (!pc->valid_u32) { if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32)) || !TEST_ulong_ne(ERR_get_error(), 0)) { TEST_note("unexpected valid conversion to uint32 on line %d", line); return 0; } } else { if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32)) || !TEST_true(u32 == pc->u32)) { TEST_note("unexpected conversion to uint32 on line %d", line); return 0; } memset(pc->datum, 44, pc->size); if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32)) || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) { TEST_note("unexpected valid conversion from uint32 on line %d", line); return 0; } } if (!pc->valid_u64) { if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64)) || !TEST_ulong_ne(ERR_get_error(), 0)) { TEST_note("unexpected valid conversion to uint64 on line %d", line); return 0; } } else { if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64)) || !TEST_true(u64 == pc->u64)) { TEST_note("unexpected conversion to uint64 on line %d", line); return 0; } memset(pc->datum, 44, pc->size); if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64)) || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) { TEST_note("unexpected valid conversion from uint64 on line %d", line); return 0; } } if (!pc->valid_d) { if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d)) || !TEST_ulong_ne(ERR_get_error(), 0)) { TEST_note("unexpected valid conversion to double on line %d", line); return 0; } } else { if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))) { TEST_note("unable to convert to double on line %d", line); return 0; } if (!(d == d)) { if (!TEST_false(pc->d == pc->d)) { TEST_note("unexpected NaN on line %d", line); return 0; } } else if (!TEST_true(d == pc->d)) { TEST_note("unexpected conversion to double on line %d", line); return 0; } memset(pc->datum, 44, pc->size); if (!TEST_true(OSSL_PARAM_set_double(pc->param, d)) || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) { TEST_note("unexpected valid conversion from double on line %d", line); return 0; } } return 1; } static int run_param_file_tests(int i) { STANZA *s; PARAM_CONVERSION pc; const char *testfile = test_get_argument(i); int res = 1; if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s)))) return 0; if (!test_start_file(s, testfile)) { OPENSSL_free(s); return 0; } while (!BIO_eof(s->fp)) { if (!test_readstanza(s)) { res = 0; goto end; } if (s->numpairs != 0) if (!param_conversion_load_stanza(&pc, s) || !param_conversion_test(&pc, s->curr)) res = 0; test_clearstanza(s); } end: test_end_file(s); OPENSSL_free(s); return res; } #endif OPT_TEST_DECLARE_USAGE("file...\n") int setup_tests(void) { size_t n; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } n = test_get_argument_count(); if (n == 0) return 0; #if !defined(OPENSSL_NO_INTTYPES_H) ADD_ALL_TESTS(run_param_file_tests, n); #endif return 1; }
test
openssl/test/params_conversion_test.c
openssl
#include "internal/quic_rcidm.h" #include "testutil.h" static const QUIC_CONN_ID cid8_1 = { 8, { 1 } }; static const QUIC_CONN_ID cid8_2 = { 8, { 2 } }; static const QUIC_CONN_ID cid8_3 = { 8, { 3 } }; static const QUIC_CONN_ID cid8_4 = { 8, { 4 } }; static const QUIC_CONN_ID cid8_5 = { 8, { 5 } }; static int test_rcidm(int idx) { int testresult = 0; QUIC_RCIDM *rcidm; OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1 = {0}, ncid_frame_2 = {0}; QUIC_CONN_ID dcid_out; const QUIC_CONN_ID *odcid = NULL; uint64_t seq_num_out; ncid_frame_1.seq_num = 2; ncid_frame_1.conn_id.id_len = 8; ncid_frame_1.conn_id.id[0] = 3; ncid_frame_2.seq_num = 3; ncid_frame_2.conn_id.id_len = 8; ncid_frame_2.conn_id.id[0] = 4; odcid = ((idx == 2) ? NULL : &cid8_1); if (!TEST_ptr(rcidm = ossl_quic_rcidm_new(odcid))) goto err; if (idx != 2) { if ( !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_1)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0)) goto err; } else { if (!TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0)) goto err; } if (idx == 1) { if (!TEST_true(ossl_quic_rcidm_add_from_server_retry(rcidm, &cid8_5)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_5)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0)) goto err; } if (!TEST_true(ossl_quic_rcidm_add_from_initial(rcidm, &cid8_2)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 1) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2)) || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_1)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 2) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2)) || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_2)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 3) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 0) || !TEST_false(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2))) goto err; ossl_quic_rcidm_on_handshake_complete(rcidm); if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_3)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 1) || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out)) || !TEST_uint64_t_eq(seq_num_out, 0)) goto err; ossl_quic_rcidm_request_roll(rcidm); if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1)) || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out)) || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_4)) || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 2) || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out)) || !TEST_uint64_t_eq(seq_num_out, 0) || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out)) || !TEST_uint64_t_eq(seq_num_out, 0) || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out)) || !TEST_uint64_t_eq(seq_num_out, 2)) goto err; testresult = 1; err: ossl_quic_rcidm_free(rcidm); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_rcidm, 3); return 1; }
test
openssl/test/quic_rcidm_test.c
openssl
#include <openssl/evp.h> #include <openssl/core_names.h> #include <openssl/param_build.h> #include <openssl/proverr.h> #include "internal/nelem.h" #include "testutil.h" #define TEST_KEM_ENCAP 0 #define TEST_KEM_DECAP 1 #define TEST_KEM_ENCAP_DECAP 2 #define TEST_TYPE_AUTH 0 #define TEST_TYPE_NOAUTH 1 #define TEST_TYPE_AUTH_NOAUTH 2 #define TEST_KEYTYPE_P256 0 #define TEST_KEYTYPE_X25519 1 #define TEST_KEYTYPES_P256_X25519 2 static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *libprov = NULL; static OSSL_PARAM opparam[2]; static EVP_PKEY *rkey[TEST_KEYTYPES_P256_X25519] = { NULL, NULL }; static EVP_PKEY_CTX *rctx[TEST_KEYTYPES_P256_X25519] = { NULL, NULL }; #include "dhkem_test.inc" static int test_dhkem_encapsulate(int tstid) { int ret = 0; EVP_PKEY *rpub = NULL, *spriv = NULL; const TEST_ENCAPDATA *t = &ec_encapdata[tstid]; TEST_note("Test %s %s Decapsulate", t->curve, t->spriv != NULL ? "Auth" : ""); if (!TEST_ptr(rpub = new_raw_public_key(t->curve, t->rpub, t->rpublen))) goto err; if (t->spriv != NULL) { if (!TEST_ptr(spriv = new_raw_private_key(t->curve, t->spriv, t->sprivlen, t->spub, t->spublen))) goto err; } ret = do_encap(t, rpub, spriv); err: EVP_PKEY_free(spriv); EVP_PKEY_free(rpub); return ret; } static int test_dhkem_decapsulate(int tstid) { int ret = 0; EVP_PKEY *rpriv = NULL, *spub = NULL; const TEST_ENCAPDATA *t = &ec_encapdata[tstid]; TEST_note("Test %s %s Decapsulate", t->curve, t->spub != NULL ? "Auth" : ""); if (!TEST_ptr(rpriv = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, t->rpub, t->rpublen))) goto err; if (t->spub != NULL) { if (!TEST_ptr(spub = new_raw_public_key(t->curve, t->spub, t->spublen))) goto err; } ret = do_decap(t, rpriv, spub); err: EVP_PKEY_free(spub); EVP_PKEY_free(rpriv); return ret; } static int test_settables(int tstid) { EVP_PKEY_CTX *ctx = rctx[tstid]; const OSSL_PARAM *settableparams; const OSSL_PARAM *p; return TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1) && TEST_ptr(settableparams = EVP_PKEY_CTX_settable_params(ctx)) && TEST_ptr(p = OSSL_PARAM_locate_const(settableparams, OSSL_KEM_PARAM_OPERATION)) && TEST_uint_eq(p->data_type, OSSL_PARAM_UTF8_STRING) && TEST_ptr(p = OSSL_PARAM_locate_const(settableparams, OSSL_KEM_PARAM_IKME)) && TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING); } static int test_init_multiple(int tstid) { EVP_PKEY_CTX *ctx = rctx[tstid]; return TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1); } static int test_ec_dhkem_derivekey_fail(void) { int ret = 0; EVP_PKEY *pkey = NULL; OSSL_PARAM params[3]; EVP_PKEY_CTX *genctx = NULL; const TEST_DERIVEKEY_DATA *t = &ec_derivekey_data[0]; BIGNUM *priv = NULL; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, "secp256k1", 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, (char *)t->ikm, t->ikmlen); params[2] = OSSL_PARAM_construct_end(); if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL)) || !TEST_int_eq(EVP_PKEY_keygen_init(genctx), 1) || !TEST_int_eq(EVP_PKEY_CTX_set_params(genctx, params), 1) || !TEST_int_eq(EVP_PKEY_generate(genctx, &pkey),0)) goto err; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, "P-224", 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, (char *)t->ikm, t->ikmlen); params[2] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_keygen_init(genctx), 1) || !TEST_int_eq(EVP_PKEY_CTX_set_params(genctx, params), 1) || !TEST_int_eq(EVP_PKEY_generate(genctx, &pkey), 0)) goto err; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, "P-256", 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, (char *)t->ikm, t->ikmlen - 1); params[2] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_CTX_set_params(genctx, params), 1) || !TEST_int_eq(EVP_PKEY_generate(genctx, &pkey), 0)) goto err; ret = 1; err: BN_free(priv); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(genctx); return ret; } static int test_no_operation_set(int tstid) { EVP_PKEY_CTX *ctx = rctx[tstid]; const TEST_ENCAPDATA *t = &ec_encapdata[tstid]; size_t len = 0; return TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &len, NULL, NULL), -2) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1) && TEST_int_eq(EVP_PKEY_decapsulate(ctx, NULL, &len, t->expected_enc, t->expected_enclen), -2); } static int test_ikm_small(int tstid) { unsigned char tmp[16] = { 0 }; unsigned char secret[256]; unsigned char enc[256]; size_t secretlen = sizeof(secret); size_t enclen = sizeof(enc); OSSL_PARAM params[3]; EVP_PKEY_CTX *ctx = rctx[tstid]; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, OSSL_KEM_PARAM_OPERATION_DHKEM, 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_KEM_PARAM_IKME, tmp, sizeof(tmp)); params[2] = OSSL_PARAM_construct_end(); return TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, params), 1) && TEST_int_eq(EVP_PKEY_encapsulate(ctx, enc, &enclen, secret, &secretlen), 0); } static int test_input_size_small(int tstid) { int ret = 0; unsigned char sec[256]; unsigned char enc[256]; size_t seclen = sizeof(sec); size_t enclen = sizeof(enc); EVP_PKEY_CTX *ctx = rctx[tstid]; if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, opparam), 1) || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &enclen, NULL, &seclen), 1)) goto err; enclen--; if (!TEST_int_eq(EVP_PKEY_encapsulate(ctx, enc, &enclen, sec, &seclen), 0)) goto err; enclen++; seclen--; if (!TEST_int_eq(EVP_PKEY_encapsulate(ctx, enc, &enclen, sec, &seclen), 0)) goto err; seclen++; if (!TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, opparam), 1)) goto err; seclen--; if (!TEST_int_eq(EVP_PKEY_decapsulate(ctx, sec, &seclen, enc, enclen), 0)) goto err; seclen++; enclen--; ret = TEST_int_eq(EVP_PKEY_decapsulate(ctx, sec, &seclen, enc, enclen), 0); err: return ret; } static int test_ec_auth_key_curve_mismatch(void) { int ret = 0; EVP_PKEY *auth = NULL; if (!TEST_ptr(auth = EVP_PKEY_Q_keygen(libctx, NULL, "EC", "P-521"))) return 0; ret = TEST_int_eq(EVP_PKEY_auth_encapsulate_init(rctx[0], auth, opparam), 0); EVP_PKEY_free(auth); return ret; } static int test_auth_key_type_mismatch(int tstid) { int id1 = tstid; int id2 = !tstid; return TEST_int_eq(EVP_PKEY_auth_encapsulate_init(rctx[id1], rkey[id2], opparam), 0); } static int test_ec_invalid_private_key(void) { int ret = 0; EVP_PKEY *priv = NULL; EVP_PKEY_CTX *ctx = NULL; const TEST_ENCAPDATA *t = &ec_encapdata[0]; static const unsigned char order[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51 }; ret = TEST_ptr(priv = new_raw_private_key("P-256", order, sizeof(order), t->rpub, t->rpublen)) && TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL)) && TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 0); EVP_PKEY_free(priv); EVP_PKEY_CTX_free(ctx); return ret; } static int test_ec_public_key_infinity(void) { int ret = 0; EVP_PKEY *key = NULL; EVP_PKEY_CTX *keyctx = NULL; unsigned char s[256]; unsigned char e[256]; size_t slen = sizeof(s); size_t elen = sizeof(e); unsigned char tmp[1] = { 0 }; EVP_PKEY_CTX *ctx = rctx[0]; const TEST_ENCAPDATA *t = &ec_encapdata[0]; ret = TEST_ptr(key = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, tmp, sizeof(tmp))) && TEST_ptr(keyctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL)) && TEST_int_eq(EVP_PKEY_encapsulate_init(keyctx, opparam), 1) && TEST_int_eq(EVP_PKEY_encapsulate(keyctx, e, &elen, s, &slen), 0) && TEST_int_eq(EVP_PKEY_decapsulate_init(keyctx, opparam), 1) && TEST_int_eq(EVP_PKEY_decapsulate(keyctx, s, &slen, t->expected_enc, t->expected_enclen), 0) && TEST_int_eq(EVP_PKEY_auth_encapsulate_init(ctx, key, opparam), 1) && TEST_int_eq(EVP_PKEY_encapsulate(ctx, e, &elen, s, &slen), 0); EVP_PKEY_free(key); EVP_PKEY_CTX_free(keyctx); return ret; } static int test_null_params(int tstid) { EVP_PKEY_CTX *ctx = rctx[tstid]; const TEST_ENCAPDATA *t = &ec_encapdata[tstid]; return TEST_int_eq(EVP_PKEY_auth_encapsulate_init(ctx, NULL, opparam), 0) && TEST_int_eq(EVP_PKEY_auth_decapsulate_init(ctx, NULL, opparam), 0) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, opparam), 1) && TEST_int_eq(EVP_PKEY_decapsulate(ctx, NULL, NULL, t->expected_enc, t->expected_enclen), 0) && TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, opparam), 1) && TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, NULL, NULL, NULL), 0); } static int test_set_params(int tstid) { int ret = 0; EVP_PKEY_CTX *ctx = rctx[tstid]; OSSL_PARAM badparams[4]; int val = 1; badparams[0] = OSSL_PARAM_construct_int(OSSL_KEM_PARAM_OPERATION, &val); badparams[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 0)) goto err; badparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, "unknown_op", 0); badparams[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 0)) goto err; badparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0); badparams[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 0)) goto err; badparams[0] = OSSL_PARAM_construct_int(OSSL_KEM_PARAM_IKME, &val); badparams[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 0)) goto err; badparams[0] = OSSL_PARAM_construct_octet_string(OSSL_KEM_PARAM_IKME, NULL, 0); badparams[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 1)) goto err; badparams[0] = OSSL_PARAM_construct_int("unknownparam", &val); badparams[1] = OSSL_PARAM_construct_end(); ret = TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, badparams), 1); err: return ret; } static int test_nopublic(int tstid) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *priv = NULL; int encap = ((tstid & 1) == 0); int keytype = tstid >= TEST_KEM_ENCAP_DECAP; const TEST_ENCAPDATA *t = &ec_encapdata[keytype]; int expected = (keytype == TEST_KEYTYPE_X25519); TEST_note("%s %s", t->curve, encap ? "Encap" : "Decap"); if (!TEST_ptr(priv = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, NULL, 0))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL))) goto err; if (encap) { if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, opparam), expected)) goto err; } else { if (!TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, opparam), expected)) goto err; } if (expected == 0 && !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PROV_R_NOT_A_PUBLIC_KEY)) goto err; ret = 1; err: EVP_PKEY_free(priv); EVP_PKEY_CTX_free(ctx); return ret; } static int test_noauthpublic(int tstid) { int ret = 0; EVP_PKEY *auth = NULL; int encap = ((tstid & 1) == 0); int keytype = tstid >= TEST_KEM_ENCAP_DECAP; const TEST_ENCAPDATA *t = &ec_encapdata[keytype]; EVP_PKEY_CTX *ctx = rctx[keytype]; int expected = (keytype == TEST_KEYTYPE_X25519); TEST_note("%s %s", t->curve, encap ? "Encap" : "Decap"); if (!TEST_ptr(auth = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, NULL, expected))) goto err; if (encap) { if (!TEST_int_eq(EVP_PKEY_auth_encapsulate_init(ctx, auth, opparam), expected)) goto err; } else { if (!TEST_int_eq(EVP_PKEY_auth_decapsulate_init(ctx, auth, opparam), expected)) goto err; } if (expected == 0 && !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PROV_R_NOT_A_PUBLIC_KEY)) goto err; ret = 1; err: EVP_PKEY_free(auth); return ret; } static int test_ec_dhkem_derivekey(int tstid) { int ret = 0; EVP_PKEY *pkey = NULL; OSSL_PARAM params[3]; EVP_PKEY_CTX *genctx = NULL; const TEST_DERIVEKEY_DATA *t = &ec_derivekey_data[tstid]; unsigned char pubkey[133]; unsigned char privkey[66]; size_t pubkeylen = 0, privkeylen = 0; BIGNUM *priv = NULL; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, (char *)t->curvename, 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, (char *)t->ikm, t->ikmlen); params[2] = OSSL_PARAM_construct_end(); ret = TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL)) && TEST_int_eq(EVP_PKEY_keygen_init(genctx), 1) && TEST_int_eq(EVP_PKEY_CTX_set_params(genctx, params), 1) && TEST_int_eq(EVP_PKEY_generate(genctx, &pkey), 1) && TEST_true(EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, pubkey, sizeof(pubkey), &pubkeylen)) && TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv)) && TEST_int_gt(privkeylen = BN_bn2bin(priv, privkey), 0) && TEST_int_le(privkeylen, sizeof(privkey)) && TEST_mem_eq(privkey, privkeylen, t->priv, t->privlen) && TEST_mem_eq(pubkey, pubkeylen, t->pub, t->publen); BN_free(priv); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(genctx); return ret; } static int test_ec_noikme(int tstid) { int ret = 0, auth = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *recip = NULL; EVP_PKEY *sender_auth = NULL; unsigned char sender_secret[256]; unsigned char recip_secret[256]; unsigned char sender_pub[256]; size_t sender_secretlen = sizeof(sender_secret); size_t recip_secretlen = sizeof(recip_secret); size_t sender_publen = sizeof(sender_pub); const char *curve; int sz = OSSL_NELEM(dhkem_supported_curves); const char *op = OSSL_KEM_PARAM_OPERATION_DHKEM; if (tstid >= sz) { auth = 1; tstid -= sz; } curve = dhkem_supported_curves[tstid]; TEST_note("testing encap/decap of curve %s%s\n", curve, auth ? " with auth" : ""); if (curve[0] == 'X') { if (!TEST_ptr(recip = EVP_PKEY_Q_keygen(libctx, NULL, curve)) || (auth && !TEST_ptr(sender_auth = EVP_PKEY_Q_keygen(libctx, NULL, curve)))) goto err; } else { if (!TEST_ptr(recip = EVP_PKEY_Q_keygen(libctx, NULL, "EC", curve)) || (auth && !TEST_ptr(sender_auth = EVP_PKEY_Q_keygen(libctx, NULL, "EC", curve)))) goto err; } ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, recip, NULL)) && (sender_auth == NULL || TEST_int_eq(EVP_PKEY_auth_encapsulate_init(ctx, sender_auth, NULL), 1)) && (sender_auth != NULL || TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1)) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(ctx, op), 1) && TEST_int_eq(EVP_PKEY_encapsulate(ctx, sender_pub, &sender_publen, sender_secret, &sender_secretlen), 1) && (sender_auth == NULL || TEST_int_eq(EVP_PKEY_auth_decapsulate_init(ctx, sender_auth, NULL), 1)) && (sender_auth != NULL || TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1)) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(ctx, op), 1) && TEST_int_eq(EVP_PKEY_decapsulate(ctx, recip_secret, &recip_secretlen, sender_pub, sender_publen), 1) && TEST_mem_eq(recip_secret, recip_secretlen, sender_secret, sender_secretlen); err: EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(sender_auth); EVP_PKEY_free(recip); return ret; } static int do_ec_curve_failtest(const char *curve) { int ret; EVP_PKEY *key = NULL; EVP_PKEY_CTX *ctx = NULL; ret = TEST_ptr(key = EVP_PKEY_Q_keygen(libctx, NULL, "EC", curve)) && TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL)) && TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), -2) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), -2); EVP_PKEY_free(key); EVP_PKEY_CTX_free(ctx); return ret; } static int test_ec_curve_nonnist(void) { return do_ec_curve_failtest("secp256k1"); } static int test_ec_curve_unsupported(void) { return do_ec_curve_failtest("P-224"); } static int test_ec_badpublic(int tstid) { int ret = 0; EVP_PKEY *recippriv = NULL; EVP_PKEY_CTX *ctx = NULL; unsigned char secret[256]; unsigned char pub[256]; size_t secretlen = sizeof(secret); int encap = ((tstid & 1) == 0); const TEST_ENCAPDATA *t = &ec_encapdata[0]; TEST_note("%s %s", t->curve, encap ? "Encap" : "Decap"); pub[0] = 0; if (!TEST_ptr(recippriv = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, pub, 1))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, recippriv, NULL))) goto err; if (encap) { unsigned char enc[256]; size_t enclen = sizeof(enc); if (!TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, opparam), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_encapsulate(ctx, enc , &enclen, secret, &secretlen), 0 )) goto err; } else { if (!TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, opparam), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret, &secretlen, t->expected_enc, t->expected_enclen), 0)) goto err; } if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PROV_R_INVALID_KEY)) goto err; ret = 1; err: EVP_PKEY_free(recippriv); EVP_PKEY_CTX_free(ctx); return ret; } static int test_ec_badauth(int tstid) { int ret = 0; EVP_PKEY *auth = NULL; unsigned char enc[256]; unsigned char secret[256]; unsigned char pub[256]; size_t enclen = sizeof(enc); size_t secretlen = sizeof(secret); int encap = ((tstid & 1) == 0); const TEST_ENCAPDATA *t = &ec_encapdata[TEST_KEYTYPE_P256]; EVP_PKEY_CTX *ctx = rctx[TEST_KEYTYPE_P256]; TEST_note("%s %s", t->curve, encap ? "Encap" : "Decap"); pub[0] = 0; if (!TEST_ptr(auth = new_raw_private_key(t->curve, t->rpriv, t->rprivlen, pub, 1))) goto err; if (encap) { if (!TEST_int_eq(EVP_PKEY_auth_encapsulate_init(ctx, auth, opparam), 1) || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, enc, &enclen, secret, &secretlen), 0)) goto err; } else { if (!TEST_int_eq(EVP_PKEY_auth_decapsulate_init(ctx, auth, opparam), 1) || !TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret, &secretlen, t->expected_enc, t->expected_enclen), 0)) goto err; } if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PROV_R_INVALID_KEY)) goto err; ret = 1; err: EVP_PKEY_free(auth); return ret; } static int test_ec_invalid_decap_enc_buffer(void) { const TEST_ENCAPDATA *t = &ec_encapdata[TEST_KEYTYPE_P256]; unsigned char enc[256]; unsigned char secret[256]; size_t secretlen = sizeof(secret); EVP_PKEY_CTX *ctx = rctx[0]; memcpy(enc, t->expected_enc, t->expected_enclen); enc[0] = 0xFF; return TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, opparam), 1) && TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret, &secretlen, enc, t->expected_enclen), 0); } #ifndef OPENSSL_NO_ECX static int test_ecx_dhkem_derivekey(int tstid) { int ret; OSSL_PARAM params[2]; EVP_PKEY_CTX *genctx; EVP_PKEY *pkey = NULL; unsigned char pubkey[64]; unsigned char privkey[64]; unsigned char masked_priv[64]; size_t pubkeylen = 0, privkeylen = 0; const TEST_DERIVEKEY_DATA *t = &ecx_derivekey_data[tstid]; memcpy(masked_priv, t->priv, t->privlen); if (OPENSSL_strcasecmp(t->curvename, "X25519") == 0) { masked_priv[0] &= 248; masked_priv[t->privlen - 1] &= 127; masked_priv[t->privlen - 1] |= 64; } else { masked_priv[0] &= 252; masked_priv[t->privlen - 1] |= 128; } params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, (char *)t->ikm, t->ikmlen); params[1] = OSSL_PARAM_construct_end(); ret = TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, t->curvename, NULL)) && TEST_int_eq(EVP_PKEY_keygen_init(genctx), 1) && TEST_int_eq(EVP_PKEY_CTX_set_params(genctx, params), 1) && TEST_int_eq(EVP_PKEY_keygen(genctx, &pkey), 1) && TEST_int_eq(EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, pubkey, sizeof(pubkey), &pubkeylen), 1) && TEST_int_eq(EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, privkey, sizeof(privkey), &privkeylen), 1) && TEST_mem_eq(t->pub, t->publen, pubkey, pubkeylen) && TEST_mem_eq(masked_priv, t->privlen, privkey, privkeylen); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(genctx); return ret; } static int test_ecx_auth_key_curve_mismatch(void) { int ret = 0; EVP_PKEY *auth = NULL; if (!TEST_ptr(auth = EVP_PKEY_Q_keygen(libctx, NULL, "X448"))) return 0; ret = TEST_int_eq(EVP_PKEY_auth_encapsulate_init(rctx[TEST_KEYTYPE_X25519], auth, opparam), 0); EVP_PKEY_free(auth); return ret; } static int test_ed_curve_unsupported(void) { int ret; EVP_PKEY *key = NULL; EVP_PKEY_CTX *ctx = NULL; ret = TEST_ptr(key = EVP_PKEY_Q_keygen(libctx, NULL, "ED448")) && TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL)) && TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), -2) && TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), -2); EVP_PKEY_free(key); EVP_PKEY_CTX_free(ctx); return ret; } #endif int setup_tests(void) { const char *prov_name = "default"; char *config_file = NULL; char *op = OSSL_KEM_PARAM_OPERATION_DHKEM; if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name)) return 0; opparam[0] = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, op, 0); opparam[1] = OSSL_PARAM_construct_end(); if (!TEST_ptr(rkey[TEST_KEYTYPE_P256] = EVP_PKEY_Q_keygen(libctx, NULL, "EC", "P-256"))) goto err; #ifndef OPENSSL_NO_ECX if (!TEST_ptr(rkey[TEST_KEYTYPE_X25519] = EVP_PKEY_Q_keygen(libctx, NULL, "X25519"))) goto err; #endif if (!TEST_ptr(rctx[TEST_KEYTYPE_P256] = EVP_PKEY_CTX_new_from_pkey(libctx, rkey[TEST_KEYTYPE_P256], NULL))) goto err; #ifndef OPENSSL_NO_ECX if (!TEST_ptr(rctx[TEST_KEYTYPE_X25519] = EVP_PKEY_CTX_new_from_pkey(libctx, rkey[TEST_KEYTYPE_X25519], NULL))) goto err; #endif ADD_ALL_TESTS(test_dhkem_encapsulate, OSSL_NELEM(ec_encapdata)); ADD_ALL_TESTS(test_dhkem_decapsulate, OSSL_NELEM(ec_encapdata)); #ifndef OPENSSL_NO_ECX ADD_ALL_TESTS(test_settables, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_init_multiple, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_auth_key_type_mismatch, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_no_operation_set, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_ikm_small, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_input_size_small, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_null_params, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_set_params, TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_nopublic, TEST_KEM_ENCAP_DECAP * TEST_KEYTYPES_P256_X25519); ADD_ALL_TESTS(test_noauthpublic, TEST_KEM_ENCAP_DECAP * TEST_KEYTYPES_P256_X25519); #else ADD_ALL_TESTS(test_settables, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_init_multiple, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_auth_key_type_mismatch, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_no_operation_set, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_ikm_small, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_input_size_small, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_null_params, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_set_params, TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_nopublic, TEST_KEM_ENCAP_DECAP * TEST_KEYTYPE_P256); ADD_ALL_TESTS(test_noauthpublic, TEST_KEM_ENCAP_DECAP * TEST_KEYTYPE_P256); #endif ADD_ALL_TESTS(test_ec_dhkem_derivekey, OSSL_NELEM(ec_derivekey_data)); ADD_ALL_TESTS(test_ec_noikme, TEST_TYPE_AUTH_NOAUTH * OSSL_NELEM(dhkem_supported_curves)); ADD_TEST(test_ec_auth_key_curve_mismatch); ADD_TEST(test_ec_invalid_private_key); ADD_TEST(test_ec_dhkem_derivekey_fail); ADD_TEST(test_ec_curve_nonnist); ADD_TEST(test_ec_curve_unsupported); ADD_TEST(test_ec_invalid_decap_enc_buffer); ADD_TEST(test_ec_public_key_infinity); ADD_ALL_TESTS(test_ec_badpublic, TEST_KEM_ENCAP_DECAP); ADD_ALL_TESTS(test_ec_badauth, TEST_KEM_ENCAP_DECAP); #ifndef OPENSSL_NO_ECX ADD_ALL_TESTS(test_ecx_dhkem_derivekey, OSSL_NELEM(ecx_derivekey_data)); ADD_TEST(test_ecx_auth_key_curve_mismatch); ADD_TEST(test_ed_curve_unsupported); #endif return 1; err: return 0; } void cleanup_tests(void) { EVP_PKEY_free(rkey[1]); EVP_PKEY_free(rkey[0]); EVP_PKEY_CTX_free(rctx[1]); EVP_PKEY_CTX_free(rctx[0]); OSSL_PROVIDER_unload(libprov); OSSL_LIB_CTX_free(libctx); OSSL_PROVIDER_unload(nullprov); }
test
openssl/test/evp_pkey_dhkem_test.c
openssl
#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; 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); 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]; 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); 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; 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; 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; 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); } 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 }; # 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 }; # 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 }; # 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 }; # 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 }; # 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 }; 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 }; # 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 }; # 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 }; # 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 }; # 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 }; # 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 }; 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 }; # 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 }; # 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 }; # 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 }; # 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 }; # 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 }; # 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 }; # define K20 K1 # define A20 A1 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; 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; }
test
openssl/test/modes_internal_test.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include "testutil.h" #include "crypto/chacha.h" static const unsigned int key[] = { 0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c }; static const unsigned int ivp[] = { 0x00000000, 0x00000000, 0x03020100, 0x07060504 }; static const unsigned char ref[] = { 0xf7, 0x98, 0xa1, 0x89, 0xf1, 0x95, 0xe6, 0x69, 0x82, 0x10, 0x5f, 0xfb, 0x64, 0x0b, 0xb7, 0x75, 0x7f, 0x57, 0x9d, 0xa3, 0x16, 0x02, 0xfc, 0x93, 0xec, 0x01, 0xac, 0x56, 0xf8, 0x5a, 0xc3, 0xc1, 0x34, 0xa4, 0x54, 0x7b, 0x73, 0x3b, 0x46, 0x41, 0x30, 0x42, 0xc9, 0x44, 0x00, 0x49, 0x17, 0x69, 0x05, 0xd3, 0xbe, 0x59, 0xea, 0x1c, 0x53, 0xf1, 0x59, 0x16, 0x15, 0x5c, 0x2b, 0xe8, 0x24, 0x1a, 0x38, 0x00, 0x8b, 0x9a, 0x26, 0xbc, 0x35, 0x94, 0x1e, 0x24, 0x44, 0x17, 0x7c, 0x8a, 0xde, 0x66, 0x89, 0xde, 0x95, 0x26, 0x49, 0x86, 0xd9, 0x58, 0x89, 0xfb, 0x60, 0xe8, 0x46, 0x29, 0xc9, 0xbd, 0x9a, 0x5a, 0xcb, 0x1c, 0xc1, 0x18, 0xbe, 0x56, 0x3e, 0xb9, 0xb3, 0xa4, 0xa4, 0x72, 0xf8, 0x2e, 0x09, 0xa7, 0xe7, 0x78, 0x49, 0x2b, 0x56, 0x2e, 0xf7, 0x13, 0x0e, 0x88, 0xdf, 0xe0, 0x31, 0xc7, 0x9d, 0xb9, 0xd4, 0xf7, 0xc7, 0xa8, 0x99, 0x15, 0x1b, 0x9a, 0x47, 0x50, 0x32, 0xb6, 0x3f, 0xc3, 0x85, 0x24, 0x5f, 0xe0, 0x54, 0xe3, 0xdd, 0x5a, 0x97, 0xa5, 0xf5, 0x76, 0xfe, 0x06, 0x40, 0x25, 0xd3, 0xce, 0x04, 0x2c, 0x56, 0x6a, 0xb2, 0xc5, 0x07, 0xb1, 0x38, 0xdb, 0x85, 0x3e, 0x3d, 0x69, 0x59, 0x66, 0x09, 0x96, 0x54, 0x6c, 0xc9, 0xc4, 0xa6, 0xea, 0xfd, 0xc7, 0x77, 0xc0, 0x40, 0xd7, 0x0e, 0xaf, 0x46, 0xf7, 0x6d, 0xad, 0x39, 0x79, 0xe5, 0xc5, 0x36, 0x0c, 0x33, 0x17, 0x16, 0x6a, 0x1c, 0x89, 0x4c, 0x94, 0xa3, 0x71, 0x87, 0x6a, 0x94, 0xdf, 0x76, 0x28, 0xfe, 0x4e, 0xaa, 0xf2, 0xcc, 0xb2, 0x7d, 0x5a, 0xaa, 0xe0, 0xad, 0x7a, 0xd0, 0xf9, 0xd4, 0xb6, 0xad, 0x3b, 0x54, 0x09, 0x87, 0x46, 0xd4, 0x52, 0x4d, 0x38, 0x40, 0x7a, 0x6d, 0xeb, 0x3a, 0xb7, 0x8f, 0xab, 0x78, 0xc9, 0x42, 0x13, 0x66, 0x8b, 0xbb, 0xd3, 0x94, 0xc5, 0xde, 0x93, 0xb8, 0x53, 0x17, 0x8a, 0xdd, 0xd6, 0xb9, 0x7f, 0x9f, 0xa1, 0xec, 0x3e, 0x56, 0xc0, 0x0c, 0x9d, 0xdf, 0xf0, 0xa4, 0x4a, 0x20, 0x42, 0x41, 0x17, 0x5a, 0x4c, 0xab, 0x0f, 0x96, 0x1b, 0xa5, 0x3e, 0xde, 0x9b, 0xdf, 0x96, 0x0b, 0x94, 0xf9, 0x82, 0x9b, 0x1f, 0x34, 0x14, 0x72, 0x64, 0x29, 0xb3, 0x62, 0xc5, 0xb5, 0x38, 0xe3, 0x91, 0x52, 0x0f, 0x48, 0x9b, 0x7e, 0xd8, 0xd2, 0x0a, 0xe3, 0xfd, 0x49, 0xe9, 0xe2, 0x59, 0xe4, 0x43, 0x97, 0x51, 0x4d, 0x61, 0x8c, 0x96, 0xc4, 0x84, 0x6b, 0xe3, 0xc6, 0x80, 0xbd, 0xc1, 0x1c, 0x71, 0xdc, 0xbb, 0xe2, 0x9c, 0xcf, 0x80, 0xd6, 0x2a, 0x09, 0x38, 0xfa, 0x54, 0x93, 0x91, 0xe6, 0xea, 0x57, 0xec, 0xbe, 0x26, 0x06, 0x79, 0x0e, 0xc1, 0x5d, 0x22, 0x24, 0xae, 0x30, 0x7c, 0x14, 0x42, 0x26, 0xb7, 0xc4, 0xe8, 0xc2, 0xf9, 0x7d, 0x2a, 0x1d, 0x67, 0x85, 0x2d, 0x29, 0xbe, 0xba, 0x11, 0x0e, 0xdd, 0x44, 0x51, 0x97, 0x01, 0x20, 0x62, 0xa3, 0x93, 0xa9, 0xc9, 0x28, 0x03, 0xad, 0x3b, 0x4f, 0x31, 0xd7, 0xbc, 0x60, 0x33, 0xcc, 0xf7, 0x93, 0x2c, 0xfe, 0xd3, 0xf0, 0x19, 0x04, 0x4d, 0x25, 0x90, 0x59, 0x16, 0x77, 0x72, 0x86, 0xf8, 0x2f, 0x9a, 0x4c, 0xc1, 0xff, 0xe4, 0x30, 0xff, 0xd1, 0xdc, 0xfc, 0x27, 0xde, 0xed, 0x32, 0x7b, 0x9f, 0x96, 0x30, 0xd2, 0xfa, 0x96, 0x9f, 0xb6, 0xf0, 0x60, 0x3c, 0xd1, 0x9d, 0xd9, 0xa9, 0x51, 0x9e, 0x67, 0x3b, 0xcf, 0xcd, 0x90, 0x14, 0x12, 0x52, 0x91, 0xa4, 0x46, 0x69, 0xef, 0x72, 0x85, 0xe7, 0x4e, 0xd3, 0x72, 0x9b, 0x67, 0x7f, 0x80, 0x1c, 0x3c, 0xdf, 0x05, 0x8c, 0x50, 0x96, 0x31, 0x68, 0xb4, 0x96, 0x04, 0x37, 0x16, 0xc7, 0x30, 0x7c, 0xd9, 0xe0, 0xcd, 0xd1, 0x37, 0xfc, 0xcb, 0x0f, 0x05, 0xb4, 0x7c, 0xdb, 0xb9, 0x5c, 0x5f, 0x54, 0x83, 0x16, 0x22, 0xc3, 0x65, 0x2a, 0x32, 0xb2, 0x53, 0x1f, 0xe3, 0x26, 0xbc, 0xd6, 0xe2, 0xbb, 0xf5, 0x6a, 0x19, 0x4f, 0xa1, 0x96, 0xfb, 0xd1, 0xa5, 0x49, 0x52, 0x11, 0x0f, 0x51, 0xc7, 0x34, 0x33, 0x86, 0x5f, 0x76, 0x64, 0xb8, 0x36, 0x68, 0x5e, 0x36, 0x64, 0xb3, 0xd8, 0x44, 0x4a, 0xf8, 0x9a, 0x24, 0x28, 0x05, 0xe1, 0x8c, 0x97, 0x5f, 0x11, 0x46, 0x32, 0x49, 0x96, 0xfd, 0xe1, 0x70, 0x07, 0xcf, 0x3e, 0x6e, 0x8f, 0x4e, 0x76, 0x40, 0x22, 0x53, 0x3e, 0xdb, 0xfe, 0x07, 0xd4, 0x73, 0x3e, 0x48, 0xbb, 0x37, 0x2d, 0x75, 0xb0, 0xef, 0x48, 0xec, 0x98, 0x3e, 0xb7, 0x85, 0x32, 0x16, 0x1c, 0xc5, 0x29, 0xe5, 0xab, 0xb8, 0x98, 0x37, 0xdf, 0xcc, 0xa6, 0x26, 0x1d, 0xbb, 0x37, 0xc7, 0xc5, 0xe6, 0xa8, 0x74, 0x78, 0xbf, 0x41, 0xee, 0x85, 0xa5, 0x18, 0xc0, 0xf4, 0xef, 0xa9, 0xbd, 0xe8, 0x28, 0xc5, 0xa7, 0x1b, 0x8e, 0x46, 0x59, 0x7b, 0x63, 0x4a, 0xfd, 0x20, 0x4d, 0x3c, 0x50, 0x13, 0x34, 0x23, 0x9c, 0x34, 0x14, 0x28, 0x5e, 0xd7, 0x2d, 0x3a, 0x91, 0x69, 0xea, 0xbb, 0xd4, 0xdc, 0x25, 0xd5, 0x2b, 0xb7, 0x51, 0x6d, 0x3b, 0xa7, 0x12, 0xd7, 0x5a, 0xd8, 0xc0, 0xae, 0x5d, 0x49, 0x3c, 0x19, 0xe3, 0x8a, 0x77, 0x93, 0x9e, 0x7a, 0x05, 0x8d, 0x71, 0x3e, 0x9c, 0xcc, 0xca, 0x58, 0x04, 0x5f, 0x43, 0x6b, 0x43, 0x4b, 0x1c, 0x80, 0xd3, 0x65, 0x47, 0x24, 0x06, 0xe3, 0x92, 0x95, 0x19, 0x87, 0xdb, 0x69, 0x05, 0xc8, 0x0d, 0x43, 0x1d, 0xa1, 0x84, 0x51, 0x13, 0x5b, 0xe7, 0xe8, 0x2b, 0xca, 0xb3, 0x58, 0xcb, 0x39, 0x71, 0xe6, 0x14, 0x05, 0xb2, 0xff, 0x17, 0x98, 0x0d, 0x6e, 0x7e, 0x67, 0xe8, 0x61, 0xe2, 0x82, 0x01, 0xc1, 0xee, 0x30, 0xb4, 0x41, 0x04, 0x0f, 0xd0, 0x68, 0x78, 0xd6, 0x50, 0x42, 0xc9, 0x55, 0x82, 0xa4, 0x31, 0x82, 0x07, 0xbf, 0xc7, 0x00, 0xbe, 0x0c, 0xe3, 0x28, 0x89, 0xae, 0xc2, 0xff, 0xe5, 0x08, 0x5e, 0x89, 0x67, 0x91, 0x0d, 0x87, 0x9f, 0xa0, 0xe8, 0xc0, 0xff, 0x85, 0xfd, 0xc5, 0x10, 0xb9, 0xff, 0x2f, 0xbf, 0x87, 0xcf, 0xcb, 0x29, 0x57, 0x7d, 0x68, 0x09, 0x9e, 0x04, 0xff, 0xa0, 0x5f, 0x75, 0x2a, 0x73, 0xd3, 0x77, 0xc7, 0x0d, 0x3a, 0x8b, 0xc2, 0xda, 0x80, 0xe6, 0xe7, 0x80, 0xec, 0x05, 0x71, 0x82, 0xc3, 0x3a, 0xd1, 0xde, 0x38, 0x72, 0x52, 0x25, 0x8a, 0x1e, 0x18, 0xe6, 0xfa, 0xd9, 0x10, 0x32, 0x7c, 0xe7, 0xf4, 0x2f, 0xd1, 0xe1, 0xe0, 0x51, 0x5f, 0x95, 0x86, 0xe2, 0xf2, 0xef, 0xcb, 0x9f, 0x47, 0x2b, 0x1d, 0xbd, 0xba, 0xc3, 0x54, 0xa4, 0x16, 0x21, 0x51, 0xe9, 0xd9, 0x2c, 0x79, 0xfb, 0x08, 0xbb, 0x4d, 0xdc, 0x56, 0xf1, 0x94, 0x48, 0xc0, 0x17, 0x5a, 0x46, 0xe2, 0xe6, 0xc4, 0x91, 0xfe, 0xc7, 0x14, 0x19, 0xaa, 0x43, 0xa3, 0x49, 0xbe, 0xa7, 0x68, 0xa9, 0x2c, 0x75, 0xde, 0x68, 0xfd, 0x95, 0x91, 0xe6, 0x80, 0x67, 0xf3, 0x19, 0x70, 0x94, 0xd3, 0xfb, 0x87, 0xed, 0x81, 0x78, 0x5e, 0xa0, 0x75, 0xe4, 0xb6, 0x5e, 0x3e, 0x4c, 0x78, 0xf8, 0x1d, 0xa9, 0xb7, 0x51, 0xc5, 0xef, 0xe0, 0x24, 0x15, 0x23, 0x01, 0xc4, 0x8e, 0x63, 0x24, 0x5b, 0x55, 0x6c, 0x4c, 0x67, 0xaf, 0xf8, 0x57, 0xe5, 0xea, 0x15, 0xa9, 0x08, 0xd8, 0x3a, 0x1d, 0x97, 0x04, 0xf8, 0xe5, 0x5e, 0x73, 0x52, 0xb2, 0x0b, 0x69, 0x4b, 0xf9, 0x97, 0x02, 0x98, 0xe6, 0xb5, 0xaa, 0xd3, 0x3e, 0xa2, 0x15, 0x5d, 0x10, 0x5d, 0x4e }; static int test_cha_cha_internal(int n) { unsigned char buf[sizeof(ref)]; unsigned int i = n + 1, j; memset(buf, 0, i); memcpy(buf + i, ref + i, sizeof(ref) - i); ChaCha20_ctr32(buf, buf, i, key, ivp); for (j = 0; j < sizeof(ref); j++) if (!TEST_uchar_eq(buf[j], ref[j])) { TEST_info("%d failed at %u (%02x)\n", i, j, buf[j]); return 0; } return 1; } int setup_tests(void) { #ifdef OPENSSL_CPUID_OBJ OPENSSL_cpuid_setup(); #endif ADD_ALL_TESTS(test_cha_cha_internal, sizeof(ref)); return 1; }
test
openssl/test/chacha_internal_test.c
openssl
#include <openssl/evp.h> #include <openssl/rand.h> #include <openssl/bio.h> #include <openssl/core_names.h> #include "crypto/rand.h" #include "testutil.h" static int test_rand(void) { EVP_RAND_CTX *privctx; OSSL_PARAM params[2], *p = params; unsigned char entropy1[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; unsigned char entropy2[] = { 0xff, 0xfe, 0xfd }; unsigned char outbuf[3]; *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, entropy1, sizeof(entropy1)); *p = OSSL_PARAM_construct_end(); if (!TEST_ptr(privctx = RAND_get0_private(NULL)) || !TEST_true(EVP_RAND_CTX_set_params(privctx, params)) || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1, sizeof(outbuf)) || !TEST_int_le(RAND_priv_bytes(outbuf, sizeof(outbuf) + 1), 0) || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1 + sizeof(outbuf), sizeof(outbuf))) return 0; *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, entropy2, sizeof(entropy2)); if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params)) || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy2, sizeof(outbuf))) return 0; return 1; } static int test_rand_uniform(void) { uint32_t x, i, j; int err = 0, res = 0; OSSL_LIB_CTX *ctx; if (!test_get_libctx(&ctx, NULL, NULL, NULL, NULL)) goto err; for (i = 1; i < 100; i += 13) { x = ossl_rand_uniform_uint32(ctx, i, &err); if (!TEST_int_eq(err, 0) || !TEST_uint_ge(x, 0) || !TEST_uint_lt(x, i)) return 0; } for (i = 1; i < 100; i += 17) for (j = i + 1; j < 150; j += 11) { x = ossl_rand_range_uint32(ctx, i, j, &err); if (!TEST_int_eq(err, 0) || !TEST_uint_ge(x, i) || !TEST_uint_lt(x, j)) return 0; } res = 1; err: OSSL_LIB_CTX_free(ctx); return res; } int setup_tests(void) { if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", NULL, NULL, NULL))) return 0; ADD_TEST(test_rand); ADD_TEST(test_rand_uniform); return 1; }
test
openssl/test/rand_test.c
openssl
#include "internal/e_os.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/x509.h> #include "testutil.h" #include "testutil/output.h" #ifndef OPENSSL_NO_LOCALE # include <locale.h> # ifdef OPENSSL_SYS_MACOSX # include <xlocale.h> # endif int setup_tests(void) { const unsigned char der_bytes[] = { 0x30, 0x82, 0x03, 0x09, 0x30, 0x82, 0x01, 0xf1, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x08, 0xe0, 0x8c, 0xd3, 0xf3, 0xbf, 0x2c, 0xf2, 0x0d, 0x0a, 0x75, 0xd1, 0xe8, 0xea, 0xbe, 0x70, 0x61, 0xd9, 0x67, 0xf9, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x34, 0x31, 0x31, 0x31, 0x34, 0x31, 0x39, 0x35, 0x37, 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x35, 0x31, 0x31, 0x31, 0x34, 0x31, 0x39, 0x35, 0x37, 0x5a, 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 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, 0xc3, 0x1f, 0x5c, 0x56, 0x46, 0x8d, 0x69, 0xb6, 0x48, 0x3c, 0xbf, 0xe2, 0x0f, 0xa7, 0x4a, 0x44, 0x72, 0x74, 0x36, 0xfe, 0xe8, 0x2f, 0x10, 0x4a, 0xe9, 0x46, 0x45, 0x72, 0x5e, 0x48, 0xdd, 0x75, 0xab, 0xd9, 0x63, 0x91, 0x37, 0x93, 0x46, 0x28, 0x7e, 0x45, 0x94, 0x4b, 0x8a, 0xd5, 0x05, 0x2b, 0x9a, 0x01, 0x96, 0x30, 0xde, 0xcc, 0x14, 0x2d, 0x06, 0x09, 0x1b, 0x7d, 0x50, 0x14, 0x99, 0x36, 0x6b, 0x97, 0x6e, 0xc9, 0xb1, 0x69, 0x70, 0xcd, 0x9b, 0x74, 0x24, 0x9a, 0xe2, 0xd4, 0xc0, 0x1e, 0xbc, 0xec, 0xf6, 0x7a, 0xbb, 0xa0, 0x53, 0x93, 0xf8, 0x68, 0x9a, 0x18, 0xa1, 0xa1, 0x5c, 0x47, 0x93, 0xd1, 0x4c, 0x36, 0x8c, 0x00, 0xb3, 0x66, 0xda, 0xf1, 0x05, 0xb2, 0x3a, 0xad, 0x7e, 0x4b, 0xf3, 0xd3, 0x93, 0xfa, 0x59, 0x09, 0x9c, 0x60, 0x37, 0x69, 0x61, 0xe8, 0x5a, 0x33, 0xc6, 0xb2, 0x1a, 0xba, 0x36, 0xe2, 0xb3, 0x58, 0xe9, 0x73, 0x01, 0x2d, 0x36, 0x48, 0x36, 0x94, 0xe4, 0xb2, 0xa4, 0x5b, 0xdf, 0x3d, 0x5f, 0x62, 0x9f, 0xd9, 0xf3, 0x24, 0x0c, 0xf0, 0x2f, 0x71, 0x44, 0x79, 0x13, 0x70, 0x95, 0xa7, 0xbe, 0xea, 0x0a, 0x08, 0x0a, 0xa6, 0x4b, 0xe9, 0x58, 0x6b, 0xa4, 0xc2, 0xed, 0x74, 0x1e, 0xb0, 0x3b, 0x59, 0xd5, 0xe6, 0xdb, 0x8f, 0x58, 0x6a, 0xa3, 0x7d, 0x52, 0x40, 0xec, 0x72, 0xb7, 0xba, 0x7e, 0x30, 0x9d, 0x12, 0x57, 0xf2, 0x48, 0xae, 0x80, 0x0d, 0x0a, 0xf4, 0xfd, 0x24, 0xed, 0xd8, 0x05, 0xb2, 0x96, 0x44, 0x02, 0x3e, 0x6e, 0x25, 0xb0, 0xc4, 0x93, 0xda, 0xfe, 0x78, 0xd9, 0xbb, 0xd2, 0x71, 0x69, 0x70, 0x7f, 0xba, 0xf7, 0xb0, 0x4f, 0x14, 0xf7, 0x98, 0x71, 0x01, 0x6c, 0xec, 0x6f, 0x76, 0x03, 0x59, 0xff, 0xe2, 0xba, 0x8d, 0xd9, 0x21, 0x08, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x59, 0xb8, 0x6e, 0x1a, 0x72, 0xe9, 0x27, 0x1e, 0xbf, 0x80, 0x87, 0x0f, 0xa9, 0xd0, 0x06, 0x6a, 0x11, 0x30, 0x77, 0x8e, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x59, 0xb8, 0x6e, 0x1a, 0x72, 0xe9, 0x27, 0x1e, 0xbf, 0x80, 0x87, 0x0f, 0xa9, 0xd0, 0x06, 0x6a, 0x11, 0x30, 0x77, 0x8e, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x98, 0x76, 0x9e, 0x3c, 0xfc, 0x3f, 0x58, 0xe8, 0xf2, 0x1f, 0x2e, 0x11, 0xa2, 0x59, 0xfa, 0x27, 0xb5, 0xec, 0x9d, 0x97, 0x05, 0x06, 0x2c, 0x95, 0xa5, 0x28, 0x88, 0x86, 0xeb, 0x4e, 0x8a, 0x62, 0xe9, 0x87, 0x78, 0xd8, 0x18, 0x22, 0x4e, 0xb1, 0x8d, 0x46, 0x4a, 0x4c, 0x6e, 0x7c, 0x53, 0x62, 0x2c, 0xf2, 0x7a, 0x95, 0xa0, 0x1a, 0x30, 0x18, 0x6a, 0x31, 0x6f, 0x3f, 0x55, 0x25, 0x9f, 0x67, 0x60, 0x68, 0x99, 0x0f, 0x41, 0x09, 0xc8, 0xe2, 0x04, 0x33, 0x22, 0x1a, 0xe9, 0xf3, 0xae, 0xce, 0xb6, 0x83, 0x64, 0x78, 0x66, 0x14, 0xc9, 0x54, 0xc8, 0x34, 0x70, 0x96, 0xaf, 0x16, 0xcd, 0xb8, 0xdf, 0x81, 0x7e, 0xf0, 0xa6, 0x7d, 0xc1, 0x13, 0xb2, 0x76, 0x3a, 0xd5, 0x7e, 0x68, 0x8c, 0xd5, 0x00, 0x70, 0x82, 0x23, 0x7e, 0x5e, 0xc9, 0x31, 0x2f, 0x33, 0x54, 0xaa, 0xaf, 0xcd, 0xe9, 0x38, 0x9a, 0x23, 0x53, 0xad, 0x4e, 0x72, 0xa7, 0x6f, 0x47, 0x60, 0xc9, 0xd3, 0x06, 0x9b, 0x7a, 0x21, 0xc6, 0xe9, 0xdb, 0x3c, 0xaa, 0xc0, 0x21, 0x29, 0x5f, 0x44, 0x6a, 0x45, 0x90, 0x73, 0x5e, 0x6d, 0x78, 0x82, 0xcb, 0x42, 0xe6, 0xba, 0x67, 0xb2, 0xe6, 0xa2, 0x15, 0x04, 0xea, 0x69, 0xae, 0x3e, 0xc0, 0x0c, 0x10, 0x99, 0xec, 0xa9, 0xb0, 0x7e, 0xe8, 0x94, 0xe2, 0xf3, 0xaf, 0xf7, 0x9f, 0x65, 0xe7, 0xd7, 0xe2, 0x49, 0xfa, 0x52, 0x7d, 0xb5, 0xfd, 0xa0, 0xa5, 0xe0, 0x49, 0xa7, 0x3d, 0x94, 0x20, 0x2d, 0xec, 0x8c, 0x22, 0xa5, 0xa4, 0x43, 0xfa, 0x7e, 0xd0, 0x50, 0x21, 0xb8, 0x67, 0x18, 0x44, 0x69, 0x8f, 0xdd, 0x47, 0x41, 0xc6, 0x35, 0xe0, 0xe9, 0x2e, 0x41, 0xa9, 0x6f, 0x41, 0xee, 0xb9, 0xbd, 0x45, 0xf3, 0x88, 0xc1, 0x23, 0x35, 0x96, 0xba, 0xf8, 0xcd, 0x4b, 0x83, 0x73, 0x5f }; char str1[] = "SubjectPublicKeyInfo", str2[] = "subjectpublickeyinfo"; int res; X509 *cert = NULL; X509_PUBKEY *cert_pubkey = NULL; const unsigned char *p = der_bytes; if (setlocale(LC_ALL, "") == NULL) return TEST_skip("Cannot set the locale necessary for test"); res = strcasecmp(str1, str2); TEST_note("Case-insensitive comparison via strcasecmp in current locale %s\n", res ? "failed" : "succeeded"); if (!TEST_false(OPENSSL_strcasecmp(str1, str2))) return 0; cert = d2i_X509(NULL, &p, sizeof(der_bytes)); if (!TEST_ptr(cert)) return 0; cert_pubkey = X509_get_X509_PUBKEY(cert); if (!TEST_ptr(cert_pubkey)) { X509_free(cert); return 0; } if (!TEST_ptr(X509_PUBKEY_get0(cert_pubkey))) { X509_free(cert); return 0; } X509_free(cert); return 1; } #else int setup_tests(void) { return TEST_skip("Locale support not available"); } #endif void cleanup_tests(void) { }
test
openssl/test/localetest.c
openssl
#include <openssl/pem.h> #include <openssl/evp.h> #include "testutil.h" static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *libprov = NULL; static const char *filename = NULL; static pem_password_cb passcb; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_PROVIDER_NAME, OPT_TEST_ENUM } OPTION_CHOICE; 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" }, { "provider", OPT_PROVIDER_NAME, 's', "The provider to load (The default value is 'default')" }, { OPT_HELP_STR, 1, '-', "file\tFile to decode.\n" }, { NULL } }; return test_options; } static int passcb(char *buf, int size, int rwflag, void *userdata) { strcpy(buf, "pass"); return strlen(buf); } static int test_decode_nonfipsalg(void) { int ret = 0; EVP_PKEY *privkey = NULL; BIO *bio = NULL; EVP_default_properties_enable_fips(libctx, 1); if (!TEST_ptr(bio = BIO_new_file(filename, "r"))) goto err; if (!TEST_ptr_null(PEM_read_bio_PrivateKey_ex(bio, &privkey, &passcb, NULL, libctx, NULL))) goto err; if (!TEST_ptr_null(PEM_read_bio_PrivateKey_ex(bio, &privkey, &passcb, NULL, libctx, "?fips=true"))) goto err; ret = 1; err: BIO_free(bio); EVP_PKEY_free(privkey); return ret; } int setup_tests(void) { const char *prov_name = "default"; char *config_file = NULL; OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_PROVIDER_NAME: prov_name = opt_arg(); break; case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } filename = test_get_argument(0); if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name)) return 0; ADD_TEST(test_decode_nonfipsalg); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(libprov); OSSL_LIB_CTX_free(libctx); OSSL_PROVIDER_unload(nullprov); }
test
openssl/test/decoder_propq_test.c
openssl
#include <string.h> #include <openssl/bn.h> #include <openssl/core.h> #include <openssl/err.h> #include <openssl/params.h> #include "internal/numbers.h" #include "internal/nelem.h" #include "testutil.h" struct object_st { int p1; double p2; BIGNUM *p3; char *p4; size_t p4_l; char p5[256]; size_t p5_l; const char *p6; size_t p6_l; }; #define p1_init 42 #define p2_init 6.283 #define p3_init \ "4142434445464748494a4b4c4d4e4f50" \ "5152535455565758595a616263646566" \ "6768696a6b6c6d6e6f70717273747576" \ "7778797a30313233343536373839" #define p4_init "BLAKE2s256" #define p5_init "Hellow World" #define p6_init OPENSSL_FULL_VERSION_STR static void cleanup_object(void *vobj) { struct object_st *obj = vobj; BN_free(obj->p3); obj->p3 = NULL; OPENSSL_free(obj->p4); obj->p4 = NULL; OPENSSL_free(obj); } static void *init_object(void) { struct object_st *obj; if (!TEST_ptr(obj = OPENSSL_zalloc(sizeof(*obj)))) return NULL; obj->p1 = p1_init; obj->p2 = p2_init; if (!TEST_true(BN_hex2bn(&obj->p3, p3_init))) goto fail; if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init))) goto fail; strcpy(obj->p5, p5_init); obj->p6 = p6_init; return obj; fail: cleanup_object(obj); obj = NULL; return NULL; } static int raw_set_params(void *vobj, const OSSL_PARAM *params) { struct object_st *obj = vobj; for (; params->key != NULL; params++) if (strcmp(params->key, "p1") == 0) { obj->p1 = *(int *)params->data; } else if (strcmp(params->key, "p2") == 0) { obj->p2 = *(double *)params->data; } else if (strcmp(params->key, "p3") == 0) { BN_free(obj->p3); if (!TEST_ptr(obj->p3 = BN_native2bn(params->data, params->data_size, NULL))) return 0; } else if (strcmp(params->key, "p4") == 0) { OPENSSL_free(obj->p4); if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data, params->data_size))) return 0; obj->p4_l = strlen(obj->p4); } else if (strcmp(params->key, "p5") == 0) { size_t data_length = OPENSSL_strnlen(params->data, params->data_size); if (!TEST_size_t_lt(data_length, sizeof(obj->p5))) return 0; strncpy(obj->p5, params->data, data_length); obj->p5[data_length] = '\0'; obj->p5_l = strlen(obj->p5); } else if (strcmp(params->key, "p6") == 0) { obj->p6 = *(const char **)params->data; obj->p6_l = params->data_size; } return 1; } static int raw_get_params(void *vobj, OSSL_PARAM *params) { struct object_st *obj = vobj; for (; params->key != NULL; params++) if (strcmp(params->key, "p1") == 0) { params->return_size = sizeof(obj->p1); *(int *)params->data = obj->p1; } else if (strcmp(params->key, "p2") == 0) { params->return_size = sizeof(obj->p2); *(double *)params->data = obj->p2; } else if (strcmp(params->key, "p3") == 0) { params->return_size = BN_num_bytes(obj->p3); if (!TEST_size_t_ge(params->data_size, params->return_size)) return 0; BN_bn2nativepad(obj->p3, params->data, params->return_size); } else if (strcmp(params->key, "p4") == 0) { params->return_size = strlen(obj->p4); if (!TEST_size_t_gt(params->data_size, params->return_size)) return 0; strcpy(params->data, obj->p4); } else if (strcmp(params->key, "p5") == 0) { params->return_size = strlen(obj->p5); if (!TEST_size_t_gt(params->data_size, params->return_size)) return 0; strcpy(params->data, obj->p5); } else if (strcmp(params->key, "p6") == 0) { params->return_size = strlen(obj->p6); *(const char **)params->data = obj->p6; } return 1; } static int api_set_params(void *vobj, const OSSL_PARAM *params) { struct object_st *obj = vobj; const OSSL_PARAM *p = NULL; if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1))) return 0; if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2))) return 0; if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3))) return 0; if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) { OPENSSL_free(obj->p4); obj->p4 = NULL; if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0))) return 0; } if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) { char *p5_ptr = obj->p5; if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5)))) return 0; obj->p5_l = strlen(obj->p5); } if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) { if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6))) return 0; obj->p6_l = strlen(obj->p6); } return 1; } static int api_get_params(void *vobj, OSSL_PARAM *params) { struct object_st *obj = vobj; OSSL_PARAM *p = NULL; if ((p = OSSL_PARAM_locate(params, "p1")) != NULL && !TEST_true(OSSL_PARAM_set_int(p, obj->p1))) return 0; if ((p = OSSL_PARAM_locate(params, "p2")) != NULL && !TEST_true(OSSL_PARAM_set_double(p, obj->p2))) return 0; if ((p = OSSL_PARAM_locate(params, "p3")) != NULL && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3))) return 0; if ((p = OSSL_PARAM_locate(params, "p4")) != NULL && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4))) return 0; if ((p = OSSL_PARAM_locate(params, "p5")) != NULL && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5))) return 0; if ((p = OSSL_PARAM_locate(params, "p6")) != NULL && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6))) return 0; return 1; } struct provider_dispatch_st { int (*set_params)(void *obj, const OSSL_PARAM *params); int (*get_params)(void *obj, OSSL_PARAM *params); }; static const struct provider_dispatch_st provider_raw = { raw_set_params, raw_get_params }; static const struct provider_dispatch_st provider_api = { api_set_params, api_get_params }; static int app_p1; static double app_p2; static BIGNUM *app_p3 = NULL; static unsigned char bignumbin[4096]; static char app_p4[256]; static char app_p5[256]; static const char *app_p6 = NULL; static unsigned char foo[1]; #define app_p1_init 17 #define app_p2_init 47.11 #define app_p3_init "deadbeef" #define app_p4_init "Hello" #define app_p5_init "World" #define app_p6_init "Cookie" #define app_foo_init 'z' static int cleanup_app_variables(void) { BN_free(app_p3); app_p3 = NULL; return 1; } static int init_app_variables(void) { int l = 0; cleanup_app_variables(); app_p1 = app_p1_init; app_p2 = app_p2_init; if (!BN_hex2bn(&app_p3, app_p3_init) || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0) return 0; strcpy(app_p4, app_p4_init); strcpy(app_p5, app_p5_init); app_p6 = app_p6_init; foo[0] = app_foo_init; return 1; } static OSSL_PARAM static_raw_params[] = { { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 }, { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 }, { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 }, { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 }, { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 }, { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 }, { NULL, 0, NULL, 0, 0 } }; static OSSL_PARAM static_api_params[] = { OSSL_PARAM_int("p1", &app_p1), OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)), OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)), OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)), OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1), OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)), OSSL_PARAM_END }; static OSSL_PARAM *construct_api_params(void) { size_t n = 0; static OSSL_PARAM params[10]; params[n++] = OSSL_PARAM_construct_int("p1", &app_p1); params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin)); params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4, sizeof(app_p4)); params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5, sizeof(app_p5)); params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6, sizeof(app_p6_init)); params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo)); params[n++] = OSSL_PARAM_construct_end(); return params; } struct param_owner_st { OSSL_PARAM *static_params; OSSL_PARAM *(*constructed_params)(void); }; static const struct param_owner_st raw_params = { static_raw_params, NULL }; static const struct param_owner_st api_params = { static_api_params, construct_api_params }; static struct { const struct provider_dispatch_st *prov; const struct param_owner_st *app; const char *desc; } test_cases[] = { { &provider_raw, &raw_params, "raw provider vs raw params" }, { &provider_api, &api_params, "api provider vs api params" }, { &provider_raw, &api_params, "raw provider vs api params" }, { &provider_api, &raw_params, "api provider vs raw params" }, }; static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov) { BIGNUM *verify_p3 = NULL; void *obj = NULL; int errcnt = 0; OSSL_PARAM *p; if (!TEST_ptr(obj = init_object()) || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) { errcnt++; goto fin; } init_app_variables(); if (!TEST_true(prov->get_params(obj, params)) || !TEST_int_eq(app_p1, p1_init) || !TEST_double_eq(app_p2, app_p2_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3")) || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3)) || !TEST_BN_eq(app_p3, verify_p3) || !TEST_str_eq(app_p4, p4_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5")) || !TEST_size_t_eq(p->return_size, sizeof(p5_init) - 1) || !TEST_str_eq(app_p5, p5_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6")) || !TEST_size_t_eq(p->return_size, sizeof(p6_init) - 1) || !TEST_str_eq(app_p6, p6_init) || !TEST_char_eq(foo[0], app_foo_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo"))) errcnt++; init_app_variables(); if (!TEST_true(prov->set_params(obj, params))) { errcnt++; } else { struct object_st *sneakpeek = obj; if (!TEST_int_eq(sneakpeek->p1, app_p1) || !TEST_double_eq(sneakpeek->p2, p2_init) || !TEST_BN_eq(sneakpeek->p3, app_p3) || !TEST_str_eq(sneakpeek->p4, app_p4) || !TEST_str_eq(sneakpeek->p5, app_p5) || !TEST_str_eq(sneakpeek->p6, app_p6)) errcnt++; } BN_free(verify_p3); verify_p3 = NULL; if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) { errcnt++; goto fin; } if (!TEST_true(prov->get_params(obj, params)) || !TEST_int_eq(app_p1, app_p1_init) || !TEST_double_eq(app_p2, app_p2_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3")) || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3)) || !TEST_BN_eq(app_p3, verify_p3) || !TEST_str_eq(app_p4, app_p4_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5")) || !TEST_size_t_eq(p->return_size, sizeof(app_p5_init) - 1) || !TEST_str_eq(app_p5, app_p5_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6")) || !TEST_size_t_eq(p->return_size, sizeof(app_p6_init) - 1) || !TEST_str_eq(app_p6, app_p6_init) || !TEST_char_eq(foo[0], app_foo_init) || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo"))) errcnt++; fin: BN_free(verify_p3); verify_p3 = NULL; cleanup_app_variables(); cleanup_object(obj); return errcnt == 0; } static int test_case(int i) { TEST_info("Case: %s", test_cases[i].desc); return test_case_variant(test_cases[i].app->static_params, test_cases[i].prov) && (test_cases[i].app->constructed_params == NULL || test_case_variant(test_cases[i].app->constructed_params(), test_cases[i].prov)); } static const OSSL_PARAM params_from_text[] = { OSSL_PARAM_int32("int", NULL), OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)), OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)), OSSL_PARAM_DEFN("num", OSSL_PARAM_INTEGER, NULL, 0), OSSL_PARAM_DEFN("unum", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0), OSSL_PARAM_DEFN("octets", OSSL_PARAM_OCTET_STRING, NULL, 0), OSSL_PARAM_END, }; struct int_from_text_test_st { const char *argname; const char *strval; long int expected_intval; int expected_res; size_t expected_bufsize; }; static struct int_from_text_test_st int_from_text_test_cases[] = { { "int", "", 0, 0, 0 }, { "int", "0", 0, 1, 4 }, { "int", "101", 101, 1, 4 }, { "int", "-102", -102, 1, 4 }, { "int", "12A", 12, 1, 4 }, { "int", "0x12B", 0x12B, 1, 4 }, { "hexint", "12C", 0x12C, 1, 4 }, { "hexint", "0x12D", 0, 1, 4 }, { "int", "0x7fffffff", INT32_MAX, 1, 4 }, { "int", "2147483647", INT32_MAX, 1, 4 }, { "int", "2147483648", 0, 0, 0 }, { "int", "-2147483648", INT32_MIN, 1, 4 }, { "int", "-2147483649", 0, 0, 4 }, { "short", "0x7fff", INT16_MAX, 1, 2 }, { "short", "32767", INT16_MAX, 1, 2 }, { "short", "32768", 0, 0, 0 }, { "ushort", "0xffff", UINT16_MAX, 1, 2 }, { "ushort", "65535", UINT16_MAX, 1, 2 }, { "ushort", "65536", 0, 0, 0 }, { "num", "0", 0, 1, 1 }, { "num", "0", 0, 1, 1 }, { "num", "0xff", 0xff, 1, 2 }, { "num", "-0xff", -0xff, 1, 2 }, { "num", "0x7f", 0x7f, 1, 1 }, { "num", "-0x7f", -0x7f, 1, 1 }, { "num", "0x80", 0x80, 1, 2 }, { "num", "-0x80", -0x80, 1, 1 }, { "num", "0x81", 0x81, 1, 2 }, { "num", "-0x81", -0x81, 1, 2 }, { "unum", "0xff", 0xff, 1, 1 }, { "unum", "-0xff", -0xff, 0, 0 }, { "unum", "0x7f", 0x7f, 1, 1 }, { "unum", "-0x7f", -0x7f, 0, 0 }, { "unum", "0x80", 0x80, 1, 1 }, { "unum", "-0x80", -0x80, 0, 0 }, { "unum", "0x81", 0x81, 1, 1 }, { "unum", "-0x81", -0x81, 0, 0 }, }; static int check_int_from_text(const struct int_from_text_test_st a) { OSSL_PARAM param; long int val = 0; int res; if (!OSSL_PARAM_allocate_from_text(&param, params_from_text, a.argname, a.strval, 0, NULL)) { if (a.expected_res) TEST_error("unexpected OSSL_PARAM_allocate_from_text() return for %s \"%s\"", a.argname, a.strval); return !a.expected_res; } if (param.data_size == 0) { OPENSSL_free(param.data); TEST_error("unexpected zero size for %s \"%s\"", a.argname, a.strval); return 0; } res = OSSL_PARAM_get_long(&param, &val); OPENSSL_free(param.data); if (res ^ a.expected_res) { TEST_error("unexpected OSSL_PARAM_get_long() return for %s \"%s\": " "%d != %d", a.argname, a.strval, a.expected_res, res); return 0; } if (val != a.expected_intval) { TEST_error("unexpected result for %s \"%s\": %li != %li", a.argname, a.strval, a.expected_intval, val); return 0; } if (param.data_size != a.expected_bufsize) { TEST_error("unexpected size for %s \"%s\": %d != %d", a.argname, a.strval, (int)a.expected_bufsize, (int)param.data_size); return 0; } return a.expected_res; } static int check_octetstr_from_hexstr(void) { OSSL_PARAM param; static const char *values[] = { "", "F", "FF", "FFF", "FFFF", NULL }; int i; int errcnt = 0; for (i = 0; values[i] != NULL; i++) { int expected = (strlen(values[i]) % 2) != 1; int result; ERR_clear_error(); memset(&param, 0, sizeof(param)); if (expected) result = TEST_true(OSSL_PARAM_allocate_from_text(&param, params_from_text, "hexoctets", values[i], 0, NULL)); else result = TEST_false(OSSL_PARAM_allocate_from_text(&param, params_from_text, "hexoctets", values[i], 0, NULL)); if (!result) { TEST_error("unexpected OSSL_PARAM_allocate_from_text() %s for 'octets' \"%s\"", (expected ? "failure" : "success"), values[i]); errcnt++; } OPENSSL_free(param.data); } return errcnt == 0; } static int test_allocate_from_text(int i) { return check_int_from_text(int_from_text_test_cases[i]); } static int test_more_allocate_from_text(void) { return check_octetstr_from_hexstr(); } int setup_tests(void) { ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases)); ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases)); ADD_TEST(test_more_allocate_from_text); return 1; }
test
openssl/test/params_test.c
openssl
#include "internal/deprecated.h" #include <openssl/rsa.h> #include <openssl/bn.h> #include "crypto/rsa.h" #include "testutil.h" static OSSL_PROVIDER *prov_null = NULL; static OSSL_LIB_CTX *libctx = NULL; static int test_rsa_x931_keygen(void) { int ret = 0; BIGNUM *e = NULL; RSA *rsa = NULL; ret = TEST_ptr(rsa = ossl_rsa_new_with_ctx(libctx)) && TEST_ptr(e = BN_new()) && TEST_int_eq(BN_set_word(e, RSA_F4), 1) && TEST_int_eq(RSA_X931_generate_key_ex(rsa, 1024, e, NULL), 1); BN_free(e); RSA_free(rsa); return ret; } int setup_tests(void) { if (!test_get_libctx(&libctx, &prov_null, NULL, NULL, NULL)) return 0; ADD_TEST(test_rsa_x931_keygen); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(prov_null); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/rsa_x931_test.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/crypto.h> #include <openssl/aes.h> #include <openssl/rand.h> #include <stdio.h> #include <string.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_DEPRECATED_3_0 # define TEST_SIZE 128 # define BIG_TEST_SIZE 10240 # if BIG_TEST_SIZE < TEST_SIZE # error BIG_TEST_SIZE is smaller than TEST_SIZE # endif static unsigned char rkey[16]; static unsigned char rkey2[16]; static unsigned char plaintext[BIG_TEST_SIZE]; static unsigned char saved_iv[AES_BLOCK_SIZE * 4]; # define MAX_VECTOR_SIZE 64 struct ige_test { const unsigned char key[16]; const unsigned char iv[32]; const unsigned char in[MAX_VECTOR_SIZE]; const unsigned char out[MAX_VECTOR_SIZE]; const size_t length; const int encrypt; }; static struct ige_test const ige_test_vectors[] = { {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x1a, 0x85, 0x19, 0xa6, 0x55, 0x7b, 0xe6, 0x52, 0xe9, 0xda, 0x8e, 0x43, 0xda, 0x4e, 0xf4, 0x45, 0x3c, 0xf4, 0x56, 0xb4, 0xca, 0x48, 0x8a, 0xa3, 0x83, 0xc7, 0x9c, 0x98, 0xb3, 0x47, 0x97, 0xcb}, 32, AES_ENCRYPT}, {{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x65}, {0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x49, 0x47, 0x45, 0x20, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53}, {0x4c, 0x2e, 0x20, 0x4c, 0x65, 0x74, 0x27, 0x73, 0x20, 0x68, 0x6f, 0x70, 0x65, 0x20, 0x42, 0x65, 0x6e, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x69, 0x74, 0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x21, 0x0a}, {0x99, 0x70, 0x64, 0x87, 0xa1, 0xcd, 0xe6, 0x13, 0xbc, 0x6d, 0xe0, 0xb6, 0xf2, 0x4b, 0x1c, 0x7a, 0xa4, 0x48, 0xc8, 0xb9, 0xc3, 0x40, 0x3e, 0x34, 0x67, 0xa8, 0xca, 0xd8, 0x93, 0x40, 0xf5, 0x3b}, 32, AES_DECRYPT}, }; struct bi_ige_test { const unsigned char key1[32]; const unsigned char key2[32]; const unsigned char iv[64]; const unsigned char in[MAX_VECTOR_SIZE]; const unsigned char out[MAX_VECTOR_SIZE]; const size_t keysize; const size_t length; const int encrypt; }; static struct bi_ige_test const bi_ige_test_vectors[] = { {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x14, 0x40, 0x6f, 0xae, 0xa2, 0x79, 0xf2, 0x56, 0x1f, 0x86, 0xeb, 0x3b, 0x7d, 0xff, 0x53, 0xdc, 0x4e, 0x27, 0x0c, 0x03, 0xde, 0x7c, 0xe5, 0x16, 0x6a, 0x9c, 0x20, 0x33, 0x9d, 0x33, 0xfe, 0x12}, 16, 32, AES_ENCRYPT}, {{0x58, 0x0a, 0x06, 0xe9, 0x97, 0x07, 0x59, 0x5c, 0x9e, 0x19, 0xd2, 0xa7, 0xbb, 0x40, 0x2b, 0x7a, 0xc7, 0xd8, 0x11, 0x9e, 0x4c, 0x51, 0x35, 0x75, 0x64, 0x28, 0x0f, 0x23, 0xad, 0x74, 0xac, 0x37}, {0xd1, 0x80, 0xa0, 0x31, 0x47, 0xa3, 0x11, 0x13, 0x86, 0x26, 0x9e, 0x6d, 0xff, 0xaf, 0x72, 0x74, 0x5b, 0xa2, 0x35, 0x81, 0xd2, 0xa6, 0x3d, 0x21, 0x67, 0x7b, 0x58, 0xa8, 0x18, 0xf9, 0x72, 0xe4}, {0x80, 0x3d, 0xbd, 0x4c, 0xe6, 0x7b, 0x06, 0xa9, 0x53, 0x35, 0xd5, 0x7e, 0x71, 0xc1, 0x70, 0x70, 0x74, 0x9a, 0x00, 0x28, 0x0c, 0xbf, 0x6c, 0x42, 0x9b, 0xa4, 0xdd, 0x65, 0x11, 0x77, 0x7c, 0x67, 0xfe, 0x76, 0x0a, 0xf0, 0xd5, 0xc6, 0x6e, 0x6a, 0xe7, 0x5e, 0x4c, 0xf2, 0x7e, 0x9e, 0xf9, 0x20, 0x0e, 0x54, 0x6f, 0x2d, 0x8a, 0x8d, 0x7e, 0xbd, 0x48, 0x79, 0x37, 0x99, 0xff, 0x27, 0x93, 0xa3}, {0xf1, 0x54, 0x3d, 0xca, 0xfe, 0xb5, 0xef, 0x1c, 0x4f, 0xa6, 0x43, 0xf6, 0xe6, 0x48, 0x57, 0xf0, 0xee, 0x15, 0x7f, 0xe3, 0xe7, 0x2f, 0xd0, 0x2f, 0x11, 0x95, 0x7a, 0x17, 0x00, 0xab, 0xa7, 0x0b, 0xbe, 0x44, 0x09, 0x9c, 0xcd, 0xac, 0xa8, 0x52, 0xa1, 0x8e, 0x7b, 0x75, 0xbc, 0xa4, 0x92, 0x5a, 0xab, 0x46, 0xd3, 0x3a, 0xa0, 0xd5, 0x35, 0x1c, 0x55, 0xa4, 0xb3, 0xa8, 0x40, 0x81, 0xa5, 0x0b}, {0x42, 0xe5, 0x28, 0x30, 0x31, 0xc2, 0xa0, 0x23, 0x68, 0x49, 0x4e, 0xb3, 0x24, 0x59, 0x92, 0x79, 0xc1, 0xa5, 0xcc, 0xe6, 0x76, 0x53, 0xb1, 0xcf, 0x20, 0x86, 0x23, 0xe8, 0x72, 0x55, 0x99, 0x92, 0x0d, 0x16, 0x1c, 0x5a, 0x2f, 0xce, 0xcb, 0x51, 0xe2, 0x67, 0xfa, 0x10, 0xec, 0xcd, 0x3d, 0x67, 0xa5, 0xe6, 0xf7, 0x31, 0x26, 0xb0, 0x0d, 0x76, 0x5e, 0x28, 0xdc, 0x7f, 0x01, 0xc5, 0xa5, 0x4c}, 32, 64, AES_ENCRYPT}, }; static int test_ige_vectors(int n) { const struct ige_test *const v = &ige_test_vectors[n]; AES_KEY key; unsigned char buf[MAX_VECTOR_SIZE]; unsigned char iv[AES_BLOCK_SIZE * 2]; int testresult = 1; if (!TEST_int_le(v->length, MAX_VECTOR_SIZE)) return 0; if (v->encrypt == AES_ENCRYPT) AES_set_encrypt_key(v->key, 8 * sizeof(v->key), &key); else AES_set_decrypt_key(v->key, 8 * sizeof(v->key), &key); memcpy(iv, v->iv, sizeof(iv)); AES_ige_encrypt(v->in, buf, v->length, &key, iv, v->encrypt); if (!TEST_mem_eq(v->out, v->length, buf, v->length)) { TEST_info("IGE test vector %d failed", n); test_output_memory("key", v->key, sizeof(v->key)); test_output_memory("iv", v->iv, sizeof(v->iv)); test_output_memory("in", v->in, v->length); testresult = 0; } memcpy(iv, v->iv, sizeof(iv)); memcpy(buf, v->in, v->length); AES_ige_encrypt(buf, buf, v->length, &key, iv, v->encrypt); if (!TEST_mem_eq(v->out, v->length, buf, v->length)) { TEST_info("IGE test vector %d failed (with in == out)", n); test_output_memory("key", v->key, sizeof(v->key)); test_output_memory("iv", v->iv, sizeof(v->iv)); test_output_memory("in", v->in, v->length); testresult = 0; } return testresult; } static int test_bi_ige_vectors(int n) { const struct bi_ige_test *const v = &bi_ige_test_vectors[n]; AES_KEY key1; AES_KEY key2; unsigned char buf[MAX_VECTOR_SIZE]; if (!TEST_int_le(v->length, MAX_VECTOR_SIZE)) return 0; if (v->encrypt == AES_ENCRYPT) { AES_set_encrypt_key(v->key1, 8 * v->keysize, &key1); AES_set_encrypt_key(v->key2, 8 * v->keysize, &key2); } else { AES_set_decrypt_key(v->key1, 8 * v->keysize, &key1); AES_set_decrypt_key(v->key2, 8 * v->keysize, &key2); } AES_bi_ige_encrypt(v->in, buf, v->length, &key1, &key2, v->iv, v->encrypt); if (!TEST_mem_eq(v->out, v->length, buf, v->length)) { test_output_memory("key 1", v->key1, sizeof(v->key1)); test_output_memory("key 2", v->key2, sizeof(v->key2)); test_output_memory("iv", v->iv, sizeof(v->iv)); test_output_memory("in", v->in, v->length); return 0; } return 1; } static int test_ige_enc_dec(void) { AES_KEY key; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; memcpy(iv, saved_iv, sizeof(iv)); AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, iv, AES_ENCRYPT); AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT); return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE); } static int test_ige_enc_chaining(void) { AES_KEY key; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv, AES_ENCRYPT); AES_ige_encrypt(plaintext + TEST_SIZE / 2, ciphertext + TEST_SIZE / 2, TEST_SIZE / 2, &key, iv, AES_ENCRYPT); AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT); return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE); } static int test_ige_dec_chaining(void) { AES_KEY key; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv, AES_ENCRYPT); AES_ige_encrypt(plaintext + TEST_SIZE / 2, ciphertext + TEST_SIZE / 2, TEST_SIZE / 2, &key, iv, AES_ENCRYPT); AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(ciphertext, checktext, TEST_SIZE / 2, &key, iv, AES_DECRYPT); AES_ige_encrypt(ciphertext + TEST_SIZE / 2, checktext + TEST_SIZE / 2, TEST_SIZE / 2, &key, iv, AES_DECRYPT); return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE); } static int test_ige_garble_forwards(void) { AES_KEY key; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; unsigned int n; int testresult = 1; const size_t ctsize = sizeof(checktext); size_t matches; AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv, AES_ENCRYPT); ++ciphertext[sizeof(ciphertext) / 2]; AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); memcpy(iv, saved_iv, sizeof(iv)); AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv, AES_DECRYPT); matches = 0; for (n = 0; n < sizeof(checktext); ++n) if (checktext[n] == plaintext[n]) ++matches; if (!TEST_size_t_le(matches, ctsize / 2 + ctsize / 100)) testresult = 0; if (!TEST_size_t_gt(matches, ctsize / 2)) testresult = 0; return testresult; } static int test_bi_ige_enc_dec(void) { AES_KEY key, key2; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; memcpy(iv, saved_iv, sizeof(iv)); AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_bi_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, &key2, iv, AES_ENCRYPT); AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_bi_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, &key2, iv, AES_DECRYPT); return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE); } static int test_bi_ige_garble1(void) { AES_KEY key, key2; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; unsigned int n; size_t matches; memcpy(iv, saved_iv, sizeof(iv)); AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv, AES_ENCRYPT); ++ciphertext[sizeof(ciphertext) / 2]; AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv, AES_DECRYPT); matches = 0; for (n = 0; n < sizeof(checktext); ++n) if (checktext[n] == plaintext[n]) ++matches; return TEST_size_t_le(matches, sizeof(checktext) / 100); } static int test_bi_ige_garble2(void) { AES_KEY key, key2; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; unsigned int n; size_t matches; memcpy(iv, saved_iv, sizeof(iv)); AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv, AES_ENCRYPT); ++ciphertext[sizeof(ciphertext) - 1]; AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv, AES_DECRYPT); matches = 0; for (n = 0; n < sizeof(checktext); ++n) if (checktext[n] == plaintext[n]) ++matches; return TEST_size_t_le(matches, sizeof(checktext) / 100); } static int test_bi_ige_garble3(void) { AES_KEY key, key2; unsigned char iv[AES_BLOCK_SIZE * 4]; unsigned char ciphertext[BIG_TEST_SIZE]; unsigned char checktext[BIG_TEST_SIZE]; unsigned int n; size_t matches; memcpy(iv, saved_iv, sizeof(iv)); AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv, AES_ENCRYPT); ++ciphertext[0]; AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key); AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2); AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv, AES_DECRYPT); matches = 0; for (n = 0; n < sizeof(checktext); ++n) if (checktext[n] == plaintext[n]) ++matches; return TEST_size_t_le(matches, sizeof(checktext) / 100); } #endif int setup_tests(void) { #ifndef OPENSSL_NO_DEPRECATED_3_0 RAND_bytes(rkey, sizeof(rkey)); RAND_bytes(rkey2, sizeof(rkey2)); RAND_bytes(plaintext, sizeof(plaintext)); RAND_bytes(saved_iv, sizeof(saved_iv)); ADD_TEST(test_ige_enc_dec); ADD_TEST(test_ige_enc_chaining); ADD_TEST(test_ige_dec_chaining); ADD_TEST(test_ige_garble_forwards); ADD_TEST(test_bi_ige_enc_dec); ADD_TEST(test_bi_ige_garble1); ADD_TEST(test_bi_ige_garble2); ADD_TEST(test_bi_ige_garble3); ADD_ALL_TESTS(test_ige_vectors, OSSL_NELEM(ige_test_vectors)); ADD_ALL_TESTS(test_bi_ige_vectors, OSSL_NELEM(bi_ige_test_vectors)); #endif return 1; }
test
openssl/test/igetest.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/opensslconf.h> # include "testutil.h" #ifdef OPENSSL_NO_SRP # include <stdio.h> #else # include <openssl/srp.h> # include <openssl/rand.h> # include <openssl/err.h> # define RANDOM_SIZE 32 static int run_srp(const char *username, const char *client_pass, const char *server_pass) { int ret = 0; BIGNUM *s = NULL; BIGNUM *v = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *u = NULL; BIGNUM *x = NULL; BIGNUM *Apub = NULL; BIGNUM *Bpub = NULL; BIGNUM *Kclient = NULL; BIGNUM *Kserver = NULL; unsigned char rand_tmp[RANDOM_SIZE]; const SRP_gN *GN; if (!TEST_ptr(GN = SRP_get_default_gN("1024"))) return 0; if (!TEST_true(SRP_create_verifier_BN(username, server_pass, &s, &v, GN->N, GN->g))) goto end; test_output_bignum("N", GN->N); test_output_bignum("g", GN->g); test_output_bignum("Salt", s); test_output_bignum("Verifier", v); RAND_bytes(rand_tmp, sizeof(rand_tmp)); b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL); if (!TEST_BN_ne_zero(b)) goto end; test_output_bignum("b", b); Bpub = SRP_Calc_B(b, GN->N, GN->g, v); test_output_bignum("B", Bpub); if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N))) goto end; RAND_bytes(rand_tmp, sizeof(rand_tmp)); a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL); if (!TEST_BN_ne_zero(a)) goto end; test_output_bignum("a", a); Apub = SRP_Calc_A(a, GN->N, GN->g); test_output_bignum("A", Apub); if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N))) goto end; u = SRP_Calc_u(Apub, Bpub, GN->N); x = SRP_Calc_x(s, username, client_pass); Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u); test_output_bignum("Client's key", Kclient); Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N); test_output_bignum("Server's key", Kserver); if (!TEST_BN_eq(Kclient, Kserver)) goto end; ret = 1; end: BN_clear_free(Kclient); BN_clear_free(Kserver); BN_clear_free(x); BN_free(u); BN_free(Apub); BN_clear_free(a); BN_free(Bpub); BN_clear_free(b); BN_free(s); BN_clear_free(v); return ret; } static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn) { BIGNUM *tmp = NULL; int r; if (!TEST_true(BN_hex2bn(&tmp, hexbn))) return 0; if (BN_cmp(bn, tmp) != 0) TEST_error("unexpected %s value", name); r = TEST_BN_eq(bn, tmp); BN_free(tmp); return r; } static int run_srp_kat(void) { int ret = 0; BIGNUM *s = NULL; BIGNUM *v = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *u = NULL; BIGNUM *x = NULL; BIGNUM *Apub = NULL; BIGNUM *Bpub = NULL; BIGNUM *Kclient = NULL; BIGNUM *Kserver = NULL; const SRP_gN *GN; if (!TEST_ptr(GN = SRP_get_default_gN("1024"))) goto err; BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE"); if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N, GN->g))) goto err; TEST_info("checking v"); if (!TEST_true(check_bn("v", v, "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812" "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5" "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5" "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78" "E955A5E29E7AB245DB2BE315E2099AFB"))) goto err; TEST_note(" okay"); BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1" "05284D20"); Bpub = SRP_Calc_B(b, GN->N, GN->g, v); if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N))) goto err; TEST_info("checking B"); if (!TEST_true(check_bn("B", Bpub, "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011" "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99" "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA" "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE" "EB4012B7D7665238A8E3FB004B117B58"))) goto err; TEST_note(" okay"); BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD" "DA2D4393"); Apub = SRP_Calc_A(a, GN->N, GN->g); if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N))) goto err; TEST_info("checking A"); if (!TEST_true(check_bn("A", Apub, "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4" "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC" "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44" "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA" "B349EF5D76988A3672FAC47B0769447B"))) goto err; TEST_note(" okay"); u = SRP_Calc_u(Apub, Bpub, GN->N); if (!TEST_true(check_bn("u", u, "CE38B9593487DA98554ED47D70A7AE5F462EF019"))) goto err; x = SRP_Calc_x(s, "alice", "password123"); Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u); TEST_info("checking client's key"); if (!TEST_true(check_bn("Client's key", Kclient, "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D" "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C" "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F" "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D" "C346D7E474B29EDE8A469FFECA686E5A"))) goto err; TEST_note(" okay"); Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N); TEST_info("checking server's key"); if (!TEST_true(check_bn("Server's key", Kserver, "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D" "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C" "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F" "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D" "C346D7E474B29EDE8A469FFECA686E5A"))) goto err; TEST_note(" okay"); ret = 1; err: BN_clear_free(Kclient); BN_clear_free(Kserver); BN_clear_free(x); BN_free(u); BN_free(Apub); BN_clear_free(a); BN_free(Bpub); BN_clear_free(b); BN_free(s); BN_clear_free(v); return ret; } static int run_srp_tests(void) { TEST_info("run_srp: expecting a mismatch"); if (!TEST_false(run_srp("alice", "password1", "password2"))) return 0; TEST_info("run_srp: expecting a match"); if (!TEST_true(run_srp("alice", "password", "password"))) return 0; return 1; } #endif int setup_tests(void) { #ifdef OPENSSL_NO_SRP printf("No SRP support\n"); #else ADD_TEST(run_srp_tests); ADD_TEST(run_srp_kat); #endif return 1; }
test
openssl/test/srptest.c
openssl
#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; 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; 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; 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; 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[] = { { {0x00}, 1, 0, 0 }, { {0x01}, 1, 1, 0 }, { {0x02}, 1, 2, 0 }, { {0x03}, 1, 3, 0 }, { {0x04}, 1, 4, 0 }, { {0x05}, 1, 5, 0 }, { {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; }
test
openssl/test/packettest.c
openssl
#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" #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; }
test
openssl/test/sparse_array_test.c
openssl
#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; }
test
openssl/test/rc4test.c
openssl
#include <openssl/evp.h> #include "testutil.h" static int test_no_deflt_ctx_init(void) { int testresult = 0; EVP_MD *md = NULL; OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(ctx)) return 0; md = EVP_MD_fetch(ctx, "SHA2-256", NULL); if (!TEST_ptr(md)) goto err; OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL); if (!TEST_true(OSSL_PROVIDER_available(NULL, "default"))) goto err; if (!TEST_false(OSSL_PROVIDER_available(NULL, "null"))) goto err; testresult = 1; err: EVP_MD_free(md); OSSL_LIB_CTX_free(ctx); return testresult; } int setup_tests(void) { ADD_TEST(test_no_deflt_ctx_init); return 1; }
test
openssl/test/nodefltctxtest.c
openssl
#include <string.h> #include <internal/cryptlib.h> #include <internal/thread_arch.h> #include <internal/thread.h> #include <openssl/thread.h> #include "testutil.h" static int test_thread_reported_flags(void) { uint32_t flags = OSSL_get_thread_support_flags(); #if !defined(OPENSSL_THREADS) if (!TEST_int_eq(flags, 0)) return 0; #endif #if defined(OPENSSL_NO_THREAD_POOL) if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL, 0)) return 0; #else if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL, OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL)) return 0; #endif #if defined(OPENSSL_NO_DEFAULT_THREAD_POOL) if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN, 0)) return 0; #else if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN, OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN)) return 0; #endif return 1; } #ifndef OPENSSL_NO_THREAD_POOL # define TEST_THREAD_NATIVE_FN_SET_VALUE 1 static uint32_t test_thread_native_fn(void *data) { uint32_t *ldata = (uint32_t*) data; *ldata = *ldata + 1; return *ldata - 1; } static int test_thread_native(void) { uint32_t retval; uint32_t local; CRYPTO_THREAD *t; local = 1; t = ossl_crypto_thread_native_start(test_thread_native_fn, &local, 1); if (!TEST_ptr(t)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1)) return 0; if (!TEST_int_eq(retval, 1) || !TEST_int_eq(local, 2)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1)) return 0; t = NULL; if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 0)) return 0; return 1; } # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL) static int test_thread_internal(void) { uint32_t retval[3]; uint32_t local[3] = { 0 }; uint32_t threads_supported; size_t i; void *t[3]; OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new(); threads_supported = OSSL_get_thread_support_flags(); threads_supported &= OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN; if (threads_supported == 0) { if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0)) return 0; if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0)) return 0; if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0)) return 0; t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]); if (!TEST_ptr_null(t[0])) return 0; return 1; } if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0)) return 0; t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]); if (!TEST_ptr_null(t[0])) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0)) return 0; if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1)) return 0; t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]); if (!TEST_ptr_null(t[0])) return 0; if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1)) return 0; if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1)) return 0; if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0)) return 0; for (i = 0; i < OSSL_NELEM(t); ++i) { local[0] = i + 1; t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]); if (!TEST_ptr(t[i])) return 0; if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1)) return 0; if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2)) return 0; if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1)) return 0; t[i] = NULL; if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0)) return 0; } if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1)) return 0; for (i = 0; i < OSSL_NELEM(t); ++i) { local[i] = i + 1; t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]); if (!TEST_ptr(t[i])) return 0; } for (i = 0; i < OSSL_NELEM(t); ++i) { if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1)) return 0; } for (i = 0; i < OSSL_NELEM(t); ++i) { if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2)) return 0; if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1)) return 0; } if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1)) return 0; for (i = 0; i < OSSL_NELEM(t); ++i) { local[i] = i + 1; t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]); if (!TEST_ptr(t[i])) return 0; } for (i = 0; i < OSSL_NELEM(t); ++i) { if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1)) return 0; } for (i = 0; i < OSSL_NELEM(t); ++i) { if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2)) return 0; if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1)) return 0; } if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1)) return 0; OSSL_LIB_CTX_free(cust_ctx); return 1; } # endif static uint32_t test_thread_native_multiple_joins_fn1(void *data) { return 0; } static uint32_t test_thread_native_multiple_joins_fn2(void *data) { ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL); return 0; } static uint32_t test_thread_native_multiple_joins_fn3(void *data) { ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL); return 0; } static int test_thread_native_multiple_joins(void) { CRYPTO_THREAD *t, *t1, *t2; t = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn1, NULL, 1); t1 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn2, t, 1); t2 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn3, t, 1); if (!TEST_ptr(t) || !TEST_ptr(t1) || !TEST_ptr(t2)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_join(t2, NULL), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_join(t1, NULL), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_clean(t2), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_clean(t1), 1)) return 0; if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1)) return 0; return 1; } #endif int setup_tests(void) { ADD_TEST(test_thread_reported_flags); #if !defined(OPENSSL_NO_THREAD_POOL) ADD_TEST(test_thread_native); ADD_TEST(test_thread_native_multiple_joins); # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL) ADD_TEST(test_thread_internal); # endif #endif return 1; }
test
openssl/test/threadpool_test.c
openssl
#include <openssl/objects.h> #include <openssl/crypto.h> #include <openssl/provider.h> #include "testutil.h" static const OSSL_ALGORITHM *obj_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; return NULL; } static const OSSL_DISPATCH obj_dispatch_table[] = { { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))obj_query }, OSSL_DISPATCH_END }; static OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid = NULL; static OSSL_FUNC_core_obj_create_fn *c_obj_create = NULL; #define SIG_OID "1.3.6.1.4.1.16604.998877.1" #define SIG_SN "my-sig" #define SIG_LN "my-sig-long" #define DIGEST_OID "1.3.6.1.4.1.16604.998877.2" #define DIGEST_SN "my-digest" #define DIGEST_LN "my-digest-long" #define SIGALG_OID "1.3.6.1.4.1.16604.998877.3" #define SIGALG_SN "my-sigalg" #define SIGALG_LN "my-sigalg-long" #define NODIG_SIG_OID "1.3.6.1.4.1.16604.998877.4" #define NODIG_SIG_SN "my-nodig-sig" #define NODIG_SIG_LN "my-nodig-sig-long" #define NODIG_SIGALG_OID "1.3.6.1.4.1.16604.998877.5" #define NODIG_SIGALG_SN "my-nodig-sigalg" #define NODIG_SIGALG_LN "my-nodig-sigalg-long" static int obj_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { *provctx = (void *)handle; *out = obj_dispatch_table; for (; in->function_id != 0; in++) { switch (in->function_id) { case OSSL_FUNC_CORE_OBJ_ADD_SIGID: c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in); break; case OSSL_FUNC_CORE_OBJ_CREATE: c_obj_create = OSSL_FUNC_core_obj_create(in); break; break; default: break; } } if (!c_obj_create(handle, DIGEST_OID, DIGEST_SN, DIGEST_LN) || !c_obj_create(handle, SIG_OID, SIG_SN, SIG_LN) || !c_obj_create(handle, SIGALG_OID, SIGALG_SN, SIGALG_LN)) return 0; if (!c_obj_create(handle, NODIG_SIG_OID, NODIG_SIG_SN, NODIG_SIG_LN) || !c_obj_create(handle, NODIG_SIGALG_OID, NODIG_SIGALG_SN, NODIG_SIGALG_LN)) return 0; if (!c_obj_add_sigid(handle, SIGALG_OID, DIGEST_SN, SIG_LN)) return 0; if (!c_obj_add_sigid(handle, NODIG_SIGALG_OID, "", NODIG_SIG_LN)) return 0; if (c_obj_add_sigid(handle, NODIG_SIGALG_OID, "NonsenseAlg", NODIG_SIG_LN)) return 0; return 1; } static int obj_create_test(void) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); OSSL_PROVIDER *objprov = NULL; int sigalgnid, digestnid, signid, foundsid; int testresult = 0; if (!TEST_ptr(libctx)) goto err; if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov", obj_provider_init)) || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov"))) goto err; sigalgnid = OBJ_txt2nid(SIGALG_OID); if (!TEST_int_ne(sigalgnid, NID_undef) || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid)) || !TEST_int_ne(digestnid, NID_undef) || !TEST_int_ne(signid, NID_undef) || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN)) || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN))) goto err; sigalgnid = OBJ_txt2nid(NODIG_SIGALG_OID); if (!TEST_int_ne(sigalgnid, NID_undef) || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid)) || !TEST_int_eq(digestnid, NID_undef) || !TEST_int_ne(signid, NID_undef)) goto err; sigalgnid = OBJ_sn2nid(SIGALG_SN); digestnid = OBJ_sn2nid(DIGEST_SN); signid = OBJ_ln2nid(SIG_LN); if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) || (foundsid != sigalgnid)) return 0; if ((OBJ_find_sigid_by_algs(&foundsid, OBJ_sn2nid("SHA512"), signid)) && (foundsid == sigalgnid)) return 0; sigalgnid = OBJ_sn2nid(NODIG_SIGALG_SN); digestnid = OBJ_sn2nid("SHA512"); signid = OBJ_ln2nid(NODIG_SIG_LN); if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) || (foundsid != sigalgnid)) return 0; digestnid = NID_undef; if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) || (foundsid != sigalgnid)) return 0; testresult = 1; err: OSSL_PROVIDER_unload(objprov); OSSL_LIB_CTX_free(libctx); return testresult; } int setup_tests(void) { ADD_TEST(obj_create_test); return 1; }
test
openssl/test/upcallstest.c
openssl
#include <string.h> #include "internal/e_os.h" #include "internal/nelem.h" #include "ssltestlib.h" #include "../testutil.h" #if (!defined(OPENSSL_NO_KTLS) || !defined(OPENSSL_NO_QUIC)) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_NO_SOCK) # define OSSL_USE_SOCKETS 1 # include "internal/sockets.h" # include <openssl/bio.h> #endif static int tls_dump_new(BIO *bi); static int tls_dump_free(BIO *a); static int tls_dump_read(BIO *b, char *out, int outl); static int tls_dump_write(BIO *b, const char *in, int inl); static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr); static int tls_dump_gets(BIO *bp, char *buf, int size); static int tls_dump_puts(BIO *bp, const char *str); #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER) #define BIO_TYPE_MEMPACKET_TEST 0x81 #define BIO_TYPE_ALWAYS_RETRY 0x82 #define BIO_TYPE_MAYBE_RETRY (0x83 | BIO_TYPE_FILTER) static BIO_METHOD *method_tls_dump = NULL; static BIO_METHOD *meth_mem = NULL; static BIO_METHOD *meth_always_retry = NULL; static BIO_METHOD *meth_maybe_retry = NULL; static int retry_err = -1; const BIO_METHOD *bio_f_tls_dump_filter(void) { if (method_tls_dump == NULL) { method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER, "TLS dump filter"); if (method_tls_dump == NULL || !BIO_meth_set_write(method_tls_dump, tls_dump_write) || !BIO_meth_set_read(method_tls_dump, tls_dump_read) || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts) || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets) || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl) || !BIO_meth_set_create(method_tls_dump, tls_dump_new) || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free)) return NULL; } return method_tls_dump; } void bio_f_tls_dump_filter_free(void) { BIO_meth_free(method_tls_dump); } static int tls_dump_new(BIO *bio) { BIO_set_init(bio, 1); return 1; } static int tls_dump_free(BIO *bio) { BIO_set_init(bio, 0); return 1; } static void copy_flags(BIO *bio) { int flags; BIO *next = BIO_next(bio); flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS); BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS); BIO_set_flags(bio, flags); } #define RECORD_CONTENT_TYPE 0 #define RECORD_VERSION_HI 1 #define RECORD_VERSION_LO 2 #define RECORD_EPOCH_HI 3 #define RECORD_EPOCH_LO 4 #define RECORD_SEQUENCE_START 5 #define RECORD_SEQUENCE_END 10 #define RECORD_LEN_HI 11 #define RECORD_LEN_LO 12 #define MSG_TYPE 0 #define MSG_LEN_HI 1 #define MSG_LEN_MID 2 #define MSG_LEN_LO 3 #define MSG_SEQ_HI 4 #define MSG_SEQ_LO 5 #define MSG_FRAG_OFF_HI 6 #define MSG_FRAG_OFF_MID 7 #define MSG_FRAG_OFF_LO 8 #define MSG_FRAG_LEN_HI 9 #define MSG_FRAG_LEN_MID 10 #define MSG_FRAG_LEN_LO 11 static void dump_data(const char *data, int len) { int rem, i, content, reclen, msglen, fragoff, fraglen, epoch; unsigned char *rec; printf("---- START OF PACKET ----\n"); rem = len; rec = (unsigned char *)data; while (rem > 0) { if (rem != len) printf("*\n"); printf("*---- START OF RECORD ----\n"); if (rem < DTLS1_RT_HEADER_LENGTH) { printf("*---- RECORD TRUNCATED ----\n"); break; } content = rec[RECORD_CONTENT_TYPE]; printf("** Record Content-type: %d\n", content); printf("** Record Version: %02x%02x\n", rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]); epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO]; printf("** Record Epoch: %d\n", epoch); printf("** Record Sequence: "); for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++) printf("%02x", rec[i]); reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO]; printf("\n** Record Length: %d\n", reclen); rec += DTLS1_RT_HEADER_LENGTH; rem -= DTLS1_RT_HEADER_LENGTH; if (content == SSL3_RT_HANDSHAKE) { printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n"); if (epoch > 0) { printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n"); } else if (rem < DTLS1_HM_HEADER_LENGTH || reclen < DTLS1_HM_HEADER_LENGTH) { printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n"); } else { printf("*** Message Type: %d\n", rec[MSG_TYPE]); msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8) | rec[MSG_LEN_LO]; printf("*** Message Length: %d\n", msglen); printf("*** Message sequence: %d\n", (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]); fragoff = (rec[MSG_FRAG_OFF_HI] << 16) | (rec[MSG_FRAG_OFF_MID] << 8) | rec[MSG_FRAG_OFF_LO]; printf("*** Message Fragment offset: %d\n", fragoff); fraglen = (rec[MSG_FRAG_LEN_HI] << 16) | (rec[MSG_FRAG_LEN_MID] << 8) | rec[MSG_FRAG_LEN_LO]; printf("*** Message Fragment len: %d\n", fraglen); if (fragoff + fraglen > msglen) printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n"); else if (reclen < fraglen) printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n"); else printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n"); } } if (rem < reclen) { printf("*---- RECORD TRUNCATED ----\n"); rem = 0; } else { rec += reclen; rem -= reclen; printf("*---- END OF RECORD ----\n"); } } printf("---- END OF PACKET ----\n\n"); fflush(stdout); } static int tls_dump_read(BIO *bio, char *out, int outl) { int ret; BIO *next = BIO_next(bio); ret = BIO_read(next, out, outl); copy_flags(bio); if (ret > 0) { dump_data(out, ret); } return ret; } static int tls_dump_write(BIO *bio, const char *in, int inl) { int ret; BIO *next = BIO_next(bio); ret = BIO_write(next, in, inl); copy_flags(bio); return ret; } static long tls_dump_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 tls_dump_gets(BIO *bio, char *buf, int size) { return -1; } static int tls_dump_puts(BIO *bio, const char *str) { return tls_dump_write(bio, str, strlen(str)); } struct mempacket_st { unsigned char *data; int len; unsigned int num; unsigned int type; }; static void mempacket_free(MEMPACKET *pkt) { if (pkt->data != NULL) OPENSSL_free(pkt->data); OPENSSL_free(pkt); } typedef struct mempacket_test_ctx_st { STACK_OF(MEMPACKET) *pkts; uint16_t epoch; unsigned int currrec; unsigned int currpkt; unsigned int lastpkt; unsigned int injected; unsigned int noinject; unsigned int dropepoch; int droprec; int duprec; } MEMPACKET_TEST_CTX; static int mempacket_test_new(BIO *bi); static int mempacket_test_free(BIO *a); static int mempacket_test_read(BIO *b, char *out, int outl); static int mempacket_test_write(BIO *b, const char *in, int inl); static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr); static int mempacket_test_gets(BIO *bp, char *buf, int size); static int mempacket_test_puts(BIO *bp, const char *str); const BIO_METHOD *bio_s_mempacket_test(void) { if (meth_mem == NULL) { if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST, "Mem Packet Test")) || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write)) || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read)) || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts)) || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets)) || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl)) || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new)) || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free))) return NULL; } return meth_mem; } void bio_s_mempacket_test_free(void) { BIO_meth_free(meth_mem); } static int mempacket_test_new(BIO *bio) { MEMPACKET_TEST_CTX *ctx; if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx)))) return 0; if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) { OPENSSL_free(ctx); return 0; } ctx->dropepoch = 0; ctx->droprec = -1; BIO_set_init(bio, 1); BIO_set_data(bio, ctx); return 1; } static int mempacket_test_free(BIO *bio) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free); OPENSSL_free(ctx); BIO_set_data(bio, NULL); BIO_set_init(bio, 0); return 1; } #define EPOCH_HI 3 #define EPOCH_LO 4 #define RECORD_SEQUENCE 10 #define RECORD_LEN_HI 11 #define RECORD_LEN_LO 12 #define STANDARD_PACKET 0 static int mempacket_test_read(BIO *bio, char *out, int outl) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt; unsigned char *rec; int rem; unsigned int seq, offset, len, epoch; BIO_clear_retry_flags(bio); if ((thispkt = sk_MEMPACKET_value(ctx->pkts, 0)) == NULL || thispkt->num != ctx->currpkt) { BIO_set_retry_read(bio); return -1; } (void)sk_MEMPACKET_shift(ctx->pkts); ctx->currpkt++; if (outl > thispkt->len) outl = thispkt->len; if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ && (ctx->injected || ctx->droprec >= 0)) { for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) { if (rem < DTLS1_RT_HEADER_LENGTH) return -1; epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO]; if (epoch != ctx->epoch) { ctx->epoch = epoch; ctx->currrec = 0; } seq = ctx->currrec; offset = 0; do { rec[RECORD_SEQUENCE - offset] = seq & 0xFF; seq >>= 8; offset++; } while (seq > 0); len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO]) + DTLS1_RT_HEADER_LENGTH; if (rem < (int)len) return -1; if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) { if (rem > (int)len) memmove(rec, rec + len, rem - len); outl -= len; ctx->droprec = -1; if (outl == 0) BIO_set_retry_read(bio); } else { rec += len; } ctx->currrec++; } } memcpy(out, thispkt->data, outl); mempacket_free(thispkt); return outl; } int mempacket_swap_epoch(BIO *bio) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt; int rem, len, prevlen = 0, pktnum; unsigned char *rec, *prevrec = NULL, *tmp; unsigned int epoch; int numpkts = sk_MEMPACKET_num(ctx->pkts); if (numpkts <= 0) return 0; thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 1); if (thispkt == NULL) return 0; for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len, rec += len) { if (rem < DTLS1_RT_HEADER_LENGTH) return 0; epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO]; len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO]) + DTLS1_RT_HEADER_LENGTH; if (rem < len) return 0; if (epoch != ctx->epoch) { if (prevrec == NULL) return 0; tmp = OPENSSL_malloc(prevlen); if (tmp == NULL) return 0; memcpy(tmp, prevrec, prevlen); memmove(prevrec, rec, rem); thispkt->len -= prevlen; pktnum = thispkt->num; thispkt = OPENSSL_malloc(sizeof(*thispkt)); if (thispkt == NULL) { OPENSSL_free(tmp); return 0; } thispkt->type = INJECT_PACKET; thispkt->data = tmp; thispkt->len = prevlen; thispkt->num = pktnum + 1; if (sk_MEMPACKET_insert(ctx->pkts, thispkt, numpkts) <= 0) { OPENSSL_free(tmp); OPENSSL_free(thispkt); return 0; } return 1; } prevrec = rec; prevlen = len; } return 0; } int mempacket_move_packet(BIO *bio, int d, int s) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt; int numpkts = sk_MEMPACKET_num(ctx->pkts); int i; if (d >= s) return 0; if (numpkts <= s) return 0; thispkt = sk_MEMPACKET_value(ctx->pkts, s); if (thispkt == NULL) return 0; if (sk_MEMPACKET_delete(ctx->pkts, s) != thispkt) return 0; thispkt->num -= (s - d); if (sk_MEMPACKET_insert(ctx->pkts, thispkt, d) <= 0) return 0; for (i = d + 1; i <= s; i++) { thispkt = sk_MEMPACKET_value(ctx->pkts, i); thispkt->num++; } return 1; } int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum, int type) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3]; int i, duprec; const unsigned char *inu = (const unsigned char *)in; size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO]) + DTLS1_RT_HEADER_LENGTH; if (ctx == NULL) return -1; if ((size_t)inl < len) return -1; if ((size_t)inl == len) duprec = 0; else duprec = ctx->duprec > 0; if (duprec && pktnum != -1) return -1; if (pktnum >= 0) { if (ctx->noinject) return -1; ctx->injected = 1; } else { ctx->noinject = 1; } for (i = 0; i < (duprec ? 3 : 1); i++) { if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt)))) goto err; thispkt = allpkts[i]; if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl))) goto err; if (duprec && i != 2) { memcpy(thispkt->data, in + len, inl - len); thispkt->len = inl - len; } else { memcpy(thispkt->data, in, inl); thispkt->len = inl; } thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i; thispkt->type = type; } for (i = 0; i < sk_MEMPACKET_num(ctx->pkts); i++) { if (!TEST_ptr(looppkt = sk_MEMPACKET_value(ctx->pkts, i))) goto err; if (looppkt->num > thispkt->num) { if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) goto err; if (pktnum >= 0) return inl; ctx->lastpkt++; do { i++; nextpkt = sk_MEMPACKET_value(ctx->pkts, i); if (nextpkt != NULL && nextpkt->num == ctx->lastpkt) ctx->lastpkt++; else return inl; } while(1); } else if (looppkt->num == thispkt->num) { if (!ctx->noinject) { goto err; } ctx->lastpkt++; thispkt->num++; } } for (i = 0; i < (duprec ? 3 : 1); i++) { thispkt = allpkts[i]; if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) goto err; if (pktnum < 0) ctx->lastpkt++; } return inl; err: for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++) mempacket_free(allpkts[i]); return -1; } static int mempacket_test_write(BIO *bio, const char *in, int inl) { return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET); } static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr) { long ret = 1; MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt; switch (cmd) { case BIO_CTRL_EOF: ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0); break; case BIO_CTRL_GET_CLOSE: ret = BIO_get_shutdown(bio); break; case BIO_CTRL_SET_CLOSE: BIO_set_shutdown(bio, (int)num); break; case BIO_CTRL_WPENDING: ret = 0L; break; case BIO_CTRL_PENDING: thispkt = sk_MEMPACKET_value(ctx->pkts, 0); if (thispkt == NULL) ret = 0; else ret = thispkt->len; break; case BIO_CTRL_FLUSH: ret = 1; break; case MEMPACKET_CTRL_SET_DROP_EPOCH: ctx->dropepoch = (unsigned int)num; break; case MEMPACKET_CTRL_SET_DROP_REC: ctx->droprec = (int)num; break; case MEMPACKET_CTRL_GET_DROP_REC: ret = ctx->droprec; break; case MEMPACKET_CTRL_SET_DUPLICATE_REC: ctx->duprec = (int)num; break; case BIO_CTRL_RESET: case BIO_CTRL_DUP: case BIO_CTRL_PUSH: case BIO_CTRL_POP: default: ret = 0; break; } return ret; } static int mempacket_test_gets(BIO *bio, char *buf, int size) { return -1; } static int mempacket_test_puts(BIO *bio, const char *str) { return mempacket_test_write(bio, str, strlen(str)); } static int always_retry_new(BIO *bi); static int always_retry_free(BIO *a); static int always_retry_read(BIO *b, char *out, int outl); static int always_retry_write(BIO *b, const char *in, int inl); static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr); static int always_retry_gets(BIO *bp, char *buf, int size); static int always_retry_puts(BIO *bp, const char *str); const BIO_METHOD *bio_s_always_retry(void) { if (meth_always_retry == NULL) { if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY, "Always Retry")) || !TEST_true(BIO_meth_set_write(meth_always_retry, always_retry_write)) || !TEST_true(BIO_meth_set_read(meth_always_retry, always_retry_read)) || !TEST_true(BIO_meth_set_puts(meth_always_retry, always_retry_puts)) || !TEST_true(BIO_meth_set_gets(meth_always_retry, always_retry_gets)) || !TEST_true(BIO_meth_set_ctrl(meth_always_retry, always_retry_ctrl)) || !TEST_true(BIO_meth_set_create(meth_always_retry, always_retry_new)) || !TEST_true(BIO_meth_set_destroy(meth_always_retry, always_retry_free))) return NULL; } return meth_always_retry; } void bio_s_always_retry_free(void) { BIO_meth_free(meth_always_retry); } static int always_retry_new(BIO *bio) { BIO_set_init(bio, 1); return 1; } static int always_retry_free(BIO *bio) { BIO_set_data(bio, NULL); BIO_set_init(bio, 0); return 1; } void set_always_retry_err_val(int err) { retry_err = err; } static int always_retry_read(BIO *bio, char *out, int outl) { BIO_set_retry_read(bio); return retry_err; } static int always_retry_write(BIO *bio, const char *in, int inl) { BIO_set_retry_write(bio); return retry_err; } static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr) { long ret = 1; switch (cmd) { case BIO_CTRL_FLUSH: BIO_set_retry_write(bio); case BIO_CTRL_EOF: case BIO_CTRL_RESET: case BIO_CTRL_DUP: case BIO_CTRL_PUSH: case BIO_CTRL_POP: default: ret = 0; break; } return ret; } static int always_retry_gets(BIO *bio, char *buf, int size) { BIO_set_retry_read(bio); return retry_err; } static int always_retry_puts(BIO *bio, const char *str) { BIO_set_retry_write(bio); return retry_err; } struct maybe_retry_data_st { unsigned int retrycnt; }; static int maybe_retry_new(BIO *bi); static int maybe_retry_free(BIO *a); static int maybe_retry_write(BIO *b, const char *in, int inl); static long maybe_retry_ctrl(BIO *b, int cmd, long num, void *ptr); const BIO_METHOD *bio_s_maybe_retry(void) { if (meth_maybe_retry == NULL) { if (!TEST_ptr(meth_maybe_retry = BIO_meth_new(BIO_TYPE_MAYBE_RETRY, "Maybe Retry")) || !TEST_true(BIO_meth_set_write(meth_maybe_retry, maybe_retry_write)) || !TEST_true(BIO_meth_set_ctrl(meth_maybe_retry, maybe_retry_ctrl)) || !TEST_true(BIO_meth_set_create(meth_maybe_retry, maybe_retry_new)) || !TEST_true(BIO_meth_set_destroy(meth_maybe_retry, maybe_retry_free))) return NULL; } return meth_maybe_retry; } void bio_s_maybe_retry_free(void) { BIO_meth_free(meth_maybe_retry); } static int maybe_retry_new(BIO *bio) { struct maybe_retry_data_st *data = OPENSSL_zalloc(sizeof(*data)); if (data == NULL) return 0; BIO_set_data(bio, data); BIO_set_init(bio, 1); return 1; } static int maybe_retry_free(BIO *bio) { struct maybe_retry_data_st *data = BIO_get_data(bio); OPENSSL_free(data); BIO_set_data(bio, NULL); BIO_set_init(bio, 0); return 1; } static int maybe_retry_write(BIO *bio, const char *in, int inl) { struct maybe_retry_data_st *data = BIO_get_data(bio); if (data == NULL) return -1; if (data->retrycnt == 0) { BIO_set_retry_write(bio); return -1; } data->retrycnt--; return BIO_write(BIO_next(bio), in, inl); } static long maybe_retry_ctrl(BIO *bio, int cmd, long num, void *ptr) { struct maybe_retry_data_st *data = BIO_get_data(bio); if (data == NULL) return 0; switch (cmd) { case MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT: data->retrycnt = num; return 1; case BIO_CTRL_FLUSH: if (data->retrycnt == 0) { BIO_set_retry_write(bio); return -1; } data->retrycnt--; default: return BIO_ctrl(BIO_next(bio), cmd, num, ptr); } } 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) { SSL_CTX *serverctx = NULL; SSL_CTX *clientctx = NULL; if (sctx != NULL) { if (*sctx != NULL) serverctx = *sctx; else if (!TEST_ptr(serverctx = SSL_CTX_new_ex(libctx, NULL, sm)) || !TEST_true(SSL_CTX_set_options(serverctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; } if (cctx != NULL) { if (*cctx != NULL) clientctx = *cctx; else if (!TEST_ptr(clientctx = SSL_CTX_new_ex(libctx, NULL, cm))) goto err; } #if !defined(OPENSSL_NO_TLS1_3) \ && defined(OPENSSL_NO_EC) \ && defined(OPENSSL_NO_DH) if (max_proto_version == 0 && (sm == TLS_server_method() || cm == TLS_client_method())) max_proto_version = TLS1_2_VERSION; #endif if (serverctx != NULL && ((min_proto_version > 0 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx, min_proto_version))) || (max_proto_version > 0 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx, max_proto_version))))) goto err; if (clientctx != NULL && ((min_proto_version > 0 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx, min_proto_version))) || (max_proto_version > 0 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx, max_proto_version))))) goto err; if (serverctx != NULL && certfile != NULL && privkeyfile != NULL) { if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile, SSL_FILETYPE_PEM), 1) || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1)) goto err; } if (sctx != NULL) *sctx = serverctx; if (cctx != NULL) *cctx = clientctx; return 1; err: if (sctx != NULL && *sctx == NULL) SSL_CTX_free(serverctx); if (cctx != NULL && *cctx == NULL) SSL_CTX_free(clientctx); return 0; } #define MAXLOOPS 1000000 #if defined(OSSL_USE_SOCKETS) int wait_until_sock_readable(int sock) { fd_set readfds; struct timeval timeout; int width; width = sock + 1; FD_ZERO(&readfds); openssl_fdset(sock, &readfds); timeout.tv_sec = 10; timeout.tv_usec = 0; select(width, &readfds, NULL, NULL, &timeout); return FD_ISSET(sock, &readfds); } int create_test_sockets(int *cfdp, int *sfdp, int socktype, BIO_ADDR *saddr) { struct sockaddr_in sin; const char *host = "127.0.0.1"; int cfd_connected = 0, ret = 0; socklen_t slen = sizeof(sin); int afd = -1, cfd = -1, sfd = -1; memset ((char *) &sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(host); afd = BIO_socket(AF_INET, socktype, socktype == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP, 0); if (afd == INVALID_SOCKET) return 0; if (bind(afd, (struct sockaddr*)&sin, sizeof(sin)) < 0) goto out; if (getsockname(afd, (struct sockaddr*)&sin, &slen) < 0) goto out; if (saddr != NULL && !BIO_ADDR_rawmake(saddr, sin.sin_family, &sin.sin_addr, sizeof(sin.sin_addr), sin.sin_port)) goto out; if (socktype == SOCK_STREAM && listen(afd, 1) < 0) goto out; cfd = BIO_socket(AF_INET, socktype, socktype == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP, 0); if (cfd == INVALID_SOCKET) goto out; if (!BIO_socket_nbio(afd, 1)) goto out; if (socktype == SOCK_DGRAM) { cfd_connected = 1; sfd = afd; afd = -1; } while (sfd == -1 || !cfd_connected) { sfd = accept(afd, NULL, 0); if (sfd == -1 && errno != EAGAIN) goto out; if (!cfd_connected && connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) goto out; else cfd_connected = 1; } if (!BIO_socket_nbio(cfd, 1) || !BIO_socket_nbio(sfd, 1)) goto out; ret = 1; *cfdp = cfd; *sfdp = sfd; goto success; out: if (cfd != -1) close(cfd); if (sfd != -1) close(sfd); success: if (afd != -1) close(afd); return ret; } int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl, SSL **cssl, int sfd, int cfd) { SSL *serverssl = NULL, *clientssl = NULL; BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL; BIO_POLL_DESCRIPTOR rdesc = {0}, wdesc = {0}; if (*sssl != NULL) serverssl = *sssl; else if (!TEST_ptr(serverssl = SSL_new(serverctx))) goto error; if (*cssl != NULL) clientssl = *cssl; else if (!TEST_ptr(clientssl = SSL_new(clientctx))) goto error; if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE)) || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE))) goto error; if (!TEST_false(SSL_get_rpoll_descriptor(clientssl, &rdesc) || !TEST_false(SSL_get_wpoll_descriptor(clientssl, &wdesc)))) goto error; SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio); SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio); if (!TEST_true(SSL_get_rpoll_descriptor(clientssl, &rdesc)) || !TEST_true(SSL_get_wpoll_descriptor(clientssl, &wdesc)) || !TEST_int_eq(rdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) || !TEST_int_eq(wdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) || !TEST_int_eq(rdesc.value.fd, cfd) || !TEST_int_eq(wdesc.value.fd, cfd)) goto error; if (!TEST_true(SSL_get_rpoll_descriptor(serverssl, &rdesc)) || !TEST_true(SSL_get_wpoll_descriptor(serverssl, &wdesc)) || !TEST_int_eq(rdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) || !TEST_int_eq(wdesc.type, BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) || !TEST_int_eq(rdesc.value.fd, sfd) || !TEST_int_eq(wdesc.value.fd, sfd)) goto error; *sssl = serverssl; *cssl = clientssl; return 1; error: SSL_free(serverssl); SSL_free(clientssl); BIO_free(s_to_c_bio); BIO_free(c_to_s_bio); return 0; } #else int wait_until_sock_readable(int sock) { return 0; } #endif int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl, SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio) { SSL *serverssl = NULL, *clientssl = NULL; BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL; if (*sssl != NULL) serverssl = *sssl; else if (!TEST_ptr(serverssl = SSL_new(serverctx))) goto error; if (*cssl != NULL) clientssl = *cssl; else if (!TEST_ptr(clientssl = SSL_new(clientctx))) goto error; if (SSL_is_dtls(clientssl)) { if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test())) || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test()))) goto error; } else { if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem())) || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem()))) goto error; } if (s_to_c_fbio != NULL && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio))) goto error; if (c_to_s_fbio != NULL && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio))) goto error; BIO_set_mem_eof_return(s_to_c_bio, -1); BIO_set_mem_eof_return(c_to_s_bio, -1); SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio); BIO_up_ref(s_to_c_bio); BIO_up_ref(c_to_s_bio); SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio); *sssl = serverssl; *cssl = clientssl; return 1; error: SSL_free(serverssl); SSL_free(clientssl); BIO_free(s_to_c_bio); BIO_free(c_to_s_bio); BIO_free(s_to_c_fbio); BIO_free(c_to_s_fbio); return 0; } int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want, int read, int listen) { int retc = -1, rets = -1, err, abortctr = 0, ret = 0; int clienterr = 0, servererr = 0; int isdtls = SSL_is_dtls(serverssl); #ifndef OPENSSL_NO_SOCK BIO_ADDR *peer = NULL; if (listen) { if (!isdtls) { TEST_error("DTLSv1_listen requested for non-DTLS object\n"); return 0; } peer = BIO_ADDR_new(); if (!TEST_ptr(peer)) return 0; } #else if (listen) { TEST_error("DTLSv1_listen requested in a no-sock build\n"); return 0; } #endif do { err = SSL_ERROR_WANT_WRITE; while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) { retc = SSL_connect(clientssl); if (retc <= 0) err = SSL_get_error(clientssl, retc); } if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) { TEST_info("SSL_connect() failed %d, %d", retc, err); if (want != SSL_ERROR_SSL) TEST_openssl_errors(); clienterr = 1; } if (want != SSL_ERROR_NONE && err == want) goto err; err = SSL_ERROR_WANT_WRITE; while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) { #ifndef OPENSSL_NO_SOCK if (listen) { rets = DTLSv1_listen(serverssl, peer); if (rets < 0) { err = SSL_ERROR_SSL; } else if (rets == 0) { err = SSL_ERROR_WANT_READ; } else { listen = 0; rets = 0; } } else #endif { rets = SSL_accept(serverssl); if (rets <= 0) err = SSL_get_error(serverssl, rets); } } if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_X509_LOOKUP) { TEST_info("SSL_accept() failed %d, %d", rets, err); if (want != SSL_ERROR_SSL) TEST_openssl_errors(); servererr = 1; } if (want != SSL_ERROR_NONE && err == want) goto err; if (clienterr && servererr) goto err; if (isdtls && read) { unsigned char buf[20]; if (rets > 0 && retc <= 0) { if (SSL_read(serverssl, buf, sizeof(buf)) > 0) { TEST_info("Unexpected SSL_read() success!"); goto err; } } if (retc > 0 && rets <= 0) { if (SSL_read(clientssl, buf, sizeof(buf)) > 0) { TEST_info("Unexpected SSL_read() success!"); goto err; } } } if (++abortctr == MAXLOOPS) { TEST_info("No progress made"); goto err; } if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) { OSSL_sleep(50); } } while (retc <=0 || rets <= 0); ret = 1; err: #ifndef OPENSSL_NO_SOCK BIO_ADDR_free(peer); #endif return ret; } int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want) { int i; unsigned char buf; size_t readbytes; if (!create_bare_ssl_connection(serverssl, clientssl, want, 1, 0)) return 0; for (i = 0; i < 2; i++) { if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) { if (!TEST_ulong_eq(readbytes, 0)) return 0; } else if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_READ)) { return 0; } } return 1; } void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl) { SSL_shutdown(clientssl); SSL_shutdown(serverssl); SSL_free(serverssl); SSL_free(clientssl); } SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize) { const SSL_CIPHER *cipher = NULL; const unsigned char key[SHA384_DIGEST_LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }; SSL_SESSION *sess = NULL; if (mdsize == SHA384_DIGEST_LENGTH) { cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES); } else if (mdsize == SHA256_DIGEST_LENGTH) { cipher = SSL_CIPHER_find(ssl, TLS13_AES_128_GCM_SHA256_BYTES); } else { return NULL; } sess = SSL_SESSION_new(); if (!TEST_ptr(sess) || !TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set1_master_key(sess, key, mdsize)) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)) || !TEST_true( SSL_SESSION_set_protocol_version(sess, TLS1_3_VERSION))) { SSL_SESSION_free(sess); return NULL; } return sess; } #define NUM_EXTRA_CERTS 40 int ssl_ctx_add_large_cert_chain(OSSL_LIB_CTX *libctx, SSL_CTX *sctx, const char *cert_file) { BIO *certbio = NULL; X509 *chaincert = NULL; int certlen; int ret = 0; int i; if (!TEST_ptr(certbio = BIO_new_file(cert_file, "r"))) goto end; if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL))) goto end; if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL) goto end; BIO_free(certbio); certbio = NULL; certlen = i2d_X509(chaincert, NULL); OPENSSL_assert(certlen * NUM_EXTRA_CERTS > (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3); for (i = 0; i < NUM_EXTRA_CERTS; i++) { if (!X509_up_ref(chaincert)) goto end; if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) { X509_free(chaincert); goto end; } } ret = 1; end: BIO_free(certbio); X509_free(chaincert); return ret; }
helpers
openssl/test/helpers/ssltestlib.c
openssl
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "internal/nelem.h" #include <openssl/pkcs12.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include "../testutil.h" #include "pkcs12.h" static int write_files = 0; static int legacy = 0; static OSSL_LIB_CTX *test_ctx = NULL; static const char *test_propq = NULL; static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs); static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac); static int write_p12(PKCS12 *p12, const char *outfile); static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac); static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac); static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac); static int check_asn1_string(const ASN1_TYPE *av, const char *txt); static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs); void PKCS12_helper_set_write_files(int enable) { write_files = enable; } void PKCS12_helper_set_legacy(int enable) { legacy = enable; } void PKCS12_helper_set_libctx(OSSL_LIB_CTX *libctx) { test_ctx = libctx; } void PKCS12_helper_set_propq(const char *propq) { test_propq = propq; } static X509 *load_cert_asn1(const unsigned char *bytes, int len) { X509 *cert = NULL; cert = d2i_X509(NULL, &bytes, len); if (!TEST_ptr(cert)) goto err; err: return cert; } static EVP_PKEY *load_pkey_asn1(const unsigned char *bytes, int len) { EVP_PKEY *pkey = NULL; pkey = d2i_AutoPrivateKey(NULL, &bytes, len); if (!TEST_ptr(pkey)) goto err; err: return pkey; } PKCS12_BUILDER *new_pkcs12_builder(const char *filename) { PKCS12_BUILDER *pb = OPENSSL_malloc(sizeof(PKCS12_BUILDER)); if (!TEST_ptr(pb)) return NULL; pb->filename = filename; pb->success = 1; return pb; } int end_pkcs12_builder(PKCS12_BUILDER *pb) { int result = pb->success; OPENSSL_free(pb); return result; } void start_pkcs12(PKCS12_BUILDER *pb) { pb->safes = NULL; } void end_pkcs12(PKCS12_BUILDER *pb) { if (!pb->success) return; generate_p12(pb, NULL); } void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac) { if (!pb->success) return; generate_p12(pb, mac); } static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac) { PKCS12 *p12; EVP_MD *md = NULL; if (!pb->success) return; pb->p12bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(pb->p12bio)) { pb->success = 0; return; } if (legacy) p12 = PKCS12_add_safes(pb->safes, 0); else p12 = PKCS12_add_safes_ex(pb->safes, 0, test_ctx, test_propq); if (!TEST_ptr(p12)) { pb->success = 0; goto err; } sk_PKCS7_pop_free(pb->safes, PKCS7_free); if (mac != NULL) { if (legacy) md = (EVP_MD *)EVP_get_digestbynid(mac->nid); else md = EVP_MD_fetch(test_ctx, OBJ_nid2sn(mac->nid), test_propq); if (!TEST_true(PKCS12_set_mac(p12, mac->pass, strlen(mac->pass), NULL, 0, mac->iter, md))) { pb->success = 0; goto err; } } i2d_PKCS12_bio(pb->p12bio, p12); if (write_files) write_p12(p12, pb->filename); err: if (!legacy && md != NULL) EVP_MD_free(md); PKCS12_free(p12); } static int write_p12(PKCS12 *p12, const char *outfile) { int ret = 0; BIO *out = BIO_new_file(outfile, "w"); if (out == NULL) goto err; if (!TEST_int_eq(i2d_PKCS12_bio(out, p12), 1)) goto err; ret = 1; err: BIO_free(out); return ret; } static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac) { PKCS12 *p12 = NULL; if (!legacy) { p12 = PKCS12_init_ex(NID_pkcs7_data, test_ctx, test_propq); if (!TEST_ptr(p12)) goto err; } p12 = d2i_PKCS12_bio(bio, &p12); BIO_free(bio); if (!TEST_ptr(p12)) goto err; if (mac == NULL) { if (!TEST_false(PKCS12_mac_present(p12))) goto err; } else { if (!check_p12_mac(p12, mac)) goto err; } return p12; err: PKCS12_free(p12); return NULL; } static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac) { PKCS12 *p12 = NULL; BIO *in = BIO_new_file(infile, "r"); if (in == NULL) goto err; p12 = d2i_PKCS12_bio(in, NULL); BIO_free(in); if (!TEST_ptr(p12)) goto err; if (mac == NULL) { if (!TEST_false(PKCS12_mac_present(p12))) goto err; } else { if (!check_p12_mac(p12, mac)) goto err; } return p12; err: PKCS12_free(p12); return NULL; } static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac) { return TEST_true(PKCS12_mac_present(p12)) && TEST_true(PKCS12_verify_mac(p12, mac->pass, strlen(mac->pass))); } void start_contentinfo(PKCS12_BUILDER *pb) { pb->bags = NULL; } void end_contentinfo(PKCS12_BUILDER *pb) { if (pb->success && pb->bags != NULL) { if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, -1, 0, NULL))) pb->success = 0; } sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free); pb->bags = NULL; } void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc) { if (pb->success && pb->bags != NULL) { if (legacy) { if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, enc->nid, enc->iter, enc->pass))) pb->success = 0; } else { if (!TEST_true(PKCS12_add_safe_ex(&pb->safes, pb->bags, enc->nid, enc->iter, enc->pass, test_ctx, test_propq))) pb->success = 0; } } sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free); pb->bags = NULL; } static STACK_OF(PKCS12_SAFEBAG) *decode_contentinfo(STACK_OF(PKCS7) *safes, int idx, const PKCS12_ENC *enc) { STACK_OF(PKCS12_SAFEBAG) *bags = NULL; int bagnid; PKCS7 *p7 = sk_PKCS7_value(safes, idx); if (!TEST_ptr(p7)) goto err; bagnid = OBJ_obj2nid(p7->type); if (enc) { if (!TEST_int_eq(bagnid, NID_pkcs7_encrypted)) goto err; bags = PKCS12_unpack_p7encdata(p7, enc->pass, strlen(enc->pass)); } else { if (!TEST_int_eq(bagnid, NID_pkcs7_data)) goto err; bags = PKCS12_unpack_p7data(p7); } if (!TEST_ptr(bags)) goto err; return bags; err: return NULL; } static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attr) { int ret = 0; int attr_nid; const PKCS12_ATTR *p_attr = attr; STACK_OF(X509_ATTRIBUTE)* attrs = NULL; X509_ATTRIBUTE *x509_attr = NULL; if (attr == NULL) return 1; while (p_attr->oid != NULL) { TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value); attr_nid = OBJ_txt2nid(p_attr->oid); if (attr_nid == NID_friendlyName) { if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1))) goto err; } else if (attr_nid == NID_localKeyID) { if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value, strlen(p_attr->value)))) goto err; } else if (attr_nid == NID_oracle_jdk_trustedkeyusage) { attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag); x509_attr = X509_ATTRIBUTE_create(attr_nid, V_ASN1_OBJECT, OBJ_txt2obj(p_attr->value, 0)); X509at_add1_attr(&attrs, x509_attr); PKCS12_SAFEBAG_set0_attrs(bag, attrs); X509_ATTRIBUTE_free(x509_attr); } else { if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC, (unsigned char *)p_attr->value, strlen(p_attr->value)))) goto err; } p_attr++; } ret = 1; err: return ret; } void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len, const PKCS12_ATTR *attrs) { PKCS12_SAFEBAG *bag = NULL; X509 *cert = NULL; char *name; if (!pb->success) return; cert = load_cert_asn1(bytes, len); if (!TEST_ptr(cert)) { pb->success = 0; return; } name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0); TEST_info("Adding certificate <%s>", name); OPENSSL_free(name); bag = PKCS12_add_cert(&pb->bags, cert); if (!TEST_ptr(bag)) { pb->success = 0; goto err; } if (!TEST_true(add_attributes(bag, attrs))) { pb->success = 0; goto err; } err: X509_free(cert); } void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len, const PKCS12_ATTR *attrs, const PKCS12_ENC *enc) { PKCS12_SAFEBAG *bag = NULL; EVP_PKEY *pkey = NULL; if (!pb->success) return; TEST_info("Adding key"); pkey = load_pkey_asn1(bytes, len); if (!TEST_ptr(pkey)) { pb->success = 0; return; } if (legacy) bag = PKCS12_add_key(&pb->bags, pkey, 0 , enc->iter, enc->nid, enc->pass); else bag = PKCS12_add_key_ex(&pb->bags, pkey, 0 , enc->iter, enc->nid, enc->pass, test_ctx, test_propq); if (!TEST_ptr(bag)) { pb->success = 0; goto err; } if (!add_attributes(bag, attrs)) pb->success = 0; err: EVP_PKEY_free(pkey); } void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs) { PKCS12_SAFEBAG *bag = NULL; if (!pb->success) return; TEST_info("Adding secret <%s>", secret); bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret)); if (!TEST_ptr(bag)) { pb->success = 0; return; } if (!add_attributes(bag, attrs)) pb->success = 0; } static int check_asn1_string(const ASN1_TYPE *av, const char *txt) { int ret = 0; char *value = NULL; if (!TEST_ptr(av)) goto err; switch (av->type) { case V_ASN1_BMPSTRING: value = OPENSSL_uni2asc(av->value.bmpstring->data, av->value.bmpstring->length); if (!TEST_str_eq(txt, (char *)value)) goto err; break; case V_ASN1_UTF8STRING: if (!TEST_mem_eq(txt, strlen(txt), (char *)av->value.utf8string->data, av->value.utf8string->length)) goto err; break; case V_ASN1_OCTET_STRING: if (!TEST_mem_eq(txt, strlen(txt), (char *)av->value.octet_string->data, av->value.octet_string->length)) goto err; break; default: goto err; } ret = 1; err: OPENSSL_free(value); return ret; } static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs) { int ret = 0; X509_ATTRIBUTE *attr; ASN1_TYPE *av; int i, j; char attr_txt[100]; for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) { const PKCS12_ATTR *p_attr = attrs; ASN1_OBJECT *attr_obj; attr = sk_X509_ATTRIBUTE_value(bag_attrs, i); attr_obj = X509_ATTRIBUTE_get0_object(attr); OBJ_obj2txt(attr_txt, 100, attr_obj, 0); while (p_attr->oid != NULL) { if (strcmp(p_attr->oid, attr_txt) == 0) { if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1)) goto err; for (j = 0; j < X509_ATTRIBUTE_count(attr); j++) { av = X509_ATTRIBUTE_get0_type(attr, j); if (!TEST_true(check_asn1_string(av, p_attr->value))) goto err; } break; } p_attr++; } } ret = 1; err: return ret; } void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len, const PKCS12_ATTR *attrs) { X509 *x509 = NULL; X509 *ref_x509 = NULL; const PKCS12_SAFEBAG *bag; if (!pb->success) return; bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++); if (!TEST_ptr(bag)) { pb->success = 0; return; } if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs) || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag) || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) { pb->success = 0; return; } x509 = PKCS12_SAFEBAG_get1_cert(bag); if (!TEST_ptr(x509)) { pb->success = 0; goto err; } ref_x509 = load_cert_asn1(bytes, len); if (!TEST_false(X509_cmp(x509, ref_x509))) pb->success = 0; err: X509_free(x509); X509_free(ref_x509); } void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len, const PKCS12_ATTR *attrs, const PKCS12_ENC *enc) { EVP_PKEY *pkey = NULL; EVP_PKEY *ref_pkey = NULL; PKCS8_PRIV_KEY_INFO *p8; const PKCS8_PRIV_KEY_INFO *p8c; const PKCS12_SAFEBAG *bag; if (!pb->success) return; bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++); if (!TEST_ptr(bag)) { pb->success = 0; return; } if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) { pb->success = 0; return; } switch (PKCS12_SAFEBAG_get_nid(bag)) { case NID_keyBag: p8c = PKCS12_SAFEBAG_get0_p8inf(bag); if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) { pb->success = 0; goto err; } break; case NID_pkcs8ShroudedKeyBag: if (legacy) p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass)); else p8 = PKCS12_decrypt_skey_ex(bag, enc->pass, strlen(enc->pass), test_ctx, test_propq); if (!TEST_ptr(p8)) { pb->success = 0; goto err; } if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) { PKCS8_PRIV_KEY_INFO_free(p8); pb->success = 0; goto err; } PKCS8_PRIV_KEY_INFO_free(p8); break; default: pb->success = 0; goto err; } ref_pkey = load_pkey_asn1(bytes, len); if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey))) pb->success = 0; err: EVP_PKEY_free(pkey); EVP_PKEY_free(ref_pkey); } void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs) { const PKCS12_SAFEBAG *bag; if (!pb->success) return; bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++); if (!TEST_ptr(bag)) { pb->success = 0; return; } if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs) || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag) || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid) || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret))) pb->success = 0; } void start_check_pkcs12(PKCS12_BUILDER *pb) { PKCS12 *p12; if (!pb->success) return; p12 = from_bio_p12(pb->p12bio, NULL); if (!TEST_ptr(p12)) { pb->success = 0; return; } pb->safes = PKCS12_unpack_authsafes(p12); if (!TEST_ptr(pb->safes)) pb->success = 0; pb->safe_idx = 0; PKCS12_free(p12); } void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac) { PKCS12 *p12; if (!pb->success) return; p12 = from_bio_p12(pb->p12bio, mac); if (!TEST_ptr(p12)) { pb->success = 0; return; } pb->safes = PKCS12_unpack_authsafes(p12); if (!TEST_ptr(pb->safes)) pb->success = 0; pb->safe_idx = 0; PKCS12_free(p12); } void start_check_pkcs12_file(PKCS12_BUILDER *pb) { PKCS12 *p12; if (!pb->success) return; p12 = read_p12(pb->filename, NULL); if (!TEST_ptr(p12)) { pb->success = 0; return; } pb->safes = PKCS12_unpack_authsafes(p12); if (!TEST_ptr(pb->safes)) pb->success = 0; pb->safe_idx = 0; PKCS12_free(p12); } void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac) { PKCS12 *p12; if (!pb->success) return; p12 = read_p12(pb->filename, mac); if (!TEST_ptr(p12)) { pb->success = 0; return; } pb->safes = PKCS12_unpack_authsafes(p12); if (!TEST_ptr(pb->safes)) pb->success = 0; pb->safe_idx = 0; PKCS12_free(p12); } void end_check_pkcs12(PKCS12_BUILDER *pb) { if (!pb->success) return; sk_PKCS7_pop_free(pb->safes, PKCS7_free); } void start_check_contentinfo(PKCS12_BUILDER *pb) { if (!pb->success) return; pb->bag_idx = 0; pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL); if (!TEST_ptr(pb->bags)) { pb->success = 0; return; } TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags)); } void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc) { if (!pb->success) return; pb->bag_idx = 0; pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc); if (!TEST_ptr(pb->bags)) { pb->success = 0; return; } TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags)); } void end_check_contentinfo(PKCS12_BUILDER *pb) { if (!pb->success) return; if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx)) pb->success = 0; sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free); pb->bags = NULL; }
helpers
openssl/test/helpers/pkcs12.c
openssl
#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; 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; \ } 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"; } 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); } 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); } 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); } 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); } 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); } 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); } 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); } IMPLEMENT_SSL_TEST_BOOL_OPTION(SSL_TEST_CTX, test, compression_expected) 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); } 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); } 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) 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) 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) 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); } IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CLIENT_CONF, client, reneg_ciphers) 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; } 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) 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); } IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, app_data_size) IMPLEMENT_SSL_TEST_INT_OPTION(SSL_TEST_CTX, test, max_fragment_size) 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); } __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); } __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); } IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, expected_cipher) 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) IMPLEMENT_SSL_TEST_STRING_OPTION(SSL_TEST_CTX, test, fips_version) 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 }, }; 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 }, }; 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; 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); 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; }
helpers
openssl/test/helpers/ssl_test_ctx.c
openssl
#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" ) < 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; }
helpers
openssl/test/helpers/handshake_srp.c
openssl
#include <openssl/bio.h> #include "quictestlib.h" #include "../testutil.h" #define MSG_DATA_LEN_MAX 1472 #define SAMPLING_WINDOW_PERIOD 10 #define MAX_PKTS_PER_WINDOW 1024 struct pkt_info_st { size_t size; OSSL_TIME timestamp; }; struct bw_limiter_st { struct pkt_info_st pinfos[MAX_PKTS_PER_WINDOW]; size_t start, num; size_t size_sum; size_t bw; }; struct noisy_dgram_st { uint64_t this_dgram; BIO_MSG msg; uint64_t reinject_dgram; int backoff; int noise_rate; struct bw_limiter_st recv_limit, send_limit; OSSL_TIME (*now_cb)(void *arg); void *now_cb_arg; }; 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; } case BIO_CTRL_NOISE_RATE: { struct noisy_dgram_st *data; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; data->noise_rate = (int)num; ret = 1; break; } case BIO_CTRL_NOISE_RECV_BANDWIDTH: { struct noisy_dgram_st *data; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; data->recv_limit.bw = (size_t)num; ret = 1; break; } case BIO_CTRL_NOISE_SEND_BANDWIDTH: { struct noisy_dgram_st *data; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; data->send_limit.bw = (size_t)num; ret = 1; break; } case BIO_CTRL_NOISE_SET_NOW_CB: { struct noisy_dgram_st *data; struct bio_noise_now_cb_st *now_cb = ptr; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; data->now_cb = now_cb->now_cb; data->now_cb_arg = now_cb->now_cb_arg; ret = 1; break; } default: ret = BIO_ctrl(next, cmd, num, ptr); break; } return ret; } static size_t bandwidth_limit(struct bw_limiter_st *limit, OSSL_TIME now, BIO_MSG *msg, size_t num_msg) { size_t i; OSSL_TIME sampling_start = ossl_time_subtract(now, ossl_ms2time(SAMPLING_WINDOW_PERIOD)); if (limit->bw == 0) return num_msg; if (num_msg > MAX_PKTS_PER_WINDOW) num_msg = MAX_PKTS_PER_WINDOW; for (i = 0; i < limit->num; i++) { size_t idx = (limit->start + i) % MAX_PKTS_PER_WINDOW; if (ossl_time_compare(limit->pinfos[idx].timestamp, sampling_start) >= 0) break; limit->size_sum -= limit->pinfos[idx].size; } limit->start = (limit->start + i) % MAX_PKTS_PER_WINDOW; limit->num -= i; for (i = 0; i < num_msg; ++i) { size_t end; size_t pktsize = msg[i].data_len; if ((limit->size_sum + pktsize) / SAMPLING_WINDOW_PERIOD > limit->bw) { #ifdef OSSL_NOISY_DGRAM_DEBUG printf("**BW limit applied: now: %llu orig packets %u new packets %u\n", (unsigned long long)ossl_time2ms(now), (unsigned int)num_msg, (unsigned int) i); #endif num_msg = i; break; } if (limit->num >= MAX_PKTS_PER_WINDOW) { limit->size_sum -= limit->pinfos[limit->start].size; limit->start = (limit->start + 1) % MAX_PKTS_PER_WINDOW; } else { ++limit->num; } end = (limit->start + limit->num) % MAX_PKTS_PER_WINDOW; limit->pinfos[end].size = pktsize; limit->pinfos[end].timestamp = now; limit->size_sum += pktsize; } return num_msg; } 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); struct noisy_dgram_st *data; OSSL_TIME now; if (next == NULL) return 0; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; now = data->now_cb != NULL ? data->now_cb(data->now_cb_arg) : ossl_time_now(); num_msg = bandwidth_limit(&data->send_limit, now, msg, num_msg); if (num_msg == 0) { *msgs_processed = 0; ERR_raise(ERR_LIB_BIO, BIO_R_NON_FATAL); return 0; } return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed); } #define NOISE_RATE 5 #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 #define MAX_DGRAM_REINJECT 4 static void get_noise(int noise_rate, 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; *should_drop = (type == NOISE_TYPE_DROP || type == NOISE_TYPE_DELAY); *reinject = (type == NOISE_TYPE_DUPLICATE || type == NOISE_TYPE_DELAY) ? (uint64_t)((test_random() % MAX_DGRAM_REINJECT) + 1) : 0; *reinject += type == NOISE_TYPE_DELAY; if (type == NOISE_TYPE_BITFLIPS) { *flip = (test_random() % 255 + 1) << (test_random() % 8); *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; 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; OSSL_TIME now; if (!TEST_ptr(next)) return 0; data = BIO_get_data(bio); if (!TEST_ptr(data)) return 0; 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 now = data->now_cb != NULL ? data->now_cb(data->now_cb_arg) : ossl_time_now(); msg_cnt = *msgs_processed; msg_cnt = bandwidth_limit(&data->recv_limit, now, msg, msg_cnt); if (msg_cnt == 0) goto end; if (data->noise_rate == 0) goto end; 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 (data->reinject_dgram > 0 && data->reinject_dgram == data->this_dgram) { if (msg_cnt < num_msg) { 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; } data->reinject_dgram = 0; } get_noise(data->noise_rate, (((uint8_t *)thismsg->data)[0] & 0x80) != 0, &reinject, &should_drop, &flip, &flip_offset); if (data->backoff) { #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); if (reinject > 0 && data->reinject_dgram == 0) { 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 end: *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->noise_rate = NOISE_RATE; 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; } #define BIO_TYPE_NOISY_DGRAM_FILTER (0x80 | BIO_TYPE_FILTER) static BIO_METHOD *method_noisy_dgram = NULL; 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, "Noisy 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); }
helpers
openssl/test/helpers/noisydgrambio.c
openssl
#include <string.h> #include <openssl/bio.h> #include <openssl/x509_vfy.h> #include <openssl/ssl.h> #include <openssl/core_names.h> #include "../../ssl/ssl_local.h" #include "internal/sockets.h" #include "internal/nelem.h" #include "handshake.h" #include "../testutil.h" #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) #include <netinet/sctp.h> #endif HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void) { HANDSHAKE_RESULT *ret; TEST_ptr(ret = OPENSSL_zalloc(sizeof(*ret))); return ret; } void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result) { if (result == NULL) return; OPENSSL_free(result->client_npn_negotiated); OPENSSL_free(result->server_npn_negotiated); OPENSSL_free(result->client_alpn_negotiated); OPENSSL_free(result->server_alpn_negotiated); OPENSSL_free(result->result_session_ticket_app_data); sk_X509_NAME_pop_free(result->server_ca_names, X509_NAME_free); sk_X509_NAME_pop_free(result->client_ca_names, X509_NAME_free); OPENSSL_free(result->cipher); OPENSSL_free(result); } typedef struct handshake_ex_data_st { int alert_sent; int num_fatal_alerts_sent; int alert_received; int session_ticket_do_not_call; ssl_servername_t servername; } HANDSHAKE_EX_DATA; static void ctx_data_free_data(CTX_DATA *ctx_data) { OPENSSL_free(ctx_data->npn_protocols); ctx_data->npn_protocols = NULL; OPENSSL_free(ctx_data->alpn_protocols); ctx_data->alpn_protocols = NULL; OPENSSL_free(ctx_data->srp_user); ctx_data->srp_user = NULL; OPENSSL_free(ctx_data->srp_password); ctx_data->srp_password = NULL; OPENSSL_free(ctx_data->session_ticket_app_data); ctx_data->session_ticket_app_data = NULL; } static int ex_data_idx; static void info_cb(const SSL *s, int where, int ret) { if (where & SSL_CB_ALERT) { HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); if (where & SSL_CB_WRITE) { ex_data->alert_sent = ret; if (strcmp(SSL_alert_type_string(ret), "F") == 0 || strcmp(SSL_alert_desc_string(ret), "CN") == 0) ex_data->num_fatal_alerts_sent++; } else { ex_data->alert_received = ret; } } } static int select_server_ctx(SSL *s, void *arg, int ignore) { const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); if (servername == NULL) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_NOACK; } if (strcmp(servername, "server2") == 0) { SSL_CTX *new_ctx = (SSL_CTX*)arg; SSL_set_SSL_CTX(s, new_ctx); SSL_clear_options(s, 0xFFFFFFFFL); SSL_set_options(s, SSL_CTX_get_options(new_ctx)); ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; return SSL_TLSEXT_ERR_OK; } else if (strcmp(servername, "server1") == 0) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_OK; } else if (ignore) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_NOACK; } else { return SSL_TLSEXT_ERR_ALERT_FATAL; } } static int client_hello_select_server_ctx(SSL *s, void *arg, int ignore) { const char *servername; const unsigned char *p; size_t len, remaining; HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); if (!SSL_client_hello_get0_ext(s, TLSEXT_TYPE_server_name, &p, &remaining) || remaining <= 2) return 0; len = (*(p++) << 8); len += *(p++); if (len + 2 != remaining) return 0; remaining = len; if (remaining == 0 || *p++ != TLSEXT_NAMETYPE_host_name) return 0; remaining--; if (remaining <= 2) return 0; len = (*(p++) << 8); len += *(p++); if (len + 2 > remaining) return 0; remaining = len; servername = (const char *)p; if (len == strlen("server2") && HAS_PREFIX(servername, "server2")) { SSL_CTX *new_ctx = arg; SSL_set_SSL_CTX(s, new_ctx); SSL_clear_options(s, 0xFFFFFFFFL); SSL_set_options(s, SSL_CTX_get_options(new_ctx)); ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; return 1; } else if (len == strlen("server1") && HAS_PREFIX(servername, "server1")) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return 1; } else if (ignore) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return 1; } return 0; } static int servername_ignore_cb(SSL *s, int *ad, void *arg) { return select_server_ctx(s, arg, 1); } static int servername_reject_cb(SSL *s, int *ad, void *arg) { return select_server_ctx(s, arg, 0); } static int client_hello_ignore_cb(SSL *s, int *al, void *arg) { if (!client_hello_select_server_ctx(s, arg, 1)) { *al = SSL_AD_UNRECOGNIZED_NAME; return SSL_CLIENT_HELLO_ERROR; } return SSL_CLIENT_HELLO_SUCCESS; } static int client_hello_reject_cb(SSL *s, int *al, void *arg) { if (!client_hello_select_server_ctx(s, arg, 0)) { *al = SSL_AD_UNRECOGNIZED_NAME; return SSL_CLIENT_HELLO_ERROR; } return SSL_CLIENT_HELLO_SUCCESS; } static int client_hello_nov12_cb(SSL *s, int *al, void *arg) { int ret; unsigned int v; const unsigned char *p; v = SSL_client_hello_get0_legacy_version(s); if (v > TLS1_2_VERSION || v < SSL3_VERSION) { *al = SSL_AD_PROTOCOL_VERSION; return SSL_CLIENT_HELLO_ERROR; } (void)SSL_client_hello_get0_session_id(s, &p); if (p == NULL || SSL_client_hello_get0_random(s, &p) == 0 || SSL_client_hello_get0_ciphers(s, &p) == 0 || SSL_client_hello_get0_compression_methods(s, &p) == 0) { *al = SSL_AD_INTERNAL_ERROR; return SSL_CLIENT_HELLO_ERROR; } ret = client_hello_select_server_ctx(s, arg, 0); SSL_set_max_proto_version(s, TLS1_1_VERSION); if (!ret) { *al = SSL_AD_UNRECOGNIZED_NAME; return SSL_CLIENT_HELLO_ERROR; } return SSL_CLIENT_HELLO_SUCCESS; } static unsigned char dummy_ocsp_resp_good_val = 0xff; static unsigned char dummy_ocsp_resp_bad_val = 0xfe; static int server_ocsp_cb(SSL *s, void *arg) { unsigned char *resp; resp = OPENSSL_malloc(1); if (resp == NULL) return SSL_TLSEXT_ERR_ALERT_FATAL; *resp = *(unsigned char *)arg; if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1)) { OPENSSL_free(resp); return SSL_TLSEXT_ERR_ALERT_FATAL; } return SSL_TLSEXT_ERR_OK; } static int client_ocsp_cb(SSL *s, void *arg) { const unsigned char *resp; int len; len = SSL_get_tlsext_status_ocsp_resp(s, &resp); if (len != 1 || *resp != dummy_ocsp_resp_good_val) return 0; return 1; } static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) { X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION); return 0; } static int n_retries = 0; static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg) { int idx = SSL_get_ex_data_X509_STORE_CTX_idx(); SSL *ssl; if (idx < 0 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL) return 0; if (--n_retries < 0) return 1; return SSL_set_retry_verify(ssl); } static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) { return 1; } static int broken_session_ticket_cb(SSL *s, unsigned char *key_name, unsigned char *iv, EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc) { return 0; } static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name, unsigned char *iv, EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc) { HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); ex_data->session_ticket_do_not_call = 1; return 0; } static int parse_protos(const char *protos, unsigned char **out, size_t *outlen) { size_t len, i, prefix; len = strlen(protos); if (!TEST_ptr_null(*out) || !TEST_ptr(*out = OPENSSL_malloc(len + 1))) return 0; *outlen = len + 1; memcpy(*out + 1, protos, len); prefix = 0; i = prefix + 1; while (i <= len) { if ((*out)[i] == ',') { if (!TEST_int_gt(i - 1, prefix)) goto err; (*out)[prefix] = (unsigned char)(i - 1 - prefix); prefix = i; } i++; } if (!TEST_int_gt(len, prefix)) goto err; (*out)[prefix] = (unsigned char)(len - prefix); return 1; err: OPENSSL_free(*out); *out = NULL; return 0; } #ifndef OPENSSL_NO_NEXTPROTONEG static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { CTX_DATA *ctx_data = (CTX_DATA*)(arg); int ret; ret = SSL_select_next_proto(out, outlen, in, inlen, ctx_data->npn_protocols, ctx_data->npn_protocols_len); return TEST_true(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP) ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL; } static int server_npn_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { CTX_DATA *ctx_data = (CTX_DATA*)(arg); *data = ctx_data->npn_protocols; *len = ctx_data->npn_protocols_len; return SSL_TLSEXT_ERR_OK; } #endif static int server_alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { CTX_DATA *ctx_data = (CTX_DATA*)(arg); int ret; unsigned char *tmp_out; ret = SSL_select_next_proto(&tmp_out, outlen, ctx_data->alpn_protocols, ctx_data->alpn_protocols_len, in, inlen); *out = tmp_out; return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL; } static int generate_session_ticket_cb(SSL *s, void *arg) { CTX_DATA *server_ctx_data = arg; SSL_SESSION *ss = SSL_get_session(s); char *app_data = server_ctx_data->session_ticket_app_data; if (ss == NULL || app_data == NULL) return 0; return SSL_SESSION_set1_ticket_appdata(ss, app_data, strlen(app_data)); } static int decrypt_session_ticket_cb(SSL *s, SSL_SESSION *ss, const unsigned char *keyname, size_t keyname_len, SSL_TICKET_STATUS status, void *arg) { switch (status) { case SSL_TICKET_EMPTY: case SSL_TICKET_NO_DECRYPT: return SSL_TICKET_RETURN_IGNORE_RENEW; case SSL_TICKET_SUCCESS: return SSL_TICKET_RETURN_USE; case SSL_TICKET_SUCCESS_RENEW: return SSL_TICKET_RETURN_USE_RENEW; default: break; } return SSL_TICKET_RETURN_ABORT; } static int configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, const SSL_TEST_CTX *test, const SSL_TEST_EXTRA_CONF *extra, CTX_DATA *server_ctx_data, CTX_DATA *server2_ctx_data, CTX_DATA *client_ctx_data) { unsigned char *ticket_keys; size_t ticket_key_len; if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server_ctx, test->max_fragment_size), 1)) goto err; if (server2_ctx != NULL) { if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server2_ctx, test->max_fragment_size), 1)) goto err; } if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(client_ctx, test->max_fragment_size), 1)) goto err; switch (extra->client.verify_callback) { case SSL_TEST_VERIFY_ACCEPT_ALL: SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb, NULL); break; case SSL_TEST_VERIFY_RETRY_ONCE: n_retries = 1; SSL_CTX_set_cert_verify_callback(client_ctx, &verify_retry_cb, NULL); break; case SSL_TEST_VERIFY_REJECT_ALL: SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, NULL); break; case SSL_TEST_VERIFY_NONE: break; } switch (extra->client.max_fragment_len_mode) { case TLSEXT_max_fragment_length_512: case TLSEXT_max_fragment_length_1024: case TLSEXT_max_fragment_length_2048: case TLSEXT_max_fragment_length_4096: case TLSEXT_max_fragment_length_DISABLED: SSL_CTX_set_tlsext_max_fragment_length( client_ctx, extra->client.max_fragment_len_mode); break; } switch (extra->server.servername_callback) { case SSL_TEST_SERVERNAME_IGNORE_MISMATCH: SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb); SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); break; case SSL_TEST_SERVERNAME_REJECT_MISMATCH: SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb); SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); break; case SSL_TEST_SERVERNAME_CB_NONE: break; case SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH: SSL_CTX_set_client_hello_cb(server_ctx, client_hello_ignore_cb, server2_ctx); break; case SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH: SSL_CTX_set_client_hello_cb(server_ctx, client_hello_reject_cb, server2_ctx); break; case SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12: SSL_CTX_set_client_hello_cb(server_ctx, client_hello_nov12_cb, server2_ctx); } if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) { SSL_CTX_set_tlsext_status_type(client_ctx, TLSEXT_STATUSTYPE_ocsp); SSL_CTX_set_tlsext_status_cb(client_ctx, client_ocsp_cb); SSL_CTX_set_tlsext_status_arg(client_ctx, NULL); SSL_CTX_set_tlsext_status_cb(server_ctx, server_ocsp_cb); SSL_CTX_set_tlsext_status_arg(server_ctx, ((extra->server.cert_status == SSL_TEST_CERT_STATUS_GOOD_RESPONSE) ? &dummy_ocsp_resp_good_val : &dummy_ocsp_resp_bad_val)); } if (server2_ctx != NULL) SSL_CTX_set_tlsext_ticket_key_evp_cb(server2_ctx, do_not_call_session_ticket_cb); if (extra->server.broken_session_ticket) { SSL_CTX_set_tlsext_ticket_key_evp_cb(server_ctx, broken_session_ticket_cb); } #ifndef OPENSSL_NO_NEXTPROTONEG if (extra->server.npn_protocols != NULL) { if (!TEST_true(parse_protos(extra->server.npn_protocols, &server_ctx_data->npn_protocols, &server_ctx_data->npn_protocols_len))) goto err; SSL_CTX_set_npn_advertised_cb(server_ctx, server_npn_cb, server_ctx_data); } if (extra->server2.npn_protocols != NULL) { if (!TEST_true(parse_protos(extra->server2.npn_protocols, &server2_ctx_data->npn_protocols, &server2_ctx_data->npn_protocols_len)) || !TEST_ptr(server2_ctx)) goto err; SSL_CTX_set_npn_advertised_cb(server2_ctx, server_npn_cb, server2_ctx_data); } if (extra->client.npn_protocols != NULL) { if (!TEST_true(parse_protos(extra->client.npn_protocols, &client_ctx_data->npn_protocols, &client_ctx_data->npn_protocols_len))) goto err; SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb, client_ctx_data); } #endif if (extra->server.alpn_protocols != NULL) { if (!TEST_true(parse_protos(extra->server.alpn_protocols, &server_ctx_data->alpn_protocols, &server_ctx_data->alpn_protocols_len))) goto err; SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data); } if (extra->server2.alpn_protocols != NULL) { if (!TEST_ptr(server2_ctx) || !TEST_true(parse_protos(extra->server2.alpn_protocols, &server2_ctx_data->alpn_protocols, &server2_ctx_data->alpn_protocols_len ))) goto err; SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, server2_ctx_data); } if (extra->client.alpn_protocols != NULL) { unsigned char *alpn_protos = NULL; size_t alpn_protos_len = 0; if (!TEST_true(parse_protos(extra->client.alpn_protocols, &alpn_protos, &alpn_protos_len)) || !TEST_int_eq(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos, alpn_protos_len), 0)) goto err; OPENSSL_free(alpn_protos); } if (extra->server.session_ticket_app_data != NULL) { server_ctx_data->session_ticket_app_data = OPENSSL_strdup(extra->server.session_ticket_app_data); if (!TEST_ptr(server_ctx_data->session_ticket_app_data)) goto err; SSL_CTX_set_session_ticket_cb(server_ctx, generate_session_ticket_cb, decrypt_session_ticket_cb, server_ctx_data); } if (extra->server2.session_ticket_app_data != NULL) { if (!TEST_ptr(server2_ctx)) goto err; server2_ctx_data->session_ticket_app_data = OPENSSL_strdup(extra->server2.session_ticket_app_data); if (!TEST_ptr(server2_ctx_data->session_ticket_app_data)) goto err; SSL_CTX_set_session_ticket_cb(server2_ctx, NULL, decrypt_session_ticket_cb, server2_ctx_data); } ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0); if (!TEST_ptr(ticket_keys = OPENSSL_zalloc(ticket_key_len)) || !TEST_int_eq(SSL_CTX_set_tlsext_ticket_keys(server_ctx, ticket_keys, ticket_key_len), 1)) { OPENSSL_free(ticket_keys); goto err; } OPENSSL_free(ticket_keys); #if !defined(OPENSSL_NO_CT) && !defined(OPENSSL_NO_EC) if (!TEST_true(SSL_CTX_set_default_ctlog_list_file(client_ctx))) goto err; switch (extra->client.ct_validation) { case SSL_TEST_CT_VALIDATION_PERMISSIVE: if (!TEST_true(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_PERMISSIVE))) goto err; break; case SSL_TEST_CT_VALIDATION_STRICT: if (!TEST_true(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT))) goto err; break; case SSL_TEST_CT_VALIDATION_NONE: break; } #endif #ifndef OPENSSL_NO_SRP if (!configure_handshake_ctx_for_srp(server_ctx, server2_ctx, client_ctx, extra, server_ctx_data, server2_ctx_data, client_ctx_data)) goto err; #endif #ifndef OPENSSL_NO_COMP_ALG if (test->compress_certificates) { if (!TEST_true(SSL_CTX_compress_certs(server_ctx, 0))) goto err; if (server2_ctx != NULL && !TEST_true(SSL_CTX_compress_certs(server2_ctx, 0))) goto err; } #endif return 1; err: return 0; } static void configure_handshake_ssl(SSL *server, SSL *client, const SSL_TEST_EXTRA_CONF *extra) { if (extra->client.servername != SSL_TEST_SERVERNAME_NONE) SSL_set_tlsext_host_name(client, ssl_servername_name(extra->client.servername)); if (extra->client.enable_pha) SSL_set_post_handshake_auth(client, 1); } typedef enum { PEER_SUCCESS, PEER_RETRY, PEER_ERROR, PEER_WAITING, PEER_TEST_FAILURE } peer_status_t; typedef struct peer_st { SSL *ssl; unsigned char *write_buf; int write_buf_len; unsigned char *read_buf; int read_buf_len; int bytes_to_write; int bytes_to_read; peer_status_t status; } PEER; static int create_peer(PEER *peer, SSL_CTX *ctx) { static const int peer_buffer_size = 64 * 1024; SSL *ssl = NULL; unsigned char *read_buf = NULL, *write_buf = NULL; if (!TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(write_buf = OPENSSL_zalloc(peer_buffer_size)) || !TEST_ptr(read_buf = OPENSSL_zalloc(peer_buffer_size))) goto err; peer->ssl = ssl; peer->write_buf = write_buf; peer->read_buf = read_buf; peer->write_buf_len = peer->read_buf_len = peer_buffer_size; return 1; err: SSL_free(ssl); OPENSSL_free(write_buf); OPENSSL_free(read_buf); return 0; } static void peer_free_data(PEER *peer) { SSL_free(peer->ssl); OPENSSL_free(peer->write_buf); OPENSSL_free(peer->read_buf); } static void do_handshake_step(PEER *peer) { if (!TEST_int_eq(peer->status, PEER_RETRY)) { peer->status = PEER_TEST_FAILURE; } else { int ret = SSL_do_handshake(peer->ssl); if (ret == 1) { peer->status = PEER_SUCCESS; } else if (ret == 0) { peer->status = PEER_ERROR; } else { int error = SSL_get_error(peer->ssl, ret); if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_RETRY_VERIFY) peer->status = PEER_ERROR; } } } static void do_app_data_step(PEER *peer) { int ret = 1, write_bytes; if (!TEST_int_eq(peer->status, PEER_RETRY)) { peer->status = PEER_TEST_FAILURE; return; } while (ret > 0 && peer->bytes_to_read) { ret = SSL_read(peer->ssl, peer->read_buf, peer->read_buf_len); if (ret > 0) { if (!TEST_int_le(ret, peer->bytes_to_read)) { peer->status = PEER_TEST_FAILURE; return; } peer->bytes_to_read -= ret; } else if (ret == 0) { peer->status = PEER_ERROR; return; } else { int error = SSL_get_error(peer->ssl, ret); if (error != SSL_ERROR_WANT_READ) { peer->status = PEER_ERROR; return; } } } write_bytes = peer->bytes_to_write < peer->write_buf_len ? peer->bytes_to_write : peer->write_buf_len; if (write_bytes) { ret = SSL_write(peer->ssl, peer->write_buf, write_bytes); if (ret > 0) { if (!TEST_int_eq(ret, write_bytes)) { peer->status = PEER_TEST_FAILURE; return; } peer->bytes_to_write -= ret; } else { peer->status = PEER_ERROR; return; } } if (peer->bytes_to_write == 0 && peer->bytes_to_read == 0) { peer->status = PEER_SUCCESS; } } static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer) { int ret; char buf; if (peer->status == PEER_SUCCESS) { peer->status = PEER_RETRY; do_handshake_step(peer); return; } if (!TEST_int_eq(peer->status, PEER_RETRY) || !TEST_true(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH)) { peer->status = PEER_TEST_FAILURE; return; } peer->bytes_to_write = peer->bytes_to_read = test_ctx->app_data_size; if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER && SSL_is_server(peer->ssl)) || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT && !SSL_is_server(peer->ssl))) { if (!SSL_renegotiate_pending(peer->ssl)) { if (SSL_is_server(peer->ssl)) { ret = SSL_renegotiate(peer->ssl); } else { int full_reneg = 0; if (test_ctx->extra.client.no_extms_on_reneg) { SSL_set_options(peer->ssl, SSL_OP_NO_EXTENDED_MASTER_SECRET); full_reneg = 1; } if (test_ctx->extra.client.reneg_ciphers != NULL) { if (!SSL_set_cipher_list(peer->ssl, test_ctx->extra.client.reneg_ciphers)) { peer->status = PEER_ERROR; return; } full_reneg = 1; } if (full_reneg) ret = SSL_renegotiate(peer->ssl); else ret = SSL_renegotiate_abbreviated(peer->ssl); } if (!ret) { peer->status = PEER_ERROR; return; } do_handshake_step(peer); if (peer->status == PEER_RETRY) peer->status = PEER_SUCCESS; else if (peer->status == PEER_SUCCESS) peer->status = PEER_RETRY; return; } } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) { if (SSL_is_server(peer->ssl) != (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER)) { peer->status = PEER_SUCCESS; return; } ret = SSL_key_update(peer->ssl, test_ctx->key_update_type); if (!ret) { peer->status = PEER_ERROR; return; } do_handshake_step(peer); if (peer->status != PEER_SUCCESS) peer->status = PEER_ERROR; return; } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH) { if (SSL_is_server(peer->ssl)) { SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(peer->ssl); if (sc == NULL) { peer->status = PEER_ERROR; return; } if (test_ctx->extra.server.force_pha) sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; ret = SSL_verify_client_post_handshake(peer->ssl); if (!ret) { peer->status = PEER_ERROR; return; } } do_handshake_step(peer); if (peer->status != PEER_SUCCESS) peer->status = PEER_ERROR; return; } ret = SSL_read(peer->ssl, &buf, sizeof(buf)); if (ret >= 0) { peer->status = PEER_ERROR; return; } else { int error = SSL_get_error(peer->ssl, ret); if (error != SSL_ERROR_WANT_READ) { peer->status = PEER_ERROR; return; } if (!SSL_in_init(peer->ssl)) return; } peer->status = PEER_SUCCESS; } static void do_shutdown_step(PEER *peer) { int ret; if (!TEST_int_eq(peer->status, PEER_RETRY)) { peer->status = PEER_TEST_FAILURE; return; } ret = SSL_shutdown(peer->ssl); if (ret == 1) { peer->status = PEER_SUCCESS; } else if (ret < 0) { int error = SSL_get_error(peer->ssl, ret); if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) peer->status = PEER_ERROR; } } typedef enum { HANDSHAKE, RENEG_APPLICATION_DATA, RENEG_SETUP, RENEG_HANDSHAKE, APPLICATION_DATA, SHUTDOWN, CONNECTION_DONE } connect_phase_t; static int renegotiate_op(const SSL_TEST_CTX *test_ctx) { switch (test_ctx->handshake_mode) { case SSL_TEST_HANDSHAKE_RENEG_SERVER: case SSL_TEST_HANDSHAKE_RENEG_CLIENT: return 1; default: return 0; } } static int post_handshake_op(const SSL_TEST_CTX *test_ctx) { switch (test_ctx->handshake_mode) { case SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT: case SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER: case SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH: return 1; default: return 0; } } static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx, connect_phase_t phase) { switch (phase) { case HANDSHAKE: if (renegotiate_op(test_ctx) || post_handshake_op(test_ctx)) return RENEG_APPLICATION_DATA; return APPLICATION_DATA; case RENEG_APPLICATION_DATA: return RENEG_SETUP; case RENEG_SETUP: if (post_handshake_op(test_ctx)) return APPLICATION_DATA; return RENEG_HANDSHAKE; case RENEG_HANDSHAKE: return APPLICATION_DATA; case APPLICATION_DATA: return SHUTDOWN; case SHUTDOWN: return CONNECTION_DONE; case CONNECTION_DONE: TEST_error("Trying to progress after connection done"); break; } return -1; } static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer, connect_phase_t phase) { switch (phase) { case HANDSHAKE: do_handshake_step(peer); break; case RENEG_APPLICATION_DATA: do_app_data_step(peer); break; case RENEG_SETUP: do_reneg_setup_step(test_ctx, peer); break; case RENEG_HANDSHAKE: do_handshake_step(peer); break; case APPLICATION_DATA: do_app_data_step(peer); break; case SHUTDOWN: do_shutdown_step(peer); break; case CONNECTION_DONE: TEST_error("Action after connection done"); break; } } typedef enum { HANDSHAKE_SUCCESS, CLIENT_ERROR, SERVER_ERROR, INTERNAL_ERROR, HANDSHAKE_RETRY } handshake_status_t; static handshake_status_t handshake_status(peer_status_t last_status, peer_status_t previous_status, int client_spoke_last) { switch (last_status) { case PEER_TEST_FAILURE: return INTERNAL_ERROR; case PEER_WAITING: return INTERNAL_ERROR; case PEER_SUCCESS: switch (previous_status) { case PEER_TEST_FAILURE: return INTERNAL_ERROR; case PEER_SUCCESS: return HANDSHAKE_SUCCESS; case PEER_WAITING: case PEER_RETRY: return HANDSHAKE_RETRY; case PEER_ERROR: return INTERNAL_ERROR; } break; case PEER_RETRY: return HANDSHAKE_RETRY; case PEER_ERROR: switch (previous_status) { case PEER_TEST_FAILURE: return INTERNAL_ERROR; case PEER_WAITING: return client_spoke_last ? CLIENT_ERROR : INTERNAL_ERROR; case PEER_SUCCESS: return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR; case PEER_RETRY: return HANDSHAKE_RETRY; case PEER_ERROR: return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR; } } return INTERNAL_ERROR; } static char *dup_str(const unsigned char *in, size_t len) { char *ret = NULL; if (len == 0) return NULL; if (TEST_size_t_eq(OPENSSL_strnlen((const char*)(in), len), len)) TEST_ptr(ret = OPENSSL_strndup((const char*)(in), len)); return ret; } static int pkey_type(EVP_PKEY *pkey) { if (EVP_PKEY_is_a(pkey, "EC")) { char name[80]; size_t name_len; if (!EVP_PKEY_get_group_name(pkey, name, sizeof(name), &name_len)) return NID_undef; return OBJ_txt2nid(name); } return EVP_PKEY_get_id(pkey); } static int peer_pkey_type(SSL *s) { X509 *x = SSL_get0_peer_certificate(s); if (x != NULL) return pkey_type(X509_get0_pubkey(x)); return NID_undef; } #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) static int set_sock_as_sctp(int sock) { struct sctp_assocparams assocparams; struct sctp_rtoinfo rto_info; BIO *tmpbio; memset(&rto_info, 0, sizeof(struct sctp_rtoinfo)); rto_info.srto_initial = 100; rto_info.srto_max = 200; rto_info.srto_min = 50; (void)setsockopt(sock, IPPROTO_SCTP, SCTP_RTOINFO, (const void *)&rto_info, sizeof(struct sctp_rtoinfo)); memset(&assocparams, 0, sizeof(struct sctp_assocparams)); assocparams.sasoc_asocmaxrxt = 2; (void)setsockopt(sock, IPPROTO_SCTP, SCTP_ASSOCINFO, (const void *)&assocparams, sizeof(struct sctp_assocparams)); tmpbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE); if (tmpbio == NULL) return 0; BIO_free(tmpbio); return 1; } static int create_sctp_socks(int *ssock, int *csock) { BIO_ADDRINFO *res = NULL; const BIO_ADDRINFO *ai = NULL; int lsock = INVALID_SOCKET, asock = INVALID_SOCKET; int consock = INVALID_SOCKET; int ret = 0; int family = 0; if (BIO_sock_init() != 1) return 0; if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_SERVER, family, SOCK_STREAM, IPPROTO_SCTP, &res)) return 0; for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) { family = BIO_ADDRINFO_family(ai); lsock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); if (lsock == INVALID_SOCKET) { continue; } if (!set_sock_as_sctp(lsock) || !BIO_listen(lsock, BIO_ADDRINFO_address(ai), BIO_SOCK_REUSEADDR)) { BIO_closesocket(lsock); lsock = INVALID_SOCKET; continue; } break; } if (lsock == INVALID_SOCKET) goto err; BIO_ADDRINFO_free(res); res = NULL; if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_CLIENT, family, SOCK_STREAM, IPPROTO_SCTP, &res)) goto err; consock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); if (consock == INVALID_SOCKET) goto err; if (!set_sock_as_sctp(consock) || !BIO_connect(consock, BIO_ADDRINFO_address(res), 0) || !BIO_socket_nbio(consock, 1)) goto err; asock = BIO_accept_ex(lsock, NULL, BIO_SOCK_NONBLOCK); if (asock == INVALID_SOCKET) goto err; *csock = consock; *ssock = asock; consock = asock = INVALID_SOCKET; ret = 1; err: BIO_ADDRINFO_free(res); if (consock != INVALID_SOCKET) BIO_closesocket(consock); if (lsock != INVALID_SOCKET) BIO_closesocket(lsock); if (asock != INVALID_SOCKET) BIO_closesocket(asock); return ret; } #endif static HANDSHAKE_RESULT *do_handshake_internal( SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx, const SSL_TEST_EXTRA_CONF *extra, SSL_SESSION *session_in, SSL_SESSION *serv_sess_in, SSL_SESSION **session_out, SSL_SESSION **serv_sess_out) { PEER server, client; BIO *client_to_server = NULL, *server_to_client = NULL; HANDSHAKE_EX_DATA server_ex_data, client_ex_data; CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data; HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new(); int client_turn = 1, client_turn_count = 0, client_wait_count = 0; connect_phase_t phase = HANDSHAKE; handshake_status_t status = HANDSHAKE_RETRY; const unsigned char* tick = NULL; size_t tick_len = 0; const unsigned char* sess_id = NULL; unsigned int sess_id_len = 0; SSL_SESSION* sess = NULL; const unsigned char *proto = NULL; unsigned int proto_len = 0; EVP_PKEY *tmp_key; const STACK_OF(X509_NAME) *names; time_t start; const char* cipher; if (ret == NULL) return NULL; memset(&server_ctx_data, 0, sizeof(server_ctx_data)); memset(&server2_ctx_data, 0, sizeof(server2_ctx_data)); memset(&client_ctx_data, 0, sizeof(client_ctx_data)); memset(&server, 0, sizeof(server)); memset(&client, 0, sizeof(client)); memset(&server_ex_data, 0, sizeof(server_ex_data)); memset(&client_ex_data, 0, sizeof(client_ex_data)); if (!configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx, extra, &server_ctx_data, &server2_ctx_data, &client_ctx_data)) { TEST_note("configure_handshake_ctx"); HANDSHAKE_RESULT_free(ret); return NULL; } #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) if (test_ctx->enable_client_sctp_label_bug) SSL_CTX_set_mode(client_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); if (test_ctx->enable_server_sctp_label_bug) SSL_CTX_set_mode(server_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); #endif if (!create_peer(&server, server_ctx)) { TEST_note("creating server context"); goto err; } if (!create_peer(&client, client_ctx)) { TEST_note("creating client context"); goto err; } server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size; client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size; configure_handshake_ssl(server.ssl, client.ssl, extra); if (session_in != NULL) { SSL_SESSION_get_id(serv_sess_in, &sess_id_len); if ((sess_id_len > 0 && !TEST_true(SSL_CTX_add_session(server_ctx, serv_sess_in))) || !TEST_true(SSL_set_session(client.ssl, session_in))) goto err; sess_id_len = 0; } ret->result = SSL_TEST_INTERNAL_ERROR; if (test_ctx->use_sctp) { #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) int csock, ssock; if (create_sctp_socks(&ssock, &csock)) { client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE); server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE); } #endif } else { client_to_server = BIO_new(BIO_s_mem()); server_to_client = BIO_new(BIO_s_mem()); } if (!TEST_ptr(client_to_server) || !TEST_ptr(server_to_client)) goto err; BIO_set_nbio(client_to_server, 1); BIO_set_nbio(server_to_client, 1); SSL_set_connect_state(client.ssl); SSL_set_accept_state(server.ssl); if (test_ctx->use_sctp) { SSL_set_bio(client.ssl, client_to_server, client_to_server); SSL_set_bio(server.ssl, server_to_client, server_to_client); } else { SSL_set_bio(client.ssl, server_to_client, client_to_server); if (!TEST_int_gt(BIO_up_ref(server_to_client), 0) || !TEST_int_gt(BIO_up_ref(client_to_server), 0)) goto err; SSL_set_bio(server.ssl, client_to_server, server_to_client); } ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL); if (!TEST_int_ge(ex_data_idx, 0) || !TEST_int_eq(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data), 1) || !TEST_int_eq(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data), 1)) goto err; SSL_set_info_callback(server.ssl, &info_cb); SSL_set_info_callback(client.ssl, &info_cb); client.status = PEER_RETRY; server.status = PEER_WAITING; start = time(NULL); for (;;) { if (client_turn) { do_connect_step(test_ctx, &client, phase); status = handshake_status(client.status, server.status, 1 ); if (server.status == PEER_WAITING) server.status = PEER_RETRY; } else { do_connect_step(test_ctx, &server, phase); status = handshake_status(server.status, client.status, 0 ); } switch (status) { case HANDSHAKE_SUCCESS: client_turn_count = 0; phase = next_phase(test_ctx, phase); if (phase == CONNECTION_DONE) { ret->result = SSL_TEST_SUCCESS; goto err; } else { client.status = server.status = PEER_RETRY; client_turn = 1; break; } case CLIENT_ERROR: ret->result = SSL_TEST_CLIENT_FAIL; goto err; case SERVER_ERROR: ret->result = SSL_TEST_SERVER_FAIL; goto err; case INTERNAL_ERROR: ret->result = SSL_TEST_INTERNAL_ERROR; goto err; case HANDSHAKE_RETRY: if (test_ctx->use_sctp) { if (time(NULL) - start > 3) { ret->result = SSL_TEST_INTERNAL_ERROR; goto err; } if ((client_turn && server.status == PEER_RETRY) || (!client_turn && client.status == PEER_RETRY)) client_turn ^= 1; } else { if (client_turn_count++ >= 2000) { ret->result = SSL_TEST_INTERNAL_ERROR; goto err; } if (client_turn && server.status == PEER_SUCCESS) { if (client_wait_count++ >= 2) { ret->result = SSL_TEST_INTERNAL_ERROR; goto err; } } else { client_turn ^= 1; } } break; } } err: ret->server_alert_sent = server_ex_data.alert_sent; ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent; ret->server_alert_received = client_ex_data.alert_received; ret->client_alert_sent = client_ex_data.alert_sent; ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent; ret->client_alert_received = server_ex_data.alert_received; ret->server_protocol = SSL_version(server.ssl); ret->client_protocol = SSL_version(client.ssl); ret->servername = server_ex_data.servername; if ((sess = SSL_get0_session(client.ssl)) != NULL) { SSL_SESSION_get0_ticket(sess, &tick, &tick_len); sess_id = SSL_SESSION_get_id(sess, &sess_id_len); } if (tick == NULL || tick_len == 0) ret->session_ticket = SSL_TEST_SESSION_TICKET_NO; else ret->session_ticket = SSL_TEST_SESSION_TICKET_YES; ret->compression = (SSL_get_current_compression(client.ssl) == NULL) ? SSL_TEST_COMPRESSION_NO : SSL_TEST_COMPRESSION_YES; if (sess_id == NULL || sess_id_len == 0) ret->session_id = SSL_TEST_SESSION_ID_NO; else ret->session_id = SSL_TEST_SESSION_ID_YES; ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call; if (extra->client.verify_callback == SSL_TEST_VERIFY_RETRY_ONCE && n_retries != -1) ret->result = SSL_TEST_SERVER_FAIL; #ifndef OPENSSL_NO_NEXTPROTONEG SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len); ret->client_npn_negotiated = dup_str(proto, proto_len); SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len); ret->server_npn_negotiated = dup_str(proto, proto_len); #endif SSL_get0_alpn_selected(client.ssl, &proto, &proto_len); ret->client_alpn_negotiated = dup_str(proto, proto_len); SSL_get0_alpn_selected(server.ssl, &proto, &proto_len); ret->server_alpn_negotiated = dup_str(proto, proto_len); if ((sess = SSL_get0_session(server.ssl)) != NULL) { SSL_SESSION_get0_ticket_appdata(sess, (void**)&tick, &tick_len); ret->result_session_ticket_app_data = OPENSSL_strndup((const char*)tick, tick_len); } ret->client_resumed = SSL_session_reused(client.ssl); ret->server_resumed = SSL_session_reused(server.ssl); cipher = SSL_CIPHER_get_name(SSL_get_current_cipher(client.ssl)); ret->cipher = dup_str((const unsigned char*)cipher, strlen(cipher)); if (session_out != NULL) *session_out = SSL_get1_session(client.ssl); if (serv_sess_out != NULL) { SSL_SESSION *tmp = SSL_get_session(server.ssl); if (tmp != NULL) *serv_sess_out = SSL_SESSION_dup(tmp); } if (SSL_get_peer_tmp_key(client.ssl, &tmp_key)) { ret->tmp_key_type = pkey_type(tmp_key); EVP_PKEY_free(tmp_key); } SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash); SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash); SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type); SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type); names = SSL_get0_peer_CA_list(client.ssl); if (names == NULL) ret->client_ca_names = NULL; else ret->client_ca_names = SSL_dup_CA_list(names); names = SSL_get0_peer_CA_list(server.ssl); if (names == NULL) ret->server_ca_names = NULL; else ret->server_ca_names = SSL_dup_CA_list(names); ret->server_cert_type = peer_pkey_type(client.ssl); ret->client_cert_type = peer_pkey_type(server.ssl); ctx_data_free_data(&server_ctx_data); ctx_data_free_data(&server2_ctx_data); ctx_data_free_data(&client_ctx_data); peer_free_data(&server); peer_free_data(&client); return ret; } HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx, SSL_CTX *resume_client_ctx, const SSL_TEST_CTX *test_ctx) { HANDSHAKE_RESULT *result; SSL_SESSION *session = NULL, *serv_sess = NULL; result = do_handshake_internal(server_ctx, server2_ctx, client_ctx, test_ctx, &test_ctx->extra, NULL, NULL, &session, &serv_sess); if (result == NULL || test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME || result->result == SSL_TEST_INTERNAL_ERROR) goto end; if (result->result != SSL_TEST_SUCCESS) { result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED; goto end; } HANDSHAKE_RESULT_free(result); result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx, test_ctx, &test_ctx->resume_extra, session, serv_sess, NULL, NULL); end: SSL_SESSION_free(session); SSL_SESSION_free(serv_sess); return result; }
helpers
openssl/test/helpers/handshake.c
openssl
#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
helpers
openssl/test/helpers/predefined_dhparams.c
openssl
#include "cmp_testlib.h" #include <openssl/rsa.h> OSSL_CMP_MSG *load_pkimsg(const char *file, OSSL_LIB_CTX *libctx) { OSSL_CMP_MSG *msg; (void)TEST_ptr((msg = OSSL_CMP_MSG_read(file, libctx, NULL))); return msg; } int valid_asn1_encoding(const OSSL_CMP_MSG *msg) { return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0; } int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2) { int i, res; X509 *a, *b; if (sk1 == sk2) return 0; if (sk1 == NULL) return -1; if (sk2 == NULL) return 1; if ((res = sk_X509_num(sk1) - sk_X509_num(sk2))) return res; for (i = 0; i < sk_X509_num(sk1); i++) { a = sk_X509_value(sk1, i); b = sk_X509_value(sk2, i); if (a != b) if ((res = X509_cmp(a, b)) != 0) return res; } return 0; } int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert) { int res; if (sk == NULL || cert == NULL) return -1; if (!X509_up_ref(cert)) return -1; res = sk_X509_push(sk, cert); if (res <= 0) X509_free(cert); return res; } int print_to_bio_out(const char *func, const char *file, int line, OSSL_CMP_severity level, const char *msg) { return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg); }
helpers
openssl/test/helpers/cmp_testlib.c
openssl