01: package org.bouncycastle.jce;
02:
03: import java.io.*;
04: import java.security.cert.*;
05:
06: import org.bouncycastle.asn1.*;
07: import org.bouncycastle.asn1.x509.*;
08:
09: /**
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: * return the issuer of the given cert as an X509PrincipalObject.
19: */
20: public static X509Principal getIssuerX509Principal(
21: X509Certificate cert) throws CertificateEncodingException {
22: try {
23: ByteArrayInputStream bIn = new ByteArrayInputStream(cert
24: .getTBSCertificate());
25: ASN1InputStream aIn = new ASN1InputStream(bIn);
26: TBSCertificateStructure tbsCert = new TBSCertificateStructure(
27: (ASN1Sequence) aIn.readObject());
28:
29: return new X509Principal(tbsCert.getIssuer());
30: } catch (IOException e) {
31: throw new CertificateEncodingException(e.toString());
32: }
33: }
34:
35: /**
36: * return the subject of the given cert as an X509PrincipalObject.
37: */
38: public static X509Principal getSubjectX509Principal(
39: X509Certificate cert) throws CertificateEncodingException {
40: try {
41: ByteArrayInputStream bIn = new ByteArrayInputStream(cert
42: .getTBSCertificate());
43: ASN1InputStream aIn = new ASN1InputStream(bIn);
44: TBSCertificateStructure tbsCert = new TBSCertificateStructure(
45: (ASN1Sequence) aIn.readObject());
46:
47: return new X509Principal(tbsCert.getSubject());
48: } catch (IOException e) {
49: throw new CertificateEncodingException(e.toString());
50: }
51: }
52:
53: /**
54: * return the issuer of the given CRL as an X509PrincipalObject.
55: */
56: public static X509Principal getIssuerX509Principal(X509CRL crl)
57: throws CRLException {
58: try {
59: ByteArrayInputStream bIn = new ByteArrayInputStream(crl
60: .getTBSCertList());
61: ASN1InputStream aIn = new ASN1InputStream(bIn);
62: TBSCertList tbsCertList = new TBSCertList(
63: (ASN1Sequence) aIn.readObject());
64:
65: return new X509Principal(tbsCertList.getIssuer());
66: } catch (IOException e) {
67: throw new CRLException(e.toString());
68: }
69: }
70: }
|