File: test_ades_time_slide.py

package info (click to toggle)
python-pyhanko-certvalidator 0.26.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,956 kB
  • sloc: python: 9,254; sh: 47; makefile: 4
file content (401 lines) | stat: -rw-r--r-- 14,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import datetime
import os

import pytest
from freezegun import freeze_time

from pyhanko_certvalidator.context import (
    CertValidationPolicySpec,
    ValidationDataHandlers,
)
from pyhanko_certvalidator.errors import (
    InsufficientPOEError,
    InsufficientRevinfoError,
)
from pyhanko_certvalidator.ltv.ades_past import past_validate
from pyhanko_certvalidator.ltv.errors import TimeSlideFailure
from pyhanko_certvalidator.ltv.poe import POEManager, POEType, digest_for_poe
from pyhanko_certvalidator.ltv.time_slide import time_slide
from pyhanko_certvalidator.path import ValidationPath
from pyhanko_certvalidator.policy_decl import (
    CertRevTrustPolicy,
    FreshnessReqType,
    RevocationCheckingPolicy,
    RevocationCheckingRule,
)
from pyhanko_certvalidator.registry import (
    CertificateRegistry,
    SimpleTrustManager,
)
from pyhanko_certvalidator.revinfo.archival import CRLContainer, OCSPContainer
from pyhanko_certvalidator.revinfo.manager import RevinfoManager

from .common import load_cert_object, load_crl, load_ocsp_response, load_path

BASE_DIR = os.path.join('ades', 'time-slide')


def read_test_path(revoked_intermediate_ca=False) -> ValidationPath:
    return load_path(
        os.path.join(BASE_DIR, 'certs'),
        'root.crt',
        'interm-revoked.crt' if revoked_intermediate_ca else 'interm.crt',
        'alice.crt',
    )


def load_cert_registry(revoked_intermediate_ca=False) -> CertificateRegistry:
    cert_files = (
        'root.crt',
        'interm-revoked.crt' if revoked_intermediate_ca else 'interm.crt',
        'interm-ocsp.crt',
        'alice.crt',
    )
    reg = CertificateRegistry()
    for cert_file in cert_files:
        reg.register(load_cert_object(BASE_DIR, 'certs', cert_file))
    return reg


def now() -> datetime.datetime:
    return datetime.datetime.now(tz=datetime.timezone.utc)


DEFAULT_REV_CHECK_POLICY = RevocationCheckingPolicy(
    ee_certificate_rule=RevocationCheckingRule.CRL_OR_OCSP_REQUIRED,
    intermediate_ca_cert_rule=RevocationCheckingRule.CRL_OR_OCSP_REQUIRED,
)
DEFAULT_TRUST_POLICY = CertRevTrustPolicy(
    revocation_checking_policy=DEFAULT_REV_CHECK_POLICY,
)

DEFAULT_TOLERANCE = datetime.timedelta(minutes=10)


@pytest.mark.asyncio
@freeze_time("2020-11-29T00:05:00+00:00")
async def test_time_slide_not_revoked():
    test_path = read_test_path()
    alice_ocsp = load_ocsp_response(BASE_DIR, 'alice-2020-11-29.ors')
    root_crl = load_crl(BASE_DIR, 'root-2020-11-29.crl')

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(),
        poe_manager=POEManager(),
        crls=[CRLContainer(root_crl)],
        ocsps=[OCSPContainer(alice_ocsp)],
    )
    control_time = await time_slide(
        test_path,
        init_control_time=now(),
        revinfo_manager=revinfo_manager,
        rev_trust_policy=DEFAULT_TRUST_POLICY,
        algo_usage_policy=None,
        time_tolerance=DEFAULT_TOLERANCE,
    )
    assert control_time == now()


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_time_slide_revocation_ocsp():
    test_path = read_test_path()
    alice_ocsp = load_ocsp_response(BASE_DIR, 'alice-2020-12-10.ors')
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(),
        poe_manager=POEManager(),
        crls=[CRLContainer(root_crl)],
        ocsps=[OCSPContainer(alice_ocsp)],
    )
    control_time = await time_slide(
        test_path,
        init_control_time=now(),
        revinfo_manager=revinfo_manager,
        rev_trust_policy=DEFAULT_TRUST_POLICY,
        algo_usage_policy=None,
        time_tolerance=DEFAULT_TOLERANCE,
    )
    assert control_time == datetime.datetime(
        2020, 12, 1, tzinfo=datetime.timezone.utc
    )


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_time_slide_revocation_crl():
    test_path = read_test_path()
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    interm_crl = load_crl(BASE_DIR, 'interm-2020-12-10.crl')

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(),
        poe_manager=POEManager(),
        crls=[CRLContainer(root_crl), CRLContainer(interm_crl)],
        ocsps=[],
    )
    control_time = await time_slide(
        test_path,
        init_control_time=now(),
        revinfo_manager=revinfo_manager,
        rev_trust_policy=DEFAULT_TRUST_POLICY,
        algo_usage_policy=None,
        time_tolerance=DEFAULT_TOLERANCE,
    )
    assert control_time == datetime.datetime(
        2020, 12, 1, tzinfo=datetime.timezone.utc
    )


