01: /*
02: * JsseSSLSupport.java
03: *
04: * Copyright (C) 2000 Jacob Smullyan
05: *
06: * This file is a supplement to the HTTPClient package by Ronald Tschalär,
07: * Copyright (C) 1996-1999 Ronald Tschalär, and the same license holds. It
08: * is based on a jsse patch by RT.
09: *
10: * This library is free software; you can redistribute it and/or
11: * modify it under the terms of the GNU Lesser General Public
12: * License as published by the Free Software Foundation; either
13: * version 2 of the License, or (at your option) any later version.
14: *
15: * This library is distributed in the hope that it will be useful,
16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
17: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18: * Lesser General Public License for more details.
19: *
20: * You should have received a copy of the GNU Lesser General Public
21: * License along with this library; if not, write to the Free
22: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23: * MA 02111-1307, USA
24: *
25: * For questions, suggestions, bug-reports, enhancement-requests etc.
26: * I may be contacted at:
27: *
28: * ronald@innovation.ch
29: *
30: */
31: package HTTPClient.jsse;
32:
33: import HTTPClient.SSLSupport;
34: import java.io.IOException;
35: import java.net.Socket;
36: import java.lang.reflect.Method;
37: import java.security.Provider;
38: import java.security.Security;
39: import java.security.cert.X509Certificate;
40: import javax.net.ssl.SSLException;
41: import javax.net.ssl.SSLSocket;
42: import javax.net.ssl.SSLSocketFactory;
43:
44: /**
45: * A wrapper which helps to the HTTPClient package for
46: * SSL support with Sun's JSSE while making it possible
47: * use the patched client, sans SSL capability, without
48: * the JSSE jars.
49: */
50: public final class JsseSSLSupport extends SSLSupport {
51: public static final String SSL_PROVIDER_CLASS = "com.sun.net.ssl.internal.ssl.Provider";
52:
53: static {
54: try {
55: Provider provider = (Provider) Class.forName(
56: SSL_PROVIDER_CLASS).newInstance();
57: Security.addProvider(provider);
58: } catch (Exception e) {
59: e.printStackTrace();
60: }
61: }
62:
63: public Socket createSocket(Socket sock, String host, int port)
64: throws IOException {
65: Socket socket = ((SSLSocketFactory) SSLSocketFactory
66: .getDefault()).createSocket(sock, host, port, true);
67: //enable all supported cipher suites
68: configureCipherSuites(socket);
69: checkCertificate(socket, host);
70: return socket;
71: }
72:
73: private void configureCipherSuites(Socket s) {
74: if (s instanceof SSLSocket) {
75: SSLSocket sock = (SSLSocket) s;
76: String[] supported = sock.getSupportedCipherSuites();
77: sock.setEnabledCipherSuites(supported);
78: }
79: }
80:
81: private void checkCertificate(Socket s, String host) {
82: try {
83: if (!((SSLSocket) s).getSession().getPeerHost()
84: .equals(host)) {
85: System.out.println("host does not match");
86: }
87:
88: } catch (Exception e) {
89: e.printStackTrace();
90: }
91: }
92: }
|