|
| 1 | +/** |
| 2 | + * Provides classes and a taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +private import semmle.code.java.security.Encryption |
| 7 | +private import semmle.code.java.dataflow.TaintTracking |
| 8 | +private import semmle.code.java.dispatch.VirtualDispatch |
| 9 | + |
| 10 | +private class ShortStringLiteral extends StringLiteral { |
| 11 | + ShortStringLiteral() { this.getValue().length() < 100 } |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * A string literal that may refer to an insecure cryptographic algorithm. |
| 16 | + */ |
| 17 | +class InsecureAlgoLiteral extends ShortStringLiteral { |
| 18 | + InsecureAlgoLiteral() { |
| 19 | + // Algorithm identifiers should be at least two characters. |
| 20 | + this.getValue().length() > 1 and |
| 21 | + exists(string s | s = this.getValue() | |
| 22 | + not s.regexpMatch(getSecureAlgorithmRegex()) and |
| 23 | + // Exclude results covered by another query. |
| 24 | + not s.regexpMatch(getInsecureAlgorithmRegex()) |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +private predicate objectToString(MethodAccess ma) { |
| 30 | + exists(ToStringMethod m | |
| 31 | + m = ma.getMethod() and |
| 32 | + m.getDeclaringType() instanceof TypeObject and |
| 33 | + DataFlow::exprNode(ma.getQualifier()).getTypeBound().getErasure() instanceof TypeObject |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +private class StringContainer extends RefType { |
| 38 | + StringContainer() { |
| 39 | + this instanceof TypeString or |
| 40 | + this instanceof StringBuildingType or |
| 41 | + this.hasQualifiedName("java.util", "StringTokenizer") or |
| 42 | + this.(Array).getComponentType() instanceof StringContainer |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * A taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms. |
| 48 | + */ |
| 49 | +module InsecureCryptoConfig implements DataFlow::ConfigSig { |
| 50 | + predicate isSource(DataFlow::Node n) { n.asExpr() instanceof InsecureAlgoLiteral } |
| 51 | + |
| 52 | + predicate isSink(DataFlow::Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) } |
| 53 | + |
| 54 | + predicate isBarrier(DataFlow::Node n) { |
| 55 | + objectToString(n.asExpr()) or |
| 56 | + not n.getType().getErasure() instanceof StringContainer |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Taint-tracking flow for use of potentially insecure cryptographic algorithms. |
| 62 | + */ |
| 63 | +module InsecureCryptoFlow = TaintTracking::Global<InsecureCryptoConfig>; |
0 commit comments