Debian Codegraph @

test/common/test_x509.py

raw
1 # SPDX-License-Identifier: MPL-2.0
2
3 import hashlib
4
5 from test.common.classes import TestClasses
6
7 from cryptodatahub.common.key import PublicKeyX509Base
8 from cryptodatahub.common.entity import Entity
9
10 from cryptoparser.common.x509 import SignedCertificateTimestampList
11
12
13 class TestTlsPubKeys(TestClasses.TestKeyBase):
14 def test_signed_certificate_timestamps(self):
15 certificate = self._get_public_key_x509('rsa8192.badssl.com_root_ca.crt')
16 self.assertEqual(certificate.signed_certificate_timestamps, SignedCertificateTimestampList([]))
17
18 certificate = self._get_public_key_x509('rsa8192.badssl.com_certificate.crt')
19 self.assertIn(
20 Entity.GOOGLE,
21 [sct.log.operator for sct in certificate.signed_certificate_timestamps]
22 )
23
24 def test_ja4x(self):
25 certificate = self._get_public_key_x509('rsa8192.badssl.com_certificate.crt')
26 ja4x = certificate.ja4x
27
28 issuer_oid_hexes, subject_oid_hexes, extension_oid_hexes = ja4x.fingerprint_raw.split('_')
29 # the subject relative distinguished names contain the common name (OID 2.5.4.3 -> 550403)
30 self.assertIn('550403', subject_oid_hexes.split(','))
31 self.assertIn('550403', issuer_oid_hexes.split(','))
32 self.assertTrue(extension_oid_hexes)
33
34 # the fingerprint is the per-section truncated SHA-256 of the raw OID lists
35 self.assertEqual(ja4x.fingerprint, '_'.join(
36 hashlib.sha256(oid_hexes.encode('ascii')).hexdigest()[:12]
37 for oid_hexes in ja4x.fingerprint_raw.split('_')
38 ))
39 self.assertEqual(certificate.ja4x, ja4x)
40
41 def test_asdict(self):
42 certificate = self._get_public_key_x509('rsa8192.badssl.com_certificate.crt')
43 dict_result = certificate._asdict()
44 self.assertIn(
45 Entity.GOOGLE,
46 [sct.log.operator for sct in dict_result.pop('signed_certificate_timestamps')],
47 )
48 dict_result.pop('ja4x')
49
50 self.assertEqual(PublicKeyX509Base._asdict(certificate), dict_result)