Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_server_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 <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/s2n_tls.h"
24 #include "tls/s2n_tls13.h"
25 #include "utils/s2n_safety.h"
26
27 /* Test vectors from https://tools.ietf.org/html/rfc8448#section-3 */
28
29 /* whole cert message without 0b0001b9 header */
30 const char tls13_cert_message_hex[] =
31 "000001b50001b03082"
32 "01ac30820115a003020102020102300d06092a8648"
33 "86f70d01010b0500300e310c300a06035504031303"
34 "727361301e170d3136303733303031323335395a17"
35 "0d3236303733303031323335395a300e310c300a06"
36 "03550403130372736130819f300d06092a864886f7"
37 "0d010101050003818d0030818902818100b4bb498f"
38 "8279303d980836399b36c6988c0c68de55e1bdb826"
39 "d3901a2461eafd2de49a91d015abbc9a95137ace6c"
40 "1af19eaa6af98c7ced43120998e187a80ee0ccb052"
41 "4b1b018c3e0b63264d449a6d38e22a5fda43084674"
42 "8030530ef0461c8ca9d9efbfae8ea6d1d03e2bd193"
43 "eff0ab9a8002c47428a6d35a8d88d79f7f1e3f0203"
44 "010001a31a301830090603551d1304023000300b06"
45 "03551d0f0404030205a0300d06092a864886f70d01"
46 "010b05000381810085aad2a0e5b9276b908c65f73a"
47 "7267170618a54c5f8a7b337d2df7a594365417f2ea"
48 "e8f8a58c8f8172f9319cf36b7fd6c55b80f21a0301"
49 "5156726096fd335e5e67f2dbf102702e608ccae6be"
50 "c1fc63a42a99be5c3eb7107c3c54e9b9eb2bd5203b"
51 "1c3b84e0a8b2f759409ba3eac9d91d402dcc0cc8f8"
52 "961229ac9187b42b4de10000";
53
54 /* cert only */
55 const char tls13_cert_hex[] =
56 "3082" /* without certificate chain header */
57 "01ac30820115a003020102020102300d06092a8648"
58 "86f70d01010b0500300e310c300a06035504031303"
59 "727361301e170d3136303733303031323335395a17"
60 "0d3236303733303031323335395a300e310c300a06"
61 "03550403130372736130819f300d06092a864886f7"
62 "0d010101050003818d0030818902818100b4bb498f"
63 "8279303d980836399b36c6988c0c68de55e1bdb826"
64 "d3901a2461eafd2de49a91d015abbc9a95137ace6c"
65 "1af19eaa6af98c7ced43120998e187a80ee0ccb052"
66 "4b1b018c3e0b63264d449a6d38e22a5fda43084674"
67 "8030530ef0461c8ca9d9efbfae8ea6d1d03e2bd193"
68 "eff0ab9a8002c47428a6d35a8d88d79f7f1e3f0203"
69 "010001a31a301830090603551d1304023000300b06"
70 "03551d0f0404030205a0300d06092a864886f70d01"
71 "010b05000381810085aad2a0e5b9276b908c65f73a"
72 "7267170618a54c5f8a7b337d2df7a594365417f2ea"
73 "e8f8a58c8f8172f9319cf36b7fd6c55b80f21a0301"
74 "5156726096fd335e5e67f2dbf102702e608ccae6be"
75 "c1fc63a42a99be5c3eb7107c3c54e9b9eb2bd5203b"
76 "1c3b84e0a8b2f759409ba3eac9d91d402dcc0cc8f8"
77 "961229ac9187b42b4de1";
78
79 /* certificate chain header. It contains
80 1. Request Context length (00)
81 2. Cert chain length (0001b5)
82 3. Cert length (0001b0)
83 */
84 const char tls13_cert_chain_header_hex[] =
85 "000001b50001b0";
86
87 int main(int argc, char **argv)
88 {
89 BEGIN_TEST();
90 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
91
92 /* Test s2n_server_cert_recv() parses tls13 certificate */
93 {
94 S2N_BLOB_FROM_HEX(tls13_cert, tls13_cert_message_hex);
95 struct s2n_connection *conn = NULL;
96 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
97
98 conn->x509_validator.skip_cert_validation = 1;
99
100 /* success case in tls13 parsing mode */
101 conn->actual_protocol_version = S2N_TLS13;
102 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS13);
103 EXPECT_SUCCESS(s2n_stuffer_write(&conn->handshake.io, &tls13_cert));
104 EXPECT_SUCCESS(s2n_server_cert_recv(conn));
105 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->handshake.io), 0);
106
107 /* failure case in tls12 parsing mode */
108 conn->actual_protocol_version = S2N_TLS12;
109 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
110 EXPECT_SUCCESS(s2n_stuffer_write(&conn->handshake.io, &tls13_cert));
111 EXPECT_FAILURE(s2n_server_cert_recv(conn));
112
113 EXPECT_SUCCESS(s2n_connection_free(conn));
114 }
115
116 /* Test s2n_server_cert_send() verify server's certificate */
117 {
118 char *tls13_cert_chain_hex = NULL;
119 /* creating a certificate chain by concatenating
120 1. chain header
121 2. certificate
122 */
123 EXPECT_NOT_NULL(tls13_cert_chain_hex = malloc(S2N_MAX_TEST_PEM_SIZE));
124 strcpy(tls13_cert_chain_hex, tls13_cert_chain_header_hex);
125 strcat(tls13_cert_chain_hex, tls13_cert_hex);
126
127 S2N_BLOB_FROM_HEX(tls13_cert, tls13_cert_chain_hex);
128 S2N_BLOB_FROM_HEX(tls13_cert_chain, tls13_cert_hex);
129
130 struct s2n_connection *conn = NULL;
131 uint8_t certificate_request_context_len = 0;
132
133 struct s2n_cert cert = { .raw = tls13_cert_chain, .next = NULL };
134 /* .chain_size is size of cert + 3 for the 3 bytes to express the length */
135 struct s2n_cert_chain cert_chain = { .head = &cert, .chain_size = tls13_cert_chain.size + 3 };
136 struct s2n_cert_chain_and_key cert_chain_and_key = { .cert_chain = &cert_chain };
137
138 /* tls13 mode */
139 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
140 conn->actual_protocol_version = S2N_TLS13;
141 conn->handshake_params.our_chain_and_key = &cert_chain_and_key;
142 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS13);
143 EXPECT_SUCCESS(s2n_server_cert_send(conn));
144
145 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->handshake.io), tls13_cert.size + 2);
146 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->handshake.io, &certificate_request_context_len));
147
148 /* server's certificate request context should always be of zero length */
149 EXPECT_EQUAL(certificate_request_context_len, 0);
150 EXPECT_SUCCESS(s2n_connection_free(conn));
151
152 /* tls12 mode */
153 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
154 conn->actual_protocol_version = S2N_TLS12;
155 conn->handshake_params.our_chain_and_key = &cert_chain_and_key;
156 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
157 EXPECT_SUCCESS(s2n_server_cert_send(conn));
158 /* In tls1.2 there is no certificate request context.
159 TLS1.2 Cert length = TLS1.3 Cert length -1 (server's request context)*/
160 EXPECT_EQUAL(s2n_stuffer_data_available(&conn->handshake.io), tls13_cert.size - 1);
161 EXPECT_SUCCESS(s2n_connection_free(conn));
162
163 free(tls13_cert_chain_hex);
164 }
165
166 /* Test server sends cert and client receives cert for tls 1.3 */
167 {
168 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
169
170 struct s2n_connection *server_conn = NULL;
171 struct s2n_connection *client_conn = NULL;
172 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
173 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
174 server_conn->actual_protocol_version = S2N_TLS13;
175 client_conn->actual_protocol_version = S2N_TLS13;
176 client_conn->x509_validator.skip_cert_validation = 1;
177
178 S2N_BLOB_FROM_HEX(tls13_cert_chain, tls13_cert_hex);
179 S2N_BLOB_FROM_HEX(tls13_cert_message, tls13_cert_message_hex);
180
181 struct s2n_cert cert = { .raw = tls13_cert_chain, .next = NULL };
182 struct s2n_cert_chain cert_chain = { .head = &cert, .chain_size = tls13_cert_chain.size + 3 };
183 struct s2n_cert_chain_and_key cert_chain_and_key = { .cert_chain = &cert_chain };
184 server_conn->handshake_params.our_chain_and_key = &cert_chain_and_key;
185
186 EXPECT_SUCCESS(s2n_server_cert_send(server_conn));
187 EXPECT_EQUAL(s2n_stuffer_data_available(&server_conn->handshake.io), tls13_cert_message.size);
188 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io, s2n_stuffer_data_available(&server_conn->handshake.io)));
189 EXPECT_EQUAL(s2n_stuffer_data_available(&client_conn->handshake.io), tls13_cert_message.size);
190 EXPECT_SUCCESS(s2n_server_cert_recv(client_conn));
191
192 EXPECT_SUCCESS(s2n_connection_free(server_conn));
193 EXPECT_SUCCESS(s2n_connection_free(client_conn));
194
195 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
196 };
197
198 END_TEST();
199 }