PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/carrot-jdk6-jnlp-macosx/src/common/share/classes/com/sun/deploy/security/X509DeployKeyManager.java

https://github.com/carrot-garden/carrot-jnlper
Java | 774 lines | 573 code | 107 blank | 94 comment | 149 complexity | aa85fc69648e458d45ea80f7945ac87e MD5 | raw file
  1. /*
  2. * @(#)X509DeployKeyManager.java 1.36 10/05/21
  3. *
  4. * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  5. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.deploy.security;
  8. import java.io.BufferedInputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.io.FileNotFoundException;
  13. import java.net.Socket;
  14. import java.security.AccessController;
  15. import java.security.UnrecoverableKeyException;
  16. import java.security.KeyStore;
  17. import java.security.KeyStore.Builder;
  18. import java.security.KeyStore.CallbackHandlerProtection;
  19. import java.security.Principal;
  20. import java.security.PrivateKey;
  21. import java.security.KeyStoreException;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.security.NoSuchProviderException;
  24. import java.security.cert.CertificateExpiredException;
  25. import java.security.cert.CertificateNotYetValidException;
  26. import java.security.cert.X509Certificate;
  27. import java.security.cert.Certificate;
  28. import java.security.cert.CertificateException;
  29. import java.security.PrivilegedAction;
  30. import java.security.PrivilegedExceptionAction;
  31. import java.security.PrivilegedActionException;
  32. import java.util.HashMap;
  33. import java.util.Arrays;
  34. import java.util.Iterator;
  35. import javax.net.ssl.X509KeyManager;
  36. import javax.net.ssl.KeyManager;
  37. import javax.net.ssl.KeyManagerFactory;
  38. import javax.net.ssl.SSLEngine;
  39. import javax.net.ssl.ManagerFactoryParameters;
  40. import javax.net.ssl.KeyStoreBuilderParameters;
  41. import javax.net.ssl.HandshakeCompletedListener;
  42. import javax.net.ssl.HandshakeCompletedEvent;
  43. import javax.net.ssl.SSLSocket;
  44. import javax.swing.JButton;
  45. import javax.swing.JLabel;
  46. import javax.swing.JPasswordField;
  47. import com.sun.deploy.config.Config;
  48. import com.sun.deploy.resources.ResourceManager;
  49. import com.sun.deploy.util.Trace;
  50. import com.sun.deploy.util.DeploySysRun;
  51. import com.sun.deploy.util.DeploySysAction;
  52. import com.sun.deploy.ui.UIFactory;
  53. import com.sun.deploy.services.Service;
  54. import com.sun.deploy.services.ServiceManager;
  55. import java.net.PasswordAuthentication;
  56. import java.lang.reflect.Method;
  57. /**
  58. * This class implements a simple key management policy, deciding what
  59. * certificate chains should be used. That is, it is a "trust provider"
  60. * which is driven <em>purely</em> by locally stored certificates. It
  61. * can be administratively substituted by another policy.
  62. *
  63. * @version 1.0
  64. * @author Dennis Gu
  65. */
  66. // NOTE: this is final because we've not yet subclassed this and we don't
  67. // know all of what's involved. This _should_ probably get subclassed, if
  68. // for no other reason than to enable user (or perhaps remote administrator)
  69. // participation in trust decisions.
  70. //
  71. // Subclassing is like any other interface though: it needs customers
  72. // before it has a prayer of being done right. And support for external
  73. // annotations, like enabling/disabling or scoping the trust extended.
  74. public final class X509DeployKeyManager implements X509KeyManager {
  75. private X509KeyManager myKeyManager = null;
  76. private X509KeyManager browserKeyManager = null;
  77. private String userKeyStore = null;
  78. private String systemKeyStore = null;
  79. private KeyStore browserKeyStore = null;
  80. private boolean isWindows =
  81. (Config.getOSName().indexOf("Windows") != -1);
  82. // Cached hostname and client authentication certificate
  83. private static HashMap clientAuthCertsCachedMap = new HashMap();
  84. // Thread local storage to remember if client certificate dialog was
  85. // cancelled by the users to avoid future recursive calls from JSSE.
  86. //
  87. private static ThreadLocal clientCertDialogCancelled = new ThreadLocal()
  88. {
  89. protected synchronized Object initialValue() {
  90. return Boolean.FALSE;
  91. }
  92. };
  93. // Thread local storage to remember if password dialog was
  94. // cancelled by the users to avoid future recursive calls from JSSE.
  95. //
  96. private static ThreadLocal passwdDialogCancelled = new ThreadLocal() {
  97. protected synchronized Object initialValue() {
  98. return Boolean.FALSE;
  99. }
  100. };
  101. /*
  102. */
  103. public X509DeployKeyManager()
  104. {
  105. // Get client auth certificate file deployment.clientauthcerts
  106. userKeyStore = Config.getUserClientAuthCertFile();
  107. systemKeyStore = Config.getSystemClientAuthCertFile();
  108. // see if user is allowed to use browser keystore
  109. if (Config.getBooleanProperty(Config.SEC_USE_BROWSER_KEYSTORE_KEY))
  110. {
  111. Service service = ServiceManager.getService();
  112. browserKeyStore = service.getBrowserClientAuthKeyStore();
  113. }
  114. }
  115. private void init() throws KeyStoreException,
  116. NoSuchAlgorithmException,
  117. NoSuchProviderException,
  118. FileNotFoundException,
  119. IOException,
  120. UnrecoverableKeyException,
  121. CertificateException
  122. {
  123. try
  124. {
  125. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  126. public Object run() throws KeyStoreException,
  127. NoSuchAlgorithmException,
  128. NoSuchProviderException, FileNotFoundException,
  129. IOException, UnrecoverableKeyException,
  130. CertificateException
  131. {
  132. do_init();
  133. return null;
  134. }
  135. });
  136. } catch (PrivilegedActionException e) {
  137. Exception ex = e.getException();
  138. if (ex instanceof KeyStoreException) {
  139. throw (KeyStoreException)ex;
  140. } else if (ex instanceof NoSuchAlgorithmException) {
  141. throw (NoSuchAlgorithmException)ex;
  142. } else if (ex instanceof NoSuchProviderException) {
  143. throw (NoSuchProviderException)ex;
  144. } else if (ex instanceof FileNotFoundException) {
  145. throw (FileNotFoundException)ex;
  146. } else if (ex instanceof IOException) {
  147. throw (IOException)ex;
  148. } else if (ex instanceof UnrecoverableKeyException) {
  149. throw (UnrecoverableKeyException)ex;
  150. } else if (ex instanceof CertificateException) {
  151. throw (CertificateException)ex;
  152. } else {
  153. Trace.securityPrintException(e);
  154. }
  155. }
  156. }
  157. /**
  158. * Initialize keyManager
  159. */
  160. private void do_init() throws KeyStoreException,
  161. NoSuchAlgorithmException,
  162. NoSuchProviderException,
  163. FileNotFoundException,
  164. IOException,
  165. UnrecoverableKeyException,
  166. CertificateException
  167. {
  168. // Get browserKeyManager
  169. browserKeyManager = getBrowserKeyManager(browserKeyStore);
  170. // Get myKeyManager
  171. // Runnig JRE 1.6 and later
  172. if (Config.isJavaVersionAtLeast16()) {
  173. myKeyManager = getNewMyKeyManager(userKeyStore, systemKeyStore);
  174. }
  175. else {
  176. // For running old JRE in Java Webstart
  177. myKeyManager = getLegacyMyKeyManager(userKeyStore);
  178. }
  179. }
  180. /**
  181. */
  182. public synchronized String chooseClientAlias(final String[] keyType,
  183. final Principal[] issuers, final Socket socket) {
  184. HashMap clientAuthCertsMap = new HashMap();
  185. HashMap clientAuthTypeMap = new HashMap();
  186. String certName = null;
  187. String hostname = null;
  188. if (clientCertDialogCancelled.get().equals(Boolean.FALSE))
  189. {
  190. // For JFB only fix 6357710
  191. // Use reflection to access SSLSocketImpl class and method
  192. // Get remote hostname
  193. // 6951688: fix for 6357710 to be made available for SE
  194. try {
  195. Class sslClazz = Class.forName("com.sun.net.ssl.internal.ssl.SSLSocketImpl");
  196. // if (socket instanceof sslClass)
  197. boolean sslImplFlag = sslClazz.isInstance(socket);
  198. if (sslImplFlag) {
  199. //SSLSocketImpl socketImpl = (SSLSocketImpl)socket;
  200. Object SSLSocketImplObj = sslClazz.cast(socket);
  201. // hostname = socketImpl.getHost();
  202. final Method privateGetHostMethod = sslClazz.getDeclaredMethod("getHost", null);
  203. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  204. public Object run() throws IOException {
  205. privateGetHostMethod.setAccessible(true);
  206. return null;
  207. }
  208. });
  209. hostname = (String)privateGetHostMethod.invoke(SSLSocketImplObj, null);
  210. }
  211. } catch (Exception ex) {
  212. Trace.msgSecurityPrintln("clientauth.readFromCache.failed");
  213. if (ex != null) {
  214. Trace.msgSecurityPrintln(ex.toString());
  215. }
  216. }
  217. // Get the list of matched certs name
  218. for (int i = 0; i < keyType.length; i++)
  219. {
  220. String[] aliases = getClientAliases(keyType[i], issuers);
  221. if ((aliases != null) && (aliases.length > 0))
  222. {
  223. for (int j=0; j<aliases.length; j++)
  224. {
  225. int certTypeLen = CertType.PLUGIN.getType().length();
  226. String newAlias = aliases[j].substring(certTypeLen);
  227. X509Certificate[] certs = getCertificateChain(newAlias);
  228. // Check client certificate before display to user
  229. try {
  230. if (CertUtils.checkTLSClient(certs[0])) {
  231. clientAuthCertsMap.put(newAlias, certs);
  232. if (aliases[j].startsWith(CertType.PLUGIN.getType())) {
  233. clientAuthTypeMap.put(newAlias, CertType.PLUGIN);
  234. }
  235. if (aliases[j].startsWith(CertType.BROWSER.getType())) {
  236. clientAuthTypeMap.put(newAlias, CertType.BROWSER);
  237. }
  238. }
  239. } catch (CertificateException ce) {
  240. Trace.msgSecurityPrintln("clientauth.checkTLSClient.failed",
  241. new Object[]{ newAlias});
  242. }
  243. }
  244. }
  245. }
  246. // Display the dialog box for user to select
  247. // Only display when user didn't hit cancel button
  248. if (passwdDialogCancelled.get().equals(Boolean.FALSE))
  249. {
  250. final HashMap theClientAuthCertsMap = clientAuthCertsMap;
  251. final HashMap theClientAuthTypeMap = clientAuthTypeMap;
  252. // For JFB only fix 6357710
  253. // Check cache value first if it is available
  254. // 6951688: Fix for 6357710 to be made available for SE
  255. if (hostname != null && clientAuthCertsCachedMap.size() > 0) {
  256. Iterator hostCachedItor = clientAuthCertsCachedMap.keySet().iterator();
  257. String cachedHostname = null;
  258. String certNameCached = null;
  259. while (hostCachedItor.hasNext()) {
  260. cachedHostname = (String)hostCachedItor.next();
  261. if (cachedHostname.compareToIgnoreCase(hostname)==0) {
  262. certNameCached = (String)clientAuthCertsCachedMap.get(cachedHostname);
  263. Trace.msgSecurityPrintln("clientauth.readFromCache.success",
  264. new Object[]{ certNameCached});
  265. return certNameCached;
  266. }
  267. }
  268. }
  269. // No cache value available
  270. if (Config.getBooleanProperty(Config.SEC_USE_CLIENTAUTH_AUTO_KEY) &&
  271. (theClientAuthCertsMap.size() == 1))
  272. {
  273. Object[] certNameArray = theClientAuthCertsMap.keySet().toArray();
  274. certName = (String) certNameArray[0];
  275. }
  276. else {
  277. DeploySysAction action = new DeploySysAction() {
  278. public Object execute() {
  279. return ClientCertDialog.showDialog(
  280. theClientAuthCertsMap, theClientAuthTypeMap);
  281. }
  282. };
  283. certName = (String)
  284. DeploySysRun.executePrivileged(action, null);
  285. } // more than one cert
  286. }
  287. // set flag if user click on cancel button
  288. if (certName == null) {
  289. clientCertDialogCancelled.set(Boolean.TRUE);
  290. }
  291. // For JFB only fix 6357710
  292. // add handshake listener
  293. // 6951688: Fix for 6357710 to made for SE
  294. if (socket instanceof SSLSocket) {
  295. HandshakeCompletedListener myListener = new MyListener(hostname, certName);
  296. ((SSLSocket)socket).addHandshakeCompletedListener(myListener);
  297. }
  298. return certName;
  299. } else {
  300. return null;
  301. }
  302. }
  303. public String chooseEngineClientAlias(String[] keyType,
  304. Principal[] issuers, SSLEngine engine) {
  305. return chooseClientAlias(keyType, issuers, null);
  306. }
  307. public synchronized String chooseServerAlias(final String keyType,
  308. final Principal[] issuers, final Socket socket) {
  309. try {
  310. if ((myKeyManager == null && browserKeyManager == null) &&
  311. passwdDialogCancelled.get().equals(Boolean.FALSE)) {
  312. init();
  313. }
  314. } catch (Exception e) {
  315. e.printStackTrace();
  316. }
  317. String serverAliasName = null;
  318. if (myKeyManager != null) {
  319. serverAliasName =
  320. myKeyManager.chooseServerAlias(keyType, issuers, socket);
  321. }
  322. if (serverAliasName == null && browserKeyManager != null) {
  323. serverAliasName = browserKeyManager.chooseServerAlias(
  324. keyType, issuers, socket);
  325. }
  326. return serverAliasName;
  327. }
  328. public String chooseEngineServerAlias(String keyType,
  329. Principal[] issuers, SSLEngine engine) {
  330. return chooseServerAlias(keyType, issuers, null);
  331. }
  332. public synchronized X509Certificate[] getCertificateChain(
  333. final String alias) {
  334. try {
  335. if ((myKeyManager == null && browserKeyManager == null) &&
  336. passwdDialogCancelled.get().equals(Boolean.FALSE)) {
  337. init();
  338. }
  339. } catch (Exception e) {
  340. e.printStackTrace();
  341. }
  342. X509Certificate[] certChain = null;
  343. if (myKeyManager != null) {
  344. if (!alias.contains("Mozilla") && !alias.contains("MSCrypto")) {
  345. certChain = myKeyManager.getCertificateChain(alias);
  346. }
  347. }
  348. if (certChain == null && browserKeyManager != null) {
  349. certChain = browserKeyManager.getCertificateChain(alias);
  350. }
  351. return certChain;
  352. }
  353. public synchronized String[] getClientAliases(final String keyType,
  354. final Principal[] issuers) {
  355. try {
  356. if ((myKeyManager == null && browserKeyManager == null) &&
  357. passwdDialogCancelled.get().equals(Boolean.FALSE)) {
  358. init();
  359. }
  360. } catch (Exception e) {
  361. e.printStackTrace();
  362. }
  363. String[] myClientAliases = null;
  364. String[] browserClientAliases = null;
  365. if (myKeyManager != null) {
  366. myClientAliases =
  367. myKeyManager.getClientAliases(keyType, issuers);
  368. }
  369. if (browserKeyManager != null) {
  370. browserClientAliases =
  371. browserKeyManager.getClientAliases(keyType, issuers);
  372. }
  373. if (myClientAliases == null) {
  374. if (browserClientAliases != null) {
  375. for (int i=0; i<browserClientAliases.length; i++){
  376. browserClientAliases[i] = CertType.BROWSER.getType() + browserClientAliases[i];
  377. }
  378. }
  379. return browserClientAliases;
  380. } else if (browserClientAliases == null) {
  381. if (myClientAliases != null) {
  382. for (int i=0; i<myClientAliases.length; i++){
  383. myClientAliases[i] = CertType.PLUGIN.getType() + myClientAliases[i];
  384. }
  385. }
  386. return myClientAliases;
  387. } else {
  388. for (int i=0; i<myClientAliases.length; i++)
  389. myClientAliases[i] = CertType.PLUGIN.getType() + myClientAliases[i];
  390. for (int i=0; i<browserClientAliases.length; i++)
  391. browserClientAliases[i] = CertType.BROWSER.getType() + browserClientAliases[i];
  392. String[] temp = new String[
  393. myClientAliases.length + browserClientAliases.length];
  394. System.arraycopy(myClientAliases, 0,
  395. temp, 0, myClientAliases.length);
  396. System.arraycopy(browserClientAliases, 0,
  397. temp, myClientAliases.length,
  398. browserClientAliases.length);
  399. return temp;
  400. }
  401. }
  402. public synchronized String[] getServerAliases(final String keyType,
  403. final Principal[] issuers) {
  404. try {
  405. if ((myKeyManager == null && browserKeyManager == null) &&
  406. passwdDialogCancelled.get().equals(Boolean.FALSE)) {
  407. init();
  408. }
  409. } catch (Exception e) {
  410. e.printStackTrace();
  411. }
  412. String[] myServerAliases = null;
  413. String[] browserServerAliases = null;
  414. if (myKeyManager != null) {
  415. myServerAliases =
  416. myKeyManager.getServerAliases(keyType, issuers);
  417. }
  418. if (browserKeyManager != null) {
  419. browserServerAliases =
  420. browserKeyManager.getServerAliases(keyType, issuers);
  421. }
  422. if (myServerAliases == null) {
  423. return browserServerAliases;
  424. } else if (browserServerAliases == null) {
  425. return myServerAliases;
  426. } else {
  427. String[] temp = new String[
  428. myServerAliases.length + browserServerAliases.length];
  429. System.arraycopy(myServerAliases, 0,
  430. temp, 0, myServerAliases.length);
  431. System.arraycopy(browserServerAliases, 0,
  432. temp, myServerAliases.length, browserServerAliases.length);
  433. return temp;
  434. }
  435. }
  436. public PrivateKey getPrivateKey(String alias) {
  437. try {
  438. if ((myKeyManager == null && browserKeyManager == null) &&
  439. passwdDialogCancelled.get().equals(Boolean.FALSE)) {
  440. init();
  441. }
  442. } catch (Exception e) {
  443. e.printStackTrace();
  444. }
  445. PrivateKey privateKey = null;
  446. if (myKeyManager != null) {
  447. if (!alias.contains("Mozilla") && !alias.contains("MSCrypto")) {
  448. privateKey = ((X509KeyManager)myKeyManager).getPrivateKey(alias);
  449. }
  450. }
  451. if (privateKey == null && browserKeyManager != null) {
  452. privateKey =
  453. ((X509KeyManager)browserKeyManager).getPrivateKey(alias);
  454. }
  455. return privateKey;
  456. }
  457. private X509KeyManager getBrowserKeyManager(KeyStore myBrowserKeyStore)
  458. throws KeyStoreException, NoSuchAlgorithmException,
  459. NoSuchProviderException, FileNotFoundException,
  460. IOException, UnrecoverableKeyException,
  461. CertificateException
  462. {
  463. X509KeyManager myBrowserKeyManager = null;
  464. if (myBrowserKeyStore != null)
  465. {
  466. // Load browser keystore
  467. myBrowserKeyStore.load(null, new char[0]);
  468. // Obtain key manager factory
  469. KeyManagerFactory kmf =
  470. KeyManagerFactory.getInstance("SunX509", "SunJSSE");
  471. // Initialize key manager factory
  472. kmf.init(myBrowserKeyStore, new char[0]);
  473. KeyManager[] kmArray = kmf.getKeyManagers();
  474. // Loop through until we find X509KeyManager object
  475. int i = 0;
  476. while (i < kmArray.length)
  477. {
  478. if (kmArray[i] instanceof X509KeyManager)
  479. {
  480. myBrowserKeyManager = (X509KeyManager) kmArray[i];
  481. break;
  482. }
  483. else
  484. i++;
  485. } //end while loop
  486. }
  487. return myBrowserKeyManager;
  488. }
  489. private X509KeyManager getNewMyKeyManager(String userMykeyStore, String systemMykeyStore)
  490. throws KeyStoreException, NoSuchAlgorithmException,
  491. NoSuchProviderException, FileNotFoundException,
  492. IOException, UnrecoverableKeyException,
  493. CertificateException
  494. {
  495. // For running old JRE in Java Webstart
  496. X509KeyManager myNewKeyManager = null;
  497. File userKeyStoreFile = new File(userMykeyStore);
  498. File systemKeyStoreFile = new File(systemMykeyStore);
  499. if (userKeyStoreFile.exists() || systemKeyStoreFile.exists()) {
  500. try {
  501. // Specify keystore builder parameters for PKCS#12
  502. PasswordCallbackHandler usermcb = new PasswordCallbackHandler
  503. ("clientauth.user.password.dialog.text");
  504. PasswordCallbackHandler sysmcb = new PasswordCallbackHandler
  505. ("clientauth.system.password.dialog.text");
  506. Builder fsBuilder = null;
  507. Builder ssBuilder = null;
  508. if (userKeyStoreFile.exists()){
  509. fsBuilder = Builder.newInstance("JKS", null,
  510. userKeyStoreFile, new CallbackHandlerProtection(usermcb));
  511. }
  512. if (systemKeyStoreFile.exists()) {
  513. ssBuilder = Builder.newInstance("JKS", null,
  514. systemKeyStoreFile, new CallbackHandlerProtection(sysmcb));
  515. }
  516. // Wrap them as key manager parameters
  517. ManagerFactoryParameters ksParams = new KeyStoreBuilderParameters
  518. (Arrays.asList(new Builder[] {fsBuilder, ssBuilder}));
  519. // Obtain key manager factory
  520. KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
  521. // Initialize key manager factory
  522. kmf.init(ksParams);
  523. KeyManager[] kmArray = kmf.getKeyManagers();
  524. // Loop through until we find X509KeyManager object
  525. int i = 0;
  526. while (i < kmArray.length) {
  527. if (kmArray[i] instanceof X509KeyManager) {
  528. myNewKeyManager = (X509KeyManager) kmArray[i];
  529. break;
  530. }
  531. else {
  532. i++;
  533. }
  534. } //end while loop
  535. }
  536. catch (Exception ioe) {
  537. ioe.printStackTrace();
  538. if (Trace.isAutomationEnabled() == false) {
  539. // Show error message
  540. String errorMsg = getMessage("clientauth.password.dialog.error.text");
  541. String errorTitle = getMessage("clientauth.password.dialog.error.caption");
  542. UIFactory.showExceptionDialog(null, ioe, errorMsg, errorTitle);
  543. }
  544. }
  545. }
  546. return myNewKeyManager;
  547. }
  548. private X509KeyManager getLegacyMyKeyManager(String userMykeyStore)
  549. throws KeyStoreException, NoSuchAlgorithmException,
  550. NoSuchProviderException, FileNotFoundException,
  551. IOException, UnrecoverableKeyException,
  552. CertificateException
  553. {
  554. // For running old JRE in Java Webstart
  555. X509KeyManager myLegacyKeyManager = null;
  556. File mykeyStoreFile = new File(userMykeyStore);
  557. if (mykeyStoreFile.exists()) {
  558. boolean tryAgain = true;
  559. // Loop until either keystore is loaded or is cancelled by user.
  560. while (tryAgain) {
  561. try {
  562. // Pop up password dialog box to get keystore password
  563. char[] keyPassphrase =
  564. getPasswordDialog("clientauth.user.password.dialog.text");
  565. // If password dialog is cancelled.
  566. if (passwdDialogCancelled.get().equals(Boolean.TRUE)) {
  567. break;
  568. }
  569. // Obtain key store
  570. String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType");
  571. if (keyStoreType == null) {
  572. keyStoreType = "JKS";
  573. }
  574. KeyStore ks = KeyStore.getInstance(keyStoreType);
  575. ks.load(new BufferedInputStream(
  576. new FileInputStream(userMykeyStore)), keyPassphrase);
  577. // Obtain key manager factory
  578. KeyManagerFactory kmf =
  579. KeyManagerFactory.getInstance("SunX509", "SunJSSE");
  580. // Initialize key manager factory
  581. kmf.init(ks, keyPassphrase);
  582. KeyManager[] kmArray = kmf.getKeyManagers();
  583. // Loop through until we find X509KeyManager object
  584. int i = 0;
  585. while (i < kmArray.length)
  586. {
  587. if (kmArray[i] instanceof X509KeyManager)
  588. {
  589. myLegacyKeyManager = (X509KeyManager) kmArray[i];
  590. break;
  591. }
  592. else
  593. i++;
  594. } //end while loop
  595. tryAgain = false;
  596. }
  597. catch (IOException ioe)
  598. {
  599. ioe.printStackTrace();
  600. if (Trace.isAutomationEnabled() == false)
  601. {
  602. // Show error message
  603. String errorMsg = getMessage( "clientauth.password.dialog.error.text");
  604. String errorTitle = getMessage( "clientauth.password.dialog.error.caption");
  605. UIFactory.showExceptionDialog(null, ioe, errorMsg, errorTitle);
  606. }
  607. }
  608. } // While loop
  609. } // KeyStoreFile exist
  610. return myLegacyKeyManager;
  611. }
  612. private char[] getPasswordDialog(final String inLabel)
  613. {
  614. // pass translated strings to the dialog.
  615. CredentialInfo passwordInfo = UIFactory.showPasswordDialog(null,
  616. getMessage("password.dialog.title"),
  617. getMessage(inLabel), false, false, null, false);
  618. // If user pressed "Cancel" in password dialog, return value
  619. // from dialog will be null. Check here if user pressed "Cancel"
  620. if ( passwordInfo == null ) {
  621. passwdDialogCancelled.set(Boolean.TRUE);
  622. return null;
  623. } else {
  624. return passwordInfo.getPassword();
  625. }
  626. }
  627. /**
  628. * Method to get an internationalized string from the resource.
  629. */
  630. private static String getMessage(String key) {
  631. return ResourceManager.getMessage(key);
  632. }
  633. private static int getAcceleratorKey(String key) {
  634. return ResourceManager.getAcceleratorKey(key);
  635. }
  636. /**
  637. * Implement HandshakeCompletedListener to add valid client certificate into cache.
  638. */
  639. final static class MyListener implements HandshakeCompletedListener {
  640. private String hostname;
  641. private String certName;
  642. public MyListener(String socketHostName, String aliasName) {
  643. hostname = socketHostName;
  644. certName = aliasName;
  645. }
  646. public void handshakeCompleted(HandshakeCompletedEvent event) {
  647. SSLSocket sslSocket = event.getSocket();
  648. // Add this client certificate into cache
  649. if (hostname != null) {
  650. clientAuthCertsCachedMap.put(hostname, certName);
  651. }
  652. // remove this listener
  653. sslSocket.removeHandshakeCompletedListener(this);
  654. }
  655. // We want to have only one listener for the same hostname
  656. public boolean equals(Object obj) {
  657. if (!(obj instanceof MyListener)) return false;
  658. MyListener newListener = (MyListener)obj;
  659. if (newListener.hostname.compareToIgnoreCase(this.hostname) == 0) {
  660. return true;
  661. }
  662. else {
  663. return false;
  664. }
  665. }
  666. }
  667. }