| 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 "testlib/s2n_testlib.h" |
| 31 |
|
| 32 |
uint8_t *cert_chain = NULL; |
| 33 |
uint8_t *private_key = NULL; |
| 34 |
uint32_t cert_chain_len = 0; |
| 35 |
uint32_t private_key_len = 0; |
| 36 |
struct s2n_cert_chain_and_key *default_cert; |
| 37 |
struct s2n_config *conn_config; |
| 38 |
|
| 39 |
int s2n_fuzz_init(int *argc, char **argv[]) |
| 40 |
{ |
| 41 |
|
| 42 |
cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 43 |
POSIX_ENSURE_REF(cert_chain); |
| 44 |
private_key = malloc(S2N_MAX_TEST_PEM_SIZE); |
| 45 |
POSIX_ENSURE_REF(private_key); |
| 46 |
default_cert = s2n_cert_chain_and_key_new(); |
| 47 |
POSIX_ENSURE_REF(default_cert); |
| 48 |
conn_config = s2n_config_new(); |
| 49 |
POSIX_ENSURE_REF(conn_config); |
| 50 |
POSIX_GUARD(s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, &cert_chain_len, S2N_MAX_TEST_PEM_SIZE)); |
| 51 |
POSIX_GUARD(s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, &private_key_len, S2N_MAX_TEST_PEM_SIZE)); |
| 52 |
POSIX_GUARD(s2n_cert_chain_and_key_load_pem_bytes(default_cert, cert_chain, cert_chain_len, private_key, private_key_len)); |
| 53 |
POSIX_GUARD(s2n_config_add_cert_chain_and_key_to_store(conn_config, default_cert)); |
| 54 |
|
| 55 |
return S2N_SUCCESS; |
| 56 |
} |
| 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 s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 65 |
POSIX_ENSURE_REF(conn); |
| 66 |
POSIX_GUARD(s2n_connection_set_config(conn, conn_config)); |
| 67 |
POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, buf, len)); |
| 68 |
|
| 69 |
|
| 70 |
uint8_t randval = 0; |
| 71 |
POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &randval)); |
| 72 |
if (randval % 2) { |
| 73 |
conn->mode = S2N_CLIENT; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
s2n_tls13_cert_verify_recv(conn); |
| 80 |
|
| 81 |
|
| 82 |
POSIX_GUARD(s2n_connection_free(conn)); |
| 83 |
|
| 84 |
return S2N_SUCCESS; |
| 85 |
} |
| 86 |
|
| 87 |
static void s2n_fuzz_cleanup() |
| 88 |
{ |
| 89 |
s2n_config_free(conn_config); |
| 90 |
free(cert_chain); |
| 91 |
free(private_key); |
| 92 |
s2n_cert_chain_and_key_free(default_cert); |
| 93 |
} |
| 94 |
|
| 95 |
S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup) |