@@ -13,6 +13,18 @@ predicate cipher_modes(string mode) {mode = ["NONE", "CBC", "CCM", "CFB", "CFBx"
13
13
//todo same as above, OAEPWith has asuffix type
14
14
predicate cipher_padding ( string padding ) { padding = [ "NoPadding" , "ISO10126Padding" , "OAEPPadding" , "OAEPWith" , "PKCS1Padding" , "PKCS5Padding" , "SSL3Padding" ] }
15
15
16
+
17
+ abstract class BlockCiper extends Crypto:: Algorithm {
18
+ CipherAlgorithmStringLiteral alg ;
19
+ CipherAlgorithmMode mode ;
20
+ CipherAlgorithmPadding padding ;
21
+
22
+
23
+ CipherAlgorithmStringLiteral getAlg ( ) { result = alg }
24
+ CipherAlgorithmMode getMode ( ) { result = mode }
25
+
26
+ CipherAlgorithmPadding getPadding ( ) { result = padding }
27
+ }
16
28
/**
17
29
* Symmetric algorithms
18
30
*/
@@ -45,6 +57,9 @@ class CipherInstance extends Call {
45
57
Expr getAlgorithmArg ( ) { result = this .getArgument ( 0 ) }
46
58
}
47
59
60
+ /**
61
+ * this may be specified either in the ALG/MODE/PADDING or just ALG format
62
+ */
48
63
class CipherAlgorithmStringLiteral extends Crypto:: NodeBase instanceof StringLiteral {
49
64
CipherAlgorithmStringLiteral ( ) { cipher_names ( this .getValue ( ) .splitAt ( "/" ) ) }
50
65
@@ -53,20 +68,28 @@ class CipherAlgorithmStringLiteral extends Crypto::NodeBase instanceof StringLit
53
68
string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) }
54
69
}
55
70
56
- class CipherAlgorithmModeStringLiteral extends Crypto:: NodeBase instanceof StringLiteral {
57
- CipherAlgorithmModeStringLiteral ( ) { cipher_modes ( this .getValue ( ) .splitAt ( "/" ) ) }
71
+ abstract class CipherAlgorithmMode extends Crypto:: NodeBase {
72
+ string getValue ( ) { result = "" }
73
+ }
74
+
75
+ class CipherAlgorithmModeStringLiteral extends CipherAlgorithmMode instanceof StringLiteral {
76
+ CipherAlgorithmModeStringLiteral ( ) { cipher_modes ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
58
77
59
78
override string toString ( ) { result = this .( StringLiteral ) .toString ( ) }
60
79
61
- string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) }
80
+ override string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) . regexpCapture ( ".*/(.*)/.*" , 1 ) }
62
81
}
63
82
64
- class CipherAlgorithmPaddingStringLiteral extends Crypto:: NodeBase instanceof StringLiteral {
65
- CipherAlgorithmPaddingStringLiteral ( ) { cipher_padding ( this .getValue ( ) .splitAt ( "/" ) ) }
83
+ abstract class CipherAlgorithmPadding extends Crypto:: NodeBase {
84
+ string getValue ( ) { result = "" }
85
+ }
86
+
87
+ class CipherAlgorithmPaddingStringLiteral extends CipherAlgorithmPadding instanceof StringLiteral {
88
+ CipherAlgorithmPaddingStringLiteral ( ) { cipher_padding ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
66
89
67
90
override string toString ( ) { result = this .( StringLiteral ) .toString ( ) }
68
91
69
- string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) }
92
+ override string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) . regexpCapture ( ".*/.*/(.*)" , 1 ) }
70
93
}
71
94
72
95
private module AlgorithmStringToFetchConfig implements DataFlow:: ConfigSig {
@@ -79,27 +102,32 @@ class CipherAlgorithmStringLiteral extends Crypto::NodeBase instanceof StringLit
79
102
80
103
module AlgorithmStringToFetchFlow = DataFlow:: Global< AlgorithmStringToFetchConfig > ;
81
104
82
- predicate algorithmStringToCipherInstanceArgFlow ( string name , CipherAlgorithmStringLiteral origin , Expr arg ) {
105
+ predicate algorithmStringToCipherInstanceArgFlow ( string name , CipherAlgorithmStringLiteral origin , CipherAlgorithmModeStringLiteral mode , CipherAlgorithmPaddingStringLiteral padding , Expr arg ) {
83
106
exists ( CipherInstance sinkCall |
84
- origin .getValue ( ) .toUpperCase ( ) = name and
107
+ origin .getValue ( ) .splitAt ( "/" ) = name and
108
+ origin = mode and
109
+ origin = padding and
85
110
arg = sinkCall .getAlgorithmArg ( ) and
86
111
AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( origin ) , DataFlow:: exprNode ( arg ) )
87
112
)
88
113
}
89
114
90
- class AES extends SymmetricAlgorithm instanceof Expr {
91
- CipherAlgorithmStringLiteral origin ;
115
+ /**
116
+ * A class to represent when AES is used AND it has literal mode and padding provided
117
+ * this does not capture the use without
118
+ */
119
+ class AESLiteral extends SymmetricAlgorithm , BlockCiper instanceof Expr {
92
120
93
- AES ( ) { algorithmStringToCipherInstanceArgFlow ( "AES" , origin , this ) }
121
+
122
+ AESLiteral ( ) { algorithmStringToCipherInstanceArgFlow ( "AES" , alg , mode , padding , this )
123
+ }
94
124
95
125
override Crypto:: LocatableElement getOrigin ( string name ) {
96
- result = origin and name = origin .toString ( )
126
+ result = alg and name = alg .toString ( )
97
127
}
98
128
99
- override string getAlgorithmName ( ) { result = "AES" }
129
+ override string getAlgorithmName ( ) { result = this . getAlgorithmName ( ) }
100
130
}
101
131
102
132
103
-
104
-
105
133
}
0 commit comments