2008-08-25 13:59:42 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
|
|
|
Scyther : An automatic verifier for security protocols.
|
|
|
|
Copyright (C) 2007 Cas Cremers
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import externals """
|
|
|
|
import wx
|
|
|
|
import os
|
|
|
|
import sys
|
2010-05-15 23:13:33 +01:00
|
|
|
from subprocess import Popen, PIPE
|
2008-08-25 13:59:42 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import scyther components """
|
2010-05-15 23:13:33 +01:00
|
|
|
from Scyther import Misc as MiscScyther
|
2008-08-25 13:59:42 +01:00
|
|
|
|
|
|
|
""" Import scyther-gui components """
|
2009-04-30 14:06:28 +01:00
|
|
|
import Temporary
|
2008-08-25 13:59:42 +01:00
|
|
|
import Preference
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
try:
|
|
|
|
import Image
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def writeGraph(attackthread,txt,fp):
|
|
|
|
|
|
|
|
EDGE = 0
|
|
|
|
NODE = 1
|
|
|
|
DEFAULT = 2
|
|
|
|
ALL = 3
|
|
|
|
|
|
|
|
def graphLine(txt):
|
|
|
|
fp.write("\t%s;\n" % (txt))
|
|
|
|
|
|
|
|
def setAttr(atxt,EdgeNodeDefAll=ALL):
|
|
|
|
if EdgeNodeDefAll == ALL:
|
|
|
|
setAttr(atxt,EDGE)
|
|
|
|
setAttr(atxt,NODE)
|
|
|
|
setAttr(atxt,DEFAULT)
|
|
|
|
else:
|
|
|
|
if EdgeNodeDefAll == EDGE:
|
|
|
|
edge = "edge"
|
|
|
|
elif EdgeNodeDefAll == NODE:
|
|
|
|
edge = "node"
|
|
|
|
else:
|
|
|
|
graphLine("%s" % atxt)
|
|
|
|
return
|
|
|
|
graphLine("%s [%s]" % (edge,atxt))
|
|
|
|
|
|
|
|
if sys.platform.startswith("darwin"):
|
|
|
|
attackthread.fontname = "Helvetica"
|
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
attackthread.fontname = "Courier"
|
|
|
|
else:
|
|
|
|
#font = wx.Font(9,wx.SWISS,wx.NORMAL,wx.NORMAL)
|
|
|
|
#attackthread.fontname = font.GetFaceName()
|
|
|
|
attackthread.fontname = "\"Helvetica\""
|
|
|
|
|
|
|
|
# write all graph lines but add layout modifiers
|
|
|
|
for l in txt.splitlines():
|
|
|
|
fp.write(l)
|
|
|
|
if l.startswith("digraph"):
|
|
|
|
# Write additional stuff for this graph
|
|
|
|
#
|
|
|
|
# [CC][x] This dpi setting messed up quite a bit
|
|
|
|
#graphLine("dpi=96")
|
|
|
|
graphLine("rankdir=TB")
|
|
|
|
#graphLine("nodesep=0.1")
|
|
|
|
#graphLine("ranksep=0.001")
|
|
|
|
#graphLine("mindist=0.1")
|
|
|
|
|
|
|
|
# Set fontname
|
|
|
|
if attackthread.fontname:
|
|
|
|
fontstring = "fontname=%s" % (attackthread.fontname)
|
|
|
|
setAttr(fontstring)
|
|
|
|
|
|
|
|
# Stupid Mac <> Graphviz bug fix
|
|
|
|
if (sys.platform.startswith("mac")) or (sys.platform.startswith("darwin")):
|
|
|
|
# Note that dot on Mac cannot find the fonts by default,
|
|
|
|
# and we have to set them accordingly.
|
|
|
|
os.environ["DOTFONTPATH"]="~/Library/Fonts:/Library/Fonts:/System/Library/Fonts"
|
|
|
|
|
|
|
|
# Select font size
|
|
|
|
if attackthread.parent and attackthread.parent.mainwin:
|
|
|
|
fontsize = attackthread.parent.mainwin.settings.fontsize
|
|
|
|
setAttr("fontsize=%s" % fontsize)
|
|
|
|
#setAttr("height=\"0.1\"",NODE)
|
|
|
|
#setAttr("width=\"1.0\"",NODE)
|
|
|
|
#setAttr("margin=\"0.3,0.03\"",NODE)
|
|
|
|
|
|
|
|
|
|
|
|
def makeImageDot(dotdata,attackthread=None):
|
|
|
|
""" create image for this particular dot data """
|
|
|
|
|
|
|
|
if Preference.usePIL():
|
|
|
|
# If we have the PIL library, we can do postscript! great
|
|
|
|
# stuff.
|
|
|
|
type = "ps"
|
|
|
|
ext = ".ps"
|
|
|
|
else:
|
|
|
|
# Ye olde pnge file
|
|
|
|
type = "png"
|
|
|
|
ext = ".png"
|
|
|
|
|
|
|
|
# command to write to temporary file
|
2009-04-30 14:06:28 +01:00
|
|
|
(fd2,fpname2) = Temporary.tempcleaned(ext)
|
2008-08-25 13:59:42 +01:00
|
|
|
f = os.fdopen(fd2,'w')
|
|
|
|
|
2010-05-15 23:13:33 +01:00
|
|
|
# Set up command
|
2008-08-25 13:59:42 +01:00
|
|
|
cmd = "dot -T%s" % (type)
|
|
|
|
|
|
|
|
# execute command
|
2010-05-15 23:13:33 +01:00
|
|
|
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
|
2008-08-25 13:59:42 +01:00
|
|
|
|
|
|
|
if attackthread:
|
2010-05-15 23:13:33 +01:00
|
|
|
writeGraph(attackthread,dotdata,p.stdin)
|
2008-08-25 13:59:42 +01:00
|
|
|
else:
|
2010-05-15 23:13:33 +01:00
|
|
|
p.stdin.write(dotdata)
|
2008-08-25 13:59:42 +01:00
|
|
|
|
2010-05-15 23:13:33 +01:00
|
|
|
p.stdin.close()
|
2008-08-25 13:59:42 +01:00
|
|
|
|
2010-05-15 23:13:33 +01:00
|
|
|
for l in p.stdout.read():
|
2008-08-25 13:59:42 +01:00
|
|
|
f.write(l)
|
|
|
|
|
2010-05-15 23:13:33 +01:00
|
|
|
p.stdout.close()
|
2008-08-25 13:59:42 +01:00
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return (fpname2, type)
|
|
|
|
|
|
|
|
|
|
|
|
def makeImage(attack,attackthread=None):
|
|
|
|
""" create image for this particular attack """
|
|
|
|
|
|
|
|
""" This should clearly be a method of 'attack' """
|
|
|
|
|
|
|
|
(name,type) = makeImageDot(attack.scytherDot,attackthread)
|
|
|
|
# if this is done, store and report
|
|
|
|
attack.file = name
|
|
|
|
attack.filetype = type
|
|
|
|
|
|
|
|
|
|
|
|
def testImage():
|
|
|
|
"""
|
|
|
|
We generate a postscript file from a dot file, and see what happens.
|
|
|
|
"""
|
|
|
|
|
|
|
|
dotdata = "digraph X {\nA->B;\n}\n"
|
|
|
|
(filename,filetype) = makeImageDot(dotdata)
|
|
|
|
testimage = Image.open(filename)
|
|
|
|
|
2008-08-27 09:02:20 +01:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# vim: set ts=4 sw=4 et list lcs=tab\:>-:
|