diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2017-08-01 18:07:56 -0400 |
---|---|---|
committer | Ashod Nakashian <ashnakash@gmail.com> | 2017-08-04 02:12:26 +0200 |
commit | 345994dad91765e5356f95786146bf8aca5a4aa3 (patch) | |
tree | 56e09f549534d18b195e4ca470690806955fea57 | |
parent | svl: move DecodeHexString from vcl (diff) | |
download | core-345994dad91765e5356f95786146bf8aca5a4aa3.tar.gz core-345994dad91765e5356f95786146bf8aca5a4aa3.zip |
sw: sign paragraph text
The results are not stored anywhere just yet.
Change-Id: I99a701ee8a16f166350c7c342b34b8fc476a81ae
Reviewed-on: https://gerrit.libreoffice.org/40721
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
-rw-r--r-- | include/svl/cryptosign.hxx | 4 | ||||
-rw-r--r-- | sw/inc/editsh.hxx | 5 | ||||
-rw-r--r-- | sw/source/core/edit/edfcol.cxx | 48 | ||||
-rw-r--r-- | sw/source/uibase/shells/textsh1.cxx | 2 |
4 files changed, 51 insertions, 8 deletions
diff --git a/include/svl/cryptosign.hxx b/include/svl/cryptosign.hxx index f8c1f36bdd8a..2a44744bbaa1 100644 --- a/include/svl/cryptosign.hxx +++ b/include/svl/cryptosign.hxx @@ -51,7 +51,7 @@ public: /// Add a range to sign. /// Note: for efficiency this takes a naked pointer, which must remain valid /// until this object is discarded. - void AddDataRange(void* pData, sal_Int32 size) + void AddDataRange(const void* pData, sal_Int32 size) { m_dataBlocks.emplace_back(pData, size); } @@ -81,7 +81,7 @@ private: const css::uno::Reference<css::security::XCertificate> m_xCertificate; /// Data blocks (pointer-size pairs). - std::vector<std::pair<void*, sal_Int32>> m_dataBlocks; + std::vector<std::pair<const void*, sal_Int32>> m_dataBlocks; OUString m_aSignTSA; OUString m_aSignPassword; }; diff --git a/sw/inc/editsh.hxx b/sw/inc/editsh.hxx index c539007c34c9..7e2d8b739427 100644 --- a/sw/inc/editsh.hxx +++ b/sw/inc/editsh.hxx @@ -372,7 +372,10 @@ public: void SetWatermark(const SfxWatermarkItem& rText); /// Sign the paragraph at the cursor. - static void SignParagraph(SwPaM* pPaM); + void SignParagraph(SwPaM* pPaM); + + /// Verify the paragraph at the cursor. + void VerifyParagraph(SwPaM* pPaM); void Insert2(SwField&, const bool bForceExpandHints); diff --git a/sw/source/core/edit/edfcol.cxx b/sw/source/core/edit/edfcol.cxx index 808525287c75..28d2907c939b 100644 --- a/sw/source/core/edit/edfcol.cxx +++ b/sw/source/core/edit/edfcol.cxx @@ -32,6 +32,7 @@ #include <com/sun/star/text/TextContentAnchorType.hpp> #include <com/sun/star/text/VertOrientation.hpp> #include <com/sun/star/text/WrapTextMode.hpp> +#include <com/sun/star/xml/crypto/SEInitializer.hpp> #include <basegfx/matrix/b2dhommatrix.hxx> #include <comphelper/propertysequence.hxx> @@ -40,6 +41,7 @@ #include <editeng/formatbreakitem.hxx> #include <editeng/unoprnms.hxx> #include <sfx2/classificationhelper.hxx> +#include <svl/cryptosign.hxx> #include <vcl/svapp.hxx> #include <hintids.hxx> @@ -60,6 +62,8 @@ #include <pagefrm.hxx> #include <sfx2/watermarkitem.hxx> +#include <cppuhelper/bootstrap.hxx> + #define WATERMARK_NAME "PowerPlusWaterMarkObject" namespace @@ -541,15 +545,51 @@ void SwEditShell::SignParagraph(SwPaM* pPaM) if (!pPaM) return; + SwDocShell* pDocShell = GetDoc()->GetDocShell(); + if (!pDocShell) + return; + SwWrtShell* pCurShell = pDocShell->GetWrtShell(); + if (!pCurShell) + return; + const SwPosition* pPosStart = pPaM->Start(); SwTextNode* pNode = pPosStart->nNode.GetNode().GetTextNode(); if (pNode) { - // Get the text (without fields). + // 1. Get the text (without fields). const OUString text = pNode->GetText(); - (void)text; - - //TODO: get signature, add signature field and metadata. + if (text.isEmpty()) + return; + + // 2. Get certificate and SignatureInformation (needed to show signer name). + //FIXME: Temporary until the Paragraph Signing Dialog is available. + uno::Reference<uno::XComponentContext> xComponentContext = cppu::defaultBootstrap_InitialComponentContext(); + uno::Reference<xml::crypto::XSEInitializer> xSEInitializer = xml::crypto::SEInitializer::create(xComponentContext); + uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext = xSEInitializer->createSecurityContext(OUString()); + uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment = xSecurityContext->getSecurityEnvironment(); + uno::Sequence<uno::Reference<security::XCertificate>> aCertificates = xSecurityEnvironment->getPersonalCertificates(); + if (!aCertificates.hasElements()) + return; + + SignatureInformation aInfo(0); + uno::Reference<security::XCertificate> xCert = aCertificates[0]; + if (!xCert.is()) + return; + + // 3. Sign it. + svl::crypto::Signing signing(xCert); + signing.AddDataRange(text.getStr(), text.getLength()); + OStringBuffer signature; + if (!signing.Sign(signature)) + return; + + const auto pData = reinterpret_cast<const unsigned char*>(text.getStr()); + const std::vector<unsigned char> data(pData, pData + text.getLength()); + const std::vector<unsigned char> sig(svl::crypto::DecodeHexString(signature.makeStringAndClear())); + if (!svl::crypto::Signing::Verify(data, true, sig, aInfo)) + return; + + // 4. Add metadata. } } diff --git a/sw/source/uibase/shells/textsh1.cxx b/sw/source/uibase/shells/textsh1.cxx index 313dabcf835b..30e923ae0eb5 100644 --- a/sw/source/uibase/shells/textsh1.cxx +++ b/sw/source/uibase/shells/textsh1.cxx @@ -1107,7 +1107,7 @@ void SwTextShell::Execute(SfxRequest &rReq) rWrtSh.StartUndo(SwUndoId::PARA_SIGN_ADD); rWrtSh.StartAction(); - SwWrtShell::SignParagraph(pPaM); + rWrtSh.SignParagraph(pPaM); rWrtSh.EndAction(); |