mfat path: keep it simple

This commit is contained in:
swrup 2026-03-21 07:38:29 +01:00 committed by Swrup
parent cbae1c773a
commit ec4e166173
4 changed files with 46 additions and 47 deletions

View file

@ -1,6 +1,5 @@
(* TODO
! use lock
!? Scanf not thread-safe
schedule tasks
sign: check timestamps before signing *)
(* IMPROVE
@ -10,7 +9,7 @@
list_issue_date: save timestamp of key generation
key validity period:
more checks + do not exceed lookahead
refacto common parts with secmod_eddsa *)
refacto common parts with secmod_rsa *)
let src = Logs.Src.create "mte.secmod_eddsa"
module Log = (val Logs.src_log src : Logs.LOG)
@ -18,19 +17,12 @@ module Log = (val Logs.src_log src : Logs.LOG)
(* - *)
open Syntax
open Time
module Sfn = Mfat.Sfn
module Spath = Mfat.Spath
module Cfg = struct
include Config.Exchange_secmod_eddsa
let to_spath s =
let open Fat.Spath in
match of_string s with Error (`Msg e) -> Fmt.failwith "%s" e | Ok v -> v
(* 8.3 filenames for FAT *)
let key_dir = to_spath "/EDDSA"
let sm_key = to_spath "/SM_EDDSA"
let key_dir = "/EDDSA"
let sm_key = "/SM_EDDSA"
end
type key = {
@ -60,25 +52,23 @@ let key_bin =
(* for sm_key only *)
let read_eddsa fs spath =
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "reading key file `%s`" spath);
let* data = Fat.read fs spath |> unwrap_msg in
let* priv = Eddsa.priv_of_octets data in
let pub = Eddsa.pub_of_priv priv in
Ok (priv, pub)
let write_eddsa fs spath priv =
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "writing key file `%s`" spath);
let data = Eddsa.priv_to_octets priv in
Fat.write fs spath data |> unwrap_msg
let key_spath k =
let sfn_res = String.sub (Eddsa.pub_to_b32 k.pub) 0 8 |> Sfn.of_string in
match sfn_res with
| Error _ -> failwith "not possible"
| Ok sfn -> Spath.(Cfg.key_dir / sfn)
let sfn = String.sub (Eddsa.pub_to_b32 k.pub) 0 8 in
Fat.Path.add Cfg.key_dir sfn
let read_key fs spath =
Log.debug (fun m -> m "reading key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "reading key file `%s`" spath);
let* data = Fat.read fs spath |> unwrap_msg in
(* todo: catch failure *)
let k = Bin.decode key_bin data (ref 0) in
@ -86,12 +76,12 @@ let read_key fs spath =
let write_key fs k =
let spath = key_spath k in
Log.debug (fun m -> m "writing key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "writing key file `%s`" spath);
let data = Bin.to_string key_bin k in
Fat.write fs spath data |> unwrap_msg
let delete_file fs spath =
Log.debug (fun m -> m "delete key file `%a`" Spath.pp spath);
Log.debug (fun m -> m "delete key file `%s`" spath);
let+ () = Fat.remove fs spath |> unwrap_msg in
()
@ -137,8 +127,8 @@ let load fs =
else Fat.mkdir fs Cfg.key_dir |> unwrap_msg
in
let* l = Fat.ls fs Cfg.key_dir |> unwrap_msg in
let l = List.map (fun entry -> Spath.add Cfg.key_dir entry.Fat.name) l in
let l = List.filter (fun spath -> not @@ Spath.equal spath Cfg.sm_key) l in
let l = List.map (fun entry -> Fat.Path.add Cfg.key_dir entry.Fat.name) l in
let l = List.filter (fun spath -> not @@ String.equal spath Cfg.sm_key) l in
let* keys = list_map (read_key fs) l in
match keys with
| [] -> Ok None