| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import collections |
| 4 |
import datetime |
| 5 |
import os |
| 6 |
import unittest |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
from test.common.classes import TestClasses |
| 10 |
|
| 11 |
import asn1crypto |
| 12 |
|
| 13 |
from cryptodatahub.common.algorithm import Authentication, Hash, KeyExchange, Signature |
| 14 |
from cryptodatahub.common.parameter import ECParamWellKnown |
| 15 |
from cryptodatahub.common.key import ( |
| 16 |
_PublicKeySizeGradeable, |
| 17 |
PublicKey, |
| 18 |
PublicKeyParamsDsa, |
| 19 |
PublicKeyParamsEddsa, |
| 20 |
PublicKeyParamsEcdsa, |
| 21 |
PublicKeyParamsRsa, |
| 22 |
PublicKeySize, |
| 23 |
PublicKeyX509Base, |
| 24 |
convert_public_key_size, |
| 25 |
) |
| 26 |
from cryptodatahub.common.grade import AttackNamed, AttackType, Grade, Vulnerability |
| 27 |
from cryptodatahub.common.utils import bytes_from_hex_string |
| 28 |
|
| 29 |
from cryptodatahub.tls.algorithm import TlsExtensionType |
| 30 |
|
| 31 |
|
| 32 |
class TestPublicKeyConverter(unittest.TestCase): |
| 33 |
def test_error_invalid_type(self): |
| 34 |
original_value = '1234' |
| 35 |
converted_value = convert_public_key_size(KeyExchange.DHE)(original_value) |
| 36 |
self.assertEqual(id(original_value), id(convert_public_key_size(KeyExchange.DHE)(converted_value))) |
| 37 |
|
| 38 |
def test_error_invalid_value(self): |
| 39 |
original_value = -1 |
| 40 |
converted_value = convert_public_key_size(KeyExchange.DHE)(original_value) |
| 41 |
self.assertEqual(id(converted_value), id(convert_public_key_size(KeyExchange.DHE)(converted_value))) |
| 42 |
|
| 43 |
def test_none(self): |
| 44 |
converted_value = convert_public_key_size(KeyExchange.DHE)(None) |
| 45 |
self.assertEqual(converted_value, None) |
| 46 |
|
| 47 |
def test_convert(self): |
| 48 |
original_value = 2048 |
| 49 |
converted_value = convert_public_key_size(KeyExchange.DHE)(original_value) |
| 50 |
|
| 51 |
self.assertEqual(converted_value, PublicKeySize(KeyExchange.DHE, 2048)) |
| 52 |
|
| 53 |
def test_repr(self): |
| 54 |
self.assertEqual(repr(convert_public_key_size(KeyExchange.DHE)), '<public key size converter>') |
| 55 |
|
| 56 |
|
| 57 |
class TestPublicKeySizeGradeable(unittest.TestCase): |
| 58 |
def test_gradeable(self): |
| 59 |
self.assertTrue(len(_PublicKeySizeGradeable.get_gradeable_name())) |
| 60 |
|
| 61 |
|
| 62 |
class TestPublicKeySize(unittest.TestCase): |
| 63 |
@staticmethod |
| 64 |
def _get_vulnerability_values(key_type, key_size): |
| 65 |
return list(PublicKeySize(key_type, key_size).gradeables) |
| 66 |
|
| 67 |
def test_vulnerabilities(self): |
| 68 |
vulnerability_integer_factorization_weak = Vulnerability( |
| 69 |
attack_type=AttackType.INTEGER_FACTORIZATION, |
| 70 |
grade=Grade.WEAK, |
| 71 |
named=None |
| 72 |
) |
| 73 |
vulnerability_discrete_logarithm_insecure = Vulnerability( |
| 74 |
attack_type=AttackType.DISCRETE_LOGARITHM, |
| 75 |
grade=Grade.INSECURE, |
| 76 |
named=None |
| 77 |
) |
| 78 |
vulnerability_discrete_logarithm_weak = Vulnerability( |
| 79 |
attack_type=AttackType.DISCRETE_LOGARITHM, |
| 80 |
grade=Grade.WEAK, |
| 81 |
named=None |
| 82 |
) |
| 83 |
vulnerability_dheat = Vulnerability( |
| 84 |
attack_type=AttackType.DOS_ATTACK, |
| 85 |
grade=Grade.WEAK, |
| 86 |
named=AttackNamed.DHEAT_ATTACK |
| 87 |
) |
| 88 |
|
| 89 |
self.assertEqual(PublicKeySize(Authentication.RSA, 2048).gradeables, [_PublicKeySizeGradeable([])]) |
| 90 |
self.assertEqual( |
| 91 |
self._get_vulnerability_values(Authentication.RSA, 1024), |
| 92 |
[_PublicKeySizeGradeable([vulnerability_integer_factorization_weak])] |
| 93 |
) |
| 94 |
self.assertEqual( |
| 95 |
self._get_vulnerability_values(Authentication.RSA, 768), [_PublicKeySizeGradeable([Vulnerability( |
| 96 |
attack_type=AttackType.INTEGER_FACTORIZATION, |
| 97 |
grade=Grade.INSECURE, |
| 98 |
named=AttackNamed.FREAK |
| 99 |
)])] |
| 100 |
) |
| 101 |
|
| 102 |
self.assertEqual(PublicKeySize(Authentication.DSS, 2048).gradeables, [_PublicKeySizeGradeable([])]) |
| 103 |
self.assertEqual( |
| 104 |
self._get_vulnerability_values(Authentication.DSS, 1024), |
| 105 |
[_PublicKeySizeGradeable([vulnerability_integer_factorization_weak])] |
| 106 |
) |
| 107 |
self.assertEqual( |
| 108 |
self._get_vulnerability_values(Authentication.DSS, 768), [_PublicKeySizeGradeable([Vulnerability( |
| 109 |
attack_type=AttackType.INTEGER_FACTORIZATION, |
| 110 |
grade=Grade.INSECURE, |
| 111 |
named=None |
| 112 |
)])] |
| 113 |
|
| 114 |
) |
| 115 |
|
| 116 |
self.assertEqual(PublicKeySize(KeyExchange.DHE, 2048).gradeables, [_PublicKeySizeGradeable([])]) |
| 117 |
self.assertEqual( |
| 118 |
self._get_vulnerability_values(KeyExchange.DHE, 1024), |
| 119 |
[_PublicKeySizeGradeable([vulnerability_integer_factorization_weak])] |
| 120 |
) |
| 121 |
self.assertEqual( |
| 122 |
self._get_vulnerability_values(KeyExchange.DHE, 768), [_PublicKeySizeGradeable([Vulnerability( |
| 123 |
attack_type=AttackType.INTEGER_FACTORIZATION, |
| 124 |
grade=Grade.INSECURE, |
| 125 |
named=AttackNamed.WEAK_DH |
| 126 |
)])] |
| 127 |
) |
| 128 |
self.assertEqual( |
| 129 |
self._get_vulnerability_values(KeyExchange.DHE, 6144), |
| 130 |
[_PublicKeySizeGradeable([vulnerability_dheat])] |
| 131 |
) |
| 132 |
|
| 133 |
self.assertEqual(PublicKeySize(Authentication.ECDSA, 256).gradeables, [_PublicKeySizeGradeable([])]) |
| 134 |
self.assertEqual( |
| 135 |
self._get_vulnerability_values(Authentication.ECDSA, 160), |
| 136 |
[_PublicKeySizeGradeable([vulnerability_discrete_logarithm_weak])] |
| 137 |
) |
| 138 |
self.assertEqual( |
| 139 |
self._get_vulnerability_values(Authentication.ECDSA, 112), |
| 140 |
[_PublicKeySizeGradeable([vulnerability_discrete_logarithm_insecure])] |
| 141 |
) |
| 142 |
|
| 143 |
self.assertEqual(PublicKeySize(Authentication.EDDSA, 256).gradeables, [_PublicKeySizeGradeable([])]) |
| 144 |
self.assertEqual( |
| 145 |
self._get_vulnerability_values(Authentication.EDDSA, 160), |
| 146 |
[_PublicKeySizeGradeable([vulnerability_discrete_logarithm_weak])] |
| 147 |
) |
| 148 |
self.assertEqual( |
| 149 |
self._get_vulnerability_values(Authentication.EDDSA, 112), |
| 150 |
[_PublicKeySizeGradeable([vulnerability_discrete_logarithm_insecure])] |
| 151 |
) |
| 152 |
|
| 153 |
self.assertEqual(PublicKeySize(Authentication.PSK, 2048).gradeables, None) |
| 154 |
|
| 155 |
def test_str(self): |
| 156 |
self.assertEqual(str(PublicKeySize(Authentication.RSA, 1024)), '1024') |
| 157 |
self.assertEqual(str(PublicKeySize(Authentication.EDDSA, 256)), '256') |
| 158 |
|
| 159 |
|
| 160 |
class TestPublicKeyParamsEcdsa(TestClasses.TestKeyBase): |
| 161 |
def test_from_octet_bit_string(self): |
| 162 |
public_key = self._get_public_key('gitlab.com_ssh_ecdsa_key') |
| 163 |
public_key_params = public_key.params |
| 164 |
|
| 165 |
self.assertEqual( |
| 166 |
public_key_params, |
| 167 |
PublicKeyParamsEcdsa.from_octet_bit_string( |
| 168 |
public_key_params.key_parameter, |
| 169 |
public_key_params.octet_bit_string, |
| 170 |
) |
| 171 |
) |
| 172 |
|
| 173 |
|
| 174 |
class TestPublicKey(TestClasses.TestKeyBase): |
| 175 |
def test_from_params(self): |
| 176 |
public_key = self._get_public_key('gitlab.com_ssh_dsa_key') |
| 177 |
public_key_params = public_key.params |
| 178 |
params = PublicKeyParamsDsa( |
| 179 |
prime=public_key_params.prime, |
| 180 |
generator=public_key_params.generator, |
| 181 |
order=public_key_params.order, |
| 182 |
public_key_value=public_key_params.public_key_value, |
| 183 |
) |
| 184 |
public_key = PublicKey.from_params(params) |
| 185 |
self.assertEqual(params, public_key.params) |
| 186 |
self.assertEqual(public_key.key_size, 1024) |
| 187 |
|
| 188 |
public_key = self._get_public_key('gitlab.com_ssh_ecdsa_key') |
| 189 |
public_key_params = public_key.params |
| 190 |
params = PublicKeyParamsEcdsa( |
| 191 |
key_parameter=ECParamWellKnown.PRIME256V1, |
| 192 |
point_x=public_key_params.point_x, |
| 193 |
point_y=public_key_params.point_y, |
| 194 |
) |
| 195 |
public_key = PublicKey.from_params(params) |
| 196 |
self.assertEqual(params, public_key.params) |
| 197 |
self.assertEqual(public_key.key_size, 256) |
| 198 |
|
| 199 |
public_key = self._get_public_key('gitlab.com_ssh_eddsa_key') |
| 200 |
public_key_params = public_key.params |
| 201 |
params = PublicKeyParamsEddsa( |
| 202 |
key_parameter=public_key_params.key_parameter, |
| 203 |
key_data=public_key_params.key_data, |
| 204 |
) |
| 205 |
public_key = PublicKey.from_params(params) |
| 206 |
self.assertEqual(params, public_key.params) |
| 207 |
self.assertEqual(public_key.key_size, 256) |
| 208 |
self.assertEqual(public_key.params.key_parameter.value.field_size, 255) |
| 209 |
|
| 210 |
public_key = self._get_public_key('ssh_ed448_key') |
| 211 |
public_key_params = public_key.params |
| 212 |
params = PublicKeyParamsEddsa( |
| 213 |
key_parameter=public_key_params.key_parameter, |
| 214 |
key_data=public_key_params.key_data, |
| 215 |
) |
| 216 |
public_key = PublicKey.from_params(params) |
| 217 |
self.assertEqual(params, public_key.params) |
| 218 |
self.assertEqual(public_key.key_size, 456) |
| 219 |
self.assertEqual(public_key.params.key_parameter.value.field_size, 448) |
| 220 |
|
| 221 |
public_key = self._get_public_key('gitlab.com_ssh_rsa_key') |
| 222 |
public_key_params = public_key.params |
| 223 |
params = PublicKeyParamsRsa( |
| 224 |
modulus=public_key_params.modulus, |
| 225 |
public_exponent=public_key_params.public_exponent |
| 226 |
) |
| 227 |
public_key = PublicKey.from_params(params) |
| 228 |
self.assertEqual(params, public_key.params) |
| 229 |
self.assertEqual(public_key.key_size, 2048) |
| 230 |
|
| 231 |
def test_pem(self): |
| 232 |
public_key = self._get_public_key('snakeoil_cert_pubkey') |
| 233 |
self.assertEqual(public_key.pem, self._get_public_key_pem('snakeoil_cert_pubkey')) |
| 234 |
|
| 235 |
def test_der(self): |
| 236 |
public_key = self._get_public_key('snakeoil_cert_pubkey') |
| 237 |
self.assertEqual(public_key, PublicKey.from_der(public_key.der)) |
| 238 |
|
| 239 |
def test_digest(self): |
| 240 |
public_key = self._get_public_key('snakeoil_cert_pubkey') |
| 241 |
self.assertEqual( |
| 242 |
public_key.get_digest(Hash.MD5), |
| 243 |
bytes_from_hex_string('5D:B2:D9:9F:97:5C:C6:19:B3:91:7E:F8:1A:37:2C:78', ':') |
| 244 |
) |
| 245 |
|
| 246 |
def test_fingerprints(self): |
| 247 |
public_key = self._get_public_key('snakeoil_cert_pubkey') |
| 248 |
self.assertEqual( |
| 249 |
public_key.fingerprints, |
| 250 |
{ |
| 251 |
Hash.MD5: |
| 252 |
'5D:B2:D9:9F:97:5C:C6:19:B3:91:7E:F8:1A:37:2C:78', |
| 253 |
Hash.SHA1: |
| 254 |
'49:4A:82:89:7C:62:18:44:CF:02:A9:74:E9:57:CD:FC:A2:A4:45:9B', |
| 255 |
Hash.SHA2_256: |
| 256 |
'29:D4:FC:11:59:74:C3:47:25:25:E7:11:77:44:7C:7A:' + |
| 257 |
'D3:68:A0:79:70:E5:2D:58:1E:54:7A:60:F9:EA:AF:F4', |
| 258 |
} |
| 259 |
) |
| 260 |
|
| 261 |
def test_key_type(self): |
| 262 |
public_key_x509 = self._get_public_key_x509('ecc256.badssl.com') |
| 263 |
self.assertEqual(public_key_x509.key_type, Authentication.ECDSA) |
| 264 |
public_key_x509 = self._get_public_key_x509('ecc384.badssl.com') |
| 265 |
self.assertEqual(public_key_x509.key_type, Authentication.ECDSA) |
| 266 |
|
| 267 |
public_key_x509 = self._get_public_key_x509('rsa2048.badssl.com') |
| 268 |
self.assertEqual(public_key_x509.key_type, Authentication.RSA) |
| 269 |
public_key_x509 = self._get_public_key_x509('rsa4096.badssl.com') |
| 270 |
self.assertEqual(public_key_x509.key_type, Authentication.RSA) |
| 271 |
public_key_x509 = self._get_public_key_x509('rsa8192.badssl.com') |
| 272 |
self.assertEqual(public_key_x509.key_type, Authentication.RSA) |
| 273 |
|
| 274 |
def test_key_size(self): |
| 275 |
public_key_x509 = self._get_public_key_x509('ecc256.badssl.com') |
| 276 |
self.assertEqual(public_key_x509.key_size, 256) |
| 277 |
public_key_x509 = self._get_public_key_x509('ecc384.badssl.com') |
| 278 |
self.assertEqual(public_key_x509.key_size, 384) |
| 279 |
|
| 280 |
public_key_x509 = self._get_public_key_x509('rsa2048.badssl.com') |
| 281 |
self.assertEqual(public_key_x509.key_size, 2048) |
| 282 |
public_key_x509 = self._get_public_key_x509('rsa4096.badssl.com') |
| 283 |
self.assertEqual(public_key_x509.key_size, 4096) |
| 284 |
public_key_x509 = self._get_public_key_x509('rsa8192.badssl.com') |
| 285 |
self.assertEqual(public_key_x509.key_size, 8192) |
| 286 |
|
| 287 |
self.assertEqual(self._get_public_key_x509('gost_2001_cert').key_size, 256) |
| 288 |
self.assertEqual(self._get_public_key_x509('gost_2012_256_cert').key_size, 256) |
| 289 |
self.assertEqual(self._get_public_key_x509('gost_2012_512_cert').key_size, 512) |
| 290 |
|
| 291 |
def test_asdict(self): |
| 292 |
public_key_x509 = self._get_public_key('gitlab.com_ssh_rsa_key') |
| 293 |
self.assertEqual(public_key_x509._asdict(), collections.OrderedDict([ |
| 294 |
('algorithm', Authentication.RSA), |
| 295 |
('size', PublicKeySize(key_type=Authentication.RSA, value=2048)), |
| 296 |
('fingerprints', collections.OrderedDict([ |
| 297 |
(Hash.MD5, '94:33:7E:9C:56:1B:CE:19:FF:D3:50:DA:D4:AA:45:D7'), |
| 298 |
(Hash.SHA1, 'BC:14:22:BF:0B:65:89:FB:9E:1C:95:C3:B6:5C:2A:16:1D:82:DB:AE'), |
| 299 |
( |
| 300 |
Hash.SHA2_256, |
| 301 |
'34:8E:31:C3:D5:88:1E:09:A4:A9:30:8A:90:D9:46:5B:73:07:79:DE:DA:D2:D8:F2:C3:81:F2:54:24:6B:73:D7' |
| 302 |
) |
| 303 |
])) |
| 304 |
])) |
| 305 |
|
| 306 |
|
| 307 |
class TestPublicKeyX509(TestClasses.TestKeyBase): # pylint: disable=too-many-public-methods |
| 308 |
def test_eq(self): |
| 309 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 310 |
self.assertEqual(public_key_x509, public_key_x509) |
| 311 |
|
| 312 |
self.assertNotEqual(public_key_x509, self._get_public_key_x509('no-common-name.badssl.com')) |
| 313 |
|
| 314 |
public_key_x509 = self._get_public_key_x509('snakeoil_cert') |
| 315 |
public_key = self._get_public_key('snakeoil_cert_pubkey') |
| 316 |
self.assertEqual(public_key_x509.public_key, public_key) |
| 317 |
|
| 318 |
def test_pem_lines(self): |
| 319 |
self.assertEqual( |
| 320 |
PublicKeyX509Base.from_pem_lines(self._get_public_key_pem('expired.badssl.com').split(os.linesep)), |
| 321 |
self._get_public_key_x509('expired.badssl.com') |
| 322 |
) |
| 323 |
|
| 324 |
def test_pem(self): |
| 325 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 326 |
self.assertEqual(public_key_x509.pem, self._get_public_key_pem('expired.badssl.com')) |
| 327 |
|
| 328 |
def test_der(self): |
| 329 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 330 |
self.assertEqual(public_key_x509, PublicKeyX509Base.from_der(public_key_x509.der)) |
| 331 |
|
| 332 |
def test_digest(self): |
| 333 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 334 |
self.assertEqual( |
| 335 |
public_key_x509.get_digest(Hash.MD5), |
| 336 |
bytes_from_hex_string('67:34:4E:61:C0:43:1C:F1:F7:25:7C:1D:6D:E7:A7:85', ':') |
| 337 |
) |
| 338 |
|
| 339 |
def test_fingerprints(self): |
| 340 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 341 |
self.assertEqual( |
| 342 |
public_key_x509.fingerprints, |
| 343 |
{ |
| 344 |
Hash.MD5: |
| 345 |
'67:34:4E:61:C0:43:1C:F1:F7:25:7C:1D:6D:E7:A7:85', |
| 346 |
Hash.SHA1: |
| 347 |
'40:4B:BD:2F:1F:4C:C2:FD:EE:F1:3A:AB:DD:52:3E:F6:1F:1C:71:F3', |
| 348 |
Hash.SHA2_256: |
| 349 |
'BA:10:5C:E0:2B:AC:76:88:8E:CE:E4:7C:D4:EB:79:41:' + |
| 350 |
'65:3E:9A:C9:93:B6:1B:2E:B3:DC:C8:20:14:D2:1B:4F', |
| 351 |
} |
| 352 |
) |
| 353 |
|
| 354 |
def test_common_name(self): |
| 355 |
public_key_x509 = self._get_public_key_x509('no-common-name.badssl.com') |
| 356 |
self.assertNotEqual(public_key_x509.subject, collections.OrderedDict([])) |
| 357 |
self.assertEqual(public_key_x509.subject_alternative_names, ['no-common-name.badssl.com', ]) |
| 358 |
self.assertEqual(public_key_x509.valid_domains, ['no-common-name.badssl.com', ]) |
| 359 |
|
| 360 |
def test_subject_alternative_names(self): |
| 361 |
public_key_x509 = self._get_public_key_x509('no-subject.badssl.com') |
| 362 |
self.assertEqual(public_key_x509.subject, collections.OrderedDict([])) |
| 363 |
self.assertEqual(public_key_x509.subject_alternative_names, ['no-subject.badssl.com']) |
| 364 |
self.assertEqual(public_key_x509.valid_domains, ['no-subject.badssl.com']) |
| 365 |
|
| 366 |
public_key_x509 = self._get_public_key_x509('badssl.com') |
| 367 |
self.assertNotEqual(public_key_x509.subject, collections.OrderedDict([])) |
| 368 |
self.assertEqual(public_key_x509.subject_alternative_names, ['*.badssl.com', 'badssl.com']) |
| 369 |
self.assertEqual(public_key_x509.valid_domains, ['*.badssl.com', 'badssl.com']) |
| 370 |
|
| 371 |
public_key_x509 = self._get_public_key_x509('gost_2001_cert') |
| 372 |
self.assertNotEqual(public_key_x509.subject, collections.OrderedDict([])) |
| 373 |
self.assertEqual(public_key_x509.subject_alternative_names, []) |
| 374 |
|
| 375 |
def test_no_subject(self): |
| 376 |
public_key_x509 = self._get_public_key_x509('no-subject.badssl.com') |
| 377 |
self.assertEqual(public_key_x509.subject_alternative_names, ['no-subject.badssl.com', ]) |
| 378 |
self.assertEqual(public_key_x509.valid_domains, ['no-subject.badssl.com', ]) |
| 379 |
|
| 380 |
def test_issuer(self): |
| 381 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 382 |
self.assertEqual( |
| 383 |
public_key_x509.issuer, |
| 384 |
collections.OrderedDict([ |
| 385 |
('country_name', 'GB'), |
| 386 |
('state_or_province_name', 'Greater Manchester'), |
| 387 |
('locality_name', 'Salford'), |
| 388 |
('organization_name', 'COMODO CA Limited'), |
| 389 |
('common_name', 'COMODO RSA Domain Validation Secure Server CA') |
| 390 |
]) |
| 391 |
) |
| 392 |
|
| 393 |
def test_crl_distribution_points(self): |
| 394 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 395 |
self.assertEqual( |
| 396 |
public_key_x509.crl_distribution_points, |
| 397 |
['http://crl.comodoca.com/COMODORSADomainValidationSecureServerCA.crl'] |
| 398 |
) |
| 399 |
|
| 400 |
public_key_x509 = self._get_public_key_x509('letsencrypt.org') |
| 401 |
self.assertEqual( |
| 402 |
public_key_x509.crl_distribution_points, |
| 403 |
[] |
| 404 |
) |
| 405 |
|
| 406 |
def test_crl_distribution_points_relative_name(self): |
| 407 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 408 |
self.assertEqual( |
| 409 |
public_key_x509.crl_distribution_points, |
| 410 |
['http://crl.comodoca.com/COMODORSADomainValidationSecureServerCA.crl', ] |
| 411 |
) |
| 412 |
|
| 413 |
def test_ocsp_responders(self): |
| 414 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 415 |
self.assertEqual(public_key_x509.ocsp_responders, ['http://ocsp.comodoca.com']) |
| 416 |
|
| 417 |
@mock.patch.object( |
| 418 |
asn1crypto.x509.Certificate, 'authority_information_access_value', |
| 419 |
return_value=None |
| 420 |
) |
| 421 |
def test_ocsp_responders_no_extension(self, _): |
| 422 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 423 |
self.assertEqual(public_key_x509.ocsp_responders, []) |
| 424 |
|
| 425 |
def test_is_ca(self): |
| 426 |
public_key_x509 = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 427 |
self.assertTrue(public_key_x509.is_ca) |
| 428 |
|
| 429 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 430 |
self.assertFalse(public_key_x509.is_ca) |
| 431 |
|
| 432 |
def test_self_signed(self): |
| 433 |
public_key_x509 = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 434 |
self.assertTrue(public_key_x509.is_self_signed) |
| 435 |
|
| 436 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 437 |
self.assertFalse(public_key_x509.is_self_signed) |
| 438 |
|
| 439 |
def test_validity(self): |
| 440 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 441 |
self.assertTrue(public_key_x509.expired) |
| 442 |
self.assertEqual( |
| 443 |
public_key_x509.valid_not_before, |
| 444 |
datetime.datetime(2015, 4, 9, 0, 0, tzinfo=asn1crypto.util.timezone.utc) |
| 445 |
) |
| 446 |
self.assertEqual( |
| 447 |
public_key_x509.valid_not_after, |
| 448 |
datetime.datetime(2015, 4, 12, 23, 59, 59, tzinfo=asn1crypto.util.timezone.utc) |
| 449 |
) |
| 450 |
self.assertEqual( |
| 451 |
public_key_x509.validity_period, |
| 452 |
datetime.timedelta(days=4, seconds=-1) |
| 453 |
) |
| 454 |
self.assertEqual( |
| 455 |
public_key_x509.validity_remaining_time, |
| 456 |
None |
| 457 |
) |
| 458 |
|
| 459 |
def test_public_key_pin(self): |
| 460 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 461 |
self.assertEqual( |
| 462 |
public_key_x509.public_key_pin, |
| 463 |
'9SLklscvzMYj8f+52lp5ze/hY0CFHyLSPQzSpYYIBm8=' |
| 464 |
) |
| 465 |
|
| 466 |
def test_extended_validation(self): |
| 467 |
public_key_x509 = self._get_public_key_x509('extended-validation.badssl.com') |
| 468 |
self.assertTrue(public_key_x509.extended_validation) |
| 469 |
|
| 470 |
public_key_x509 = self._get_public_key_x509('badssl.com') |
| 471 |
self.assertFalse(public_key_x509.extended_validation) |
| 472 |
|
| 473 |
with mock.patch( |
| 474 |
'asn1crypto.x509.Certificate.certificate_policies_value', |
| 475 |
new_callable=mock.PropertyMock |
| 476 |
) as prop_mock: |
| 477 |
prop_mock.return_value = None |
| 478 |
self.assertFalse(public_key_x509.extended_validation) |
| 479 |
|
| 480 |
def test_tls_features(self): |
| 481 |
public_key_x509 = self._get_public_key_x509('badssl.com') |
| 482 |
self.assertEqual(public_key_x509.tls_features, []) |
| 483 |
|
| 484 |
public_key_x509 = self._get_public_key_x509('bitnami.com') |
| 485 |
self.assertEqual(public_key_x509.tls_features, [TlsExtensionType.STATUS_REQUEST]) |
| 486 |
|
| 487 |
def test_signature_algorithm_unknown(self): |
| 488 |
public_key_x509 = self._get_public_key_x509('sha1-intermediate.badssl.com') |
| 489 |
with mock.patch('asn1crypto.algos.SignedDigestAlgorithmId.dotted', new_callable=mock.PropertyMock) as prop_mock: |
| 490 |
prop_mock.side_effect = KeyError('1.2.840.113549.1.1.2') |
| 491 |
self.assertEqual( |
| 492 |
public_key_x509.signature_hash_algorithm, |
| 493 |
Signature.RSA_WITH_MD2 |
| 494 |
) |
| 495 |
|
| 496 |
def test_signature_algorithm(self): |
| 497 |
public_key_x509 = self._get_public_key_x509('comodo_ssl_ca_sha1') |
| 498 |
self.assertEqual( |
| 499 |
public_key_x509.signature_hash_algorithm, |
| 500 |
Signature.RSA_WITH_SHA1 |
| 501 |
) |
| 502 |
|
| 503 |
public_key_x509 = self._get_public_key_x509('sha256.badssl.com') |
| 504 |
self.assertEqual( |
| 505 |
public_key_x509.signature_hash_algorithm, |
| 506 |
Signature.RSA_WITH_SHA2_256 |
| 507 |
) |
| 508 |
public_key_x509 = self._get_public_key_x509('sha384.badssl.com') |
| 509 |
self.assertEqual( |
| 510 |
public_key_x509.signature_hash_algorithm, |
| 511 |
Signature.RSA_WITH_SHA2_384 |
| 512 |
) |
| 513 |
public_key_x509 = self._get_public_key_x509('sha512.badssl.com') |
| 514 |
self.assertEqual(public_key_x509.signature_hash_algorithm, Signature.RSA_WITH_SHA2_512) |
| 515 |
|
| 516 |
def test_subject_match(self): |
| 517 |
public_key_x509 = self._get_public_key_x509('expired.badssl.com') |
| 518 |
self.assertTrue(public_key_x509.is_subject_matches('expired.badssl.com')) |
| 519 |
self.assertTrue(public_key_x509.is_subject_matches('whatever.badssl.com')) |
| 520 |
self.assertFalse(public_key_x509.is_subject_matches('what.ever.badssl.com')) |
| 521 |
|
| 522 |
def test_asdict(self): |
| 523 |
public_key_x509 = self._get_public_key_x509('rsa2048.badssl.com') |
| 524 |
self.assertEqual(public_key_x509._asdict(), collections.OrderedDict([ |
| 525 |
('algorithm', Authentication.RSA), |
| 526 |
('size', PublicKeySize( |
| 527 |
key_type=Authentication.RSA, |
| 528 |
value=2048, |
| 529 |
)), |
| 530 |
('version', 'v3'), |
| 531 |
('serial_number', '398674370847804942957222553262670217215220'), |
| 532 |
('subject', collections.OrderedDict([('common_name', '*.badssl.com')])), |
| 533 |
('subject_alternative_names', ['*.badssl.com', 'badssl.com']), |
| 534 |
('issuer', collections.OrderedDict([ |
| 535 |
('country_name', 'US'), |
| 536 |
('organization_name', "Let's Encrypt"), |
| 537 |
('common_name', 'R3') |
| 538 |
])), |
| 539 |
('signature_hash_algorithm', Signature.RSA_WITH_SHA2_256), |
| 540 |
('validity', collections.OrderedDict([ |
| 541 |
('not_before', '2023-04-23 23:00:10+00:00'), |
| 542 |
('not_after', '2023-07-22 23:00:09+00:00'), |
| 543 |
('period', '89 days, 23:59:59'), |
| 544 |
('remaining', None) |
| 545 |
])), |
| 546 |
('revocation', collections.OrderedDict([ |
| 547 |
('crl_distribution_points', []), |
| 548 |
('ocsp_responders', ['http://r3.o.lencr.org']) |
| 549 |
])), |
| 550 |
('fingerprints', collections.OrderedDict([ |
| 551 |
(Hash.MD5, 'E5:CE:F4:ED:BB:42:C4:59:01:63:29:4B:DD:55:64:E2'), |
| 552 |
(Hash.SHA1, 'B8:68:EC:8E:5C:9F:C2:EC:B2:E5:A7:12:C5:B8:F2:34:B7:33:CD:44'), |
| 553 |
( |
| 554 |
Hash.SHA2_256, |
| 555 |
'17:7D:74:6A:01:B5:8C:CD:E4:5D:F9:09:48:E4:01:72:4D:83:6A:C6:E1:44:7F:11:2F:7E:20:C3:40:C0:2A:63' |
| 556 |
) |
| 557 |
])), |
| 558 |
('public_key_pin', '6Lkip0FxqykIPcMKjlwSSxNYRqG1EHcSNLMvN4uV1zc='), |
| 559 |
('end_entity', collections.OrderedDict([ |
| 560 |
('extended_validation', False), |
| 561 |
('tls_features', []) |
| 562 |
])) |
| 563 |
])) |