| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
# pylint: disable=too-many-lines |
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
import collections |
| 7 |
import copy |
| 8 |
import datetime |
| 9 |
import hashlib |
| 10 |
|
| 11 |
from cryptodatahub.common.exception import InvalidValue |
| 12 |
from cryptodatahub.tls.algorithm import ( |
| 13 |
TlsCipherSuiteExtension, |
| 14 |
TlsECPointFormat, |
| 15 |
TlsGreaseOneByte, |
| 16 |
TlsGreaseTwoByte, |
| 17 |
TlsNamedCurve, |
| 18 |
TlsProtocolName, |
| 19 |
TlsSignatureAndHashAlgorithm, |
| 20 |
) |
| 21 |
|
| 22 |
from cryptoparser.common.exception import InvalidType, NotEnoughData |
| 23 |
from cryptoparser.tls.ciphersuite import TlsCipherSuite, SslCipherKind |
| 24 |
from cryptoparser.tls.extension import ( |
| 25 |
TlsExtensionApplicationLayerProtocolNegotiation, |
| 26 |
TlsExtensionSignatureAlgorithms, |
| 27 |
TlsExtensionSupportedVersionsClient, |
| 28 |
TlsExtensionSupportedVersionsServer, |
| 29 |
TlsExtensionUnparsed, |
| 30 |
TlsExtensionEllipticCurves, |
| 31 |
TlsExtensionECPointFormats, |
| 32 |
TlsECPointFormatVector, |
| 33 |
TlsEllipticCurveVector, |
| 34 |
TlsSupportedVersionVector, |
| 35 |
) |
| 36 |
from cryptoparser.tls.grease import TlsInvalidTypeOneByte, TlsInvalidTypeTwoByte |
| 37 |
from cryptoparser.tls.subprotocol import ( |
| 38 |
TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM, |
| 39 |
TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM_BYTES, |
| 40 |
SslHandshakeClientHello, |
| 41 |
SslHandshakeServerHello, |
| 42 |
SslMessageType, |
| 43 |
TlsAlertMessage, |
| 44 |
TlsCertificate, |
| 45 |
TlsCertificateEntry, |
| 46 |
TlsCertificateEntryVector, |
| 47 |
TlsCertificateStatusType, |
| 48 |
TlsCertificates, |
| 49 |
TlsClientCertificateType, |
| 50 |
TlsCipherSuiteVector, |
| 51 |
TlsCompressionMethod, |
| 52 |
TlsCompressionMethodVector, |
| 53 |
TlsContentType, |
| 54 |
TlsDistinguishedName, |
| 55 |
TlsExtensionType, |
| 56 |
TlsExtensionsClient, |
| 57 |
TlsExtensionsServer, |
| 58 |
TlsHandshakeCertificate, |
| 59 |
TlsHandshakeServerCertificate, |
| 60 |
TlsHandshakeCertificateRequest, |
| 61 |
TlsHandshakeCertificateStatus, |
| 62 |
TlsHandshakeClientHello, |
| 63 |
TlsHandshakeEncryptedExtensions, |
| 64 |
TlsHandshakeHelloRandom, |
| 65 |
TlsHandshakeHelloRandomBytes, |
| 66 |
TlsHandshakeHelloRetryRequest, |
| 67 |
TlsHandshakeMessageVariant, |
| 68 |
TlsHandshakeServerHello, |
| 69 |
TlsHandshakeServerHelloDone, |
| 70 |
TlsHandshakeServerKeyExchange, |
| 71 |
TlsHandshakeType, |
| 72 |
TlsSessionIdVector, |
| 73 |
TlsSubprotocolMessageParser, |
| 74 |
) |
| 75 |
|
| 76 |
from cryptoparser.tls.record import TlsRecord |
| 77 |
from cryptoparser.tls.version import TlsVersion, TlsProtocolVersion |
| 78 |
|
| 79 |
from .classes import TestMessage |
| 80 |
|
| 81 |
|
| 82 |
class TestSubprotocolParser(unittest.TestCase): |
| 83 |
def test_error(self): |
| 84 |
subprotocol_parser = TlsSubprotocolMessageParser(TlsContentType.HEARTBEAT) |
| 85 |
with self.assertRaises(InvalidValue) as context_manager: |
| 86 |
subprotocol_parser.parse( |
| 87 |
b'\x18' + # type = heartbeat |
| 88 |
b'\x03\x03' + # version = TLS 1.2 |
| 89 |
b'\x00\x01' + # length = 1 |
| 90 |
b'\x00' |
| 91 |
) |
| 92 |
self.assertEqual(context_manager.exception.value, 0x18) |
| 93 |
|
| 94 |
def test_registered_parser(self): |
| 95 |
tls_message_dict = collections.OrderedDict([ |
| 96 |
('level', b'\x02'), # FATAL |
| 97 |
('description', b'\x28'), # HANDSHAKE_FAILURE |
| 98 |
]) |
| 99 |
tls_message_bytes = b''.join(tls_message_dict.values()) |
| 100 |
tls_parser = TlsSubprotocolMessageParser(TlsContentType.ALERT) |
| 101 |
tls_parser.parse(tls_message_bytes) |
| 102 |
|
| 103 |
tls_parser.register_subprotocol_parser(TlsContentType.ALERT, TestMessage) |
| 104 |
with self.assertRaises(NotImplementedError): |
| 105 |
tls_parser.parse(tls_message_bytes) |
| 106 |
|
| 107 |
tls_parser.register_subprotocol_parser(TlsContentType.ALERT, TlsAlertMessage) |
| 108 |
parsed_object, _ = tls_parser.parse(tls_message_bytes) |
| 109 |
self.assertEqual(parsed_object.compose(), tls_message_bytes) |
| 110 |
|
| 111 |
|
| 112 |
class TestVariantParsable(unittest.TestCase): |
| 113 |
def setUp(self): |
| 114 |
self.server_hello_done_dict = collections.OrderedDict([ |
| 115 |
('handshake_type', b'\x0e'), # SERVER_HELLO_DONE |
| 116 |
('length', b'\x00\x00\x00'), # 0x00 |
| 117 |
]) |
| 118 |
self.server_hello_done_bytes = b''.join(self.server_hello_done_dict.values()) |
| 119 |
self.server_hello_done = TlsHandshakeServerHelloDone() |
| 120 |
|
| 121 |
def test_error(self): |
| 122 |
invalid_tls_message_dict = collections.OrderedDict([ |
| 123 |
('content_type', b'\x17'), |
| 124 |
('data', b'\x00\x00\x00'), |
| 125 |
]) |
| 126 |
invalid_tls_message_bytes = b''.join(invalid_tls_message_dict.values()) |
| 127 |
|
| 128 |
with self.assertRaisesRegex(InvalidValue, 'is not a valid TlsHandshakeMessageVariant'): |
| 129 |
TlsHandshakeMessageVariant.parse_exact_size(invalid_tls_message_bytes) |
| 130 |
|
| 131 |
def test_compose(self): |
| 132 |
self.assertEqual(TlsHandshakeMessageVariant(self.server_hello_done).compose(), self.server_hello_done_bytes) |
| 133 |
|
| 134 |
|
| 135 |
class TestTlsCipherSuiteVector(unittest.TestCase): |
| 136 |
def test_parse(self): |
| 137 |
cipher_suites = TlsCipherSuiteVector.parse_exact_size(b'\x00\x02\x00\x00') |
| 138 |
self.assertEqual(cipher_suites, TlsCipherSuiteVector([TlsCipherSuite.TLS_NULL_WITH_NULL_NULL])) |
| 139 |
|
| 140 |
cipher_suites = TlsCipherSuiteVector.parse_exact_size(b'\x00\x04\x56\x00\x00\xff') |
| 141 |
self.assertEqual( |
| 142 |
cipher_suites, |
| 143 |
TlsCipherSuiteVector([ |
| 144 |
TlsInvalidTypeTwoByte(TlsCipherSuiteExtension.FALLBACK_SCSV), |
| 145 |
TlsInvalidTypeTwoByte(TlsCipherSuiteExtension.EMPTY_RENEGOTIATION_INFO_SCSV), |
| 146 |
]) |
| 147 |
) |
| 148 |
|
| 149 |
cipher_suites = TlsCipherSuiteVector.parse_exact_size(b'\x00\x06\x56\x00\x00\x00\x00\xff') |
| 150 |
self.assertEqual( |
| 151 |
cipher_suites, |
| 152 |
TlsCipherSuiteVector([ |
| 153 |
TlsInvalidTypeTwoByte(TlsCipherSuiteExtension.FALLBACK_SCSV), |
| 154 |
TlsCipherSuite.TLS_NULL_WITH_NULL_NULL, |
| 155 |
TlsInvalidTypeTwoByte(TlsCipherSuiteExtension.EMPTY_RENEGOTIATION_INFO_SCSV), |
| 156 |
]) |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
class TestTlsHandshake(unittest.TestCase): |
| 161 |
def setUp(self): |
| 162 |
self.server_hello_done_dict = collections.OrderedDict([ |
| 163 |
('handshake_type', b'\x0e'), # SERVER_HELLO_DONE |
| 164 |
('length', b'\x00\x00\x00'), |
| 165 |
]) |
| 166 |
self.server_hello_done_bytes = b''.join(self.server_hello_done_dict.values()) |
| 167 |
self.server_hello_done_record_dict = collections.OrderedDict([ |
| 168 |
('content_type', b'\x16'), # HANDSHAKE |
| 169 |
('protocol_version', b'\x03\x01'), # TLS1 |
| 170 |
('length', b'\x00\x04'), |
| 171 |
]) |
| 172 |
self.server_hello_done_record_bytes = b''.join(self.server_hello_done_record_dict.values()) |
| 173 |
|
| 174 |
def test_error(self): |
| 175 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 176 |
# pylint: disable=expression-not-assigned |
| 177 |
TlsHandshakeClientHello.parse_exact_size(b'') |
| 178 |
self.assertEqual(context_manager.exception.bytes_needed, 4) |
| 179 |
|
| 180 |
with self.assertRaises(InvalidType): |
| 181 |
# pylint: disable=expression-not-assigned |
| 182 |
TlsHandshakeClientHello.parse_exact_size(self.server_hello_done_bytes) |
| 183 |
|
| 184 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 185 |
TlsHandshakeClientHello.parse_exact_size( |
| 186 |
b'\x01' # handshake_type: CLIENT_HELLO |
| 187 |
b'\x00\x00\x03' + # handshake_length = 3 |
| 188 |
b'\x03\x03' + # version = TLS 1.2 |
| 189 |
b'' |
| 190 |
) |
| 191 |
self.assertEqual(context_manager.exception.bytes_needed, 1) |
| 192 |
|
| 193 |
with self.assertRaises(InvalidValue) as context_manager: |
| 194 |
TlsHandshakeClientHello.parse_exact_size( |
| 195 |
b'\xff' # handshake_type: INVALID |
| 196 |
b'\x00\x00\x02' + # handshake_length = 2 |
| 197 |
b'\x03\x03' + # version = TLS 1.2 |
| 198 |
b'' |
| 199 |
) |
| 200 |
self.assertEqual(context_manager.exception.value, 0xff) |
| 201 |
|
| 202 |
def test_parse(self): |
| 203 |
record = TlsRecord.parse_exact_size( |
| 204 |
self.server_hello_done_record_bytes + self.server_hello_done_bytes |
| 205 |
) |
| 206 |
self.assertEqual(record.protocol_version, TlsProtocolVersion(TlsVersion.TLS1)) |
| 207 |
record.protocol_version = TlsProtocolVersion(TlsVersion.TLS1_2) |
| 208 |
self.assertEqual(record.protocol_version, TlsProtocolVersion(TlsVersion.TLS1_2)) |
| 209 |
|
| 210 |
|
| 211 |
class TestTlsHandshakeClientHello(unittest.TestCase): |
| 212 |
def setUp(self): |
| 213 |
self.client_hello_minimal_dict = collections.OrderedDict([ |
| 214 |
('handshake_type ', b'\x01'), # CLIENT_HELLO |
| 215 |
('length ', b'\x00\x00\x37'), |
| 216 |
('version ', b'\x03\x03'), |
| 217 |
('random ', |
| 218 |
b'\x5b\x6c\xd5\x80\x04\x05\x06\x07' + |
| 219 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 220 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 221 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 222 |
b''), |
| 223 |
('session_id_length', b'\x00'), |
| 224 |
('cipher_suite_length', b'\x00\x10'), |
| 225 |
('cipher_suites', |
| 226 |
b'\x0a\x0a\x00\x01\x00\x02\x00\x03' + |
| 227 |
b'\x00\x04\x00\x05\x56\x00\x00\xff' + |
| 228 |
b''), |
| 229 |
('compression_method_length', b'\x01'), |
| 230 |
('compression_methods', b'\x00'), |
| 231 |
]) |
| 232 |
self.client_hello_minimal_bytes = b''.join(self.client_hello_minimal_dict.values()) |
| 233 |
self.client_hello_minimal_extensions_dict = collections.OrderedDict([ |
| 234 |
('extensions_length', b'\x00\x0d'), |
| 235 |
('extension_type', b'\x00\x2b'), # SUPPORTED_VERSIONS |
| 236 |
('extension_length', b'\x00\x05'), |
| 237 |
('supported_version_list_length', b'\x04'), |
| 238 |
('supported_version_list', b'\x03\x02\x03\x03'), # TLS1_1, TLS1_2 |
| 239 |
('extension_grease', b'\x0a\x0a'), |
| 240 |
('extension_grease_length', b'\x00\x00'), |
| 241 |
]) |
| 242 |
self.client_hello_minimal_extensions_bytes = b''.join(self.client_hello_minimal_extensions_dict.values()) |
| 243 |
self.client_hello_extension_bytes = bytearray( |
| 244 |
self.client_hello_minimal_bytes + |
| 245 |
self.client_hello_minimal_extensions_bytes + |
| 246 |
b'' |
| 247 |
) |
| 248 |
self.client_hello_extension_bytes[3] += ( |
| 249 |
len(self.client_hello_extension_bytes) - |
| 250 |
len(self.client_hello_minimal_bytes) |
| 251 |
) |
| 252 |
|
| 253 |
self.random_time = datetime.datetime(2018, 8, 10, tzinfo=datetime.timezone.utc) |
| 254 |
self.client_hello_minimal = TlsHandshakeClientHello( |
| 255 |
TlsCipherSuiteVector([ |
| 256 |
TlsInvalidTypeTwoByte(TlsGreaseTwoByte.GREASE_0A0A), |
| 257 |
TlsCipherSuite.TLS_RSA_WITH_NULL_MD5, |
| 258 |
TlsCipherSuite.TLS_RSA_WITH_NULL_SHA, |
| 259 |
TlsCipherSuite.TLS_RSA_EXPORT_WITH_RC4_40_MD5, |
| 260 |
TlsCipherSuite.TLS_RSA_WITH_RC4_128_MD5, |
| 261 |
TlsCipherSuite.TLS_RSA_WITH_RC4_128_SHA, |
| 262 |
]), |
| 263 |
TlsProtocolVersion(TlsVersion.TLS1_2), |
| 264 |
TlsHandshakeHelloRandom( |
| 265 |
self.random_time, |
| 266 |
TlsHandshakeHelloRandomBytes(bytearray( |
| 267 |
b'\x04\x05\x06\x07' + |
| 268 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 269 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 270 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 271 |
b'' |
| 272 |
)) |
| 273 |
), |
| 274 |
TlsSessionIdVector(()), |
| 275 |
TlsCompressionMethodVector([TlsCompressionMethod.NULL, ]), |
| 276 |
TlsExtensionsClient(()), |
| 277 |
fallback_scsv=True, |
| 278 |
empty_renegotiation_info_scsv=True, |
| 279 |
) |
| 280 |
|
| 281 |
def test_parse(self): |
| 282 |
client_hello_minimal = TlsHandshakeClientHello.parse_exact_size(self.client_hello_minimal_bytes) |
| 283 |
|
| 284 |
self.assertEqual(client_hello_minimal.get_handshake_type(), TlsHandshakeType.CLIENT_HELLO) |
| 285 |
|
| 286 |
self.assertEqual( |
| 287 |
client_hello_minimal.protocol_version, |
| 288 |
TlsProtocolVersion(TlsVersion.TLS1_2) |
| 289 |
) |
| 290 |
self.assertEqual( |
| 291 |
client_hello_minimal.random, |
| 292 |
self.client_hello_minimal.random |
| 293 |
) |
| 294 |
self.assertEqual( |
| 295 |
client_hello_minimal.random.time, |
| 296 |
self.random_time, |
| 297 |
) |
| 298 |
self.assertEqual( |
| 299 |
client_hello_minimal.random.random, |
| 300 |
self.client_hello_minimal.random.random |
| 301 |
) |
| 302 |
self.assertEqual( |
| 303 |
client_hello_minimal.cipher_suites, |
| 304 |
self.client_hello_minimal.cipher_suites |
| 305 |
) |
| 306 |
self.assertEqual( |
| 307 |
client_hello_minimal.compression_methods, |
| 308 |
self.client_hello_minimal.compression_methods |
| 309 |
) |
| 310 |
self.assertEqual( |
| 311 |
client_hello_minimal.extensions, |
| 312 |
self.client_hello_minimal.extensions |
| 313 |
) |
| 314 |
self.assertTrue(client_hello_minimal.fallback_scsv) |
| 315 |
self.assertTrue(client_hello_minimal.empty_renegotiation_info_scsv) |
| 316 |
|
| 317 |
client_hello_extension = TlsHandshakeClientHello.parse_exact_size(self.client_hello_extension_bytes) |
| 318 |
self.assertEqual(len(client_hello_extension.extensions), 2) |
| 319 |
self.assertEqual( |
| 320 |
client_hello_extension.extensions.get_item_by_type(TlsExtensionType.SUPPORTED_VERSIONS), |
| 321 |
TlsExtensionSupportedVersionsClient(TlsSupportedVersionVector([ |
| 322 |
TlsProtocolVersion(TlsVersion.TLS1_1), |
| 323 |
TlsProtocolVersion(TlsVersion.TLS1_2), |
| 324 |
])) |
| 325 |
) |
| 326 |
self.assertEqual( |
| 327 |
client_hello_extension.extensions[1], |
| 328 |
TlsExtensionUnparsed(TlsInvalidTypeTwoByte(TlsGreaseTwoByte.GREASE_0A0A), b'') |
| 329 |
) |
| 330 |
with self.assertRaises(KeyError): |
| 331 |
client_hello_extension.extensions.get_item_by_type(TlsGreaseTwoByte.GREASE_0A0A) |
| 332 |
|
| 333 |
def test_compose(self): |
| 334 |
self.assertEqual( |
| 335 |
self.client_hello_minimal.compose(), |
| 336 |
self.client_hello_minimal_bytes |
| 337 |
) |
| 338 |
client_hello_extension = TlsHandshakeClientHello.parse_exact_size(self.client_hello_extension_bytes) |
| 339 |
self.assertEqual( |
| 340 |
client_hello_extension.compose(), |
| 341 |
self.client_hello_extension_bytes |
| 342 |
) |
| 343 |
|
| 344 |
def test_ja3(self): |
| 345 |
client_hello_minimal = copy.copy(self.client_hello_minimal) |
| 346 |
self.assertEqual(client_hello_minimal.ja3(), '771,2570-1-2-3-4-5,,,') |
| 347 |
|
| 348 |
client_hello_minimal.extensions.append( |
| 349 |
TlsExtensionEllipticCurves(TlsEllipticCurveVector([TlsNamedCurve.SECT163K1])) |
| 350 |
) |
| 351 |
self.assertEqual(client_hello_minimal.ja3(), '771,2570-1-2-3-4-5,10,1,') |
| 352 |
client_hello_minimal.extensions[0].elliptic_curves.append(TlsInvalidTypeTwoByte(TlsGreaseTwoByte.GREASE_0A0A)) |
| 353 |
self.assertEqual(client_hello_minimal.ja3(), '771,2570-1-2-3-4-5,10,1,') |
| 354 |
|
| 355 |
client_hello_minimal.extensions.append( |
| 356 |
TlsExtensionECPointFormats(TlsECPointFormatVector([TlsECPointFormat.UNCOMPRESSED])) |
| 357 |
) |
| 358 |
self.assertEqual(client_hello_minimal.ja3(), '771,2570-1-2-3-4-5,10-11,1,0') |
| 359 |
client_hello_minimal.extensions[1].point_formats.append(TlsInvalidTypeOneByte(TlsGreaseOneByte.GREASE_0B)) |
| 360 |
self.assertEqual(client_hello_minimal.ja3(), '771,2570-1-2-3-4-5,10-11,1,0') |
| 361 |
|
| 362 |
@staticmethod |
| 363 |
def _ja4_hash(text): |
| 364 |
return hashlib.sha256(text.encode('ascii')).hexdigest()[:12] |
| 365 |
|
| 366 |
def test_ja4_sub_hashes(self): |
| 367 |
# canonical example from the JA4 technical specification |
| 368 |
ciphers = '002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9' |
| 369 |
extensions = '0005,000a,000b,000d,0012,0015,0017,001b,0023,002b,002d,0033,4469,ff01' |
| 370 |
signature_algorithms = '0403,0804,0401,0503,0805,0501,0806,0601' |
| 371 |
|
| 372 |
self.assertEqual(self._ja4_hash(ciphers), '8daaf6152771') |
| 373 |
self.assertEqual(self._ja4_hash(extensions + '_' + signature_algorithms), 'e5627efa2ab1') |
| 374 |
|
| 375 |
def test_ja4(self): |
| 376 |
client_hello_minimal = copy.copy(self.client_hello_minimal) |
| 377 |
|
| 378 |
# the GREASE cipher (2570 in the JA3 tag) is excluded; five ciphers remain |
| 379 |
cipher_hash = self._ja4_hash('0001,0002,0003,0004,0005') |
| 380 |
result = client_hello_minimal.ja4() |
| 381 |
self.assertEqual(result.fingerprint, f't12i050000_{cipher_hash}_000000000000') |
| 382 |
# ciphers are already in ascending order and there are no extensions, so the original-order |
| 383 |
# hashed form equals the sorted one |
| 384 |
self.assertEqual(result.fingerprint_original, result.fingerprint) |
| 385 |
self.assertEqual(result.fingerprint_raw, 't12i050000_0001,0002,0003,0004,0005__') |
| 386 |
self.assertEqual(result.fingerprint_raw_original, 't12i050000_0001,0002,0003,0004,0005__') |
| 387 |
|
| 388 |
def test_ja4_original_order(self): |
| 389 |
client_hello = TlsHandshakeClientHello( |
| 390 |
TlsCipherSuiteVector([ |
| 391 |
TlsCipherSuite.TLS_RSA_WITH_NULL_SHA, # 0x0002 |
| 392 |
TlsCipherSuite.TLS_RSA_WITH_NULL_MD5, # 0x0001 |
| 393 |
]), |
| 394 |
TlsProtocolVersion(TlsVersion.TLS1_2), |
| 395 |
) |
| 396 |
result = client_hello.ja4() |
| 397 |
# the sorted hash is over 0001,0002; the original-order hash is over 0002,0001, so they differ |
| 398 |
self.assertEqual(result.fingerprint, f't12i020000_{self._ja4_hash("0001,0002")}_000000000000') |
| 399 |
self.assertEqual(result.fingerprint_original, f't12i020000_{self._ja4_hash("0002,0001")}_000000000000') |
| 400 |
self.assertNotEqual(result.fingerprint, result.fingerprint_original) |
| 401 |
self.assertEqual(result.fingerprint_raw, 't12i020000_0001,0002__') |
| 402 |
self.assertEqual(result.fingerprint_raw_original, 't12i020000_0002,0001__') |
| 403 |
|
| 404 |
def test_ja4_supported_versions_and_extensions(self): |
| 405 |
client_hello_minimal = copy.copy(self.client_hello_minimal) |
| 406 |
client_hello_minimal.extensions.append( |
| 407 |
TlsExtensionSupportedVersionsClient(TlsSupportedVersionVector([ |
| 408 |
TlsProtocolVersion(TlsVersion.TLS1_3), |
| 409 |
])) |
| 410 |
) |
| 411 |
|
| 412 |
cipher_hash = self._ja4_hash('0001,0002,0003,0004,0005') |
| 413 |
extension_hash = self._ja4_hash('002b') |
| 414 |
result = client_hello_minimal.ja4() |
| 415 |
# version is the highest in supported_versions (TLS 1.3 -> "13"), not the legacy protocol version |
| 416 |
self.assertEqual(result.fingerprint, f't13i050100_{cipher_hash}_{extension_hash}') |
| 417 |
self.assertEqual(result.fingerprint_raw, 't13i050100_0001,0002,0003,0004,0005_002b_') |
| 418 |
|
| 419 |
def test_ja4_alpn_and_signature_algorithms(self): |
| 420 |
client_hello_minimal = copy.copy(self.client_hello_minimal) |
| 421 |
client_hello_minimal.extensions.append( |
| 422 |
TlsExtensionApplicationLayerProtocolNegotiation([TlsProtocolName.H2]) |
| 423 |
) |
| 424 |
client_hello_minimal.extensions.append( |
| 425 |
TlsExtensionSignatureAlgorithms([TlsSignatureAndHashAlgorithm.RSA_PSS_RSAE_SHA256]) |
| 426 |
) |
| 427 |
|
| 428 |
signature_algorithm_hex = f'{TlsSignatureAndHashAlgorithm.RSA_PSS_RSAE_SHA256.value.code:04x}' |
| 429 |
cipher_hash = self._ja4_hash('0001,0002,0003,0004,0005') |
| 430 |
extension_hash = self._ja4_hash(f'000d_{signature_algorithm_hex}') |
| 431 |
result = client_hello_minimal.ja4() |
| 432 |
# the ALPN value "h2" is in the prefix; SNI (0000) and ALPN (0010) are excluded from the |
| 433 |
# extension hash, leaving signature_algorithms (000d) with the algorithms appended |
| 434 |
self.assertEqual(result.fingerprint, f't12i0502h2_{cipher_hash}_{extension_hash}') |
| 435 |
self.assertEqual( |
| 436 |
result.fingerprint_raw, f't12i0502h2_0001,0002,0003,0004,0005_000d_{signature_algorithm_hex}' |
| 437 |
) |
| 438 |
|
| 439 |
|
| 440 |
class TestTlsHandshakeServerHello(unittest.TestCase): |
| 441 |
def setUp(self): |
| 442 |
self.server_hello_minimal_dict = collections.OrderedDict([ |
| 443 |
('handshake_type', b'\x02'), # SERVER_HELLO |
| 444 |
('length', b'\x00\x00\x26'), |
| 445 |
('version', b'\x03\x03'), # TLS1_2 |
| 446 |
('random', |
| 447 |
b'\x5b\x6c\xd5\x80\x04\x05\x06\x07' + |
| 448 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 449 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 450 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 451 |
b''), |
| 452 |
('session_id_length', b'\x00'), |
| 453 |
('cipher_suite', b'\x00\x01'), |
| 454 |
('compression_method', b'\x00'), |
| 455 |
]) |
| 456 |
self.server_hello_minimal_bytes = b''.join(self.server_hello_minimal_dict.values()) |
| 457 |
|
| 458 |
self.server_hello_minimal = TlsHandshakeServerHello( |
| 459 |
TlsProtocolVersion(TlsVersion.TLS1_2), |
| 460 |
TlsHandshakeHelloRandom( |
| 461 |
datetime.datetime(2018, 8, 10, tzinfo=datetime.timezone.utc), |
| 462 |
TlsHandshakeHelloRandomBytes(bytearray( |
| 463 |
b'\x04\x05\x06\x07' + |
| 464 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 465 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 466 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 467 |
b'' |
| 468 |
)) |
| 469 |
), |
| 470 |
TlsSessionIdVector(()), |
| 471 |
TlsCompressionMethod.NULL, |
| 472 |
TlsCipherSuite.TLS_RSA_WITH_NULL_MD5, |
| 473 |
TlsExtensionsServer(()) |
| 474 |
) |
| 475 |
|
| 476 |
self.server_hello_minimal_bytes = b''.join(self.server_hello_minimal_dict.values()) |
| 477 |
self.server_hello_minimal_extensions_dict = collections.OrderedDict([ |
| 478 |
('extensions_length', b'\x00\x0a'), |
| 479 |
('extension_type', b'\x00\x2b'), # SUPPORTED_VERSIONS |
| 480 |
('extension_length', b'\x00\x05'), |
| 481 |
('selected_version', b'\x03\x03'), # TLS1_2 |
| 482 |
('extension_grease', b'\x0a\x0a'), |
| 483 |
('extension_grease_length', b'\x00\x00'), |
| 484 |
]) |
| 485 |
self.server_hello_minimal_extensions_bytes = b''.join(self.server_hello_minimal_extensions_dict.values()) |
| 486 |
self.server_hello_extension_bytes = bytearray( |
| 487 |
self.server_hello_minimal_bytes + |
| 488 |
self.server_hello_minimal_extensions_bytes + |
| 489 |
b'' |
| 490 |
) |
| 491 |
self.server_hello_extension_bytes[3] += ( |
| 492 |
len(self.server_hello_extension_bytes) - |
| 493 |
len(self.server_hello_minimal_bytes) |
| 494 |
) |
| 495 |
|
| 496 |
def test_parse(self): |
| 497 |
server_hello_minimal = TlsHandshakeServerHello.parse_exact_size(self.server_hello_minimal_bytes) |
| 498 |
|
| 499 |
self.assertEqual(server_hello_minimal.get_handshake_type(), TlsHandshakeType.SERVER_HELLO) |
| 500 |
|
| 501 |
self.assertEqual( |
| 502 |
server_hello_minimal.protocol_version, |
| 503 |
TlsProtocolVersion(TlsVersion.TLS1_2) |
| 504 |
) |
| 505 |
self.assertEqual( |
| 506 |
server_hello_minimal.random, |
| 507 |
self.server_hello_minimal.random |
| 508 |
) |
| 509 |
self.assertEqual( |
| 510 |
server_hello_minimal.cipher_suite, |
| 511 |
self.server_hello_minimal.cipher_suite |
| 512 |
) |
| 513 |
self.assertEqual( |
| 514 |
server_hello_minimal.compression_method, |
| 515 |
self.server_hello_minimal.compression_method |
| 516 |
) |
| 517 |
self.assertEqual( |
| 518 |
server_hello_minimal.extensions, |
| 519 |
self.server_hello_minimal.extensions |
| 520 |
) |
| 521 |
|
| 522 |
server_hello_extension = TlsHandshakeServerHello.parse_exact_size(self.server_hello_extension_bytes) |
| 523 |
self.assertEqual(len(server_hello_extension.extensions), 2) |
| 524 |
self.assertEqual( |
| 525 |
server_hello_extension.extensions.get_item_by_type(TlsExtensionType.SUPPORTED_VERSIONS), |
| 526 |
TlsExtensionSupportedVersionsServer(TlsProtocolVersion(TlsVersion.TLS1_2)) |
| 527 |
) |
| 528 |
self.assertEqual( |
| 529 |
server_hello_extension.extensions[1], |
| 530 |
TlsExtensionUnparsed(TlsInvalidTypeTwoByte(TlsGreaseTwoByte.GREASE_0A0A), b'') |
| 531 |
) |
| 532 |
with self.assertRaises(KeyError): |
| 533 |
server_hello_extension.extensions.get_item_by_type(TlsGreaseTwoByte.GREASE_0A0A) |
| 534 |
|
| 535 |
def test_compose(self): |
| 536 |
self.assertEqual( |
| 537 |
self.server_hello_minimal.compose(), |
| 538 |
self.server_hello_minimal_bytes |
| 539 |
) |
| 540 |
|
| 541 |
|
| 542 |
class TestTlsHandshakeHelloRetryRequest(unittest.TestCase): |
| 543 |
def setUp(self): |
| 544 |
self.hello_retry_request_minimal_dict = collections.OrderedDict([ |
| 545 |
('handshake_type', b'\x06'), # HELLO_RETRY_REQUEST |
| 546 |
('length', b'\x00\x00\x26'), |
| 547 |
('version', b'\x03\x03'), # TLS1_2 |
| 548 |
('random', |
| 549 |
b'\x5b\x6c\xd5\x80\x04\x05\x06\x07' + |
| 550 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 551 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 552 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 553 |
b''), |
| 554 |
('session_id_length', b'\x00'), |
| 555 |
('cipher_suite', b'\x00\x01'), |
| 556 |
('compression_method', b'\x00'), |
| 557 |
]) |
| 558 |
self.hello_retry_request_minimal_bytes = b''.join(self.hello_retry_request_minimal_dict.values()) |
| 559 |
|
| 560 |
self.hello_retry_request_minimal = TlsHandshakeHelloRetryRequest( |
| 561 |
TlsCipherSuite.TLS_RSA_WITH_NULL_MD5, |
| 562 |
TlsProtocolVersion(TlsVersion.TLS1_2), |
| 563 |
TlsHandshakeHelloRandom( |
| 564 |
datetime.datetime(2018, 8, 10, tzinfo=datetime.timezone.utc), |
| 565 |
TlsHandshakeHelloRandomBytes(bytearray( |
| 566 |
b'\x04\x05\x06\x07' + |
| 567 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 568 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 569 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 570 |
b'' |
| 571 |
)) |
| 572 |
), |
| 573 |
TlsSessionIdVector(()), |
| 574 |
TlsCompressionMethod.NULL, |
| 575 |
TlsExtensionsClient(()) |
| 576 |
) |
| 577 |
|
| 578 |
def test_parse(self): |
| 579 |
hello_retry_request_minimal = TlsHandshakeHelloRetryRequest.parse_exact_size( |
| 580 |
self.hello_retry_request_minimal_bytes |
| 581 |
) |
| 582 |
|
| 583 |
self.assertEqual(hello_retry_request_minimal.get_handshake_type(), TlsHandshakeType.HELLO_RETRY_REQUEST) |
| 584 |
|
| 585 |
self.assertEqual( |
| 586 |
hello_retry_request_minimal.protocol_version, |
| 587 |
TlsProtocolVersion(TlsVersion.TLS1_2) |
| 588 |
) |
| 589 |
self.assertEqual( |
| 590 |
hello_retry_request_minimal.random_bytes, |
| 591 |
self.hello_retry_request_minimal.random_bytes |
| 592 |
) |
| 593 |
self.assertEqual( |
| 594 |
hello_retry_request_minimal.cipher_suite, |
| 595 |
self.hello_retry_request_minimal.cipher_suite |
| 596 |
) |
| 597 |
self.assertEqual( |
| 598 |
hello_retry_request_minimal.compression_method, |
| 599 |
self.hello_retry_request_minimal.compression_method |
| 600 |
) |
| 601 |
self.assertEqual( |
| 602 |
hello_retry_request_minimal.extensions, |
| 603 |
self.hello_retry_request_minimal.extensions |
| 604 |
) |
| 605 |
|
| 606 |
def test_compose(self): |
| 607 |
self.assertEqual( |
| 608 |
self.hello_retry_request_minimal.compose(), |
| 609 |
self.hello_retry_request_minimal_bytes |
| 610 |
) |
| 611 |
|
| 612 |
def test_random(self): |
| 613 |
self.assertEqual( |
| 614 |
TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM.compose(), |
| 615 |
TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM_BYTES |
| 616 |
) |
| 617 |
self.assertEqual( |
| 618 |
TlsHandshakeHelloRandom.parse_exact_size(TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM_BYTES), |
| 619 |
TLS_HANDSHAKE_HELLO_RETRY_REQUEST_RANDOM |
| 620 |
) |
| 621 |
|
| 622 |
|
| 623 |
class TestTlsHandshakeServerCertificate(unittest.TestCase): |
| 624 |
def setUp(self): |
| 625 |
self.certificate_minimal_dict = collections.OrderedDict([ |
| 626 |
('handshake_type', b'\x0b'), # CERTIFICATE |
| 627 |
('length', b'\x00\x00\x31'), |
| 628 |
('cretificates', b'\x00\x00\x2e'), |
| 629 |
('peer_cretificate_length', b'\x00\x00\x10'), |
| 630 |
('peer_certificate_bytes', b'peer certificate'), |
| 631 |
('intermrdiate_cretificate_length', b'\x00\x00\x18'), |
| 632 |
('intermrdiate_certificate_bytes', b'intermediate certificate'), |
| 633 |
]) |
| 634 |
self.certificate_minimal_bytes = b''.join(self.certificate_minimal_dict.values()) |
| 635 |
|
| 636 |
self.certificate_minimal = TlsHandshakeServerCertificate( |
| 637 |
TlsCertificates([ |
| 638 |
TlsCertificate(b'peer certificate'), |
| 639 |
TlsCertificate(b'intermediate certificate'), |
| 640 |
]) |
| 641 |
) |
| 642 |
|
| 643 |
def test_parse(self): |
| 644 |
certificate_minimal = TlsHandshakeServerCertificate.parse_exact_size(self.certificate_minimal_bytes) |
| 645 |
|
| 646 |
self.assertEqual( |
| 647 |
certificate_minimal.certificate_chain, |
| 648 |
self.certificate_minimal.certificate_chain |
| 649 |
) |
| 650 |
|
| 651 |
def test_compose(self): |
| 652 |
self.assertEqual( |
| 653 |
self.certificate_minimal.compose(), |
| 654 |
self.certificate_minimal_bytes |
| 655 |
) |
| 656 |
|
| 657 |
|
| 658 |
class TestTlsHandshakeCertificate(unittest.TestCase): |
| 659 |
def setUp(self): |
| 660 |
self.certificate_minimal_dict = collections.OrderedDict([ |
| 661 |
('handshake_type', b'\x0b'), # CERTIFICATE |
| 662 |
('length', b'\x00\x00\x1a'), |
| 663 |
('certificate_request_context_length', b'\x01'), |
| 664 |
('certificate_request_context', b'\xaa'), |
| 665 |
('certificate_entries_length', b'\x00\x00\x15'), |
| 666 |
('entry_certificate_length', b'\x00\x00\x10'), |
| 667 |
('entry_certificate', b'first-cert-bytes'), |
| 668 |
('entry_extensions_length', b'\x00\x00'), |
| 669 |
]) |
| 670 |
self.certificate_minimal_bytes = b''.join(self.certificate_minimal_dict.values()) |
| 671 |
|
| 672 |
self.certificate_minimal = TlsHandshakeCertificate( |
| 673 |
certificate_request_context=b'\xaa', |
| 674 |
certificate_entries=TlsCertificateEntryVector([ |
| 675 |
TlsCertificateEntry( |
| 676 |
TlsCertificate(b'first-cert-bytes'), |
| 677 |
b'', |
| 678 |
) |
| 679 |
]), |
| 680 |
) |
| 681 |
|
| 682 |
def test_parse(self): |
| 683 |
certificate = TlsHandshakeCertificate.parse_exact_size(self.certificate_minimal_bytes) |
| 684 |
self.assertEqual( |
| 685 |
certificate.certificate_request_context, |
| 686 |
self.certificate_minimal.certificate_request_context |
| 687 |
) |
| 688 |
self.assertEqual( |
| 689 |
len(certificate.certificate_entries), |
| 690 |
len(self.certificate_minimal.certificate_entries) |
| 691 |
) |
| 692 |
|
| 693 |
def test_compose(self): |
| 694 |
self.assertEqual( |
| 695 |
self.certificate_minimal.compose(), |
| 696 |
self.certificate_minimal_bytes |
| 697 |
) |
| 698 |
|
| 699 |
def test_error_certificate_request_context_too_long(self): |
| 700 |
with self.assertRaises(InvalidValue) as context_manager: |
| 701 |
TlsHandshakeCertificate( |
| 702 |
certificate_request_context=b'\x00' * 256, |
| 703 |
certificate_entries=TlsCertificateEntryVector([]), |
| 704 |
) |
| 705 |
self.assertEqual(context_manager.exception.value, 256) |
| 706 |
|
| 707 |
def test_error_entry_extensions_too_long(self): |
| 708 |
with self.assertRaises(InvalidValue) as context_manager: |
| 709 |
TlsCertificateEntry( |
| 710 |
TlsCertificate(b'cert'), |
| 711 |
b'\x00' * (2 ** 16), |
| 712 |
) |
| 713 |
self.assertEqual(context_manager.exception.value, 2 ** 16) |
| 714 |
|
| 715 |
def test_error_parse_invalid_entries(self): |
| 716 |
bad_dict = collections.OrderedDict([ |
| 717 |
('handshake_type', b'\x0b'), # CERTIFICATE |
| 718 |
('length', b'\x00\x00\x05'), |
| 719 |
('certificate_request_context_length', b'\x01'), |
| 720 |
('certificate_request_context', b'\xaa'), |
| 721 |
('certificate_entries_length', b'\x00\x00\x0f'), # claims more data than available |
| 722 |
]) |
| 723 |
bad_bytes = b''.join(bad_dict.values()) |
| 724 |
with self.assertRaises(InvalidType): |
| 725 |
TlsHandshakeCertificate.parse_exact_size(bad_bytes) |
| 726 |
|
| 727 |
|
| 728 |
class TestTlsHandshakeCertificateRequestTls10(unittest.TestCase): |
| 729 |
def setUp(self): |
| 730 |
self.certificate_request_dict = collections.OrderedDict([ |
| 731 |
('handshake_type', b'\x0d'), # CERTIFICATE_REQUEST |
| 732 |
('length', b'\x00\x00\x19'), |
| 733 |
('certificate_types_length', b'\x04'), |
| 734 |
('certificate_types', b'\x01\02\x03\x04'), |
| 735 |
('certificate_authorities_length', b'\x00\x12'), |
| 736 |
('certificate_authority_length', b'\x00\x10'), |
| 737 |
('certificate_authority', b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 738 |
]) |
| 739 |
self.certificate_request_bytes = b''.join(self.certificate_request_dict.values()) |
| 740 |
|
| 741 |
self.certificate_request = TlsHandshakeCertificateRequest( |
| 742 |
certificate_types=[ |
| 743 |
TlsClientCertificateType.RSA_SIGN, |
| 744 |
TlsClientCertificateType.DSS_SIGN, |
| 745 |
TlsClientCertificateType.RSA_FIXED_DH, |
| 746 |
TlsClientCertificateType.DSS_FIXED_DH, |
| 747 |
], |
| 748 |
certificate_authorities=[ |
| 749 |
TlsDistinguishedName(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 750 |
] |
| 751 |
) |
| 752 |
|
| 753 |
def test_parse(self): |
| 754 |
certificate_request = TlsHandshakeCertificateRequest.parse_exact_size(self.certificate_request_bytes) |
| 755 |
|
| 756 |
self.assertEqual( |
| 757 |
list(certificate_request.certificate_types), |
| 758 |
[ |
| 759 |
TlsClientCertificateType.RSA_SIGN, |
| 760 |
TlsClientCertificateType.DSS_SIGN, |
| 761 |
TlsClientCertificateType.RSA_FIXED_DH, |
| 762 |
TlsClientCertificateType.DSS_FIXED_DH, |
| 763 |
] |
| 764 |
) |
| 765 |
self.assertEqual( |
| 766 |
list(certificate_request.certificate_authorities), |
| 767 |
[TlsDistinguishedName(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), ] |
| 768 |
) |
| 769 |
self.assertEqual(certificate_request.supported_signature_algorithms, None) |
| 770 |
|
| 771 |
def test_compose(self): |
| 772 |
self.assertEqual( |
| 773 |
self.certificate_request.compose(), |
| 774 |
self.certificate_request_bytes |
| 775 |
) |
| 776 |
|
| 777 |
|
| 778 |
class TestTlsHandshakeCertificateRequestTls12(unittest.TestCase): |
| 779 |
def setUp(self): |
| 780 |
self.certificate_request_dict = collections.OrderedDict([ |
| 781 |
('handshake_type', b'\x0d'), # CERTIFICATE_REQUEST |
| 782 |
('length', b'\x00\x00\x23'), |
| 783 |
('certificate_types_length', b'\x04'), |
| 784 |
('certificate_types', b'\x01\02\x03\x04'), |
| 785 |
('signature_algorithm_list_length', b'\x00\x08'), |
| 786 |
('signature_algorithm_list', b'\x01\x00\x02\x01\x03\x02\x04\x03'), |
| 787 |
('certificate_authorities_length', b'\x00\x12'), |
| 788 |
('certificate_authority_length', b'\x00\x10'), |
| 789 |
('certificate_authority', b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 790 |
]) |
| 791 |
self.certificate_request_bytes = b''.join(self.certificate_request_dict.values()) |
| 792 |
|
| 793 |
self.certificate_request = TlsHandshakeCertificateRequest( |
| 794 |
certificate_types=[ |
| 795 |
TlsClientCertificateType.RSA_SIGN, |
| 796 |
TlsClientCertificateType.DSS_SIGN, |
| 797 |
TlsClientCertificateType.RSA_FIXED_DH, |
| 798 |
TlsClientCertificateType.DSS_FIXED_DH, |
| 799 |
], |
| 800 |
supported_signature_algorithms=[ |
| 801 |
TlsSignatureAndHashAlgorithm.ANONYMOUS_MD5, |
| 802 |
TlsSignatureAndHashAlgorithm.RSA_SHA1, |
| 803 |
TlsSignatureAndHashAlgorithm.DSA_SHA224, |
| 804 |
TlsSignatureAndHashAlgorithm.ECDSA_SHA256, |
| 805 |
], |
| 806 |
certificate_authorities=[ |
| 807 |
TlsDistinguishedName(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), |
| 808 |
] |
| 809 |
) |
| 810 |
|
| 811 |
def test_parse(self): |
| 812 |
certificate_request = TlsHandshakeCertificateRequest.parse_exact_size(self.certificate_request_bytes) |
| 813 |
|
| 814 |
self.assertEqual( |
| 815 |
list(certificate_request.certificate_types), |
| 816 |
[ |
| 817 |
TlsClientCertificateType.RSA_SIGN, |
| 818 |
TlsClientCertificateType.DSS_SIGN, |
| 819 |
TlsClientCertificateType.RSA_FIXED_DH, |
| 820 |
TlsClientCertificateType.DSS_FIXED_DH, |
| 821 |
] |
| 822 |
) |
| 823 |
self.assertEqual( |
| 824 |
list(certificate_request.supported_signature_algorithms), |
| 825 |
[ |
| 826 |
TlsSignatureAndHashAlgorithm.ANONYMOUS_MD5, |
| 827 |
TlsSignatureAndHashAlgorithm.RSA_SHA1, |
| 828 |
TlsSignatureAndHashAlgorithm.DSA_SHA224, |
| 829 |
TlsSignatureAndHashAlgorithm.ECDSA_SHA256, |
| 830 |
] |
| 831 |
) |
| 832 |
self.assertEqual( |
| 833 |
list(certificate_request.certificate_authorities), |
| 834 |
[TlsDistinguishedName(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'), ] |
| 835 |
) |
| 836 |
|
| 837 |
def test_compose(self): |
| 838 |
self.assertEqual( |
| 839 |
self.certificate_request.compose(), |
| 840 |
self.certificate_request_bytes |
| 841 |
) |
| 842 |
|
| 843 |
|
| 844 |
class TestTlsHandshakeServerHelloDone(unittest.TestCase): |
| 845 |
def setUp(self): |
| 846 |
self.server_hello_done_dict = collections.OrderedDict([ |
| 847 |
('handshake_type', b'\x0e'), # SERVER_HELLO_DONE |
| 848 |
('length', b'\x00\x00\x00'), # 0x00 |
| 849 |
]) |
| 850 |
self.server_hello_done_bytes = b''.join(self.server_hello_done_dict.values()) |
| 851 |
|
| 852 |
self.server_hello_done = TlsHandshakeServerHelloDone() |
| 853 |
|
| 854 |
def test_error(self): |
| 855 |
error_regex = 'b\'\\\\x00\' is not a valid TlsHandshakeServerHelloDone payload value' |
| 856 |
with self.assertRaisesRegex(InvalidValue, error_regex): |
| 857 |
# pylint: disable=expression-not-assigned |
| 858 |
TlsHandshakeServerHelloDone.parse_exact_size(b'\x0e\x00\x00\x01\x00') |
| 859 |
|
| 860 |
def test_parse(self): |
| 861 |
server_hello_done = TlsHandshakeServerHelloDone.parse_exact_size(self.server_hello_done_bytes) |
| 862 |
|
| 863 |
self.assertEqual(server_hello_done.get_handshake_type(), TlsHandshakeType.SERVER_HELLO_DONE) |
| 864 |
|
| 865 |
def test_compose(self): |
| 866 |
self.assertEqual(self.server_hello_done.compose(), self.server_hello_done_bytes) |
| 867 |
|
| 868 |
|
| 869 |
class TestTlsHandshakeServerKeyExcahnge(unittest.TestCase): |
| 870 |
def setUp(self): |
| 871 |
self.param_bytes = b'\x00\x01\x02\x03\x04\x05\x06\x07' |
| 872 |
self.server_key_exchange_dict = collections.OrderedDict([ |
| 873 |
('handshake_type', b'\x0c'), # SERVER_KEY_EXCHANGE |
| 874 |
('length', b'\x00\x00\x08'), |
| 875 |
('param_bytes', self.param_bytes), |
| 876 |
]) |
| 877 |
self.server_key_exchange_bytes = b''.join(self.server_key_exchange_dict.values()) |
| 878 |
|
| 879 |
self.server_key_exchange = TlsHandshakeServerKeyExchange(self.param_bytes) |
| 880 |
|
| 881 |
def test_parse(self): |
| 882 |
server_key_exchange = TlsHandshakeServerKeyExchange.parse_exact_size(self.server_key_exchange_bytes) |
| 883 |
|
| 884 |
self.assertEqual(server_key_exchange.get_handshake_type(), TlsHandshakeType.SERVER_KEY_EXCHANGE) |
| 885 |
|
| 886 |
self.assertEqual(server_key_exchange.param_bytes, self.param_bytes) |
| 887 |
|
| 888 |
def test_compose(self): |
| 889 |
self.assertEqual(self.server_key_exchange.compose(), self.server_key_exchange_bytes) |
| 890 |
|
| 891 |
|
| 892 |
class TestTlsHandshakeCertificateStatus(unittest.TestCase): |
| 893 |
def setUp(self): |
| 894 |
self.status_bytes = b'\x00\x01\x02\x03\x04\x05\x06\x07' |
| 895 |
self.certificate_status_bytes = bytes( |
| 896 |
b'\x16' + # handshake_type = CERTIFICATE_STATUS |
| 897 |
b'\x00\x00\x0c' + # length = 0x0c |
| 898 |
b'\x01' + # status_type = OCSP |
| 899 |
b'\x00\x00\x08' + # length = 0x08 |
| 900 |
self.status_bytes + # status_bytes |
| 901 |
b'' |
| 902 |
) |
| 903 |
|
| 904 |
self.certificate_status = TlsHandshakeCertificateStatus(TlsCertificateStatusType.OCSP, self.status_bytes) |
| 905 |
|
| 906 |
def test_parse(self): |
| 907 |
certificate_status = TlsHandshakeCertificateStatus.parse_exact_size(self.certificate_status_bytes) |
| 908 |
|
| 909 |
self.assertEqual(certificate_status.get_handshake_type(), TlsHandshakeType.CERTIFICATE_STATUS) |
| 910 |
|
| 911 |
self.assertEqual(certificate_status.status, self.status_bytes) |
| 912 |
|
| 913 |
def test_compose(self): |
| 914 |
self.assertEqual(self.certificate_status.compose(), self.certificate_status_bytes) |
| 915 |
|
| 916 |
|
| 917 |
class TestTlsHandshakeEncryptedExtensions(unittest.TestCase): |
| 918 |
def setUp(self): |
| 919 |
self.extension_data = b'\x00\x10\x00\x0b\x00\x0c\x00\x00' |
| 920 |
self.encrypted_extensions_dict = collections.OrderedDict([ |
| 921 |
('handshake_type', b'\x08'), # ENCRYPTED_EXTENSIONS |
| 922 |
('length', b'\x00\x00\x08'), |
| 923 |
('extension_data', self.extension_data), |
| 924 |
]) |
| 925 |
self.encrypted_extensions_bytes = b''.join(self.encrypted_extensions_dict.values()) |
| 926 |
|
| 927 |
self.encrypted_extensions = TlsHandshakeEncryptedExtensions(self.extension_data) |
| 928 |
|
| 929 |
def test_parse(self): |
| 930 |
encrypted_extensions = TlsHandshakeEncryptedExtensions.parse_exact_size(self.encrypted_extensions_bytes) |
| 931 |
|
| 932 |
self.assertEqual(encrypted_extensions.get_handshake_type(), TlsHandshakeType.ENCRYPTED_EXTENSIONS) |
| 933 |
self.assertEqual(encrypted_extensions.extension_data, self.extension_data) |
| 934 |
|
| 935 |
def test_compose(self): |
| 936 |
self.assertEqual( |
| 937 |
self.encrypted_extensions.compose(), |
| 938 |
self.encrypted_extensions_bytes |
| 939 |
) |
| 940 |
|
| 941 |
|
| 942 |
class TestSslHandshakeClientHello(unittest.TestCase): |
| 943 |
def setUp(self): |
| 944 |
self.client_hello_dict = collections.OrderedDict([ |
| 945 |
('version', b'\x00\x02'), # SSL2 |
| 946 |
('cipher_kinds_length', b'\x00\x06'), |
| 947 |
('session_id_length', b'\x00\x08'), |
| 948 |
('challenge_length', b'\x00\x10'), |
| 949 |
('cipher_kinds', b'\x01\x00\x80\x07\x00\xc0'), |
| 950 |
('session_id', b'\x00\x01\x02\x03\x04\x05\x06\x07'), |
| 951 |
('challenge', |
| 952 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 953 |
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' + |
| 954 |
b'') |
| 955 |
]) |
| 956 |
self.client_hello_bytes = b''.join(self.client_hello_dict.values()) |
| 957 |
|
| 958 |
self.client_hello = SslHandshakeClientHello( |
| 959 |
cipher_kinds=[ |
| 960 |
SslCipherKind.SSL_CK_RC4_128_WITH_MD5, |
| 961 |
SslCipherKind.SSL_CK_DES_192_EDE3_CBC_WITH_MD5 |
| 962 |
], |
| 963 |
session_id=b'\x00\x01\x02\x03\x04\x05\x06\x07', |
| 964 |
challenge=b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' |
| 965 |
) |
| 966 |
|
| 967 |
def test_default(self): |
| 968 |
self.assertEqual(SslHandshakeClientHello([]).session_id, b'') |
| 969 |
self.assertNotEqual(SslHandshakeClientHello([]).challenge, SslHandshakeClientHello([]).challenge) |
| 970 |
|
| 971 |
self.assertEqual(SslHandshakeServerHello(b'', []).connection_id, b'') |
| 972 |
|
| 973 |
def test_parse(self): |
| 974 |
client_hello_minimal = SslHandshakeClientHello.parse_exact_size(self.client_hello_bytes) |
| 975 |
|
| 976 |
self.assertEqual(client_hello_minimal.get_message_type(), SslMessageType.CLIENT_HELLO) |
| 977 |
|
| 978 |
def test_compose(self): |
| 979 |
self.assertEqual(self.client_hello.compose(), self.client_hello_bytes) |
| 980 |
|
| 981 |
|
| 982 |
class TestSslHandshakeServerHello(unittest.TestCase): |
| 983 |
def setUp(self): |
| 984 |
self.server_hello_done_dict = collections.OrderedDict([ |
| 985 |
('session_id_hit', b'\x00'), # False |
| 986 |
('certificate_type', b'\x01'), # X509_CERTIFICATE |
| 987 |
('version', b'\x00\x02'), # SSL2 |
| 988 |
('certificate_length', b'\x00\x0b'), |
| 989 |
('cipher_kinds_length', b'\x00\x06'), |
| 990 |
('connection_id_length', b'\x00\x10'), |
| 991 |
('certificate', b'certificate'), |
| 992 |
('cipher_kinds', b'\x01\x00\x80\x07\x00\xc0'), |
| 993 |
('connection_id', |
| 994 |
b'\x00\x01\x02\x03\x04\x05\x06\x07' + |
| 995 |
b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' + |
| 996 |
b''), |
| 997 |
]) |
| 998 |
self.server_hello_bytes = b''.join(self.server_hello_done_dict.values()) |
| 999 |
|
| 1000 |
self.server_hello = SslHandshakeServerHello( |
| 1001 |
certificate=b'certificate', |
| 1002 |
cipher_kinds=[ |
| 1003 |
SslCipherKind.SSL_CK_RC4_128_WITH_MD5, |
| 1004 |
SslCipherKind.SSL_CK_DES_192_EDE3_CBC_WITH_MD5 |
| 1005 |
], |
| 1006 |
connection_id=b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f', |
| 1007 |
session_id_hit=False |
| 1008 |
) |
| 1009 |
|
| 1010 |
def test_parse(self): |
| 1011 |
server_hello_minimal = SslHandshakeServerHello.parse_exact_size(self.server_hello_bytes) |
| 1012 |
|
| 1013 |
self.assertEqual(server_hello_minimal.get_message_type(), SslMessageType.SERVER_HELLO) |
| 1014 |
|
| 1015 |
def test_compose(self): |
| 1016 |
self.assertEqual(self.server_hello.compose(), self.server_hello_bytes) |