@@ -14,37 +14,43 @@ import cpp
1414import semmle.code.cpp.ir.dataflow.DataFlow
1515import semmle.code.cpp.ir.IR
1616
17- int getMinimumKeyStrength ( string func ) {
18- func = "EVP_PKEY_CTX_set_dsa_paramgen_bits" and result = 2048
19- or
20- func = "EVP_PKEY_CTX_set_dh_paramgen_prime_len" and result = 2048
21- or
22- func = "EVP_PKEY_CTX_set_rsa_keygen_bits" and result = 2048
17+ // Holds if `func` is the name of an encryption function that accepts a key size as parameter `paramIndex`
18+ int getMinimumKeyStrength ( string func , int paramIndex ) {
19+ func =
20+ [
21+ "EVP_PKEY_CTX_set_dsa_paramgen_bits" , "DSA_generate_parameters_ex" ,
22+ "EVP_PKEY_CTX_set_rsa_keygen_bits" , "RSA_generate_key_ex" , "RSA_generate_key_fips" ,
23+ "EVP_PKEY_CTX_set_dh_paramgen_prime_len" , "DH_generate_parameters_ex"
24+ ] and
25+ paramIndex = 1 and
26+ result = 2048
2327}
2428
2529class KeyStrengthFlow extends DataFlow:: Configuration {
26- KeyStrengthFlow ( ) {
27- this = "KeyStrengthFlow"
28- }
30+ KeyStrengthFlow ( ) { this = "KeyStrengthFlow" }
2931
3032 override predicate isSource ( DataFlow:: Node node ) {
3133 node .asInstruction ( ) instanceof IntegerConstantInstruction
3234 }
3335
3436 override predicate isSink ( DataFlow:: Node node ) {
35- exists ( FunctionCall fc , string name |
36- node .asExpr ( ) = fc .getArgument ( 1 ) and
37+ exists ( FunctionCall fc , string name , int param |
38+ node .asExpr ( ) = fc .getArgument ( param ) and
3739 fc .getTarget ( ) .hasGlobalName ( name ) and
38- exists ( getMinimumKeyStrength ( name ) )
40+ exists ( getMinimumKeyStrength ( name , param ) )
3941 )
4042 }
4143}
4244
43- from DataFlow:: PathNode source , DataFlow:: PathNode sink , KeyStrengthFlow conf , FunctionCall fc , string name , int bits
45+ from
46+ DataFlow:: PathNode source , DataFlow:: PathNode sink , KeyStrengthFlow conf , FunctionCall fc ,
47+ string name , int bits
4448where
4549 conf .hasFlowPath ( source , sink ) and
4650 sink .getNode ( ) .asExpr ( ) = fc .getArgument ( 1 ) and
4751 fc .getTarget ( ) .hasGlobalName ( name ) and
48- bits = getMinimumKeyStrength ( name ) and
49- source .getNode ( ) .asInstruction ( ) .( ConstantValueInstruction ) .getValue ( ) .toInt ( ) < bits
50- select fc , source , sink , "The key size $@ is insufficient for security" , source , source .toString ( )
52+ bits = getMinimumKeyStrength ( name , _) and
53+ source .getNode ( ) .asInstruction ( ) .( ConstantValueInstruction ) .getValue ( ) .toInt ( ) < bits
54+ select fc , source , sink ,
55+ "The key size $@ is less than the recommended key size of " + bits .toString ( ) + " bits." , source ,
56+ source .toString ( )
0 commit comments