/
psl.maude
1438 lines (1257 loc) · 56.7 KB
/
psl.maude
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
---(
Maude-PSL, Version: [1.0] [May 15th 2015]
Copyright (c) 2015, University of Illinois
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Illinois nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------------------------------------
Copyright (c) 2015. To the extent that a federal employee is an author of
a portion of the software or a derivative work thereof, no copyright is
claimed by the United States Government, as represented by the Secretary
of the Navy ("GOVERNMENT") under Title 17, U.S. Code. All Other Rights Reserved.
Permission to use, copy, and modify this software and its documentation is
hereby granted, provided that both the copyright notice and this permission
notice appear in all copies of the software, derivative works or modified
versions, and any portions thereof, and that both notices appear in
supporting documentation.
GOVERNMENT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
FROM THE USE OF THIS SOFTWARE.
GOVERNMENT requests users of this software to return modifications,
improvements or extensions that they make to:
maudenpa@chacs.nrl.navy.mil]
-or-
Naval Research Laboratory, Code 5543
4555 Overlook Avenue, SW
Washington, DC 20375
---)
---(
This file contains the second stage of the translation from a Maude-PSL specification to a Maude-NPA specification. To use this code, load it into
Maude and call red T ., where T is an AC soup containing the following:
**********************
The Protocol, Intruder, and Attack sections as a term of the form:
Specification
{
Protocol
{
...
}
Intruder
{
...
}
Attacks
{
...
}
}
Note the addition of the "Specification" section heading, and the brackets
at the beginning and end of each section.
**********************
[<DEFS>] - set of user-provided definitions. <DEFS> = $noDefs if we have no
definitions
**********************
[mt] - Starting Strand data for protocols.
**********************
[empty] - Starting Strand Set for the Intruder.
**********************(Optional)**********************
[mt] - Another strand data if we're rewriting a composition term (and ONLY
if we're rewriting a composition term). This should be included
at the top level (i.e. same level as the [comp] structure and
the translate terms).
If there are any problems, then the term will be lifted up to the kind, and
somewhere in the soup will be a term prefixed by a triple dollar sign: $$$
---)
---Contains the syntax for each the following translation.
load PSL-Syntax.maude
---Following one to one and one to many modules should only be used for
---debugging.
---One to One
---load nsldb.maude
---One to many
---load nslkd.maude
---TODO: Rewrite the error message terms so that they are easier for Python to parse.
---(
Because of weird pregularity problems when trying to make Knowledge-!=
[from NPA-Syntax] a subsort of Disequalities, I've had to use the operator
$!= in the Maude code. So Python needs to convert != to $!=.
---)
fmod SECTION-SEMANTICS is
protecting SECTION-SYNTAX .
*************************Section*********************************
op _ in _ : SectionName SubSection -> Bool .
eq SN:SectionName in SN:SectionName {S:Stmts} SS:SubSection = true .
eq SN:SectionName in SS:SubSection = false [owise] .
endfm
fmod DEFINITION-SEMANTICS is
protecting DEFINITION-SYNTAX .
eq D:Definition, D:Definition, DS:Definitions =
D:Definition, DS:Definitions .
---This exists because it's required by downTerm. Checks performed both by
---the Python and by Maude should catch any errors before ever
---invoking $applyDefs. So, if $errorDefs shows up, it's because of an
---error in my code, not the user's.
op $errorDefs : -> MsgSet .
op $applyDefs : MsgSet Definitions -> MsgSet .
---This variant of applyDefs exists at the meta level, and is where we
---actually apply the definitions.
---The other variants all reduce to this case.
op $applyDefs : TermList Definitions -> TermList .
eq $applyDefs(MS:MsgSet, $noDefs) = MS:MsgSet .
eq $applyDefs(MS:MsgSet, D:NeDefinitions) =
downTerm($applyDefs(upTerm(MS:MsgSet), D:NeDefinitions), $errorDefs) .
eq $applyDefs((empty).TermList, D:Definitions) = empty .
ceq $applyDefs((T:Term, TL:TermList), (M1:Msg := M2:Msg, D:Definitions))
= upTerm(M2:Msg), $applyDefs(TL:TermList, (M1:Msg := M2:Msg, D:Definitions))
if downTerm(T:Term, $errorDefs) == M1:Msg .
eq $applyDefs((V:Variable, TL:TermList), D:Definitions)
= V:Variable, $applyDefs(TL:TermList, D:Definitions) [owise] .
eq $applyDefs((C:Constant, TL:TermList), D:Definitions)
= C:Constant, $applyDefs(TL:TermList, D:Definitions) [owise] .
eq $applyDefs((F:Qid[TL:TermList], TL1:TermList), D:Definitions) =
F:Qid[$applyDefs(TL:TermList, D:Definitions)],
$applyDefs(TL1:TermList, D:Definitions) [owise] .
op $applyDefs : Strand Definitions -> Strand .
var L : SMsgList-L .
var R : SMsgList-R .
op $errorStrand : -> Strand .
eq $applyDefs(S:Strand, D:Definitions) =
downTerm($applyDefs(upTerm(S:Strand), D:Definitions), $errorStrand) .
eq numIterations = 100 .
eq $makeIdem($noDefs) = $noDefs .
eq $makeIdem((P:Msg := M:Msg, DS:Definitions)) =
$makeIdem(P:Msg := M:Msg, P:Msg := downTerm($applyDefs(upTerm(M:Msg), DS:Definitions), $errorDefs), DS:Definitions,
P:Msg := M:Msg, 0) .
eq $makeIdem(D:Definition, D:Definition, DS:Definitions, DORIG:Definition, N:Nat) = D:Definition, $makeIdem(DS:Definitions) .
eq $makeIdem(D:Definition, D':Definition, DS:Definitions, DORIG:Definition, numIterations) =
$cantMakeDefsIdempotent((DORIG:Definition, DS:Definitions), numIterations) .
eq $makeIdem(P:Msg := M:Msg, P:Msg := M':Msg, DS:Definitions, DORIG:Definition, N:Nat) =
$makeIdem(P:Msg := M':Msg, P:Msg := downTerm($applyDefs(upTerm(M':Msg), DS:Definitions), $errorDefs), DS:Definitions,
DORIG:Definition, s(N:Nat)) [owise] .
eq $checkWellFormed(((K:Msg, N:Nat) := T:Msg), DS:Definitions) = K:Msg := T:Msg, $checkWellFormed(DS:Definitions) .
eq $checkWellFormed($noDefs) = $noDefs .
ceq $checkWellFormed((DS:Definitions, DSK:[Definitions])) = $checkWellFormed(DSK:[Definitions])
if DS:Definitions =/= $noDefs .
eq $checkWellFormed(DSK:[Definitions]) = $$$malformedDefs($moveLineNum(DSK:[Definitions])) [owise] .
eq $moveLineNum(((K:[Msg], N:Nat) := T:[Msg], DS:[Definitions])) = (K:[Msg] := T:[Msg] $$,$$ N:Nat) $$;;;$$
$moveLineNum(DS:[Definitions]) .
eq $moveLineNum($noDefs) = $noDefs .
endfm
mod PROTOCOL-SEMANTICS is
protecting SECTION-SEMANTICS .
protecting DEFINITION-SEMANTICS .
protecting PROTOCOL-SYNTAX .
vars N LN N1 : Nat .
var P : Role .
vars IN OUT : MsgSet .
---mb Protocol {PS:ProtStmts} : ProtocolSection .
var DEFS : Definitions .
---(
The following two rules process the input and output for each role.
The rules are identical, except for the order in which the Input and
Output statements appear.
Note that these two rules create the strand for each role. Therefore,
these rules must fire before any of the rules that populate the strands.
---)
crl Specification
{
Protocol{
PS1:Stmts
In(P) = IN .[N]
PS2:Stmts
Out(P) = OUT .[N1]
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
[DEFS]
=>
Specification
{
Protocol
{
PS1:Stmts PS2:Stmts PS3:Stmts
}
SS:SubSection
}
[P |-> {IN} :: nil :: [(nil).SMsgList-L | nil]
{$applyDefs(OUT, DEFS)} & STR:StrandData]
[DEFS]
if IN are variables .
crl Specification
{
Protocol{
PS1:Stmts
Out(P) = OUT .[N]
PS2:Stmts
In(P) = IN .[N1]
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
[DEFS]
=>
Specification
{
Protocol
{
PS1:Stmts PS2:Stmts PS3:Stmts
}
SS:SubSection
}
[P |-> {IN} :: nil :: [(nil).SMsgList-L | nil]
{$applyDefs(OUT, DEFS)} & STR:StrandData]
[DEFS]
if IN are variables .
---(
The next few rules handle ways in which the input and output can
fail: the input contains something other than variables, or the
input and output statements are missing.
Note that these checks will be pushed to Python, except possibly
for the check that the input is variables. That depends on what
I manage to accomplish with the parser. I'll probably keep the variable
checking in Maude, because that requires distinguishing between
user-defined terms, and high level syntax, which Maude is better at
than Python.
However, checking if the input and output statements exist should
be easily done in Python, regardless of the power of the parser.
---)
crl Specification
{
Protocol{
PS1:Stmts
In(P) = IN .[N]
PS2:Stmts
Out(P) = OUT .[N1]
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
=>
$invalidInput(P, $errorInput(IN), N)
Specification
{
Protocol{
PS1:Stmts
PS2:Stmts
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
if not IN are variables .
crl Specification
{
Protocol{
PS1:Stmts
Out(P) = OUT .[N]
PS2:Stmts
In(P) = IN .[N1]
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
$invalidInput(P, $errorInput(IN), N1)
=>
Specification
{
Protocol{
PS1:Stmts
PS2:Stmts
PS3:Stmts
}
SS:SubSection
}
[STR:StrandData]
if not IN are variables .
crl Specification
{
Protocol
{
PS1:Stmts
In(P) = IN .[N]
PS2:Stmts
}
SS:SubSection
}
=>
$missingOutput(P, N)
Specification
{
Protocol
{
PS1:Stmts
PS2:Stmts
}
SS:SubSection
}
if not $out P listed in (PS1:Stmts PS2:Stmts) .
op $out_listed in_ : Role Stmts -> Bool .
eq $out P listed in (Out(P) = OUT .[N] SS:Stmts) = true .
eq $out P listed in pass = false .
eq $out P listed in (S:Stmt SS:Stmts) = $out P listed in SS:Stmts [owise] .
---Indicates that the first argument is missing an output statement.
op $missingOutput : Role Nat -> [TranslationData] .
crl [missingInput] : Specification
{
Protocol
{
PS1:Stmts
Out(P) = OUT .[N]
PS2:Stmts
}
SS:SubSection
}
=>
$missingInput(P, N)
Specification
{
Protocol
{
PS1:Stmts
PS2:Stmts
}
SS:SubSection
}
if not $in P listed in (PS1:Stmts PS2:Stmts) .
op $in _ listed in _ : Role Stmts -> Bool .
eq [in1] : $in P listed in (In(P) = IN .[N] SS:Stmts) = true .
eq [in2] : $in P listed in pass = false .
eq [in3] : $in P listed in (S:Stmt SS:Stmts) =
$in P listed in SS:Stmts [owise] .
---Indicates that we're missing an input statement for the first argument.
op $missingInput : Role Nat -> [TranslationData] .
---An error indicating that one of the inputs was not a variable. Lifts the
---entire term to the kind, so that we can check if there is an error by checking
---if the result sort is at the kind. If it is, we look through the output for
---the appropriate error.
---Arguments:
--- 1. The Principal with the invalid input
--- 2. The first invalid input.
--- 3. The line number on which the error occured.
op $invalidInput : Role Msg Nat -> [TranslationData] .
---Gives us the first input that is not a variable.
op $errorInput : MsgSet -> Msg .
eq $errorInput(V:Msg, IN) = if V:Msg are variables then $errorInput(IN) else V:Msg fi .
op _are variables : MsgSet -> Bool .
ceq (V:Msg, IN) are variables = IN are variables if UPV:Term := upTerm(V:Msg) /\
UPV:Term :: Variable .
eq emptyMsgSet are variables = true .
ceq (V:Msg, IN) are variables = false if UPV:Term := upTerm(V:Msg) /\
not UPV:Term :: Variable .
**************************Protocol Steps***************************
---1 . A -> B : T |- T .[LN] where LN is the current line number.
vars TA TB : Msg .
vars A B : Role .
vars INA OUTA INB OUTB : MsgSet .
var MSA MSB : SMsgList-L .
var FSA FSB : FreshSet .
---(
The following rule populates strands of A and B with the appropriate
term, and extracts the fresh variables from TA.
Observe that this rule requires both A's and B's strands to already
exist.
Note that the messages are to the left of |. This is
because the message list to the left is left associative, so we can append
messages to the end. Also, we'll need the messages to be in front of the
bar for the attacks anyway, so this just eases implementation of the
attack states. We'll move the
bars to the end when we actually build the maude module.
---)
rl Specification
{
Protocol
{
N . A -> B : TA |- TB .[LN]
S:Stmts
}
SS:SubSection
}
[DEFS]
[A |-> {INA} :: FSA :: [MSA | nil]{OUTA} &
B |-> {INB} :: FSB :: [MSB | nil]{OUTB} &
SP:StrandData]
=>
Specification
{
Protocol
{
S:Stmts
}
SS:SubSection
}
[DEFS]
[A |-> {INA} :: FSA, $fresh($applyDefs(TA, DEFS)) ::
[MSA, +($applyDefs(TA, DEFS)) | nil] {OUTA} &
B |-> {INB} :: FSB :: [MSB, -($applyDefs(TB, DEFS)) | nil]{OUTB} &
SP:StrandData] .
---(
$fresh extracts the variables of sort fresh from the passed
term [note that all terms passed to this function are user-defined
terms, which must all be a subsort of Msg].
---)
op $fresh : Msg -> FreshSet .
eq $fresh(T:Msg) = $fresh(upTerm(T:Msg), empty) .
---(
The first argument represents the list of terms that need to be
searched through for fresh variables, while the second argument
accumulates any found fresh variables.
---)
op $fresh : TermList TermList -> FreshSet .
---These three rules are the base cases: a single variable, or a
---single constant.
ceq $fresh(T:Variable, TL:TermList) = $downFresh((T:Variable, TL:TermList))
if getType(T:Variable) == 'Fresh .
ceq $fresh(T:Variable, TL:TermList) = $downFresh(TL:TermList)
if getType(T:Variable) =/= 'Fresh .
eq $fresh(T:Constant, TL:TermList) = $downFresh(TL:TermList) .
---The first equation deals with the case where the termlist to
---be checked for Fresh variables contains a single term of the
---form f(t_1, t_2, ..., t_n) (which is not a base case, because we
---need to check t_1, t_2, ..., t_n).
eq $fresh(F:Qid[TL:TermList], TL1:TermList) =
$fresh(TL:TermList, TL1:TermList) .
eq $fresh((F:Qid[TL:TermList], TL2:TermList), TL1:TermList) =
$fresh((TL:TermList, TL2:TermList), TL1:TermList) .
ceq $fresh((T:Variable, TL:TermList), TL1:TermList) =
$fresh(TL:TermList, (T:Variable, TL1:TermList))
if getType(T:Variable) == 'Fresh /\ TL:TermList =/= empty .
ceq $fresh((T:Variable, TL:TermList), TL1:TermList) =
$fresh(TL:TermList, TL1:TermList)
if getType(T:Variable) =/= 'Fresh /\ TL:TermList =/= empty .
ceq $fresh((T:Constant, TL:TermList), TL1:TermList) =
$fresh(TL:TermList, TL1:TermList)
if TL:TermList =/= empty .
---(
Given a list of terms representing variables of sort Fresh,
calls downterm on each variable, allowing us to then add them
to a role's strand.
---)
op $downFresh : TermList -> FreshSet .
---This should never appear in the output, even if the user writes
---something incorrectly.
op $error : -> Fresh .
eq $downFresh((T:Variable, TL:TermList)) = downTerm(T:Variable, $error),
$downFresh(TL:TermList) .
eq $downFresh(empty) = nil .
eq F:Fresh, F:Fresh, FS:FreshSet = F:Fresh, FS:FreshSet .
endm
mod INTRUDER-SEMANTICS is
protecting INTRUDER-SYNTAX .
protecting SECTION-SEMANTICS .
protecting PROTOCOL-SEMANTICS .
var DEFS : Definitions .
---mb Intruder {IS:IntStmts} : IntruderSection .
---(
Syntactic desugaring. The standard form of an intruder rule is
MS => M where MS is a [possibly empty] set of messages, and M is a
single message.
These equations put every intruder capability into that form.
---)
eq => MS:MsgSet .[N:Nat] = emptyMsgSet => MS:MsgSet .[N:Nat] .
eq MS:MsgSet => M:Msg, M1:Msg, MS1:MsgSet .[N:Nat]
=
MS:MsgSet => M:Msg .[N:Nat]
MS:MsgSet => M1:Msg, MS1:MsgSet .[N:Nat] .
eq MS:MsgSet => emptyMsgSet .[N:Nat] = pass .
eq MS1:MsgSet <=> MS2:MsgSet .[N:Nat] = (MS1:MsgSet => MS2:MsgSet .[N:Nat]
MS2:MsgSet => MS1:MsgSet .[N:Nat]) .
---(
Generates the intruder strand from a single intruder capability.
The function signedList constructs a list of signed messages in
the structure demanded by a strand [a strand's structure is a bit
more complicated than you would expect, because the use of narrowing
keeps us from making the list of messages in a strand associative].
---)
rl [IntruderConversion] :
Specification
{
Intruder
{
MS:MsgSet => M:Msg .[N:Nat]
IS:Stmts
}
SS:SubSection
}
[SS:StrandSet]
[DEFS]
=>
[DEFS]
[:: $fresh(M:Msg) ::
[ (nil).SMsgList-L | $signedList($applyDefs(MS:MsgSet, DEFS),
$applyDefs(M:Msg, DEFS))] & SS:StrandSet]
Specification
{
Intruder{ IS:Stmts }
SS:SubSection
} .
---(
Given a set of messages, m_1, m_2, ..., m_n and a single message, m,
returns a signed list of messages: -[m_1], -[m_2], ..., -[m_n], +[m].
Note this function is technically not a function, because the same
set can generate different functions depending on the order in which
elements are removed from the set. However, the order of received
messages does not matter for intruder strands, so this nondeterminism
doesn't affect the semantics of the specification.
Note that a SMsgList-R list is considered right associative for parsing
purposes, meaning you can only extract and append messages from the
front of the list, not the back.
---)
op $signedList : MsgSet Msg -> SMsgList-R .
op $signedList : MsgSet SMsgList-R -> SMsgList-R .
eq $signedList(MS:MsgSet, M:Msg) = $signedList(MS:MsgSet, (+(M:Msg), nil)) .
eq $signedList((M:Msg, MS:MsgSet), SMR:SMsgList-R) =
$signedList(MS:MsgSet, (-(M:Msg), SMR:SMsgList-R)) .
eq $signedList(emptyMsgSet, SMR:SMsgList-R) = SMR:SMsgList-R .
eq S:Strand & S:Strand = S:Strand .
eq Specification{Intruder{pass} SS:SubSection} =
Specification{$emptyIntruder SS:SubSection} .
endm
mod ATTACK-SEMANTICS is
protecting SECTION-SEMANTICS .
protecting META-TERM .
protecting ATTACK-SYNTAX .
protecting PROTOCOL-SEMANTICS .
var P : Role .
---This equation builds the set of attack data that the translation
---rules depended on. By waiting until the Protocol section has been
---translated before creating the empty set of attack data, we can
---guarantee that the Attack section won't be processed, until the
---Protocol section has been fully translated.
eq Specification { Protocol { pass } SS:SubSection } =
[$emptyAttackData] Specification { $emptyProtocol SS:SubSection } .
var DEFS : Definitions .
var N : Nat .
---(
Builds attacks that have at least one without block.
The variable declarations in brackets are actually terms that allow us to
add variables to the attack states, without forcing the user to provide
to the original term to be translated.
These declarations will be added to the Maude module when constructing
the module.
---)
rl [translateAttacksWithNeverPattern] :
Specification
{
Attacks{
N .{CA:CoreAttack WA:WithoutBlocks}
A:Stmts
}
SS:SubSection
}
[DEFS]
[SP:StrandData]
[AT:AttackData]
=>
[DEFS]
[SP:StrandData]
Specification {Attacks{ A:Stmts} SS:SubSection}
[var S : StrandSet .]
[var K : IntruderKnowledge .]
[var LIST : SMsgList-R .]
[ AT:AttackData
[N:Nat |-> $genAttackStrands(CA:CoreAttack, $subst(CA:CoreAttack, DEFS), SP:StrandData, DEFS)
|| $genIntruderKnowledge(CA:CoreAttack, $subst(CA:CoreAttack, DEFS), DEFS)
|| nil
|| nil
|| never($genNeverPatterns(WA:WithoutBlocks, SP:StrandData, DEFS))]] .
---(
Builds attacks that don't have any without blocks. Other than the processing
of without blocks, this and the previous rule are identical.
---)
rl [translateAttackWithoutNever] :
Specification
{
Attacks{
N .{CA:CoreAttack}
A:Stmts
}
SS:SubSection
}
[SP:StrandData]
[DEFS]
[AT:AttackData]
=>
[var S : StrandSet .]
[var K : IntruderKnowledge .]
[var LIST : SMsgList-R .]
[SP:StrandData]
[DEFS]
Specification {Attacks{A:Stmts} SS:SubSection}
[AT:AttackData
[N:Nat |-> $genAttackStrands(CA:CoreAttack, $subst(CA:CoreAttack, DEFS), SP:StrandData, DEFS)
|| $genIntruderKnowledge(CA:CoreAttack, $subst(CA:CoreAttack, DEFS), DEFS)
|| nil
|| nil
|| nil]] .
eq [V:VarDecl] [V:VarDecl] = [V:VarDecl] .
---(
Given a set of core attack statements (intruder knowledge, execution
statements, substitutions, and constraints), a substitution, the set of
strands computed while translating the Protocol section, and the user-defined
definitions, returns a set of strands [instantiated by the second argument]
that correspond to the execution statements in the first argument.
So if we have the following execution statements:
A executes protocol .
B executes protocol .
and the substitution theta, then
we get the set of strands
s_A\theta & s_B\theta
where s_A is A's strand, and s_B is B's strand.
---)
op $genAttackStrands : CoreAttack Mappings StrandData Definitions ~> StrandSet .
vars IN OUT : MsgSet .
eq $genAttackStrands(R:Role executes protocol .[N] CA:CoreAttack,
M:Mappings, R:Role |-> {IN}S:Strand{OUT} & SD:StrandData, DEFS)
=
$applyMapping($applyDefs(S:Strand, DEFS), M:Mappings) &
$genAttackStrands(CA:CoreAttack, M:Mappings, SD:StrandData, DEFS) .
eq $genAttackStrands(R:Role executes up to N1:Nat .[N] CA:CoreAttack,
M:Mappings, R:Role |-> {IN}S:Strand{OUT} & SD:StrandData, DEFS)
=
$applyMapping($applyDefs($prefix(S:Strand, N1:Nat), DEFS), M:Mappings) &
$genAttackStrands(CA:CoreAttack, M:Mappings, SD:StrandData, DEFS) .
ceq $genAttackStrands(CA:CoreAttack, M:Mappings, SD:StrandData, DEFS) = empty
if not $hasExecutionStmt(CA:CoreAttack) .
eq $genAttackStrands(CA:CoreAttack, M:Mappings, SD:StrandData, DEFS) = empty [owise] .
op $hasExecutionStmt : CoreAttack -> Bool .
eq $hasExecutionStmt(R:Role executes protocol .[N] CA:CoreAttack) = true .
eq $hasExecutionStmt(R:Role executes up to N1:Nat . [N] CA:CoreAttack) = true .
eq $hasExecutionStmt(CA:CoreAttack) = false [owise] .
---(
Given a Strand, :: r1 :: [m_1, m_2, ..., m_l] and a natural number
n < l, returns a prefix of the strand of the form:
:: r1 :: [m_1, m_2, ..., m_n | L] where L is a variable
representing a list of signed messages.
---)
op $prefix : Strand Nat -> Strand .
op $prefixList : SMsgList-L Nat -> SMsgList-L .
eq $prefix(:: r1:FreshSet :: [L:SMsgList-L | nil], N) = :: r1:FreshSet :: [$prefixList(L:SMsgList-L, N) | LIST] .
---All of this "makeAssoc" and "makeRightAssoc" is necessary because
---strand lists aren't associative (because associative lists have
---infinitary unification algorithms). In fact, an SMsgList-L is
---considered left associative, meaning that you can only pluck
---messages off the end. Not exactly useful when you need the FIRST
---n messages in the list.
eq $prefixList(L:SMsgList-L, N:Nat) = $makeLeftAssoc($prefix($makeAssoc(L:SMsgList-L), N:Nat)) .
sort $SMsgList .
subsort SMsg < $SMsgList .
op $makeAssoc : SMsgList-L -> $SMsgList .
op _$;$_ : SMsg SMsg -> $SMsgList [assoc id: $nil] .
op $nil : -> $SMsgList .
eq $makeAssoc((L:SMsgList-L, M:SMsg)) = $makeAssoc(L:SMsgList-L) $;$ M:SMsg .
eq $makeAssoc(nil) = $nil .
op $prefix : $SMsgList Nat -> $SMsgList .
eq $prefix(M:SMsg $;$ L:$SMsgList, s(N)) = M:SMsg $;$ $prefix(L:$SMsgList, N) .
eq $prefix(L:SMsgList, 0) = $nil .
op $makeLeftAssoc : $SMsgList -> SMsgList-L .
eq $makeLeftAssoc(L:$SMsgList $;$ M:SMsg) =
$makeLeftAssoc(L:$SMsgList), M:SMsg .
eq $makeLeftAssoc($nil) = nil .
vars N1 N2 N3 N4 : Nat .
---(
Given a set of core attack statements, and a set of definitions, returns
an idempotent substitution that has been built from the substitution
statements in
argument 1, and has had the definitions applied to its range.
Third argument is the list of line numbers on which the first substitution appears.
This function, also checks to make sure that the generated substitution
is a valid order-sorted substitution.
---)
op $subst : CoreAttack Definitions -> Mapping .
eq $subst(CA:CoreAttack, DEFS) = $makeIdem($isValid($extractMappings(CA:CoreAttack, DEFS)),
$mappingLineNums(CA:CoreAttack)) .
---(
Given a set of core attack statements, extracts all of the substitution
statements Subst(A) = v_1 |-> t_1, v_2 |-> t_2, ... , v_m |-> t_m .[n], and
constructs a set of mappings
v_1 |-> ${t_1 ; n}$, v_2 |-> ${t_2 ; n}$, ... , v_m |-> ${t_m ; v_m}$ that
associates to each range message t_i the line on which v_i |-> t_i is
defined. This information will be needed when printing error messages
about poorly formed substitutions.
---)
op $extractMappings : CoreAttack Definitions -> MsgPairs .
eq $extractMappings(Subst(R:Role) = M:Mappings .[N] CA:CoreAttack, DEFS) =
$buildMsgPairs(M:Mappings, N, DEFS) $extractMappings(CA:CoreAttack, DEFS) .
eq $extractMappings(CA:CoreAttack, DEFS) = $none [owise] .
---(
Given a core attack, returns the list of line numbers on which the substitutions appear.
---)
op $mappingLineNums : CoreAttack -> MyNatList .
eq $mappingLineNums(Subst(R:Role) = M:Mappings .[N] CA:CoreAttack) = N : $mappingLineNums(CA:CoreAttack) .
eq $mappingLineNums(CA:CoreAttack) = mt [owise] .
---(
Given a set of mappings, a natural number representing the line number on
which the mappings were defined, and the user-defined definitions, this
function appends the passed line number to the range of each mapping,
encoding the line number on which that particular pair was declared. It
also applies the definitions to the range of each pair.
---)
op $buildMsgPairs : Mappings Nat Definitions -> MsgPairs .
eq $buildMsgPairs((M:Msg |-> M1:Msg, MS:Mappings), N:Nat, DEFS) =
M:Msg |-> ${$applyDefs(M1:Msg, DEFS) ; N:Nat}$ $buildMsgPairs(MS:Mappings, N:Nat, DEFS) .
eq $buildMsgPairs(M:Msg |-> M1:Msg, N:Nat, DEFS) = M:Msg |-> ${$applyDefs(M1:Msg, DEFS) ; N:Nat}$ .
eq $buildMsgPairs(id, N:Nat, DEFS) = $none .
var L : MyNatList .
---(
First argument is the list of mappings to be validated. Note that the line numbers are already encoded inside the MsgPairs, so
we don't need to separately track the line numbers.
---)
op $isValid : MsgPairs -> Mappings .
eq $isValid(M:MsgPairs) = $checkSorts($isFunction(M:MsgPairs)) .
---(
Line numbers are already encoded in the $$$notAFunction error term, so we don't need to encode them separately.
---)
op $isFunction : MsgPairs -> MsgPairs .
eq $isFunction(M:Msg |-> ${M1:Msg ; N1:Nat}$ M:Msg |-> ${M2:Msg ; N2:Nat}$
MS:MsgPairs)
=
if M1:Msg == M2:Msg
then
$isFunction(M:Msg |-> ${M1:Msg ; N1:Nat}$ MS:MsgPairs)
else
$$$notAFunction(M:Msg |-> ${M1:Msg ; N1:Nat}$ ${M2:Msg ; N2:Nat}$
$isFunction(M:Msg |-> ${M1:Msg ; N1:Nat}$ MS:MsgPairs))
fi .
eq $isFunction(MS:MsgPairs) = MS:MsgPairs [owise] .
---We only care about those mappings that have more than mapping. Anything
---with a single result term is not ambiguous, and is left over from how
---we implemented $isFunction.
eq $$$notAFunction(M:Msg |-> ${M1:Msg ; N1:Nat}$ MS:[MsgPairs]) =
$$$notAFunction(MS:[MsgPairs]) .
eq $$$notAFunction($$$notAFunction(MS1:[MsgPairs]) MS2:[MsgPairs]) =
$$$notAFunction(MS1:[MsgPairs] $$$;;;$$$ MS2:[MsgPairs]) .
eq $$$notAFunction(M:Msg |-> MN1:MsgNumSet M:Msg |-> MN2:MsgNumSet
MS:[MsgPairs])
=
$$$notAFunction(M:Msg |-> MN1:MsgNumSet MN2:MsgNumSet $$$;;;$$$
MS:[MsgPairs]) .
---Checks to make sure each mapping is a valid order-sorted substitution.
op $checkSorts : MsgPairs -> Mappings .
eq $checkSorts(M:Msg |-> ${M1:Msg ; N}$ MS:MsgPairs) =
$isValidPair(M:Msg, M1:Msg, N), $checkSorts(MS:MsgPairs) .
eq $checkSorts($none) = id .
---Checks if the sort of the first argument is a supersort of the sort of
---the second argument.
op $isValidPair : Msg Msg Nat -> Mapping .
ceq $isValidPair(D:Msg, R:Msg, N) =
if sortLeq(META-MOD:Module, getType(metaReduce(META-MOD:Module, upTerm(R:Msg))), getType(metaReduce(META-MOD:Module, upTerm(D:Msg))))
then
D:Msg |-> R:Msg
else
$$$invalidSorting(D:Msg |-> ${R:Msg ; N}$)
fi
if META-MOD:Module := upModule('PROTOCOL-EXAMPLE-SYMBOLS, false) .
---Given a mapping, returns the idempotent version, by applying the
---mapping to itself until we reach a fixed point.
---Second argument is the line number on which the mapping appears
op $makeIdem : Mappings MyNatList -> Mappings .
eq $makeIdem(id, L) = id .
eq $makeIdem(M:Mappings, L) = $makeIdem(M:Mappings, M:Mappings, false, 0, L) [owise] .
---First argument is the original mapping
---Second argument is the partially idempotenized mapping
---Third argument is how many times we've applied the original mapping
---to the idempotenized mapping.
---Fourth argument indicates whether or not we've reached the fixpoint.
---Fifth argument is the list of line numbers on which the substituions that make up the mapping appear.
---Result is the idempotenized mapping.
op $makeIdem : Mappings Mappings Bool Nat MyNatList -> Mappings .
eq $makeIdem(M:Mappings, M1:Mappings, true, N, L:MyNatList) = M1:Mappings .
eq $makeIdem(M:Mappings, M1:Mappings, false, 101, L:MyNatList) =
$$$infiniteIdem(M:Mappings, L:MyNatList) .
ceq $makeIdem(M:Mappings, M1:Mappings, false, N:Nat, L:MyNatList) =
$makeIdem(M:Mappings, M2:Mappings, M1:Mappings == M2:Mappings, s(N:Nat), L:MyNatList)
if M2:Mappings := $applyMapping(M:Mappings, M1:Mappings) /\ N:Nat < 101 .
---(
op _===_ : Mappings Mappings -> Bool .
eq M:Mappings === M:Mappings = true .
eq M:Mappings === M1:Mappings = false [owise] .
---)
---Applies the first mapping to the range of the second mapping, and
---returns the resultant mapping.
op $applyMapping : Mappings Mappings -> Mappings .
op $msgError : -> [Msg] .
eq $applyMapping(M2:Mappings, (N:Msg |-> N1:Msg, M:Mappings)) =
N:Msg |-> downTerm($applyMapping1(upTerm(N1:Msg), M2:Mappings), $msgError),
$applyMapping(M2:Mappings, M:Mappings) .
---Here we are treating id as the base case of the recursion, not as
---an empty substitution. Technically, idM should be M, not id. However,
---the equation $applyMapping(M, id) = M would have the effect of copying
---M into the composed substitution, which is most definitely not what
---we want, because this would end up duplicating some mappings, and
---creating ambiguity for others.
eq $applyMapping(M:Mappings, id) = id .
---If we go too many iterations of self-application without hitting
---idempotency, then this error gets added to the TranslationData pool.
op $$$infiniteIdem : Mappings MyNatList -> [Mappings] .
---op $$$missingSubstitution : MsgSet Mappings Nat -> [Strand] .
---(
The following are a group of very messy functions that instantiate the
passed strand with the passed mapping.
Here be dragons.
---)
op $applyMapping : Strand Mappings ~> Strand .
eq $applyMapping(S:Strand, id) = S:Strand .
eq $applyMapping(S:Strand, M:Mappings) =
$applyMapping(upTerm(S:Strand), M:Mappings) [owise] .
op $applyMapping : Term Mappings ~> Strand .
op $error : Term -> Strand .
eq $applyMapping(T:Term, id) = downTerm(T:Term, $error(T:Term)) [print "Strand meta: " T:Term] .
eq $applyMapping('::_::`[_|_`][F:Term, ML:Term, 'nil.SMsgList-R], M:Mappings) =
downTerm('::_::`[_|_`][F:Term, $applyMapping1(ML:Term, M:Mappings),
'nil.SMsgList-R],
$error('::_::`[_|_`][F:Term, $applyMapping1(ML:Term, M:Mappings),
'nil.SMsgList-R])) [owise print "Strand meta term list: " ML:Term] .
---Applies if we're using the "up to" syntax, in which case the last
---term in the list is a constant (which Maude-NPA will treat as a variable)
---of sort LIST, NOT nil.
eq $applyMapping('::_::`[_|_`][F:Term, ML:Term, 'LIST.SMsgList-R], M:Mappings) =
downTerm('::_::`[_|_`][F:Term, $applyMapping1(ML:Term, M:Mappings),
'LIST.SMsgList-R],
$error('::_::`[_|_`][F:Term, $applyMapping1(ML:Term, M:Mappings),
'LIST.SMsgList-R])) [owise print "Strand meta term list: " ML:Term] .
op $applyMapping1 : TermList Mappings ~> TermList .
var M : Mappings .
var T : Term .
var TL TL1 : TermList .
var F : Qid .
vars M1 M2 : Msg .
op $error : -> Msg .
var T1 : Term .
eq $applyMapping1(TL:TermList, id) = TL:TermList .
ceq $applyMapping1((T, TL), (M1 |-> M2, M)) = T1,
$applyMapping1(TL, (M1 |-> M2, M))
if downTerm(T, $error) == M1 /\
T1 := upTerm(M2) /\
M3:Msg := downTerm(T, $error) [print "Downterm: " M3:Msg] .
eq $applyMapping1((F[TL], TL1), M) =
F[$applyMapping1(TL, M)], $applyMapping1(TL1, M) [owise] .
eq $applyMapping1((C:Constant, TL), M) =
C:Constant, $applyMapping1(TL, M) [owise] .
eq $applyMapping1((V:Variable, TL), M) =
V:Variable, $applyMapping1(TL, M) [owise] .
eq $applyMapping1(empty, M) = empty .
op $applyDefs : Mappings Definitions -> Mappings .
eq $applyDefs(M:Mappings, $noDefs) = M:Mappings .
eq $applyDefs(id, D:Definitions) = id .
eq $applyDefs((M1:Msg |-> M2:Msg, MP:Mappings), D:NeDefinitions) =
$applyDefs(M1:Msg, D:NeDefinitions) |-> $applyDefs(M2:Msg, D:NeDefinitions),
$applyDefs(MP:Mappings, D:NeDefinitions) .
eq $applyDefs(M1:Msg |-> M2:Msg, D:NeDefinitions) =
$applyDefs(M1:Msg, D:NeDefinitions) |-> $applyDefs(M2:Msg, D:NeDefinitions) .
---(
Given a set of core attack statements, a set of mappings, and a set of
definitions, returns a sequence of disequality constraints and inI
statements to use as an attack's intruder knowledge.
---)
op $genIntruderKnowledge : CoreAttack Mappings Definitions -> IntruderKnowledge .
eq $genIntruderKnowledge((Intruder learns MS:MsgSet .[N]) CA:CoreAttack, M:Mappings, DEFS)
=
$msgSetToInI($applyMapping($applyDefs(MS:MsgSet, DEFS), M:Mappings)),
$genIntruderKnowledge(CA:CoreAttack, M:Mappings, DEFS) .
eq $genIntruderKnowledge(With constraints I:Disequalities .[N] CA:CoreAttack,
M:Mappings, DEFS)
=
$ineqToKnow-!=($applyMapping($applyDefs(I:Disequalities, DEFS), M:Mappings)),
$genIntruderKnowledge(CA:CoreAttack, M:Mappings, DEFS) .
eq $genIntruderKnowledge(CA:CoreAttack, M:Mappings, DEFS) = empty .
---(
Converts disequalities into disequalities in Maude-NPA. We don't use
IntruderKnowledge-!= directly because of issues with pre-regularity and
garbage and stuff. I are eloquent!
---)
op $ineqToKnow-!= : Disequalities -> IntruderKnowledge-!= .
eq $ineqToKnow-!=(M1:Msg $!= M2:Msg, I:Disequalities) = M1:Msg != M2:Msg, $ineqToKnow-!=(I:Disequalities) .