| 1 |
// This module contains a high-level model of the cork-uncork state |
| 2 |
// machine. This state machine only tracks the current sender (client, |
| 3 |
// server or both) and is driven by the new value of the 'writer' field |
| 4 |
// of the handshake state machine state (the 'handshake_action'). |
| 5 |
// |
| 6 |
// It also contains a predicate that states the high-level model |
| 7 |
// simulates the lower-level model of s2n_advance_message in Cryptol |
| 8 |
// and is verified in the SAW proof script. |
| 9 |
// |
| 10 |
// Finally, the property we want to prove is about not corking or |
| 11 |
// uncorking a socket twice in a row. It is encoded in 'noDoubleCorkUncork'. |
| 12 |
|
| 13 |
module CorkUncork where |
| 14 |
|
| 15 |
import s2n_handshake_io |
| 16 |
|
| 17 |
type State = ([2], [2]) |
| 18 |
(clientSender : [2]) = 0 |
| 19 |
(serverSender : [2]) = 1 |
| 20 |
(appData : [2]) = 2 |
| 21 |
|
| 22 |
(corked : [2]) = 1 |
| 23 |
(uncorked : [2]) = 0 |
| 24 |
|
| 25 |
// For any two related 'connection' and corking 'State' they produce related |
| 26 |
// corking traces. |
| 27 |
// We only check handshakes up until the first APPLICATION_DATA. |
| 28 |
highLevelSimulatesLowLevel : {len} (fin len, len >= 2) => connection -> State -> Bit |
| 29 |
highLevelSimulatesLowLevel conn state = |
| 30 |
initial_connection conn /\ conn.mode == S2N_SERVER /\ conn.corked == uncorked /\ conn2state conn == state ==> |
| 31 |
go 0 |
| 32 |
where (conn_trace : [len]connection) = trace_advance_message `{n = len} conn |
| 33 |
writers = drop `{1} (map getWriter conn_trace) |
| 34 |
|
| 35 |
conn_trace_states : [len]State |
| 36 |
conn_trace_states = map conn2state conn_trace |
| 37 |
|
| 38 |
trace_corking_states : [len]State |
| 39 |
trace_corking_states = trace_corking `{n = len} state writers |
| 40 |
|
| 41 |
go : [width len] -> Bit |
| 42 |
go i = if i >= `len |
| 43 |
then True |
| 44 |
| (ACTIVE_STATE (conn_trace@i)).record_type == TLS_APPLICATION_DATA |
| 45 |
then (trace_corking_states@i).1 == uncorked |
| 46 |
else (conn_trace_states@i == trace_corking_states@i) /\ go (i+1) |
| 47 |
|
| 48 |
// Property of the high-level state machine: there is no uncorking or corking |
| 49 |
// twice in a row |
| 50 |
// This is not true for arbitrary sequences of writers, as for example a server |
| 51 |
// could transition S->A->S and cork twice. However, we do not have such |
| 52 |
// a sequence possible given the handshake sequences |
| 53 |
// We only check handshakes up until the first APPLICATION_DATA. |
| 54 |
noDoubleCorkUncork : {len} (fin len, len >= 2) => connection -> Bit |
| 55 |
noDoubleCorkUncork conn = |
| 56 |
initial_connection conn ==> go 0 |
| 57 |
where (conn_trace : [len]connection) = trace_advance_message `{n = len} conn |
| 58 |
writers = map getWriter conn_trace |
| 59 |
|
| 60 |
run_writers_corking_in_bounds : [len+1]Bit |
| 61 |
run_writers_corking_in_bounds = map corkingInBounds (run writers) |
| 62 |
|
| 63 |
go : [width len] -> Bit |
| 64 |
go i = if i >= `len then True |
| 65 |
| (ACTIVE_STATE (conn_trace@i)).record_type == TLS_APPLICATION_DATA then True |
| 66 |
else run_writers_corking_in_bounds@i /\ go (i+1) |
| 67 |
|
| 68 |
writer2sender : [8] -> [2] |
| 69 |
writer2sender w = if w == 'C' then clientSender else |
| 70 |
if w == 'S' then serverSender else |
| 71 |
appData |
| 72 |
|
| 73 |
corkedFromConn : connection -> [2] |
| 74 |
corkedFromConn conn = if mode_writer conn.mode == (ACTIVE_STATE conn).writer then 1 |
| 75 |
else 0 |
| 76 |
|
| 77 |
trace_advance_message : {n} (fin n) => connection -> [n]connection |
| 78 |
trace_advance_message conn = take (iterate s2nTrans conn) |
| 79 |
|
| 80 |
trace_corking : {n} (fin n, n >= 2) => State -> [n-1][8] -> [n]State |
| 81 |
trace_corking s writers = scanl cork_transition s writers |
| 82 |
|
| 83 |
getWriter : connection -> [8] |
| 84 |
getWriter conn = (ACTIVE_STATE conn).writer |
| 85 |
|
| 86 |
conn2state : connection -> State |
| 87 |
conn2state conn = (writer2sender (getWriter conn), conn.corked) |
| 88 |
|
| 89 |
// A (failing) simulation relation that omits the "only in server |
| 90 |
// mode" precondition. |
| 91 |
// We wrap this with negation to show that we can't do this proof without |
| 92 |
// the server-mode assumption |
| 93 |
// We only check handshakes up until the first APPLICATION_DATA. |
| 94 |
highLevelDoesNotSimulateLowLevel : {len} (fin len, len >= 2) => connection -> State -> Bit |
| 95 |
highLevelDoesNotSimulateLowLevel conn state = |
| 96 |
initial_connection conn /\ conn2state conn == state /\ conn.corked == uncorked ==> |
| 97 |
go 0 |
| 98 |
where (conn_trace : [len]connection) = trace_advance_message `{n = len} conn |
| 99 |
writers = drop `{1} (map getWriter conn_trace) |
| 100 |
|
| 101 |
conn_trace_states : [len]State |
| 102 |
conn_trace_states = map conn2state conn_trace |
| 103 |
|
| 104 |
trace_corking_states : [len]State |
| 105 |
trace_corking_states = trace_corking `{n = len} state writers |
| 106 |
|
| 107 |
go : [width len] -> Bit |
| 108 |
go i = if i >= `len |
| 109 |
then True |
| 110 |
| (ACTIVE_STATE (conn_trace@i)).record_type == TLS_APPLICATION_DATA |
| 111 |
then (trace_corking_states@i).1 == uncorked |
| 112 |
else (conn_trace_states@i == trace_corking_states@i) /\ go (i+1) |
| 113 |
|
| 114 |
// The abstract transition function for the cork/uncork state machine |
| 115 |
cork_transition : State -> [8] -> State |
| 116 |
cork_transition (s, corking) c = |
| 117 |
if (s == clientSender) then |
| 118 |
if c == 'C' then (clientSender, corking) |
| 119 |
| c == 'S' then (serverSender, corking + 1) |
| 120 |
else (appData, corking) |
| 121 |
else if s == serverSender then |
| 122 |
if c == 'C' then (clientSender, corking - 1) |
| 123 |
| c == 'S' then (serverSender, corking) |
| 124 |
else (appData, corking - 1) |
| 125 |
else (appData, corking) |
| 126 |
|
| 127 |
// generate a trace of states for the standard cork/uncork state // |
| 128 |
// machine (assumes socket is initially uncorked) given a list of input |
| 129 |
// actions (new writers) |
| 130 |
run : {n} [n][8] -> [n+1]State |
| 131 |
run writers = scanl cork_transition (clientSender, uncorked) writers |
| 132 |
|
| 133 |
corkingInBounds : State -> Bit |
| 134 |
corkingInBounds (_, corking) = corking == corked \/ corking == uncorked |