File: list.py

package info (click to toggle)
tboot 1.10.5-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,000 kB
  • sloc: ansic: 56,029; python: 6,595; perl: 2,303; sh: 455; asm: 442; makefile: 377
file content (1345 lines) | stat: -rw-r--r-- 59,256 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
#!/usr/bin/python
#  Copyright (c) 2013, Intel Corporation. All rights reserved.

# using print() built infunction, disable print statement
from __future__ import print_function

import string

#
# wxPython is not part of the standard Python distribution and has to be downloaded and installed separately.
# Tell the user that wxPython is required but has not been found
#
try:
  import wx
except ImportError:
  raise ImportError, "Please download the appropriate version of wxPython from www.wxpython.org"

try:
  import os
  import shutil
except ImportError:
  raise ImportError, "import OS failed"

from defines import DEFINES
from pdef import PLIST_DEF, MLE_DEF, PCONF_DEF, SBIOS_DEF
from LcpPolicy import LCP_POLICY2, LCP_SIGNATURE2
from util import UTILS
utilities = UTILS()

from sbios import SBIOS
from mle import MLE
from pconf import PCONF
from stm import STM
from sbiosLegacy import SBIOSLegacy
from pconfLegacy import PCONFLegacy
from mleLegacy import MLELegacy
from pdef import *


try:
    import cPickle as pickle
except ImportError:
    import pickle         # fall back on Python version


import array
import base64
from struct import *