VERY_LENIENT_FRESHNESS = CertRevTrustPolicy(
    revocation_checking_policy=DEFAULT_REV_CHECK_POLICY,
    freshness_req_type=FreshnessReqType.MAX_DIFF_REVOCATION_VALIDATION,
    freshness=datetime.timedelta(days=100),
)


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_time_slide_revoked_intermediate():
    test_path = read_test_path(revoked_intermediate_ca=True)
    # the intermediate cert is listed as revoked on this CRL
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    # this CRL would be valid long enough to serve as non-revocation
    # evidence for the 'alice' cert
    # We set a ridiculous freshness window to ensure it's covered.
    poe_manager = POEManager()
    interm_crl = load_crl(BASE_DIR, 'interm-2020-11-29.crl')
    poe_date = datetime.datetime(2020, 11, 30, tzinfo=datetime.timezone.utc)
    poe_manager.register(test_path.leaf, dt=poe_date, poe_type=POEType.PROVIDED)
    # ...make sure to include some POE prior to the revocation date of the
    # intermediate cert
    poe_manager.register(
        CRLContainer(interm_crl), dt=poe_date, poe_type=POEType.PROVIDED
    )

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(revoked_intermediate_ca=True),
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl), CRLContainer(interm_crl)],
        ocsps=[],
    )
    control_time = await time_slide(
        test_path,
        init_control_time=now(),
        revinfo_manager=revinfo_manager,
        rev_trust_policy=VERY_LENIENT_FRESHNESS,
        algo_usage_policy=None,
        time_tolerance=DEFAULT_TOLERANCE,
    )
    assert control_time == datetime.datetime(
        2020, 12, 1, tzinfo=datetime.timezone.utc
    )


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_time_slide_revoked_intermediate_enforce_cert_poe():
    test_path = read_test_path(revoked_intermediate_ca=True)
    # the intermediate cert is listed as revoked on this CRL
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    poe_manager = POEManager()
    # No POE for the leaf cert at the control time
    # at which the intermediate cert was revoked => fail
    interm_crl = load_crl(BASE_DIR, 'interm-2020-11-29.crl')
    poe_date = datetime.datetime(2020, 11, 30, tzinfo=datetime.timezone.utc)
    poe_manager.register(
        CRLContainer(interm_crl), dt=poe_date, poe_type=POEType.PROVIDED
    )

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(revoked_intermediate_ca=True),
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl), CRLContainer(interm_crl)],
        ocsps=[],
    )
    with pytest.raises(InsufficientPOEError, match='for.*Alice'):
        await time_slide(
            test_path,
            init_control_time=now(),
            revinfo_manager=revinfo_manager,
            rev_trust_policy=VERY_LENIENT_FRESHNESS,
            algo_usage_policy=None,
            time_tolerance=DEFAULT_TOLERANCE,
        )


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_time_slide_revoked_intermediate_enforce_revinfo_poe():
    test_path = read_test_path(revoked_intermediate_ca=True)
    # the intermediate cert is listed as revoked on this CRL
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    poe_manager = POEManager()
    poe_date = datetime.datetime(2020, 11, 30, tzinfo=datetime.timezone.utc)
    poe_manager.register(test_path.leaf, dt=poe_date, poe_type=POEType.PROVIDED)
    # This CRL issued by the intermediate CA predates its revocation date
    # so without POE, it should be treated as no longer valid
    # => no revinfo for the leaf cert => can't finish
    interm_crl = load_crl(BASE_DIR, 'interm-2020-11-29.crl')

    revinfo_manager = RevinfoManager(
        certificate_registry=load_cert_registry(revoked_intermediate_ca=True),
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl), CRLContainer(interm_crl)],
        ocsps=[],
    )
    with pytest.raises(InsufficientRevinfoError, match='for.*Alice'):
        await time_slide(
            test_path,
            init_control_time=now(),
            revinfo_manager=revinfo_manager,
            rev_trust_policy=VERY_LENIENT_FRESHNESS,
            algo_usage_policy=None,
            time_tolerance=DEFAULT_TOLERANCE,
        )


VALIDATION_POLICY_SPEC = CertValidationPolicySpec(
    trust_manager=SimpleTrustManager.build(
        trust_roots=[load_cert_object(BASE_DIR, 'certs', 'root.crt')]
    ),
    revinfo_policy=DEFAULT_TRUST_POLICY,
)


