Skip to content

Quantum: Initial support for C# #19905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9decd51
Initial quantum support for C#
fegge Jun 16, 2025
66eee73
Fixed issues in C# Language.qll
fegge Jun 16, 2025
69e6308
Added C# query to generate CBOM graph
fegge Jun 19, 2025
07a3032
quantum-c#: initial support for ECDSA and RSA signatures
fcasal Jun 19, 2025
36213c5
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 19, 2025
d690a34
quantum-c#: make type precise
fcasal Jun 19, 2025
9970e58
quantum-c#: add HashAlgorithms
fcasal Jun 19, 2025
1762959
quantum-c#: Add HashAlgorithmInstance
fcasal Jun 23, 2025
01b98fc
quantum-c#: Add generic dataflow module
fcasal Jun 23, 2025
3913f44
quanntum-c#: refactor class names
fcasal Jun 23, 2025
b1bbd97
Added initial support for C# block ciphers
fegge Jun 23, 2025
791c260
quantum-c#: Add HashAlgorithmInstance
fcasal Jun 23, 2025
2642e32
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 23, 2025
6d0024d
quantum-c#: refactoring
fcasal Jun 23, 2025
c7caf97
Extended CryptoStreamKeyOperationInstance
fegge Jun 23, 2025
a3e93ce
quantum-c#: add hash operation instance
fcasal Jun 24, 2025
e0430a7
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 24, 2025
9238b12
Added support for the system CSPRNG
fegge Jun 24, 2025
46c037c
Added unit tests for C# block cipher modes
fegge Jun 24, 2025
a7c5f7a
refactoring
fcasal Jun 24, 2025
50c98f6
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 24, 2025
3e70e8e
Updated Stream related data flow
fegge Jun 24, 2025
e488a74
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 24, 2025
f3c436a
quantum-c#: Use stream flows for the HashOperationInstance
fcasal Jun 24, 2025
45ae4d1
Added support for AesGcm and AesCcm
fegge Jun 25, 2025
e643cc4
Updated test suite with AesGcm and AesCcm tests
fegge Jun 25, 2025
5fc267a
Added support for ChaCha20Poly1305
fegge Jun 25, 2025
e344505
quantum-c#: refactor AVCs for hashes and signatures.
fcasal Jun 25, 2025
601d5d1
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 25, 2025
298d226
Added support for bare symmetric algorithms
fegge Jun 25, 2025
2d4af33
quantum-c#: add tests for signatures
fcasal Jun 25, 2025
7c72a65
Add RSAPKCS1Signature formatters
fcasal Jun 25, 2025
7d7b8b2
Merge branch 'tob/quantum-csharp' of github.com:trailofbits/codeql in…
fcasal Jun 25, 2025
39581e2
quantum-c#: add support for RSAPKC1Signature formatters
fcasal Jun 26, 2025
6763f18
quantum-c#: Add hash tests
fcasal Jun 26, 2025
b417436
quantum-c#: Add support for HMACs
fcasal Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions csharp/ql/lib/experimental/quantum/Language.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
private import csharp as Language
private import semmle.code.csharp.dataflow.DataFlow
private import codeql.quantum.experimental.Model

private class UnknownLocation extends Language::Location {
UnknownLocation() { this.getFile().getAbsolutePath() = "" }
}

/**
* A dummy location which is used when something doesn't have a location in
* the source code but needs to have a `Location` associated with it. There
* may be several distinct kinds of unknown locations. For example: one for
* expressions, one for statements and one for other program elements.
*/
private class UnknownDefaultLocation extends UnknownLocation {
UnknownDefaultLocation() { locations_default(this, _, 0, 0, 0, 0) }
}

