| 1 |
|
| 2 |
|
| 3 |
import copy |
| 4 |
import platform |
| 5 |
import pytest |
| 6 |
import re |
| 7 |
|
| 8 |
from configuration import ( |
| 9 |
available_ports, |
| 10 |
TLS13_CIPHERS, |
| 11 |
ALL_TEST_CURVES, |
| 12 |
MINIMAL_TEST_CERTS, |
| 13 |
) |
| 14 |
from common import ProviderOptions, Protocols, data_bytes |
| 15 |
from fixtures import managed_process |
| 16 |
from providers import Provider, S2N, OpenSSL |
| 17 |
from utils import ( |
| 18 |
invalid_test_parameters, |
| 19 |
get_parameter_name, |
| 20 |
get_expected_s2n_version, |
| 21 |
to_bytes, |
| 22 |
) |
| 23 |
|
| 24 |
PADDING_SIZE_SMALL = 250 |
| 25 |
PADDING_SIZE_MEDIUM = 1000 |
| 26 |
PADDING_SIZE_MAX = 1 << 14 |
| 27 |
|
| 28 |
PADDING_SIZES = [PADDING_SIZE_SMALL, PADDING_SIZE_MEDIUM, PADDING_SIZE_MAX] |
| 29 |
|
| 30 |
|
| 31 |
PAYLOAD_SIZE = 1024 |
| 32 |
|
| 33 |
OPENSSL_RECORD_WRITTEN_PATTERN = r"write to .*?\\n(.*?)\\n" |
| 34 |
OPENSSL_APP_DATA_HEADER_PATTERN = r"17 03 03 ([0-9a-f]{2} [0-9a-f]{2})" |
| 35 |
RECORD_SIZE_GROUP = 1 |
| 36 |
|
| 37 |
|
| 38 |
def strip_string_of_bytes(s: str) -> str: |
| 39 |
|
| 40 |
return s[2:-1] |
| 41 |
|
| 42 |
|
| 43 |
def get_payload_size_from_openssl_trace(record_size_bytes: str) -> int: |
| 44 |
|
| 45 |
size_in_hex = record_size_bytes.replace(" ", "") |
| 46 |
size = int(size_in_hex, 16) |
| 47 |
|
| 48 |
return size - 16 |
| 49 |
|
| 50 |
|
| 51 |
def assert_openssl_records_are_padded_correctly(openssl_output: str, padding_size: int): |
| 52 |
number_of_app_data_records = 0 |
| 53 |
|
| 54 |
records_written = re.findall(OPENSSL_RECORD_WRITTEN_PATTERN, openssl_output) |
| 55 |
for record_prefix in records_written: |
| 56 |
app_data_header = re.search(OPENSSL_APP_DATA_HEADER_PATTERN, record_prefix) |
| 57 |
if app_data_header: |
| 58 |
size_bytes = app_data_header.group(RECORD_SIZE_GROUP) |
| 59 |
size = get_payload_size_from_openssl_trace(size_bytes) |
| 60 |
|
| 61 |
assert size > 0 |
| 62 |
assert size % padding_size == 0 |
| 63 |
|
| 64 |
number_of_app_data_records += 1 |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
assert number_of_app_data_records >= 2 |
| 69 |
|
| 70 |
|
| 71 |
def test_nothing(): |
| 72 |
""" |
| 73 |
Sometimes the record padding test parameters in combination with the s2n libcrypto |
| 74 |
results in no test cases existing. In this case, pass a nothing test to avoid |
| 75 |
marking the entire codebuild run as failed. |
| 76 |
""" |
| 77 |
assert True |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 81 |
@pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name) |
| 82 |
@pytest.mark.parametrize("provider", [OpenSSL]) |
| 83 |
@pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name) |
| 84 |
|
| 85 |
@pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name) |
| 86 |
@pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name) |
| 87 |
@pytest.mark.parametrize("padding_size", PADDING_SIZES, ids=get_parameter_name) |
| 88 |
def test_s2n_server_handles_padded_records( |
| 89 |
managed_process, |
| 90 |
cipher, |
| 91 |
provider, |
| 92 |
curve, |
| 93 |
protocol, |
| 94 |
certificate, |
| 95 |
padding_size, |
| 96 |
): |
| 97 |
port = next(available_ports) |
| 98 |
|
| 99 |
random_bytes = data_bytes(PAYLOAD_SIZE) |
| 100 |
client_options = ProviderOptions( |
| 101 |
mode=Provider.ClientMode, |
| 102 |
port=port, |
| 103 |
cipher=cipher, |
| 104 |
cert=certificate.cert, |
| 105 |
curve=curve, |
| 106 |
data_to_send=random_bytes, |
| 107 |
insecure=True, |
| 108 |
protocol=protocol, |
| 109 |
extra_flags=["-record_padding", padding_size], |
| 110 |
verbose=True, |
| 111 |
) |
| 112 |
|
| 113 |
server_options = copy.copy(client_options) |
| 114 |
server_options.data_to_send = None |
| 115 |
server_options.mode = Provider.ServerMode |
| 116 |
server_options.key = certificate.key |
| 117 |
server_options.extra_flags = None |
| 118 |
|
| 119 |
s2nd = managed_process(S2N, server_options, timeout=5) |
| 120 |
openssl = managed_process(provider, client_options, timeout=5) |
| 121 |
|
| 122 |
for client_results in openssl.get_results(): |
| 123 |
client_results.assert_success() |
| 124 |
assert_openssl_records_are_padded_correctly( |
| 125 |
str(client_results.stdout), padding_size |
| 126 |
) |
| 127 |
|
| 128 |
expected_version = get_expected_s2n_version(protocol, provider) |
| 129 |
|
| 130 |
for server_results in s2nd.get_results(): |
| 131 |
server_results.assert_success() |
| 132 |
|
| 133 |
assert random_bytes in server_results.stdout |
| 134 |
|
| 135 |
assert ( |
| 136 |
to_bytes("Actual protocol version: {}".format(expected_version)) |
| 137 |
in server_results.stdout |
| 138 |
) |
| 139 |
|
| 140 |
assert ( |
| 141 |
to_bytes("Cipher negotiated: {}".format(cipher.name)) |
| 142 |
in server_results.stdout |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.flaky( |
| 147 |
reruns=5, reruns_delay=2, condition=platform.machine().startswith("aarch") |
| 148 |
) |
| 149 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 150 |
@pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name) |
| 151 |
@pytest.mark.parametrize("provider", [OpenSSL]) |
| 152 |
@pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name) |
| 153 |
|
| 154 |
@pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name) |
| 155 |
@pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name) |
| 156 |
@pytest.mark.parametrize("padding_size", PADDING_SIZES, ids=get_parameter_name) |
| 157 |
def test_s2n_client_handles_padded_records( |
| 158 |
managed_process, |
| 159 |
cipher, |
| 160 |
provider, |
| 161 |
curve, |
| 162 |
protocol, |
| 163 |
certificate, |
| 164 |
padding_size, |
| 165 |
): |
| 166 |
port = next(available_ports) |
| 167 |
|
| 168 |
client_random_bytes = data_bytes(PAYLOAD_SIZE) |
| 169 |
server_random_bytes = data_bytes(PAYLOAD_SIZE) |
| 170 |
|
| 171 |
server_options = ProviderOptions( |
| 172 |
mode=Provider.ServerMode, |
| 173 |
port=port, |
| 174 |
cipher=cipher, |
| 175 |
curve=curve, |
| 176 |
cert=certificate.cert, |
| 177 |
key=certificate.key, |
| 178 |
insecure=True, |
| 179 |
protocol=protocol, |
| 180 |
data_to_send=server_random_bytes, |
| 181 |
extra_flags=["-record_padding", padding_size], |
| 182 |
verbose=True, |
| 183 |
) |
| 184 |
|
| 185 |
client_options = copy.copy(server_options) |
| 186 |
client_options.mode = Provider.ClientMode |
| 187 |
client_options.extra_flags = None |
| 188 |
client_options.data_to_send = client_random_bytes |
| 189 |
|
| 190 |
|
| 191 |
openssl = managed_process( |
| 192 |
provider, |
| 193 |
server_options, |
| 194 |
timeout=5, |
| 195 |
send_marker=strip_string_of_bytes(str(client_random_bytes)), |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
s2nc = managed_process( |
| 200 |
S2N, |
| 201 |
client_options, |
| 202 |
timeout=5, |
| 203 |
close_marker=strip_string_of_bytes(str(server_random_bytes)), |
| 204 |
expect_stderr=True, |
| 205 |
) |
| 206 |
|
| 207 |
expected_version = get_expected_s2n_version(protocol, provider) |
| 208 |
for client_results in s2nc.get_results(): |
| 209 |
|
| 210 |
|
| 211 |
client_results.expect_nonzero_exit = True |
| 212 |
client_results.assert_success() |
| 213 |
|
| 214 |
assert server_random_bytes in client_results.stdout |
| 215 |
assert ( |
| 216 |
to_bytes("Actual protocol version: {}".format(expected_version)) |
| 217 |
in client_results.stdout |
| 218 |
) |
| 219 |
assert ( |
| 220 |
to_bytes("Cipher negotiated: {}".format(cipher.name)) |
| 221 |
in client_results.stdout |
| 222 |
) |
| 223 |
|
| 224 |
for server_results in openssl.get_results(): |
| 225 |
server_results.assert_success() |
| 226 |
assert_openssl_records_are_padded_correctly( |
| 227 |
str(server_results.stdout), padding_size |
| 228 |
) |