KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > CustomKeyManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server;
21
22 import java.net.Socket JavaDoc;
23 import java.security.Principal JavaDoc;
24 import java.security.PrivateKey JavaDoc;
25 import java.security.cert.Certificate JavaDoc;
26 import java.security.cert.X509Certificate JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.net.ssl.X509KeyManager;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.sslexplorer.boot.ContextHolder;
36 import com.sslexplorer.boot.ContextKey;
37 import com.sslexplorer.boot.KeyStoreManager;
38 import com.sslexplorer.boot.PropertyClass;
39
40
41 /**
42  * Implementation of an {@link javax.net.ssl.X509KeyManager} that uses
43  * the SSL-Explorer keystore and the <b>Active Certifice Name</b>
44  * configured in the property database to determine the alias to load as the
45  * SSL Certificate.
46  *
47  * @author Brett Smith <brett@3sp.com>
48  */

49 public class CustomKeyManager implements X509KeyManager {
50
51     final static Log log = LogFactory.getLog(CustomKeyManager.class);
52     private String JavaDoc keyPassword;
53     private PropertyClass contextConfig;
54     /**
55      * Constructor
56      *
57      * @param keyPassword key password
58      */

59     public CustomKeyManager(String JavaDoc keyPassword) {
60         this.keyPassword = keyPassword;
61         contextConfig = ContextHolder.getContext().getConfig();
62     }
63
64     /* (non-Javadoc)
65      * @see javax.net.ssl.X509KeyManager#chooseClientAlias(java.lang.String[], java.security.Principal[], java.net.Socket)
66      */

67     public String JavaDoc chooseClientAlias(String JavaDoc[] arg0, Principal JavaDoc[] arg1, Socket JavaDoc socket) {
68         return null;
69     }
70
71     /* (non-Javadoc)
72      * @see javax.net.ssl.X509KeyManager#chooseServerAlias(java.lang.String, java.security.Principal[], java.net.Socket)
73      */

74     public String JavaDoc chooseServerAlias(String JavaDoc keyType, Principal JavaDoc[] issuers, Socket JavaDoc socket) {
75         String JavaDoc alias = ContextHolder.getContext().getConfig().retrieveProperty(new ContextKey("webServer.alias"));
76         return alias;
77     }
78
79     /* (non-Javadoc)
80      * @see javax.net.ssl.X509KeyManager#getCertificateChain(java.lang.String)
81      */

82     public X509Certificate JavaDoc[] getCertificateChain(String JavaDoc certname) {
83         try {
84             Certificate JavaDoc[] f = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getCertificateChain(certname);
85             List JavaDoc l = new ArrayList JavaDoc();
86             for(int i = 0 ; i < f.length ; i++) {
87                 if(f[i] instanceof X509Certificate JavaDoc) {
88                     l.add(f[i]);
89                 }
90             }
91             return (X509Certificate JavaDoc[])l.toArray(new X509Certificate JavaDoc[l.size()]);
92         } catch (Exception JavaDoc e) {
93             Main.log.error(e);
94         }
95         return null;
96     }
97
98     /* (non-Javadoc)
99      * @see javax.net.ssl.X509KeyManager#getClientAliases(java.lang.String, java.security.Principal[])
100      */

101     public String JavaDoc[] getClientAliases(String JavaDoc keyType, Principal JavaDoc[] issuers) {
102         String JavaDoc str[] = { "" };
103         return str;
104     }
105
106     /* (non-Javadoc)
107      * @see javax.net.ssl.X509KeyManager#getPrivateKey(java.lang.String)
108      */

109     public PrivateKey JavaDoc getPrivateKey(String JavaDoc alias) {
110         try {
111             return (PrivateKey JavaDoc) KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getPrivateKey(contextConfig.retrieveProperty(new ContextKey("webServer.alias")), contextConfig.retrieveProperty(new ContextKey("webServer.keystore.sslCertificate.password")).toCharArray());
112         } catch (Exception JavaDoc e) {
113             Main.log.error(e);
114         }
115         return null;
116     }
117
118     /* (non-Javadoc)
119      * @see javax.net.ssl.X509KeyManager#getServerAliases(java.lang.String, java.security.Principal[])
120      */

121     public String JavaDoc[] getServerAliases(String JavaDoc keyType, Principal JavaDoc[] issuers) {
122         String JavaDoc str[] = { contextConfig.retrieveProperty(new ContextKey("webServer.alias")) };
123         return str;
124     }
125
126 }
Popular Tags