Example usage for org.bouncycastle.asn1.x509 TBSCertificateStructure TBSCertificateStructure

List of usage examples for org.bouncycastle.asn1.x509 TBSCertificateStructure TBSCertificateStructure

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 TBSCertificateStructure TBSCertificateStructure.

Prototype

public TBSCertificateStructure(ASN1Sequence seq) 

Source Link

Usage

From source file:net.ripe.rpki.commons.crypto.x509cert.X509CertificateUtil.java

License:BSD License

/**
 * Get a base 64-encoded, DER-encoded X.509 subjectPublicKeyInfo as used for the Trust Anchor Locator (TAL)
 *
 * @throws X509CertificateOperationException
 *
 * @throws IOException/*from w  ww. ja  v a 2 s.co m*/
 */
public static String getEncodedSubjectPublicKeyInfo(X509Certificate certificate) {

    byte[] tbsCertificate;
    try {
        tbsCertificate = certificate.getTBSCertificate();
    } catch (CertificateEncodingException e) {
        throw new X509CertificateOperationException("Can't extract TBSCertificate from certificate", e);
    }
    ASN1Sequence tbsCertificateSequence = (ASN1Sequence) Asn1Util.decode(tbsCertificate);
    TBSCertificateStructure tbsCertificateStructure = new TBSCertificateStructure(tbsCertificateSequence);
    SubjectPublicKeyInfo subjectPublicKeyInfo = tbsCertificateStructure.getSubjectPublicKeyInfo();

    try {
        byte[] data = subjectPublicKeyInfo.getEncoded();
        Base64Encoder encoder = new Base64Encoder();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        encoder.encode(data, 0, data.length, out);
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new X509CertificateOperationException("Can't encode SubjectPublicKeyInfo for certificate", e);
    }
}