use of javax.net.ssl.TrustManager in project robovm by robovm.
the class MySslContext method test_getServerSocketFactory.
/**
* Test for <code>getServerSocketFactory()</code>
* <code>getSocketFactory()</code>
* <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
* methods Assertion: returns correspondent object
*
*/
public void test_getServerSocketFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
SSLContext[] sslC = createSSLCon();
assertNotNull("SSLContext objects were not created", sslC);
String tAlg = TrustManagerFactory.getDefaultAlgorithm();
String kAlg = KeyManagerFactory.getDefaultAlgorithm();
if (tAlg == null) {
fail("TrustManagerFactory default algorithm is not defined");
return;
}
if (kAlg == null) {
fail("KeyManagerFactory default algorithm is not defined");
return;
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
} catch (Exception e) {
fail(e + " was thrown for method load(null, null)");
}
kmf.init(ks, new char[10]);
KeyManager[] kms = kmf.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
tmf.init(ks);
TrustManager[] tms = tmf.getTrustManagers();
for (int i = 0; i < sslC.length; i++) {
sslC[i].init(kms, tms, new SecureRandom());
assertNotNull("No SSLServerSocketFactory available", sslC[i].getServerSocketFactory());
assertNotNull("No SSLSocketFactory available", sslC[i].getSocketFactory());
}
}
use of javax.net.ssl.TrustManager in project robovm by robovm.
the class SSLContextSpiTest method test_commonTest_02.
/**
* SSLContextSpi#engineCreateSSLEngine()
* SSLContextSpi#engineCreateSSLEngine(String host, int port)
* SSLContextSpi#engineGetClientSessionContext()
* SSLContextSpi#engineGetServerSessionContext()
* SSLContextSpi#engineGetServerSocketFactory()
* SSLContextSpi#engineGetSocketFactory()
*/
public void test_commonTest_02() {
SSLContextSpiImpl ssl = new SSLContextSpiImpl();
String defaultAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
try {
KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
char[] pass = "password".toCharArray();
kmf.init(null, pass);
KeyManager[] km = kmf.getKeyManagers();
defaultAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
trustMF.init(ks);
TrustManager[] tm = trustMF.getTrustManagers();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
ssl.engineInit(km, tm, sr);
} catch (Exception ex) {
fail(ex + " unexpected exception");
}
try {
assertNotNull("Subtest_01: Object is NULL", ssl.engineCreateSSLEngine());
SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", 1080);
assertNotNull("Subtest_02: Object is NULL", sleng);
assertEquals(sleng.getPeerPort(), 1080);
assertEquals(sleng.getPeerHost(), "localhost");
assertNull("Subtest_03: Object not NULL", ssl.engineGetClientSessionContext());
assertNull("Subtest_04: Object not NULL", ssl.engineGetServerSessionContext());
assertNull("Subtest_05: Object not NULL", ssl.engineGetServerSocketFactory());
assertNull("Subtest_06: Object not NULL", ssl.engineGetSocketFactory());
} catch (Exception e) {
fail("Unexpected exception " + e);
}
}
use of javax.net.ssl.TrustManager in project robovm by robovm.
the class SSLParametersImpl method createDefaultTrustManager.
private static X509TrustManager createDefaultTrustManager() throws KeyManagementException {
try {
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init((KeyStore) null);
TrustManager[] tms = tmf.getTrustManagers();
X509TrustManager trustManager = findX509TrustManager(tms);
return trustManager;
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException(e);
} catch (KeyStoreException e) {
throw new KeyManagementException(e);
}
}
use of javax.net.ssl.TrustManager in project muzei by romannurik.
the class OkHttpClientFactory method enableTls12.
/**
* Enable TLS on the OKHttp builder by setting a custom SocketFactory
*/
private static OkHttpClient.Builder enableTls12(OkHttpClient.Builder client) {
Log.i(TAG, "Enabling HTTPS compatibility mode");
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
client.sslSocketFactory(new TLSSocketFactory(), trustManager);
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1).build();
List<ConnectionSpec> specs = new ArrayList<>();
specs.add(cs);
specs.add(ConnectionSpec.COMPATIBLE_TLS);
specs.add(ConnectionSpec.CLEARTEXT);
client.connectionSpecs(specs);
} catch (Exception exc) {
Log.e(TAG, "Error while setting TLS", exc);
}
return client;
}
use of javax.net.ssl.TrustManager in project robovm by robovm.
the class HttpsURLConnectionTest method getContext.
/**
* Builds and returns the context used for secure socket creation.
*/
private static SSLContext getContext() throws Exception {
String type = KeyStore.getDefaultType();
String keyStore = getKeyStoreFileName();
File keyStoreFile = new File(keyStore);
FileInputStream fis = new FileInputStream(keyStoreFile);
KeyStore ks = KeyStore.getInstance(type);
ks.load(fis, KS_PASSWORD.toCharArray());
fis.close();
if (DO_LOG && false) {
TestKeyStore.dump("HttpsURLConnection.getContext", ks, KS_PASSWORD.toCharArray());
}
String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
kmf.init(ks, KS_PASSWORD.toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
String tmfAlgorthm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorthm);
tmf.init(ks);
TrustManager[] trustManagers = tmf.getTrustManagers();
if (DO_LOG) {
trustManagers = TestTrustManager.wrap(trustManagers);
}
SSLContext ctx = SSLContext.getInstance("TLSv1");
ctx.init(keyManagers, trustManagers, null);
return ctx;
}
Aggregations