Search in sources :

Example 91 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project keystore-explorer by kaikramer.

the class X509Ext method getSubjectDirectoryAttributesStringValue.

private static String getSubjectDirectoryAttributesStringValue(byte[] value) throws IOException {
    // @formatter:off
    /*
		 * SubjectDirectoryAttributes ::= ASN1Sequence SIZE (1..MAX) OF Attribute
		 *
		 * Attribute ::= ASN1Sequence
		 * {
		 *      type AttributeType,
		 *      values SET OF AttributeValue
		 * }
		 *
		 * RFC 3739: "Compliant implementations SHALL be able to interpret the following attributes:"
		 *    DateOfBirth (1.3.6.1.5.5.7.9.1) ::= GeneralizedTime
		 *    PlaceOfBirth (1.3.6.1.5.5.7.9.2) ::= DirectoryString
		 *    Gender (1.3.6.1.5.5.7.9.3) ::= PrintableString (SIZE(1)) -- "M", "F", "m" or "f"
		 *    CountryOfCitizenship (1.3.6.1.5.5.7.9.4) ::= PrintableString (SIZE (2)) -- ISO 3166 Country Code
		 *    CountryOfResidence (1.3.6.1.5.5.7.9.5) ::= PrintableString (SIZE (2)) -- ISO 3166 Country Code
		 */
    // @formatter:on
    StringBuilder sb = new StringBuilder();
    SubjectDirectoryAttributes subjectDirectoryAttributes = SubjectDirectoryAttributes.getInstance(value);
    for (Object attribute : subjectDirectoryAttributes.getAttributes()) {
        ASN1ObjectIdentifier attributeType = ((Attribute) attribute).getAttrType();
        AttributeTypeType att = AttributeTypeType.resolveOid(attributeType.getId());
        String attributeTypeStr = (att == AttributeTypeType.UNKNOWN) ? attributeType.getId() : att.friendly();
        ASN1Encodable[] attributeValues = ((Attribute) attribute).getAttributeValues();
        for (ASN1Encodable attributeValue : attributeValues) {
            String attributeValueStr = getAttributeValueString(attributeType, attributeValue);
            sb.append(MessageFormat.format("{0}: {1}", attributeTypeStr, attributeValueStr));
            sb.append(NEWLINE);
        }
    }
    return sb.toString();
}
Also used : Attribute(org.bouncycastle.asn1.x509.Attribute) SubjectDirectoryAttributes(org.bouncycastle.asn1.x509.SubjectDirectoryAttributes) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERGeneralString(org.bouncycastle.asn1.DERGeneralString) ASN1IA5String(org.bouncycastle.asn1.ASN1IA5String) DirectoryString(org.bouncycastle.asn1.x500.DirectoryString) ASN1BitString(org.bouncycastle.asn1.ASN1BitString) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1BMPString(org.bouncycastle.asn1.ASN1BMPString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ASN1PrintableString(org.bouncycastle.asn1.ASN1PrintableString) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 92 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project keystore-explorer by kaikramer.

the class GeneralNameUtil method parseUPN.

/**
 * Parse UPN/otherName
 *
 * @param generalName otherName object
 * @return UPN as string
 */
public static String parseUPN(GeneralName generalName) {
    // OtherName ::= SEQUENCE {
    // type-id OBJECT IDENTIFIER,
    // value [0] EXPLICIT ANY DEFINED BY type-id }
    ASN1Sequence otherName = (ASN1Sequence) generalName.getName();
    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) otherName.getObjectAt(0);
    if (UPN_OID.equals(oid.getId())) {
        ASN1TaggedObject asn1TaggedObject = (ASN1TaggedObject) otherName.getObjectAt(1);
        ASN1UTF8String upn = ASN1UTF8String.getInstance(asn1TaggedObject.getTagClass());
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), "UPN", upn.getString());
    }
    // fallback to generic handling
    ASN1Encodable value = otherName.getObjectAt(1);
    try {
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), ObjectIdUtil.toString(oid), HexUtil.getHexString(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
    } catch (IOException e) {
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), ObjectIdUtil.toString(oid), "");
    }
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1UTF8String(org.bouncycastle.asn1.ASN1UTF8String) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 93 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project keystore-explorer by kaikramer.

the class GeneralNameUtil method toString.

/**
 * Get string representation for all General Names.
 *
 * @param generalName General name
 * @return String representation of general name
 * @throws IOException If general name is invalid
 */
