This commit is contained in:
parent
e55e2fac37
commit
3c81bb0ba3
1 changed files with 80 additions and 80 deletions
160
src/secmod.ml
160
src/secmod.ml
|
|
@ -73,86 +73,6 @@ module Make (Conn : Pg.CONN) = struct
|
|||
let dn_fname section_name =
|
||||
Fpath.(Config.secmod_dir / Fmt.str "dn_%s" section_name)
|
||||
|
||||
let database_find_sk conn pub =
|
||||
let* opt = Pg.find_signkey conn pub |> unwrap_err_caqti in
|
||||
match opt with
|
||||
| None ->
|
||||
Fmt.error "secmod failure: a signkey could not be found in database"
|
||||
| Some sk_data -> Ok sk_data
|
||||
|
||||
let database_find_dn conn h_pub =
|
||||
let* opt = Pg.find_denom conn h_pub |> unwrap_err_caqti in
|
||||
match opt with
|
||||
| None ->
|
||||
Fmt.error
|
||||
"secmod failure: a denomination could not be found in database"
|
||||
| Some dn_data -> Ok dn_data
|
||||
|
||||
let list_to_ht l = Hashtbl.of_seq (List.to_seq l)
|
||||
|
||||
let load () =
|
||||
let* sm_key = Data_file.read_eddsa sm_key_fname in
|
||||
let sm_pubkey = EddsaPrivateKey.pub_of_priv sm_key in
|
||||
|
||||
let* sk_keys = list_map Data_file.read_eddsa (List.init 1 sk_fname) in
|
||||
let* sk_l =
|
||||
list_map
|
||||
(fun priv ->
|
||||
let pub = EddsaPrivateKey.pub_of_priv priv in
|
||||
let+ sk = database_find_sk conn pub in
|
||||
((pub, sk), (pub, priv)))
|
||||
sk_keys
|
||||
in
|
||||
let sk_ht, sk_key_ht =
|
||||
match List.split sk_l with l1, l2 -> (list_to_ht l1, list_to_ht l2)
|
||||
in
|
||||
|
||||
let dn_section_name_ht = Hashtbl.create 0xff in
|
||||
let* dn_keys =
|
||||
list_map
|
||||
(fun coin ->
|
||||
let section_name = coin.Config.Coin.section_name in
|
||||
let+ priv = Data_file.read_rsa (dn_fname section_name) in
|
||||
(section_name, priv))
|
||||
Config.Coin.all_coins
|
||||
in
|
||||
let* dn_l =
|
||||
list_map
|
||||
(fun (section_name, priv) ->
|
||||
let h_pub =
|
||||
priv
|
||||
|> RsaPrivateKey.pub_of_priv
|
||||
|> RsaPublicKey.to_octets
|
||||
|> DenominationHash.hash
|
||||
in
|
||||
(* fill dn_section_name_ht *)
|
||||
Hashtbl.replace dn_section_name_ht h_pub section_name;
|
||||
let+ dn = database_find_dn conn h_pub in
|
||||
((h_pub, dn), (h_pub, priv)))
|
||||
dn_keys
|
||||
in
|
||||
let dn_ht, dn_key_ht =
|
||||
match List.split dn_l with l1, l2 -> (list_to_ht l1, list_to_ht l2)
|
||||
in
|
||||
(* future keys are not stored anywhere until they are certified with a master_sig
|
||||
so we don't have any future key to load *)
|
||||
let t =
|
||||
{
|
||||
sm_key;
|
||||
sm_pubkey;
|
||||
sk_ht;
|
||||
dn_ht;
|
||||
sk_key_ht;
|
||||
dn_key_ht;
|
||||
future_sk_ht= Hashtbl.create 0xff;
|
||||
future_dn_ht= Hashtbl.create 0xff;
|
||||
future_sk_key_ht= Hashtbl.create 0xff;
|
||||
future_dn_key_ht= Hashtbl.create 0xff;
|
||||
dn_section_name_ht;
|
||||
}
|
||||
in
|
||||
Ok t
|
||||
|
||||
let sign_with_sm_key t s = EddsaSignature.sign ~key:t.sm_key s
|
||||
|
||||
let make_future_sk t =
|
||||
|
|
@ -267,6 +187,86 @@ module Make (Conn : Pg.CONN) = struct
|
|||
List.iter (make_future_dn t) Config.Coin.all_coins;
|
||||
t
|
||||
|
||||
let database_find_sk conn pub =
|
||||
let* opt = Pg.find_signkey conn pub |> unwrap_err_caqti in
|
||||
match opt with
|
||||
| None ->
|
||||
Fmt.error "secmod failure: a signkey could not be found in database"
|
||||
| Some sk_data -> Ok sk_data
|
||||
|
||||
let database_find_dn conn h_pub =
|
||||
let* opt = Pg.find_denom conn h_pub |> unwrap_err_caqti in
|
||||
match opt with
|
||||
| None ->
|
||||
Fmt.error
|
||||
"secmod failure: a denomination could not be found in database"
|
||||
| Some dn_data -> Ok dn_data
|
||||
|
||||
let list_to_ht l = Hashtbl.of_seq (List.to_seq l)
|
||||
|
||||
let load () =
|
||||
let* sm_key = Data_file.read_eddsa sm_key_fname in
|
||||
let sm_pubkey = EddsaPrivateKey.pub_of_priv sm_key in
|
||||
|
||||
let* sk_keys = list_map Data_file.read_eddsa (List.init 1 sk_fname) in
|
||||
let* sk_l =
|
||||
list_map
|
||||
(fun priv ->
|
||||
let pub = EddsaPrivateKey.pub_of_priv priv in
|
||||
let+ sk = database_find_sk conn pub in
|
||||
((pub, sk), (pub, priv)))
|
||||
sk_keys
|
||||
in
|
||||
let sk_ht, sk_key_ht =
|
||||
match List.split sk_l with l1, l2 -> (list_to_ht l1, list_to_ht l2)
|
||||
in
|
||||
|
||||
let dn_section_name_ht = Hashtbl.create 0xff in
|
||||
let* dn_keys =
|
||||
list_map
|
||||
(fun coin ->
|
||||
let section_name = coin.Config.Coin.section_name in
|
||||
let+ priv = Data_file.read_rsa (dn_fname section_name) in
|
||||
(section_name, priv))
|
||||
Config.Coin.all_coins
|
||||
in
|
||||
let* dn_l =
|
||||
list_map
|
||||
(fun (section_name, priv) ->
|
||||
let h_pub =
|
||||
priv
|
||||
|> RsaPrivateKey.pub_of_priv
|
||||
|> RsaPublicKey.to_octets
|
||||
|> DenominationHash.hash
|
||||
in
|
||||
(* fill dn_section_name_ht *)
|
||||
Hashtbl.replace dn_section_name_ht h_pub section_name;
|
||||
let+ dn = database_find_dn conn h_pub in
|
||||
((h_pub, dn), (h_pub, priv)))
|
||||
dn_keys
|
||||
in
|
||||
let dn_ht, dn_key_ht =
|
||||
match List.split dn_l with l1, l2 -> (list_to_ht l1, list_to_ht l2)
|
||||
in
|
||||
(* future keys are not stored anywhere until they are certified with a master_sig
|
||||
so we don't have any future key to load *)
|
||||
let t =
|
||||
{
|
||||
sm_key;
|
||||
sm_pubkey;
|
||||
sk_ht;
|
||||
dn_ht;
|
||||
sk_key_ht;
|
||||
dn_key_ht;
|
||||
future_sk_ht= Hashtbl.create 0xff;
|
||||
future_dn_ht= Hashtbl.create 0xff;
|
||||
future_sk_key_ht= Hashtbl.create 0xff;
|
||||
future_dn_key_ht= Hashtbl.create 0xff;
|
||||
dn_section_name_ht;
|
||||
}
|
||||
in
|
||||
Ok t
|
||||
|
||||
let init () =
|
||||
let dir = Config.secmod_dir in
|
||||
let* b = Bos.OS.Dir.create ~mode:0o700 dir |> unwrap_err_msg in
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue