1 28 29 package com.caucho.vfs; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.IntMap; 33 34 import javax.net.ssl.SSLPeerUnverifiedException; 35 import javax.net.ssl.SSLSession; 36 import javax.net.ssl.SSLSocket; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.net.InetAddress ; 41 import java.net.Socket ; 42 import java.nio.channels.SelectableChannel ; 43 import java.security.cert.CertificateException ; 44 import java.security.cert.X509Certificate ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 public class QSocketWrapper extends QSocket { 52 private static final Logger log = Log.open(QSocketWrapper.class); 53 private static Class sslSocketClass; 54 private static IntMap sslKeySizes; 55 56 private Socket _s; 57 private InputStream _is; 58 private OutputStream _os; 59 private SocketStream _streamImpl; 60 61 public QSocketWrapper() 62 { 63 } 64 65 public QSocketWrapper(Socket s) 66 { 67 init(s); 68 } 69 70 public void init(Socket s) 71 { 72 _s = s; 73 _is = null; 74 _os = null; 75 } 76 77 80 public void setReadTimeout(int ms) 81 throws IOException 82 { 83 _s.setSoTimeout(ms); 84 } 85 86 89 public InetAddress getLocalAddress() 90 { 91 return _s.getLocalAddress(); 92 } 93 94 97 public int getLocalPort() 98 { 99 return _s.getLocalPort(); 100 } 101 102 105 public long getRemoteIP() 106 { 107 InetAddress addr = _s.getInetAddress(); 108 byte []bytes = addr.getAddress(); 109 110 long address = 0; 111 for (int i = 0; i < bytes.length; i++) 112 address = 256 * address + (bytes[i] & 0xff); 113 114 return address; 115 } 116 117 120 public InetAddress getRemoteAddress() 121 { 122 return _s.getInetAddress(); 123 } 124 125 128 public int getRemotePort() 129 { 130 return _s.getPort(); 131 } 132 133 136 public boolean isSecure() 137 { 138 if (_s == null || sslSocketClass == null) 139 return false; 140 else 141 return sslSocketClass.isAssignableFrom(_s.getClass()); 142 } 143 146 public String getCipherSuite() 147 { 148 if (! (_s instanceof SSLSocket)) 149 return super.getCipherSuite(); 150 151 SSLSocket sslSocket = (SSLSocket) _s; 152 153 SSLSession sslSession = sslSocket.getSession(); 154 155 if (sslSession != null) 156 return sslSession.getCipherSuite(); 157 else 158 return null; 159 } 160 161 164 public int getCipherBits() 165 { 166 if (! (_s instanceof SSLSocket)) 167 return super.getCipherBits(); 168 169 SSLSocket sslSocket = (SSLSocket) _s; 170 171 SSLSession sslSession = sslSocket.getSession(); 172 173 if (sslSession != null) 174 return sslKeySizes.get(sslSession.getCipherSuite()); 175 else 176 return 0; 177 } 178 179 182 public X509Certificate getClientCertificate() 183 throws CertificateException 184 { 185 X509Certificate []certs = getClientCertificates(); 186 187 if (certs == null || certs.length == 0) 188 return null; 189 else 190 return certs[0]; 191 } 192 193 196 public X509Certificate []getClientCertificates() 197 throws CertificateException 198 { 199 if (sslSocketClass == null) 200 return null; 201 else 202 return getClientCertificatesImpl(); 203 } 204 205 208 private X509Certificate []getClientCertificatesImpl() 209 throws CertificateException 210 { 211 if (! (_s instanceof SSLSocket)) 212 return null; 213 214 SSLSocket sslSocket = (SSLSocket) _s; 215 216 SSLSession sslSession = sslSocket.getSession(); 217 if (sslSession == null) 218 return null; 219 220 String cipherSuite = sslSession.getCipherSuite(); 221 222 try { 223 return (X509Certificate []) sslSession.getPeerCertificates(); 224 } catch (SSLPeerUnverifiedException e) { 225 if (log.isLoggable(Level.FINEST)) 226 log.log(Level.FINEST, e.toString(), e); 227 228 return null; 229 } catch (Throwable e) { 230 log.log(Level.FINER, e.toString(), e); 231 } 232 233 return null; 234 } 235 236 239 public SelectableChannel getSelectableChannel() 240 { 241 if (_s != null) 242 return _s.getChannel(); 243 else 244 return null; 245 } 246 247 250 public StreamImpl getStream() 251 throws IOException 252 { 253 if (_streamImpl == null) 254 _streamImpl = new SocketStream(); 255 256 _streamImpl.init(getInputStream(), getOutputStream()); 257 258 return _streamImpl; 259 } 260 261 264 private InputStream getInputStream() 265 throws IOException 266 { 267 if (_is == null) 268 _is = _s.getInputStream(); 269 270 return _is; 271 } 272 273 276 private OutputStream getOutputStream() 277 throws IOException 278 { 279 if (_os == null) 280 _os = _s.getOutputStream(); 281 282 return _os; 283 } 284 285 public void resetTotalBytes() 286 { 287 if (_streamImpl != null) 288 _streamImpl.resetTotalBytes(); 289 } 290 291 public long getTotalReadBytes() 292 { 293 return (_streamImpl == null) ? 0 : _streamImpl.getTotalReadBytes(); 294 } 295 296 public long getTotalWriteBytes() 297 { 298 return (_streamImpl == null) ? 0 : _streamImpl.getTotalWriteBytes(); 299 } 300 301 304 public boolean isClosed() 305 { 306 return _s == null; 307 } 308 309 312 public void close() 313 throws IOException 314 { 315 Socket s = _s; 316 _s = null; 317 318 InputStream is = _is; 319 _is = null; 320 321 OutputStream os = _os; 322 _os = null; 323 324 if (os != null) { 325 try { 326 os.close(); 327 } catch (Exception e) { 328 } 329 } 330 331 if (is != null) { 332 try { 333 is.close(); 334 } catch (Exception e) { 335 } 336 } 337 338 if (s != null) { 339 try { 340 s.close(); 341 } catch (Exception e) { 342 } 343 } 344 } 345 346 public String toString() 347 { 348 return "QSocketWrapper[" + _s + "]"; 349 } 350 351 static { 352 try { 353 sslSocketClass = Class.forName("javax.net.ssl.SSLSocket"); 354 } catch (Throwable e) { 355 } 356 357 sslKeySizes = new IntMap(); 358 sslKeySizes.put("SSL_DH_anon_WITH_DES_CBC_SHA", 56); 359 sslKeySizes.put("SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", 168); 360 sslKeySizes.put("SSL_DH_anon_WITH_RC4_128_MD5", 128); 361 sslKeySizes.put("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", 40); 362 sslKeySizes.put("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", 40); 363 sslKeySizes.put("SSL_DHE_DSS_WITH_DES_CBC_SHA", 56); 364 sslKeySizes.put("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", 40); 365 sslKeySizes.put("SSL_RSA_WITH_RC4_128_MD5", 128); 366 sslKeySizes.put("SSL_RSA_WITH_RC4_128_SHA", 128); 367 sslKeySizes.put("SSL_RSA_WITH_DES_CBC_SHA", 56); 368 sslKeySizes.put("SSL_RSA_WITH_3DES_EDE_CBC_SHA", 168); 369 sslKeySizes.put("SSL_RSA_EXPORT_WITH_RC4_40_MD5", 40); 370 sslKeySizes.put("SSL_RSA_WITH_NULL_MD5", 0); 371 sslKeySizes.put("SSL_RSA_WITH_NULL_SHA", 0); 372 sslKeySizes.put("SSL_DSA_WITH_RC4_128_MD5", 128); 373 sslKeySizes.put("SSL_DSA_WITH_RC4_128_SHA", 128); 374 sslKeySizes.put("SSL_DSA_WITH_DES_CBC_SHA", 56); 375 sslKeySizes.put("SSL_DSA_WITH_3DES_EDE_CBC_SHA", 168); 376 sslKeySizes.put("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", 168); 377 sslKeySizes.put("SSL_DSA_EXPORT_WITH_RC4_40_MD5", 40); 378 sslKeySizes.put("SSL_DSA_WITH_NULL_MD5", 0); 379 sslKeySizes.put("SSL_DSA_WITH_NULL_SHA", 0); 380 } 381 } 382 383 | Popular Tags |