Debian Codegraph @

crt/s2n/tests/fuzz/s2n_client_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_client_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 *server_conn = s2n_connection_new(S2N_SERVER);
67 POSIX_ENSURE_REF(server_conn);
68 POSIX_GUARD(s2n_stuffer_write_bytes(&server_conn->handshake.io, buf, len));
69
70 /* Returns void, so can't be guarded */
71 s2n_x509_trust_store_init_empty(&trust_store);
72
73 /* Pull a byte off the libfuzzer input and use it to set parameters */
74 uint8_t randval = 0;
75 POSIX_GUARD(s2n_stuffer_read_uint8(&server_conn->handshake.io, &randval));
76
77 if(randval % 2) {
78 POSIX_GUARD(s2n_x509_trust_store_from_ca_file(&trust_store, S2N_DEFAULT_TEST_CERT_CHAIN, NULL));
79 POSIX_GUARD(s2n_connection_set_verify_host_callback(server_conn, verify_host_accept_everything, &verify_data));
80 }
81
82 /* Returns void, so can't be guarded */
83 s2n_x509_validator_wipe(&server_conn->x509_validator);
84 POSIX_GUARD(s2n_x509_validator_init(&server_conn->x509_validator, &trust_store, 1));
85 server_conn->x509_validator.skip_cert_validation = (randval >> 1) % 2;
86 server_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_client_cert_recv(server_conn);
92
93 /* Cleanup */
94
95 /* Returns void, so can't be guarded */
96 s2n_x509_trust_store_wipe(&trust_store);
97 s2n_x509_validator_wipe(&server_conn->x509_validator);
98
99 POSIX_GUARD(s2n_connection_free(server_conn));
100
101 return S2N_SUCCESS;
102 }
103
104 S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, NULL)