Debian Codegraph @

crt/s2n/tests/fuzz/s2n_client_hello_recv_fuzz_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 /* Target Functions: s2n_client_hello_recv s2n_parse_client_hello s2n_populate_client_hello_extensions
17 s2n_process_client_hello s2n_collect_client_hello */
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 /* Returns the value of ctx as an int when called */
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 /* We need at least two bytes of input to set parameters */
50 S2N_FUZZ_ENSURE_MIN_LEN(len, 2);
51
52 /* Setup */
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 /* Pull a byte off the libfuzzer input and use it to set parameters */
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 /* When callback function is called, return int chosen by libfuzzer between -1 and 1 to reach all code branches */
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 /* Run Test
70 * Do not use GUARD macro here since the connection memory hasn't been freed.
71 */
72 s2n_client_hello_recv(server_conn);
73
74 /* Cleanup */
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)