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.
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.
>>> fromcryptography.hazmat.primitivesimportserialization>>> fromcryptography.hazmat.primitives.hashesimportSHA256>>> fromcryptography.x509importload_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=='
class
cryptography.x509.ocsp.
OCSPResponseBuilder
[source]
Added in version 2.4.
This class is used to create
OCSPResponse
objects. You cannot set
produced_at
on OCSP responses at this time.
Instead the field is set to current UTC time when calling
sign
. For
unsuccessful statuses call the class method
build_unsuccessful()
.
cert_status
– An item from the
OCSPCertStatus
enumeration.
this_update
– A naïve
datetime.datetime
object
representing the most recent time in UTC at which the status being
indicated is known by the responder to be correct.
next_update
– A naïve
datetime.datetime
object or
None
. The time in UTC at or before which newer information will
be available about the status of the certificate.
revocation_time
– A naïve
datetime.datetime
object or
None
if the
cert
is not revoked. The time in UTC at which
the certificate was revoked.
revocation_reason
– An item from the
ReasonFlags
enumeration or
None
if
the
cert
is not revoked.
Add additional certificates that should be used to verify the
signature on the response. This is typically used when the responder
utilizes an OCSP delegate.
Set the
responderID
on the OCSP response. This is the data a
client will use to determine what certificate signed the response.
Parameters
:
responder_cert
– The
Certificate
object for the certificate whose private key will sign the
OCSP response. If the certificate and key do not match an
error will be raised when calling
sign
.
>>> importdatetime>>> fromcryptography.hazmat.primitivesimporthashes,serialization>>> fromcryptography.x509importload_pem_x509_certificate,ocsp>>> cert=load_pem_x509_certificate(pem_cert)>>> issuer=load_pem_x509_certificate(pem_issuer)>>> responder_cert=load_pem_x509_certificate(pem_responder_cert)>>> responder_key=serialization.load_pem_private_key(pem_responder_key,None)>>> builder=ocsp.OCSPResponseBuilder()>>> # 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_response(... cert=cert,issuer=issuer,algorithm=hashes.SHA256(),... cert_status=ocsp.OCSPCertStatus.GOOD,... this_update=datetime.datetime.now(),... next_update=datetime.datetime.now(),... revocation_time=None,revocation_reason=None... ).responder_id(... ocsp.OCSPResponderEncoding.HASH,responder_cert... )>>> response=builder.sign(responder_key,hashes.SHA256())>>> response.certificate_status<OCSPCertStatus.GOOD: 0>
Creates an unsigned OCSP response which can then be serialized and
sent to clients.
build_unsuccessful
may only be called with a
OCSPResponseStatus
that is not
SUCCESSFUL
. Since this is a class method note that no other
methods can or should be called as unsuccessful statuses do not
encode additional data.
A list of zero or more
Certificate
objects
used to help build a chain to verify the OCSP response. This situation
occurs when the OCSP responder uses a delegate certificate.
May be returned by an OCSP responder when queried for a certificate for
which the responder is unaware or an issuer for which the responder is
not authoritative.
class
cryptography.x509.ocsp.
OCSPCertStatus
[source]
Added in version 2.4.
An enumeration of certificate statuses in an OCSP response.