rules:
  - id: kotlin-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: |
              val $SPEC = DESKeySpec(...)
          - pattern: |
              $KF.generateSecret(DESKeySpec(...))
          - pattern: Cipher.getInstance("DES")
          - pattern: Cipher.getInstance("DES/...")
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10

  - id: kotlin-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: |
              val $SPEC = DESedeKeySpec(...)
          - pattern: |
              $KF.generateSecret(DESedeKeySpec(...))
          - pattern: Cipher.getInstance("DESede")
          - pattern: Cipher.getInstance("DESede/...")
    message: >-
      3DES has 112-bit effective key strength -- broken classically and quantum-vulnerable. NIST deprecated
      3DES in 2023. Migrate to AES-256-GCM.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 8

  - id: kotlin-weak-random-generation
    patterns:
      - pattern-either:
          - pattern: Random(System.currentTimeMillis())
          - pattern: Random($SEED)
          - pattern: java.util.Random()
    message: >-
      Non-cryptographic PRNG used for security-sensitive operations -- output is predictable. Use a
      cryptographically secure random number generator.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: kotlin-kotlin-random-for-crypto
    patterns:
      - pattern-either:
          - pattern: |
              ByteArray($SIZE) { Random.nextInt(...).toByte() }
          - pattern: Random.nextInt(...)
          - pattern: Random.nextLong(...)
          - pattern: Random.nextBytes(...)
    message: >-
      CRYPTOGRAPHIC VULNERABILITY: Using kotlin.random.Random for cryptographic purposes. This is not
      cryptographically secure.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: kotlin-static-iv
    patterns:
      - pattern-either:
          - pattern: |
              val $IV = ByteArray($SIZE) { 0 }
              ...
              val $IVSPEC = IvParameterSpec($IV)
          - pattern: IvParameterSpec(ByteArray(...))
    message: >-
      Static or hardcoded IV breaks IND-CPA security -- identical plaintexts produce identical ciphertext.
      Generate a fresh random IV for every encryption operation.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-329: Not Using a Random IV with CBC Mode"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: kotlin-hardcoded-key
    patterns:
      - pattern-either:
          - pattern: |
              const val SECRET_KEY = "..."
          - pattern: |
              val HARDCODED_PASSWORD = "..."
          - pattern: |
              val HARDCODED_IV = ByteArray(...)
    message: >-
      CRYPTOGRAPHIC VULNERABILITY: Hard-coded encryption key or password detected. Hard-coded credentials
      can be extracted from the binary.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A07:2021 - Identification and Authentication Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: kotlin-weak-key-derivation
    mode: taint
    pattern-sources:
      - pattern: "$PASSWORD + $SALT"
      - pattern: "\"$PASSWORD$SALT\""
    pattern-sinks:
      - pattern: MessageDigest.getInstance(...).digest(...)
    message: >-
      Insufficient PBKDF2 iterations make the derived key brute-forceable. Use at least 310,000 iterations
      (NIST SP 800-132) or switch to Argon2.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: kotlin-sql-injection
    patterns:
      - pattern-either:
          - pattern: "\"SELECT * FROM $TABLE WHERE $COL = $VAR\""
          - pattern: "\"INSERT INTO $TABLE ... VALUES ($VAL)\""
          - pattern: "\"UPDATE $TABLE SET ... WHERE $COL = $VAR\""
          - pattern: "\"DELETE FROM $TABLE WHERE $COL = $VAR\""
    message: >-
      SQL INJECTION VULNERABILITY: String interpolation in SQL queries can lead to SQL injection attacks.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - sql
      cwe:
        - "CWE-89: SQL Injection"
      owasp:
        - A03:2021 - Injection
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: kotlin-command-injection
    patterns:
      - pattern-either:
          - pattern: Runtime.getRuntime().exec("... $VAR ...")
          - pattern: Runtime.getRuntime().exec("$CMD")
          - pattern: ProcessBuilder("$CMD", ...)
    message: >-
      COMMAND INJECTION VULNERABILITY: Executing commands with user-controlled input can lead to command
      injection.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
      cwe:
        - "CWE-78: OS Command Injection"
      owasp:
        - A03:2021 - Injection
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: kotlin-path-traversal
    patterns:
      - pattern-either:
          - pattern: java.io.File("...$VAR...").readText()
          - pattern: java.io.File("...$VAR...").readBytes()
          - pattern: File("...$PATH...$VAR...")
    message: >-
      RSA is broken by Shor's algorithm regardless of key size. Migrate to ML-KEM (FIPS 203) for key
      encapsulation or ML-DSA (FIPS 204) for signatures.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
      cwe:
        - "CWE-22: Path Traversal"
      owasp:
        - A01:2021 - Broken Access Control
      confidence: MEDIUM
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: kotlin-force-unwrap-operator
    patterns:
      - pattern-either:
          - pattern: $VAR!!
          - pattern: $VAR!!.toUpperCase()
          - pattern: $MAP[$KEY]!!
    message: >-
      KOTLIN SAFETY VIOLATION: Using the !! force unwrap operator can cause NullPointerException at
      runtime.
    severity: WARNING
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
      cwe:
        - "CWE-476: NULL Pointer Dereference"
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: MEDIUM
      quantum_vulnerable: false
      quantum_risk_score: 4

  - id: kotlin-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: MessageDigest.getInstance("MD5")
          - pattern: MessageDigest.getInstance("md5")
    message: >-
      MD5 is cryptographically broken and quantum-weakened by Grover's algorithm -- effective security
      drops from 128 to 64 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-384"
        - "SHA-512"
        - "SPHINCS+"

  - id: kotlin-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: MessageDigest.getInstance("SHA-1")
          - pattern: MessageDigest.getInstance("SHA1")
          - pattern: MessageDigest.getInstance("sha-1")
          - pattern: MessageDigest.getInstance("sha1")
    message: >-
      SHA-1 is deprecated and quantum-weakened by Grover's algorithm -- effective security drops from 160
      to 80 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: kotlin-quantum-vulnerable-ecc
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("EC")
          - pattern: KeyPairGenerator.getInstance("ECDSA")
          - pattern: KeyPairGenerator.getInstance("ECDH")
          - pattern: Signature.getInstance("SHA256withECDSA")
          - pattern: Signature.getInstance("SHA384withECDSA")
          - pattern: Signature.getInstance("SHA512withECDSA")
          - pattern: KeyAgreement.getInstance("ECDH")
    message: >-
      ECC (ECDSA/ECDH) is broken by Shor's algorithm -- all curves (P-256, P-384, secp256k1, Curve25519)
      are equally vulnerable regardless of key size. Migrate: ECDSA  ML-DSA (FIPS 204)  ECDH  ML-KEM
      (FIPS 203).
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber (encryption/KEM)"
        - "CRYSTALS-Dilithium (signatures)"
        - "FALCON (signatures)"
        - "SPHINCS+ (hash-based signatures)"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography
        - https://nvlpubs.nist.gov/nistpubs/ir/2024/NIST.IR.8547.ipd.pdf

  - id: kotlin-quantum-vulnerable-dsa
    patterns:
      - pattern-either:
          - pattern: KeyPairGenerator.getInstance("DSA")
          - pattern: Signature.getInstance("SHA1withDSA")
          - pattern: Signature.getInstance("SHA256withDSA")
    message: >-
      DH/DSA is broken by Shor's algorithm. Migrate to ML-KEM (FIPS 203) for key exchange or ML-DSA (FIPS
      204) for signatures.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "FALCON"
        - "SPHINCS+"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography

  - id: kotlin-quantum-vulnerable-diffie-hellman
    patterns:
      - pattern-either:
          - pattern: KeyAgreement.getInstance("DH")
          - pattern: KeyAgreement.getInstance("DiffieHellman")
          - pattern: KeyPairGenerator.getInstance("DH")
          - pattern: KeyPairGenerator.getInstance("DiffieHellman")
    message: >-
      DH/DSA is broken by Shor's algorithm. Migrate to ML-KEM (FIPS 203) for key exchange or ML-DSA (FIPS
      204) for signatures.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Kyber"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography
        - https://www.rfc-editor.org/rfc/rfc9180.html

  - id: kotlin-weak-rsa-key-quantum
    patterns:
      - pattern-either:
          - pattern: |
              val $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(512)
          - pattern: |
              val $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(1024)
          - pattern: |
              val $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(2048)
          - pattern: |
              $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(512)
          - pattern: |
              $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(1024)
          - pattern: |
              $KPG = KeyPairGenerator.getInstance("RSA")
              ...
              $KPG.initialize(2048)
    message: >-
      RSA is broken by Shor's algorithm regardless of key size. Migrate to ML-KEM (FIPS 203) for key
      encapsulation or ML-DSA (FIPS 204) for signatures.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
        - post-quantum
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "CRYSTALS-Kyber"
        - "FALCON"

  - id: kotlin-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("RC4")
          - pattern: Cipher.getInstance("ARCFOUR")
          - pattern: Cipher.getInstance("ARC4")
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "AES-256-GCM"
        - "ChaCha20-Poly1305"

  - id: kotlin-aes-ecb-mode
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("AES/ECB/...")
          - pattern: Cipher.getInstance("AES")
    message: >-
      AES in ECB mode leaks data patterns -- identical plaintext blocks produce identical ciphertext. Use
      AES-256-GCM for authenticated encryption.
    severity: ERROR
    languages: [kotlin]
    metadata:
      category: security
      technology:
        - kotlin
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 6
