1 pub mod record_layer_header;
2
3 #[cfg(test)]
4 mod record_layer_test;
5
6 use super::content::*;
7 use super::error::*;
8 use crate::alert::Alert;
9 use crate::application_data::ApplicationData;
10 use crate::change_cipher_spec::ChangeCipherSpec;
11 use crate::handshake::Handshake;
12 use record_layer_header::*;
13
14 use std::io::{Read, Write};
15
16 /*
17 The TLS Record Layer which handles all data transport.
18 The record layer is assumed to sit directly on top of some
19 reliable transport such as TCP. The record layer can carry four types of content:
20
21 1. Handshake messages—used for algorithm negotiation and key establishment.
22 2. ChangeCipherSpec messages—really part of the handshake but technically a separate kind of message.
23 3. Alert messages—used to signal that errors have occurred
24 4. Application layer data
25
26 The DTLS record layer is extremely similar to that of TLS 1.1. The
27 only change is the inclusion of an explicit sequence number in the
28 record. This sequence number allows the recipient to correctly
29 verify the TLS MAC.
30 https://tools.ietf.org/html/rfc4347#section-4.1
31 */
32 #[derive(Debug, Clone, PartialEq)]
33 pub struct RecordLayer {
34 pub record_layer_header: RecordLayerHeader,
35 pub content: Content,
36 }
37
38 impl RecordLayer {
new(protocol_version: ProtocolVersion, epoch: u16, content: Content) -> Self39 pub fn new(protocol_version: ProtocolVersion, epoch: u16, content: Content) -> Self {
40 RecordLayer {
41 record_layer_header: RecordLayerHeader {
42 content_type: content.content_type(),
43 protocol_version,
44 epoch,
45 sequence_number: 0,
46 content_len: content.size() as u16,
47 },
48 content,
49 }
50 }
51
marshal<W: Write>(&self, writer: &mut W) -> Result<()>52 pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
53 self.record_layer_header.marshal(writer)?;
54 self.content.marshal(writer)?;
55 Ok(())
56 }
57
unmarshal<R: Read>(reader: &mut R) -> Result<Self>58 pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
59 let record_layer_header = RecordLayerHeader::unmarshal(reader)?;
60 let content = match record_layer_header.content_type {
61 ContentType::Alert => Content::Alert(Alert::unmarshal(reader)?),
62 ContentType::ApplicationData => {
63 Content::ApplicationData(ApplicationData::unmarshal(reader)?)
64 }
65 ContentType::ChangeCipherSpec => {
66 Content::ChangeCipherSpec(ChangeCipherSpec::unmarshal(reader)?)
67 }
68 ContentType::Handshake => Content::Handshake(Handshake::unmarshal(reader)?),
69 _ => return Err(Error::Other("Invalid Content Type".to_owned())),
70 };
71
72 Ok(RecordLayer {
73 record_layer_header,
74 content,
75 })
76 }
77 }
78
79 // Note that as with TLS, multiple handshake messages may be placed in
80 // the same DTLS record, provided that there is room and that they are
81 // part of the same flight. Thus, there are two acceptable ways to pack
82 // two DTLS messages into the same datagram: in the same record or in
83 // separate records.
84 // https://tools.ietf.org/html/rfc6347#section-4.2.3
unpack_datagram(buf: &[u8]) -> Result<Vec<Vec<u8>>>85 pub(crate) fn unpack_datagram(buf: &[u8]) -> Result<Vec<Vec<u8>>> {
86 let mut out = vec![];
87
88 let mut offset = 0;
89 while buf.len() != offset {
90 if buf.len() - offset <= RECORD_LAYER_HEADER_SIZE {
91 return Err(Error::ErrInvalidPacketLength);
92 }
93
94 let pkt_len = RECORD_LAYER_HEADER_SIZE
95 + (((buf[offset + RECORD_LAYER_HEADER_SIZE - 2] as usize) << 8)
96 | buf[offset + RECORD_LAYER_HEADER_SIZE - 1] as usize);
97 if offset + pkt_len > buf.len() {
98 return Err(Error::ErrInvalidPacketLength);
99 }
100
101 out.push(buf[offset..offset + pkt_len].to_vec());
102 offset += pkt_len
103 }
104
105 Ok(out)
106 }
107