Example usage for org.bouncycastle.jce X509Principal getOIDs

List of usage examples for org.bouncycastle.jce X509Principal getOIDs

Introduction

In this page you can find the example usage for org.bouncycastle.jce X509Principal getOIDs.

Prototype

public Vector getOIDs() 

Source Link

Document

return a vector of the oids in the name, in the order they were found.

Usage

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

License:Apache License

/**
 * Creates a new DNImpl object.//from   w  ww . j  a  va  2 s.  c o m
 * 
 * @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.security.util.DNImplRFC2253.java

License:Apache License

/**
 * Parses the RFC2253 format string and puts the fields into the internal structure.
 * /*from ww w  . ja  v  a2 s .c o m*/
 * @param inputDN the string that contains the DN to parse.
 * @param reversed Whether the given DN is to be considered reversed or not.
 */
@SuppressWarnings("unchecked")
private void parse(String inputDN, boolean reversed) {
    X509Principal x509Principal = new X509Principal(reversed, inputDN);

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

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

License:Apache License

/**
 * Parses the input DN into the internal data structure.
 * @param inputDN The DN to parse.// w w  w.j  a v a  2 s  . c  o  m
 */
@SuppressWarnings("unchecked")
private void parseX500Int(String inputDN) {
    String[] parts = inputDN.split("/");

    if (parts.length < 2) {
        return;
    }

    StringBuffer newInput = new StringBuffer();
    newInput.append(parts[1]);

    for (int i = 2; i < parts.length; i++) {
        if (parts[i].contains("=")) {
            newInput = newInput.append(", ").append(parts[i]);
        } else {
            newInput.append('/').append(parts[i]);
        }
    }

    X509Principal x509Principal = new X509Principal(false, newInput.toString());

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

From source file:org.glite.slcs.pki.bouncycastle.Codec.java

License:eu-egee.org license

/**
 * Gets the first value of the {@link X509Principal} corresponding to the
 * given oid./*w  w  w. j av  a 2 s . c  om*/
 * 
 * @param certificate
 *            The X509 certificate, containing the X509Principal.
 * @param oid
 *            The OID of the desired value.
 * @return The value or <code>null</code> if the principal doesn't contain
 *         the oid.
 * @throws GeneralSecurityException
 *             If a crypto error occurs.
 */
static public String getPrincipalValue(X509Certificate certificate, DERObjectIdentifier oid)
        throws GeneralSecurityException {
    X509Principal subject = PrincipalUtil.getSubjectX509Principal(certificate);
    Vector oids = subject.getOIDs();
    int valueIndex = oids.indexOf(oid);
    if (valueIndex < 0) {
        // oid not found
        return null;
    }
    Vector values = subject.getValues();
    String value = values.get(valueIndex).toString();
    return value;
}