use of javax.net.ssl.TrustManagerFactory in project OpenAttestation by OpenAttestation.
the class SslUtil method createX509TrustManagerWithCertificates.
public static X509TrustManager createX509TrustManagerWithCertificates(X509Certificate[] certificates) throws KeyManagementException {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(createTrustedSslKeystore(certificates));
TrustManager[] tms = tmf.getTrustManagers();
for (TrustManager tm : tms) {
if (tm instanceof X509TrustManager) {
return (X509TrustManager) tm;
}
}
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (IOException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (CertificateException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (UnrecoverableEntryException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
} catch (KeyStoreException e) {
throw new KeyManagementException("Cannot create X509TrustManager", e);
}
throw new IllegalArgumentException("TrustManagerFactory did not return an X509TrustManager instance");
}
use of javax.net.ssl.TrustManagerFactory in project qi4j-sdk by Qi4j.
the class AbstractSecureJettyTest method beforeSecureClass.
@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String string, SSLSession ssls) {
return true;
}
});
KeyStore truststore = KeyStore.getInstance("JCEKS");
truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
SSLContext sslCtx = SSLContext.getInstance("TLS");
TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
caTrustManagerFactory.init(truststore);
sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}
use of javax.net.ssl.TrustManagerFactory in project custom-cert-https by nelenkov.
the class MainActivity method dumpTrustedCerts.
private void dumpTrustedCerts() {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
X509TrustManager xtm = (X509TrustManager) tmf.getTrustManagers()[0];
StringBuffer buff = new StringBuffer();
for (X509Certificate cert : xtm.getAcceptedIssuers()) {
String certStr = "S:" + cert.getSubjectDN().getName() + "\nI:" + cert.getIssuerDN().getName();
Log.d(TAG, certStr);
buff.append(certStr + "\n\n");
}
resultText.setText(buff.toString());
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.TrustManagerFactory in project scdl by passy.
the class PinningTrustManagerImpl method initializeSystemTrustManagers.
private TrustManager[] initializeSystemTrustManagers() throws CertificateException {
try {
final TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init((KeyStore) null);
return tmf.getTrustManagers();
} catch (final NoSuchAlgorithmException nsae) {
throw new CertificateException(nsae);
} catch (final KeyStoreException e) {
throw new CertificateException(e);
}
}
use of javax.net.ssl.TrustManagerFactory in project spark by perwendel.
the class SparkTestUtil method getSslFactory.
/**
* Convenience method to use own truststore on SSL Sockets. Will default to
* the self signed keystore provided in resources, but will respect
* <p/>
* -Djavax.net.ssl.keyStore=serverKeys
* -Djavax.net.ssl.keyStorePassword=password
* -Djavax.net.ssl.trustStore=serverTrust
* -Djavax.net.ssl.trustStorePassword=password SSLApplication
* <p/>
* So these can be used to specify other key/trust stores if required.
*
* @return an SSL Socket Factory using either provided keystore OR the
* keystore specified in JVM params
*/
private SSLSocketFactory getSslFactory() {
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream fis = new FileInputStream(getTrustStoreLocation());
keyStore.load(fis, getTrustStorePassword().toCharArray());
fis.close();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
return ctx.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations