이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: kotlin-security/verify-ssl-certificates

Language: Kotlin

Severity: Error

Category: Security

CWE: 295

Description

This rule mandates that SSL/TLS certificates always be validated. Certificate validation is an essential part of the SSL/TLS protocol that ensures the server you are communicating with is indeed who it claims to be. This prevents man-in-the-middle attacks, where an attacker intercepts and possibly alters the communication between two parties without their knowledge.

Ignoring or bypassing certificate validation severely undermines the security of your application and should be avoided.

To adhere to this rule, always use the system’s default SSLSocketFactory and TrustManager for SSL/TLS connections. These default settings perform certificate validation automatically. Never attempt to bypass or disable certificate validation. If you need to trust a self-signed certificate for testing purposes, add it to a custom trust store and use that instead of bypassing all certificate validation.

Non-Compliant Code Examples

import javax.net.ssl.*
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import java.security.KeyStore

class InsecureTlsConfigurations {
    // Pattern 1: Bypass certificate validation in OkHttpClient
    fun createInsecureOkHttpClient(): OkHttpClient {
        val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
            override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
        })

        val sslContext = SSLContext.getInstance("TLS").apply {
            init(null, trustAllCerts, java.security.SecureRandom())
        }

        return OkHttpClient.Builder()
            .sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
            .build()
    }

    // Pattern 2: Bypass in HttpsURLConnection
    fun disableUrlConnectionValidation() {
        val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
            override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
        })

        val sslContext = SSLContext.getInstance("TLS").apply {
            init(null, trustAllCerts, java.security.SecureRandom())
        }

        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)
    }
}

Compliant Code Examples

import javax.net.ssl.*
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import java.security.KeyStore

class SecureTlsConfigurations {
    // Pattern 1: OkHttpClient with proper validation
    fun createSecureOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            // Uses system default SSLSocketFactory and TrustManager
            .build()
    }

    // Pattern 2: HttpsURLConnection with proper validation
    fun createSecureUrlConnection(urlString: String): HttpsURLConnection {
        val url = URL(urlString)
        val connection = url.openConnection() as HttpsURLConnection
        // Uses system default SSLSocketFactory and trust manager
        // No need to override any SSL settings
        return connection
    }
}