1 package com.ca.commons.security; 2 3 import java.io.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.util.*; 7 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 import javax.swing.*; 10 11 import java.security.*; 12 import java.security.cert.*; 13 import java.security.spec.*; 14 15 16 import com.ca.commons.cbutil.*; 18 import com.ca.commons.security.cert.CertViewer; 19 20 public class KeystoreGUI extends CBDialog implements ActionListener 21 { 22 23 public static final String ERRORCERT = "<unable to read>"; 24 public static final String DELETEDCERT = "<deleted>"; 25 26 CBButton viewCert, addCert, deleteCert, passwordButton, 27 importKeyButton, exportKeyButton; 28 29 CBButton[] commandButtons; 30 31 protected KeyStore keystore = null; 32 33 final JList certList; DefaultListModel certListModel; 35 36 public static ImageIcon smallCert; 37 public static ImageIcon smallKeyCert; 38 39 Properties properties; 40 41 protected CBHelpSystem helpBroker; 42 43 char[] password = null; 44 45 protected String keystoreFile; 46 47 protected String keystoreType; 48 49 private static Logger log = Logger.getLogger(KeystoreGUI.class.getName()); 50 51 55 56 private boolean crippled = false; 57 58 62 63 private boolean cripplePassword = false; 64 65 private boolean changed = false; 67 68 83 84 public KeystoreGUI( Frame owner, Properties props, String keyStoreLocation, 85 char[] keyStorePassword, String keyStoreType, String title, 86 boolean handlePrivateKeys, String helpTopic) 87 { 88 super(owner, title, helpTopic); 90 if ("KSE".equals(keyStoreType)) 91 crippled = true; 92 93 properties = props; 94 95 password = keyStorePassword; 96 97 CertViewer.setProperties(properties); 98 99 if (smallCert == null) 100 smallCert = getImageIcon("sslcert.gif"); 101 if (smallKeyCert == null) 102 smallKeyCert = getImageIcon("sslkeycert.gif"); 103 104 keystoreFile = keyStoreLocation; 105 106 keystoreType = keyStoreType; 107 108 display.makeHeavy(); 109 110 JScrollPane scrollPane = new JScrollPane(); 111 112 certList = new JList(); 113 114 120 121 if (password != null || "JKS".equalsIgnoreCase(keystoreType)) 122 { 123 setupCertificateList(); 124 125 } 126 else if ("KSE".equalsIgnoreCase(keystoreType) && keystoreFile!= null && 127 keystoreFile.toLowerCase().endsWith(".der")) 128 { 129 setupCertificateList(); 130 cripplePassword = true; 131 } 132 else 133 { 134 if (setupPasswordAndKeystore(keystoreType, keystoreFile, this)) { 136 refreshView(); certList.setModel(certListModel); } 139 } 140 141 scrollPane.getViewport().setView(certList); 142 143 display.add(scrollPane, 1, 1, 2, ((handlePrivateKeys)?7:5)); 144 145 display.makeLight(); 146 147 display.add(viewCert = new CBButton(" " + CBIntText.get("View Certificate"), CBIntText.get("View a certificate in detail."), getImageIcon("sslview.gif")), 3, 1); 148 149 display.add(addCert = new CBButton(" " + CBIntText.get("Add Certificate"), CBIntText.get("Add a new trusted server certificate"), getImageIcon("ssladd.gif")), 3, 2); 150 if (crippled) 151 addCert.setEnabled(false); 153 154 display.add(deleteCert = new CBButton(" " + CBIntText.get("Delete Certificate"), CBIntText.get("Delete an unwanted or out of date server certificate"), getImageIcon("ssldelete.gif")), 3, 3); 155 156 display.add(passwordButton = new CBButton(" " + CBIntText.get("Set Password"), CBIntText.get("Change the certificate keystore password."), getImageIcon("sslpassword.gif")), 3, 4); 157 158 importKeyButton = new CBButton(" " + CBIntText.get("Set Private Key"), CBIntText.get("Match a PKCS-8 private key with a certificate"), getImageIcon("sslprivatekey.gif")); 159 160 exportKeyButton = new CBButton(" " + CBIntText.get("Export Private Key"), CBIntText.get("Export the PKCS-8 private key matching a certificate"), getImageIcon("sslexprivatekey.gif")); 161 162 if (handlePrivateKeys) 163 { 164 display.add(importKeyButton, 3, 5); 165 display.add(exportKeyButton, 3, 6); 166 } 167 168 169 commandButtons = new CBButton[] {viewCert, addCert, deleteCert, passwordButton, importKeyButton, exportKeyButton}; 170 171 for (int i=0; i<commandButtons.length; i++) 172 { 173 commandButtons[i].setHorizontalAlignment(SwingConstants.LEFT); 174 commandButtons[i].addActionListener(this); 175 } 176 177 if (crippled) 178 { 179 JButton[] crippledButton = {addCert, deleteCert, exportKeyButton, importKeyButton}; 180 for (int i=0; i<4; i++) 181 { 182 crippledButton[i].setEnabled(false); 184 crippledButton[i].removeActionListener(this); 185 crippledButton[i].setToolTipText(CBIntText.get("Not available with this security provider")); 186 crippledButton[i].setForeground(Color.gray); 187 } 188 } 189 190 if (cripplePassword) 191 { 192 passwordButton.setEnabled(false); 194 passwordButton.removeActionListener(this); 195 passwordButton.setToolTipText(CBIntText.get("Not available with this security provider")); 196 passwordButton.setForeground(Color.gray); 197 } 198 199 200 202 MouseListener mouseListener = new MouseAdapter() 203 { 204 public void mouseClicked(MouseEvent e) 205 { 206 if (e.getClickCount() == 2) 207 { 208 if (e.getModifiers() == MouseEvent.BUTTON1_MASK) 209 { 210 CertItem cert = (CertItem)certList.getSelectedValue(); 212 viewCurrentCert(cert); 213 } 214 } 215 } 216 }; 217 218 certList.addMouseListener(mouseListener); 219 220 display.add(new JLabel(" "), 3, ((handlePrivateKeys)?7:5)); } 222 223 228 229 public void actionPerformed(ActionEvent e) 230 { 231 232 JButton src = ((JButton)e.getSource()); 233 234 CertItem cert = (CertItem)certList.getSelectedValue(); 235 236 if (src == viewCert) 237 { 238 viewCurrentCert(cert); 239 } 240 else if (src == addCert) 241 { 242 addNewCert(); 243 } 244 else if (src == deleteCert) 245 { 246 if(cert==null) 247 CBUtility.error(CBIntText.get("Please select a certificate to delete."), null); 248 else 249 deleteCurrentCert(cert); 250 } 251 else if (src == passwordButton) 252 { 253 setupPasswords(); 254 } 255 else if (src == importKeyButton) 256 { 257 importKey(cert); 258 } 259 else if (src == exportKeyButton) 260 { 261 exportKey(cert); 262 } 263 } 264 265 266 271 272 public void doOK() 273 { 274 if (changed) 275 { 276 277 if (checkPassword() == false) 278 return; 280 try 281 { 282 if (writeKeyStore(password, keystore, keystoreFile, keystoreType) == false) 283 { 284 clearPassword(password); 285 password = null; 286 return; } 288 } 289 catch (Exception e) 290 { 291 CBUtility.error(CBIntText.get("Error importing key file."), e); 292 return; 293 } 294 } 295 296 changed = false; 297 298 300 clearPassword(password); 301 password = null; 302 303 super.doOK(); 304 } 306 307 public void doCancel() 308 { 309 if (changed) 310 { 311 String [] options = { CBIntText.get("Revise Changes"), CBIntText.get("Discard Changes") }; 312 313 int opt = JOptionPane.showOptionDialog(null, CBIntText.get("You have unsaved changes!"), "Warning", 314 JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 315 null, options, options[0]); 316 317 if (opt == 0) return; 318 } 319 320 super.doCancel(); 321 323 } 324 325 331 332 protected void importKey(CertItem certItem) 333 { 334 try 335 { 336 337 338 if (certItem == null || certItem.getX509Cert() == null) 339 { 340 CBUtility.error(CBIntText.get("Please select a certificate to match with a key."), null); 341 return; 342 } 343 344 345 346 File keyFile = getKeyFile(CBIntText.get("Select a pkcs8 private key file")); 347 348 if (keyFile == null) 349 return; 351 352 353 FileInputStream in = new FileInputStream(keyFile); 354 byte [] buffer = new byte[(int) (keyFile.length())]; 355 in.read(buffer); 356 in.close(); 357 358 359 if (CBSecurity.isPEM(buffer)) 360 { 361 363 byte[] pemData = CBSecurity.convertFromPEM(buffer, new String (CBSecurity.PEM_KEY_HEADER).getBytes()); 364 if (pemData != null) 365 buffer = pemData; 366 else 367 { 368 CBUtility.error(CBIntText.get("Unable to load key: does not begin with {0} ", new String [] {new String (CBSecurity.PEM_KEY_HEADER)})); 369 return; 370 } 371 } 372 373 374 if (checkPassword() == false) 375 return; 377 378 379 String alias = certItem.getAlias(); 380 381 java.security.cert.Certificate [] certChain = keystore.getCertificateChain(alias); 382 383 385 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); 386 387 KeyFactory factory = KeyFactory.getInstance("RSA"); 388 389 PrivateKey key = factory.generatePrivate(keySpec); 390 391 if (certChain == null || certChain.length == 0) { certChain = new java.security.cert.Certificate [1]; certChain[0] = certItem.getX509Cert(); 395 } 396 397 keystore.setKeyEntry(alias, key, password, certChain); 398 399 refreshView(); 400 changed = true; 401 } 402 catch (Exception e) 403 { 404 CBUtility.error("Error importing key file.", e); 405 e.printStackTrace(); 406 } 407 408 } 409 410 416 417 protected void exportKey(CertItem certItem) 418 { 419 try 420 { 421 422 423 if (certItem == null || certItem.getX509Cert() == null) 424 { 425 CBUtility.error(CBIntText.get("Please select a certificate to match with a key."), null); 426 return; 427 } 428 429 430 431 File keyFile = getKeyFile(CBIntText.get("Select a file to save the pkcs8 key to.")); 432 433 if (keyFile == null) 434 return; 436 437 438 if (checkPassword() == false) 439 return; 441 442 443 Key myKey = keystore.getKey(certItem.getAlias(), password); 444 445 if (myKey == null) 446 { 447 return; 448 } 449 byte[] data = myKey.getEncoded(); 450 451 if (data == null) 452 { 453 throw new Exception ("Unable to access encoded private key data"); 454 } 455 456 if (keyFile.toString().toLowerCase().endsWith(".pem")) 457 { 458 data = CBSecurity.convertToPEMPrivateKey(data); 459 } 460 461 FileOutputStream out = new FileOutputStream(keyFile); 462 out.write(data); 463 out.close(); 464 } 465 catch (Exception e) 466 { 467 CBUtility.error("Error exporting key file.", e); 468 e.printStackTrace(); 469 } 470 471 } 472 473 474 475 476 481 482 protected File getKeyFile(String title) 483 { 484 JFileChooser chooser = new JFileChooser(properties.getProperty("cert.homeDir")); 485 chooser.addChoosableFileFilter(new CBFileFilter(new String [] {"der", "pem"},"Certificate Files (*.der, *.pem)")); 486 chooser.setDialogTitle(title); 487 488 int option = chooser.showOpenDialog(owner); 489 490 while (true) 491 { 492 if (option == JFileChooser.APPROVE_OPTION) { 494 File keyFile = chooser.getSelectedFile(); 495 if (keyFile == null) 496 CBUtility.error(CBIntText.get("Please select a file")); 497 else 498 { 499 properties.setProperty("cert.homeDir", keyFile.getParent()); 500 chooser = null; 501 return keyFile; 502 } 503 } 504 else 505 { 506 chooser = null; 507 return null; } 509 } 510 } 511 512 517 518 protected void viewCurrentCert(CertItem cert) 519 { 520 if (cert == null || cert.getX509Cert() == null) { 522 CBUtility.error(CBIntText.get("Please select a certificate to view."), null); 523 return; 524 } 525 526 CertViewer viewer = new CertViewer(owner, cert.getX509Cert()); 527 viewer.setVisible(true); 528 } 529 530 535 536 protected void deleteCurrentCert(CertItem certItem) 537 { 538 539 if (certItem == null) 540 return; 542 int delete = JOptionPane.showConfirmDialog(this, CBIntText.get("delete certificate: {0} ?", new String [] {certItem.getAlias()}), 543 CBIntText.get("Confirm Certificate Deletion"), JOptionPane.OK_CANCEL_OPTION); 544 545 if (delete != JOptionPane.OK_OPTION) 546 return; 548 if (keystore == null) { 550 CBUtility.error(CBIntText.get("Internal Error: unable to find Certificate Keystore"), null); 551 return; 552 } 553 554 if (checkPassword() == false) 555 return; 557 try 558 { 559 keystore.deleteEntry(certItem.getAlias()); 560 561 refreshView(); 562 changed = true; 563 564 return; 565 572 } 573 catch (KeyStoreException e) 574 { 575 CBUtility.error(CBIntText.get("Error - unable to delete key: {0} from key store", new String [] {certItem.getAlias()}), e); 576 } 577 578 try { 581 keystore.setCertificateEntry(certItem.getAlias(), certItem.getX509Cert()); 582 } 583 catch (Exception e) 584 { 585 log.log(Level.WARNING, "unable to recover key store.",e); 586 } 587 } 588 589 590 596 597 protected boolean checkPassword() 598 { 599 if (password != null) 600 return true; 602 return setupPasswordAndKeystore(keystoreType, keystoreFile, this); } 604 605 612 613 public boolean setupPasswordAndKeystore(String keystoreType, String keystoreFile, Component owner) 614 { 615 if ((password != null) && (keystore != null)) return true; 617 618 String message = CBIntText.get("Enter Key Store Password"); 619 while ((password = getPassword(owner, message)) != null) 620 { 621 keystore = readKeyStore(password, keystoreType, keystoreFile); 622 623 if (keystore != null) 624 { 625 return true; } 627 message = CBIntText.get("Password incorrect. Please try again."); 629 } 630 631 return false; } 633 634 public static char[] getPassword(Component owner, String message) 635 { 636 char[] password; 637 JPasswordField passwordInput = new JPasswordField(); 638 int response = JOptionPane.showConfirmDialog(owner, passwordInput, 639 message, JOptionPane.OK_CANCEL_OPTION); 640 641 if (response != JOptionPane.OK_OPTION) 642 password = null; else 644 password = passwordInput.getPassword(); 645 return password; 646 } 647 648 652 653 668 672 673 private boolean listContains(String aliasName) 674 { 675 if (aliasName == null) return false; 676 677 for (int i=0; i<certListModel.size(); i++) 678 if (aliasName.equals(((CertItem)certListModel.get(i)).alias)) 679 return true; 680 681 return false; 682 } 683 684 688 689 protected void addNewCert() 690 { 691 CertViewer.CertAndFileName info = CertViewer.loadCertificate(owner); 692 if (info == null || info.cert == null) 693 { 694 return; } 696 697 String alias = null; 698 699 if (info.fileName != null) 700 { 701 alias = new File(info.fileName).getName(); 702 if (alias != null && alias.indexOf('.')>0) 703 alias = alias.substring(0, alias.indexOf('.')); } 705 706 if (alias == null) 707 alias = CBIntText.get("default"); 708 709 boolean nameAlreadyExists = false; 710 do 711 { 712 alias = (String )JOptionPane.showInputDialog(this, CBIntText.get("Please enter a short unique name for this Certificate"), 713 CBIntText.get("Enter Certificate Alias"), JOptionPane.QUESTION_MESSAGE, null, null, alias); 714 715 nameAlreadyExists = listContains(alias); 716 if (nameAlreadyExists) 717 { 718 JOptionPane.showMessageDialog(this, CBIntText.get("That name already exists."), 719 CBIntText.get("Duplicate Alias"), JOptionPane.ERROR_MESSAGE); 720 } 721 } 722 while (nameAlreadyExists); 723 724 if (alias == null || alias.length() == 0) 725 return; 727 if (checkPassword() == false) 728 return; 730 try 731 { 732 keystore.setCertificateEntry(alias, info.cert); 733 734 refreshView(); 735 changed = true; 736 return; 737 } 738 catch (KeyStoreException e) 739 { 740 CBUtility.error(CBIntText.get("Error - unable to add key: {0} from key store", new String [] {alias}), e); 741 } 742 try 744 { 745 keystore.deleteEntry(alias); } 747 catch (Exception e) 748 {} 749 } 750 751 755 756 protected void refreshView() 757 { 758 CertItem[] certs = getKeyStoreCerts(keystore); 759 760 if (certListModel == null) 761 setupCertificateListGUI(); 762 763 certListModel.removeAllElements(); 764 for (int i=0; i<certs.length; i++) 765 certListModel.addElement(certs[i]); 766 } 767 768 773 774 protected void setupCertificateListGUI() 775 { 776 certListModel = new DefaultListModel(); 777 778 certList.setModel(certListModel); 779 780 certList.setCellRenderer(new CertificateListRenderer()); 781 } 782 783 786 787 protected void setupCertificateList() 788 { 789 792 keystore = readKeyStore(password, keystoreType, keystoreFile); 793 794 setupCertificateListGUI(); 795 796 if (keystore == null) 797 JOptionPane.showMessageDialog(this, CBIntText.get("Unable to find/open keystore: {0}", new String [] {keystoreFile}), CBIntText.get("Error: no Keystore"), JOptionPane.ERROR_MESSAGE); 798 else 799 refreshView(); 800 } 801 802 803 807 808 public class PasswordDialog extends CBDialog 809 { 810 public JPasswordField old, new1, new2; 811 812 public PasswordDialog(Frame owner) 813 { 814 super(owner, CBIntText.get("Change the Key Store Password."), null); 815 addln(new JLabel(getImageIcon("sslpassword.gif"))); 816 addln(new JLabel(CBIntText.get("This screen allows you to enter"))); 817 addln(new JLabel(CBIntText.get("a new key store password"))); 818 addln(new JLabel(" ")); 819 addln(new JLabel(CBIntText.get("Enter the old password"))); 820 addln(old = new JPasswordField()); 821 addln(new JLabel(CBIntText.get("The new Password") + ":")); 822 addln(new1 = new JPasswordField()); 823 addln(new JLabel(CBIntText.get("Confirm the new Password") + ":")); 824 addln(new2 = new JPasswordField()); 825 setSize(240, 320); 826 CBUtility.center(this, owner); 827 } 828 829 } 830 831 836 837 protected void setupPasswords() 838 { 839 PasswordDialog newPassword = new PasswordDialog(owner); 840 841 842 846 while (newPassword.wasCancelled() == false) 847 { 848 newPassword.setVisible(true); 849 850 if (newPassword.wasCancelled()) 851 return; 853 char[] oldPass, newPass1, newPass2; 854 oldPass = newPassword.old.getPassword(); 855 newPass1 = newPassword.new1.getPassword(); 856 newPass2 = newPassword.new2.getPassword(); 857 858 if (Arrays.equals(newPass1, newPass2) == true) 859 { 860 KeyStore newKeystore = readKeyStore(oldPass, keystoreType, keystoreFile); 862 if (newKeystore != null) 863 { 864 if (writeKeyStore(newPass1, newKeystore, keystoreFile, keystoreType) == true) 865 { 866 keystore = newKeystore; 867 password = newPass1; 868 869 JOptionPane.showMessageDialog(this, CBIntText.get("Passwords successfully changed!"), 870 CBIntText.get("Success!"), JOptionPane.INFORMATION_MESSAGE); 871 return; } 873 } 874 else 875 CBUtility.error(CBIntText.get("Unable to change password - incorrect password entered?")); 876 877 } 878 else 879 { 880 CBUtility.error(CBIntText.get("The new passwords were not identical!"), null); 881 } 882 } 883 884 } 885 886 protected void clearPassword(char[] c) 887 { 888 if (c != null) 889 for (int i=0; i<c.length; i++) 890 c[i] = 0; 891 } 892 893 900 901 public static CertItem[] getKeyStoreCerts(KeyStore keystore) 902 { 903 try 904 { 905 Vector certVector = new Vector(10); 907 909 Enumeration a = keystore.aliases(); 910 while ( a.hasMoreElements() ) 911 { 912 String alias = (String ) a.nextElement(); 913 CertItem item = new CertItem(alias); 914 915 if ( keystore.isKeyEntry(alias) ) 916 { 917 X509Certificate userCert = (X509Certificate)keystore.getCertificate(alias); 918 item.addX509Cert(userCert); 919 item.setHasPrivateKey(true); 920 } 921 else 922 { 923 X509Certificate userCert = (X509Certificate)keystore.getCertificate(alias); 924 item.addX509Cert(userCert); 925 } 926 certVector.add(item); 927 } 928 929 return (CertItem[]) certVector.toArray(new CertItem[0]); 930 } 931 catch (Exception e) 932 { 933 CBUtility.error(CBIntText.get("Error reading certificate from keystore."), e); 934 return null; 935 } 936 937 938 } 939 940 951 952 public static KeyStore readKeyStore(char[] pass, String storeType, String keyFile) 953 { 954 956 try 957 { 958 KeyStore keystore = KeyStore.getInstance( storeType ); 960 FileInputStream fis = new FileInputStream(keyFile); 961 keystore.load(fis, pass); 962 963 fis.close(); 964 965 return keystore; 966 } 967 catch (Exception e) 968 { 969 970 CBUtility.error(CBIntText.get("Error opening certificate keystore {0}. Probably an incorrect password", new String [] {keyFile}), e); 971 972 return null; 973 } 974 } 975 976 984 985 public static boolean writeKeyStore(char[] password, KeyStore keystore, String keyFile, String keystoreType) 986 { 987 if ("KSE".equalsIgnoreCase(keystoreType)) 988 { 989 CertItem[] certs = getKeyStoreCerts(keystore); 990 991 if (certs.length > 2) 992 return givePKCS12ErrorMsg(CBIntText.get("This PKCS12 File can only have one certificate, one key, and one CA certificate")); 993 994 if (certs.length == 2 && certs[0].hasPrivateKey && certs[1].hasPrivateKey) 995 return givePKCS12ErrorMsg(CBIntText.get("This PKCS12 File can only have one certificate, one key, and one CA certificate")); 996 997 } 999 FileOutputStream fos = null; 1000 try 1001 { 1002 if (password == null) 1003 throw new KeyStoreException("null password not allowed"); 1004 fos = new FileOutputStream(keyFile); 1005 keystore.store(fos, password); 1006 fos.close(); 1007 return true; 1008 } 1009 catch (Exception e) { 1011 CBUtility.error(CBIntText.get("Error saving certificate keystore.") + 1012 "\n" + CBIntText.get("Probably an invalid password"), e); 1013 1014 if (fos != null) 1016 try {fos.close();} catch(IOException e2) {} 1017 1018 return false; 1019 } 1020 1021 } 1022 1023 1026 1027 private static boolean givePKCS12ErrorMsg(String msg) 1028 { 1029 CBUtility.error(msg); 1030 return false; 1031 } 1032 1033 1034 1038 1039 public static class CertItem 1040 { 1041 public String alias; 1042 1043 public X509Certificate x509Cert = null; 1044 1045 public boolean hasPrivateKey = false; 1046 1047 1054 1055 public CertItem(String certAlias) 1056 { 1057 this(certAlias, null); 1058 } 1059 1060 1067 1068 public CertItem(String certAlias, X509Certificate cert) 1069 { 1070 alias = certAlias; 1071 x509Cert = cert; 1072 } 1073 1074 1075 1079 1080 public void addX509Cert(X509Certificate x) 1081 { 1082 x509Cert = x; 1083 } 1084 1085 1089 1090 public String toString() 1091 { 1092 if (hasPrivateKey) 1093 return "<html><b><font color=black>" + alias + "</font><br><font color=blue>(has private key)</font></b></html>"; 1094 else 1095 return alias; 1096 } 1097 1098 1102 1103 public String getSelectedText() 1104 { 1105 if (hasPrivateKey) 1106 return "<html><b><font color=white>" + alias + "</font><br><font color=white>(has private key)</font></b></html>"; 1107 else 1108 return alias; 1109 } 1110 1111 1115 1116 public String getAlias() 1117 { 1118 return alias; 1119 } 1120 1121 1124 1125 public ImageIcon getIcon() 1126 { 1127 if (hasPrivateKey) 1128 return smallKeyCert; 1129 else 1130 return smallCert; 1131 } 1132 1133 1138 1139 public X509Certificate getX509Cert() 1140 { 1141 return x509Cert; 1142 } 1143 1144 public void setHasPrivateKey(boolean state) 1145 { 1146 hasPrivateKey = state; 1147 } 1148 1149 public boolean getHasPrivateKey() 1150 { 1151 return hasPrivateKey; 1152 } 1153 } 1154 1155 1156 1161 1162 class CertificateListRenderer extends JLabel implements ListCellRenderer 1163 { 1164 Color highlight = new Color(0,0,128); 1166 CertificateListRenderer() 1167 { 1168 setOpaque(true); 1169 } 1170 1171 public Component getListCellRendererComponent(JList list, Object value, int index, 1172 boolean isSelected, boolean cellHasFocus) 1173 { 1174 if (value instanceof CertItem == false) { 1176 System.err.println("Rendering error in KeystoreGUI"); 1177 setText(ERRORCERT); 1178 return this; 1179 } 1180 1181 if (index == -1) 1182 { 1183 index = list.getSelectedIndex(); 1184 if (index == -1) 1185 { 1186 setText("<error>"); 1187 return this; 1188 } 1189 } 1190 1191 if (value == null) { 1193 setBackground(Color.white); 1194 setForeground(Color.gray); 1195 setText("<deleted>"); 1196 return this; 1197 } 1198 1199 CertItem item = (CertItem)value; 1200 1201 setIcon(item.getIcon()); 1202 1203 if (isSelected) 1204 { 1205 setText(item.getSelectedText()); 1206 setBackground(highlight); 1207 setForeground(Color.white); 1208 } 1209 else 1210 { 1211 setText(item.toString()); 1212 setBackground(Color.white); 1213 setForeground(Color.black); 1214 } 1215 return this; 1216 } 1217 } 1218 1219 public ImageIcon getImageIcon(String name) 1220 { 1221 ImageIcon newIcon = new ImageIcon(properties.getProperty("dir.images") + name); 1222 return newIcon; 1223 } 1224 1225 1226 1227 1228 1229 private static void printUsageAndExit() 1230 { 1231 System.out.println("USAGE: java KeystoreGUI [keystore file|path] [keystore password] [keystore type] [provider]\n" + 1232 "(defaults are 'security/clientcerts' and 'jks'"); 1233 System.exit(0); 1234 } 1235 1236 1239 1240 public static void main(String [] argsv) 1241 { 1242 String keystoreType = "jks"; 1243 1244 String provider = null; 1245 1246 String password = null; 1247 1248 Frame rootFrame = new Frame(); 1249 1250 CBUtility.initDefaultDisplay(rootFrame); 1251 1252 1254 System.out.println("running KeystoreGUI 1.0 stand alone demo - Chris Betts 2002\n"); 1255 1256 1257 1258 1259 String localDir = System.getProperty("user.dir") + File.separator; 1260 1261 Properties props = new Properties(); 1262 1263 1268 1269 props.setProperty("cert.homeDir", localDir + "certs" + File.separator); 1270 1271 1275 1276 props.setProperty("dir.images", localDir + "images" + File.separator); 1277 1278 1281 1282 String keystoreName = localDir + "security" + File.separator + "clientcerts"; 1283 1284 if (argsv.length < 1) 1285 printUsageAndExit(); 1286 1287 if (argsv[0].startsWith("-h")) 1288 printUsageAndExit(); 1289 1290 if (argsv[0].length() < 2) 1291 { 1292 keystoreName = argsv[0]; 1293 } 1294 else if (argsv[0].charAt(1) == ':' || argsv[0].charAt(0) == '/') { 1296 keystoreName = argsv[0]; 1297 } 1298 else { 1300 keystoreName = localDir + argsv[0]; 1301 } 1302 1303 1306 1307 if (argsv.length > 1) 1308 { 1309 password = argsv[1]; 1310 } 1311 1312 1315 1316 if (argsv.length > 2) 1317 { 1318 keystoreType = argsv[2]; 1319 } 1320 1321 1324 1325 if (argsv.length > 3) 1326 { 1327 provider = argsv[3]; 1328 1329 try 1331 { 1332 Class providerClass = Class.forName(provider); 1333 Provider providerObject = (Provider)providerClass.newInstance(); 1334 Security.insertProviderAt(providerObject, 1); 1335 System.out.println("\nPROVIDER: " + providerObject.getName() + " v" + providerObject.getVersion() + " has been registered "); 1336 } 1337 catch (Exception e) 1338 { 1339 System.err.println("\n*** unable to load new security provider: " + ((provider==null)?"null":provider)); 1340 System.err.println(e + "\n"); 1341 printUsageAndExit(); 1342 } 1343 } 1344 1345 Provider[] current = Security.getProviders(); 1346 for (int i=0; i<current.length; i++) 1347 System.out.println("registered security providers: " + i + " = " + current[i].getName() + " " + current[i].getInfo()); 1348 1349 1351 class StandaloneKeystore extends KeystoreGUI 1352 { 1353 public StandaloneKeystore( Frame owner, Properties props, String keyStoreLocation, char[] pwd, String keystoreType, String title, boolean handlePrivateKeys, String helpTopic) 1354 { 1355 super(owner, props, keyStoreLocation, pwd, keystoreType, title, handlePrivateKeys, helpTopic); 1356 } 1357 1358 public void doOK() 1359 { 1360 super.doOK(); 1361 System.exit(0); 1362 } 1363 1364 public void doCancel() 1365 { 1366 super.doCancel(); 1367 System.exit(0); 1368 } 1369 } 1370 1371 char[] pwd = null; 1373 StandaloneKeystore gui = new StandaloneKeystore(rootFrame, props, keystoreName, pwd, keystoreType, "CB Keystore GUI Demo", true, null); 1374 gui.setSize(450,440); 1375 1376 CBUtility.center(gui, null); 1377 1378 gui.setVisible(true); 1379 } 1380 1381 1382} 1383 | Popular Tags |