buildRequestDer method

Uint8List buildRequestDer({
  1. required Pointer<X509> certificate,
  2. required Pointer<X509> issuerCertificate,
  3. String hashAlgorithm = 'SHA1',
  4. int? nonceLength,
})

Builds a DER-encoded OCSP request for certificate/issuerCertificate.

hashAlgorithm defaults to SHA1 to match OpenSSL CLI behavior. Set nonceLength > 0 to include a nonce.

Implementation

Uint8List buildRequestDer({
  required Pointer<X509> certificate,
  required Pointer<X509> issuerCertificate,
  String hashAlgorithm = 'SHA1',
  int? nonceLength,
}) {
  final bindings = _context.bindings;

  final request = bindings.OCSP_REQUEST_new();
  if (request == nullptr) {
    throw OpenSslException('Failed to create OCSP request');
  }

  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');
    }

    final oneReq = bindings.OCSP_request_add0_id(request, certId);
    if (oneReq == nullptr) {
      bindings.OCSP_CERTID_free(certId);
      throw OpenSslException('Failed to add cert id to OCSP request');
    }

    if (nonceLength != null && nonceLength > 0) {
      final nonce = calloc<UnsignedChar>(nonceLength);
      try {
        if (bindings.RAND_bytes(nonce, nonceLength) != 1) {
          throw OpenSslException('Failed to generate OCSP nonce');
        }
        if (bindings.OCSP_request_add1_nonce(request, nonce, nonceLength) !=
            1) {
          throw OpenSslException('Failed to add OCSP nonce');
        }
      } finally {
        calloc.free(nonce);
      }
    }

    return _encodeRequest(request);
  } finally {
    bindings.OCSP_REQUEST_free(request);
  }
}