cdx-core 0.7.1

Core library for reading, writing, and validating Codex Document Format (.cdx) files
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
//! Certificate revocation checking via OCSP and CRL.
//!
//! This module provides online revocation checking for X.509 certificates
//! using OCSP (Online Certificate Status Protocol) and CRL (Certificate
//! Revocation Lists).
//!
//! # Feature Flag
//!
//! This module requires the `ocsp` feature:
//!
//! ```toml
//! [dependencies]
//! cdx-core = { version = "0.1", features = ["ocsp"] }
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use cdx_core::security::revocation::{RevocationChecker, RevocationStatus};
//!
//! let checker = RevocationChecker::new();
//! let status = checker.check_ocsp(certificate_der, issuer_der).await?;
//!
//! match status {
//!     RevocationStatus::Good => println!("Certificate is valid"),
//!     RevocationStatus::Revoked { reason, time } => {
//!         println!("Certificate revoked: {:?}", reason);
//!     }
//!     RevocationStatus::Unknown => println!("Status unknown"),
//! }
//! ```

use std::fmt;
use std::time::Duration;

use serde::{Deserialize, Serialize};

#[cfg(feature = "ocsp")]
use crate::error::{invalid_certificate, network_error};

/// Revocation status of a certificate.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "camelCase")]
pub enum RevocationStatus {
    /// Certificate is not revoked.
    Good,

    /// Certificate has been revoked.
    Revoked {
        /// Reason for revocation.
        #[serde(skip_serializing_if = "Option::is_none")]
        reason: Option<RevocationReason>,
        /// Time of revocation (ISO 8601).
        #[serde(skip_serializing_if = "Option::is_none")]
        revocation_time: Option<String>,
    },

    /// Revocation status is unknown.
    Unknown,

    /// Revocation check failed.
    Error {
        /// Error message.
        message: String,
    },
}

impl RevocationStatus {
    /// Check if the certificate is known to be good.
    #[must_use]
    pub fn is_good(&self) -> bool {
        matches!(self, Self::Good)
    }

    /// Check if the certificate is revoked.
    #[must_use]
    pub fn is_revoked(&self) -> bool {
        matches!(self, Self::Revoked { .. })
    }

    /// Check if there was an error checking revocation.
    #[must_use]
    pub fn is_error(&self) -> bool {
        matches!(self, Self::Error { .. })
    }
}

impl fmt::Display for RevocationStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Good => write!(f, "good"),
            Self::Revoked { reason, .. } => {
                if let Some(r) = reason {
                    write!(f, "revoked ({r})")
                } else {
                    write!(f, "revoked")
                }
            }
            Self::Unknown => write!(f, "unknown"),
            Self::Error { message } => write!(f, "error: {message}"),
        }
    }
}

/// Reason for certificate revocation (RFC 5280).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "camelCase")]
#[repr(u8)]
pub enum RevocationReason {
    /// Unspecified reason.
    #[strum(serialize = "unspecified")]
    Unspecified = 0,
    /// Key has been compromised.
    #[strum(serialize = "key compromise")]
    KeyCompromise = 1,
    /// CA key has been compromised.
    #[strum(serialize = "CA compromise")]
    CaCompromise = 2,
    /// Affiliation has changed.
    #[strum(serialize = "affiliation changed")]
    AffiliationChanged = 3,
    /// Certificate has been superseded.
    #[strum(serialize = "superseded")]
    Superseded = 4,
    /// Certificate is no longer needed.
    #[strum(serialize = "cessation of operation")]
    CessationOfOperation = 5,
    /// Certificate is on hold.
    #[strum(serialize = "certificate hold")]
    CertificateHold = 6,
    /// Removed from CRL (not revoked).
    #[strum(serialize = "remove from CRL")]
    RemoveFromCrl = 8,
    /// Privilege has been withdrawn.
    #[strum(serialize = "privilege withdrawn")]
    PrivilegeWithdrawn = 9,
    /// AA has been compromised.
    #[strum(serialize = "AA compromise")]
    AaCompromise = 10,
}

impl RevocationReason {
    /// Create from RFC 5280 reason code.
    #[must_use]
    pub fn from_code(code: u8) -> Option<Self> {
        match code {
            0 => Some(Self::Unspecified),
            1 => Some(Self::KeyCompromise),
            2 => Some(Self::CaCompromise),
            3 => Some(Self::AffiliationChanged),
            4 => Some(Self::Superseded),
            5 => Some(Self::CessationOfOperation),
            6 => Some(Self::CertificateHold),
            8 => Some(Self::RemoveFromCrl),
            9 => Some(Self::PrivilegeWithdrawn),
            10 => Some(Self::AaCompromise),
            _ => None,
        }
    }

