This commit is contained in:
swrup 2026-03-03 10:09:03 +01:00 committed by Swrup
parent 93f2dcb292
commit 4bb62e369d
20 changed files with 112 additions and 130 deletions

View file

@ -1,4 +1,3 @@
(* TODO refacto common parts with secmod_eddsa *)
let src = Logs.Src.create "mte.secmod_rsa"
module Log = (val Logs.src_log src : Logs.LOG)
@ -80,22 +79,22 @@ let key_fpath k =
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
let* data = Bos.OS.File.read fpath |> unwrap_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
let* data = Bos.OS.File.read fpath |> unwrap_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
Bos.OS.File.write fpath data |> unwrap_msg
let write_rsa fpath priv =
let data = RsaPrivateKey.to_octets priv in
Bos.OS.File.write fpath data |> unwrap_err_msg
Bos.OS.File.write fpath data |> unwrap_msg
let write_key k = write_rsa (key_fpath k) k.priv
@ -109,14 +108,14 @@ 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_err_msg in
let+ () = Bos.OS.File.delete ~must_exist:true fpath |> unwrap_msg in
()
let get_key_dir_contents dir_fpath =
let* b = Bos.OS.Dir.create ~mode:0o700 dir_fpath |> unwrap_err_msg in
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 =
Bos.OS.Dir.contents ~dotfiles:false ~rel:false dir_fpath |> unwrap_err_msg
Bos.OS.Dir.contents ~dotfiles:false ~rel:false dir_fpath |> unwrap_msg
in
List.map Fpath.normalize l
@ -168,7 +167,7 @@ let gen_additional_keys_until_lookahead ~now ~section_name l =
let sm_key_fpath =
Result.get_ok
@@
let+ fpath = Fpath.of_string Cfg.sm_priv_key |> unwrap_err_msg in
let+ fpath = Fpath.of_string Cfg.sm_priv_key |> unwrap_msg in
Fpath.normalize fpath
(* we load sm_key separately
@ -199,9 +198,7 @@ let load () =
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
not ideal, not sure what to put here
it would be best to save keys creation time *)
(* TODO list_issue_date *)
let last_change = Timestamp.never in
Ok (Some { sm_key_priv; sm_pub; ht; last_change })