embedded_tls/extensions/extension_data/
max_fragment_length.rs

1use crate::{
2    buffer::CryptoBuffer,
3    parse_buffer::{ParseBuffer, ParseError},
4    TlsError,
5};
6
7/// Maximum plaintext fragment length
8///
9/// RFC 6066, Section 4.  Maximum Fragment Length Negotiation
10/// Without this extension, TLS specifies a fixed maximum plaintext
11/// fragment length of 2^14 bytes.  It may be desirable for constrained
12/// clients to negotiate a smaller maximum fragment length due to memory
13/// limitations or bandwidth limitations.
14#[derive(Debug, Copy, Clone, PartialEq)]
15#[cfg_attr(feature = "defmt", derive(defmt::Format))]
16pub enum MaxFragmentLength {
17    /// 512 bytes
18    Bits9 = 1,
19    /// 1024 bytes
20    Bits10 = 2,
21    /// 2048 bytes
22    Bits11 = 3,
23    /// 4096 bytes
24    Bits12 = 4,
25}
26
27impl MaxFragmentLength {
28    pub fn parse(buf: &mut ParseBuffer) -> Result<Self, ParseError> {
29        match buf.read_u8()? {
30            1 => Ok(Self::Bits9),
31            2 => Ok(Self::Bits10),
32            3 => Ok(Self::Bits11),
33            4 => Ok(Self::Bits12),
34            other => {
35                warn!("Read unknown MaxFragmentLength: {}", other);
36                Err(ParseError::InvalidData)
37            }
38        }
39    }
40
41    pub fn encode(&self, buf: &mut CryptoBuffer) -> Result<(), TlsError> {
42        buf.push(*self as u8).map_err(|_| TlsError::EncodeError)
43    }
44}