    /// Get the RFC 5280 reason code.
    #[must_use]
    pub const fn code(&self) -> u8 {
        *self as u8
    }
}

/// Result of a revocation check.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RevocationResult {
    /// The revocation status.
    pub status: RevocationStatus,

    /// Method used for the check.
    pub method: RevocationMethod,

    /// URL of the responder or CRL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub responder_url: Option<String>,

    /// When this check was performed (ISO 8601).
    pub checked_at: String,

    /// When the response was produced (ISO 8601).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub produced_at: Option<String>,

    /// When this response should be considered stale (ISO 8601).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_update: Option<String>,

    /// Certificate serial number that was checked.
    pub serial_number: String,
}

impl RevocationResult {
    /// Create a new revocation result.
    #[must_use]
    pub fn new(status: RevocationStatus, method: RevocationMethod, serial_number: String) -> Self {
        Self {
            status,
            method,
            responder_url: None,
            checked_at: chrono::Utc::now().to_rfc3339(),
            produced_at: None,
            next_update: None,
            serial_number,
        }
    }

    /// Set the responder URL.
    #[must_use]
    pub fn with_responder(mut self, url: impl Into<String>) -> Self {
        self.responder_url = Some(url.into());
        self
    }

    /// Set the produced_at time.
    #[must_use]
    pub fn with_produced_at(mut self, time: impl Into<String>) -> Self {
        self.produced_at = Some(time.into());
        self
    }

    /// Set the next_update time.
    #[must_use]
    pub fn with_next_update(mut self, time: impl Into<String>) -> Self {
        self.next_update = Some(time.into());
        self
    }

    /// Check if the result indicates the certificate is valid.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.status.is_good()
    }
}

/// Method used for revocation checking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "camelCase")]
pub enum RevocationMethod {
    /// OCSP (Online Certificate Status Protocol).
    #[strum(serialize = "OCSP")]
    Ocsp,
    /// CRL (Certificate Revocation List).
    #[strum(serialize = "CRL")]
    Crl,
    /// OCSP stapled in TLS handshake.
    #[strum(serialize = "OCSP Stapling")]
    OcspStapling,
}

/// Configuration for revocation checking.
#[derive(Debug, Clone)]
pub struct RevocationConfig {
    /// Timeout for network requests.
    pub timeout: Duration,

    /// Whether to prefer OCSP over CRL.
    pub prefer_ocsp: bool,

    /// Whether to use OCSP stapling when available.
    pub use_stapling: bool,

    /// Whether to require a valid revocation response.
    /// If false, unknown status is treated as valid.
    pub strict_mode: bool,

    /// Maximum age of cached CRL responses in seconds.
    pub max_crl_age: u64,

    /// Custom OCSP responder URL (overrides AIA).
    pub ocsp_responder: Option<String>,
}

impl Default for RevocationConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(10),
            prefer_ocsp: true,
            use_stapling: true,
            strict_mode: false,
            max_crl_age: 86400, // 24 hours
            ocsp_responder: None,
        }
    }
}

impl RevocationConfig {
    /// Create a new configuration with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the network timeout.
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set whether to prefer OCSP over CRL.
    #[must_use]
    pub fn with_prefer_ocsp(mut self, prefer: bool) -> Self {
        self.prefer_ocsp = prefer;
        self
    }

    /// Set strict mode (require valid revocation response).
    #[must_use]
    pub fn with_strict_mode(mut self, strict: bool) -> Self {
        self.strict_mode = strict;
        self
    }

    /// Set a custom OCSP responder URL.
    #[must_use]
    pub fn with_ocsp_responder(mut self, url: impl Into<String>) -> Self {
        self.ocsp_responder = Some(url.into());
        self
    }
}

/// Certificate revocation checker.
///
/// This provides methods for checking certificate revocation status
/// using OCSP and CRL protocols.
pub struct RevocationChecker {
    config: RevocationConfig,
    #[cfg(feature = "ocsp")]
    client: reqwest::Client,
}

impl RevocationChecker {
    /// Create a new revocation checker with default configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be initialized.
    #[cfg(feature = "ocsp")]
    pub fn new() -> Result<Self, crate::Error> {
        Self::with_config(RevocationConfig::default())
    }

