1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_xmlsecurity.hxx" 26 27 #include <xmlsecurity/macrosecurity.hxx> 28 #include <xmlsecurity/certificatechooser.hxx> 29 #include <xmlsecurity/certificateviewer.hxx> 30 #include <xmlsecurity/biginteger.hxx> 31 32 #include <osl/file.hxx> 33 #include <vcl/help.hxx> 34 35 36 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> 37 #include <com/sun/star/security/SerialNumberAdapter.hpp> 38 #include <comphelper/sequence.hxx> 39 #include <sfx2/filedlghelper.hxx> 40 #include <comphelper/processfactory.hxx> 41 #include <com/sun/star/uno/Exception.hpp> 42 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 43 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp> 44 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> 45 #include <tools/urlobj.hxx> 46 47 #include <vcl/msgbox.hxx> 48 49 #include "dialogs.hrc" 50 #include "resourcemanager.hxx" 51 52 /* HACK: disable some warnings for MS-C */ 53 #ifdef _MSC_VER 54 #pragma warning (disable : 4355) // 4355: this used in initializer-list 55 #endif 56 57 using namespace ::com::sun::star; 58 59 60 IMPL_LINK( MacroSecurity, OkBtnHdl, void*, EMPTYARG ) 61 { 62 mpLevelTP->ClosePage(); 63 mpTrustSrcTP->ClosePage(); 64 65 EndDialog( RET_OK ); 66 67 return 0; 68 } 69 70 MacroSecurity::MacroSecurity( Window* _pParent, const cssu::Reference< cssu::XComponentContext> &_rxCtx, const cssu::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment ) 71 :TabDialog ( _pParent, XMLSEC_RES( RID_XMLSECTP_MACROSEC ) ) 72 ,maTabCtrl ( this, XMLSEC_RES( 1 ) ) 73 ,maOkBtn ( this, XMLSEC_RES( BTN_OK ) ) 74 ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL ) ) 75 ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) ) 76 ,maResetBtn ( this, XMLSEC_RES( BTN_RESET ) ) 77 { 78 FreeResource(); 79 80 mxCtx = _rxCtx; 81 mxSecurityEnvironment = _rxSecurityEnvironment; 82 83 mpLevelTP = new MacroSecurityLevelTP( &maTabCtrl, this ); 84 mpTrustSrcTP = new MacroSecurityTrustedSourcesTP( &maTabCtrl, this ); 85 86 maTabCtrl.SetTabPage( RID_XMLSECTP_SECLEVEL, mpLevelTP ); 87 maTabCtrl.SetTabPage( RID_XMLSECTP_TRUSTSOURCES, mpTrustSrcTP ); 88 maTabCtrl.SetCurPageId( RID_XMLSECTP_SECLEVEL ); 89 90 maOkBtn.SetClickHdl( LINK( this, MacroSecurity, OkBtnHdl ) ); 91 } 92 93 MacroSecurity::~MacroSecurity() 94 { 95 delete maTabCtrl.GetTabPage( RID_XMLSECTP_TRUSTSOURCES ); 96 delete maTabCtrl.GetTabPage( RID_XMLSECTP_SECLEVEL ); 97 } 98 99 100 MacroSecurityTP::MacroSecurityTP( Window* _pParent, const ResId& _rResId, MacroSecurity* _pDlg ) 101 :TabPage ( _pParent, _rResId ) 102 ,mpDlg ( _pDlg ) 103 { 104 } 105 106 MacroSecurityLevelTP::MacroSecurityLevelTP( Window* _pParent, MacroSecurity* _pDlg ) 107 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_SECLEVEL ), _pDlg ) 108 ,maSecLevelFL ( this, XMLSEC_RES( FL_SECLEVEL ) ) 109 ,maSecReadonlyFI ( this, XMLSEC_RES( FI_SEC_READONLY )) 110 ,maVeryHighRB ( this, XMLSEC_RES( RB_VERYHIGH ) ) 111 ,maHighRB ( this, XMLSEC_RES( RB_HIGH ) ) 112 ,maMediumRB ( this, XMLSEC_RES( RB_MEDIUM ) ) 113 ,maLowRB ( this, XMLSEC_RES( RB_LOW ) ) 114 { 115 FreeResource(); 116 117 maLowRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) ); 118 maMediumRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) ); 119 maHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) ); 120 maVeryHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) ); 121 122 mnCurLevel = (sal_uInt16) mpDlg->maSecOptions.GetMacroSecurityLevel(); 123 sal_Bool bReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL ); 124 125 RadioButton* pCheck = 0; 126 switch( mnCurLevel ) 127 { 128 case 3: pCheck = &maVeryHighRB; break; 129 case 2: pCheck = &maHighRB; break; 130 case 1: pCheck = &maMediumRB; break; 131 case 0: pCheck = &maLowRB; break; 132 } 133 if(pCheck) 134 pCheck->Check(); 135 else 136 { 137 DBG_ERROR("illegal macro security level"); 138 } 139 maSecReadonlyFI.Show(bReadonly); 140 if(bReadonly) 141 { 142 //move to the selected button 143 if( pCheck && pCheck != &maVeryHighRB) 144 { 145 long nDiff = pCheck->GetPosPixel().Y() - maVeryHighRB.GetPosPixel().Y(); 146 Point aPos(maSecReadonlyFI.GetPosPixel()); 147 aPos.Y() += nDiff; 148 maSecReadonlyFI.SetPosPixel(aPos); 149 } 150 maVeryHighRB.Enable(sal_False); 151 maHighRB.Enable(sal_False); 152 maMediumRB.Enable(sal_False); 153 maLowRB.Enable(sal_False); 154 } 155 156 } 157 158 IMPL_LINK( MacroSecurityLevelTP, RadioButtonHdl, RadioButton*, EMPTYARG ) 159 { 160 sal_uInt16 nNewLevel = 0; 161 if( maVeryHighRB.IsChecked() ) 162 nNewLevel = 3; 163 else if( maHighRB.IsChecked() ) 164 nNewLevel = 2; 165 else if( maMediumRB.IsChecked() ) 166 nNewLevel = 1; 167 168 if ( nNewLevel != mnCurLevel ) 169 { 170 mnCurLevel = nNewLevel; 171 mpDlg->EnableReset(); 172 } 173 174 return 0; 175 } 176 177 void MacroSecurityLevelTP::ClosePage( void ) 178 { 179 mpDlg->maSecOptions.SetMacroSecurityLevel( mnCurLevel ); 180 } 181 182 void MacroSecurityTrustedSourcesTP::ImplCheckButtons() 183 { 184 bool bCertSelected = maTrustCertLB.FirstSelected() != NULL; 185 maViewCertPB.Enable( bCertSelected ); 186 maRemoveCertPB.Enable( bCertSelected && !mbAuthorsReadonly); 187 188 bool bLocationSelected = maTrustFileLocLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND; 189 maRemoveLocPB.Enable( bLocationSelected && !mbURLsReadonly); 190 } 191 192 193 IMPL_LINK( MacroSecurityTrustedSourcesTP, ViewCertPBHdl, void*, EMPTYARG ) 194 { 195 if( maTrustCertLB.FirstSelected() ) 196 { 197 sal_uInt16 nSelected = sal_uInt16( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) ); 198 199 uno::Reference< dcss::security::XSerialNumberAdapter > xSerialNumberAdapter = 200 ::com::sun::star::security::SerialNumberAdapter::create(mpDlg->mxCtx); 201 202 uno::Reference< dcss::security::XCertificate > xCert = mpDlg->mxSecurityEnvironment->getCertificate( maTrustedAuthors[nSelected][0], xSerialNumberAdapter->toSequence( maTrustedAuthors[nSelected][1] ) ); 203 204 // If we don't get it, create it from signature data: 205 if ( !xCert.is() ) 206 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ; 207 208 DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" ); 209 210 if ( xCert.is() ) 211 { 212 CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, xCert, sal_False ); 213 aViewer.Execute(); 214 } 215 } 216 return 0; 217 } 218 219 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveCertPBHdl, void*, EMPTYARG ) 220 { 221 if( maTrustCertLB.FirstSelected() ) 222 { 223 sal_uInt16 nAuthor = sal_uInt16( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) ); 224 ::comphelper::removeElementAt( maTrustedAuthors, nAuthor ); 225 226 FillCertLB(); 227 ImplCheckButtons(); 228 } 229 230 return 0; 231 } 232 233 IMPL_LINK( MacroSecurityTrustedSourcesTP, AddLocPBHdl, void*, EMPTYARG ) 234 { 235 try 236 { 237 rtl::OUString aService( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME ) ); 238 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() ); 239 uno::Reference < ui::dialogs::XFolderPicker > xFolderPicker( xFactory->createInstance( aService ), uno::UNO_QUERY ); 240 241 short nRet = xFolderPicker->execute(); 242 243 if( ui::dialogs::ExecutableDialogResults::OK != nRet ) 244 return 0; 245 246 rtl::OUString aPathStr = xFolderPicker->getDirectory(); 247 INetURLObject aNewObj( aPathStr ); 248 aNewObj.removeFinalSlash(); 249 250 // then the new path also an URL else system path 251 ::rtl::OUString aSystemFileURL = ( aNewObj.GetProtocol() != INET_PROT_NOT_VALID ) ? 252 aPathStr : aNewObj.getFSysPath( INetURLObject::FSYS_DETECT ); 253 254 String aNewPathStr(aSystemFileURL); 255 256 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None ) 257 aNewPathStr = aSystemFileURL; 258 259 if( maTrustFileLocLB.GetEntryPos( aNewPathStr ) == LISTBOX_ENTRY_NOTFOUND ) 260 { 261 maTrustFileLocLB.InsertEntry( aNewPathStr ); 262 } 263 264 ImplCheckButtons(); 265 } 266 catch( uno::Exception& ) 267 { 268 DBG_ERRORFILE( "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" ); 269 } 270 271 return 0; 272 } 273 274 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveLocPBHdl, void*, EMPTYARG ) 275 { 276 sal_uInt16 nSel = maTrustFileLocLB.GetSelectEntryPos(); 277 if( nSel != LISTBOX_ENTRY_NOTFOUND ) 278 { 279 maTrustFileLocLB.RemoveEntry( nSel ); 280 // --> PB 2004-09-21 #i33584# 281 // after remove an entry, select another one if exists 282 sal_uInt16 nNewCount = maTrustFileLocLB.GetEntryCount(); 283 if ( nNewCount > 0 ) 284 { 285 if ( nSel >= nNewCount ) 286 nSel = nNewCount - 1; 287 maTrustFileLocLB.SelectEntryPos( nSel ); 288 } 289 // <-- 290 ImplCheckButtons(); 291 } 292 293 return 0; 294 } 295 296 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl, void*, EMPTYARG ) 297 { 298 ImplCheckButtons(); 299 return 0; 300 } 301 302 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl, void*, EMPTYARG ) 303 { 304 ImplCheckButtons(); 305 return 0; 306 } 307 308 void MacroSecurityTrustedSourcesTP::FillCertLB( void ) 309 { 310 maTrustCertLB.Clear(); 311 312 sal_uInt32 nEntries = maTrustedAuthors.getLength(); 313 314 if ( nEntries && mpDlg->mxSecurityEnvironment.is() ) 315 { 316 for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry ) 317 { 318 cssu::Sequence< ::rtl::OUString >& rEntry = maTrustedAuthors[ nEntry ]; 319 uno::Reference< css::security::XCertificate > xCert; 320 321 // create from RawData 322 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] ); 323 324 SvLBoxEntry* pLBEntry = maTrustCertLB.InsertEntry( XmlSec::GetContentPart( xCert->getSubjectName() ) ); 325 maTrustCertLB.SetEntryText( XmlSec::GetContentPart( xCert->getIssuerName() ), pLBEntry, 1 ); 326 maTrustCertLB.SetEntryText( XmlSec::GetDateTimeString( xCert->getNotValidAfter() ), pLBEntry, 2 ); 327 pLBEntry->SetUserData( ( void* ) sal_Int32( nEntry ) ); // missuse user data as index 328 } 329 } 330 } 331 332 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP( Window* _pParent, MacroSecurity* _pDlg ) 333 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_TRUSTSOURCES ), _pDlg ) 334 ,maTrustCertFL ( this, XMLSEC_RES( FL_TRUSTCERT ) ) 335 ,maTrustCertROFI ( this, XMLSEC_RES( FI_TRUSTCERT_RO ) ) 336 ,maTrustCertLB ( this, XMLSEC_RES( LB_TRUSTCERT ) ) 337 ,maAddCertPB ( this, XMLSEC_RES( PB_ADD_TRUSTCERT ) ) 338 ,maViewCertPB ( this, XMLSEC_RES( PB_VIEW_TRUSTCERT ) ) 339 ,maRemoveCertPB ( this, XMLSEC_RES( PB_REMOVE_TRUSTCERT ) ) 340 ,maTrustFileLocFL ( this, XMLSEC_RES( FL_TRUSTFILELOC ) ) 341 ,maTrustFileROFI ( this, XMLSEC_RES( FI_TRUSTFILE_RO ) ) 342 ,maTrustFileLocFI ( this, XMLSEC_RES( FI_TRUSTFILELOC ) ) 343 ,maTrustFileLocLB ( this, XMLSEC_RES( LB_TRUSTFILELOC ) ) 344 ,maAddLocPB ( this, XMLSEC_RES( FL_ADD_TRUSTFILELOC ) ) 345 ,maRemoveLocPB ( this, XMLSEC_RES( FL_REMOVE_TRUSTFILELOC ) ) 346 { 347 static long nTabs[] = { 3, 0, 35*CS_LB_WIDTH/100, 70*CS_LB_WIDTH/100 }; 348 maTrustCertLB.SetTabs( &nTabs[ 0 ] ); 349 maTrustCertLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) ); 350 351 FreeResource(); 352 353 maTrustCertLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) ); 354 maAddCertPB.Hide(); // not used in the moment... 355 maViewCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) ); 356 maViewCertPB.Disable(); 357 maRemoveCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) ); 358 maRemoveCertPB.Disable(); 359 360 maTrustFileLocLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) ); 361 maAddLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) ); 362 maRemoveLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) ); 363 maRemoveLocPB.Disable(); 364 365 maTrustedAuthors = mpDlg->maSecOptions.GetTrustedAuthors(); 366 mbAuthorsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS ); 367 maTrustCertROFI.Show( mbAuthorsReadonly ); 368 mbAuthorsReadonly ? maTrustCertLB.DisableTable() : maTrustCertLB.EnableTable(); 369 // unused button 370 // maAddCertPB.Enable( !mbAuthorsReadonly ); 371 372 FillCertLB(); 373 374 cssu::Sequence< rtl::OUString > aSecureURLs = mpDlg->maSecOptions.GetSecureURLs(); 375 mbURLsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_SECUREURLS ); 376 maTrustFileROFI.Show( mbURLsReadonly ); 377 maTrustFileLocLB.Enable( !mbURLsReadonly ); 378 maAddLocPB .Enable( !mbURLsReadonly ); 379 380 sal_Int32 nEntryCnt = aSecureURLs.getLength(); 381 for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i ) 382 { 383 ::rtl::OUString aSystemFileURL( aSecureURLs[ i ] ); 384 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ); 385 maTrustFileLocLB.InsertEntry( aSystemFileURL ); 386 } 387 } 388 389 void MacroSecurityTrustedSourcesTP::ActivatePage() 390 { 391 mpDlg->EnableReset( false ); 392 FillCertLB(); 393 } 394 395 void MacroSecurityTrustedSourcesTP::ClosePage( void ) 396 { 397 sal_uInt16 nEntryCnt = maTrustFileLocLB.GetEntryCount(); 398 if( nEntryCnt ) 399 { 400 cssu::Sequence< rtl::OUString > aSecureURLs( nEntryCnt ); 401 for( sal_uInt16 i = 0 ; i < nEntryCnt ; ++i ) 402 { 403 ::rtl::OUString aURL( maTrustFileLocLB.GetEntry( i ) ); 404 osl::FileBase::getFileURLFromSystemPath( aURL, aURL ); 405 aSecureURLs[ i ] = aURL; 406 } 407 408 mpDlg->maSecOptions.SetSecureURLs( aSecureURLs ); 409 } 410 // --> PB 2004-09-21 #i33584# 411 // don't forget to remove the old saved SecureURLs 412 else 413 mpDlg->maSecOptions.SetSecureURLs( cssu::Sequence< rtl::OUString >() ); 414 // <-- 415 416 mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors ); 417 } 418 /*-- 26.02.2004 13:31:04--------------------------------------------------- 419 420 -----------------------------------------------------------------------*/ 421 ReadOnlyImage::ReadOnlyImage(Window* pParent, const ResId rResId) : 422 FixedImage(pParent, rResId) 423 { 424 sal_Bool bHighContrast = pParent->GetSettings().GetStyleSettings().GetHighContrastMode(); 425 SetImage( Image(XMLSEC_RES( bHighContrast ? RID_XMLSECTP_LOCK_HC : RID_XMLSECTP_LOCK ))); 426 } 427 428 /*-- 26.02.2004 13:31:04--------------------------------------------------- 429 430 -----------------------------------------------------------------------*/ 431 ReadOnlyImage::~ReadOnlyImage() 432 { 433 } 434 /*-- 26.02.2004 13:31:04--------------------------------------------------- 435 436 -----------------------------------------------------------------------*/ 437 void ReadOnlyImage::RequestHelp( const HelpEvent& rHEvt ) 438 { 439 if( Help::IsBalloonHelpEnabled() || Help::IsQuickHelpEnabled() ) 440 { 441 Rectangle aLogicPix( LogicToPixel( Rectangle( Point(), GetOutputSize() ) ) ); 442 Rectangle aScreenRect( OutputToScreenPixel( aLogicPix.TopLeft() ), 443 OutputToScreenPixel( aLogicPix.BottomRight() ) ); 444 445 String aStr(ReadOnlyImage::GetHelpTip()); 446 if ( Help::IsBalloonHelpEnabled() ) 447 Help::ShowBalloon( this, rHEvt.GetMousePosPixel(), aScreenRect, 448 aStr ); 449 else if ( Help::IsQuickHelpEnabled() ) 450 Help::ShowQuickHelp( this, aScreenRect, aStr ); 451 } 452 else 453 Window::RequestHelp( rHEvt ); 454 } 455 456 /*-- 26.02.2004 14:20:21--------------------------------------------------- 457 458 -----------------------------------------------------------------------*/ 459 const String& ReadOnlyImage::GetHelpTip() 460 { 461 static String aStr(XMLSEC_RES( RID_XMLSECTP_READONLY_CONFIG_TIP)); 462 return aStr; 463 } 464 465