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
|
/* -*- 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 <config_gpgme.h>
#include <xsecctl.hxx>
#include "xsecparser.hxx"
#include "ooxmlsecparser.hxx"
#include <biginteger.hxx>
#include <framework/signatureverifierimpl.hxx>
#include <framework/saxeventkeeperimpl.hxx>
#include <gpg/xmlsignature_gpgimpl.hxx>
#include <gpg/SEInitializer.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
#include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
#include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
#include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
#include <com/sun/star/xml/crypto/XSEInitializer.hpp>
#include <com/sun/star/graphic/GraphicProvider.hpp>
#include <com/sun/star/embed/StorageFormats.hpp>
#include <sal/log.hxx>
#include <unotools/datetime.hxx>
#include <comphelper/base64.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/seqstream.hxx>
namespace com::sun::star::graphic { class XGraphic; }
using namespace css;
using namespace css::uno;
using namespace css::beans;
/* protected: for signature verify */
css::uno::Reference< css::xml::crypto::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
sal_Int32 nSecurityId)
{
if ( m_eStatusOfSecurityComponents != InitializationState::INITIALIZED )
{
return nullptr;
}
sal_Int32 nIdOfSignatureElementCollector;
css::uno::Reference< css::xml::crypto::sax::XReferenceResolvedListener > xReferenceResolvedListener;
nIdOfSignatureElementCollector =
m_xSAXEventKeeper->addSecurityElementCollector( css::xml::crypto::sax::ElementMarkPriority_BEFOREMODIFY, false);
m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
/*
* create a SignatureVerifier
*/
xReferenceResolvedListener = new SignatureVerifierImpl;
css::uno::Reference<css::lang::XInitialization> xInitialization(xReferenceResolvedListener, css::uno::UNO_QUERY);
css::uno::Sequence<css::uno::Any> args(5);
args[0] <<= OUString::number(nSecurityId);
args[1] <<= uno::Reference<xml::crypto::sax::XSecuritySAXEventKeeper>(static_cast<cppu::OWeakObject*>(m_xSAXEventKeeper.get()), uno::UNO_QUERY);
args[2] <<= OUString::number(nIdOfSignatureElementCollector);
args[3] <<= m_xSecurityContext;
args[4] <<= m_xXMLSignature;
xInitialization->initialize(args);
css::uno::Reference< css::xml::crypto::sax::XSignatureVerifyResultBroadcaster >
signatureVerifyResultBroadcaster(xReferenceResolvedListener, css::uno::UNO_QUERY);
signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
m_xSAXEventKeeper->addReferenceResolvedListener(
nIdOfSignatureElementCollector,
xReferenceResolvedListener);
css::uno::Reference<css::xml::crypto::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, css::uno::UNO_QUERY);
keyCollector->setKeyId(0);
return xReferenceResolvedListener;
}
void XSecController::addSignature()
{
css::uno::Reference< css::xml::crypto::sax::XReferenceResolvedListener > xReferenceResolvedListener;
sal_Int32 nSignatureId = 0;
if (m_bVerifyCurrentSignature)
{
chainOn();
xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
m_bVerifyCurrentSignature = false;
nSignatureId = m_nReservedSignatureId;
}
InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
m_vInternalSignatureInformations.push_back( isi );
}
void XSecController::setSignatureMethod(svl::crypto::SignatureMethodAlgorithm eAlgorithmID)
{
if (m_vInternalSignatureInformations.empty())
return;
m_vInternalSignatureInformations.back().signatureInfor.eAlgorithmID = eAlgorithmID;
}
void XSecController::switchGpgSignature()
{
#if HAVE_FEATURE_GPGME
// swap signature verifier for the Gpg one
m_xXMLSignature.set(new XMLSignature_GpgImpl());
if (m_vInternalSignatureInformations.empty())
return;
SignatureVerifierImpl* pImpl=
dynamic_cast<SignatureVerifierImpl*>(
m_vInternalSignatureInformations.back().xReferenceResolvedListener.get());
if (pImpl)
{
css::uno::Reference<css::xml::crypto::XSEInitializer> xGpgSEInitializer(
new SEInitializerGpg());
pImpl->updateSignature(new XMLSignature_GpgImpl(),
xGpgSEInitializer->createSecurityContext(OUString()));
}
#else
(void) this;
#endif
}
bool XSecController::haveReferenceForId(std::u16string_view rId) const
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::haveReferenceForId: no signature");
return false;
}
InternalSignatureInformation const& rIsi(m_vInternalSignatureInformations.back());
for (SignatureReferenceInformation const& rSri : rIsi.signatureInfor.vSignatureReferenceInfors)
{
if (rSri.nType == SignatureReferenceType::SAMEDOCUMENT
&& rSri.ouURI == rId) // ouUri has # stripped
{
return true;
}
}
return false;
}
void XSecController::addReference( const OUString& ouUri, sal_Int32 nDigestID, const OUString& ouType )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::addReference: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.addReference(SignatureReferenceType::SAMEDOCUMENT, nDigestID, ouUri, -1, ouType );
}
void XSecController::addStreamReference(
const OUString& ouUri,
bool isBinary,
sal_Int32 nDigestID )
{
SignatureReferenceType type = (isBinary?SignatureReferenceType::BINARYSTREAM:SignatureReferenceType::XMLSTREAM);
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::addStreamReference: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
if ( isi.xReferenceResolvedListener.is() )
{
/*
* get the input stream
*/
css::uno::Reference< css::io::XInputStream > xObjectInputStream
= getObjectInputStream( ouUri );
if ( xObjectInputStream.is() )
{
css::uno::Reference<css::xml::crypto::XUriBinding> xUriBinding
(isi.xReferenceResolvedListener, css::uno::UNO_QUERY);
xUriBinding->setUriBinding(ouUri, xObjectInputStream);
}
}
isi.addReference(type, nDigestID, ouUri, -1, OUString());
}
void XSecController::setReferenceCount() const
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setReferenceCount: no signature");
return;
}
const InternalSignatureInformation &isi =
m_vInternalSignatureInformations.back();
if ( !isi.xReferenceResolvedListener.is() )
return;
const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
int refNum = refInfors.size();
sal_Int32 referenceCount = 0;
for(int i=0 ; i<refNum; ++i)
{
if (refInfors[i].nType == SignatureReferenceType::SAMEDOCUMENT )
/*
* same-document reference
*/
{
referenceCount++;
}
}
css::uno::Reference<css::xml::crypto::sax::XReferenceCollector> xReferenceCollector
(isi.xReferenceResolvedListener, css::uno::UNO_QUERY);
xReferenceCollector->setReferenceCount( referenceCount );
}
void XSecController::setX509Data(SignatureInformation::X509Data const& rData)
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setX509IssuerName: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
// TODO: ImplVerifySignatures() handles all-empty case?
isi.signatureInfor.X509Datas.push_back(rData);
}
void XSecController::setSignatureValue( OUString const & ouSignatureValue )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setSignatureValue: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.signatureInfor.ouSignatureValue = ouSignatureValue;
}
void XSecController::setDigestValue( sal_Int32 nDigestID, OUString const & ouDigestValue )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
if (isi.signatureInfor.vSignatureReferenceInfors.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature reference");
return;
}
SignatureReferenceInformation &reference =
isi.signatureInfor.vSignatureReferenceInfors.back();
reference.nDigestID = nDigestID;
reference.ouDigestValue = ouDigestValue;
}
void XSecController::setGpgKeyID( OUString const & ouKeyID )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setGpgKeyID: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.signatureInfor.ouGpgKeyID = ouKeyID;
}
void XSecController::setGpgCertificate( OUString const & ouGpgCert )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setGpgCertificate: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.signatureInfor.ouGpgCertificate = ouGpgCert;
}
void XSecController::setGpgOwner( OUString const & ouGpgOwner )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setGpgOwner: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.signatureInfor.ouGpgOwner = ouGpgOwner;
}
void XSecController::setDate(OUString const& rId, OUString const& ouDate)
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setDate: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
// there may be multiple timestamps in a signature - check them for consistency
if (!isi.signatureInfor.ouDateTime.isEmpty()
&& isi.signatureInfor.ouDateTime != ouDate)
{
isi.signatureInfor.hasInconsistentSigningTime = true;
}
(void)utl::ISO8601parseDateTime( ouDate, isi.signatureInfor.stDateTime);
isi.signatureInfor.ouDateTime = ouDate;
if (!rId.isEmpty())
{
isi.signatureInfor.ouDateTimePropertyId = rId;
}
}
void XSecController::setDescription(OUString const& rId, OUString const& rDescription)
{
if (m_vInternalSignatureInformations.empty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.ouDescription = rDescription;
if (!rId.isEmpty())
{
rInformation.signatureInfor.ouDescriptionPropertyId = rId;
}
}
void XSecController::setSignatureBytes(const uno::Sequence<sal_Int8>& rBytes)
{
if (m_vInternalSignatureInformations.empty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.aSignatureBytes = rBytes;
}
void XSecController::setX509CertDigest(
OUString const& rCertDigest, sal_Int32 const /*TODO nReferenceDigestID*/,
std::u16string_view const& rX509IssuerName, std::u16string_view const& rX509SerialNumber)
{
if (m_vInternalSignatureInformations.empty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
for (auto & it : rInformation.signatureInfor.X509Datas)
{
if (xmlsecurity::EqualDistinguishedNames(it.X509IssuerName, rX509IssuerName)
&& it.X509SerialNumber == rX509SerialNumber)
{
it.CertDigest = rCertDigest;
return;
}
}
// fall-back: read the actual certificates
for (auto & it : rInformation.signatureInfor.X509Datas)
{
if (!it.X509Certificate.isEmpty())
{
try
{
uno::Reference<xml::crypto::XSecurityEnvironment> const xSecEnv(m_xSecurityContext->getSecurityEnvironment());
uno::Reference<security::XCertificate> const xCert(xSecEnv->createCertificateFromAscii(it.X509Certificate));
if (!xCert.is())
{
SAL_INFO("xmlsecurity.helper", "cannot parse X509Certificate");
}
else if (xmlsecurity::EqualDistinguishedNames(xCert->getIssuerName(),rX509IssuerName)
&& xmlsecurity::bigIntegerToNumericString(xCert->getSerialNumber()) == rX509SerialNumber)
{
it.CertDigest = rCertDigest;
// note: testInsertCertificate_PEM_DOCX requires these!
it.X509SerialNumber = rX509SerialNumber;
it.X509IssuerName = rX509IssuerName;
return;
}
}
catch (uno::Exception const&)
{
SAL_INFO("xmlsecurity.helper", "cannot parse X509Certificate");
}
}
}
if (!rInformation.signatureInfor.ouGpgCertificate.isEmpty())
{
SAL_INFO_IF(rCertDigest != rInformation.signatureInfor.ouGpgKeyID,
"xmlsecurity.helper", "PGPKeyID vs CertDigest mismatch");
}
else
{
SAL_INFO("xmlsecurity.helper", "cannot find X509Data for CertDigest");
}
}
namespace {
Reference<css::graphic::XGraphic> lcl_getGraphicFromString(const OUString& rImage)
{
Sequence<sal_Int8> seq;
comphelper::Base64::decode(seq, rImage);
Reference< graphic::XGraphic > xGraphic;
if( !seq.hasElements() )
return Reference<css::graphic::XGraphic>();
Reference< graphic::XGraphicProvider > xGraphicProvider(
graphic::GraphicProvider::create(comphelper::getProcessComponentContext()) );
Reference< io::XInputStream > xInputStream( new ::comphelper::SequenceInputStream( seq ) );
Sequence< PropertyValue > aArgs( 1 );
aArgs[ 0 ].Name = "InputStream";
aArgs[ 0 ].Value <<= xInputStream;
xGraphic = xGraphicProvider->queryGraphic(aArgs);
return xGraphic;
}
}
void XSecController::setValidSignatureImage(const OUString& rValidSigImg)
{
if (m_vInternalSignatureInformations.empty() || rValidSigImg.isEmpty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.aValidSignatureImage = lcl_getGraphicFromString(rValidSigImg);
}
void XSecController::setInvalidSignatureImage(const OUString& rInvalidSigImg)
{
if (m_vInternalSignatureInformations.empty() || rInvalidSigImg.isEmpty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.aInvalidSignatureImage = lcl_getGraphicFromString(rInvalidSigImg);
}
void XSecController::setSignatureLineId(const OUString& rSignatureLineId)
{
if (m_vInternalSignatureInformations.empty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.ouSignatureLineId = rSignatureLineId;
}
void XSecController::addEncapsulatedX509Certificate(const OUString& rEncapsulatedX509Certificate)
{
if (m_vInternalSignatureInformations.empty())
return;
if (rEncapsulatedX509Certificate.isEmpty())
return;
InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
rInformation.signatureInfor.maEncapsulatedX509Certificates.insert(rEncapsulatedX509Certificate);
}
void XSecController::setId( OUString const & ouId )
{
if (m_vInternalSignatureInformations.empty())
{
SAL_INFO("xmlsecurity.helper","XSecController::setId: no signature");
return;
}
InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
isi.signatureInfor.ouSignatureId = ouId;
}
/* public: for signature verify */
void XSecController::collectToVerify( std::u16string_view referenceId )
{
/* SAL_WARN_IF( !m_xSAXEventKeeper.is(), "xmlsecurity", "the SAXEventKeeper is NULL" ); */
if ( m_eStatusOfSecurityComponents != InitializationState::INITIALIZED )
/*
* if all security components are ready, verify the signature.
*/
return;
bool bJustChainingOn = false;
css::uno::Reference< css::xml::sax::XDocumentHandler > xHandler;
int i,j;
int sigNum = m_vInternalSignatureInformations.size();
for (i=0; i<sigNum; ++i)
{
InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
int refNum = vReferenceInfors.size();
for (j=0; j<refNum; ++j)
{
SignatureReferenceInformation &refInfor = vReferenceInfors[j];
if (refInfor.ouURI == referenceId)
{
if (chainOn())
{
bJustChainingOn = true;
xHandler = m_xSAXEventKeeper->setNextHandler(nullptr);
}
sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
css::xml::crypto::sax::ElementMarkPriority_BEFOREMODIFY, false );
css::uno::Reference<css::xml::crypto::sax::XReferenceCollector> xReferenceCollector
( isi.xReferenceResolvedListener, css::uno::UNO_QUERY );
m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
m_xSAXEventKeeper->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
xReferenceCollector->setReferenceId( nKeeperId );
isi.vKeeperIds[j] = nKeeperId;
break;
}
}
}
if ( bJustChainingOn )
{
m_xSAXEventKeeper->setNextHandler(xHandler);
}
}
void XSecController::addSignature( sal_Int32 nSignatureId )
{
SAL_WARN_IF( !m_xSecParser.is(), "xmlsecurity.helper", "No XSecParser initialized" );
m_nReservedSignatureId = nSignatureId;
m_bVerifyCurrentSignature = true;
}
css::uno::Reference< css::xml::sax::XDocumentHandler > const & XSecController::createSignatureReader(XMLSignatureHelper& rXMLSignatureHelper, sal_Int32 nType)
{
if (nType == embed::StorageFormats::OFOPXML)
m_xSecParser = new OOXMLSecParser(rXMLSignatureHelper, this);
else
m_xSecParser = new XSecParser(rXMLSignatureHelper, this);
css::uno::Reference< css::lang::XInitialization > xInitialization(m_xSecParser, uno::UNO_QUERY);
setSAXChainConnector(xInitialization);
return m_xSecParser;
}
void XSecController::releaseSignatureReader()
{
clearSAXChainConnector( );
m_xSecParser.clear();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|