not-yet-commons-ssl-0.3.11

eption(e);
    }
    return newChain;
  }
  
  protected final Object buildKeyManagerFactory(KeyStore ks, char[] password)
    throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException
  {
    String alg = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(alg);
    kmf.init(ks, password);
    return kmf;
  }
  
  protected final Object buildTrustManagerFactory(KeyStore ks)
    throws NoSuchAlgorithmException, KeyStoreException
  {
    String alg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg);
    tmf.init(ks);
    return tmf;
  }
  
  protected final Object[] retrieveKeyManagers(Object keyManagerFactory)
  {
    KeyManagerFactory kmf = (KeyManagerFactory)keyManagerFactory;
    return kmf.getKeyManagers();
  }
  
  protected final Object[] retrieveTrustManagers(Object trustManagerFactory)
  {
    TrustManagerFactory tmf = (TrustManagerFactory)trustManagerFactory;
    return tmf.getTrustManagers();
  }
  
  protected final SSLSocketFactory buildSSLSocketFactory(Object ssl)
  {
    return ((SSLContext)ssl).getSocketFactory();
  }
  
  protected final SSLServerSocketFactory buildSSLServerSocketFactory(Object ssl)
  {
    return ((SSLContext)ssl).getServerSocketFactory();
  }
  
  protected final RuntimeException buildRuntimeException(Exception cause)
  {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
    PrintStream ps = new PrintStream(byteOut);
    ps.println(cause.toString());
    cause.printStackTrace(ps);
    ps.flush();
    String originalCause = byteOut.toString();
    return new RuntimeException(originalCause);
  }
  
  protected final SSLSocket buildSocket(SSL ssl)
  {
    throw new UnsupportedOperationException();
  }
  
  protected final SSLSocket buildSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout)
    throws IOException
  {
    SSLSocketFactory sf = ssl.getSSLSocketFactory();
    SSLSocket s = (SSLSocket)connectSocket(null, sf, remoteHost, remotePort, localHost, localPort, -1, ssl);
    
    ssl.doPreConnectSocketStuff(s);
    ssl.doPostConnectSocketStuff(s, remoteHost);
    return s;
  }
  
  protected final Socket buildPlainSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout)
    throws IOException
  {
    SocketFactory sf = SocketFactory.getDefault();
    Socket s = connectSocket(null, sf, remoteHost, remotePort, localHost, localPort, -1, ssl);
    
    ssl.doPreConnectSocketStuff(s);
    ssl.doPostConnectSocketStuff(s, remoteHost);
    return s;
  }
  
  protected final Socket connectSocket(Socket s, SocketFactory sf, String remoteHost, int remotePort, InetAddress localHost, int localPort, int timeout, SSL ssl)
    throws IOException
  {
    remoteHost = ssl.dnsOverride(remoteHost);
    if (s == null) {
      if (sf == null) {
        s = new Socket(remoteHost, remotePort, localHost, localPort);
      } else {
        s = sf.createSocket(remoteHost, remotePort, localHost, localPort);
      }
    }
    return s;
  }
  
  protected final SSLServerSocket buildServerSocket(SSL ssl)
  {
    throw new UnsupportedOperationException();
  }
  
  protected final void wantClientAuth(Object o, boolean wantClientAuth) {}
  
  protected final void enabledProtocols(Object o, String[] enabledProtocols) {}
  
  protected void checkTrusted(Object trustManager, java.security.cert.X509Certificate[] chain, String authType)
    throws CertificateException
  {
    X509TrustManager tm = (X509TrustManager)trustManager;
    boolean result = tm.isServerTrusted(chain);
    if (!result) {
      throw new CertificateException("commons-ssl java13 mode: certificate chain not trusted");
    }
  }
  
  protected final Object initSSL(SSL ssl, TrustChain tc, KeyMaterial k)
    throws NoSuchAlgorithmException, KeyStoreException, CertificateException, KeyManagementException, IOException
  {
    SSLContext context = SSLContext.getInstance(ssl.getDefaultProtocol());
    TrustManager[] trustManagers = null;
    KeyManager[] keyManagers = null;
    if (tc != null) {
      trustManagers = (TrustManager[])tc.getTrustManagers();
    }
    if (k != null) {
      keyManagers = (KeyManager[])k.getKeyManagers();
    }
    if (keyManagers != null) {
      for (int i = 0; i < keyManagers.length; i++) {
        if ((keyManagers[i] instanceof X509KeyManager))
        {
          X509KeyManager km = (X509KeyManager)keyManagers[i];
          keyManagers[i] = new Java13KeyManagerWrapper(km, k, ssl);
        }
      }
    }
    if (trustManagers != null) {
      for (int i = 0; i < trustManagers.length; i++) {
        if ((trustManagers[i] instanceof X509TrustManager))
        {
          X509TrustManager tm = (X509TrustManager)trustManagers[i];
          trustManagers[i] = new Java13TrustManagerWrapper(tm, tc, ssl);
        }
      }
    }
    context.init(keyManagers, trustManagers, null);
    return context;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java13
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import com.sun.net.ssl.X509KeyManager;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

public class Java13KeyManagerWrapper
  implements X509KeyManager
{
  private final X509KeyManager keyManager;
  
  public Java13KeyManagerWrapper(X509KeyManager m, KeyMaterial km, SSL h)
  {
    keyManager = m;
  }
  
  public String chooseClientAlias(String keyType, Principal[] issuers)
  {
    return keyManager.chooseClientAlias(keyType, issuers);
  }
  
  public String chooseServerAlias(String keyType, Principal[] issuers)
  {
    return keyManager.chooseServerAlias(keyType, issuers);
  }
  
  public X509Certificate[] getCertificateChain(String alias)
  {
    return keyManager.getCertificateChain(alias);
  }
  
  public String[] getClientAliases(String keyType, Principal[] issuers)
  {
    return keyManager.getClientAliases(keyType, issuers);
  }
  
  public PrivateKey getPrivateKey(String alias)
  {
    return keyManager.getPrivateKey(alias);
  }
  
  public String[] getServerAliases(String keyType, Principal[] issuers)
  {
    return keyManager.getServerAliases(keyType, issuers);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java13KeyManagerWrapper
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import com.sun.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class Java13TrustManagerWrapper
  implements X509TrustManager
{
  private final X509TrustManager trustManager;
  private final TrustChain trustChain;
  private final SSL ssl;
  
  public Java13TrustManagerWrapper(X509TrustManager m, TrustChain tc, SSL h)
  {
    trustManager = m;
    trustChain = tc;
    ssl = h;
  }
  
  public boolean isClientTrusted(X509Certificate[] chain)
  {
    ssl.setCurrentClientChain(chain);
    boolean firstTest = trustManager.isClientTrusted(chain);
    return test(firstTest, chain);
  }
  
  public boolean isServerTrusted(X509Certificate[] chain)
  {
    ssl.setCurrentServerChain(chain);
    boolean firstTest = trustManager.isServerTrusted(chain);
    return test(firstTest, chain);
  }
  
  public X509Certificate[] getAcceptedIssuers()
  {
    if (trustChain.containsTrustAll()) {
      return new X509Certificate[0];
    }
    return trustManager.getAcceptedIssuers();
  }
  
  private boolean test(boolean firstTest, X509Certificate[] chain)
  {
    if ((!firstTest) && 
      (!trustChain.contains(TrustMaterial.TRUST_ALL))) {
      return false;
    }
    try
    {
      for (int i = 0; i < chain.length; i++)
      {
        X509Certificate c = chain[i];
        if (ssl.getCheckExpiry()) {
          c.checkValidity();
        }
        if (ssl.getCheckCRL()) {
          Certificates.checkCRL(c);
        }
      }
      return true;
    }
    catch (CertificateException ce) {}
    return false;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java13TrustManagerWrapper
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.x500.X500Principal;

public final class Java14
  extends JavaImpl
{
  private static Java14 instance = new Java14();
  
  private Java14()
  {
    try
    {
      SSLSocketFactory.getDefault().createSocket();
    }
    catch (IOException ioe)
    {
      ioe.hashCode();
    }
  }
  
  public static Java14 getInstance()
  {
    return instance;
  }
  
  public final String getVersion()
  {
    return "Java14";
  }
  
  protected final String retrieveSubjectX500(X509Certificate cert)
  {
    return cert.getSubjectX500Principal().toString();
  }
  
  protected final String retrieveIssuerX500(X509Certificate cert)
  {
    return cert.getIssuerX500Principal().toString();
  }
  
  protected final Certificate[] retrievePeerCerts(SSLSession sslSession)
    throws SSLPeerUnverifiedException
  {
    return sslSession.getPeerCertificates();
  }
  
  protected final Object buildKeyManagerFactory(KeyStore ks, char[] password)
    throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException
  {
    String alg = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(alg);
    kmf.init(ks, password);
    return kmf;
  }
  
  protected final Object buildTrustManagerFactory(KeyStore ks)
    throws NoSuchAlgorithmException, KeyStoreException
  {
    String alg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg);
    tmf.init(ks);
    return tmf;
  }
  
  protected final Object[] retrieveKeyManagers(Object keyManagerFactory)
  {
    KeyManagerFactory kmf = (KeyManagerFactory)keyManagerFactory;
    return kmf.getKeyManagers();
  }
  
  protected final Object[] retrieveTrustManagers(Object trustManagerFactory)
  {
    TrustManagerFactory tmf = (TrustManagerFactory)trustManagerFactory;
    return tmf.getTrustManagers();
  }
  
  protected final SSLSocketFactory buildSSLSocketFactory(Object ssl)
  {
    return ((SSLContext)ssl).getSocketFactory();
  }
  
  protected final SSLServerSocketFactory buildSSLServerSocketFactory(Object ssl)
  {
    return ((SSLContext)ssl).getServerSocketFactory();
  }
  
  protected final RuntimeException buildRuntimeException(Exception cause)
  {
    return new RuntimeException(cause);
  }
  
  protected final SSLSocket buildSocket(SSL ssl)
    throws IOException
  {
    SSLSocketFactory sf = ssl.getSSLSocketFactory();
    SSLSocket s = (SSLSocket)sf.createSocket();
    ssl.doPreConnectSocketStuff(s);
    return s;
  }
  
  protected final SSLSocket buildSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int timeout)
    throws IOException
  {
    SSLSocket s = buildSocket(ssl);
    s = (SSLSocket)connectSocket(s, null, remoteHost, remotePort, localHost, localPort, timeout, ssl);
    
    ssl.doPostConnectSocketStuff(s, remoteHost);
    return s;
  }
  
  protected final Socket buildPlainSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int timeout)
    throws IOException
  {
    Socket s = SocketFactory.getDefault().createSocket();
    ssl.doPreConnectSocketStuff(s);
    s = connectSocket(s, null, remoteHost, remotePort, localHost, localPort, timeout, ssl);
    
    ssl.doPostConnectSocketStuff(s, remoteHost);
    return s;
  }
  
  protected final Socket connectSocket(Socket s, SocketFactory sf, String remoteHost, int remotePort, InetAddress localHost, int localPort, int timeout, SSL ssl)
    throws IOException
  {
    if (s == null) {
      if (sf == null) {
        s = new Socket();
      } else {
        s = sf.createSocket();
      }
    }
    String orig = remoteHost;
    remoteHost = ssl.dnsOverride(remoteHost);
    InetSocketAddress dest = new InetSocketAddress(remoteHost, remotePort);
    InetSocketAddress src = new InetSocketAddress(localHost, localPort);
    s.bind(src);
    s.connect(dest, timeout);
    return s;
  }
  
  protected final SSLServerSocket buildServerSocket(SSL ssl)
    throws IOException
  {
    ServerSocket s = ssl.getSSLServerSocketFactory().createServerSocket();
    SSLServerSocket ss = (SSLServerSocket)s;
    ssl.doPreConnectServerSocketStuff(ss);
    return ss;
  }
  
  protected final void wantClientAuth(Object o, boolean wantClientAuth)
  {
    if ((o instanceof SSLSocket))
    {
      SSLSocket s = (SSLSocket)o;
      s.setWantClientAuth(wantClientAuth);
    }
    else if ((o instanceof SSLServerSocket))
    {
      SSLServerSocket ss = (SSLServerSocket)o;
      ss.setWantClientAuth(wantClientAuth);
    }
    else
    {
      throw new ClassCastException("need SSLSocket or SSLServerSocket");
    }
  }
  
  protected final void enabledProtocols(Object o, String[] enabledProtocols)
  {
    if ((o instanceof SSLSocket))
    {
      SSLSocket s = (SSLSocket)o;
      s.setEnabledProtocols(enabledProtocols);
    }
    else if ((o instanceof SSLServerSocket))
    {
      SSLServerSocket ss = (SSLServerSocket)o;
      ss.setEnabledProtocols(enabledProtocols);
    }
    else
    {
      throw new ClassCastException("need SSLSocket or SSLServerSocket");
    }
  }
  
  protected void checkTrusted(Object trustManager, X509Certificate[] chain, String authType)
    throws CertificateException
  {
    X509TrustManager tm = (X509TrustManager)trustManager;
    tm.checkServerTrusted(chain, authType);
  }
  
  protected final Object initSSL(SSL ssl, TrustChain tc, KeyMaterial k)
    throws NoSuchAlgorithmException, KeyStoreException, CertificateException, KeyManagementException, IOException
  {
    SSLContext context = SSLContext.getInstance(ssl.getDefaultProtocol());
    TrustManager[] trustManagers = null;
    KeyManager[] keyManagers = null;
    if (tc != null) {
      trustManagers = (TrustManager[])tc.getTrustManagers();
    }
    if (k != null) {
      keyManagers = (KeyManager[])k.getKeyManagers();
    }
    if (keyManagers != null) {
      for (int i = 0; i < keyManagers.length; i++) {
        if ((keyManagers[i] instanceof X509KeyManager))
        {
          X509KeyManager km = (X509KeyManager)keyManagers[i];
          keyManagers[i] = new Java14KeyManagerWrapper(km, k, ssl);
        }
      }
    }
    if (trustManagers != null) {
      for (int i = 0; i < trustManagers.length; i++) {
        if ((trustManagers[i] instanceof X509TrustManager))
        {
          X509TrustManager tm = (X509TrustManager)trustManagers[i];
          trustManagers[i] = new Java14TrustManagerWrapper(tm, tc, ssl);
        }
      }
    }
    context.init(keyManagers, trustManagers, null);
    return context;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java14
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.net.Socket;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509KeyManager;

public class Java14KeyManagerWrapper
  implements X509KeyManager
{
  private final X509KeyManager keyManager;
  
  public Java14KeyManagerWrapper(X509KeyManager m, KeyMaterial km, SSL h)
  {
    keyManager = m;
  }
  
  public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)
  {
    return keyManager.chooseClientAlias(keyType, issuers, socket);
  }
  
  public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket)
  {
    return keyManager.chooseServerAlias(keyType, issuers, socket);
  }
  
  public X509Certificate[] getCertificateChain(String alias)
  {
    return keyManager.getCertificateChain(alias);
  }
  
  public String[] getClientAliases(String keyType, Principal[] issuers)
  {
    return keyManager.getClientAliases(keyType, issuers);
  }
  
  public PrivateKey getPrivateKey(String alias)
  {
    return keyManager.getPrivateKey(alias);
  }
  
  public String[] getServerAliases(String keyType, Principal[] issuers)
  {
    return keyManager.getServerAliases(keyType, issuers);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java14KeyManagerWrapper
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;

public class Java14TrustManagerWrapper
  implements X509TrustManager
{
  private final X509TrustManager trustManager;
  private final TrustChain trustChain;
  private final SSL ssl;
  
  public Java14TrustManagerWrapper(X509TrustManager m, TrustChain tc, SSL h)
  {
    trustManager = m;
    trustChain = tc;
    ssl = h;
  }
  
  public void checkClientTrusted(X509Certificate[] chain, String authType)
    throws CertificateException
  {
    ssl.setCurrentClientChain(chain);
    CertificateException ce = null;
    try
    {
      trustManager.checkClientTrusted(chain, authType);
    }
    catch (CertificateException e)
    {
      ce = e;
    }
    testShouldWeThrow(ce, chain);
  }
  
  public void checkServerTrusted(X509Certificate[] chain, String authType)
    throws CertificateException
  {
    ssl.setCurrentServerChain(chain);
    CertificateException ce = null;
    try
    {
      trustManager.checkServerTrusted(chain, authType);
    }
    catch (CertificateException e)
    {
      ce = e;
    }
    testShouldWeThrow(ce, chain);
  }
  
  public X509Certificate[] getAcceptedIssuers()
  {
    if (trustChain.containsTrustAll()) {
      return new X509Certificate[0];
    }
    return trustManager.getAcceptedIssuers();
  }
  
  private void testShouldWeThrow(CertificateException checkException, X509Certificate[] chain)
    throws CertificateException
  {
    if (checkException != null)
    {
      Throwable root = getRootThrowable(checkException);
      boolean expiryProblem = root instanceof CertificateExpiredException;
      if (expiryProblem)
      {
        if (ssl.getCheckExpiry()) {
          throw checkException;
        }
      }
      else if (!trustChain.contains(TrustMaterial.TRUST_ALL)) {
        throw checkException;
      }
    }
    for (int i = 0; i < chain.length; i++)
    {
      X509Certificate c = chain[i];
      if (ssl.getCheckExpiry()) {
        c.checkValidity();
      }
      if (ssl.getCheckCRL()) {
        Certificates.checkCRL(c);
      }
    }
  }
  
  private static Throwable getRootThrowable(Throwable t)
  {
    if (t == null) {
      return t;
    }
    Throwable cause = t.getCause();
    while ((cause != null) && (!t.equals(cause)))
    {
      t = cause;
      cause = t.getCause();
    }
    return t;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.Java14TrustManagerWrapper
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public abstract class JavaImpl
{
  private static JavaImpl HANDLER;
  
  static
  {
    JavaImpl h = null;
    try
    {
      h = Java14.getInstance();
    }
    catch (Throwable t)
    {
      System.out.println("commons-ssl reverting to: Java 1.3 + jsse.jar");
    }
    if (h == null) {
      h = Java13.getInstance();
    }
    HANDLER = h;
  }
  
  public static void downgrade()
  {
    if ((HANDLER instanceof Java14)) {
      HANDLER = Java13.getInstance();
    }
  }
  
  public static boolean isJava13()
  {
    return HANDLER instanceof Java13;
  }
  
  public static void uprade()
  {
    if ((HANDLER instanceof Java13)) {
      HANDLER = Java14.getInstance();
    }
  }
  
  public static Object init(SSL ssl, TrustChain trustChain, KeyMaterial keyMaterial)
    throws NoSuchAlgorithmException, KeyStoreException, CertificateException, KeyManagementException, IOException
  {
    return HANDLER.initSSL(ssl, trustChain, keyMaterial);
  }
  
  public static RuntimeException newRuntimeException(Exception cause)
  {
    return HANDLER.buildRuntimeException(cause);
  }
  
  public static SSLSocketFactory getSSLSocketFactory(Object sslContext)
  {
    return HANDLER.buildSSLSocketFactory(sslContext);
  }
  
  public static SSLServerSocketFactory getSSLServerSocketFactory(Object sslContext)
  {
    return HANDLER.buildSSLServerSocketFactory(sslContext);
  }
  
  public static String getSubjectX500(X509Certificate cert)
  {
    return HANDLER.retrieveSubjectX500(cert);
  }
  
  public static String getIssuerX500(X509Certificate cert)
  {
    return HANDLER.retrieveIssuerX500(cert);
  }
  
  public static Object newKeyManagerFactory(KeyStore ks, char[] password)
    throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException
  {
    return HANDLER.buildKeyManagerFactory(ks, password);
  }
  
  public static Object[] getKeyManagers(Object keyManagerFactory)
  {
    return HANDLER.retrieveKeyManagers(keyManagerFactory);
  }
  
  public static Object newTrustManagerFactory(KeyStore ks)
    throws NoSuchAlgorithmException, KeyStoreException
  {
    return HANDLER.buildTrustManagerFactory(ks);
  }
  
  public static Object[] getTrustManagers(Object trustManagerFactory)
  {
    return HANDLER.retrieveTrustManagers(trustManagerFactory);
  }
  
  public static SSLSocket createSocket(SSL ssl)
    throws IOException
  {
    return HANDLER.buildSocket(ssl);
  }
  
  public static SSLSocket createSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout)
    throws IOException
  {
    return HANDLER.buildSocket(ssl, remoteHost, remotePort, localHost, localPort, connectTimeout);
  }
  
  public static Socket createPlainSocket(SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout)
    throws IOException
  {
    return HANDLER.buildPlainSocket(ssl, remoteHost, remotePort, localHost, localPort, connectTimeout);
  }
  
  protected static Socket connect(Socket s, SocketFactory sf, String remoteHost, int remotePort, InetAddress localHost, int localPort, int timeout, SSL ssl)
    throws IOException
  {
    return HANDLER.connectSocket(s, sf, remoteHost, remotePort, localHost, localPort, timeout, ssl);
  }
  
  public static SSLServerSocket createServerSocket(SSL ssl)
    throws IOException
  {
    return HANDLER.buildServerSocket(ssl);
  }
  
  public static void setWantClientAuth(Object o, boolean wantClientAuth)
  {
    HANDLER.wantClientAuth(o, wantClientAuth);
  }
  
  public static void setEnabledProtocols(Object o, String[] enabledProtocols)
  {
    HANDLER.enabledProtocols(o, enabledProtocols);
  }
  
  public static Certificate[] getPeerCertificates(SSLSession session)
    throws SSLPeerUnverifiedException
  {
    return HANDLER.retrievePeerCerts(session);
  }
  
  public static void testTrust(Object trustManager, X509Certificate[] chain, String authType)
    throws CertificateException
  {
    HANDLER.checkTrusted(trustManager, chain, authType);
  }
  
  public static void load()
  {
    HANDLER.hashCode();
  }
  
  public abstract String getVersion();
  
  protected abstract Object buildKeyManagerFactory(KeyStore paramKeyStore, char[] paramArrayOfChar)
    throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException;
  
  protected abstract Object[] retrieveKeyManagers(Object paramObject);
  
  protected abstract Object buildTrustManagerFactory(KeyStore paramKeyStore)
    throws NoSuchAlgorithmException, KeyStoreException;
  
  protected abstract Object[] retrieveTrustManagers(Object paramObject);
  
  protected abstract String retrieveSubjectX500(X509Certificate paramX509Certificate);
  
  protected abstract String retrieveIssuerX500(X509Certificate paramX509Certificate);
  
  protected abstract Certificate[] retrievePeerCerts(SSLSession paramSSLSession)
    throws SSLPeerUnverifiedException;
  
  protected abstract SSLSocketFactory buildSSLSocketFactory(Object paramObject);
  
  protected abstract SSLServerSocketFactory buildSSLServerSocketFactory(Object paramObject);
  
  protected abstract SSLSocket buildSocket(SSL paramSSL)
    throws IOException;
  
  protected abstract SSLSocket buildSocket(SSL paramSSL, String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2, int paramInt3)
    throws IOException;
  
  protected abstract Socket buildPlainSocket(SSL paramSSL, String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2, int paramInt3)
    throws IOException;
  
  protected abstract Socket connectSocket(Socket paramSocket, SocketFactory paramSocketFactory, String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2, int paramInt3, SSL paramSSL)
    throws IOException;
  
  protected abstract SSLServerSocket buildServerSocket(SSL paramSSL)
    throws IOException;
  
  protected abstract void wantClientAuth(Object paramObject, boolean paramBoolean);
  
  protected abstract void enabledProtocols(Object paramObject, String[] paramArrayOfString);
  
  protected abstract RuntimeException buildRuntimeException(Exception paramException);
  
  protected abstract Object initSSL(SSL paramSSL, TrustChain paramTrustChain, KeyMaterial paramKeyMaterial)
    throws NoSuchAlgorithmException, KeyStoreException, CertificateException, KeyManagementException, IOException;
  
  protected abstract void checkTrusted(Object paramObject, X509Certificate[] paramArrayOfX509Certificate, String paramString)
    throws CertificateException;
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.JavaImpl
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class KeyMaterial
  extends TrustMaterial
{
  private final Object keyManagerFactory;
  private final List aliases;
  private final List associatedChains;
  
  public KeyMaterial(InputStream jks, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(Util.streamToBytes(jks), password);
  }
  
  public KeyMaterial(InputStream jks, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(Util.streamToBytes(jks), jksPass, keyPass);
  }
  
  public KeyMaterial(InputStream jks, InputStream key, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(jks != null ? Util.streamToBytes(jks) : null, key != null ? Util.streamToBytes(key) : null, password);
  }
  
  public KeyMaterial(InputStream jks, InputStream key, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(jks != null ? Util.streamToBytes(jks) : null, key != null ? Util.streamToBytes(key) : null, jksPass, keyPass);
  }
  
  public KeyMaterial(String pathToJksFile, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(new File(pathToJksFile), password);
  }
  
  public KeyMaterial(String pathToJksFile, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(new File(pathToJksFile), jksPass, keyPass);
  }
  
  public KeyMaterial(String pathToCerts, String pathToKey, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(pathToCerts != null ? new File(pathToCerts) : null, pathToKey != null ? new File(pathToKey) : null, password);
  }
  
  public KeyMaterial(String pathToCerts, String pathToKey, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(pathToCerts != null ? new File(pathToCerts) : null, pathToKey != null ? new File(pathToKey) : null, jksPass, keyPass);
  }
  
  public KeyMaterial(File jksFile, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(new FileInputStream(jksFile), password);
  }
  
  public KeyMaterial(File jksFile, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(new FileInputStream(jksFile), jksPass, keyPass);
  }
  
  public KeyMaterial(File certsFile, File keyFile, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(certsFile != null ? new FileInputStream(certsFile) : null, keyFile != null ? new FileInputStream(keyFile) : null, password);
  }
  
  public KeyMaterial(File certsFile, File keyFile, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(certsFile != null ? new FileInputStream(certsFile) : null, keyFile != null ? new FileInputStream(keyFile) : null, jksPass, keyPass);
  }
  
  public KeyMaterial(URL urlToJKS, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(urlToJKS.openStream(), password);
  }
  
  public KeyMaterial(URL urlToJKS, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(urlToJKS.openStream(), jksPass, keyPass);
  }
  
  public KeyMaterial(URL urlToCerts, URL urlToKey, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(urlToCerts.openStream(), urlToKey.openStream(), password);
  }
  
  public KeyMaterial(URL urlToCerts, URL urlToKey, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(urlToCerts.openStream(), urlToKey.openStream(), jksPass, keyPass);
  }
  
  public KeyMaterial(byte[] jks, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(jks, (byte[])null, password);
  }
  
  public KeyMaterial(byte[] jks, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    this(jks, null, jksPass, keyPass);
  }
  
  public KeyMaterial(byte[] jksOrCerts, byte[] key, char[] password)
    throws GeneralSecurityException, IOException
  {
    this(jksOrCerts, key, password, password);
  }
  
  public KeyMaterial(byte[] jksOrCerts, byte[] key, char[] jksPass, char[] keyPass)
    throws GeneralSecurityException, IOException
  {
    super(KeyStoreBuilder.build(jksOrCerts, key, jksPass, keyPass), 0);
    KeyStore ks = getKeyStore();
    Enumeration en = ks.aliases();
    List myAliases = new LinkedList();
    List myChains = new LinkedList();
    while (en.hasMoreElements())
    {
      String alias = (String)en.nextElement();
      if (ks.isKeyEntry(alias))
      {
        X509Certificate[] c;
        try
        {
          ks.getKey(alias, keyPass);
          
          myAliases.add(alias);
          Certificate[] chain = ks.getCertificateChain(alias);
          if (chain != null)
          {
            X509Certificate[] c = Certificates.x509ifyChain(chain);
            if (c != null)
            {
              X509Certificate l = c[0];
              c = X509CertificateChainBuilder.buildPath(l, c);
            }
            myChains.add(c);
          }
          else
          {
            throw new KeyStoreException("Could not find KeyMaterial's associated certificate chain with alis=[" + alias + "]");
          }
        }
        catch (GeneralSecurityException gse) {}
      }
    }
    if (myAliases.isEmpty()) {
      throw new KeyStoreException("KeyMaterial provided does not contain any keys!");
    }
    aliases = Collections.unmodifiableList(myAliases);
    associatedChains = Collections.unmodifiableList(myChains);
    keyManagerFactory = JavaImpl.newKeyManagerFactory(ks, keyPass);
  }
  
  public Object[] getKeyManagers()
  {
    return JavaImpl.getKeyManagers(keyManagerFactory);
  }
  
  public List getAssociatedCertificateChains()
  {
    return associatedChains;
  }
  
  public KeyStore getKeyStore()
  {
    return super.getKeyStore();
  }
  
  public List getAliases()
  {
    return aliases;
  }
  
  public static void main(String[] args)
    throws Exception
  {
    if (args.length < 2)
    {
      System.out.println("Usage1:  java org.apache.commons.ssl.KeyMaterial [password] [pkcs12 or jks]");
      System.out.println("Usage2:  java org.apache.commons.ssl.KeyMaterial [password] [private-key] [cert-chain]");
      System.exit(1);
    }
    char[] jksPass = args[0].toCharArray();
    char[] keyPass = jksPass;
    String path1 = args[1];
    String path2 = null;
    if (args.length >= 3) {
      path2 = args[2];
    }
    if (args.length >= 4)
    {
      keyPass = args[3].toCharArray();
    }
    else if (path2 != null)
    {
      File f = new File(path2);
      if (!f.exists())
      {
        keyPass = path2.toCharArray();
        path2 = null;
      }
    }
    KeyMaterial km = new KeyMaterial(path1, path2, jksPass, keyPass);
    System.out.println(km);
  }
  
  public String toString()
  {
    List chains = getAssociatedCertificateChains();
    List aliases = getAliases();
    Iterator it = chains.iterator();
    Iterator aliasesIt = aliases.iterator();
    StringBuffer buf = new StringBuffer(8192);
    while (it.hasNext())
    {
      X509Certificate[] certs = (X509Certificate[])it.next();
      String alias = (String)aliasesIt.next();
      buf.append("Alias: ");
      buf.append(alias);
      buf.append('\n');
      if (certs != null) {
        for (int i = 0; i < certs.length; i++)
        {
          buf.append(Certificates.toString(certs[i]));
          try
          {
            buf.append(Certificates.toPEMString(certs[i]));
          }
          catch (CertificateEncodingException cee)
          {
            buf.append(cee.toString());
            buf.append('\n');
          }
        }
      }
    }
    return buf.toString();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.KeyMaterial
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class KeyStoreBuilder$BuildResult
{
  protected final List keys;
  protected final List chains;
  protected final KeyStore jks;
  
  protected KeyStoreBuilder$BuildResult(List keys, List chains, KeyStore jks)
  {
    if ((keys == null) || (keys.isEmpty())) {
      this.keys = null;
    } else {
      this.keys = Collections.unmodifiableList(keys);
    }
    this.jks = jks;
    List x509Chains = new LinkedList();
    if (chains != null)
    {
      Iterator it = chains.iterator();
      while (it.hasNext())
      {
        Certificate[] chain = (Certificate[])it.next();
        if ((chain != null) && (chain.length > 0))
        {
          int len = chain.length;
          X509Certificate[] x509 = new X509Certificate[len];
          for (int i = 0; i < x509.length; i++) {
            x509[i] = ((X509Certificate)chain[i]);
          }
          x509Chains.add(x509);
        }
      }
    }
    if ((x509Chains == null) || (x509Chains.isEmpty())) {
      this.chains = null;
    } else {
      this.chains = Collections.unmodifiableList(x509Chains);
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.KeyStoreBuilder.BuildResult
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.ssl.asn1.ASN1EncodableVector;
import org.apache.commons.ssl.asn1.DERInteger;
import org.apache.commons.ssl.asn1.DERSequence;

public class KeyStoreBuilder
{
  private static final String PKCS7_ENCRYPTED = "1.2.840.113549.1.7.6";
  
  public static KeyStore build(byte[] jksOrCerts, char[] password)
    throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, ProbablyBadPasswordException, UnrecoverableKeyException
  {
    return build(jksOrCerts, null, password);
  }
  
  public static KeyStore build(byte[] jksOrCerts, byte[] privateKey, char[] password)
    throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, ProbablyBadPasswordException, UnrecoverableKeyException
  {
    return build(jksOrCerts, privateKey, password, null);
  }
  
  public static KeyStore build(byte[] jksOrCerts, byte[] privateKey, char[] jksPassword, char[] keyPassword)
    throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, ProbablyBadPasswordException, UnrecoverableKeyException
  {
    if ((keyPassword == null) || (keyPassword.length <= 0)) {
      keyPassword = jksPassword;
    }
    BuildResult br1 = parse(jksOrCerts, jksPassword, keyPas
1 2 3 4 5 6 7 8 9 10 11 12 13

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd