(
cert: Certificate, issuer: Certificate, uri: Union[str, bytes], ocsp_response_cache: _OCSPCache
)
| 280 | |
| 281 | |
| 282 | def _get_ocsp_response( |
| 283 | cert: Certificate, issuer: Certificate, uri: Union[str, bytes], ocsp_response_cache: _OCSPCache |
| 284 | ) -> Optional[OCSPResponse]: |
| 285 | ocsp_request = _build_ocsp_request(cert, issuer) |
| 286 | try: |
| 287 | ocsp_response = ocsp_response_cache[ocsp_request] |
| 288 | _LOGGER.debug("Using cached OCSP response.") |
| 289 | except KeyError: |
| 290 | # CSOT: use the configured timeout or 5 seconds, whichever is smaller. |
| 291 | # Note that request's timeout works differently and does not imply an absolute |
| 292 | # deadline: https://requests.readthedocs.io/en/stable/user/quickstart/#timeouts |
| 293 | timeout = max(_csot.clamp_remaining(5), 0.001) |
| 294 | try: |
| 295 | response = _post( |
| 296 | uri, |
| 297 | data=ocsp_request.public_bytes(_Encoding.DER), |
| 298 | headers={"Content-Type": "application/ocsp-request"}, |
| 299 | timeout=timeout, |
| 300 | ) |
| 301 | except _RequestException as exc: |
| 302 | _LOGGER.debug("HTTP request failed: %s", exc) |
| 303 | return None |
| 304 | if response.status_code != 200: |
| 305 | _LOGGER.debug("HTTP request returned %d", response.status_code) |
| 306 | return None |
| 307 | ocsp_response = _load_der_ocsp_response(response.content) |
| 308 | _LOGGER.debug("OCSP response status: %r", ocsp_response.response_status) |
| 309 | if ocsp_response.response_status != _OCSPResponseStatus.SUCCESSFUL: |
| 310 | return None |
| 311 | # RFC6960, Section 3.2, Number 1. Only relevant if we need to |
| 312 | # talk to the responder directly. |
| 313 | # Accessing response.serial_number raises if response status is not |
| 314 | # SUCCESSFUL. |
| 315 | if ocsp_response.serial_number != ocsp_request.serial_number: |
| 316 | _LOGGER.debug("Response serial number does not match request") |
| 317 | return None |
| 318 | if not _verify_response(issuer, ocsp_response): |
| 319 | # The response failed verification. |
| 320 | return None |
| 321 | _LOGGER.debug("Caching OCSP response.") |
| 322 | ocsp_response_cache[ocsp_request] = ocsp_response |
| 323 | |
| 324 | return ocsp_response |
| 325 | |
| 326 | |
| 327 | def _ocsp_callback(conn: Connection, ocsp_bytes: bytes, user_data: Optional[_CallbackData]) -> bool: |
no test coverage detected