FileDocCategorySizeDatePackage
PKIXCertPath.javaAPI DocAzureus 3.0.3.46713Fri Oct 01 08:54:58 BST 2004org.bouncycastle.jce.provider

PKIXCertPath

public class PKIXCertPath extends CertPath
CertPath implementation for X.509 certificates.
TODO: add PKCS #7 encoding support

Fields Summary
static final List
certPathEncodings
private List
certificates
Constructors Summary
PKIXCertPath(List certificates)
Creates a CertPath of the specified type. This constructor is protected because most users should use a CertificateFactory to create CertPaths.

param
type the standard name of the type of Certificatesin this path

	super("X.509");
	this.certificates = new ArrayList( certificates );
    
PKIXCertPath(InputStream inStream, String encoding)
Creates a CertPath of the specified type. This constructor is protected because most users should use a CertificateFactory to create CertPaths. TODO: implement PKCS7 decoding

param
type the standard name of the type of Certificatesin this path

	super("X.509");
	try {
	    if ( encoding.equals( "PkiPath" ) )
	    {
		DERInputStream derInStream = new DERInputStream(inStream);
		DERObject derObject = derInStream.readObject();
		if ( derObject == null || ! ( derObject instanceof ASN1Sequence ) )
		{
		    throw new CertificateException("input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath" );
		}
		Enumeration enumx = ((ASN1Sequence)derObject).getObjects();
		InputStream certInStream;
		ByteArrayOutputStream outStream;
		DEROutputStream derOutStream;
		certificates = new ArrayList();
		CertificateFactory certFactory= CertificateFactory.getInstance( "X.509", "BC" );
		while ( enumx.hasMoreElements() ) {
		    outStream = new ByteArrayOutputStream();
		    derOutStream = new DEROutputStream(outStream);
    
        	    derOutStream.writeObject(enumx.nextElement());
        	    derOutStream.close();

		    certInStream = new ByteArrayInputStream(outStream.toByteArray());
		    certificates.add(0,certFactory.generateCertificate(certInStream));
		}
	    }
	    else
	    {
		throw new CertificateException( "unsupported encoding" );
	    }
	} catch ( IOException ex ) {
	    throw new CertificateException( "IOException throw while decoding CertPath:\n" + ex.toString() ); 
	} catch ( NoSuchProviderException ex ) {
	    throw new CertificateException( "BouncyCastle provider not found while trying to get a CertificateFactory:\n" + ex.toString() ); 
	}
    
Methods Summary
public java.util.ListgetCertificates()
Returns the list of certificates in this certification path. The List returned must be immutable and thread-safe. TODO: return immutable List

return
an immutable List of Certificates (may be empty, but not null)

	return new ArrayList( certificates );
    
public byte[]getEncoded()
Returns the encoded form of this certification path, using the default encoding.

return
the encoded bytes
exception
CertificateEncodingException if an encoding error occurs

	Iterator iter = getEncodings();
	if ( iter.hasNext() )
	{
	    Object enc = iter.next();
	    if ( enc instanceof String )
	    {
		return getEncoded((String)enc);
	    }
	}
	return null;
    
public byte[]getEncoded(java.lang.String encoding)
Returns the encoded form of this certification path, using the specified encoding. TODO: implement PKCS7 decoding

param
encoding the name of the encoding to use
return
the encoded bytes
exception
CertificateEncodingException if an encoding error occurs or the encoding requested is not supported

	DERObject encoded = null;
	if ( encoding.equals("PkiPath") )
	{
        ASN1EncodableVector v = new ASN1EncodableVector();

		// TODO check ListIterator  implementation for JDK 1.1
	    ListIterator iter = certificates.listIterator(certificates.size());
	    while ( iter.hasPrevious() )
	    {
		    v.add(getEncodedX509Certificate((X509Certificate)iter.previous()));
	    }

        encoded = new DERSequence(v);
	}
	else
	    throw new CertificateEncodingException( "unsupported encoding" );

	if ( encoded == null )
	    return null;

	ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        DEROutputStream derOutStream = new DEROutputStream(outStream);

	try {
	    derOutStream.writeObject( encoded );
	    derOutStream.close();
	} catch ( IOException ex ) {
	    throw new CertificateEncodingException( "IOExeption thrown: " + ex.toString() );
	}

        return outStream.toByteArray();
    
private org.bouncycastle.asn1.DERObjectgetEncodedX509Certificate(java.security.cert.X509Certificate cert)
Return a DERObject containing the encoded certificate.

param
cert the X509Certificate object to be encoded
return
the DERObject

	try {
	    ByteArrayInputStream inStream = new ByteArrayInputStream( cert.getEncoded() );
	    DERInputStream derInStream = new DERInputStream( inStream );
	    return derInStream.readObject();
	} catch ( IOException ex ) {
	    throw new CertificateEncodingException( "IOException caught while encoding certificate\n" + ex.toString() );
	}
    
public java.util.IteratorgetEncodings()
Returns an iteration of the encodings supported by this certification path, with the default encoding first. Attempts to modify the returned Iterator via its remove method result in an UnsupportedOperationException.

return
an Iterator over the names of the supported encodings (as Strings)

	return certPathEncodings.iterator();