Skip to content

Commit 7e8acd7

Browse files
committed
Crypto: Update WeakAsymmetricKeyGenSize to a path problem.
1 parent 8b5a423 commit 7e8acd7

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @name Weak Asymmetric Key Size
33
* @id java/quantum/weak-asymmetric-key-gen-size
44
* @description An asymmetric key of known size is less than 2048 bits for any non-elliptic curve key operation.
5-
* @kind problem
5+
* @kind path-problem
66
* @problem.severity error
77
* @precision high
88
* @tags quantum
@@ -12,12 +12,57 @@
1212
import java
1313
import experimental.quantum.Language
1414

15-
from Crypto::KeyArtifactNode key, int keySize, Crypto::AlgorithmNode alg
15+
module KeySizeFlowConfig implements DataFlow::ConfigSig {
16+
predicate isSource(DataFlow::Node source) {
17+
source = any(Crypto::GenericSourceInstance i).getOutputNode() or
18+
source = any(Crypto::ArtifactInstance artifact).getOutputNode()
19+
}
20+
21+
predicate isSink(DataFlow::Node sink) {
22+
exists(Crypto::KeyCreationOperationNode kgen |
23+
sink = kgen.getKeySizeConsumer().getConsumer().getInputNode()
24+
)
25+
}
26+
27+
predicate isBarrierOut(DataFlow::Node node) {
28+
node = any(Crypto::FlowAwareElement element).getInputNode()
29+
}
30+
31+
predicate isBarrierIn(DataFlow::Node node) {
32+
node = any(Crypto::FlowAwareElement element).getOutputNode()
33+
}
34+
35+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
36+
node1.(AdditionalFlowInputStep).getOutput() = node2
37+
or
38+
exists(MethodCall m |
39+
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
40+
node1.asExpr() = m.getQualifier() and
41+
node2.asExpr() = m
42+
)
43+
}
44+
}
45+
46+
module KeySizeFlow = TaintTracking::Global<KeySizeFlowConfig>;
47+
48+
import KeySizeFlow::PathGraph
49+
50+
from
51+
Crypto::KeyCreationOperationNode keygen, int keySize, Crypto::AlgorithmNode alg,
52+
KeySizeFlow::PathNode srcNode, KeySizeFlow::PathNode sinkNode
1653
where
17-
key.getCreatingOperation().getAKeySizeSource().asElement().(Literal).getValue().toInt() = keySize and
18-
alg = key.getAKnownAlgorithm() and // NOTE: if algorithm is not known (doesn't bind) we need a separate query
54+
// ASSUMPTION/NOTE: if the key size is set on a key creation, but the key creation itself is not observed
55+
// (i.e., the size is initialized but the operation not observed) currently we will not
56+
// detect the size. A key creation operation currently must be observed.
57+
keygen.getAKeySizeSource().asElement().(Literal).getValue().toInt() = keySize and
58+
// NOTE: if algorithm is not known (doesn't bind) we need a separate query
59+
// Also note the algorithm may also be re-specified at a use of the key
60+
alg = keygen.getAKnownAlgorithm() and
1961
not alg instanceof Crypto::EllipticCurveNode and // Elliptic curve sizes are handled separately and are more tied directly to the algorithm
2062
alg instanceof Crypto::AsymmetricAlgorithmNode and
21-
keySize < 2048
22-
select key, "Use of weak asymmetric key size (" + keySize.toString() + " bits) for algorithm $@",
23-
alg, alg.getAlgorithmName()
63+
keySize < 2048 and
64+
srcNode.getNode().asExpr() = keygen.getAKeySizeSource().asElement() and
65+
sinkNode.getNode() = keygen.getKeySizeConsumer().getConsumer().getInputNode()
66+
select sinkNode, srcNode, sinkNode,
67+
"Use of weak asymmetric key size (" + keySize.toString() + " bits) for algorithm $@", alg,
68+
alg.getAlgorithmName()
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
| InsufficientAsymmetricKeySize.java:6:9:6:37 | Key | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:4:69:4:73 | KeyOperationAlgorithm | RSA |
2-
| InsufficientAsymmetricKeySize.java:10:9:10:37 | Key | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:8:69:8:73 | KeyOperationAlgorithm | DSA |
3-
| InsufficientAsymmetricKeySize.java:14:9:14:37 | Key | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:12:69:12:72 | KeyAgreementAlgorithm | DH |
1+
edges
2+
nodes
3+
| InsufficientAsymmetricKeySize.java:5:32:5:35 | 1024 | semmle.label | 1024 |
4+
| InsufficientAsymmetricKeySize.java:9:32:9:35 | 1024 | semmle.label | 1024 |
5+
| InsufficientAsymmetricKeySize.java:13:32:13:35 | 1024 | semmle.label | 1024 |
6+
| InsufficientAsymmetricKeySize.java:17:32:17:35 | 2048 | semmle.label | 2048 |
7+
| InsufficientAsymmetricKeySize.java:21:32:21:35 | 2048 | semmle.label | 2048 |
8+
| InsufficientAsymmetricKeySize.java:25:32:25:35 | 2048 | semmle.label | 2048 |
9+
subpaths
10+
#select
11+
| InsufficientAsymmetricKeySize.java:5:32:5:35 | 1024 | InsufficientAsymmetricKeySize.java:5:32:5:35 | 1024 | InsufficientAsymmetricKeySize.java:5:32:5:35 | 1024 | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:4:69:4:73 | KeyOperationAlgorithm | RSA |
12+
| InsufficientAsymmetricKeySize.java:9:32:9:35 | 1024 | InsufficientAsymmetricKeySize.java:9:32:9:35 | 1024 | InsufficientAsymmetricKeySize.java:9:32:9:35 | 1024 | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:8:69:8:73 | KeyOperationAlgorithm | DSA |
13+
| InsufficientAsymmetricKeySize.java:13:32:13:35 | 1024 | InsufficientAsymmetricKeySize.java:13:32:13:35 | 1024 | InsufficientAsymmetricKeySize.java:13:32:13:35 | 1024 | Use of weak asymmetric key size (1024 bits) for algorithm $@ | InsufficientAsymmetricKeySize.java:12:69:12:72 | KeyAgreementAlgorithm | DH |

0 commit comments

Comments
 (0)