native-ossl 0.1.1

Native Rust idiomatic bindings to OpenSSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! OCSP — Online Certificate Status Protocol (`RFC 2560` / `RFC 6960`).
//!
//! Provides the full client-side OCSP stack:
//!
//! - [`OcspCertId`] — identifies a certificate to query
//! - [`OcspRequest`] — encodes the DER request to send to a responder
//! - [`OcspResponse`] — decodes and validates a DER response
//! - [`OcspBasicResp`] — the signed inner response; drives per-cert status lookup
//! - [`OcspSingleStatus`] — per-certificate status result from [`OcspBasicResp::find_status`]
//!
//! # Typical flow
//!
//! ```ignore
//! // Build a request for a specific certificate.
//! let id = OcspCertId::from_cert(None, &end_entity_cert, &issuer_cert)?;
//! let mut req = OcspRequest::new()?;
//! req.add_cert_id(id)?;
//! let req_der = req.to_der()?;
//!
//! // ... send req_der over HTTP, receive resp_der ...
//!
//! let resp = OcspResponse::from_der(&resp_der)?;
//! assert_eq!(resp.status(), OcspResponseStatus::Successful);
//!
//! let basic = resp.basic()?;
//! basic.verify(&trust_store, 0)?;
//!
//! let id2 = OcspCertId::from_cert(None, &end_entity_cert, &issuer_cert)?;
//! match basic.find_status(&id2)? {
//!     Some(s) if s.cert_status == OcspCertStatus::Good => println!("certificate is good"),
//!     Some(s) => println!("certificate status: {:?}", s.cert_status),
//!     None => println!("certificate not found in response"),
//! }
//! ```
//!
//! HTTP transport is **out of scope** — the caller is responsible for fetching
//! the OCSP response from the responder URL and passing the raw DER bytes.

use crate::bio::MemBio;
use crate::error::ErrorStack;
use native_ossl_sys as sys;

// ── OcspResponseStatus ────────────────────────────────────────────────────────

/// OCSP response status (RFC 6960 §4.2.1).
///
/// This is the *top-level* status of the response packet itself, not the status
/// of any individual certificate.  A `Successful` response still requires
/// per-certificate inspection via [`OcspBasicResp::find_status`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcspResponseStatus {
    /// `successful` (0) — Response packet is valid.
    Successful,
    /// `malformedRequest` (1) — Server could not parse the request.
    MalformedRequest,
    /// `internalError` (2) — Server internal error.
    InternalError,
    /// `tryLater` (3) — Server is busy; retry later.
    TryLater,
    /// `sigRequired` (5) — Signed request required by policy.
    SigRequired,
    /// `unauthorized` (6) — Unauthorized request.
    Unauthorized,
    /// Unknown status code (forward-compatibility guard).
    Unknown(i32),
}

impl From<i32> for OcspResponseStatus {
    fn from(v: i32) -> Self {
        match v {
            0 => Self::Successful,
            1 => Self::MalformedRequest,
            2 => Self::InternalError,
            3 => Self::TryLater,
            5 => Self::SigRequired,
            6 => Self::Unauthorized,
            n => Self::Unknown(n),
        }
    }
}

// ── OcspCertStatus ────────────────────────────────────────────────────────────

/// Per-certificate revocation status from an `OCSP_SINGLERESP`.
///
/// Returned inside [`OcspSingleStatus`] by [`OcspBasicResp::find_status`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OcspCertStatus {
    /// Certificate is currently valid (`V_OCSP_CERTSTATUS_GOOD = 0`).
    Good,
    /// Certificate has been revoked (`V_OCSP_CERTSTATUS_REVOKED = 1`).
    ///
    /// `reason` is one of the `CRLReason` codes (RFC 5280 §5.3.1):
    /// 0=unspecified, 1=keyCompromise, 2=cACompromise, 3=affiliationChanged,
    /// 4=superseded, 5=cessationOfOperation, 6=certificateHold, 8=removeFromCRL,
    /// 9=privilegeWithdrawn, 10=aACompromise.  -1 means no reason was given.
    Revoked { reason: i32 },
    /// Responder does not know this certificate (`V_OCSP_CERTSTATUS_UNKNOWN = 2`).
    Unknown,
}

impl OcspCertStatus {
    fn from_raw(status: i32, reason: i32) -> Self {
        match status {
            0 => Self::Good,
            1 => Self::Revoked { reason },
            _ => Self::Unknown,
        }
    }
}

