From c3f8702241b625db994bcb059d8c91c25fd43e53 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 25 Jun 2020 14:32:11 +0200 Subject: sd signature line: pass the model down to xmlsecurity So it can avoid SfxObjectShell::Current(), which is only correct when a single document is open. Also add an sfx2::DigitalSignatures interface so this can be done without UNO API changes. Change-Id: Ie81996b8f1e8851975b27c43a53f9d23e316004e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/97116 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- xmlsecurity/inc/documentsignaturemanager.hxx | 6 +++ xmlsecurity/inc/pdfsignaturehelper.hxx | 7 ++- .../source/component/documentdigitalsignatures.cxx | 51 +++++++++++++++++----- .../source/helper/documentsignaturemanager.cxx | 8 +++- xmlsecurity/source/helper/pdfsignaturehelper.cxx | 15 +++---- 5 files changed, 63 insertions(+), 24 deletions(-) (limited to 'xmlsecurity') diff --git a/xmlsecurity/inc/documentsignaturemanager.hxx b/xmlsecurity/inc/documentsignaturemanager.hxx index 3d001966f3b0..e7149c3fe195 100644 --- a/xmlsecurity/inc/documentsignaturemanager.hxx +++ b/xmlsecurity/inc/documentsignaturemanager.hxx @@ -44,6 +44,10 @@ namespace embed { class XStorage; } +namespace frame +{ +class XModel; +} namespace graphic { class XGraphic; @@ -69,6 +73,7 @@ private: DocumentSignatureMode const meSignatureMode; css::uno::Sequence> m_manifest; css::uno::Reference mxSignatureStream; + css::uno::Reference mxModel; css::uno::Reference mxTempSignatureStream; /// Storage containing all OOXML signatures, unused for ODF. css::uno::Reference mxTempSignatureStorage; @@ -125,6 +130,7 @@ public: { mxSignatureStream = xSignatureStream; } + void setModel(const css::uno::Reference& xModel); const css::uno::Reference& getStore() const { return mxStore; } DocumentSignatureMode getSignatureMode() const { return meSignatureMode; } SignatureInformations& getCurrentSignatureInformations() diff --git a/xmlsecurity/inc/pdfsignaturehelper.hxx b/xmlsecurity/inc/pdfsignaturehelper.hxx index f23a371d2820..f8f1e893beb2 100644 --- a/xmlsecurity/inc/pdfsignaturehelper.hxx +++ b/xmlsecurity/inc/pdfsignaturehelper.hxx @@ -21,6 +21,10 @@ namespace sun { namespace star { +namespace frame +{ +class XModel; +} namespace io { class XInputStream; @@ -67,7 +71,8 @@ public: /// Comment / reason to be used next time signing is performed. void SetDescription(const OUString& rDescription); /// Append a new signature at the end of xInputStream. - bool Sign(const css::uno::Reference& xInputStream, bool bAdES); + bool Sign(const css::uno::Reference& xModel, + const css::uno::Reference& xInputStream, bool bAdES); /// Remove the signature at nPosition (and all dependent signatures) from xInputStream. static bool RemoveSignature(const css::uno::Reference& xInputStream, sal_uInt16 nPosition); diff --git a/xmlsecurity/source/component/documentdigitalsignatures.cxx b/xmlsecurity/source/component/documentdigitalsignatures.cxx index d149bf5e30b2..3baab59e03ac 100644 --- a/xmlsecurity/source/component/documentdigitalsignatures.cxx +++ b/xmlsecurity/source/component/documentdigitalsignatures.cxx @@ -57,6 +57,7 @@ #include #include #include +#include #include @@ -67,10 +68,10 @@ using namespace css::security; using namespace css::xml::crypto; namespace { - class DocumentDigitalSignatures : public cppu::WeakImplHelper + css::lang::XInitialization, css::lang::XServiceInfo>, + public sfx2::DigitalSignatures { private: css::uno::Reference mxCtx; @@ -103,12 +104,14 @@ private: chooseCertificatesImpl(std::map& rProperties, const UserAction eAction, const CertificateKind certificateKind=CertificateKind_NONE); - bool signWithCertificateImpl( - css::uno::Reference const& xCertificate, - css::uno::Reference const& xStorage, - css::uno::Reference const& xStream, DocumentSignatureMode eMode); + bool + signWithCertificateImpl(const uno::Reference& /*xModel*/, + css::uno::Reference const& xCertificate, + css::uno::Reference const& xStorage, + css::uno::Reference const& xStream, + DocumentSignatureMode eMode); - public: +public: explicit DocumentDigitalSignatures( const css::uno::Reference& rxCtx); @@ -205,6 +208,13 @@ private: { mxParentWindow = rParentwindow; } + + /// See sfx2::DigitalSignatures::SignModelWithCertificate(). + bool + SignModelWithCertificate(const css::uno::Reference& xModel, + const css::uno::Reference& xCertificate, + const css::uno::Reference& xStorage, + const css::uno::Reference& xStream) override; }; } @@ -785,7 +795,19 @@ sal_Bool DocumentDigitalSignatures::signDocumentWithCertificate( css::uno::Reference const & xStorage, css::uno::Reference const & xStream) { - return signWithCertificateImpl(xCertificate, xStorage, xStream, DocumentSignatureMode::Content); + uno::Reference xModel; + return signWithCertificateImpl(xModel, xCertificate, xStorage, xStream, + DocumentSignatureMode::Content); +} + +bool DocumentDigitalSignatures::SignModelWithCertificate( + const uno::Reference& xModel, + const css::uno::Reference& xCertificate, + const css::uno::Reference& xStorage, + const css::uno::Reference& xStream) +{ + return signWithCertificateImpl(xModel, xCertificate, xStorage, xStream, + DocumentSignatureMode::Content); } sal_Bool DocumentDigitalSignatures::signPackageWithCertificate( @@ -793,7 +815,9 @@ sal_Bool DocumentDigitalSignatures::signPackageWithCertificate( css::uno::Reference const& xStorage, css::uno::Reference const& xStream) { - return signWithCertificateImpl(xCertificate, xStorage, xStream, DocumentSignatureMode::Package); + uno::Reference xModel; + return signWithCertificateImpl(xModel, xCertificate, xStorage, xStream, + DocumentSignatureMode::Package); } sal_Bool DocumentDigitalSignatures::signScriptingContentWithCertificate( @@ -801,14 +825,16 @@ sal_Bool DocumentDigitalSignatures::signScriptingContentWithCertificate( css::uno::Reference const& xStorage, css::uno::Reference const& xStream) { - return signWithCertificateImpl(xCertificate, xStorage, xStream, DocumentSignatureMode::Macros); + uno::Reference xModel; + return signWithCertificateImpl(xModel, xCertificate, xStorage, xStream, + DocumentSignatureMode::Macros); } bool DocumentDigitalSignatures::signWithCertificateImpl( + const uno::Reference& xModel, css::uno::Reference const& xCertificate, css::uno::Reference const& xStorage, - css::uno::Reference const& xStream, - DocumentSignatureMode eMode) + css::uno::Reference const& xStream, DocumentSignatureMode eMode) { OSL_ENSURE(!m_sODFVersion.isEmpty(), "DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); @@ -821,6 +847,7 @@ bool DocumentDigitalSignatures::signWithCertificateImpl( aSignatureManager.setStore(xStorage); aSignatureManager.getSignatureHelper().SetStorage(xStorage, m_sODFVersion); aSignatureManager.setSignatureStream(xStream); + aSignatureManager.setModel(xModel); Reference xSecurityContext = aSignatureManager.getSecurityContext(); diff --git a/xmlsecurity/source/helper/documentsignaturemanager.cxx b/xmlsecurity/source/helper/documentsignaturemanager.cxx index 79d2cdf26b61..295522775951 100644 --- a/xmlsecurity/source/helper/documentsignaturemanager.cxx +++ b/xmlsecurity/source/helper/documentsignaturemanager.cxx @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -359,7 +360,7 @@ bool DocumentSignatureManager::add( getPDFSignatureHelper().SetX509Certificate(xCert); getPDFSignatureHelper().SetDescription(rDescription); uno::Reference xInputStream(mxSignatureStream, uno::UNO_QUERY); - if (!getPDFSignatureHelper().Sign(xInputStream, bAdESCompliant)) + if (!getPDFSignatureHelper().Sign(mxModel, xInputStream, bAdESCompliant)) { SAL_WARN("xmlsecurity.helper", "PDFSignatureHelper::Sign() failed"); return false; @@ -684,4 +685,9 @@ DocumentSignatureManager::getGpgSecurityContext() const return mxGpgSecurityContext; } +void DocumentSignatureManager::setModel(const uno::Reference& xModel) +{ + mxModel = xModel; +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmlsecurity/source/helper/pdfsignaturehelper.cxx b/xmlsecurity/source/helper/pdfsignaturehelper.cxx index 79979c715bff..fe65745b3e90 100644 --- a/xmlsecurity/source/helper/pdfsignaturehelper.cxx +++ b/xmlsecurity/source/helper/pdfsignaturehelper.cxx @@ -57,15 +57,9 @@ bool GetSignatureLinePage(const uno::Reference& xModel, sal_Int32 } /// If the currently selected shape is a Draw signature line, export that to PDF. -void GetSignatureLineShape(sal_Int32& rPage, std::vector& rSignatureLineShape) +void GetSignatureLineShape(const uno::Reference& xModel, sal_Int32& rPage, + std::vector& rSignatureLineShape) { - SfxObjectShell* pObjectShell = SfxObjectShell::Current(); - if (!pObjectShell) - { - return; - } - - uno::Reference xModel = pObjectShell->GetBaseModel(); if (!xModel.is()) { return; @@ -214,7 +208,8 @@ void PDFSignatureHelper::SetDescription(const OUString& rDescription) m_aDescription = rDescription; } -bool PDFSignatureHelper::Sign(const uno::Reference& xInputStream, bool bAdES) +bool PDFSignatureHelper::Sign(const uno::Reference& xModel, + const uno::Reference& xInputStream, bool bAdES) { std::unique_ptr pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true)); vcl::filter::PDFDocument aDocument; @@ -226,7 +221,7 @@ bool PDFSignatureHelper::Sign(const uno::Reference& xInputStre sal_Int32 nPage = 0; std::vector aSignatureLineShape; - GetSignatureLineShape(nPage, aSignatureLineShape); + GetSignatureLineShape(xModel, nPage, aSignatureLineShape); if (nPage > 0) { // UNO page number is 1-based. -- cgit