From 3a5368434c22c7f7ceff565f114642f9d74c8929 Mon Sep 17 00:00:00 2001 From: Eitan Chatav Date: Sat, 5 Nov 2016 09:12:25 -0700 Subject: [PATCH] add secret key to peer file --- src/node/net/p2p.ml | 27 ++++++++++++++++++--------- src/utils/crypto_box.ml | 2 ++ src/utils/crypto_box.mli | 2 ++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index 9bf11f423..bf88a2703 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -438,11 +438,19 @@ let public_key_encoding = (Crypto_box.to_public_key << MBytes.of_string) string +let secret_key_encoding = + let open Data_encoding in + conv + (MBytes.to_string << Crypto_box.of_secret_key) + (Crypto_box.to_secret_key << MBytes.of_string) + string + let peers_file_encoding = let open Data_encoding in - obj3 + obj4 (req "gid" string) (req "public_key" public_key_encoding) + (req "secret_key" secret_key_encoding) (req "peers" (obj3 (req "known" @@ -601,7 +609,7 @@ let bootstrap config limits = on_cancel (fun () -> close_msg_queue () ; return ()) ; (* fill the known peers pools from last time *) Data_encoding.Json.read_file config.peers_file >>= fun res -> - let known_peers, black_list, my_gid, my_public_key = + let known_peers, black_list, my_gid, my_public_key, my_secret_key = let init_peers () = let my_gid = fresh_gid () in @@ -617,19 +625,19 @@ let bootstrap config limits = PeerMap.empty config.known_peers in let black_list = BlackList.empty in - known_peers, black_list, my_gid, my_public_key in + known_peers, black_list, my_gid, my_public_key, my_secret_key in match res with | None -> - let known_peers, black_list, my_gid, my_public_key = init_peers () in + let known_peers, black_list, my_gid, my_public_key, my_secret_key = init_peers () in debug "(%a) peer cache initiated" pp_gid my_gid ; - ref known_peers, ref black_list, my_gid, my_public_key + ref known_peers, ref black_list, my_gid, my_public_key, my_secret_key | Some json -> match Data_encoding.Json.destruct peers_file_encoding json with | exception _ -> - let known_peers, black_list, my_gid, my_public_key = init_peers () in + let known_peers, black_list, my_gid, my_public_key, my_secret_key = init_peers () in debug "(%a) peer cache reset" pp_gid my_gid ; - ref known_peers, ref black_list, my_gid, my_public_key - | (my_gid, my_public_key, (k, b, w)) -> + ref known_peers, ref black_list, my_gid, my_public_key, my_secret_key + | (my_gid, my_public_key, my_secret_key, (k, b, w)) -> let white_list = List.fold_right PointSet.add w PointSet.empty in let known_peers = @@ -654,7 +662,7 @@ let bootstrap config limits = (fun r (a, d) -> BlackList.add a d r) BlackList.empty b in debug "(%a) peer cache loaded" pp_gid my_gid ; - ref known_peers, ref black_list, my_gid, my_public_key + ref known_peers, ref black_list, my_gid, my_public_key, my_secret_key in (* some peer reachability predicates *) let black_listed (addr, _) = @@ -673,6 +681,7 @@ let bootstrap config limits = Data_encoding.Json.construct peers_file_encoding @@ (my_gid, my_public_key, + my_secret_key, PeerMap.fold (fun (addr, port) gid source (k, b, w) -> let infos = match gid, source.connections with diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index d53fa84ec..e9ea2f904 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -19,5 +19,7 @@ let random_nonce = Sodium.Box.random_nonce let increment_nonce = Sodium.Box.increment_nonce let box = Sodium.Box.Bigbytes.box let box_open = Sodium.Box.Bigbytes.box_open +let to_secret_key = Sodium.Box.Bigbytes.to_secret_key +let of_secret_key = Sodium.Box.Bigbytes.of_secret_key let to_public_key = Sodium.Box.Bigbytes.to_public_key let of_public_key = Sodium.Box.Bigbytes.of_public_key diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index 9e1243ad7..1908e18b3 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -18,5 +18,7 @@ val random_nonce : unit -> nonce val increment_nonce : ?step:int -> nonce -> nonce val box : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t val box_open : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t +val to_secret_key : MBytes.t -> secret_key +val of_secret_key : secret_key -> MBytes.t val to_public_key : MBytes.t -> public_key val of_public_key : public_key -> MBytes.t