| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
# pylint: disable=too-many-lines |
| 3 |
|
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import argparse |
| 7 |
import base64 |
| 8 |
import collections |
| 9 |
import csv |
| 10 |
import datetime |
| 11 |
import io |
| 12 |
import json |
| 13 |
import os |
| 14 |
import tarfile |
| 15 |
|
| 16 |
from test.common.classes import TestClasses |
| 17 |
from test.updaters.classes import MockSelectedStoreFetcher |
| 18 |
|
| 19 |
import asn1crypto.cms |
| 20 |
import asn1crypto.core |
| 21 |
import asn1crypto.pkcs12 |
| 22 |
import asn1crypto.x509 |
| 23 |
|
| 24 |
from cryptodatahub.common.algorithm import Hash |
| 25 |
from cryptodatahub.common.entity import Entity |
| 26 |
from cryptodatahub.common.fetcher import ( |
| 27 |
CertificatePemFetcher, |
| 28 |
FetcherRootCertificateStore, |
| 29 |
FetcherRootCertificateStoreApple, |
| 30 |
FetcherRootCertificateStoreAndroid, |
| 31 |
FetcherRootCertificateStoreChrome, |
| 32 |
FetcherRootCertificateStoreMicrosoft, |
| 33 |
FetcherRootCertificateStoreMozilla, |
| 34 |
FetcherRootCertificateStoreOpenJDK, |
| 35 |
FetcherRootCertificateStoreOracleJDK, |
| 36 |
) |
| 37 |
from cryptodatahub.common.stores import ( |
| 38 |
CertificateTrustConstraint, |
| 39 |
RootCertificateBase, |
| 40 |
RootCertificateParams, |
| 41 |
RootCertificateTrustConstraintAction, |
| 42 |
RootCertificateTrustStoreConstraint, |
| 43 |
RootCertificate, |
| 44 |
) |
| 45 |
from cryptodatahub.common.types import Base64Data |
| 46 |
from cryptodatahub.common.utils import HttpFetcher |
| 47 |
|
| 48 |
from updaters.common import UpdaterBase |
| 49 |
from updaters.trust_stores import ( |
| 50 |
RootCertificateStore, |
| 51 |
UpdaterRootCertificateTrustStore, |
| 52 |
_parse_trust_store_owner, |
| 53 |
main, |
| 54 |
) |
| 55 |
import updaters.trust_stores as trust_stores_module |
| 56 |
|
| 57 |
|
| 58 |
class TestRootCertificateBase(TestClasses.TestKeyBase): |
| 59 |
def setUp(self): |
| 60 |
super().setUp() |
| 61 |
|
| 62 |
# Mock CertificatePemFetcher to prevent external HTTP calls |
| 63 |
self.mock_certificate_pem_fetcher = mock.Mock(spec=CertificatePemFetcher) |
| 64 |
self.mock_certificate_pem_fetcher.return_value = '' # Return empty string for certificate PEMs |
| 65 |
self.certificate_pem_fetcher_patcher = mock.patch( |
| 66 |
'cryptodatahub.common.fetcher.CertificatePemFetcher', |
| 67 |
return_value=self.mock_certificate_pem_fetcher |
| 68 |
) |
| 69 |
self.certificate_pem_fetcher_patcher.start() |
| 70 |
self.addCleanup(self.certificate_pem_fetcher_patcher.stop) |
| 71 |
|
| 72 |
self.public_key_x509_lets_encrypt = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 73 |
self.public_key_x509_snakeoil_ca = self._get_public_key_x509('snakeoil_ca_cert') |
| 74 |
|
| 75 |
@staticmethod |
| 76 |
def _get_mock_data_mozilla(public_keys=(), options=None): |
| 77 |
mock_data = io.StringIO() |
| 78 |
dict_writer = csv.DictWriter( |
| 79 |
mock_data, FetcherRootCertificateStoreMozilla.CSV_FIELDS, |
| 80 |
quotechar='"', quoting=csv.QUOTE_ALL |
| 81 |
) |
| 82 |
dict_writer.writeheader() |
| 83 |
|
| 84 |
if options is None: |
| 85 |
options = len(public_keys) * [{}] |
| 86 |
|
| 87 |
for i, public_key in enumerate(public_keys): |
| 88 |
dict_writer.writerow(dict(options[i], **{'PEM': public_key.pem})) |
| 89 |
|
| 90 |
return mock_data.getvalue().encode('ascii') |
| 91 |
|
| 92 |
@staticmethod |
| 93 |
def _get_mock_data_microsoft(public_keys=(), options=None): |
| 94 |
mock_data = io.StringIO() |
| 95 |
dict_writer = csv.DictWriter( |
| 96 |
mock_data, FetcherRootCertificateStoreMicrosoft.CSV_FIELDS, |
| 97 |
quotechar='"', quoting=csv.QUOTE_ALL |
| 98 |
) |
| 99 |
dict_writer.writeheader() |
| 100 |
|
| 101 |
if options is None: |
| 102 |
options = len(public_keys) * [{}] |
| 103 |
|
| 104 |
for i, public_key in enumerate(public_keys): |
| 105 |
dict_writer.writerow(dict(options[i], **{'PEM': public_key.pem})) |
| 106 |
|
| 107 |
return mock_data.getvalue().encode('ascii') |
| 108 |
|
| 109 |
@staticmethod |
| 110 |
def _get_mock_data_apple(public_keys=()): |
| 111 |
mock_data = os.linesep.join([ |
| 112 |
'<h2 id="trusted">Trusted Certificates</h2>', |
| 113 |
'<tbody>', |
| 114 |
'<tr>', |
| 115 |
] + [ |
| 116 |
f'<th>{field_name}</th>' |
| 117 |
for field_name in FetcherRootCertificateStoreApple.FIELDS |
| 118 |
] + [ |
| 119 |
'</tr>', |
| 120 |
]) |
| 121 |
|
| 122 |
for public_key in public_keys: |
| 123 |
mock_data += '<tr>' + os.linesep |
| 124 |
for field_name in FetcherRootCertificateStoreApple.FIELDS: |
| 125 |
if field_name == 'Fingerprint (SHA-256)': |
| 126 |
data = public_key.fingerprints[Hash.SHA2_256].replace(':', ' ') |
| 127 |
else: |
| 128 |
data = field_name |
| 129 |
|
| 130 |
mock_data += f'<td>{data}</td>{os.linesep}' |
| 131 |
mock_data += '</tr>' + os.linesep |
| 132 |
|
| 133 |
return mock_data.encode('ascii') |
| 134 |
|
| 135 |
@staticmethod |
| 136 |
def _get_mock_data_android(public_keys=()): |
| 137 |
commit_log = b")]}\'\n" + json.dumps({'log': [{'commit': 'deadbeef'}]}).encode('ascii') |
| 138 |
entries = [ |
| 139 |
{'name': public_key.fingerprints[Hash.SHA2_256], 'type': 'blob'} |
| 140 |
for public_key in public_keys |
| 141 |
] |
| 142 |
listing = b")]}'\n" + json.dumps({'entries': entries}).encode('ascii') |
| 143 |
|
| 144 |
file_contents = [ |
| 145 |
base64.b64encode(public_key.pem.encode('ascii')) |
| 146 |
for public_key in public_keys |
| 147 |
] |
| 148 |
|
| 149 |
return [commit_log, listing] + file_contents |
| 150 |
|
| 151 |
@staticmethod |
| 152 |
def _get_mock_data_chrome(public_keys=()): |
| 153 |
commit_log = b")]}\'\n" + json.dumps({'log': [{'commit': 'deadbeef'}]}).encode('ascii') |
| 154 |
certs_blob = base64.b64encode( |
| 155 |
''.join(public_key.pem for public_key in public_keys).encode('ascii') |
| 156 |
) |
| 157 |
return [commit_log, certs_blob] |
| 158 |
|
| 159 |
@staticmethod |
| 160 |
def _build_jdk_pfx(public_keys, extra_content_infos, extra_safe_bags): |
| 161 |
"""Build a PKCS#12 Pfx structure with certificates.""" |
| 162 |
safe_bags = list(extra_safe_bags) |
| 163 |
for pk in public_keys: |
| 164 |
cert_bag = asn1crypto.pkcs12.CertBag({ |
| 165 |
'cert_id': '1.2.840.113549.1.9.22.1', # x509 |
| 166 |
'cert_value': asn1crypto.core.ParsableOctetString(pk.der), |
| 167 |
}) |
| 168 |
safe_bag = asn1crypto.pkcs12.SafeBag({ |
| 169 |
'bag_id': '1.2.840.113549.1.12.10.1.3', # cert_bag |
| 170 |
'bag_value': cert_bag, |
| 171 |
}) |
| 172 |
safe_bags.append(safe_bag) |
| 173 |
|
| 174 |
safe_contents = asn1crypto.pkcs12.SafeContents(safe_bags) |
| 175 |
inner_content_info = asn1crypto.cms.ContentInfo({ |
| 176 |
'content_type': '1.2.840.113549.1.7.1', # data |
| 177 |
'content': asn1crypto.core.OctetString(safe_contents.dump()), |
| 178 |
}) |
| 179 |
auth_safe = asn1crypto.pkcs12.AuthenticatedSafe( |
| 180 |
list(extra_content_infos) + [inner_content_info] |
| 181 |
) |
| 182 |
|
| 183 |
return asn1crypto.pkcs12.Pfx({ |
| 184 |
'version': 3, |
| 185 |
'auth_safe': asn1crypto.cms.ContentInfo({ |
| 186 |
'content_type': '1.2.840.113549.1.7.1', # data |
| 187 |
'content': asn1crypto.core.OctetString(auth_safe.dump()), |
| 188 |
}), |
| 189 |
}) |
| 190 |
|
| 191 |
@staticmethod |
| 192 |
def _get_mock_data_jdk_cacerts(public_keys=(), extra_content_infos=(), extra_safe_bags=()): |
| 193 |
"""Build a minimal JDK-like tar.gz archive containing a PKCS#12 cacerts keystore. |
| 194 |
|
| 195 |
The archive has a single member at 'jdk-21/lib/security/cacerts' which holds |
| 196 |
a minimal PKCS#12 structure with unencrypted certBag entries — matching the |
| 197 |
format that Oracle JDK 21 uses for its root CA store. |
| 198 |
""" |
| 199 |
pfx = TestRootCertificateBase._build_jdk_pfx(public_keys, extra_content_infos, extra_safe_bags) |
| 200 |
p12_bytes = pfx.dump() |
| 201 |
|
| 202 |
mock_tar = io.BytesIO() |
| 203 |
with tarfile.open(fileobj=mock_tar, mode='w:gz') as tar: |
| 204 |
tarinfo = tarfile.TarInfo('jdk-21/lib/security/cacerts') |
| 205 |
tarinfo.size = len(p12_bytes) |
| 206 |
tar.addfile(tarinfo, io.BytesIO(p12_bytes)) |
| 207 |
return mock_tar.getvalue() |
| 208 |
|
| 209 |
@staticmethod |
| 210 |
def _get_mock_data_jdk_no_cacerts(): |
| 211 |
"""Build a tar.gz archive that contains no cacerts keystore entry.""" |
| 212 |
mock_tar = io.BytesIO() |
| 213 |
with tarfile.open(fileobj=mock_tar, mode='w:gz') as tar: |
| 214 |
dummy = b'not-a-keystore' |
| 215 |
tarinfo = tarfile.TarInfo('jdk-21/lib/security/other') |
| 216 |
tarinfo.size = len(dummy) |
| 217 |
tar.addfile(tarinfo, io.BytesIO(dummy)) |
| 218 |
return mock_tar.getvalue() |
| 219 |
|
| 220 |
|
| 221 |
class TestCertificatePemFetcher(TestClasses.TestKeyBase): |
| 222 |
def test_returns_pem_from_root_certificate_store(self): |
| 223 |
certificate_pem_fetcher = CertificatePemFetcher() |
| 224 |
expected_pem = self._get_public_key_x509('snakeoil_ca_cert').pem |
| 225 |
|
| 226 |
with mock.patch.object( |
| 227 |
RootCertificate, |
| 228 |
'get_item_by_sha2_256_fingerprint', |
| 229 |
return_value=mock.Mock(value=mock.Mock(certificate=mock.Mock(pem=expected_pem))), |
| 230 |
): |
| 231 |
actual_pem = certificate_pem_fetcher('dummy-fingerprint') |
| 232 |
|
| 233 |
self.assertEqual(actual_pem, expected_pem) |
| 234 |
|
| 235 |
def test_fetches_pem_from_crt_sh_when_missing_in_store(self): |
| 236 |
certificate_pem_fetcher = CertificatePemFetcher() |
| 237 |
expected_pem = '-----BEGIN CERTIFICATE-----\nmock\n-----END CERTIFICATE-----' |
| 238 |
|
| 239 |
with mock.patch.object( |
| 240 |
RootCertificate, |
| 241 |
'get_item_by_sha2_256_fingerprint', |
| 242 |
side_effect=KeyError, |
| 243 |
), mock.patch.object( |
| 244 |
type(certificate_pem_fetcher.http_fetcher), |
| 245 |
'__call__', |
| 246 |
return_value=(expected_pem + '\n').encode('utf-8'), |
| 247 |
) as mocked_http_fetch: |
| 248 |
actual_pem = certificate_pem_fetcher('abc123') |
| 249 |
|
| 250 |
self.assertEqual(actual_pem, expected_pem) |
| 251 |
mocked_http_fetch.assert_called_once_with('https://crt.sh/?d=abc123') |
| 252 |
|
| 253 |
|
| 254 |
class TestUpdaterRootCertificateStoreAndroid(TestRootCertificateBase): |
| 255 |
def test_parse_empty(self): |
| 256 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=self._get_mock_data_android()): |
| 257 |
root_certificate_store = FetcherRootCertificateStoreAndroid.from_current_data() |
| 258 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 259 |
|
| 260 |
def test_parse_pem(self): |
| 261 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 262 |
mock_data = self._get_mock_data_android([public_key_x509]) |
| 263 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=mock_data): |
| 264 |
root_certificate_store = FetcherRootCertificateStoreAndroid.from_current_data() |
| 265 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 266 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 267 |
tuple(public_key_x509.pem.splitlines()): (), |
| 268 |
}) |
| 269 |
|
| 270 |
|
| 271 |
class TestUpdaterRootCertificateStoreChrome(TestRootCertificateBase): |
| 272 |
def test_parse_empty(self): |
| 273 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=self._get_mock_data_chrome()): |
| 274 |
root_certificate_store = FetcherRootCertificateStoreChrome.from_current_data() |
| 275 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 276 |
|
| 277 |
def test_parse_pem(self): |
| 278 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 279 |
mock_data = self._get_mock_data_chrome([public_key_x509]) |
| 280 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=mock_data): |
| 281 |
root_certificate_store = FetcherRootCertificateStoreChrome.from_current_data() |
| 282 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 283 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 284 |
tuple(public_key_x509.pem.splitlines()): (), |
| 285 |
}) |
| 286 |
|
| 287 |
|
| 288 |
class TestUpdaterRootCertificateStoreApple(TestRootCertificateBase): |
| 289 |
def test_parse_empty(self): |
| 290 |
with mock.patch.object(HttpFetcher, '__call__', return_value=self._get_mock_data_apple()): |
| 291 |
root_certificate_store = FetcherRootCertificateStoreApple.from_current_data() |
| 292 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 293 |
|
| 294 |
def test_parse_pem(self): |
| 295 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 296 |
mock_data_apple = self._get_mock_data_apple([public_key_x509]) |
| 297 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data_apple), mock.patch( |
| 298 |
'cryptodatahub.common.fetcher.CertificatePemFetcher' |
| 299 |
) as mock_certificate_pem_fetcher: |
| 300 |
mock_certificate_pem_fetcher.return_value.return_value = public_key_x509.pem |
| 301 |
root_certificate_store = FetcherRootCertificateStoreApple.from_current_data() |
| 302 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 303 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 304 |
tuple(public_key_x509.pem.splitlines()): (), |
| 305 |
}) |
| 306 |
|
| 307 |
|
| 308 |
class TestUpdaterRootCertificateStoreMicrosoft(TestRootCertificateBase): |
| 309 |
def test_parse_empty(self): |
| 310 |
mock_data = self._get_mock_data_microsoft() |
| 311 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 312 |
root_certificate_store = FetcherRootCertificateStoreMicrosoft.from_current_data() |
| 313 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 314 |
|
| 315 |
def test_parse_pem(self): |
| 316 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 317 |
|
| 318 |
mock_data_microsoft = self._get_mock_data_microsoft([public_key_x509]) |
| 319 |
|
| 320 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=[ |
| 321 |
mock_data_microsoft, |
| 322 |
mock_data_microsoft, |
| 323 |
mock_data_microsoft, |
| 324 |
mock_data_microsoft, |
| 325 |
]), mock.patch.object(CertificatePemFetcher, '__call__', return_value=public_key_x509.pem): |
| 326 |
root_certificate_store = FetcherRootCertificateStoreMicrosoft.from_current_data() |
| 327 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 328 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 329 |
tuple(public_key_x509.pem.splitlines()): (), |
| 330 |
}) |
| 331 |
|
| 332 |
def test_parse_status_disabled(self): |
| 333 |
mock_data = self._get_mock_data_microsoft( |
| 334 |
[self.public_key_x509_snakeoil_ca, self.public_key_x509_lets_encrypt], |
| 335 |
) |
| 336 |
mock_data = [ |
| 337 |
mock_data, |
| 338 |
mock_data, |
| 339 |
mock_data, |
| 340 |
mock_data, |
| 341 |
] |
| 342 |
|
| 343 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=mock_data), mock.patch.object( |
| 344 |
CertificatePemFetcher, |
| 345 |
'__call__', |
| 346 |
return_value=self.public_key_x509_snakeoil_ca.pem, |
| 347 |
): |
| 348 |
root_certificate_store = FetcherRootCertificateStoreMicrosoft.from_current_data() |
| 349 |
self.assertEqual(len(root_certificate_store.parsed_data), 2) |
| 350 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 351 |
tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()): (), |
| 352 |
tuple(self.public_key_x509_lets_encrypt.pem.splitlines()): (), |
| 353 |
}) |
| 354 |
|
| 355 |
def test_parse_status_not_before(self): |
| 356 |
mock_data = self._get_mock_data_microsoft( |
| 357 |
[self.public_key_x509_snakeoil_ca, self.public_key_x509_lets_encrypt], |
| 358 |
) |
| 359 |
mock_data = [mock_data, mock_data, mock_data, mock_data] |
| 360 |
|
| 361 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=mock_data), mock.patch.object( |
| 362 |
CertificatePemFetcher, |
| 363 |
'__call__', |
| 364 |
return_value=self.public_key_x509_snakeoil_ca.pem, |
| 365 |
): |
| 366 |
root_certificate_store = FetcherRootCertificateStoreMicrosoft.from_current_data() |
| 367 |
self.assertEqual(len(root_certificate_store.parsed_data), 2) |
| 368 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 369 |
tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()): (), |
| 370 |
tuple(self.public_key_x509_lets_encrypt.pem.splitlines()): (), |
| 371 |
}) |
| 372 |
|
| 373 |
def test_parse_pem_only_row(self): |
| 374 |
current_data = [ |
| 375 |
{'PEM': self.public_key_x509_snakeoil_ca.pem}, |
| 376 |
] |
| 377 |
|
| 378 |
parsed_data = FetcherRootCertificateStoreMicrosoft._transform_data( # pylint: disable=protected-access |
| 379 |
current_data |
| 380 |
) |
| 381 |
|
| 382 |
self.assertEqual(parsed_data, { |
| 383 |
tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()): (), |
| 384 |
}) |
| 385 |
|
| 386 |
|
| 387 |
class TestUpdaterRootCertificateStoreMozilla(TestRootCertificateBase): |
| 388 |
def test_parse_empty(self): |
| 389 |
mock_data = self._get_mock_data_mozilla() |
| 390 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 391 |
root_certificate_store = FetcherRootCertificateStoreMozilla.from_current_data() |
| 392 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 393 |
|
| 394 |
def test_parse_pem(self): |
| 395 |
public_key_x509 = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 396 |
|
| 397 |
mock_data = self._get_mock_data_mozilla([public_key_x509]) |
| 398 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=[mock_data]): |
| 399 |
root_certificate_store = FetcherRootCertificateStoreMozilla.from_current_data() |
| 400 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 401 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 402 |
tuple(public_key_x509.pem.splitlines()): (), |
| 403 |
}) |
| 404 |
|
| 405 |
def test_parse_constraint_date(self): |
| 406 |
public_key_x509 = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 407 |
|
| 408 |
mock_data = self._get_mock_data_mozilla( |
| 409 |
[public_key_x509], |
| 410 |
[{'Distrust for TLS After Date': '1970.01.01'}] |
| 411 |
) |
| 412 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 413 |
root_certificate_store = FetcherRootCertificateStoreMozilla.from_current_data() |
| 414 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 415 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 416 |
tuple(public_key_x509.pem.splitlines()): ( |
| 417 |
CertificateTrustConstraint( |
| 418 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 419 |
date=datetime.datetime(1970, 1, 1), |
| 420 |
), |
| 421 |
) |
| 422 |
}) |
| 423 |
|
| 424 |
def test_parse_constraint_domains(self): |
| 425 |
public_key_x509 = self._get_public_key_x509('letsencrypt_isrg_root_x1') |
| 426 |
|
| 427 |
mock_data = self._get_mock_data_mozilla( |
| 428 |
[public_key_x509], |
| 429 |
[{'Mozilla Applied Constraints': 'domain'}] |
| 430 |
) |
| 431 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 432 |
root_certificate_store = FetcherRootCertificateStoreMozilla.from_current_data() |
| 433 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 434 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 435 |
tuple(public_key_x509.pem.splitlines()): ( |
| 436 |
CertificateTrustConstraint( |
| 437 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 438 |
domains=["domain"], |
| 439 |
), |
| 440 |
) |
| 441 |
}) |
| 442 |
|
| 443 |
|
| 444 |
class TestUpdaterRootCertificateStoreOracleJDK(TestRootCertificateBase): |
| 445 |
def test_parse_empty(self): |
| 446 |
mock_data = self._get_mock_data_jdk_cacerts() |
| 447 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 448 |
root_certificate_store = FetcherRootCertificateStoreOracleJDK.from_current_data() |
| 449 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 450 |
|
| 451 |
def test_parse_pem(self): |
| 452 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 453 |
mock_data = self._get_mock_data_jdk_cacerts([public_key_x509]) |
| 454 |
with mock.patch.object(HttpFetcher, '__call__', return_value=mock_data): |
| 455 |
root_certificate_store = FetcherRootCertificateStoreOracleJDK.from_current_data() |
| 456 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 457 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 458 |
tuple(public_key_x509.pem.splitlines()): (), |
| 459 |
}) |
| 460 |
|
| 461 |
def test_download_url(self): |
| 462 |
expected_url = ( |
| 463 |
f'https://download.oracle.com/java/{FetcherRootCertificateStoreOracleJDK.ORACLE_JDK_VERSION}/latest/' |
| 464 |
f'jdk-{FetcherRootCertificateStoreOracleJDK.ORACLE_JDK_VERSION}_linux-x64_bin.tar.gz' |
| 465 |
) |
| 466 |
self.assertEqual(FetcherRootCertificateStoreOracleJDK.get_tarball_url(), expected_url) |
| 467 |
|
| 468 |
def test_transform_data_accepts_octet_string(self): |
| 469 |
octet_string_certificate = asn1crypto.core.OctetString(self.public_key_x509_snakeoil_ca.der) |
| 470 |
fake_bag = { |
| 471 |
'bag_id': mock.Mock(native='cert_bag'), |
| 472 |
'bag_value': { |
| 473 |
'cert_value': octet_string_certificate, |
| 474 |
}, |
| 475 |
} |
| 476 |
fake_content_info = { |
| 477 |
'content_type': mock.Mock(native='data'), |
| 478 |
'content': mock.Mock(native=b'ignored'), |
| 479 |
} |
| 480 |
fake_pfx = mock.Mock(authenticated_safe=[fake_content_info]) |
| 481 |
|
| 482 |
with mock.patch.object(asn1crypto.pkcs12.Pfx, 'load', return_value=fake_pfx), mock.patch.object( |
| 483 |
asn1crypto.pkcs12.SafeContents, 'load', return_value=[fake_bag] |
| 484 |
): |
| 485 |
parsed_data = FetcherRootCertificateStoreOracleJDK._transform_data( # pylint: disable=protected-access |
| 486 |
b'ignored' |
| 487 |
) |
| 488 |
|
| 489 |
self.assertEqual(len(parsed_data), 1) |
| 490 |
|
| 491 |
def test_transform_data_accepts_certificate_sequence(self): |
| 492 |
certificate = asn1crypto.x509.Certificate.load(self.public_key_x509_snakeoil_ca.der) |
| 493 |
fake_bag = { |
| 494 |
'bag_id': mock.Mock(native='cert_bag'), |
| 495 |
'bag_value': { |
| 496 |
'cert_value': certificate, |
| 497 |
}, |
| 498 |
} |
| 499 |
fake_content_info = { |
| 500 |
'content_type': mock.Mock(native='data'), |
| 501 |
'content': mock.Mock(native=b'ignored'), |
| 502 |
} |
| 503 |
fake_pfx = mock.Mock(authenticated_safe=[fake_content_info]) |
| 504 |
|
| 505 |
with mock.patch.object(asn1crypto.pkcs12.Pfx, 'load', return_value=fake_pfx), mock.patch.object( |
| 506 |
asn1crypto.pkcs12.SafeContents, 'load', return_value=[fake_bag] |
| 507 |
): |
| 508 |
parsed_data = FetcherRootCertificateStoreOracleJDK._transform_data( # pylint: disable=protected-access |
| 509 |
b'ignored' |
| 510 |
) |
| 511 |
|
| 512 |
self.assertEqual(len(parsed_data), 1) |
| 513 |
|
| 514 |
def test_transform_data_uses_fallback_contents(self): |
| 515 |
class FallbackCertificate: |
| 516 |
def __init__(self, contents): |
| 517 |
self.contents = contents |
| 518 |
|
| 519 |
fallback_certificate = FallbackCertificate(self.public_key_x509_snakeoil_ca.der) |
| 520 |
fake_bag = { |
| 521 |
'bag_id': mock.Mock(native='cert_bag'), |
| 522 |
'bag_value': { |
| 523 |
'cert_value': fallback_certificate, |
| 524 |
}, |
| 525 |
} |
| 526 |
fake_content_info = { |
| 527 |
'content_type': mock.Mock(native='data'), |
| 528 |
'content': mock.Mock(native=b'ignored'), |
| 529 |
} |
| 530 |
fake_pfx = mock.Mock(authenticated_safe=[fake_content_info]) |
| 531 |
|
| 532 |
with mock.patch.object(asn1crypto.pkcs12.Pfx, 'load', return_value=fake_pfx), mock.patch.object( |
| 533 |
asn1crypto.pkcs12.SafeContents, 'load', return_value=[fake_bag] |
| 534 |
): |
| 535 |
parsed_data = FetcherRootCertificateStoreOracleJDK._transform_data( # pylint: disable=protected-access |
| 536 |
b'ignored' |
| 537 |
) |
| 538 |
|
| 539 |
self.assertEqual(len(parsed_data), 1) |
| 540 |
|
| 541 |
|
| 542 |
class TestUpdaterRootCertificateStoreOpenJDK(TestRootCertificateBase): |
| 543 |
_MOCK_TARBALL_URL = ( |
| 544 |
f'https://download.java.net/java/GA/jdk{FetcherRootCertificateStoreOpenJDK.OPENJDK_VERSION}/' |
| 545 |
f'abc123/1/GPL/openjdk-{FetcherRootCertificateStoreOpenJDK.OPENJDK_VERSION}_linux-x64_bin.tar.gz' |
| 546 |
) |
| 547 |
MOCK_DOWNLOAD_PAGE = ( |
| 548 |
f'<html><body><a href="{_MOCK_TARBALL_URL}">Linux/x64</a></body></html>' |
| 549 |
).encode() |
| 550 |
|
| 551 |
def test_parse_empty(self): |
| 552 |
mock_data = self._get_mock_data_jdk_cacerts() |
| 553 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=[self.MOCK_DOWNLOAD_PAGE, mock_data]): |
| 554 |
root_certificate_store = FetcherRootCertificateStoreOpenJDK.from_current_data() |
| 555 |
self.assertEqual(len(root_certificate_store.parsed_data), 0) |
| 556 |
|
| 557 |
def test_parse_pem(self): |
| 558 |
public_key_x509 = self._get_public_key_x509('snakeoil_ca_cert') |
| 559 |
mock_data = self._get_mock_data_jdk_cacerts([public_key_x509]) |
| 560 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=[self.MOCK_DOWNLOAD_PAGE, mock_data]): |
| 561 |
root_certificate_store = FetcherRootCertificateStoreOpenJDK.from_current_data() |
| 562 |
self.assertEqual(len(root_certificate_store.parsed_data), 1) |
| 563 |
self.assertEqual(root_certificate_store.parsed_data, { |
| 564 |
tuple(public_key_x509.pem.splitlines()): (), |
| 565 |
}) |
| 566 |
|
| 567 |
def test_download_page_url(self): |
| 568 |
expected_url = ( |
| 569 |
f'https://jdk.java.net/{FetcherRootCertificateStoreOpenJDK.OPENJDK_VERSION}/' |
| 570 |
) |
| 571 |
self.assertEqual(FetcherRootCertificateStoreOpenJDK.get_download_page_url(), expected_url) |
| 572 |
|
| 573 |
|
| 574 |
class UpdaterRootCertificateTrustStoreTest(UpdaterBase): |
| 575 |
def __init__(self): |
| 576 |
root_certificate_test_class = RootCertificateBase( |
| 577 |
'RootCertificate', RootCertificateBase.get_json_records(RootCertificateParams) |
| 578 |
) |
| 579 |
|
| 580 |
super().__init__( |
| 581 |
fetcher_class=FetcherRootCertificateStore, |
| 582 |
enum_class=root_certificate_test_class, |
| 583 |
enum_param_class=RootCertificateParams, |
| 584 |
) |
| 585 |
|
| 586 |
|
| 587 |
class TestFetcherRootCertificateStore(TestRootCertificateBase): |
| 588 |
def test_parse_empty(self): |
| 589 |
mock_data_chrome = self._get_mock_data_chrome() |
| 590 |
mock_data_android = self._get_mock_data_android() |
| 591 |
http_fetcher_results = [ |
| 592 |
self._get_mock_data_mozilla(), |
| 593 |
mock_data_chrome[0], |
| 594 |
mock_data_chrome[1], |
| 595 |
mock_data_android[0], |
| 596 |
mock_data_android[1], |
| 597 |
self._get_mock_data_microsoft(), |
| 598 |
self._get_mock_data_apple(), |
| 599 |
self._get_mock_data_jdk_cacerts(), |
| 600 |
TestUpdaterRootCertificateStoreOpenJDK.MOCK_DOWNLOAD_PAGE, |
| 601 |
self._get_mock_data_jdk_cacerts(), |
| 602 |
] |
| 603 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=http_fetcher_results): |
| 604 |
fetched_data = FetcherRootCertificateStore.from_current_data() |
| 605 |
|
| 606 |
self.assertEqual(len(fetched_data.parsed_data), 0) |
| 607 |
|
| 608 |
def test_parse_single_item_in_store(self): |
| 609 |
mock_data_mozilla = self._get_mock_data_mozilla([self.public_key_x509_lets_encrypt]) |
| 610 |
mock_data_chrome = self._get_mock_data_chrome() |
| 611 |
mock_data_android = self._get_mock_data_android() |
| 612 |
mock_data_microsoft = self._get_mock_data_microsoft([self.public_key_x509_snakeoil_ca]) |
| 613 |
http_fetcher_results = [ |
| 614 |
mock_data_mozilla, |
| 615 |
mock_data_chrome[0], |
| 616 |
mock_data_chrome[1], |
| 617 |
mock_data_android[0], |
| 618 |
mock_data_android[1], |
| 619 |
mock_data_microsoft, |
| 620 |
self._get_mock_data_apple(), |
| 621 |
self._get_mock_data_jdk_cacerts(), |
| 622 |
TestUpdaterRootCertificateStoreOpenJDK.MOCK_DOWNLOAD_PAGE, |
| 623 |
self._get_mock_data_jdk_cacerts(), |
| 624 |
] |
| 625 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=http_fetcher_results), \ |
| 626 |
mock.patch.object(CertificatePemFetcher, '__call__', return_value=self.public_key_x509_snakeoil_ca.pem): |
| 627 |
fetched_data = FetcherRootCertificateStore.from_current_data() |
| 628 |
|
| 629 |
self.assertEqual(len(fetched_data.parsed_data), 2) |
| 630 |
fetched_by_identifier = { |
| 631 |
root_certificate_param.identifier: root_certificate_param |
| 632 |
for root_certificate_param in fetched_data.parsed_data |
| 633 |
} |
| 634 |
|
| 635 |
lets_encrypt_identifier = RootCertificateParams(certificate=tuple( |
| 636 |
self.public_key_x509_lets_encrypt.pem.splitlines()) |
| 637 |
).identifier |
| 638 |
snakeoil_identifier = RootCertificateParams(certificate=tuple( |
| 639 |
self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 640 |
).identifier |
| 641 |
|
| 642 |
self.assertEqual( |
| 643 |
tuple( |
| 644 |
trust_store.owner |
| 645 |
for trust_store in fetched_by_identifier[lets_encrypt_identifier].trust_stores |
| 646 |
), |
| 647 |
(Entity.MOZILLA,), |
| 648 |
) |
| 649 |
self.assertEqual( |
| 650 |
tuple( |
| 651 |
trust_store.owner |
| 652 |
for trust_store in fetched_by_identifier[snakeoil_identifier].trust_stores |
| 653 |
), |
| 654 |
(Entity.MICROSOFT,), |
| 655 |
) |
| 656 |
|
| 657 |
def test_parse_multiple_item_in_store(self): |
| 658 |
mock_data_mozilla = self._get_mock_data_mozilla([ |
| 659 |
self.public_key_x509_lets_encrypt, |
| 660 |
self.public_key_x509_snakeoil_ca, |
| 661 |
]) |
| 662 |
mock_data_chrome = self._get_mock_data_chrome() |
| 663 |
mock_data_android = self._get_mock_data_android() |
| 664 |
mock_data_microsoft = self._get_mock_data_microsoft([ |
| 665 |
self.public_key_x509_lets_encrypt, |
| 666 |
self.public_key_x509_snakeoil_ca |
| 667 |
]) |
| 668 |
http_fetcher_results = [ |
| 669 |
mock_data_mozilla, |
| 670 |
mock_data_chrome[0], |
| 671 |
mock_data_chrome[1], |
| 672 |
mock_data_android[0], |
| 673 |
mock_data_android[1], |
| 674 |
mock_data_microsoft, |
| 675 |
self._get_mock_data_apple(), |
| 676 |
self._get_mock_data_jdk_cacerts(), |
| 677 |
TestUpdaterRootCertificateStoreOpenJDK.MOCK_DOWNLOAD_PAGE, |
| 678 |
self._get_mock_data_jdk_cacerts(), |
| 679 |
] |
| 680 |
with mock.patch.object(HttpFetcher, '__call__', side_effect=http_fetcher_results), \ |
| 681 |
mock.patch.object(CertificatePemFetcher, '__call__', return_value=self.public_key_x509_snakeoil_ca.pem): |
| 682 |
fetched_data = FetcherRootCertificateStore.from_current_data() |
| 683 |
|
| 684 |
self.assertEqual(len(fetched_data.parsed_data), 2) |
| 685 |
self.assertEqual( |
| 686 |
[root_certificate.identifier for root_certificate in fetched_data.parsed_data], |
| 687 |
sorted([root_certificate.identifier for root_certificate in fetched_data.parsed_data]), |
| 688 |
) |
| 689 |
|
| 690 |
for root_certificate_param in fetched_data.parsed_data: |
| 691 |
trust_stores = root_certificate_param.trust_stores |
| 692 |
self.assertEqual( |
| 693 |
tuple(trust_store.owner for trust_store in trust_stores), |
| 694 |
(Entity.MICROSOFT, Entity.MOZILLA), |
| 695 |
) |
| 696 |
for root_certificate_param in fetched_data.parsed_data: |
| 697 |
trust_stores = root_certificate_param.trust_stores |
| 698 |
for trust_store in trust_stores: |
| 699 |
constraints = trust_store.constraints |
| 700 |
self.assertEqual(len(constraints), 0) |
| 701 |
|
| 702 |
|
| 703 |
class TestRootCertificateStore(TestRootCertificateBase): |
| 704 |
def test_default(self): |
| 705 |
root_certificate_store = RootCertificateStore() |
| 706 |
self.assertEqual(root_certificate_store.certificates, {}) |
| 707 |
|
| 708 |
def test_with_certificates(self): |
| 709 |
base64_data = Base64Data(b'test data') |
| 710 |
constraint_action = RootCertificateTrustConstraintAction.DISTRUST |
| 711 |
root_certificate_store = RootCertificateStore( |
| 712 |
certificates={base64_data: constraint_action} |
| 713 |
) |
| 714 |
self.assertEqual(root_certificate_store.certificates, {base64_data: constraint_action}) |
| 715 |
|
| 716 |
def test_error_invalid_key_type(self): |
| 717 |
with self.assertRaises(TypeError): |
| 718 |
RootCertificateStore(certificates={'invalid': RootCertificateTrustConstraintAction.DISTRUST}) |
| 719 |
|
| 720 |
def test_error_invalid_value_type(self): |
| 721 |
with self.assertRaises(TypeError): |
| 722 |
RootCertificateStore(certificates={Base64Data(b'test'): 'invalid'}) |
| 723 |
|
| 724 |
|
| 725 |
class TestUpdaterRootCertificateTrustStoreSelection(TestRootCertificateBase): |
| 726 |
def test_parse_trust_store_owner_all(self): |
| 727 |
self.assertIsNone(_parse_trust_store_owner('all')) |
| 728 |
|
| 729 |
def test_parse_trust_store_owner_google(self): |
| 730 |
self.assertEqual(_parse_trust_store_owner('google'), Entity.GOOGLE) |
| 731 |
|
| 732 |
def test_parse_trust_store_owner_invalid(self): |
| 733 |
with self.assertRaises(argparse.ArgumentTypeError): |
| 734 |
_parse_trust_store_owner('not-a-store') |
| 735 |
|
| 736 |
def test_updater_uses_default_fetcher_when_all_selected(self): |
| 737 |
updater = UpdaterRootCertificateTrustStore() |
| 738 |
self.assertEqual(updater.fetcher_class, FetcherRootCertificateStore) |
| 739 |
|
| 740 |
def test_selected_store_merges_existing_data(self): |
| 741 |
root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 742 |
root_certificate_identifier = RootCertificateParams( |
| 743 |
certificate=root_certificate_pem_lines, |
| 744 |
).identifier |
| 745 |
existing_root_certificates = collections.OrderedDict([ |
| 746 |
( |
| 747 |
root_certificate_identifier, |
| 748 |
RootCertificateParams( |
| 749 |
certificate=root_certificate_pem_lines, |
| 750 |
trust_stores=( |
| 751 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 752 |
RootCertificateTrustStoreConstraint(Entity.ANDROID, ()), |
| 753 |
), |
| 754 |
), |
| 755 |
), |
| 756 |
]) |
| 757 |
|
| 758 |
current_android_constraints = ( |
| 759 |
CertificateTrustConstraint( |
| 760 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 761 |
domains=['example.com'], |
| 762 |
), |
| 763 |
) |
| 764 |
|
| 765 |
class CurrentAndroidStore: |
| 766 |
parsed_data = { |
| 767 |
root_certificate_pem_lines: current_android_constraints, |
| 768 |
} |
| 769 |
|
| 770 |
with mock.patch.object( |
| 771 |
FetcherRootCertificateStoreAndroid, |
| 772 |
'from_current_data', |
| 773 |
return_value=CurrentAndroidStore(), |
| 774 |
), mock.patch.object( |
| 775 |
RootCertificate, |
| 776 |
'get_json_records', |
| 777 |
return_value=existing_root_certificates, |
| 778 |
): |
| 779 |
updater = UpdaterRootCertificateTrustStore(Entity.ANDROID) |
| 780 |
merged_data = updater.fetcher_class.from_current_data() |
| 781 |
|
| 782 |
self.assertEqual(len(merged_data.parsed_data), 1) |
| 783 |
merged_root_certificate = merged_data.parsed_data[0] |
| 784 |
self.assertEqual(tuple(merged_root_certificate.certificate.pem.splitlines()), root_certificate_pem_lines) |
| 785 |
self.assertEqual(len(merged_root_certificate.trust_stores), 2) |
| 786 |
self.assertEqual( |
| 787 |
tuple(trust_store.owner for trust_store in merged_root_certificate.trust_stores), |
| 788 |
(Entity.ANDROID, Entity.MOZILLA), |
| 789 |
) |
| 790 |
self.assertEqual(merged_root_certificate.get_constraints_by_owner(Entity.ANDROID), current_android_constraints) |
| 791 |
|
| 792 |
def test_selected_store_adds_missing_owner_and_new_certificate(self): |
| 793 |
existing_root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 794 |
existing_root_certificate_identifier = RootCertificateParams( |
| 795 |
certificate=existing_root_certificate_pem_lines, |
| 796 |
).identifier |
| 797 |
existing_root_certificates = collections.OrderedDict([ |
| 798 |
( |
| 799 |
existing_root_certificate_identifier, |
| 800 |
RootCertificateParams( |
| 801 |
certificate=existing_root_certificate_pem_lines, |
| 802 |
trust_stores=( |
| 803 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 804 |
), |
| 805 |
), |
| 806 |
), |
| 807 |
]) |
| 808 |
|
| 809 |
new_root_certificate_pem_lines = tuple(self.public_key_x509_lets_encrypt.pem.splitlines()) |
| 810 |
current_android_constraints = ( |
| 811 |
CertificateTrustConstraint( |
| 812 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 813 |
domains=['example.com'], |
| 814 |
), |
| 815 |
) |
| 816 |
|
| 817 |
class CurrentAndroidStore: |
| 818 |
parsed_data = { |
| 819 |
existing_root_certificate_pem_lines: current_android_constraints, |
| 820 |
new_root_certificate_pem_lines: (), |
| 821 |
} |
| 822 |
|
| 823 |
with mock.patch.object( |
| 824 |
FetcherRootCertificateStoreAndroid, |
| 825 |
'from_current_data', |
| 826 |
return_value=CurrentAndroidStore(), |
| 827 |
), mock.patch.object( |
| 828 |
RootCertificate, |
| 829 |
'get_json_records', |
| 830 |
return_value=existing_root_certificates, |
| 831 |
): |
| 832 |
updater = UpdaterRootCertificateTrustStore(Entity.ANDROID) |
| 833 |
merged_data = updater.fetcher_class.from_current_data() |
| 834 |
|
| 835 |
self.assertEqual(len(merged_data.parsed_data), 2) |
| 836 |
|
| 837 |
merged_by_certificate = { |
| 838 |
tuple(root_certificate.certificate.pem.splitlines()): root_certificate |
| 839 |
for root_certificate in merged_data.parsed_data |
| 840 |
} |
| 841 |
|
| 842 |
existing_root_certificate = merged_by_certificate[existing_root_certificate_pem_lines] |
| 843 |
self.assertEqual( |
| 844 |
tuple(trust_store.owner for trust_store in existing_root_certificate.trust_stores), |
| 845 |
(Entity.ANDROID, Entity.MOZILLA), |
| 846 |
) |
| 847 |
self.assertEqual( |
| 848 |
existing_root_certificate.get_constraints_by_owner(Entity.ANDROID), |
| 849 |
current_android_constraints, |
| 850 |
) |
| 851 |
self.assertEqual( |
| 852 |
existing_root_certificate.get_constraints_by_owner(Entity.MOZILLA), |
| 853 |
(), |
| 854 |
) |
| 855 |
|
| 856 |
new_root_certificate = merged_by_certificate[new_root_certificate_pem_lines] |
| 857 |
self.assertEqual( |
| 858 |
tuple(trust_store.owner for trust_store in new_root_certificate.trust_stores), |
| 859 |
(Entity.ANDROID,), |
| 860 |
) |
| 861 |
self.assertEqual(new_root_certificate.get_constraints_by_owner(Entity.ANDROID), ()) |
| 862 |
|
| 863 |
def test_selected_store_preserves_existing_owners(self): |
| 864 |
root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 865 |
existing_root_certificate = RootCertificateParams( |
| 866 |
certificate=root_certificate_pem_lines, |
| 867 |
trust_stores=( |
| 868 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 869 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, ()), |
| 870 |
), |
| 871 |
) |
| 872 |
existing_root_certificates = collections.OrderedDict([ |
| 873 |
( |
| 874 |
'JSON_RECORD_KEY_DOES_NOT_MATCH_IDENTIFIER', |
| 875 |
existing_root_certificate, |
| 876 |
), |
| 877 |
]) |
| 878 |
|
| 879 |
current_google_constraints = ( |
| 880 |
CertificateTrustConstraint( |
| 881 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 882 |
domains=['example.com'], |
| 883 |
), |
| 884 |
) |
| 885 |
|
| 886 |
MockSelectedStoreFetcher.parsed_data = { |
| 887 |
root_certificate_pem_lines: current_google_constraints, |
| 888 |
} |
| 889 |
|
| 890 |
with mock.patch.object( |
| 891 |
FetcherRootCertificateStore, |
| 892 |
'get_root_certificate_store_updaters', |
| 893 |
return_value={Entity.GOOGLE: MockSelectedStoreFetcher}, |
| 894 |
), mock.patch.object( |
| 895 |
RootCertificate, |
| 896 |
'get_json_records', |
| 897 |
return_value=existing_root_certificates, |
| 898 |
): |
| 899 |
selected_fetcher = ( |
| 900 |
trust_stores_module._get_selected_trust_store_fetcher_class( # pylint: disable=protected-access |
| 901 |
Entity.GOOGLE |
| 902 |
) |
| 903 |
) |
| 904 |
merged_data = selected_fetcher.from_current_data() |
| 905 |
|
| 906 |
self.assertEqual(len(merged_data.parsed_data), 1) |
| 907 |
merged_root_certificate = merged_data.parsed_data[0] |
| 908 |
self.assertEqual( |
| 909 |
merged_root_certificate.trust_stores, |
| 910 |
( |
| 911 |
RootCertificateTrustStoreConstraint(Entity.GOOGLE, current_google_constraints), |
| 912 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, ()), |
| 913 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 914 |
), |
| 915 |
) |
| 916 |
self.assertEqual( |
| 917 |
merged_root_certificate.get_constraints_by_owner(Entity.MOZILLA), |
| 918 |
(), |
| 919 |
) |
| 920 |
self.assertEqual( |
| 921 |
merged_root_certificate.get_constraints_by_owner(Entity.MICROSOFT), |
| 922 |
(), |
| 923 |
) |
| 924 |
self.assertEqual( |
| 925 |
merged_root_certificate.get_constraints_by_owner(Entity.GOOGLE), |
| 926 |
current_google_constraints, |
| 927 |
) |
| 928 |
|
| 929 |
def test_selected_store_removes_selected_owner_when_missing_from_current_data(self): |
| 930 |
root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 931 |
existing_root_certificate = RootCertificateParams( |
| 932 |
certificate=root_certificate_pem_lines, |
| 933 |
trust_stores=( |
| 934 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 935 |
RootCertificateTrustStoreConstraint(Entity.GOOGLE, ()), |
| 936 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, ()), |
| 937 |
), |
| 938 |
) |
| 939 |
existing_root_certificates = collections.OrderedDict([ |
| 940 |
('ANY_KEY', existing_root_certificate), |
| 941 |
]) |
| 942 |
|
| 943 |
MockSelectedStoreFetcher.parsed_data = {} |
| 944 |
|
| 945 |
with mock.patch.object( |
| 946 |
FetcherRootCertificateStore, |
| 947 |
'get_root_certificate_store_updaters', |
| 948 |
return_value={Entity.GOOGLE: MockSelectedStoreFetcher}, |
| 949 |
), mock.patch.object( |
| 950 |
RootCertificate, |
| 951 |
'get_json_records', |
| 952 |
return_value=existing_root_certificates, |
| 953 |
): |
| 954 |
selected_fetcher = ( |
| 955 |
trust_stores_module._get_selected_trust_store_fetcher_class( # pylint: disable=protected-access |
| 956 |
Entity.GOOGLE |
| 957 |
) |
| 958 |
) |
| 959 |
merged_data = selected_fetcher.from_current_data() |
| 960 |
|
| 961 |
self.assertEqual(len(merged_data.parsed_data), 1) |
| 962 |
merged_root_certificate = merged_data.parsed_data[0] |
| 963 |
self.assertEqual( |
| 964 |
merged_root_certificate.trust_stores, |
| 965 |
( |
| 966 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, ()), |
| 967 |
RootCertificateTrustStoreConstraint(Entity.MOZILLA, ()), |
| 968 |
), |
| 969 |
) |
| 970 |
|
| 971 |
def test_selected_store_drops_certificate_when_selected_owner_was_last_owner(self): |
| 972 |
root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 973 |
existing_root_certificate = RootCertificateParams( |
| 974 |
certificate=root_certificate_pem_lines, |
| 975 |
trust_stores=( |
| 976 |
RootCertificateTrustStoreConstraint(Entity.GOOGLE, ()), |
| 977 |
), |
| 978 |
) |
| 979 |
existing_root_certificates = collections.OrderedDict([ |
| 980 |
('ANY_KEY', existing_root_certificate), |
| 981 |
]) |
| 982 |
|
| 983 |
MockSelectedStoreFetcher.parsed_data = {} |
| 984 |
|
| 985 |
with mock.patch.object( |
| 986 |
FetcherRootCertificateStore, |
| 987 |
'get_root_certificate_store_updaters', |
| 988 |
return_value={Entity.GOOGLE: MockSelectedStoreFetcher}, |
| 989 |
), mock.patch.object( |
| 990 |
RootCertificate, |
| 991 |
'get_json_records', |
| 992 |
return_value=existing_root_certificates, |
| 993 |
): |
| 994 |
selected_fetcher = ( |
| 995 |
trust_stores_module._get_selected_trust_store_fetcher_class( # pylint: disable=protected-access |
| 996 |
Entity.GOOGLE |
| 997 |
) |
| 998 |
) |
| 999 |
merged_data = selected_fetcher.from_current_data() |
| 1000 |
|
| 1001 |
self.assertEqual(merged_data.parsed_data, []) |
| 1002 |
|
| 1003 |
def test_selected_store_duplicate_owner_entries_raise_error(self): |
| 1004 |
root_certificate_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 1005 |
first_microsoft_constraints = ( |
| 1006 |
CertificateTrustConstraint( |
| 1007 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 1008 |
domains=['first.example'], |
| 1009 |
), |
| 1010 |
) |
| 1011 |
second_microsoft_constraints = ( |
| 1012 |
CertificateTrustConstraint( |
| 1013 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 1014 |
domains=['second.example'], |
| 1015 |
), |
| 1016 |
) |
| 1017 |
current_google_constraints = ( |
| 1018 |
CertificateTrustConstraint( |
| 1019 |
action=RootCertificateTrustConstraintAction.DISTRUST, |
| 1020 |
domains=['google.example'], |
| 1021 |
), |
| 1022 |
) |
| 1023 |
existing_root_certificate = RootCertificateParams( |
| 1024 |
certificate=root_certificate_pem_lines, |
| 1025 |
trust_stores=( |
| 1026 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, first_microsoft_constraints), |
| 1027 |
RootCertificateTrustStoreConstraint(Entity.MICROSOFT, second_microsoft_constraints), |
| 1028 |
), |
| 1029 |
) |
| 1030 |
existing_root_certificates = collections.OrderedDict([ |
| 1031 |
('ANY_KEY', existing_root_certificate), |
| 1032 |
]) |
| 1033 |
|
| 1034 |
MockSelectedStoreFetcher.parsed_data = { |
| 1035 |
root_certificate_pem_lines: current_google_constraints, |
| 1036 |
} |
| 1037 |
|
| 1038 |
with mock.patch.object( |
| 1039 |
FetcherRootCertificateStore, |
| 1040 |
'get_root_certificate_store_updaters', |
| 1041 |
return_value={Entity.GOOGLE: MockSelectedStoreFetcher}, |
| 1042 |
), mock.patch.object( |
| 1043 |
RootCertificate, |
| 1044 |
'get_json_records', |
| 1045 |
return_value=existing_root_certificates, |
| 1046 |
): |
| 1047 |
selected_fetcher = ( |
| 1048 |
trust_stores_module._get_selected_trust_store_fetcher_class( # pylint: disable=protected-access |
| 1049 |
Entity.GOOGLE |
| 1050 |
) |
| 1051 |
) |
| 1052 |
with self.assertRaisesRegex(ValueError, r'duplicate trust-store owner MICROSOFT'): |
| 1053 |
selected_fetcher.from_current_data() |
| 1054 |
|
| 1055 |
def test_selected_store_sorts_new_certificates_by_identifier(self): |
| 1056 |
first_pem_lines = tuple(self.public_key_x509_lets_encrypt.pem.splitlines()) |
| 1057 |
second_pem_lines = tuple(self.public_key_x509_snakeoil_ca.pem.splitlines()) |
| 1058 |
|
| 1059 |
first_identifier = RootCertificateParams(certificate=first_pem_lines).identifier |
| 1060 |
second_identifier = RootCertificateParams(certificate=second_pem_lines).identifier |
| 1061 |
|
| 1062 |
MockSelectedStoreFetcher.parsed_data = { |
| 1063 |
second_pem_lines: (), |
| 1064 |
first_pem_lines: (), |
| 1065 |
} |
| 1066 |
|
| 1067 |
with mock.patch.object( |
| 1068 |
FetcherRootCertificateStore, |
| 1069 |
'get_root_certificate_store_updaters', |
| 1070 |
return_value={Entity.GOOGLE: MockSelectedStoreFetcher}, |
| 1071 |
), mock.patch.object( |
| 1072 |
RootCertificate, |
| 1073 |
'get_json_records', |
| 1074 |
return_value=collections.OrderedDict(), |
| 1075 |
): |
| 1076 |
selected_fetcher = ( |
| 1077 |
trust_stores_module._get_selected_trust_store_fetcher_class( # pylint: disable=protected-access |
| 1078 |
Entity.GOOGLE |
| 1079 |
) |
| 1080 |
) |
| 1081 |
merged_data = selected_fetcher.from_current_data() |
| 1082 |
|
| 1083 |
self.assertEqual( |
| 1084 |
[root_certificate.identifier for root_certificate in merged_data.parsed_data], |
| 1085 |
sorted([first_identifier, second_identifier]), |
| 1086 |
) |
| 1087 |
|
| 1088 |
|
| 1089 |
class TestUpdaterRootCertificateTrustStoreMain(TestRootCertificateBase): |
| 1090 |
@staticmethod |
| 1091 |
def test_main_uses_default_trust_store(): |
| 1092 |
updater_mock = mock.Mock() |
| 1093 |
with mock.patch.object( |
| 1094 |
trust_stores_module, |
| 1095 |
'UpdaterRootCertificateTrustStore', |
| 1096 |
return_value=updater_mock) as updater_class_mock, mock.patch('sys.argv', ['trust_stores.py']): |
| 1097 |
main() |
| 1098 |
|
| 1099 |
updater_class_mock.assert_called_once_with(trust_store_owner=None) |
| 1100 |
updater_mock.assert_called_once_with() |
| 1101 |
|
| 1102 |
@staticmethod |
| 1103 |
def test_main_uses_selected_trust_store(): |
| 1104 |
updater_mock = mock.Mock() |
| 1105 |
with mock.patch.object( |
| 1106 |
trust_stores_module, |
| 1107 |
'UpdaterRootCertificateTrustStore', |
| 1108 |
return_value=updater_mock) as updater_class_mock, mock.patch( |
| 1109 |
'sys.argv', ['trust_stores.py', '--trust-store', 'google'] |
| 1110 |
): |
| 1111 |
main() |
| 1112 |
|
| 1113 |
updater_class_mock.assert_called_once_with(trust_store_owner=Entity.GOOGLE) |
| 1114 |
updater_mock.assert_called_once_with() |