@@ -3,60 +3,31 @@ private import experimental.quantum.OpenSSL.CtxFlow
33private import OpenSSLOperationBase
44private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
55
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-
336// TODO: need to add key consumer
347abstract class EVP_Cipher_Initializer extends EvpKeyOperationSubtypeInitializer ,
35- EvpAlgorithmInitializer , EvpKeyInitializer , EvpIVInitializer
8+ EvpPrimaryAlgorithmInitializer , EvpKeyInitializer , EvpIVInitializer
369{
37- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
10+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
3811
3912 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- }
5413}
5514
5615abstract 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+ }
5823
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+ }
6031}
6132
6233abstract class EVP_EX2_Initializer extends EVP_Cipher_Initializer {
@@ -65,19 +36,26 @@ abstract class EVP_EX2_Initializer extends EVP_Cipher_Initializer {
6536 override Expr getIVArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
6637}
6738
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 ( ) {
7041 this .( Call ) .getTarget ( ) .getName ( ) in [
7142 "EVP_EncryptInit_ex" , "EVP_DecryptInit_ex" , "EVP_CipherInit_ex"
7243 ]
7344 }
7445
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
7649 this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%cipherinit%" ) and
7750 result = this .( Call ) .getArgument ( 5 )
7851 }
7952}
8053
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
8159class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
8260 EVP_Cipher_EX2_or_Simple_Init_Call ( ) {
8361 this .( Call ) .getTarget ( ) .getName ( ) in [
@@ -86,7 +64,7 @@ class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
8664 ]
8765 }
8866
89- override Expr getOperationSubtypeArg ( ) {
67+ override Expr getKeyOperationSubtypeArg ( ) {
9068 this .( Call ) .getTarget ( ) .getName ( ) .toLowerCase ( ) .matches ( "%cipherinit%" ) and
9169 result = this .( Call ) .getArgument ( 4 )
9270 }
@@ -95,7 +73,7 @@ class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer {
9573class EVP_CipherInit_SKEY_Call extends EVP_EX2_Initializer {
9674 EVP_CipherInit_SKEY_Call ( ) { this .( Call ) .getTarget ( ) .getName ( ) in [ "EVP_CipherInit_SKEY" ] }
9775
98- override Expr getOperationSubtypeArg ( ) { result = this .( Call ) .getArgument ( 5 ) }
76+ override Expr getKeyOperationSubtypeArg ( ) { result = this .( Call ) .getArgument ( 5 ) }
9977}
10078
10179class EVP_Cipher_Update_Call extends EvpUpdate {
@@ -105,7 +83,7 @@ class EVP_Cipher_Update_Call extends EvpUpdate {
10583 ]
10684 }
10785
108- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
86+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
10987
11088 override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
11189
@@ -154,10 +132,10 @@ class EVP_Cipher_Call extends EvpOperation, EVP_Cipher_Operation {
154132 override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 2 ) }
155133
156134 override Expr getAlgorithmArg ( ) {
157- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
135+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
158136 }
159137
160- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
138+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
161139}
162140
163141class EVP_Cipher_Final_Call extends EVPFinal , EVP_Cipher_Operation {
@@ -178,10 +156,10 @@ class EVP_Cipher_Final_Call extends EVPFinal, EVP_Cipher_Operation {
178156 }
179157
180158 override Expr getAlgorithmArg ( ) {
181- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
159+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
182160 }
183161
184- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
162+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
185163}
186164
187165/**
@@ -195,9 +173,9 @@ class Evp_PKey_Cipher_Operation extends EVP_Cipher_Operation {
195173
196174 override Expr getInputArg ( ) { result = this .( Call ) .getArgument ( 3 ) }
197175
198- override CtxPointerSource getContextArg ( ) { result = this .( Call ) .getArgument ( 0 ) }
176+ override CtxPointerSource getContext ( ) { result = this .( Call ) .getArgument ( 0 ) }
199177
200178 override Expr getAlgorithmArg ( ) {
201- result = this .getInitCall ( ) .( EvpAlgorithmInitializer ) .getAlgorithmArg ( )
179+ result = this .getInitCall ( ) .( EvpPrimaryAlgorithmInitializer ) .getAlgorithmArg ( )
202180 }
203181}
0 commit comments