tsp-ltv 0.1.0

Shared timestamping (RFC 3161) and long-term validation (OCSP, CRL, trust stores) infrastructure for AdES signatures
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
//! CRL fetching, caching, and revocation checking.
//!
//! Fetches Certificate Revocation Lists from distribution points found in
//! X.509 certificates, with both in-memory and optional disk caching.
//! Also provides CRL content parsing and signature verification for
//! offline revocation status checking.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use reqwest::Client;
use x509_cert::Certificate;

use crate::der_utils::{
    find_tagged_value, integer_bodies_equal, parse_integer_body, parse_tlv,
    parse_tlv_with_rest, parse_x509_time,
};
use crate::error::LtvError;
use crate::ltv::status::{RevocationReason, RevocationSource, ValidationStatus};

/// A cached CRL entry.
#[derive(Debug, Clone)]
struct CrlCacheEntry {
    /// Raw DER-encoded CRL bytes.
    der: Vec<u8>,
    /// When this entry was fetched.
    fetched_at: Instant,
}

/// CRL client with in-memory caching.
///
/// Fetches CRLs from distribution points and caches them to avoid
/// redundant network requests.
#[derive(Debug, Clone)]
pub struct CrlClient {
    http_client: Client,
    timeout: Duration,
    /// In-memory cache: URL -> CRL entry
    cache: Arc<Mutex<HashMap<String, CrlCacheEntry>>>,
    /// How long cached CRLs remain valid.
    grace_period: Duration,
}

impl CrlClient {
    /// Create a new CRL client with default settings.
    ///
    /// Default grace period: 1 hour.
    pub fn new() -> Self {
        Self {
            http_client: Client::new(),
            timeout: Duration::from_secs(30),
            cache: Arc::new(Mutex::new(HashMap::new())),
            grace_period: Duration::from_secs(3600),
        }
    }

    /// Set the HTTP client.
    pub fn http_client(mut self, client: Client) -> Self {
        self.http_client = client;
        self
    }

    /// Set the request timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the cache grace period.
    pub fn grace_period(mut self, grace: Duration) -> Self {
        self.grace_period = grace;
        self
    }

    /// Extract CRL distribution point URLs from a certificate.
    pub fn extract_crl_urls(cert: &Certificate) -> Vec<String> {
        let mut urls = Vec::new();

        // CRL Distribution Points extension OID: 2.5.29.31
        let crl_dp_oid = const_oid::ObjectIdentifier::new_unwrap("2.5.29.31");

        if let Some(extensions) = &cert.tbs_certificate.extensions {
            for ext in extensions.iter() {
                if ext.extn_id == crl_dp_oid {
                    // Parse the CRL Distribution Points extension value
                    // It's a SEQUENCE OF DistributionPoint
                    if let Ok(urls_from_ext) = parse_crl_dp_extension(ext.extn_value.as_bytes()) {
                        urls.extend(urls_from_ext);
                    }
                }
            }
        }

        urls
    }

    /// Fetch a CRL from the given URL, using cache if available.
    pub async fn fetch_crl(&self, url: &str) -> Result<Vec<u8>, LtvError> {
        // Check cache first
        {
            let cache = self.cache.lock().map_err(|e| {
                LtvError::Crl(format!("cache lock poisoned: {e}"))
            })?;
            if let Some(entry) = cache.get(url) {
                if entry.fetched_at.elapsed() < self.grace_period {
                    log::debug!("CRL cache hit for {url}");
                    return Ok(entry.der.clone());
                }
            }
        }

        log::debug!("Fetching CRL from {url}");

        let response = self
            .http_client
            .get(url)
            .timeout(self.timeout)
            .send()
            .await
            .map_err(|e| LtvError::Crl(format!("CRL fetch from {url} failed: {e}")))?;

        if !response.status().is_success() {
            return Err(LtvError::Crl(format!(
                "CRL fetch from {url} returned HTTP {}",
                response.status()
            )));
        }

        let crl_bytes = response
            .bytes()
            .await
            .map_err(|e| LtvError::Crl(format!("failed to read CRL response body: {e}")))?
            .to_vec();

        // Validate that it looks like a DER-encoded CRL (starts with SEQUENCE tag)
        if crl_bytes.is_empty() || crl_bytes[0] != 0x30 {
            return Err(LtvError::Crl(format!(
                "CRL from {url} does not appear to be DER-encoded"
            )));
        }

        log::debug!("CRL from {url}: {} bytes", crl_bytes.len());

        // Update cache
        {
            let mut cache = self.cache.lock().map_err(|e| {
                LtvError::Crl(format!("cache lock poisoned: {e}"))
            })?;
            cache.insert(
                url.to_string(),
                CrlCacheEntry {
                    der: crl_bytes.clone(),
                    fetched_at: Instant::now(),
                },
            );
        }

        Ok(crl_bytes)
    }

