This commit is contained in:
swrup 2026-04-03 16:51:24 +02:00 committed by Swrup
parent 8a9f188db3
commit 33dce050fd
2 changed files with 52 additions and 39 deletions

View file

@ -29,6 +29,7 @@ type t = {
sm_priv: Eddsa.priv;
sm_pub: Eddsa.pub;
ht: (DenominationHash.t, key) Hashtbl.t;
mutex: Miou.Mutex.t;
}
open struct
@ -134,7 +135,8 @@ open struct
let sm_pub = Eddsa.pub_of_priv sm_priv in
let ht = Hashtbl.create 0xff in
List.iter (fun k -> Hashtbl.replace ht k.h_pub k) keys;
Ok (Some { fs; sm_priv; sm_pub; ht })
let mutex = Miou.Mutex.create () in
Ok (Some { fs; sm_priv; sm_pub; ht; mutex })
let create fs =
let* opt = load fs in
@ -148,7 +150,8 @@ open struct
m "generated secmod key: `%a`" Eddsa.pp_pub sm_pub);
let* () = write_sm_key fs Cfg.sm_key_path sm_priv in
let ht = Hashtbl.create 0xff in
Ok { fs; sm_priv; sm_pub; ht }
let mutex = Miou.Mutex.create () in
Ok { fs; sm_priv; sm_pub; ht; mutex }
in
let keys = Hashtbl.to_seq_values t.ht |> List.of_seq in
let new_keys =
@ -168,6 +171,14 @@ open struct
List.iter (fun k -> Hashtbl.replace t.ht k.h_pub k) new_keys;
let+ () = list_iter (write_key fs) new_keys in
t
let find_exn t h_pub =
Log.debug (fun m -> m "find_exn: `%a`" DenominationHash.pp h_pub);
match Hashtbl.find_opt t.ht h_pub with
| None -> Fmt.failwith "secmod_rsa operation on unknown key"
| Some v -> v
let conv k = (k.h_pub, (k.coin, k.pub, k.t1))
end
(* ### *)
@ -178,23 +189,23 @@ let create fs =
Fmt.failwith "secmod_rsa initialization failure: %a." Result.pp_err e
| Ok t -> t
let find_exn t h_pub =
Log.debug (fun m -> m "find_exn: `%a`" DenominationHash.pp h_pub);
match Hashtbl.find_opt t.ht h_pub with
| None -> Fmt.failwith "secmod_rsa operation on unknown key"
| Some v -> v
let sm_pub t = t.sm_pub
let conv k = (k.h_pub, (k.coin, k.pub, k.t1))
let keys t = Hashtbl.to_seq_values t.ht |> List.of_seq |> List.map conv
let find_key t h_pub = Hashtbl.find_opt t.ht h_pub |> Option.map conv
let sign_secmod t s = Eddsa.sign ~key:t.sm_priv s
let keys t =
Miou.Mutex.protect t.mutex @@ fun () ->
Hashtbl.to_seq_values t.ht |> List.of_seq |> List.map conv
let find_key t h_pub =
Miou.Mutex.protect t.mutex @@ fun () ->
Hashtbl.find_opt t.ht h_pub |> Option.map conv
let sign t h_pub s =
let k = find_exn t h_pub in
let k = Miou.Mutex.protect t.mutex @@ fun () -> find_exn t h_pub in
Rsa.sign ~key:k.priv s
let revoke t h_pub =
Miou.Mutex.protect t.mutex @@ fun () ->
let k = find_exn t h_pub in
Hashtbl.remove t.ht h_pub;
let* () = delete_file t.fs (key_spath k) in