| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.http.transport; |
| 34 | | | |
| 35 | | | import java.io.FileInputStream; |
| 36 | | | import java.io.FileNotFoundException; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.net.InetAddress; |
| 39 | | | import java.net.Socket; |
| 40 | | | import java.net.SocketAddress; |
| 41 | | | import java.net.InetSocketAddress; |
| 42 | | | import java.net.UnknownHostException; |
| 43 | | | import java.security.GeneralSecurityException; |
| 44 | | | import java.security.KeyStore; |
| 45 | | | import java.security.Security; |
| 46 | | | import java.util.logging.Logger; |
| 47 | | | |
| 48 | | | import javax.net.ssl.KeyManager; |
| 49 | | | import javax.net.ssl.SSLContext; |
| 50 | | | import javax.net.ssl.SSLSocketFactory; |
| 51 | | | import javax.net.ssl.TrustManager; |
| 52 | | | import javax.net.ssl.TrustManagerFactory; |
| 53 | | | |
| 54 | | | import org.apache.commons.httpclient.ConnectTimeoutException; |
| 55 | | | import org.apache.commons.httpclient.params.HttpConnectionParams; |
| 56 | | | import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; |
| 57 | | | import org.jcoderz.commons.connector.InitializingSslFailedException; |
| 58 | | | import org.jcoderz.commons.util.Assert; |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | (1) | public final class SslSocketFactory |
| 66 | | | implements SecureProtocolSocketFactory |
| 67 | | | { |
| 68 | | | |
| 69 | 0 | | private static final String CLASSNAME = SslSocketFactory.class.getName(); |
| 70 | | | |
| 71 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 72 | | | |
| 73 | | | |
| 74 | 0 | | private static final ThreadLocal SSL_SOCKET_FACTORIES = new ThreadLocal(); |
| 75 | | | |
| 76 | | | private final String mKeyStoreLocation; |
| 77 | | | private final String mKeyStorePassword; |
| 78 | | | private final String mTrustStoreLocation; |
| 79 | | | private final String mTrustStorePassword; |
| 80 | | | private final String mKeyAlias; |
| 81 | | | private final String mKeyPassword; |
| 82 | 0 | | private KeyStore mKeyStore = null; |
| 83 | 0 | | private KeyStore mTrustStore = null; |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | @param |
| 88 | | | |
| 89 | | | @param |
| 90 | | | |
| 91 | | | @param |
| 92 | | | |
| 93 | | | @param |
| 94 | | | |
| 95 | | | @param |
| 96 | | | |
| 97 | | | @param |
| 98 | | | |
| 99 | | | |
| 100 | | | public SslSocketFactory ( |
| 101 | | | String keyStoreLocation, |
| 102 | | | String keyStorePassword, |
| 103 | | | String trustStoreLocation, |
| 104 | | | String trustStorePassword, |
| 105 | | | String keyAlias, |
| 106 | | | String keyPassword) |
| 107 | 0 | | { |
| 108 | 0 | | Assert.notNull(keyStoreLocation, "keyStoreLocation"); |
| 109 | 0 | | Assert.notNull(keyStorePassword, "keyStorePassword"); |
| 110 | 0 | | Assert.notNull(trustStoreLocation, "trustStoreLocation"); |
| 111 | 0 | | Assert.notNull(trustStorePassword, "trustStorePassword"); |
| 112 | 0 | | Assert.notNull(keyAlias, "keyAlias"); |
| 113 | 0 | | Assert.notNull(keyPassword, "keyPassword"); |
| 114 | 0 | | mKeyStoreLocation = keyStoreLocation; |
| 115 | 0 | | mKeyStorePassword = keyStorePassword; |
| 116 | 0 | | mTrustStoreLocation = trustStoreLocation; |
| 117 | 0 | | mTrustStorePassword = trustStorePassword; |
| 118 | 0 | | mKeyAlias = keyAlias; |
| 119 | 0 | | mKeyPassword = keyPassword; |
| 120 | 0 | | } |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | @param |
| 125 | | | @param |
| 126 | | | @param |
| 127 | | | @param |
| 128 | | | |
| 129 | | | public SslSocketFactory ( |
| 130 | | | KeyStore keyStore, |
| 131 | | | KeyStore trustStore, |
| 132 | | | String keyAlias, |
| 133 | | | String keyPassword) |
| 134 | 0 | | { |
| 135 | 0 | | Assert.notNull(keyStore, "keyStore"); |
| 136 | 0 | | Assert.notNull(keyAlias, "keyAlias"); |
| 137 | 0 | | Assert.notNull(keyPassword, "keyPassword"); |
| 138 | 0 | | mKeyStore = keyStore; |
| 139 | 0 | | mTrustStore = trustStore; |
| 140 | 0 | | mKeyAlias = keyAlias; |
| 141 | 0 | | mKeyPassword = keyPassword; |
| 142 | 0 | | mKeyStoreLocation = null; |
| 143 | 0 | | mKeyStorePassword = null; |
| 144 | 0 | | mTrustStoreLocation = null; |
| 145 | 0 | | mTrustStorePassword = null; |
| 146 | 0 | | } |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | @return |
| 152 | | | @throws |
| 153 | | | |
| 154 | | | |
| 155 | | | private KeyManager[] getKeyManagers () |
| 156 | | | { |
| 157 | 0 | | final KeyManager manager |
| 158 | | | = new HttpsKeyManager(null, mKeyStore, mKeyAlias, mKeyPassword); |
| 159 | 0 | | final KeyManager[] managers = {manager}; |
| 160 | 0 | (2)(3) | return managers; |
| 161 | | | } |
| 162 | | | |
| 163 | | | |
| 164 | | | |
| 165 | | | |
| 166 | | | @return |
| 167 | | | @throws |
| 168 | | | |
| 169 | | | |
| 170 | | | private TrustManager[] getTrustManagers () |
| 171 | | | throws GeneralSecurityException |
| 172 | | | { |
| 173 | 0 | | final TrustManagerFactory tmf |
| 174 | | | = TrustManagerFactory.getInstance( |
| 175 | | | Security.getProperty("ssl.TrustManagerFactory.algorithm")); |
| 176 | 0 | | tmf.init(mTrustStore); |
| 177 | 0 | | return tmf.getTrustManagers(); |
| 178 | | | } |
| 179 | | | |
| 180 | | | private SSLSocketFactory getSslSocketFactory () |
| 181 | | | throws IOException, FileNotFoundException |
| 182 | | | { |
| 183 | | | SSLSocketFactory result; |
| 184 | 0 | | result = (SSLSocketFactory) SSL_SOCKET_FACTORIES.get(); |
| 185 | | | |
| 186 | 0 | | if (result == null) |
| 187 | | | { |
| 188 | 0 | | logger.fine("Creating new SSL_SOCKET_FACTORY for Thread."); |
| 189 | 0 | | SSLContext ctx = null; |
| 190 | | | try |
| 191 | | | { |
| 192 | | | |
| 193 | 0 | | if (mKeyStore == null) |
| 194 | | | { |
| 195 | 0 | | logger.finest("Loading keystore from file system - " |
| 196 | | | + mKeyStoreLocation); |
| 197 | 0 | | final char[] passphraseKeyStore |
| 198 | | | = mKeyStorePassword.toCharArray(); |
| 199 | 0 | | mKeyStore = KeyStore.getInstance("JKS"); |
| 200 | 0 | (4)(5) | mKeyStore.load(new FileInputStream( |
| 201 | | | mKeyStoreLocation), passphraseKeyStore); |
| 202 | | | } |
| 203 | 0 | | if (mTrustStore == null) |
| 204 | | | { |
| 205 | 0 | | logger.finest("Loading truststore from file system - " |
| 206 | | | + mTrustStoreLocation); |
| 207 | 0 | | final char[] passphraseTrustStore |
| 208 | | | = mTrustStorePassword.toCharArray(); |
| 209 | 0 | | mTrustStore = KeyStore.getInstance("JKS"); |
| 210 | 0 | | mTrustStore.load(new FileInputStream( |
| 211 | | | mTrustStoreLocation), passphraseTrustStore); |
| 212 | | | } |
| 213 | | | |
| 214 | 0 | | if (!mKeyStore.containsAlias(mKeyAlias)) |
| 215 | | | { |
| 216 | 0 | | final String reason |
| 217 | | | = "Keystore does not contain key for alias " |
| 218 | | | + "<" + mKeyAlias + ">"; |
| 219 | 0 | | final InitializingSslFailedException sse |
| 220 | | | = new InitializingSslFailedException(reason); |
| 221 | 0 | | throw sse; |
| 222 | | | } |
| 223 | 0 | | ctx = SSLContext.getInstance("TLS"); |
| 224 | 0 | | ctx.init(getKeyManagers(), getTrustManagers(), null); |
| 225 | | | } |
| 226 | 0 | | catch (GeneralSecurityException gse) |
| 227 | | | { |
| 228 | 0 | | final String reason = gse.getMessage(); |
| 229 | 0 | | final InitializingSslFailedException sse |
| 230 | | | = new InitializingSslFailedException(reason, gse); |
| 231 | 0 | | throw sse; |
| 232 | 0 | | } |
| 233 | 0 | | result = ctx.getSocketFactory(); |
| 234 | 0 | | SSL_SOCKET_FACTORIES.set(result); |
| 235 | | | } |
| 236 | 0 | | return result; |
| 237 | | | } |
| 238 | | | |
| 239 | | | {@inheritDoc} |
| 240 | | | public Socket createSocket ( |
| 241 | | | String host, int port, InetAddress localAddress , |
| 242 | | | int localPort, HttpConnectionParams params) |
| 243 | | (6) | throws IOException, UnknownHostException, ConnectTimeoutException |
| 244 | | | { |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | |
| 249 | 0 | | final Socket tcpSock = new Socket(); |
| 250 | 0 | | final SocketAddress endPoint = new InetSocketAddress(host, port); |
| 251 | 0 | | tcpSock.connect(endPoint, params.getConnectionTimeout()); |
| 252 | 0 | | final Socket sock |
| 253 | | | = getSslSocketFactory().createSocket(tcpSock, host, port, true); |
| 254 | 0 | (7) | return sock; |
| 255 | | | } |
| 256 | | | |
| 257 | | | {@inheritDoc} |
| 258 | | | public Socket createSocket (String host, int port) |
| 259 | | | throws IOException, UnknownHostException |
| 260 | | | { |
| 261 | 0 | (8) | throw new UnsupportedOperationException("Method not supported"); |
| 262 | | | } |
| 263 | | | |
| 264 | | | {@inheritDoc} |
| 265 | | | public Socket createSocket ( |
| 266 | | | Socket socket, String host, int port, boolean autoClose) |
| 267 | | | { |
| 268 | 0 | (9) | throw new UnsupportedOperationException("Method not supported"); |
| 269 | | | } |
| 270 | | | |
| 271 | | | {@inheritDoc} |
| 272 | | | public Socket createSocket ( |
| 273 | | | String arg0, int arg1, InetAddress arg2, int arg3) |
| 274 | | | throws IOException, UnknownHostException |
| 275 | | | { |
| 276 | 0 | (10) | throw new UnsupportedOperationException("Method not supported"); |
| 277 | | | } |
| 278 | | | } |