Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <xmlsecurity/macrosecurity.hxx>
22 : #include <xmlsecurity/certificatechooser.hxx>
23 : #include <xmlsecurity/certificateviewer.hxx>
24 : #include <xmlsecurity/biginteger.hxx>
25 :
26 : #include <osl/file.hxx>
27 : #include <vcl/help.hxx>
28 : #include <vcl/layout.hxx>
29 :
30 :
31 : #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
32 : #include <com/sun/star/security/SerialNumberAdapter.hpp>
33 : #include <comphelper/sequence.hxx>
34 : #include <sfx2/filedlghelper.hxx>
35 : #include <comphelper/processfactory.hxx>
36 : #include <com/sun/star/uno/Exception.hpp>
37 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 : #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
39 : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
40 : #include <tools/urlobj.hxx>
41 : #include <svtools/treelistentry.hxx>
42 :
43 : #include <vcl/msgbox.hxx>
44 :
45 : #include "dialogs.hrc"
46 : #include "resourcemanager.hxx"
47 :
48 : using namespace ::com::sun::star;
49 :
50 :
51 0 : IMPL_LINK_NOARG(MacroSecurity, OkBtnHdl)
52 : {
53 0 : mpLevelTP->ClosePage();
54 0 : mpTrustSrcTP->ClosePage();
55 :
56 0 : EndDialog( RET_OK );
57 :
58 0 : return 0;
59 : }
60 :
61 0 : MacroSecurity::MacroSecurity( vcl::Window* _pParent,
62 : const css::uno::Reference< css::uno::XComponentContext> &_rxCtx,
63 : const css::uno::Reference< css::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment)
64 : : TabDialog(_pParent, "MacroSecurityDialog", "xmlsec/ui/macrosecuritydialog.ui")
65 : , mxCtx(_rxCtx)
66 0 : , mxSecurityEnvironment(_rxSecurityEnvironment)
67 : {
68 0 : get(m_pTabCtrl, "tabcontrol");
69 0 : get(m_pResetBtn, "reset");
70 0 : get(m_pOkBtn, "ok");
71 :
72 0 : mpLevelTP.reset(VclPtr<MacroSecurityLevelTP>::Create(m_pTabCtrl, this));
73 0 : mpTrustSrcTP.reset(VclPtr<MacroSecurityTrustedSourcesTP>::Create(m_pTabCtrl, this));
74 :
75 0 : m_nSecLevelId = m_pTabCtrl->GetPageId("SecurityLevelPage");
76 0 : m_nSecTrustId = m_pTabCtrl->GetPageId("SecurityTrustPage");
77 :
78 0 : m_pTabCtrl->SetTabPage(m_nSecLevelId, mpLevelTP);
79 0 : m_pTabCtrl->SetTabPage(m_nSecTrustId, mpTrustSrcTP);
80 0 : m_pTabCtrl->SetCurPageId(m_nSecLevelId);
81 :
82 0 : m_pOkBtn->SetClickHdl( LINK( this, MacroSecurity, OkBtnHdl ) );
83 0 : }
84 :
85 0 : MacroSecurity::~MacroSecurity()
86 : {
87 0 : disposeOnce();
88 0 : }
89 :
90 0 : void MacroSecurity::dispose()
91 : {
92 0 : m_pTabCtrl->GetTabPage(m_nSecTrustId)->disposeOnce();
93 0 : m_pTabCtrl->GetTabPage(m_nSecLevelId)->disposeOnce();
94 0 : m_pTabCtrl.clear();
95 0 : m_pOkBtn.clear();
96 0 : m_pResetBtn.clear();
97 0 : mpLevelTP.disposeAndClear();
98 0 : mpTrustSrcTP.disposeAndClear();
99 0 : TabDialog::dispose();
100 0 : }
101 :
102 0 : MacroSecurityTP::MacroSecurityTP(vcl::Window* _pParent, const OString& rID,
103 : const OUString& rUIXMLDescription, MacroSecurity* _pDlg)
104 : : TabPage(_pParent, rID, rUIXMLDescription)
105 0 : , mpDlg(_pDlg)
106 : {
107 0 : }
108 :
109 0 : MacroSecurityTP::~MacroSecurityTP()
110 : {
111 0 : disposeOnce();
112 0 : }
113 :
114 0 : void MacroSecurityTP::dispose()
115 : {
116 0 : mpDlg.clear();
117 0 : TabPage::dispose();
118 0 : }
119 :
120 0 : MacroSecurityLevelTP::MacroSecurityLevelTP(vcl::Window* _pParent, MacroSecurity* _pDlg)
121 0 : : MacroSecurityTP(_pParent, "SecurityLevelPage", "xmlsec/ui/securitylevelpage.ui", _pDlg)
122 : {
123 0 : get(m_pVeryHighRB, "vhigh");
124 0 : get(m_pHighRB, "high");
125 0 : get(m_pMediumRB, "med");
126 0 : get(m_pLowRB, "low");
127 :
128 0 : m_pLowRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
129 0 : m_pMediumRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
130 0 : m_pHighRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
131 0 : m_pVeryHighRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
132 :
133 0 : mnCurLevel = (sal_uInt16) mpDlg->maSecOptions.GetMacroSecurityLevel();
134 0 : bool bReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL );
135 :
136 0 : RadioButton* pCheck = 0;
137 0 : FixedImage* pImage = 0;
138 0 : switch (mnCurLevel)
139 : {
140 : case 3:
141 0 : pCheck = m_pVeryHighRB;
142 0 : pImage = get<FixedImage>("vhighimg");
143 0 : break;
144 : case 2:
145 0 : pCheck = m_pHighRB;
146 0 : pImage = get<FixedImage>("highimg");
147 0 : break;
148 : case 1:
149 0 : pCheck = m_pMediumRB;
150 0 : pImage = get<FixedImage>("medimg");
151 0 : break;
152 : case 0:
153 0 : pCheck = m_pLowRB;
154 0 : pImage = get<FixedImage>("lowimg");
155 0 : break;
156 : }
157 0 : if (pCheck)
158 0 : pCheck->Check();
159 : else
160 : {
161 : OSL_FAIL("illegal macro security level");
162 : }
163 0 : if (bReadonly && pImage)
164 : {
165 0 : pImage->Show(true);
166 0 : m_pVeryHighRB->Enable(false);
167 0 : m_pHighRB->Enable(false);
168 0 : m_pMediumRB->Enable(false);
169 0 : m_pLowRB->Enable(false);
170 : }
171 0 : }
172 :
173 0 : MacroSecurityLevelTP::~MacroSecurityLevelTP()
174 : {
175 0 : disposeOnce();
176 0 : }
177 :
178 0 : void MacroSecurityLevelTP::dispose()
179 : {
180 0 : m_pVeryHighRB.clear();
181 0 : m_pHighRB.clear();
182 0 : m_pMediumRB.clear();
183 0 : m_pLowRB.clear();
184 0 : MacroSecurityTP::dispose();
185 0 : }
186 :
187 0 : IMPL_LINK_NOARG(MacroSecurityLevelTP, RadioButtonHdl)
188 : {
189 0 : sal_uInt16 nNewLevel = 0;
190 0 : if( m_pVeryHighRB->IsChecked() )
191 0 : nNewLevel = 3;
192 0 : else if( m_pHighRB->IsChecked() )
193 0 : nNewLevel = 2;
194 0 : else if( m_pMediumRB->IsChecked() )
195 0 : nNewLevel = 1;
196 :
197 0 : if ( nNewLevel != mnCurLevel )
198 : {
199 0 : mnCurLevel = nNewLevel;
200 0 : mpDlg->EnableReset();
201 : }
202 :
203 0 : return 0;
204 : }
205 :
206 0 : void MacroSecurityLevelTP::ClosePage()
207 : {
208 0 : mpDlg->maSecOptions.SetMacroSecurityLevel( mnCurLevel );
209 0 : }
210 :
211 0 : void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
212 : {
213 0 : bool bCertSelected = m_pTrustCertLB->FirstSelected() != NULL;
214 0 : m_pViewCertPB->Enable( bCertSelected );
215 0 : m_pRemoveCertPB->Enable( bCertSelected && !mbAuthorsReadonly);
216 :
217 0 : bool bLocationSelected = m_pTrustFileLocLB->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND;
218 0 : m_pRemoveLocPB->Enable( bLocationSelected && !mbURLsReadonly);
219 0 : }
220 :
221 :
222 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, ViewCertPBHdl)
223 : {
224 0 : if( m_pTrustCertLB->FirstSelected() )
225 : {
226 0 : sal_uInt16 nSelected = sal_uInt16( sal_uIntPtr( m_pTrustCertLB->FirstSelected()->GetUserData() ) );
227 :
228 : uno::Reference< css::security::XSerialNumberAdapter > xSerialNumberAdapter =
229 0 : ::com::sun::star::security::SerialNumberAdapter::create(mpDlg->mxCtx);
230 :
231 0 : uno::Reference< css::security::XCertificate > xCert = mpDlg->mxSecurityEnvironment->getCertificate( maTrustedAuthors[nSelected][0], xSerialNumberAdapter->toSequence( maTrustedAuthors[nSelected][1] ) );
232 :
233 : // If we don't get it, create it from signature data:
234 0 : if ( !xCert.is() )
235 0 : xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ;
236 :
237 : DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
238 :
239 0 : if ( xCert.is() )
240 : {
241 0 : ScopedVclPtrInstance< CertificateViewer > aViewer( this, mpDlg->mxSecurityEnvironment, xCert, false );
242 0 : aViewer->Execute();
243 0 : }
244 : }
245 0 : return 0;
246 : }
247 :
248 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveCertPBHdl)
249 : {
250 0 : if( m_pTrustCertLB->FirstSelected() )
251 : {
252 0 : sal_uInt16 nAuthor = sal_uInt16( sal_uIntPtr( m_pTrustCertLB->FirstSelected()->GetUserData() ) );
253 0 : ::comphelper::removeElementAt( maTrustedAuthors, nAuthor );
254 :
255 0 : FillCertLB();
256 0 : ImplCheckButtons();
257 : }
258 :
259 0 : return 0;
260 : }
261 :
262 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, AddLocPBHdl)
263 : {
264 : try
265 : {
266 0 : uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
267 0 : uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
268 :
269 0 : short nRet = xFolderPicker->execute();
270 :
271 0 : if( ui::dialogs::ExecutableDialogResults::OK != nRet )
272 0 : return 0;
273 :
274 0 : OUString aPathStr = xFolderPicker->getDirectory();
275 0 : INetURLObject aNewObj( aPathStr );
276 0 : aNewObj.removeFinalSlash();
277 :
278 : // then the new path also an URL else system path
279 0 : OUString aSystemFileURL = ( aNewObj.GetProtocol() != INetProtocol::NotValid ) ?
280 0 : aPathStr : aNewObj.getFSysPath( INetURLObject::FSYS_DETECT );
281 :
282 0 : OUString aNewPathStr(aSystemFileURL);
283 :
284 0 : if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None )
285 0 : aNewPathStr = aSystemFileURL;
286 :
287 0 : if( m_pTrustFileLocLB->GetEntryPos( aNewPathStr ) == LISTBOX_ENTRY_NOTFOUND )
288 : {
289 0 : m_pTrustFileLocLB->InsertEntry( aNewPathStr );
290 : }
291 :
292 0 : ImplCheckButtons();
293 : }
294 0 : catch( uno::Exception& )
295 : {
296 : SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
297 : }
298 :
299 0 : return 0;
300 : }
301 :
302 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveLocPBHdl)
303 : {
304 0 : sal_Int32 nSel = m_pTrustFileLocLB->GetSelectEntryPos();
305 0 : if( nSel != LISTBOX_ENTRY_NOTFOUND )
306 : {
307 0 : m_pTrustFileLocLB->RemoveEntry( nSel );
308 : // Trusted Path could not be removed (#i33584#)
309 : // after remove an entry, select another one if exists
310 0 : sal_Int32 nNewCount = m_pTrustFileLocLB->GetEntryCount();
311 0 : if ( nNewCount > 0 )
312 : {
313 0 : if ( nSel >= nNewCount )
314 0 : nSel = nNewCount - 1;
315 0 : m_pTrustFileLocLB->SelectEntryPos( nSel );
316 : }
317 0 : ImplCheckButtons();
318 : }
319 :
320 0 : return 0;
321 : }
322 :
323 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl)
324 : {
325 0 : ImplCheckButtons();
326 0 : return 0;
327 : }
328 :
329 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl)
330 : {
331 0 : ImplCheckButtons();
332 0 : return 0;
333 : }
334 :
335 0 : void MacroSecurityTrustedSourcesTP::FillCertLB()
336 : {
337 0 : m_pTrustCertLB->Clear();
338 :
339 0 : sal_uInt32 nEntries = maTrustedAuthors.getLength();
340 :
341 0 : if ( nEntries && mpDlg->mxSecurityEnvironment.is() )
342 : {
343 0 : for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry )
344 : {
345 0 : css::uno::Sequence< OUString >& rEntry = maTrustedAuthors[ nEntry ];
346 0 : uno::Reference< css::security::XCertificate > xCert;
347 :
348 : // create from RawData
349 0 : xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] );
350 :
351 0 : SvTreeListEntry* pLBEntry = m_pTrustCertLB->InsertEntry( XmlSec::GetContentPart( xCert->getSubjectName() ) );
352 0 : m_pTrustCertLB->SetEntryText( XmlSec::GetContentPart( xCert->getIssuerName() ), pLBEntry, 1 );
353 0 : m_pTrustCertLB->SetEntryText( XmlSec::GetDateTimeString( xCert->getNotValidAfter() ), pLBEntry, 2 );
354 0 : pLBEntry->SetUserData( reinterpret_cast<void*>(nEntry) ); // missuse user data as index
355 0 : }
356 : }
357 0 : }
358 :
359 0 : class TrustCertLB : public SvSimpleTable
360 : {
361 : public:
362 0 : explicit TrustCertLB(SvSimpleTableContainer &rContainer)
363 0 : : SvSimpleTable(rContainer, 0)
364 : {
365 0 : }
366 0 : virtual void Resize() SAL_OVERRIDE
367 : {
368 0 : SvSimpleTable::Resize();
369 0 : if (isInitialLayout(this))
370 : {
371 0 : const long nControlWidth = GetSizePixel().Width();
372 0 : long aTabLocs[] = { 3, 0, 35*nControlWidth/100, 70*nControlWidth/100 };
373 0 : SvSimpleTable::SetTabs(aTabLocs, MAP_PIXEL);
374 : }
375 0 : }
376 : };
377 :
378 0 : MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(vcl::Window* _pParent, MacroSecurity* _pDlg)
379 0 : : MacroSecurityTP(_pParent, "SecurityTrustPage", "xmlsec/ui/securitytrustpage.ui", _pDlg)
380 : {
381 0 : get(m_pTrustCertROFI, "lockcertimg");
382 0 : get(m_pViewCertPB, "viewcert");
383 0 : get(m_pRemoveCertPB, "removecert");
384 0 : get(m_pTrustFileROFI, "lockfileimg");
385 0 : get(m_pTrustFileLocLB, "locations");
386 0 : m_pTrustFileLocLB->SetDropDownLineCount(6);
387 0 : get(m_pAddLocPB, "addfile");
388 0 : get(m_pRemoveLocPB, "removefile");
389 :
390 0 : SvSimpleTableContainer *pCertificates = get<SvSimpleTableContainer>("certificates");
391 0 : m_pTrustCertLB.reset(VclPtr<TrustCertLB>::Create(*pCertificates));
392 : static long aTabs[] = { 3, 0, 0, 0 };
393 0 : m_pTrustCertLB->SetTabs( aTabs );
394 :
395 0 : m_pTrustCertLB->InsertHeaderEntry(get<FixedText>("to")->GetText() + "\t"
396 0 : + get<FixedText>("by")->GetText() + "\t" + get<FixedText>("date")->GetText());
397 :
398 0 : m_pTrustCertLB->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) );
399 0 : m_pViewCertPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) );
400 0 : m_pViewCertPB->Disable();
401 0 : m_pRemoveCertPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) );
402 0 : m_pRemoveCertPB->Disable();
403 :
404 0 : m_pTrustFileLocLB->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) );
405 0 : m_pAddLocPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) );
406 0 : m_pRemoveLocPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) );
407 0 : m_pRemoveLocPB->Disable();
408 :
409 0 : maTrustedAuthors = mpDlg->maSecOptions.GetTrustedAuthors();
410 0 : mbAuthorsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS );
411 0 : m_pTrustCertROFI->Show( mbAuthorsReadonly );
412 0 : mbAuthorsReadonly ? m_pTrustCertLB->DisableTable() : m_pTrustCertLB->EnableTable();
413 :
414 0 : FillCertLB();
415 :
416 0 : css::uno::Sequence< OUString > aSecureURLs = mpDlg->maSecOptions.GetSecureURLs();
417 0 : mbURLsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_SECUREURLS );
418 0 : m_pTrustFileROFI->Show( mbURLsReadonly );
419 0 : m_pTrustFileLocLB->Enable( !mbURLsReadonly );
420 0 : m_pAddLocPB->Enable( !mbURLsReadonly );
421 :
422 0 : sal_Int32 nEntryCnt = aSecureURLs.getLength();
423 0 : for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
424 : {
425 0 : OUString aSystemFileURL( aSecureURLs[ i ] );
426 0 : osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
427 0 : m_pTrustFileLocLB->InsertEntry( aSystemFileURL );
428 0 : }
429 0 : }
430 :
431 0 : MacroSecurityTrustedSourcesTP::~MacroSecurityTrustedSourcesTP()
432 : {
433 0 : disposeOnce();
434 0 : }
435 :
436 0 : void MacroSecurityTrustedSourcesTP::dispose()
437 : {
438 0 : m_pTrustCertLB.disposeAndClear();
439 0 : m_pTrustCertROFI.clear();
440 0 : m_pViewCertPB.clear();
441 0 : m_pRemoveCertPB.clear();
442 0 : m_pTrustFileROFI.clear();
443 0 : m_pTrustFileLocLB.clear();
444 0 : m_pAddLocPB.clear();
445 0 : m_pRemoveLocPB.clear();
446 0 : MacroSecurityTP::dispose();
447 0 : }
448 :
449 0 : void MacroSecurityTrustedSourcesTP::ActivatePage()
450 : {
451 0 : mpDlg->EnableReset( false );
452 0 : FillCertLB();
453 0 : }
454 :
455 0 : void MacroSecurityTrustedSourcesTP::ClosePage()
456 : {
457 0 : sal_Int32 nEntryCnt = m_pTrustFileLocLB->GetEntryCount();
458 0 : if( nEntryCnt )
459 : {
460 0 : css::uno::Sequence< OUString > aSecureURLs( nEntryCnt );
461 0 : for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
462 : {
463 0 : OUString aURL( m_pTrustFileLocLB->GetEntry( i ) );
464 0 : osl::FileBase::getFileURLFromSystemPath( aURL, aURL );
465 0 : aSecureURLs[ i ] = aURL;
466 0 : }
467 :
468 0 : mpDlg->maSecOptions.SetSecureURLs( aSecureURLs );
469 : }
470 : // Trusted Path could not be removed (#i33584#)
471 : // don't forget to remove the old saved SecureURLs
472 : else
473 0 : mpDlg->maSecOptions.SetSecureURLs( css::uno::Sequence< OUString >() );
474 :
475 0 : mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors );
476 114 : }
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|