    /// Create a new revocation checker with custom configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be initialized.
    #[cfg(feature = "ocsp")]
    pub fn with_config(config: RevocationConfig) -> Result<Self, crate::Error> {
        let client = reqwest::Client::builder()
            .timeout(config.timeout)
            .build()
            .map_err(|e| network_error(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self { config, client })
    }

    /// Check certificate revocation status via OCSP.
    ///
    /// # Arguments
    ///
    /// * `cert_der` - DER-encoded certificate to check
    /// * `issuer_der` - DER-encoded issuer certificate
    ///
    /// # Returns
    ///
    /// The revocation status of the certificate.
    ///
    /// # Errors
    ///
    /// Returns an error if the OCSP check fails.
    #[cfg(feature = "ocsp")]
    pub async fn check_ocsp(
        &self,
        cert_der: &[u8],
        issuer_der: &[u8],
    ) -> Result<RevocationResult, crate::Error> {
        use der::Decode;
        use x509_cert::Certificate;

        // Parse the certificates
        let cert = Certificate::from_der(cert_der)
            .map_err(|e| invalid_certificate(format!("Failed to parse certificate: {e}")))?;

        let issuer = Certificate::from_der(issuer_der)
            .map_err(|e| invalid_certificate(format!("Failed to parse issuer certificate: {e}")))?;

        // Get serial number as hex string
        let serial_bytes = cert.tbs_certificate().serial_number().as_bytes();
        let serial = bytes_to_hex(serial_bytes);

        // Get OCSP responder URL
        let responder_url = self
            .config
            .ocsp_responder
            .clone()
            .or_else(|| extract_ocsp_url(&cert))
            .ok_or_else(|| crate::Error::InvalidCertificate {
                reason: "No OCSP responder URL found in certificate".to_string(),
            })?;

        // Build OCSP request
        let request_body = build_ocsp_request(&cert, &issuer)?;

        // Send OCSP request
        let response = self
            .client
            .post(&responder_url)
            .header("Content-Type", "application/ocsp-request")
            .body(request_body)
            .send()
            .await
            .map_err(|e| network_error(format!("OCSP request failed: {e}")))?;

        if !response.status().is_success() {
            return Ok(RevocationResult::new(
                RevocationStatus::Error {
                    message: format!("OCSP responder returned status {}", response.status()),
                },
                RevocationMethod::Ocsp,
                serial,
            )
            .with_responder(&responder_url));
        }

        let response_body = response
            .bytes()
            .await
            .map_err(|e| network_error(format!("Failed to read OCSP response: {e}")))?;

        // Parse OCSP response
        let status = parse_ocsp_response(&response_body);

        Ok(
            RevocationResult::new(status, RevocationMethod::Ocsp, serial)
                .with_responder(&responder_url),
        )
    }

    /// Check certificate revocation status via CRL.
    ///
    /// # Arguments
    ///
    /// * `cert_der` - DER-encoded certificate to check
    ///
    /// # Returns
    ///
    /// The revocation status of the certificate.
    ///
    /// # Errors
    ///
    /// Returns an error if the CRL check fails.
    #[cfg(feature = "ocsp")]
    pub async fn check_crl(&self, cert_der: &[u8]) -> Result<RevocationResult, crate::Error> {
        use der::Decode;
        use x509_cert::Certificate;

        // Parse the certificate
        let cert = Certificate::from_der(cert_der)
            .map_err(|e| invalid_certificate(format!("Failed to parse certificate: {e}")))?;

        // Get serial number as hex string
        let serial_bytes = cert.tbs_certificate().serial_number().as_bytes();
        let serial = bytes_to_hex(serial_bytes);

        // Get CRL distribution point
        let crl_url = extract_crl_url(&cert)
            .ok_or_else(|| invalid_certificate("No CRL distribution point found in certificate"))?;

        // Fetch CRL
        let response = self
            .client
            .get(&crl_url)
            .send()
            .await
            .map_err(|e| network_error(format!("CRL fetch failed: {e}")))?;

        if !response.status().is_success() {
            return Ok(RevocationResult::new(
                RevocationStatus::Error {
                    message: format!("CRL server returned status {}", response.status()),
                },
                RevocationMethod::Crl,
                serial,
            )
            .with_responder(&crl_url));
        }

        let crl_data = response
            .bytes()
            .await
            .map_err(|e| network_error(format!("Failed to read CRL: {e}")))?;

        // Parse and check CRL
        let status = check_crl_for_serial(&crl_data, cert.tbs_certificate().serial_number())?;

        Ok(RevocationResult::new(status, RevocationMethod::Crl, serial).with_responder(&crl_url))
    }

    /// Check certificate revocation status using the preferred method.
    ///
    /// Tries OCSP first if configured, falls back to CRL.
    ///
    /// # Arguments
    ///
    /// * `cert_der` - DER-encoded certificate to check
    /// * `issuer_der` - DER-encoded issuer certificate (for OCSP)
    ///
    /// # Returns
    ///
    /// The revocation status of the certificate.
    ///
    /// # Errors
    ///
    /// Returns an error if all revocation checks fail.
    #[cfg(feature = "ocsp")]
    pub async fn check(
        &self,
        cert_der: &[u8],
        issuer_der: Option<&[u8]>,
    ) -> Result<RevocationResult, crate::Error> {
        if self.config.prefer_ocsp {
            // Try OCSP first
            let ocsp_err = if let Some(issuer) = issuer_der {
                match self.check_ocsp(cert_der, issuer).await {
                    Ok(result) if !result.status.is_error() => return Ok(result),
                    Ok(result) => Some(format!("OCSP returned error: {}", result.status)),
                    Err(e) => Some(format!("OCSP check failed: {e}")),
                }
            } else {
                None
            };

            // Fall back to CRL
            match self.check_crl(cert_der).await {
                Ok(result) if !result.status.is_error() => Ok(result),
                Ok(result) => Err(crate::Error::InvalidCertificate {
                    reason: format!(
                        "CRL returned error: {}{}",
                        result.status,
                        ocsp_err
                            .as_ref()
                            .map_or(String::new(), |e| format!("; prior {e}"))
                    ),
                }),
                Err(crl_err) => Err(crate::Error::InvalidCertificate {
                    reason: format!(
                        "CRL check failed: {crl_err}{}",
                        ocsp_err
                            .as_ref()
                            .map_or(String::new(), |e| format!("; prior {e}"))
                    ),
                }),
            }
        } else {
            // Try CRL first
            let crl_err = match self.check_crl(cert_der).await {
                Ok(result) if !result.status.is_error() => return Ok(result),
                Ok(result) => Some(format!("CRL returned error: {}", result.status)),
                Err(e) => Some(format!("CRL check failed: {e}")),
            };

            // Fall back to OCSP
            if let Some(issuer) = issuer_der {
                match self.check_ocsp(cert_der, issuer).await {
                    Ok(result) if !result.status.is_error() => Ok(result),
                    Ok(result) => Err(crate::Error::InvalidCertificate {
                        reason: format!(
                            "OCSP returned error: {}{}",
                            result.status,
                            crl_err
                                .as_ref()
                                .map_or(String::new(), |e| format!("; prior {e}"))
                        ),
                    }),
                    Err(ocsp_err) => Err(crate::Error::InvalidCertificate {
                        reason: format!(
                            "OCSP check failed: {ocsp_err}{}",
                            crl_err
                                .as_ref()
                                .map_or(String::new(), |e| format!("; prior {e}"))
                        ),
                    }),
                }
            } else {
                Err(crate::Error::InvalidCertificate {
                    reason: format!(
                        "No issuer provided for OCSP fallback{}",
                        crl_err
                            .as_ref()
                            .map_or(String::new(), |e| format!("; prior {e}"))
                    ),
                })
            }
        }
    }

    /// Get the current configuration.
    #[must_use]
    pub fn config(&self) -> &RevocationConfig {
        &self.config
    }
}

impl std::fmt::Debug for RevocationChecker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RevocationChecker")
            .field("config", &self.config)
            .finish_non_exhaustive()
    }
}

