Search in sources :

Example 1 with ASN1UTF8String

use of org.webpki.asn1.ASN1UTF8String in project ldapsdk by pingidentity.

the class X509CertificateTestCase method testDecodeMalformedName.

/**
 * Tests the behavior when trying to decode a DN that includes a malformed RDN
 * element, as well as an attribute type OID that is not defined in the
 * schema.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { CertException.class })
public void testDecodeMalformedName() throws Exception {
    final ASN1Sequence dnSequence = new ASN1Sequence(new ASN1Set(new ASN1Sequence(new ASN1ObjectIdentifier(new OID("1.2.3.4.5.6.7.8")), new ASN1UTF8String("value"))), new ASN1OctetString("not a valid set"));
    X509Certificate.decodeName(dnSequence);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Set(com.unboundid.asn1.ASN1Set) ASN1UTF8String(com.unboundid.asn1.ASN1UTF8String) OID(com.unboundid.util.OID) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) Test(org.testng.annotations.Test)

Example 2 with ASN1UTF8String

use of org.webpki.asn1.ASN1UTF8String in project ldapsdk by pingidentity.

the class CRLDistributionPoint method encode.

/**
 * Encodes this CRL distribution point to an ASN.1 element.
 *
 * @return  The encoded CRL distribution point.
 *
 * @throws  CertException  If a problem is encountered while encoding this
 *                         CRL distribution point.
 */
@NotNull()
ASN1Element encode() throws CertException {
    final ArrayList<ASN1Element> elements = new ArrayList<>(3);
    ASN1Element distributionPointElement = null;
    if (fullName != null) {
        distributionPointElement = new ASN1Element(TYPE_FULL_NAME, fullName.encode().getValue());
    } else if (nameRelativeToCRLIssuer != null) {
        final Schema schema;
        try {
            schema = Schema.getDefaultStandardSchema();
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new CertException(ERR_CRL_DP_ENCODE_CANNOT_GET_SCHEMA.get(toString(), String.valueOf(nameRelativeToCRLIssuer), StaticUtils.getExceptionMessage(e)), e);
        }
        final String[] names = nameRelativeToCRLIssuer.getAttributeNames();
        final String[] values = nameRelativeToCRLIssuer.getAttributeValues();
        final ArrayList<ASN1Element> rdnElements = new ArrayList<>(names.length);
        for (int i = 0; i < names.length; i++) {
            final AttributeTypeDefinition at = schema.getAttributeType(names[i]);
            if (at == null) {
                throw new CertException(ERR_CRL_DP_ENCODE_UNKNOWN_ATTR_TYPE.get(toString(), String.valueOf(nameRelativeToCRLIssuer), names[i]));
            }
            try {
                rdnElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(at.getOID()), new ASN1UTF8String(values[i])));
            } catch (final Exception e) {
                Debug.debugException(e);
                throw new CertException(ERR_CRL_DP_ENCODE_ERROR.get(toString(), String.valueOf(nameRelativeToCRLIssuer), StaticUtils.getExceptionMessage(e)), e);
            }
        }
        distributionPointElement = new ASN1Set(TYPE_NAME_RELATIVE_TO_CRL_ISSUER, rdnElements);
    }
    if (distributionPointElement != null) {
        elements.add(new ASN1Element(TYPE_DISTRIBUTION_POINT, distributionPointElement.encode()));
    }
    if (!revocationReasons.equals(EnumSet.allOf(CRLDistributionPointRevocationReason.class))) {
        elements.add(CRLDistributionPointRevocationReason.toBitString(TYPE_REASONS, revocationReasons));
    }
    if (crlIssuer != null) {
        elements.add(new ASN1Element(TYPE_CRL_ISSUER, crlIssuer.encode().getValue()));
    }
    return new ASN1Sequence(elements);
}
Also used : AttributeTypeDefinition(com.unboundid.ldap.sdk.schema.AttributeTypeDefinition) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Set(com.unboundid.asn1.ASN1Set) ASN1UTF8String(com.unboundid.asn1.ASN1UTF8String) ASN1Element(com.unboundid.asn1.ASN1Element) Schema(com.unboundid.ldap.sdk.schema.Schema) ArrayList(java.util.ArrayList) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) NotNull(com.unboundid.util.NotNull)

Example 3 with ASN1UTF8String

use of org.webpki.asn1.ASN1UTF8String in project ldapsdk by pingidentity.

the class X509Certificate method encodeName.