// ── OcspSingleStatus ──────────────────────────────────────────────────────────

/// Status of a single certificate, returned by [`OcspBasicResp::find_status`].
#[derive(Debug, Clone)]
pub struct OcspSingleStatus {
    /// Per-certificate status.
    pub cert_status: OcspCertStatus,
    /// `thisUpdate` time as a human-readable UTC string, if present.
    pub this_update: Option<String>,
    /// `nextUpdate` time as a human-readable UTC string, if present.
    pub next_update: Option<String>,
    /// Revocation time as a human-readable UTC string (`cert_status == Revoked` only).
    pub revocation_time: Option<String>,
}

// ── OcspCertId ───────────────────────────────────────────────────────────────

/// Certificate identifier for OCSP (`OCSP_CERTID*`).
///
/// Created from a subject certificate and its issuer with [`OcspCertId::from_cert`].
/// Add to a request with [`OcspRequest::add_cert_id`], or use to look up a status
/// in a response with [`OcspBasicResp::find_status`].
///
/// `Clone` is implemented via `OCSP_CERTID_dup`.
pub struct OcspCertId {
    ptr: *mut sys::OCSP_CERTID,
}

unsafe impl Send for OcspCertId {}

impl Clone for OcspCertId {
    fn clone(&self) -> Self {
        let ptr = unsafe { sys::OCSP_CERTID_dup(self.ptr) };
        // OCSP_CERTID_dup returns null only on allocation failure; treat as abort.
        assert!(!ptr.is_null(), "OCSP_CERTID_dup: allocation failure");
        OcspCertId { ptr }
    }
}

impl Drop for OcspCertId {
    fn drop(&mut self) {
        unsafe { sys::OCSP_CERTID_free(self.ptr) };
    }
}

impl OcspCertId {
    /// Build a cert ID from a subject certificate and its direct issuer.
    ///
    /// `digest` is the hash algorithm used to hash the issuer name and key
    /// (default: SHA-1 when `None`, per RFC 6960).  SHA-1 is required by most
    /// deployed OCSP responders; pass `Some(sha256_alg)` only when the responder
    /// is known to support it.
    ///
    /// # Errors
    pub fn from_cert(
        digest: Option<&crate::digest::DigestAlg>,
        subject: &crate::x509::X509,
        issuer: &crate::x509::X509,
    ) -> Result<Self, ErrorStack> {
        let dgst_ptr = digest.map_or(std::ptr::null(), crate::digest::DigestAlg::as_ptr);
        let ptr = unsafe { sys::OCSP_cert_to_id(dgst_ptr, subject.as_ptr(), issuer.as_ptr()) };
        if ptr.is_null() {
            return Err(ErrorStack::drain());
        }
        Ok(OcspCertId { ptr })
    }
}

// ── OcspRequest ───────────────────────────────────────────────────────────────

/// An OCSP request (`OCSP_REQUEST*`).
///
/// Build with [`OcspRequest::new`], populate with [`OcspRequest::add_cert_id`],
/// then encode with [`OcspRequest::to_der`] and send to the OCSP responder.
pub struct OcspRequest {
    ptr: *mut sys::OCSP_REQUEST,
}

unsafe impl Send for OcspRequest {}

impl Drop for OcspRequest {
    fn drop(&mut self) {
        unsafe { sys::OCSP_REQUEST_free(self.ptr) };
    }
}

impl OcspRequest {
    /// Create a new, empty OCSP request.
    ///
    /// # Errors
    pub fn new() -> Result<Self, ErrorStack> {
        let ptr = unsafe { sys::OCSP_REQUEST_new() };
        if ptr.is_null() {
            return Err(ErrorStack::drain());
        }
        Ok(OcspRequest { ptr })
    }

    /// Decode an OCSP request from DER bytes.
    ///
    /// # Errors
    pub fn from_der(der: &[u8]) -> Result<Self, ErrorStack> {
        let mut ptr = std::ptr::null_mut::<sys::OCSP_REQUEST>();
        let mut p = der.as_ptr();
        let len = i64::try_from(der.len()).unwrap_or(i64::MAX);
        let result = unsafe {
            sys::d2i_OCSP_REQUEST(std::ptr::addr_of_mut!(ptr), std::ptr::addr_of_mut!(p), len)
        };
        if result.is_null() {
            return Err(ErrorStack::drain());
        }
        Ok(OcspRequest { ptr })
    }