// Helper functions for OCSP

/// Convert bytes to uppercase hex string.
#[cfg(feature = "ocsp")]
fn bytes_to_hex(bytes: &[u8]) -> String {
    use std::fmt::Write;
    bytes
        .iter()
        .fold(String::with_capacity(bytes.len() * 2), |mut acc, b| {
            let _ = write!(acc, "{b:02X}");
            acc
        })
}

#[cfg(feature = "ocsp")]
fn extract_ocsp_url(cert: &x509_cert::Certificate) -> Option<String> {
    use x509_cert::ext::pkix::AuthorityInfoAccessSyntax;

    let extensions = cert.tbs_certificate().extensions()?;

    for ext in extensions {
        // OID for Authority Information Access: 1.3.6.1.5.5.7.1.1
        if ext.extn_id.to_string() == "1.3.6.1.5.5.7.1.1" {
            if let Ok(aia) =
                <AuthorityInfoAccessSyntax as der::Decode>::from_der(ext.extn_value.as_bytes())
            {
                for access_desc in &aia.0 {
                    // OID for OCSP: 1.3.6.1.5.5.7.48.1
                    if access_desc.access_method.to_string() == "1.3.6.1.5.5.7.48.1" {
                        if let x509_cert::ext::pkix::name::GeneralName::UniformResourceIdentifier(
                            uri,
                        ) = &access_desc.access_location
                        {
                            return Some(uri.to_string());
                        }
                    }
                }
            }
        }
    }

    None
}

