Debian Codegraph @

crt/s2n/tests/fuzz/s2n_tls13_cert_req_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_tls13_cert_req_recv s2n_extension_list_recv s2n_extension_process
17 s2n_extension_list_process s2n_extension_parse s2n_extension_list_parse */
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 static char *cert_chain, *private_key;
33 struct s2n_cert_chain_and_key *default_cert;
34 struct s2n_config *client_config;
35
36 int s2n_fuzz_init(int *argc, char **argv[])
37 {
38 /* Initialize test chain and key */
39 cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE);
40 POSIX_ENSURE_REF(cert_chain);
41 private_key = malloc(S2N_MAX_TEST_PEM_SIZE);
42 POSIX_ENSURE_REF(private_key);
43 default_cert = s2n_cert_chain_and_key_new();
44 POSIX_ENSURE_REF(default_cert);
45 client_config = s2n_config_new();
46 POSIX_ENSURE_REF(client_config);
47 POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
48 POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
49 POSIX_GUARD(s2n_cert_chain_and_key_load_pem(default_cert, cert_chain, private_key));
50 POSIX_GUARD(s2n_config_add_cert_chain_and_key_to_store(client_config, default_cert));
51 return S2N_SUCCESS;
52 }
53
54 int s2n_fuzz_test(const uint8_t *buf, size_t len)
55 {
56 /* Setup */
57 struct s2n_connection *client_conn = s2n_connection_new(S2N_SERVER);
58 POSIX_ENSURE_REF(client_conn);
59 POSIX_GUARD(s2n_connection_set_config(client_conn, client_config));
60 POSIX_GUARD(s2n_stuffer_write_bytes(&client_conn->handshake.io, buf, len));
61
62 /* Run Test
63 * Do not use GUARD macro here since the connection memory hasn't been freed.
64 */
65 s2n_tls13_cert_req_recv(client_conn);
66
67 /* Cleanup */
68 POSIX_GUARD(s2n_connection_free(client_conn));
69
70 return S2N_SUCCESS;
71 }
72
73 static void s2n_fuzz_cleanup()
74 {
75 s2n_config_free(client_config);
76 free(cert_chain);
77 free(private_key);
78 s2n_cert_chain_and_key_free(default_cert);
79 }
80
81 S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup)