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
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: XMLSecurityFrameworkController.java,v $
*
* $Revision: 1.3 $
*
* last change: $Author: rt $ $Date: 2005-09-09 17:45:38 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
package com.sun.star.xml.security.uno;
import java.util.Stack;
import java.util.Vector;
/* uno classes */
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XInitialization;
import com.sun.star.uno.XComponentContext;
import com.sun.star.xml.sax.XDocumentHandler;
import com.sun.star.xml.sax.XAttributeList;
import com.sun.star.xml.sax.SAXException;
import com.sun.star.xml.crypto.*;
import com.sun.star.xml.crypto.sax.*;
import com.sun.star.xml.wrapper.*;
/*
* the XMLSecurityFrameworkController class is used to controll the xml security framework.
*/
public class XMLSecurityFrameworkController
implements XDocumentHandler, XSignatureCreationResultListener, XSignatureVerifyResultListener,
XEncryptionResultListener, XDecryptionResultListener, XSAXEventKeeperStatusChangeListener
{
/*
* UNO framework component
*/
private XMultiComponentFactory m_xRemoteServiceManager;
private XComponentContext m_xRemoteContext;
/*
* xml security related UNO components
*/
private XSecuritySAXEventKeeper m_xSAXEventKeeper;
private XXMLDocumentWrapper m_xXMLDocumentWrapper;
private XDocumentHandler m_xOutputHandler;
private XXMLSecurityContext m_xXMLSecurityContext;
private XXMLSignature m_xXMLSignature;
private XXMLEncryption m_xXMLEncryption;
/*
* used to reserve the current SAX ancestor path
*/
private Stack m_currentPath;
/*
* maintains all SignatureEntities.
*/
private Vector m_signatureList;
/*
* maintains all EncryptionEntities.
*/
private Vector m_encryptionList;
/*
* maintains all unsolved reference Ids.
* These ids are strings which is the value of the id attribute
* of the referenced element.
*/
private Vector m_vUnsolvedReferenceIds;
/*
* maintains all unsolved reference keeper ids.
* The keeper id is used to uniquely identify a bufferred element
* by the SAXEventKeeper.
*/
private Vector m_vUnsolvedReferencedKeeperIds;
/*
* maintains the left time that each unsolved reference can be
* claimed.
*/
private Vector m_vUnsolvedReferenceRefNum;
/*
* whether exporting or importing
*/
private boolean m_bIsExporting;
/*
* whether java or c
*/
private boolean m_bIsJavaBased;
/*
* whether the SAXEventKeeper is blocking
*/
private boolean m_bIsBlocking;
/*
* whether it is collecting a bufferred element
*/
private boolean m_bIsInsideCollectedElement;
/*
* whether a SAXEventKeeper is in the SAX chain
*/
private boolean m_bSAXEventKeeperIncluded;
/*
* the ParsingThread used to parse the document
*/
private ParsingThread m_parsingThread;
/*
* the next document handler that will receives SAX events
* from the parsing thread.
* if the SAXEventKeeper is on the SAX chain, then this
* variable will be the SAXEventKeeper, otherwise, this
* variable will be the xOutputHandler.
*/
private XDocumentHandler m_xExportHandler;
/*
* the TestTool used to feedback information
*/
private TestTool m_testTool;
/*
* for encryption target
*/
private boolean m_bIsEncryptionTarget;
private EncryptionEntity m_EncryptionForTarget;
XMLSecurityFrameworkController(
TestTool testTool,
boolean bIsExporting,
boolean bIsJavaBased,
XDocumentHandler xOutputHandler,
ParsingThread parsingThread,
XXMLSecurityContext xXMLSecurityContext,
XXMLSignature xXMLSignature,
XXMLEncryption xXMLEncryption,
XMultiComponentFactory xRemoteServiceManager,
XComponentContext xRemoteContext)
{
m_bIsExporting = bIsExporting;
m_bIsJavaBased = bIsJavaBased;
m_xOutputHandler = xOutputHandler;
m_xXMLSecurityContext = xXMLSecurityContext;
m_xXMLSignature = xXMLSignature;
m_xXMLEncryption = xXMLEncryption;
m_xRemoteServiceManager = xRemoteServiceManager;
m_xRemoteContext = xRemoteContext;
m_testTool = testTool;
m_parsingThread = parsingThread;
m_signatureList = new Vector();
m_encryptionList = new Vector();
m_vUnsolvedReferenceIds = new Vector();
m_vUnsolvedReferencedKeeperIds = new Vector();
m_vUnsolvedReferenceRefNum = new Vector();
m_xXMLDocumentWrapper = null;
m_xSAXEventKeeper = null;
m_bSAXEventKeeperIncluded = false;
m_bIsBlocking = false;
m_bIsInsideCollectedElement = false;
m_bIsEncryptionTarget = false;
m_EncryptionForTarget = null;
changeOutput();
m_currentPath = new Stack();
foundSecurityRelated();
}
/**************************************************************************************
* private methods
**************************************************************************************/
/*
* changes the output document handler.
*/
private void changeOutput()
{
if (m_bIsExporting)
{
m_parsingThread.setHandler(this);
/*
* If the SAXEventKeeper is in the SAX chain, then redirects output
* to the SAXEventKeeper, otherwise, to the m_xOutputHandler
*/
if (m_bSAXEventKeeperIncluded)
{
m_xExportHandler = (XDocumentHandler)UnoRuntime.queryInterface(
XDocumentHandler.class, m_xSAXEventKeeper);
m_xSAXEventKeeper.setNextHandler(m_xOutputHandler);
m_testTool.updatesSAXChainInformation("XMLExporter -> SAXEventKeeper -> SAXWriter");
}
else
{
m_xExportHandler = m_xOutputHandler;
m_testTool.updatesSAXChainInformation("XMLExporter -> SAXWriter");
}
}
else
{
if (m_bSAXEventKeeperIncluded)
{
m_parsingThread.setHandler(
(XDocumentHandler)UnoRuntime.queryInterface(XDocumentHandler.class, m_xSAXEventKeeper));
m_xSAXEventKeeper.setNextHandler(this);
m_testTool.updatesSAXChainInformation("SAXParser -> SAXEventKeeper -> XMLImporter");
}
else
{
m_parsingThread.setHandler(this);
m_testTool.updatesSAXChainInformation("SAXParser -> XMLImporter");
}
m_xExportHandler = m_xOutputHandler;
}
}
/*
* handles the situation when a security related element is found.
* if the SAXEventKeeper is not initialized, then creates a
* SAXEventKeeper.
* the return value represents whether the SAXEventKeeper is newly
* created.
*/
private boolean foundSecurityRelated()
{
if (m_xSAXEventKeeper == null)
{
m_testTool.showMessage("Message from : "+
(m_bIsExporting?"XMLExporter":"XMLImporter")+
"\n\nA security related content found, a SAXEventKeeper is created.\n ");
m_bIsBlocking = false;
m_bIsInsideCollectedElement = false;
try
{
/*
* creates an XMLDocumentWrapper component.
*/
Object xmlDocumentObj = null;
if (m_bIsJavaBased)
{
xmlDocumentObj = m_xRemoteServiceManager.createInstanceWithContext(
TestTool.XMLDOCUMENTWRAPPER_COMPONENT_JAVA, m_xRemoteContext);
}
else
{
xmlDocumentObj = m_xRemoteServiceManager.createInstanceWithContext(
TestTool.XMLDOCUMENTWRAPPER_COMPONENT_C, m_xRemoteContext);
}
m_xXMLDocumentWrapper = (XXMLDocumentWrapper)UnoRuntime.queryInterface(
XXMLDocumentWrapper.class, xmlDocumentObj);
/*
* creates a SAXEventKeeper component.
*/
Object saxEventKeeperObj = m_xRemoteServiceManager.createInstanceWithContext(
TestTool.SAXEVENTKEEPER_COMPONENT, m_xRemoteContext);
m_xSAXEventKeeper =
(XSecuritySAXEventKeeper)UnoRuntime.queryInterface(
XSecuritySAXEventKeeper.class, saxEventKeeperObj);
/*
* initializes the SAXEventKeeper component with the XMLDocumentWrapper component.
*/
XInitialization xInitialization =
(XInitialization)UnoRuntime.queryInterface(
XInitialization.class, m_xSAXEventKeeper);
Object args[]=new Object[1];
args[0] = m_xXMLDocumentWrapper;
xInitialization.initialize(args);
}
catch( com.sun.star.uno.Exception e)
{
e.printStackTrace();
}
/*
* configures the SAXEventKeeper's status change listener.
*/
XSAXEventKeeperStatusChangeBroadcaster xSaxEventKeeperStatusChangeBroadcaster =
(XSAXEventKeeperStatusChangeBroadcaster)UnoRuntime.queryInterface(
XSAXEventKeeperStatusChangeBroadcaster.class, m_xSAXEventKeeper);
xSaxEventKeeperStatusChangeBroadcaster.addSAXEventKeeperStatusChangeListener(this);
}
boolean rc = !m_bSAXEventKeeperIncluded;
/*
* changes the export document handler.
*/
m_bSAXEventKeeperIncluded=true;
changeOutput();
return rc;
}
/*
* finds key element or referenced element for a signature.
*/
private void findKeyOrReference(SecurityEntity signatureEntity, String uriStr, boolean isFindingKey)
{
int i=0;
while (i<m_vUnsolvedReferenceIds.size())
{
String id = (String)m_vUnsolvedReferenceIds.elementAt(i);
if (id.equals(uriStr))
{
int refNum = ((Integer)m_vUnsolvedReferenceRefNum.elementAt(i)).intValue();
int keeperId = ((Integer)m_vUnsolvedReferencedKeeperIds.elementAt(i)).intValue();
if (isFindingKey)
{
/*
* clones a new ElementCollector for the key element.
*/
int cloneKeeperId = m_xSAXEventKeeper.cloneElementCollector(
keeperId,
m_bIsExporting?
(ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY));
/*
* notifies the key keeper id.
*/
signatureEntity.setKeyId(cloneKeeperId);
/*
* sets the security id for the key.
*/
m_xSAXEventKeeper.setSecurityId(cloneKeeperId, signatureEntity.getSecurityId());
/*
* sets the resolve listener.
*/
XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
xReferenceResolvedBroadcaster.addReferenceResolvedListener(
cloneKeeperId,
signatureEntity.getReferenceListener());
}
else
{
/*
* clones a new ElementCollector for the referenced element.
*/
int cloneKeeperId = m_xSAXEventKeeper.cloneElementCollector(
keeperId,
m_bIsExporting?
(ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY));
/*
* sets the security id.
*/
m_xSAXEventKeeper.setSecurityId(cloneKeeperId, signatureEntity.getSecurityId());
/*
* sets the resolve listener.
*/
XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
xReferenceResolvedBroadcaster.addReferenceResolvedListener(cloneKeeperId,
signatureEntity.getReferenceListener());
try{
XReferenceCollector xReferenceCollector =
(XReferenceCollector)UnoRuntime.queryInterface(
XReferenceCollector.class, signatureEntity.getReferenceListener());
xReferenceCollector.setReferenceId(cloneKeeperId);
}
catch( com.sun.star.uno.Exception e)
{
e.printStackTrace();
}
}
/*
* if this unsolved reference reaches its max reference number, remove this reference
* from all vectors.
*/
refNum--;
if (refNum == 0)
{
m_xSAXEventKeeper.removeElementCollector(keeperId);
m_vUnsolvedReferenceIds.remove(i);
m_vUnsolvedReferencedKeeperIds.remove(i);
m_vUnsolvedReferenceRefNum.remove(i);
}
else
{
m_vUnsolvedReferenceRefNum.setElementAt(new Integer(refNum),(i));
++i;
}
/*
* If it is find a key, then no further search is needed, one
* signature has one key at most.
*/
if (isFindingKey)
{
break;
}
}
else
{
++i;
}
}
}
/*
* checks whether a startElement event represents any security related information.
* return true if this event can't be forwarded into the SAX chain.
*/
private boolean checkSecurityElement(String localName, com.sun.star.xml.sax.XAttributeList xattribs)
{
boolean rc = false;
if (localName.equals("Signature"))
/*
* this element is a Signature element.
*/
{
SignatureEntity signatureEntity = new SignatureEntity(
m_xSAXEventKeeper,
m_bIsExporting,
this,
m_xXMLSecurityContext,
m_xXMLSignature,
m_xXMLEncryption,
m_xRemoteServiceManager,
m_xRemoteContext);
m_signatureList.add(signatureEntity);
m_currentPath.push(signatureEntity);
}
else if(localName.equals("Reference"))
{
if (!m_currentPath.empty())
{
Object signedInfo = m_currentPath.pop();
if (!m_currentPath.empty())
{
Object objSignature = m_currentPath.peek();
if ((objSignature instanceof SignatureEntity) && signedInfo.toString().equals("SignedInfo"))
/*
* this element is a Reference element in a signature.
*/
{
String uriStr = xattribs.getValueByName("URI");
if (uriStr.charAt(0) == '#')
{
uriStr = uriStr.substring(1);
SignatureEntity signatureEntity = (SignatureEntity)objSignature;
if (uriStr != null && uriStr.length()>0)
{
signatureEntity.addReferenceId(uriStr);
findKeyOrReference(signatureEntity, uriStr, false);
}
}
}
}
m_currentPath.push(signedInfo);
}
m_currentPath.push(localName);
}
else if(localName.equals("KeyValue") ||
localName.equals("KeyName") ||
localName.equals("X509Data") ||
localName.equals("EncryptedKey"))
{
if (!m_currentPath.empty())
{
Object keyInfo = m_currentPath.pop();
if (!m_currentPath.empty())
{
Object objSorE = m_currentPath.peek();
if ((objSorE instanceof SignatureEntity) && keyInfo.toString().equals("KeyInfo"))
/*
* this element is the key element of a signature.
*/
{
SignatureEntity signatureEntity = (SignatureEntity)objSorE;
signatureEntity.setKeyId(0);
}
else if ((objSorE instanceof EncryptionEntity) && keyInfo.toString().equals("KeyInfo"))
/*
* this element is the key element of an encryption.
*/
{
EncryptionEntity theEncryption = (EncryptionEntity)objSorE;
theEncryption.setKeyId(0);
}
}
m_currentPath.push(keyInfo);
}
m_currentPath.push(localName);
}
else if(localName.equals("RetrievalMethod"))
{
if (!m_currentPath.empty())
{
Object keyInfo = m_currentPath.pop();
if (!m_currentPath.empty())
{
Object objSorE = m_currentPath.peek();
if ((objSorE instanceof SignatureEntity) && keyInfo.toString().equals("KeyInfo"))
/*
* this element is the RetrievalMethod element in a signature,
* which will include the key uri of this signature.
*/
{
String uriStr = xattribs.getValueByName("URI");
SignatureEntity signatureEntity = (SignatureEntity)objSorE;
if (uriStr != null && uriStr.length()>0)
{
signatureEntity.setKeyURI(uriStr);
findKeyOrReference(signatureEntity,uriStr, true);
}
}
else if ((objSorE instanceof EncryptionEntity) && keyInfo.toString().equals("KeyInfo"))
/*
* this element is the RetrievalMethod element in an encryption,
* which will include the key uri of this encryption.
*/
{
String uriStr = xattribs.getValueByName("URI");
EncryptionEntity theEncryption = (EncryptionEntity)objSorE;
if (uriStr != null && uriStr.length()>0)
{
theEncryption.setKeyURI(uriStr);
findKeyOrReference(theEncryption, uriStr, true);
}
}
}
m_currentPath.push(keyInfo);
}
m_currentPath.push(localName);
}
else if (localName.equals("EncryptedData")) /* || localName.equals("EncryptedKey")) */
/*
* this element is an Encryption element.
*/
{
EncryptionEntity theEncryption = new EncryptionEntity(
m_xSAXEventKeeper,
m_bIsExporting,
this,
m_xXMLSecurityContext,
m_xXMLSignature,
m_xXMLEncryption,
m_xRemoteServiceManager,
m_xRemoteContext);
m_encryptionList.add(theEncryption);
if (m_bIsExporting)
{
m_currentPath.push(theEncryption);
}
else
{
String uriStr = xattribs.getValueByName("keyURI");
if (uriStr != null && uriStr.length()>0)
{
theEncryption.setKeyURI(uriStr);
findKeyOrReference(theEncryption,uriStr, true);
}
else
{
theEncryption.setKeyId(0);
}
rc = true;
}
}
else
/*
* not a security related element.
*/
{
m_currentPath.push(localName);
}
return rc;
}
/*
* checks whether a startElement event is referenced by any security entity.
*/
private void checkReference(String localName, com.sun.star.xml.sax.XAttributeList xattribs, String id)
{
String refNumStr = xattribs.getValueByName("refNum");
if ( m_bIsEncryptionTarget )
{
m_EncryptionForTarget.setReference(m_bIsExporting);
m_bIsEncryptionTarget = false;
}
if (id != null && id.length()>0 )
/*
* only if this element has id attribute, then it can be referenced by
* a security entity.
*/
{
/*
* if this element has an "refNum" attribute, then the value will be
* the max referencing number on this element, otherwise, set the max
* referencing number to 999.
*/
int refNum = 999;
if (refNumStr != null && refNumStr.length()>0 )
{
refNum = new Integer(refNumStr).intValue();
}
int length;
/*
* searches the signature list to check whether any sigture has
* reference on this element.
*/
length = m_signatureList.size();
for (int i=0; i<length; ++i)
{
SignatureEntity signatureEntity = (SignatureEntity)m_signatureList.elementAt(i);
if (signatureEntity.setReference(id, m_bIsExporting))
{
refNum--;
}
if (signatureEntity.setKey(id, m_bIsExporting))
{
refNum--;
}
}
/*
* searches the encryption list for reference.
*/
length = m_encryptionList.size();
for (int i=0; i<length; ++i)
{
EncryptionEntity theEncryption = (EncryptionEntity)m_encryptionList.elementAt(i);
if (theEncryption.setKey(id, m_bIsExporting))
{
refNum--;
}
}
/*
* if the max referencing number is not reached, then add this element
* into the unsolved reference list.
*/
if (refNum>0)
{
int keeperId;
if (localName.equals("EncryptedKey"))
{
keeperId = m_xSAXEventKeeper.addSecurityElementCollector(
m_bIsExporting?
(ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY),
true);
}
else
{
keeperId = m_xSAXEventKeeper.addSecurityElementCollector(
m_bIsExporting?
(ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY),
false);
}
m_vUnsolvedReferenceIds.add(id);
m_vUnsolvedReferencedKeeperIds.add(new Integer(keeperId));
m_vUnsolvedReferenceRefNum.add(new Integer(refNum));
}
}
}
/*
* configures the output handler.
*/
private void setOutputHandler(XDocumentHandler handler)
{
m_xOutputHandler = handler;
changeOutput();
}
/**************************************************************************************
* protected methods
**************************************************************************************/
/*
* methods used to transfer unsolved reference information.
*/
protected Vector getUnsolvedReferenceIds()
{
return m_vUnsolvedReferenceIds;
}
protected Vector getUnsolvedReferenceKeeperIds()
{
return m_vUnsolvedReferencedKeeperIds;
}
protected Vector getUnsolvedReferenceRefNum()
{
return m_vUnsolvedReferenceRefNum;
}
protected String getBufferNodeTreeInformation()
{
if (m_xSAXEventKeeper != null)
{
return m_xSAXEventKeeper.printBufferNodeTree();
}
else
{
return null;
}
}
protected void getDocument(XDocumentHandler handler)
{
if (m_xXMLDocumentWrapper != null)
{
try
{
m_xXMLDocumentWrapper.getTree(handler);
}
catch(SAXException e)
{
e.printStackTrace();
}
}
}
protected void endMission()
{
while (m_signatureList.size()>0 || m_encryptionList.size()>0)
{
if (m_signatureList.size()>0)
{
SignatureEntity signatureEntity = (SignatureEntity)m_signatureList.elementAt(0);
m_signatureList.remove(0);
signatureEntity.endMission();
}
else if (m_encryptionList.size()>0)
{
EncryptionEntity theEncryption = (EncryptionEntity)m_encryptionList.elementAt(0);
m_encryptionList.remove(0);
theEncryption.endMission();
}
}
while (m_vUnsolvedReferenceIds.size()>0)
{
int keeperId = ((Integer)m_vUnsolvedReferencedKeeperIds.elementAt(0)).intValue();
m_xSAXEventKeeper.removeElementCollector(keeperId);
m_vUnsolvedReferenceIds.remove(0);
m_vUnsolvedReferencedKeeperIds.remove(0);
m_vUnsolvedReferenceRefNum.remove(0);
}
m_xSAXEventKeeper.setNextHandler(null);
XSAXEventKeeperStatusChangeBroadcaster xSaxEventKeeperStatusChangeBroadcaster =
(XSAXEventKeeperStatusChangeBroadcaster)UnoRuntime.queryInterface(
XSAXEventKeeperStatusChangeBroadcaster.class, m_xSAXEventKeeper);
xSaxEventKeeperStatusChangeBroadcaster.addSAXEventKeeperStatusChangeListener(null);
m_xSAXEventKeeper = null;
m_xXMLDocumentWrapper = null;
m_xOutputHandler = null;
m_xXMLSecurityContext = null;
m_xXMLSignature = null;
m_xXMLEncryption = null;
m_xExportHandler = null;
m_parsingThread.setHandler(null);
}
/**************************************************************************************
* public methods
**************************************************************************************/
/*
* XDocumentHandler
*/
public void startDocument()
{
try{
m_xExportHandler.startDocument();
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void endDocument()
{
try{
m_xExportHandler.endDocument();
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
{
try{
String idAttr = xattribs.getValueByName("id");
if (idAttr == null)
{
idAttr = xattribs.getValueByName("Id");
}
boolean hasIdAttr = (idAttr != null && idAttr.length()>0 );
boolean needResend = false;
if (hasIdAttr ||
(str.equals("Signature")||str.equals("EncryptedData")))/* || str.equals("EncryptedKey"))) */
{
if (foundSecurityRelated() && !m_bIsExporting)
{
needResend = true;
}
}
boolean suppressToNext = checkSecurityElement(str, xattribs);
checkReference(str, xattribs, idAttr);
if (needResend)
{
m_xSAXEventKeeper.setNextHandler(null);
XDocumentHandler saxEventKeeperHandler =
(XDocumentHandler)UnoRuntime.queryInterface(
XDocumentHandler.class, m_xSAXEventKeeper);
saxEventKeeperHandler.startElement(str, xattribs);
m_xSAXEventKeeper.setNextHandler((XDocumentHandler)this);
}
if (!suppressToNext)
{
m_xExportHandler.startElement(str, xattribs);
}
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void endElement(String str)
{
if (!m_currentPath.empty())
{
Object obj = m_currentPath.pop();
if (obj.toString().equals("SignedInfo"))
{
if (!m_currentPath.empty())
{
Object objSignature = m_currentPath.peek();
if (objSignature != null && objSignature instanceof SignatureEntity)
{
((SignatureEntity)objSignature).setReferenceNumber();
}
}
}
else if (obj instanceof EncryptionEntity)
{
m_bIsEncryptionTarget = true;
m_EncryptionForTarget = (EncryptionEntity)obj;
}
}
try{
m_xExportHandler.endElement(str);
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void characters(String str)
{
try{
m_xExportHandler.characters(str);
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void ignorableWhitespace(String str)
{
}
public void processingInstruction(String aTarget, String aData)
{
try{
m_xExportHandler.processingInstruction(aTarget, aData);
}
catch( com.sun.star.xml.sax.SAXException e)
{
e.printStackTrace();
}
}
public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
throws com.sun.star.xml.sax.SAXException
{
}
/*
* XSignatureCreationResultListener
*/
public void signatureCreated(int securityId, SecurityOperationStatus creationResult)
{
String message = new String();
message += "A Signature is created:";
message += "\nSecurity Id = "+securityId;
message += "\nCreation result = "+((creationResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
m_testTool.showMessage("Message from : SignatureCreator\n\n"+message+"\n ");
}
/*
* XSignatureVerifyResultListener
*/
public void signatureVerified(int securityId, SecurityOperationStatus verifyResult)
{
String message = new String();
message += "A Signature is verified:";
message += "\nSecurity Id = "+securityId;
message += "\nVerify result = "+((verifyResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
m_testTool.showMessage("Message from : SignatureVerifier\n\n"+message+"\n ");
}
/*
* XEncryptionResultListener
*/
public void encrypted(int securityId, SecurityOperationStatus encryptionResult)
{
String message = new String();
message += "An EncryptedData is encrypted:";
message += "\nSecurity Id = "+securityId;
message += "\nEncrypt result = "+((encryptionResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
m_testTool.showMessage("Message from : Encryptor\n\n"+message+"\n ");
}
/*
* XDecryptionResultListener methods
*/
public void decrypted(int securityId, SecurityOperationStatus decryptionResult)
{
String message = new String();
message += "An EncryptedData is decrypted:";
message += "\nSecurity Id = "+securityId;
message += "\nDecrypt result = "+((decryptionResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
m_testTool.showMessage("Message from : Decryptor\n\n"+message+"\n ");
}
/*
* XSAXEventKeeperStatusChangeListener methods
*/
public void blockingStatusChanged(boolean isBlocking)
{
m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
(isBlocking?"The SAX event stream is blocked.":"The SAX event stream is unblocked.")+
"\n ");
this.m_bIsBlocking = isBlocking;
}
public void collectionStatusChanged(boolean isInsideCollectedElement)
{
m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
(isInsideCollectedElement?"Begin to buffer data ...":"End of data bufferring.")+
"\n ");
/*
this.m_bIsInsideCollectedElement = isInsideCollectedElement;
if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
{
m_bSAXEventKeeperIncluded = false;
}
else
{
m_bSAXEventKeeperIncluded = true;
}
changeOutput();
*/
}
public void bufferStatusChanged(boolean isBufferEmpty)
{
m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
(isBufferEmpty?"All bufferred data are released, the SAXEventKeeper is destroyed.":"buffer data appears.")+
"\n ");
/*
if (isBufferEmpty)
{
m_xXMLDocumentWrapper = null;
m_xSAXEventKeeper = null;
m_bSAXEventKeeperIncluded = false;
changeOutput();
}
*/
}
}
|