#[cfg(feature = "ocsp")]
fn extract_crl_url(cert: &x509_cert::Certificate) -> Option<String> {
    let extensions = cert.tbs_certificate().extensions()?;

    for ext in extensions {
        // OID for CRL Distribution Points: 2.5.29.31
        if ext.extn_id.to_string() == "2.5.29.31" {
            // Parse the CRL distribution points
            // This is a simplified extraction - in practice you'd fully parse the ASN.1
            let bytes = ext.extn_value.as_bytes();
            // Look for http:// or https:// in the extension value
            if let Ok(s) = std::str::from_utf8(bytes) {
                if let Some(start) = s.find("http://").or_else(|| s.find("https://")) {
                    let end = s[start..]
                        .find(|c: char| c.is_control() || c == '\0')
                        .map_or(s.len(), |e| start + e);
                    return Some(s[start..end].to_string());
                }
            }
        }
    }

    None
}

#[cfg(feature = "ocsp")]
fn build_ocsp_request(
    _cert: &x509_cert::Certificate,
    _issuer: &x509_cert::Certificate,
) -> Result<Vec<u8>, crate::Error> {
    // Build a minimal OCSP request
    // In a full implementation, this would:
    // 1. Hash the issuer's name and key
    // 2. Include the certificate serial number
    // 3. Optionally add a nonce for replay protection

    // For now, return a placeholder that indicates this needs full implementation
    // with a proper ASN.1 OCSP request builder
    Err(crate::Error::NotImplemented {
        feature: "Full OCSP request building requires ocsp-rs or similar crate".to_string(),
    })
}

#[cfg(feature = "ocsp")]
fn parse_ocsp_response(_response: &[u8]) -> RevocationStatus {
    // OCSP response parsing is not yet implemented.
    // A full implementation would:
    // 1. Check the response status (successful, malformed, etc.)
    // 2. Verify the response signature
    // 3. Extract the certificate status
    //
    // Return Error rather than Unknown so callers know the check was not performed,
    // rather than incorrectly treating an unparsed response as indeterminate.
    RevocationStatus::Error {
        message: "OCSP response parsing not yet implemented".to_string(),
    }
}

