rules:
  - id: java-weak-rsa-key-size
    patterns:
      - pattern-either:
          - patterns:
              - pattern: |
                  $KG.initialize($SIZE);
              - pattern-inside: |
                  KeyPairGenerator $KG = KeyPairGenerator.getInstance("RSA");
                  ...
              - metavariable-comparison:
                  metavariable: $SIZE
                  comparison: $SIZE < 2048
          - patterns:
              - pattern: |
                  $KG.initialize($SIZE, ...);
              - pattern-inside: |
                  KeyPairGenerator $KG = KeyPairGenerator.getInstance("RSA");
                  ...
              - metavariable-comparison:
                  metavariable: $SIZE
                  comparison: $SIZE < 2048
    message: >-
      QUANTUM VULNERABILITY: RSA key size $SIZE bits is too small. RSA is broken
      by Shor's algorithm regardless of key size. Migrate to CRYSTALS-Kyber (ML-KEM)
      for key encapsulation or CRYSTALS-Dilithium (ML-DSA) for signatures.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10

  - id: java-des-cipher
    pattern-either:
      - pattern: Cipher.getInstance("DES")
      - pattern: Cipher.getInstance("DES/...")
      - pattern: Cipher.getInstance("DESede")
      - pattern: Cipher.getInstance("DESede/...")
      - pattern: SecretKeyFactory.getInstance("DES")
      - pattern: SecretKeyFactory.getInstance("DESede")
      - pattern: new DESKeySpec(...)
      - pattern: new DESedeKeySpec(...)
    message: >-
      QUANTUM VULNERABILITY: DES/3DES is broken classically and quantum-vulnerable.
      DES has a 56-bit key. 3DES is deprecated by NIST (2023).
      Use AES-256-GCM instead.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10

  - id: java-md5-hash
    pattern-either:
      - pattern: MessageDigest.getInstance("MD5")
      - pattern: MessageDigest.getInstance("MD2")
    message: >-
      QUANTUM VULNERABILITY: MD5/MD2 is cryptographically broken and quantum-vulnerable
      via Grover's algorithm. Use SHA-256 or SHA-3 instead.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: java-sha1-hash
    pattern-either:
      - pattern: MessageDigest.getInstance("SHA-1")
      - pattern: MessageDigest.getInstance("SHA1")
      - pattern: Signature.getInstance("SHA1withRSA")
      - pattern: Signature.getInstance("SHA1withDSA")
      - pattern: Signature.getInstance("SHA1withECDSA")
    message: >-
      QUANTUM VULNERABILITY: SHA-1 is deprecated and quantum-vulnerable via
      Grover's algorithm. Use SHA-256 or SHA-3 instead.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: java-rc4-cipher
    pattern-either:
      - pattern: Cipher.getInstance("RC4")
      - pattern: Cipher.getInstance("ARCFOUR")
    message: >-
      RC4 is completely broken and banned by RFC 7465. Use AES-256-GCM instead.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_risk_score: 10

  - id: java-aes-small-key
    patterns:
      - pattern-either:
          - patterns:
              - pattern: $KG.init($SIZE)
              - pattern-inside: |
                  KeyGenerator $KG = KeyGenerator.getInstance("AES");
                  ...
              - metavariable-comparison:
                  metavariable: $SIZE
                  comparison: $SIZE < 256
          - patterns:
              - pattern: $KG.init($SIZE, ...)
              - pattern-inside: |
                  KeyGenerator $KG = KeyGenerator.getInstance("AES");
                  ...
              - metavariable-comparison:
                  metavariable: $SIZE
                  comparison: $SIZE < 256
    message: >-
      QUANTUM VULNERABILITY: AES key size $SIZE bits is insufficient for post-quantum security.
      AES-128 provides 64-bit and AES-192 provides 96-bit quantum security via Grover's algorithm.
      Use AES-256 (256-bit key) for 128-bit quantum security.
    severity: WARNING
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7

  - id: java-dsa-quantum-vulnerable
    pattern-either:
      - pattern: KeyPairGenerator.getInstance("DSA")
      - pattern: Signature.getInstance("SHA256withDSA")
      - pattern: Signature.getInstance("SHA1withDSA")
    message: >-
      QUANTUM VULNERABILITY: DSA is broken by Shor's algorithm.
      Migrate to CRYSTALS-Dilithium (ML-DSA) for digital signatures.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10

  - id: java-dh-quantum-vulnerable
    pattern-either:
      - pattern: KeyPairGenerator.getInstance("DH")
      - pattern: KeyAgreement.getInstance("DH")
      - pattern: AlgorithmParameterGenerator.getInstance("DH")
    message: >-
      QUANTUM VULNERABILITY: Diffie-Hellman key exchange is broken by Shor's
      algorithm. Migrate to CRYSTALS-Kyber (ML-KEM) for key encapsulation.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10

  - id: java-blowfish-cipher
    pattern-either:
      - pattern: Cipher.getInstance("Blowfish")
      - pattern: Cipher.getInstance("Blowfish/...")
      - pattern: SecretKeyFactory.getInstance("Blowfish")
    message: >-
      Blowfish uses 64-bit blocks, making it vulnerable to birthday attacks, and provides no post-quantum
      security. Migrate to AES-256-GCM.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7

  - id: java-cbc-padding-oracle
    pattern-either:
      - pattern: Cipher.getInstance("AES/CBC/NoPadding")
      - pattern: Cipher.getInstance("AES/CBC/PKCS5Padding")
      - pattern: Cipher.getInstance("AES/CBC/PKCS7Padding")
      - pattern: Cipher.getInstance("DES/CBC/...")
      - pattern: Cipher.getInstance("DESede/CBC/...")
      - patterns:
          - pattern: Cipher.getInstance($ALGO)
          - metavariable-regex:
              metavariable: $ALGO
              regex: '.*\/CBC\/.*'
    message: >-
      AES-CBC without authentication is vulnerable to padding oracle and bit-flipping attacks. Use
      AES-256-GCM for authenticated encryption.
    severity: WARNING
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-649: Reliance on Obfuscation or Encryption without Integrity Checking"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: false
      quantum_risk_score: 6

  - id: java-static-iv
    pattern-either:
      - pattern: new IvParameterSpec(new byte[$SIZE])
      - pattern: new IvParameterSpec(new byte[]{...})
    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: [java]
    metadata:
      category: security
      cwe:
        - "CWE-329: Generation of Predictable IV with CBC Mode"
        - "CWE-330: Use of Insufficiently Random Values"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: java-ecb-mode
    patterns:
      - pattern-either:
          - pattern: Cipher.getInstance("AES/ECB/NoPadding")
          - pattern: Cipher.getInstance("AES/ECB/PKCS5Padding")
          - pattern: Cipher.getInstance("AES/ECB/PKCS7Padding")
          - pattern: Cipher.getInstance("DES/ECB/NoPadding")
          - pattern: Cipher.getInstance("DES/ECB/PKCS5Padding")
          - pattern: Cipher.getInstance("DESede/ECB/NoPadding")
          - pattern: Cipher.getInstance("DESede/ECB/PKCS5Padding")
          - patterns:
              - pattern: Cipher.getInstance($ALGO)
              - metavariable-regex:
                  metavariable: $ALGO
                  regex: '.*\/ECB\/.*'
    message: >-
      AES in ECB mode leaks data patterns -- identical plaintext blocks produce identical ciphertext. Use
      AES-256-GCM for authenticated encryption.
    severity: ERROR
    languages: [java]
    metadata:
      category: security
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 8
