Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsClient/MyKeyManager.java
41161 views
/*1* Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.net.ssl.X509KeyManager;24import java.io.*;25import java.security.*;26import java.security.cert.*;27import java.security.cert.Certificate;28import java.util.*;29import java.net.Socket;30import javax.net.ssl.X509KeyManager;31import java.util.Set;3233final class MyKeyManager implements X509KeyManager {34private HashMap keyMap = new HashMap();35private HashMap certChainMap = new HashMap();3637MyKeyManager(KeyStore ks, char[] password)38throws KeyStoreException, NoSuchAlgorithmException,39UnrecoverableKeyException40{41if (ks == null) {42return;43}4445Enumeration aliases = ks.aliases();46while (aliases.hasMoreElements()) {47String alias = (String)aliases.nextElement();48if (ks.isKeyEntry(alias)) {49Certificate[] certs;50certs = ks.getCertificateChain(alias);51if (certs != null && certs.length > 0 &&52certs[0] instanceof X509Certificate) {53if (!(certs instanceof X509Certificate[])) {54Certificate[] tmp = new X509Certificate[certs.length];55System.arraycopy(certs, 0, tmp, 0, certs.length);56certs = tmp;57}58Key key = ks.getKey(alias, password);59certChainMap.put(alias, certs);60keyMap.put(alias, key);61}62}63}64}6566/*67* Choose an alias to authenticate the client side of a secure68* socket given the public key type and the list of69* certificate issuer authorities recognized by the peer (if any).70*/71public String chooseClientAlias(String[] keyTypes, Principal[] issuers,72Socket socket) {73return "client";74}7576/*77* Get the matching aliases for authenticating the client side of a secure78* socket given the public key type and the list of79* certificate issuer authorities recognized by the peer (if any).80*/81public String[] getClientAliases(String keyType, Principal[] issuers) {82String[] s = new String[1];83s[0] = "client";84return s;85}8687private HashMap serverAliasCache = new HashMap();8889/*90* Choose an alias to authenticate the server side of a secure91* socket given the public key type and the list of92* certificate issuer authorities recognized by the peer (if any).93*/94public synchronized String chooseServerAlias(String keyType,95Principal[] issuers, Socket socket) {96return "server";97}9899/*100* Get the matching aliases for authenticating the server side of a secure101* socket given the public key type and the list of102* certificate issuer authorities recognized by the peer (if any).103*/104public String[] getServerAliases(String keyType, Principal[] issuers) {105String[] s = new String[1];106s[0] = "server";107return s;108}109110/**111* Returns the certificate chain associated with the given alias.112*113* @param alias the alias name114*115* @return the certificate chain (ordered with the user's certificate first116* and the root certificate authority last)117*118* @exception KeyStoreException if the alias is invalid119*/120public X509Certificate[] getCertificateChain(String alias) {121Object chain;122123chain = certChainMap.get(alias);124if (!(chain instanceof X509Certificate[]))125return null;126return (X509Certificate[]) chain;127}128129/*130* Returns the key associated with the given alias, using the given131* password to recover it.132*133* @param alias the alias name134*135* @return the requested key136* @exception KeyStoreException if the alias is invalid137*/138public PrivateKey getPrivateKey(String alias) {139Object key;140141key = keyMap.get(alias);142if (!(key instanceof PrivateKey))143return null;144return (PrivateKey)key;145}146}147148149