webrtc_dtls/change_cipher_spec/
mod.rs

1#[cfg(test)]
2mod change_cipher_spec_test;
3
4use std::io::{Read, Write};
5
6use byteorder::{ReadBytesExt, WriteBytesExt};
7
8use super::content::*;
9use super::error::*;
10
11// The change cipher spec protocol exists to signal transitions in
12// ciphering strategies.  The protocol consists of a single message,
13// which is encrypted and compressed under the current (not the pending)
14// connection state.  The message consists of a single byte of value 1.
15/// ## Specifications
16///
17/// * [RFC 5246 §7.1]
18///
19/// [RFC 5246 §7.1]: https://tools.ietf.org/html/rfc5246#section-7.1
20#[derive(Clone, PartialEq, Eq, Debug)]
21pub struct ChangeCipherSpec;
22
23impl ChangeCipherSpec {
24    pub fn content_type(&self) -> ContentType {
25        ContentType::ChangeCipherSpec
26    }
27
28    pub fn size(&self) -> usize {
29        1
30    }
31
32    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
33        writer.write_u8(0x01)?;
34
35        Ok(writer.flush()?)
36    }
37
38    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
39        let data = reader.read_u8()?;
40        if data != 0x01 {
41            return Err(Error::ErrInvalidCipherSpec);
42        }
43
44        Ok(ChangeCipherSpec {})
45    }
46}