1 /* 2 * Created on 12-Jun-2004 3 * Created by Paul Gardner 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 */ 19 20 package org.gudy.azureus2.ui.swt.views.configsections; 21 22 import java.io.*; 23 import java.util.ArrayList; 24 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.dnd.Clipboard; 27 import org.eclipse.swt.dnd.TextTransfer; 28 import org.eclipse.swt.dnd.Transfer; 29 import org.eclipse.swt.events.MouseAdapter; 30 import org.eclipse.swt.events.MouseEvent; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.*; 35 import org.gudy.azureus2.core3.config.COConfigurationManager; 36 import org.gudy.azureus2.core3.internat.MessageText; 37 import org.gudy.azureus2.core3.security.SESecurityManager; 38 import org.gudy.azureus2.core3.util.Base32; 39 import org.gudy.azureus2.core3.util.Debug; 40 import org.gudy.azureus2.ui.swt.Messages; 41 import org.gudy.azureus2.ui.swt.Utils; 42 import org.gudy.azureus2.ui.swt.auth.CertificateCreatorWindow; 43 import org.gudy.azureus2.ui.swt.config.*; 44 import org.gudy.azureus2.ui.swt.mainwindow.ClipboardCopy; 45 import org.gudy.azureus2.ui.swt.mainwindow.Colors; 46 import org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection; 47 import org.gudy.azureus2.ui.swt.shells.MessageBoxShell; 48 49 import com.aelitis.azureus.core.security.*; 50 import com.aelitis.azureus.ui.UserPrompterResultListener; 51 import com.aelitis.azureus.ui.swt.UIFunctionsManagerSWT; 52 import com.aelitis.azureus.ui.swt.UIFunctionsSWT; 53 import com.aelitis.azureus.ui.swt.imageloader.ImageLoader; 54 55 import org.gudy.azureus2.plugins.ui.config.ConfigSection; 56 57 /** 58 * @author parg 59 * 60 */ 61 public class 62 ConfigSectionSecurity 63 implements UISWTConfigSection 64 { 65 public String configSectionGetParentSection()66 configSectionGetParentSection() 67 { 68 return ConfigSection.SECTION_ROOT; 69 } 70 71 public String configSectionGetName()72 configSectionGetName() 73 { 74 return( "security" ); 75 } 76 77 public void configSectionSave()78 configSectionSave() 79 { 80 } 81 82 public void configSectionDelete()83 configSectionDelete() 84 { 85 ImageLoader imageLoader = ImageLoader.getInstance(); 86 imageLoader.releaseImage("openFolderButton"); 87 } 88 maxUserMode()89 public int maxUserMode() { 90 return 2; 91 } 92 93 public Composite configSectionCreate( final Composite parent)94 configSectionCreate( 95 final Composite parent) 96 { 97 int userMode = COConfigurationManager.getIntParameter("User Mode"); 98 99 java.util.List<Button> buttons = new ArrayList<Button>(); 100 101 GridData gridData; 102 103 Composite gSecurity = new Composite(parent, SWT.NULL); 104 gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL); 105 gSecurity.setLayoutData(gridData); 106 GridLayout layout = new GridLayout(); 107 layout.numColumns = 3; 108 gSecurity.setLayout(layout); 109 110 // row 111 112 Label cert_label = new Label(gSecurity, SWT.NULL ); 113 Messages.setLanguageText(cert_label, "ConfigView.section.tracker.createcert"); 114 115 Button cert_button = new Button(gSecurity, SWT.PUSH); 116 buttons.add( cert_button ); 117 118 Messages.setLanguageText(cert_button, "ConfigView.section.tracker.createbutton"); 119 120 cert_button.addListener(SWT.Selection, 121 new Listener() 122 { 123 public void 124 handleEvent(Event event) 125 { 126 new CertificateCreatorWindow(); 127 } 128 }); 129 130 new Label(gSecurity, SWT.NULL ); 131 132 // row 133 134 Label reset_certs_label = new Label(gSecurity, SWT.NULL ); 135 Messages.setLanguageText(reset_certs_label, "ConfigView.section.security.resetcerts"); 136 137 Button reset_certs_button = new Button(gSecurity, SWT.PUSH); 138 buttons.add( reset_certs_button ); 139 140 Messages.setLanguageText(reset_certs_button, "Button.reset"); 141 142 reset_certs_button.addListener(SWT.Selection, 143 new Listener() 144 { 145 public void 146 handleEvent(Event event) 147 { 148 MessageBoxShell mb = new MessageBoxShell( 149 SWT.ICON_WARNING | SWT.OK | SWT.CANCEL, 150 MessageText.getString("ConfigView.section.security.resetcerts.warning.title"), 151 MessageText.getString("ConfigView.section.security.resetcerts.warning.msg")); 152 mb.setDefaultButtonUsingStyle(SWT.CANCEL); 153 mb.setParent(parent.getShell()); 154 155 mb.open(new UserPrompterResultListener() { 156 public void prompterClosed(int returnVal) { 157 if (returnVal != SWT.OK) { 158 return; 159 } 160 161 if ( SESecurityManager.resetTrustStore( false )){ 162 163 MessageBoxShell mb = new MessageBoxShell( 164 SWT.ICON_INFORMATION | SWT.OK, 165 MessageText.getString( "ConfigView.section.security.restart.title" ), 166 MessageText.getString( "ConfigView.section.security.restart.msg" )); 167 mb.setParent(parent.getShell()); 168 mb.open(null); 169 170 171 UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT(); 172 173 if ( uiFunctions != null ){ 174 175 uiFunctions.dispose(true, false); 176 } 177 178 }else{ 179 180 MessageBoxShell mb = new MessageBoxShell( 181 SWT.ICON_ERROR | SWT.OK, 182 MessageText.getString( "ConfigView.section.security.resetcerts.error.title"), 183 MessageText.getString( "ConfigView.section.security.resetcerts.error.msg" )); 184 mb.setParent(parent.getShell()); 185 mb.open(null); 186 } 187 } 188 }); 189 190 } 191 }); 192 193 reset_certs_button.setEnabled( SESecurityManager.resetTrustStore( true )); 194 195 new Label(gSecurity, SWT.NULL ); 196 197 // row 198 199 gridData = new GridData(); 200 gridData.horizontalSpan = 3; 201 new BooleanParameter( 202 gSecurity, 203 "security.cert.auto.install", 204 "security.cert.auto.install").setLayoutData( gridData ); 205 206 // row 207 208 Label info_label = new Label( gSecurity, SWT.WRAP ); 209 Messages.setLanguageText( info_label, "ConfigView.section.security.toolsinfo" ); 210 info_label.setLayoutData(Utils.getWrappableLabelGridData(3, 0)); 211 212 // row 213 214 Label lStatsPath = new Label(gSecurity, SWT.NULL); 215 216 Messages.setLanguageText(lStatsPath, "ConfigView.section.security.toolsdir"); //$NON-NLS-1$ 217 218 ImageLoader imageLoader = ImageLoader.getInstance(); 219 Image imgOpenFolder = imageLoader.getImage("openFolderButton"); 220 221 gridData = new GridData(); 222 223 gridData.widthHint = 150; 224 225 final StringParameter pathParameter = new StringParameter(gSecurity, "Security.JAR.tools.dir", ""); //$NON-NLS-1$ //$NON-NLS-2$ 226 227 pathParameter.setLayoutData(gridData); 228 229 Button browse = new Button(gSecurity, SWT.PUSH); 230 231 browse.setImage(imgOpenFolder); 232 233 imgOpenFolder.setBackground(browse.getBackground()); 234 235 browse.setToolTipText(MessageText.getString("ConfigView.button.browse")); 236 237 browse.addListener(SWT.Selection, new Listener() { 238 public void handleEvent(Event event) { 239 DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.APPLICATION_MODAL); 240 241 dialog.setFilterPath(pathParameter.getValue()); 242 243 dialog.setText(MessageText.getString("ConfigView.section.security.choosetoolssavedir")); //$NON-NLS-1$ 244 245 String path = dialog.open(); 246 247 if (path != null) { 248 pathParameter.setValue(path); 249 } 250 } 251 }); 252 253 254 // row 255 256 Label pw_label = new Label(gSecurity, SWT.NULL ); 257 Messages.setLanguageText(pw_label, "ConfigView.section.security.clearpasswords"); 258 259 Button pw_button = new Button(gSecurity, SWT.PUSH); 260 buttons.add( pw_button ); 261 262 Messages.setLanguageText(pw_button, "ConfigView.section.security.clearpasswords.button"); 263 264 pw_button.addListener(SWT.Selection, 265 new Listener() 266 { 267 public void 268 handleEvent(Event event) 269 { 270 SESecurityManager.clearPasswords(); 271 272 CryptoManagerFactory.getSingleton().clearPasswords(); 273 } 274 }); 275 276 new Label(gSecurity, SWT.NULL ); 277 278 if ( userMode >= 2 ){ 279 280 final CryptoManager crypt_man = CryptoManagerFactory.getSingleton(); 281 282 final Group crypto_group = new Group(gSecurity, SWT.NULL); 283 gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL); 284 gridData.horizontalSpan = 3; 285 crypto_group.setLayoutData(gridData); 286 layout = new GridLayout(); 287 layout.numColumns = 3; 288 crypto_group.setLayout(layout); 289 290 Messages.setLanguageText(crypto_group,"ConfigView.section.security.group.crypto"); 291 292 // wiki link 293 294 final Label linkLabel = new Label(crypto_group, SWT.NULL); 295 linkLabel.setText(MessageText.getString("ConfigView.label.please.visit.here")); 296 linkLabel.setData("http://wiki.vuze.com/w/Public_Private_Keys"); 297 linkLabel.setCursor(linkLabel.getDisplay().getSystemCursor(SWT.CURSOR_HAND)); 298 linkLabel.setForeground(Colors.blue); 299 gridData = new GridData(); 300 gridData.horizontalSpan = 3; 301 linkLabel.setLayoutData(gridData); 302 linkLabel.addMouseListener(new MouseAdapter() { 303 public void mouseDoubleClick(MouseEvent arg0) { 304 Utils.launch((String) ((Label) arg0.widget).getData()); 305 } 306 307 public void mouseDown(MouseEvent arg0) { 308 Utils.launch((String) ((Label) arg0.widget).getData()); 309 } 310 }); 311 ClipboardCopy.addCopyToClipMenu( linkLabel ); 312 313 // publick key display 314 315 byte[] public_key = crypt_man.getECCHandler().peekPublicKey(); 316 317 Label public_key_label = new Label(crypto_group, SWT.NULL ); 318 Messages.setLanguageText(public_key_label, "ConfigView.section.security.publickey"); 319 320 final Label public_key_value = new Label(crypto_group, SWT.NULL ); 321 322 if ( public_key == null ){ 323 324 Messages.setLanguageText(public_key_value, "ConfigView.section.security.publickey.undef"); 325 326 }else{ 327 328 public_key_value.setText( Base32.encode( public_key )); 329 } 330 331 Messages.setLanguageText(public_key_value, "ConfigView.copy.to.clipboard.tooltip", true); 332 333 public_key_value.setCursor(public_key_value.getDisplay().getSystemCursor(SWT.CURSOR_HAND)); 334 public_key_value.setForeground(Colors.blue); 335 public_key_value.addMouseListener(new MouseAdapter() { 336 public void mouseDoubleClick(MouseEvent arg0) { 337 copyToClipboard(); 338 } 339 public void mouseDown(MouseEvent arg0) { 340 copyToClipboard(); 341 } 342 protected void 343 copyToClipboard() 344 { 345 new Clipboard(parent.getDisplay()).setContents(new Object[] {public_key_value.getText()}, new Transfer[] {TextTransfer.getInstance()}); 346 } 347 }); 348 349 crypt_man.addKeyListener( 350 new CryptoManagerKeyListener() 351 { 352 public void 353 keyChanged( 354 final CryptoHandler handler ) 355 { 356 final CryptoManagerKeyListener me = this; 357 358 Utils.execSWTThread( 359 new Runnable() 360 { 361 public void 362 run() 363 { 364 if ( public_key_value.isDisposed()){ 365 366 crypt_man.removeKeyListener( me ); 367 368 }else{ 369 if ( handler.getType() == CryptoManager.HANDLER_ECC ){ 370 371 byte[] public_key = handler.peekPublicKey(); 372 373 if ( public_key == null ){ 374 375 Messages.setLanguageText(public_key_value, "ConfigView.section.security.publickey.undef"); 376 377 }else{ 378 379 public_key_value.setText( Base32.encode( public_key )); 380 } 381 382 crypto_group.layout(); 383 } 384 } 385 } 386 }); 387 } 388 389 public void 390 keyLockStatusChanged( 391 CryptoHandler handler ) 392 { 393 } 394 }); 395 396 new Label(crypto_group, SWT.NULL ); 397 398 // manage keys 399 400 /* 401 gridData = new GridData(); 402 gridData.horizontalSpan = 3; 403 404 final BooleanParameter manage_keys = new BooleanParameter( 405 crypto_group, "crypto.keys.system.managed.temp", 406 "ConfigView.section.security.system.managed"); 407 408 manage_keys.setLayoutData( gridData ); 409 410 final CryptoManager crypto_man = CryptoManagerFactory.getSingleton(); 411 final CryptoHandler ecc_handler = crypto_man.getECCHandler(); 412 413 manage_keys.setSelected( 414 ecc_handler.getDefaultPasswordHandlerType() == CryptoManagerPasswordHandler.HANDLER_TYPE_SYSTEM ); 415 416 417 manage_keys.addChangeListener( 418 new ParameterChangeAdapter () 419 { 420 public void 421 parameterChanged( 422 Parameter p, 423 boolean caused_internally ) 424 { 425 boolean existing_value = ecc_handler.getDefaultPasswordHandlerType() == CryptoManagerPasswordHandler.HANDLER_TYPE_SYSTEM; 426 427 if ( existing_value == manage_keys.isSelected()){ 428 429 return; 430 } 431 432 String error = null; 433 434 int new_type = manage_keys.isSelected()?CryptoManagerPasswordHandler.HANDLER_TYPE_SYSTEM:CryptoManagerPasswordHandler.HANDLER_TYPE_USER; 435 436 try{ 437 ecc_handler.setDefaultPasswordHandlerType( new_type ); 438 439 error = null; 440 441 }catch( CryptoManagerPasswordException e ){ 442 443 if ( e.wasIncorrect()){ 444 445 error = MessageText.getString( "ConfigView.section.security.unlockkey.error" ); 446 447 }else{ 448 449 if ( existing_value || !ecc_handler.isUnlocked()){ 450 451 error = MessageText.getString( "Torrent.create.progress.cancelled" ); 452 453 }else{ 454 455 error = MessageText.getString( "ConfigView.section.security.vuze.login" ); 456 } 457 } 458 }catch( Throwable e ){ 459 460 error = Debug.getNestedExceptionMessage( e ); 461 } 462 463 if ( error != null ){ 464 465 MessageBoxShell mb = new MessageBoxShell( 466 SWT.ICON_ERROR | SWT.OK, 467 MessageText.getString("ConfigView.section.security.op.error.title"), 468 MessageText.getString("ConfigView.section.security.op.error", 469 new String[] { error })); 470 mb.setParent(parent.getShell()); 471 mb.open(null); 472 } 473 474 boolean new_value = ecc_handler.getDefaultPasswordHandlerType() == CryptoManagerPasswordHandler.HANDLER_TYPE_SYSTEM; 475 476 if ( new_value != manage_keys.isSelected()){ 477 478 manage_keys.setSelected( new_value ); 479 } 480 } 481 }); 482 */ 483 484 // reset keys 485 486 Label reset_key_label = new Label(crypto_group, SWT.NULL ); 487 Messages.setLanguageText(reset_key_label, "ConfigView.section.security.resetkey"); 488 489 Button reset_key_button = new Button(crypto_group, SWT.PUSH); 490 buttons.add( reset_key_button ); 491 492 Messages.setLanguageText(reset_key_button, "ConfigView.section.security.clearpasswords.button"); 493 494 reset_key_button.addListener(SWT.Selection, 495 new Listener() 496 { 497 public void 498 handleEvent(Event event) 499 { 500 MessageBoxShell mb = new MessageBoxShell( 501 SWT.ICON_WARNING | SWT.OK | SWT.CANCEL, 502 MessageText.getString("ConfigView.section.security.resetkey.warning.title"), 503 MessageText.getString("ConfigView.section.security.resetkey.warning")); 504 mb.setDefaultButtonUsingStyle(SWT.CANCEL); 505 mb.setParent(parent.getShell()); 506 507 mb.open(new UserPrompterResultListener() { 508 public void prompterClosed(int returnVal) { 509 if (returnVal != SWT.OK) { 510 return; 511 } 512 513 try{ 514 crypt_man.getECCHandler().resetKeys( "Manual key reset" ); 515 516 }catch( Throwable e ){ 517 518 MessageBoxShell mb = new MessageBoxShell( 519 SWT.ICON_ERROR | SWT.OK, 520 MessageText.getString( "ConfigView.section.security.resetkey.error.title"), 521 getError( e )); 522 mb.setParent(parent.getShell()); 523 mb.open(null); 524 } 525 } 526 }); 527 } 528 }); 529 530 new Label(crypto_group, SWT.NULL ); 531 532 // unlock 533 534 Label priv_key_label = new Label(crypto_group, SWT.NULL ); 535 Messages.setLanguageText(priv_key_label, "ConfigView.section.security.unlockkey"); 536 537 Button priv_key_button = new Button(crypto_group, SWT.PUSH); 538 buttons.add( priv_key_button ); 539 540 Messages.setLanguageText(priv_key_button, "ConfigView.section.security.unlockkey.button"); 541 542 priv_key_button.addListener(SWT.Selection, 543 new Listener() 544 { 545 public void 546 handleEvent(Event event) 547 { 548 try{ 549 crypt_man.getECCHandler().getEncryptedPrivateKey( "Manual unlock" ); 550 551 }catch( Throwable e ){ 552 553 MessageBoxShell mb = new MessageBoxShell( 554 SWT.ICON_ERROR | SWT.OK, 555 MessageText.getString( "ConfigView.section.security.resetkey.error.title" ), 556 getError( e )); 557 mb.setParent(parent.getShell()); 558 mb.open(null); 559 }; 560 561 } 562 }); 563 564 new Label(crypto_group, SWT.NULL ); 565 566 // backup 567 568 Label backup_keys_label = new Label(crypto_group, SWT.NULL ); 569 Messages.setLanguageText(backup_keys_label, "ConfigView.section.security.backupkeys"); 570 571 final Button backup_keys_button = new Button(crypto_group, SWT.PUSH); 572 buttons.add( backup_keys_button ); 573 574 Messages.setLanguageText(backup_keys_button, "ConfigView.section.security.backupkeys.button"); 575 576 backup_keys_button.addListener(SWT.Selection, 577 new Listener() 578 { 579 public void 580 handleEvent(Event event) 581 { 582 FileDialog dialog = new FileDialog( backup_keys_button.getShell(), SWT.APPLICATION_MODAL ); 583 584 String target = dialog.open(); 585 586 if ( target != null ){ 587 588 try{ 589 String keys = crypt_man.getECCHandler().exportKeys(); 590 591 PrintWriter pw = new PrintWriter(new FileWriter( target )); 592 593 pw.println( keys ); 594 595 pw.close(); 596 597 }catch( Throwable e ){ 598 599 MessageBoxShell mb = new MessageBoxShell( 600 SWT.ICON_ERROR | SWT.OK, 601 MessageText.getString( "ConfigView.section.security.op.error.title" ), 602 MessageText.getString( "ConfigView.section.security.op.error", 603 new String[]{ getError(e) })); 604 mb.setParent(parent.getShell()); 605 mb.open(null); 606 } 607 } 608 } 609 }); 610 611 new Label(crypto_group, SWT.NULL ); 612 613 // restore 614 615 Label restore_keys_label = new Label(crypto_group, SWT.NULL ); 616 Messages.setLanguageText(restore_keys_label, "ConfigView.section.security.restorekeys"); 617 618 final Button restore_keys_button = new Button(crypto_group, SWT.PUSH); 619 buttons.add( restore_keys_button ); 620 621 Messages.setLanguageText(restore_keys_button, "ConfigView.section.security.restorekeys.button"); 622 623 restore_keys_button.addListener(SWT.Selection, 624 new Listener() 625 { 626 public void 627 handleEvent(Event event) 628 { 629 FileDialog dialog = new FileDialog( backup_keys_button.getShell(), SWT.APPLICATION_MODAL ); 630 631 String target = dialog.open(); 632 633 if ( target != null ){ 634 635 try{ 636 LineNumberReader reader = new LineNumberReader( new FileReader( target )); 637 638 String str = ""; 639 640 try{ 641 while( true ){ 642 643 String line = reader.readLine(); 644 645 if ( line == null ){ 646 647 break; 648 } 649 650 str += line + "\r\n"; 651 } 652 }finally{ 653 654 reader.close(); 655 } 656 657 boolean restart = crypt_man.getECCHandler().importKeys(str); 658 659 if ( restart ){ 660 661 MessageBoxShell mb = new MessageBoxShell( 662 SWT.ICON_INFORMATION | SWT.OK, 663 MessageText.getString( "ConfigView.section.security.restart.title" ), 664 MessageText.getString( "ConfigView.section.security.restart.msg" )); 665 mb.setParent(parent.getShell()); 666 mb.open(null); 667 668 669 UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT(); 670 671 if ( uiFunctions != null ){ 672 673 uiFunctions.dispose(true, false); 674 } 675 } 676 }catch( Throwable e ){ 677 678 MessageBoxShell mb = new MessageBoxShell( 679 SWT.ICON_ERROR | SWT.OK, 680 MessageText.getString( "ConfigView.section.security.op.error.title" ), 681 MessageText.getString( "ConfigView.section.security.op.error", 682 new String[]{ getError( e )})); 683 mb.setParent(parent.getShell()); 684 mb.open(null); 685 } 686 } 687 } 688 }); 689 690 new Label(crypto_group, SWT.NULL ); 691 } 692 693 Utils.makeButtonsEqualWidth( buttons ); 694 695 return gSecurity; 696 } 697 698 protected String getError( Throwable e )699 getError( 700 Throwable e ) 701 { 702 String error; 703 704 if ( e instanceof CryptoManagerPasswordException ){ 705 706 if (((CryptoManagerPasswordException)e).wasIncorrect()){ 707 708 error = MessageText.getString( "ConfigView.section.security.unlockkey.error"); 709 710 }else{ 711 712 final CryptoManager crypto_man = CryptoManagerFactory.getSingleton(); 713 final CryptoHandler ecc_handler = crypto_man.getECCHandler(); 714 715 //if ( ecc_handler.getDefaultPasswordHandlerType() == CryptoManagerPasswordHandler.HANDLER_TYPE_SYSTEM ){ 716 // 717 // error = MessageText.getString( "ConfigView.section.security.nopw_v" ); 718 // 719 //}else{ 720 721 error = MessageText.getString( "ConfigView.section.security.nopw" ); 722 //} 723 } 724 }else{ 725 726 error = MessageText.getString( "ConfigView.section.security.resetkey.error" ) + ": " + Debug.getNestedExceptionMessage(e); 727 } 728 729 return( error ); 730 } 731 } 732