| 1 |
|
| 2 |
|
| 3 |
import pytest |
| 4 |
import copy |
| 5 |
import os |
| 6 |
from enum import Enum, auto |
| 7 |
|
| 8 |
from configuration import available_ports |
| 9 |
from common import Ciphers, ProviderOptions, Protocols, random_str |
| 10 |
from fixtures import managed_process |
| 11 |
from providers import Provider, S2N |
| 12 |
from utils import invalid_test_parameters, get_parameter_name, to_bytes |
| 13 |
|
| 14 |
SERVER_STATE_FILE = "server_state" |
| 15 |
CLIENT_STATE_FILE = "client_state" |
| 16 |
|
| 17 |
SERVER_DATA = "Some random data from the server:" + random_str(10) |
| 18 |
CLIENT_DATA = "Some random data from the client:" + random_str(10) |
| 19 |
|
| 20 |
S2N_TEST_POLICIES = { |
| 21 |
Protocols.TLS12.value: Ciphers.SECURITY_POLICY_DEFAULT_TLS12, |
| 22 |
Protocols.TLS13.value: Ciphers.SECURITY_POLICY_DEFAULT_TLS13, |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
class MainlineRole(Enum): |
| 27 |
Serialize = auto() |
| 28 |
Deserialize = auto() |
| 29 |
|
| 30 |
|
| 31 |
class Mode(Enum): |
| 32 |
Server = auto() |
| 33 |
Client = auto() |
| 34 |
|
| 35 |
|
| 36 |
""" |
| 37 |
This test file checks that a serialized connection can be deserialized by an older version of |
| 38 |
s2n-tls and vice versa. This ensures that any future changes we make to the handshake are backwards-compatible |
| 39 |
with an older version of s2n-tls. |
| 40 |
|
| 41 |
This feature requires an uninterrupted TCP connection with the peer in-between serialization and |
| 42 |
deserialization. Our integration test setup can't easily provide that while also using two different |
| 43 |
s2n-tls versions. To get around that we do a hack and serialize/deserialize both peers in the TLS connection. |
| 44 |
This prevents one peer from receiving a TCP FIN message and shutting the connection down early. |
| 45 |
""" |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.uncollect_if(func=invalid_test_parameters) |
| 49 |
@pytest.mark.parametrize( |
| 50 |
"protocol", [Protocols.TLS13, Protocols.TLS12], ids=get_parameter_name |
| 51 |
) |
| 52 |
@pytest.mark.parametrize( |
| 53 |
"mainline_role", |
| 54 |
[MainlineRole.Serialize, MainlineRole.Deserialize], |
| 55 |
ids=get_parameter_name, |
| 56 |
) |
| 57 |
@pytest.mark.parametrize( |
| 58 |
"version_change", [Mode.Server, Mode.Client], ids=get_parameter_name |
| 59 |
) |
| 60 |
def test_server_serialization_backwards_compat( |
| 61 |
managed_process, |
| 62 |
tmp_path, |
| 63 |
protocol, |
| 64 |
mainline_role, |
| 65 |
version_change, |
| 66 |
): |
| 67 |
server_state_file = str(tmp_path / SERVER_STATE_FILE) |
| 68 |
client_state_file = str(tmp_path / CLIENT_STATE_FILE) |
| 69 |
assert not os.path.exists(server_state_file) |
| 70 |
assert not os.path.exists(client_state_file) |
| 71 |
|
| 72 |
options = ProviderOptions( |
| 73 |
port=next(available_ports), |
| 74 |
cipher=S2N_TEST_POLICIES[protocol.value], |
| 75 |
insecure=True, |
| 76 |
echo=False, |
| 77 |
) |
| 78 |
|
| 79 |
client_options = copy.copy(options) |
| 80 |
client_options.mode = Provider.ClientMode |
| 81 |
client_options.extra_flags = ["--serialize-out", client_state_file] |
| 82 |
|
| 83 |
server_options = copy.copy(options) |
| 84 |
server_options.mode = Provider.ServerMode |
| 85 |
server_options.extra_flags = ["--serialize-out", server_state_file] |
| 86 |
|
| 87 |
if mainline_role is MainlineRole.Serialize: |
| 88 |
if version_change == Mode.Server: |
| 89 |
server_options.use_mainline_version = True |
| 90 |
else: |
| 91 |
client_options.use_mainline_version = True |
| 92 |
|
| 93 |
server = managed_process(S2N, server_options, send_marker=S2N.get_send_marker()) |
| 94 |
client = managed_process(S2N, client_options, send_marker=S2N.get_send_marker()) |
| 95 |
|
| 96 |
for results in client.get_results(): |
| 97 |
results.assert_success() |
| 98 |
assert ( |
| 99 |
to_bytes("Actual protocol version: {}".format(protocol.value)) |
| 100 |
in results.stdout |
| 101 |
) |
| 102 |
|
| 103 |
for results in server.get_results(): |
| 104 |
results.assert_success() |
| 105 |
assert ( |
| 106 |
to_bytes("Actual protocol version: {}".format(protocol.value)) |
| 107 |
in results.stdout |
| 108 |
) |
| 109 |
|
| 110 |
assert os.path.exists(server_state_file) |
| 111 |
assert os.path.exists(client_state_file) |
| 112 |
|
| 113 |
client_options.extra_flags = ["--deserialize-in", client_state_file] |
| 114 |
server_options.extra_flags = ["--deserialize-in", server_state_file] |
| 115 |
if mainline_role is MainlineRole.Deserialize: |
| 116 |
if version_change == Mode.Server: |
| 117 |
server_options.use_mainline_version = True |
| 118 |
else: |
| 119 |
client_options.use_mainline_version = True |
| 120 |
|
| 121 |
server_options.echo = True |
| 122 |
server_options.data_to_send = SERVER_DATA.encode() |
| 123 |
|
| 124 |
client_options.echo = True |
| 125 |
client_options.data_to_send = CLIENT_DATA.encode() |
| 126 |
|
| 127 |
server = managed_process(S2N, server_options, send_marker=CLIENT_DATA) |
| 128 |
client = managed_process( |
| 129 |
S2N, |
| 130 |
client_options, |
| 131 |
send_marker="Connected to localhost", |
| 132 |
close_marker=SERVER_DATA, |
| 133 |
) |
| 134 |
|
| 135 |
for results in server.get_results(): |
| 136 |
results.assert_success() |
| 137 |
|
| 138 |
assert to_bytes("Actual protocol version:") not in results.stdout |
| 139 |
assert CLIENT_DATA.encode() in results.stdout |
| 140 |
|
| 141 |
for results in client.get_results(): |
| 142 |
results.assert_success() |
| 143 |
assert to_bytes("Actual protocol version:") not in results.stdout |
| 144 |
assert SERVER_DATA.encode() in results.stdout |