| 1 |
|
| 2 |
|
| 3 |
import copy |
| 4 |
import pytest |
| 5 |
|
| 6 |
from configuration import available_ports, ALL_TEST_CIPHERS, PROTOCOLS |
| 7 |
from common import Certificates, ProviderOptions, Protocols, data_bytes, Signatures |
| 8 |
from fixtures import managed_process |
| 9 |
from providers import Provider, S2N, GnuTLS, OpenSSL |
| 10 |
from utils import ( |
| 11 |
invalid_test_parameters, |
| 12 |
get_parameter_name, |
| 13 |
get_expected_s2n_version, |
| 14 |
to_bytes, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
CERTS_TO_TEST = [ |
| 20 |
Certificates.RSA_1024_SHA256, |
| 21 |
Certificates.RSA_4096_SHA512, |
| 22 |
Certificates.ECDSA_256, |
| 23 |
Certificates.ECDSA_384, |
| 24 |
Certificates.RSA_PSS_2048_SHA256, |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
def assert_openssl_handshake_complete(results, is_complete=True): |
| 29 |
if is_complete: |
| 30 |
assert b"read finished" in results.stderr |
| 31 |
assert b"write finished" in results.stderr |
| 32 |
else: |
| 33 |
assert ( |
| 34 |
b"read finished" not in results.stderr |
| 35 |
or b"write finished" not in results.stderr |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def assert_s2n_handshake_complete(results, protocol, provider, is_complete=True): |
| 40 |
expected_version = get_expected_s2n_version(protocol, provider) |
| 41 |
if is_complete: |
| 42 |
assert ( |
| 43 |
to_bytes("Actual protocol version: {}".format(expected_version)) |
| 44 |
in results.stdout |
| 45 |
) |
| 46 |
else: |
| 47 |
assert ( |
| 48 |
to_bytes("Actual protocol version: {}".format(expected_version)) |
| 49 |
not in results.stdout |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 54 |
@pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name) |
| 55 |
@pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name) |
| 56 |
@pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name) |
| 57 |
@pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name) |
| 58 |
@pytest.mark.parametrize("certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 59 |
@pytest.mark.parametrize("client_certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 60 |
def test_client_auth_with_s2n_server( |
| 61 |
managed_process, |
| 62 |
provider, |
| 63 |
other_provider, |
| 64 |
protocol, |
| 65 |
cipher, |
| 66 |
certificate, |
| 67 |
client_certificate, |
| 68 |
): |
| 69 |
port = next(available_ports) |
| 70 |
|
| 71 |
random_bytes = data_bytes(64) |
| 72 |
client_options = ProviderOptions( |
| 73 |
mode=Provider.ClientMode, |
| 74 |
port=port, |
| 75 |
cipher=cipher, |
| 76 |
data_to_send=random_bytes, |
| 77 |
use_client_auth=True, |
| 78 |
key=client_certificate.key, |
| 79 |
cert=client_certificate.cert, |
| 80 |
trust_store=certificate.cert, |
| 81 |
insecure=False, |
| 82 |
protocol=protocol, |
| 83 |
) |
| 84 |
|
| 85 |
server_options = copy.copy(client_options) |
| 86 |
server_options.data_to_send = None |
| 87 |
server_options.mode = Provider.ServerMode |
| 88 |
server_options.key = certificate.key |
| 89 |
server_options.cert = certificate.cert |
| 90 |
server_options.trust_store = client_certificate.cert |
| 91 |
|
| 92 |
server = managed_process(S2N, server_options, timeout=5) |
| 93 |
client = managed_process(provider, client_options, timeout=5) |
| 94 |
|
| 95 |
|
| 96 |
for results in client.get_results(): |
| 97 |
results.assert_success() |
| 98 |
assert b"write client certificate" in results.stderr |
| 99 |
assert b"write certificate verify" in results.stderr |
| 100 |
assert_openssl_handshake_complete(results) |
| 101 |
|
| 102 |
|
| 103 |
for results in server.get_results(): |
| 104 |
results.assert_success() |
| 105 |
assert_s2n_handshake_complete(results, protocol, provider) |
| 106 |
assert random_bytes in results.stdout |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 110 |
@pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name) |
| 111 |
@pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name) |
| 112 |
@pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name) |
| 113 |
@pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name) |
| 114 |
@pytest.mark.parametrize("certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 115 |
@pytest.mark.parametrize("client_certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 116 |
def test_client_auth_with_s2n_server_using_nonmatching_certs( |
| 117 |
managed_process, |
| 118 |
provider, |
| 119 |
other_provider, |
| 120 |
protocol, |
| 121 |
cipher, |
| 122 |
certificate, |
| 123 |
client_certificate, |
| 124 |
): |
| 125 |
port = next(available_ports) |
| 126 |
|
| 127 |
client_options = ProviderOptions( |
| 128 |
mode=Provider.ClientMode, |
| 129 |
port=port, |
| 130 |
cipher=cipher, |
| 131 |
data_to_send=b"", |
| 132 |
use_client_auth=True, |
| 133 |
key=client_certificate.key, |
| 134 |
cert=client_certificate.cert, |
| 135 |
trust_store=certificate.cert, |
| 136 |
insecure=False, |
| 137 |
protocol=protocol, |
| 138 |
) |
| 139 |
|
| 140 |
server_options = copy.copy(client_options) |
| 141 |
server_options.data_to_send = None |
| 142 |
server_options.mode = Provider.ServerMode |
| 143 |
server_options.key = certificate.key |
| 144 |
server_options.cert = certificate.cert |
| 145 |
|
| 146 |
|
| 147 |
server_options.trust_store = Certificates.RSA_2048_SHA256_WILDCARD.cert |
| 148 |
|
| 149 |
server = managed_process(S2N, server_options, timeout=5) |
| 150 |
client = managed_process(OpenSSL, client_options, timeout=5) |
| 151 |
|
| 152 |
|
| 153 |
for results in client.get_results(): |
| 154 |
assert results.exception is None |
| 155 |
assert b"write client certificate" in results.stderr |
| 156 |
assert b"write certificate verify" in results.stderr |
| 157 |
|
| 158 |
if protocol is not Protocols.TLS13: |
| 159 |
assert results.exit_code != 0 |
| 160 |
assert_openssl_handshake_complete(results, False) |
| 161 |
|
| 162 |
|
| 163 |
for results in server.get_results(): |
| 164 |
assert results.exception is None |
| 165 |
assert results.exit_code != 0 |
| 166 |
assert b"Certificate is untrusted" in results.stderr |
| 167 |
assert b"Error: Mutual Auth was required, but not negotiated" in results.stderr |
| 168 |
assert_s2n_handshake_complete(results, protocol, provider, False) |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 172 |
@pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name) |
| 173 |
@pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name) |
| 174 |
@pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name) |
| 175 |
@pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name) |
| 176 |
@pytest.mark.parametrize("certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 177 |
def test_client_auth_with_s2n_client_no_cert( |
| 178 |
managed_process, |
| 179 |
provider, |
| 180 |
other_provider, |
| 181 |
protocol, |
| 182 |
cipher, |
| 183 |
certificate, |
| 184 |
): |
| 185 |
port = next(available_ports) |
| 186 |
|
| 187 |
random_bytes = data_bytes(64) |
| 188 |
client_options = ProviderOptions( |
| 189 |
mode=Provider.ClientMode, |
| 190 |
port=port, |
| 191 |
cipher=cipher, |
| 192 |
data_to_send=random_bytes, |
| 193 |
use_client_auth=True, |
| 194 |
trust_store=certificate.cert, |
| 195 |
insecure=False, |
| 196 |
protocol=protocol, |
| 197 |
) |
| 198 |
|
| 199 |
server_options = copy.copy(client_options) |
| 200 |
server_options.data_to_send = None |
| 201 |
server_options.mode = Provider.ServerMode |
| 202 |
server_options.key = certificate.key |
| 203 |
server_options.cert = certificate.cert |
| 204 |
|
| 205 |
server = managed_process(provider, server_options, timeout=5) |
| 206 |
client = managed_process(S2N, client_options, timeout=5) |
| 207 |
|
| 208 |
|
| 209 |
for results in server.get_results(): |
| 210 |
results.assert_success() |
| 211 |
assert b"write certificate request" in results.stderr |
| 212 |
assert b"read client certificate" not in results.stderr |
| 213 |
assert b"peer did not return a certificate" in results.stderr |
| 214 |
assert_openssl_handshake_complete(results, False) |
| 215 |
|
| 216 |
for results in client.get_results(): |
| 217 |
assert results.exception is None |
| 218 |
|
| 219 |
if protocol is not Protocols.TLS13: |
| 220 |
assert results.exit_code != 0 |
| 221 |
assert b"Failed to negotiate: 'TLS alert received'" in results.stderr |
| 222 |
assert_s2n_handshake_complete(results, protocol, provider, False) |
| 223 |
|
| 224 |
|
| 225 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 226 |
@pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name) |
| 227 |
@pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name) |
| 228 |
@pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name) |
| 229 |
@pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name) |
| 230 |
@pytest.mark.parametrize("certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 231 |
@pytest.mark.parametrize("client_certificate", CERTS_TO_TEST, ids=get_parameter_name) |
| 232 |
def test_client_auth_with_s2n_client_with_cert( |
| 233 |
managed_process, |
| 234 |
provider, |
| 235 |
other_provider, |
| 236 |
protocol, |
| 237 |
cipher, |
| 238 |
certificate, |
| 239 |
client_certificate, |
| 240 |
): |
| 241 |
port = next(available_ports) |
| 242 |
|
| 243 |
random_bytes = data_bytes(64) |
| 244 |
client_options = ProviderOptions( |
| 245 |
mode=Provider.ClientMode, |
| 246 |
port=port, |
| 247 |
cipher=cipher, |
| 248 |
data_to_send=random_bytes, |
| 249 |
use_client_auth=True, |
| 250 |
key=client_certificate.key, |
| 251 |
cert=client_certificate.cert, |
| 252 |
trust_store=certificate.cert, |
| 253 |
insecure=False, |
| 254 |
protocol=protocol, |
| 255 |
) |
| 256 |
|
| 257 |
server_options = copy.copy(client_options) |
| 258 |
server_options.data_to_send = None |
| 259 |
server_options.mode = Provider.ServerMode |
| 260 |
server_options.key = certificate.key |
| 261 |
server_options.cert = certificate.cert |
| 262 |
server_options.trust_store = client_certificate.cert |
| 263 |
|
| 264 |
server = managed_process(provider, server_options, timeout=5) |
| 265 |
client = managed_process(S2N, client_options, timeout=5) |
| 266 |
|
| 267 |
|
| 268 |
for results in client.get_results(): |
| 269 |
results.assert_success() |
| 270 |
assert_s2n_handshake_complete(results, protocol, provider) |
| 271 |
|
| 272 |
|
| 273 |
for results in server.get_results(): |
| 274 |
results.assert_success() |
| 275 |
assert random_bytes[1:] in results.stdout |
| 276 |
assert b"read client certificate" in results.stderr |
| 277 |
assert b"read certificate verify" in results.stderr |
| 278 |
assert_openssl_handshake_complete(results) |
| 279 |
|
| 280 |
|
| 281 |
""" |
| 282 |
TLS1.3 requires that RSA-PSS be used for RSA signatures. However, older libcrypto implementations |
| 283 |
like openssl-1.0.2 do not support the RSA-PSS algorithm, so s2n-tls doesn't support RSA-PSS when |
| 284 |
built with those older libcryptos. If a server that doesn't support RSA-PSS requests client |
| 285 |
authentication when using TLS1.3, and the client responds with an RSA certificate, then the |
| 286 |
connection will fail because the server is unable to use RSA-PSS. To avoid this scenario, a server |
| 287 |
configured to request client authentication but not built to support RSA-PSS should not support |
| 288 |
TLS1.3, even if its security policy would normally allow TLS1.3. |
| 289 |
""" |
| 290 |
|
| 291 |
|
| 292 |
@pytest.mark.parametrize( |
| 293 |
"certificate", |
| 294 |
[Certificates.RSA_2048_PKCS1, Certificates.ECDSA_256], |
| 295 |
ids=get_parameter_name, |
| 296 |
) |
| 297 |
def test_tls_12_client_auth_downgrade(managed_process, certificate): |
| 298 |
port = next(available_ports) |
| 299 |
|
| 300 |
random_bytes = data_bytes(64) |
| 301 |
client_options = ProviderOptions( |
| 302 |
mode=Provider.ClientMode, |
| 303 |
port=port, |
| 304 |
data_to_send=random_bytes, |
| 305 |
use_client_auth=True, |
| 306 |
key=certificate.key, |
| 307 |
cert=certificate.cert, |
| 308 |
trust_store=Certificates.ECDSA_256.cert, |
| 309 |
insecure=False, |
| 310 |
) |
| 311 |
|
| 312 |
client_options.extra_flags = ["--no-ca-verification"] |
| 313 |
|
| 314 |
server_options = ProviderOptions( |
| 315 |
mode=Provider.ServerMode, |
| 316 |
port=port, |
| 317 |
use_client_auth=True, |
| 318 |
protocol=Protocols.TLS13, |
| 319 |
key=Certificates.ECDSA_256.key, |
| 320 |
cert=Certificates.ECDSA_256.cert, |
| 321 |
trust_store=certificate.cert, |
| 322 |
insecure=False, |
| 323 |
) |
| 324 |
|
| 325 |
server = managed_process(S2N, server_options, timeout=5) |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
client = managed_process(GnuTLS, client_options, timeout=5) |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if S2N.supports_signature(Signatures.RSA_PSS_RSAE_SHA256): |
| 338 |
expected_protocol_version = Protocols.TLS13.value |
| 339 |
else: |
| 340 |
expected_protocol_version = Protocols.TLS12.value |
| 341 |
|
| 342 |
expected_signature_type = "ECDSA" |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if certificate.algorithm == "RSA": |
| 347 |
if expected_protocol_version == Protocols.TLS13.value: |
| 348 |
expected_signature_type = "RSA-PSS" |
| 349 |
else: |
| 350 |
expected_signature_type = "RSA" |
| 351 |
|
| 352 |
for results in client.get_results(): |
| 353 |
results.assert_success() |
| 354 |
|
| 355 |
for results in server.get_results(): |
| 356 |
results.assert_success() |
| 357 |
assert ( |
| 358 |
to_bytes(f"Actual protocol version: {expected_protocol_version}") |
| 359 |
in results.stdout |
| 360 |
) |
| 361 |
assert ( |
| 362 |
to_bytes(f"Client signature negotiated: {expected_signature_type}") |
| 363 |
in results.stdout |
| 364 |
) |