20#include <config_gpgme.h>
31#include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
32#include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
33#include <com/sun/star/uno/XComponentContext.hpp>
34#include <com/sun/star/xml/sax/XParser.hpp>
35#include <com/sun/star/xml/crypto/XXMLSignature.hpp>
38#include <rtl/ustrbuf.hxx>
49OUString getDigestURI(sal_Int32 nID)
53 case css::xml::crypto::DigestID::SHA1:
55 case css::xml::crypto::DigestID::SHA256:
57 case css::xml::crypto::DigestID::SHA512:
71 case css::xml::crypto::DigestID::SHA1:
74 case css::xml::crypto::DigestID::SHA256:
77 case css::xml::crypto::DigestID::SHA512:
90 case css::xml::crypto::DigestID::SHA1:
92 case css::xml::crypto::DigestID::SHA256:
94 case css::xml::crypto::DigestID::SHA512:
104 , m_nNextSecurityId(1)
105 , m_bIsPreviousNodeInitializable(false)
106 , m_bIsSAXEventKeeperConnected(false)
107 , m_bIsCollectingElement(false)
108 , m_bIsBlocking(false)
110 , m_bIsSAXEventKeeperSticky(false)
111 , m_nReservedSignatureId(0)
112 , m_bVerifyCurrentSignature(false)
143 int size = m_vInternalSignatureInformations.size();
147 if (m_vInternalSignatureInformations[
i].signatureInfor.nSecurityId == nSecurityId)
177 css::uno::Reference< css::lang::XMultiComponentFactory > xMCF(
mxCtx->getServiceManager() );
179#if HAVE_FEATURE_GPGME
180 uno::Reference< lang::XServiceInfo > xServiceInfo(
m_xSecurityContext, css::uno::UNO_QUERY );
181 if (xServiceInfo->getImplementationName() ==
"com.sun.star.xml.security.gpg.XMLSecurityContext_GpgImpl")
185 m_xXMLSignature.set(xMCF->createInstanceWithContext(
"com.sun.star.xml.crypto.XMLSignature",
mxCtx), css::uno::UNO_QUERY);
206 css::uno::Sequence <css::uno::Any> arg{ css::uno::Any(
210 css::uno::Reference< css::xml::crypto::sax::XSAXEventKeeperStatusChangeListener >
211 xStatusChangeListener =
this;
213 m_xSAXEventKeeper->addSAXEventKeeperStatusChangeListener( xStatusChangeListener );
272 css::uno::Reference< css::xml::sax::XDocumentHandler > xSEKHandler(
m_xSAXEventKeeper);
281 css::uno::Reference< css::lang::XInitialization > xInitialization
284 xInitialization->initialize({ css::uno::Any(xSEKHandler) });
288 css::uno::Reference< css::xml::sax::XParser > xParser
290 xParser->setDocumentHandler( xSEKHandler );
327 css::uno::Reference< css::lang::XInitialization > xInitialization
330 css::uno::Sequence<css::uno::Any> aArgs{ css::uno::Any(
331 uno::Reference<xml::sax::XDocumentHandler>()) };
332 xInitialization->initialize(aArgs);
337 xParser->setDocumentHandler(uno::Reference<xml::sax::XDocumentHandler>());
391css::uno::Reference< css::io::XInputStream >
408 css::uno::Reference< css::io::XInputStream > xObjectInputStream;
412 xObjectInputStream =
m_xUriBinding->getUriBinding(objectURL);
414 return xObjectInputStream;
505 css::uno::Reference< css::xml::crypto::sax::XMissionTaker > xMissionTaker
512 xMissionTaker->endMission();
528void writeUnsignedProperties(
529 const css::uno::Reference<css::xml::sax::XDocumentHandler>& xDocumentHandler,
534 pAttributeList->AddAttribute(
"Id",
"idUnsignedProperties_" + signatureInfo.
ouSignatureId);
535 xDocumentHandler->startElement(
"xd:UnsignedProperties", uno::Reference<xml::sax::XAttributeList>(pAttributeList));
539 xDocumentHandler->startElement(
"xd:UnsignedSignatureProperties", uno::Reference<xml::sax::XAttributeList>(
new comphelper::AttributeList()));
542 xDocumentHandler->startElement(
"xd:CertificateValues", uno::Reference<xml::sax::XAttributeList>(
new comphelper::AttributeList()));
547 xDocumentHandler->startElement(
"xd:EncapsulatedX509Certificate", uno::Reference<xml::sax::XAttributeList>(
new comphelper::AttributeList()));
548 xDocumentHandler->characters(i);
549 xDocumentHandler->endElement(
"xd:EncapsulatedX509Certificate");
553 xDocumentHandler->endElement(
"xd:CertificateValues");
556 xDocumentHandler->endElement(
"xd:UnsignedSignatureProperties");
559 xDocumentHandler->endElement(
"xd:UnsignedProperties");
565 const css::uno::Reference<css::xml::sax::XDocumentHandler>& xDocumentHandler,
567 bool bXAdESCompliantIfODF )
588 pAttributeList->AddAttribute(
594 pAttributeList->AddAttribute(
599 xDocumentHandler->startElement(
"Signature", pAttributeList);
602 xDocumentHandler->startElement(
608 pAttributeList->AddAttribute(
611 xDocumentHandler->startElement(
"CanonicalizationMethod", pAttributeList );
612 xDocumentHandler->endElement(
"CanonicalizationMethod" );
622 pAttributeList->AddAttribute(
624 getSignatureURI(signatureInfo.
eAlgorithmID, vReferenceInfors[0].nDigestID));
625 xDocumentHandler->startElement(
"SignatureMethod", pAttributeList );
626 xDocumentHandler->endElement(
"SignatureMethod" );
630 int refNum = vReferenceInfors.size();
632 for(j=0; j<refNum; ++j)
637 if ( refInfor.
nType != SignatureReferenceType::SAMEDOCUMENT )
642 pAttributeList->AddAttribute(
651 if (refInfor.
ouURI.startsWith(
"idSignedProperties"))
653 pAttributeList->AddAttribute(
"URI",
"#idSignedProperties_" + signatureInfo.
ouSignatureId);
654 if (bXAdESCompliantIfODF && !refInfor.
ouType.isEmpty())
658 pAttributeList->AddAttribute(
"Type", refInfor.
ouType);
663 pAttributeList->AddAttribute(
665 "#" + refInfor.
ouURI);
669 xDocumentHandler->startElement(
"Reference", pAttributeList );
672 if (refInfor.
nType == SignatureReferenceType::XMLSTREAM)
677 xDocumentHandler->startElement(
682 pAttributeList->AddAttribute(
685 xDocumentHandler->startElement(
688 xDocumentHandler->endElement(
"Transform" );
690 xDocumentHandler->endElement(
"Transforms" );
695 pAttributeList->AddAttribute(
698 xDocumentHandler->startElement(
701 xDocumentHandler->endElement(
"DigestMethod" );
704 xDocumentHandler->startElement(
708 xDocumentHandler->endElement(
"DigestValue" );
710 xDocumentHandler->endElement(
"Reference" );
713 xDocumentHandler->endElement(
"SignedInfo" );
716 xDocumentHandler->startElement(
720 xDocumentHandler->endElement(
"SignatureValue" );
723 xDocumentHandler->startElement(
731 pAttributeList->AddAttribute(
"xmlns:loext",
NS_LOEXT);
733 xDocumentHandler->startElement(
738 xDocumentHandler->startElement(
741 xDocumentHandler->characters(signatureInfo.
ouGpgKeyID);
742 xDocumentHandler->endElement(
"PGPKeyID" );
747 xDocumentHandler->startElement(
751 xDocumentHandler->endElement(
"PGPKeyPacket" );
755 xDocumentHandler->startElement(
758 xDocumentHandler->characters( signatureInfo.
ouGpgOwner );
759 xDocumentHandler->endElement(
"loext:PGPOwner" );
761 xDocumentHandler->endElement(
"PGPData" );
766 for (
auto const& rData : signatureInfo.
X509Datas)
769 xDocumentHandler->startElement(
773 for (
auto const& it : rData)
776 xDocumentHandler->startElement(
781 xDocumentHandler->startElement(
784 xDocumentHandler->characters(it.X509IssuerName);
785 xDocumentHandler->endElement(
"X509IssuerName" );
788 xDocumentHandler->startElement(
791 xDocumentHandler->characters(it.X509SerialNumber);
792 xDocumentHandler->endElement(
"X509SerialNumber" );
794 xDocumentHandler->endElement(
"X509IssuerSerial" );
797 if (!it.X509Certificate.isEmpty())
799 xDocumentHandler->startElement(
802 xDocumentHandler->characters(it.X509Certificate);
803 xDocumentHandler->endElement(
"X509Certificate" );
807 xDocumentHandler->endElement(
"X509Data" );
811 xDocumentHandler->endElement(
"KeyInfo" );
816 xDocumentHandler->startElement(
821 xDocumentHandler->startElement(
822 "SignatureProperties",
827 pAttributeList->AddAttribute(
830 pAttributeList->AddAttribute(
833 xDocumentHandler->startElement(
840 pAttributeList->AddAttribute(
844 xDocumentHandler->startElement(
848 OUStringBuffer buffer;
860 buffer.replace(
',',
'.');
862 sDate = buffer.makeStringAndClear();
863 xDocumentHandler->characters( sDate );
865 xDocumentHandler->endElement(
868 xDocumentHandler->endElement(
"SignatureProperty" );
877 pAttributeList->AddAttribute(
"Target",
"#" + signatureInfo.
ouSignatureId);
878 xDocumentHandler->startElement(
"SignatureProperty", pAttributeList);
883 pAttributeList->AddAttribute(
"xmlns:dc",
NS_DC);
885 xDocumentHandler->startElement(
"dc:description", pAttributeList);
887 xDocumentHandler->endElement(
"dc:description");
890 xDocumentHandler->endElement(
"SignatureProperty");
893 xDocumentHandler->endElement(
"SignatureProperties" );
895 xDocumentHandler->endElement(
"Object" );
898 if (bXAdESCompliantIfODF)
901 pAttributeList->AddAttribute(
"xmlns:xd",
NS_XD);
902 xDocumentHandler->startElement(
907 pAttributeList->AddAttribute(
"Target",
"#" + signatureInfo.
ouSignatureId);
908 xDocumentHandler->startElement(
909 "xd:QualifyingProperties",
912 writeUnsignedProperties(xDocumentHandler, signatureInfo);
913 xDocumentHandler->endElement(
"xd:QualifyingProperties" );
915 xDocumentHandler->endElement(
"Object" );
918 xDocumentHandler->endElement(
"Signature" );
928 std::vector<SignatureInformation::X509Data> && rDatas)
939 SAL_WARN_IF(
nIndex == -1,
"xmlsecurity.helper",
"getSignatureInformation - SecurityId is invalid!" );
952 for (
int i=0;
i<sigNum; ++
i)
955 vInfors.push_back(si);
989 assert(
index != -1 &&
"Signature Not Found!");
991 signatureInfor.
nStatus = nResult;
1000 assert(
index != -1 &&
"Signature Not Found!");
1002 signatureInfor.
nStatus = nResult;
Writes a single OOXML digital signature.
NAME XMLDocumentWrapper_XmlSecImpl – Class to manipulate a libxml2 document.
bool m_bIsSAXEventKeeperSticky
rtl::Reference< SAXEventKeeperImpl > m_xSAXEventKeeper
enum XSecController::InitializationState m_eStatusOfSecurityComponents
virtual void SAL_CALL collectionStatusChanged(sal_Bool isInsideCollectedElement) override
css::uno::Reference< css::uno::XInterface > m_xPreviousNodeOnSAXChain
css::uno::Reference< css::xml::crypto::XXMLSecurityContext > m_xSecurityContext
css::uno::Reference< css::xml::crypto::XXMLSignature > m_xXMLSignature
bool m_bIsCollectingElement
css::uno::Reference< css::uno::XComponentContext > mxCtx
std::vector< InternalSignatureInformation > m_vInternalSignatureInformations
void createXSecComponent()
void clearSAXChainConnector()
bool m_bIsPreviousNodeInitializable
virtual void SAL_CALL blockingStatusChanged(sal_Bool isBlocking) override
void exportOOXMLSignature(const css::uno::Reference< css::embed::XStorage > &xRootStorage, const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocumentHandler, const SignatureInformation &rInformation)
Exports an OOXML signature, called by WriteOOXMLSignature().
virtual void SAL_CALL signatureVerified(sal_Int32 securityId, css::xml::crypto::SecurityOperationStatus nResult) override
virtual void SAL_CALL bufferStatusChanged(sal_Bool isBufferEmpty) override
SignatureInformation getSignatureInformation(sal_Int32 nSecurityId) const
bool m_bVerifyCurrentSignature
void checkChainingStatus()
virtual void SAL_CALL signatureCreated(sal_Int32 securityId, css::xml::crypto::SecurityOperationStatus nResult) override
void startMission(const rtl::Reference< UriBindingHelper > &xUriBinding, const css::uno::Reference< css::xml::crypto::XXMLSecurityContext > &xSecurityContext)
static void exportSignature(const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocumentHandler, const SignatureInformation &signatureInfo, bool bXAdESCompliantIfODF)
void setSAXChainConnector(const css::uno::Reference< css::lang::XInitialization > &xInitialization)
void UpdateSignatureInformation(sal_Int32 nSecurityId, std::vector< SignatureInformation::X509Data > &&rDatas)
only verify can figure out which X509Data is the signing certificate
sal_Int32 getNewSecurityId()
XSecController(css::uno::Reference< css::uno::XComponentContext > xCtx)
bool m_bIsSAXEventKeeperConnected
virtual ~XSecController() override
rtl::Reference< XMLDocumentWrapper_XmlSecImpl > m_xXMLDocumentWrapper
rtl::Reference< UriBindingHelper > m_xUriBinding
css::uno::Reference< css::io::XInputStream > getObjectInputStream(const OUString &objectURL)
int findSignatureInfor(sal_Int32 nSecurityId) const
void initializeSAXChain()
SignatureInformations getSignatureInformations() const
sal_Int32 m_nNextSecurityId
Reference< XComponentContext > mxCtx
#define SAL_WARN_IF(condition, area, stream)
void writeSignedProperties(const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocumentHandler, const SignatureInformation &signatureInfo, const OUString &sDate, const bool bWriteSignatureLineData)
OUString toISO8601(const css::util::DateTime &rDateTime)
::std::vector< SignatureReferenceInformation > SignatureReferenceInformations
::std::vector< SignatureInformation > SignatureInformations
constexpr OUStringLiteral ALGO_XMLDSIGSHA512
constexpr OUStringLiteral NS_XMLDSIG
constexpr OUStringLiteral ALGO_RSASHA1
constexpr OUStringLiteral NS_XD
constexpr OUStringLiteral ALGO_XMLDSIGSHA256
constexpr OUStringLiteral ALGO_ECDSASHA512
constexpr OUStringLiteral NS_LOEXT
constexpr OUStringLiteral NS_DC
constexpr OUStringLiteral ALGO_RSASHA256
constexpr OUStringLiteral ALGO_ECDSASHA1
constexpr OUStringLiteral ALGO_C14N
constexpr OUStringLiteral ALGO_XMLDSIGSHA1
constexpr OUStringLiteral ALGO_ECDSASHA256
constexpr OUStringLiteral ALGO_RSASHA512