|
2 | 2 | * @name Weak Asymmetric Key Size |
3 | 3 | * @id java/quantum/weak-asymmetric-key-gen-size |
4 | 4 | * @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 |
6 | 6 | * @problem.severity error |
7 | 7 | * @precision high |
8 | 8 | * @tags quantum |
|
12 | 12 | import java |
13 | 13 | import experimental.quantum.Language |
14 | 14 |
|
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 |
16 | 53 | 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 |
19 | 61 | not alg instanceof Crypto::EllipticCurveNode and // Elliptic curve sizes are handled separately and are more tied directly to the algorithm |
20 | 62 | 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() |
0 commit comments