# TXT Policy Generator Tool
# List Class - Policy Definition File Lists
#
class LIST( object ):
  """__init__() - List class constructor"""
  def __init__( self ):
    """LIST __init__ constructor"""
    self.listPanelWidgets = []
    self.visibleElement = DEFINES.ELEMENT_NAME_NONE
    self.includedElements = []
    #print("in LIST __init__() listPanelWidgets=%s" % (self.listPanelWidgets))   # DBGDBG

  #
  # create the List Panel
  #     LIST X
  #       Version 2.0   Number of 0 Select  NONE      DELETE ELEMENT
  #                     Elements    Element
  #       Revocation 23 [] Signed                     ADD SBIOS
  #       Count            Key File my.key  BROWSE    ADD MLE
  #       Allowed    23    Key Size 2048  RSA PKCS1.5 ADD PCONF
  #      RESET   [] Sync                  Algorithm
  #
  def createListPanel(self, wx, parent, listNumber, pdef, statusBar):
    """createListPanel - create the List Panel"""

    #print("createListPanel - list %i" % (listNumber))
    self.pdef = pdef
    self.StatusBar = statusBar
    self.parent = parent
    parentSizer = parent.GetSizer()

    # create the List Panel sizers
    self.listPanel = wx.Panel(parent, -1)
    self.listPanelSizer = wx.GridBagSizer(hgap=5, vgap=5)
    self.listPanel.SetSizer(self.listPanelSizer)
    #listHorizSizer = wx.BoxSize4r(wx.HORIZONTAL)

    #listLabelText = "LIST " + str(listNumber)
    listLabelText = "LIST"
    self.listLabel = wx.StaticText(self.listPanel, -1, listLabelText)
    font = wx.Font( 18, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)
    self.listLabel.SetFont( font )
    self.listPanelSizer.Add( self.listLabel, pos=(0, 0))
    self.listPanelWidgets = [self.listLabel]

    self.listLabelNum = wx.TextCtrl( self.listPanel, value="X", size=(30, -1))
    self.listLabelNum.Enable( False )              # LIST num cannot be modified by user
    self.listLabelNum.SetFont( font )
    #self.listLabelNum.SetBackgroundColour(self.listPanel.GetBackgroundColour())            # no change to color??   Fore or Background N/C
    self.listLabelNum.SetFocus()
    self.listLabelNum.Refresh()   # no help
    self.listLabelNum.ChangeValue(str(listNumber))
    self.listPanelSizer.Add( self.listLabelNum,  pos=(0,1))
    self.listPanelWidgets.append(self.listLabelNum)

    # LIST Version text box - PDEF.PolList[n].ListVersion
    # Change list version number to corresponds to policy version number
    #policyversion = str(pdef.PolVersionMajor) + '.' + str(pdef.PolVersionMinor)
    #listversion = DEFINES.SUPPORTED_LCP_VERSION[policyversion]
    #majorstring, minorstring = listversion.split('.')
    #self.pdef.PolListInfo[str(listNumber-1)].ListVersionMajor = int(majorstring)
    #self.pdef.PolListInfo[str(listNumber-1)].ListVersionMinor = int(minorstring)
    self.versionLabel = wx.StaticText(self.listPanel, label="Version: ", size = (90, -1), style = wx.ALIGN_RIGHT)
    self.listPanelSizer.Add( self.versionLabel, pos=(1,0))
    self.listPanelWidgets.append(self.versionLabel)
    version = str(self.pdef.PolListInfo[str(listNumber-1)].ListVersionMajor)+"."+str(self.pdef.PolListInfo[str(listNumber-1)].ListVersionMinor)
    self.versionEdit  = wx.TextCtrl( self.listPanel, value=version, size=(30, -1))
    self.versionEdit.Enable( False )              # Version cannot be modified
    self.listPanelSizer.Add( self.versionEdit,  pos=(1,1))
    self.listPanelWidgets.append(self.versionEdit)

    # Number Elements text box - number of elements that are marked VALID
    self.numberElementsLabel = wx.StaticText(self.listPanel, label="Number of Elements ")
    self.listPanelSizer.Add( self.numberElementsLabel, pos=(2,2))
    self.listPanelWidgets.append(self.numberElementsLabel)
    self.numberElementsEdit  = wx.TextCtrl( self.listPanel, value="0", size=(30, -1))
    self.numberElementsEdit.Disable()
    self.listPanelSizer.Add( self.numberElementsEdit,  pos=(2,3))
    self.listPanelWidgets.append(self.numberElementsEdit)

    # Select Element Control - PDEF.PolList[n].CurrentElementView
    #     Allows user to select which elements to view
    #     Disabled when NumberOfElements = 0
    #     SBIOS is only valid if PS rules.
    #     MLE and PCONF are valid for both.
    self.selectElementList = [DEFINES.ELEMENT_NAME_NONE]

    self.selectElementLabel = wx.StaticText(self.listPanel, label="\nView Element")
    font = wx.Font( 10, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
    self.selectElementLabel.SetFont( font )
    self.listPanelSizer.Add(self.selectElementLabel, pos=(3,2))
    self.listPanelWidgets.append(self.selectElementLabel)

    self.selectElementEdit = wx.ComboBox( self.listPanel, size=(100, -1), value=DEFINES.ELEMENT_NAME_NONE, choices=self.selectElementList, style=wx.CB_READONLY )
    self.selectElementEdit.Enable( False )
    self.selectElementEdit.SetSelection(0)
    self.listPanelSizer.Add(self.selectElementEdit, pos=(4,2))
    self.selectElementEdit.Bind(wx.EVT_TEXT, self.onSelectElementEdit)
    self.listPanelWidgets.append(self.selectElementEdit)

    # ADD ELEMENT
    #     adds the selected element
    #     Disabled when all possible elements exist
    #     sets corresponding IncludeXXX to TRUE
    #
    self.addElementButton = wx.Button( self.listPanel, -1, label="Add    Element")
    self.listPanelSizer.Add( self.addElementButton, pos=(2,0))
    self.listPanelWidgets.append(self.addElementButton)
    self.addElementButton.Bind(wx.EVT_BUTTON, self.onAddElementClick)

    self.addElementChoicesPs = []
    self.addElementChoicesPs += DEFINES.ELEMENT
    self.addElementChoicesPo = []
    self.addElementChoicesPo += DEFINES.ELEMENT_PO_RULES

    # DELETE ELEMENT combo
    #     Deletes the selected element
    #     Disabled when NumberOfElements = 0
    #     sets corresponding IncludeXXX to FALSE
    #
    self.deleteElementButton = wx.Button( self.listPanel, -1, label="Delete Element")
    self.deleteElementButton.Enable( False )
    self.listPanelSizer.Add( self.deleteElementButton, pos=(3,0))
    self.listPanelWidgets.append(self.deleteElementButton)
    self.deleteElementButton.Bind(wx.EVT_BUTTON, self.onDeleteElementButtonClick)

    # Revocation Count text box - PDEF.PolList[n]ListVersion.RevocationCounter
    #     updated automatically each time the user performs a BUID, but only if the list is signed
    self.revocationCountLabel = wx.StaticText(self.listPanel, label="Revocation\nCount: ")
    self.listPanelSizer.Add( self.revocationCountLabel, pos=(2,6))
    self.listPanelWidgets.append(self.revocationCountLabel)
    self.revocationCountEdit  = wx.TextCtrl( self.listPanel, value="0", size=(30, -1))
    self.revocationCountEdit.Enable(False)                  # disable editing since updated automatically on BUILD's
    self.listPanelSizer.Add( self.revocationCountEdit,  pos=(2,7))
    self.listPanelWidgets.append(self.revocationCountEdit)

    # Allowed text box - PDEF.PolList[n]ListVersion.RevokeCount
    #     permits user to enter a value between 0 and RevocationCounter
    #     Copied to PDEF.RevocationCounters[n] when policy is built
    self.allowedLabel = wx.StaticText(self.listPanel, label="Allowed")
    self.listPanelSizer.Add( self.allowedLabel, pos=(3,6))
    self.listPanelWidgets.append(self.allowedLabel)
    self.allowedEdit  = wx.TextCtrl( self.listPanel, value="0", size=(30, -1))
    self.allowedEdit.Enable(False)                       # disabled since Sync checkbox defaults to True
    self.listPanelSizer.Add( self.allowedEdit,  pos=(3,7))
    self.listPanelWidgets.append(self.allowedEdit)
    self.allowedEdit.Bind(wx.EVT_TEXT, self.onAllowedEdit)

    # Reset button
    #     Resets the Revocation Count to 0
    #
    self.resetButton = wx.Button( self.listPanel, -1, label="Reset", style=wx.BU_EXACTFIT)
    self.resetButton.Enable(False)
    self.listPanelSizer.Add( self.resetButton, pos=(4,6))
    self.listPanelWidgets.append(self.resetButton)
    self.resetButton.Bind(wx.EVT_BUTTON, self.onResetButtonClick)

    # Sync checkbox - PDEF.PolListInfo[n].SyncRevCount
    #      if checked, force Allowed to equal Revocation Count and Allowed box is disabled
    #
    self.Sync = wx.CheckBox(self.listPanel, label="Sync")
    self.Sync.SetValue(True)
    self.Sync.Bind(wx.EVT_CHECKBOX, self.onSync)
    self.listPanelSizer.Add(self.Sync,  pos=(4,7), span=(1,2), flag=wx.BOTTOM, border=5)
    self.listPanelWidgets.append(self.Sync)

    # Private Key File text box - PDEF.PolList[n].PvtKeyFile
    #     allows user to specify the file that contains the signing key
    self.pvtKeyFileSel = wx.ComboBox( self.listPanel, size=(80, -1), value="Private Key", choices=["Private Key", "Signature"], style=wx.CB_READONLY )
    self.pvtKeyFileSel.Bind(wx.EVT_TEXT, self.onPvtKeyFileSel)
    self.listPanelSizer.Add( self.pvtKeyFileSel, pos=(2,11))
    self.listPanelWidgets.append(self.pvtKeyFileSel)

    self.pvtKeyFileEdit  = wx.TextCtrl( self.listPanel, value="", size=(110, -1))
    self.pvtKeyFileEdit.Bind(wx.EVT_TEXT, self.onPvtKeyFileEdit)
    self.listPanelSizer.Add( self.pvtKeyFileEdit,  pos=(2,12))
    self.listPanelWidgets.append(self.pvtKeyFileEdit)

    # BROWSE button
    #     bring up a File Browse dialogue
    #
    self.pvtBrowseButton = wx.Button( self.listPanel, -1, label="Browse", style=wx.BU_EXACTFIT)
    self.listPanelSizer.Add( self.pvtBrowseButton, pos=(2,13))
    self.listPanelWidgets.append(self.pvtBrowseButton)
    self.pvtBrowseButton.Bind(wx.EVT_BUTTON, self.onPvtBrowseButtonClick, self.pvtBrowseButton)

    # Key File text box - PDEF.PolList[n].PubKeyFile
    #     allows user to specify the file that contains the signing key
    self.pubKeyFileLabel = wx.StaticText(self.listPanel, label="Public Key")
    #font = wx.Font( 10, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
    #self.pubKeyFileLabel.SetFont( font )
    self.listPanelSizer.Add( self.pubKeyFileLabel, pos=(3,11))
    self.listPanelWidgets.append(self.pubKeyFileLabel)

    self.pubKeyFileEdit  = wx.TextCtrl( self.listPanel, value="", size=(110, -1))
    self.pubKeyFileEdit.Bind(wx.EVT_TEXT, self.onPubKeyFileEdit)
    self.listPanelSizer.Add( self.pubKeyFileEdit,  pos=(3,12))
    self.listPanelWidgets.append(self.pubKeyFileEdit)

    # BROWSE button
    #     bring up a File Browse dialogue
    #
    self.pubBrowseButton = wx.Button( self.listPanel, -1, label="Browse", style=wx.BU_EXACTFIT)
    self.listPanelSizer.Add( self.pubBrowseButton, pos=(3,13))
    self.listPanelWidgets.append(self.pubBrowseButton)
    self.pubBrowseButton.Bind(wx.EVT_BUTTON, self.onPubBrowseButtonClick)

    # Key Size Control - PDEF.PolList[n].KeySize
    #     Allows user to select one of the supported key sizes: 1024, 2048, 3072
    #
    self.keySizeList = DEFINES.SIGNATURE_KEY_SIZE["RSA PKCS1.5/SHA256"]
    self.keySizeLabel = wx.StaticText(self.listPanel, label="Key Size")
    font = wx.Font( 10, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
    self.keySizeLabel.SetFont( font )
    self.listPanelSizer.Add(self.keySizeLabel, pos=(2,10))
    self.listPanelWidgets.append(self.keySizeLabel)

    self.keySizeEdit = wx.ComboBox( self.listPanel, size=(110, -1), value="2048", choices=self.keySizeList, style=wx.CB_READONLY )
    self.keySizeEdit.Bind(wx.EVT_TEXT, self.onKeySizeEdit)
    self.listPanelSizer.Add(self.keySizeEdit, pos=(3,10))
    self.listPanelWidgets.append(self.keySizeEdit)

    # AlgorithmControl - PDEF.PolList[n].SigAlgorithm
    #     Allows user to select supported algorithms, but only RSA PKCS1.5
    #
    self.algorithmList = DEFINES.SIGNATURE_ALGORITHMS
    self.algorithmLabel = wx.StaticText(self.listPanel, label="Signing\nAlgorithm ")
    font = wx.Font( 10, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)
    self.algorithmLabel.SetFont( font )
    self.listPanelSizer.Add(self.algorithmLabel, pos=(1,11))
    self.listPanelWidgets.append(self.algorithmLabel)

    self.algorithmEdit = wx.ComboBox( self.listPanel, size=(165, -1), value="None", choices=self.algorithmList, style=wx.CB_READONLY )
    self.algorithmEdit.Bind(wx.EVT_TEXT, self.onAlgorithmEdit)
    self.listPanelSizer.Add(self.algorithmEdit, pos=(1,12), span=(1,2))
    self.listPanelWidgets.append(self.algorithmEdit)
    self.enableDisableSigned(False)                     # default to not signed

    #listHorizSizer.Add(listGridSizer,  0, wx.ALL, 5)
    #self.listPanelSizer.Add(listHorizSizer, 0, wx.ALL, 5)
    #parent.Add(listHorizSizer)
    parentSizer.Add(self.listPanel)
    w,h = parentSizer.GetMinSize()
    parent.SetVirtualSize((w,h))
    print("parent sizer type = %s  size = %d, %d" %(type(parentSizer).__name__, w, h))
    #parent.Fit()
    parent.Layout()
    #parent.SetSizerAndFit(self.listPanelSizer)

  def hideListPanel(self):
    """hideListPanel - remove [actually just hide] the list panel and any element panels from the policy panel"""

    for i in self.listPanelWidgets:
        i.Hide()

    self.hideAllPanels()     # hide all the panels

    #TODO: wxPython: hideListPanel - how to resize window back to the original Policy Panel size when delting a list?

  def showListPanel(self):
    """showListPanel - re-show the list and element panels"""

    #print("in showListPanel listPanelWidgets=%s" % (self.listPanelWidgets))   # DBGDBG
    for i in self.listPanelWidgets:
        i.Show()

    for element in self.includedElements:
      element.showPanel()

    parentSizer = self.parent.GetSizer()
    w,h = parentSizer.GetMinSize()
    self.parent.SetVirtualSize((w,h))


  def setListPanelToDefaults(self, wx, parent, listNumber, pdef):
    """setListPanelToDefaults - widgets were all ready created, restore defaults"""
    #self.StatusBar.SetStatusText("Created LIST %i with default values" % (listNumber))
    print("Created LIST %i with default values" % (listNumber))

    self.listLabelNum.ChangeValue(str(listNumber))
    policyversion = str(pdef.PolVersionMajor)+'.'+str(pdef.PolVersionMinor)
    listversion = DEFINES.SUPPORTED_LCP_VERSION[policyversion]
    self.versionEdit.ChangeValue(listversion)
    self.numberElementsEdit.ChangeValue("0")
    self.selectElementEdit.Clear()
    self.selectElementEdit.Append('None')
    self.selectElementEdit.SetSelection(0)
    self.addElementChoicesPs = []
    self.addElementChoicesPs += DEFINES.ELEMENT
    self.addElementChoicesPo = []
    self.addElementChoicesPo += DEFINES.ELEMENT_PO_RULES
    self.revocationCountLabel.Enable(True)
    self.revocationCountEdit.ChangeValue("0")
    self.allowedLabel.Enable(True)
    self.allowedEdit.ChangeValue("0")
    self.resetButton.Enable(True)
    self.Sync.SetValue(True)
    self.Sync.Enable(True)
    self.algorithmEdit.SetValue("None")
    self.pubKeyFileEdit.ChangeValue("")
    self.pubKeyFileEdit.Enable(False)
    self.pvtKeyFileEdit.ChangeValue("")
    self.pvtKeyFileEdit.Enable(False)
    self.pubBrowseButton.Enable(False)
    self.pvtBrowseButton.Enable(False)
    self.keySizeEdit.SetValue("2048")
    self.keySizeEdit.Enable(False)
    self.algorithmEdit.Enable(True)

    for i in self.listPanelWidgets:
        i.Show()

    for elements in self.includedElements:
      elements.hidePanel()

    # delete element GUI panels in self.includedElements[] and elements in pdef.CurrentList.ElementDefData[]
    self.includedElements = []
    currentListObject = pdef.getCurrentListObject()
    currentListObject.ElementDefData = []


  def restoreListPanel(self, currentList):
    """restoreListPanel - restore the list panel's widgets"""
    func = 'restoreListPanel'

    self.hideAllPanels()
    self.listLabelNum.ChangeValue(str(self.pdef.CurrentListView))
    cnt = 0
    self.rebuildSelectElementChoices()
    self.addElementChoicesPs = []
    self.addElementChoicesPs += DEFINES.ELEMENT           # copy elements not just reference
    self.addElementChoicesPo = []
    self.addElementChoicesPo += DEFINES.ELEMENT_PO_RULES  # copy elements not just reference

    cnt = len(currentList.ElementDefData)
    #self.numberElementsEdit.ChangeValue(str(cnt))

    if(cnt > 0):
      flag = True
      self.selectElementEdit.SetValue(currentList.CurrentElementView)
      #self.selectElementEdit.SetSelection(cnt)
    else:
      flag = False
      currentList.CurrentElementView  = DEFINES.ELEMENT_NAME_NONE
      #self.visibleElement = DEFINES.ELEMENT_NAME_NONE
      self.selectElementEdit.SetValue(DEFINES.ELEMENT_NAME_NONE)
      self.selectElementEdit.SetSelection(0)

    # show element panel for selected element
    selection = self.selectElementEdit.GetSelection()
    if selection > 0:
      self.includedElements[selection-1].showPanel()
      self.selectElementEdit.Enable(flag)
      self.deleteElementButton.Enable(flag)

    #print("restoreListPanel - Sync=%s SigAlgorithm=%x" % (currentList.SyncRevCount, currentList.SigAlgorithm))  # DBGDBG
    self.Sync.SetValue(currentList.SyncRevCount)
    self.allowedEdit.ChangeValue(str(currentList.RevokeCounter))
    self.revocationCountEdit.ChangeValue(str(currentList.RevocationCounter))
    self.keySizeEdit.SetValue(str(currentList.KeySize))
    #if (currentList.SigAlgorithm == 1):  # For TPM 1.2

    signAlgName = ""
    try:
      signAlgName = (key for key,val in DEFINES.TPM_ALG_SIGN.items() if currentList.SigAlgorithm == val).next()
    except StopIteration:
      print("WARNING - Invalid signature algorithm (%d)" %(currentList.SigAlgorithm))

    hashAlgName = ""
    try:
      hashAlgName = (key for key,val in DEFINES.TPM_ALG_HASH.items() if currentList.sigAlgorithmHash == val).next()
    except StopIteration:
      print("WARNING - Invalid hash algorithm (%d)" %(currentList.sigAlgorithmHash))

    selectionString = "None"
    if 'RSA' in signAlgName:
      selectionString = (name for name in DEFINES.SIGNATURE_ALGORITHMS if 'RSA' in name and hashAlgName in name).next()
      enable = True
    elif 'ECDSA' in signAlgName:
      selectionString = (name for name in DEFINES.SIGNATURE_ALGORITHMS if signAlgName in name and currentList.KeySize in name).next()
      enable = True
    #elif 'SM2' in signAlgName:
    #  selectionString = (name for name in DEFINES.SIGNATURE_ALGORITHMS if signAlgName in name).next()
    #  enable = True
    else:
      enable = False

    self.enableDisableSigned(enable, selectionString)
    self.algorithmEdit.SetValue(selectionString)
    self.pvtKeyFileSel.SetSelection(currentList.PvtKeyFileIsSignature)
    self.pubKeyFileEdit.ChangeValue(currentList.PubKeyFile)
    self.pvtKeyFileEdit.ChangeValue(currentList.PvtKeyFile)
    #currentList.ListModified = False # don't want to change the state of this variable

  def setListPanelToCurrentListView(self):
    """setListPanelToCurrentListView - update the list panel to the specified list"""

    currentList = self.pdef.getCurrentListObject()
    print("setListPanelToCurrentListView %i AllowedCounter=%i" % (self.pdef.CurrentListView, currentList.RevokeCounter)) # DBGDBG

    # Hide all panels before rebuilding the list for the new panel
    self.hideAllPanels()
    # Clear GUI panels for each element
    self.includedElements = []

    # Re-build the GUI panel for each element from the PDEF persistent object in pdef.py file
    index = 0
    for defdata in currentList.ElementDefData:
      # Get Name and Hash type of each and recreate GUI element with it.

      # Create a new element of selected spec and populate into currentListObject.MleDefData
      elementType, hashAlg = defdata.Name.split('-')

      if hashAlg == 'LEGACY':
        if elementType == 'SBIOS':
          element = SBIOSLegacy()
        elif elementType == 'MLE':
          element = MLELegacy()
        elif elementType == 'PCONF':
          element = PCONFLegacy()
        else:
          print ("ERROR: invalid element")
      else:
        if elementType == 'SBIOS':
          element = SBIOS(DEFINES.TPM_ALG_HASH[hashAlg])      # GUI panel
        elif elementType == 'STM':
          element = STM(DEFINES.TPM_ALG_HASH[hashAlg])
        elif elementType == 'MLE':
          element = MLE(DEFINES.TPM_ALG_HASH[hashAlg])
        elif elementType == 'PCONF':
          element = PCONF(DEFINES.TPM_ALG_HASH[hashAlg])
        else:
          print ("ERROR: invalid element")

      element.createOrShowPanel(wx, self, self.parent, self.pdef, self.StatusBar)
      element.myIndex = index
      self.includedElements.append(element)
      element.restorePanel(currentList, self.pdef.MaxHashes)
      element.hidePanel()
      index += 1

    # restore any element panels that exist in the current PLIST_DEF

    # Also update this list's element panel, if one exists
    # 1st hide all the  elements and enable if they don't exist and rules allow that
    #self.hideAllPanels()    # Hide all the element panels

    # then disable add buttons for elements that exist
    # and count the number of elements in this list
    numberOfElements = len(currentList.ElementDefData)

    # rebuild selectElementEdit's choices list for the elements existing in this view
    self.rebuildSelectElementChoices()

    # then show a panel

    # update the NumberOfElements and enable selectElementsEdit if > 0
    self.numberElementsEdit.ChangeValue(str(numberOfElements))
    if(numberOfElements > 0):
        self.selectElementEdit.Enable( True )
        self.deleteElementButton.Enable( True )

    self.restoreListPanel(currentList)


  # only called if PolicyType = LIST if pdef.NumLists > 0
  # for each signed list,
  #   If Sync is checked,
  #     then update the current list's AllowedCounter from its RevocationCounter
  #   update pdef.DataRevocationCounters[list#] from the current list's Allowedounter
  #   if the current List is displayed in the list panel, update the list panel widgets
  #
  # NOTE: the current list's RevocationCounter is incr on its 1st modification
  #
  def onBuildButtonClick(self, pdef):
    """ onBuildButtonClick - perform a build"""
    #print("list::onBuildButtonClick")   # DBGDBG

    listNum = 0
    while( listNum < pdef.NumLists):
      thisList = pdef.PolListInfo[str(listNum)]
      #if(thisList.SigAlgorithm == DEFINES.LCP_POLSALG_RSA_PKCS_15):  # For TPM1.2
      if(thisList.SigAlgorithm == DEFINES.TPM_ALG_SIGN['RSASSA']):
        if(thisList.SyncRevCount == True):
          thisList.RevokeCounter = thisList.RevocationCounter

        pdef.DataRevocationCounters[listNum] = thisList.RevokeCounter

        # is this list being shown in the list panel?
        if(pdef.CurrentListView == listNum + 1):
          self.allowedEdit.ChangeValue(str(thisList.RevokeCounter))
          self.revocationCountEdit.ChangeValue(str(thisList.RevocationCounter))

        if(thisList.RevocationCounter > 0):       # can't reset til cnt > 0
            self.resetButton.Enable(True)

        print("list.onBuild - list %d, RevCnt=%d, RevokeCnt=%d, pdef.DataRevocationCounters[%d]=%d" %
          (listNum+1, thisList.RevocationCounter, thisList.RevokeCounter, listNum+1, pdef.DataRevocationCounters[listNum]))    # DBGDBG

      listNum += 1

    #print("list.onBuild - pdef.DataRevocationCounters=%s" % (pdef.DataRevocationCounters)) # DBGDBG

  def onAllowedEdit(self, event):
    """ onAllowedEdit - handle Allowed value change"""

    string = event.GetString()
    self.StatusBar.SetStatusText("in onAllowedEdit, value=%s" % (string))

    currentList = self.pdef.getCurrentListObject()
    maxValue = currentList.RevocationCounter
    #print("onAllowedEdit - MaxValue=%i" % (maxValue))  # DBGDBG

    try:
      value = int(string)
    except:
      self.StatusBar.SetStatusText(  "%s is invalid, Please enter only digits between 0 and %i" % (string, maxValue))
    else:
      #print("onAllowedEdit - Value=%i" % (value))  # DBGDBG
      if(value > maxValue):
        self.allowedEdit.ChangeValue(str(currentList.RevokeCounter))
        self.StatusBar.SetStatusText( "%i is too large, the max value for Allowed is %i" % ( value, maxValue ))
      else:
        currentList.RevokeCounter = value
        print("onAllowedEdit - setting RevokeCounter=%i MaxValue=%i" % (value, maxValue))
        self.setListModified()

  def onSelectElementEdit(self, event):
    """onSelectElementEdit - view the selected element"""
    func = 'onSelectElementEdit'

    #selection = self.selectElementEdit.GetSelection()
    selection = event.GetEventObject().GetSelection()
    selectionString = event.GetString()
    #self.setListModified()   # just looking at different element shouldn't be considered modifying it.
    #Element = DEFINES.ELEMENT
    #Object  = self.getObject()
    currentList = self.pdef.getCurrentListObject()
    #IncludeElement = utilities.getIncludeElement(currentList)

    # Using exhaustive find method to match selected element by it's element name and hash alg
    #for element in self.includedElements:
    #  found = element.isElementType(selectionString)
    #  if found:
    #    self.StatusBar.SetStatusText( "Displaying the %s element" % (selectionString))
    #    self.hideAllPanels()
    #    element.showPanel()
    #    self.visibleElement = selectionString
    #    currentList.CurrentElementView = selectionString
    #    break

    if(selectionString == DEFINES.ELEMENT_NAME_NONE):
      self.StatusBar.SetStatusText( "No element selected")
      self.visibleElement = selectionString
      currentList.CurrentElementView = selectionString
      self.hideAllPanels()
    else:
      self.StatusBar.SetStatusText( "Displaying the %s element" % (selectionString))
      self.hideAllPanels()
      self.includedElements[selection-1].showPanel()
      self.visibleElement = selectionString
      currentList.CurrentElementView = selectionString


    #TODO: wxPython: onSelectElementEdit - how to resize panel here?  Or set min size after each panel is created?
    print("%s: selection=%d, visibleElement=%s" % (func, self.selectElementEdit.GetSelection(), self.visibleElement)) # DBGDBG)

  def onDeleteElementButtonClick(self, event):
    """ onDeleteElementButtonClick - delete the current element"""
    func = 'onDeleteElementButtonClick'
    self.StatusBar.SetStatusText( "Deleted the current element")

    #exit if None is selected, else confirm the deletion and continue
    if(self.visibleElement == DEFINES.ELEMENT_NAME_NONE):
      self.StatusBar.SetStatusText( "No element selected, Please select the element to be deleted")
      return

    # confirm delete
    dlg = wx.MessageDialog(None, "Deleted Elements cannot be recovered. Continue?", 'Confirm Element Deletion', wx.YES_NO | wx.ICON_QUESTION)
    response = dlg.ShowModal()
    dlg.Destroy()

    if(response == wx.ID_NO):
      self.StatusBar.SetStatusText( "Element Deletion cancelled" )
      return
    else:
      self.StatusBar.SetStatusText( "Element deleted")

    # ComboBox selection value is one index higher than self.includedElement[] index and PLIST_DEF.ElementDefData[] index
    # because of the None element to select no view.
    selection = self.selectElementEdit.GetSelection()             # get the index
    selectionString = self.selectElementEdit.GetValue()           # get selected element and populate into Add Element menu
    self.selectElementEdit.Delete(selection)
    print("%s - Selection=%d VisibleElement=%s - Removed" % (func, selection, self.visibleElement)) # DBGDBG

    currentElementsCnt = int(self.numberElementsEdit.GetValue())
    if(selection > 0):
      nextselection = selection - 1
      # fixup selection for out of order case where selection 1 was deleted, but are > 1 elements left
      # ie should only select 0 if 0 elements left  ie None is the only choice left
      # Scenario:  create > 1 elements, select 1, delete 1. 1 still selected
      if((nextselection == 0) and (currentElementsCnt > 1)):
        nextselection = 1
      self.selectElementEdit.SetSelection(nextselection)
    else:
      print("%s: selectElementEdit.GetSelection() is invalid, Aborting!! % (func)")
      return

    # set IncludeXXX to False
    # hide the current element, show another element if one exists
    self.setListModified()
    #self.dumpIncludeXXX( currentListObject, 'before delete' )      # show current value of all the  XXXDefData[i].IncludeXXXX  # DBGDBG

    # Add deleted element back to Add Element menu.
    self.updateAddElementChoicesAfterDelete(selectionString)

    # update myIndex of the elements behind this element
    for count in range(selection, len(self.includedElements)):
      self.includedElements[count].myIndex -= 1

    # remove item from pdef list
    currentListObject = self.pdef.getCurrentListObject()
    currentListObject.ElementDefData.pop(selection-1)

    # update GUI to show other element
    if selection > 0:
      self.includedElements[selection-1].hidePanel()
      # remove this element's GUI
      self.includedElements.pop(selection-1)

    # nextselection is indexing into the updated self.includedElements[]
    if nextselection != 0:
      self.includedElements[nextselection-1].showPanel()

    # Set other GUI fields
    currentElementsCnt -= 1
    self.numberElementsEdit.ChangeValue(str(currentElementsCnt))
    if(currentElementsCnt > 0):
      self.selectElementEdit.Enable( True )
      self.addElementButton.Enable(True)
      self.visibleElement = self.selectElementEdit.GetValue()
      currentListObject.CurrentElementView = self.selectElementEdit.GetValue()
      print("%s - Selection=%d VisibleElement=%s" % (func, selection, self.visibleElement)) # DBGDBG
    elif(currentElementsCnt == 0):
      self.selectElementEdit.Enable( False )
      self.deleteElementButton.Enable( False )
      self.visibleElement = DEFINES.ELEMENT_NAME_NONE
      currentListObject.CurrentElementView = DEFINES.ELEMENT_NAME_NONE

    #self.dumpIncludeXXX( currentListObject, 'after delete' )      # show current value of all the  XXXDefData[i].IncludeXXXX  # DBGDBG

  # Object - array of all the mle, sbios, pconf & stm 1 and 256 objects
  # Element - array of all the ELEMENT_NAME_XXXX_SHAYYY strings
  # IncludeElement - array of all the XXXXDefData[DEFINES.DEFDATA_INDEX_SHANNN.IncludeXXXX
  #         updated to indicate the just deleted element's value is now False
  #
  def showNextElement(self, Object, Element, IncludeElement, currentListObject):
    """showNextElement - if another element's panel exists, show it"""
    func = 'showNextElement'

    i=0
    while(i < len(Element)):
      #print("%s - checking %s" % (func, Element[i]))  # DBGDBG
      if(IncludeElement[i] == True):
        print("%s - showing %s" % (func, Element[i]))  # DBGDBG
        Object[i].showPanel()
        self.visibleElement = Element[i]
        currentListObject.CurrentElementView = Element[i]
        self.selectElementEdit.SetValue(Element[i])
        break

      i += 1      # next element


  def onResetButtonClick(self, event):
    """ onResetButtonClick - reset the revocation count"""
    self.StatusBar.SetStatusText( "You clicked the Reset Button!")

    if(self.revocationCountEdit.GetValue() != "0"):
      dlg = wx.MessageDialog(None, "Revocation Count reset cannot be undone. Continue?", 'Confirm Revocation Count reset', wx.YES_NO | wx.ICON_QUESTION)
      response = dlg.ShowModal()
      dlg.Destroy()

      if(response == wx.ID_NO):
        self.StatusBar.SetStatusText( "Revocation Count reset cancelled" )
      else:
        self.StatusBar.SetStatusText( "Reset the Revocation Count " )
        self.revocationCountEdit.ChangeValue("0")
        currentList = self.pdef.getCurrentListObject()
        currentList.RevocationCounter = 0
        #self.setListModified()         # don't set list modified here as that re-increments the revocation ctr
        self.resetButton.Enable(False)

  def onSync(self, event):
    """ onSync - force Allowed to equal Revocation Count and disable Allowed"""

    self.StatusBar.SetStatusText("You checked Sync")
    currentList = self.pdef.getCurrentListObject()
    if(event.IsChecked()):
      self.allowedEdit.ChangeValue(self.revocationCountEdit.GetValue())
      currentList.SyncRevCount = True
      currentList.RevokeCounter = currentList.RevocationCounter
      self.setListModified()
      print("onSync - PolList[%i].RevCnt=%i, Allowed=%i" %
        (self.pdef.CurrentListView, currentList.RevokeCounter, currentList.RevocationCounter))    # DBGDBG
    else:
      currentList.SyncRevCount = False

    # when checked disable editing the allowed box
    self.allowedEdit.Enable(not event.IsChecked())

  def enableDisableSigned(self, value, signAlgName='None'):
    """enableDiableSigned - perform common actions when Signed or Algorithm changed"""

    # Note: Sync checkbox and Algorithm pulldown are redundant
    #   Sync unchecked  == LCP_POLSALG_NONE
    #   Sync checked    == LCP_POLSALG_RSA_PKCS_15

    #If Unsigned [ie algorithm=None], then revocation count, allowed, RESET, Sync, Key File & Key Size are all disabled,
    # else enabled

    currentList = self.pdef.getCurrentListObject()
    keysizeList = DEFINES.SIGNATURE_KEY_SIZE[signAlgName]

    if(value == False):
      #value = False
      self.allowedEdit.Enable(value)
      #currentList.SigAlgorithm = DEFINES.TPM_ALG_SIGN['NULL']       # 0=Not signed
      #self.algorithmEdit.SetValue(DEFINES.ELEMENT_NAME_NONE)
    else:
      #value = True
      self.allowedEdit.Enable(not value)

    self.setListModified()
    self.resetButton.Enable(value)
    self.Sync.Enable(value)
    self.pubKeyFileEdit.Enable(value)
    self.pvtKeyFileEdit.Enable(value)
    self.pubBrowseButton.Enable(value)
    self.pvtBrowseButton.Enable(value)
    self.keySizeEdit.SetItems(keysizeList)
    self.keySizeEdit.SetValue(str(currentList.KeySize))
    self.keySizeEdit.Enable(value)
    self.revocationCountLabel.Enable(value)
    self.allowedLabel.Enable(value)
    self.pvtKeyFileSel.Enable(value)
    self.pubKeyFileLabel.Enable(value)
    self.keySizeLabel.Enable(value)

  def onPubKeyFileEdit(self, event):
    """ onpubKeyFileEdit - update the key file"""

    currentList = self.pdef.getCurrentListObject()
    currentList.PubKeyFile = event.GetString()
    self.setListModified()

    # once the entire file name is entered, verify it, else clear it
    if(currentList.PubKeyFile.endswith(".pem")):
      if currentList.SigAlgorithm == DEFINES.TPM_ALG_SIGN['RSASSA']:
        type = DEFINES.KEY_FILE_TYPE['PUBLIC_RSASSA']
      elif currentList.SigAlgorithm == DEFINES.TPM_ALG_SIGN['ECDSA']:
        type = DEFINES.KEY_FILE_TYPE['PUBLIC_ECDSA']

      if(utilities.verifyKeyFile(currentList.PubKeyFile, type, currentList) == False):
        self.pubKeyFileEdit.ChangeValue("")

  def onPvtKeyFileSel(self, event):
    """ onPvtKeyFileEdit - update the key file"""
    currentList = self.pdef.getCurrentListObject()
    selectionString = event.GetString()
    if selectionString == "Private Key":
      currentList.PvtKeyFileIsSignature = False
    else:
      currentList.PvtKeyFileIsSignature = True


  def onPvtKeyFileEdit(self, event):
    """ onPvtKeyFileEdit - update the key file"""

    currentList = self.pdef.getCurrentListObject()
    currentList.PvtKeyFile = event.GetString()
    self.setListModified()

    # once the entire file name is entered, verify it, else clear it
    if((currentList.PvtKeyFile.endswith(".pem")) or (currentList.PvtKeyFile.endswith(".key"))):
      if(utilities.verifyKeyFile(currentList.PvtKeyFile, KEY_FILE_TYPE_PRIVATE, currentList) == False):
        self.pvtKeyFileEdit.ChangeValue("")

  def onKeySizeEdit(self, event):
    """ onKeySizeEdit - update the key size"""

    currentList = self.pdef.getCurrentListObject()
    currentList.KeySize = event.GetString()
    self.setListModified()
    self.StatusBar.SetStatusText("")   # clear any previous error messages

    #if key file was all rdy specified, verify its size is correct
    file = self.pubKeyFileEdit.GetValue()
    if(file != ""):
      if(utilities.verifyKeyFile(file, KEY_FILE_TYPE_PUBLIC, currentList) == False):
        self.pubKeyFileEdit.ChangeValue("")
        currentList.PubKeyFile = ""

    file = self.pvtKeyFileEdit.GetValue()
    if(file != ""):
      if(utilities.verifyKeyFile(file, KEY_FILE_TYPE_PRIVATE, currentList) == False):
        self.pvtKeyFileEdit.ChangeValue("")
        currentList.PvtKeyFile = ""

  def onPvtBrowseButtonClick(self, event):
    """ onPvtBrowseButtonClick - browse to an existing PDEF file"""

    #self.StatusBar.SetStatusText("You clicked the Browse button")
    #TODO: Bill: handle .pems that have both the public and private keys?
    #TODO: Bill: handle .pems with password protected private keys?

    currentList = self.pdef.getCurrentListObject()
    #accept .key as well as pem files for private keys?
    dirname = ''   #  current working directory
    workdir = self.pdef.WorkingDirectory

    if currentList.PvtKeyFileIsSignature:
      title = "Choose the Signature file"
      wildcard = "All Files (*.*)|*.*|" \
                 "Key file (*.sig)|*.sig|"
    else:
      title = "Choose the Private Key file"
      wildcard = "Key file (*.pem)|*.pem|" \
                 "Key file (*.key)|*.key|" \
                 "All Files (*.*)|*.*"
    dlg = wx.FileDialog(self.parent, title, workdir, "", wildcard, wx.FD_OPEN)

    if dlg.ShowModal() == wx.ID_OK:
      filename = dlg.GetFilename()
      dirname  = dlg.GetDirectory()

      self.pvtKeyFileEdit.ChangeValue(filename)
      currentList.PvtKeyFile = filename
      self.setListModified()

      if currentList.SigAlgorithm == DEFINES.TPM_ALG_SIGN['RSASSA']:
        filetype = DEFINES.KEY_FILE_TYPE['PRIVATE_RSASSA']
      else:
        filetype = DEFINES.KEY_FILE_TYPE['PRIVATE_ECDSA']

      if currentList.PvtKeyFileIsSignature:
        #copy signature file to the current working directory
        pass
      else:
      # Verify Private Key file
        if(utilities.verifyKeyFile(os.path.join(dirname, currentList.PvtKeyFile), filetype, currentList) == False):
          self.pvtKeyFileEdit.ChangeValue("")
          currentList.PvtKeyFile = ""
        elif (dirname != workdir):
          if (os.path.exists(os.path.join(workdir, filename))) :
            confdlg = wx.MessageDialog(self.parent, filename+" already exists in working directory\nOverwrite file in working directory?", "Confirm Copy", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
            abortFlag = False
            if (confdlg.ShowModal() == wx.ID_OK):
              shutil.copyfile(os.path.join(dirname, filename), os.path.join(workdir, filename))
            else:
              abortFlag = True

            confdlg.Destroy()
            if(abortFlag == True):
              if(utilities.verifyKeyFile(os.path.join(workdir, currentList.PvtKeyFile), filetype, currentList) == False):
                # don't change
                self.pvtKeyFileEdit.ChangeValue("")
                currentList.PvtKeyFile = ""
              self.StatusBar.SetStatusText( "Copy cancelled, using exiting key" )
          else:
            shutil.copyfile(os.path.join(dirname, filename), os.path.join(workdir, filename))

    dlg.Destroy()
    #print("onPvtBrowseButtonClick: %s" % (currentList.PvtKeyFile))   # DBGDBG

  def onPubBrowseButtonClick(self, event):
    """ onPubBrowseButtonClick - browse to an existing PDEF file"""

    #self.StatusBar.SetStatusText("You clicked the Browse button")
    dirname = ''   #  current working directory
    workdir = self.pdef.WorkingDirectory
    wildcard = "Key file (*.pem)|*.pem|" \
               "All Files (*.*)| *.*"
    dlg = wx.FileDialog(self.parent, "Choose the Public Key file", workdir, "", wildcard, wx.FD_OPEN)

    currentList = self.pdef.getCurrentListObject()
    if currentList.SigAlgorithm == DEFINES.TPM_ALG_SIGN['RSASSA']:
      filetype = DEFINES.KEY_FILE_TYPE['PUBLIC_RSASSA']
    else:
      # treat SM2 as ECDSA key
      filetype = DEFINES.KEY_FILE_TYPE['PUBLIC_ECDSA']

    if dlg.ShowModal() == wx.ID_OK:
      filename = dlg.GetFilename()
      dirname  = dlg.GetDirectory()

      self.pubKeyFileEdit.ChangeValue(filename)
      currentList.PubKeyFile = filename
      self.setListModified()
      if(utilities.verifyKeyFile(os.path.join(dirname, currentList.PubKeyFile), filetype, currentList) == False):
        self.pubKeyFileEdit.ChangeValue("")
        currentList.PubKeyFile = ""
      elif (dirname != workdir):
        if (os.path.exists(os.path.join(workdir, filename))) :
          confdlg = wx.MessageDialog(self.parent, filename+" already exists in working directory\nOverwrite file in working directory?", "Confirm Copy", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
          abortFlag = False
          if (confdlg.ShowModal() == wx.ID_OK):
            shutil.copyfile(os.path.join(dirname, filename), os.path.join(workdir, filename))
          else:
            abortFlag = True

          confdlg.Destroy()
          if(abortFlag == True):
            # verify the existing file
            if(utilities.verifyKeyFile(os.path.join(workdir, currentList.PubKeyFile), filetype, currentList) == False):
              self.pubKeyFileEdit.ChangeValue("")
              currentList.PubKeyFile = ""
            self.StatusBar.SetStatusText( "Copy cancelled, using exiting key" )
        else:
          shutil.copyfile(os.path.join(dirname, filename), os.path.join(workdir, filename))

    dlg.Destroy()
    #print("onPubBrowseButtonClick: %s" % (currentList.PubKeyFile))   # DBGDBG

  def onAlgorithmEdit(self, event):
    """ onAlgorithmEdit - select the algorithm"""

    func = "onAlgorithmEdit"
    currentList = self.pdef.getCurrentListObject()
    selectionString = event.GetString()
    algName = selectionString.split('/')
    if len(algName) == 2:
      sigAlg = algName[0].split(' ')[0]
      hashAlg = algName[1]

      # Check for valid signature algorithm name
      try:
        sigAlgName = (key for key in DEFINES.TPM_ALG_SIGN.keys() if (sigAlg in key)).next()
      except StopIteration:
        self.StatusBar.SetStatusText("Signature algorithm name mismatch")
        print("%s - Signature algorithm name mismatch" % (func))  # DBGDBG
        return

      # Check for valid algorithm name
      try:
        hashAlgName = (key for key in DEFINES.TPM_ALG_HASH.keys() if (hashAlg in key)).next()
      except StopIteration:
        self.StatusBar.SetStatusText("Hash algotithm name mismatch")
        print("%s - Hash algorithm name mismatch" % (func))  # DBGDBG
        return

      currentList.SigAlgorithm = DEFINES.TPM_ALG_SIGN[sigAlgName]
      currentList.sigAlgorithmHash = DEFINES.TPM_ALG_HASH[hashAlgName]
      if currentList.KeySize not in DEFINES.SIGNATURE_KEY_SIZE[selectionString]:
        currentList.KeySize = DEFINES.SIGNATURE_KEY_SIZE[selectionString][0]
      self.enableDisableSigned(True, selectionString)
    else:
      currentList.SigAlgorithm = DEFINES.TPM_ALG_SIGN['NULL']
      self.enableDisableSigned(False)


  def onAddElementClick(self, event):
    """onAddElementClick"""
    func = "onAddElementClick"

    # ask user to select the type of element and its hash algorithm
    if(self.pdef.Rules == DEFINES.PsRules):
      addElementChoices = self.addElementChoicesPs
    elif(self.pdef.Rules == DEFINES.PoRules):
      addElementChoices = self.addElementChoicesPo
    else:
      print("%s - invalid pdef.Rules=%d" % (func, self.pdef.Rules))
      return

    message = "Select the type of element and its hash algorithm"
    title   = "Add Element Selections"
    dialog = wx.SingleChoiceDialog(None, message, title, addElementChoices)
    if dialog.ShowModal() == wx.ID_OK:
      requestedElement = dialog.GetStringSelection()
    else:
      dialog.Destroy()
      return          # cancel out

    dialog.Destroy()
    self.StatusBar.SetStatusText("Adding a %s element to this LIST" % (requestedElement))

    print("%s: adding %s" % (func, requestedElement))  # DBGDBG
    self.deleteElementButton.Enable( True )
    currentListObject = self.pdef.getCurrentListObject()
    self.selectElementEdit.Enable( True )
    currentElementsCnt = int(self.numberElementsEdit.GetValue())
    currentElementsCnt += 1
    self.numberElementsEdit.ChangeValue(str(currentElementsCnt))

    # if an element is being shown, hide it.  Then show the new one
    if(currentListObject.CurrentElementView != DEFINES.ELEMENT_NAME_NONE):
      self.hideThisPanel(currentListObject.CurrentElementView)

    currentListObject.CurrentElementView = requestedElement
    self.selectElementEdit.Append(requestedElement)
    currentSelection = self.selectElementEdit.GetSelection()
    #print("%s: currentSelection=%d newSelection=%d" % (func, currentSelection, currentSelection+1)) # DBGDBG
    self.selectElementEdit.SetSelection(currentSelection + 1)
    self.selectElementEdit.SetValue(requestedElement)
    self.updateAddElementChoicesAfterAdd(requestedElement)
    self.visibleElement = requestedElement

    # Create a new element of selected spec and populate into currentListObject.MleDefData
    elementType, hashAlg = requestedElement.split('-')

    if hashAlg == 'LEGACY':
      if elementType == 'SBIOS':
        element = SBIOSLegacy()
        defdata = SBIOSLEGACY_DEF()
      elif elementType == 'MLE':
        element = MLELegacy()
        defdata = MLELEGACY_DEF()
      elif elementType == 'PCONF':
        element = PCONFLegacy()
        defdata = PCONFLEGACY_DEF()
      else:
        print ("ERROR: invalid element")
    else:
      if elementType == 'SBIOS':
        element = SBIOS(DEFINES.TPM_ALG_HASH[hashAlg])      # GUI panel
        defdata = SBIOS_DEF(DEFINES.TPM_ALG_HASH[hashAlg])  # Element defined in pdef.py
      elif elementType == 'STM':
        element = STM(DEFINES.TPM_ALG_HASH[hashAlg])
        defdata = STM_DEF(DEFINES.TPM_ALG_HASH[hashAlg])
      elif elementType == 'MLE':
        element = MLE(DEFINES.TPM_ALG_HASH[hashAlg])
        defdata = MLE_DEF(DEFINES.TPM_ALG_HASH[hashAlg])
      elif elementType == 'PCONF':
        element = PCONF(DEFINES.TPM_ALG_HASH[hashAlg])
        defdata = PCONF_DEF(DEFINES.TPM_ALG_HASH[hashAlg])
      else:
        print ("ERROR: invalid element")

    currentListObject.ElementDefData.append(defdata)
    element.createOrShowPanel(wx, self, self.parent, self.pdef, self.StatusBar)
    self.includedElements.append(element)

    # Note - panels were constructed with the correct hashAlg, so no need to pass that thru here
    # Note - iterators not used here since need to update the pdef's  xxxDefData.IncludeXXX not the array's

    self.selectElementEdit.Enable(True)
    self.parent.Layout()


  def syncVersion(self, rule):
    currentList = self.pdef.getCurrentListObject()
    listversion = str(currentList.ListVersionMajor)+'.'+str(currentList.ListVersionMinor)
    self.versionEdit.ChangeValue(listversion)
    if rule == DEFINES.PoRules:
      enable = True
    else:
      enable = False
    for element in self.includedElements:
      element.restorePanel(currentList, self.pdef.MaxHashes)
      element.enableDisableOverridePsPolicy(enable)


  def checkListModified(self, pdef):
    """checkListModified - return True if any list has been modified, else False"""

    # pdef.PolListInfo is a dictionary of 8 PLIST_DEF's from '0' to '7'
    # or None if that list was not added
    # i.e. {'0':PLIST_DEF or None, ... '7':PLIST_DEF or None}
    #
    i = '0'
    while(i<'8'):
      #print("checkListModified list %s" % (i))  # DBGDBG
      if(pdef.PolListInfo[i] != None):              # if list exists
        if(pdef.PolListInfo[i].ListModified == True):
          print("checkListModified Return - list %s was modified" % (i))  # DBGDBG
          return True

      i = str(int(i) + 1)

    return False


  def setListModified(self):
    """setListModified - if list not modified yet, increment its rev cnt and set it to modified"""

    currentList = self.pdef.getCurrentListObject()
    #also set pdef.Modified for saving file\
    self.pdef.Modified =  True
    #print("setListModified - ListModified=%s" % (currentList.ListModified))  # DBGDBG
    if(currentList.ListModified == False):
      currentList.RevocationCounter += 1
      self.resetButton.Enable(True)

      self.revocationCountEdit.ChangeValue(str(currentList.RevocationCounter))   # update the GUI
      currentList.ListModified = True

  def rebuildSelectElementChoices(self):
    """rebuildSelectElementChoices - rebuild selectElementEdit's choices list for the elements existing in this view"""
    func = 'rebuildSelectElementChoices'

    self.selectElementEdit.Clear()
    self.selectElementEdit.Append('None')
    # For each existing element in this list (ie IncludeXXXX==True)
    currentList = self.pdef.getCurrentListObject()
    for element in currentList.ElementDefData:
      self.selectElementEdit.Append(element.Name)


  # remove the added element from the add element selection list
  def updateAddElementChoicesAfterAdd(self, requestedElement):
    """updateAddElementChoicesAfterAdd"""
    func = 'updateAddElementChoicesAfterAdd'

    if(self.pdef.Rules == DEFINES.PoRules):
      self.addElementChoicesPo.remove(requestedElement)
      if(len(self.addElementChoicesPo) == 0):
        self.addElementButton.Enable(False)
    if(self.pdef.Rules == DEFINES.PsRules):
      self.addElementChoicesPs.remove(requestedElement)
      if(len(self.addElementChoicesPs) == 0):
        self.addElementButton.Enable(False)

  # append the deleted element to the add element selection list
  def updateAddElementChoicesAfterDelete(self, deletedElement):
    """updateAddElementChoicesAfterDelete"""
    func='updateAddElementChoicesAfterDelete'

    # append the deleted element to the add element selection list
    self.addElementChoicesPs.append(deletedElement)
    self.addElementChoicesPo.append(deletedElement)


  # rebuild the add element selection list to include all the elements that don't exist
  def rebuildAddElementChoices(self):
    """rebuildAddElementChoices"""
    self.rebuildAddElementChoicesForPoRules()
    self.rebuildAddElementChoicesForPsRules()

  # if a PO rules element doesn't exist, add it to the selections list
  def rebuildAddElementChoicesForPoRules(self):
    """rebuildAddElementChoicesForPoRules"""
    func='rebuildAddElementChoicesForPoRules'

    #Element = DEFINES.ELEMENT_PO_RULES
    self.addElementChoicesPo = []
    self.addElementChoicesPo += DEFINES.ELEMENT_PO_RULES
    currentList = self.pdef.getCurrentListObject()

    for element in currentList.ElementDefData:
      if element.Name in self.addElementChoicesPo:
        self.addElementChoicesPo.remove(element.Name)
      else:
        print ("Element Name %s not found in Elements for PO rule" %(element.Name))

    if len(currentList.ElementDefData) >= 8 or len(self.addElementChoicesPs) == 0:
      flag = False
    else:
      flag = True
    self.addElementButton.Enable(flag)
    #print("%s - addElementChoicesPo=%s" % (func, self.addElementChoicesPo)) #DBGDBG

  # if a PS rules element doesn't exist, add it to the selections list
  def rebuildAddElementChoicesForPsRules(self):
    """rebuildAddElementChoicesForPsRules"""
    func='rebuildAddElementChoicesForPsRules'

    #Element = DEFINES.ELEMENT                    # gets all the elements i.e. = PS rules
    self.addElementChoicesPs = []
    self.addElementChoicesPs += DEFINES.ELEMENT   # gets all the elements i.e. = PS rules
    currentList = self.pdef.getCurrentListObject()

    for element in currentList.ElementDefData:
      if element.Name in self.addElementChoicesPs:
        self.addElementChoicesPs.remove(element.Name)
      else:
        print ("Element Name %s not found in Elements for PS rule" %(element.Name))

    if len(currentList.ElementDefData) >= 8 or len(self.addElementChoicesPs) == 0:
      flag = False
    else:
      flag = True
    self.addElementButton.Enable(flag)
    #print("%s - addElementChoicesPs=%s" % (func, self.addElementChoicesPs)) #DBGDBG

  def loadAndDisplayPlistDefFile(self, file, wx, pdef, statusBar, parent):
    """ loadAndDisplayPlistDefFile - get a saved plistDef object from a PDEF file and update the list panel"""
    func = 'loadAndDisplayPlistDefFile'

    self.pdef = pdef
    self.StatusBar = statusBar
    self.parent = parent

    print("%s load plistDef %d, NumLists=%d" % (func, pdef.CurrentListView, pdef.NumLists))     # DBGDBG
    currentListObject = self.pdef.getCurrentListObject()


    # If this list is to be the displayed list panel[ie pdef.CurrentListView]
    # then restore the list panel & enable/disable the AddElement buttons
    # restore the list's element panels that exist but hide them
    # only show & restore the specified element [ie list.CurrentElementView]
    #
    print("%s thisListNum=%d, CurrentListView=%d" % (func, pdef.CurrentListView, pdef.CurrentListView))     # DBGDBG
    if(self.listPanelWidgets == []):
      self.createListPanel(wx, parent, pdef.CurrentListView, pdef, statusBar)
    else:
      # in case list or element panels all ready exist ...
      self.hideListPanel()
      for i in self.listPanelWidgets:
        i.Show()

    self.setListPanelToCurrentListView()
    #currentListObject.ListModified = False

  def printPlistDefs(self, pdef, f):
    """printPlistDefs - write all created PLIST_DEFs to the specified human readable text file for printing"""

    # pdef.PolListInfo is a dictionary of 8 PLIST_DEF's from '0' to '7'
    # or None if that list was not added
    # i.e. {'0':PLIST_DEF or None, ... '7':PLIST_DEF or None}
    #
    i = '0'             # i is 0 based index for loop control
    cnt = '1'         # cnt is 1 based 'i' for printing
    while(i<'8'):
      print("PlistInfo", cnt, " = ", pdef.PolListInfo[i], file=f)
      if(pdef.PolListInfo[i] != None):
        self.printPlistDef(pdef.PolListInfo[i], f)     # write this PolListInfo[]

      i = str(int(i) + 1)
      cnt = str(int(cnt) + 1)

  def printPlistDef(self, plistDef, f):
    """printPlistDef - write this PLIST_DEF to the specified human readable text file for printing"""

    #print("printPlistDef - PLIST_DEF object: %s" % (plistDef))     # DBGDBG

    print("LdefSize",             " = ", plistDef.LdefSize,            file=f)
    print("Tag",                  " = ", plistDef.Tag,                 file=f)
    print("ListVersion",          " = ", plistDef.ListVersionMajor, ".", plistDef.ListVersionMinor, sep='', file=f)
    print("ListValid",            " = ", plistDef.ListValid,           file=f)
    print("ListModified",         " = ", plistDef.ListModified,        file=f)
    print("SigAlgorithm",         " = ", hex(plistDef.SigAlgorithm),        file=f)
    print("sigAlgorithmHash",     " = ", hex(plistDef.sigAlgorithmHash),    file=f)
    print("PolicyElementSize",    " = ", plistDef.PolicyElementSize,   file=f)
    print("CurrentElementView",   " = ", plistDef.CurrentElementView,  file=f)
    print("SyncRevCount",         " = ", plistDef.SyncRevCount,        file=f)
    print("AllowedCounter",       " = ", plistDef.RevokeCounter,       file=f)
    print("RevocationCounter",    " = ", plistDef.RevocationCounter,   file=f)
    print("KeySize",              " = ", plistDef.KeySize,             file=f)
    print("PubKeyFile",           " = ", plistDef.PubKeyFile,          file=f)
    print("PvtKeyFile",           " = ", plistDef.PvtKeyFile,          file=f)

    currentListObject = plistDef

    # print the element summary showing which elements were created
    # Lists always contain all elements, even if they haven't been added
    # so need to check if they should be included
    for element in plistDef.ElementDefData:
      print("%s DefData[Index]"% (element.Name),  " = ", element,  file=f)
      element.printDef(f)
      print("\n", file=f)         # for readability


#  def enablePconfOverridePsPolicyCheckbox(self, value):
#    """enablePconfOverridePsPolicyCheckbox - if current list has a PCONF element, enable its OverridePsPolicy checkbox"""
#
#    currentList = self.pdef.getCurrentListObject()
#    if(currentList.PconfDefData[DEFINES.DEFDATA_INDEX['SHA256']].IncludeInList == True):
#      pconf256.enablePconfOverridePsPolicyCheckbox(value)
#    if(currentList.PconfDefData[DEFINES.DEFDATA_INDEX['SHA1']].IncludeInList == True):
#      pconf1.enablePconfOverridePsPolicyCheckbox(value)
#    if(currentList.PconfLegacyDefData[DEFINES.DEFDATA_INDEX['SHA1']].IncludeInList == True):
#      pconf0.enablePconfOverridePsPolicyCheckbox(value)


  def hideAllPanels( self ):
    """hideAllPanels - hide all the element panels"""

    for element in self.includedElements:
      element.hidePanel()


  def hideThisPanel( self, panelToHide ):
    """hideThisPanel"""

    for element in self.includedElements:
      found = element.isElementType(panelToHide)
      if found:
        element.hidePanel()

    print("hideThisPanel: %s" % (panelToHide)) # DBGDBG

  # the last function in the file doesn't show up in the scope list in Understand for some reason!
  def stub(self):
    pass