1 23 package com.sun.enterprise.security; 24 25 import com.sun.enterprise.security.SecurityUtil; 26 import com.sun.enterprise.server.pluggable.SecuritySupport; 27 28 import java.io.IOException ; 29 import java.security.KeyStore ; 30 import java.security.KeyStoreException ; 31 import java.util.Enumeration ; 32 import javax.net.ssl.KeyManagerFactory; 33 import javax.net.ssl.TrustManagerFactory; 34 import javax.net.ssl.KeyManager; 35 import javax.net.ssl.TrustManager; 36 import javax.net.ssl.X509KeyManager; 37 38 import org.apache.tomcat.util.net.jsse.JSSE14SocketFactory; 39 import org.apache.tomcat.util.net.jsse.JSSEKeyManager; 40 41 46 public class NSSSocketFactory extends JSSE14SocketFactory { 47 48 final public static String INTERNAL_TOKEN = "NSS Certificate DB"; 49 50 public NSSSocketFactory() { 51 super(); 52 } 53 54 55 58 protected KeyStore getKeystore(String type, String pass) throws IOException { 59 String keyAlias = (String )attributes.get("keyAlias"); 60 String token = getTokenFromKeyAlias(keyAlias); 61 SecuritySupport secSupp = SecurityUtil.getSecuritySupport(); 62 KeyStore ks = secSupp.getKeyStore(token); 63 if (ks==null) { 64 throw new IOException ("keystore not found for token " + token); 65 } 66 return ks; 67 } 68 69 70 78 protected KeyStore getTrustStore(String keystoreType) throws IOException { 79 try { 80 return SSLUtils.mergingTrustStores( 81 SecurityUtil.getSecuritySupport().getTrustStores()); 82 } catch (Exception ex) { 83 throw new IOException (ex.getMessage()); 84 } 85 } 86 87 90 protected KeyManager[] getKeyManagers(String keystoreType, 91 String algorithm, 92 String keyAlias) 93 throws Exception { 94 KeyManager[] kms = null; 95 SecuritySupport secSupp = SecurityUtil.getSecuritySupport(); 96 String token=getTokenFromKeyAlias(keyAlias); 97 String certAlias = getCertAliasFromKeyAlias(keyAlias); 98 String keystorePass = secSupp.getKeyStorePassword(token); 99 KeyStore ks = secSupp.getKeyStore(token); 100 if (ks==null) { 101 throw new IOException ("keystore not found for token " + token); 102 } 103 KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); 104 kmf.init(ks, keystorePass.toCharArray()); 105 kms = kmf.getKeyManagers(); 106 for(int i=0; certAlias!=null && i<kms.length; i++) { 107 kms[i] = new JSSEKeyManager((X509KeyManager)kms[i], certAlias); 108 } 109 return kms; 110 } 111 112 private static String getTokenFromKeyAlias(String keyAlias) { 113 String token = null; 114 if (keyAlias!=null) { 115 int idx = keyAlias.indexOf(':'); 116 if (idx != -1) { 117 token = keyAlias.substring(0, idx); 118 } 119 } 120 if (token==null) { 121 token = INTERNAL_TOKEN; 122 } else { 123 token = token.trim(); 124 } 125 return token; 126 } 127 128 145 private static String getCertAliasFromKeyAlias(String keyAlias) { 146 String certAlias = null; 147 if (keyAlias!=null) { 148 int idx = keyAlias.indexOf(':'); 149 if (idx == -1) { 150 certAlias = keyAlias; 151 } else { 152 idx++; 153 if (idx < keyAlias.length()-1 ) { 154 certAlias = keyAlias.substring(idx); 155 } 156 } 157 } 158 if (certAlias!=null) 159 certAlias = certAlias.trim(); 160 return certAlias; 161 } 162 163 } 164 | Popular Tags |