1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/extensions/s2n_cert_status.h"
const uint8_t ocsp_data[] = "OCSP DATA";
int s2n_test_enable_sending_extension(struct s2n_connection *conn, struct s2n_cert_chain_and_key *chain_and_key)
{
conn->status_type = S2N_STATUS_REQUEST_OCSP;
conn->handshake_params.our_chain_and_key = chain_and_key;
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_ocsp_data(chain_and_key, ocsp_data, s2n_array_len(ocsp_data)));
conn->x509_validator.state = VALIDATED;
return S2N_SUCCESS;
}
int main(int argc, char **argv)
{
BEGIN_TEST();
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
/* should_send */
{
struct s2n_config *config;
EXPECT_NOT_NULL(config = s2n_config_new());
struct s2n_connection *conn;
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
/* Don't send by default */
EXPECT_FALSE(s2n_cert_status_extension.should_send(conn));
/* Send if all prerequisites met */
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
EXPECT_TRUE(s2n_cert_status_extension.should_send(conn));
/* Send if client */
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
conn->mode = S2N_CLIENT;
EXPECT_TRUE(s2n_cert_status_extension.should_send(conn));
/* Send if server */
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
conn->mode = S2N_SERVER;
EXPECT_TRUE(s2n_cert_status_extension.should_send(conn));
/* Don't send if no certificate set */
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
conn->handshake_params.our_chain_and_key = NULL;
EXPECT_FALSE(s2n_cert_status_extension.should_send(conn));
/* Don't send if no ocsp data */
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
EXPECT_SUCCESS(s2n_free(&conn->handshake_params.our_chain_and_key->ocsp_status));
EXPECT_FALSE(s2n_cert_status_extension.should_send(conn));
EXPECT_SUCCESS(s2n_connection_free(conn));
EXPECT_SUCCESS(s2n_config_free(config));
};
/* Test send */
{
struct s2n_connection *conn;
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
struct s2n_stuffer stuffer = { 0 };
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_cert_status_extension.send(conn, &stuffer));
uint8_t request_type;
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&stuffer, &request_type));
EXPECT_EQUAL(request_type, S2N_STATUS_REQUEST_OCSP);
uint32_t ocsp_size;
EXPECT_SUCCESS(s2n_stuffer_read_uint24(&stuffer, &ocsp_size));
EXPECT_EQUAL(ocsp_size, s2n_stuffer_data_available(&stuffer));
EXPECT_EQUAL(ocsp_size, s2n_array_len(ocsp_data));
uint8_t *actual_ocsp_data;
EXPECT_NOT_NULL(actual_ocsp_data = s2n_stuffer_raw_read(&stuffer, ocsp_size));
EXPECT_BYTEARRAY_EQUAL(actual_ocsp_data, ocsp_data, ocsp_size);
EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Test recv */
{
struct s2n_connection *conn;
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
struct s2n_stuffer stuffer = { 0 };
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_cert_status_extension.send(conn, &stuffer));
EXPECT_EQUAL(conn->status_response.size, 0);
EXPECT_SUCCESS(s2n_cert_status_extension.recv(conn, &stuffer));
EXPECT_BYTEARRAY_EQUAL(conn->status_response.data, ocsp_data, s2n_array_len(ocsp_data));
EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Test recv - not ocsp */
{
struct s2n_connection *conn;
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
struct s2n_stuffer stuffer = { 0 };
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&stuffer, S2N_STATUS_REQUEST_NONE));
EXPECT_EQUAL(conn->status_response.size, 0);
EXPECT_SUCCESS(s2n_cert_status_extension.recv(conn, &stuffer));
EXPECT_EQUAL(conn->status_response.size, 0);
EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Test recv - bad ocsp data */
{
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
EXPECT_NOT_NULL(conn);
EXPECT_SUCCESS(s2n_test_enable_sending_extension(conn, chain_and_key));
DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_cert_status_extension.send(conn, &stuffer));
if (s2n_x509_ocsp_stapling_supported()) {
EXPECT_FAILURE_WITH_ERRNO(s2n_cert_status_extension.recv(conn, &stuffer),
S2N_ERR_INVALID_OCSP_RESPONSE);
} else {
/* s2n_x509_validator_validate_cert_stapled_ocsp_response returns untrusted error if ocsp is not supported */
EXPECT_FAILURE_WITH_ERRNO(s2n_cert_status_extension.recv(conn, &stuffer),
S2N_ERR_CERT_UNTRUSTED);
}
EXPECT_SUCCESS(s2n_connection_free(conn));
};
/* Self-talk tests */
if (s2n_x509_ocsp_stapling_supported() && s2n_is_tls13_fully_supported()) {
uint8_t ocsp_response[S2N_MAX_TEST_PEM_SIZE] = { 0 };
uint32_t ocsp_response_len = 0;
EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_OCSP_RESPONSE_DER, ocsp_response, &ocsp_response_len, S2N_MAX_TEST_PEM_SIZE));
EXPECT_TRUE(ocsp_response_len > 0);
DEFER_CLEANUP(struct s2n_cert_chain_and_key *ocsp_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ocsp_chain_and_key, S2N_OCSP_SERVER_CERT, S2N_OCSP_SERVER_KEY));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_ocsp_data(ocsp_chain_and_key, ocsp_response, ocsp_response_len));
/* Client requests OCSP staple, and server sends OCSP response */
{
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(client_config);
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_OCSP_CA_CERT, NULL));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(client_config, S2N_STATUS_REQUEST_OCSP));
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(server_config);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ocsp_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
EXPECT_SUCCESS(s2n_set_server_name(client_conn, "s2n Test Cert"));
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
uint32_t client_received_ocsp_response_len = 0;
const uint8_t *client_received_ocsp_response = s2n_connection_get_ocsp_response(client_conn,
&client_received_ocsp_response_len);
EXPECT_NOT_NULL(client_received_ocsp_response);
uint32_t server_received_ocsp_response_len = 0;
const uint8_t *server_received_ocsp_response = s2n_connection_get_ocsp_response(server_conn,
&server_received_ocsp_response_len);
/* Only the client requested a response, the server should not have received one. */
EXPECT_NULL(server_received_ocsp_response);
/* The server sent an OCSP response, and the client received an OCSP response */
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(server_conn), 1);
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(client_conn), 1);
}
/* Server requests OCSP staple, and client sends OCSP response */
{
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(client_config);
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_OCSP_CA_CERT, NULL));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(client_config, S2N_STATUS_REQUEST_NONE));
EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, ocsp_chain_and_key));
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(server_config);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ocsp_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(server_config, S2N_STATUS_REQUEST_OCSP));
EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config, S2N_OCSP_CA_CERT, NULL));
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
EXPECT_SUCCESS(s2n_set_server_name(client_conn, "s2n Test Cert"));
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
uint32_t client_received_ocsp_response_len = 0;
const uint8_t *client_received_ocsp_response = s2n_connection_get_ocsp_response(client_conn,
&client_received_ocsp_response_len);
/* Only the server requested a response, the client should not have received one. */
EXPECT_NULL(client_received_ocsp_response);
uint32_t server_received_ocsp_response_len = 0;
const uint8_t *server_received_ocsp_response = s2n_connection_get_ocsp_response(server_conn,
&server_received_ocsp_response_len);
EXPECT_NOT_NULL(server_received_ocsp_response);
/* The server did not send an OCSP response, and the client did not receive an OCSP response */
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(server_conn), 0);
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(client_conn), 0);
}
/* Client and server both request OCSP staples, and client and server both send responses */
{
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(client_config);
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_OCSP_CA_CERT, NULL));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(client_config, S2N_STATUS_REQUEST_OCSP));
EXPECT_SUCCESS(s2n_config_set_client_auth_type(client_config, S2N_CERT_AUTH_OPTIONAL));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, ocsp_chain_and_key));
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(server_config);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ocsp_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(server_config, S2N_STATUS_REQUEST_OCSP));
EXPECT_SUCCESS(s2n_config_set_client_auth_type(server_config, S2N_CERT_AUTH_REQUIRED));
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(server_config, S2N_OCSP_CA_CERT, NULL));
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
EXPECT_SUCCESS(s2n_set_server_name(client_conn, "s2n Test Cert"));
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
uint32_t client_received_ocsp_response_len = 0;
const uint8_t *client_received_ocsp_response = s2n_connection_get_ocsp_response(client_conn,
&client_received_ocsp_response_len);
EXPECT_NOT_NULL(client_received_ocsp_response);
uint32_t server_received_ocsp_response_len = 0;
const uint8_t *server_received_ocsp_response = s2n_connection_get_ocsp_response(server_conn,
&server_received_ocsp_response_len);
EXPECT_NOT_NULL(server_received_ocsp_response);
/* The server sent an OCSP response, and the client received an OCSP response */
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(server_conn), 1);
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(client_conn), 1);
}
/* Server sets an OCSP response but client does not request OCSP stapling */
{
DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(client_config);
EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_OCSP_CA_CERT, NULL));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "default_tls13"));
EXPECT_SUCCESS(s2n_config_set_status_request_type(client_config, S2N_STATUS_REQUEST_NONE));
DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
EXPECT_NOT_NULL(server_config);
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ocsp_chain_and_key));
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "default_tls13"));
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_NOT_NULL(server_conn);
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
EXPECT_NOT_NULL(client_conn);
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
EXPECT_SUCCESS(s2n_set_server_name(client_conn, "s2n Test Cert"));
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
uint32_t client_received_ocsp_response_len = 0;
const uint8_t *client_received_ocsp_response = s2n_connection_get_ocsp_response(client_conn,
&client_received_ocsp_response_len);
uint32_t server_received_ocsp_response_len = 0;
const uint8_t *server_received_ocsp_response = s2n_connection_get_ocsp_response(server_conn,
&server_received_ocsp_response_len);
/* Both the server and client did not request OCSP responses, so neither should have received them. */
EXPECT_NULL(client_received_ocsp_response);
EXPECT_NULL(server_received_ocsp_response);
/* The server did not send an OCSP response, and the client did not receive an OCSP response */
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(server_conn), 0);
EXPECT_EQUAL(s2n_connection_is_ocsp_stapled(client_conn), 0);
}
}
END_TEST();
return 0;
}
|