rules:
  - id: csharp-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: MD5.Create()
          - pattern: new MD5CryptoServiceProvider()
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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:
        - "SHA256"
        - "SHA384"
        - "SHA512"
        - "SPHINCS+"

  - id: csharp-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: SHA1.Create()
          - pattern: new SHA1CryptoServiceProvider()
          - pattern: new SHA1Managed()
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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: csharp-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: DES.Create()
          - pattern: new DESCryptoServiceProvider()
          - pattern: TripleDES.Create()
          - pattern: new TripleDESCryptoServiceProvider()
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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: csharp-weak-rsa-key-quantum
    patterns:
      - pattern-either:
        # Match variable declarations with type
        - pattern: |
            $TYPE $RSA = RSA.Create();
            ...
            $RSA.KeySize = 512;
        - pattern: |
            $TYPE $RSA = RSA.Create();
            ...
            $RSA.KeySize = 1024;
        - pattern: |
            $TYPE $RSA = RSA.Create();
            ...
            $RSA.KeySize = 2048;
        # Match simple assignments
        - pattern: |
            $RSA = RSA.Create();
            ...
            $RSA.KeySize = 512;
        - pattern: |
            $RSA = RSA.Create();
            ...
            $RSA.KeySize = 1024;
        - pattern: |
            $RSA = RSA.Create();
            ...
            $RSA.KeySize = 2048;
        # Match constructor calls
        - pattern: |
            $RSA = new RSACryptoServiceProvider(512);
        - pattern: |
            $RSA = new RSACryptoServiceProvider(1024);
        - pattern: |
            $RSA = new RSACryptoServiceProvider(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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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: 10
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "CRYSTALS-Kyber"
        - "FALCON"

  - id: csharp-aes-ecb-mode
    patterns:
      - pattern: |
          $AES.Mode = CipherMode.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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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

  - id: csharp-weak-random
    patterns:
      - pattern: new Random($SEED)
    message: >-
      Non-cryptographic PRNG used for security-sensitive operations -- output is predictable. Use a
      cryptographically secure random number generator.
    severity: WARNING
    languages: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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

  - id: csharp-hardcoded-crypto-key
    patterns:
      - pattern-either:
          # Detect private const/readonly string fields with literal values
          - pattern: private const string $KEY = "...";
          - pattern: private static readonly string $KEY = "...";
      - pattern-not: private const string $KEY = "";

    message: >-
      CRYPTOGRAPHIC VULNERABILITY: Hard-coded cryptographic keys are visible in source code and compiled
      assemblies, making them easy to extract. Particularly dangerous with "Harvest Now, Decrypt Later"
      quantum attacks.
    severity: ERROR
    languages: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - crypto
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: csharp-zero-iv
    patterns:
      - pattern-either:
        # Original pattern with intermediate variable
        - pattern: |
            $IV = new byte[$SIZE];
            ...
            $AES.IV = $IV;
        # Direct assignment without intermediate variable
        - pattern: |
            $CRYPTO.IV = new byte[$SIZE];

    message: >-
      AES-CBC without authentication is vulnerable to padding oracle and bit-flipping attacks. Use
      AES-256-GCM for authenticated encryption.
    severity: WARNING
    languages: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - crypto
      cwe:
        - "CWE-329: Generation of Predictable IV with CBC Mode"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: MEDIUM
      quantum_vulnerable: false
      quantum_risk_score: 5

  - id: csharp-rc4-usage
    patterns:
      - pattern-either:
          # Detect RC4CryptoServiceProvider usage
          - pattern: new RC4CryptoServiceProvider()
          # Detect manual RC4 KSA pattern (key scheduling algorithm)
          - patterns:
              - pattern-inside: |
                  byte[] $S = new byte[256];
                  ...
              - pattern: $J = ($J + $S[$I] + ...) % 256;
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_risk_score: 10

  - id: csharp-weak-key-derivation
    mode: taint
    pattern-sources:
      - pattern: $PASSWORD + $SALT
    pattern-sinks:
      - pattern: $HASH.ComputeHash(Encoding.$METHOD.GetBytes(...))
    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: WARNING
    languages: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - crypto
      cwe:
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 7

  - id: csharp-quantum-vulnerable-ecc
    patterns:
      - pattern-either:
          - pattern: ECDsa.Create()
          - pattern: ECDsa.Create(...)
          - pattern: new ECDsaCng()
          - pattern: new ECDsaCng(...)
          - pattern: ECDiffieHellman.Create()
          - pattern: ECDiffieHellman.Create(...)
          - pattern: new ECDiffieHellmanCng()
          - pattern: new ECDiffieHellmanCng(...)
          - pattern: CngKey.Create(CngAlgorithm.ECDsa, ...)
          - pattern: CngKey.Create(CngAlgorithm.ECDiffieHellman, ...)
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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: csharp-quantum-vulnerable-dsa
    patterns:
      - pattern-either:
          - pattern: DSA.Create()
          - pattern: DSA.Create(...)
          - pattern: new DSACryptoServiceProvider()
          - pattern: new DSACryptoServiceProvider(...)
          - pattern: new DSACng()
          - pattern: new DSACng(...)
          - pattern: CngKey.Create(CngAlgorithm.Dsa, ...)
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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: csharp-quantum-vulnerable-diffie-hellman
    patterns:
      - pattern-either:
          - pattern: ECDiffieHellman.Create()
          - pattern: ECDiffieHellman.Create(...)
          - pattern: new ECDiffieHellmanCng()
          - pattern: new ECDiffieHellmanCng(...)
    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: [csharp]
    metadata:
      category: security
      technology:
        - csharp
        - dotnet
        - 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

