| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <stddef.h> |
| 24 |
#include <stdint.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <unistd.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_safety.h" |
| 38 |
#include "s2n_test.h" |
| 39 |
#include "testlib/s2n_testlib.h" |
| 40 |
|
| 41 |
static char *cert_chain, *private_key; |
| 42 |
struct s2n_cert_chain_and_key *default_cert; |
| 43 |
|
| 44 |
int s2n_fuzz_init(int *argc, char **argv[]) |
| 45 |
{ |
| 46 |
|
| 47 |
cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 48 |
POSIX_ENSURE_REF(cert_chain); |
| 49 |
private_key = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 50 |
POSIX_ENSURE_REF(private_key); |
| 51 |
default_cert = s2n_cert_chain_and_key_new(); |
| 52 |
POSIX_ENSURE_REF(default_cert); |
| 53 |
POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE)); |
| 54 |
POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE)); |
| 55 |
POSIX_GUARD(s2n_cert_chain_and_key_load_pem(default_cert, cert_chain, private_key)); |
| 56 |
|
| 57 |
return S2N_SUCCESS; |
| 58 |
} |
| 59 |
|
| 60 |
static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12}; |
| 61 |
|
| 62 |
int s2n_fuzz_test(const uint8_t *buf, size_t len) |
| 63 |
{ |
| 64 |
|
| 65 |
S2N_FUZZ_ENSURE_MIN_LEN(len, 1); |
| 66 |
|
| 67 |
|
| 68 |
struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT); |
| 69 |
POSIX_ENSURE_REF(client_conn); |
| 70 |
struct s2n_config *client_config = s2n_config_new(); |
| 71 |
POSIX_ENSURE_REF(client_config); |
| 72 |
POSIX_GUARD(s2n_config_add_cert_chain_and_key_to_store(client_config, default_cert)); |
| 73 |
POSIX_GUARD(s2n_connection_set_config(client_conn, client_config)); |
| 74 |
POSIX_GUARD(s2n_stuffer_write_bytes(&client_conn->handshake.io, buf, len)); |
| 75 |
|
| 76 |
|
| 77 |
uint8_t randval = 0; |
| 78 |
POSIX_GUARD(s2n_stuffer_read_uint8(&client_conn->handshake.io, &randval)); |
| 79 |
client_conn->actual_protocol_version = TLS_VERSIONS[randval % s2n_array_len(TLS_VERSIONS)]; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
s2n_cert_req_recv(client_conn); |
| 85 |
|
| 86 |
|
| 87 |
POSIX_GUARD(s2n_connection_free(client_conn)); |
| 88 |
POSIX_GUARD(s2n_config_free(client_config)); |
| 89 |
|
| 90 |
return S2N_SUCCESS; |
| 91 |
} |
| 92 |
|
| 93 |
static void s2n_fuzz_cleanup() |
| 94 |
{ |
| 95 |
free(cert_chain); |
| 96 |
free(private_key); |
| 97 |
s2n_cert_chain_and_key_free(default_cert); |
| 98 |
} |
| 99 |
|
| 100 |
S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup) |