FileDocCategorySizeDatePackage
TrustStoreTrustManager.javaAPI DocGlassfish v2 API13609Fri May 04 22:30:32 BST 2007com.sun.appserv.management.client

TrustStoreTrustManager

public class TrustStoreTrustManager extends Object implements X509TrustManager
This X509TrustManager implementation supports a trust-store file and allows adding new certificates to it. It is designed to allow a subclass to override a variety of protected methods including those of TrustManager:
  • checkClientTrusted
  • checkServerTrusted
  • getAcceptedIssuers
as well as:
  • #checkCertificate
  • #getTrustStorePassword
  • #shouldAddToTrustStore
  • #askShouldAddToTrustStore
  • #getCertificateAlias
  • #addCertificateToTrustStore
  • #writeStore
  • #certificateNotInTrustStore
  • #getTrustStore

For convenience, if setPrompt( true ) is called, then when a new Certificate is encountered, askShouldAddToTrustStore( c ) prompts the user via System.in as to whether to accept this new Certificate as trusted. Subclasses can of course override this behavior any any desired way.

Fields Summary
private final File
mTrustStoreFile
private final char[]
mTrustStorePassword
private final String
mKeyStoreType
private KeyStore
mTrustStore
private boolean
mPrompt
public static final String
TRUSTSTORE_FILE_SPROP
Standard system property denoting the trust-store.
public static final String
TRUSTSTORE_PASSWORD_SPROP
Standard system property denoting the trust-store password.
Constructors Summary
public TrustStoreTrustManager(File trustStoreFile, String keyStoreType, char[] trustStorePassword)
Create a new instance with the specified File and password The trustStoreFile must exist.

param
trustStoreFile (not required to exist)
param
keyStoreType keystore (truststore) type, eg "JKS"
param
trustStorePassword (may be null)

		if ( trustStoreFile == null || keyStoreType == null )
		{
			throw new IllegalArgumentException();
		}
		
		mTrustStoreFile		= trustStoreFile;
		mKeyStoreType		= keyStoreType;
		mTrustStorePassword	= trustStorePassword;
		mTrustStore			= null;
		mPrompt				= false;
		
		try
		{
			getTrustStore();	// force initialization now
		}
		catch( Exception e )
		{
			throw new RuntimeException( e );
		}
	
public TrustStoreTrustManager(File trustStoreFile, char[] trustStorePassword)
calls this( trustStoreFile,"JKS", trustStorePassword )

		this( trustStoreFile, "JKS", trustStorePassword );
	
Methods Summary
protected voidaddCertificateToTrustStore(java.lang.String alias, java.security.cert.Certificate c)
Add the Certificate with the specified alias to the trust-store.

param
alias
param
c

        mTrustStore.setCertificateEntry( alias, c );
       	writeStore();
	
protected voidaddCertificateToTrustStore(java.security.cert.Certificate c)
Add the Certificate to the trust-store, using the alias returned by getCertificateAlias( c ).

param
c

        final String aliasName = getCertificateAlias( c );
        
        addCertificateToTrustStore( aliasName, c );
	
protected booleanaskShouldAddToTrustStore(java.security.cert.Certificate c)
Prompts via System.in to ask whether the Certificate should be added.

param
c
return
true if the response is yes.

		final LineReaderImpl	reader	= new LineReaderImpl( System.in );
		
		final String prompt	= c.toString() + 
			"\n\nAdd the above certificate to the truststore [y/n]?";
			
		final String result	= reader.readLine( prompt );
		
		return( result.equalsIgnoreCase( "y" ) || result.equalsIgnoreCase( "yes" ) );
	
protected voidcertificateNotInTrustStore(java.security.cert.Certificate c)
The Certificate is not found in the trust-store. If shouldAddToTrustStore( c ) returns false, then a CertificateException is thrown. Otherwise, addCertificateToTrustStore( c ) is called.

param
c

        if ( shouldAddToTrustStore( c ) )
        {
        	addCertificateToTrustStore( c );
        }
        else
        {
            throw new CertificateException( "Certificate not trusted:\n" + c );
        }
	
protected voidcheckCertificate(java.security.cert.X509Certificate[] chain)

param
chain
throws
RuntimeException
throws
CertificateException

		try
		{
            //First ensure that the certificate is valid.
            for (int i = 0 ; i < chain.length ; i ++)
            {
                chain[i].checkValidity();   
            }
            
            mTrustStore	= getTrustStore();
            
            final Certificate	cert	= chain[ 0 ];
            
            //if the certificate already exists in the truststore, it is implicitly trusted
            if ( mTrustStore.getCertificateAlias( cert ) == null )
            {
            	certificateNotInTrustStore( cert );
            }
        }
        catch (CertificateException e)
        {
            throw e;
        }
        catch (Exception e)
        {        
			throw new RuntimeException( e );
		}
	
