@@ -3,60 +3,31 @@ private import experimental.quantum.OpenSSL.CtxFlow
3
3
private import OpenSSLOperationBase
4
4
private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
5
5
6
- module EncValToInitEncArgConfig implements DataFlow:: ConfigSig {
7
- predicate isSource ( DataFlow:: Node source ) { source .asExpr ( ) .getValue ( ) .toInt ( ) in [ 0 , 1 ] }
8
-
9
- predicate isSink ( DataFlow:: Node sink ) {
10
- exists ( EVP_Cipher_Initializer initCall | sink .asExpr ( ) = initCall .getOperationSubtypeArg ( ) )
11
- }
12
- }
13
-
14
- module EncValToInitEncArgFlow = DataFlow:: Global< EncValToInitEncArgConfig > ;
15
-
16
- int getEncConfigValue ( Expr e ) {
17
- exists ( EVP_Cipher_Initializer initCall | e = initCall .getOperationSubtypeArg ( ) ) and
18
- exists ( DataFlow:: Node a , DataFlow:: Node b |
19
- EncValToInitEncArgFlow:: flow ( a , b ) and b .asExpr ( ) = e and result = a .asExpr ( ) .getValue ( ) .toInt ( )
20
- )
21
- }
22
-
23
- bindingset [ i]
24
- Crypto:: KeyOperationSubtype intToCipherOperationSubtype ( int i ) {
25
- if i = 0
26
- then result instanceof Crypto:: TEncryptMode
27
- else
28
- if i = 1
29
- then result instanceof Crypto:: TDecryptMode
30
- else result instanceof Crypto:: TUnknownKeyOperationMode
31
- }
32
-
33
6
// TODO: need to add key consumer
34
7
abstract class EVP_Cipher_Initializer extends EvpKeyOperationSubtypeInitializer ,
35
- EvpAlgorithmInitializer , EvpKeyInitializer , EvpIVInitializer
8
+ EvpPrimaryAlgorithmInitializer , EvpKeyInitializer , EvpIVInitializer
36
9
{
37
- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
10
+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
38
11
39
12
override Expr getAlgorithmArg ( ) { result = this .( Call ) .getArgument ( 1 ) }
40
-
41
- abstract Expr getOperationSubtypeArg ( ) ;
42
-
43
- override Crypto:: KeyOperationSubtype getKeyOperationSubtype ( ) {
44
- if this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%encrypt%" )
45
- then result instanceof Crypto:: TEncryptMode
46
- else
47
- if this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%decrypt%" )
48
- then result instanceof Crypto:: TDecryptMode
49
- else
50
- if exists ( getEncConfigValue ( this .getOperationSubtypeArg ( ) ) )
51
- then result = intToCipherOperationSubtype ( getEncConfigValue ( this .getOperationSubtypeArg ( ) ) )
52
- else result instanceof Crypto:: TUnknownKeyOperationMode
53
- }
54
13
}
55
14
56
15
abstract class EVP_EX_Initializer extends EVP_Cipher_Initializer {
57
- override Expr getKeyArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
16
+ override Expr getKeyArg ( ) {
17
+ // Null key indicates the key is not actually set
18
+ // This pattern can occur during a multi-step initialization
19
+ // TODO/Note: not flowing 0 to the sink, assuming a direct use of NULL for now
20
+ result = this .( Call ) .getArgument ( 3 ) and
21
+ ( exists ( result .getValue ( ) ) implies result .getValue ( ) .toInt ( ) != 0 )
22
+ }
58
23
59
- override Expr getIVArg ( ) { result = this .( Call ) .getArgument ( 4 ) }
24
+ override Expr getIVArg ( ) {
25
+ // Null IV indicates the IV is not actually set
26
+ // This occurs given that setting the IV sometimes requires first setting the IV size.
27
+ // TODO/Note: not flowing 0 to the sink, assuming a direct use of NULL for now
28
+ result = this .( Call ) .getArgument ( 4 ) and
29
+ ( exists ( result .getValue ( ) ) implies result .getValue ( ) .toInt ( ) != 0 )
30
+ }
60
31
}
61
32
62
33
abstract class EVP_EX2_Initializer extends EVP_Cipher_Initializer {
@@ -65,19 +36,26 @@ abstract class EVP_EX2_Initializer extends EVP_Cipher_Initializer {
65
36
override Expr getIVArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
66
37
}
67
38
68
- class EVP_Cipher_EX_Init_Call extends EVP_EX_Initializer {
69
- EVP_Cipher_EX_Init_Call ( ) {
39
+ class EvpCipherEXInitCall extends EVP_EX_Initializer {
40
+ EvpCipherEXInitCall ( ) {
70
41
this .( Call ) .getTarget ( ) .getName ( ) in [
71
42
"EVP_EncryptInit_ex" , "EVP_DecryptInit_ex" , "EVP_CipherInit_ex"
72
43
]
73
44
}
74
45
75
- override Expr getOperationSubtypeArg ( ) {
46
+ override Expr getKeyOperationSubtypeArg ( ) {
47
+ // NOTE: for EncryptInit and DecryptInit there is no subtype arg
48
+ // the subtype is determined automatically by the initializer based on the operation name
76
49
this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%cipherinit%" ) and
77
50
result = this .( Call ) .getArgument ( 5 )
78
51
}
79
52
}
80
53
54
+ // if this.(Call).getTarget().getName().toLowerCase().matches("%encrypt%")
55
+ // then result instanceof Crypto::TEncryptMode
56
+ // else
57
+ // if this.(Call).getTarget().getName().toLowerCase().matches("%decrypt%")
58
+ // then result instanceof Crypto::TDecryptMode
81
59
class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
82
60
EVP_Cipher_EX2_or_Simple_Init_Call ( ) {
83
61
this .( Call ) .getTarget ( ) .getName ( ) in [
@@ -86,7 +64,7 @@ class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
86
64
]
87
65
}
88
66
89
- override Expr getOperationSubtypeArg ( ) {
67
+ override Expr getKeyOperationSubtypeArg ( ) {
90
68
this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%cipherinit%" ) and
91
69
result = this .( Call ) .getArgument ( 4 )
92
70
}
@@ -95,7 +73,7 @@ class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
95
73
class EVP_CipherInit_SKEY_Call extends EVP_EX2_Initializer {
96
74
EVP_CipherInit_SKEY_Call ( ) { this .( Call ) .getTarget ( ) .getName ( ) in [ "EVP_CipherInit_SKEY" ] }
97
75
98
- override Expr getOperationSubtypeArg ( ) { result = this .( Call ) .getArgument ( 5 ) }
76
+ override Expr getKeyOperationSubtypeArg ( ) { result = this .( Call ) .getArgument ( 5 ) }
99
77
}
100
78
101
79
class EVP_Cipher_Update_Call extends EvpUpdate {
@@ -105,7 +83,7 @@ class EVP_Cipher_Update_Call extends EvpUpdate {
105
83
]
106
84
}
107
85
108
- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
86
+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
109
87
110
88
override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
111
89
@@ -154,10 +132,10 @@ class EVP_Cipher_Call extends EvpOperation, EVP_Cipher_Operation {
154
132
override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 2 ) }
155
133
156
134
override Expr getAlgorithmArg ( ) {
157
- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
135
+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
158
136
}
159
137
160
- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
138
+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
161
139
}
162
140
163
141
class EVP_Cipher_Final_Call extends EVPFinal , EVP_Cipher_Operation {
@@ -178,10 +156,10 @@ class EVP_Cipher_Final_Call extends EVPFinal, EVP_Cipher_Operation {
178
156
}
179
157
180
158
override Expr getAlgorithmArg ( ) {
181
- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
159
+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
182
160
}
183
161
184
- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
162
+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
185
163
}
186
164
187
165
/**
@@ -195,9 +173,9 @@ class Evp_PKey_Cipher_Operation extends EVP_Cipher_Operation {
195
173
196
174
override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
197
175
198
- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
176
+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
199
177
200
178
override Expr getAlgorithmArg ( ) {
201
- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
179
+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
202
180
}
203
181
}
0 commit comments