Debian Codegraph @

crt/s2n/tests/integrationv2/test_record_padding.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
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 # noqa: F401
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 # arbitrarily large payload size
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 # s has the form `b'<>'`. We need to strip the literal `b'` and the last `'`
40 return s[2:-1]
41
42
43 def get_payload_size_from_openssl_trace(record_size_bytes: str) -> int:
44 # record_size_bytes is in the form XX XX where X is a hex digit
45 size_in_hex = record_size_bytes.replace(" ", "")
46 size = int(size_in_hex, 16)
47 # record includes 16 bytes of aead tag
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 # The client and server write a variable number of encrypted handshake records,
67 # but each write at least one (Finished). We also send at least one ApplicationData record.
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 # only tls 1.3 supports record padding
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, # noqa: F811
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 # verify that the payload was correctly received by the server
133 assert random_bytes in server_results.stdout
134 # verify that the version was correctly negotiated
135 assert (
136 to_bytes("Actual protocol version: {}".format(expected_version))
137 in server_results.stdout
138 )
139 # verify that the cipher was correctly negotiated
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 # only tls 1.3 supports record padding
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, # noqa: F811
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 # openssl will send its response after it has received s2nc's record
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 # s2nc will wait until it has received the server's response before closing
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 # Aware of I/O issues causing this testcase to sometimes fail with a non zero exit status
210 # https://github.com/aws/s2n-tls/issues/5130
211 client_results.expect_nonzero_exit = True
212 client_results.assert_success()
213 # assert that the client has received server's application payload
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 )