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    /// The record content type this message is carried in.
24    pub fn content_type(&self) -> ContentType {
25        ContentType::ChangeCipherSpec
26    }
27
28    /// The encoded size of this message in bytes.
29    pub fn size(&self) -> usize {
30        1
31    }
32
33    /// Encodes this message to `writer`.
34    ///
35    /// # Errors
36    ///
37    /// Fails on a write error, or if a field exceeds the length its wire format allows.
38    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
39        writer.write_u8(0x01)?;
40
41        Ok(writer.flush()?)
42    }
43
44    /// Decodes one of these messages from `reader`.
45    ///
46    /// # Errors
47    ///
48    /// Fails if `reader` is truncated or its contents are not a valid encoding.
49    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
50        let data = reader.read_u8()?;
51        if data != 0x01 {
52            return Err(Error::ErrInvalidCipherSpec);
53        }
54
55        Ok(ChangeCipherSpec {})
56    }
57}