Skip to content

Commit 2e12bb5

Browse files
committed
Merge branch 'nic/crypto-test' into knewbury01/JCA-sample
2 parents efcf7ea + cd70acd commit 2e12bb5

File tree

11 files changed

+555
-191
lines changed

11 files changed

+555
-191
lines changed

cpp/ql/lib/experimental/Quantum/Base.qll

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
5151
* This predicate is used by derived classes to construct the graph of cryptographic operations.
5252
*/
5353
predicate properties(string key, string value, Location location) {
54-
key = "origin" and location = this.getOrigin(value).getLocation()
54+
key = "origin" and
55+
location = this.getOrigin(value).getLocation() and
56+
not location = this.getLocation()
5557
}
5658

5759
/**
@@ -92,6 +94,11 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
9294
*/
9395
abstract string getAlgorithmName();
9496

97+
/**
98+
* Gets the raw name of this algorithm from source (no parsing or formatting)
99+
*/
100+
abstract string getRawAlgorithmName();
101+
95102
final override string toString() { result = this.getAlgorithmName() }
96103
}
97104

@@ -145,10 +152,6 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
145152

146153
override string getAlgorithmName() { this.hashTypeToNameMapping(this.getHashType(), result) }
147154

148-
/**
149-
* Gets the raw name of this hash algorithm from source.
150-
*/
151-
abstract string getRawAlgorithmName();
152155
}
153156

154157
/**
@@ -195,30 +198,55 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
195198
}
196199
}
197200

201+
newtype TEllipticCurveFamilyType =
202+
// We're saying by this that all of these have an identical interface / properties / edges
203+
NIST() or
204+
SEC() or
205+
NUMS() or
206+
PRIME() or
207+
BRAINPOOL() or
208+
CURVE25519() or
209+
CURVE448() or
210+
C2() or
211+
SM2() or
212+
ES() or
213+
OtherEllipticCurveFamilyType()
214+
215+
198216
/**
199217
* Elliptic curve algorithm
200218
*/
201219
abstract class EllipticCurve extends Algorithm {
202-
abstract string getVersion(Location location);
220+
203221

204222
abstract string getKeySize(Location location);
205223

224+
abstract TEllipticCurveFamilyType getCurveFamilyType();
225+
206226
override predicate properties(string key, string value, Location location) {
207227
super.properties(key, value, location)
208228
or
209-
key = "version" and
210-
if exists(this.getVersion(location))
211-
then value = this.getVersion(location)
212-
else (
213-
value instanceof UnknownPropertyValue and location instanceof UnknownLocation
214-
)
215-
or
216229
key = "key_size" and
217230
if exists(this.getKeySize(location))
218231
then value = this.getKeySize(location)
219232
else (
220233
value instanceof UnknownPropertyValue and location instanceof UnknownLocation
221234
)
235+
// other properties, like field type are possible, but not modeled until considered necessary
222236
}
237+
238+
override string getAlgorithmName() { result = this.getRawAlgorithmName().toUpperCase()}
239+
240+
/**
241+
* Mandating that for Elliptic Curves specifically, users are responsible
242+
* for providing as the 'raw' name, the official name of the algorithm.
243+
* Casing doesn't matter, we will enforce further naming restrictions on
244+
* `getAlgorithmName` by default.
245+
* Rationale: elliptic curve names can have a lot of variation in their components
246+
* (e.g., "secp256r1" vs "P-256"), trying to produce generalized set of properties
247+
* is possible to capture all cases, but such modeling is likely not necessary.
248+
* if all properties need to be captured, we can reassess how names are generated.
249+
*/
250+
override abstract string getRawAlgorithmName();
223251
}
224252
}

cpp/ql/lib/experimental/Quantum/BaseBackup.qll

Lines changed: 0 additions & 125 deletions
This file was deleted.

cpp/ql/lib/experimental/Quantum/OpenSSL.qll

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ module OpenSSLModel {
8484
}
8585
}
8686

87-
class TestKeyDerivationOperationHacky extends KeyDerivationOperation instanceof FunctionCall {
88-
HKDF hkdf;
89-
90-
TestKeyDerivationOperationHacky() {
91-
this.getEnclosingFunction() = hkdf.(Expr).getEnclosingFunction()
92-
}
93-
94-
override Crypto::KeyDerivationAlgorithm getAlgorithm() { result = hkdf }
95-
}
96-
9787
class PKCS12KDF extends KeyDerivationAlgorithm, Crypto::PKCS12KDF instanceof Expr {
9888
KDFAlgorithmStringLiteral origin;
9989

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @name "Print CBOM Graph"
3+
* @description "Outputs a graph representation of the cryptographic bill of materials."
4+
* @kind graph
5+
* @id cbomgraph
6+
*/
7+
8+
import experimental.Quantum.Language
9+
10+
string getPropertyString(Crypto::NodeBase node, string key) {
11+
result =
12+
strictconcat(any(string value, Location location, string parsed |
13+
node.properties(key, value, location) and
14+
parsed = "(" + value + "," + location.toString() + ")"
15+
|
16+
parsed
17+
), ","
18+
)
19+
}
20+
21+
string getLabel(Crypto::NodeBase node) { result = node.toString() }
22+
23+
query predicate nodes(Crypto::NodeBase node, string key, string value) {
24+
key = "semmle.label" and
25+
value = getLabel(node)
26+
or
27+
// CodeQL's DGML output does not include a location
28+
key = "Location" and
29+
value = node.getLocation().toString()
30+
or
31+
// Known unknown edges should be reported as properties rather than edges
32+
node = node.getChild(key) and
33+
value = "<unknown>"
34+
or
35+
// Report properties
36+
value = getPropertyString(node, key)
37+
}
38+
39+
query predicate edges(Crypto::NodeBase source, Crypto::NodeBase target, string key, string value) {
40+
key = "semmle.label" and
41+
target = source.getChild(value) and
42+
// Known unknowns are reported as properties rather than edges
43+
not source = target
44+
}
45+
46+
query predicate graphProperties(string key, string value) {
47+
key = "semmle.graphKind" and value = "graph"
48+
}

cpp/ql/src/experimental/Quantum/Test.ql

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
CODEQL_PATH="/Users/nicolaswill/Library/Application Support/Code/User/globalStorage/github.vscode-codeql/distribution5/codeql/codeql"
4+
DATABASE_PATH="/Users/nicolaswill/openssl_codeql/openssl/openssl_db"
5+
QUERY_FILE="CBOMGraph.ql"
6+
OUTPUT_DIR="graph_output"
7+
8+
python3 generate_cbom.py -c "$CODEQL_PATH" -d "$DATABASE_PATH" -q "$QUERY_FILE" -o "$OUTPUT_DIR"

0 commit comments

Comments
 (0)