1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <macrosecurity.hxx>
22 #include <certificateviewer.hxx>
23 #include <biginteger.hxx>
24 #include <resourcemanager.hxx>
25 #include <strings.hrc>
27 #include <o3tl/safeint.hxx>
28 #include <osl/file.hxx>
29 #include <sal/log.hxx>
31 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
32 #include <comphelper/sequence.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/xmlsechelper.hxx>
35 #include <com/sun/star/uno/Exception.hpp>
36 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
37 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
38 #include <sfx2/filedlghelper.hxx>
39 #include <comphelper/diagnose_ex.hxx>
40 #include <tools/urlobj.hxx>
41 #include <unotools/datetime.hxx>
44 #include <vcl/svapp.hxx>
46 using namespace comphelper
;
47 using namespace ::com::sun::star
;
50 IMPL_LINK_NOARG(MacroSecurity
, OkBtnHdl
, weld::Button
&, void)
52 m_xLevelTP
->ClosePage();
53 m_xTrustSrcTP
->ClosePage();
54 m_xDialog
->response(RET_OK
);
57 MacroSecurity::MacroSecurity(weld::Window
* pParent
,
58 css::uno::Reference
<css::xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
)
59 : GenericDialogController(pParent
, "xmlsec/ui/macrosecuritydialog.ui", "MacroSecurityDialog")
60 , m_xSecurityEnvironment(std::move(xSecurityEnvironment
))
61 , m_xTabCtrl(m_xBuilder
->weld_notebook("tabcontrol"))
62 , m_xOkBtn(m_xBuilder
->weld_button("ok"))
63 , m_xResetBtn(m_xBuilder
->weld_button("reset"))
65 m_xTabCtrl
->connect_enter_page(LINK(this, MacroSecurity
, ActivatePageHdl
));
67 m_xLevelTP
.reset(new MacroSecurityLevelTP(m_xTabCtrl
->get_page("SecurityLevelPage"), this));
68 m_xTrustSrcTP
.reset(new MacroSecurityTrustedSourcesTP(m_xTabCtrl
->get_page("SecurityTrustPage"), this));
70 m_xTabCtrl
->set_current_page("SecurityLevelPage");
71 m_xOkBtn
->connect_clicked(LINK(this, MacroSecurity
, OkBtnHdl
));
74 IMPL_LINK(MacroSecurity
, ActivatePageHdl
, const OString
&, rPage
, void)
76 if (rPage
== "SecurityLevelPage")
77 m_xLevelTP
->ActivatePage();
78 else if (rPage
== "SecurityTrustPage")
79 m_xTrustSrcTP
->ActivatePage();
82 MacroSecurityTP::MacroSecurityTP(weld::Container
* pParent
, const OUString
& rUIXMLDescription
,
83 const OString
& rID
, MacroSecurity
* pDlg
)
84 : m_xBuilder(Application::CreateBuilder(pParent
, rUIXMLDescription
))
85 , m_xContainer(m_xBuilder
->weld_container(rID
))
90 void MacroSecurityTP::ActivatePage()
94 MacroSecurityTP::~MacroSecurityTP()
98 MacroSecurityLevelTP::MacroSecurityLevelTP(weld::Container
* pParent
, MacroSecurity
* pDlg
)
99 : MacroSecurityTP(pParent
, "xmlsec/ui/securitylevelpage.ui", "SecurityLevelPage", pDlg
)
100 , m_xVeryHighRB(m_xBuilder
->weld_radio_button("vhigh"))
101 , m_xHighRB(m_xBuilder
->weld_radio_button("high"))
102 , m_xMediumRB(m_xBuilder
->weld_radio_button("med"))
103 , m_xLowRB(m_xBuilder
->weld_radio_button("low"))
104 , m_xVHighImg(m_xBuilder
->weld_widget("vhighimg"))
105 , m_xHighImg(m_xBuilder
->weld_widget("highimg"))
106 , m_xMedImg(m_xBuilder
->weld_widget("medimg"))
107 , m_xLowImg(m_xBuilder
->weld_widget("lowimg"))
109 m_xLowRB
->connect_toggled( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
110 m_xMediumRB
->connect_toggled( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
111 m_xHighRB
->connect_toggled( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
112 m_xVeryHighRB
->connect_toggled( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
114 int nPrefWidth(std::max({m_xVeryHighRB
->get_preferred_size().Width(),
115 m_xHighRB
->get_preferred_size().Width(),
116 m_xMediumRB
->get_preferred_size().Width(),
117 m_xLowRB
->get_preferred_size().Width()}));
118 int nMaxWidth
= m_xLowRB
->get_approximate_digit_width() * 60;
119 if (nPrefWidth
> nMaxWidth
)
121 m_xLowRB
->set_label_wrap(true);
122 m_xLowRB
->set_size_request(nMaxWidth
, -1);
123 m_xMediumRB
->set_label_wrap(true);
124 m_xMediumRB
->set_size_request(nMaxWidth
, -1);
125 m_xHighRB
->set_label_wrap(true);
126 m_xHighRB
->set_size_request(nMaxWidth
, -1);
127 m_xVeryHighRB
->set_label_wrap(true);
128 m_xVeryHighRB
->set_size_request(nMaxWidth
, -1);
131 mnCurLevel
= static_cast<sal_uInt16
>(SvtSecurityOptions::GetMacroSecurityLevel());
132 bool bReadonly
= SvtSecurityOptions::IsReadOnly( SvtSecurityOptions::EOption::MacroSecLevel
);
134 weld::RadioButton
* pCheck
= nullptr;
135 weld::Widget
* pImage
= nullptr;
139 pCheck
= m_xVeryHighRB
.get();
140 pImage
= m_xVHighImg
.get();
143 pCheck
= m_xHighRB
.get();
144 pImage
= m_xHighImg
.get();
147 pCheck
= m_xMediumRB
.get();
148 pImage
= m_xMedImg
.get();
151 pCheck
= m_xLowRB
.get();
152 pImage
= m_xLowImg
.get();
156 pCheck
->set_active(true);
159 OSL_FAIL("illegal macro security level");
161 if (bReadonly
&& pImage
)
164 m_xVeryHighRB
->set_sensitive(false);
165 m_xHighRB
->set_sensitive(false);
166 m_xMediumRB
->set_sensitive(false);
167 m_xLowRB
->set_sensitive(false);
171 IMPL_LINK_NOARG(MacroSecurityLevelTP
, RadioButtonHdl
, weld::Toggleable
&, void)
173 sal_uInt16 nNewLevel
= 0;
174 if( m_xVeryHighRB
->get_active() )
176 else if( m_xHighRB
->get_active() )
178 else if( m_xMediumRB
->get_active() )
181 if ( nNewLevel
!= mnCurLevel
)
183 mnCurLevel
= nNewLevel
;
184 m_pDlg
->EnableReset();
188 void MacroSecurityLevelTP::ClosePage()
190 SvtSecurityOptions::SetMacroSecurityLevel( mnCurLevel
);
193 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
195 bool bCertSelected
= m_xTrustCertLB
->get_selected_index() != -1;
196 m_xViewCertPB
->set_sensitive( bCertSelected
);
197 m_xRemoveCertPB
->set_sensitive( bCertSelected
&& !mbAuthorsReadonly
);
199 bool bLocationSelected
= m_xTrustFileLocLB
->get_selected_index() != -1;
200 m_xRemoveLocPB
->set_sensitive( bLocationSelected
&& !mbURLsReadonly
);
203 void MacroSecurityTrustedSourcesTP::ShowBrokenCertificateError(std::u16string_view rData
)
205 OUString aMsg
= XsResId(STR_BROKEN_MACRO_CERTIFICATE_DATA
);
206 aMsg
= aMsg
.replaceFirst("%{data}", rData
);
207 std::unique_ptr
<weld::MessageDialog
> xErrorBox(Application::CreateMessageDialog(m_pDlg
->getDialog(),
208 VclMessageType::Error
, VclButtonsType::Ok
, aMsg
));
212 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
, weld::Button
&, void)
214 int nEntry
= m_xTrustCertLB
->get_selected_index();
218 const sal_uInt16 nSelected
= m_xTrustCertLB
->get_id(nEntry
).toUInt32();
219 uno::Reference
< css::security::XCertificate
> xCert
;
222 xCert
= m_pDlg
->m_xSecurityEnvironment
->getCertificate(m_aTrustedAuthors
[nSelected
].SubjectName
,
223 xmlsecurity::numericStringToBigInteger(m_aTrustedAuthors
[nSelected
].SerialNumber
));
227 TOOLS_WARN_EXCEPTION("xmlsecurity.dialogs", "matching certificate not found for: " << m_aTrustedAuthors
[nSelected
].SubjectName
);
234 xCert
= m_pDlg
->m_xSecurityEnvironment
->createCertificateFromAscii(m_aTrustedAuthors
[nSelected
].RawData
);
238 TOOLS_WARN_EXCEPTION("xmlsecurity.dialogs", "certificate data couldn't be parsed: " << m_aTrustedAuthors
[nSelected
].RawData
);
244 CertificateViewer
aViewer(m_pDlg
->getDialog(), m_pDlg
->m_xSecurityEnvironment
, xCert
, false, nullptr);
248 // should never happen, as we parsed the certificate data when we added it!
249 ShowBrokenCertificateError(m_aTrustedAuthors
[nSelected
].RawData
);
252 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
, weld::Button
&, void)
254 int nEntry
= m_xTrustCertLB
->get_selected_index();
257 sal_uInt16 nAuthor
= m_xTrustCertLB
->get_id(nEntry
).toUInt32();
258 m_aTrustedAuthors
.erase(m_aTrustedAuthors
.begin() + nAuthor
);
265 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, AddLocPBHdl
, weld::Button
&, void)
269 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
270 uno::Reference
< ui::dialogs::XFolderPicker2
> xFolderPicker
= sfx2::createFolderPicker(xContext
, m_pDlg
->getDialog());
272 short nRet
= xFolderPicker
->execute();
274 if( ui::dialogs::ExecutableDialogResults::OK
!= nRet
)
277 OUString aPathStr
= xFolderPicker
->getDirectory();
278 INetURLObject
aNewObj( aPathStr
);
279 aNewObj
.removeFinalSlash();
281 // then the new path also a URL else system path
282 OUString aSystemFileURL
= ( aNewObj
.GetProtocol() != INetProtocol::NotValid
) ?
283 aPathStr
: aNewObj
.getFSysPath( FSysStyle::Detect
);
285 OUString
aNewPathStr(aSystemFileURL
);
287 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
) == osl::FileBase::E_None
)
288 aNewPathStr
= aSystemFileURL
;
290 if (m_xTrustFileLocLB
->find_text(aNewPathStr
) == -1)
291 m_xTrustFileLocLB
->append_text(aNewPathStr
);
295 catch( uno::Exception
& )
297 TOOLS_WARN_EXCEPTION( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
301 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
, weld::Button
&, void)
303 sal_Int32 nSel
= m_xTrustFileLocLB
->get_selected_index();
307 m_xTrustFileLocLB
->remove(nSel
);
308 // Trusted Path could not be removed (#i33584#)
309 // after remove an entry, select another one if exists
310 int nNewCount
= m_xTrustFileLocLB
->n_children();
313 if (nSel
>= nNewCount
)
314 nSel
= nNewCount
- 1;
315 m_xTrustFileLocLB
->select(nSel
);
320 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
, weld::TreeView
&, void)
325 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
, weld::TreeView
&, void)
330 void MacroSecurityTrustedSourcesTP::FillCertLB(const bool bShowWarnings
)
332 m_xTrustCertLB
->clear();
334 sal_uInt32 nEntries
= m_aTrustedAuthors
.size();
336 if ( !(nEntries
&& m_pDlg
->m_xSecurityEnvironment
.is()) )
339 for( sal_uInt32 nEntry
= 0 ; nEntry
< nEntries
; ++nEntry
)
341 SvtSecurityOptions::Certificate
& rEntry
= m_aTrustedAuthors
[ nEntry
];
345 // create from RawData
346 uno::Reference
< css::security::XCertificate
> xCert
= m_pDlg
->m_xSecurityEnvironment
->createCertificateFromAscii(rEntry
.RawData
);
347 m_xTrustCertLB
->append(OUString::number(nEntry
), xmlsec::GetContentPart(xCert
->getSubjectName(), xCert
->getCertificateKind()));
348 m_xTrustCertLB
->set_text(nEntry
, xmlsec::GetContentPart(xCert
->getIssuerName(), xCert
->getCertificateKind()), 1);
349 m_xTrustCertLB
->set_text(nEntry
, utl::GetDateTimeString(xCert
->getNotValidAfter()), 2);
355 TOOLS_WARN_EXCEPTION("xmlsecurity.dialogs", "certificate data couldn't be parsed: " << rEntry
.RawData
);
356 OUString sData
= rEntry
.RawData
;
357 css::uno::Any
tools_warn_exception(DbgGetCaughtException());
358 OUString sException
= OStringToOUString(exceptionToString(tools_warn_exception
), RTL_TEXTENCODING_UTF8
);
359 if (!sException
.isEmpty())
360 sData
+= " / " + sException
;
361 ShowBrokenCertificateError(sData
);
367 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(weld::Container
* pParent
, MacroSecurity
* pDlg
)
368 : MacroSecurityTP(pParent
, "xmlsec/ui/securitytrustpage.ui", "SecurityTrustPage", pDlg
)
369 , m_xTrustCertROFI(m_xBuilder
->weld_image("lockcertimg"))
370 , m_xTrustCertLB(m_xBuilder
->weld_tree_view("certificates"))
371 , m_xViewCertPB(m_xBuilder
->weld_button("viewcert"))
372 , m_xRemoveCertPB(m_xBuilder
->weld_button("removecert"))
373 , m_xTrustFileROFI(m_xBuilder
->weld_image("lockfileimg"))
374 , m_xTrustFileLocLB(m_xBuilder
->weld_tree_view("locations"))
375 , m_xAddLocPB(m_xBuilder
->weld_button("addfile"))
376 , m_xRemoveLocPB(m_xBuilder
->weld_button("removefile"))
378 auto nColWidth
= m_xTrustCertLB
->get_approximate_digit_width() * 12;
379 std::vector
<int> aWidths
381 o3tl::narrowing
<int>(nColWidth
* 2),
382 o3tl::narrowing
<int>(nColWidth
* 2)
384 m_xTrustCertLB
->set_column_fixed_widths(aWidths
);
385 m_xTrustCertLB
->set_size_request(nColWidth
* 5.5, m_xTrustCertLB
->get_height_rows(5));
387 m_xTrustCertLB
->connect_changed( LINK( this, MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
) );
388 m_xViewCertPB
->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
) );
389 m_xViewCertPB
->set_sensitive(false);
390 m_xRemoveCertPB
->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
) );
391 m_xRemoveCertPB
->set_sensitive(false);
393 m_xTrustFileLocLB
->connect_changed( LINK( this, MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
) );
394 m_xTrustFileLocLB
->set_size_request(nColWidth
* 5, m_xTrustFileLocLB
->get_height_rows(5));
395 m_xAddLocPB
->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP
, AddLocPBHdl
) );
396 m_xRemoveLocPB
->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
) );
397 m_xRemoveLocPB
->set_sensitive(false);
399 m_aTrustedAuthors
= SvtSecurityOptions::GetTrustedAuthors();
400 mbAuthorsReadonly
= SvtSecurityOptions::IsReadOnly( SvtSecurityOptions::EOption::MacroTrustedAuthors
);
401 m_xTrustCertROFI
->set_visible(mbAuthorsReadonly
);
405 std::vector
< OUString
> aSecureURLs
= SvtSecurityOptions::GetSecureURLs();
406 mbURLsReadonly
= SvtSecurityOptions::IsReadOnly( SvtSecurityOptions::EOption::SecureUrls
);
407 m_xTrustFileROFI
->set_visible(mbURLsReadonly
);
408 m_xAddLocPB
->set_sensitive(!mbURLsReadonly
);
410 for (const auto& rSecureURL
: aSecureURLs
)
412 OUString
aSystemFileURL( rSecureURL
);
413 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
);
414 m_xTrustFileLocLB
->append_text(aSystemFileURL
);
418 void MacroSecurityTrustedSourcesTP::ActivatePage()
420 m_pDlg
->EnableReset( false );
424 void MacroSecurityTrustedSourcesTP::ClosePage()
426 sal_Int32 nEntryCnt
= m_xTrustFileLocLB
->n_children();
429 std::vector
< OUString
> aSecureURLs
;
430 for (sal_Int32 i
= 0; i
< nEntryCnt
; ++i
)
432 OUString
aURL(m_xTrustFileLocLB
->get_text(i
));
433 osl::FileBase::getFileURLFromSystemPath( aURL
, aURL
);
434 aSecureURLs
.push_back(aURL
);
437 SvtSecurityOptions::SetSecureURLs( std::move(aSecureURLs
) );
439 // Trusted Path could not be removed (#i33584#)
440 // don't forget to remove the old saved SecureURLs
442 SvtSecurityOptions::SetSecureURLs( std::vector
< OUString
>() );
444 SvtSecurityOptions::SetTrustedAuthors( m_aTrustedAuthors
);
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */