|
| 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 "tls/s2n_crypto.h" |
| 17 | + |
| 18 | +#include "crypto/s2n_fips.h" |
| 19 | +#include "s2n_test.h" |
| 20 | +#include "testlib/s2n_testlib.h" |
| 21 | + |
| 22 | +int main() |
| 23 | +{ |
| 24 | + BEGIN_TEST(); |
| 25 | + |
| 26 | + DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, |
| 27 | + s2n_cert_chain_and_key_ptr_free); |
| 28 | + EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key, |
| 29 | + S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); |
| 30 | + |
| 31 | + /* Test s2n_connection_get_master_secret */ |
| 32 | + { |
| 33 | + const uint8_t test_secret[S2N_TLS_SECRET_LEN] = { |
| 34 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 35 | + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, |
| 36 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, |
| 37 | + 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFF, |
| 38 | + 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81 |
| 39 | + }; |
| 40 | + |
| 41 | + const uint8_t supported_versions[] = { S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12 }; |
| 42 | + |
| 43 | + /* s2n_connection_get_master_secret takes a constant connection, so our |
| 44 | + * tests can share the same connection. |
| 45 | + */ |
| 46 | + DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), |
| 47 | + s2n_connection_ptr_free); |
| 48 | + EXPECT_OK(s2n_skip_handshake(conn)); |
| 49 | + EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.master_secret, |
| 50 | + test_secret, sizeof(test_secret)); |
| 51 | + |
| 52 | + /* Test safety checks */ |
| 53 | + { |
| 54 | + uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 55 | + EXPECT_FAILURE_WITH_ERRNO( |
| 56 | + s2n_connection_get_master_secret(conn, NULL, 0), |
| 57 | + S2N_ERR_NULL); |
| 58 | + EXPECT_FAILURE_WITH_ERRNO( |
| 59 | + s2n_connection_get_master_secret(NULL, output, 0), |
| 60 | + S2N_ERR_NULL); |
| 61 | + }; |
| 62 | + |
| 63 | + /* Test: successfully get master secret */ |
| 64 | + { |
| 65 | + uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 66 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); |
| 67 | + EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); |
| 68 | + }; |
| 69 | + |
| 70 | + /* Test: TLS1.3 not supported */ |
| 71 | + { |
| 72 | + uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 73 | + |
| 74 | + conn->actual_protocol_version = S2N_TLS13; |
| 75 | + EXPECT_FAILURE_WITH_ERRNO( |
| 76 | + s2n_connection_get_master_secret(conn, output, sizeof(output)), |
| 77 | + S2N_ERR_INVALID_STATE); |
| 78 | + |
| 79 | + conn->actual_protocol_version = S2N_TLS12; |
| 80 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); |
| 81 | + EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); |
| 82 | + }; |
| 83 | + |
| 84 | + /* Test: at least S2N_TLS_SECRET_LEN of output required */ |
| 85 | + { |
| 86 | + uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 87 | + |
| 88 | + /* Fail if insufficient memory */ |
| 89 | + EXPECT_FAILURE_WITH_ERRNO( |
| 90 | + s2n_connection_get_master_secret(conn, output, 0), |
| 91 | + S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 92 | + EXPECT_FAILURE_WITH_ERRNO( |
| 93 | + s2n_connection_get_master_secret(conn, output, 1), |
| 94 | + S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 95 | + EXPECT_FAILURE_WITH_ERRNO( |
| 96 | + s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN - 1), |
| 97 | + S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 98 | + |
| 99 | + /* Succeed if exactly S2N_TLS_SECRET_LEN bytes */ |
| 100 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN)); |
| 101 | + EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); |
| 102 | + |
| 103 | + /* Succeed if more than S2N_TLS_SECRET_LEN bytes */ |
| 104 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN + 1)); |
| 105 | + EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); |
| 106 | + }; |
| 107 | + |
| 108 | + /* Test: handshake must be complete */ |
| 109 | + { |
| 110 | + uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 111 | + |
| 112 | + conn->handshake.message_number = 0; |
| 113 | + EXPECT_FAILURE_WITH_ERRNO( |
| 114 | + s2n_connection_get_master_secret(conn, output, sizeof(output)), |
| 115 | + S2N_ERR_HANDSHAKE_NOT_COMPLETE); |
| 116 | + |
| 117 | + EXPECT_OK(s2n_skip_handshake(conn)); |
| 118 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); |
| 119 | + EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); |
| 120 | + }; |
| 121 | + |
| 122 | + /* Test: self-talk */ |
| 123 | + for (size_t i = 0; i < s2n_array_len(supported_versions); i++) { |
| 124 | + const uint8_t version = supported_versions[i]; |
| 125 | + |
| 126 | + /* See https://github.com/aws/s2n-tls/issues/4476 |
| 127 | + * Retrieving the master secret won't vary between FIPS and non-FIPS, |
| 128 | + * so this testing limitation is not a concern. |
| 129 | + */ |
| 130 | + if (s2n_is_in_fips_mode() && version == S2N_SSLv3) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 135 | + EXPECT_NOT_NULL(config); |
| 136 | + EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key)); |
| 137 | + EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); |
| 138 | + EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all")); |
| 139 | + |
| 140 | + DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), |
| 141 | + s2n_connection_ptr_free); |
| 142 | + EXPECT_NOT_NULL(client); |
| 143 | + EXPECT_SUCCESS(s2n_connection_set_config(client, config)); |
| 144 | + client->client_protocol_version = version; |
| 145 | + |
| 146 | + DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), |
| 147 | + s2n_connection_ptr_free); |
| 148 | + EXPECT_NOT_NULL(server); |
| 149 | + EXPECT_SUCCESS(s2n_connection_set_config(server, config)); |
| 150 | + /* Set server master secret to known value to ensure overridden later */ |
| 151 | + memset(server->secrets.version.tls12.master_secret, 1, S2N_TLS_SECRET_LEN); |
| 152 | + |
| 153 | + struct s2n_test_io_pair io_pair = { 0 }; |
| 154 | + EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); |
| 155 | + EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair)); |
| 156 | + EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); |
| 157 | + EXPECT_EQUAL(server->actual_protocol_version, version); |
| 158 | + |
| 159 | + /* server output matches master secret */ |
| 160 | + uint8_t server_output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 161 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(server, |
| 162 | + server_output, sizeof(server_output))); |
| 163 | + EXPECT_BYTEARRAY_EQUAL(server->secrets.version.tls12.master_secret, |
| 164 | + server_output, sizeof(server_output)); |
| 165 | + |
| 166 | + /* client output matches master secret */ |
| 167 | + uint8_t client_output[S2N_TLS_SECRET_LEN] = { 0 }; |
| 168 | + EXPECT_SUCCESS(s2n_connection_get_master_secret(client, |
| 169 | + client_output, sizeof(client_output))); |
| 170 | + EXPECT_BYTEARRAY_EQUAL(client->secrets.version.tls12.master_secret, |
| 171 | + client_output, sizeof(client_output)); |
| 172 | + |
| 173 | + /* client and server output match */ |
| 174 | + EXPECT_BYTEARRAY_EQUAL(server_output, client_output, sizeof(client_output)); |
| 175 | + }; |
| 176 | + }; |
| 177 | + |
| 178 | + END_TEST(); |
| 179 | +} |
0 commit comments