wintrust 0.1.0

Portable Rust catalog trust with Windows API name aliases
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
//! Signed, explicitly supplied offline CRL/OCSP evidence. Missing evidence is unknown.
use super::crypto;
use anyhow::{Context, Result, ensure};
use der::{Decode, Encode};
use serde::{Deserialize, Serialize};
use x509_cert::{
    Certificate,
    crl::CertificateList,
    ext::pkix::{ExtendedKeyUsage, KeyUsage},
};
use x509_ocsp::{BasicOcspResponse, CertStatus, OcspResponse, OcspResponseStatus, ResponderId};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RevocationStatus {
    Good,
    Revoked,
    Unknown,
}
#[derive(Debug, Clone, Serialize)]
pub struct CertificateStatus {
    pub certificate_sha256: String,
    pub status: RevocationStatus,
    pub evidence_sha256: Vec<String>,
    pub diagnostics: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct RevocationReport {
    pub status: RevocationStatus,
    pub certificates: Vec<CertificateStatus>,
    pub current_unix_time: u64,
    pub signature_verification_time: u64,
}
#[derive(Debug, Clone, Copy)]
pub struct RevocationLimits {
    pub max_artifacts: usize,
    pub max_artifact_bytes: usize,
    pub max_chain_certificates: usize,
    pub max_entries: usize,
    /// NextUpdate alone may permit excessively old replay: also bound age.
    pub max_age_seconds: u64,
    pub allow_sha1: bool,
}
impl Default for RevocationLimits {
    fn default() -> Self {
        Self {
            max_artifacts: 256,
            max_artifact_bytes: 16 * 1024 * 1024,
            max_chain_certificates: 16,
            max_entries: 100_000,
            max_age_seconds: 7 * 24 * 3600,
            allow_sha1: false,
        }
    }
}
fn hash(bytes: &[u8]) -> Result<String> {
    Ok(hex::encode(crypto::digest(
        "2.16.840.1.101.3.4.2.1",
        bytes,
    )?))
}
fn verify_signature(
    cert: &Certificate,
    algorithm_der: &[u8],
    message: &[u8],
    signature: &[u8],
    allow_sha1: bool,
) -> Result<()> {
    crypto::verify_algorithm(
        &cert.tbs_certificate.subject_public_key_info.to_der()?,
        algorithm_der,
        None,
        message,
        signature,
        allow_sha1,
    )
}
fn fresh(this: u64, next: Option<u64>, now: u64, limits: RevocationLimits) -> Result<()> {
    let next = next.context("revocation evidence lacks nextUpdate")?;
    ensure!(
        this <= now && now <= next && this < next,
        "stale or future revocation evidence"
    );
    ensure!(
        now - this <= limits.max_age_seconds,
        "revocation evidence age exceeds policy"
    );
    Ok(())
}
fn extensions_supported(
    exts: Option<&[x509_cert::ext::Extension]>,
    allowed: &[&str],
) -> Result<()> {
    let mut seen = std::collections::BTreeSet::new();
    for e in exts.unwrap_or_default() {
        let oid = e.extn_id.to_string();
        ensure!(seen.insert(oid.clone()), "duplicate revocation extension");
        ensure!(
            !e.critical || allowed.contains(&oid.as_str()),
            "unsupported critical revocation extension {oid}"
        );
    }
    Ok(())
}
/// Verify a complete direct CRL issued by this exact certificate issuer.
/// Delta/indirect/partitioned CRLs fail closed; they cannot prove absence here.
pub fn verify_crl(
    bytes: &[u8],
    certificate: &Certificate,
    issuer: &Certificate,
    now: u64,
    limits: RevocationLimits,
) -> Result<RevocationStatus> {
    ensure!(bytes.len() <= limits.max_artifact_bytes, "CRL byte limit");
    let crl = CertificateList::from_der(bytes)?;
    let tbs = &crl.tbs_cert_list;
    ensure!(
        certificate.tbs_certificate.issuer == issuer.tbs_certificate.subject
            && tbs.issuer == issuer.tbs_certificate.subject,
        "CRL issuer mismatch"
    );
    ensure!(
        tbs.signature == crl.signature_algorithm,
        "CRL signature algorithm mismatch"
    );
    let ku = issuer
        .tbs_certificate
        .get::<KeyUsage>()?
        .context("CRL signer keyUsage missing")?;
    ensure!(ku.1.crl_sign(), "issuer lacks cRLSign usage");
    let exts = tbs.crl_extensions.as_deref();
    extensions_supported(exts, &[])?;
    for e in exts.unwrap_or_default() {
        let id = e.extn_id.to_string();
        ensure!(
            id != "2.5.29.27" && id != "2.5.29.28" && id != "2.5.29.46",
            "delta or scoped CRL unsupported"
        );
        if id == "2.5.29.35" {
            let aki =
                x509_cert::ext::pkix::AuthorityKeyIdentifier::from_der(e.extn_value.as_bytes())?;
            ensure!(
                aki.authority_cert_issuer.is_some() == aki.authority_cert_serial_number.is_some(),
                "CRL authority issuer/serial must appear together"
            );
            if let Some(names) = &aki.authority_cert_issuer {
                use x509_cert::ext::pkix::name::GeneralName;
                ensure!(
                    names.len() == 1
                        && matches!(&names[0],GeneralName::DirectoryName(name) if *name==issuer.tbs_certificate.issuer),
                    "CRL authority certificate issuer mismatch"
                );
            }
            if let Some(key) = aki.key_identifier {
                let ski = issuer
                    .tbs_certificate
                    .get::<x509_cert::ext::pkix::SubjectKeyIdentifier>()?
                    .context("CRL AKI has no issuer SKI")?;
                ensure!(key == ski.1.0, "CRL authority key mismatch");
            }
            if let Some(serial) = aki.authority_cert_serial_number {
                ensure!(
                    serial == issuer.tbs_certificate.serial_number,
                    "CRL authority serial mismatch"
                );
            }
        }
    }
    let signature = crl
        .signature
        .as_bytes()
        .context("unaligned CRL signature")?;
    verify_signature(
        issuer,
        &crl.signature_algorithm.to_der()?,
        &tbs.to_der()?,
        signature,
        limits.allow_sha1,
    )?;
    let entries = tbs.revoked_certificates.as_deref().unwrap_or_default();
    ensure!(entries.len() <= limits.max_entries, "CRL entry limit");
    let mut serials = std::collections::BTreeSet::new();
    let mut revoked = false;
    for entry in entries {
        ensure!(
            serials.insert(entry.serial_number.as_bytes().to_vec()),
            "duplicate CRL serial"
        );
        extensions_supported(entry.crl_entry_extensions.as_deref(), &[])?;
        ensure!(
            !entry
                .crl_entry_extensions
                .as_deref()
                .unwrap_or_default()
                .iter()
                .any(|e| e.extn_id.to_string() == "2.5.29.29"),
            "indirect CRL entry unsupported"
        );
        ensure!(
            entry.revocation_date.to_unix_duration().as_secs() <= now,
            "CRL revocation date is future"
        );
        if entry.serial_number == certificate.tbs_certificate.serial_number {
            revoked = true;
        }
    }
    // A signed revocation always dominates, including an otherwise stale CRL.
    if revoked {
        return Ok(RevocationStatus::Revoked);
    }
    fresh(
        tbs.this_update.to_unix_duration().as_secs(),
        tbs.next_update.map(|t| t.to_unix_duration().as_secs()),
        now,
        limits,
    )?;
    Ok(RevocationStatus::Good)
}
fn responder_matches(id: &ResponderId, certificate: &Certificate) -> Result<bool> {
    Ok(match id {
        ResponderId::ByName(name) => *name == certificate.tbs_certificate.subject,
        ResponderId::ByKey(key) => {
            key.as_bytes()
                == crypto::digest_with_policy(
                    "1.3.14.3.2.26",
                    certificate
                        .tbs_certificate
                        .subject_public_key_info
                        .subject_public_key
                        .as_bytes()
                        .context("unaligned responder public key")?,
                    true,
                )?
        }
    })
}
/// Verify issuer-signed or directly delegated OCSP. Delegation requires an
/// issuer signature and OCSP EKU, never merely a matching responder name.
pub fn verify_ocsp(
    bytes: &[u8],
    certificate: &Certificate,
    issuer: &Certificate,
    now: u64,
    limits: RevocationLimits,
) -> Result<RevocationStatus> {
    ensure!(bytes.len() <= limits.max_artifact_bytes, "OCSP byte limit");
    let response = OcspResponse::from_der(bytes)?;
    ensure!(
        response.response_status == OcspResponseStatus::Successful,
        "OCSP unsuccessful response"
    );
    let response = response
        .response_bytes
        .context("missing OCSP responseBytes")?;
    ensure!(
        response.response_type.to_string() == "1.3.6.1.5.5.7.48.1.1",
        "unsupported OCSP response type"
    );
    let basic = BasicOcspResponse::from_der(response.response.as_bytes())?;
    let tbs = &basic.tbs_response_data;
    ensure!(
        certificate.tbs_certificate.issuer == issuer.tbs_certificate.subject,
        "OCSP certificate issuer mismatch"
    );
    ensure!(
        tbs.responses.len() <= limits.max_entries,
        "OCSP response limit"
    );
    extensions_supported(tbs.response_extensions.as_deref(), &[])?;
    let produced = tbs.produced_at.0.to_unix_duration().as_secs();
    ensure!(produced <= now, "future OCSP producedAt");
    let responder = if responder_matches(&tbs.responder_id, issuer)? {
        issuer.clone()
    } else {
        let certs = basic
            .certs
            .as_deref()
            .context("OCSP responder certificate absent")?;
        ensure!(
            certs.len() <= limits.max_chain_certificates,
            "OCSP certificate limit"
        );
        let candidates = certs
            .iter()
            .map(|c| responder_matches(&tbs.responder_id, c).map(|yes| (c, yes)))
            .collect::<Result<Vec<_>>>()?;
        let candidates: Vec<_> = candidates.into_iter().filter(|(_, yes)| *yes).collect();
        ensure!(candidates.len() == 1, "ambiguous OCSP responder");
        let cert = candidates[0].0;
        extensions_supported(
            cert.tbs_certificate.extensions.as_deref(),
            &[
                "2.5.29.19",
                "2.5.29.15",
                "2.5.29.37",
                "2.5.29.14",
                "2.5.29.35",
            ],
        )?;
        if let Some((_, constraints)) = cert
            .tbs_certificate
            .get::<x509_cert::ext::pkix::BasicConstraints>()?
        {
            ensure!(!constraints.ca, "OCSP delegate must be an end entity");
        }

        ensure!(
            cert.tbs_certificate.issuer == issuer.tbs_certificate.subject,
            "OCSP responder not delegated by issuer"
        );
        ensure!(
            cert.signature_algorithm == cert.tbs_certificate.signature,
            "OCSP responder signature algorithm mismatch"
        );
        verify_signature(
            issuer,
            &cert.signature_algorithm.to_der()?,
            &cert.tbs_certificate.to_der()?,
            cert.signature
                .as_bytes()
                .context("unaligned responder signature")?,
            limits.allow_sha1,
        )?;
        let eku = cert
            .tbs_certificate
            .get::<ExtendedKeyUsage>()?
            .context("OCSP responder EKU absent")?;
        ensure!(
            eku.1.0.iter().any(|o| o.to_string() == "1.3.6.1.5.5.7.3.9"),
            "OCSP responder EKU missing"
        );
        let usage = cert
            .tbs_certificate
            .get::<KeyUsage>()?
            .context("OCSP responder keyUsage absent")?;
        ensure!(
            usage.1.digital_signature(),
            "OCSP responder lacks digitalSignature"
        );
        ensure!(
            cert.tbs_certificate
                .validity
                .not_before
                .to_unix_duration()
                .as_secs()
                <= produced
                && produced
                    <= cert
                        .tbs_certificate
                        .validity
                        .not_after
                        .to_unix_duration()
                        .as_secs(),
            "OCSP responder expired at producedAt"
        );
        // Delegated responder revocation is checked using issuer-signed CRLs by
        // the outer policy; until evidence is supplied, require no-check rather
        // than silently ignoring responder revocation.
        ensure!(
            cert.tbs_certificate
                .extensions
                .as_deref()
                .unwrap_or_default()
                .iter()
                .any(|e| e.extn_id.to_string() == "1.3.6.1.5.5.7.48.1.5"
                    && der::asn1::Null::from_der(e.extn_value.as_bytes()).is_ok()),
            "delegated OCSP responder requires signed nocheck"
        );
        cert.clone()
    };
    verify_signature(
        &responder,
        &basic.signature_algorithm.to_der()?,
        &tbs.to_der()?,
        basic
            .signature
            .as_bytes()
            .context("unaligned OCSP signature")?,
        limits.allow_sha1,
    )?;
    let mut matches = Vec::new();
    for single in &tbs.responses {
        let id = &single.cert_id;
        let oid = id.hash_algorithm.oid.to_string();
        if let Some(parameters) = &id.hash_algorithm.parameters {
            ensure!(
                parameters.is_null(),
                "unsupported OCSP CertID hash parameters"
            );
        }
        if id.serial_number != certificate.tbs_certificate.serial_number {
            continue;
        }
        if id.issuer_name_hash.as_bytes()
            != crypto::digest_with_policy(&oid, &issuer.tbs_certificate.subject.to_der()?, true)?
            || id.issuer_key_hash.as_bytes()
                != crypto::digest_with_policy(
                    &oid,
                    issuer
                        .tbs_certificate
                        .subject_public_key_info
                        .subject_public_key
                        .as_bytes()
                        .context("unaligned issuer public key")?,
                    true,
                )?
        {
            continue;
        }
        extensions_supported(single.single_extensions.as_deref(), &[])?;
        if let CertStatus::Revoked(info) = single.cert_status {
            ensure!(
                info.revocation_time.0.to_unix_duration().as_secs() <= now,
                "OCSP revocationTime in future"
            );
            return Ok(RevocationStatus::Revoked);
        }
        ensure!(
            now - produced <= limits.max_age_seconds,
            "stale OCSP producedAt"
        );
        fresh(
            single.this_update.0.to_unix_duration().as_secs(),
            single.next_update.map(|t| t.0.to_unix_duration().as_secs()),
            now,
            limits,
        )?;
        ensure!(
            single.this_update.0.to_unix_duration().as_secs() <= produced,
            "OCSP producedAt precedes thisUpdate"
        );
        matches.push(match single.cert_status {
            CertStatus::Good(_) => RevocationStatus::Good,
            _ => RevocationStatus::Unknown,
        });
    }
    ensure!(
        matches.len() == 1,
        "OCSP absent or ambiguous certificate status"
    );
    Ok(matches[0])
}

/// Check each non-anchor certificate. Invalid artifacts are diagnostics, never
/// positive evidence. Any authenticated revocation dominates every good result.
pub fn verify_chain_revocation(
    path_der: &[Vec<u8>],
    crls: &[Vec<u8>],
    ocsp: &[Vec<u8>],
    verification_time: u64,
    now: u64,
    limits: RevocationLimits,
) -> Result<RevocationReport> {
    ensure!(
        !path_der.is_empty() && path_der.len() <= limits.max_chain_certificates,
        "revocation chain length limit"
    );
    ensure!(
        crls.len() + ocsp.len() <= limits.max_artifacts,
        "revocation artifact count limit"
    );
    let path = path_der
        .iter()
        .map(|b| Certificate::from_der(b))
        .collect::<std::result::Result<Vec<_>, _>>()?;
    let mut certificates = Vec::new();
    for i in 0..path.len() - 1 {
        let mut report = CertificateStatus {
            certificate_sha256: hash(&path_der[i])?,
            status: RevocationStatus::Unknown,
            evidence_sha256: Vec::new(),
            diagnostics: Vec::new(),
        };
        for (kind, artifacts) in [("CRL", crls), ("OCSP", ocsp)] {
            for artifact in artifacts {
                let result = if kind == "CRL" {
                    verify_crl(artifact, &path[i], &path[i + 1], now, limits)
                } else {
                    verify_ocsp(artifact, &path[i], &path[i + 1], now, limits)
                };
                match result {
                    Ok(status) => {
                        report.evidence_sha256.push(hash(artifact)?);
                        if status == RevocationStatus::Revoked
                            || report.status != RevocationStatus::Revoked
                                && status == RevocationStatus::Good
                        {
                            report.status = status;
                        }
                    }
                    Err(error) => report.diagnostics.push(format!("{kind}: {error}")),
                }
            }
        }
        if report.evidence_sha256.is_empty() {
            report
                .diagnostics
                .push("no authenticated fresh issuer-bound status".into());
        }
        certificates.push(report);
    }
    let status = if certificates
        .iter()
        .any(|r| r.status == RevocationStatus::Revoked)
    {
        RevocationStatus::Revoked
    } else if certificates
        .iter()
        .any(|r| r.status == RevocationStatus::Unknown)
    {
        RevocationStatus::Unknown
    } else {
        RevocationStatus::Good
    };
    Ok(RevocationReport {
        status,
        certificates,
        current_unix_time: now,
        signature_verification_time: verification_time,
    })
}

#[derive(Debug, Clone, Copy)]
pub struct OnlineLimits {
    pub max_requests: usize,
    pub max_response_bytes: usize,
    pub max_total_bytes: usize,
    pub timeout_seconds: u64,
}
impl Default for OnlineLimits {
    fn default() -> Self {
        Self {
            max_requests: 16,
            max_response_bytes: 16 * 1024 * 1024,
            max_total_bytes: 32 * 1024 * 1024,
            timeout_seconds: 30,
        }
    }
}
#[derive(Debug, Default)]
pub struct AcquiredRevocation {
    pub crls: Vec<Vec<u8>>,
    pub ocsp_responses: Vec<Vec<u8>>,
    pub diagnostics: Vec<String>,
}
/// Retrieve status only when caller explicitly chose online policy. The chain
/// must already be authenticated to caller-supplied anchors. Redirects and URL
/// credentials are refused; byte/request/wall-time limits apply. HTTP transport
/// is permitted because acceptance depends on the signed issuer-bound artifact.
pub fn acquire_chain_revocation(
    path_der: &[Vec<u8>],
    now: u64,
    limits: RevocationLimits,
    online: OnlineLimits,
) -> Result<AcquiredRevocation> {
    use std::io::Read;
    use x509_cert::ext::pkix::{
        AuthorityInfoAccessSyntax, CrlDistributionPoints,
        name::{DistributionPointName, GeneralName},
    };
    ensure!(
        path_der.len() <= limits.max_chain_certificates,
        "online chain limit"
    );
    ensure!(
        online.timeout_seconds > 0
            && online.timeout_seconds <= 120
            && online.max_requests <= 64
            && online.max_response_bytes <= limits.max_artifact_bytes,
        "invalid online limits"
    );
    let path = path_der
        .iter()
        .map(|b| Certificate::from_der(b))
        .collect::<std::result::Result<Vec<_>, _>>()?;
    let client = reqwest::blocking::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .connect_timeout(std::time::Duration::from_secs(5))
        .build()?;
    let start = std::time::Instant::now();
    let deadline = std::time::Duration::from_secs(online.timeout_seconds);
    let mut output = AcquiredRevocation::default();
    let mut count = 0usize;
    let mut total = 0usize;
    let mut attempted = std::collections::BTreeSet::new();
    for pair in path.windows(2) {
        let cert = &pair[0];
        let issuer = &pair[1];
        let mut requests = Vec::new();
        if let Some((_, points)) = cert.tbs_certificate.get::<CrlDistributionPoints>()? {
            ensure!(points.0.len() <= 64, "online distribution point limit");
            for point in points.0 {
                if point.reasons.is_some() || point.crl_issuer.is_some() {
                    continue;
                }
                if let Some(DistributionPointName::FullName(names)) = point.distribution_point {
                    for name in names {
                        if let GeneralName::UniformResourceIdentifier(uri) = name {
                            requests.push((false, uri.as_str().to_owned()));
                        }
                    }
                }
            }
        }
        if let Some((_, access)) = cert.tbs_certificate.get::<AuthorityInfoAccessSyntax>()? {
            ensure!(access.0.len() <= 64, "online AIA limit");
            for description in access.0 {
                if description.access_method.to_string() == "1.3.6.1.5.5.7.48.1"
                    && let GeneralName::UniformResourceIdentifier(uri) = description.access_location
                {
                    requests.push((true, uri.as_str().to_owned()));
                }
            }
        }
        for (is_ocsp, url) in requests {
            if !attempted.insert((
                is_ocsp,
                url.clone(),
                cert.tbs_certificate.serial_number.as_bytes().to_vec(),
            )) {
                continue;
            }
            if count >= online.max_requests
                || start.elapsed() >= deadline
                || total >= online.max_total_bytes
            {
                output
                    .diagnostics
                    .push("online revocation request/time/total byte limit reached".into());
                return Ok(output);
            }
            let result = (|| -> Result<Vec<u8>> {
                ensure!(url.len() <= 8192, "revocation URL length limit");
                let parsed = reqwest::Url::parse(&url)?;
                ensure!(
                    matches!(parsed.scheme(), "http" | "https")
                        && parsed.host_str().is_some()
                        && parsed.username().is_empty()
                        && parsed.password().is_none()
                        && parsed.fragment().is_none(),
                    "unsupported revocation URL"
                );
                let timeout = deadline
                    .checked_sub(start.elapsed())
                    .context("online time limit")?;
                let request = if is_ocsp {
                    client
                        .post(parsed)
                        .header("Content-Type", "application/ocsp-request")
                        .header("Accept", "application/ocsp-response")
                        .body(ocsp_request(cert, issuer)?)
                } else {
                    client.get(parsed)
                };
                let mut response = request.timeout(timeout).send()?.error_for_status()?;
                ensure!(
                    response.status().is_success(),
                    "revocation redirect or non-success status"
                );
                let remaining = online.max_total_bytes.saturating_sub(total);
                let response_limit = online.max_response_bytes.min(remaining);
                if let Some(length) = response.content_length() {
                    ensure!(
                        length <= response_limit as u64,
                        "revocation response declared byte limit"
                    );
                }
                let mut bytes = Vec::new();
                response
                    .by_ref()
                    .take(response_limit as u64 + 1)
                    .read_to_end(&mut bytes)?;
                total = total
                    .checked_add(bytes.len())
                    .context("online total overflow")?;
                ensure!(
                    bytes.len() <= response_limit,
                    "revocation response byte limit"
                );
                ensure!(
                    total <= online.max_total_bytes,
                    "online response total byte limit"
                );
                let status = if is_ocsp {
                    verify_ocsp(&bytes, cert, issuer, now, limits)?
                } else {
                    verify_crl(&bytes, cert, issuer, now, limits)?
                };
                ensure!(
                    status != RevocationStatus::Unknown,
                    "online responder status unknown"
                );
                Ok(bytes)
            })();
            count += 1;
            match result {
                Ok(bytes) => {
                    if is_ocsp {
                        output.ocsp_responses.push(bytes)
                    } else {
                        output.crls.push(bytes)
                    }
                }
                Err(error) => output.diagnostics.push(format!(
                    "online {}: {error}",
                    if is_ocsp { "OCSP" } else { "CRL" }
                )),
            }
        }
    }
    Ok(output)
}
fn ocsp_request(certificate: &Certificate, issuer: &Certificate) -> Result<Vec<u8>> {
    use der::asn1::{Null, ObjectIdentifier, OctetString};
    let hash_oid = "1.3.14.3.2.26";
    let id = x509_ocsp::CertId {
        hash_algorithm: x509_cert::spki::AlgorithmIdentifierOwned {
            oid: ObjectIdentifier::new(hash_oid)?,
            parameters: Some(Null.into()),
        },
        issuer_name_hash: OctetString::new(crypto::digest_with_policy(
            hash_oid,
            &issuer.tbs_certificate.subject.to_der()?,
            true,
        )?)?,
        issuer_key_hash: OctetString::new(crypto::digest_with_policy(
            hash_oid,
            issuer
                .tbs_certificate
                .subject_public_key_info
                .subject_public_key
                .as_bytes()
                .context("unaligned issuer key")?,
            true,
        )?)?,
        serial_number: certificate.tbs_certificate.serial_number.clone(),
    };
    let request = x509_ocsp::OcspRequest {
        tbs_request: x509_ocsp::TbsRequest {
            version: Default::default(),
            requestor_name: None,
            request_list: vec![x509_ocsp::Request {
                req_cert: id,
                single_request_extensions: None,
            }],
            request_extensions: None,
        },
        optional_signature: None,
    };
    Ok(request.to_der()?)
}