Debian Codegraph @

crt/s2n/tests/unit/s2n_client_empty_cert_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 "api/s2n.h"
19 #include "s2n_test.h"
20 #include "tls/s2n_connection.h"
21 #include "tls/s2n_tls.h"
22 #include "utils/s2n_safety.h"
23
24 int main(int argc, char **argv)
25 {
26 BEGIN_TEST();
27 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
28
29 /* Test s2n_send_empty_cert_chain */
30 {
31 struct s2n_stuffer out = { 0 };
32 /* Magic number 3 is the length of the certificate_length field */
33 EXPECT_SUCCESS(s2n_stuffer_alloc(&out, 3));
34
35 EXPECT_SUCCESS(s2n_send_empty_cert_chain(&out));
36 EXPECT_EQUAL(s2n_stuffer_data_available(&out), 3);
37 uint32_t cert_len = 0;
38 EXPECT_SUCCESS(s2n_stuffer_read_uint24(&out, &cert_len));
39 EXPECT_EQUAL(cert_len, 0);
40
41 EXPECT_SUCCESS(s2n_stuffer_free(&out));
42 };
43
44 /* Client sends the empty cert when no client default chain and key */
45 {
46 struct s2n_config *client_config = NULL;
47 EXPECT_NOT_NULL(client_config = s2n_config_new());
48
49 struct s2n_connection *client_conn = NULL;
50 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
51 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
52 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_OPTIONAL));
53
54 /* client send empty cert */
55 EXPECT_SUCCESS(s2n_client_cert_send(client_conn));
56
57 /* verify post-conditions */
58 EXPECT_TRUE(client_conn->handshake.handshake_type & NO_CLIENT_CERT);
59 /* Magic number 3 is the length of the certificate_length field */
60 EXPECT_EQUAL(s2n_stuffer_data_available(&client_conn->handshake.io), 3);
61
62 uint32_t cert_len = 0;
63 EXPECT_SUCCESS(s2n_stuffer_read_uint24(&client_conn->handshake.io, &cert_len));
64 EXPECT_EQUAL(cert_len, 0);
65
66 EXPECT_SUCCESS(s2n_connection_free(client_conn));
67 EXPECT_SUCCESS(s2n_config_free(client_config));
68 };
69
70 /* Client fails to send empty cert when S2N_CERT_AUTH_REQUIRED */
71 {
72 struct s2n_config *client_config = NULL;
73 EXPECT_NOT_NULL(client_config = s2n_config_new());
74
75 struct s2n_connection *client_conn = NULL;
76 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
77 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
78 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
79
80 /* client send empty cert */
81 EXPECT_FAILURE(s2n_client_cert_send(client_conn));
82
83 EXPECT_SUCCESS(s2n_connection_free(client_conn));
84 EXPECT_SUCCESS(s2n_config_free(client_config));
85 };
86
87 /* Server receives empty cert */
88 {
89 struct s2n_config *config = NULL;
90 EXPECT_NOT_NULL(config = s2n_config_new());
91
92 struct s2n_connection *client_conn = NULL;
93 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
94 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
95 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_OPTIONAL));
96
97 struct s2n_connection *server_conn = NULL;
98 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
99 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
100 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_OPTIONAL));
101
102 /* client send empty cert */
103 EXPECT_SUCCESS(s2n_client_cert_send(client_conn));
104
105 EXPECT_TRUE(client_conn->handshake.handshake_type & NO_CLIENT_CERT);
106 /* Magic number 3 is the length of the certificate_length field */
107 EXPECT_EQUAL(s2n_stuffer_data_available(&client_conn->handshake.io), 3);
108
109 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io, s2n_stuffer_data_available(&client_conn->handshake.io)));
110 EXPECT_EQUAL(s2n_stuffer_data_available(&server_conn->handshake.io), 3);
111
112 /* server receives empty cert */
113 EXPECT_SUCCESS(s2n_client_cert_recv(server_conn));
114 EXPECT_TRUE(server_conn->handshake.handshake_type & NO_CLIENT_CERT);
115
116 EXPECT_SUCCESS(s2n_connection_free(server_conn));
117 EXPECT_SUCCESS(s2n_connection_free(client_conn));
118 EXPECT_SUCCESS(s2n_config_free(config));
119 };
120
121 END_TEST();
122 }