    /// Add a certificate identifier to the request.
    ///
    /// `cert_id` ownership is transferred to the request (`add0` semantics);
    /// the `OcspCertId` is consumed.
    ///
    /// # Errors
    pub fn add_cert_id(&mut self, cert_id: OcspCertId) -> Result<(), ErrorStack> {
        // OCSP_request_add0_id transfers ownership of the CERTID on success only.
        // Do not forget cert_id until after the null check — on failure the CERTID
        // is NOT consumed by OpenSSL and must still be freed by our Drop impl.
        let rc = unsafe { sys::OCSP_request_add0_id(self.ptr, cert_id.ptr) };
        if rc.is_null() {
            return Err(ErrorStack::drain());
        }
        // Success: ownership transferred; suppress Drop.
        std::mem::forget(cert_id);
        Ok(())
    }

    /// Encode the OCSP request to DER bytes.
    ///
    /// # Errors
    pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        let len = unsafe { sys::i2d_OCSP_REQUEST(self.ptr, std::ptr::null_mut()) };
        if len < 0 {
            return Err(ErrorStack::drain());
        }
        #[allow(clippy::cast_sign_loss)] // len > 0 checked above
        let mut buf = vec![0u8; len as usize];
        let mut out_ptr = buf.as_mut_ptr();
        let written = unsafe { sys::i2d_OCSP_REQUEST(self.ptr, std::ptr::addr_of_mut!(out_ptr)) };
        if written < 0 {
            return Err(ErrorStack::drain());
        }
        #[allow(clippy::cast_sign_loss)] // written >= 0 checked above
        buf.truncate(written as usize);
        Ok(buf)
    }
}

// ── OcspBasicResp ─────────────────────────────────────────────────────────────

/// The signed inner OCSP response (`OCSP_BASICRESP*`).
///
/// Extracted from an [`OcspResponse`] via [`OcspResponse::basic`].
/// Provides signature verification and per-certificate status lookup.
pub struct OcspBasicResp {
    ptr: *mut sys::OCSP_BASICRESP,
}

unsafe impl Send for OcspBasicResp {}

impl Drop for OcspBasicResp {
    fn drop(&mut self) {
        unsafe { sys::OCSP_BASICRESP_free(self.ptr) };
    }
}

impl OcspBasicResp {
    /// Verify the response signature against `store`.
    ///
    /// `flags` is passed directly to `OCSP_basic_verify` (use 0 for defaults,
    /// which verifies the signature and checks the signing certificate chain).
    ///
    /// Returns `Ok(true)` if the signature is valid.
    ///
    /// # Errors
    pub fn verify(&self, store: &crate::x509::X509Store, flags: u64) -> Result<bool, ErrorStack> {
        match unsafe {
            sys::OCSP_basic_verify(self.ptr, std::ptr::null_mut(), store.as_ptr(), flags)
        } {
            1 => Ok(true),
            0 => Ok(false),
            _ => Err(ErrorStack::drain()),
        }
    }

    /// Number of `SingleResponse` entries in this basic response.
    #[must_use]
    pub fn count(&self) -> usize {
        let n = unsafe { sys::OCSP_resp_count(self.ptr) };
        usize::try_from(n).unwrap_or(0)
    }

    /// Look up the status for a specific certificate by its [`OcspCertId`].
    ///
    /// Returns `Ok(Some(status))` if the responder included a `SingleResponse`
    /// for that certificate, `Ok(None)` if not found, or `Err` on a fatal
    /// OpenSSL error.
    ///
    /// The `cert_id` is passed by shared reference; its pointer is only used
    /// for the duration of this call (`OCSP_resp_find_status` does not store it).
    ///
    /// # Errors
    pub fn find_status(
        &self,
        cert_id: &OcspCertId,
    ) -> Result<Option<OcspSingleStatus>, ErrorStack> {
        let mut status: i32 = -1;
        let mut reason: i32 = -1;
        let mut revtime: *mut sys::ASN1_GENERALIZEDTIME = std::ptr::null_mut();
        let mut thisupd: *mut sys::ASN1_GENERALIZEDTIME = std::ptr::null_mut();
        let mut nextupd: *mut sys::ASN1_GENERALIZEDTIME = std::ptr::null_mut();

        let rc = unsafe {
            sys::OCSP_resp_find_status(
                self.ptr,
                cert_id.ptr,
                std::ptr::addr_of_mut!(status),
                std::ptr::addr_of_mut!(reason),
                std::ptr::addr_of_mut!(revtime),
                std::ptr::addr_of_mut!(thisupd),
                std::ptr::addr_of_mut!(nextupd),
            )
        };

        // rc == 1 → found; rc == 0 → not found; anything else → error.
        match rc {
            1 => Ok(Some(OcspSingleStatus {
                cert_status: OcspCertStatus::from_raw(status, reason),
                this_update: generalizedtime_to_str(thisupd),
                next_update: generalizedtime_to_str(nextupd),
                revocation_time: generalizedtime_to_str(revtime),
            })),
            0 => Ok(None),
            _ => Err(ErrorStack::drain()),
        }
    }

