tlspuffin/tls/rustls/
key.rs

1use std::fmt;
2
3use comparable::Comparable;
4use extractable_macro::Extractable;
5use puffin::codec::{Codec, Reader};
6
7use crate::tls::TLSProtocolTypes;
8
9/// This type contains a private key by value.
10///
11/// The private key must be DER-encoded ASN.1 in either
12/// PKCS#8 or PKCS#1 format.
13///
14/// The `rustls-pemfile` crate can be used to extract
15/// private keys from a PEM file in these formats.
16#[derive(Debug, Clone, Eq, PartialEq)]
17pub struct PrivateKey(pub Vec<u8>);
18
19/// This type contains a single certificate by value.
20///
21/// The certificate must be DER-encoded X.509.
22///
23/// The `rustls-pemfile` crate can be used to parse a PEM file.
24#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Extractable, Comparable)]
25#[extractable(TLSProtocolTypes)]
26pub struct Certificate(#[extractable_no_recursion] pub Vec<u8>);
27
28impl Codec for PrivateKey {
29    fn encode(&self, bytes: &mut Vec<u8>) {
30        bytes.append(&mut self.0.clone())
31    }
32
33    fn read(r: &mut Reader) -> Option<Self> {
34        <Vec<u8> as Codec>::read(r).map(PrivateKey)
35    }
36}
37
38impl AsRef<[u8]> for Certificate {
39    fn as_ref(&self) -> &[u8] {
40        &self.0
41    }
42}
43
44impl fmt::Debug for Certificate {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        f.debug_tuple("Certificate").field(&self.0).finish()
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::Certificate;
53
54    #[test_log::test]
55    fn certificate_debug() {
56        assert_eq!(
57            "Certificate([97, 98])",
58            format!("{:?}", Certificate(b"ab".to_vec()))
59        );
60    }
61}