001: /**
002: * $RCSfile$
003: * $Revision: 9675 $
004: * $Date: 2007-12-25 23:58:10 -0800 (Tue, 25 Dec 2007) $
005: *
006: * Copyright (C) 2004-2005 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util;
011:
012: import javax.net.ssl.SSLSocketFactory;
013: import javax.net.ssl.SSLContext;
014: import javax.net.ssl.TrustManager;
015: import javax.net.ssl.X509TrustManager;
016: import javax.net.SocketFactory;
017: import java.security.NoSuchAlgorithmException;
018: import java.security.KeyManagementException;
019: import java.security.cert.CertificateException;
020: import java.security.cert.X509Certificate;
021: import java.security.cert.CertificateExpiredException;
022: import java.security.cert.CertificateNotYetValidException;
023: import java.net.Socket;
024: import java.net.InetAddress;
025: import java.io.IOException;
026:
027: /**
028: * SSLSocketFactory that accepts any certificate chain and also accepts expired
029: * certificates.
030: *
031: * @author Matt Tucker
032: */
033: public class SimpleSSLSocketFactory extends SSLSocketFactory {
034:
035: private SSLSocketFactory factory;
036:
037: public SimpleSSLSocketFactory() {
038:
039: try {
040: SSLContext sslcontent = SSLContext.getInstance("TLS");
041: sslcontent.init(
042: null, // KeyManager not required
043: new TrustManager[] { new DummyTrustManager() },
044: new java.security.SecureRandom());
045: factory = sslcontent.getSocketFactory();
046: } catch (NoSuchAlgorithmException e) {
047: Log.error(e);
048: } catch (KeyManagementException e) {
049: Log.error(e);
050: }
051: }
052:
053: public static SocketFactory getDefault() {
054: return new SimpleSSLSocketFactory();
055: }
056:
057: public Socket createSocket() throws IOException {
058: return factory.createSocket();
059: }
060:
061: public Socket createSocket(Socket socket, String s, int i,
062: boolean flag) throws IOException {
063: return factory.createSocket(socket, s, i, flag);
064: }
065:
066: public Socket createSocket(InetAddress inaddr, int i,
067: InetAddress inaddr2, int j) throws IOException {
068: return factory.createSocket(inaddr, i, inaddr2, j);
069: }
070:
071: public Socket createSocket(InetAddress inaddr, int i)
072: throws IOException {
073: return factory.createSocket(inaddr, i);
074: }
075:
076: public Socket createSocket(String s, int i, InetAddress inaddr,
077: int j) throws IOException {
078: return factory.createSocket(s, i, inaddr, j);
079: }
080:
081: public Socket createSocket(String s, int i) throws IOException {
082: return factory.createSocket(s, i);
083: }
084:
085: public String[] getDefaultCipherSuites() {
086: return factory.getSupportedCipherSuites();
087: }
088:
089: public String[] getSupportedCipherSuites() {
090: return factory.getSupportedCipherSuites();
091: }
092:
093: private static class DummyTrustManager implements X509TrustManager {
094:
095: public boolean isClientTrusted(X509Certificate[] cert) {
096: return true;
097: }
098:
099: public boolean isServerTrusted(X509Certificate[] cert) {
100: try {
101: cert[0].checkValidity();
102: return true;
103: } catch (CertificateExpiredException e) {
104: return false;
105: } catch (CertificateNotYetValidException e) {
106: return false;
107: }
108: }
109:
110: public void checkClientTrusted(
111: java.security.cert.X509Certificate[] x509Certificates,
112: String s) throws CertificateException {
113: }
114:
115: public void checkServerTrusted(
116: java.security.cert.X509Certificate[] x509Certificates,
117: String s) throws CertificateException {
118: }
119:
120: public X509Certificate[] getAcceptedIssuers() {
121: return new X509Certificate[0];
122: }
123: }
124: }
|