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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org 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 version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_xmlsecurity.hxx"
#include <xmlsecurity/digitalsignaturesdialog.hxx>
#include <xmlsecurity/certificatechooser.hxx>
#include <xmlsecurity/certificateviewer.hxx>
#include <xmlsecurity/biginteger.hxx>
#include <xmloff/xmluconv.hxx>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/io/XTruncate.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/security/NoPasswordException.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/security/CertificateValidity.hdl>
#include <com/sun/star/packages/WrongPasswordException.hpp>
#include <com/sun/star/security/SerialNumberAdapter.hpp>
#include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
#include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
#include <com/sun/star/packages/manifest/XManifestReader.hpp>
#include <rtl/ustrbuf.hxx>
#include <rtl/uri.hxx>
#include <tools/date.hxx>
#include <tools/time.hxx>
#include "dialogs.hrc"
#include "digitalsignaturesdialog.hrc"
#include "helpids.hrc"
#include "resourcemanager.hxx"
#include <vcl/msgbox.hxx> // Until encrypted docs work...
#include <unotools/configitem.hxx>
#include <comphelper/componentcontext.hxx>
#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
/* HACK: disable some warnings for MS-C */
#ifdef _MSC_VER
#pragma warning (disable : 4355) // 4355: this used in initializer-list
#endif
using namespace ::com::sun::star::security;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star;
namespace css = ::com::sun::star;
using ::rtl::OUString;
namespace
{
class SaveODFItem: public utl::ConfigItem
{
sal_Int16 m_nODF;
public:
virtual void Commit();
virtual void Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
SaveODFItem();
//See group ODF in Common.xcs
bool isLessODF1_2()
{
return m_nODF < 3;
}
};
void SaveODFItem::Commit() {}
void SaveODFItem::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& ) {}
SaveODFItem::SaveODFItem(): utl::ConfigItem(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
"Office.Common/Save"))), m_nODF(0)
{
OUString sDef(RTL_CONSTASCII_USTRINGPARAM("ODF/DefaultVersion"));
Sequence< css::uno::Any > aValues = GetProperties( Sequence<OUString>(&sDef,1) );
if ( aValues.getLength() == 1)
{
sal_Int16 nTmp = 0;
if ( aValues[0] >>= nTmp )
m_nODF = nTmp;
else
throw uno::RuntimeException(
OUString(RTL_CONSTASCII_USTRINGPARAM(
"[xmlsecurity]SaveODFItem::SaveODFItem(): Wrong Type!")), 0 );
}
else
throw uno::RuntimeException(
OUString(RTL_CONSTASCII_USTRINGPARAM(
"[xmlsecurity] Could not open property Office.Common/Save/ODF/DefaultVersion")), 0);
}
}
/* Using the zip storage, we cannot get the properties "MediaType" and "IsEncrypted"
We use the manifest to find out if a file is xml and if it is encrypted.
The parameter is an encoded uri. However, the manifest contains paths. Therefore
the path is encoded as uri, so they can be compared.
*/
bool DigitalSignaturesDialog::isXML(const rtl::OUString& rURI )
{
OSL_ASSERT(mxStore.is());
bool bIsXML = false;
bool bPropsAvailable = false;
const OUString sPropFullPath(RTL_CONSTASCII_USTRINGPARAM("FullPath"));
const OUString sPropMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
const OUString sPropDigest(RTL_CONSTASCII_USTRINGPARAM("Digest"));
for (int i = 0; i < m_manifest.getLength(); i++)
{
Any digest;
const Sequence< css::beans::PropertyValue >& entry = m_manifest[i];
OUString sPath, sMediaType;
bool bEncrypted = false;
for (int j = 0; j < entry.getLength(); j++)
{
const css::beans::PropertyValue & prop = entry[j];
if (prop.Name.equals( sPropFullPath ) )
prop.Value >>= sPath;
else if (prop.Name.equals( sPropMediaType ) )
prop.Value >>= sMediaType;
else if (prop.Name.equals( sPropDigest ) )
bEncrypted = true;
}
if (DocumentSignatureHelper::equalsReferenceUriManifestPath(rURI, sPath))
{
bIsXML = sMediaType.equals(OUSTR("text/xml")) && ! bEncrypted;
bPropsAvailable = true;
break;
}
}
if (!bPropsAvailable)
{
//This would be the case for at least mimetype, META-INF/manifest.xml
//META-INF/macrosignatures.xml.
//Files can only be encrypted if they are in the manifest.xml.
//That is, the current file cannot be encrypted, otherwise bPropsAvailable
//would be true.
OUString aXMLExt( RTL_CONSTASCII_USTRINGPARAM( "XML" ) );
sal_Int32 nSep = rURI.lastIndexOf( '.' );
if ( nSep != (-1) )
{
OUString aExt = rURI.copy( nSep+1 );
if (aExt.equalsIgnoreAsciiCase(aXMLExt ))
bIsXML = true;
}
}
return bIsXML;
}
DigitalSignaturesDialog::DigitalSignaturesDialog(
Window* pParent,
uno::Reference< uno::XComponentContext >& rxCtx, DocumentSignatureMode eMode,
sal_Bool bReadOnly, const ::rtl::OUString& sODFVersion, bool bHasDocumentSignature)
:ModalDialog ( pParent, XMLSEC_RES( RID_XMLSECDLG_DIGSIG ) )
,mxCtx ( rxCtx )
,maSignatureHelper ( rxCtx )
,meSignatureMode ( eMode )
,maHintDocFT ( this, XMLSEC_RES( FT_HINT_DOC ) )
,maHintBasicFT ( this, XMLSEC_RES( FT_HINT_BASIC ) )
,maHintPackageFT ( this, XMLSEC_RES( FT_HINT_PACK ) )
,maSignaturesLB ( this, XMLSEC_RES( LB_SIGNATURES ) )
,maSigsValidImg ( this, XMLSEC_RES( IMG_STATE_VALID ) )
,maSigsValidFI ( this, XMLSEC_RES( FI_STATE_VALID ) )
,maSigsInvalidImg ( this, XMLSEC_RES( IMG_STATE_BROKEN ) )
,maSigsInvalidFI ( this, XMLSEC_RES( FI_STATE_BROKEN ) )
,maSigsNotvalidatedImg( this, XMLSEC_RES( IMG_STATE_NOTVALIDATED ) )
,maSigsNotvalidatedFI ( this, XMLSEC_RES( FI_STATE_NOTVALIDATED ) )
,maSigsOldSignatureFI ( this, XMLSEC_RES( FI_STATE_OLDSIGNATURE) )
,maViewBtn ( this, XMLSEC_RES( BTN_VIEWCERT ) )
,maAddBtn ( this, XMLSEC_RES( BTN_ADDCERT ) )
,maRemoveBtn ( this, XMLSEC_RES( BTN_REMOVECERT ) )
,maBottomSepFL ( this, XMLSEC_RES( FL_BOTTOM_SEP ) )
,maOKBtn ( this, XMLSEC_RES( BTN_OK ) )
,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) )
,m_sODFVersion (sODFVersion)
,m_bHasDocumentSignature(bHasDocumentSignature)
,m_bWarningShowSignMacro(false)
{
// --> PB #i48253 the tablistbox needs its own unique id
maSignaturesLB.Window::SetUniqueId( HID_XMLSEC_TREE_SIGNATURESDLG );
// <--
Size aControlSize( maSignaturesLB.GetSizePixel() );
aControlSize = maSignaturesLB.PixelToLogic( aControlSize, MapMode( MAP_APPFONT ) );
const long nControlWidth = aControlSize.Width();
static long nTabs[] = { 4, 0, 6*nControlWidth/100, 36*nControlWidth/100, 74*nControlWidth/100 };
maSignaturesLB.SetTabs( &nTabs[ 0 ] );
maSignaturesLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) );
maSigsNotvalidatedFI.SetText( String( XMLSEC_RES( STR_NO_INFO_TO_VERIFY ) ) );
FreeResource();
mbVerifySignatures = true;
mbSignaturesChanged = false;
maSignaturesLB.SetSelectHdl( LINK( this, DigitalSignaturesDialog, SignatureHighlightHdl ) );
maSignaturesLB.SetDoubleClickHdl( LINK( this, DigitalSignaturesDialog, SignatureSelectHdl ) );
maViewBtn.SetClickHdl( LINK( this, DigitalSignaturesDialog, ViewButtonHdl ) );
maViewBtn.Disable();
maAddBtn.SetClickHdl( LINK( this, DigitalSignaturesDialog, AddButtonHdl ) );
if ( bReadOnly )
maAddBtn.Disable();
maRemoveBtn.SetClickHdl( LINK( this, DigitalSignaturesDialog, RemoveButtonHdl ) );
maRemoveBtn.Disable();
maOKBtn.SetClickHdl( LINK( this, DigitalSignaturesDialog, OKButtonHdl) );
switch( meSignatureMode )
{
case SignatureModeDocumentContent: maHintDocFT.Show(); break;
case SignatureModeMacros: maHintBasicFT.Show(); break;
case SignatureModePackage: maHintPackageFT.Show(); break;
}
// adjust fixed text to images
XmlSec::AlignAndFitImageAndControl( maSigsValidImg, maSigsValidFI, 5 );
XmlSec::AlignAndFitImageAndControl( maSigsInvalidImg, maSigsInvalidFI, 5 );
XmlSec::AlignAndFitImageAndControl( maSigsNotvalidatedImg, maSigsNotvalidatedFI, 5 );
XmlSec::AlignAndFitImageAndControl( maSigsNotvalidatedImg, maSigsOldSignatureFI, 5 );
}
DigitalSignaturesDialog::~DigitalSignaturesDialog()
{
}
BOOL DigitalSignaturesDialog::Init( const rtl::OUString& rTokenName )
{
bool bInit = maSignatureHelper.Init( rTokenName );
DBG_ASSERT( bInit, "Error initializing security context!" );
if ( bInit )
{
maSignatureHelper.SetStartVerifySignatureHdl( LINK( this, DigitalSignaturesDialog, StartVerifySignatureHdl ) );
}
return bInit;
}
void DigitalSignaturesDialog::SetStorage( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStore )
{
mxStore = rxStore;
maSignatureHelper.SetStorage( mxStore, m_sODFVersion);
Reference < css::packages::manifest::XManifestReader > xReader(
mxCtx->getServiceManager()->createInstanceWithContext(
OUSTR("com.sun.star.packages.manifest.ManifestReader"), mxCtx), UNO_QUERY_THROW);
//Get the manifest.xml
Reference < css::embed::XStorage > xSubStore(rxStore->openStorageElement(
OUSTR("META-INF"), css::embed::ElementModes::READ), UNO_QUERY_THROW);
Reference< css::io::XInputStream > xStream(
xSubStore->openStreamElement(OUSTR("manifest.xml"), css::embed::ElementModes::READ),
UNO_QUERY_THROW);
m_manifest = xReader->readManifestSequence(xStream);
}
void DigitalSignaturesDialog::SetSignatureStream( const cssu::Reference < css::io::XStream >& rxStream )
{
mxSignatureStream = rxStream;
}
bool DigitalSignaturesDialog::canAddRemove()
{
//m56
bool ret = true;
OSL_ASSERT(mxStore.is());
bool bDoc1_1 = DocumentSignatureHelper::isODFPre_1_2(m_sODFVersion);
SaveODFItem item;
bool bSave1_1 = item.isLessODF1_2();
// see specification
//cvs: specs/www/appwide/security/Electronic_Signatures_and_Security.sxw
//Paragraph 'Behavior with regard to ODF 1.2'
//For both, macro and document
if ( (!bSave1_1 && bDoc1_1) || (bSave1_1 && bDoc1_1) )
{
//#4
ErrorBox err(NULL, XMLSEC_RES(RID_XMLSECDLG_OLD_ODF_FORMAT));
err.Execute();
ret = false;
}
//As of OOo 3.2 the document signature includes in macrosignatures.xml. That is
//adding a macro signature will break an existing document signature.
//The sfx2 will remove the documentsignature when the user adds a macro signature
if (meSignatureMode == SignatureModeMacros
&& ret)
{
if (m_bHasDocumentSignature && !m_bWarningShowSignMacro)
{
//The warning says that the document signatures will be removed if the user
//continues. He can then either press 'OK' or 'NO'
//It the user presses 'Add' or 'Remove' several times then, then the warning
//is shown every time until the user presses 'OK'. From then on, the warning
//is not displayed anymore as long as the signatures dialog is alive.
if (QueryBox(
NULL, XMLSEC_RES(MSG_XMLSECDLG_QUERY_REMOVEDOCSIGNBEFORESIGN)).Execute() == RET_NO)
ret = false;
else
m_bWarningShowSignMacro = true;
}
}
return ret;
}
bool DigitalSignaturesDialog::canAdd()
{
if (canAddRemove())
return true;
return false;
}
bool DigitalSignaturesDialog::canRemove()
{
if (canAddRemove())
return true;
return false;
}
short DigitalSignaturesDialog::Execute()
{
// Verify Signatures and add certificates to ListBox...
mbVerifySignatures = true;
ImplGetSignatureInformations(false);
ImplFillSignaturesBox();
// Only verify once, content will not change.
// But for refreshing signature information, StartVerifySignatureHdl will be called after each add/remove
mbVerifySignatures = false;
return Dialog::Execute();
}
IMPL_LINK( DigitalSignaturesDialog, SignatureHighlightHdl, void*, EMPTYARG )
{
bool bSel = maSignaturesLB.FirstSelected() ? true : false;
maViewBtn.Enable( bSel );
if ( maAddBtn.IsEnabled() ) // not read only
maRemoveBtn.Enable( bSel );
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, OKButtonHdl, void*, EMPTYARG )
{
// Export all other signatures...
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
embed::ElementModes::WRITE|embed::ElementModes::TRUNCATE, false );
uno::Reference< io::XOutputStream > xOutputStream(
aStreamHelper.xSignatureStream, uno::UNO_QUERY );
uno::Reference< com::sun::star::xml::sax::XDocumentHandler> xDocumentHandler =
maSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream );
size_t nInfos = maCurrentSignatureInformations.size();
for( size_t n = 0 ; n < nInfos ; ++n )
maSignatureHelper.ExportSignature(
xDocumentHandler, maCurrentSignatureInformations[ n ] );
maSignatureHelper.CloseDocumentHandler( xDocumentHandler);
// If stream was not provided, we are responsible for committing it....
if ( !mxSignatureStream.is() )
{
uno::Reference< embed::XTransactedObject > xTrans(
aStreamHelper.xSignatureStorage, uno::UNO_QUERY );
xTrans->commit();
}
EndDialog(RET_OK);
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, SignatureSelectHdl, void*, EMPTYARG )
{
ImplShowSignaturesDetails();
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, ViewButtonHdl, Button*, EMPTYARG )
{
ImplShowSignaturesDetails();
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, AddButtonHdl, Button*, EMPTYARG )
{
if( ! canAdd())
return 0;
try
{
uno::Reference<com::sun::star::xml::crypto::XSecurityEnvironment> xSecEnv = maSignatureHelper.GetSecurityEnvironment();
uno::Reference<com::sun::star::security::XSerialNumberAdapter> xSerialNumberAdapter =
::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
CertificateChooser aChooser( this, mxCtx, xSecEnv, maCurrentSignatureInformations );
if ( aChooser.Execute() == RET_OK )
{
uno::Reference< ::com::sun::star::security::XCertificate > xCert = aChooser.GetSelectedCertificate();
if ( !xCert.is() )
{
DBG_ERRORFILE( "no certificate selected" );
return -1;
}
rtl::OUString aCertSerial = xSerialNumberAdapter->toString( xCert->getSerialNumber() );
if ( !aCertSerial.getLength() )
{
OSL_FAIL( "Error in Certificate, problem with serial number!" );
return -1;
}
maSignatureHelper.StartMission();
sal_Int32 nSecurityId = maSignatureHelper.GetNewSecurityId();
rtl::OUStringBuffer aStrBuffer;
SvXMLUnitConverter::encodeBase64(aStrBuffer, xCert->getEncoded());
maSignatureHelper.SetX509Certificate( nSecurityId,
xCert->getIssuerName(), aCertSerial,
aStrBuffer.makeStringAndClear());
std::vector< rtl::OUString > aElements =
DocumentSignatureHelper::CreateElementList(
mxStore, rtl::OUString(), meSignatureMode, OOo3_2Document);
sal_Int32 nElements = aElements.size();
for ( sal_Int32 n = 0; n < nElements; n++ )
{
bool bBinaryMode = !isXML(aElements[n]);
maSignatureHelper.AddForSigning( nSecurityId, aElements[n], aElements[n], bBinaryMode );
}
maSignatureHelper.SetDateTime( nSecurityId, Date(), Time() );
// We open a signature stream in which the existing and the new
//signature is written. ImplGetSignatureInformation (later in this function) will
//then read the stream an will fill maCurrentSignatureInformations. The final signature
//is written when the user presses OK. Then only maCurrentSignatureInformation and
//a sax writer are used to write the information.
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
css::embed::ElementModes::WRITE|css::embed::ElementModes::TRUNCATE, true);
Reference< css::io::XOutputStream > xOutputStream(
aStreamHelper.xSignatureStream, UNO_QUERY_THROW);
Reference< css::xml::sax::XDocumentHandler> xDocumentHandler =
maSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream );
// Export old signatures...
size_t nInfos = maCurrentSignatureInformations.size();
for ( size_t n = 0; n < nInfos; n++ )
maSignatureHelper.ExportSignature( xDocumentHandler, maCurrentSignatureInformations[n]);
// Create a new one...
maSignatureHelper.CreateAndWriteSignature( xDocumentHandler );
// That's it...
maSignatureHelper.CloseDocumentHandler( xDocumentHandler);
maSignatureHelper.EndMission();
aStreamHelper = SignatureStreamHelper(); // release objects...
mbSignaturesChanged = true;
sal_Int32 nStatus = maSignatureHelper.GetSignatureInformation( nSecurityId ).nStatus;
if ( nStatus == ::com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
{
mbSignaturesChanged = true;
// Can't simply remember current information, need parsing for getting full information :(
// We need to verify the signatures again, otherwise the status in the signature information
// will not contain
// SecurityOperationStatus_OPERATION_SUCCEEDED
mbVerifySignatures = true;
ImplGetSignatureInformations(true);
ImplFillSignaturesBox();
}
}
}
catch ( uno::Exception& )
{
OSL_FAIL( "Exception while adding a signature!" );
// Don't keep invalid entries...
ImplGetSignatureInformations(true);
ImplFillSignaturesBox();
}
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, RemoveButtonHdl, Button*, EMPTYARG )
{
if (!canRemove())
return 0;
if( maSignaturesLB.FirstSelected() )
{
try
{
USHORT nSelected = (USHORT) (sal_uIntPtr) maSignaturesLB.FirstSelected()->GetUserData();
maCurrentSignatureInformations.erase( maCurrentSignatureInformations.begin()+nSelected );
// Export all other signatures...
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
css::embed::ElementModes::WRITE | css::embed::ElementModes::TRUNCATE, true);
Reference< css::io::XOutputStream > xOutputStream(
aStreamHelper.xSignatureStream, UNO_QUERY_THROW);
Reference< css::xml::sax::XDocumentHandler> xDocumentHandler =
maSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream );
size_t nInfos = maCurrentSignatureInformations.size();
for( size_t n = 0 ; n < nInfos ; ++n )
maSignatureHelper.ExportSignature( xDocumentHandler, maCurrentSignatureInformations[ n ] );
maSignatureHelper.CloseDocumentHandler( xDocumentHandler);
mbSignaturesChanged = true;
aStreamHelper = SignatureStreamHelper(); // release objects...
ImplFillSignaturesBox();
}
catch ( uno::Exception& )
{
OSL_FAIL( "Exception while removing a signature!" );
// Don't keep invalid entries...
ImplGetSignatureInformations(true);
ImplFillSignaturesBox();
}
}
return 0;
}
IMPL_LINK( DigitalSignaturesDialog, StartVerifySignatureHdl, void*, EMPTYARG )
{
return mbVerifySignatures ? 1 : 0;
}
void DigitalSignaturesDialog::ImplFillSignaturesBox()
{
maSignaturesLB.Clear();
uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecEnv = maSignatureHelper.GetSecurityEnvironment();
uno::Reference<com::sun::star::security::XSerialNumberAdapter> xSerialNumberAdapter =
::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
uno::Reference< ::com::sun::star::security::XCertificate > xCert;
String aNullStr;
size_t nInfos = maCurrentSignatureInformations.size();
size_t nValidSigs = 0, nValidCerts = 0;
bool bAllNewSignatures = true;
if( nInfos )
{
for( size_t n = 0; n < nInfos; ++n )
{
DocumentSignatureAlgorithm mode = DocumentSignatureHelper::getDocumentAlgorithm(
m_sODFVersion, maCurrentSignatureInformations[n]);
std::vector< rtl::OUString > aElementsToBeVerified =
DocumentSignatureHelper::CreateElementList(
mxStore, ::rtl::OUString(), meSignatureMode, mode);
const SignatureInformation& rInfo = maCurrentSignatureInformations[n];
//First we try to get the certificate which is embedded in the XML Signature
if (rInfo.ouX509Certificate.getLength())
xCert = xSecEnv->createCertificateFromAscii(rInfo.ouX509Certificate);
else {
//There must be an embedded certificate because we use it to get the
//issuer name. We cannot use /Signature/KeyInfo/X509Data/X509IssuerName
//because it could be modified by an attacker. The issuer is displayed
//in the digital signature dialog.
//Comparing the X509IssuerName with the one from the X509Certificate in order
//to find out if the X509IssuerName was modified does not work. See #i62684
DBG_ASSERT(sal_False, "Could not find embedded certificate!");
}
//In case there is no embedded certificate we try to get it from a local store
//Todo: This probably could be removed, see above.
if (!xCert.is())
xCert = xSecEnv->getCertificate( rInfo.ouX509IssuerName, xSerialNumberAdapter->toSequence( rInfo.ouX509SerialNumber ) );
DBG_ASSERT( xCert.is(), "Certificate not found and can't be created!" );
String aSubject;
String aIssuer;
String aDateTimeStr;
bool bSigValid = false;
bool bCertValid = false;
if( xCert.is() )
{
//check the validity of the cert
try {
sal_Int32 certResult = xSecEnv->verifyCertificate(xCert,
Sequence<css::uno::Reference<css::security::XCertificate> >());
bCertValid = certResult == css::security::CertificateValidity::VALID ? true : false;
if ( bCertValid )
nValidCerts++;
} catch (css::uno::SecurityException& ) {
OSL_ENSURE(0, "Verification of certificate failed");
bCertValid = false;
}
aSubject = XmlSec::GetContentPart( xCert->getSubjectName() );
aIssuer = XmlSec::GetContentPart( xCert->getIssuerName() );
// String with date and time information (#i20172#)
aDateTimeStr = XmlSec::GetDateTimeString( rInfo.stDateTime );
}
bSigValid = ( rInfo.nStatus == ::com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED );
if ( bSigValid )
{
bSigValid = DocumentSignatureHelper::checkIfAllFilesAreSigned(
aElementsToBeVerified, rInfo, mode);
if( bSigValid )
nValidSigs++;
}
Image aImage;
if (!bSigValid)
{
aImage = maSigsInvalidImg.GetImage();
}
else if (bSigValid && !bCertValid)
{
aImage = maSigsNotvalidatedImg.GetImage();
}
//Check if the signature is a "old" document signature, that is, which was created
//by an version of OOo previous to 3.2
else if (meSignatureMode == SignatureModeDocumentContent
&& bSigValid && bCertValid && !DocumentSignatureHelper::isOOo3_2_Signature(
maCurrentSignatureInformations[n]))
{
aImage = maSigsNotvalidatedImg.GetImage();
bAllNewSignatures &= false;
}
else if (meSignatureMode == SignatureModeDocumentContent
&& bSigValid && bCertValid && DocumentSignatureHelper::isOOo3_2_Signature(
maCurrentSignatureInformations[n]))
{
aImage = maSigsValidImg.GetImage();
}
else if (meSignatureMode == SignatureModeMacros
&& bSigValid && bCertValid)
{
aImage = maSigsValidImg.GetImage();
}
SvLBoxEntry* pEntry = maSignaturesLB.InsertEntry( aNullStr, aImage, aImage );
maSignaturesLB.SetEntryText( aSubject, pEntry, 1 );
maSignaturesLB.SetEntryText( aIssuer, pEntry, 2 );
maSignaturesLB.SetEntryText( aDateTimeStr, pEntry, 3 );
pEntry->SetUserData( ( void* ) n ); // missuse user data as index
}
}
bool bAllSigsValid = (nValidSigs == nInfos);
bool bAllCertsValid = (nValidCerts == nInfos);
bool bShowValidState = nInfos && (bAllSigsValid && bAllCertsValid && bAllNewSignatures);
bool bShowNotValidatedState = nInfos && (bAllSigsValid && (!bAllCertsValid || !bAllNewSignatures));
bool bShowInvalidState = nInfos && !bAllSigsValid;
maSigsValidImg.Show( bShowValidState);
maSigsValidFI.Show( bShowValidState );
maSigsInvalidImg.Show( bShowInvalidState );
maSigsInvalidFI.Show( bShowInvalidState );
maSigsNotvalidatedImg.Show(bShowNotValidatedState);
//bAllNewSignatures is always true if we are not in document mode
maSigsNotvalidatedFI.Show(nInfos && bAllSigsValid && ! bAllCertsValid);
maSigsOldSignatureFI.Show(nInfos && bAllSigsValid && bAllCertsValid && !bAllNewSignatures);
SignatureHighlightHdl( NULL );
}
//If bUseTempStream is true then the temporary signature stream is used.
//Otherwise the real signature stream is used.
void DigitalSignaturesDialog::ImplGetSignatureInformations(bool bUseTempStream)
{
maCurrentSignatureInformations.clear();
maSignatureHelper.StartMission();
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
css::embed::ElementModes::READ, bUseTempStream);
if ( aStreamHelper.xSignatureStream.is() )
{
uno::Reference< io::XInputStream > xInputStream( aStreamHelper.xSignatureStream, uno::UNO_QUERY );
maSignatureHelper.ReadAndVerifySignature( xInputStream );
}
maSignatureHelper.EndMission();
maCurrentSignatureInformations = maSignatureHelper.GetSignatureInformations();
mbVerifySignatures = false;
}
void DigitalSignaturesDialog::ImplShowSignaturesDetails()
{
if( maSignaturesLB.FirstSelected() )
{
USHORT nSelected = (USHORT) (sal_uIntPtr) maSignaturesLB.FirstSelected()->GetUserData();
const SignatureInformation& rInfo = maCurrentSignatureInformations[ nSelected ];
css::uno::Reference<css::xml::crypto::XSecurityEnvironment > xSecEnv =
maSignatureHelper.GetSecurityEnvironment();
css::uno::Reference<com::sun::star::security::XSerialNumberAdapter> xSerialNumberAdapter =
::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
// Use Certificate from doc, not from key store
uno::Reference< dcss::security::XCertificate > xCert;
if (rInfo.ouX509Certificate.getLength())
xCert = xSecEnv->createCertificateFromAscii(rInfo.ouX509Certificate);
//fallback if no certificate is embedded, get if from store
if (!xCert.is())
xCert = xSecEnv->getCertificate( rInfo.ouX509IssuerName, xSerialNumberAdapter->toSequence( rInfo.ouX509SerialNumber ) );
DBG_ASSERT( xCert.is(), "Error getting cCertificate!" );
if ( xCert.is() )
{
CertificateViewer aViewer( this, maSignatureHelper.GetSecurityEnvironment(), xCert, FALSE );
aViewer.Execute();
}
}
}
//If bTempStream is true, then a temporary stream is return. If it is false then, the actual
//signature stream is used.
//Everytime the user presses Add a new temporary stream is created.
//We keep the temporary stream as member because ImplGetSignatureInformations
//will later access the stream to create DocumentSignatureInformation objects
//which are stored in maCurrentSignatureInformations.
SignatureStreamHelper DigitalSignaturesDialog::ImplOpenSignatureStream(
sal_Int32 nStreamOpenMode, bool bTempStream)
{
SignatureStreamHelper aHelper;
if (bTempStream)
{
if (nStreamOpenMode & css::embed::ElementModes::TRUNCATE)
{
//We write always into a new temporary stream.
mxTempSignatureStream = Reference < css::io::XStream >(
mxCtx->getServiceManager()->createInstanceWithContext(
OUSTR( "com.sun.star.io.TempFile" ), mxCtx) ,
UNO_QUERY_THROW);
aHelper.xSignatureStream = mxTempSignatureStream;
}
else
{
//When we read from the temp stream, then we must have previously
//created one.
OSL_ASSERT(mxTempSignatureStream.is());
}
aHelper.xSignatureStream = mxTempSignatureStream;
}
else
{
//No temporary stream
if (!mxSignatureStream.is())
{
//We may not have a dedicated stream for writing the signature
//So we take one directly from the storage
//Or DocumentDigitalSignatures::showDocumentContentSignatures was called,
//in which case Add/Remove is not allowed. This is done, for example, if the
//document is readonly
aHelper = DocumentSignatureHelper::OpenSignatureStream(
mxStore, nStreamOpenMode, meSignatureMode );
}
else
{
aHelper.xSignatureStream = mxSignatureStream;
}
}
if (nStreamOpenMode & css::embed::ElementModes::TRUNCATE)
{
css::uno::Reference < css::io::XTruncate > xTruncate(
aHelper.xSignatureStream, UNO_QUERY_THROW);
DBG_ASSERT( xTruncate.is(), "ImplOpenSignatureStream - Stream does not support xTruncate!" );
xTruncate->truncate();
}
else if ( bTempStream || mxSignatureStream.is())
{
//In case we read the signature stream from the storage directly,
//which is the case when DocumentDigitalSignatures::showDocumentContentSignatures
//then XSeakable is not supported
css::uno::Reference < css::io::XSeekable > xSeek(
aHelper.xSignatureStream, UNO_QUERY_THROW);
DBG_ASSERT( xSeek.is(), "ImplOpenSignatureStream - Stream does not support xSeekable!" );
xSeek->seek( 0 );
}
return aHelper;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|