coqui_access_mtls/
trust.rs1use 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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Leaf)]
12#[map(rename_all = "kebab-case")]
13pub enum TrustMode {
14 #[default]
16 Pinned,
17 Ca,
19 PinnedOrCa,
21}
22
23impl TrustMode {
24 pub(crate) fn pins(self) -> bool {
26 matches!(self, Self::Pinned | Self::PinnedOrCa)
27 }
28
29 pub(crate) fn uses_ca(self) -> bool {
31 matches!(self, Self::Ca | Self::PinnedOrCa)
32 }
33}
34
35pub fn default_account() -> Vec<Pick> {
39 Vec::new()
40}
41
42pub(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 Err(PeerError::Io(_)) => return Ok(None),
58 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}