Search in sources :

Example 1 with ASN1String

use of org.webpki.asn1.ASN1String in project bitcoinj by bitcoinj.

the class X509Utils method getDisplayNameFromCertificate.

/**
 * Returns either a string that "sums up" the certificate for humans, in a similar manner to what you might see
 * in a web browser, or null if one cannot be extracted. This will typically be the common name (CN) field, but
 * can also be the org (O) field, org+location+country if withLocation is set, or the email
 * address for S/MIME certificates.
 */
@Nullable
public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException {
    X500Name name = new X500Name(certificate.getSubjectX500Principal().getName());
    String commonName = null, org = null, location = null, country = null;
    for (RDN rdn : name.getRDNs()) {
        AttributeTypeAndValue pair = rdn.getFirst();
        String val = ((ASN1String) pair.getValue()).getString();
        ASN1ObjectIdentifier type = pair.getType();
        if (type.equals(RFC4519Style.cn))
            commonName = val;
        else if (type.equals(RFC4519Style.o))
            org = val;
        else if (type.equals(RFC4519Style.l))
            location = val;
        else if (type.equals(RFC4519Style.c))
            country = val;
    }
    final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames();
    String altName = null;
    if (subjectAlternativeNames != null)
        for (final List<?> subjectAlternativeName : subjectAlternativeNames) if (// rfc822name
        (Integer) subjectAlternativeName.get(0) == 1)
            altName = (String) subjectAlternativeName.get(1);
    if (org != null) {
        return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org;
    } else if (commonName != null) {
        return commonName;
    } else {
        return altName;
    }
}
Also used : List(java.util.List) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1String(org.bouncycastle.asn1.ASN1String) RDN(org.bouncycastle.asn1.x500.RDN) AttributeTypeAndValue(org.bouncycastle.asn1.x500.AttributeTypeAndValue) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) Nullable(javax.annotation.Nullable)

Example 2 with ASN1String

use of org.webpki.asn1.ASN1String in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class X509NameHelper method toString.

private static String toString(ASN1Sequence seq) {
    if (seq == null) {
        return null;
    }
    Enumeration e = seq.getObjects();
    StringBuffer buf = new StringBuffer();
    while (e.hasMoreElements()) {
        ASN1Set set = (ASN1Set) e.nextElement();
        Enumeration ee = set.getObjects();
        buf.append('/');
        while (ee.hasMoreElements()) {
            ASN1Sequence s = (ASN1Sequence) ee.nextElement();
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) s.getObjectAt(0);
            String sym = (String) X509Name.DefaultSymbols.get(oid);
            if (sym == null) {
                buf.append(oid.getId());
            } else {
                buf.append(sym);
            }
            buf.append('=');
            buf.append(((ASN1String) s.getObjectAt(1)).getString());
            if (ee.hasMoreElements()) {
                buf.append('+');
            }
        }
    }
    return buf.toString();
}
Also used : Enumeration(java.util.Enumeration) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 3 with ASN1String

use of org.webpki.asn1.ASN1String in project cas by apereo.

the class X509UPNExtractorUtils method getUPNStringFromSequence.

/**
 * Get UPN String.
 *
 * @param seq ASN1Sequence abstraction representing subject alternative name.
 *            First element is the object identifier, second is the object itself.
 * @return UPN string or null
 */
private String getUPNStringFromSequence(final ASN1Sequence seq) {
    val id = seq != null ? ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)) : null;
    if (id != null && UPN_OBJECTID.equals(id.getId())) {
        val obj = (ASN1TaggedObject) seq.getObjectAt(1);
        val primitiveObj = obj.getObject();
        val func = FunctionUtils.doIf(Predicates.instanceOf(ASN1TaggedObject.class), () -> ASN1TaggedObject.getInstance(primitiveObj).getObject(), () -> primitiveObj);
        val prim = func.apply(primitiveObj);
        if (prim instanceof ASN1OctetString) {
            return new String(((ASN1OctetString) prim).getOctets(), StandardCharsets.UTF_8);
        }
        if (prim instanceof ASN1String) {
            return ((ASN1String) prim).getString();
        }
    }
    return null;
}
Also used : lombok.val(lombok.val) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1String(org.bouncycastle.asn1.ASN1String)

Example 4 with ASN1String

use of org.webpki.asn1.ASN1String in project robovm by robovm.

the class IETFUtils method canonicalize.

public static String canonicalize(String s) {
    String value = Strings.toLowerCase(s.trim());
    if (value.length() > 0 && value.charAt(0) == '#') {
        ASN1Primitive obj = decodeObject(value);
        if (obj instanceof ASN1String) {
            value = Strings.toLowerCase(((ASN1String) obj).getString().trim());
        }
    }
    value = stripInternalSpaces(value);
    return value;
}
Also used : ASN1String(org.bouncycastle.asn1.ASN1String) DERUniversalString(org.bouncycastle.asn1.DERUniversalString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 5 with ASN1String

use of org.webpki.asn1.ASN1String in project XobotOS by xamarin.

the class IETFUtils method valueToString.

public static String valueToString(ASN1Encodable value) {
    StringBuffer vBuf = new StringBuffer();
    if (value instanceof ASN1String && !(value instanceof DERUniversalString)) {
        String v = ((ASN1String) value).getString();
        if (v.length() > 0 && v.charAt(0) == '#') {
            vBuf.append("\\" + v);
        } else {
            vBuf.append(v);
        }
    } else {
        vBuf.append("#" + bytesToString(Hex.encode(value.getDERObject().getDEREncoded())));
    }
    int end = vBuf.length();
    int index = 0;
    if (vBuf.length() >= 2 && vBuf.charAt(0) == '\\' && vBuf.charAt(1) == '#') {
        index += 2;
    }
    while (index != end) {
        if ((vBuf.charAt(index) == ',') || (vBuf.charAt(index) == '"') || (vBuf.charAt(index) == '\\') || (vBuf.charAt(index) == '+') || (vBuf.charAt(index) == '=') || (vBuf.charAt(index) == '<') || (vBuf.charAt(index) == '>') || (vBuf.charAt(index) == ';')) {
            vBuf.insert(index, "\\");
            index++;
            end++;
        }
        index++;
    }
    return vBuf.toString();
}
Also used : DERUniversalString(org.bouncycastle.asn1.DERUniversalString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1String(org.bouncycastle.asn1.ASN1String) DERUniversalString(org.bouncycastle.asn1.DERUniversalString)

Aggregations

ASN1String (org.bouncycastle.asn1.ASN1String)28 IOException (java.io.IOException)14 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)9 DERIA5String (org.bouncycastle.asn1.DERIA5String)9 DEROctetString (org.bouncycastle.asn1.DEROctetString)9 DERUniversalString (org.bouncycastle.asn1.DERUniversalString)8 ASN1String (com.github.zhenwei.core.asn1.ASN1String)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)7 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)7 CertificateException (java.security.cert.CertificateException)6 DERBMPString (org.bouncycastle.asn1.DERBMPString)6 DERT61String (org.bouncycastle.asn1.DERT61String)6 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)6 GeneralName (org.bouncycastle.asn1.x509.GeneralName)6 CertificateExpiredException (java.security.cert.CertificateExpiredException)5 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)5 CertificateParsingException (java.security.cert.CertificateParsingException)5 Enumeration (java.util.Enumeration)5