Skip to content

Commit 76128ed

Browse files
committed
Crypto: Update InsecureIVorNonce to be a path problem.
1 parent bd068c2 commit 76128ed

File tree

4 files changed

+179
-32
lines changed

4 files changed

+179
-32
lines changed

java/ql/lib/experimental/quantum/Language.qll

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,6 @@ private class GenericRemoteDataSource extends Crypto::GenericRemoteDataSource {
9393
override string getAdditionalDescription() { result = this.toString() }
9494
}
9595

96-
// /**
97-
// * A property access value (constant from a file)
98-
// */
99-
// class PropertyConstant extends Crypto::GenericConstantSourceInstance instanceof Literal{
100-
// PropertyConstant() {
101-
// value = this.getPropertyValue() and
102-
// // Since properties pairs are not included in the java/weak-cryptographic-algorithm,
103-
// // the check for values from properties files can be less strict than `InsecureAlgoLiteral`.
104-
// not value.regexpMatch(getSecureAlgorithmRegex())
105-
// }
106-
// override string getStringValue() { result = value }
107-
// }
108-
import semmle.code.java.dataflow.RangeUtils
109-
// TODO: import all frameworks?
11096
import semmle.code.java.frameworks.Properties
11197
private import semmle.code.configfiles.ConfigFiles
11298

java/ql/src/experimental/quantum/Examples/InsecureIVorNonceSource.ql

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* is any static nonce, or any known insecure source for a nonce/iv if
77
* the value is used for an encryption operation (decryption operations are ignored
88
* as the nonce/iv would be provided alongside the ciphertext).
9-
* @kind problem
9+
* @kind path-problem
1010
* @problem.severity error
1111
* @precision high
1212
* @tags quantum
@@ -15,7 +15,42 @@
1515

1616
import experimental.quantum.Language
1717

18-
from Crypto::NonceArtifactNode nonce, Crypto::NodeBase src, Crypto::NodeBase op, string msg
18+
module NonceSrcFlowConfig implements DataFlow::ConfigSig {
19+
predicate isSource(DataFlow::Node source) {
20+
source = any(Crypto::GenericSourceInstance i).getOutputNode() or
21+
source = any(Crypto::ArtifactInstance artifact).getOutputNode()
22+
}
23+
24+
predicate isSink(DataFlow::Node sink) {
25+
exists(Crypto::NonceArtifactNode nonce | sink.asExpr() = nonce.asElement())
26+
}
27+
28+
predicate isBarrierOut(DataFlow::Node node) {
29+
node = any(Crypto::FlowAwareElement element).getInputNode()
30+
}
31+
32+
predicate isBarrierIn(DataFlow::Node node) {
33+
node = any(Crypto::FlowAwareElement element).getOutputNode()
34+
}
35+
36+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
37+
node1.(AdditionalFlowInputStep).getOutput() = node2
38+
or
39+
exists(MethodCall m |
40+
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
41+
node1.asExpr() = m.getQualifier() and
42+
node2.asExpr() = m
43+
)
44+
}
45+
}
46+
47+
module NonceSrcFlow = TaintTracking::Global<NonceSrcFlowConfig>;
48+
49+
import NonceSrcFlow::PathGraph
50+
51+
from
52+
Crypto::NonceArtifactNode nonce, Crypto::NodeBase src, Crypto::NodeBase op, string msg,
53+
NonceSrcFlow::PathNode srcNode, NonceSrcFlow::PathNode sinkNode
1954
where
2055
nonce.getSourceNode() = src and
2156
// NOTE: null nonces should be handled seaparately, often used for default values prior to initialization
@@ -25,7 +60,7 @@ where
2560
(
2661
// Case 1: Any constant nonce/iv is bad, regardless of how it is used
2762
src.asElement() instanceof Crypto::GenericConstantSourceInstance and
28-
op = nonce and // binding op by not using it
63+
op = nonce and // binding op but not using it
2964
msg = "Nonce or IV uses constant source $@"
3065
or
3166
// Case 2: The nonce has a non-random source and there is no known operation for the nonce
@@ -47,5 +82,8 @@ where
4782
op.(Crypto::CipherOperationNode).getKeyOperationSubtype() instanceof Crypto::TWrapMode
4883
) and
4984
msg = "Nonce or IV uses insecure source $@ at encryption operation $@"
50-
)
51-
select nonce, msg, src, src.toString(), op, op.toString()
85+
) and
86+
srcNode.getNode().asExpr() = src.asElement() and
87+
sinkNode.getNode().asExpr() = nonce.asElement() and
88+
NonceSrcFlow::flowPath(srcNode, sinkNode)
89+
select sinkNode, srcNode, sinkNode, msg, src, src.toString(), op, op.toString()

0 commit comments

Comments
 (0)