public static String toString(GeneralName generalName) throws IOException {
    if (generalName == null) {
        return "";
    }
    switch(generalName.getTagNo()) {
        case GeneralName.ediPartyName:
            /* EDIPartyName ::= SEQUENCE {
             *      nameAssigner            [0]     DirectoryString OPTIONAL,
             *      partyName               [1]     DirectoryString }
             */
            ASN1Sequence ediPartyName = (ASN1Sequence) generalName.getName();
            DirectoryString nameAssigner = DirectoryString.getInstance(ediPartyName.getObjectAt(0));
            DirectoryString partyName = DirectoryString.getInstance(ediPartyName.getObjectAt(1));
            String nameAssignerStr = null;
            if (nameAssigner != null) {
                // Optional
                nameAssignerStr = nameAssigner.getString();
            }
            String partyNameStr = partyName.getString();
            if (nameAssignerStr != null) {
                return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralName"), nameAssignerStr, partyNameStr);
            } else {
                return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralNameNoAssigner"), partyNameStr);
            }
        case GeneralName.otherName:
            return parseUPN(generalName);
        case GeneralName.x400Address:
            /*
             * No support for this at the moment - just get a hex dump
             * The Oracle CertificateFactory blows up if a certificate extension contains this anyway
             */
            ASN1Encodable x400Address = generalName.getName();
            return MessageFormat.format(res.getString("GeneralNameUtil.X400AddressGeneralName"), HexUtil.getHexString(x400Address.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
        default:
            return safeToString(generalName, true);
    }
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) DirectoryString(org.bouncycastle.asn1.x500.DirectoryString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ASN1UTF8String(org.bouncycastle.asn1.ASN1UTF8String) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DirectoryString(org.bouncycastle.asn1.x500.DirectoryString) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 94 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project keystore-explorer by kaikramer.

the class PolicyInformationUtil method toString.

/**
 * Get string representation of policy qualifier info.
 *
 * @param policyQualifierInfo Policy qualifier info
 * @return String representation of policy qualifier info
 * @throws IOException If policy qualifier info is invalid
 */
public static String toString(PolicyQualifierInfo policyQualifierInfo) throws IOException {
    StringBuilder sbPolicyQualifier = new StringBuilder();
    ASN1ObjectIdentifier policyQualifierId = policyQualifierInfo.getPolicyQualifierId();
    CertificatePolicyQualifierType certificatePolicyQualifierType = CertificatePolicyQualifierType.resolveOid(policyQualifierId.getId());
    if (certificatePolicyQualifierType == PKIX_CPS_POINTER_QUALIFIER) {
        DERIA5String cpsPointer = ((DERIA5String) policyQualifierInfo.getQualifier());
        sbPolicyQualifier.append(MessageFormat.format(res.getString("PolicyInformationUtil.CpsPointer"), cpsPointer));
    } else if (certificatePolicyQualifierType == PKIX_USER_NOTICE_QUALIFIER) {
        ASN1Encodable userNoticeObj = policyQualifierInfo.getQualifier();
        UserNotice userNotice = UserNotice.getInstance(userNoticeObj);
        sbPolicyQualifier.append(MessageFormat.format(res.getString("PolicyInformationUtil.UserNotice"), toString(userNotice)));
    }
    return sbPolicyQualifier.toString();
}
Also used : DERIA5String(org.bouncycastle.asn1.DERIA5String) UserNotice(org.bouncycastle.asn1.x509.UserNotice) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 95 with ASN1Encodable

use of com.github.zhenwei.core.asn1.ASN1Encodable in project jmulticard by ctt-gob-es.

the class X509Name method equals.

/**
 * @param inOrder if true the order of both X509 names must be the same,
 * as well as the values associated with each element.
 */
public boolean equals(Object obj, boolean inOrder) {
    if (!inOrder) {
        return this.equals(obj);
    }
    if (obj == this) {
        return true;
    }
    if (!(obj instanceof X509Name || obj instanceof ASN1Sequence)) {
        return false;
    }
    ASN1Primitive derO = ((ASN1Encodable) obj).toASN1Primitive();
    if (this.toASN1Primitive().equals(derO)) {
        return true;
    }
    X509Name other;
    try {
        other = X509Name.getInstance(obj);
    } catch (IllegalArgumentException e) {
        return false;
    }
    int orderingSize = ordering.size();
    if (orderingSize != other.ordering.size()) {
        return false;
    }
    for (int i = 0; i < orderingSize; i++) {
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) ordering.elementAt(i);
        ASN1ObjectIdentifier oOid = (ASN1ObjectIdentifier) other.ordering.elementAt(i);
        if (oid.equals(oOid)) {
            String value = (String) values.elementAt(i);
            String oValue = (String) other.values.elementAt(i);
            if (!equivalentStrings(value, oValue)) {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1UniversalString(org.bouncycastle.asn1.ASN1UniversalString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Aggregations

ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)209 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)89 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)76 IOException (java.io.IOException)72 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)58 ArrayList (java.util.ArrayList)45 DEROctetString (org.bouncycastle.asn1.DEROctetString)43 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)42 DERSequence (org.bouncycastle.asn1.DERSequence)35 BigInteger (java.math.BigInteger)31 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)30 DERIA5String (org.bouncycastle.asn1.DERIA5String)30 X509Certificate (java.security.cert.X509Certificate)29 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)29 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)29 GeneralName (org.bouncycastle.asn1.x509.GeneralName)26 List (java.util.List)25 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)24 HashSet (java.util.HashSet)24 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)23