| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
|
| 23 |
#include <openssl/crypto.h> |
| 24 |
#include <openssl/err.h> |
| 25 |
|
| 26 |
#include "tls/s2n_kem.h" |
| 27 |
#include "tls/s2n_client_key_exchange.h" |
| 28 |
#include "tls/s2n_kex.h" |
| 29 |
#include "tls/s2n_security_policies.h" |
| 30 |
|
| 31 |
#include "api/s2n.h" |
| 32 |
#include "stuffer/s2n_stuffer.h" |
| 33 |
#include "tls/s2n_cipher_preferences.h" |
| 34 |
#include "tls/s2n_cipher_suites.h" |
| 35 |
#include "tls/s2n_connection.h" |
| 36 |
#include "tls/s2n_tls.h" |
| 37 |
#include "utils/s2n_safety.h" |
| 38 |
#include "s2n_test.h" |
| 39 |
#include "testlib/s2n_testlib.h" |
| 40 |
|
| 41 |
static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12}; |
| 42 |
|
| 43 |
|
| 44 |
uint8_t *cert_chain_pem = NULL; |
| 45 |
uint8_t *private_key_pem = NULL; |
| 46 |
char *dhparams_pem = NULL; |
| 47 |
uint32_t cert_chain_len = 0; |
| 48 |
uint32_t private_key_len = 0; |
| 49 |
struct s2n_config *config; |
| 50 |
struct s2n_cert_chain_and_key *chain_and_key; |
| 51 |
struct s2n_cert_chain_and_key *cert; |
| 52 |
struct s2n_cipher_suite **test_suites; |
| 53 |
int num_suites; |
| 54 |
|
| 55 |
int s2n_fuzz_init(int *argc, char **argv[]) |
| 56 |
{ |
| 57 |
test_suites = cipher_preferences_test_all.suites; |
| 58 |
num_suites = cipher_preferences_test_all.count; |
| 59 |
|
| 60 |
|
| 61 |
cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 62 |
private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 63 |
dhparams_pem = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 64 |
|
| 65 |
POSIX_ENSURE_REF(cert_chain_pem); |
| 66 |
POSIX_ENSURE_REF(private_key_pem); |
| 67 |
POSIX_ENSURE_REF(dhparams_pem); |
| 68 |
|
| 69 |
s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, &cert_chain_len, S2N_MAX_TEST_PEM_SIZE); |
| 70 |
s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, &private_key_len, S2N_MAX_TEST_PEM_SIZE); |
| 71 |
s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE); |
| 72 |
|
| 73 |
config = s2n_config_new(); |
| 74 |
chain_and_key = s2n_cert_chain_and_key_new(); |
| 75 |
|
| 76 |
POSIX_ENSURE_REF(config); |
| 77 |
POSIX_ENSURE_REF(chain_and_key); |
| 78 |
|
| 79 |
s2n_cert_chain_and_key_load_pem_bytes(chain_and_key, cert_chain_pem, cert_chain_len, private_key_pem, private_key_len); |
| 80 |
s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key); |
| 81 |
s2n_config_add_dhparams(config, dhparams_pem); |
| 82 |
|
| 83 |
cert = s2n_config_get_single_default_cert(config); |
| 84 |
POSIX_ENSURE_REF(cert); |
| 85 |
|
| 86 |
return S2N_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
int s2n_fuzz_test(const uint8_t *buf, size_t len) |
| 90 |
{ |
| 91 |
|
| 92 |
S2N_FUZZ_ENSURE_MIN_LEN(len, 2); |
| 93 |
|
| 94 |
|
| 95 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 96 |
POSIX_ENSURE_REF(server_conn); |
| 97 |
POSIX_GUARD(s2n_stuffer_write_bytes(&server_conn->handshake.io, buf, len)); |
| 98 |
|
| 99 |
|
| 100 |
uint8_t randval = 0; |
| 101 |
POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval)); |
| 102 |
server_conn->server_protocol_version = TLS_VERSIONS[randval % s2n_array_len(TLS_VERSIONS)]; |
| 103 |
|
| 104 |
POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval)); |
| 105 |
server_conn->secure->cipher_suite = test_suites[randval % num_suites]; |
| 106 |
|
| 107 |
|
| 108 |
if (server_conn->secure->cipher_suite->key_exchange_alg == NULL) { |
| 109 |
POSIX_GUARD(s2n_connection_free(server_conn)); |
| 110 |
return S2N_SUCCESS; |
| 111 |
} |
| 112 |
|
| 113 |
server_conn->handshake_params.our_chain_and_key = cert; |
| 114 |
|
| 115 |
const struct s2n_ecc_preferences *ecc_preferences = NULL; |
| 116 |
POSIX_GUARD(s2n_connection_get_ecc_preferences(server_conn, &ecc_preferences)); |
| 117 |
POSIX_ENSURE_REF(ecc_preferences); |
| 118 |
|
| 119 |
if (server_conn->secure->cipher_suite->key_exchange_alg->client_key_recv == s2n_ecdhe_client_key_recv || server_conn->secure->cipher_suite->key_exchange_alg->client_key_recv == s2n_hybrid_client_key_recv) { |
| 120 |
server_conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_preferences->ecc_curves[0]; |
| 121 |
s2n_ecc_evp_generate_ephemeral_key(&server_conn->kex_params.server_ecc_evp_params); |
| 122 |
} |
| 123 |
|
| 124 |
if (server_conn->secure->cipher_suite->key_exchange_alg->client_key_recv == s2n_kem_client_key_recv || server_conn->secure->cipher_suite->key_exchange_alg->client_key_recv == s2n_hybrid_client_key_recv) { |
| 125 |
server_conn->kex_params.kem_params.kem = &s2n_mlkem_768; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
s2n_client_key_recv(server_conn); |
| 132 |
|
| 133 |
|
| 134 |
POSIX_GUARD(s2n_connection_free(server_conn)); |
| 135 |
|
| 136 |
return S2N_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
static void s2n_fuzz_cleanup() |
| 140 |
{ |
| 141 |
free(cert_chain_pem); |
| 142 |
free(private_key_pem); |
| 143 |
free(dhparams_pem); |
| 144 |
s2n_config_free(config); |
| 145 |
s2n_cert_chain_and_key_free(chain_and_key); |
| 146 |
} |
| 147 |
|
| 148 |
S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup) |