JCas
v0.3.1

cas.io
Class SSLTools

java.lang.Object
  extended bycas.io.SSLTools

public class SSLTools
extends Object

Useful methods for working with SSL sockets and streams.

Version:
1.0
Author:
Ralph Schuster

Field Summary
protected static SSLSocketFactory factory
          The SSL socket factory for all trusted certs.
protected static SSLServerSocketFactory serverFactory
          The SSL server socket factory.
 
Constructor Summary
SSLTools()
           
 
Method Summary
static KeyStore createKeyStore(char[] passwd)
          Returns a new and empty KeyStore.
static SSLServerSocketFactory getServerSocketFactory(KeyManagerFactory kmf)
          Returns the SSL server socket factory based on the given KeyManagerFactory.
static SSLServerSocketFactory getServerSocketFactory(KeyStore ks, char[] passwd)
          Returns the SSL server socket factory based on the given KeyStore.
static SSLServerSocketFactory getServerSocketFactory(SSLContext sslContext)
          Returns the SSL server socket factory based on the given SSLContext.
static SSLSocketFactory getSocketFactory()
          Returns the SSL socket factory.
static Certificate loadCertificate(InputStream in)
          Returns the certificate in the stream.
static Certificate[] loadCertificates(InputStream in)
          Returns the certificates in the stream.
static KeyStore loadKeyStore(File file, char[] passwd)
          Loads a KeyStore from a file.
static KeyStore loadKeyStore(InputStream in, char[] passwd)
          Loads a KeyStore from an input stream.
static KeyStore loadKeyStore(String file, char[] passwd)
          Loads a KeyStore from a file.
static void main(String[] args)
          For test use only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected static SSLSocketFactory factory
The SSL socket factory for all trusted certs.


serverFactory

protected static SSLServerSocketFactory serverFactory
The SSL server socket factory.

Constructor Detail

SSLTools

public SSLTools()
Method Detail

getSocketFactory

public static SSLSocketFactory getSocketFactory()
Returns the SSL socket factory.


loadCertificate

public static Certificate loadCertificate(InputStream in)
                                   throws IOException,
                                          CertificateException
Returns the certificate in the stream.

Throws:
IOException
CertificateException

loadCertificates

public static Certificate[] loadCertificates(InputStream in)
                                      throws CertificateException
Returns the certificates in the stream.

Throws:
CertificateException

loadKeyStore

public static KeyStore loadKeyStore(String file,
                                    char[] passwd)
                             throws IOException,
                                    GeneralSecurityException
Loads a KeyStore from a file.

Throws:
IOException
GeneralSecurityException

loadKeyStore

public static KeyStore loadKeyStore(File file,
                                    char[] passwd)
                             throws IOException,
                                    GeneralSecurityException
Loads a KeyStore from a file.

Throws:
IOException
GeneralSecurityException

loadKeyStore

public static KeyStore loadKeyStore(InputStream in,
                                    char[] passwd)
                             throws GeneralSecurityException,
                                    IOException
Loads a KeyStore from an input stream.

Throws:
GeneralSecurityException
IOException

createKeyStore

public static KeyStore createKeyStore(char[] passwd)
                               throws GeneralSecurityException,
                                      IOException
Returns a new and empty KeyStore.

Throws:
GeneralSecurityException
IOException

getServerSocketFactory

public static SSLServerSocketFactory getServerSocketFactory(KeyStore ks,
                                                            char[] passwd)
                                                     throws GeneralSecurityException
Returns the SSL server socket factory based on the given KeyStore. If you need to import a certificate, use the following code. char passwd[] = "passwd".toCharArray(); // password of the keystore char keypass[] = "passwd".toCharArray(); // password of the private key String alias = "localkey"; // alias for the keystore entry to create String keyfile = "your.key.der"; // see notice below String certfile = "your.crt"; // Read authentication keys. These are used to authenticate // ourselves to the client java.security.KeyStore ks = createKeyStore(passwd); // Import certificate and key rfc.io.ImportKey.importKey(ks, keyfile, certfile, keypass, alias); You need to transform the PEM coded private key to a PKCS#8 format, e.g. with OpenSSL openssl pkcs8 -topk8 -nocrypt -in your.key -out your.key.der -outform der

Throws:
GeneralSecurityException

getServerSocketFactory

public static SSLServerSocketFactory getServerSocketFactory(KeyManagerFactory kmf)
                                                     throws GeneralSecurityException
Returns the SSL server socket factory based on the given KeyManagerFactory.

Throws:
GeneralSecurityException

getServerSocketFactory

public static SSLServerSocketFactory getServerSocketFactory(SSLContext sslContext)
Returns the SSL server socket factory based on the given SSLContext.


main

public static void main(String[] args)
For test use only.


JCas
v0.3.1