diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2018-10-15 10:06:56 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-10-25 23:33:53 +0200 |
commit | 40810c43c48b25385b487c0b79c4c466dbf36be7 (patch) | |
tree | 2679b17629e279d4631800b9ce923d6d39d76b90 | |
parent | 2fac8cf22f687b247ba6fba8e7caf8e970cd15d9 (diff) |
Support to sign a document with a specified signature
Change-Id: I92e392b315c9217b63db830dea1b2e91387b56c2
Reviewed-on: https://gerrit.libreoffice.org/61778
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | offapi/com/sun/star/security/XDocumentDigitalSignatures.idl | 10 | ||||
-rw-r--r-- | xmlsecurity/source/component/documentdigitalsignatures.cxx | 41 |
2 files changed, 50 insertions, 1 deletions
diff --git a/offapi/com/sun/star/security/XDocumentDigitalSignatures.idl b/offapi/com/sun/star/security/XDocumentDigitalSignatures.idl index ebd42c6ecef6..d7e001c57607 100644 --- a/offapi/com/sun/star/security/XDocumentDigitalSignatures.idl +++ b/offapi/com/sun/star/security/XDocumentDigitalSignatures.idl @@ -191,7 +191,15 @@ interface XDocumentDigitalSignatures : com::sun::star::uno::XInterface */ com::sun::star::security::XCertificate chooseCertificateWithProps( [out] sequence<::com::sun::star::beans::PropertyValue> Properties); -} ; + /** Signs the content of the document with the provided certificate. + + @since LibreOffice 6.2 + */ + boolean signDocumentWithCertificate([in] ::com::sun::star::security::XCertificate xCertificate, + [in] ::com::sun::star::embed::XStorage xStorage, + [in] ::com::sun::star::io::XStream xStream); + +}; } ; } ; } ; } ; diff --git a/xmlsecurity/source/component/documentdigitalsignatures.cxx b/xmlsecurity/source/component/documentdigitalsignatures.cxx index 23689c1e4fa3..d19f6bd854e0 100644 --- a/xmlsecurity/source/component/documentdigitalsignatures.cxx +++ b/xmlsecurity/source/component/documentdigitalsignatures.cxx @@ -168,6 +168,11 @@ public: SAL_CALL chooseEncryptionCertificate() override; css::uno::Reference<css::security::XCertificate> SAL_CALL chooseCertificateWithProps( css::uno::Sequence<::com::sun::star::beans::PropertyValue>& Properties) override; + + sal_Bool SAL_CALL signDocumentWithCertificate( + css::uno::Reference<css::security::XCertificate> const & xCertificate, + css::uno::Reference<css::embed::XStorage> const & xStoragexStorage, + css::uno::Reference<css::io::XStream> const & xStream) override; }; DocumentDigitalSignatures::DocumentDigitalSignatures( const Reference< XComponentContext >& rxCtx ): @@ -749,6 +754,42 @@ void DocumentDigitalSignatures::addLocationToTrustedSources( const OUString& Loc aSecOpt.SetSecureURLs( aSecURLs ); } +sal_Bool DocumentDigitalSignatures::signDocumentWithCertificate( + css::uno::Reference<css::security::XCertificate> const & xCertificate, + css::uno::Reference<css::embed::XStorage> const & xStorage, + css::uno::Reference<css::io::XStream> const & xStream) +{ + DocumentSignatureManager aSignatureManager(mxCtx, DocumentSignatureMode::Content); + + if (!aSignatureManager.init()) + return false; + + aSignatureManager.mxStore = xStorage; + aSignatureManager.maSignatureHelper.SetStorage(xStorage, m_sODFVersion); + aSignatureManager.mxSignatureStream = xStream; + + Reference<XXMLSecurityContext> xSecurityContext; + Reference<XServiceInfo> xServiceInfo(xCertificate, UNO_QUERY); + xSecurityContext = aSignatureManager.getSecurityContext(); + + sal_Int32 nSecurityId; + + bool bSuccess = aSignatureManager.add(xCertificate, xSecurityContext, "", nSecurityId, true); + if (!bSuccess) + return false; + + aSignatureManager.read(/*bUseTempStream=*/true, /*bCacheLastSignature=*/false); + aSignatureManager.write(true); + + if (xStorage.is() && !xStream.is()) + { + uno::Reference<embed::XTransactedObject> xTransaction(xStorage, uno::UNO_QUERY); + xTransaction->commit(); + } + + return true; +} + extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* com_sun_star_security_DocumentDigitalSignatures_get_implementation( uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |