summaryrefslogtreecommitdiff
path: root/sfx2/source/doc
diff options
context:
space:
mode:
authorSarper Akdemir <sarper.akdemir@allotropia.de>2024-07-17 12:47:22 +0200
committerSarper Akdemir <sarper.akdemir@allotropia.de>2024-07-24 07:08:46 +0200
commit426c641976688e3e4d1ce66f76b27ccbd2dca55a (patch)
tree8a56790e284f7f586c1298959a58d61490a6d241 /sfx2/source/doc
parent6ca02ef177df62edb124881248a1ebcd9876ff4c (diff)
tdf#159040: add sign with default certificate to save dialog
Include X.509 certificates under Tools->Options->UserData among the keys for signing. Add a new checkbox to Save file dialog, to sign with that selected key easily. The checkbox is disabled if there's no matching key found. Change-Id: I9fc16790c479819cd1f35bcad040d0ebc7c4bdef Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170619 Tested-by: Jenkins Reviewed-by: Sarper Akdemir <sarper.akdemir@allotropia.de>
Diffstat (limited to 'sfx2/source/doc')
-rw-r--r--sfx2/source/doc/guisaveas.cxx61
-rw-r--r--sfx2/source/doc/objserv.cxx25
2 files changed, 76 insertions, 10 deletions
diff --git a/sfx2/source/doc/guisaveas.cxx b/sfx2/source/doc/guisaveas.cxx
index ce31ee2da6f7..eee91ba06a38 100644
--- a/sfx2/source/doc/guisaveas.cxx
+++ b/sfx2/source/doc/guisaveas.cxx
@@ -16,6 +16,14 @@
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
+#include <config_gpgme.h>
+#if HAVE_FEATURE_GPGME
+#include <com/sun/star/xml/crypto/GPGSEInitializer.hpp>
+#include <com/sun/star/xml/crypto/SEInitializer.hpp>
+#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
+#endif
+#include <com/sun/star/security/DocumentDigitalSignatures.hpp>
+#include <com/sun/star/security/XCertificate.hpp>
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
@@ -63,6 +71,7 @@
#include <comphelper/sequenceashashmap.hxx>
#include <comphelper/mimeconfighelper.hxx>
#include <comphelper/lok.hxx>
+#include <comphelper/xmlsechelper.hxx>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <utility>
#include <vcl/svapp.hxx>
@@ -275,6 +284,7 @@ class ModelData_Impl
::comphelper::SequenceAsHashMap m_aMediaDescrHM;
bool m_bRecommendReadOnly;
+ bool m_bSignWithDefaultSignature;
DECL_LINK(OptionsDialogClosedHdl, css::ui::dialogs::DialogClosedEvent*, void);
@@ -294,6 +304,7 @@ public:
::comphelper::SequenceAsHashMap& GetMediaDescr() { return m_aMediaDescrHM; }
bool IsRecommendReadOnly() const { return m_bRecommendReadOnly; }
+ bool IsSignWithDefaultSignature() const { return m_bSignWithDefaultSignature; }
const ::comphelper::SequenceAsHashMap& GetDocProps();
@@ -1093,6 +1104,10 @@ bool ModelData_Impl::OutputFileDialog( sal_Int16 nStoreMode,
m_bRecommendReadOnly = ( pRecommendReadOnly && pRecommendReadOnly->GetValue() );
pDialogParams->ClearItem( SID_RECOMMENDREADONLY );
+ const SfxBoolItem* pSignWithDefaultKey = SfxItemSet::GetItem<SfxBoolItem>(&*pDialogParams, SID_GPGSIGN, false);
+ m_bSignWithDefaultSignature = (pSignWithDefaultKey && pSignWithDefaultKey->GetValue());
+ pDialogParams->ClearItem( SID_GPGSIGN );
+
uno::Sequence< beans::PropertyValue > aPropsFromDialog;
TransformItems( nSlotID, *pDialogParams, aPropsFromDialog );
GetMediaDescr() << aPropsFromDialog;
@@ -1878,6 +1893,52 @@ bool SfxStoringHelper::FinishGUIStoreModel(::comphelper::SequenceAsHashMap::cons
#endif
}
+ if (aModelData.IsSignWithDefaultSignature())
+ {
+ auto SignWithDefaultSignature = [&]()
+ {
+#if HAVE_FEATURE_GPGME
+ auto aSigningKey = SvtUserOptions().GetSigningKey();
+ if (aSigningKey.isEmpty())
+ return;
+
+ std::vector<uno::Reference<xml::crypto::XXMLSecurityContext>> xSecurityContexts{
+ xml::crypto::SEInitializer::create(comphelper::getProcessComponentContext())
+ ->createSecurityContext({}),
+ xml::crypto::GPGSEInitializer::create(comphelper::getProcessComponentContext())
+ ->createSecurityContext({}),
+ };
+
+ for (const auto& xSecurityContext : xSecurityContexts)
+ {
+ if (xSecurityContext.is())
+ {
+ css::uno::Reference<css::security::XCertificate> xCert
+ = comphelper::xmlsec::FindCertInContext(xSecurityContext, aSigningKey);
+
+ if (xCert.is() && SfxViewShell::Current())
+ {
+ SfxObjectShell* pDocShell = SfxViewShell::Current()->GetObjectShell();
+ bool bSigned = pDocShell->SignDocumentContentUsingCertificate(xCert);
+ if (bSigned && pDocShell->HasValidSignatures())
+ {
+ std::unique_ptr<weld::MessageDialog> xBox(
+ Application::CreateMessageDialog(
+ SfxStoringHelper::GetModelWindow(aModelData.GetModel()),
+ VclMessageType::Question, VclButtonsType::YesNo,
+ SfxResId(STR_QUERY_REMEMBERSIGNATURE)));
+ pDocShell->SetRememberCurrentSignature(xBox->run() == RET_YES);
+ }
+ return;
+ }
+ }
+ }
+ return;
+#endif
+ };
+ SignWithDefaultSignature();
+ }
+
// Launch PDF viewer
if ( nStoreMode & PDFEXPORT_REQUESTED && !comphelper::LibreOfficeKit::isActive() )
{
diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx
index 555e57492801..26301eb1cb6d 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -600,16 +600,7 @@ void SfxObjectShell::ExecFile_Impl(SfxRequest &rReq)
{
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog( pDialogParent,
VclMessageType::Question, VclButtonsType::YesNo, SfxResId(STR_QUERY_REMEMBERSIGNATURE)));
- if (xBox->run() == RET_YES)
- {
- rSignatureInfosRemembered = GetDocumentSignatureInformation(false);
- bRememberSignature = true;
- }
- else
- {
- rSignatureInfosRemembered = uno::Sequence< security::DocumentSignatureInformation >();
- bRememberSignature = false;
- }
+ SetRememberCurrentSignature(xBox->run() == RET_YES);
}
return;
@@ -1899,6 +1890,20 @@ uno::Sequence< security::DocumentSignatureInformation > SfxObjectShell::GetDocum
return aResult;
}
+void SfxObjectShell::SetRememberCurrentSignature(bool bRemember)
+{
+ if (bRemember)
+ {
+ rSignatureInfosRemembered = GetDocumentSignatureInformation(false);
+ bRememberSignature = true;
+ }
+ else
+ {
+ rSignatureInfosRemembered = uno::Sequence<security::DocumentSignatureInformation>();
+ bRememberSignature = false;
+ }
+}
+
SignatureState SfxObjectShell::ImplGetSignatureState( bool bScriptingContent )
{
SignatureState* pState = bScriptingContent ? &pImpl->nScriptingSignatureState : &pImpl->nDocumentSignatureState;