@pytest.mark.asyncio
@freeze_time("2020-11-29T00:05:00+00:00")
async def test_point_in_time_validation_not_revoked():
    test_path = read_test_path()
    alice_ocsp = load_ocsp_response(BASE_DIR, 'alice-2020-11-29.ors')
    root_crl = load_crl(BASE_DIR, 'root-2020-11-29.crl')

    cert_registry = load_cert_registry()
    poe_manager = POEManager()
    revinfo_manager = RevinfoManager(
        certificate_registry=cert_registry,
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl)],
        ocsps=[OCSPContainer(alice_ocsp)],
    )

    last_valid_time = await past_validate(
        test_path,
        validation_policy_spec=VALIDATION_POLICY_SPEC,
        init_control_time=now(),
        validation_data_handlers=ValidationDataHandlers(
            revinfo_manager=revinfo_manager,
            poe_manager=poe_manager,
            cert_registry=cert_registry,
        ),
    )
    assert last_valid_time == now()


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_point_in_time_validation_revoked_intermediate():
    # Same scenario as the time slide test w/ revoked intermediate cert & PoE
    # in this module

    test_path = read_test_path(revoked_intermediate_ca=True)
    # the intermediate cert is listed as revoked on this CRL
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    # this CRL would be valid long enough to serve as non-revocation
    # evidence for the 'alice' cert
    # We set a ridiculous freshness window to ensure it's covered.
    poe_date = datetime.datetime(2020, 11, 30, tzinfo=datetime.timezone.utc)
    poe_manager = POEManager()
    poe_manager.register(test_path.leaf, dt=poe_date, poe_type=POEType.PROVIDED)
    interm_crl = load_crl(BASE_DIR, 'interm-2020-11-29.crl')
    # ...make sure to include some POE prior to the revocation date of the
    # intermediate cert
    poe_manager.register(
        CRLContainer(interm_crl), dt=poe_date, poe_type=POEType.PROVIDED
    )

    cert_registry = load_cert_registry(revoked_intermediate_ca=True)
    revinfo_manager = RevinfoManager(
        certificate_registry=cert_registry,
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl), CRLContainer(interm_crl)],
        ocsps=[],
    )
    last_valid_time = await past_validate(
        test_path,
        validation_policy_spec=VALIDATION_POLICY_SPEC,
        init_control_time=now(),
        validation_data_handlers=ValidationDataHandlers(
            revinfo_manager=revinfo_manager,
            poe_manager=poe_manager,
            cert_registry=cert_registry,
        ),
    )
    assert last_valid_time == datetime.datetime(
        2020, 12, 1, tzinfo=datetime.timezone.utc
    )


@pytest.mark.asyncio
@freeze_time("2020-12-10T00:05:00+00:00")
async def test_point_in_time_validation_revinfo_insufficient_poe():
    test_path = read_test_path(revoked_intermediate_ca=True)
    # the intermediate cert is listed as revoked on this CRL
    root_crl = load_crl(BASE_DIR, 'root-2020-12-10.crl')
    poe_manager = POEManager()

    cert_registry = load_cert_registry(revoked_intermediate_ca=True)
    revinfo_manager = RevinfoManager(
        certificate_registry=cert_registry,
        poe_manager=poe_manager,
        crls=[CRLContainer(root_crl)],
        ocsps=[],
    )
    with pytest.raises(TimeSlideFailure):
        await past_validate(
            test_path,
            validation_policy_spec=VALIDATION_POLICY_SPEC,
            init_control_time=now(),
            validation_data_handlers=ValidationDataHandlers(
                revinfo_manager=revinfo_manager,
                poe_manager=poe_manager,
                cert_registry=cert_registry,
            ),
        )


def test_poe_manager_read_cert():
    manager = POEManager()
    cert = load_cert_object(BASE_DIR, 'certs', 'root.crt')
    with freeze_time('2020-11-11'):
        manager.register(cert, poe_type=POEType.PROVIDED)
    assert manager[cert].date() == datetime.date(2020, 11, 11)


def test_poe_manager_cert_by_digest():
    manager = POEManager()
    cert = load_cert_object(BASE_DIR, 'certs', 'root.crt')
    with freeze_time('2020-11-11'):
        manager.register_by_digest(
            digest_for_poe(cert.dump()), poe_type=POEType.PROVIDED
        )
    assert manager[cert].date() == datetime.date(2020, 11, 11)


def test_poe_manager_read_bytes():
    manager = POEManager()
    msg = b'deadbeef'
    with freeze_time('2020-11-11'):
        manager.register(msg, poe_type=POEType.PROVIDED)
    assert manager[msg].date() == datetime.date(2020, 11, 11)


def test_poe_manager_read_bytes_by_digest():
    manager = POEManager()
    msg = b'deadbeef'
    with freeze_time('2020-11-11'):
        manager.register_by_digest(
            digest_for_poe(msg), poe_type=POEType.PROVIDED
        )
    assert manager[msg].date() == datetime.date(2020, 11, 11)