module CryptoInput implements InputSig<Language::Location> {
class DataFlowNode = Language::DataFlow::Node;

class LocatableElement = Language::Element;

class UnknownLocation = UnknownDefaultLocation;

string locationToFileBaseNameAndLineNumberString(Language::Location location) {
result = location.getFile().getBaseName() + ":" + location.getStartLine()
}

LocatableElement dfn_to_element(Language::DataFlow::Node node) {
result = node.asExpr() or
result = node.asParameter()
}

predicate artifactOutputFlowsToGenericInput(
Language::DataFlow::Node artifactOutput, Language::DataFlow::Node otherInput
) {
ArtifactFlow::flow(artifactOutput, otherInput)
}
}

// Instantiate the `CryptographyBase` module
module Crypto = CryptographyBase<Language::Location, CryptoInput>;

/**
* An additional flow step in generic data-flow configurations.
* Where a step is an edge between nodes `n1` and `n2`,
* `this` = `n1` and `getOutput()` = `n2`.
*
* FOR INTERNAL MODELING USE ONLY.
*/
abstract class AdditionalFlowInputStep extends DataFlow::Node {
abstract DataFlow::Node getOutput();

final DataFlow::Node getInput() { result = this }
}

/**
* Generic data source to node input configuration
*/
module GenericDataSourceFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source = any(Crypto::GenericSourceInstance i).getOutputNode()
}

predicate isSink(DataFlow::Node sink) {
sink = any(Crypto::FlowAwareElement other).getInputNode()
}

predicate isBarrierOut(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getInputNode()
}

predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}

predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
}
}

module ArtifactFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source = any(Crypto::ArtifactInstance artifact).getOutputNode()
}

predicate isSink(DataFlow::Node sink) {
sink = any(Crypto::FlowAwareElement other).getInputNode()
}

predicate isBarrierOut(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getInputNode()
}

predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}

predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
}
}

module GenericDataSourceFlow = TaintTracking::Global<GenericDataSourceFlowConfig>;

module ArtifactFlow = DataFlow::Global<ArtifactFlowConfig>;

/**
* A method access that returns random data or writes random data to an argument.
*/
abstract class RandomnessSource extends MethodCall {
/**
* Gets the expression representing the output of this source.
*/
abstract Expr getOutput();

/**
* Gets the type of the source of randomness used by this call.
*/
Type getGenerator() { result = this.getQualifier().getType() }
}

/**
* A call to `System.Security.Cryptography.RandomNumberGenerator.GetBytes`.
*/
class SecureRandomnessSource extends RandomnessSource {
SecureRandomnessSource() {
this.getTarget()
.hasFullyQualifiedName("System.Security.Cryptography", "RandomNumberGenerator", "GetBytes")
}

override Expr getOutput() { result = this.getArgument(0) }
}

/**
* A call to `System.Random.NextBytes`.
*/
class InsecureRandomnessSource extends RandomnessSource {
InsecureRandomnessSource() {
this.getTarget().hasFullyQualifiedName("System", "Random", "NextBytes")
}

override Expr getOutput() { result = this.getArgument(0) }
}

/**
* An instance of random number generation, modelled as the expression
* tied to an output node (i.e., the RNG output)
*/
abstract class RandomnessInstance extends Crypto::RandomNumberGenerationInstance {
override DataFlow::Node getOutputNode() { result.asExpr() = this }
}

/**
* An output instance from the system cryptographically secure RNG.
*/
class SecureRandomnessInstance extends RandomnessInstance {
RandomnessSource source;

SecureRandomnessInstance() {
source instanceof SecureRandomnessSource and
this = source.getOutput()
}

override string getGeneratorName() { result = source.getGenerator().getName() }
}

/**
* An output instance from an insecure RNG.
*/
class InsecureRandomnessInstance extends RandomnessInstance {
RandomnessSource source;

InsecureRandomnessInstance() {
not source instanceof SecureRandomnessSource and
this = source.getOutput()
}

override string getGeneratorName() { result = source.getGenerator().getName() }
}

import dotnet
3 changes: 3 additions & 0 deletions csharp/ql/lib/experimental/quantum/dotnet.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dotnet.AlgorithmValueConsumers
import dotnet.AlgorithmInstances
import dotnet.OperationInstances
Loading