|
| 1 | +/** |
| 2 | + * Provides classes for modeling OpenSSL's EVP signature operations |
| 3 | + */ |
| 4 | + |
| 5 | +private import experimental.quantum.Language |
| 6 | +private import OpenSSLOperationBase |
| 7 | +private import experimental.quantum.OpenSSL.CtxFlow |
| 8 | +private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers |
| 9 | +private import experimental.quantum.OpenSSL.Operations.OpenSSLOperations |
| 10 | + |
| 11 | +module OpenSSLKeyGenToArgConfig implements DataFlow::ConfigSig { |
| 12 | + predicate isSource(DataFlow::Node source) { |
| 13 | + exists(Crypto::KeyGenerationOperationInstance keygen | keygen.getOutputKeyArtifact() = source) |
| 14 | + } |
| 15 | + |
| 16 | + predicate isSink(DataFlow::Node sink) { exists(Call c | c.getAnArgument() = sink.asExpr()) } |
| 17 | +} |
| 18 | + |
| 19 | +module OpenSSLKeyGenToArgFlow = TaintTracking::Global<OpenSSLKeyGenToArgConfig>; |
| 20 | + |
| 21 | +// TODO: verification functions |
| 22 | +class EVP_Signature_Initializer extends EVPInitialize { |
| 23 | + EVP_Signature_Initializer() { |
| 24 | + this.(Call).getTarget().getName() in [ |
| 25 | + "EVP_DigestSignInit", "EVP_DigestSignInit_ex", "EVP_SignInit", "EVP_SignInit_ex", |
| 26 | + "EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex", "EVP_PKEY_sign_init_ex2", |
| 27 | + "EVP_PKEY_sign_message_init" |
| 28 | + ] |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the algorithm associated with this initialization by following |
| 33 | + * where the algorithm is set through the context argument. |
| 34 | + */ |
| 35 | + Expr getAlgorithmArgFromCtx() { |
| 36 | + // exists(EVPPKeyAlgorithmConsumer source, DataFlow::Node sink | |
| 37 | + // result = source.getInputNode().asExpr() and |
| 38 | + // sink.asExpr() = this.getContextArg() and |
| 39 | + // OpenSSLCtxSourceToArgumentFlow::flow(source.getResultNode(), sink) |
| 40 | + // ) |
| 41 | + // or |
| 42 | + result = this.getAlgorithmArgFromKey(this.getKeyArgFromCtx()) |
| 43 | + } |
| 44 | + |
| 45 | + Expr getAlgorithmArgFromKey(Expr keyArg) { |
| 46 | + exists(Crypto::KeyGenerationOperationInstance keygen | |
| 47 | + OpenSSLKeyGenToArgFlow::flow(keygen.getOutputKeyArtifact(), DataFlow::exprNode(keyArg)) and |
| 48 | + result = keygen.(OpenSSLOperation).getAlgorithmArg() |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets the argument ingesting a key |
| 54 | + * by tracing the context arg back to a context creation |
| 55 | + */ |
| 56 | + Expr getKeyArgFromCtx() { |
| 57 | + exists(Call contextCreationCall | |
| 58 | + ctxArgOrRetFlowsToCtxArg(contextCreationCall, this.getContextArg()) and |
| 59 | + ( |
| 60 | + contextCreationCall.getTarget().getName() = "EVP_PKEY_CTX_new" and |
| 61 | + result = contextCreationCall.getArgument(0) |
| 62 | + or |
| 63 | + contextCreationCall.getTarget().getName() = "EVP_PKEY_CTX_new_from_pkey" and |
| 64 | + result = contextCreationCall.getArgument(1) |
| 65 | + ) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + override Expr getAlgorithmArg() { |
| 70 | + // explicit algorithm as argument |
| 71 | + this.(Call).getTarget().getName() in ["EVP_PKEY_sign_init_ex2", "EVP_PKEY_sign_message_init"] and |
| 72 | + result = this.(Call).getArgument(1) |
| 73 | + // or |
| 74 | + // // algorithm (and key) specified in the context |
| 75 | + // this.(Call).getTarget().getName() in ["EVP_PKEY_sign_init", "EVP_PKEY_sign_init_ex"] and |
| 76 | + // result = getAlgorithmArgFromCtx() |
| 77 | + // or |
| 78 | + // // algorithm specified by the key |
| 79 | + // this.(Call).getTarget().getName() in ["EVP_DigestSignInit", "EVP_DigestSignInit_ex"] and |
| 80 | + // result = getAlgorithmArgFromKey() |
| 81 | + // // NOTE: for EVP_SignInit and EVP_SignInit_ex the algorithm is not specified |
| 82 | + // // rather the algorithm is specified by the key used for signing later in a final call. |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the key argument if there is one. |
| 87 | + * If the key was provided via the context, we track it to the context. |
| 88 | + */ |
| 89 | + override Expr getKeyArg() { |
| 90 | + this.(Call).getTarget().getName() = "EVP_DigestSignInit" and |
| 91 | + result = this.(Call).getArgument(4) |
| 92 | + or |
| 93 | + this.(Call).getTarget().getName() = "EVP_DigestSignInit_ex" and |
| 94 | + result = this.(Call).getArgument(5) |
| 95 | + or |
| 96 | + this.(Call).getTarget().getName().matches("EVP_PKEY_%") and |
| 97 | + result = this.getKeyArgFromCtx() |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Signing, verification or unknown. |
| 102 | + */ |
| 103 | + override Crypto::KeyOperationSubtype getKeyOperationSubtype() { |
| 104 | + if this.(Call).getTarget().getName().toLowerCase().matches("%sign%") |
| 105 | + then result instanceof Crypto::TSignMode |
| 106 | + else |
| 107 | + if this.(Call).getTarget().getName().toLowerCase().matches("%verify%") |
| 108 | + then result instanceof Crypto::TVerifyMode |
| 109 | + else result instanceof Crypto::TUnknownKeyOperationMode |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +class EVP_Signature_Update_Call extends EVPUpdate { |
| 114 | + EVP_Signature_Update_Call() { |
| 115 | + this.(Call).getTarget().getName() in [ |
| 116 | + "EVP_DigestSignUpdate", "EVP_SignUpdate", "EVP_PKEY_sign_message_update" |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Input is the message to sign. |
| 122 | + */ |
| 123 | + override Expr getInputArg() { result = this.(Call).getArgument(1) } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * We model output explicit output arguments as predicate to use it in constructors. |
| 128 | + * The predicate must cover all EVP_Signature_Operation subclasses. |
| 129 | + */ |
| 130 | +private Expr signatureOperationOutputArg(Call call) { |
| 131 | + if call.getTarget().getName() = "EVP_SignFinal_ex" |
| 132 | + then result = call.getArgument(2) |
| 133 | + else result = call.getArgument(1) |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Base configuration for all EVP signature operations. |
| 138 | + */ |
| 139 | +abstract class EVP_Signature_Operation extends EVPOperation, Crypto::SignatureOperationInstance { |
| 140 | + EVP_Signature_Operation() { |
| 141 | + this.(Call).getTarget().getName().matches("EVP_%") and |
| 142 | + // NULL output argument means the call is to get the size of the signature and such call is not an operation |
| 143 | + ( |
| 144 | + not exists(signatureOperationOutputArg(this).getValue()) |
| 145 | + or |
| 146 | + signatureOperationOutputArg(this).getValue() != "0" |
| 147 | + ) |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Signing, verification or unknown. |
| 152 | + */ |
| 153 | + override Crypto::KeyOperationSubtype getKeyOperationSubtype() { |
| 154 | + // TODO: if this KeyOperationSubtype does not match initialization call's KeyOperationSubtype then we found a bug |
| 155 | + if this.(Call).getTarget().getName().toLowerCase().matches("%sign%") |
| 156 | + then result instanceof Crypto::TSignMode |
| 157 | + else |
| 158 | + if this.(Call).getTarget().getName().toLowerCase().matches("%verify%") |
| 159 | + then result instanceof Crypto::TVerifyMode |
| 160 | + else result instanceof Crypto::TUnknownKeyOperationMode |
| 161 | + } |
| 162 | + |
| 163 | + override Crypto::ConsumerInputDataFlowNode getNonceConsumer() { |
| 164 | + // TODO: some signing operations may have explicit nonce generators |
| 165 | + none() |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Keys provided in the initialization call or in a context are found by this method. |
| 170 | + * Keys in explicit arguments are found by overriden methods in extending classes. |
| 171 | + */ |
| 172 | + override Crypto::ConsumerInputDataFlowNode getKeyConsumer() { |
| 173 | + result = DataFlow::exprNode(this.getInitCall().getKeyArg()) |
| 174 | + } |
| 175 | + |
| 176 | + override Crypto::ArtifactOutputDataFlowNode getOutputArtifact() { |
| 177 | + result = EVPOperation.super.getOutputArtifact() |
| 178 | + } |
| 179 | + |
| 180 | + override Crypto::ConsumerInputDataFlowNode getInputConsumer() { |
| 181 | + result = EVPOperation.super.getInputConsumer() |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * TODO: only signing operations for now, change when verificaiton is added |
| 186 | + */ |
| 187 | + override Crypto::ConsumerInputDataFlowNode getSignatureConsumer() { none() } |
| 188 | +} |
| 189 | +// class EVP_Signature_Call extends EVPOperation, EVP_Signature_Operation { |
| 190 | +// EVP_Signature_Call() { this.(Call).getTarget().getName() in ["EVP_DigestSign", "EVP_PKEY_sign"] } |
| 191 | +// /** |
| 192 | +// * Output is the signature. |
| 193 | +// */ |
| 194 | +// override Expr getOutputArg() { result = signatureOperationOutputArg(this) } |
| 195 | +// /** |
| 196 | +// * Input is the message to sign. |
| 197 | +// */ |
| 198 | +// override Expr getInputArg() { result = this.(Call).getArgument(3) } |
| 199 | +// } |
| 200 | +// class EVP_Signature_Final_Call extends EVPFinal, EVP_Signature_Operation { |
| 201 | +// EVP_Signature_Final_Call() { |
| 202 | +// this.(Call).getTarget().getName() in [ |
| 203 | +// "EVP_DigestSignFinal", "EVP_SignFinal_ex", "EVP_SignFinal", "EVP_PKEY_sign_message_final" |
| 204 | +// ] |
| 205 | +// } |
| 206 | +// override Expr getAlgorithmArg() { |
| 207 | +// none() |
| 208 | +// // // algorithm specified by the key and the key is provided in this operation |
| 209 | +// // if this.(Call).getTarget().getName() in ["EVP_SignFinal", "EVP_SignFinal_ex"] |
| 210 | +// // then result = getAlgorithmFromKey(this.getKeyConsumer().asExpr()) |
| 211 | +// // else |
| 212 | +// // // or find algorithm in the initialization call |
| 213 | +// // result = EVP_Signature_Operation.super.getAlgorithmArg() |
| 214 | +// } |
| 215 | +// override Crypto::ConsumerInputDataFlowNode getKeyConsumer() { |
| 216 | +// // key provided as an argument |
| 217 | +// if this.(Call).getTarget().getName() in ["EVP_SignFinal", "EVP_SignFinal_ex"] |
| 218 | +// then result = DataFlow::exprNode(this.(Call).getArgument(3)) |
| 219 | +// else |
| 220 | +// // or find key in the initialization call |
| 221 | +// result = EVP_Signature_Operation.super.getKeyConsumer() |
| 222 | +// } |
| 223 | +// /** |
| 224 | +// * Output is the signature. |
| 225 | +// */ |
| 226 | +// override Expr getOutputArg() { result = signatureOperationOutputArg(this) } |
| 227 | +// } |
0 commit comments