use of java.security.cert.CertificateException in project robovm by robovm.
the class SSLSocketTest method test_SSLSocket_TrustManagerRuntimeException.
public void test_SSLSocket_TrustManagerRuntimeException() throws Exception {
TestSSLContext c = TestSSLContext.create();
SSLContext clientContext = SSLContext.getInstance("TLS");
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
throw new AssertionError();
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// throw a RuntimeException from custom TrustManager
throw new RuntimeException();
}
@Override
public X509Certificate[] getAcceptedIssuers() {
throw new AssertionError();
}
};
clientContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocket client = (SSLSocket) clientContext.getSocketFactory().createSocket(c.host, c.port);
final SSLSocket server = (SSLSocket) c.serverSocket.accept();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Void> future = executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
server.startHandshake();
return null;
}
});
executor.shutdown();
try {
client.startHandshake();
fail();
} catch (SSLHandshakeException expected) {
// before we would get a RuntimeException from checkServerTrusted.
}
future.get();
client.close();
server.close();
c.close();
}
use of java.security.cert.CertificateException in project robovm by robovm.
the class TrustManagerFactoryTest method test_X509TrustManager.
private void test_X509TrustManager(X509TrustManager tm) throws Exception {
for (String keyType : KEY_TYPES) {
X509Certificate[] issuers = tm.getAcceptedIssuers();
assertNotNull(issuers);
assertTrue(issuers.length > 1);
assertNotSame(issuers, tm.getAcceptedIssuers());
boolean defaultTrustManager = // RI de-duplicates certs from TrustedCertificateEntry and PrivateKeyEntry
issuers.length > (StandardNames.IS_RI ? 1 : 2) * KEY_TYPES.length;
String keyAlgName = TestKeyStore.keyAlgorithm(keyType);
String sigAlgName = TestKeyStore.signatureAlgorithm(keyType);
PrivateKeyEntry pke = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName);
X509Certificate[] chain = (X509Certificate[]) pke.getCertificateChain();
if (defaultTrustManager) {
try {
tm.checkClientTrusted(chain, keyType);
fail();
} catch (CertificateException expected) {
}
try {
tm.checkServerTrusted(chain, keyType);
fail();
} catch (CertificateException expected) {
}
} else {
tm.checkClientTrusted(chain, keyType);
tm.checkServerTrusted(chain, keyType);
}
}
}
use of java.security.cert.CertificateException in project robovm by robovm.
the class KeyStore4Test method testLoadLoadStoreParameter.
public void testLoadLoadStoreParameter() {
try {
keyStore.load(null);
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
}
});
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return null;
}
});
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return new KeyStore.ProtectionParameter() {
};
}
});
fail("expected CertificateException");
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (CertificateException e) {
// ok
} catch (IOException e) {
fail("unexpected exception: " + e);
}
}
use of java.security.cert.CertificateException 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");
}
}
use of java.security.cert.CertificateException in project robovm by robovm.
the class CertificateFactory2Test method GetInstance01.
/**
* Test for <code>getInstance(String type)</code> method
* Assertions:
* throws NullPointerException when type is null
* throws CertificateException when type is not available
* returns CertificateFactory object
*/
public void GetInstance01(boolean mode) throws CertificateException, CRLException {
try {
CertificateFactory.getInstance(null);
fail("NullPointerException or CertificateException must be thrown when type is null");
} catch (CertificateException e) {
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertificateFactory.getInstance(invalidValues[i]);
fail("CertificateException must be thrown (type: ".concat(invalidValues[i]).concat(")"));
} catch (CertificateException e) {
}
}
CertificateFactory cerF;
for (int i = 0; i < validValues.length; i++) {
cerF = CertificateFactory.getInstance(validValues[i]);
assertEquals("Incorrect type", cerF.getType(), validValues[i]);
assertEquals("Incorrect provider", cerF.getProvider(), mProv);
checkResult(cerF, mode);
}
}
Aggregations