Skip to main content

coqui_peer_tls/
certificate.rs

1use core::net::SocketAddr;
2
3use coqui_peer::{KeyMaterial, PeerId, TrustStore};
4
5use crate::error::PeerError;
6use crate::policy::CertificateType;
7
8/// The method name under which certificates and raw public keys are kept in a [`TrustStore`].
9pub const METHOD: &str = "tls";
10
11/// The PEM label of a certificate in the store.
12const CERTIFICATE_LABEL: &str = "CERTIFICATE";
13
14/// The PEM label of a raw public key in the store.
15const PUBLIC_KEY_LABEL: &str = "PUBLIC KEY";
16
17/// A DER-encoded certificate as the trust store's key material: method `tls`, PEM label
18/// `CERTIFICATE`.
19///
20/// # Errors
21///
22/// [`PeerError::Certificate`] when `der` is empty or larger than the store holds.
23pub fn certificate_key(der: &[u8]) -> Result<KeyMaterial, PeerError> {
24    KeyMaterial::new(METHOD, CERTIFICATE_LABEL, der).map_err(|_| PeerError::Certificate)
25}
26
27/// A DER-encoded `SubjectPublicKeyInfo` (a raw public key, RFC 7250) as the trust store's key
28/// material: method `tls`, PEM label `PUBLIC KEY`. Its id is the SHA-256 of the `SubjectPublicKeyInfo`.
29///
30/// # Errors
31///
32/// [`PeerError::Certificate`] when `spki` is empty or larger than the store holds.
33pub fn public_key_key(spki: &[u8]) -> Result<KeyMaterial, PeerError> {
34    KeyMaterial::new(METHOD, PUBLIC_KEY_LABEL, spki).map_err(|_| PeerError::Certificate)
35}
36
37/// The key material of what a peer presented, as the policy's certificate type says.
38pub(crate) fn key_for(kind: CertificateType, der: &[u8]) -> Result<KeyMaterial, PeerError> {
39    match kind {
40        CertificateType::RawPublicKey => public_key_key(der),
41        CertificateType::X509 => certificate_key(der),
42    }
43}
44
45/// The key material of what a connection's peer presented, told by its shape: a handshake
46/// that finished knows no more than the bytes.
47pub(crate) fn key_of_presented(der: &[u8]) -> Result<KeyMaterial, PeerError> {
48    if crate::x509::is_public_key_info(der) {
49        public_key_key(der)
50    } else {
51        certificate_key(der)
52    }
53}
54
55/// The PEM label a stored key of `kind` has.
56pub(crate) fn label_of(kind: CertificateType) -> &'static str {
57    match kind {
58        CertificateType::RawPublicKey => PUBLIC_KEY_LABEL,
59        CertificateType::X509 => CERTIFICATE_LABEL,
60    }
61}
62
63/// Certificate-shaped access to a [`TrustStore`].
64pub trait TrustStoreExt {
65    /// Accepts a certificate directly, without it having waited.
66    ///
67    /// # Errors
68    ///
69    /// [`PeerError::Certificate`] when `der` is too large, [`PeerError::AlreadyAccepted`],
70    /// [`PeerError::Io`] on write failure.
71    fn accept_certificate(&self, der: &[u8]) -> Result<PeerId, PeerError>;
72
73    /// Records a turned-away peer's certificate as waiting. Call it only after the peer
74    /// proved it holds the certificate's key, as the verifiers of this crate do.
75    fn record_waiting_certificate(&self, der: &[u8], source: Option<SocketAddr>);
76
77    /// Accepts a raw public key (a DER `SubjectPublicKeyInfo`) directly, without it having
78    /// waited.
79    ///
80    /// # Errors
81    ///
82    /// As [`accept_certificate`](Self::accept_certificate).
83    fn accept_public_key(&self, spki: &[u8]) -> Result<PeerId, PeerError>;
84
85    /// Records a turned-away peer's raw public key as waiting. Call it only after the peer
86    /// proved it holds the key, as the verifiers of this crate do.
87    fn record_waiting_public_key(&self, spki: &[u8], source: Option<SocketAddr>);
88}
89
90impl TrustStoreExt for TrustStore {
91    fn accept_certificate(&self, der: &[u8]) -> Result<PeerId, PeerError> {
92        Ok(self.accept_key(&certificate_key(der)?)?)
93    }
94
95    fn record_waiting_certificate(&self, der: &[u8], source: Option<SocketAddr>) {
96        if let Ok(key) = certificate_key(der) {
97            self.record_waiting(&key, source);
98        }
99    }
100
101    fn accept_public_key(&self, spki: &[u8]) -> Result<PeerId, PeerError> {
102        Ok(self.accept_key(&public_key_key(spki)?)?)
103    }
104
105    fn record_waiting_public_key(&self, spki: &[u8], source: Option<SocketAddr>) {
106        if let Ok(key) = public_key_key(spki) {
107            self.record_waiting(&key, source);
108        }
109    }
110}