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