| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <errno.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <stddef.h> |
| 21 |
#include <stdint.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include <openssl/crypto.h> |
| 27 |
#include <openssl/err.h> |
| 28 |
|
| 29 |
#include "api/s2n.h" |
| 30 |
#include "stuffer/s2n_stuffer.h" |
| 31 |
#include "tls/s2n_cipher_suites.h" |
| 32 |
#include "tls/s2n_config.h" |
| 33 |
#include "tls/s2n_connection.h" |
| 34 |
#include "tls/s2n_crypto.h" |
| 35 |
#include "tls/s2n_tls.h" |
| 36 |
#include "tls/s2n_tls_parameters.h" |
| 37 |
#include "utils/s2n_random.h" |
| 38 |
#include "utils/s2n_safety.h" |
| 39 |
|
| 40 |
#include "s2n_test.h" |
| 41 |
#include "testlib/s2n_testlib.h" |
| 42 |
|
| 43 |
struct host_verify_data { |
| 44 |
const char *name; |
| 45 |
uint8_t found_name; |
| 46 |
uint8_t callback_invoked; |
| 47 |
}; |
| 48 |
|
| 49 |
static uint8_t verify_host_accept_everything(const char *host_name, size_t host_name_len, void *data) |
| 50 |
{ |
| 51 |
struct host_verify_data *verify_data = (struct host_verify_data *) data; |
| 52 |
verify_data->callback_invoked = 1; |
| 53 |
return 1; |
| 54 |
} |
| 55 |
|
| 56 |
static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12, S2N_TLS13}; |
| 57 |
|
| 58 |
int s2n_fuzz_test(const uint8_t *buf, size_t len) |
| 59 |
{ |
| 60 |
|
| 61 |
S2N_FUZZ_ENSURE_MIN_LEN(len, 1); |
| 62 |
|
| 63 |
|
| 64 |
struct host_verify_data verify_data = { .callback_invoked = 0, .found_name = 0, .name = NULL }; |
| 65 |
struct s2n_x509_trust_store trust_store; |
| 66 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 67 |
POSIX_ENSURE_REF(server_conn); |
| 68 |
POSIX_GUARD(s2n_stuffer_write_bytes(&server_conn->handshake.io, buf, len)); |
| 69 |
|
| 70 |
|
| 71 |
s2n_x509_trust_store_init_empty(&trust_store); |
| 72 |
|
| 73 |
|
| 74 |
uint8_t randval = 0; |
| 75 |
POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval)); |
| 76 |
|
| 77 |
if(randval % 2) { |
| 78 |
POSIX_GUARD(s2n_x509_trust_store_from_ca_file(&trust_store, S2N_DEFAULT_TEST_CERT_CHAIN, NULL)); |
| 79 |
POSIX_GUARD(s2n_connection_set_verify_host_callback(server_conn, verify_host_accept_everything, &verify_data)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
s2n_x509_validator_wipe(&server_conn->x509_validator); |
| 84 |
POSIX_GUARD(s2n_x509_validator_init(&server_conn->x509_validator, &trust_store, 1)); |
| 85 |
server_conn->x509_validator.skip_cert_validation = (randval >> 1) % 2; |
| 86 |
server_conn->actual_protocol_version = TLS_VERSIONS[((randval >> 4) & 0x0f) % s2n_array_len(TLS_VERSIONS)]; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
s2n_client_cert_recv(server_conn); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
s2n_x509_trust_store_wipe(&trust_store); |
| 97 |
s2n_x509_validator_wipe(&server_conn->x509_validator); |
| 98 |
|
| 99 |
POSIX_GUARD(s2n_connection_free(server_conn)); |
| 100 |
|
| 101 |
return S2N_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, NULL) |