OCSP

OCSP (Online Certificate Status Protocol) is a method of checking the revocation status of certificates. It is specified in RFC 6960 , as well as other obsoleted RFCs.

Loading Requests

cryptography.x509.ocsp. load_der_ocsp_request ( data )

Added in version 2.4.

Deserialize an OCSP request from DER encoded data.

Parameters :

data ( bytes ) – The DER encoded OCSP request data.

Returns :

An instance of OCSPRequest .

>>> from cryptography.x509 import ocsp
>>> ocsp_req = ocsp.load_der_ocsp_request(der_ocsp_req)
>>> print(ocsp_req.serial_number)
872625873161273451176241581705670534707360122361
													

Creating Requests

class cryptography.x509.ocsp. OCSPRequestBuilder [source]

Added in version 2.4.

This class is used to create OCSPRequest objects.

add_certificate ( cert , issuer , algorithm ) [source]

Adds a request using a certificate, issuer certificate, and hash algorithm. You can call this method or add_certificate_by_hash only once.

Parameters :
add_certificate_by_hash ( issuer_name_hash , issuer_key_hash , serial_number , algorithm ) [source]

Added in version 39.0.0.

Adds a request using the issuer’s name hash, key hash, the certificate serial number and hash algorithm. You can call this method or add_certificate only once.

Parameters :
  • issuer_name_hash ( bytes ) – The hash of the issuer’s DER encoded name using the same hash algorithm as the one specified in the algorithm parameter.

  • issuer_key_hash ( bytes ) – The hash of the issuer’s public key bit string DER encoding using the same hash algorithm as the one specified in the algorithm parameter.

  • serial_number ( int ) – The serial number of the certificate being checked.

  • algorithm – A HashAlgorithm instance. For OCSP only SHA1 , SHA224 , SHA256 , SHA384 , and SHA512 are allowed.

add_extension ( extval , critical ) [source]

Adds an extension to the request.

Parameters :
  • extval – An extension conforming to the ExtensionType interface.

  • critical – Set to True if the extension must be understood and handled.

build ( ) [source]
Returns :

A new OCSPRequest .

>>> from cryptography.hazmat.primitives import serialization
>>> from cryptography.hazmat.primitives.hashes import SHA256
>>> from cryptography.x509 import load_pem_x509_certificate, ocsp
>>> cert = load_pem_x509_certificate(pem_cert)
>>> issuer = load_pem_x509_certificate(pem_issuer)
>>> builder = ocsp.OCSPRequestBuilder()
>>> # SHA256 is in this example because while RFC 5019 originally
>>> # required SHA1 RFC 6960 updates that to SHA256.
>>> # However, depending on your requirements you may need to use SHA1
>>> # for compatibility reasons.
>>> builder = builder.add_certificate(cert, issuer, SHA256())
>>> req = builder.build()
>>> base64.b64encode(req.public_bytes(serialization.Encoding.DER))
b'MF8wXTBbMFkwVzANBglghkgBZQMEAgEFAAQgn3BowBaoh77h17ULfkX6781dUDPD82Taj8wO1jZWhZoEINxPgjoQth3w7q4AouKKerMxIMIuUG4EuWU2pZfwih52AgI/IA=='