Мне нужно добавить ssl-сертификат в мои запросы на модернизацию. У меня есть file_cer.crt с сертификатом и file_key с "----- BEGIN PRIVATE KEY -----"
Вот мой код
fun addCustomCertificateOkHttpClient(ctx: Context, okHttpClient: OkHttpClient.Builder): OkHttpClient.Builder{ // loading CAs from an InputStream
val ips: InputStream = ctx.resources.openRawResource(R.raw.custom_crt)
val size = ips.available()
val buffer = ByteArray(size) //declare the size of the byte array with size of the file
ips.read(buffer) //read file
ips.close() //close file
// Store text file data in the string variable
val str_data = String(buffer).replace("\r\n","").replace("\n","")
val stream: InputStream = ByteArrayInputStream(str_data.toByteArray())
val bis = BufferedInputStream(stream)
val cf: CertificateFactory = CertificateFactory.getInstance("X.509")
val ca: Certificate?
ca = try {
cf.generateCertificate(bis)
} catch (ex:Exception){
Log.d("Tag",ex.toString()) // **here I've got an exception**
null
}
finally {
stream.close()
}
// creating a KeyStore containing our trusted CAs
val keyStoreType: String = KeyStore.getDefaultType()
val keyStore: KeyStore = KeyStore.getInstance(keyStoreType)
keyStore.load(null, null)
keyStore.setCertificateEntry("ca", ca)
// creating a TrustManager that trusts the CAs in our KeyStore
val tmfAlgorithm: String = TrustManagerFactory.getDefaultAlgorithm()
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance(tmfAlgorithm)
tmf.init(keyStore)
// creating an SSLSocketFactory that uses our TrustManager
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, tmf.getTrustManagers(), null)
// creating an OkHttpClient that uses our SSLSocketFactory
okHttpClient.sslSocketFactory(sslContext.socketFactory, tmf.getTrustManagers().first() as X509TrustManager)
return okHttpClient
}
Выход исключения
com. android .org.conscrypt.OpenSSLX509CertificateFactory $ ParsingException: com. android .org.conscrypt.OpenSSLX509CertificateFactory $ ParsingException: * 1013untimeg. : ошибка: 0c0000be: процедуры кодирования ASN.1: OPENSSL_internal: WRONG_TAG
Я уже просмотрел эту топи c, хотя SO и сеть, и я не знаю, что мне следует попробовать