Debian Codegraph @

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

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 //
17 // This file proves correctness of the S2N TLS handshake. It:
18 // - Proves the correspondence between the Cryptol specification
19 // of the TLS handshake and the C code
20 // - Proves that the Cryptol specification of the TLS state machine
21 // matches the state machine described in the RFC
22 // - Proves that the Cryptol specification handles corking and
23 // uncorking input correctly
24 //
25 ////////////////////////////////////////////////////////////////////////////
26
27 // WARNING: handshake_io_lowlevel.saw enables lax_loads_and_stores, which is
28 // currently only supported by SAW's What4 backend. As a result, all of the
29 // llvm_verify commands in this file use What4-based tactics (e.g., w4_unint_z3)
30 // rather than SBV-based tactics (e.g., sbv_unint_z3 or z3).
31 include "handshake_io_lowlevel.saw";
32 import "rfc_handshake_tls12.cry";
33 import "rfc_handshake_tls13.cry";
34
35 import "cork_uncork.cry";
36
37 // Verification tactic: use the Yices prover and print detailed information in
38 // case of failure.
39 let yices_debug = do { yices; print_goal; };
40
41 // Workaround for If then else on nat
42 let equalNat_ite = core_axiom
43 "(x y z : Nat) -> (b : Bool) -> Eq Bool (equalNat x (ite Nat b y z)) (ite Bool b (equalNat x y) (equalNat x z))";
44
45 // Low-level handshake_io correspondence proof
46 //
47 // WARNING: Because `enable_lax_loads_and_stores` is enabled in this module,
48 // these proofs must use What4-based proof tactics (i.e., those with a `w4_`
49 // prefix). Otherwise, they will fail, likely with an error message to the
50 // effect of:
51 //
52 // FOTArray unimplemented for backend
53 let prove_handshake_io_lowlevel = do {
54 print "Beginning the low-level spec equivalence proof";
55
56 // Dependencies specifications/overrides
57 print "Assuming specifications for dependencies";
58 print "s2n_socket_write_uncork";
59 s2n_socket_write_uncork <- crucible_llvm_unsafe_assume_spec llvm "s2n_socket_write_uncork" s2n_socket_write_uncork_spec;
60 print "s2n_socket_write_cork";
61 s2n_socket_write_cork <- crucible_llvm_unsafe_assume_spec llvm "s2n_socket_write_cork" s2n_socket_write_cork_spec;
62 print "s2n_socket_was_corked";
63 s2n_socket_was_corked <- crucible_llvm_unsafe_assume_spec llvm "s2n_socket_was_corked" s2n_socket_was_corked_spec;
64 print "s2n_socket_quickack";
65 s2n_socket_quickack <- crucible_llvm_unsafe_assume_spec llvm "s2n_socket_quickack" s2n_socket_quickack_spec;
66 print "s2n_connection_is_managed_corked";
67 s2n_connection_is_managed_corked <- crucible_llvm_unsafe_assume_spec llvm "s2n_connection_is_managed_corked" s2n_connection_is_managed_corked_spec;
68 print "s2n_generate_new_client_session_id";
69 s2n_generate_new_client_session_id <- crucible_llvm_unsafe_assume_spec llvm "s2n_generate_new_client_session_id" s2n_generate_new_client_session_id_spec;
70 print "s2n_allowed_to_cache_connection";
71 s2n_allowed_to_cache_connection <- crucible_llvm_unsafe_assume_spec llvm "s2n_allowed_to_cache_connection" s2n_allowed_to_cache_connection_spec;
72 print "s2n_resume_decrypt_session";
73 s2n_resume_decrypt_session <- crucible_llvm_unsafe_assume_spec llvm "s2n_resume_decrypt_session" s2n_resume_decrypt_session_spec;
74 let dependencies = [s2n_socket_write_uncork, s2n_socket_write_cork, s2n_socket_was_corked, s2n_connection_is_managed_corked, s2n_socket_quickack];
75
76 print "Proving correctness of get_auth_type";
77 auth_type_proof <- crucible_llvm_verify llvm "s2n_connection_get_client_auth_type" dependencies false s2n_connection_get_client_auth_type_spec (do {simplify (addsimp equalNat_ite basic_ss); (w4_unint_yices []);});
78 print "Proving correctness of s2n_advance_message";
79 s2n_advance_message_proof <- crucible_llvm_verify llvm "s2n_advance_message" dependencies false s2n_advance_message_spec (w4_unint_yices []);
80 // To prove s2n_conn_set_handshake_type's correctness, we invoke its
81 // specification (s2n_conn_set_handshake_type_spec) twice: once where
82 // chosen_psk is assumed to be NULL, and once again where chosen_psk is
83 // assumed to be non-NULL. This is needed to ensure all code paths are
84 // tested if TLS 1.3 is used, as whether FULL_HANDSHAKE is enabled or not
85 // depends on whether chosen_psk is NULL.
86 //
87 // Issue #3052 is about removing the need to invoke the specification twice.
88 let s2n_conn_set_handshake_type_ovs = [s2n_allowed_to_cache_connection, auth_type_proof, s2n_generate_new_client_session_id, s2n_resume_decrypt_session];
89 print "Proving correctness of s2n_conn_set_handshake_type (NULL chosen_psk)";
90 s2n_conn_set_handshake_type_chosen_psk_null_proof <- crucible_llvm_verify llvm "s2n_conn_set_handshake_type" s2n_conn_set_handshake_type_ovs false (s2n_conn_set_handshake_type_spec true) (w4_unint_yices []);
91 print "Proving correctness of s2n_conn_set_handshake_type (non-NULL chosen_psk)";
92 s2n_conn_set_handshake_type_chosen_psk_non_null_proof <- crucible_llvm_verify llvm "s2n_conn_set_handshake_type" s2n_conn_set_handshake_type_ovs false (s2n_conn_set_handshake_type_spec false) (w4_unint_yices []);
93
94 print "Done: Verified that the low-level specification corresponds to the C code";
95
96 return ();
97 };
98
99 // WARNING: Because `enable_lax_loads_and_stores` is enabled in this module,
100 // these proofs must use What4-based proof tactics (i.e., those with a `w4_`
101 // prefix). Otherwise, they will fail, likely with an error message to the
102 // effect of:
103 //
104 // FOTArray unimplemented for backend
105 let prove_state_machine = do {
106 print "Checking proof that the TLS1.2 RFC simulates our Cryptol s2n spec";
107 prove_print (w4_unint_z3 []) {{ tls12rfcSimulatesS2N `{16} }};
108
109 print "Checking proof that the TLS1.3 RFC simulates our Cryptol s2n spec";
110 prove_print (w4_unint_z3 []) {{ tls13rfcSimulatesS2N `{16} }};
111
112 return ();
113 };
114
115 // WARNING: Because `enable_lax_loads_and_stores` is enabled in this module,
116 // these proofs must use What4-based proof tactics (i.e., those with a `w4_`
117 // prefix). Otherwise, they will fail, likely with an error message to the
118 // effect of:
119 //
120 // FOTArray unimplemented for backend
121 let prove_cork_uncork = do {
122 print "Verifying the low-level->high-level cork-uncork simulation";
123 prove_print (w4_unint_z3 []) {{ highLevelSimulatesLowLevel `{16} }};
124
125 print "Verifying that double uncorking or corking cannot occur in server mode";
126 prove_print (w4_unint_z3 []) {{ noDoubleCorkUncork `{16} }};
127
128 print "Expecting failure when proving low-high simulation without the server mode assumption";
129 sat (w4_unint_z3 []) {{ ~highLevelDoesNotSimulateLowLevel `{16} }};
130
131 return ();
132 };