01: /*
02: * This file is part of DrFTPD, Distributed FTP Daemon.
03: *
04: * DrFTPD is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * DrFTPD is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with DrFTPD; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.drftpd;
19:
20: import java.io.FileInputStream;
21: import java.io.IOException;
22:
23: import java.security.GeneralSecurityException;
24: import java.security.KeyStore;
25:
26: import javax.net.ssl.KeyManagerFactory;
27: import javax.net.ssl.SSLContext;
28: import javax.net.ssl.TrustManager;
29: import javax.net.ssl.X509TrustManager;
30:
31: /**
32: * @author mog
33: * @version $Id: SSLGetContext.java 1230 2005-08-28 20:05:49Z zubov $
34: */
35: public class SSLGetContext {
36: static SSLContext ctx = null;
37:
38: public static SSLContext getSSLContext()
39: throws GeneralSecurityException, IOException {
40: // Create a trust manager that does not validate certificate chains
41: TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
42: public java.security.cert.X509Certificate[] getAcceptedIssuers() {
43: return null;
44: }
45:
46: public void checkClientTrusted(
47: java.security.cert.X509Certificate[] certs,
48: String authType) {
49: }
50:
51: public void checkServerTrusted(
52: java.security.cert.X509Certificate[] certs,
53: String authType) {
54: }
55: } };
56: if (ctx != null)
57: return ctx; // reuse previous SSLContext
58:
59: ctx = SSLContext.getInstance("SSLv3");
60:
61: KeyManagerFactory kmf = KeyManagerFactory
62: .getInstance("SunX509");
63:
64: KeyStore ks = KeyStore.getInstance("JKS");
65: FileInputStream fis = null;
66: try {
67: fis = new FileInputStream("drftpd.key");
68: ks.load(fis, "drftpd".toCharArray());
69: } finally {
70: if (fis != null) {
71: fis.close();
72: }
73: }
74:
75: kmf.init(ks, "drftpd".toCharArray());
76:
77: ctx.init(kmf.getKeyManagers(), trustAllCerts, null);
78:
79: return ctx;
80: }
81: }
|