Debian Codegraph @

crt/s2n/tests/unit/s2n_client_signature_algorithms_extension_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 #include <stdint.h>
17
18 #include "s2n_test.h"
19 #include "stuffer/s2n_stuffer.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/extensions/s2n_client_signature_algorithms.h"
22 #include "tls/s2n_config.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_tls.h"
25 #include "utils/s2n_safety.h"
26
27 int main(int argc, char **argv)
28 {
29 BEGIN_TEST();
30 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
31 struct s2n_config *config = NULL;
32 EXPECT_NOT_NULL(config = s2n_config_new());
33
34 struct s2n_cert_chain_and_key *chain_and_key = NULL;
35 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
36 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
37 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
38
39 /* Test should_send */
40 {
41 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
42
43 conn->actual_protocol_version = S2N_TLS10;
44 EXPECT_FALSE(s2n_client_signature_algorithms_extension.should_send(conn));
45
46 conn->actual_protocol_version = S2N_TLS12;
47 EXPECT_TRUE(s2n_client_signature_algorithms_extension.should_send(conn));
48
49 conn->actual_protocol_version = S2N_TLS13;
50 EXPECT_TRUE(s2n_client_signature_algorithms_extension.should_send(conn));
51
52 s2n_connection_free(conn);
53 };
54
55 /* Test that recv can parse send */
56 {
57 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
58 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
59
60 struct s2n_stuffer io = { 0 };
61 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io, 0));
62
63 EXPECT_SUCCESS(s2n_client_signature_algorithms_extension.send(client_conn, &io));
64 EXPECT_SUCCESS(s2n_client_signature_algorithms_extension.recv(server_conn, &io));
65 EXPECT_EQUAL(s2n_stuffer_data_available(&io), 0);
66
67 EXPECT_TRUE(server_conn->handshake_params.peer_sig_scheme_list.len > 0);
68
69 s2n_stuffer_free(&io);
70 s2n_connection_free(client_conn);
71 s2n_connection_free(server_conn);
72 };
73
74 /* Test that a valid algorithm is chosen when it is offered among unknown algorithms */
75 {
76 struct s2n_sig_scheme_list sig_hash_algs = {
77 .iana_list = { 0xFF01, 0xFFFF, TLS_SIGNATURE_SCHEME_RSA_PKCS1_SHA384 },
78 .len = 3,
79 };
80 struct s2n_connection *conn = NULL;
81 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
82 POSIX_GUARD(s2n_connection_set_config(conn, config));
83 conn->actual_protocol_version = S2N_TLS12;
84 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
85
86 struct s2n_stuffer signature_algorithms_extension = { 0 };
87 EXPECT_SUCCESS(s2n_stuffer_alloc(&signature_algorithms_extension, 2 + (sig_hash_algs.len * 2)));
88 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&signature_algorithms_extension, sig_hash_algs.len * 2));
89 for (size_t i = 0; i < sig_hash_algs.len; i++) {
90 EXPECT_SUCCESS(s2n_stuffer_write_uint16(&signature_algorithms_extension, sig_hash_algs.iana_list[i]));
91 }
92
93 /* If a valid algorithm is offered among unknown algorithms, the valid one should be chosen */
94 EXPECT_SUCCESS(s2n_client_signature_algorithms_extension.recv(conn, &signature_algorithms_extension));
95 EXPECT_EQUAL(conn->handshake_params.peer_sig_scheme_list.len, sig_hash_algs.len);
96 EXPECT_OK(s2n_signature_algorithm_select(conn));
97 EXPECT_EQUAL(conn->handshake_params.server_cert_sig_scheme->iana_value, TLS_SIGNATURE_SCHEME_RSA_PKCS1_SHA384);
98
99 EXPECT_SUCCESS(s2n_stuffer_free(&signature_algorithms_extension));
100 EXPECT_SUCCESS(s2n_connection_free(conn));
101 };
102
103 EXPECT_SUCCESS(s2n_config_free(config));
104 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
105
106 END_TEST();
107 }