webrtc_dtls/handshake/
handshake_message_hello_verify_request.rs

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