@@ -14,41 +14,6 @@ predicate cipher_modes(string mode) {mode = ["NONE", "CBC", "CCM", "CFB", "CFBx"
1414predicate cipher_padding ( string padding ) { padding = [ "NoPadding" , "ISO10126Padding" , "OAEPPadding" , "OAEPWith" , "PKCS1Padding" , "PKCS5Padding" , "SSL3Padding" ] }
1515
1616
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- }
28- /**
29- * Symmetric algorithms
30- */
31- abstract class SymmetricAlgorithm extends Crypto:: Algorithm {
32-
33-
34- //TODO figure out how to get this from the Cipher interface, is it explicit?
35- //abstract string getKeySize(Location location);
36-
37- // override predicate properties(string key, string value, Location location) {
38- // super.properties(key, value, location)
39- // or
40- // key = "key_size" and
41- // if exists(this.getKeySize(location))
42- // then value = this.getKeySize(location)
43- // else (
44- // value instanceof Crypto::UnknownPropertyValue and location instanceof UnknownLocation
45- // )
46- // // other properties, like field type are possible, but not modeled until considered necessary
47- // }
48-
49- abstract override string getAlgorithmName ( ) ;
50- }
51-
5217////cipher specifics ----------------------------------------
5318
5419class CipherInstance extends Call {
@@ -60,24 +25,26 @@ class CipherInstance extends Call {
6025 /**
6126 * this may be specified either in the ALG/MODE/PADDING or just ALG format
6227 */
63- class CipherAlgorithmStringLiteral extends Crypto :: NodeBase instanceof StringLiteral {
28+ class CipherAlgorithmStringLiteral extends StringLiteral {
6429 CipherAlgorithmStringLiteral ( ) { cipher_names ( this .getValue ( ) .splitAt ( "/" ) ) }
65-
66- override string toString ( ) { result = this .( StringLiteral ) .toString ( ) }
67-
68- string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) }
6930 }
7031
71- abstract class CipherAlgorithmMode extends Crypto:: NodeBase {
72- string getValue ( ) { result = "" }
73- }
7432
75- class CipherAlgorithmModeStringLiteral extends CipherAlgorithmMode instanceof StringLiteral {
76- CipherAlgorithmModeStringLiteral ( ) { cipher_modes ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
33+ class ModeOfOperationStringLiteral extends Crypto :: ModeOfOperation instanceof StringLiteral {
34+ ModeOfOperationStringLiteral ( ) { cipher_modes ( this .( StringLiteral ) .getValue ( ) .splitAt ( "/" ) ) }
7735
78- override string toString ( ) { result = this .( StringLiteral ) .toString ( ) }
36+ override string getRawAlgorithmName ( ) { result = this .( StringLiteral ) .getValue ( ) . regexpCapture ( ".*/(.*)/.*" , 1 ) }
7937
8038 override string getValue ( ) { result = this .( StringLiteral ) .getValue ( ) .regexpCapture ( ".*/(.*)/.*" , 1 ) }
39+
40+
41+ predicate modeToNameMapping ( Crypto:: TModeOperation type , string name ) {
42+ name = "ECB" and type instanceof Crypto:: ECB
43+ }
44+
45+ override Crypto:: TModeOperation getModeType ( ) {
46+ modeToNameMapping ( result , this .getRawAlgorithmName ( ) )
47+ }
8148 }
8249
8350 abstract class CipherAlgorithmPadding extends Crypto:: NodeBase {
@@ -102,32 +69,51 @@ abstract class CipherAlgorithmMode extends Crypto::NodeBase {
10269
10370 module AlgorithmStringToFetchFlow = DataFlow:: Global< AlgorithmStringToFetchConfig > ;
10471
105- predicate algorithmStringToCipherInstanceArgFlow ( string name , CipherAlgorithmStringLiteral origin , CipherAlgorithmModeStringLiteral mode , CipherAlgorithmPaddingStringLiteral padding , Expr arg ) {
72+ predicate algorithmStringToCipherInstanceArgFlow ( string name , CipherAlgorithmStringLiteral origin , Expr arg ) {
10673 exists ( CipherInstance sinkCall |
10774 origin .getValue ( ) .splitAt ( "/" ) = name and
108- origin = mode and
109- origin = padding and
11075 arg = sinkCall .getAlgorithmArg ( ) and
11176 AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( origin ) , DataFlow:: exprNode ( arg ) )
11277 )
11378 }
11479
80+
81+ predicate modeStringToCipherInstanceArgFlow ( string name , ModeOfOperationStringLiteral mode , Expr arg ) {
82+ exists ( CipherInstance sinkCall |
83+ mode .getRawAlgorithmName ( ) = name and
84+ arg = sinkCall .getAlgorithmArg ( ) and
85+ AlgorithmStringToFetchFlow:: flow ( DataFlow:: exprNode ( mode ) , DataFlow:: exprNode ( arg ) )
86+ )
87+ }
88+
11589 /**
11690 * A class to represent when AES is used AND it has literal mode and padding provided
11791 * this does not capture the use without
11892 */
119- class AESLiteral extends SymmetricAlgorithm , BlockCiper instanceof Expr {
93+ // class AESLiteral extends Crypto::SymmetricAlgorithm instanceof Expr {
94+ // CipherAlgorithmStringLiteral alg;
95+ // AESLiteral() { algorithmStringToCipherInstanceArgFlow("AES", alg, this)
96+ // }
12097
98+ // override Crypto::ModeOfOperation getModeOfOperation(){ modeStringToCipherInstanceArgFlow(result.getAlgorithmName(), result, this)}
12199
122- AESLiteral ( ) { algorithmStringToCipherInstanceArgFlow ( "AES" , alg , mode , padding , this )
123- }
100+ // override Crypto::LocatableElement getOrigin(string name) {
101+ // result = alg and name = alg.toString()
102+ // }
124103
125- override Crypto:: LocatableElement getOrigin ( string name ) {
126- result = alg and name = alg .toString ( )
127- }
104+ // override string getAlgorithmName(){ result = "AES" }
128105
129- override string getAlgorithmName ( ) { result = alg .getValue ( ) }
130- }
106+ // override string getRawAlgorithmName(){ result = alg.getValue()}
107+
108+ // override Crypto::TSymmetricCipherFamilyType getSymmetricCipherFamilyType() { result instanceof Crypto::AES}
109+
110+ // //temp hacks for testing
111+ // override string getKeySize(Location location){
112+ // result = ""
113+ // }
114+
115+ // override Crypto::TCipherStructure getCipherType(){ none()}
116+ // }
131117
132118
133119}
0 commit comments