- Much work on the new style parser.

This commit is contained in:
ccremers 2005-11-28 14:31:16 +00:00
parent 5276630007
commit 8b736ede0e
3 changed files with 2405 additions and 9 deletions

View File

@ -3,16 +3,157 @@
# requires python-pyparsing module # requires python-pyparsing module
# http://pyparsing.sourceforge.net/ # http://pyparsing.sourceforge.net/
from pyparsing import Word, alphanums, alphas, nums, oneOf, restOfLine, OneOrMore, \ from pyparsing import Literal, alphas, nums, Word, oneOf, Or, Group, \
ParseResults, Forward, Combine, Or, Optional,MatchFirst, \ restOfLine, Forward, Optional, delimitedList
ZeroOrMore, StringEnd, LineEnd, delimitedList, Group, Literal
import Term import Term
typedversion = False
# Generate atom parser
#
# Takes a list of tokens, returns
def atomsParser ():
global typedversion
# ------------------------------------------------------
# Atomic
# ------------------------------------------------------
# Tokens
lbr = Literal("(").suppress()
rbr = Literal(")").suppress()
comma = Literal(",").suppress()
hash = Literal("#").suppress()
equ = Literal("=").suppress()
implies = Literal("=>").suppress()
dot = Literal(".").suppress()
eol = Literal("\n").suppress()
# Basic constructors
Alfabet= alphas+nums+"_$"
Number = Word(nums)
Number.setParseAction(lambda s,l,t: [ "number", Term.TermConstant(t[0]) ])
# Typeinfo/Constant
TypeInfo = oneOf ("mr nonce pk sk fu table")
TypeInfo.setParseAction(lambda s,l,t: [ "typeinfo", Term.TermConstant(t[0]) ])
Const = Word(alphas,Alfabet)
Const.setParseAction(lambda s,l,t: [ "constant", Term.TermConstant(t[0]) ])
# Time
nTime = Group(Number)
nTime.setParseAction(lambda s,l,t: ["n", t[0] ])
xTime = Literal("xTime")
xTime.setParseAction(lambda s,l,t: ["x", 0 ])
sTime = Literal("s").suppress() + lbr + Group(Number) + rbr
sTime.setParseAction(lambda s,l,t: ["s", t[0] ])
Time = Or([nTime,xTime,sTime])
Time.setParseAction(lambda s,l,t: ["time", t[0],t[1] ])
# Two versions
Variable = Word("x",Alfabet)
Variable.setParseAction(lambda s,l,t: [ "v", Term.TermVariable(t[0],None) ])
if typedversion:
Variable = TypeInfo + "(" + Variable + ")"
# Atomic
Atomic = Or([ TypeInfo + lbr + Const + rbr, Variable])
### TEST
print Time.parseString("s(25)")
# ------------------------------------------------------
# Messages
# ------------------------------------------------------
# Base forward declaration
Message = Forward()
# Optional prime
optprime = Optional(Literal("'"))
# Agents etc
Agent = Or ([Literal("mr") + lbr + Const + rbr, Variable])
KeyTable = Or ([Literal("table") + lbr + Const + rbr, Variable])
KeyTableApp = Literal("tb") + lbr + KeyTable + comma + Agent + rbr + optprime
# Crypto
pkterm = Literal("pk") + lbr + Const + rbr + optprime
varterm = Variable + optprime
Invertible = Or( [pkterm, KeyTableApp, varterm])
PublicCypher = Literal("crypt") + lbr + Invertible + comma + Message + rbr
XOR = Literal("rcrypt") + lbr + Message + comma + Message + rbr
SymmetricCypher = Literal("scrypt") + lbr + Message + comma + Message + rbr
futerm = Or([ Literal("fu") + lbr + Const + rbr, Variable ])
Function = Literal("funct") + lbr + futerm + comma + Message + rbr
# Message composition
Concatenation = Literal("c") + lbr + Message + comma + Message + rbr
Composed = Or([ Concatenation, SymmetricCypher, XOR,
PublicCypher, Function, KeyTable, KeyTableApp ])
Message = Or ([Composed, Atomic])
# ------------------------------------------------------
# Model of honest agents
# ------------------------------------------------------
Boolean = Or ([ Literal("true"), Literal("false"), Variable ])
Session = Forward()
Session = Or ([ Literal("s") + lbr + Session + rbr, Number, Variable ])
MsgList = Forward()
MsgEtc = Literal("etc")
MsgComp = Literal("c") + lbr + Message + comma + MsgList + rbr
MsgList = Or ([ MsgEtc, Variable, MsgComp ])
Step = Or ([ Number, Variable ])
### TEST
print Message.parseString("xKb")
print MsgList.parseString("etc")
print MsgComp.parseString("c(xKb,etc)")
print MsgList.parseString("c(xA,c(xB,c(xKa,c(xKa',c(xKb,etc)))))")
# Principal fact
Principal = Literal("w") + lbr + Step + comma + Agent + comma + Agent + comma + MsgList + comma + MsgList + comma + Boolean + comma + Session + rbr
# Message fact
MessageFact = Literal("m") + lbr + Step + comma + Agent + comma + Agent + comma + Agent + comma + Message + comma + Session + rbr
# Goal fact
GoalFact = Literal ("nogniet")
GoalState = Literal ("nogniet")
# Facts and states
Fact = Or ([ Principal, MessageFact ])
State = Group(delimitedList (Fact, "."))
# Rules
mr1 = Literal("h") + lbr + Literal("s") + lbr + Literal("xTime") + rbr + rbr + dot + State
mr2 = implies
mr3 = Literal("h") + lbr + Literal("xTime") + rbr + dot + MessageFact + dot + Principal + dot + GoalFact + eol
MessageRule = mr1 + eol + mr2 + eol + mr3 + eol
InitialState = Literal("h") + lbr + Literal("xTime") + rbr + dot + State + eol
# Intruder
IntruderRule = Literal("nogniet")
# Simplification
SimplificationRule = Literal("nogniet")
# Compose all rules
Rule = Or([ InitialState, MessageRule, IntruderRule, GoalState, SimplificationRule ])
print Rule.parseFile("test.if")
def ifParse (str): def ifParse (str):
# Tokens # Tokens
lbr = Literal("(").suppress() lbr = Literal("(").suppress()
rbr = Literal(")").suppress() rbr = Literal(")").suppress()
com = Literal(",").suppress() comma = Literal(",").suppress()
hash = Literal("#").suppress() hash = Literal("#").suppress()
equ = Literal("=").suppress() equ = Literal("=").suppress()
implies = Literal("=>").suppress() implies = Literal("=>").suppress()
@ -24,7 +165,7 @@ def ifParse (str):
def ntup(n): def ntup(n):
x = Message x = Message
while n > 1: while n > 1:
x = x + com + Message x = x + comma + Message
n = n - 1 n = n - 1
return x return x
@ -69,7 +210,7 @@ def ifParse (str):
return [Term.TermEncrypt(t[0][2],t[0][1])] return [Term.TermEncrypt(t[0][2],t[0][1])]
CryptOp = oneOf ("crypt scrypt c funct rcrypt tb") CryptOp = oneOf ("crypt scrypt c funct rcrypt tb")
CryptMsg = Group(CryptOp + lbr + Message + com + Message + rbr).setParseAction(parseCrypt) CryptMsg = Group(CryptOp + lbr + Message + comma + Message + rbr).setParseAction(parseCrypt)
def parseSMsg(s,l,t): def parseSMsg(s,l,t):
return [Term.TermEncrypt(t[0][1],Term.Termconstant("succ") )] return [Term.TermEncrypt(t[0][1],Term.Termconstant("succ") )]
@ -85,9 +226,9 @@ def ifParse (str):
# Fact section # Fact section
Request = Group("request" + btup(4)) Request = Group("request" + btup(4))
Witness = Group("witness" + btup(4)) Witness = Group("witness" + btup(4))
Give = Group("give" + lbr + Message + com + ftup(Literal("f"), Give = Group("give" + lbr + Message + comma + ftup(Literal("f"),
1) + rbr) 1) + rbr)
Secret = Group("secret" + lbr + Message + com + Secret = Group("secret" + lbr + Message + comma +
ftup(Literal("f"),1) + rbr) ftup(Literal("f"),1) + rbr)
TimeFact = Group(ftup (Literal("h"), 1)) TimeFact = Group(ftup (Literal("h"), 1))
IntruderKnowledge = Group(ftup (Literal("i"), 1)) IntruderKnowledge = Group(ftup (Literal("i"), 1))
@ -102,7 +243,7 @@ def ifParse (str):
# Rules and labels # Rules and labels
rulename = Word (alphanums + "_") rulename = Word (alphanums + "_")
rulecategory = oneOf("Protocol_Rules Invariant_Rules Decomposition_Rules Intruder_Rules Init Goal") rulecategory = oneOf("Protocol_Rules Invariant_Rules Decomposition_Rules Intruder_Rules Init Goal")
label = hash + "lb" + equ + rulename + com + "type" + equ + rulecategory label = hash + "lb" + equ + rulename + comma + "type" + equ + rulecategory
rule = Group(State + Optional(implies + State)) rule = Group(State + Optional(implies + State))
labeledrule = Group(label + rule) labeledrule = Group(label + rule)
typeflag = hash + "option" + equ + oneOf ("untyped","typed") typeflag = hash + "option" + equ + oneOf ("untyped","typed")
@ -115,4 +256,12 @@ def ifParse (str):
return parser.parseString(str) return parser.parseString(str)
def main():
global typedversion
typedversion = False
atomsParser()
if __name__ == '__main__':
main()

