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
|
/* -*- 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 "xsecctl.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/XReferenceResolvedBroadcaster.hpp>
#include <com/sun/star/xml/crypto/sax/XBlockerMonitor.hpp>
#include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
#include <com/sun/star/xml/crypto/sax/XSignatureCreationResultBroadcaster.hpp>
#include <com/sun/star/io/XActiveDataSource.hpp>
#include <com/sun/star/embed/StorageFormats.hpp>
#include <rtl/uuid.h>
#include "framework/signaturecreatorimpl.hxx"
#include "framework/saxeventkeeperimpl.hxx"
using namespace com::sun::star;
namespace cssu = com::sun::star::uno;
namespace cssl = com::sun::star::lang;
namespace cssxc = com::sun::star::xml::crypto;
namespace cssxs = com::sun::star::xml::sax;
/* protected: for signature generation */
OUString XSecController::createId()
{
sal_uInt8 aSeq[16];
rtl_createUuid( aSeq, nullptr, true );
char str[68]="ID_";
int length = 3;
for (sal_uInt8 i : aSeq)
{
length += sprintf(str+length, "%04x", i);
}
return OUString::createFromAscii(str);
}
cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToWrite(
InternalSignatureInformation& internalSignatureInfor,
sal_Int32 nStorageFormat,
bool bXAdESCompliantIfODF)
{
sal_Int32 nSecurityId = internalSignatureInfor.signatureInfor.nSecurityId;
SignatureReferenceInformations& vReferenceInfors = internalSignatureInfor.signatureInfor.vSignatureReferenceInfors;
sal_Int32 nIdOfSignatureElementCollector;
nIdOfSignatureElementCollector =
m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_AFTERMODIFY, true );
m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
uno::Reference<xml::crypto::sax::XReferenceResolvedListener> xReferenceResolvedListener(new SignatureCreatorImpl);
cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
cssu::Sequence<cssu::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);
//for nss, the internal module is used for signing, which needs to be improved later
args[3] <<= m_xSecurityContext->getSecurityEnvironment();
args[4] <<= m_xXMLSignature;
xInitialization->initialize(args);
sal_Int32 nBlockerId = m_xSAXEventKeeper->addBlocker();
m_xSAXEventKeeper->setSecurityId(nBlockerId, nSecurityId);
cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(xReferenceResolvedListener, cssu::UNO_QUERY);
xBlockerMonitor->setBlockerId(nBlockerId);
cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster >
xSignatureCreationResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
xSignatureCreationResultBroadcaster->addSignatureCreationResultListener( this );
m_xSAXEventKeeper->addReferenceResolvedListener(
nIdOfSignatureElementCollector,
xReferenceResolvedListener);
cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
(xReferenceResolvedListener, cssu::UNO_QUERY);
int i;
int size = vReferenceInfors.size();
sal_Int32 nReferenceCount = 0;
for(i=0; i<size; ++i)
{
sal_Int32 keeperId = internalSignatureInfor.vKeeperIds[i];
if ( keeperId != -1)
{
m_xSAXEventKeeper->setSecurityId(keeperId, nSecurityId);
m_xSAXEventKeeper->addReferenceResolvedListener( keeperId, xReferenceResolvedListener);
xReferenceCollector->setReferenceId( keeperId );
nReferenceCount++;
}
}
xReferenceCollector->setReferenceCount( nReferenceCount );
/*
* adds all URI binding
*/
cssu::Reference<cssxc::XUriBinding> xUriBinding
(xReferenceResolvedListener, cssu::UNO_QUERY);
for(i=0; i<size; ++i)
{
const SignatureReferenceInformation& refInfor = vReferenceInfors[i];
cssu::Reference< css::io::XInputStream > xInputStream
= getObjectInputStream( refInfor.ouURI );
if (xInputStream.is())
{
xUriBinding->setUriBinding(refInfor.ouURI,xInputStream);
}
}
cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
keyCollector->setKeyId(0);
// use sha512 for gpg signing unconditionally
const sal_Int32 digestID = !internalSignatureInfor.signatureInfor.ouGpgCertificate.isEmpty()?
cssxc::DigestID::SHA512 : (bXAdESCompliantIfODF ? cssxc::DigestID::SHA256 : cssxc::DigestID::SHA1);
if (nStorageFormat != embed::StorageFormats::OFOPXML)
{
internalSignatureInfor.signatureInfor.ouSignatureId = createId();
internalSignatureInfor.signatureInfor.ouPropertyId = createId();
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouPropertyId, -1 );
size++;
if (bXAdESCompliantIfODF)
{
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, "idSignedProperties", -1);
size++;
}
if (!internalSignatureInfor.signatureInfor.ouDescription.isEmpty())
{
// Only mention the hash of the description in the signature if it's non-empty.
internalSignatureInfor.signatureInfor.ouDescriptionPropertyId = createId();
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouDescriptionPropertyId, -1);
size++;
}
}
else
{
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, "idPackageObject", -1);
size++;
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, "idOfficeObject", -1);
size++;
internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, "idSignedProperties", -1);
size++;
}
/*
* replace both digestValues and signatureValue to " "
*/
for(i=0; i<size; ++i)
{
SignatureReferenceInformation& refInfor = vReferenceInfors[i];
refInfor.ouDigestValue = " ";
}
internalSignatureInfor.signatureInfor.ouSignatureValue = " ";
return xReferenceResolvedListener;
}
void XSecController::signAStream( sal_Int32 securityId, const OUString& uri, bool isBinary, bool bXAdESCompliantIfODF)
{
const SignatureReferenceType type = isBinary ? SignatureReferenceType::BINARYSTREAM : SignatureReferenceType::XMLSTREAM;
sal_Int32 digestID = bXAdESCompliantIfODF ? cssxc::DigestID::SHA256 : cssxc::DigestID::SHA1;
int index = findSignatureInfor( securityId );
if (index == -1)
{
InternalSignatureInformation isi(securityId, nullptr);
isi.addReference(type, digestID, uri, -1);
m_vInternalSignatureInformations.push_back( isi );
}
else
{
// use sha512 for gpg signing unconditionally
if (!m_vInternalSignatureInformations[index].signatureInfor.ouGpgCertificate.isEmpty())
digestID = cssxc::DigestID::SHA512;
m_vInternalSignatureInformations[index].addReference(type, digestID, uri, -1);
}
}
void XSecController::setX509Certificate(
sal_Int32 nSecurityId,
const OUString& ouX509IssuerName,
const OUString& ouX509SerialNumber,
const OUString& ouX509Cert,
const OUString& ouX509CertDigest)
{
int index = findSignatureInfor( nSecurityId );
if ( index == -1 )
{
InternalSignatureInformation isi(nSecurityId, nullptr);
isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
isi.signatureInfor.ouX509Certificate = ouX509Cert;
isi.signatureInfor.ouCertDigest = ouX509CertDigest;
m_vInternalSignatureInformations.push_back( isi );
}
else
{
SignatureInformation &si
= m_vInternalSignatureInformations[index].signatureInfor;
si.ouX509IssuerName = ouX509IssuerName;
si.ouX509SerialNumber = ouX509SerialNumber;
si.ouX509Certificate = ouX509Cert;
si.ouCertDigest = ouX509CertDigest;
}
}
void XSecController::setGpgCertificate(
sal_Int32 nSecurityId,
const OUString& ouCertDigest,
const OUString& ouCert,
const OUString& ouOwner)
{
int index = findSignatureInfor( nSecurityId );
if ( index == -1 )
{
InternalSignatureInformation isi(nSecurityId, nullptr);
isi.signatureInfor.ouGpgCertificate = ouCert;
isi.signatureInfor.ouGpgOwner = ouOwner;
isi.signatureInfor.ouCertDigest = ouCertDigest;
m_vInternalSignatureInformations.push_back( isi );
}
else
{
SignatureInformation &si
= m_vInternalSignatureInformations[index].signatureInfor;
si.ouGpgCertificate = ouCert;
si.ouGpgOwner = ouOwner;
si.ouCertDigest = ouCertDigest;
}
}
void XSecController::setDate(
sal_Int32 nSecurityId,
const css::util::DateTime& rDateTime )
{
int index = findSignatureInfor( nSecurityId );
if ( index == -1 )
{
InternalSignatureInformation isi(nSecurityId, nullptr);
isi.signatureInfor.stDateTime = rDateTime;
m_vInternalSignatureInformations.push_back( isi );
}
else
{
SignatureInformation &si
= m_vInternalSignatureInformations[index].signatureInfor;
si.stDateTime = rDateTime;
}
}
void XSecController::setDescription(sal_Int32 nSecurityId, const OUString& rDescription)
{
int nIndex = findSignatureInfor(nSecurityId);
if (nIndex == -1)
{
InternalSignatureInformation aInformation(nSecurityId, nullptr);
aInformation.signatureInfor.ouDescription = rDescription;
m_vInternalSignatureInformations.push_back(aInformation);
}
else
{
SignatureInformation& rInformation = m_vInternalSignatureInformations[nIndex].signatureInfor;
rInformation.ouDescription = rDescription;
}
}
bool XSecController::WriteSignature(
const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler,
bool bXAdESCompliantIfODF )
{
bool rc = false;
SAL_WARN_IF( !xDocumentHandler.is(), "xmlsecurity.helper", "I really need a document handler!" );
/*
* chain the SAXEventKeeper to the SAX chain
*/
chainOn(true);
if ( m_eStatusOfSecurityComponents == InitializationState::INITIALIZED )
/*
* if all security components are ready, add the signature
* stream.
*/
{
m_bIsSAXEventKeeperSticky = true;
m_xSAXEventKeeper->setNextHandler(xDocumentHandler);
try
{
/*
* export the signature template
*/
cssu::Reference<cssxs::XDocumentHandler> xSEKHandler(static_cast<cppu::OWeakObject*>(m_xSAXEventKeeper.get()),cssu::UNO_QUERY);
int i;
int sigNum = m_vInternalSignatureInformations.size();
for (i=0; i<sigNum; ++i)
{
InternalSignatureInformation &isi = m_vInternalSignatureInformations[i];
// Prepare the signature creator.
// 0 is not a documented value of embed::StorageFormats, ugh
isi.xReferenceResolvedListener = prepareSignatureToWrite( isi, 0, bXAdESCompliantIfODF );
exportSignature( xSEKHandler, isi.signatureInfor, bXAdESCompliantIfODF );
}
m_bIsSAXEventKeeperSticky = false;
chainOff();
rc = true;
}
catch( cssu::Exception& )
{
}
m_xSAXEventKeeper->setNextHandler( nullptr );
m_bIsSAXEventKeeperSticky = false;
}
return rc;
}
bool XSecController::WriteOOXMLSignature(const uno::Reference<embed::XStorage>& xRootStorage, const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler)
{
bool bRet = false;
SAL_WARN_IF(!xDocumentHandler.is(), "xmlsecurity.helper", "empty xDocumentHandler reference");
// Chain the SAXEventKeeper to the SAX chain.
chainOn(/*bRetrievingLastEvent=*/true);
if (m_eStatusOfSecurityComponents == InitializationState::INITIALIZED)
{
m_bIsSAXEventKeeperSticky = true;
m_xSAXEventKeeper->setNextHandler(xDocumentHandler);
try
{
// Export the signature template.
cssu::Reference<xml::sax::XDocumentHandler> xSEKHandler(static_cast<cppu::OWeakObject*>(m_xSAXEventKeeper.get()), uno::UNO_QUERY);
for (InternalSignatureInformation & rInformation : m_vInternalSignatureInformations)
{
// Prepare the signature creator.
rInformation.xReferenceResolvedListener = prepareSignatureToWrite(rInformation, embed::StorageFormats::OFOPXML, false);
exportOOXMLSignature(xRootStorage, xSEKHandler, rInformation.signatureInfor);
}
m_bIsSAXEventKeeperSticky = false;
chainOff();
bRet = true;
}
catch(const uno::Exception&)
{
}
m_xSAXEventKeeper->setNextHandler(nullptr);
m_bIsSAXEventKeeperSticky = false;
}
return bRet;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|