ok secmod + logs

This commit is contained in:
swrup 2026-02-21 19:25:31 +01:00
parent cb33942711
commit 37ed47326a
5 changed files with 79 additions and 51 deletions

View file

@ -1,4 +1,9 @@
(* TODO refacto common parts with secmod_eddsa *)
let src = Logs.Src.create "mte.secmod_rsa"
module Log = (val Logs.src_log src : Logs.LOG)
(* - *)
open Syntax
open Crypto
open Time
@ -50,14 +55,17 @@ let key_fpath k =
(* -- IO -- *)
let read_eddsa fpath =
Log.debug (fun m -> m "reading key file `%a`" Fpath.pp fpath);
let* data = Bos.OS.File.read fpath |> unwrap_err_msg in
EddsaPrivateKey.of_octets data
let read_rsa fpath =
Log.debug (fun m -> m "reading key file `%a`" Fpath.pp fpath);
let* data = Bos.OS.File.read fpath |> unwrap_err_msg in
RsaPrivateKey.of_octets data
let write_eddsa 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_err_msg
@ -67,24 +75,30 @@ let write_rsa fpath priv =
let write_key k = write_rsa (key_fpath k) k.priv
let delete_key_file k =
let+ () =
Bos.OS.File.delete ~must_exist:true (key_fpath k) |> unwrap_err_msg
in
()
let delete_file fpath =
Log.debug (fun m -> m "(disabled) delete key file `%a`" Fpath.pp fpath);
(* TODO just to be safe~~
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
if b then
Logs.info (fun m ->
m "secmod_rsa: created directory `%a`" Fpath.pp dir_fpath);
if b then Log.info (fun m -> m "created directory `%a`" Fpath.pp dir_fpath);
let+ l =
Bos.OS.Dir.contents ~dotfiles:false ~rel:true dir_fpath |> unwrap_err_msg
Bos.OS.Dir.contents ~dotfiles:false ~rel:false dir_fpath |> unwrap_err_msg
in
l
(* -- *)
let gen_key section_name t1 t2 =
let priv, pub = RsaPrivateKey.generate ~bits:Cfg.rsa_keysize () in
Log.debug (fun m ->
m "generated key (%s-%s):@,`%s`" (time_abs_to_string t1)
(time_abs_to_string t2) (RsaPublicKey.to_b32 pub));
{ section_name; priv; pub; t1; t2 }
let sort_keys l = List.sort (fun a b -> Absolute.compare a.t2 b.t2) l
let split_in_periodes ~start ~end_ =
@ -110,15 +124,13 @@ let gen_additional_keys_until_lookahead ~now ~section_name l =
| hd :: _ -> Absolute.sub hd.t2 Cfg.overlap_duration
in
let end_ = Absolute.add now Cfg.lookahead_sign in
let periodes = split_in_periodes ~start ~end_ in
let new_keys =
List.map
(fun (t1, t2) ->
let priv, pub = RsaPrivateKey.generate ~bits:Cfg.rsa_keysize () in
{ section_name; priv; pub; t1; t2 })
periodes
in
new_keys
if Absolute.compare start end_ >= 0 then []
else
let periodes = split_in_periodes ~start ~end_ in
let new_keys =
List.map (fun (t1, t2) -> gen_key section_name t1 t2) periodes
in
new_keys
let sm_key_fpath =
Result.get_ok
@ -165,6 +177,8 @@ let init () =
| Some t -> Ok t
| None ->
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 ht = Hashtbl.create 0xff in
Ok { sm_key_priv; sm_pub; ht }
@ -202,7 +216,8 @@ module Make () = struct
let delete pub =
let* k = find pub in
Hashtbl.remove t.ht k.pub; delete_key_file k
Hashtbl.remove t.ht k.pub;
delete_file (key_fpath k)
let _delete_outdated ~now =
Hashtbl.to_seq_values t.ht
@ -212,8 +227,7 @@ module Make () = struct
|> list_iter delete
let add section_name t1 t2 =
let priv, pub = RsaPrivateKey.generate ~bits:Cfg.rsa_keysize () in
let k = { section_name; priv; pub; t1; t2 } in
let k = gen_key section_name t1 t2 in
Hashtbl.replace t.ht k.pub k;
()