001/* 002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.9/src/java/org/apache/commons/ssl/JavaImpl.java $ 003 * $Revision: 121 $ 004 * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 2007) $ 005 * 006 * ==================================================================== 007 * Licensed to the Apache Software Foundation (ASF) under one 008 * or more contributor license agreements. See the NOTICE file 009 * distributed with this work for additional information 010 * regarding copyright ownership. The ASF licenses this file 011 * to you under the Apache License, Version 2.0 (the 012 * "License"); you may not use this file except in compliance 013 * with the License. You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, 018 * software distributed under the License is distributed on an 019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 020 * KIND, either express or implied. See the License for the 021 * specific language governing permissions and limitations 022 * under the License. 023 * ==================================================================== 024 * 025 * This software consists of voluntary contributions made by many 026 * individuals on behalf of the Apache Software Foundation. For more 027 * information on the Apache Software Foundation, please see 028 * <http://www.apache.org/>. 029 * 030 */ 031 032package org.apache.commons.ssl; 033 034import javax.net.SocketFactory; 035import javax.net.ssl.SSLPeerUnverifiedException; 036import javax.net.ssl.SSLServerSocket; 037import javax.net.ssl.SSLServerSocketFactory; 038import javax.net.ssl.SSLSession; 039import javax.net.ssl.SSLSocket; 040import javax.net.ssl.SSLSocketFactory; 041import java.io.IOException; 042import java.net.InetAddress; 043import java.net.Socket; 044import java.security.KeyManagementException; 045import java.security.KeyStore; 046import java.security.KeyStoreException; 047import java.security.NoSuchAlgorithmException; 048import java.security.UnrecoverableKeyException; 049import java.security.cert.Certificate; 050import java.security.cert.CertificateException; 051import java.security.cert.X509Certificate; 052 053/** 054 * @author Credit Union Central of British Columbia 055 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a> 056 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a> 057 * @since 30-Jun-2006 058 */ 059public abstract class JavaImpl { 060 private static JavaImpl HANDLER; 061 062 static { 063 JavaImpl h = null; 064 try { 065 h = Java14.getInstance(); 066 } 067 catch (Throwable t) { 068 // System.out.println( t.toString() ); 069 System.out.println("commons-ssl reverting to: Java 1.3 + jsse.jar"); 070 } 071 if (h == null) { 072 h = Java13.getInstance(); 073 } 074 HANDLER = h; 075 } 076 077 public static void downgrade() { 078 if (HANDLER instanceof Java14) { 079 HANDLER = Java13.getInstance(); 080 } 081 } 082 083 public static boolean isJava13() { 084 return HANDLER instanceof Java13; 085 } 086 087 public static void uprade() { 088 if (HANDLER instanceof Java13) { 089 HANDLER = Java14.getInstance(); 090 } 091 } 092 093 public abstract String getVersion(); 094 095 protected abstract Object buildKeyManagerFactory(KeyStore ks, char[] pass) 096 throws NoSuchAlgorithmException, KeyStoreException, 097 UnrecoverableKeyException; 098 099 protected abstract Object[] retrieveKeyManagers(Object keyManagerFactory); 100 101 protected abstract Object buildTrustManagerFactory(KeyStore ks) 102 throws NoSuchAlgorithmException, KeyStoreException; 103 104 protected abstract Object[] retrieveTrustManagers(Object trustManagerFactory); 105 106 protected abstract String retrieveSubjectX500(X509Certificate cert); 107 108 protected abstract String retrieveIssuerX500(X509Certificate cert); 109 110 protected abstract Certificate[] retrievePeerCerts(SSLSession sslSession) 111 throws SSLPeerUnverifiedException; 112 113 protected abstract SSLSocketFactory buildSSLSocketFactory(Object ssl); 114 115 protected abstract SSLServerSocketFactory buildSSLServerSocketFactory(Object ssl); 116 117 protected abstract SSLSocket buildSocket(SSL ssl) 118 throws IOException; 119 120 protected abstract SSLSocket buildSocket(SSL ssl, String remoteHost, 121 int remotePort, 122 InetAddress localHost, 123 int localPort, int connectTimeout) 124 throws IOException; 125 126 protected abstract Socket connectSocket(Socket s, SocketFactory sf, 127 String remoteHost, int remotePort, 128 InetAddress localHost, int localPort, 129 int timeout) 130 throws IOException; 131 132 protected abstract SSLServerSocket buildServerSocket(SSL ssl) 133 throws IOException; 134 135 protected abstract void wantClientAuth(Object o, boolean wantClientAuth); 136 137 protected abstract void enabledProtocols(Object o, String[] enabledProtocols); 138 139 protected abstract RuntimeException buildRuntimeException(Exception cause); 140 141 protected abstract Object initSSL(SSL ssl, TrustChain tc, KeyMaterial km) 142 throws NoSuchAlgorithmException, KeyStoreException, 143 CertificateException, KeyManagementException, IOException; 144 145 protected abstract void checkTrusted(Object trustManager, 146 X509Certificate[] chain, 147 String authType) 148 throws CertificateException; 149 150 public static Object init(SSL ssl, TrustChain trustChain, KeyMaterial keyMaterial) 151 throws NoSuchAlgorithmException, KeyStoreException, 152 CertificateException, KeyManagementException, IOException { 153 return HANDLER.initSSL(ssl, trustChain, keyMaterial); 154 } 155 156 public static RuntimeException newRuntimeException(Exception cause) { 157 return HANDLER.buildRuntimeException(cause); 158 } 159 160 public static SSLSocketFactory getSSLSocketFactory(Object sslContext) { 161 return HANDLER.buildSSLSocketFactory(sslContext); 162 } 163 164 public static SSLServerSocketFactory getSSLServerSocketFactory(Object sslContext) { 165 return HANDLER.buildSSLServerSocketFactory(sslContext); 166 } 167 168 public static String getSubjectX500(X509Certificate cert) { 169 return HANDLER.retrieveSubjectX500(cert); 170 } 171 172 public static String getIssuerX500(X509Certificate cert) { 173 return HANDLER.retrieveIssuerX500(cert); 174 } 175 176 public static Object newKeyManagerFactory(KeyStore ks, char[] password) 177 throws NoSuchAlgorithmException, KeyStoreException, 178 UnrecoverableKeyException { 179 return HANDLER.buildKeyManagerFactory(ks, password); 180 } 181 182 public static Object[] getKeyManagers(Object keyManagerFactory) { 183 return HANDLER.retrieveKeyManagers(keyManagerFactory); 184 } 185 186 public static Object newTrustManagerFactory(KeyStore ks) 187 throws NoSuchAlgorithmException, KeyStoreException { 188 return HANDLER.buildTrustManagerFactory(ks); 189 } 190 191 public static Object[] getTrustManagers(Object trustManagerFactory) { 192 return HANDLER.retrieveTrustManagers(trustManagerFactory); 193 } 194 195 public static SSLSocket createSocket(SSL ssl) 196 throws IOException { 197 return HANDLER.buildSocket(ssl); 198 } 199 200 public static SSLSocket createSocket(SSL ssl, String remoteHost, 201 int remotePort, InetAddress localHost, 202 int localPort, int connectTimeout) 203 throws IOException { 204 return HANDLER.buildSocket(ssl, remoteHost, remotePort, localHost, 205 localPort, connectTimeout); 206 } 207 208 protected static Socket connect(Socket s, SocketFactory sf, 209 String remoteHost, int remotePort, 210 InetAddress localHost, int localPort, 211 int timeout) 212 throws IOException { 213 return HANDLER.connectSocket(s, sf, remoteHost, remotePort, localHost, 214 localPort, timeout); 215 } 216 217 public static SSLServerSocket createServerSocket(SSL ssl) 218 throws IOException { 219 return HANDLER.buildServerSocket(ssl); 220 } 221 222 public static void setWantClientAuth(Object o, boolean wantClientAuth) { 223 HANDLER.wantClientAuth(o, wantClientAuth); 224 } 225 226 public static void setEnabledProtocols(Object o, String[] enabledProtocols) { 227 HANDLER.enabledProtocols(o, enabledProtocols); 228 } 229 230 public static Certificate[] getPeerCertificates(SSLSession session) 231 throws SSLPeerUnverifiedException { 232 return HANDLER.retrievePeerCerts(session); 233 } 234 235 public static void testTrust(Object trustManager, X509Certificate[] chain, 236 String authType) 237 throws CertificateException { 238 HANDLER.checkTrusted(trustManager, chain, authType); 239 } 240 241 public static void load() { 242 HANDLER.hashCode(); 243 } 244 245}