Home | History | Annotate | Download | only in jce
      1 package org.bouncycastle.jce;
      2 
      3 import java.io.*;
      4 import java.security.cert.*;
      5 
      6 import org.bouncycastle.asn1.*;
      7 import org.bouncycastle.asn1.x509.*;
      8 
      9 /**
     10  * a utility class that will extract X509Principal objects from X.509 certificates.
     11  * <p>
     12  * Use this in preference to trying to recreate a principal from a String, not all
     13  * DNs are what they should be, so it's best to leave them encoded where they
     14  * can be.
     15  */
     16 public class PrincipalUtil
     17 {
     18     /**
     19      * return the issuer of the given cert as an X509PrincipalObject.
     20      */
     21     public static X509Principal getIssuerX509Principal(
     22         X509Certificate cert)
     23         throws CertificateEncodingException
     24     {
     25         try
     26         {
     27             TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance(
     28                     ASN1Object.fromByteArray(cert.getTBSCertificate()));
     29 
     30             return new X509Principal(tbsCert.getIssuer());
     31         }
     32         catch (IOException e)
     33         {
     34             throw new CertificateEncodingException(e.toString());
     35         }
     36     }
     37 
     38     /**
     39      * return the subject of the given cert as an X509PrincipalObject.
     40      */
     41     public static X509Principal getSubjectX509Principal(
     42         X509Certificate cert)
     43         throws CertificateEncodingException
     44     {
     45         try
     46         {
     47             TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance(
     48                     ASN1Object.fromByteArray(cert.getTBSCertificate()));
     49             return new X509Principal(tbsCert.getSubject());
     50         }
     51         catch (IOException e)
     52         {
     53             throw new CertificateEncodingException(e.toString());
     54         }
     55     }
     56 
     57     /**
     58      * return the issuer of the given CRL as an X509PrincipalObject.
     59      */
     60     public static X509Principal getIssuerX509Principal(
     61         X509CRL crl)
     62         throws CRLException
     63     {
     64         try
     65         {
     66             TBSCertList tbsCertList = TBSCertList.getInstance(
     67                 ASN1Object.fromByteArray(crl.getTBSCertList()));
     68 
     69             return new X509Principal(tbsCertList.getIssuer());
     70         }
     71         catch (IOException e)
     72         {
     73             throw new CRLException(e.toString());
     74         }
     75     }
     76 }
     77