-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.v
1836 lines (1651 loc) · 55.9 KB
/
main.v
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Coq.Lists.List.
Require Import Coq.Strings.String.
Require Import Coq.Strings.Ascii.
Require Import Coq.Bool.Bool.
Require Import Strlib.
Require Import Notations Logic Datatypes.
Require Export Setoid.
Require Import LibTactics.
Require Import Coq.omega.Omega.
Import ListNotations.
Definition NameComp := string.
Definition Name := list NameComp.
Fixpoint beq_name (n1:Name) (n2:Name) : bool :=
match n1,n2 with
| [],[] => true
| h1::t1, h2::t2 => if beq_string h1 h2 then beq_name t1 t2 else false
| _, _ => false
end.
Fixpoint nameToString (name:Name) : string :=
match name with
| h :: t => h
| _ => ""
end.
Inductive Data : Type :=
| wraped_data : Name -> Name -> Data -> Data
| data : Name -> Name -> Data.
Inductive RuleName : Type :=
| ruleName : string -> RuleName.
Inductive MatchComp : Type :=
| mc_wild : MatchComp
| mc_sequence_wild : string -> MatchComp
| mc_exact : string -> MatchComp
| mc_indexed : MatchComp -> MatchComp.
(* you can't have index inside a index *)
Definition MatchPattern := list MatchComp.
Inductive MatchCompMatch : MatchComp -> NameComp -> Prop :=
| mcm_wild : forall x, MatchCompMatch mc_wild x
| mcm_exact : forall x, MatchCompMatch (mc_exact x) x
| mcm_index : forall x y, MatchCompMatch x y -> MatchCompMatch (mc_indexed x) y
| mcm_seq : forall x y, x <> y -> MatchCompMatch (mc_sequence_wild x) y
.
Inductive regMatch : MatchPattern -> Name -> Prop :=
| Msingle : forall x y, MatchCompMatch x y -> regMatch [x] [y]
| Mseq : forall n1 n2 s1,
regMatch [(mc_sequence_wild s1)] n1 ->
regMatch [(mc_sequence_wild s1)] n2 ->
regMatch [(mc_sequence_wild s1)] (n1 ++ n2)
| MApp : forall s1 r1 s2 r2,
regMatch r1 s1 ->
regMatch r2 s2 ->
regMatch (r1 ++ r2) (s1 ++ s2).
Local Open Scope string_scope.
Example mcm1 : MatchCompMatch (mc_indexed (mc_sequence_wild "blog")) "a".
Proof.
apply mcm_index. apply mcm_seq. apply string_neq_ref. reflexivity.
Qed.
Inductive RuleParameter : Type :=
| rp_indexed : nat -> RuleParameter
| rp_prefixOfIndexed : nat -> nat -> RuleParameter.
Inductive RuleCall : Type :=
| ruleCall : RuleName -> list RuleParameter -> RuleCall.
Inductive Action : Type :=
(* TryElse is temporarily removed, it introduces too much problems when proofing *)
(* | actTryElse : RuleCall -> RuleCall -> Action *)
(* try expr1, if authentication failed, unwrap data and try Rule *)
| actRc : RuleCall -> Action
| actOrAnchor : RuleCall -> RuleCall -> Action
| actAnchor : string -> Action.
(*
if first pattern match, use first one,
if pattern does not match,
or get an exception of noMorePrefix,
try second anchor call
*)
Inductive Expr : Type :=
| expr : RuleName -> MatchPattern -> Action -> Expr.
Inductive ExprMiddle : Type :=
| exprm : RuleName -> MatchPattern -> Action -> option nat -> ExprMiddle.
(* last nat is nToShrink *)
Inductive ExprLabeled : Type :=
| exprl : RuleName -> MatchPattern -> Action -> nat -> ExprLabeled.
Definition Program := list Expr.
Definition ProgramMiddle := list ExprMiddle.
Definition ProgramLabeled := list ExprLabeled.
Example sampleRuleName: RuleName := ruleName "test".
Example sampleProgram1 :=
[(expr (ruleName "article")
[(mc_indexed (mc_sequence_wild "blog"));
(mc_exact "blog");
(mc_exact "article")]
(actRc (ruleCall (ruleName "author") [(rp_indexed 1)])
)
);
(expr (ruleName "author")
[(mc_indexed (mc_sequence_wild "blog"));
(mc_exact "blog");
(mc_exact "article")]
(actRc (ruleCall (ruleName "article") [(rp_prefixOfIndexed 1 1)])
)
)
].
(* firstPart, rest(targetIncluded) *)
Fixpoint findFirst (target:string) (nm:Name) : Name * Name :=
match nm with
| [] => ([], [])
| h :: t => match (beq_string target h) with
| true => ([], h :: t)
| false => let (matched, rest):= (findFirst target t) in
(h :: matched, rest)
end
end.
Compute findFirst "test"%string [ "a"%string;
"b"%string;
"c"%string;
"test"%string;
"e"%string
].
(* Fixpoint getExactContent (mc:MatchComp) : string := *)
(* match mc with *)
(* | mc_wild => ""%string *)
(* | mc_sequence_wild => ""%string *)
(* | mc_exact s => s *)
(* | mc_indexed mc' => getExactContent mc' *)
(* end. *)
Fixpoint isMatch (nm:Name) (mp:MatchPattern) : bool * (list Name) :=
match mp with
| [] => match nm with
| [] => (true, [])
| h :: t => (false, [])
end
| h :: t => match h with
| mc_wild => isMatch (tl nm) t
| mc_sequence_wild s => let (part1, rest) := findFirst s nm in
(isMatch rest t)
| mc_exact ss => match nm with
| [] => (false, [])
| hd_nm :: tl_nm =>
if (beq_string ss hd_nm)
then isMatch (tl_nm) t
else (false, [])
end
| mc_indexed nmx => match nmx with
| mc_wild => let (isOk, indexedList) := isMatch (tl nm) t in
(isOk, [(hd ""%string nm)] :: indexedList)
| mc_exact ss => match nm with
| [] => (false, [])
| hd_nm :: tl_nm => if (beq_string ss hd_nm)
then
let (isOk, indexedList) := isMatch tl_nm t in
(isOk, [ss]::indexedList)
else (false, [])
end
| mc_sequence_wild s => let (indexedName, rest) := findFirst s nm in
let (isOk, indexedList) := isMatch rest t in
(isOk, indexedName :: indexedList)
| _ => (false, [])
end
end
end.
Example isMatch_test1 : isMatch ["a";"b";"c";"d"] [(mc_indexed (mc_sequence_wild "c"));
(mc_exact "c");
(mc_exact "d")] = (true, [["a";"b"]]).
Proof.
simpl. reflexivity.
Qed.
Example isMatch_test2 : isMatch ["a";"b";"c";"d"] [(mc_indexed (mc_sequence_wild ""))] = (true, [["a";"b";"c";"d"]]).
Proof.
simpl. reflexivity.
Qed.
Example isMatch_test3 : isMatch ["a";"b";"c";"d"] [(mc_indexed (mc_sequence_wild "e"));
(mc_exact "e")]
= (false, [["a";"b";"c";"d"]]).
Proof.
simpl. reflexivity.
Qed.
Example isMatch_test4 : isMatch ["a";"b";"c";"d"] [(mc_indexed (mc_sequence_wild "c"));
(mc_exact "c")]
= (false, [["a";"b"]]).
Proof.
simpl. reflexivity.
Qed.
Example isMatch_test5 : isMatch ["a";"b";"c";"d"] [(mc_indexed (mc_sequence_wild "a"));
(mc_exact "a");(mc_sequence_wild "")]
= (true, [[]]).
Proof.
simpl. reflexivity.
Qed.
Definition Network := list Data.
Definition empty_name : Name := [""].
Definition empty_data : Data := data empty_name empty_name.
Definition empty_expr : Expr := (expr (ruleName "") []
(actRc (ruleCall (ruleName "") []))).
Definition empty_expr_middle: ExprMiddle := (exprm (ruleName "") []
(actRc (ruleCall (ruleName "") []))
None).
Definition empty_expr_labeled : ExprLabeled := (exprl (ruleName "") []
(actRc (ruleCall (ruleName "") []))
0).
(* leave this part for later, right now, if not exist, just return False *)
(* Fixpoint ruleDefined (nm:string) (prog:Program) := *)
(* match prog with *)
(* | [] => false *)
(* | e::t => let '(expr (ruleName rn) mp act) := e in *)
(* end. *)
(* Fixpoint syntaxCheck (prog:Program) : bool := *)
(* match prog with *)
(* | [] => true *)
(* | e::t => let '(expr (ruleName rn) mp act) := e in *)
(* if ruleDefined rn prog *)
(* then *)
(* else false *)
(* end. *)
(*
syntax check:
1. ruleCall exist
2. indexedValue used is no more than indexed in matchPattern
3. substitution call is no more than indexed in matchPattern
*)
Fixpoint hasPrefix (para:list RuleParameter) : bool :=
match para with
| [] => false
| p::t => match p with
| rp_indexed _ => false
| rp_prefixOfIndexed _ _ => true
end
end.
Lemma hasPrefixHd : forall h t,
hasPrefix (h::t) = true ->
(exists x y, h = rp_prefixOfIndexed x y).
Proof.
intros.
unfold hasPrefix in H.
destruct h.
inversion H.
exists n n0. eauto.
Qed.
Example hasPrefixTest1 : hasPrefix [(rp_prefixOfIndexed 1 1)] = true.
Proof.
reflexivity.
Qed.
Fixpoint actionOf (name:string) (prog:Program) : Action :=
match prog with
| [] => actRc (ruleCall (ruleName "RuleNotExist") [])
| e::t => match e with
| expr (ruleName rname) _ act => if beq_string rname name
then act
else actionOf name t
end
end.
Fixpoint has1stArg (para:list RuleParameter) : bool :=
match para with
| [] => false
| p::t => match p with
| rp_indexed _ => true
| rp_prefixOfIndexed _ n => Nat.leb n 1
end
end.
Fixpoint mustHave1stArg (prog:Program) : bool :=
match prog with
| [] => true
| (expr _ _ act)::t => match act with
| actOrAnchor (ruleCall _ para) _ =>
if has1stArg para then mustHave1stArg t
else false
| actRc (ruleCall _ para) => if has1stArg para then mustHave1stArg t
else false
| actAnchor _ => mustHave1stArg t
end
end.
Fixpoint hasActLoop (rname:string) (act:Action) (limit:nat) (prog:Program) : bool :=
match limit with
| 0 => false
| S n' =>
match act with
| actOrAnchor (ruleCall (ruleName rcname) para) _ => if hasPrefix para then false else
(if (beq_string rcname rname) then true
else hasActLoop rname (actionOf rcname prog) n' prog)
| actRc (ruleCall (ruleName rcname) para) => if hasPrefix para then false else
(if (beq_string rcname rname) then true
else hasActLoop rname (actionOf rcname prog) n' prog)
| actAnchor _ => false
end
end.
Fixpoint hasLoop (e:Expr) (prog:Program) : bool :=
let n := List.length prog in
match e with
| expr (ruleName s) mp act => hasActLoop s act n prog
end.
Fixpoint checkNoLoopImpl (prog:Program) (progConst:Program) : bool :=
match prog with
| [] => true
| e::t => if (hasLoop e progConst)
then false
else checkNoLoopImpl t progConst
end.
Definition noLoop (prog:Program) := checkNoLoopImpl prog prog.
Example checkLoopTest1: noLoop sampleProgram1 = true.
Proof.
unfold noLoop. simpl. reflexivity.
Qed.
Example sampleProgram2 :=
[(expr (ruleName "article")
[(mc_indexed (mc_sequence_wild "blog"));
(mc_exact "blog");
(mc_exact "article")]
(actRc (ruleCall (ruleName "author") [(rp_indexed 1)])
)
);
(expr (ruleName "author")
[(mc_indexed (mc_sequence_wild "blog"));
(mc_exact "blog");
(mc_exact "article")]
(actRc (ruleCall (ruleName "article") [(rp_indexed 1)])
)
)
].
Example checkLoopTest2 : noLoop sampleProgram2 = false.
Proof.
reflexivity.
Qed.
Example sampleProgram3 :=
[(expr (ruleName "article")
[(mc_indexed (mc_sequence_wild "blog"))]
(actRc (ruleCall (ruleName "author") [(rp_indexed 1)])
)
);
(expr (ruleName "author")
[(mc_indexed (mc_sequence_wild "blog"))]
(actRc (ruleCall (ruleName "admin") [(rp_indexed 1)])
)
);
(expr (ruleName "admin")
[(mc_indexed (mc_sequence_wild "blog"))]
(actRc (ruleCall (ruleName "author") [(rp_prefixOfIndexed 1 1)])
)
)
].
Example checkLoopTest3 : noLoop sampleProgram3 = true.
Proof.
reflexivity.
Qed.
(* loop-trust: done *)
Fixpoint hasActAnchor (rname:string) (act:Action) (limit:nat) (prog:Program) : bool :=
match limit with
| 0 => false
| S n' =>
match act with
| actOrAnchor _ _ => true
| actRc (ruleCall (ruleName rcname) para) => (hasActAnchor rcname (actionOf rcname prog) n' prog)
| actAnchor _ => true
end
end.
Fixpoint hasAnchorCheck (e:Expr) (prog:Program) : bool :=
let n := List.length prog in
match e with
| expr (ruleName s) mp act => hasActAnchor s act n prog
end.
Fixpoint hasAnchorImpl (prog:Program) (progConst:Program) : bool :=
match prog with
| [] => true
| e::t => if (hasAnchorCheck e progConst)
then hasAnchorImpl t progConst
else false
end.
Definition hasAnchor (prog:Program) := hasAnchorImpl prog prog.
Example hasAnchorTest1 : hasAnchor sampleProgram1 = false.
Proof.
reflexivity.
Qed.
Example hasAnchorTest2 : hasAnchor sampleProgram2 = false.
Proof.
reflexivity.
Qed.
Example hasAnchorTest3 : hasAnchor sampleProgram3 = false.
Proof.
reflexivity.
Qed.
Example sampleProgram4 :=
[(expr (ruleName "article")
[(mc_indexed (mc_sequence_wild "blog"))]
(actRc (ruleCall (ruleName "author") [(rp_indexed 1)])
)
);
(expr (ruleName "author")
[(mc_indexed (mc_sequence_wild "blog"))]
(actRc (ruleCall (ruleName "admin") [(rp_indexed 1)])
)
);
(expr (ruleName "admin")
[(mc_indexed (mc_sequence_wild "blog"))]
(actOrAnchor (ruleCall (ruleName "author") [(rp_prefixOfIndexed 1 1)])
(ruleCall (ruleName "root") [])
)
);
(expr (ruleName "root")
[(mc_indexed (mc_sequence_wild "blog"))]
(actAnchor "/usr/local/key1")
)
].
Example hasAnchorTest4 : hasAnchor sampleProgram4 = true.
Proof.
reflexivity.
Qed.
(* recursive dependecy on trust anchor: done*)
Fixpoint noSameAnchorBeforeBack (prog:Program) (anchorName:string) (homeName:string) (limit:nat) (act:Action) :=
match limit with
| 0 => false
| S n' =>
match act with
| actAnchor _ => true
| actOrAnchor (ruleCall (ruleName nextRule) _) (ruleCall (ruleName anchorRule) _) =>
if beq_string anchorRule anchorName
then false
else (if beq_string homeName nextRule
then true
else (noSameAnchorBeforeBack prog anchorName homeName n' (actionOf nextRule prog))
)
| actRc (ruleCall (ruleName nextRule) para) =>
(if beq_string homeName nextRule
then true
else (if beq_string nextRule anchorName
then false
else (noSameAnchorBeforeBack prog anchorName homeName n' (actionOf nextRule prog ))
)
)
end
end.
Fixpoint satisfyLppCheck (e:Expr) (prog:Program) : bool :=
let n := List.length prog in
match e with
| expr (ruleName exprName) mp act =>
match act with
| actOrAnchor (ruleCall (ruleName nextRule) _) (ruleCall (ruleName anchorRule) _) =>
noSameAnchorBeforeBack prog anchorRule exprName n (actionOf nextRule prog)
| _ => true
end
end.
Fixpoint satisfyLppImpl (prog:Program) (progConst:Program) : bool :=
match prog with
| [] => true
| e::t => if (satisfyLppCheck e progConst)
then satisfyLppImpl t progConst
else false
end.
Definition satisfyLpp (prog:Program) := satisfyLppImpl prog prog.
Example satisfyLppTest1 : satisfyLpp sampleProgram1 = true.
Proof.
reflexivity.
Qed.
Example satisfyLppTest2 : satisfyLpp sampleProgram2 = true.
Proof.
reflexivity.
Qed.
Example satisfyLppTest3 : satisfyLpp sampleProgram3 = true.
Proof.
reflexivity.
Qed.
Example satisfyLppTest4 : satisfyLpp sampleProgram4 = true.
Proof.
reflexivity.
Qed.
Example sampleProgram5 :=
[
(expr (ruleName "sub.foo.com")
[]
(actRc (ruleCall (ruleName "foo.com") [(rp_indexed 1)])
)
);
(expr (ruleName "foo.com")
[]
(actRc (ruleCall (ruleName ".com") [(rp_indexed 1)])
)
);
(expr (ruleName "bar.com")
[]
(actOrAnchor (ruleCall (ruleName "foo.com") [(rp_prefixOfIndexed 1 1)])
(ruleCall (ruleName ".com") [])
)
);
(expr (ruleName ".com")
[]
(actAnchor "/usr/local/key1")
)
].
Example satisfyLppTest5 : satisfyLpp sampleProgram5 = false.
Proof.
reflexivity.
Qed.
(* least privilege principle: done*)
(* Lemma regEquivlence : forall name p, regMatch p name <-> fst (isMatch name p) = true. *)
(* Proof. *)
(* Admitted. *)
Inductive Rtn : Type :=
| keyNotMatch : Rtn
| authFail : Rtn
| networkFail : Rtn
| succ : Rtn
| noMatchingRule : Rtn
| noMorePrefix : Rtn
| debug1 : Rtn
| debug2 : Rtn
| rtnDebugExpr : Expr -> Rtn
| rtnDebugAny : forall X, X -> Rtn
.
Definition getKeyLocator (data:Data) : Name :=
match data with
| data name key => key
| wraped_data name key _ => key
end.
Definition getName (data:Data) : Name :=
match data with
| data name key => name
| wraped_data name key _ => name
end.
Definition unwrap (data:Data) : option Data :=
match data with
| data _ _ => None
| wraped_data _ _ d => Some d
end.
(* one more check on that data is mostly wraped once *)
Fixpoint getPrefix (nm:Name) (n:nat) : option Name :=
let len := length nm in
match Nat.ltb len n with
| true => None
| false => match Nat.eqb len n with
| true => Some []
| false => match nm with
| h::t => match (getPrefix t n) with
| None => None
| Some rtn => Some (h::rtn)
end
| _ => None
end
end
end.
Lemma getPrefixLt :
forall nm n nm',
(getPrefix nm n = Some nm') ->
length nm' < length nm.
Proof.
Admitted.
Example getPrefixTest1 : getPrefix ["a";"b";"c";"d"] 2 = Some ["a";"b"].
Proof.
reflexivity.
Qed.
Example getPrefixTest2 : getPrefix ["c";"d"] 2 = Some [].
Proof.
reflexivity.
Qed.
Example getPrefixTest3 : getPrefix ["c";"d"] 3 = None.
Proof.
reflexivity.
Qed.
Example getPrefixTest4 : getPrefix [] 1 = None.
Proof.
reflexivity.
Qed.
Fixpoint getNinList (n:nat) {T:Type} (l:list T) : option T :=
match n with
| 0 => match l with
| [] => None
| h :: t => Some h
end
| S n' => match l with
| [] => None
| h :: t => getNinList n' t
end
end.
Check [].
Lemma getNinListEmptyList : forall n T (l:list T),
(l = []) ->
getNinList n l = None.
Proof.
intros.
rewrite H.
unfold getNinList.
destruct n. eauto. eauto.
Qed.
Fixpoint genArgs (indexed:list Name) (rp: list RuleParameter) : option (list Name) :=
match rp with
| [] => Some []
| h::t => match h with
| rp_indexed n =>
match indexed with
| [] => None
| hdIndexed::tlIndexed =>
match (genArgs tlIndexed t) with
| None => None
| Some rtn => Some (hdIndexed :: rtn)
end
end
| rp_prefixOfIndexed n minusN =>
match indexed with
| [] => None
| hdIndexed::tlIndexed =>
match (getPrefix hdIndexed minusN) with
| None => None
| Some prefix => match (genArgs tlIndexed t) with
| None => None
| Some rtn => Some (prefix::rtn)
end
end
end
end
end.
(* Fixpoint getExpr (prog:Program) (name:RuleName) : Expr := *)
(* match prog with *)
(* | [] => empty_expr *)
(* | e::t => let '(expr (ruleName ename) _ _) := e in *)
(* let '(ruleName rname) := name in *)
(* if beq_string ename rname *)
(* then e *)
(* else getExpr t name *)
(* end. *)
Fixpoint getExprM (prog:ProgramMiddle) (name:RuleName) : ExprMiddle :=
match prog with
| [] => empty_expr_middle
| e::t => let '(exprm (ruleName ename) _ _ _) := e in
let '(ruleName rname) := name in
if beq_string ename rname
then e
else getExprM t name
end.
Fixpoint getExpr (prog:ProgramLabeled) (name:RuleName) : ExprLabeled :=
match prog with
| [] => empty_expr_labeled
| e::t => let '(exprl (ruleName ename) _ _ _) := e in
let '(ruleName rname) := name in
if beq_string ename rname
then e
else getExpr t name
end.
(* Example getExprTest1 : getExpr sampleProgram5 (ruleName ".com") = (expr (ruleName ".com") [] (actAnchor "/usr/local/key1")). *)
(* Proof. *)
(* reflexivity. *)
(* Qed. *)
Fixpoint getKey (net:Network) (data:Data) :=
match net with
| [] => empty_data
| h::t => if beq_name (getName h) (getKeyLocator data)
then h
else getKey t data
end.
Fixpoint argTest (arg1:list Name) (arg2:list Name) : bool :=
match arg1,arg2 with
| _, [] => true
| h1::t1,h2::t2 => if beq_name h1 h2
then argTest t1 t2
else false
| _,_ => false
end.
Fixpoint interpr_findMatchRule (prog:Program) (data:Data) : option Expr :=
match prog with
| [] => None
| h::t =>
let '(expr rname mp act) := h in
let '(bMatch, indexed) := isMatch (getName data) mp in
match bMatch with
| false => interpr_findMatchRule t data
| true => Some h
end
end.
Example sampleProgram6 :=
[(expr (ruleName "article")
[(mc_indexed (mc_sequence_wild "blog"));(mc_exact "blog");(mc_exact "article");(mc_wild)]
(actRc (ruleCall (ruleName "author") [(rp_indexed 1)]))
);
(expr (ruleName "author")
[(mc_indexed (mc_sequence_wild "blog"));(mc_exact "blog");(mc_exact "author");(mc_wild)]
(actRc (ruleCall (ruleName "admin") [(rp_indexed 1)]))
);
(expr (ruleName "admin")
[(mc_indexed (mc_sequence_wild "blog"));(mc_exact "blog");(mc_exact "admin");(mc_wild)]
(actRc (ruleCall (ruleName "root") [(rp_indexed 1)]))
);
(expr (ruleName "root")
[(mc_indexed (mc_sequence_wild "blog"));(mc_exact "blog");(mc_exact "KEY");(mc_wild)]
(actAnchor "/usr/local/key1")
)
].
Example dataArticle := data ["domain";"test";"blog";"article";"1"] ["domain";"test";"blog";"author";"1"].
Example dataAuthor := data ["domain";"test";"blog";"author";"1"] ["domain";"test";"blog";"admin";"1"].
Example dataAdmin:= data ["domain";"test";"blog";"admin";"1"] ["domain";"test";"blog";"KEY";"1"].
Example dataKey := data ["domain";"test";"blog";"KEY";"1"] ["/usr/local/key1"].
Example blogNet := [dataKey; dataAdmin; dataAuthor; dataArticle].
(* Example interpreterTest1 : interpr_main sampleProgram6 10 blogNet dataArticle = Some succ. *)
(* Proof. *)
(* reflexivity. *)
(* Qed. *)
(* -------------------------------- example NDNS --------------------------- *)
(* This example includes use of TryElse rule, now it's temporarily removed *)
(* Example sampleProgramNdns := *)
(* [(expr (ruleName "CacheResolver") *)
(* [(mc_exact "NDNS-R");(mc_sequence_wild "")] *)
(* (actTryElse (ruleCall (ruleName "Local") []) (ruleCall (ruleName "NDNS-data") [])) *)
(* ); *)
(* (expr (ruleName "NDNS-data") *)
(* [(mc_indexed (mc_sequence_wild "NDNS"));(mc_exact "NDNS");(mc_wild);(mc_exact "NS");(mc_indexed (mc_sequence_wild ""))] *)
(* (actRc (ruleCall (ruleName "NDNS-DSK") [(rp_indexed 1)])) *)
(* ); *)
(* (expr (ruleName "NDNS-DSK") *)
(* [(mc_indexed (mc_sequence_wild "NDNS"));(mc_exact "NDNS");(mc_exact "DSK");(mc_indexed (mc_sequence_wild ""))] *)
(* (actRc (ruleCall (ruleName "NDNS-KSK") [(rp_indexed 1)])) *)
(* ); *)
(* (expr (ruleName "NDNS-KSK") *)
(* [(mc_indexed (mc_sequence_wild "NDNS"));(mc_exact "NDNS");(mc_exact "KSK");(mc_indexed (mc_sequence_wild ""))] *)
(* (actOrAnchor (ruleCall (ruleName "NDNS-DKEY") [(rp_prefixOfIndexed 1 1)]) *)
(* (ruleCall (ruleName "NDNS-Root") [])) *)
(* ); *)
(* (expr (ruleName "NDNS-DKEY") *)
(* [(mc_indexed (mc_sequence_wild "NDNS"));(mc_exact "NDNS");(mc_wild);(mc_exact "DKEY");(mc_indexed (mc_sequence_wild ""))] *)
(* (actRc (ruleCall (ruleName "NDNS-DSK") [(rp_indexed 1)])) *)
(* ); *)
(* (expr (ruleName "Local") *)
(* [(mc_exact "NDNS-R");(mc_sequence_wild "")] *)
(* (actAnchor "/usr/local/ucla/key") *)
(* ); *)
(* (expr (ruleName "NDNS-Root") *)
(* [(mc_exact "NDNS");(mc_sequence_wild "")] *)
(* (actAnchor "/usr/local/dns/key") *)
(* ) *)
(* ]. *)
(* "NDNS-R/com/ucla/NDNS/www/NS/v1/s1" *)
(* "usr/local/ucla/key" *)
(* "com/ucla/NDNS/www/NS/v1/s1" *)
(* "com/ucla/NDNS/DSK/CERT/1" *)
(* "com/ucla/NDNS/KSK/CERT/1" *)
(* "com/NDNS/ucla/DKEY/CERT/1" *)
(* "com/NDNS/DSK/CERT/1" *)
(* "com/NDNS/KSK/CERT/1" *)
(* "NDNS/com/DKEY/CERT/1" *)
(* "NDNS/DSK/CERT/1" *)
(* "NDNS/KSK/CERT/1" *)
(* "usr/local/dns/key" *)
(* Example data0 := data ["com";"ucla";"NDNS";"www";"NS";"v1";"s1"] ["com";"ucla";"NDNS";"DSK";"CERT";"1"]. *)
(* Example data1 := data ["com";"ucla";"NDNS";"DSK";"CERT";"1"] ["com";"ucla";"NDNS";"KSK";"CERT";"1"]. *)
(* Example data2 := data ["com";"ucla";"NDNS";"KSK";"CERT";"1"] ["com";"NDNS";"ucla";"DKEY";"CERT";"1"]. *)
(* Example data3 := data ["com";"NDNS";"ucla";"DKEY";"CERT";"1"] ["com";"NDNS";"DSK";"CERT";"1"]. *)
(* Example data4 := data ["com";"NDNS";"DSK";"CERT";"1"] ["com";"NDNS";"KSK";"CERT";"1"]. *)
(* Example data5 := data ["com";"NDNS";"KSK";"CERT";"1"] ["NDNS";"com";"DKEY";"CERT";"1"]. *)
(* Example data6 := data ["NDNS";"com";"DKEY";"CERT";"1"] ["NDNS";"DSK";"CERT";"1"]. *)
(* Example data7 := data ["NDNS";"DSK";"CERT";"1"] ["NDNS";"KSK";"CERT";"1"]. *)
(* Example data8 := data ["NDNS";"KSK";"CERT";"1"] ["/usr/local/dns/key"]. *)
(* Example dataR := wraped_data ["NDNS-R";"com";"ucla";"NDNS";"www";"NS";"v1";"s1"] ["/usr/local/mit/key"] data0. *)
(* Example NdnsNet := [data0;data1;data2;data3;data4;data5;data6;data7;data8;dataR]. *)
(* Example interpreterTest2 : interpr_main sampleProgramNdns 100 NdnsNet dataR = Some succ. *)
(* Proof. *)
(* reflexivity. *)
(* Qed. *)
(* -----------------------end of example NDNS -------------------------------- *)
Fixpoint exprLength (prog:Program) : nat :=
match prog with
| [] => 0
| h::t => S (exprLength t)
end.
(* Inductive Action : Type := *)
(* (* TryElse is temporarily removed, it introduces too much problems when proofing *) *)
(* (* | actTryElse : RuleCall -> RuleCall -> Action *) *)
(* (* try expr1, if authentication failed, unwrap data and try Rule *) *)
(* | actRc : RuleCall -> Action *)
(* | actOrAnchor : RuleCall -> RuleCall -> Action *)
(* | actAnchor : string -> Action. *)
Definition exprInitLabel (expr:Expr) : ExprMiddle :=
let '(expr rname mp act) := expr in
(exprm rname mp act None).
Definition progInitLabel (prog:Program) : ProgramMiddle :=
map exprInitLabel prog.
Fixpoint caculateExprLabel (prog:ProgramMiddle) (expr:ExprMiddle) : ExprMiddle :=
let '(exprm rname mp act label) := expr in
match act with
| actRc (ruleCall nxtRn nxtPl) => if (hasPrefix nxtPl)
then exprm rname mp act (Some 0)
else let nxtExpr := (getExprM prog nxtRn) in
let '(exprm _ _ _ nxtLabel) := nxtExpr in
match nxtLabel with
| None => exprm rname mp act None
| Some n' => exprm rname mp act (Some (S n'))
end
| actAnchor addr => exprm rname mp act (Some 0)
| actOrAnchor pRule aRule =>
let '(ruleCall pRn pPl) := pRule in
if (hasPrefix pPl)
then exprm rname mp act (Some 0)
else let nxtExpr := (getExprM prog pRn) in
let '(exprm _ _ _ pLabel) := nxtExpr in
match pLabel with
| None => exprm rname mp act None
| Some n' => exprm rname mp act (Some (S n'))
end
end.
Fixpoint iterativeLabel (n:nat) (prog:ProgramMiddle) : ProgramMiddle :=
match n with
| 0 => prog
| S n' => let tmp := (map (caculateExprLabel prog) prog) in
iterativeLabel n' tmp
end.
Fixpoint ExprMiddle2Label (expr:ExprMiddle) : ExprLabeled :=
let '(exprm rn mp act optNat) := expr in
match optNat with
| None => exprl rn mp act 0
| Some x => exprl rn mp act x
end.
Fixpoint progMiddle2Labled (prog:ProgramMiddle) : ProgramLabeled :=
map ExprMiddle2Label prog.
Fixpoint labelProgram (prog:Program) : ProgramLabeled :=
let n := List.length prog in
let progInitMiddle := (progInitLabel prog) in
let progFinalMidle := iterativeLabel n progInitMiddle in
progMiddle2Labled progFinalMidle.
Compute (labelProgram sampleProgram6).
(* check program labels against specs *)
(* 1. expr with getPrefix in parameter: n2s = 0 *)
(* 2. expr anchor has: n2s = 0 *)
(* 3. expr without prefix nor anchor: n2s = (getExpr prog n)'s n + 1 *)
(* Must use function due to there are _ in matching,
coq can't unfold match with _*)
Function checkExpr (prog:ProgramLabeled) (e:ExprLabeled) : bool :=
let '(exprl _ _ act n2s) := e in
(* match e with *)
(* | (exprl _ _ act n2s) => *)
match act with
| actRc (ruleCall nxtRn nxtPl) =>
if Nat.eqb n2s 0
then (if hasPrefix nxtPl
then true
else false)
else (let e' := getExpr prog nxtRn in
let '(exprl _ _ _ n2s') := e' in
Nat.ltb n2s' n2s
)
| actOrAnchor
(ruleCall nxtRn1 nxtPl1)
(ruleCall nxtRn2 nxtPl2) =>
if Nat.eqb n2s 0
then (if hasPrefix nxtPl1
then (hasPrefix nxtPl2)
else false)
else (let e' := getExpr prog nxtRn1 in
let '(exprl _ _ _ n2s') := e' in
let e'' := getExpr prog nxtRn2 in
let '(exprl _ _ _ n2s'') := e'' in
if Nat.ltb n2s' n2s
then (if Nat.ltb n2s'' n2s then true else false)
else false
)
| actAnchor _ => Nat.eqb n2s 0
end.
(* small trick: use eqb true is easier to say every expr is true *)
Fixpoint checkLabeledProgramExpr (prog:ProgramLabeled) : bool :=
let temp := (map (checkExpr prog) prog) in
fold_left eqb temp true.
Fixpoint checkProg (prog:ProgramLabeled) : bool :=
if (Nat.leb (length prog) 0)
then false
else
checkLabeledProgramExpr prog.
Definition getNPrefix (args:list Name) : nat :=
match args with
| [] => 0
| h :: t => List.length h
end.
Fixpoint similarFindMatch (n:nat) (l:list nat) (target:nat) : option bool :=
match n with
| 0 => None