iyi

Standard library, 0.22.0

std/ocsp

OCSP responses read and checked (RFC 6960), and requests built, written in iyi over std/asn1 and std/x509: what a TLS client asks of the response a server staples beside its certificate.

import std/ocsp to use it. Source: src/std/ocsp.iyi at v0.22.0.

  import std/ocsp::{OcspResponse, OcspCertStatus, OcspError, Ocsp}
  response = OcspResponse.parse(der).or_panic   # an OCSPResponse
  verdict = response.verify(leaf, issuer).or_panic
  verdict.status                                # Good, Revoked or Unknown
  request = Ocsp.request(leaf, issuer)          # an OCSPRequest's DER

OcspResponse.parse reads an OCSPResponse: its responseStatus, and for a successful one the BasicOCSPResponse - ResponseData (version, the responder by name or by key hash, producedAt, each SingleResponse's CertID, status, thisUpdate and nextUpdate, the nonce among its extensions), the signature algorithm and signature, and the certificates it carries, each read by std/x509. A responder that answered with no response (tryLater, unauthorized, ...) is an OcspError carrying that status. Everything else that is not RFC 6960's DER is an OcspError naming it: a response type other than id-pkix-ocsp-basic, a version written out where DER leaves the default v1 out, a time that is not a GeneralizedTime, a CertStatus that is none of good, revoked and unknown, a revocation reason CRLReason does not have, a CertID hashed by something other than SHA-1 or SHA-2 or whose hashes are not that hash's size, an extension twice, and a critical extension this does not understand. A nonce is read and not judged: a stapled response is the server's, fetched before any client asked, and carries none of the client's.

verify(leaf, issuer, now, distrusted:) checks, in this order, what OpenSSL's ocsp -verify checks in it, so that a verdict is refused for the same reason:

* the signature, by the issuer itself or by a delegated responder - a
  certificate the response carries, named by its responderID, issued
  and signed by the issuer, valid at *now*, carrying extKeyUsage with
  id-kp-OCSPSigning itself (anyExtendedKeyUsage is not it, as RFC 6960
  section 4.2.2.2 has it), allowed digitalSignature if it carries
  keyUsage, with no critical extension this does not understand, and
  neither a certificate in *distrusted* nor holding the key of one
  (the list `X509.verify` takes, matched as it matches a chain's;
  std/tls passes the same one, the platform's when its roots are).
  Signatures are std/x509's: SHA-1 is refused as it is in a
  certificate, and so is an RSA key under `X509::MIN_RSA_BITS` or
  past `X509::MAX_RSA_BITS`;
* a SingleResponse whose CertID is the leaf's: issuerNameHash the hash
  of the leaf's issuer name, issuerKeyHash the hash of the issuer's
  key, and serialNumber the leaf's;
* its time: thisUpdate no later than *now* plus `Ocsp::SKEW_SECONDS`,
  and nextUpdate no earlier than *now* less it - OpenSSL's five minutes
  either way. RFC 6960 lets a response leave nextUpdate out, saying
  newer information is always there; a staple a server holds would
  then be good forever, so one without it is taken for
  `Ocsp::NO_NEXT_UPDATE_SECONDS` (a day) after its thisUpdate.

The answer is the status the response gives - revoked, with its time and reason, and unknown are answers too, which the caller judges (std/tls refuses both).

A delegated responder's id-pkix-ocsp-nocheck extension is accepted, and read (Certificate#ocsp_nocheck): it says the responder's own status is not to be asked, and here it never is - with or without the extension, as OpenSSL and Go take a responder. Asking would need the revocation this module does not do (below), and the CA/Browser Forum's Baseline Requirements have every delegated responder certificate carry it.

Ocsp.request builds an OCSPRequest for one certificate, as openssl ocsp -issuer ... -cert ... -no_nonce -reqout writes it (a nonce when one is given): unsigned, version left out, the CertID under SHA-1 unless another hash is asked for.

Not done: no CRLs, and no fetching. A response is what the caller hands in - in std/tls, what the server stapled - and nothing here asks a responder over the network, follows a certificate's AIA, or keeps a cache. The revocation of anything but the leaf is not asked: an intermediate's status, which TLS 1.3 lets a server send, is not judged, and neither is a delegated responder's (above).

Pure computation over the bytes given: no platform, no clock unless now is left out, no randomness (a request's nonce is the caller's).

Ocspstruct

std/ocspiyi doc src/std/ocsp.iyi
pub struct Ocsp
  def initialize
  # A CRLReason by the name RFC 5280 section 5.3.1 gives it.
  def self.reason_name(reason : Int32) : String
  # An OCSPRequest (RFC 6960 section 4.1.1) for *leaf*, *issuer* its
  # issuer's certificate: one Request, unsigned, and a nonce extension
  # holding *nonce* when one is given.
  def self.request(leaf : Certificate, issuer : Certificate, hash : OcspHash = OcspHash::SHA1, nonce : Array(UInt8) | ::Nil = nil) : Array(UInt8)
  # A responseStatus by the name RFC 6960 gives it.
  def self.status_name(status : Int64) : String
end

OcspCertIdstruct

A CertID (RFC 6960 section 4.1.1): the hash it is made under, the hashes of the issuer's name and key, and the certificate's serial number (its two's-complement bytes, as Certificate#serial holds it).

std/ocspiyi doc src/std/ocsp.iyi
pub struct OcspCertId
  # Its DER: the hash's AlgorithmIdentifier with NULL parameters, as
  # OpenSSL writes it.
  def der : Array(UInt8)
  def hash : OcspHash
  def initialize(hash : OcspHash, issuer_name_hash : Array(UInt8), issuer_key_hash : Array(UInt8), serial : Array(UInt8))
  def issuer_key_hash : Array(UInt8)
  def issuer_name_hash : Array(UInt8)
  # Whether this is *leaf*'s CertID, *issuer* its issuer: the name hash
  # over the leaf's issuer name, the key hash over the issuer's key, and
  # the serial number the leaf's.
  def matches?(leaf : Certificate, issuer : Certificate) : Bool
  # *leaf*'s CertID, *issuer* its issuer's certificate.
  def self.of(leaf : Certificate, issuer : Certificate, hash : OcspHash = OcspHash::SHA1) : OcspCertId
  def serial : Array(UInt8)
end

OcspCertStatusenum

What a SingleResponse says of a certificate (RFC 6960 section 4.2.1).

std/ocspiyi doc src/std/ocsp.iyi
pub enum OcspCertStatus : Int32
  Good = 0
  Revoked = 1
  Unknown = 2
  def word : String
end

OcspErrorstruct

What reading or checking a response answers when it fails.

std/ocspiyi doc src/std/ocsp.iyi
pub struct OcspError
  def initialize(message : String, response_status : Int32 | ::Nil = nil)
  # The responseStatus of a responder that answered with no response: 1
  # malformedRequest, 2 internalError, 3 tryLater, 5 sigRequired, 6
  # unauthorized. Nil for every other refusal.
  def response_status : Int32 | ::Nil
  def to_s : String
end

OcspHashenum

The hashes a CertID is made under.

std/ocspiyi doc src/std/ocsp.iyi
pub enum OcspHash : Int32
  SHA1 = 0
  SHA256 = 1
  SHA384 = 2
  SHA512 = 3
  def digest(data : Array(UInt8)) : Array(UInt8)
  def self.from_oid(oid : String) : OcspHash | ::Nil
  def oid : String
  def size : Int32
end

OcspResponsestruct

std/ocspiyi doc src/std/ocsp.iyi
pub struct OcspResponse
  # The certificates the response carries, for a delegated responder.
  def certificates : Array(Certificate)
  def initialize(raw : Array(UInt8), tbs : Array(UInt8), version : Int32, responder_name : Array(UInt8) | ::Nil, responder_key_hash : Array(UInt8) | ::Nil, produced_at : Time, responses : Array(OcspSingleResponse), nonce : Array(UInt8) | ::Nil, signature_algorithm : String, signature_parameters : DerValue | ::Nil, signature : Array(UInt8), certificates : Array(Certificate))
  # The nonce extension's OCTET STRING, nil when absent.
  def nonce : Array(UInt8) | ::Nil
  # The OCSPResponse *der* holds (RFC 6960 section 4.2.1).
  def self.parse(der : Array(UInt8)) : OcspResponse | OcspError
  def produced_at : Time
  # The whole OCSPResponse as it was read, and the ResponseData the
  # signature is over.
  def raw : Array(UInt8)
  def responder_key_hash : Array(UInt8) | ::Nil
  # The responderID: a Name's DER (byName), or the SHA-1 of the
  # responder's key (byKey); one of the two is nil.
  def responder_name : Array(UInt8) | ::Nil
  # The responderID, for a sentence.
  def responder_text : String
  def responses : Array(OcspSingleResponse)
  def signature : Array(UInt8)
  # The signature algorithm's OID, and the signature.
  def signature_algorithm : String
  # The SingleResponse for *leaf*, *issuer* its issuer, or nil.
  def single_for(leaf : Certificate, issuer : Certificate) : OcspSingleResponse | ::Nil
  def tbs : Array(UInt8)
  # The status this response gives *leaf*, *issuer* its issuer's
  # certificate, checked at *now* (see the module's header), a delegated
  # responder held to *distrusted* as `X509.verify` holds a chain; or why
  # it gives none that can be taken.
  def verify(leaf : Certificate, issuer : Certificate, now : Time = Time.utc, skew_seconds : Int32 = Ocsp::SKEW_SECONDS, distrusted : Array(Array(UInt8)) | ::Nil = nil) : OcspVerdict | OcspError
  def version : Int32
end

OcspSingleResponsestruct

One SingleResponse (RFC 6960 section 4.2.1): a certificate's status, when it was revoked and why (a CRLReason, nil when not given), and the time the status is for.

std/ocspiyi doc src/std/ocsp.iyi
pub struct OcspSingleResponse
  def cert_id : OcspCertId
  def initialize(cert_id : OcspCertId, status : OcspCertStatus, revocation_time : Time | ::Nil, revocation_reason : Int32 | ::Nil, this_update : Time, next_update : Time | ::Nil)
  def next_update : Time | ::Nil
  def revocation_reason : Int32 | ::Nil
  def revocation_time : Time | ::Nil
  def status : OcspCertStatus
  def this_update : Time
end

OcspVerdictstruct

What OcspResponse#verify answers: the status of the leaf, the SingleResponse that gave it, and the certificate that signed - the issuer, or a delegated responder.

std/ocspiyi doc src/std/ocsp.iyi
pub struct OcspVerdict
  def delegated : Bool
  def initialize(status : OcspCertStatus, single : OcspSingleResponse, signer : Certificate, delegated : Bool)
  def signer : Certificate
  def single : OcspSingleResponse
  def status : OcspCertStatus
end

Error for OcspErrorimpl

std/ocspiyi doc src/std/ocsp.iyi
impl Error for OcspError
  def message : String
end