use of javax.net.ssl.TrustManager in project android_frameworks_base by ResurrectionRemix.
the class XmlConfigTests method testTrustManagerKeystore.
public void testTrustManagerKeystore() throws Exception {
XmlConfigSource source = new XmlConfigSource(getContext(), R.xml.bad_pin, true);
ApplicationConfig appConfig = new ApplicationConfig(source);
Provider provider = new NetworkSecurityConfigProvider();
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX", provider);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null);
int i = 0;
for (X509Certificate cert : SystemCertificateSource.getInstance().getCertificates()) {
keystore.setEntry(String.valueOf(i), new KeyStore.TrustedCertificateEntry(cert), null);
i++;
}
tmf.init(keystore);
TrustManager[] tms = tmf.getTrustManagers();
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tms, null);
TestUtils.assertConnectionSucceeds(context, "android.com", 443);
}
use of javax.net.ssl.TrustManager in project ats-framework by Axway.
the class InetSmtpConnection method getSSLSocketFactory.
/**
* Returns a configured SSLSocketFactory to use in creating new SSL
* sockets.
* @param tm an optional trust manager to use
*/
protected SSLSocketFactory getSSLSocketFactory(TrustManager tm) throws GeneralSecurityException {
if (tm == null) {
tm = new EmptyX509TrustManager();
}
SSLContext context = SSLContext.getInstance("TLS");
TrustManager[] trust = new TrustManager[] { tm };
context.init(null, trust, null);
return context.getSocketFactory();
}
use of javax.net.ssl.TrustManager in project CloudStack-archive by CloudStack-extras.
the class Link method initSSLContext.
public static SSLContext initSSLContext(boolean isClient) throws Exception {
InputStream stream;
SSLContext sslContext = null;
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
TrustManager[] tms;
if (!isClient) {
char[] passphrase = "vmops.com".toCharArray();
File confFile = PropertiesUtil.findConfigFile("db.properties");
/* This line may throw a NPE, but that's due to fail to find db.properities, meant some bugs in the other places */
String confPath = confFile.getParent();
String keystorePath = confPath + "/cloud.keystore";
if (new File(keystorePath).exists()) {
stream = new FileInputStream(keystorePath);
} else {
s_logger.warn("SSL: Fail to find the generated keystore. Loading fail-safe one to continue.");
stream = NioConnection.class.getResourceAsStream("/cloud.keystore");
}
ks.load(stream, passphrase);
stream.close();
kmf.init(ks, passphrase);
tmf.init(ks);
tms = tmf.getTrustManagers();
} else {
ks.load(null, null);
kmf.init(ks, null);
tms = new TrustManager[1];
tms[0] = new TrustAllManager();
}
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tms, null);
if (s_logger.isTraceEnabled()) {
s_logger.trace("SSL: SSLcontext has been initialized");
}
return sslContext;
}
use of javax.net.ssl.TrustManager in project midpoint by Evolveum.
the class ProtectorImpl method init.
/**
* @throws SystemException if jceks keystore is not available on {@link ProtectorImpl#getKeyStorePath}
*/
public void init() {
InputStream stream = null;
try {
// Test if use file or classpath resource
File f = new File(getKeyStorePath());
if (f.exists()) {
LOGGER.info("Using file keystore at {}", getKeyStorePath());
if (!f.canRead()) {
LOGGER.error("Provided keystore file {} is unreadable.", getKeyStorePath());
throw new EncryptionException("Provided keystore file " + getKeyStorePath() + " is unreadable.");
}
stream = new FileInputStream(f);
// Use class path keystore
} else {
LOGGER.warn("Using default keystore from classpath ({}).", getKeyStorePath());
// Read from class path
stream = ProtectorImpl.class.getClassLoader().getResourceAsStream(getKeyStorePath());
// class path
if (stream == null) {
stream = ProtectorImpl.class.getClassLoader().getResourceAsStream("com/../../" + getKeyStorePath());
}
}
// Test if we have valid stream
if (stream == null) {
throw new EncryptionException("Couldn't load keystore as resource '" + getKeyStorePath() + "'");
}
// Load keystore
keyStore.load(stream, getKeyStorePassword().toCharArray());
stream.close();
// Initialze trust manager list
TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmFactory.init(keyStore);
trustManagers = new ArrayList<TrustManager>();
for (TrustManager trustManager : tmFactory.getTrustManagers()) {
trustManagers.add(trustManager);
}
//init apache crypto library
Init.init();
} catch (Exception ex) {
LOGGER.error("Unable to work with keystore {}, reason {}.", new Object[] { getKeyStorePath(), ex.getMessage() }, ex);
throw new SystemException(ex.getMessage(), ex);
}
randomNumberGenerator = new SecureRandom();
}
use of javax.net.ssl.TrustManager in project android_frameworks_base by DirtyUnicorns.
the class X509TrustManagerExtensionsTest method testNormalUseCase.
public void testNormalUseCase() throws Exception {
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm);
String defaultKeystoreType = KeyStore.getDefaultType();
tmf.init(KeyStore.getInstance(defaultKeystoreType));
TrustManager[] tms = tmf.getTrustManagers();
for (TrustManager tm : tms) {
if (tm instanceof X509TrustManager) {
new X509TrustManagerExtensions((X509TrustManager) tm);
return;
}
}
fail();
}
Aggregations