functorize secmod

This commit is contained in:
swrup 2026-02-21 18:43:47 +01:00
parent 7f440077ed
commit cb33942711
6 changed files with 110 additions and 97 deletions

View file

@ -1,17 +1,21 @@
open Syntax
open Crypto
(* TODO better error type *)
type 'a result = ('a, string) Result.t
module type S = Mod_intf.KEYS
module Make (Conn : Pg.CONN) = struct
module Sm_eddsa = Secmod_eddsa.Make ()
module Sm_rsa = Secmod_rsa.Make ()
let conn = (module Conn : Pg.CONN)
open Syntax
open Crypto
(* - *)
let master_pub = Config.Exchange.master_public_key
let secmod_rsa_pub = Secmod_rsa.sm_pub
let secmod_eddsa_pub = Secmod_eddsa.sm_pub
let secmod_eddsa_pub = Sm_eddsa.sm_pub
let secmod_rsa_pub = Sm_rsa.sm_pub
(* TODO error
should be a "key not found", either:
@ -20,12 +24,12 @@ module Make (Conn : Pg.CONN) = struct
- bad keyring state
*)
let sign pub s =
match Secmod_eddsa.sign ~pub s with
match Sm_eddsa.sign pub s with
| Error e -> Fmt.failwith "sign failure: %s." e
| Ok v -> v
let sign_denom pub s =
match Secmod_rsa.sign ~pub s with
match Sm_rsa.sign pub s with
| Error e -> Fmt.failwith "sign_denom failure: %s." e
| Ok v -> v
@ -49,7 +53,7 @@ module Make (Conn : Pg.CONN) = struct
let exchange_pub = pub in
let anchor_time = stamp_start in
let duration = Timestamp.diff stamp_start stamp_expire in
signf Secmod_eddsa.sign_secmod { exchange_pub; anchor_time; duration }
signf Sm_eddsa.sign_secmod { exchange_pub; anchor_time; duration }
in
Api.FutureSignKey.
{ key= pub; stamp_start; stamp_expire; stamp_end; signkey_secmod_sig }
@ -96,7 +100,7 @@ module Make (Conn : Pg.CONN) = struct
let duration_withdraw =
Timestamp.diff stamp_start stamp_expire_withdraw
in
signf Secmod_rsa.sign_secmod
signf Sm_rsa.sign_secmod
{ h_denom_pub; h_section_name; anchor_time; duration_withdraw }
in
FutureDenom.
@ -116,6 +120,7 @@ module Make (Conn : Pg.CONN) = struct
}
let future_signkeys () =
let l : Sm_eddsa.info list = Sm_eddsa.keys () in
let+ l =
list_map
(fun (pub, t1, t2) ->
@ -131,7 +136,7 @@ module Make (Conn : Pg.CONN) = struct
"secmod/database stamp_start mismatch for signkey `%s`"
(EddsaPublicKey.to_b32 pub)
| true -> Ok None))
(Secmod_eddsa.keys ())
l
in
List.filter_map Fun.id l
@ -163,7 +168,7 @@ module Make (Conn : Pg.CONN) = struct
"secmod/database stamp_start mismatch for denomination `%s`"
section_name
| true -> Ok None))
(Secmod_rsa.keys ())
(Sm_rsa.keys ())
in
List.filter_map Fun.id l
@ -223,7 +228,7 @@ module Make (Conn : Pg.CONN) = struct
}
let certify_future_signkey pub master_sig =
Secmod_eddsa.keys () |> List.find_opt (fun (pub', _t1, _t2) -> pub' = pub)
Sm_eddsa.keys () |> List.find_opt (fun (pub', _t1, _t2) -> pub' = pub)
|> function
| None -> Error "future signkey not found"
| Some (pub, t1, t2) ->
@ -240,7 +245,7 @@ module Make (Conn : Pg.CONN) = struct
Ok ()
let certify_future_denomination h_pub master_sig =
Secmod_rsa.keys ()
Sm_rsa.keys ()
|> List.find_opt (fun (_section_name, pub, _t1, _t2) ->
let h_pub' = Hash.DenominationHash.hash (RsaPublicKey.to_octets pub) in
h_pub' = h_pub)
@ -272,7 +277,7 @@ module Make (Conn : Pg.CONN) = struct
let revoke_signkey pub revoked_sig =
let* opt = find_signkey pub in
let* _sk = Option.to_result ~none:"signkey not found" opt in
let* () = Secmod_eddsa.revoke pub in
let* () = Sm_eddsa.revoke pub in
let+ () =
Pg.insert_signkey_revocation conn pub revoked_sig |> unwrap_err_caqti
in
@ -282,7 +287,7 @@ module Make (Conn : Pg.CONN) = struct
let* opt = find_denomination h_pub in
let* dn = Option.to_result ~none:"denomination not found" opt in
let pub = dn.pub in
let* () = Secmod_rsa.revoke pub in
let* () = Sm_rsa.revoke pub in
let+ () =
Pg.insert_denomination_revocation conn h_pub revoked_sig
|> unwrap_err_caqti