    /// Validate the `thisUpdate` / `nextUpdate` window of a `SingleResponse`.
    ///
    /// `sec` is the acceptable clock-skew in seconds (typically 300).
    /// `maxsec` limits how far in the future `nextUpdate` may be (-1 = no limit).
    ///
    /// # Errors
    pub fn check_validity(
        &self,
        cert_id: &OcspCertId,
        sec: i64,
        maxsec: i64,
    ) -> Result<bool, ErrorStack> {
        // Re-run find_status to get thisupd / nextupd pointers.
        let mut thisupd: *mut sys::ASN1_GENERALIZEDTIME = std::ptr::null_mut();
        let mut nextupd: *mut sys::ASN1_GENERALIZEDTIME = std::ptr::null_mut();
        let rc = unsafe {
            sys::OCSP_resp_find_status(
                self.ptr,
                cert_id.ptr,
                std::ptr::null_mut(), // status
                std::ptr::null_mut(), // reason
                std::ptr::null_mut(), // revtime
                std::ptr::addr_of_mut!(thisupd),
                std::ptr::addr_of_mut!(nextupd),
            )
        };
        // rc == 1 → found; rc == 0 → not found; negative → fatal error.
        match rc {
            1 => {}
            0 => return Ok(false),
            _ => return Err(ErrorStack::drain()),
        }
        match unsafe { sys::OCSP_check_validity(thisupd, nextupd, sec, maxsec) } {
            1 => Ok(true),
            0 => Ok(false),
            _ => Err(ErrorStack::drain()),
        }
    }
}

// ── OcspResponse ──────────────────────────────────────────────────────────────

/// An OCSP response (`OCSP_RESPONSE*`).
///
/// Decode from DER with [`OcspResponse::from_der`].  Check the top-level
/// [`OcspResponse::status`], then extract the signed inner response with
/// [`OcspResponse::basic`] for per-certificate status lookup.
pub struct OcspResponse {
    ptr: *mut sys::OCSP_RESPONSE,
}

unsafe impl Send for OcspResponse {}

impl Drop for OcspResponse {
    fn drop(&mut self) {
        unsafe { sys::OCSP_RESPONSE_free(self.ptr) };
    }
}

impl OcspResponse {
    /// Decode an OCSP response from DER bytes.
    ///
    /// # Errors
    pub fn from_der(der: &[u8]) -> Result<Self, ErrorStack> {
        let mut ptr = std::ptr::null_mut::<sys::OCSP_RESPONSE>();
        let mut p = der.as_ptr();
        let len = i64::try_from(der.len()).unwrap_or(i64::MAX);
        let result = unsafe {
            sys::d2i_OCSP_RESPONSE(std::ptr::addr_of_mut!(ptr), std::ptr::addr_of_mut!(p), len)
        };
        if result.is_null() {
            return Err(ErrorStack::drain());
        }
        Ok(OcspResponse { ptr })
    }

    /// Encode the OCSP response to DER bytes.
    ///
    /// # Errors
    pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack> {
        let len = unsafe { sys::i2d_OCSP_RESPONSE(self.ptr, std::ptr::null_mut()) };
        if len < 0 {
            return Err(ErrorStack::drain());
        }
        #[allow(clippy::cast_sign_loss)] // len > 0 checked above
        let mut buf = vec![0u8; len as usize];
        let mut out_ptr = buf.as_mut_ptr();
        let written = unsafe { sys::i2d_OCSP_RESPONSE(self.ptr, std::ptr::addr_of_mut!(out_ptr)) };
        if written < 0 {
            return Err(ErrorStack::drain());
        }
        #[allow(clippy::cast_sign_loss)] // written >= 0 checked above
        buf.truncate(written as usize);
        Ok(buf)
    }

