本文整理汇总了Java中javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm方法的典型用法代码示例。如果您正苦于以下问题:Java TrustManagerFactory.getDefaultAlgorithm方法的具体用法?Java TrustManagerFactory.getDefaultAlgorithm怎么用?Java TrustManagerFactory.getDefaultAlgorithm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.TrustManagerFactory
的用法示例。
在下文中一共展示了TrustManagerFactory.getDefaultAlgorithm方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTrustManager
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
/**
* Creates a X509TrustManagerImplementation backed up by the given key
* store. When null is passed as a key store, system default trust store is
* used. Returns null if no created TrustManager was suitable.
* @throws KeyStoreException, NoSuchAlgorithmException on error initializing the TrustManager.
*/
private static X509TrustManagerImplementation createTrustManager(KeyStore keyStore) throws
KeyStoreException, NoSuchAlgorithmException {
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init(keyStore);
for (TrustManager tm : tmf.getTrustManagers()) {
if (tm instanceof X509TrustManager) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return new X509TrustManagerJellyBean((X509TrustManager) tm);
} else {
return new X509TrustManagerIceCreamSandwich((X509TrustManager) tm);
}
} catch (IllegalArgumentException e) {
String className = tm.getClass().getName();
Log.e(TAG, "Error creating trust manager (" + className + "): " + e);
}
}
}
Log.e(TAG, "Could not find suitable trust manager");
return null;
}
示例2: create
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
private void create(Path path)
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
KeyManagementException {
TrustManager[] trustManagers;
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
installCertificates(path, keyStore);
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(defaultAlgorithm);
trustManagerFactory.init(keyStore);
trustManagers = trustManagerFactory.getTrustManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
trustManager = (X509TrustManager) trustManagers[0];
X509Certificate[] acceptedIssuers = trustManager.getAcceptedIssuers();
for (X509Certificate acceptedIssuer : acceptedIssuers) {
logger.info("installed cert details: subject={} issuer={}",
acceptedIssuer.getSubjectX500Principal(), acceptedIssuer.getIssuerX500Principal());
}
}
示例3: getTrustManagers
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
@Override
public TrustManager[] getTrustManagers() throws Exception {
String truststoreType = endpoint.getTruststoreType();
if (truststoreType == null) {
truststoreType = System.getProperty("javax.net.ssl.trustStoreType");
}
if (truststoreType == null) {
truststoreType = endpoint.getKeystoreType();
}
if (truststoreType == null) {
truststoreType = defaultKeystoreType;
}
String algorithm = endpoint.getTruststoreAlgorithm();
if (algorithm == null) {
algorithm = TrustManagerFactory.getDefaultAlgorithm();
}
return getTrustManagers(truststoreType, endpoint.getKeystoreProvider(),
algorithm);
}
示例4: createSSLContext
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
private SSLContext createSSLContext() throws GeneralSecurityException, IOException {
SSLContext sslContext;
if (provider != null)
sslContext = SSLContext.getInstance(protocol, provider);
else
sslContext = SSLContext.getInstance(protocol);
KeyManager[] keyManagers = null;
if (keystore != null) {
String kmfAlgorithm = this.kmfAlgorithm != null ? this.kmfAlgorithm : KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
KeyStore ks = keystore.load();
Password keyPassword = this.keyPassword != null ? this.keyPassword : keystore.password;
kmf.init(ks, keyPassword.value().toCharArray());
keyManagers = kmf.getKeyManagers();
}
String tmfAlgorithm = this.tmfAlgorithm != null ? this.tmfAlgorithm : TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
KeyStore ts = truststore == null ? null : truststore.load();
tmf.init(ts);
sslContext.init(keyManagers, tmf.getTrustManagers(), this.secureRandomImplementation);
return sslContext;
}
示例5: addCertificate
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
private SSLSocketFactory addCertificate(InputStream inputStream) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, KeyManagementException {
// loading CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca;
try {
ca = cf.generateCertificate(inputStream);
} finally {
inputStream.close();
}
// creating a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// creating a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// creating an SSLSocketFactory that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
return sslContext.getSocketFactory();
}
示例6: getTrustManagers
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
@Override
public TrustManager[] getTrustManagers() throws Exception {
String truststoreType = endpoint.getTruststoreType();
if (truststoreType == null) {
truststoreType = System.getProperty("javax.net.ssl.trustStoreType");
}
if (truststoreType == null) {
truststoreType = endpoint.getKeystoreType();
}
if (truststoreType == null) {
truststoreType = defaultKeystoreType;
}
String algorithm = endpoint.getTruststoreAlgorithm();
if (algorithm == null) {
algorithm = TrustManagerFactory.getDefaultAlgorithm();
}
return getTrustManagers(truststoreType, endpoint.getKeystoreProvider(), algorithm);
}
示例7: getDefaultSocketFactory
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
public static SSLSocketFactory getDefaultSocketFactory(InputStream inputStream) {
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca;
try {
ca = cf.generateCertificate(inputStream);
} finally {
inputStream.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
return sslContext.getSocketFactory();
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
return null;
}
示例8: getClient
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
public static OkHttpClient.Builder getClient(SatispayContext satispayContext) {
OkHttpClient.Builder okHttpClientBuilder;
okHttpClientBuilder = new OkHttpClient.Builder();
// ==> the SSL context is build only in environments different from PROD / STAGING, where the server cert is self signed
String serverCert = satispayContext.getServerCert();
if (serverCert != null) {
try {
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore;
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", CryptoUtils.certificateX509(serverCert));
String trustManagerDefaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerDefaultAlgorithm);
trustManagerFactory.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
okHttpClientBuilder.sslSocketFactory(sslSocketFactory);
} catch (Exception e) {
ProtoLogger.error("!!! Error generating TLS context !!!");
}
}
okHttpClientBuilder.connectTimeout(10, TimeUnit.SECONDS);
okHttpClientBuilder.writeTimeout(10, TimeUnit.SECONDS);
okHttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
return okHttpClientBuilder;
}
示例9: createClient
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
public static OkHttpClient createClient(Context context) {
OkHttpClient client = null;
CertificateFactory cf = null;
InputStream cert = null;
Certificate ca = null;
SSLContext sslContext = null;
try {
cf = CertificateFactory.getInstance("X.509");
cert = context.getResources().openRawResource(R.raw.public_key); // Place your 'my_cert.crt' file in `res/raw`
ca = cf.generateCertificate(cert);
cert.close();
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
client = new OkHttpClient.Builder()
.sslSocketFactory(sslContext.getSocketFactory())
.addInterceptor(new JwtTokenInterceptor())
.build();
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | KeyManagementException e) {
e.printStackTrace();
}
return client;
}
开发者ID:humaniq,项目名称:react-native-android-library-humaniq-api,代码行数:40,代码来源:SelfSigningClientBuilder.java
示例10: createSSLSocketFactory
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
/**
* create ssl socket factory. by target crt file.
* @param context the context.
* @param assetsFilePath the crt file path in assets.
* @return an instance of SSLSocketFactory.
*/
public static SSLSocketFactory createSSLSocketFactory(Context context, String assetsFilePath) {
SSLContext sslContext = null;
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
InputStream certificates = new BufferedInputStream(context.getAssets().open(assetsFilePath));
Certificate ca;
try {
ca = certificateFactory.generateCertificate(certificates);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
certificates.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
sslContext = SSLContext.getInstance("TLSv1", "AndroidOpenSSL");
// sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
} catch (Exception e) {
e.printStackTrace();
}
return sslContext != null ? sslContext.getSocketFactory() : null;
}
示例11: getDefaultTrustManager
import javax.net.ssl.TrustManagerFactory; //导入方法依赖的package包/类
/**
* Return the default TrustManager provided by the JVM.
* <p>
* This should be the same as the default used by {@link javax.net.ssl.SSLContext#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom)
* SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}
* when the TrustManager parameter is set to {@code null}
* @param keyStore the KeyStore to use, may be {@code null}
* @return the default TrustManager
* @throws java.security.GeneralSecurityException
*/
public static X509TrustManager getDefaultTrustManager(KeyStore keyStore) throws GeneralSecurityException {
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory instance = TrustManagerFactory.getInstance(defaultAlgorithm);
instance.init(keyStore);
return (X509TrustManager) instance.getTrustManagers()[0];
}