use of java.security.cert.CertificateFactory in project robovm by robovm.
the class KeyStore2Test method test_aliases.
/**
* java.security.KeyStore#aliases()
*/
public void test_aliases() throws Exception {
// Test for method java.util.Enumeration
// java.security.KeyStore.aliases()
// NOT COMPATIBLE WITH PCS#12
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate[] cert = new X509Certificate[2];
cert[0] = (X509Certificate) cf.generateCertificate(certArray);
cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
try {
keyTest.aliases();
fail();
} catch (KeyStoreException expected) {
}
keyTest.load(null, null);
// KeyStore keyTest =
// KeyStore.getInstance(KeyStore.getDefaultType());
// alias 1
keyTest.setCertificateEntry("alias1", cert[0]);
// alias 2
keyTest.setCertificateEntry("alias2", cert[0]);
// alias 3
keyTest.setCertificateEntry("alias3", cert[0]);
// obtaining the aliase
Enumeration<String> aliase = keyTest.aliases();
Set<String> alia = new HashSet<String>();
int i = 0;
while (aliase.hasMoreElements()) {
alia.add(aliase.nextElement());
i++;
}
assertEquals("the wrong aliases were returned", i, 3);
assertTrue("the wrong aliases were returned", alia.contains("alias1"));
assertTrue("the wrong aliases were returned", alia.contains("alias2"));
assertTrue("the wrong aliases were returned", alia.contains("alias3"));
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class HostnameVerifierTest method testSubjectAlt.
public void testSubjectAlt() throws Exception {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(X509_MULTIPLE_SUBJECT_ALT);
X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
assertEquals(new X500Principal("CN=localhost"), x509.getSubjectX500Principal());
assertTrue(verifier.verify("localhost", session));
assertTrue(verifier.verify("localhost.localdomain", session));
assertFalse(verifier.verify("local.host", session));
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class HostnameVerifierTest method testSubjectAltName.
public void testSubjectAltName() throws Exception {
/*
* $ cat ./cert.cnf
* [req]
* distinguished_name=distinguished_name
* req_extensions=req_extensions
* x509_extensions=x509_extensions
* [distinguished_name]
* [req_extensions]
* [x509_extensions]
* subjectAltName=DNS:bar.com,DNS:baz.com
*
* $ openssl req -x509 -nodes -days 36500 -subj '/CN=foo.com' -config ./cert.cnf \
* -newkey rsa:512 -out cert.pem
*/
String cert = "-----BEGIN CERTIFICATE-----\n" + "MIIBPTCB6KADAgECAgkA7zoHaaqNGHQwDQYJKoZIhvcNAQEFBQAwEjEQMA4GA1UE\n" + "AxMHZm9vLmNvbTAgFw0xMDEyMjAxODM5MzZaGA8yMTEwMTEyNjE4MzkzNlowEjEQ\n" + "MA4GA1UEAxMHZm9vLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC+gmoSxF+8\n" + "hbV+rgRQqHIJd50216OWQJbU3BvdlPbca779NYO4+UZWTFdBM8BdQqs3H4B5Agvp\n" + "y7HeSff1F7XRAgMBAAGjHzAdMBsGA1UdEQQUMBKCB2Jhci5jb22CB2Jhei5jb20w\n" + "DQYJKoZIhvcNAQEFBQADQQBXpZZPOY2Dy1lGG81JTr8L4or9jpKacD7n51eS8iqI\n" + "oTznPNuXHU5bFN0AAGX2ij47f/EahqTpo5RdS95P4sVm\n" + "-----END CERTIFICATE-----";
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(cert.getBytes("UTF-8"));
X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
assertFalse(verifier.verify("foo.com", session));
assertTrue(verifier.verify("bar.com", session));
assertTrue(verifier.verify("baz.com", session));
assertFalse(verifier.verify("a.foo.com", session));
assertFalse(verifier.verify("quux.com", session));
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class HostnameVerifierTest method testSubjectAltNameWithWildcard.
public void testSubjectAltNameWithWildcard() throws Exception {
/*
* $ cat ./cert.cnf
* [req]
* distinguished_name=distinguished_name
* req_extensions=req_extensions
* x509_extensions=x509_extensions
* [distinguished_name]
* [req_extensions]
* [x509_extensions]
* subjectAltName=DNS:bar.com,DNS:*.baz.com
*
* $ openssl req -x509 -nodes -days 36500 -subj '/CN=foo.com' -config ./cert.cnf \
* -newkey rsa:512 -out cert.pem
*/
String cert = "-----BEGIN CERTIFICATE-----\n" + "MIIBPzCB6qADAgECAgkAnv/7Jv5r7pMwDQYJKoZIhvcNAQEFBQAwEjEQMA4GA1UE\n" + "AxMHZm9vLmNvbTAgFw0xMDEyMjAxODQ2MDFaGA8yMTEwMTEyNjE4NDYwMVowEjEQ\n" + "MA4GA1UEAxMHZm9vLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDAz2YXnyog\n" + "YdYLSFr/OEgSumtwqtZKJTB4wqTW/eKbBCEzxnyUMxWZIqUGu353PzwfOuWp2re3\n" + "nvVV+QDYQlh9AgMBAAGjITAfMB0GA1UdEQQWMBSCB2Jhci5jb22CCSouYmF6LmNv\n" + "bTANBgkqhkiG9w0BAQUFAANBAB8yrSl8zqy07i0SNYx2B/FnvQY734pxioaqFWfO\n" + "Bqo1ZZl/9aPHEWIwBrxYNVB0SGu/kkbt/vxqOjzzrkXukmI=\n" + "-----END CERTIFICATE-----";
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(cert.getBytes("UTF-8"));
X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
assertFalse(verifier.verify("foo.com", session));
assertTrue(verifier.verify("bar.com", session));
assertTrue(verifier.verify("a.baz.com", session));
assertTrue(verifier.verify("baz.com", session));
assertFalse(verifier.verify("a.foo.com", session));
assertFalse(verifier.verify("a.bar.com", session));
assertFalse(verifier.verify("quux.com", session));
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class myHostnameVerifier method getServerCertificates.
/*
* @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
*/
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
try {
CertificateFactory cf = CertificateFactory.getInstance(typeDone);
byte[] barr = TestUtils.getX509Certificate_v3();
ByteArrayInputStream bis = new ByteArrayInputStream(barr);
Certificate cert = cf.generateCertificate(bis);
return new Certificate[] { cert };
} catch (CertificateException se) {
throw new SSLPeerUnverifiedException("No server's end-entity certificate");
}
}
Aggregations