MNET: wip devices

This commit is contained in:
swrup 2026-03-12 12:08:21 +01:00
parent 832098dd58
commit 7ed875cd5e
6 changed files with 162 additions and 79 deletions

View file

@ -48,6 +48,7 @@ type key = {
}
type t = {
fs: Fat.t;
sm_key_priv: EddsaPrivateKey.t;
sm_pub: EddsaPublicKey.t;
ht: (DenominationHash.t, key) Hashtbl.t;
@ -77,28 +78,28 @@ let key_fpath k =
(* -- IO -- *)
let read_eddsa fpath =
let read_eddsa fs fpath =
Log.debug (fun m -> m "reading key file `%a`" Fpath.pp fpath);
let* data = Bos.OS.File.read fpath |> unwrap_msg in
let* data = Fat.read fs fpath |> unwrap_msg in
EddsaPrivateKey.of_octets data
let read_rsa fpath =
let read_rsa fs fpath =
Log.debug (fun m -> m "reading key file `%a`" Fpath.pp fpath);
let* data = Bos.OS.File.read fpath |> unwrap_msg in
let* data = Fat.read fs fpath |> unwrap_msg in
RsaPrivateKey.of_octets data
let write_eddsa fpath priv =
let write_eddsa fs fpath priv =
Log.debug (fun m -> m "writing key file `%a`" Fpath.pp fpath);
let data = EddsaPrivateKey.to_octets priv in
Bos.OS.File.write fpath data |> unwrap_msg
Fat.write fs fpath data |> unwrap_msg
let write_rsa fpath priv =
let write_rsa fs fpath priv =
let data = RsaPrivateKey.to_octets priv in
Bos.OS.File.write fpath data |> unwrap_msg
Fat.write fs fpath data |> unwrap_msg
let write_key k = write_rsa (key_fpath k) k.priv
let write_key fs k = write_rsa fs (key_fpath k) k.priv
let delete_file fpath =
let delete_file fs fpath =
(* check fpath just to be safe *)
let () =
let root = Fpath.v Cfg.key_dir in
@ -108,10 +109,10 @@ let delete_file fpath =
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_msg in
let+ () = Fat.remove fs fpath |> unwrap_msg in
()
let get_key_dir_contents dir_fpath =
let get_key_dir_contents fs dir_fpath =
let* b = Bos.OS.Dir.create ~mode:0o700 dir_fpath |> unwrap_msg in
if b then Log.info (fun m -> m "created directory `%a`" Fpath.pp dir_fpath);
let+ l =
@ -172,38 +173,38 @@ let sm_key_fpath =
(* we load sm_key separately
we don't accept non-key files in key_dir *)
let load_key ~section_name fpath =
let load_key fs ~section_name fpath =
match parse_filename fpath with
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
| Some (t1, t2) ->
let+ priv = read_rsa fpath in
let+ priv = read_rsa fs fpath in
let pub = RsaPrivateKey.pub_of_priv priv in
let h_pub = DenominationHash.hash_of_rsa pub in
{ section_name; priv; pub; h_pub; t1; t2 }
let load_section section_name =
let load_section fs section_name =
let section_fpath = Fpath.(v Cfg.key_dir / section_name) in
let* l = get_key_dir_contents section_fpath in
let* l = get_key_dir_contents fs section_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
let* keys = list_map (load_key fs ~section_name) l in
Ok keys
let load () =
let* keys_l = list_map load_section Cfg.sections in
let load fs =
let* keys_l = list_map (load_section fs) Cfg.sections in
let keys = List.concat keys_l in
match keys with
| [] -> Ok None
| _l ->
let* sm_key_priv = read_eddsa sm_key_fpath in
let* sm_key_priv = read_eddsa fs sm_key_fpath in
let sm_pub = EddsaPrivateKey.pub_of_priv sm_key_priv in
let ht = Hashtbl.create 0xff in
let () = List.iter (fun k -> Hashtbl.replace ht k.h_pub k) keys in
(* TODO list_issue_date *)
let last_change = Timestamp.never in
Ok (Some { sm_key_priv; sm_pub; ht; last_change })
Ok (Some { fs; sm_key_priv; sm_pub; ht; last_change })
let init () =
let* opt = load () in
let init fs =
let* opt = load fs in
let now = TimeAbsolute.of_ptime (Ptime_clock.now ()) in
let* t =
match opt with
@ -212,10 +213,10 @@ let init () =
let sm_key_priv, sm_pub = EddsaPrivateKey.generate () in
Log.debug (fun m ->
m "generated secmod key: `%s`" (EddsaPublicKey.to_b32 sm_pub));
let* () = write_eddsa sm_key_fpath sm_key_priv in
let* () = write_eddsa fs sm_key_fpath sm_key_priv in
let ht = Hashtbl.create 0xff in
let last_change = Timestamp.of_absolute now in
Ok { sm_key_priv; sm_pub; ht; last_change }
Ok { fs; sm_key_priv; sm_pub; ht; last_change }
in
let all_keys = List.of_seq @@ Hashtbl.to_seq_values t.ht in
let new_keys_l =
@ -229,12 +230,12 @@ let init () =
in
let new_keys = List.concat new_keys_l in
List.iter (fun k -> Hashtbl.replace t.ht k.h_pub k) new_keys;
let+ () = list_iter write_key new_keys in
let+ () = list_iter (write_key fs) new_keys in
t
module Make () = struct
module Make (Fs : Fat.FS) = struct
let t =
match init () with
match init Fs.t with
| Error e -> Fmt.failwith "secmod_rsa initialization failure: %s." e
| Ok t -> t
@ -244,7 +245,7 @@ module Make () = struct
let delete h_pub =
let* k = find h_pub in
Hashtbl.remove t.ht h_pub;
delete_file (key_fpath k)
delete_file t.fs (key_fpath k)
let _delete_outdated ~now =
Hashtbl.to_seq t.ht
@ -256,7 +257,7 @@ module Make () = struct
let add section_name t1 t2 =
let last_change = Timestamp.of_ptime (Ptime_clock.now ()) in
let k = gen_key ~section_name t1 t2 in
let+ () = write_key k in
let+ () = write_key t.fs k in
Hashtbl.replace t.ht k.h_pub k;
t.last_change <- last_change;
()