Example usage for org.bouncycastle.asn1.x509 X509Name UID

List of usage examples for org.bouncycastle.asn1.x509 X509Name UID

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 X509Name UID.

Prototype

ASN1ObjectIdentifier UID

To view the source code for org.bouncycastle.asn1.x509 X509Name UID.

Click Source Link

Document

LDAP User id.

Usage

From source file:org.glite.security.util.DNImplRFC2253.java

License:Apache License

/**
 * Creates a new DNImpl object./*from   w  ww .  j  a  v a 2s  .c om*/
 * 
 * @param principal The Principal holding the information to generate the DN from.
 */
@SuppressWarnings("unchecked")
public DNImplRFC2253(Principal principal) {
    X509Principal x509Principal;
    //        LOGGER.debug("input is: " + principal.getClass().getName() + " from classloader: " + principal.getClass().getClassLoader() + " current one is: " + getClass().getClassLoader());

    if (principal instanceof X509Principal) {
        // for X509Principal use it directly.
        //           LOGGER.debug("input is X509Principal");
        x509Principal = (X509Principal) principal;
    } else {
        if (principal instanceof X500Principal) {
            // for X500Principal, get the encoded and reparse as bouncycastle X509Principal.
            //                LOGGER.debug("input is java Principal");

            try {
                x509Principal = new X509Principal((((X500Principal) principal).getEncoded()));
            } catch (Exception e) {
                LOGGER.error("Invalid X500Principal DN name: " + principal);
                throw new IllegalArgumentException("Invalid X500Principal DN name: " + principal);
            }
        } else {
            // for other principals, get the name and try to parse it.
            LOGGER.debug("input is some other principal: " + principal.getClass().getName());
            String name = principal.getName();
            String testName = name.toLowerCase().trim();
            // UGLY HACK, shouldn't do this, but there seems to be no way around it, input can be many classes that give the DN in different orders. And from different classloaders preventing casts etc.
            // if DN starts with email or CN, it's in reversed order
            //                LOGGER.debug("test name: " + testName);
            if (testName.startsWith("email") || testName.startsWith("e=") || testName.startsWith("cn=")
                    || testName.startsWith("uid=") || testName.startsWith("sn=")) {
                x509Principal = new X509Principal(true, principal.getName());
                //                    LOGGER.debug("name first " + x509Principal);
            } else {
                // if it starts with country or state, it's in direct order
                if (testName.startsWith("c=") || testName.startsWith("st=") || testName.startsWith("ou=")
                        || testName.startsWith("dc=") || testName.startsWith("o=")) {
                    x509Principal = new X509Principal(false, principal.getName());
                    //                        LOGGER.debug("country first, reverse " + x509Principal);
                } else {
                    // check if it end with CN, email, UID or SN, and then not flip it.
                    x509Principal = new X509Principal(false, principal.getName());
                    Vector oids = x509Principal.getOIDs();
                    String rdn = ((DERObjectIdentifier) oids.lastElement()).getId();
                    if (rdn.equals(X509Name.CN.getId()) || rdn.equals(X509Name.E.getId())
                            || rdn.equals(X509Name.UID.getId()) || rdn.equals(X509Name.SN.getId())) {
                        x509Principal = new X509Principal(false, principal.getName());
                    } else {
                        // other cases assume it's in reverse order
                        x509Principal = new X509Principal(true, principal.getName());
                        //                            LOGGER.debug("unknown first " + x509Principal);
                    }
                }
            }
        }
    }

    m_oids = (DERObjectIdentifier[]) x509Principal.getOIDs().toArray(new DERObjectIdentifier[] {});
    m_rdns = (String[]) x509Principal.getValues().toArray(new String[] {});
    m_count = m_oids.length;
}

From source file:org.glite.voms.PKIUtils.java

License:Open Source License

/**
 * Gets an OpenSSL-style representation of a principal.
 *
 * @param principal the principal//from  w w  w.j  a v  a  2s  . c  o  m
 *
 * @return a String representing the principal.
 */
public static String getOpenSSLFormatPrincipal(Principal principal) {
    X509Name name = new X509Name(principal.getName());

    Vector oids = name.getOIDs();
    Vector values = name.getValues();

    ListIterator oids_iter = oids.listIterator();
    ListIterator values_iter = values.listIterator();
    String result = new String();

    while (oids_iter.hasNext()) {
        DERObjectIdentifier oid = (DERObjectIdentifier) oids_iter.next();
        String value = (String) values_iter.next();
        if (oid.equals(X509Name.C))
            result += "/C=" + value;
        else if (oid.equals(X509Name.CN))
            result += "/CN=" + value;
        else if (oid.equals(X509Name.DC))
            result += "/DC=" + value;
        else if (oid.equals(X509Name.E))
            result += "/E=" + value;
        else if (oid.equals(X509Name.EmailAddress))
            result += "/Email=" + value;
        else if (oid.equals(X509Name.L))
            result += "/L=" + value;
        else if (oid.equals(X509Name.O))
            result += "/O=" + value;
        else if (oid.equals(X509Name.OU))
            result += "/OU=" + value;
        else if (oid.equals(X509Name.ST))
            result += "/ST=" + value;
        else if (oid.equals(X509Name.UID))
            result += "/UID=" + value;
        else
            result += "/" + oid.toString() + "=" + value;
    }

    logger.debug("SSLFormat: " + result);
    return result;
}

From source file:org.xwiki.crypto.x509.XWikiX509Certificate.java

License:Open Source License

/**
 * Get user name (stored as UID in the distinguished subject name) of this certificate's author, or empty string
 * if UID is not present.//from  w w  w  .j  a va2 s .com
 * 
 * @return author UID
 */
public String getAuthorUID() {
    try {
        X509Principal author = PrincipalUtil.getSubjectX509Principal(this.certificate);
        if (author.getValues(X509Name.UID).size() == 0) {
            return "";
        }
        return (String) author.getValues(X509Name.UID).get(0);
    } catch (CertificateEncodingException exception) {
        // should not happen
        return "";
    }
}