fix secmod add/write; + wip test revoke

This commit is contained in:
swrup 2026-02-25 00:12:05 +01:00
parent 836b9ff64a
commit 05cfa7a5b1
8 changed files with 165 additions and 45 deletions

View file

@ -100,11 +100,17 @@ let write_rsa fpath priv =
let write_key k = write_rsa (key_fpath k) k.priv
let delete_file fpath =
Log.debug (fun m -> m "(disabled) delete key file `%a`" Fpath.pp fpath);
(* TODO just to be safe~~
(* check fpath just to be safe *)
let () =
let root = Fpath.v Cfg.key_dir in
if not @@ Fpath.is_rooted ~root fpath then
Fmt.failwith
"delete_file failure: file `%a` is not contained in secmod directory"
Fpath.pp fpath
in
Log.debug (fun m -> m "delete key file `%a`" Fpath.pp fpath);
let+ () = Bos.OS.File.delete ~must_exist:true fpath |> unwrap_err_msg in
*)
Ok ()
()
let get_key_dir_contents dir_fpath =
let* b = Bos.OS.Dir.create ~mode:0o700 dir_fpath |> unwrap_err_msg in
@ -112,7 +118,7 @@ let get_key_dir_contents dir_fpath =
let+ l =
Bos.OS.Dir.contents ~dotfiles:false ~rel:false dir_fpath |> unwrap_err_msg
in
l
List.map Fpath.normalize l
(* -- *)
@ -179,11 +185,7 @@ let load_key ~section_name fpath =
let load_section section_name =
let section_fpath = Fpath.(v Cfg.key_dir / section_name) in
let* l = get_key_dir_contents section_fpath in
let l =
l
|> List.map Fpath.normalize
|> List.filter (fun fpath -> not @@ Fpath.equal fpath sm_key_fpath)
in
let l = List.filter (fun fpath -> not @@ Fpath.equal fpath sm_key_fpath) l in
let* keys = list_map (load_key ~section_name) l in
Ok keys
@ -224,7 +226,7 @@ let init () =
Cfg.sections
in
let new_keys = List.concat new_keys_l in
let () = List.iter (fun k -> Hashtbl.replace t.ht k.h_pub k) new_keys in
List.iter (fun k -> Hashtbl.replace t.ht k.h_pub k) new_keys;
let+ () = list_iter write_key new_keys in
t
@ -252,6 +254,7 @@ module Make () = struct
let add section_name t1 t2 =
let k = gen_key ~section_name t1 t2 in
Hashtbl.replace t.ht k.h_pub k;
let+ () = write_key k in
()
let sm_pub = t.sm_pub
@ -263,9 +266,11 @@ module Make () = struct
data
let revoke h_pub =
Log.debug (fun m ->
m "revoke `%s`" (DenominationHash.to_octets h_pub |> B32.encode));
let* k = find h_pub in
let* () = delete h_pub in
add k.section_name k.t1 k.t2;
let* () = add k.section_name k.t1 k.t2 in
Ok ()
let conv =