Skip to main content

ytls_server/server_ctx/s_handshake/
r_server_hello.rs

1//! Respond with Server Hello
2
3use crate::ServerHandshakeCtx;
4use crate::TlsServerCtxConfig;
5
6use crate::CtxError;
7use ytls_record::StaticRecordBuilder;
8use ytls_traits::CryptoConfig;
9use ytls_traits::CryptoRng;
10use ytls_traits::ServerHelloBuilder;
11
12use ytls_traits::HandshakeBuilder;
13use ytls_traits::TlsLeftOut;
14
15use ytls_traits::CryptoSha256TranscriptProcessor;
16
17use ytls_traits::Tls13KeyScheduleHandshakeSha256;
18
19impl<Config, Crypto, Rng> ServerHandshakeCtx<Config, Crypto, Rng>
20where
21    Config: TlsServerCtxConfig,
22    Crypto: CryptoConfig,
23    Rng: CryptoRng,
24{
25    #[inline]
26    pub(crate) fn do_server_hello<L: TlsLeftOut, T: CryptoSha256TranscriptProcessor>(
27        &mut self,
28        l: &mut L,
29        t: &mut T,
30    ) -> Result<(), CtxError> {
31        let b =
32            StaticRecordBuilder::<8192>::server_hello_untyped(self).map_err(CtxError::Builder)?;
33
34        t.sha256_update(b.as_hashing_context());
35        l.send_record_out(b.as_encoded_bytes());
36        Ok(())
37    }
38    #[inline]
39    pub(crate) fn key_share_x25519(&self) -> [u8; 36] {
40        let mut r: [u8; 36] = [0; 36];
41        r[0..4].copy_from_slice(&[0x00, 0x1d, 0x00, 0x20]);
42        match &self.public_key {
43            Some(ref s) => {
44                r[4..36].copy_from_slice(s);
45            }
46            None => {}
47        }
48        r
49    }
50}
51
52impl<Config, Crypto, Rng> ServerHelloBuilder for ServerHandshakeCtx<Config, Crypto, Rng>
53where
54    Config: TlsServerCtxConfig,
55    Crypto: CryptoConfig,
56    Rng: CryptoRng,
57{
58    fn legacy_version(&self) -> &[u8; 2] {
59        &[3, 3]
60    }
61    fn server_random(&self) -> &[u8; 32] {
62        &[
63            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
64            25, 26, 27, 28, 29, 30, 31, 32,
65        ]
66    }
67    fn legacy_session_id(&self) -> &[u8] {
68        match &self.client_session_id {
69            Some(ref s) => &s[0..self.client_session_len],
70            None => &[],
71        }
72    }
73    fn selected_cipher_suite(&self) -> &[u8; 2] {
74        // TLS_CHACHA20_POLY1305_SHA256
75        &[0x13, 0x03]
76    }
77    fn selected_legacy_insecure_compression_method(&self) -> Option<u8> {
78        None
79    }
80    fn extensions_list(&self) -> &[u16] {
81        // 28 = Record Size Limit
82        // 43 = Supported Versions
83        // 51 = Key Share
84        // 41 = pre-shared_key
85        &[43, 51]
86    }
87    fn extension_data(&self, ext: u16) -> &[u8] {
88        match ext {
89            // Len 2B + Tls13 = 3 bytes
90            43 => &[0x03, 0x04],
91            // 1d = X25519 pub key 32b len (0x20)
92            51 => &self.key_share,
93            //41 => &[0x00, 0x00],
94            _ => unreachable!(),
95        }
96    }
97}