Debian Codegraph @

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

raw
1 ////////////////////////////////////////////////////////////////////////////
2 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License").
5 // You may not use this file except in compliance with the License.
6 // A copy of the License is located at
7 //
8 // http://aws.amazon.com/apache2.0
9 //
10 // or in the "license" file accompanying this file. This file is distributed
11 // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 // express or implied. See the License for the specific language governing
13 // permissions and limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////
16 // This module provides a specification of the TLS 1.3 handshake message
17 // state machine according to RFC 8446.
18 ////////////////////////////////////////////////////////////////////////////
19
20 module rfc_handshake_tls13 where
21
22 import s2n_handshake_io
23
24 /**
25 * True if the given Parameters do not match the given connection, or if
26 * the sequence of handshake messages produced by s2n matches the sequence
27 * produced by the cryptol RFC implementation in this file.
28 *
29 * Why is this True if the Parameters don't match the connection? We prove
30 * this function for all possible connection and Parameter inputs. Inputs
31 * where the connection doesn't match the Parameters should be ignored as
32 * invalid instead of causing the proof to fail. This is basically equivalent
33 * to generating the Parameters from the connection, except theoretically
34 * one connection could correspond to multiple Parameters.
35 */
36 tls13rfcSimulatesS2N : {len} (fin len, len >= 1) => connection -> Parameters -> Bit
37 tls13rfcSimulatesS2N conn params =
38 (initial_connection conn /\ connectionParameters conn params) ==>
39 tracesMatchExcludingWaits
40 (map rfc2S2N (doHandshake`{len} params))
41 (traceS2N`{len} conn)
42
43 type MessageType = [4]
44 (applicationData : MessageType) = 0
45 (clientHello : MessageType) = 1
46 (helloRetryRequest : MessageType) = 2
47 (serverHello : MessageType) = 3
48 (encryptedExtensions : MessageType) = 4
49 (certificateRequest : MessageType) = 5
50 (certificate : MessageType) = 6
51 (certificateVerify : MessageType) = 7
52 (finished : MessageType) = 8
53 (endOfEarlyData : MessageType) = 9
54 (changeCipherSpec : MessageType) = 10
55 (error : MessageType) = 12
56
57 type Sender = [2]
58 (server : Sender) = 0
59 (client : Sender) = 1
60 (both : Sender) = 2
61
62 type Message = {
63 messageType : MessageType,
64 sender : Sender
65 }
66
67 type Parameters = {
68 compat_mode : [2],
69 psk_mode : Bit,
70 retry : Bit,
71 client_auth : Bit,
72 no_client_cert : Bit,
73 zero_rtt : Bit,
74 early_ccs : Bit
75 }
76
77 /**
78 * True if the given Parameters are equivalent to the given s2n connection.
79 * As features are added to s2n, update the conn_* variables to indicate
80 * whether the feature is active for a given connection.
81 */
82 connectionParameters : connection -> Parameters -> Bit
83 connectionParameters conn params = params.psk_mode == conn_psk_mode
84 /\ params.retry == conn_retry
85 /\ params.client_auth == (conn_client_auth /\ ~params.psk_mode)
86 /\ params.no_client_cert == (conn_no_client_cert /\ params.client_auth)
87 /\ params.zero_rtt == (conn_zero_rtt /\ params.psk_mode)
88 /\ params.compat_mode ==
89 [ when_middlebox_compat (conn.mode != S2N_CLIENT)
90 , when_middlebox_compat (conn.mode == S2N_CLIENT) ]
91 /\ params.early_ccs == conn_early_ccs
92 /\ conn.actual_protocol_version == S2N_TLS13
93 where conn_psk_mode = ~conn.chosen_psk_null
94 conn_retry = (conn.handshake.handshake_type && HELLO_RETRY_REQUEST) != zero
95 conn_client_auth = conn.client_auth_flag
96 conn_no_client_cert = conn.no_client_cert
97 conn_early_ccs = (conn.handshake.handshake_type && EARLY_CLIENT_CCS) != zero
98 conn_zero_rtt = (conn.early_data_state == S2N_EARLY_DATA_ACCEPTED)
99
100 conn_middlebox_compat = ~conn.quic_enabled \/ ((conn.handshake.handshake_type && MIDDLEBOX_COMPAT) != zero)
101 when_middlebox_compat b = conn_middlebox_compat /\ b
102
103 type Action = {
104 message : Message,
105 valid : Bit
106 }
107
108 type StateId = [5]
109 type State = {
110 actions : [5]Action,
111 id : StateId,
112 next : StateId
113 }
114
115 /**
116 * Represents the sequence of the first n handshake messages for given
117 * Parameters.
118 *
119 * Procedural Translation: The handshake starts as n applicationData
120 * messages and index==0. For the first n States in the infinite series
121 * of states returned by 'stateMachine', loop over the state's list of
122 * actions. While index < n, for any Action with valid==True, add the
123 * Action's message to the handshake at 'index' and then increment 'index'.
124 */
125 doHandshake : {n} (fin n, n >= 1) => Parameters -> [n]Message
126 doHandshake params = (foldl doState initialHandshake (take`{n} (stateMachine params))).messages
127 where doState hs state = foldl doAction hs state.actions
128 doAction hs action = if hs.index < `n /\ action.valid
129 then updateHandshake hs action.message
130 else hs
131 updateHandshake hs value = {
132 messages = update hs.messages hs.index value,
133 index = hs.index + 1
134 }
135 initialHandshake = {
136 messages = repeat { messageType = applicationData, sender = both },
137 index = 0:[width n]
138 }
139
140 /*
141 * State machine based on RFC 8446, specifically:
142 * - The basic state machines in Appendix A
143 * - The middlebox compatability rules in Appendix D.4
144 *
145 * "*" -> Indicates optional or situation-dependent
146 * messages that are not always sent.
147 *
148 * +------------------+
149 * |CLIENT_START_STATE|
150 * |------------------|
151 * |clientHello (C) |
152 * |* CCS (C) |
153 * +------------------+
154 * | |
155 * | | if retry
156 * | v
157 * | +---------------------+
158 * | | HELLO_RETRY_STATE |
159 * if not retry | |---------------------|
160 * | |helloRetryRequest (S)|
161 * | |* CCS (S) |
162 * | |* CCS (C) |
163 * | |clientHello (C) |
164 * | +---------------------+
165 * | |
166 * v v
167 * +-----------------------+
168 * | SERVER_HELLO_STATE |
169 * |-----------------------|
170 * |serverHello (S) |
171 * |* CCS (S) |
172 * |encryptedExtensions (S)|
173 * +-----------------------+
174 * | |
175 * | | if not psk
176 * | v
177 * | +------------------------+
178 * | | SERVER_AUTH_STATE |
179 * if psk | |------------------------|
180 * | |* certificateRequest (S)|
181 * | |certificate (S) |
182 * | |certificateVerify (S) |
183 * | +------------------------+
184 * | |
185 * v v
186 * +--------------------+
187 * |SERVER_FINISH_STATE |
188 * |--------------------|
189 * |finished (S) |
190 * |* CCS (C) |
191 * |* endOfEarlyData (C)|
192 * +--------------------+
193 * | |
194 * | | if client_auth
195 * | v
196 * | +-----------------------+
197 * | | CLIENT_AUTH_STATE |
198 * if not client_auth | |-----------------------|
199 * | |certificate (C) |
200 * | |* certificateVerify (C)|
201 * | +-----------------------+
202 * | |
203 * v v
204 * +---------------------+
205 * |CLIENT_FINISHED_STATE|
206 * |---------------------|
207 * |finished (C) |
208 * +---------------------+
209 * |
210 * v
211 * +-------------------+
212 * | FINISHED |
213 * |-------------------|<---+
214 * |applicationData (B)| |
215 * +---------+---------+ |
216 * | |
217 * +--------------+
218 */
219 /**
220 * Represents the infinite sequence of states that make up the state
221 * machine for given Parameters.
222 *
223 * Procedural Translation: Starting with CLIENT_START_STATE, add the
224 * State associated with the 'next' field to the sequence and then
225 * repeat for that State. Eventually, we will reach FINISHED_STATE and
226 * loop on it indefinitely.
227 */
228 stateMachine : Parameters -> [inf]State
229 stateMachine params = iterate (\s -> stateFor s.next) CLIENT_START_STATE
230 where stateFor id = find (\s -> s.id == id) ERROR_STATE states
231 states = [
232 CLIENT_START_STATE, HELLO_RETRY_STATE, SERVER_HELLO_STATE,
233 SERVER_AUTH_STATE, SERVER_FINISH_STATE, CLIENT_AUTH_STATE,
234 CLIENT_FINISH_STATE, FINISHED_STATE
235 ]
236
237 state : {n} (fin n, n <= 5) => StateId -> [n]Action -> State -> State
238 state id actions next = { actions = actions # zero, id = id, next = next.id }
239 action mt s v = { message = { messageType = mt, sender = s }, valid = v }
240 compatMode mode = params.compat_mode @ mode
241
242 ERROR_STATE = state 0 [
243 action error both True
244 ] ERROR_STATE
245
246 CLIENT_START_STATE = state 1 [
247 action clientHello client True,
248 action changeCipherSpec client (params.early_ccs /\ compatMode client)
249 ] (if params.retry then HELLO_RETRY_STATE else SERVER_HELLO_STATE)
250
251 HELLO_RETRY_STATE = state 2 [
252 action helloRetryRequest server True,
253 action changeCipherSpec server (compatMode server),
254 action changeCipherSpec client (~params.early_ccs /\ compatMode client),
255 action clientHello client True
256 ] SERVER_HELLO_STATE
257
258 SERVER_HELLO_STATE = state 3 [
259 action serverHello server True,
260 action changeCipherSpec server (~params.retry /\ compatMode server),
261 action encryptedExtensions server True
262 ] (if params.psk_mode then SERVER_FINISH_STATE else SERVER_AUTH_STATE)
263
264 SERVER_AUTH_STATE = state 4 [
265 action certificateRequest server params.client_auth,
266 action certificate server True,
267 action certificateVerify server True
268 ] SERVER_FINISH_STATE
269
270 SERVER_FINISH_STATE = state 5 [
271 action finished server True,
272 action changeCipherSpec client (~params.retry /\ ~params.early_ccs /\ compatMode client),
273 action endOfEarlyData client params.zero_rtt
274 ] (if params.client_auth then CLIENT_AUTH_STATE else CLIENT_FINISH_STATE)
275
276 CLIENT_AUTH_STATE = state 6 [
277 action certificate client True,
278 action certificateVerify client (~params.no_client_cert)
279 ] CLIENT_FINISH_STATE
280
281 CLIENT_FINISH_STATE = state 7 [
282 action finished client True
283 ] FINISHED_STATE
284
285 FINISHED_STATE = state 8 [
286 action applicationData both True
287 ] FINISHED_STATE
288
289 /**
290 * The s2n 'handshake_action' that corresponds to the Message produced
291 * by this cryptol implementation. Necessary to compare results from the
292 * s2n and RFC cryptol implementations.
293 */
294 rfc2S2N : Message -> handshake_action
295 rfc2S2N msg = mkAct recordType messageType writer
296 where recordType = if msg.messageType == changeCipherSpec then TLS_CHANGE_CIPHER_SPEC
297 | msg.messageType == applicationData then TLS_APPLICATION_DATA
298 | msg.messageType == error then TLS_ALERT
299 else TLS_HANDSHAKE
300 messageType = if recordType != TLS_HANDSHAKE then noMessageType
301 | msg.messageType == clientHello then TLS_CLIENT_HELLO
302 | msg.messageType == serverHello then TLS_SERVER_HELLO
303 | msg.messageType == helloRetryRequest then TLS_SERVER_HELLO
304 | msg.messageType == encryptedExtensions then TLS_ENCRYPTED_EXTENSIONS
305 | msg.messageType == certificateRequest then TLS_CERTIFICATE_REQ
306 | msg.messageType == certificate then TLS_CERTIFICATE
307 | msg.messageType == certificateVerify then TLS_CERT_VERIFY
308 | msg.messageType == finished then TLS_FINISHED
309 | msg.messageType == endOfEarlyData then TLS_END_OF_EARLY_DATA
310 else noMessageType
311 writer = if msg.sender == server then 'S'
312 | msg.sender == client then 'C'
313 | msg.sender == both then 'B'
314 else '!'
315 noMessageType = TLS_HELLO_REQUEST // HelloRequests don't exist in tls1.3
316
317 /**
318 * printHandshake and s2nToWords are two helper functions for debugging cryptol
319 * Take the counterexample from tls13rfcSimulatesS2N and plug the connection and
320 * parameter values into testParameters and testConnection. Run printHandshake
321 * with ascii set to on.
322 * $ :s ascii = on
323 * $ printHandshake`{16}
324 * Note with ascii on, you may see actual_protocol_version = '"'. This is because
325 * decimal value 34 (hex value 22) == asci character double quotes.
326 */
327
328 testParameters : Parameters
329 testParameters = {
330 compat_mode = [False, False],
331 psk_mode = False,
332 retry = False,
333 client_auth = False,
334 no_client_cert = False,
335 zero_rtt = False,
336 early_ccs = False
337 }
338
339 testConnection : connection
340 testConnection = {
341 handshake = {handshake_type = 0x00000000,
342 message_number = 0x00000000,
343 state_machine = 0x00000000},
344 mode = 0x00000000,
345 corked_io = False,
346 corked = zero,
347 is_caching_enabled = False,
348 resume_from_cache = False,
349 server_can_send_ocsp = False,
350 key_exchange_eph = False,
351 client_auth_flag = False,
352 actual_protocol_version = 0x22,
353 no_client_cert = True,
354 early_data_state = zero,
355 chosen_psk_null = True,
356 quic_enabled = False,
357 npn_negotiated = False
358 }
359
360 type Character = [8]
361 type MessageName = [22]Character
362
363 printHandshake : {len} (fin len, len >= 1) => ([3]Character, [len]MessageName, [3]Character, [len]MessageName)
364 printHandshake = ("rfc", map s2nToWords (map rfc2S2N (doHandshake`{len} params)),
365 "s2n", map s2nToWords (traceS2N`{len} conn))
366 where (params : Parameters) = testParameters
367 (conn : connection) = testConnection
368
369 s2nToWords : handshake_action -> MessageName
370 s2nToWords action = name # [ '(', action.writer, ')']
371 where padded : {a, b} (fin a, fin b) => [b]Character -> [a+b]Character
372 padded msg = repeat ' ' # msg
373 name : [19]Character
374 name = if action.record_type == TLS_CHANGE_CIPHER_SPEC then padded "CCS"
375 | action.record_type == TLS_APPLICATION_DATA then padded "Data"
376 | action.message_type == TLS_CLIENT_HELLO then padded "ClientHello"
377 | action.message_type == TLS_SERVER_HELLO then padded "ServerHello"
378 | action.message_type == TLS_ENCRYPTED_EXTENSIONS then padded "EncryptedExtensions"
379 | action.message_type == TLS_CERTIFICATE_REQ then padded "CertRequest"
380 | action.message_type == TLS_CERTIFICATE then padded "Cert"
381 | action.message_type == TLS_CERT_VERIFY then padded "CertVerify"
382 | action.message_type == TLS_FINISHED then padded "Finished"
383 | action.message_type == TLS_END_OF_EARLY_DATA then padded "EndOfEarlyData"
384 else padded "Error"