package org.bouncycastle.jce;

import java.io.*;
import java.security.cert.*;

import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.x509.*;

/**
 * a utility class that will extract X509Principal objects from X.509 certificates.
 * <p>
 * Use this in preference to trying to recreate a principal from a String, not all
 * DNs are what they should be, so it's best to leave them encoded where they
 * can be.
 */
public class PrincipalUtil
{
    /**
     * return the issuer of the given cert as an X509PrincipalObject.
     */
    public static X509Principal getIssuerX509Principal(
        X509Certificate cert)
        throws CertificateEncodingException
    {
        try
        {
            ByteArrayInputStream    bIn = new ByteArrayInputStream(
                cert.getTBSCertificate());
            ASN1InputStream         aIn = new ASN1InputStream(bIn);
            TBSCertificateStructure tbsCert = new TBSCertificateStructure(
                                            (ASN1Sequence)aIn.readObject());

            return new X509Principal(tbsCert.getIssuer());
        }
        catch (IOException e)
        {
            throw new CertificateEncodingException(e.toString());
        }
    }

    /**
     * return the subject of the given cert as an X509PrincipalObject.
     */
    public static X509Principal getSubjectX509Principal(
        X509Certificate cert)
        throws CertificateEncodingException
    {
        try
        {
            ByteArrayInputStream    bIn = new ByteArrayInputStream(
                cert.getTBSCertificate());
            ASN1InputStream         aIn = new ASN1InputStream(bIn);
            TBSCertificateStructure tbsCert = new TBSCertificateStructure(
                                            (ASN1Sequence)aIn.readObject());

            return new X509Principal(tbsCert.getSubject());
        }
        catch (IOException e)
        {
            throw new CertificateEncodingException(e.toString());
        }
    }
    
    /**
     * return the issuer of the given CRL as an X509PrincipalObject.
     */
    public static X509Principal getIssuerX509Principal(
        X509CRL crl)
        throws CRLException
    {
        try
        {
            ByteArrayInputStream    bIn = new ByteArrayInputStream(
                crl.getTBSCertList());
            ASN1InputStream         aIn = new ASN1InputStream(bIn);
            TBSCertList tbsCertList = new TBSCertList(
                                            (ASN1Sequence)aIn.readObject());

            return new X509Principal(tbsCertList.getIssuer());
        }
        catch (IOException e)
        {
            throw new CRLException(e.toString());
        }
    }
}

@@ -690,187 +690,8 @@
   %7B%0A
-            ByteArrayInputStream    bIn = new ByteArrayInputStream(%0A                cert.getTBSCertificate());%0A            ASN1InputStream         aIn = new ASN1InputStream(bIn);%0A
     
@@ -723,36 +723,32 @@
 ucture tbsCert =
- new
  TBSCertificateS
@@ -751,33 +751,38 @@
 ateStructure
-(%0A       
+.getInstance(%0A
             
@@ -793,54 +793,57 @@
     
-                 (ASN1Sequence)aIn.readObject(
+ASN1Object.fromByteArray(cert.getTBSCertificate()
 ));%0A
@@ -1285,187 +1285,8 @@
   %7B%0A
-            ByteArrayInputStream    bIn = new ByteArrayInputStream(%0A                cert.getTBSCertificate());%0A            ASN1InputStream         aIn = new ASN1InputStream(bIn);%0A
     
@@ -1326,20 +1326,16 @@
 bsCert =
- new
  TBSCert
@@ -1350,26 +1350,30 @@
 tructure
-(%0A        
+.getInstance(%0A
         
@@ -1388,58 +1388,61 @@
     
-                (ASN1Sequence)aIn.readObject
+ASN1Object.fromByteArray(cert.getTBSCertificate
 ())
+)
 ;%0A
-%0A
     
@@ -1856,183 +1856,8 @@
   %7B%0A
-            ByteArrayInputStream    bIn = new ByteArrayInputStream(%0A                crl.getTBSCertList());%0A            ASN1InputStream         aIn = new ASN1InputStream(bIn);%0A
     
@@ -1889,20 +1889,16 @@
 rtList =
- new
  TBSCert
@@ -1901,28 +1901,30 @@
 CertList
-(%0A          
+.getInstance(%0A
         
@@ -1935,57 +1935,55 @@
     
-                  (ASN1Sequence)aIn.readObjec
+ASN1Object.fromByteArray(crl.getTBSCertLis
 t())
+)
 ;%0A%0A 

Refactor to use ASN1Object.fromByteArray