Debian Codegraph @

crt/s2n/tests/integrationv2/test_external_psk.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import pytest
4
5 from configuration import (
6 available_ports,
7 TLS13_CIPHERS,
8 ALL_TEST_CURVES,
9 MINIMAL_TEST_CERTS,
10 )
11 from common import ProviderOptions, Protocols, data_bytes
12 from fixtures import managed_process # noqa: F401
13 from providers import S2N, OpenSSL
14 from utils import invalid_test_parameters, get_parameter_name, to_bytes
15 from enum import Enum, auto
16
17 # Known value test vectors from https://tools.ietf.org/html/rfc8448#section-4
18 known_psk_identity = (
19 "2c035d829359ee5ff7af4ec900000000262a6494dc486d2c8a34cb33fa90bf1b00"
20 "70ad3c498883c9367c09a2be785abc55cd226097a3a982117283f82a03a143efd3"
21 "ff5dd36d64e861be7fd61d2827db279cce145077d454a3664d4e6da4d29ee03725"
22 "a6a4dafcd0fc67d2aea70529513e3da2677fa5906c5b3f7d8f92f228bda40dda72"
23 "1470f9fbf297b5aea617646fac5c03272e970727c621a79141ef5f7de6505e5bfb"
24 "c388e93343694093934ae4d357"
25 )
26 known_psk_secret = "4ecd0eb6ec3b4d87f5d6028f922ca4c5851a277fd41311c9e62d2c9492e1c4f3"
27
28 # Arbitrary test vectors
29 PSK_IDENTITY_LIST = [known_psk_identity, "psk_identity", "test_psk_identity"]
30 PSK_SECRET_LIST = [known_psk_secret, "a6dadae4567876", "a64dafcd0fc67d2a"]
31 PSK_IDENTITY_NO_MATCH = "PSK_IDENTITY_NO_MATCH"
32 PSK_SECRET_NO_MATCH = "e9492e1c"
33 PSK_IDENTITY_NO_MATCH_2 = "PSK_IDENTITY_NO_MATCH_2"
34 PSK_SECRET_NO_MATCH_2 = "c1e29493fd"
35
36 MINIMAL_TEST_CERTS_WITH_EMPTY_CERT = MINIMAL_TEST_CERTS + [None]
37 PSK_PROVIDERS = [OpenSSL, S2N]
38
39
40 class Outcome(Enum):
41 psk_connection = auto()
42 full_handshake = auto()
43 handshake_failed = auto()
44
45
46 def setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg):
47 return ["--psk", psk_identity + "," + psk_secret + "," + psk_hash_alg]
48
49
50 def setup_openssl_psk_params(psk_identity, psk_secret):
51 return ["-psk_identity", psk_identity, "--psk", psk_secret]
52
53
54 def setup_provider_options(
55 mode, port, cipher, curve, certificate, data_to_send, client_psk_params
56 ):
57 options = ProviderOptions(
58 host="localhost",
59 port=port,
60 cipher=cipher,
61 curve=curve,
62 insecure=True,
63 protocol=Protocols.TLS13,
64 data_to_send=data_to_send,
65 mode=mode,
66 extra_flags=client_psk_params,
67 )
68 if certificate:
69 options.key = certificate.key
70 options.cert = certificate.cert
71 options.trust_store = certificate.cert
72 return options
73
74
75 def get_psk_hash_alg_from_cipher(cipher):
76 # S2N supports only SHA256 and SHA384 PSK Hash Algorithms
77 if "SHA256" in cipher.name:
78 return "SHA256"
79 elif "SHA384" in cipher.name:
80 return "SHA384"
81 else:
82 return None
83
84
85 def skip_invalid_psk_tests(provider, psk_hash_alg):
86 # If the PSK hash algorithm is None, it is not supported and we can safely skip the test case.
87 if psk_hash_alg is None:
88 pytest.skip()
89
90 # In OpenSSL, PSK works only with TLS1.3 ciphersuites based on SHA256 hash algorithm which includes
91 # all TLS1.3 ciphersuites supported by S2N except TLS_AES_256_GCM_SHA384.
92 if provider == OpenSSL and psk_hash_alg == "SHA384":
93 pytest.skip()
94
95
96 def validate_negotiated_psk_s2n(outcome, psk_identity, results):
97 if outcome == Outcome.psk_connection:
98 assert (
99 to_bytes("Negotiated PSK identity: {}".format(psk_identity))
100 in results.stdout
101 )
102 elif outcome == Outcome.full_handshake:
103 assert (
104 to_bytes("Negotiated PSK identity: {}".format(psk_identity))
105 not in results.stdout
106 )
107 else:
108 assert results.exit_code != 0
109 assert to_bytes("Failed to negotiate: 'TLS alert received'") in results.stderr
110
111
112 def validate_negotiated_psk_openssl(outcome, results):
113 if outcome == Outcome.psk_connection:
114 assert to_bytes('extension "psk"') in results.stdout
115 elif outcome == Outcome.full_handshake:
116 assert (
117 to_bytes("SSL_connect:SSLv3/TLS read server certificate") in results.stderr
118 )
119 else:
120 assert to_bytes("SSL_accept:error in error") in results.stderr
121
122
123 def test_nothing():
124 """
125 Sometimes the external psk test parameters in combination with the s2n libcrypto
126 results in no test cases existing. In this case, pass a nothing test to avoid
127 marking the entire codebuild run as failed.
128 """
129 assert True
130
131
132 """
133 Basic S2N server happy case.
134
135 Tests a single psk connection with no fallback option.
136 """
137
138
139 @pytest.mark.uncollect_if(func=invalid_test_parameters)
140 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
141 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
142 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
143 @pytest.mark.parametrize("provider", PSK_PROVIDERS, ids=get_parameter_name)
144 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
145 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
146 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
147 def test_s2n_server_psk_connection(
148 managed_process, # noqa: F811
149 cipher,
150 curve,
151 protocol,
152 provider,
153 other_provider,
154 psk_identity,
155 psk_secret,
156 ):
157 port = next(available_ports)
158 random_bytes = data_bytes(10)
159 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
160 skip_invalid_psk_tests(provider, psk_hash_alg)
161
162 if provider == S2N:
163 client_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
164 else:
165 client_psk_params = setup_openssl_psk_params(psk_identity, psk_secret)
166 client_options = setup_provider_options(
167 provider.ClientMode, port, cipher, curve, None, random_bytes, client_psk_params
168 )
169
170 server_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
171 server_options = setup_provider_options(
172 S2N.ServerMode, port, cipher, curve, None, None, server_psk_params
173 )
174
175 server = managed_process(
176 S2N, server_options, timeout=5, close_marker=str(random_bytes)
177 )
178 client = managed_process(provider, client_options, timeout=5)
179
180 for results in client.get_results():
181 results.assert_success()
182 if provider == S2N:
183 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
184 else:
185 validate_negotiated_psk_openssl(Outcome.psk_connection, results)
186
187 for results in server.get_results():
188 results.assert_success()
189 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
190 assert random_bytes in results.stdout
191
192
193 """
194 Tests S2N server's behavior with multiple PSKs and no fallback options.
195
196 Note that OpenSSL does not support multiple PSKs.
197 """
198
199
200 @pytest.mark.uncollect_if(func=invalid_test_parameters)
201 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
202 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
203 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
204 @pytest.mark.parametrize("provider", PSK_PROVIDERS, ids=get_parameter_name)
205 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
206 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
207 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
208 def test_s2n_server_multiple_psks(
209 managed_process, # noqa: F811
210 cipher,
211 curve,
212 protocol,
213 provider,
214 other_provider,
215 psk_identity,
216 psk_secret,
217 ):
218 port = next(available_ports)
219 random_bytes = data_bytes(10)
220 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
221 skip_invalid_psk_tests(provider, psk_hash_alg)
222
223 client_psk_params = []
224 if provider == OpenSSL:
225 """
226 OpenSSL Provider does not support multiple PSKs in the same connection,
227 the last psk parameter is the psk parameter used in the connection.
228 """
229 client_psk_params.extend(
230 setup_openssl_psk_params(PSK_IDENTITY_NO_MATCH, PSK_SECRET_NO_MATCH)
231 )
232 client_psk_params.extend(setup_openssl_psk_params(psk_identity, psk_secret))
233 else:
234 client_psk_params.extend(
235 setup_s2n_psk_params(
236 PSK_IDENTITY_NO_MATCH, PSK_SECRET_NO_MATCH, psk_hash_alg
237 )
238 )
239 client_psk_params.extend(
240 setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
241 )
242 client_options = setup_provider_options(
243 provider.ClientMode, port, cipher, curve, None, random_bytes, client_psk_params
244 )
245
246 server_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
247 server_psk_params.extend(
248 setup_s2n_psk_params(
249 PSK_IDENTITY_NO_MATCH_2, PSK_SECRET_NO_MATCH_2, psk_hash_alg
250 )
251 )
252 server_options = setup_provider_options(
253 S2N.ServerMode, port, cipher, curve, None, None, server_psk_params
254 )
255
256 server = managed_process(
257 S2N, server_options, timeout=5, close_marker=str(random_bytes)
258 )
259 client = managed_process(provider, client_options, timeout=5)
260
261 for results in client.get_results():
262 results.assert_success()
263 if provider == S2N:
264 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
265 else:
266 validate_negotiated_psk_openssl(Outcome.psk_connection, results)
267
268 for results in server.get_results():
269 results.assert_success()
270 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
271 assert random_bytes in results.stdout
272
273
274 """
275 Tests S2N Server's fallback to full handshake.
276
277 Verify S2N Server's fallback behavior with an invalid PSK parameter and a valid certificate as the input.
278
279 Note that S2N Server succeeds with a full handshake when an invalid PSK parameter and an empty
280 certificate is provided as the input, as S2N Server uses a default certificate if a certificate is not provided
281 as the input.
282 """
283
284
285 @pytest.mark.uncollect_if(func=invalid_test_parameters)
286 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
287 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
288 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
289 @pytest.mark.parametrize("provider", PSK_PROVIDERS, ids=get_parameter_name)
290 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
291 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
292 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
293 @pytest.mark.parametrize(
294 "certificate", MINIMAL_TEST_CERTS_WITH_EMPTY_CERT, ids=get_parameter_name
295 )
296 def test_s2n_server_full_handshake(
297 managed_process, # noqa: F811
298 cipher,
299 curve,
300 protocol,
301 provider,
302 other_provider,
303 psk_identity,
304 psk_secret,
305 certificate,
306 ):
307 port = next(available_ports)
308 random_bytes = data_bytes(10)
309 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
310 skip_invalid_psk_tests(provider, psk_hash_alg)
311
312 if provider == S2N:
313 client_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
314 else:
315 client_psk_params = setup_openssl_psk_params(psk_identity, psk_secret)
316 client_options = setup_provider_options(
317 provider.ClientMode,
318 port,
319 cipher,
320 curve,
321 certificate,
322 random_bytes,
323 client_psk_params,
324 )
325
326 server_psk_params = setup_s2n_psk_params(
327 PSK_IDENTITY_NO_MATCH, PSK_SECRET_NO_MATCH, psk_hash_alg
328 )
329 server_options = setup_provider_options(
330 S2N.ServerMode, port, cipher, curve, certificate, None, server_psk_params
331 )
332
333 server = managed_process(
334 S2N, server_options, timeout=5, close_marker=str(random_bytes)
335 )
336 client = managed_process(provider, client_options, timeout=5)
337
338 for results in client.get_results():
339 results.assert_success()
340 if provider == S2N:
341 validate_negotiated_psk_s2n(Outcome.full_handshake, psk_identity, results)
342 else:
343 validate_negotiated_psk_openssl(Outcome.full_handshake, results)
344
345 for results in server.get_results():
346 results.assert_success()
347 validate_negotiated_psk_s2n(
348 Outcome.full_handshake, PSK_IDENTITY_NO_MATCH, results
349 )
350 assert random_bytes in results.stdout
351
352
353 """
354 Basic S2N client happy case.
355
356 Tests a single psk connection with no fallback option.
357 """
358
359
360 @pytest.mark.uncollect_if(func=invalid_test_parameters)
361 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
362 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
363 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
364 @pytest.mark.parametrize("provider", PSK_PROVIDERS, ids=get_parameter_name)
365 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
366 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
367 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
368 def test_s2n_client_psk_connection(
369 managed_process, # noqa: F811
370 cipher,
371 curve,
372 protocol,
373 provider,
374 other_provider,
375 psk_identity,
376 psk_secret,
377 ):
378 port = next(available_ports)
379 random_bytes = data_bytes(10)
380 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
381 skip_invalid_psk_tests(provider, psk_hash_alg)
382
383 client_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
384 client_options = setup_provider_options(
385 S2N.ClientMode, port, cipher, curve, None, random_bytes, client_psk_params
386 )
387
388 if provider == S2N:
389 server_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
390 else:
391 server_psk_params = setup_openssl_psk_params(psk_identity, psk_secret)
392 server_psk_params += ["-nocert"]
393 server_options = setup_provider_options(
394 provider.ServerMode, port, cipher, curve, None, None, server_psk_params
395 )
396
397 server = managed_process(
398 provider, server_options, timeout=5, close_marker=str(random_bytes)
399 )
400 client = managed_process(S2N, client_options, timeout=5)
401
402 for results in client.get_results():
403 results.assert_success()
404 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
405
406 for results in server.get_results():
407 results.assert_success()
408 if provider == S2N:
409 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
410 else:
411 validate_negotiated_psk_openssl(Outcome.psk_connection, results)
412 assert random_bytes in results.stdout
413
414
415 """
416 Tests S2N client's behavior with multiple PSKs and no fallback option.
417
418 Note that OpenSSL does not support multiple PSKs.
419 """
420
421
422 @pytest.mark.uncollect_if(func=invalid_test_parameters)
423 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
424 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
425 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
426 @pytest.mark.parametrize("provider", PSK_PROVIDERS, ids=get_parameter_name)
427 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
428 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
429 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
430 def test_s2n_client_multiple_psks(
431 managed_process, # noqa: F811
432 cipher,
433 curve,
434 protocol,
435 provider,
436 other_provider,
437 psk_identity,
438 psk_secret,
439 ):
440 port = next(available_ports)
441 random_bytes = data_bytes(10)
442 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
443 skip_invalid_psk_tests(provider, psk_hash_alg)
444
445 client_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
446 client_psk_params.extend(
447 setup_s2n_psk_params(PSK_IDENTITY_NO_MATCH, PSK_SECRET_NO_MATCH, psk_hash_alg)
448 )
449 client_options = setup_provider_options(
450 S2N.ClientMode, port, cipher, curve, None, random_bytes, client_psk_params
451 )
452
453 server_psk_params = []
454 if provider == OpenSSL:
455 """
456 OpenSSL Provider does not support multiple PSKs in the same connection,
457 the last psk params is the final psk used in the connection.
458 """
459 server_psk_params.extend(
460 setup_openssl_psk_params(PSK_IDENTITY_NO_MATCH_2, PSK_SECRET_NO_MATCH_2)
461 )
462 server_psk_params.extend(setup_openssl_psk_params(psk_identity, psk_secret))
463 server_psk_params += ["-nocert"]
464 else:
465 server_psk_params.extend(
466 setup_s2n_psk_params(
467 PSK_IDENTITY_NO_MATCH_2, PSK_SECRET_NO_MATCH_2, psk_hash_alg
468 )
469 )
470 server_psk_params.extend(
471 setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
472 )
473 server_options = setup_provider_options(
474 provider.ServerMode, port, cipher, curve, None, None, server_psk_params
475 )
476
477 server = managed_process(
478 provider, server_options, timeout=5, close_marker=str(random_bytes)
479 )
480 client = managed_process(S2N, client_options, timeout=5)
481
482 for results in client.get_results():
483 results.assert_success()
484 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
485
486 for results in server.get_results():
487 results.assert_success()
488 if provider == S2N:
489 validate_negotiated_psk_s2n(Outcome.psk_connection, psk_identity, results)
490 else:
491 validate_negotiated_psk_openssl(Outcome.psk_connection, results)
492 assert random_bytes in results.stdout
493
494
495 """
496 S2N Client fails to succeed with a handshake when an invalid PSK parameter
497 and an invalid certificate is provided as the input.
498
499 Note that we cannot use S2N Server as a provider input for this test as S2N Server
500 uses a default certificate if a certificate is not provided as the input.
501 """
502
503
504 @pytest.mark.uncollect_if(func=invalid_test_parameters)
505 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
506 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
507 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
508 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
509 @pytest.mark.parametrize("psk_identity", PSK_IDENTITY_LIST, ids=get_parameter_name)
510 @pytest.mark.parametrize("psk_secret", PSK_SECRET_LIST, ids=get_parameter_name)
511 def test_s2n_client_psk_handshake_failure(
512 managed_process, # noqa: F811
513 cipher,
514 curve,
515 protocol,
516 provider,
517 psk_identity,
518 psk_secret,
519 ):
520 port = next(available_ports)
521 random_bytes = data_bytes(10)
522 psk_hash_alg = get_psk_hash_alg_from_cipher(cipher)
523 skip_invalid_psk_tests(provider, psk_hash_alg)
524
525 client_psk_params = setup_s2n_psk_params(psk_identity, psk_secret, psk_hash_alg)
526 client_options = setup_provider_options(
527 S2N.ClientMode, port, cipher, curve, None, random_bytes, client_psk_params
528 )
529
530 server_psk_params = setup_openssl_psk_params(
531 PSK_IDENTITY_NO_MATCH, PSK_SECRET_NO_MATCH
532 )
533 server_psk_params += ["-nocert"]
534 server_options = setup_provider_options(
535 provider.ServerMode, port, cipher, curve, None, None, server_psk_params
536 )
537
538 server = managed_process(
539 provider, server_options, timeout=5, close_marker=str(random_bytes)
540 )
541 client = managed_process(S2N, client_options, timeout=5)
542
543 for results in client.get_results():
544 assert to_bytes("Failed to negotiate: 'TLS alert received'") in results.stderr
545 validate_negotiated_psk_s2n(Outcome.handshake_failed, psk_identity, results)
546
547 for results in server.get_results():
548 assert to_bytes("SSL_accept:error in error") in results.stderr
549 validate_negotiated_psk_openssl(Outcome.handshake_failed, results)
550 assert random_bytes not in results.stdout