    /// Fetch all CRLs for a certificate (from all distribution points).
    pub async fn fetch_crls_for_cert(
        &self,
        cert: &Certificate,
    ) -> Result<Vec<Vec<u8>>, LtvError> {
        let urls = Self::extract_crl_urls(cert);
        let mut crls = Vec::new();

        for url in &urls {
            match self.fetch_crl(url).await {
                Ok(crl) => {
                    crls.push(crl);
                    // One CRL is sufficient for most validation scenarios
                    break;
                }
                Err(e) => {
                    log::warn!("Failed to fetch CRL from {url}: {e}");
                    // Try next URL
                }
            }
        }

        Ok(crls)
    }

    /// Clear the in-memory cache.
    pub fn clear_cache(&self) {
        if let Ok(mut cache) = self.cache.lock() {
            cache.clear();
        }
    }
}

impl Default for CrlClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Parse the CRL Distribution Points extension value.
///
/// ```text
/// CRLDistributionPoints ::= SEQUENCE SIZE (1..MAX) OF DistributionPoint
/// DistributionPoint ::= SEQUENCE {
///     distributionPoint  [0] DistributionPointName OPTIONAL,
///     reasons            [1] ReasonFlags OPTIONAL,
///     cRLIssuer          [2] GeneralNames OPTIONAL
/// }
/// DistributionPointName ::= CHOICE {
///     fullName           [0] GeneralNames,
///     nameRelativeToCRLIssuer [1] RelativeDistinguishedName
/// }
/// GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
/// GeneralName ::= CHOICE {
///     uniformResourceIdentifier [6] IA5String,
///     ...
/// }
/// ```
fn parse_crl_dp_extension(der_bytes: &[u8]) -> Result<Vec<String>, String> {
    let mut urls = Vec::new();

    // SEQUENCE OF DistributionPoint
    let (tag, body) = parse_tlv(der_bytes)?;
    if tag != 0x30 {
        return Err(format!("expected SEQUENCE, got 0x{tag:02x}"));
    }

    let mut pos = &body[..];
    while !pos.is_empty() {
        let (dp_tag, dp_body, rest) = parse_tlv_with_rest(pos)?;
        if dp_tag == 0x30 {
            // DistributionPoint SEQUENCE
            // Look for distributionPoint [0]
            if !dp_body.is_empty() {
                if let Ok((inner_tag, inner_body, _)) = parse_tlv_with_rest(&dp_body) {
                    if inner_tag == 0xA0 {
                        // DistributionPointName — look for fullName [0]
                        if let Ok((fn_tag, fn_body, _)) = parse_tlv_with_rest(&inner_body) {
                            if fn_tag == 0xA0 {
                                // GeneralNames — look for URI [6]
                                let mut gn_pos = &fn_body[..];
                                while !gn_pos.is_empty() {
                                    if let Ok((gn_tag, gn_body, gn_rest)) =
                                        parse_tlv_with_rest(gn_pos)
                                    {
                                        if gn_tag == 0x86 {
                                            // uniformResourceIdentifier [6] IMPLICIT IA5String
                                            if let Ok(uri) = std::str::from_utf8(&gn_body) {
                                                urls.push(uri.to_string());
                                            }
                                        }
                                        gn_pos = gn_rest;
                                    } else {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        pos = rest;
    }

    Ok(urls)
}

// ── CRL content parsing and revocation checking ─────────────────────────────

/// A parsed revoked certificate entry from a CRL.
#[derive(Debug, Clone)]
pub struct RevokedEntry {
    /// Serial number of the revoked certificate (leading-zero-stripped).
    pub serial_number: Vec<u8>,
    /// When the certificate was revoked.
    pub revocation_time: chrono::DateTime<chrono::Utc>,
    /// Reason for revocation, if present in CRL entry extensions.
    pub reason: RevocationReason,
}

/// Parsed contents of a CRL's TBSCertList.
#[derive(Debug)]
pub struct ParsedCrl {
    /// Raw TBS bytes (for signature verification).
    pub tbs_bytes: Vec<u8>,
    /// Signature algorithm OID.
    pub signature_algorithm_oid: const_oid::ObjectIdentifier,
    /// Raw signature bytes (BIT STRING contents, without the unused-bits byte).
    pub signature_bytes: Vec<u8>,
    /// CRL issuer distinguished name (raw DER of the Name SEQUENCE).
    pub issuer_der: Vec<u8>,
    /// thisUpdate timestamp.
    pub this_update: chrono::DateTime<chrono::Utc>,
    /// nextUpdate timestamp, if present.
    pub next_update: Option<chrono::DateTime<chrono::Utc>>,
    /// Revoked certificate entries.
    pub revoked_entries: Vec<RevokedEntry>,
}

/// Parse a DER-encoded CRL into its structural components.
///
/// ```text
/// CertificateList ::= SEQUENCE {
///     tbsCertList          TBSCertList,
///     signatureAlgorithm   AlgorithmIdentifier,
///     signatureValue       BIT STRING
/// }
///
/// TBSCertList ::= SEQUENCE {
///     version              Version OPTIONAL (v2 = INTEGER 1),
///     signature            AlgorithmIdentifier,
///     issuer               Name,
///     thisUpdate           Time,
///     nextUpdate           Time OPTIONAL,
///     revokedCertificates  SEQUENCE OF SEQUENCE { ... } OPTIONAL,
///     crlExtensions    [0] Extensions OPTIONAL
/// }
/// ```
pub fn parse_crl(crl_der: &[u8]) -> Result<ParsedCrl, LtvError> {
    // Outer SEQUENCE: CertificateList
    let (outer_tag, outer_body) =
        parse_tlv(crl_der).map_err(|e| LtvError::Crl(format!("CRL outer SEQUENCE: {e}")))?;
    if outer_tag != 0x30 {
        return Err(LtvError::Crl(format!(
            "expected CRL SEQUENCE (0x30), got 0x{outer_tag:02x}"
        )));
    }

    // Parse the three children: tbsCertList, signatureAlgorithm, signatureValue
    let (tbs_tag, tbs_value, rest) = parse_tlv_with_rest(&outer_body)
        .map_err(|e| LtvError::Crl(format!("CRL tbsCertList: {e}")))?;
    if tbs_tag != 0x30 {
        return Err(LtvError::Crl(format!(
            "expected tbsCertList SEQUENCE, got 0x{tbs_tag:02x}"
        )));
    }
    // Reconstruct full TBS DER (tag + length + value) for signature verification
    let tbs_start = crl_der.len() - outer_body.len();
    let tbs_end = crl_der.len() - outer_body.len() + (outer_body.len() - rest.len());
    let tbs_bytes = crl_der[tbs_start..tbs_end].to_vec();

    // signatureAlgorithm SEQUENCE
    let (sig_alg_tag, sig_alg_body, rest) =
        parse_tlv_with_rest(rest).map_err(|e| LtvError::Crl(format!("CRL sigAlg: {e}")))?;
    if sig_alg_tag != 0x30 {
        return Err(LtvError::Crl(format!(
            "expected signatureAlgorithm SEQUENCE, got 0x{sig_alg_tag:02x}"
        )));
    }
    // Extract OID from the AlgorithmIdentifier SEQUENCE
    let sig_oid = parse_oid_from_algorithm_identifier(&sig_alg_body)?;

    // signatureValue BIT STRING
    let (sig_val_tag, sig_val_body, _) =
        parse_tlv_with_rest(rest).map_err(|e| LtvError::Crl(format!("CRL sigValue: {e}")))?;
    if sig_val_tag != 0x03 {
        return Err(LtvError::Crl(format!(
            "expected signatureValue BIT STRING (0x03), got 0x{sig_val_tag:02x}"
        )));
    }
    // BIT STRING: first byte is unused-bits count (should be 0)
    if sig_val_body.is_empty() {
        return Err(LtvError::Crl("empty signature BIT STRING".into()));
    }
    let signature_bytes = sig_val_body[1..].to_vec();

    // Parse TBSCertList body
    let mut tbs_pos = &tbs_value[..];

    // Optional: version [0] EXPLICIT INTEGER (v2 = 1)
    if !tbs_pos.is_empty() && tbs_pos[0] == 0x02 {
        // Check if first field is an INTEGER — could be version if small,
        // or could be the AlgorithmIdentifier. Actually version is optional
        // and v1 CRLs might omit it. The next field after optional version
        // is a SEQUENCE (AlgorithmIdentifier). Let's peek:
        // If we see INTEGER, skip it as version.
        let (_, _, r) = parse_tlv_with_rest(tbs_pos)
            .map_err(|e| LtvError::Crl(format!("CRL version: {e}")))?;
        tbs_pos = r;
    }

    // signature AlgorithmIdentifier (SEQUENCE) — skip, we got it from outer
    if !tbs_pos.is_empty() {
        let (tag, _, r) = parse_tlv_with_rest(tbs_pos)
            .map_err(|e| LtvError::Crl(format!("CRL inner sigAlg: {e}")))?;
        if tag == 0x30 {
            tbs_pos = r;
        }
    }

    // issuer Name (SEQUENCE)
    let (issuer_tag, _issuer_body, rest_after_issuer) = parse_tlv_with_rest(tbs_pos)
        .map_err(|e| LtvError::Crl(format!("CRL issuer: {e}")))?;
    if issuer_tag != 0x30 {
        return Err(LtvError::Crl(format!(
            "expected issuer SEQUENCE, got 0x{issuer_tag:02x}"
        )));
    }
    // Capture raw issuer DER (full TLV)
    let issuer_len = tbs_pos.len() - rest_after_issuer.len();
    let issuer_der = tbs_pos[..issuer_len].to_vec();
    tbs_pos = rest_after_issuer;

    // thisUpdate Time (UTCTime 0x17 or GeneralizedTime 0x18)
    let (time_tag, time_body, rest_after_this) = parse_tlv_with_rest(tbs_pos)
        .map_err(|e| LtvError::Crl(format!("CRL thisUpdate: {e}")))?;
    let this_update = parse_x509_time(time_tag, time_body)
        .map_err(|e| LtvError::Crl(format!("CRL thisUpdate parse: {e}")))?;
    tbs_pos = rest_after_this;

    // nextUpdate Time OPTIONAL
    let mut next_update = None;
    if !tbs_pos.is_empty() && (tbs_pos[0] == 0x17 || tbs_pos[0] == 0x18) {
        let (nt_tag, nt_body, r) = parse_tlv_with_rest(tbs_pos)
            .map_err(|e| LtvError::Crl(format!("CRL nextUpdate: {e}")))?;
        next_update = Some(
            parse_x509_time(nt_tag, nt_body)
                .map_err(|e| LtvError::Crl(format!("CRL nextUpdate parse: {e}")))?,
        );
        tbs_pos = r;
    }

    // revokedCertificates SEQUENCE OF OPTIONAL
    let mut revoked_entries = Vec::new();
    if !tbs_pos.is_empty() && tbs_pos[0] == 0x30 {
        let (rc_tag, rc_body, r) = parse_tlv_with_rest(tbs_pos)
            .map_err(|e| LtvError::Crl(format!("CRL revokedCertificates: {e}")))?;
        if rc_tag == 0x30 {
            parse_revoked_certificates(rc_body, &mut revoked_entries)?;
        }
        tbs_pos = r;
    }
    // Remaining: optional [0] crlExtensions — we skip for now
    let _ = tbs_pos;

    Ok(ParsedCrl {
        tbs_bytes,
        signature_algorithm_oid: sig_oid,
        signature_bytes,
        issuer_der,
        this_update,
        next_update,
        revoked_entries,
    })
}

/// Parse an OID from a DER AlgorithmIdentifier body.
fn parse_oid_from_algorithm_identifier(body: &[u8]) -> Result<const_oid::ObjectIdentifier, LtvError>
{
    let (tag, oid_bytes, _) =
        parse_tlv_with_rest(body).map_err(|e| LtvError::Crl(format!("AlgId OID: {e}")))?;
    if tag != 0x06 {
        return Err(LtvError::Crl(format!(
            "expected OID (0x06) in AlgorithmIdentifier, got 0x{tag:02x}"
        )));
    }
    const_oid::ObjectIdentifier::from_bytes(oid_bytes)
        .map_err(|e| LtvError::Crl(format!("invalid OID: {e}")))
}

/// Parse the revokedCertificates SEQUENCE body.
///
/// ```text
/// revokedCertificates ::= SEQUENCE OF SEQUENCE {
///     userCertificate    CertificateSerialNumber (INTEGER),
///     revocationDate     Time,
///     crlEntryExtensions Extensions OPTIONAL
/// }
/// ```
fn parse_revoked_certificates(
    body: &[u8],
    entries: &mut Vec<RevokedEntry>,
) -> Result<(), LtvError> {
    let mut pos = body;
    while !pos.is_empty() {
        let (entry_tag, entry_body, rest) = parse_tlv_with_rest(pos)
            .map_err(|e| LtvError::Crl(format!("revoked entry: {e}")))?;
        if entry_tag != 0x30 {
            return Err(LtvError::Crl(format!(
                "expected revoked entry SEQUENCE, got 0x{entry_tag:02x}"
            )));
        }

        // userCertificate INTEGER
        let (serial_tag, serial_body, entry_rest) = parse_tlv_with_rest(entry_body)
            .map_err(|e| LtvError::Crl(format!("revoked serial: {e}")))?;
        if serial_tag != 0x02 {
            return Err(LtvError::Crl(format!(
                "expected serial INTEGER (0x02), got 0x{serial_tag:02x}"
            )));
        }
        let serial_number = parse_integer_body(serial_body);

        // revocationDate Time
        let (time_tag, time_body, entry_rest2) = parse_tlv_with_rest(entry_rest)
            .map_err(|e| LtvError::Crl(format!("revocation date: {e}")))?;
        let revocation_time = parse_x509_time(time_tag, time_body)
            .map_err(|e| LtvError::Crl(format!("revocation date parse: {e}")))?;

        // crlEntryExtensions OPTIONAL — look for reason code
        let reason = parse_revocation_reason(entry_rest2);

        entries.push(RevokedEntry {
            serial_number,
            revocation_time,
            reason,
        });

        pos = rest;
    }
    Ok(())
}

/// Parse optional CRL entry extensions to find a reason code.
///
/// The reason code extension (OID 2.5.29.21) contains an ENUMERATED value.
fn parse_revocation_reason(extensions_area: &[u8]) -> RevocationReason {
    if extensions_area.is_empty() {
        return RevocationReason::Unspecified;
    }

    // Extensions is a SEQUENCE OF Extension
    let Ok((tag, ext_body, _)) = parse_tlv_with_rest(extensions_area) else {
        return RevocationReason::Unspecified;
    };
    if tag != 0x30 {
        return RevocationReason::Unspecified;
    }

    // CRL reason code OID: 2.5.29.21
    let reason_oid_bytes: &[u8] = &[0x55, 0x1D, 0x15]; // 2.5.29.21

    // Walk through extensions
    let mut pos = &ext_body[..];
    while !pos.is_empty() {
        let Ok((ext_tag, ext_value, rest)) = parse_tlv_with_rest(pos) else {
            break;
        };
        if ext_tag == 0x30 {
            // Extension SEQUENCE: OID + optional critical BOOLEAN + value OCTET STRING
            if let Some(oid_body) = find_tagged_value(ext_value, 0x06) {
                if oid_body == reason_oid_bytes {
                    // Found reason code extension — value is in OCTET STRING
                    if let Some(octet_body) = find_tagged_value(ext_value, 0x04) {
                        // Inside the OCTET STRING is an ENUMERATED value
                        if let Some(enum_body) = find_tagged_value(octet_body, 0x0A) {
                            if !enum_body.is_empty() {
                                return RevocationReason::from_code(enum_body[0]);
                            }
                        }
                    }
                }
            }
        }
        pos = rest;
    }

    RevocationReason::Unspecified
}

/// Verify a CRL's signature against the issuer's public key.
///
/// Extracts the issuer's SPKI, then delegates to [`crate::crypto::verify::verify_signature_by_oid`].
pub fn verify_crl_signature(
    parsed_crl: &ParsedCrl,
    issuer: &Certificate,
) -> Result<(), LtvError> {
    use der::Encode;

    let spki_der = issuer
        .tbs_certificate
        .subject_public_key_info
        .to_der()
        .map_err(|e| LtvError::Crl(format!("issuer SPKI encode failed: {e}")))?;

    crate::crypto::verify::verify_signature_by_oid(
        &parsed_crl.tbs_bytes,
        &parsed_crl.signature_bytes,
        &spki_der,
        &parsed_crl.signature_algorithm_oid,
    )
    .map_err(|e| LtvError::Crl(format!("CRL signature verification failed: {e}")))
}

/// Check whether a certificate is revoked according to a CRL.
///
/// Performs the full CRL validation pipeline:
/// 1. Parse CRL structure
/// 2. Verify CRL signature against issuer's public key
/// 3. Check CRL freshness (thisUpdate/nextUpdate vs validation_time)
/// 4. Verify CRL issuer DN matches the certificate's issuer
/// 5. Search for the certificate's serial number in revoked entries
/// 6. Time-aware: if `revocationDate > validation_time` → `Valid`
///
/// Returns a [`ValidationStatus`] indicating the result.
pub fn check_revocation(
    crl_der: &[u8],
    cert: &Certificate,
    issuer: &Certificate,
    validation_time: Option<chrono::DateTime<chrono::Utc>>,
) -> Result<ValidationStatus, LtvError> {
    let now = validation_time.unwrap_or_else(chrono::Utc::now);

    // 1. Parse CRL
    let parsed = parse_crl(crl_der)?;

    // 2. Verify CRL signature
    verify_crl_signature(&parsed, issuer)?;

    // 3. Check CRL freshness: nextUpdate should be in the future
    if let Some(next_update) = parsed.next_update {
        if now > next_update {
            log::warn!("CRL is stale: nextUpdate={next_update}, validation_time={now}");
            // Stale CRL — we still process it but log a warning.
            // The Java stack treats stale CRLs as valid for revocation
            // checking but flags it in diagnostics.
        }
    }

    // 4. Verify CRL issuer matches cert's issuer
    // We compare raw DER issuer names
    let cert_issuer_der = get_cert_issuer_der(cert)?;
    if parsed.issuer_der != cert_issuer_der {
        return Err(LtvError::Crl(
            "CRL issuer does not match certificate issuer".into(),
        ));
    }

    // 5. Get the certificate's serial number for lookup
    let cert_serial = get_cert_serial_body(cert);

    // 6. Search for serial number in revoked entries
    for entry in &parsed.revoked_entries {
        if integer_bodies_equal(&entry.serial_number, &cert_serial) {
            // Found! Time-aware check: if revocationDate > validation_time → Valid
            if entry.revocation_time > now {
                log::debug!(
                    "cert serial found in CRL but revocation_time ({}) is in the future relative to validation_time ({})",
                    entry.revocation_time, now
                );
                return Ok(ValidationStatus::Valid {
                    source: RevocationSource::Crl,
                    checked_at: now,
                });
            }

            return Ok(ValidationStatus::Revoked {
                source: RevocationSource::Crl,
                reason: entry.reason,
                revocation_time: entry.revocation_time,
            });
        }
    }

    // Serial not found in revoked list → Valid
    Ok(ValidationStatus::Valid {
        source: RevocationSource::Crl,
        checked_at: now,
    })
}

/// Extract the raw DER encoding of a certificate's issuer Name.
fn get_cert_issuer_der(cert: &Certificate) -> Result<Vec<u8>, LtvError> {
    use der::Encode;
    cert.tbs_certificate
        .issuer
        .to_der()
        .map_err(|e| LtvError::Crl(format!("cert issuer DER encode failed: {e}")))
}

/// Extract the serial number body from a certificate (stripped of leading zero padding).
fn get_cert_serial_body(cert: &Certificate) -> Vec<u8> {
    let serial = &cert.tbs_certificate.serial_number;
    let bytes = serial.as_bytes();
    parse_integer_body(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::der_utils::{encode_integer_u64, encode_sequence_from_parts, encode_sequence_raw, encode_tlv};
    use der::{Decode, Encode};

    #[test]
    fn test_crl_client_default() {
        let client = CrlClient::new();
        assert_eq!(client.grace_period, Duration::from_secs(3600));
    }

    #[test]
    fn test_crl_client_builder() {
        let client = CrlClient::new()
            .timeout(Duration::from_secs(10))
            .grace_period(Duration::from_secs(7200));
        assert_eq!(client.grace_period, Duration::from_secs(7200));
    }

    #[test]
    fn test_extract_crl_urls_no_extensions() {
        // A certificate without CRL DP extensions should return empty
        let cert_pem = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/ca_cert.pem"
        ));
        // Root CAs typically don't have CRL DPs
        let pem_data = pem_rfc7468::decode_vec(cert_pem.as_bytes());
        if let Ok((_label, der)) = pem_data {
            if let Ok(cert) = Certificate::from_der(&der) {
                let urls = CrlClient::extract_crl_urls(&cert);
                // Root CA may or may not have CRL DPs, just ensure no panic
                let _ = urls;
            }
        }
    }

    // ── CRL content parsing tests ─────────────────────────────────────

    /// Build a synthetic DER-encoded CRL signed by the test intermediate CA.
    ///
    /// This constructs a minimal CRL by hand:
    /// - TBSCertList with version, AlgId, issuer, thisUpdate, revokedCerts
    /// - Signs it with the intermediate CA key
    fn build_test_crl(
        issuer_cert: &Certificate,
        issuer_key_pem: &str,
        revoked_serials: &[(Vec<u8>, &str)], // (serial_bytes, "YYMMDDHHMMSSZ")
    ) -> Vec<u8> {
        use rsa::pkcs1v15::SigningKey;
        use rsa::signature::Signer;
        use rsa::signature::SignatureEncoding;
        use rsa::pkcs8::DecodePrivateKey;
        use sha2::Sha256;

        // Build TBSCertList body
        let mut tbs_body = Vec::new();

        // version INTEGER 1 (v2)
        tbs_body.extend_from_slice(&encode_integer_u64(1));

        // signature AlgorithmIdentifier: sha256WithRSAEncryption
        let sha256_rsa_oid: &[u8] = &[
            0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
        ];
        let alg_id = encode_sequence_from_parts(&[sha256_rsa_oid, &[0x05, 0x00]]);
        tbs_body.extend_from_slice(&alg_id);

        // issuer Name — use the issuer cert's subject DER
        let issuer_name_der = issuer_cert
            .tbs_certificate
            .subject
            .to_der()
            .unwrap();
        tbs_body.extend_from_slice(&issuer_name_der);

        // thisUpdate UTCTime
        let this_update_utc = encode_tlv(0x17, b"260101000000Z");
        tbs_body.extend_from_slice(&this_update_utc);

        // nextUpdate UTCTime
        let next_update_utc = encode_tlv(0x17, b"270101000000Z");
        tbs_body.extend_from_slice(&next_update_utc);

        // revokedCertificates SEQUENCE OF
        if !revoked_serials.is_empty() {
            let mut revoked_body = Vec::new();
            for (serial, time_str) in revoked_serials {
                // Each entry: SEQUENCE { INTEGER serial, UTCTime revocationDate }
                let serial_tlv = encode_tlv(0x02, serial);
                let time_tlv = encode_tlv(0x17, time_str.as_bytes());
                let entry = encode_sequence_from_parts(&[&serial_tlv, &time_tlv]);
                revoked_body.extend_from_slice(&entry);
            }
            let revoked_seq = encode_sequence_raw(&revoked_body);
            tbs_body.extend_from_slice(&revoked_seq);
        }

        // Wrap as TBSCertList SEQUENCE
        let tbs_der = encode_sequence_raw(&tbs_body);

        // Sign TBS with issuer's RSA key
        let key_der = pem_rfc7468::decode_vec(issuer_key_pem.as_bytes())
            .unwrap()
            .1;
        let private_key =
            rsa::RsaPrivateKey::from_pkcs8_der(&key_der).unwrap();
        let signing_key = SigningKey::<Sha256>::new(private_key);
        let signature: rsa::pkcs1v15::Signature = signing_key.sign(&tbs_der);
        let sig_bytes = signature.to_vec();

        // Build outer CertificateList SEQUENCE
        let outer_alg_id = alg_id.clone();
        // BIT STRING: 0x00 unused bits prefix + signature bytes
        let mut bit_string_value = vec![0x00];
        bit_string_value.extend_from_slice(&sig_bytes);
        let sig_bit_string = encode_tlv(0x03, &bit_string_value);

        let cert_list =
            encode_sequence_from_parts(&[&tbs_der, &outer_alg_id, &sig_bit_string]);
        cert_list
    }

    fn load_test_cert(pem_str: &str) -> Certificate {
        let (_, der) = pem_rfc7468::decode_vec(pem_str.as_bytes()).unwrap();
        Certificate::from_der(&der).unwrap()
    }

    fn intermediate_ca_cert() -> Certificate {
        let pem = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/intermediate_ca_cert.pem"
        ));
        load_test_cert(pem)
    }

    fn intermediate_ca_key_pem() -> &'static str {
        // This is generated by gen-test-fixtures.sh and is gitignored.
        // Tests that need it should check for its existence.
        // For CI, the fixture script must be run first.
        concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/intermediate_ca_key.pem"
        )
    }

    fn signer_cert() -> Certificate {
        let pem = include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/fixtures/signer_cert.pem"
        ));
        load_test_cert(pem)
    }

    #[test]
    fn test_parse_crl_empty_revoked_list() {
        // We need the intermediate CA key to sign. If it doesn't exist, skip.
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found (run gen-test-fixtures.sh)");
            return;
        };

        let issuer = intermediate_ca_cert();
        let crl_der = build_test_crl(&issuer, &key_pem, &[]);
        let parsed = parse_crl(&crl_der).unwrap();

        assert!(parsed.revoked_entries.is_empty());
        assert!(parsed.next_update.is_some());
        assert_eq!(
            parsed.this_update.to_rfc3339(),
            "2026-01-01T00:00:00+00:00"
        );
    }

    #[test]
    fn test_parse_crl_with_revoked_entries() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let revoked = vec![
            (vec![0x01], "250601120000Z"),
            (vec![0x00, 0xFF], "250701120000Z"),
        ];
        let crl_der = build_test_crl(&issuer, &key_pem, &revoked);
        let parsed = parse_crl(&crl_der).unwrap();

        assert_eq!(parsed.revoked_entries.len(), 2);
        assert_eq!(parsed.revoked_entries[0].serial_number, vec![0x01]);
        assert_eq!(parsed.revoked_entries[1].serial_number, vec![0xFF]);
    }

    #[test]
    fn test_verify_crl_signature() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let crl_der = build_test_crl(&issuer, &key_pem, &[]);
        let parsed = parse_crl(&crl_der).unwrap();

        let result = verify_crl_signature(&parsed, &issuer);
        assert!(result.is_ok(), "CRL signature should verify: {result:?}");
    }

    #[test]
    fn test_verify_crl_signature_wrong_issuer() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let crl_der = build_test_crl(&issuer, &key_pem, &[]);
        let parsed = parse_crl(&crl_der).unwrap();

        // Use a different cert (signer) as "issuer" — should fail
        let wrong_issuer = signer_cert();
        let result = verify_crl_signature(&parsed, &wrong_issuer);
        assert!(result.is_err(), "wrong issuer should fail verification");
    }

    #[test]
    fn test_check_revocation_not_revoked() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let cert = signer_cert();

        // CRL with no revoked entries
        let crl_der = build_test_crl(&issuer, &key_pem, &[]);
        let validation_time = chrono::DateTime::parse_from_rfc3339("2026-06-01T00:00:00Z")
            .unwrap()
            .with_timezone(&chrono::Utc);

        let status = check_revocation(&crl_der, &cert, &issuer, Some(validation_time)).unwrap();
        assert!(status.is_valid(), "should be valid: {status}");
    }

    #[test]
    fn test_check_revocation_cert_is_revoked() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let cert = signer_cert();

        // Get the signer cert's serial number
        let serial = get_cert_serial_body(&cert);

        // CRL with the signer's serial revoked
        let revoked = vec![(serial, "250601120000Z")];
        let crl_der = build_test_crl(&issuer, &key_pem, &revoked);
        let validation_time = chrono::DateTime::parse_from_rfc3339("2026-06-01T00:00:00Z")
            .unwrap()
            .with_timezone(&chrono::Utc);

        let status = check_revocation(&crl_der, &cert, &issuer, Some(validation_time)).unwrap();
        assert!(status.is_revoked(), "should be revoked: {status}");
    }

    #[test]
    fn test_check_revocation_time_aware_future_revocation() {
        let key_path = intermediate_ca_key_pem();
        let Ok(key_pem) = std::fs::read_to_string(key_path) else {
            eprintln!("skipping test: intermediate_ca_key.pem not found");
            return;
        };

        let issuer = intermediate_ca_cert();
        let cert = signer_cert();

        let serial = get_cert_serial_body(&cert);

        // Revocation date is 2027-01-01 but validation_time is 2026-06-01
        // → cert should be VALID at validation_time
        let revoked = vec![(serial, "270101120000Z")];
        let crl_der = build_test_crl(&issuer, &key_pem, &revoked);
        let validation_time = chrono::DateTime::parse_from_rfc3339("2026-06-01T00:00:00Z")
            .unwrap()
            .with_timezone(&chrono::Utc);

        let status = check_revocation(&crl_der, &cert, &issuer, Some(validation_time)).unwrap();
        assert!(
            status.is_valid(),
            "should be valid (revocation in future): {status}"
        );
    }

    #[test]
    fn test_parse_crl_invalid_data() {
        let result = parse_crl(&[0x04, 0x00]); // OCTET STRING, not SEQUENCE
        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("SEQUENCE"));
    }

    #[test]
    fn test_parse_revocation_reason_unspecified() {
        // No extensions → Unspecified
        let reason = parse_revocation_reason(&[]);
        assert_eq!(reason, RevocationReason::Unspecified);
    }
}