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