#[cfg(feature = "ocsp")]
fn check_crl_for_serial(
    crl_data: &[u8],
    serial: &x509_cert::serial_number::SerialNumber,
) -> Result<RevocationStatus, crate::Error> {
    use der::Decode;
    use x509_cert::crl::CertificateList;

    // Parse the CRL
    let crl = CertificateList::from_der(crl_data)
        .map_err(|e| invalid_certificate(format!("Failed to parse CRL: {e}")))?;

    // Check if the serial number is in the revoked list
    if let Some(revoked_certs) = &crl.tbs_cert_list.revoked_certificates {
        for revoked in revoked_certs {
            if &revoked.serial_number == serial {
                // Found in CRL - certificate is revoked
                let reason = revoked.crl_entry_extensions.as_ref().and_then(|exts| {
                    exts.iter().find_map(|ext| {
                        // OID for CRL Reason: 2.5.29.21
                        if ext.extn_id.to_string() == "2.5.29.21" {
                            let bytes = ext.extn_value.as_bytes();
                            if bytes.len() >= 3 {
                                RevocationReason::from_code(bytes[2])
                            } else {
                                None
                            }
                        } else {
                            None
                        }
                    })
                });

                return Ok(RevocationStatus::Revoked {
                    reason,
                    revocation_time: Some(revoked.revocation_date.to_string()),
                });
            }
        }
    }

    // Not found in CRL - certificate is good
    Ok(RevocationStatus::Good)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_revocation_status_display() {
        assert_eq!(RevocationStatus::Good.to_string(), "good");
        assert_eq!(RevocationStatus::Unknown.to_string(), "unknown");
        assert_eq!(
            RevocationStatus::Revoked {
                reason: Some(RevocationReason::KeyCompromise),
                revocation_time: None,
            }
            .to_string(),
            "revoked (key compromise)"
        );
    }

    #[test]
    fn test_revocation_status_checks() {
        assert!(RevocationStatus::Good.is_good());
        assert!(!RevocationStatus::Good.is_revoked());

        let revoked = RevocationStatus::Revoked {
            reason: None,
            revocation_time: None,
        };
        assert!(!revoked.is_good());
        assert!(revoked.is_revoked());

        let error = RevocationStatus::Error {
            message: "test".to_string(),
        };
        assert!(error.is_error());
    }

    #[test]
    fn test_revocation_reason_from_code() {
        assert_eq!(
            RevocationReason::from_code(0),
            Some(RevocationReason::Unspecified)
        );
        assert_eq!(
            RevocationReason::from_code(1),
            Some(RevocationReason::KeyCompromise)
        );
        assert_eq!(
            RevocationReason::from_code(5),
            Some(RevocationReason::CessationOfOperation)
        );
        assert_eq!(RevocationReason::from_code(7), None); // No reason code 7
        assert_eq!(RevocationReason::from_code(255), None);
    }

    #[test]
    fn test_revocation_reason_code() {
        assert_eq!(RevocationReason::Unspecified.code(), 0);
        assert_eq!(RevocationReason::KeyCompromise.code(), 1);
        assert_eq!(RevocationReason::AaCompromise.code(), 10);
    }

    #[test]
    fn test_revocation_reason_display() {
        assert_eq!(
            RevocationReason::KeyCompromise.to_string(),
            "key compromise"
        );
        assert_eq!(
            RevocationReason::CessationOfOperation.to_string(),
            "cessation of operation"
        );
    }

    #[test]
    fn test_revocation_config_default() {
        let config = RevocationConfig::default();
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert!(config.prefer_ocsp);
        assert!(config.use_stapling);
        assert!(!config.strict_mode);
        assert!(config.ocsp_responder.is_none());
    }

    #[test]
    fn test_revocation_config_builder() {
        let config = RevocationConfig::new()
            .with_timeout(Duration::from_secs(30))
            .with_prefer_ocsp(false)
            .with_strict_mode(true)
            .with_ocsp_responder("http://ocsp.example.com");

        assert_eq!(config.timeout, Duration::from_secs(30));
        assert!(!config.prefer_ocsp);
        assert!(config.strict_mode);
        assert_eq!(
            config.ocsp_responder,
            Some("http://ocsp.example.com".to_string())
        );
    }

    #[test]
    fn test_revocation_result_new() {
        let result = RevocationResult::new(
            RevocationStatus::Good,
            RevocationMethod::Ocsp,
            "1234ABCD".to_string(),
        );

        assert!(result.is_valid());
        assert_eq!(result.method, RevocationMethod::Ocsp);
        assert_eq!(result.serial_number, "1234ABCD");
        assert!(result.responder_url.is_none());
    }

    #[test]
    fn test_revocation_result_builder() {
        let result = RevocationResult::new(
            RevocationStatus::Good,
            RevocationMethod::Ocsp,
            "1234".to_string(),
        )
        .with_responder("http://ocsp.example.com")
        .with_produced_at("2024-01-01T00:00:00Z")
        .with_next_update("2024-01-02T00:00:00Z");

        assert_eq!(
            result.responder_url,
            Some("http://ocsp.example.com".to_string())
        );
        assert_eq!(result.produced_at, Some("2024-01-01T00:00:00Z".to_string()));
        assert_eq!(result.next_update, Some("2024-01-02T00:00:00Z".to_string()));
    }

    #[test]
    fn test_revocation_method_display() {
        assert_eq!(RevocationMethod::Ocsp.to_string(), "OCSP");
        assert_eq!(RevocationMethod::Crl.to_string(), "CRL");
        assert_eq!(RevocationMethod::OcspStapling.to_string(), "OCSP Stapling");
    }

    #[test]
    fn test_revocation_status_serialization() {
        let good = RevocationStatus::Good;
        let json = serde_json::to_string(&good).unwrap();
        assert!(json.contains("\"status\":\"good\""));

        let revoked = RevocationStatus::Revoked {
            reason: Some(RevocationReason::KeyCompromise),
            revocation_time: Some("2024-01-01T00:00:00Z".to_string()),
        };
        let json = serde_json::to_string(&revoked).unwrap();
        assert!(json.contains("\"status\":\"revoked\""));
        assert!(json.contains("\"reason\":\"keyCompromise\""));
    }
}