1 /* X509KeyManagerFactory.java -- X.509 key manager factory.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.net.ssl.provider;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.net.Socket;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.LinkedList;
48 import java.util.Enumeration;
50 import java.security.InvalidAlgorithmParameterException;
51 import java.security.KeyStore;
52 import java.security.KeyStoreException;
53 import java.security.NoSuchAlgorithmException;
54 import java.security.Principal;
55 import java.security.PrivateKey;
56 import java.security.PublicKey;
57 import java.security.UnrecoverableKeyException;
58 import java.security.cert.Certificate;
59 import java.security.cert.CertificateException;
60 import java.security.cert.X509Certificate;
61 import java.security.interfaces.DSAPrivateKey;
62 import java.security.interfaces.DSAPublicKey;
63 import java.security.interfaces.RSAPrivateKey;
64 import java.security.interfaces.RSAPublicKey;
66 import java.util.Collections;
67 import java.util.HashMap;
68 import java.util.Iterator;
70 import java.util.List;
72 import javax.crypto.interfaces.DHPrivateKey;
73 import javax.crypto.interfaces.DHPublicKey;
75 import javax.net.ssl.KeyManager;
76 import javax.net.ssl.KeyManagerFactorySpi;
77 import javax.net.ssl.ManagerFactoryParameters;
78 import javax.net.ssl.SSLEngine;
79 import javax.net.ssl.X509ExtendedKeyManager;
80 import javax.net.ssl.X509KeyManager;
82 import gnu.javax.net.ssl.NullManagerParameters;
83 import gnu.javax.net.ssl.PrivateCredentials;
86 * This class implements a {@link javax.net.ssl.KeyManagerFactory} engine
87 * for the ``JessieX509'' algorithm.
89 public class X509KeyManagerFactory extends KeyManagerFactorySpi
93 // -------------------------------------------------------------------------
95 private Manager current;
98 // -------------------------------------------------------------------------
100 public X509KeyManagerFactory()
106 // -------------------------------------------------------------------------
108 protected KeyManager[] engineGetKeyManagers()
112 throw new IllegalStateException();
114 return new KeyManager[] { current };
117 protected void engineInit(ManagerFactoryParameters params)
118 throws InvalidAlgorithmParameterException
120 if (params instanceof NullManagerParameters)
122 current = new Manager(Collections.EMPTY_MAP, Collections.EMPTY_MAP);
124 else if (params instanceof PrivateCredentials)
126 List<X509Certificate[]> chains
127 = ((PrivateCredentials) params).getCertChains();
128 List<PrivateKey> keys
129 = ((PrivateCredentials) params).getPrivateKeys();
131 HashMap<String, X509Certificate[]> certMap
132 = new HashMap<String, X509Certificate[]>();
133 HashMap<String, PrivateKey> keyMap
134 = new HashMap<String, PrivateKey>();
135 Iterator<X509Certificate[]> c = chains.iterator();
136 Iterator<PrivateKey> k = keys.iterator();
137 while (c.hasNext() && k.hasNext())
139 certMap.put(String.valueOf(i), c.next());
140 keyMap.put(String.valueOf(i), k.next());
143 current = new Manager(keyMap, certMap);
147 throw new InvalidAlgorithmParameterException();
151 protected void engineInit(KeyStore store, char[] passwd)
152 throws KeyStoreException, NoSuchAlgorithmException,
153 UnrecoverableKeyException
157 String s = Util.getProperty("javax.net.ssl.keyStoreType");
159 s = KeyStore.getDefaultType();
160 store = KeyStore.getInstance(s);
161 s = Util.getProperty("javax.net.ssl.keyStore");
164 String p = Util.getProperty("javax.net.ssl.keyStorePassword");
167 store.load(new FileInputStream(s), p != null ? p.toCharArray() : null);
169 catch (IOException ioe)
171 throw new KeyStoreException(ioe.toString());
173 catch (CertificateException ce)
175 throw new KeyStoreException(ce.toString());
179 HashMap<String, PrivateKey> p = new HashMap<String, PrivateKey>();
180 HashMap<String, X509Certificate[]> c
181 = new HashMap<String, X509Certificate[]>();
182 Enumeration aliases = store.aliases();
183 UnrecoverableKeyException exception = null;
184 while (aliases.hasMoreElements())
186 String alias = (String) aliases.nextElement();
187 if (!store.isKeyEntry(alias))
191 X509Certificate[] chain = null;
192 Certificate[] chain2 = store.getCertificateChain (alias);
193 if (chain2 != null && chain2.length > 0 &&
194 (chain2[0] instanceof X509Certificate))
196 chain = toX509Chain(chain2);
202 PrivateKey key = null;
205 key = (PrivateKey) store.getKey(alias, passwd);
207 catch (UnrecoverableKeyException uke)
219 if (p.isEmpty () && c.isEmpty ())
221 if (exception != null)
225 throw new KeyStoreException ("no private credentials found");
227 current = this.new Manager(p, c);
230 private static X509Certificate[] toX509Chain(Certificate[] chain)
232 if (chain instanceof X509Certificate[])
234 return (X509Certificate[]) chain;
236 X509Certificate[] _chain = new X509Certificate[chain.length];
237 for (int i = 0; i < chain.length; i++)
238 _chain[i] = (X509Certificate) chain[i];
243 // -------------------------------------------------------------------------
245 private class Manager extends X509ExtendedKeyManager
248 // -----------------------------------------------------------------------
250 private final Map<String, PrivateKey> privateKeys;
251 private final Map<String, X509Certificate[]> certChains;
254 // -----------------------------------------------------------------------
256 Manager(Map<String, PrivateKey> privateKeys,
257 Map<String, X509Certificate[]> certChains)
259 this.privateKeys = privateKeys;
260 this.certChains = certChains;
264 // -----------------------------------------------------------------------
266 public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
269 for (int i = 0; i < keyTypes.length; i++)
271 String[] s = getClientAliases(keyTypes[i], issuers);
278 public @Override String chooseEngineClientAlias(String[] keyTypes,
282 for (String type : keyTypes)
284 String[] s = getClientAliases(type, issuers);
291 public String[] getClientAliases(String keyType, Principal[] issuers)
293 return getAliases(keyType, issuers);
296 public String chooseServerAlias(String keyType, Principal[] issuers,
299 String[] s = getServerAliases(keyType, issuers);
305 public @Override String chooseEngineServerAlias(String keyType,
309 String[] s = getServerAliases(keyType, issuers);
315 public String[] getServerAliases(String keyType, Principal[] issuers)
317 return getAliases(keyType, issuers);
320 private String[] getAliases(String keyType, Principal[] issuers)
322 LinkedList<String> l = new LinkedList<String>();
323 for (Iterator i = privateKeys.keySet().iterator(); i.hasNext(); )
325 String alias = (String) i.next();
326 X509Certificate[] chain = getCertificateChain(alias);
327 if (chain.length == 0)
329 PrivateKey privKey = getPrivateKey(alias);
332 PublicKey pubKey = chain[0].getPublicKey();
333 if (keyType.equalsIgnoreCase("RSA")
334 || keyType.equalsIgnoreCase("DHE_RSA")
335 || keyType.equalsIgnoreCase("SRP_RSA")
336 || keyType.equalsIgnoreCase("rsa_sign")
337 || keyType.equalsIgnoreCase("RSA_PSK"))
339 if (!(privKey instanceof RSAPrivateKey) ||
340 !(pubKey instanceof RSAPublicKey))
343 else if (keyType.equalsIgnoreCase("DHE_DSS")
344 || keyType.equalsIgnoreCase("dss_sign")
345 || keyType.equalsIgnoreCase("SRP_DSS")
346 || keyType.equalsIgnoreCase("DSA"))
348 if (!(privKey instanceof DSAPrivateKey) ||
349 !(pubKey instanceof DSAPublicKey))
352 else if (keyType.equalsIgnoreCase("DH_RSA")
353 || keyType.equalsIgnoreCase("rsa_fixed_dh"))
355 if (!(privKey instanceof DHPrivateKey) ||
356 !(pubKey instanceof DHPublicKey))
358 if (!chain[0].getSigAlgName().equalsIgnoreCase("RSA"))
361 else if (keyType.equalsIgnoreCase("DH_DSS")
362 || keyType.equalsIgnoreCase("dss_fixed_dh"))
364 if (!(privKey instanceof DHPrivateKey) ||
365 !(pubKey instanceof DHPublicKey))
367 if (!chain[0].getSigAlgName().equalsIgnoreCase("DSA"))
370 else // Unknown key type; ignore it.
372 if (issuers == null || issuers.length == 0)
377 for (Principal issuer : issuers)
379 if (chain[0].getIssuerDN().equals(issuer))
386 return l.toArray(new String[l.size()]);
389 public X509Certificate[] getCertificateChain(String alias)
391 X509Certificate[] c = (X509Certificate[]) certChains.get(alias);
392 return c != null ? (X509Certificate[]) c.clone() : null;
395 public PrivateKey getPrivateKey(String alias)
397 return (PrivateKey) privateKeys.get(alias);