|
1 | | -/** Provides classes and predicates related to insufficient key sizes in Java. */ |
| 1 | +/** Provides data flow configurations to be used in queries related to insufficient key sizes. */ |
2 | 2 |
|
3 | 3 | import semmle.code.java.security.Encryption |
4 | 4 | import semmle.code.java.dataflow.DataFlow |
5 | 5 | import semmle.code.java.dataflow.TaintTracking |
| 6 | +import semmle.code.java.security.InsufficientKeySize |
6 | 7 |
|
7 | 8 | /** |
8 | | - * An Asymmetric (RSA, DSA, DH) key length data flow tracking configuration. |
| 9 | + * A data flow configuration for tracking non-elliptic curve asymmetric algorithms |
| 10 | + * (RSA, DSA, and DH) key sizes. |
9 | 11 | */ |
10 | 12 | class AsymmetricNonECKeyTrackingConfiguration extends DataFlow::Configuration { |
11 | 13 | AsymmetricNonECKeyTrackingConfiguration() { this = "AsymmetricNonECKeyTrackingConfiguration" } |
12 | 14 |
|
13 | | - override predicate isSource(DataFlow::Node source) { |
14 | | - source.asExpr().(IntegerLiteral).getIntValue() < 2048 |
15 | | - } |
| 15 | + override predicate isSource(DataFlow::Node source) { source instanceof AsymmetricNonECSource } |
16 | 16 |
|
17 | | - override predicate isSink(DataFlow::Node sink) { |
18 | | - exists(MethodAccess ma, JavaSecurityKeyPairGenerator jpg | |
19 | | - ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
20 | | - jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches(["RSA", "DSA", "DH"]) and |
21 | | - DataFlow::localExprFlow(jpg, ma.getQualifier()) and |
22 | | - sink.asExpr() = ma.getArgument(0) |
23 | | - ) |
24 | | - or |
25 | | - // TODO: combine below three for less duplicated code |
26 | | - exists(ClassInstanceExpr rsaKeyGenParamSpec | |
27 | | - rsaKeyGenParamSpec.getConstructedType() instanceof RsaKeyGenParameterSpec and |
28 | | - sink.asExpr() = rsaKeyGenParamSpec.getArgument(0) |
29 | | - ) |
30 | | - or |
31 | | - exists(ClassInstanceExpr dsaGenParamSpec | |
32 | | - dsaGenParamSpec.getConstructedType() instanceof DsaGenParameterSpec and |
33 | | - sink.asExpr() = dsaGenParamSpec.getArgument(0) |
34 | | - ) |
35 | | - or |
36 | | - exists(ClassInstanceExpr dhGenParamSpec | |
37 | | - dhGenParamSpec.getConstructedType() instanceof DhGenParameterSpec and |
38 | | - sink.asExpr() = dhGenParamSpec.getArgument(0) |
39 | | - ) |
40 | | - } |
| 17 | + override predicate isSink(DataFlow::Node sink) { sink instanceof AsymmetricNonECSink } |
41 | 18 | } |
42 | 19 |
|
43 | 20 | /** |
44 | | - * An Asymmetric (EC) key length data flow tracking configuration. |
| 21 | + * A data flow configuration for tracking elliptic curve (EC) asymmetric |
| 22 | + * algorithm key sizes. |
45 | 23 | */ |
46 | 24 | class AsymmetricECKeyTrackingConfiguration extends DataFlow::Configuration { |
47 | 25 | AsymmetricECKeyTrackingConfiguration() { this = "AsymmetricECKeyTrackingConfiguration" } |
48 | 26 |
|
49 | | - override predicate isSource(DataFlow::Node source) { |
50 | | - source.asExpr().(IntegerLiteral).getIntValue() < 256 or |
51 | | - getECKeySize(source.asExpr().(StringLiteral).getValue()) < 256 // need this for the cases when the key size is embedded in the curve name. |
52 | | - } |
| 27 | + override predicate isSource(DataFlow::Node source) { source instanceof AsymmetricECSource } |
53 | 28 |
|
54 | | - override predicate isSink(DataFlow::Node sink) { |
55 | | - exists(MethodAccess ma, JavaSecurityKeyPairGenerator jpg | |
56 | | - ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
57 | | - jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches("EC%") and |
58 | | - DataFlow::localExprFlow(jpg, ma.getQualifier()) and |
59 | | - sink.asExpr() = ma.getArgument(0) |
60 | | - ) |
61 | | - or |
62 | | - exists(ClassInstanceExpr ecGenParamSpec | |
63 | | - ecGenParamSpec.getConstructedType() instanceof EcGenParameterSpec and |
64 | | - sink.asExpr() = ecGenParamSpec.getArgument(0) |
65 | | - ) |
66 | | - } |
| 29 | + override predicate isSink(DataFlow::Node sink) { sink instanceof AsymmetricECSink } |
67 | 30 | } |
68 | 31 |
|
69 | | -/** |
70 | | - * A Symmetric (AES) key length data flow tracking configuration. |
71 | | - */ |
| 32 | +/** A data flow configuration for tracking symmetric algorithm (AES) key sizes. */ |
72 | 33 | class SymmetricKeyTrackingConfiguration extends DataFlow::Configuration { |
73 | 34 | SymmetricKeyTrackingConfiguration() { this = "SymmetricKeyTrackingConfiguration" } |
74 | 35 |
|
75 | | - override predicate isSource(DataFlow::Node source) { |
76 | | - source.asExpr().(IntegerLiteral).getIntValue() < 128 |
77 | | - } |
78 | | - |
79 | | - override predicate isSink(DataFlow::Node sink) { |
80 | | - exists(MethodAccess ma, JavaxCryptoKeyGenerator jcg | |
81 | | - ma.getMethod() instanceof KeyGeneratorInitMethod and |
82 | | - jcg.getAlgoSpec().(StringLiteral).getValue().toUpperCase() = "AES" and |
83 | | - DataFlow::localExprFlow(jcg, ma.getQualifier()) and |
84 | | - sink.asExpr() = ma.getArgument(0) |
85 | | - ) |
86 | | - } |
87 | | -} |
88 | | - |
89 | | -// ********************** Need the below models for the above configs ********************** |
90 | | -// todo: move some/all of below to Encryption.qll or elsewhere? |
91 | | -/** The Java class `java.security.spec.ECGenParameterSpec`. */ |
92 | | -private class EcGenParameterSpec extends RefType { |
93 | | - EcGenParameterSpec() { this.hasQualifiedName("java.security.spec", "ECGenParameterSpec") } |
94 | | -} |
95 | | - |
96 | | -/** The Java class `java.security.spec.RSAKeyGenParameterSpec`. */ |
97 | | -private class RsaKeyGenParameterSpec extends RefType { |
98 | | - RsaKeyGenParameterSpec() { this.hasQualifiedName("java.security.spec", "RSAKeyGenParameterSpec") } |
99 | | -} |
100 | | - |
101 | | -/** The Java class `java.security.spec.DSAGenParameterSpec`. */ |
102 | | -private class DsaGenParameterSpec extends RefType { |
103 | | - DsaGenParameterSpec() { this.hasQualifiedName("java.security.spec", "DSAGenParameterSpec") } |
104 | | -} |
105 | | - |
106 | | -/** The Java class `javax.crypto.spec.DHGenParameterSpec`. */ |
107 | | -private class DhGenParameterSpec extends RefType { |
108 | | - DhGenParameterSpec() { this.hasQualifiedName("javax.crypto.spec", "DHGenParameterSpec") } |
109 | | -} |
110 | | - |
111 | | -/** The `init` method declared in `javax.crypto.KeyGenerator`. */ |
112 | | -private class KeyGeneratorInitMethod extends Method { |
113 | | - KeyGeneratorInitMethod() { |
114 | | - this.getDeclaringType() instanceof KeyGenerator and |
115 | | - this.hasName("init") |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -/** The `initialize` method declared in `java.security.KeyPairGenerator`. */ |
120 | | -private class KeyPairGeneratorInitMethod extends Method { |
121 | | - KeyPairGeneratorInitMethod() { |
122 | | - this.getDeclaringType() instanceof KeyPairGenerator and |
123 | | - this.hasName("initialize") |
124 | | - } |
125 | | -} |
| 36 | + override predicate isSource(DataFlow::Node source) { source instanceof SymmetricSource } |
126 | 37 |
|
127 | | -/** Returns the key size in the EC algorithm string */ |
128 | | -bindingset[algorithm] |
129 | | -private int getECKeySize(string algorithm) { |
130 | | - algorithm.matches("sec%") and // specification such as "secp256r1" |
131 | | - result = algorithm.regexpCapture("sec[p|t](\\d+)[a-zA-Z].*", 1).toInt() |
132 | | - or |
133 | | - algorithm.matches("X9.62%") and //specification such as "X9.62 prime192v2" |
134 | | - result = algorithm.regexpCapture("X9\\.62 .*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
135 | | - or |
136 | | - (algorithm.matches("prime%") or algorithm.matches("c2tnb%")) and //specification such as "prime192v2" |
137 | | - result = algorithm.regexpCapture(".*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
| 38 | + override predicate isSink(DataFlow::Node sink) { sink instanceof SymmetricSink } |
138 | 39 | } |
139 | | -// ******* DATAFLOW ABOVE ************************************************************************* |
140 | | -// TODO: |
141 | | -// todo #0: look into use of specs without keygens; should spec not be a sink in these cases? |
142 | | -// todo #1: make representation of source that can be shared across the configs |
143 | | -// todo #2: make representation of sink that can be shared across the configs |
144 | | -// todo #3: make list of algo names more easily reusable (either as constant-type variable at top of file, or model as own class to share, etc.) |
145 | | -// todo #4: refactor to be more like the Python (or C#) version? (or not possible because of lack of DataFlow::Node for void method in Java?) |
| 40 | +// ******* 3 DATAFLOW CONFIGS ABOVE ************************************************************************* |
146 | 41 | // ******* SINGLE CONFIG ATTEMPT BELOW ************************************************************************* |
147 | 42 | // /** |
148 | 43 | // * A key length data flow tracking configuration. |
|
0 commit comments