2226
scripts/if2spdl/pyparsing.py Normal file

File diff suppressed because it is too large Load Diff

21
scripts/if2spdl/test.if Normal file
View File

@ -0,0 +1,21 @@
h(s(xTime)).w(0,mr(I),xA,etc,c(xA,c(xB,c(xKa,c(xKa',c(xKb,etc))))),true,xc)
=>
h(xTime).m(1,xA,xA,xB,crypt(xKb,c(nonce(c(Na,xTime)),xA)),xc).w(2,xB,xA,c(nonce(c(Na,xTime)),etc),c(xA,c(xB,c(xKa,c(xKa',c(xKb,etc))))),true,xc).witness(xA,xB,Na,nonce(c(Na,xTime)))
h(s(xTime)).m(1,xr,xA,xB,crypt(xKb,c(xNa,xA)),xc2).w(1,xA,xB,etc,c(xA,c(xB,c(xKa,c(xKb,c(xKb',etc))))),xbool,xc)
=>
h(xTime).m(2,xB,xB,xA,crypt(xKa,c(c(xNa,nonce(c(Nb,xTime))),xB)),xc2).w(3,xA,xB,c(crypt(xKb,c(xNa,xA)),c(nonce(c(Nb,xTime)),etc)),c(xA,c(xB,c(xKa,c(xKb,c(xKb',etc))))),true,xc).witness(xB,xA,Nb,nonce(c(Nb,xTime)))
h(s(xTime)).m(2,xr,xB,xA,crypt(xKa,c(c(xNa,xNb),xB)),xc2).w(2,xB,xA,c(xNa,etc),c(xA,c(xB,c(xKa,c(xKa',c(xKb,etc))))),xbool,xc)
=>
h(xTime).m(3,xA,xA,xB,crypt(xKb,xNb),xc2).w(0,mr(I),xA,etc,c(xA,c(xB,c(xKa,c(xKa',c(xKb,etc))))),true,s(xc)).request(xA,xB,Nb,xNb)
h(s(xTime)).m(3,xr,xA,xB,crypt(xKb,xNb),xc2).w(3,xA,xB,c(crypt(xKb,c(xNa,xA)),c(xNb,etc)),c(xA,c(xB,c(xKa,c(xKb,c(xKb',etc))))),xbool,xc)
=>
h(xTime).w(1,xA,xB,etc,c(xA,c(xB,c(xKa,c(xKb,c(xKb',etc))))),true,s(xc)).request(xB,xA,Na,xNa)