Debian Codegraph @

crt/s2n/tests/saw/spec/handshake/rfc_handshake_tls12.cry

raw
1 // This module provides an specification of the TLS 1.2 handshake message
2 // state machine according to RFCs 5246, 5077 and 6066 (OCSP extension only).
3 // The NPN extension is based on the draft
4 // https://datatracker.ietf.org/doc/html/draft-agl-tls-nextprotoneg-03
5
6
7 module rfc_handshake_tls12 where
8
9 import s2n_handshake_io
10
11 // Main correctness property: a simulation of the S2N handshake state
12 // machine by an RFC-derived state machine. Every pair of related initial
13 // 'connection' and 'Parameters' generates the same sequence of messages.
14 tls12rfcSimulatesS2N : {len} (fin len) => connection -> Parameters -> Bool
15 tls12rfcSimulatesS2N conn pars =
16 initial_connection conn /\ connectionParameters conn pars ==>
17 map rfc2S2N (traceRFC `{n = len} pars) == traceS2N `{n = len} conn
18
19 // Generate a sequence of states for the the RFC handshake state machine given
20 // handshake parameters
21 traceRFC : {n} (fin n) => Parameters -> [n]Message
22 traceRFC params = take (map message (iterate (handshakeTransition params) clientHelloSent))
23
24 // RFC Handshake states
25 type State = [5]
26
27 // Handshake states
28 (helloRequestSent : State) = 0
29 (clientHelloSent : State) = 1
30 (serverHelloSent : State) = 2
31 (serverCertificateSent : State) = 3
32 (serverKeyExchangeSent : State) = 4
33 (certificateRequestSent : State) = 5
34 (serverHelloDoneSent : State) = 6
35 (serverHelloDoneSentCertRequested : State) = 7
36 (clientCertificateSent : State) = 8
37 (clientKeyExchangeSent : State) = 9
38 (clientKeyExchangeSentCertSent : State) = 10
39 (certificateVerifySent : State) = 11
40 (clientChangeCipherSpecSent : State) = 12
41 (clientFinishedSent : State) = 13
42 (serverChangeCipherSpecSent : State) = 14
43 (serverFinishedSent : State) = 15
44 (applicationDataTransmission : State) = 16
45 (serverNewSessionTicketSent : State) = 17
46 (serverRenewSessionTicketSent : State) = 18
47 (serverChangeCipherSpecWithTicketSent : State) = 19
48 (serverFinishedWithTicketSent : State) = 20
49 (clientChangeCipherSpecWithTicketSent : State) = 21
50 (clientFinishedWithTicketSent : State) = 22
51 (serverCertificateStatusSent : State) = 23
52 (nextProtocolSent : State) = 24
53 (nextProtocolWithTicketSent : State) = 25
54
55 // TLS protocol types
56 type Protocol = [2]
57 (handshake : Protocol) = 0
58 (changeCipherSpec : Protocol) = 1
59 (alert : Protocol) = 2
60 (data : Protocol) = 3
61
62 // TLS Message types
63 type MessageType = [4]
64 //handshake message types
65 (helloRequest : MessageType) = 0
66 (clientHello : MessageType) = 1
67 (serverHello : MessageType) = 2
68 (certificate : MessageType) = 3
69 (serverKeyExchange : MessageType) = 4
70 (certificateRequest : MessageType) = 5
71 (serverHelloDone : MessageType) = 6
72 (certificateVerify : MessageType) = 7
73 (clientKeyExchange : MessageType) = 8
74 (finished : MessageType) = 9
75 (newSessionTicket : MessageType) = 10
76 (certificateStatus : MessageType) = 11
77 (nextProtocol : MessageType) = 12
78
79 // ChangeCipherSpec message types
80 (changeCipherSpecMessage : MessageType) = 0
81
82 // Data message types
83 (applicationData : MessageType) = 0
84 (error : MessageType) = 1
85
86 // Sender (data flow direction)
87 type Sender = [2]
88 (server : Sender) = 0
89 (client : Sender) = 1
90 (both : Sender) = 2
91 (noSender : Sender) = 3
92
93 // Key exchange algorithms
94 type KeyExchange = [3]
95 (DH_anon : KeyExchange) = 0
96 (DHE_DSS : KeyExchange) = 1
97 (DHE_RSA : KeyExchange) = 2
98 (RSA : KeyExchange) = 3
99 (DH_DSS : KeyExchange) = 4
100 (DH_RSA : KeyExchange) = 5
101
102 // TLS protocol parameters that determine the transition to take in each state.
103 // Some of these fields are set as a configuration to the end-point, while others
104 // are determined from the messages send by the counterparty. We model both as
105 // parameters since the state machine model reasons about both end-points.
106 type Parameters = {keyExchange : KeyExchange
107 //^ Negotiated key exchange algorithm
108 ,sessionTicket : Bit
109 //^ The client had a session ticket
110 ,renewSessionTicket : Bit
111 //^ Whether the server decides to renew a session ticket
112 ,sendCertificateStatus : Bit
113 //^ Whether the server decides to send the certificate status
114 // message
115 ,requestClientCert : Bit
116 //^ Whether the server requests a certificate from the client
117 ,includeSessionTicket : Bit
118 //^ Whether the server includes a session ticket extension in
119 // SERVER_HELLO
120 ,npnNegotiated : Bit
121 //^ Whether the client sends a ``NextProtocol'' message.
122 // This allows the application layer to negotiate which
123 // protocol should be performed over the secure connection.
124 }
125
126 // A relation between the s2n_connection struct and the parameters
127 connectionParameters : connection -> Parameters -> Bit
128 connectionParameters conn params =
129 conn.server_can_send_ocsp == params.sendCertificateStatus
130 /\ ((conn.key_exchange_eph /\ ~keyExchangeNonEphemeral params /\ params.keyExchange != DH_anon) \/
131 (~conn.key_exchange_eph /\ keyExchangeNonEphemeral params))
132 /\ (conn.is_caching_enabled /\ ~conn.resume_from_cache) == params.sessionTicket
133 /\ (~params.includeSessionTicket) // s2n server does not issue tickets at this time
134 /\ (~params.renewSessionTicket) // s2n server does not issue tickets at this time
135 /\ conn.client_auth_flag == params.requestClientCert
136 /\ conn.npn_negotiated == params.npnNegotiated
137 /\ conn.actual_protocol_version != S2N_TLS13
138
139 // A predicate that tells whether key-exchange is non-ephemeral and uses server certificate
140 // data for exchanging a premaster secret (RFC 5246 7.4.3). In this case the key exchange
141 // algorithm does not require KeyExchange messages.
142 keyExchangeNonEphemeral : Parameters -> Bit
143 keyExchangeNonEphemeral params = params.keyExchange == RSA \/ params.keyExchange == DH_DSS \/ params.keyExchange == DH_RSA
144
145 // Handshake state transition relation per the RFCs. Given handshake parameters
146 // and a handshake state, return the next state. If there is no valid next state,
147 // the state machine stutters (returns the previous state).
148 handshakeTransition : Parameters -> State -> State
149 handshakeTransition params old =
150 snd (find fst (True, old) [ (old == from /\ p, to) | (from, p, to) <- valid_transitions])
151 where // The encoding of the transition relation of the state machine is a triple
152 // (oldState, transitionCondition, newState) with the meaning "if we are
153 // in the oldState, the transitionCondition is True (for the given
154 // Parameters) then the next state is newState.
155 valid_transitions =
156 [(helloRequestSent, True, clientHelloSent)
157
158 ,(clientHelloSent, True, serverHelloSent)
159
160 ,(serverHelloSent, params.keyExchange != DH_anon /\ ~params.sessionTicket, serverCertificateSent)
161 ,(serverHelloSent, params.keyExchange == DH_anon /\ ~params.sessionTicket, serverKeyExchangeSent)
162 ,(serverHelloSent, params.sessionTicket /\ params.renewSessionTicket, serverNewSessionTicketSent)
163 ,(serverHelloSent, params.sessionTicket /\ ~params.renewSessionTicket, serverChangeCipherSpecWithTicketSent)
164
165 ,(serverCertificateSent, keyExchangeNonEphemeral params /\ ~params.requestClientCert /\ ~params.sendCertificateStatus, serverHelloDoneSent)
166 ,(serverCertificateSent, keyExchangeNonEphemeral params /\ params.requestClientCert /\ ~params.sendCertificateStatus, certificateRequestSent)
167 ,(serverCertificateSent, ~(keyExchangeNonEphemeral params) /\ ~params.sendCertificateStatus, serverKeyExchangeSent)
168 ,(serverCertificateSent, params.sendCertificateStatus, serverCertificateStatusSent)
169
170 ,(serverKeyExchangeSent, ~params.requestClientCert, serverHelloDoneSent)
171 ,(serverKeyExchangeSent, params.requestClientCert, certificateRequestSent)
172
173 ,(certificateRequestSent, True, serverHelloDoneSentCertRequested)
174
175 ,(serverHelloDoneSent, True, clientKeyExchangeSent)
176
177 ,(clientKeyExchangeSent, True, clientChangeCipherSpecSent)
178
179 ,(serverHelloDoneSentCertRequested, True, clientCertificateSent)
180
181 ,(clientCertificateSent, True, clientKeyExchangeSentCertSent)
182
183 ,(clientKeyExchangeSentCertSent, True, certificateVerifySent)
184
185 ,(certificateVerifySent, True, clientChangeCipherSpecSent)
186
187 ,(clientChangeCipherSpecSent, ~params.npnNegotiated, clientFinishedSent)
188 ,(clientChangeCipherSpecSent, params.npnNegotiated, nextProtocolSent)
189
190 ,(nextProtocolSent, True, clientFinishedSent)
191
192 ,(clientFinishedSent, ~params.includeSessionTicket, serverChangeCipherSpecSent)
193 ,(clientFinishedSent, params.includeSessionTicket, serverNewSessionTicketSent)
194
195 ,(serverNewSessionTicketSent, True, serverChangeCipherSpecSent)
196
197 ,(serverChangeCipherSpecSent, True, serverFinishedSent)
198
199 ,(serverFinishedSent, True, applicationDataTransmission)
200
201 ,(serverChangeCipherSpecWithTicketSent, True, serverFinishedWithTicketSent)
202
203 ,(serverFinishedWithTicketSent, True, clientChangeCipherSpecWithTicketSent)
204
205 ,(clientChangeCipherSpecWithTicketSent, ~params.npnNegotiated, clientFinishedWithTicketSent)
206 ,(clientChangeCipherSpecWithTicketSent, params.npnNegotiated, nextProtocolWithTicketSent)
207 ,(nextProtocolWithTicketSent, True, clientFinishedWithTicketSent)
208
209 ,(clientFinishedWithTicketSent, True, applicationDataTransmission)
210
211 ,(serverCertificateStatusSent, keyExchangeNonEphemeral params /\ ~params.requestClientCert, serverHelloDoneSent)
212 ,(serverCertificateStatusSent, keyExchangeNonEphemeral params /\ params.requestClientCert, certificateRequestSent)
213 ,(serverCertificateStatusSent, ~(keyExchangeNonEphemeral params), serverKeyExchangeSent)
214 ]
215
216 // Abstraction of the TLS message which records the protocol, message type and
217 // direction
218 type Message = {messageType : MessageType
219 ,protocol : Protocol
220 ,sender : Sender
221 }
222
223 mkMessage : Sender -> Protocol -> MessageType -> Message
224 mkMessage s p mt = {sender = s, protocol = p, messageType = mt}
225
226 // Message sent in each handshake state
227 message : State -> Message
228 message = lookupDefault messages (mkMessage noSender data error)
229 where messages =
230 [(helloRequestSent, mkMessage server handshake helloRequest)
231 ,(clientHelloSent, mkMessage client handshake clientHello)
232 ,(serverHelloSent, mkMessage server handshake serverHello)
233 ,(serverCertificateSent, mkMessage server handshake certificate)
234 ,(serverKeyExchangeSent, mkMessage server handshake serverKeyExchange)
235 ,(certificateRequestSent, mkMessage server handshake certificateRequest)
236 ,(serverHelloDoneSent, mkMessage server handshake serverHelloDone)
237 ,(serverHelloDoneSentCertRequested, mkMessage server handshake serverHelloDone)
238 ,(clientCertificateSent, mkMessage client handshake certificate)
239 ,(clientKeyExchangeSent, mkMessage client handshake clientKeyExchange)
240 ,(clientKeyExchangeSentCertSent, mkMessage client handshake clientKeyExchange)
241 ,(certificateVerifySent, mkMessage client handshake certificateVerify)
242 ,(clientChangeCipherSpecSent, mkMessage client changeCipherSpec changeCipherSpecMessage)
243 ,(clientFinishedSent, mkMessage client handshake finished)
244 ,(serverChangeCipherSpecSent, mkMessage server changeCipherSpec changeCipherSpecMessage)
245 ,(serverFinishedSent, mkMessage server handshake finished)
246 ,(applicationDataTransmission, mkMessage both data applicationData)
247 ,(serverNewSessionTicketSent, mkMessage server handshake newSessionTicket)
248 ,(serverChangeCipherSpecWithTicketSent, mkMessage server changeCipherSpec changeCipherSpecMessage)
249 ,(serverFinishedWithTicketSent, mkMessage server handshake finished)
250 ,(clientChangeCipherSpecWithTicketSent, mkMessage client changeCipherSpec changeCipherSpecMessage)
251 ,(clientFinishedWithTicketSent, mkMessage client handshake finished)
252 ,(serverCertificateStatusSent, mkMessage server handshake certificateStatus)
253 ,(nextProtocolSent, mkMessage client handshake nextProtocol)
254 ,(nextProtocolWithTicketSent, mkMessage client handshake nextProtocol)
255 ]
256
257 // a mapping from RFC messages to s2n handshake_action's
258 rfc2S2N : Message -> handshake_action
259 rfc2S2N msg = {writer = sender2writer msg.sender
260 ,record_type = protocol2recordType msg.protocol
261 ,message_type = s2nMessageType msg
262 }
263
264 // an equality relation between RFC and s2n messages
265 msgRFCS2N : Message -> handshake_action -> Bool
266 msgRFCS2N msg hsa = rfc2S2N msg == hsa
267
268 // convert a specification representation of sender to a 'writer' character in s2n.
269 sender2writer : Sender -> [8]
270 sender2writer sndr = if sndr == server then 'S'
271 else if sndr == client then 'C'
272 else if sndr == both then 'B'
273 else 'E'
274
275 // Convert the RFC protocol number to s2n TLS record type constant
276 protocol2recordType : Protocol -> [8]
277 protocol2recordType = lookupDefault protocols TLS_APPLICATION_DATA
278 where protocols = [(handshake, TLS_HANDSHAKE)
279 ,(changeCipherSpec, TLS_CHANGE_CIPHER_SPEC)
280 ,(alert, TLS_ALERT)
281 ,(data, TLS_APPLICATION_DATA)
282 ]
283
284 // Returns the s2n message type for a given RFC Message
285 s2nMessageType : Message -> [8]
286 s2nMessageType msg =
287 if prot == handshake then (if mt == helloRequest then TLS_HELLO_REQUEST
288 else if mt == clientHello then TLS_CLIENT_HELLO
289 else if mt == serverHello then TLS_SERVER_HELLO
290 else if mt == certificate then TLS_CERTIFICATE
291 else if mt == serverKeyExchange then TLS_SERVER_KEY
292 else if mt == certificateRequest then TLS_CERTIFICATE_REQ
293 else if mt == serverHelloDone then TLS_SERVER_HELLO_DONE
294 else if mt == certificateVerify then TLS_CERT_VERIFY
295 else if mt == clientKeyExchange then TLS_CLIENT_KEY
296 else if mt == finished then TLS_FINISHED
297 else if mt == newSessionTicket then TLS_SERVER_NEW_SESSION_TICKET
298 else if mt == certificateStatus then TLS_CERTIFICATE_STATUS
299 else if mt == nextProtocol then TLS_NPN
300 else ERROR_MESSAGE)
301
302 else 0
303 where mt = msg.messageType
304 send = msg.sender
305 prot = msg.protocol
306
307 ERROR_MESSAGE = 10 // unused in current s2n