2006-08-02 13:59:57 +01:00
|
|
|
#!/usr/bin/python
|
2012-04-23 14:01:15 +01:00
|
|
|
from __future__ import division # 2.2+-only
|
2007-06-11 13:09:24 +01:00
|
|
|
"""
|
|
|
|
Scyther : An automatic verifier for security protocols.
|
2013-10-05 23:56:12 +01:00
|
|
|
Copyright (C) 2007-2013 Cas Cremers
|
2007-06-11 13:09:24 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import externals """
|
|
|
|
import wx
|
2008-08-27 09:04:22 +01:00
|
|
|
import os
|
|
|
|
from Misc import *
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import scyther-gui components """
|
|
|
|
import Icon
|
2006-08-10 12:50:57 +01:00
|
|
|
import Preference
|
2008-05-02 16:10:29 +01:00
|
|
|
import Error
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
2008-08-25 13:59:42 +01:00
|
|
|
try:
|
2006-08-10 12:50:57 +01:00
|
|
|
import Image
|
2008-08-25 13:59:42 +01:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2006-08-10 12:50:57 +01:00
|
|
|
#---------------------------------------------------------------------------
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
class AttackDisplay(wx.ScrolledWindow):
|
2006-12-13 15:43:10 +00:00
|
|
|
"""
|
|
|
|
Display an attack (inside a tab or not)
|
|
|
|
"""
|
2006-08-02 23:07:29 +01:00
|
|
|
def __init__(self, daddy, parent, attack):
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
self.win = daddy
|
2006-08-02 23:07:29 +01:00
|
|
|
self.attack = attack
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
wx.ScrolledWindow.__init__(self,parent,id=-1)
|
2006-12-13 11:59:44 +00:00
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
self.Image = wx.StaticBitmap(self, -1, wx.EmptyBitmap(1,1))
|
|
|
|
self.box = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
self.box.Add(self.Image,1,wx.ALIGN_CENTER)
|
|
|
|
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
self.hbox.Add(self.box,1,wx.ALIGN_CENTER)
|
|
|
|
self.SetSizer(self.hbox)
|
|
|
|
|
2006-08-08 16:14:51 +01:00
|
|
|
self.original = None
|
|
|
|
|
2006-08-04 23:29:51 +01:00
|
|
|
filename = attack.file
|
2006-08-05 00:22:03 +01:00
|
|
|
if attack.filetype == "png":
|
|
|
|
self.original = wx.Image(filename,wx.BITMAP_TYPE_PNG)
|
|
|
|
elif attack.filetype == "ps":
|
|
|
|
# depends on PIL lib
|
2008-05-02 16:10:29 +01:00
|
|
|
try:
|
|
|
|
self.original = Image.open(filename)
|
|
|
|
except:
|
|
|
|
Preference.doNotUsePIL()
|
|
|
|
raise Error.PILError
|
2006-08-05 00:22:03 +01:00
|
|
|
else:
|
|
|
|
print "Unknown file type %s." % (self.filetype)
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
# TODO self.Bind(wxSizeEvent
|
2006-08-11 16:44:36 +01:00
|
|
|
self.update(True)
|
2006-08-08 16:14:51 +01:00
|
|
|
self.Fit()
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def OnSize(self,event):
|
2006-08-11 16:43:04 +01:00
|
|
|
self.update(False)
|
2006-08-02 13:59:57 +01:00
|
|
|
event.Skip()
|
|
|
|
|
2006-08-11 16:43:04 +01:00
|
|
|
def update(self,force=True):
|
|
|
|
|
|
|
|
if not force:
|
|
|
|
if not self.win.fit:
|
|
|
|
return
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-12-13 15:43:10 +00:00
|
|
|
# This is needed, don't ask me why.
|
2006-08-02 13:59:57 +01:00
|
|
|
self.SetScrollbars(0,0,0,0,0,0)
|
|
|
|
|
2006-12-13 15:43:10 +00:00
|
|
|
(framewidth,frameheight) = self.GetClientSizeTuple()
|
|
|
|
(virtualwidth,virtualheight) = (framewidth,frameheight)
|
|
|
|
|
|
|
|
def makefit(width,height):
|
2006-08-04 23:08:00 +01:00
|
|
|
if self.win.fit:
|
2006-08-07 13:13:39 +01:00
|
|
|
# determine scaling factors for fitting
|
2006-12-13 15:43:10 +00:00
|
|
|
wfactor = float(framewidth) / width
|
|
|
|
hfactor = float(frameheight) / height
|
2006-08-07 13:13:39 +01:00
|
|
|
|
|
|
|
# select smallest factor (so it will fit)
|
|
|
|
if hfactor < wfactor:
|
|
|
|
factor = hfactor
|
|
|
|
else:
|
|
|
|
factor = wfactor
|
|
|
|
|
|
|
|
# apply scaling factor
|
2006-12-13 15:43:10 +00:00
|
|
|
width = width * factor
|
|
|
|
height = height * factor
|
|
|
|
else:
|
|
|
|
factor = 1.0
|
|
|
|
|
|
|
|
return (factor, int(width), int(height))
|
2006-08-05 00:22:03 +01:00
|
|
|
|
|
|
|
if self.attack.filetype == "png":
|
|
|
|
bmp = self.original
|
|
|
|
if not bmp.Ok():
|
2006-08-10 15:42:51 +01:00
|
|
|
bmp = wx.EmptyImage(1,1)
|
2006-08-05 00:22:03 +01:00
|
|
|
else:
|
2006-12-13 15:43:10 +00:00
|
|
|
(originalwidth,originalheight) = (bmp.GetWidth(), bmp.GetHeight())
|
2006-08-05 00:22:03 +01:00
|
|
|
if self.win.fit:
|
2006-12-13 15:43:10 +00:00
|
|
|
(factor, virtualwidth, virtualheight) = makefit(originalwidth,originalheight)
|
|
|
|
bmp = self.original.Scale(virtualwidth,virtualheight)
|
2006-08-05 00:22:03 +01:00
|
|
|
self.Image.SetBitmap(wx.BitmapFromImage(bmp))
|
2006-08-07 12:54:43 +01:00
|
|
|
|
2006-08-05 00:22:03 +01:00
|
|
|
elif self.attack.filetype == "ps":
|
|
|
|
pil = self.original.copy()
|
2006-12-13 15:43:10 +00:00
|
|
|
(originalwidth,originalheight) = pil.size
|
|
|
|
(factor, virtualwidth, virtualheight) = makefit(originalwidth,originalheight)
|
2006-08-05 00:22:03 +01:00
|
|
|
# we really only want antialias when it's smaller
|
2006-12-13 15:43:10 +00:00
|
|
|
if factor < 1.0:
|
|
|
|
pil.thumbnail((virtualwidth,virtualheight),Image.ANTIALIAS)
|
|
|
|
else:
|
|
|
|
pil.thumbnail((virtualwidth,virtualheight))
|
2006-08-05 00:22:03 +01:00
|
|
|
|
|
|
|
image = wx.EmptyImage(pil.size[0],pil.size[1])
|
|
|
|
image.SetData(pil.convert('RGB').tostring())
|
|
|
|
self.Image.SetBitmap(image.ConvertToBitmap())
|
2006-12-13 15:43:10 +00:00
|
|
|
|
2006-08-05 00:22:03 +01:00
|
|
|
else:
|
|
|
|
print "Unknown file type %s." % (self.attack.filetype)
|
2006-08-04 23:08:00 +01:00
|
|
|
|
2006-12-13 15:43:10 +00:00
|
|
|
self.SetVirtualSize((virtualwidth,virtualheight))
|
2006-08-04 23:08:00 +01:00
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
#self.box.SetItemMinSize(self.Image.GetContainingSizer())
|
|
|
|
self.box.Layout()
|
|
|
|
|
|
|
|
step = 20
|
2012-04-23 14:01:15 +01:00
|
|
|
xn = int(virtualwidth // step) + 1
|
|
|
|
yn = int(virtualheight // step) + 1
|
2006-08-02 13:59:57 +01:00
|
|
|
self.SetScrollbars(step,step,xn,yn,0,0)
|
|
|
|
|
2008-08-26 16:14:08 +01:00
|
|
|
"""
|
|
|
|
Pop up menu
|
|
|
|
"""
|
|
|
|
self.popupmenu = wx.Menu()
|
2008-08-27 09:04:22 +01:00
|
|
|
item = self.popupmenu.Append(-1,"Export image (.png)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportPng, item)
|
|
|
|
item = self.popupmenu.Append(-1,"Export image (.ps)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportPs, item)
|
|
|
|
item = self.popupmenu.Append(-1,"Export image (.pdf)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportPdf, item)
|
|
|
|
item = self.popupmenu.Append(-1,"Export image (.svg)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportSvg, item)
|
|
|
|
item = self.popupmenu.Append(-1,"Export image (.fig)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportFig, item)
|
2008-08-26 16:14:08 +01:00
|
|
|
item = self.popupmenu.Append(-1,"Export graphviz data (.dot)")
|
|
|
|
self.Bind(wx.EVT_MENU, self.OnExportDot, item)
|
|
|
|
|
|
|
|
self.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
self.Refresh()
|
|
|
|
|
2008-08-26 16:14:08 +01:00
|
|
|
def OnShowPopup(self, event):
|
|
|
|
pos = event.GetPosition()
|
|
|
|
pos = self.Image.ScreenToClient(pos)
|
|
|
|
self.PopupMenu(self.popupmenu, pos)
|
|
|
|
|
|
|
|
def OnPopupItemSelected(self, event):
|
|
|
|
item = self.popupmenu.FindItemById(event.GetId())
|
|
|
|
text = item.GetText()
|
|
|
|
wx.MessageBox("You selected item %s" % text)
|
|
|
|
|
|
|
|
def askUserForFilename(self, **dialogOptions):
|
|
|
|
dialog = wx.FileDialog(self, **dialogOptions)
|
|
|
|
if dialog.ShowModal() == wx.ID_OK:
|
|
|
|
res = "%s/%s" % (dialog.GetDirectory(), dialog.GetFilename())
|
|
|
|
else:
|
|
|
|
res = None
|
|
|
|
dialog.Destroy()
|
|
|
|
return res
|
|
|
|
|
2008-08-27 09:04:22 +01:00
|
|
|
def saveFileName(self, ext):
|
2008-08-26 16:14:08 +01:00
|
|
|
(p,r,l) = self.win.claim.triplet()
|
|
|
|
prefix = "pattern-%s_%s_%s-%s" % (p,r,l,self.attack.id)
|
|
|
|
suggested = "%s.%s" % (prefix,ext)
|
|
|
|
res = self.askUserForFilename(style=wx.SAVE, wildcard="*.%s" % (ext), defaultFile = "%s" % (suggested))
|
2008-08-27 09:04:22 +01:00
|
|
|
return res
|
|
|
|
|
|
|
|
def exportImage(self, type,ext=None):
|
|
|
|
if ext == None:
|
|
|
|
ext = type
|
|
|
|
res = self.saveFileName(ext)
|
2008-08-26 16:14:08 +01:00
|
|
|
if res != None:
|
2008-08-27 09:04:22 +01:00
|
|
|
cmd = "dot -T%s" % (type)
|
|
|
|
cmdpushwrite(cmd,self.attack.scytherDot,res)
|
|
|
|
|
|
|
|
def OnExportPng(self, event):
|
|
|
|
self.exportImage("png")
|
|
|
|
|
|
|
|
def OnExportPs(self, event):
|
|
|
|
self.exportImage("ps")
|
2008-08-26 16:14:08 +01:00
|
|
|
|
2008-08-27 09:04:22 +01:00
|
|
|
def OnExportPdf(self, event):
|
|
|
|
self.exportImage("pdf")
|
|
|
|
|
|
|
|
def OnExportSvg(self, event):
|
|
|
|
self.exportImage("svg")
|
|
|
|
|
|
|
|
def OnExportFig(self, event):
|
|
|
|
self.exportImage("fig")
|
2008-08-26 16:14:08 +01:00
|
|
|
|
|
|
|
def OnExportDot(self, event):
|
2008-08-27 09:04:22 +01:00
|
|
|
res = self.saveFileName("dot")
|
|
|
|
if res != None:
|
|
|
|
fp = open(res,'w')
|
|
|
|
fp.write(self.attack.scytherDot)
|
|
|
|
fp.close()
|
|
|
|
|
2008-08-26 16:14:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-07 15:02:59 +01:00
|
|
|
#---------------------------------------------------------------------------
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
class AttackWindow(wx.Frame):
|
|
|
|
def __init__(self,cl):
|
2006-12-13 15:43:10 +00:00
|
|
|
super(AttackWindow, self).__init__(None, size=(800,800))
|
2006-12-14 14:06:50 +00:00
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
self.claim = cl
|
2006-08-07 15:02:59 +01:00
|
|
|
|
|
|
|
# TODO maybe fitting defaults should come from Preferences.
|
2006-12-12 16:33:21 +00:00
|
|
|
# Now, it is default no even if we have PIL, for performance
|
|
|
|
# reasons.
|
|
|
|
self.fit = False
|
2006-08-07 15:02:59 +01:00
|
|
|
|
2016-02-28 22:18:21 +00:00
|
|
|
aTable = wx.AcceleratorTable([
|
|
|
|
(wx.ACCEL_CTRL, ord('W'), wx.ID_CLOSE),
|
|
|
|
(wx.ACCEL_NORMAL, wx.WXK_ESCAPE, wx.ID_CLOSE),
|
|
|
|
])
|
|
|
|
self.SetAcceleratorTable(aTable)
|
|
|
|
self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
|
|
|
|
self.Bind(wx.EVT_MENU, self.onCloseWindow, id=wx.ID_CLOSE)
|
2006-08-02 13:59:57 +01:00
|
|
|
self.CreateInteriorWindowComponents()
|
|
|
|
|
|
|
|
Icon.ScytherIcon(self)
|
|
|
|
self.SetTitle()
|
|
|
|
|
|
|
|
def SetTitle(self):
|
|
|
|
|
2006-08-09 13:11:55 +01:00
|
|
|
tstr = self.claim.stateName(len(self.claim.attacks),True)
|
2006-08-02 22:56:14 +01:00
|
|
|
tstr += " for claim %s" % self.claim.id
|
2006-08-02 13:59:57 +01:00
|
|
|
super(AttackWindow, self).SetTitle(tstr)
|
|
|
|
|
|
|
|
def CreateInteriorWindowComponents(self):
|
|
|
|
''' Create "interior" window components. In this case it is the
|
|
|
|
attack picture. '''
|
|
|
|
|
2006-08-07 15:02:59 +01:00
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
2006-08-10 15:58:08 +01:00
|
|
|
|
|
|
|
# Make zoom buttons
|
|
|
|
if Preference.usePIL():
|
|
|
|
buttons = wx.BoxSizer(wx.HORIZONTAL)
|
2006-08-11 10:41:12 +01:00
|
|
|
bt = wx.ToggleButton(self,-1,"Fit to window")
|
|
|
|
bt.SetValue(self.fit)
|
2006-08-10 15:58:08 +01:00
|
|
|
buttons.Add(bt,0)
|
2006-08-11 10:41:12 +01:00
|
|
|
self.Bind(wx.EVT_TOGGLEBUTTON, self.OnFit, bt)
|
2006-08-10 15:58:08 +01:00
|
|
|
sizer.Add(buttons, 0, wx.ALIGN_LEFT)
|
2006-08-07 15:02:59 +01:00
|
|
|
|
|
|
|
# Add attacks (possible with tabs)
|
2006-08-02 13:59:57 +01:00
|
|
|
self.displays=[]
|
2006-08-03 14:40:39 +01:00
|
|
|
attacks = self.claim.attacks
|
2007-05-18 13:06:29 +01:00
|
|
|
|
2006-08-03 14:40:39 +01:00
|
|
|
n = len(attacks)
|
|
|
|
if n <= 1:
|
2006-08-02 13:59:57 +01:00
|
|
|
# Just a single window
|
2006-08-07 15:02:59 +01:00
|
|
|
dp = AttackDisplay(self, self, attacks[0])
|
|
|
|
self.displays.append(dp)
|
2006-08-02 13:59:57 +01:00
|
|
|
else:
|
|
|
|
# Multiple tabs
|
2006-08-07 15:02:59 +01:00
|
|
|
dp = wx.Notebook(self,-1)
|
2006-08-03 14:40:39 +01:00
|
|
|
for i in range(0,n):
|
2006-08-07 15:02:59 +01:00
|
|
|
disp = AttackDisplay(self,dp,attacks[i])
|
2006-08-09 13:11:55 +01:00
|
|
|
classname = "%s %i" % (self.claim.stateName(1,True),(i+1))
|
2006-08-07 15:02:59 +01:00
|
|
|
dp.AddPage(disp, classname)
|
2006-08-02 13:59:57 +01:00
|
|
|
self.displays.append(disp)
|
|
|
|
|
2006-08-07 15:02:59 +01:00
|
|
|
sizer.Add(dp, 1, wx.EXPAND,1)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-07 15:02:59 +01:00
|
|
|
self.SetSizer(sizer)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-11 16:43:04 +01:00
|
|
|
def update(self,force=False):
|
2006-08-02 13:59:57 +01:00
|
|
|
for t in self.displays:
|
2006-08-11 16:43:04 +01:00
|
|
|
t.update(force)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def OnFit(self,event):
|
|
|
|
|
|
|
|
if self.fit:
|
|
|
|
self.fit = False
|
|
|
|
else:
|
|
|
|
self.fit = True
|
2006-08-11 16:43:04 +01:00
|
|
|
self.update(True)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def OnRealSize(self):
|
|
|
|
|
|
|
|
self.fit = False
|
2006-08-11 16:43:04 +01:00
|
|
|
self.update(True)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-07 15:02:59 +01:00
|
|
|
def OnSize(self):
|
|
|
|
self.Refresh()
|
|
|
|
|
|
|
|
def OnZoom100(self,evt):
|
|
|
|
self.fit = False
|
2006-08-11 16:43:04 +01:00
|
|
|
self.update(True)
|
2006-08-11 10:41:12 +01:00
|
|
|
self.Refresh()
|
2006-08-07 15:02:59 +01:00
|
|
|
|
|
|
|
def OnZoomFit(self,evt):
|
|
|
|
self.fit = True
|
2006-08-11 16:43:04 +01:00
|
|
|
self.update(True)
|
2006-08-11 10:41:12 +01:00
|
|
|
self.Refresh()
|
|
|
|
|
2016-02-28 22:18:21 +00:00
|
|
|
def onCloseWindow(self,evt):
|
|
|
|
self.Destroy()
|
|
|
|
|
2008-08-27 09:02:20 +01:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# vim: set ts=4 sw=4 et list lcs=tab\:>-:
|