Could not validate certificate signature?

后端 未结 5 781
误落风尘
误落风尘 2021-01-07 19:27

I use the SSL Socket and Trustmanager from this side Self signed SSL

but i keep getting following error:

09-28 19:52:41.942: WARN/System.err(1

相关标签:
5条回答
  • 2021-01-07 19:39

    Check the time of your device, correct it and then check again.

    0 讨论(0)
  • 2021-01-07 19:52

    In my case a following error raised on Android 4 and 5:

    Caused by: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: Certificate expired at Sat May 30 10:48:38 GMT+00:00 2020 (compared to Thu Aug 13 11:47:00 GMT+00:00 2020)

    ...

    Caused by: java.security.cert.CertificateExpiredException: Certificate expired at Sat May 30 10:48:38 GMT+00:00 2020 (compared to Thu Aug 13 11:47:00 GMT+00:00 2020)

    The server has certificate error (probably expired).

    For Retrofit see https://stackoverflow.com/a/60507560/2914140. If you use Fuel as a REST library, see kotlin library that can do httpS connection without certificate verification (like curl --insecure).

    You can trust all certificates, but it's dangerous.

    import java.security.SecureRandom
    import java.security.cert.X509Certificate
    import javax.net.ssl.*
    import javax.security.cert.CertificateException
    
    companion object {
    
        private val gson: Gson
        private val retrofit: Retrofit
    
        init {
    
            val okHttpClient = getOkHttpBuilder().build()
    
            gson = GsonBuilder().setLenient().create()
    
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
        }
    
        private fun getOkHttpBuilder(): OkHttpClient.Builder =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                OkHttpClient().newBuilder()
            } else {
                getUnsafeOkHttpClient()
            }
    
        private fun getUnsafeOkHttpClient(): OkHttpClient.Builder =
            try {
                // Create a trust manager that does not validate certificate chains
                val trustAllCerts: Array<TrustManager> = arrayOf(
                    object : X509TrustManager {
                        @Throws(CertificateException::class)
                        override fun checkClientTrusted(chain: Array<X509Certificate?>?,
                                                        authType: String?) = Unit
    
                        @Throws(CertificateException::class)
                        override fun checkServerTrusted(chain: Array<X509Certificate?>?,
                                                        authType: String?) = Unit
    
                        override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
                    }
                )
                // Install the all-trusting trust manager
                val sslContext: SSLContext = SSLContext.getInstance("SSL")
                sslContext.init(null, trustAllCerts, SecureRandom())
                // Create an ssl socket factory with our all-trusting manager
                val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory
                val builder = OkHttpClient.Builder()
                builder.sslSocketFactory(sslSocketFactory,
                    trustAllCerts[0] as X509TrustManager)
                builder.hostnameVerifier { _, _ -> true }
                builder
            } catch (e: Exception) {
                throw RuntimeException(e)
            }
    }
    

    See also https://stackoverflow.com/a/60507560/2914140 for Android version check and Glide connection.

    0 讨论(0)
  • 2021-01-07 20:02

    BTW,we could re-produce this error easily -- just change the date of the phone to several years later.

    NOTE: the error might be a little difference in different phone. Some might show that the certificate has expired.

    0 讨论(0)
  • 2021-01-07 20:04

    Most probably server returned certificate chain with authorities you do not trust. (means: authority certificates are not known to your device as trusted) Solution: carefully examine certificates coming from HTTPS website, and add respective authorities to your truststore - but this part seems to be tricky

    ( here some explanations : http://groups.google.com/group/android-security-discuss/browse_thread/thread/0bf726de4f5275a3/391b900631d7f358 )

    0 讨论(0)
  • 2021-01-07 20:05

    As Michael Levy mentioned, the reason I was getting this exception is that I had left my Android Emulator open for a few days and the clock had gotten pretty far out of sync. Once I restarted the emulator, the exception went away.

    0 讨论(0)
提交回复
热议问题