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
|
"""
Internal backend-agnostic utilities to help process fetched certificates, CRLs
and OCSP responses.
"""
import asyncio
import logging
import os
from typing import Awaitable, Callable, Dict, Optional, TypeVar, Union
from asn1crypto import algos, cms, core, ocsp, pem, x509
from .. import errors
from ..authority import Authority
from ..util import get_ac_extension_value
__all__ = [
'unpack_cert_content',
'format_ocsp_request',
'process_ocsp_response_data',
'queue_fetch_task',
'crl_job_results_as_completed',
'ocsp_job_get_earliest',
'complete_certificate_fetch_jobs',
'gather_aia_issuer_urls',
'ACCEPTABLE_STRICT_CERT_CONTENT_TYPES',
'ACCEPTABLE_CERT_PEM_ALIASES',
'ACCEPTABLE_PKCS7_DER_ALIASES',
'ACCEPTABLE_CERT_DER_ALIASES',
]
logger = logging.getLogger(__name__)
ACCEPTABLE_STRICT_CERT_CONTENT_TYPES = frozenset(
[
'application/pkix-cert',
'application/pkcs7-mime',
'application/x-x509-ca-cert',
'application/x-pkcs7-certificates',
]
)
ACCEPTABLE_CERT_PEM_ALIASES = frozenset(
[
'application/x-pem-file',
'text/plain',
'application/octet-stream',
'binary/octet-stream',
]
)
ACCEPTABLE_CERT_DER_ALIASES = frozenset(
[
'application/pkix-cert',
'application/x-x509-ca-cert',
'application/octet-stream',
'binary/octet-stream',
]
)
ACCEPTABLE_PKCS7_DER_ALIASES = frozenset(
[
'application/pkcs7-mime',
'application/x-pkcs7-certificates',
'binary/octet-stream',
]
)
def unpack_cert_content(
response_data: bytes,
content_type: Optional[str],
url: str,
permit_pem: bool,
):
is_pem = pem.detect(response_data)
if (
content_type is None or content_type in ACCEPTABLE_CERT_DER_ALIASES
) and not is_pem:
# sometimes we get DER over octet-stream
if content_type is None:
logger.warning(
f"Response to certificate fetch request to {url} did not "
f"include a content type, verifying it's sequence length to "
f"check if it is a certificate or pkcs7."
)
der_sequence_length = len(core.Sequence.load(response_data))
if der_sequence_length == 2:
yield from _unpack_der_pkcs7(response_data, url)
elif der_sequence_length == 3:
yield x509.Certificate.load(response_data)
elif (content_type in ACCEPTABLE_PKCS7_DER_ALIASES) and not is_pem:
yield from _unpack_der_pkcs7(response_data, url)
elif permit_pem and is_pem:
# technically, PEM is not allowed here, but of course some people don't
# bother following the rules
for type_name, _, data in pem.unarmor(response_data, multiple=True):
if type_name == 'PKCS7':
yield from _unpack_der_pkcs7(data, url)
else:
yield x509.Certificate.load(data)
else: # pragma: nocover
raise ValueError(
f"Failed to extract certs from {content_type} payload. "
f"Source URL: {url}."
)
def _unpack_der_pkcs7(pkcs7_data: bytes, pkcs7_url: str):
content_info: cms.ContentInfo = cms.ContentInfo.load(pkcs7_data)
cms_ct = content_info['content_type'].native
if cms_ct != 'signed_data':
raise ValueError(
"Expected CMS SignedData when extracting certs from "
"application/pkcs7-mime payload, but content type was "
f"'{cms_ct}'. Source URL: {pkcs7_url}."
)
signed_data = content_info['content']
if isinstance(signed_data['certificates'], cms.CertificateSet):
for cert_choice in signed_data['certificates']:
if cert_choice.name == 'certificate':
yield cert_choice.chosen
def get_certid(
cert: Union[x509.Certificate, cms.AttributeCertificateV2],
authority: Authority,
*,
certid_hash_algo,
) -> ocsp.CertId:
if isinstance(cert, x509.Certificate):
serial_number = cert.serial_number
else:
serial_number = cert['ac_info']['serial_number'].native
iss_name_hash = getattr(authority.name, certid_hash_algo)
cert_id = ocsp.CertId(
{
'hash_algorithm': algos.DigestAlgorithm(
{'algorithm': certid_hash_algo}
),
'issuer_name_hash': iss_name_hash,
'issuer_key_hash': getattr(authority.public_key, certid_hash_algo),
'serial_number': serial_number,
}
)
return cert_id
def format_ocsp_request(
cert: x509.Certificate,
authority: Authority,
*,
certid_hash_algo: str,
request_nonces: bool,
):
cert_id = get_certid(cert, authority, certid_hash_algo=certid_hash_algo)
request = ocsp.Request(
{
'req_cert': cert_id,
}
)
tbs_request = ocsp.TBSRequest(
{
'request_list': ocsp.Requests([request]),
}
)
if request_nonces:
nonce_extension = ocsp.TBSRequestExtension(
{
'extn_id': 'nonce',
'critical': False,
'extn_value': core.OctetString(os.urandom(16)),
}
)
tbs_request['request_extensions'] = ocsp.TBSRequestExtensions(
[nonce_extension]
)
return ocsp.OCSPRequest({'tbs_request': tbs_request})
def process_ocsp_response_data(
response_data: bytes, *, ocsp_request: ocsp.OCSPRequest, ocsp_url: str
):
try:
ocsp_response = ocsp.OCSPResponse.load(response_data)
except ValueError:
raise errors.OCSPFetchError('Failed to parse response from OCSP server')
status = ocsp_response['response_status'].native
if status != 'successful':
raise errors.OCSPValidationError(
'OCSP server at %s returned an error. Status was \'%s\'.'
% (ocsp_url, status)
)
request_nonce = ocsp_request.nonce_value
if request_nonce:
response_nonce = ocsp_response.nonce_value
# if the response did not contain the nonce extension, there's no
# point in trying to enforce it, that's the CA's problem.
# (I suppose we could give callers the option to mark the nonce
# extension as critical in the request, but that's discouraged by the
# specification)
if response_nonce and (request_nonce.native != response_nonce.native):
raise errors.OCSPValidationError(
'Unable to verify OCSP response since the request and '
'response nonces do not match'
)
return ocsp_response
T = TypeVar('T')
R = TypeVar('R')
async def queue_fetch_task(
results: Dict[T, Union[R, Exception]],
running_jobs: Dict[T, asyncio.Event],
tag: T,
async_fun: Callable[[], Awaitable[R]],
) -> Union[R, Exception]:
# use an asyncio events to make sure that we don't attempt to re-fetch
# the same tag while the job is running
# Note: this uses asyncio locking, so we only transfer control
# on 'await'.
# We use events instead of locks because we don't care about fairness,
# and events are easier to reason about.
try:
result = results[tag]
logger.debug(
f"Result for fetch job with tag {repr(tag)} was available in cache."
)
return _return_or_raise(result)
except KeyError:
pass
try:
wait_event: asyncio.Event = running_jobs[tag]
logger.debug(f"Waiting for fetch job with tag {repr(tag)} to return...")
# there's a fetch job running, wait for it to finish and then
# return the result
await wait_event.wait()
logger.debug(
f"Received completion signal for job with tag {repr(tag)}."
)
return _return_or_raise(results[tag])
except KeyError:
logger.debug(f"Starting new fetch job with tag {repr(tag)}...")
# no fetch job running, run the task and store the result
running_jobs[tag] = wait_event = asyncio.Event()
try:
result = await async_fun()
except Exception as e:
logger.debug(
f"New fetch job with tag {repr(tag)} threw an exception: {e}"
)
result = e
results[tag] = result
logger.debug(f"New fetch job with tag {repr(tag)} returned.")
# deregister event, notify waiters
del running_jobs[tag]
wait_event.set()
return _return_or_raise(result)
def _return_or_raise(result):
if isinstance(result, Exception):
raise result
return result
async def crl_job_results_as_completed(jobs):
last_e = None
at_least_one_success = False
for crl_job in asyncio.as_completed(list(jobs)):
try:
fetched_crl = await crl_job
yield fetched_crl
except errors.CRLFetchError as e:
last_e = e
if last_e is not None and not at_least_one_success:
raise last_e
async def cancel_all(pending_tasks):
pending = asyncio.gather(*pending_tasks)
pending.cancel()
try:
await pending
except asyncio.CancelledError:
pass
async def ocsp_job_get_earliest(jobs):
queue = [asyncio.create_task(coro) for coro in jobs]
ocsp_resp = last_e = None
while queue:
done, queue = await asyncio.wait(
queue, return_when=asyncio.FIRST_COMPLETED
)
for ocsp_job in done:
try:
ocsp_resp = await ocsp_job
break
except errors.OCSPFetchError as e:
last_e = e
if ocsp_resp is not None:
# cancel remaining fetch tasks
await cancel_all(queue)
return ocsp_resp
raise last_e or errors.OCSPFetchError("No OCSP results")
def gather_aia_issuer_urls(
cert: Union[x509.Certificate, cms.AttributeCertificateV2]
):
if isinstance(cert, x509.Certificate):
aia_value = cert.authority_information_access_value
else:
aia_value = get_ac_extension_value(cert, 'authority_information_access')
if aia_value is None:
return
for entry in aia_value:
if entry['access_method'].native == 'ca_issuers':
location = entry['access_location']
if location.name != 'uniform_resource_identifier':
continue
url = location.native
if url.startswith('http'):
yield url
async def complete_certificate_fetch_jobs(fetch_jobs):
for fetch_job in asyncio.as_completed(fetch_jobs):
try:
certs_fetched = await fetch_job
except errors.CertificateFetchError as e:
logger.warning(
f'Error during certificate fetch job, skipping... '
f'(Error: {e})',
)
continue
for cert in certs_fetched:
yield cert
|