/**
 * Encodes the provided DN as an X.509 name for inclusion in an encoded
 * certificate.
 *
 * @param  dn  The DN to encode.
 *
 * @return  The encoded X.509 name.
 *
 * @throws  CertException  If a problem is encountered while encoding the
 *                         provided DN as an X.509 name.
 */
@NotNull()
static ASN1Element encodeName(@NotNull final DN dn) throws CertException {
    final Schema schema;
    try {
        schema = Schema.getDefaultStandardSchema();
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new CertException(ERR_CERT_ENCODE_NAME_CANNOT_GET_SCHEMA.get(String.valueOf(dn), StaticUtils.getExceptionMessage(e)), e);
    }
    final RDN[] rdns = dn.getRDNs();
    final ArrayList<ASN1Element> rdnSequenceElements = new ArrayList<>(rdns.length);
    for (int i = rdns.length - 1; i >= 0; i--) {
        final RDN rdn = rdns[i];
        final String[] names = rdn.getAttributeNames();
        final String[] values = rdn.getAttributeValues();
        final ArrayList<ASN1Element> rdnElements = new ArrayList<>(names.length);
        for (int j = 0; j < names.length; j++) {
            final AttributeTypeDefinition at = schema.getAttributeType(names[j]);
            if (at == null) {
                throw new CertException(ERR_CERT_ENCODE_NAME_UNKNOWN_ATTR_TYPE.get(String.valueOf(dn), names[j]));
            }
            try {
                rdnElements.add(new ASN1Sequence(new ASN1ObjectIdentifier(at.getOID()), new ASN1UTF8String(values[j])));
            } catch (final Exception e) {
                Debug.debugException(e);
                throw new CertException(ERR_CERT_ENCODE_NAME_ERROR.get(String.valueOf(dn), StaticUtils.getExceptionMessage(e)), e);
            }
        }
        rdnSequenceElements.add(new ASN1Set(rdnElements));
    }
    return new ASN1Sequence(rdnSequenceElements);
}
Also used : ASN1UTF8String(com.unboundid.asn1.ASN1UTF8String) Schema(com.unboundid.ldap.sdk.schema.Schema) ArrayList(java.util.ArrayList) ASN1UTF8String(com.unboundid.asn1.ASN1UTF8String) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1BitString(com.unboundid.asn1.ASN1BitString) ASN1Exception(com.unboundid.asn1.ASN1Exception) CertificateException(java.security.cert.CertificateException) AttributeTypeDefinition(com.unboundid.ldap.sdk.schema.AttributeTypeDefinition) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Set(com.unboundid.asn1.ASN1Set) ASN1Element(com.unboundid.asn1.ASN1Element) RDN(com.unboundid.ldap.sdk.RDN) ASN1ObjectIdentifier(com.unboundid.asn1.ASN1ObjectIdentifier) NotNull(com.unboundid.util.NotNull)

Example 4 with ASN1UTF8String

use of org.webpki.asn1.ASN1UTF8String 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 5 with ASN1UTF8String

use of org.webpki.asn1.ASN1UTF8String in project LinLong-Java by zhenwei1108.

the class ASN1Dump method _dumpAsString.

/**
 * dump a DER object as a formatted string with indentation
 *
 * @param obj the ASN1Primitive to be dumped out.
 */
