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