@@ -7,56 +7,80 @@ module JCAModel {
7
7
abstract class EncryptionOperation extends Crypto:: EncryptionOperation { }
8
8
9
9
//TODO PBEWith can have suffixes. how to do? enumerate? or match a pattern?
10
- predicate cipher_names ( string algo ) { algo = [ "AES" , "AESWrap" , "AESWrapPad" , "ARCFOUR" , "Blowfish" , "ChaCha20" , "ChaCha20-Poly1305" , "DES" , "DESede" , "DESedeWrap" , "ECIES" , "PBEWith" , "RC2" , "RC4" , "RC5" , "RSA" ] }
11
- //TODO solve the fact that x is an int of various values. same as above... enumerate?
12
- predicate cipher_modes ( string mode ) { mode = [ "NONE" , "CBC" , "CCM" , "CFB" , "CFBx" , "CTR" , "CTS" , "ECB" , "GCM" , "KW" , "KWP" , "OFB" , "OFBx" , "PCBC" ] }
13
- //todo same as above, OAEPWith has asuffix type
14
- predicate cipher_padding ( string padding ) { padding = [ "NoPadding" , "ISO10126Padding" , "OAEPPadding" , "OAEPWith" , "PKCS1Padding" , "PKCS5Padding" , "SSL3Padding" ] }
10
+ predicate cipher_names ( string algo ) {
11
+ algo =
12
+ [
13
+ "AES" , "AESWrap" , "AESWrapPad" , "ARCFOUR" , "Blowfish" , "ChaCha20" , "ChaCha20-Poly1305" ,
14
+ "DES" , "DESede" , "DESedeWrap" , "ECIES" , "PBEWith" , "RC2" , "RC4" , "RC5" , "RSA"
15
+ ]
16
+ }
15
17
18
+ //TODO solve the fact that x is an int of various values. same as above... enumerate?
19
+ predicate cipher_modes ( string mode ) {
20
+ mode =
21
+ [
22
+ "NONE" , "CBC" , "CCM" , "CFB" , "CFBx" , "CTR" , "CTS" , "ECB" , "GCM" , "KW" , "KWP" , "OFB" , "OFBx" ,
23
+ "PCBC"
24
+ ]
25
+ }
16
26
17
- ////cipher specifics ----------------------------------------
27
+ //todo same as above, OAEPWith has asuffix type
28
+ predicate cipher_padding ( string padding ) {
29
+ padding =
30
+ [
31
+ "NoPadding" , "ISO10126Padding" , "OAEPPadding" , "OAEPWith" , "PKCS1Padding" , "PKCS5Padding" ,
32
+ "SSL3Padding"
33
+ ]
34
+ }
18
35
19
- class CipherInstance extends Call {
20
- CipherInstance ( ) { this .getCallee ( ) .hasQualifiedName ( "javax.crypto" , "Cipher" , "getInstance" ) }
36
+ ////cipher specifics ----------------------------------------
37
+ class CipherInstance extends Call {
38
+ CipherInstance ( ) { this .getCallee ( ) .hasQualifiedName ( "javax.crypto" , "Cipher" , "getInstance" ) }
21
39
22
40
Expr getAlgorithmArg ( ) { result = this .getArgument ( 0 ) }
23
41
}
24
42
25
43
/**
26
44
* this may be specified either in the ALG/MODE/PADDING or just ALG format
27
45
*/
28
- class CipherAlgorithmStringLiteral extends StringLiteral {
29
- CipherAlgorithmStringLiteral ( ) { cipher_names ( this .getValue ( ) .splitAt ( "/" ) ) }
46
+ class CipherAlgorithmStringLiteral extends StringLiteral {
47
+ CipherAlgorithmStringLiteral ( ) { cipher_names ( this .getValue ( ) .splitAt ( "/" ) ) }
30
48
}
31
49
32
-
33
50
class ModeOfOperationStringLiteral extends Crypto:: ModeOfOperation instanceof StringLiteral {
34
- ModeOfOperationStringLiteral ( ) { cipher_modes ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
51
+ ModeOfOperationStringLiteral ( ) { cipher_modes ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
35
52
36
- override string getRawAlgorithmName ( ) { result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/(.*)/.*" , 1 ) }
53
+ override string getRawAlgorithmName ( ) {
54
+ result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/(.*)/.*" , 1 )
55
+ }
37
56
38
- override string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/(.*)/.*" , 1 ) }
57
+ override string getValue ( ) {
58
+ result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/(.*)/.*" , 1 )
59
+ }
39
60
61
+ predicate modeToNameMapping ( Crypto:: TModeOperation type , string name ) {
62
+ name = "ECB" and type instanceof Crypto:: ECB
63
+ }
40
64
41
- predicate modeToNameMapping ( Crypto:: TModeOperation type , string name ) {
42
- name = "ECB" and type instanceof Crypto:: ECB
43
- }
44
-
45
- override Crypto:: TModeOperation getModeType ( ) {
65
+ override Crypto:: TModeOperation getModeType ( ) {
46
66
modeToNameMapping ( result , this .getRawAlgorithmName ( ) )
47
67
}
48
68
}
49
69
50
70
abstract class CipherAlgorithmPadding extends Crypto:: NodeBase {
51
- string getValue ( ) { result = "" }
71
+ string getValue ( ) { result = "" }
52
72
}
53
73
54
74
class CipherAlgorithmPaddingStringLiteral extends CipherAlgorithmPadding instanceof StringLiteral {
55
- CipherAlgorithmPaddingStringLiteral ( ) { cipher_padding ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
75
+ CipherAlgorithmPaddingStringLiteral ( ) {
76
+ cipher_padding ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) )
77
+ }
56
78
57
79
override string toString ( ) { result = this .( StringLiteral ) .toString ( ) }
58
80
59
- override string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/.*/(.*)" , 1 ) }
81
+ override string getValue ( ) {
82
+ result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/.*/(.*)" , 1 )
83
+ }
60
84
}
61
85
62
86
private module AlgorithmStringToFetchConfig implements DataFlow:: ConfigSig {
@@ -69,51 +93,59 @@ class CipherAlgorithmStringLiteral extends StringLiteral {
69
93
70
94
module AlgorithmStringToFetchFlow = DataFlow:: Global< AlgorithmStringToFetchConfig > ;
71
95
72
- predicate algorithmStringToCipherInstanceArgFlow ( string name , CipherAlgorithmStringLiteral origin , Expr arg ) {
96
+ predicate algorithmStringToCipherInstanceArgFlow (
97
+ string name , CipherAlgorithmStringLiteral origin , Expr arg
98
+ ) {
73
99
exists ( CipherInstance sinkCall |
74
100
origin .getValue ( ) .splitAt ( "/" ) = name and
75
- arg = sinkCall .getAlgorithmArg ( ) and
76
- AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( origin ) , DataFlow:: exprNode ( arg ) )
101
+ arg = sinkCall and
102
+ AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( origin ) ,
103
+ DataFlow:: exprNode ( sinkCall .getAlgorithmArg ( ) ) )
77
104
)
78
105
}
79
106
80
-
81
- predicate modeStringToCipherInstanceArgFlow ( string name , ModeOfOperationStringLiteral mode , Expr arg ) {
107
+ predicate modeStringToCipherInstanceArgFlow (
108
+ string name , ModeOfOperationStringLiteral mode , Expr arg
109
+ ) {
82
110
exists ( CipherInstance sinkCall |
83
111
mode .getRawAlgorithmName ( ) = name and
84
- arg = sinkCall .getAlgorithmArg ( ) and
85
- AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( mode ) , DataFlow:: exprNode ( arg ) )
112
+ arg = sinkCall and
113
+ AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( mode ) ,
114
+ DataFlow:: exprNode ( sinkCall .getAlgorithmArg ( ) ) )
86
115
)
87
116
}
88
117
89
118
/**
90
- * A class to represent when AES is used AND it has literal mode and padding provided
91
- * this does not capture the use without
119
+ * A class to represent when AES is used
120
+ * AND currently it has literal mode and padding provided
121
+ *
122
+ * this currently does not capture the use without a literal
123
+ * though should be extended to
92
124
*/
93
- // class AESLiteral extends Crypto::SymmetricAlgorithm instanceof Expr {
94
- // CipherAlgorithmStringLiteral alg;
95
- // AESLiteral() { algorithmStringToCipherInstanceArgFlow("AES", alg, this)
96
- // }
125
+ class AESAlgo extends Crypto:: SymmetricAlgorithm instanceof Expr {
126
+ CipherAlgorithmStringLiteral alg ;
97
127
98
- // override Crypto::ModeOfOperation getModeOfOperation(){ modeStringToCipherInstanceArgFlow(result.getAlgorithmName(), result , this)}
128
+ AESAlgo ( ) { algorithmStringToCipherInstanceArgFlow ( "AES" , alg , this ) }
99
129
100
- // override Crypto::LocatableElement getOrigin(string name ) {
101
- // result = alg and name = alg.toString( )
102
- // }
130
+ override Crypto:: ModeOfOperation getModeOfOperation ( ) {
131
+ modeStringToCipherInstanceArgFlow ( result . getAlgorithmName ( ) , result , this )
132
+ }
103
133
104
- // override string getAlgorithmName(){ result = "AES" }
134
+ override Crypto:: LocatableElement getOrigin ( string name ) {
135
+ result = alg and name = alg .toString ( )
136
+ }
105
137
106
- // override string getRawAlgorithmName() { result = alg.getValue() }
138
+ override string getAlgorithmName ( ) { result = "AES" }
107
139
108
- // override Crypto::TSymmetricCipherFamilyType getSymmetricCipherFamilyType () { result instanceof Crypto::AES }
140
+ override string getRawAlgorithmName ( ) { result = alg . getValue ( ) }
109
141
110
- // //temp hacks for testing
111
- // override string getKeySize(Location location){
112
- // result = ""
113
- // }
142
+ override Crypto:: TSymmetricCipherFamilyType getSymmetricCipherFamilyType ( ) {
143
+ result instanceof Crypto:: AES
144
+ }
114
145
115
- // override Crypto::TCipherStructure getCipherType(){ none()}
116
- // }
146
+ //temp hacks for testing
147
+ override string getKeySize ( Location location ) { result = "" }
117
148
118
-
119
- }
149
+ override Crypto:: TCipherStructure getCipherType ( ) { none ( ) }
150
+ }
151
+ }
0 commit comments