forked from rusticata/tls-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls_states.rs
142 lines (130 loc) · 7.18 KB
/
tls_states.rs
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
141
142
use crate::tls::*;
use crate::tls_alert::TlsAlertSeverity;
#[derive(Debug, PartialEq, Eq)]
/// Error types for the state machine
pub enum StateChangeError {
InvalidTransition,
ParseError,
}
/// TLS machine possible states
#[derive(Copy, Clone, Debug, PartialEq, Hash)]
pub enum TlsState {
None,
ClientHello,
AskResumeSession,
ResumeSession,
ServerHello,
Certificate,
CertificateSt,
ServerKeyExchange,
ServerHelloDone,
ClientKeyExchange,
ClientChangeCipherSpec,
CRCertRequest,
CRHelloDone,
CRCert,
CRClientKeyExchange,
CRCertVerify,
NoCertSKE,
NoCertHelloDone,
NoCertCKE,
PskHelloDone,
PskCKE,
SessionEncrypted,
Alert,
Finished,
Invalid,
}
impl Default for TlsState {
fn default() -> Self {
TlsState::None
}
}
#[rustfmt::skip]
fn tls_state_transition_handshake(state: TlsState, msg: &TlsMessageHandshake, to_server:bool) -> Result<TlsState,StateChangeError> {
match (state,msg,to_server) {
(TlsState::None, &TlsMessageHandshake::ClientHello(ref msg), true) => {
match msg.session_id {
Some(_) => Ok(TlsState::AskResumeSession),
_ => Ok(TlsState::ClientHello)
}
},
// Server certificate
(TlsState::ClientHello, &TlsMessageHandshake::ServerHello(_), false) => Ok(TlsState::ServerHello),
(TlsState::ServerHello, &TlsMessageHandshake::Certificate(_), false) => Ok(TlsState::Certificate),
// Server certificate, no client certificate requested
(TlsState::Certificate, &TlsMessageHandshake::ServerKeyExchange(_), false) => Ok(TlsState::ServerKeyExchange),
(TlsState::Certificate, &TlsMessageHandshake::CertificateStatus(_), false) => Ok(TlsState::CertificateSt),
(TlsState::CertificateSt, &TlsMessageHandshake::ServerKeyExchange(_), false) => Ok(TlsState::ServerKeyExchange),
(TlsState::ServerKeyExchange,&TlsMessageHandshake::ServerDone(_), false) => Ok(TlsState::ServerHelloDone),
(TlsState::ServerHelloDone ,&TlsMessageHandshake::ClientKeyExchange(_), true) => Ok(TlsState::ClientKeyExchange),
// Server certificate, client certificate requested
(TlsState::Certificate, &TlsMessageHandshake::CertificateRequest(_), false)=> Ok(TlsState::CRCertRequest),
(TlsState::ServerKeyExchange,&TlsMessageHandshake::CertificateRequest(_), false)=> Ok(TlsState::CRCertRequest),
(TlsState::CRCertRequest, &TlsMessageHandshake::ServerDone(_), false) => Ok(TlsState::CRHelloDone),
(TlsState::CRHelloDone, &TlsMessageHandshake::Certificate(_), true) => Ok(TlsState::CRCert),
(TlsState::CRCert, &TlsMessageHandshake::ClientKeyExchange(_), true) => Ok(TlsState::CRClientKeyExchange),
(TlsState::CRClientKeyExchange, &TlsMessageHandshake::CertificateVerify(_), _) => Ok(TlsState::CRCertVerify),
// Server has no certificate (but accepts anonymous)
(TlsState::ServerHello, &TlsMessageHandshake::ServerKeyExchange(_), false) => Ok(TlsState::NoCertSKE),
(TlsState::NoCertSKE, &TlsMessageHandshake::ServerDone(_), false) => Ok(TlsState::NoCertHelloDone),
(TlsState::NoCertHelloDone, &TlsMessageHandshake::ClientKeyExchange(_), true) => Ok(TlsState::NoCertCKE),
// PSK
(TlsState::Certificate, &TlsMessageHandshake::ServerDone(_), false) => Ok(TlsState::PskHelloDone),
(TlsState::PskHelloDone, &TlsMessageHandshake::ClientKeyExchange(_), true) => Ok(TlsState::PskCKE),
// Resuming session
(TlsState::AskResumeSession, &TlsMessageHandshake::ServerHello(_), false) => Ok(TlsState::ResumeSession),
// Resume session failed
(TlsState::ResumeSession, &TlsMessageHandshake::Certificate(_), false) => Ok(TlsState::Certificate),
// TLS 1.3 Draft 18 1-RTT
// Re-use the ClientChangeCipherSpec state to indicate the next message will be encrypted
(TlsState::ClientHello, &TlsMessageHandshake::ServerHelloV13Draft18(_), false) => Ok(TlsState::ClientChangeCipherSpec),
// Hello requests must be accepted at any time (except start), but ignored [RFC5246] 7.4.1.1
(TlsState::None, &TlsMessageHandshake::HelloRequest, _) => Err(StateChangeError::InvalidTransition),
(s, &TlsMessageHandshake::HelloRequest, _) => Ok(s),
// NewSessionTicket message (after CCS)
(TlsState::ClientChangeCipherSpec, &TlsMessageHandshake::NewSessionTicket(_), false) => Ok(TlsState::ClientChangeCipherSpec),
// All other transitions are considered invalid
_ => Err(StateChangeError::InvalidTransition),
}
}
/// Update the TLS state machine, doing one transition
///
/// Given the previous state and the parsed message, return the new state or a state machine error.
///
/// This state machine only implements the TLS handshake.
///
/// Some transitions only check the new message type, while some others must match the content
/// (for example, to check if the client asked to resume a session).
///
/// If the previous state is `Invalid`, the state machine will not return an error, but keep the
/// same `Invalid` state. This is used to raise error only once if the state machine keeps being
/// updated by new messages.
#[rustfmt::skip]
pub fn tls_state_transition(state: TlsState, msg: &TlsMessage, to_server:bool) -> Result<TlsState,StateChangeError> {
match (state,msg,to_server) {
(TlsState::Invalid,_,_) => Ok(TlsState::Invalid),
(TlsState::SessionEncrypted,_, _) => Ok(TlsState::SessionEncrypted),
(TlsState::Finished,_,_) => Ok(TlsState::Invalid),
(_,&TlsMessage::Handshake(ref m),_) => tls_state_transition_handshake(state,m,to_server),
// Server certificate
(TlsState::ClientKeyExchange, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
(TlsState::ClientChangeCipherSpec,&TlsMessage::ChangeCipherSpec, false) => Ok(TlsState::SessionEncrypted),
// Server certificate, client certificate requested
(TlsState::CRClientKeyExchange, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
(TlsState::CRCertVerify, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
// No server certificate
(TlsState::NoCertCKE, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
// PSK
(TlsState::PskCKE, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
// Resume session
(TlsState::ResumeSession, &TlsMessage::ChangeCipherSpec, _) => Ok(TlsState::ClientChangeCipherSpec),
// 0-rtt
(TlsState::AskResumeSession, &TlsMessage::ChangeCipherSpec, true) => Ok(TlsState::AskResumeSession),
// non-fatal alerts
(s, &TlsMessage::Alert(ref a), _) => {
if a.severity == TlsAlertSeverity::Warning { Ok(s) } else { Ok(TlsState::Finished) }
},
(_,_,_) => Err(StateChangeError::InvalidTransition),
}
}