001: package com.sun.portal.proxylet.crypt.jsse;
002:
003: import com.sun.portal.proxylet.crypt.jsse.NetletJSSEAuthContext;
004:
005: import javax.net.ssl.X509KeyManager;
006: import javax.net.ssl.KeyManagerFactory;
007: import javax.net.ssl.KeyManager;
008: import java.security.cert.X509Certificate;
009: import java.security.*;
010: import java.net.Socket;
011: import java.io.FileInputStream;
012: import java.util.Vector;
013:
014: /**
015: * com.sun.portal.proxylet.crypt.jsse.NetletKeyManager is an implementation of X509KeyManager which
016: * wraps an existing X509KeyManager instance, and prompts for
017: * keystore password and path.
018: */
019:
020: public class NetletKeyManager implements X509KeyManager {
021: NetletJSSEAuthContext authContext = null;
022: NetletDataRepository repos = null;
023: KeyStore ks = null;
024: String alias = null;
025:
026: /**
027: * @param authContext This will be used for callbacks
028: */
029: public NetletKeyManager(NetletJSSEAuthContext authContext) {
030:
031: this .authContext = authContext;
032: repos = NetletDataRepository.getInstance();
033:
034: }
035:
036: /**
037: * chooseClientAlias selects an alias to authenticate the client side
038: * of a SSL connection. This implementation prompts the user for the
039: * keystore path and password.
040: * @param keyType the key algorithm type name(s)
041: * @param issuers the list of acceptable CA issuer subject names
042: * @param socket the socket to be used for this connection.
043: */
044: public String chooseClientAlias(String[] keyType,
045: Principal[] issuers, Socket socket)
046:
047: {
048:
049: // Call the callback functions to set password and keystore path
050: // if not already set.
051: setContext();
052: try {
053: // Create a keystore of the specified type
054: if (ks == null) {
055:
056: ks = KeyStore.getInstance(repos.getKeyStoreType()
057: .toString(), repos.getKeyStoreType()
058: .getProvider());
059:
060: if (repos.getKeyStoreStream() != null)
061: ks.load(repos.getKeyStoreStream(), repos
062: .getKeyStorePassphrase());
063: else
064: ks
065: .load(new FileInputStream(repos
066: .getKeyStorePath()), repos
067: .getKeyStorePassphrase());
068: }
069:
070: alias = (String) ks.aliases().nextElement();
071: // return the alias
072: return alias;
073: } catch (KeyStoreException e) {
074: throw new IllegalArgumentException(e.getMessage());
075: } catch (NoSuchProviderException ne) {
076: throw new IllegalArgumentException(ne.getMessage());
077: } catch (Exception na) {
078: throw new IllegalArgumentException(na.getMessage());
079: }
080:
081: }
082:
083: public String chooseServerAlias(String keyType,
084: Principal[] issuers, Socket socket) {
085: return null;
086: }
087:
088: public X509Certificate[] getCertificateChain(String a) {
089: alias = chooseClientAlias(null, null, null);
090: try {
091: java.security.cert.Certificate[] certs = ks
092: .getCertificateChain(alias);
093: Vector temp = new Vector(certs.length);
094: for (int i = 0; i < certs.length; i++)
095: temp.add(certs[i]);
096:
097: return (X509Certificate[]) temp
098: .toArray(new X509Certificate[0]);
099: } catch (KeyStoreException e) {
100: return null;
101: }
102:
103: }
104:
105: public String[] getClientAliases(String keyType, Principal[] issuers) {
106:
107: String temp[] = new String[1];
108: if (alias == null) {
109: alias = chooseClientAlias(null, null, null);
110: temp[0] = alias;
111: }
112: return temp;
113: }
114:
115: public PrivateKey getPrivateKey(String a) {
116: alias = chooseClientAlias(null, null, null);
117: try {
118: return (PrivateKey) ks.getKey(alias, repos
119: .getKeyStorePassphrase());
120:
121: } catch (Exception e) {
122: return null;
123: }
124:
125: }
126:
127: public String[] getServerAliases(String keyType, Principal[] issuers) {
128: return null;
129: }
130:
131: private void setContext() {
132: if (repos.getKeyStorePassphrase() == null) {
133: repos.setKeyStoreStream(authContext.OnsetKeyStoreStream());
134: repos.setKeyStorePath(authContext.OnsetKeyStorePath());
135: repos.setKeyStorePassphrase(authContext
136: .OnsetKeyStorePassword());
137: repos.setKeyStoreType(authContext.OnsetKeyStoreType());
138: }
139: }
140: }
|