Skip to main content

coqui_access_mtls/
trust.rs

1//! Which client certificates are trusted, and which account a CA-trusted one names.
2
3use coqui_access::guatiao::Bytes;
4use coqui_access::guatiao::library::ProviderError;
5use coqui_access::{Lookup, Verdict, error};
6use coqui_access_kit::pick::{Pick, Picked, pick};
7use coqui_builder::Leaf;
8use coqui_peer_tls::{CertificateFields, PeerError, Policy, Trust};
9
10/// How a client certificate is trusted.
11#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Leaf)]
12#[map(rename_all = "kebab-case")]
13pub enum TrustMode {
14    /// Its fingerprint is a record of the table: a pinned peer.
15    #[default]
16    Pinned,
17    /// A CA of the configured `ca_file` signed it, and its names match the globs.
18    Ca,
19    /// Pinned, or else signed by the CA.
20    PinnedOrCa,
21}
22
23impl TrustMode {
24    /// Whether the table's records are consulted.
25    pub(crate) fn pins(self) -> bool {
26        matches!(self, Self::Pinned | Self::PinnedOrCa)
27    }
28
29    /// Whether the CA is consulted.
30    pub(crate) fn uses_ca(self) -> bool {
31        matches!(self, Self::Ca | Self::PinnedOrCa)
32    }
33}
34
35/// The picks a CA-trusted certificate names its account by unless told otherwise: none. It
36/// is then answered by its claims (`Lookup { by: "claims", .. }`), and the consumer's
37/// matcher ties it to a directory account.
38pub fn default_account() -> Vec<Pick> {
39    Vec::new()
40}
41
42/// Checks a chain against the CA and picks the account it names; `None` when the CA does not
43/// trust it.
44pub(crate) fn by_ca(
45    trust: &Trust,
46    policy: &Policy,
47    account: &[Pick],
48    chain: &[Bytes],
49) -> Result<Option<Verdict>, ProviderError> {
50    let Some((leaf, rest)) = chain.split_first() else {
51        return Ok(None);
52    };
53    let intermediates: Vec<&[u8]> = rest.iter().map(AsRef::as_ref).collect();
54    match trust.verify_client_chain(policy, leaf.as_ref(), &intermediates) {
55        Ok(()) => {}
56        // The chain is refused: not trusted.
57        Err(PeerError::Io(_)) => return Ok(None),
58        // The trust could not be built (an unreadable CA file): could not find out.
59        Err(other) => {
60            return Err(error::internal(&format!(
61                "client certificate authority: {other}"
62            )));
63        }
64    }
65    let Ok(fields) = CertificateFields::parse(leaf.as_ref()) else {
66        return Ok(None);
67    };
68    let claims = coqui_access_claims::of_certificate(leaf.as_ref());
69    if account.is_empty() {
70        return Ok(Some(Verdict::Valid(Lookup {
71            by: "claims".to_owned(),
72            value: fields.fingerprint.canonical(),
73            claims,
74        })));
75    }
76    Ok(Some(match pick(&fields.to_map(), account) {
77        Picked::Found { by, value } => Verdict::Valid(Lookup { by, value, claims }),
78        Picked::Ambiguous => Verdict::Invalid,
79        _ => Verdict::Unknown,
80    }))
81}