Source file conex_resource.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
open Conex_utils
type name = string
let pp_name = Format.pp_print_string
let name_equal a b = String.compare_insensitive a b = 0
type identifier = string
let pp_id = Format.pp_print_string
let id_equal a b = String.compare_insensitive a b = 0
type timestamp = string
let pp_timestamp = Format.pp_print_string
let timestamp_equal a b = String.compare a b = 0
module Wire = struct
type s =
| Map of s M.t
| List of s list
| Identifier of identifier
| Data of string
| Bigint of Uint.t
| Smallint of int
| Pair of s * s
| And of s * s
| Or of s * s
let rec s_to_string = function
| Bigint x -> "0x" ^ Uint.to_string x
| Smallint i -> string_of_int i
| Data s -> string_of_int (String.length s) ^ "'" ^ s
| Identifier i -> i
| List xs -> "[" ^ String.concat ";" (List.map s_to_string xs) ^ "]"
| Map m ->
let strs =
let bindings = M.bindings m in
let sorted = List.sort (fun (a, _) (b, _) -> String.compare a b) bindings in
List.map (fun (k, v) -> k ^ ":" ^ s_to_string v) sorted
in
"{" ^ String.concat ";" strs ^ "}"
| Pair (i, s) -> "(" ^ s_to_string i ^ s_to_string s ^ ")"
| And (a, b) -> "(" ^ s_to_string a ^ "&&" ^ s_to_string b ^ ")"
| Or (a, b) -> "(" ^ s_to_string a ^ "||" ^ s_to_string b ^ ")"
type t = s M.t
let to_string t = s_to_string (Map t)
let opt_err = function
| Some x -> Ok x
| None -> Error "expected some, got none"
let pdata = function
| Data x -> Ok x
| _ -> Error "couldn't find data"
let puint = function
| Bigint x -> Ok x
| _ -> Error "couldn't find int"
let pint = function
| Smallint x -> Ok x
| _ -> Error "couldn't find int"
let plist = function
| List x -> Ok x
| _ -> Error "couldn't find list"
let pmap = function
| Map m -> Ok m
| _ -> Error "couldn't find map"
end
type typ = [
| `Root
| `Targets
]
let typ_equal a b = match a, b with
| `Root, `Root
| `Targets, `Targets -> true
| _ -> false
let typ_to_string = function
| `Root -> "root"
| `Targets -> "targets"
let string_to_typ = function
| "root" -> Some `Root
| "targets" -> Some `Targets
| _ -> None
let pp_typ ppf typ = Format.pp_print_string ppf (typ_to_string typ)
let wire_typ typ = Wire.Identifier (typ_to_string typ)
let typ_of_wire = function
| Wire.Identifier str ->
(match string_to_typ str with
| None -> Error "unknown resource type"
| Some x -> Ok x)
| _ -> Error "cannot parse resource type"
type err = [
| `Parse of string
| `Unknown_alg of string
| `Malformed
]
let pp_err ppf = function
| `Parse err -> Format.fprintf ppf "parse error %s" err
| `Unknown_alg alg -> Format.fprintf ppf "unknown algorithm %s" alg
| `Malformed -> Format.pp_print_string ppf "malformed"
module Digest = struct
type alg = [ `SHA256 ]
let alg_to_string = function `SHA256 -> "sha256"
let string_to_alg = function
| "sha256" -> Some `SHA256
| _ -> None
type t = alg * string
let compare (ta, a) (tb, b) = match ta, tb with
| `SHA256, `SHA256 -> String.compare a b
let equal a b = compare a b = 0
let to_string (typ, data) = alg_to_string typ ^ "=" ^ data
let of_string str = match String.cut '=' str with
| None -> Error `Malformed
| Some (alg, data) ->
match string_to_alg alg with
| Some hash -> Ok (hash, data)
| None -> Error (`Unknown_alg alg)
let pp ppf t = Format.pp_print_string ppf (to_string t)
let of_wire = function
| Wire.Data dgst -> of_string dgst
| _ -> Error `Malformed
let wire_raw t = Wire.Data (to_string t)
end
module Digest_map = struct
include Map.Make(Digest)
let find k m = try Some (find k m) with Not_found -> None
let pp pp_e ppf t =
iter (fun k v -> Format.fprintf ppf "%a -> %a@ " Digest.pp k pp_e v) t
end
module Key = struct
type alg = [ `RSA ]
let alg_to_string = function `RSA -> "rsa"
let string_to_alg = function "rsa" -> Some `RSA | _ -> None
let alg_equal a b = match a, b with
| `RSA, `RSA -> true
type t = identifier * timestamp * alg * string
let equal (id, ts, alg, data) (id', ts', alg', data') =
id_equal id id' && timestamp_equal ts ts' &&
alg_equal alg alg' && String.compare data data' = 0
let pp ppf (id, created, a, x) =
Format.fprintf ppf "%a (created %a) %s key %d bytes"
pp_id id pp_timestamp created
(alg_to_string a) (String.length x)
let of_wire data =
let open Wire in
match plist data with
| Error str -> Error (`Parse str)
| Ok [ Identifier id ; Data ts ; Data raw ] ->
begin match String.cut '=' raw with
| None -> Error `Malformed
| Some (alg, data) ->
match string_to_alg alg with
| Some alg -> Ok (id, ts, alg, data)
| None -> Error (`Unknown_alg alg)
end
| _ -> Error `Malformed
let many_of_wire keys =
foldM (fun (acc, msgs) k ->
match of_wire k with
| Ok ((id, _, _, _) as key) ->
if M.mem id acc then begin
let msg = "key with id " ^ id ^ " already present, ignoring" in
Ok (acc, msg :: msgs)
end else
Ok (M.add id key acc, msgs)
| Error (`Unknown_alg alg) ->
let msg = "couldn't parse key algorithm " ^ alg ^ ", ignoring" in
Ok (acc, msg :: msgs)
| Error e -> Error (str_pp pp_err e))
(M.empty, []) keys
let to_string (_, _, a, k) = alg_to_string a ^ "=" ^ k
let wire_raw k =
let (id, created, _, _) = k in
Wire.List [ Wire.Identifier id ; Wire.Data created ; Wire.Data (to_string k) ]
let wire k = M.add "keys" (wire_raw k) M.empty
let keyid hash t = hash (to_string t)
end
module Signature = struct
type alg = [ `RSA_PSS_SHA256 ]
let alg_to_string = function
| `RSA_PSS_SHA256 -> "rsapss-sha256"
let alg_equal a b = match a, b with
| `RSA_PSS_SHA256, `RSA_PSS_SHA256 -> true
let string_to_alg = function
| "rsapss-sha256" -> Some `RSA_PSS_SHA256
| _ -> None
type t = identifier * timestamp * alg * string
let equal (id, ts, alg, data) (id', ts', alg', data') =
id_equal id id' && timestamp_equal ts ts' &&
alg_equal alg alg' && String.compare data data' = 0
let pp ppf (id, created, alg, data) =
Format.fprintf ppf "%s signature by %a (created %a), %d bytes"
(alg_to_string alg) pp_id id pp_timestamp created (String.length data)
let of_wire data =
let open Wire in
match plist data with
| Error str -> Error (`Parse str)
| Ok [ Identifier id ; Data created ; Data s ] ->
begin match String.cut '=' s with
| None -> Error `Malformed
| Some (alg, data) ->
match string_to_alg alg with
| None -> Error (`Unknown_alg alg)
| Some alg -> Ok (id, created, alg, data)
end
| _ -> Error `Malformed
let many_of_wire sigs =
foldM (fun (acc, msgs) s ->
match of_wire s with
| Ok ((id, _, _, _) as s) ->
if M.mem id acc then begin
let msg = "signature from " ^ id ^ " already present, ignoring" in
Ok (acc, msg :: msgs)
end else
Ok (M.add id s acc, msgs)
| Error (`Unknown_alg a) ->
let msg = "no support for signature algorithm " ^ a ^ ", ignoring" in
Ok (acc, msg :: msgs)
| Error e -> Error (str_pp pp_err e))
(M.empty, []) sigs
let wire_raw (id, created, alg, s) =
let value = alg_to_string alg ^ "=" ^ s in
let open Wire in
List [ Identifier id ; Data created ; Data value ]
end
let to_be_signed data created id alg =
let open Wire in
M.add "created" (Data created)
(M.add "identifier" (Identifier id)
(M.add "sigtype" (Identifier (Signature.alg_to_string alg))
(M.add "data" (Map data) M.empty)))
module Expression = struct
type keyref =
| Remote of identifier * Digest.t * Uint.t
| Local of identifier
let keyref_compare a b =
match a, b with
| Local id, Local id' -> String.compare_insensitive id id'
| Local _, Remote _ -> -1
| Remote _, Local _ -> 1
| Remote (id, dgst, e), Remote (id', dgst', e') ->
match Uint.compare e e', String.compare_insensitive id id', Digest.compare dgst dgst' with
| 0, 0, x -> x
| 0, x, _ -> x
| x, _, _ -> x
module KS = Set.Make(struct
type t = keyref
let compare = keyref_compare
end)
let pp_keyref ppf = function
| Local id -> Format.fprintf ppf "local %a" pp_id id
| Remote (id, digest, epoch) ->
Format.fprintf ppf "remote %a %a %s" pp_id id Digest.pp digest (Uint.to_string epoch)
let keyref_of_wire data =
let open Wire in
match data with
| Identifier id ->
Ok (Local id)
| List [ Identifier id ; digest ; Bigint epoch ] ->
Digest.of_wire digest >>= fun dgst ->
Ok (Remote (id, dgst, epoch))
| _ -> Error `Malformed
let keyref_to_wire =
let open Wire in
function
| Remote (id, digest, epoch) ->
List [ Identifier id ; Digest.wire_raw digest ; Bigint epoch ]
| Local id -> Identifier id
type t =
| Quorum of int * KS.t
| And of t * t
| Or of t * t
let int_compare : int -> int -> int = compare
let rec compare a b =
match a, b with
| Or (a, b), Or (a', b') ->
begin match compare a a', compare b b' with
| 0, x -> x
| x, _ -> x
end
| Or _, _ -> 1 | _, Or _ -> -1
| And (a, b), And (a', b') ->
begin match compare a a', compare b b' with
| 0, x -> x
| x, _ -> x
end
| And _, _ -> 1 | _, And _ -> -1
| Quorum (n, s), Quorum (n', s') ->
match int_compare n n' with
| 0 ->
begin match int_compare (KS.cardinal s) (KS.cardinal s') with
| 0 -> KS.compare s s'
| x -> x
end
| x -> x
let rec equal a b =
match a, b with
| Quorum (n, s), Quorum (n', s') -> n = n' && KS.equal s s'
| And (a, b), And (a', b') -> equal a a' && equal b b'
| Or (a, b), Or (a', b') -> equal a a' && equal b b'
| _ -> false
let rec pp ppf = function
| Quorum (quorum, keys) ->
Format.fprintf ppf "(%d %a)" quorum (pp_list pp_keyref) (KS.elements keys)
| And (a, b) -> Format.fprintf ppf "(%a & %a)" pp a pp b
| Or (a, b) -> Format.fprintf ppf "(%a | %a)" pp a pp b
let rec keys m = function
| Quorum (_, keyrefs) ->
KS.fold
(fun keyref m -> match keyref with
| Local _ -> m
| Remote (id, d, e) -> match M.find id m with
| None -> M.add id (d, e) m
| Some (d', e') when Digest.equal d d' && Uint.compare e e' = 0 -> m
| Some (d', e') ->
Format.printf "WARN: key %s already needed in (epoch %a, digest %a), now (epoch %a, digest %a) [selected higher epoch]\n"
id Uint.pp e' Digest.pp d' Uint.pp e Digest.pp d ;
if Uint.compare e' e = 1 then begin
Format.printf "WARN: replacing existing digest with newer epoch\n" ;
M.add id (d', e') m
end else
m)
keyrefs m
| And (a, b) -> keys (keys m a) b
| Or (a, b) -> keys (keys m a) b
let rec of_wire =
let single w = match keyref_of_wire w with
| Ok kr -> Ok (KS.singleton kr)
| Error (`Unknown_alg a) ->
Printf.printf "WARN: unknown algorithm %s while parsing key (ignoring entry)\n" a ;
Ok KS.empty
| Error e -> Error (str_pp pp_err e)
in
let multi ws =
foldM (fun acc k -> single k >>= fun kr -> Ok (KS.union kr acc))
KS.empty ws
in
function
| Wire.Pair (i, s) ->
Wire.pint i >>= fun i ->
begin match s with
| Wire.List [] -> Ok KS.empty
| Wire.List (Wire.List _ :: _ as keyrefs) -> multi keyrefs
| Wire.Identifier _ -> single s
| Wire.List [ Wire.Identifier _ as e ] -> single e
| Wire.List (Wire.Identifier _ :: Wire.Data _ :: _) -> single s
| Wire.List (Wire.Identifier _ :: _ as ids) -> multi ids
| _ -> Error "malformed"
end >>= fun keyrefs ->
guard (i <= KS.cardinal keyrefs) ("insufficient keys for quorum") >>= fun () ->
Ok (Quorum (i, keyrefs))
| Wire.And (a, b) ->
of_wire a >>= fun a -> of_wire b >>= fun b -> Ok (And (a, b))
| Wire.Or (a, b) ->
of_wire a >>= fun a -> of_wire b >>= fun b -> Ok (Or (a, b))
| Wire.Identifier _
| Wire.List (Wire.Identifier _ :: _) as e ->
single e >>= fun ks -> Ok (Quorum (1, ks))
| _ -> Error "malformed"
let rec to_wire = function
| Quorum (1, s) when KS.cardinal s = 1 -> keyref_to_wire (KS.choose s)
| Quorum (i, s) -> Wire.Pair (Wire.Smallint i, Wire.List (List.map keyref_to_wire (KS.elements s)))
| And (a, b) -> Wire.And (to_wire a, to_wire b)
| Or (a, b) -> Wire.Or (to_wire a, to_wire b)
let hash f id_m expr =
let map = function
| Remote (id, hash, epoch) ->
Ok (id ^ ":" ^ Digest.to_string hash ^ ":" ^ Uint.to_string epoch)
| Local id -> match M.find id id_m with
| None -> Error (id ^ " was not found in provided map")
| Some x -> Ok x
in
let rec to_hash = function
| And (a, b) ->
to_hash a >>= fun ha ->
to_hash b >>= fun hb ->
Ok ("(" ^ ha ^ "&" ^ hb ^ ")")
| Or (a, b) ->
to_hash a >>= fun ha ->
to_hash b >>= fun hb ->
Ok ("(" ^ ha ^ "|" ^ hb ^ ")")
| Quorum (1, s) when KS.cardinal s = 1 ->
map (KS.choose s)
| Quorum (n, s) ->
foldM (fun acc kr ->
map kr >>= fun s ->
Ok (s :: acc))
[] (KS.elements s) >>= fun ks ->
Ok ("(" ^ string_of_int n ^ "[" ^ String.concat "," ks ^ "])")
in
to_hash expr >>= fun s ->
Ok (f s)
let rec eval t keys sigs =
match t with
| Quorum (quorum, ids) ->
let good = KS.fold
(fun keyref acc ->
match keyref with
| Local id -> if S.mem id sigs then succ acc else acc
| Remote (id, digest, e) ->
match Digest_map.find digest keys with
| Some (id', e') when id_equal id id' && Uint.compare e e' = 0 -> succ acc
| _ -> acc)
ids 0
in
good >= quorum
| And (a, b) -> eval a keys sigs && eval b keys sigs
| Or (a, b) -> eval a keys sigs || eval b keys sigs
end
module Root = struct
let version = 1
let supported_roles = [ "timestamp" ; "snapshot" ; "maintainer" ]
type t = {
created : timestamp ;
counter : Uint.t ;
epoch : Uint.t ;
name : identifier ;
datadir : path ;
keydir : path ;
keys : Key.t M.t ;
valid : Expression.t ;
roles : Expression.t M.t ;
signatures : Signature.t M.t ;
}
let t ?(counter = Uint.zero) ?(epoch = Uint.zero) ?(name = "root")
?(datadir = [ "packages" ]) ?(keydir = [ "keys" ]) ?(keys = M.empty)
?(roles = M.empty) ?(signatures = M.empty) created valid =
{ created ; counter ; epoch ; name ; datadir ; keydir ; keys ; roles ; signatures ; valid }
let add_signature t id s =
let signatures = M.add id s t.signatures in
{ t with signatures }
let pp ppf t =
Format.fprintf ppf
"root %s (created %a), datadir %a keydir %a@.@valid %a@.[<2>keys %a@]@.@[<2>roles %a@]@.@[<2> signatures %a@]"
(Header.counter t.counter t.epoch)
pp_timestamp t.created
pp_path t.datadir pp_path t.keydir
Expression.pp t.valid
(M.pp Key.pp) t.keys
(M.pp Expression.pp) t.roles
(M.pp Signature.pp) t.signatures
let safe_role s = guard (String.is_ascii s) ("invalid role")
let of_wire data =
Header.split_signed data >>= fun (signed, sigs) ->
let keys = [ "datadir" ; "keydir" ; "keys" ; "roles" ; "valid" ] in
Header.keys keys signed >>= fun () ->
Header.of_wire signed >>= fun h ->
Header.check `Root version h >>= fun () ->
let open Wire in
opt_err (M.find "datadir" signed) >>= pdata >>= string_to_path >>= fun datadir ->
opt_err (M.find "keydir" signed) >>= pdata >>= string_to_path >>= fun keydir ->
opt_err (M.find "keys" signed) >>= plist >>= Key.many_of_wire >>= fun (keys, w) ->
opt_err (M.find "roles" signed) >>= pmap >>= fun roles ->
opt_err (M.find "valid" signed) >>= Expression.of_wire >>= fun valid ->
Header.keys ~header:false supported_roles roles >>= fun () ->
M.fold (fun k v acc ->
acc >>= fun (map, msgs) ->
safe_role k >>= fun () ->
Expression.of_wire v >>= fun expr ->
if M.mem k map then begin
let msg = "roles with name " ^ k ^ " already present, ignoring" in
Ok (map, msg :: msgs)
end else
Ok (M.add k expr map, msgs))
roles (Ok (M.empty, [])) >>= fun (roles, w') ->
Signature.many_of_wire sigs >>= fun (signatures, w'') ->
let warns = w @ w' @ w'' in
Ok ({ created = h.Header.created ; counter = h.Header.counter ;
epoch = h.Header.epoch ; name = h.Header.name ;
datadir ; keydir ; keys ; roles ; signatures ; valid }, warns)
let wire_raw t =
let open Wire in
let created = t.created
and counter = t.counter
and epoch = t.epoch
and name = t.name
and typ = `Root
in
let = { Header.version ; created ; counter ; epoch ; name ; typ } in
let roles =
M.fold (fun k v acc -> M.add k (Expression.to_wire v) acc) t.roles M.empty
in
M.add "datadir" (Data (path_to_string t.datadir))
(M.add "keydir" (Data (path_to_string t.keydir))
(M.add "keys" (List (M.fold (fun _ key acc -> Key.wire_raw key :: acc) t.keys []))
(M.add "valid" (Expression.to_wire t.valid)
(M.add "roles" (Map roles)
(Header.wire header)))))
let wire t =
let open Wire in
M.add "signed" (Map (wire_raw t))
(M.add "signatures" (List (M.fold (fun _ s acc -> Signature.wire_raw s :: acc) t.signatures []))
M.empty)
end
module Delegation = struct
type t = {
paths : path list ;
valid : Expression.t ;
terminating : bool
}
let equal a b =
List.length a.paths = List.length b.paths &&
List.for_all (fun p -> List.exists (path_equal p) b.paths) a.paths &&
Expression.equal a.valid b.valid &&
a.terminating = b.terminating
let pp ppf t =
Format.fprintf ppf "delegation (terminating %b) paths %a@.keys %a@."
t.terminating
(pp_list pp_path) t.paths
Expression.pp t.valid
let of_wire wire =
let open Wire in
pmap wire >>= fun delegation ->
Header.keys ~header:false [ "paths" ; "valid" ; "terminating" ] delegation >>= fun () ->
opt_err (M.find "paths" delegation) >>= plist >>= fun paths ->
opt_err (M.find "valid" delegation) >>= Expression.of_wire >>= fun valid ->
(match M.find "terminating" delegation with
| None -> Ok true
| Some x -> pdata x >>= function
| "true" | "yes" -> Ok true
| "false" | "no" -> Ok false
| x -> Error ("unkown value for terminating " ^ x)) >>= fun terminating ->
foldM (fun acc p -> pdata p >>= string_to_path >>= fun s -> Ok (s :: acc)) [] paths >>= fun paths ->
let paths = List.rev paths in
Ok { paths ; valid ; terminating }
let wire_raw t =
let open Wire in
let map =
M.add "paths" (List (List.map (fun p -> Data (path_to_string p)) t.paths))
(M.add "valid" (Expression.to_wire t.valid)
(M.add "terminating" (Data (if t.terminating then "true" else "false"))
M.empty))
in
Map map
end
module Target = struct
type t = {
filename : path ;
digest : Digest.t list ;
size : Uint.t ;
}
let equal t t' =
path_equal t.filename t'.filename && Uint.compare t.size t'.size = 0 &&
List.length t.digest = List.length t'.digest &&
List.for_all (fun d -> List.exists (Digest.equal d) t'.digest) t.digest
let pp ppf t =
Format.fprintf ppf "%a (%s bytes) %a"
pp_path t.filename
(Uint.decimal t.size)
(pp_list Digest.pp) t.digest
let valid_opam_path t =
match t.filename with
| [ pname ; pversion ; "opam" ] | [ pname ; pversion ; "files" ; _ ] ->
String.is_prefix ~prefix:(pname ^ ".") pversion
| [ _ ; "opam" ] | [ _ ; "files" ; _ ] -> true
| _ -> false
let of_wire wire =
let open Wire in
pmap wire >>= fun target ->
Header.keys ~header:false [ "filename" ; "digest" ; "size" ] target >>= fun () ->
opt_err (M.find "filename" target) >>= pdata >>= string_to_path >>= fun filename ->
opt_err (M.find "size" target) >>= puint >>= fun size ->
opt_err (M.find "digest" target) >>= plist >>= fun digest ->
foldM (fun acc d -> match Digest.of_wire d with
| Ok d -> Ok (d :: acc)
| Error (`Unknown_alg a) ->
Printf.printf "WARN: ignoring unknown digest algorithm %s\n" a ;
Ok acc
| Error e -> Error (str_pp pp_err e))
[] digest >>= fun digest ->
let digest = List.rev digest in
Ok { filename ; size ; digest }
let wire_raw t =
let open Wire in
let map =
M.add "filename" (Data (path_to_string t.filename))
(M.add "size" (Bigint t.size)
(M.add "digest" (List (List.map Digest.wire_raw t.digest))
M.empty))
in
Map map
end
module Targets = struct
let version = 0
type t = {
created : timestamp ;
counter : Uint.t ;
epoch : Uint.t ;
name : identifier ;
keys : Key.t M.t ;
valid : Expression.t ;
delegations : Delegation.t list ;
targets : Target.t list ;
signatures : Signature.t M.t ;
}
let t ?(counter = Uint.zero) ?(epoch = Uint.zero) ?(keys = M.empty)
?(delegations = []) ?(targets = []) ?(signatures = M.empty) created name valid
= { created ; counter ; epoch ; name ; keys ; valid ; delegations ;
targets ; signatures }
let add_signature t id s =
let signatures = M.add id s t.signatures in
{ t with signatures }
let equal t t' =
timestamp_equal t.created t'.created &&
Uint.compare t.counter t'.counter = 0 &&
Uint.compare t.epoch t'.epoch = 0 &&
name_equal t.name t'.name &&
M.equal Key.equal t.keys t'.keys &&
Expression.equal t.valid t'.valid &&
List.length t.delegations = List.length t'.delegations &&
List.for_all (fun d -> List.exists (Delegation.equal d) t'.delegations) t.delegations &&
List.length t.targets = List.length t'.targets &&
List.for_all (fun t -> List.exists (Target.equal t) t'.targets) t.targets &&
M.equal Signature.equal t.signatures t'.signatures
let pp ppf t =
Format.fprintf ppf "targets %a %s (created %a)@.@[<2>keys %a@]@.@[<2>valid %a@]@.@[<2>delegations %a@]@.@[<2>targets %a@]@.@[<2>signatures %a@]"
pp_name t.name
(Header.counter t.counter t.epoch)
pp_timestamp t.created
(M.pp Key.pp) t.keys
Expression.pp t.valid
(pp_list Delegation.pp) t.delegations
(pp_list Target.pp) t.targets
(M.pp Signature.pp) t.signatures
let of_wire data =
Header.split_signed data >>= fun (signed, sigs) ->
let keys = [ "keys" ; "valid" ; "delegations" ; "targets" ] in
Header.keys keys signed >>= fun () ->
Header.of_wire signed >>= fun h ->
Header.check `Targets version h >>= fun () ->
let open Wire in
(match M.find "keys" signed with
| None -> Ok (M.empty, [])
| Some keys -> plist keys >>= Key.many_of_wire) >>= fun (keys, w) ->
opt_err (M.find "valid" signed) >>= Expression.of_wire >>= fun valid ->
opt_err (M.find "delegations" signed) >>= plist >>= fun delegations ->
opt_err (M.find "targets" signed) >>= plist >>= fun targets ->
foldM (fun acc d -> Delegation.of_wire d >>= fun d -> Ok (d :: acc)) [] delegations >>= fun delegations ->
let delegations = List.rev delegations in
foldM (fun acc t -> Target.of_wire t >>= fun t -> Ok (t :: acc)) [] targets >>= fun targets ->
let targets = List.rev targets in
Signature.many_of_wire sigs >>= fun (signatures, w') ->
let warn = w @ w' in
Ok ({ created = h.Header.created ; counter = h.Header.counter ;
epoch = h.Header.epoch ; name = h.Header.name ;
keys ; valid ; delegations ; targets ; signatures }, warn)
let wire_raw t =
let open Wire in
let created = t.created
and counter = t.counter
and epoch = t.epoch
and name = t.name
and typ = `Targets
in
let = { Header.version ; created ; counter ; epoch ; name ; typ } in
M.add "keys" (List (M.fold (fun _ key acc -> Key.wire_raw key :: acc) t.keys []))
(M.add "valid" (Expression.to_wire t.valid)
(M.add "delegations" (List (List.map Delegation.wire_raw t.delegations))
(M.add "targets" (List (List.map Target.wire_raw t.targets))
(Header.wire header))))
let wire t =
let open Wire in
M.add "signed" (Map (wire_raw t))
(M.add "signatures" (List (M.fold (fun _ s acc -> Signature.wire_raw s :: acc) t.signatures []))
M.empty)
end