1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.innergrid.api;
19
20 import com.gridsystems.innergrid.kernel.KernelException;
21
22 import java.net.Socket;
23 import java.net.SocketException;
24 import java.security.Provider;
25 import java.security.Security;
26 import java.util.Hashtable;
27
28 import javax.net.ssl.SSLSocketFactory;
29
30 import org.apache.axis.AxisProperties;
31 import org.apache.axis.components.net.BooleanHolder;
32 import org.apache.axis.components.net.SocketFactoryFactory;
33 import org.apache.axis.components.net.SunFakeTrustSocketFactory;
34
35
36
37
38
39
40
41 public class InnergridJSSESocketFactory extends SunFakeTrustSocketFactory {
42
43
44
45
46 static {
47 SocketFactoryFactory.getFactory("http", new Hashtable());
48 AxisProperties.setProperty("axis.socketSecureFactory",
49 InnergridJSSESocketFactory.class.getName(),
50 true);
51
52
53 addSecurityProvider("sun.security.provider.Sun");
54 addSecurityProvider("com.sun.net.ssl.internal.ssl.Provider");
55 addSecurityProvider("org.bouncycastle.jce.provider.BouncyCastleProvider");
56 }
57
58
59
60
61 private static final AcceptAllCertificates ACCEPT_ALL = new AcceptAllCertificates();
62
63
64
65
66
67 private static Hashtable<String, SSLConnectionInfo> connections
68 = new Hashtable<String, SSLConnectionInfo>();
69
70
71
72
73
74
75
76
77 public static void registrySSLConnectionInfo(String host, int port,
78 SSLConnectionInfo sslinfo) {
79 if ((host == null) || (port < 1) || (sslinfo == null)) {
80 return;
81 }
82 connections.put(host + "_" + port, sslinfo);
83 }
84
85
86
87
88
89
90 public InnergridJSSESocketFactory(Hashtable attrib) {
91 super(attrib);
92 }
93
94
95
96
97 @Override
98 protected void initFactory() {
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 @Override
113 public synchronized Socket create(String host, int port, StringBuffer otherHeaders,
114 BooleanHolder useFullURL) throws Exception {
115
116 sslFactory = getSSLSocketFactory(host, port);
117 try {
118 Socket s = super.create(host, port, otherHeaders, useFullURL);
119 s.setSoLinger(false, 0);
120 return s;
121 } catch (SocketException se) {
122 throw se;
123 } catch (Exception e) {
124
125 throw new CKernelException(e, "CLT031");
126 }
127 }
128
129
130
131
132
133
134 private static void addSecurityProvider(String className) {
135 try {
136 Class<?> c = Class.forName(className);
137 Provider provider = (Provider)c.newInstance();
138 Security.addProvider(provider);
139 } catch (Throwable t) { }
140 }
141
142
143
144
145
146
147
148
149
150 private SSLSocketFactory getSSLSocketFactory(String host, int port)
151 throws KernelException {
152
153 String key = host + "_" + port;
154 SSLConnectionInfo sslinfo = (SSLConnectionInfo) connections.get(key);
155
156 if (sslinfo == null) {
157 sslinfo = ACCEPT_ALL;
158 }
159 return sslinfo.getSSLSocketFactory();
160 }
161
162 }