1 23 package com.sun.enterprise.security.ssl; 24 25 import java.net.Socket ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.security.Principal ; 30 import java.security.PrivateKey ; 31 import java.security.cert.X509Certificate ; 32 import javax.net.ssl.X509KeyManager; 33 34 38 public class UnifiedX509KeyManager implements X509KeyManager { 39 private X509KeyManager[] mgrs = null; 40 private String [] tokenNames = null; 41 42 46 public UnifiedX509KeyManager(X509KeyManager[] mgrs, String [] tokenNames) { 47 if (mgrs == null || tokenNames == null) { 48 throw new IllegalArgumentException ("Null array of X509KeyManagers or tokenNames"); 49 } 50 if (mgrs.length != tokenNames.length) { 51 throw new IllegalArgumentException ("Size of X509KeyManagers array and tokenNames array do not match."); 52 } 53 this.mgrs = mgrs; 54 this.tokenNames = tokenNames; 55 } 56 57 public String chooseClientAlias(String [] keyType, Principal [] issuers, 59 Socket socket) { 60 String alias = null; 61 for (int i = 0; i < mgrs.length; i++) { 62 alias = mgrs[i].chooseClientAlias(keyType, issuers, socket); 63 if (alias != null) { 64 break; 65 } 66 } 67 return alias; 68 } 69 70 public String chooseServerAlias(String keyType, Principal [] issuers, 71 Socket socket) { 72 String alias = null; 73 for (int i = 0; i < mgrs.length; i++) { 74 alias = mgrs[i].chooseServerAlias(keyType, issuers, socket); 75 if (alias != null) { 76 break; 77 } 78 } 79 return alias; 80 } 81 82 public X509Certificate [] getCertificateChain(String alias) { 83 X509Certificate [] chain = null; 84 for (int i = 0; i < mgrs.length; i++) { 85 chain = mgrs[i].getCertificateChain(alias); 86 if (chain != null) { 87 break; 88 } 89 } 90 return chain; 91 } 92 93 public String [] getClientAliases(String keyType, Principal [] issuers) { 94 ArrayList clientAliases = new ArrayList (); 95 for (int i = 0; i < mgrs.length; i++) { 96 String [] clAliases = mgrs[i].getClientAliases(keyType, issuers); 97 if (clAliases != null && clAliases.length > 0) { 98 for (int j = 0; j < clAliases.length; j++) { 99 clientAliases.add(clAliases[j]); 100 } 101 } 102 } 103 104 return (clientAliases.size() == 0) ? null : 105 (String [])clientAliases.toArray(new String [clientAliases.size()]); 106 } 107 108 public PrivateKey getPrivateKey(String alias) { 109 PrivateKey privKey = null; 110 for (int i = 0; i < mgrs.length; i++) { 111 privKey = mgrs[i].getPrivateKey(alias); 112 if (privKey != null) { 113 break; 114 } 115 } 116 return privKey; 117 } 118 119 public String [] getServerAliases(String keyType, Principal [] issuers) { 120 ArrayList serverAliases = new ArrayList (); 121 for (int i = 0; i < mgrs.length; i++) { 122 String [] serAliases = mgrs[i].getClientAliases(keyType, issuers); 123 if (serAliases != null && serAliases.length > 0) { 124 for (int j = 0; j < serAliases.length; j++) { 125 serverAliases.add(serAliases[j]); 126 } 127 } 128 } 129 130 return (serverAliases.size() == 0) ? null : 131 (String [])serverAliases.toArray(new String [serverAliases.size()]); 132 } 133 134 136 X509KeyManager[] getX509KeyManagers() { 137 X509KeyManager[] kmgrs = new X509KeyManager[mgrs.length]; 138 System.arraycopy(mgrs, 0, kmgrs, 0, mgrs.length); 139 return kmgrs; 140 } 141 142 String [] getTokenNames() { 143 String [] tokens = new String [tokenNames.length]; 144 System.arraycopy(tokenNames, 0, tokens, 0, tokenNames.length); 145 return tokens; 146 } 147 } 148 | Popular Tags |