Skip to main content

rustls/
compress.rs

1//! Certificate compression and decompression support
2//!
3//! This crate supports compression and decompression everywhere
4//! certificates are used, in accordance with [RFC 8879][rfc8879].
5//!
6//! Note that this is only supported for TLS1.3 connections.
7//!
8//! # Getting started
9//!
10//! Build this crate with the `brotli` and/or `zlib` crate features.  This
11//! adds dependencies on these crates.  They are used by default if enabled.
12//!
13//! We especially recommend `brotli` as it has the widest deployment so far.
14//!
15//! # Custom compression/decompression implementations
16//!
17//! 1. Implement the [`CertCompressor`] and/or [`CertDecompressor`] traits
18//! 2. Provide those to:
19//!   - [`ClientConfig::cert_compressors`][cc_cc] or [`ServerConfig::cert_compressors`][sc_cc].
20//!   - [`ClientConfig::cert_decompressors`][cc_cd] or [`ServerConfig::cert_decompressors`][sc_cd].
21//!
22//! These are used in these circumstances:
23//!
24//! | Peer | Client authentication | Server authentication |
25//! | ---- | --------------------- | --------------------- |
26//! | *Client* | [`ClientConfig::cert_compressors`][cc_cc] | [`ClientConfig::cert_decompressors`][cc_cd] |
27//! | *Server* | [`ServerConfig::cert_decompressors`][sc_cd] | [`ServerConfig::cert_compressors`][sc_cc] |
28//!
29//! [rfc8879]: https://datatracker.ietf.org/doc/html/rfc8879
30//! [cc_cc]: crate::ClientConfig::cert_compressors
31//! [sc_cc]: crate::ServerConfig::cert_compressors
32//! [cc_cd]: crate::ClientConfig::cert_decompressors
33//! [sc_cd]: crate::ServerConfig::cert_decompressors
34
35use alloc::collections::VecDeque;
36use alloc::vec::Vec;
37use core::fmt::Debug;
38use std::sync::Mutex;
39
40use crate::crypto::cipher::Payload;
41use crate::enums::CertificateCompressionAlgorithm;
42use crate::msgs::{CertificatePayloadTls13, Codec, CompressedCertificatePayload, SizedPayload};
43use crate::sync::Arc;
44
45/// Returns the supported `CertDecompressor` implementations enabled
46/// by crate features.
47pub fn default_cert_decompressors() -> &'static [&'static dyn CertDecompressor] {
48    &[
49        #[cfg(feature = "brotli")]
50        BROTLI_DECOMPRESSOR,
51        #[cfg(feature = "zlib")]
52        ZLIB_DECOMPRESSOR,
53    ]
54}
55
56/// An available certificate decompression algorithm.
57pub trait CertDecompressor: Debug + Send + Sync {
58    /// Decompress `input`, writing the result to `output`.
59    ///
60    /// `output` is sized to match the declared length of the decompressed data.
61    ///
62    /// `Err(DecompressionFailed)` should be returned if decompression produces more, or fewer
63    /// bytes than fit in `output`, or if the `input` is in any way malformed.
64    fn decompress(&self, input: &[u8], output: &mut [u8]) -> Result<(), DecompressionFailed>;
65
66    /// Which algorithm this decompressor handles.
67    fn algorithm(&self) -> CertificateCompressionAlgorithm;
68}
69
70/// Returns the supported `CertCompressor` implementations enabled
71/// by crate features.
72pub fn default_cert_compressors() -> &'static [&'static dyn CertCompressor] {
73    &[
74        #[cfg(feature = "brotli")]
75        BROTLI_COMPRESSOR,
76        #[cfg(feature = "zlib")]
77        ZLIB_COMPRESSOR,
78    ]
79}
80
81/// An available certificate compression algorithm.
82pub trait CertCompressor: Debug + Send + Sync {
83    /// Compress `input`, returning the result.
84    ///
85    /// `input` is consumed by this function so (if the underlying implementation
86    /// supports it) the compression can be performed in-place.
87    ///
88    /// `level` is a hint as to how much effort to expend on the compression.
89    ///
90    /// `Err(CompressionFailed)` may be returned for any reason.
91    fn compress(
92        &self,
93        input: Vec<u8>,
94        level: CompressionLevel,
95    ) -> Result<Vec<u8>, CompressionFailed>;
96
97    /// Which algorithm this compressor handles.
98    fn algorithm(&self) -> CertificateCompressionAlgorithm;
99}
100
101/// A hint for how many resources to dedicate to a compression.
102#[non_exhaustive]
103#[derive(Debug, Copy, Clone, Eq, PartialEq)]
104pub enum CompressionLevel {
105    /// This compression is happening interactively during a handshake.
106    ///
107    /// Implementations may wish to choose a conservative compression level.
108    Interactive,
109
110    /// The compression may be amortized over many connections.
111    ///
112    /// Implementations may wish to choose an aggressive compression level.
113    Amortized,
114}
115
116/// A content-less error for when `CertDecompressor::decompress` fails.
117#[expect(clippy::exhaustive_structs)]
118#[derive(Debug)]
119pub struct DecompressionFailed;
120
121/// A content-less error for when `CertCompressor::compress` fails.
122#[expect(clippy::exhaustive_structs)]
123#[derive(Debug)]
124pub struct CompressionFailed;
125
126#[cfg(feature = "zlib")]
127mod feat_zlib_rs {
128    use zlib_rs::{
129        DeflateConfig, Inflate, InflateFlush, ReturnCode, Status, compress_bound, compress_slice,
130    };
131
132    use super::*;
133
134    /// A certificate decompressor for the Zlib algorithm using the `zlib-rs` crate.
135    pub const ZLIB_DECOMPRESSOR: &dyn CertDecompressor = &ZlibRsDecompressor;
136
137    #[derive(Debug)]
138    struct ZlibRsDecompressor;
139
140    impl CertDecompressor for ZlibRsDecompressor {
141        fn decompress(&self, input: &[u8], output: &mut [u8]) -> Result<(), DecompressionFailed> {
142            // `Inflate` rather than `decompress_slice()`, because the latter does not
143            // report how much of `input` the stream used: see `total_in()` below.
144            let mut state = Inflate::new(true, ZLIB_WINDOW_BITS);
145            match state.decompress(input, output, InflateFlush::Finish) {
146                Ok(Status::StreamEnd)
147                    if state.total_in() == input.len() as u64
148                        && state.total_out() == output.len() as u64 =>
149                {
150                    Ok(())
151                }
152                _ => Err(DecompressionFailed),
153            }
154        }
155
156        fn algorithm(&self) -> CertificateCompressionAlgorithm {
157            CertificateCompressionAlgorithm::Zlib
158        }
159    }
160
161    /// A certificate compressor for the Zlib algorithm using the `zlib-rs` crate.
162    pub const ZLIB_COMPRESSOR: &dyn CertCompressor = &ZlibRsCompressor;
163
164    #[derive(Debug)]
165    struct ZlibRsCompressor;
166
167    impl CertCompressor for ZlibRsCompressor {
168        fn compress(
169            &self,
170            input: Vec<u8>,
171            level: CompressionLevel,
172        ) -> Result<Vec<u8>, CompressionFailed> {
173            let mut output = alloc::vec![0u8; compress_bound(input.len())];
174            let config = match level {
175                CompressionLevel::Interactive => DeflateConfig::default(),
176                CompressionLevel::Amortized => DeflateConfig::best_compression(),
177            };
178            let (output_filled, rc) = compress_slice(&mut output, &input, config);
179            if rc != ReturnCode::Ok {
180                return Err(CompressionFailed);
181            }
182
183            let used = output_filled.len();
184            output.truncate(used);
185            Ok(output)
186        }
187
188        fn algorithm(&self) -> CertificateCompressionAlgorithm {
189            CertificateCompressionAlgorithm::Zlib
190        }
191    }
192
193    /// Largest LZ77 window, matching `InflateConfig::default()`.
194    const ZLIB_WINDOW_BITS: u8 = 15;
195}
196
197#[cfg(feature = "zlib")]
198pub use feat_zlib_rs::{ZLIB_COMPRESSOR, ZLIB_DECOMPRESSOR};
199
200#[allow(clippy::std_instead_of_core)] // awaits core::io::Cursor (1.97) in crate MSRV
201#[cfg(feature = "brotli")]
202mod feat_brotli {
203    use std::io::{Cursor, Write};
204
205    use brotli_decompressor::{BrotliDecompressStream, BrotliResult, BrotliState, StandardAlloc};
206
207    use super::*;
208
209    /// A certificate decompressor for the brotli algorithm using the `brotli` crate.
210    pub const BROTLI_DECOMPRESSOR: &dyn CertDecompressor = &BrotliDecompressor;
211
212    #[derive(Debug)]
213    struct BrotliDecompressor;
214
215    impl CertDecompressor for BrotliDecompressor {
216        fn decompress(&self, input: &[u8], output: &mut [u8]) -> Result<(), DecompressionFailed> {
217            let mut state = BrotliState::new_strict(
218                StandardAlloc::default(),
219                StandardAlloc::default(),
220                StandardAlloc::default(),
221            );
222            let (mut available_in, mut input_offset) = (input.len(), 0);
223            let (mut available_out, mut output_offset, mut total_out) = (output.len(), 0, 0);
224
225            match BrotliDecompressStream(
226                &mut available_in,
227                &mut input_offset,
228                input,
229                &mut available_out,
230                &mut output_offset,
231                output,
232                &mut total_out,
233                &mut state,
234            ) {
235                BrotliResult::ResultSuccess if available_out == 0 && available_in == 0 => Ok(()),
236                _ => Err(DecompressionFailed),
237            }
238        }
239
240        fn algorithm(&self) -> CertificateCompressionAlgorithm {
241            CertificateCompressionAlgorithm::Brotli
242        }
243    }
244
245    /// A certificate compressor for the brotli algorithm using the `brotli` crate.
246    pub const BROTLI_COMPRESSOR: &dyn CertCompressor = &BrotliCompressor;
247
248    #[derive(Debug)]
249    struct BrotliCompressor;
250
251    impl CertCompressor for BrotliCompressor {
252        fn compress(
253            &self,
254            input: Vec<u8>,
255            level: CompressionLevel,
256        ) -> Result<Vec<u8>, CompressionFailed> {
257            let quality = match level {
258                CompressionLevel::Interactive => QUALITY_FAST,
259                CompressionLevel::Amortized => QUALITY_SLOW,
260            };
261            let output = Cursor::new(Vec::with_capacity(input.len() / 2));
262            let mut compressor = brotli::CompressorWriter::new(output, BUFFER_SIZE, quality, LGWIN);
263            compressor
264                .write_all(&input)
265                .map_err(|_| CompressionFailed)?;
266            Ok(compressor.into_inner().into_inner())
267        }
268
269        fn algorithm(&self) -> CertificateCompressionAlgorithm {
270            CertificateCompressionAlgorithm::Brotli
271        }
272    }
273
274    /// Brotli buffer size.
275    ///
276    /// Chosen based on brotli `examples/compress.rs`.
277    const BUFFER_SIZE: usize = 4096;
278
279    /// This is the default lgwin parameter, see `BrotliEncoderInitParams()`
280    const LGWIN: u32 = 22;
281
282    /// Compression quality we use for interactive compressions.
283    /// See <https://blog.cloudflare.com/results-experimenting-brotli> for data.
284    const QUALITY_FAST: u32 = 4;
285
286    /// Compression quality we use for offline compressions (the maximum).
287    const QUALITY_SLOW: u32 = 11;
288}
289
290#[cfg(feature = "brotli")]
291pub use feat_brotli::{BROTLI_COMPRESSOR, BROTLI_DECOMPRESSOR};
292
293/// An LRU cache for compressions.
294///
295/// The prospect of being able to reuse a given compression for many connections
296/// means we can afford to spend more time on that compression (by passing
297/// [`CompressionLevel::Amortized`] to the compressor).
298#[expect(clippy::exhaustive_enums)]
299#[derive(Debug)]
300pub enum CompressionCache {
301    /// No caching happens, and compression happens each time using
302    /// [`CompressionLevel::Interactive`].
303    Disabled,
304
305    /// Compressions are stored in an LRU cache.
306    Enabled(CompressionCacheInner),
307}
308
309/// Innards of an enabled CompressionCache.
310///
311/// You cannot make one of these directly. Use [`CompressionCache::new`].
312#[derive(Debug)]
313pub struct CompressionCacheInner {
314    /// Maximum size of underlying storage.
315    size: usize,
316
317    /// LRU-order entries.
318    ///
319    /// First is least-used, last is most-used.
320    entries: Mutex<VecDeque<Arc<CompressionCacheEntry>>>,
321}
322
323impl CompressionCache {
324    /// Make a `CompressionCache` that stores up to `size` compressed
325    /// certificate messages.
326    pub fn new(size: usize) -> Self {
327        if size == 0 {
328            return Self::Disabled;
329        }
330
331        Self::Enabled(CompressionCacheInner {
332            size,
333            entries: Mutex::new(VecDeque::with_capacity(size)),
334        })
335    }
336
337    /// Return a `CompressionCacheEntry`, which is an owning
338    /// wrapper for a `CompressedCertificatePayload`.
339    ///
340    /// `compressor` is the compression function we have negotiated.
341    /// `original` is the uncompressed certificate message.
342    pub(crate) fn compression_for(
343        &self,
344        compressor: &dyn CertCompressor,
345        original: &CertificatePayloadTls13<'_>,
346    ) -> Result<Arc<CompressionCacheEntry>, CompressionFailed> {
347        match self {
348            Self::Disabled => Self::uncached_compression(compressor, original),
349            Self::Enabled(_) => self.compression_for_impl(compressor, original),
350        }
351    }
352
353    fn compression_for_impl(
354        &self,
355        compressor: &dyn CertCompressor,
356        original: &CertificatePayloadTls13<'_>,
357    ) -> Result<Arc<CompressionCacheEntry>, CompressionFailed> {
358        let (max_size, entries) = match self {
359            Self::Enabled(CompressionCacheInner { size, entries }) => (*size, entries),
360            _ => unreachable!(),
361        };
362
363        // context is a per-connection quantity, and included in the compressed data.
364        // it is not suitable for inclusion in the cache.
365        if !original.context.is_empty() {
366            return Self::uncached_compression(compressor, original);
367        }
368
369        // cache probe:
370        let encoding = original.get_encoding();
371        let algorithm = compressor.algorithm();
372
373        let mut cache = entries
374            .lock()
375            .map_err(|_| CompressionFailed)?;
376        for (i, item) in cache.iter().enumerate() {
377            if item.algorithm == algorithm && item.original == encoding {
378                // this item is now MRU
379                let item = cache.remove(i).unwrap();
380                cache.push_back(item.clone());
381                return Ok(item);
382            }
383        }
384        drop(cache);
385
386        // do compression:
387        let uncompressed_len = encoding.len() as u32;
388        let compressed = compressor.compress(encoding.clone(), CompressionLevel::Amortized)?;
389        let new_entry = Arc::new(CompressionCacheEntry {
390            algorithm,
391            original: encoding,
392            compressed: CompressedCertificatePayload {
393                alg: algorithm,
394                uncompressed_len,
395                compressed: SizedPayload::from(Payload::new(compressed)),
396            },
397        });
398
399        // insert into cache
400        let mut cache = entries
401            .lock()
402            .map_err(|_| CompressionFailed)?;
403        if cache.len() == max_size {
404            cache.pop_front();
405        }
406        cache.push_back(new_entry.clone());
407        Ok(new_entry)
408    }
409
410    /// Compress `original` using `compressor` at `Interactive` level.
411    fn uncached_compression(
412        compressor: &dyn CertCompressor,
413        original: &CertificatePayloadTls13<'_>,
414    ) -> Result<Arc<CompressionCacheEntry>, CompressionFailed> {
415        let algorithm = compressor.algorithm();
416        let encoding = original.get_encoding();
417        let uncompressed_len = encoding.len() as u32;
418        let compressed = compressor.compress(encoding, CompressionLevel::Interactive)?;
419
420        // this `CompressionCacheEntry` in fact never makes it into the cache, so
421        // `original` is left empty
422        Ok(Arc::new(CompressionCacheEntry {
423            algorithm,
424            original: Vec::new(),
425            compressed: CompressedCertificatePayload {
426                alg: algorithm,
427                uncompressed_len,
428                compressed: SizedPayload::from(Payload::new(compressed)),
429            },
430        }))
431    }
432}
433
434impl Default for CompressionCache {
435    fn default() -> Self {
436        // 4 entries allows 2 certificate chains times 2 compression algorithms
437        Self::new(4)
438    }
439}
440
441#[derive(Debug)]
442pub(crate) struct CompressionCacheEntry {
443    // cache key is algorithm + original:
444    algorithm: CertificateCompressionAlgorithm,
445    original: Vec<u8>,
446
447    // cache value is compression result:
448    compressed: CompressedCertificatePayload<'static>,
449}
450
451impl CompressionCacheEntry {
452    pub(crate) fn compressed_cert_payload(&self) -> CompressedCertificatePayload<'_> {
453        self.compressed.as_borrowed()
454    }
455}
456
457#[cfg(all(test, any(feature = "brotli", feature = "zlib")))]
458mod tests {
459    use std::{println, vec};
460
461    use super::*;
462
463    #[test]
464    #[cfg(feature = "zlib")]
465    fn test_zlib() {
466        test_compressor(ZLIB_COMPRESSOR, ZLIB_DECOMPRESSOR);
467    }
468
469    #[test]
470    #[cfg(feature = "brotli")]
471    fn test_brotli() {
472        test_compressor(BROTLI_COMPRESSOR, BROTLI_DECOMPRESSOR);
473    }
474
475    #[test]
476    #[cfg(feature = "brotli")]
477    fn test_brotli_rejects_large_window() {
478        let large_window_empty = [0x11, 0xd0];
479        brotli::BrotliDecompress(&mut &large_window_empty[..], &mut Vec::new()).unwrap();
480        BROTLI_DECOMPRESSOR
481            .decompress(&large_window_empty, &mut [])
482            .unwrap_err();
483    }
484
485    fn test_compressor(comp: &dyn CertCompressor, decomp: &dyn CertDecompressor) {
486        assert_eq!(comp.algorithm(), decomp.algorithm());
487        for sz in [16, 64, 512, 2048, 8192, 16384] {
488            test_trivial_pairwise(comp, decomp, sz);
489        }
490        test_decompress_wrong_len(comp, decomp);
491        test_decompress_trailing_data(comp, decomp);
492        test_decompress_garbage(decomp);
493    }
494
495    fn test_trivial_pairwise(
496        comp: &dyn CertCompressor,
497        decomp: &dyn CertDecompressor,
498        plain_len: usize,
499    ) {
500        let original = vec![0u8; plain_len];
501
502        for level in [CompressionLevel::Interactive, CompressionLevel::Amortized] {
503            let compressed = comp
504                .compress(original.clone(), level)
505                .unwrap();
506            println!(
507                "{:?} compressed trivial {} -> {} using {:?} level",
508                comp.algorithm(),
509                original.len(),
510                compressed.len(),
511                level
512            );
513            let mut recovered = vec![0xffu8; plain_len];
514            decomp
515                .decompress(&compressed, &mut recovered)
516                .unwrap();
517            assert_eq!(original, recovered);
518        }
519    }
520
521    fn test_decompress_wrong_len(comp: &dyn CertCompressor, decomp: &dyn CertDecompressor) {
522        let original = vec![0u8; 2048];
523        let compressed = comp
524            .compress(original.clone(), CompressionLevel::Interactive)
525            .unwrap();
526        println!("{compressed:?}");
527
528        // too big
529        let mut recovered = vec![0xffu8; original.len() + 1];
530        decomp
531            .decompress(&compressed, &mut recovered)
532            .unwrap_err();
533
534        // too small
535        let mut recovered = vec![0xffu8; original.len() - 1];
536        decomp
537            .decompress(&compressed, &mut recovered)
538            .unwrap_err();
539    }
540
541    fn test_decompress_trailing_data(comp: &dyn CertCompressor, decomp: &dyn CertDecompressor) {
542        let original = vec![0u8; 2048];
543        let mut compressed = comp
544            .compress(original.clone(), CompressionLevel::Interactive)
545            .unwrap();
546        compressed.extend_from_slice(b"trailing data");
547
548        let mut recovered = vec![0xffu8; original.len()];
549        decomp
550            .decompress(&compressed, &mut recovered)
551            .unwrap_err();
552    }
553
554    fn test_decompress_garbage(decomp: &dyn CertDecompressor) {
555        let junk = [0u8; 1024];
556        let mut recovered = vec![0u8; 512];
557        decomp
558            .decompress(&junk, &mut recovered)
559            .unwrap_err();
560    }
561
562    #[test]
563    #[cfg(all(feature = "brotli", feature = "zlib"))]
564    fn test_cache_evicts_lru() {
565        use core::sync::atomic::{AtomicBool, Ordering};
566
567        use pki_types::CertificateDer;
568
569        let cache = CompressionCache::default();
570
571        let certs = [CertificateDer::from(vec![1])].into_iter();
572
573        let cert1 = CertificatePayloadTls13::new(certs.clone(), Some(b"1"));
574        let cert2 = CertificatePayloadTls13::new(certs.clone(), Some(b"2"));
575        let cert3 = CertificatePayloadTls13::new(certs.clone(), Some(b"3"));
576        let cert4 = CertificatePayloadTls13::new(certs.clone(), Some(b"4"));
577
578        // insert zlib (1), (2), (3), (4)
579
580        cache
581            .compression_for(
582                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), true),
583                &cert1,
584            )
585            .unwrap();
586        cache
587            .compression_for(
588                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), true),
589                &cert2,
590            )
591            .unwrap();
592        cache
593            .compression_for(
594                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), true),
595                &cert3,
596            )
597            .unwrap();
598        cache
599            .compression_for(
600                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), true),
601                &cert4,
602            )
603            .unwrap();
604
605        // -- now full
606
607        // insert brotli (1) evicts zlib (1)
608        cache
609            .compression_for(
610                &RequireCompress(BROTLI_COMPRESSOR, AtomicBool::default(), true),
611                &cert4,
612            )
613            .unwrap();
614
615        // now zlib (2), (3), (4) and brotli (4) exist
616        cache
617            .compression_for(
618                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
619                &cert2,
620            )
621            .unwrap();
622        cache
623            .compression_for(
624                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
625                &cert3,
626            )
627            .unwrap();
628        cache
629            .compression_for(
630                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
631                &cert4,
632            )
633            .unwrap();
634        cache
635            .compression_for(
636                &RequireCompress(BROTLI_COMPRESSOR, AtomicBool::default(), false),
637                &cert4,
638            )
639            .unwrap();
640
641        // insert zlib (1) requires re-compression & evicts zlib (2)
642        cache
643            .compression_for(
644                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), true),
645                &cert1,
646            )
647            .unwrap();
648
649        // now zlib (1), (3), (4) and brotli (4) exist
650        // query zlib (4), (3), (1) to demonstrate LRU tracks usage rather than insertion
651        cache
652            .compression_for(
653                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
654                &cert4,
655            )
656            .unwrap();
657        cache
658            .compression_for(
659                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
660                &cert3,
661            )
662            .unwrap();
663        cache
664            .compression_for(
665                &RequireCompress(ZLIB_COMPRESSOR, AtomicBool::default(), false),
666                &cert1,
667            )
668            .unwrap();
669
670        // now brotli (4), zlib (4), (3), (1)
671        // insert brotli (1) evicting brotli (4)
672        cache
673            .compression_for(
674                &RequireCompress(BROTLI_COMPRESSOR, AtomicBool::default(), true),
675                &cert1,
676            )
677            .unwrap();
678
679        // verify brotli (4) disappeared
680        cache
681            .compression_for(
682                &RequireCompress(BROTLI_COMPRESSOR, AtomicBool::default(), true),
683                &cert4,
684            )
685            .unwrap();
686
687        #[derive(Debug)]
688        struct RequireCompress(&'static dyn CertCompressor, AtomicBool, bool);
689
690        impl CertCompressor for RequireCompress {
691            fn compress(
692                &self,
693                input: Vec<u8>,
694                level: CompressionLevel,
695            ) -> Result<Vec<u8>, CompressionFailed> {
696                self.1.store(true, Ordering::SeqCst);
697                self.0.compress(input, level)
698            }
699
700            fn algorithm(&self) -> CertificateCompressionAlgorithm {
701                self.0.algorithm()
702            }
703        }
704
705        impl Drop for RequireCompress {
706            fn drop(&mut self) {
707                assert_eq!(self.1.load(Ordering::SeqCst), self.2);
708            }
709        }
710    }
711}