Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_cert_request_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 <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "s2n_test.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "testlib/s2n_testlib.h"
23 #include "tls/extensions/s2n_server_signature_algorithms.h"
24 #include "tls/s2n_tls.h"
25 #include "tls/s2n_tls13.h"
26 #include "utils/s2n_safety.h"
27
28 int main(int argc, char **argv)
29 {
30 BEGIN_TEST();
31 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
32
33 /* Test the output of s2n_tls13_cert_req_send() */
34 {
35 struct s2n_connection *server_conn = NULL;
36 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
37 server_conn->actual_protocol_version = S2N_TLS13;
38
39 EXPECT_SUCCESS(s2n_tls13_cert_req_send(server_conn));
40
41 /* verify output */
42 uint8_t request_context_length = 0;
43 uint16_t extensions_length = 0, extension_size = 0, extension_type = 0;
44 EXPECT_TRUE(s2n_stuffer_data_available(&server_conn->handshake.io) > 7);
45 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&server_conn->handshake.io, &request_context_length));
46 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&server_conn->handshake.io, &extensions_length));
47 EXPECT_EQUAL(s2n_stuffer_data_available(&server_conn->handshake.io), extensions_length);
48 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&server_conn->handshake.io, &extension_type));
49 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&server_conn->handshake.io, &extension_size));
50 EXPECT_EQUAL(request_context_length, 0);
51 EXPECT_EQUAL(extension_type, TLS_EXTENSION_SIGNATURE_ALGORITHMS);
52 EXPECT_TRUE(extension_size > 0);
53
54 EXPECT_SUCCESS(s2n_connection_free(server_conn));
55 }
56
57 /* Test client can receive and parse certificate request */
58 {
59 struct s2n_connection *server_conn = NULL;
60 struct s2n_connection *client_conn = NULL;
61 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
62 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
63 server_conn->actual_protocol_version = S2N_TLS13;
64 client_conn->actual_protocol_version = S2N_TLS13;
65
66 EXPECT_SUCCESS(s2n_tls13_cert_req_send(server_conn));
67 EXPECT_TRUE(s2n_stuffer_data_available(&server_conn->handshake.io) > 0);
68 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
69 EXPECT_TRUE(s2n_stuffer_data_available(&client_conn->handshake.io) > 0);
70 EXPECT_SUCCESS(s2n_tls13_cert_req_recv(client_conn));
71
72 EXPECT_TRUE(client_conn->handshake_params.peer_sig_scheme_list.len > 0);
73
74 EXPECT_SUCCESS(s2n_connection_free(client_conn));
75 EXPECT_SUCCESS(s2n_connection_free(server_conn));
76 };
77
78 /* Test request context length other than 0 fails */
79 {
80 struct s2n_connection *client_conn = NULL;
81 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
82 client_conn->actual_protocol_version = S2N_TLS13;
83
84 /* Request context correct */
85 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&client_conn->handshake.io, 0));
86 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_req_recv(client_conn), S2N_ERR_MISSING_EXTENSION);
87
88 /* Request context incorrect */
89 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&client_conn->handshake.io, 2));
90 EXPECT_FAILURE_WITH_ERRNO(s2n_tls13_cert_req_recv(client_conn), S2N_ERR_BAD_MESSAGE);
91
92 EXPECT_SUCCESS(s2n_connection_free(client_conn));
93 };
94
95 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
96 END_TEST();
97
98 return 0;
99 }