Skip to content

Commit 95544e2

Browse files
committed
Add model and sample query for constants used in key specs
1 parent 2b0b927 commit 95544e2

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

java/ql/lib/experimental/Quantum/JCA.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,31 @@ module JCAModel {
532532
this.getMethod().getParameterType(2).hasName("AlgorithmParameterSpec")
533533
}
534534
}
535+
536+
/**
537+
* Key Material Concept
538+
* any class that implements `java.security.spec.KeySpec`
539+
*/
540+
class KeyMaterialObject extends Class {
541+
KeyMaterialObject() {
542+
exists(RefType t |
543+
this.extendsOrImplements*(t) and
544+
t.hasQualifiedName("java.security.spec", "KeySpec")
545+
)
546+
}
547+
}
548+
549+
/**
550+
* KeyMaterial
551+
* ie some plain material that gets used to generate a Key
552+
*/
553+
class KeyMaterialInstantiation extends Crypto::KeyMaterialInstance instanceof ClassInstanceExpr {
554+
KeyMaterialInstantiation() {
555+
this.(ClassInstanceExpr).getConstructedType() instanceof KeyMaterialObject
556+
}
557+
558+
override DataFlow::Node asOutputData() { result.asExpr() = this }
559+
560+
override DataFlow::Node getInput() { result.asExpr() = this.(ClassInstanceExpr).getArgument(0) }
561+
}
535562
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @name Constant password
3+
* @description Using constant passwords is not secure, because potential attackers can easily recover them from the source code.
4+
* @kind problem
5+
* @problem.severity error
6+
* @security-severity 6.8
7+
* @precision high
8+
* @id java/constant-password-new-model
9+
* @tags security
10+
* external/cwe/cwe-259
11+
*/
12+
13+
//this query is a replica of the concept in: https://github.com/github/codeql/blob/main/swift/ql/src/queries/Security/CWE-259/ConstantPassword.ql
14+
//but uses the **NEW MODELLING**
15+
import experimental.Quantum.Language
16+
import semmle.code.java.dataflow.TaintTracking
17+
18+
/**
19+
* A constant password is created through either a byte array or string literals.
20+
*/
21+
class ConstantPasswordSource extends Expr {
22+
ConstantPasswordSource() {
23+
this instanceof CharacterLiteral or
24+
this instanceof StringLiteral
25+
}
26+
}
27+
28+
module ConstantToKeyDerivationFlow implements DataFlow::ConfigSig {
29+
predicate isSource(DataFlow::Node source) { source.asExpr() instanceof ConstantPasswordSource }
30+
31+
predicate isSink(DataFlow::Node sink) { any(Crypto::KeyMaterialInstance i).getInput() = sink }
32+
}
33+
34+
module ConstantToKeyDerivationFlowInit = TaintTracking::Global<ConstantToKeyDerivationFlow>;
35+
36+
from DataFlow::Node source, DataFlow::Node sink
37+
where ConstantToKeyDerivationFlowInit::flow(source, sink)
38+
select sink, "Constant password $@ is used.", source, source.toString()

shared/cryptography/codeql/cryptography/Model.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
118118

119119
abstract class DigestArtifactInstance extends ArtifactLocatableElement { }
120120

121+
abstract class KeyMaterialInstance extends ArtifactLocatableElement { }
122+
121123
abstract class KeyArtifactInstance extends ArtifactLocatableElement { }
122124

123125
abstract class NonceArtifactInstance extends ArtifactLocatableElement { }
@@ -130,6 +132,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
130132
// Artifacts (data that is not an operation or algorithm, e.g., a key)
131133
TDigest(DigestArtifactInstance e) or
132134
TKey(KeyArtifactInstance e) or
135+
TKeyMaterial(KeyMaterialInstance e) or
133136
TNonce(NonceArtifactInstance e) or
134137
TRandomNumberGeneration(RandomNumberGenerationInstance e) or
135138
// Operations (e.g., hashing, encryption)
@@ -1000,4 +1003,23 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
10001003
abstract class KEMAlgorithm extends TKeyEncapsulationAlgorithm, Algorithm {
10011004
final override string getAlgorithmType() { result = "KeyEncapsulationAlgorithm" }
10021005
}
1006+
1007+
/**
1008+
* A Key Material Object
1009+
*/
1010+
private class KeyMaterialImpl extends Artifact, TKeyMaterial {
1011+
KeyMaterialInstance instance;
1012+
1013+
KeyMaterialImpl() { this = TKeyMaterial(instance) }
1014+
1015+
final override string getInternalType() { result = "KeyMaterial" }
1016+
1017+
override Location getLocation() { result = instance.getLocation() }
1018+
1019+
override DataFlowNode asOutputData() { result = instance.asOutputData() }
1020+
1021+
override DataFlowNode getInputData() { result = instance.getInput() }
1022+
}
1023+
1024+
final class KeyMaterial = KeyMaterialImpl;
10031025
}

0 commit comments

Comments
 (0)