1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::cipher::{MessageEncrypter, MessageDecrypter};
use crate::error::TLSError;
use crate::msgs::message::{Message, BorrowMessage};
static SEQ_SOFT_LIMIT: u64 = 0xffff_ffff_ffff_0000u64;
static SEQ_HARD_LIMIT: u64 = 0xffff_ffff_ffff_fffeu64;
#[derive(PartialEq)]
enum DirectionState {
Invalid,
Prepared,
Active,
}
pub struct RecordLayer {
message_encrypter: Box<dyn MessageEncrypter>,
message_decrypter: Box<dyn MessageDecrypter>,
write_seq: u64,
read_seq: u64,
encrypt_state: DirectionState,
decrypt_state: DirectionState,
}
impl RecordLayer {
pub fn new() -> RecordLayer {
RecordLayer {
message_encrypter: MessageEncrypter::invalid(),
message_decrypter: MessageDecrypter::invalid(),
write_seq: 0,
read_seq: 0,
encrypt_state: DirectionState::Invalid,
decrypt_state: DirectionState::Invalid,
}
}
pub fn is_encrypting(&self) -> bool {
self.encrypt_state == DirectionState::Active
}
pub fn is_decrypting(&self) -> bool {
self.decrypt_state == DirectionState::Active
}
pub fn prepare_message_encrypter(&mut self, cipher: Box<dyn MessageEncrypter>) {
self.message_encrypter = cipher;
self.write_seq = 0;
self.encrypt_state = DirectionState::Prepared;
}
pub fn prepare_message_decrypter(&mut self, cipher: Box<dyn MessageDecrypter>) {
self.message_decrypter = cipher;
self.read_seq = 0;
self.decrypt_state = DirectionState::Prepared;
}
pub fn start_encrypting(&mut self) {
debug_assert!(self.encrypt_state == DirectionState::Prepared);
self.encrypt_state = DirectionState::Active;
}
pub fn start_decrypting(&mut self) {
debug_assert!(self.decrypt_state == DirectionState::Prepared);
self.decrypt_state = DirectionState::Active;
}
pub fn set_message_encrypter(&mut self, cipher: Box<dyn MessageEncrypter>) {
self.prepare_message_encrypter(cipher);
self.start_encrypting();
}
pub fn set_message_decrypter(&mut self, cipher: Box<dyn MessageDecrypter>) {
self.prepare_message_decrypter(cipher);
self.start_decrypting();
}
pub fn wants_close_before_decrypt(&self) -> bool {
self.read_seq == SEQ_SOFT_LIMIT
}
pub fn wants_close_before_encrypt(&self) -> bool {
self.write_seq == SEQ_SOFT_LIMIT
}
pub fn encrypt_exhausted(&self) -> bool {
self.write_seq >= SEQ_HARD_LIMIT
}
pub fn decrypt_incoming(&mut self, encr: Message) -> Result<Message, TLSError> {
debug_assert!(self.decrypt_state == DirectionState::Active);
let seq = self.read_seq;
self.read_seq += 1;
self.message_decrypter.decrypt(encr, seq)
}
pub fn encrypt_outgoing(&mut self, plain: BorrowMessage) -> Message {
debug_assert!(self.encrypt_state == DirectionState::Active);
assert!(!self.encrypt_exhausted());
let seq = self.write_seq;
self.write_seq += 1;
self.message_encrypter.encrypt(plain, seq).unwrap()
}
}