001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.net.jsse;
018:
019: import java.net.Socket;
020: import java.security.Principal;
021: import java.security.PrivateKey;
022: import java.security.cert.X509Certificate;
023: import javax.net.ssl.X509KeyManager;
024:
025: /**
026: * X509KeyManager which allows selection of a specific keypair and certificate
027: * chain (identified by their keystore alias name) to be used by the server to
028: * authenticate itself to SSL clients.
029: *
030: * @author Jan Luehe
031: */
032: public final class JSSEKeyManager implements X509KeyManager {
033:
034: private X509KeyManager delegate;
035: private String serverKeyAlias;
036:
037: /**
038: * Constructor.
039: *
040: * @param mgr The X509KeyManager used as a delegate
041: * @param serverKeyAlias The alias name of the server's keypair and
042: * supporting certificate chain
043: */
044: public JSSEKeyManager(X509KeyManager mgr, String serverKeyAlias) {
045: this .delegate = mgr;
046: this .serverKeyAlias = serverKeyAlias;
047: }
048:
049: /**
050: * Choose an alias to authenticate the client side of a secure socket,
051: * given the public key type and the list of certificate issuer authorities
052: * recognized by the peer (if any).
053: *
054: * @param keyType The key algorithm type name(s), ordered with the
055: * most-preferred key type first
056: * @param issuers The list of acceptable CA issuer subject names, or null
057: * if it does not matter which issuers are used
058: * @param socket The socket to be used for this connection. This parameter
059: * can be null, in which case this method will return the most generic
060: * alias to use
061: *
062: * @return The alias name for the desired key, or null if there are no
063: * matches
064: */
065: public String chooseClientAlias(String[] keyType,
066: Principal[] issuers, Socket socket) {
067: return delegate.chooseClientAlias(keyType, issuers, socket);
068: }
069:
070: /**
071: * Returns this key manager's server key alias that was provided in the
072: * constructor.
073: *
074: * @param keyType The key algorithm type name (ignored)
075: * @param issuers The list of acceptable CA issuer subject names, or null
076: * if it does not matter which issuers are used (ignored)
077: * @param socket The socket to be used for this connection. This parameter
078: * can be null, in which case this method will return the most generic
079: * alias to use (ignored)
080: *
081: * @return Alias name for the desired key
082: */
083: public String chooseServerAlias(String keyType,
084: Principal[] issuers, Socket socket) {
085: return serverKeyAlias;
086: }
087:
088: /**
089: * Returns the certificate chain associated with the given alias.
090: *
091: * @param alias The alias name
092: *
093: * @return Certificate chain (ordered with the user's certificate first
094: * and the root certificate authority last), or null if the alias can't be
095: * found
096: */
097: public X509Certificate[] getCertificateChain(String alias) {
098: return delegate.getCertificateChain(alias);
099: }
100:
101: /**
102: * Get the matching aliases for authenticating the client side of a secure
103: * socket, given the public key type and the list of certificate issuer
104: * authorities recognized by the peer (if any).
105: *
106: * @param keyType The key algorithm type name
107: * @param issuers The list of acceptable CA issuer subject names, or null
108: * if it does not matter which issuers are used
109: *
110: * @return Array of the matching alias names, or null if there were no
111: * matches
112: */
113: public String[] getClientAliases(String keyType, Principal[] issuers) {
114: return delegate.getClientAliases(keyType, issuers);
115: }
116:
117: /**
118: * Get the matching aliases for authenticating the server side of a secure
119: * socket, given the public key type and the list of certificate issuer
120: * authorities recognized by the peer (if any).
121: *
122: * @param keyType The key algorithm type name
123: * @param issuers The list of acceptable CA issuer subject names, or null
124: * if it does not matter which issuers are used
125: *
126: * @return Array of the matching alias names, or null if there were no
127: * matches
128: */
129: public String[] getServerAliases(String keyType, Principal[] issuers) {
130: return delegate.getServerAliases(keyType, issuers);
131: }
132:
133: /**
134: * Returns the key associated with the given alias.
135: *
136: * @param alias The alias name
137: *
138: * @return The requested key, or null if the alias can't be found
139: */
140: public PrivateKey getPrivateKey(String alias) {
141: return delegate.getPrivateKey(alias);
142: }
143: }
|