2005-12-04 14:46:37 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Spdl generator
|
|
|
|
#
|
|
|
|
import If
|
2005-12-04 16:41:43 +00:00
|
|
|
from misc import *
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def action(actiontype, fact):
|
2005-12-04 18:42:24 +00:00
|
|
|
res = actiontype + "_"
|
|
|
|
res += str(fact.step)
|
|
|
|
res += fact.spdl()
|
|
|
|
return res
|
|
|
|
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def getRoles(protocol):
|
|
|
|
roles = []
|
2005-12-04 18:42:24 +00:00
|
|
|
for rule in protocol:
|
2005-12-05 11:16:06 +00:00
|
|
|
roles += rule.getActors()
|
|
|
|
return uniq(roles)
|
2005-12-04 18:42:24 +00:00
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
class Event(object):
|
|
|
|
""" SPDL role event """
|
|
|
|
def substitute(self, msgfrom, msgto):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def spdl(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
class CommEvent(Event):
|
|
|
|
""" SPDL message event """
|
|
|
|
def __init__(self,type,label,fromrole,torole,message):
|
|
|
|
self.type = type
|
|
|
|
self.label = label
|
|
|
|
self.fromrole = fromrole
|
|
|
|
self.torole = torole
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def substitute(self, msgfrom, msgto):
|
|
|
|
self.message = self.message.substitute(msgfrom, msgto)
|
|
|
|
|
|
|
|
def spdl(self):
|
|
|
|
res = str(self.type) + "_"
|
|
|
|
res += str(self.label)
|
|
|
|
res += "("
|
|
|
|
res += self.fromrole.spdl()
|
|
|
|
res += ","
|
|
|
|
res += self.torole.spdl()
|
|
|
|
res += ", "
|
|
|
|
res += self.message.spdl(False)
|
|
|
|
res += " )"
|
|
|
|
return res
|
|
|
|
|
|
|
|
def inTerms(self):
|
|
|
|
l = []
|
|
|
|
l += self.fromrole.inTerms()
|
|
|
|
l += self.torole.inTerms()
|
|
|
|
l += self.message.inTerms()
|
|
|
|
return l
|
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
class Role(object):
|
|
|
|
""" Containts a list of rules, to be executed sequentially """
|
|
|
|
def __init__(self,name,actor):
|
|
|
|
self.name = name
|
|
|
|
self.rules = []
|
|
|
|
self.actor = actor
|
2005-12-05 14:28:09 +00:00
|
|
|
self.events = []
|
|
|
|
self.knowledge = If.MsgList([])
|
|
|
|
self.constants = If.MsgList([])
|
|
|
|
self.variables = If.MsgList([])
|
2005-12-05 14:58:35 +00:00
|
|
|
self.asymmetric = If.MsgList([]) # Asymmetric keys
|
2005-12-05 11:16:06 +00:00
|
|
|
|
|
|
|
def prependRule(self,rule):
|
|
|
|
self.rules = [rule] + self.rules
|
2005-12-05 14:28:09 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def getLength(self):
|
|
|
|
return len(self.rules)
|
2005-12-04 18:42:24 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def getFirst(self):
|
|
|
|
if self.getLength() > 0:
|
|
|
|
return self.rules[0]
|
|
|
|
else:
|
|
|
|
return None
|
2005-12-04 18:42:24 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def getFirstStep(self):
|
|
|
|
return self.getFirst().getStepFrom()
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def getActor(self):
|
|
|
|
return self.actor
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def __str__(self):
|
|
|
|
res = "Role " + self.name + "\n\n"
|
|
|
|
for rule in self.rules:
|
|
|
|
res += str(rule)
|
|
|
|
res += "\n\n"
|
|
|
|
return res
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
def appendEvent(self, event):
|
|
|
|
self.event += [event]
|
2005-12-05 14:58:35 +00:00
|
|
|
|
|
|
|
def substitute(self, msgfrom, msgto):
|
|
|
|
def subst(o):
|
|
|
|
o = o.substitute(msgfrom, msgto)
|
|
|
|
|
|
|
|
subst(self.events)
|
|
|
|
subst(self.knowledge)
|
|
|
|
subst(self.constants)
|
|
|
|
subst(self.variables)
|
|
|
|
subst(self.asymmetric)
|
2005-12-05 14:28:09 +00:00
|
|
|
|
|
|
|
def inTerms(self):
|
|
|
|
l = []
|
|
|
|
for ev in self.events:
|
|
|
|
l += ev.inTerms()
|
|
|
|
return l
|
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def spdl(self):
|
2005-12-05 14:28:09 +00:00
|
|
|
pf = "\t\t"
|
|
|
|
pfc = pf + "// "
|
|
|
|
|
|
|
|
# Start output
|
2005-12-05 11:16:06 +00:00
|
|
|
res = ""
|
|
|
|
if len(self.rules) == 0:
|
|
|
|
return res
|
2005-12-05 14:28:09 +00:00
|
|
|
res += "\trole " + self.name + "\n"
|
|
|
|
res += "\t{\n"
|
|
|
|
|
2005-12-05 14:58:35 +00:00
|
|
|
res += pfc + "Rule list based messages\n\n"
|
|
|
|
res += pfc + "Asymmetric keys: " + str(self.asymmetric) + "\n"
|
2005-12-05 14:28:09 +00:00
|
|
|
|
|
|
|
# Message sequence (based on rules)
|
|
|
|
res += pfc + "Knowledge before: " + str(self.rules[0].before.knowledge) + "\n"
|
2005-12-05 11:16:06 +00:00
|
|
|
for rule in self.rules:
|
|
|
|
# Read
|
|
|
|
if rule.readFact != None:
|
2005-12-05 14:28:09 +00:00
|
|
|
res += pfc + action("read",rule.readFact) + ";\n"
|
2005-12-05 11:16:06 +00:00
|
|
|
# Show knowledge extending for this read
|
2005-12-05 14:28:09 +00:00
|
|
|
res += pfc + "Knowledge delta: " + str(rule.before.runknowledge) + " -> " + str(rule.after.runknowledge) + "\n"
|
2005-12-05 11:16:06 +00:00
|
|
|
|
|
|
|
# Send
|
|
|
|
if rule.sendFact != None:
|
2005-12-05 14:28:09 +00:00
|
|
|
res += pfc + action("send",rule.sendFact) + ";\n"
|
|
|
|
res += "\n"
|
|
|
|
|
2005-12-05 14:58:35 +00:00
|
|
|
# TODO declare constants, variables
|
|
|
|
res += pfc + "Constants and variables\n"
|
|
|
|
if len(self.constants) > 0:
|
|
|
|
res += pf + "const " + self.constants.spdl() + ";\n"
|
|
|
|
if len(self.variables) > 0:
|
|
|
|
res += pf + "var " + self.variables.spdl() + ";\n"
|
|
|
|
res += "\n"
|
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
# Message sequence (based on event list)
|
|
|
|
res += pfc + "Event list based messages\n"
|
|
|
|
for event in self.events:
|
|
|
|
res += pf + event.spdl() + ";\n"
|
|
|
|
res += "\n"
|
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
# TODO claims
|
2005-12-05 14:28:09 +00:00
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
# Close up
|
2005-12-05 14:28:09 +00:00
|
|
|
res += "\t}\n\n"
|
2005-12-05 11:16:06 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def __cmp__(self,other):
|
|
|
|
return cmp(self.name, other.name)
|
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
def sanitizeRole(protocol, role):
|
|
|
|
""" Get rid of If artefacts, and construct role.events """
|
|
|
|
rules = role.rules
|
|
|
|
|
|
|
|
# Create events for each rule
|
|
|
|
ruleevents = []
|
|
|
|
role.events = []
|
|
|
|
for rule in role.rules:
|
|
|
|
events = []
|
|
|
|
if rule.readFact != None:
|
|
|
|
f = rule.readFact
|
|
|
|
events.append(CommEvent("read", f.step, f.claimsender, f.recipient, f.message))
|
|
|
|
if rule.sendFact != None:
|
|
|
|
f = rule.sendFact
|
|
|
|
events.append(CommEvent("send", f.step, f.claimsender, f.recipient, f.message))
|
|
|
|
ruleevents.append(events)
|
|
|
|
role.events += events
|
|
|
|
|
|
|
|
# Try to substitute stuff until sane
|
|
|
|
# First check whether knowledge lists actually agree in length,
|
|
|
|
# otherwise this does not make sense at all.
|
2005-12-05 15:35:00 +00:00
|
|
|
for n in range(0,len(rules)-1):
|
2005-12-05 14:28:09 +00:00
|
|
|
knowbefore = rules[n].after.runknowledge
|
|
|
|
knowafter = rules[n+1].before.runknowledge
|
|
|
|
if len(knowbefore) != len(knowafter):
|
2005-12-05 15:35:00 +00:00
|
|
|
raise "KnowledgeDeltaLenDiff", n
|
2005-12-05 14:28:09 +00:00
|
|
|
else:
|
|
|
|
# The after items should be substituted by the
|
|
|
|
# before items
|
2005-12-05 15:35:00 +00:00
|
|
|
for i in range(0,len(knowbefore)):
|
2005-12-05 14:28:09 +00:00
|
|
|
# Substitute this item
|
|
|
|
msgfrom = knowafter[i]
|
|
|
|
msgto = knowbefore[i]
|
|
|
|
if msgfrom != msgto:
|
|
|
|
### TEST
|
2020-10-28 14:13:02 +00:00
|
|
|
print("Substituting %s by %s" % (str(msgfrom), str(msgto)))
|
2005-12-05 14:28:09 +00:00
|
|
|
# In all subsequent terms... TODO or
|
|
|
|
# just the next one?
|
2005-12-05 15:35:00 +00:00
|
|
|
for j in range(n+1, len(rules)):
|
2005-12-05 14:28:09 +00:00
|
|
|
events = ruleevents[j]
|
|
|
|
for ev in events:
|
|
|
|
ev.substitute(msgfrom, msgto)
|
|
|
|
|
|
|
|
|
|
|
|
# Extract knowledge etc
|
|
|
|
role.knowledge = role.rules[0].before.knowledge
|
|
|
|
role.constants = If.MsgList([])
|
|
|
|
role.variables = If.MsgList([])
|
|
|
|
l = uniq(role.inTerms())
|
|
|
|
replacelist = []
|
|
|
|
noncecounter = 0
|
|
|
|
for t in l:
|
|
|
|
if t not in role.knowledge:
|
|
|
|
if t.isVariable():
|
|
|
|
role.variables.append(t)
|
|
|
|
else:
|
|
|
|
# For now, we say local constants from the intermediate
|
|
|
|
# format are simply replaced by
|
|
|
|
# local constants from the
|
|
|
|
# operational semantics. This is
|
|
|
|
# not necessarily correct. TODO
|
|
|
|
### constants.append(t)
|
|
|
|
cname = "n"
|
2005-12-05 15:35:00 +00:00
|
|
|
rname = role.name.lower()
|
|
|
|
if rname.startswith("x"):
|
2005-12-05 14:28:09 +00:00
|
|
|
rname = rname[1:]
|
|
|
|
cname += rname
|
|
|
|
cname += str(noncecounter)
|
|
|
|
msg = If.Constant("nonce",cname)
|
|
|
|
noncecounter = noncecounter + 1
|
|
|
|
replacelist.append( (t,msg) )
|
|
|
|
role.constants.append(msg)
|
2005-12-05 14:58:35 +00:00
|
|
|
### TEST
|
2020-10-28 14:13:02 +00:00
|
|
|
print("Substituting %s by %s" % (str(t), str(msg)))
|
2005-12-05 14:28:09 +00:00
|
|
|
# Apply replacelist
|
|
|
|
if len(replacelist) > 0:
|
|
|
|
for ev in role.events:
|
|
|
|
for (f,t) in replacelist:
|
|
|
|
ev.substitute(f,t)
|
|
|
|
|
2005-12-05 14:58:35 +00:00
|
|
|
# Extract keys
|
|
|
|
akeys = []
|
|
|
|
for ev in role.events:
|
|
|
|
akeys += ev.message.aKeys()
|
|
|
|
role.asymmetric = uniq(akeys)
|
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
def extractRoles(protocol):
|
|
|
|
""" Extract the roles of a protocol description. This yields a
|
|
|
|
list of Role objects """
|
|
|
|
|
|
|
|
# Construct full list of rules to do
|
|
|
|
rulestodo = []
|
2005-12-04 18:42:24 +00:00
|
|
|
for rule in protocol:
|
2005-12-05 11:16:06 +00:00
|
|
|
if type(rule) == If.MessageRule:
|
|
|
|
rulestodo.append(rule)
|
|
|
|
|
|
|
|
# Now process them until none are left
|
|
|
|
# First, we have no rolenames yet
|
|
|
|
rolenames = []
|
|
|
|
roles = []
|
2005-12-05 15:35:00 +00:00
|
|
|
while (len(rulestodo) > 0):
|
2005-12-05 11:16:06 +00:00
|
|
|
# Pick one hrule (with the highest step number, maybe)
|
|
|
|
highest = rulestodo[0].getStepFrom()
|
|
|
|
hrule = rulestodo[0]
|
|
|
|
for rule in rulestodo:
|
|
|
|
step = rule.getStepFrom()
|
|
|
|
step = max(step,rule.getStepTo())
|
|
|
|
if step >= highest:
|
|
|
|
highest = step
|
|
|
|
hrule = rule
|
|
|
|
# hrule has been picked. Work back from here
|
|
|
|
# first make up a name
|
|
|
|
if len(hrule.getActors()) != 1:
|
2020-10-28 14:13:02 +00:00
|
|
|
print("Warning: weird actor list for hrule:", hrule.getActors())
|
2005-12-05 11:16:06 +00:00
|
|
|
name = "X"
|
|
|
|
actor = None
|
|
|
|
else:
|
|
|
|
actor = hrule.getActors()[0]
|
|
|
|
name = str(actor)
|
|
|
|
|
|
|
|
# This name is maybe already taken
|
|
|
|
if name in rolenames:
|
|
|
|
# Append counter
|
|
|
|
counter = 2
|
2005-12-05 15:35:00 +00:00
|
|
|
while (name + str(counter) in rolenames):
|
2005-12-05 11:16:06 +00:00
|
|
|
counter = counter+1
|
|
|
|
name = name + str(counter)
|
|
|
|
|
|
|
|
rolenames.append(name)
|
|
|
|
role = Role(name,actor)
|
|
|
|
roles.append(role)
|
|
|
|
|
|
|
|
# Transfer to rule
|
|
|
|
role.prependRule(hrule)
|
|
|
|
rulestodo.remove(hrule)
|
|
|
|
|
|
|
|
# Scan for preceding events until none is found
|
|
|
|
scan = True
|
2005-12-05 15:35:00 +00:00
|
|
|
while (scan and role.getFirstStep() != -1):
|
2005-12-05 11:16:06 +00:00
|
|
|
scan = False
|
2005-12-05 11:26:05 +00:00
|
|
|
for rule in protocol:
|
2005-12-05 11:16:06 +00:00
|
|
|
if actor in rule.getActors() and rule.getStepTo() == role.getFirstStep():
|
2005-12-05 11:26:05 +00:00
|
|
|
# Remove if not yet covered
|
2005-12-05 11:16:06 +00:00
|
|
|
scan = True
|
2005-12-05 11:26:05 +00:00
|
|
|
if rule in rulestodo:
|
|
|
|
rulestodo.remove(rule)
|
|
|
|
# Loop detection
|
|
|
|
if rule in role.rules:
|
|
|
|
# This is a loop TODO
|
2020-10-28 14:13:02 +00:00
|
|
|
print("Warning: loop detected for role", role.name)
|
2005-12-05 11:26:05 +00:00
|
|
|
scan = False # Current setting: stop scan
|
|
|
|
else:
|
|
|
|
# No loop, prepend
|
|
|
|
role.prependRule(rule)
|
2005-12-05 11:16:06 +00:00
|
|
|
|
2005-12-05 14:28:09 +00:00
|
|
|
# Role done, sanitize
|
|
|
|
sanitizeRole( protocol, role)
|
|
|
|
|
2005-12-05 11:16:06 +00:00
|
|
|
return roles
|
2005-12-04 14:46:37 +00:00
|
|
|
|
2005-12-04 18:42:24 +00:00
|
|
|
def generator(protocol):
|
2005-12-05 11:16:06 +00:00
|
|
|
roles = extractRoles(protocol)
|
|
|
|
roles.sort()
|
2005-12-04 14:46:37 +00:00
|
|
|
res = ""
|
2005-12-05 14:28:09 +00:00
|
|
|
res += "protocol " + protocol.getName() + " ("
|
|
|
|
namelist = []
|
|
|
|
for role in roles:
|
|
|
|
namelist += [role.name]
|
|
|
|
res += ", ".join(namelist)
|
|
|
|
res += ")\n"
|
|
|
|
res += "{\n"
|
2005-12-04 14:46:37 +00:00
|
|
|
for role in roles:
|
2005-12-05 11:16:06 +00:00
|
|
|
res += role.spdl()
|
2005-12-05 14:28:09 +00:00
|
|
|
res += "}\n"
|
2005-12-04 14:46:37 +00:00
|
|
|
return res
|
|
|
|
|