Changeset 7303
- Timestamp:
- 09/08/10 11:53:20 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/EsgYadisParser/src/org/earthsystemgrid/security/yadis/YadisTrustManager.java
r7275 r7303 1 1 package org.earthsystemgrid.security.yadis; 2 2 3 import java.io.FileInputStream; 3 4 import java.io.FileNotFoundException; 4 5 import java.io.IOException; … … 11 12 import java.util.Enumeration; 12 13 14 import javax.net.ssl.TrustManager; 15 import javax.net.ssl.TrustManagerFactory; 13 16 import javax.net.ssl.X509TrustManager; 14 17 15 18 16 public class YadisTrustManager implements X509TrustManager 17 { 19 class YadisTrustManager implements X509TrustManager { 20 18 21 protected Principal [] certificateDnWhiteList; 19 protected KeyStore keyStore;20 22 21 YadisTrustManager(String keyStoreFilePath, char [] keyStorePassword, 22 Principal [] certificateDnWhiteList) throws 23 NoSuchAlgorithmException, CertificateException, IOException, 24 KeyStoreException 25 { 23 /* 24 * The default PKIX X509TrustManager9. We'll delegate 25 * decisions to it, and fall back to the logic in this class if the 26 * default X509TrustManager doesn't trust it. 27 */ 28 X509TrustManager pkixTrustManager; 29 30 YadisTrustManager(Principal[] certificateDnWhiteList) throws Exception { 31 26 32 this.certificateDnWhiteList = certificateDnWhiteList; 27 33 28 if (keyStoreFilePath == null) 29 { 30 return; 31 } 32 33 // Create/load keystore 34 keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 34 // create a "default" JSSE X509TrustManager. 35 35 36 java.io.FileInputStream fis = null; 37 try 38 { 39 fis = new java.io.FileInputStream(keyStoreFilePath); 40 keyStore.load(fis, keyStorePassword); 41 } 42 finally 43 { 44 if (fis != null) 45 { 46 fis.close(); 47 } 48 } 49 } 36 KeyStore ks = KeyStore.getInstance("JKS"); 37 ks.load(new FileInputStream("trustedCerts"), 38 "passphrase".toCharArray()); 50 39 51 public X509Certificate[] getAcceptedIssuers() 52 { 53 X509Certificate [] certList = {}; 54 return certList; 55 56 Enumeration aliases; 57 try 58 { 59 aliases = keyStore.aliases(); 60 } 61 catch (KeyStoreException e) 62 { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 return null; 66 } 67 68 String alias; 69 try 70 { 71 int i=0; 72 while ((alias = (String) aliases.nextElement()) != null) 73 { 74 certList[i] = (X509Certificate)keyStore.getCertificate(alias); 75 i++; 76 } 77 } 78 catch (KeyStoreException e) 79 { 80 return null; 81 } 40 TrustManagerFactory tmf = 41 TrustManagerFactory.getInstance("PKIX"); 42 tmf.init(ks); 82 43 83 return certList; 84 } 44 TrustManager tms [] = tmf.getTrustManagers(); 85 45 86 @Override 87 public void checkClientTrusted(X509Certificate[] arg0, String arg1) 88 throws CertificateException { 89 // TODO Auto-generated method stub 90 91 } 46 /* 47 * Iterate over the returned trustmanagers, look 48 * for an instance of X509TrustManager. If found, 49 * use that as our "default" trust manager. 50 */ 51 for (int i = 0; i < tms.length; i++) { 52 if (tms[i] instanceof X509TrustManager) { 53 pkixTrustManager = (X509TrustManager) tms[i]; 54 return; 55 } 56 } 92 57 93 @Override 94 public void checkServerTrusted(X509Certificate[] certList, String arg1) 95 throws CertificateException 96 { 97 // TODO Auto-generated method stub 58 /* 59 * Find some other way to initialize, or else we have to fail the 60 * constructor. 61 */ 62 throw new Exception("Couldn't initialize"); 63 } 64 65 /* 66 * Delegate to the default trust manager. 67 */ 68 @Override 69 public void checkClientTrusted(X509Certificate[] chain, String authType) 70 throws CertificateException { 71 try { 72 pkixTrustManager.checkClientTrusted(chain, authType); 73 } catch (CertificateException excep) { 74 // do any special handling here, or rethrow exception. 75 } 76 } 77 78 /* 79 * Delegate to the default trust manager but append DN whitelist checking 80 */ 81 @Override 82 public void checkServerTrusted(X509Certificate[] certList, String authType) 83 throws CertificateException { 84 try { 85 pkixTrustManager.checkServerTrusted(certList, authType); 86 } catch (CertificateException excep) { 87 /* 88 * Possibly pop up a dialog box asking whether to trust the 89 * cert chain. 90 */ 91 } 92 93 // Whitelisting of DNs 98 94 Principal subject = null; 99 95 … … 118 114 "against Certificate DN whitelist"); 119 115 } 116 117 118 /* 119 * Merely pass this through. 120 */ 121 @Override 122 public X509Certificate[] getAcceptedIssuers() { 123 return pkixTrustManager.getAcceptedIssuers(); 124 } 120 125 } 121
Note: See TracChangeset
for help on using the changeset viewer.