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
|
from datetime import datetime
from typing import Dict, Iterable, List, Optional, Set
from asn1crypto import crl, ocsp, x509
from pyhanko_certvalidator.authority import Authority
from pyhanko_certvalidator.errors import OCSPFetchError
from pyhanko_certvalidator.fetchers import Fetchers
from pyhanko_certvalidator.ltv.poe import (
KnownPOE,
POEManager,
POEType,
ValidationObject,
ValidationObjectType,
digest_for_poe,
)
from pyhanko_certvalidator.policy_decl import NonRevokedStatusAssertion
from pyhanko_certvalidator.registry import CertificateRegistry
from pyhanko_certvalidator.revinfo.archival import (
CRLContainer,
OCSPContainer,
sort_freshest_first,
)
class RevinfoManager:
"""
.. versionadded:: 0.20.0
Class to manage and potentially fetch revocation information.
:param certificate_registry:
The associated certificate registry.
:param poe_manager:
The proof-of-existence (POE) data manager.
:param crls:
CRL data.
:param ocsps:
OCSP response data.
:param fetchers:
Fetchers for collecting revocation information.
If ``None``, no fetching will be performed.
"""
def __init__(
self,
certificate_registry: CertificateRegistry,
poe_manager: POEManager,
crls: Iterable[CRLContainer],
ocsps: Iterable[OCSPContainer],
assertions: Iterable[NonRevokedStatusAssertion] = (),
fetchers: Optional[Fetchers] = None,
):
self._certificate_registry = certificate_registry
self._poe_manager = poe_manager
self._revocation_certs: Dict[bytes, x509.Certificate] = {}
self._crl_issuer_map: Dict[bytes, x509.Certificate] = {}
self._crls: List[CRLContainer] = []
if crls:
self._crls = sort_freshest_first(crls)
self._ocsps: List[OCSPContainer] = []
if ocsps:
self._ocsps = ocsps = sort_freshest_first(ocsps)
for ocsp_response in ocsps:
self._extract_ocsp_certs(ocsp_response)
self._fetchers = fetchers
self._assertions: Dict[bytes, NonRevokedStatusAssertion] = {
assertion.cert_sha256: assertion for assertion in assertions
}
@property
def poe_manager(self) -> POEManager:
"""
The proof-of-existence (POE) data manager.
"""
return self._poe_manager
@property
def certificate_registry(self) -> CertificateRegistry:
"""
The associated certificate registry.
"""
return self._certificate_registry
@property
def fetching_allowed(self) -> bool:
"""
Boolean indicating whether fetching is allowed.
"""
return self._fetchers is not None
@property
def crls(self) -> List[crl.CertificateList]:
"""
A list of all cached :class:`crl.CertificateList` objects
"""
raw_crls = [cont.crl_data for cont in self._crls]
if not self._fetchers:
return raw_crls
return list(self._fetchers.crl_fetcher.fetched_crls()) + raw_crls
@property
def ocsps(self) -> List[ocsp.OCSPResponse]:
"""
A list of all cached :class:`ocsp.OCSPResponse` objects
"""
raw_ocsps = [cont.ocsp_response_data for cont in self._ocsps]
if not self._fetchers:
return raw_ocsps
return list(self._fetchers.ocsp_fetcher.fetched_responses()) + raw_ocsps
@property
def new_revocation_certs(self) -> List[x509.Certificate]:
"""
A list of newly-fetched :class:`x509.Certificate` objects that were
obtained from OCSP responses and CRLs
"""
return list(self._revocation_certs.values())
def _extract_ocsp_certs(self, ocsp_response: OCSPContainer):
"""
Extracts any certificates included with an OCSP response and adds them
to the certificate registry
:param ocsp_response:
An asn1crypto.ocsp.OCSPResponse object to look for certs inside of
"""
poe_man = self._poe_manager
ocsp_poe_time = poe_man[ocsp_response]
registry = self._certificate_registry
revo_certs = self._revocation_certs
basic = ocsp_response.extract_basic_ocsp_response()
if basic is not None and basic['certs']:
for other_cert in basic['certs']:
if registry.register(other_cert):
revo_certs[other_cert.issuer_serial] = other_cert
poe_man.register_known_poe(
KnownPOE(
poe_type=POEType.VALIDATION,
digest=digest_for_poe(other_cert.dump()),
# register with the same POE time as the OCSP
# response
poe_time=ocsp_poe_time,
validation_object=ValidationObject(
object_type=ValidationObjectType.CERTIFICATE,
value=other_cert,
),
)
)
def record_crl_issuer(self, certificate_list, cert):
"""
Records the certificate that issued a certificate list. Used to reduce
processing code when dealing with self-issued certificates and multiple
CRLs.
:param certificate_list:
An ans1crypto.crl.CertificateList object
:param cert:
An ans1crypto.x509.Certificate object
"""
self._crl_issuer_map[certificate_list.signature] = cert
def check_crl_issuer(self, certificate_list) -> Optional[x509.Certificate]:
"""
Checks to see if the certificate that signed a certificate list has
been found
:param certificate_list:
An ans1crypto.crl.CertificateList object
:return:
None if not found, or an asn1crypto.x509.Certificate object of the
issuer
"""
return self._crl_issuer_map.get(certificate_list.signature)
async def async_retrieve_crls(self, cert) -> List[CRLContainer]:
"""
.. versionadded:: 0.20.0
:param cert:
An asn1crypto.x509.Certificate object
:return:
A list of :class:`CRLContainer` objects
"""
if not self._fetchers:
return self._crls
fetchers = self._fetchers
try:
crls = fetchers.crl_fetcher.fetched_crls_for_cert(cert)
except KeyError:
crls = await fetchers.crl_fetcher.fetch(cert)
conts = [CRLContainer(crl_data) for crl_data in crls]
return conts + self._crls
async def async_retrieve_ocsps(
self, cert, authority: Authority
) -> List[OCSPContainer]:
"""
.. versionadded:: 0.20.0
:param cert:
An asn1crypto.x509.Certificate object
:param authority:
The issuing authority for the certificate
:return:
A list of :class:`OCSPContainer` objects
"""
if not self._fetchers:
return self._ocsps
fetchers = self._fetchers
ocsps = [
OCSPContainer(resp)
for resp in fetchers.ocsp_fetcher.fetched_responses_for_cert(cert)
]
if not ocsps:
ocsp_response_data = await fetchers.ocsp_fetcher.fetch(
cert, authority
)
ocsps = OCSPContainer.load_multi(ocsp_response_data)
# Responses can contain certificates that are useful in
# validating the response itself. We can use these since they
# will be validated using the local trust roots.
for resp in ocsps:
try:
self._extract_ocsp_certs(resp)
except ValueError:
raise OCSPFetchError(
"Failed to extract certificates from "
"fetched OCSP response"
)
return ocsps + self._ocsps
def evict_ocsps(self, hashes_to_evict: Set[bytes]):
"""
Internal API to eliminate local OCSP records from consideration.
:param hashes_to_evict:
A collection of OCSP response hashes; see :func:`.digest_for_poe`.
"""
def p(container: OCSPContainer):
digest = digest_for_poe(container.ocsp_response_data.dump())
return digest not in hashes_to_evict
self._ocsps = list(filter(p, self._ocsps))
def evict_crls(self, hashes_to_evict: Set[bytes]):
"""
Internal API to eliminate local CRLs from consideration.
:param hashes_to_evict:
A collection of CRL hashes; see :func:`.digest_for_poe`.
"""
def p(container: CRLContainer):
digest = digest_for_poe(container.crl_data.dump())
return digest not in hashes_to_evict
self._crls = list(filter(p, self._crls))
def check_asserted_unrevoked(
self, cert: x509.Certificate, at: datetime
) -> bool:
try:
return at <= self._assertions[cert.sha256].at
except KeyError:
return False
|