diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2018-10-15 10:09:15 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-10-28 21:29:19 +0100 |
commit | 23a2312344ac961ead9ee14140c0b3e879bb7a41 (patch) | |
tree | 866e85ef63364028cff008f12ad9d520a1f48cc4 /desktop | |
parent | 12b9952a9a04341bc41b0a5838bd25cfbb835e62 (diff) |
lokit: add funct. to insert, sign and verify signature
A lot of signing code paths trigger a GUI dialog (to select the
certificate for example) which aren't acceptable when triggering
through the LOKit. This code paths needed to be duplicated and
reworked to not trigger any GUI action.
Change-Id: I2f0d6038fb1bcd00adcdf86e432f9df8858cc21c
Reviewed-on: https://gerrit.libreoffice.org/61780
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/qa/desktop_lib/test_desktop_lib.cxx | 4 | ||||
-rw-r--r-- | desktop/source/lib/init.cxx | 69 |
2 files changed, 72 insertions, 1 deletions
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx index 69f2b3ac2bfb..9a8d7b88b0b7 100644 --- a/desktop/qa/desktop_lib/test_desktop_lib.cxx +++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx @@ -2299,10 +2299,12 @@ void DesktopLOKTest::testABI() CPPUNIT_ASSERT_EQUAL(documentClassOffset(39), offsetof(struct _LibreOfficeKitDocumentClass, setViewLanguage)); CPPUNIT_ASSERT_EQUAL(documentClassOffset(40), offsetof(struct _LibreOfficeKitDocumentClass, postWindowExtTextInputEvent)); CPPUNIT_ASSERT_EQUAL(documentClassOffset(41), offsetof(struct _LibreOfficeKitDocumentClass, getPartInfo)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(42), offsetof(struct _LibreOfficeKitDocumentClass, insertCertificate)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(43), offsetof(struct _LibreOfficeKitDocumentClass, getSignatureState)); // Extending is fine, update this, and add new assert for the offsetof the // new method - CPPUNIT_ASSERT_EQUAL(documentClassOffset(42), sizeof(struct _LibreOfficeKitDocumentClass)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(44), sizeof(struct _LibreOfficeKitDocumentClass)); } CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest); diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index a2182e9ea2a8..2a13775dcc64 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -73,6 +73,13 @@ #include <com/sun/star/document/XRedlinesSupplier.hpp> #include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp> +#include <com/sun/star/xml/crypto/SEInitializer.hpp> +#include <com/sun/star/xml/crypto/XSEInitializer.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> +#include <com/sun/star/security/DocumentDigitalSignatures.hpp> +#include <com/sun/star/security/XDocumentDigitalSignatures.hpp> +#include <com/sun/star/security/XCertificate.hpp> + #include <com/sun/star/linguistic2/LinguServiceManager.hpp> #include <com/sun/star/linguistic2/XSpellChecker.hpp> #include <com/sun/star/i18n/ScriptType.hpp> @@ -682,6 +689,12 @@ static void doc_postWindow(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId, static char* doc_getPartInfo(LibreOfficeKitDocument* pThis, int nPart); +static bool doc_insertCertificate(LibreOfficeKitDocument* pThis, + const unsigned char* pCertificateBinary, + const int pCertificateBinarySize); + +static int doc_getSignatureState(LibreOfficeKitDocument* pThis); + LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent) : mxComponent(xComponent) { @@ -742,6 +755,9 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XCompone m_pDocumentClass->getPartInfo = doc_getPartInfo; + m_pDocumentClass->insertCertificate = doc_insertCertificate; + m_pDocumentClass->getSignatureState = doc_getSignatureState; + gDocumentClass = m_pDocumentClass; } pClass = m_pDocumentClass.get(); @@ -3584,6 +3600,59 @@ static void doc_postWindow(LibreOfficeKitDocument* /*pThis*/, unsigned nLOKWindo } } +// CERTIFICATE AND DOCUMENT SIGNING +static bool doc_insertCertificate(LibreOfficeKitDocument* /*pThis*/, const unsigned char* pCertificateBinary, const int nCertificateBinarySize) +{ + if (!xContext.is()) + return false; + + uno::Reference<xml::crypto::XSEInitializer> xSEInitializer = xml::crypto::SEInitializer::create(xContext); + uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext; + xSecurityContext = xSEInitializer->createSecurityContext(OUString()); + if (!xSecurityContext.is()) + return false; + + uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment; + xSecurityEnvironment = xSecurityContext->getSecurityEnvironment(); + + uno::Sequence<sal_Int8> aCertificateSequence(nCertificateBinarySize); + std::copy(pCertificateBinary, pCertificateBinary + nCertificateBinarySize, aCertificateSequence.begin()); + + uno::Reference<security::XCertificate> xCertificate = xSecurityEnvironment->createCertificateFromRaw(aCertificateSequence); + + if (!xCertificate.is()) + return false; + + printf("CERTIFICATE\n\tIssuerName: %s \n\tSubjectName: %s\n\tPK %s\n\n", + xCertificate->getIssuerName().toUtf8().getStr(), + xCertificate->getSubjectName().toUtf8().getStr(), + xCertificate->getSubjectPublicKeyAlgorithm().toUtf8().getStr()); + + SfxObjectShell* pDoc = SfxObjectShell::Current(); + if (!pDoc) + return false; + + return pDoc->SignDocumentContentUsingCertificate(xCertificate); +} + +static int doc_getSignatureState(LibreOfficeKitDocument* pThis) +{ + LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis); + + if (!pDocument->mxComponent.is()) + return int(SignatureState::UNKNOWN); + + SfxBaseModel* pBaseModel = dynamic_cast<SfxBaseModel*>(pDocument->mxComponent.get()); + if (!pBaseModel) + return int(SignatureState::UNKNOWN); + + SfxObjectShell* pObjectShell = pBaseModel->GetObjectShell(); + if (!pObjectShell) + return int(SignatureState::UNKNOWN); + + return int(pObjectShell->GetDocumentSignatureState()); +} + static char* lo_getError (LibreOfficeKit *pThis) { SolarMutexGuard aGuard; |