static void _dumpAsString(String indent, boolean verbose, ASN1Primitive obj, StringBuffer buf) {
    String nl = Strings.lineSeparator();
    if (obj instanceof ASN1Null) {
        buf.append(indent);
        buf.append("NULL");
        buf.append(nl);
    } else if (obj instanceof ASN1Sequence) {
        buf.append(indent);
        if (obj instanceof BERSequence) {
            buf.append("BER Sequence");
        } else if (obj instanceof DERSequence) {
            buf.append("DER Sequence");
        } else {
            buf.append("Sequence");
        }
        buf.append(nl);
        ASN1Sequence sequence = (ASN1Sequence) obj;
        String elementsIndent = indent + TAB;
        for (int i = 0, count = sequence.size(); i < count; ++i) {
            _dumpAsString(elementsIndent, verbose, sequence.getObjectAt(i).toASN1Primitive(), buf);
        }
    } else if (obj instanceof ASN1Set) {
        buf.append(indent);
        if (obj instanceof BERSet) {
            buf.append("BER Set");
        } else if (obj instanceof DERSet) {
            buf.append("DER Set");
        } else {
            buf.append("Set");
        }
        buf.append(nl);
        ASN1Set set = (ASN1Set) obj;
        String elementsIndent = indent + TAB;
        for (int i = 0, count = set.size(); i < count; ++i) {
            _dumpAsString(elementsIndent, verbose, set.getObjectAt(i).toASN1Primitive(), buf);
        }
    } else if (obj instanceof ASN1ApplicationSpecific) {
        _dumpAsString(indent, verbose, ((ASN1ApplicationSpecific) obj).getTaggedObject(), buf);
    } else if (obj instanceof ASN1TaggedObject) {
        buf.append(indent);
        if (obj instanceof BERTaggedObject) {
            buf.append("BER Tagged ");
        } else if (obj instanceof DERTaggedObject) {
            buf.append("DER Tagged ");
        } else {
            buf.append("Tagged ");
        }
        ASN1TaggedObject o = (ASN1TaggedObject) obj;
        buf.append(ASN1Util.getTagText(o));
        if (!o.isExplicit()) {
            buf.append(" IMPLICIT ");
        }
        buf.append(nl);
        String baseIndent = indent + TAB;
        _dumpAsString(baseIndent, verbose, o.getBaseObject().toASN1Primitive(), buf);
    } else if (obj instanceof ASN1OctetString) {
        ASN1OctetString oct = (ASN1OctetString) obj;
        if (obj instanceof BEROctetString) {
            buf.append(indent + "BER Constructed Octet String" + "[" + oct.getOctets().length + "] ");
        } else {
            buf.append(indent + "DER Octet String" + "[" + oct.getOctets().length + "] ");
        }
        if (verbose) {
            buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
        } else {
            buf.append(nl);
        }
    } else if (obj instanceof ASN1ObjectIdentifier) {
        buf.append(indent + "ObjectIdentifier(" + ((ASN1ObjectIdentifier) obj).getId() + ")" + nl);
    } else if (obj instanceof ASN1RelativeOID) {
        buf.append(indent + "RelativeOID(" + ((ASN1RelativeOID) obj).getId() + ")" + nl);
    } else if (obj instanceof ASN1Boolean) {
        buf.append(indent + "Boolean(" + ((ASN1Boolean) obj).isTrue() + ")" + nl);
    } else if (obj instanceof ASN1Integer) {
        buf.append(indent + "Integer(" + ((ASN1Integer) obj).getValue() + ")" + nl);
    } else if (obj instanceof ASN1BitString) {
        ASN1BitString bitString = (ASN1BitString) obj;
        byte[] bytes = bitString.getBytes();
        int padBits = bitString.getPadBits();
        if (bitString instanceof DERBitString) {
            buf.append(indent + "DER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        } else if (bitString instanceof DLBitString) {
            buf.append(indent + "DL Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        } else {
            buf.append(indent + "BER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
        }
        if (verbose) {
            buf.append(dumpBinaryDataAsString(indent, bytes));
        } else {
            buf.append(nl);
        }
    } else if (obj instanceof ASN1IA5String) {
        buf.append(indent + "IA5String(" + ((ASN1IA5String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1UTF8String) {
        buf.append(indent + "UTF8String(" + ((ASN1UTF8String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1NumericString) {
        buf.append(indent + "NumericString(" + ((ASN1NumericString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1PrintableString) {
        buf.append(indent + "PrintableString(" + ((ASN1PrintableString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1VisibleString) {
        buf.append(indent + "VisibleString(" + ((ASN1VisibleString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1BMPString) {
        buf.append(indent + "BMPString(" + ((ASN1BMPString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1T61String) {
        buf.append(indent + "T61String(" + ((ASN1T61String) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1GraphicString) {
        buf.append(indent + "GraphicString(" + ((ASN1GraphicString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1VideotexString) {
        buf.append(indent + "VideotexString(" + ((ASN1VideotexString) obj).getString() + ") " + nl);
    } else if (obj instanceof ASN1UTCTime) {
        buf.append(indent + "UTCTime(" + ((ASN1UTCTime) obj).getTime() + ") " + nl);
    } else if (obj instanceof ASN1GeneralizedTime) {
        buf.append(indent + "GeneralizedTime(" + ((ASN1GeneralizedTime) obj).getTime() + ") " + nl);
    } else if (obj instanceof ASN1Enumerated) {
        ASN1Enumerated en = (ASN1Enumerated) obj;
        buf.append(indent + "DER Enumerated(" + en.getValue() + ")" + nl);
    } else if (obj instanceof ASN1ObjectDescriptor) {
        ASN1ObjectDescriptor od = (ASN1ObjectDescriptor) obj;
        buf.append(indent + "ObjectDescriptor(" + od.getBaseGraphicString().getString() + ") " + nl);
    } else if (obj instanceof ASN1External) {
        ASN1External ext = (ASN1External) obj;
        buf.append(indent + "External " + nl);
        String tab = indent + TAB;
        if (ext.getDirectReference() != null) {
            buf.append(tab + "Direct Reference: " + ext.getDirectReference().getId() + nl);
        }
        if (ext.getIndirectReference() != null) {
            buf.append(tab + "Indirect Reference: " + ext.getIndirectReference().toString() + nl);
        }
        if (ext.getDataValueDescriptor() != null) {
            _dumpAsString(tab, verbose, ext.getDataValueDescriptor(), buf);
        }
        buf.append(tab + "Encoding: " + ext.getEncoding() + nl);
        _dumpAsString(tab, verbose, ext.getExternalContent(), buf);
    } else {
        buf.append(indent + obj.toString() + nl);
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1UTCTime(com.github.zhenwei.core.asn1.ASN1UTCTime) ASN1GeneralizedTime(com.github.zhenwei.core.asn1.ASN1GeneralizedTime) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String) ASN1T61String(com.github.zhenwei.core.asn1.ASN1T61String) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1VisibleString(com.github.zhenwei.core.asn1.ASN1VisibleString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ASN1NumericString(com.github.zhenwei.core.asn1.ASN1NumericString) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1BMPString(com.github.zhenwei.core.asn1.ASN1BMPString) ASN1VideotexString(com.github.zhenwei.core.asn1.ASN1VideotexString) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1PrintableString(com.github.zhenwei.core.asn1.ASN1PrintableString) DLBitString(com.github.zhenwei.core.asn1.DLBitString) ASN1GraphicString(com.github.zhenwei.core.asn1.ASN1GraphicString) DLBitString(com.github.zhenwei.core.asn1.DLBitString) DERSet(com.github.zhenwei.core.asn1.DERSet) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1External(com.github.zhenwei.core.asn1.ASN1External) ASN1T61String(com.github.zhenwei.core.asn1.ASN1T61String) DERSequence(com.github.zhenwei.core.asn1.DERSequence) BEROctetString(com.github.zhenwei.core.asn1.BEROctetString) ASN1Enumerated(com.github.zhenwei.core.asn1.ASN1Enumerated) BERTaggedObject(com.github.zhenwei.core.asn1.BERTaggedObject) ASN1ObjectDescriptor(com.github.zhenwei.core.asn1.ASN1ObjectDescriptor) BERSet(com.github.zhenwei.core.asn1.BERSet) ASN1NumericString(com.github.zhenwei.core.asn1.ASN1NumericString) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1GraphicString(com.github.zhenwei.core.asn1.ASN1GraphicString) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) BERSequence(com.github.zhenwei.core.asn1.BERSequence) ASN1ApplicationSpecific(com.github.zhenwei.core.asn1.ASN1ApplicationSpecific) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ASN1RelativeOID(com.github.zhenwei.core.asn1.ASN1RelativeOID) ASN1VideotexString(com.github.zhenwei.core.asn1.ASN1VideotexString) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ASN1VisibleString(com.github.zhenwei.core.asn1.ASN1VisibleString) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String) ASN1PrintableString(com.github.zhenwei.core.asn1.ASN1PrintableString) ASN1Boolean(com.github.zhenwei.core.asn1.ASN1Boolean) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ASN1Null(com.github.zhenwei.core.asn1.ASN1Null)

Aggregations

ASN1UTF8String (com.github.zhenwei.core.asn1.ASN1UTF8String)3 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)3 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)3 ASN1Set (com.unboundid.asn1.ASN1Set)3 ASN1UTF8String (com.unboundid.asn1.ASN1UTF8String)3 ASN1ApplicationSpecific (com.github.zhenwei.core.asn1.ASN1ApplicationSpecific)2 ASN1Boolean (com.github.zhenwei.core.asn1.ASN1Boolean)2 ASN1IA5String (com.github.zhenwei.core.asn1.ASN1IA5String)2 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)2 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)2 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)2 ASN1TaggedObject (com.github.zhenwei.core.asn1.ASN1TaggedObject)2 DERBitString (com.github.zhenwei.core.asn1.DERBitString)2 ASN1Element (com.unboundid.asn1.ASN1Element)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 AttributeTypeDefinition (com.unboundid.ldap.sdk.schema.AttributeTypeDefinition)2 Schema (com.unboundid.ldap.sdk.schema.Schema)2 NotNull (com.unboundid.util.NotNull)2 IOException (java.io.IOException)2