Debian Codegraph @

crt/s2n/tests/fuzz/s2n_tls13_cert_verify_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_verify_recv s2n_signature_algorithm_recv
17 s2n_tls13_cert_read_and_verify_signature */
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 uint8_t *cert_chain = NULL;
33 uint8_t *private_key = NULL;
34 uint32_t cert_chain_len = 0;
35 uint32_t private_key_len = 0;
36 struct s2n_cert_chain_and_key *default_cert;
37 struct s2n_config *conn_config;
38
39 int s2n_fuzz_init(int *argc, char **argv[])
40 {
41 /* Initialize test chain and key */
42 cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE);
43 POSIX_ENSURE_REF(cert_chain);
44 private_key = malloc(S2N_MAX_TEST_PEM_SIZE);
45 POSIX_ENSURE_REF(private_key);
46 default_cert = s2n_cert_chain_and_key_new();
47 POSIX_ENSURE_REF(default_cert);
48 conn_config = s2n_config_new();
49 POSIX_ENSURE_REF(conn_config);
50 POSIX_GUARD(s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, &cert_chain_len, S2N_MAX_TEST_PEM_SIZE));
51 POSIX_GUARD(s2n_read_test_pem_and_len(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, &private_key_len, S2N_MAX_TEST_PEM_SIZE));
52 POSIX_GUARD(s2n_cert_chain_and_key_load_pem_bytes(default_cert, cert_chain, cert_chain_len, private_key, private_key_len));
53 POSIX_GUARD(s2n_config_add_cert_chain_and_key_to_store(conn_config, default_cert));
54
55 return S2N_SUCCESS;
56 }
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 s2n_connection *conn = s2n_connection_new(S2N_SERVER);
65 POSIX_ENSURE_REF(conn);
66 POSIX_GUARD(s2n_connection_set_config(conn, conn_config));
67 POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, buf, len));
68
69 /* Pull a byte off the libfuzzer input and use it to set the connection mode */
70 uint8_t randval = 0;
71 POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &randval));
72 if (randval % 2) {
73 conn->mode = S2N_CLIENT;
74 }
75
76 /* Run Test
77 * Do not use GUARD macro here since the connection memory hasn't been freed.
78 */
79 s2n_tls13_cert_verify_recv(conn);
80
81 /* Cleanup */
82 POSIX_GUARD(s2n_connection_free(conn));
83
84 return S2N_SUCCESS;
85 }
86
87 static void s2n_fuzz_cleanup()
88 {
89 s2n_config_free(conn_config);
90 free(cert_chain);
91 free(private_key);
92 s2n_cert_chain_and_key_free(default_cert);
93 }
94
95 S2N_FUZZ_TARGET(s2n_fuzz_init, s2n_fuzz_test, s2n_fuzz_cleanup)