readResponseStatus method

OcspCertificateStatusResult readResponseStatus({
  1. required Uint8List responseDer,
  2. required Pointer<X509> certificate,
  3. required Pointer<X509> issuerCertificate,
  4. String hashAlgorithm = 'SHA1',
  5. bool requireSuccessfulResponse = true,
})

Parses responseDer and extracts status for certificate/issuerCertificate.

Throws OpenSslException if response is malformed, unsuccessful, or does not contain status for the target certificate.

Implementation

OcspCertificateStatusResult readResponseStatus({
  required Uint8List responseDer,
  required Pointer<X509> certificate,
  required Pointer<X509> issuerCertificate,
  String hashAlgorithm = 'SHA1',
  bool requireSuccessfulResponse = true,
}) {
  final bindings = _context.bindings;
  final response = _d2iOcspResponse(responseDer);
  if (response == nullptr) {
    throw OpenSslException('Failed to parse OCSP response');
  }

  try {
    final responseStatus = bindings.OCSP_response_status(response);
    if (requireSuccessfulResponse && responseStatus != 0) {
      throw OpenSslException(
        'OCSP response status is not successful: $responseStatus',
      );
    }

    final basic = bindings.OCSP_response_get1_basic(response);
    if (basic == nullptr) {
      throw OpenSslException('Failed to extract OCSP basic response');
    }

    try {
      final digest = _getDigestByName(hashAlgorithm);
      final certId = bindings.OCSP_cert_to_id(
        digest,
        certificate,
        issuerCertificate,
      );
      if (certId == nullptr) {
        throw OpenSslException('Failed to create OCSP cert id');
      }

      try {
        final status = calloc<Int>();
        final reason = calloc<Int>();
        final revTime = calloc<Pointer<ASN1_GENERALIZEDTIME>>();
        final thisUpd = calloc<Pointer<ASN1_GENERALIZEDTIME>>();
        final nextUpd = calloc<Pointer<ASN1_GENERALIZEDTIME>>();

        try {
          final found = bindings.OCSP_resp_find_status(
            basic,
            certId,
            status,
            reason,
            revTime,
            thisUpd,
            nextUpd,
          );
          if (found != 1) {
            throw OpenSslException(
              'OCSP response does not contain status for certificate',
            );
          }

          final parsedStatus = _statusFromNative(status.value);
          final parsedReason = parsedStatus == OcspCertStatus.revoked
              ? _reasonFromNative(reason.value)
              : null;

          return OcspCertificateStatusResult(
            responseStatusCode: responseStatus,
            status: parsedStatus,
            revocationReason: parsedReason,
            revocationTime: _parseGeneralizedTime(revTime.value),
            thisUpdate: _parseGeneralizedTime(thisUpd.value),
            nextUpdate: _parseGeneralizedTime(nextUpd.value),
          );
        } finally {
          calloc.free(nextUpd);
          calloc.free(thisUpd);
          calloc.free(revTime);
          calloc.free(reason);
          calloc.free(status);
        }
      } finally {
        bindings.OCSP_CERTID_free(certId);
      }
    } finally {
      bindings.OCSP_BASICRESP_free(basic);
    }
  } finally {
    bindings.OCSP_RESPONSE_free(response);
  }
}