Skip to main content

rtc_dtls/change_cipher_spec/
mod.rs

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