@@ -2,6 +2,11 @@ import semmle.code.java.security.Encryption
22import semmle.code.java.dataflow.TaintTracking
33import semmle.code.java.dataflow.DataFlow
44
5+ // TODO:
6+ // todo #1: make representation of source that can be shared across the configs
7+ // todo #2: make representation of sink that can be shared across the configs
8+ // todo #3: finish adding tracking for algo type/name... need flow/taint-tracking for across methods??
9+ // todo #3a: 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.)
510// ******* DATAFLOW BELOW *************************************************************************
611/**
712 * Asymmetric (RSA, DSA, DH) key length data flow tracking configuration.
@@ -10,13 +15,27 @@ class AsymmetricKeyTrackingConfiguration extends DataFlow::Configuration {
1015 AsymmetricKeyTrackingConfiguration ( ) { this = "AsymmetricKeyTrackingConfiguration" }
1116
1217 override predicate isSource ( DataFlow:: Node source ) {
13- source .asExpr ( ) instanceof IntegerLiteral and // ! this works with current test cases, but reconsider IntegerLiteral when variables are used
14- source .toString ( ) .toInt ( ) < 2048
18+ exists ( ClassInstanceExpr rsaGenParamSpec |
19+ rsaGenParamSpec .getConstructedType ( ) instanceof RSAGenParameterSpec and // ! double-check if should just use getType() instead
20+ rsaGenParamSpec .getArgument ( 0 ) .( IntegerLiteral ) .getIntValue ( ) < 2048 and
21+ source .asExpr ( ) = rsaGenParamSpec
22+ )
23+ or
24+ source .asExpr ( ) .( IntegerLiteral ) .getIntValue ( ) < 2048
1525 }
1626
1727 override predicate isSink ( DataFlow:: Node sink ) {
18- exists ( MethodAccess ma |
28+ exists ( MethodAccess ma , VarAccess va |
1929 ma .getMethod ( ) instanceof KeyPairGeneratorInitMethod and
30+ va .getVariable ( )
31+ .getAnAssignedValue ( )
32+ .( JavaSecurityKeyPairGenerator )
33+ .getAlgoSpec ( )
34+ .( StringLiteral )
35+ .getValue ( )
36+ .toUpperCase ( )
37+ .matches ( [ "RSA" , "DSA" , "DH" ] ) and
38+ ma .getQualifier ( ) = va and
2039 sink .asExpr ( ) = ma .getArgument ( 0 )
2140 )
2241 }
@@ -30,15 +49,26 @@ class AsymmetricECCKeyTrackingConfiguration extends DataFlow::Configuration {
3049
3150 override predicate isSource ( DataFlow:: Node source ) {
3251 exists ( ClassInstanceExpr ecGenParamSpec |
33- getECKeySize ( ecGenParamSpec .getArgument ( 0 ) .( StringLiteral ) .getValue ( ) ) < 256 and
52+ getECKeySize ( ecGenParamSpec .getArgument ( 0 ) .( StringLiteral ) .getValue ( ) ) < 256 and // ! can generate EC with just the keysize and not the curve apparently... (based on netty/netty FP example)
3453 source .asExpr ( ) = ecGenParamSpec
3554 )
55+ or
56+ source .asExpr ( ) .( IntegerLiteral ) .getIntValue ( ) < 256
3657 }
3758
3859 override predicate isSink ( DataFlow:: Node sink ) {
39- exists ( MethodAccess ma |
60+ exists ( MethodAccess ma , VarAccess va |
4061 ma .getMethod ( ) instanceof KeyPairGeneratorInitMethod and
41- ma .getArgument ( 0 ) .getType ( ) instanceof ECGenParameterSpec and
62+ //ma.getArgument(0).getType() instanceof ECGenParameterSpec and // ! can generate EC with just the keysize and not the curve apparently... (based on netty/netty FP example)
63+ va .getVariable ( )
64+ .getAnAssignedValue ( )
65+ .( JavaSecurityKeyPairGenerator )
66+ .getAlgoSpec ( )
67+ .( StringLiteral )
68+ .getValue ( )
69+ .toUpperCase ( )
70+ .matches ( [ "EC%" ] ) and
71+ ma .getQualifier ( ) = va and
4272 sink .asExpr ( ) = ma .getArgument ( 0 )
4373 )
4474 }
@@ -51,13 +81,21 @@ class SymmetricKeyTrackingConfiguration extends DataFlow::Configuration {
5181 SymmetricKeyTrackingConfiguration ( ) { this = "SymmetricKeyTrackingConfiguration2" }
5282
5383 override predicate isSource ( DataFlow:: Node source ) {
54- source .asExpr ( ) instanceof IntegerLiteral and // ! this works with current test cases, but reconsider IntegerLiteral when variables are used
55- source .toString ( ) .toInt ( ) < 128
84+ source .asExpr ( ) .( IntegerLiteral ) .getIntValue ( ) < 128
5685 }
5786
5887 override predicate isSink ( DataFlow:: Node sink ) {
59- exists ( MethodAccess ma |
88+ exists ( MethodAccess ma , VarAccess va |
6089 ma .getMethod ( ) instanceof KeyGeneratorInitMethod and
90+ va .getVariable ( )
91+ .getAnAssignedValue ( )
92+ .( JavaxCryptoKeyGenerator )
93+ .getAlgoSpec ( )
94+ .( StringLiteral )
95+ .getValue ( )
96+ .toUpperCase ( )
97+ .matches ( [ "AES" ] ) and
98+ ma .getQualifier ( ) = va and
6199 sink .asExpr ( ) = ma .getArgument ( 0 )
62100 )
63101 }
@@ -77,6 +115,11 @@ private class ECGenParameterSpec extends RefType {
77115 ECGenParameterSpec ( ) { this .hasQualifiedName ( "java.security.spec" , "ECGenParameterSpec" ) }
78116}
79117
118+ /** The Java class `java.security.spec.ECGenParameterSpec`. */
119+ private class RSAGenParameterSpec extends RefType {
120+ RSAGenParameterSpec ( ) { this .hasQualifiedName ( "java.security.spec" , "RSAKeyGenParameterSpec" ) }
121+ }
122+
80123// ! move to Encryption.qll?
81124/** Returns the key size in the EC algorithm string */
82125bindingset [ algorithm]
0 commit comments