| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#include <openssl/crypto.h> |
| 22 |
#include <openssl/err.h> |
| 23 |
|
| 24 |
#include "api/s2n.h" |
| 25 |
#include "stuffer/s2n_stuffer.h" |
| 26 |
#include "tls/s2n_connection.h" |
| 27 |
#include "tls/s2n_tls.h" |
| 28 |
#include "utils/s2n_safety.h" |
| 29 |
#include "s2n_test.h" |
| 30 |
#include "tls/s2n_tls13.h" |
| 31 |
|
| 32 |
static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12, S2N_TLS13}; |
| 33 |
|
| 34 |
int s2n_fuzz_init(int *argc, char **argv[]) |
| 35 |
{ |
| 36 |
POSIX_GUARD(s2n_enable_tls13_in_test()); |
| 37 |
srand(time(0)); |
| 38 |
return S2N_SUCCESS; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
int client_hello_cb_ret(struct s2n_connection *conn, void *ctx) |
| 43 |
{ |
| 44 |
return *((int*)ctx); |
| 45 |
} |
| 46 |
|
| 47 |
int s2n_fuzz_test(const uint8_t *buf, size_t len) |
| 48 |
{ |
| 49 |
|
| 50 |
S2N_FUZZ_ENSURE_MIN_LEN(len, 2); |
| 51 |
|
| 52 |
|
| 53 |
struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER); |
| 54 |
POSIX_ENSURE_REF(server_conn); |
| 55 |
POSIX_GUARD(s2n_stuffer_write_bytes(&server_conn->handshake.io, buf, len)); |
| 56 |
|
| 57 |
|
| 58 |
uint8_t randval = 0; |
| 59 |
int ctxval = 0; |
| 60 |
POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval)); |
| 61 |
server_conn->actual_protocol_version = TLS_VERSIONS[(randval & 0x0F) % s2n_array_len(TLS_VERSIONS)]; |
| 62 |
server_conn->server_protocol_version = TLS_VERSIONS[(randval >> 4) % s2n_array_len(TLS_VERSIONS)]; |
| 63 |
|
| 64 |
|
| 65 |
POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval)); |
| 66 |
ctxval = (int)(randval % 3) - 1; |
| 67 |
POSIX_GUARD(s2n_config_set_client_hello_cb(server_conn->config, client_hello_cb_ret, &ctxval)); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
s2n_client_hello_recv(server_conn); |
| 73 |
|
| 74 |
|
| 75 |
POSIX_GUARD(s2n_connection_free(server_conn)); |
| 76 |
|
| 77 |
return S2N_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, NULL) |