rtc_dtls/handshake/
handshake_message_hello_verify_request.rs

1#[cfg(test)]
2mod handshake_message_hello_verify_request_test;
3
4use super::*;
5use crate::record_layer::record_layer_header::*;
6
7use byteorder::{ReadBytesExt, WriteBytesExt};
8use std::io::{Read, Write};
9
10/*
11   The definition of HelloVerifyRequest is as follows:
12
13   struct {
14     ProtocolVersion server_version;
15     opaque cookie<0..2^8-1>;
16   } HelloVerifyRequest;
17
18   The HelloVerifyRequest message type is hello_verify_request(3).
19
20   When the client sends its ClientHello message to the server, the server
21   MAY respond with a HelloVerifyRequest message.  This message contains
22   a stateless cookie generated using the technique of [PHOTURIS].  The
23   client MUST retransmit the ClientHello with the cookie added.
24*/
25/// ## Specifications
26///
27/// * [RFC 6347 §4.2.1]
28///
29/// [RFC 6347 §4.2.1]: https://tools.ietf.org/html/rfc6347#section-4.2.1
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct HandshakeMessageHelloVerifyRequest {
32    pub(crate) version: ProtocolVersion,
33    pub(crate) cookie: Vec<u8>,
34}
35
36impl HandshakeMessageHelloVerifyRequest {
37    pub fn handshake_type(&self) -> HandshakeType {
38        HandshakeType::HelloVerifyRequest
39    }
40
41    pub fn size(&self) -> usize {
42        1 + 1 + 1 + self.cookie.len()
43    }
44
45    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
46        if self.cookie.len() > 255 {
47            return Err(Error::ErrCookieTooLong);
48        }
49
50        writer.write_u8(self.version.major)?;
51        writer.write_u8(self.version.minor)?;
52        writer.write_u8(self.cookie.len() as u8)?;
53        writer.write_all(&self.cookie)?;
54
55        Ok(writer.flush()?)
56    }
57
58    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
59        let major = reader.read_u8()?;
60        let minor = reader.read_u8()?;
61        let cookie_length = reader.read_u8()?;
62        let mut cookie = vec![];
63        reader.read_to_end(&mut cookie)?;
64
65        if cookie.len() < cookie_length as usize {
66            return Err(Error::ErrBufferTooSmall);
67        }
68
69        Ok(HandshakeMessageHelloVerifyRequest {
70            version: ProtocolVersion { major, minor },
71            cookie,
72        })
73    }
74}