File: test_freshness.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 (204 lines) | stat: -rw-r--r-- 7,337 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
import os
from datetime import datetime, timedelta, timezone

import pytest

from pyhanko_certvalidator import ValidationContext
from pyhanko_certvalidator.errors import PathValidationError, RevokedError
from pyhanko_certvalidator.policy_decl import (
    CertRevTrustPolicy,
    FreshnessReqType,
    RevocationCheckingPolicy,
)
from pyhanko_certvalidator.validate import async_validate_path

from .common import load_cert_object, load_crl, load_ocsp_response

freshness_dir = 'freshness'
certs = os.path.join('freshness', 'certs')


@pytest.mark.asyncio
async def test_cooldown_period_ok():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(days=3),
        freshness_req_type=FreshnessReqType.TIME_AFTER_SIGNATURE,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp = load_ocsp_response(freshness_dir, 'alice-2020-10-01.ors')
    root_crl = load_crl(freshness_dir, 'root-2020-10-01.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 10, 1, tzinfo=timezone.utc),
        best_signature_time=datetime(2020, 9, 18, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    await async_validate_path(vc, path)


@pytest.mark.asyncio
async def test_cooldown_period_too_early():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(days=3),
        freshness_req_type=FreshnessReqType.TIME_AFTER_SIGNATURE,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp = load_ocsp_response(freshness_dir, 'alice-2020-10-01.ors')
    root_crl = load_crl(freshness_dir, 'root-2020-10-01.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 10, 1, tzinfo=timezone.utc),
        best_signature_time=datetime(2020, 9, 30, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    with pytest.raises(PathValidationError, match='CRL.*recent enough'):
        await async_validate_path(vc, path)


@pytest.mark.asyncio
async def test_use_delta_ok():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(days=9),
        freshness_req_type=FreshnessReqType.MAX_DIFF_REVOCATION_VALIDATION,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp = load_ocsp_response(freshness_dir, 'alice-2020-10-01.ors')
    root_crl = load_crl(freshness_dir, 'root-2020-10-01.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 10, 1, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    await async_validate_path(vc, path)


@pytest.mark.asyncio
async def test_use_delta_stale():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(hours=1),
        freshness_req_type=FreshnessReqType.MAX_DIFF_REVOCATION_VALIDATION,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp = load_ocsp_response(freshness_dir, 'alice-2020-10-01.ors')
    root_crl = load_crl(freshness_dir, 'root-2020-10-01.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 10, 1, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    with pytest.raises(PathValidationError, match='CRL.*recent enough'):
        await async_validate_path(vc, path)


@pytest.mark.asyncio
async def test_use_most_recent():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(days=20),  # some ridiculous value
        freshness_req_type=FreshnessReqType.MAX_DIFF_REVOCATION_VALIDATION,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp_older = load_ocsp_response(freshness_dir, 'alice-2020-11-29.ors')
    alice_ocsp_recent = load_ocsp_response(
        freshness_dir, 'alice-2020-12-10.ors'
    )
    root_crl = load_crl(freshness_dir, 'root-2020-12-10.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp_older, alice_ocsp_recent],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 12, 10, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    with pytest.raises(RevokedError):
        await async_validate_path(vc, path)

    # Double-check: the validator should be fooled if we don't include the
    #  second OCSP response because of the very lenient time delta allowed
    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp_older],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 12, 10, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    await async_validate_path(vc, path)


@pytest.mark.asyncio
async def test_discard_post_validation_time():
    req_policy = RevocationCheckingPolicy.from_legacy('require')
    policy = CertRevTrustPolicy(
        revocation_checking_policy=req_policy,
        freshness=timedelta(days=20),  # some ridiculous value
        freshness_req_type=FreshnessReqType.MAX_DIFF_REVOCATION_VALIDATION,
    )
    root = load_cert_object(certs, 'root.crt')
    alice = load_cert_object(certs, 'alice.crt')
    interm = load_cert_object(certs, 'interm.crt')

    alice_ocsp_older = load_ocsp_response(freshness_dir, 'alice-2020-11-29.ors')
    alice_ocsp_recent = load_ocsp_response(
        freshness_dir, 'alice-2020-12-10.ors'
    )
    root_crl = load_crl(freshness_dir, 'root-2020-11-29.crl')

    vc = ValidationContext(
        trust_roots=[root],
        other_certs=[interm],
        ocsps=[alice_ocsp_older, alice_ocsp_recent],
        crls=[root_crl],
        revinfo_policy=policy,
        moment=datetime(2020, 11, 29, tzinfo=timezone.utc),
    )
    (path,) = await vc.path_builder.async_build_paths(alice)
    await async_validate_path(vc, path)