Skip to content

Commit

Permalink
Merge pull request #136 from robur-coop/minor
Browse files Browse the repository at this point in the history
rename "own_" to "my_" and "peer_" to "their_", following the codebase
  • Loading branch information
hannesm authored Oct 23, 2023
2 parents a6db3e6 + 64d0267 commit 17fc413
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
73 changes: 38 additions & 35 deletions src/engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,29 @@ let prf ?sids ~label ~secret ~client_random ~server_random len =
and sha = p_hash Mirage_crypto.Hash.SHA1.(hmac, digest_size) s2 in
Mirage_crypto.Uncommon.Cs.xor md5 sha

let derive_keys session (own_key_material : State.own_key_material)
(peer_key_material : Packet.tls_data) =
let derive_keys session (my_key_material : State.my_key_material)
(their_key_material : Packet.tls_data) =
(* are we the server? *)
let server = Cstruct.is_empty my_key_material.pre_master in
let ( pre_master,
client_random,
server_random,
client_random',
server_random',
sids ) =
if Cstruct.(equal empty own_key_material.pre_master) then
(* we're the server *)
( peer_key_material.pre_master,
peer_key_material.random1,
own_key_material.random1,
peer_key_material.random2,
own_key_material.random2,
if server then
( their_key_material.pre_master,
their_key_material.random1,
my_key_material.random1,
their_key_material.random2,
my_key_material.random2,
(session.their_session_id, session.my_session_id) )
else
( own_key_material.pre_master,
own_key_material.random1,
peer_key_material.random1,
own_key_material.random2,
peer_key_material.random2,
( my_key_material.pre_master,
my_key_material.random1,
their_key_material.random1,
my_key_material.random2,
their_key_material.random2,
(session.my_session_id, session.their_session_id) )
in
let master_key =
Expand All @@ -320,7 +321,7 @@ let derive_keys session (own_key_material : State.own_key_material)
prf ~label:"OpenVPN key expansion" ~secret:master_key
~client_random:client_random' ~server_random:server_random' ~sids (4 * 64)
in
keys
(server, keys)

let incoming_tls tls data =
match Tls.Engine.handle_tls tls data with
Expand Down Expand Up @@ -367,22 +368,19 @@ let maybe_kex_client rng config tls =
in
let td =
{ Packet.pre_master; random1; random2; options; user_pass; peer_info }
and own_key_material = { State.pre_master; random1; random2 } in
and my_key_material = { State.pre_master; random1; random2 } in
match
Tls.Engine.send_application_data tls [ Packet.encode_tls_data td ]
with
| None -> Error (`Msg "Tls.send application data failed for tls_data")
| Some (tls', payload) ->
let client_state = TLS_established (tls', own_key_material) in
let client_state = TLS_established (tls', my_key_material) in
Ok (client_state, Some payload)
else Ok (TLS_handshake tls, None)

let kdf session cipher hmac_algorithm own_key_material peer_key_material =
let keys = derive_keys session own_key_material peer_key_material in
let maybe_swap (a, b, c, d) =
if Cstruct.(equal empty own_key_material.State.pre_master) then (c, d, a, b)
else (a, b, c, d)
in
let kdf session cipher hmac_algorithm my_key_material their_key_material =
let server, keys = derive_keys session my_key_material their_key_material in
let maybe_swap (a, b, c, d) = if server then (c, d, a, b) else (a, b, c, d) in
let extract klen hlen =
( Cstruct.sub keys 0 klen,
Cstruct.sub keys 64 hlen,
Expand Down Expand Up @@ -439,28 +437,27 @@ let kdf session cipher hmac_algorithm own_key_material peer_key_material =
in
{ my_packet_id = 1l; their_packet_id = 1l; keys }

let kex_server config session (own_key_material : own_key_material) tls data =
let kex_server config session (my_key_material : my_key_material) tls data =
let open Result.Infix in
(* TODO verify username + password, respect incoming data, including NCP *)
let options = Config.server_generate_connect_options config in
let td =
{
Packet.pre_master = Cstruct.empty;
random1 = own_key_material.random1;
random2 = own_key_material.random2;
random1 = my_key_material.random1;
random2 = my_key_material.random2;
options;
user_pass = None;
peer_info = None;
}
in
Packet.decode_tls_data ~with_premaster:true data >>= fun peer_key_material ->
Packet.decode_tls_data ~with_premaster:true data >>= fun their_tls_data ->
match Tls.Engine.send_application_data tls [ Packet.encode_tls_data td ] with
| None -> Error (`Msg "not yet established")
| Some (tls', payload) ->
(match Config.find Ifconfig config with
| None ->
Ok
(Push_request_sent (tls', own_key_material, peer_key_material), None)
Ok (Push_request_sent (tls', my_key_material, their_tls_data), None)
| Some (Ipaddr.V4 address, Ipaddr.V4 netmask) ->
let ip_config =
let cidr = Ipaddr.V4.Prefix.of_netmask_exn ~netmask ~address in
Expand All @@ -469,7 +466,7 @@ let kex_server config session (own_key_material : own_key_material) tls data =
let cipher = Config.get Cipher config
and hmac_algorithm = Config.get Auth config in
let keys_ctx =
kdf session cipher hmac_algorithm own_key_material peer_key_material
kdf session cipher hmac_algorithm my_key_material their_tls_data
in
Ok (Established keys_ctx, Some ip_config)
| _ ->
Expand Down Expand Up @@ -573,15 +570,15 @@ let incoming_control_client config rng session channel now op data =
[ (`Control, res); (`Control, data) ]
in
(None, config, { channel with channel_st }, out)
| TLS_established (tls, key), Packet.Control -> (
| TLS_established (tls, my_key_material), Packet.Control -> (
let open Result.Infix in
incoming_tls tls data >>= fun (tls', tls_resp, d) ->
let tls_out =
match tls_resp with None -> [] | Some c -> [ (`Control, c) ]
in
match d with
| None ->
let channel_st = TLS_established (tls', key) in
let channel_st = TLS_established (tls', my_key_material) in
Ok (None, config, { channel with channel_st }, tls_out)
| Some d -> (
Packet.decode_tls_data d >>= fun tls_data ->
Expand All @@ -606,13 +603,17 @@ let incoming_control_client config rng session channel now op data =
let ip_config = ip_from_config config in
let cipher = Config.get Cipher config
and hmac_algorithm = Config.get Auth config in
let keys = kdf session cipher hmac_algorithm key tls_data in
let keys =
kdf session cipher hmac_algorithm my_key_material tls_data
in
let channel_st = Established keys in
Ok (Some ip_config, config, { channel with channel_st }, tls_out)
| None ->
let pull = Config.mem Pull config in
if pull then
let channel_st = Push_request_sent (tls', key, tls_data) in
let channel_st =
Push_request_sent (tls', my_key_material, tls_data)
in
Ok
( None,
config,
Expand All @@ -621,7 +622,9 @@ let incoming_control_client config rng session channel now op data =
else
(* now we send a PUSH_REQUEST\0 and see what happens *)
push_request tls' >>| fun (tls'', out) ->
let channel_st = Push_request_sent (tls'', key, tls_data) in
let channel_st =
Push_request_sent (tls'', my_key_material, tls_data)
in
(* first send an ack for the received key data packet (this needs to be
a separate packet from the PUSH_REQUEST for unknown reasons) *)
( None,
Expand Down
6 changes: 3 additions & 3 deletions src/state.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type own_key_material = {
type my_key_material = {
pre_master : Cstruct.t; (* only in client -> server, 48 bytes *)
random1 : Cstruct.t; (* 32 bytes *)
random2 : Cstruct.t; (* 32 bytes *)
Expand Down Expand Up @@ -65,8 +65,8 @@ let pp_keys ppf t =
type channel_state =
| Expect_reset
| TLS_handshake of Tls.Engine.state
| TLS_established of Tls.Engine.state * own_key_material
| Push_request_sent of Tls.Engine.state * own_key_material * Packet.tls_data
| TLS_established of Tls.Engine.state * my_key_material
| Push_request_sent of Tls.Engine.state * my_key_material * Packet.tls_data
| Established of keys

let pp_channel_state ppf = function
Expand Down

0 comments on commit 17fc413

Please sign in to comment.