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 = new MacroSecurityLevelTP(m_pTabCtrl, this);
73 0 : mpTrustSrcTP = new MacroSecurityTrustedSourcesTP(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 : delete m_pTabCtrl->GetTabPage(m_nSecTrustId);
88 0 : delete m_pTabCtrl->GetTabPage(m_nSecLevelId);
89 0 : }
90 :
91 0 : MacroSecurityTP::MacroSecurityTP(vcl::Window* _pParent, const OString& rID,
92 : const OUString& rUIXMLDescription, MacroSecurity* _pDlg)
93 : : TabPage(_pParent, rID, rUIXMLDescription)
94 0 : , mpDlg(_pDlg)
95 : {
96 0 : }
97 :
98 0 : MacroSecurityLevelTP::MacroSecurityLevelTP(vcl::Window* _pParent, MacroSecurity* _pDlg)
99 0 : : MacroSecurityTP(_pParent, "SecurityLevelPage", "xmlsec/ui/securitylevelpage.ui", _pDlg)
100 : {
101 0 : get(m_pVeryHighRB, "vhigh");
102 0 : get(m_pHighRB, "high");
103 0 : get(m_pMediumRB, "med");
104 0 : get(m_pLowRB, "low");
105 :
106 0 : m_pLowRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
107 0 : m_pMediumRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
108 0 : m_pHighRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
109 0 : m_pVeryHighRB->SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
110 :
111 0 : mnCurLevel = (sal_uInt16) mpDlg->maSecOptions.GetMacroSecurityLevel();
112 0 : bool bReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL );
113 :
114 0 : RadioButton* pCheck = 0;
115 0 : FixedImage* pImage = 0;
116 0 : switch (mnCurLevel)
117 : {
118 : case 3:
119 0 : pCheck = m_pVeryHighRB;
120 0 : pImage = get<FixedImage>("vhighimg");
121 0 : break;
122 : case 2:
123 0 : pCheck = m_pHighRB;
124 0 : pImage = get<FixedImage>("highimg");
125 0 : break;
126 : case 1:
127 0 : pCheck = m_pMediumRB;
128 0 : pImage = get<FixedImage>("medimg");
129 0 : break;
130 : case 0:
131 0 : pCheck = m_pLowRB;
132 0 : pImage = get<FixedImage>("lowimg");
133 0 : break;
134 : }
135 0 : if (pCheck)
136 0 : pCheck->Check();
137 : else
138 : {
139 : OSL_FAIL("illegal macro security level");
140 : }
141 0 : if (bReadonly && pImage)
142 : {
143 0 : pImage->Show(true);
144 0 : m_pVeryHighRB->Enable(false);
145 0 : m_pHighRB->Enable(false);
146 0 : m_pMediumRB->Enable(false);
147 0 : m_pLowRB->Enable(false);
148 : }
149 0 : }
150 :
151 0 : IMPL_LINK_NOARG(MacroSecurityLevelTP, RadioButtonHdl)
152 : {
153 0 : sal_uInt16 nNewLevel = 0;
154 0 : if( m_pVeryHighRB->IsChecked() )
155 0 : nNewLevel = 3;
156 0 : else if( m_pHighRB->IsChecked() )
157 0 : nNewLevel = 2;
158 0 : else if( m_pMediumRB->IsChecked() )
159 0 : nNewLevel = 1;
160 :
161 0 : if ( nNewLevel != mnCurLevel )
162 : {
163 0 : mnCurLevel = nNewLevel;
164 0 : mpDlg->EnableReset();
165 : }
166 :
167 0 : return 0;
168 : }
169 :
170 0 : void MacroSecurityLevelTP::ClosePage( void )
171 : {
172 0 : mpDlg->maSecOptions.SetMacroSecurityLevel( mnCurLevel );
173 0 : }
174 :
175 0 : void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
176 : {
177 0 : bool bCertSelected = m_pTrustCertLB->FirstSelected() != NULL;
178 0 : m_pViewCertPB->Enable( bCertSelected );
179 0 : m_pRemoveCertPB->Enable( bCertSelected && !mbAuthorsReadonly);
180 :
181 0 : bool bLocationSelected = m_pTrustFileLocLB->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND;
182 0 : m_pRemoveLocPB->Enable( bLocationSelected && !mbURLsReadonly);
183 0 : }
184 :
185 :
186 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, ViewCertPBHdl)
187 : {
188 0 : if( m_pTrustCertLB->FirstSelected() )
189 : {
190 0 : sal_uInt16 nSelected = sal_uInt16( sal_uIntPtr( m_pTrustCertLB->FirstSelected()->GetUserData() ) );
191 :
192 : uno::Reference< css::security::XSerialNumberAdapter > xSerialNumberAdapter =
193 0 : ::com::sun::star::security::SerialNumberAdapter::create(mpDlg->mxCtx);
194 :
195 0 : uno::Reference< css::security::XCertificate > xCert = mpDlg->mxSecurityEnvironment->getCertificate( maTrustedAuthors[nSelected][0], xSerialNumberAdapter->toSequence( maTrustedAuthors[nSelected][1] ) );
196 :
197 : // If we don't get it, create it from signature data:
198 0 : if ( !xCert.is() )
199 0 : xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ;
200 :
201 : DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
202 :
203 0 : if ( xCert.is() )
204 : {
205 0 : CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, xCert, false );
206 0 : aViewer.Execute();
207 0 : }
208 : }
209 0 : return 0;
210 : }
211 :
212 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveCertPBHdl)
213 : {
214 0 : if( m_pTrustCertLB->FirstSelected() )
215 : {
216 0 : sal_uInt16 nAuthor = sal_uInt16( sal_uIntPtr( m_pTrustCertLB->FirstSelected()->GetUserData() ) );
217 0 : ::comphelper::removeElementAt( maTrustedAuthors, nAuthor );
218 :
219 0 : FillCertLB();
220 0 : ImplCheckButtons();
221 : }
222 :
223 0 : return 0;
224 : }
225 :
226 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, AddLocPBHdl)
227 : {
228 : try
229 : {
230 0 : uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
231 0 : uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
232 :
233 0 : short nRet = xFolderPicker->execute();
234 :
235 0 : if( ui::dialogs::ExecutableDialogResults::OK != nRet )
236 0 : return 0;
237 :
238 0 : OUString aPathStr = xFolderPicker->getDirectory();
239 0 : INetURLObject aNewObj( aPathStr );
240 0 : aNewObj.removeFinalSlash();
241 :
242 : // then the new path also an URL else system path
243 0 : OUString aSystemFileURL = ( aNewObj.GetProtocol() != INET_PROT_NOT_VALID ) ?
244 0 : aPathStr : aNewObj.getFSysPath( INetURLObject::FSYS_DETECT );
245 :
246 0 : OUString aNewPathStr(aSystemFileURL);
247 :
248 0 : if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None )
249 0 : aNewPathStr = aSystemFileURL;
250 :
251 0 : if( m_pTrustFileLocLB->GetEntryPos( aNewPathStr ) == LISTBOX_ENTRY_NOTFOUND )
252 : {
253 0 : m_pTrustFileLocLB->InsertEntry( aNewPathStr );
254 : }
255 :
256 0 : ImplCheckButtons();
257 : }
258 0 : catch( uno::Exception& )
259 : {
260 : SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
261 : }
262 :
263 0 : return 0;
264 : }
265 :
266 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, RemoveLocPBHdl)
267 : {
268 0 : sal_Int32 nSel = m_pTrustFileLocLB->GetSelectEntryPos();
269 0 : if( nSel != LISTBOX_ENTRY_NOTFOUND )
270 : {
271 0 : m_pTrustFileLocLB->RemoveEntry( nSel );
272 : // Trusted Path could not be removed (#i33584#)
273 : // after remove an entry, select another one if exists
274 0 : sal_Int32 nNewCount = m_pTrustFileLocLB->GetEntryCount();
275 0 : if ( nNewCount > 0 )
276 : {
277 0 : if ( nSel >= nNewCount )
278 0 : nSel = nNewCount - 1;
279 0 : m_pTrustFileLocLB->SelectEntryPos( nSel );
280 : }
281 0 : ImplCheckButtons();
282 : }
283 :
284 0 : return 0;
285 : }
286 :
287 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl)
288 : {
289 0 : ImplCheckButtons();
290 0 : return 0;
291 : }
292 :
293 0 : IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl)
294 : {
295 0 : ImplCheckButtons();
296 0 : return 0;
297 : }
298 :
299 0 : void MacroSecurityTrustedSourcesTP::FillCertLB( void )
300 : {
301 0 : m_pTrustCertLB->Clear();
302 :
303 0 : sal_uInt32 nEntries = maTrustedAuthors.getLength();
304 :
305 0 : if ( nEntries && mpDlg->mxSecurityEnvironment.is() )
306 : {
307 0 : for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry )
308 : {
309 0 : css::uno::Sequence< OUString >& rEntry = maTrustedAuthors[ nEntry ];
310 0 : uno::Reference< css::security::XCertificate > xCert;
311 :
312 : // create from RawData
313 0 : xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] );
314 :
315 0 : SvTreeListEntry* pLBEntry = m_pTrustCertLB->InsertEntry( XmlSec::GetContentPart( xCert->getSubjectName() ) );
316 0 : m_pTrustCertLB->SetEntryText( XmlSec::GetContentPart( xCert->getIssuerName() ), pLBEntry, 1 );
317 0 : m_pTrustCertLB->SetEntryText( XmlSec::GetDateTimeString( xCert->getNotValidAfter() ), pLBEntry, 2 );
318 0 : pLBEntry->SetUserData( reinterpret_cast<void*>(nEntry) ); // missuse user data as index
319 0 : }
320 : }
321 0 : }
322 :
323 0 : class TrustCertLB : public SvSimpleTable
324 : {
325 : public:
326 0 : TrustCertLB(SvSimpleTableContainer &rContainer)
327 0 : : SvSimpleTable(rContainer, 0)
328 : {
329 0 : }
330 0 : virtual void Resize() SAL_OVERRIDE
331 : {
332 0 : SvSimpleTable::Resize();
333 0 : if (isInitialLayout(this))
334 : {
335 0 : const long nControlWidth = GetSizePixel().Width();
336 0 : long aTabLocs[] = { 3, 0, 35*nControlWidth/100, 70*nControlWidth/100 };
337 0 : SvSimpleTable::SetTabs(aTabLocs, MAP_PIXEL);
338 : }
339 0 : }
340 : };
341 :
342 0 : MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(vcl::Window* _pParent, MacroSecurity* _pDlg)
343 0 : : MacroSecurityTP(_pParent, "SecurityTrustPage", "xmlsec/ui/securitytrustpage.ui", _pDlg)
344 : {
345 0 : get(m_pTrustCertROFI, "lockcertimg");
346 0 : get(m_pViewCertPB, "viewcert");
347 0 : get(m_pRemoveCertPB, "removecert");
348 0 : get(m_pTrustFileROFI, "lockfileimg");
349 0 : get(m_pTrustFileLocLB, "locations");
350 0 : m_pTrustFileLocLB->SetDropDownLineCount(6);
351 0 : get(m_pAddLocPB, "addfile");
352 0 : get(m_pRemoveLocPB, "removefile");
353 :
354 0 : SvSimpleTableContainer *pCertificates = get<SvSimpleTableContainer>("certificates");
355 0 : m_pTrustCertLB = new TrustCertLB(*pCertificates);
356 : static long aTabs[] = { 3, 0, 0, 0 };
357 0 : m_pTrustCertLB->SetTabs( aTabs );
358 :
359 0 : m_pTrustCertLB->InsertHeaderEntry(get<FixedText>("to")->GetText() + "\t"
360 0 : + get<FixedText>("by")->GetText() + "\t" + get<FixedText>("date")->GetText());
361 :
362 0 : m_pTrustCertLB->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) );
363 0 : m_pViewCertPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) );
364 0 : m_pViewCertPB->Disable();
365 0 : m_pRemoveCertPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) );
366 0 : m_pRemoveCertPB->Disable();
367 :
368 0 : m_pTrustFileLocLB->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) );
369 0 : m_pAddLocPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) );
370 0 : m_pRemoveLocPB->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) );
371 0 : m_pRemoveLocPB->Disable();
372 :
373 0 : maTrustedAuthors = mpDlg->maSecOptions.GetTrustedAuthors();
374 0 : mbAuthorsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS );
375 0 : m_pTrustCertROFI->Show( mbAuthorsReadonly );
376 0 : mbAuthorsReadonly ? m_pTrustCertLB->DisableTable() : m_pTrustCertLB->EnableTable();
377 :
378 0 : FillCertLB();
379 :
380 0 : css::uno::Sequence< OUString > aSecureURLs = mpDlg->maSecOptions.GetSecureURLs();
381 0 : mbURLsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_SECUREURLS );
382 0 : m_pTrustFileROFI->Show( mbURLsReadonly );
383 0 : m_pTrustFileLocLB->Enable( !mbURLsReadonly );
384 0 : m_pAddLocPB->Enable( !mbURLsReadonly );
385 :
386 0 : sal_Int32 nEntryCnt = aSecureURLs.getLength();
387 0 : for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
388 : {
389 0 : OUString aSystemFileURL( aSecureURLs[ i ] );
390 0 : osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
391 0 : m_pTrustFileLocLB->InsertEntry( aSystemFileURL );
392 0 : }
393 0 : }
394 :
395 0 : MacroSecurityTrustedSourcesTP::~MacroSecurityTrustedSourcesTP()
396 : {
397 0 : delete m_pTrustCertLB;
398 0 : }
399 :
400 0 : void MacroSecurityTrustedSourcesTP::ActivatePage()
401 : {
402 0 : mpDlg->EnableReset( false );
403 0 : FillCertLB();
404 0 : }
405 :
406 0 : void MacroSecurityTrustedSourcesTP::ClosePage( void )
407 : {
408 0 : sal_Int32 nEntryCnt = m_pTrustFileLocLB->GetEntryCount();
409 0 : if( nEntryCnt )
410 : {
411 0 : css::uno::Sequence< OUString > aSecureURLs( nEntryCnt );
412 0 : for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
413 : {
414 0 : OUString aURL( m_pTrustFileLocLB->GetEntry( i ) );
415 0 : osl::FileBase::getFileURLFromSystemPath( aURL, aURL );
416 0 : aSecureURLs[ i ] = aURL;
417 0 : }
418 :
419 0 : mpDlg->maSecOptions.SetSecureURLs( aSecureURLs );
420 : }
421 : // Trusted Path could not be removed (#i33584#)
422 : // don't forget to remove the old saved SecureURLs
423 : else
424 0 : mpDlg->maSecOptions.SetSecureURLs( css::uno::Sequence< OUString >() );
425 :
426 0 : mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors );
427 162 : }
428 :
429 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|