rustls/client/
common.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3
4use super::ResolvesClientCert;
5use crate::log::{debug, trace};
6use crate::msgs::enums::ExtensionType;
7use crate::msgs::handshake::{CertificateChain, DistinguishedName, ServerExtension};
8use crate::sync::Arc;
9use crate::{SignatureScheme, compress, sign};
10
11#[derive(Debug)]
12pub(super) struct ServerCertDetails<'a> {
13    pub(super) cert_chain: CertificateChain<'a>,
14    pub(super) ocsp_response: Vec<u8>,
15}
16
17impl<'a> ServerCertDetails<'a> {
18    pub(super) fn new(cert_chain: CertificateChain<'a>, ocsp_response: Vec<u8>) -> Self {
19        Self {
20            cert_chain,
21            ocsp_response,
22        }
23    }
24
25    pub(super) fn into_owned(self) -> ServerCertDetails<'static> {
26        let Self {
27            cert_chain,
28            ocsp_response,
29        } = self;
30        ServerCertDetails {
31            cert_chain: cert_chain.into_owned(),
32            ocsp_response,
33        }
34    }
35}
36
37pub(super) struct ClientHelloDetails {
38    pub(super) alpn_protocols: Vec<Vec<u8>>,
39    pub(super) sent_extensions: Vec<ExtensionType>,
40    pub(super) extension_order_seed: u16,
41    pub(super) offered_cert_compression: bool,
42}
43
44impl ClientHelloDetails {
45    pub(super) fn new(alpn_protocols: Vec<Vec<u8>>, extension_order_seed: u16) -> Self {
46        Self {
47            alpn_protocols,
48            sent_extensions: Vec::new(),
49            extension_order_seed,
50            offered_cert_compression: false,
51        }
52    }
53
54    pub(super) fn server_sent_unsolicited_extensions(
55        &self,
56        received_exts: &[ServerExtension],
57        allowed_unsolicited: &[ExtensionType],
58    ) -> bool {
59        for ext in received_exts {
60            let ext_type = ext.ext_type();
61            if !self.sent_extensions.contains(&ext_type) && !allowed_unsolicited.contains(&ext_type)
62            {
63                trace!("Unsolicited extension {:?}", ext_type);
64                return true;
65            }
66        }
67
68        false
69    }
70}
71
72pub(super) enum ClientAuthDetails {
73    /// Send an empty `Certificate` and no `CertificateVerify`.
74    Empty { auth_context_tls13: Option<Vec<u8>> },
75    /// Send a non-empty `Certificate` and a `CertificateVerify`.
76    Verify {
77        certkey: Arc<sign::CertifiedKey>,
78        signer: Box<dyn sign::Signer>,
79        auth_context_tls13: Option<Vec<u8>>,
80        compressor: Option<&'static dyn compress::CertCompressor>,
81    },
82}
83
84impl ClientAuthDetails {
85    pub(super) fn resolve(
86        resolver: &dyn ResolvesClientCert,
87        canames: Option<&[DistinguishedName]>,
88        sigschemes: &[SignatureScheme],
89        auth_context_tls13: Option<Vec<u8>>,
90        compressor: Option<&'static dyn compress::CertCompressor>,
91    ) -> Self {
92        let acceptable_issuers = canames
93            .unwrap_or_default()
94            .iter()
95            .map(|p| p.as_ref())
96            .collect::<Vec<&[u8]>>();
97
98        if let Some(certkey) = resolver.resolve(&acceptable_issuers, sigschemes) {
99            if let Some(signer) = certkey.key.choose_scheme(sigschemes) {
100                debug!("Attempting client auth");
101                return Self::Verify {
102                    certkey,
103                    signer,
104                    auth_context_tls13,
105                    compressor,
106                };
107            }
108        }
109
110        debug!("Client auth requested but no cert/sigscheme available");
111        Self::Empty { auth_context_tls13 }
112    }
113}