Example usage for org.bouncycastle.asn1 DERGeneralizedTime getDate

List of usage examples for org.bouncycastle.asn1 DERGeneralizedTime getDate

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERGeneralizedTime getDate.

Prototype

public Date getDate() throws ParseException 

Source Link

Usage

From source file:org.ejbca.util.cert.SubjectDirAttrExtension.java

License:Open Source License

/**
* SubjectDirectoryAttributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
*
* Attribute ::= SEQUENCE {//from   www  .j  ava 2 s .c  o m
 *  type AttributeType,
 *  values SET OF AttributeValue }
 *  -- at least one value is required
 * 
 * AttributeType ::= OBJECT IDENTIFIER
 * AttributeValue ::= ANY
 * 
* SubjectDirectoryAttributes is of form 
* dateOfBirth=<19590927>, placeOfBirth=<string>, gender=<M/F>, countryOfCitizenship=<two letter ISO3166>, countryOfResidence=<two letter ISO3166>
 * 
 * Supported subjectDirectoryAttributes are the ones above 
*
* @param certificate containing subject directory attributes
* @return String containing directoryAttributes of form the form specified above or null if no directoryAttributes exist. 
*   Values in returned String is from CertTools constants. 
*   DirectoryAttributes not supported are simply not shown in the resulting string.  
* @throws java.lang.Exception
*/
public static String getSubjectDirectoryAttributes(Certificate certificate) throws Exception {
    log.debug("Search for SubjectAltName");
    String result = "";
    if (certificate instanceof X509Certificate) {
        X509Certificate x509cert = (X509Certificate) certificate;
        DERObject obj = CertTools.getExtensionValue(x509cert,
                X509Extensions.SubjectDirectoryAttributes.getId());
        if (obj == null) {
            return null;
        }
        ASN1Sequence seq = (ASN1Sequence) obj;

        String prefix = "";
        FastDateFormat dateF = FastDateFormat.getInstance("yyyyMMdd");
        for (int i = 0; i < seq.size(); i++) {
            Attribute attr = Attribute.getInstance(seq.getObjectAt(i));
            if (!StringUtils.isEmpty(result)) {
                prefix = ", ";
            }
            if (attr.getAttrType().getId().equals(id_pda_dateOfBirth)) {
                ASN1Set set = attr.getAttrValues();
                // Come on, we'll only allow one dateOfBirth, we're not allowing such frauds with multiple birth dates
                DERGeneralizedTime time = DERGeneralizedTime.getInstance(set.getObjectAt(0));
                Date date = time.getDate();
                String dateStr = dateF.format(date);
                result += prefix + "dateOfBirth=" + dateStr;
            }
            if (attr.getAttrType().getId().equals(id_pda_placeOfBirth)) {
                ASN1Set set = attr.getAttrValues();
                // same here only one placeOfBirth
                String pb = ((DERString) set.getObjectAt(0)).getString();
                result += prefix + "placeOfBirth=" + pb;
            }
            if (attr.getAttrType().getId().equals(id_pda_gender)) {
                ASN1Set set = attr.getAttrValues();
                // same here only one gender
                String g = ((DERString) set.getObjectAt(0)).getString();
                result += prefix + "gender=" + g;
            }
            if (attr.getAttrType().getId().equals(id_pda_countryOfCitizenship)) {
                ASN1Set set = attr.getAttrValues();
                // same here only one citizenship
                String g = ((DERString) set.getObjectAt(0)).getString();
                result += prefix + "countryOfCitizenship=" + g;
            }
            if (attr.getAttrType().getId().equals(id_pda_countryOfResidence)) {
                ASN1Set set = attr.getAttrValues();
                // same here only one residence
                String g = ((DERString) set.getObjectAt(0)).getString();
                result += prefix + "countryOfResidence=" + g;
            }
        }
    }
    if (StringUtils.isEmpty(result)) {
        return null;
    }
    return result;
}