Search in sources :

Example 16 with X500Principal

use of javax.security.auth.x500.X500Principal in project robovm by robovm.

the class AttributeCertificateIssuer method getNames.

private Object[] getNames() {
    GeneralNames name;
    if (form instanceof V2Form) {
        name = ((V2Form) form).getIssuerName();
    } else {
        name = (GeneralNames) form;
    }
    GeneralName[] names = name.getNames();
    List l = new ArrayList(names.length);
    for (int i = 0; i != names.length; i++) {
        if (names[i].getTagNo() == GeneralName.directoryName) {
            try {
                l.add(new X500Principal(((ASN1Encodable) names[i].getName()).toASN1Primitive().getEncoded()));
            } catch (IOException e) {
                throw new RuntimeException("badly formed Name object");
            }
        }
    }
    return l.toArray(new Object[l.size()]);
}
Also used : V2Form(org.bouncycastle.asn1.x509.V2Form) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) ArrayList(java.util.ArrayList) X500Principal(javax.security.auth.x500.X500Principal) ArrayList(java.util.ArrayList) List(java.util.List) GeneralName(org.bouncycastle.asn1.x509.GeneralName) IOException(java.io.IOException)

Example 17 with X500Principal

use of javax.security.auth.x500.X500Principal in project robovm by robovm.

the class DefaultHostnameVerifier method verifyHostName.

/**
     * Returns true if {@code certificate} matches {@code hostName}.
     */
private boolean verifyHostName(String hostName, X509Certificate certificate) {
    hostName = hostName.toLowerCase(Locale.US);
    boolean hasDns = false;
    for (String altName : getSubjectAltNames(certificate, ALT_DNS_NAME)) {
        hasDns = true;
        if (verifyHostName(hostName, altName)) {
            return true;
        }
    }
    if (!hasDns) {
        X500Principal principal = certificate.getSubjectX500Principal();
        // RFC 2818 advises using the most specific name for matching.
        String cn = new DistinguishedNameParser(principal).findMostSpecific("cn");
        if (cn != null) {
            return verifyHostName(hostName, cn);
        }
    }
    return false;
}
Also used : X500Principal(javax.security.auth.x500.X500Principal)

Example 18 with X500Principal

use of javax.security.auth.x500.X500Principal in project robovm by robovm.

the class JarEntry method getCodeSigners.

private CodeSigner[] getCodeSigners(Certificate[] certs) {
    if (certs == null) {
        return null;
    }
    X500Principal prevIssuer = null;
    ArrayList<Certificate> list = new ArrayList<Certificate>(certs.length);
    ArrayList<CodeSigner> asigners = new ArrayList<CodeSigner>();
    for (Certificate element : certs) {
        if (!(element instanceof X509Certificate)) {
            // Only X509Certificate-s are taken into account - see API spec.
            continue;
        }
        X509Certificate x509 = (X509Certificate) element;
        if (prevIssuer != null) {
            X500Principal subj = x509.getSubjectX500Principal();
            if (!prevIssuer.equals(subj)) {
                // Ok, this ends the previous chain,
                // so transform this one into CertPath ...
                addCodeSigner(asigners, list);
                // ... and start a new one
                list.clear();
            }
        // else { it's still the same chain }
        }
        prevIssuer = x509.getIssuerX500Principal();
        list.add(x509);
    }
    if (!list.isEmpty()) {
        addCodeSigner(asigners, list);
    }
    if (asigners.isEmpty()) {
        // 'signers' is 'null' already
        return null;
    }
    CodeSigner[] tmp = new CodeSigner[asigners.size()];
    asigners.toArray(tmp);
    return tmp;
}
Also used : ArrayList(java.util.ArrayList) X500Principal(javax.security.auth.x500.X500Principal) CodeSigner(java.security.CodeSigner) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 19 with X500Principal

use of javax.security.auth.x500.X500Principal in project robovm by robovm.

the class X500PrincipalTest method test_X500Principal_03.

/**
     * javax.security.auth.x500.X500Principal#X500Principal(byte[] name)
     */
public void test_X500Principal_03() {
    String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
    byte[] baNull = null;
    try {
        X500Principal xpr = new X500Principal(ba);
        assertNotNull("Null object returned", xpr);
        byte[] resArray = xpr.getEncoded();
        assertEquals(ba.length, resArray.length);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        X500Principal xpr = new X500Principal(baNull);
        fail("IllegalArgumentException wasn't thrown");
    } catch (IllegalArgumentException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of IllegalArgumentException");
    }
    ba = name.getBytes();
    try {
        X500Principal xpr = new X500Principal(ba);
        fail("IllegalArgumentException wasn't thrown");
    } catch (IllegalArgumentException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of IllegalArgumentException");
    }
}
Also used : X500Principal(javax.security.auth.x500.X500Principal)

Example 20 with X500Principal

use of javax.security.auth.x500.X500Principal in project robovm by robovm.

the class X500PrincipalTest method test_X500Principal_02.

/**
     * javax.security.auth.x500.X500Principal#X500Principal(InputStream is)
     */
public void test_X500Principal_02() {
    String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US";
    byte[] ba = getByteArray(TestUtils.getX509Certificate_v1());
    ByteArrayInputStream is = new ByteArrayInputStream(ba);
    InputStream isNull = null;
    try {
        X500Principal xpr = new X500Principal(is);
        assertNotNull("Null object returned", xpr);
        byte[] resArray = xpr.getEncoded();
        assertEquals(ba.length, resArray.length);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        X500Principal xpr = new X500Principal(isNull);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of NullPointerException");
    }
    is = new ByteArrayInputStream(name.getBytes());
    try {
        X500Principal xpr = new X500Principal(is);
        fail("IllegalArgumentException wasn't thrown");
    } catch (IllegalArgumentException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of IllegalArgumentException");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) X500Principal(javax.security.auth.x500.X500Principal)

Aggregations

X500Principal (javax.security.auth.x500.X500Principal)246 X509Certificate (java.security.cert.X509Certificate)68 IOException (java.io.IOException)52 ArrayList (java.util.ArrayList)39 List (java.util.List)25 Principal (java.security.Principal)21 PublicKey (java.security.PublicKey)21 TrustAnchor (java.security.cert.TrustAnchor)21 Certificate (java.security.cert.Certificate)20 X509CertSelector (java.security.cert.X509CertSelector)16 HashMap (java.util.HashMap)16 BigInteger (java.math.BigInteger)15 KeyPair (java.security.KeyPair)15 HashSet (java.util.HashSet)14 Test (org.junit.Test)14 KeyPairGenerator (java.security.KeyPairGenerator)13 CertPathValidatorException (java.security.cert.CertPathValidatorException)13 CertificateException (java.security.cert.CertificateException)13 GeneralSecurityException (java.security.GeneralSecurityException)12 CertificateParsingException (java.security.cert.CertificateParsingException)12