    /// Overall OCSP response status (top-level packet status, not cert status).
    ///
    /// A `Successful` value means the server processed the request; it does not
    /// mean any individual certificate is good.  Use [`Self::basic`] and then
    /// [`OcspBasicResp::find_status`] for per-certificate results.
    #[must_use]
    pub fn status(&self) -> OcspResponseStatus {
        OcspResponseStatus::from(unsafe { sys::OCSP_response_status(self.ptr) })
    }

    /// Extract the signed inner response (`OCSP_BASICRESP*`).
    ///
    /// Only valid when [`Self::status`] is [`OcspResponseStatus::Successful`].
    ///
    /// # Errors
    ///
    /// Returns `Err` if the response has no basic response body (e.g. the
    /// top-level status is not `Successful`).
    pub fn basic(&self) -> Result<OcspBasicResp, ErrorStack> {
        let ptr = unsafe { sys::OCSP_response_get1_basic(self.ptr) };
        if ptr.is_null() {
            return Err(ErrorStack::drain());
        }
        Ok(OcspBasicResp { ptr })
    }

    /// Convenience: verify the basic response signature and look up a cert status
    /// in one call.
    ///
    /// Equivalent to `resp.basic()?.verify(store, 0)?; resp.basic()?.find_status(id)`.
    ///
    /// # Errors
    pub fn verified_status(
        &self,
        store: &crate::x509::X509Store,
        cert_id: &OcspCertId,
    ) -> Result<Option<OcspSingleStatus>, ErrorStack> {
        let basic = self.basic()?;
        // verify() returns Ok(false) when the signature is invalid — treat that
        // as an error to prevent certificate status from an unverified response.
        if !basic.verify(store, 0)? {
            return Err(ErrorStack::drain());
        }
        basic.find_status(cert_id)
    }

    /// Build a minimal `OCSP_RESPONSE` (status = successful, no basic response)
    /// and return it as DER. Used for testing only.
    #[cfg(test)]
    fn new_successful_der() -> Vec<u8> {
        // DER: SEQUENCE { ENUMERATED 0 }
        // OCSPResponseStatus successful(0) with no responseBytes.
        vec![0x30, 0x03, 0x0A, 0x01, 0x00]
    }
}

// ── Private helpers ───────────────────────────────────────────────────────────

