Debian Codegraph @

crt/s2n/tests/integrationv2/providers.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import os
4 import subprocess
5 import pytest
6 import threading
7
8 from common import (
9 ProviderOptions,
10 Certificates,
11 Ciphers,
12 Curves,
13 Protocols,
14 Signatures,
15 Cert,
16 )
17 from global_flags import get_flag, S2N_PROVIDER_VERSION, S2N_FIPS_MODE
18 from stat import S_IMODE
19
20
21 class Provider(object):
22 """
23 A provider defines a specific provider of TLS. This could be
24 S2N, OpenSSL, BoringSSL, etc.
25 """
26
27 ClientMode = "client"
28 ServerMode = "server"
29
30 def __init__(self, options: ProviderOptions):
31 # If the provider includes stderr output on a success, set this to True.
32 self.expect_stderr = False
33
34 # If the test should wait for a specific output message before beginning,
35 # put that message in ready_to_test_marker
36 self.ready_to_test_marker = None
37
38 # If a newline character should be added to messages being sent. Required
39 # with some providers to properly write to stdin.
40 self.send_with_newline = False
41
42 # By default, we expect clients to send, but not servers.
43 if options.mode == Provider.ClientMode:
44 self.ready_to_send_input_marker = self.get_send_marker()
45 else:
46 self.ready_to_send_input_marker = None
47
48 # Allows users to determine if the provider is ready to begin testing
49 self._provider_ready_condition = threading.Condition()
50 self._provider_ready = False
51
52 if type(options) is not ProviderOptions:
53 raise TypeError
54
55 self.options = options
56 if self.options.mode == Provider.ServerMode:
57 self.cmd_line = self.setup_server() # lgtm [py/init-calls-subclass]
58 elif self.options.mode == Provider.ClientMode:
59 self.cmd_line = self.setup_client() # lgtm [py/init-calls-subclass]
60
61 def setup_client(self):
62 """
63 Provider specific setup code goes here.
64 This will probably include creating the command line based on ProviderOptions.
65 """
66 raise NotImplementedError
67
68 def setup_server(self):
69 """
70 Provider specific setup code goes here.
71 This will probably include creating the command line based on ProviderOptions.
72 """
73 raise NotImplementedError
74
75 @classmethod
76 def get_send_marker(cls):
77 """
78 This should be the last message printed before the client/server can send data.
79 """
80 return None
81
82 @classmethod
83 def supports_protocol(cls, protocol):
84 raise NotImplementedError
85
86 @classmethod
87 def supports_cipher(cls, cipher, with_curve=None):
88 raise NotImplementedError
89
90 @classmethod
91 def supports_signature(cls, signature):
92 return True
93
94 def get_cmd_line(self):
95 return self.cmd_line
96
97 def is_provider_ready(self):
98 return self._provider_ready is True
99
100 def set_provider_ready(self):
101 with self._provider_ready_condition:
102 self._provider_ready = True
103 self._provider_ready_condition.notify()
104
105 @classmethod
106 def supports_certificate(cls, cert: Cert):
107 return True
108
109 @classmethod
110 def get_name(cls, cmd_line):
111 return cmd_line[0]
112
113
114 class S2N(Provider):
115 """
116 The S2N provider translates flags into s2nc/s2nd command line arguments.
117 """
118
119 def __init__(self, options: ProviderOptions):
120 Provider.__init__(self, options)
121
122 self.send_with_newline = True # lgtm [py/overwritten-inherited-attribute]
123
124 @classmethod
125 def get_send_marker(cls):
126 return "s2n is ready"
127
128 @classmethod
129 def _pss_supported(cls):
130 # RSA-PSS is unsupported for openssl-1.0
131 # libressl and boringssl are disabled because of configuration issues
132 # see https://github.com/aws/s2n-tls/issues/3250
133 PSS_UNSUPPORTED_LIBCRYPTOS = {"libressl", "boringssl", "openssl-1.0"}
134 for libcrypto in PSS_UNSUPPORTED_LIBCRYPTOS:
135 # e.g. "openssl-1.0" in "openssl-1.0.2-fips"
136 if libcrypto in get_flag(S2N_PROVIDER_VERSION):
137 return False
138 return True
139
140 @classmethod
141 def supports_certificate(cls, cert: Cert):
142 if not cls._pss_supported() and cert.algorithm == "RSAPSS":
143 return False
144 # https://github.com/aws/s2n-tls/issues/5200
145 if (
146 "openssl-3.0-fips" in get_flag(S2N_PROVIDER_VERSION)
147 and "RSA_1024" in cert.name
148 ):
149 return False
150 return True
151
152 @classmethod
153 def supports_protocol(cls, protocol):
154 if not cls._pss_supported() and protocol == Protocols.TLS13:
155 return False
156
157 # SSLv3 cannot be negotiated in FIPS mode with libcryptos other than AWS-LC.
158 if all(
159 [
160 protocol == Protocols.SSLv3,
161 get_flag(S2N_FIPS_MODE),
162 "awslc" not in get_flag(S2N_PROVIDER_VERSION),
163 ]
164 ):
165 return False
166
167 return True
168
169 @classmethod
170 def supports_cipher(cls, cipher, with_curve=None):
171 # Disable chacha20 and RC4 tests in libcryptos that don't support those
172 # algorithms
173 unsupported_configurations = {
174 "CHACHA20": ["openssl-1.0.2", "libressl"],
175 "RC4": ["openssl-3"],
176 }
177
178 for (
179 unsupported_cipher,
180 unsupported_libcryptos,
181 ) in unsupported_configurations.items():
182 # the queried cipher has some libcrypto's that don't support it
183 # e.g. "RC4" in "TLS_ECDHE_RSA_WITH_RC4_128_SHA"
184 if unsupported_cipher in cipher.name:
185 current_libcrypto = get_flag(S2N_PROVIDER_VERSION)
186 for lc in unsupported_libcryptos:
187 # e.g. "openssl-3" in "openssl-3.0.7"
188 if lc in current_libcrypto:
189 return False
190 return True
191
192 @classmethod
193 def supports_signature(cls, signature):
194 # Disable RSA_PSS_RSAE_SHA256 in unsupported libcryptos
195 if (
196 any(
197 [
198 libcrypto in get_flag(S2N_PROVIDER_VERSION)
199 for libcrypto in ["openssl-1.0.2", "libressl", "boringssl"]
200 ]
201 )
202 and signature == Signatures.RSA_PSS_RSAE_SHA256
203 ):
204 return False
205
206 return True
207
208 def setup_client(self):
209 """
210 Using the passed ProviderOptions, create a command line.
211 """
212 cmd_line = []
213 if self.options.use_mainline_version is True:
214 cmd_line.append("s2nc_head")
215 else:
216 cmd_line.append("s2nc")
217 cmd_line.append("--non-blocking")
218
219 # Tests requiring reconnects can't wait on echo data
220 if self.options.echo and not self.options.reconnect:
221 cmd_line.append("-e")
222
223 if self.options.use_session_ticket is False:
224 cmd_line.append("-T")
225
226 if self.options.insecure is True:
227 cmd_line.append("--insecure")
228 elif self.options.trust_store:
229 cmd_line.extend(["-f", self.options.trust_store])
230 elif self.options.cert:
231 cmd_line.extend(["-f", self.options.cert])
232
233 if self.options.reconnect is True:
234 cmd_line.append("-r")
235
236 # If the test provided a cipher (security policy) that is compatible with
237 # s2n, we'll use it. Otherwise, default to the appropriate `test_all` policy.
238 cipher_prefs = "test_all_tls12"
239 if self.options.protocol is Protocols.TLS13:
240 cipher_prefs = "test_all"
241 if self.options.cipher and self.options.cipher.s2n:
242 cipher_prefs = self.options.cipher.name
243
244 cmd_line.extend(["-c", cipher_prefs])
245
246 if self.options.use_client_auth:
247 if self.options.key:
248 cmd_line.extend(["--key", self.options.key])
249 if self.options.cert:
250 cmd_line.extend(["--cert", self.options.cert])
251
252 if get_flag(S2N_FIPS_MODE):
253 cmd_line.append("--enter-fips-mode")
254
255 if self.options.enable_client_ocsp:
256 cmd_line.extend(["--status"])
257
258 if self.options.extra_flags is not None:
259 cmd_line.extend(self.options.extra_flags)
260
261 cmd_line.extend([self.options.host, self.options.port])
262
263 # Clients are always ready to connect
264 self.set_provider_ready()
265
266 return cmd_line
267
268 def setup_server(self):
269 # s2nd prints this message after it begins listening for connections
270 self.ready_to_test_marker = "Listening on"
271
272 """
273 Using the passed ProviderOptions, create a command line.
274 """
275 cmd_line = []
276 if self.options.use_mainline_version is True:
277 cmd_line.append("s2nd_head")
278 else:
279 cmd_line.append("s2nd")
280 cmd_line.extend(["-X", "--self-service-blinding", "--non-blocking"])
281
282 if self.options.key is not None:
283 cmd_line.extend(["--key", self.options.key])
284 if self.options.cert is not None:
285 cmd_line.extend(["--cert", self.options.cert])
286
287 if self.options.insecure is True:
288 cmd_line.append("--insecure")
289 elif self.options.trust_store:
290 cmd_line.extend(["-t", self.options.trust_store])
291 elif self.options.cert:
292 cmd_line.extend(["-t", self.options.cert])
293
294 # If the test provided a cipher (security policy) that is compatible with
295 # s2n, we'll use it. Otherwise, default to the appropriate `test_all` policy.
296 cipher_prefs = "test_all_tls12"
297 if self.options.protocol is Protocols.TLS13:
298 cipher_prefs = "test_all"
299 if self.options.cipher and self.options.cipher.s2n:
300 cipher_prefs = self.options.cipher.name
301
302 cmd_line.extend(["-c", cipher_prefs])
303
304 if not self.options.echo:
305 cmd_line.append("-n")
306
307 if self.options.use_client_auth is True:
308 cmd_line.append("-m")
309
310 if self.options.use_session_ticket is False:
311 cmd_line.append("-T")
312
313 if self.options.reconnects_before_exit is not None:
314 cmd_line.append(
315 "--max-conns={}".format(self.options.reconnects_before_exit)
316 )
317
318 if get_flag(S2N_FIPS_MODE):
319 cmd_line.append("--enter-fips-mode")
320
321 if self.options.ocsp_response is not None:
322 cmd_line.extend(["--ocsp", self.options.ocsp_response])
323
324 if self.options.extra_flags is not None:
325 cmd_line.extend(self.options.extra_flags)
326
327 cmd_line.extend([self.options.host, self.options.port])
328
329 return cmd_line
330
331
332 class OpenSSL(Provider):
333 result = subprocess.run(
334 ["openssl", "version"], shell=False, capture_output=True, text=True
335 )
336 # After splitting, version_str would be: ["OpenSSL", "3.0.8", "7", "Feb", "2023\n"]
337 version_str = result.stdout.split(" ")
338 # e.g., "OpenSSL"
339 provider = version_str[0]
340 # e.g., "3.0.8"
341 version_openssl = version_str[1]
342
343 def __init__(self, options: ProviderOptions):
344 Provider.__init__(self, options)
345 # We print some OpenSSL logging that includes stderr
346 self.expect_stderr = True # lgtm [py/overwritten-inherited-attribute]
347 # Current provider needs 1.1.x https://github.com/aws/s2n-tls/issues/3963
348 self.at_least_openssl_1_1()
349
350 @classmethod
351 def get_send_marker(cls):
352 return "Verify return code"
353
354 def _join_ciphers(self, ciphers):
355 """
356 Given a list of ciphers, join the names with a ':' like OpenSSL expects
357 """
358 assert type(ciphers) is list
359
360 cipher_list = []
361 for c in ciphers:
362 cipher_list.append(c.name)
363
364 ciphers = ":".join(cipher_list)
365
366 return ciphers
367
368 def _cipher_to_cmdline(self, cipher):
369 cmdline = list()
370
371 ciphers = []
372 if type(cipher) is list:
373 # In the case of a cipher list we need to be sure TLS13 specific ciphers aren't
374 # mixed with ciphers from previous versions
375 is_tls13_or_above = cipher[0].min_version >= Protocols.TLS13
376 mismatch = [
377 c
378 for c in cipher
379 if (c.min_version >= Protocols.TLS13) != is_tls13_or_above
380 ]
381
382 if len(mismatch) > 0:
383 raise Exception(
384 "Cannot combine ciphers for TLS1.3 or above with older ciphers: {}".format(
385 [c.name for c in cipher]
386 )
387 )
388
389 ciphers.append(self._join_ciphers(cipher))
390 else:
391 is_tls13_or_above = cipher.min_version >= Protocols.TLS13
392 ciphers.append(cipher.name)
393
394 if is_tls13_or_above:
395 cmdline.append("-ciphersuites")
396 else:
397 cmdline.append("-cipher")
398
399 return cmdline + ciphers
400
401 @classmethod
402 def get_version(cls):
403 return cls.version_openssl
404
405 @classmethod
406 def get_provider(cls):
407 return cls.provider
408
409 @classmethod
410 def supports_protocol(cls, protocol):
411 if OpenSSL.get_version()[0:3] == "1.1":
412 return protocol not in (Protocols.SSLv3,)
413 elif OpenSSL.get_version()[0:3] == "3.0":
414 return protocol not in (Protocols.SSLv3, Protocols.TLS10, Protocols.TLS11)
415 else:
416 return True
417
418 @classmethod
419 def supports_certificate(cls, cert: Cert):
420 if OpenSSL.get_version()[0:3] >= "3.0":
421 return cert not in (
422 Certificates.RSA_1024_SHA256,
423 Certificates.RSA_1024_SHA384,
424 Certificates.RSA_1024_SHA512,
425 )
426
427 return True
428
429 @classmethod
430 def supports_cipher(cls, cipher, with_curve=None):
431 return True
432
433 def at_least_openssl_1_1(self) -> None:
434 if OpenSSL.get_version() < "1.1":
435 raise FileNotFoundError(
436 f"Openssl version returned {OpenSSL.get_version()}, expected at least 1.1.x."
437 )
438
439 def setup_client(self):
440 cmd_line = ["openssl", "s_client"]
441 cmd_line.extend(
442 ["-connect", "{}:{}".format(self.options.host, self.options.port)]
443 )
444
445 # Additional debugging that will be captured incase of failure
446 if self.options.verbose:
447 cmd_line.append("-debug")
448
449 cmd_line.extend(["-tlsextdebug", "-state"])
450
451 if self.options.key is not None:
452 cmd_line.extend(["-key", self.options.key])
453
454 # Unlike s2n, OpenSSL allows us to be much more specific about which TLS
455 # protocol to use.
456 if self.options.protocol == Protocols.TLS13:
457 cmd_line.append("-tls1_3")
458 elif self.options.protocol == Protocols.TLS12:
459 cmd_line.append("-tls1_2")
460 elif self.options.protocol == Protocols.TLS11:
461 cmd_line.append("-tls1_1")
462 elif self.options.protocol == Protocols.TLS10:
463 cmd_line.append("-tls1")
464 elif self.options.protocol == Protocols.SSLv3:
465 cmd_line.append("-ssl3")
466
467 if self.options.cipher is not None:
468 cmd_line.extend(self._cipher_to_cmdline(self.options.cipher))
469
470 if self.options.curve is not None:
471 cmd_line.extend(["-curves", str(self.options.curve)])
472
473 if self.options.use_client_auth:
474 if self.options.key:
475 cmd_line.extend(["-key", self.options.key])
476 if self.options.cert:
477 cmd_line.extend(["-cert", self.options.cert])
478
479 if self.options.reconnect is True:
480 cmd_line.append("-reconnect")
481
482 if self.options.extra_flags is not None:
483 cmd_line.extend(self.options.extra_flags)
484
485 if self.options.server_name is not None:
486 cmd_line.extend(["-servername", self.options.server_name])
487 if self.options.verify_hostname is not None:
488 cmd_line.extend(["-verify_hostname", self.options.server_name])
489
490 if self.options.enable_client_ocsp:
491 cmd_line.append("-status")
492
493 if self.options.signature_algorithm is not None:
494 cmd_line.extend(["-sigalgs", self.options.signature_algorithm.name])
495
496 if self.options.record_size is not None:
497 cmd_line.extend(["-max_send_frag", str(self.options.record_size)])
498
499 # Clients are always ready to connect
500 self.set_provider_ready()
501
502 return cmd_line
503
504 def setup_server(self):
505 # s_server prints this message before it is ready to send/receive data
506 self.ready_to_test_marker = "ACCEPT"
507
508 cmd_line = ["openssl", "s_server"]
509 cmd_line.extend(["-accept", "{}".format(self.options.port)])
510
511 if self.options.reconnects_before_exit is not None:
512 # If the user request a specific reconnection count, set it here
513 cmd_line.extend(["-naccept", str(self.options.reconnects_before_exit)])
514 else:
515 # Exit after the first connection by default
516 cmd_line.extend(["-naccept", "1"])
517
518 # Additional debugging that will be captured incase of failure
519 if self.options.verbose:
520 cmd_line.append("-debug")
521
522 cmd_line.extend(["-tlsextdebug", "-state"])
523
524 if self.options.cert is not None:
525 cmd_line.extend(["-cert", self.options.cert])
526 if self.options.key is not None:
527 cmd_line.extend(["-key", self.options.key])
528
529 # Unlike s2n, OpenSSL allows us to be much more specific about which TLS
530 # protocol to use.
531 if self.options.protocol == Protocols.TLS13:
532 cmd_line.append("-tls1_3")
533 elif self.options.protocol == Protocols.TLS12:
534 cmd_line.append("-tls1_2")
535 elif self.options.protocol == Protocols.TLS11:
536 cmd_line.append("-tls1_1")
537 elif self.options.protocol == Protocols.TLS10:
538 cmd_line.append("-tls1")
539 elif self.options.protocol == Protocols.SSLv3:
540 cmd_line.append("-ssl3")
541
542 if self.options.cipher is not None:
543 cmd_line.extend(self._cipher_to_cmdline(self.options.cipher))
544 if self.options.cipher.parameters is not None:
545 cmd_line.extend(["-dhparam", self.options.cipher.parameters])
546
547 if self.options.curve is not None:
548 cmd_line.extend(["-curves", str(self.options.curve)])
549 if self.options.use_client_auth is True:
550 # We use "Verify" instead of "verify" to require a client cert
551 cmd_line.extend(["-Verify", "1"])
552
553 if self.options.ocsp_response is not None:
554 cmd_line.extend(["-status_file", self.options.ocsp_response])
555
556 if self.options.signature_algorithm is not None:
557 cmd_line.extend(["-sigalgs", self.options.signature_algorithm.name])
558
559 if self.options.extra_flags is not None:
560 cmd_line.extend(self.options.extra_flags)
561
562 return cmd_line
563
564 @classmethod
565 def get_name(cls, cmd_line):
566 return cmd_line[1]
567
568
569 class SSLv3Provider(OpenSSL):
570 def __init__(self, options: ProviderOptions):
571 OpenSSL.__init__(self, options)
572 self._override_libssl(options)
573
574 def _override_libssl(self, options: ProviderOptions):
575 install_dir = os.environ["OPENSSL_1_0_2_INSTALL_DIR"]
576
577 override_env_vars = dict()
578 override_env_vars["PATH"] = install_dir + "/bin"
579 override_env_vars["LD_LIBRARY_PATH"] = install_dir + "/lib"
580 options.env_overrides = override_env_vars
581
582 @classmethod
583 def supports_protocol(cls, protocol):
584 if protocol is Protocols.SSLv3:
585 return True
586 return False
587
588
589 class JavaSSL(Provider):
590 """
591 NOTE: Only a Java SSL client has been set up. The server has not been
592 implemented yet.
593 """
594
595 def __init__(self, options: ProviderOptions):
596 Provider.__init__(self, options)
597
598 @classmethod
599 def get_send_marker(cls):
600 return "Starting handshake"
601
602 @classmethod
603 def supports_protocol(cls, protocol):
604 # https://aws.amazon.com/blogs/opensource/tls-1-0-1-1-changes-in-openjdk-and-amazon-corretto/
605 if (
606 protocol is Protocols.SSLv3
607 or protocol is Protocols.TLS10
608 or protocol is Protocols.TLS11
609 ):
610 return False
611
612 return True
613
614 @classmethod
615 def supports_cipher(cls, cipher, with_curve=None):
616 # Java SSL does not support CHACHA20
617 if "CHACHA20" in cipher.name:
618 return False
619
620 return True
621
622 def setup_server(self):
623 pytest.skip("JavaSSL does not support server mode at this time")
624
625 def setup_client(self):
626 cmd_line = ["java", "-classpath", "bin", "SSLSocketClient"]
627
628 if self.options.port is not None:
629 cmd_line.extend([self.options.port])
630
631 if self.options.trust_store:
632 cmd_line.extend([self.options.trust_store])
633 elif self.options.cert:
634 cmd_line.extend([self.options.cert])
635 if self.options.cipher.iana_standard_name is not None:
636 cmd_line.extend([self.options.cipher.iana_standard_name])
637
638 if self.options.protocol is not None:
639 cmd_line.extend([self.options.protocol.name])
640 # SSLv2ClientHello is a "protocol" for Java TLS, so we append it next to
641 # the existing protocol.
642 if self.options.extra_flags is not None:
643 cmd_line.extend(self.options.extra_flags)
644
645 # Clients are always ready to connect
646 self.set_provider_ready()
647
648 return cmd_line
649
650
651 class BoringSSL(Provider):
652 """
653 NOTE: In order to focus on the general use of this framework, BoringSSL
654 is not yet supported. The client works, the server has not yet been
655 implemented, neither are in the default configuration.
656 """
657
658 def __init__(self, options: ProviderOptions):
659 Provider.__init__(self, options)
660
661 @classmethod
662 def get_send_marker(cls):
663 return "Cert issuer:"
664
665 def setup_server(self):
666 cmd_line = ["bssl", "s_server"]
667 cmd_line.extend(["-accept", self.options.port])
668 if self.options.cert is not None:
669 cmd_line.extend(["-cert", self.options.cert])
670 if self.options.key is not None:
671 cmd_line.extend(["-key", self.options.key])
672 if self.options.curve is not None:
673 if self.options.curve == Curves.P256:
674 cmd_line.extend(["-curves", "P-256"])
675 elif self.options.curve == Curves.P384:
676 cmd_line.extend(["-curves", "P-384"])
677 elif self.options.curve == Curves.P521:
678 cmd_line.extend(["-curves", "P-521"])
679 elif self.options.curve == Curves.X25519:
680 pytest.skip(
681 "BoringSSL does not support curve {}".format(self.options.curve)
682 )
683
684 if self.options.extra_flags is not None:
685 cmd_line.extend(self.options.extra_flags)
686
687 return cmd_line
688
689 def setup_client(self):
690 cmd_line = ["bssl", "s_client"]
691 cmd_line.extend(
692 ["-connect", "{}:{}".format(self.options.host, self.options.port)]
693 )
694 if self.options.cert is not None:
695 cmd_line.extend(["-cert", self.options.cert])
696 if self.options.key is not None:
697 cmd_line.extend(["-key", self.options.key])
698 if self.options.cipher is not None:
699 if self.options.cipher == Ciphers.TLS_CHACHA20_POLY1305_SHA256:
700 cmd_line.extend(
701 ["-cipher", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"]
702 )
703 if self.options.curve is not None:
704 if self.options.curve == Curves.P256:
705 cmd_line.extend(["-curves", "P-256"])
706 elif self.options.curve == Curves.P384:
707 cmd_line.extend(["-curves", "P-384"])
708 elif self.options.curve == Curves.P521:
709 cmd_line.extend(["-curves", "P-521"])
710 elif self.options.curve == Curves.X25519:
711 pytest.skip(
712 "BoringSSL does not support curve {}".format(self.options.curve)
713 )
714
715 if self.options.extra_flags is not None:
716 cmd_line.extend(self.options.extra_flags)
717
718 # Clients are always ready to connect
719 self.set_provider_ready()
720
721 return cmd_line
722
723
724 class GnuTLS(Provider):
725 def __init__(self, options: ProviderOptions):
726 Provider.__init__(self, options)
727
728 self.expect_stderr = True # lgtm [py/overwritten-inherited-attribute]
729 self.send_with_newline = True # lgtm [py/overwritten-inherited-attribute]
730
731 @staticmethod
732 def cipher_to_priority_str(cipher):
733 return {
734 Ciphers.DHE_RSA_AES128_SHA: "DHE-RSA:+AES-128-CBC:+SHA1",
735 Ciphers.DHE_RSA_AES256_SHA: "DHE-RSA:+AES-256-CBC:+SHA1",
736 Ciphers.DHE_RSA_AES128_SHA256: "DHE-RSA:+AES-128-CBC:+SHA256",
737 Ciphers.DHE_RSA_AES256_SHA256: "DHE-RSA:+AES-256-CBC:+SHA256",
738 Ciphers.DHE_RSA_AES128_GCM_SHA256: "DHE-RSA:+AES-128-GCM:+AEAD",
739 Ciphers.DHE_RSA_AES256_GCM_SHA384: "DHE-RSA:+AES-256-GCM:+AEAD",
740 Ciphers.DHE_RSA_CHACHA20_POLY1305: "DHE-RSA:+CHACHA20-POLY1305:+AEAD",
741 Ciphers.AES128_SHA: "RSA:+AES-128-CBC:+SHA1",
742 Ciphers.AES256_SHA: "RSA:+AES-256-CBC:+SHA1",
743 Ciphers.AES128_SHA256: "RSA:+AES-128-CBC:+SHA256",
744 Ciphers.AES256_SHA256: "RSA:+AES-256-CBC:+SHA256",
745 Ciphers.AES128_GCM_SHA256: "RSA:+AES-128-GCM:+AEAD",
746 Ciphers.AES256_GCM_SHA384: "RSA:+AES-256-GCM:+AEAD",
747 Ciphers.ECDHE_ECDSA_AES128_SHA: "ECDHE-ECDSA:+AES-128-CBC:+SHA1",
748 Ciphers.ECDHE_ECDSA_AES256_SHA: "ECDHE-ECDSA:+AES-256-CBC:+SHA1",
749 Ciphers.ECDHE_ECDSA_AES128_SHA256: "ECDHE-ECDSA:+AES-128-CBC:+SHA256",
750 Ciphers.ECDHE_ECDSA_AES256_SHA384: "ECDHE-ECDSA:+AES-256-CBC:+SHA384",
751 Ciphers.ECDHE_ECDSA_AES128_GCM_SHA256: "ECDHE-ECDSA:+AES-128-GCM:+AEAD",
752 Ciphers.ECDHE_ECDSA_AES256_GCM_SHA384: "ECDHE-ECDSA:+AES-256-GCM:+AEAD",
753 Ciphers.ECDHE_RSA_AES128_SHA: "ECDHE-RSA:+AES-128-CBC:+SHA1",
754 Ciphers.ECDHE_RSA_AES256_SHA: "ECDHE-RSA:+AES-256-CBC:+SHA1",
755 Ciphers.ECDHE_RSA_AES128_SHA256: "ECDHE-RSA:+AES-128-CBC:+SHA256",
756 Ciphers.ECDHE_RSA_AES256_SHA384: "ECDHE-RSA:+AES-256-CBC:+SHA384",
757 Ciphers.ECDHE_RSA_AES128_GCM_SHA256: "ECDHE-RSA:+AES-128-GCM:+AEAD",
758 Ciphers.ECDHE_RSA_AES256_GCM_SHA384: "ECDHE-RSA:+AES-256-GCM:+AEAD",
759 Ciphers.ECDHE_RSA_CHACHA20_POLY1305: "ECDHE-RSA:+CHACHA20-POLY1305:+AEAD",
760 }.get(cipher)
761
762 @staticmethod
763 def protocol_to_priority_str(protocol):
764 if not protocol:
765 return None
766 return {
767 Protocols.TLS10.value: "VERS-TLS1.0",
768 Protocols.TLS11.value: "VERS-TLS1.1",
769 Protocols.TLS12.value: "VERS-TLS1.2",
770 Protocols.TLS13.value: "VERS-TLS1.3",
771 }.get(protocol.value)
772
773 @staticmethod
774 def curve_to_priority_str(curve):
775 return {
776 Curves.P256: "CURVE-SECP256R1",
777 Curves.P384: "CURVE-SECP384R1",
778 Curves.P521: "CURVE-SECP521R1",
779 Curves.X25519: "CURVE-X25519",
780 }.get(curve)
781
782 @staticmethod
783 def sigalg_to_priority_str(sigalg):
784 return {
785 Signatures.RSA_SHA1: "SIGN-RSA-SHA1",
786 Signatures.RSA_SHA256: "SIGN-RSA-SHA256",
787 Signatures.RSA_SHA384: "SIGN-RSA-SHA384",
788 Signatures.RSA_SHA512: "SIGN-RSA-SHA512",
789 }.get(sigalg)
790
791 @classmethod
792 def get_send_marker(cls):
793 return "Simple Client Mode:"
794
795 def create_priority_str(self):
796 priority_str = "NONE"
797
798 protocol_to_priority_str = self.protocol_to_priority_str(self.options.protocol)
799 if protocol_to_priority_str:
800 priority_str += ":+" + protocol_to_priority_str
801 else:
802 priority_str += ":+VERS-ALL"
803
804 cipher_to_priority_str = self.cipher_to_priority_str(self.options.cipher)
805 if cipher_to_priority_str:
806 priority_str += ":+" + cipher_to_priority_str
807 else:
808 priority_str += ":+KX-ALL:+CIPHER-ALL:+MAC-ALL"
809
810 curve_to_priority_str = self.curve_to_priority_str(self.options.curve)
811 if curve_to_priority_str:
812 priority_str += ":+" + curve_to_priority_str
813 else:
814 priority_str += ":+GROUP-ALL"
815
816 sigalg_to_priority_str = self.sigalg_to_priority_str(
817 self.options.signature_algorithm
818 )
819 if sigalg_to_priority_str:
820 priority_str += ":+" + sigalg_to_priority_str
821 else:
822 priority_str += ":+SIGN-ALL"
823
824 priority_str += ":+COMP-NULL"
825
826 # A digital signature option is not included for the test RSA certs, so GnuTLS must be
827 # told to use these certs regardless. The %COMPAT priority string option enables this for
828 # client certificates, and the undocumented %DEBUG_ALLOW_KEY_USAGE_VIOLATIONS priority
829 # string option enables this for server certificates.
830 priority_str += ":%COMPAT"
831 priority_str += ":%DEBUG_ALLOW_KEY_USAGE_VIOLATIONS"
832
833 return priority_str
834
835 def setup_client(self):
836 self.set_provider_ready()
837
838 cmd_line = [
839 "gnutls-cli",
840 "--port",
841 str(self.options.port),
842 self.options.host,
843 ]
844
845 # Most GnuTLS tests expect verbose output, so default to True.
846 if self.options.verbose is not False:
847 cmd_line.append("--verbose")
848
849 if self.options.cert and self.options.key:
850 cmd_line.extend(["--x509certfile", self.options.cert])
851 cmd_line.extend(["--x509keyfile", self.options.key])
852
853 priority_str = self.create_priority_str()
854 cmd_line.extend(["--priority", priority_str])
855
856 if self.options.insecure:
857 cmd_line.extend(["--insecure"])
858
859 if self.options.enable_client_ocsp:
860 cmd_line.append("--ocsp")
861
862 if self.options.record_size:
863 cmd_line.extend(["--recordsize", str(self.options.record_size)])
864
865 if self.options.extra_flags:
866 cmd_line.extend(self.options.extra_flags)
867
868 return cmd_line
869
870 def setup_server(self):
871 self.ready_to_test_marker = "Echo Server listening on"
872
873 cmd_line = [
874 "gnutls-serv",
875 f"--port={self.options.port}",
876 "--echo",
877 ]
878
879 if self.options.cert is not None:
880 cmd_line.extend(["--x509certfile", self.options.cert])
881 if self.options.key is not None:
882 cmd_line.extend(["--x509keyfile", self.options.key])
883
884 priority_str = self.create_priority_str()
885 cmd_line.extend(["--priority", priority_str])
886
887 if self.options.cipher:
888 if self.options.cipher.parameters:
889 cmd_line.extend(["--dhparams", self.options.cipher.parameters])
890
891 if self.options.ocsp_response:
892 cmd_line.extend(["--ocsp-response", self.options.ocsp_response])
893
894 if self.options.use_client_auth:
895 cmd_line.append("--require-client-cert")
896
897 if self.options.extra_flags:
898 cmd_line.extend(self.options.extra_flags)
899
900 return cmd_line
901
902 @classmethod
903 def supports_protocol(cls, protocol):
904 return GnuTLS.protocol_to_priority_str(protocol) is not None
905
906 @classmethod
907 def supports_cipher(cls, cipher, with_curve=None):
908 return GnuTLS.cipher_to_priority_str(cipher) is not None
909
910 @classmethod
911 def supports_signature(cls, signature):
912 return GnuTLS.sigalg_to_priority_str(signature) is not None
913
914
915 def find_files(file_glob, root_dir=".", modes=[]):
916 """
917 find util in python form.
918 file_glob: a snippet of the filename, e.g. ".py"
919 root_dir: starting point for search
920 mode is an octal representation of owner/group/other, e.g.: '0o644'
921 """
922 result = []
923 for root, dirs, files in os.walk(root_dir):
924 for file in files:
925 if file_glob in file:
926 full_name = os.path.abspath(os.path.join(root, file))
927 if len(modes) != 0:
928 try:
929 stat = oct(S_IMODE(os.stat(full_name).st_mode))
930 if stat in modes:
931 result.append(full_name)
932 except FileNotFoundError:
933 # symlinks
934 pass
935 else:
936 result.append(full_name)
937
938 return result