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
|
/* -*- 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 .
*/
#ifndef INCLUDED_XMLSECURITY_SOURCE_HELPER_XSECCTL_HXX
#define INCLUDED_XMLSECURITY_SOURCE_HELPER_XSECCTL_HXX
#include <xmlsecurity/sigstruct.hxx>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/XAttributeList.hpp>
#include <com/sun/star/xml/crypto/XXMLSignature.hpp>
#include <com/sun/star/xml/crypto/XSEInitializer.hpp>
#include <com/sun/star/xml/crypto/sax/XSecurityController.hpp>
#include <com/sun/star/xml/crypto/sax/XElementStackKeeper.hpp>
#include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
#include <com/sun/star/xml/crypto/sax/XReferenceResolvedListener.hpp>
#include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeListener.hpp>
#include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
#include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
#include <com/sun/star/beans/XFastPropertySet.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <rtl/ustrbuf.hxx>
#include <cppuhelper/implbase4.hxx>
#include <vector>
/*
* all error information
*/
#define ERROR_CANNOTCREATEXMLSECURITYCOMPONENT "Can't create XML security components."
#define ERROR_SAXEXCEPTIONDURINGCREATION "A SAX exception is throwed during signature creation."
#define ERROR_IOEXCEPTIONDURINGCREATION "An IO exception is throwed during signature creation."
#define ERROR_EXCEPTIONDURINGCREATION "An exception is throwed during signature creation."
/*
* all stringS in signature element
*/
#define TAG_SIGNATURE "Signature"
#define TAG_SIGNEDINFO "SignedInfo"
#define TAG_CANONICALIZATIONMETHOD "CanonicalizationMethod"
#define TAG_SIGNATUREMETHOD "SignatureMethod"
#define TAG_REFERENCE "Reference"
#define TAG_TRANSFORMS "Transforms"
#define TAG_TRANSFORM "Transform"
#define TAG_DIGESTMETHOD "DigestMethod"
#define TAG_DIGESTVALUE "DigestValue"
#define TAG_SIGNATUREVALUE "SignatureValue"
#define TAG_KEYINFO "KeyInfo"
#define TAG_X509DATA "X509Data"
#define TAG_X509ISSUERSERIAL "X509IssuerSerial"
#define TAG_X509ISSUERNAME "X509IssuerName"
#define TAG_X509SERIALNUMBER "X509SerialNumber"
#define TAG_X509CERTIFICATE "X509Certificate"
#define TAG_OBJECT "Object"
#define TAG_SIGNATUREPROPERTIES "SignatureProperties"
#define TAG_SIGNATUREPROPERTY "SignatureProperty"
#define TAG_TIMESTAMP "timestamp"
#define TAG_DATE "date"
//#define TAG_TIME "time"
#define ATTR_XMLNS "xmlns"
#define ATTR_ALGORITHM "Algorithm"
#define ATTR_URI "URI"
#define ATTR_ID "Id"
#define ATTR_TARGET "Target"
#define NSTAG_DC "dc"
#define NS_XMLDSIG "http://www.w3.org/2000/09/xmldsig#"
//#define NS_DATETIME "http://www.ietf.org/rfcXXXX.txt"
#define NS_DC "http://purl.org/dc/elements/1.1/"
#define ALGO_C14N "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
#define ALGO_RSASHA1 "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
#define ALGO_XMLDSIGSHA1 "http://www.w3.org/2000/09/xmldsig#sha1"
#define CHAR_FRAGMENT "#"
#define CHAR_BLANK " "
/*
* status of security related components
*/
#define UNINITIALIZED 0
#define INITIALIZED 1
#define FAILTOINITIALIZED 2
// forward declaration
class XSecParser;
class InternalSignatureInformation
{
public:
SignatureInformation signatureInfor;
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
xReferenceResolvedListener;
::std::vector< sal_Int32 > vKeeperIds;
InternalSignatureInformation(
sal_Int32 nId,
com::sun::star::uno::Reference< com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
xListener)
:signatureInfor(nId)
{
xReferenceResolvedListener = xListener;
}
void addReference( sal_Int32 type, const OUString& uri, sal_Int32 keeperId )
{
signatureInfor.vSignatureReferenceInfors.push_back(
SignatureReferenceInformation(type, uri));
vKeeperIds.push_back( keeperId );
}
};
typedef ::std::vector< InternalSignatureInformation > InternalSignatureInformations;
class XSecController : public cppu::WeakImplHelper4
<
com::sun::star::xml::crypto::sax::XSecurityController,
//com::sun::star::beans::XFastPropertySet,
com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener,
com::sun::star::xml::crypto::sax::XSignatureCreationResultListener,
com::sun::star::xml::crypto::sax::XSignatureVerifyResultListener
>
/****** XSecController.hxx/CLASS XSecController *******************************
*
* NAME
* XSecController -- the xml security framework controller
*
* FUNCTION
* Controlls the whole xml security framework to create signatures or to
* verify signatures.
*
* NOTES
* The XFastPropertySet interface is used to transfer common values to
* classes in other module, for instance, the signature id for all
* sessions is transferred to xmloff module through this interface.
*
* AUTHOR
* Michael Mi
* Email: michael.mi@sun.com
******************************************************************************/
{
friend class XSecParser;
private:
com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext> mxCtx;
/*
* used to buffer SAX events
*/
com::sun::star::uno::Reference<
com::sun::star::xml::wrapper::XXMLDocumentWrapper > m_xXMLDocumentWrapper;
/*
* the SAX events keeper
*/
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper > m_xSAXEventKeeper;
/*
* the bridge component which creates/verifies signature
*/
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::XXMLSignature > m_xXMLSignature;
/*
* the Security Context
*/
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::XXMLSecurityContext > m_xSecurityContext;
/*
* the security id incrementer, in order to make any security id unique
* to the SAXEventKeeper.
* Because each XSecController has its own SAXEventKeeper, so this variable
* is not necessary to be static.
*/
sal_Int32 m_nNextSecurityId;
/*
* Signature information
*/
InternalSignatureInformations m_vInternalSignatureInformations;
/*
* the previous node on the SAX chain.
* The reason that use a Reference<XInterface> type variable
* is that the previous components are different when exporting
* and importing, and there is no other common interface they
* can provided.
*/
com::sun::star::uno::Reference<
com::sun::star::uno::XInterface > m_xPreviousNodeOnSAXChain;
/*
* whether the preivous node can provide an XInitiazlize interface,
* use this variable in order to typecast the XInterface to the
* correct interface type.
*/
bool m_bIsPreviousNodeInitializable;
/*
* the next node on the SAX chain.
* it can always provide an XDocumentHandler interface.
*/
com::sun::star::uno::Reference<
com::sun::star::xml::sax::XDocumentHandler > m_xNextNodeOnSAXChain;
/*
* the ElementStackKeeper is used to reserve the key SAX events.
* when the SAXEventKeeper is chained on the SAX chain, it need
* first get all missed key SAX events in order to make sure the
* DOM tree it buffering has the same structure with the original
* document.
*
* For a given section of a SAX event stream, the key SAX events
* are the minimal SAX event subset of that section, which,
* combining with SAX events outside of this section, has the same
* structure with the original document.
*
* For example, sees the following dom fragment:
* <A>
* <B/>
* <C>
* <D>
* <E/>
* </D>
* </C>
* </A>
*
* If we consider the SAX event section from startElement(<A>) to
* startElement(<D>), then the key SAX events are:
*
* startElement(<A>), startElement(<C>), startElement(<D>)
*
* The startElement(<B>) and endElement(<B>) is ignored, because
* they are unimportant for the tree structure in this section.
*
* If we consider the SAX event section from startElement(<D>) to
* endElement(<A>), the key SAX events are:
*
* startElement(<D>), endElement(<D>), endElement(<C>),
* endElement(<A>).
*/
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XElementStackKeeper > m_xElementStackKeeper;
/*
* a flag representing whether the SAXEventKeeper is now on the
* SAX chain.
*/
bool m_bIsSAXEventKeeperConnected;
/*
* a flag representing whether it is collecting some element,
* which means that the SAXEventKeeper can't be chained off the
* SAX chain.
*/
bool m_bIsCollectingElement;
/*
* a flag representing whether the SAX event stream is blocking,
* which also means that the SAXEventKeeper can't be chained off
* the SAX chain.
*/
bool m_bIsBlocking;
/*
* a flag representing the current status of security related
* components.
*/
sal_Int32 m_nStatusOfSecurityComponents;
/*
* a flag representing whether the SAXEventKeeper need to be
* on the SAX chain all the time.
* This flag is used to the situation when creating signature.
*/
bool m_bIsSAXEventKeeperSticky;
/*
* fast property vector
*/
std::vector< sal_Int32 > m_vFastPropertyIndexs;
std::vector< com::sun::star::uno::Any > m_vFastPropertyValues;
/*
* error message pointer
*/
const char *m_pErrorMessage;
/*
* the XSecParser which is used to parse the signature stream
*/
XSecParser *m_pXSecParser;
/*
* the caller assigned signature id for the next signature in the
* signature stream
*/
sal_Int32 m_nReservedSignatureId;
/*
* representing whether to verify the current signature
*/
bool m_bVerifyCurrentSignature;
public:
/*
* An xUriBinding is provided to map Uris to XInputStream interfaces.
*/
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::XUriBinding > m_xUriBinding;
private:
/*
* Common methods
*/
void createXSecComponent( );
int findSignatureInfor( sal_Int32 nSecurityId ) const;
bool chainOn( bool bRetrievingLastEvent );
void chainOff();
void checkChainingStatus();
void initializeSAXChain();
com::sun::star::uno::Reference<
com::sun::star::io::XInputStream > getObjectInputStream( const OUString& objectURL );
//sal_Int32 getFastPropertyIndex(sal_Int32 nHandle) const;
/*
* For signature generation
*/
OUString createId();
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XReferenceResolvedListener > prepareSignatureToWrite(
InternalSignatureInformation& signatureInfo );
/*
* For signature verification
*/
void addSignature();
void addReference( const OUString& ouUri);
void addStreamReference(
const OUString& ouUri,
bool isBinary );
void setReferenceCount() const;
void setX509IssuerName( OUString& ouX509IssuerName );
void setX509SerialNumber( OUString& ouX509SerialNumber );
void setX509Certificate( OUString& ouX509Certificate );
void setSignatureValue( OUString& ouSignatureValue );
void setDigestValue( OUString& ouDigestValue );
void setDate( OUString& ouDate );
void setId( OUString& ouId );
void setPropertyId( OUString& ouPropertyId );
com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XReferenceResolvedListener > prepareSignatureToRead(
sal_Int32 nSecurityId );
public:
XSecController(const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext>& rxCtx);
virtual ~XSecController();
sal_Int32 getNewSecurityId( );
void startMission( const com::sun::star::uno::Reference<
com::sun::star::xml::crypto::XUriBinding >& xUriBinding,
const com::sun::star::uno::Reference<
com::sun::star::xml::crypto::XXMLSecurityContext >& xSecurityContext );
void setSAXChainConnector(
const com::sun::star::uno::Reference<
com::sun::star::lang::XInitialization >& xInitialization,
const com::sun::star::uno::Reference<
com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler,
const com::sun::star::uno::Reference<
com::sun::star::xml::crypto::sax::XElementStackKeeper >& xElementStackKeeper);
void clearSAXChainConnector();
void endMission();
SignatureInformation getSignatureInformation( sal_Int32 nSecurityId ) const;
SignatureInformations getSignatureInformations() const;
void exportSignature(
const com::sun::star::uno::Reference<
com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler,
const SignatureInformation& signatureInfo );
/*
* For signature generation
*/
void signAStream( sal_Int32 securityId, const OUString& uri, const OUString& objectURL, bool isBinary);
/** sets data that describes the certificate.
It is absolutely necessary that the parameter ouX509IssuerName is set. It contains
the base64 encoded certificate, which is DER encoded. The XMLSec needs it to find
the private key. Although issuer name and certificate should be sufficient to identify
the certificate the implementation in XMLSec is broken, both for Windows and mozilla.
The reason is that they use functions to find the certificate which take as parameter
the DER encoded ASN.1 issuer name. The issuer name is a DName, where most attributes
are of type DirectoryName, which is a choice of 5 string types. This information is
not contained in the issuer string and while it is converted to the ASN.1 name the
conversion function must assume a particular type, which is often wrong. For example,
the Windows function CertStrToName will use a T.61 string if the string does not contain
special characters. So if the certificate uses simple characters but encodes the
issuer attributes in Utf8, then CertStrToName will use T.61. The resulting DER encoded
ASN.1 name now contains different bytes which indicate the string type. The functions
for finding the certificate apparently use memcmp - hence they fail to find the
certificate.
*/
void setX509Certificate(
sal_Int32 nSecurityId,
const OUString& ouX509IssuerName,
const OUString& ouX509SerialNumber,
const OUString& ouX509Cert);
// see the other setX509Certifcate function
void setX509Certificate(
sal_Int32 nSecurityId,
const sal_Int32 nSecurityEnvironmentIndex,
const OUString& ouX509IssuerName,
const OUString& ouX509SerialNumber,
const OUString& ouX509Cert);
void setDate(
sal_Int32 nSecurityId,
const ::com::sun::star::util::DateTime& rDateTime );
bool WriteSignature(
const com::sun::star::uno::Reference<
com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler );
/*
* For signature verification
*/
void collectToVerify( const OUString& referenceId );
void addSignature( sal_Int32 nSignatureId );
com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler > createSignatureReader();
void releaseSignatureReader();
public:
/* Interface methods */
/*
* XSecurityController
*
* no method in XSecurityController interface
*/
/*
* XFastPropertySet
*/
/*
* XSAXEventKeeperStatusChangeListener
*/
virtual void SAL_CALL blockingStatusChanged( sal_Bool isBlocking )
throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual void SAL_CALL collectionStatusChanged(
sal_Bool isInsideCollectedElement )
throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual void SAL_CALL bufferStatusChanged( sal_Bool isBufferEmpty )
throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
/*
* XSignatureCreationResultListener
*/
virtual void SAL_CALL signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
/*
* XSignatureVerifyResultListener
*/
virtual void SAL_CALL signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|