|
1 | 1 | private import experimental.quantum.Language
|
2 | 2 | private import experimental.quantum.OpenSSL.CtxFlow as CTXFlow
|
3 |
| -private import EVPCipherInitializer |
4 | 3 | private import OpenSSLOperationBase
|
5 | 4 | private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
|
6 | 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 | +// TODO: need to add key consumer |
| 34 | +abstract class EVP_Cipher_Initializer extends EVPInitialize { |
| 35 | + override Expr getAlgorithmArg() { result = this.(Call).getArgument(1) } |
| 36 | + |
| 37 | + abstract Expr getOperationSubtypeArg(); |
| 38 | + |
| 39 | + override Crypto::KeyOperationSubtype getKeyOperationSubtype() { |
| 40 | + if this.(Call).getTarget().getName().toLowerCase().matches("%encrypt%") |
| 41 | + then result instanceof Crypto::TEncryptMode |
| 42 | + else |
| 43 | + if this.(Call).getTarget().getName().toLowerCase().matches("%decrypt%") |
| 44 | + then result instanceof Crypto::TDecryptMode |
| 45 | + else |
| 46 | + if exists(getEncConfigValue(this.getOperationSubtypeArg())) |
| 47 | + then result = intToCipherOperationSubtype(getEncConfigValue(this.getOperationSubtypeArg())) |
| 48 | + else result instanceof Crypto::TUnknownKeyOperationMode |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +abstract class EVP_EX_Initializer extends EVP_Cipher_Initializer { |
| 53 | + override Expr getKeyArg() { result = this.(Call).getArgument(3) } |
| 54 | + |
| 55 | + override Expr getIVArg() { result = this.(Call).getArgument(4) } |
| 56 | +} |
| 57 | + |
| 58 | +abstract class EVP_EX2_Initializer extends EVP_Cipher_Initializer { |
| 59 | + override Expr getKeyArg() { result = this.(Call).getArgument(2) } |
| 60 | + |
| 61 | + override Expr getIVArg() { result = this.(Call).getArgument(3) } |
| 62 | +} |
| 63 | + |
| 64 | +class EVP_Cipher_EX_Init_Call extends EVP_EX_Initializer { |
| 65 | + EVP_Cipher_EX_Init_Call() { |
| 66 | + this.(Call).getTarget().getName() in [ |
| 67 | + "EVP_EncryptInit_ex", "EVP_DecryptInit_ex", "EVP_CipherInit_ex" |
| 68 | + ] |
| 69 | + } |
| 70 | + |
| 71 | + override Expr getOperationSubtypeArg() { |
| 72 | + this.(Call).getTarget().getName().toLowerCase().matches("%cipherinit%") and |
| 73 | + result = this.(Call).getArgument(5) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class EVP_Cipher_EX2_or_Simple_Init_Call extends EVP_EX2_Initializer { |
| 78 | + EVP_Cipher_EX2_or_Simple_Init_Call() { |
| 79 | + this.(Call).getTarget().getName() in [ |
| 80 | + "EVP_EncryptInit_ex2", "EVP_DecryptInit_ex2", "EVP_CipherInit_ex2", "EVP_EncryptInit", |
| 81 | + "EVP_DecryptInit", "EVP_CipherInit" |
| 82 | + ] |
| 83 | + } |
| 84 | + |
| 85 | + override Expr getOperationSubtypeArg() { |
| 86 | + this.(Call).getTarget().getName().toLowerCase().matches("%cipherinit%") and |
| 87 | + result = this.(Call).getArgument(4) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +class EVP_CipherInit_SKEY_Call extends EVP_EX2_Initializer { |
| 92 | + EVP_CipherInit_SKEY_Call() { this.(Call).getTarget().getName() in ["EVP_CipherInit_SKEY"] } |
| 93 | + |
| 94 | + override Expr getOperationSubtypeArg() { result = this.(Call).getArgument(5) } |
| 95 | +} |
| 96 | + |
| 97 | +class EVPCipherInitializerAlgorithmArgument extends Expr { |
| 98 | + EVPCipherInitializerAlgorithmArgument() { |
| 99 | + exists(EVP_Cipher_Initializer initCall | this = initCall.getAlgorithmArg()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class EVPCipherInitializerKeyArgument extends Expr { |
| 104 | + EVPCipherInitializerKeyArgument() { |
| 105 | + exists(EVP_Cipher_Initializer initCall | this = initCall.getKeyArg()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +class EVPCipherInitializerIVArgument extends Expr { |
| 110 | + EVPCipherInitializerIVArgument() { |
| 111 | + exists(EVP_Cipher_Initializer initCall | this = initCall.getIVArg()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
7 | 115 | class EVP_Cipher_Update_Call extends EVPUpdate {
|
8 | 116 | EVP_Cipher_Update_Call() {
|
9 | 117 | this.(Call).getTarget().getName() in [
|
|
0 commit comments