public voidcheckClientTrusted(java.security.cert.X509Certificate[] chain, java.lang.String authType)

		throw new UnsupportedOperationException( "checkClientTrusted() not supported" );
	
public voidcheckServerTrusted(java.security.cert.X509Certificate[] chain, java.lang.String authType)

		if (chain == null || chain.length == 0)
		{
			throw new IllegalArgumentException();
        }
        
		checkCertificate(chain);
	
private voidcreateTrustStoreFile(java.security.KeyStore keyStore, char[] pw, java.io.File f)

		f.createNewFile();
		writeStore( keyStore, pw, f );
	
public java.security.cert.X509Certificate[]getAcceptedIssuers()
By default, no issuers are trusted. It is better to trust specific Certificates explicitly.

return
X509Certificate[]

		// none, by default
		return( new X509Certificate[ 0 ] );
	
protected java.lang.StringgetCertificateAlias(java.security.cert.Certificate c)
Return an alias for a Certificate to be added to the TrustStore.

param
c
return
an alias to be used for adding the Certificate to the trust-store

        final DateFormat f = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);     
        
		return( "cert" +  f.format( new Date() ) );
	
public static com.sun.appserv.management.client.TrustStoreTrustManagergetSystemInstance()
Create an instance using the system trust-store as returned by getSystemTrustStoreFile().

return
an instance or null if not possible

    	final File		trustStore			= getSystemTrustStoreFile();
    	final char[]	trustStorePassword	= getSystemTrustStorePassword();
    	
    	TrustStoreTrustManager	mgr	= null;
    	
    	if ( trustStore != null && trustStorePassword != null )
    	{
    		return( new TrustStoreTrustManager( trustStore, trustStorePassword ) );
    	}
    	
    	return( mgr );
	
public static java.io.FilegetSystemTrustStoreFile()
Use System.getProperty( "javax.net.ssl.trustStore" ) to find a trust-store.

	
			       	 
		  
	
	
		final String	prop	= System.getProperty( TRUSTSTORE_FILE_SPROP );
		final File trustStore	= prop == null ? null : new File( prop );
		return( trustStore );
	
public static char[]getSystemTrustStorePassword()
Use System.getProperty( "javax.net.ssl.trustStorePassword" ) to find the trust-store password.

		return( toCharArray( System.getProperty( TRUSTSTORE_PASSWORD_SPROP ) ) );
	
protected synchronized java.security.KeyStoregetTrustStore()
Get the KeyStore containing the Certificates to be trusted. This should be a KeyStore corresponding to the file that was specified. The same KeyStore should be returned each time.

return
KeyStore

		if ( mTrustStore == null )
		{
			mTrustStore	= KeyStore.getInstance( mKeyStoreType );
			final File	f	= getTrustStoreFile();
			final char[]	pw	= getTrustStorePassword();
			if ( (! f.exists()) || f.length() == 0 )
			{
				f.delete();
				mTrustStore.load( null, pw );
				createTrustStoreFile( mTrustStore, pw, f);
			}
			else
			{
				final FileInputStream is	= new FileInputStream( f );
				try
				{
					mTrustStore.load( is, pw );
				}
				finally
				{
					is.close();
				}
			}
		}
		
		return( mTrustStore );
	
public final java.io.FilegetTrustStoreFile()
Return the trust-store that was initially passed in.

return
File

		return( mTrustStoreFile );
	
protected char[]getTrustStorePassword()
Subclass may choose to override this method to get the password from any desired source. Otherwise, the password used to create this instance is returned.

return
char[]

		return( mTrustStorePassword );
	
public voidsetPrompt(boolean prompt)
If set to true, then when a new Certificate is encountered, the user will be prompted via System.in as to whether it should be trusted.

param
prompt

		mPrompt	= prompt;
	
protected booleanshouldAddToTrustStore(java.security.cert.Certificate c)
Subclass may wish to override this routine and call defaultShouldAddToTrustStore( c );

param
c
return
true if the Certificate should be trusted and added to the trust-store

		return( mPrompt ? askShouldAddToTrustStore( c ) : false );
	
private static char[]toCharArray(java.lang.String s)

		return( s == null ? null : s.toCharArray() );
	
public java.lang.StringtoString()

		return( "TrustStoreTrustManager--trusts certificates found in truststore: " + mTrustStore );
	
private voidwriteStore(java.security.KeyStore trustStore, char[] trustStorePassword, java.io.File f)

	
		FileOutputStream	out	= new FileOutputStream( f );
    	
		try
		{
			trustStore.store( out, trustStorePassword );
		}
		catch( Throwable t )
		{
			t.printStackTrace();
		}
		finally
		{
			out.close();
		}
    
protected voidwriteStore()
Write the store to disk. Results are undefined if an error occurs while writing the file.

	
		writeStore( getTrustStore(), getTrustStorePassword(), getTrustStoreFile() );
    	// NOTE: any exception thrown from here is squelched by calling JDK code
    	// if in the middle of a SSL negotiation