Debian Codegraph @

crt/s2n/tests/fuzz/s2n_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_cert_req_recv s2n_recv_client_cert_preferences
17 s2n_cert_type_to_pkey_type s2n_recv_supported_sig_scheme_list
18 s2n_choose_sig_scheme_from_peer_preference_list
19 s2n_set_cert_chain_as_client */
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.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_safety.h"
38 #include "s2n_test.h"
39 #include "testlib/s2n_testlib.h"
40
41 static char *cert_chain, *private_key;
42 struct s2n_cert_chain_and_key *default_cert;
43
44 int s2n_fuzz_init(int *argc, char **argv[])
45 {
46 /* Initialize test chain and key */
47 cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE);
48 POSIX_ENSURE_REF(cert_chain);
49 private_key = malloc(S2N_MAX_TEST_PEM_SIZE);
50 POSIX_ENSURE_REF(private_key);
51 default_cert = s2n_cert_chain_and_key_new();
52 POSIX_ENSURE_REF(default_cert);
53 POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
54 POSIX_GUARD(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
55 POSIX_GUARD(s2n_cert_chain_and_key_load_pem(default_cert, cert_chain, private_key));
56
57 return S2N_SUCCESS;
58 }
59
60 static const uint8_t TLS_VERSIONS[] = {S2N_TLS10, S2N_TLS11, S2N_TLS12};
61
62 int s2n_fuzz_test(const uint8_t *buf, size_t len)
63 {
64 /* We need at least one byte of input to set parameters */
65 S2N_FUZZ_ENSURE_MIN_LEN(len, 1);
66
67 /* Setup */
68 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
69 POSIX_ENSURE_REF(client_conn);
70 struct s2n_config *client_config = s2n_config_new();
71 POSIX_ENSURE_REF(client_config);
72 POSIX_GUARD(s2n_config_add_cert_chain_and_key_to_store(client_config, default_cert));
73 POSIX_GUARD(s2n_connection_set_config(client_conn, client_config));
74 POSIX_GUARD(s2n_stuffer_write_bytes(&client_conn->handshake.io, buf, len));
75
76 /* Pull a byte off the libfuzzer input and use it to set parameters */
77 uint8_t randval = 0;
78 POSIX_GUARD(s2n_stuffer_read_uint8(&client_conn->handshake.io, &randval));
79 client_conn->actual_protocol_version = TLS_VERSIONS[randval % s2n_array_len(TLS_VERSIONS)];
80
81 /* Run Test
82 * Do not use GUARD macro here since the connection memory hasn't been freed.
83 */
84 s2n_cert_req_recv(client_conn);
85
86 /* Cleanup */
87 POSIX_GUARD(s2n_connection_free(client_conn));
88 POSIX_GUARD(s2n_config_free(client_config));
89
90 return S2N_SUCCESS;
91 }
92
93 static void s2n_fuzz_cleanup()
94 {
95 free(cert_chain);
96 free(private_key);
97 s2n_cert_chain_and_key_free(default_cert);
98 }
99
100 S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup)