Debian Codegraph @

crt/s2n/tests/fuzz/s2n_server_cert_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_cert_recv s2n_x509_validator_validate_cert_chain */
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <openssl/crypto.h>
27 #include <openssl/err.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_random.h"
38 #include "utils/s2n_safety.h"
39
40 #include "s2n_test.h"
41 #include "testlib/s2n_testlib.h"
42
43 struct host_verify_data {
44 const char *name;
45 uint8_t found_name;
46 uint8_t callback_invoked;
47 };
48
49 static uint8_t verify_host_accept_everything(const char *host_name, size_t host_name_len, void *data)
50 {
51 struct host_verify_data *verify_data = (struct host_verify_data *) data;
52 verify_data->callback_invoked = 1;
53 return 1;
54 }
55
56 static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12, S2N_TLS13};
57
58 int s2n_fuzz_test(const uint8_t *buf, size_t len)
59 {
60 /* We need at least one byte of input to set parameters */
61 S2N_FUZZ_ENSURE_MIN_LEN(len, 1);
62
63 /* Setup */
64 struct host_verify_data verify_data = { .callback_invoked = 0, .found_name = 0, .name = NULL };
65 struct s2n_x509_trust_store trust_store;
66 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
67 POSIX_ENSURE_REF(conn);
68 POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, buf, len));
69
70 /* Returns void, so can't be guarded */
71 s2n_x509_validator_wipe(&conn->x509_validator);
72 s2n_x509_trust_store_init_empty(&trust_store);
73
74 /* Pull a byte off the libfuzzer input and use it to set parameters */
75 uint8_t randval = 0;
76 POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &randval));
77
78 if (randval % 2) {
79 POSIX_GUARD(s2n_x509_trust_store_from_ca_file(&trust_store, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
80 POSIX_GUARD(s2n_connection_set_verify_host_callback(conn, verify_host_accept_everything, &verify_data));
81 }
82
83 POSIX_GUARD(s2n_x509_validator_init(&conn->x509_validator, &trust_store, 1));
84
85 conn->x509_validator.skip_cert_validation = (randval >> 1) % 2;
86 conn->actual_protocol_version = TLS_VERSIONS[((randval >> 4) & 0x0f) % s2n_array_len(TLS_VERSIONS)];
87
88 /* Run Test
89 * Do not use GUARD macro here since the connection memory hasn't been freed.
90 */
91 s2n_server_cert_recv(conn);
92
93 /* Cleanup */
94 s2n_x509_trust_store_wipe(&trust_store);
95 s2n_x509_validator_wipe(&conn->x509_validator);
96
97 POSIX_GUARD(s2n_connection_free(conn));
98
99 return S2N_SUCCESS;
100 }
101
102 S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, NULL)