/// Convert an `ASN1_GENERALIZEDTIME*` (which is really `ASN1_STRING*`) to a
/// human-readable string via `ASN1_TIME_print` on a memory BIO.
fn generalizedtime_to_str(t: *mut sys::ASN1_GENERALIZEDTIME) -> Option<String> {
    if t.is_null() {
        return None;
    }
    // ASN1_GENERALIZEDTIME is typedef'd to asn1_string_st, same as ASN1_TIME.
    // ASN1_TIME_print handles both UTCTime and GeneralizedTime.
    let Ok(mut bio) = MemBio::new() else {
        // BIO allocation failed; clear the error queue so callers do not
        // see a stale allocation error as if it came from their own call.
        unsafe { sys::ERR_clear_error() };
        return None;
    };
    let rc = unsafe { sys::ASN1_TIME_print(bio.as_ptr(), t.cast::<sys::ASN1_TIME>()) };
    if rc != 1 {
        unsafe { sys::ERR_clear_error() };
        return None;
    }
    String::from_utf8(bio.into_vec()).ok()
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pkey::{KeygenCtx, Pkey, Private, Public};
    use crate::x509::{X509Builder, X509NameOwned};

    /// Build a minimal CA + end-entity certificate pair for testing.
    fn make_ca_and_ee() -> (
        crate::x509::X509,
        Pkey<Private>,
        crate::x509::X509,
        Pkey<Private>,
    ) {
        // CA key + cert (self-signed)
        let mut ca_kgen = KeygenCtx::new(c"ED25519").unwrap();
        let ca_priv = ca_kgen.generate().unwrap();
        let ca_pub = Pkey::<Public>::from(ca_priv.clone());

        let mut ca_name = X509NameOwned::new().unwrap();
        ca_name.add_entry_by_txt(c"CN", b"OCSP Test CA").unwrap();

        let ca_cert = X509Builder::new()
            .unwrap()
            .set_version(2)
            .unwrap()
            .set_serial_number(1)
            .unwrap()
            .set_not_before_offset(0)
            .unwrap()
            .set_not_after_offset(365 * 86400)
            .unwrap()
            .set_subject_name(&ca_name)
            .unwrap()
            .set_issuer_name(&ca_name)
            .unwrap()
            .set_public_key(&ca_pub)
            .unwrap()
            .sign(&ca_priv, None)
            .unwrap()
            .build();

        // EE key + cert (signed by CA)
        let mut ee_kgen = KeygenCtx::new(c"ED25519").unwrap();
        let ee_priv = ee_kgen.generate().unwrap();
        let ee_pub = Pkey::<Public>::from(ee_priv.clone());

        let mut ee_name = X509NameOwned::new().unwrap();
        ee_name.add_entry_by_txt(c"CN", b"OCSP Test EE").unwrap();

        let ee_cert = X509Builder::new()
            .unwrap()
            .set_version(2)
            .unwrap()
            .set_serial_number(2)
            .unwrap()
            .set_not_before_offset(0)
            .unwrap()
            .set_not_after_offset(365 * 86400)
            .unwrap()
            .set_subject_name(&ee_name)
            .unwrap()
            .set_issuer_name(&ca_name)
            .unwrap()
            .set_public_key(&ee_pub)
            .unwrap()
            .sign(&ca_priv, None)
            .unwrap()
            .build();

        (ca_cert, ca_priv, ee_cert, ee_priv)
    }

    // ── OcspCertId tests ──────────────────────────────────────────────────────

    #[test]
    fn cert_id_from_cert() {
        let (ca_cert, _, ee_cert, _) = make_ca_and_ee();
        // SHA-1 is the OCSP default; pass None for the digest.
        let id = OcspCertId::from_cert(None, &ee_cert, &ca_cert).unwrap();
        // Clone must not crash.
        let _id2 = id.clone();
    }

    // ── OcspRequest tests ─────────────────────────────────────────────────────

    #[test]
    fn ocsp_request_new_and_to_der() {
        let req = OcspRequest::new().unwrap();
        let der = req.to_der().unwrap();
        assert!(!der.is_empty());
    }

    #[test]
    fn ocsp_request_with_cert_id() {
        let (ca_cert, _, ee_cert, _) = make_ca_and_ee();
        let id = OcspCertId::from_cert(None, &ee_cert, &ca_cert).unwrap();

        let mut req = OcspRequest::new().unwrap();
        req.add_cert_id(id).unwrap();
        let der = req.to_der().unwrap();
        assert!(!der.is_empty());
        // DER with a cert ID is larger than an empty request.
        let empty_der = OcspRequest::new().unwrap().to_der().unwrap();
        assert!(der.len() > empty_der.len());
    }

    #[test]
    fn ocsp_request_der_roundtrip() {
        let req = OcspRequest::new().unwrap();
        let der = req.to_der().unwrap();
        let req2 = OcspRequest::from_der(&der).unwrap();
        assert_eq!(req2.to_der().unwrap(), der);
    }

    // ── OcspResponse tests ────────────────────────────────────────────────────

    #[test]
    fn ocsp_response_status_decode() {
        let der = OcspResponse::new_successful_der();
        let resp = OcspResponse::from_der(&der).unwrap();
        assert_eq!(resp.status(), OcspResponseStatus::Successful);
    }

    #[test]
    fn ocsp_response_der_roundtrip() {
        let der = OcspResponse::new_successful_der();
        let resp = OcspResponse::from_der(&der).unwrap();
        assert_eq!(resp.to_der().unwrap(), der);
    }

    #[test]
    fn ocsp_response_basic_fails_without_body() {
        // A response with only a status code and no responseBytes has no basic resp.
        let der = OcspResponse::new_successful_der();
        let resp = OcspResponse::from_der(&der).unwrap();
        // basic() should return Err because there is no responseBytes.
        assert!(resp.basic().is_err());
    }

    // ── OcspBasicResp / find_status tests ────────────────────────────────────
    //
    // Building a real OCSP_BASICRESP from scratch requires the full OCSP
    // responder stack (OCSP_basic_sign, OCSP_basic_add1_status) which is
    // outside the scope of unit tests. Instead we verify that find_status
    // returns None when the cert is not in the response (requires a real
    // OCSP response DER), and test the X509Store/X509StoreCtx path via
    // the integration-level store tests in x509.rs.
    //
    // The important invariants (OcspCertId::from_cert, add_cert_id, DER
    // round-trip) are covered by the tests above.
    //
    // If a real OCSP response is available (e.g. from a test OCSP responder),
    // use OcspResponse::from_der + basic() + find_status() to validate the
    // full stack.
}