Example usage for org.bouncycastle.asn1.x509 X509CertificateStructure getIssuer

List of usage examples for org.bouncycastle.asn1.x509 X509CertificateStructure getIssuer

Introduction

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

Prototype

public X500Name getIssuer() 

Source Link

Usage

From source file:com.yacme.ext.oxsit.Helpers.java

License:Open Source License

public static String getIssuerName(X509Certificate _Cert) {
    //convert to bouncycaste
    String sRet = "";

    ByteArrayInputStream as;//from  w ww .  ja v  a  2 s  .  co  m
    try {
        as = new ByteArrayInputStream(_Cert.getEncoded());
        ASN1InputStream aderin = new ASN1InputStream(as);
        DERObject ado;
        ado = aderin.readObject();
        X509CertificateStructure _aX509 = new X509CertificateStructure((ASN1Sequence) ado);
        //extract the name, same as in display         
        X509Name aName = _aX509.getIssuer();
        Vector<DERObjectIdentifier> oidv = aName.getOIDs();
        HashMap<DERObjectIdentifier, String> hm = new HashMap<DERObjectIdentifier, String>(20);
        Vector<?> values = aName.getValues();
        for (int i = 0; i < oidv.size(); i++) {
            hm.put(oidv.elementAt(i), values.elementAt(i).toString());
        }
        //look for givename (=nome di battesimo)
        //see BC source code for details about DefaultLookUp behaviour
        DERObjectIdentifier oix;
        if (sRet.length() == 0) {
            //check for O
            oix = (DERObjectIdentifier) (X509Name.DefaultLookUp.get("o"));
            if (hm.containsKey(oix)) {
                sRet = hm.get(oix).toString();
            }
        }
        if (sRet.length() == 0) {
            //check for CN
            oix = (DERObjectIdentifier) (X509Name.DefaultLookUp.get("cn"));
            if (hm.containsKey(oix)) {
                sRet = hm.get(oix).toString();
            }
        }
        if (sRet.length() == 0) {
            //if still not, check for pseudodym
            oix = (DERObjectIdentifier) (X509Name.DefaultLookUp.get("pseudonym"));
            if (hm.containsKey(oix))
                sRet = hm.get(oix).toString();
        }
        //check for CN
        oix = (DERObjectIdentifier) (X509Name.DefaultLookUp.get("cn"));
        if (hm.containsKey(oix)) {
            sRet = sRet + ((sRet.length() > 0) ? ", " : "") + hm.get(oix).toString();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CertificateEncodingException e) {
        e.printStackTrace();
    }
    return sRet;
}