rules:
  # =============================================================================
  # COBOL Quantum-Vulnerable Cryptography Detection Rules
  # =============================================================================
  # Detects cryptographic patterns in COBOL that are vulnerable to quantum
  # computer attacks, particularly on IBM z/OS mainframes using ICSF.
  #
  # Pattern discipline:
  #   - All patterns exclude COBOL comment lines (column 7 = '*')
  #   - Broad token patterns are anchored to operational verbs:
  #     CALL, PERFORM, DISPLAY, MOVE...VALUE, COMPUTE, FUNCTION
  #   - Variable declarations (PIC, VALUE in 01-level) are excluded
  #   - USING argument lines are excluded (they are covered by the CALL match)
  # =============================================================================

  # ---------------------------------------------------------------------------
  # DES/3DES Encryption (Vulnerable to Grover's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-des-encryption
    patterns:
      - pattern-either:
          # ICSF callable services -- precise, no change needed
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBENC['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNEENC['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBDEC['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNEDEC['\"]"
          # Operational lines only -- CALL, PERFORM, DISPLAY
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]DES-ENCRYPT['\"]"
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:DES.ENCRYPT|TDES.ENCRYPT|TRIPLE.DES)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:DES|3DES|TRIPLE.DES).*['\"]"
          # RULE-ARRAY VALUE literal -- the actual algorithm selection
          - pattern-regex: "^(?!.{6}\\*).*VALUE\\s+['\"]\\s*(?i:DES|3DES)\\s*['\"]"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, crypto]
      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
      post_quantum_alternatives:
        - "AES-256 (CSNBSAE/CSNBSAD)"
        - "CRYSTALS-Kyber (future ICSF support)"

  # ---------------------------------------------------------------------------
  # MD5 Hash Algorithm (Vulnerable to Grover's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-md5-hash
    patterns:
      - pattern-either:
          # ICSF CSNBOWH call with MD5 algorithm parameter
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBOWH['\"]"
          # Operational lines: PERFORM paragraph, DISPLAY message, MOVE to algorithm field
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:MD5|COMPUTE.MD5|MESSAGE.DIGEST)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:MD5).*['\"]"
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"]\\s*MD5\\s*['\"]\\s+TO\\s"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, 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_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-3"
        - "SHA-512"

  # ---------------------------------------------------------------------------
  # SHA-1 Hash Algorithm (Vulnerable to Grover's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-sha1-hash
    patterns:
      - pattern-either:
          # Operational lines only
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:SHA.?1|SECURE.HASH)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:SHA-?1).*['\"]"
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"]\\s*SHA-?1\\s*['\"]\\s+TO\\s"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, 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_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-3"

  # ---------------------------------------------------------------------------
  # RSA Encryption/Signatures (Vulnerable to Shor's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-rsa-crypto
    patterns:
      - pattern-either:
          # ICSF PKA callable services -- all precise CALL matches
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBPKE['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNEPKE['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBPKD['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNEPKD['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNDPKG['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBDSG['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNBDSV['\"]"
          # Operational: PERFORM or MOVE VALUE with RSA algorithm name
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:RSA)"
          - pattern-regex: "^(?!.{6}\\*).*VALUE\\s+['\"]\\s*RSA\\s*['\"]"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, 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)"
      references:
        - https://csrc.nist.gov/projects/post-quantum-cryptography
        - https://www.ibm.com/docs/en/zos/latest?topic=programming-icsf-application-programming

  # ---------------------------------------------------------------------------
  # ECC/ECDSA (Vulnerable to Shor's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-ecc-crypto
    patterns:
      - pattern-either:
          # ICSF ECC callable services
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNDEDH['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNFEDH['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNDDSG['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSNDDSV['\"]"
          # Operational: PERFORM paragraph with ECC/ECDSA/ECDH name
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:ECDSA|ECDH|ELLIPTIC.CURVE|ECC)"
          # MOVE VALUE with ECC algorithm literal
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"]\\s*(?i:ECC|PRIME256|SECP256|EC-P).*['\"]\\s+TO\\s"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, 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 (key exchange)"
        - "CRYSTALS-Dilithium (signatures)"
        - "FALCON (signatures)"

  # ---------------------------------------------------------------------------
  # Diffie-Hellman Key Exchange (Vulnerable to Shor's Algorithm)
  # ---------------------------------------------------------------------------
  - id: cobol-diffie-hellman
    patterns:
      - pattern-either:
          # Operational: PERFORM or DISPLAY with DH/Diffie-Hellman
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:DH.KEY|DIFFIE.HELLMAN)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:DIFFIE.HELLMAN|DH.KEY|DH.SHARED).*['\"]"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, 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"

  # ---------------------------------------------------------------------------
  # Hardcoded Cryptographic Keys
  # ---------------------------------------------------------------------------
  - id: cobol-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern-regex: "^(?!.{6}\\*).*01\\s+\\S*(?i:SECRET.KEY|ENCRYPTION.KEY|CRYPTO.KEY|AES.KEY|DES.KEY)\\S*\\s+PIC\\s+X\\([0-9]+\\)\\s+VALUE\\s+['\"][^'\"]{8,}['\"]"
    message: >-
      CRYPTOGRAPHIC VULNERABILITY: Hardcoded cryptographic key detected in COBOL. Keys embedded in source
      code are visible in compiled programs and can be extracted by anyone with access to the load module.
    severity: ERROR
    languages: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, crypto]
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  # ---------------------------------------------------------------------------
  # Weak Random Number Generation
  # ---------------------------------------------------------------------------
  - id: cobol-weak-random
    patterns:
      - pattern-either:
          - pattern-regex: "^(?!.{6}\\*).*COMPUTE\\s+\\S+\\s*=\\s*FUNCTION\\s+RANDOM"
    message: >-
      CRYPTOGRAPHIC VULNERABILITY: COBOL RANDOM function detected. The intrinsic RANDOM function is not
      cryptographically secure and should not be used for key generation, nonces, IVs, or any
      security-sensitive operations.
    severity: WARNING
    languages: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, crypto]
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  # ---------------------------------------------------------------------------
  # RC4 Stream Cipher (Broken)
  # ---------------------------------------------------------------------------
  - id: cobol-rc4-cipher
    patterns:
      - pattern-either:
          # Operational lines only: PERFORM paragraph, DISPLAY message
          # Single pattern covers RC4/ARC4/ARCFOUR to avoid multi-match per line
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:RC4|ARCFOUR|ARC4)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:RC4|ARCFOUR|ARC4).*['\"]"
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"].*(?i:RC4|ARCFOUR|ARC4).*['\"]\\s+TO\\s"
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, 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"
      references:
        - https://tools.ietf.org/html/rfc7465

  # ---------------------------------------------------------------------------
  # Blowfish Cipher (Deprecated)
  # ---------------------------------------------------------------------------
  - id: cobol-blowfish-cipher
    patterns:
      - pattern-either:
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:BLOWFISH|BLOW.FISH)"
          - pattern-regex: "^(?!.{6}\\*).*DISPLAY\\s+['\"].*(?i:BLOWFISH|BLOW.FISH).*['\"]"
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"].*(?i:BLOWFISH|BLOW.FISH).*['\"]\\s+TO\\s"
    message: >-
      Blowfish uses 64-bit blocks, making it vulnerable to birthday attacks, and provides no post-quantum
      security. Migrate to AES-256-GCM.
    severity: WARNING
    languages: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, crypto]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: MEDIUM
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7
      post_quantum_alternatives:
        - "AES-256"

  # ---------------------------------------------------------------------------
  # ICSF Callable Services (General Detection for Audit)
  # ---------------------------------------------------------------------------
  - id: cobol-icsf-crypto-usage
    patterns:
      - pattern-either:
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSN[A-Z]{1}[A-Z0-9]{3}['\"]"
          - pattern-regex: "^(?!.{6}\\*).*CALL\\s+['\"]CSF[A-Z]{1}[A-Z0-9]{3}['\"]"
    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: INFO
    languages: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, crypto]
      confidence: HIGH
      likelihood: LOW
      impact: LOW
      quantum_vulnerable: false
      quantum_risk_score: 0
      references:
        - https://www.ibm.com/docs/en/zos/latest?topic=guide-icsf-callable-services

  # ---------------------------------------------------------------------------
  # ECB Mode (Grover's Algorithm -- deterministic, pattern-leaking)
  # ---------------------------------------------------------------------------
  - id: cobol-ecb-mode
    patterns:
      - pattern-either:
          - pattern-regex: "^(?!.{6}\\*).*VALUE\\s+['\"]\\s*ECB\\s*['\"]"
          - pattern-regex: "^(?!.{6}\\*).*MOVE\\s+['\"]\\s*ECB\\s*['\"]\\s+TO"
          - pattern-regex: "^(?!.{6}\\*).*PERFORM\\s+\\S*(?i:ECB)"
    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: [generic]
    metadata:
      category: security
      technology: [cobol, mainframe, icsf, post-quantum]
      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
