Debian Codegraph @

crt/s2n/tests/fuzz/s2n_server_hello_recv_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_server_hello_recv s2n_server_extensions_recv s2n_server_hello_retry_recv
17 s2n_connection_get_cipher_preferences s2n_set_cipher_as_client */
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
31 static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12, S2N_TLS13};
32 struct s2n_config *client_config;
33
34 int s2n_fuzz_test(const uint8_t *buf, size_t len)
35 {
36 /* We need at least one byte of input to set parameters */
37 S2N_FUZZ_ENSURE_MIN_LEN(len, 1);
38
39 /* Setup */
40 client_config = s2n_config_new();
41 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
42 POSIX_GUARD(s2n_connection_set_config(client_conn, client_config));
43 POSIX_ENSURE_REF(client_conn);
44 POSIX_GUARD(s2n_stuffer_write_bytes(&client_conn->handshake.io, buf, len));
45
46 /* Pull a byte off the libfuzzer input and use it to set parameters */
47 uint8_t randval = 0;
48 POSIX_GUARD(s2n_stuffer_read_uint8(&client_conn->handshake.io, &randval));
49 client_conn->client_protocol_version = TLS_VERSIONS[(randval & 0x0f) % s2n_array_len(TLS_VERSIONS)];
50 client_conn->server_protocol_version = TLS_VERSIONS[(randval >> 4) % s2n_array_len(TLS_VERSIONS)];
51 /* Run Test
52 * Do not use GUARD macro here since the connection memory hasn't been freed.
53 */
54 s2n_server_hello_recv(client_conn);
55
56 /* Cleanup */
57 POSIX_GUARD(s2n_config_free(client_config));
58 POSIX_GUARD(s2n_connection_free(client_conn));
59
60 return S2N_SUCCESS;
61 }
62
63 S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, NULL)