rules:
  - id: kotlin-tls-version-too-old
    patterns:
      - pattern-either:
          - pattern: SSLContext.getInstance("TLSv1")
          - pattern: SSLContext.getInstance("TLSv1.1")
          - pattern: SSLContext.getInstance("SSLv3")
          - pattern: SSLContext.getInstance("SSLv2")
          - pattern: SSLContext.getInstance("SSL")
    message: >-
      TLS 1.0/1.1 use broken cipher suites (RC4, 3DES, EXPORT) and are prohibited by RFC 8996. Upgrade to
      TLS 1.3.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology: [kotlin, jvm, tls, post-quantum]
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Blocks PQC migration"
      quantum_risk_score: 8

  - id: kotlin-tls-cert-validation-disabled
    patterns:
      - pattern-either:
          - pattern: |
              $CONN.setHostnameVerifier { $HOST, $SESSION -> true }
          - pattern: HttpsURLConnection.setDefaultHostnameVerifier { $HOST, $SESSION -> true }
          - pattern: |
              val $TM = arrayOf(object : X509TrustManager {
                  override fun checkServerTrusted(...) {}
                  ...
              })
    message: >-
      SECURITY VULNERABILITY: TLS certificate or hostname validation is disabled. This allows
      man-in-the-middle attacks and completely defeats TLS security.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology: [kotlin, jvm, tls]
      cwe:
        - "CWE-295: Improper Certificate Validation"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: kotlin-tls-weak-cipher-suite
    patterns:
      - pattern-either:
          - pattern: $ENGINE.enabledCipherSuites = arrayOf("SSL_RSA_WITH_RC4_128_SHA", ...)
          - pattern: $ENGINE.enabledCipherSuites = arrayOf("SSL_RSA_WITH_DES_CBC_SHA", ...)
          - pattern: $ENGINE.enabledCipherSuites = arrayOf("TLS_RSA_WITH_NULL_SHA", ...)
          - pattern: $SOCKET.enabledCipherSuites = arrayOf("SSL_RSA_WITH_RC4_128_SHA", ...)
    message: >-
      Explicit weak cipher suite configuration bypasses secure TLS defaults. Use TLS 1.3 which negotiates
      cipher suites automatically.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology: [kotlin, jvm, tls, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Blocks PQC migration"
      quantum_risk_score: 8
