Example usage for org.bouncycastle.asn1 DERPrintableString getInstance

List of usage examples for org.bouncycastle.asn1 DERPrintableString getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERPrintableString getInstance.

Prototype

public static DERPrintableString getInstance(ASN1TaggedObject obj, boolean explicit) 

Source Link

Document

Return a Printable String from a tagged object.

Usage

From source file:ca.trustpoint.m2m.M2mCertificateFactory.java

License:Apache License

/**
 * Parses ASN.1 tagged object to construct an {@link EntityNameAttribute} object.
 *
 * @param obj ASN.1 tagged object for {@link EntityNameAttribute}.
 * @return An instance of {@link EntityNameAttribute} constructed from obj.
 * @throw IOException if parsing has error or unknown ID or no value.
 *///from   www  .jav a2s  .co m
private EntityNameAttribute parseEntityNameAttribute(ASN1TaggedObject obj) throws IOException {
    EntityNameAttributeId aid = EntityNameAttributeId.getInstance(obj.getTagNo());
    String value = null;

    switch (aid) {
    case Country:
    case DistinguishedNameQualifier:
    case SerialNumber:
        value = DERPrintableString.getInstance(obj, false).getString();
        break;
    case Organization:
    case OrganizationalUnit:
    case StateOrProvince:
    case Locality:
    case CommonName:
        value = DERUTF8String.getInstance(obj, false).getString();
        break;
    case DomainComponent:
        value = DERIA5String.getInstance(obj, false).getString();
        break;
    case RegisteredId:
        value = ASN1ObjectIdentifier.getInstance(obj, false).getId();
        break;
    case OctetsName:
        byte[] octets = ASN1OctetString.getInstance(obj, false).getOctets();
        value = Hex.toHexString(octets);
        break;
    default:
        throw new IOException("unknown entity name attribute id: " + aid.getIndexId());
    }

    if (value == null) {
        throw new IOException("null entity name attribute value for id: " + aid.getIndexId());
    }

    EntityNameAttribute attribute = new EntityNameAttribute();
    attribute.setId(aid);
    attribute.setValue(value);

    if (!attribute.isValid()) {
        throw new IOException("invalid entity name attribute value for id: " + aid.getIndexId());
    }

    return attribute;
}