MNET: wip devices
This commit is contained in:
parent
832098dd58
commit
7ed875cd5e
6 changed files with 162 additions and 79 deletions
|
|
@ -1,11 +1,12 @@
|
|||
(* TODO
|
||||
! use lock
|
||||
schedule tasks
|
||||
sign: check timestamps before signing
|
||||
|
||||
sign: check timestamps before signing *)
|
||||
(* IMPROVE
|
||||
list_issue_date: save timestamp of key generation
|
||||
key validity period:
|
||||
more checks + do not exceed lookahead
|
||||
look into ocaml-diet maybe
|
||||
refacto common parts with secmod_eddsa *)
|
||||
let src = Logs.Src.create "mte.secmod_eddsa"
|
||||
|
||||
|
|
@ -25,6 +26,7 @@ type key = {
|
|||
}
|
||||
|
||||
type t = {
|
||||
fs: Fat.t;
|
||||
sm_key_priv: EddsaPrivateKey.t;
|
||||
sm_pub: EddsaPublicKey.t;
|
||||
ht: (EddsaPublicKey.t, key) Hashtbl.t;
|
||||
|
|
@ -53,19 +55,19 @@ let key_fpath k =
|
|||
|
||||
(* -- IO -- *)
|
||||
|
||||
let read_key fpath =
|
||||
let read_key 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 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_key k = write_eddsa (key_fpath k) k.priv
|
||||
let write_key fs k = write_eddsa 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
|
||||
|
|
@ -75,14 +77,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_msg in
|
||||
let+ () = Fat.remove fs fpath |> unwrap_msg in
|
||||
()
|
||||
|
||||
let get_key_dir_contents dir =
|
||||
let get_key_dir_contents fs dir =
|
||||
let* dir = Fpath.of_string dir |> unwrap_msg in
|
||||
let* b = Bos.OS.Dir.create ~mode:0o700 dir |> unwrap_msg in
|
||||
let* b = Fat.create fs dir |> unwrap_msg in
|
||||
if b then Log.info (fun m -> m "created directory `%a`" Fpath.pp dir);
|
||||
let+ l = Bos.OS.Dir.contents ~dotfiles:false ~rel:false dir |> unwrap_msg in
|
||||
let+ l = Fat.ls fs dir |> unwrap_msg in
|
||||
List.map Fpath.normalize l
|
||||
|
||||
(* -- *)
|
||||
|
|
@ -132,29 +134,29 @@ let sm_key_fpath =
|
|||
|
||||
(* we load sm_key separately
|
||||
we don't accept non-key files in key_dir *)
|
||||
let load_key fpath =
|
||||
let load_key fs fpath =
|
||||
match parse_filename fpath with
|
||||
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
|
||||
| Some (t1, t2) ->
|
||||
let+ priv = read_key fpath in
|
||||
let+ priv = read_key fs fpath in
|
||||
let pub = EddsaPrivateKey.pub_of_priv priv in
|
||||
{ priv; pub; t1; t2 }
|
||||
|
||||
let load () =
|
||||
let* l = get_key_dir_contents Cfg.key_dir in
|
||||
let load fs =
|
||||
let* l = get_key_dir_contents fs Cfg.key_dir in
|
||||
let l = List.filter (fun fpath -> not @@ Fpath.equal fpath sm_key_fpath) l in
|
||||
let* keys = list_map load_key l in
|
||||
let* keys = list_map (load_key fs) l in
|
||||
match keys with
|
||||
| [] -> Ok None
|
||||
| _l ->
|
||||
let* sm_key_priv = read_key sm_key_fpath in
|
||||
let* sm_key_priv = read_key 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.pub k) keys in
|
||||
Ok (Some { sm_key_priv; sm_pub; ht })
|
||||
Ok (Some { fs; sm_key_priv; sm_pub; ht })
|
||||
|
||||
let init () =
|
||||
let* opt = load () in
|
||||
let init fs =
|
||||
let* opt = load fs in
|
||||
let* t =
|
||||
match opt with
|
||||
| Some t -> Ok t
|
||||
|
|
@ -164,18 +166,18 @@ let init () =
|
|||
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 }
|
||||
Ok { fs; sm_key_priv; sm_pub; ht }
|
||||
in
|
||||
let now = TimeAbsolute.of_ptime (Ptime_clock.now ()) in
|
||||
let keys = List.of_seq @@ Hashtbl.to_seq_values t.ht in
|
||||
let new_keys = gen_additional_keys_until_lookahead ~now keys in
|
||||
List.iter (fun k -> Hashtbl.replace t.ht k.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_eddsa initialization failure: %s." e
|
||||
| Ok t -> t
|
||||
|
||||
|
|
@ -185,13 +187,13 @@ module Make () = struct
|
|||
let add t1 t2 =
|
||||
let k = gen_key t1 t2 in
|
||||
Hashtbl.replace t.ht k.pub k;
|
||||
let+ () = write_key k in
|
||||
let+ () = write_key t.fs k in
|
||||
()
|
||||
|
||||
let delete pub =
|
||||
let* k = find pub in
|
||||
Hashtbl.remove t.ht k.pub;
|
||||
delete_file (key_fpath k)
|
||||
delete_file t.fs (key_fpath k)
|
||||
|
||||
let _delete_outdated ~now =
|
||||
Hashtbl.to_seq_values t.ht
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue