1 /// Represents a X509 certificate.
2 #[derive(Debug, Clone)]
3 pub struct Certificate {
4     pub(crate) pem: Vec<u8>,
5 }
6 
7 /// Represents a private key and X509 certificate.
8 #[derive(Debug, Clone)]
9 pub struct Identity {
10     pub(crate) cert: Certificate,
11     pub(crate) key: Vec<u8>,
12 }
13 
14 impl Certificate {
15     /// Parse a PEM encoded X509 Certificate.
16     ///
17     /// The provided PEM should include at least one PEM encoded certificate.
from_pem(pem: impl AsRef<[u8]>) -> Self18     pub fn from_pem(pem: impl AsRef<[u8]>) -> Self {
19         let pem = pem.as_ref().into();
20         Self { pem }
21     }
22 
23     /// Get a immutable reference to underlying certificate
get_ref(&self) -> &[u8]24     pub fn get_ref(&self) -> &[u8] {
25         self.pem.as_slice()
26     }
27 
28     /// Get a mutable reference to underlying certificate
get_mut(&mut self) -> &mut [u8]29     pub fn get_mut(&mut self) -> &mut [u8] {
30         self.pem.as_mut()
31     }
32 
33     /// Consumes `self`, returning the underlying certificate
into_inner(self) -> Vec<u8>34     pub fn into_inner(self) -> Vec<u8> {
35         self.pem
36     }
37 }
38 
39 impl AsRef<[u8]> for Certificate {
as_ref(&self) -> &[u8]40     fn as_ref(&self) -> &[u8] {
41         self.pem.as_ref()
42     }
43 }
44 
45 impl AsMut<[u8]> for Certificate {
as_mut(&mut self) -> &mut [u8]46     fn as_mut(&mut self) -> &mut [u8] {
47         self.pem.as_mut()
48     }
49 }
50 
51 impl Identity {
52     /// Parse a PEM encoded certificate and private key.
53     ///
54     /// The provided cert must contain at least one PEM encoded certificate.
from_pem(cert: impl AsRef<[u8]>, key: impl AsRef<[u8]>) -> Self55     pub fn from_pem(cert: impl AsRef<[u8]>, key: impl AsRef<[u8]>) -> Self {
56         let cert = Certificate::from_pem(cert);
57         let key = key.as_ref().into();
58         Self { cert, key }
59     }
60 }
61