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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <documentsignaturemanager.hxx>
#include <config_gpgme.h>
#include <gpg/SEInitializer.hxx>
#include <com/sun/star/embed/StorageFormats.hpp>
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/io/TempFile.hpp>
#include <com/sun/star/io/XTruncate.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/xml/crypto/SEInitializer.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/packages/manifest/ManifestReader.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/XWriter.hpp>
#include <comphelper/base64.hxx>
#include <comphelper/storagehelper.hxx>
#include <rtl/ustrbuf.hxx>
#include <sal/log.hxx>
#include <tools/datetime.hxx>
#include <certificate.hxx>
#include <biginteger.hxx>
#include <xmlsec/xmlsec_init.hxx>
#include <pdfsignaturehelper.hxx>
#include <memory>
using namespace css;
using namespace css::graphic;
using namespace css::uno;
DocumentSignatureManager::DocumentSignatureManager(
const uno::Reference<uno::XComponentContext>& xContext, DocumentSignatureMode eMode)
: mxContext(xContext)
, maSignatureHelper(xContext)
, meSignatureMode(eMode)
{
}
DocumentSignatureManager::~DocumentSignatureManager() { deInitXmlSec(); }
bool DocumentSignatureManager::init()
{
SAL_WARN_IF(mxSEInitializer.is(), "xmlsecurity.helper",
"DocumentSignatureManager::Init - mxSEInitializer already set!");
SAL_WARN_IF(mxSecurityContext.is(), "xmlsecurity.helper",
"DocumentSignatureManager::Init - mxSecurityContext already set!");
SAL_WARN_IF(mxGpgSEInitializer.is(), "xmlsecurity.helper",
"DocumentSignatureManager::Init - mxGpgSEInitializer already set!");
// xmlsec is needed by both services, so init before those
initXmlSec();
mxSEInitializer = xml::crypto::SEInitializer::create(mxContext);
#if HAVE_FEATURE_GPGME
mxGpgSEInitializer.set(new SEInitializerGpg());
#endif
if (mxSEInitializer.is())
mxSecurityContext = mxSEInitializer->createSecurityContext(OUString());
#if HAVE_FEATURE_GPGME
if (mxGpgSEInitializer.is())
mxGpgSecurityContext = mxGpgSEInitializer->createSecurityContext(OUString());
return mxSecurityContext.is() || mxGpgSecurityContext.is();
#else
return mxSecurityContext.is();
#endif
}
PDFSignatureHelper& DocumentSignatureManager::getPDFSignatureHelper()
{
bool bInit = true;
if (!mxSecurityContext.is())
bInit = init();
SAL_WARN_IF(!bInit, "xmlsecurity.comp", "Error initializing security context!");
if (!mpPDFSignatureHelper)
mpPDFSignatureHelper = std::make_unique<PDFSignatureHelper>();
return *mpPDFSignatureHelper;
}
#if 0 // For some reason does not work
bool DocumentSignatureManager::IsXAdESRelevant()
{
if (mxStore.is())
{
// ZIP-based: ODF or OOXML.
maSignatureHelper.StartMission();
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(embed::ElementModes::READ, /*bUseTempStream=*/true);
if (aStreamHelper.nStorageFormat == embed::StorageFormats::OFOPXML)
{
maSignatureHelper.EndMission();
return false;
}
// FIXME: How to figure out if it is ODF 1.2?
maSignatureHelper.EndMission();
return true;
}
return false;
}
#endif
bool DocumentSignatureManager::readManifest()
{
// Check if manifest was already read
if (m_manifest.hasElements())
return true;
if (!mxContext.is())
return false;
if (!mxStore.is())
return false;
uno::Reference<packages::manifest::XManifestReader> xReader
= packages::manifest::ManifestReader::create(mxContext);
if (mxStore->hasByName("META-INF"))
{
//Get the manifest.xml
uno::Reference<embed::XStorage> xSubStore(
mxStore->openStorageElement("META-INF", embed::ElementModes::READ), UNO_SET_THROW);
uno::Reference<io::XInputStream> xStream(
xSubStore->openStreamElement("manifest.xml", css::embed::ElementModes::READ),
UNO_QUERY_THROW);
m_manifest = xReader->readManifestSequence(xStream);
}
return true;
}
/* 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 DocumentSignatureManager::isXML(const OUString& rURI)
{
SAL_WARN_IF(!mxStore.is(), "xmlsecurity.helper", "empty storage reference");
bool bIsXML = false;
bool bPropsAvailable = false;
const OUString sPropFullPath("FullPath");
const OUString sPropMediaType("MediaType");
const OUString sPropDigest("Digest");
if (readManifest())
{
for (const uno::Sequence<beans::PropertyValue>& entry : std::as_const(m_manifest))
{
OUString sPath;
OUString sMediaType;
bool bEncrypted = false;
for (const beans::PropertyValue& prop : entry)
{
if (prop.Name == sPropFullPath)
prop.Value >>= sPath;
else if (prop.Name == sPropMediaType)
prop.Value >>= sMediaType;
else if (prop.Name == sPropDigest)
bEncrypted = true;
}
if (DocumentSignatureHelper::equalsReferenceUriManifestPath(rURI, sPath))
{
bIsXML = sMediaType == "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.
sal_Int32 nSep = rURI.lastIndexOf('.');
if (nSep != -1)
{
OUString aExt = rURI.copy(nSep + 1);
if (aExt.equalsIgnoreAsciiCase("XML"))
bIsXML = true;
}
}
return bIsXML;
}
//If bTempStream is true, then a temporary stream is return. If it is false then, the actual
//signature stream is used.
//Every time 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 DocumentSignatureManager::ImplOpenSignatureStream(sal_Int32 nStreamOpenMode,
bool bTempStream)
{
SignatureStreamHelper aHelper;
if (mxStore.is() && mxStore->hasByName("[Content_Types].xml"))
aHelper.nStorageFormat = embed::StorageFormats::OFOPXML;
if (bTempStream)
{
if (nStreamOpenMode & embed::ElementModes::TRUNCATE)
{
//We write always into a new temporary stream.
mxTempSignatureStream.set(io::TempFile::create(mxContext), uno::UNO_QUERY_THROW);
if (aHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
aHelper.xSignatureStream = mxTempSignatureStream;<--- aHelper.xSignatureStream is assigned
else
{
mxTempSignatureStorage = comphelper::OStorageHelper::GetStorageOfFormatFromStream(
ZIP_STORAGE_FORMAT_STRING, mxTempSignatureStream);
aHelper.xSignatureStorage = mxTempSignatureStorage;
}
}
else
{
//When we read from the temp stream, then we must have previously
//created one.
SAL_WARN_IF(!mxTempSignatureStream.is(), "xmlsecurity.helper",
"empty temp. signature stream reference");
}
aHelper.xSignatureStream = mxTempSignatureStream;<--- aHelper.xSignatureStream is overwritten
if (aHelper.nStorageFormat == embed::StorageFormats::OFOPXML)
aHelper.xSignatureStorage = mxTempSignatureStorage;
}
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 & embed::ElementModes::TRUNCATE)
{
if (aHelper.xSignatureStream.is()
&& aHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
{
uno::Reference<io::XTruncate> xTruncate(aHelper.xSignatureStream, uno::UNO_QUERY_THROW);
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
uno::Reference<io::XSeekable> xSeek(aHelper.xSignatureStream, uno::UNO_QUERY_THROW);
xSeek->seek(0);
}
return aHelper;
}
bool DocumentSignatureManager::add(
const uno::Reference<security::XCertificate>& xCert,
const uno::Reference<xml::crypto::XXMLSecurityContext>& xSecurityContext,
const OUString& rDescription, sal_Int32& nSecurityId, bool bAdESCompliant,
const OUString& rSignatureLineId, const Reference<XGraphic>& xValidGraphic,
const Reference<XGraphic>& xInvalidGraphic)
{
if (!xCert.is())
{
SAL_WARN("xmlsecurity.helper", "no certificate selected");
return false;
}
// GPG or X509 key?
uno::Reference<lang::XServiceInfo> xServiceInfo(xSecurityContext, uno::UNO_QUERY);
if (xServiceInfo->getImplementationName()
== "com.sun.star.xml.security.gpg.XMLSecurityContext_GpgImpl")
{
// GPG keys only really have PGPKeyId and PGPKeyPacket
if (!mxStore.is())
{
SAL_WARN("xmlsecurity.helper", "cannot sign pdfs with GPG keys");
return false;
}
maSignatureHelper.StartMission(xSecurityContext);
nSecurityId = maSignatureHelper.GetNewSecurityId();
OUStringBuffer aStrBuffer;
comphelper::Base64::encode(aStrBuffer, xCert->getEncoded());
OUString aKeyId;
if (auto pCertificate = dynamic_cast<xmlsecurity::Certificate*>(xCert.get()))
{
OUStringBuffer aBuffer;
comphelper::Base64::encode(aBuffer, pCertificate->getSHA256Thumbprint());
aKeyId = aBuffer.makeStringAndClear();
}
else
SAL_WARN("xmlsecurity.helper",
"XCertificate implementation without an xmlsecurity::Certificate one");
maSignatureHelper.SetGpgCertificate(nSecurityId, aKeyId, aStrBuffer.makeStringAndClear(),
xCert->getIssuerName());
}
else
{
OUString aCertSerial = xmlsecurity::bigIntegerToNumericString(xCert->getSerialNumber());
if (aCertSerial.isEmpty())
{
SAL_WARN("xmlsecurity.helper", "Error in Certificate, problem with serial number!");
return false;
}
if (!mxStore.is())
{
// Something not ZIP based, try PDF.
nSecurityId = getPDFSignatureHelper().GetNewSecurityId();
getPDFSignatureHelper().SetX509Certificate(xCert);
getPDFSignatureHelper().SetDescription(rDescription);
uno::Reference<io::XInputStream> xInputStream(mxSignatureStream, uno::UNO_QUERY);
if (!getPDFSignatureHelper().Sign(xInputStream, bAdESCompliant))
{
SAL_WARN("xmlsecurity.helper", "PDFSignatureHelper::Sign() failed");
return false;
}
return true;
}
maSignatureHelper.StartMission(xSecurityContext);
nSecurityId = maSignatureHelper.GetNewSecurityId();
OUStringBuffer aStrBuffer;
comphelper::Base64::encode(aStrBuffer, xCert->getEncoded());
OUString aCertDigest;
svl::crypto::SignatureMethodAlgorithm eAlgorithmID
= svl::crypto::SignatureMethodAlgorithm::RSA;
if (auto pCertificate = dynamic_cast<xmlsecurity::Certificate*>(xCert.get()))
{
OUStringBuffer aBuffer;
comphelper::Base64::encode(aBuffer, pCertificate->getSHA256Thumbprint());
aCertDigest = aBuffer.makeStringAndClear();
eAlgorithmID = pCertificate->getSignatureMethodAlgorithm();
}
else
SAL_WARN("xmlsecurity.helper",
"XCertificate implementation without an xmlsecurity::Certificate one");
maSignatureHelper.SetX509Certificate(nSecurityId, xCert->getIssuerName(), aCertSerial,
aStrBuffer.makeStringAndClear(), aCertDigest,
eAlgorithmID);
}
const uno::Sequence<uno::Reference<security::XCertificate>> aCertPath
= xSecurityContext->getSecurityEnvironment()->buildCertificatePath(xCert);
OUStringBuffer aStrBuffer;
for (uno::Reference<security::XCertificate> const& rxCertificate : aCertPath)
{
comphelper::Base64::encode(aStrBuffer, rxCertificate->getEncoded());
OUString aString = aStrBuffer.makeStringAndClear();
maSignatureHelper.AddEncapsulatedX509Certificate(aString);
}
std::vector<OUString> aElements = DocumentSignatureHelper::CreateElementList(
mxStore, meSignatureMode, DocumentSignatureAlgorithm::OOo3_2);
DocumentSignatureHelper::AppendContentTypes(mxStore, aElements);
for (OUString const& rUri : aElements)
{
bool bBinaryMode = !isXML(rUri);
maSignatureHelper.AddForSigning(nSecurityId, rUri, bBinaryMode, bAdESCompliant);
}
maSignatureHelper.SetDateTime(nSecurityId, DateTime(DateTime::SYSTEM));
maSignatureHelper.SetDescription(nSecurityId, rDescription);
if (!rSignatureLineId.isEmpty())
maSignatureHelper.SetSignatureLineId(nSecurityId, rSignatureLineId);
if (xValidGraphic.is())
maSignatureHelper.SetSignatureLineValidGraphic(nSecurityId, xValidGraphic);
if (xInvalidGraphic.is())
maSignatureHelper.SetSignatureLineInvalidGraphic(nSecurityId, xInvalidGraphic);
// 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 and 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(embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, true);
if (aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
{
uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY_THROW);
uno::Reference<xml::sax::XWriter> xSaxWriter
= maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
// Export old signatures...
uno::Reference<xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter,
uno::UNO_QUERY_THROW);
std::size_t nInfos = maCurrentSignatureInformations.size();
for (std::size_t n = 0; n < nInfos; n++)
XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n],
bAdESCompliant);
// Create a new one...
maSignatureHelper.CreateAndWriteSignature(xDocumentHandler, bAdESCompliant);
// That's it...
XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
}
else
{
// OOXML
// Handle relations.
maSignatureHelper.EnsureSignaturesRelation(mxStore, /*bAdd=*/true);
// Old signatures + the new one.
int nSignatureCount = maCurrentSignatureInformations.size() + 1;
maSignatureHelper.ExportSignatureRelations(aStreamHelper.xSignatureStorage,
nSignatureCount);
// Export old signatures.
for (std::size_t i = 0; i < maCurrentSignatureInformations.size(); ++i)
maSignatureHelper.ExportOOXMLSignature(mxStore, aStreamHelper.xSignatureStorage,
maCurrentSignatureInformations[i], i + 1);
// Create a new signature.
maSignatureHelper.CreateAndWriteOOXMLSignature(mxStore, aStreamHelper.xSignatureStorage,
nSignatureCount);
// Flush objects.
uno::Reference<embed::XTransactedObject> xTransact(aStreamHelper.xSignatureStorage,
uno::UNO_QUERY);
xTransact->commit();
uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY);
xOutputStream->closeOutput();
uno::Reference<io::XTempFile> xTempFile(aStreamHelper.xSignatureStream, uno::UNO_QUERY);
SAL_INFO("xmlsecurity.helper",
"DocumentSignatureManager::add temporary storage at " << xTempFile->getUri());
}
maSignatureHelper.EndMission();
return true;
}
void DocumentSignatureManager::remove(sal_uInt16 nPosition)
{
if (!mxStore.is())
{
// Something not ZIP based, try PDF.
uno::Reference<io::XInputStream> xInputStream(mxSignatureStream, uno::UNO_QUERY);
if (!PDFSignatureHelper::RemoveSignature(xInputStream, nPosition))
{
SAL_WARN("xmlsecurity.helper", "PDFSignatureHelper::RemoveSignature() failed");
return;
}
// Only erase when the removal was successful, it may fail for PDF.
// Also, erase the requested and all following signatures, as PDF signatures are always chained.
maCurrentSignatureInformations.erase(maCurrentSignatureInformations.begin() + nPosition,
maCurrentSignatureInformations.end());
return;
}
maCurrentSignatureInformations.erase(maCurrentSignatureInformations.begin() + nPosition);
// Export all other signatures...
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, /*bTempStream=*/true);
if (aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
{
uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY_THROW);
uno::Reference<xml::sax::XWriter> xSaxWriter
= maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
uno::Reference<xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter,
uno::UNO_QUERY_THROW);
std::size_t nInfos = maCurrentSignatureInformations.size();
for (std::size_t n = 0; n < nInfos; ++n)
XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n],
false /* ??? */);
XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
}
else
{
// OOXML
// Handle relations.
int nSignatureCount = maCurrentSignatureInformations.size();
maSignatureHelper.ExportSignatureRelations(aStreamHelper.xSignatureStorage,
nSignatureCount);
// Export old signatures.
for (std::size_t i = 0; i < maCurrentSignatureInformations.size(); ++i)
maSignatureHelper.ExportOOXMLSignature(mxStore, aStreamHelper.xSignatureStorage,
maCurrentSignatureInformations[i], i + 1);
// Flush objects.
uno::Reference<embed::XTransactedObject> xTransact(aStreamHelper.xSignatureStorage,
uno::UNO_QUERY);
xTransact->commit();
uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY);
xOutputStream->closeOutput();
uno::Reference<io::XTempFile> xTempFile(aStreamHelper.xSignatureStream, uno::UNO_QUERY);
SAL_INFO("xmlsecurity.helper", "DocumentSignatureManager::remove: temporary storage is at "
<< xTempFile->getUri());
}
}
void DocumentSignatureManager::read(bool bUseTempStream, bool bCacheLastSignature)
{
maCurrentSignatureInformations.clear();
if (mxStore.is())
{
// ZIP-based: ODF or OOXML.
maSignatureHelper.StartMission(mxSecurityContext);
SignatureStreamHelper aStreamHelper
= ImplOpenSignatureStream(embed::ElementModes::READ, bUseTempStream);
if (aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML
&& aStreamHelper.xSignatureStream.is())
{
uno::Reference<io::XInputStream> xInputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY);
maSignatureHelper.ReadAndVerifySignature(xInputStream);
}
else if (aStreamHelper.nStorageFormat == embed::StorageFormats::OFOPXML
&& aStreamHelper.xSignatureStorage.is())
maSignatureHelper.ReadAndVerifySignatureStorage(aStreamHelper.xSignatureStorage,
bCacheLastSignature);
maSignatureHelper.EndMission();
maCurrentSignatureInformations = maSignatureHelper.GetSignatureInformations();
}
else
{
// Something not ZIP based, try PDF.
uno::Reference<io::XInputStream> xInputStream(mxSignatureStream, uno::UNO_QUERY);
if (getPDFSignatureHelper().ReadAndVerifySignature(xInputStream))
maCurrentSignatureInformations = getPDFSignatureHelper().GetSignatureInformations();
}
}
void DocumentSignatureManager::write(bool bXAdESCompliantIfODF)
{
if (!mxStore.is())
{
// Something not ZIP based, assume PDF, which is written directly in add() already.
return;
}
// Export all other signatures...
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(
embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, false);
if (aStreamHelper.xSignatureStream.is()
&& aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
{
// ODF
uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream,
uno::UNO_QUERY);
uno::Reference<xml::sax::XWriter> xSaxWriter
= maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
uno::Reference<xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter,
uno::UNO_QUERY_THROW);
std::size_t nInfos = maCurrentSignatureInformations.size();
for (std::size_t n = 0; n < nInfos; ++n)
XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n],
bXAdESCompliantIfODF);
XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
}
else if (aStreamHelper.xSignatureStorage.is()
&& aStreamHelper.nStorageFormat == embed::StorageFormats::OFOPXML)
{
// OOXML
std::size_t nSignatureCount = maCurrentSignatureInformations.size();
maSignatureHelper.ExportSignatureContentTypes(mxStore, nSignatureCount);
if (nSignatureCount > 0)
maSignatureHelper.ExportSignatureRelations(aStreamHelper.xSignatureStorage,
nSignatureCount);
else
{
// Removing all signatures: then need to remove the signature relation as well.
maSignatureHelper.EnsureSignaturesRelation(mxStore, /*bAdd=*/false);
// Also remove the whole signature sub-storage: release our read-write reference + remove the element.
aStreamHelper = SignatureStreamHelper();
mxStore->removeElement("_xmlsignatures");
}
for (std::size_t i = 0; i < nSignatureCount; ++i)
maSignatureHelper.ExportOOXMLSignature(mxStore, aStreamHelper.xSignatureStorage,
maCurrentSignatureInformations[i], i + 1);
}
// If stream was not provided, we are responsible for committing it...
if (!mxSignatureStream.is() && aStreamHelper.xSignatureStorage.is())
{
uno::Reference<embed::XTransactedObject> xTrans(aStreamHelper.xSignatureStorage,
uno::UNO_QUERY);
xTrans->commit();
}
}
uno::Reference<xml::crypto::XSecurityEnvironment> DocumentSignatureManager::getSecurityEnvironment()
{
return mxSecurityContext.is() ? mxSecurityContext->getSecurityEnvironment()
: uno::Reference<xml::crypto::XSecurityEnvironment>();
}
uno::Reference<xml::crypto::XSecurityEnvironment>
DocumentSignatureManager::getGpgSecurityEnvironment()
{
return mxGpgSecurityContext.is() ? mxGpgSecurityContext->getSecurityEnvironment()
: uno::Reference<xml::crypto::XSecurityEnvironment>();
}
uno::Reference<xml::crypto::XXMLSecurityContext> const&
DocumentSignatureManager::getSecurityContext() const
{
return mxSecurityContext;
}
uno::Reference<xml::crypto::XXMLSecurityContext> const&
DocumentSignatureManager::getGpgSecurityContext() const
{
return mxGpgSecurityContext;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|