|
2 | 2 |
|
3 | 3 | import semmle.code.java.security.Encryption |
4 | 4 | import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.dataflow.TaintTracking |
| 6 | + |
| 7 | +//import semmle.code.java.dataflow.internal.DataFlowImplCommonPublic |
| 8 | +//import semmle.code.java.dataflow.FlowSources |
| 9 | +//import semmle.code.java.dataflow.internal.DataFlowNodes |
| 10 | +/** |
| 11 | + * An Asymmetric (RSA, DSA, DH) key length data flow tracking configuration. |
| 12 | + */ |
| 13 | +class AsymmetricKeyTrackingConfiguration extends DataFlow::Configuration { |
| 14 | + AsymmetricKeyTrackingConfiguration() { this = "AsymmetricKeyTrackingConfiguration" } |
| 15 | + |
| 16 | + override predicate isSource(DataFlow::Node source, DataFlow::FlowState state) { |
| 17 | + //state instanceof DataFlow::FlowStateEmpty and |
| 18 | + source.asExpr().(IntegerLiteral).getIntValue() < 2048 and state = "2048" |
| 19 | + or |
| 20 | + source.asExpr().(IntegerLiteral).getIntValue() < 256 and state = "256" |
| 21 | + or |
| 22 | + getECKeySize(source.asExpr().(StringLiteral).getValue()) < 256 and state = "256" // need this for the cases when the key size is embedded in the curve name. |
| 23 | + } |
| 24 | + |
| 25 | + override predicate isSink(DataFlow::Node sink, DataFlow::FlowState state) { |
| 26 | + exists(MethodAccess ma, JavaSecurityKeyPairGenerator jpg | |
| 27 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 28 | + ( |
| 29 | + jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches(["RSA", "DSA", "DH"]) and |
| 30 | + DataFlow::localExprFlow(jpg, ma.getQualifier()) and |
| 31 | + sink.asExpr() = ma.getArgument(0) and |
| 32 | + //ma.getArgument(0).(LocalSourceNode).flowsTo(sink) and |
| 33 | + //ma.getArgument(0).(CompileTimeConstantExpr).getIntValue() < 2048 and |
| 34 | + state = "2048" |
| 35 | + ) |
| 36 | + or |
| 37 | + jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches("EC%") and |
| 38 | + DataFlow::localExprFlow(jpg, ma.getQualifier()) and |
| 39 | + sink.asExpr() = ma.getArgument(0) and |
| 40 | + //ma.getArgument(0).(CompileTimeConstantExpr).getIntValue() < 256 and |
| 41 | + state = "256" |
| 42 | + ) |
| 43 | + or |
| 44 | + // TODO: combine below three for less duplicated code |
| 45 | + exists(ClassInstanceExpr rsaKeyGenParamSpec | |
| 46 | + rsaKeyGenParamSpec.getConstructedType() instanceof RsaKeyGenParameterSpec and |
| 47 | + sink.asExpr() = rsaKeyGenParamSpec.getArgument(0) and |
| 48 | + state = "2048" |
| 49 | + ) |
| 50 | + or |
| 51 | + exists(ClassInstanceExpr dsaGenParamSpec | |
| 52 | + dsaGenParamSpec.getConstructedType() instanceof DsaGenParameterSpec and |
| 53 | + sink.asExpr() = dsaGenParamSpec.getArgument(0) and |
| 54 | + state = "2048" |
| 55 | + ) |
| 56 | + or |
| 57 | + exists(ClassInstanceExpr dhGenParamSpec | |
| 58 | + dhGenParamSpec.getConstructedType() instanceof DhGenParameterSpec and |
| 59 | + sink.asExpr() = dhGenParamSpec.getArgument(0) and |
| 60 | + state = "2048" |
| 61 | + ) |
| 62 | + or |
| 63 | + exists(ClassInstanceExpr ecGenParamSpec | |
| 64 | + ecGenParamSpec.getConstructedType() instanceof EcGenParameterSpec and |
| 65 | + sink.asExpr() = ecGenParamSpec.getArgument(0) and |
| 66 | + state = "256" |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + override predicate isAdditionalFlowStep( |
| 71 | + DataFlow::Node node1, DataFlow::FlowState state1, DataFlow::Node node2, |
| 72 | + DataFlow::FlowState state2 |
| 73 | + ) { |
| 74 | + exists(IntegerLiteral intLiteral | |
| 75 | + state1 = "" and |
| 76 | + state2 = intLiteral.toString() and |
| 77 | + node1.asExpr() = intLiteral and |
| 78 | + node2.asExpr() = intLiteral |
| 79 | + //intLiteral.toString().toInt() = 64 // test viability of this craziness |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
5 | 83 |
|
6 | 84 | /** |
7 | 85 | * An Asymmetric (RSA, DSA, DH) key length data flow tracking configuration. |
|
0 commit comments