1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <macrosecurity.hxx>
#include <certificatechooser.hxx>
#include <certificateviewer.hxx>
#include <biginteger.hxx>
#include <osl/file.hxx>
#include <sal/log.hxx>
#include <vcl/help.hxx>
#include <vcl/layout.hxx>
#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
#include <comphelper/sequence.hxx>
#include <sfx2/filedlghelper.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/xmlsechelper.hxx>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ui/dialogs/FolderPicker.hpp>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
#include <tools/urlobj.hxx>
#include <unotools/datetime.hxx>
#include <vcl/treelistentry.hxx>
#include <strings.hrc>
using namespace comphelper;
using namespace ::com::sun::star;
IMPL_LINK_NOARG(MacroSecurity, OkBtnHdl, weld::Button&, void)
{
m_xLevelTP->ClosePage();
m_xTrustSrcTP->ClosePage();
m_xDialog->response(RET_OK);
}
MacroSecurity::MacroSecurity(weld::Window* pParent,
const css::uno::Reference<css::xml::crypto::XSecurityEnvironment>& rxSecurityEnvironment)
: GenericDialogController(pParent, "xmlsec/ui/macrosecuritydialog.ui", "MacroSecurityDialog")
, m_xSecurityEnvironment(rxSecurityEnvironment)
, m_xTabCtrl(m_xBuilder->weld_notebook("tabcontrol"))
, m_xOkBtn(m_xBuilder->weld_button("ok"))
, m_xResetBtn(m_xBuilder->weld_button("reset"))
{
m_xTabCtrl->connect_enter_page(LINK(this, MacroSecurity, ActivatePageHdl));
m_xLevelTP.reset(new MacroSecurityLevelTP(m_xTabCtrl->get_page("SecurityLevelPage"), this));
m_xTrustSrcTP.reset(new MacroSecurityTrustedSourcesTP(m_xTabCtrl->get_page("SecurityTrustPage"), this));
m_xTabCtrl->set_current_page("SecurityLevelPage");
m_xOkBtn->connect_clicked(LINK(this, MacroSecurity, OkBtnHdl));
}
IMPL_LINK(MacroSecurity, ActivatePageHdl, const OString&, rPage, void)
{
if (rPage == "SecurityLevelPage")
m_xLevelTP->ActivatePage();
else if (rPage == "SecurityTrustPage")
m_xTrustSrcTP->ActivatePage();
}
MacroSecurityTP::MacroSecurityTP(weld::Container* pParent, const OUString& rUIXMLDescription,
const OString& rID, MacroSecurity* pDlg)
: m_xBuilder(Application::CreateBuilder(pParent, rUIXMLDescription))
, m_xContainer(m_xBuilder->weld_container(rID))
, m_pDlg(pDlg)
{
}
void MacroSecurityTP::ActivatePage()
{
}
MacroSecurityTP::~MacroSecurityTP()
{
}
MacroSecurityLevelTP::MacroSecurityLevelTP(weld::Container* pParent, MacroSecurity* pDlg)
: MacroSecurityTP(pParent, "xmlsec/ui/securitylevelpage.ui", "SecurityLevelPage", pDlg)
, m_xVeryHighRB(m_xBuilder->weld_radio_button("vhigh"))
, m_xHighRB(m_xBuilder->weld_radio_button("high"))
, m_xMediumRB(m_xBuilder->weld_radio_button("med"))
, m_xLowRB(m_xBuilder->weld_radio_button("low"))
, m_xVHighImg(m_xBuilder->weld_widget("vhighimg"))
, m_xHighImg(m_xBuilder->weld_widget("highimg"))
, m_xMedImg(m_xBuilder->weld_widget("medimg"))
, m_xLowImg(m_xBuilder->weld_widget("lowimg"))
{
m_xLowRB->connect_toggled( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
m_xMediumRB->connect_toggled( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
m_xHighRB->connect_toggled( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
m_xVeryHighRB->connect_toggled( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
int nPrefWidth(std::max({m_xVeryHighRB->get_preferred_size().Width(),
m_xHighRB->get_preferred_size().Width(),
m_xMediumRB->get_preferred_size().Width(),
m_xLowRB->get_preferred_size().Width()}));
int nMaxWidth = m_xLowRB->get_approximate_digit_width() * 60;
if (nPrefWidth > nMaxWidth)
{
m_xLowRB->set_label_line_wrap(true);
m_xLowRB->set_size_request(nMaxWidth, -1);
m_xMediumRB->set_label_line_wrap(true);
m_xMediumRB->set_size_request(nMaxWidth, -1);
m_xHighRB->set_label_line_wrap(true);
m_xHighRB->set_size_request(nMaxWidth, -1);
m_xVeryHighRB->set_label_line_wrap(true);
m_xVeryHighRB->set_size_request(nMaxWidth, -1);
}
mnCurLevel = static_cast<sal_uInt16>(m_pDlg->m_aSecOptions.GetMacroSecurityLevel());
bool bReadonly = m_pDlg->m_aSecOptions.IsReadOnly( SvtSecurityOptions::EOption::MacroSecLevel );
weld::RadioButton* pCheck = nullptr;
weld::Widget* pImage = nullptr;
switch (mnCurLevel)
{
case 3:
pCheck = m_xVeryHighRB.get();
pImage = m_xVHighImg.get();
break;
case 2:
pCheck = m_xHighRB.get();
pImage = m_xHighImg.get();
break;
case 1:
pCheck = m_xMediumRB.get();
pImage = m_xMedImg.get();
break;
case 0:
pCheck = m_xLowRB.get();
pImage = m_xLowImg.get();
break;
}
if (pCheck)
pCheck->set_active(true);
else
{
OSL_FAIL("illegal macro security level");
}
if (bReadonly && pImage)
{
pImage->show();
m_xVeryHighRB->set_sensitive(false);
m_xHighRB->set_sensitive(false);
m_xMediumRB->set_sensitive(false);
m_xLowRB->set_sensitive(false);
}
}
IMPL_LINK_NOARG(MacroSecurityLevelTP, RadioButtonHdl, weld::ToggleButton&, void)
{
sal_uInt16 nNewLevel = 0;
if( m_xVeryHighRB->get_active() )
nNewLevel = 3;
else if( m_xHighRB->get_active() )
nNewLevel = 2;
else if( m_xMediumRB->get_active() )
nNewLevel = 1;
if ( nNewLevel != mnCurLevel )
{
mnCurLevel = nNewLevel;
m_pDlg->EnableReset();
}
}
void MacroSecurityLevelTP::ClosePage()
{
m_pDlg->m_aSecOptions.SetMacroSecurityLevel( mnCurLevel );
}
void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
{
bool bCertSelected = m_xTrustCertLB->get_selected_index() != -1;
m_xViewCertPB->set_sensitive( bCertSelected );
m_xRemoveCertPB->set_sensitive( bCertSelected && !mbAuthorsReadonly);
bool bLocationSelected = m_xTrustFileLocLB->get_selected_index() != -1;
m_xRemoveLocPB->set_sensitive( bLocationSelected && !mbURLsReadonly);
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, ViewCertPBHdl, weld::Button&, void)
{
int nEntry = m_xTrustCertLB->get_selected_index();
if (nEntry != -1)
{
sal_uInt16 nSelected = m_xTrustCertLB->get_id(nEntry).toUInt32();
uno::Reference< css::security::XCertificate > xCert = m_pDlg->m_xSecurityEnvironment->getCertificate( m_aTrustedAuthors[nSelected][0], xmlsecurity::numericStringToBigInteger( m_aTrustedAuthors[nSelected][1] ) );
// If we don't get it, create it from signature data:
if ( !xCert.is() )
xCert = m_pDlg->m_xSecurityEnvironment->createCertificateFromAscii( m_aTrustedAuthors[nSelected][2] ) ;
SAL_WARN_IF( !xCert.is(), "xmlsecurity.dialogs", "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
if ( xCert.is() )
{
CertificateViewer aViewer(m_pDlg->getDialog(), m_pDlg->m_xSecurityEnvironment, xCert, false, nullptr);
aViewer.run();
}
}
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveCertPBHdl, weld::Button&, void)
{
int nEntry = m_xTrustCertLB->get_selected_index();
if (nEntry != -1)
{
sal_uInt16 nAuthor = m_xTrustCertLB->get_id(nEntry).toUInt32();
::comphelper::removeElementAt( m_aTrustedAuthors, nAuthor );
FillCertLB();
ImplCheckButtons();
}
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, AddLocPBHdl, weld::Button&, void)
{
try
{
uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
short nRet = xFolderPicker->execute();
if( ui::dialogs::ExecutableDialogResults::OK != nRet )
return;
OUString aPathStr = xFolderPicker->getDirectory();
INetURLObject aNewObj( aPathStr );
aNewObj.removeFinalSlash();
// then the new path also an URL else system path
OUString aSystemFileURL = ( aNewObj.GetProtocol() != INetProtocol::NotValid ) ?
aPathStr : aNewObj.getFSysPath( FSysStyle::Detect );
OUString aNewPathStr(aSystemFileURL);
if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None )
aNewPathStr = aSystemFileURL;
if (m_xTrustFileLocLB->find_text(aNewPathStr) == -1)
m_xTrustFileLocLB->append_text(aNewPathStr);
ImplCheckButtons();
}
catch( uno::Exception& )
{
SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
}
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveLocPBHdl, weld::Button&, void)
{
sal_Int32 nSel = m_xTrustFileLocLB->get_selected_index();
if (nSel != -1)
{
m_xTrustFileLocLB->remove(nSel);
// Trusted Path could not be removed (#i33584#)
// after remove an entry, select another one if exists
int nNewCount = m_xTrustFileLocLB->n_children();
if (nNewCount > 0)
{
if (nSel >= nNewCount)
nSel = nNewCount - 1;
m_xTrustFileLocLB->select(nSel);
}
ImplCheckButtons();
}
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl, weld::TreeView&, void)
{
ImplCheckButtons();
}
IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl, weld::TreeView&, void)
{
ImplCheckButtons();
}
void MacroSecurityTrustedSourcesTP::FillCertLB()
{
m_xTrustCertLB->clear();
sal_uInt32 nEntries = m_aTrustedAuthors.getLength();
if ( nEntries && m_pDlg->m_xSecurityEnvironment.is() )
{
for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry )
{
css::uno::Sequence< OUString >& rEntry = m_aTrustedAuthors[ nEntry ];
uno::Reference< css::security::XCertificate > xCert;
// create from RawData
xCert = m_pDlg->m_xSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] );
m_xTrustCertLB->append(OUString::number(nEntry), xmlsec::GetContentPart(xCert->getSubjectName()));
m_xTrustCertLB->set_text(nEntry, xmlsec::GetContentPart(xCert->getIssuerName()), 1);
m_xTrustCertLB->set_text(nEntry, utl::GetDateTimeString(xCert->getNotValidAfter()), 2);
}
}
}
MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(weld::Container* pParent, MacroSecurity* pDlg)
: MacroSecurityTP(pParent, "xmlsec/ui/securitytrustpage.ui", "SecurityTrustPage", pDlg)
, m_xTrustCertROFI(m_xBuilder->weld_image("lockcertimg"))
, m_xTrustCertLB(m_xBuilder->weld_tree_view("certificates"))
, m_xViewCertPB(m_xBuilder->weld_button("viewcert"))
, m_xRemoveCertPB(m_xBuilder->weld_button("removecert"))
, m_xTrustFileROFI(m_xBuilder->weld_image("lockfileimg"))
, m_xTrustFileLocLB(m_xBuilder->weld_tree_view("locations"))
, m_xAddLocPB(m_xBuilder->weld_button("addfile"))
, m_xRemoveLocPB(m_xBuilder->weld_button("removefile"))
{
auto nColWidth = m_xTrustCertLB->get_approximate_digit_width() * 12;
std::vector<int> aWidths;
aWidths.push_back(nColWidth * 2);
aWidths.push_back(nColWidth * 2);
m_xTrustCertLB->set_column_fixed_widths(aWidths);
m_xTrustCertLB->set_size_request(nColWidth * 5.5, m_xTrustCertLB->get_height_rows(5));
m_xTrustCertLB->connect_changed( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) );
m_xViewCertPB->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) );
m_xViewCertPB->set_sensitive(false);
m_xRemoveCertPB->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) );
m_xRemoveCertPB->set_sensitive(false);
m_xTrustFileLocLB->connect_changed( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) );
m_xTrustFileLocLB->set_size_request(nColWidth * 5, m_xTrustFileLocLB->get_height_rows(5));
m_xAddLocPB->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) );
m_xRemoveLocPB->connect_clicked( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) );
m_xRemoveLocPB->set_sensitive(false);
m_aTrustedAuthors = m_pDlg->m_aSecOptions.GetTrustedAuthors();
mbAuthorsReadonly = m_pDlg->m_aSecOptions.IsReadOnly( SvtSecurityOptions::EOption::MacroTrustedAuthors );
m_xTrustCertROFI->show(mbAuthorsReadonly);
m_xTrustCertLB->set_sensitive(!mbAuthorsReadonly);
FillCertLB();
css::uno::Sequence< OUString > aSecureURLs = m_pDlg->m_aSecOptions.GetSecureURLs();
mbURLsReadonly = m_pDlg->m_aSecOptions.IsReadOnly( SvtSecurityOptions::EOption::SecureUrls );
m_xTrustFileROFI->show(mbURLsReadonly);
m_xTrustFileLocLB->set_sensitive(!mbURLsReadonly);
m_xAddLocPB->set_sensitive(!mbURLsReadonly);
sal_Int32 nEntryCnt = aSecureURLs.getLength();
for (sal_Int32 i = 0; i < nEntryCnt; ++i)
{
OUString aSystemFileURL( aSecureURLs[ i ] );
osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
m_xTrustFileLocLB->append_text(aSystemFileURL);
}
}
void MacroSecurityTrustedSourcesTP::ActivatePage()
{
m_pDlg->EnableReset( false );
FillCertLB();
}
void MacroSecurityTrustedSourcesTP::ClosePage()
{
sal_Int32 nEntryCnt = m_xTrustFileLocLB->n_children();
if( nEntryCnt )
{
css::uno::Sequence< OUString > aSecureURLs( nEntryCnt );
for (sal_Int32 i = 0; i < nEntryCnt; ++i)
{
OUString aURL(m_xTrustFileLocLB->get_text(i));
osl::FileBase::getFileURLFromSystemPath( aURL, aURL );
aSecureURLs[ i ] = aURL;
}
m_pDlg->m_aSecOptions.SetSecureURLs( aSecureURLs );
}
// Trusted Path could not be removed (#i33584#)
// don't forget to remove the old saved SecureURLs
else
m_pDlg->m_aSecOptions.SetSecureURLs( css::uno::Sequence< OUString >() );
m_pDlg->m_aSecOptions.SetTrustedAuthors( m_aTrustedAuthors );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|