Я хочу добавить доверенный сертификат с URL-соединением, используя хранилище ключей. Ниже мой код ...
try {
val url = URL("https://-------.com")
val sslCtx = SSLContext.getInstance("TLS")
sslCtx.init(null, arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private var accepted: Array<X509Certificate>? = null
override fun checkServerTrusted(xcs: Array<X509Certificate>, string: String) {
accepted = xcs
}
override fun getAcceptedIssuers(): Array<X509Certificate>? {
return accepted
}
}), null)
val connection = url.openConnection() as HttpsURLConnection
connection.hostnameVerifier = HostnameVerifier { string, ssls -> true }
connection.sslSocketFactory = sslCtx.socketFactory
val keyStoreType = KeyStore.getDefaultType()
val keyStore = KeyStore.getInstance(keyStoreType)
val `in` = resources.openRawResource(R.raw.gd_bundle)
keyStore.load(null, "12345678".toCharArray())
Thread(Runnable {
try {
connection.connect()
// Your implementation
Log.e("http.responseCode", "" + connection.responseCode)
if (connection.responseCode == 200) {
val certificates = connection.serverCertificates
println("cert added")
ca = certificates[0]
Log.e("getAlgorithm", "" + ca!!.publicKey.algorithm)
Log.e("getFormat", "" + ca!!.publicKey.format)
}
keyStore.setCertificateEntry("MEDITAB", ca)
// Create a TrustManager that trusts the CAs in our KeyStore
val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
val tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
tmf.init(keyStore)
Log.e("SSSSSSSSSSSSS", "" + tmf.toString())
// Create an SSLContext that uses our TrustManager
context1 = SSLContext.getInstance("TLS")
context1!!.init(null, tmf.trustManagers, null)
if (connection.responseCode == 200) {
// InputStream in = getResources().openRawResource(R.raw.gd_bundle);
// System.out.println(in.read());
// String line;
// BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// StringBuilder out = new StringBuilder();
// while ((line = reader.readLine()) != null) {
// out.append(line);
// }
// System.out.println(out.toString());
Executors.newSingleThreadExecutor().execute {
try {
Log.e("http.responseCodeeeeeeeeeeeeeeeeeeeeeeee", "" + connection.responseCode)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}).start()
connection.disconnect()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: CertificateEncodingException) {
e.printStackTrace()
} catch (e: KeyManagementException) {
e.printStackTrace()
} catch (e: CertificateException) {
e.printStackTrace()
} catch (e: KeyStoreException) {
e.printStackTrace()
} catch (e: UnrecoverableEntryException) {
e.printStackTrace()
}
Как сделать вышеуказанный код общим для всех URL. А также я хочу знать, как проверить этот добавленный сертификат привязки доверия в устройстве?
Пожалуйста, помогите