code
stringlengths
49
1.37M
repo_name
stringclasses
117 values
path
stringlengths
17
73
from
stringclasses
1 value
#include <openssl/evp.h> #include <openssl/provider.h> #include "testutil.h" static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *libprov = NULL; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { NULL } }; return test_options; } static int test_fips_version(int n) { const char *version = test_get_argument(n); if (!TEST_ptr(version)) return 0; return TEST_int_eq(fips_provider_version_match(libctx, version), 1); } int setup_tests(void) { char *config_file = NULL; OPTION_CHOICE o; int n; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } if (!test_get_libctx(&libctx, NULL, config_file, &libprov, NULL)) return 0; n = test_get_argument_count(); if (n == 0) return 0; ADD_ALL_TESTS(test_fips_version, n); return 1; } void cleanup_tests(void) { OSSL_PROVIDER_unload(libprov); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/fips_version_test.c
openssl
#include "internal/deprecated.h" #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_RC2 # include <openssl/rc2.h> static unsigned char RC2key[4][16] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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}, {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, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}, }; static unsigned char RC2plain[4][8] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, }; static unsigned char RC2cipher[4][8] = { {0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7}, {0x21, 0x82, 0x9C, 0x78, 0xA9, 0xF9, 0xC0, 0x74}, {0x13, 0xDB, 0x35, 0x17, 0xD3, 0x21, 0x86, 0x9E}, {0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31}, }; static int test_rc2(const int n) { int testresult = 1; RC2_KEY key; unsigned char buf[8], buf2[8]; RC2_set_key(&key, 16, &(RC2key[n][0]), 0 ); RC2_ecb_encrypt(&RC2plain[n][0], buf, &key, RC2_ENCRYPT); if (!TEST_mem_eq(&RC2cipher[n][0], 8, buf, 8)) testresult = 0; RC2_ecb_encrypt(buf, buf2, &key, RC2_DECRYPT); if (!TEST_mem_eq(&RC2plain[n][0], 8, buf2, 8)) testresult = 0; return testresult; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_RC2 ADD_ALL_TESTS(test_rc2, OSSL_NELEM(RC2key)); #endif return 1; }
test
openssl/test/rc2test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/bio.h> #include <openssl/conf.h> #include <openssl/safestack.h> #include <openssl/err.h> static void dump_section(const char *name, const CONF *cnf) { STACK_OF(CONF_VALUE) *sect = NCONF_get_section(cnf, name); int i; printf("[ %s ]\n", name); for (i = 0; i < sk_CONF_VALUE_num(sect); i++) { CONF_VALUE *cv = sk_CONF_VALUE_value(sect, i); printf("%s = %s\n", cv->name, cv->value); } } int main(int argc, char **argv) { long eline; CONF *conf = NCONF_new(NCONF_default()); int ret = 1; STACK_OF(OPENSSL_CSTRING) *section_names = NULL; if (conf != NULL && NCONF_load(conf, argv[1], &eline)) { int i; section_names = NCONF_get_section_names(conf); for (i = 0; i < sk_OPENSSL_CSTRING_num(section_names); i++) { dump_section(sk_OPENSSL_CSTRING_value(section_names, i), conf); } sk_OPENSSL_CSTRING_free(section_names); ret = 0; } else { ERR_print_errors_fp(stderr); } NCONF_free(conf); return ret; }
test
openssl/test/confdump.c
openssl
#include "internal/event_queue.h" #include "internal/nelem.h" #include "testutil.h" static OSSL_TIME cur_time = { 100 }; OSSL_TIME ossl_time_now(void) { return cur_time; } #define PAYLOAD(s) s, strlen(s) + 1 static int event_test(void) { int res = 0; size_t len = 0; OSSL_EVENT *e1, *e2, e3, *e4 = NULL, *ep = NULL; OSSL_EVENT_QUEUE *q = NULL; void *p; static char payload[] = "payload"; if (!TEST_ptr(q = ossl_event_queue_new()) || !TEST_ptr(e1 = ossl_event_queue_add_new(q, 1, 10, ossl_ticks2time(1100), "ctx 1", PAYLOAD(payload))) || !TEST_ptr(e2 = ossl_event_queue_add_new(q, 2, 5, ossl_ticks2time(1100), "ctx 2", PAYLOAD("data"))) || !TEST_true(ossl_event_queue_add(q, &e3, 3, 20, ossl_ticks2time(1200), "ctx 3", PAYLOAD("more data"))) || !TEST_ptr(e4 = ossl_event_queue_add_new(q, 2, 5, ossl_ticks2time(1150), "ctx 2", PAYLOAD("data"))) || !TEST_uint_eq(ossl_event_get_type(e1), 1) || !TEST_uint_eq(ossl_event_get_priority(e1), 10) || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1)) , 1100) || !TEST_str_eq(ossl_event_get0_ctx(e1), "ctx 1") || !TEST_ptr(p = ossl_event_get0_payload(e1, &len)) || !TEST_str_eq((char *)p, payload) || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_time_until(&e3)), 1100) || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_queue_time_until_next(q)), 1000) || !TEST_true(ossl_event_queue_postpone_until(q, e1, ossl_ticks2time(1200))) || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1)), 1200) || !TEST_true(ossl_event_queue_remove(q, e4))) goto err; ossl_event_free(e4); cur_time = ossl_ticks2time(1000); if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_null(ep)) goto err; cur_time = ossl_ticks2time(1100); if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_eq(ep, e2)) goto err; ossl_event_free(ep); ep = e2 = NULL; if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_null(ep)) goto err; cur_time = ossl_ticks2time(1250); if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_eq(ep, &e3)) goto err; ossl_event_free(ep); ep = NULL; if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_eq(ep, e1)) goto err; ossl_event_free(ep); ep = e1 = NULL; if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep)) || !TEST_ptr_null(ep)) goto err; res = 1; err: ossl_event_free(ep); ossl_event_queue_free(q); return res; } int setup_tests(void) { ADD_TEST(event_test); return 1; }
test
openssl/test/event_queue_test.c
openssl
#include <openssl/ssl.h> #include <openssl/evp.h> #include "../ssl/ssl_local.h" #include "testutil.h" #define IVLEN 12 #define KEYLEN 16 static unsigned char hs_start_hash[] = { 0xc6, 0xc9, 0x18, 0xad, 0x2f, 0x41, 0x99, 0xd5, 0x59, 0x8e, 0xaf, 0x01, 0x16, 0xcb, 0x7a, 0x5c, 0x2c, 0x14, 0xcb, 0x54, 0x78, 0x12, 0x18, 0x88, 0x8d, 0xb7, 0x03, 0x0d, 0xd5, 0x0d, 0x5e, 0x6d }; static unsigned char hs_full_hash[] = { 0xf8, 0xc1, 0x9e, 0x8c, 0x77, 0xc0, 0x38, 0x79, 0xbb, 0xc8, 0xeb, 0x6d, 0x56, 0xe0, 0x0d, 0xd5, 0xd8, 0x6e, 0xf5, 0x59, 0x27, 0xee, 0xfc, 0x08, 0xe1, 0xb0, 0x02, 0xb6, 0xec, 0xe0, 0x5d, 0xbf }; static unsigned char early_secret[] = { 0x33, 0xad, 0x0a, 0x1c, 0x60, 0x7e, 0xc0, 0x3b, 0x09, 0xe6, 0xcd, 0x98, 0x93, 0x68, 0x0c, 0xe2, 0x10, 0xad, 0xf3, 0x00, 0xaa, 0x1f, 0x26, 0x60, 0xe1, 0xb2, 0x2e, 0x10, 0xf1, 0x70, 0xf9, 0x2a }; static unsigned char ecdhe_secret[] = { 0x81, 0x51, 0xd1, 0x46, 0x4c, 0x1b, 0x55, 0x53, 0x36, 0x23, 0xb9, 0xc2, 0x24, 0x6a, 0x6a, 0x0e, 0x6e, 0x7e, 0x18, 0x50, 0x63, 0xe1, 0x4a, 0xfd, 0xaf, 0xf0, 0xb6, 0xe1, 0xc6, 0x1a, 0x86, 0x42 }; static unsigned char handshake_secret[] = { 0x5b, 0x4f, 0x96, 0x5d, 0xf0, 0x3c, 0x68, 0x2c, 0x46, 0xe6, 0xee, 0x86, 0xc3, 0x11, 0x63, 0x66, 0x15, 0xa1, 0xd2, 0xbb, 0xb2, 0x43, 0x45, 0xc2, 0x52, 0x05, 0x95, 0x3c, 0x87, 0x9e, 0x8d, 0x06 }; static const char *client_hts_label = "c hs traffic"; static unsigned char client_hts[] = { 0xe2, 0xe2, 0x32, 0x07, 0xbd, 0x93, 0xfb, 0x7f, 0xe4, 0xfc, 0x2e, 0x29, 0x7a, 0xfe, 0xab, 0x16, 0x0e, 0x52, 0x2b, 0x5a, 0xb7, 0x5d, 0x64, 0xa8, 0x6e, 0x75, 0xbc, 0xac, 0x3f, 0x3e, 0x51, 0x03 }; static unsigned char client_hts_key[] = { 0x26, 0x79, 0xa4, 0x3e, 0x1d, 0x76, 0x78, 0x40, 0x34, 0xea, 0x17, 0x97, 0xd5, 0xad, 0x26, 0x49 }; static unsigned char client_hts_iv[] = { 0x54, 0x82, 0x40, 0x52, 0x90, 0xdd, 0x0d, 0x2f, 0x81, 0xc0, 0xd9, 0x42 }; static const char *server_hts_label = "s hs traffic"; static unsigned char server_hts[] = { 0x3b, 0x7a, 0x83, 0x9c, 0x23, 0x9e, 0xf2, 0xbf, 0x0b, 0x73, 0x05, 0xa0, 0xe0, 0xc4, 0xe5, 0xa8, 0xc6, 0xc6, 0x93, 0x30, 0xa7, 0x53, 0xb3, 0x08, 0xf5, 0xe3, 0xa8, 0x3a, 0xa2, 0xef, 0x69, 0x79 }; static unsigned char server_hts_key[] = { 0xc6, 0x6c, 0xb1, 0xae, 0xc5, 0x19, 0xdf, 0x44, 0xc9, 0x1e, 0x10, 0x99, 0x55, 0x11, 0xac, 0x8b }; static unsigned char server_hts_iv[] = { 0xf7, 0xf6, 0x88, 0x4c, 0x49, 0x81, 0x71, 0x6c, 0x2d, 0x0d, 0x29, 0xa4 }; static unsigned char master_secret[] = { 0x5c, 0x79, 0xd1, 0x69, 0x42, 0x4e, 0x26, 0x2b, 0x56, 0x32, 0x03, 0x62, 0x7b, 0xe4, 0xeb, 0x51, 0x03, 0x3f, 0x58, 0x8c, 0x43, 0xc9, 0xce, 0x03, 0x73, 0x37, 0x2d, 0xbc, 0xbc, 0x01, 0x85, 0xa7 }; static const char *client_ats_label = "c ap traffic"; static unsigned char client_ats[] = { 0xe2, 0xf0, 0xdb, 0x6a, 0x82, 0xe8, 0x82, 0x80, 0xfc, 0x26, 0xf7, 0x3c, 0x89, 0x85, 0x4e, 0xe8, 0x61, 0x5e, 0x25, 0xdf, 0x28, 0xb2, 0x20, 0x79, 0x62, 0xfa, 0x78, 0x22, 0x26, 0xb2, 0x36, 0x26 }; static unsigned char client_ats_key[] = { 0x88, 0xb9, 0x6a, 0xd6, 0x86, 0xc8, 0x4b, 0xe5, 0x5a, 0xce, 0x18, 0xa5, 0x9c, 0xce, 0x5c, 0x87 }; static unsigned char client_ats_iv[] = { 0xb9, 0x9d, 0xc5, 0x8c, 0xd5, 0xff, 0x5a, 0xb0, 0x82, 0xfd, 0xad, 0x19 }; static const char *server_ats_label = "s ap traffic"; static unsigned char server_ats[] = { 0x5b, 0x73, 0xb1, 0x08, 0xd9, 0xac, 0x1b, 0x9b, 0x0c, 0x82, 0x48, 0xca, 0x39, 0x26, 0xec, 0x6e, 0x7b, 0xc4, 0x7e, 0x41, 0x17, 0x06, 0x96, 0x39, 0x87, 0xec, 0x11, 0x43, 0x5d, 0x30, 0x57, 0x19 }; static unsigned char server_ats_key[] = { 0xa6, 0x88, 0xeb, 0xb5, 0xac, 0x82, 0x6d, 0x6f, 0x42, 0xd4, 0x5c, 0x0c, 0xc4, 0x4b, 0x9b, 0x7d }; static unsigned char server_ats_iv[] = { 0xc1, 0xca, 0xd4, 0x42, 0x5a, 0x43, 0x8b, 0x5d, 0xe7, 0x14, 0x83, 0x0a }; int ssl3_digest_cached_records(SSL_CONNECTION *s, int keep) { return 1; } static int full_hash = 0; int ssl_handshake_hash(SSL_CONNECTION *s, unsigned char *out, size_t outlen, size_t *hashlen) { if (sizeof(hs_start_hash) > outlen || sizeof(hs_full_hash) != sizeof(hs_start_hash)) return 0; if (full_hash) { memcpy(out, hs_full_hash, sizeof(hs_full_hash)); *hashlen = sizeof(hs_full_hash); } else { memcpy(out, hs_start_hash, sizeof(hs_start_hash)); *hashlen = sizeof(hs_start_hash); } return 1; } const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s) { return EVP_sha256(); } int ssl_cipher_get_evp_cipher(SSL_CTX *ctx, const SSL_CIPHER *sslc, const EVP_CIPHER **enc) { return 0; } int ssl_cipher_get_evp(SSL_CTX *ctx, const SSL_SESSION *s, const EVP_CIPHER **enc, const EVP_MD **md, int *mac_pkey_type, size_t *mac_secret_size, SSL_COMP **comp, int use_etm) { return 0; } int tls1_alert_code(int code) { return code; } int ssl_log_secret(SSL_CONNECTION *sc, const char *label, const uint8_t *secret, size_t secret_len) { return 1; } const EVP_MD *ssl_md(SSL_CTX *ctx, int idx) { return EVP_sha256(); } void ossl_statem_send_fatal(SSL_CONNECTION *s, int al) { } void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason, const char *fmt, ...) { } int ossl_statem_export_allowed(SSL_CONNECTION *s) { return 1; } int ossl_statem_export_early_allowed(SSL_CONNECTION *s) { return 1; } void ssl_evp_cipher_free(const EVP_CIPHER *cipher) { } void ssl_evp_md_free(const EVP_MD *md) { } int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, int direction, int level, unsigned char *secret, size_t secretlen, unsigned char *key, size_t keylen, unsigned char *iv, size_t ivlen, unsigned char *mackey, size_t mackeylen, const EVP_CIPHER *ciph, size_t taglen, int mactype, const EVP_MD *md, const SSL_COMP *comp, const EVP_MD *kdfdigest) { return 0; } static int test_secret(SSL_CONNECTION *s, unsigned char *prk, const unsigned char *label, size_t labellen, const unsigned char *ref_secret, const unsigned char *ref_key, const unsigned char *ref_iv) { size_t hashsize; unsigned char gensecret[EVP_MAX_MD_SIZE]; unsigned char hash[EVP_MAX_MD_SIZE]; unsigned char key[KEYLEN]; unsigned char iv[IVLEN]; const EVP_MD *md = ssl_handshake_md(s); if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashsize)) { TEST_error("Failed to get hash"); return 0; } if (!tls13_hkdf_expand(s, md, prk, label, labellen, hash, hashsize, gensecret, hashsize, 1)) { TEST_error("Secret generation failed"); return 0; } if (!TEST_mem_eq(gensecret, hashsize, ref_secret, hashsize)) return 0; if (!tls13_derive_key(s, md, gensecret, key, KEYLEN)) { TEST_error("Key generation failed"); return 0; } if (!TEST_mem_eq(key, KEYLEN, ref_key, KEYLEN)) return 0; if (!tls13_derive_iv(s, md, gensecret, iv, IVLEN)) { TEST_error("IV generation failed"); return 0; } if (!TEST_mem_eq(iv, IVLEN, ref_iv, IVLEN)) return 0; return 1; } static int test_handshake_secrets(void) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; SSL_CONNECTION *s; int ret = 0; size_t hashsize; unsigned char out_master_secret[EVP_MAX_MD_SIZE]; size_t master_secret_length; ctx = SSL_CTX_new(TLS_method()); if (!TEST_ptr(ctx)) goto err; ssl = SSL_new(ctx); if (!TEST_ptr(ssl) || !TEST_ptr(s = SSL_CONNECTION_FROM_SSL_ONLY(ssl))) goto err; s->session = SSL_SESSION_new(); if (!TEST_ptr(s->session)) goto err; if (!TEST_true(tls13_generate_secret(s, ssl_handshake_md(s), NULL, NULL, 0, (unsigned char *)&s->early_secret))) { TEST_info("Early secret generation failed"); goto err; } if (!TEST_mem_eq(s->early_secret, sizeof(early_secret), early_secret, sizeof(early_secret))) { TEST_info("Early secret does not match"); goto err; } if (!TEST_true(tls13_generate_handshake_secret(s, ecdhe_secret, sizeof(ecdhe_secret)))) { TEST_info("Handshake secret generation failed"); goto err; } if (!TEST_mem_eq(s->handshake_secret, sizeof(handshake_secret), handshake_secret, sizeof(handshake_secret))) goto err; hashsize = EVP_MD_get_size(ssl_handshake_md(s)); if (!TEST_size_t_eq(sizeof(client_hts), hashsize)) goto err; if (!TEST_size_t_eq(sizeof(client_hts_key), KEYLEN)) goto err; if (!TEST_size_t_eq(sizeof(client_hts_iv), IVLEN)) goto err; if (!TEST_true(test_secret(s, s->handshake_secret, (unsigned char *)client_hts_label, strlen(client_hts_label), client_hts, client_hts_key, client_hts_iv))) { TEST_info("Client handshake secret test failed"); goto err; } if (!TEST_size_t_eq(sizeof(server_hts), hashsize)) goto err; if (!TEST_size_t_eq(sizeof(server_hts_key), KEYLEN)) goto err; if (!TEST_size_t_eq(sizeof(server_hts_iv), IVLEN)) goto err; if (!TEST_true(test_secret(s, s->handshake_secret, (unsigned char *)server_hts_label, strlen(server_hts_label), server_hts, server_hts_key, server_hts_iv))) { TEST_info("Server handshake secret test failed"); goto err; } full_hash = 1; if (!TEST_true(tls13_generate_master_secret(s, out_master_secret, s->handshake_secret, hashsize, &master_secret_length))) { TEST_info("Master secret generation failed"); goto err; } if (!TEST_mem_eq(out_master_secret, master_secret_length, master_secret, sizeof(master_secret))) { TEST_info("Master secret does not match"); goto err; } if (!TEST_size_t_eq(sizeof(client_ats), hashsize)) goto err; if (!TEST_size_t_eq(sizeof(client_ats_key), KEYLEN)) goto err; if (!TEST_size_t_eq(sizeof(client_ats_iv), IVLEN)) goto err; if (!TEST_true(test_secret(s, out_master_secret, (unsigned char *)client_ats_label, strlen(client_ats_label), client_ats, client_ats_key, client_ats_iv))) { TEST_info("Client application data secret test failed"); goto err; } if (!TEST_size_t_eq(sizeof(server_ats), hashsize)) goto err; if (!TEST_size_t_eq(sizeof(server_ats_key), KEYLEN)) goto err; if (!TEST_size_t_eq(sizeof(server_ats_iv), IVLEN)) goto err; if (!TEST_true(test_secret(s, out_master_secret, (unsigned char *)server_ats_label, strlen(server_ats_label), server_ats, server_ats_key, server_ats_iv))) { TEST_info("Server application data secret test failed"); goto err; } ret = 1; err: SSL_free(ssl); SSL_CTX_free(ctx); return ret; } int setup_tests(void) { ADD_TEST(test_handshake_secrets); return 1; }
test
openssl/test/tls13secretstest.c
openssl
#include <string.h> #include <openssl/e_os2.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include "internal/nelem.h" #include "testutil.h" static const char *const names[] = { "a", "b", ".", "*", "@", ".a", "a.", ".b", "b.", ".*", "*.", "*@", "@*", "a@", "@a", "b@", "..", "-example.com", "example-.com", "@@", "**", "*.com", "*com", "*.*.com", "*com", "com*", "*example.com", "*@example.com", "test@*.example.com", "example.com", "www.example.com", "test.www.example.com", "*.example.com", "*.www.example.com", "test.*.example.com", "www.*.com", ".www.example.com", "*www.example.com", "example.net", "xn--rger-koa.example.com", "*.xn--rger-koa.example.com", "www.xn--rger-koa.example.com", "*.good--example.com", "www.good--example.com", "*.xn--bar.com", "xn--foo.xn--bar.com", "a.example.com", "b.example.com", "postmaster@example.com", "Postmaster@example.com", "postmaster@EXAMPLE.COM", NULL }; static const char *const exceptions[] = { "set CN: host: [*.example.com] matches [a.example.com]", "set CN: host: [*.example.com] matches [b.example.com]", "set CN: host: [*.example.com] matches [www.example.com]", "set CN: host: [*.example.com] matches [xn--rger-koa.example.com]", "set CN: host: [*.www.example.com] matches [test.www.example.com]", "set CN: host: [*.www.example.com] matches [.www.example.com]", "set CN: host: [*www.example.com] matches [www.example.com]", "set CN: host: [test.www.example.com] matches [.www.example.com]", "set CN: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]", "set CN: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]", "set CN: host: [*.good--example.com] matches [www.good--example.com]", "set CN: host-no-wildcards: [*.www.example.com] matches [.www.example.com]", "set CN: host-no-wildcards: [test.www.example.com] matches [.www.example.com]", "set emailAddress: email: [postmaster@example.com] does not match [Postmaster@example.com]", "set emailAddress: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]", "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@example.com]", "set emailAddress: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]", "set dnsName: host: [*.example.com] matches [www.example.com]", "set dnsName: host: [*.example.com] matches [a.example.com]", "set dnsName: host: [*.example.com] matches [b.example.com]", "set dnsName: host: [*.example.com] matches [xn--rger-koa.example.com]", "set dnsName: host: [*.www.example.com] matches [test.www.example.com]", "set dnsName: host-no-wildcards: [*.www.example.com] matches [.www.example.com]", "set dnsName: host-no-wildcards: [test.www.example.com] matches [.www.example.com]", "set dnsName: host: [*.www.example.com] matches [.www.example.com]", "set dnsName: host: [*www.example.com] matches [www.example.com]", "set dnsName: host: [test.www.example.com] matches [.www.example.com]", "set dnsName: host: [*.xn--rger-koa.example.com] matches [www.xn--rger-koa.example.com]", "set dnsName: host: [*.xn--bar.com] matches [xn--foo.xn--bar.com]", "set dnsName: host: [*.good--example.com] matches [www.good--example.com]", "set rfc822Name: email: [postmaster@example.com] does not match [Postmaster@example.com]", "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@example.com]", "set rfc822Name: email: [Postmaster@example.com] does not match [postmaster@EXAMPLE.COM]", "set rfc822Name: email: [postmaster@EXAMPLE.COM] does not match [Postmaster@example.com]", NULL }; static int is_exception(const char *msg) { const char *const *p; for (p = exceptions; *p; ++p) if (strcmp(msg, *p) == 0) return 1; return 0; } static int set_cn(X509 *crt, ...) { int ret = 0; X509_NAME *n = NULL; va_list ap; va_start(ap, crt); n = X509_NAME_new(); if (n == NULL) goto out; while (1) { int nid; const char *name; nid = va_arg(ap, int); if (nid == 0) break; name = va_arg(ap, const char *); if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_ASC, (unsigned char *)name, -1, -1, 1)) goto out; } if (!X509_set_subject_name(crt, n)) goto out; ret = 1; out: X509_NAME_free(n); va_end(ap); return ret; } static int set_altname(X509 *crt, ...) { int ret = 0; GENERAL_NAMES *gens = NULL; GENERAL_NAME *gen = NULL; ASN1_IA5STRING *ia5 = NULL; va_list ap; va_start(ap, crt); gens = sk_GENERAL_NAME_new_null(); if (gens == NULL) goto out; while (1) { int type; const char *name; type = va_arg(ap, int); if (type == 0) break; name = va_arg(ap, const char *); gen = GENERAL_NAME_new(); if (gen == NULL) goto out; ia5 = ASN1_IA5STRING_new(); if (ia5 == NULL) goto out; if (!ASN1_STRING_set(ia5, name, -1)) goto out; switch (type) { case GEN_EMAIL: case GEN_DNS: GENERAL_NAME_set0_value(gen, type, ia5); ia5 = NULL; break; default: abort(); } sk_GENERAL_NAME_push(gens, gen); gen = NULL; } if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0)) goto out; ret = 1; out: ASN1_IA5STRING_free(ia5); GENERAL_NAME_free(gen); GENERAL_NAMES_free(gens); va_end(ap); return ret; } static int set_cn1(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, 0); } static int set_cn_and_email(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, NID_pkcs9_emailAddress, "dummy@example.com", 0); } static int set_cn2(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, "dummy value", NID_commonName, name, 0); } static int set_cn3(X509 *crt, const char *name) { return set_cn(crt, NID_commonName, name, NID_commonName, "dummy value", 0); } static int set_email1(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, 0); } static int set_email2(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, "dummy@example.com", NID_pkcs9_emailAddress, name, 0); } static int set_email3(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, NID_pkcs9_emailAddress, "dummy@example.com", 0); } static int set_email_and_cn(X509 *crt, const char *name) { return set_cn(crt, NID_pkcs9_emailAddress, name, NID_commonName, "www.example.org", 0); } static int set_altname_dns(X509 *crt, const char *name) { return set_altname(crt, GEN_DNS, name, 0); } static int set_altname_email(X509 *crt, const char *name) { return set_altname(crt, GEN_EMAIL, name, 0); } struct set_name_fn { int (*fn) (X509 *, const char *); const char *name; int host; int email; }; static const struct set_name_fn name_fns[] = { {set_cn1, "set CN", 1, 0}, {set_cn2, "set CN", 1, 0}, {set_cn3, "set CN", 1, 0}, {set_cn_and_email, "set CN", 1, 0}, {set_email1, "set emailAddress", 0, 1}, {set_email2, "set emailAddress", 0, 1}, {set_email3, "set emailAddress", 0, 1}, {set_email_and_cn, "set emailAddress", 0, 1}, {set_altname_dns, "set dnsName", 1, 0}, {set_altname_email, "set rfc822Name", 0, 1}, }; static X509 *make_cert(void) { X509 *crt = NULL; if (!TEST_ptr(crt = X509_new())) return NULL; if (!TEST_true(X509_set_version(crt, X509_VERSION_3))) { X509_free(crt); return NULL; } return crt; } static int check_message(const struct set_name_fn *fn, const char *op, const char *nameincert, int match, const char *name) { char msg[1024]; if (match < 0) return 1; BIO_snprintf(msg, sizeof(msg), "%s: %s: [%s] %s [%s]", fn->name, op, nameincert, match ? "matches" : "does not match", name); if (is_exception(msg)) return 1; TEST_error("%s", msg); return 0; } static int run_cert(X509 *crt, const char *nameincert, const struct set_name_fn *fn) { const char *const *pname = names; int failed = 0; for (; *pname != NULL; ++pname) { int samename = OPENSSL_strcasecmp(nameincert, *pname) == 0; size_t namelen = strlen(*pname); char *name = OPENSSL_malloc(namelen + 1); int match, ret; if (!TEST_ptr(name)) return 0; memcpy(name, *pname, namelen + 1); match = -1; if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, 0, NULL), 0)) { failed = 1; } else if (fn->host) { if (ret == 1 && !samename) match = 1; if (ret == 0 && samename) match = 0; } else if (ret == 1) match = 1; if (!TEST_true(check_message(fn, "host", nameincert, match, *pname))) failed = 1; match = -1; if (!TEST_int_ge(ret = X509_check_host(crt, name, namelen, X509_CHECK_FLAG_NO_WILDCARDS, NULL), 0)) { failed = 1; } else if (fn->host) { if (ret == 1 && !samename) match = 1; if (ret == 0 && samename) match = 0; } else if (ret == 1) match = 1; if (!TEST_true(check_message(fn, "host-no-wildcards", nameincert, match, *pname))) failed = 1; match = -1; ret = X509_check_email(crt, name, namelen, 0); if (fn->email) { if (ret && !samename) match = 1; if (!ret && samename && strchr(nameincert, '@') != NULL) match = 0; } else if (ret) match = 1; if (!TEST_true(check_message(fn, "email", nameincert, match, *pname))) failed = 1; OPENSSL_free(name); } return failed == 0; } static int call_run_cert(int i) { int failed = 0; const struct set_name_fn *pfn = &name_fns[i]; X509 *crt; const char *const *pname; TEST_info("%s", pfn->name); for (pname = names; *pname != NULL; pname++) { if (!TEST_ptr(crt = make_cert()) || !TEST_true(pfn->fn(crt, *pname)) || !run_cert(crt, *pname, pfn)) failed = 1; X509_free(crt); } return failed == 0; } static struct gennamedata { const unsigned char der[22]; size_t derlen; } gennames[] = { { { 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x30, 0x00 }, 21 }, { { 0xa0, 0x13, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x02, 0x60, 0x00 }, 21 }, { { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x61 }, 22 }, { { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02, 0xa0, 0x03, 0x0c, 0x01, 0x61 }, 22 }, { { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x0c, 0x01, 0x62 }, 22 }, { { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0xff }, 22 }, { { 0xa0, 0x14, 0x06, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01, 0xa0, 0x03, 0x01, 0x01, 0x00 }, 22 }, { { 0x81, 0x01, 0x61 }, 3 }, { { 0x81, 0x01, 0x62 }, 3 }, { { 0x82, 0x01, 0x61 }, 3 }, { { 0x82, 0x01, 0x62 }, 3 }, { { 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x01, 0x61 }, 16 }, { { 0xa4, 0x0e, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x01, 0x62 }, 16 }, { { 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 7 }, { { 0xa5, 0x05, 0xa1, 0x03, 0x0c, 0x01, 0x62 }, 7 }, { { 0xa5, 0x09, 0xa0, 0x02, 0x0c, 0x00, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 11 }, { { 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x61, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 12 }, { { 0xa5, 0x0a, 0xa0, 0x03, 0x0c, 0x01, 0x62, 0xa1, 0x03, 0x0c, 0x01, 0x61 }, 12 }, { { 0x86, 0x01, 0x61 }, 3 }, { { 0x86, 0x01, 0x62 }, 3 }, { { 0x87, 0x04, 0x11, 0x11, 0x11, 0x11 }, 6 }, { { 0x87, 0x04, 0x22, 0x22, 0x22, 0x22 }, 6 }, { { 0x87, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, 18 }, { { 0x87, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }, 18 }, { { 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x01 }, 15 }, { { 0x88, 0x0d, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, 0x02 }, 15 }, { { 0xa3, 0x00 }, 2 } }; static int test_GENERAL_NAME_cmp(void) { size_t i, j; GENERAL_NAME **namesa = OPENSSL_malloc(sizeof(*namesa) * OSSL_NELEM(gennames)); GENERAL_NAME **namesb = OPENSSL_malloc(sizeof(*namesb) * OSSL_NELEM(gennames)); int testresult = 0; if (!TEST_ptr(namesa) || !TEST_ptr(namesb)) goto end; for (i = 0; i < OSSL_NELEM(gennames); i++) { const unsigned char *derp = gennames[i].der; namesa[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); derp = gennames[i].der; namesb[i] = d2i_GENERAL_NAME(NULL, &derp, gennames[i].derlen); if (!TEST_ptr(namesa[i]) || !TEST_ptr(namesb[i])) goto end; } for (i = 0; i < OSSL_NELEM(gennames); i++) { for (j = 0; j < OSSL_NELEM(gennames); j++) { if (i == j) { if (!TEST_int_eq(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0)) goto end; } else { if (!TEST_int_ne(GENERAL_NAME_cmp(namesa[i], namesb[j]), 0)) goto end; } } } testresult = 1; end: for (i = 0; i < OSSL_NELEM(gennames); i++) { if (namesa != NULL) GENERAL_NAME_free(namesa[i]); if (namesb != NULL) GENERAL_NAME_free(namesb[i]); } OPENSSL_free(namesa); OPENSSL_free(namesb); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(call_run_cert, OSSL_NELEM(name_fns)); ADD_TEST(test_GENERAL_NAME_cmp); return 1; }
test
openssl/test/v3nametest.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <openssl/asn1.h> #include <openssl/evp.h> #include <openssl/objects.h> #include "testutil.h" #include "internal/nelem.h" #include "../crypto/asn1/tbl_standard.h" static int test_tbl_standard(void) { const ASN1_STRING_TABLE *tmp; int last_nid = -1; size_t i; for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) { if (tmp->nid < last_nid) { last_nid = 0; break; } last_nid = tmp->nid; } if (TEST_int_ne(last_nid, 0)) { TEST_info("asn1 tbl_standard: Table order OK"); return 1; } TEST_info("asn1 tbl_standard: out of order"); for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s", i, tmp->nid, OBJ_nid2ln(tmp->nid)); return 0; } #include "crypto/asn1.h" #include "../crypto/asn1/standard_methods.h" static int test_standard_methods(void) { const EVP_PKEY_ASN1_METHOD **tmp; int last_pkey_id = -1; size_t i; int ok = 1; for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods); i++, tmp++) { if ((*tmp)->pkey_id < last_pkey_id) { last_pkey_id = 0; break; } last_pkey_id = (*tmp)->pkey_id; if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0) || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) { TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s", i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id)); ok = 0; } } if (TEST_int_ne(last_pkey_id, 0)) { TEST_info("asn1 standard methods: Table order OK"); return ok; } TEST_note("asn1 standard methods: out of order"); for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods); i++, tmp++) TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s", i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id)); return 0; } #include <openssl/rsa.h> static int test_empty_nonoptional_content(void) { RSA *rsa = NULL; BIGNUM *n = NULL; BIGNUM *e = NULL; int ok = 0; if (!TEST_ptr(rsa = RSA_new()) || !TEST_ptr(n = BN_new()) || !TEST_ptr(e = BN_new()) || !TEST_true(RSA_set0_key(rsa, n, e, NULL))) goto end; n = e = NULL; if (TEST_int_le(i2d_RSAPrivateKey(rsa, NULL), 0)) ok = 1; end: RSA_free(rsa); BN_free(n); BN_free(e); return ok; } static int test_unicode(const unsigned char *univ, size_t len, int expected) { const unsigned char *end = univ + len; int ok = 1; for (; univ < end; univ += 4) { if (!TEST_int_eq(ASN1_mbstring_copy(NULL, univ, 4, MBSTRING_UNIV, B_ASN1_UTF8STRING), expected)) ok = 0; } return ok; } static int test_unicode_range(void) { const unsigned char univ_ok[] = "\0\0\0\0" "\0\0\xd7\xff" "\0\0\xe0\x00" "\0\x10\xff\xff"; const unsigned char univ_bad[] = "\0\0\xd8\x00" "\0\0\xdf\xff" "\0\x11\x00\x00" "\x80\x00\x00\x00" "\xff\xff\xff\xff"; int ok = 1; if (!test_unicode(univ_ok, sizeof univ_ok - 1, V_ASN1_UTF8STRING)) ok = 0; if (!test_unicode(univ_bad, sizeof univ_bad - 1, -1)) ok = 0; return ok; } static int test_obj_create_once(const char *oid, const char *sn, const char *ln) { int nid; ERR_set_mark(); nid = OBJ_create(oid, sn, ln); if (nid == NID_undef) { unsigned long err = ERR_peek_last_error(); int l = ERR_GET_LIB(err); int r = ERR_GET_REASON(err); if (l != ERR_LIB_OBJ || r != OBJ_R_OID_EXISTS) { ERR_clear_last_mark(); return 0; } } ERR_pop_to_mark(); return 1; } static int test_obj_create(void) { #define arc "1.3.6.1.4.1.16604.998866." #define broken_arc "25." #define sn_prefix "custom" #define ln_prefix "custom" if (!TEST_true(test_obj_create_once(NULL, sn_prefix "1", NULL)) || !TEST_int_ne(OBJ_sn2nid(sn_prefix "1"), NID_undef) || !TEST_true(test_obj_create_once(NULL, NULL, ln_prefix "2")) || !TEST_int_ne(OBJ_ln2nid(ln_prefix "2"), NID_undef) || !TEST_true(test_obj_create_once(NULL, sn_prefix "3", ln_prefix "3")) || !TEST_int_ne(OBJ_sn2nid(sn_prefix "3"), NID_undef) || !TEST_int_ne(OBJ_ln2nid(ln_prefix "3"), NID_undef) || !TEST_true(test_obj_create_once(arc "4", NULL, NULL)) || !TEST_true(test_obj_create_once(arc "5", sn_prefix "5", NULL)) || !TEST_int_ne(OBJ_sn2nid(sn_prefix "5"), NID_undef) || !TEST_true(test_obj_create_once(arc "6", NULL, ln_prefix "6")) || !TEST_int_ne(OBJ_ln2nid(ln_prefix "6"), NID_undef) || !TEST_true(test_obj_create_once(arc "7", sn_prefix "7", ln_prefix "7")) || !TEST_int_ne(OBJ_sn2nid(sn_prefix "7"), NID_undef) || !TEST_int_ne(OBJ_ln2nid(ln_prefix "7"), NID_undef)) return 0; if (!TEST_false(test_obj_create_once(NULL, NULL, NULL)) || !TEST_false(test_obj_create_once(broken_arc "8", sn_prefix "8", ln_prefix "8"))) return 0; return 1; } static int test_obj_nid_undef(void) { if (!TEST_ptr(OBJ_nid2obj(NID_undef)) || !TEST_ptr(OBJ_nid2sn(NID_undef)) || !TEST_ptr(OBJ_nid2ln(NID_undef))) return 0; return 1; } int setup_tests(void) { ADD_TEST(test_tbl_standard); ADD_TEST(test_standard_methods); ADD_TEST(test_empty_nonoptional_content); ADD_TEST(test_unicode_range); ADD_TEST(test_obj_create); ADD_TEST(test_obj_nid_undef); return 1; }
test
openssl/test/asn1_internal_test.c
openssl
#include <string.h> #include <openssl/buffer.h> #include <openssl/rand.h> #include "internal/packet_quic.h" #include "testutil.h" static const unsigned char simple1[] = { 0xff }; static const unsigned char simple2[] = { 0x01, 0xff }; static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff }; static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff }; static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff }; static const unsigned char empty[] = { 0x00 }; static const unsigned char alloc[] = { 0x02, 0xfe, 0xff }; static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff }; static const unsigned char fixed[] = { 0xff, 0xff, 0xff }; static const unsigned char simpleder[] = { 0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd }; #ifndef OPENSSL_NO_QUIC static const unsigned char quic1[] = { 0x80, 0x00, 0x00, 0x01, 0x09 }; static const unsigned char quic2[] = { 0x01, 0x09 }; static const unsigned char quic3[] = { 0x40, 0x02, 0x40, 0x41 }; static const unsigned char quic4[] = { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x80, 0x01, 0x3c, 0x6a }; static const unsigned char quic5[] = { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xef, 0x77, 0x21, 0x3f, 0x3f, 0x50, 0x5b, 0xa5 }; static const unsigned char quic6[] = { 0x03, 0x55, 0x66, 0x77 }; static const unsigned char quic7[] = { 0x07, 0x80, 0x00, 0x00, 0x08, 0x65, 0x14, 0x40, 0x01, 0x05, 0x40, 0x01, 0x11, 0x40, 0x01, 0x12, 0x40, 0x01, 0x13 }; #endif static BUF_MEM *buf; static int cleanup(WPACKET *pkt) { WPACKET_cleanup(pkt); return 0; } static int test_WPACKET_init(void) { WPACKET pkt; int i; size_t written; unsigned char sbuf[3]; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 4)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))) return cleanup(&pkt); for (i = 1; i < 257; i++) { if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256)) return cleanup(&pkt); } if (!TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0)) || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff)) || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf)) || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1)) || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff)) || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc))) return cleanup(&pkt); return 1; } static int test_WPACKET_set_max_size(void) { WPACKET pkt; size_t written; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX)) || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1)) || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX)) || !TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_false(WPACKET_set_max_size(&pkt, 0)) || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101)) || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100)) || !TEST_true(WPACKET_set_max_size(&pkt, 0x01)) || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_set_max_size(&pkt, 0x02)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); return 1; } static int test_WPACKET_start_sub_packet(void) { WPACKET pkt; size_t written; size_t len; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 3) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 3) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_fill_lengths(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)) || !TEST_true(WPACKET_finish(&pkt))) return cleanup(&pkt); return 1; } static int test_WPACKET_set_flags(void) { WPACKET pkt; size_t written; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_size_t_eq(written, 0)) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, empty, sizeof(empty))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_sub_packet_u8(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2))) return cleanup(&pkt); return 1; } static int test_WPACKET_allocate_bytes(void) { WPACKET pkt; size_t written; unsigned char *bytes; if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes))) return cleanup(&pkt); bytes[0] = 0xfe; bytes[1] = 0xff; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes))) return cleanup(&pkt); bytes[0] = 0xfe; bytes[1] = 0xff; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, submem, sizeof(submem))) return cleanup(&pkt); return 1; } static int test_WPACKET_memcpy(void) { WPACKET pkt; size_t written; const unsigned char bytes[] = { 0xfe, 0xff }; if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes))) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc))) return cleanup(&pkt); if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)) || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, submem, sizeof(submem))) return cleanup(&pkt); return 1; } static int test_WPACKET_init_der(void) { WPACKET pkt; unsigned char sbuf[1024]; unsigned char testdata[] = { 0x00, 0x01, 0x02, 0x03 }; unsigned char testdata2[259] = { 0x82, 0x01, 0x00 }; size_t written[2]; size_t size1, size2; int flags = WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH; int i; if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf))) || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xfffefd)) || !TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_memcpy(&pkt, testdata, sizeof(testdata))) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xfc)) || (!TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_set_flags(&pkt, flags)) || !TEST_true(WPACKET_get_total_written(&pkt, &size1)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &size2)) || !TEST_size_t_eq(size1, size2)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written[0])) || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[0], simpleder, sizeof(simpleder))) return cleanup(&pkt); if (!TEST_int_gt(RAND_bytes(&testdata2[3], sizeof(testdata2) - 3), 0)) return 0; for (i = 0; i < 2; i++) { if (i == 0) { if (!TEST_true(WPACKET_init_null_der(&pkt))) return 0; } else { if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf)))) return 0; } if (!TEST_true(WPACKET_start_sub_packet(&pkt)) || !TEST_true(WPACKET_memcpy(&pkt, &testdata2[3], sizeof(testdata2) - 3)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written[i]))) return cleanup(&pkt); } if (!TEST_size_t_eq(written[0], written[1]) || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[1], testdata2, sizeof(testdata2))) return 0; return 1; } #ifndef OPENSSL_NO_QUIC static int test_WPACKET_quic(void) { WPACKET pkt; size_t written, len; unsigned char *bytes; if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet(&pkt)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic1, sizeof(quic1))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_1B_MAX)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic2, sizeof(quic2))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x41)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic3, sizeof(quic3))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13c6a)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic4, sizeof(quic4))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2f77213f3f505ba5ULL)) || !TEST_false(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_false(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic5, sizeof(quic5))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_sub_allocate_bytes(&pkt, 3, &bytes))) return cleanup(&pkt); bytes[0] = 0x55; bytes[1] = 0x66; bytes[2] = 0x77; if (!TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic6, sizeof(quic6))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x07)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_4B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2514)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 2) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x05)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 1) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x11)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_get_length(&pkt, &len)) || !TEST_size_t_eq(len, 8) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x12)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13)) || !TEST_true(WPACKET_close(&pkt)) || !TEST_true(WPACKET_finish(&pkt)) || !TEST_true(WPACKET_get_total_written(&pkt, &written)) || !TEST_mem_eq(buf->data, written, quic7, sizeof(quic7))) return cleanup(&pkt); if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_false(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX+1)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX))) return cleanup(&pkt); WPACKET_cleanup(&pkt); return 1; } static int test_WPACKET_quic_vlint_random(void) { size_t i, written; uint64_t expected, actual = 0; unsigned char rand_data[9]; WPACKET pkt; PACKET read_pkt = {0}; for (i = 0; i < 10000; ++i) { if (!TEST_int_gt(RAND_bytes(rand_data, sizeof(rand_data)), 0)) return cleanup(&pkt); expected = *(uint64_t*)rand_data; switch (rand_data[8] & 3) { case 0: expected &= OSSL_QUIC_VLINT_1B_MAX; break; case 1: expected &= OSSL_QUIC_VLINT_2B_MAX; break; case 2: expected &= OSSL_QUIC_VLINT_4B_MAX; break; case 3: expected &= OSSL_QUIC_VLINT_8B_MAX; break; } if (!TEST_true(WPACKET_init(&pkt, buf)) || !TEST_true(WPACKET_quic_write_vlint(&pkt, expected)) || !TEST_true(WPACKET_get_total_written(&pkt, &written))) return cleanup(&pkt); if (!TEST_true(PACKET_buf_init(&read_pkt, (unsigned char *)buf->data, written)) || !TEST_true(PACKET_get_quic_vlint(&read_pkt, &actual)) || !TEST_uint64_t_eq(expected, actual)) return cleanup(&pkt); WPACKET_cleanup(&pkt); } WPACKET_cleanup(&pkt); return 1; } #endif int setup_tests(void) { if (!TEST_ptr(buf = BUF_MEM_new())) return 0; ADD_TEST(test_WPACKET_init); ADD_TEST(test_WPACKET_set_max_size); ADD_TEST(test_WPACKET_start_sub_packet); ADD_TEST(test_WPACKET_set_flags); ADD_TEST(test_WPACKET_allocate_bytes); ADD_TEST(test_WPACKET_memcpy); ADD_TEST(test_WPACKET_init_der); #ifndef OPENSSL_NO_QUIC ADD_TEST(test_WPACKET_quic); ADD_TEST(test_WPACKET_quic_vlint_random); #endif return 1; } void cleanup_tests(void) { BUF_MEM_free(buf); }
test
openssl/test/wpackettest.c
openssl
#include <string.h> #include <stdlib.h> #include <openssl/macros.h> #include <openssl/e_os2.h> #include "simpledynamic.h" #if defined(DSO_DLFCN) || defined(DSO_VMS) int sd_load(const char *filename, SD *lib, int type) { int dl_flags = type; #ifdef _AIX if (filename[strlen(filename) - 1] == ')') dl_flags |= RTLD_MEMBER; #endif *lib = dlopen(filename, dl_flags); return *lib == NULL ? 0 : 1; } int sd_sym(SD lib, const char *symname, SD_SYM *sym) { *sym = dlsym(lib, symname); return *sym != NULL; } int sd_close(SD lib) { return dlclose(lib) != 0 ? 0 : 1; } const char *sd_error(void) { return dlerror(); } #elif defined(DSO_WIN32) int sd_load(const char *filename, SD *lib, ossl_unused int type) { *lib = LoadLibraryA(filename); return *lib == NULL ? 0 : 1; } int sd_sym(SD lib, const char *symname, SD_SYM *sym) { *sym = (SD_SYM)GetProcAddress(lib, symname); return *sym != NULL; } int sd_close(SD lib) { return FreeLibrary(lib) == 0 ? 0 : 1; } const char *sd_error(void) { static char buffer[255]; buffer[0] = '\0'; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, buffer, sizeof(buffer), NULL); return buffer; } #else NON_EMPTY_TRANSLATION_UNIT #endif
test
openssl/test/simpledynamic.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/evp.h> #include <openssl/bio.h> #include <openssl/rand.h> #include "testutil.h" #define ENCRYPT 1 #define DECRYPT 0 #define DATA_SIZE 1024 #define MAX_IV 32 #define BUF_SIZE (DATA_SIZE + MAX_IV) static const unsigned char KEY[] = { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a, 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1, 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b }; static const unsigned char IV[] = { 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 }; static int do_bio_cipher(const EVP_CIPHER* cipher, const unsigned char* key, const unsigned char* iv) { BIO *b, *mem; static unsigned char inp[BUF_SIZE] = { 0 }; unsigned char out[BUF_SIZE], ref[BUF_SIZE]; int i, lref, len; if (!TEST_int_gt(RAND_bytes(inp, DATA_SIZE), 0)) return 0; b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) goto err; mem = BIO_new_mem_buf(inp, DATA_SIZE); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); lref = BIO_read(b, ref, sizeof(ref)); BIO_free_all(b); for (i = 1; i < lref; i++) { b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) { TEST_info("Split encrypt failed @ operation %d", i); goto err; } mem = BIO_new_mem_buf(inp, DATA_SIZE); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); memset(out, 0, sizeof(out)); out[i] = ~ref[i]; len = BIO_read(b, out, i); if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) { TEST_info("Encrypt overstep check failed @ operation %d", i); goto err; } len += BIO_read(b, out + len, sizeof(out) - len); BIO_free_all(b); if (!TEST_mem_eq(out, len, ref, lref)) { TEST_info("Encrypt compare failed @ operation %d", i); return 0; } } for (i = 1; i < lref / 2; i++) { int delta; b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, ENCRYPT))) { TEST_info("Small chunk encrypt failed @ operation %d", i); goto err; } mem = BIO_new_mem_buf(inp, DATA_SIZE); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); memset(out, 0, sizeof(out)); for (len = 0; (delta = BIO_read(b, out + len, i)); ) { len += delta; } BIO_free_all(b); if (!TEST_mem_eq(out, len, ref, lref)) { TEST_info("Small chunk encrypt compare failed @ operation %d", i); return 0; } } b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) goto err; mem = BIO_new_mem_buf(ref, lref); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); (void)BIO_flush(b); memset(out, 0, sizeof(out)); len = BIO_read(b, out, sizeof(out)); BIO_free_all(b); if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) return 0; for (i = 1; i < lref; i++) { b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) { TEST_info("Split decrypt failed @ operation %d", i); goto err; } mem = BIO_new_mem_buf(ref, lref); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); memset(out, 0, sizeof(out)); out[i] = ~ref[i]; len = BIO_read(b, out, i); if (!TEST_uchar_eq(out[i], (unsigned char)~ref[i])) { TEST_info("Decrypt overstep check failed @ operation %d", i); goto err; } len += BIO_read(b, out + len, sizeof(out) - len); BIO_free_all(b); if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) { TEST_info("Decrypt compare failed @ operation %d", i); return 0; } } for (i = 1; i < lref / 2; i++) { int delta; b = BIO_new(BIO_f_cipher()); if (!TEST_ptr(b)) return 0; if (!TEST_true(BIO_set_cipher(b, cipher, key, iv, DECRYPT))) { TEST_info("Small chunk decrypt failed @ operation %d", i); goto err; } mem = BIO_new_mem_buf(ref, lref); if (!TEST_ptr(mem)) goto err; BIO_push(b, mem); memset(out, 0, sizeof(out)); for (len = 0; (delta = BIO_read(b, out + len, i)); ) { len += delta; } BIO_free_all(b); if (!TEST_mem_eq(inp, DATA_SIZE, out, len)) { TEST_info("Small chunk decrypt compare failed @ operation %d", i); return 0; } } return 1; err: BIO_free_all(b); return 0; } static int do_test_bio_cipher(const EVP_CIPHER* cipher, int idx) { switch (idx) { case 0: return do_bio_cipher(cipher, KEY, NULL); case 1: return do_bio_cipher(cipher, KEY, IV); } return 0; } static int test_bio_enc_aes_128_cbc(int idx) { return do_test_bio_cipher(EVP_aes_128_cbc(), idx); } static int test_bio_enc_aes_128_ctr(int idx) { return do_test_bio_cipher(EVP_aes_128_ctr(), idx); } static int test_bio_enc_aes_256_cfb(int idx) { return do_test_bio_cipher(EVP_aes_256_cfb(), idx); } static int test_bio_enc_aes_256_ofb(int idx) { return do_test_bio_cipher(EVP_aes_256_ofb(), idx); } # ifndef OPENSSL_NO_CHACHA static int test_bio_enc_chacha20(int idx) { return do_test_bio_cipher(EVP_chacha20(), idx); } # ifndef OPENSSL_NO_POLY1305 static int test_bio_enc_chacha20_poly1305(int idx) { return do_test_bio_cipher(EVP_chacha20_poly1305(), idx); } # endif # endif int setup_tests(void) { ADD_ALL_TESTS(test_bio_enc_aes_128_cbc, 2); ADD_ALL_TESTS(test_bio_enc_aes_128_ctr, 2); ADD_ALL_TESTS(test_bio_enc_aes_256_cfb, 2); ADD_ALL_TESTS(test_bio_enc_aes_256_ofb, 2); # ifndef OPENSSL_NO_CHACHA ADD_ALL_TESTS(test_bio_enc_chacha20, 2); # ifndef OPENSSL_NO_POLY1305 ADD_ALL_TESTS(test_bio_enc_chacha20_poly1305, 2); # endif # endif return 1; }
test
openssl/test/bio_enc_test.c
openssl
#include <string.h> #include <limits.h> #include <openssl/store.h> #include <openssl/ui.h> #include "testutil.h" #ifndef PATH_MAX # if defined(_WIN32) && defined(_MAX_PATH) # define PATH_MAX _MAX_PATH # else # define PATH_MAX 4096 # endif #endif typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_INPUTDIR, OPT_INFILE, OPT_SM2FILE, OPT_DATADIR, OPT_TEST_ENUM } OPTION_CHOICE; static const char *inputdir = NULL; static const char *infile = NULL; static const char *sm2file = NULL; static const char *datadir = NULL; static int test_store_open(void) { int ret = 0; OSSL_STORE_CTX *sctx = NULL; OSSL_STORE_SEARCH *search = NULL; UI_METHOD *ui_method = NULL; char *input = test_mk_file_path(inputdir, infile); ret = TEST_ptr(input) && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing")) && TEST_ptr(ui_method= UI_create_method("DummyUI")) && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method, NULL, NULL, NULL, NULL)) && TEST_false(OSSL_STORE_find(sctx, NULL)) && TEST_true(OSSL_STORE_find(sctx, search)); UI_destroy_method(ui_method); OSSL_STORE_SEARCH_free(search); OSSL_STORE_close(sctx); OPENSSL_free(input); return ret; } static int test_store_search_by_key_fingerprint_fail(void) { int ret; OSSL_STORE_SEARCH *search = NULL; ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint( EVP_sha256(), NULL, 0)); OSSL_STORE_SEARCH_free(search); return ret; } static int get_params(const char *uri, const char *type) { EVP_PKEY *pkey = NULL; OSSL_STORE_CTX *ctx = NULL; OSSL_STORE_INFO *info; int ret = 0; ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (!TEST_ptr(ctx)) goto err; while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL && pkey == NULL) { if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { pkey = OSSL_STORE_INFO_get1_PARAMS(info); } OSSL_STORE_INFO_free(info); info = NULL; } if (pkey != NULL) ret = EVP_PKEY_is_a(pkey, type); EVP_PKEY_free(pkey); err: OSSL_STORE_close(ctx); return ret; } static int test_store_get_params(int idx) { const char *type; const char *urifmt; char uri[PATH_MAX]; switch (idx) { #ifndef OPENSSL_NO_DH case 0: type = "DH"; break; case 1: type = "DHX"; break; #else case 0: case 1: return 1; #endif case 2: #ifndef OPENSSL_NO_DSA type = "DSA"; break; #else return 1; #endif default: TEST_error("Invalid test index"); return 0; } urifmt = "%s/%s-params.pem"; #ifdef __VMS { char datadir_end = datadir[strlen(datadir) - 1]; if (datadir_end == ':' || datadir_end == ']' || datadir_end == '>') urifmt = "%s%s-params.pem"; } #endif if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type))) return 0; TEST_info("Testing uri: %s", uri); if (!TEST_true(get_params(uri, type))) return 0; return 1; } static int test_store_attach_unregistered_scheme(void) { int ret; OSSL_STORE_CTX *store_ctx = NULL; OSSL_PROVIDER *provider = NULL; OSSL_LIB_CTX *libctx = NULL; BIO *bio = NULL; char *input = test_mk_file_path(inputdir, sm2file); ret = TEST_ptr(input) && TEST_ptr(libctx = OSSL_LIB_CTX_new()) && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default")) && TEST_ptr(bio = BIO_new_file(input, "r")) && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL, NULL, NULL, NULL, NULL, NULL)) && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE) && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()), OSSL_STORE_R_UNREGISTERED_SCHEME); BIO_free(bio); OSSL_STORE_close(store_ctx); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); OPENSSL_free(input); return ret; } const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "dir", OPT_INPUTDIR, '/' }, { "in", OPT_INFILE, '<' }, { "sm2", OPT_SM2FILE, '<' }, { "data", OPT_DATADIR, 's' }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_INPUTDIR: inputdir = opt_arg(); break; case OPT_INFILE: infile = opt_arg(); break; case OPT_SM2FILE: sm2file = opt_arg(); break; case OPT_DATADIR: datadir = opt_arg(); break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } if (datadir == NULL) { TEST_error("No data directory specified"); return 0; } if (inputdir == NULL) { TEST_error("No input directory specified"); return 0; } if (infile != NULL) ADD_TEST(test_store_open); ADD_TEST(test_store_search_by_key_fingerprint_fail); ADD_ALL_TESTS(test_store_get_params, 3); if (sm2file != NULL) ADD_TEST(test_store_attach_unregistered_scheme); return 1; }
test
openssl/test/ossl_store_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/evp.h> #include <openssl/bio.h> #include <openssl/rand.h> #include <openssl/comp.h> #include "testutil.h" #include "testutil/output.h" #include "testutil/tu_local.h" #define COMPRESS 1 #define EXPAND 0 #define BUFFER_SIZE 32 * 1024 #define NUM_SIZES 4 static int sizes[NUM_SIZES] = { 64, 512, 2048, 16 * 1024 }; static unsigned char *original = NULL; static unsigned char *result = NULL; static int do_bio_comp_test(const BIO_METHOD *meth, size_t size) { BIO *bcomp = NULL; BIO *bmem = NULL; BIO *bexp = NULL; int osize; int rsize; int ret = 0; if (!TEST_ptr(meth)) goto err; if (!TEST_ptr(bcomp = BIO_new(meth))) goto err; if (!TEST_ptr(bmem = BIO_new(BIO_s_mem()))) goto err; BIO_push(bcomp, bmem); osize = BIO_write(bcomp, original, size); if (!TEST_int_eq(osize, size) || !TEST_true(BIO_flush(bcomp))) goto err; BIO_free(bcomp); bcomp = NULL; if (!TEST_ptr(bexp = BIO_new(meth))) goto err; BIO_push(bexp, bmem); rsize = BIO_read(bexp, result, size); if (!TEST_int_eq(size, rsize) || !TEST_mem_eq(original, osize, result, rsize)) goto err; ret = 1; err: BIO_free(bexp); BIO_free(bcomp); BIO_free(bmem); return ret; } static int do_bio_comp(const BIO_METHOD *meth, int n) { int i; int success = 0; int size = sizes[n % 4]; int type = n / 4; if (!TEST_ptr(original = OPENSSL_malloc(BUFFER_SIZE)) || !TEST_ptr(result = OPENSSL_malloc(BUFFER_SIZE))) goto err; switch (type) { case 0: TEST_info("zeros of size %d\n", size); memset(original, 0, BUFFER_SIZE); break; case 1: TEST_info("ones of size %d\n", size); memset(original, 1, BUFFER_SIZE); break; case 2: TEST_info("sequential of size %d\n", size); for (i = 0; i < BUFFER_SIZE; i++) original[i] = i & 0xFF; break; case 3: TEST_info("random of size %d\n", size); if (!TEST_int_gt(RAND_bytes(original, BUFFER_SIZE), 0)) goto err; break; default: goto err; } if (!TEST_true(do_bio_comp_test(meth, size))) goto err; success = 1; err: OPENSSL_free(original); OPENSSL_free(result); return success; } #ifndef OPENSSL_NO_ZSTD static int test_zstd(int n) { return do_bio_comp(BIO_f_zstd(), n); } #endif #ifndef OPENSSL_NO_BROTLI static int test_brotli(int n) { return do_bio_comp(BIO_f_brotli(), n); } #endif #ifndef OPENSSL_NO_ZLIB static int test_zlib(int n) { return do_bio_comp(BIO_f_zlib(), n); } #endif int setup_tests(void) { #ifndef OPENSSL_NO_ZLIB ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4); #endif #ifndef OPENSSL_NO_BROTLI ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4); #endif #ifndef OPENSSL_NO_ZSTD ADD_ALL_TESTS(test_zstd, NUM_SIZES * 4); #endif return 1; }
test
openssl/test/bio_comp_test.c
openssl
#include "testutil.h" #include <openssl/ssl.h> typedef struct { int proto; int min_version; int max_version; int min_ok; int max_ok; int expected_min; int expected_max; } version_test; #define PROTO_TLS 0 #define PROTO_DTLS 1 #define PROTO_QUIC 2 static const version_test version_testdata[] = { {PROTO_TLS, 0, 0, 1, 1, 0, 0}, {PROTO_TLS, SSL3_VERSION, TLS1_3_VERSION, 1, 1, SSL3_VERSION, TLS1_3_VERSION}, {PROTO_TLS, TLS1_VERSION, TLS1_3_VERSION, 1, 1, TLS1_VERSION, TLS1_3_VERSION}, {PROTO_TLS, TLS1_VERSION, TLS1_2_VERSION, 1, 1, TLS1_VERSION, TLS1_2_VERSION}, {PROTO_TLS, TLS1_2_VERSION, TLS1_2_VERSION, 1, 1, TLS1_2_VERSION, TLS1_2_VERSION}, {PROTO_TLS, TLS1_2_VERSION, TLS1_1_VERSION, 1, 1, TLS1_2_VERSION, TLS1_1_VERSION}, {PROTO_TLS, SSL3_VERSION - 1, TLS1_3_VERSION, 0, 1, 0, TLS1_3_VERSION}, {PROTO_TLS, SSL3_VERSION, TLS1_3_VERSION + 1, 1, 0, SSL3_VERSION, 0}, #ifndef OPENSSL_NO_DTLS {PROTO_TLS, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, 0, 0}, #endif {PROTO_TLS, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, {PROTO_TLS, 7, 42, 0, 0, 0, 0}, {PROTO_DTLS, 0, 0, 1, 1, 0, 0}, {PROTO_DTLS, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, DTLS1_VERSION, DTLS1_2_VERSION}, #ifndef OPENSSL_NO_DTLS1_2 {PROTO_DTLS, DTLS1_2_VERSION, DTLS1_2_VERSION, 1, 1, DTLS1_2_VERSION, DTLS1_2_VERSION}, #endif #ifndef OPENSSL_NO_DTLS1 {PROTO_DTLS, DTLS1_VERSION, DTLS1_VERSION, 1, 1, DTLS1_VERSION, DTLS1_VERSION}, #endif #if !defined(OPENSSL_NO_DTLS1) && !defined(OPENSSL_NO_DTLS1_2) {PROTO_DTLS, DTLS1_2_VERSION, DTLS1_VERSION, 1, 1, DTLS1_2_VERSION, DTLS1_VERSION}, #endif {PROTO_DTLS, DTLS1_VERSION + 1, DTLS1_2_VERSION, 0, 1, 0, DTLS1_2_VERSION}, {PROTO_DTLS, DTLS1_VERSION, DTLS1_2_VERSION - 1, 1, 0, DTLS1_VERSION, 0}, {PROTO_DTLS, TLS1_VERSION, TLS1_3_VERSION, 1, 1, 0, 0}, {PROTO_DTLS, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, {PROTO_QUIC, 0, 0, 1, 1, 0, 0}, {PROTO_QUIC, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, {PROTO_QUIC, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION + 1, 0, 0, 0, 0}, {PROTO_QUIC, TLS1_VERSION, TLS1_3_VERSION, 1, 1, 0, 0}, #ifndef OPENSSL_NO_DTLS {PROTO_QUIC, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, 0, 0}, #endif }; static int test_set_min_max_version(int idx_tst) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; int testresult = 0; version_test t = version_testdata[idx_tst]; const SSL_METHOD *meth = NULL; switch (t.proto) { case PROTO_TLS: meth = TLS_client_method(); break; #ifndef OPENSSL_NO_DTLS case PROTO_DTLS: meth = DTLS_client_method(); break; #endif #ifndef OPENSSL_NO_QUIC case PROTO_QUIC: meth = OSSL_QUIC_client_method(); break; #endif } if (meth == NULL) return TEST_skip("Protocol not supported"); ctx = SSL_CTX_new(meth); if (ctx == NULL) goto end; ssl = SSL_new(ctx); if (ssl == NULL) goto end; if (!TEST_int_eq(SSL_CTX_set_min_proto_version(ctx, t.min_version), t.min_ok)) goto end; if (!TEST_int_eq(SSL_CTX_set_max_proto_version(ctx, t.max_version), t.max_ok)) goto end; if (!TEST_int_eq(SSL_CTX_get_min_proto_version(ctx), t.expected_min)) goto end; if (!TEST_int_eq(SSL_CTX_get_max_proto_version(ctx), t.expected_max)) goto end; if (!TEST_int_eq(SSL_set_min_proto_version(ssl, t.min_version), t.min_ok)) goto end; if (!TEST_int_eq(SSL_set_max_proto_version(ssl, t.max_version), t.max_ok)) goto end; if (!TEST_int_eq(SSL_get_min_proto_version(ssl), t.expected_min)) goto end; if (!TEST_int_eq(SSL_get_max_proto_version(ssl), t.expected_max)) goto end; testresult = 1; end: SSL_free(ssl); SSL_CTX_free(ctx); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_set_min_max_version, sizeof(version_testdata) / sizeof(version_test)); return 1; }
test
openssl/test/ssl_ctx_test.c
openssl
#include "internal/packet.h" #include "internal/quic_txp.h" #include "internal/quic_statm.h" #include "internal/quic_demux.h" #include "internal/quic_record_rx.h" #include "testutil.h" #include "quic_record_test_util.h" static const QUIC_CONN_ID scid_1 = { 1, { 0x5f } }; static const QUIC_CONN_ID dcid_1 = { 8, { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8 } }; static const QUIC_CONN_ID cid_1 = { 8, { 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8 } }; static const unsigned char reset_token_1[16] = { 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x12, }; static const unsigned char secret_1[32] = { 0x01 }; static OSSL_TIME fake_now(void *arg) { return ossl_time_now(); } struct helper { OSSL_QUIC_TX_PACKETISER *txp; OSSL_QUIC_TX_PACKETISER_ARGS args; OSSL_QTX_ARGS qtx_args; BIO *bio1, *bio2; QUIC_TXFC conn_txfc; QUIC_RXFC conn_rxfc, stream_rxfc; QUIC_RXFC max_streams_bidi_rxfc, max_streams_uni_rxfc; OSSL_STATM statm; OSSL_CC_DATA *cc_data; const OSSL_CC_METHOD *cc_method; QUIC_STREAM_MAP qsm; char have_statm, have_qsm; QUIC_DEMUX *demux; OSSL_QRX *qrx; OSSL_QRX_ARGS qrx_args; OSSL_QRX_PKT *qrx_pkt; PACKET pkt; uint64_t frame_type; union { uint64_t max_data; OSSL_QUIC_FRAME_NEW_CONN_ID new_conn_id; OSSL_QUIC_FRAME_ACK ack; struct { const unsigned char *token; size_t token_len; } new_token; OSSL_QUIC_FRAME_CRYPTO crypto; OSSL_QUIC_FRAME_STREAM stream; OSSL_QUIC_FRAME_STOP_SENDING stop_sending; OSSL_QUIC_FRAME_RESET_STREAM reset_stream; OSSL_QUIC_FRAME_CONN_CLOSE conn_close; } frame; OSSL_QUIC_ACK_RANGE ack_ranges[16]; }; static void helper_cleanup(struct helper *h) { size_t i; uint32_t pn_space; ossl_qrx_pkt_release(h->qrx_pkt); h->qrx_pkt = NULL; for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) ossl_ackm_on_pkt_space_discarded(h->args.ackm, pn_space); ossl_quic_tx_packetiser_free(h->txp); ossl_qtx_free(h->args.qtx); ossl_quic_txpim_free(h->args.txpim); ossl_quic_cfq_free(h->args.cfq); if (h->cc_data != NULL) h->cc_method->free(h->cc_data); if (h->have_statm) ossl_statm_destroy(&h->statm); if (h->have_qsm) ossl_quic_stream_map_cleanup(&h->qsm); for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) ossl_quic_sstream_free(h->args.crypto[i]); ossl_ackm_free(h->args.ackm); ossl_qrx_free(h->qrx); ossl_quic_demux_free(h->demux); BIO_free(h->bio1); BIO_free(h->bio2); } static void demux_default_handler(QUIC_URXE *e, void *arg, const QUIC_CONN_ID *dcid) { struct helper *h = arg; if (dcid == NULL || !ossl_quic_conn_id_eq(dcid, &dcid_1)) return; ossl_qrx_inject_urxe(h->qrx, e); } static int helper_init(struct helper *h) { int rc = 0; size_t i; memset(h, 0, sizeof(*h)); if (!TEST_true(BIO_new_bio_dgram_pair(&h->bio1, 0, &h->bio2, 0))) goto err; h->qtx_args.bio = h->bio1; h->qtx_args.mdpl = 1200; if (!TEST_ptr(h->args.qtx = ossl_qtx_new(&h->qtx_args))) goto err; if (!TEST_ptr(h->args.txpim = ossl_quic_txpim_new())) goto err; if (!TEST_ptr(h->args.cfq = ossl_quic_cfq_new())) goto err; if (!TEST_true(ossl_quic_txfc_init(&h->conn_txfc, NULL))) goto err; if (!TEST_true(ossl_quic_rxfc_init(&h->conn_rxfc, NULL, 2 * 1024 * 1024, 10 * 1024 * 1024, fake_now, NULL))) goto err; if (!TEST_true(ossl_quic_rxfc_init(&h->stream_rxfc, &h->conn_rxfc, 1 * 1024 * 1024, 5 * 1024 * 1024, fake_now, NULL))) goto err; if (!TEST_true(ossl_quic_rxfc_init(&h->max_streams_bidi_rxfc, NULL, 100, 100, fake_now, NULL))) goto err; if (!TEST_true(ossl_quic_rxfc_init(&h->max_streams_uni_rxfc, NULL, 100, 100, fake_now, NULL))) if (!TEST_true(ossl_statm_init(&h->statm))) goto err; h->have_statm = 1; h->cc_method = &ossl_cc_dummy_method; if (!TEST_ptr(h->cc_data = h->cc_method->new(fake_now, NULL))) goto err; if (!TEST_ptr(h->args.ackm = ossl_ackm_new(fake_now, NULL, &h->statm, h->cc_method, h->cc_data))) goto err; if (!TEST_true(ossl_quic_stream_map_init(&h->qsm, NULL, NULL, &h->max_streams_bidi_rxfc, &h->max_streams_uni_rxfc, 0))) goto err; h->have_qsm = 1; for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) if (!TEST_ptr(h->args.crypto[i] = ossl_quic_sstream_new(4096))) goto err; h->args.cur_scid = scid_1; h->args.cur_dcid = dcid_1; h->args.qsm = &h->qsm; h->args.conn_txfc = &h->conn_txfc; h->args.conn_rxfc = &h->conn_rxfc; h->args.max_streams_bidi_rxfc = &h->max_streams_bidi_rxfc; h->args.max_streams_uni_rxfc = &h->max_streams_uni_rxfc; h->args.cc_method = h->cc_method; h->args.cc_data = h->cc_data; h->args.now = fake_now; if (!TEST_ptr(h->txp = ossl_quic_tx_packetiser_new(&h->args))) goto err; if (!TEST_ptr(h->demux = ossl_quic_demux_new(h->bio2, 8, fake_now, NULL))) goto err; ossl_quic_demux_set_default_handler(h->demux, demux_default_handler, h); h->qrx_args.demux = h->demux; h->qrx_args.short_conn_id_len = 8; h->qrx_args.max_deferred = 32; if (!TEST_ptr(h->qrx = ossl_qrx_new(&h->qrx_args))) goto err; ossl_qrx_allow_1rtt_processing(h->qrx); rc = 1; err: if (!rc) helper_cleanup(h); return rc; } #define OPK_END 0 #define OPK_TXP_GENERATE 1 #define OPK_TXP_GENERATE_NONE 2 #define OPK_RX_PKT 3 #define OPK_RX_PKT_NONE 4 #define OPK_EXPECT_DGRAM_LEN 5 #define OPK_EXPECT_FRAME 6 #define OPK_EXPECT_INITIAL_TOKEN 7 #define OPK_EXPECT_HDR 8 #define OPK_CHECK 9 #define OPK_NEXT_FRAME 10 #define OPK_EXPECT_NO_FRAME 11 #define OPK_PROVIDE_SECRET 12 #define OPK_DISCARD_EL 13 #define OPK_CRYPTO_SEND 14 #define OPK_STREAM_NEW 15 #define OPK_STREAM_SEND 16 #define OPK_STREAM_FIN 17 #define OPK_STOP_SENDING 18 #define OPK_RESET_STREAM 19 #define OPK_CONN_TXFC_BUMP 20 #define OPK_STREAM_TXFC_BUMP 21 #define OPK_HANDSHAKE_COMPLETE 22 #define OPK_NOP 23 struct script_op { uint32_t opcode; uint64_t arg0, arg1; const void *buf; size_t buf_len; int (*check_func)(struct helper *h); }; #define OP_END \ { OPK_END } #define OP_TXP_GENERATE() \ { OPK_TXP_GENERATE }, #define OP_TXP_GENERATE_NONE() \ { OPK_TXP_GENERATE_NONE }, #define OP_RX_PKT() \ { OPK_RX_PKT }, #define OP_RX_PKT_NONE() \ { OPK_RX_PKT_NONE }, #define OP_EXPECT_DGRAM_LEN(lo, hi) \ { OPK_EXPECT_DGRAM_LEN, (lo), (hi) }, #define OP_EXPECT_FRAME(frame_type) \ { OPK_EXPECT_FRAME, (frame_type) }, #define OP_EXPECT_INITIAL_TOKEN(buf) \ { OPK_EXPECT_INITIAL_TOKEN, sizeof(buf), 0, buf }, #define OP_EXPECT_HDR(hdr) \ { OPK_EXPECT_HDR, 0, 0, &(hdr) }, #define OP_CHECK(func) \ { OPK_CHECK, 0, 0, NULL, 0, (func) }, #define OP_NEXT_FRAME() \ { OPK_NEXT_FRAME }, #define OP_EXPECT_NO_FRAME() \ { OPK_EXPECT_NO_FRAME }, #define OP_PROVIDE_SECRET(el, suite, secret) \ { OPK_PROVIDE_SECRET, (el), (suite), (secret), sizeof(secret) }, #define OP_DISCARD_EL(el) \ { OPK_DISCARD_EL, (el) }, #define OP_CRYPTO_SEND(pn_space, buf) \ { OPK_CRYPTO_SEND, (pn_space), 0, (buf), sizeof(buf) }, #define OP_STREAM_NEW(id) \ { OPK_STREAM_NEW, (id) }, #define OP_STREAM_SEND(id, buf) \ { OPK_STREAM_SEND, (id), 0, (buf), sizeof(buf) }, #define OP_STREAM_FIN(id) \ { OPK_STREAM_FIN, (id) }, #define OP_STOP_SENDING(id, aec) \ { OPK_STOP_SENDING, (id), (aec) }, #define OP_RESET_STREAM(id, aec) \ { OPK_RESET_STREAM, (id), (aec) }, #define OP_CONN_TXFC_BUMP(cwm) \ { OPK_CONN_TXFC_BUMP, (cwm) }, #define OP_STREAM_TXFC_BUMP(id, cwm) \ { OPK_STREAM_TXFC_BUMP, (cwm), (id) }, #define OP_HANDSHAKE_COMPLETE() \ { OPK_HANDSHAKE_COMPLETE }, #define OP_NOP() \ { OPK_NOP }, static int schedule_handshake_done(struct helper *h) { ossl_quic_tx_packetiser_schedule_handshake_done(h->txp); return 1; } static int schedule_ack_eliciting_app(struct helper *h) { ossl_quic_tx_packetiser_schedule_ack_eliciting(h->txp, QUIC_PN_SPACE_APP); return 1; } static const struct script_op script_1[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_handshake_done) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 32) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const struct script_op script_2[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_ack_eliciting_app) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 32) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int schedule_max_data(struct helper *h) { uint64_t cwm; cwm = ossl_quic_rxfc_get_cwm(&h->stream_rxfc); if (!TEST_true(ossl_quic_rxfc_on_rx_stream_frame(&h->stream_rxfc, cwm, 0)) || !TEST_true(ossl_quic_rxfc_on_retire(&h->stream_rxfc, cwm, ossl_ticks2time(OSSL_TIME_MS)))) return 0; return 1; } static const struct script_op script_3[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_max_data) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 40) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_MAX_DATA) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static void free_buf_mem(unsigned char *buf, size_t buf_len, void *arg) { BUF_MEM_free((BUF_MEM *)arg); } static int schedule_cfq_new_conn_id(struct helper *h) { int rc = 0; QUIC_CFQ_ITEM *cfq_item; WPACKET wpkt; BUF_MEM *buf_mem = NULL; size_t l = 0; OSSL_QUIC_FRAME_NEW_CONN_ID ncid = {0}; ncid.seq_num = 2345; ncid.retire_prior_to = 1234; ncid.conn_id = cid_1; memcpy(ncid.stateless_reset.token, reset_token_1, sizeof(reset_token_1)); if (!TEST_ptr(buf_mem = BUF_MEM_new())) goto err; if (!TEST_true(WPACKET_init(&wpkt, buf_mem))) goto err; if (!TEST_true(ossl_quic_wire_encode_frame_new_conn_id(&wpkt, &ncid))) { WPACKET_cleanup(&wpkt); goto err; } WPACKET_finish(&wpkt); if (!TEST_true(WPACKET_get_total_written(&wpkt, &l))) goto err; if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(h->args.cfq, 1, QUIC_PN_SPACE_APP, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, 0, (unsigned char *)buf_mem->data, l, free_buf_mem, buf_mem))) goto err; rc = 1; err: if (!rc) BUF_MEM_free(buf_mem); return rc; } static int check_cfq_new_conn_id(struct helper *h) { if (!TEST_uint64_t_eq(h->frame.new_conn_id.seq_num, 2345) || !TEST_uint64_t_eq(h->frame.new_conn_id.retire_prior_to, 1234) || !TEST_mem_eq(&h->frame.new_conn_id.conn_id, sizeof(cid_1), &cid_1, sizeof(cid_1)) || !TEST_mem_eq(&h->frame.new_conn_id.stateless_reset.token, sizeof(reset_token_1), reset_token_1, sizeof(reset_token_1))) return 0; return 1; } static const struct script_op script_4[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_cfq_new_conn_id) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 128) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID) OP_CHECK(check_cfq_new_conn_id) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char token_1[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 }; static int schedule_cfq_new_token(struct helper *h) { int rc = 0; QUIC_CFQ_ITEM *cfq_item; WPACKET wpkt; BUF_MEM *buf_mem = NULL; size_t l = 0; if (!TEST_ptr(buf_mem = BUF_MEM_new())) goto err; if (!TEST_true(WPACKET_init(&wpkt, buf_mem))) goto err; if (!TEST_true(ossl_quic_wire_encode_frame_new_token(&wpkt, token_1, sizeof(token_1)))) { WPACKET_cleanup(&wpkt); goto err; } WPACKET_finish(&wpkt); if (!TEST_true(WPACKET_get_total_written(&wpkt, &l))) goto err; if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(h->args.cfq, 1, QUIC_PN_SPACE_APP, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN, 0, (unsigned char *)buf_mem->data, l, free_buf_mem, buf_mem))) goto err; rc = 1; err: if (!rc) BUF_MEM_free(buf_mem); return rc; } static int check_cfq_new_token(struct helper *h) { if (!TEST_mem_eq(h->frame.new_token.token, h->frame.new_token.token_len, token_1, sizeof(token_1))) return 0; return 1; } static const struct script_op script_5[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_cfq_new_token) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_TOKEN) OP_CHECK(check_cfq_new_token) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int schedule_ack(struct helper *h) { size_t i; OSSL_ACKM_RX_PKT rx_pkt = {0}; for (i = 0; i < 5; ++i) { rx_pkt.pkt_num = i; rx_pkt.time = fake_now(NULL); rx_pkt.pkt_space = QUIC_PN_SPACE_APP; rx_pkt.is_ack_eliciting = 1; if (!TEST_true(ossl_ackm_on_rx_packet(h->args.ackm, &rx_pkt))) return 0; } return 1; } static const struct script_op script_6[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_ack) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const struct script_op script_7[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(schedule_cfq_new_token) OP_CHECK(schedule_ack) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_NEW_TOKEN) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char crypto_1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; static const struct script_op script_8[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CRYPTO_SEND(QUIC_PN_SPACE_APP, crypto_1) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CRYPTO) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char stream_9[] = { 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x7a, 0x7b }; static int check_stream_9(struct helper *h) { if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len, stream_9, sizeof(stream_9))) return 0; return 1; } static const struct script_op script_9[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_HANDSHAKE_COMPLETE() OP_TXP_GENERATE_NONE() OP_STREAM_NEW(42) OP_STREAM_SEND(42, stream_9) OP_TXP_GENERATE_NONE() OP_CONN_TXFC_BUMP(1000) OP_STREAM_TXFC_BUMP(42, 1000) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM) OP_CHECK(check_stream_9) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char stream_10a[1300] = { 0x40, 0x0d, 0xb6, 0x0d, 0x25, 0x5f, 0xdd, 0xb9, 0x05, 0x79, 0xa8, 0xe3, 0x79, 0x32, 0xb2, 0xa7, 0x30, 0x6d, 0x29, 0xf6, 0xba, 0x50, 0xbe, 0x83, 0xcb, 0x56, 0xec, 0xd6, 0xc7, 0x80, 0x84, 0xa2, 0x2f, 0xeb, 0xc4, 0x37, 0x40, 0x44, 0xef, 0xd8, 0x78, 0xbb, 0x92, 0x80, 0x22, 0x33, 0xc0, 0xce, 0x33, 0x5b, 0x75, 0x8c, 0xa5, 0x1a, 0x7a, 0x2a, 0xa9, 0x88, 0xaf, 0xf6, 0x3a, 0xe2, 0x5e, 0x60, 0x52, 0x6d, 0xef, 0x7f, 0x2a, 0x9a, 0xaa, 0x17, 0x0e, 0x12, 0x51, 0x82, 0x08, 0x2f, 0x0f, 0x5b, 0xff, 0xf5, 0x7c, 0x7c, 0x89, 0x04, 0xfb, 0xa7, 0x80, 0x4e, 0xda, 0x12, 0x89, 0x01, 0x4a, 0x81, 0x84, 0x78, 0x15, 0xa9, 0x12, 0x28, 0x69, 0x4a, 0x25, 0xe5, 0x8b, 0x69, 0xc2, 0x9f, 0xb6, 0x59, 0x49, 0xe3, 0x53, 0x90, 0xef, 0xc9, 0xb8, 0x40, 0xdd, 0x62, 0x5f, 0x99, 0x68, 0xd2, 0x0a, 0x77, 0xde, 0xf3, 0x11, 0x39, 0x7f, 0x93, 0x8b, 0x81, 0x69, 0x36, 0xa7, 0x76, 0xa4, 0x10, 0x56, 0x51, 0xe5, 0x45, 0x3a, 0x42, 0x49, 0x6c, 0xc6, 0xa0, 0xb4, 0x13, 0x46, 0x59, 0x0e, 0x48, 0x60, 0xc9, 0xff, 0x70, 0x10, 0x8d, 0x6a, 0xf9, 0x5b, 0x94, 0xc2, 0x9e, 0x49, 0x19, 0x56, 0xf2, 0xc1, 0xff, 0x08, 0x3f, 0x9e, 0x26, 0x8e, 0x99, 0x71, 0xc4, 0x25, 0xb1, 0x4e, 0xcc, 0x7e, 0x5f, 0xf0, 0x4e, 0x25, 0xa2, 0x2f, 0x3f, 0x68, 0xaa, 0xcf, 0xbd, 0x19, 0x19, 0x1c, 0x92, 0xa0, 0xb6, 0xb8, 0x32, 0xb1, 0x0b, 0x91, 0x05, 0xa9, 0xf8, 0x1a, 0x4b, 0x74, 0x09, 0xf9, 0x57, 0xd0, 0x1c, 0x38, 0x10, 0x05, 0x54, 0xd8, 0x4e, 0x12, 0x67, 0xcc, 0x43, 0xa3, 0x81, 0xa9, 0x3a, 0x12, 0x57, 0xe7, 0x4b, 0x0e, 0xe5, 0x51, 0xf9, 0x5f, 0xd4, 0x46, 0x73, 0xa2, 0x78, 0xb7, 0x00, 0x24, 0x69, 0x35, 0x10, 0x1e, 0xb8, 0xa7, 0x4a, 0x9b, 0xbc, 0xfc, 0x04, 0x6f, 0x1a, 0xb0, 0x4f, 0x12, 0xc9, 0x2b, 0x3b, 0x94, 0x85, 0x1b, 0x8e, 0xba, 0xac, 0xfd, 0x10, 0x22, 0x68, 0x90, 0x17, 0x13, 0x44, 0x18, 0x2f, 0x33, 0x37, 0x1a, 0x89, 0xc0, 0x2c, 0x14, 0x59, 0xb2, 0xaf, 0xc0, 0x6b, 0xdc, 0x28, 0xe1, 0xe9, 0xc1, 0x0c, 0xb4, 0x80, 0x90, 0xb9, 0x1f, 0x45, 0xb4, 0x63, 0x9a, 0x0e, 0xfa, 0x33, 0xf5, 0x75, 0x3a, 0x4f, 0xc3, 0x8c, 0x70, 0xdb, 0xd7, 0xbf, 0xf6, 0xb8, 0x7f, 0xcc, 0xe5, 0x85, 0xb6, 0xae, 0x25, 0x60, 0x18, 0x5b, 0xf1, 0x51, 0x1a, 0x85, 0xc1, 0x7f, 0xf3, 0xbe, 0xb6, 0x82, 0x38, 0xe3, 0xd2, 0xff, 0x8a, 0xc4, 0xdb, 0x08, 0xe6, 0x96, 0xd5, 0x3d, 0x1f, 0xc5, 0x12, 0x35, 0x45, 0x75, 0x5d, 0x17, 0x4e, 0xe1, 0xb8, 0xc9, 0xf0, 0x45, 0x95, 0x0b, 0x03, 0xcb, 0x85, 0x47, 0xaf, 0xc7, 0x88, 0xb6, 0xc1, 0x2c, 0xb8, 0x9b, 0xe6, 0x8b, 0x51, 0xd5, 0x2e, 0x71, 0xba, 0xc9, 0xa9, 0x37, 0x5e, 0x1c, 0x2c, 0x03, 0xf0, 0xc7, 0xc1, 0xd3, 0x72, 0xaa, 0x4d, 0x19, 0xd6, 0x51, 0x64, 0x12, 0xeb, 0x39, 0xeb, 0x45, 0xe9, 0xb4, 0x84, 0x08, 0xb6, 0x6c, 0xc7, 0x3e, 0xf0, 0x88, 0x64, 0xc2, 0x91, 0xb7, 0xa5, 0x86, 0x66, 0x83, 0xd5, 0xd3, 0x41, 0x24, 0xb2, 0x1c, 0x9a, 0x18, 0x10, 0x0e, 0xa5, 0xc9, 0xef, 0xcd, 0x06, 0xce, 0xa8, 0xaf, 0x22, 0x52, 0x25, 0x0b, 0x99, 0x3d, 0xe9, 0x26, 0xda, 0xa9, 0x47, 0xd1, 0x4b, 0xa6, 0x4c, 0xfc, 0x80, 0xaf, 0x6a, 0x59, 0x4b, 0x35, 0xa4, 0x93, 0x39, 0x5b, 0xfa, 0x91, 0x9d, 0xdf, 0x9d, 0x3c, 0xfb, 0x53, 0xca, 0x18, 0x19, 0xe4, 0xda, 0x95, 0x47, 0x5a, 0x37, 0x59, 0xd7, 0xd2, 0xe4, 0x75, 0x45, 0x0d, 0x03, 0x7f, 0xa0, 0xa9, 0xa0, 0x71, 0x06, 0xb1, 0x9d, 0x46, 0xbd, 0xcf, 0x4a, 0x8b, 0x73, 0xc1, 0x45, 0x5c, 0x00, 0x61, 0xfd, 0xd1, 0xa4, 0xa2, 0x3e, 0xaa, 0xbe, 0x72, 0xf1, 0x7a, 0x1a, 0x76, 0x88, 0x5c, 0x9e, 0x74, 0x6d, 0x2a, 0x34, 0xfc, 0xf7, 0x41, 0x28, 0xe8, 0xa3, 0x43, 0x4d, 0x43, 0x1d, 0x6c, 0x36, 0xb1, 0x45, 0x71, 0x5a, 0x3c, 0xd3, 0x28, 0x44, 0xe4, 0x9b, 0xbf, 0x54, 0x16, 0xc3, 0x99, 0x6c, 0x42, 0xd8, 0x20, 0xb6, 0x20, 0x5f, 0x6e, 0xbc, 0xba, 0x88, 0x5e, 0x2f, 0xa5, 0xd1, 0x82, 0x5c, 0x92, 0xd0, 0x79, 0xfd, 0xcc, 0x61, 0x49, 0xd0, 0x73, 0x92, 0xe6, 0x98, 0xe3, 0x80, 0x7a, 0xf9, 0x56, 0x63, 0x33, 0x19, 0xda, 0x54, 0x13, 0xf0, 0x21, 0xa8, 0x15, 0xf6, 0xb7, 0x43, 0x7c, 0x1c, 0x1e, 0xb1, 0x89, 0x8d, 0xce, 0x20, 0x54, 0x81, 0x80, 0xb5, 0x8f, 0x9b, 0xb1, 0x09, 0x92, 0xdb, 0x25, 0x6f, 0x30, 0x29, 0x08, 0x1a, 0x05, 0x08, 0xf4, 0x83, 0x8b, 0x1e, 0x2d, 0xfd, 0xe4, 0xb2, 0x76, 0xc8, 0x4d, 0xf3, 0xa6, 0x49, 0x5f, 0x2c, 0x99, 0x78, 0xbd, 0x07, 0xef, 0xc8, 0xd9, 0xb5, 0x70, 0x3b, 0x0a, 0xcb, 0xbd, 0xa0, 0xea, 0x15, 0xfb, 0xd1, 0x6e, 0x61, 0x83, 0xcb, 0x90, 0xd0, 0xa3, 0x81, 0x28, 0xdc, 0xd5, 0x84, 0xae, 0x55, 0x28, 0x13, 0x9e, 0xc6, 0xd8, 0xf4, 0x67, 0xd6, 0x0d, 0xd4, 0x69, 0xac, 0xf6, 0x35, 0x95, 0x99, 0x44, 0x26, 0x72, 0x36, 0x55, 0xf9, 0x42, 0xa6, 0x1b, 0x00, 0x93, 0x00, 0x19, 0x2f, 0x70, 0xd3, 0x16, 0x66, 0x4e, 0x80, 0xbb, 0xb6, 0x84, 0xa1, 0x2c, 0x09, 0xfb, 0x41, 0xdf, 0x63, 0xde, 0x62, 0x3e, 0xd0, 0xa8, 0xd8, 0x0c, 0x03, 0x06, 0xa9, 0x82, 0x17, 0x9c, 0xd2, 0xa9, 0xd5, 0x6f, 0xcc, 0xc0, 0xf2, 0x5d, 0xb1, 0xba, 0xf8, 0x2e, 0x37, 0x8b, 0xe6, 0x5d, 0x9f, 0x1b, 0xfb, 0x53, 0x0a, 0x96, 0xbe, 0x69, 0x31, 0x19, 0x8f, 0x44, 0x1b, 0xc2, 0x42, 0x7e, 0x65, 0x12, 0x1d, 0x52, 0x1e, 0xe2, 0xc0, 0x86, 0x70, 0x88, 0xe5, 0xf6, 0x87, 0x5d, 0x03, 0x4b, 0x12, 0x3c, 0x2d, 0xaf, 0x09, 0xf5, 0x4f, 0x82, 0x2e, 0x2e, 0xbe, 0x07, 0xe8, 0x8d, 0x57, 0x6e, 0xc0, 0xeb, 0xf9, 0x37, 0xac, 0x89, 0x01, 0xb7, 0xc6, 0x52, 0x1c, 0x86, 0xe5, 0xbc, 0x1f, 0xbd, 0xde, 0xa2, 0x42, 0xb6, 0x73, 0x85, 0x6f, 0x06, 0x36, 0x56, 0x40, 0x2b, 0xea, 0x16, 0x8c, 0xf4, 0x7b, 0x65, 0x6a, 0xca, 0x3c, 0x56, 0x68, 0x01, 0xe3, 0x9c, 0xbb, 0xb9, 0x45, 0x54, 0xcd, 0x13, 0x74, 0xad, 0x80, 0x40, 0xbc, 0xd0, 0x74, 0xb4, 0x31, 0xe4, 0xca, 0xd5, 0xf8, 0x4f, 0x08, 0x5b, 0xc4, 0x15, 0x1a, 0x51, 0x3b, 0xc6, 0x40, 0xc8, 0xea, 0x76, 0x30, 0x95, 0xb7, 0x76, 0xa4, 0xda, 0x20, 0xdb, 0x75, 0x1c, 0xf4, 0x87, 0x24, 0x29, 0x54, 0xc6, 0x59, 0x0c, 0xf0, 0xed, 0xf5, 0x3d, 0xce, 0x95, 0x23, 0x30, 0x49, 0x91, 0xa7, 0x7b, 0x22, 0xb5, 0xd7, 0x71, 0xb0, 0x60, 0xe1, 0xf0, 0x84, 0x74, 0x0e, 0x2f, 0xa8, 0x79, 0x35, 0xb9, 0x03, 0xb5, 0x2c, 0xdc, 0x60, 0x48, 0x12, 0xd9, 0x14, 0x5a, 0x58, 0x5d, 0x95, 0xc6, 0x47, 0xfd, 0xaf, 0x09, 0xc2, 0x67, 0xa5, 0x09, 0xae, 0xff, 0x4b, 0xd5, 0x6c, 0x2f, 0x1d, 0x33, 0x31, 0xcb, 0xdb, 0xcf, 0xf5, 0xf6, 0xbc, 0x90, 0xb2, 0x15, 0xd4, 0x34, 0xeb, 0xde, 0x0e, 0x8f, 0x3d, 0xea, 0xa4, 0x9b, 0x29, 0x8a, 0xf9, 0x4a, 0xac, 0x38, 0x1e, 0x46, 0xb2, 0x2d, 0xa2, 0x61, 0xc5, 0x99, 0x5e, 0x85, 0x36, 0x85, 0xb0, 0xb1, 0x6b, 0xc4, 0x06, 0x68, 0xc7, 0x9b, 0x54, 0xb9, 0xc8, 0x9d, 0xf3, 0x1a, 0xe0, 0x67, 0x0e, 0x4d, 0x5c, 0x13, 0x54, 0xa4, 0x62, 0x62, 0x6f, 0xae, 0x0e, 0x86, 0xa2, 0xe0, 0x31, 0xc7, 0x72, 0xa1, 0xbb, 0x87, 0x3e, 0x61, 0x96, 0xb7, 0x53, 0xf9, 0x34, 0xcb, 0xfd, 0x6c, 0x67, 0x25, 0x73, 0x61, 0x75, 0x4f, 0xab, 0x37, 0x08, 0xef, 0x35, 0x5a, 0x03, 0xe5, 0x08, 0x43, 0xec, 0xdc, 0xb5, 0x2c, 0x1f, 0xe6, 0xeb, 0xc6, 0x06, 0x0b, 0xed, 0xad, 0x74, 0xf4, 0x55, 0xef, 0xe0, 0x2e, 0x83, 0x00, 0xdb, 0x32, 0xde, 0xe9, 0xe4, 0x2f, 0xf5, 0x20, 0x6d, 0x72, 0x47, 0xf4, 0x68, 0xa6, 0x7f, 0x3e, 0x6a, 0x5a, 0x21, 0x76, 0x31, 0x97, 0xa0, 0xc6, 0x7d, 0x03, 0xf7, 0x27, 0x45, 0x5a, 0x75, 0x03, 0xc1, 0x5c, 0x94, 0x2b, 0x37, 0x9f, 0x46, 0x8f, 0xc3, 0xa7, 0x50, 0xe4, 0xe7, 0x23, 0xf7, 0x20, 0xa2, 0x8e, 0x4b, 0xfd, 0x7a, 0xa7, 0x8a, 0x54, 0x7b, 0x32, 0xef, 0x0e, 0x82, 0xb9, 0xf9, 0x14, 0x62, 0x68, 0x32, 0x9e, 0x55, 0xc0, 0xd8, 0xc7, 0x41, 0x9c, 0x67, 0x95, 0xbf, 0xc3, 0x86, 0x74, 0x70, 0x64, 0x44, 0x23, 0x77, 0x79, 0x82, 0x23, 0x1c, 0xf4, 0xa1, 0x05, 0xd3, 0x98, 0x89, 0xde, 0x7d, 0xb3, 0x5b, 0xef, 0x38, 0xd2, 0x07, 0xbc, 0x5a, 0x69, 0xa3, 0xe4, 0x37, 0x9b, 0x53, 0xff, 0x04, 0x6b, 0xd9, 0xd8, 0x32, 0x89, 0xf7, 0x82, 0x77, 0xcf, 0xe6, 0xff, 0xf4, 0x15, 0x54, 0x91, 0x65, 0x96, 0x49, 0xd7, 0x0a, 0xa4, 0xf3, 0x55, 0x2b, 0xc1, 0x48, 0xc1, 0x7e, 0x56, 0x69, 0x27, 0xf4, 0xd1, 0x47, 0x1f, 0xde, 0x86, 0x15, 0x67, 0x04, 0x9d, 0x41, 0x1f, 0xe8, 0xe1, 0x23, 0xe4, 0x56, 0xb9, 0xdb, 0x4e, 0xe4, 0x84, 0x6c, 0x63, 0x39, 0xad, 0x44, 0x6d, 0x4e, 0x28, 0xcd, 0xf6, 0xac, 0xec, 0xc2, 0xad, 0xcd, 0xc3, 0xed, 0x03, 0x63, 0x5d, 0xef, 0x1d, 0x40, 0x8d, 0x9a, 0x02, 0x67, 0x4b, 0x55, 0xb5, 0xfe, 0x75, 0xb6, 0x53, 0x34, 0x1d, 0x7b, 0x26, 0x23, 0xfe, 0xb9, 0x21, 0xd3, 0xe0, 0xa0, 0x1a, 0x85, 0xe5 }; static const unsigned char stream_10b[1300] = { 0x18, 0x00, 0xd7, 0xfb, 0x12, 0xda, 0xdb, 0x68, 0xeb, 0x38, 0x4d, 0xf6, 0xb2, 0x45, 0x74, 0x4c, 0xcc, 0xe7, 0xa7, 0xc1, 0x26, 0x84, 0x3d, 0xdf, 0x7d, 0xc5, 0xe9, 0xd4, 0x31, 0xa2, 0x51, 0x38, 0x95, 0xe2, 0x68, 0x11, 0x9d, 0xd1, 0x52, 0xb5, 0xef, 0x76, 0xe0, 0x3d, 0x11, 0x50, 0xd7, 0xb2, 0xc1, 0x7d, 0x12, 0xaf, 0x02, 0x52, 0x97, 0x03, 0xf3, 0x2e, 0x54, 0xdf, 0xa0, 0x40, 0x76, 0x52, 0x82, 0x23, 0x3c, 0xbd, 0x20, 0x6d, 0x0a, 0x6f, 0x81, 0xfc, 0x41, 0x9d, 0x2e, 0xa7, 0x2c, 0x78, 0x9c, 0xd8, 0x56, 0xb0, 0x31, 0x35, 0xc8, 0x53, 0xef, 0xf9, 0x43, 0x17, 0xc0, 0x8c, 0x2c, 0x8f, 0x4a, 0x68, 0xe8, 0x9f, 0xbd, 0x3f, 0xf2, 0x18, 0xb8, 0xe6, 0x55, 0xea, 0x2a, 0x37, 0x3e, 0xac, 0xb0, 0x75, 0xd4, 0x75, 0x12, 0x82, 0xec, 0x21, 0xb9, 0xce, 0xe5, 0xc1, 0x62, 0x49, 0xd5, 0xf1, 0xca, 0xd4, 0x32, 0x76, 0x34, 0x5f, 0x3e, 0xc9, 0xb3, 0x54, 0xe4, 0xd0, 0xa9, 0x7d, 0x0c, 0x64, 0x48, 0x0a, 0x74, 0x38, 0x03, 0xd0, 0x20, 0xac, 0xe3, 0x58, 0x3d, 0x4b, 0xa7, 0x46, 0xac, 0x57, 0x63, 0x12, 0x17, 0xcb, 0x96, 0xed, 0xc9, 0x39, 0x64, 0xde, 0xff, 0xc6, 0xb2, 0x40, 0x2c, 0xf9, 0x1d, 0xa6, 0x94, 0x2a, 0x16, 0x4d, 0x7f, 0x22, 0x91, 0x8b, 0xfe, 0x83, 0x77, 0x02, 0x68, 0x62, 0x27, 0x77, 0x2e, 0xe9, 0xce, 0xbc, 0x20, 0xe8, 0xfb, 0xf8, 0x4e, 0x17, 0x07, 0xe1, 0xaa, 0x29, 0xb7, 0x50, 0xcf, 0xb0, 0x6a, 0xcf, 0x01, 0xec, 0xbf, 0xff, 0xb5, 0x9f, 0x00, 0x64, 0x80, 0xbb, 0xa6, 0xe4, 0xa2, 0x1e, 0xe4, 0xf8, 0xa3, 0x0d, 0xc7, 0x65, 0x45, 0xb7, 0x01, 0x33, 0x80, 0x37, 0x11, 0x16, 0x34, 0xc1, 0x06, 0xc5, 0xd3, 0xc4, 0x70, 0x62, 0x75, 0xd8, 0xa3, 0xba, 0x84, 0x9f, 0x81, 0x9f, 0xda, 0x01, 0x83, 0x42, 0x84, 0x05, 0x69, 0x68, 0xb0, 0x74, 0x73, 0x0f, 0x68, 0x39, 0xd3, 0x11, 0xc5, 0x55, 0x3e, 0xf2, 0xb7, 0xf4, 0xa6, 0xed, 0x0b, 0x50, 0xbe, 0x44, 0xf8, 0x67, 0x48, 0x46, 0x5e, 0x71, 0x07, 0xcf, 0xca, 0x8a, 0xbc, 0xa4, 0x3c, 0xd2, 0x4a, 0x80, 0x2e, 0x4f, 0xc5, 0x3b, 0x61, 0xc1, 0x7e, 0x93, 0x9e, 0xe0, 0x05, 0xfb, 0x10, 0xe8, 0x53, 0xff, 0x16, 0x5e, 0x18, 0xe0, 0x9f, 0x39, 0xbf, 0xaa, 0x80, 0x6d, 0xb7, 0x9f, 0x51, 0x91, 0xa0, 0xf6, 0xce, 0xad, 0xed, 0x56, 0x15, 0xb9, 0x12, 0x57, 0x60, 0xa6, 0xae, 0x54, 0x6e, 0x36, 0xf3, 0xe0, 0x05, 0xd8, 0x3e, 0x6d, 0x08, 0x36, 0xc9, 0x79, 0x64, 0x51, 0x63, 0x92, 0xa8, 0xa1, 0xbf, 0x55, 0x26, 0x80, 0x75, 0x44, 0x33, 0x33, 0xfb, 0xb7, 0xec, 0xf9, 0xc6, 0x01, 0xf9, 0xd5, 0x93, 0xfc, 0xb7, 0x43, 0xa2, 0x38, 0x0d, 0x17, 0x75, 0x67, 0xec, 0xc9, 0x98, 0xd6, 0x25, 0xe6, 0xb9, 0xed, 0x61, 0xa4, 0xee, 0x2c, 0xda, 0x27, 0xbd, 0xff, 0x86, 0x1e, 0x45, 0x64, 0xfe, 0xcf, 0x0c, 0x9b, 0x7b, 0x75, 0x5f, 0xf1, 0xe0, 0xba, 0x77, 0x8c, 0x03, 0x8f, 0xb4, 0x3a, 0xb6, 0x9c, 0xda, 0x9a, 0x83, 0xcb, 0xe9, 0xcb, 0x3f, 0xf4, 0x10, 0x99, 0x5b, 0xe1, 0x19, 0x8f, 0x6b, 0x95, 0x50, 0xe6, 0x78, 0xc9, 0x35, 0xb6, 0x87, 0xd8, 0x9e, 0x17, 0x30, 0x96, 0x70, 0xa3, 0x04, 0x69, 0x1c, 0xa2, 0x6c, 0xd4, 0x88, 0x48, 0x44, 0x14, 0x94, 0xd4, 0xc9, 0x4d, 0xe3, 0x82, 0x7e, 0x62, 0xf0, 0x0a, 0x18, 0x4d, 0xd0, 0xd6, 0x63, 0xa3, 0xdf, 0xea, 0x28, 0xf4, 0x00, 0x75, 0x70, 0x78, 0x08, 0x70, 0x3f, 0xff, 0x84, 0x86, 0x72, 0xea, 0x4f, 0x15, 0x8c, 0x17, 0x60, 0x5f, 0xa1, 0x50, 0xa0, 0xfc, 0x6f, 0x8a, 0x46, 0xfc, 0x01, 0x8d, 0x7c, 0xdc, 0x69, 0x6a, 0xd3, 0x74, 0x69, 0x76, 0x77, 0xdd, 0xe4, 0x9c, 0x49, 0x1e, 0x6f, 0x7d, 0x31, 0x14, 0xd9, 0xe9, 0xe7, 0x17, 0x66, 0x82, 0x1b, 0xf1, 0x0f, 0xe2, 0xba, 0xd2, 0x28, 0xd1, 0x6f, 0x48, 0xc7, 0xac, 0x08, 0x4e, 0xee, 0x94, 0x66, 0x99, 0x34, 0x16, 0x5d, 0x95, 0xae, 0xe3, 0x59, 0x79, 0x7f, 0x8e, 0x9f, 0xe3, 0xdb, 0xff, 0xdc, 0x4d, 0xb0, 0xbf, 0xf9, 0xf3, 0x3e, 0xec, 0xcf, 0x50, 0x3d, 0x2d, 0xba, 0x94, 0x1f, 0x1a, 0xab, 0xa4, 0xf4, 0x67, 0x43, 0x7e, 0xb9, 0x65, 0x20, 0x13, 0xb1, 0xd9, 0x88, 0x4a, 0x24, 0x13, 0x84, 0x86, 0xae, 0x2b, 0x0c, 0x6c, 0x7e, 0xd4, 0x25, 0x6e, 0xaa, 0x8d, 0x0c, 0x54, 0x99, 0xde, 0x1d, 0xac, 0x8c, 0x5c, 0x73, 0x94, 0xd9, 0x75, 0xcb, 0x5a, 0x54, 0x3d, 0xeb, 0xff, 0xc1, 0x95, 0x53, 0xb5, 0x39, 0xf7, 0xe5, 0xf1, 0x77, 0xd1, 0x42, 0x82, 0x4b, 0xb0, 0xab, 0x19, 0x28, 0xff, 0x53, 0x28, 0x87, 0x46, 0xc6, 0x6f, 0x05, 0x06, 0xa6, 0x0c, 0x97, 0x93, 0x68, 0x38, 0xe1, 0x61, 0xed, 0xf8, 0x90, 0x13, 0xa3, 0x6f, 0xf2, 0x08, 0x37, 0xd7, 0x05, 0x25, 0x34, 0x43, 0x57, 0x72, 0xfd, 0x6c, 0xc2, 0x19, 0x26, 0xe7, 0x50, 0x30, 0xb8, 0x6d, 0x09, 0x71, 0x83, 0x75, 0xd4, 0x11, 0x25, 0x29, 0xc6, 0xee, 0xb2, 0x51, 0x1c, 0x1c, 0x9e, 0x2d, 0x09, 0xb9, 0x73, 0x2b, 0xbf, 0xda, 0xc8, 0x1e, 0x2b, 0xe5, 0x3f, 0x1e, 0x63, 0xe9, 0xc0, 0x6d, 0x04, 0x3a, 0x48, 0x61, 0xa8, 0xc6, 0x16, 0x8d, 0x69, 0xc0, 0x67, 0x0c, 0x3b, 0xc4, 0x05, 0x36, 0xa1, 0x30, 0x62, 0x92, 0x4d, 0x44, 0x31, 0x66, 0x46, 0xda, 0xef, 0x0f, 0x4e, 0xfb, 0x78, 0x6a, 0xa9, 0x5b, 0xf8, 0x56, 0x26, 0x74, 0x16, 0xab, 0x17, 0x93, 0x3c, 0x36, 0xbb, 0xa2, 0xbf, 0xad, 0xba, 0xb1, 0xfe, 0xc4, 0x9f, 0x75, 0x47, 0x1e, 0x99, 0x7e, 0x32, 0xe8, 0xd4, 0x6c, 0xa4, 0xf8, 0xd2, 0xe4, 0xb2, 0x51, 0xbb, 0xb2, 0xd7, 0xce, 0x94, 0xaf, 0x7f, 0xe6, 0x2c, 0x13, 0xae, 0xd2, 0x29, 0x30, 0x7b, 0xfd, 0x25, 0x61, 0xf9, 0xe8, 0x35, 0x2d, 0x1a, 0xc9, 0x81, 0xa5, 0xfe, 0xce, 0xf6, 0x17, 0xc5, 0xfb, 0x8c, 0x79, 0x67, 0xa8, 0x5f, 0x5c, 0x31, 0xbc, 0xfc, 0xf3, 0x6b, 0xd3, 0x0d, 0xe0, 0x62, 0xab, 0x86, 0xc3, 0x17, 0x5a, 0xba, 0x97, 0x86, 0x8f, 0x65, 0xd6, 0xbd, 0x0c, 0xa1, 0xfb, 0x7f, 0x7c, 0xdc, 0xcb, 0x94, 0x30, 0x0b, 0x04, 0x54, 0xc4, 0x31, 0xa1, 0xca, 0x1e, 0xc5, 0xf0, 0xb6, 0x08, 0xd7, 0x2e, 0xa1, 0x90, 0x41, 0xce, 0xd9, 0xef, 0x3a, 0x58, 0x01, 0x1a, 0x73, 0x18, 0xad, 0xdc, 0x20, 0x25, 0x95, 0x1a, 0xfe, 0x61, 0xf1, 0x58, 0x32, 0x8b, 0x43, 0x59, 0xd6, 0x21, 0xdb, 0xa9, 0x8e, 0x54, 0xe6, 0x21, 0xcf, 0xd3, 0x6b, 0x59, 0x29, 0x9b, 0x3e, 0x6c, 0x7f, 0xe2, 0x29, 0x72, 0x8c, 0xd1, 0x3e, 0x9a, 0x84, 0x98, 0xb0, 0xf3, 0x20, 0x30, 0x34, 0x71, 0xa7, 0x5b, 0xf0, 0x26, 0xe1, 0xf4, 0x76, 0x65, 0xc9, 0xd7, 0xe4, 0xb9, 0x25, 0x48, 0xc2, 0x7e, 0xa6, 0x0b, 0x0d, 0x05, 0x68, 0xa1, 0x96, 0x61, 0x0b, 0x4c, 0x2f, 0x1a, 0xe3, 0x56, 0x71, 0x89, 0x48, 0x66, 0xd8, 0xd0, 0x69, 0x37, 0x7a, 0xdf, 0xdb, 0xed, 0xad, 0x82, 0xaa, 0x40, 0x25, 0x47, 0x3e, 0x75, 0xa6, 0x0e, 0xf5, 0x2f, 0xa7, 0x4e, 0x97, 0xa2, 0x5f, 0x01, 0x99, 0x48, 0x3a, 0x63, 0x18, 0x20, 0x61, 0x72, 0xe4, 0xcf, 0x4b, 0x3b, 0x99, 0x36, 0xe1, 0xf3, 0xbf, 0xae, 0x2b, 0x6b, 0xa1, 0x94, 0xa0, 0x15, 0x94, 0xd6, 0xe0, 0xba, 0x71, 0xa2, 0x85, 0xa0, 0x8c, 0x5e, 0x58, 0xe2, 0xde, 0x6b, 0x08, 0x68, 0x90, 0x82, 0x71, 0x8d, 0xfd, 0x12, 0xa2, 0x49, 0x87, 0x70, 0xee, 0x2a, 0x08, 0xe2, 0x26, 0xaf, 0xeb, 0x85, 0x35, 0xd2, 0x0e, 0xfd, 0x2b, 0x6f, 0xc0, 0xfe, 0x41, 0xbb, 0xd7, 0x0a, 0xa3, 0x8d, 0x8b, 0xec, 0x44, 0x9f, 0x46, 0x59, 0x4d, 0xac, 0x04, 0x1e, 0xde, 0x10, 0x7b, 0x17, 0x0a, 0xb0, 0xcc, 0x26, 0x0c, 0xa9, 0x3c, 0x5f, 0xd8, 0xe6, 0x52, 0xd3, 0xfd, 0x0b, 0x66, 0x75, 0x06, 0x84, 0x23, 0x64, 0x2b, 0x80, 0x68, 0xf9, 0xcb, 0xcd, 0x04, 0x07, 0xf7, 0xe0, 0x07, 0xb4, 0xc6, 0xa0, 0x08, 0xd0, 0x76, 0x16, 0x77, 0xd8, 0x48, 0xf0, 0x45, 0x4e, 0xe2, 0xf2, 0x88, 0xcd, 0x0f, 0xbd, 0x7d, 0xb6, 0xbe, 0x4e, 0x9e, 0x5d, 0x6c, 0x47, 0x26, 0x34, 0x94, 0xfb, 0xc5, 0x4f, 0x5c, 0xb5, 0xb5, 0xfc, 0x99, 0x34, 0x71, 0xe5, 0xe1, 0x36, 0x0c, 0xd2, 0x95, 0xb8, 0x93, 0x3c, 0x5d, 0x2d, 0x71, 0x55, 0x0b, 0x96, 0x4e, 0x9f, 0x07, 0x9a, 0x38, 0x9a, 0xcc, 0x24, 0xb5, 0xac, 0x05, 0x8b, 0x1c, 0x61, 0xd4, 0xf2, 0xdf, 0x9e, 0x11, 0xe3, 0x7d, 0x64, 0x2f, 0xe5, 0x13, 0xd4, 0x0a, 0xe9, 0x32, 0x26, 0xa8, 0x93, 0x21, 0x59, 0xf3, 0x41, 0x48, 0x0a, 0xbd, 0x59, 0x8f, 0xf8, 0x72, 0xab, 0xd3, 0x65, 0x8e, 0xdc, 0xaa, 0x0c, 0xc0, 0x01, 0x36, 0xb7, 0xf5, 0x84, 0x27, 0x9a, 0x98, 0x89, 0x73, 0x3a, 0xeb, 0x55, 0x15, 0xc9, 0x3d, 0xe1, 0xf8, 0xea, 0xf6, 0x11, 0x28, 0xe0, 0x80, 0x93, 0xcc, 0xba, 0xe1, 0xf1, 0x81, 0xbc, 0xa4, 0x30, 0xbc, 0x98, 0xe8, 0x9e, 0x8d, 0x17, 0x7e, 0xb7, 0xb1, 0x27, 0x6f, 0xcf, 0x9c, 0x0d, 0x1d, 0x01, 0xea, 0x45, 0xc0, 0x90, 0xda, 0x53, 0xf6, 0xde, 0xdf, 0x12, 0xa1, 0x23, 0x3d, 0x92, 0x89, 0x77, 0xa7, 0x2a, 0xe7, 0x45, 0x24, 0xdd, 0xf2, 0x17, 0x10, 0xca, 0x6e, 0x14, 0xb2, 0x77, 0x08, 0xc4, 0x18, 0xcd }; static uint64_t stream_10a_off, stream_10b_off; static int check_stream_10a(struct helper *h) { if (!TEST_uint64_t_ge(h->frame.stream.len, 1150) || !TEST_uint64_t_le(h->frame.stream.len, 1200)) return 0; if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len, stream_10a, (size_t)h->frame.stream.len)) return 0; stream_10a_off = h->frame.stream.offset + h->frame.stream.len; return 1; } static int check_stream_10b(struct helper *h) { if (!TEST_uint64_t_ge(h->frame.stream.len, 1150) || !TEST_uint64_t_le(h->frame.stream.len, 1200)) return 0; if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len, stream_10b, (size_t)h->frame.stream.len)) return 0; stream_10b_off = h->frame.stream.offset + h->frame.stream.len; return 1; } static int check_stream_10c(struct helper *h) { if (!TEST_uint64_t_ge(h->frame.stream.len, 5) || !TEST_uint64_t_le(h->frame.stream.len, 200)) return 0; if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len, stream_10a + stream_10a_off, (size_t)h->frame.stream.len)) return 0; return 1; } static int check_stream_10d(struct helper *h) { if (!TEST_uint64_t_ge(h->frame.stream.len, 5) || !TEST_uint64_t_le(h->frame.stream.len, 200)) return 0; if (!TEST_mem_eq(h->frame.stream.data, (size_t)h->frame.stream.len, stream_10b + stream_10b_off, (size_t)h->frame.stream.len)) return 0; return 1; } static const struct script_op script_10[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_HANDSHAKE_COMPLETE() OP_TXP_GENERATE_NONE() OP_STREAM_NEW(42) OP_STREAM_NEW(43) OP_CONN_TXFC_BUMP(10000) OP_STREAM_TXFC_BUMP(42, 5000) OP_STREAM_TXFC_BUMP(43, 5000) OP_STREAM_SEND(42, stream_10a) OP_STREAM_SEND(43, stream_10b) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1100, 1200) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM) OP_CHECK(check_stream_10a) OP_EXPECT_NO_FRAME() OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1100, 1200) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM) OP_CHECK(check_stream_10b) OP_EXPECT_NO_FRAME() OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(200, 500) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN) OP_CHECK(check_stream_10c) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STREAM_OFF) OP_CHECK(check_stream_10d) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const struct script_op script_11[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CRYPTO_SEND(QUIC_PN_SPACE_INITIAL, crypto_1) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1200, 1200) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CRYPTO) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int check_stream_12(struct helper *h) { if (!TEST_uint64_t_eq(h->frame.stop_sending.stream_id, 42) || !TEST_uint64_t_eq(h->frame.stop_sending.app_error_code, 4568)) return 0; return 1; } static const struct script_op script_12[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_HANDSHAKE_COMPLETE() OP_TXP_GENERATE_NONE() OP_STREAM_NEW(42) OP_STOP_SENDING(42, 4568) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 128) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_STOP_SENDING) OP_CHECK(check_stream_12) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char stream_13[] = { 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x7a, 0x7b }; static ossl_unused int check_stream_13(struct helper *h) { if (!TEST_uint64_t_eq(h->frame.reset_stream.stream_id, 42) || !TEST_uint64_t_eq(h->frame.reset_stream.app_error_code, 4568) || !TEST_uint64_t_eq(h->frame.reset_stream.final_size, 0)) return 0; return 1; } static const struct script_op script_13[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_HANDSHAKE_COMPLETE() OP_TXP_GENERATE_NONE() OP_STREAM_NEW(42) OP_CONN_TXFC_BUMP(8) OP_STREAM_TXFC_BUMP(42, 8) OP_STREAM_SEND(42, stream_13) OP_RESET_STREAM(42, 4568) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 128) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_RESET_STREAM) OP_CHECK(check_stream_13) OP_NEXT_FRAME() OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int gen_conn_close(struct helper *h) { OSSL_QUIC_FRAME_CONN_CLOSE f = {0}; f.error_code = 2345; f.frame_type = OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE; f.reason = "Reason string"; f.reason_len = strlen(f.reason); if (!TEST_true(ossl_quic_tx_packetiser_schedule_conn_close(h->txp, &f))) return 0; return 1; } static int check_14(struct helper *h) { if (!TEST_int_eq(h->frame.conn_close.is_app, 0) || !TEST_uint64_t_eq(h->frame.conn_close.frame_type, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE) || !TEST_uint64_t_eq(h->frame.conn_close.error_code, 2345) || !TEST_mem_eq(h->frame.conn_close.reason, h->frame.conn_close.reason_len, "Reason string", 13)) return 0; return 1; } static const struct script_op script_14[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_HANDSHAKE_COMPLETE() OP_TXP_GENERATE_NONE() OP_CHECK(gen_conn_close) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT) OP_CHECK(check_14) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_END }; static int gen_probe_initial(struct helper *h) { OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm); ++probe->anti_deadlock_initial; return 1; } static const struct script_op script_15[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(gen_probe_initial) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1200, 1200) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int gen_probe_handshake(struct helper *h) { OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm); ++probe->anti_deadlock_handshake; return 1; } static const struct script_op script_16[] = { OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(gen_probe_handshake) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static int gen_probe_1rtt(struct helper *h) { OSSL_ACKM_PROBE_INFO *probe = ossl_ackm_get0_probe_request(h->args.ackm); ++probe->pto[QUIC_PN_SPACE_APP]; return 1; } static const struct script_op script_17[] = { OP_DISCARD_EL(QUIC_ENC_LEVEL_INITIAL) OP_DISCARD_EL(QUIC_ENC_LEVEL_HANDSHAKE) OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_1RTT, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(gen_probe_1rtt) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(21, 512) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_PING) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() OP_END }; static const unsigned char big_token[1950]; static int try_big_token(struct helper *h) { size_t i; if (!TEST_false(ossl_quic_tx_packetiser_set_initial_token(h->txp, big_token, sizeof(big_token), NULL, NULL))) return 0; for (i = sizeof(big_token) - 1;; --i) { if (!TEST_size_t_gt(i, 0)) return 0; if (ossl_quic_tx_packetiser_set_initial_token(h->txp, big_token, i, NULL, NULL)) break; } return 1; } static const struct script_op script_18[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CHECK(try_big_token) OP_TXP_GENERATE_NONE() OP_CRYPTO_SEND(QUIC_PN_SPACE_INITIAL, crypto_1) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1200, 1200) OP_NEXT_FRAME() OP_EXPECT_FRAME(OSSL_QUIC_FRAME_TYPE_CRYPTO) OP_EXPECT_NO_FRAME() OP_RX_PKT_NONE() OP_TXP_GENERATE_NONE() 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 }; static void skip_padding(struct helper *h) { uint64_t frame_type; if (!ossl_quic_wire_peek_frame_header(&h->pkt, &frame_type, NULL)) return; if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) ossl_quic_wire_decode_padding(&h->pkt); } static int run_script(int script_idx, const struct script_op *script) { int testresult = 0, have_helper = 0; QUIC_TXP_STATUS status; struct helper h; const struct script_op *op; size_t opn = 0; if (!helper_init(&h)) goto err; have_helper = 1; for (op = script, opn = 0; op->opcode != OPK_END; ++op, ++opn) { switch (op->opcode) { case OPK_TXP_GENERATE: if (!TEST_true(ossl_quic_tx_packetiser_generate(h.txp, &status)) && !TEST_size_t_gt(status.sent_pkt, 0)) goto err; ossl_qtx_finish_dgram(h.args.qtx); ossl_qtx_flush_net(h.args.qtx); break; case OPK_TXP_GENERATE_NONE: if (!TEST_true(ossl_quic_tx_packetiser_generate(h.txp, &status)) && !TEST_size_t_eq(status.sent_pkt, 0)) goto err; break; case OPK_RX_PKT: ossl_quic_demux_pump(h.demux); ossl_qrx_pkt_release(h.qrx_pkt); h.qrx_pkt = NULL; if (!TEST_true(ossl_qrx_read_pkt(h.qrx, &h.qrx_pkt))) goto err; if (!TEST_true(PACKET_buf_init(&h.pkt, h.qrx_pkt->hdr->data, h.qrx_pkt->hdr->len))) goto err; h.frame_type = UINT64_MAX; break; case OPK_RX_PKT_NONE: ossl_quic_demux_pump(h.demux); if (!TEST_false(ossl_qrx_read_pkt(h.qrx, &h.qrx_pkt))) goto err; h.frame_type = UINT64_MAX; break; case OPK_EXPECT_DGRAM_LEN: if (!TEST_size_t_ge(h.qrx_pkt->datagram_len, (size_t)op->arg0) || !TEST_size_t_le(h.qrx_pkt->datagram_len, (size_t)op->arg1)) goto err; break; case OPK_EXPECT_FRAME: if (!TEST_uint64_t_eq(h.frame_type, op->arg0)) goto err; break; case OPK_EXPECT_INITIAL_TOKEN: if (!TEST_mem_eq(h.qrx_pkt->hdr->token, h.qrx_pkt->hdr->token_len, op->buf, (size_t)op->arg0)) goto err; break; case OPK_EXPECT_HDR: if (!TEST_true(cmp_pkt_hdr(h.qrx_pkt->hdr, op->buf, NULL, 0, 0))) goto err; break; case OPK_CHECK: if (!TEST_true(op->check_func(&h))) goto err; break; case OPK_NEXT_FRAME: skip_padding(&h); if (!ossl_quic_wire_peek_frame_header(&h.pkt, &h.frame_type, NULL)) { h.frame_type = UINT64_MAX; break; } switch (h.frame_type) { case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE: if (!TEST_true(ossl_quic_wire_decode_frame_handshake_done(&h.pkt))) goto err; break; case OSSL_QUIC_FRAME_TYPE_PING: if (!TEST_true(ossl_quic_wire_decode_frame_ping(&h.pkt))) goto err; break; case OSSL_QUIC_FRAME_TYPE_MAX_DATA: if (!TEST_true(ossl_quic_wire_decode_frame_max_data(&h.pkt, &h.frame.max_data))) goto err; break; case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID: if (!TEST_true(ossl_quic_wire_decode_frame_new_conn_id(&h.pkt, &h.frame.new_conn_id))) goto err; break; case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN: if (!TEST_true(ossl_quic_wire_decode_frame_new_token(&h.pkt, &h.frame.new_token.token, &h.frame.new_token.token_len))) goto err; break; case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN: case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN: h.frame.ack.ack_ranges = h.ack_ranges; h.frame.ack.num_ack_ranges = OSSL_NELEM(h.ack_ranges); if (!TEST_true(ossl_quic_wire_decode_frame_ack(&h.pkt, h.args.ack_delay_exponent, &h.frame.ack, NULL))) goto err; break; case OSSL_QUIC_FRAME_TYPE_CRYPTO: if (!TEST_true(ossl_quic_wire_decode_frame_crypto(&h.pkt, 0, &h.frame.crypto))) goto err; break; case OSSL_QUIC_FRAME_TYPE_STREAM: case OSSL_QUIC_FRAME_TYPE_STREAM_FIN: case OSSL_QUIC_FRAME_TYPE_STREAM_LEN: case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN: case OSSL_QUIC_FRAME_TYPE_STREAM_OFF: case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN: case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN: case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN: if (!TEST_true(ossl_quic_wire_decode_frame_stream(&h.pkt, 0, &h.frame.stream))) goto err; break; case OSSL_QUIC_FRAME_TYPE_STOP_SENDING: if (!TEST_true(ossl_quic_wire_decode_frame_stop_sending(&h.pkt, &h.frame.stop_sending))) goto err; break; case OSSL_QUIC_FRAME_TYPE_RESET_STREAM: if (!TEST_true(ossl_quic_wire_decode_frame_reset_stream(&h.pkt, &h.frame.reset_stream))) goto err; break; case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT: case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP: if (!TEST_true(ossl_quic_wire_decode_frame_conn_close(&h.pkt, &h.frame.conn_close))) goto err; break; default: TEST_error("unknown frame type"); goto err; } break; case OPK_EXPECT_NO_FRAME: skip_padding(&h); if (!TEST_size_t_eq(PACKET_remaining(&h.pkt), 0)) goto err; break; case OPK_PROVIDE_SECRET: if (!TEST_true(ossl_qtx_provide_secret(h.args.qtx, (uint32_t)op->arg0, (uint32_t)op->arg1, NULL, op->buf, op->buf_len))) goto err; if (!TEST_true(ossl_qrx_provide_secret(h.qrx, (uint32_t)op->arg0, (uint32_t)op->arg1, NULL, op->buf, op->buf_len))) goto err; break; case OPK_DISCARD_EL: if (!TEST_true(ossl_quic_tx_packetiser_discard_enc_level(h.txp, (uint32_t)op->arg0))) goto err; break; case OPK_CRYPTO_SEND: { size_t consumed = 0; if (!TEST_true(ossl_quic_sstream_append(h.args.crypto[op->arg0], op->buf, op->buf_len, &consumed))) goto err; if (!TEST_size_t_eq(consumed, op->buf_len)) goto err; } break; case OPK_STREAM_NEW: { QUIC_STREAM *s; if (!TEST_ptr(s = ossl_quic_stream_map_alloc(h.args.qsm, op->arg0, QUIC_STREAM_DIR_BIDI))) goto err; if (!TEST_ptr(s->sstream = ossl_quic_sstream_new(512 * 1024)) || !TEST_true(ossl_quic_txfc_init(&s->txfc, &h.conn_txfc)) || !TEST_true(ossl_quic_rxfc_init(&s->rxfc, &h.conn_rxfc, 1 * 1024 * 1024, 16 * 1024 * 1024, fake_now, NULL)) || !TEST_ptr(s->rstream = ossl_quic_rstream_new(&s->rxfc, NULL, 1024))) { ossl_quic_sstream_free(s->sstream); ossl_quic_stream_map_release(h.args.qsm, s); goto err; } } break; case OPK_STREAM_SEND: { QUIC_STREAM *s; size_t consumed = 0; if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm, op->arg0))) goto err; if (!TEST_true(ossl_quic_sstream_append(s->sstream, op->buf, op->buf_len, &consumed))) goto err; if (!TEST_size_t_eq(consumed, op->buf_len)) goto err; ossl_quic_stream_map_update_state(h.args.qsm, s); } break; case OPK_STREAM_FIN: { QUIC_STREAM *s; if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm, op->arg0))) goto err; ossl_quic_sstream_fin(s->sstream); } break; case OPK_STOP_SENDING: { QUIC_STREAM *s; if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm, op->arg0))) goto err; if (!TEST_true(ossl_quic_stream_map_stop_sending_recv_part(h.args.qsm, s, op->arg1))) goto err; ossl_quic_stream_map_update_state(h.args.qsm, s); if (!TEST_true(s->active)) goto err; } break; case OPK_RESET_STREAM: { QUIC_STREAM *s; if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm, op->arg0))) goto err; if (!TEST_true(ossl_quic_stream_map_reset_stream_send_part(h.args.qsm, s, op->arg1))) goto err; ossl_quic_stream_map_update_state(h.args.qsm, s); if (!TEST_true(s->active)) goto err; } break; case OPK_CONN_TXFC_BUMP: if (!TEST_true(ossl_quic_txfc_bump_cwm(h.args.conn_txfc, op->arg0))) goto err; break; case OPK_STREAM_TXFC_BUMP: { QUIC_STREAM *s; if (!TEST_ptr(s = ossl_quic_stream_map_get_by_id(h.args.qsm, op->arg1))) goto err; if (!TEST_true(ossl_quic_txfc_bump_cwm(&s->txfc, op->arg0))) goto err; ossl_quic_stream_map_update_state(h.args.qsm, s); } break; case OPK_HANDSHAKE_COMPLETE: ossl_quic_tx_packetiser_notify_handshake_complete(h.txp); break; case OPK_NOP: break; default: TEST_error("bad opcode"); goto err; } } testresult = 1; err: if (!testresult) TEST_error("script %d failed at op %zu", script_idx + 1, opn + 1); if (have_helper) helper_cleanup(&h); return testresult; } static int test_script(int idx) { return run_script(idx, scripts[idx]); } static const unsigned char dyn_script_1_crypto_1a[1200]; static const unsigned char dyn_script_1_crypto_1b[1]; static int check_is_initial(struct helper *h) { return h->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL; } static int check_is_handshake(struct helper *h) { return h->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE; } static struct script_op dyn_script_1[] = { OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_INITIAL, QRL_SUITE_AES128GCM, secret_1) OP_PROVIDE_SECRET(QUIC_ENC_LEVEL_HANDSHAKE, QRL_SUITE_AES128GCM, secret_1) OP_TXP_GENERATE_NONE() OP_CRYPTO_SEND(QUIC_PN_SPACE_INITIAL, dyn_script_1_crypto_1a) OP_CRYPTO_SEND(QUIC_PN_SPACE_HANDSHAKE, dyn_script_1_crypto_1b) OP_TXP_GENERATE() OP_RX_PKT() OP_EXPECT_DGRAM_LEN(1200, 1200) OP_CHECK(check_is_initial) OP_NOP() OP_NOP() OP_END }; static const size_t dyn_script_1_crypto_idx = 3; static const size_t dyn_script_1_pkt_idx = 9; static const size_t dyn_script_1_check_idx = 10; static const size_t dyn_script_1_start_from = 1000; static int test_dyn_script_1(int idx) { size_t target_size = dyn_script_1_start_from + (size_t)idx; int expect_handshake_pkt_in_same_dgram = (target_size <= 1115); dyn_script_1[dyn_script_1_crypto_idx].buf_len = target_size; if (expect_handshake_pkt_in_same_dgram) { dyn_script_1[dyn_script_1_pkt_idx].opcode = OPK_RX_PKT; dyn_script_1[dyn_script_1_check_idx].opcode = OPK_CHECK; dyn_script_1[dyn_script_1_check_idx].check_func = check_is_handshake; } else { dyn_script_1[dyn_script_1_pkt_idx].opcode = OPK_RX_PKT_NONE; dyn_script_1[dyn_script_1_check_idx].opcode = OPK_NOP; } if (!run_script(idx, dyn_script_1)) { TEST_error("failed dyn script 1 with target size %zu", target_size); return 0; } return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts)); ADD_ALL_TESTS(test_dyn_script_1, OSSL_NELEM(dyn_script_1_crypto_1a) - dyn_script_1_start_from + 1); return 1; }
test
openssl/test/quic_txp_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include "internal/nelem.h" #include <openssl/cmac.h> #include <openssl/aes.h> #include <openssl/evp.h> #include "testutil.h" static const char xtskey[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, 0x1f }; static struct test_st { const char key[32]; int key_len; unsigned char data[4096]; int data_len; const char *mac; } test[] = { { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 16, "My test data", 12, "29cec977c48f63c200bd5c4a6881b224" }, { { 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 }, 32, "My test data", 12, "db6493aa04e4761f473b2b453c031c9a" }, { { 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 }, 32, "My test data again", 18, "65c11c75ecf590badd0a5e56cbb8af60" }, { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 16, "#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#", 3072, "35da8a02a7afce90e5b711308cee2dee" }, { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, 24, "#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#", 4095, "59053f4e81f3593610f987adb547c5b2" }, { { 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 }, 32, "#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#", 2560, "9c6cf85f7f4baca99725764a0df973a9" }, { { 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 }, 24, "#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#", 2048, "2c2fccc7fcc5d98a" }, { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 16, "#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#", 2049, "c9a9cbc82a3b2d96074e386fce1216f2" }, }; static char *pt(unsigned char *md, unsigned int len); static int test_cmac_bad(void) { CMAC_CTX *ctx = NULL; int ret = 0; ctx = CMAC_CTX_new(); if (!TEST_ptr(ctx) || !TEST_false(CMAC_Init(ctx, NULL, 0, NULL, NULL)) || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len)) || !TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_128_cbc(), NULL)) || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len)) || !TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len, NULL, NULL)) || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len)) || !TEST_false(CMAC_Init(ctx, xtskey, sizeof(xtskey), EVP_aes_128_xts(), NULL)) || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len))) goto err; ret = 1; err: CMAC_CTX_free(ctx); return ret; } static int test_cmac_run(void) { char *p; CMAC_CTX *ctx = NULL; unsigned char buf[AES_BLOCK_SIZE]; size_t len; int ret = 0; size_t case_idx = 0; ctx = CMAC_CTX_new(); for (case_idx = 0; case_idx < OSSL_NELEM(test); case_idx++) { size_t str_len = strlen((char *)test[case_idx].data); size_t fill_len = test[case_idx].data_len - str_len; size_t fill_idx = str_len; while (fill_len > 0) { if (fill_len > str_len) { memcpy(&test[case_idx].data[fill_idx], test[case_idx].data, str_len); fill_len -= str_len; fill_idx += str_len; } else { memcpy(&test[case_idx].data[fill_idx], test[case_idx].data, fill_len); fill_len = 0; } } } if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len, EVP_aes_128_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[0].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, test[1].key, test[1].key_len, EVP_aes_256_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[1].data, test[1].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[1].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL)) || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[2].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, NULL, 0, NULL, NULL)) || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[2].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_256_cbc(), NULL)) || !TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL)) || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[2].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, test[3].key, test[3].key_len, EVP_aes_128_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[3].data, test[3].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[3].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, test[4].key, test[4].key_len, EVP_aes_192_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[4].data, test[4].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[4].mac)) goto err; if (!TEST_true(CMAC_Init(ctx, test[5].key, test[5].key_len, EVP_aes_256_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[5].data, test[5].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[5].mac)) goto err; #ifndef OPENSSL_NO_DES if (!TEST_true(CMAC_Init(ctx, test[6].key, test[6].key_len, EVP_des_ede3_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[6].data, test[6].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[6].mac)) goto err; #endif #ifndef OPENSSL_NO_SM4 if (!TEST_true(CMAC_Init(ctx, test[7].key, test[7].key_len, EVP_sm4_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[7].data, test[7].data_len)) || !TEST_true(CMAC_Final(ctx, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[7].mac)) goto err; #endif ret = 1; err: CMAC_CTX_free(ctx); return ret; } static int test_cmac_copy(void) { char *p; CMAC_CTX *ctx = NULL, *ctx2 = NULL; unsigned char buf[AES_BLOCK_SIZE]; size_t len; int ret = 0; ctx = CMAC_CTX_new(); ctx2 = CMAC_CTX_new(); if (!TEST_ptr(ctx) || !TEST_ptr(ctx2)) goto err; if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len, EVP_aes_128_cbc(), NULL)) || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len)) || !TEST_true(CMAC_CTX_copy(ctx2, ctx)) || !TEST_true(CMAC_Final(ctx2, buf, &len))) goto err; p = pt(buf, len); if (!TEST_str_eq(p, test[0].mac)) goto err; ret = 1; err: CMAC_CTX_free(ctx2); CMAC_CTX_free(ctx); return ret; } static char *pt(unsigned char *md, unsigned int len) { unsigned int i; static char buf[80]; for (i = 0; i < len; i++) sprintf(&(buf[i * 2]), "%02x", md[i]); return buf; } int setup_tests(void) { ADD_TEST(test_cmac_bad); ADD_TEST(test_cmac_run); ADD_TEST(test_cmac_copy); return 1; }
test
openssl/test/cmactest.c
openssl
#include <string.h> #include <openssl/ssl.h> #include <openssl/bio.h> #include <openssl/err.h> #include <openssl/conf.h> #include "internal/nelem.h" #include "testutil.h" #ifndef OPENSSL_NO_SOCK static const unsigned char clienthello_nocookie[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3A, 0x01, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x00, 0x00, 0x04, 0x00, 0x2f, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00 }; static const unsigned char clienthello_nocookie_frag[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x00 }; static const unsigned char clienthello_nocookie_short[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2F, 0x01, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00 }; static const unsigned char clienthello_2ndfrag[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x2C, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x00, 0x00, 0x04, 0x00, 0x2f, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00 }; static const unsigned char clienthello_cookie[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x01, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x14, 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x00, 0x04, 0x00, 0x2f, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00 }; static const unsigned char clienthello_cookie_frag[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x14, 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 }; static const unsigned char clienthello_badcookie[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x01, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x14, 0x01, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x00, 0x04, 0x00, 0x2f, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00 }; static const unsigned char clienthello_cookie_short[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x01, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37, 0xFE, 0xFD, 0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90, 0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56, 0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, 0x00, 0x14, 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12 }; static const unsigned char record_short[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char verify[] = { 0x16, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x03, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0xFE, 0xFF, 0x14, 0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 }; typedef struct { const unsigned char *in; unsigned int inlen; enum {GOOD, VERIFY, DROP} outtype; } tests; static tests testpackets[9] = { { clienthello_nocookie, sizeof(clienthello_nocookie), VERIFY }, { clienthello_nocookie_frag, sizeof(clienthello_nocookie_frag), VERIFY }, { clienthello_nocookie_short, sizeof(clienthello_nocookie_short), DROP }, { clienthello_2ndfrag, sizeof(clienthello_2ndfrag), DROP }, { clienthello_cookie, sizeof(clienthello_cookie), GOOD }, { clienthello_cookie_frag, sizeof(clienthello_cookie_frag), GOOD }, { clienthello_badcookie, sizeof(clienthello_badcookie), VERIFY }, { clienthello_cookie_short, sizeof(clienthello_cookie_short), DROP }, { record_short, sizeof(record_short), DROP } }; # define COOKIE_LEN 20 static int cookie_gen(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) { unsigned int i; for (i = 0; i < COOKIE_LEN; i++, cookie++) *cookie = i; *cookie_len = COOKIE_LEN; return 1; } static int cookie_verify(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len) { unsigned int i; if (cookie_len != COOKIE_LEN) return 0; for (i = 0; i < COOKIE_LEN; i++, cookie++) { if (*cookie != i) return 0; } return 1; } static int dtls_listen_test(int i) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; BIO *outbio = NULL; BIO *inbio = NULL; BIO_ADDR *peer = NULL; tests *tp = &testpackets[i]; char *data; long datalen; int ret, success = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_server_method())) || !TEST_ptr(peer = BIO_ADDR_new())) goto err; SSL_CTX_set_cookie_generate_cb(ctx, cookie_gen); SSL_CTX_set_cookie_verify_cb(ctx, cookie_verify); if (!TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(outbio = BIO_new(BIO_s_mem()))) goto err; SSL_set0_wbio(ssl, outbio); if (!TEST_ptr(inbio = BIO_new_mem_buf((char *)tp->in, tp->inlen))) goto err; BIO_set_mem_eof_return(inbio, -1); SSL_set0_rbio(ssl, inbio); if (!TEST_int_ge(ret = DTLSv1_listen(ssl, peer), 0)) goto err; datalen = BIO_get_mem_data(outbio, &data); if (tp->outtype == VERIFY) { if (!TEST_int_eq(ret, 0) || !TEST_mem_eq(data, datalen, verify, sizeof(verify))) goto err; } else if (datalen == 0) { if (!TEST_true((ret == 0 && tp->outtype == DROP) || (ret == 1 && tp->outtype == GOOD))) goto err; } else { TEST_info("Test %d: unexpected data output", i); goto err; } (void)BIO_reset(outbio); inbio = NULL; SSL_set0_rbio(ssl, NULL); success = 1; err: SSL_free(ssl); SSL_CTX_free(ctx); BIO_free(inbio); OPENSSL_free(peer); return success; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_SOCK ADD_ALL_TESTS(dtls_listen_test, (int)OSSL_NELEM(testpackets)); #endif return 1; }
test
openssl/test/dtlsv1listentest.c
openssl
#include "internal/deprecated.h" #include "internal/nelem.h" #include "testutil.h" #include <openssl/ec.h> #include "ec_local.h" #include <openssl/objects.h> static size_t crv_len = 0; static EC_builtin_curve *curves = NULL; static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx) { BIGNUM *a = NULL, *b = NULL, *c = NULL; int ret = 0; if (group->meth->field_inv == NULL || group->meth->field_mul == NULL) return 1; BN_CTX_start(ctx); a = BN_CTX_get(ctx); b = BN_CTX_get(ctx); if (!TEST_ptr(c = BN_CTX_get(ctx)) || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx)) || !TEST_true(BN_is_one(b)) || !TEST_true(BN_rand(a, BN_num_bits(group->field) - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) || !TEST_true(group->meth->field_inv(group, b, a, ctx)) || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, a, a, ctx))) || (group->meth->field_encode && !TEST_true(group->meth->field_encode(group, b, b, ctx))) || !TEST_true(group->meth->field_mul(group, c, a, b, ctx)) || (group->meth->field_decode && !TEST_true(group->meth->field_decode(group, c, c, ctx))) || !TEST_true(BN_is_one(c))) goto err; BN_zero(a); if (!TEST_false(group->meth->field_inv(group, b, a, ctx)) || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC) || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT) || !TEST_false(group->meth->field_inv(group, b, group->field, ctx)) || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC) || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) == EC_R_CANNOT_INVERT)) goto err; ERR_clear_error(); ret = 1; err: BN_CTX_end(ctx); return ret; } static int field_tests(const EC_METHOD *meth, const unsigned char *params, int len) { BN_CTX *ctx = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; EC_GROUP *group = NULL; int ret = 0; if (!TEST_ptr(ctx = BN_CTX_new())) return 0; BN_CTX_start(ctx); p = BN_CTX_get(ctx); a = BN_CTX_get(ctx); if (!TEST_ptr(b = BN_CTX_get(ctx)) || !TEST_ptr(group = EC_GROUP_new(meth)) || !TEST_true(BN_bin2bn(params, len, p)) || !TEST_true(BN_bin2bn(params + len, len, a)) || !TEST_true(BN_bin2bn(params + 2 * len, len, b)) || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx)) || !group_field_tests(group, ctx)) goto err; ret = 1; err: BN_CTX_end(ctx); BN_CTX_free(ctx); if (group != NULL) EC_GROUP_free(group); return ret; } static const unsigned char params_p256[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6, 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B }; #ifndef OPENSSL_NO_EC2M static const unsigned char params_b283[] = { 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A, 0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2, 0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5 }; #endif static int field_tests_ecp_simple(void) { TEST_info("Testing EC_GFp_simple_method()\n"); return field_tests(EC_GFp_simple_method(), params_p256, sizeof(params_p256) / 3); } static int field_tests_ecp_mont(void) { TEST_info("Testing EC_GFp_mont_method()\n"); return field_tests(EC_GFp_mont_method(), params_p256, sizeof(params_p256) / 3); } #ifndef OPENSSL_NO_EC2M static int field_tests_ec2_simple(void) { TEST_info("Testing EC_GF2m_simple_method()\n"); return field_tests(EC_GF2m_simple_method(), params_b283, sizeof(params_b283) / 3); } #endif static int field_tests_default(int n) { BN_CTX *ctx = NULL; EC_GROUP *group = NULL; int nid = curves[n].nid; int ret = 0; TEST_info("Testing curve %s\n", OBJ_nid2sn(nid)); if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)) || !TEST_ptr(ctx = BN_CTX_new()) || !group_field_tests(group, ctx)) goto err; ret = 1; err: if (group != NULL) EC_GROUP_free(group); if (ctx != NULL) BN_CTX_free(ctx); return ret; } #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 static int underflow_test(void) { BN_CTX *ctx = NULL; EC_GROUP *grp = NULL; EC_POINT *P = NULL, *Q = NULL, *R = NULL; BIGNUM *x1 = NULL, *y1 = NULL, *z1 = NULL, *x2 = NULL, *y2 = NULL; BIGNUM *k = NULL; int testresult = 0; const char *x1str = "1534f0077fffffe87e9adcfe000000000000000000003e05a21d2400002e031b1f4" "b80000c6fafa4f3c1288798d624a247b5e2ffffffffffffffefe099241900004"; const char *p521m1 = "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"; ctx = BN_CTX_new(); if (!TEST_ptr(ctx)) return 0; BN_CTX_start(ctx); x1 = BN_CTX_get(ctx); y1 = BN_CTX_get(ctx); z1 = BN_CTX_get(ctx); x2 = BN_CTX_get(ctx); y2 = BN_CTX_get(ctx); k = BN_CTX_get(ctx); if (!TEST_ptr(k)) goto err; grp = EC_GROUP_new_by_curve_name(NID_secp521r1); P = EC_POINT_new(grp); Q = EC_POINT_new(grp); R = EC_POINT_new(grp); if (!TEST_ptr(grp) || !TEST_ptr(P) || !TEST_ptr(Q) || !TEST_ptr(R)) goto err; if (!TEST_int_gt(BN_hex2bn(&x1, x1str), 0) || !TEST_int_gt(BN_hex2bn(&y1, p521m1), 0) || !TEST_int_gt(BN_hex2bn(&z1, p521m1), 0) || !TEST_int_gt(BN_hex2bn(&k, "02"), 0) || !TEST_true(ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(grp, P, x1, y1, z1, ctx)) || !TEST_true(EC_POINT_mul(grp, Q, NULL, P, k, ctx)) || !TEST_true(EC_POINT_get_affine_coordinates(grp, Q, x1, y1, ctx)) || !TEST_true(EC_POINT_dbl(grp, R, P, ctx)) || !TEST_true(EC_POINT_get_affine_coordinates(grp, R, x2, y2, ctx))) goto err; if (!TEST_int_eq(BN_cmp(x1, x2), 0) || !TEST_int_eq(BN_cmp(y1, y2), 0)) goto err; testresult = 1; err: BN_CTX_end(ctx); EC_POINT_free(P); EC_POINT_free(Q); EC_POINT_free(R); EC_GROUP_free(grp); BN_CTX_free(ctx); return testresult; } #endif static int set_private_key(void) { EC_KEY *key = NULL, *aux_key = NULL; int testresult = 0; key = EC_KEY_new_by_curve_name(NID_secp224r1); aux_key = EC_KEY_new_by_curve_name(NID_secp224r1); if (!TEST_ptr(key) || !TEST_ptr(aux_key) || !TEST_int_eq(EC_KEY_generate_key(key), 1) || !TEST_int_eq(EC_KEY_generate_key(aux_key), 1)) goto err; if (!TEST_int_eq(EC_KEY_set_private_key(key, aux_key->priv_key), 1)) goto err; if (!TEST_int_eq(EC_KEY_set_private_key(key, NULL), 0) || !TEST_ptr_null(key->priv_key)) goto err; testresult = 1; err: EC_KEY_free(key); EC_KEY_free(aux_key); return testresult; } static int decoded_flag_test(void) { EC_GROUP *grp; EC_GROUP *grp_copy = NULL; ECPARAMETERS *ecparams = NULL; ECPKPARAMETERS *ecpkparams = NULL; EC_KEY *key = NULL; unsigned char *encodedparams = NULL; const unsigned char *encp; int encodedlen; int testresult = 0; grp = EC_GROUP_new(EC_GFp_simple_method()); if (!TEST_ptr(grp) || !TEST_int_eq(grp->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp); grp = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); if (!TEST_ptr(grp) || !TEST_int_eq(grp->decoded_from_explicit_params, 0)) goto err; if (!TEST_ptr(ecparams = EC_GROUP_get_ecparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecparameters(ecparams)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; ECPARAMETERS_free(ecparams); ecparams = NULL; if (!TEST_int_eq(EC_GROUP_get_asn1_flag(grp), OPENSSL_EC_NAMED_CURVE) || !TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0) || !TEST_ptr(key = EC_KEY_new()) || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), -1) || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1) || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; ECPKPARAMETERS_free(ecpkparams); ecpkparams = NULL; if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0) || !TEST_ptr(encp = encodedparams) || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen)) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; OPENSSL_free(encodedparams); encodedparams = NULL; EC_GROUP_set_asn1_flag(grp, OPENSSL_EC_EXPLICIT_CURVE); if (!TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL)) || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)) goto err; EC_GROUP_free(grp_copy); grp_copy = NULL; if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0) || !TEST_ptr(encp = encodedparams) || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen)) || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE) || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 1) || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1) || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 1)) goto err; testresult = 1; err: EC_KEY_free(key); EC_GROUP_free(grp); EC_GROUP_free(grp_copy); ECPARAMETERS_free(ecparams); ECPKPARAMETERS_free(ecpkparams); OPENSSL_free(encodedparams); return testresult; } static int ecpkparams_i2d2i_test(int n) { EC_GROUP *g1 = NULL, *g2 = NULL; FILE *fp = NULL; int nid = curves[n].nid; int testresult = 0; if (!TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) goto end; if (!TEST_ptr(fp = fopen("params.der", "wb")) || !TEST_true(i2d_ECPKParameters_fp(fp, g1))) goto end; if (!TEST_int_eq(fclose(fp), 0)) { fp = NULL; goto end; } fp = NULL; if (!TEST_ptr(fp = fopen("params.der", "rb")) || !TEST_ptr(g2 = d2i_ECPKParameters_fp(fp, NULL))) goto end; testresult = 1; end: if (fp != NULL) fclose(fp); EC_GROUP_free(g1); EC_GROUP_free(g2); return testresult; } int setup_tests(void) { crv_len = EC_get_builtin_curves(NULL, 0); if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len)) || !TEST_true(EC_get_builtin_curves(curves, crv_len))) return 0; ADD_TEST(field_tests_ecp_simple); ADD_TEST(field_tests_ecp_mont); #ifndef OPENSSL_NO_EC2M ADD_TEST(field_tests_ec2_simple); #endif ADD_ALL_TESTS(field_tests_default, crv_len); #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 ADD_TEST(underflow_test); #endif ADD_TEST(set_private_key); ADD_TEST(decoded_flag_test); ADD_ALL_TESTS(ecpkparams_i2d2i_test, crv_len); return 1; } void cleanup_tests(void) { OPENSSL_free(curves); }
test
openssl/test/ec_internal_test.c
openssl
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "testutil.h" #include "internal/cryptlib.h" #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ) # define IS_X_86 1 size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len); size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len); #else # define IS_X_86 0 #endif #if defined(__aarch64__) && defined(OPENSSL_CPUID_OBJ) # define IS_AARCH_64 1 # include "arm_arch.h" size_t OPENSSL_rndr_bytes(unsigned char *buf, size_t len); size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len); #else # define IS_AARCH_64 0 #endif #if (IS_X_86 || IS_AARCH_64) static int sanity_check_bytes(size_t (*rng)(unsigned char *, size_t), int rounds, int min_failures, int max_retries, int max_zero_words) { int testresult = 0; unsigned char prior[31] = {0}, buf[31] = {0}, check[7]; int failures = 0, zero_words = 0; int i; for (i = 0; i < rounds; i++) { size_t generated = 0; int retry; for (retry = 0; retry < max_retries; retry++) { generated = rng(buf, sizeof(buf)); if (generated == sizeof(buf)) break; failures++; } size_t j; for (j = 0; j < sizeof(buf) - 1; j++) { if (buf[j] == 0 && buf[j+1] == 0) { zero_words++; } } if (!TEST_int_eq(generated, sizeof(buf))) goto end; if (!TEST_false(!memcmp(prior, buf, sizeof(buf)))) goto end; unsigned char *tail = &buf[sizeof(buf) - sizeof(check)]; memset(check, tail[0], 7); if (!TEST_false(!memcmp(check, tail, sizeof(check)))) goto end; memcpy(prior, buf, sizeof(buf)); } if (!TEST_int_le(zero_words, max_zero_words)) goto end; if (!TEST_int_ge(failures, min_failures)) goto end; testresult = 1; end: return testresult; } #endif #if IS_X_86 static int sanity_check_rdrand_bytes(void) { return sanity_check_bytes(OPENSSL_ia32_rdrand_bytes, 1000, 0, 10, 10); } static int sanity_check_rdseed_bytes(void) { return sanity_check_bytes(OPENSSL_ia32_rdseed_bytes, 1000, 1, 10000, 10); } #elif IS_AARCH_64 static int sanity_check_rndr_bytes(void) { return sanity_check_bytes(OPENSSL_rndr_bytes, 1000, 0, 10, 10); } static int sanity_check_rndrrs_bytes(void) { return sanity_check_bytes(OPENSSL_rndrrs_bytes, 1000, 0, 10000, 10); } #endif int setup_tests(void) { #if (IS_X_86 || IS_AARCH_64) OPENSSL_cpuid_setup(); # if IS_X_86 int have_rdseed = (OPENSSL_ia32cap_P[2] & (1 << 18)) != 0; int have_rdrand = (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0; if (have_rdrand) { ADD_TEST(sanity_check_rdrand_bytes); } if (have_rdseed) { ADD_TEST(sanity_check_rdseed_bytes); } # elif IS_AARCH_64 int have_rndr_rndrrs = (OPENSSL_armcap_P & (1 << 8)) != 0; if (have_rndr_rndrrs) { ADD_TEST(sanity_check_rndr_bytes); ADD_TEST(sanity_check_rndrrs_bytes); } # endif #endif return 1; }
test
openssl/test/rdcpu_sanitytest.c
openssl
#include "internal/packet.h" #include "internal/quic_txpim.h" #include "testutil.h" static int test_txpim(void) { int testresult = 0; QUIC_TXPIM *txpim; size_t i, j; QUIC_TXPIM_PKT *pkts[10] = {NULL}; QUIC_TXPIM_CHUNK chunks[3]; const QUIC_TXPIM_CHUNK *rchunks; if (!TEST_ptr(txpim = ossl_quic_txpim_new())) goto err; for (i = 0; i < OSSL_NELEM(pkts); ++i) { if (!TEST_ptr(pkts[i] = ossl_quic_txpim_pkt_alloc(txpim))) goto err; if (!TEST_size_t_eq(ossl_quic_txpim_pkt_get_num_chunks(pkts[i]), 0)) goto err; for (j = 0; j < OSSL_NELEM(chunks); ++j) { chunks[j].stream_id = 100 - j; chunks[j].start = 1000 * i + j * 10; chunks[j].end = chunks[j].start + 5; if (!TEST_true(ossl_quic_txpim_pkt_append_chunk(pkts[i], chunks + j))) goto err; } if (!TEST_size_t_eq(ossl_quic_txpim_pkt_get_num_chunks(pkts[i]), OSSL_NELEM(chunks))) goto err; rchunks = ossl_quic_txpim_pkt_get_chunks(pkts[i]); if (!TEST_uint64_t_eq(rchunks[0].stream_id, 98) || !TEST_uint64_t_eq(rchunks[1].stream_id, 99) || !TEST_uint64_t_eq(rchunks[2].stream_id, 100)) goto err; } testresult = 1; err: for (i = 0; i < OSSL_NELEM(pkts); ++i) if (txpim != NULL && pkts[i] != NULL) ossl_quic_txpim_pkt_release(txpim, pkts[i]); ossl_quic_txpim_free(txpim); return testresult; } int setup_tests(void) { ADD_TEST(test_txpim); return 1; }
test
openssl/test/quic_txpim_test.c
openssl
#include <string.h> #include <openssl/core.h> #include <openssl/provider.h> #include <openssl/crypto.h> #include "testutil.h" #include "filterprov.h" #define MAX_FILTERS 10 #define MAX_ALG_FILTERS 5 struct filter_prov_globals_st { OSSL_LIB_CTX *libctx; OSSL_PROVIDER *deflt; struct { int operation; OSSL_ALGORITHM alg[MAX_ALG_FILTERS + 1]; } dispatch[MAX_FILTERS]; int num_dispatch; int no_cache; unsigned long int query_count; int error; }; static struct filter_prov_globals_st ourglobals; static struct filter_prov_globals_st *get_globals(void) { return &ourglobals; } static OSSL_FUNC_provider_gettable_params_fn filter_gettable_params; static OSSL_FUNC_provider_get_params_fn filter_get_params; static OSSL_FUNC_provider_query_operation_fn filter_query; static OSSL_FUNC_provider_unquery_operation_fn filter_unquery; static OSSL_FUNC_provider_teardown_fn filter_teardown; static const OSSL_PARAM *filter_gettable_params(void *provctx) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_gettable_params(globs->deflt); } static int filter_get_params(void *provctx, OSSL_PARAM params[]) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_get_params(globs->deflt, params); } static int filter_get_capabilities(void *provctx, const char *capability, OSSL_CALLBACK *cb, void *arg) { struct filter_prov_globals_st *globs = get_globals(); return OSSL_PROVIDER_get_capabilities(globs->deflt, capability, cb, arg); } static const OSSL_ALGORITHM *filter_query(void *provctx, int operation_id, int *no_cache) { struct filter_prov_globals_st *globs = get_globals(); int i; globs->query_count++; for (i = 0; i < globs->num_dispatch; i++) { if (globs->dispatch[i].operation == operation_id) { *no_cache = globs->no_cache; return globs->dispatch[i].alg; } } return OSSL_PROVIDER_query_operation(globs->deflt, operation_id, no_cache); } static void filter_unquery(void *provctx, int operation_id, const OSSL_ALGORITHM *algs) { struct filter_prov_globals_st *globs = get_globals(); int i; if (!TEST_ulong_gt(globs->query_count, 0)) globs->error = 1; else globs->query_count--; for (i = 0; i < globs->num_dispatch; i++) if (globs->dispatch[i].alg == algs) return; OSSL_PROVIDER_unquery_operation(globs->deflt, operation_id, algs); } static void filter_teardown(void *provctx) { struct filter_prov_globals_st *globs = get_globals(); OSSL_PROVIDER_unload(globs->deflt); OSSL_LIB_CTX_free(globs->libctx); memset(globs, 0, sizeof(*globs)); } static const OSSL_DISPATCH filter_dispatch_table[] = { { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))filter_gettable_params }, { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))filter_get_params }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))filter_query }, { OSSL_FUNC_PROVIDER_UNQUERY_OPERATION, (void (*)(void))filter_unquery }, { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))filter_get_capabilities }, { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))filter_teardown }, OSSL_DISPATCH_END }; int filter_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { memset(&ourglobals, 0, sizeof(ourglobals)); ourglobals.libctx = OSSL_LIB_CTX_new(); if (ourglobals.libctx == NULL) goto err; ourglobals.deflt = OSSL_PROVIDER_load(ourglobals.libctx, "default"); if (ourglobals.deflt == NULL) goto err; *provctx = OSSL_PROVIDER_get0_provider_ctx(ourglobals.deflt); *out = filter_dispatch_table; return 1; err: OSSL_PROVIDER_unload(ourglobals.deflt); OSSL_LIB_CTX_free(ourglobals.libctx); return 0; } int filter_provider_set_filter(int operation, const char *filterstr) { int no_cache = 0; int algnum = 0, last = 0, ret = 0; struct filter_prov_globals_st *globs = get_globals(); size_t namelen; char *filterstrtmp = OPENSSL_strdup(filterstr); char *name, *sep; const OSSL_ALGORITHM *provalgs = OSSL_PROVIDER_query_operation(globs->deflt, operation, &no_cache); const OSSL_ALGORITHM *algs; if (filterstrtmp == NULL) goto err; if (provalgs == NULL) goto err; if (globs->num_dispatch >= MAX_FILTERS) goto err; for (name = filterstrtmp; !last; name = (sep == NULL ? NULL : sep + 1)) { sep = strstr(name, ":"); if (sep != NULL) *sep = '\0'; else last = 1; namelen = strlen(name); for (algs = provalgs; algs->algorithm_names != NULL; algs++) { const char *found = strstr(algs->algorithm_names, name); if (found == NULL) continue; if (found[namelen] != '\0' && found[namelen] != ':') continue; if (found != algs->algorithm_names && found[-1] != ':') continue; if (algnum >= MAX_ALG_FILTERS) goto err; globs->dispatch[globs->num_dispatch].alg[algnum++] = *algs; break; } if (algs->algorithm_names == NULL) { goto err; } } globs->dispatch[globs->num_dispatch].operation = operation; globs->no_cache = no_cache; globs->num_dispatch++; ret = 1; err: OSSL_PROVIDER_unquery_operation(globs->deflt, operation, provalgs); OPENSSL_free(filterstrtmp); return ret; } int filter_provider_check_clean_finish(void) { struct filter_prov_globals_st *globs = get_globals(); return TEST_ulong_eq(globs->query_count, 0) && !globs->error; }
test
openssl/test/filterprov.c
openssl
#include "internal/deprecated.h" #include <openssl/e_os2.h> #include <string.h> #include "testutil.h" #include "internal/nelem.h" #ifndef OPENSSL_NO_DES # include <openssl/des.h> # define TEST_cs_eq TEST_uint_eq # define DATA_BUF_SIZE 20 # define NUM_TESTS 34 static unsigned char key_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] = { {0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7}, {0x73, 0x59, 0xB2, 0x16, 0x3E, 0x4E, 0xDC, 0x58}, {0x95, 0x8E, 0x6E, 0x62, 0x7A, 0x05, 0x55, 0x7B}, {0xF4, 0x03, 0x79, 0xAB, 0x9E, 0x0E, 0xC5, 0x33}, {0x17, 0x66, 0x8D, 0xFC, 0x72, 0x92, 0x53, 0x2D}, {0x8A, 0x5A, 0xE1, 0xF8, 0x1A, 0xB8, 0xF2, 0xDD}, {0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7}, {0xED, 0x39, 0xD9, 0x50, 0xFA, 0x74, 0xBC, 0xC4}, {0x69, 0x0F, 0x5B, 0x0D, 0x9A, 0x26, 0x93, 0x9B}, {0x7A, 0x38, 0x9D, 0x10, 0x35, 0x4B, 0xD2, 0x71}, {0x86, 0x8E, 0xBB, 0x51, 0xCA, 0xB4, 0x59, 0x9A}, {0x71, 0x78, 0x87, 0x6E, 0x01, 0xF1, 0x9B, 0x2A}, {0xAF, 0x37, 0xFB, 0x42, 0x1F, 0x8C, 0x40, 0x95}, {0x86, 0xA5, 0x60, 0xF1, 0x0E, 0xC6, 0xD8, 0x5B}, {0x0C, 0xD3, 0xDA, 0x02, 0x00, 0x21, 0xDC, 0x09}, {0xEA, 0x67, 0x6B, 0x2C, 0xB7, 0xDB, 0x2B, 0x7A}, {0xDF, 0xD6, 0x4A, 0x81, 0x5C, 0xAF, 0x1A, 0x0F}, {0x5C, 0x51, 0x3C, 0x9C, 0x48, 0x86, 0xC0, 0x88}, {0x0A, 0x2A, 0xEE, 0xAE, 0x3F, 0xF4, 0xAB, 0x77}, {0xEF, 0x1B, 0xF0, 0x3E, 0x5D, 0xFA, 0x57, 0x5A}, {0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56}, {0xA1, 0xF9, 0x91, 0x55, 0x41, 0x02, 0x0B, 0x56}, {0x6F, 0xBF, 0x1C, 0xAF, 0xCF, 0xFD, 0x05, 0x56}, {0x2F, 0x22, 0xE4, 0x9B, 0xAB, 0x7C, 0xA1, 0xAC}, {0x5A, 0x6B, 0x61, 0x2C, 0xC2, 0x6C, 0xCE, 0x4A}, {0x5F, 0x4C, 0x03, 0x8E, 0xD1, 0x2B, 0x2E, 0x41}, {0x63, 0xFA, 0xC0, 0xD0, 0x34, 0xD9, 0xF7, 0x93}, {0x61, 0x7B, 0x3A, 0x0C, 0xE8, 0xF0, 0x71, 0x00}, {0xDB, 0x95, 0x86, 0x05, 0xF8, 0xC8, 0xC6, 0x06}, {0xED, 0xBF, 0xD1, 0xC6, 0x6C, 0x29, 0xCC, 0xC7}, {0x35, 0x55, 0x50, 0xB2, 0x15, 0x0E, 0x24, 0x51}, {0xCA, 0xAA, 0xAF, 0x4D, 0xEA, 0xF1, 0xDB, 0xAE}, {0xD5, 0xD4, 0x4F, 0xF7, 0x20, 0x68, 0x3D, 0x0D}, {0x2A, 0x2B, 0xB0, 0x08, 0xDF, 0x97, 0xC2, 0xF2} }; static unsigned char cipher_ecb2[NUM_TESTS - 1][8] = { {0x92, 0x95, 0xB5, 0x9B, 0xB3, 0x84, 0x73, 0x6E}, {0x19, 0x9E, 0x9D, 0x6D, 0xF3, 0x9A, 0xA8, 0x16}, {0x2A, 0x4B, 0x4D, 0x24, 0x52, 0x43, 0x84, 0x27}, {0x35, 0x84, 0x3C, 0x01, 0x9D, 0x18, 0xC5, 0xB6}, {0x4A, 0x5B, 0x2F, 0x42, 0xAA, 0x77, 0x19, 0x25}, {0xA0, 0x6B, 0xA9, 0xB8, 0xCA, 0x5B, 0x17, 0x8A}, {0xAB, 0x9D, 0xB7, 0xFB, 0xED, 0x95, 0xF2, 0x74}, {0x3D, 0x25, 0x6C, 0x23, 0xA7, 0x25, 0x2F, 0xD6}, {0xB7, 0x6F, 0xAB, 0x4F, 0xBD, 0xBD, 0xB7, 0x67}, {0x8F, 0x68, 0x27, 0xD6, 0x9C, 0xF4, 0x1A, 0x10}, {0x82, 0x57, 0xA1, 0xD6, 0x50, 0x5E, 0x81, 0x85}, {0xA2, 0x0F, 0x0A, 0xCD, 0x80, 0x89, 0x7D, 0xFA}, {0xCD, 0x2A, 0x53, 0x3A, 0xDB, 0x0D, 0x7E, 0xF3}, {0xD2, 0xC2, 0xBE, 0x27, 0xE8, 0x1B, 0x68, 0xE3}, {0xE9, 0x24, 0xCF, 0x4F, 0x89, 0x3C, 0x5B, 0x0A}, {0xA7, 0x18, 0xC3, 0x9F, 0xFA, 0x9F, 0xD7, 0x69}, {0x77, 0x2C, 0x79, 0xB1, 0xD2, 0x31, 0x7E, 0xB1}, {0x49, 0xAB, 0x92, 0x7F, 0xD0, 0x22, 0x00, 0xB7}, {0xCE, 0x1C, 0x6C, 0x7D, 0x85, 0xE3, 0x4A, 0x6F}, {0xBE, 0x91, 0xD6, 0xE1, 0x27, 0xB2, 0xE9, 0x87}, {0x70, 0x28, 0xAE, 0x8F, 0xD1, 0xF5, 0x74, 0x1A}, {0xAA, 0x37, 0x80, 0xBB, 0xF3, 0x22, 0x1D, 0xDE}, {0xA6, 0xC4, 0xD2, 0x5E, 0x28, 0x93, 0xAC, 0xB3}, {0x22, 0x07, 0x81, 0x5A, 0xE4, 0xB7, 0x1A, 0xAD}, {0xDC, 0xCE, 0x05, 0xE7, 0x07, 0xBD, 0xF5, 0x84}, {0x26, 0x1D, 0x39, 0x2C, 0xB3, 0xBA, 0xA5, 0x85}, {0xB4, 0xF7, 0x0F, 0x72, 0xFB, 0x04, 0xF0, 0xDC}, {0x95, 0xBA, 0xA9, 0x4E, 0x87, 0x36, 0xF2, 0x89}, {0xD4, 0x07, 0x3A, 0xF1, 0x5A, 0x17, 0x82, 0x0E}, {0xEF, 0x6F, 0xAF, 0xA7, 0x66, 0x1A, 0x7E, 0x89}, {0xC1, 0x97, 0xF5, 0x58, 0x74, 0x8A, 0x20, 0xE7}, {0x43, 0x34, 0xCF, 0xDA, 0x22, 0xC4, 0x86, 0xC8}, {0x08, 0xD7, 0xB4, 0xFB, 0x62, 0x9D, 0x08, 0x85} }; static unsigned char cbc_key[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; static unsigned char cbc2_key[8] = { 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86 }; static unsigned char cbc3_key[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; static unsigned char cbc_iv[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; static unsigned char cbc_data[40] = { 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20, 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; static unsigned char cbc_ok[32] = { 0xcc, 0xd1, 0x73, 0xff, 0xab, 0x20, 0x39, 0xf4, 0xac, 0xd8, 0xae, 0xfd, 0xdf, 0xd8, 0xa1, 0xeb, 0x46, 0x8e, 0x91, 0x15, 0x78, 0x88, 0xba, 0x68, 0x1d, 0x26, 0x93, 0x97, 0xf7, 0xfe, 0x62, 0xb4 }; # ifdef SCREW_THE_PARITY # error "SCREW_THE_PARITY is not meant to be defined." # error "Original vectors are preserved for reference only." static unsigned char cbc2_key[8] = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }; static unsigned char xcbc_ok[32] = { 0x86, 0x74, 0x81, 0x0D, 0x61, 0xA4, 0xA5, 0x48, 0xB9, 0x93, 0x03, 0xE1, 0xB8, 0xBB, 0xBD, 0xBD, 0x64, 0x30, 0x0B, 0xB9, 0x06, 0x65, 0x81, 0x76, 0x04, 0x1D, 0x77, 0x62, 0x17, 0xCA, 0x2B, 0xD2, }; # else static unsigned char xcbc_ok[32] = { 0x84, 0x6B, 0x29, 0x14, 0x85, 0x1E, 0x9A, 0x29, 0x54, 0x73, 0x2F, 0x8A, 0xA0, 0xA6, 0x11, 0xC1, 0x15, 0xCD, 0xC2, 0xD7, 0x95, 0x1B, 0x10, 0x53, 0xA6, 0x3C, 0x5E, 0x03, 0xB2, 0x1A, 0xA3, 0xC4, }; # endif static unsigned char cbc3_ok[32] = { 0x3F, 0xE3, 0x01, 0xC9, 0x62, 0xAC, 0x01, 0xD0, 0x22, 0x13, 0x76, 0x3C, 0x1C, 0xBD, 0x4C, 0xDC, 0x79, 0x96, 0x57, 0xC0, 0x64, 0xEC, 0xF5, 0xD4, 0x1C, 0x67, 0x38, 0x12, 0xCF, 0xDE, 0x96, 0x75 }; static unsigned char pcbc_ok[32] = { 0xcc, 0xd1, 0x73, 0xff, 0xab, 0x20, 0x39, 0xf4, 0x6d, 0xec, 0xb4, 0x70, 0xa0, 0xe5, 0x6b, 0x15, 0xae, 0xa6, 0xbf, 0x61, 0xed, 0x7d, 0x9c, 0x9f, 0xf7, 0x17, 0x46, 0x3b, 0x8a, 0xb3, 0xcc, 0x88 }; static unsigned char cfb_key[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; static unsigned char cfb_iv[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; static unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8]; static unsigned char plain[24] = { 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20 }; static unsigned char cfb_cipher8[24] = { 0xf3, 0x1f, 0xda, 0x07, 0x01, 0x14, 0x62, 0xee, 0x18, 0x7f, 0x43, 0xd8, 0x0a, 0x7c, 0xd9, 0xb5, 0xb0, 0xd2, 0x90, 0xda, 0x6e, 0x5b, 0x9a, 0x87 }; static unsigned char cfb_cipher16[24] = { 0xF3, 0x09, 0x87, 0x87, 0x7F, 0x57, 0xF7, 0x3C, 0x36, 0xB6, 0xDB, 0x70, 0xD8, 0xD5, 0x34, 0x19, 0xD3, 0x86, 0xB2, 0x23, 0xB7, 0xB2, 0xAD, 0x1B }; static unsigned char cfb_cipher32[24] = { 0xF3, 0x09, 0x62, 0x49, 0xA4, 0xDF, 0xA4, 0x9F, 0x33, 0xDC, 0x7B, 0xAD, 0x4C, 0xC8, 0x9F, 0x64, 0xE4, 0x53, 0xE5, 0xEC, 0x67, 0x20, 0xDA, 0xB6 }; static unsigned char cfb_cipher48[24] = { 0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x30, 0xB5, 0x15, 0xEC, 0xBB, 0x85, 0x97, 0x5A, 0x13, 0x8C, 0x68, 0x60, 0xE2, 0x38, 0x34, 0x3C, 0xDC, 0x1F }; static unsigned char cfb_cipher64[24] = { 0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51, 0xA6, 0x9E, 0x83, 0x9B, 0x1A, 0x92, 0xF7, 0x84, 0x03, 0x46, 0x71, 0x33, 0x89, 0x8E, 0xA6, 0x22 }; static unsigned char ofb_key[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; static unsigned char ofb_iv[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; static unsigned char ofb_buf1[24], ofb_buf2[24], ofb_tmp[8]; static unsigned char ofb_cipher[24] = { 0xf3, 0x09, 0x62, 0x49, 0xc7, 0xf4, 0x6e, 0x51, 0x35, 0xf2, 0x4a, 0x24, 0x2e, 0xeb, 0x3d, 0x3f, 0x3d, 0x6d, 0x5b, 0xe3, 0x25, 0x5a, 0xf8, 0xc3 }; static DES_LONG cbc_cksum_ret = 0xF7FE62B4L; static unsigned char cbc_cksum_data[8] = { 0x1D, 0x26, 0x93, 0x97, 0xf7, 0xfe, 0x62, 0xb4 }; static char *pt(const unsigned char *p, char buf[DATA_BUF_SIZE]) { char *ret; int i; static const char *f = "0123456789ABCDEF"; ret = &(buf[0]); for (i = 0; i < 8; i++) { ret[i * 2] = f[(p[i] >> 4) & 0xf]; ret[i * 2 + 1] = f[p[i] & 0xf]; } ret[16] = '\0'; return ret; } static int test_des_ecb(int i) { DES_key_schedule ks; DES_cblock in, out, outin; char b1[DATA_BUF_SIZE], b2[DATA_BUF_SIZE]; DES_set_key_unchecked(&key_data[i], &ks); memcpy(in, plain_data[i], 8); memset(out, 0, 8); memset(outin, 0, 8); DES_ecb_encrypt(&in, &out, &ks, DES_ENCRYPT); DES_ecb_encrypt(&out, &outin, &ks, DES_DECRYPT); if (!TEST_mem_eq(out, 8, cipher_data[i], 8)) { TEST_info("Encryption error %2d k=%s p=%s", i + 1, pt(key_data[i], b1), pt(in, b2)); return 0; } if (!TEST_mem_eq(in, 8, outin, 8)) { TEST_info("Decryption error %2d k=%s p=%s", i + 1, pt(key_data[i], b1), pt(out, b2)); return 0; } return 1; } static int test_des_ede_ecb(int i) { DES_cblock in, out, outin; DES_key_schedule ks, ks2, ks3; char b1[DATA_BUF_SIZE], b2[DATA_BUF_SIZE]; DES_set_key_unchecked(&key_data[i], &ks); DES_set_key_unchecked(&key_data[i + 1], &ks2); DES_set_key_unchecked(&key_data[i + 2], &ks3); memcpy(in, plain_data[i], 8); memset(out, 0, 8); memset(outin, 0, 8); DES_ecb3_encrypt(&in, &out, &ks, &ks2, &ks, DES_ENCRYPT); DES_ecb3_encrypt(&out, &outin, &ks, &ks2, &ks, DES_DECRYPT); if (!TEST_mem_eq(out, 8, cipher_ecb2[i], 8)) { TEST_info("Encryption error %2d k=%s p=%s", i + 1, pt(key_data[i], b1), pt(in, b2)); return 0; } if (!TEST_mem_eq(in, 8, outin, 8)) { TEST_info("Decryption error %2d k=%s p=%s ", i + 1, pt(key_data[i], b1), pt(out, b2)); return 0; } return 1; } static int test_des_cbc(void) { unsigned char cbc_in[40]; unsigned char cbc_out[40]; DES_cblock iv3; DES_key_schedule ks; const size_t cbc_data_len = strlen((char *)cbc_data); if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; memset(cbc_out, 0, sizeof(cbc_out)); memset(cbc_in, 0, sizeof(cbc_in)); memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_ncbc_encrypt(cbc_data, cbc_out, cbc_data_len + 1, &ks, &iv3, DES_ENCRYPT); if (!TEST_mem_eq(cbc_out, 32, cbc_ok, 32)) return 0; memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_ncbc_encrypt(cbc_out, cbc_in, cbc_data_len + 1, &ks, &iv3, DES_DECRYPT); return TEST_mem_eq(cbc_in, cbc_data_len, cbc_data, cbc_data_len); } static int test_des_ede_cbc(void) { DES_cblock iv3; DES_key_schedule ks; unsigned char cbc_in[40]; unsigned char cbc_out[40]; const size_t n = strlen((char *)cbc_data) + 1; if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; memset(cbc_out, 0, sizeof(cbc_out)); memset(cbc_in, 0, sizeof(cbc_in)); memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_xcbc_encrypt(cbc_data, cbc_out, n, &ks, &iv3, &cbc2_key, &cbc3_key, DES_ENCRYPT); if (!TEST_mem_eq(cbc_out, sizeof(xcbc_ok), xcbc_ok, sizeof(xcbc_ok))) return 0; memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_xcbc_encrypt(cbc_out, cbc_in, n, &ks, &iv3, &cbc2_key, &cbc3_key, DES_DECRYPT); return TEST_mem_eq(cbc_data, n, cbc_data, n); } static int test_ede_cbc(void) { DES_cblock iv3; DES_key_schedule ks, ks2, ks3; unsigned char cbc_in[40]; unsigned char cbc_out[40]; const size_t i = strlen((char *)cbc_data) + 1; const size_t n = (i + 7) / 8 * 8; if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; if (!TEST_int_eq(DES_set_key_checked(&cbc2_key, &ks2), 0)) return 0; if (!TEST_int_eq(DES_set_key_checked(&cbc3_key, &ks3), 0)) return 0; memset(cbc_out, 0, sizeof(cbc_out)); memset(cbc_in, 0, sizeof(cbc_in)); memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_data, cbc_out, 16L, &ks, &ks2, &ks3, &iv3, DES_ENCRYPT); DES_ede3_cbc_encrypt(&cbc_data[16], &cbc_out[16], i - 16, &ks, &ks2, &ks3, &iv3, DES_ENCRYPT); if (!TEST_mem_eq(cbc_out, n, cbc3_ok, n)) return 0; memcpy(iv3, cbc_iv, sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_out, cbc_in, i, &ks, &ks2, &ks3, &iv3, DES_DECRYPT); return TEST_mem_eq(cbc_in, i, cbc_data, i); } static int test_input_align(int i) { unsigned char cbc_out[40]; DES_cblock iv; DES_key_schedule ks; const size_t n = strlen(i + (char *)cbc_data) + 1; memset(cbc_out, 0, sizeof(cbc_out)); memcpy(iv, cbc_iv, sizeof(cbc_iv)); if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; DES_ncbc_encrypt(&cbc_data[i], cbc_out, n, &ks, &iv, DES_ENCRYPT); return 1; } static int test_output_align(int i) { unsigned char cbc_out[40]; DES_cblock iv; DES_key_schedule ks; const size_t n = strlen((char *)cbc_data) + 1; memset(cbc_out, 0, sizeof(cbc_out)); memcpy(iv, cbc_iv, sizeof(cbc_iv)); if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; DES_ncbc_encrypt(cbc_data, &cbc_out[i], n, &ks, &iv, DES_ENCRYPT); return 1; } static int test_des_crypt(void) { if (!TEST_str_eq("efGnQx2725bI2", DES_crypt("testing", "ef"))) return 0; if (!TEST_str_eq("yA1Rp/1hZXIJk", DES_crypt("bca76;23", "yA"))) return 0; if (!TEST_ptr_null(DES_crypt("testing", "y\202"))) return 0; if (!TEST_ptr_null(DES_crypt("testing", "\0A"))) return 0; if (!TEST_ptr_null(DES_crypt("testing", "A"))) return 0; return 1; } static int test_des_pcbc(void) { unsigned char cbc_in[40]; unsigned char cbc_out[40]; DES_key_schedule ks; const int n = strlen((char *)cbc_data) + 1; if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0)) return 0; memset(cbc_out, 0, sizeof(cbc_out)); memset(cbc_in, 0, sizeof(cbc_in)); DES_pcbc_encrypt(cbc_data, cbc_out, n, &ks, &cbc_iv, DES_ENCRYPT); if (!TEST_mem_eq(cbc_out, sizeof(pcbc_ok), pcbc_ok, sizeof(pcbc_ok))) return 0; DES_pcbc_encrypt(cbc_out, cbc_in, n, &ks, &cbc_iv, DES_DECRYPT); return TEST_mem_eq(cbc_in, n, cbc_data, n); } static int cfb_test(int bits, unsigned char *cfb_cipher) { DES_key_schedule ks; DES_set_key_checked(&cfb_key, &ks); memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); DES_cfb_encrypt(plain, cfb_buf1, bits, sizeof(plain), &ks, &cfb_tmp, DES_ENCRYPT); if (!TEST_mem_eq(cfb_cipher, sizeof(plain), cfb_buf1, sizeof(plain))) return 0; memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); DES_cfb_encrypt(cfb_buf1, cfb_buf2, bits, sizeof(plain), &ks, &cfb_tmp, DES_DECRYPT); return TEST_mem_eq(plain, sizeof(plain), cfb_buf2, sizeof(plain)); } static int test_des_cfb8(void) { return cfb_test(8, cfb_cipher8); } static int test_des_cfb16(void) { return cfb_test(16, cfb_cipher16); } static int test_des_cfb32(void) { return cfb_test(32, cfb_cipher32); } static int test_des_cfb48(void) { return cfb_test(48, cfb_cipher48); } static int test_des_cfb64(void) { DES_key_schedule ks; int n; size_t i; if (!cfb_test(64, cfb_cipher64)) return 0; DES_set_key_checked(&cfb_key, &ks); memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); n = 0; DES_cfb64_encrypt(plain, cfb_buf1, 12, &ks, &cfb_tmp, &n, DES_ENCRYPT); DES_cfb64_encrypt(&plain[12], &cfb_buf1[12], sizeof(plain) - 12, &ks, &cfb_tmp, &n, DES_ENCRYPT); if (!TEST_mem_eq(cfb_cipher64, sizeof(plain), cfb_buf1, sizeof(plain))) return 0; memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); n = 0; DES_cfb64_encrypt(cfb_buf1, cfb_buf2, 17, &ks, &cfb_tmp, &n, DES_DECRYPT); DES_cfb64_encrypt(&cfb_buf1[17], &cfb_buf2[17], sizeof(plain) - 17, &ks, &cfb_tmp, &n, DES_DECRYPT); if (!TEST_mem_eq(plain, sizeof(plain), cfb_buf2, sizeof(plain))) return 0; memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); for (i = 0; i < sizeof(plain); i++) DES_cfb_encrypt(&plain[i], &cfb_buf1[i], 8, 1, &ks, &cfb_tmp, DES_ENCRYPT); if (!TEST_mem_eq(cfb_cipher8, sizeof(plain), cfb_buf1, sizeof(plain))) return 0; memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); for (i = 0; i < sizeof(plain); i++) DES_cfb_encrypt(&cfb_buf1[i], &cfb_buf2[i], 8, 1, &ks, &cfb_tmp, DES_DECRYPT); return TEST_mem_eq(plain, sizeof(plain), cfb_buf2, sizeof(plain)); } static int test_des_ede_cfb64(void) { DES_key_schedule ks; int n; DES_set_key_checked(&cfb_key, &ks); memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); n = 0; DES_ede3_cfb64_encrypt(plain, cfb_buf1, 12, &ks, &ks, &ks, &cfb_tmp, &n, DES_ENCRYPT); DES_ede3_cfb64_encrypt(&plain[12], &cfb_buf1[12], sizeof(plain) - 12, &ks, &ks, &ks, &cfb_tmp, &n, DES_ENCRYPT); if (!TEST_mem_eq(cfb_cipher64, sizeof(plain), cfb_buf1, sizeof(plain))) return 0; memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv)); n = 0; DES_ede3_cfb64_encrypt(cfb_buf1, cfb_buf2, (long)17, &ks, &ks, &ks, &cfb_tmp, &n, DES_DECRYPT); DES_ede3_cfb64_encrypt(&cfb_buf1[17], &cfb_buf2[17], sizeof(plain) - 17, &ks, &ks, &ks, &cfb_tmp, &n, DES_DECRYPT); return TEST_mem_eq(plain, sizeof(plain), cfb_buf2, sizeof(plain)); } static int test_des_ofb(void) { DES_key_schedule ks; DES_set_key_checked(&ofb_key, &ks); memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); DES_ofb_encrypt(plain, ofb_buf1, 64, sizeof(plain) / 8, &ks, &ofb_tmp); if (!TEST_mem_eq(ofb_cipher, sizeof(ofb_buf1), ofb_buf1, sizeof(ofb_buf1))) return 0; memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); DES_ofb_encrypt(ofb_buf1, ofb_buf2, 64, sizeof(ofb_buf1) / 8, &ks, &ofb_tmp); return TEST_mem_eq(plain, sizeof(ofb_buf2), ofb_buf2, sizeof(ofb_buf2)); } static int test_des_ofb64(void) { DES_key_schedule ks; int num; size_t i; DES_set_key_checked(&ofb_key, &ks); memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); memset(ofb_buf1, 0, sizeof(ofb_buf1)); memset(ofb_buf2, 0, sizeof(ofb_buf1)); num = 0; for (i = 0; i < sizeof(plain); i++) { DES_ofb64_encrypt(&plain[i], &ofb_buf1[i], 1, &ks, &ofb_tmp, &num); } if (!TEST_mem_eq(ofb_cipher, sizeof(ofb_buf1), ofb_buf1, sizeof(ofb_buf1))) return 0; memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); num = 0; DES_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ofb_tmp, &num); return TEST_mem_eq(plain, sizeof(ofb_buf2), ofb_buf2, sizeof(ofb_buf2)); } static int test_des_ede_ofb64(void) { DES_key_schedule ks; int num; size_t i; DES_set_key_checked(&ofb_key, &ks); memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); memset(ofb_buf1, 0, sizeof(ofb_buf1)); memset(ofb_buf2, 0, sizeof(ofb_buf1)); num = 0; for (i = 0; i < sizeof(plain); i++) { DES_ede3_ofb64_encrypt(&plain[i], &ofb_buf1[i], 1, &ks, &ks, &ks, &ofb_tmp, &num); } if (!TEST_mem_eq(ofb_cipher, sizeof(ofb_buf1), ofb_buf1, sizeof(ofb_buf1))) return 0; memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv)); num = 0; DES_ede3_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ks, &ks, &ofb_tmp, &num); return TEST_mem_eq(plain, sizeof(ofb_buf2), ofb_buf2, sizeof(ofb_buf2)); } static int test_des_cbc_cksum(void) { DES_LONG cs; DES_key_schedule ks; unsigned char cret[8]; DES_set_key_checked(&cbc_key, &ks); cs = DES_cbc_cksum(cbc_data, &cret, strlen((char *)cbc_data), &ks, &cbc_iv); if (!TEST_cs_eq(cs, cbc_cksum_ret)) return 0; return TEST_mem_eq(cret, 8, cbc_cksum_data, 8); } static int test_des_quad_cksum(void) { DES_LONG cs, lqret[4]; cs = DES_quad_cksum(cbc_data, (DES_cblock *)lqret, (long)strlen((char *)cbc_data), 2, (DES_cblock *)cbc_iv); if (!TEST_cs_eq(cs, 0x70d7a63aL)) return 0; if (!TEST_cs_eq(lqret[0], 0x327eba8dL)) return 0; if (!TEST_cs_eq(lqret[1], 0x201a49ccL)) return 0; if (!TEST_cs_eq(lqret[2], 0x70d7a63aL)) return 0; if (!TEST_cs_eq(lqret[3], 0x501c2c26L)) return 0; return 1; } static const int test_des_key_wrap_sizes[] = { 8, 16, 24, 32, 64, 80 }; static int test_des_key_wrap(int idx) { int in_bytes = test_des_key_wrap_sizes[idx]; unsigned char in[100], c_txt[200], p_txt[200], key[24]; int clen, clen_upd, clen_fin, plen, plen_upd, plen_fin, expect, bs, i; EVP_CIPHER *cipher = NULL; EVP_CIPHER_CTX *ctx = NULL; int res = 0; if (!TEST_size_t_le(in_bytes, sizeof(in)) || !TEST_ptr(cipher = EVP_CIPHER_fetch(NULL, "DES3-WRAP", NULL)) || !TEST_int_eq(bs = EVP_CIPHER_get_block_size(cipher), 8) || !TEST_size_t_eq(bs * 3u, sizeof(key)) || !TEST_true(in_bytes % bs == 0) || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; for (i = 0; i < in_bytes; i++) in[i] = test_random(); memcpy(key, cbc_key, sizeof(cbc_key)); memcpy(key + sizeof(cbc_key), cbc2_key, sizeof(cbc2_key)); memcpy(key + sizeof(cbc_key) + sizeof(cbc3_key), cbc_key, sizeof(cbc3_key)); clen_upd = sizeof(c_txt); if (!TEST_true(EVP_EncryptInit(ctx, cipher, key, NULL)) || !TEST_true(EVP_EncryptUpdate(ctx, c_txt, &clen_upd, in, in_bytes))) goto err; expect = (in_bytes + (bs - 1)) / bs * bs + 2 * bs; if (!TEST_int_eq(clen_upd, expect)) goto err; clen_fin = sizeof(c_txt) - clen_upd; if (!TEST_true(EVP_EncryptFinal(ctx, c_txt + clen_upd, &clen_fin)) || !TEST_int_eq(clen_fin, 0)) goto err; clen = clen_upd + clen_fin; plen_upd = sizeof(p_txt); if (!TEST_true(EVP_DecryptInit(ctx, cipher, key, NULL)) || !TEST_true(EVP_DecryptUpdate(ctx, p_txt, &plen_upd, c_txt, clen))) goto err; plen_fin = sizeof(p_txt) - plen_upd; if (!TEST_true(EVP_DecryptFinal(ctx, p_txt + plen_upd, &plen_fin))) goto err; plen = plen_upd + plen_fin; if (!TEST_mem_eq(in, in_bytes, p_txt, plen)) goto err; res = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return res; } static struct { const DES_cblock key; int expect; } weak_keys[] = { {{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, 1 }, {{0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE}, 1 }, {{0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E}, 1 }, {{0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1}, 1 }, {{0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE}, 1 }, {{0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01}, 1 }, {{0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1}, 1 }, {{0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E}, 1 }, {{0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1}, 1 }, {{0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01}, 1 }, {{0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE}, 1 }, {{0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E}, 1 }, {{0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E}, 1 }, {{0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01}, 1 }, {{0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE}, 1 }, {{0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1}, 1 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, 0 } }; static int test_des_weak_keys(int n) { const_DES_cblock *key = (unsigned char (*)[8])weak_keys[n].key; return TEST_int_eq(DES_is_weak_key(key), weak_keys[n].expect); } static struct { const DES_cblock key; int expect; } bad_parity_keys[] = { {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0 }, {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 0 }, {{0x48, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, 0 }, {{0x49, 0xE8, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, 0 }, {{0x49, 0xE9, 0x5C, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, 0 }, {{0x49, 0xE9, 0x5D, 0x7D, 0x4C, 0xA2, 0x29, 0xBF}, 0 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x5C, 0xA2, 0x29, 0xBF}, 0 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA3, 0x29, 0xBF}, 0 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x39, 0xBF}, 0 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBE}, 0 }, {{0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF}, 1 } }; static int test_des_check_bad_parity(int n) { const_DES_cblock *key = (unsigned char (*)[8])bad_parity_keys[n].key; return TEST_int_eq(DES_check_key_parity(key), bad_parity_keys[n].expect); } static int test_des_two_key(void) { int res = 0; EVP_CIPHER *cipher = NULL; EVP_CIPHER_CTX *ctx = NULL; unsigned char key[16]; if (!TEST_ptr(cipher = EVP_CIPHER_fetch(NULL, "DES-EDE-ECB", NULL)) || !TEST_ptr(ctx = EVP_CIPHER_CTX_new()) || !EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) || !EVP_CIPHER_CTX_set_key_length(ctx, sizeof(key)) || !EVP_CIPHER_CTX_rand_key(ctx, key)) goto err; res = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return res; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_DES ADD_ALL_TESTS(test_des_ecb, NUM_TESTS); ADD_TEST(test_des_cbc); ADD_TEST(test_ede_cbc); ADD_ALL_TESTS(test_des_ede_ecb, NUM_TESTS - 2); ADD_TEST(test_des_ede_cbc); ADD_TEST(test_des_pcbc); ADD_TEST(test_des_cfb8); ADD_TEST(test_des_cfb16); ADD_TEST(test_des_cfb32); ADD_TEST(test_des_cfb48); ADD_TEST(test_des_cfb64); ADD_TEST(test_des_ede_cfb64); ADD_TEST(test_des_ofb); ADD_TEST(test_des_ofb64); ADD_TEST(test_des_ede_ofb64); ADD_TEST(test_des_cbc_cksum); ADD_TEST(test_des_quad_cksum); ADD_TEST(test_des_crypt); ADD_ALL_TESTS(test_input_align, 4); ADD_ALL_TESTS(test_output_align, 4); ADD_ALL_TESTS(test_des_key_wrap, OSSL_NELEM(test_des_key_wrap_sizes)); ADD_ALL_TESTS(test_des_weak_keys, OSSL_NELEM(weak_keys)); ADD_ALL_TESTS(test_des_check_bad_parity, OSSL_NELEM(bad_parity_keys)); ADD_TEST(test_des_two_key); #endif return 1; }
test
openssl/test/destest.c
openssl
#include <openssl/evp.h> #include <openssl/rand.h> #include <openssl/core_names.h> #include "testutil.h" #include "internal/nelem.h" static const unsigned char shake256_input[] = { 0x8d, 0x80, 0x01, 0xe2, 0xc0, 0x96, 0xf1, 0xb8, 0x8e, 0x7c, 0x92, 0x24, 0xa0, 0x86, 0xef, 0xd4, 0x79, 0x7f, 0xbf, 0x74, 0xa8, 0x03, 0x3a, 0x2d, 0x42, 0x2a, 0x2b, 0x6b, 0x8f, 0x67, 0x47, 0xe4 }; static const unsigned char shake256_output[] = { 0x2e, 0x97, 0x5f, 0x6a, 0x8a, 0x14, 0xf0, 0x70, 0x4d, 0x51, 0xb1, 0x36, 0x67, 0xd8, 0x19, 0x5c, 0x21, 0x9f, 0x71, 0xe6, 0x34, 0x56, 0x96, 0xc4, 0x9f, 0xa4, 0xb9, 0xd0, 0x8e, 0x92, 0x25, 0xd3, 0xd3, 0x93, 0x93, 0x42, 0x51, 0x52, 0xc9, 0x7e, 0x71, 0xdd, 0x24, 0x60, 0x1c, 0x11, 0xab, 0xcf, 0xa0, 0xf1, 0x2f, 0x53, 0xc6, 0x80, 0xbd, 0x3a, 0xe7, 0x57, 0xb8, 0x13, 0x4a, 0x9c, 0x10, 0xd4, 0x29, 0x61, 0x58, 0x69, 0x21, 0x7f, 0xdd, 0x58, 0x85, 0xc4, 0xdb, 0x17, 0x49, 0x85, 0x70, 0x3a, 0x6d, 0x6d, 0xe9, 0x4a, 0x66, 0x7e, 0xac, 0x30, 0x23, 0x44, 0x3a, 0x83, 0x37, 0xae, 0x1b, 0xc6, 0x01, 0xb7, 0x6d, 0x7d, 0x38, 0xec, 0x3c, 0x34, 0x46, 0x31, 0x05, 0xf0, 0xd3, 0x94, 0x9d, 0x78, 0xe5, 0x62, 0xa0, 0x39, 0xe4, 0x46, 0x95, 0x48, 0xb6, 0x09, 0x39, 0x5d, 0xe5, 0xa4, 0xfd, 0x43, 0xc4, 0x6c, 0xa9, 0xfd, 0x6e, 0xe2, 0x9a, 0xda, 0x5e, 0xfc, 0x07, 0xd8, 0x4d, 0x55, 0x32, 0x49, 0x45, 0x0d, 0xab, 0x4a, 0x49, 0xc4, 0x83, 0xde, 0xd2, 0x50, 0xc9, 0x33, 0x8f, 0x85, 0xcd, 0x93, 0x7a, 0xe6, 0x6b, 0xb4, 0x36, 0xf3, 0xb4, 0x02, 0x6e, 0x85, 0x9f, 0xda, 0x1c, 0xa5, 0x71, 0x43, 0x2f, 0x3b, 0xfc, 0x09, 0xe7, 0xc0, 0x3c, 0xa4, 0xd1, 0x83, 0xb7, 0x41, 0x11, 0x1c, 0xa0, 0x48, 0x3d, 0x0e, 0xda, 0xbc, 0x03, 0xfe, 0xb2, 0x3b, 0x17, 0xee, 0x48, 0xe8, 0x44, 0xba, 0x24, 0x08, 0xd9, 0xdc, 0xfd, 0x01, 0x39, 0xd2, 0xe8, 0xc7, 0x31, 0x01, 0x25, 0xae, 0xe8, 0x01, 0xc6, 0x1a, 0xb7, 0x90, 0x0d, 0x1e, 0xfc, 0x47, 0xc0, 0x78, 0x28, 0x17, 0x66, 0xf3, 0x61, 0xc5, 0xe6, 0x11, 0x13, 0x46, 0x23, 0x5e, 0x1d, 0xc3, 0x83, 0x25, 0x66, 0x6c }; static const unsigned char shake256_largemsg_input[] = { 0xb2, 0xd2, 0x38, 0x65, 0xaf, 0x8f, 0x25, 0x6e, 0x64, 0x40, 0xe2, 0x0d, 0x49, 0x8e, 0x3e, 0x64, 0x46, 0xd2, 0x03, 0xa4, 0x19, 0xe3, 0x7b, 0x80, 0xf7, 0x2b, 0x32, 0xe2, 0x76, 0x01, 0xfe, 0xdd, 0xaa, 0x33, 0x3d, 0xe4, 0x8e, 0xe1, 0x5e, 0x39, 0xa6, 0x92, 0xa3, 0xa7, 0xe3, 0x81, 0x24, 0x74, 0xc7, 0x38, 0x18, 0x92, 0xc9, 0x60, 0x50, 0x15, 0xfb, 0xd8, 0x04, 0xea, 0xea, 0x04, 0xd2, 0xc5, 0xc6, 0x68, 0x04, 0x5b, 0xc3, 0x75, 0x12, 0xd2, 0xbe, 0xa2, 0x67, 0x75, 0x24, 0xbf, 0x68, 0xad, 0x10, 0x86, 0xb3, 0x2c, 0xb3, 0x74, 0xa4, 0x6c, 0xf9, 0xd7, 0x1e, 0x58, 0x69, 0x27, 0x88, 0x49, 0x4e, 0x99, 0x15, 0x33, 0x14, 0xf2, 0x49, 0x21, 0xf4, 0x99, 0xb9, 0xde, 0xd4, 0xf1, 0x12, 0xf5, 0x68, 0xe5, 0x5c, 0xdc, 0x9e, 0xc5, 0x80, 0x6d, 0x39, 0x50, 0x08, 0x95, 0xbb, 0x12, 0x27, 0x50, 0x89, 0xf0, 0xf9, 0xd5, 0x4a, 0x01, 0x0b, 0x0d, 0x90, 0x9f, 0x1e, 0x4a, 0xba, 0xbe, 0x28, 0x36, 0x19, 0x7d, 0x9c, 0x0a, 0x51, 0xfb, 0xeb, 0x00, 0x02, 0x6c, 0x4b, 0x0a, 0xa8, 0x6c, 0xb7, 0xc4, 0xc0, 0x92, 0x37, 0xa7, 0x2d, 0x49, 0x61, 0x80, 0xd9, 0xdb, 0x20, 0x21, 0x9f, 0xcf, 0xb4, 0x57, 0x69, 0x75, 0xfa, 0x1c, 0x95, 0xbf, 0xee, 0x0d, 0x9e, 0x52, 0x6e, 0x1e, 0xf8, 0xdd, 0x41, 0x8c, 0x3b, 0xaa, 0x57, 0x13, 0x84, 0x73, 0x52, 0x62, 0x18, 0x76, 0x46, 0xcc, 0x4b, 0xcb, 0xbd, 0x40, 0xa1, 0xf6, 0xff, 0x7b, 0x32, 0xb9, 0x90, 0x7c, 0x53, 0x2c, 0xf9, 0x38, 0x72, 0x0f, 0xcb, 0x90, 0x42, 0x5e, 0xe2, 0x80, 0x19, 0x26, 0xe7, 0x99, 0x96, 0x98, 0x18, 0xb1, 0x86, 0x5b, 0x4c, 0xd9, 0x08, 0x27, 0x31, 0x8f, 0xf0, 0x90, 0xd9, 0x35, 0x6a, 0x1f, 0x75, 0xc2, 0xe0, 0xa7, 0x60, 0xb8, 0x1d, 0xd6, 0x5f, 0x56, 0xb2, 0x0b, 0x27, 0x0e, 0x98, 0x67, 0x1f, 0x39, 0x18, 0x27, 0x68, 0x0a, 0xe8, 0x31, 0x1b, 0xc0, 0x97, 0xec, 0xd1, 0x20, 0x2a, 0x55, 0x69, 0x23, 0x08, 0x50, 0x05, 0xec, 0x13, 0x3b, 0x56, 0xfc, 0x18, 0xc9, 0x1a, 0xa9, 0x69, 0x0e, 0xe2, 0xcc, 0xc8, 0xd6, 0x19, 0xbb, 0x87, 0x3b, 0x42, 0x77, 0xee, 0x77, 0x81, 0x26, 0xdd, 0xf6, 0x5d, 0xc3, 0xb2, 0xb0, 0xc4, 0x14, 0x6d, 0xb5, 0x4f, 0xdc, 0x13, 0x09, 0xc8, 0x53, 0x50, 0xb3, 0xea, 0xd3, 0x5f, 0x11, 0x67, 0xd4, 0x2f, 0x6e, 0x30, 0x1a, 0xbe, 0xd6, 0xf0, 0x2d, 0xc9, 0x29, 0xd9, 0x0a, 0xa8, 0x6f, 0xa4, 0x18, 0x74, 0x6b, 0xd3, 0x5d, 0x6a, 0x73, 0x3a, 0xf2, 0x94, 0x7f, 0xbd, 0xb4, 0xa6, 0x7f, 0x5b, 0x3d, 0x26, 0xf2, 0x6c, 0x13, 0xcf, 0xb4, 0x26, 0x1e, 0x38, 0x17, 0x66, 0x60, 0xb1, 0x36, 0xae, 0xe0, 0x6d, 0x86, 0x69, 0xe7, 0xe7, 0xae, 0x77, 0x6f, 0x7e, 0x99, 0xe5, 0xd9, 0x62, 0xc9, 0xfc, 0xde, 0xb4, 0xee, 0x7e, 0xc8, 0xe9, 0xb7, 0x2c, 0xe2, 0x70, 0xe8, 0x8b, 0x2d, 0x94, 0xad, 0xe8, 0x54, 0xa3, 0x2d, 0x9a, 0xe2, 0x50, 0x63, 0x87, 0xb3, 0x56, 0x29, 0xea, 0xa8, 0x5e, 0x96, 0x53, 0x9f, 0x23, 0x8a, 0xef, 0xa3, 0xd4, 0x87, 0x09, 0x5f, 0xba, 0xc3, 0xd1, 0xd9, 0x1a, 0x7b, 0x5c, 0x5d, 0x5d, 0x89, 0xed, 0xb6, 0x6e, 0x39, 0x73, 0xa5, 0x64, 0x59, 0x52, 0x8b, 0x61, 0x8f, 0x66, 0x69, 0xb9, 0xf0, 0x45, 0x0a, 0x57, 0xcd, 0xc5, 0x7f, 0x5d, 0xd0, 0xbf, 0xcc, 0x0b, 0x48, 0x12, 0xe1, 0xe2, 0xc2, 0xea, 0xcc, 0x09, 0xd9, 0x42, 0x2c, 0xef, 0x4f, 0xa7, 0xe9, 0x32, 0x5c, 0x3f, 0x22, 0xc0, 0x45, 0x0b, 0x67, 0x3c, 0x31, 0x69, 0x29, 0xa3, 0x39, 0xdd, 0x6e, 0x2f, 0xbe, 0x10, 0xc9, 0x7b, 0xff, 0x19, 0x8a, 0xe9, 0xea, 0xfc, 0x32, 0x41, 0x33, 0x70, 0x2a, 0x9a, 0xa4, 0xe6, 0xb4, 0x7e, 0xb4, 0xc6, 0x21, 0x49, 0x5a, 0xfc, 0x45, 0xd2, 0x23, 0xb3, 0x28, 0x4d, 0x83, 0x60, 0xfe, 0x70, 0x68, 0x03, 0x59, 0xd5, 0x15, 0xaa, 0x9e, 0xa0, 0x2e, 0x36, 0xb5, 0x61, 0x0f, 0x61, 0x05, 0x3c, 0x62, 0x00, 0xa0, 0x47, 0xf1, 0x86, 0xba, 0x33, 0xb8, 0xca, 0x60, 0x2f, 0x3f, 0x0a, 0x67, 0x09, 0x27, 0x2f, 0xa2, 0x96, 0x02, 0x52, 0x58, 0x55, 0x68, 0x80, 0xf4, 0x4f, 0x47, 0xba, 0xff, 0x41, 0x7a, 0x40, 0x4c, 0xfd, 0x9d, 0x10, 0x72, 0x0e, 0x20, 0xa9, 0x7f, 0x9b, 0x9b, 0x14, 0xeb, 0x8e, 0x61, 0x25, 0xcb, 0xf4, 0x58, 0xff, 0x47, 0xa7, 0x08, 0xd6, 0x4e, 0x2b, 0xf1, 0xf9, 0x89, 0xd7, 0x22, 0x0f, 0x8d, 0x35, 0x07, 0xa0, 0x54, 0xab, 0x83, 0xd8, 0xee, 0x5a, 0x3e, 0x88, 0x74, 0x46, 0x41, 0x6e, 0x3e, 0xb7, 0xc0, 0xb6, 0x55, 0xe0, 0x36, 0xc0, 0x2b, 0xbf, 0xb8, 0x24, 0x8a, 0x44, 0x82, 0xf4, 0xcb, 0xb5, 0xd7, 0x41, 0x48, 0x51, 0x08, 0xe0, 0x14, 0x34, 0xd2, 0x6d, 0xe9, 0x7a, 0xec, 0x91, 0x61, 0xa7, 0xe1, 0x81, 0x69, 0x47, 0x1c, 0xc7, 0xf3 }; static const unsigned char shake256_largemsg_output[] = { 0x64, 0xea, 0x24, 0x6a, 0xab, 0x80, 0x37, 0x9e, 0x08, 0xe2, 0x19, 0x9e, 0x09, 0x69, 0xe2, 0xee, 0x1a, 0x5d, 0xd1, 0x68, 0x68, 0xec, 0x8d, 0x42, 0xd0, 0xf8, 0xb8, 0x44, 0x74, 0x54, 0x87, 0x3e, }; static EVP_MD_CTX *shake_setup(const char *name) { EVP_MD_CTX *ctx = NULL; EVP_MD *md = NULL; if (!TEST_ptr(md = EVP_MD_fetch(NULL, name, NULL))) return NULL; if (!TEST_ptr(ctx = EVP_MD_CTX_new())) goto err; if (!TEST_true(EVP_DigestInit_ex2(ctx, md, NULL))) goto err; EVP_MD_free(md); return ctx; err: EVP_MD_free(md); EVP_MD_CTX_free(ctx); return NULL; } static int shake_kat_test(void) { int ret = 0; EVP_MD_CTX *ctx = NULL; unsigned char out[sizeof(shake256_output)]; if (!TEST_ptr(ctx = shake_setup("SHAKE256"))) return 0; if (!TEST_true(EVP_DigestUpdate(ctx, shake256_input, sizeof(shake256_input))) || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out))) || !TEST_mem_eq(out, sizeof(out), shake256_output,sizeof(shake256_output)) || !TEST_false(EVP_DigestFinalXOF(ctx, out, sizeof(out))) || !TEST_false(EVP_DigestSqueeze(ctx, out, sizeof(out)))) goto err; ret = 1; err: EVP_MD_CTX_free(ctx); return ret; } static int shake_kat_digestfinal_test(void) { int ret = 0; unsigned int digest_length = 0; EVP_MD_CTX *ctx = NULL; unsigned char out[sizeof(shake256_output)]; if (!TEST_ptr(ctx = shake_setup("SHAKE256"))) return 0; if (!TEST_true(EVP_DigestUpdate(ctx, shake256_input, sizeof(shake256_input))) || !TEST_true(EVP_DigestFinal(ctx, out, &digest_length)) || !TEST_uint_eq(digest_length, 32) || !TEST_mem_eq(out, digest_length, shake256_output, digest_length) || !TEST_false(EVP_DigestFinalXOF(ctx, out, sizeof(out)))) goto err; ret = 1; err: EVP_MD_CTX_free(ctx); return ret; } static int shake_kat_digestfinal_xoflen_test(void) { int ret = 0; unsigned int digest_length = 0; EVP_MD_CTX *ctx = NULL; unsigned char out[sizeof(shake256_output)]; OSSL_PARAM params[2]; size_t sz = 12; if (!TEST_ptr(ctx = shake_setup("SHAKE256"))) return 0; memset(out, 0, sizeof(out)); params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz); params[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_MD_CTX_set_params(ctx, params), 1) || !TEST_true(EVP_DigestUpdate(ctx, shake256_input, sizeof(shake256_input))) || !TEST_true(EVP_DigestFinal(ctx, out, &digest_length)) || !TEST_uint_eq(digest_length, (unsigned int)sz) || !TEST_mem_eq(out, digest_length, shake256_output, digest_length) || !TEST_uchar_eq(out[digest_length], 0)) goto err; ret = 1; err: EVP_MD_CTX_free(ctx); return ret; } static int shake_absorb_test(void) { int ret = 0; EVP_MD_CTX *ctx = NULL; unsigned char out[sizeof(shake256_largemsg_output)]; size_t total = sizeof(shake256_largemsg_input); size_t i, stride, sz; if (!TEST_ptr(ctx = shake_setup("SHAKE256"))) return 0; for (stride = 1; stride < total; ++stride) { sz = 0; for (i = 0; i < total; i += sz) { sz += stride; if ((i + sz) > total) sz = total - i; if (!TEST_true(EVP_DigestUpdate(ctx, shake256_largemsg_input + i, sz))) goto err; } if (!TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out))) || !TEST_mem_eq(out, sizeof(out), shake256_largemsg_output, sizeof(shake256_largemsg_output))) goto err; if (!TEST_true(EVP_DigestInit_ex2(ctx, NULL, NULL))) goto err; } ret = 1; err: EVP_MD_CTX_free(ctx); return ret; } static const struct { size_t startsz, incsz; } stride_tests[] = { { 1, 1 }, { 1, 136 }, { 1, 136/2 }, { 1, 136/2-1 }, { 1, 136/2+1 }, { 1, 136*3 }, { 8, 8 }, { 9, 9 }, { 10, 10 }, { 136/2 - 1, 136 }, { 136/2 - 1, 136-1 }, { 136/2 - 1, 136+1 }, { 136/2, 136 }, { 136/2, 136-1 }, { 136/2, 136+1 }, { 136/2 + 1, 136 }, { 136/2 + 1, 136-1 }, { 136/2 + 1, 136+1 }, { 136, 2 }, { 136, 136 }, { 136-1, 136 }, { 136-1, 136-1 }, { 136-1, 136+1 }, { 136+1, 136 }, { 136+1, 136-1 }, { 136+1, 136+1 }, { 136*3, 136 }, { 136*3, 136 + 1 }, { 136*3, 136 - 1 }, { 136*3, 136/2 }, { 136*3, 136/2 + 1 }, { 136*3, 136/2 - 1 }, }; static int do_shake_squeeze_test(int tst, const unsigned char *in, size_t inlen, const unsigned char *expected_out, size_t expected_outlen) { int ret = 0; EVP_MD_CTX *ctx = NULL; unsigned char *out = NULL; size_t i = 0, sz = stride_tests[tst].startsz; if (!TEST_ptr(ctx = shake_setup("SHAKE256"))) return 0; if (!TEST_ptr(out = OPENSSL_malloc(expected_outlen))) goto err; if (!TEST_true(EVP_DigestUpdate(ctx, in, inlen))) goto err; while (i < expected_outlen) { if ((i + sz) > expected_outlen) sz = expected_outlen - i; if (!TEST_true(EVP_DigestSqueeze(ctx, out + i, sz))) goto err; i += sz; sz = stride_tests[tst].incsz; } if (!TEST_mem_eq(out, expected_outlen, expected_out, expected_outlen)) goto err; ret = 1; err: OPENSSL_free(out); EVP_MD_CTX_free(ctx); return ret; } static int shake_squeeze_kat_test(int tst) { return do_shake_squeeze_test(tst, shake256_input, sizeof(shake256_input), shake256_output, sizeof(shake256_output)); } static int shake_squeeze_large_test(int tst) { int ret = 0; EVP_MD_CTX *ctx = NULL; unsigned char msg[16]; unsigned char out[2000]; if (!TEST_int_gt(RAND_bytes(msg, sizeof(msg)), 0) || !TEST_ptr(ctx = shake_setup("SHAKE256")) || !TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg))) || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out)))) goto err; ret = do_shake_squeeze_test(tst, msg, sizeof(msg), out, sizeof(out)); err: EVP_MD_CTX_free(ctx); return ret; } static const size_t dupoffset_tests[] = { 1, 135, 136, 137, 136*3-1, 136*3, 136*3+1 }; static int do_shake_squeeze_dup_test(int tst, const char *alg, const unsigned char *in, size_t inlen, const unsigned char *expected_out, size_t expected_outlen) { int ret = 0; EVP_MD_CTX *cur, *ctx = NULL, *dupctx = NULL; unsigned char *out = NULL; size_t i = 0, sz = 10; size_t dupoffset = dupoffset_tests[tst]; if (!TEST_ptr(ctx = shake_setup(alg))) return 0; cur = ctx; if (!TEST_ptr(out = OPENSSL_malloc(expected_outlen))) goto err; if (!TEST_true(EVP_DigestUpdate(ctx, in, inlen))) goto err; while (i < expected_outlen) { if ((i + sz) > expected_outlen) sz = expected_outlen - i; if (!TEST_true(EVP_DigestSqueeze(cur, out + i, sz))) goto err; i += sz; if (dupctx == NULL && i >= dupoffset) { if (!TEST_ptr(dupctx = EVP_MD_CTX_dup(ctx))) goto err; cur = dupctx; } } if (!TEST_mem_eq(out, expected_outlen, expected_out, expected_outlen)) goto err; ret = 1; err: OPENSSL_free(out); EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(dupctx); return ret; } static int shake_squeeze_dup_test(int tst) { int ret = 0; EVP_MD_CTX *ctx = NULL; unsigned char msg[16]; unsigned char out[1000]; const char *alg = "SHAKE128"; if (!TEST_int_gt(RAND_bytes(msg, sizeof(msg)), 0) || !TEST_ptr(ctx = shake_setup(alg)) || !TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg))) || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out)))) goto err; ret = do_shake_squeeze_dup_test(tst, alg, msg, sizeof(msg), out, sizeof(out)); err: EVP_MD_CTX_free(ctx); return ret; } int setup_tests(void) { ADD_TEST(shake_kat_test); ADD_TEST(shake_kat_digestfinal_test); ADD_TEST(shake_kat_digestfinal_xoflen_test); ADD_TEST(shake_absorb_test); ADD_ALL_TESTS(shake_squeeze_kat_test, OSSL_NELEM(stride_tests)); ADD_ALL_TESTS(shake_squeeze_large_test, OSSL_NELEM(stride_tests)); ADD_ALL_TESTS(shake_squeeze_dup_test, OSSL_NELEM(dupoffset_tests)); return 1; }
test
openssl/test/evp_xof_test.c
openssl
#include <stdio.h> #include <string.h> #include "testutil.h" #include <openssl/asn1.h> #include <openssl/asn1t.h> #include <openssl/bio.h> #include <openssl/err.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include "internal/nelem.h" static const ASN1_ITEM *item_type; static const char *test_file; typedef enum { ASN1_UNKNOWN, ASN1_OK, ASN1_BIO, ASN1_DECODE, ASN1_ENCODE, ASN1_COMPARE } expected_error_t; typedef struct { const char *str; expected_error_t code; } error_enum; static expected_error_t expected_error = ASN1_UNKNOWN; static int test_bad_asn1(void) { BIO *bio = NULL; ASN1_VALUE *value = NULL; int ret = 0; unsigned char buf[2048]; const unsigned char *buf_ptr = buf; unsigned char *der = NULL; int derlen; int len; bio = BIO_new_file(test_file, "r"); if (!TEST_ptr(bio)) return 0; if (expected_error == ASN1_BIO) { if (TEST_ptr_null(ASN1_item_d2i_bio(item_type, bio, NULL))) ret = 1; goto err; } len = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_ge(len, 0)) goto err; value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type); if (value == NULL) { if (TEST_int_eq(expected_error, ASN1_DECODE)) ret = 1; goto err; } derlen = ASN1_item_i2d(value, &der, item_type); if (der == NULL || derlen < 0) { if (TEST_int_eq(expected_error, ASN1_ENCODE)) ret = 1; goto err; } if (derlen != len || memcmp(der, buf, derlen) != 0) { if (TEST_int_eq(expected_error, ASN1_COMPARE)) ret = 1; goto err; } if (TEST_int_eq(expected_error, ASN1_OK)) ret = 1; err: if (ret == 1 && !TEST_false(ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)) ret = 0; BIO_free(bio); OPENSSL_free(der); ASN1_item_free(value, item_type); return ret; } OPT_TEST_DECLARE_USAGE("item_name expected_error test_file.der\n") int setup_tests(void) { const char *test_type_name; const char *expected_error_string; size_t i; static error_enum expected_errors[] = { {"OK", ASN1_OK}, {"BIO", ASN1_BIO}, {"decode", ASN1_DECODE}, {"encode", ASN1_ENCODE}, {"compare", ASN1_COMPARE} }; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(test_type_name = test_get_argument(0)) || !TEST_ptr(expected_error_string = test_get_argument(1)) || !TEST_ptr(test_file = test_get_argument(2))) return 0; item_type = ASN1_ITEM_lookup(test_type_name); if (item_type == NULL) { TEST_error("Unknown type %s", test_type_name); TEST_note("Supported types:"); for (i = 0;; i++) { const ASN1_ITEM *it = ASN1_ITEM_get(i); if (it == NULL) break; TEST_note("\t%s", it->sname); } return 0; } for (i = 0; i < OSSL_NELEM(expected_errors); i++) { if (strcmp(expected_errors[i].str, expected_error_string) == 0) { expected_error = expected_errors[i].code; break; } } if (expected_error == ASN1_UNKNOWN) { TEST_error("Unknown expected error %s\n", expected_error_string); return 0; } ADD_TEST(test_bad_asn1); return 1; }
test
openssl/test/d2i_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <openssl/crypto.h> #include <openssl/rand.h> #include <openssl/bn.h> #include <openssl/dsa.h> #include <openssl/evp.h> #include <openssl/core_names.h> #include "testutil.h" #include "internal/nelem.h" #ifndef OPENSSL_NO_DSA static int dsa_cb(int p, int n, BN_GENCB *arg); static unsigned char out_p[] = { 0x8d, 0xf2, 0xa4, 0x94, 0x49, 0x22, 0x76, 0xaa, 0x3d, 0x25, 0x75, 0x9b, 0xb0, 0x68, 0x69, 0xcb, 0xea, 0xc0, 0xd8, 0x3a, 0xfb, 0x8d, 0x0c, 0xf7, 0xcb, 0xb8, 0x32, 0x4f, 0x0d, 0x78, 0x82, 0xe5, 0xd0, 0x76, 0x2f, 0xc5, 0xb7, 0x21, 0x0e, 0xaf, 0xc2, 0xe9, 0xad, 0xac, 0x32, 0xab, 0x7a, 0xac, 0x49, 0x69, 0x3d, 0xfb, 0xf8, 0x37, 0x24, 0xc2, 0xec, 0x07, 0x36, 0xee, 0x31, 0xc8, 0x02, 0x91, }; static unsigned char out_q[] = { 0xc7, 0x73, 0x21, 0x8c, 0x73, 0x7e, 0xc8, 0xee, 0x99, 0x3b, 0x4f, 0x2d, 0xed, 0x30, 0xf4, 0x8e, 0xda, 0xce, 0x91, 0x5f, }; static unsigned char out_g[] = { 0x62, 0x6d, 0x02, 0x78, 0x39, 0xea, 0x0a, 0x13, 0x41, 0x31, 0x63, 0xa5, 0x5b, 0x4c, 0xb5, 0x00, 0x29, 0x9d, 0x55, 0x22, 0x95, 0x6c, 0xef, 0xcb, 0x3b, 0xff, 0x10, 0xf3, 0x99, 0xce, 0x2c, 0x2e, 0x71, 0xcb, 0x9d, 0xe5, 0xfa, 0x24, 0xba, 0xbf, 0x58, 0xe5, 0xb7, 0x95, 0x21, 0x92, 0x5c, 0x9c, 0xc4, 0x2e, 0x9f, 0x6f, 0x46, 0x4b, 0x08, 0x8c, 0xc5, 0x72, 0xaf, 0x53, 0xe6, 0xd7, 0x88, 0x02, }; static int dsa_test(void) { BN_GENCB *cb; DSA *dsa = NULL; int counter, ret = 0, i, j; unsigned char buf[256]; unsigned long h; unsigned char sig[256]; unsigned int siglen; const BIGNUM *p = NULL, *q = NULL, *g = NULL; static unsigned char seed[20] = { 0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, 0xb6, 0x21, 0x1b, 0x40, 0x62, 0xba, 0x32, 0x24, 0xe0, 0x42, 0x7d, 0xd3, }; static const unsigned char str1[] = "12345678901234567890"; if (!TEST_ptr(cb = BN_GENCB_new())) goto end; BN_GENCB_set(cb, dsa_cb, NULL); if (!TEST_ptr(dsa = DSA_new()) || !TEST_true(DSA_generate_parameters_ex(dsa, 512, seed, 20, &counter, &h, cb))) goto end; if (!TEST_int_eq(counter, 105)) goto end; if (!TEST_int_eq(h, 2)) goto end; DSA_get0_pqg(dsa, &p, &q, &g); i = BN_bn2bin(q, buf); j = sizeof(out_q); if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_q, i)) goto end; i = BN_bn2bin(p, buf); j = sizeof(out_p); if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_p, i)) goto end; i = BN_bn2bin(g, buf); j = sizeof(out_g); if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_g, i)) goto end; if (!TEST_true(DSA_generate_key(dsa))) goto end; if (!TEST_true(DSA_sign(0, str1, 20, sig, &siglen, dsa))) goto end; if (TEST_int_gt(DSA_verify(0, str1, 20, sig, siglen, dsa), 0)) ret = 1; end: DSA_free(dsa); BN_GENCB_free(cb); return ret; } static int dsa_cb(int p, int n, BN_GENCB *arg) { static int ok = 0, num = 0; if (p == 0) num++; if (p == 2) ok++; if (!ok && (p == 0) && (num > 1)) { TEST_error("dsa_cb error"); return 0; } return 1; } # define P 0 # define Q 1 # define G 2 # define SEED 3 # define PCOUNT 4 # define GINDEX 5 # define HCOUNT 6 # define GROUP 7 static int dsa_keygen_test(void) { int ret = 0; EVP_PKEY *param_key = NULL, *key = NULL; EVP_PKEY_CTX *pg_ctx = NULL, *kg_ctx = NULL; BIGNUM *p_in = NULL, *q_in = NULL, *g_in = NULL; BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL; int gindex_out = 0, pcount_out = 0, hcount_out = 0; unsigned char seed_out[32]; char group_out[32]; size_t len = 0; const OSSL_PARAM *settables = NULL; static const unsigned char seed_data[] = { 0xa6, 0xf5, 0x28, 0x8c, 0x50, 0x77, 0xa5, 0x68, 0x6d, 0x3a, 0xf5, 0xf1, 0xc6, 0x4c, 0xdc, 0x35, 0x95, 0x26, 0x3f, 0x03, 0xdc, 0x00, 0x3f, 0x44, 0x7b, 0x2a, 0xc7, 0x29 }; static const unsigned char expected_p[]= { 0xdb, 0x47, 0x07, 0xaf, 0xf0, 0x06, 0x49, 0x55, 0xc9, 0xbb, 0x09, 0x41, 0xb8, 0xdb, 0x1f, 0xbc, 0xa8, 0xed, 0x12, 0x06, 0x7f, 0x88, 0x49, 0xb8, 0xc9, 0x12, 0x87, 0x21, 0xbb, 0x08, 0x6c, 0xbd, 0xf1, 0x89, 0xef, 0x84, 0xd9, 0x7a, 0x93, 0xe8, 0x45, 0x40, 0x81, 0xec, 0x37, 0x27, 0x1a, 0xa4, 0x22, 0x51, 0x99, 0xf0, 0xde, 0x04, 0xdb, 0xea, 0xa1, 0xf9, 0x37, 0x83, 0x80, 0x96, 0x36, 0x53, 0xf6, 0xae, 0x14, 0x73, 0x33, 0x0f, 0xdf, 0x0b, 0xf9, 0x2f, 0x08, 0x46, 0x31, 0xf9, 0x66, 0xcd, 0x5a, 0xeb, 0x6c, 0xf3, 0xbb, 0x74, 0xf3, 0x88, 0xf0, 0x31, 0x5c, 0xa4, 0xc8, 0x0f, 0x86, 0xf3, 0x0f, 0x9f, 0xc0, 0x8c, 0x57, 0xe4, 0x7f, 0x95, 0xb3, 0x62, 0xc8, 0x4e, 0xae, 0xf3, 0xd8, 0x14, 0xcc, 0x47, 0xc2, 0x4b, 0x4f, 0xef, 0xaf, 0xcd, 0xcf, 0xb2, 0xbb, 0xe8, 0xbe, 0x08, 0xca, 0x15, 0x90, 0x59, 0x35, 0xef, 0x35, 0x1c, 0xfe, 0xeb, 0x33, 0x2e, 0x25, 0x22, 0x57, 0x9c, 0x55, 0x23, 0x0c, 0x6f, 0xed, 0x7c, 0xb6, 0xc7, 0x36, 0x0b, 0xcb, 0x2b, 0x6a, 0x21, 0xa1, 0x1d, 0x55, 0x77, 0xd9, 0x91, 0xcd, 0xc1, 0xcd, 0x3d, 0x82, 0x16, 0x9c, 0xa0, 0x13, 0xa5, 0x83, 0x55, 0x3a, 0x73, 0x7e, 0x2c, 0x44, 0x3e, 0x70, 0x2e, 0x50, 0x91, 0x6e, 0xca, 0x3b, 0xef, 0xff, 0x85, 0x35, 0x70, 0xff, 0x61, 0x0c, 0xb1, 0xb2, 0xb7, 0x94, 0x6f, 0x65, 0xa4, 0x57, 0x62, 0xef, 0x21, 0x83, 0x0f, 0x3e, 0x71, 0xae, 0x7d, 0xe4, 0xad, 0xfb, 0xe3, 0xdd, 0xd6, 0x03, 0xda, 0x9a, 0xd8, 0x8f, 0x2d, 0xbb, 0x90, 0x87, 0xf8, 0xdb, 0xdc, 0xec, 0x71, 0xf2, 0xdb, 0x0b, 0x8e, 0xfc, 0x1a, 0x7e, 0x79, 0xb1, 0x1b, 0x0d, 0xfc, 0x70, 0xec, 0x85, 0xc2, 0xc5, 0xba, 0xb9, 0x69, 0x3f, 0x88, 0xbc, 0xcb }; static const unsigned char expected_q[]= { 0x99, 0xb6, 0xa0, 0xee, 0xb3, 0xa6, 0x99, 0x1a, 0xb6, 0x67, 0x8d, 0xc1, 0x2b, 0x9b, 0xce, 0x2b, 0x01, 0x72, 0x5a, 0x65, 0x76, 0x3d, 0x93, 0x69, 0xe2, 0x56, 0xae, 0xd7 }; static const unsigned char expected_g[]= { 0x63, 0xf8, 0xb6, 0xee, 0x2a, 0x27, 0xaf, 0x4f, 0x4c, 0xf6, 0x08, 0x28, 0x87, 0x4a, 0xe7, 0x1f, 0x45, 0x46, 0x27, 0x52, 0x3b, 0x7f, 0x6f, 0xd2, 0x29, 0xcb, 0xe8, 0x11, 0x19, 0x25, 0x35, 0x76, 0x99, 0xcb, 0x4f, 0x1b, 0xe0, 0xed, 0x32, 0x9e, 0x05, 0xb5, 0xbe, 0xd7, 0xf6, 0x5a, 0xb2, 0xf6, 0x0e, 0x0c, 0x7e, 0xf5, 0xe1, 0x05, 0xfe, 0xda, 0xaf, 0x0f, 0x27, 0x1e, 0x40, 0x2a, 0xf7, 0xa7, 0x23, 0x49, 0x2c, 0xd9, 0x1b, 0x0a, 0xbe, 0xff, 0xc7, 0x7c, 0x7d, 0x60, 0xca, 0xa3, 0x19, 0xc3, 0xb7, 0xe4, 0x43, 0xb0, 0xf5, 0x75, 0x44, 0x90, 0x46, 0x47, 0xb1, 0xa6, 0x48, 0x0b, 0x21, 0x8e, 0xee, 0x75, 0xe6, 0x3d, 0xa7, 0xd3, 0x7b, 0x31, 0xd1, 0xd2, 0x9d, 0xe2, 0x8a, 0xfc, 0x57, 0xfd, 0x8a, 0x10, 0x31, 0xeb, 0x87, 0x36, 0x3f, 0x65, 0x72, 0x23, 0x2c, 0xd3, 0xd6, 0x17, 0xa5, 0x62, 0x58, 0x65, 0x57, 0x6a, 0xd4, 0xa8, 0xfe, 0xec, 0x57, 0x76, 0x0c, 0xb1, 0x4c, 0x93, 0xed, 0xb0, 0xb4, 0xf9, 0x45, 0xb3, 0x3e, 0xdd, 0x47, 0xf1, 0xfb, 0x7d, 0x25, 0x79, 0x3d, 0xfc, 0xa7, 0x39, 0x90, 0x68, 0x6a, 0x6b, 0xae, 0xf2, 0x6e, 0x64, 0x8c, 0xfb, 0xb8, 0xdd, 0x76, 0x4e, 0x4a, 0x69, 0x8c, 0x97, 0x15, 0x77, 0xb2, 0x67, 0xdc, 0xeb, 0x4a, 0x40, 0x6b, 0xb9, 0x47, 0x8f, 0xa6, 0xab, 0x6e, 0x98, 0xc0, 0x97, 0x9a, 0x0c, 0xea, 0x00, 0xfd, 0x56, 0x1a, 0x74, 0x9a, 0x32, 0x6b, 0xfe, 0xbd, 0xdf, 0x6c, 0x82, 0x54, 0x53, 0x4d, 0x70, 0x65, 0xe3, 0x8b, 0x37, 0xb8, 0xe4, 0x70, 0x08, 0xb7, 0x3b, 0x30, 0x27, 0xaf, 0x1c, 0x77, 0xf3, 0x62, 0xd4, 0x9a, 0x59, 0xba, 0xd1, 0x6e, 0x89, 0x5c, 0x34, 0x9a, 0xa1, 0xb7, 0x4f, 0x7d, 0x8c, 0xdc, 0xbc, 0x74, 0x25, 0x5e, 0xbf, 0x77, 0x46 }; int expected_c = 1316; int expected_h = 2; if (!TEST_ptr(p_in = BN_bin2bn(expected_p, sizeof(expected_p), NULL)) || !TEST_ptr(q_in = BN_bin2bn(expected_q, sizeof(expected_q), NULL)) || !TEST_ptr(g_in = BN_bin2bn(expected_g, sizeof(expected_g), NULL))) goto end; if (!TEST_ptr(pg_ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pg_ctx), 0) || !TEST_ptr_null(EVP_PKEY_CTX_gettable_params(pg_ctx)) || !TEST_ptr(settables = EVP_PKEY_CTX_settable_params(pg_ctx)) || !TEST_ptr(OSSL_PARAM_locate_const(settables, OSSL_PKEY_PARAM_FFC_PBITS)) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_type(pg_ctx, "fips186_4")) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(pg_ctx, 2048)) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_q_bits(pg_ctx, 224)) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_seed(pg_ctx, seed_data, sizeof(seed_data))) || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_md_props(pg_ctx, "SHA256", "")) || !TEST_int_gt(EVP_PKEY_generate(pg_ctx, &param_key), 0) || !TEST_ptr(kg_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param_key, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(kg_ctx), 0) || !TEST_int_gt(EVP_PKEY_generate(kg_ctx, &key), 0)) goto end; if (!TEST_true(EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_FFC_P, &p_out)) || !TEST_BN_eq(p_in, p_out) || !TEST_true(EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_FFC_Q, &q_out)) || !TEST_BN_eq(q_in, q_out) || !TEST_true(EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_FFC_G, &g_out)) || !TEST_BN_eq(g_in, g_out) || !TEST_true(EVP_PKEY_get_octet_string_param( key, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_mem_eq(seed_out, len, seed_data, sizeof(seed_data)) || !TEST_true(EVP_PKEY_get_int_param(key, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex_out)) || !TEST_int_eq(gindex_out, -1) || !TEST_true(EVP_PKEY_get_int_param(key, OSSL_PKEY_PARAM_FFC_H, &hcount_out)) || !TEST_int_eq(hcount_out, expected_h) || !TEST_true(EVP_PKEY_get_int_param(key, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcount_out)) || !TEST_int_eq(pcount_out, expected_c) || !TEST_false(EVP_PKEY_get_utf8_string_param(key, OSSL_PKEY_PARAM_GROUP_NAME, group_out, sizeof(group_out), &len))) goto end; ret = 1; end: BN_free(p_in); BN_free(q_in); BN_free(g_in); BN_free(p_out); BN_free(q_out); BN_free(g_out); EVP_PKEY_free(param_key); EVP_PKEY_free(key); EVP_PKEY_CTX_free(kg_ctx); EVP_PKEY_CTX_free(pg_ctx); return ret; } static int test_dsa_default_paramgen_validate(int i) { int ret; EVP_PKEY_CTX *gen_ctx = NULL; EVP_PKEY_CTX *check_ctx = NULL; EVP_PKEY *params = NULL; ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) && TEST_int_gt(EVP_PKEY_paramgen_init(gen_ctx), 0) && (i == 0 || TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(gen_ctx, 512))) && TEST_int_gt(EVP_PKEY_generate(gen_ctx, &params), 0) && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, params, NULL)) && TEST_int_gt(EVP_PKEY_param_check(check_ctx), 0); EVP_PKEY_free(params); EVP_PKEY_CTX_free(check_ctx); EVP_PKEY_CTX_free(gen_ctx); return ret; } static int test_dsa_sig_infinite_loop(void) { int ret = 0; DSA *dsa = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *priv = NULL, *pub = NULL, *priv2 = NULL; BIGNUM *badq = NULL, *badpriv = NULL; const unsigned char msg[] = { 0x00 }; unsigned int signature_len; unsigned char signature[64]; static unsigned char out_priv[] = { 0x17, 0x00, 0xb2, 0x8d, 0xcb, 0x24, 0xc9, 0x98, 0xd0, 0x7f, 0x1f, 0x83, 0x1a, 0xa1, 0xc4, 0xa4, 0xf8, 0x0f, 0x7f, 0x12 }; static unsigned char out_pub[] = { 0x04, 0x72, 0xee, 0x8d, 0xaa, 0x4d, 0x89, 0x60, 0x0e, 0xb2, 0xd4, 0x38, 0x84, 0xa2, 0x2a, 0x60, 0x5f, 0x67, 0xd7, 0x9e, 0x24, 0xdd, 0xe8, 0x50, 0xf2, 0x23, 0x71, 0x55, 0x53, 0x94, 0x0d, 0x6b, 0x2e, 0xcd, 0x30, 0xda, 0x6f, 0x1e, 0x2c, 0xcf, 0x59, 0xbe, 0x05, 0x6c, 0x07, 0x0e, 0xc6, 0x38, 0x05, 0xcb, 0x0c, 0x44, 0x0a, 0x08, 0x13, 0xb6, 0x0f, 0x14, 0xde, 0x4a, 0xf6, 0xed, 0x4e, 0xc3 }; if (!TEST_ptr(p = BN_bin2bn(out_p, sizeof(out_p), NULL)) || !TEST_ptr(q = BN_bin2bn(out_q, sizeof(out_q), NULL)) || !TEST_ptr(g = BN_bin2bn(out_g, sizeof(out_g), NULL)) || !TEST_ptr(pub = BN_bin2bn(out_pub, sizeof(out_pub), NULL)) || !TEST_ptr(priv = BN_bin2bn(out_priv, sizeof(out_priv), NULL)) || !TEST_ptr(priv2 = BN_dup(priv)) || !TEST_ptr(badq = BN_new()) || !TEST_true(BN_set_word(badq, 1)) || !TEST_ptr(badpriv = BN_new()) || !TEST_true(BN_set_word(badpriv, 0)) || !TEST_ptr(dsa = DSA_new())) goto err; if (!TEST_true(DSA_set0_pqg(dsa, p, q, g))) goto err; p = q = g = NULL; if (!TEST_true(DSA_set0_key(dsa, pub, priv))) goto err; pub = priv = NULL; if (!TEST_int_le(DSA_size(dsa), sizeof(signature))) goto err; if (!TEST_true(DSA_sign(0, msg, sizeof(msg), NULL, &signature_len, dsa))) goto err; if (!TEST_true(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; if (!TEST_true(DSA_set0_key(dsa, NULL, badpriv))) goto err; badpriv = NULL; if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; if (!TEST_true(DSA_set0_key(dsa, NULL, priv2))) goto err; priv2 = NULL; if (!TEST_true(DSA_set0_pqg(dsa, NULL, badq, NULL))) goto err; badq = NULL; if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; ret = 1; err: BN_free(badq); BN_free(badpriv); BN_free(pub); BN_free(priv); BN_free(priv2); BN_free(g); BN_free(q); BN_free(p); DSA_free(dsa); return ret; } static int test_dsa_sig_neg_param(void) { int ret = 0, setpqg = 0; DSA *dsa = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *priv = NULL, *pub = NULL; const unsigned char msg[] = { 0x00 }; unsigned int signature_len; unsigned char signature[64]; static unsigned char out_priv[] = { 0x17, 0x00, 0xb2, 0x8d, 0xcb, 0x24, 0xc9, 0x98, 0xd0, 0x7f, 0x1f, 0x83, 0x1a, 0xa1, 0xc4, 0xa4, 0xf8, 0x0f, 0x7f, 0x12 }; static unsigned char out_pub[] = { 0x04, 0x72, 0xee, 0x8d, 0xaa, 0x4d, 0x89, 0x60, 0x0e, 0xb2, 0xd4, 0x38, 0x84, 0xa2, 0x2a, 0x60, 0x5f, 0x67, 0xd7, 0x9e, 0x24, 0xdd, 0xe8, 0x50, 0xf2, 0x23, 0x71, 0x55, 0x53, 0x94, 0x0d, 0x6b, 0x2e, 0xcd, 0x30, 0xda, 0x6f, 0x1e, 0x2c, 0xcf, 0x59, 0xbe, 0x05, 0x6c, 0x07, 0x0e, 0xc6, 0x38, 0x05, 0xcb, 0x0c, 0x44, 0x0a, 0x08, 0x13, 0xb6, 0x0f, 0x14, 0xde, 0x4a, 0xf6, 0xed, 0x4e, 0xc3 }; if (!TEST_ptr(p = BN_bin2bn(out_p, sizeof(out_p), NULL)) || !TEST_ptr(q = BN_bin2bn(out_q, sizeof(out_q), NULL)) || !TEST_ptr(g = BN_bin2bn(out_g, sizeof(out_g), NULL)) || !TEST_ptr(pub = BN_bin2bn(out_pub, sizeof(out_pub), NULL)) || !TEST_ptr(priv = BN_bin2bn(out_priv, sizeof(out_priv), NULL)) || !TEST_ptr(dsa = DSA_new())) goto err; if (!TEST_true(DSA_set0_pqg(dsa, p, q, g))) goto err; setpqg = 1; if (!TEST_true(DSA_set0_key(dsa, pub, priv))) goto err; pub = priv = NULL; BN_set_negative(p, 1); if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; BN_set_negative(p, 0); BN_set_negative(q, 1); if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; BN_set_negative(q, 0); BN_set_negative(g, 1); if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; BN_set_negative(p, 1); BN_set_negative(q, 1); BN_set_negative(g, 1); if (!TEST_false(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))) goto err; ret = 1; err: BN_free(pub); BN_free(priv); if (setpqg == 0) { BN_free(g); BN_free(q); BN_free(p); } DSA_free(dsa); return ret; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_DSA ADD_TEST(dsa_test); ADD_TEST(dsa_keygen_test); ADD_TEST(test_dsa_sig_infinite_loop); ADD_TEST(test_dsa_sig_neg_param); ADD_ALL_TESTS(test_dsa_default_paramgen_validate, 2); #endif return 1; }
test
openssl/test/dsatest.c
openssl
#include <string.h> #include <openssl/core_names.h> #include <openssl/core_object.h> #include <openssl/rand.h> #include <openssl/provider.h> #include "testutil.h" #include "fake_rsaprov.h" static OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new; static OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free; static OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has; static OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query; static OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import; static OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes; static OSSL_FUNC_keymgmt_export_fn fake_rsa_keymgmt_export; static OSSL_FUNC_keymgmt_export_types_fn fake_rsa_keymgmt_exptypes; static OSSL_FUNC_keymgmt_load_fn fake_rsa_keymgmt_load; static int has_selection; static int imptypes_selection; static int exptypes_selection; static int query_id; static int key_deleted; struct fake_rsa_keydata { int selection; int status; }; void fake_rsa_restore_store_state(void) { key_deleted = 0; } static void *fake_rsa_keymgmt_new(void *provctx) { struct fake_rsa_keydata *key; if (!TEST_ptr(key = OPENSSL_zalloc(sizeof(struct fake_rsa_keydata)))) return NULL; has_selection = 0; imptypes_selection = 0; exptypes_selection = 0; query_id = 0; return key; } static void fake_rsa_keymgmt_free(void *keydata) { OPENSSL_free(keydata); } static int fake_rsa_keymgmt_has(const void *key, int selection) { has_selection = selection; return 1; } static const char *fake_rsa_keymgmt_query(int id) { query_id = id; return "RSA"; } static int fake_rsa_keymgmt_import(void *keydata, int selection, const OSSL_PARAM *p) { struct fake_rsa_keydata *fake_rsa_key = keydata; fake_rsa_key->status = 1; return 1; } static unsigned char fake_rsa_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 fake_rsa_e[] = "\x11"; static unsigned char fake_rsa_d[] = "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44" "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64" "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9" "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51"; static unsigned char fake_rsa_p[] = "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5" "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12" "\x0D"; static unsigned char fake_rsa_q[] = "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9" "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D" "\x89"; static unsigned char fake_rsa_dmp1[] = "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF" "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05"; static unsigned char fake_rsa_dmq1[] = "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99" "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D" "\x51"; static unsigned char fake_rsa_iqmp[] = "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8" "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26"; OSSL_PARAM *fake_rsa_key_params(int priv) { if (priv) { OSSL_PARAM params[] = { OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n, sizeof(fake_rsa_n) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e, sizeof(fake_rsa_e) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, fake_rsa_d, sizeof(fake_rsa_d) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, fake_rsa_p, sizeof(fake_rsa_p) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, fake_rsa_q, sizeof(fake_rsa_q) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, fake_rsa_dmp1, sizeof(fake_rsa_dmp1) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, fake_rsa_dmq1, sizeof(fake_rsa_dmq1) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, fake_rsa_iqmp, sizeof(fake_rsa_iqmp) -1), OSSL_PARAM_END }; return OSSL_PARAM_dup(params); } else { OSSL_PARAM params[] = { OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n, sizeof(fake_rsa_n) -1), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e, sizeof(fake_rsa_e) -1), OSSL_PARAM_END }; return OSSL_PARAM_dup(params); } } static int fake_rsa_keymgmt_export(void *keydata, int selection, OSSL_CALLBACK *param_callback, void *cbarg) { OSSL_PARAM *params = NULL; int ret; if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) return 0; if (!TEST_ptr(params = fake_rsa_key_params(0))) return 0; ret = param_callback(params, cbarg); OSSL_PARAM_free(params); return ret; } static const OSSL_PARAM fake_rsa_import_key_types[] = { OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection) { imptypes_selection = selection; return fake_rsa_import_key_types; } static const OSSL_PARAM fake_rsa_export_key_types[] = { OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), OSSL_PARAM_END }; static const OSSL_PARAM *fake_rsa_keymgmt_exptypes(int selection) { exptypes_selection = selection; return fake_rsa_export_key_types; } static void *fake_rsa_keymgmt_load(const void *reference, size_t reference_sz) { struct fake_rsa_keydata *key = NULL; if (reference_sz != sizeof(*key)) return NULL; key = *(struct fake_rsa_keydata **)reference; if (key->status != 1) return NULL; *(struct fake_rsa_keydata **)reference = NULL; return key; } static void *fake_rsa_gen_init(void *provctx, int selection, const OSSL_PARAM params[]) { unsigned char *gctx = NULL; if (!TEST_ptr(gctx = OPENSSL_malloc(1))) return NULL; *gctx = 1; return gctx; } static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { unsigned char *gctx = genctx; static const unsigned char inited[] = { 1 }; struct fake_rsa_keydata *keydata; if (!TEST_ptr(gctx) || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited))) return NULL; if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL))) return NULL; keydata->status = 2; return keydata; } static void fake_rsa_gen_cleanup(void *genctx) { OPENSSL_free(genctx); } static const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = { { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new }, { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} , { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has }, { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void (*)(void))fake_rsa_keymgmt_query }, { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))fake_rsa_keymgmt_imptypes }, { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))fake_rsa_keymgmt_export }, { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))fake_rsa_keymgmt_exptypes }, { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))fake_rsa_keymgmt_load }, { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init }, { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen }, { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = { { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" }, { NULL, NULL, NULL, NULL } }; static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx; static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx; static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init; static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign; static void *fake_rsa_sig_newctx(void *provctx, const char *propq) { unsigned char *sigctx = OPENSSL_zalloc(1); TEST_ptr(sigctx); return sigctx; } static void fake_rsa_sig_freectx(void *sigctx) { OPENSSL_free(sigctx); } static int fake_rsa_sig_sign_init(void *ctx, void *provkey, const OSSL_PARAM params[]) { unsigned char *sigctx = ctx; struct fake_rsa_keydata *keydata = provkey; if (!TEST_ptr(sigctx)) return 0; if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0)) return 0; *sigctx = 1; return 1; } static int fake_rsa_sig_sign(void *ctx, unsigned char *sig, size_t *siglen, size_t sigsize, const unsigned char *tbs, size_t tbslen) { unsigned char *sigctx = ctx; if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1)) return 0; *siglen = 256; if (sig != NULL) { if (!TEST_int_ge(sigsize, *siglen)) return 0; *sigctx = 2; memset(sig, 'a', *siglen); } return 1; } #define FAKE_DGSTSGN_SIGN 0x01 #define FAKE_DGSTSGN_VERIFY 0x02 #define FAKE_DGSTSGN_UPDATED 0x04 #define FAKE_DGSTSGN_FINALISED 0x08 #define FAKE_DGSTSGN_NO_DUP 0xA0 static void *fake_rsa_sig_dupctx(void *ctx) { unsigned char *sigctx = ctx; unsigned char *newctx; if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0) return NULL; if (!TEST_ptr(newctx = OPENSSL_zalloc(1))) return NULL; *newctx = *sigctx; return newctx; } static int fake_rsa_dgstsgnvfy_init(void *ctx, unsigned char type, void *provkey, const OSSL_PARAM params[]) { unsigned char *sigctx = ctx; struct fake_rsa_keydata *keydata = provkey; if (!TEST_ptr(sigctx)) return 0; if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0)) return 0; *sigctx = type; if (params) { const OSSL_PARAM *p; int dup; p = OSSL_PARAM_locate_const(params, "NO_DUP"); if (p != NULL) { if (OSSL_PARAM_get_int(p, &dup)) { *sigctx |= FAKE_DGSTSGN_NO_DUP; } } } return 1; } static int fake_rsa_dgstsgn_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) { return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_SIGN, provkey, params); } static int fake_rsa_dgstvfy_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) { return fake_rsa_dgstsgnvfy_init(ctx, FAKE_DGSTSGN_VERIFY, provkey, params); } static int fake_rsa_dgstsgnvfy_update(void *ctx, const unsigned char *data, size_t datalen) { unsigned char *sigctx = ctx; if (!TEST_ptr(sigctx)) return 0; if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0) return 0; *sigctx |= FAKE_DGSTSGN_UPDATED; return 1; } static int fake_rsa_dgstsgnvfy_final(void *ctx, unsigned char *sig, size_t *siglen, size_t sigsize) { unsigned char *sigctx = ctx; if (!TEST_ptr(sigctx)) return 0; if (*sigctx == 0 || (*sigctx & FAKE_DGSTSGN_FINALISED) != 0) return 0; if ((*sigctx & FAKE_DGSTSGN_SIGN) != 0 && (siglen == NULL)) return 0; if ((*sigctx & FAKE_DGSTSGN_VERIFY) != 0 && (siglen != NULL)) return 0; if (siglen) { *siglen = 256; if (sig != NULL) { if (!TEST_int_ge(sigsize, *siglen)) return 0; memset(sig, 'a', *siglen); } } if ((*sigctx & FAKE_DGSTSGN_NO_DUP) != 0) { *sigctx |= FAKE_DGSTSGN_FINALISED; } return 1; } static int fake_rsa_dgstvfy_final(void *ctx, unsigned char *sig, size_t siglen) { return fake_rsa_dgstsgnvfy_final(ctx, sig, NULL, siglen); } static int fake_rsa_dgstsgn(void *ctx, unsigned char *sig, size_t *siglen, size_t sigsize, const unsigned char *tbs, size_t tbslen) { if (!fake_rsa_dgstsgnvfy_update(ctx, tbs, tbslen)) return 0; return fake_rsa_dgstsgnvfy_final(ctx, sig, siglen, sigsize); } static int fake_rsa_dgstvfy(void *ctx, unsigned char *sig, size_t siglen, const unsigned char *tbv, size_t tbvlen) { if (!fake_rsa_dgstsgnvfy_update(ctx, tbv, tbvlen)) return 0; return fake_rsa_dgstvfy_final(ctx, sig, siglen); } static const OSSL_DISPATCH fake_rsa_sig_funcs[] = { { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx }, { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx }, { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init }, { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign }, { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))fake_rsa_sig_dupctx }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void (*)(void))fake_rsa_dgstsgn_init }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void (*)(void))fake_rsa_dgstsgnvfy_update }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, (void (*)(void))fake_rsa_dgstsgnvfy_final }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN, (void (*)(void))fake_rsa_dgstsgn }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void (*)(void))fake_rsa_dgstvfy_init }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void (*)(void))fake_rsa_dgstsgnvfy_update }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, (void (*)(void))fake_rsa_dgstvfy_final }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, (void (*)(void))fake_rsa_dgstvfy }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM fake_rsa_sig_algs[] = { { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" }, { NULL, NULL, NULL, NULL } }; static OSSL_FUNC_store_open_fn fake_rsa_st_open; static OSSL_FUNC_store_open_ex_fn fake_rsa_st_open_ex; static OSSL_FUNC_store_settable_ctx_params_fn fake_rsa_st_settable_ctx_params; static OSSL_FUNC_store_set_ctx_params_fn fake_rsa_st_set_ctx_params; static OSSL_FUNC_store_load_fn fake_rsa_st_load; static OSSL_FUNC_store_eof_fn fake_rsa_st_eof; static OSSL_FUNC_store_close_fn fake_rsa_st_close; static OSSL_FUNC_store_delete_fn fake_rsa_st_delete; static const char fake_rsa_scheme[] = "fake_rsa:"; static const char fake_rsa_openpwtest[] = "fake_rsa:openpwtest"; static const char fake_rsa_prompt[] = "Fake Prompt Info"; static void *fake_rsa_st_open_ex(void *provctx, const char *uri, const OSSL_PARAM params[], OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { unsigned char *storectx = NULL; if (strncmp(uri, fake_rsa_scheme, sizeof(fake_rsa_scheme) - 1) != 0) return NULL; if (strncmp(uri, fake_rsa_openpwtest, sizeof(fake_rsa_openpwtest) - 1) == 0) { const char *pw_check = FAKE_PASSPHRASE; char fakepw[sizeof(FAKE_PASSPHRASE) + 1] = { 0 }; size_t fakepw_len = 0; OSSL_PARAM pw_params[2] = { OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, (void *)fake_rsa_prompt, sizeof(fake_rsa_prompt) - 1), OSSL_PARAM_END, }; if (pw_cb == NULL) { return NULL; } if (!pw_cb(fakepw, sizeof(fakepw), &fakepw_len, pw_params, pw_cbarg)) { TEST_info("fake_rsa_open_ex failed passphrase callback"); return NULL; } if (strncmp(pw_check, fakepw, sizeof(pw_check) - 1) != 0) { TEST_info("fake_rsa_open_ex failed passphrase check"); return NULL; } } storectx = OPENSSL_zalloc(1); if (!TEST_ptr(storectx)) return NULL; TEST_info("fake_rsa_open_ex called"); return storectx; } static void *fake_rsa_st_open(void *provctx, const char *uri) { unsigned char *storectx = NULL; storectx = fake_rsa_st_open_ex(provctx, uri, NULL, NULL, NULL); TEST_info("fake_rsa_open called"); return storectx; } static const OSSL_PARAM *fake_rsa_st_settable_ctx_params(void *provctx) { static const OSSL_PARAM known_settable_ctx_params[] = { OSSL_PARAM_END }; return known_settable_ctx_params; } static int fake_rsa_st_set_ctx_params(void *loaderctx, const OSSL_PARAM params[]) { return 1; } static int fake_rsa_st_load(void *loaderctx, OSSL_CALLBACK *object_cb, void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { unsigned char *storectx = loaderctx; OSSL_PARAM params[4]; int object_type = OSSL_OBJECT_PKEY; struct fake_rsa_keydata *key = NULL; int rv = 0; switch (*storectx) { case 0: if (key_deleted == 1) { *storectx = 1; break; } if (!TEST_ptr(key = fake_rsa_keymgmt_new(NULL))) break; if (!TEST_int_gt(fake_rsa_keymgmt_import(key, 0, NULL), 0)) break; params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, "RSA", 0); params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, &key, sizeof(*key)); params[3] = OSSL_PARAM_construct_end(); rv = object_cb(params, object_cbarg); *storectx = 1; break; case 2: TEST_info("fake_rsa_load() called in error state"); break; default: TEST_info("fake_rsa_load() called in eof state"); break; } TEST_info("fake_rsa_load called - rv: %d", rv); if (rv == 0 && key_deleted == 0) { fake_rsa_keymgmt_free(key); *storectx = 2; } return rv; } static int fake_rsa_st_delete(void *loaderctx, const char *uri, const OSSL_PARAM params[], OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { key_deleted = 1; return 1; } static int fake_rsa_st_eof(void *loaderctx) { unsigned char *storectx = loaderctx; return *storectx != 0; } static int fake_rsa_st_close(void *loaderctx) { OPENSSL_free(loaderctx); return 1; } static const OSSL_DISPATCH fake_rsa_store_funcs[] = { { OSSL_FUNC_STORE_OPEN, (void (*)(void))fake_rsa_st_open }, { OSSL_FUNC_STORE_OPEN_EX, (void (*)(void))fake_rsa_st_open_ex }, { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS, (void (*)(void))fake_rsa_st_settable_ctx_params }, { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))fake_rsa_st_set_ctx_params }, { OSSL_FUNC_STORE_LOAD, (void (*)(void))fake_rsa_st_load }, { OSSL_FUNC_STORE_EOF, (void (*)(void))fake_rsa_st_eof }, { OSSL_FUNC_STORE_CLOSE, (void (*)(void))fake_rsa_st_close }, { OSSL_FUNC_STORE_DELETE, (void (*)(void))fake_rsa_st_delete }, OSSL_DISPATCH_END, }; static const OSSL_ALGORITHM fake_rsa_store_algs[] = { { "fake_rsa", "provider=fake-rsa", fake_rsa_store_funcs }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *fake_rsa_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; switch (operation_id) { case OSSL_OP_SIGNATURE: return fake_rsa_sig_algs; case OSSL_OP_KEYMGMT: return fake_rsa_keymgmt_algs; case OSSL_OP_STORE: return fake_rsa_store_algs; } return NULL; } static const OSSL_DISPATCH fake_rsa_method[] = { { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query }, OSSL_DISPATCH_END }; static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new())) return 0; *out = fake_rsa_method; return 1; } OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx) { OSSL_PROVIDER *p; if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa", fake_rsa_provider_init)) || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1))) return NULL; return p; } void fake_rsa_finish(OSSL_PROVIDER *p) { OSSL_PROVIDER_unload(p); }
test
openssl/test/fake_rsaprov.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/buffer.h> #include <openssl/bio.h> #include "testutil.h" static int test_bio_memleak(void) { int ok = 0; BIO *bio; BUF_MEM bufmem; static const char str[] = "BIO test\n"; char buf[100]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; bufmem.length = sizeof(str); bufmem.data = (char *) str; bufmem.max = bufmem.length; BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE); BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str))) goto finish; if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str))) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_get_mem(void) { int ok = 0; BIO *bio = NULL; BUF_MEM *bufmem = NULL; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_get_mem_ptr(bio, &bufmem); if (!TEST_ptr(bufmem)) goto finish; if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0)) goto finish; BIO_free(bio); bio = NULL; if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12)) goto finish; ok = 1; finish: BIO_free(bio); BUF_MEM_free(bufmem); return ok; } static int test_bio_new_mem_buf(void) { int ok = 0; BIO *bio; BUF_MEM *bufmem; char data[16]; bio = BIO_new_mem_buf("Hello World\n", 12); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 5), 5)) goto finish; if (!TEST_mem_eq(data, 5, "Hello", 5)) goto finish; if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0)) goto finish; if (!TEST_int_lt(BIO_write(bio, "test", 4), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_rdonly_mem_buf(void) { int ok = 0; BIO *bio, *bio2 = NULL; BUF_MEM *bufmem; char data[16]; bio = BIO_new_mem_buf("Hello World\n", 12); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 5), 5)) goto finish; if (!TEST_mem_eq(data, 5, "Hello", 5)) goto finish; if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0)) goto finish; (void)BIO_set_close(bio, BIO_NOCLOSE); bio2 = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio2)) goto finish; BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE); BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio2, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; if (!TEST_int_gt(BIO_reset(bio2), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio2, data, 16), 7)) goto finish; if (!TEST_mem_eq(data, 7, " World\n", 7)) goto finish; ok = 1; finish: BIO_free(bio); BIO_free(bio2); return ok; } static int test_bio_rdwr_rdonly(void) { int ok = 0; BIO *bio = NULL; char data[16]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY); if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 16)) goto finish; if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int test_bio_nonclear_rst(void) { int ok = 0; BIO *bio = NULL; char data[16]; bio = BIO_new(BIO_s_mem()); if (!TEST_ptr(bio)) goto finish; if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12)) goto finish; BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST); if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_eq(BIO_read(bio, data, 16), 12)) goto finish; if (!TEST_mem_eq(data, 12, "Hello World\n", 12)) goto finish; BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST); if (!TEST_int_gt(BIO_reset(bio), 0)) goto finish; if (!TEST_int_lt(BIO_read(bio, data, 16), 1)) goto finish; ok = 1; finish: BIO_free(bio); return ok; } static int error_callback_fired; static long BIO_error_callback(BIO *bio, int cmd, const char *argp, size_t len, int argi, long argl, int ret, size_t *processed) { if ((cmd & (BIO_CB_READ | BIO_CB_RETURN)) != 0) { error_callback_fired = 1; ret = 0; } return ret; } static int test_bio_i2d_ASN1_mime(void) { int ok = 0; BIO *bio = NULL, *out = NULL; BUF_MEM bufmem; static const char str[] = "BIO mime test\n"; PKCS7 *p7 = NULL; if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto finish; bufmem.length = sizeof(str); bufmem.data = (char *) str; bufmem.max = bufmem.length; BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE); BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY); BIO_set_callback_ex(bio, BIO_error_callback); if (!TEST_ptr(out = BIO_new(BIO_s_mem()))) goto finish; if (!TEST_ptr(p7 = PKCS7_new())) goto finish; if (!TEST_true(PKCS7_set_type(p7, NID_pkcs7_data))) goto finish; error_callback_fired = 0; if (!TEST_false(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio, SMIME_STREAM | SMIME_BINARY, ASN1_ITEM_rptr(PKCS7)))) goto finish; if (!TEST_int_eq(error_callback_fired, 1)) goto finish; ok = 1; finish: BIO_free(bio); BIO_free(out); PKCS7_free(p7); return ok; } int setup_tests(void) { ADD_TEST(test_bio_memleak); ADD_TEST(test_bio_get_mem); ADD_TEST(test_bio_new_mem_buf); ADD_TEST(test_bio_rdonly_mem_buf); ADD_TEST(test_bio_rdwr_rdonly); ADD_TEST(test_bio_nonclear_rst); ADD_TEST(test_bio_i2d_ASN1_mime); return 1; }
test
openssl/test/bio_memleak_test.c
openssl
#include <openssl/core.h> #include <openssl/core_dispatch.h> #include <openssl/core_names.h> #include <openssl/provider.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include "testutil.h" #define MYPROPERTIES "foo.bar=yes" static OSSL_FUNC_provider_query_operation_fn testprov_query; static OSSL_FUNC_digest_get_params_fn tmpmd_get_params; static OSSL_FUNC_digest_digest_fn tmpmd_digest; static int tmpmd_get_params(OSSL_PARAM params[]) { OSSL_PARAM *p = NULL; p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); if (p != NULL && !OSSL_PARAM_set_size_t(p, 1)) return 0; p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); if (p != NULL && !OSSL_PARAM_set_size_t(p, 1)) return 0; return 1; } static int tmpmd_digest(void *provctx, const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, size_t outsz) { return 0; } static const OSSL_DISPATCH testprovmd_functions[] = { { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))tmpmd_get_params }, { OSSL_FUNC_DIGEST_DIGEST, (void (*)(void))tmpmd_digest }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM testprov_digests[] = { { "testprovmd", MYPROPERTIES, testprovmd_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *testprov_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; return operation_id == OSSL_OP_DIGEST ? testprov_digests : NULL; } static const OSSL_DISPATCH testprov_dispatch_table[] = { { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))testprov_query }, OSSL_DISPATCH_END }; static int testprov_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { *provctx = (void *)handle; *out = testprov_dispatch_table; return 1; } enum { DEFAULT_PROPS_FIRST = 0, DEFAULT_PROPS_AFTER_LOAD, DEFAULT_PROPS_AFTER_FETCH, DEFAULT_PROPS_FINAL }; static int test_default_props_and_providers(int propsorder) { OSSL_LIB_CTX *libctx; OSSL_PROVIDER *testprov = NULL; EVP_MD *testprovmd = NULL; int res = 0; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()) || !TEST_true(OSSL_PROVIDER_add_builtin(libctx, "testprov", testprov_provider_init))) goto err; if (propsorder == DEFAULT_PROPS_FIRST && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; if (!TEST_ptr(testprov = OSSL_PROVIDER_load(libctx, "testprov"))) goto err; if (propsorder == DEFAULT_PROPS_AFTER_LOAD && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL))) goto err; if (propsorder == DEFAULT_PROPS_AFTER_FETCH) { if (!TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES))) goto err; EVP_MD_free(testprovmd); if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL))) goto err; } res = 1; err: EVP_MD_free(testprovmd); OSSL_PROVIDER_unload(testprov); OSSL_LIB_CTX_free(libctx); return res; } int setup_tests(void) { ADD_ALL_TESTS(test_default_props_and_providers, DEFAULT_PROPS_FINAL); return 1; }
test
openssl/test/user_property_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/rand.h> #include <openssl/asn1t.h> #include "internal/numbers.h" #include "testutil.h" #ifdef __GNUC__ # pragma GCC diagnostic ignored "-Wunused-function" # pragma GCC diagnostic ignored "-Wformat" #endif #ifdef __clang__ # pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wformat" #endif static unsigned char t_zero[] = { 0x00 }; static unsigned char t_one[] = { 0x01 }; static unsigned char t_one_neg[] = { 0xff }; static unsigned char t_minus_256[] = { 0xff, 0x00 }; static unsigned char t_longundef[] = { 0x7f, 0xff, 0xff, 0xff }; static unsigned char t_9bytes_1[] = { 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static unsigned char t_8bytes_1[] = { 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static unsigned char t_8bytes_2[] = { 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static unsigned char t_8bytes_3_pad[] = { 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static unsigned char t_8bytes_4_neg[] = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static unsigned char t_8bytes_5_negpad[] = { 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static unsigned char t_5bytes_1[] = { 0x01, 0xff, 0xff, 0xff, 0xff }; static unsigned char t_4bytes_1[] = { 0x00, 0x80, 0x00, 0x00, 0x00 }; static unsigned char t_4bytes_2[] = { 0x7f, 0xff, 0xff, 0xfe }; static unsigned char t_4bytes_3_pad[] = { 0x00, 0x7f, 0xff, 0xff, 0xfe }; static unsigned char t_4bytes_4_neg[] = { 0x80, 0x00, 0x00, 0x00 }; static unsigned char t_4bytes_5_negpad[] = { 0xff, 0x80, 0x00, 0x00, 0x00 }; typedef struct { unsigned char *bytes1; size_t nbytes1; unsigned char *bytes2; size_t nbytes2; } TEST_CUSTOM_DATA; #define CUSTOM_DATA(v) \ { v, sizeof(v), t_one, sizeof(t_one) }, \ { t_one, sizeof(t_one), v, sizeof(v) } static TEST_CUSTOM_DATA test_custom_data[] = { CUSTOM_DATA(t_zero), CUSTOM_DATA(t_longundef), CUSTOM_DATA(t_one), CUSTOM_DATA(t_one_neg), CUSTOM_DATA(t_minus_256), CUSTOM_DATA(t_9bytes_1), CUSTOM_DATA(t_8bytes_1), CUSTOM_DATA(t_8bytes_2), CUSTOM_DATA(t_8bytes_3_pad), CUSTOM_DATA(t_8bytes_4_neg), CUSTOM_DATA(t_8bytes_5_negpad), CUSTOM_DATA(t_5bytes_1), CUSTOM_DATA(t_4bytes_1), CUSTOM_DATA(t_4bytes_2), CUSTOM_DATA(t_4bytes_3_pad), CUSTOM_DATA(t_4bytes_4_neg), CUSTOM_DATA(t_4bytes_5_negpad), }; #define CUSTOM_EXPECTED_SUCCESS(num, znum) \ { 0xff, num, 1 }, \ { 0xff, 1, znum } #define CUSTOM_EXPECTED_FAILURE \ { 0, 0, 0 }, \ { 0, 0, 0 } typedef int i2d_fn(void *a, unsigned char **pp); typedef void *d2i_fn(void **a, unsigned char **pp, long length); typedef void ifree_fn(void *a); typedef struct { ASN1_ITEM_EXP *asn1_type; const char *name; int skip; void *encode_expectations; size_t encode_expectations_size; size_t encode_expectations_elem_size; void *encdec_data; size_t encdec_data_size; size_t encdec_data_elem_size; i2d_fn *i2d; d2i_fn *d2i; ifree_fn *ifree; } TEST_PACKAGE; #define ENCDEC_DATA(num, znum) \ { 0xff, num, 1 }, { 0xff, 1, znum } #define ENCDEC_ARRAY(max, zmax, min, zmin) \ ENCDEC_DATA(max,zmax), \ ENCDEC_DATA(min,zmin), \ ENCDEC_DATA(1, 1), \ ENCDEC_DATA(-1, -1), \ ENCDEC_DATA(0, ASN1_LONG_UNDEF) #ifndef OPENSSL_NO_DEPRECATED_3_0 typedef struct { ASN1_BOOLEAN success; long test_long; long test_zlong; } ASN1_LONG_DATA; ASN1_SEQUENCE(ASN1_LONG_DATA) = { ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_BOOLEAN), ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG), ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0) } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA) static ASN1_LONG_DATA long_expected_32bit[] = { { 0xff, 0, 1 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_SUCCESS(-1, -1), CUSTOM_EXPECTED_SUCCESS(-256, -256), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), CUSTOM_EXPECTED_FAILURE, }; static ASN1_LONG_DATA long_encdec_data_32bit[] = { ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN), { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 } }; static TEST_PACKAGE long_test_package_32bit = { ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4, long_expected_32bit, sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]), long_encdec_data_32bit, sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]), (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA, (ifree_fn *)ASN1_LONG_DATA_free }; static ASN1_LONG_DATA long_expected_64bit[] = { { 0xff, 0, 1 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_SUCCESS(-1, -1), CUSTOM_EXPECTED_SUCCESS(-256, -256), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), CUSTOM_EXPECTED_FAILURE, }; static ASN1_LONG_DATA long_encdec_data_64bit[] = { ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN), { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 } }; static TEST_PACKAGE long_test_package_64bit = { ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8, long_expected_64bit, sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]), long_encdec_data_64bit, sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]), (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA, (ifree_fn *)ASN1_LONG_DATA_free }; #endif typedef struct { ASN1_BOOLEAN success; int32_t test_int32; int32_t test_zint32; } ASN1_INT32_DATA; ASN1_SEQUENCE(ASN1_INT32_DATA) = { ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_BOOLEAN), ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32), ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0) } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA) static ASN1_INT32_DATA int32_expected[] = { CUSTOM_EXPECTED_SUCCESS(0, 0), CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_SUCCESS(-1, -1), CUSTOM_EXPECTED_SUCCESS(-256, -256), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), CUSTOM_EXPECTED_FAILURE, }; static ASN1_INT32_DATA int32_encdec_data[] = { ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN), }; static TEST_PACKAGE int32_test_package = { ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0, int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]), int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]), (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA, (ifree_fn *)ASN1_INT32_DATA_free }; typedef struct { ASN1_BOOLEAN success; uint32_t test_uint32; uint32_t test_zuint32; } ASN1_UINT32_DATA; ASN1_SEQUENCE(ASN1_UINT32_DATA) = { ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_BOOLEAN), ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32), ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0) } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA) static ASN1_UINT32_DATA uint32_expected[] = { CUSTOM_EXPECTED_SUCCESS(0, 0), CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, }; static ASN1_UINT32_DATA uint32_encdec_data[] = { ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0), }; static TEST_PACKAGE uint32_test_package = { ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0, uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]), uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]), (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA, (ifree_fn *)ASN1_UINT32_DATA_free }; typedef struct { ASN1_BOOLEAN success; int64_t test_int64; int64_t test_zint64; } ASN1_INT64_DATA; ASN1_SEQUENCE(ASN1_INT64_DATA) = { ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_BOOLEAN), ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64), ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0) } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA) static ASN1_INT64_DATA int64_expected[] = { CUSTOM_EXPECTED_SUCCESS(0, 0), CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_SUCCESS(-1, -1), CUSTOM_EXPECTED_SUCCESS(-256, -256), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), CUSTOM_EXPECTED_FAILURE, }; static ASN1_INT64_DATA int64_encdec_data[] = { ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN), ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN), }; static TEST_PACKAGE int64_test_package = { ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0, int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]), int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]), (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA, (ifree_fn *)ASN1_INT64_DATA_free }; typedef struct { ASN1_BOOLEAN success; uint64_t test_uint64; uint64_t test_zuint64; } ASN1_UINT64_DATA; ASN1_SEQUENCE(ASN1_UINT64_DATA) = { ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_BOOLEAN), ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64), ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0) } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA) static ASN1_UINT64_DATA uint64_expected[] = { CUSTOM_EXPECTED_SUCCESS(0, 0), CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), CUSTOM_EXPECTED_SUCCESS(1, 1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1), CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, CUSTOM_EXPECTED_FAILURE, }; static ASN1_UINT64_DATA uint64_encdec_data[] = { ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0), }; static TEST_PACKAGE uint64_test_package = { ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0, uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]), uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]), (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA, (ifree_fn *)ASN1_UINT64_DATA_free }; typedef struct { ASN1_BOOLEAN success; unsigned char bytes[1]; } EXPECTED; static int do_decode(unsigned char *bytes, long nbytes, const EXPECTED *expected, size_t expected_size, const TEST_PACKAGE *package) { EXPECTED *enctst = NULL; const unsigned char *start; int ret = 0; start = bytes; enctst = package->d2i(NULL, &bytes, nbytes); if (enctst == NULL) { if (expected->success == 0) { ret = 1; ERR_clear_error(); } else { ret = -1; } } else { if (start + nbytes == bytes && memcmp(enctst, expected, expected_size) == 0) ret = 1; else ret = 0; } package->ifree(enctst); return ret; } static int do_encode(EXPECTED *input, const unsigned char *expected, size_t expected_len, const TEST_PACKAGE *package) { unsigned char *data = NULL; int len; int ret = 0; len = package->i2d(input, &data); if (len < 0) return -1; if ((size_t)len != expected_len || memcmp(data, expected, expected_len) != 0) { if (input->success == 0) { ret = 1; ERR_clear_error(); } else { ret = 0; } } else { ret = 1; } OPENSSL_free(data); return ret; } static int do_enc_dec(EXPECTED *bytes, long nbytes, const TEST_PACKAGE *package) { unsigned char *data = NULL; int len; int ret = 0; void *p = bytes; len = package->i2d(p, &data); if (len < 0) return -1; ret = do_decode(data, len, bytes, nbytes, package); OPENSSL_free(data); return ret; } static size_t der_encode_length(size_t len, unsigned char **pp) { size_t lenbytes; OPENSSL_assert(len < 0x8000); if (len > 255) lenbytes = 3; else if (len > 127) lenbytes = 2; else lenbytes = 1; if (pp != NULL) { if (lenbytes == 1) { *(*pp)++ = (unsigned char)len; } else { *(*pp)++ = (unsigned char)(lenbytes - 1); if (lenbytes == 2) { *(*pp)++ = (unsigned char)(0x80 | len); } else { *(*pp)++ = (unsigned char)(0x80 | (len >> 8)); *(*pp)++ = (unsigned char)(len); } } } return lenbytes; } static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data, unsigned char **encoding, int explicit_default) { size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes; const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff }; unsigned char *p = NULL; size_t i; firstbytes = 1 + der_encode_length(custom_data->nbytes1, NULL) + custom_data->nbytes1; for (i = custom_data->nbytes2; i > 0; i--) { if (custom_data->bytes2[i - 1] != '\0') break; } if (explicit_default || i > 0) { secondbytesinner = 1 + der_encode_length(custom_data->nbytes2, NULL) + custom_data->nbytes2; secondbytes = 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner; } seqbytes = 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL) + sizeof(t_true) + firstbytes + secondbytes; *encoding = p = OPENSSL_malloc(seqbytes); if (*encoding == NULL) return 0; *p++ = 0x30; der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p); memcpy(p, t_true, sizeof(t_true)); p += sizeof(t_true); *p++ = V_ASN1_INTEGER; der_encode_length(custom_data->nbytes1, &p); memcpy(p, custom_data->bytes1, custom_data->nbytes1); p += custom_data->nbytes1; if (secondbytes > 0) { *p++ = 0xa0; der_encode_length(secondbytesinner, &p); *p++ = V_ASN1_INTEGER; der_encode_length(custom_data->nbytes2, &p); memcpy(p, custom_data->bytes2, custom_data->nbytes2); p += custom_data->nbytes2; } OPENSSL_assert(seqbytes == (size_t)(p - *encoding)); return seqbytes; } static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data, const EXPECTED *expected, size_t expected_size, const TEST_PACKAGE *package) { unsigned char *encoding = NULL; size_t encoding_length = make_custom_der(custom_data, &encoding, 1); int ret; if (encoding_length == 0) return -1; ret = do_decode(encoding, encoding_length, expected, expected_size, package); OPENSSL_free(encoding); return ret; } static int do_encode_custom(EXPECTED *input, const TEST_CUSTOM_DATA *custom_data, const TEST_PACKAGE *package) { unsigned char *expected = NULL; size_t expected_length = make_custom_der(custom_data, &expected, 0); int ret; if (expected_length == 0) return -1; ret = do_encode(input, expected, expected_length, package); OPENSSL_free(expected); return ret; } static int do_print_item(const TEST_PACKAGE *package) { #define DATA_BUF_SIZE 256 const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type); ASN1_VALUE *o; int ret; OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE); if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL) return 0; (void)RAND_bytes((unsigned char*)o, (int)package->encode_expectations_elem_size); ret = ASN1_item_print(bio_err, o, 0, i, NULL); OPENSSL_free(o); return ret; } static int test_intern(const TEST_PACKAGE *package) { unsigned int i; size_t nelems; int fail = 0; if (package->skip) return 1; nelems = package->encode_expectations_size / package->encode_expectations_elem_size; OPENSSL_assert(nelems == sizeof(test_custom_data) / sizeof(test_custom_data[0])); for (i = 0; i < nelems; i++) { size_t pos = i * package->encode_expectations_elem_size; EXPECTED *expected = (EXPECTED *)&((unsigned char *)package->encode_expectations)[pos]; switch (do_encode_custom(expected, &test_custom_data[i], package)) { case -1: if (expected->success) { TEST_error("Failed custom encode round trip %u of %s", i, package->name); TEST_openssl_errors(); fail++; } break; case 0: TEST_error("Custom encode round trip %u of %s mismatch", i, package->name); TEST_openssl_errors(); fail++; break; case 1: break; default: OPENSSL_die("do_encode_custom() return unknown value", __FILE__, __LINE__); } switch (do_decode_custom(&test_custom_data[i], expected, package->encode_expectations_elem_size, package)) { case -1: if (expected->success) { TEST_error("Failed custom decode round trip %u of %s", i, package->name); TEST_openssl_errors(); fail++; } break; case 0: TEST_error("Custom decode round trip %u of %s mismatch", i, package->name); TEST_openssl_errors(); fail++; break; case 1: break; default: OPENSSL_die("do_decode_custom() return unknown value", __FILE__, __LINE__); } } nelems = package->encdec_data_size / package->encdec_data_elem_size; for (i = 0; i < nelems; i++) { size_t pos = i * package->encdec_data_elem_size; EXPECTED *expected = (EXPECTED *)&((unsigned char *)package->encdec_data)[pos]; switch (do_enc_dec(expected, package->encdec_data_elem_size, package)) { case -1: if (expected->success) { TEST_error("Failed encode/decode round trip %u of %s", i, package->name); TEST_openssl_errors(); fail++; } break; case 0: TEST_error("Encode/decode round trip %u of %s mismatch", i, package->name); fail++; break; case 1: break; default: OPENSSL_die("do_enc_dec() return unknown value", __FILE__, __LINE__); } } if (!do_print_item(package)) { TEST_error("Printing of %s failed", package->name); TEST_openssl_errors(); fail++; } return fail == 0; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_long_32bit(void) { return test_intern(&long_test_package_32bit); } static int test_long_64bit(void) { return test_intern(&long_test_package_64bit); } #endif static int test_int32(void) { return test_intern(&int32_test_package); } static int test_uint32(void) { return test_intern(&uint32_test_package); } static int test_int64(void) { return test_intern(&int64_test_package); } static int test_uint64(void) { return test_intern(&uint64_test_package); } typedef struct { ASN1_STRING *invalidDirString; } INVALIDTEMPLATE; ASN1_SEQUENCE(INVALIDTEMPLATE) = { ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12) } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE) IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE) IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE) static int test_invalid_template(void) { INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new(); int ret; if (!TEST_ptr(temp)) return 0; ret = i2d_INVALIDTEMPLATE(temp, NULL); INVALIDTEMPLATE_free(temp); return ret < 0; } int setup_tests(void) { #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_long_32bit); ADD_TEST(test_long_64bit); #endif ADD_TEST(test_int32); ADD_TEST(test_uint32); ADD_TEST(test_int64); ADD_TEST(test_uint64); ADD_TEST(test_invalid_template); return 1; }
test
openssl/test/asn1_encode_test.c
openssl
#include <stdio.h> #include <string.h> #include <errno.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/conf.h> #include <openssl/err.h> #include "testutil.h" static int test_certs(int num) { int c; char *name = 0; char *header = 0; unsigned char *data = 0; long len; typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long); typedef int (*i2d_X509_t)(const X509 *, unsigned char **); int err = 0; BIO *fp = BIO_new_file(test_get_argument(num), "r"); if (!TEST_ptr(fp)) return 0; for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) { const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0); d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509; i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509; X509 *cert = NULL; X509 *reuse = NULL; const unsigned char *p = data; unsigned char *buf = NULL; unsigned char *bufp; long enclen; if (!trusted && strcmp(name, PEM_STRING_X509) != 0 && strcmp(name, PEM_STRING_X509_OLD) != 0) { TEST_error("unexpected PEM object: %s", name); err = 1; goto next; } cert = d2i(NULL, &p, len); if (cert == NULL || (p - data) != len) { TEST_error("error parsing input %s", name); err = 1; goto next; } enclen = i2d(cert, NULL); if (len != enclen) { TEST_error("encoded length %ld of %s != input length %ld", enclen, name, len); err = 1; goto next; } if ((buf = bufp = OPENSSL_malloc(len)) == NULL) { TEST_perror("malloc"); err = 1; goto next; } enclen = i2d(cert, &bufp); if (len != enclen) { TEST_error("encoded length %ld of %s != input length %ld", enclen, name, len); err = 1; goto next; } enclen = (long) (bufp - buf); if (enclen != len) { TEST_error("unexpected buffer position after encoding %s", name); err = 1; goto next; } if (memcmp(buf, data, len) != 0) { TEST_error("encoded content of %s does not match input", name); err = 1; goto next; } p = buf; reuse = d2i(NULL, &p, enclen); if (reuse == NULL) { TEST_error("second d2i call failed for %s", name); err = 1; goto next; } err = X509_cmp(reuse, cert); if (err != 0) { TEST_error("X509_cmp for %s resulted in %d", name, err); err = 1; goto next; } OPENSSL_free(buf); buf = NULL; enclen = i2d(cert, &buf); if (len != enclen) { TEST_error("encoded length %ld of %s != input length %ld", enclen, name, len); err = 1; goto next; } if (memcmp(buf, data, len) != 0) { TEST_error("encoded content of %s does not match input", name); err = 1; goto next; } if (trusted) { OPENSSL_free(buf); buf = NULL; enclen = i2d(cert, &buf); if (enclen > len) { TEST_error("encoded length %ld of %s > input length %ld", enclen, name, len); err = 1; goto next; } if (memcmp(buf, data, enclen) != 0) { TEST_error("encoded cert content does not match input"); err = 1; goto next; } } next: X509_free(cert); X509_free(reuse); OPENSSL_free(buf); OPENSSL_free(name); OPENSSL_free(header); OPENSSL_free(data); } BIO_free(fp); if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) { if (c > 0) { ERR_clear_error(); return 1; } } return 0; } OPT_TEST_DECLARE_USAGE("certfile...\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; ADD_ALL_TESTS(test_certs, (int)n); return 1; }
test
openssl/test/x509aux.c
openssl
#if defined(_WIN32) # include <windows.h> #endif #include "testutil.h" #include "threadstest.h" static int success; static void thread_fips_rand_fetch(void) { EVP_MD *md; if (!TEST_true(md = EVP_MD_fetch(NULL, "SHA2-256", NULL))) success = 0; EVP_MD_free(md); } static int test_fips_rand_leak(void) { thread_t thread; success = 1; if (!TEST_true(run_thread(&thread, thread_fips_rand_fetch))) return 0; if (!TEST_true(wait_for_thread(thread))) return 0; return TEST_true(success); } int setup_tests(void) { ADD_TEST(test_fips_rand_leak); return 1; }
test
openssl/test/threadstest_fips.c
openssl
#include <openssl/bio.h> #include "testutil.h" static const char *filename = NULL; static int test_readbuffer_file_bio(int tstid) { int ret = 0, len, partial; BIO *in = NULL, *in_bio = NULL, *readbuf_bio = NULL; char buf[255]; char expected[4096]; size_t readbytes = 0, bytes = 0, count = 0; if (!TEST_ptr(in = BIO_new_file(filename, "r")) || !TEST_int_eq(BIO_read_ex(in, expected, sizeof(expected), &readbytes), 1) || !TEST_int_lt(readbytes, sizeof(expected))) goto err; BIO_free(in); in = NULL; if (!TEST_ptr(readbuf_bio = BIO_new(BIO_f_readbuffer())) || !TEST_ptr(in_bio = BIO_new_file(filename, "r"))) goto err; in_bio = BIO_push(readbuf_bio, in_bio); readbuf_bio = NULL; if (!TEST_int_eq(BIO_tell(in_bio), 0)) goto err; if (tstid != 0) { partial = 4; while (!BIO_eof(in_bio)) { len = BIO_gets(in_bio, buf, sizeof(buf)); if (len == 0) { if (!TEST_true(BIO_eof(in_bio))) goto err; } else { if (!TEST_int_gt(len, 0) || !TEST_int_le(len, (int)sizeof(buf) - 1)) goto err; if (!TEST_true(buf[len] == 0)) goto err; if (len > 1 && !BIO_eof(in_bio) && len != ((int)sizeof(buf) - 1) && !TEST_true(buf[len - 1] == '\n')) goto err; } if (tstid == 1 && --partial == 0) break; } } if (!TEST_int_eq(BIO_seek(in_bio, 0), 1)) goto err; len = 8; while (!BIO_eof(in_bio)) { if (!TEST_int_eq(BIO_read_ex(in_bio, buf, len, &bytes), 1)) break; if (!TEST_mem_eq(buf, bytes, expected + count, bytes)) goto err; count += bytes; len = sizeof(buf); } if (!TEST_int_eq(count, readbytes)) goto err; ret = 1; err: BIO_free(in); BIO_free_all(in_bio); BIO_free(readbuf_bio); return ret; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file\n"), { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_TEST_CASES: break; default: return 0; } } filename = test_get_argument(0); ADD_ALL_TESTS(test_readbuffer_file_bio, 3); return 1; }
test
openssl/test/bio_readbuffer_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include "testutil.h" #include "internal/nelem.h" #include "../crypto/x509/ext_dat.h" #include "../crypto/x509/standard_exts.h" static int test_standard_exts(void) { size_t i; int prev = -1, good = 1; const X509V3_EXT_METHOD **tmp; tmp = standard_exts; for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) { if ((*tmp)->ext_nid < prev) good = 0; prev = (*tmp)->ext_nid; } if (!good) { tmp = standard_exts; TEST_error("Extensions out of order!"); for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++) TEST_note("%d : %s", (*tmp)->ext_nid, OBJ_nid2sn((*tmp)->ext_nid)); } return good; } typedef struct { const char *ipasc; const char *data; int length; } IP_TESTDATA; static IP_TESTDATA a2i_ipaddress_tests[] = { {"127.0.0.1", "\x7f\x00\x00\x01", 4}, {"1.2.3.4", "\x01\x02\x03\x04", 4}, {"1.2.3.255", "\x01\x02\x03\xff", 4}, {"1.2.3", NULL, 0}, {"1.2.3 .4", NULL, 0}, {"::1", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16}, {"1:1:1:1:1:1:1:1", "\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 16}, {"2001:db8::ff00:42:8329", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\xff\x00\x00\x42\x83\x29", 16}, {"1:1:1:1:1:1:1:1.test", NULL, 0}, {":::1", NULL, 0}, {"2001::123g", NULL, 0}, {"example.test", NULL, 0}, {"", NULL, 0}, {"1.2.3.4 ", "\x01\x02\x03\x04", 4}, {" 1.2.3.4", "\x01\x02\x03\x04", 4}, {" 1.2.3.4 ", "\x01\x02\x03\x04", 4}, {"1.2.3.4.example.test", NULL, 0}, }; static int test_a2i_ipaddress(int idx) { int good = 1; ASN1_OCTET_STRING *ip; int len = a2i_ipaddress_tests[idx].length; ip = a2i_IPADDRESS(a2i_ipaddress_tests[idx].ipasc); if (len == 0) { if (!TEST_ptr_null(ip)) { good = 0; TEST_note("'%s' should not be parsed as IP address", a2i_ipaddress_tests[idx].ipasc); } } else { if (!TEST_ptr(ip) || !TEST_int_eq(ASN1_STRING_length(ip), len) || !TEST_mem_eq(ASN1_STRING_get0_data(ip), len, a2i_ipaddress_tests[idx].data, len)) { good = 0; } } ASN1_OCTET_STRING_free(ip); return good; } int setup_tests(void) { ADD_TEST(test_standard_exts); ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests)); return 1; }
test
openssl/test/x509_internal_test.c
openssl
#include "internal/deprecated.h" #include <string.h> #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/rsa.h> #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/provider.h> #include <openssl/core_names.h> #include "internal/core.h" #include "internal/nelem.h" #include "crypto/evp.h" #include "testutil.h" typedef struct { OSSL_LIB_CTX *ctx1; OSSL_PROVIDER *prov1; OSSL_LIB_CTX *ctx2; OSSL_PROVIDER *prov2; } FIXTURE; static const char *cert_filename = NULL; static void tear_down(FIXTURE *fixture) { if (fixture != NULL) { OSSL_PROVIDER_unload(fixture->prov1); OSSL_PROVIDER_unload(fixture->prov2); OSSL_LIB_CTX_free(fixture->ctx1); OSSL_LIB_CTX_free(fixture->ctx2); OPENSSL_free(fixture); } } static FIXTURE *set_up(const char *testcase_name) { FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))) || !TEST_ptr(fixture->ctx1 = OSSL_LIB_CTX_new()) || !TEST_ptr(fixture->prov1 = OSSL_PROVIDER_load(fixture->ctx1, "default")) || !TEST_ptr(fixture->ctx2 = OSSL_LIB_CTX_new()) || !TEST_ptr(fixture->prov2 = OSSL_PROVIDER_load(fixture->ctx2, "default"))) { tear_down(fixture); return NULL; } return fixture; } #define N 0 #define E 1 #define D 2 #define P 3 #define Q 4 #define F3 5 #define DP 6 #define DQ 7 #define E3 8 #define QINV 9 #define C2 10 static int get_ulong_via_BN(const OSSL_PARAM *p, unsigned long *goal) { BIGNUM *n = NULL; int ret = 1; if (!TEST_true(OSSL_PARAM_get_BN(p, &n)) || !TEST_int_ge(BN_bn2nativepad(n, (unsigned char *)goal, sizeof(*goal)), 0)) ret = 0; BN_free(n); return ret; } static int export_cb(const OSSL_PARAM *params, void *arg) { unsigned long *keydata = arg; const OSSL_PARAM *p = NULL; if (keydata == NULL) return 0; if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N)) || !TEST_true(get_ulong_via_BN(p, &keydata[N])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) || !TEST_true(get_ulong_via_BN(p, &keydata[E])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D)) || !TEST_true(get_ulong_via_BN(p, &keydata[D]))) return 0; if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR1)) || !TEST_true(get_ulong_via_BN(p, &keydata[P])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR2)) || !TEST_true(get_ulong_via_BN(p, &keydata[Q])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR3)) || !TEST_true(get_ulong_via_BN(p, &keydata[F3]))) return 0; if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT1)) || !TEST_true(get_ulong_via_BN(p, &keydata[DP])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT2)) || !TEST_true(get_ulong_via_BN(p, &keydata[DQ])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT3)) || !TEST_true(get_ulong_via_BN(p, &keydata[E3]))) return 0; if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT1)) || !TEST_true(get_ulong_via_BN(p, &keydata[QINV])) || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT2)) || !TEST_true(get_ulong_via_BN(p, &keydata[C2]))) return 0; return 1; } static int test_pass_rsa(FIXTURE *fixture) { size_t i; int ret = 0; RSA *rsa = NULL; BIGNUM *bn1 = NULL, *bn2 = NULL, *bn3 = NULL; EVP_PKEY *pk = NULL, *dup_pk = NULL; EVP_KEYMGMT *km = NULL, *km1 = NULL, *km2 = NULL, *km3 = NULL; void *provkey = NULL, *provkey2 = NULL; BIGNUM *bn_primes[1] = { NULL }; BIGNUM *bn_exps[1] = { NULL }; BIGNUM *bn_coeffs[1] = { NULL }; static BN_ULONG expected[] = { 0xbc747fc5, 0x10001, 0x7b133399, 0xe963, 0xceb7, 1, 0x8599, 0xbd87, 2, 0xcc3b, 3, 0 }; static unsigned long keydata[OSSL_NELEM(expected)] = { 0, }; if (!TEST_ptr(rsa = RSA_new())) goto err; if (!TEST_ptr(bn1 = BN_new()) || !TEST_true(BN_set_word(bn1, expected[N])) || !TEST_ptr(bn2 = BN_new()) || !TEST_true(BN_set_word(bn2, expected[E])) || !TEST_ptr(bn3 = BN_new()) || !TEST_true(BN_set_word(bn3, expected[D])) || !TEST_true(RSA_set0_key(rsa, bn1, bn2, bn3))) goto err; if (!TEST_ptr(bn1 = BN_new()) || !TEST_true(BN_set_word(bn1, expected[P])) || !TEST_ptr(bn2 = BN_new()) || !TEST_true(BN_set_word(bn2, expected[Q])) || !TEST_true(RSA_set0_factors(rsa, bn1, bn2))) goto err; if (!TEST_ptr(bn1 = BN_new()) || !TEST_true(BN_set_word(bn1, expected[DP])) || !TEST_ptr(bn2 = BN_new()) || !TEST_true(BN_set_word(bn2, expected[DQ])) || !TEST_ptr(bn3 = BN_new()) || !TEST_true(BN_set_word(bn3, expected[QINV])) || !TEST_true(RSA_set0_crt_params(rsa, bn1, bn2, bn3))) goto err; bn1 = bn2 = bn3 = NULL; if (!TEST_ptr(bn_primes[0] = BN_new()) || !TEST_true(BN_set_word(bn_primes[0], expected[F3])) || !TEST_ptr(bn_exps[0] = BN_new()) || !TEST_true(BN_set_word(bn_exps[0], expected[E3])) || !TEST_ptr(bn_coeffs[0] = BN_new()) || !TEST_true(BN_set_word(bn_coeffs[0], expected[C2])) || !TEST_true(RSA_set0_multi_prime_params(rsa, bn_primes, bn_exps, bn_coeffs, 1))) goto err; if (!TEST_ptr(pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_RSA(pk, rsa))) goto err; rsa = NULL; if (!TEST_ptr(km1 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA", NULL)) || !TEST_ptr(km2 = EVP_KEYMGMT_fetch(fixture->ctx2, "RSA", NULL)) || !TEST_ptr(km3 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA-PSS", NULL)) || !TEST_ptr_ne(km1, km2)) goto err; for (;;) { ret = 0; km = km3; if (!TEST_ptr_null(provkey2 = evp_pkey_export_to_provider(pk, NULL, &km, NULL))) goto err; if (!TEST_ptr(provkey = evp_pkey_export_to_provider(pk, NULL, &km1, NULL)) || !TEST_true(evp_keymgmt_export(km2, provkey, OSSL_KEYMGMT_SELECT_KEYPAIR, &export_cb, keydata))) goto err; for (i = 0; i < OSSL_NELEM(expected); i++) { int rv = TEST_int_eq(expected[i], keydata[i]); if (!rv) TEST_info("i = %zu", i); else ret++; } ret = (ret == OSSL_NELEM(expected)); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: RSA_free(rsa); BN_free(bn1); BN_free(bn2); BN_free(bn3); EVP_PKEY_free(pk); EVP_KEYMGMT_free(km1); EVP_KEYMGMT_free(km2); EVP_KEYMGMT_free(km3); return ret; } static int (*tests[])(FIXTURE *) = { test_pass_rsa }; static int test_pass_key(int n) { SETUP_TEST_FIXTURE(FIXTURE, set_up); EXECUTE_TEST(tests[n], tear_down); return result; } static int test_evp_pkey_export_to_provider(int n) { OSSL_LIB_CTX *libctx = NULL; OSSL_PROVIDER *prov = NULL; X509 *cert = NULL; BIO *bio = NULL; X509_PUBKEY *pubkey = NULL; EVP_KEYMGMT *keymgmt = NULL; EVP_PKEY *pkey = NULL; void *keydata = NULL; int ret = 0; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()) || !TEST_ptr(prov = OSSL_PROVIDER_load(libctx, "default"))) goto end; if ((bio = BIO_new_file(cert_filename, "r")) == NULL) { TEST_error("Couldn't open '%s' for reading\n", cert_filename); TEST_openssl_errors(); goto end; } if ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n", cert_filename); TEST_openssl_errors(); goto end; } pubkey = X509_get_X509_PUBKEY(cert); pkey = X509_PUBKEY_get0(pubkey); if (n == 0) { if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL, NULL, NULL))) goto end; } else if (n == 1) { if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL, &keymgmt, NULL))) goto end; } else { keymgmt = EVP_KEYMGMT_fetch(libctx, "RSA", NULL); if (!TEST_ptr(keydata = evp_pkey_export_to_provider(pkey, NULL, &keymgmt, NULL))) goto end; } ret = 1; end: BIO_free(bio); X509_free(cert); EVP_KEYMGMT_free(keymgmt); OSSL_PROVIDER_unload(prov); OSSL_LIB_CTX_free(libctx); return ret; } int setup_tests(void) { if (!TEST_ptr(cert_filename = test_get_argument(0))) return 0; ADD_ALL_TESTS(test_pass_key, 1); ADD_ALL_TESTS(test_evp_pkey_export_to_provider, 3); return 1; }
test
openssl/test/keymgmt_internal_test.c
openssl
#include <openssl/asn1.h> #include <openssl/pem.h> #include "internal/sizes.h" #include "crypto/evp.h" #include "testutil.h" static const char *eecert_filename = NULL; static const char *cacert_filename = NULL; static const char *pubkey_filename = NULL; #define ALGORITHMID_NAME "algorithm-id" static int test_spki_aid(X509_PUBKEY *pubkey, const char *filename) { const ASN1_OBJECT *oid; X509_ALGOR *alg = NULL; EVP_PKEY *pkey = NULL; EVP_KEYMGMT *keymgmt = NULL; void *keydata = NULL; char name[OSSL_MAX_NAME_SIZE] = ""; unsigned char *algid_legacy = NULL; int algid_legacy_len = 0; static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE]; size_t algid_prov_len = 0; const OSSL_PARAM *gettable_params = NULL; OSSL_PARAM params[] = { OSSL_PARAM_octet_string(ALGORITHMID_NAME, &algid_prov, sizeof(algid_prov)), OSSL_PARAM_END }; int ret = 0; if (!TEST_true(X509_PUBKEY_get0_param(NULL, NULL, NULL, &alg, pubkey)) || !TEST_ptr(pkey = X509_PUBKEY_get0(pubkey))) goto end; if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0)) goto end; X509_ALGOR_get0(&oid, NULL, NULL, alg); if (!TEST_int_gt(OBJ_obj2txt(name, sizeof(name), oid, 0), 0)) goto end; if ((keydata = evp_pkey_export_to_provider(pkey, NULL, &keymgmt, NULL)) == NULL) { TEST_info("The public key found in '%s' doesn't have provider support." " Skipping...", filename); ret = 1; goto end; } if (!TEST_true(EVP_KEYMGMT_is_a(keymgmt, name))) { TEST_info("The AlgorithmID key type (%s) for the public key found in" " '%s' doesn't match the key type of the extracted public" " key.", name, filename); ret = 1; goto end; } if (!TEST_ptr(gettable_params = EVP_KEYMGMT_gettable_params(keymgmt)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) { TEST_info("The %s provider keymgmt appears to lack support for algorithm-id." " Skipping...", name); ret = 1; goto end; } algid_prov[0] = '\0'; if (!TEST_true(evp_keymgmt_get_params(keymgmt, keydata, params))) goto end; algid_prov_len = params[0].return_size; if (TEST_mem_eq(algid_legacy, algid_legacy_len, algid_prov, algid_prov_len)) ret = 1; end: EVP_KEYMGMT_free(keymgmt); OPENSSL_free(algid_legacy); return ret; } static int test_x509_spki_aid(X509 *cert, const char *filename) { X509_PUBKEY *pubkey = X509_get_X509_PUBKEY(cert); return test_spki_aid(pubkey, filename); } static int test_x509_sig_aid(X509 *eecert, const char *ee_filename, X509 *cacert, const char *ca_filename) { const ASN1_OBJECT *sig_oid = NULL; const X509_ALGOR *alg = NULL; int sig_nid = NID_undef, dig_nid = NID_undef, pkey_nid = NID_undef; EVP_MD_CTX *mdctx = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; unsigned char *algid_legacy = NULL; int algid_legacy_len = 0; static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE]; size_t algid_prov_len = 0; const OSSL_PARAM *gettable_params = NULL; OSSL_PARAM params[] = { OSSL_PARAM_octet_string("algorithm-id", &algid_prov, sizeof(algid_prov)), OSSL_PARAM_END }; int ret = 0; X509_get0_signature(NULL, &alg, eecert); X509_ALGOR_get0(&sig_oid, NULL, NULL, alg); if (!TEST_int_eq(X509_ALGOR_cmp(alg, X509_get0_tbs_sigalg(eecert)), 0)) goto end; if (!TEST_int_ne(sig_nid = OBJ_obj2nid(sig_oid), NID_undef) || !TEST_true(OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) || !TEST_ptr(pkey = X509_get0_pubkey(cacert))) goto end; if (!TEST_true(EVP_PKEY_is_a(pkey, OBJ_nid2sn(pkey_nid)))) { TEST_info("The '%s' pubkey can't be used to verify the '%s' signature", ca_filename, ee_filename); TEST_info("Signature algorithm is %s (pkey type %s, hash type %s)", OBJ_nid2sn(sig_nid), OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid)); TEST_info("Pkey key type is %s", EVP_PKEY_get0_type_name(pkey)); goto end; } if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0)) goto end; if (!TEST_ptr(mdctx = EVP_MD_CTX_new()) || !TEST_true(EVP_DigestVerifyInit_ex(mdctx, &pctx, OBJ_nid2sn(dig_nid), NULL, NULL, pkey, NULL))) { TEST_info("Couldn't initialize a DigestVerify operation with " "pkey type %s and hash type %s", OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid)); goto end; } if (!TEST_ptr(gettable_params = EVP_PKEY_CTX_gettable_params(pctx)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) { TEST_info("The %s provider keymgmt appears to lack support for algorithm-id" " Skipping...", OBJ_nid2sn(pkey_nid)); ret = 1; goto end; } algid_prov[0] = '\0'; if (!TEST_true(EVP_PKEY_CTX_get_params(pctx, params))) goto end; algid_prov_len = params[0].return_size; if (TEST_mem_eq(algid_legacy, algid_legacy_len, algid_prov, algid_prov_len)) ret = 1; end: EVP_MD_CTX_free(mdctx); OPENSSL_free(algid_legacy); return ret; } static int test_spki_file(void) { X509_PUBKEY *pubkey = NULL; BIO *b = BIO_new_file(pubkey_filename, "r"); int ret = 0; if (b == NULL) { TEST_error("Couldn't open '%s' for reading\n", pubkey_filename); TEST_openssl_errors(); goto end; } if ((pubkey = PEM_read_bio_X509_PUBKEY(b, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a SubjectPublicKeyInfo in PEM format\n", pubkey_filename); TEST_openssl_errors(); goto end; } ret = test_spki_aid(pubkey, pubkey_filename); end: BIO_free(b); X509_PUBKEY_free(pubkey); return ret; } static int test_x509_files(void) { X509 *eecert = NULL, *cacert = NULL; BIO *bee = NULL, *bca = NULL; int ret = 0; if ((bee = BIO_new_file(eecert_filename, "r")) == NULL) { TEST_error("Couldn't open '%s' for reading\n", eecert_filename); TEST_openssl_errors(); goto end; } if ((bca = BIO_new_file(cacert_filename, "r")) == NULL) { TEST_error("Couldn't open '%s' for reading\n", cacert_filename); TEST_openssl_errors(); goto end; } if ((eecert = PEM_read_bio_X509(bee, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n", eecert_filename); TEST_openssl_errors(); goto end; } if ((cacert = PEM_read_bio_X509(bca, NULL, NULL, NULL)) == NULL) { TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n", cacert_filename); TEST_openssl_errors(); goto end; } ret = test_x509_sig_aid(eecert, eecert_filename, cacert, cacert_filename) & test_x509_spki_aid(eecert, eecert_filename) & test_x509_spki_aid(cacert, cacert_filename); end: BIO_free(bee); BIO_free(bca); X509_free(eecert); X509_free(cacert); return ret; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_X509, OPT_SPKI, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file...\n"), { "x509", OPT_X509, '-', "Test X.509 certificates. Requires two files" }, { "spki", OPT_SPKI, '-', "Test public keys in SubjectPublicKeyInfo form. Requires one file" }, { OPT_HELP_STR, 1, '-', "file...\tFile(s) to run tests on. All files must be PEM encoded.\n" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; int n, x509 = 0, spki = 0, testcount = 0; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_X509: x509 = 1; break; case OPT_SPKI: spki = 1; break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } testcount = x509 + spki; if (testcount < 1) BIO_printf(bio_err, "No test type given\n"); else if (testcount > 1) BIO_printf(bio_err, "Only one test type may be given\n"); if (testcount != 1) return 0; n = test_get_argument_count(); if (spki && n == 1) { pubkey_filename = test_get_argument(0); } else if (x509 && n == 2) { eecert_filename = test_get_argument(0); cacert_filename = test_get_argument(1); } if (spki && pubkey_filename == NULL) { BIO_printf(bio_err, "Missing -spki argument\n"); return 0; } else if (x509 && (eecert_filename == NULL || cacert_filename == NULL)) { BIO_printf(bio_err, "Missing -x509 argument(s)\n"); return 0; } if (x509) ADD_TEST(test_x509_files); if (spki) ADD_TEST(test_spki_file); return 1; }
test
openssl/test/algorithmid_test.c
openssl
#include <stdio.h> #include <openssl/err.h> #include <openssl/x509_vfy.h> #include "testutil.h" static const char *chain; static const char *crl; static int test_load_cert_file(void) { int ret = 0, i; X509_STORE *store = NULL; X509_LOOKUP *lookup = NULL; STACK_OF(X509) *certs = NULL; STACK_OF(X509_OBJECT) *objs = NULL; if (!TEST_ptr(store = X509_STORE_new()) || !TEST_ptr(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file())) || !TEST_true(X509_load_cert_file(lookup, chain, X509_FILETYPE_PEM)) || !TEST_ptr(certs = X509_STORE_get1_all_certs(store)) || !TEST_int_eq(sk_X509_num(certs), 4) || !TEST_ptr(objs = X509_STORE_get1_objects(store)) || !TEST_int_eq(sk_X509_OBJECT_num(objs), 4)) goto err; for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { const X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i); if (!TEST_int_eq(X509_OBJECT_get_type(obj), X509_LU_X509)) goto err; } if (crl != NULL && !TEST_true(X509_load_crl_file(lookup, crl, X509_FILETYPE_PEM))) goto err; ret = 1; err: OSSL_STACK_OF_X509_free(certs); sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free); X509_STORE_free(store); return ret; } OPT_TEST_DECLARE_USAGE("cert.pem [crl.pem]\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } chain = test_get_argument(0); if (chain == NULL) return 0; crl = test_get_argument(1); ADD_TEST(test_load_cert_file); return 1; }
test
openssl/test/x509_load_cert_file_test.c
openssl
#include "internal/deprecated.h" #include <assert.h> #include <openssl/evp.h> #include <openssl/provider.h> #include <openssl/dsa.h> #include <openssl/dh.h> #include <openssl/safestack.h> #include <openssl/core_dispatch.h> #include <openssl/core_names.h> #include <openssl/x509.h> #include <openssl/encoder.h> #include "testutil.h" #include "internal/nelem.h" #include "crypto/bn_dh.h" static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *nullprov = NULL; static OSSL_PROVIDER *libprov = NULL; static STACK_OF(OPENSSL_STRING) *cipher_names = NULL; 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_DEFAULT_USAGE, { "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')" }, { NULL } }; return test_options; } #ifndef OPENSSL_NO_DH static const char *getname(int id) { const char *name[] = {"p", "q", "g" }; if (id >= 0 && id < 3) return name[id]; return "?"; } #endif static int test_evp_cipher_api_safety(void) { int ret = 0; EVP_CIPHER_CTX *ctx = NULL; ctx = EVP_CIPHER_CTX_new(); if (!TEST_ptr(ctx)) goto err; if (!TEST_int_eq(EVP_CIPHER_CTX_get_block_size(ctx), 0)) goto err_free; if (!TEST_int_eq(EVP_CIPHER_CTX_get_iv_length(ctx), 0)) goto err_free; ret = 1; err_free: EVP_CIPHER_CTX_free(ctx); err: return ret; } #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH) static int test_dsa_param_keygen(int tstid) { int ret = 0; int expected; EVP_PKEY_CTX *gen_ctx = NULL; EVP_PKEY *pkey_parm = NULL; EVP_PKEY *pkey = NULL, *dup_pk = NULL; DSA *dsa = NULL; int pind, qind, gind; BIGNUM *p = NULL, *q = NULL, *g = NULL; static const BIGNUM *bn[] = { &ossl_bignum_dh2048_256_p, &ossl_bignum_dh2048_256_q, &ossl_bignum_dh2048_256_g }; pind = tstid / 9; qind = (tstid / 3) % 3; gind = tstid % 3; expected = (pind == 0 && qind == 1 && gind == 2); TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind), getname(qind), getname(gind)); if (!TEST_ptr(pkey_parm = EVP_PKEY_new()) || !TEST_ptr(dsa = DSA_new()) || !TEST_ptr(p = BN_dup(bn[pind])) || !TEST_ptr(q = BN_dup(bn[qind])) || !TEST_ptr(g = BN_dup(bn[gind])) || !TEST_true(DSA_set0_pqg(dsa, p, q, g))) goto err; p = q = g = NULL; if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa))) goto err; dsa = NULL; if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0) || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected)) goto err; if (expected) { if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) goto err; } ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(dup_pk); EVP_PKEY_CTX_free(gen_ctx); EVP_PKEY_free(pkey_parm); DSA_free(dsa); BN_free(g); BN_free(q); BN_free(p); return ret; } #endif #ifndef OPENSSL_NO_DH static int do_dh_param_keygen(int tstid, const BIGNUM **bn) { int ret = 0; int expected; EVP_PKEY_CTX *gen_ctx = NULL; EVP_PKEY *pkey_parm = NULL; EVP_PKEY *pkey = NULL, *dup_pk = NULL; DH *dh = NULL; int pind, qind, gind; BIGNUM *p = NULL, *q = NULL, *g = NULL; pind = tstid / 9; qind = (tstid / 3) % 3; gind = tstid % 3; expected = (pind == 0 && qind == 1 && gind == 2); TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind), getname(qind), getname(gind)); if (!TEST_ptr(pkey_parm = EVP_PKEY_new()) || !TEST_ptr(dh = DH_new()) || !TEST_ptr(p = BN_dup(bn[pind])) || !TEST_ptr(q = BN_dup(bn[qind])) || !TEST_ptr(g = BN_dup(bn[gind])) || !TEST_true(DH_set0_pqg(dh, p, q, g))) goto err; p = q = g = NULL; if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh))) goto err; dh = NULL; if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0) || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected)) goto err; if (expected) { if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) goto err; } ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(dup_pk); EVP_PKEY_CTX_free(gen_ctx); EVP_PKEY_free(pkey_parm); DH_free(dh); BN_free(g); BN_free(q); BN_free(p); return ret; } static int test_dh_safeprime_param_keygen(int tstid) { static const BIGNUM *bn[] = { &ossl_bignum_ffdhe2048_p, &ossl_bignum_ffdhe2048_q, &ossl_bignum_const_2 }; return do_dh_param_keygen(tstid, bn); } static int dhx_cert_load(void) { int ret = 0; X509 *cert = NULL; BIO *bio = NULL; static const unsigned char dhx_cert[] = { 0x30,0x82,0x03,0xff,0x30,0x82,0x02,0xe7,0xa0,0x03,0x02,0x01,0x02,0x02,0x09,0x00, 0xdb,0xf5,0x4d,0x22,0xa0,0x7a,0x67,0xa6,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86, 0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x44,0x31,0x0b,0x30,0x09,0x06,0x03,0x55, 0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,0x0c, 0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,0x72,0x6f,0x75,0x70,0x31,0x1d, 0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,0x65,0x73,0x74,0x20,0x53,0x2f, 0x4d,0x49,0x4d,0x45,0x20,0x52,0x53,0x41,0x20,0x52,0x6f,0x6f,0x74,0x30,0x1e,0x17, 0x0d,0x31,0x33,0x30,0x38,0x30,0x32,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x17,0x0d, 0x32,0x33,0x30,0x36,0x31,0x31,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x30,0x44,0x31, 0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14, 0x06,0x03,0x55,0x04,0x0a,0x0c,0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47, 0x72,0x6f,0x75,0x70,0x31,0x1d,0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54, 0x65,0x73,0x74,0x20,0x53,0x2f,0x4d,0x49,0x4d,0x45,0x20,0x45,0x45,0x20,0x44,0x48, 0x20,0x23,0x31,0x30,0x82,0x01,0xb6,0x30,0x82,0x01,0x2b,0x06,0x07,0x2a,0x86,0x48, 0xce,0x3e,0x02,0x01,0x30,0x82,0x01,0x1e,0x02,0x81,0x81,0x00,0xd4,0x0c,0x4a,0x0c, 0x04,0x72,0x71,0x19,0xdf,0x59,0x19,0xc5,0xaf,0x44,0x7f,0xca,0x8e,0x2b,0xf0,0x09, 0xf5,0xd3,0x25,0xb1,0x73,0x16,0x55,0x89,0xdf,0xfd,0x07,0xaf,0x19,0xd3,0x7f,0xd0, 0x07,0xa2,0xfe,0x3f,0x5a,0xf1,0x01,0xc6,0xf8,0x2b,0xef,0x4e,0x6d,0x03,0x38,0x42, 0xa1,0x37,0xd4,0x14,0xb4,0x00,0x4a,0xb1,0x86,0x5a,0x83,0xce,0xb9,0x08,0x0e,0xc1, 0x99,0x27,0x47,0x8d,0x0b,0x85,0xa8,0x82,0xed,0xcc,0x0d,0xb9,0xb0,0x32,0x7e,0xdf, 0xe8,0xe4,0xf6,0xf6,0xec,0xb3,0xee,0x7a,0x11,0x34,0x65,0x97,0xfc,0x1a,0xb0,0x95, 0x4b,0x19,0xb9,0xa6,0x1c,0xd9,0x01,0x32,0xf7,0x35,0x7c,0x2d,0x5d,0xfe,0xc1,0x85, 0x70,0x49,0xf8,0xcc,0x99,0xd0,0xbe,0xf1,0x5a,0x78,0xc8,0x03,0x02,0x81,0x80,0x69, 0x00,0xfd,0x66,0xf2,0xfc,0x15,0x8b,0x09,0xb8,0xdc,0x4d,0xea,0xaa,0x79,0x55,0xf9, 0xdf,0x46,0xa6,0x2f,0xca,0x2d,0x8f,0x59,0x2a,0xad,0x44,0xa3,0xc6,0x18,0x2f,0x95, 0xb6,0x16,0x20,0xe3,0xd3,0xd1,0x8f,0x03,0xce,0x71,0x7c,0xef,0x3a,0xc7,0x44,0x39, 0x0e,0xe2,0x1f,0xd8,0xd3,0x89,0x2b,0xe7,0x51,0xdc,0x12,0x48,0x4c,0x18,0x4d,0x99, 0x12,0x06,0xe4,0x17,0x02,0x03,0x8c,0x24,0x05,0x8e,0xa6,0x85,0xf2,0x69,0x1b,0xe1, 0x6a,0xdc,0xe2,0x04,0x3a,0x01,0x9d,0x64,0xbe,0xfe,0x45,0xf9,0x44,0x18,0x71,0xbd, 0x2d,0x3e,0x7a,0x6f,0x72,0x7d,0x1a,0x80,0x42,0x57,0xae,0x18,0x6f,0x91,0xd6,0x61, 0x03,0x8a,0x1c,0x89,0x73,0xc7,0x56,0x41,0x03,0xd3,0xf8,0xed,0x65,0xe2,0x85,0x02, 0x15,0x00,0x89,0x94,0xab,0x10,0x67,0x45,0x41,0xad,0x63,0xc6,0x71,0x40,0x8d,0x6b, 0x9e,0x19,0x5b,0xa4,0xc7,0xf5,0x03,0x81,0x84,0x00,0x02,0x81,0x80,0x2f,0x5b,0xde, 0x72,0x02,0x36,0x6b,0x00,0x5e,0x24,0x7f,0x14,0x2c,0x18,0x52,0x42,0x97,0x4b,0xdb, 0x6e,0x15,0x50,0x3c,0x45,0x3e,0x25,0xf3,0xb7,0xc5,0x6e,0xe5,0x52,0xe7,0xc4,0xfb, 0xf4,0xa5,0xf0,0x39,0x12,0x7f,0xbc,0x54,0x1c,0x93,0xb9,0x5e,0xee,0xe9,0x14,0xb0, 0xdf,0xfe,0xfc,0x36,0xe4,0xf2,0xaf,0xfb,0x13,0xc8,0xdf,0x18,0x94,0x1d,0x40,0xb9, 0x71,0xdd,0x4c,0x9c,0xa7,0x03,0x52,0x02,0xb5,0xed,0x71,0x80,0x3e,0x23,0xda,0x28, 0xe5,0xab,0xe7,0x6f,0xf2,0x0a,0x0e,0x00,0x5b,0x7d,0xc6,0x4b,0xd7,0xc7,0xb2,0xc3, 0xba,0x62,0x7f,0x70,0x28,0xa0,0x9d,0x71,0x13,0x70,0xd1,0x9f,0x32,0x2f,0x3e,0xd2, 0xcd,0x1b,0xa4,0xc6,0x72,0xa0,0x74,0x5d,0x71,0xef,0x03,0x43,0x6e,0xa3,0x60,0x30, 0x5e,0x30,0x0c,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x02,0x30,0x00,0x30, 0x0e,0x06,0x03,0x55,0x1d,0x0f,0x01,0x01,0xff,0x04,0x04,0x03,0x02,0x05,0xe0,0x30, 0x1d,0x06,0x03,0x55,0x1d,0x0e,0x04,0x16,0x04,0x14,0x0b,0x5a,0x4d,0x5f,0x7d,0x25, 0xc7,0xf2,0x9d,0xc1,0xaa,0xb7,0x63,0x82,0x2f,0xfa,0x8f,0x32,0xe7,0xc0,0x30,0x1f, 0x06,0x03,0x55,0x1d,0x23,0x04,0x18,0x30,0x16,0x80,0x14,0xdf,0x7e,0x5e,0x88,0x05, 0x24,0x33,0x08,0xdd,0x22,0x81,0x02,0x97,0xcc,0x9a,0xb7,0xb1,0x33,0x27,0x30,0x30, 0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x82, 0x01,0x01,0x00,0x5a,0xf2,0x63,0xef,0xd3,0x16,0xd7,0xf5,0xaa,0xdd,0x12,0x00,0x36, 0x00,0x21,0xa2,0x7b,0x08,0xd6,0x3b,0x9f,0x62,0xac,0x53,0x1f,0xed,0x4c,0xd1,0x15, 0x34,0x65,0x71,0xee,0x96,0x07,0xa6,0xef,0xb2,0xde,0xd8,0xbb,0x35,0x6e,0x2c,0xe2, 0xd1,0x26,0xef,0x7e,0x94,0xe2,0x88,0x51,0xa4,0x6c,0xaa,0x27,0x2a,0xd3,0xb6,0xc2, 0xf7,0xea,0xc3,0x0b,0xa9,0xb5,0x28,0x37,0xa2,0x63,0x08,0xe4,0x88,0xc0,0x1b,0x16, 0x1b,0xca,0xfd,0x8a,0x07,0x32,0x29,0xa7,0x53,0xb5,0x2d,0x30,0xe4,0xf5,0x16,0xc3, 0xe3,0xc2,0x4c,0x30,0x5d,0x35,0x80,0x1c,0xa2,0xdb,0xe3,0x4b,0x51,0x0d,0x4c,0x60, 0x5f,0xb9,0x46,0xac,0xa8,0x46,0xa7,0x32,0xa7,0x9c,0x76,0xf8,0xe9,0xb5,0x19,0xe2, 0x0c,0xe1,0x0f,0xc6,0x46,0xe2,0x38,0xa7,0x87,0x72,0x6d,0x6c,0xbc,0x88,0x2f,0x9d, 0x2d,0xe5,0xd0,0x7d,0x1e,0xc7,0x5d,0xf8,0x7e,0xb4,0x0b,0xa6,0xf9,0x6c,0xe3,0x7c, 0xb2,0x70,0x6e,0x75,0x9b,0x1e,0x63,0xe1,0x4d,0xb2,0x81,0xd3,0x55,0x38,0x94,0x1a, 0x7a,0xfa,0xbf,0x01,0x18,0x70,0x2d,0x35,0xd3,0xe3,0x10,0x7a,0x9a,0xa7,0x8f,0xf3, 0xbd,0x56,0x55,0x5e,0xd8,0xbd,0x4e,0x16,0x76,0xd0,0x48,0x4c,0xf9,0x51,0x54,0xdf, 0x2d,0xb0,0xc9,0xaa,0x5e,0x42,0x38,0x50,0xbf,0x0f,0xc0,0xd9,0x84,0x44,0x4b,0x42, 0x24,0xec,0x14,0xa3,0xde,0x11,0xdf,0x58,0x7f,0xc2,0x4d,0xb2,0xd5,0x42,0x78,0x6e, 0x52,0x3e,0xad,0xc3,0x5f,0x04,0xc4,0xe6,0x31,0xaa,0x81,0x06,0x8b,0x13,0x4b,0x3c, 0x0e,0x6a,0xb1 }; if (!TEST_ptr(bio = BIO_new_mem_buf(dhx_cert, sizeof(dhx_cert))) || !TEST_ptr(cert = X509_new_ex(libctx, NULL)) || !TEST_ptr(d2i_X509_bio(bio, &cert))) goto err; ret = 1; err: X509_free(cert); BIO_free(bio); return ret; } #endif static int test_cipher_reinit(int test_id) { int ret = 0, diff, ccm, siv, no_null_key; int out1_len = 0, out2_len = 0, out3_len = 0; EVP_CIPHER *cipher = NULL; EVP_CIPHER_CTX *ctx = NULL; unsigned char out1[256]; unsigned char out2[256]; unsigned char out3[256]; unsigned char in[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }; unsigned char key[64] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; unsigned char iv[16] = { 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; TEST_note("Fetching %s\n", name); if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL))) goto err; ccm = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE); siv = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_SIV_MODE); no_null_key = EVP_CIPHER_is_a(cipher, "RC4") || EVP_CIPHER_is_a(cipher, "RC4-40") || EVP_CIPHER_is_a(cipher, "RC4-HMAC-MD5"); diff = EVP_CIPHER_is_a(cipher, "DES3-WRAP"); if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, sizeof(in))) || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) || !TEST_int_eq(EVP_EncryptUpdate(ctx, out2, &out2_len, in, sizeof(in)), ccm ? 0 : 1) || (!no_null_key && (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv)) || !TEST_int_eq(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in)), ccm || siv ? 0 : 1)))) goto err; if (ccm == 0) { if (diff) { if (!TEST_mem_ne(out1, out1_len, out2, out2_len) || !TEST_mem_ne(out1, out1_len, out3, out3_len) || !TEST_mem_ne(out2, out2_len, out3, out3_len)) goto err; } else { if (!TEST_mem_eq(out1, out1_len, out2, out2_len) || (!siv && !no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len))) goto err; } } ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int test_cipher_reinit_partialupdate(int test_id) { int ret = 0, in_len; int out1_len = 0, out2_len = 0, out3_len = 0; EVP_CIPHER *cipher = NULL; EVP_CIPHER_CTX *ctx = NULL; unsigned char out1[256]; unsigned char out2[256]; unsigned char out3[256]; static const unsigned char in[32] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xba, 0xbe, 0xba, 0xbe, 0x00, 0x00, 0xba, 0xbe, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; static const unsigned char key[64] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; static const unsigned char iv[16] = { 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id); if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) goto err; TEST_note("Fetching %s\n", name); if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL))) goto err; in_len = EVP_CIPHER_get_block_size(cipher); if (!TEST_int_gt(in_len, 0)) goto err; if (in_len > 1) in_len /= 2; if (((EVP_CIPHER_get_flags(cipher) & (EVP_CIPH_FLAG_CTS | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) != 0) || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_WRAP_MODE) { ret = 1; goto err; } if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, in_len)) || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len))) goto err; if (EVP_CIPHER_get_iv_length(cipher) != 0) if (!TEST_mem_eq(out1, out1_len, out2, out2_len)) goto err; if (EVP_CIPHER_get_mode(cipher) != EVP_CIPH_SIV_MODE) { if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv)) || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len))) goto err; if (EVP_CIPHER_get_iv_length(cipher) != 0) if (!TEST_mem_eq(out1, out1_len, out3, out3_len)) goto err; } ret = 1; err: EVP_CIPHER_free(cipher); EVP_CIPHER_CTX_free(ctx); return ret; } static int name_cmp(const char * const *a, const char * const *b) { return OPENSSL_strcasecmp(*a, *b); } static void collect_cipher_names(EVP_CIPHER *cipher, void *cipher_names_list) { STACK_OF(OPENSSL_STRING) *names = cipher_names_list; const char *name = EVP_CIPHER_get0_name(cipher); char *namedup = NULL; assert(name != NULL); if ((namedup = OPENSSL_strdup(name)) != NULL && !sk_OPENSSL_STRING_push(names, namedup)) OPENSSL_free(namedup); } static int rsa_keygen(int bits, EVP_PKEY **pub, EVP_PKEY **priv) { int ret = 0; unsigned char *pub_der = NULL; const unsigned char *pp = NULL; size_t len = 0; OSSL_ENCODER_CTX *ectx = NULL; if (!TEST_ptr(*priv = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", bits)) || !TEST_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(*priv, EVP_PKEY_PUBLIC_KEY, "DER", "type-specific", NULL)) || !TEST_true(OSSL_ENCODER_to_data(ectx, &pub_der, &len))) goto err; pp = pub_der; if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_RSA, pub, &pp, len))) goto err; ret = 1; err: OSSL_ENCODER_CTX_free(ectx); OPENSSL_free(pub_der); return ret; } static int kem_rsa_gen_recover(void) { int ret = 0; EVP_PKEY *pub = NULL; EVP_PKEY *priv = NULL; EVP_PKEY_CTX *sctx = NULL, *rctx = NULL, *dctx = NULL; unsigned char secret[256] = { 0, }; unsigned char ct[256] = { 0, }; unsigned char unwrap[256] = { 0, }; size_t ctlen = 0, unwraplen = 0, secretlen = 0; int bits = 2048; ret = TEST_true(rsa_keygen(bits, &pub, &priv)) && TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL)) && TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), 1) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(sctx, "RSASVE"), 1) && TEST_ptr(dctx = EVP_PKEY_CTX_dup(sctx)) && TEST_int_eq(EVP_PKEY_encapsulate(dctx, NULL, &ctlen, NULL, &secretlen), 1) && TEST_int_eq(ctlen, secretlen) && TEST_int_eq(ctlen, bits / 8) && TEST_int_eq(EVP_PKEY_encapsulate(dctx, ct, &ctlen, secret, &secretlen), 1) && TEST_ptr(rctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL)) && TEST_int_eq(EVP_PKEY_decapsulate_init(rctx, NULL), 1) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(rctx, "RSASVE"), 1) && TEST_int_eq(EVP_PKEY_decapsulate(rctx, NULL, &unwraplen, ct, ctlen), 1) && TEST_int_eq(EVP_PKEY_decapsulate(rctx, unwrap, &unwraplen, ct, ctlen), 1) && TEST_mem_eq(unwrap, unwraplen, secret, secretlen); EVP_PKEY_free(pub); EVP_PKEY_free(priv); EVP_PKEY_CTX_free(rctx); EVP_PKEY_CTX_free(dctx); EVP_PKEY_CTX_free(sctx); return ret; } #ifndef OPENSSL_NO_DES static int test_cipher_tdes_randkey(void) { int ret; EVP_CIPHER_CTX *ctx = NULL; EVP_CIPHER *tdes_cipher = NULL, *aes_cipher = NULL; unsigned char key[24] = { 0 }; ret = TEST_ptr(aes_cipher = EVP_CIPHER_fetch(libctx, "AES-256-CBC", NULL)) && TEST_int_eq(EVP_CIPHER_get_flags(aes_cipher) & EVP_CIPH_RAND_KEY, 0) && TEST_ptr(tdes_cipher = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", NULL)) && TEST_int_ne(EVP_CIPHER_get_flags(tdes_cipher) & EVP_CIPH_RAND_KEY, 0) && TEST_ptr(ctx = EVP_CIPHER_CTX_new()) && TEST_true(EVP_CipherInit_ex(ctx, tdes_cipher, NULL, NULL, NULL, 1)) && TEST_int_gt(EVP_CIPHER_CTX_rand_key(ctx, key), 0); EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_free(tdes_cipher); EVP_CIPHER_free(aes_cipher); return ret; } #endif static int kem_rsa_params(void) { int ret = 0; EVP_PKEY *pub = NULL; EVP_PKEY *priv = NULL; EVP_PKEY_CTX *pubctx = NULL, *privctx = NULL; unsigned char secret[256] = { 0, }; unsigned char ct[256] = { 0, }; size_t ctlen = 0, secretlen = 0; ret = TEST_true(rsa_keygen(2048, &pub, &priv)) && TEST_ptr(pubctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL)) && TEST_ptr(privctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL)) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), -2) && TEST_int_eq(EVP_PKEY_encapsulate_init(NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_encapsulate(NULL, NULL, NULL, NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_decapsulate_init(NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_decapsulate(NULL, NULL, NULL, NULL, 0), 0) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), -1) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, NULL, 0), 0) && TEST_int_eq(EVP_PKEY_decapsulate_init(pubctx, NULL), 1) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1) && TEST_int_eq(EVP_PKEY_decapsulate(pubctx, secret, &secretlen, ct, sizeof(ct)), 0) && TEST_uchar_eq(secret[0], 0) && TEST_int_eq(EVP_PKEY_encapsulate_init(pubctx, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, &secretlen), -2) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSA"), 0) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, NULL), 0) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, "RSASVE"), 0) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, &secretlen), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, &secretlen), 1) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, NULL, NULL), 0) && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, NULL), 1) && TEST_int_eq(EVP_PKEY_decapsulate_init(privctx, NULL), 1) && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(privctx, "RSASVE"), 1) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, NULL, ct, sizeof(ct)), 1) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, &secretlen, ct, sizeof(ct)), 1) && TEST_int_eq(secretlen, 256) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, ct, sizeof(ct)), 0) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, 0), 0) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, sizeof(ct)), 0) && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, ct, 0), 0); EVP_PKEY_free(pub); EVP_PKEY_free(priv); EVP_PKEY_CTX_free(pubctx); EVP_PKEY_CTX_free(privctx); return ret; } #ifndef OPENSSL_NO_DH static EVP_PKEY *gen_dh_key(void) { EVP_PKEY_CTX *gctx = NULL; EVP_PKEY *pkey = NULL; OSSL_PARAM params[2]; params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0); params[1] = OSSL_PARAM_construct_end(); if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0) || !TEST_true(EVP_PKEY_CTX_set_params(gctx, params)) || !TEST_true(EVP_PKEY_keygen(gctx, &pkey))) goto err; err: EVP_PKEY_CTX_free(gctx); return pkey; } static int kem_invalid_keytype(void) { int ret = 0; EVP_PKEY *key = NULL; EVP_PKEY_CTX *sctx = NULL; if (!TEST_ptr(key = gen_dh_key())) goto done; if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL))) goto done; if (!TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), -2)) goto done; ret = 1; done: EVP_PKEY_free(key); EVP_PKEY_CTX_free(sctx); return ret; } #endif 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; } } if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name)) return 0; ADD_TEST(test_evp_cipher_api_safety); #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH) ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3); #endif #ifndef OPENSSL_NO_DH ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3); ADD_TEST(dhx_cert_load); #endif if (!TEST_ptr(cipher_names = sk_OPENSSL_STRING_new(name_cmp))) return 0; EVP_CIPHER_do_all_provided(libctx, collect_cipher_names, cipher_names); ADD_ALL_TESTS(test_cipher_reinit, sk_OPENSSL_STRING_num(cipher_names)); ADD_ALL_TESTS(test_cipher_reinit_partialupdate, sk_OPENSSL_STRING_num(cipher_names)); ADD_TEST(kem_rsa_gen_recover); ADD_TEST(kem_rsa_params); #ifndef OPENSSL_NO_DH ADD_TEST(kem_invalid_keytype); #endif #ifndef OPENSSL_NO_DES ADD_TEST(test_cipher_tdes_randkey); #endif return 1; } static void string_free(char *m) { OPENSSL_free(m); } void cleanup_tests(void) { sk_OPENSSL_STRING_pop_free(cipher_names, string_free); OSSL_PROVIDER_unload(libprov); OSSL_LIB_CTX_free(libctx); OSSL_PROVIDER_unload(nullprov); }
test
openssl/test/evp_libctx_test.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/macros.h> #include "testutil.h" #if defined(OPENSSL_SYS_WINDOWS) # include <windows.h> #else # include <errno.h> #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F')) static int test_print_error_format(void) { char *lib; const char *func = OPENSSL_FUNC; char *reason; # ifdef OPENSSL_NO_ERR char reasonbuf[255]; # endif # ifndef OPENSSL_NO_FILENAMES const char *file = OPENSSL_FILE; const int line = OPENSSL_LINE; # else const char *file = ""; const int line = 0; # endif const char *expected_format = ":error:%08lX:%s:%s:%s:%s:%d"; char expected[512]; char *out = NULL, *p = NULL; int ret = 0, len; BIO *bio = NULL; const int syserr = EPERM; unsigned long errorcode; unsigned long reasoncode; ERR_set_mark(); ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line); errorcode = ERR_peek_error(); reasoncode = ERR_GET_REASON(errorcode); if (!TEST_int_eq(reasoncode, syserr)) { ERR_pop_to_mark(); goto err; } # if !defined(OPENSSL_NO_ERR) # if defined(OPENSSL_NO_AUTOERRINIT) lib = "lib(2)"; # else lib = "system library"; # endif reason = strerror(syserr); # else lib = "lib(2)"; BIO_snprintf(reasonbuf, sizeof(reasonbuf), "reason(%lu)", reasoncode); reason = reasonbuf; # endif BIO_snprintf(expected, sizeof(expected), expected_format, errorcode, lib, func, reason, file, line); if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto err; ERR_print_errors(bio); if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0)) goto err; for (p = out; *p != ':' && *p != 0; ++p) { if (!TEST_true(IS_HEX(*p))) goto err; } if (!TEST_true(*p != 0) || !TEST_strn_eq(expected, p, strlen(expected))) goto err; ret = 1; err: BIO_free(bio); return ret; } #endif static int preserves_system_error(void) { #if defined(OPENSSL_SYS_WINDOWS) SetLastError(ERROR_INVALID_FUNCTION); ERR_get_error(); return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION); #else errno = EINVAL; ERR_get_error(); return TEST_int_eq(errno, EINVAL); #endif } static int vdata_appends(void) { const char *data; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); ERR_add_error_data(1, "hello "); ERR_add_error_data(1, "world"); ERR_peek_error_data(&data, NULL); return TEST_str_eq(data, "hello world"); } static int raised_error(void) { const char *f, *data; int l; unsigned long e; #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) const char *file; int line; file = __FILE__; line = __LINE__ + 2; #endif ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR, "calling exit()"); if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0) || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR) #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR) || !TEST_int_eq(l, line) || !TEST_str_eq(f, file) #endif || !TEST_str_eq(data, "calling exit()")) return 0; return 1; } static int test_marks(void) { unsigned long mallocfail, shouldnot; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); mallocfail = ERR_peek_last_error(); if (!TEST_ulong_gt(mallocfail, 0)) return 0; if (!TEST_true(ERR_set_mark()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_set_mark()) || !TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark()) || !TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); shouldnot = ERR_peek_last_error(); if (!TEST_ulong_ne(mallocfail, shouldnot) || !TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); if (!TEST_ulong_ne(shouldnot, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()) || !TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); if (!TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())) return 0; if (!TEST_false(ERR_pop_to_mark()) || !TEST_ulong_eq(0, ERR_peek_last_error())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); if (!TEST_false(ERR_clear_last_mark()) || !TEST_ulong_eq(mallocfail, ERR_get_error()) || !TEST_ulong_eq(0, ERR_peek_last_error())) return 0; if (!TEST_false(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); if (!TEST_true(ERR_pop_to_mark()) || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())) return 0; if (!TEST_true(ERR_set_mark())) return 0; ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); if (!TEST_true(ERR_clear_last_mark()) || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())) return 0; ERR_clear_error(); return 1; } static int test_clear_error(void) { int flags = -1; const char *data = NULL; int res = 0; ERR_raise_data(0, 0, "hello %s", "world"); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "hello world") || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); ERR_raise(0, 0); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "") || !TEST_int_eq(flags, ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); ERR_raise_data(0, 0, "goodbye %s world", "cruel"); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "goodbye cruel world") || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); ERR_raise(0, 0); ERR_peek_error_data(&data, &flags); if (!TEST_str_eq(data, "") || !TEST_int_eq(flags, ERR_TXT_MALLOCED)) goto err; ERR_clear_error(); res = 1; err: ERR_clear_error(); return res; } static int test_save_restore(int idx) { ERR_STATE *es; int res = 0, i, flags = -1; unsigned long mallocfail, interr; static const char testdata[] = "test data"; const char *data = NULL; if (!TEST_ptr(es = OSSL_ERR_STATE_new())) goto err; ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); mallocfail = ERR_peek_last_error(); if (!TEST_ulong_gt(mallocfail, 0)) goto err; if (idx == 1 && !TEST_int_eq(ERR_set_mark(), 1)) goto err; ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR, testdata); interr = ERR_peek_last_error(); if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())) goto err; if (idx == 0) { OSSL_ERR_STATE_save(es); if (!TEST_ulong_eq(ERR_peek_last_error(), 0)) goto err; } else { OSSL_ERR_STATE_save_to_mark(es); if (!TEST_ulong_ne(ERR_peek_last_error(), 0)) goto err; } for (i = 0; i < 2; i++) { OSSL_ERR_STATE_restore(es); if (!TEST_ulong_eq(ERR_peek_last_error(), interr)) goto err; ERR_peek_last_error_data(&data, &flags); if (!TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; OSSL_ERR_STATE_restore(es); if (idx == 0 || i == 0) { if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), mallocfail) || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; } if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), interr) || !TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; if (idx == 0) { if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), mallocfail) || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; } if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL, &data, &flags), interr) || !TEST_str_eq(data, testdata) || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED)) goto err; if (!TEST_ulong_eq(ERR_get_error(), 0)) goto err; } res = 1; err: OSSL_ERR_STATE_free(es); return res; } int setup_tests(void) { ADD_TEST(preserves_system_error); ADD_TEST(vdata_appends); ADD_TEST(raised_error); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_print_error_format); #endif ADD_TEST(test_marks); ADD_ALL_TESTS(test_save_restore, 2); ADD_TEST(test_clear_error); return 1; }
test
openssl/test/errtest.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/bn.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/rand.h> #include "testutil.h" #ifndef OPENSSL_NO_SM2 # include "crypto/sm2.h" static fake_random_generate_cb get_faked_bytes; static OSSL_PROVIDER *fake_rand = NULL; static uint8_t *fake_rand_bytes = NULL; static size_t fake_rand_bytes_offset = 0; static size_t fake_rand_size = 0; static int get_faked_bytes(unsigned char *buf, size_t num, ossl_unused const char *name, ossl_unused EVP_RAND_CTX *ctx) { if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0)) return 0; while (num-- > 0) { if (fake_rand_bytes_offset >= fake_rand_size) fake_rand_bytes_offset = 0; *buf++ = fake_rand_bytes[fake_rand_bytes_offset++]; } return 1; } static int start_fake_rand(const char *hex_bytes) { OPENSSL_free(fake_rand_bytes); fake_rand_bytes_offset = 0; fake_rand_size = strlen(hex_bytes) / 2; if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL))) return 0; fake_rand_set_public_private_callbacks(NULL, get_faked_bytes); return 1; } static void restore_rand(void) { fake_rand_set_public_private_callbacks(NULL, NULL); OPENSSL_free(fake_rand_bytes); fake_rand_bytes = NULL; fake_rand_bytes_offset = 0; } static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex, const char *b_hex, const char *x_hex, const char *y_hex, const char *order_hex, const char *cof_hex) { BIGNUM *p = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *g_x = NULL; BIGNUM *g_y = NULL; BIGNUM *order = NULL; BIGNUM *cof = NULL; EC_POINT *generator = NULL; EC_GROUP *group = NULL; int ok = 0; if (!TEST_true(BN_hex2bn(&p, p_hex)) || !TEST_true(BN_hex2bn(&a, a_hex)) || !TEST_true(BN_hex2bn(&b, b_hex))) goto done; group = EC_GROUP_new_curve_GFp(p, a, b, NULL); if (!TEST_ptr(group)) goto done; generator = EC_POINT_new(group); if (!TEST_ptr(generator)) goto done; if (!TEST_true(BN_hex2bn(&g_x, x_hex)) || !TEST_true(BN_hex2bn(&g_y, y_hex)) || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x, g_y, NULL))) goto done; if (!TEST_true(BN_hex2bn(&order, order_hex)) || !TEST_true(BN_hex2bn(&cof, cof_hex)) || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof))) goto done; ok = 1; done: BN_free(p); BN_free(a); BN_free(b); BN_free(g_x); BN_free(g_y); EC_POINT_free(generator); BN_free(order); BN_free(cof); if (!ok) { EC_GROUP_free(group); group = NULL; } return group; } static int test_sm2_crypt(const EC_GROUP *group, const EVP_MD *digest, const char *privkey_hex, const char *message, const char *k_hex, const char *ctext_hex) { const size_t msg_len = strlen(message); BIGNUM *priv = NULL; EC_KEY *key = NULL; EC_POINT *pt = NULL; unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL); size_t ctext_len = 0; size_t ptext_len = 0; uint8_t *ctext = NULL; uint8_t *recovered = NULL; size_t recovered_len = msg_len; int rc = 0; if (!TEST_ptr(expected) || !TEST_true(BN_hex2bn(&priv, privkey_hex))) goto done; key = EC_KEY_new(); if (!TEST_ptr(key) || !TEST_true(EC_KEY_set_group(key, group)) || !TEST_true(EC_KEY_set_private_key(key, priv))) goto done; pt = EC_POINT_new(group); if (!TEST_ptr(pt) || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL)) || !TEST_true(EC_KEY_set_public_key(key, pt)) || !TEST_true(ossl_sm2_ciphertext_size(key, digest, msg_len, &ctext_len))) goto done; ctext = OPENSSL_zalloc(ctext_len); if (!TEST_ptr(ctext)) goto done; start_fake_rand(k_hex); if (!TEST_true(ossl_sm2_encrypt(key, digest, (const uint8_t *)message, msg_len, ctext, &ctext_len))) { restore_rand(); goto done; } restore_rand(); if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len)) goto done; if (!TEST_true(ossl_sm2_plaintext_size(ctext, ctext_len, &ptext_len)) || !TEST_int_eq(ptext_len, msg_len)) goto done; recovered = OPENSSL_zalloc(ptext_len); if (!TEST_ptr(recovered) || !TEST_true(ossl_sm2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len)) || !TEST_int_eq(recovered_len, msg_len) || !TEST_mem_eq(recovered, recovered_len, message, msg_len)) goto done; rc = 1; done: BN_free(priv); EC_POINT_free(pt); OPENSSL_free(ctext); OPENSSL_free(recovered); OPENSSL_free(expected); EC_KEY_free(key); return rc; } static int sm2_crypt_test(void) { int testresult = 0; EC_GROUP *gm_group = NULL; EC_GROUP *test_group = create_EC_group ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", "1"); if (!TEST_ptr(test_group)) goto done; if (!test_sm2_crypt( test_group, EVP_sm3(), "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", "encryption standard", "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F" "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820" "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7", "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1" "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2" "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A" "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467")) goto done; if (!test_sm2_crypt( test_group, EVP_sha256(), "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", "encryption standard", "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F" "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98" "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588", "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F" "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84" "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9" "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33")) goto done; gm_group = create_EC_group( "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff", "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc", "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93", "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7", "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0", "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123", "1"); if (!TEST_ptr(gm_group)) goto done; if (!test_sm2_crypt( gm_group, EVP_sm3(), "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8", "encryption standard", "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21", "307C" "0220" "04EBFC718E8D1798620432268E77FEB6415E2EDE0E073C0F4F640ECD2E149A73" "0221" "00" "E858F9D81E5430A57B36DAAB8F950A3C64E6EE6A63094D99283AFF767E124DF0" "0420" "59983C18F809E262923C53AEC295D30383B54E39D609D160AFCB1908D0BD8766" "0413" "21886CA989CA9C7D58087307CA93092D651EFA")) goto done; testresult = 1; done: EC_GROUP_free(test_group); EC_GROUP_free(gm_group); return testresult; } static int test_sm2_sign(const EC_GROUP *group, const char *userid, const char *privkey_hex, const char *message, const char *k_hex, const char *r_hex, const char *s_hex) { const size_t msg_len = strlen(message); int ok = 0; BIGNUM *priv = NULL; EC_POINT *pt = NULL; EC_KEY *key = NULL; ECDSA_SIG *sig = NULL; const BIGNUM *sig_r = NULL; const BIGNUM *sig_s = NULL; BIGNUM *r = NULL; BIGNUM *s = NULL; if (!TEST_true(BN_hex2bn(&priv, privkey_hex))) goto done; key = EC_KEY_new(); if (!TEST_ptr(key) || !TEST_true(EC_KEY_set_group(key, group)) || !TEST_true(EC_KEY_set_private_key(key, priv))) goto done; pt = EC_POINT_new(group); if (!TEST_ptr(pt) || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL)) || !TEST_true(EC_KEY_set_public_key(key, pt))) goto done; start_fake_rand(k_hex); sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid, strlen(userid), (const uint8_t *)message, msg_len); if (!TEST_ptr(sig)) { restore_rand(); goto done; } restore_rand(); ECDSA_SIG_get0(sig, &sig_r, &sig_s); if (!TEST_true(BN_hex2bn(&r, r_hex)) || !TEST_true(BN_hex2bn(&s, s_hex)) || !TEST_BN_eq(r, sig_r) || !TEST_BN_eq(s, sig_s)) goto done; ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid, strlen(userid), (const uint8_t *)message, msg_len); TEST_true(ok); done: ECDSA_SIG_free(sig); EC_POINT_free(pt); EC_KEY_free(key); BN_free(priv); BN_free(r); BN_free(s); return ok; } static int sm2_sig_test(void) { int testresult = 0; EC_GROUP *gm_group = NULL; EC_GROUP *test_group = create_EC_group ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", "1"); if (!TEST_ptr(test_group)) goto done; if (!TEST_true(test_sm2_sign( test_group, "ALICE123@YAHOO.COM", "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263", "message digest", "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F" "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a", "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1", "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7"))) goto done; gm_group = create_EC_group( "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff", "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc", "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93", "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7", "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0", "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123", "1"); if (!TEST_ptr(gm_group)) goto done; if (!TEST_true(test_sm2_sign( gm_group, SM2_DEFAULT_USERID, "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8", "message digest", "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21", "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3", "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA"))) goto done; testresult = 1; done: EC_GROUP_free(test_group); EC_GROUP_free(gm_group); return testresult; } #endif int setup_tests(void) { #ifdef OPENSSL_NO_SM2 TEST_note("SM2 is disabled."); #else fake_rand = fake_rand_start(NULL); if (fake_rand == NULL) return 0; ADD_TEST(sm2_crypt_test); ADD_TEST(sm2_sig_test); #endif return 1; } void cleanup_tests(void) { #ifndef OPENSSL_NO_SM2 fake_rand_finish(fake_rand); #endif }
test
openssl/test/sm2_internal_test.c
openssl
#include <openssl/bio.h> #include "testutil.h" static int test_bio_meth(void) { int i, ret = 0, id; BIO_METHOD *meth1 = NULL, *meth2 = NULL, *meth3 = NULL; BIO *membio = NULL, *bio1 = NULL, *bio2 = NULL, *bio3 = NULL; id = BIO_get_new_index(); if (!TEST_int_eq(id, BIO_TYPE_START + 1)) goto err; if (!TEST_ptr(meth1 = BIO_meth_new(id, "Method1")) || !TEST_ptr(meth2 = BIO_meth_new(BIO_TYPE_NONE, "Method2")) || !TEST_ptr(meth3 = BIO_meth_new(BIO_TYPE_NONE|BIO_TYPE_FILTER, "Method3")) || !TEST_ptr(bio1 = BIO_new(meth1)) || !TEST_ptr(bio2 = BIO_new(meth2)) || !TEST_ptr(bio3 = BIO_new(meth3)) || !TEST_ptr(membio = BIO_new(BIO_s_mem()))) goto err; BIO_set_next(bio3, bio2); BIO_set_next(bio2, bio1); BIO_set_next(bio1, membio); for (i = id + 1; i <= BIO_TYPE_MASK; ++i) { if (!TEST_int_eq(BIO_get_new_index(), i)) goto err; } if (!TEST_int_eq(BIO_get_new_index(), -1)) goto err; if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_MEM), membio) || !TEST_ptr_eq(BIO_find_type(bio3, id), bio1)) goto err; if (!TEST_ptr_null(BIO_find_type(bio3, BIO_TYPE_NONE))) goto err; if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_FILTER), bio3)) goto err; ret = 1; err: BIO_free(membio); BIO_free(bio3); BIO_free(bio2); BIO_free(bio1); BIO_meth_free(meth3); BIO_meth_free(meth2); BIO_meth_free(meth1); return ret; } int setup_tests(void) { ADD_TEST(test_bio_meth); return 1; }
test
openssl/test/bio_meth_test.c
openssl
#include "internal/packet.h" #include "internal/quic_txpim.h" #include "internal/quic_fifd.h" #include "testutil.h" static OSSL_TIME cur_time; static OSSL_TIME fake_now(void *arg) { return cur_time; } static void step_time(uint64_t ms) { cur_time = ossl_time_add(cur_time, ossl_ms2time(ms)); } static QUIC_SSTREAM *(*get_sstream_by_id_p)(uint64_t stream_id, uint32_t pn_space, void *arg); static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space, void *arg) { return get_sstream_by_id_p(stream_id, pn_space, arg); } static void (*regen_frame_p)(uint64_t frame_type, uint64_t stream_id, QUIC_TXPIM_PKT *pkt, void *arg); static void regen_frame(uint64_t frame_type, uint64_t stream_id, QUIC_TXPIM_PKT *pkt, void *arg) { regen_frame_p(frame_type, stream_id, pkt, arg); } static void confirm_frame(uint64_t frame_type, uint64_t stream_id, QUIC_TXPIM_PKT *pkt, void *arg) {} static void sstream_updated(uint64_t stream_id, void *arg) {} typedef struct info_st { QUIC_FIFD fifd; OSSL_ACKM *ackm; QUIC_CFQ *cfq; QUIC_TXPIM *txpim; OSSL_STATM statm; OSSL_CC_DATA *ccdata; QUIC_SSTREAM *sstream[4]; } INFO; static INFO *cur_info; static int cb_fail; static int cfq_freed; static QUIC_SSTREAM *sstream_expect(uint64_t stream_id, uint32_t pn_space, void *arg) { if (stream_id == 42 || stream_id == 43) return cur_info->sstream[stream_id - 42]; cb_fail = 1; return NULL; } static uint64_t regen_frame_type[16]; static uint64_t regen_stream_id[16]; static size_t regen_count; static void regen_expect(uint64_t frame_type, uint64_t stream_id, QUIC_TXPIM_PKT *pkt, void *arg) { regen_frame_type[regen_count] = frame_type; regen_stream_id[regen_count] = stream_id; ++regen_count; } static const unsigned char placeholder_data[] = "placeholder"; static void cfq_free_cb_(unsigned char *buf, size_t buf_len, void *arg) { if (buf == placeholder_data && buf_len == sizeof(placeholder_data)) cfq_freed = 1; } #define TEST_KIND_ACK 0 #define TEST_KIND_LOSS 1 #define TEST_KIND_DISCARD 2 #define TEST_KIND_NUM 3 static int test_generic(INFO *info, int kind) { int testresult = 0; size_t i, consumed = 0; QUIC_TXPIM_PKT *pkt = NULL, *pkt2 = NULL; OSSL_QUIC_FRAME_STREAM hdr = {0}; OSSL_QTX_IOVEC iov[2]; size_t num_iov; QUIC_TXPIM_CHUNK chunk = {42, 0, 11, 0}; OSSL_QUIC_FRAME_ACK ack = {0}; OSSL_QUIC_ACK_RANGE ack_ranges[1] = {0}; QUIC_CFQ_ITEM *cfq_item = NULL; uint32_t pn_space = (kind == TEST_KIND_DISCARD) ? QUIC_PN_SPACE_HANDSHAKE : QUIC_PN_SPACE_APP; cur_time = ossl_seconds2time(1000); regen_count = 0; get_sstream_by_id_p = sstream_expect; regen_frame_p = regen_expect; if (!TEST_ptr(pkt = ossl_quic_txpim_pkt_alloc(info->txpim))) goto err; for (i = 0; i < 2; ++i) { num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_append(info->sstream[i], (unsigned char *)"Test message", 12, &consumed)) || !TEST_size_t_eq(consumed, 12)) goto err; if (i == 1) ossl_quic_sstream_fin(info->sstream[i]); if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[i], 0, &hdr, iov, &num_iov)) || !TEST_int_eq(hdr.is_fin, i == 1) || !TEST_uint64_t_eq(hdr.offset, 0) || !TEST_uint64_t_eq(hdr.len, 12) || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(info->sstream[i]), 12) || !TEST_true(ossl_quic_sstream_mark_transmitted(info->sstream[i], hdr.offset, hdr.offset + hdr.len - 1))) goto err; if (i == 1 && !TEST_true(ossl_quic_sstream_mark_transmitted_fin(info->sstream[i], hdr.offset + hdr.len))) goto err; chunk.has_fin = hdr.is_fin; chunk.stream_id = 42 + i; if (!TEST_true(ossl_quic_txpim_pkt_append_chunk(pkt, &chunk))) goto err; } cfq_freed = 0; if (!TEST_ptr(cfq_item = ossl_quic_cfq_add_frame(info->cfq, 10, pn_space, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID, 0, placeholder_data, sizeof(placeholder_data), cfq_free_cb_, NULL)) || !TEST_ptr_eq(cfq_item, ossl_quic_cfq_get_priority_head(info->cfq, pn_space))) goto err; ossl_quic_txpim_pkt_add_cfq_item(pkt, cfq_item); pkt->ackm_pkt.pkt_num = 0; pkt->ackm_pkt.pkt_space = pn_space; pkt->ackm_pkt.largest_acked = QUIC_PN_INVALID; pkt->ackm_pkt.num_bytes = 50; pkt->ackm_pkt.time = cur_time; pkt->ackm_pkt.is_inflight = 1; pkt->ackm_pkt.is_ack_eliciting = 1; if (kind == TEST_KIND_LOSS) { pkt->had_handshake_done_frame = 1; pkt->had_max_data_frame = 1; pkt->had_max_streams_bidi_frame = 1; pkt->had_max_streams_uni_frame = 1; pkt->had_ack_frame = 1; } ack_ranges[0].start = 0; ack_ranges[0].end = 0; ack.ack_ranges = ack_ranges; ack.num_ack_ranges = 1; if (!TEST_true(ossl_quic_fifd_pkt_commit(&info->fifd, pkt))) goto err; if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(info->cfq, pn_space))) goto err; switch (kind) { case TEST_KIND_ACK: if (!TEST_true(ossl_ackm_on_rx_ack_frame(info->ackm, &ack, pn_space, cur_time))) goto err; for (i = 0; i < 2; ++i) if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(info->sstream[i]), 0)) goto err; if (!TEST_false(ossl_quic_sstream_mark_lost_fin(info->sstream[1]))) goto err; if (!TEST_true(cfq_freed)) goto err; if (!TEST_size_t_eq(regen_count, 0)) goto err; break; case TEST_KIND_LOSS: if (!TEST_ptr(pkt2 = ossl_quic_txpim_pkt_alloc(info->txpim))) goto err; step_time(10000); pkt2->ackm_pkt.pkt_num = 50; pkt2->ackm_pkt.pkt_space = pn_space; pkt2->ackm_pkt.largest_acked = QUIC_PN_INVALID; pkt2->ackm_pkt.num_bytes = 50; pkt2->ackm_pkt.time = cur_time; pkt2->ackm_pkt.is_inflight = 1; pkt2->ackm_pkt.is_ack_eliciting = 1; ack_ranges[0].start = 50; ack_ranges[0].end = 50; ack.ack_ranges = ack_ranges; ack.num_ack_ranges = 1; if (!TEST_true(ossl_quic_fifd_pkt_commit(&info->fifd, pkt2)) || !TEST_true(ossl_ackm_on_rx_ack_frame(info->ackm, &ack, pn_space, cur_time))) goto err; for (i = 0; i < 2; ++i) { num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[i], 0, &hdr, iov, &num_iov)) || !TEST_uint64_t_eq(hdr.offset, 0) || !TEST_uint64_t_eq(hdr.len, 12)) goto err; } if (!TEST_size_t_eq(regen_count, 7) || !TEST_uint64_t_eq(regen_stream_id[0], 42) || !TEST_uint64_t_eq(regen_frame_type[0], OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) || !TEST_uint64_t_eq(regen_stream_id[1], 43) || !TEST_uint64_t_eq(regen_frame_type[1], OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA) || !TEST_uint64_t_eq(regen_frame_type[2], OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE) || !TEST_uint64_t_eq(regen_stream_id[2], UINT64_MAX) || !TEST_uint64_t_eq(regen_frame_type[3], OSSL_QUIC_FRAME_TYPE_MAX_DATA) || !TEST_uint64_t_eq(regen_stream_id[3], UINT64_MAX) || !TEST_uint64_t_eq(regen_frame_type[4], OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI) || !TEST_uint64_t_eq(regen_stream_id[4], UINT64_MAX) || !TEST_uint64_t_eq(regen_frame_type[5], OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI) || !TEST_uint64_t_eq(regen_stream_id[5], UINT64_MAX) || !TEST_uint64_t_eq(regen_frame_type[6], OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) || !TEST_uint64_t_eq(regen_stream_id[6], UINT64_MAX)) goto err; if (!TEST_ptr_eq(cfq_item, ossl_quic_cfq_get_priority_head(info->cfq, pn_space))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(info->sstream[1], 1, &hdr, iov, &num_iov)) || !TEST_true(hdr.is_fin) || !TEST_uint64_t_eq(hdr.len, 0)) goto err; break; case TEST_KIND_DISCARD: if (!TEST_true(ossl_ackm_on_pkt_space_discarded(info->ackm, pn_space))) goto err; if (!TEST_true(cfq_freed)) goto err; break; default: goto err; } if (!TEST_size_t_eq(ossl_quic_txpim_get_in_use(info->txpim), 0)) goto err; testresult = 1; err: return testresult; } static int test_fifd(int idx) { int testresult = 0; INFO info = {0}; size_t i; cur_info = &info; cb_fail = 0; if (!TEST_true(ossl_statm_init(&info.statm)) || !TEST_ptr(info.ccdata = ossl_cc_dummy_method.new(fake_now, NULL)) || !TEST_ptr(info.ackm = ossl_ackm_new(fake_now, NULL, &info.statm, &ossl_cc_dummy_method, info.ccdata)) || !TEST_true(ossl_ackm_on_handshake_confirmed(info.ackm)) || !TEST_ptr(info.cfq = ossl_quic_cfq_new()) || !TEST_ptr(info.txpim = ossl_quic_txpim_new()) || !TEST_true(ossl_quic_fifd_init(&info.fifd, info.cfq, info.ackm, info.txpim, get_sstream_by_id, NULL, regen_frame, NULL, confirm_frame, NULL, sstream_updated, NULL, NULL, NULL))) goto err; for (i = 0; i < OSSL_NELEM(info.sstream); ++i) if (!TEST_ptr(info.sstream[i] = ossl_quic_sstream_new(1024))) goto err; ossl_statm_update_rtt(&info.statm, ossl_time_zero(), ossl_ms2time(1)); if (!TEST_true(test_generic(&info, idx)) || !TEST_false(cb_fail)) goto err; testresult = 1; err: ossl_quic_fifd_cleanup(&info.fifd); ossl_quic_cfq_free(info.cfq); ossl_quic_txpim_free(info.txpim); ossl_ackm_free(info.ackm); ossl_statm_destroy(&info.statm); if (info.ccdata != NULL) ossl_cc_dummy_method.free(info.ccdata); for (i = 0; i < OSSL_NELEM(info.sstream); ++i) ossl_quic_sstream_free(info.sstream[i]); cur_info = NULL; return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_fifd, TEST_KIND_NUM); return 1; }
test
openssl/test/quic_fifd_test.c
openssl
#include "internal/e_os.h" #ifndef _BSD_SOURCE # define _BSD_SOURCE 1 #endif #ifndef _DEFAULT_SOURCE # define _DEFAULT_SOURCE 1 #endif #include <assert.h> #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "internal/nelem.h" #ifdef OPENSSL_SYS_VMS # define _XOPEN_SOURCE 500 #endif #include <ctype.h> #include <openssl/bio.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/rsa.h> #ifndef OPENSSL_NO_DSA # include <openssl/dsa.h> #endif #include <openssl/bn.h> #ifndef OPENSSL_NO_CT # include <openssl/ct.h> #endif #include <openssl/provider.h> #include "testutil.h" #include "testutil/output.h" #define _XOPEN_SOURCE_EXTENDED 1 #ifdef OPENSSL_SYS_WINDOWS # include <winsock.h> #else # include <unistd.h> #endif #include "helpers/predefined_dhparams.h" static SSL_CTX *s_ctx = NULL; static SSL_CTX *s_ctx2 = NULL; #define COMP_ZLIB 1 static int verify_callback(int ok, X509_STORE_CTX *ctx); static int app_verify_callback(X509_STORE_CTX *ctx, void *arg); #define APP_CALLBACK_STRING "Test Callback Argument" struct app_verify_arg { char *string; int app_verify; }; static char *psk_key = NULL; #ifndef OPENSSL_NO_PSK static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len); static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len); #endif static BIO *bio_stdout = NULL; #ifndef OPENSSL_NO_NEXTPROTONEG static const char NEXT_PROTO_STRING[] = "\x09testproto"; static int npn_client = 0; static int npn_server = 0; static int npn_server_reject = 0; static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { *out = (unsigned char *)NEXT_PROTO_STRING + 1; *outlen = sizeof(NEXT_PROTO_STRING) - 2; return SSL_TLSEXT_ERR_OK; } static int cb_server_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { *data = (const unsigned char *)NEXT_PROTO_STRING; *len = sizeof(NEXT_PROTO_STRING) - 1; return SSL_TLSEXT_ERR_OK; } static int cb_server_rejects_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { return SSL_TLSEXT_ERR_NOACK; } static int verify_npn(SSL *client, SSL *server) { const unsigned char *client_s; unsigned client_len; const unsigned char *server_s; unsigned server_len; SSL_get0_next_proto_negotiated(client, &client_s, &client_len); SSL_get0_next_proto_negotiated(server, &server_s, &server_len); if (client_len) { BIO_printf(bio_stdout, "Client NPN: "); BIO_write(bio_stdout, client_s, client_len); BIO_printf(bio_stdout, "\n"); } if (server_len) { BIO_printf(bio_stdout, "Server NPN: "); BIO_write(bio_stdout, server_s, server_len); BIO_printf(bio_stdout, "\n"); } if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(client_s, NEXT_PROTO_STRING + 1, client_len))) return -1; if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 || memcmp(server_s, NEXT_PROTO_STRING + 1, server_len))) return -1; if (!npn_client && client_len) return -1; if (!npn_server && server_len) return -1; if (npn_server_reject && server_len) return -1; if (npn_client && npn_server && (!client_len || !server_len)) return -1; return 0; } #endif static const char *alpn_client; static char *alpn_server; static char *alpn_server2; static const char *alpn_expected; static unsigned char *alpn_selected; static const char *server_min_proto; static const char *server_max_proto; static const char *client_min_proto; static const char *client_max_proto; static const char *should_negotiate; static const char *sn_client; static const char *sn_server1; static const char *sn_server2; static int sn_expect = 0; static const char *server_sess_out; static const char *server_sess_in; static const char *client_sess_out; static const char *client_sess_in; static SSL_SESSION *server_sess; static SSL_SESSION *client_sess; static int servername_cb(SSL *s, int *ad, void *arg) { const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (sn_server2 == NULL) { BIO_printf(bio_stdout, "Servername 2 is NULL\n"); return SSL_TLSEXT_ERR_NOACK; } if (servername) { if (s_ctx2 != NULL && sn_server2 != NULL && !OPENSSL_strcasecmp(servername, sn_server2)) { BIO_printf(bio_stdout, "Switching server context.\n"); SSL_set_SSL_CTX(s, s_ctx2); } } return SSL_TLSEXT_ERR_OK; } static int verify_servername(SSL *client, SSL *server) { SSL_CTX* ctx = SSL_get_SSL_CTX(server); if (sn_expect == 0) return 0; if (sn_expect == 1 && ctx == s_ctx) return 0; if (sn_expect == 2 && ctx == s_ctx2) return 0; BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect); if (ctx == s_ctx2) BIO_printf(bio_stdout, "Servername: context is 2\n"); else if (ctx == s_ctx) BIO_printf(bio_stdout, "Servername: context is 1\n"); else BIO_printf(bio_stdout, "Servername: context is unknown\n"); return -1; } static unsigned char *next_protos_parse(size_t *outlen, const char *in) { size_t len; unsigned char *out; size_t i, start = 0; len = strlen(in); if (len >= 65535) return NULL; out = OPENSSL_malloc(strlen(in) + 1); if (!out) return NULL; for (i = 0; i <= len; ++i) { if (i == len || in[i] == ',') { if (i - start > 255) { OPENSSL_free(out); return NULL; } out[start] = (unsigned char)(i - start); start = i + 1; } else out[i + 1] = in[i]; } *outlen = len + 1; return out; } static int cb_server_alpn(SSL *s, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { unsigned char *protos; size_t protos_len; char* alpn_str = arg; protos = next_protos_parse(&protos_len, alpn_str); if (protos == NULL) { fprintf(stderr, "failed to parser ALPN server protocol string: %s\n", alpn_str); abort(); } if (SSL_select_next_proto ((unsigned char **)out, outlen, protos, protos_len, in, inlen) != OPENSSL_NPN_NEGOTIATED) { OPENSSL_free(protos); return SSL_TLSEXT_ERR_NOACK; } alpn_selected = OPENSSL_malloc(*outlen); if (alpn_selected == NULL) { fprintf(stderr, "failed to allocate memory\n"); OPENSSL_free(protos); abort(); } memcpy(alpn_selected, *out, *outlen); *out = alpn_selected; OPENSSL_free(protos); return SSL_TLSEXT_ERR_OK; } static int verify_alpn(SSL *client, SSL *server) { const unsigned char *client_proto, *server_proto; unsigned int client_proto_len = 0, server_proto_len = 0; SSL_get0_alpn_selected(client, &client_proto, &client_proto_len); SSL_get0_alpn_selected(server, &server_proto, &server_proto_len); OPENSSL_free(alpn_selected); alpn_selected = NULL; if (client_proto == NULL && client_proto_len != 0) { BIO_printf(bio_stdout, "Inconsistent SSL_get0_alpn_selected() for client!\n"); goto err; } if (server_proto == NULL && server_proto_len != 0) { BIO_printf(bio_stdout, "Inconsistent SSL_get0_alpn_selected() for server!\n"); goto err; } if (client_proto_len != server_proto_len) { BIO_printf(bio_stdout, "ALPN selected protocols differ!\n"); goto err; } if (client_proto != NULL && memcmp(client_proto, server_proto, client_proto_len) != 0) { BIO_printf(bio_stdout, "ALPN selected protocols differ!\n"); goto err; } if (client_proto_len > 0 && alpn_expected == NULL) { BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n"); goto err; } if (alpn_expected != NULL && (client_proto_len != strlen(alpn_expected) || memcmp(client_proto, alpn_expected, client_proto_len) != 0)) { BIO_printf(bio_stdout, "ALPN selected protocols not equal to expected protocol: %s\n", alpn_expected); goto err; } return 0; err: BIO_printf(bio_stdout, "ALPN results: client: '"); BIO_write(bio_stdout, client_proto, client_proto_len); BIO_printf(bio_stdout, "', server: '"); BIO_write(bio_stdout, server_proto, server_proto_len); BIO_printf(bio_stdout, "'\n"); BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '", alpn_client); if (SSL_get_SSL_CTX(server) == s_ctx2) { BIO_printf(bio_stdout, "%s'\n", alpn_server2); } else { BIO_printf(bio_stdout, "%s'\n", alpn_server); } return -1; } #define TACK_EXT_TYPE 62208 #define CUSTOM_EXT_TYPE_0 1000 #define CUSTOM_EXT_TYPE_1 1001 #define CUSTOM_EXT_TYPE_2 1002 #define CUSTOM_EXT_TYPE_3 1003 static const char custom_ext_cli_string[] = "abc"; static const char custom_ext_srv_string[] = "defg"; static char *serverinfo_file = NULL; static int serverinfo_sct = 0; static int serverinfo_tack = 0; static int serverinfo_sct_seen = 0; static int serverinfo_tack_seen = 0; static int serverinfo_other_seen = 0; static int custom_ext = 0; static int custom_ext_error = 0; static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp) serverinfo_sct_seen++; else if (ext_type == TACK_EXT_TYPE) serverinfo_tack_seen++; else serverinfo_other_seen++; return 1; } static int verify_serverinfo(void) { if (serverinfo_sct != serverinfo_sct_seen) return -1; if (serverinfo_tack != serverinfo_tack_seen) return -1; if (serverinfo_other_seen) return -1; return 0; } static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_0) custom_ext_error = 1; return 0; } static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { return 1; } static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_1) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; } static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { return 1; } static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; } static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; if (inlen != 0) custom_ext_error = 1; return 1; } static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; *out = (const unsigned char *)custom_ext_cli_string; *outlen = strlen(custom_ext_cli_string); return 1; } static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; if (inlen != strlen(custom_ext_srv_string)) custom_ext_error = 1; if (memcmp(custom_ext_srv_string, in, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { custom_ext_error = 1; return 1; } static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { custom_ext_error = 1; return 0; } static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_1) custom_ext_error = 1; if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { return 0; } static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_2) custom_ext_error = 1; if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { *out = NULL; *outlen = 0; return 1; } static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { if (ext_type != CUSTOM_EXT_TYPE_3) custom_ext_error = 1; if (inlen != strlen(custom_ext_cli_string)) custom_ext_error = 1; if (memcmp(in, custom_ext_cli_string, inlen) != 0) custom_ext_error = 1; return 1; } static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, size_t *outlen, int *al, void *arg) { *out = (const unsigned char *)custom_ext_srv_string; *outlen = strlen(custom_ext_srv_string); return 1; } static char *cipher = NULL; static char *ciphersuites = NULL; static int verbose = 0; static int debug = 0; int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long bytes, clock_t *s_time, clock_t *c_time); int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time, clock_t *c_time); int doit(SSL *s_ssl, SSL *c_ssl, long bytes); static void sv_usage(void) { fprintf(stderr, "usage: ssltest [args ...]\n"); fprintf(stderr, "\n"); fprintf(stderr, " -server_auth - check server certificate\n"); fprintf(stderr, " -client_auth - do client authentication\n"); fprintf(stderr, " -v - more output\n"); fprintf(stderr, " -d - debug output\n"); fprintf(stderr, " -reuse - use session-id reuse\n"); fprintf(stderr, " -num <val> - number of connections to perform\n"); fprintf(stderr, " -bytes <val> - number of bytes to swap between client/server\n"); #ifndef OPENSSL_NO_DH fprintf(stderr, " -dhe512 - use 512 bit key for DHE (to test failure)\n"); fprintf(stderr, " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n"); fprintf(stderr, " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n"); fprintf(stderr, " -dhe4096 - use 4096 bit key (safe prime) for DHE\n"); #endif fprintf(stderr, " -no_dhe - disable DHE\n"); #ifndef OPENSSL_NO_EC fprintf(stderr, " -no_ecdhe - disable ECDHE\n"); #endif #ifndef OPENSSL_NO_PSK fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n"); #endif #ifndef OPENSSL_NO_SSL3 fprintf(stderr, " -ssl3 - use SSLv3\n"); #endif #ifndef OPENSSL_NO_TLS1 fprintf(stderr, " -tls1 - use TLSv1\n"); #endif #ifndef OPENSSL_NO_TLS1_1 fprintf(stderr, " -tls1_1 - use TLSv1.1\n"); #endif #ifndef OPENSSL_NO_TLS1_2 fprintf(stderr, " -tls1_2 - use TLSv1.2\n"); #endif #ifndef OPENSSL_NO_DTLS fprintf(stderr, " -dtls - use DTLS\n"); #ifndef OPENSSL_NO_DTLS1 fprintf(stderr, " -dtls1 - use DTLSv1\n"); #endif #ifndef OPENSSL_NO_DTLS1_2 fprintf(stderr, " -dtls12 - use DTLSv1.2\n"); #endif #endif fprintf(stderr, " -CApath arg - PEM format directory of CA's\n"); fprintf(stderr, " -CAfile arg - PEM format file of CA's\n"); fprintf(stderr, " -s_cert arg - Server certificate file\n"); fprintf(stderr, " -s_key arg - Server key file (default: same as -cert)\n"); fprintf(stderr, " -c_cert arg - Client certificate file\n"); fprintf(stderr, " -c_key arg - Client key file (default: same as -c_cert)\n"); fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n"); fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n"); fprintf(stderr, " -bio_pair - Use BIO pairs\n"); fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n"); fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n"); fprintf(stderr, " -f - Test even cases that can't work\n"); fprintf(stderr, " -time - measure processor time used by client and server\n"); fprintf(stderr, " -zlib - use zlib compression\n"); #ifndef OPENSSL_NO_NEXTPROTONEG fprintf(stderr, " -npn_client - have client side offer NPN\n"); fprintf(stderr, " -npn_server - have server side offer NPN\n"); fprintf(stderr, " -npn_server_reject - have server reject NPN\n"); #endif fprintf(stderr, " -serverinfo_file file - have server use this file\n"); fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n"); fprintf(stderr, " -serverinfo_tack - have client offer and expect TACK\n"); fprintf(stderr, " -custom_ext - try various custom extension callbacks\n"); fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n"); fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n"); fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n"); fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n"); fprintf(stderr, " -alpn_expected <string> - the ALPN protocol that should be negotiated\n"); fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n"); fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n"); fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n"); fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n"); fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n"); #ifndef OPENSSL_NO_CT fprintf(stderr, " -noct - no certificate transparency\n"); fprintf(stderr, " -requestct - request certificate transparency\n"); fprintf(stderr, " -requirect - require certificate transparency\n"); #endif fprintf(stderr, " -sn_client <string> - have client request this servername\n"); fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n"); fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n"); fprintf(stderr, " -sn_expect1 - expected server 1\n"); fprintf(stderr, " -sn_expect2 - expected server 2\n"); fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n"); fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n"); fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n"); fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n"); fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n"); fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n"); fprintf(stderr, " -client_ktls - try to enable client KTLS\n"); fprintf(stderr, " -server_ktls - try to enable server KTLS\n"); fprintf(stderr, " -provider <name> - Load the given provider into the library context\n"); fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n"); } static void print_key_details(BIO *out, EVP_PKEY *key) { int keyid = EVP_PKEY_get_id(key); #ifndef OPENSSL_NO_EC if (keyid == EVP_PKEY_EC) { char group[80]; size_t size; if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size)) strcpy(group, "unknown group"); BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group); } else #endif { const char *algname; switch (keyid) { case EVP_PKEY_RSA: algname = "RSA"; break; case EVP_PKEY_DSA: algname = "DSA"; break; case EVP_PKEY_DH: algname = "DH"; break; default: algname = OBJ_nid2sn(keyid); break; } BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname); } } static void print_details(SSL *c_ssl, const char *prefix) { const SSL_CIPHER *ciph; int mdnid; X509 *cert; EVP_PKEY *pkey; ciph = SSL_get_current_cipher(c_ssl); BIO_printf(bio_stdout, "%s%s, cipher %s %s", prefix, SSL_get_version(c_ssl), SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph)); cert = SSL_get0_peer_certificate(c_ssl); if (cert != NULL) { EVP_PKEY* pubkey = X509_get0_pubkey(cert); if (pubkey != NULL) { BIO_puts(bio_stdout, ", "); print_key_details(bio_stdout, pubkey); } } if (SSL_get_peer_tmp_key(c_ssl, &pkey)) { BIO_puts(bio_stdout, ", temp key: "); print_key_details(bio_stdout, pkey); EVP_PKEY_free(pkey); } if (SSL_get_peer_signature_nid(c_ssl, &mdnid)) BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid)); BIO_printf(bio_stdout, "\n"); } static int protocol_from_string(const char *value) { struct protocol_versions { const char *name; int version; }; static const struct protocol_versions versions[] = { {"ssl3", SSL3_VERSION}, {"tls1", TLS1_VERSION}, {"tls1.1", TLS1_1_VERSION}, {"tls1.2", TLS1_2_VERSION}, {"tls1.3", TLS1_3_VERSION}, {"dtls1", DTLS1_VERSION}, {"dtls1.2", DTLS1_2_VERSION}}; size_t i; size_t n = OSSL_NELEM(versions); for (i = 0; i < n; i++) if (strcmp(versions[i].name, value) == 0) return versions[i].version; return -1; } static SSL_SESSION *read_session(const char *filename) { SSL_SESSION *sess; BIO *f = BIO_new_file(filename, "r"); if (f == NULL) { BIO_printf(bio_err, "Can't open session file %s\n", filename); ERR_print_errors(bio_err); return NULL; } sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL); if (sess == NULL) { BIO_printf(bio_err, "Can't parse session file %s\n", filename); ERR_print_errors(bio_err); } BIO_free(f); return sess; } static int write_session(const char *filename, SSL_SESSION *sess) { BIO *f; if (sess == NULL) { BIO_printf(bio_err, "No session information\n"); return 0; } f = BIO_new_file(filename, "w"); if (f == NULL) { BIO_printf(bio_err, "Can't open session file %s\n", filename); ERR_print_errors(bio_err); return 0; } PEM_write_bio_SSL_SESSION(f, sess); BIO_free(f); return 1; } static int set_protocol_version(const char *version, SSL *ssl, int setting) { if (version != NULL) { int ver = protocol_from_string(version); if (ver < 0) { BIO_printf(bio_err, "Error parsing: %s\n", version); return 0; } return SSL_ctrl(ssl, setting, ver, NULL); } return 1; } int main(int argc, char *argv[]) { const char *CApath = NULL, *CAfile = NULL; int badop = 0; enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM; int force = 0; int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0; int ret = EXIT_FAILURE; int client_auth = 0; int server_auth = 0, i; struct app_verify_arg app_verify_arg = { APP_CALLBACK_STRING, 0 }; SSL_CTX *c_ctx = NULL; const SSL_METHOD *meth = NULL; SSL *c_ssl = NULL; SSL *s_ssl = NULL; int number = 1, reuse = 0; int should_reuse = -1; int no_ticket = 0; int client_ktls = 0, server_ktls = 0; long bytes = 256L; #ifndef OPENSSL_NO_DH EVP_PKEY *dhpkey; int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0; int no_dhe = 0; #endif int no_psk = 0; int print_time = 0; clock_t s_time = 0, c_time = 0; #ifndef OPENSSL_NO_COMP int n, comp = 0; COMP_METHOD *cm = NULL; STACK_OF(SSL_COMP) *ssl_comp_methods = NULL; #endif int no_protocol; int min_version = 0, max_version = 0; #ifndef OPENSSL_NO_CT int ct_validation = 0; #endif SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL; STACK_OF(OPENSSL_STRING) *conf_args = NULL; char *arg = NULL, *argn = NULL; const char *provider = NULL, *config = NULL; OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL; OSSL_LIB_CTX *libctx = NULL; verbose = 0; debug = 0; test_open_streams(); bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT); s_cctx = SSL_CONF_CTX_new(); s_cctx2 = SSL_CONF_CTX_new(); c_cctx = SSL_CONF_CTX_new(); if (!s_cctx || !c_cctx || !s_cctx2) { ERR_print_errors(bio_err); goto end; } SSL_CONF_CTX_set_flags(s_cctx, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); SSL_CONF_CTX_set_flags(s_cctx2, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) { ERR_print_errors(bio_err); goto end; } if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) { ERR_print_errors(bio_err); goto end; } SSL_CONF_CTX_set_flags(c_cctx, SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE); if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) { ERR_print_errors(bio_err); goto end; } argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-F") == 0) { fprintf(stderr, "not compiled with FIPS support, so exiting without running.\n"); ret = EXIT_SUCCESS; goto end; } else if (strcmp(*argv, "-server_auth") == 0) server_auth = 1; else if (strcmp(*argv, "-client_auth") == 0) client_auth = 1; else if (strcmp(*argv, "-v") == 0) verbose = 1; else if (strcmp(*argv, "-d") == 0) debug = 1; else if (strcmp(*argv, "-reuse") == 0) reuse = 1; else if (strcmp(*argv, "-no_dhe") == 0) #ifdef OPENSSL_NO_DH ; #else no_dhe = 1; else if (strcmp(*argv, "-dhe512") == 0) dhe512 = 1; else if (strcmp(*argv, "-dhe1024dsa") == 0) dhe1024dsa = 1; else if (strcmp(*argv, "-dhe4096") == 0) dhe4096 = 1; #endif else if (strcmp(*argv, "-no_ecdhe") == 0) ; else if (strcmp(*argv, "-psk") == 0) { if (--argc < 1) goto bad; psk_key = *(++argv); #ifndef OPENSSL_NO_PSK if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) { BIO_printf(bio_err, "Not a hex number '%s'\n", *argv); goto bad; } #else no_psk = 1; #endif } else if (strcmp(*argv, "-tls1_2") == 0) { tls1_2 = 1; } else if (strcmp(*argv, "-tls1_1") == 0) { tls1_1 = 1; } else if (strcmp(*argv, "-tls1") == 0) { tls1 = 1; } else if (strcmp(*argv, "-ssl3") == 0) { ssl3 = 1; } else if (strcmp(*argv, "-dtls1") == 0) { dtls1 = 1; } else if (strcmp(*argv, "-dtls12") == 0) { dtls12 = 1; } else if (strcmp(*argv, "-dtls") == 0) { dtls = 1; } else if (HAS_PREFIX(*argv, "-num")) { if (--argc < 1) goto bad; number = atoi(*(++argv)); if (number == 0) number = 1; } else if (strcmp(*argv, "-bytes") == 0) { if (--argc < 1) goto bad; bytes = atol(*(++argv)); if (bytes == 0L) bytes = 1L; i = strlen(argv[0]); if (argv[0][i - 1] == 'k') bytes *= 1024L; if (argv[0][i - 1] == 'm') bytes *= 1024L * 1024L; } else if (strcmp(*argv, "-cipher") == 0) { if (--argc < 1) goto bad; cipher = *(++argv); } else if (strcmp(*argv, "-ciphersuites") == 0) { if (--argc < 1) goto bad; ciphersuites = *(++argv); } else if (strcmp(*argv, "-CApath") == 0) { if (--argc < 1) goto bad; CApath = *(++argv); } else if (strcmp(*argv, "-CAfile") == 0) { if (--argc < 1) goto bad; CAfile = *(++argv); } else if (strcmp(*argv, "-bio_pair") == 0) { bio_type = BIO_PAIR; } #ifndef OPENSSL_NO_SOCK else if (strcmp(*argv, "-ipv4") == 0) { bio_type = BIO_IPV4; } else if (strcmp(*argv, "-ipv6") == 0) { bio_type = BIO_IPV6; } #endif else if (strcmp(*argv, "-f") == 0) { force = 1; } else if (strcmp(*argv, "-time") == 0) { print_time = 1; } #ifndef OPENSSL_NO_CT else if (strcmp(*argv, "-noct") == 0) { ct_validation = 0; } else if (strcmp(*argv, "-ct") == 0) { ct_validation = 1; } #endif #ifndef OPENSSL_NO_COMP else if (strcmp(*argv, "-zlib") == 0) { comp = COMP_ZLIB; } #endif else if (strcmp(*argv, "-app_verify") == 0) { app_verify_arg.app_verify = 1; } #ifndef OPENSSL_NO_NEXTPROTONEG else if (strcmp(*argv, "-npn_client") == 0) { npn_client = 1; } else if (strcmp(*argv, "-npn_server") == 0) { npn_server = 1; } else if (strcmp(*argv, "-npn_server_reject") == 0) { npn_server_reject = 1; } #endif else if (strcmp(*argv, "-serverinfo_sct") == 0) { serverinfo_sct = 1; } else if (strcmp(*argv, "-serverinfo_tack") == 0) { serverinfo_tack = 1; } else if (strcmp(*argv, "-serverinfo_file") == 0) { if (--argc < 1) goto bad; serverinfo_file = *(++argv); } else if (strcmp(*argv, "-custom_ext") == 0) { custom_ext = 1; } else if (strcmp(*argv, "-alpn_client") == 0) { if (--argc < 1) goto bad; alpn_client = *(++argv); } else if (strcmp(*argv, "-alpn_server") == 0 || strcmp(*argv, "-alpn_server1") == 0) { if (--argc < 1) goto bad; alpn_server = *(++argv); } else if (strcmp(*argv, "-alpn_server2") == 0) { if (--argc < 1) goto bad; alpn_server2 = *(++argv); } else if (strcmp(*argv, "-alpn_expected") == 0) { if (--argc < 1) goto bad; alpn_expected = *(++argv); } else if (strcmp(*argv, "-server_min_proto") == 0) { if (--argc < 1) goto bad; server_min_proto = *(++argv); } else if (strcmp(*argv, "-server_max_proto") == 0) { if (--argc < 1) goto bad; server_max_proto = *(++argv); } else if (strcmp(*argv, "-client_min_proto") == 0) { if (--argc < 1) goto bad; client_min_proto = *(++argv); } else if (strcmp(*argv, "-client_max_proto") == 0) { if (--argc < 1) goto bad; client_max_proto = *(++argv); } else if (strcmp(*argv, "-should_negotiate") == 0) { if (--argc < 1) goto bad; should_negotiate = *(++argv); } else if (strcmp(*argv, "-sn_client") == 0) { if (--argc < 1) goto bad; sn_client = *(++argv); } else if (strcmp(*argv, "-sn_server1") == 0) { if (--argc < 1) goto bad; sn_server1 = *(++argv); } else if (strcmp(*argv, "-sn_server2") == 0) { if (--argc < 1) goto bad; sn_server2 = *(++argv); } else if (strcmp(*argv, "-sn_expect1") == 0) { sn_expect = 1; } else if (strcmp(*argv, "-sn_expect2") == 0) { sn_expect = 2; } else if (strcmp(*argv, "-server_sess_out") == 0) { if (--argc < 1) goto bad; server_sess_out = *(++argv); } else if (strcmp(*argv, "-server_sess_in") == 0) { if (--argc < 1) goto bad; server_sess_in = *(++argv); } else if (strcmp(*argv, "-client_sess_out") == 0) { if (--argc < 1) goto bad; client_sess_out = *(++argv); } else if (strcmp(*argv, "-client_sess_in") == 0) { if (--argc < 1) goto bad; client_sess_in = *(++argv); } else if (strcmp(*argv, "-should_reuse") == 0) { if (--argc < 1) goto bad; should_reuse = !!atoi(*(++argv)); } else if (strcmp(*argv, "-no_ticket") == 0) { no_ticket = 1; } else if (strcmp(*argv, "-client_ktls") == 0) { client_ktls = 1; } else if (strcmp(*argv, "-server_ktls") == 0) { server_ktls = 1; } else if (strcmp(*argv, "-provider") == 0) { if (--argc < 1) goto bad; provider = *(++argv); } else if (strcmp(*argv, "-config") == 0) { if (--argc < 1) goto bad; config = *(++argv); } else { int rv; arg = argv[0]; argn = argv[1]; rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv); if (rv == 0) rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv); if (rv > 0) { if (rv == 1) argn = NULL; if (!conf_args) { conf_args = sk_OPENSSL_STRING_new_null(); if (!conf_args) goto end; } if (!sk_OPENSSL_STRING_push(conf_args, arg)) goto end; if (!sk_OPENSSL_STRING_push(conf_args, argn)) goto end; continue; } if (rv == -3) BIO_printf(bio_err, "Missing argument for %s\n", arg); else if (rv < 0) BIO_printf(bio_err, "Error with command %s\n", arg); else if (rv == 0) BIO_printf(bio_err, "unknown option %s\n", arg); badop = 1; break; } argc--; argv++; } if (badop) { bad: sv_usage(); goto end; } if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) { fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should " "be requested.\n"); goto end; } #ifdef OPENSSL_NO_SSL3 if (ssl3) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1 if (tls1) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1_1 if (tls1_1) no_protocol = 1; else #endif #ifdef OPENSSL_NO_TLS1_2 if (tls1_2) no_protocol = 1; else #endif #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1) if (dtls1) no_protocol = 1; else #endif #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2) if (dtls12) no_protocol = 1; else #endif no_protocol = 0; if (no_protocol) { fprintf(stderr, "Testing was requested for a disabled protocol. " "Skipping tests.\n"); ret = EXIT_SUCCESS; goto end; } if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1 && !reuse && !force) { fprintf(stderr, "This case cannot work. Use -f to perform " "the test anyway (and\n-d to see what happens), " "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n" "to avoid protocol mismatch.\n"); goto end; } if (print_time) { if (bio_type == BIO_MEM) { fprintf(stderr, "Using BIO pair (-bio_pair)\n"); bio_type = BIO_PAIR; } if (number < 50 && !force) fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n"); } #ifndef OPENSSL_NO_COMP if (comp == COMP_ZLIB) cm = COMP_zlib(); if (cm != NULL) { if (SSL_COMP_add_compression_method(comp, cm) != 0) { fprintf(stderr, "Failed to add compression method\n"); ERR_print_errors_fp(stderr); } } else { fprintf(stderr, "Warning: %s compression not supported\n", comp == COMP_ZLIB ? "zlib" : "unknown"); ERR_print_errors_fp(stderr); } ssl_comp_methods = SSL_COMP_get_compression_methods(); n = sk_SSL_COMP_num(ssl_comp_methods); if (n) { int j; printf("Available compression methods:"); for (j = 0; j < n; j++) { SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j); printf(" %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c)); } printf("\n"); } #endif #ifndef OPENSSL_NO_TLS meth = TLS_method(); if (ssl3) { min_version = SSL3_VERSION; max_version = SSL3_VERSION; } else if (tls1) { min_version = TLS1_VERSION; max_version = TLS1_VERSION; } else if (tls1_1) { min_version = TLS1_1_VERSION; max_version = TLS1_1_VERSION; } else if (tls1_2) { min_version = TLS1_2_VERSION; max_version = TLS1_2_VERSION; } else { min_version = 0; # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH) max_version = TLS1_2_VERSION; # else max_version = 0; # endif } #endif #ifndef OPENSSL_NO_DTLS if (dtls || dtls1 || dtls12) { meth = DTLS_method(); if (dtls1) { min_version = DTLS1_VERSION; max_version = DTLS1_VERSION; } else if (dtls12) { min_version = DTLS1_2_VERSION; max_version = DTLS1_2_VERSION; } else { min_version = 0; max_version = 0; } } #endif if (provider != NULL && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider)) goto end; c_ctx = SSL_CTX_new_ex(libctx, NULL, meth); s_ctx = SSL_CTX_new_ex(libctx, NULL, meth); s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) { ERR_print_errors(bio_err); goto end; } SSL_CTX_set_security_level(c_ctx, 0); SSL_CTX_set_security_level(s_ctx, 0); SSL_CTX_set_security_level(s_ctx2, 0); if (no_ticket) { SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET); SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET); } if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0) goto end; if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0) goto end; if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0) goto end; if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0) goto end; if (cipher != NULL) { if (strcmp(cipher, "") == 0) { if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) { if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) { ERR_clear_error(); } else { ERR_print_errors(bio_err); goto end; } } else { fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n"); goto end; } } else { if (!SSL_CTX_set_cipher_list(c_ctx, cipher) || !SSL_CTX_set_cipher_list(s_ctx, cipher) || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) { ERR_print_errors(bio_err); goto end; } } } if (ciphersuites != NULL) { if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites) || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites) || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) { ERR_print_errors(bio_err); goto end; } } #ifndef OPENSSL_NO_CT if (ct_validation && !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) { ERR_print_errors(bio_err); goto end; } #endif SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx); SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx); SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2); for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) { int rv; arg = sk_OPENSSL_STRING_value(conf_args, i); argn = sk_OPENSSL_STRING_value(conf_args, i + 1); rv = SSL_CONF_cmd(c_cctx, arg, argn); if (rv == -2) { rv = SSL_CONF_cmd(s_cctx2, arg, argn); if (rv > 0) rv = SSL_CONF_cmd(s_cctx, arg, argn); } if (rv <= 0) { BIO_printf(bio_err, "Error processing %s %s\n", arg, argn ? argn : ""); ERR_print_errors(bio_err); goto end; } } if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) { BIO_puts(bio_err, "Error finishing context\n"); ERR_print_errors(bio_err); goto end; } #ifndef OPENSSL_NO_DH if (!no_dhe) { if (dhe1024dsa) dhpkey = get_dh1024dsa(libctx); else if (dhe512) dhpkey = get_dh512(libctx); else if (dhe4096) dhpkey = get_dh4096(libctx); else dhpkey = get_dh2048(libctx); if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) { EVP_PKEY_free(dhpkey); BIO_puts(bio_err, "Error getting DH parameters\n"); ERR_print_errors(bio_err); goto end; } if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey)) EVP_PKEY_free(dhpkey); if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey)) EVP_PKEY_free(dhpkey); } #endif if (!(SSL_CTX_load_verify_file(s_ctx, CAfile) || SSL_CTX_load_verify_dir(s_ctx, CApath)) || !SSL_CTX_set_default_verify_paths(s_ctx) || !(SSL_CTX_load_verify_file(s_ctx2, CAfile) || SSL_CTX_load_verify_dir(s_ctx2, CApath)) || !SSL_CTX_set_default_verify_paths(s_ctx2) || !(SSL_CTX_load_verify_file(c_ctx, CAfile) || SSL_CTX_load_verify_dir(c_ctx, CApath)) || !SSL_CTX_set_default_verify_paths(c_ctx)) { ERR_print_errors(bio_err); } #ifndef OPENSSL_NO_CT if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) || !SSL_CTX_set_default_ctlog_list_file(s_ctx2) || !SSL_CTX_set_default_ctlog_list_file(c_ctx)) { ERR_print_errors(bio_err); } #endif if (client_auth) { printf("client authentication\n"); SSL_CTX_set_verify(s_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback); SSL_CTX_set_verify(s_ctx2, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback); SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg); SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback, &app_verify_arg); } if (server_auth) { printf("server authentication\n"); SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback); SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg); } { int session_id_context = 0; if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof(session_id_context)) || !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context, sizeof(session_id_context))) { ERR_print_errors(bio_err); goto end; } } if (psk_key != NULL) { if (no_psk) { ret = EXIT_SUCCESS; goto end; } #ifndef OPENSSL_NO_PSK SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback); SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback); SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback); if (debug) BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n"); if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") || !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) { BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n"); ERR_print_errors(bio_err); goto end; } #endif } #ifndef OPENSSL_NO_NEXTPROTONEG if (npn_client) { SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL); } if (npn_server) { if (npn_server_reject) { BIO_printf(bio_err, "Can't have both -npn_server and -npn_server_reject\n"); goto end; } SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL); SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL); } if (npn_server_reject) { SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL); SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL); } #endif if (serverinfo_sct) { if (!SSL_CTX_add_client_custom_ext(c_ctx, TLSEXT_TYPE_signed_certificate_timestamp, NULL, NULL, NULL, serverinfo_cli_parse_cb, NULL)) { BIO_printf(bio_err, "Error adding SCT extension\n"); goto end; } } if (serverinfo_tack) { if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE, NULL, NULL, NULL, serverinfo_cli_parse_cb, NULL)) { BIO_printf(bio_err, "Error adding TACK extension\n"); goto end; } } if (serverinfo_file) if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) || !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) { BIO_printf(bio_err, "missing serverinfo file\n"); goto end; } if (custom_ext) { if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0, custom_ext_0_cli_add_cb, NULL, NULL, custom_ext_0_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1, custom_ext_1_cli_add_cb, NULL, NULL, custom_ext_1_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2, custom_ext_2_cli_add_cb, NULL, NULL, custom_ext_2_cli_parse_cb, NULL) || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3, custom_ext_3_cli_add_cb, NULL, NULL, custom_ext_3_cli_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0, custom_ext_0_srv_add_cb, NULL, NULL, custom_ext_0_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0, custom_ext_0_srv_add_cb, NULL, NULL, custom_ext_0_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1, custom_ext_1_srv_add_cb, NULL, NULL, custom_ext_1_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1, custom_ext_1_srv_add_cb, NULL, NULL, custom_ext_1_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2, custom_ext_2_srv_add_cb, NULL, NULL, custom_ext_2_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2, custom_ext_2_srv_add_cb, NULL, NULL, custom_ext_2_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3, custom_ext_3_srv_add_cb, NULL, NULL, custom_ext_3_srv_parse_cb, NULL) || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3, custom_ext_3_srv_add_cb, NULL, NULL, custom_ext_3_srv_parse_cb, NULL)) { BIO_printf(bio_err, "Error setting custom extensions\n"); goto end; } } if (alpn_server) SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server); if (alpn_server2) SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2); if (alpn_client) { size_t alpn_len; unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client); if (alpn == NULL) { BIO_printf(bio_err, "Error parsing -alpn_client argument\n"); goto end; } if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) { BIO_printf(bio_err, "Error setting ALPN\n"); OPENSSL_free(alpn); goto end; } OPENSSL_free(alpn); } if (server_sess_in != NULL) { server_sess = read_session(server_sess_in); if (server_sess == NULL) goto end; } if (client_sess_in != NULL) { client_sess = read_session(client_sess_in); if (client_sess == NULL) goto end; } if (server_sess_out != NULL || server_sess_in != NULL) { char *keys; long size; size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0); keys = OPENSSL_zalloc(size); if (keys == NULL) goto end; SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size); OPENSSL_free(keys); } if (sn_server1 != NULL || sn_server2 != NULL) SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb); c_ssl = SSL_new(c_ctx); s_ssl = SSL_new(s_ctx); if (c_ssl == NULL || s_ssl == NULL) goto end; if (sn_client) SSL_set_tlsext_host_name(c_ssl, sn_client); if (client_ktls) SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS); if (server_ktls) SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS); if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION)) goto end; if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION)) goto end; if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION)) goto end; if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION)) goto end; if (server_sess) { if (SSL_CTX_add_session(s_ctx, server_sess) == 0) { BIO_printf(bio_err, "Can't add server session\n"); ERR_print_errors(bio_err); goto end; } } BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes); for (i = 0; i < number; i++) { if (!reuse) { if (!SSL_set_session(c_ssl, NULL)) { BIO_printf(bio_err, "Failed to set session\n"); goto end; } } if (client_sess_in != NULL) { if (SSL_set_session(c_ssl, client_sess) == 0) { BIO_printf(bio_err, "Can't set client session\n"); ERR_print_errors(bio_err); goto end; } } switch (bio_type) { case BIO_MEM: ret = doit(s_ssl, c_ssl, bytes); break; case BIO_PAIR: ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time); break; #ifndef OPENSSL_NO_SOCK case BIO_IPV4: ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4, bytes, &s_time, &c_time); break; case BIO_IPV6: ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6, bytes, &s_time, &c_time); break; #else case BIO_IPV4: case BIO_IPV6: ret = EXIT_FAILURE; goto end; #endif } if (ret != EXIT_SUCCESS) break; } if (should_negotiate && ret == EXIT_SUCCESS && strcmp(should_negotiate, "fail-server") != 0 && strcmp(should_negotiate, "fail-client") != 0) { int version = protocol_from_string(should_negotiate); if (version < 0) { BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate); ret = EXIT_FAILURE; goto end; } if (SSL_version(c_ssl) != version) { BIO_printf(bio_err, "Unexpected version negotiated. " "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl)); ret = EXIT_FAILURE; goto end; } } if (should_reuse != -1) { if (SSL_session_reused(s_ssl) != should_reuse || SSL_session_reused(c_ssl) != should_reuse) { BIO_printf(bio_err, "Unexpected session reuse state. " "Expected: %d, server: %d, client: %d\n", should_reuse, SSL_session_reused(s_ssl), SSL_session_reused(c_ssl)); ret = EXIT_FAILURE; goto end; } } if (server_sess_out != NULL) { if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) { ret = EXIT_FAILURE; goto end; } } if (client_sess_out != NULL) { if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) { ret = EXIT_FAILURE; goto end; } } if (!verbose) { print_details(c_ssl, ""); } if (print_time) { #ifdef CLOCKS_PER_SEC BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n" "Approximate total client time: %6.2f s\n", (double)s_time / CLOCKS_PER_SEC, (double)c_time / CLOCKS_PER_SEC); #else BIO_printf(bio_stdout, "Approximate total server time: %6.2f units\n" "Approximate total client time: %6.2f units\n", (double)s_time, (double)c_time); #endif } end: SSL_free(s_ssl); SSL_free(c_ssl); SSL_CTX_free(s_ctx); SSL_CTX_free(s_ctx2); SSL_CTX_free(c_ctx); SSL_CONF_CTX_free(s_cctx); SSL_CONF_CTX_free(s_cctx2); SSL_CONF_CTX_free(c_cctx); sk_OPENSSL_STRING_free(conf_args); BIO_free(bio_stdout); SSL_SESSION_free(server_sess); SSL_SESSION_free(client_sess); OSSL_PROVIDER_unload(defctxnull); OSSL_PROVIDER_unload(thisprov); OSSL_LIB_CTX_free(libctx); test_close_streams(); EXIT(ret); } #ifndef OPENSSL_NO_SOCK int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count, clock_t *s_time, clock_t *c_time) { long cw_num = count, cr_num = count, sw_num = count, sr_num = count; BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; BIO *acpt = NULL, *server = NULL, *client = NULL; char addr_str[40]; int ret = EXIT_FAILURE; int err_in_client = 0; int err_in_server = 0; acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0" : "[::1]:0"); if (acpt == NULL) goto err; BIO_set_accept_ip_family(acpt, family); BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR); if (BIO_do_accept(acpt) <= 0) goto err; BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt)); client = BIO_new_connect(addr_str); if (!client) goto err; BIO_set_conn_ip_family(client, family); if (BIO_set_nbio(client, 1) <= 0) goto err; if (BIO_set_nbio(acpt, 1) <= 0) goto err; { int st_connect = 0, st_accept = 0; while (!st_connect || !st_accept) { if (!st_connect) { if (BIO_do_connect(client) <= 0) { if (!BIO_should_retry(client)) goto err; } else { st_connect = 1; } } if (!st_accept) { if (BIO_do_accept(acpt) <= 0) { if (!BIO_should_retry(acpt)) goto err; } else { st_accept = 1; } } } } server = BIO_pop(acpt); BIO_free_all(acpt); acpt = NULL; s_ssl_bio = BIO_new(BIO_f_ssl()); if (!s_ssl_bio) goto err; c_ssl_bio = BIO_new(BIO_f_ssl()); if (!c_ssl_bio) goto err; SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, client, client); (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, server, server); (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); do { { char cbuf[1024 * 8]; int i, r; clock_t c_clock = clock(); memset(cbuf, 0, sizeof(cbuf)); if (debug) if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); if (cw_num > 0) { if (cw_num > (long)sizeof(cbuf)) i = sizeof(cbuf); else i = (int)cw_num; r = BIO_write(c_ssl_bio, cbuf, i); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT (write)\n"); err_in_client = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", r); cw_num -= r; } } if (cr_num > 0) { r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT (read)\n"); err_in_client = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", r); cr_num -= r; } } *c_time += (clock() - c_clock); } { char sbuf[1024 * 8]; int i, r; clock_t s_clock = clock(); memset(sbuf, 0, sizeof(sbuf)); if (debug) if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); if (sw_num > 0) { if (sw_num > (long)sizeof(sbuf)) i = sizeof(sbuf); else i = (int)sw_num; r = BIO_write(s_ssl_bio, sbuf, i); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER (write)\n"); err_in_server = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server wrote %d\n", r); sw_num -= r; } } if (sr_num > 0) { r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER (read)\n"); err_in_server = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server read %d\n", r); sr_num -= r; } } *s_time += (clock() - s_clock); } } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); if (verbose) { print_details(c_ssl, "DONE via TCP connect: "); if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)) && BIO_get_ktls_recv(SSL_get_rbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n"); else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n"); else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl))) BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n"); if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)) && BIO_get_ktls_recv(SSL_get_rbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n"); else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n"); else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl))) BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n"); } # ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto end; # endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (verify_alpn(c_ssl, s_ssl) < 0 || verify_servername(c_ssl, s_ssl) < 0) goto err; if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } # ifndef OPENSSL_NO_NEXTPROTONEG end: # endif ret = EXIT_SUCCESS; err: ERR_print_errors(bio_err); BIO_free_all(acpt); BIO_free(server); BIO_free(client); BIO_free(s_ssl_bio); BIO_free(c_ssl_bio); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } #endif int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count, clock_t *s_time, clock_t *c_time) { long cw_num = count, cr_num = count, sw_num = count, sr_num = count; BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL; int ret = EXIT_FAILURE; int err_in_client = 0; int err_in_server = 0; size_t bufsiz = 256; if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz)) goto err; if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz)) goto err; s_ssl_bio = BIO_new(BIO_f_ssl()); if (!s_ssl_bio) goto err; c_ssl_bio = BIO_new(BIO_f_ssl()); if (!c_ssl_bio) goto err; SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, client, client); (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, server, server); (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); do { { char cbuf[1024 * 8]; int i, r; clock_t c_clock = clock(); memset(cbuf, 0, sizeof(cbuf)); if (debug) if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); if (cw_num > 0) { if (cw_num > (long)sizeof(cbuf)) i = sizeof(cbuf); else i = (int)cw_num; r = BIO_write(c_ssl_bio, cbuf, i); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", r); cw_num -= r; } } if (cr_num > 0) { r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); if (r < 0) { if (!BIO_should_retry(c_ssl_bio)) { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", r); cr_num -= r; } } *c_time += (clock() - c_clock); } { char sbuf[1024 * 8]; int i, r; clock_t s_clock = clock(); memset(sbuf, 0, sizeof(sbuf)); if (debug) if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); if (sw_num > 0) { if (sw_num > (long)sizeof(sbuf)) i = sizeof(sbuf); else i = (int)sw_num; r = BIO_write(s_ssl_bio, sbuf, i); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server wrote %d\n", r); sw_num -= r; } } if (sr_num > 0) { r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); if (r < 0) { if (!BIO_should_retry(s_ssl_bio)) { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; goto err; } } else if (r == 0) { fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); goto err; } else { if (debug) printf("server read %d\n", r); sr_num -= r; } } *s_time += (clock() - s_clock); } { size_t r1, r2; BIO *io1 = server_io, *io2 = client_io; static int prev_progress = 1; int progress = 0; do { size_t num; int r; r1 = BIO_ctrl_pending(io1); r2 = BIO_ctrl_get_write_guarantee(io2); num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) num = INT_MAX; r = BIO_nread(io1, &dataptr, (int)num); assert(r > 0); assert(r <= (int)num); num = r; r = BIO_write(io2, dataptr, (int)num); if (r != (int)num) { fprintf(stderr, "ERROR: BIO_write could not write " "BIO_ctrl_get_write_guarantee() bytes"); goto err; } progress = 1; if (debug) printf((io1 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } while (r1 && r2); { size_t num; int r; r1 = BIO_ctrl_pending(io2); r2 = BIO_ctrl_get_read_request(io1); num = r1; if (r2 < num) num = r2; if (num) { char *dataptr; if (INT_MAX < num) num = INT_MAX; if (num > 1) --num; r = BIO_nwrite0(io1, &dataptr); assert(r > 0); if (r < (int)num) num = r; r = BIO_read(io2, dataptr, (int)num); if (r != (int)num) { fprintf(stderr, "ERROR: BIO_read could not read " "BIO_ctrl_pending() bytes"); goto err; } progress = 1; r = BIO_nwrite(io1, &dataptr, (int)num); if (r != (int)num) { fprintf(stderr, "ERROR: BIO_nwrite() did not accept " "BIO_nwrite0() bytes"); goto err; } if (debug) printf((io2 == client_io) ? "C->S relaying: %d bytes\n" : "S->C relaying: %d bytes\n", (int)num); } } if (!progress && !prev_progress) if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) { fprintf(stderr, "ERROR: got stuck\n"); fprintf(stderr, " ERROR.\n"); goto err; } prev_progress = progress; } } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); if (verbose) print_details(c_ssl, "DONE via BIO pair: "); #ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto end; #endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (verify_alpn(c_ssl, s_ssl) < 0 || verify_servername(c_ssl, s_ssl) < 0) goto err; if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } #ifndef OPENSSL_NO_NEXTPROTONEG end: #endif ret = EXIT_SUCCESS; err: ERR_print_errors(bio_err); BIO_free(server); BIO_free(server_io); BIO_free(client); BIO_free(client_io); BIO_free(s_ssl_bio); BIO_free(c_ssl_bio); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } #define W_READ 1 #define W_WRITE 2 #define C_DONE 1 #define S_DONE 2 int doit(SSL *s_ssl, SSL *c_ssl, long count) { char *cbuf = NULL, *sbuf = NULL; long bufsiz; long cw_num = count, cr_num = count; long sw_num = count, sr_num = count; int ret = EXIT_FAILURE; BIO *c_to_s = NULL; BIO *s_to_c = NULL; BIO *c_bio = NULL; BIO *s_bio = NULL; int c_r, c_w, s_r, s_w; int i, j; int done = 0; int c_write, s_write; int do_server = 0, do_client = 0; int max_frag = 5 * 1024; int err_in_client = 0; int err_in_server = 0; bufsiz = count > 40 * 1024 ? 40 * 1024 : count; if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL) goto err; if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL) goto err; c_to_s = BIO_new(BIO_s_mem()); s_to_c = BIO_new(BIO_s_mem()); if ((s_to_c == NULL) || (c_to_s == NULL)) { ERR_print_errors(bio_err); goto err; } c_bio = BIO_new(BIO_f_ssl()); s_bio = BIO_new(BIO_f_ssl()); if ((c_bio == NULL) || (s_bio == NULL)) { ERR_print_errors(bio_err); goto err; } SSL_set_connect_state(c_ssl); SSL_set_bio(c_ssl, s_to_c, c_to_s); SSL_set_max_send_fragment(c_ssl, max_frag); BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE); if (!BIO_up_ref(c_to_s)) { c_to_s = NULL; s_to_c = NULL; goto err; } if (!BIO_up_ref(s_to_c)) { s_to_c = NULL; goto err; } SSL_set_accept_state(s_ssl); SSL_set_bio(s_ssl, c_to_s, s_to_c); c_to_s = NULL; s_to_c = NULL; SSL_set_max_send_fragment(s_ssl, max_frag); BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE); c_r = 0; s_r = 1; c_w = 1; s_w = 0; c_write = 1, s_write = 0; for (;;) { do_server = 0; do_client = 0; i = (int)BIO_pending(s_bio); if ((i && s_r) || s_w) do_server = 1; i = (int)BIO_pending(c_bio); if ((i && c_r) || c_w) do_client = 1; if (do_server && debug) { if (SSL_in_init(s_ssl)) printf("server waiting in SSL_accept - %s\n", SSL_state_string_long(s_ssl)); } if (do_client && debug) { if (SSL_in_init(c_ssl)) printf("client waiting in SSL_connect - %s\n", SSL_state_string_long(c_ssl)); } if (!do_client && !do_server) { fprintf(stdout, "ERROR IN STARTUP\n"); ERR_print_errors(bio_err); goto err; } if (do_client && !(done & C_DONE)) { if (c_write) { j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num; i = BIO_write(c_bio, cbuf, j); if (i < 0) { c_r = 0; c_w = 0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r = 1; if (BIO_should_write(c_bio)) c_w = 1; } else { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client wrote %d\n", i); s_r = 1; c_write = 0; cw_num -= i; if (max_frag > 1029) SSL_set_max_send_fragment(c_ssl, max_frag -= 5); } } else { i = BIO_read(c_bio, cbuf, bufsiz); if (i < 0) { c_r = 0; c_w = 0; if (BIO_should_retry(c_bio)) { if (BIO_should_read(c_bio)) c_r = 1; if (BIO_should_write(c_bio)) c_w = 1; } else { fprintf(stderr, "ERROR in CLIENT\n"); err_in_client = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); goto err; } else { if (debug) printf("client read %d\n", i); cr_num -= i; if (sw_num > 0) { s_write = 1; s_w = 1; } if (cr_num <= 0) { s_write = 1; s_w = 1; done = S_DONE | C_DONE; } } } } if (do_server && !(done & S_DONE)) { if (!s_write) { i = BIO_read(s_bio, sbuf, bufsiz); if (i < 0) { s_r = 0; s_w = 0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r = 1; if (BIO_should_write(s_bio)) s_w = 1; } else { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_read\n"); goto err; } else { if (debug) printf("server read %d\n", i); sr_num -= i; if (cw_num > 0) { c_write = 1; c_w = 1; } if (sr_num <= 0) { s_write = 1; s_w = 1; c_write = 0; } } } else { j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num; i = BIO_write(s_bio, sbuf, j); if (i < 0) { s_r = 0; s_w = 0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r = 1; if (BIO_should_write(s_bio)) s_w = 1; } else { fprintf(stderr, "ERROR in SERVER\n"); err_in_server = 1; ERR_print_errors(bio_err); goto err; } } else if (i == 0) { ERR_print_errors(bio_err); fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_write\n"); goto err; } else { if (debug) printf("server wrote %d\n", i); sw_num -= i; s_write = 0; c_r = 1; if (sw_num <= 0) done |= S_DONE; if (max_frag > 1029) SSL_set_max_send_fragment(s_ssl, max_frag -= 5); } } } if ((done & S_DONE) && (done & C_DONE)) break; } if (verbose) print_details(c_ssl, "DONE: "); #ifndef OPENSSL_NO_NEXTPROTONEG if (verify_npn(c_ssl, s_ssl) < 0) goto err; #endif if (verify_serverinfo() < 0) { fprintf(stderr, "Server info verify error\n"); goto err; } if (custom_ext_error) { fprintf(stderr, "Custom extension error\n"); goto err; } ret = EXIT_SUCCESS; err: BIO_free(c_to_s); BIO_free(s_to_c); BIO_free_all(c_bio); BIO_free_all(s_bio); OPENSSL_free(cbuf); OPENSSL_free(sbuf); if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0) ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE; else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0) ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE; return ret; } static int verify_callback(int ok, X509_STORE_CTX *ctx) { char *s, buf[256]; s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)), buf, sizeof(buf)); if (s != NULL) { if (ok) printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf); else { fprintf(stderr, "depth=%d error=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), X509_STORE_CTX_get_error(ctx), buf); } } if (ok == 0) { int i = X509_STORE_CTX_get_error(ctx); switch (i) { default: fprintf(stderr, "Error string: %s\n", X509_verify_cert_error_string(i)); break; case X509_V_ERR_CERT_NOT_YET_VALID: case X509_V_ERR_CERT_HAS_EXPIRED: case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: ok = 1; break; } } return ok; } static int app_verify_callback(X509_STORE_CTX *ctx, void *arg) { int ok = 1; struct app_verify_arg *cb_arg = arg; if (cb_arg->app_verify) { char *s = NULL, buf[256]; X509 *c = X509_STORE_CTX_get0_cert(ctx); printf("In app_verify_callback, allowing cert. "); printf("Arg is: %s\n", cb_arg->string); printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n", (void *)ctx, (void *)c); if (c) s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256); if (s != NULL) { printf("cert depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf); } return 1; } ok = X509_verify_cert(ctx); return ok; } #ifndef OPENSSL_NO_PSK static int psk_key2bn(const char *pskkey, unsigned char *psk, unsigned int max_psk_len) { int ret; BIGNUM *bn = NULL; ret = BN_hex2bn(&bn, pskkey); if (!ret) { BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", pskkey); BN_free(bn); return 0; } if (BN_num_bytes(bn) > (int)max_psk_len) { BIO_printf(bio_err, "psk buffer of callback is too small (%d) for key (%d)\n", max_psk_len, BN_num_bytes(bn)); BN_free(bn); return 0; } ret = BN_bn2bin(bn, psk); BN_free(bn); return ret; } static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len) { int ret; unsigned int psk_len = 0; ret = BIO_snprintf(identity, max_identity_len, "Client_identity"); if (ret < 0) goto out_err; if (debug) fprintf(stderr, "client: created identity '%s' len=%d\n", identity, ret); ret = psk_key2bn(psk_key, psk, max_psk_len); if (ret < 0) goto out_err; psk_len = ret; out_err: return psk_len; } static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { unsigned int psk_len = 0; if (strcmp(identity, "Client_identity") != 0) { BIO_printf(bio_err, "server: PSK error: client identity not found\n"); return 0; } psk_len = psk_key2bn(psk_key, psk, max_psk_len); return psk_len; } #endif
test
openssl/test/ssl_old_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/e_os2.h> #include <openssl/evp.h> #include "crypto/ecx.h" #include "curve448_local.h" #include "testutil.h" static unsigned int max = 1000; static unsigned int verbose = 0; static const uint8_t in_scalar1[56] = { 0x3d, 0x26, 0x2f, 0xdd, 0xf9, 0xec, 0x8e, 0x88, 0x49, 0x52, 0x66, 0xfe, 0xa1, 0x9a, 0x34, 0xd2, 0x88, 0x82, 0xac, 0xef, 0x04, 0x51, 0x04, 0xd0, 0xd1, 0xaa, 0xe1, 0x21, 0x70, 0x0a, 0x77, 0x9c, 0x98, 0x4c, 0x24, 0xf8, 0xcd, 0xd7, 0x8f, 0xbf, 0xf4, 0x49, 0x43, 0xeb, 0xa3, 0x68, 0xf5, 0x4b, 0x29, 0x25, 0x9a, 0x4f, 0x1c, 0x60, 0x0a, 0xd3 }; static const uint8_t in_u1[56] = { 0x06, 0xfc, 0xe6, 0x40, 0xfa, 0x34, 0x87, 0xbf, 0xda, 0x5f, 0x6c, 0xf2, 0xd5, 0x26, 0x3f, 0x8a, 0xad, 0x88, 0x33, 0x4c, 0xbd, 0x07, 0x43, 0x7f, 0x02, 0x0f, 0x08, 0xf9, 0x81, 0x4d, 0xc0, 0x31, 0xdd, 0xbd, 0xc3, 0x8c, 0x19, 0xc6, 0xda, 0x25, 0x83, 0xfa, 0x54, 0x29, 0xdb, 0x94, 0xad, 0xa1, 0x8a, 0xa7, 0xa7, 0xfb, 0x4e, 0xf8, 0xa0, 0x86 }; static const uint8_t out_u1[56] = { 0xce, 0x3e, 0x4f, 0xf9, 0x5a, 0x60, 0xdc, 0x66, 0x97, 0xda, 0x1d, 0xb1, 0xd8, 0x5e, 0x6a, 0xfb, 0xdf, 0x79, 0xb5, 0x0a, 0x24, 0x12, 0xd7, 0x54, 0x6d, 0x5f, 0x23, 0x9f, 0xe1, 0x4f, 0xba, 0xad, 0xeb, 0x44, 0x5f, 0xc6, 0x6a, 0x01, 0xb0, 0x77, 0x9d, 0x98, 0x22, 0x39, 0x61, 0x11, 0x1e, 0x21, 0x76, 0x62, 0x82, 0xf7, 0x3d, 0xd9, 0x6b, 0x6f }; static const uint8_t in_scalar2[56] = { 0x20, 0x3d, 0x49, 0x44, 0x28, 0xb8, 0x39, 0x93, 0x52, 0x66, 0x5d, 0xdc, 0xa4, 0x2f, 0x9d, 0xe8, 0xfe, 0xf6, 0x00, 0x90, 0x8e, 0x0d, 0x46, 0x1c, 0xb0, 0x21, 0xf8, 0xc5, 0x38, 0x34, 0x5d, 0xd7, 0x7c, 0x3e, 0x48, 0x06, 0xe2, 0x5f, 0x46, 0xd3, 0x31, 0x5c, 0x44, 0xe0, 0xa5, 0xb4, 0x37, 0x12, 0x82, 0xdd, 0x2c, 0x8d, 0x5b, 0xe3, 0x09, 0x5f }; static const uint8_t in_u2[56] = { 0x0f, 0xbc, 0xc2, 0xf9, 0x93, 0xcd, 0x56, 0xd3, 0x30, 0x5b, 0x0b, 0x7d, 0x9e, 0x55, 0xd4, 0xc1, 0xa8, 0xfb, 0x5d, 0xbb, 0x52, 0xf8, 0xe9, 0xa1, 0xe9, 0xb6, 0x20, 0x1b, 0x16, 0x5d, 0x01, 0x58, 0x94, 0xe5, 0x6c, 0x4d, 0x35, 0x70, 0xbe, 0xe5, 0x2f, 0xe2, 0x05, 0xe2, 0x8a, 0x78, 0xb9, 0x1c, 0xdf, 0xbd, 0xe7, 0x1c, 0xe8, 0xd1, 0x57, 0xdb }; static const uint8_t out_u2[56] = { 0x88, 0x4a, 0x02, 0x57, 0x62, 0x39, 0xff, 0x7a, 0x2f, 0x2f, 0x63, 0xb2, 0xdb, 0x6a, 0x9f, 0xf3, 0x70, 0x47, 0xac, 0x13, 0x56, 0x8e, 0x1e, 0x30, 0xfe, 0x63, 0xc4, 0xa7, 0xad, 0x1b, 0x3e, 0xe3, 0xa5, 0x70, 0x0d, 0xf3, 0x43, 0x21, 0xd6, 0x20, 0x77, 0xe6, 0x36, 0x33, 0xc5, 0x75, 0xc1, 0xc9, 0x54, 0x51, 0x4e, 0x99, 0xda, 0x7c, 0x17, 0x9d }; static const uint8_t in_u3[56] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const uint8_t out_u3[3][56] = { { 0x3f, 0x48, 0x2c, 0x8a, 0x9f, 0x19, 0xb0, 0x1e, 0x6c, 0x46, 0xee, 0x97, 0x11, 0xd9, 0xdc, 0x14, 0xfd, 0x4b, 0xf6, 0x7a, 0xf3, 0x07, 0x65, 0xc2, 0xae, 0x2b, 0x84, 0x6a, 0x4d, 0x23, 0xa8, 0xcd, 0x0d, 0xb8, 0x97, 0x08, 0x62, 0x39, 0x49, 0x2c, 0xaf, 0x35, 0x0b, 0x51, 0xf8, 0x33, 0x86, 0x8b, 0x9b, 0xc2, 0xb3, 0xbc, 0xa9, 0xcf, 0x41, 0x13 }, { 0xaa, 0x3b, 0x47, 0x49, 0xd5, 0x5b, 0x9d, 0xaf, 0x1e, 0x5b, 0x00, 0x28, 0x88, 0x26, 0xc4, 0x67, 0x27, 0x4c, 0xe3, 0xeb, 0xbd, 0xd5, 0xc1, 0x7b, 0x97, 0x5e, 0x09, 0xd4, 0xaf, 0x6c, 0x67, 0xcf, 0x10, 0xd0, 0x87, 0x20, 0x2d, 0xb8, 0x82, 0x86, 0xe2, 0xb7, 0x9f, 0xce, 0xea, 0x3e, 0xc3, 0x53, 0xef, 0x54, 0xfa, 0xa2, 0x6e, 0x21, 0x9f, 0x38 }, { 0x07, 0x7f, 0x45, 0x36, 0x81, 0xca, 0xca, 0x36, 0x93, 0x19, 0x84, 0x20, 0xbb, 0xe5, 0x15, 0xca, 0xe0, 0x00, 0x24, 0x72, 0x51, 0x9b, 0x3e, 0x67, 0x66, 0x1a, 0x7e, 0x89, 0xca, 0xb9, 0x46, 0x95, 0xc8, 0xf4, 0xbc, 0xd6, 0x6e, 0x61, 0xb9, 0xb9, 0xc9, 0x46, 0xda, 0x8d, 0x52, 0x4d, 0xe3, 0xd6, 0x9b, 0xd9, 0xd9, 0xd6, 0x6b, 0x99, 0x7e, 0x37 } }; static const uint8_t privkey1[57] = { 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b }; static const uint8_t pubkey1[57] = { 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80 }; static const uint8_t sig1[114] = { 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25, 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae, 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2, 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f, 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81, 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78, 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39, 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a, 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d, 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41, 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd, 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb, 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26, 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65, 0x26, 0x00 }; static const uint8_t privkey2[57] = { 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6, 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d, 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d, 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a, 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c, 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e, 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27, 0x4e }; static const uint8_t pubkey2[57] = { 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45, 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a, 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37, 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86, 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02, 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c, 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94, 0x80 }; static const uint8_t msg2[1] = { 0x03 }; static const uint8_t sig2[114] = { 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89, 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77, 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35, 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35, 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7, 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33, 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79, 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb, 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36, 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36, 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29, 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f, 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf, 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0, 0x3a, 0x00 }; static const uint8_t privkey3[57] = { 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6, 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d, 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d, 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a, 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c, 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e, 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27, 0x4e }; static const uint8_t pubkey3[57] = { 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45, 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a, 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37, 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86, 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02, 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c, 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94, 0x80 }; static const uint8_t msg3[1] = { 0x03 }; static const uint8_t context3[3] = { 0x66, 0x6f, 0x6f }; static const uint8_t sig3[114] = { 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46, 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55, 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab, 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2, 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2, 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21, 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea, 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda, 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d, 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24, 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f, 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d, 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98, 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c, 0x3c, 0x00 }; static const uint8_t privkey4[57] = { 0xcd, 0x23, 0xd2, 0x4f, 0x71, 0x42, 0x74, 0xe7, 0x44, 0x34, 0x32, 0x37, 0xb9, 0x32, 0x90, 0xf5, 0x11, 0xf6, 0x42, 0x5f, 0x98, 0xe6, 0x44, 0x59, 0xff, 0x20, 0x3e, 0x89, 0x85, 0x08, 0x3f, 0xfd, 0xf6, 0x05, 0x00, 0x55, 0x3a, 0xbc, 0x0e, 0x05, 0xcd, 0x02, 0x18, 0x4b, 0xdb, 0x89, 0xc4, 0xcc, 0xd6, 0x7e, 0x18, 0x79, 0x51, 0x26, 0x7e, 0xb3, 0x28 }; static const uint8_t pubkey4[57] = { 0xdc, 0xea, 0x9e, 0x78, 0xf3, 0x5a, 0x1b, 0xf3, 0x49, 0x9a, 0x83, 0x1b, 0x10, 0xb8, 0x6c, 0x90, 0xaa, 0xc0, 0x1c, 0xd8, 0x4b, 0x67, 0xa0, 0x10, 0x9b, 0x55, 0xa3, 0x6e, 0x93, 0x28, 0xb1, 0xe3, 0x65, 0xfc, 0xe1, 0x61, 0xd7, 0x1c, 0xe7, 0x13, 0x1a, 0x54, 0x3e, 0xa4, 0xcb, 0x5f, 0x7e, 0x9f, 0x1d, 0x8b, 0x00, 0x69, 0x64, 0x47, 0x00, 0x14, 0x00 }; static const uint8_t msg4[11] = { 0x0c, 0x3e, 0x54, 0x40, 0x74, 0xec, 0x63, 0xb0, 0x26, 0x5e, 0x0c }; static const uint8_t sig4[114] = { 0x1f, 0x0a, 0x88, 0x88, 0xce, 0x25, 0xe8, 0xd4, 0x58, 0xa2, 0x11, 0x30, 0x87, 0x9b, 0x84, 0x0a, 0x90, 0x89, 0xd9, 0x99, 0xaa, 0xba, 0x03, 0x9e, 0xaf, 0x3e, 0x3a, 0xfa, 0x09, 0x0a, 0x09, 0xd3, 0x89, 0xdb, 0xa8, 0x2c, 0x4f, 0xf2, 0xae, 0x8a, 0xc5, 0xcd, 0xfb, 0x7c, 0x55, 0xe9, 0x4d, 0x5d, 0x96, 0x1a, 0x29, 0xfe, 0x01, 0x09, 0x94, 0x1e, 0x00, 0xb8, 0xdb, 0xde, 0xea, 0x6d, 0x3b, 0x05, 0x10, 0x68, 0xdf, 0x72, 0x54, 0xc0, 0xcd, 0xc1, 0x29, 0xcb, 0xe6, 0x2d, 0xb2, 0xdc, 0x95, 0x7d, 0xbb, 0x47, 0xb5, 0x1f, 0xd3, 0xf2, 0x13, 0xfb, 0x86, 0x98, 0xf0, 0x64, 0x77, 0x42, 0x50, 0xa5, 0x02, 0x89, 0x61, 0xc9, 0xbf, 0x8f, 0xfd, 0x97, 0x3f, 0xe5, 0xd5, 0xc2, 0x06, 0x49, 0x2b, 0x14, 0x0e, 0x00 }; static const uint8_t privkey5[57] = { 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c, 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82, 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8, 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93, 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33, 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc, 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2, 0x1b }; static const uint8_t pubkey5[57] = { 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f, 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79, 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63, 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc, 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f, 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b, 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5, 0x80 }; static const uint8_t msg5[12] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd, 0x66, 0x81, 0x1e, 0x29, 0x15 }; static const uint8_t sig5[114] = { 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79, 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6, 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89, 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a, 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4, 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07, 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf, 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e, 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e, 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7, 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef, 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61, 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b, 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff, 0x3c, 0x00 }; static const uint8_t privkey6[57] = { 0x7e, 0xf4, 0xe8, 0x45, 0x44, 0x23, 0x67, 0x52, 0xfb, 0xb5, 0x6b, 0x8f, 0x31, 0xa2, 0x3a, 0x10, 0xe4, 0x28, 0x14, 0xf5, 0xf5, 0x5c, 0xa0, 0x37, 0xcd, 0xcc, 0x11, 0xc6, 0x4c, 0x9a, 0x3b, 0x29, 0x49, 0xc1, 0xbb, 0x60, 0x70, 0x03, 0x14, 0x61, 0x17, 0x32, 0xa6, 0xc2, 0xfe, 0xa9, 0x8e, 0xeb, 0xc0, 0x26, 0x6a, 0x11, 0xa9, 0x39, 0x70, 0x10, 0x0e }; static const uint8_t pubkey6[57] = { 0xb3, 0xda, 0x07, 0x9b, 0x0a, 0xa4, 0x93, 0xa5, 0x77, 0x20, 0x29, 0xf0, 0x46, 0x7b, 0xae, 0xbe, 0xe5, 0xa8, 0x11, 0x2d, 0x9d, 0x3a, 0x22, 0x53, 0x23, 0x61, 0xda, 0x29, 0x4f, 0x7b, 0xb3, 0x81, 0x5c, 0x5d, 0xc5, 0x9e, 0x17, 0x6b, 0x4d, 0x9f, 0x38, 0x1c, 0xa0, 0x93, 0x8e, 0x13, 0xc6, 0xc0, 0x7b, 0x17, 0x4b, 0xe6, 0x5d, 0xfa, 0x57, 0x8e, 0x80 }; static const uint8_t msg6[13] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd, 0x66, 0x81, 0x1e, 0x29, 0x15, 0xe7 }; static const uint8_t sig6[114] = { 0x6a, 0x12, 0x06, 0x6f, 0x55, 0x33, 0x1b, 0x6c, 0x22, 0xac, 0xd5, 0xd5, 0xbf, 0xc5, 0xd7, 0x12, 0x28, 0xfb, 0xda, 0x80, 0xae, 0x8d, 0xec, 0x26, 0xbd, 0xd3, 0x06, 0x74, 0x3c, 0x50, 0x27, 0xcb, 0x48, 0x90, 0x81, 0x0c, 0x16, 0x2c, 0x02, 0x74, 0x68, 0x67, 0x5e, 0xcf, 0x64, 0x5a, 0x83, 0x17, 0x6c, 0x0d, 0x73, 0x23, 0xa2, 0xcc, 0xde, 0x2d, 0x80, 0xef, 0xe5, 0xa1, 0x26, 0x8e, 0x8a, 0xca, 0x1d, 0x6f, 0xbc, 0x19, 0x4d, 0x3f, 0x77, 0xc4, 0x49, 0x86, 0xeb, 0x4a, 0xb4, 0x17, 0x79, 0x19, 0xad, 0x8b, 0xec, 0x33, 0xeb, 0x47, 0xbb, 0xb5, 0xfc, 0x6e, 0x28, 0x19, 0x6f, 0xd1, 0xca, 0xf5, 0x6b, 0x4e, 0x7e, 0x0b, 0xa5, 0x51, 0x92, 0x34, 0xd0, 0x47, 0x15, 0x5a, 0xc7, 0x27, 0xa1, 0x05, 0x31, 0x00 }; static const uint8_t privkey7[57] = { 0xd6, 0x5d, 0xf3, 0x41, 0xad, 0x13, 0xe0, 0x08, 0x56, 0x76, 0x88, 0xba, 0xed, 0xda, 0x8e, 0x9d, 0xcd, 0xc1, 0x7d, 0xc0, 0x24, 0x97, 0x4e, 0xa5, 0xb4, 0x22, 0x7b, 0x65, 0x30, 0xe3, 0x39, 0xbf, 0xf2, 0x1f, 0x99, 0xe6, 0x8c, 0xa6, 0x96, 0x8f, 0x3c, 0xca, 0x6d, 0xfe, 0x0f, 0xb9, 0xf4, 0xfa, 0xb4, 0xfa, 0x13, 0x5d, 0x55, 0x42, 0xea, 0x3f, 0x01 }; static const uint8_t pubkey7[57] = { 0xdf, 0x97, 0x05, 0xf5, 0x8e, 0xdb, 0xab, 0x80, 0x2c, 0x7f, 0x83, 0x63, 0xcf, 0xe5, 0x56, 0x0a, 0xb1, 0xc6, 0x13, 0x2c, 0x20, 0xa9, 0xf1, 0xdd, 0x16, 0x34, 0x83, 0xa2, 0x6f, 0x8a, 0xc5, 0x3a, 0x39, 0xd6, 0x80, 0x8b, 0xf4, 0xa1, 0xdf, 0xbd, 0x26, 0x1b, 0x09, 0x9b, 0xb0, 0x3b, 0x3f, 0xb5, 0x09, 0x06, 0xcb, 0x28, 0xbd, 0x8a, 0x08, 0x1f, 0x00 }; static const uint8_t msg7[64] = { 0xbd, 0x0f, 0x6a, 0x37, 0x47, 0xcd, 0x56, 0x1b, 0xdd, 0xdf, 0x46, 0x40, 0xa3, 0x32, 0x46, 0x1a, 0x4a, 0x30, 0xa1, 0x2a, 0x43, 0x4c, 0xd0, 0xbf, 0x40, 0xd7, 0x66, 0xd9, 0xc6, 0xd4, 0x58, 0xe5, 0x51, 0x22, 0x04, 0xa3, 0x0c, 0x17, 0xd1, 0xf5, 0x0b, 0x50, 0x79, 0x63, 0x1f, 0x64, 0xeb, 0x31, 0x12, 0x18, 0x2d, 0xa3, 0x00, 0x58, 0x35, 0x46, 0x11, 0x13, 0x71, 0x8d, 0x1a, 0x5e, 0xf9, 0x44 }; static const uint8_t sig7[114] = { 0x55, 0x4b, 0xc2, 0x48, 0x08, 0x60, 0xb4, 0x9e, 0xab, 0x85, 0x32, 0xd2, 0xa5, 0x33, 0xb7, 0xd5, 0x78, 0xef, 0x47, 0x3e, 0xeb, 0x58, 0xc9, 0x8b, 0xb2, 0xd0, 0xe1, 0xce, 0x48, 0x8a, 0x98, 0xb1, 0x8d, 0xfd, 0xe9, 0xb9, 0xb9, 0x07, 0x75, 0xe6, 0x7f, 0x47, 0xd4, 0xa1, 0xc3, 0x48, 0x20, 0x58, 0xef, 0xc9, 0xf4, 0x0d, 0x2c, 0xa0, 0x33, 0xa0, 0x80, 0x1b, 0x63, 0xd4, 0x5b, 0x3b, 0x72, 0x2e, 0xf5, 0x52, 0xba, 0xd3, 0xb4, 0xcc, 0xb6, 0x67, 0xda, 0x35, 0x01, 0x92, 0xb6, 0x1c, 0x50, 0x8c, 0xf7, 0xb6, 0xb5, 0xad, 0xad, 0xc2, 0xc8, 0xd9, 0xa4, 0x46, 0xef, 0x00, 0x3f, 0xb0, 0x5c, 0xba, 0x5f, 0x30, 0xe8, 0x8e, 0x36, 0xec, 0x27, 0x03, 0xb3, 0x49, 0xca, 0x22, 0x9c, 0x26, 0x70, 0x83, 0x39, 0x00 }; static const uint8_t privkey8[57] = { 0x2e, 0xc5, 0xfe, 0x3c, 0x17, 0x04, 0x5a, 0xbd, 0xb1, 0x36, 0xa5, 0xe6, 0xa9, 0x13, 0xe3, 0x2a, 0xb7, 0x5a, 0xe6, 0x8b, 0x53, 0xd2, 0xfc, 0x14, 0x9b, 0x77, 0xe5, 0x04, 0x13, 0x2d, 0x37, 0x56, 0x9b, 0x7e, 0x76, 0x6b, 0xa7, 0x4a, 0x19, 0xbd, 0x61, 0x62, 0x34, 0x3a, 0x21, 0xc8, 0x59, 0x0a, 0xa9, 0xce, 0xbc, 0xa9, 0x01, 0x4c, 0x63, 0x6d, 0xf5 }; static const uint8_t pubkey8[57] = { 0x79, 0x75, 0x6f, 0x01, 0x4d, 0xcf, 0xe2, 0x07, 0x9f, 0x5d, 0xd9, 0xe7, 0x18, 0xbe, 0x41, 0x71, 0xe2, 0xef, 0x24, 0x86, 0xa0, 0x8f, 0x25, 0x18, 0x6f, 0x6b, 0xff, 0x43, 0xa9, 0x93, 0x6b, 0x9b, 0xfe, 0x12, 0x40, 0x2b, 0x08, 0xae, 0x65, 0x79, 0x8a, 0x3d, 0x81, 0xe2, 0x2e, 0x9e, 0xc8, 0x0e, 0x76, 0x90, 0x86, 0x2e, 0xf3, 0xd4, 0xed, 0x3a, 0x00 }; static const uint8_t msg8[256] = { 0x15, 0x77, 0x75, 0x32, 0xb0, 0xbd, 0xd0, 0xd1, 0x38, 0x9f, 0x63, 0x6c, 0x5f, 0x6b, 0x9b, 0xa7, 0x34, 0xc9, 0x0a, 0xf5, 0x72, 0x87, 0x7e, 0x2d, 0x27, 0x2d, 0xd0, 0x78, 0xaa, 0x1e, 0x56, 0x7c, 0xfa, 0x80, 0xe1, 0x29, 0x28, 0xbb, 0x54, 0x23, 0x30, 0xe8, 0x40, 0x9f, 0x31, 0x74, 0x50, 0x41, 0x07, 0xec, 0xd5, 0xef, 0xac, 0x61, 0xae, 0x75, 0x04, 0xda, 0xbe, 0x2a, 0x60, 0x2e, 0xde, 0x89, 0xe5, 0xcc, 0xa6, 0x25, 0x7a, 0x7c, 0x77, 0xe2, 0x7a, 0x70, 0x2b, 0x3a, 0xe3, 0x9f, 0xc7, 0x69, 0xfc, 0x54, 0xf2, 0x39, 0x5a, 0xe6, 0xa1, 0x17, 0x8c, 0xab, 0x47, 0x38, 0xe5, 0x43, 0x07, 0x2f, 0xc1, 0xc1, 0x77, 0xfe, 0x71, 0xe9, 0x2e, 0x25, 0xbf, 0x03, 0xe4, 0xec, 0xb7, 0x2f, 0x47, 0xb6, 0x4d, 0x04, 0x65, 0xaa, 0xea, 0x4c, 0x7f, 0xad, 0x37, 0x25, 0x36, 0xc8, 0xba, 0x51, 0x6a, 0x60, 0x39, 0xc3, 0xc2, 0xa3, 0x9f, 0x0e, 0x4d, 0x83, 0x2b, 0xe4, 0x32, 0xdf, 0xa9, 0xa7, 0x06, 0xa6, 0xe5, 0xc7, 0xe1, 0x9f, 0x39, 0x79, 0x64, 0xca, 0x42, 0x58, 0x00, 0x2f, 0x7c, 0x05, 0x41, 0xb5, 0x90, 0x31, 0x6d, 0xbc, 0x56, 0x22, 0xb6, 0xb2, 0xa6, 0xfe, 0x7a, 0x4a, 0xbf, 0xfd, 0x96, 0x10, 0x5e, 0xca, 0x76, 0xea, 0x7b, 0x98, 0x81, 0x6a, 0xf0, 0x74, 0x8c, 0x10, 0xdf, 0x04, 0x8c, 0xe0, 0x12, 0xd9, 0x01, 0x01, 0x5a, 0x51, 0xf1, 0x89, 0xf3, 0x88, 0x81, 0x45, 0xc0, 0x36, 0x50, 0xaa, 0x23, 0xce, 0x89, 0x4c, 0x3b, 0xd8, 0x89, 0xe0, 0x30, 0xd5, 0x65, 0x07, 0x1c, 0x59, 0xf4, 0x09, 0xa9, 0x98, 0x1b, 0x51, 0x87, 0x8f, 0xd6, 0xfc, 0x11, 0x06, 0x24, 0xdc, 0xbc, 0xde, 0x0b, 0xf7, 0xa6, 0x9c, 0xcc, 0xe3, 0x8f, 0xab, 0xdf, 0x86, 0xf3, 0xbe, 0xf6, 0x04, 0x48, 0x19, 0xde, 0x11 }; static const uint8_t sig8[114] = { 0xc6, 0x50, 0xdd, 0xbb, 0x06, 0x01, 0xc1, 0x9c, 0xa1, 0x14, 0x39, 0xe1, 0x64, 0x0d, 0xd9, 0x31, 0xf4, 0x3c, 0x51, 0x8e, 0xa5, 0xbe, 0xa7, 0x0d, 0x3d, 0xcd, 0xe5, 0xf4, 0x19, 0x1f, 0xe5, 0x3f, 0x00, 0xcf, 0x96, 0x65, 0x46, 0xb7, 0x2b, 0xcc, 0x7d, 0x58, 0xbe, 0x2b, 0x9b, 0xad, 0xef, 0x28, 0x74, 0x39, 0x54, 0xe3, 0xa4, 0x4a, 0x23, 0xf8, 0x80, 0xe8, 0xd4, 0xf1, 0xcf, 0xce, 0x2d, 0x7a, 0x61, 0x45, 0x2d, 0x26, 0xda, 0x05, 0x89, 0x6f, 0x0a, 0x50, 0xda, 0x66, 0xa2, 0x39, 0xa8, 0xa1, 0x88, 0xb6, 0xd8, 0x25, 0xb3, 0x30, 0x5a, 0xd7, 0x7b, 0x73, 0xfb, 0xac, 0x08, 0x36, 0xec, 0xc6, 0x09, 0x87, 0xfd, 0x08, 0x52, 0x7c, 0x1a, 0x8e, 0x80, 0xd5, 0x82, 0x3e, 0x65, 0xca, 0xfe, 0x2a, 0x3d, 0x00 }; static const uint8_t privkey9[57] = { 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73, 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8, 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83, 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec, 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea, 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19, 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c, 0xe8 }; static const uint8_t pubkey9[57] = { 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94, 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb, 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14, 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d, 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c, 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd, 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4, 0x00 }; static const uint8_t msg9[1023] = { 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86, 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35, 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c, 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12, 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51, 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50, 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4, 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd, 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6, 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa, 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36, 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc, 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc, 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76, 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf, 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9, 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69, 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e, 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23, 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9, 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47, 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60, 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6, 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21, 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad, 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77, 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17, 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17, 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78, 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e, 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3, 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13, 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93, 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25, 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5, 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0, 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb, 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b, 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed, 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20, 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97, 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73, 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22, 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1, 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5, 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9, 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c, 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98, 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25, 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1, 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6, 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee, 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13, 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7, 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d, 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6, 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f, 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42, 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8, 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4, 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6, 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68, 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00, 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9, 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c, 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40, 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6, 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41, 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c, 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84, 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2, 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56, 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63, 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa, 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99, 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab, 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c, 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2, 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42, 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3, 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17, 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26, 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20, 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35, 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e, 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72, 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a, 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14, 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53, 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc, 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c, 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67, 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01, 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa, 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65, 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05, 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde, 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37, 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07, 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63, 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd, 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64, 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a, 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d, 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6, 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c, 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2, 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c, 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7, 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56, 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1, 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e, 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9, 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15, 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd, 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd, 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d, 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b, 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56, 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4, 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b, 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69, 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4, 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95, 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff, 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e, 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1, 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87 }; static const uint8_t sig9[114] = { 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d, 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75, 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2, 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd, 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63, 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a, 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97, 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d, 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c, 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29, 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d, 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57, 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17, 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e, 0x1a, 0x00 }; static const uint8_t phprivkey1[57] = { 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3, 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49 }; static const uint8_t phpubkey1[57] = { 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde, 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76, 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80 }; static const uint8_t phmsg1[3] = { 0x61, 0x62, 0x63 }; static const uint8_t phsig1[114] = { 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d, 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69, 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56, 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38, 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15, 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd, 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b, 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd, 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94, 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5, 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94, 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3, 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60, 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9, 0x0f, 0x00 }; static const uint8_t phprivkey2[57] = { 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3, 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49 }; static const uint8_t phpubkey2[57] = { 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde, 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76, 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80 }; static const uint8_t phmsg2[3] = { 0x61, 0x62, 0x63 }; static const uint8_t phcontext2[3] = { 0x66, 0x6f, 0x6f }; static const uint8_t phsig2[114] = { 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02, 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9, 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49, 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48, 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92, 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda, 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2, 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3, 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23, 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30, 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb, 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28, 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a, 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13, 0x21, 0x00 }; static const uint8_t *dohash(EVP_MD_CTX *hashctx, const uint8_t *msg, size_t msglen) { static uint8_t hashout[64]; if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL) || !EVP_DigestUpdate(hashctx, msg, msglen) || !EVP_DigestFinalXOF(hashctx, hashout, sizeof(hashout))) return NULL; return hashout; } static int test_ed448(void) { uint8_t outsig[114]; EVP_MD_CTX *hashctx = EVP_MD_CTX_new(); if (!TEST_ptr(hashctx) || !TEST_true(ossl_ed448_sign(NULL, outsig, NULL, 0, pubkey1, privkey1, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig1, outsig, sizeof(sig1)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg2, sizeof(msg2), pubkey2, privkey2, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig2, outsig, sizeof(sig2)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg3, sizeof(msg3), pubkey3, privkey3, context3, sizeof(context3), 0, NULL)) || !TEST_int_eq(memcmp(sig3, outsig, sizeof(sig3)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg4, sizeof(msg4), pubkey4, privkey4, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig4, outsig, sizeof(sig4)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg5, sizeof(msg5), pubkey5, privkey5, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig5, outsig, sizeof(sig5)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg6, sizeof(msg6), pubkey6, privkey6, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig6, outsig, sizeof(sig6)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg7, sizeof(msg7), pubkey7, privkey7, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig7, outsig, sizeof(sig7)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg8, sizeof(msg8), pubkey8, privkey8, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig8, outsig, sizeof(sig8)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, msg9, sizeof(msg9), pubkey9, privkey9, NULL, 0, 0, NULL)) || !TEST_int_eq(memcmp(sig9, outsig, sizeof(sig9)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, dohash(hashctx, phmsg1, sizeof(phmsg1)), 64, phpubkey1, phprivkey1, NULL, 0, 1, NULL)) || !TEST_int_eq(memcmp(phsig1, outsig, sizeof(phsig1)), 0) || !TEST_true(ossl_ed448_sign(NULL, outsig, dohash(hashctx, phmsg2, sizeof(phmsg2)), 64, phpubkey2, phprivkey2, phcontext2, sizeof(phcontext2), 1, NULL)) || !TEST_int_eq(memcmp(phsig2, outsig, sizeof(phsig2)), 0)) { EVP_MD_CTX_free(hashctx); return 0; } EVP_MD_CTX_free(hashctx); return 1; } static int test_x448(void) { uint8_t u[56], k[56], out[56]; unsigned int i; int j = -1; if (!TEST_true(ossl_x448(out, in_scalar1, in_u1)) || !TEST_int_eq(memcmp(out, out_u1, sizeof(out)), 0) || !TEST_true(ossl_x448(out, in_scalar2, in_u2)) || !TEST_int_eq(memcmp(out, out_u2, sizeof(out)), 0)) return 0; memcpy(u, in_u3, sizeof(u)); memcpy(k, in_u3, sizeof(k)); for (i = 1; i <= max; i++) { if (verbose && i % 10000 == 0) { printf("."); fflush(stdout); } if (!TEST_true(ossl_x448(out, k, u))) return 0; if (i == 1 || i == 1000 || i == 1000000) { j++; if (!TEST_int_eq(memcmp(out, out_u3[j], sizeof(out)), 0)) { TEST_info("Failed at iteration %d", i); return 0; } } memcpy(u, k, sizeof(u)); memcpy(k, out, sizeof(k)); } return 1; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_PROGRESS, OPT_SLOW, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"), { "f", OPT_SLOW, '-', "Enables a slow test" }, { "v", OPT_PROGRESS, '-', "Enables verbose mode (prints progress dots)" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_TEST_CASES: break; default: return 0; case OPT_SLOW: max = 1000000; break; case OPT_PROGRESS: verbose = 1; break; } } ADD_TEST(test_x448); ADD_TEST(test_ed448); return 1; }
test
openssl/test/curve448_internal_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/evp.h> #include <openssl/kdf.h> #include <openssl/core_names.h> #include "internal/numbers.h" #include "testutil.h" static EVP_KDF_CTX *get_kdfbyname_libctx(OSSL_LIB_CTX *libctx, const char *name) { EVP_KDF *kdf = EVP_KDF_fetch(libctx, name, NULL); EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); EVP_KDF_free(kdf); return kctx; } static EVP_KDF_CTX *get_kdfbyname(const char *name) { return get_kdfbyname_libctx(NULL, name); } static OSSL_PARAM *construct_tls1_prf_params(const char *digest, const char *secret, const char *seed) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 4); OSSL_PARAM *p = params; if (params == NULL) return NULL; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)digest, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, (unsigned char *)secret, strlen(secret)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED, (unsigned char *)seed, strlen(seed)); *p = OSSL_PARAM_construct_end(); return params; } static int test_kdf_tls1_prf(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; static const unsigned char expected[sizeof(out)] = { 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0, 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc }; params = construct_tls1_prf_params("sha256", "secret", "seed"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_invalid_digest(void) { int ret; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM *params; params = construct_tls1_prf_params("blah", "secret", "seed"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_false(EVP_KDF_CTX_set_params(kctx, params)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_zero_output_size(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; params = construct_tls1_prf_params("sha256", "secret", "seed"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_empty_secret(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; params = construct_tls1_prf_params("sha256", "", "seed"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_1byte_secret(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; params = construct_tls1_prf_params("sha256", "1", "seed"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_empty_seed(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; params = construct_tls1_prf_params("sha256", "secret", ""); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_tls1_prf_1byte_seed(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[16]; OSSL_PARAM *params; params = construct_tls1_prf_params("sha256", "secret", "1"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static OSSL_PARAM *construct_hkdf_params(char *digest, char *key, size_t keylen, char *salt, char *info) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5); OSSL_PARAM *p = params; if (params == NULL) return NULL; if (digest != NULL) *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, strlen(salt)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (unsigned char *)key, keylen); if (info != NULL) *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, info, strlen(info)); else *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, "EXTRACT_ONLY", 0); *p = OSSL_PARAM_construct_end(); return params; } static int test_kdf_hkdf(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[10]; OSSL_PARAM *params; static const unsigned char expected[sizeof(out)] = { 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13 }; params = construct_hkdf_params("sha256", "secret", 6, "salt", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int do_kdf_hkdf_gettables(int expand_only, int has_digest) { int ret = 0; size_t sz = 0; OSSL_PARAM *params; OSSL_PARAM params_get[2]; const OSSL_PARAM *gettables, *p; EVP_KDF_CTX *kctx = NULL; if (!TEST_ptr(params = construct_hkdf_params( has_digest ? "sha256" : NULL, "secret", 6, "salt", expand_only ? NULL : "label")) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))) goto err; if (!TEST_ptr(gettables = EVP_KDF_CTX_gettable_params(kctx)) || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_SIZE))) goto err; params_get[0] = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &sz); params_get[1] = OSSL_PARAM_construct_end(); if (has_digest) { if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1) || !TEST_size_t_eq(sz, expand_only ? SHA256_DIGEST_LENGTH : SIZE_MAX)) goto err; } else { if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 0)) goto err; } params_get[0] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), -2)) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_hkdf_gettables(void) { return do_kdf_hkdf_gettables(0, 1); } static int test_kdf_hkdf_gettables_expandonly(void) { return do_kdf_hkdf_gettables(1, 1); } static int test_kdf_hkdf_gettables_no_digest(void) { return do_kdf_hkdf_gettables(1, 0); } static int test_kdf_hkdf_invalid_digest(void) { int ret; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM *params; params = construct_hkdf_params("blah", "secret", 6, "salt", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_false(EVP_KDF_CTX_set_params(kctx, params)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_hkdf_derive_set_params_fail(void) { int ret = 0, i = 0; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM params[2]; unsigned char out[10]; if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))) goto end; params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_DIGEST, &i); params[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)) goto end; ret = 1; end: EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_hkdf_set_invalid_mode(void) { int ret = 0, bad_mode = 100; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM params[2]; if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))) goto end; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, "BADMODE", 0); params[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0)) goto end; params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &bad_mode); if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0)) goto end; ret = 1; end: EVP_KDF_CTX_free(kctx); return ret; } static int do_kdf_hkdf_set_invalid_param(const char *key, int type) { int ret = 0; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM params[2]; unsigned char buf[2]; if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))) goto end; if (type == OSSL_PARAM_UTF8_STRING) params[0] = OSSL_PARAM_construct_utf8_string(key, "BAD", 0); else params[0] = OSSL_PARAM_construct_octet_string(key, buf, sizeof(buf)); params[1] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0)) goto end; ret = 1; end: EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_hkdf_set_ctx_param_fail(void) { return do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_MODE, OSSL_PARAM_OCTET_STRING) && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_KEY, OSSL_PARAM_UTF8_STRING) && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_SALT, OSSL_PARAM_UTF8_STRING) && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_INFO, OSSL_PARAM_UTF8_STRING); } static int test_kdf_hkdf_zero_output_size(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[10]; OSSL_PARAM *params; params = construct_hkdf_params("sha256", "secret", 6, "salt", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_hkdf_empty_key(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[10]; OSSL_PARAM *params; params = construct_hkdf_params("sha256", "", 0, "salt", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_hkdf_1byte_key(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[10]; OSSL_PARAM *params; params = construct_hkdf_params("sha256", "1", 1, "salt", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_hkdf_empty_salt(void) { int ret; EVP_KDF_CTX *kctx = NULL; unsigned char out[10]; OSSL_PARAM *params; params = construct_hkdf_params("sha256", "secret", 6, "", "label"); ret = TEST_ptr(params) && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static OSSL_PARAM *construct_pbkdf1_params(char *pass, char *digest, char *salt, unsigned int *iter) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5); OSSL_PARAM *p = params; if (params == NULL) return NULL; *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (unsigned char *)pass, strlen(pass)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (unsigned char *)salt, strlen(salt)); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0); *p = OSSL_PARAM_construct_end(); return params; } static int test_kdf_pbkdf1(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; unsigned int iterations = 4096; OSSL_LIB_CTX *libctx = NULL; OSSL_PARAM *params = NULL; OSSL_PROVIDER *legacyprov = NULL; OSSL_PROVIDER *defprov = NULL; const unsigned char expected[sizeof(out)] = { 0xfb, 0x83, 0x4d, 0x36, 0x6d, 0xbc, 0x53, 0x87, 0x35, 0x1b, 0x34, 0x75, 0x95, 0x88, 0x32, 0x4f, 0x3e, 0x82, 0x81, 0x01, 0x21, 0x93, 0x64, 0x00, 0xcc }; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())) goto err; legacyprov = OSSL_PROVIDER_load(libctx, "legacy"); if (legacyprov == NULL) { OSSL_LIB_CTX_free(libctx); return TEST_skip("PBKDF1 only available in legacy provider"); } if (!TEST_ptr(defprov = OSSL_PROVIDER_load(libctx, "default"))) goto err; params = construct_pbkdf1_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname_libctx(libctx, OSSL_KDF_NAME_PBKDF1)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0) || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); OSSL_PROVIDER_unload(defprov); OSSL_PROVIDER_unload(legacyprov); OSSL_LIB_CTX_free(libctx); return ret; } static int test_kdf_pbkdf1_key_too_long(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[EVP_MAX_MD_SIZE + 1]; unsigned int iterations = 4096; OSSL_LIB_CTX *libctx = NULL; OSSL_PARAM *params = NULL; OSSL_PROVIDER *legacyprov = NULL; OSSL_PROVIDER *defprov = NULL; if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())) goto err; legacyprov = OSSL_PROVIDER_load(libctx, "legacy"); if (legacyprov == NULL) { OSSL_LIB_CTX_free(libctx); return TEST_skip("PBKDF1 only available in legacy provider"); } if (!TEST_ptr(defprov = OSSL_PROVIDER_load(libctx, "default"))) goto err; params = construct_pbkdf1_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname_libctx(libctx, OSSL_KDF_NAME_PBKDF1)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); OSSL_PROVIDER_unload(defprov); OSSL_PROVIDER_unload(legacyprov); OSSL_LIB_CTX_free(libctx); return ret; } static OSSL_PARAM *construct_pbkdf2_params(char *pass, char *digest, char *salt, unsigned int *iter, int *mode) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 6); OSSL_PARAM *p = params; if (params == NULL) return NULL; *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (unsigned char *)pass, strlen(pass)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (unsigned char *)salt, strlen(salt)); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0); *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, mode); *p = OSSL_PARAM_construct_end(); return params; } static int test_kdf_pbkdf2(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; unsigned int iterations = 4096; int mode = 0; OSSL_PARAM *params; const unsigned char expected[sizeof(out)] = { 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, 0x1c }; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_small_output(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; unsigned int iterations = 4096; int mode = 0; OSSL_PARAM *params; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1, NULL), 0)) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_large_output(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; size_t len = 0; unsigned int iterations = 4096; int mode = 0; OSSL_PARAM *params; if (sizeof(len) > 32) len = SIZE_MAX; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || (len != 0 && !TEST_int_eq(EVP_KDF_derive(kctx, out, len, NULL), 0))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_small_salt(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned int iterations = 4096; int mode = 0; OSSL_PARAM *params; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALT", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_false(EVP_KDF_CTX_set_params(kctx, params))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_small_iterations(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned int iterations = 1; int mode = 0; OSSL_PARAM *params; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_false(EVP_KDF_CTX_set_params(kctx, params))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_small_salt_pkcs5(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; unsigned int iterations = 4096; int mode = 1; OSSL_PARAM *params; OSSL_PARAM mode_params[2]; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALT", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; mode = 0; mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode); mode_params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params)) || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_small_iterations_pkcs5(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned char out[25]; unsigned int iterations = 1; int mode = 1; OSSL_PARAM *params; OSSL_PARAM mode_params[2]; params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)) || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; mode = 0; mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode); mode_params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params)) || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_pbkdf2_invalid_digest(void) { int ret = 0; EVP_KDF_CTX *kctx = NULL; unsigned int iterations = 4096; int mode = 0; OSSL_PARAM *params; params = construct_pbkdf2_params("passwordPASSWORDpassword", "blah", "saltSALTsaltSALTsaltSALTsaltSALTsalt", &iterations, &mode); if (!TEST_ptr(params) || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2)) || !TEST_false(EVP_KDF_CTX_set_params(kctx, params))) goto err; ret = 1; err: EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } #ifndef OPENSSL_NO_SCRYPT static int test_kdf_scrypt(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[7], *p = params; unsigned char out[64]; unsigned int nu = 1024, ru = 8, pu = 16, maxmem = 16; static 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 }; *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (char *)"password", 8); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (char *)"NaCl", 4); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &nu); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &ru); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &pu); *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SCRYPT)) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_true(OSSL_PARAM_set_uint(p - 1, 10 * 1024 * 1024)) && TEST_true(EVP_KDF_CTX_set_params(kctx, p - 1)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } #endif static int test_kdf_ss_hash(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[4], *p = params; unsigned char out[14]; static unsigned char z[] = { 0x6d,0xbd,0xc2,0x3f,0x04,0x54,0x88,0xe4,0x06,0x27,0x57,0xb0,0x6b,0x9e, 0xba,0xe1,0x83,0xfc,0x5a,0x59,0x46,0xd8,0x0d,0xb9,0x3f,0xec,0x6f,0x62, 0xec,0x07,0xe3,0x72,0x7f,0x01,0x26,0xae,0xd1,0x2c,0xe4,0xb2,0x62,0xf4, 0x7d,0x48,0xd5,0x42,0x87,0xf8,0x1d,0x47,0x4c,0x7c,0x3b,0x18,0x50,0xe9 }; static unsigned char other[] = { 0xa1,0xb2,0xc3,0xd4,0xe5,0x43,0x41,0x56,0x53,0x69,0x64,0x3c,0x83,0x2e, 0x98,0x49,0xdc,0xdb,0xa7,0x1e,0x9a,0x31,0x39,0xe6,0x06,0xe0,0x95,0xde, 0x3c,0x26,0x4a,0x66,0xe9,0x8a,0x16,0x58,0x54,0xcd,0x07,0x98,0x9b,0x1e, 0xe0,0xec,0x3f,0x8d,0xbe }; static const unsigned char expected[sizeof(out)] = { 0xa4,0x62,0xde,0x16,0xa8,0x9d,0xe8,0x46,0x6e,0xf5,0x46,0x0b,0x47,0xb8 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"sha224", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other, sizeof(other)); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_x963(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[4], *p = params; unsigned char out[1024 / 8]; static unsigned char z[] = { 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89, 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f, 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9, 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8, 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd, 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f, 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47, 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0, 0x5a, 0x2d }; static unsigned char shared[] = { 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d, 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31 }; static const unsigned char expected[sizeof(out)] = { 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76, 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85, 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8, 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d, 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3, 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e, 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50, 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26, 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02, 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d, 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2, 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd, 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc, 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17, 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc, 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"sha512", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, shared, sizeof(shared)); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X963KDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA) static int test_kdf_kbkdf_6803_128(void) { int ret = 0, i, p; EVP_KDF_CTX *kctx; OSSL_PARAM params[7]; static unsigned char input_key[] = { 0x57, 0xD0, 0x29, 0x72, 0x98, 0xFF, 0xD9, 0xD3, 0x5D, 0xE5, 0xA4, 0x7F, 0xB4, 0xBD, 0xE2, 0x4B, }; static unsigned char constants[][5] = { { 0x00, 0x00, 0x00, 0x02, 0x99 }, { 0x00, 0x00, 0x00, 0x02, 0xaa }, { 0x00, 0x00, 0x00, 0x02, 0x55 }, }; static unsigned char outputs[][16] = { {0xD1, 0x55, 0x77, 0x5A, 0x20, 0x9D, 0x05, 0xF0, 0x2B, 0x38, 0xD4, 0x2A, 0x38, 0x9E, 0x5A, 0x56}, {0x64, 0xDF, 0x83, 0xF8, 0x5A, 0x53, 0x2F, 0x17, 0x57, 0x7D, 0x8C, 0x37, 0x03, 0x57, 0x96, 0xAB}, {0x3E, 0x4F, 0xBD, 0xF3, 0x0F, 0xB8, 0x25, 0x9C, 0x42, 0x5C, 0xB6, 0xC9, 0x6F, 0x1F, 0x46, 0x35} }; static unsigned char iv[16] = { 0 }; unsigned char result[16] = { 0 }; for (i = 0; i < 3; i++) { p = 0; params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_CIPHER, "CAMELLIA-128-CBC", 0); params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MAC, "CMAC", 0); params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MODE, "FEEDBACK", 0); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i])); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SEED, iv, sizeof(iv)); params[p] = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), outputs[i], sizeof(outputs[i])); EVP_KDF_CTX_free(kctx); if (ret != 1) return ret; } return ret; } static int test_kdf_kbkdf_6803_256(void) { int ret = 0, i, p; EVP_KDF_CTX *kctx; OSSL_PARAM params[7]; static unsigned char input_key[] = { 0xB9, 0xD6, 0x82, 0x8B, 0x20, 0x56, 0xB7, 0xBE, 0x65, 0x6D, 0x88, 0xA1, 0x23, 0xB1, 0xFA, 0xC6, 0x82, 0x14, 0xAC, 0x2B, 0x72, 0x7E, 0xCF, 0x5F, 0x69, 0xAF, 0xE0, 0xC4, 0xDF, 0x2A, 0x6D, 0x2C, }; static unsigned char constants[][5] = { { 0x00, 0x00, 0x00, 0x02, 0x99 }, { 0x00, 0x00, 0x00, 0x02, 0xaa }, { 0x00, 0x00, 0x00, 0x02, 0x55 }, }; static unsigned char outputs[][32] = { {0xE4, 0x67, 0xF9, 0xA9, 0x55, 0x2B, 0xC7, 0xD3, 0x15, 0x5A, 0x62, 0x20, 0xAF, 0x9C, 0x19, 0x22, 0x0E, 0xEE, 0xD4, 0xFF, 0x78, 0xB0, 0xD1, 0xE6, 0xA1, 0x54, 0x49, 0x91, 0x46, 0x1A, 0x9E, 0x50, }, {0x41, 0x2A, 0xEF, 0xC3, 0x62, 0xA7, 0x28, 0x5F, 0xC3, 0x96, 0x6C, 0x6A, 0x51, 0x81, 0xE7, 0x60, 0x5A, 0xE6, 0x75, 0x23, 0x5B, 0x6D, 0x54, 0x9F, 0xBF, 0xC9, 0xAB, 0x66, 0x30, 0xA4, 0xC6, 0x04, }, {0xFA, 0x62, 0x4F, 0xA0, 0xE5, 0x23, 0x99, 0x3F, 0xA3, 0x88, 0xAE, 0xFD, 0xC6, 0x7E, 0x67, 0xEB, 0xCD, 0x8C, 0x08, 0xE8, 0xA0, 0x24, 0x6B, 0x1D, 0x73, 0xB0, 0xD1, 0xDD, 0x9F, 0xC5, 0x82, 0xB0, }, }; static unsigned char iv[16] = { 0 }; unsigned char result[32] = { 0 }; for (i = 0; i < 3; i++) { p = 0; params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_CIPHER, "CAMELLIA-256-CBC", 0); params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MAC, "CMAC", 0); params[p++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MODE, "FEEDBACK", 0); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i])); params[p++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SEED, iv, sizeof(iv)); params[p] = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), outputs[i], sizeof(outputs[i])); EVP_KDF_CTX_free(kctx); if (ret != 1) return ret; } return ret; } #endif static OSSL_PARAM *construct_kbkdf_params(char *digest, char *mac, unsigned char *key, size_t keylen, char *salt, char *info, int *r) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 8); OSSL_PARAM *p = params; if (params == NULL) return NULL; *p++ = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_DIGEST, digest, 0); *p++ = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MAC, mac, 0); *p++ = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MODE, "COUNTER", 0); *p++ = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_KEY, key, keylen); *p++ = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SALT, salt, strlen(salt)); *p++ = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_INFO, info, strlen(info)); *p++ = OSSL_PARAM_construct_int( OSSL_KDF_PARAM_KBKDF_R, r); *p = OSSL_PARAM_construct_end(); return params; } static int test_kdf_kbkdf_invalid_digest(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; int r = 32; params = construct_kbkdf_params("blah", "HMAC", key, 1, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_false(EVP_KDF_CTX_set_params(kctx, params)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_invalid_mac(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; int r = 32; params = construct_kbkdf_params("sha256", "blah", key, 1, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_false(EVP_KDF_CTX_set_params(kctx, params)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_invalid_r(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; int r = 31; params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_false(EVP_KDF_CTX_set_params(kctx, params)); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_empty_key(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; unsigned char result[32] = { 0 }; int r = 32; params = construct_kbkdf_params("sha256", "HMAC", key, 0, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_int_eq(EVP_KDF_derive(kctx, result, sizeof(result), NULL), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_1byte_key(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; unsigned char result[32] = { 0 }; int r = 32; params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_zero_output_size(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM *params; static unsigned char key[] = {0x01}; unsigned char result[32] = { 0 }; int r = 32; params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r); if (!TEST_ptr(params)) return 0; kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_true(EVP_KDF_CTX_set_params(kctx, params)) && TEST_int_eq(EVP_KDF_derive(kctx, result, 0, NULL), 0); EVP_KDF_CTX_free(kctx); OPENSSL_free(params); return ret; } static int test_kdf_kbkdf_8009_prf1(void) { int ret, i = 0; EVP_KDF_CTX *kctx; OSSL_PARAM params[6]; char *label = "prf", *digest = "sha256", *prf_input = "test", *mac = "HMAC"; static unsigned char input_key[] = { 0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28, 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C, }; static unsigned char output[] = { 0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE, 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86, 0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B, 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95, }; unsigned char result[sizeof(output)] = { 0 }; params[i++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_DIGEST, digest, 0); params[i++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MAC, mac, 0); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SALT, label, strlen(label)); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input)); params[i] = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), output, sizeof(output)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_kbkdf_8009_prf2(void) { int ret, i = 0; EVP_KDF_CTX *kctx; OSSL_PARAM params[6]; char *label = "prf", *digest = "sha384", *prf_input = "test", *mac = "HMAC"; static unsigned char input_key[] = { 0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D, 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98, 0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0, 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52, }; static unsigned char output[] = { 0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6, 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0, 0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C, 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D, 0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33, 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2, }; unsigned char result[sizeof(output)] = { 0 }; params[i++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_DIGEST, digest, 0); params[i++] = OSSL_PARAM_construct_utf8_string( OSSL_KDF_PARAM_MAC, mac, 0); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_SALT, label, strlen(label)); params[i++] = OSSL_PARAM_construct_octet_string( OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input)); params[i] = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), output, sizeof(output)); EVP_KDF_CTX_free(kctx); return ret; } #if !defined(OPENSSL_NO_CMAC) static int test_kdf_kbkdf_fixedinfo(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[8], *p = params; static char *cipher = "AES128"; static char *mac = "CMAC"; static char *mode = "COUNTER"; int use_l = 0; int use_separator = 0; static unsigned char input_key[] = { 0xc1, 0x0b, 0x15, 0x2e, 0x8c, 0x97, 0xb7, 0x7e, 0x18, 0x70, 0x4e, 0x0f, 0x0b, 0xd3, 0x83, 0x05, }; static unsigned char fixed_input[] = { 0x98, 0xcd, 0x4c, 0xbb, 0xbe, 0xbe, 0x15, 0xd1, 0x7d, 0xc8, 0x6e, 0x6d, 0xba, 0xd8, 0x00, 0xa2, 0xdc, 0xbd, 0x64, 0xf7, 0xc7, 0xad, 0x0e, 0x78, 0xe9, 0xcf, 0x94, 0xff, 0xdb, 0xa8, 0x9d, 0x03, 0xe9, 0x7e, 0xad, 0xf6, 0xc4, 0xf7, 0xb8, 0x06, 0xca, 0xf5, 0x2a, 0xa3, 0x8f, 0x09, 0xd0, 0xeb, 0x71, 0xd7, 0x1f, 0x49, 0x7b, 0xcc, 0x69, 0x06, 0xb4, 0x8d, 0x36, 0xc4, }; static unsigned char output[] = { 0x26, 0xfa, 0xf6, 0x19, 0x08, 0xad, 0x9e, 0xe8, 0x81, 0xb8, 0x30, 0x5c, 0x22, 0x1d, 0xb5, 0x3f, }; unsigned char result[sizeof(output)] = { 0 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER, cipher, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, mode, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, fixed_input, sizeof(fixed_input)); *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_L, &use_l); *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, &use_separator); *p = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), output, sizeof(output)); EVP_KDF_CTX_free(kctx); return ret; } #endif static int test_kdf_kbkdf_kmac(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[5], *p = params; static char *mac = "KMAC256"; static unsigned char input_key[] = { 0xDD, 0x81, 0xEF, 0xC8, 0x2C, 0xDD, 0xEC, 0x51, 0xC4, 0x09, 0xBD, 0x8C, 0xCB, 0xAF, 0x94, 0xF6, 0x5F, 0xFA, 0x7B, 0x92, 0xF1, 0x11, 0xF9, 0x40, 0x2B, 0x0D, 0x6A, 0xE0, 0x5E, 0x44, 0x92, 0x34, 0xF0, 0x3B, 0xBA, 0xF5, 0x4F, 0xEF, 0x19, 0x45, 0xDA }; static unsigned char context[] = { 0x81, 0xA1, 0xFE, 0x39, 0x91, 0xEE, 0x3F, 0xD3, 0x90, 0x4E, 0x82, 0xE6, 0x13, 0x20, 0xEC, 0x6B, 0x6E, 0x14, 0x0B, 0xBA, 0x95, 0x5D, 0x0B, 0x52, 0x8E, 0x27, 0x67, 0xB3, 0xE0, 0x77, 0x05, 0x15, 0xBD, 0x78, 0xF6, 0xE8, 0x8A, 0x7D, 0x9B, 0x08, 0x20, 0x0F, 0xE9, 0x8D, 0xD6, 0x24, 0x67, 0xE2, 0xCC, 0x6D, 0x42, 0xE6, 0x60, 0x50, 0x20, 0x77, 0x89, 0x89, 0xB7, 0x2D, 0xF7, 0x5F, 0xE2, 0x79, 0xDB, 0x58, 0x0B, 0x7B, 0x02, 0xB9, 0xD9, 0xB0, 0xFA, 0x6B, 0x0B, 0xB6, 0xD4, 0x95, 0xDB, 0x46, 0x55, 0x5F, 0x12, 0xC3, 0xF0, 0xE0, 0x6E, 0xC8, 0xF4, 0xF8, 0xA1, 0x64, 0x2E, 0x96, 0x74, 0x2B, 0xC6, 0xBD, 0x22, 0xB1, 0x6A, 0xBC, 0x41, 0xDF, 0x30, 0x32, 0xC7, 0xCE, 0x18, 0x14, 0x70, 0x2A, 0xED, 0xE5, 0xC4, 0x6B, 0x8A, 0xA8, 0x36, 0xFD, 0x0A, 0x76, 0x38, 0x44, 0x98, 0x0A, 0xE3, 0xC2, 0x3A, 0x24, 0xCB, 0x45, 0xBF, 0xC9, 0x2C, 0x19, 0xCB, 0x9D, 0x6C, 0x27, 0xDE, 0x78, 0x3E, 0x2C, 0x3D, 0x39, 0x6E, 0x11, 0x59, 0xAE, 0x4F, 0x91, 0x03, 0xE2, 0x7B, 0x97, 0xD6, 0x0C, 0x7D, 0x9D, 0x5A, 0xA5, 0x47, 0x57, 0x41, 0xAD, 0x64, 0x5B, 0xF7, 0x1D, 0x1A, 0xDA, 0x3A, 0x39, 0xDF, 0x85, 0x0D, 0x0F, 0x50, 0x15, 0xA7, 0x3D, 0x68, 0x81, 0x7B, 0x0D, 0xF2, 0x24, 0x24, 0x23, 0x37, 0xE5, 0x77, 0xA6, 0x61, 0xBE, 0xFE, 0x4B, 0x3B, 0x8E, 0x4F, 0x15, 0x4F, 0xC1, 0x30, 0xCB, 0x9E, 0xF5, 0x06, 0x9F, 0xBB, 0x0E, 0xF2, 0xF4, 0x43, 0xBB, 0x64, 0x45, 0xA3, 0x7D, 0x3B, 0xB4, 0x70, 0x47, 0xDF, 0x4A, 0xA5, 0xD9, 0x2F, 0xE6, 0x25, 0xC8, 0x1D, 0x43, 0x0A, 0xEA, 0xF9, 0xCC, 0xC7, 0x1F, 0x8A, 0x2D, 0xD8, 0x95, 0x6B, 0x16, 0x30, 0x1D, 0x80, 0x90, 0xA4, 0x23, 0x14, 0x59, 0xD1, 0x5A, 0x00, 0x48, 0x8D, 0xF7, 0xEA, 0x29, 0x23, 0xDF, 0x35, 0x26, 0x25, 0x22, 0x12, 0xC4, 0x4C, 0x09, 0x69, 0xB8, 0xD6, 0x0C, 0x0E, 0x71, 0x90, 0x6C, 0x42, 0x90, 0x02, 0x53, 0xC5, 0x5A, 0xEF, 0x42, 0x66, 0x1D, 0xAF, 0x45, 0xD5, 0x31, 0xD7, 0x61, 0x3A, 0xE6, 0x06, 0xFB, 0x83, 0x72, 0xAD, 0x82, 0xE3, 0x6A, 0x7E, 0x03, 0x9B, 0x37, 0x77, 0xAF, 0x8D, 0x63, 0x28, 0xC2, 0x8A, 0x5E, 0xC6, 0x3B, 0x22, 0xA8, 0x94, 0xC0, 0x46, 0x2F, 0x73, 0xE7, 0xBB, 0x72, 0x44, 0x85, 0x20, 0x1D, 0xD0, 0x6A, 0x52, 0x8C, 0xB1, 0x8B, 0x96, 0x11, 0xEB, 0xFB, 0xDD, 0xF5, 0x74, 0x49, 0x19, 0x93, 0xD3, 0x7F, 0x6C, 0x27, 0x19, 0x54, 0xDD, 0x00, 0x0F, 0x95, 0xF6, 0x14, 0x15, 0x87, 0x32, 0x54, 0xA5, 0x02, 0xAD, 0x41, 0x55, 0x5E, 0xDD, 0x32, 0x62, 0x3B, 0xFC, 0x71, 0xC1, 0x56, 0xC4, 0x6A, 0xFC, 0xD0, 0xF9, 0x77, 0xDA, 0xC5, 0x20, 0x7D, 0xAC, 0xA8, 0xEB, 0x8F, 0xBE, 0xF9, 0x4D, 0xE8, 0x6D, 0x9E, 0x4C, 0x39, 0xB3, 0x15, 0x63, 0xCD, 0xF6, 0x46, 0xEC, 0x3A, 0xD2, 0x89, 0xA9, 0xFA, 0x24, 0xB4, 0x0E, 0x62, 0x6F, 0x9F, 0xF3, 0xF1, 0x3C, 0x61, 0x57, 0xB9, 0x2C, 0xD4, 0x78, 0x4F, 0x76, 0xCF, 0xFB, 0x6A, 0x51, 0xE8, 0x1E, 0x0A, 0x33, 0x69, 0x16, 0xCD, 0xB7, 0x5C, 0xDF, 0x03, 0x62, 0x17, 0x63, 0x37, 0x49, 0xC3, 0xB7, 0x68, 0x09, 0x9E, 0x22, 0xD2, 0x20, 0x96, 0x37, 0x0D, 0x13, 0xA4, 0x96, 0xB1, 0x8D, 0x0B, 0x12, 0x87, 0xEB, 0x57, 0x25, 0x27, 0x08, 0xFC, 0x90, 0x5E, 0x33, 0x77, 0x50, 0x63, 0xE1, 0x8C, 0xF4, 0x0C, 0x80, 0x89, 0x76, 0x63, 0x70, 0x0A, 0x61, 0x59, 0x90, 0x1F, 0xC9, 0x47, 0xBA, 0x12, 0x7B, 0xB2, 0x7A, 0x44, 0xC3, 0x3D, 0xD0, 0x38, 0xF1, 0x7F, 0x02, 0x92 }; static unsigned char label[] = { 0xA5, 0xDE, 0x2A, 0x0A, 0xF0, 0xDA, 0x59, 0x04, 0xCC, 0xFF, 0x50, 0xD3, 0xA5, 0xD2, 0xDE, 0xA3, 0x33, 0xC0, 0x27, 0xED, 0xDC, 0x6A, 0x54, 0x54, 0x95, 0x78, 0x74, 0x0D, 0xE7, 0xB7, 0x92, 0xD6, 0x64, 0xD5, 0xFB, 0x1F, 0x0F, 0x87, 0xFD, 0x65, 0x79, 0x8B, 0x81, 0x83, 0x95, 0x40, 0x7A, 0x19, 0x8D, 0xCA, 0xE0, 0x4A, 0x93, 0xA8 }; static unsigned char output[] = { 0xB5, 0x61, 0xE3, 0x7D, 0x06, 0xD5, 0x34, 0x80, 0x74, 0x61, 0x16, 0x08, 0x6F, 0x89, 0x6F, 0xB1, 0x43, 0xAF, 0x61, 0x28, 0x93, 0xD8, 0xDF, 0xF6, 0xB6, 0x23, 0x43, 0x68, 0xE4, 0x84, 0xF3, 0xED, 0x50, 0xB6, 0x81, 0x6D, 0x50, 0xF4, 0xAF, 0xF2, 0xA5, 0x50, 0x7E, 0x25, 0xBF, 0x05, 0xBE, 0xE7, 0x07, 0xB0, 0x95, 0xC3, 0x04, 0x38, 0xB4, 0xF9, 0xC1, 0x1E, 0x96, 0x08, 0xF4, 0xC9, 0x05, 0x54, 0x4A, 0xB6, 0x81, 0x92, 0x5B, 0x34, 0x8A, 0x45, 0xDD, 0x7D, 0x98, 0x51, 0x1F, 0xD9, 0x90, 0x23, 0x59, 0x97, 0xA2, 0x4E, 0x43, 0x49, 0xEB, 0x4E, 0x86, 0xEC, 0x20, 0x3C, 0x31, 0xFF, 0x49, 0x55, 0x49, 0xF5, 0xF5, 0x16, 0x79, 0xD9, 0x1C, 0x8E, 0x6E, 0xB3, 0x1C, 0xAF, 0xC8, 0xAB, 0x3A, 0x5A, 0xCE, 0xB1, 0xBD, 0x59, 0x69, 0xEE, 0xC0, 0x28, 0x3E, 0x94, 0xD2, 0xCC, 0x91, 0x93, 0x73, 0x6A, 0xD6, 0xB6, 0xC1, 0x42, 0x97, 0xB1, 0x13, 0xCF, 0xF9, 0x55, 0x35, 0x50, 0xFC, 0x86, 0x75, 0x98, 0x9F, 0xFC, 0x96, 0xB1, 0x43, 0x41, 0x8F, 0xFC, 0x31, 0x09, 0x3B, 0x35, 0x22, 0x7B, 0x01, 0x96, 0xA7, 0xF0, 0x78, 0x7B, 0x57, 0x00, 0xF2, 0xE5, 0x92, 0x36, 0xCE, 0x64, 0xFD, 0x65, 0x09, 0xD8, 0xBC, 0x5C, 0x82, 0x5C, 0x4C, 0x62, 0x5B, 0xCE, 0x09, 0xB6, 0xCF, 0x4D, 0xAD, 0x8E, 0xDD, 0x96, 0xB0, 0xCA, 0x52, 0xC1, 0xF4, 0x17, 0x0E, 0x2D, 0x4E, 0xC3, 0xF9, 0x89, 0x1A, 0x24, 0x3D, 0x01, 0xC8, 0x05, 0xBF, 0x7D, 0x2A, 0x46, 0xCD, 0x9A, 0x66, 0xEE, 0x05, 0x78, 0x88, 0x2A, 0xEF, 0x37, 0x9E, 0x72, 0x55, 0xDA, 0x82, 0x7A, 0x9B, 0xE8, 0xF7, 0xA6, 0x74, 0xB8, 0x74, 0x39, 0x03, 0xE8, 0xB9, 0x1F, 0x97, 0x78, 0xB9, 0xD9, 0x37, 0x16, 0xFD, 0x2F, 0x31, 0xDE, 0xCC, 0x06, 0xD6, 0x5A, 0xEB, 0xD1, 0xBB, 0x84, 0x30, 0x16, 0x81, 0xB0, 0x7E, 0x04, 0x8C, 0x06, 0x67, 0xD1, 0x8A, 0x07, 0x33, 0x76, 0x42, 0x8E, 0x87, 0xAB, 0x90, 0x6F, 0x08, 0xED, 0x8D, 0xE8, 0xD0, 0x20, 0x00, 0x7E, 0x3C, 0x4D, 0xA4, 0x40, 0x37, 0x13, 0x0F, 0x00, 0x0C, 0xB7, 0x26, 0x03, 0x93, 0xD0, 0xBB, 0x08, 0xD3, 0xCC, 0xA9, 0x28, 0xC2 }; unsigned char result[sizeof(output)] = { 0 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, context, sizeof(context)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, label, sizeof(label)); *p = OSSL_PARAM_construct_end(); kctx = get_kdfbyname("KBKDF"); ret = TEST_ptr(kctx) && TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), SIZE_MAX) && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0) && TEST_mem_eq(result, sizeof(result), output, sizeof(output)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_ss_hmac(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[6], *p = params; unsigned char out[16]; static unsigned char z[] = { 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4 }; static unsigned char other[] = { 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc }; static unsigned char salt[] = { 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef, 0x3f,0x89 }; static const unsigned char expected[sizeof(out)] = { 0x44,0xf6,0x76,0xe8,0x5c,0x1b,0x1a,0x8b,0xbc,0x3d,0x31,0x92,0x18,0x63, 0x1c,0xa3 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, (char *)OSSL_MAC_NAME_HMAC, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"sha256", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other, sizeof(other)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt)); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_ss_kmac(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[7], *p = params; unsigned char out[64]; size_t mac_size = 20; static unsigned char z[] = { 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4 }; static unsigned char other[] = { 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc }; static unsigned char salt[] = { 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef, 0x3f,0x89 }; static const unsigned char expected[sizeof(out)] = { 0xe9,0xc1,0x84,0x53,0xa0,0x62,0xb5,0x3b,0xdb,0xfc,0xbb,0x5a,0x34,0xbd, 0xb8,0xe5,0xe7,0x07,0xee,0xbb,0x5d,0xd1,0x34,0x42,0x43,0xd8,0xcf,0xc2, 0xc2,0xe6,0x33,0x2f,0x91,0xbd,0xa5,0x86,0xf3,0x7d,0xe4,0x8a,0x65,0xd4, 0xc5,0x14,0xfd,0xef,0xaa,0x1e,0x67,0x54,0xf3,0x73,0xd2,0x38,0xe1,0x95, 0xae,0x15,0x7e,0x1d,0xe8,0x14,0x98,0x03 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, (char *)OSSL_MAC_NAME_KMAC128, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"SHA256", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other, sizeof(other)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt)); *p++ = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_MAC_SIZE, &mac_size); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF)) && TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), 0) && TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1) && (fips_provider_version_lt(NULL, 3, 0, 8) || TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), SIZE_MAX)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_sshkdf(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[6], *p = params; char kdftype = EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV; unsigned char out[8]; static unsigned char key[] = { 0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a, 0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a, 0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa, 0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78, 0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2, 0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90, 0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80, 0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2, 0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08, 0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43, 0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14, 0x4e }; static unsigned char xcghash[] = { 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23, 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7, 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42 }; static unsigned char sessid[] = { 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23, 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7, 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42 }; static const unsigned char expected[sizeof(out)] = { 0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6 }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"sha256", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, sizeof(key)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, xcghash, sizeof(xcghash)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, sessid, sizeof(sessid)); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, &kdftype, sizeof(kdftype)); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSHKDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdfs_same(EVP_KDF *kdf1, EVP_KDF *kdf2) { if (kdf1 == kdf2) return 1; return TEST_ptr_eq(EVP_KDF_get0_provider(kdf1), EVP_KDF_get0_provider(kdf2)) && TEST_str_eq(EVP_KDF_get0_name(kdf1), EVP_KDF_get0_name(kdf2)); } static int test_kdf_get_kdf(void) { EVP_KDF *kdf1 = NULL, *kdf2 = NULL; ASN1_OBJECT *obj; int ok = 1; if (!TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2)) || !TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_PBKDF2, NULL)) || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(OBJ_obj2nid(obj)), NULL)) || !test_kdfs_same(kdf1, kdf2)) ok = 0; EVP_KDF_free(kdf1); kdf1 = NULL; EVP_KDF_free(kdf2); kdf2 = NULL; if (!TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL)) || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, LN_tls1_prf, NULL)) || !test_kdfs_same(kdf1, kdf2)) ok = 0; EVP_KDF_free(kdf2); kdf2 = NULL; if (!TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(NID_tls1_prf), NULL)) || !test_kdfs_same(kdf1, kdf2)) ok = 0; EVP_KDF_free(kdf1); kdf1 = NULL; EVP_KDF_free(kdf2); kdf2 = NULL; return ok; } #if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES) static int test_kdf_x942_asn1(void) { int ret; EVP_KDF_CTX *kctx = NULL; OSSL_PARAM params[4], *p = params; const char *cek_alg = SN_id_smime_alg_CMS3DESwrap; unsigned char out[24]; static unsigned char z[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d, 0x0e,0x0f,0x10,0x11,0x12,0x13 }; static const unsigned char expected[sizeof(out)] = { 0xa0,0x96,0x61,0x39,0x23,0x76,0xf7,0x04, 0x4d,0x90,0x52,0xa3,0x97,0x88,0x32,0x46, 0xb6,0x7f,0x5f,0x1e,0xf6,0x3e,0xb5,0xfb }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)"sha1", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z)); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG, (char *)cek_alg, 0); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X942KDF_ASN1)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } #endif static int test_kdf_krb5kdf(void) { int ret; EVP_KDF_CTX *kctx; OSSL_PARAM params[4], *p = params; unsigned char out[16]; static unsigned char key[] = { 0x42, 0x26, 0x3C, 0x6E, 0x89, 0xF4, 0xFC, 0x28, 0xB8, 0xDF, 0x68, 0xEE, 0x09, 0x79, 0x9F, 0x15 }; static unsigned char constant[] = { 0x00, 0x00, 0x00, 0x02, 0x99 }; static const unsigned char expected[sizeof(out)] = { 0x34, 0x28, 0x0A, 0x38, 0x2B, 0xC9, 0x27, 0x69, 0xB2, 0xDA, 0x2F, 0x9E, 0xF0, 0x66, 0x85, 0x4B }; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER, (char *)"AES-128-CBC", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, sizeof(key)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_CONSTANT, constant, sizeof(constant)); *p = OSSL_PARAM_construct_end(); ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_KRB5KDF)) && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0) && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_hmac_drbg_settables(void) { int ret = 0, i = 0, j = 0; EVP_KDF_CTX *kctx = NULL; const OSSL_PARAM *settableparams; OSSL_PARAM params[5]; static const unsigned char ent[32] = { 0 }; unsigned char out[32]; char digestname[32]; char macname[32]; EVP_MD *shake256 = NULL; if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF)) || !TEST_ptr(settableparams = EVP_KDF_CTX_settable_params(kctx))) goto err; if (!TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)) goto err; params[1] = OSSL_PARAM_construct_end(); for (i = 0; settableparams[i].key != NULL; ++i) { if (OPENSSL_strcasecmp(settableparams[i].key, OSSL_KDF_PARAM_PROPERTIES) != 0) { TEST_note("Testing set int into %s fails", settableparams[i].key); params[0] = OSSL_PARAM_construct_int(settableparams[i].key, &j); if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0)) goto err; } } params[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, (char *)ent, sizeof(ent)); params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, (char *)ent, sizeof(ent)); params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, "SHA256", 0); params[3] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES, "", 0); params[4] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1)) goto err; if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1)) goto err; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, digestname, sizeof(digestname)); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, macname, sizeof(macname)); params[2] = OSSL_PARAM_construct_end(); if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params), 1) || !TEST_mem_eq(digestname, params[0].return_size, "SHA2-256", 8) || !TEST_mem_eq(macname, params[1].return_size, "HMAC", 4)) goto err; if (!TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 1)) goto err; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, "shake256", 0); params[1] = OSSL_PARAM_construct_end(); if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0)) goto err; ret = 1; err: EVP_MD_free(shake256); EVP_KDF_CTX_free(kctx); return ret; } static int test_kdf_hmac_drbg_gettables(void) { int ret = 0, i, j = 0; EVP_KDF_CTX *kctx = NULL; const OSSL_PARAM *gettableparams; OSSL_PARAM params[3]; char buf[64]; if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF)) || !TEST_ptr(gettableparams = EVP_KDF_CTX_gettable_params(kctx))) goto err; params[1] = OSSL_PARAM_construct_end(); for (i = 0; gettableparams[i].key != NULL; ++i) { params[0] = OSSL_PARAM_construct_int(gettableparams[i].key, &j); if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0)) goto err; } for (i = 0; gettableparams[i].key != NULL; ++i) { params[0] = OSSL_PARAM_construct_utf8_string(gettableparams[i].key, buf, sizeof(buf)); if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0)) goto err; } ret = 1; err: EVP_KDF_CTX_free(kctx); return ret; } int setup_tests(void) { ADD_TEST(test_kdf_pbkdf1); ADD_TEST(test_kdf_pbkdf1_key_too_long); #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA) ADD_TEST(test_kdf_kbkdf_6803_128); ADD_TEST(test_kdf_kbkdf_6803_256); #endif ADD_TEST(test_kdf_kbkdf_invalid_digest); ADD_TEST(test_kdf_kbkdf_invalid_mac); ADD_TEST(test_kdf_kbkdf_invalid_r); ADD_TEST(test_kdf_kbkdf_zero_output_size); ADD_TEST(test_kdf_kbkdf_empty_key); ADD_TEST(test_kdf_kbkdf_1byte_key); ADD_TEST(test_kdf_kbkdf_8009_prf1); ADD_TEST(test_kdf_kbkdf_8009_prf2); #if !defined(OPENSSL_NO_CMAC) ADD_TEST(test_kdf_kbkdf_fixedinfo); #endif if (fips_provider_version_ge(NULL, 3, 1, 0)) ADD_TEST(test_kdf_kbkdf_kmac); ADD_TEST(test_kdf_get_kdf); ADD_TEST(test_kdf_tls1_prf); ADD_TEST(test_kdf_tls1_prf_invalid_digest); ADD_TEST(test_kdf_tls1_prf_zero_output_size); ADD_TEST(test_kdf_tls1_prf_empty_secret); ADD_TEST(test_kdf_tls1_prf_1byte_secret); ADD_TEST(test_kdf_tls1_prf_empty_seed); ADD_TEST(test_kdf_tls1_prf_1byte_seed); ADD_TEST(test_kdf_hkdf); ADD_TEST(test_kdf_hkdf_invalid_digest); ADD_TEST(test_kdf_hkdf_zero_output_size); ADD_TEST(test_kdf_hkdf_empty_key); ADD_TEST(test_kdf_hkdf_1byte_key); ADD_TEST(test_kdf_hkdf_empty_salt); ADD_TEST(test_kdf_hkdf_gettables); ADD_TEST(test_kdf_hkdf_gettables_expandonly); ADD_TEST(test_kdf_hkdf_gettables_no_digest); ADD_TEST(test_kdf_hkdf_derive_set_params_fail); ADD_TEST(test_kdf_hkdf_set_invalid_mode); ADD_TEST(test_kdf_hkdf_set_ctx_param_fail); ADD_TEST(test_kdf_pbkdf2); ADD_TEST(test_kdf_pbkdf2_small_output); ADD_TEST(test_kdf_pbkdf2_large_output); ADD_TEST(test_kdf_pbkdf2_small_salt); ADD_TEST(test_kdf_pbkdf2_small_iterations); ADD_TEST(test_kdf_pbkdf2_small_salt_pkcs5); ADD_TEST(test_kdf_pbkdf2_small_iterations_pkcs5); ADD_TEST(test_kdf_pbkdf2_invalid_digest); #ifndef OPENSSL_NO_SCRYPT ADD_TEST(test_kdf_scrypt); #endif ADD_TEST(test_kdf_ss_hash); ADD_TEST(test_kdf_ss_hmac); ADD_TEST(test_kdf_ss_kmac); ADD_TEST(test_kdf_sshkdf); ADD_TEST(test_kdf_x963); #if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES) ADD_TEST(test_kdf_x942_asn1); #endif ADD_TEST(test_kdf_krb5kdf); ADD_TEST(test_kdf_hmac_drbg_settables); ADD_TEST(test_kdf_hmac_drbg_gettables); return 1; }
test
openssl/test/evp_kdf_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <openssl/mdc2.h> #include "testutil.h" #include "internal/nelem.h" typedef struct { const char *input; const unsigned char expected[MDC2_DIGEST_LENGTH]; } TESTDATA; static TESTDATA tests[] = { { "Now is the time for all ", { 0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA, 0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A } } }; static int test_mdc2(int idx) { unsigned char md[MDC2_DIGEST_LENGTH]; MDC2_CTX c; const TESTDATA testdata = tests[idx]; MDC2_Init(&c); MDC2_Update(&c, (const unsigned char *)testdata.input, strlen(testdata.input)); MDC2_Final(&(md[0]), &c); if (!TEST_mem_eq(testdata.expected, MDC2_DIGEST_LENGTH, md, MDC2_DIGEST_LENGTH)) { TEST_info("mdc2 test %d: unexpected output", idx); return 0; } return 1; } int setup_tests(void) { ADD_ALL_TESTS(test_mdc2, OSSL_NELEM(tests)); return 1; }
test
openssl/test/mdc2_internal_test.c
openssl
#include <string.h> #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/encoder.h> #include <openssl/provider.h> #include <openssl/param_build.h> #include <openssl/core_names.h> #include <openssl/sha.h> #include "crypto/ecx.h" #include "crypto/evp.h" #include "crypto/bn_dh.h" #include "internal/nelem.h" #include "testutil.h" static char *datadir = NULL; #define PRIV_TEXT 0 #define PRIV_PEM 1 #define PRIV_DER 2 #define PUB_TEXT 3 #define PUB_PEM 4 #define PUB_DER 5 static void stripcr(char *buf, size_t *len) { size_t i; char *curr, *writ; for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) { if (*curr == '\r') { (*len)--; continue; } if (curr != writ) *writ = *curr; writ++; } } static int compare_with_file(const char *alg, int type, BIO *membio) { char filename[80]; BIO *file = NULL; char buf[4096]; char *memdata, *fullfile = NULL; const char *suffix; size_t readbytes; int ret = 0; int len; size_t slen; switch (type) { case PRIV_TEXT: suffix = "priv.txt"; break; case PRIV_PEM: suffix = "priv.pem"; break; case PRIV_DER: suffix = "priv.der"; break; case PUB_TEXT: suffix = "pub.txt"; break; case PUB_PEM: suffix = "pub.pem"; break; case PUB_DER: suffix = "pub.der"; break; default: TEST_error("Invalid file type"); goto err; } BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix); fullfile = test_mk_file_path(datadir, filename); if (!TEST_ptr(fullfile)) goto err; file = BIO_new_file(fullfile, "rb"); if (!TEST_ptr(file)) goto err; if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes)) || !TEST_true(BIO_eof(file)) || !TEST_size_t_lt(readbytes, sizeof(buf))) goto err; len = BIO_get_mem_data(membio, &memdata); if (!TEST_int_gt(len, 0)) goto err; slen = len; if (type != PRIV_DER && type != PUB_DER) { stripcr(memdata, &slen); stripcr(buf, &readbytes); } if (!TEST_mem_eq(memdata, slen, buf, readbytes)) goto err; ret = 1; err: OPENSSL_free(fullfile); (void)BIO_reset(membio); BIO_free(file); return ret; } static int pass_cb(char *buf, int size, int rwflag, void *u) { return 0; } static int pass_cb_error(char *buf, int size, int rwflag, void *u) { return -1; } static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk) { BIO *membio = BIO_new(BIO_s_mem()); int ret = 0; if (!TEST_ptr(membio)) goto err; if ( !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(), (unsigned char *)"pass", 4, NULL, NULL)) || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), (const char *)~0, 0, NULL, NULL)) || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, NULL, "")) || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, pass_cb, NULL)) || !TEST_false(PEM_write_bio_PKCS8PrivateKey(bio_out, pk, EVP_aes_256_cbc(), NULL, 0, pass_cb_error, NULL)) #ifndef OPENSSL_NO_DES || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, (const char *)~0, 0, NULL, NULL)) || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, NULL, "")) || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, pass_cb, NULL)) || !TEST_false(PEM_write_bio_PKCS8PrivateKey_nid( bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0, pass_cb_error, NULL)) #endif || !TEST_int_gt(EVP_PKEY_print_private(membio, pk, 0, NULL), 0) || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio)) || !TEST_true(PEM_write_bio_PUBKEY(membio, pk)) || !TEST_true(compare_with_file(alg, PUB_PEM, membio)) || !TEST_true(PEM_write_bio_PrivateKey(membio, pk, NULL, NULL, 0, NULL, NULL)) || !TEST_true(compare_with_file(alg, PRIV_PEM, membio)) || !TEST_false(PEM_write_bio_PrivateKey(membio, NULL, NULL, NULL, 0, NULL, NULL)) || !TEST_false(PEM_write_bio_PrivateKey_traditional(membio, NULL, NULL, NULL, 0, NULL, NULL))) goto err; ret = 1; err: BIO_free(membio); return ret; } static int test_print_key_type_using_encoder(const char *alg, int type, const EVP_PKEY *pk) { const char *output_type, *output_structure; int selection; OSSL_ENCODER_CTX *ctx = NULL; BIO *membio = BIO_new(BIO_s_mem()); int ret = 0; switch (type) { case PRIV_TEXT: output_type = "TEXT"; output_structure = NULL; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PRIV_PEM: output_type = "PEM"; output_structure = "PrivateKeyInfo"; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PRIV_DER: output_type = "DER"; output_structure = "PrivateKeyInfo"; selection = OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_TEXT: output_type = "TEXT"; output_structure = NULL; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_PEM: output_type = "PEM"; output_structure = "SubjectPublicKeyInfo"; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; case PUB_DER: output_type = "DER"; output_structure = "SubjectPublicKeyInfo"; selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; break; default: TEST_error("Invalid encoding type"); goto err; } if (!TEST_ptr(membio)) goto err; TEST_note("Setting up a OSSL_ENCODER context with passphrase"); if (!TEST_ptr(ctx = OSSL_ENCODER_CTX_new_for_pkey(pk, selection, output_type, output_structure, NULL)) || !TEST_int_ne(OSSL_ENCODER_CTX_get_num_encoders(ctx), 0)) goto err; TEST_note("Testing with no encryption"); if (!TEST_true(OSSL_ENCODER_to_bio(ctx, membio)) || !TEST_true(compare_with_file(alg, type, membio))) goto err; if (type == PRIV_PEM) { if (!TEST_true(OSSL_ENCODER_CTX_set_passphrase(ctx, (unsigned char *)"pass", 4))) goto err; TEST_note("Displaying PEM encrypted with AES-256-CBC"); if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, "AES-256-CBC", NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ctx, bio_out))) goto err; TEST_note("NOT Displaying PEM encrypted with (invalid) FOO"); if (!TEST_false(OSSL_ENCODER_CTX_set_cipher(ctx, "FOO", NULL)) || !TEST_false(OSSL_ENCODER_to_bio(ctx, bio_out))) goto err; TEST_note("Testing with encryption cleared (no encryption)"); if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, NULL, NULL)) || !TEST_true(OSSL_ENCODER_to_bio(ctx, membio)) || !TEST_true(compare_with_file(alg, type, membio))) goto err; } ret = 1; err: BIO_free(membio); OSSL_ENCODER_CTX_free(ctx); return ret; } static int test_print_key_using_encoder(const char *alg, const EVP_PKEY *pk) { int i; int ret = 1; for (i = PRIV_TEXT; i <= PUB_DER; i++) ret = ret && test_print_key_type_using_encoder(alg, i, pk); return ret; } #ifndef OPENSSL_NO_ECX static int test_print_key_using_encoder_public(const char *alg, const EVP_PKEY *pk) { int i; int ret = 1; for (i = PUB_TEXT; i <= PUB_DER; i++) ret = ret && test_print_key_type_using_encoder(alg, i, pk); return ret; } #endif #define N 0 #define E 1 #define D 2 #define P 3 #define Q 4 #define DP 5 #define DQ 6 #define QINV 7 static int test_fromdata_rsa(void) { int ret = 0, i; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; static unsigned long key_numbers[] = { 0xbc747fc5, 0x10001, 0x7b133399, 0xe963, 0xceb7, 0x8599, 0xbd87, 0xcc3b, }; OSSL_PARAM fromdata_params[] = { OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]), OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]), OSSL_PARAM_END }; BIGNUM *bn = BN_new(); BIGNUM *bn_from = BN_new(); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 32) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 8) || !TEST_int_eq(EVP_PKEY_get_size(pk), 4) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; EVP_PKEY_CTX_free(key_ctx); if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("RSA", pk) && test_print_key_using_encoder("RSA", pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: for (i = 0; fromdata_params[i].key != NULL; ++i) { if (!TEST_true(BN_set_word(bn_from, key_numbers[i])) || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key, &bn)) || !TEST_BN_eq(bn, bn_from)) ret = 0; } BN_free(bn_from); BN_free(bn); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(key_ctx); EVP_PKEY_CTX_free(ctx); return ret; } struct check_data { const char *pname; BIGNUM *comparebn; }; static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params, struct check_data check[], int expected_nbits, int expected_sbits, int expected_ksize) { const OSSL_PARAM *check_param = NULL; BIGNUM *check_bn = NULL; OSSL_PARAM *todata_params = NULL; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; int i; int ret = 0; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_todata(pk, EVP_PKEY_KEYPAIR, &todata_params), 1)) goto err; for (i = 0; check[i].pname != NULL; i++) { if (!TEST_ptr(check_param = OSSL_PARAM_locate_const(todata_params, check[i].pname))) goto err; if (!TEST_int_eq(OSSL_PARAM_get_BN(check_param, &check_bn), 1)) goto err; if (!TEST_BN_eq(check_bn, check[i].comparebn)) { TEST_info("Data mismatch for parameter %s", check[i].pname); goto err; } BN_free(check_bn); check_bn = NULL; } for (;;) { if (!TEST_int_eq(EVP_PKEY_get_bits(pk), expected_nbits) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), expected_sbits) || !TEST_int_eq(EVP_PKEY_get_size(pk), expected_ksize) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; EVP_PKEY_CTX_free(key_ctx); if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; if (!TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1)) { EVP_PKEY_free(dup_pk); goto err; } EVP_PKEY_free(pk); pk = dup_pk; } ret = 1; err: BN_free(check_bn); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_free(todata_params); return ret; } static int test_fromdata_rsa_derive_from_pq_sp800(void) { OSSL_PARAM_BLD *bld = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL; BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; OSSL_PARAM *fromdata_params = NULL; struct check_data cdata[4]; int ret = 0; static unsigned char n_data[] = {0x00, 0xc7, 0x06, 0xd8, 0x6b, 0x3c, 0x4f, 0xb7, 0x95, 0x42, 0x44, 0x90, 0xbd, 0xef, 0xf3, 0xc4, 0xb5, 0xa8, 0x55, 0x9e, 0x33, 0xa3, 0x04, 0x3a, 0x90, 0xe5, 0x13, 0xff, 0x87, 0x69, 0x15, 0xa4, 0x8a, 0x17, 0x10, 0xcc, 0xdf, 0xf9, 0xc5, 0x0f, 0xf1, 0x12, 0xff, 0x12, 0x11, 0xe5, 0x6b, 0x5c, 0x83, 0xd9, 0x43, 0xd1, 0x8a, 0x7e, 0xa6, 0x60, 0x07, 0x2e, 0xbb, 0x03, 0x17, 0x2d, 0xec, 0x17, 0x87}; static unsigned char e_data[] = {0x01, 0x00, 0x01}; static unsigned char d_data[] = {0x1e, 0x5e, 0x5d, 0x07, 0x7f, 0xdc, 0x6a, 0x16, 0xcc, 0x55, 0xca, 0x00, 0x31, 0x6c, 0xf0, 0xc7, 0x07, 0x38, 0x89, 0x3b, 0x37, 0xd4, 0x9d, 0x5b, 0x1e, 0x99, 0x3e, 0x94, 0x5a, 0xe4, 0x82, 0x86, 0x8a, 0x78, 0x34, 0x09, 0x37, 0xd5, 0xe7, 0xb4, 0xef, 0x5f, 0x83, 0x94, 0xff, 0xe5, 0x36, 0x79, 0x10, 0x0c, 0x38, 0xc5, 0x3a, 0x33, 0xa6, 0x7c, 0x3c, 0xcc, 0x98, 0xe0, 0xf5, 0xdb, 0xe6, 0x81}; static unsigned char p_data[] = {0x00, 0xf6, 0x61, 0x38, 0x0e, 0x1f, 0x82, 0x7c, 0xb8, 0xba, 0x00, 0xd3, 0xac, 0xdc, 0x4e, 0x6b, 0x7e, 0xf7, 0x58, 0xf3, 0xd9, 0xd8, 0x21, 0xed, 0x54, 0xa3, 0x36, 0xd2, 0x2c, 0x5f, 0x06, 0x7d, 0xc5}; static unsigned char q_data[] = {0x00, 0xce, 0xcc, 0x4a, 0xa5, 0x4f, 0xd6, 0x73, 0xd0, 0x20, 0xc3, 0x98, 0x64, 0x20, 0x9b, 0xc1, 0x23, 0xd8, 0x5c, 0x82, 0x4f, 0xe8, 0xa5, 0x32, 0xcd, 0x7e, 0x97, 0xb4, 0xde, 0xf6, 0x4c, 0x80, 0xdb}; static unsigned char dmp1_data[] = {0x00, 0xd1, 0x07, 0xb6, 0x79, 0x34, 0xfe, 0x8e, 0x36, 0x63, 0x88, 0xa4, 0x0e, 0x3a, 0x73, 0x45, 0xfc, 0x58, 0x7a, 0x5d, 0x98, 0xeb, 0x28, 0x0d, 0xa5, 0x0b, 0x3c, 0x4d, 0xa0, 0x5b, 0x96, 0xb4, 0x49}; static unsigned char dmq1_data[] = {0x5b, 0x47, 0x02, 0xdf, 0xaa, 0xb8, 0xae, 0x8f, 0xbc, 0x16, 0x79, 0x6a, 0x20, 0x96, 0x7f, 0x0e, 0x92, 0x4e, 0x6a, 0xda, 0x58, 0x86, 0xaa, 0x40, 0xd7, 0xd2, 0xa0, 0x6c, 0x15, 0x6c, 0xb9, 0x27}; static unsigned char iqmp_data[] = {0x00, 0xa0, 0xd6, 0xf0, 0xe8, 0x17, 0x9e, 0xe7, 0xe6, 0x99, 0x12, 0xd6, 0xd9, 0x43, 0xcf, 0xed, 0x37, 0x29, 0xf5, 0x6c, 0x3e, 0xc1, 0x7f, 0x2e, 0x31, 0x3f, 0x64, 0x34, 0x66, 0x68, 0x5c, 0x22, 0x08}; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL)) || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL)) || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2, q)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1; cdata[0].comparebn = dmp1; cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2; cdata[1].comparebn = dmq1; cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1; cdata[2].comparebn = iqmp; cdata[3].pname = NULL; cdata[3].comparebn = NULL; ret = do_fromdata_rsa_derive(fromdata_params, cdata, 512, 56, 64); err: BN_free(n); BN_free(e); BN_free(d); BN_free(p); BN_free(q); BN_free(dmp1); BN_free(dmq1); BN_free(iqmp); OSSL_PARAM_BLD_free(bld); return ret; } static int test_fromdata_rsa_derive_from_pq_multiprime(void) { OSSL_PARAM_BLD *bld = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL; BIGNUM *p = NULL, *q = NULL, *p2 = NULL; BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL; BIGNUM *exp3 = NULL, *coeff2 = NULL; OSSL_PARAM *fromdata_params = NULL; struct check_data cdata[12]; int ret = 0; static unsigned char n_data[] = {0x00, 0x95, 0x78, 0x21, 0xe0, 0xca, 0x94, 0x6c, 0x0b, 0x86, 0x2a, 0x01, 0xde, 0xd9, 0xab, 0xee, 0x88, 0x4a, 0x27, 0x4f, 0xcc, 0x5f, 0xf1, 0x71, 0xe1, 0x0b, 0xc3, 0xd1, 0x88, 0x76, 0xf0, 0x83, 0x03, 0x93, 0x7e, 0x39, 0xfa, 0x47, 0x89, 0x34, 0x27, 0x18, 0x19, 0x97, 0xfc, 0xd4, 0xfe, 0xe5, 0x8a, 0xa9, 0x11, 0x83, 0xb5, 0x15, 0x4a, 0x29, 0xa6, 0xa6, 0xd0, 0x6e, 0x0c, 0x7f, 0x61, 0x8f, 0x7e, 0x7c, 0xfb, 0xfc, 0x04, 0x8b, 0xca, 0x44, 0xf8, 0x59, 0x0b, 0x22, 0x6f, 0x3f, 0x92, 0x23, 0x98, 0xb5, 0xc8, 0xf7, 0xff, 0xf7, 0xac, 0x6b, 0x36, 0xb3, 0xaf, 0x39, 0xde, 0x66, 0x38, 0x51, 0x9f, 0xbe, 0xe2, 0xfc, 0xe4, 0x6f, 0x1a, 0x0f, 0x7a, 0xde, 0x7f, 0x0f, 0x4e, 0xbc, 0xed, 0xa2, 0x99, 0xc5, 0xd1, 0xbf, 0x8f, 0xba, 0x92, 0x91, 0xe4, 0x00, 0x91, 0xbb, 0x67, 0x36, 0x7d, 0x00, 0x50, 0xda, 0x28, 0x38, 0xdc, 0x9f, 0xfe, 0x3f, 0x24, 0x5a, 0x0d, 0xe1, 0x8d, 0xe9, 0x45, 0x2c, 0xd7, 0xf2, 0x67, 0x8c, 0x0c, 0x6e, 0xdb, 0xc8, 0x8b, 0x6b, 0x38, 0x30, 0x21, 0x94, 0xc0, 0xe3, 0xd7, 0xe0, 0x23, 0xd3, 0xd4, 0xfa, 0xdb, 0xb9, 0xfe, 0x1a, 0xcc, 0xc9, 0x79, 0x19, 0x35, 0x18, 0x42, 0x30, 0xc4, 0xb5, 0x92, 0x33, 0x1e, 0xd4, 0xc4, 0xc0, 0x9d, 0x55, 0x37, 0xd4, 0xef, 0x54, 0x71, 0x81, 0x09, 0x15, 0xdb, 0x11, 0x38, 0x6b, 0x35, 0x93, 0x11, 0xdc, 0xb1, 0x6c, 0xd6, 0xa4, 0x37, 0x84, 0xf3, 0xb2, 0x2f, 0x1b, 0xd6, 0x05, 0x9f, 0x0e, 0x5c, 0x98, 0x29, 0x2f, 0x95, 0xb6, 0x55, 0xbd, 0x24, 0x44, 0xc5, 0xc8, 0xa2, 0x76, 0x1e, 0xf8, 0x82, 0x8a, 0xdf, 0x34, 0x72, 0x7e, 0xdd, 0x65, 0x4b, 0xfc, 0x6c, 0x1c, 0x96, 0x70, 0xe2, 0x69, 0xb5, 0x12, 0x1b, 0x59, 0x67, 0x14, 0x9d}; static unsigned char e_data[] = {0x01, 0x00, 0x01}; static unsigned char d_data[] = {0x64, 0x57, 0x4d, 0x86, 0xf6, 0xf8, 0x44, 0xc0, 0x47, 0xc5, 0x13, 0x94, 0x63, 0x54, 0x84, 0xc1, 0x81, 0xe6, 0x7a, 0x2f, 0x9d, 0x89, 0x1d, 0x06, 0x13, 0x3b, 0xd6, 0x02, 0x62, 0xb6, 0x7b, 0x7d, 0x7f, 0x1a, 0x92, 0x19, 0x6e, 0xc4, 0xb0, 0xfa, 0x3d, 0xb7, 0x90, 0xcc, 0xee, 0xc0, 0x5f, 0xa0, 0x82, 0x77, 0x7b, 0x8f, 0xa9, 0x47, 0x2c, 0x46, 0xf0, 0x5d, 0xa4, 0x43, 0x47, 0x90, 0x5b, 0x20, 0x73, 0x0f, 0x46, 0xd4, 0x56, 0x73, 0xe7, 0x71, 0x41, 0x75, 0xb4, 0x1c, 0x32, 0xf5, 0x0c, 0x68, 0x8c, 0x40, 0xea, 0x1c, 0x30, 0x12, 0xa2, 0x65, 0x02, 0x27, 0x98, 0x4e, 0x0a, 0xbf, 0x2b, 0x72, 0xb2, 0x5c, 0xe3, 0xbe, 0x3e, 0xc7, 0xdb, 0x9b, 0xa2, 0x4a, 0x90, 0xc0, 0xa7, 0xb0, 0x00, 0xf1, 0x6a, 0xff, 0xa3, 0x77, 0xf7, 0x71, 0xa2, 0x41, 0xe9, 0x6e, 0x7c, 0x38, 0x24, 0x46, 0xd5, 0x5c, 0x49, 0x2a, 0xe6, 0xee, 0x27, 0x4b, 0x2e, 0x6f, 0x16, 0x54, 0x2d, 0x37, 0x36, 0x01, 0x39, 0x2b, 0x23, 0x4b, 0xb4, 0x65, 0x25, 0x4d, 0x7f, 0x72, 0x20, 0x7f, 0x5d, 0xec, 0x50, 0xba, 0xbb, 0xaa, 0x9c, 0x3c, 0x1d, 0xa1, 0x40, 0x2c, 0x6a, 0x8b, 0x5f, 0x2e, 0xe0, 0xa6, 0xf7, 0x9e, 0x03, 0xb5, 0x44, 0x5f, 0x74, 0xc7, 0x9f, 0x89, 0x2b, 0x71, 0x2f, 0x66, 0x9f, 0x03, 0x6c, 0x96, 0xd0, 0x23, 0x36, 0x4d, 0xa1, 0xf0, 0x82, 0xcc, 0x43, 0xe7, 0x08, 0x93, 0x40, 0x18, 0xc0, 0x39, 0x73, 0x83, 0xe2, 0xec, 0x9b, 0x81, 0x9d, 0x4c, 0x86, 0xaa, 0x59, 0xa8, 0x67, 0x1c, 0x80, 0xdc, 0x6f, 0x7f, 0x23, 0x6b, 0x7d, 0x2c, 0x56, 0x99, 0xa0, 0x89, 0x7e, 0xdb, 0x8b, 0x7a, 0xaa, 0x03, 0x8e, 0x8e, 0x8e, 0x3a, 0x58, 0xb4, 0x03, 0x6b, 0x65, 0xfa, 0x92, 0x0a, 0x96, 0x93, 0xa6, 0x07, 0x60, 0x01}; static unsigned char p_data[] = {0x06, 0x55, 0x7f, 0xbd, 0xfd, 0xa8, 0x4c, 0x94, 0x5e, 0x10, 0x8a, 0x54, 0x37, 0xf3, 0x64, 0x37, 0x3a, 0xca, 0x18, 0x1b, 0xdd, 0x71, 0xa5, 0x94, 0xc9, 0x31, 0x59, 0xa5, 0x89, 0xe9, 0xc4, 0xba, 0x55, 0x90, 0x6d, 0x9c, 0xcc, 0x52, 0x5d, 0x44, 0xa8, 0xbc, 0x2b, 0x3b, 0x8c, 0xbd, 0x96, 0xfa, 0xcd, 0x54, 0x63, 0xe3, 0xc8, 0xfe, 0x5e, 0xc6, 0x73, 0x98, 0x14, 0x7a, 0x54, 0x0e, 0xe7, 0x75, 0x49, 0x93, 0x20, 0x33, 0x17, 0xa9, 0x34, 0xa8, 0xee, 0xaf, 0x3a, 0xcc, 0xf5, 0x69, 0xfc, 0x30, 0x1a, 0xdf, 0x49, 0x61, 0xa4, 0xd1}; static unsigned char p2_data[] = {0x03, 0xe2, 0x41, 0x3d, 0xb1, 0xdd, 0xad, 0xd7, 0x3b, 0xf8, 0xab, 0x32, 0x27, 0x8b, 0xac, 0x95, 0xc0, 0x1a, 0x3f, 0x80, 0x8e, 0x21, 0xa9, 0xb8, 0xa2, 0xed, 0xcf, 0x97, 0x5c, 0x61, 0x10, 0x94, 0x1b, 0xd0, 0xbe, 0x88, 0xc2, 0xa7, 0x20, 0xe5, 0xa5, 0xc2, 0x7a, 0x7e, 0xf0, 0xd1, 0xe4, 0x13, 0x75, 0xb9, 0x62, 0x90, 0xf1, 0xc3, 0x5b, 0x8c, 0xe9, 0xa9, 0x5b, 0xb7, 0x6d, 0xdc, 0xcd, 0x12, 0xea, 0x97, 0x05, 0x04, 0x25, 0x2a, 0x93, 0xd1, 0x4e, 0x05, 0x1a, 0x50, 0xa2, 0x67, 0xb8, 0x4b, 0x09, 0x15, 0x65, 0x6c, 0x66, 0x2d}; static unsigned char q_data[] = {0x06, 0x13, 0x74, 0x6e, 0xde, 0x7c, 0x33, 0xc2, 0xe7, 0x05, 0x2c, 0xeb, 0x25, 0x7d, 0x4a, 0x07, 0x7e, 0x03, 0xcf, 0x6a, 0x23, 0x36, 0x25, 0x23, 0xf6, 0x5d, 0xde, 0xa3, 0x0f, 0x82, 0xe6, 0x4b, 0xec, 0x39, 0xbf, 0x37, 0x1f, 0x4f, 0x56, 0x1e, 0xd8, 0x62, 0x32, 0x5c, 0xf5, 0x37, 0x75, 0x20, 0xe2, 0x7e, 0x56, 0x82, 0xc6, 0x35, 0xd3, 0x4d, 0xfa, 0x6c, 0xc3, 0x93, 0xf0, 0x60, 0x53, 0x78, 0x95, 0xee, 0xf9, 0x8b, 0x2c, 0xaf, 0xb1, 0x47, 0x5c, 0x29, 0x0d, 0x2a, 0x47, 0x7f, 0xd0, 0x7a, 0x4e, 0x26, 0x7b, 0x47, 0xfb, 0x61}; static unsigned char dmp1_data[] = {0x01, 0x13, 0x3a, 0x1f, 0x91, 0x92, 0xa3, 0x8c, 0xfb, 0x7a, 0x6b, 0x40, 0x68, 0x4e, 0xd3, 0xcf, 0xdc, 0x16, 0xb9, 0x88, 0xe1, 0x49, 0x8d, 0x05, 0x78, 0x30, 0xfc, 0x3a, 0x70, 0xf2, 0x51, 0x06, 0x1f, 0xc7, 0xe8, 0x13, 0x19, 0x4b, 0x51, 0xb1, 0x79, 0xc2, 0x96, 0xc4, 0x00, 0xdb, 0x9d, 0x68, 0xec, 0xb9, 0x4a, 0x4b, 0x3b, 0xae, 0x91, 0x7f, 0xb5, 0xd7, 0x36, 0x82, 0x9d, 0x09, 0xfa, 0x97, 0x99, 0xe9, 0x73, 0x29, 0xb8, 0xf6, 0x6b, 0x8d, 0xd1, 0x15, 0xc5, 0x31, 0x4c, 0xe6, 0xb4, 0x7b, 0xa5, 0xd4, 0x08, 0xac, 0x9e, 0x41}; static unsigned char dmq1_data[] = {0x05, 0xcd, 0x33, 0xc2, 0xdd, 0x3b, 0xb8, 0xec, 0xe4, 0x4c, 0x03, 0xcc, 0xef, 0xba, 0x07, 0x22, 0xca, 0x47, 0x77, 0x18, 0x40, 0x50, 0xe5, 0xfb, 0xc5, 0xb5, 0x71, 0xed, 0x3e, 0xd5, 0x5d, 0x72, 0xa7, 0x37, 0xa8, 0x86, 0x48, 0xa6, 0x27, 0x74, 0x42, 0x66, 0xd8, 0xf1, 0xfb, 0xcf, 0x1d, 0x4e, 0xee, 0x15, 0x76, 0x23, 0x5e, 0x81, 0x6c, 0xa7, 0x2b, 0x74, 0x08, 0xf7, 0x4c, 0x71, 0x9d, 0xa2, 0x29, 0x7f, 0xca, 0xd5, 0x02, 0x31, 0x2c, 0x54, 0x18, 0x02, 0xb6, 0xa8, 0x65, 0x26, 0xfc, 0xf8, 0x9b, 0x80, 0x90, 0xfc, 0x75, 0x61}; static unsigned char iqmp_data[] = {0x05, 0x78, 0xf8, 0xdd, 0x1c, 0x6f, 0x3d, 0xaf, 0x53, 0x84, 0x32, 0xa9, 0x35, 0x52, 0xf3, 0xd0, 0x4d, 0xf8, 0x09, 0x85, 0x3d, 0x72, 0x20, 0x8b, 0x47, 0xba, 0xc8, 0xce, 0xac, 0xd9, 0x76, 0x90, 0x05, 0x88, 0x63, 0x8a, 0x10, 0x2b, 0xcd, 0xd3, 0xbe, 0x8c, 0x16, 0x60, 0x6a, 0xfd, 0xce, 0xc7, 0x9f, 0xfa, 0xbb, 0xe3, 0xa6, 0xde, 0xc2, 0x8f, 0x1d, 0x25, 0xdc, 0x41, 0xcb, 0xa4, 0xeb, 0x76, 0xc9, 0xdc, 0x8e, 0x49, 0x0e, 0xe4, 0x7c, 0xd2, 0xd5, 0x6e, 0x26, 0x3c, 0x0b, 0xd3, 0xc5, 0x20, 0x4e, 0x4b, 0xb6, 0xf7, 0xae, 0xef}; static unsigned char exp3_data[] = {0x02, 0x7d, 0x16, 0x24, 0xfc, 0x35, 0xf9, 0xd0, 0xb3, 0x02, 0xf2, 0x5f, 0xde, 0xeb, 0x27, 0x19, 0x85, 0xd0, 0xcb, 0xe4, 0x0a, 0x2f, 0x13, 0xdb, 0xd5, 0xba, 0xe0, 0x8c, 0x32, 0x8b, 0x97, 0xdd, 0xef, 0xbc, 0xe0, 0x7a, 0x2d, 0x90, 0x7e, 0x09, 0xe9, 0x1f, 0x26, 0xf2, 0xf4, 0x48, 0xea, 0x06, 0x76, 0x26, 0xe6, 0x3b, 0xce, 0x4e, 0xc9, 0xf9, 0x0f, 0x38, 0x90, 0x26, 0x87, 0x65, 0x36, 0x9a, 0xea, 0x6a, 0xfe, 0xb1, 0xdb, 0x46, 0xdf, 0x14, 0xfd, 0x13, 0x53, 0xfb, 0x5b, 0x35, 0x6e, 0xe7, 0xd5, 0xd8, 0x39, 0xf7, 0x2d, 0xb9}; static unsigned char coeff2_data[] = {0x01, 0xba, 0x66, 0x0a, 0xa2, 0x86, 0xc0, 0x57, 0x7f, 0x4e, 0x68, 0xb1, 0x86, 0x63, 0x23, 0x5b, 0x0e, 0xeb, 0x93, 0x42, 0xd1, 0xaa, 0x15, 0x13, 0xcc, 0x29, 0x71, 0x8a, 0xb0, 0xe0, 0xc9, 0x67, 0xde, 0x1a, 0x7c, 0x1a, 0xef, 0xa7, 0x08, 0x85, 0xb3, 0xae, 0x98, 0x99, 0xde, 0xaf, 0x09, 0x38, 0xfc, 0x46, 0x29, 0x5f, 0x4f, 0x7e, 0x01, 0x6c, 0x50, 0x13, 0x95, 0x91, 0x4c, 0x0f, 0x00, 0xba, 0xca, 0x40, 0xa3, 0xd0, 0x58, 0xb6, 0x62, 0x4c, 0xd1, 0xb6, 0xd3, 0x29, 0x5d, 0x82, 0xb3, 0x3d, 0x61, 0xbe, 0x5d, 0xf0, 0x4b, 0xf4}; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(p2 = BN_bin2bn(p2_data, sizeof(p2_data), NULL)) || !TEST_ptr(exp3 = BN_bin2bn(exp3_data, sizeof(exp3_data), NULL)) || !TEST_ptr(coeff2 = BN_bin2bn(coeff2_data, sizeof(coeff2_data), NULL)) || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL)) || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL)) || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2, q)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR3, p2)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1; cdata[0].comparebn = dmp1; cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2; cdata[1].comparebn = dmq1; cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1; cdata[2].comparebn = iqmp; cdata[3].pname = OSSL_PKEY_PARAM_RSA_EXPONENT3; cdata[3].comparebn = exp3; cdata[4].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT2; cdata[4].comparebn = coeff2; cdata[5].pname = OSSL_PKEY_PARAM_RSA_N; cdata[5].comparebn = n; cdata[6].pname = OSSL_PKEY_PARAM_RSA_E; cdata[6].comparebn = e; cdata[7].pname = OSSL_PKEY_PARAM_RSA_D; cdata[7].comparebn = d; cdata[8].pname = OSSL_PKEY_PARAM_RSA_FACTOR1; cdata[8].comparebn = p; cdata[9].pname = OSSL_PKEY_PARAM_RSA_FACTOR2; cdata[9].comparebn = q; cdata[10].pname = OSSL_PKEY_PARAM_RSA_FACTOR3; cdata[10].comparebn = p2; cdata[11].pname = NULL; cdata[11].comparebn = NULL; ret = do_fromdata_rsa_derive(fromdata_params, cdata, 2048, 112, 256); err: BN_free(n); BN_free(e); BN_free(d); BN_free(p); BN_free(p2); BN_free(q); BN_free(dmp1); BN_free(dmq1); BN_free(iqmp); BN_free(exp3); BN_free(coeff2); OSSL_PARAM_BLD_free(bld); return ret; } static int test_evp_pkey_get_bn_param_large(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL; static unsigned char n_data[2050]; static const unsigned char e_data[] = { 0x1, 0x00, 0x01 }; static const unsigned char d_data[] = { 0x99, 0x33, 0x13, 0x7b }; memset(n_data, 0xCE, sizeof(n_data)); if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)) || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1) || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out)) || !TEST_BN_eq(n, n_out)) goto err; ret = 1; err: BN_free(n_out); BN_free(n); BN_free(e); BN_free(d); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(key_ctx); EVP_PKEY_CTX_free(ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } #ifndef OPENSSL_NO_DH static int test_fromdata_dh_named_group(void) { int ret = 0; int gindex = 0, pcounter = 0, hindex = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; size_t len; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; OSSL_PARAM *fromdata_params = NULL; OSSL_PARAM_BLD *bld = NULL; char name_out[80]; unsigned char seed_out[32]; static const unsigned char priv_data[] = { 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, 0x87, 0xe8, 0xa9, 0x7b, }; static const unsigned char pub_data[] = { 0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf, 0x33, 0x42, 0x83, 0x42 }; static const char group_name[] = "ffdhe2048"; static const long priv_len = 224; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN, priv_len)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, NULL, sizeof(name_out), &len)) || !TEST_size_t_eq(len, sizeof(group_name) - 1) || !TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name), &len)) || !TEST_size_t_eq(len, sizeof(group_name) - 1) || !TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name) - 1, &len)) || !TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(group_name) - 2, &len))) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 256) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_str_eq(name_out, group_name) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) || !TEST_ptr(q) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) || !TEST_BN_eq(&ossl_bignum_const_2, g) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j)) || !TEST_ptr_null(j) || !TEST_false(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex)) || !TEST_int_eq(gindex, -1) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) || !TEST_int_eq(hindex, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter)) || !TEST_int_eq(pcounter, -1)) goto err; BN_free(p); p = NULL; BN_free(q); q = NULL; BN_free(g); g = NULL; BN_free(j); j = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("DH", pk) && test_print_key_using_encoder("DH", pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: BN_free(p); BN_free(q); BN_free(g); BN_free(j); BN_free(pub); BN_free(priv); BN_free(pub_out); BN_free(priv_out); EVP_PKEY_free(copy_pk); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } static int test_fromdata_dh_fips186_4(void) { int ret = 0; int gindex = 0, pcounter = 0, hindex = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *dup_pk = NULL; size_t len; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; char name_out[80]; unsigned char seed_out[32]; static const unsigned char priv_data[] = { 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, 0x87, 0xe8, 0xa9, 0x7b, }; static const unsigned char pub_data[] = { 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf, 0x33, 0x42, 0x83, 0x42 }; static const char group_name[] = "ffdhe2048"; static const long priv_len = 224; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0)) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN, priv_len)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 256) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_str_eq(name_out, group_name) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) || !TEST_ptr(q) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) || !TEST_BN_eq(&ossl_bignum_const_2, g) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j)) || !TEST_ptr_null(j) || !TEST_false(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex)) || !TEST_int_eq(gindex, -1) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) || !TEST_int_eq(hindex, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter)) || !TEST_int_eq(pcounter, -1)) goto err; BN_free(p); p = NULL; BN_free(q); q = NULL; BN_free(g); g = NULL; BN_free(j); j = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; ret = test_print_key_using_pem("DH", pk) && test_print_key_using_encoder("DH", pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: BN_free(p); BN_free(q); BN_free(g); BN_free(j); BN_free(pub); BN_free(priv); BN_free(pub_out); BN_free(priv_out); EVP_PKEY_free(pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); return ret; } #endif #ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_ECX # define PRIV_KEY 0 # define PUB_KEY 1 # define X25519_IDX 0 # define X448_IDX 1 # define ED25519_IDX 2 # define ED448_IDX 3 static int test_fromdata_ecx(int tst) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *ctx2 = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; const char *alg = NULL; size_t len; unsigned char out_pub[ED448_KEYLEN]; unsigned char out_priv[ED448_KEYLEN]; OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; static unsigned char key_numbers[4][2][ED448_KEYLEN] = { { { 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a }, { 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a } }, { { 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf, 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba, 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91, 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2, 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b }, { 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22, 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8, 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4, 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f, 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0 } }, { { 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60 }, { 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a } }, { { 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b }, { 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80 } } }; OSSL_PARAM x25519_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[X25519_IDX][PRIV_KEY], X25519_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[X25519_IDX][PUB_KEY], X25519_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM x448_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[X448_IDX][PRIV_KEY], X448_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[X448_IDX][PUB_KEY], X448_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM ed25519_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[ED25519_IDX][PRIV_KEY], ED25519_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[ED25519_IDX][PUB_KEY], ED25519_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM ed448_fromdata_params[] = { OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, key_numbers[ED448_IDX][PRIV_KEY], ED448_KEYLEN), OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, key_numbers[ED448_IDX][PUB_KEY], ED448_KEYLEN), OSSL_PARAM_END }; OSSL_PARAM *fromdata_params = NULL; int bits = 0, security_bits = 0, size = 0; OSSL_PARAM *orig_fromdata_params = NULL; switch (tst & 3) { case X25519_IDX: fromdata_params = x25519_fromdata_params; bits = X25519_BITS; security_bits = X25519_SECURITY_BITS; size = X25519_KEYLEN; alg = "X25519"; break; case X448_IDX: fromdata_params = x448_fromdata_params; bits = X448_BITS; security_bits = X448_SECURITY_BITS; size = X448_KEYLEN; alg = "X448"; break; case ED25519_IDX: fromdata_params = ed25519_fromdata_params; bits = ED25519_BITS; security_bits = ED25519_SECURITY_BITS; size = ED25519_SIGSIZE; alg = "ED25519"; break; case ED448_IDX: fromdata_params = ed448_fromdata_params; bits = ED448_BITS; security_bits = ED448_SECURITY_BITS; size = ED448_SIGSIZE; alg = "ED448"; break; default: goto err; } ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); if (!TEST_ptr(ctx)) goto err; orig_fromdata_params = fromdata_params; if (tst > 7) { fromdata_params++; } else if (tst > 3) { params[0] = fromdata_params[0]; params[1] = fromdata_params[2]; fromdata_params = params; } if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits) || !TEST_int_eq(EVP_PKEY_get_size(pk), size) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, pk, NULL))) goto err; if (tst <= 7) { if (!TEST_int_gt(EVP_PKEY_check(ctx2), 0)) goto err; if (!TEST_true(EVP_PKEY_get_octet_string_param( pk, orig_fromdata_params[PRIV_KEY].key, out_priv, sizeof(out_priv), &len)) || !TEST_mem_eq(out_priv, len, orig_fromdata_params[PRIV_KEY].data, orig_fromdata_params[PRIV_KEY].data_size) || !TEST_true(EVP_PKEY_get_octet_string_param( pk, orig_fromdata_params[PUB_KEY].key, out_pub, sizeof(out_pub), &len)) || !TEST_mem_eq(out_pub, len, orig_fromdata_params[PUB_KEY].data, orig_fromdata_params[PUB_KEY].data_size)) goto err; } else { if (!TEST_int_gt(EVP_PKEY_public_check(ctx2), 0) || !TEST_int_le(EVP_PKEY_private_check(ctx2), 0) || !TEST_int_le(EVP_PKEY_check(ctx2), 0)) goto err; } EVP_PKEY_CTX_free(ctx2); ctx2 = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, copy_pk, NULL)) || !TEST_int_le(EVP_PKEY_public_check(ctx2), 0)) goto err; EVP_PKEY_CTX_free(ctx2); ctx2 = NULL; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (tst > 7) ret = test_print_key_using_encoder_public(alg, pk); else ret = test_print_key_using_pem(alg, pk) && test_print_key_using_encoder(alg, pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(ctx2); return ret; } # endif static int test_fromdata_ec(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; OSSL_PARAM_BLD *bld = NULL; BIGNUM *ec_priv_bn = NULL; BIGNUM *bn_priv = NULL; OSSL_PARAM *fromdata_params = NULL; const char *alg = "EC"; const char *curve = "prime256v1"; const char bad_curve[] = "nonexistent-curve"; OSSL_PARAM nokey_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; static const unsigned char ec_pub_keydata[] = { POINT_CONVERSION_UNCOMPRESSED, 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63, 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d, 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73, 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2, 0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f, 0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d, 0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf, 0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29 }; static const unsigned char ec_pub_keydata_compressed[] = { POINT_CONVERSION_COMPRESSED+1, 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63, 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d, 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73, 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2 }; static const unsigned char ec_priv_keydata[] = { 0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03, 0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59, 0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e, 0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad }; unsigned char out_pub[sizeof(ec_pub_keydata)]; char out_curve_name[80]; const OSSL_PARAM *gettable = NULL; size_t len; EC_GROUP *group = NULL; BIGNUM *group_a = NULL; BIGNUM *group_b = NULL; BIGNUM *group_p = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *p = NULL; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata, sizeof(ec_priv_keydata), NULL))) goto err; if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, curve, 0) <= 0) goto err; if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, ec_pub_keydata_compressed, sizeof(ec_pub_keydata_compressed)) <= 0) goto err; if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0) goto err; if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); if (!TEST_ptr(ctx)) goto err; nokey_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, (char *)bad_curve, sizeof(bad_curve)); if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEY_PARAMETERS, nokey_params), 0) || !TEST_ptr_null(pk)) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128) || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 35 * 2) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PUB_KEY)) || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PRIV_KEY))) goto err; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(OBJ_sn2nid(curve))) || !TEST_ptr(group_p = BN_new()) || !TEST_ptr(group_a = BN_new()) || !TEST_ptr(group_b = BN_new()) || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL))) goto err; if (!TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_A, &a)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_B, &b)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_P, &p))) goto err; if (!TEST_BN_eq(group_p, p) || !TEST_BN_eq(group_a, a) || !TEST_BN_eq(group_b, b)) goto err; EC_GROUP_free(group); group = NULL; BN_free(group_p); group_p = NULL; BN_free(group_a); group_a = NULL; BN_free(group_b); group_b = NULL; if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, out_curve_name, sizeof(out_curve_name), &len) || !TEST_str_eq(out_curve_name, curve) || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY, out_pub, sizeof(out_pub), &len) || !TEST_true(out_pub[0] == POINT_CONVERSION_UNCOMPRESSED) || !TEST_mem_eq(out_pub + 1, len - 1, ec_pub_keydata + 1, sizeof(ec_pub_keydata) - 1) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &bn_priv)) || !TEST_BN_eq(ec_priv_bn, bn_priv)) goto err; BN_free(bn_priv); bn_priv = NULL; ret = test_print_key_using_pem(alg, pk) && test_print_key_using_encoder(alg, pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: EC_GROUP_free(group); BN_free(group_a); BN_free(group_b); BN_free(group_p); BN_free(a); BN_free(b); BN_free(p); BN_free(bn_priv); BN_free(ec_priv_bn); OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); return ret; } static int test_ec_dup_no_operation(void) { int ret = 0; EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL; EVP_PKEY *param = NULL, *pkey = NULL; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1), 0) || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0) || !TEST_ptr(param)) goto err; EVP_PKEY_CTX_free(pctx); pctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL)) || !TEST_ptr(kctx = EVP_PKEY_CTX_dup(ctx)) || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0) || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(param); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(pctx); return ret; } static int test_ec_dup_keygen_operation(void) { int ret = 0; EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL; EVP_PKEY *param = NULL, *pkey = NULL; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1), 0) || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0) || !TEST_ptr(param)) goto err; EVP_PKEY_CTX_free(pctx); pctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) || !TEST_ptr_null(kctx = EVP_PKEY_CTX_dup(ctx))) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(param); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(kctx); EVP_PKEY_CTX_free(pctx); return ret; } #endif #ifndef OPENSSL_NO_DSA static int test_fromdata_dsa_fips186_4(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL; BIGNUM *pub = NULL, *priv = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL; BIGNUM *pub_out = NULL, *priv_out = NULL; BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL; int gindex_out = 0, pcounter_out = 0, hindex_out = 0; char name_out[80]; unsigned char seed_out[32]; size_t len; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *fromdata_params = NULL; static const unsigned char p_data[] = { 0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b, 0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21, 0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8, 0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7, 0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc, 0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4, 0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23, 0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64, 0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e, 0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73, 0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a, 0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6, 0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde, 0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd, 0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c, 0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71, 0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18, 0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56, 0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85, 0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c, 0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79, 0x7c, 0xfe, 0xaf, 0x6a, 0x05 }; static const unsigned char q_data[] = { 0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04, 0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40, 0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7 }; static const unsigned char g_data[] = { 0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b, 0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b, 0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4, 0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6, 0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa, 0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57, 0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72, 0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c, 0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60, 0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1, 0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35, 0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e, 0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05, 0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25, 0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb, 0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a, 0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25, 0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0, 0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd, 0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53, 0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62, 0x02, 0x6f, 0x96, 0x36 }; static const unsigned char seed_data[] = { 0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62, 0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04, 0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84 }; const int gindex = 1; const int pcounter = 53; static const unsigned char priv_data[] = { 0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73, 0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8, 0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90 }; static const unsigned char pub_data[] = { 0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29, 0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80, 0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02, 0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73, 0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf, 0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda, 0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda, 0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47, 0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08, 0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1, 0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2, 0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b, 0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b, 0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3, 0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05, 0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4, 0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27, 0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6, 0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49, 0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a, 0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60, 0x48, 0xd1, 0x8a, 0xbd }; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_FFC_SEED, seed_data, sizeof(seed_data))) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX, gindex)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_PCOUNTER, pcounter)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR, fromdata_params), 1)) goto err; for (;;) { ret = 0; if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048) || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112) || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 2 * (3 + sizeof(q_data))) || !TEST_false(EVP_PKEY_missing_parameters(pk))) goto err; if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, name_out, sizeof(name_out), &len)) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, &pub_out)) || !TEST_BN_eq(pub, pub_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, &priv_out)) || !TEST_BN_eq(priv, priv_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p_out)) || !TEST_BN_eq(p, p_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q_out)) || !TEST_BN_eq(q, q_out) || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g_out)) || !TEST_BN_eq(g, g_out) || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, &j_out)) || !TEST_ptr_null(j_out) || !TEST_true(EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_FFC_SEED, seed_out, sizeof(seed_out), &len)) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, &gindex_out)) || !TEST_int_eq(gindex, gindex_out) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex_out)) || !TEST_int_eq(hindex_out, 0) || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, &pcounter_out)) || !TEST_int_eq(pcounter, pcounter_out)) goto err; BN_free(p_out); p_out = NULL; BN_free(q_out); q_out = NULL; BN_free(g_out); g_out = NULL; BN_free(j_out); j_out = NULL; BN_free(pub_out); pub_out = NULL; BN_free(priv_out); priv_out = NULL; if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) goto err; if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0) || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0)) goto err; EVP_PKEY_CTX_free(key_ctx); key_ctx = NULL; if (!TEST_ptr(copy_pk = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) goto err; EVP_PKEY_free(copy_pk); copy_pk = NULL; ret = test_print_key_using_pem("DSA", pk) && test_print_key_using_encoder("DSA", pk); if (!ret || dup_pk != NULL) break; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk))) goto err; ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1); EVP_PKEY_free(pk); pk = dup_pk; if (!ret) goto err; } err: OSSL_PARAM_free(fromdata_params); OSSL_PARAM_BLD_free(bld); BN_free(p); BN_free(q); BN_free(g); BN_free(pub); BN_free(priv); BN_free(p_out); BN_free(q_out); BN_free(g_out); BN_free(pub_out); BN_free(priv_out); BN_free(j_out); EVP_PKEY_free(pk); EVP_PKEY_free(copy_pk); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(key_ctx); return ret; } static int test_check_dsa(void) { int ret = 0; EVP_PKEY_CTX *ctx = NULL; if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) || !TEST_int_le(EVP_PKEY_check(ctx), 0) || !TEST_int_le(EVP_PKEY_public_check(ctx), 0) || !TEST_int_le(EVP_PKEY_private_check(ctx), 0) || !TEST_int_le(EVP_PKEY_pairwise_check(ctx), 0)) goto err; ret = 1; err: EVP_PKEY_CTX_free(ctx); return ret; } #endif static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key, size_t keylen, char *salt) { OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5); OSSL_PARAM *p = params; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, strlen(salt)); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (unsigned char *)key, keylen); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, "EXTRACT_ONLY", 0); *p = OSSL_PARAM_construct_end(); return params; } static int test_evp_pkey_ctx_dup_kdf(void) { int ret = 0; size_t len = 0, dlen = 0; EVP_PKEY_CTX *pctx = NULL, *dctx = NULL; OSSL_PARAM *params = NULL; if (!TEST_ptr(params = do_construct_hkdf_params("sha256", "secret", 6, "salt"))) goto err; if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "HKDF", NULL))) goto err; if (!TEST_int_eq(EVP_PKEY_derive_init_ex(pctx, params), 1)) goto err; if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(pctx))) goto err; if (!TEST_int_eq(EVP_PKEY_derive(pctx, NULL, &len), 1) || !TEST_size_t_eq(len, SHA256_DIGEST_LENGTH) || !TEST_int_eq(EVP_PKEY_derive(dctx, NULL, &dlen), 1) || !TEST_size_t_eq(dlen, SHA256_DIGEST_LENGTH)) goto err; ret = 1; err: OPENSSL_free(params); EVP_PKEY_CTX_free(dctx); EVP_PKEY_CTX_free(pctx); return ret; } int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(datadir = test_get_argument(0))) return 0; ADD_TEST(test_evp_pkey_ctx_dup_kdf); ADD_TEST(test_evp_pkey_get_bn_param_large); ADD_TEST(test_fromdata_rsa); ADD_TEST(test_fromdata_rsa_derive_from_pq_sp800); ADD_TEST(test_fromdata_rsa_derive_from_pq_multiprime); #ifndef OPENSSL_NO_DH ADD_TEST(test_fromdata_dh_fips186_4); ADD_TEST(test_fromdata_dh_named_group); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_check_dsa); ADD_TEST(test_fromdata_dsa_fips186_4); #endif #ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_ECX ADD_ALL_TESTS(test_fromdata_ecx, 4 * 3); # endif ADD_TEST(test_fromdata_ec); ADD_TEST(test_ec_dup_no_operation); ADD_TEST(test_ec_dup_keygen_operation); #endif return 1; }
test
openssl/test/evp_pkey_provided_test.c
openssl
#include <stdlib.h> #include <string.h> #include <openssl/conf.h> #include <openssl/err.h> #include "testutil.h" #ifdef _WIN32 # include <direct.h> # define DIRSEP "/\\" # ifndef __BORLANDC__ # define chdir _chdir # endif # define DIRSEP_PRESERVE 0 #elif !defined(OPENSSL_NO_POSIX_IO) # include <unistd.h> # ifndef OPENSSL_SYS_VMS # define DIRSEP "/" # define DIRSEP_PRESERVE 0 # else # define DIRSEP "/]:" # define DIRSEP_PRESERVE 1 # endif #else # define chdir(x) (-1); # define DIRSEP "/" # define DIRSEP_PRESERVE 0 #endif static char *change_path(const char *file) { char *s = OPENSSL_strdup(file); char *p = s; char *last = NULL; int ret = 0; char *new_config_name = NULL; if (s == NULL) return NULL; while ((p = strpbrk(p, DIRSEP)) != NULL) { last = p++; } if (last == NULL) goto err; last[DIRSEP_PRESERVE] = 0; TEST_note("changing path to %s", s); ret = chdir(s); if (ret == 0) new_config_name = strdup(last + DIRSEP_PRESERVE + 1); err: OPENSSL_free(s); return new_config_name; } static CONF *conf; static BIO *in; static int expect_failure = 0; static int test_providers = 0; static OSSL_LIB_CTX *libctx = NULL; static char *rel_conf_file = NULL; static int test_load_config(void) { long errline; long val; char *str; long err; if (!TEST_int_gt(NCONF_load_bio(conf, in, &errline), 0) || !TEST_int_eq(err = ERR_peek_error(), 0)) { if (expect_failure) return 1; TEST_note("Failure loading the configuration at line %ld", errline); return 0; } if (expect_failure) { TEST_note("Failure expected but did not happen"); return 0; } if (!TEST_int_gt(CONF_modules_load(conf, NULL, 0), 0)) { TEST_note("Failed in CONF_modules_load"); return 0; } val = 0; if (!TEST_int_eq(NCONF_get_number(conf, "CA_default", "default_days", &val), 1) || !TEST_int_eq(val, 365)) { TEST_note("default_days incorrect"); return 0; } val = 0; if (!TEST_int_eq(NCONF_get_number(conf, "req", "default_bits", &val), 1) || !TEST_int_eq(val, 2048)) { TEST_note("default_bits incorrect"); return 0; } str = NCONF_get_string(conf, "req_distinguished_name", "countryName_default"); if (!TEST_ptr(str) || !TEST_str_eq(str, "AU")) { TEST_note("countryName_default incorrect"); return 0; } if (test_providers != 0) { val = 0; if (!TEST_int_eq(NCONF_get_number(conf, "null_sect", "activate", &val), 1) || !TEST_int_eq(val, 1)) { TEST_note("null provider not activated"); return 0; } val = 0; if (!TEST_int_eq(NCONF_get_number(conf, "default_sect", "activate", &val), 1) || !TEST_int_eq(val, 1)) { TEST_note("default provider not activated"); return 0; } val = 0; if (!TEST_int_eq(NCONF_get_number(conf, "legacy_sect", "activate", &val), 1) || !TEST_int_eq(val, 1)) { TEST_note("legacy provider not activated"); return 0; } } return 1; } static int test_check_null_numbers(void) { #if defined(_BSD_SOURCE) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \ || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) long val = 0; if (!TEST_int_eq(setenv("FNORD", "123", 1), 0) || !TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val)) || !TEST_long_eq(val, 123)) { TEST_note("environment variable with NULL conf failed"); return 0; } if (!TEST_int_eq(unsetenv("FNORD"), 0) || !TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) { TEST_note("missing environment variable with NULL conf failed"); return 0; } #endif return 1; } static int test_check_overflow(void) { #if defined(_BSD_SOURCE) \ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) \ || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) long val = 0; char max[(sizeof(long) * 8) / 3 + 3]; char *p; p = max + sprintf(max, "0%ld", LONG_MAX) - 1; setenv("FNORD", max, 1); if (!TEST_true(NCONF_get_number(NULL, "missing", "FNORD", &val)) || !TEST_long_eq(val, LONG_MAX)) return 0; while (++*p > '9') *p-- = '0'; setenv("FNORD", max, 1); if (!TEST_false(NCONF_get_number(NULL, "missing", "FNORD", &val))) return 0; #endif return 1; } static int test_available_providers(void) { libctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(libctx)) return 0; if (!TEST_ptr(rel_conf_file) || !OSSL_LIB_CTX_load_config(libctx, rel_conf_file)) { TEST_note("Failed to load config"); return 0; } if (OSSL_PROVIDER_available(libctx, "default") != 1) { TEST_note("Default provider is missing"); return 0; } if (OSSL_PROVIDER_available(libctx, "legacy") != 1) { TEST_note("Legacy provider is missing"); return 0; } return 1; } typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_FAIL, OPT_TEST_PROV, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"), { "f", OPT_FAIL, '-', "A failure is expected" }, { "providers", OPT_TEST_PROV, '-', "Test for activated default and legacy providers"}, { NULL } }; return test_options; } int setup_tests(void) { char *conf_file = NULL; OPTION_CHOICE o; if (!TEST_ptr(conf = NCONF_new(NULL))) return 0; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_FAIL: expect_failure = 1; break; case OPT_TEST_PROV: test_providers = 1; case OPT_TEST_CASES: break; default: return 0; } } conf_file = test_get_argument(0); if (!TEST_ptr(conf_file) || !TEST_ptr(in = BIO_new_file(conf_file, "r"))) { TEST_note("Unable to open the file argument"); return 0; } rel_conf_file = change_path(conf_file); if (!TEST_ptr(rel_conf_file)) { TEST_note("Unable to change path"); return 0; } ADD_TEST(test_load_config); ADD_TEST(test_check_null_numbers); ADD_TEST(test_check_overflow); if (test_providers != 0) ADD_TEST(test_available_providers); return 1; } void cleanup_tests(void) { OPENSSL_free(rel_conf_file); BIO_vfree(in); NCONF_free(conf); CONF_modules_unload(1); }
test
openssl/test/conf_include_test.c
openssl
#include <stdio.h> #include <stdlib.h> #include <openssl/e_os2.h> #ifdef OPENSSL_SYS_UNIX # include <sys/stat.h> # include <sys/resource.h> # include <openssl/pem.h> # include <openssl/x509.h> # include <openssl/err.h> # include <openssl/bio.h> # include "internal/e_os.h" # if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L # ifndef timersub # define timersub(a, b, res) \ do { \ (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ if ((a)->tv_usec < (b)->tv_usec) { \ (res)->tv_usec = (a)->tv_usec + 1000000 - (b)->tv_usec; \ --(res)->tv_sec; \ } else { \ (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ } \ } while(0) # endif static char *prog; static void readx509(const char *contents, int size) { X509 *x = NULL; BIO *b = BIO_new_mem_buf(contents, size); if (b == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } PEM_read_bio_X509(b, &x, 0, NULL); if (x == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } X509_free(x); BIO_free(b); } static void readpkey(const char *contents, int size) { BIO *b = BIO_new_mem_buf(contents, size); EVP_PKEY *pkey; if (b == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL); if (pkey == NULL) { ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } EVP_PKEY_free(pkey); BIO_free(b); } static void print_timeval(const char *what, struct timeval *tp) { printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec); } static void usage(void) { fprintf(stderr, "Usage: %s [flags] pem-file\n", prog); fprintf(stderr, "Flags, with the default being '-wc':\n"); fprintf(stderr, " -c # Repeat count\n"); fprintf(stderr, " -d Debugging output (minimal)\n"); fprintf(stderr, " -w<T> What to load T is a single character:\n"); fprintf(stderr, " c for cert\n"); fprintf(stderr, " p for private key\n"); exit(EXIT_FAILURE); } # endif #endif int main(int ac, char **av) { #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L int i, debug = 0, count = 100, what = 'c'; struct stat sb; FILE *fp; char *contents; struct rusage start, end, elapsed; struct timeval e_start, e_end, e_elapsed; prog = av[0]; while ((i = getopt(ac, av, "c:dw:")) != EOF) { switch (i) { default: usage(); break; case 'c': if ((count = atoi(optarg)) < 0) usage(); break; case 'd': debug = 1; break; case 'w': if (optarg[1] != '\0') usage(); switch (*optarg) { default: usage(); break; case 'c': case 'p': what = *optarg; break; } break; } } ac -= optind; av += optind; if (av[0] == NULL) usage(); if (stat(av[0], &sb) < 0) { perror(av[0]); exit(EXIT_FAILURE); } contents = OPENSSL_malloc(sb.st_size + 1); if (contents == NULL) { perror("malloc"); exit(EXIT_FAILURE); } fp = fopen(av[0], "r"); if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) { perror("fread"); exit(EXIT_FAILURE); } contents[sb.st_size] = '\0'; fclose(fp); if (debug) printf(">%s<\n", contents); for (i = 10; i > 0; i--) { switch (what) { case 'c': readx509(contents, (int)sb.st_size); break; case 'p': readpkey(contents, (int)sb.st_size); break; } } if (gettimeofday(&e_start, NULL) < 0) { perror("elapsed start"); exit(EXIT_FAILURE); } if (getrusage(RUSAGE_SELF, &start) < 0) { perror("start"); exit(EXIT_FAILURE); } for (i = count; i > 0; i--) { switch (what) { case 'c': readx509(contents, (int)sb.st_size); break; case 'p': readpkey(contents, (int)sb.st_size); break; } } if (getrusage(RUSAGE_SELF, &end) < 0) { perror("getrusage"); exit(EXIT_FAILURE); } if (gettimeofday(&e_end, NULL) < 0) { perror("gettimeofday"); exit(EXIT_FAILURE); } timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime); timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime); timersub(&e_end, &e_start, &e_elapsed); print_timeval("user ", &elapsed.ru_utime); print_timeval("sys ", &elapsed.ru_stime); if (debug) print_timeval("elapsed??", &e_elapsed); OPENSSL_free(contents); return EXIT_SUCCESS; #else fprintf(stderr, "This tool is not supported on this platform for lack of POSIX1.2001 support\n"); exit(EXIT_FAILURE); #endif }
test
openssl/test/timing_load_creds.c
openssl
#include <openssl/evp.h> #include "testutil.h" static char *config_file = NULL; typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { NULL } }; return options; } static int test_asn1_stable_parse(void) { int testret = 0; unsigned long errcode; OSSL_LIB_CTX *newctx = OSSL_LIB_CTX_new(); if (!TEST_ptr(newctx)) goto out; if (!TEST_int_eq(OSSL_LIB_CTX_load_config(newctx, config_file), 0)) goto err; errcode = ERR_peek_error(); if (ERR_GET_LIB(errcode) != ERR_LIB_ASN1) goto err; if (ERR_GET_REASON(errcode) != ASN1_R_INVALID_STRING_TABLE_VALUE) goto err; ERR_clear_error(); testret = 1; err: OSSL_LIB_CTX_free(newctx); out: return testret; } int setup_tests(void) { OPTION_CHOICE o; while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_CONFIG_FILE: config_file = opt_arg(); break; default: return 0; } } ADD_TEST(test_asn1_stable_parse); return 1; }
test
openssl/test/asn1_stable_parse_test.c
openssl
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/opensslv.h> #include <openssl/ssl.h> #include <openssl/types.h> #include "simpledynamic.h" typedef void DSO; typedef const SSL_METHOD * (*TLS_method_t)(void); typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth); typedef void (*SSL_CTX_free_t)(SSL_CTX *); typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *); typedef int (*OPENSSL_atexit_t)(void (*handler)(void)); typedef unsigned long (*ERR_get_error_t)(void); typedef unsigned long (*OPENSSL_version_major_t)(void); typedef unsigned long (*OPENSSL_version_minor_t)(void); typedef unsigned long (*OPENSSL_version_patch_t)(void); typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags); typedef int (*DSO_free_t)(DSO *dso); typedef enum test_types_en { CRYPTO_FIRST, SSL_FIRST, JUST_CRYPTO, DSO_REFTEST, NO_ATEXIT } TEST_TYPE; static TEST_TYPE test_type; static const char *path_crypto; static const char *path_ssl; static const char *path_atexit; #ifdef SD_INIT static int atexit_handler_done = 0; static void atexit_handler(void) { FILE *atexit_file = fopen(path_atexit, "w"); if (atexit_file == NULL) return; fprintf(atexit_file, "atexit() run\n"); fclose(atexit_file); atexit_handler_done++; } static int test_lib(void) { SD ssllib = SD_INIT; SD cryptolib = SD_INIT; SSL_CTX *ctx; union { void (*func)(void); SD_SYM sym; } symbols[5]; TLS_method_t myTLS_method; SSL_CTX_new_t mySSL_CTX_new; SSL_CTX_free_t mySSL_CTX_free; ERR_get_error_t myERR_get_error; OPENSSL_version_major_t myOPENSSL_version_major; OPENSSL_version_minor_t myOPENSSL_version_minor; OPENSSL_version_patch_t myOPENSSL_version_patch; OPENSSL_atexit_t myOPENSSL_atexit; int result = 0; switch (test_type) { case JUST_CRYPTO: case DSO_REFTEST: case NO_ATEXIT: case CRYPTO_FIRST: if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { fprintf(stderr, "Failed to load libcrypto\n"); goto end; } if (test_type != CRYPTO_FIRST) break; case SSL_FIRST: if (!sd_load(path_ssl, &ssllib, SD_SHLIB)) { fprintf(stderr, "Failed to load libssl\n"); goto end; } if (test_type != SSL_FIRST) break; if (!sd_load(path_crypto, &cryptolib, SD_SHLIB)) { fprintf(stderr, "Failed to load libcrypto\n"); goto end; } break; } if (test_type == NO_ATEXIT) { OPENSSL_init_crypto_t myOPENSSL_init_crypto; if (!sd_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) { fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n"); goto end; } myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func; if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) { fprintf(stderr, "Failed to initialise libcrypto\n"); goto end; } } if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST && test_type != NO_ATEXIT) { if (!sd_sym(ssllib, "TLS_method", &symbols[0].sym) || !sd_sym(ssllib, "SSL_CTX_new", &symbols[1].sym) || !sd_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) { fprintf(stderr, "Failed to load libssl symbols\n"); goto end; } myTLS_method = (TLS_method_t)symbols[0].func; mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func; mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func; ctx = mySSL_CTX_new(myTLS_method()); if (ctx == NULL) { fprintf(stderr, "Failed to create SSL_CTX\n"); goto end; } mySSL_CTX_free(ctx); } if (!sd_sym(cryptolib, "ERR_get_error", &symbols[0].sym) || !sd_sym(cryptolib, "OPENSSL_version_major", &symbols[1].sym) || !sd_sym(cryptolib, "OPENSSL_version_minor", &symbols[2].sym) || !sd_sym(cryptolib, "OPENSSL_version_patch", &symbols[3].sym) || !sd_sym(cryptolib, "OPENSSL_atexit", &symbols[4].sym)) { fprintf(stderr, "Failed to load libcrypto symbols\n"); goto end; } myERR_get_error = (ERR_get_error_t)symbols[0].func; if (myERR_get_error() != 0) { fprintf(stderr, "Unexpected ERR_get_error() response\n"); goto end; } myOPENSSL_version_major = (OPENSSL_version_major_t)symbols[1].func; myOPENSSL_version_minor = (OPENSSL_version_minor_t)symbols[2].func; myOPENSSL_version_patch = (OPENSSL_version_patch_t)symbols[3].func; if (myOPENSSL_version_major() != OPENSSL_VERSION_MAJOR || myOPENSSL_version_minor() != OPENSSL_VERSION_MINOR || myOPENSSL_version_patch() != OPENSSL_VERSION_PATCH) { fprintf(stderr, "Invalid library version number\n"); goto end; } myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[4].func; if (!myOPENSSL_atexit(atexit_handler)) { fprintf(stderr, "Failed to register atexit handler\n"); goto end; } if (test_type == DSO_REFTEST) { # ifdef DSO_DLFCN DSO_dsobyaddr_t myDSO_dsobyaddr; DSO_free_t myDSO_free; if (!sd_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym) || !sd_sym(cryptolib, "DSO_free", &symbols[1].sym)) { fprintf(stderr, "Unable to load DSO symbols\n"); goto end; } myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func; myDSO_free = (DSO_free_t)symbols[1].func; { DSO *hndl; hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0); if (hndl == NULL) { fprintf(stderr, "DSO_dsobyaddr() failed\n"); goto end; } myDSO_free(hndl); } # endif } if (!sd_close(cryptolib)) { fprintf(stderr, "Failed to close libcrypto\n"); goto end; } cryptolib = SD_INIT; if (test_type == CRYPTO_FIRST || test_type == SSL_FIRST) { if (!sd_close(ssllib)) { fprintf(stderr, "Failed to close libssl\n"); goto end; } ssllib = SD_INIT; } # if defined(OPENSSL_NO_PINSHARED) \ && defined(__GLIBC__) \ && defined(__GLIBC_PREREQ) \ && defined(OPENSSL_SYS_LINUX) # if __GLIBC_PREREQ(2, 3) if (test_type != NO_ATEXIT && atexit_handler_done != 1) { fprintf(stderr, "atexit() handler did not run\n"); goto end; } # endif # endif result = 1; end: if (cryptolib != SD_INIT) sd_close(cryptolib); if (ssllib != SD_INIT) sd_close(ssllib); return result; } #endif int main(int argc, char *argv[]) { const char *p; if (argc != 5) { fprintf(stderr, "Incorrect number of arguments\n"); return 1; } p = argv[1]; if (strcmp(p, "-crypto_first") == 0) { test_type = CRYPTO_FIRST; } else if (strcmp(p, "-ssl_first") == 0) { test_type = SSL_FIRST; } else if (strcmp(p, "-just_crypto") == 0) { test_type = JUST_CRYPTO; } else if (strcmp(p, "-dso_ref") == 0) { test_type = DSO_REFTEST; } else if (strcmp(p, "-no_atexit") == 0) { test_type = NO_ATEXIT; } else { fprintf(stderr, "Unrecognised argument\n"); return 1; } path_crypto = argv[2]; path_ssl = argv[3]; path_atexit = argv[4]; if (path_crypto == NULL || path_ssl == NULL) { fprintf(stderr, "Invalid libcrypto/libssl path\n"); return 1; } #ifdef SD_INIT if (!test_lib()) return 1; #endif return 0; }
test
openssl/test/shlibloadtest.c
openssl
#include <stdio.h> #include <openssl/opensslconf.h> #include <string.h> #include <openssl/evp.h> #include <openssl/ssl.h> #include <openssl/tls1.h> #include "testutil.h" static SSL_CTX *ctx; static int test_func(void) { if (!TEST_int_eq(SSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION) && !TEST_int_eq(SSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION)) { TEST_info("min/max version setting incorrect"); return 0; } return 1; } int global_init(void) { if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN | OPENSSL_INIT_LOAD_CONFIG, NULL)) return 0; return 1; } int setup_tests(void) { if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))) return 0; ADD_TEST(test_func); return 1; } void cleanup_tests(void) { SSL_CTX_free(ctx); }
test
openssl/test/sysdefaulttest.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <string.h> #include <openssl/bio.h> #include "testutil.h" #define MAXCOUNT 5 static int my_param_count; static BIO *my_param_b[MAXCOUNT]; static int my_param_oper[MAXCOUNT]; static const char *my_param_argp[MAXCOUNT]; static int my_param_argi[MAXCOUNT]; static long my_param_argl[MAXCOUNT]; static long my_param_ret[MAXCOUNT]; static size_t my_param_len[MAXCOUNT]; static size_t my_param_processed[MAXCOUNT]; static long my_bio_cb_ex(BIO *b, int oper, const char *argp, size_t len, int argi, long argl, int ret, size_t *processed) { if (my_param_count >= MAXCOUNT) return -1; my_param_b[my_param_count] = b; my_param_oper[my_param_count] = oper; my_param_argp[my_param_count] = argp; my_param_argi[my_param_count] = argi; my_param_argl[my_param_count] = argl; my_param_ret[my_param_count] = ret; my_param_len[my_param_count] = len; my_param_processed[my_param_count] = processed != NULL ? *processed : 0; my_param_count++; return ret; } static int test_bio_callback_ex(void) { int ok = 0; BIO *bio; int i; char test1[] = "test"; const size_t test1len = sizeof(test1) - 1; char test2[] = "hello"; const size_t test2len = sizeof(test2) - 1; char buf[16]; my_param_count = 0; bio = BIO_new(BIO_s_mem()); if (bio == NULL) goto err; BIO_set_callback_ex(bio, my_bio_cb_ex); i = BIO_write(bio, test1, test1len); if (!TEST_int_eq(i, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE) || !TEST_ptr_eq(my_param_argp[0], test1) || !TEST_size_t_eq(my_param_len[0], test1len) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test1) || !TEST_size_t_eq(my_param_len[1], test1len) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test1len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_mem_eq(buf, i, test1, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test1len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, -1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], 0) || !TEST_int_eq((int)my_param_ret[1], -1)) goto err; my_param_count = 0; i = BIO_set_mem_eof_return(bio, 0); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_CTRL) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], BIO_C_SET_BUF_MEM_EOF_RETURN) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_CTRL | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], NULL) || !TEST_int_eq(my_param_argi[1], BIO_C_SET_BUF_MEM_EOF_RETURN) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, 0) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_size_t_eq(my_param_len[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_size_t_eq(my_param_len[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], 0) || !TEST_int_eq((int)my_param_ret[1], 0)) goto err; my_param_count = 0; i = BIO_puts(bio, test2); if (!TEST_int_eq(i, 5) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS) || !TEST_ptr_eq(my_param_argp[0], test2) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test2) || !TEST_int_eq(my_param_argi[1], 0) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_size_t_eq(my_param_processed[1], test2len) || !TEST_int_eq((int)my_param_ret[1], 1)) goto err; my_param_count = 0; i = BIO_free(bio); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 1) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_int_eq((int)my_param_ret[0], 1)) goto finish; ok = 1; goto finish; err: BIO_free(bio); finish: memset(my_param_b, 0, sizeof(my_param_b)); memset(my_param_argp, 0, sizeof(my_param_argp)); return ok; } #ifndef OPENSSL_NO_DEPRECATED_3_0 static long my_bio_callback(BIO *b, int oper, const char *argp, int argi, long argl, long ret) { if (my_param_count >= MAXCOUNT) return -1; my_param_b[my_param_count] = b; my_param_oper[my_param_count] = oper; my_param_argp[my_param_count] = argp; my_param_argi[my_param_count] = argi; my_param_argl[my_param_count] = argl; my_param_ret[my_param_count] = ret; my_param_count++; return ret; } static int test_bio_callback(void) { int ok = 0; BIO *bio; int i; char test1[] = "test"; const int test1len = sizeof(test1) - 1; char test2[] = "hello"; const int test2len = sizeof(test2) - 1; char buf[16]; my_param_count = 0; bio = BIO_new(BIO_s_mem()); if (bio == NULL) goto err; BIO_set_callback(bio, my_bio_callback); i = BIO_write(bio, test1, test1len); if (!TEST_int_eq(i, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE) || !TEST_ptr_eq(my_param_argp[0], test1) || !TEST_int_eq(my_param_argi[0], test1len) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test1) || !TEST_int_eq(my_param_argi[1], test1len) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test1len)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_mem_eq(buf, i, test1, test1len) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test1len)) goto err; my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, -1) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], -1L)) goto err; BIO_set_mem_eof_return(bio, 0); my_param_count = 0; i = BIO_read(bio, buf, sizeof(buf)); if (!TEST_int_eq(i, 0) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_READ) || !TEST_ptr_eq(my_param_argp[0], buf) || !TEST_int_eq(my_param_argi[0], sizeof(buf)) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], buf) || !TEST_int_eq(my_param_argi[1], sizeof(buf)) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], 0L)) goto err; my_param_count = 0; i = BIO_puts(bio, test2); if (!TEST_int_eq(i, 5) || !TEST_int_eq(my_param_count, 2) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS) || !TEST_ptr_eq(my_param_argp[0], test2) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L) || !TEST_ptr_eq(my_param_b[1], bio) || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN) || !TEST_ptr_eq(my_param_argp[1], test2) || !TEST_int_eq(my_param_argi[1], 0) || !TEST_long_eq(my_param_argl[1], 0L) || !TEST_long_eq(my_param_ret[1], (long)test2len)) goto err; my_param_count = 0; i = BIO_free(bio); if (!TEST_int_eq(i, 1) || !TEST_int_eq(my_param_count, 1) || !TEST_ptr_eq(my_param_b[0], bio) || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE) || !TEST_ptr_eq(my_param_argp[0], NULL) || !TEST_int_eq(my_param_argi[0], 0) || !TEST_long_eq(my_param_argl[0], 0L) || !TEST_long_eq(my_param_ret[0], 1L)) goto finish; ok = 1; goto finish; err: BIO_free(bio); finish: memset(my_param_b, 0, sizeof(my_param_b)); memset(my_param_argp, 0, sizeof(my_param_argp)); return ok; } #endif int setup_tests(void) { ADD_TEST(test_bio_callback_ex); #ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_bio_callback); #endif return 1; }
test
openssl/test/bio_callback_test.c
openssl
#include <openssl/evp.h> #include <openssl/conf.h> #include "testutil.h" static char *configfile = NULL; static char *recurseconfigfile = NULL; static int test_double_config(void) { OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); int testresult = 0; EVP_MD *sha256 = NULL; if (!TEST_ptr(configfile)) return 0; if (!TEST_ptr(ctx)) return 0; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) return 0; if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) return 0; sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL); if (!TEST_ptr(sha256)) goto err; testresult = 1; err: EVP_MD_free(sha256); OSSL_LIB_CTX_free(ctx); return testresult; } static int test_recursive_config(void) { OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); int testresult = 0; unsigned long err; if (!TEST_ptr(recurseconfigfile)) goto err; if (!TEST_ptr(ctx)) goto err; if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile))) goto err; err = ERR_peek_error(); if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE) testresult = 1; err: OSSL_LIB_CTX_free(ctx); return testresult; } OPT_TEST_DECLARE_USAGE("configfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(configfile = test_get_argument(0))) return 0; if (!TEST_ptr(recurseconfigfile = test_get_argument(1))) return 0; ADD_TEST(test_recursive_config); ADD_TEST(test_double_config); return 1; }
test
openssl/test/prov_config_test.c
openssl
#include "internal/deprecated.h" #include <openssl/opensslconf.h> #include "testutil.h" #ifndef OPENSSL_NO_EC # include <openssl/evp.h> # include <openssl/bn.h> # include <openssl/ec.h> # include <openssl/rand.h> # include "internal/nelem.h" # include "ecdsatest.h" static fake_random_generate_cb fbytes; static const char *numbers[2]; static size_t crv_len = 0; static EC_builtin_curve *curves = NULL; static OSSL_PROVIDER *fake_rand = NULL; static int fbytes(unsigned char *buf, size_t num, ossl_unused const char *name, EVP_RAND_CTX *ctx) { int ret = 0; static int fbytes_counter = 0; BIGNUM *tmp = NULL; fake_rand_set_callback(ctx, NULL); if (!TEST_ptr(tmp = BN_new()) || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers)) || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter])) || !TEST_int_le(BN_num_bytes(tmp), num) || !TEST_int_gt(BN_bn2binpad(tmp, buf, num), 0)) goto err; fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers); ret = 1; err: BN_free(tmp); return ret; } static int x9_62_tests(int n) { int nid, md_nid, ret = 0; const char *r_in = NULL, *s_in = NULL, *tbs = NULL; unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL; unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int dgst_len = 0; long q_len, msg_len = 0; size_t p_len; EVP_MD_CTX *mctx = NULL; EC_KEY *key = NULL; ECDSA_SIG *signature = NULL; BIGNUM *r = NULL, *s = NULL; BIGNUM *kinv = NULL, *rp = NULL; const BIGNUM *sig_r = NULL, *sig_s = NULL; nid = ecdsa_cavs_kats[n].nid; md_nid = ecdsa_cavs_kats[n].md_nid; r_in = ecdsa_cavs_kats[n].r; s_in = ecdsa_cavs_kats[n].s; tbs = ecdsa_cavs_kats[n].msg; numbers[0] = ecdsa_cavs_kats[n].d; numbers[1] = ecdsa_cavs_kats[n].k; TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid)); #ifdef FIPS_MODULE if (EC_curve_nid2nist(nid) == NULL) return TEST_skip("skip non approved curves"); #endif if (!TEST_ptr(mctx = EVP_MD_CTX_new()) || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len)) || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL)) || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len)) || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len)) || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid)) || !TEST_ptr(r = BN_new()) || !TEST_ptr(s = BN_new()) || !TEST_true(BN_hex2bn(&r, r_in)) || !TEST_true(BN_hex2bn(&s, s_in))) goto err; fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); if (!TEST_true(EC_KEY_generate_key(key)) || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED, &pbuf, NULL)) || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len)) || !TEST_int_eq(q_len, p_len) || !TEST_mem_eq(qbuf, q_len, pbuf, p_len)) goto err; fake_rand_set_callback(RAND_get0_private(NULL), &fbytes); if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)) || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len, kinv, rp, key)) || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1)) goto err; ECDSA_SIG_get0(signature, &sig_r, &sig_s); if (!TEST_BN_eq(sig_r, r) || !TEST_BN_eq(sig_s, s)) goto err; ret = 1; err: OPENSSL_free(message); OPENSSL_free(pbuf); OPENSSL_free(qbuf); EC_KEY_free(key); ECDSA_SIG_free(signature); BN_free(r); BN_free(s); EVP_MD_CTX_free(mctx); BN_clear_free(kinv); BN_clear_free(rp); return ret; } static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey) { static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' }; EVP_PKEY_CTX *pctx; if (!TEST_ptr(pctx = EVP_MD_CTX_get_pkey_ctx(mctx)) || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0)) return 0; return 1; } static int test_builtin(int n, int as) { EC_KEY *eckey_neg = NULL, *eckey = NULL; unsigned char dirt, offset, tbs[128]; unsigned char *sig = NULL; EVP_PKEY *pkey_neg = NULL, *pkey = NULL, *dup_pk = NULL; EVP_MD_CTX *mctx = NULL; size_t sig_len; int nid, ret = 0; int temp; nid = curves[n].nid; if (nid == NID_ipsec4 || nid == NID_ipsec3) { TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid)); return 1; } if (nid == NID_sm2 && as == EVP_PKEY_EC) { TEST_info("skipped: EC key type unsupported for curve %s", OBJ_nid2sn(nid)); return 1; } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) { TEST_info("skipped: SM2 key type unsupported for curve %s", OBJ_nid2sn(nid)); return 1; } TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid), as == EVP_PKEY_EC ? "EC" : "SM2"); if (!TEST_ptr(mctx = EVP_MD_CTX_new()) || !TEST_int_gt(RAND_bytes(tbs, sizeof(tbs)), 0) || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid)) || !TEST_true(EC_KEY_generate_key(eckey)) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)) || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid)) || !TEST_true(EC_KEY_generate_key(eckey_neg)) || !TEST_ptr(pkey_neg = EVP_PKEY_new()) || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL)) || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg))) goto err; if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)) || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1)) goto err; temp = ECDSA_size(eckey); if (!TEST_int_ge(temp, 0) || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp)) || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs))) || !TEST_int_le(sig_len, ECDSA_size(eckey)) || !TEST_true(EVP_MD_CTX_reset(mctx)) || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) || !TEST_true(EVP_MD_CTX_reset(mctx)) || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1) || !TEST_true(EVP_MD_CTX_reset(mctx)) || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) || !TEST_true(EVP_MD_CTX_reset(mctx))) goto err; tbs[0] ^= 1; if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0) || !TEST_true(EVP_MD_CTX_reset(mctx))) goto err; tbs[0] ^= 1; if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) || !TEST_true(EVP_MD_CTX_reset(mctx))) goto err; offset = tbs[0] % sig_len; dirt = tbs[1] ? tbs[1] : 1; sig[offset] ^= dirt; if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) || !TEST_true(EVP_MD_CTX_reset(mctx))) goto err; sig[offset] ^= dirt; if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey)) || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey)) || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1) || !TEST_true(EVP_MD_CTX_reset(mctx))) goto err; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_free(pkey_neg); EVP_PKEY_free(dup_pk); EVP_MD_CTX_free(mctx); OPENSSL_free(sig); return ret; } static int test_builtin_as_ec(int n) { return test_builtin(n, EVP_PKEY_EC); } # ifndef OPENSSL_NO_SM2 static int test_builtin_as_sm2(int n) { return test_builtin(n, EVP_PKEY_SM2); } # endif static int test_ecdsa_sig_NULL(void) { int ret; unsigned int siglen; unsigned char dgst[128] = { 0 }; EC_KEY *eckey = NULL; ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)) && TEST_int_eq(EC_KEY_generate_key(eckey), 1) && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen, eckey), 1) && TEST_int_gt(siglen, 0); EC_KEY_free(eckey); return ret; } #endif int setup_tests(void) { #ifdef OPENSSL_NO_EC TEST_note("Elliptic curves are disabled."); #else fake_rand = fake_rand_start(NULL); if (fake_rand == NULL) return 0; 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))) { fake_rand_finish(fake_rand); return 0; } ADD_ALL_TESTS(test_builtin_as_ec, crv_len); ADD_TEST(test_ecdsa_sig_NULL); # ifndef OPENSSL_NO_SM2 ADD_ALL_TESTS(test_builtin_as_sm2, crv_len); # endif ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats)); #endif return 1; } void cleanup_tests(void) { #ifndef OPENSSL_NO_EC fake_rand_finish(fake_rand); OPENSSL_free(curves); #endif }
test
openssl/test/ecdsatest.c
openssl
#include "internal/quic_fc.h" #include "internal/quic_error.h" #include "testutil.h" static int test_txfc(int is_stream) { int testresult = 0; QUIC_TXFC conn_txfc, stream_txfc, *txfc, *parent_txfc; if (!TEST_true(ossl_quic_txfc_init(&conn_txfc, 0))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_init(&stream_txfc, &conn_txfc))) goto err; txfc = is_stream ? &stream_txfc : &conn_txfc; parent_txfc = is_stream ? &conn_txfc : NULL; if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2000))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2000))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 0)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 100), 1900)) goto err; if (is_stream) { if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 2000)) goto err; if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 100), 1900)) goto err; } if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 500))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1500)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1500)) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 500)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 100))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 600)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1400)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1400)) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1400))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 0)) goto err; if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 0)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_false(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_false(ossl_quic_txfc_bump_cwm(txfc, 2000))) goto err; if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2500))) goto err; if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2400))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2500)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000)) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 500)) goto err; if (is_stream) ossl_quic_txfc_has_become_blocked(parent_txfc, 1); if (is_stream) { if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 400), 0)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 399))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1)) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; } else { if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 499))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (is_stream && !TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1))) goto err; if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0))) goto err; } testresult = 1; err: return testresult; } static OSSL_TIME cur_time; static OSSL_TIME fake_now(void *arg) { return cur_time; } #define RX_OPC_END 0 #define RX_OPC_INIT_CONN 1 #define RX_OPC_INIT_STREAM 2 #define RX_OPC_RX 3 #define RX_OPC_RETIRE 4 #define RX_OPC_CHECK_CWM_CONN 5 #define RX_OPC_CHECK_CWM_STREAM 6 #define RX_OPC_CHECK_SWM_CONN 7 #define RX_OPC_CHECK_SWM_STREAM 8 #define RX_OPC_CHECK_RWM_CONN 9 #define RX_OPC_CHECK_RWM_STREAM 10 #define RX_OPC_CHECK_CHANGED_CONN 11 #define RX_OPC_CHECK_CHANGED_STREAM 12 #define RX_OPC_CHECK_ERROR_CONN 13 #define RX_OPC_CHECK_ERROR_STREAM 14 #define RX_OPC_STEP_TIME 15 #define RX_OPC_MSG 16 struct rx_test_op { unsigned char op; size_t stream_idx; uint64_t arg0, arg1; unsigned char expect_fail; const char *msg; }; #define RX_OP_END \ { RX_OPC_END } #define RX_OP_INIT_CONN(init_window_size, max_window_size) \ { RX_OPC_INIT_CONN, 0, (init_window_size), (max_window_size) }, #define RX_OP_INIT_STREAM(stream_idx, init_window_size, max_window_size) \ { RX_OPC_INIT_STREAM, (stream_idx), (init_window_size), (max_window_size) }, #define RX_OP_RX(stream_idx, end, is_fin) \ { RX_OPC_RX, (stream_idx), (end), (is_fin) }, #define RX_OP_RETIRE(stream_idx, num_bytes, rtt, expect_fail) \ { RX_OPC_RETIRE, (stream_idx), (num_bytes), (rtt), (expect_fail) }, #define RX_OP_CHECK_CWM_CONN(expected) \ { RX_OPC_CHECK_CWM_CONN, 0, (expected) }, #define RX_OP_CHECK_CWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_CWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_SWM_CONN(expected) \ { RX_OPC_CHECK_SWM_CONN, 0, (expected) }, #define RX_OP_CHECK_SWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_SWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_RWM_CONN(expected) \ { RX_OPC_CHECK_RWM_CONN, 0, (expected) }, #define RX_OP_CHECK_RWM_STREAM(stream_id, expected) \ { RX_OPC_CHECK_RWM_STREAM, (stream_id), (expected) }, #define RX_OP_CHECK_CHANGED_CONN(expected, clear) \ { RX_OPC_CHECK_CHANGED_CONN, 0, (expected), (clear) }, #define RX_OP_CHECK_CHANGED_STREAM(stream_id, expected, clear) \ { RX_OPC_CHECK_CHANGED_STREAM, (stream_id), (expected), (clear) }, #define RX_OP_CHECK_ERROR_CONN(expected, clear) \ { RX_OPC_CHECK_ERROR_CONN, 0, (expected), (clear) }, #define RX_OP_CHECK_ERROR_STREAM(stream_id, expected, clear) \ { RX_OPC_CHECK_ERROR_STREAM, (stream_id), (expected), (clear) }, #define RX_OP_STEP_TIME(t) \ { RX_OPC_STEP_TIME, 0, (t) }, #define RX_OP_MSG(msg) \ { RX_OPC_MSG, 0, 0, 0, 0, (msg) }, #define RX_OP_INIT(init_window_size, max_window_size) \ RX_OP_INIT_CONN(init_window_size, max_window_size) \ RX_OP_INIT_STREAM(0, init_window_size, max_window_size) #define RX_OP_CHECK_CWM(expected) \ RX_OP_CHECK_CWM_CONN(expected) \ RX_OP_CHECK_CWM_STREAM(0, expected) #define RX_OP_CHECK_SWM(expected) \ RX_OP_CHECK_SWM_CONN(expected) \ RX_OP_CHECK_SWM_STREAM(0, expected) #define RX_OP_CHECK_RWM(expected) \ RX_OP_CHECK_RWM_CONN(expected) \ RX_OP_CHECK_RWM_STREAM(0, expected) #define RX_OP_CHECK_CHANGED(expected, clear) \ RX_OP_CHECK_CHANGED_CONN(expected, clear) \ RX_OP_CHECK_CHANGED_STREAM(0, expected, clear) #define RX_OP_CHECK_ERROR(expected, clear) \ RX_OP_CHECK_ERROR_CONN(expected, clear) \ RX_OP_CHECK_ERROR_STREAM(0, expected, clear) #define INIT_WINDOW_SIZE (1 * 1024 * 1024) #define INIT_S_WINDOW_SIZE (384 * 1024) static const struct rx_test_op rx_script_1[] = { RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_INIT(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CHANGED(0, 0) RX_OP_RETIRE(0, 1, 0, 1) RX_OP_RETIRE(0, 0, 0, 0) RX_OP_RX(0, 50, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_SWM(50) RX_OP_RX(0, 60, 0) RX_OP_CHECK_SWM(60) RX_OP_RETIRE(0, 20, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM(20) RX_OP_CHECK_SWM(60) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_RETIRE(0, 41, 0, 1) RX_OP_RETIRE(0, 40, 0, 0) RX_OP_CHECK_SWM(60) RX_OP_CHECK_RWM(60) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_STEP_TIME(201 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE - 60, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 2) RX_OP_RX(0, INIT_WINDOW_SIZE + 1, 0) RX_OP_STEP_TIME(201 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 2) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 3) RX_OP_RX(0, INIT_WINDOW_SIZE * 2 + 1, 0) RX_OP_STEP_TIME(199 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 3, 0) RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 3) RX_OP_CHECK_CHANGED(1, 0) RX_OP_CHECK_CHANGED(1, 1) RX_OP_CHECK_CHANGED(0, 0) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5) RX_OP_RX(0, INIT_WINDOW_SIZE * 5 - 5, 0) RX_OP_STEP_TIME(250 * OSSL_TIME_MS) RX_OP_RX(0, INIT_WINDOW_SIZE * 5 + 1, 0) RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 5) RX_OP_CHECK_ERROR(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR(0, 0) RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5) RX_OP_CHECK_CHANGED(0, 0) RX_OP_END }; static const struct rx_test_op rx_script_2[] = { RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_INIT_CONN(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE) RX_OP_INIT_STREAM(0, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE) RX_OP_INIT_STREAM(1, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE) RX_OP_RX(0, 10, 0) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_CONN(10) RX_OP_CHECK_SWM_STREAM(0, 10) RX_OP_CHECK_SWM_STREAM(1, 0) RX_OP_CHECK_RWM_CONN(0) RX_OP_CHECK_RWM_STREAM(0, 0) RX_OP_CHECK_RWM_STREAM(1, 0) RX_OP_RX(1, 42, 0) RX_OP_RX(1, 42, 0) RX_OP_RX(1, 35, 0) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_CONN(52) RX_OP_CHECK_SWM_STREAM(0, 10) RX_OP_CHECK_SWM_STREAM(1, 42) RX_OP_CHECK_RWM_CONN(0) RX_OP_CHECK_RWM_STREAM(0, 0) RX_OP_CHECK_RWM_STREAM(1, 0) RX_OP_RETIRE(0, 10, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM_CONN(10) RX_OP_CHECK_RWM_STREAM(0, 10) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_RETIRE(1, 42, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_RWM_CONN(52) RX_OP_CHECK_RWM_STREAM(1, 42) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE) RX_OP_CHECK_CHANGED_CONN(0, 0) RX_OP_STEP_TIME(1000 * OSSL_TIME_MS) RX_OP_RX(0, INIT_S_WINDOW_SIZE, 0) RX_OP_CHECK_SWM_CONN(INIT_S_WINDOW_SIZE + 42) RX_OP_CHECK_SWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_SWM_STREAM(1, 42) RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_RETIRE(0, INIT_S_WINDOW_SIZE - 10, 50 * OSSL_TIME_MS, 0) RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE * 2) RX_OP_CHECK_CHANGED_STREAM(0, 1, 0) RX_OP_CHECK_CHANGED_STREAM(0, 1, 1) RX_OP_CHECK_CHANGED_STREAM(0, 0, 0) RX_OP_CHECK_CWM_CONN(INIT_S_WINDOW_SIZE + INIT_WINDOW_SIZE + 42) RX_OP_CHECK_RWM_CONN(INIT_S_WINDOW_SIZE + 42) RX_OP_CHECK_RWM_STREAM(0, INIT_S_WINDOW_SIZE) RX_OP_CHECK_RWM_STREAM(1, 42) RX_OP_CHECK_CHANGED_CONN(1, 0) RX_OP_CHECK_CHANGED_CONN(1, 1) RX_OP_CHECK_CHANGED_CONN(0, 0) RX_OP_CHECK_ERROR_CONN(0, 0) RX_OP_CHECK_ERROR_STREAM(0, 0, 0) RX_OP_CHECK_ERROR_STREAM(1, 0, 0) RX_OP_RX(0, INIT_S_WINDOW_SIZE * 2 + 1, 0) RX_OP_CHECK_ERROR_STREAM(0, OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR_STREAM(0, OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR_STREAM(0, 0, 0) RX_OP_CHECK_ERROR_CONN(0, 0) RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0) RX_OP_CHECK_ERROR_CONN(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0) RX_OP_CHECK_ERROR_CONN(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1) RX_OP_CHECK_ERROR_CONN(0, 0) RX_OP_END }; static const struct rx_test_op *rx_scripts[] = { rx_script_1, rx_script_2 }; static int run_rxfc_script(const struct rx_test_op *script) { #define MAX_STREAMS 3 int testresult = 0; const struct rx_test_op *op = script; QUIC_RXFC conn_rxfc = {0}, stream_rxfc[MAX_STREAMS] = {0}; char stream_init_done[MAX_STREAMS] = {0}; int conn_init_done = 0; cur_time = ossl_time_zero(); for (; op->op != RX_OPC_END; ++op) { switch (op->op) { case RX_OPC_INIT_CONN: if (!TEST_true(ossl_quic_rxfc_init(&conn_rxfc, 0, op->arg0, op->arg1, fake_now, NULL))) goto err; conn_init_done = 1; break; case RX_OPC_INIT_STREAM: if (!TEST_size_t_lt(op->stream_idx, OSSL_NELEM(stream_rxfc)) || !TEST_true(conn_init_done)) goto err; if (!TEST_true(ossl_quic_rxfc_init(&stream_rxfc[op->stream_idx], &conn_rxfc, op->arg0, op->arg1, fake_now, NULL))) goto err; stream_init_done[op->stream_idx] = 1; break; case RX_OPC_RX: if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_true(ossl_quic_rxfc_on_rx_stream_frame(&stream_rxfc[op->stream_idx], op->arg0, (int)op->arg1))) goto err; break; case RX_OPC_RETIRE: if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_on_retire(&stream_rxfc[op->stream_idx], op->arg0, ossl_ticks2time(op->arg1)), !op->expect_fail)) goto err; break; case RX_OPC_CHECK_CWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_CWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_SWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_SWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_RWM_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&conn_rxfc), op->arg0)) goto err; break; case RX_OPC_CHECK_RWM_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&stream_rxfc[op->stream_idx]), op->arg0)) goto err; break; case RX_OPC_CHECK_CHANGED_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&conn_rxfc, (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_CHANGED_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&stream_rxfc[op->stream_idx], (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_ERROR_CONN: if (!TEST_true(conn_init_done)) goto err; if (!TEST_int_eq(ossl_quic_rxfc_get_error(&conn_rxfc, (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_CHECK_ERROR_STREAM: if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc) && stream_init_done[op->stream_idx])) goto err; if (!TEST_int_eq(ossl_quic_rxfc_get_error(&stream_rxfc[op->stream_idx], (int)op->arg1), (int)op->arg0)) goto err; break; case RX_OPC_STEP_TIME: cur_time = ossl_time_add(cur_time, ossl_ticks2time(op->arg0)); break; case RX_OPC_MSG: fprintf(stderr, "# %s\n", op->msg); break; default: goto err; } } testresult = 1; err: return testresult; } static int test_rxfc(int idx) { return run_rxfc_script(rx_scripts[idx]); } int setup_tests(void) { ADD_ALL_TESTS(test_txfc, 2); ADD_ALL_TESTS(test_rxfc, OSSL_NELEM(rx_scripts)); return 1; }
test
openssl/test/quic_fc_test.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <openssl/opensslconf.h> #include <string.h> #include <openssl/engine.h> #include <openssl/evp.h> #include <openssl/rand.h> #include "testutil.h" #define BUFFER_SIZE 17 #ifndef OPENSSL_NO_ENGINE static ENGINE *e; static int test_afalg_aes_cbc(int keysize_idx) { EVP_CIPHER_CTX *ctx; const EVP_CIPHER *cipher; unsigned char ebuf[BUFFER_SIZE + 32]; unsigned char dbuf[BUFFER_SIZE + 32]; const unsigned char *enc_result = NULL; int encl, encf, decl, decf; int ret = 0; static const unsigned char key[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06" "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"; static const unsigned char iv[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41"; static const unsigned char in[BUFFER_SIZE] = "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67" "\x0a"; static const unsigned char encresult_128[BUFFER_SIZE] = "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a" "\x2d"; static const unsigned char encresult_192[BUFFER_SIZE] = "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55" "\xeb"; static const unsigned char encresult_256[BUFFER_SIZE] = "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4" "\x13"; #ifdef OSSL_SANITIZE_MEMORY OPENSSL_cleanse(ebuf, sizeof(ebuf)); OPENSSL_cleanse(dbuf, sizeof(dbuf)); #endif switch (keysize_idx) { case 0: cipher = EVP_aes_128_cbc(); enc_result = &encresult_128[0]; break; case 1: cipher = EVP_aes_192_cbc(); enc_result = &encresult_192[0]; break; case 2: cipher = EVP_aes_256_cbc(); enc_result = &encresult_256[0]; break; default: cipher = NULL; } if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) return 0; if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1)) || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE)) || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf))) goto end; encl += encf; if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE)) goto end; if (!TEST_true(EVP_CIPHER_CTX_reset(ctx)) || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0)) || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl)) || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf))) goto end; decl += decf; if (!TEST_int_eq(decl, BUFFER_SIZE) || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE)) goto end; ret = 1; end: EVP_CIPHER_CTX_free(ctx); return ret; } static int test_pr16743(void) { int ret = 0; const EVP_CIPHER *cipher; EVP_CIPHER_CTX *ctx; if (!TEST_true(ENGINE_init(e))) return 0; cipher = ENGINE_get_cipher(e, NID_aes_128_cbc); ctx = EVP_CIPHER_CTX_new(); if (cipher != NULL && ctx != NULL) ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL); TEST_true(ret); EVP_CIPHER_CTX_free(ctx); ENGINE_finish(e); return ret; } int global_init(void) { ENGINE_load_builtin_engines(); # ifndef OPENSSL_NO_STATIC_ENGINE OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL); # endif return 1; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_ENGINE if ((e = ENGINE_by_id("afalg")) == NULL) { TEST_info("Can't load AFALG engine"); } else { ADD_ALL_TESTS(test_afalg_aes_cbc, 3); ADD_TEST(test_pr16743); } #endif return 1; } #ifndef OPENSSL_NO_ENGINE void cleanup_tests(void) { ENGINE_free(e); } #endif
test
openssl/test/afalgtest.c
openssl
#include <assert.h> #include <errno.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <openssl/bn.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/rand.h> #include "internal/nelem.h" #include "internal/numbers.h" #include "testutil.h" #include "bn_prime.h" #include "crypto/bn.h" static BN_CTX *ctx; static int test_is_prime_enhanced(void) { int ret; int status = 0; BIGNUM *bn = NULL; ret = TEST_ptr(bn = BN_new()) && TEST_true(BN_set_word(bn, 11)) && TEST_true(ossl_bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1, &status)) && TEST_int_eq(status, BN_PRIMETEST_PROBABLY_PRIME); BN_free(bn); return ret; } static int composites[] = { 9, 21, 77, 81, 265 }; static int test_is_composite_enhanced(int id) { int ret; int status = 0; BIGNUM *bn = NULL; ret = TEST_ptr(bn = BN_new()) && TEST_true(BN_set_word(bn, composites[id])) && TEST_true(ossl_bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1, &status)) && TEST_int_ne(status, BN_PRIMETEST_PROBABLY_PRIME); BN_free(bn); return ret; } static int test_bn_small_factors(void) { int ret = 0, i; BIGNUM *b = NULL; if (!(TEST_ptr(b = BN_new()) && TEST_true(BN_set_word(b, 3)))) goto err; for (i = 1; i < NUMPRIMES; i++) { prime_t p = primes[i]; if (p > 3 && p <= 751 && !BN_mul_word(b, p)) goto err; if (p > 751) break; } ret = TEST_BN_eq(ossl_bn_get0_small_factors(), b); err: BN_free(b); return ret; } int setup_tests(void) { if (!TEST_ptr(ctx = BN_CTX_new())) return 0; ADD_TEST(test_is_prime_enhanced); ADD_ALL_TESTS(test_is_composite_enhanced, (int)OSSL_NELEM(composites)); ADD_TEST(test_bn_small_factors); return 1; } void cleanup_tests(void) { BN_CTX_free(ctx); }
test
openssl/test/bn_internal_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <internal/priority_queue.h> #include <openssl/err.h> #include <openssl/crypto.h> #include "internal/nelem.h" #include "testutil.h" #define MAX_SAMPLES 500000 DEFINE_PRIORITY_QUEUE_OF(size_t); static size_t num_rec_freed; static int size_t_compare(const size_t *a, const size_t *b) { if (*a < *b) return -1; if (*a > *b) return 1; return 0; } static int qsort_size_t_compare(const void *a, const void *b) { return size_t_compare((size_t *)a, (size_t *)b); } static int qsort_size_t_compare_rev(const void *a, const void *b) { return size_t_compare((size_t *)b, (size_t *)a); } static void free_checker(ossl_unused size_t *p) { num_rec_freed++; } static int test_size_t_priority_queue_int(int reserve, int order, int count, int remove, int random, int popfree) { PRIORITY_QUEUE_OF(size_t) *pq = NULL; static size_t values[MAX_SAMPLES], sorted[MAX_SAMPLES], ref[MAX_SAMPLES]; size_t n; int i, res = 0; static const char *orders[3] = { "unordered", "ascending", "descending" }; TEST_info("testing count %d, %s, %s, values %s, remove %d, %sfree", count, orders[order], reserve ? "reserve" : "grow", random ? "random" : "deterministic", remove, popfree ? "pop " : ""); if (!TEST_size_t_le(count, MAX_SAMPLES)) return 0; memset(values, 0, sizeof(values)); memset(sorted, 0, sizeof(sorted)); memset(ref, 0, sizeof(ref)); for (i = 0; i < count; i++) values[i] = random ? test_random() : (size_t)(count - i); memcpy(sorted, values, sizeof(*sorted) * count); qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare); if (order == 1) memcpy(values, sorted, sizeof(*values) * count); else if (order == 2) qsort(values, count, sizeof(*values), &qsort_size_t_compare_rev); if (!TEST_ptr(pq = ossl_pqueue_size_t_new(&size_t_compare)) || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), 0)) goto err; if (reserve && !TEST_true(ossl_pqueue_size_t_reserve(pq, count))) goto err; for (i = 0; i < count; i++) if (!TEST_true(ossl_pqueue_size_t_push(pq, values + i, ref + i))) goto err; if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), *sorted) || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), count)) goto err; if (remove) { while (remove-- > 0) { i = test_random() % count; if (values[i] != SIZE_MAX) { if (!TEST_ptr_eq(ossl_pqueue_size_t_remove(pq, ref[i]), values + i)) goto err; values[i] = SIZE_MAX; } } memcpy(sorted, values, sizeof(*sorted) * count); qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare); } for (i = 0; ossl_pqueue_size_t_peek(pq) != NULL; i++) if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), sorted[i]) || !TEST_size_t_eq(*ossl_pqueue_size_t_pop(pq), sorted[i])) goto err; if (popfree) { num_rec_freed = 0; n = ossl_pqueue_size_t_num(pq); ossl_pqueue_size_t_pop_free(pq, &free_checker); pq = NULL; if (!TEST_size_t_eq(num_rec_freed, n)) goto err; } res = 1; err: ossl_pqueue_size_t_free(pq); return res; } static const int test_size_t_priority_counts[] = { 10, 11, 6, 5, 3, 1, 2, 7500 }; static int test_size_t_priority_queue(int n) { int reserve, order, count, remove, random, popfree; count = n % OSSL_NELEM(test_size_t_priority_counts); n /= OSSL_NELEM(test_size_t_priority_counts); order = n % 3; n /= 3; random = n % 2; n /= 2; reserve = n % 2; n /= 2; remove = n % 6; n /= 6; popfree = n % 2; count = test_size_t_priority_counts[count]; return test_size_t_priority_queue_int(reserve, order, count, remove, random, popfree); } static int test_large_priority_queue(void) { return test_size_t_priority_queue_int(0, 0, MAX_SAMPLES, MAX_SAMPLES / 100, 1, 1); } typedef struct info_st { uint64_t seq_num, sub_seq; size_t idx; } INFO; DEFINE_PRIORITY_QUEUE_OF(INFO); static int cmp(const INFO *a, const INFO *b) { if (a->seq_num < b->seq_num) return -1; if (a->seq_num > b->seq_num) return 1; if (a->sub_seq < b->sub_seq) return -1; if (a->sub_seq > b->sub_seq) return 1; return 0; } static int test_22644(void) { size_t i; INFO infos[32]; int res = 0; PRIORITY_QUEUE_OF(INFO) *pq = ossl_pqueue_INFO_new(cmp); memset(infos, 0, sizeof(infos)); for (i = 0; i < 32; ++i) infos[i].sub_seq = i; infos[0].seq_num = 70650219160667140; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[0], &infos[0].idx)) || !TEST_size_t_eq(infos[0].idx, 7) || !TEST_ptr(ossl_pqueue_INFO_remove(pq, infos[0].idx))) goto err; infos[1].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[1], &infos[1].idx)) || !TEST_size_t_eq(infos[1].idx, 7) || !TEST_ptr(ossl_pqueue_INFO_remove(pq, infos[1].idx))) goto err; infos[2].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[2], &infos[2].idx)) || !TEST_size_t_eq(infos[2].idx, 7)) goto err; infos[3].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[3], &infos[3].idx)) || !TEST_size_t_eq(infos[3].idx, 6)) goto err; infos[4].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[4], &infos[4].idx)) || !TEST_size_t_eq(infos[4].idx, 5)) goto err; infos[5].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[5], &infos[5].idx)) || !TEST_size_t_eq(infos[5].idx, 4)) goto err; infos[6].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[6], &infos[6].idx)) || !TEST_size_t_eq(infos[6].idx, 3)) goto err; infos[7].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[7], &infos[7].idx)) || !TEST_size_t_eq(infos[7].idx, 2)) goto err; infos[8].seq_num = 289360691352306692; if (!TEST_true(ossl_pqueue_INFO_push(pq, &infos[8], &infos[8].idx)) || !TEST_size_t_eq(infos[8].idx, 1)) goto err; if (!TEST_ptr(ossl_pqueue_INFO_pop(pq)) || !TEST_ptr(ossl_pqueue_INFO_pop(pq))) goto err; res = 1; err: ossl_pqueue_INFO_free(pq); return res; } int setup_tests(void) { ADD_ALL_TESTS(test_size_t_priority_queue, OSSL_NELEM(test_size_t_priority_counts) * 3 * 2 * 2 * 6 * 2); ADD_TEST(test_large_priority_queue); ADD_TEST(test_22644); return 1; }
test
openssl/test/priority_queue_test.c
openssl
#include <stdio.h> #include <string.h> #include "internal/nelem.h" #include "helpers/ssl_test_ctx.h" #include "testutil.h" #include <openssl/e_os2.h> #include <openssl/err.h> #include <openssl/conf.h> #include <openssl/ssl.h> static CONF *conf = NULL; typedef struct ssl_test_ctx_test_fixture { const char *test_case_name; const char *test_section; SSL_TEST_CTX *expected_ctx; } SSL_TEST_CTX_TEST_FIXTURE; static int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1, SSL_TEST_CLIENT_CONF *conf2) { if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback) || !TEST_int_eq(conf1->servername, conf2->servername) || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols) || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols) || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation) || !TEST_int_eq(conf1->max_fragment_len_mode, conf2->max_fragment_len_mode)) return 0; return 1; } static int serverconf_eq(SSL_TEST_SERVER_CONF *serv, SSL_TEST_SERVER_CONF *serv2) { if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback) || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols) || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols) || !TEST_int_eq(serv->broken_session_ticket, serv2->broken_session_ticket) || !TEST_str_eq(serv->session_ticket_app_data, serv2->session_ticket_app_data) || !TEST_int_eq(serv->cert_status, serv2->cert_status)) return 0; return 1; } static int extraconf_eq(SSL_TEST_EXTRA_CONF *extra, SSL_TEST_EXTRA_CONF *extra2) { if (!TEST_true(clientconf_eq(&extra->client, &extra2->client)) || !TEST_true(serverconf_eq(&extra->server, &extra2->server)) || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2))) return 0; return 1; } static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2) { if (!TEST_int_eq(ctx->method, ctx2->method) || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode) || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size) || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size) || !extraconf_eq(&ctx->extra, &ctx2->extra) || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra) || !TEST_int_eq(ctx->expected_result, ctx2->expected_result) || !TEST_int_eq(ctx->expected_client_alert, ctx2->expected_client_alert) || !TEST_int_eq(ctx->expected_server_alert, ctx2->expected_server_alert) || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol) || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername) || !TEST_int_eq(ctx->session_ticket_expected, ctx2->session_ticket_expected) || !TEST_int_eq(ctx->compression_expected, ctx2->compression_expected) || !TEST_str_eq(ctx->expected_npn_protocol, ctx2->expected_npn_protocol) || !TEST_str_eq(ctx->expected_alpn_protocol, ctx2->expected_alpn_protocol) || !TEST_str_eq(ctx->expected_cipher, ctx2->expected_cipher) || !TEST_str_eq(ctx->expected_session_ticket_app_data, ctx2->expected_session_ticket_app_data) || !TEST_int_eq(ctx->resumption_expected, ctx2->resumption_expected) || !TEST_int_eq(ctx->session_id_expected, ctx2->session_id_expected)) return 0; return 1; } static SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name) { SSL_TEST_CTX_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new(NULL))) { OPENSSL_free(fixture); return NULL; } return fixture; } static int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture) { int success = 0; SSL_TEST_CTX *ctx; if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section, fixture->expected_ctx->libctx)) || !testctx_eq(ctx, fixture->expected_ctx)) goto err; success = 1; err: SSL_TEST_CTX_free(ctx); return success; } static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture) { SSL_TEST_CTX_free(fixture->expected_ctx); OPENSSL_free(fixture); } #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \ SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up); #define EXECUTE_SSL_TEST_CTX_TEST() \ EXECUTE_TEST(execute_test, tear_down) static int test_empty_configuration(void) { SETUP_SSL_TEST_CTX_TEST_FIXTURE(); fixture->test_section = "ssltest_default"; fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS; EXECUTE_SSL_TEST_CTX_TEST(); return result; } static int test_good_configuration(void) { SETUP_SSL_TEST_CTX_TEST_FIXTURE(); fixture->test_section = "ssltest_good"; fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS; fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME; fixture->expected_ctx->app_data_size = 1024; fixture->expected_ctx->max_fragment_size = 2048; fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL; fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA; fixture->expected_ctx->expected_server_alert = 0; fixture->expected_ctx->expected_protocol = TLS1_1_VERSION; fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2; fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES; fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO; fixture->expected_ctx->session_id_expected = SSL_TEST_SESSION_ID_IGNORE; fixture->expected_ctx->resumption_expected = 1; fixture->expected_ctx->extra.client.verify_callback = SSL_TEST_VERIFY_REJECT_ALL; fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2; fixture->expected_ctx->extra.client.npn_protocols = OPENSSL_strdup("foo,bar"); if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols)) goto err; fixture->expected_ctx->extra.client.max_fragment_len_mode = 0; fixture->expected_ctx->extra.server.servername_callback = SSL_TEST_SERVERNAME_IGNORE_MISMATCH; fixture->expected_ctx->extra.server.broken_session_ticket = 1; fixture->expected_ctx->resume_extra.server2.alpn_protocols = OPENSSL_strdup("baz"); if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols)) goto err; fixture->expected_ctx->resume_extra.client.ct_validation = SSL_TEST_CT_VALIDATION_STRICT; EXECUTE_SSL_TEST_CTX_TEST(); return result; err: tear_down(fixture); return 0; } static const char *bad_configurations[] = { "ssltest_unknown_option", "ssltest_wrong_section", "ssltest_unknown_expected_result", "ssltest_unknown_alert", "ssltest_unknown_protocol", "ssltest_unknown_verify_callback", "ssltest_unknown_servername", "ssltest_unknown_servername_callback", "ssltest_unknown_session_ticket_expected", "ssltest_unknown_compression_expected", "ssltest_unknown_session_id_expected", "ssltest_unknown_method", "ssltest_unknown_handshake_mode", "ssltest_unknown_resumption_expected", "ssltest_unknown_ct_validation", "ssltest_invalid_max_fragment_len", }; static int test_bad_configuration(int idx) { SSL_TEST_CTX *ctx; if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf, bad_configurations[idx], NULL))) { SSL_TEST_CTX_free(ctx); return 0; } return 1; } OPT_TEST_DECLARE_USAGE("conf_file\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(conf = NCONF_new(NULL))) return 0; if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)) return 0; ADD_TEST(test_empty_configuration); ADD_TEST(test_good_configuration); ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations)); return 1; } void cleanup_tests(void) { NCONF_free(conf); }
test
openssl/test/ssl_test_ctx_test.c
openssl
#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 type; time_t cmp_time; int expected; } TESTDATA; typedef struct { const char *data; int set_string; int expected; int expected_type; const char *expected_string; } TESTDATA_FORMAT; static TESTDATA_FORMAT x509_format_tests[] = { { "20170217180105Z", 0, 1, -1, NULL, }, { "20170229180105Z", 0, 0, -1, NULL, }, { "20160229180105Z", 0, 1, -1, NULL, }, { "201702171801Z", 0, 0, -1, NULL, }, { "20170217180105.001Z", 0, 0, -1, NULL, }, { "20170217180105+0800", 0, 0, -1, NULL, }, { "201702171801Z", 1, 0, -1, NULL, }, { "20170217180105.001Z", 1, 0, -1, NULL, }, { "20170217180105+0800", 1, 0, -1, NULL, }, { "20170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z", }, { "20510217180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "20510217180154Z", }, { "19230419180154Z", 1, 1, V_ASN1_GENERALIZEDTIME, "19230419180154Z", }, { "1702171801Z", 0, 0, -1, NULL, }, { "050229180101Z", 0, 0, -1, NULL, }, { "040229180101Z", 0, 1, -1, NULL, }, { "170217180154+0800", 0, 0, -1, NULL, }, { "1702171801Z", 1, 0, -1, NULL, }, { "170217180154+0800", 1, 0, -1, NULL, }, { "170217180154Z", 1, 1, V_ASN1_UTCTIME, "170217180154Z", }, { "981223180154Z", 1, 1, V_ASN1_UTCTIME, "981223180154Z", }, }; static TESTDATA x509_cmp_tests[] = { { "20170217180154Z", V_ASN1_GENERALIZEDTIME, 1487354514, -1, }, { "20170217180154Z", V_ASN1_GENERALIZEDTIME, 1487354515, -1, }, { "20170217180154Z", V_ASN1_GENERALIZEDTIME, 1487354513, 1, }, { "170217180154Z", V_ASN1_UTCTIME, 1487354514, -1, }, { "170217180154Z", V_ASN1_UTCTIME, 1487354515, -1, }, { "170217180154Z", V_ASN1_UTCTIME, 1487354513, 1, }, { "990217180154Z", V_ASN1_UTCTIME, 919274514, -1, }, { "990217180154Z", V_ASN1_UTCTIME, 919274515, -1, }, { "990217180154Z", V_ASN1_UTCTIME, 919274513, 1, }, { "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "170217180154", V_ASN1_UTCTIME, 0, 0, }, { "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "1702171801Z", V_ASN1_UTCTIME, 0, 0, }, { "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "170217180154.001Z", V_ASN1_UTCTIME, 0, 0, }, { "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "170217180154+0100", V_ASN1_UTCTIME, 0, 0, }, { "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "17021718015400Z", V_ASN1_UTCTIME, 0, 0, }, { "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "17021718015aZ", V_ASN1_UTCTIME, 0, 0, }, { "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0, }, { "20170217180154Z", V_ASN1_UTCTIME, 0, 0, }, { "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0, }, { "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0, }, }; static int test_x509_cmp_time(int idx) { ASN1_TIME t; int result; memset(&t, 0, sizeof(t)); t.type = x509_cmp_tests[idx].type; t.data = (unsigned char*)(x509_cmp_tests[idx].data); t.length = strlen(x509_cmp_tests[idx].data); t.flags = 0; result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time); if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) { TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n", idx, x509_cmp_tests[idx].expected, result); return 0; } return 1; } static int test_x509_cmp_time_current(void) { time_t now = time(NULL); ASN1_TIME *asn1_before = NULL, *asn1_after = NULL; int cmp_result, failed = 0; asn1_before = ASN1_TIME_adj(NULL, now, -1, 0); asn1_after = ASN1_TIME_adj(NULL, now, 1, 0); cmp_result = X509_cmp_time(asn1_before, NULL); if (!TEST_int_eq(cmp_result, -1)) failed = 1; cmp_result = X509_cmp_time(asn1_after, NULL); if (!TEST_int_eq(cmp_result, 1)) failed = 1; ASN1_TIME_free(asn1_before); ASN1_TIME_free(asn1_after); return failed == 0; } static int test_X509_cmp_timeframe_vpm(const X509_VERIFY_PARAM *vpm, ASN1_TIME *asn1_before, ASN1_TIME *asn1_mid, ASN1_TIME *asn1_after) { int always_0 = vpm != NULL && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_USE_CHECK_TIME) == 0 && (X509_VERIFY_PARAM_get_flags(vpm) & X509_V_FLAG_NO_CHECK_TIME) != 0; return asn1_before != NULL && asn1_mid != NULL && asn1_after != NULL && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_after), 0) && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, NULL), 0) && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, asn1_after), 0) && TEST_int_eq(X509_cmp_timeframe(vpm, NULL, NULL), 0) && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_after), always_0 ? 0 : -1) && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_before, asn1_before), always_0 ? 0 : 1) && TEST_int_eq(X509_cmp_timeframe(vpm, asn1_after, asn1_before), always_0 ? 0 : 1); } static int test_X509_cmp_timeframe(void) { time_t now = time(NULL); ASN1_TIME *asn1_mid = ASN1_TIME_adj(NULL, now, 0, 0); ASN1_TIME *asn1_before = ASN1_TIME_adj(NULL, now, -1, 0); ASN1_TIME *asn1_after = ASN1_TIME_adj(NULL, now, 1, 0); X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new(); int res = 0; if (vpm == NULL) goto finish; res = test_X509_cmp_timeframe_vpm(NULL, asn1_before, asn1_mid, asn1_after) && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after); X509_VERIFY_PARAM_set_time(vpm, now); res = res && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after) && X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME) && test_X509_cmp_timeframe_vpm(vpm, asn1_before, asn1_mid, asn1_after); X509_VERIFY_PARAM_free(vpm); finish: ASN1_TIME_free(asn1_mid); ASN1_TIME_free(asn1_before); ASN1_TIME_free(asn1_after); return res; } static int test_x509_time(int idx) { ASN1_TIME *t = NULL; int result, rv = 0; if (x509_format_tests[idx].set_string) { t = ASN1_TIME_new(); if (t == NULL) { TEST_info("test_x509_time(%d) failed: internal error\n", idx); return 0; } } result = ASN1_TIME_set_string_X509(t, x509_format_tests[idx].data); if (!TEST_int_eq(result, x509_format_tests[idx].expected)) { TEST_info("test_x509_time(%d) failed: expected %d, got %d\n", idx, x509_format_tests[idx].expected, result); goto out; } if (t != NULL && x509_format_tests[idx].expected_type != -1) { if (!TEST_int_eq(t->type, x509_format_tests[idx].expected_type)) { TEST_info("test_x509_time(%d) failed: expected_type %d, got %d\n", idx, x509_format_tests[idx].expected_type, t->type); goto out; } } if (t != NULL && x509_format_tests[idx].expected_string) { if (!TEST_mem_eq((const char *)t->data, t->length, x509_format_tests[idx].expected_string, strlen(x509_format_tests[idx].expected_string))) { TEST_info("test_x509_time(%d) failed: expected_string %s, got %.*s\n", idx, x509_format_tests[idx].expected_string, t->length, t->data); goto out; } } rv = 1; out: if (t != NULL) ASN1_TIME_free(t); return rv; } static const struct { int y, m, d; int yd, wd; } day_of_week_tests[] = { { 1900, 1, 1, 0, 1 }, { 1900, 2, 28, 58, 3 }, { 1900, 3, 1, 59, 4 }, { 1900, 12, 31, 364, 1 }, { 1901, 1, 1, 0, 2 }, { 1970, 1, 1, 0, 4 }, { 1999, 1, 10, 9, 0 }, { 1999, 12, 31, 364, 5 }, { 2000, 1, 1, 0, 6 }, { 2000, 2, 28, 58, 1 }, { 2000, 2, 29, 59, 2 }, { 2000, 3, 1, 60, 3 }, { 2000, 12, 31, 365, 0 }, { 2001, 1, 1, 0, 1 }, { 2008, 1, 1, 0, 2 }, { 2008, 2, 28, 58, 4 }, { 2008, 2, 29, 59, 5 }, { 2008, 3, 1, 60, 6 }, { 2008, 12, 31, 365, 3 }, { 2009, 1, 1, 0, 4 }, { 2011, 1, 1, 0, 6 }, { 2011, 2, 28, 58, 1 }, { 2011, 3, 1, 59, 2 }, { 2011, 12, 31, 364, 6 }, { 2012, 1, 1, 0, 0 }, { 2019, 1, 2, 1, 3 }, { 2019, 2, 2, 32, 6 }, { 2019, 3, 2, 60, 6 }, { 2019, 4, 2, 91, 2 }, { 2019, 5, 2, 121, 4 }, { 2019, 6, 2, 152, 0 }, { 2019, 7, 2, 182, 2 }, { 2019, 8, 2, 213, 5 }, { 2019, 9, 2, 244, 1 }, { 2019, 10, 2, 274, 3 }, { 2019, 11, 2, 305, 6 }, { 2019, 12, 2, 335, 1 }, { 2020, 1, 2, 1, 4 }, { 2020, 2, 2, 32, 0 }, { 2020, 3, 2, 61, 1 }, { 2020, 4, 2, 92, 4 }, { 2020, 5, 2, 122, 6 }, { 2020, 6, 2, 153, 2 }, { 2020, 7, 2, 183, 4 }, { 2020, 8, 2, 214, 0 }, { 2020, 9, 2, 245, 3 }, { 2020, 10, 2, 275, 5 }, { 2020, 11, 2, 306, 1 }, { 2020, 12, 2, 336, 3 } }; static int test_days(int n) { char d[16]; ASN1_TIME *a = NULL; struct tm t; int r; BIO_snprintf(d, sizeof(d), "%04d%02d%02d050505Z", day_of_week_tests[n].y, day_of_week_tests[n].m, day_of_week_tests[n].d); if (!TEST_ptr(a = ASN1_TIME_new())) return 0; r = TEST_true(ASN1_TIME_set_string(a, d)) && TEST_true(ASN1_TIME_to_tm(a, &t)) && TEST_int_eq(t.tm_yday, day_of_week_tests[n].yd) && TEST_int_eq(t.tm_wday, day_of_week_tests[n].wd); ASN1_TIME_free(a); return r; } #define construct_asn1_time(s, t, e) \ { { sizeof(s) - 1, t, (unsigned char*)s, 0 }, e } static const struct { ASN1_TIME asn1; const char *readable; } x509_print_tests_rfc_822 [] = { construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME, "Jul 31 22:20:50 2017 GMT"), construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME, "Bad time value"), construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME, "Jul 31 22:20:50.123 2017 GMT"), construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME, "Jul 31 22:20:50.1 2017 GMT"), construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME, "Bad time value"), construct_asn1_time("170731222050Z", V_ASN1_UTCTIME, "Jul 31 22:20:50 2017 GMT"), construct_asn1_time("1707312220Z", V_ASN1_UTCTIME, "Bad time value"), }; static const struct { ASN1_TIME asn1; const char *readable; } x509_print_tests_iso_8601 [] = { construct_asn1_time("20170731222050Z", V_ASN1_GENERALIZEDTIME, "2017-07-31 22:20:50Z"), construct_asn1_time("201707312220Z", V_ASN1_GENERALIZEDTIME, "Bad time value"), construct_asn1_time("20170731222050.123Z", V_ASN1_GENERALIZEDTIME, "2017-07-31 22:20:50.123Z"), construct_asn1_time("20170731222050.1Z", V_ASN1_GENERALIZEDTIME, "2017-07-31 22:20:50.1Z"), construct_asn1_time("20170731222050.Z", V_ASN1_GENERALIZEDTIME, "Bad time value"), construct_asn1_time("170731222050Z", V_ASN1_UTCTIME, "2017-07-31 22:20:50Z"), construct_asn1_time("1707312220Z", V_ASN1_UTCTIME, "Bad time value"), }; static int test_x509_time_print_rfc_822(int idx) { BIO *m; int ret = 0, rv; char *pp; const char *readable; if (!TEST_ptr(m = BIO_new(BIO_s_mem()))) goto err; rv = ASN1_TIME_print_ex(m, &x509_print_tests_rfc_822[idx].asn1, ASN1_DTFLGS_RFC822); readable = x509_print_tests_rfc_822[idx].readable; if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) { goto err; } if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0) || !TEST_int_eq(rv, (int)strlen(readable)) || !TEST_strn_eq(pp, readable, rv)) goto err; ret = 1; err: BIO_free(m); return ret; } static int test_x509_time_print_iso_8601(int idx) { BIO *m; int ret = 0, rv; char *pp; const char *readable; if (!TEST_ptr(m = BIO_new(BIO_s_mem()))) goto err; rv = ASN1_TIME_print_ex(m, &x509_print_tests_iso_8601[idx].asn1, ASN1_DTFLGS_ISO8601); readable = x509_print_tests_iso_8601[idx].readable; if (rv == 0 && !TEST_str_eq(readable, "Bad time value")) { goto err; } if (!TEST_int_ne(rv = BIO_get_mem_data(m, &pp), 0) || !TEST_int_eq(rv, (int)strlen(readable)) || !TEST_strn_eq(pp, readable, rv)) goto err; ret = 1; err: BIO_free(m); return ret; } int setup_tests(void) { ADD_TEST(test_x509_cmp_time_current); ADD_TEST(test_X509_cmp_timeframe); ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests)); ADD_ALL_TESTS(test_x509_time, OSSL_NELEM(x509_format_tests)); ADD_ALL_TESTS(test_days, OSSL_NELEM(day_of_week_tests)); ADD_ALL_TESTS(test_x509_time_print_rfc_822, OSSL_NELEM(x509_print_tests_rfc_822)); ADD_ALL_TESTS(test_x509_time_print_iso_8601, OSSL_NELEM(x509_print_tests_iso_8601)); return 1; }
test
openssl/test/x509_time_test.c
openssl
#include "internal/cryptlib.h" #include "testutil.h" static int test_set0_default(void) { OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default(); OSSL_LIB_CTX *local = OSSL_LIB_CTX_new(); OSSL_LIB_CTX *prev; int testresult = 0; if (!TEST_ptr(global) || !TEST_ptr(local) || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))) goto err; if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local)) || !TEST_ptr_eq(global, prev)) goto err; if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))) goto err; if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default())) goto err; if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global)) || !TEST_ptr_eq(local, prev)) goto err; testresult = 1; err: OSSL_LIB_CTX_free(local); return testresult; } int setup_tests(void) { ADD_TEST(test_set0_default); return 1; }
test
openssl/test/context_internal_test.c
openssl
#include <string.h> #include <stdio.h> #ifdef PROVIDER_INIT_FUNCTION_NAME # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME #endif #include "internal/e_os.h" #include <openssl/core.h> #include <openssl/core_dispatch.h> #include <openssl/err.h> #include <openssl/evp.h> #include <openssl/crypto.h> #include <openssl/provider.h> typedef struct p_test_ctx { char *thisfile; char *thisfunc; const OSSL_CORE_HANDLE *handle; OSSL_LIB_CTX *libctx; } P_TEST_CTX; static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL; static OSSL_FUNC_core_get_params_fn *c_get_params = NULL; static OSSL_FUNC_core_new_error_fn *c_new_error; static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug; static OSSL_FUNC_core_vset_error_fn *c_vset_error; static const OSSL_PARAM p_param_types[] = { { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 }, { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, { NULL, 0, NULL, 0, 0 } }; static OSSL_FUNC_provider_gettable_params_fn p_gettable_params; static OSSL_FUNC_provider_get_params_fn p_get_params; static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings; static OSSL_FUNC_provider_teardown_fn p_teardown; static void p_set_error(int lib, int reason, const char *file, int line, const char *func, const char *fmt, ...) { va_list ap; va_start(ap, fmt); c_new_error(NULL); c_set_error_debug(NULL, file, line, func); c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap); va_end(ap); } static const OSSL_PARAM *p_gettable_params(void *_) { return p_param_types; } static int p_get_params(void *provctx, OSSL_PARAM params[]) { P_TEST_CTX *ctx = (P_TEST_CTX *)provctx; const OSSL_CORE_HANDLE *hand = ctx->handle; OSSL_PARAM *p = params; int ok = 1; for (; ok && p->key != NULL; p++) { if (strcmp(p->key, "greeting") == 0) { static char *opensslv; static char *provname; static char *greeting; static OSSL_PARAM counter_request[] = { { "openssl-version", OSSL_PARAM_UTF8_PTR, &opensslv, sizeof(&opensslv), 0 }, { "provider-name", OSSL_PARAM_UTF8_PTR, &provname, sizeof(&provname), 0}, { "greeting", OSSL_PARAM_UTF8_PTR, &greeting, sizeof(&greeting), 0 }, { NULL, 0, NULL, 0, 0 } }; char buf[256]; size_t buf_l; opensslv = provname = greeting = NULL; if (c_get_params(hand, counter_request)) { if (greeting) { strcpy(buf, greeting); } else { const char *versionp = *(void **)counter_request[0].data; const char *namep = *(void **)counter_request[1].data; sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!", versionp, namep); } } else { sprintf(buf, "Howdy stranger..."); } p->return_size = buf_l = strlen(buf) + 1; if (p->data_size >= buf_l) strcpy(p->data, buf); else ok = 0; } else if (strcmp(p->key, "digest-check") == 0) { unsigned int digestsuccess = 0; #ifdef PROVIDER_INIT_FUNCTION_NAME EVP_MD *md4 = EVP_MD_fetch(ctx->libctx, "MD4", NULL); EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); const char *msg = "Hello world"; unsigned char out[16]; OSSL_PROVIDER *deflt; deflt = OSSL_PROVIDER_load(ctx->libctx, "default"); if (deflt == NULL || !OSSL_PROVIDER_available(ctx->libctx, "default")) { p_set_error(ERR_LIB_PROV, 3, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL); ok = 0; } if (ok && OSSL_PROVIDER_available(ctx->libctx, "default") && OSSL_PROVIDER_available(ctx->libctx, "base") && OSSL_PROVIDER_available(ctx->libctx, "legacy") && OSSL_PROVIDER_available(ctx->libctx, "p_test") && md4 != NULL && mdctx != NULL) { if (EVP_DigestInit_ex(mdctx, md4, NULL) && EVP_DigestUpdate(mdctx, (const unsigned char *)msg, strlen(msg)) && EVP_DigestFinal(mdctx, out, NULL)) digestsuccess = 1; } EVP_MD_CTX_free(mdctx); EVP_MD_free(md4); OSSL_PROVIDER_unload(deflt); #endif if (p->data_size >= sizeof(digestsuccess)) { *(unsigned int *)p->data = digestsuccess; p->return_size = sizeof(digestsuccess); } else { ok = 0; } } else if (strcmp(p->key, "stop-property-mirror") == 0) { unsigned int stopsuccess = 0; #ifdef PROVIDER_INIT_FUNCTION_NAME stopsuccess = EVP_set_default_properties(ctx->libctx, NULL); #endif if (p->data_size >= sizeof(stopsuccess)) { *(unsigned int *)p->data = stopsuccess; p->return_size = sizeof(stopsuccess); } else { ok = 0; } } } return ok; } static const OSSL_ITEM *p_get_reason_strings(void *_) { static const OSSL_ITEM reason_strings[] = { {1, "dummy reason string"}, {2, "Can't create child library context"}, {3, "Can't load default provider"}, {0, NULL} }; return reason_strings; } static const OSSL_DISPATCH p_test_table[] = { { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params }, { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params }, { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (void (*)(void))p_get_reason_strings}, { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown }, OSSL_DISPATCH_END }; int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *oin, const OSSL_DISPATCH **out, void **provctx) { P_TEST_CTX *ctx; const OSSL_DISPATCH *in = oin; for (; in->function_id != 0; in++) { switch (in->function_id) { case OSSL_FUNC_CORE_GETTABLE_PARAMS: c_gettable_params = OSSL_FUNC_core_gettable_params(in); break; case OSSL_FUNC_CORE_GET_PARAMS: c_get_params = OSSL_FUNC_core_get_params(in); break; case OSSL_FUNC_CORE_NEW_ERROR: c_new_error = OSSL_FUNC_core_new_error(in); break; case OSSL_FUNC_CORE_SET_ERROR_DEBUG: c_set_error_debug = OSSL_FUNC_core_set_error_debug(in); break; case OSSL_FUNC_CORE_VSET_ERROR: c_vset_error = OSSL_FUNC_core_vset_error(in); break; default: break; } } ctx = malloc(sizeof(*ctx)); if (ctx == NULL) return 0; ctx->thisfile = strdup(OPENSSL_FILE); ctx->thisfunc = strdup(OPENSSL_FUNC); ctx->handle = handle; #ifdef PROVIDER_INIT_FUNCTION_NAME ctx->libctx = OSSL_LIB_CTX_new_child(handle, oin); if (ctx->libctx == NULL) { p_set_error(ERR_LIB_PROV, 2, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL); p_teardown(ctx); return 0; } { EVP_MD *sha256 = EVP_MD_fetch(ctx->libctx, "SHA2-256", NULL); if (sha256 != NULL) { EVP_MD_free(sha256); p_teardown(ctx); return 0; } } #endif p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL); *provctx = (void *)ctx; *out = p_test_table; return 1; } static void p_teardown(void *provctx) { P_TEST_CTX *ctx = (P_TEST_CTX *)provctx; #ifdef PROVIDER_INIT_FUNCTION_NAME OSSL_LIB_CTX_free(ctx->libctx); #endif free(ctx->thisfile); free(ctx->thisfunc); free(ctx); }
test
openssl/test/p_test.c
openssl
#include <openssl/x509.h> #include "testutil.h" static EVP_PKEY *pubkey = NULL; static EVP_PKEY *privkey = NULL; static EVP_MD *signmd = NULL; static const unsigned char privkeydata[] = { 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x7d, 0x2b, 0xfe, 0x5c, 0xcb, 0xcb, 0x27, 0xd6, 0x28, 0xfe, 0x98, 0x34, 0x84, 0x4a, 0x13, 0x6f, 0x70, 0xc4, 0x1a, 0x0b, 0xfc, 0xde, 0xb0, 0xb2, 0x32, 0xb1, 0xdd, 0x4f, 0x0e, 0xbc, 0xdf, 0x89, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d }; static const unsigned char pubkeydata[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d }; static const unsigned char certdata[] = { 0x30, 0x82, 0x01, 0x86, 0x30, 0x82, 0x01, 0x2d, 0x02, 0x14, 0x75, 0xd6, 0x04, 0xd2, 0x80, 0x61, 0xd3, 0x32, 0xbc, 0xae, 0x38, 0x58, 0xfe, 0x12, 0x42, 0x81, 0x7a, 0xdd, 0x0b, 0x99, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30, 0x37, 0x32, 0x37, 0x35, 0x35, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x35, 0x30, 0x30, 0x32, 0x32, 0x37, 0x30, 0x37, 0x32, 0x37, 0x35, 0x35, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x82, 0xd9, 0xc9, 0x4b, 0x19, 0x43, 0x45, 0x6b, 0xd4, 0x50, 0x64, 0x9b, 0xd5, 0x8d, 0x5a, 0xd9, 0xdc, 0xc9, 0x24, 0x23, 0x7a, 0x3b, 0x48, 0x23, 0xe2, 0x2a, 0x24, 0xf2, 0x9c, 0x6f, 0x87, 0xd0, 0xc4, 0x0f, 0xcc, 0x7e, 0x7c, 0x8d, 0xfc, 0x08, 0x46, 0x37, 0x85, 0x4f, 0x5b, 0x3a, 0x0b, 0x97, 0xd7, 0x57, 0x2a, 0x5a, 0x6b, 0x7a, 0x0b, 0xe4, 0xe8, 0x9c, 0x4a, 0xbb, 0xbf, 0x09, 0x4d, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x5f, 0x45, 0x7f, 0xa4, 0x6a, 0x03, 0xfd, 0xe7, 0xf3, 0x42, 0x43, 0x38, 0x5b, 0x81, 0x08, 0x1a, 0x47, 0x8e, 0x59, 0x3a, 0x28, 0x5b, 0x97, 0x67, 0x47, 0x66, 0x2a, 0x16, 0xf5, 0xce, 0xf5, 0x92, 0x02, 0x20, 0x22, 0x0e, 0xab, 0x35, 0xdf, 0x49, 0xb1, 0x86, 0xa3, 0x3b, 0x26, 0xda, 0x7e, 0x8b, 0x44, 0x45, 0xc6, 0x46, 0x14, 0x04, 0x22, 0x2b, 0xe5, 0x2a, 0x62, 0x84, 0xc5, 0x94, 0xa0, 0x1b, 0xaa, 0xa9 }; static const unsigned char crldata[] = { 0x30, 0x81, 0x8B, 0x30, 0x31, 0x02, 0x01, 0x01, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x05, 0x00, 0x30, 0x0F, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x04, 0x54, 0x65, 0x73, 0x74, 0x17, 0x0D, 0x32, 0x32, 0x31, 0x30, 0x31, 0x32, 0x30, 0x35, 0x33, 0x34, 0x30, 0x31, 0x5A, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x05, 0x00, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x75, 0xAC, 0xA9, 0xB5, 0xFE, 0x63, 0x09, 0x8B, 0x57, 0x4F, 0xBB, 0xC6, 0x0C, 0xA9, 0x9A, 0x7C, 0x55, 0x89, 0xF9, 0x9C, 0x48, 0xE9, 0xF3, 0xED, 0xE5, 0xC2, 0x88, 0xCE, 0xEC, 0xB1, 0x51, 0xF1, 0x02, 0x21, 0x00, 0x8B, 0x93, 0xC5, 0xA6, 0x28, 0x48, 0x5A, 0x4E, 0x10, 0x52, 0x82, 0x12, 0x2F, 0xC4, 0x62, 0x2D, 0x3F, 0x5A, 0x62, 0x7F, 0x9D, 0x1B, 0x12, 0xC5, 0x36, 0x25, 0x73, 0x03, 0xF4, 0xDE, 0x62, 0x24 }; static int test_x509_tbs_cache(void) { int ret; X509 *x = NULL; const unsigned char *p = certdata; ret = TEST_ptr(x = d2i_X509(NULL, &p, sizeof(certdata))) && TEST_int_gt(X509_sign(x, privkey, signmd), 0) && TEST_int_eq(X509_verify(x, pubkey), 1); X509_free(x); return ret; } static int test_x509_crl_tbs_cache(void) { int ret; X509_CRL *crl = NULL; const unsigned char *p = crldata; ret = TEST_ptr(crl = d2i_X509_CRL(NULL, &p, sizeof(crldata))) && TEST_int_gt(X509_CRL_sign(crl, privkey, signmd), 0) && TEST_int_eq(X509_CRL_verify(crl, pubkey), 1); X509_CRL_free(crl); return ret; } int setup_tests(void) { const unsigned char *p; p = pubkeydata; pubkey = d2i_PUBKEY(NULL, &p, sizeof(pubkeydata)); p = privkeydata; privkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(privkeydata)); if (pubkey == NULL || privkey == NULL) { BIO_printf(bio_err, "Failed to create keys\n"); return 0; } signmd = EVP_MD_fetch(NULL, "SHA384", NULL); if (signmd == NULL) { BIO_printf(bio_err, "Failed to fetch digest\n"); return 0; } ADD_TEST(test_x509_tbs_cache); ADD_TEST(test_x509_crl_tbs_cache); return 1; } void cleanup_tests(void) { EVP_MD_free(signmd); EVP_PKEY_free(pubkey); EVP_PKEY_free(privkey); }
test
openssl/test/x509_test.c
openssl
#include <string.h> #include <openssl/ssl.h> #include <openssl/bio.h> #include <openssl/err.h> #include "internal/packet.h" #include "helpers/ssltestlib.h" #include "testutil.h" static int fragment = 0; static char *cert = NULL; static char *privkey = NULL; static int async_new(BIO *bi); static int async_free(BIO *a); static int async_read(BIO *b, char *out, int outl); static int async_write(BIO *b, const char *in, int inl); static long async_ctrl(BIO *b, int cmd, long num, void *ptr); static int async_gets(BIO *bp, char *buf, int size); static int async_puts(BIO *bp, const char *str); # define BIO_TYPE_ASYNC_FILTER (0x80 | BIO_TYPE_FILTER) static BIO_METHOD *methods_async = NULL; struct async_ctrs { unsigned int rctr; unsigned int wctr; }; static const BIO_METHOD *bio_f_async_filter(void) { if (methods_async == NULL) { methods_async = BIO_meth_new(BIO_TYPE_ASYNC_FILTER, "Async filter"); if ( methods_async == NULL || !BIO_meth_set_write(methods_async, async_write) || !BIO_meth_set_read(methods_async, async_read) || !BIO_meth_set_puts(methods_async, async_puts) || !BIO_meth_set_gets(methods_async, async_gets) || !BIO_meth_set_ctrl(methods_async, async_ctrl) || !BIO_meth_set_create(methods_async, async_new) || !BIO_meth_set_destroy(methods_async, async_free)) return NULL; } return methods_async; } static int async_new(BIO *bio) { struct async_ctrs *ctrs; ctrs = OPENSSL_zalloc(sizeof(struct async_ctrs)); if (ctrs == NULL) return 0; BIO_set_data(bio, ctrs); BIO_set_init(bio, 1); return 1; } static int async_free(BIO *bio) { struct async_ctrs *ctrs; if (bio == NULL) return 0; ctrs = BIO_get_data(bio); OPENSSL_free(ctrs); BIO_set_data(bio, NULL); BIO_set_init(bio, 0); return 1; } static int async_read(BIO *bio, char *out, int outl) { struct async_ctrs *ctrs; int ret = 0; BIO *next = BIO_next(bio); if (outl <= 0) return 0; if (next == NULL) return 0; ctrs = BIO_get_data(bio); BIO_clear_retry_flags(bio); if (ctrs->rctr > 0) { ret = BIO_read(next, out, 1); if (ret <= 0 && BIO_should_read(next)) BIO_set_retry_read(bio); ctrs->rctr = 0; } else { ctrs->rctr++; BIO_set_retry_read(bio); } return ret; } #define MIN_RECORD_LEN 6 #define CONTENTTYPEPOS 0 #define VERSIONHIPOS 1 #define VERSIONLOPOS 2 #define DATAPOS 5 static int async_write(BIO *bio, const char *in, int inl) { struct async_ctrs *ctrs; int ret = 0; size_t written = 0; BIO *next = BIO_next(bio); if (inl <= 0) return 0; if (next == NULL) return 0; ctrs = BIO_get_data(bio); BIO_clear_retry_flags(bio); if (ctrs->wctr > 0) { ctrs->wctr = 0; if (fragment) { PACKET pkt; if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl)) return -1; while (PACKET_remaining(&pkt) > 0) { PACKET payload, wholebody, sessionid, extensions; unsigned int contenttype, versionhi, versionlo, data; unsigned int msgtype = 0, negversion = 0; if (!PACKET_get_1(&pkt, &contenttype) || !PACKET_get_1(&pkt, &versionhi) || !PACKET_get_1(&pkt, &versionlo) || !PACKET_get_length_prefixed_2(&pkt, &payload)) return -1; written += SSL3_RT_HEADER_LENGTH; wholebody = payload; if (contenttype == SSL3_RT_HANDSHAKE && !PACKET_get_1(&wholebody, &msgtype)) return -1; if (msgtype == SSL3_MT_SERVER_HELLO) { if (!PACKET_forward(&wholebody, SSL3_HM_HEADER_LENGTH - 1) || !PACKET_get_net_2(&wholebody, &negversion) || !PACKET_forward(&wholebody, 32) || !PACKET_get_length_prefixed_1(&wholebody, &sessionid) || !PACKET_forward(&wholebody, 2 + 1) || !PACKET_get_length_prefixed_2(&wholebody, &extensions)) return -1; while (PACKET_remaining(&extensions)) { unsigned int type; PACKET extbody; if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extbody)) return -1; if (type == TLSEXT_TYPE_supported_versions && (!PACKET_get_net_2(&extbody, &negversion) || PACKET_remaining(&extbody) != 0)) return -1; } } while (PACKET_get_1(&payload, &data)) { char smallrec[MIN_RECORD_LEN] = { 0, 0, 0, 0, 1, 0 }; smallrec[CONTENTTYPEPOS] = contenttype; smallrec[VERSIONHIPOS] = versionhi; smallrec[VERSIONLOPOS] = versionlo; smallrec[DATAPOS] = data; ret = BIO_write(next, smallrec, MIN_RECORD_LEN); if (ret <= 0) return -1; written++; } if (contenttype == SSL3_RT_CHANGE_CIPHER_SPEC || (negversion == TLS1_3_VERSION && msgtype == SSL3_MT_SERVER_HELLO)) { fragment = 0; break; } } } ret = 0; if ((int)written < inl) { ret = BIO_write(next, in + written, inl - written); } if (ret <= 0 && BIO_should_write(next)) BIO_set_retry_write(bio); else ret += written; } else { ctrs->wctr++; BIO_set_retry_write(bio); } return ret; } static long async_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 async_gets(BIO *bio, char *buf, int size) { return -1; } static int async_puts(BIO *bio, const char *str) { return async_write(bio, str, strlen(str)); } #define MAX_ATTEMPTS 100 static int test_asyncio(int test) { SSL_CTX *serverctx = NULL, *clientctx = NULL; SSL *serverssl = NULL, *clientssl = NULL; BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL; int testresult = 0, ret; size_t i, j; const char testdata[] = "Test data"; char buf[sizeof(testdata)]; if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &serverctx, &clientctx, cert, privkey))) goto end; if (test == 1) fragment = 1; s_to_c_fbio = BIO_new(bio_f_async_filter()); c_to_s_fbio = BIO_new(bio_f_async_filter()); if (!TEST_ptr(s_to_c_fbio) || !TEST_ptr(c_to_s_fbio)) { BIO_free(s_to_c_fbio); BIO_free(c_to_s_fbio); goto end; } if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl, s_to_c_fbio, c_to_s_fbio)) || !TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) goto end; for (j = 0; j < 2; j++) { int len; for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2; i++) { ret = SSL_write(clientssl, testdata + len, sizeof(testdata) - len); if (ret > 0) { len += ret; } else { int ssl_error = SSL_get_error(clientssl, ret); if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL || ssl_error == SSL_ERROR_SSL)) goto end; } } if (!TEST_size_t_eq(len, sizeof(testdata))) goto end; for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < MAX_ATTEMPTS; i++) { ret = SSL_read(serverssl, buf + len, sizeof(buf) - len); if (ret > 0) { len += ret; } else { int ssl_error = SSL_get_error(serverssl, ret); if (!TEST_false(ssl_error == SSL_ERROR_SYSCALL || ssl_error == SSL_ERROR_SSL)) goto end; } } if (!TEST_mem_eq(testdata, sizeof(testdata), buf, len)) goto end; } SSL_free(clientssl); SSL_free(serverssl); clientssl = serverssl = NULL; testresult = 1; end: SSL_free(clientssl); SSL_free(serverssl); SSL_CTX_free(clientctx); SSL_CTX_free(serverctx); return testresult; } OPT_TEST_DECLARE_USAGE("certname privkey\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_asyncio, 2); return 1; } void cleanup_tests(void) { BIO_meth_free(methods_async); }
test
openssl/test/asynciotest.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/e_os2.h> #include <openssl/ssl.h> #include <openssl/ssl3.h> #include <openssl/tls1.h> #include "internal/nelem.h" #include "testutil.h" typedef struct cipherlist_test_fixture { const char *test_case_name; SSL_CTX *server; SSL_CTX *client; } CIPHERLIST_TEST_FIXTURE; static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture) { if (fixture != NULL) { SSL_CTX_free(fixture->server); SSL_CTX_free(fixture->client); fixture->server = fixture->client = NULL; OPENSSL_free(fixture); } } static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name) { CIPHERLIST_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method())) || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) { tear_down(fixture); return NULL; } return fixture; } static const uint32_t default_ciphers_in_order[] = { #ifndef OPENSSL_NO_TLS1_3 TLS1_3_CK_AES_256_GCM_SHA384, # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) TLS1_3_CK_CHACHA20_POLY1305_SHA256, # endif TLS1_3_CK_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_2 # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, # endif # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305, # endif # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, # endif #endif #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, # endif #ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_256_SHA, # endif # ifndef OPENSSL_NO_EC TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, # endif # ifndef OPENSSL_NO_DH TLS1_CK_DHE_RSA_WITH_AES_128_SHA, # endif #endif #ifndef OPENSSL_NO_TLS1_2 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, #endif #ifndef OPENSSL_NO_TLS1_2 TLS1_CK_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256, #endif #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) TLS1_CK_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, #endif }; static int test_default_cipherlist(SSL_CTX *ctx) { STACK_OF(SSL_CIPHER) *ciphers = NULL; SSL *ssl = NULL; int i, ret = 0, num_expected_ciphers, num_ciphers; uint32_t expected_cipher_id, cipher_id; if (ctx == NULL) return 0; if (!TEST_ptr(ssl = SSL_new(ctx)) || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))) goto err; num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order); num_ciphers = sk_SSL_CIPHER_num(ciphers); if (!TEST_int_eq(num_ciphers, num_expected_ciphers)) goto err; for (i = 0; i < num_ciphers; i++) { expected_cipher_id = default_ciphers_in_order[i]; cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i)); if (!TEST_int_eq(cipher_id, expected_cipher_id)) { TEST_info("Wrong cipher at position %d", i); goto err; } } ret = 1; err: sk_SSL_CIPHER_free(ciphers); SSL_free(ssl); return ret; } static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture) { return fixture != NULL && test_default_cipherlist(fixture->server) && test_default_cipherlist(fixture->client); } #define SETUP_CIPHERLIST_TEST_FIXTURE() \ SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up) #define EXECUTE_CIPHERLIST_TEST() \ EXECUTE_TEST(execute_test, tear_down) static int test_default_cipherlist_implicit(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); EXECUTE_CIPHERLIST_TEST(); return result; } static int test_default_cipherlist_explicit(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT")) || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) { tear_down(fixture); fixture = NULL; } EXECUTE_CIPHERLIST_TEST(); return result; } static int test_default_cipherlist_clear(void) { SSL *s = NULL; SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0)) goto end; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH)) goto end; s = SSL_new(fixture->client); if (!TEST_ptr(s)) goto end; if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0)) goto end; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH)) goto end; result = 1; end: SSL_free(s); tear_down(fixture); return result; } static int test_stdname_cipherlist(void) { SETUP_CIPHERLIST_TEST_FIXTURE(); if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, TLS1_RFC_RSA_WITH_AES_128_SHA)) || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, TLS1_RFC_RSA_WITH_AES_128_SHA))) { goto end; } result = 1; end: tear_down(fixture); fixture = NULL; return result; } int setup_tests(void) { ADD_TEST(test_default_cipherlist_implicit); ADD_TEST(test_default_cipherlist_explicit); ADD_TEST(test_default_cipherlist_clear); ADD_TEST(test_stdname_cipherlist); return 1; }
test
openssl/test/cipherlist_test.c
openssl
#include <openssl/crypto.h> #include <openssl/provider.h> #include <openssl/decoder.h> #include <openssl/encoder.h> #include <openssl/store.h> #include <openssl/rand.h> #include <openssl/core_names.h> #include "testutil.h" static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection, OSSL_CALLBACK *object_cb, void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { return 0; } static const OSSL_DISPATCH dummy_decoder_functions[] = { { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_decoders[] = { { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions }, { NULL, NULL, NULL } }; static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out, const void *obj_raw, const OSSL_PARAM obj_abstract[], int selection, OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) { return 0; } static const OSSL_DISPATCH dummy_encoder_functions[] = { { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_encoders[] = { { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions }, { NULL, NULL, NULL } }; static void *dummy_store_open(void *provctx, const char *uri) { return NULL; } static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb, void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { return 0; } static int dumm_store_eof(void *loaderctx) { return 0; } static int dummy_store_close(void *loaderctx) { return 0; } static const OSSL_DISPATCH dummy_store_functions[] = { { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open }, { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load }, { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof }, { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_store[] = { { "DUMMY", "provider=dummy", dummy_store_functions }, { NULL, NULL, NULL } }; static void *dummy_rand_newctx(void *provctx, void *parent, const OSSL_DISPATCH *parent_calls) { return provctx; } static void dummy_rand_freectx(void *vctx) { } static int dummy_rand_instantiate(void *vdrbg, unsigned int strength, int prediction_resistance, const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[]) { return 1; } static int dummy_rand_uninstantiate(void *vdrbg) { return 1; } static int dummy_rand_generate(void *vctx, unsigned char *out, size_t outlen, unsigned int strength, int prediction_resistance, const unsigned char *addin, size_t addin_len) { size_t i; for (i = 0; i <outlen; i++) out[i] = (unsigned char)(i & 0xff); return 1; } static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx) { static const OSSL_PARAM known_gettable_ctx_params[] = { OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), OSSL_PARAM_END }; return known_gettable_ctx_params; } static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[]) { OSSL_PARAM *p; p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX)) return 0; return 1; } static int dummy_rand_enable_locking(void *vtest) { return 1; } static int dummy_rand_lock(void *vtest) { return 1; } static void dummy_rand_unlock(void *vtest) { } static const OSSL_DISPATCH dummy_rand_functions[] = { { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))dummy_rand_newctx }, { OSSL_FUNC_RAND_FREECTX, (void (*)(void))dummy_rand_freectx }, { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))dummy_rand_instantiate }, { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))dummy_rand_uninstantiate }, { OSSL_FUNC_RAND_GENERATE, (void (*)(void))dummy_rand_generate }, { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, (void(*)(void))dummy_rand_gettable_ctx_params }, { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))dummy_rand_get_ctx_params }, { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))dummy_rand_enable_locking }, { OSSL_FUNC_RAND_LOCK, (void(*)(void))dummy_rand_lock }, { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))dummy_rand_unlock }, OSSL_DISPATCH_END }; static const OSSL_ALGORITHM dummy_rand[] = { { "DUMMY", "provider=dummy", dummy_rand_functions }, { NULL, NULL, NULL } }; static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id, int *no_cache) { *no_cache = 0; switch (operation_id) { case OSSL_OP_DECODER: return dummy_decoders; case OSSL_OP_ENCODER: return dummy_encoders; case OSSL_OP_STORE: return dummy_store; case OSSL_OP_RAND: return dummy_rand; } return NULL; } static const OSSL_DISPATCH dummy_dispatch_table[] = { { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query }, { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, OSSL_DISPATCH_END }; static int dummy_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in); unsigned char buf[32]; *provctx = (void *)libctx; *out = dummy_dispatch_table; if (RAND_bytes_ex(libctx, buf, sizeof(buf), 0) <= 0) return 0; return 1; } static int fetch_test(int tst) { OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); OSSL_PROVIDER *dummyprov = NULL; OSSL_PROVIDER *nullprov = NULL; OSSL_DECODER *decoder = NULL; OSSL_ENCODER *encoder = NULL; OSSL_STORE_LOADER *loader = NULL; int testresult = 0; unsigned char buf[32]; int query = tst > 3; if (!TEST_ptr(libctx)) goto err; if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov", dummy_provider_init)) || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default")) || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov"))) goto err; switch (tst % 4) { case 0: decoder = OSSL_DECODER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(decoder)) goto err; break; case 1: encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(encoder)) goto err; break; case 2: loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", query ? "provider=dummy" : NULL); if (!TEST_ptr(loader)) goto err; break; case 3: if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY", query ? "provider=dummy" : NULL, NULL, NULL)) || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1)) goto err; break; default: goto err; } testresult = 1; err: OSSL_DECODER_free(decoder); OSSL_ENCODER_free(encoder); OSSL_STORE_LOADER_free(loader); OSSL_PROVIDER_unload(dummyprov); OSSL_PROVIDER_unload(nullprov); OSSL_LIB_CTX_free(libctx); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(fetch_test, 8); return 1; }
test
openssl/test/provfetchtest.c
openssl
#include "helpers/cmp_testlib.h" typedef struct test_fixture { const char *test_case_name; int expected; OSSL_CMP_SRV_CTX *srv_ctx; OSSL_CMP_MSG *req; } CMP_SRV_TEST_FIXTURE; static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; static OSSL_CMP_MSG *request = NULL; static void tear_down(CMP_SRV_TEST_FIXTURE *fixture) { OSSL_CMP_SRV_CTX_free(fixture->srv_ctx); OPENSSL_free(fixture); } static CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_SRV_TEST_FIXTURE *fixture; if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) return NULL; fixture->test_case_name = test_case_name; if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new(libctx, NULL))) goto err; return fixture; err: tear_down(fixture); return NULL; } static int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *cert_req, int certReqId, const OSSL_CRMF_MSG *crm, const X509_REQ *p10cr, X509 **certOut, STACK_OF(X509) **chainOut, STACK_OF(X509) **caPubs) { ERR_raise(ERR_LIB_CMP, dummy_errorCode); return NULL; } static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture) { OSSL_CMP_SRV_CTX *ctx = fixture->srv_ctx; OSSL_CMP_CTX *client_ctx; OSSL_CMP_CTX *cmp_ctx; char *dummy_custom_ctx = "@test_dummy", *custom_ctx; OSSL_CMP_MSG *rsp = NULL; OSSL_CMP_ERRORMSGCONTENT *errorContent; int res = 0; if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new(libctx, NULL)) || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx))) goto end; if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx, process_cert_request, NULL, NULL, NULL, NULL, NULL)) || !TEST_true(OSSL_CMP_SRV_CTX_init_trans(ctx, NULL, NULL)) || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx)) || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0)) goto end; if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0)) || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0)) || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1)) || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1))) goto end; if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx)) || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx, (unsigned char *)"server", 6) || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx, (unsigned char *)"1234", 4)) goto end; if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req)) || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp), OSSL_CMP_PKIBODY_ERROR) || !TEST_ptr(errorContent = rsp->body->value.error) || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode), ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode))) goto end; res = 1; end: OSSL_CMP_MSG_free(rsp); OSSL_CMP_CTX_free(client_ctx); return res; } static int test_handle_request(void) { SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up); fixture->req = request; fixture->expected = 1; EXECUTE_TEST(execute_test_handle_request, tear_down); return result; } void cleanup_tests(void) { OSSL_CMP_MSG_free(request); OSSL_PROVIDER_unload(default_null_provider); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); return; } #define USAGE \ "CR_protected_PBM_1234.der module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { const char *request_f; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(request_f = test_get_argument(0))) { TEST_error("usage: cmp_server_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE)) return 0; if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) { cleanup_tests(); return 0; } ADD_TEST(test_handle_request); return 1; }
test
openssl/test/cmp_server_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include "internal/nelem.h" #include <openssl/crypto.h> #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/rand.h> #include <openssl/err.h> #include "testutil.h" #include "internal/ffc.h" #include "crypto/security_bits.h" #ifndef OPENSSL_NO_DSA static const unsigned char dsa_2048_224_sha224_p[] = { 0x93, 0x57, 0x93, 0x62, 0x1b, 0x9a, 0x10, 0x9b, 0xc1, 0x56, 0x0f, 0x24, 0x71, 0x76, 0x4e, 0xd3, 0xed, 0x78, 0x78, 0x7a, 0xbf, 0x89, 0x71, 0x67, 0x8e, 0x03, 0xd8, 0x5b, 0xcd, 0x22, 0x8f, 0x70, 0x74, 0xff, 0x22, 0x05, 0x07, 0x0c, 0x4c, 0x60, 0xed, 0x41, 0xe1, 0x9e, 0x9c, 0xaa, 0x3e, 0x19, 0x5c, 0x3d, 0x80, 0x58, 0xb2, 0x7f, 0x5f, 0x89, 0xec, 0xb5, 0x19, 0xdb, 0x06, 0x11, 0xe9, 0x78, 0x5c, 0xf9, 0xa0, 0x9e, 0x70, 0x62, 0x14, 0x7b, 0xda, 0x92, 0xbf, 0xb2, 0x6b, 0x01, 0x6f, 0xb8, 0x68, 0x9c, 0x89, 0x36, 0x89, 0x72, 0x79, 0x49, 0x93, 0x3d, 0x14, 0xb2, 0x2d, 0xbb, 0xf0, 0xdf, 0x94, 0x45, 0x0b, 0x5f, 0xf1, 0x75, 0x37, 0xeb, 0x49, 0xb9, 0x2d, 0xce, 0xb7, 0xf4, 0x95, 0x77, 0xc2, 0xe9, 0x39, 0x1c, 0x4e, 0x0c, 0x40, 0x62, 0x33, 0x0a, 0xe6, 0x29, 0x6f, 0xba, 0xef, 0x02, 0xdd, 0x0d, 0xe4, 0x04, 0x01, 0x70, 0x40, 0xb9, 0xc9, 0x7e, 0x2f, 0x10, 0x37, 0xe9, 0xde, 0xb0, 0xf6, 0xeb, 0x71, 0x7f, 0x9c, 0x35, 0x16, 0xf3, 0x0d, 0xc4, 0xe8, 0x02, 0x37, 0x6c, 0xdd, 0xb3, 0x8d, 0x2d, 0x1e, 0x28, 0x13, 0x22, 0x89, 0x40, 0xe5, 0xfa, 0x16, 0x67, 0xd6, 0xda, 0x12, 0xa2, 0x38, 0x83, 0x25, 0xcc, 0x26, 0xc1, 0x27, 0x74, 0xfe, 0xf6, 0x7a, 0xb6, 0xa1, 0xe4, 0xe8, 0xdf, 0x5d, 0xd2, 0x9c, 0x2f, 0xec, 0xea, 0x08, 0xca, 0x48, 0xdb, 0x18, 0x4b, 0x12, 0xee, 0x16, 0x9b, 0xa6, 0x00, 0xa0, 0x18, 0x98, 0x7d, 0xce, 0x6c, 0x6d, 0xf8, 0xfc, 0x95, 0x51, 0x1b, 0x0a, 0x40, 0xb6, 0xfc, 0xe5, 0xe2, 0xb0, 0x26, 0x53, 0x4c, 0xd7, 0xfe, 0xaa, 0x6d, 0xbc, 0xdd, 0xc0, 0x61, 0x65, 0xe4, 0x89, 0x44, 0x18, 0x6f, 0xd5, 0x39, 0xcf, 0x75, 0x6d, 0x29, 0xcc, 0xf8, 0x40, 0xab }; static const unsigned char dsa_2048_224_sha224_q[] = { 0xf2, 0x5e, 0x4e, 0x9a, 0x15, 0xa8, 0x13, 0xdf, 0xa3, 0x17, 0x90, 0xc6, 0xd6, 0x5e, 0xb1, 0xfb, 0x31, 0xf8, 0xb5, 0xb1, 0x4b, 0xa7, 0x6d, 0xde, 0x57, 0x76, 0x6f, 0x11 }; static const unsigned char dsa_2048_224_sha224_seed[] = { 0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85, 0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3, 0x36, 0x17, 0x06, 0xcf }; static const unsigned char dsa_2048_224_sha224_bad_seed[] = { 0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85, 0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3, 0x36, 0x17, 0x06, 0xd0 }; static int dsa_2048_224_sha224_counter = 2878; static const unsigned char dsa_3072_256_sha512_p[] = { 0x9a, 0x82, 0x8b, 0x8d, 0xea, 0xd0, 0x56, 0x23, 0x88, 0x2d, 0x5d, 0x41, 0x42, 0x4c, 0x13, 0x5a, 0x15, 0x81, 0x59, 0x02, 0xc5, 0x00, 0x82, 0x28, 0x01, 0xee, 0x8f, 0x99, 0xfd, 0x6a, 0x95, 0xf2, 0x0f, 0xae, 0x34, 0x77, 0x29, 0xcc, 0xc7, 0x50, 0x0e, 0x03, 0xef, 0xb0, 0x4d, 0xe5, 0x10, 0x00, 0xa8, 0x7b, 0xce, 0x8c, 0xc6, 0xb2, 0x01, 0x74, 0x23, 0x1b, 0x7f, 0xe8, 0xf9, 0x71, 0x28, 0x39, 0xcf, 0x18, 0x04, 0xb2, 0x95, 0x61, 0x2d, 0x11, 0x71, 0x6b, 0xdd, 0x0d, 0x0b, 0xf0, 0xe6, 0x97, 0x52, 0x29, 0x9d, 0x45, 0xb1, 0x23, 0xda, 0xb0, 0xd5, 0xcb, 0x51, 0x71, 0x8e, 0x40, 0x9c, 0x97, 0x13, 0xea, 0x1f, 0x4b, 0x32, 0x5d, 0x27, 0x74, 0x81, 0x8d, 0x47, 0x8a, 0x08, 0xce, 0xf4, 0xd1, 0x28, 0xa2, 0x0f, 0x9b, 0x2e, 0xc9, 0xa3, 0x0e, 0x5d, 0xde, 0x47, 0x19, 0x6d, 0x5f, 0x98, 0xe0, 0x8e, 0x7f, 0x60, 0x8f, 0x25, 0xa7, 0xa4, 0xeb, 0xb9, 0xf3, 0x24, 0xa4, 0x9e, 0xc1, 0xbd, 0x14, 0x27, 0x7c, 0x27, 0xc8, 0x4f, 0x5f, 0xed, 0xfd, 0x86, 0xc8, 0xf1, 0xd7, 0x82, 0xe2, 0xeb, 0xe5, 0xd2, 0xbe, 0xb0, 0x65, 0x28, 0xab, 0x99, 0x9e, 0xcd, 0xd5, 0x22, 0xf8, 0x1b, 0x3b, 0x01, 0xe9, 0x20, 0x3d, 0xe4, 0x98, 0x22, 0xfe, 0xfc, 0x09, 0x7e, 0x95, 0x20, 0xda, 0xb6, 0x12, 0x2c, 0x94, 0x5c, 0xea, 0x74, 0x71, 0xbd, 0x19, 0xac, 0x78, 0x43, 0x02, 0x51, 0xb8, 0x5f, 0x06, 0x1d, 0xea, 0xc8, 0xa4, 0x3b, 0xc9, 0x78, 0xa3, 0x2b, 0x09, 0xdc, 0x76, 0x74, 0xc4, 0x23, 0x14, 0x48, 0x2e, 0x84, 0x2b, 0xa3, 0x82, 0xc1, 0xba, 0x0b, 0x39, 0x2a, 0x9f, 0x24, 0x7b, 0xd6, 0xc2, 0xea, 0x5a, 0xb6, 0xbd, 0x15, 0x82, 0x21, 0x85, 0xe0, 0x6b, 0x12, 0x4f, 0x8d, 0x64, 0x75, 0xeb, 0x7e, 0xa1, 0xdb, 0xe0, 0x9d, 0x25, 0xae, 0x3b, 0xe9, 0x9b, 0x21, 0x7f, 0x9a, 0x3d, 0x66, 0xd0, 0x52, 0x1d, 0x39, 0x8b, 0xeb, 0xfc, 0xec, 0xbe, 0x72, 0x20, 0x5a, 0xdf, 0x1b, 0x00, 0xf1, 0x0e, 0xed, 0xc6, 0x78, 0x6f, 0xc9, 0xab, 0xe4, 0xd6, 0x81, 0x8b, 0xcc, 0xf6, 0xd4, 0x6a, 0x31, 0x62, 0x08, 0xd9, 0x38, 0x21, 0x8f, 0xda, 0x9e, 0xb1, 0x2b, 0x9c, 0xc0, 0xbe, 0xf7, 0x9a, 0x43, 0x2d, 0x07, 0x59, 0x46, 0x0e, 0xd5, 0x23, 0x4e, 0xaa, 0x4a, 0x04, 0xc2, 0xde, 0x33, 0xa6, 0x34, 0xba, 0xac, 0x4f, 0x78, 0xd8, 0xca, 0x76, 0xce, 0x5e, 0xd4, 0xf6, 0x85, 0x4c, 0x6a, 0x60, 0x08, 0x5d, 0x0e, 0x34, 0x8b, 0xf2, 0xb6, 0xe3, 0xb7, 0x51, 0xca, 0x43, 0xaa, 0x68, 0x7b, 0x0a, 0x6e, 0xea, 0xce, 0x1e, 0x2c, 0x34, 0x8e, 0x0f, 0xe2, 0xcc, 0x38, 0xf2, 0x9a, 0x98, 0xef, 0xe6, 0x7f, 0xf6, 0x62, 0xbb }; static const unsigned char dsa_3072_256_sha512_q[] = { 0xc1, 0xdb, 0xc1, 0x21, 0x50, 0x49, 0x63, 0xa3, 0x77, 0x6d, 0x4c, 0x92, 0xed, 0x58, 0x9e, 0x98, 0xea, 0xac, 0x7a, 0x90, 0x13, 0x24, 0xf7, 0xcd, 0xd7, 0xe6, 0xd4, 0x8f, 0xf0, 0x45, 0x4b, 0xf7 }; static const unsigned char dsa_3072_256_sha512_seed[] = { 0x35, 0x24, 0xb5, 0x59, 0xd5, 0x27, 0x58, 0x10, 0xf6, 0xa2, 0x7c, 0x9a, 0x0d, 0xc2, 0x70, 0x8a, 0xb0, 0x41, 0x4a, 0x84, 0x0b, 0xfe, 0x66, 0xf5, 0x3a, 0xbf, 0x4a, 0xa9, 0xcb, 0xfc, 0xa6, 0x22 }; static int dsa_3072_256_sha512_counter = 1604; static const unsigned char dsa_2048_224_sha256_p[] = { 0xe9, 0x13, 0xbc, 0xf2, 0x14, 0x5d, 0xf9, 0x79, 0xd6, 0x6d, 0xf5, 0xc5, 0xbe, 0x7b, 0x6f, 0x90, 0x63, 0xd0, 0xfd, 0xee, 0x4f, 0xc4, 0x65, 0x83, 0xbf, 0xec, 0xc3, 0x2c, 0x5d, 0x30, 0xc8, 0xa4, 0x3b, 0x2f, 0x3b, 0x29, 0x43, 0x69, 0xfb, 0x6e, 0xa9, 0xa4, 0x07, 0x6c, 0xcd, 0xb0, 0xd2, 0xd9, 0xd3, 0xe6, 0xf4, 0x87, 0x16, 0xb7, 0xe5, 0x06, 0xb9, 0xba, 0xd6, 0x87, 0xbc, 0x01, 0x9e, 0xba, 0xc2, 0xcf, 0x39, 0xb6, 0xec, 0xdc, 0x75, 0x07, 0xc1, 0x39, 0x2d, 0x6a, 0x95, 0x31, 0x97, 0xda, 0x54, 0x20, 0x29, 0xe0, 0x1b, 0xf9, 0x74, 0x65, 0xaa, 0xc1, 0x47, 0xd3, 0x9e, 0xb4, 0x3c, 0x1d, 0xe0, 0xdc, 0x2d, 0x21, 0xab, 0x12, 0x3b, 0xa5, 0x51, 0x1e, 0xc6, 0xbc, 0x6b, 0x4c, 0x22, 0xd1, 0x7c, 0xc6, 0xce, 0xcb, 0x8c, 0x1d, 0x1f, 0xce, 0x1c, 0xe2, 0x75, 0x49, 0x6d, 0x2c, 0xee, 0x7f, 0x5f, 0xb8, 0x74, 0x42, 0x5c, 0x96, 0x77, 0x13, 0xff, 0x80, 0xf3, 0x05, 0xc7, 0xfe, 0x08, 0x3b, 0x25, 0x36, 0x46, 0xa2, 0xc4, 0x26, 0xb4, 0xb0, 0x3b, 0xd5, 0xb2, 0x4c, 0x13, 0x29, 0x0e, 0x47, 0x31, 0x66, 0x7d, 0x78, 0x57, 0xe6, 0xc2, 0xb5, 0x9f, 0x46, 0x17, 0xbc, 0xa9, 0x9a, 0x49, 0x1c, 0x0f, 0x45, 0xe0, 0x88, 0x97, 0xa1, 0x30, 0x7c, 0x42, 0xb7, 0x2c, 0x0a, 0xce, 0xb3, 0xa5, 0x7a, 0x61, 0x8e, 0xab, 0x44, 0xc1, 0xdc, 0x70, 0xe5, 0xda, 0x78, 0x2a, 0xb4, 0xe6, 0x3c, 0xa0, 0x58, 0xda, 0x62, 0x0a, 0xb2, 0xa9, 0x3d, 0xaa, 0x49, 0x7e, 0x7f, 0x9a, 0x19, 0x67, 0xee, 0xd6, 0xe3, 0x67, 0x13, 0xe8, 0x6f, 0x79, 0x50, 0x76, 0xfc, 0xb3, 0x9d, 0x7e, 0x9e, 0x3e, 0x6e, 0x47, 0xb1, 0x11, 0x5e, 0xc8, 0x83, 0x3a, 0x3c, 0xfc, 0x82, 0x5c, 0x9d, 0x34, 0x65, 0x73, 0xb4, 0x56, 0xd5 }; static const unsigned char dsa_2048_224_sha256_q[] = { 0xb0, 0xdf, 0xa1, 0x7b, 0xa4, 0x77, 0x64, 0x0e, 0xb9, 0x28, 0xbb, 0xbc, 0xd4, 0x60, 0x02, 0xaf, 0x21, 0x8c, 0xb0, 0x69, 0x0f, 0x8a, 0x7b, 0xc6, 0x80, 0xcb, 0x0a, 0x45 }; static const unsigned char dsa_2048_224_sha256_g[] = { 0x11, 0x7c, 0x5f, 0xf6, 0x99, 0x44, 0x67, 0x5b, 0x69, 0xa3, 0x83, 0xef, 0xb5, 0x85, 0xa2, 0x19, 0x35, 0x18, 0x2a, 0xf2, 0x58, 0xf4, 0xc9, 0x58, 0x9e, 0xb9, 0xe8, 0x91, 0x17, 0x2f, 0xb0, 0x60, 0x85, 0x95, 0xa6, 0x62, 0x36, 0xd0, 0xff, 0x94, 0xb9, 0xa6, 0x50, 0xad, 0xa6, 0xf6, 0x04, 0x28, 0xc2, 0xc9, 0xb9, 0x75, 0xf3, 0x66, 0xb4, 0xeb, 0xf6, 0xd5, 0x06, 0x13, 0x01, 0x64, 0x82, 0xa9, 0xf1, 0xd5, 0x41, 0xdc, 0xf2, 0x08, 0xfc, 0x2f, 0xc4, 0xa1, 0x21, 0xee, 0x7d, 0xbc, 0xda, 0x5a, 0xa4, 0xa2, 0xb9, 0x68, 0x87, 0x36, 0xba, 0x53, 0x9e, 0x14, 0x4e, 0x76, 0x5c, 0xba, 0x79, 0x3d, 0x0f, 0xe5, 0x99, 0x1c, 0x27, 0xfc, 0xaf, 0x10, 0x63, 0x87, 0x68, 0x0e, 0x3e, 0x6e, 0xaa, 0xf3, 0xdf, 0x76, 0x7e, 0x02, 0x9a, 0x41, 0x96, 0xa1, 0x6c, 0xbb, 0x67, 0xee, 0x0c, 0xad, 0x72, 0x65, 0xf1, 0x70, 0xb0, 0x39, 0x9b, 0x54, 0x5f, 0xd7, 0x6c, 0xc5, 0x9a, 0x90, 0x53, 0x18, 0xde, 0x5e, 0x62, 0x89, 0xb9, 0x2f, 0x66, 0x59, 0x3a, 0x3d, 0x10, 0xeb, 0xa5, 0x99, 0xf6, 0x21, 0x7d, 0xf2, 0x7b, 0x42, 0x15, 0x1c, 0x55, 0x79, 0x15, 0xaa, 0xa4, 0x17, 0x2e, 0x48, 0xc3, 0xa8, 0x36, 0xf5, 0x1a, 0x97, 0xce, 0xbd, 0x72, 0xef, 0x1d, 0x50, 0x5b, 0xb1, 0x60, 0x0a, 0x5c, 0x0b, 0xa6, 0x21, 0x38, 0x28, 0x4e, 0x89, 0x33, 0x1d, 0xb5, 0x7e, 0x5c, 0xf1, 0x6b, 0x2c, 0xbd, 0xad, 0x84, 0xb2, 0x8e, 0x96, 0xe2, 0x30, 0xe7, 0x54, 0xb8, 0xc9, 0x70, 0xcb, 0x10, 0x30, 0x63, 0x90, 0xf4, 0x45, 0x64, 0x93, 0x09, 0x38, 0x6a, 0x47, 0x58, 0x31, 0x04, 0x1a, 0x18, 0x04, 0x1a, 0xe0, 0xd7, 0x0b, 0x3c, 0xbe, 0x2a, 0x9c, 0xec, 0xcc, 0x0d, 0x0c, 0xed, 0xde, 0x54, 0xbc, 0xe6, 0x93, 0x59, 0xfc }; static int ffc_params_validate_g_unverified_test(void) { int ret = 0, res; FFC_PARAMS params; BIGNUM *p = NULL, *q = NULL, *g = NULL; BIGNUM *p1 = NULL, *g1 = NULL; ossl_ffc_params_init(&params); if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha256_p, sizeof(dsa_2048_224_sha256_p), NULL))) goto err; p1 = p; if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha256_q, sizeof(dsa_2048_224_sha256_q), NULL))) goto err; if (!TEST_ptr(g = BN_bin2bn(dsa_2048_224_sha256_g, sizeof(dsa_2048_224_sha256_g), NULL))) goto err; g1 = g; ossl_ffc_params_set0_pqg(&params, p, q, NULL); p = NULL; q = NULL; ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G); ossl_ffc_set_digest(&params, "SHA256", NULL); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ossl_ffc_params_set0_pqg(&params, p, q, g); g = NULL; if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; BN_add_word(g1, 1); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; BN_set_word(g1, 1); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; BN_copy(g1, p1); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ret = 1; err: ossl_ffc_params_cleanup(&params); BN_free(p); BN_free(q); BN_free(g); return ret; } static int ffc_params_validate_pq_test(void) { int ret = 0, res = -1; FFC_PARAMS params; BIGNUM *p = NULL, *q = NULL; ossl_ffc_params_init(&params); if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha224_p, sizeof(dsa_2048_224_sha224_p), NULL))) goto err; if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha224_q, sizeof(dsa_2048_224_sha224_q), NULL))) goto err; ossl_ffc_params_set0_pqg(&params, NULL, q, NULL); q = NULL; ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_PQ); ossl_ffc_set_digest(&params, "SHA224", NULL); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ossl_ffc_params_set0_pqg(&params, p, NULL, NULL); p = NULL; ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed, sizeof(dsa_2048_224_sha224_seed), dsa_2048_224_sha224_counter); if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed, sizeof(dsa_2048_224_sha224_seed), 1); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed, sizeof(dsa_2048_224_sha224_seed)-1, dsa_2048_224_sha224_counter); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_bad_seed, sizeof(dsa_2048_224_sha224_bad_seed), dsa_2048_224_sha224_counter); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; if (!TEST_ptr(p = BN_bin2bn(dsa_3072_256_sha512_p, sizeof(dsa_3072_256_sha512_p), NULL))) goto err; if (!TEST_ptr(q = BN_bin2bn(dsa_3072_256_sha512_q, sizeof(dsa_3072_256_sha512_q), NULL))) goto err; ossl_ffc_params_set0_pqg(&params, p, q, NULL); p = q = NULL; ossl_ffc_set_digest(&params, "SHA512", NULL); ossl_ffc_params_set_validate_params(&params, dsa_3072_256_sha512_seed, sizeof(dsa_3072_256_sha512_seed), dsa_3072_256_sha512_counter); if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DH, &res, NULL))) goto err; ret = 1; err: ossl_ffc_params_cleanup(&params); BN_free(p); BN_free(q); return ret; } #endif #ifndef OPENSSL_NO_DH static int ffc_params_gen_test(void) { int ret = 0, res = -1; FFC_PARAMS params; ossl_ffc_params_init(&params); if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params, FFC_PARAM_TYPE_DH, 2048, 256, &res, NULL))) goto err; if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DH, &res, NULL))) goto err; ret = 1; err: ossl_ffc_params_cleanup(&params); return ret; } static int ffc_params_gen_canonicalg_test(void) { int ret = 0, res = -1; FFC_PARAMS params; ossl_ffc_params_init(&params); params.gindex = 1; if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params, FFC_PARAM_TYPE_DH, 2048, 256, &res, NULL))) goto err; if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DH, &res, NULL))) goto err; if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4))) goto err; ret = 1; err: ossl_ffc_params_cleanup(&params); return ret; } static int ffc_params_fips186_2_gen_validate_test(void) { int ret = 0, res = -1; FFC_PARAMS params; BIGNUM *bn = NULL; ossl_ffc_params_init(&params); if (!TEST_ptr(bn = BN_new())) goto err; if (!TEST_true(ossl_ffc_params_FIPS186_2_generate(NULL, &params, FFC_PARAM_TYPE_DH, 1024, 160, &res, NULL))) goto err; if (!TEST_true(ossl_ffc_params_FIPS186_2_validate(NULL, &params, FFC_PARAM_TYPE_DH, &res, NULL))) goto err; if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL))) goto err; if (!TEST_true(res == FFC_CHECK_Q_MISMATCH || res == FFC_CHECK_Q_NOT_PRIME)) goto err; ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G); if (!TEST_int_eq(ossl_ffc_params_FIPS186_4_validate(NULL, &params, FFC_PARAM_TYPE_DSA, &res, NULL), 2)) goto err; if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4))) goto err; ret = 1; err: BN_free(bn); ossl_ffc_params_cleanup(&params); return ret; } extern FFC_PARAMS *ossl_dh_get0_params(DH *dh); static int ffc_public_validate_test(void) { int ret = 0, res = -1; FFC_PARAMS *params; BIGNUM *pub = NULL; DH *dh = NULL; if (!TEST_ptr(pub = BN_new())) goto err; if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048))) goto err; params = ossl_dh_get0_params(dh); if (!TEST_true(BN_set_word(pub, 1))) goto err; BN_set_negative(pub, 1); if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res)) goto err; if (!TEST_true(BN_set_word(pub, 0))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res)) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, BN_value_one(), &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res)) goto err; if (!TEST_true(BN_add_word(pub, 2))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_ptr(BN_copy(pub, params->p))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res)) goto err; if (!TEST_true(BN_sub_word(pub, 1))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res)) goto err; if (!TEST_true(BN_sub_word(pub, 1))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PUBKEY_INVALID, res)) goto err; if (!TEST_true(BN_sub_word(pub, 5))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_true(ossl_ffc_validate_public_key(NULL, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res)) goto err; res = -1; if (!TEST_true(ossl_ffc_validate_public_key(params, NULL, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res)) goto err; res = -1; BN_free(params->p); params->p = NULL; if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res)) goto err; ret = 1; err: DH_free(dh); BN_free(pub); return ret; } static int ffc_private_validate_test(void) { int ret = 0, res = -1; FFC_PARAMS *params; BIGNUM *priv = NULL; DH *dh = NULL; if (!TEST_ptr(priv = BN_new())) goto err; if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048))) goto err; params = ossl_dh_get0_params(dh); if (!TEST_true(BN_set_word(priv, 1))) goto err; BN_set_negative(priv, 1); if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res)) goto err; if (!TEST_true(BN_set_word(priv, 0))) goto err; if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res)) goto err; if (!TEST_true(ossl_ffc_validate_private_key(params->q, BN_value_one(), &res))) goto err; if (!TEST_ptr(BN_copy(priv, params->q))) goto err; if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_LARGE, res)) goto err; if (!TEST_true(BN_sub_word(priv, 1))) goto err; if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_false(ossl_ffc_validate_private_key(NULL, priv, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res)) goto err; res = -1; if (!TEST_false(ossl_ffc_validate_private_key(params->q, NULL, &res))) goto err; if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res)) goto err; ret = 1; err: DH_free(dh); BN_free(priv); return ret; } static int ffc_private_gen_test(int index) { int ret = 0, res = -1, N; FFC_PARAMS *params; BIGNUM *priv = NULL; DH *dh = NULL; BN_CTX *ctx = NULL; if (!TEST_ptr(ctx = BN_CTX_new_ex(NULL))) goto err; if (!TEST_ptr(priv = BN_new())) goto err; if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048))) goto err; params = ossl_dh_get0_params(dh); N = BN_num_bits(params->q); if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, 220, 112, priv))) goto err; if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, N + 1, 112, priv))) goto err; if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, N, 0, priv))) goto err; if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N, 112, priv))) goto err; if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N / 2, 112, priv))) goto err; if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, 0, ossl_ifc_ffc_compute_security_bits(BN_num_bits(params->p)), priv))) goto err; if (!TEST_int_le(BN_num_bits(priv), 225)) goto err; if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res))) goto err; ret = 1; err: DH_free(dh); BN_free(priv); BN_CTX_free(ctx); return ret; } static int ffc_params_copy_test(void) { int ret = 0; DH *dh = NULL; FFC_PARAMS *params, copy; ossl_ffc_params_init(&copy); if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe3072))) goto err; params = ossl_dh_get0_params(dh); if (!TEST_int_eq(params->keylength, 275)) goto err; if (!TEST_true(ossl_ffc_params_copy(&copy, params))) goto err; if (!TEST_int_eq(copy.keylength, 275)) goto err; if (!TEST_true(ossl_ffc_params_cmp(&copy, params, 0))) goto err; ret = 1; err: ossl_ffc_params_cleanup(&copy); DH_free(dh); return ret; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_DSA ADD_TEST(ffc_params_validate_pq_test); ADD_TEST(ffc_params_validate_g_unverified_test); #endif #ifndef OPENSSL_NO_DH ADD_TEST(ffc_params_gen_test); ADD_TEST(ffc_params_gen_canonicalg_test); ADD_TEST(ffc_params_fips186_2_gen_validate_test); ADD_TEST(ffc_public_validate_test); ADD_TEST(ffc_private_validate_test); ADD_ALL_TESTS(ffc_private_gen_test, 10); ADD_TEST(ffc_params_copy_test); #endif return 1; }
test
openssl/test/ffc_internal_test.c
openssl
#include <openssl/types.h> #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include <windows.h> # include <wincrypt.h> # ifndef X509_NAME # ifndef PEDANTIC # ifdef _MSC_VER # pragma message("wincrypt.h no longer defining X509_NAME before OpenSSL headers") # else # warning "wincrypt.h no longer defining X509_NAME before OpenSSL headers" # endif # endif # endif #endif #include <openssl/opensslconf.h> #ifndef OPENSSL_NO_STDIO # include <stdio.h> #endif #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/x509v3.h> int main(void) { return 0; }
test
openssl/test/build_wincrypt_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include "apps_ui.h" #include "testutil.h" #include <openssl/ui.h> static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata) { OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size); return strlen(buf); } static int test_old(void) { UI_METHOD *ui_method = NULL; UI *ui = NULL; char defpass[] = "password"; char pass[16]; int ok = 0; if (!TEST_ptr(ui_method = UI_UTIL_wrap_read_pem_callback(test_pem_password_cb, 0)) || !TEST_ptr(ui = UI_new_method(ui_method))) goto err; UI_add_user_data(ui, defpass); if (UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD, pass, 0, sizeof(pass) - 1) <= 0) goto err; switch (UI_process(ui)) { case -2: TEST_info("test_old: UI process interrupted or cancelled"); case -1: goto err; default: break; } if (TEST_str_eq(pass, defpass)) ok = 1; err: UI_free(ui); UI_destroy_method(ui_method); return ok; } static int test_new_ui(void) { PW_CB_DATA cb_data = { "password", "prompt" }; char pass[16]; int ok = 0; (void)setup_ui_method(); if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0) && TEST_str_eq(pass, cb_data.password)) ok = 1; destroy_ui_method(); return ok; } int setup_tests(void) { ADD_TEST(test_old); ADD_TEST(test_new_ui); return 1; }
test
openssl/test/uitest.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/safestack.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 typedef struct { int n; char c; } SS; typedef union { int n; char c; } SU; DEFINE_SPECIAL_STACK_OF(sint, int) DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char) DEFINE_STACK_OF(SS) DEFINE_STACK_OF_CONST(SU) static int int_compare(const int *const *a, const int *const *b) { if (**a < **b) return -1; if (**a > **b) return 1; return 0; } static int test_int_stack(int reserve) { static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 }; static int notpresent = -1; const int n = OSSL_NELEM(v); static struct { int value; int unsorted; int sorted; int ex; } finds[] = { { 2, 1, 5, 5 }, { 9, 7, 6, 6 }, { -173, 5, 0, 0 }, { 999, 3, 8, 8 }, { 0, -1, -1, 1 } }; const int n_finds = OSSL_NELEM(finds); static struct { int value; int ex; } exfinds[] = { { 3, 5 }, { 1000, 8 }, { 20, 8 }, { -999, 0 }, { -5, 0 }, { 8, 5 } }; const int n_exfinds = OSSL_NELEM(exfinds); STACK_OF(sint) *s = sk_sint_new_null(); int i; int testresult = 0; if (!TEST_ptr(s) || (reserve > 0 && !TEST_true(sk_sint_reserve(s, 5 * reserve)))) goto end; for (i = 0; i < n; i++) { if (!TEST_int_eq(sk_sint_num(s), i)) { TEST_info("int stack size %d", i); goto end; } sk_sint_push(s, v + i); } if (!TEST_int_eq(sk_sint_num(s), n)) goto end; for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) { TEST_info("int value %d", i); goto end; } for (i = 0; i < n_finds; i++) { int *val = (finds[i].unsorted == -1) ? &notpresent : v + finds[i].unsorted; if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) { TEST_info("int unsorted find %d", i); goto end; } } for (i = 0; i < n_finds; i++) { int *val = (finds[i].unsorted == -1) ? &notpresent : v + finds[i].unsorted; if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) { TEST_info("int unsorted find_ex %d", i); goto end; } } if (!TEST_false(sk_sint_is_sorted(s))) goto end; (void)sk_sint_set_cmp_func(s, &int_compare); sk_sint_sort(s); if (!TEST_true(sk_sint_is_sorted(s))) goto end; for (i = 0; i < n_finds; i++) if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) { TEST_info("int sorted find %d", i); goto end; } for (i = 0; i < n_finds; i++) if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) { TEST_info("int sorted find_ex present %d", i); goto end; } for (i = 0; i < n_exfinds; i++) if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)) { TEST_info("int sorted find_ex absent %d", i); goto end; } if (!TEST_ptr_eq(sk_sint_shift(s), v + 6)) goto end; testresult = 1; end: sk_sint_free(s); return testresult; } static int uchar_compare(const unsigned char *const *a, const unsigned char *const *b) { return **a - (signed int)**b; } static int test_uchar_stack(int reserve) { static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 }; const int n = OSSL_NELEM(v); STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL; int i; int testresult = 0; if (!TEST_ptr(s) || (reserve > 0 && !TEST_true(sk_uchar_reserve(s, 5 * reserve)))) goto end; for (i = 0; i < n; i++) { if (!TEST_int_eq(sk_uchar_num(s), i)) { TEST_info("uchar stack size %d", i); goto end; } sk_uchar_unshift(s, v + i); } if (!TEST_int_eq(sk_uchar_num(s), n)) goto end; r = sk_uchar_dup(NULL); if (sk_uchar_num(r) != 0) goto end; sk_uchar_free(r); r = sk_uchar_dup(s); if (!TEST_int_eq(sk_uchar_num(r), n)) goto end; sk_uchar_sort(r); for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) { TEST_info("uchar pop %d", i); goto end; } sk_uchar_free(s); s = NULL; if (!TEST_int_eq(sk_uchar_num(r), n)) goto end; sk_uchar_zero(r); if (!TEST_int_eq(sk_uchar_num(r), 0)) goto end; sk_uchar_insert(r, v, 0); sk_uchar_insert(r, v + 2, -1); sk_uchar_insert(r, v + 1, 1); for (i = 0; i < 3; i++) if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) { TEST_info("uchar insert %d", i); goto end; } if (!TEST_ptr_null(sk_uchar_delete(r, 12))) goto end; if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1)) goto end; (void)sk_uchar_set(r, 1, v + 1); for (i = 0; i < 2; i++) if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) { TEST_info("uchar set %d", i); goto end; } testresult = 1; end: sk_uchar_free(r); sk_uchar_free(s); return testresult; } static SS *SS_copy(const SS *p) { SS *q = OPENSSL_malloc(sizeof(*q)); if (q != NULL) memcpy(q, p, sizeof(*q)); return q; } static void SS_free(SS *p) { OPENSSL_free(p); } static int test_SS_stack(void) { STACK_OF(SS) *s = sk_SS_new_null(); STACK_OF(SS) *r = NULL; SS *v[10], *p; const int n = OSSL_NELEM(v); int i; int testresult = 0; for (i = 0; i < n; i++) { v[i] = OPENSSL_malloc(sizeof(*v[i])); if (!TEST_ptr(v[i])) goto end; v[i]->n = i; v[i]->c = 'A' + i; if (!TEST_int_eq(sk_SS_num(s), i)) { TEST_info("SS stack size %d", i); goto end; } sk_SS_push(s, v[i]); } if (!TEST_int_eq(sk_SS_num(s), n)) goto end; r = sk_SS_deep_copy(NULL, &SS_copy, &SS_free); if (sk_SS_num(r) != 0) goto end; sk_SS_free(r); r = sk_SS_deep_copy(s, &SS_copy, &SS_free); if (!TEST_ptr(r)) goto end; for (i = 0; i < n; i++) { p = sk_SS_value(r, i); if (!TEST_ptr_ne(p, v[i])) { TEST_info("SS deepcopy non-copy %d", i); goto end; } if (!TEST_int_eq(p->n, v[i]->n)) { TEST_info("test SS deepcopy int %d", i); goto end; } if (!TEST_char_eq(p->c, v[i]->c)) { TEST_info("SS deepcopy char %d", i); goto end; } } sk_SS_pop_free(r, &SS_free); r = NULL; p = sk_SS_delete_ptr(s, v[3]); if (!TEST_ptr(p)) goto end; SS_free(p); if (!TEST_int_eq(sk_SS_num(s), n - 1)) goto end; for (i = 0; i < n-1; i++) if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) { TEST_info("SS delete ptr item %d", i); goto end; } testresult = 1; end: sk_SS_pop_free(r, &SS_free); sk_SS_pop_free(s, &SS_free); return testresult; } static int test_SU_stack(void) { STACK_OF(SU) *s = sk_SU_new_null(); SU v[10]; const int n = OSSL_NELEM(v); int i; int testresult = 0; for (i = 0; i < n; i++) { if ((i & 1) == 0) v[i].n = i; else v[i].c = 'A' + i; if (!TEST_int_eq(sk_SU_num(s), i)) { TEST_info("SU stack size %d", i); goto end; } sk_SU_push(s, v + i); } if (!TEST_int_eq(sk_SU_num(s), n)) goto end; for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_SU_value(s, i), v + i)) { TEST_info("SU pointer check %d", i); goto end; } testresult = 1; end: sk_SU_free(s); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_int_stack, 4); ADD_ALL_TESTS(test_uchar_stack, 4); ADD_TEST(test_SS_stack); ADD_TEST(test_SU_stack); return 1; }
test
openssl/test/stack_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/ssl.h> #include "testutil.h" #include "internal/nelem.h" #include "../ssl/ssl_local.h" #include "../ssl/ssl_cert_table.h" #define test_cert_table(nid, amask, idx) \ do_test_cert_table(nid, amask, idx, #idx) static int do_test_cert_table(int nid, uint32_t amask, size_t idx, const char *idxname) { const SSL_CERT_LOOKUP *clu = &ssl_cert_info[idx]; if (clu->nid == nid && clu->amask == amask) return 1; TEST_error("Invalid table entry for certificate type %s, index %zu", idxname, idx); if (clu->nid != nid) TEST_note("Expected %s, got %s\n", OBJ_nid2sn(nid), OBJ_nid2sn(clu->nid)); if (clu->amask != amask) TEST_note("Expected auth mask 0x%x, got 0x%x\n", (unsigned int)amask, (unsigned int)clu->amask); return 0; } static int test_ssl_cert_table(void) { return TEST_size_t_eq(OSSL_NELEM(ssl_cert_info), SSL_PKEY_NUM) && test_cert_table(EVP_PKEY_RSA, SSL_aRSA, SSL_PKEY_RSA) && test_cert_table(EVP_PKEY_DSA, SSL_aDSS, SSL_PKEY_DSA_SIGN) && test_cert_table(EVP_PKEY_EC, SSL_aECDSA, SSL_PKEY_ECC) && test_cert_table(NID_id_GostR3410_2001, SSL_aGOST01, SSL_PKEY_GOST01) && test_cert_table(NID_id_GostR3410_2012_256, SSL_aGOST12, SSL_PKEY_GOST12_256) && test_cert_table(NID_id_GostR3410_2012_512, SSL_aGOST12, SSL_PKEY_GOST12_512) && test_cert_table(EVP_PKEY_ED25519, SSL_aECDSA, SSL_PKEY_ED25519) && test_cert_table(EVP_PKEY_ED448, SSL_aECDSA, SSL_PKEY_ED448); } int setup_tests(void) { ADD_TEST(test_ssl_cert_table); return 1; }
test
openssl/test/ssl_cert_table_internal_test.c
openssl
#include "testutil.h" #include <openssl/ssl.h> #include "internal/quic_cc.h" #include "internal/priority_queue.h" static OSSL_TIME fake_time = {0}; #define TIME_BASE (ossl_ticks2time(5 * OSSL_TIME_SECOND)) static OSSL_TIME fake_now(void *arg) { return fake_time; } static void step_time(uint32_t ms) { fake_time = ossl_time_add(fake_time, ossl_ms2time(ms)); } typedef struct net_pkt_st { OSSL_TIME tx_time; OSSL_TIME arrive_time; OSSL_TIME determination_time; OSSL_TIME next_time; int success; int arrived; size_t size; size_t idx; } NET_PKT; DEFINE_PRIORITY_QUEUE_OF(NET_PKT); static int net_pkt_cmp(const NET_PKT *a, const NET_PKT *b) { return ossl_time_compare(a->next_time, b->next_time); } struct net_sim { const OSSL_CC_METHOD *ccm; OSSL_CC_DATA *cc; uint64_t capacity; uint64_t latency; uint64_t spare_capacity; PRIORITY_QUEUE_OF(NET_PKT) *pkts; uint64_t total_acked, total_lost; }; static int net_sim_init(struct net_sim *s, const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc, uint64_t capacity, uint64_t latency) { s->ccm = ccm; s->cc = cc; s->capacity = capacity; s->latency = latency; s->spare_capacity = capacity; s->total_acked = 0; s->total_lost = 0; if (!TEST_ptr(s->pkts = ossl_pqueue_NET_PKT_new(net_pkt_cmp))) return 0; return 1; } static void do_free(NET_PKT *pkt) { OPENSSL_free(pkt); } static void net_sim_cleanup(struct net_sim *s) { ossl_pqueue_NET_PKT_pop_free(s->pkts, do_free); } static int net_sim_process(struct net_sim *s, size_t skip_forward); static int net_sim_send(struct net_sim *s, size_t sz) { NET_PKT *pkt = OPENSSL_zalloc(sizeof(*pkt)); int success; if (!TEST_ptr(pkt)) return 0; if (!TEST_true(net_sim_process(s, 0))) goto err; success = (sz <= s->spare_capacity); pkt->tx_time = fake_time; pkt->success = success; if (success) { pkt->arrive_time = ossl_time_add(pkt->tx_time, ossl_ms2time(s->latency)); pkt->determination_time = ossl_time_add(pkt->arrive_time, ossl_ms2time(s->latency)); pkt->next_time = pkt->arrive_time; s->spare_capacity -= sz; } else { pkt->arrive_time = ossl_time_add(pkt->tx_time, ossl_ms2time(s->latency)); pkt->determination_time = ossl_time_add(pkt->tx_time, ossl_ms2time(3 * s->latency)); pkt->next_time = pkt->determination_time; } pkt->size = sz; if (!TEST_true(s->ccm->on_data_sent(s->cc, sz))) goto err; if (!TEST_true(ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx))) goto err; return 1; err: OPENSSL_free(pkt); return 0; } static int net_sim_process_one(struct net_sim *s, int skip_forward) { NET_PKT *pkt = ossl_pqueue_NET_PKT_peek(s->pkts); if (pkt == NULL) return 3; if (skip_forward && ossl_time_compare(pkt->next_time, fake_time) > 0) fake_time = pkt->next_time; if (pkt->success && !pkt->arrived && ossl_time_compare(fake_time, pkt->arrive_time) >= 0) { s->spare_capacity += pkt->size; pkt->arrived = 1; ossl_pqueue_NET_PKT_pop(s->pkts); pkt->next_time = pkt->determination_time; if (!ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx)) return 0; return 1; } if (ossl_time_compare(fake_time, pkt->determination_time) < 0) return 2; if (!TEST_true(!pkt->success || pkt->arrived)) return 0; if (!pkt->success) { OSSL_CC_LOSS_INFO loss_info = {0}; loss_info.tx_time = pkt->tx_time; loss_info.tx_size = pkt->size; if (!TEST_true(s->ccm->on_data_lost(s->cc, &loss_info))) return 0; if (!TEST_true(s->ccm->on_data_lost_finished(s->cc, 0))) return 0; s->total_lost += pkt->size; ossl_pqueue_NET_PKT_pop(s->pkts); OPENSSL_free(pkt); } else { OSSL_CC_ACK_INFO ack_info = {0}; ack_info.tx_time = pkt->tx_time; ack_info.tx_size = pkt->size; if (!TEST_true(s->ccm->on_data_acked(s->cc, &ack_info))) return 0; s->total_acked += pkt->size; ossl_pqueue_NET_PKT_pop(s->pkts); OPENSSL_free(pkt); } return 1; } static int net_sim_process(struct net_sim *s, size_t skip_forward) { int rc; while ((rc = net_sim_process_one(s, skip_forward > 0 ? 1 : 0)) == 1) if (skip_forward > 0) --skip_forward; return rc; } #ifdef GENERATE_LOG static FILE *logfile; #endif static int dump_state(const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc, struct net_sim *s) { #ifdef GENERATE_LOG uint64_t cwnd_size, cur_bytes, state; if (logfile == NULL) return 1; if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_CWND_SIZE, &cwnd_size))) return 0; if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT, &cur_bytes))) return 0; if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_STATE, &state))) return 0; fprintf(logfile, "%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,\"%c\"\n", ossl_time2ms(fake_time), ccm->get_tx_allowance(cc), cwnd_size, cur_bytes, s->total_acked, s->total_lost, s->capacity, s->spare_capacity, (char)state); #endif return 1; } static int test_simulate(void) { int testresult = 0; int rc; int have_sim = 0; const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method; OSSL_CC_DATA *cc = NULL; size_t mdpl = 1472; uint64_t total_sent = 0, total_to_send, allowance; uint64_t actual_capacity = 16000; uint64_t cwnd_sample_sum = 0, cwnd_sample_count = 0; uint64_t diag_cur_bytes_in_flight = UINT64_MAX; uint64_t diag_cur_cwnd_size = UINT64_MAX; struct net_sim sim; OSSL_PARAM params[3], *p = params; fake_time = TIME_BASE; if (!TEST_ptr(cc = ccm->new(fake_now, NULL))) goto err; if (!TEST_true(net_sim_init(&sim, ccm, cc, actual_capacity, 100))) goto err; have_sim = 1; *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN, &mdpl); *p++ = OSSL_PARAM_construct_end(); if (!TEST_true(ccm->set_input_params(cc, params))) goto err; p = params; *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT, &diag_cur_bytes_in_flight); *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_CWND_SIZE, &diag_cur_cwnd_size); *p++ = OSSL_PARAM_construct_end(); if (!TEST_true(ccm->bind_diagnostics(cc, params))) goto err; ccm->reset(cc); if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), mdpl)) goto err; total_to_send = 30 * 1024 * 1024; while (total_sent < total_to_send) { for (;;) { uint64_t sz; dump_state(ccm, cc, &sim); allowance = ccm->get_tx_allowance(cc); sz = allowance > mdpl ? mdpl : allowance; if (sz > SIZE_MAX) sz = SIZE_MAX; if (sz < 30) break; step_time(7); if (!TEST_true(net_sim_send(&sim, (size_t)sz))) goto err; total_sent += sz; } rc = net_sim_process(&sim, 1); if (!TEST_int_gt(rc, 0)) goto err; if (rc == 3) { if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0)) goto err; if (!TEST_uint64_t_ge(ccm->get_tx_allowance(cc), mdpl)) goto err; } { uint64_t v = 1; if (!TEST_uint64_t_ne(diag_cur_bytes_in_flight, UINT64_MAX) || !TEST_uint64_t_ne(diag_cur_cwnd_size, UINT64_MAX)) goto err; cwnd_sample_sum += v; ++cwnd_sample_count; } } { uint64_t estimated_capacity = cwnd_sample_sum / cwnd_sample_count; double error = ((double)estimated_capacity / (double)actual_capacity) - 1.0; TEST_info("est = %6llu kB/s, act=%6llu kB/s (error=%.02f%%)\n", (unsigned long long)estimated_capacity, (unsigned long long)actual_capacity, error * 100.0); if (!TEST_double_le(error, 0.05)) goto err; } testresult = 1; err: if (have_sim) net_sim_cleanup(&sim); if (cc != NULL) ccm->free(cc); #ifdef GENERATE_LOG if (logfile != NULL) fflush(logfile); #endif return testresult; } static int test_sanity(void) { int testresult = 0; OSSL_CC_DATA *cc = NULL; const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method; OSSL_CC_LOSS_INFO loss_info = {0}; OSSL_CC_ACK_INFO ack_info = {0}; uint64_t allowance, allowance2; OSSL_PARAM params[3], *p = params; size_t mdpl = 1472, diag_mdpl = SIZE_MAX; uint64_t diag_cur_bytes_in_flight = UINT64_MAX; fake_time = TIME_BASE; if (!TEST_ptr(cc = ccm->new(fake_now, NULL))) goto err; *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN, &mdpl); *p++ = OSSL_PARAM_construct_end(); if (!TEST_true(ccm->set_input_params(cc, params))) goto err; ccm->reset(cc); p = params; *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN, &diag_mdpl); *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT, &diag_cur_bytes_in_flight); *p++ = OSSL_PARAM_construct_end(); if (!TEST_true(ccm->bind_diagnostics(cc, params)) || !TEST_size_t_eq(diag_mdpl, 1472)) goto err; if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1472)) goto err; if (!TEST_true(ossl_time_is_zero(ccm->get_wakeup_deadline(cc)))) goto err; if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0)) goto err; if (!TEST_true(ccm->on_data_sent(cc, 1200))) goto err; if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200)) goto err; ack_info.tx_time = fake_time; ack_info.tx_size = 1200; step_time(100); if (!TEST_true(ccm->on_data_acked(cc, &ack_info))) goto err; if (!TEST_uint64_t_ge(allowance2 = ccm->get_tx_allowance(cc), allowance)) goto err; if (!TEST_true(ccm->on_data_sent(cc, 1200))) goto err; if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200)) goto err; if (!TEST_true(ccm->on_data_invalidated(cc, 1200))) goto err; if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance2)) goto err; if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1200 + 1300)) goto err; if (!TEST_true(ccm->on_data_sent(cc, 1200))) goto err; if (!TEST_true(ccm->on_data_sent(cc, 1300))) goto err; if (!TEST_uint64_t_eq(allowance2 = ccm->get_tx_allowance(cc), allowance - 1200 - 1300)) goto err; loss_info.tx_time = fake_time; loss_info.tx_size = 1200; step_time(100); if (!TEST_true(ccm->on_data_lost(cc, &loss_info))) goto err; loss_info.tx_size = 1300; if (!TEST_true(ccm->on_data_lost(cc, &loss_info))) goto err; if (!TEST_true(ccm->on_data_lost_finished(cc, 0))) goto err; if (!TEST_uint64_t_ne(ccm->get_tx_allowance(cc), allowance2)) goto err; if (!TEST_uint64_t_lt(ccm->get_tx_allowance(cc), allowance)) goto err; testresult = 1; err: if (cc != NULL) ccm->free(cc); return testresult; } int setup_tests(void) { #ifdef GENERATE_LOG logfile = fopen("quic_cc_stats.csv", "w"); fprintf(logfile, "\"Time\"," "\"TX Allowance\"," "\"CWND Size\"," "\"Bytes in Flight\"," "\"Total Acked\",\"Total Lost\"," "\"Capacity\",\"Spare Capacity\"," "\"State\"\n"); #endif ADD_TEST(test_simulate); ADD_TEST(test_sanity); return 1; }
test
openssl/test/quic_cc_test.c
openssl
#include <stdio.h> #include <openssl/ssl.h> #include <openssl/quic.h> #include <openssl/bio.h> #include "internal/common.h" #include "internal/sockets.h" #include "internal/time.h" #include "testutil.h" static const char msg1[] = "GET LICENSE.txt\r\n"; static char msg2[16000]; 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 test_quic_client(void) { int testresult = 0, ret; int c_fd = INVALID_SOCKET; BIO *c_net_bio = NULL, *c_net_bio_own = NULL; BIO_ADDR *s_addr_ = NULL; struct in_addr ina = {0}; SSL_CTX *c_ctx = NULL; SSL *c_ssl = NULL; short port = 4433; int c_connected = 0, c_write_done = 0, c_shutdown = 0; size_t l = 0, c_total_read = 0; OSSL_TIME start_time; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '0', '.', '9' }; ina.s_addr = htonl(0x7f000001UL); c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); if (!TEST_int_ne(c_fd, INVALID_SOCKET)) goto err; if (!TEST_true(BIO_socket_nbio(c_fd, 1))) goto err; if (!TEST_ptr(s_addr_ = BIO_ADDR_new())) goto err; if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina), htons(port)))) goto err; if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0))) goto err; if (!BIO_dgram_set_peer(c_net_bio, s_addr_)) goto err; if (!TEST_ptr(c_ctx = SSL_CTX_new(OSSL_QUIC_client_method()))) goto err; if (!TEST_ptr(c_ssl = SSL_new(c_ctx))) goto err; if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn)))) goto err; SSL_set0_rbio(c_ssl, c_net_bio); if (!TEST_true(BIO_up_ref(c_net_bio))) { c_net_bio_own = NULL; goto err; } SSL_set0_wbio(c_ssl, c_net_bio); c_net_bio_own = NULL; if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0))) goto err; start_time = ossl_time_now(); for (;;) { if (ossl_time_compare(ossl_time_subtract(ossl_time_now(), start_time), ossl_ms2time(10000)) >= 0) { TEST_error("timeout while attempting QUIC client test"); goto err; } if (!c_connected) { ret = SSL_connect(c_ssl); if (!TEST_true(ret == 1 || is_want(c_ssl, ret))) goto err; if (ret == 1) { c_connected = 1; TEST_info("Connected!"); } } if (c_connected && !c_write_done) { if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1), (int)sizeof(msg1) - 1)) goto err; if (!TEST_true(SSL_stream_conclude(c_ssl, 0))) goto err; c_write_done = 1; } if (c_write_done && !c_shutdown && c_total_read < sizeof(msg2) - 1) { ret = SSL_read_ex(c_ssl, msg2 + c_total_read, sizeof(msg2) - 1 - c_total_read, &l); if (ret != 1) { if (SSL_get_error(c_ssl, ret) == SSL_ERROR_ZERO_RETURN) { c_shutdown = 1; TEST_info("Message: \n%s\n", msg2); } else if (!TEST_true(is_want(c_ssl, ret))) { goto err; } } else { c_total_read += l; if (!TEST_size_t_lt(c_total_read, sizeof(msg2) - 1)) goto err; } } if (c_shutdown) { ret = SSL_shutdown(c_ssl); if (ret == 1) break; } OSSL_sleep(0); SSL_handle_events(c_ssl); } testresult = 1; err: SSL_free(c_ssl); SSL_CTX_free(c_ctx); BIO_ADDR_free(s_addr_); BIO_free(c_net_bio_own); if (c_fd != INVALID_SOCKET) BIO_closesocket(c_fd); 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; } ADD_TEST(test_quic_client); return 1; }
test
openssl/test/quic_client_test.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/engine.h> #include "helpers/ssltestlib.h" #include "testutil.h" #include "testutil/output.h" #include "internal/ktls.h" #include "../ssl/ssl_local.h" #include "../ssl/statem/statem_local.h" static OSSL_LIB_CTX *libctx = NULL; static char *cert = NULL; static char *privkey = NULL; static int test_handshake_rtt(int tst) { SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; SSL_CONNECTION *s = NULL; OSSL_STATEM *st = NULL; uint64_t rtt; #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)) || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL))) goto end; s = SSL_CONNECTION_FROM_SSL(tst % 2 == 0 ? clientssl : serverssl); if (!TEST_ptr(s) || !TEST_ptr(st = &s->statem)) return 0; switch (tst) { case 0: st->hand_state = TLS_ST_CW_CLNT_HELLO; ossl_statem_client_write_transition(s); OSSL_sleep(1); st->hand_state = TLS_ST_CR_SRVR_DONE; ossl_statem_client_write_transition(s); break; case 1: st->hand_state = TLS_ST_SW_SRVR_DONE; ossl_statem_server_write_transition(s); OSSL_sleep(1); st->hand_state = TLS_ST_SR_FINISHED; ossl_statem_server_write_transition(s); break; case 2: st->hand_state = TLS_ST_CW_CLNT_HELLO; ossl_statem_client_write_transition(s); OSSL_sleep(1); st->hand_state = TLS_ST_CR_SRVR_DONE; ossl_statem_client_write_transition(s); break; case 3: st->hand_state = TLS_ST_SW_SRVR_DONE; ossl_statem_server_write_transition(s); OSSL_sleep(1); st->hand_state = TLS_ST_SR_FINISHED; ossl_statem_server_write_transition(s); break; case 4: st->hand_state = TLS_ST_EARLY_DATA; ossl_statem_client_write_transition(s); OSSL_sleep(1); st->hand_state = TLS_ST_CR_SRVR_DONE; ossl_statem_client_write_transition(s); break; } if (!TEST_int_gt(SSL_get_handshake_rtt(SSL_CONNECTION_GET_SSL(s), &rtt), 0)) goto end; if (!TEST_uint64_t_ge(rtt, 1000)) goto end; testresult = 1; end: SSL_free(serverssl); SSL_free(clientssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return testresult; } int setup_tests(void) { ADD_ALL_TESTS(test_handshake_rtt, 5); return 1; }
test
openssl/test/ssl_handshake_rtt_test.c
openssl
#include "internal/packet.h" #include "internal/quic_stream.h" #include "testutil.h" static int compare_iov(const unsigned char *ref, size_t ref_len, const OSSL_QTX_IOVEC *iov, size_t iov_len) { size_t i, total_len = 0; const unsigned char *cur = ref; for (i = 0; i < iov_len; ++i) total_len += iov[i].buf_len; if (ref_len != total_len) return 0; for (i = 0; i < iov_len; ++i) { if (memcmp(cur, iov[i].buf, iov[i].buf_len)) return 0; cur += iov[i].buf_len; } return 1; } static const unsigned char data_1[] = { 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f }; static int test_sstream_simple(void) { int testresult = 0; QUIC_SSTREAM *sstream = NULL; OSSL_QUIC_FRAME_STREAM hdr; OSSL_QTX_IOVEC iov[2]; size_t num_iov = 0, wr = 0, i, init_size = 8192; if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size))) goto err; if (!TEST_true(ossl_quic_sstream_is_totally_acked(sstream))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_append(sstream, data_1, sizeof(data_1), &wr)) || !TEST_size_t_eq(wr, sizeof(data_1))) goto err; if (!TEST_false(ossl_quic_sstream_is_totally_acked(sstream))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov)) || !TEST_size_t_gt(num_iov, 0) || !TEST_uint64_t_eq(hdr.offset, 0) || !TEST_uint64_t_eq(hdr.len, sizeof(data_1)) || !TEST_false(hdr.is_fin)) goto err; if (!TEST_true(compare_iov(data_1, sizeof(data_1), iov, num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, 7))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov)) || !TEST_size_t_gt(num_iov, 0) || !TEST_uint64_t_eq(hdr.offset, 8) || !TEST_uint64_t_eq(hdr.len, sizeof(data_1) - 8) || !TEST_false(hdr.is_fin)) goto err; if (!TEST_true(compare_iov(data_1 + 8, sizeof(data_1) - 8, iov, num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 8, 15))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_lost(sstream, 4, 6))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov)) || !TEST_size_t_gt(num_iov, 0) || !TEST_uint64_t_eq(hdr.offset, 4) || !TEST_uint64_t_eq(hdr.len, 3) || !TEST_false(hdr.is_fin)) goto err; if (!TEST_true(compare_iov(data_1 + 4, 3, iov, num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 4, 6))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov))) goto err; if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16)) goto err; if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 1, 7)) || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16)) goto err; if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 0)) || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 8)) goto err; if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 15)) || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 0)) goto err; ossl_quic_sstream_fin(sstream); for (i = 0; i < 2; ++i) { num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov)) || !TEST_uint64_t_eq(hdr.offset, 16) || !TEST_uint64_t_eq(hdr.len, 0) || !TEST_true(hdr.is_fin) || !TEST_size_t_eq(num_iov, 0)) goto err; } if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_lost_fin(sstream))) goto err; for (i = 0; i < 2; ++i) { num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov)) || !TEST_uint64_t_eq(hdr.offset, 16) || !TEST_uint64_t_eq(hdr.len, 0) || !TEST_true(hdr.is_fin) || !TEST_size_t_eq(num_iov, 0)) goto err; } if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16))) goto err; num_iov = OSSL_NELEM(iov); if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov))) goto err; if (!TEST_true(ossl_quic_sstream_mark_acked_fin(sstream))) goto err; if (!TEST_true(ossl_quic_sstream_is_totally_acked(sstream))) goto err; testresult = 1; err: ossl_quic_sstream_free(sstream); return testresult; } static int test_sstream_bulk(int idx) { int testresult = 0; QUIC_SSTREAM *sstream = NULL; OSSL_QUIC_FRAME_STREAM hdr; OSSL_QTX_IOVEC iov[2]; size_t i, j, num_iov = 0, init_size = 8192, l; size_t consumed = 0, total_written = 0, rd, cur_rd, expected = 0, start_at; unsigned char *src_buf = NULL, *dst_buf = NULL; unsigned char *ref_src_buf = NULL, *ref_dst_buf = NULL; unsigned char *ref_dst_cur, *ref_src_cur, *dst_cur; if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size))) goto err; if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_size(sstream), init_size)) goto err; if (!TEST_ptr(src_buf = OPENSSL_zalloc(init_size))) goto err; if (!TEST_ptr(dst_buf = OPENSSL_malloc(init_size))) goto err; if (!TEST_ptr(ref_src_buf = OPENSSL_malloc(init_size))) goto err; if (!TEST_ptr(ref_dst_buf = OPENSSL_malloc(init_size))) goto err; if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, init_size / 2, &consumed)) || !TEST_size_t_eq(consumed, init_size / 2) || !TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, init_size / 2 - 1)) || !TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, init_size / 2 - 1))) goto err; start_at = init_size / 2; for (i = 0; i < init_size; ++i) src_buf[i] = (unsigned char)(test_random() & 0xFF); ref_src_cur = ref_src_buf; do { l = (test_random() % init_size) + 1; if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, l, &consumed))) goto err; memcpy(ref_src_cur, src_buf, consumed); ref_src_cur += consumed; total_written += consumed; } while (consumed > 0); if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), init_size) || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_avail(sstream), 0)) goto err; ref_src_cur = ref_src_buf; ref_dst_cur = ref_dst_buf; for (i = 0; i < total_written; ++i) { if ((test_random() & 1) != 0) { *ref_dst_cur++ = *ref_src_cur; ++expected; } else if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, start_at + i, start_at + i))) goto err; ++ref_src_cur; } if (!TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size * 2)) || !TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size))) goto err; dst_cur = dst_buf; for (i = 0, rd = 0; rd < expected; ++i) { num_iov = OSSL_NELEM(iov); if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, i, &hdr, iov, &num_iov))) goto err; cur_rd = 0; for (j = 0; j < num_iov; ++j) { if (!TEST_size_t_le(iov[j].buf_len + rd, expected)) goto err; memcpy(dst_cur, iov[j].buf, iov[j].buf_len); dst_cur += iov[j].buf_len; cur_rd += iov[j].buf_len; } if (!TEST_uint64_t_eq(cur_rd, hdr.len)) goto err; rd += cur_rd; } if (!TEST_mem_eq(dst_buf, rd, ref_dst_buf, expected)) goto err; testresult = 1; err: OPENSSL_free(src_buf); OPENSSL_free(dst_buf); OPENSSL_free(ref_src_buf); OPENSSL_free(ref_dst_buf); ossl_quic_sstream_free(sstream); return testresult; } static int test_single_copy_read(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size, size_t *readbytes, int *fin) { const unsigned char *record; size_t rec_len; *readbytes = 0; for (;;) { if (!ossl_quic_rstream_get_record(qrs, &record, &rec_len, fin)) return 0; if (rec_len == 0) break; if (rec_len > size) { rec_len = size; *fin = 0; } memcpy(buf, record, rec_len); size -= rec_len; *readbytes += rec_len; buf += rec_len; if (!ossl_quic_rstream_release_record(qrs, rec_len)) return 0; if (*fin || size == 0) break; } return 1; } static const unsigned char simple_data[] = "Hello world! And thank you for all the fish!"; static int test_rstream_simple(int idx) { QUIC_RSTREAM *rstream = NULL; int ret = 0; unsigned char buf[sizeof(simple_data)]; size_t readbytes = 0, avail = 0; int fin = 0; int use_rbuf = idx > 1; int use_sc = idx % 2; int (* read_fn)(QUIC_RSTREAM *, unsigned char *, size_t, size_t *, int *) = use_sc ? test_single_copy_read : ossl_quic_rstream_read; if (!TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL, 0))) goto err; if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 5, simple_data + 5, 10, 0)) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, sizeof(simple_data) - 1, simple_data + sizeof(simple_data) - 1, 1, 1)) || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf), &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 0) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, sizeof(simple_data) - 10, simple_data + sizeof(simple_data) - 10, 10, 1)) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 0, simple_data, 1, 0)) || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf), &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 1) || !TEST_mem_eq(buf, 1, simple_data, 1) || (use_rbuf && !TEST_false(ossl_quic_rstream_move_to_rbuf(rstream))) || (use_rbuf && !TEST_true(ossl_quic_rstream_resize_rbuf(rstream, sizeof(simple_data)))) || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream))) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 0, simple_data, 10, 0)) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, sizeof(simple_data), NULL, 0, 1)) || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf), &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 15) || !TEST_mem_eq(buf, 15, simple_data, 15) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 15, simple_data + 15, sizeof(simple_data) - 15, 1)) || !TEST_true(ossl_quic_rstream_available(rstream, &avail, &fin)) || !TEST_true(fin) || !TEST_size_t_eq(avail, sizeof(simple_data)) || !TEST_true(read_fn(rstream, buf, 2, &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 2) || !TEST_mem_eq(buf, 2, simple_data, 2) || !TEST_true(read_fn(rstream, buf + 2, 12, &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 12) || !TEST_mem_eq(buf + 2, 12, simple_data + 2, 12) || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, sizeof(simple_data), NULL, 0, 1)) || (use_rbuf && !TEST_true(ossl_quic_rstream_resize_rbuf(rstream, 2 * sizeof(simple_data)))) || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream))) || !TEST_true(read_fn(rstream, buf + 14, 5, &readbytes, &fin)) || !TEST_false(fin) || !TEST_size_t_eq(readbytes, 5) || !TEST_mem_eq(buf, 14 + 5, simple_data, 14 + 5) || !TEST_true(read_fn(rstream, buf + 14 + 5, sizeof(buf) - 14 - 5, &readbytes, &fin)) || !TEST_true(fin) || !TEST_size_t_eq(readbytes, sizeof(buf) - 14 - 5) || !TEST_mem_eq(buf, sizeof(buf), simple_data, sizeof(simple_data)) || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream))) || !TEST_true(read_fn(rstream, buf, sizeof(buf), &readbytes, &fin)) || !TEST_true(fin) || !TEST_size_t_eq(readbytes, 0)) goto err; ret = 1; err: ossl_quic_rstream_free(rstream); return ret; } static int test_rstream_random(int idx) { unsigned char *bulk_data = NULL; unsigned char *read_buf = NULL; QUIC_RSTREAM *rstream = NULL; size_t i, read_off, queued_min, queued_max; const size_t data_size = 10000; int r, s, fin = 0, fin_set = 0; int ret = 0; size_t readbytes = 0; if (!TEST_ptr(bulk_data = OPENSSL_malloc(data_size)) || !TEST_ptr(read_buf = OPENSSL_malloc(data_size)) || !TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL, 0))) goto err; if (idx % 3 == 0) ossl_quic_rstream_set_cleanse(rstream, 1); for (i = 0; i < data_size; ++i) bulk_data[i] = (unsigned char)(test_random() & 0xFF); read_off = queued_min = queued_max = 0; for (r = 0; r < 100; ++r) { for (s = 0; s < 10; ++s) { size_t off = (r * 10 + s) * 10, size = 10; if (test_random() % 10 == 0) continue; if (off <= queued_min && off + size > queued_min) queued_min = off + size; if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off, bulk_data + off, size, 0))) goto err; if (queued_max < off + size) queued_max = off + size; if (test_random() % 5 != 0) continue; off = read_off + test_random() % 50; if (off > 50) off -= 50; size = test_random() % 100 + 1; if (off + size > data_size) off = data_size - size; if (off <= queued_min && off + size > queued_min) queued_min = off + size; if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off, bulk_data + off, size, 0))) goto err; if (queued_max < off + size) queued_max = off + size; } if (idx % 2 == 0) { if (!TEST_true(test_single_copy_read(rstream, read_buf, data_size, &readbytes, &fin))) goto err; } else if (!TEST_true(ossl_quic_rstream_read(rstream, read_buf, data_size, &readbytes, &fin))) { goto err; } if (!TEST_size_t_ge(readbytes, queued_min - read_off) || !TEST_size_t_le(readbytes + read_off, data_size) || (idx % 3 != 0 && !TEST_mem_eq(read_buf, readbytes, bulk_data + read_off, readbytes))) goto err; read_off += readbytes; queued_min = read_off; if (test_random() % 50 == 0) if (!TEST_true(ossl_quic_rstream_resize_rbuf(rstream, queued_max - read_off + 1)) || !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream))) goto err; if (!fin_set && queued_max >= data_size - test_random() % 200) { fin_set = 1; if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, data_size, NULL, 0, 1))) goto err; } } TEST_info("Total read bytes: %zu Fin rcvd: %d", read_off, fin); if (idx % 3 == 0) for (i = 0; i < read_off; i++) if (!TEST_uchar_eq(bulk_data[i], 0)) goto err; if (read_off == data_size && fin_set && !fin) { if (idx % 2 == 0) { if (!TEST_true(test_single_copy_read(rstream, read_buf, data_size, &readbytes, &fin))) goto err; } else if (!TEST_true(ossl_quic_rstream_read(rstream, read_buf, data_size, &readbytes, &fin))) { goto err; } if (!TEST_size_t_eq(readbytes, 0) || !TEST_true(fin)) goto err; } ret = 1; err: ossl_quic_rstream_free(rstream); OPENSSL_free(bulk_data); OPENSSL_free(read_buf); return ret; } int setup_tests(void) { ADD_TEST(test_sstream_simple); ADD_ALL_TESTS(test_sstream_bulk, 100); ADD_ALL_TESTS(test_rstream_simple, 4); ADD_ALL_TESTS(test_rstream_random, 100); return 1; }
test
openssl/test/quic_stream_test.c
openssl
#include "internal/nelem.h" #include "testutil.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define NUM_REPEATS "1000000" static ossl_intmax_t num_repeats; static int print_mode = 0; #ifndef OPENSSL_NO_EC # include <openssl/ec.h> # 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> static const char *kP256DefaultResult = "A1E24B223B8E81BC1FFF99BAFB909EDB895FACDE7D6DA5EF5E7B3255FB378E0F"; static BIGNUM *walk_curve(const EC_GROUP *group, EC_POINT *point, ossl_intmax_t num) { BIGNUM *scalar = NULL; ossl_intmax_t i; if (!TEST_ptr(scalar = BN_new()) || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar, NULL, NULL))) goto err; for (i = 0; i < num; i++) { if (!TEST_true(EC_POINT_mul(group, point, NULL, point, scalar, NULL)) || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar, NULL, NULL))) goto err; } return scalar; err: BN_free(scalar); return NULL; } static int test_curve(void) { EC_GROUP *group = NULL; EC_POINT *point = NULL; BIGNUM *result = NULL, *expected_result = NULL; int ret = 0; if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)) || !TEST_ptr(point = EC_POINT_dup(EC_GROUP_get0_generator(group), group)) || !TEST_ptr(result = walk_curve(group, point, num_repeats))) return 0; if (print_mode) { BN_print(bio_out, result); BIO_printf(bio_out, "\n"); ret = 1; } else { if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult)) || !TEST_ptr(expected_result) || !TEST_BN_eq(result, expected_result)) goto err; ret = 1; } err: EC_GROUP_free(group); EC_POINT_free(point); BN_free(result); BN_free(expected_result); return ret; } #endif typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_NUM_REPEATS, OPT_TEST_ENUM } OPTION_CHOICE; const OPTIONS *test_get_options(void) { static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_DEFAULT_USAGE, { "num", OPT_NUM_REPEATS, 'M', "Number of repeats" }, { NULL } }; return test_options; } int setup_tests(void) { OPTION_CHOICE o; if (!opt_intmax(NUM_REPEATS, &num_repeats)) { TEST_error("Cannot parse " NUM_REPEATS); return 0; } while ((o = opt_next()) != OPT_EOF) { switch (o) { case OPT_NUM_REPEATS: if (!opt_intmax(opt_arg(), &num_repeats) || num_repeats < 0) return 0; print_mode = 1; break; case OPT_TEST_CASES: break; default: case OPT_ERR: return 0; } } #ifndef OPENSSL_NO_EC ADD_TEST(test_curve); #endif return 1; }
test
openssl/test/ecstresstest.c
openssl
#include <stddef.h> #include <string.h> #include <openssl/provider.h> #include <openssl/params.h> #include <openssl/core_names.h> #include <openssl/self_test.h> #include <openssl/evp.h> #include "testutil.h" typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_PROVIDER_NAME, OPT_CONFIG_FILE, OPT_TEST_ENUM } OPTION_CHOICE; struct self_test_arg { int count; }; static OSSL_LIB_CTX *libctx = NULL; static char *provider_name = 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, { "provider_name", OPT_PROVIDER_NAME, 's', "The name of the provider to load" }, { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" }, { NULL } }; return test_options; } static int self_test_events(const OSSL_PARAM params[], void *arg, const char *title, int corrupt) { struct self_test_arg *args = arg; const OSSL_PARAM *p = NULL; const char *phase = NULL, *type = NULL, *desc = NULL; int ret = 0; if (args->count == 0) BIO_printf(bio_out, "\n%s\n", title); args->count++; 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; if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0) BIO_printf(bio_out, "%s : (%s) : ", desc, type); else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0) BIO_printf(bio_out, "%s\n", phase); if (corrupt && strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0) goto err; ret = 1; err: return ret; } static int self_test_on_demand_fail(const OSSL_PARAM params[], void *arg) { return self_test_events(params, arg, "On Demand Failure", 1); } static int self_test_on_demand(const OSSL_PARAM params[], void *arg) { return self_test_events(params, arg, "On Demand", 0); } static int self_test_on_load(const OSSL_PARAM params[], void *arg) { return self_test_events(params, arg, "On Loading", 0); } static int get_provider_params(const OSSL_PROVIDER *prov) { int ret = 0; OSSL_PARAM params[5]; char *name, *version, *buildinfo; int status; const OSSL_PARAM *gettable, *p; if (!TEST_ptr(gettable = OSSL_PROVIDER_gettable_params(prov)) || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_NAME)) || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_VERSION)) || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_STATUS)) || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_BUILDINFO))) goto end; params[0] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME, &name, 0); params[1] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION, &version, 0); params[2] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_STATUS, &status); params[3] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO, &buildinfo, 0); params[4] = OSSL_PARAM_construct_end(); OSSL_PARAM_set_all_unmodified(params); if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))) goto end; if (!TEST_true(OSSL_PARAM_modified(params + 0)) || !TEST_true(OSSL_PARAM_modified(params + 1)) || !TEST_true(OSSL_PARAM_modified(params + 2)) || !TEST_true(OSSL_PARAM_modified(params + 3)) || !TEST_true(status == 1)) goto end; ret = 1; end: return ret; } static int test_provider_status(void) { int ret = 0; unsigned int status = 0; OSSL_PROVIDER *prov = NULL; OSSL_PARAM params[2]; EVP_MD *fetch = NULL; if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name))) goto err; if (!get_provider_params(prov)) goto err; params[0] = OSSL_PARAM_construct_uint(OSSL_PROV_PARAM_STATUS, &status); params[1] = OSSL_PARAM_construct_end(); if (!TEST_true(OSSL_PROVIDER_get_params(prov, params)) || !TEST_true(status == 1)) goto err; if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL))) goto err; EVP_MD_free(fetch); fetch = NULL; self_test_args.count = 0; OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand, &self_test_args); if (!TEST_true(OSSL_PROVIDER_self_test(prov))) goto err; self_test_args.count = 0; OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand_fail, &self_test_args); if (!TEST_false(OSSL_PROVIDER_self_test(prov))) goto err; if (!TEST_true(OSSL_PROVIDER_get_params(prov, params)) || !TEST_uint_eq(status, 0)) goto err; if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL))) goto err; ret = 1; err: EVP_MD_free(fetch); OSSL_PROVIDER_unload(prov); return ret; } static int test_provider_gettable_params(void) { OSSL_PROVIDER *prov; int ret; if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name))) return 0; ret = get_provider_params(prov); OSSL_PROVIDER_unload(prov); 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_PROVIDER_NAME: provider_name = 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 (strcmp(provider_name, "fips") == 0) { self_test_args.count = 0; OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args); if (!OSSL_LIB_CTX_load_config(libctx, config_file)) { opt_printf_stderr("Failed to load config\n"); return 0; } ADD_TEST(test_provider_status); } else { ADD_TEST(test_provider_gettable_params); } return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(libctx); }
test
openssl/test/provider_status_test.c
openssl
#include <string.h> #include "testutil.h" #include "internal/nelem.h" #include "internal/endian.h" #include <openssl/params.h> #include <openssl/bn.h> #define MAX_LEN 20 static void swap_copy(unsigned char *out, const void *in, size_t len) { size_t j; for (j = 0; j < len; j++) out[j] = ((unsigned char *)in)[len - j - 1]; } static void le_copy(unsigned char *out, size_t outlen, const void *in, size_t inlen) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) { memcpy(out, in, outlen); } else { if (outlen < inlen) in = (const char *)in + inlen - outlen; swap_copy(out, in, outlen); } } static const struct { size_t len; unsigned char value[MAX_LEN]; } raw_values[] = { { 1, { 0x47 } }, { 1, { 0xd0 } }, { 2, { 0x01, 0xe9 } }, { 2, { 0xff, 0x53 } }, { 3, { 0x16, 0xff, 0x7c } }, { 3, { 0xa8, 0x9c, 0x0e } }, { 4, { 0x38, 0x27, 0xbf, 0x3b } }, { 4, { 0x9f, 0x26, 0x48, 0x22 } }, { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } }, { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } }, { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } }, { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } }, { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d, 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } }, { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc, 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } }, }; static int test_param_type_null(OSSL_PARAM *param) { int rc = 0; uint64_t intval; double dval; BIGNUM *bn; switch(param->data_type) { case OSSL_PARAM_INTEGER: if (param->data_size == sizeof(int32_t)) rc = OSSL_PARAM_get_int32(param, (int32_t *)&intval); else if (param->data_size == sizeof(uint64_t)) rc = OSSL_PARAM_get_int64(param, (int64_t *)&intval); else return 1; break; case OSSL_PARAM_UNSIGNED_INTEGER: if (param->data_size == sizeof(uint32_t)) rc = OSSL_PARAM_get_uint32(param, (uint32_t *)&intval); else if (param->data_size == sizeof(uint64_t)) rc = OSSL_PARAM_get_uint64(param, &intval); else rc = OSSL_PARAM_get_BN(param, &bn); break; case OSSL_PARAM_REAL: rc = OSSL_PARAM_get_double(param, &dval); break; case OSSL_PARAM_UTF8_STRING: case OSSL_PARAM_OCTET_STRING: case OSSL_PARAM_UTF8_PTR: case OSSL_PARAM_OCTET_PTR: return 1; break; } return rc == 0; } static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp, size_t width) { int32_t i32; int64_t i64; size_t s, sz; unsigned char buf[MAX_LEN]; const int bit32 = param->data_size <= sizeof(int32_t); const int sizet = param->data_size <= sizeof(size_t); const int signd = param->data_type == OSSL_PARAM_INTEGER; param->return_size = OSSL_PARAM_UNMODIFIED; if (signd) { if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32))) || !TEST_true(OSSL_PARAM_get_int64(param, &i64))) return 0; } else { if ((bit32 && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32))) || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64)) || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s)))) return 0; } if (!TEST_false(OSSL_PARAM_modified(param))) return 0; if (bit32) { le_copy(buf, sizeof(i32), &i32, sizeof(i32)); sz = sizeof(i32) < width ? sizeof(i32) : width; if (!TEST_mem_eq(buf, sz, cmp, sz)) return 0; } le_copy(buf, sizeof(i64), &i64, sizeof(i64)); sz = sizeof(i64) < width ? sizeof(i64) : width; if (!TEST_mem_eq(buf, sz, cmp, sz)) return 0; if (sizet && !signd) { le_copy(buf, sizeof(s), &s, sizeof(s)); sz = sizeof(s) < width ? sizeof(s) : width; if (!TEST_mem_eq(buf, sz, cmp, sz)) return 0; } if (sizeof(size_t) > width) { if (signd) { if (!TEST_true(OSSL_PARAM_set_int32(param, 12345)) || !TEST_true(OSSL_PARAM_get_int64(param, &i64)) || !TEST_size_t_eq((size_t)i64, 12345)) return 0; } else { if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345)) || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64)) || !TEST_size_t_eq((size_t)i64, 12345)) return 0; } if (!TEST_true(OSSL_PARAM_modified(param))) return 0; } return 1; } static int test_param_int(int n) { int in, out; unsigned char buf[MAX_LEN], cmp[sizeof(int)]; const size_t len = raw_values[n].len >= sizeof(int) ? sizeof(int) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_int("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_int(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_int(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(int)); } static int test_param_long(int n) { long int in, out; unsigned char buf[MAX_LEN], cmp[sizeof(long int)]; const size_t len = raw_values[n].len >= sizeof(long int) ? sizeof(long int) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_long("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_long(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_long(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(long int)); } static int test_param_uint(int n) { unsigned int in, out; unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)]; const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_uint("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_uint(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_uint(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int)); } static int test_param_ulong(int n) { unsigned long int in, out; unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)]; const size_t len = raw_values[n].len >= sizeof(unsigned long int) ? sizeof(unsigned long int) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_ulong(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int)); } static int test_param_int32(int n) { int32_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)]; const size_t len = raw_values[n].len >= sizeof(int32_t) ? sizeof(int32_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_int32("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_int32(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_int32(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t)); } static int test_param_uint32(int n) { uint32_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)]; const size_t len = raw_values[n].len >= sizeof(uint32_t) ? sizeof(uint32_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_uint32(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t)); } static int test_param_int64(int n) { int64_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)]; const size_t len = raw_values[n].len >= sizeof(int64_t) ? sizeof(int64_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_int64("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_int64(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_int64(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t)); } static int test_param_uint64(int n) { uint64_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)]; const size_t len = raw_values[n].len >= sizeof(uint64_t) ? sizeof(uint64_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_uint64(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t)); } static int test_param_size_t(int n) { size_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(size_t)]; const size_t len = raw_values[n].len >= sizeof(size_t) ? sizeof(size_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_size_t(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t)); } static int test_param_time_t(int n) { time_t in, out; unsigned char buf[MAX_LEN], cmp[sizeof(time_t)]; const size_t len = raw_values[n].len >= sizeof(time_t) ? sizeof(time_t) : raw_values[n].len; OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; memset(buf, 0, sizeof(buf)); le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in)); memcpy(&in, buf, sizeof(in)); param.data = &out; if (!TEST_true(OSSL_PARAM_set_time_t(&param, in))) return 0; le_copy(cmp, sizeof(out), &out, sizeof(out)); if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) return 0; in = 0; if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in))) return 0; le_copy(cmp, sizeof(in), &in, sizeof(in)); if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) return 0; param.data = &out; return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t)); } static int test_param_bignum(int n) { unsigned char buf[MAX_LEN], bnbuf[MAX_LEN]; const size_t len = raw_values[n].len; BIGNUM *b = NULL, *c = NULL; OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0); int ret = 0; if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; param.data = bnbuf; param.data_size = sizeof(bnbuf); if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL))) goto err; if (!TEST_true(OSSL_PARAM_set_BN(&param, b))) goto err; le_copy(buf, len, bnbuf, sizeof(bnbuf)); if (!TEST_mem_eq(raw_values[n].value, len, buf, len)) goto err; param.data_size = param.return_size; if (!TEST_true(OSSL_PARAM_get_BN(&param, &c)) || !TEST_BN_eq(b, c)) goto err; ret = 1; err: BN_free(b); BN_free(c); return ret; } static int test_param_signed_bignum(int n) { unsigned char buf[MAX_LEN], bnbuf[MAX_LEN]; const size_t len = raw_values[n].len; BIGNUM *b = NULL, *c = NULL; OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER, NULL, 0); int ret = 0; if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; param.data = bnbuf; param.data_size = sizeof(bnbuf); if (!TEST_ptr(b = BN_signed_lebin2bn(raw_values[n].value, (int)len, NULL))) goto err; if (!TEST_false(!!(raw_values[n].value[len - 1] & 0x80) ^ BN_is_negative(b))) goto err; if (!TEST_true(OSSL_PARAM_set_BN(&param, b))) goto err; le_copy(buf, len, bnbuf, sizeof(bnbuf)); if (!TEST_mem_eq(raw_values[n].value, len, buf, len)) goto err; param.data_size = param.return_size; if (!TEST_true(OSSL_PARAM_get_BN(&param, &c)) || !TEST_BN_eq(b, c)) { BN_print_fp(stderr, c); goto err; } ret = 1; err: BN_free(b); BN_free(c); return ret; } static int test_param_real(void) { double p; OSSL_PARAM param = OSSL_PARAM_double("r", NULL); if (!TEST_int_eq(test_param_type_null(&param), 1)) return 0; param.data = &p; return TEST_true(OSSL_PARAM_set_double(&param, 3.14159)) && TEST_double_eq(p, 3.14159); } static int test_param_construct(int tstid) { static const char *int_names[] = { "int", "long", "int32", "int64" }; static const char *uint_names[] = { "uint", "ulong", "uint32", "uint64", "size_t" }; static const unsigned char bn_val[16] = { 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23, 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22 }; OSSL_PARAM *p = NULL, *p1 = NULL; static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END }; OSSL_PARAM params[20]; char buf[100], buf2[100], *bufp, *bufp2; unsigned char ubuf[100]; void *vp, *vpn = NULL, *vp2; OSSL_PARAM *cp; int i, n = 0, ret = 0; unsigned int u; long int l; unsigned long int ul; int32_t i32; uint32_t u32; int64_t i64; uint64_t u64; size_t j, k, s; double d, d2; BIGNUM *bn = NULL, *bn2 = NULL; params[n++] = OSSL_PARAM_construct_int("int", &i); params[n++] = OSSL_PARAM_construct_uint("uint", &u); params[n++] = OSSL_PARAM_construct_long("long", &l); params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul); params[n++] = OSSL_PARAM_construct_int32("int32", &i32); params[n++] = OSSL_PARAM_construct_int64("int64", &i64); params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32); params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64); params[n++] = OSSL_PARAM_construct_size_t("size_t", &s); params[n++] = OSSL_PARAM_construct_double("double", &d); params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf)); params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf)); params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf)); params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0); params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0); params[n] = OSSL_PARAM_construct_end(); switch (tstid) { case 0: p = params; break; case 1: p = OSSL_PARAM_merge(params, params_empty); break; case 2: p = OSSL_PARAM_dup(params); break; default: p1 = OSSL_PARAM_dup(params); p = OSSL_PARAM_merge(p1, params_empty); break; } if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord"))) goto err; for (j = 0; j < OSSL_NELEM(int_names); j++) { if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j])) || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j))) || !TEST_true(OSSL_PARAM_get_int64(cp, &i64)) || !TEST_size_t_eq(cp->data_size, cp->return_size) || !TEST_size_t_eq((size_t)i64, 3 + j)) { TEST_note("iteration %zu var %s", j + 1, int_names[j]); goto err; } } for (j = 0; j < OSSL_NELEM(uint_names); j++) { if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j])) || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j))) || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64)) || !TEST_size_t_eq(cp->data_size, cp->return_size) || !TEST_size_t_eq((size_t)u64, 3 + j)) { TEST_note("iteration %zu var %s", j + 1, uint_names[j]); goto err; } } if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double")) || !TEST_true(OSSL_PARAM_set_double(cp, 3.14)) || !TEST_true(OSSL_PARAM_get_double(cp, &d2)) || !TEST_size_t_eq(cp->return_size, sizeof(double)) || !TEST_double_eq(d2, 3.14) || (tstid <= 1 && !TEST_double_eq(d, d2))) goto err; bufp = NULL; if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str")) || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef")) || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1) || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0)) || !TEST_str_eq(bufp, "abcdef")) { OPENSSL_free(bufp); goto err; } OPENSSL_free(bufp); bufp = buf2; if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2))) || !TEST_str_eq(buf2, "abcdef")) goto err; bufp = buf; if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr")) || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz")) || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1) || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2)) || !TEST_str_eq(bufp2, "tuvwxyz") || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp))) goto err; if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr")) || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi", sizeof("abcdefghi"))) || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi"))) goto err; cp->data_size = cp->return_size; if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s)) || !TEST_size_t_eq(s, sizeof("abcdefghi")) || !TEST_mem_eq(vpn, sizeof("abcdefghi"), "abcdefghi", sizeof("abcdefghi"))) goto err; vp = buf2; if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s)) || !TEST_size_t_eq(s, sizeof("abcdefghi")) || !TEST_mem_eq(vp, sizeof("abcdefghi"), "abcdefghi", sizeof("abcdefghi"))) goto err; vp = &l; if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr")) || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul))) || !TEST_size_t_eq(cp->return_size, sizeof(ul)) || (tstid <= 1 && !TEST_ptr_eq(vp, &ul))) goto err; cp->data_size = cp->return_size; if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k)) || !TEST_size_t_eq(k, sizeof(ul)) || (tstid <= 1 && !TEST_ptr_eq(vp2, vp))) goto err; if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum")) || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL)) || !TEST_true(OSSL_PARAM_set_BN(cp, bn)) || !TEST_size_t_eq(cp->data_size, cp->return_size)) goto err; cp->data_size = cp->return_size; if (!TEST_true(OSSL_PARAM_get_BN(cp, &bn2)) || !TEST_BN_eq(bn, bn2)) goto err; ret = 1; err: if (p != params) OPENSSL_free(p); OPENSSL_free(p1); OPENSSL_free(vpn); BN_free(bn); BN_free(bn2); return ret; } static int test_param_modified(void) { OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL), OSSL_PARAM_int("b", NULL), OSSL_PARAM_END }; int a, b; param->data = &a; param[1].data = &b; if (!TEST_false(OSSL_PARAM_modified(param)) && !TEST_true(OSSL_PARAM_set_int32(param, 1234)) && !TEST_true(OSSL_PARAM_modified(param)) && !TEST_false(OSSL_PARAM_modified(param + 1)) && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1)) && !TEST_true(OSSL_PARAM_modified(param + 1))) return 0; OSSL_PARAM_set_all_unmodified(param); if (!TEST_false(OSSL_PARAM_modified(param)) && !TEST_true(OSSL_PARAM_set_int32(param, 4321)) && !TEST_true(OSSL_PARAM_modified(param)) && !TEST_false(OSSL_PARAM_modified(param + 1)) && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2)) && !TEST_true(OSSL_PARAM_modified(param + 1))) return 0; return 1; } static int test_param_copy_null(void) { int ret, val; int a = 1, b = 2, i = 0; OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p; OSSL_PARAM param[3]; param[i++] = OSSL_PARAM_construct_int("a", &a); param[i++] = OSSL_PARAM_construct_int("b", &b); param[i] = OSSL_PARAM_construct_end(); ret = TEST_ptr_null(OSSL_PARAM_dup(NULL)) && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param)) && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a")) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, 1) && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b")) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, 2) && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL)) && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a")) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, 1) && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b")) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, 2) && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL)); OSSL_PARAM_free(cp2); OSSL_PARAM_free(cp1); return ret; } int setup_tests(void) { ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values)); ADD_ALL_TESTS(test_param_signed_bignum, OSSL_NELEM(raw_values)); ADD_TEST(test_param_real); ADD_ALL_TESTS(test_param_construct, 4); ADD_TEST(test_param_modified); ADD_TEST(test_param_copy_null); return 1; }
test
openssl/test/params_api_test.c
openssl
#include <openssl/bio.h> #include "testutil.h" #ifndef OPENSSL_NO_DGRAM static int test_dgram(void) { BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL; int testresult = 0; const char msg1[] = "12345656"; const char msg2[] = "abcdefghijklmno"; const char msg3[] = "ABCDEF"; const char msg4[] = "FEDCBA"; char buf[80]; if (!TEST_ptr(bio)) goto err; rbio = BIO_new_mem_buf(msg1, sizeof(msg1)); if (!TEST_ptr(rbio)) goto err; if (!TEST_int_gt(BIO_set_mem_eof_return(rbio, 0), 0)) goto err; if (!TEST_int_le(BIO_set_mem_eof_return(bio, 0), 0)) goto err; if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) goto err; if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) goto err; if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) goto err; if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4))) goto err; if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1)) || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1)) || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2)) || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2)) || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3)) || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3)) || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg4)) || !TEST_mem_eq(buf, sizeof(msg4), msg4, sizeof(msg4))) goto err; if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) goto err; if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) goto err; if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1)) || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))) goto err; if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3))) goto err; if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2)) || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2)) || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3)) || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3))) goto err; if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1))) goto err; if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2))) goto err; if (!TEST_int_eq(BIO_read(bio, buf, 2), 2) || !TEST_mem_eq(buf, 2, msg1, 2)) goto err; if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2)) || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))) goto err; if (!TEST_int_eq(BIO_write(bio, NULL, 0), 0) || !TEST_int_lt(BIO_read(bio, buf, sizeof(buf)), 0) || !TEST_false(BIO_eof(bio)) || !TEST_true(BIO_should_retry(bio))) goto err; if (!TEST_int_eq(BIO_dgram_set_mtu(bio, 123456), 1) || !TEST_int_eq(BIO_dgram_get_mtu(bio), 123456)) goto err; testresult = 1; err: BIO_free(rbio); BIO_free(bio); return testresult; } #endif int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } #ifndef OPENSSL_NO_DGRAM ADD_TEST(test_dgram); #endif return 1; }
test
openssl/test/membio_test.c
openssl
#include <string.h> #include <openssl/ssl.h> #include <openssl/bio.h> #include <openssl/err.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/packet.h" #include "helpers/ssltestlib.h" #include "testutil.h" struct async_ctrs { unsigned int rctr; unsigned int wctr; }; static SSL_CTX *serverctx = NULL; static SSL_CTX *clientctx = NULL; #define MAX_ATTEMPTS 100 static int checkbuffers(SSL *s, int isalloced) { SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); OSSL_RECORD_LAYER *rrl = sc->rlayer.rrl; OSSL_RECORD_LAYER *wrl = sc->rlayer.wrl; if (isalloced) return rrl->rbuf.buf != NULL && wrl->wbuf[0].buf != NULL; return rrl->rbuf.buf == NULL && wrl->wbuf[0].buf == NULL; } static int test_func(int test) { int result = 0; SSL *serverssl = NULL, *clientssl = NULL; int ret; size_t i, j; const char testdata[] = "Test data"; char buf[sizeof(testdata)]; if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl, NULL, NULL))) { TEST_error("Test %d failed: Create SSL objects failed\n", test); goto end; } if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) { TEST_error("Test %d failed: Create SSL connection failed\n", test); goto end; } for (j = 0; j < 2; j++) { int len; for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2; i++) { if (test >= 1 && (!TEST_true(SSL_free_buffers(clientssl)) || !TEST_true(checkbuffers(clientssl, 0)))) goto end; if (test >= 2 && (!TEST_true(SSL_alloc_buffers(clientssl)) || !TEST_true(checkbuffers(clientssl, 1)))) goto end; if (test >= 3 && (!TEST_true(SSL_alloc_buffers(clientssl)) || !TEST_true(checkbuffers(clientssl, 1)))) goto end; if (test >= 4 && (!TEST_true(SSL_free_buffers(clientssl)) || !TEST_true(checkbuffers(clientssl, 0)))) goto end; ret = SSL_write(clientssl, testdata + len, sizeof(testdata) - len); if (ret > 0) { len += ret; } else { int ssl_error = SSL_get_error(clientssl, ret); if (ssl_error == SSL_ERROR_SYSCALL || ssl_error == SSL_ERROR_SSL) { TEST_error("Test %d failed: Failed to write app data\n", test); goto end; } } } if (!TEST_size_t_eq(len, sizeof(testdata))) goto end; for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < MAX_ATTEMPTS; i++) { if (test >= 5 && (!TEST_true(SSL_free_buffers(serverssl)) || !TEST_true(checkbuffers(serverssl, 0)))) goto end; if (test >= 6 && (!TEST_true(SSL_free_buffers(serverssl)) || !TEST_true(checkbuffers(serverssl, 0)))) goto end; if (test >= 7 && (!TEST_true(SSL_alloc_buffers(serverssl)) || !TEST_true(checkbuffers(serverssl, 1)))) goto end; if (test >= 8 && (!TEST_true(SSL_free_buffers(serverssl)) || !TEST_true(checkbuffers(serverssl, 0)))) goto end; ret = SSL_read(serverssl, buf + len, sizeof(buf) - len); if (ret > 0) { len += ret; } else { int ssl_error = SSL_get_error(serverssl, ret); if (ssl_error == SSL_ERROR_SYSCALL || ssl_error == SSL_ERROR_SSL) { TEST_error("Test %d failed: Failed to read app data\n", test); goto end; } } } if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata))) goto end; } result = 1; end: if (!result) ERR_print_errors_fp(stderr); SSL_free(clientssl); SSL_free(serverssl); return result; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") int setup_tests(void) { char *cert, *pkey; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(cert = test_get_argument(0)) || !TEST_ptr(pkey = test_get_argument(1))) return 0; if (!create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(), TLS1_VERSION, 0, &serverctx, &clientctx, cert, pkey)) { TEST_error("Failed to create SSL_CTX pair\n"); return 0; } ADD_ALL_TESTS(test_func, 9); return 1; } void cleanup_tests(void) { SSL_CTX_free(clientctx); SSL_CTX_free(serverctx); }
test
openssl/test/sslbuffertest.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/crypto.h> #include <openssl/bio.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include <openssl/err.h> #include "testutil.h" static const char *certs_dir; static char *root_f = NULL; static char *roots_f = NULL; static char *untrusted_f = NULL; static char *bad_f = NULL; static char *req_f = NULL; static char *sroot_cert = NULL; static char *ca_cert = NULL; static char *ee_cert = NULL; #define load_cert_from_file(file) load_cert_pem(file, NULL) static int test_alt_chains_cert_forgery(void) { int ret = 0; int i; X509 *x = NULL; STACK_OF(X509) *untrusted = NULL; X509_STORE_CTX *sctx = NULL; X509_STORE *store = NULL; X509_LOOKUP *lookup = NULL; store = X509_STORE_new(); if (store == NULL) goto err; lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); if (lookup == NULL) goto err; if (!X509_LOOKUP_load_file(lookup, roots_f, X509_FILETYPE_PEM)) goto err; untrusted = load_certs_pem(untrusted_f); if ((x = load_cert_from_file(bad_f)) == NULL) goto err; sctx = X509_STORE_CTX_new(); if (sctx == NULL) goto err; if (!X509_STORE_CTX_init(sctx, store, x, untrusted)) goto err; i = X509_verify_cert(sctx); if (i == 0 && X509_STORE_CTX_get_error(sctx) == X509_V_ERR_INVALID_CA) { ret = 1; } err: X509_STORE_CTX_free(sctx); X509_free(x); OSSL_STACK_OF_X509_free(untrusted); X509_STORE_free(store); return ret; } static int test_distinguishing_id(void) { X509 *x = NULL; int ret = 0; ASN1_OCTET_STRING *v = NULL, *v2 = NULL; char *distid = "this is an ID"; x = load_cert_from_file(bad_f); if (x == NULL) goto err; v = ASN1_OCTET_STRING_new(); if (v == NULL) goto err; if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid, (int)strlen(distid))) { ASN1_OCTET_STRING_free(v); goto err; } X509_set0_distinguishing_id(x, v); v2 = X509_get0_distinguishing_id(x); if (!TEST_ptr(v2) || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0)) goto err; ret = 1; err: X509_free(x); return ret; } static int test_req_distinguishing_id(void) { X509_REQ *x = NULL; BIO *bio = NULL; int ret = 0; ASN1_OCTET_STRING *v = NULL, *v2 = NULL; char *distid = "this is an ID"; bio = BIO_new_file(req_f, "r"); if (bio == NULL) goto err; x = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL); if (x == NULL) goto err; v = ASN1_OCTET_STRING_new(); if (v == NULL) goto err; if (!ASN1_OCTET_STRING_set(v, (unsigned char *)distid, (int)strlen(distid))) { ASN1_OCTET_STRING_free(v); goto err; } X509_REQ_set0_distinguishing_id(x, v); v2 = X509_REQ_get0_distinguishing_id(x); if (!TEST_ptr(v2) || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v, v2), 0)) goto err; ret = 1; err: X509_REQ_free(x); BIO_free(bio); return ret; } static int test_self_signed(const char *filename, int use_trusted, int expected) { X509 *cert = load_cert_from_file(filename); STACK_OF(X509) *trusted = sk_X509_new_null(); X509_STORE_CTX *ctx = X509_STORE_CTX_new(); int ret; ret = TEST_int_eq(X509_self_signed(cert, 1), expected); if (cert != NULL) { if (use_trusted) ret = ret && TEST_true(sk_X509_push(trusted, cert)); ret = ret && TEST_true(X509_STORE_CTX_init(ctx, NULL, cert, NULL)); X509_STORE_CTX_set0_trusted_stack(ctx, trusted); ret = ret && TEST_int_eq(X509_verify_cert(ctx), expected); } X509_STORE_CTX_free(ctx); sk_X509_free(trusted); X509_free(cert); return ret; } static int test_self_signed_good(void) { return test_self_signed(root_f, 1, 1); } static int test_self_signed_bad(void) { return test_self_signed(bad_f, 1, 0); } static int test_self_signed_error(void) { return test_self_signed("nonexistent file name", 1, -1); } static int test_store_ctx(void) { return test_self_signed(bad_f, 0, 0); } static int do_test_purpose(int purpose, int expected) { X509 *eecert = load_cert_from_file(ee_cert); X509 *untrcert = load_cert_from_file(ca_cert); X509 *trcert = load_cert_from_file(sroot_cert); STACK_OF(X509) *trusted = sk_X509_new_null(); STACK_OF(X509) *untrusted = sk_X509_new_null(); X509_STORE_CTX *ctx = X509_STORE_CTX_new(); int testresult = 0; if (!TEST_ptr(eecert) || !TEST_ptr(untrcert) || !TEST_ptr(trcert) || !TEST_ptr(trusted) || !TEST_ptr(untrusted) || !TEST_ptr(ctx)) goto err; if (!TEST_true(sk_X509_push(trusted, trcert))) goto err; trcert = NULL; if (!TEST_true(sk_X509_push(untrusted, untrcert))) goto err; untrcert = NULL; if (!TEST_true(X509_STORE_CTX_init(ctx, NULL, eecert, untrusted))) goto err; if (!TEST_true(X509_STORE_CTX_set_purpose(ctx, purpose))) goto err; X509_STORE_CTX_set0_trusted_stack(ctx, trusted); if (!TEST_int_eq(X509_verify_cert(ctx), expected)) goto err; testresult = 1; err: OSSL_STACK_OF_X509_free(trusted); OSSL_STACK_OF_X509_free(untrusted); X509_STORE_CTX_free(ctx); X509_free(eecert); X509_free(untrcert); X509_free(trcert); return testresult; } static int test_purpose_ssl_client(void) { return do_test_purpose(X509_PURPOSE_SSL_CLIENT, 0); } static int test_purpose_ssl_server(void) { return do_test_purpose(X509_PURPOSE_SSL_SERVER, 1); } static int test_purpose_any(void) { return do_test_purpose(X509_PURPOSE_ANY, 1); } OPT_TEST_DECLARE_USAGE("certs-dir\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(certs_dir = test_get_argument(0))) return 0; if (!TEST_ptr(root_f = test_mk_file_path(certs_dir, "rootCA.pem")) || !TEST_ptr(roots_f = test_mk_file_path(certs_dir, "roots.pem")) || !TEST_ptr(untrusted_f = test_mk_file_path(certs_dir, "untrusted.pem")) || !TEST_ptr(bad_f = test_mk_file_path(certs_dir, "bad.pem")) || !TEST_ptr(req_f = test_mk_file_path(certs_dir, "sm2-csr.pem")) || !TEST_ptr(sroot_cert = test_mk_file_path(certs_dir, "sroot-cert.pem")) || !TEST_ptr(ca_cert = test_mk_file_path(certs_dir, "ca-cert.pem")) || !TEST_ptr(ee_cert = test_mk_file_path(certs_dir, "ee-cert.pem"))) goto err; ADD_TEST(test_alt_chains_cert_forgery); ADD_TEST(test_store_ctx); ADD_TEST(test_distinguishing_id); ADD_TEST(test_req_distinguishing_id); ADD_TEST(test_self_signed_good); ADD_TEST(test_self_signed_bad); ADD_TEST(test_self_signed_error); ADD_TEST(test_purpose_ssl_client); ADD_TEST(test_purpose_ssl_server); ADD_TEST(test_purpose_any); return 1; err: cleanup_tests(); return 0; } void cleanup_tests(void) { OPENSSL_free(root_f); OPENSSL_free(roots_f); OPENSSL_free(untrusted_f); OPENSSL_free(bad_f); OPENSSL_free(req_f); OPENSSL_free(sroot_cert); OPENSSL_free(ca_cert); OPENSSL_free(ee_cert); }
test
openssl/test/verify_extra_test.c
openssl
#include "internal/qlog.h" #include "testutil.h" static const char expected[] = { 30, '{', '"', 'q', 'l', 'o', 'g', '_', 'v', 'e', 'r', 's', 'i', 'o', 'n', '"', ':', '"', '0', '.', '3', '"', ',', '"', 'q', 'l', 'o', 'g', '_', 'f', 'o', 'r', 'm', 'a', 't', '"', ':', '"', 'J', 'S', 'O', 'N', '-', 'S', 'E', 'Q', '"', ',', '"', 't', 'i', 't', 'l', 'e', '"', ':', '"', 't', 'e', 's', 't', ' ', 't', 'i', 't', 'l', 'e', '"', ',', '"', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', '"', ':', '"', 't', 'e', 's', 't', ' ', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', '"', ',', '"', 't', 'r', 'a', 'c', 'e', '"', ':', '{', '"', 'c', 'o', 'm', 'm', 'o', 'n', '_', 'f', 'i', 'e', 'l', 'd', 's', '"', ':', '{', '"', 't', 'i', 'm', 'e', '_', 'f', 'o', 'r', 'm', 'a', 't', '"', ':', '"', 'd', 'e', 'l', 't', 'a', '"', ',', '"', 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', '_', 't', 'y', 'p', 'e', '"', ':', '[', '"', 'Q', 'U', 'I', 'C', '"', ']', ',', '"', 'g', 'r', 'o', 'u', 'p', '_', 'i', 'd', '"', ':', '"', 't', 'e', 's', 't', ' ', 'g', 'r', 'o', 'u', 'p', ' ', 'I', 'D', '"', ',', '"', 's', 'y', 's', 't', 'e', 'm', '_', 'i', 'n', 'f', 'o', '"', ':', '{', '"', 'p', 'r', 'o', 'c', 'e', 's', 's', '_', 'i', 'd', '"', ':', '1', '2', '3', '}', '}', ',', '"', 'v', 'a', 'n', 't', 'a', 'g', 'e', '_', 'p', 'o', 'i', 'n', 't', '"', ':', '{', '"', 't', 'y', 'p', 'e', '"', ':', '"', 'c', 'l', 'i', 'e', 'n', 't', '"', ',', '"', 'n', 'a', 'm', 'e', '"', ':', '"', 'O', 'p', 'e', 'n', 'S', 'S', 'L', '/', 'x', '.', 'y', '.', 'z', '"', '}', '}', '}', 10, 30, '{', '"', 'n', 'a', 'm', 'e', '"', ':', '"', 't', 'r', 'a', 'n', 's', 'p', 'o', 'r', 't', ':', 'p', 'a', 'c', 'k', 'e', 't', '_', 's', 'e', 'n', 't', '"', ',', '"', 'd', 'a', 't', 'a', '"', ':', '{', '"', 'f', 'i', 'e', 'l', 'd', '1', '"', ':', '"', 'f', 'o', 'o', '"', ',', '"', 'f', 'i', 'e', 'l', 'd', '2', '"', ':', '"', 'b', 'a', 'r', '"', ',', '"', 'f', 'i', 'e', 'l', 'd', '3', '"', ':', '4', '2', ',', '"', 'f', 'i', 'e', 'l', 'd', '4', '"', ':', '"', '1', '1', '5', '2', '9', '2', '1', '5', '0', '4', '6', '0', '6', '8', '4', '6', '9', '7', '6', '"', ',', '"', 'f', 'i', 'e', 'l', 'd', '5', '"', ':', '"', '1', '8', '4', '4', '6', '7', '4', '4', '0', '7', '3', '7', '0', '9', '5', '5', '1', '6', '1', '5', '"', ',', '"', 'f', 'i', 'e', 'l', 'd', '6', '"', ':', 'f', 'a', 'l', 's', 'e', ',', '"', 'f', 'i', 'e', 'l', 'd', '7', '"', ':', 't', 'r', 'u', 'e', ',', '"', 'f', 'i', 'e', 'l', 'd', '8', '"', ':', '"', '0', '1', 'a', 'f', '"', ',', '"', 'f', 'i', 'e', 'l', 'd', '9', '"', ':', '"', '5', '5', '"', ',', '"', 's', 'u', 'b', 'g', 'r', 'o', 'u', 'p', '"', ':', '{', '"', 'f', 'i', 'e', 'l', 'd', '1', '0', '"', ':', '"', 'b', 'a', 'z', '"', '}', ',', '"', 'a', 'r', 'r', 'a', 'y', '"', ':', '[', '"', 'a', '"', ',', '"', 'b', '"', ']', '}', ',', '"', 't', 'i', 'm', 'e', '"', ':', '1', '7', '0', '6', '5', '3', '1', '1', '7', '0', '0', '0', '}', 10, 30, '{', '"', 'n', 'a', 'm', 'e', '"', ':', '"', 't', 'r', 'a', 'n', 's', 'p', 'o', 'r', 't', ':', 'p', 'a', 'c', 'k', 'e', 't', '_', 's', 'e', 'n', 't', '"', ',', '"', 'd', 'a', 't', 'a', '"', ':', '{', '"', 'f', 'i', 'e', 'l', 'd', '1', '"', ':', '"', 'b', 'a', 'r', '"', '}', ',', '"', 't', 'i', 'm', 'e', '"', ':', '1', '0', '0', '0', '}', 10 }; static const unsigned char bin_buf[] = { 0x01, 0xaf }; static OSSL_TIME last_time; static OSSL_TIME now(void *arg) { OSSL_TIME t = last_time; last_time = ossl_time_add(t, ossl_ms2time(1000)); return t; } static int test_qlog(void) { int testresult = 0; QLOG_TRACE_INFO qti = {0}; QLOG *qlog; BIO *bio; char *buf = NULL; size_t buf_len = 0; last_time = ossl_time_from_time_t(170653117); qti.odcid.id_len = 1; qti.odcid.id[0] = 0x55; qti.title = "test title"; qti.description = "test description"; qti.group_id = "test group ID"; qti.override_process_id = 123; qti.now_cb = now; qti.override_impl_name = "OpenSSL/x.y.z"; if (!TEST_ptr(qlog = ossl_qlog_new(&qti))) goto err; if (!TEST_true(ossl_qlog_set_event_type_enabled(qlog, QLOG_EVENT_TYPE_transport_packet_sent, 1))) goto err; if (!TEST_ptr(bio = BIO_new(BIO_s_mem()))) goto err; if (!TEST_true(ossl_qlog_set_sink_bio(qlog, bio))) goto err; QLOG_EVENT_BEGIN(qlog, transport, packet_sent) QLOG_STR("field1", "foo"); QLOG_STR_LEN("field2", "bar", 3); QLOG_I64("field3", 42); QLOG_I64("field4", 1ULL << 60); QLOG_U64("field5", UINT64_MAX); QLOG_BOOL("field6", 0); QLOG_BOOL("field7", 1); QLOG_BIN("field8", bin_buf, sizeof(bin_buf)); QLOG_CID("field9", &qti.odcid); QLOG_BEGIN("subgroup") QLOG_STR("field10", "baz"); QLOG_END() QLOG_BEGIN_ARRAY("array") QLOG_STR(NULL, "a"); QLOG_STR(NULL, "b"); QLOG_END_ARRAY() QLOG_EVENT_END() QLOG_EVENT_BEGIN(qlog, transport, packet_received) QLOG_STR("field1", "foo"); QLOG_EVENT_END() QLOG_EVENT_BEGIN(qlog, transport, packet_sent) QLOG_STR("field1", "bar"); QLOG_EVENT_END() if (!TEST_true(ossl_qlog_flush(qlog))) goto err; buf_len = BIO_get_mem_data(bio, &buf); if (!TEST_size_t_gt(buf_len, 0)) goto err; if (!TEST_mem_eq(buf, buf_len, expected, sizeof(expected))) goto err; testresult = 1; err: ossl_qlog_free(qlog); return testresult; } struct filter_spec { const char *filter; int expect_ok; uint32_t expect_event_type; int expect_event_enable; }; static const struct filter_spec filters[] = { { "*", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { "-*", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "+*", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { "* *", 1, QLOG_EVENT_TYPE_transport_packet_received, 1 }, { "-* +*", 1, QLOG_EVENT_TYPE_transport_packet_received, 1 }, { "-* +* -*", 1, QLOG_EVENT_TYPE_transport_packet_received, 0 }, { " *", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { " ", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { "transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_received, 0 }, { "* -transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_received, 1 }, { "* -transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "unknown:event", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "unknown:event +transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { "unknown:event transport:*", 1, QLOG_EVENT_TYPE_transport_packet_sent, 1 }, { "unknown:event +transport:* -transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_received, 1 }, { "unknown:event transport:* -transport:packet_sent", 1, QLOG_EVENT_TYPE_transport_packet_sent, 0 }, { "* -transport:*", 1, QLOG_EVENT_TYPE_connectivity_connection_started, 1 }, { "* -transport:*", 1, QLOG_EVENT_TYPE_transport_parameters_set, 0 }, { "&", 0 }, { "event_name_without_category", 0 }, { "event_name_with_@badchar:foo", 0 }, { "event_name_with_badchar:f@oo", 0 }, { "category:", 0 }, { ":name", 0 }, { ":", 0 }, { "**", 0 }, { "foo:bar*", 0 }, { "foo:*bar", 0 }, { "foo*:bar", 0 }, { "*foo:bar", 0 }, }; static int test_qlog_filter(int idx) { int testresult = 0; QLOG_TRACE_INFO qti = {0}; QLOG *qlog; qti.odcid.id_len = 1; qti.odcid.id[0] = 0x55; if (!TEST_ptr(qlog = ossl_qlog_new(&qti))) goto err; if (!TEST_int_eq(ossl_qlog_set_filter(qlog, filters[idx].filter), filters[idx].expect_ok)) goto err; if (filters[idx].expect_event_type != QLOG_EVENT_TYPE_NONE) if (!TEST_int_eq(ossl_qlog_enabled(qlog, filters[idx].expect_event_type), filters[idx].expect_event_enable)) goto err; testresult = 1; err: ossl_qlog_free(qlog); return testresult; } int setup_tests(void) { ADD_TEST(test_qlog); ADD_ALL_TESTS(test_qlog_filter, OSSL_NELEM(filters)); return 1; }
test
openssl/test/quic_qlog_test.c
openssl
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "internal/nelem.h" #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/rand.h> #include <openssl/err.h> #include "testutil.h" #define NUM_BITS (BN_BITS2 * 4) #define BN_print_var(v) test_output_bignum(#v, v) static int a_is_zero_mod_one(const char *method, const BIGNUM *r, const BIGNUM *a) { if (!BN_is_zero(r)) { TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method); BN_print_var(a); BN_print_var(r); return 0; } return 1; } static int test_mod_exp_zero(void) { BIGNUM *a = NULL, *p = NULL, *m = NULL; BIGNUM *r = NULL; BN_ULONG one_word = 1; BN_CTX *ctx = BN_CTX_new(); int ret = 0, failed = 0; BN_MONT_CTX *mont = NULL; if (!TEST_ptr(m = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(r = BN_new())) goto err; BN_one(m); BN_one(a); BN_zero(p); if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_true(BN_mod_exp(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))) failed = 1; if (!TEST_ptr(mont = BN_MONT_CTX_new())) goto err; ERR_set_mark(); if (!TEST_false(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont))) goto err; if (!TEST_false(BN_mod_exp_mont(r, p, a, m, ctx, mont))) goto err; ERR_pop_to_mark(); if (!TEST_true(BN_MONT_CTX_set(mont, m, ctx))) goto err; if (!TEST_true(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont(r, p, a, m, ctx, mont))) goto err; if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a))) failed = 1; if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))) goto err; if (!TEST_BN_eq_zero(r)) { TEST_error("BN_mod_exp_mont_word failed: " "1 ** 0 mod 1 = r (should be 0)"); BN_print_var(r); goto err; } ret = !failed; err: BN_free(r); BN_free(a); BN_free(p); BN_free(m); BN_MONT_CTX_free(mont); BN_CTX_free(ctx); return ret; } static int test_mod_exp(int round) { BN_CTX *ctx; unsigned char c; int ret = 0; BIGNUM *r_mont = NULL; BIGNUM *r_mont_const = NULL; BIGNUM *r_recp = NULL; BIGNUM *r_simple = NULL; BIGNUM *a = NULL; BIGNUM *b = NULL; BIGNUM *m = NULL; if (!TEST_ptr(ctx = BN_CTX_new())) goto err; if (!TEST_ptr(r_mont = BN_new()) || !TEST_ptr(r_mont_const = BN_new()) || !TEST_ptr(r_recp = BN_new()) || !TEST_ptr(r_simple = BN_new()) || !TEST_ptr(a = BN_new()) || !TEST_ptr(b = BN_new()) || !TEST_ptr(m = BN_new())) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))) goto err; if (!TEST_int_gt(RAND_bytes(&c, 1), 0)) goto err; c = (c % BN_BITS) - BN_BITS2; if (!TEST_true(BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))) goto err; if (!TEST_true(BN_mod(a, a, m, ctx)) || !TEST_true(BN_mod(b, b, m, ctx)) || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL)) || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx)) || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL))) goto err; if (!TEST_BN_eq(r_simple, r_mont) || !TEST_BN_eq(r_simple, r_recp) || !TEST_BN_eq(r_simple, r_mont_const)) { if (BN_cmp(r_simple, r_mont) != 0) TEST_info("simple and mont results differ"); if (BN_cmp(r_simple, r_mont_const) != 0) TEST_info("simple and mont const time results differ"); if (BN_cmp(r_simple, r_recp) != 0) TEST_info("simple and recp results differ"); BN_print_var(a); BN_print_var(b); BN_print_var(m); BN_print_var(r_simple); BN_print_var(r_recp); BN_print_var(r_mont); BN_print_var(r_mont_const); goto err; } ret = 1; err: BN_free(r_mont); BN_free(r_mont_const); BN_free(r_recp); BN_free(r_simple); BN_free(a); BN_free(b); BN_free(m); BN_CTX_free(ctx); return ret; } static int test_mod_exp_x2(int idx) { BN_CTX *ctx; int ret = 0; BIGNUM *r_mont_const_x2_1 = NULL; BIGNUM *r_mont_const_x2_2 = NULL; BIGNUM *r_simple1 = NULL; BIGNUM *r_simple2 = NULL; BIGNUM *a1 = NULL; BIGNUM *b1 = NULL; BIGNUM *m1 = NULL; BIGNUM *a2 = NULL; BIGNUM *b2 = NULL; BIGNUM *m2 = NULL; int factor_size = 0; if (idx <= 100) factor_size = 1024; else if (idx <= 200) factor_size = 1536; else if (idx <= 300) factor_size = 2048; if (!TEST_ptr(ctx = BN_CTX_new())) goto err; if (!TEST_ptr(r_mont_const_x2_1 = BN_new()) || !TEST_ptr(r_mont_const_x2_2 = BN_new()) || !TEST_ptr(r_simple1 = BN_new()) || !TEST_ptr(r_simple2 = BN_new()) || !TEST_ptr(a1 = BN_new()) || !TEST_ptr(b1 = BN_new()) || !TEST_ptr(m1 = BN_new()) || !TEST_ptr(a2 = BN_new()) || !TEST_ptr(b2 = BN_new()) || !TEST_ptr(m2 = BN_new())) goto err; BN_rand(a1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(b1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(m1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD); BN_rand(a2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(b2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY); BN_rand(m2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD); if (!TEST_true(BN_mod(a1, a1, m1, ctx)) || !TEST_true(BN_mod(b1, b1, m1, ctx)) || !TEST_true(BN_mod(a2, a2, m2, ctx)) || !TEST_true(BN_mod(b2, b2, m2, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple1, a1, b1, m1, ctx)) || !TEST_true(BN_mod_exp_simple(r_simple2, a2, b2, m2, ctx)) || !TEST_true(BN_mod_exp_mont_consttime_x2(r_mont_const_x2_1, a1, b1, m1, NULL, r_mont_const_x2_2, a2, b2, m2, NULL, ctx))) goto err; if (!TEST_BN_eq(r_simple1, r_mont_const_x2_1) || !TEST_BN_eq(r_simple2, r_mont_const_x2_2)) { if (BN_cmp(r_simple1, r_mont_const_x2_1) != 0) TEST_info("simple and mont const time x2 (#1) results differ"); if (BN_cmp(r_simple2, r_mont_const_x2_2) != 0) TEST_info("simple and mont const time x2 (#2) results differ"); BN_print_var(a1); BN_print_var(b1); BN_print_var(m1); BN_print_var(a2); BN_print_var(b2); BN_print_var(m2); BN_print_var(r_simple1); BN_print_var(r_simple2); BN_print_var(r_mont_const_x2_1); BN_print_var(r_mont_const_x2_2); goto err; } ret = 1; err: BN_free(r_mont_const_x2_1); BN_free(r_mont_const_x2_2); BN_free(r_simple1); BN_free(r_simple2); BN_free(a1); BN_free(b1); BN_free(m1); BN_free(a2); BN_free(b2); BN_free(m2); BN_CTX_free(ctx); return ret; } int setup_tests(void) { ADD_TEST(test_mod_exp_zero); ADD_ALL_TESTS(test_mod_exp, 200); ADD_ALL_TESTS(test_mod_exp_x2, 300); return 1; }
test
openssl/test/exptest.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/dtls1.h> #include <openssl/ssl.h> #include <openssl/err.h> #include "helpers/ssltestlib.h" #include "testutil.h" #include "../ssl/ssl_local.h" static int debug = 0; static unsigned int clnt_psk_callback(SSL *ssl, const char *hint, char *ident, unsigned int max_ident_len, unsigned char *psk, unsigned int max_psk_len) { BIO_snprintf(ident, max_ident_len, "psk"); if (max_psk_len > 20) max_psk_len = 20; memset(psk, 0x5a, max_psk_len); return max_psk_len; } static unsigned int srvr_psk_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { if (max_psk_len > 20) max_psk_len = 20; memset(psk, 0x5a, max_psk_len); return max_psk_len; } static int mtu_test(SSL_CTX *ctx, const char *cs, int no_etm) { SSL *srvr_ssl = NULL, *clnt_ssl = NULL; BIO *sc_bio = NULL; int i; size_t s; size_t mtus[30]; unsigned char buf[600]; int rv = 0; SSL_CONNECTION *clnt_sc; memset(buf, 0x5a, sizeof(buf)); if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl, NULL, NULL))) goto end; if (no_etm) SSL_set_options(srvr_ssl, SSL_OP_NO_ENCRYPT_THEN_MAC); if (!TEST_true(SSL_set_cipher_list(srvr_ssl, cs)) || !TEST_true(SSL_set_cipher_list(clnt_ssl, cs)) || !TEST_ptr(sc_bio = SSL_get_rbio(srvr_ssl)) || !TEST_true(create_ssl_connection(clnt_ssl, srvr_ssl, SSL_ERROR_NONE))) goto end; if (debug) TEST_info("Channel established"); for (i = 0; i < 30; i++) { SSL_set_mtu(clnt_ssl, 500 + i); mtus[i] = DTLS_get_data_mtu(clnt_ssl); if (debug) TEST_info("%s%s MTU for record mtu %d = %lu", cs, no_etm ? "-noEtM" : "", 500 + i, (unsigned long)mtus[i]); if (!TEST_size_t_ne(mtus[i], 0)) { TEST_info("Cipher %s MTU %d", cs, 500 + i); goto end; } } SSL_set_mtu(clnt_ssl, 1000); for (s = mtus[0]; s <= mtus[29]; s++) { size_t reclen; if (!TEST_int_eq(SSL_write(clnt_ssl, buf, s), (int)s)) goto end; reclen = BIO_read(sc_bio, buf, sizeof(buf)); if (debug) TEST_info("record %zu for payload %zu", reclen, s); for (i = 0; i < 30; i++) { if (!TEST_false(s <= mtus[i] && reclen > (size_t)(500 + i))) { TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d", cs, (unsigned long)s, (unsigned long)mtus[i], (unsigned long)reclen, 500 + i); goto end; } if (!TEST_false(s > mtus[i] && reclen <= (size_t)(500 + i))) { TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d", cs, (unsigned long)s, (unsigned long)mtus[i], (unsigned long)reclen, 500 + i); goto end; } } } if (!TEST_ptr(clnt_sc = SSL_CONNECTION_FROM_SSL_ONLY(clnt_ssl))) goto end; rv = 1; if (SSL_READ_ETM(clnt_sc)) rv = 2; end: SSL_free(clnt_ssl); SSL_free(srvr_ssl); return rv; } static int run_mtu_tests(void) { SSL_CTX *ctx = NULL; STACK_OF(SSL_CIPHER) *ciphers; int i, ret = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method()))) goto end; SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback); SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback); SSL_CTX_set_security_level(ctx, 0); if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "PSK"))) goto end; ciphers = SSL_CTX_get_ciphers(ctx); for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); const char *cipher_name = SSL_CIPHER_get_name(cipher); if (!HAS_PREFIX(cipher_name, "PSK-")) continue; if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 0), 0)) break; TEST_info("%s OK", cipher_name); if (ret == 1) continue; if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 1), 0)) break; TEST_info("%s without EtM OK", cipher_name); } end: SSL_CTX_free(ctx); return ret; } static int test_server_mtu_larger_than_max_fragment_length(void) { SSL_CTX *ctx = NULL; SSL *srvr_ssl = NULL, *clnt_ssl = NULL; int rv = 0; if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method()))) goto end; SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback); SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback); #ifndef OPENSSL_NO_DH if (!TEST_true(SSL_CTX_set_dh_auto(ctx, 1))) goto end; #endif if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl, NULL, NULL))) goto end; SSL_set_options(srvr_ssl, SSL_OP_NO_QUERY_MTU); if (!TEST_true(DTLS_set_link_mtu(srvr_ssl, 1500))) goto end; SSL_set_tlsext_max_fragment_length(clnt_ssl, TLSEXT_max_fragment_length_512); if (!TEST_true(create_ssl_connection(srvr_ssl, clnt_ssl, SSL_ERROR_NONE))) goto end; rv = 1; end: SSL_free(clnt_ssl); SSL_free(srvr_ssl); SSL_CTX_free(ctx); return rv; } int setup_tests(void) { ADD_TEST(run_mtu_tests); ADD_TEST(test_server_mtu_larger_than_max_fragment_length); return 1; } void cleanup_tests(void) { bio_s_mempacket_test_free(); }
test
openssl/test/dtls_mtu_test.c
openssl
#include <string.h> #include <openssl/params.h> #include <openssl/param_build.h> #include "internal/nelem.h" #include "testutil.h" static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END }; static int template_public_single_zero_test(int idx) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; int res = 0; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(zbn = BN_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", idx == 0 ? zbn : NULL)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; params = params_blt; if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_BN_eq(zbn_res, zbn)) goto err; res = 1; err: if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); BN_free(zbn); BN_free(zbn_res); return res; } static int template_private_single_zero_test(void) { OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; int res = 0; if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) || !TEST_ptr(zbn = BN_secure_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; params = params_blt; if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_int_eq(BN_get_flags(zbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(zbn_res, zbn)) goto err; res = 1; err: if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); BN_free(zbn); BN_free(zbn_res); return res; } static int template_public_test(int tstid) { OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; BIGNUM *zbn = NULL, *zbn_res = NULL; BIGNUM *pbn = NULL, *pbn_res = NULL; BIGNUM *nbn = NULL, *nbn_res = NULL; int i; long int l; int32_t i32; int64_t i64; double d; time_t t; char *utf = NULL; const char *cutf; int res = 0; if (!TEST_ptr(bld) || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999)) || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224)) || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875)) || !TEST_ptr(zbn = BN_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(pbn = BN_new()) || !TEST_true(BN_set_word(pbn, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) || !TEST_ptr(nbn = BN_secure_new()) || !TEST_true(BN_set_word(nbn, 1733)) || !TEST_true((BN_set_negative(nbn, 1), 1)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo", sizeof("foo"))) || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom", 0)) || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; switch (tstid) { case 0: params = params_blt; break; case 1: params = OSSL_PARAM_merge(params_blt, params_empty); break; case 2: params = OSSL_PARAM_dup(params_blt); break; case 3: p1 = OSSL_PARAM_merge(params_blt, params_empty); params = OSSL_PARAM_dup(p1); break; default: p1 = OSSL_PARAM_dup(params_blt); params = OSSL_PARAM_merge(p1, params_empty); break; } if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_true(OSSL_PARAM_get_int(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_int_eq(i, -6) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_true(OSSL_PARAM_get_int32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_int_eq((int)i32, 1532) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_int64(p, &i64)) || !TEST_long_eq((long)i64, -9999999) || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(long int)) || !TEST_true(OSSL_PARAM_get_long(p, &l)) || !TEST_long_eq(l, 42) || !TEST_ptr(p = OSSL_PARAM_locate(params, "t")) || !TEST_str_eq(p->key, "t") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(time_t)) || !TEST_true(OSSL_PARAM_get_time_t(p, &t)) || !TEST_time_t_eq(t, 11224) || !TEST_ptr(p = OSSL_PARAM_locate(params, "d")) || !TEST_true(OSSL_PARAM_get_double(p, &d)) || !TEST_str_eq(p->key, "d") || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL) || !TEST_size_t_eq(p->data_size, sizeof(double)) || !TEST_double_eq(d, 1.61803398875) || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s")) || !TEST_str_eq(p->data, "foo") || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0)) || !TEST_str_eq(utf, "foo") || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p")) || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf)) || !TEST_str_eq(cutf, "bar-boom") || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_BN_eq(zbn_res, zbn) || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) || !TEST_str_eq(p->key, "bignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) || !TEST_BN_eq(pbn_res, pbn) || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) || !TEST_str_eq(p->key, "negativebignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) || !TEST_BN_eq(nbn_res, nbn)) goto err; res = 1; err: OPENSSL_free(p1); if (params != params_blt) OPENSSL_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); OPENSSL_free(utf); BN_free(zbn); BN_free(zbn_res); BN_free(pbn); BN_free(pbn_res); BN_free(nbn); BN_free(nbn_res); return res; } static int template_private_test(int tstid) { int *data1 = NULL, *data2 = NULL, j; const int data1_num = 12; const int data1_size = data1_num * sizeof(int); const int data2_num = 5; const int data2_size = data2_num * sizeof(int); OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; unsigned int i; unsigned long int l; uint32_t i32; uint64_t i64; size_t st; BIGNUM *zbn = NULL, *zbn_res = NULL; BIGNUM *pbn = NULL, *pbn_res = NULL; BIGNUM *nbn = NULL, *nbn_res = NULL; int res = 0; if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size)) || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size)) || !TEST_ptr(bld = OSSL_PARAM_BLD_new())) goto err; for (j = 0; j < data1_num; j++) data1[j] = -16 * j; for (j = 0; j < data2_num; j++) data2[j] = 2 * j; if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) || !TEST_ptr(zbn = BN_secure_new()) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) || !TEST_ptr(pbn = BN_secure_new()) || !TEST_true(BN_set_word(pbn, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) || !TEST_ptr(nbn = BN_secure_new()) || !TEST_true(BN_set_word(nbn, 1733)) || !TEST_true((BN_set_negative(nbn, 1), 1)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1, data1_size)) || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2, data2_size)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; switch (tstid) { case 0: params = params_blt; break; case 1: params = OSSL_PARAM_merge(params_blt, params_empty); break; case 2: params = OSSL_PARAM_dup(params_blt); break; case 3: p1 = OSSL_PARAM_merge(params_blt, params_empty); params = OSSL_PARAM_dup(p1); break; default: p1 = OSSL_PARAM_dup(params_blt); params = OSSL_PARAM_merge(p1, params_empty); break; } if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(OSSL_PARAM_get_uint(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_uint_eq(i, 6) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_uint_eq((unsigned int)i32, 1532) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) || !TEST_ulong_eq((unsigned long)i64, 9999999) || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) || !TEST_ulong_eq(l, 42) || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "st") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(size_t)) || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) || !TEST_size_t_eq(st, 65537) || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "oct_s") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) || !TEST_mem_eq(p->data, p->data_size, data1, data1_size) || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) || !TEST_false(CRYPTO_secure_allocated(p->data)) || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data)) || !TEST_str_eq(p->key, "oct_p") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size) || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "zeronumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(zbn_res, zbn) || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "bignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(pbn_res, pbn) || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) || !TEST_true(CRYPTO_secure_allocated(p->data)) || !TEST_str_eq(p->key, "negativebignumber") || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) || !TEST_int_eq(BN_get_flags(nbn, BN_FLG_SECURE), BN_FLG_SECURE) || !TEST_BN_eq(nbn_res, nbn)) goto err; res = 1; err: OSSL_PARAM_free(p1); if (params != params_blt) OSSL_PARAM_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_BLD_free(bld); OPENSSL_secure_free(data1); OPENSSL_secure_free(data2); BN_free(zbn); BN_free(zbn_res); BN_free(pbn); BN_free(pbn_res); BN_free(nbn); BN_free(nbn_res); return res; } static int builder_limit_test(void) { const int n = 100; char names[100][3]; OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL; int i, res = 0; if (!TEST_ptr(bld)) goto err; for (i = 0; i < n; i++) { names[i][0] = 'A' + (i / 26) - 1; names[i][1] = 'a' + (i % 26) - 1; names[i][2] = '\0'; if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1))) goto err; } if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; for (i = 0; params[i].key != NULL; i++); if (!TEST_int_eq(i, n)) goto err; OSSL_PARAM_free(params); params = NULL; if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2)) || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) goto err; for (i = 0; params[i].key != NULL; i++); if (!TEST_int_eq(i, 1)) goto err; res = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); return res; } static int builder_merge_test(void) { static int data1[] = { 2, 3, 5, 7, 11, 15, 17 }; static unsigned char data2[] = { 2, 4, 6, 8, 10 }; OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new(); OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p; unsigned int i; unsigned long int l; uint32_t i32; uint64_t i64; size_t st; BIGNUM *bn_priv = NULL, *bn_priv_res = NULL; BIGNUM *bn_pub = NULL, *bn_pub_res = NULL; int res = 0; if (!TEST_ptr(bld) || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) || !TEST_ptr(bn_priv = BN_secure_new()) || !TEST_true(BN_set_word(bn_priv, 1729)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv)) || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) goto err; if (!TEST_ptr(bld2) || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1, sizeof(data1))) || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2, sizeof(data2))) || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99)) || !TEST_ptr(bn_pub = BN_new()) || !TEST_true(BN_set_word(bn_pub, 0x42)) || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub)) || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2))) goto err; if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt))) goto err; if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) || !TEST_true(OSSL_PARAM_get_uint(p, &i)) || !TEST_str_eq(p->key, "i") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int)) || !TEST_uint_eq(i, 6) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) || !TEST_str_eq(p->key, "i32") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) || !TEST_uint_eq((unsigned int)i32, 99) || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) || !TEST_str_eq(p->key, "i64") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) || !TEST_ulong_eq((unsigned long)i64, 9999999) || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) || !TEST_str_eq(p->key, "l") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) || !TEST_ulong_eq(l, 42) || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) || !TEST_str_eq(p->key, "st") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_size_t_eq(p->data_size, sizeof(size_t)) || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) || !TEST_size_t_eq(st, 65537) || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) || !TEST_str_eq(p->key, "oct_s") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1)) || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) || !TEST_str_eq(p->key, "oct_p") || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2)) || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub")) || !TEST_str_eq(p->key, "bignumber_pub") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res)) || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0) || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv")) || !TEST_str_eq(p->key, "bignumber_priv") || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res)) || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0)) goto err; res = 1; err: OSSL_PARAM_free(params); OSSL_PARAM_free(params_blt); OSSL_PARAM_free(params2_blt); OSSL_PARAM_BLD_free(bld); OSSL_PARAM_BLD_free(bld2); BN_free(bn_priv); BN_free(bn_priv_res); BN_free(bn_pub); BN_free(bn_pub_res); return res; } int setup_tests(void) { ADD_ALL_TESTS(template_public_single_zero_test, 2); ADD_ALL_TESTS(template_public_test, 5); if (CRYPTO_secure_malloc_init(1<<16, 16)) { ADD_TEST(template_private_single_zero_test); ADD_ALL_TESTS(template_private_test, 5); } ADD_TEST(builder_limit_test); ADD_TEST(builder_merge_test); return 1; }
test
openssl/test/param_build_test.c
openssl
#include <openssl/ssl.h> #include <string.h> #include "helpers/ssltestlib.h" #include "testutil.h" #include "internal/packet.h" static char *cert = NULL; static char *privkey = NULL; static BIO *s_to_c_fbio = NULL, *c_to_s_fbio = NULL; static int chseen = 0, shseen = 0, sccsseen = 0, ccsaftersh = 0; static int ccsbeforesh = 0, sappdataseen = 0, cappdataseen = 0, badccs = 0; static int badvers = 0, badsessid = 0; static unsigned char chsessid[SSL_MAX_SSL_SESSION_ID_LENGTH]; static size_t chsessidlen = 0; static int watchccs_new(BIO *bi); static int watchccs_free(BIO *a); static int watchccs_read(BIO *b, char *out, int outl); static int watchccs_write(BIO *b, const char *in, int inl); static long watchccs_ctrl(BIO *b, int cmd, long num, void *ptr); static int watchccs_gets(BIO *bp, char *buf, int size); static int watchccs_puts(BIO *bp, const char *str); # define BIO_TYPE_WATCHCCS_FILTER (0x80 | BIO_TYPE_FILTER) static BIO_METHOD *method_watchccs = NULL; static const BIO_METHOD *bio_f_watchccs_filter(void) { if (method_watchccs == NULL) { method_watchccs = BIO_meth_new(BIO_TYPE_WATCHCCS_FILTER, "Watch CCS filter"); if (method_watchccs == NULL || !BIO_meth_set_write(method_watchccs, watchccs_write) || !BIO_meth_set_read(method_watchccs, watchccs_read) || !BIO_meth_set_puts(method_watchccs, watchccs_puts) || !BIO_meth_set_gets(method_watchccs, watchccs_gets) || !BIO_meth_set_ctrl(method_watchccs, watchccs_ctrl) || !BIO_meth_set_create(method_watchccs, watchccs_new) || !BIO_meth_set_destroy(method_watchccs, watchccs_free)) return NULL; } return method_watchccs; } static int watchccs_new(BIO *bio) { BIO_set_init(bio, 1); return 1; } static int watchccs_free(BIO *bio) { BIO_set_init(bio, 0); return 1; } static int watchccs_read(BIO *bio, char *out, int outl) { int ret = 0; BIO *next = BIO_next(bio); if (outl <= 0) return 0; if (next == NULL) return 0; BIO_clear_retry_flags(bio); ret = BIO_read(next, out, outl); if (ret <= 0 && BIO_should_read(next)) BIO_set_retry_read(bio); return ret; } static int watchccs_write(BIO *bio, const char *in, int inl) { int ret = 0; BIO *next = BIO_next(bio); PACKET pkt, msg, msgbody, sessionid; unsigned int rectype, recvers, msgtype, expectedrecvers; if (inl <= 0) return 0; if (next == NULL) return 0; BIO_clear_retry_flags(bio); if (!PACKET_buf_init(&pkt, (const unsigned char *)in, inl)) return 0; while (PACKET_remaining(&pkt)) { if (!PACKET_get_1(&pkt, &rectype) || !PACKET_get_net_2(&pkt, &recvers) || !PACKET_get_length_prefixed_2(&pkt, &msg)) return 0; expectedrecvers = TLS1_2_VERSION; if (rectype == SSL3_RT_HANDSHAKE) { if (!PACKET_get_1(&msg, &msgtype) || !PACKET_get_length_prefixed_3(&msg, &msgbody)) return 0; if (msgtype == SSL3_MT_CLIENT_HELLO) { chseen++; if (!PACKET_forward(&msgbody, 34) || !PACKET_get_length_prefixed_1(&msgbody, &sessionid)) return 0; if (chseen == 1) { expectedrecvers = TLS1_VERSION; chsessidlen = PACKET_remaining(&sessionid); if (!PACKET_copy_bytes(&sessionid, chsessid, chsessidlen)) return 0; } else { if (PACKET_remaining(&sessionid) != chsessidlen || (chsessidlen > 0 && memcmp(chsessid, PACKET_data(&sessionid), chsessidlen) != 0)) badsessid = 1; } } else if (msgtype == SSL3_MT_SERVER_HELLO) { shseen++; if (!PACKET_forward(&msgbody, 34) || !PACKET_get_length_prefixed_1(&msgbody, &sessionid)) return 0; if (PACKET_remaining(&sessionid) != chsessidlen || (chsessidlen > 0 && memcmp(chsessid, PACKET_data(&sessionid), chsessidlen) != 0)) badsessid = 1; } } else if (rectype == SSL3_RT_CHANGE_CIPHER_SPEC) { if (bio == s_to_c_fbio) { if (!sappdataseen && chseen == 1 && shseen == 1 && !sccsseen) sccsseen = 1; else badccs = 1; } else if (!cappdataseen) { if (shseen == 1 && !ccsaftersh) ccsaftersh = 1; else if (shseen == 0 && !ccsbeforesh) ccsbeforesh = 1; else badccs = 1; } else { badccs = 1; } } else if (rectype == SSL3_RT_APPLICATION_DATA) { if (bio == s_to_c_fbio) sappdataseen = 1; else cappdataseen = 1; } if (recvers != expectedrecvers) badvers = 1; } ret = BIO_write(next, in, inl); if (ret <= 0 && BIO_should_write(next)) BIO_set_retry_write(bio); return ret; } static long watchccs_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 = 0; break; default: ret = BIO_ctrl(next, cmd, num, ptr); break; } return ret; } static int watchccs_gets(BIO *bio, char *buf, int size) { return -1; } static int watchccs_puts(BIO *bio, const char *str) { return watchccs_write(bio, str, strlen(str)); } static int test_tls13ccs(int tst) { SSL_CTX *sctx = NULL, *cctx = NULL; SSL *sssl = NULL, *cssl = NULL; int ret = 0; const char msg[] = "Dummy data"; char buf[80]; size_t written, readbytes; SSL_SESSION *sess = NULL; chseen = shseen = sccsseen = ccsaftersh = ccsbeforesh = 0; sappdataseen = cappdataseen = badccs = badvers = badsessid = 0; chsessidlen = 0; if (!TEST_true(create_ssl_ctx_pair(NULL, 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))) goto err; switch (tst) { case 0: case 3: case 6: case 9: break; case 1: case 4: case 7: case 10: SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); break; case 2: case 5: case 8: case 11: SSL_CTX_clear_options(sctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); break; default: TEST_error("Invalid test value"); goto err; } if (tst >= 6) { if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) || !TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE))) goto err; sess = SSL_get1_session(cssl); if (!TEST_ptr(sess)) goto err; SSL_shutdown(cssl); SSL_shutdown(sssl); SSL_free(sssl); SSL_free(cssl); sssl = cssl = NULL; } if ((tst >= 3 && tst <= 5) || tst >= 9) { #if defined(OPENSSL_NO_EC) # if !defined(OPENSSL_NO_DH) if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "ffdhe3072"))) goto err; # endif #else if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "P-384"))) goto err; #endif } s_to_c_fbio = BIO_new(bio_f_watchccs_filter()); c_to_s_fbio = BIO_new(bio_f_watchccs_filter()); if (!TEST_ptr(s_to_c_fbio) || !TEST_ptr(c_to_s_fbio)) { BIO_free(s_to_c_fbio); BIO_free(c_to_s_fbio); goto err; } if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, s_to_c_fbio, c_to_s_fbio))) goto err; if (tst >= 6) { if (!TEST_true(SSL_set_session(cssl, sess)) || !TEST_true(SSL_write_early_data(cssl, msg, strlen(msg), &written)) || (tst <= 8 && !TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_SUCCESS))) goto err; if (tst <= 8) { if (!TEST_int_gt(SSL_connect(cssl), 0)) goto err; } else { if (!TEST_int_le(SSL_connect(cssl), 0)) goto err; } if (!TEST_int_eq(SSL_read_early_data(sssl, buf, sizeof(buf), &readbytes), SSL_READ_EARLY_DATA_FINISH)) goto err; } if (!TEST_true(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE))) goto err; if (!TEST_false(badccs) || !TEST_false(badvers) || !TEST_false(badsessid)) goto err; switch (tst) { case 0: if (!TEST_true(sccsseen) || !TEST_true(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 1: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_eq(chsessidlen, 0)) goto err; break; case 2: if (!TEST_false(sccsseen) || !TEST_true(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 3: if (!TEST_true(sccsseen) || !TEST_true(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 4: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_eq(chsessidlen, 0)) goto err; break; case 5: if (!TEST_false(sccsseen) || !TEST_true(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 6: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_true(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 7: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_eq(chsessidlen, 0)) goto err; break; case 8: if (!TEST_false(sccsseen) || !TEST_false(ccsaftersh) || !TEST_true(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 9: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_true(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; case 10: if (!TEST_true(sccsseen) || !TEST_false(ccsaftersh) || !TEST_false(ccsbeforesh) || !TEST_size_t_eq(chsessidlen, 0)) goto err; break; case 11: if (!TEST_false(sccsseen) || !TEST_false(ccsaftersh) || !TEST_true(ccsbeforesh) || !TEST_size_t_gt(chsessidlen, 0)) goto err; break; } ret = 1; err: SSL_SESSION_free(sess); SSL_free(sssl); SSL_free(cssl); SSL_CTX_free(sctx); SSL_CTX_free(cctx); return ret; } OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(cert = test_get_argument(0)) || !TEST_ptr(privkey = test_get_argument(1))) return 0; ADD_ALL_TESTS(test_tls13ccs, 12); return 1; } void cleanup_tests(void) { BIO_meth_free(method_watchccs); }
test
openssl/test/tls13ccstest.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/provider.h> #include <openssl/rsa.h> #include <openssl/dh.h> #include <openssl/core_names.h> #include <openssl/ui.h> #include "testutil.h" #include "internal/nelem.h" static OSSL_LIB_CTX *mainctx = NULL; static OSSL_PROVIDER *nullprov = 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, }; 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_DH static const unsigned char kExampleDHPrivateKeyDER[] = { 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B, 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20, 0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87, 0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1, 0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51, 0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D, 0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0, 0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3, 0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC5, 0x7D, 0x03, 0x8E, 0x05, 0x3C, 0x0A, 0x40, 0x17, 0xAD, 0xA8, 0x0F, 0x9B, 0xF4, 0x8B, 0xA7, 0xDB, 0x16, 0x4F, 0x4A, 0x57, 0x0B, 0x89, 0x80, 0x0B, 0x9F, 0x26, 0x56, 0x3F, 0x1D, 0xFA, 0x52, 0x2D, 0x1A, 0x9E, 0xDC, 0x42, 0xA3, 0x2E, 0xA9, 0x87, 0xE3, 0x8B, 0x45, 0x5E, 0xEE, 0x99, 0xB8, 0x30, 0x15, 0x58, 0xA3, 0x5F, 0xB5, 0x69, 0xD8, 0x0C, 0xE8, 0x6B, 0x36, 0xD8, 0xAB, 0xD8, 0xE4, 0x77, 0x46, 0x13, 0xA2, 0x15, 0xB3, 0x9C, 0xAD, 0x99, 0x91, 0xE5, 0xA3, 0x30, 0x7D, 0x40, 0x70, 0xB3, 0x32, 0x5E, 0xAF, 0x96, 0x8D, 0xE6, 0x3F, 0x47, 0xA3, 0x18, 0xDA, 0xE1, 0x9A, 0x20, 0x11, 0xE1, 0x49, 0x51, 0x45, 0xE3, 0x8C, 0xA5, 0x56, 0x39, 0x67, 0xCB, 0x9D, 0xCF, 0xBA, 0xF4, 0x46, 0x4E, 0x0A, 0xB6, 0x0B, 0xA9, 0xB4, 0xF6, 0xF1, 0x6A, 0xC8, 0x63, 0xE2, 0xB4, 0xB2, 0x9F, 0x44, 0xAA, 0x0A, 0xDA, 0x53, 0xF7, 0x52, 0x14, 0x57, 0xEE, 0x2C, 0x5D, 0x31, 0x9C, 0x27, 0x03, 0x64, 0x9E, 0xC0, 0x1E, 0x4B, 0x1B, 0x4F, 0xEE, 0xA6, 0x3F, 0xC1, 0x3E, 0x61, 0x93, 0x02, 0x01, 0x02, 0x04, 0x82, 0x01, 0x04, 0x02, 0x82, 0x01, 0x00, 0x7E, 0xC2, 0x04, 0xF9, 0x95, 0xC7, 0xEF, 0x96, 0xBE, 0xA0, 0x9D, 0x2D, 0xC3, 0x0C, 0x3A, 0x67, 0x02, 0x7C, 0x7D, 0x3B, 0xC9, 0xB1, 0xDE, 0x13, 0x97, 0x64, 0xEF, 0x87, 0x80, 0x4F, 0xBF, 0xA2, 0xAC, 0x18, 0x6B, 0xD5, 0xB2, 0x42, 0x0F, 0xDA, 0x28, 0x40, 0x93, 0x40, 0xB2, 0x1E, 0x80, 0xB0, 0x6C, 0xDE, 0x9C, 0x54, 0xA4, 0xB4, 0x68, 0x29, 0xE0, 0x13, 0x57, 0x1D, 0xC9, 0x87, 0xC0, 0xDE, 0x2F, 0x1D, 0x72, 0xF0, 0xC0, 0xE4, 0x4E, 0x04, 0x48, 0xF5, 0x2D, 0x8D, 0x9A, 0x1B, 0xE5, 0xEB, 0x06, 0xAB, 0x7C, 0x74, 0x10, 0x3C, 0xA8, 0x2D, 0x39, 0xBC, 0xE3, 0x15, 0x3E, 0x63, 0x37, 0x8C, 0x1B, 0xF1, 0xB3, 0x99, 0xB6, 0xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB, 0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5, 0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03, 0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD, 0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F, 0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62, 0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A, 0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6, 0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E, 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26, 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82, 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E, 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03, 0x8D, 0x03 }; #endif #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 kExampleECKey2DER[] = { 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05, 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17, 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C, 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E, 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42 }; # ifndef OPENSSL_NO_ECX static const unsigned char kExampleECXKey2DER[] = { 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85, 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3, 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75 }; # endif #endif typedef struct APK_DATA_st { const unsigned char *kder; size_t size; int evptype; } APK_DATA; static APK_DATA keydata[] = { {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA}, {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA}, #ifndef OPENSSL_NO_EC # ifndef OPENSSL_NO_ECX {kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519}, # endif {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC}, {kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC}, #endif #ifndef OPENSSL_NO_DH {kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH}, #endif }; static int pkey_has_private(EVP_PKEY *key, const char *privtag, int use_octstring) { int ret = 0; if (use_octstring) { unsigned char buf[64]; ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf), NULL); } else { BIGNUM *bn = NULL; ret = EVP_PKEY_get_bn_param(key, privtag, &bn); BN_free(bn); } return ret; } static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype) { int ret = 0; OSSL_PARAM *pub_params = NULL, *keypair_params = NULL; EVP_PKEY *fromkey = NULL, *fromkeypair = NULL; EVP_PKEY_CTX *fromctx = NULL; const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv"; const int use_octstring = strcmp(keytype, "X25519") == 0; if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1) || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag))) goto end; if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1) || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag))) goto end; if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY, keypair_params), 1) || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring))) goto end; if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair, EVP_PKEY_KEYPAIR, keypair_params), 1) || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring))) goto end; ret = 1; end: EVP_PKEY_free(fromkeypair); EVP_PKEY_free(fromkey); EVP_PKEY_CTX_free(fromctx); OSSL_PARAM_free(keypair_params); OSSL_PARAM_free(pub_params); return ret; } #ifndef OPENSSL_NO_DH static int test_dh_tofrom_data_select(void) { int ret; OSSL_PARAM params[2]; EVP_PKEY *key = NULL; EVP_PKEY_CTX *gctx = NULL; # ifndef OPENSSL_NO_DEPRECATED_3_0 const DH *dhkey; const BIGNUM *privkey; # endif params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0); params[1] = OSSL_PARAM_construct_end(); ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL)) && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0) && TEST_true(EVP_PKEY_CTX_set_params(gctx, params)) && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0) && TEST_true(do_pkey_tofrom_data_select(key, "DHX")); # ifndef OPENSSL_NO_DEPRECATED_3_0 ret = ret && TEST_ptr(dhkey = EVP_PKEY_get0_DH(key)) && TEST_ptr(privkey = DH_get0_priv_key(dhkey)) && TEST_int_le(BN_num_bits(privkey), 225); # endif EVP_PKEY_free(key); EVP_PKEY_CTX_free(gctx); return ret; } static int test_dh_paramgen(void) { int ret; OSSL_PARAM params[3]; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *gctx = NULL; unsigned int pbits = 512; params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits); params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, "generator", 0); params[2] = OSSL_PARAM_construct_end(); ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL)) && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0) && TEST_true(EVP_PKEY_CTX_set_params(gctx, params)) && TEST_true(EVP_PKEY_paramgen(gctx, &pkey)) && TEST_ptr(pkey); EVP_PKEY_CTX_free(gctx); gctx = NULL; ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_eq(EVP_PKEY_param_check(gctx), 1) && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1); EVP_PKEY_CTX_free(gctx); EVP_PKEY_free(pkey); return ret; } static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value) { int ret; OSSL_PARAM params[2]; EVP_PKEY *pkey = NULL; if (EVP_PKEY_fromdata_init(ctx) != 1) return -1; params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0); params[1] = OSSL_PARAM_construct_end(); ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params); EVP_PKEY_free(pkey); return ret; } static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name) { int ret; unsigned int tmp = 0; OSSL_PARAM params[2]; EVP_PKEY *pkey = NULL; if (EVP_PKEY_fromdata_init(ctx) != 1) return -1; params[0] = OSSL_PARAM_construct_uint(name, &tmp); params[1] = OSSL_PARAM_construct_end(); ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params); EVP_PKEY_free(pkey); return ret; } static int test_dh_paramfromdata(void) { EVP_PKEY_CTX *ctx = NULL; int ret = 0; ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL)) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0) && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0) && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0); EVP_PKEY_CTX_free(ctx); return ret; } #endif #ifndef OPENSSL_NO_EC static int test_ec_d2i_i2d_pubkey(void) { int ret = 0; FILE *fp = NULL; EVP_PKEY *key = NULL, *outkey = NULL; static const char *filename = "pubkey.der"; if (!TEST_ptr(fp = fopen(filename, "wb")) || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256")) || !TEST_true(i2d_PUBKEY_fp(fp, key)) || !TEST_int_eq(fclose(fp), 0)) goto err; fp = NULL; if (!TEST_ptr(fp = fopen(filename, "rb")) || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL)) || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1)) goto err; ret = 1; err: EVP_PKEY_free(outkey); EVP_PKEY_free(key); fclose(fp); return ret; } static int test_ec_tofrom_data_select(void) { int ret; EVP_PKEY *key = NULL; ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256")) && TEST_true(do_pkey_tofrom_data_select(key, "EC")); EVP_PKEY_free(key); return ret; } # ifndef OPENSSL_NO_ECX static int test_ecx_tofrom_data_select(void) { int ret; EVP_PKEY *key = NULL; ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519")) && TEST_true(do_pkey_tofrom_data_select(key, "X25519")); EVP_PKEY_free(key); return ret; } # endif #endif #ifndef OPENSSL_NO_SM2 static int test_sm2_tofrom_data_select(void) { int ret; EVP_PKEY *key = NULL; ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "SM2")) && TEST_true(do_pkey_tofrom_data_select(key, "SM2")); EVP_PKEY_free(key); return ret; } #endif static int test_rsa_tofrom_data_select(void) { int ret; EVP_PKEY *key = NULL; const unsigned char *pdata = kExampleRSAKeyDER; int pdata_len = sizeof(kExampleRSAKeyDER); ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len, mainctx, NULL)) && TEST_true(do_pkey_tofrom_data_select(key, "RSA")); EVP_PKEY_free(key); return ret; } static int test_d2i_AutoPrivateKey_ex(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; BIGNUM *p_bn = NULL; BIGNUM *g_bn = NULL; BIGNUM *priv_bn = NULL; p = input; if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx, NULL)) || !TEST_ptr_eq(p, input + input_len) || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id)) goto done; if (ak->evptype == EVP_PKEY_RSA) { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D, &priv_bn))) goto done; } else if (ak->evptype == EVP_PKEY_X25519) { unsigned char buffer[32]; size_t len; if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, buffer, sizeof(buffer), &len))) goto done; } else { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv_bn))) goto done; } if (ak->evptype == EVP_PKEY_DH) { if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn)) || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G, &g_bn))) goto done; } ret = 1; done: BN_free(p_bn); BN_free(g_bn); BN_free(priv_bn); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_DES static int test_pkcs8key_nid_bio(void) { int ret; const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; static const char pwd[] = "PASSWORD"; EVP_PKEY *pkey = NULL, *pkey_dec = NULL; BIO *in = NULL, *enc_bio = NULL; char *enc_data = NULL; long enc_datalen = 0; OSSL_PROVIDER *provider = NULL; ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")) && TEST_ptr(enc_bio = BIO_new(BIO_s_mem())) && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))) && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL)) && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid, pwd, sizeof(pwd) - 1, NULL, NULL), 1) && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0) && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL, (void *)pwd)) && TEST_true(EVP_PKEY_eq(pkey, pkey_dec)); EVP_PKEY_free(pkey_dec); EVP_PKEY_free(pkey); BIO_free(in); BIO_free(enc_bio); OSSL_PROVIDER_unload(provider); return ret; } #endif static int test_alternative_default(void) { OSSL_LIB_CTX *oldctx; EVP_MD *sha256; int ok = 0; if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx)) || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; EVP_MD_free(sha256); sha256 = NULL; if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx) || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; ok = 1; err: EVP_MD_free(sha256); return ok; } static int test_provider_unload_effective(int testid) { EVP_MD *sha256 = NULL; OSSL_PROVIDER *provider = NULL; int ok = 0; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")) || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; if (testid > 0) { OSSL_PROVIDER_unload(provider); provider = NULL; EVP_MD_free(sha256); sha256 = NULL; } else { EVP_MD_free(sha256); sha256 = NULL; OSSL_PROVIDER_unload(provider); provider = NULL; } if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL))) goto err; ok = 1; err: EVP_MD_free(sha256); OSSL_PROVIDER_unload(provider); return ok; } static int test_d2i_PrivateKey_ex(int testid) { int ok = 0; OSSL_PROVIDER *provider = NULL; BIO *key_bio = NULL; EVP_PKEY *pkey = NULL; int id = (testid == 0) ? 0 : 2; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))) goto err; if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size))) goto err; if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; ERR_clear_error(); if (!TEST_int_ge(BIO_seek(key_bio, 0), 0)) goto err; ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL)); TEST_int_eq(ERR_peek_error(), 0); test_openssl_errors(); err: EVP_PKEY_free(pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); return ok; } static int test_PEM_read_bio_negative(int testid) { int ok = 0; OSSL_PROVIDER *provider = NULL; BIO *key_bio = NULL; EVP_PKEY *pkey = NULL; if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size))) goto err; ERR_clear_error(); if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; if (!TEST_int_ne(ERR_peek_error(), 0)) goto err; if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))) goto err; if (!TEST_int_ge(BIO_seek(key_bio, 0), 0)) goto err; ERR_clear_error(); if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL))) goto err; if (!TEST_int_ne(ERR_peek_error(), 0)) goto err; ok = 1; err: test_openssl_errors(); EVP_PKEY_free(pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); return ok; } static int test_PEM_read_bio_negative_wrong_password(int testid) { int ok = 0; OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default"); EVP_PKEY *read_pkey = NULL; EVP_PKEY *write_pkey = EVP_RSA_gen(1024); BIO *key_bio = BIO_new(BIO_s_mem()); const UI_METHOD *undo_ui_method = NULL; const UI_METHOD *ui_method = NULL; if (testid > 0) ui_method = UI_null(); if (!TEST_ptr(provider)) goto err; if (!TEST_ptr(key_bio)) goto err; if (!TEST_ptr(write_pkey)) goto err; undo_ui_method = UI_get_default_method(); UI_set_default_method(ui_method); if ( !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(), NULL, 0, NULL, "pass"))) goto err; ERR_clear_error(); read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL); if (!TEST_ptr_null(read_pkey)) goto err; if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD)) goto err; ok = 1; err: test_openssl_errors(); EVP_PKEY_free(read_pkey); EVP_PKEY_free(write_pkey); BIO_free(key_bio); OSSL_PROVIDER_unload(provider); UI_set_default_method(undo_ui_method); return ok; } static int do_fromdata_key_is_equal(const OSSL_PARAM params[], const EVP_PKEY *expected, const char *type) { EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; int ret; ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL)) && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, (OSSL_PARAM *)params), 1) && TEST_true(EVP_PKEY_eq(pkey, expected)); EVP_PKEY_CTX_free(ctx); EVP_PKEY_free(pkey); return ret; } #ifndef OPENSSL_NO_DSA static const unsigned char dsa_key[] = { 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d, 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02, 0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37, 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d, 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41, 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05, 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34, 0x02, 0x82, 0x01, 0x00, 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99, 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63, 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e, 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58, 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45, 0xfd, 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21, }; static const unsigned char dsa_p[] = { 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe, 0xfd, 0x1b }; static const unsigned char dsa_q[] = { 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31 }; static const unsigned char dsa_g[] = { 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37, 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d, 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41, 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05, 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34 }; static const unsigned char dsa_priv[] = { 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21 }; static const unsigned char dsa_pub[] = { 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99, 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63, 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e, 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58, 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45, 0xfd }; static int do_check_params(OSSL_PARAM key_params[], int expected) { EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL; EVP_PKEY *pkey = NULL; int ret; ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL)) && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1) && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey, EVP_PKEY_KEYPAIR, key_params), 1) && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected); EVP_PKEY_CTX_free(check_ctx); EVP_PKEY_CTX_free(gen_ctx); EVP_PKEY_free(pkey); return ret; } static int do_check_bn(OSSL_PARAM params[], const char *key, const unsigned char *expected, size_t expected_len) { OSSL_PARAM *p; BIGNUM *bn = NULL; unsigned char buffer[256 + 1]; int ret, len; ret = TEST_ptr(p = OSSL_PARAM_locate(params, key)) && TEST_true(OSSL_PARAM_get_BN(p, &bn)) && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0) && TEST_mem_eq(expected, expected_len, buffer, len); BN_free(bn); return ret; } static int do_check_int(OSSL_PARAM params[], const char *key, int expected) { OSSL_PARAM *p; int val = 0; return TEST_ptr(p = OSSL_PARAM_locate(params, key)) && TEST_true(OSSL_PARAM_get_int(p, &val)) && TEST_int_eq(val, expected); } static int test_dsa_tofrom_data_select(void) { int ret; EVP_PKEY *key = NULL; const unsigned char *pkeydata = dsa_key; ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key), mainctx, NULL)) && TEST_true(do_pkey_tofrom_data_select(key, "DSA")); EVP_PKEY_free(key); return ret; } static int test_dsa_todata(void) { EVP_PKEY *pkey = NULL; OSSL_PARAM *to_params = NULL, *all_params = NULL; OSSL_PARAM gen_params[4]; int ret = 0; const unsigned char *pkeydata = dsa_key; unsigned char dsa_seed[] = { 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd, 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b, 0x90, 0xc1, 0xe3, 0xe0 }; int dsa_pcounter = 319; int dsa_gindex = 5; gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED, (void*)dsa_seed, sizeof(dsa_seed)); gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &dsa_gindex); gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, &dsa_pcounter); gen_params[3] = OSSL_PARAM_construct_end(); if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key), mainctx, NULL)) || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub, sizeof(dsa_pub)) || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv, sizeof(dsa_priv)) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1) || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0) || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED))) goto err; if (!do_fromdata_key_is_equal(to_params, pkey, "DSA")) goto err; if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params)) || !do_check_params(all_params, 1)) goto err; gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &dsa_gindex); gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, &dsa_pcounter); dsa_gindex++; if (!do_check_params(all_params, 0)) goto err; dsa_gindex--; dsa_pcounter++; if (!do_check_params(all_params, 0)) goto err; dsa_pcounter--; dsa_seed[0] = 0xb0; if (!do_check_params(all_params, 0)) goto err; ret = 1; err: EVP_PKEY_free(pkey); OSSL_PARAM_free(all_params); OSSL_PARAM_free(to_params); return ret; } static int test_dsa_fromdata_digest_prop(int tstid) { EVP_PKEY_CTX *ctx = NULL, *gctx = NULL; EVP_PKEY *pkey = NULL, *pkey2 = NULL; OSSL_PARAM params[4], *p = params; int ret = 0; int expected = (tstid == 0 ? 0 : 1); unsigned int pbits = 512; *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, tstid == 0 ? "provider=unknown" : "provider=default", 0); *p++ = OSSL_PARAM_construct_end(); if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL)) || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1) || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1)) goto err; if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1) || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected)) goto err; ret = 1; err: EVP_PKEY_free(pkey2); EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(gctx); return ret; } #endif static int test_pkey_todata_null(void) { OSSL_PARAM *params = NULL; EVP_PKEY *pkey = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, &params), 0) && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0); EVP_PKEY_free(pkey); return ret; } static OSSL_CALLBACK test_pkey_export_cb; static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg) { if (arg == NULL) return 0; return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA"); } static int test_pkey_export_null(void) { EVP_PKEY *pkey = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL), 0) && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0); EVP_PKEY_free(pkey); return ret; } static int test_pkey_export(void) { EVP_PKEY *pkey = NULL; #ifndef OPENSSL_NO_DEPRECATED_3_0 RSA *rsa = NULL; #endif int ret = 1; const unsigned char *pdata = keydata[0].kder; int pdata_len = keydata[0].size; if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len, mainctx, NULL)) || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, pkey)) || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL))) ret = 0; EVP_PKEY_free(pkey); #ifndef OPENSSL_NO_DEPRECATED_3_0 pdata = keydata[0].kder; pdata_len = keydata[0].size; if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len)) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)) || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, pkey)) || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, test_pkey_export_cb, NULL))) ret = 0; EVP_PKEY_free(pkey); #endif return ret; } static int test_rsa_pss_sign(void) { EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; int ret = 0; const unsigned char *pdata = keydata[0].kder; const char *mdname = "SHA2-256"; OSSL_PARAM sig_params[3]; unsigned char mdbuf[256 / 8] = { 0 }; int padding = RSA_PKCS1_PSS_PADDING; unsigned char *sig = NULL; size_t sig_len = 0; sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &padding); sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (char *)mdname, 0); sig_params[2] = OSSL_PARAM_construct_end(); ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size, mainctx, NULL)) && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL)) && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0) && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf, sizeof(mdbuf)), 0) && TEST_int_gt(sig_len, 0) && TEST_ptr(sig = OPENSSL_malloc(sig_len)) && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf, sizeof(mdbuf)), 0); EVP_PKEY_CTX_free(pctx); OPENSSL_free(sig); EVP_PKEY_free(pkey); return ret; } static int test_evp_md_ctx_dup(void) { EVP_MD_CTX *mdctx; EVP_MD_CTX *copyctx = NULL; int ret; ret = TEST_ptr(mdctx = EVP_MD_CTX_new()) && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx)); EVP_MD_CTX_free(mdctx); EVP_MD_CTX_free(copyctx); return ret; } static int test_evp_md_ctx_copy(void) { EVP_MD_CTX *mdctx = NULL; EVP_MD_CTX *copyctx = NULL; int ret; ret = TEST_ptr(mdctx = EVP_MD_CTX_new()) && TEST_ptr(copyctx = EVP_MD_CTX_new()) && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx)); EVP_MD_CTX_free(mdctx); EVP_MD_CTX_free(copyctx); return ret; } #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5 static int test_evp_pbe_alg_add(void) { int ret = 0; int cipher_nid = 0, md_nid = 0; EVP_PBE_KEYGEN_EX *keygen_ex = NULL; EVP_PBE_KEYGEN *keygen = NULL; if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(), PKCS5_PBE_keyivgen))) goto err; if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC, &cipher_nid, &md_nid, &keygen, &keygen_ex))) goto err; if (!TEST_true(keygen != NULL)) goto err; if (!TEST_true(keygen_ex == NULL)) goto err; ret = 1; err: return ret; } #endif static int evp_test_name_parsing(void) { EVP_MD *md; if (!TEST_ptr_null(md = EVP_MD_fetch(mainctx, "SHA256:BogusName", NULL))) { EVP_MD_free(md); return 0; } return 1; } int setup_tests(void) { if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) { OSSL_LIB_CTX_free(mainctx); mainctx = NULL; return 0; } ADD_TEST(evp_test_name_parsing); ADD_TEST(test_alternative_default); ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata)); #ifndef OPENSSL_NO_EC ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2); ADD_TEST(test_ec_tofrom_data_select); # ifndef OPENSSL_NO_ECX ADD_TEST(test_ecx_tofrom_data_select); # endif ADD_TEST(test_ec_d2i_i2d_pubkey); #else ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1); #endif #ifndef OPENSSL_NO_SM2 ADD_TEST(test_sm2_tofrom_data_select); #endif #ifndef OPENSSL_NO_DSA ADD_TEST(test_dsa_todata); ADD_TEST(test_dsa_tofrom_data_select); ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2); #endif #ifndef OPENSSL_NO_DH ADD_TEST(test_dh_tofrom_data_select); ADD_TEST(test_dh_paramgen); ADD_TEST(test_dh_paramfromdata); #endif ADD_TEST(test_rsa_tofrom_data_select); ADD_TEST(test_pkey_todata_null); ADD_TEST(test_pkey_export_null); ADD_TEST(test_pkey_export); #ifndef OPENSSL_NO_DES ADD_TEST(test_pkcs8key_nid_bio); #endif ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata)); ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2); ADD_TEST(test_rsa_pss_sign); ADD_TEST(test_evp_md_ctx_dup); ADD_TEST(test_evp_md_ctx_copy); ADD_ALL_TESTS(test_provider_unload_effective, 2); #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5 ADD_TEST(test_evp_pbe_alg_add); #endif return 1; } void cleanup_tests(void) { OSSL_LIB_CTX_free(mainctx); OSSL_PROVIDER_unload(nullprov); }
test
openssl/test/evp_extra_test2.c
openssl
#include <string.h> #include <openssl/evp.h> #include <openssl/provider.h> #include "testutil.h" static int is_fips; static int bad_fips; static int test_is_fips_enabled(void) { int is_fips_enabled, is_fips_loaded; EVP_MD *sha256 = NULL; is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL); is_fips_loaded = OSSL_PROVIDER_available(NULL, "fips"); if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled) || !TEST_int_eq(is_fips && !bad_fips, is_fips_loaded)) return 0; sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL); if (bad_fips) { if (!TEST_ptr_null(sha256)) { EVP_MD_free(sha256); return 0; } } else { if (!TEST_ptr(sha256)) return 0; if (is_fips && !TEST_str_eq(OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(sha256)), "fips")) { EVP_MD_free(sha256); return 0; } EVP_MD_free(sha256); } is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL); if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled)) return 0; return 1; } int setup_tests(void) { size_t argc; char *arg1; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } argc = test_get_argument_count(); switch (argc) { case 0: is_fips = 0; bad_fips = 0; break; case 1: arg1 = test_get_argument(0); if (strcmp(arg1, "fips") == 0) { is_fips = 1; bad_fips = 0; break; } else if (strcmp(arg1, "badfips") == 0) { is_fips = 0; bad_fips = 1; break; } default: TEST_error("Invalid argument\n"); return 0; } ADD_TEST(test_is_fips_enabled); return 1; }
test
openssl/test/defltfips_test.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include "internal/nelem.h" #include <openssl/crypto.h> #include <openssl/bio.h> #include <openssl/bn.h> #include <openssl/rand.h> #include <openssl/err.h> #include <openssl/obj_mac.h> #include <openssl/core_names.h> #include "testutil.h" #ifndef OPENSSL_NO_DH # include <openssl/dh.h> # include "crypto/bn_dh.h" # include "crypto/dh.h" static int cb(int p, int n, BN_GENCB *arg); static int dh_test(void) { DH *dh = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL; const BIGNUM *p2, *q2, *g2; BIGNUM *priv_key = NULL; const BIGNUM *pub_key2, *priv_key2; BN_GENCB *_cb = NULL; DH *a = NULL; DH *b = NULL; DH *c = NULL; const BIGNUM *ap = NULL, *ag = NULL, *apub_key = NULL; const BIGNUM *bpub_key = NULL, *bpriv_key = NULL; BIGNUM *bp = NULL, *bg = NULL, *cpriv_key = NULL; unsigned char *abuf = NULL; unsigned char *bbuf = NULL; unsigned char *cbuf = NULL; int i, alen, blen, clen, aout, bout, cout; int ret = 0; if (!TEST_ptr(dh = DH_new()) || !TEST_ptr(p = BN_new()) || !TEST_ptr(q = BN_new()) || !TEST_ptr(g = BN_new()) || !TEST_ptr(priv_key = BN_new())) goto err1; if (!TEST_true(BN_set_word(p, 4079L)) || !TEST_true(BN_set_word(q, 2039L)) || !TEST_true(BN_set_word(g, 3L)) || !TEST_true(DH_set0_pqg(dh, p, q, g))) goto err1; if (!TEST_true(DH_check(dh, &i))) goto err2; i ^= DH_MODULUS_TOO_SMALL; if (!TEST_false(i & DH_CHECK_P_NOT_PRIME) || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME) || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR) || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR) || !TEST_false(i & DH_CHECK_Q_NOT_PRIME) || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE) || !TEST_false(i & DH_CHECK_INVALID_J_VALUE) || !TEST_false(i & DH_MODULUS_TOO_SMALL) || !TEST_false(i & DH_MODULUS_TOO_LARGE) || !TEST_false(i)) goto err2; DH_get0_pqg(dh, &p2, &q2, &g2); if (!TEST_ptr_eq(p2, p) || !TEST_ptr_eq(q2, q) || !TEST_ptr_eq(g2, g)) goto err2; if (!TEST_ptr_eq(DH_get0_p(dh), p2) || !TEST_ptr_eq(DH_get0_q(dh), q2) || !TEST_ptr_eq(DH_get0_g(dh), g2)) goto err2; if (!TEST_true(BN_set_word(priv_key, 1234L)) || !TEST_true(DH_set0_key(dh, NULL, priv_key))) goto err2; DH_get0_key(dh, &pub_key2, &priv_key2); if (!TEST_ptr_eq(pub_key2, NULL) || !TEST_ptr_eq(priv_key2, priv_key)) goto err3; if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2) || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2)) goto err3; if (!TEST_false(DH_generate_key(dh))) goto err3; ERR_clear_error(); if (!TEST_ptr(BN_copy(q, p)) || !TEST_true(BN_add(q, q, BN_value_one()))) goto err3; if (!TEST_true(DH_check(dh, &i))) goto err3; if (!TEST_true(i & DH_CHECK_INVALID_Q_VALUE) || !TEST_false(i & DH_CHECK_Q_NOT_PRIME)) goto err3; if (!TEST_true(BN_set_word(p, 1)) || !TEST_true(BN_lshift(p, p, OPENSSL_DH_CHECK_MAX_MODULUS_BITS))) goto err3; if (!TEST_false(DH_check(dh, &i))) goto err3; ERR_clear_error(); if (!TEST_ptr(_cb = BN_GENCB_new())) goto err3; BN_GENCB_set(_cb, &cb, NULL); if (!TEST_ptr(a = DH_new()) || !TEST_true(DH_generate_parameters_ex(a, 512, DH_GENERATOR_5, _cb))) goto err3; if (!TEST_true(DH_check(a, &i))) goto err3; if (!TEST_false(i & DH_CHECK_P_NOT_PRIME) || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME) || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR) || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR) || !TEST_false(i & DH_CHECK_Q_NOT_PRIME) || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE) || !TEST_false(i & DH_CHECK_INVALID_J_VALUE) || !TEST_false(i & DH_MODULUS_TOO_SMALL) || !TEST_false(i & DH_MODULUS_TOO_LARGE) || !TEST_false(i)) goto err3; DH_get0_pqg(a, &ap, NULL, &ag); if (!TEST_ptr(b = DH_new())) goto err3; if (!TEST_ptr(bp = BN_dup(ap)) || !TEST_ptr(bg = BN_dup(ag)) || !TEST_true(DH_set0_pqg(b, bp, NULL, bg))) goto err3; bp = bg = NULL; if (!DH_generate_key(a)) goto err3; DH_get0_key(a, &apub_key, NULL); if (!DH_generate_key(b)) goto err3; DH_get0_key(b, &bpub_key, &bpriv_key); if (!TEST_ptr(c = DHparams_dup(b)) || !TEST_ptr(cpriv_key = BN_dup(bpriv_key)) || !TEST_true(DH_set0_key(c, NULL, cpriv_key))) goto err3; cpriv_key = NULL; alen = DH_size(a); if (!TEST_ptr(abuf = OPENSSL_malloc(alen)) || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1)) goto err3; blen = DH_size(b); if (!TEST_ptr(bbuf = OPENSSL_malloc(blen)) || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1)) goto err3; clen = DH_size(c); if (!TEST_ptr(cbuf = OPENSSL_malloc(clen)) || !TEST_true((cout = DH_compute_key(cbuf, apub_key, c)) != -1)) goto err3; if (!TEST_true(aout >= 20) || !TEST_mem_eq(abuf, aout, bbuf, bout) || !TEST_mem_eq(abuf, aout, cbuf, cout)) goto err3; ret = 1; goto success; err1: BN_free(p); BN_free(q); BN_free(g); err2: BN_free(priv_key); err3: success: OPENSSL_free(abuf); OPENSSL_free(bbuf); OPENSSL_free(cbuf); DH_free(b); DH_free(a); DH_free(c); BN_free(bp); BN_free(bg); BN_free(cpriv_key); BN_GENCB_free(_cb); DH_free(dh); return ret; } static int cb(int p, int n, BN_GENCB *arg) { return 1; } static int dh_computekey_range_test(void) { int ret = 0, sz; DH *dh = NULL; BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL; unsigned char *buf = NULL; if (!TEST_ptr(p = BN_dup(&ossl_bignum_ffdhe2048_p)) || !TEST_ptr(q = BN_dup(&ossl_bignum_ffdhe2048_q)) || !TEST_ptr(g = BN_dup(&ossl_bignum_const_2)) || !TEST_ptr(dh = DH_new()) || !TEST_true(DH_set0_pqg(dh, p, q, g))) goto err; p = q = g = NULL; if (!TEST_int_gt(sz = DH_size(dh), 0) || !TEST_ptr(buf = OPENSSL_malloc(sz)) || !TEST_ptr(pub = BN_new()) || !TEST_ptr(priv = BN_new())) goto err; if (!TEST_true(BN_set_word(priv, 1)) || !TEST_true(DH_set0_key(dh, NULL, priv))) goto err; priv = NULL; if (!TEST_true(BN_set_word(pub, 1))) goto err; if (!TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0)) goto err; if (!TEST_ptr(BN_copy(pub, DH_get0_p(dh))) || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0)) goto err; if (!TEST_true(BN_sub_word(pub, 1)) || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0)) goto err; if (!TEST_true(BN_sub_word(pub, 1)) || !TEST_int_eq(ossl_dh_compute_key(buf, pub, dh), sz)) goto err; ret = 1; err: OPENSSL_free(buf); BN_free(priv); BN_free(pub); BN_free(g); BN_free(q); BN_free(p); DH_free(dh); return ret; } static const unsigned char dhtest_1024_160_xA[] = { 0xB9, 0xA3, 0xB3, 0xAE, 0x8F, 0xEF, 0xC1, 0xA2, 0x93, 0x04, 0x96, 0x50, 0x70, 0x86, 0xF8, 0x45, 0x5D, 0x48, 0x94, 0x3E }; static const unsigned char dhtest_1024_160_yA[] = { 0x2A, 0x85, 0x3B, 0x3D, 0x92, 0x19, 0x75, 0x01, 0xB9, 0x01, 0x5B, 0x2D, 0xEB, 0x3E, 0xD8, 0x4F, 0x5E, 0x02, 0x1D, 0xCC, 0x3E, 0x52, 0xF1, 0x09, 0xD3, 0x27, 0x3D, 0x2B, 0x75, 0x21, 0x28, 0x1C, 0xBA, 0xBE, 0x0E, 0x76, 0xFF, 0x57, 0x27, 0xFA, 0x8A, 0xCC, 0xE2, 0x69, 0x56, 0xBA, 0x9A, 0x1F, 0xCA, 0x26, 0xF2, 0x02, 0x28, 0xD8, 0x69, 0x3F, 0xEB, 0x10, 0x84, 0x1D, 0x84, 0xA7, 0x36, 0x00, 0x54, 0xEC, 0xE5, 0xA7, 0xF5, 0xB7, 0xA6, 0x1A, 0xD3, 0xDF, 0xB3, 0xC6, 0x0D, 0x2E, 0x43, 0x10, 0x6D, 0x87, 0x27, 0xDA, 0x37, 0xDF, 0x9C, 0xCE, 0x95, 0xB4, 0x78, 0x75, 0x5D, 0x06, 0xBC, 0xEA, 0x8F, 0x9D, 0x45, 0x96, 0x5F, 0x75, 0xA5, 0xF3, 0xD1, 0xDF, 0x37, 0x01, 0x16, 0x5F, 0xC9, 0xE5, 0x0C, 0x42, 0x79, 0xCE, 0xB0, 0x7F, 0x98, 0x95, 0x40, 0xAE, 0x96, 0xD5, 0xD8, 0x8E, 0xD7, 0x76 }; static const unsigned char dhtest_1024_160_xB[] = { 0x93, 0x92, 0xC9, 0xF9, 0xEB, 0x6A, 0x7A, 0x6A, 0x90, 0x22, 0xF7, 0xD8, 0x3E, 0x72, 0x23, 0xC6, 0x83, 0x5B, 0xBD, 0xDA }; static const unsigned char dhtest_1024_160_yB[] = { 0x71, 0x7A, 0x6C, 0xB0, 0x53, 0x37, 0x1F, 0xF4, 0xA3, 0xB9, 0x32, 0x94, 0x1C, 0x1E, 0x56, 0x63, 0xF8, 0x61, 0xA1, 0xD6, 0xAD, 0x34, 0xAE, 0x66, 0x57, 0x6D, 0xFB, 0x98, 0xF6, 0xC6, 0xCB, 0xF9, 0xDD, 0xD5, 0xA5, 0x6C, 0x78, 0x33, 0xF6, 0xBC, 0xFD, 0xFF, 0x09, 0x55, 0x82, 0xAD, 0x86, 0x8E, 0x44, 0x0E, 0x8D, 0x09, 0xFD, 0x76, 0x9E, 0x3C, 0xEC, 0xCD, 0xC3, 0xD3, 0xB1, 0xE4, 0xCF, 0xA0, 0x57, 0x77, 0x6C, 0xAA, 0xF9, 0x73, 0x9B, 0x6A, 0x9F, 0xEE, 0x8E, 0x74, 0x11, 0xF8, 0xD6, 0xDA, 0xC0, 0x9D, 0x6A, 0x4E, 0xDB, 0x46, 0xCC, 0x2B, 0x5D, 0x52, 0x03, 0x09, 0x0E, 0xAE, 0x61, 0x26, 0x31, 0x1E, 0x53, 0xFD, 0x2C, 0x14, 0xB5, 0x74, 0xE6, 0xA3, 0x10, 0x9A, 0x3D, 0xA1, 0xBE, 0x41, 0xBD, 0xCE, 0xAA, 0x18, 0x6F, 0x5C, 0xE0, 0x67, 0x16, 0xA2, 0xB6, 0xA0, 0x7B, 0x3C, 0x33, 0xFE }; static const unsigned char dhtest_1024_160_Z[] = { 0x5C, 0x80, 0x4F, 0x45, 0x4D, 0x30, 0xD9, 0xC4, 0xDF, 0x85, 0x27, 0x1F, 0x93, 0x52, 0x8C, 0x91, 0xDF, 0x6B, 0x48, 0xAB, 0x5F, 0x80, 0xB3, 0xB5, 0x9C, 0xAA, 0xC1, 0xB2, 0x8F, 0x8A, 0xCB, 0xA9, 0xCD, 0x3E, 0x39, 0xF3, 0xCB, 0x61, 0x45, 0x25, 0xD9, 0x52, 0x1D, 0x2E, 0x64, 0x4C, 0x53, 0xB8, 0x07, 0xB8, 0x10, 0xF3, 0x40, 0x06, 0x2F, 0x25, 0x7D, 0x7D, 0x6F, 0xBF, 0xE8, 0xD5, 0xE8, 0xF0, 0x72, 0xE9, 0xB6, 0xE9, 0xAF, 0xDA, 0x94, 0x13, 0xEA, 0xFB, 0x2E, 0x8B, 0x06, 0x99, 0xB1, 0xFB, 0x5A, 0x0C, 0xAC, 0xED, 0xDE, 0xAE, 0xAD, 0x7E, 0x9C, 0xFB, 0xB3, 0x6A, 0xE2, 0xB4, 0x20, 0x83, 0x5B, 0xD8, 0x3A, 0x19, 0xFB, 0x0B, 0x5E, 0x96, 0xBF, 0x8F, 0xA4, 0xD0, 0x9E, 0x34, 0x55, 0x25, 0x16, 0x7E, 0xCD, 0x91, 0x55, 0x41, 0x6F, 0x46, 0xF4, 0x08, 0xED, 0x31, 0xB6, 0x3C, 0x6E, 0x6D }; static const unsigned char dhtest_2048_224_xA[] = { 0x22, 0xE6, 0x26, 0x01, 0xDB, 0xFF, 0xD0, 0x67, 0x08, 0xA6, 0x80, 0xF7, 0x47, 0xF3, 0x61, 0xF7, 0x6D, 0x8F, 0x4F, 0x72, 0x1A, 0x05, 0x48, 0xE4, 0x83, 0x29, 0x4B, 0x0C }; static const unsigned char dhtest_2048_224_yA[] = { 0x1B, 0x3A, 0x63, 0x45, 0x1B, 0xD8, 0x86, 0xE6, 0x99, 0xE6, 0x7B, 0x49, 0x4E, 0x28, 0x8B, 0xD7, 0xF8, 0xE0, 0xD3, 0x70, 0xBA, 0xDD, 0xA7, 0xA0, 0xEF, 0xD2, 0xFD, 0xE7, 0xD8, 0xF6, 0x61, 0x45, 0xCC, 0x9F, 0x28, 0x04, 0x19, 0x97, 0x5E, 0xB8, 0x08, 0x87, 0x7C, 0x8A, 0x4C, 0x0C, 0x8E, 0x0B, 0xD4, 0x8D, 0x4A, 0x54, 0x01, 0xEB, 0x1E, 0x87, 0x76, 0xBF, 0xEE, 0xE1, 0x34, 0xC0, 0x38, 0x31, 0xAC, 0x27, 0x3C, 0xD9, 0xD6, 0x35, 0xAB, 0x0C, 0xE0, 0x06, 0xA4, 0x2A, 0x88, 0x7E, 0x3F, 0x52, 0xFB, 0x87, 0x66, 0xB6, 0x50, 0xF3, 0x80, 0x78, 0xBC, 0x8E, 0xE8, 0x58, 0x0C, 0xEF, 0xE2, 0x43, 0x96, 0x8C, 0xFC, 0x4F, 0x8D, 0xC3, 0xDB, 0x08, 0x45, 0x54, 0x17, 0x1D, 0x41, 0xBF, 0x2E, 0x86, 0x1B, 0x7B, 0xB4, 0xD6, 0x9D, 0xD0, 0xE0, 0x1E, 0xA3, 0x87, 0xCB, 0xAA, 0x5C, 0xA6, 0x72, 0xAF, 0xCB, 0xE8, 0xBD, 0xB9, 0xD6, 0x2D, 0x4C, 0xE1, 0x5F, 0x17, 0xDD, 0x36, 0xF9, 0x1E, 0xD1, 0xEE, 0xDD, 0x65, 0xCA, 0x4A, 0x06, 0x45, 0x5C, 0xB9, 0x4C, 0xD4, 0x0A, 0x52, 0xEC, 0x36, 0x0E, 0x84, 0xB3, 0xC9, 0x26, 0xE2, 0x2C, 0x43, 0x80, 0xA3, 0xBF, 0x30, 0x9D, 0x56, 0x84, 0x97, 0x68, 0xB7, 0xF5, 0x2C, 0xFD, 0xF6, 0x55, 0xFD, 0x05, 0x3A, 0x7E, 0xF7, 0x06, 0x97, 0x9E, 0x7E, 0x58, 0x06, 0xB1, 0x7D, 0xFA, 0xE5, 0x3A, 0xD2, 0xA5, 0xBC, 0x56, 0x8E, 0xBB, 0x52, 0x9A, 0x7A, 0x61, 0xD6, 0x8D, 0x25, 0x6F, 0x8F, 0xC9, 0x7C, 0x07, 0x4A, 0x86, 0x1D, 0x82, 0x7E, 0x2E, 0xBC, 0x8C, 0x61, 0x34, 0x55, 0x31, 0x15, 0xB7, 0x0E, 0x71, 0x03, 0x92, 0x0A, 0xA1, 0x6D, 0x85, 0xE5, 0x2B, 0xCB, 0xAB, 0x8D, 0x78, 0x6A, 0x68, 0x17, 0x8F, 0xA8, 0xFF, 0x7C, 0x2F, 0x5C, 0x71, 0x64, 0x8D, 0x6F }; static const unsigned char dhtest_2048_224_xB[] = { 0x4F, 0xF3, 0xBC, 0x96, 0xC7, 0xFC, 0x6A, 0x6D, 0x71, 0xD3, 0xB3, 0x63, 0x80, 0x0A, 0x7C, 0xDF, 0xEF, 0x6F, 0xC4, 0x1B, 0x44, 0x17, 0xEA, 0x15, 0x35, 0x3B, 0x75, 0x90 }; static const unsigned char dhtest_2048_224_yB[] = { 0x4D, 0xCE, 0xE9, 0x92, 0xA9, 0x76, 0x2A, 0x13, 0xF2, 0xF8, 0x38, 0x44, 0xAD, 0x3D, 0x77, 0xEE, 0x0E, 0x31, 0xC9, 0x71, 0x8B, 0x3D, 0xB6, 0xC2, 0x03, 0x5D, 0x39, 0x61, 0x18, 0x2C, 0x3E, 0x0B, 0xA2, 0x47, 0xEC, 0x41, 0x82, 0xD7, 0x60, 0xCD, 0x48, 0xD9, 0x95, 0x99, 0x97, 0x06, 0x22, 0xA1, 0x88, 0x1B, 0xBA, 0x2D, 0xC8, 0x22, 0x93, 0x9C, 0x78, 0xC3, 0x91, 0x2C, 0x66, 0x61, 0xFA, 0x54, 0x38, 0xB2, 0x07, 0x66, 0x22, 0x2B, 0x75, 0xE2, 0x4C, 0x2E, 0x3A, 0xD0, 0xC7, 0x28, 0x72, 0x36, 0x12, 0x95, 0x25, 0xEE, 0x15, 0xB5, 0xDD, 0x79, 0x98, 0xAA, 0x04, 0xC4, 0xA9, 0x69, 0x6C, 0xAC, 0xD7, 0x17, 0x20, 0x83, 0xA9, 0x7A, 0x81, 0x66, 0x4E, 0xAD, 0x2C, 0x47, 0x9E, 0x44, 0x4E, 0x4C, 0x06, 0x54, 0xCC, 0x19, 0xE2, 0x8D, 0x77, 0x03, 0xCE, 0xE8, 0xDA, 0xCD, 0x61, 0x26, 0xF5, 0xD6, 0x65, 0xEC, 0x52, 0xC6, 0x72, 0x55, 0xDB, 0x92, 0x01, 0x4B, 0x03, 0x7E, 0xB6, 0x21, 0xA2, 0xAC, 0x8E, 0x36, 0x5D, 0xE0, 0x71, 0xFF, 0xC1, 0x40, 0x0A, 0xCF, 0x07, 0x7A, 0x12, 0x91, 0x3D, 0xD8, 0xDE, 0x89, 0x47, 0x34, 0x37, 0xAB, 0x7B, 0xA3, 0x46, 0x74, 0x3C, 0x1B, 0x21, 0x5D, 0xD9, 0xC1, 0x21, 0x64, 0xA7, 0xE4, 0x05, 0x31, 0x18, 0xD1, 0x99, 0xBE, 0xC8, 0xEF, 0x6F, 0xC5, 0x61, 0x17, 0x0C, 0x84, 0xC8, 0x7D, 0x10, 0xEE, 0x9A, 0x67, 0x4A, 0x1F, 0xA8, 0xFF, 0xE1, 0x3B, 0xDF, 0xBA, 0x1D, 0x44, 0xDE, 0x48, 0x94, 0x6D, 0x68, 0xDC, 0x0C, 0xDD, 0x77, 0x76, 0x35, 0xA7, 0xAB, 0x5B, 0xFB, 0x1E, 0x4B, 0xB7, 0xB8, 0x56, 0xF9, 0x68, 0x27, 0x73, 0x4C, 0x18, 0x41, 0x38, 0xE9, 0x15, 0xD9, 0xC3, 0x00, 0x2E, 0xBC, 0xE5, 0x31, 0x20, 0x54, 0x6A, 0x7E, 0x20, 0x02, 0x14, 0x2B, 0x6C }; static const unsigned char dhtest_2048_224_Z[] = { 0x34, 0xD9, 0xBD, 0xDC, 0x1B, 0x42, 0x17, 0x6C, 0x31, 0x3F, 0xEA, 0x03, 0x4C, 0x21, 0x03, 0x4D, 0x07, 0x4A, 0x63, 0x13, 0xBB, 0x4E, 0xCD, 0xB3, 0x70, 0x3F, 0xFF, 0x42, 0x45, 0x67, 0xA4, 0x6B, 0xDF, 0x75, 0x53, 0x0E, 0xDE, 0x0A, 0x9D, 0xA5, 0x22, 0x9D, 0xE7, 0xD7, 0x67, 0x32, 0x28, 0x6C, 0xBC, 0x0F, 0x91, 0xDA, 0x4C, 0x3C, 0x85, 0x2F, 0xC0, 0x99, 0xC6, 0x79, 0x53, 0x1D, 0x94, 0xC7, 0x8A, 0xB0, 0x3D, 0x9D, 0xEC, 0xB0, 0xA4, 0xE4, 0xCA, 0x8B, 0x2B, 0xB4, 0x59, 0x1C, 0x40, 0x21, 0xCF, 0x8C, 0xE3, 0xA2, 0x0A, 0x54, 0x1D, 0x33, 0x99, 0x40, 0x17, 0xD0, 0x20, 0x0A, 0xE2, 0xC9, 0x51, 0x6E, 0x2F, 0xF5, 0x14, 0x57, 0x79, 0x26, 0x9E, 0x86, 0x2B, 0x0F, 0xB4, 0x74, 0xA2, 0xD5, 0x6D, 0xC3, 0x1E, 0xD5, 0x69, 0xA7, 0x70, 0x0B, 0x4C, 0x4A, 0xB1, 0x6B, 0x22, 0xA4, 0x55, 0x13, 0x53, 0x1E, 0xF5, 0x23, 0xD7, 0x12, 0x12, 0x07, 0x7B, 0x5A, 0x16, 0x9B, 0xDE, 0xFF, 0xAD, 0x7A, 0xD9, 0x60, 0x82, 0x84, 0xC7, 0x79, 0x5B, 0x6D, 0x5A, 0x51, 0x83, 0xB8, 0x70, 0x66, 0xDE, 0x17, 0xD8, 0xD6, 0x71, 0xC9, 0xEB, 0xD8, 0xEC, 0x89, 0x54, 0x4D, 0x45, 0xEC, 0x06, 0x15, 0x93, 0xD4, 0x42, 0xC6, 0x2A, 0xB9, 0xCE, 0x3B, 0x1C, 0xB9, 0x94, 0x3A, 0x1D, 0x23, 0xA5, 0xEA, 0x3B, 0xCF, 0x21, 0xA0, 0x14, 0x71, 0xE6, 0x7E, 0x00, 0x3E, 0x7F, 0x8A, 0x69, 0xC7, 0x28, 0xBE, 0x49, 0x0B, 0x2F, 0xC8, 0x8C, 0xFE, 0xB9, 0x2D, 0xB6, 0xA2, 0x15, 0xE5, 0xD0, 0x3C, 0x17, 0xC4, 0x64, 0xC9, 0xAC, 0x1A, 0x46, 0xE2, 0x03, 0xE1, 0x3F, 0x95, 0x29, 0x95, 0xFB, 0x03, 0xC6, 0x9D, 0x3C, 0xC4, 0x7F, 0xCB, 0x51, 0x0B, 0x69, 0x98, 0xFF, 0xD3, 0xAA, 0x6D, 0xE7, 0x3C, 0xF9, 0xF6, 0x38, 0x69 }; static const unsigned char dhtest_2048_256_xA[] = { 0x08, 0x81, 0x38, 0x2C, 0xDB, 0x87, 0x66, 0x0C, 0x6D, 0xC1, 0x3E, 0x61, 0x49, 0x38, 0xD5, 0xB9, 0xC8, 0xB2, 0xF2, 0x48, 0x58, 0x1C, 0xC5, 0xE3, 0x1B, 0x35, 0x45, 0x43, 0x97, 0xFC, 0xE5, 0x0E }; static const unsigned char dhtest_2048_256_yA[] = { 0x2E, 0x93, 0x80, 0xC8, 0x32, 0x3A, 0xF9, 0x75, 0x45, 0xBC, 0x49, 0x41, 0xDE, 0xB0, 0xEC, 0x37, 0x42, 0xC6, 0x2F, 0xE0, 0xEC, 0xE8, 0x24, 0xA6, 0xAB, 0xDB, 0xE6, 0x6C, 0x59, 0xBE, 0xE0, 0x24, 0x29, 0x11, 0xBF, 0xB9, 0x67, 0x23, 0x5C, 0xEB, 0xA3, 0x5A, 0xE1, 0x3E, 0x4E, 0xC7, 0x52, 0xBE, 0x63, 0x0B, 0x92, 0xDC, 0x4B, 0xDE, 0x28, 0x47, 0xA9, 0xC6, 0x2C, 0xB8, 0x15, 0x27, 0x45, 0x42, 0x1F, 0xB7, 0xEB, 0x60, 0xA6, 0x3C, 0x0F, 0xE9, 0x15, 0x9F, 0xCC, 0xE7, 0x26, 0xCE, 0x7C, 0xD8, 0x52, 0x3D, 0x74, 0x50, 0x66, 0x7E, 0xF8, 0x40, 0xE4, 0x91, 0x91, 0x21, 0xEB, 0x5F, 0x01, 0xC8, 0xC9, 0xB0, 0xD3, 0xD6, 0x48, 0xA9, 0x3B, 0xFB, 0x75, 0x68, 0x9E, 0x82, 0x44, 0xAC, 0x13, 0x4A, 0xF5, 0x44, 0x71, 0x1C, 0xE7, 0x9A, 0x02, 0xDC, 0xC3, 0x42, 0x26, 0x68, 0x47, 0x80, 0xDD, 0xDC, 0xB4, 0x98, 0x59, 0x41, 0x06, 0xC3, 0x7F, 0x5B, 0xC7, 0x98, 0x56, 0x48, 0x7A, 0xF5, 0xAB, 0x02, 0x2A, 0x2E, 0x5E, 0x42, 0xF0, 0x98, 0x97, 0xC1, 0xA8, 0x5A, 0x11, 0xEA, 0x02, 0x12, 0xAF, 0x04, 0xD9, 0xB4, 0xCE, 0xBC, 0x93, 0x7C, 0x3C, 0x1A, 0x3E, 0x15, 0xA8, 0xA0, 0x34, 0x2E, 0x33, 0x76, 0x15, 0xC8, 0x4E, 0x7F, 0xE3, 0xB8, 0xB9, 0xB8, 0x7F, 0xB1, 0xE7, 0x3A, 0x15, 0xAF, 0x12, 0xA3, 0x0D, 0x74, 0x6E, 0x06, 0xDF, 0xC3, 0x4F, 0x29, 0x0D, 0x79, 0x7C, 0xE5, 0x1A, 0xA1, 0x3A, 0xA7, 0x85, 0xBF, 0x66, 0x58, 0xAF, 0xF5, 0xE4, 0xB0, 0x93, 0x00, 0x3C, 0xBE, 0xAF, 0x66, 0x5B, 0x3C, 0x2E, 0x11, 0x3A, 0x3A, 0x4E, 0x90, 0x52, 0x69, 0x34, 0x1D, 0xC0, 0x71, 0x14, 0x26, 0x68, 0x5F, 0x4E, 0xF3, 0x7E, 0x86, 0x8A, 0x81, 0x26, 0xFF, 0x3F, 0x22, 0x79, 0xB5, 0x7C, 0xA6, 0x7E, 0x29 }; static const unsigned char dhtest_2048_256_xB[] = { 0x7D, 0x62, 0xA7, 0xE3, 0xEF, 0x36, 0xDE, 0x61, 0x7B, 0x13, 0xD1, 0xAF, 0xB8, 0x2C, 0x78, 0x0D, 0x83, 0xA2, 0x3B, 0xD4, 0xEE, 0x67, 0x05, 0x64, 0x51, 0x21, 0xF3, 0x71, 0xF5, 0x46, 0xA5, 0x3D }; static const unsigned char dhtest_2048_256_yB[] = { 0x57, 0x5F, 0x03, 0x51, 0xBD, 0x2B, 0x1B, 0x81, 0x74, 0x48, 0xBD, 0xF8, 0x7A, 0x6C, 0x36, 0x2C, 0x1E, 0x28, 0x9D, 0x39, 0x03, 0xA3, 0x0B, 0x98, 0x32, 0xC5, 0x74, 0x1F, 0xA2, 0x50, 0x36, 0x3E, 0x7A, 0xCB, 0xC7, 0xF7, 0x7F, 0x3D, 0xAC, 0xBC, 0x1F, 0x13, 0x1A, 0xDD, 0x8E, 0x03, 0x36, 0x7E, 0xFF, 0x8F, 0xBB, 0xB3, 0xE1, 0xC5, 0x78, 0x44, 0x24, 0x80, 0x9B, 0x25, 0xAF, 0xE4, 0xD2, 0x26, 0x2A, 0x1A, 0x6F, 0xD2, 0xFA, 0xB6, 0x41, 0x05, 0xCA, 0x30, 0xA6, 0x74, 0xE0, 0x7F, 0x78, 0x09, 0x85, 0x20, 0x88, 0x63, 0x2F, 0xC0, 0x49, 0x23, 0x37, 0x91, 0xAD, 0x4E, 0xDD, 0x08, 0x3A, 0x97, 0x8B, 0x88, 0x3E, 0xE6, 0x18, 0xBC, 0x5E, 0x0D, 0xD0, 0x47, 0x41, 0x5F, 0x2D, 0x95, 0xE6, 0x83, 0xCF, 0x14, 0x82, 0x6B, 0x5F, 0xBE, 0x10, 0xD3, 0xCE, 0x41, 0xC6, 0xC1, 0x20, 0xC7, 0x8A, 0xB2, 0x00, 0x08, 0xC6, 0x98, 0xBF, 0x7F, 0x0B, 0xCA, 0xB9, 0xD7, 0xF4, 0x07, 0xBE, 0xD0, 0xF4, 0x3A, 0xFB, 0x29, 0x70, 0xF5, 0x7F, 0x8D, 0x12, 0x04, 0x39, 0x63, 0xE6, 0x6D, 0xDD, 0x32, 0x0D, 0x59, 0x9A, 0xD9, 0x93, 0x6C, 0x8F, 0x44, 0x13, 0x7C, 0x08, 0xB1, 0x80, 0xEC, 0x5E, 0x98, 0x5C, 0xEB, 0xE1, 0x86, 0xF3, 0xD5, 0x49, 0x67, 0x7E, 0x80, 0x60, 0x73, 0x31, 0xEE, 0x17, 0xAF, 0x33, 0x80, 0xA7, 0x25, 0xB0, 0x78, 0x23, 0x17, 0xD7, 0xDD, 0x43, 0xF5, 0x9D, 0x7A, 0xF9, 0x56, 0x8A, 0x9B, 0xB6, 0x3A, 0x84, 0xD3, 0x65, 0xF9, 0x22, 0x44, 0xED, 0x12, 0x09, 0x88, 0x21, 0x93, 0x02, 0xF4, 0x29, 0x24, 0xC7, 0xCA, 0x90, 0xB8, 0x9D, 0x24, 0xF7, 0x1B, 0x0A, 0xB6, 0x97, 0x82, 0x3D, 0x7D, 0xEB, 0x1A, 0xFF, 0x5B, 0x0E, 0x8E, 0x4A, 0x45, 0xD4, 0x9F, 0x7F, 0x53, 0x75, 0x7E, 0x19, 0x13 }; static const unsigned char dhtest_2048_256_Z[] = { 0x86, 0xC7, 0x0B, 0xF8, 0xD0, 0xBB, 0x81, 0xBB, 0x01, 0x07, 0x8A, 0x17, 0x21, 0x9C, 0xB7, 0xD2, 0x72, 0x03, 0xDB, 0x2A, 0x19, 0xC8, 0x77, 0xF1, 0xD1, 0xF1, 0x9F, 0xD7, 0xD7, 0x7E, 0xF2, 0x25, 0x46, 0xA6, 0x8F, 0x00, 0x5A, 0xD5, 0x2D, 0xC8, 0x45, 0x53, 0xB7, 0x8F, 0xC6, 0x03, 0x30, 0xBE, 0x51, 0xEA, 0x7C, 0x06, 0x72, 0xCA, 0xC1, 0x51, 0x5E, 0x4B, 0x35, 0xC0, 0x47, 0xB9, 0xA5, 0x51, 0xB8, 0x8F, 0x39, 0xDC, 0x26, 0xDA, 0x14, 0xA0, 0x9E, 0xF7, 0x47, 0x74, 0xD4, 0x7C, 0x76, 0x2D, 0xD1, 0x77, 0xF9, 0xED, 0x5B, 0xC2, 0xF1, 0x1E, 0x52, 0xC8, 0x79, 0xBD, 0x95, 0x09, 0x85, 0x04, 0xCD, 0x9E, 0xEC, 0xD8, 0xA8, 0xF9, 0xB3, 0xEF, 0xBD, 0x1F, 0x00, 0x8A, 0xC5, 0x85, 0x30, 0x97, 0xD9, 0xD1, 0x83, 0x7F, 0x2B, 0x18, 0xF7, 0x7C, 0xD7, 0xBE, 0x01, 0xAF, 0x80, 0xA7, 0xC7, 0xB5, 0xEA, 0x3C, 0xA5, 0x4C, 0xC0, 0x2D, 0x0C, 0x11, 0x6F, 0xEE, 0x3F, 0x95, 0xBB, 0x87, 0x39, 0x93, 0x85, 0x87, 0x5D, 0x7E, 0x86, 0x74, 0x7E, 0x67, 0x6E, 0x72, 0x89, 0x38, 0xAC, 0xBF, 0xF7, 0x09, 0x8E, 0x05, 0xBE, 0x4D, 0xCF, 0xB2, 0x40, 0x52, 0xB8, 0x3A, 0xEF, 0xFB, 0x14, 0x78, 0x3F, 0x02, 0x9A, 0xDB, 0xDE, 0x7F, 0x53, 0xFA, 0xE9, 0x20, 0x84, 0x22, 0x40, 0x90, 0xE0, 0x07, 0xCE, 0xE9, 0x4D, 0x4B, 0xF2, 0xBA, 0xCE, 0x9F, 0xFD, 0x4B, 0x57, 0xD2, 0xAF, 0x7C, 0x72, 0x4D, 0x0C, 0xAA, 0x19, 0xBF, 0x05, 0x01, 0xF6, 0xF1, 0x7B, 0x4A, 0xA1, 0x0F, 0x42, 0x5E, 0x3E, 0xA7, 0x60, 0x80, 0xB4, 0xB9, 0xD6, 0xB3, 0xCE, 0xFE, 0xA1, 0x15, 0xB2, 0xCE, 0xB8, 0x78, 0x9B, 0xB8, 0xA3, 0xB0, 0xEA, 0x87, 0xFE, 0xBE, 0x63, 0xB6, 0xC8, 0xF8, 0x46, 0xEC, 0x6D, 0xB0, 0xC2, 0x6C, 0x5D, 0x7C }; typedef struct { DH *(*get_param) (void); const unsigned char *xA; size_t xA_len; const unsigned char *yA; size_t yA_len; const unsigned char *xB; size_t xB_len; const unsigned char *yB; size_t yB_len; const unsigned char *Z; size_t Z_len; } rfc5114_td; # define make_rfc5114_td(pre) { \ DH_get_##pre, \ dhtest_##pre##_xA, sizeof(dhtest_##pre##_xA), \ dhtest_##pre##_yA, sizeof(dhtest_##pre##_yA), \ dhtest_##pre##_xB, sizeof(dhtest_##pre##_xB), \ dhtest_##pre##_yB, sizeof(dhtest_##pre##_yB), \ dhtest_##pre##_Z, sizeof(dhtest_##pre##_Z) \ } static const rfc5114_td rfctd[] = { make_rfc5114_td(1024_160), make_rfc5114_td(2048_224), make_rfc5114_td(2048_256) }; static int rfc5114_test(void) { int i; DH *dhA = NULL; DH *dhB = NULL; unsigned char *Z1 = NULL; unsigned char *Z2 = NULL; int szA, szB; const rfc5114_td *td = NULL; BIGNUM *priv_key = NULL, *pub_key = NULL; const BIGNUM *pub_key_tmp; for (i = 0; i < (int)OSSL_NELEM(rfctd); i++) { td = rfctd + i; if (!TEST_ptr(dhA = td->get_param()) || !TEST_ptr(dhB = td->get_param())) goto bad_err; if (!TEST_ptr(priv_key = BN_bin2bn(td->xA, td->xA_len, NULL)) || !TEST_ptr(pub_key = BN_bin2bn(td->yA, td->yA_len, NULL)) || !TEST_true(DH_set0_key(dhA, pub_key, priv_key))) goto bad_err; if (!TEST_ptr(priv_key = BN_bin2bn(td->xB, td->xB_len, NULL)) || !TEST_ptr(pub_key = BN_bin2bn(td->yB, td->yB_len, NULL)) || !TEST_true(DH_set0_key(dhB, pub_key, priv_key))) goto bad_err; priv_key = pub_key = NULL; if (!TEST_int_gt(szA = DH_size(dhA), 0) || !TEST_int_gt(szB = DH_size(dhB), 0) || !TEST_size_t_eq(td->Z_len, (size_t)szA) || !TEST_size_t_eq(td->Z_len, (size_t)szB)) goto err; if (!TEST_ptr(Z1 = OPENSSL_malloc((size_t)szA)) || !TEST_ptr(Z2 = OPENSSL_malloc((size_t)szB))) goto bad_err; DH_get0_key(dhB, &pub_key_tmp, NULL); if (!TEST_int_ne(DH_compute_key(Z1, pub_key_tmp, dhA), -1)) goto bad_err; DH_get0_key(dhA, &pub_key_tmp, NULL); if (!TEST_int_ne(DH_compute_key(Z2, pub_key_tmp, dhB), -1)) goto bad_err; if (!TEST_mem_eq(Z1, td->Z_len, td->Z, td->Z_len) || !TEST_mem_eq(Z2, td->Z_len, td->Z, td->Z_len)) goto err; DH_free(dhA); dhA = NULL; DH_free(dhB); dhB = NULL; OPENSSL_free(Z1); Z1 = NULL; OPENSSL_free(Z2); Z2 = NULL; } return 1; bad_err: DH_free(dhA); DH_free(dhB); BN_free(pub_key); BN_free(priv_key); OPENSSL_free(Z1); OPENSSL_free(Z2); TEST_error("Initialisation error RFC5114 set %d\n", i + 1); return 0; err: DH_free(dhA); DH_free(dhB); OPENSSL_free(Z1); OPENSSL_free(Z2); TEST_error("Test failed RFC5114 set %d\n", i + 1); return 0; } static int rfc7919_test(void) { DH *a = NULL, *b = NULL; const BIGNUM *apub_key = NULL, *bpub_key = NULL; unsigned char *abuf = NULL; unsigned char *bbuf = NULL; int i, alen, blen, aout, bout; int ret = 0; if (!TEST_ptr(a = DH_new_by_nid(NID_ffdhe2048))) goto err; if (!DH_check(a, &i)) goto err; if (!TEST_false(i & DH_CHECK_P_NOT_PRIME) || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME) || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR) || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR) || !TEST_false(i)) goto err; if (!DH_generate_key(a)) goto err; DH_get0_key(a, &apub_key, NULL); if (!TEST_ptr(b = DH_new_by_nid(NID_ffdhe2048))) goto err; if (!DH_generate_key(b)) goto err; DH_get0_key(b, &bpub_key, NULL); alen = DH_size(a); if (!TEST_int_gt(alen, 0) || !TEST_ptr(abuf = OPENSSL_malloc(alen)) || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1)) goto err; blen = DH_size(b); if (!TEST_int_gt(blen, 0) || !TEST_ptr(bbuf = OPENSSL_malloc(blen)) || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1)) goto err; if (!TEST_true(aout >= 20) || !TEST_mem_eq(abuf, aout, bbuf, bout)) goto err; ret = 1; err: OPENSSL_free(abuf); OPENSSL_free(bbuf); DH_free(a); DH_free(b); return ret; } static int prime_groups[] = { NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096, NID_ffdhe6144, NID_ffdhe8192, NID_modp_2048, NID_modp_3072, NID_modp_4096, NID_modp_6144, }; static int dh_test_prime_groups(int index) { int ok = 0; DH *dh = NULL; const BIGNUM *p, *q, *g; if (!TEST_ptr(dh = DH_new_by_nid(prime_groups[index]))) goto err; DH_get0_pqg(dh, &p, &q, &g); if (!TEST_ptr(p) || !TEST_ptr(q) || !TEST_ptr(g)) goto err; if (!TEST_int_eq(DH_get_nid(dh), prime_groups[index])) goto err; if (!TEST_int_eq((int)DH_get_length(dh), 0)) goto err; ok = 1; err: DH_free(dh); return ok; } static int dh_rfc5114_fix_nid_test(void) { int ok = 0; EVP_PKEY_CTX *paramgen_ctx; paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DHX, 0); if (!TEST_ptr(paramgen_ctx)) goto err; if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 3), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 99), 0)) goto err; ok = 1; err: EVP_PKEY_CTX_free(paramgen_ctx); return ok; } static int dh_set_dh_nid_test(void) { int ok = 0; EVP_PKEY_CTX *paramgen_ctx; paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, 0); if (!TEST_ptr(paramgen_ctx)) goto err; if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_ffdhe2048), 1)) goto err; if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_secp521r1), 0)) goto err; ok = 1; err: EVP_PKEY_CTX_free(paramgen_ctx); return ok; } static int dh_get_nid(void) { int ok = 0; const BIGNUM *p, *q, *g; BIGNUM *pcpy = NULL, *gcpy = NULL, *qcpy = NULL; DH *dh1 = DH_new_by_nid(NID_ffdhe2048); DH *dh2 = DH_new(); if (!TEST_ptr(dh1) || !TEST_ptr(dh2)) goto err; DH_get0_pqg(dh1, &p, &q, &g); if (!TEST_ptr(p) || !TEST_ptr(q) || !TEST_ptr(g) || !TEST_ptr(pcpy = BN_dup(p)) || !TEST_ptr(gcpy = BN_dup(g))) goto err; if (!TEST_true(DH_set0_pqg(dh2, pcpy, NULL, gcpy))) goto err; pcpy = gcpy = NULL; if (!TEST_ptr(DH_get0_q(dh2))) goto err; if (!TEST_int_eq(DH_get_nid(dh2), NID_ffdhe2048)) goto err; if (!TEST_ptr(gcpy = BN_dup(BN_value_one()))) goto err; if (!TEST_true(DH_set0_pqg(dh2, NULL, NULL, gcpy))) goto err; gcpy = NULL; if (!TEST_int_eq(DH_get_nid(dh2), NID_undef)) goto err; if (!TEST_ptr(pcpy = BN_dup(p)) || !TEST_ptr(qcpy = BN_dup(q)) || !TEST_ptr(gcpy = BN_dup(g)) || !TEST_int_eq(BN_add_word(qcpy, 2), 1) || !TEST_true(DH_set0_pqg(dh2, pcpy, qcpy, gcpy))) goto err; pcpy = qcpy = gcpy = NULL; if (!TEST_int_eq(DH_get_nid(dh2), NID_undef)) goto err; ok = 1; err: BN_free(pcpy); BN_free(qcpy); BN_free(gcpy); DH_free(dh2); DH_free(dh1); return ok; } static const unsigned char dh_pub_der[] = { 0x30, 0x82, 0x02, 0x28, 0x30, 0x82, 0x01, 0x1b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x82, 0x01, 0x0c, 0x02, 0x82, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6, 0xf4, 0x06, 0xb7, 0xed, 0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5, 0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe4, 0x5b, 0x3d, 0xc2, 0x00, 0x7c, 0xb8, 0xa1, 0x63, 0xbf, 0x05, 0x98, 0xda, 0x48, 0x36, 0x1c, 0x55, 0xd3, 0x9a, 0x69, 0x16, 0x3f, 0xa8, 0xfd, 0x24, 0xcf, 0x5f, 0x83, 0x65, 0x5d, 0x23, 0xdc, 0xa3, 0xad, 0x96, 0x1c, 0x62, 0xf3, 0x56, 0x20, 0x85, 0x52, 0xbb, 0x9e, 0xd5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6d, 0x67, 0x0c, 0x35, 0x4e, 0x4a, 0xbc, 0x98, 0x04, 0xf1, 0x74, 0x6c, 0x08, 0xca, 0x18, 0x21, 0x7c, 0x32, 0x90, 0x5e, 0x46, 0x2e, 0x36, 0xce, 0x3b, 0xe3, 0x9e, 0x77, 0x2c, 0x18, 0x0e, 0x86, 0x03, 0x9b, 0x27, 0x83, 0xa2, 0xec, 0x07, 0xa2, 0x8f, 0xb5, 0xc5, 0x5d, 0xf0, 0x6f, 0x4c, 0x52, 0xc9, 0xde, 0x2b, 0xcb, 0xf6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7c, 0xea, 0x95, 0x6a, 0xe5, 0x15, 0xd2, 0x26, 0x18, 0x98, 0xfa, 0x05, 0x10, 0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x01, 0x02, 0x02, 0x02, 0x04, 0x00, 0x03, 0x82, 0x01, 0x05, 0x00, 0x02, 0x82, 0x01, 0x00, 0x08, 0x87, 0x8a, 0x5f, 0x4f, 0x3b, 0xef, 0xe1, 0x77, 0x13, 0x3b, 0xd7, 0x58, 0x76, 0xc9, 0xeb, 0x7e, 0x2d, 0xcc, 0x7e, 0xed, 0xc5, 0xee, 0xf9, 0x2d, 0x55, 0xb0, 0xe2, 0x37, 0x8c, 0x51, 0x87, 0x6a, 0x8e, 0x0d, 0xb2, 0x08, 0xed, 0x4f, 0x88, 0x9b, 0x63, 0x19, 0x7a, 0x67, 0xa1, 0x61, 0xd8, 0x17, 0xa0, 0x2c, 0xdb, 0xc2, 0xfa, 0xb3, 0x4f, 0xe7, 0xcb, 0x16, 0xf2, 0xe7, 0xd0, 0x2c, 0xf8, 0xcc, 0x97, 0xd3, 0xe7, 0xae, 0xc2, 0x71, 0xd8, 0x2b, 0x12, 0x83, 0xe9, 0x5a, 0x45, 0xfe, 0x66, 0x5c, 0xa2, 0xb6, 0xce, 0x2f, 0x04, 0x05, 0xe7, 0xa7, 0xbc, 0xe5, 0x63, 0x1a, 0x93, 0x3d, 0x4d, 0xf4, 0x77, 0xdd, 0x2a, 0xc9, 0x51, 0x7b, 0xf5, 0x54, 0xa2, 0xab, 0x26, 0xee, 0x16, 0xd3, 0x83, 0x92, 0x85, 0x40, 0x67, 0xa3, 0xa9, 0x31, 0x16, 0x64, 0x45, 0x5a, 0x2a, 0x9d, 0xa8, 0x1a, 0x84, 0x2f, 0x59, 0x57, 0x6b, 0xbb, 0x51, 0x28, 0xbd, 0x91, 0x60, 0xd9, 0x8f, 0x54, 0x6a, 0xa0, 0x6b, 0xb2, 0xf6, 0x78, 0x79, 0xd2, 0x3a, 0x8f, 0xa6, 0x24, 0x7e, 0xe9, 0x6e, 0x66, 0x30, 0xed, 0xbf, 0x55, 0x71, 0x9c, 0x89, 0x81, 0xf0, 0xa7, 0xe7, 0x05, 0x87, 0x51, 0xc1, 0xff, 0xe5, 0xcf, 0x1f, 0x19, 0xe4, 0xeb, 0x7c, 0x1c, 0x1a, 0x58, 0xd5, 0x22, 0x3d, 0x31, 0x22, 0xc7, 0x8b, 0x60, 0xf5, 0xe8, 0x95, 0x73, 0xe0, 0x20, 0xe2, 0x4f, 0x03, 0x9e, 0x89, 0x34, 0x91, 0x5e, 0xda, 0x4f, 0x60, 0xff, 0xc9, 0x4f, 0x5a, 0x37, 0x1e, 0xb0, 0xed, 0x26, 0x4c, 0xa4, 0xc6, 0x26, 0xc9, 0xcc, 0xab, 0xd2, 0x1a, 0x3a, 0x82, 0x68, 0x03, 0x49, 0x8f, 0xb0, 0xb9, 0xc8, 0x48, 0x9d, 0xc7, 0xdf, 0x8b, 0x1c, 0xbf, 0xda, 0x89, 0x78, 0x6f, 0xd3, 0x62, 0xad, 0x35, 0xb9, 0xd3, 0x9b, 0xd0, 0x25, 0x65 }; static int dh_load_pkcs3_namedgroup_privlen_test(void) { int ret, privlen = 0; EVP_PKEY *pkey = NULL; const unsigned char *p = dh_pub_der; ret = TEST_ptr(pkey = d2i_PUBKEY_ex(NULL, &p, sizeof(dh_pub_der), NULL, NULL)) && TEST_true(EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_DH_PRIV_LEN, &privlen)) && TEST_int_eq(privlen, 1024); EVP_PKEY_free(pkey); return ret; } #endif int setup_tests(void) { #ifdef OPENSSL_NO_DH TEST_note("No DH support"); #else ADD_TEST(dh_test); ADD_TEST(dh_computekey_range_test); ADD_TEST(rfc5114_test); ADD_TEST(rfc7919_test); ADD_ALL_TESTS(dh_test_prime_groups, OSSL_NELEM(prime_groups)); ADD_TEST(dh_get_nid); ADD_TEST(dh_load_pkcs3_namedgroup_privlen_test); ADD_TEST(dh_rfc5114_fix_nid_test); ADD_TEST(dh_set_dh_nid_test); #endif return 1; }
test
openssl/test/dhtest.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include "testutil.h" #ifndef OPENSSL_NO_SM3 # include "internal/sm3.h" static int test_sm3(void) { static const unsigned char input1[] = { 0x61, 0x62, 0x63 }; static const unsigned char expected1[SM3_DIGEST_LENGTH] = { 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2, 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0 }; static const unsigned char input2[] = { 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64 }; static const unsigned char expected2[SM3_DIGEST_LENGTH] = { 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d, 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65, 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32 }; SM3_CTX ctx1, ctx2; unsigned char md1[SM3_DIGEST_LENGTH], md2[SM3_DIGEST_LENGTH]; if (!TEST_true(ossl_sm3_init(&ctx1)) || !TEST_true(ossl_sm3_update(&ctx1, input1, sizeof(input1))) || !TEST_true(ossl_sm3_final(md1, &ctx1)) || !TEST_mem_eq(md1, SM3_DIGEST_LENGTH, expected1, SM3_DIGEST_LENGTH)) return 0; if (!TEST_true(ossl_sm3_init(&ctx2)) || !TEST_true(ossl_sm3_update(&ctx2, input2, sizeof(input2))) || !TEST_true(ossl_sm3_final(md2, &ctx2)) || !TEST_mem_eq(md2, SM3_DIGEST_LENGTH, expected2, SM3_DIGEST_LENGTH)) return 0; return 1; } #endif int setup_tests(void) { #ifndef OPENSSL_NO_SM3 ADD_TEST(test_sm3); #endif return 1; }
test
openssl/test/sm3_internal_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/conf.h> #include <openssl/err.h> #include <openssl/ssl.h> #include <openssl/provider.h> #include "helpers/handshake.h" #include "helpers/ssl_test_ctx.h" #include "testutil.h" static CONF *conf = NULL; static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL; static OSSL_LIB_CTX *libctx = NULL; #define MAX_TESTCASE_NAME_LENGTH 100 static const char *print_alert(int alert) { return alert ? SSL_alert_desc_string_long(alert) : "no alert"; } static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->result, test_ctx->expected_result)) { TEST_info("ExpectedResult mismatch: expected %s, got %s.", ssl_test_result_name(test_ctx->expected_result), ssl_test_result_name(result->result)); return 0; } return 1; } static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->client_alert_sent, result->client_alert_received)) { TEST_info("Client sent alert %s but server received %s.", print_alert(result->client_alert_sent), print_alert(result->client_alert_received)); } if (!TEST_int_eq(result->server_alert_sent, result->server_alert_received)) { TEST_info("Server sent alert %s but client received %s.", print_alert(result->server_alert_sent), print_alert(result->server_alert_received)); } if (test_ctx->expected_client_alert && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) { TEST_error("ClientAlert mismatch: expected %s, got %s.", print_alert(test_ctx->expected_client_alert), print_alert(result->client_alert_sent)); return 0; } if (test_ctx->expected_server_alert && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) { TEST_error("ServerAlert mismatch: expected %s, got %s.", print_alert(test_ctx->expected_server_alert), print_alert(result->server_alert_sent)); return 0; } if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1)) return 0; if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1)) return 0; return 1; } static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->client_protocol, result->server_protocol)) { TEST_info("Client has protocol %s but server has %s.", ssl_protocol_name(result->client_protocol), ssl_protocol_name(result->server_protocol)); return 0; } if (test_ctx->expected_protocol) { if (!TEST_int_eq(result->client_protocol, test_ctx->expected_protocol)) { TEST_info("Protocol mismatch: expected %s, got %s.\n", ssl_protocol_name(test_ctx->expected_protocol), ssl_protocol_name(result->client_protocol)); return 0; } } return 1; } static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) { TEST_info("Client ServerName mismatch, expected %s, got %s.", ssl_servername_name(test_ctx->expected_servername), ssl_servername_name(result->servername)); return 0; } return 1; } static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE) return 1; if (!TEST_int_eq(result->session_ticket, test_ctx->session_ticket_expected)) { TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.", ssl_session_ticket_name(test_ctx->session_ticket_expected), ssl_session_ticket_name(result->session_ticket)); return 0; } return 1; } static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE) return 1; if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) { TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.", ssl_session_id_name(test_ctx->session_id_expected), ssl_session_id_name(result->session_id)); return 0; } return 1; } static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->compression, test_ctx->compression_expected)) return 0; return 1; } #ifndef OPENSSL_NO_NEXTPROTONEG static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { int ret = 1; if (!TEST_str_eq(result->client_npn_negotiated, result->server_npn_negotiated)) ret = 0; if (!TEST_str_eq(test_ctx->expected_npn_protocol, result->client_npn_negotiated)) ret = 0; return ret; } #endif static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { int ret = 1; if (!TEST_str_eq(result->client_alpn_negotiated, result->server_alpn_negotiated)) ret = 0; if (!TEST_str_eq(test_ctx->expected_alpn_protocol, result->client_alpn_negotiated)) ret = 0; return ret; } static int check_session_ticket_app_data(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { size_t result_len = 0; size_t expected_len = 0; if (result->result_session_ticket_app_data != NULL) result_len = strlen(result->result_session_ticket_app_data); if (test_ctx->expected_session_ticket_app_data != NULL) expected_len = strlen(test_ctx->expected_session_ticket_app_data); if (result_len == 0 && expected_len == 0) return 1; if (!TEST_str_eq(result->result_session_ticket_app_data, test_ctx->expected_session_ticket_app_data)) return 0; return 1; } static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (!TEST_int_eq(result->client_resumed, result->server_resumed)) return 0; if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected)) return 0; return 1; } static int check_nid(const char *name, int expected_nid, int nid) { if (expected_nid == 0 || expected_nid == nid) return 1; TEST_error("%s type mismatch, %s vs %s\n", name, OBJ_nid2ln(expected_nid), nid == NID_undef ? "absent" : OBJ_nid2ln(nid)); return 0; } static void print_ca_names(STACK_OF(X509_NAME) *names) { int i; if (names == NULL || sk_X509_NAME_num(names) == 0) { TEST_note(" <empty>"); return; } for (i = 0; i < sk_X509_NAME_num(names); i++) { X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4, XN_FLAG_ONELINE); BIO_puts(bio_err, "\n"); } } static int check_ca_names(const char *name, STACK_OF(X509_NAME) *expected_names, STACK_OF(X509_NAME) *names) { int i; if (expected_names == NULL) return 1; if (names == NULL || sk_X509_NAME_num(names) == 0) { if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0)) return 1; goto err; } if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names)) goto err; for (i = 0; i < sk_X509_NAME_num(names); i++) { if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i), sk_X509_NAME_value(expected_names, i)), 0)) { goto err; } } return 1; err: TEST_info("%s: list mismatch", name); TEST_note("Expected Names:"); print_ca_names(expected_names); TEST_note("Received Names:"); print_ca_names(names); return 0; } static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Tmp key", test_ctx->expected_tmp_key_type, result->tmp_key_type); } static int check_server_cert_type(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Server certificate", test_ctx->expected_server_cert_type, result->server_cert_type); } static int check_server_sign_hash(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Server signing hash", test_ctx->expected_server_sign_hash, result->server_sign_hash); } static int check_server_sign_type(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Server signing", test_ctx->expected_server_sign_type, result->server_sign_type); } static int check_server_ca_names(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_ca_names("Server CA names", test_ctx->expected_server_ca_names, result->server_ca_names); } static int check_client_cert_type(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Client certificate", test_ctx->expected_client_cert_type, result->client_cert_type); } static int check_client_sign_hash(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Client signing hash", test_ctx->expected_client_sign_hash, result->client_sign_hash); } static int check_client_sign_type(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_nid("Client signing", test_ctx->expected_client_sign_type, result->client_sign_type); } static int check_client_ca_names(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { return check_ca_names("Client CA names", test_ctx->expected_client_ca_names, result->client_ca_names); } static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { if (test_ctx->expected_cipher == NULL) return 1; if (!TEST_ptr(result->cipher)) return 0; if (!TEST_str_eq(test_ctx->expected_cipher, result->cipher)) return 0; return 1; } static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx) { int ret = 1; ret &= check_result(result, test_ctx); ret &= check_alerts(result, test_ctx); if (result->result == SSL_TEST_SUCCESS) { ret &= check_protocol(result, test_ctx); ret &= check_servername(result, test_ctx); ret &= check_session_ticket(result, test_ctx); ret &= check_compression(result, test_ctx); ret &= check_session_id(result, test_ctx); ret &= (result->session_ticket_do_not_call == 0); #ifndef OPENSSL_NO_NEXTPROTONEG ret &= check_npn(result, test_ctx); #endif ret &= check_cipher(result, test_ctx); ret &= check_alpn(result, test_ctx); ret &= check_session_ticket_app_data(result, test_ctx); ret &= check_resumption(result, test_ctx); ret &= check_tmp_key(result, test_ctx); ret &= check_server_cert_type(result, test_ctx); ret &= check_server_sign_hash(result, test_ctx); ret &= check_server_sign_type(result, test_ctx); ret &= check_server_ca_names(result, test_ctx); ret &= check_client_cert_type(result, test_ctx); ret &= check_client_sign_hash(result, test_ctx); ret &= check_client_sign_type(result, test_ctx); ret &= check_client_ca_names(result, test_ctx); } return ret; } static int test_handshake(int idx) { int ret = 0; SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL, *resume_server_ctx = NULL, *resume_client_ctx = NULL; SSL_TEST_CTX *test_ctx = NULL; HANDSHAKE_RESULT *result = NULL; char test_app[MAX_TESTCASE_NAME_LENGTH]; BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx); test_ctx = SSL_TEST_CTX_create(conf, test_app, libctx); if (!TEST_ptr(test_ctx)) goto err; if (test_ctx->fips_version != NULL && !fips_provider_version_match(libctx, test_ctx->fips_version)) { ret = TEST_skip("FIPS provider unable to run this test"); goto err; } #ifndef OPENSSL_NO_DTLS if (test_ctx->method == SSL_TEST_METHOD_DTLS) { server_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_server_method()); if (!TEST_true(SSL_CTX_set_options(server_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION)) || !TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0))) goto err; if (test_ctx->extra.server.servername_callback != SSL_TEST_SERVERNAME_CB_NONE) { if (!TEST_ptr(server2_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_server_method())) || !TEST_true(SSL_CTX_set_options(server2_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; } client_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_client_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0))) goto err; if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) { resume_server_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_server_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)) || !TEST_true(SSL_CTX_set_options(resume_server_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; resume_client_ctx = SSL_CTX_new_ex(libctx, NULL, DTLS_client_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0))) goto err; if (!TEST_ptr(resume_server_ctx) || !TEST_ptr(resume_client_ctx)) goto err; } } #endif if (test_ctx->method == SSL_TEST_METHOD_TLS) { #if !defined(OPENSSL_NO_TLS1_3) \ && defined(OPENSSL_NO_EC) \ && defined(OPENSSL_NO_DH) int maxversion = TLS1_2_VERSION; #else int maxversion = 0; #endif server_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, maxversion)) || !TEST_true(SSL_CTX_set_options(server_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; if (test_ctx->extra.server.servername_callback != SSL_TEST_SERVERNAME_CB_NONE) { if (!TEST_ptr(server2_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())) || !TEST_true(SSL_CTX_set_options(server2_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx, maxversion))) goto err; } client_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, maxversion))) goto err; if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) { resume_server_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, maxversion)) || !TEST_true(SSL_CTX_set_options(resume_server_ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION))) goto err; resume_client_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()); if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, maxversion))) goto err; if (!TEST_ptr(resume_server_ctx) || !TEST_ptr(resume_client_ctx)) goto err; } } #ifdef OPENSSL_NO_AUTOLOAD_CONFIG if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL))) goto err; #endif if (!TEST_ptr(server_ctx) || !TEST_ptr(client_ctx) || !TEST_int_gt(CONF_modules_load(conf, test_app, 0), 0)) goto err; if (!SSL_CTX_config(server_ctx, "server") || !SSL_CTX_config(client_ctx, "client")) { goto err; } if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2")) goto err; if (resume_server_ctx != NULL && !SSL_CTX_config(resume_server_ctx, "resume-server")) goto err; if (resume_client_ctx != NULL && !SSL_CTX_config(resume_client_ctx, "resume-client")) goto err; result = do_handshake(server_ctx, server2_ctx, client_ctx, resume_server_ctx, resume_client_ctx, test_ctx); if (result != NULL) ret = check_test(result, test_ctx); err: CONF_modules_unload(0); SSL_CTX_free(server_ctx); SSL_CTX_free(server2_ctx); SSL_CTX_free(client_ctx); SSL_CTX_free(resume_server_ctx); SSL_CTX_free(resume_client_ctx); SSL_TEST_CTX_free(test_ctx); HANDSHAKE_RESULT_free(result); return ret; } #define USAGE "conf_file module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { long num_tests; if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(conf = NCONF_new(NULL)) || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0) || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests), 0)) { TEST_error("usage: ssl_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE)) return 0; ADD_ALL_TESTS(test_handshake, (int)num_tests); return 1; } void cleanup_tests(void) { NCONF_free(conf); OSSL_PROVIDER_unload(defctxnull); OSSL_PROVIDER_unload(thisprov); OSSL_LIB_CTX_free(libctx); }
test
openssl/test/ssl_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/opensslconf.h> #include <openssl/err.h> #include <openssl/crypto.h> #include <openssl/bn.h> #include "internal/nelem.h" #include "testutil.h" #define TEST(expected, test) test_case((expected), #test, (test)) static int test_case(int expected, const char *test, int result) { if (result != expected) { fprintf(stderr, "# FATAL: %s != %d\n", test, expected); return 0; } return 1; } static int test_int(void) { if (!TEST(1, TEST_int_eq(1, 1)) || !TEST(0, TEST_int_eq(1, -1)) || !TEST(1, TEST_int_ne(1, 2)) || !TEST(0, TEST_int_ne(3, 3)) || !TEST(1, TEST_int_lt(4, 9)) || !TEST(0, TEST_int_lt(9, 4)) || !TEST(1, TEST_int_le(4, 9)) || !TEST(1, TEST_int_le(5, 5)) || !TEST(0, TEST_int_le(9, 4)) || !TEST(1, TEST_int_gt(8, 5)) || !TEST(0, TEST_int_gt(5, 8)) || !TEST(1, TEST_int_ge(8, 5)) || !TEST(1, TEST_int_ge(6, 6)) || !TEST(0, TEST_int_ge(5, 8))) goto err; return 1; err: return 0; } static int test_uint(void) { if (!TEST(1, TEST_uint_eq(3u, 3u)) || !TEST(0, TEST_uint_eq(3u, 5u)) || !TEST(1, TEST_uint_ne(4u, 2u)) || !TEST(0, TEST_uint_ne(6u, 6u)) || !TEST(1, TEST_uint_lt(5u, 9u)) || !TEST(0, TEST_uint_lt(9u, 5u)) || !TEST(1, TEST_uint_le(5u, 9u)) || !TEST(1, TEST_uint_le(7u, 7u)) || !TEST(0, TEST_uint_le(9u, 5u)) || !TEST(1, TEST_uint_gt(11u, 1u)) || !TEST(0, TEST_uint_gt(1u, 11u)) || !TEST(1, TEST_uint_ge(11u, 1u)) || !TEST(1, TEST_uint_ge(6u, 6u)) || !TEST(0, TEST_uint_ge(1u, 11u))) goto err; return 1; err: return 0; } static int test_char(void) { if (!TEST(1, TEST_char_eq('a', 'a')) || !TEST(0, TEST_char_eq('a', 'A')) || !TEST(1, TEST_char_ne('a', 'c')) || !TEST(0, TEST_char_ne('e', 'e')) || !TEST(1, TEST_char_lt('i', 'x')) || !TEST(0, TEST_char_lt('x', 'i')) || !TEST(1, TEST_char_le('i', 'x')) || !TEST(1, TEST_char_le('n', 'n')) || !TEST(0, TEST_char_le('x', 'i')) || !TEST(1, TEST_char_gt('w', 'n')) || !TEST(0, TEST_char_gt('n', 'w')) || !TEST(1, TEST_char_ge('w', 'n')) || !TEST(1, TEST_char_ge('p', 'p')) || !TEST(0, TEST_char_ge('n', 'w'))) goto err; return 1; err: return 0; } static int test_uchar(void) { if (!TEST(1, TEST_uchar_eq(49, 49)) || !TEST(0, TEST_uchar_eq(49, 60)) || !TEST(1, TEST_uchar_ne(50, 2)) || !TEST(0, TEST_uchar_ne(66, 66)) || !TEST(1, TEST_uchar_lt(60, 80)) || !TEST(0, TEST_uchar_lt(80, 60)) || !TEST(1, TEST_uchar_le(60, 80)) || !TEST(1, TEST_uchar_le(78, 78)) || !TEST(0, TEST_uchar_le(80, 60)) || !TEST(1, TEST_uchar_gt(88, 37)) || !TEST(0, TEST_uchar_gt(37, 88)) || !TEST(1, TEST_uchar_ge(88, 37)) || !TEST(1, TEST_uchar_ge(66, 66)) || !TEST(0, TEST_uchar_ge(37, 88))) goto err; return 1; err: return 0; } static int test_long(void) { if (!TEST(1, TEST_long_eq(123l, 123l)) || !TEST(0, TEST_long_eq(123l, -123l)) || !TEST(1, TEST_long_ne(123l, 500l)) || !TEST(0, TEST_long_ne(1000l, 1000l)) || !TEST(1, TEST_long_lt(-8923l, 102934563l)) || !TEST(0, TEST_long_lt(102934563l, -8923l)) || !TEST(1, TEST_long_le(-8923l, 102934563l)) || !TEST(1, TEST_long_le(12345l, 12345l)) || !TEST(0, TEST_long_le(102934563l, -8923l)) || !TEST(1, TEST_long_gt(84325677l, 12345l)) || !TEST(0, TEST_long_gt(12345l, 84325677l)) || !TEST(1, TEST_long_ge(84325677l, 12345l)) || !TEST(1, TEST_long_ge(465869l, 465869l)) || !TEST(0, TEST_long_ge(12345l, 84325677l))) goto err; return 1; err: return 0; } static int test_ulong(void) { if (!TEST(1, TEST_ulong_eq(919ul, 919ul)) || !TEST(0, TEST_ulong_eq(919ul, 10234ul)) || !TEST(1, TEST_ulong_ne(8190ul, 66ul)) || !TEST(0, TEST_ulong_ne(10555ul, 10555ul)) || !TEST(1, TEST_ulong_lt(10234ul, 1000000ul)) || !TEST(0, TEST_ulong_lt(1000000ul, 10234ul)) || !TEST(1, TEST_ulong_le(10234ul, 1000000ul)) || !TEST(1, TEST_ulong_le(100000ul, 100000ul)) || !TEST(0, TEST_ulong_le(1000000ul, 10234ul)) || !TEST(1, TEST_ulong_gt(100000000ul, 22ul)) || !TEST(0, TEST_ulong_gt(22ul, 100000000ul)) || !TEST(1, TEST_ulong_ge(100000000ul, 22ul)) || !TEST(1, TEST_ulong_ge(10555ul, 10555ul)) || !TEST(0, TEST_ulong_ge(22ul, 100000000ul))) goto err; return 1; err: return 0; } static int test_size_t(void) { if (!TEST(1, TEST_size_t_eq((size_t)10, (size_t)10)) || !TEST(0, TEST_size_t_eq((size_t)10, (size_t)12)) || !TEST(1, TEST_size_t_ne((size_t)10, (size_t)12)) || !TEST(0, TEST_size_t_ne((size_t)24, (size_t)24)) || !TEST(1, TEST_size_t_lt((size_t)30, (size_t)88)) || !TEST(0, TEST_size_t_lt((size_t)88, (size_t)30)) || !TEST(1, TEST_size_t_le((size_t)30, (size_t)88)) || !TEST(1, TEST_size_t_le((size_t)33, (size_t)33)) || !TEST(0, TEST_size_t_le((size_t)88, (size_t)30)) || !TEST(1, TEST_size_t_gt((size_t)52, (size_t)33)) || !TEST(0, TEST_size_t_gt((size_t)33, (size_t)52)) || !TEST(1, TEST_size_t_ge((size_t)52, (size_t)33)) || !TEST(1, TEST_size_t_ge((size_t)38, (size_t)38)) || !TEST(0, TEST_size_t_ge((size_t)33, (size_t)52))) goto err; return 1; err: return 0; } static int test_time_t(void) { if (!TEST(1, TEST_time_t_eq((time_t)10, (time_t)10)) || !TEST(0, TEST_time_t_eq((time_t)10, (time_t)12)) || !TEST(1, TEST_time_t_ne((time_t)10, (time_t)12)) || !TEST(0, TEST_time_t_ne((time_t)24, (time_t)24)) || !TEST(1, TEST_time_t_lt((time_t)30, (time_t)88)) || !TEST(0, TEST_time_t_lt((time_t)88, (time_t)30)) || !TEST(1, TEST_time_t_le((time_t)30, (time_t)88)) || !TEST(1, TEST_time_t_le((time_t)33, (time_t)33)) || !TEST(0, TEST_time_t_le((time_t)88, (time_t)30)) || !TEST(1, TEST_time_t_gt((time_t)52, (time_t)33)) || !TEST(0, TEST_time_t_gt((time_t)33, (time_t)52)) || !TEST(1, TEST_time_t_ge((time_t)52, (time_t)33)) || !TEST(1, TEST_time_t_ge((time_t)38, (time_t)38)) || !TEST(0, TEST_time_t_ge((time_t)33, (time_t)52))) goto err; return 1; err: return 0; } static int test_pointer(void) { int x = 0; char y = 1; if (!TEST(1, TEST_ptr(&y)) || !TEST(0, TEST_ptr(NULL)) || !TEST(0, TEST_ptr_null(&y)) || !TEST(1, TEST_ptr_null(NULL)) || !TEST(1, TEST_ptr_eq(NULL, NULL)) || !TEST(0, TEST_ptr_eq(NULL, &y)) || !TEST(0, TEST_ptr_eq(&y, NULL)) || !TEST(0, TEST_ptr_eq(&y, &x)) || !TEST(1, TEST_ptr_eq(&x, &x)) || !TEST(0, TEST_ptr_ne(NULL, NULL)) || !TEST(1, TEST_ptr_ne(NULL, &y)) || !TEST(1, TEST_ptr_ne(&y, NULL)) || !TEST(1, TEST_ptr_ne(&y, &x)) || !TEST(0, TEST_ptr_ne(&x, &x))) goto err; return 1; err: return 0; } static int test_bool(void) { if (!TEST(0, TEST_true(0)) || !TEST(1, TEST_true(1)) || !TEST(1, TEST_false(0)) || !TEST(0, TEST_false(1))) goto err; return 1; err: return 0; } static int test_string(void) { static char buf[] = "abc"; if (!TEST(1, TEST_str_eq(NULL, NULL)) || !TEST(1, TEST_str_eq("abc", buf)) || !TEST(0, TEST_str_eq("abc", NULL)) || !TEST(0, TEST_str_eq("abc", "")) || !TEST(0, TEST_str_eq(NULL, buf)) || !TEST(0, TEST_str_ne(NULL, NULL)) || !TEST(0, TEST_str_eq("", NULL)) || !TEST(0, TEST_str_eq(NULL, "")) || !TEST(0, TEST_str_ne("", "")) || !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5")) || !TEST(0, TEST_str_ne("abc", buf)) || !TEST(1, TEST_str_ne("abc", NULL)) || !TEST(1, TEST_str_ne(NULL, buf)) || !TEST(0, TEST_str_eq("abcdef", "abcdefghijk"))) goto err; return 1; err: return 0; } static int test_memory(void) { static char buf[] = "xyz"; if (!TEST(1, TEST_mem_eq(NULL, 0, NULL, 0)) || !TEST(1, TEST_mem_eq(NULL, 1, NULL, 2)) || !TEST(0, TEST_mem_eq(NULL, 0, "xyz", 3)) || !TEST(0, TEST_mem_eq(NULL, 7, "abc", 3)) || !TEST(0, TEST_mem_ne(NULL, 0, NULL, 0)) || !TEST(0, TEST_mem_eq(NULL, 0, "", 0)) || !TEST(0, TEST_mem_eq("", 0, NULL, 0)) || !TEST(0, TEST_mem_ne("", 0, "", 0)) || !TEST(0, TEST_mem_eq("xyz", 3, NULL, 0)) || !TEST(0, TEST_mem_eq("xyz", 3, buf, sizeof(buf))) || !TEST(1, TEST_mem_eq("xyz", 4, buf, sizeof(buf)))) goto err; return 1; err: return 0; } static int test_memory_overflow(void) { const char *p = "1234567890123456789012345678901234567890123456789012"; const char *q = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q))); } static int test_bignum(void) { BIGNUM *a = NULL, *b = NULL, *c = NULL; int r = 0; if (!TEST(1, TEST_int_eq(BN_dec2bn(&a, "0"), 1)) || !TEST(1, TEST_BN_eq_word(a, 0)) || !TEST(0, TEST_BN_eq_word(a, 30)) || !TEST(1, TEST_BN_abs_eq_word(a, 0)) || !TEST(0, TEST_BN_eq_one(a)) || !TEST(1, TEST_BN_eq_zero(a)) || !TEST(0, TEST_BN_ne_zero(a)) || !TEST(1, TEST_BN_le_zero(a)) || !TEST(0, TEST_BN_lt_zero(a)) || !TEST(1, TEST_BN_ge_zero(a)) || !TEST(0, TEST_BN_gt_zero(a)) || !TEST(1, TEST_BN_even(a)) || !TEST(0, TEST_BN_odd(a)) || !TEST(1, TEST_BN_eq(b, c)) || !TEST(0, TEST_BN_eq(a, b)) || !TEST(0, TEST_BN_ne(NULL, c)) || !TEST(1, TEST_int_eq(BN_dec2bn(&b, "1"), 1)) || !TEST(1, TEST_BN_eq_word(b, 1)) || !TEST(1, TEST_BN_eq_one(b)) || !TEST(0, TEST_BN_abs_eq_word(b, 0)) || !TEST(1, TEST_BN_abs_eq_word(b, 1)) || !TEST(0, TEST_BN_eq_zero(b)) || !TEST(1, TEST_BN_ne_zero(b)) || !TEST(0, TEST_BN_le_zero(b)) || !TEST(0, TEST_BN_lt_zero(b)) || !TEST(1, TEST_BN_ge_zero(b)) || !TEST(1, TEST_BN_gt_zero(b)) || !TEST(0, TEST_BN_even(b)) || !TEST(1, TEST_BN_odd(b)) || !TEST(1, TEST_int_eq(BN_dec2bn(&c, "-334739439"), 10)) || !TEST(0, TEST_BN_eq_word(c, 334739439)) || !TEST(1, TEST_BN_abs_eq_word(c, 334739439)) || !TEST(0, TEST_BN_eq_zero(c)) || !TEST(1, TEST_BN_ne_zero(c)) || !TEST(1, TEST_BN_le_zero(c)) || !TEST(1, TEST_BN_lt_zero(c)) || !TEST(0, TEST_BN_ge_zero(c)) || !TEST(0, TEST_BN_gt_zero(c)) || !TEST(0, TEST_BN_even(c)) || !TEST(1, TEST_BN_odd(c)) || !TEST(1, TEST_BN_eq(a, a)) || !TEST(0, TEST_BN_ne(a, a)) || !TEST(0, TEST_BN_eq(a, b)) || !TEST(1, TEST_BN_ne(a, b)) || !TEST(0, TEST_BN_lt(a, c)) || !TEST(1, TEST_BN_lt(c, b)) || !TEST(0, TEST_BN_lt(b, c)) || !TEST(0, TEST_BN_le(a, c)) || !TEST(1, TEST_BN_le(c, b)) || !TEST(0, TEST_BN_le(b, c)) || !TEST(1, TEST_BN_gt(a, c)) || !TEST(0, TEST_BN_gt(c, b)) || !TEST(1, TEST_BN_gt(b, c)) || !TEST(1, TEST_BN_ge(a, c)) || !TEST(0, TEST_BN_ge(c, b)) || !TEST(1, TEST_BN_ge(b, c))) goto err; r = 1; err: BN_free(a); BN_free(b); BN_free(c); return r; } static int test_long_output(void) { const char *p = "1234567890123456789012345678901234567890123456789012"; const char *q = "1234567890klmnopqrs01234567890EFGHIJKLM0123456789XYZ"; const char *r = "1234567890123456789012345678901234567890123456789012" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY+" "12345678901234567890123ABC78901234567890123456789012"; const char *s = "1234567890123456789012345678901234567890123456789012" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-" "1234567890123456789012345678901234567890123456789012" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; return TEST(0, TEST_str_eq(p, q)) & TEST(0, TEST_str_eq(q, r)) & TEST(0, TEST_str_eq(r, s)) & TEST(0, TEST_mem_eq(r, strlen(r), s, strlen(s))); } static int test_long_bignum(void) { int r; BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL; const char as[] = "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789012" "FFFFFF"; const char bs[] = "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789013" "987657"; const char cs[] = "-" "123456789012345678901234567890" "123456789012345678901234567890" "ABCD"; const char ds[] = "-" "23456789A123456789B123456789C" "123456789D123456789E123456789F" "ABCD"; r = TEST_true(BN_hex2bn(&a, as)) && TEST_true(BN_hex2bn(&b, bs)) && TEST_true(BN_hex2bn(&c, cs)) && TEST_true(BN_hex2bn(&d, ds)) && (TEST(0, TEST_BN_eq(a, b)) & TEST(0, TEST_BN_eq(b, a)) & TEST(0, TEST_BN_eq(b, NULL)) & TEST(0, TEST_BN_eq(NULL, a)) & TEST(1, TEST_BN_ne(a, NULL)) & TEST(0, TEST_BN_eq(c, d))); BN_free(a); BN_free(b); BN_free(c); BN_free(d); return r; } static int test_messages(void) { TEST_info("This is an %s message.", "info"); TEST_error("This is an %s message.", "error"); return 1; } static int test_single_eval(void) { int i = 4; long l = -9000; char c = 'd'; unsigned char uc = 22; unsigned long ul = 500; size_t st = 1234; char buf[4] = { 0 }, *p = buf; return TEST_int_eq(i++, 4) && TEST_int_eq(i, 5) && TEST_int_gt(++i, 5) && TEST_int_le(5, i++) && TEST_int_ne(--i, 5) && TEST_int_eq(12, i *= 2) && TEST_long_eq(l--, -9000L) && TEST_long_eq(++l, -9000L) && TEST_long_ne(-9000L, l /= 2) && TEST_long_lt(--l, -4500L) && TEST_char_eq(++c, 'e') && TEST_char_eq('e', c--) && TEST_char_ne('d', --c) && TEST_char_le('b', --c) && TEST_char_lt(c++, 'c') && TEST_uchar_eq(22, uc++) && TEST_uchar_eq(uc /= 2, 11) && TEST_ulong_eq(ul ^= 1, 501) && TEST_ulong_eq(502, ul ^= 3) && TEST_ulong_eq(ul = ul * 3 - 6, 1500) && TEST_size_t_eq((--i, st++), 1234) && TEST_size_t_eq(st, 1235) && TEST_int_eq(11, i) && TEST_ptr_eq(p++, buf) && TEST_ptr_eq(buf + 2, ++p) && TEST_ptr_eq(buf, p -= 2) && TEST_ptr(++p) && TEST_ptr_eq(p, buf + 1) && TEST_ptr_null(p = NULL) && TEST_str_eq(p = &("123456"[1]), "23456") && TEST_str_eq("3456", ++p) && TEST_str_ne(p++, "456") && TEST_mem_eq(--p, sizeof("3456"), "3456", sizeof("3456")) && TEST_mem_ne(p++, sizeof("456"), "456", sizeof("456")) && TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456")); } static int test_output(void) { const char s[] = "1234567890123456789012345678901234567890123456789012" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; test_output_string("test", s, sizeof(s) - 1); test_output_memory("test", (const unsigned char *)s, sizeof(s)); return 1; } static const char *bn_output_tests[] = { NULL, "0", "-12345678", "1234567890123456789012345678901234567890123456789012" "1234567890123456789012345678901234567890123456789013" "987657" }; static int test_bn_output(int n) { BIGNUM *b = NULL; if (bn_output_tests[n] != NULL && !TEST_true(BN_hex2bn(&b, bn_output_tests[n]))) return 0; test_output_bignum(bn_output_tests[n], b); BN_free(b); return 1; } static int test_skip_one(void) { return TEST_skip("skip test"); } static int test_skip_many(int n) { return TEST_skip("skip tests: %d", n); } static int test_skip_null(void) { return TEST_skip(NULL); } int setup_tests(void) { ADD_TEST(test_int); ADD_TEST(test_uint); ADD_TEST(test_char); ADD_TEST(test_uchar); ADD_TEST(test_long); ADD_TEST(test_ulong); ADD_TEST(test_size_t); ADD_TEST(test_time_t); ADD_TEST(test_pointer); ADD_TEST(test_bool); ADD_TEST(test_string); ADD_TEST(test_memory); ADD_TEST(test_memory_overflow); ADD_TEST(test_bignum); ADD_TEST(test_long_bignum); ADD_TEST(test_long_output); ADD_TEST(test_messages); ADD_TEST(test_single_eval); ADD_TEST(test_output); ADD_ALL_TESTS(test_bn_output, OSSL_NELEM(bn_output_tests)); ADD_TEST(test_skip_one); ADD_TEST(test_skip_null); ADD_ALL_TESTS(test_skip_many, 3); return 1; }
test
openssl/test/test_test.c
openssl
#include <stdio.h> #include <string.h> #include "testutil.h" #include "internal/json_enc.h" struct helper { OSSL_JSON_ENC j; int init; uint32_t flags; BIO *mem_bio; }; static int helper_ensure(struct helper *h) { if (h->init) return 1; if (!TEST_ptr(h->mem_bio = BIO_new(BIO_s_mem()))) return 0; if (!ossl_json_init(&h->j, h->mem_bio, h->flags)) { BIO_free_all(h->mem_bio); h->mem_bio = NULL; return 0; } h->init = 1; return 1; } static void helper_cleanup(struct helper *h) { BIO_free_all(h->mem_bio); h->mem_bio = NULL; if (h->init) { ossl_json_cleanup(&h->j); h->init = 0; } } static void helper_set_flags(struct helper *h, uint32_t flags) { helper_cleanup(h); h->flags = flags; } struct script_word { void *p; uint64_t u64; int64_t i64; double d; void (*fp)(void); }; #define OP_P(x) { (x) }, #define OP_U64(x) { NULL, (x) }, #define OP_I64(x) { NULL, 0, (x) }, #define OP_D(x) { NULL, 0, 0, (x) }, #define OP_FP(x) { NULL, 0, 0, 0, (void (*)(void))(x) }, struct script_info { const char *name, *title; const struct script_word *words; size_t num_words; const char *expected_output; size_t expected_output_len; }; typedef const struct script_info *(*info_func)(void); enum { OPK_END, OPK_CALL, OPK_CALL_P, OPK_CALL_I, OPK_CALL_U64, OPK_CALL_I64, OPK_CALL_D, OPK_CALL_PZ, OPK_ASSERT_ERROR, OPK_INIT_FLAGS }; typedef void (*fp_type)(OSSL_JSON_ENC *); typedef void (*fp_p_type)(OSSL_JSON_ENC *, const void *); typedef void (*fp_i_type)(OSSL_JSON_ENC *, int); typedef void (*fp_u64_type)(OSSL_JSON_ENC *, uint64_t); typedef void (*fp_i64_type)(OSSL_JSON_ENC *, int64_t); typedef void (*fp_d_type)(OSSL_JSON_ENC *, double); typedef void (*fp_pz_type)(OSSL_JSON_ENC *, const void *, size_t); #define OP_END() OP_U64(OPK_END) #define OP_CALL(f) OP_U64(OPK_CALL) OP_FP(f) #define OP_CALL_P(f, x) OP_U64(OPK_CALL_P) OP_FP(f) OP_P (x) #define OP_CALL_I(f, x) OP_U64(OPK_CALL_I) OP_FP(f) OP_I64(x) #define OP_CALL_U64(f, x) OP_U64(OPK_CALL_U64) OP_FP(f) OP_U64(x) #define OP_CALL_I64(f, x) OP_U64(OPK_CALL_I64) OP_FP(f) OP_I64(x) #define OP_CALL_D(f, x) OP_U64(OPK_CALL_D) OP_FP(f) OP_D (x) #define OP_CALL_PZ(f, x, xl) OP_U64(OPK_CALL_PZ) OP_FP(f) OP_P (x) OP_U64(xl) #define OP_ASSERT_ERROR(err) OP_U64(OPK_ASSERT_ERROR) OP_U64(err) #define OP_INIT_FLAGS(flags) OP_U64(OPK_INIT_FLAGS) OP_U64(flags) #define OPJ_BEGIN_O() OP_CALL(ossl_json_object_begin) #define OPJ_END_O() OP_CALL(ossl_json_object_end) #define OPJ_BEGIN_A() OP_CALL(ossl_json_array_begin) #define OPJ_END_A() OP_CALL(ossl_json_array_end) #define OPJ_NULL() OP_CALL(ossl_json_null) #define OPJ_BOOL(x) OP_CALL_I(ossl_json_bool, (x)) #define OPJ_U64(x) OP_CALL_U64(ossl_json_u64, (x)) #define OPJ_I64(x) OP_CALL_I64(ossl_json_i64, (x)) #define OPJ_F64(x) OP_CALL_D(ossl_json_f64, (x)) #define OPJ_KEY(x) OP_CALL_P(ossl_json_key, (x)) #define OPJ_STR(x) OP_CALL_P(ossl_json_str, (x)) #define OPJ_STR_LEN(x, xl) OP_CALL_PZ(ossl_json_str_len, (x), (xl)) #define OPJ_STR_HEX(x, xl) OP_CALL_PZ(ossl_json_str_hex, (x), (xl)) #define BEGIN_SCRIPT(name, title, flags) \ static const struct script_info *get_script_##name(void) \ { \ static const char script_name[] = #name; \ static const char script_title[] = #title; \ \ static const struct script_word script_words[] = { \ OP_INIT_FLAGS(flags) #define END_SCRIPT_EXPECTING(s, slen) \ OP_END() \ }; \ static const struct script_info script_info = { \ script_name, script_title, script_words, OSSL_NELEM(script_words), \ (s), (slen) \ }; \ return &script_info; \ } #define END_SCRIPT_EXPECTING_S(s) END_SCRIPT_EXPECTING(s, SIZE_MAX) #define END_SCRIPT_EXPECTING_Q(s) END_SCRIPT_EXPECTING(#s, sizeof(#s) - 1) #define SCRIPT(name) get_script_##name, BEGIN_SCRIPT(null, "serialize a single null", 0) OPJ_NULL() END_SCRIPT_EXPECTING_Q(null) BEGIN_SCRIPT(obj_empty, "serialize an empty object", 0) OPJ_BEGIN_O() OPJ_END_O() END_SCRIPT_EXPECTING_Q({}) BEGIN_SCRIPT(array_empty, "serialize an empty array", 0) OPJ_BEGIN_A() OPJ_END_A() END_SCRIPT_EXPECTING_Q([]) BEGIN_SCRIPT(bool_false, "serialize false", 0) OPJ_BOOL(0) END_SCRIPT_EXPECTING_Q(false) BEGIN_SCRIPT(bool_true, "serialize true", 0) OPJ_BOOL(1) END_SCRIPT_EXPECTING_Q(true) BEGIN_SCRIPT(u64_0, "serialize u64(0)", 0) OPJ_U64(0) END_SCRIPT_EXPECTING_Q(0) BEGIN_SCRIPT(u64_1, "serialize u64(1)", 0) OPJ_U64(1) END_SCRIPT_EXPECTING_Q(1) BEGIN_SCRIPT(u64_10, "serialize u64(10)", 0) OPJ_U64(10) END_SCRIPT_EXPECTING_Q(10) BEGIN_SCRIPT(u64_12345, "serialize u64(12345)", 0) OPJ_U64(12345) END_SCRIPT_EXPECTING_Q(12345) BEGIN_SCRIPT(u64_18446744073709551615, "serialize u64(18446744073709551615)", 0) OPJ_U64(18446744073709551615ULL) END_SCRIPT_EXPECTING_Q(18446744073709551615) BEGIN_SCRIPT(i64_0, "serialize i64(0)", 0) OPJ_I64(0) END_SCRIPT_EXPECTING_Q(0) BEGIN_SCRIPT(i64_1, "serialize i64(1)", 0) OPJ_I64(1) END_SCRIPT_EXPECTING_Q(1) BEGIN_SCRIPT(i64_2, "serialize i64(2)", 0) OPJ_I64(2) END_SCRIPT_EXPECTING_Q(2) BEGIN_SCRIPT(i64_10, "serialize i64(10)", 0) OPJ_I64(10) END_SCRIPT_EXPECTING_Q(10) BEGIN_SCRIPT(i64_12345, "serialize i64(12345)", 0) OPJ_I64(12345) END_SCRIPT_EXPECTING_Q(12345) BEGIN_SCRIPT(i64_9223372036854775807, "serialize i64(9223372036854775807)", 0) OPJ_I64(9223372036854775807LL) END_SCRIPT_EXPECTING_Q(9223372036854775807) BEGIN_SCRIPT(i64_m1, "serialize i64(-1)", 0) OPJ_I64(-1) END_SCRIPT_EXPECTING_Q(-1) BEGIN_SCRIPT(i64_m2, "serialize i64(-2)", 0) OPJ_I64(-2) END_SCRIPT_EXPECTING_Q(-2) BEGIN_SCRIPT(i64_m10, "serialize i64(-10)", 0) OPJ_I64(-10) END_SCRIPT_EXPECTING_Q(-10) BEGIN_SCRIPT(i64_m12345, "serialize i64(-12345)", 0) OPJ_I64(-12345) END_SCRIPT_EXPECTING_Q(-12345) BEGIN_SCRIPT(i64_m9223372036854775807, "serialize i64(-9223372036854775807)", 0) OPJ_I64(-9223372036854775807LL) END_SCRIPT_EXPECTING_Q(-9223372036854775807) BEGIN_SCRIPT(i64_m9223372036854775808, "serialize i64(-9223372036854775808)", 0) OPJ_I64(-9223372036854775807LL - 1LL) END_SCRIPT_EXPECTING_Q(-9223372036854775808) BEGIN_SCRIPT(str_empty, "serialize \"\"", 0) OPJ_STR("") END_SCRIPT_EXPECTING_Q("") BEGIN_SCRIPT(str_a, "serialize \"a\"", 0) OPJ_STR("a") END_SCRIPT_EXPECTING_Q("a") BEGIN_SCRIPT(str_abc, "serialize \"abc\"", 0) OPJ_STR("abc") END_SCRIPT_EXPECTING_Q("abc") BEGIN_SCRIPT(str_quote, "serialize with quote", 0) OPJ_STR("abc\"def") END_SCRIPT_EXPECTING_Q("abc\"def") BEGIN_SCRIPT(str_quote2, "serialize with quote", 0) OPJ_STR("abc\"\"def") END_SCRIPT_EXPECTING_Q("abc\"\"def") BEGIN_SCRIPT(str_escape, "serialize with various escapes", 0) OPJ_STR("abc\"\"de'f\r\n\t\b\f\\\x01\v\x7f\\") END_SCRIPT_EXPECTING_Q("abc\"\"de'f\r\n\t\b\f\\\u0001\u000b\u007f\\") BEGIN_SCRIPT(str_len, "length-signalled string", 0) OPJ_STR_LEN("abcdef", 6) END_SCRIPT_EXPECTING_Q("abcdef") BEGIN_SCRIPT(str_len0, "0-length-signalled string", 0) OPJ_STR_LEN("", 0) END_SCRIPT_EXPECTING_Q("") BEGIN_SCRIPT(str_len_nul, "string with NUL", 0) OPJ_STR_LEN("x\0y", 3) END_SCRIPT_EXPECTING_Q("x\u0000y") BEGIN_SCRIPT(hex_data0, "zero-length hex data", 0) OPJ_STR_HEX("", 0) END_SCRIPT_EXPECTING_Q("") BEGIN_SCRIPT(hex_data, "hex data", 0) OPJ_STR_HEX("\x00\x01\x5a\xfb\xff", 5) END_SCRIPT_EXPECTING_Q("00015afbff") BEGIN_SCRIPT(array_nest1, "serialize nested empty arrays", 0) OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_END_A() END_SCRIPT_EXPECTING_Q([[]]) BEGIN_SCRIPT(array_nest2, "serialize nested empty arrays", 0) OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() END_SCRIPT_EXPECTING_Q([[[]]]) BEGIN_SCRIPT(array_nest3, "serialize nested empty arrays", 0) OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_END_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_END_A() END_SCRIPT_EXPECTING_S("[[[],[],[]],[]]") BEGIN_SCRIPT(array_nest4, "deep nested arrays", 0) OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_BEGIN_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_END_A() OPJ_NULL() OPJ_END_A() END_SCRIPT_EXPECTING_S("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]],null]") BEGIN_SCRIPT(obj_nontrivial1, "serialize nontrivial object", 0) OPJ_BEGIN_O() OPJ_KEY("") OPJ_NULL() OPJ_END_O() END_SCRIPT_EXPECTING_S("{\"\":null}") BEGIN_SCRIPT(obj_nontrivial2, "serialize nontrivial object", 0) OPJ_BEGIN_O() OPJ_KEY("") OPJ_NULL() OPJ_KEY("x") OPJ_NULL() OPJ_END_O() END_SCRIPT_EXPECTING_S("{\"\":null,\"x\":null}") BEGIN_SCRIPT(obj_nest1, "serialize nested objects", 0) OPJ_BEGIN_O() OPJ_KEY("") OPJ_BEGIN_O() OPJ_KEY("x") OPJ_U64(42) OPJ_END_O() OPJ_KEY("x") OPJ_BEGIN_A() OPJ_U64(42) OPJ_U64(101) OPJ_END_A() OPJ_KEY("y") OPJ_NULL() OPJ_KEY("z") OPJ_BEGIN_O() OPJ_KEY("z0") OPJ_I64(-1) OPJ_KEY("z1") OPJ_I64(-2) OPJ_END_O() OPJ_END_O() END_SCRIPT_EXPECTING_S("{\"\":{\"x\":42},\"x\":[42,101],\"y\":null,\"z\":{\"z0\":-1,\"z1\":-2}}") BEGIN_SCRIPT(err_obj_no_key, "error test: object item without key", 0) OPJ_BEGIN_O() OP_ASSERT_ERROR(0) OPJ_NULL() OP_ASSERT_ERROR(1) OPJ_END_O() OP_ASSERT_ERROR(1) END_SCRIPT_EXPECTING_S("{") BEGIN_SCRIPT(err_obj_multi_key, "error test: object item with repeated key", 0) OPJ_BEGIN_O() OPJ_KEY("x") OP_ASSERT_ERROR(0) OPJ_KEY("y") OP_ASSERT_ERROR(1) OPJ_NULL() OP_ASSERT_ERROR(1) END_SCRIPT_EXPECTING_S("{\"x\":") BEGIN_SCRIPT(err_obj_no_value, "error test: object item with no value", 0) OPJ_BEGIN_O() OPJ_KEY("x") OP_ASSERT_ERROR(0) OPJ_END_O() OP_ASSERT_ERROR(1) END_SCRIPT_EXPECTING_S("{\"x\":") BEGIN_SCRIPT(err_utf8, "error test: only basic ASCII supported", 0) OPJ_STR("\x80") OP_ASSERT_ERROR(0) END_SCRIPT_EXPECTING_S("\"\\u0080\"") BEGIN_SCRIPT(utf8_2, "test: valid UTF-8 2byte supported", 0) OPJ_STR("low=\xc2\x80, high=\xdf\xbf") OP_ASSERT_ERROR(0) END_SCRIPT_EXPECTING_S("\"low=\xc2\x80, high=\xdf\xbf\"") BEGIN_SCRIPT(utf8_3, "test: valid UTF-8 3byte supported", 0) OPJ_STR("low=\xe0\xa0\x80, high=\xef\xbf\xbf") OP_ASSERT_ERROR(0) END_SCRIPT_EXPECTING_S("\"low=\xe0\xa0\x80, high=\xef\xbf\xbf\"") BEGIN_SCRIPT(utf8_4, "test: valid UTF-8 4byte supported", 0) OPJ_STR("low=\xf0\x90\xbf\xbf, high=\xf4\x8f\xbf\xbf") OP_ASSERT_ERROR(0) END_SCRIPT_EXPECTING_S("\"low=\xf0\x90\xbf\xbf, high=\xf4\x8f\xbf\xbf\"") BEGIN_SCRIPT(ijson_int, "I-JSON: large integer", OSSL_JSON_FLAG_IJSON) OPJ_BEGIN_A() OPJ_U64(1) OPJ_I64(-1) OPJ_U64(9007199254740991) OPJ_U64(9007199254740992) OPJ_I64(-9007199254740991) OPJ_I64(-9007199254740992) OPJ_END_A() END_SCRIPT_EXPECTING_S("[1,-1,9007199254740991,\"9007199254740992\",-9007199254740991,\"-9007199254740992\"]") BEGIN_SCRIPT(multi_item, "multiple top level items", 0) OPJ_NULL() OPJ_NULL() OPJ_BEGIN_A() OPJ_END_A() OPJ_BEGIN_A() OPJ_END_A() END_SCRIPT_EXPECTING_S("nullnull[][]") BEGIN_SCRIPT(seq, "JSON-SEQ", OSSL_JSON_FLAG_SEQ) OPJ_NULL() OPJ_NULL() OPJ_NULL() OPJ_BEGIN_O() OPJ_KEY("x") OPJ_U64(1) OPJ_KEY("y") OPJ_BEGIN_O() OPJ_END_O() OPJ_END_O() END_SCRIPT_EXPECTING_S("\x1Enull\n" "\x1Enull\n" "\x1Enull\n" "\x1E{\"x\":1,\"y\":{}}\n") static const info_func scripts[] = { SCRIPT(null) SCRIPT(obj_empty) SCRIPT(array_empty) SCRIPT(bool_false) SCRIPT(bool_true) SCRIPT(u64_0) SCRIPT(u64_1) SCRIPT(u64_10) SCRIPT(u64_12345) SCRIPT(u64_18446744073709551615) SCRIPT(i64_0) SCRIPT(i64_1) SCRIPT(i64_2) SCRIPT(i64_10) SCRIPT(i64_12345) SCRIPT(i64_9223372036854775807) SCRIPT(i64_m1) SCRIPT(i64_m2) SCRIPT(i64_m10) SCRIPT(i64_m12345) SCRIPT(i64_m9223372036854775807) SCRIPT(i64_m9223372036854775808) SCRIPT(str_empty) SCRIPT(str_a) SCRIPT(str_abc) SCRIPT(str_quote) SCRIPT(str_quote2) SCRIPT(str_escape) SCRIPT(str_len) SCRIPT(str_len0) SCRIPT(str_len_nul) SCRIPT(hex_data0) SCRIPT(hex_data) SCRIPT(array_nest1) SCRIPT(array_nest2) SCRIPT(array_nest3) SCRIPT(array_nest4) SCRIPT(obj_nontrivial1) SCRIPT(obj_nontrivial2) SCRIPT(obj_nest1) SCRIPT(err_obj_no_key) SCRIPT(err_obj_multi_key) SCRIPT(err_obj_no_value) SCRIPT(err_utf8) SCRIPT(utf8_2) SCRIPT(utf8_3) SCRIPT(utf8_4) SCRIPT(ijson_int) SCRIPT(multi_item) SCRIPT(seq) }; static int run_script(const struct script_info *info) { int ok = 0, asserted = -1; const struct script_word *words = info->words; size_t wp = 0; struct script_word w; struct helper h = {0}; BUF_MEM *bufp = NULL; TEST_info("running script '%s' (%s)", info->name, info->title); #define GET_WORD() (w = words[wp++]) #define GET_U64() (GET_WORD().u64) #define GET_I64() (GET_WORD().i64) #define GET_FP() (GET_WORD().fp) #define GET_P() (GET_WORD().p) for (;;) switch (GET_U64()) { case OPK_END: goto stop; case OPK_INIT_FLAGS: helper_set_flags(&h, (uint32_t)GET_U64()); break; case OPK_CALL: { fp_type f = (fp_type)GET_FP(); if (!TEST_true(helper_ensure(&h))) goto err; f(&h.j); break; } case OPK_CALL_I: { fp_i_type f = (fp_i_type)GET_FP(); if (!TEST_true(helper_ensure(&h))) goto err; f(&h.j, (int)GET_I64()); break; } case OPK_CALL_U64: { fp_u64_type f = (fp_u64_type)GET_FP(); if (!TEST_true(helper_ensure(&h))) goto err; f(&h.j, GET_U64()); break; } case OPK_CALL_I64: { fp_i64_type f = (fp_i64_type)GET_FP(); if (!TEST_true(helper_ensure(&h))) goto err; f(&h.j, GET_I64()); break; } case OPK_CALL_P: { fp_p_type f = (fp_p_type)GET_FP(); if (!TEST_true(helper_ensure(&h))) goto err; f(&h.j, GET_P()); break; } case OPK_CALL_PZ: { fp_pz_type f = (fp_pz_type)GET_FP(); void *p; uint64_t u64; if (!TEST_true(helper_ensure(&h))) goto err; p = GET_P(); u64 = GET_U64(); f(&h.j, p, (size_t)u64); break; } case OPK_ASSERT_ERROR: { if (!TEST_true(helper_ensure(&h))) goto err; asserted = (int)GET_U64(); if (!TEST_int_eq(ossl_json_in_error(&h.j), asserted)) goto err; break; } #define OP_ASSERT_ERROR(err) OP_U64(OPK_ASSERT_ERROR) OP_U64(err) default: TEST_error("unknown opcode"); goto err; } stop: if (!TEST_true(helper_ensure(&h))) goto err; if (!TEST_true(ossl_json_flush(&h.j))) goto err; if (asserted < 0 && !TEST_false(ossl_json_in_error(&h.j))) goto err; if (!TEST_true(BIO_get_mem_ptr(h.mem_bio, &bufp))) goto err; if (!TEST_mem_eq(bufp->data, bufp->length, info->expected_output, info->expected_output_len == SIZE_MAX ? strlen(info->expected_output) : info->expected_output_len)) goto err; ok = 1; err: if (!ok) TEST_error("script '%s' failed", info->name); helper_cleanup(&h); return ok; } static int test_json_enc(void) { int ok = 1; size_t i; for (i = 0; i < OSSL_NELEM(scripts); ++i) if (!TEST_true(run_script(scripts[i]()))) ok = 0; return ok; } int setup_tests(void) { ADD_TEST(test_json_enc); return 1; }
test
openssl/test/json_test.c
openssl
#include "helpers/cmp_testlib.h" static const char *ir_protected_f; static const char *genm_prot_Ed_f; static const char *ir_unprotected_f; static const char *ip_PBM_f; typedef struct test_fixture { const char *test_case_name; OSSL_CMP_CTX *cmp_ctx; OSSL_CMP_MSG *msg; OSSL_CMP_PKISI *si; EVP_PKEY *pubkey; unsigned char *mem; int memlen; X509 *cert; STACK_OF(X509) *certs; STACK_OF(X509) *chain; int with_ss; int callback_arg; int expected; } CMP_PROTECT_TEST_FIXTURE; static OSSL_LIB_CTX *libctx = NULL; static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture) { if (fixture != NULL) { OSSL_CMP_CTX_free(fixture->cmp_ctx); OSSL_CMP_MSG_free(fixture->msg); OSSL_CMP_PKISI_free(fixture->si); OPENSSL_free(fixture->mem); sk_X509_free(fixture->certs); sk_X509_free(fixture->chain); OPENSSL_free(fixture); } } static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name) { CMP_PROTECT_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))) { tear_down(fixture); return NULL; } return fixture; } static EVP_PKEY *prot_RSA_key = NULL; #ifndef OPENSSL_NO_ECX static EVP_PKEY *prot_Ed_key = NULL; static OSSL_CMP_MSG *genm_protected_Ed; #endif static EVP_PKEY *server_key = NULL; static X509 *server_cert = NULL; static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; static OSSL_CMP_MSG *ir_unprotected, *ir_protected; static X509 *endentity1 = NULL, *endentity2 = NULL, *root = NULL, *intermediate = NULL; static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture) { ASN1_BIT_STRING *protection = ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); int res = TEST_ptr_null(protection); ASN1_BIT_STRING_free(protection); return res; } static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture) { ASN1_BIT_STRING *protection = ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); int res = TEST_ptr(protection) && TEST_true(ASN1_STRING_cmp(protection, fixture->msg->protection) == 0); ASN1_BIT_STRING_free(protection); return res; } static int verify_signature(OSSL_CMP_MSG *msg, ASN1_BIT_STRING *protection, EVP_PKEY *pkey, EVP_MD *digest) { OSSL_CMP_PROTECTEDPART prot_part; prot_part.header = OSSL_CMP_MSG_get0_header(msg); prot_part.body = msg->body; return ASN1_item_verify_ex(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART), msg->header->protectionAlg, protection, &prot_part, NULL, pkey, libctx, NULL) > 0; } static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE * fixture) { ASN1_BIT_STRING *protection = ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); int ret = (TEST_ptr(protection) && TEST_true(verify_signature(fixture->msg, protection, fixture->pubkey, fixture->cmp_ctx->digest))); ASN1_BIT_STRING_free(protection); return ret; } static int test_cmp_calc_protection_no_key_no_secret(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f, libctx)) || !TEST_ptr(fixture->msg->header->protectionAlg = X509_ALGOR_new() )) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_calc_protection_fails_test, tear_down); return result; } static int test_cmp_calc_protection_pkey(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->pubkey = prot_RSA_key; if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, prot_RSA_key)) || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_calc_protection_signature_test, tear_down); return result; } #ifndef OPENSSL_NO_ECX static int test_cmp_calc_protection_pkey_Ed(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->pubkey = prot_Ed_key; if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, prot_Ed_key)) || !TEST_ptr(fixture->msg = load_pkimsg(genm_prot_Ed_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_calc_protection_signature_test, tear_down); return result; } #endif static int test_cmp_calc_protection_pbmac(void) { unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' }; SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_insta, sizeof(sec_insta))) || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f, libctx))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down); return result; } static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture) { return TEST_int_eq(fixture->expected, ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg)); } #define SET_OPT_UNPROTECTED_SEND(ctx, val) \ OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val)) static int test_MSG_protect_unprotected_request(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void) { const size_t size = sizeof(rand_data) / 2; SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)) || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, rand_data, size)) || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, rand_data + size, size))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_with_certificate_and_key(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)) || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, server_key)) || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, server_cert))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_certificate_based_without_cert(void) { OSSL_CMP_CTX *ctx; SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); ctx = fixture->cmp_ctx; fixture->expected = 0; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0)) || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, server_key))) { tear_down(fixture); fixture = NULL; } EVP_PKEY_up_ref(server_key); EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_no_key_no_secret(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 0; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_pbmac_no_sender(int with_ref) { static unsigned char secret[] = { 47, 11, 8, 15 }; static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe }; SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = with_ref; if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0) || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL) || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, secret, sizeof(secret)) || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, with_ref ? ref : NULL, sizeof(ref)))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_protect_test, tear_down); return result; } static int test_MSG_protect_pbmac_no_sender_with_ref(void) { return test_MSG_protect_pbmac_no_sender(1); } static int test_MSG_protect_pbmac_no_sender_no_ref(void) { return test_MSG_protect_pbmac_no_sender(0); } static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture) { return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx, fixture->msg)); } static int test_MSG_add_extraCerts(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down); return result; } #ifndef OPENSSL_NO_EC static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture) { int ret = 0; OSSL_CMP_CTX *ctx = fixture->cmp_ctx; X509_STORE *store; STACK_OF(X509) *chain = X509_build_chain(fixture->cert, fixture->certs, NULL, fixture->with_ss, ctx->libctx, ctx->propq); if (TEST_ptr(chain)) { ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain)); OSSL_STACK_OF_X509_free(chain); } if (!ret) return 0; if (TEST_ptr(store = X509_STORE_new()) && TEST_true(X509_STORE_add_cert(store, root))) { X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store), X509_V_FLAG_NO_CHECK_TIME); chain = X509_build_chain(fixture->cert, fixture->certs, store, fixture->with_ss, ctx->libctx, ctx->propq); ret = TEST_int_eq(fixture->expected, chain != NULL); if (ret && chain != NULL) { ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain)); OSSL_STACK_OF_X509_free(chain); } } X509_STORE_free(store); return ret; } static int test_cmp_build_cert_chain(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; fixture->with_ss = 0; fixture->cert = endentity2; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !TEST_ptr(fixture->chain = sk_X509_new_null()) || !TEST_true(sk_X509_push(fixture->certs, endentity1)) || !TEST_true(sk_X509_push(fixture->certs, root)) || !TEST_true(sk_X509_push(fixture->certs, intermediate)) || !TEST_true(sk_X509_push(fixture->chain, endentity2)) || !TEST_true(sk_X509_push(fixture->chain, intermediate))) { tear_down(fixture); fixture = NULL; } if (fixture != NULL) { result = execute_cmp_build_cert_chain_test(fixture); fixture->with_ss = 1; if (result && TEST_true(sk_X509_push(fixture->chain, root))) result = execute_cmp_build_cert_chain_test(fixture); } tear_down(fixture); return result; } static int test_cmp_build_cert_chain_missing_intermediate(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 0; fixture->with_ss = 0; fixture->cert = endentity2; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !TEST_ptr(fixture->chain = sk_X509_new_null()) || !TEST_true(sk_X509_push(fixture->certs, endentity1)) || !TEST_true(sk_X509_push(fixture->certs, root)) || !TEST_true(sk_X509_push(fixture->chain, endentity2))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); return result; } static int test_cmp_build_cert_chain_no_root(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; fixture->with_ss = 0; fixture->cert = endentity2; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !TEST_ptr(fixture->chain = sk_X509_new_null()) || !TEST_true(sk_X509_push(fixture->certs, endentity1)) || !TEST_true(sk_X509_push(fixture->certs, intermediate)) || !TEST_true(sk_X509_push(fixture->chain, endentity2)) || !TEST_true(sk_X509_push(fixture->chain, intermediate))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); return result; } static int test_cmp_build_cert_chain_only_root(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 1; fixture->with_ss = 0; fixture->cert = root; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !TEST_ptr(fixture->chain = sk_X509_new_null()) || !TEST_true(sk_X509_push(fixture->certs, root)) || !TEST_true(sk_X509_push(fixture->chain, root))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); return result; } static int test_cmp_build_cert_chain_no_certs(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->expected = 0; fixture->with_ss = 0; fixture->cert = endentity2; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !TEST_ptr(fixture->chain = sk_X509_new_null()) || !TEST_true(sk_X509_push(fixture->chain, endentity2))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); return result; } #endif static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture) { X509_STORE *store = X509_STORE_new(); STACK_OF(X509) *sk = NULL; int res = 0; if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store, fixture->certs, fixture->callback_arg))) goto err; sk = X509_STORE_get1_all_certs(store); if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain))) goto err; res = 1; err: X509_STORE_free(store); OSSL_STACK_OF_X509_free(sk); return res; } static int test_X509_STORE(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->callback_arg = 0; if (!TEST_ptr(fixture->certs = sk_X509_new_null()) || !sk_X509_push(fixture->certs, endentity1) || !sk_X509_push(fixture->certs, endentity2) || !sk_X509_push(fixture->certs, root) || !sk_X509_push(fixture->certs, intermediate) || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_X509_STORE_test, tear_down); return result; } static int test_X509_STORE_only_self_issued(void) { SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); fixture->certs = sk_X509_new_null(); fixture->chain = sk_X509_new_null(); fixture->callback_arg = 1; if (!TEST_true(sk_X509_push(fixture->certs, endentity1)) || !TEST_true(sk_X509_push(fixture->certs, endentity2)) || !TEST_true(sk_X509_push(fixture->certs, root)) || !TEST_true(sk_X509_push(fixture->certs, intermediate)) || !TEST_true(sk_X509_push(fixture->chain, root))) { tear_down(fixture); fixture = NULL; } EXECUTE_TEST(execute_X509_STORE_test, tear_down); return result; } void cleanup_tests(void) { EVP_PKEY_free(prot_RSA_key); #ifndef OPENSSL_NO_ECX EVP_PKEY_free(prot_Ed_key); OSSL_CMP_MSG_free(genm_protected_Ed); #endif EVP_PKEY_free(server_key); X509_free(server_cert); X509_free(endentity1); X509_free(endentity2); X509_free(root); X509_free(intermediate); OSSL_CMP_MSG_free(ir_protected); OSSL_CMP_MSG_free(ir_unprotected); OSSL_PROVIDER_unload(default_null_provider); OSSL_PROVIDER_unload(provider); OSSL_LIB_CTX_free(libctx); } #define USAGE "prot_RSA.pem IR_protected.der prot_Ed.pem " \ "GENM_protected_Ed.der IR_unprotected.der IP_PBM.der " \ "server.crt server.pem EndEntity1.crt EndEntity2.crt Root_CA.crt " \ "Intermediate_CA.crt module_name [module_conf_file]\n" OPT_TEST_DECLARE_USAGE(USAGE) int setup_tests(void) { char *prot_RSA_f; char *prot_Ed_f; char *server_key_f; char *server_cert_f; char *endentity1_f; char *endentity2_f; char *root_f; char *intermediate_f; 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(prot_RSA_f = test_get_argument(0)) || !TEST_ptr(ir_protected_f = test_get_argument(1)) || !TEST_ptr(prot_Ed_f = test_get_argument(2)) || !TEST_ptr(genm_prot_Ed_f = test_get_argument(3)) || !TEST_ptr(ir_unprotected_f = test_get_argument(4)) || !TEST_ptr(ip_PBM_f = test_get_argument(5)) || !TEST_ptr(server_cert_f = test_get_argument(6)) || !TEST_ptr(server_key_f = test_get_argument(7)) || !TEST_ptr(endentity1_f = test_get_argument(8)) || !TEST_ptr(endentity2_f = test_get_argument(9)) || !TEST_ptr(root_f = test_get_argument(10)) || !TEST_ptr(intermediate_f = test_get_argument(11))) { TEST_error("usage: cmp_protect_test %s", USAGE); return 0; } if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 12, USAGE)) return 0; if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx)) || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))) return 0; if (!TEST_ptr(prot_RSA_key = load_pkey_pem(prot_RSA_f, libctx))) return 0; #ifndef OPENSSL_NO_ECX if (!TEST_ptr(prot_Ed_key = load_pkey_pem(prot_Ed_f, libctx))) return 0; #endif if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f, libctx)) #ifndef OPENSSL_NO_ECX || !TEST_ptr(genm_protected_Ed = load_pkimsg(genm_prot_Ed_f, libctx)) #endif || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f, libctx))) 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, libctx)) || !TEST_ptr(intermediate = load_cert_pem(intermediate_f, libctx))) return 0; if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH))) return 0; ADD_TEST(test_cmp_calc_protection_no_key_no_secret); ADD_TEST(test_cmp_calc_protection_pkey); #ifndef OPENSSL_NO_ECX ADD_TEST(test_cmp_calc_protection_pkey_Ed); #endif ADD_TEST(test_cmp_calc_protection_pbmac); ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key); ADD_TEST(test_MSG_protect_with_certificate_and_key); ADD_TEST(test_MSG_protect_certificate_based_without_cert); ADD_TEST(test_MSG_protect_unprotected_request); ADD_TEST(test_MSG_protect_no_key_no_secret); ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref); ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref); ADD_TEST(test_MSG_add_extraCerts); #ifndef OPENSSL_NO_EC ADD_TEST(test_cmp_build_cert_chain); ADD_TEST(test_cmp_build_cert_chain_only_root); ADD_TEST(test_cmp_build_cert_chain_no_root); ADD_TEST(test_cmp_build_cert_chain_missing_intermediate); ADD_TEST(test_cmp_build_cert_chain_no_certs); #endif ADD_TEST(test_X509_STORE); ADD_TEST(test_X509_STORE_only_self_issued); return 1; }
test
openssl/test/cmp_protect_test.c
openssl
#include "testutil.h" #include "internal/time.h" static int test_time_to_timeval(void) { OSSL_TIME a; struct timeval tv; a = ossl_time_zero(); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 0)) return 0; if (!TEST_true(ossl_time_is_zero(ossl_time_from_timeval(tv)))) return 0; a = ossl_ticks2time(1); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1)) return 0; a = ossl_ticks2time(999); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1)) return 0; a = ossl_ticks2time(1000); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1)) return 0; a = ossl_ticks2time(1001); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 2)) return 0; a = ossl_ticks2time(999000); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 999)) return 0; a = ossl_ticks2time(999999001); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0)) return 0; a = ossl_ticks2time(999999999); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0)) return 0; a = ossl_ticks2time(1000000000); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0)) return 0; a = ossl_ticks2time(1000000001); tv = ossl_time_to_timeval(a); if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 1)) return 0; return 1; } int setup_tests(void) { ADD_TEST(test_time_to_timeval); return 1; }
test
openssl/test/time_test.c
openssl
#include <openssl/ssl.h> #include <openssl/quic.h> #include <openssl/bio.h> #include "internal/common.h" #include "internal/sockets.h" #include "internal/quic_tserver.h" #include "internal/quic_thread_assist.h" #include "internal/quic_ssl.h" #include "internal/time.h" #include "testutil.h" static const char msg1[] = "The quick brown fox jumped over the lazy dogs."; static char msg2[1024], msg3[1024]; static OSSL_TIME fake_time; static CRYPTO_RWLOCK *fake_time_lock; static const char *certfile, *keyfile; 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 unsigned char scratch_buf[2048]; static OSSL_TIME fake_now(void *arg) { OSSL_TIME t; if (!CRYPTO_THREAD_read_lock(fake_time_lock)) return ossl_time_zero(); t = fake_time; CRYPTO_THREAD_unlock(fake_time_lock); return t; } static OSSL_TIME real_now(void *arg) { return ossl_time_now(); } static int do_test(int use_thread_assist, int use_fake_time, int use_inject) { int testresult = 0, ret; int s_fd = -1, c_fd = -1; BIO *s_net_bio = NULL, *s_net_bio_own = NULL; BIO *c_net_bio = NULL, *c_net_bio_own = NULL; BIO *c_pair_own = NULL, *s_pair_own = NULL; QUIC_TSERVER_ARGS tserver_args = {0}; QUIC_TSERVER *tserver = NULL; BIO_ADDR *s_addr_ = NULL; struct in_addr ina = {0}; union BIO_sock_info_u s_info = {0}; SSL_CTX *c_ctx = NULL; SSL *c_ssl = NULL; int c_connected = 0, c_write_done = 0, c_begin_read = 0, s_read_done = 0; int c_wait_eos = 0, c_done_eos = 0; int c_start_idle_test = 0, c_done_idle_test = 0; size_t l = 0, s_total_read = 0, s_total_written = 0, c_total_read = 0; size_t idle_units_done = 0; int s_begin_write = 0; OSSL_TIME start_time; unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' }; size_t limit_ms = 10000; #if defined(OPENSSL_NO_QUIC_THREAD_ASSIST) if (use_thread_assist) { TEST_skip("thread assisted mode not enabled"); return 1; } #endif ina.s_addr = htonl(0x7f000001UL); s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); if (!TEST_int_ge(s_fd, 0)) goto err; if (!TEST_true(BIO_socket_nbio(s_fd, 1))) goto err; if (!TEST_ptr(s_addr_ = BIO_ADDR_new())) goto err; if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina), 0))) goto err; if (!TEST_true(BIO_bind(s_fd, s_addr_, 0))) goto err; s_info.addr = s_addr_; if (!TEST_true(BIO_sock_info(s_fd, BIO_SOCK_INFO_ADDRESS, &s_info))) goto err; if (!TEST_int_gt(BIO_ADDR_rawport(s_addr_), 0)) goto err; if (!TEST_ptr(s_net_bio = s_net_bio_own = BIO_new_dgram(s_fd, 0))) goto err; if (!BIO_up_ref(s_net_bio)) goto err; fake_time = ossl_ms2time(1000); tserver_args.net_rbio = s_net_bio; tserver_args.net_wbio = s_net_bio; tserver_args.alpn = NULL; tserver_args.ctx = NULL; if (use_fake_time) tserver_args.now_cb = fake_now; if (!TEST_ptr(tserver = ossl_quic_tserver_new(&tserver_args, certfile, keyfile))) { BIO_free(s_net_bio); goto err; } s_net_bio_own = NULL; if (use_inject) { if (!TEST_true(BIO_new_bio_dgram_pair(&c_pair_own, 5000, &s_pair_own, 5000))) goto err; } c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); if (!TEST_int_ge(c_fd, 0)) goto err; if (!TEST_true(BIO_socket_nbio(c_fd, 1))) goto err; if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0))) goto err; if (!BIO_dgram_set_peer(c_net_bio, s_addr_)) goto err; if (!TEST_ptr(c_ctx = SSL_CTX_new(use_thread_assist ? OSSL_QUIC_client_thread_method() : OSSL_QUIC_client_method()))) goto err; if (!TEST_ptr(c_ssl = SSL_new(c_ctx))) goto err; if (use_fake_time) if (!TEST_true(ossl_quic_conn_set_override_now_cb(c_ssl, fake_now, NULL))) goto err; if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn)))) goto err; if (use_inject) { SSL_set0_rbio(c_ssl, c_pair_own); c_pair_own = NULL; } else { SSL_set0_rbio(c_ssl, c_net_bio); if (!TEST_true(BIO_up_ref(c_net_bio))) { c_net_bio_own = NULL; goto err; } } SSL_set0_wbio(c_ssl, c_net_bio); c_net_bio_own = NULL; if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0))) goto err; start_time = real_now(NULL); for (;;) { if (ossl_time_compare(ossl_time_subtract(real_now(NULL), start_time), ossl_ms2time(limit_ms)) >= 0) { TEST_error("timeout while attempting QUIC server test"); goto err; } if (!c_start_idle_test) { ret = SSL_connect(c_ssl); if (!TEST_true(ret == 1 || is_want(c_ssl, ret))) goto err; if (ret == 1) c_connected = 1; } if (c_connected && !c_write_done) { if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1), (int)sizeof(msg1) - 1)) goto err; if (!TEST_true(SSL_stream_conclude(c_ssl, 0))) goto err; c_write_done = 1; } if (c_connected && c_write_done && !s_read_done) { if (!ossl_quic_tserver_read(tserver, 0, (unsigned char *)msg2 + s_total_read, sizeof(msg2) - s_total_read, &l)) { if (!TEST_true(ossl_quic_tserver_has_read_ended(tserver, 0))) goto err; if (!TEST_mem_eq(msg1, sizeof(msg1) - 1, msg2, s_total_read)) goto err; s_begin_write = 1; s_read_done = 1; } else { s_total_read += l; if (!TEST_size_t_le(s_total_read, sizeof(msg1) - 1)) goto err; } } if (s_begin_write && s_total_written < sizeof(msg1) - 1) { if (!TEST_true(ossl_quic_tserver_write(tserver, 0, (unsigned char *)msg2 + s_total_written, sizeof(msg1) - 1 - s_total_written, &l))) goto err; s_total_written += l; if (s_total_written == sizeof(msg1) - 1) { ossl_quic_tserver_conclude(tserver, 0); c_begin_read = 1; } } if (c_begin_read && c_total_read < sizeof(msg1) - 1) { ret = SSL_read_ex(c_ssl, msg3 + c_total_read, sizeof(msg1) - 1 - c_total_read, &l); if (!TEST_true(ret == 1 || is_want(c_ssl, ret))) goto err; c_total_read += l; if (c_total_read == sizeof(msg1) - 1) { if (!TEST_mem_eq(msg1, sizeof(msg1) - 1, msg3, c_total_read)) goto err; c_wait_eos = 1; } } if (c_wait_eos && !c_done_eos) { unsigned char c; ret = SSL_read_ex(c_ssl, &c, sizeof(c), &l); if (!TEST_false(ret)) goto err; if (SSL_get_error(c_ssl, ret) != SSL_ERROR_WANT_READ) { if (!TEST_int_eq(SSL_get_error(c_ssl, ret), SSL_ERROR_ZERO_RETURN)) goto err; c_done_eos = 1; if (use_thread_assist && use_fake_time) { if (!TEST_true(ossl_quic_tserver_is_connected(tserver))) goto err; c_start_idle_test = 1; limit_ms = 120000; } else { break; } } } if (c_start_idle_test && !c_done_idle_test) { if (idle_units_done < 600) { struct timeval tv; int isinf; if (!TEST_true(CRYPTO_THREAD_write_lock(fake_time_lock))) goto err; fake_time = ossl_time_add(fake_time, ossl_ms2time(100)); CRYPTO_THREAD_unlock(fake_time_lock); ++idle_units_done; ossl_quic_conn_force_assist_thread_wake(c_ssl); if (!TEST_true(SSL_get_event_timeout(c_ssl, &tv, &isinf))) goto err; if (!isinf && ossl_time_compare(ossl_time_zero(), ossl_time_from_timeval(tv)) >= 0) OSSL_sleep(100); } else { c_done_idle_test = 1; } } if (c_done_idle_test) { if (!TEST_true(ossl_quic_tserver_is_connected(tserver))) goto err; break; } if (!c_start_idle_test || c_done_idle_test) { SSL_handle_events(c_ssl); } ossl_quic_tserver_tick(tserver); if (use_inject) { BIO_MSG rmsg = {0}; size_t msgs_processed = 0; for (;;) { rmsg.data = scratch_buf; rmsg.data_len = sizeof(scratch_buf); if (!BIO_recvmmsg(c_net_bio, &rmsg, sizeof(rmsg), 1, 0, &msgs_processed) || msgs_processed == 0 || rmsg.data_len == 0) break; if (!TEST_true(SSL_inject_net_dgram(c_ssl, rmsg.data, rmsg.data_len, NULL, NULL))) goto err; } } } testresult = 1; err: SSL_free(c_ssl); SSL_CTX_free(c_ctx); ossl_quic_tserver_free(tserver); BIO_ADDR_free(s_addr_); BIO_free(s_net_bio_own); BIO_free(c_net_bio_own); BIO_free(c_pair_own); BIO_free(s_pair_own); if (s_fd >= 0) BIO_closesocket(s_fd); if (c_fd >= 0) BIO_closesocket(c_fd); return testresult; } static int test_tserver(int idx) { int thread_assisted, use_fake_time, use_inject; thread_assisted = idx % 2; idx /= 2; use_inject = idx % 2; idx /= 2; use_fake_time = idx % 2; if (use_fake_time && !thread_assisted) return 1; return do_test(thread_assisted, use_fake_time, use_inject); } 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; if ((fake_time_lock = CRYPTO_THREAD_lock_new()) == NULL) return 0; ADD_ALL_TESTS(test_tserver, 2 * 2 * 2); return 1; }
test
openssl/test/quic_tserver_test.c
openssl
#include <openssl/evp.h> #include "internal/namemap.h" #include "testutil.h" #define NAME1 "name1" #define NAME2 "name2" #define ALIAS1 "alias1" #define ALIAS1_UC "ALIAS1" static int test_namemap_empty(void) { OSSL_NAMEMAP *nm = NULL; int ok; ok = TEST_int_eq(ossl_namemap_empty(NULL), 1) && TEST_ptr(nm = ossl_namemap_new()) && TEST_int_eq(ossl_namemap_empty(nm), 1) && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0) && TEST_int_eq(ossl_namemap_empty(nm), 0); ossl_namemap_free(nm); return ok; } static int test_namemap(OSSL_NAMEMAP *nm) { int num1 = ossl_namemap_add_name(nm, 0, NAME1); int num2 = ossl_namemap_add_name(nm, 0, NAME2); int num3 = ossl_namemap_add_name(nm, num1, ALIAS1); int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC); int check1 = ossl_namemap_name2num(nm, NAME1); int check2 = ossl_namemap_name2num(nm, NAME2); int check3 = ossl_namemap_name2num(nm, ALIAS1); int check4 = ossl_namemap_name2num(nm, ALIAS1_UC); int false1 = ossl_namemap_name2num(nm, "cookie"); return TEST_int_ne(num1, 0) && TEST_int_ne(num2, 0) && TEST_int_eq(num1, num3) && TEST_int_eq(num3, num4) && TEST_int_eq(num1, check1) && TEST_int_eq(num2, check2) && TEST_int_eq(num3, check3) && TEST_int_eq(num4, check4) && TEST_int_eq(false1, 0); } static int test_namemap_independent(void) { OSSL_NAMEMAP *nm = ossl_namemap_new(); int ok = TEST_ptr(nm) && test_namemap(nm); ossl_namemap_free(nm); return ok; } static int test_namemap_stored(void) { OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); return TEST_ptr(nm) && test_namemap(nm); } static int test_digestbyname(void) { int id; OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); const EVP_MD *sha256, *foo; if (!TEST_ptr(nm)) return 0; id = ossl_namemap_add_name(nm, 0, "SHA256"); if (!TEST_int_ne(id, 0)) return 0; if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id)) return 0; sha256 = EVP_get_digestbyname("SHA256"); if (!TEST_ptr(sha256)) return 0; foo = EVP_get_digestbyname("foo"); if (!TEST_ptr_eq(sha256, foo)) return 0; return 1; } static int test_cipherbyname(void) { int id; OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL); const EVP_CIPHER *aes128, *bar; if (!TEST_ptr(nm)) return 0; id = ossl_namemap_add_name(nm, 0, "AES-128-CBC"); if (!TEST_int_ne(id, 0)) return 0; if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id)) return 0; aes128 = EVP_get_cipherbyname("AES-128-CBC"); if (!TEST_ptr(aes128)) return 0; bar = EVP_get_cipherbyname("bar"); if (!TEST_ptr_eq(aes128, bar)) return 0; return 1; } static int test_cipher_is_a(void) { EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL); int rv = 1; if (!TEST_ptr(fetched)) return 0; if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM")) || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM"))) rv = 0; if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM")) || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM"))) rv = 0; EVP_CIPHER_free(fetched); return rv; } static int test_digest_is_a(void) { EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL); int rv = 1; if (!TEST_ptr(fetched)) return 0; if (!TEST_true(EVP_MD_is_a(fetched, "SHA512")) || !TEST_false(EVP_MD_is_a(fetched, "SHA1"))) rv = 0; if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256")) || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256"))) rv = 0; EVP_MD_free(fetched); return rv; } int setup_tests(void) { ADD_TEST(test_namemap_empty); ADD_TEST(test_namemap_independent); ADD_TEST(test_namemap_stored); ADD_TEST(test_digestbyname); ADD_TEST(test_cipherbyname); ADD_TEST(test_digest_is_a); ADD_TEST(test_cipher_is_a); return 1; }
test
openssl/test/namemap_internal_test.c
openssl
#include <string.h> #include <openssl/opensslconf.h> #include <openssl/crypto.h> #include <openssl/ocsp.h> #include <openssl/x509.h> #include <openssl/asn1.h> #include <openssl/pem.h> #include "testutil.h" static const char *certstr; static const char *privkeystr; #ifndef OPENSSL_NO_OCSP static int get_cert_and_key(X509 **cert_out, EVP_PKEY **key_out) { BIO *certbio, *keybio; X509 *cert = NULL; EVP_PKEY *key = NULL; if (!TEST_ptr(certbio = BIO_new_file(certstr, "r"))) return 0; cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); BIO_free(certbio); if (!TEST_ptr(keybio = BIO_new_file(privkeystr, "r"))) goto end; key = PEM_read_bio_PrivateKey(keybio, NULL, NULL, NULL); BIO_free(keybio); if (!TEST_ptr(cert) || !TEST_ptr(key)) goto end; *cert_out = cert; *key_out = key; return 1; end: X509_free(cert); EVP_PKEY_free(key); return 0; } static int get_cert(X509 **cert_out) { BIO *certbio; X509 *cert = NULL; if (!TEST_ptr(certbio = BIO_new_file(certstr, "r"))) return 0; cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); BIO_free(certbio); if (!TEST_ptr(cert)) goto end; *cert_out = cert; return 1; end: X509_free(cert); return 0; } static OCSP_BASICRESP *make_dummy_resp(void) { const unsigned char namestr[] = "openssl.example.com"; unsigned char keybytes[128] = {7}; OCSP_BASICRESP *bs = OCSP_BASICRESP_new(); OCSP_BASICRESP *bs_out = NULL; OCSP_CERTID *cid = NULL; ASN1_TIME *thisupd = ASN1_TIME_set(NULL, time(NULL)); ASN1_TIME *nextupd = ASN1_TIME_set(NULL, time(NULL) + 200); X509_NAME *name = X509_NAME_new(); ASN1_BIT_STRING *key = ASN1_BIT_STRING_new(); ASN1_INTEGER *serial = ASN1_INTEGER_new(); if (!TEST_ptr(name) || !TEST_ptr(key) || !TEST_ptr(serial) || !TEST_true(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, namestr, -1, -1, 1)) || !TEST_true(ASN1_BIT_STRING_set(key, keybytes, sizeof(keybytes))) || !TEST_true(ASN1_INTEGER_set_uint64(serial, (uint64_t)1))) goto err; cid = OCSP_cert_id_new(EVP_sha256(), name, key, serial); if (!TEST_ptr(bs) || !TEST_ptr(thisupd) || !TEST_ptr(nextupd) || !TEST_ptr(cid) || !TEST_true(OCSP_basic_add1_status(bs, cid, V_OCSP_CERTSTATUS_UNKNOWN, 0, NULL, thisupd, nextupd))) goto err; bs_out = bs; bs = NULL; err: ASN1_TIME_free(thisupd); ASN1_TIME_free(nextupd); ASN1_BIT_STRING_free(key); ASN1_INTEGER_free(serial); OCSP_CERTID_free(cid); OCSP_BASICRESP_free(bs); X509_NAME_free(name); return bs_out; } static int test_resp_signer(void) { OCSP_BASICRESP *bs = NULL; X509 *signer = NULL, *tmp; EVP_PKEY *key = NULL; STACK_OF(X509) *extra_certs = NULL; int ret = 0; bs = make_dummy_resp(); extra_certs = sk_X509_new_null(); if (!TEST_ptr(bs) || !TEST_ptr(extra_certs) || !TEST_true(get_cert_and_key(&signer, &key)) || !TEST_true(sk_X509_push(extra_certs, signer)) || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(), NULL, OCSP_NOCERTS))) goto err; if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, extra_certs)) || !TEST_int_eq(X509_cmp(tmp, signer), 0)) goto err; OCSP_BASICRESP_free(bs); bs = make_dummy_resp(); tmp = NULL; if (!TEST_ptr(bs) || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(), NULL, 0))) goto err; if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, NULL)) || !TEST_int_eq(X509_cmp(tmp, signer), 0)) goto err; ret = 1; err: OCSP_BASICRESP_free(bs); sk_X509_free(extra_certs); X509_free(signer); EVP_PKEY_free(key); return ret; } static int test_access_description(int testcase) { ACCESS_DESCRIPTION *ad = ACCESS_DESCRIPTION_new(); int ret = 0; if (!TEST_ptr(ad)) goto err; switch (testcase) { case 0: break; case 1: if (!TEST_ptr(ad->location)) goto err; GENERAL_NAME_free(ad->location); ad->location = NULL; break; case 2: GENERAL_NAME_free(ad->location); ad->location = GENERAL_NAME_new(); if (!TEST_ptr(ad->location)) goto err; break; } ACCESS_DESCRIPTION_free(ad); ret = 1; err: return ret; } static int test_ocsp_url_svcloc_new(void) { static const char *urls[] = { "www.openssl.org", "www.openssl.net", NULL }; X509 *issuer = NULL; X509_EXTENSION *ext = NULL; int ret = 0; if (!TEST_true(get_cert(&issuer))) goto err; ext = OCSP_url_svcloc_new(X509_get_issuer_name(issuer), urls); if (!TEST_ptr(ext)) goto err; X509_EXTENSION_free(ext); ret = 1; err: X509_free(issuer); return ret; } #endif 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(certstr = test_get_argument(0)) || !TEST_ptr(privkeystr = test_get_argument(1))) return 0; #ifndef OPENSSL_NO_OCSP ADD_TEST(test_resp_signer); ADD_ALL_TESTS(test_access_description, 3); ADD_TEST(test_ocsp_url_svcloc_new); #endif return 1; }
test
openssl/test/ocspapitest.c
openssl
#include "internal/quic_lcidm.h" #include "testutil.h" static char ptrs[8]; 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_lcidm(void) { int testresult = 0; QUIC_LCIDM *lcidm; size_t lcid_len = 10; QUIC_CONN_ID lcid_1, lcid_dummy, lcid_init = {0}; OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1, ncid_frame_2, ncid_frame_3; void *opaque = NULL; uint64_t seq_num = UINT64_MAX; int did_retire = 0; if (!TEST_ptr(lcidm = ossl_quic_lcidm_new(NULL, lcid_len))) goto err; if (!TEST_size_t_eq(ossl_quic_lcidm_get_lcid_len(lcidm), lcid_len)) goto err; if (!TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1)) || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_2)) || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_1)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 0) || !TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_3)) || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_4)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 1) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 1) || !TEST_true(ossl_quic_lcidm_retire_odcid(lcidm, ptrs + 0)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0) || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1)) || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_5)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0) || !TEST_true(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_1)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 1) || !TEST_false(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_init)) || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_1)) || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_2)) || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_3)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 4) || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &lcid_1, &seq_num, &opaque)) || !TEST_ptr_eq(opaque, ptrs + 2) || !TEST_uint64_t_eq(seq_num, 0) || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id, &seq_num, &opaque)) || !TEST_ptr_eq(opaque, ptrs + 2) || !TEST_uint64_t_eq(seq_num, 1) || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id, &seq_num, &opaque)) || !TEST_ptr_eq(opaque, ptrs + 2) || !TEST_uint64_t_eq(seq_num, 2) || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_3.conn_id, &seq_num, &opaque)) || !TEST_ptr_eq(opaque, ptrs + 2) || !TEST_uint64_t_eq(seq_num, 3) || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL, &lcid_dummy, &seq_num, &did_retire)) || !TEST_true(did_retire) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 3) || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL, &lcid_dummy, &seq_num, &did_retire)) || !TEST_true(did_retire) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2) || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL, &lcid_dummy, &seq_num, &did_retire)) || !TEST_false(did_retire) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2) || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &lcid_init, &seq_num, &opaque)) || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id, &seq_num, &opaque)) || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id, &seq_num, &opaque)) || !TEST_true(ossl_quic_lcidm_cull(lcidm, ptrs + 2)) || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 0)) goto err; testresult = 1; err: ossl_quic_lcidm_free(lcidm); return testresult; } int setup_tests(void) { ADD_TEST(test_lcidm); return 1; }
test
openssl/test/quic_lcidm_test.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/x509.h> #include <openssl/x509v3.h> #include <openssl/pem.h> #include <openssl/err.h> #include "internal/nelem.h" #include "testutil.h" static const char *infile; static int test_pathlen(void) { X509 *x = NULL; BIO *b = NULL; long pathlen; int ret = 0; if (!TEST_ptr(b = BIO_new_file(infile, "r")) || !TEST_ptr(x = PEM_read_bio_X509(b, NULL, NULL, NULL)) || !TEST_int_eq(pathlen = X509_get_pathlen(x), 6)) goto end; ret = 1; end: BIO_free(b); X509_free(x); return ret; } #ifndef OPENSSL_NO_RFC3779 static int test_asid(void) { ASN1_INTEGER *val1 = NULL, *val2 = NULL; ASIdentifiers *asid1 = ASIdentifiers_new(), *asid2 = ASIdentifiers_new(), *asid3 = ASIdentifiers_new(), *asid4 = ASIdentifiers_new(); int testresult = 0; if (!TEST_ptr(asid1) || !TEST_ptr(asid2) || !TEST_ptr(asid3)) goto err; if (!TEST_ptr(val1 = ASN1_INTEGER_new()) || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496))) goto err; if (!TEST_true(X509v3_asid_add_id_or_range(asid1, V3_ASID_ASNUM, val1, NULL))) goto err; val1 = NULL; if (!TEST_ptr(val2 = ASN1_INTEGER_new()) || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497))) goto err; if (!TEST_true(X509v3_asid_add_id_or_range(asid2, V3_ASID_ASNUM, val2, NULL))) goto err; val2 = NULL; if (!TEST_ptr(val1 = ASN1_INTEGER_new()) || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496)) || !TEST_ptr(val2 = ASN1_INTEGER_new()) || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497))) goto err; if (!TEST_true(X509v3_asid_add_id_or_range(asid3, V3_ASID_ASNUM, val1, val2))) goto err; val1 = val2 = NULL; if (!TEST_true(X509v3_asid_subset(NULL, NULL)) || !TEST_true(X509v3_asid_subset(NULL, asid1)) || !TEST_true(X509v3_asid_subset(asid1, asid1)) || !TEST_true(X509v3_asid_subset(asid2, asid2)) || !TEST_true(X509v3_asid_subset(asid1, asid3)) || !TEST_true(X509v3_asid_subset(asid2, asid3)) || !TEST_true(X509v3_asid_subset(asid3, asid3)) || !TEST_true(X509v3_asid_subset(asid4, asid1)) || !TEST_true(X509v3_asid_subset(asid4, asid2)) || !TEST_true(X509v3_asid_subset(asid4, asid3))) goto err; if (!TEST_false(X509v3_asid_subset(asid1, NULL)) || !TEST_false(X509v3_asid_subset(asid1, asid2)) || !TEST_false(X509v3_asid_subset(asid2, asid1)) || !TEST_false(X509v3_asid_subset(asid3, asid1)) || !TEST_false(X509v3_asid_subset(asid3, asid2)) || !TEST_false(X509v3_asid_subset(asid1, asid4)) || !TEST_false(X509v3_asid_subset(asid2, asid4)) || !TEST_false(X509v3_asid_subset(asid3, asid4))) goto err; testresult = 1; err: ASN1_INTEGER_free(val1); ASN1_INTEGER_free(val2); ASIdentifiers_free(asid1); ASIdentifiers_free(asid2); ASIdentifiers_free(asid3); ASIdentifiers_free(asid4); return testresult; } static struct ip_ranges_st { const unsigned int afi; const char *ip1; const char *ip2; int rorp; } ranges[] = { { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.1", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.2", IPAddressOrRange_addressRange}, { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.3", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.254", IPAddressOrRange_addressRange}, { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.255", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.255", IPAddressOrRange_addressRange}, { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.1", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV4, "192.168.0.0", "192.168.255.255", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV4, "192.168.1.0", "192.168.255.255", IPAddressOrRange_addressRange}, { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::1", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::2", IPAddressOrRange_addressRange}, { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::3", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::fffe", IPAddressOrRange_addressRange}, { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::ffff", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::ffff", IPAddressOrRange_addressRange}, { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::1", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV6, "2001:0db8::0:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressPrefix}, { IANA_AFI_IPV6, "2001:0db8::1:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressRange} }; static int check_addr(IPAddrBlocks *addr, int type) { IPAddressFamily *fam; IPAddressOrRange *aorr; if (!TEST_int_eq(sk_IPAddressFamily_num(addr), 1)) return 0; fam = sk_IPAddressFamily_value(addr, 0); if (!TEST_ptr(fam)) return 0; if (!TEST_int_eq(fam->ipAddressChoice->type, IPAddressChoice_addressesOrRanges)) return 0; if (!TEST_int_eq(sk_IPAddressOrRange_num(fam->ipAddressChoice->u.addressesOrRanges), 1)) return 0; aorr = sk_IPAddressOrRange_value(fam->ipAddressChoice->u.addressesOrRanges, 0); if (!TEST_ptr(aorr)) return 0; if (!TEST_int_eq(aorr->type, type)) return 0; return 1; } static int test_addr_ranges(void) { IPAddrBlocks *addr = NULL; ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL; size_t i; int testresult = 0; for (i = 0; i < OSSL_NELEM(ranges); i++) { addr = sk_IPAddressFamily_new_null(); if (!TEST_ptr(addr)) goto end; if (!TEST_true(X509v3_addr_canonize(addr))) goto end; ip1 = a2i_IPADDRESS(ranges[i].ip1); if (!TEST_ptr(ip1)) goto end; if (!TEST_true(ip1->length == 4 || ip1->length == 16)) goto end; ip2 = a2i_IPADDRESS(ranges[i].ip2); if (!TEST_ptr(ip2)) goto end; if (!TEST_int_eq(ip2->length, ip1->length)) goto end; if (!TEST_true(memcmp(ip1->data, ip2->data, ip1->length) <= 0)) goto end; if (!TEST_true(X509v3_addr_add_range(addr, ranges[i].afi, NULL, ip1->data, ip2->data))) goto end; if (!TEST_true(X509v3_addr_is_canonical(addr))) goto end; if (!check_addr(addr, ranges[i].rorp)) goto end; sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free); addr = NULL; ASN1_OCTET_STRING_free(ip1); ASN1_OCTET_STRING_free(ip2); ip1 = ip2 = NULL; } testresult = 1; end: sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free); ASN1_OCTET_STRING_free(ip1); ASN1_OCTET_STRING_free(ip2); return testresult; } static int test_addr_fam_len(void) { int testresult = 0; IPAddrBlocks *addr = NULL; IPAddressFamily *f1 = NULL; ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL; unsigned char key[6]; unsigned int keylen; unsigned afi = IANA_AFI_IPV4; addr = sk_IPAddressFamily_new_null(); if (!TEST_ptr(addr)) goto end; ip1 = a2i_IPADDRESS(ranges[0].ip1); if (!TEST_ptr(ip1)) goto end; ip2 = a2i_IPADDRESS(ranges[0].ip2); if (!TEST_ptr(ip2)) goto end; if (!TEST_true(X509v3_addr_add_range(addr, ranges[0].afi, NULL, ip1->data, ip2->data))) goto end; if (!TEST_true(X509v3_addr_is_canonical(addr))) goto end; key[0] = (afi >> 8) & 0xFF; key[1] = afi & 0xFF; key[2] = 0xD; key[3] = 0xE; key[4] = 0xA; key[5] = 0xD; keylen = 6; if ((f1 = IPAddressFamily_new()) == NULL) goto end; if (f1->ipAddressChoice == NULL && (f1->ipAddressChoice = IPAddressChoice_new()) == NULL) goto end; if (f1->addressFamily == NULL && (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL) goto end; if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen)) goto end; if (!sk_IPAddressFamily_push(addr, f1)) goto end; if (!TEST_false(X509v3_addr_canonize(addr))) goto end; f1 = sk_IPAddressFamily_pop(addr); IPAddressFamily_free(f1); key[0] = (afi >> 8) & 0xFF; key[1] = afi & 0xFF; key[2] = 0x1; keylen = 3; if ((f1 = IPAddressFamily_new()) == NULL) goto end; if (f1->ipAddressChoice == NULL && (f1->ipAddressChoice = IPAddressChoice_new()) == NULL) goto end; if (f1->addressFamily == NULL && (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL) goto end; if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen)) goto end; f1->ipAddressChoice->type = IPAddressChoice_inherit; if (!sk_IPAddressFamily_push(addr, f1)) goto end; if (!TEST_true(X509v3_addr_canonize(addr))) goto end; testresult = 1; end: sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free); ASN1_OCTET_STRING_free(ip1); ASN1_OCTET_STRING_free(ip2); return testresult; } static struct extvalues_st { const char *value; int pass; } extvalues[] = { { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 }, { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 }, { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 }, { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 }, { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 }, { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 }, { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 }, { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 }, { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 }, { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 }, { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 }, { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 } }; static int test_ext_syntax(void) { size_t i; int testresult = 1; for (i = 0; i < OSSL_NELEM(extvalues); i++) { X509V3_CTX ctx; BIO *extbio = BIO_new_mem_buf(extvalues[i].value, strlen(extvalues[i].value)); CONF *conf; long eline; if (!TEST_ptr(extbio)) return 0 ; conf = NCONF_new_ex(NULL, NULL); if (!TEST_ptr(conf)) { BIO_free(extbio); return 0; } if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) { testresult = 0; } else { X509V3_set_ctx_test(&ctx); X509V3_set_nconf(&ctx, conf); if (extvalues[i].pass) { if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default", NULL))) { TEST_info("Value: %s", extvalues[i].value); testresult = 0; } } else { ERR_set_mark(); if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default", NULL))) { testresult = 0; TEST_info("Value: %s", extvalues[i].value); ERR_clear_last_mark(); } else { ERR_pop_to_mark(); } } } BIO_free(extbio); NCONF_free(conf); } return testresult; } static int test_addr_subset(void) { int i; int ret = 0; IPAddrBlocks *addrEmpty = NULL; IPAddrBlocks *addr[3] = { NULL, NULL }; ASN1_OCTET_STRING *ip1[3] = { NULL, NULL }; ASN1_OCTET_STRING *ip2[3] = { NULL, NULL }; int sz = OSSL_NELEM(addr); for (i = 0; i < sz; ++i) { if (!TEST_ptr(addr[i] = sk_IPAddressFamily_new_null()) || !TEST_ptr(ip1[i] = a2i_IPADDRESS(ranges[i].ip1)) || !TEST_ptr(ip2[i] = a2i_IPADDRESS(ranges[i].ip2)) || !TEST_true(X509v3_addr_add_range(addr[i], ranges[i].afi, NULL, ip1[i]->data, ip2[i]->data))) goto end; } ret = TEST_ptr(addrEmpty = sk_IPAddressFamily_new_null()) && TEST_true(X509v3_addr_subset(NULL, NULL)) && TEST_true(X509v3_addr_subset(NULL, addr[0])) && TEST_true(X509v3_addr_subset(addrEmpty, addr[0])) && TEST_true(X509v3_addr_subset(addr[0], addr[0])) && TEST_true(X509v3_addr_subset(addr[0], addr[1])) && TEST_true(X509v3_addr_subset(addr[0], addr[2])) && TEST_true(X509v3_addr_subset(addr[1], addr[2])) && TEST_false(X509v3_addr_subset(addr[0], NULL)) && TEST_false(X509v3_addr_subset(addr[1], addr[0])) && TEST_false(X509v3_addr_subset(addr[2], addr[1])) && TEST_false(X509v3_addr_subset(addr[0], addrEmpty)); end: sk_IPAddressFamily_pop_free(addrEmpty, IPAddressFamily_free); for (i = 0; i < sz; ++i) { sk_IPAddressFamily_pop_free(addr[i], IPAddressFamily_free); ASN1_OCTET_STRING_free(ip1[i]); ASN1_OCTET_STRING_free(ip2[i]); } return ret; } #endif OPT_TEST_DECLARE_USAGE("cert.pem\n") int setup_tests(void) { if (!test_skip_common_options()) { TEST_error("Error parsing test options\n"); return 0; } if (!TEST_ptr(infile = test_get_argument(0))) return 0; ADD_TEST(test_pathlen); #ifndef OPENSSL_NO_RFC3779 ADD_TEST(test_asid); ADD_TEST(test_addr_ranges); ADD_TEST(test_ext_syntax); ADD_TEST(test_addr_fam_len); ADD_TEST(test_addr_subset); #endif return 1; }
test
openssl/test/v3ext.c
openssl