---
title: "Generate an offline OCSP response in C#"
url: https://recursiveai.org/onecrypto/cookbook/preproduce-ocsp-response/
description: "Sign a DER-encoded OCSP response in C# with a local test CA, then reuse it for a request without a nonce. No server or network required."
version: "0.4.0-preview.2"
---

# Generate an offline OCSP response in C#

**OneCrypto** is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/preproduce-ocsp-response/preproduce-ocsp-response.zip)

Sign an OCSP response with no server or HTTP request.

```shell
dotnet add package RecursiveAI.OneCrypto --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

The full program uses a test CA to supply the responder and certificate ID.

```csharp
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.Issuance;

static OcspResponse WriteResponse(
    OcspResponder responder, OcspCertId id,
    DateTimeOffset now)
{
    var reply = responder.Preproduce([id], now);
    File.WriteAllBytes("ocsp.der",
        reply.Encoded.ToArray());
    Console.WriteLine("Wrote ocsp.der");
    return reply;
}
```

Unzip [the example](https://recursiveai.org/onecrypto/cookbook/preproduce-ocsp-response/preproduce-ocsp-response.zip), then `dotnet run`.

```text
Wrote ocsp.der
```

## Full program

```csharp
using System.Security.Cryptography;
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.Issuance;
using RecursiveAI.OneCrypto.X509;

var now = DateTimeOffset.UtcNow;
using var issuerKey = RSA.Create(2048);
var signer = X509Signer.FromRsa(issuerKey, HashAlgorithmName.SHA256);
var ca = CertificateAuthority.CreateRoot(
    DistinguishedName.Create((KnownOids.CommonName, "Example Test CA")),
    signer, now.AddDays(-1), TimeSpan.FromDays(30));
using var leafKey = RSA.Create(2048);
var leaf = ca.Issue(
    X509Signer.FromRsa(leafKey, HashAlgorithmName.SHA256).PublicKey,
    DistinguishedName.Create((KnownOids.CommonName, "www.example.test")),
    new CertificateIssueOptions
    {
        Profile = CertificateProfile.TlsServer,
        SubjectAlternativeNames = [GeneralName.Dns("www.example.test")],
        NotBefore = now.AddHours(-1),
        Validity = TimeSpan.FromDays(7),
    });

var source = new InMemoryRevocationSource(TimeSpan.FromHours(1));
source.SetGood(leaf.SerialNumber.Span);
var store = new InMemoryPreproducedResponses();
var responder = new OcspResponder(new OcspResponderOptions
{
    Issuer = ca.Certificate,
    ResponderCertificate = ca.Certificate,
    Signer = signer,
    Source = source,
    Preproduced = store,
});
var id = OcspCertId.Create(leaf, ca.Certificate);
var response = WriteResponse(responder, id, now);
Console.WriteLine($"Certificate: {response.BasicResponse!.Responses.Single().Status}");

store.Set(id, response);
var request = OcspRequestBuilder.Create(new OcspRequestOptions { Requests = [id] });
var served = responder.Handle(request.Encoded, now.AddMinutes(5));
bool reused = served.Encoded.Span.SequenceEqual(response.Encoded.Span);
Console.WriteLine($"Reused signed response: {reused}");

static OcspResponse WriteResponse(
    OcspResponder responder, OcspCertId id,
    DateTimeOffset now)
{
    var reply = responder.Preproduce([id], now);
    File.WriteAllBytes("ocsp.der",
        reply.Encoded.ToArray());
    Console.WriteLine("Wrote ocsp.der");
    return reply;
}
```

```text
Wrote ocsp.der
Certificate: Good
Reused signed response: True
```

The program records the synthetic leaf as not revoked, signs its response with the CA key and saves the encoded bytes to disk. Then it stores the response and sends a request directly to the handler, five minutes after signing. The byte-for-byte comparison shows it got the stored response back. The CA and its key exist only in this run; don’t use them for real certificates. If the source returns unusable status data, such as a `nextUpdate` already in the past, `Preproduce` throws `InvalidOperationException` before the file is written.

## Variations

**Answer a request that includes a nonce.** Replace the request and reuse check after `store.Set` with this:

```csharp
byte[] nonce = RandomNumberGenerator.GetBytes(16);
var request = OcspRequestBuilder.Create(
    new OcspRequestOptions { Requests = [id], Nonce = nonce });
var fresh = responder.Handle(request.Encoded, now.AddMinutes(5));
bool echoed = fresh.BasicResponse?.Nonce is { } echo &&
    echo.Span.SequenceEqual(nonce);
Console.WriteLine($"Nonce echoed: {echoed}");
```

```text
Wrote ocsp.der
Certificate: Good
Nonce echoed: True
```

## Related recipes

- [Create a self-signed root certificate](https://recursiveai.org/onecrypto/cookbook/create-self-signed-root-certificate/)
- [Issue a certificate from a CSR](https://recursiveai.org/onecrypto/cookbook/issue-certificate-from-csr/)

## The short answer

Build an `OcspResponder` with the issuing CA’s certificate, an authorized signer and a source of certificate status. Call `Preproduce([id], now)` and write `response.Encoded` to a file. Those are the binary DER bytes of the signed OCSP reply, ready to store or distribute. OneCrypto does not need an HTTP request to create them, and the sample does not look up status over the network.

## What is an OCSP responder?

An OCSP responder signs a statement about one or more certificates. The answer can say `good`, `revoked`, or `unknown`, based on the responder’s status source. In this example the issuing CA signs directly. You can also use a delegate that the issuing CA certified for `id-kp-OCSPSigning`; see the [responder configuration](https://recursiveai.org/onecrypto/docs/ocsp-responder/) and [RFC 6960 §4.2.2.2](https://www.rfc-editor.org/rfc/rfc6960.html#section-4.2.2.2). A certificate’s public bytes alone aren’t enough to produce an authorized response: the responder needs access to the signing key and reliable revocation records.

## How do I check whether a pre-signed OCSP response is still current?

[RFC 5019 §2.2.4](https://www.rfc-editor.org/rfc/rfc5019.html#section-2.2.4) distinguishes three times. `thisUpdate` is when the status was known to be correct, `nextUpdate` is when newer information will be available by, and `producedAt` is when the response was signed. Refresh the stored answer before `nextUpdate`, and replace it when its status changes. Removing a cached copy from your server doesn’t invalidate copies a client already has. On the client side, check the signature and signer’s authorization, the certificate ID and freshness; `good` is a revocation answer, not a substitute for validating the certificate chain.

## Can a pre-produced OCSP response include a nonce?

A nonce is a value supplied by the client that ties a reply to that request. `Preproduce` signs without one. OneCrypto reuses a stored response only for a current, matching, single-certificate request without a nonce, after checking the stored signature against the responder’s key. If the request carries a nonce, the handler signs a fresh reply that echoes it. That path needs access to the signer when the request arrives; this offline example does not implement a network service.
