|
| 1 | +private import experimental.quantum.Language |
| 2 | +private import experimental.quantum.OpenSSL.LibraryDetector |
| 3 | +private import experimental.quantum.OpenSSL.CtxFlow as CTXFlow |
| 4 | +private import OpenSSLOperationBase |
| 5 | +private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers |
| 6 | +private import semmle.code.cpp.dataflow.new.DataFlow |
| 7 | + |
| 8 | +private module AlgGetterToAlgConsumerConfig implements DataFlow::ConfigSig { |
| 9 | + predicate isSource(DataFlow::Node source) { |
| 10 | + exists(OpenSSLAlgorithmValueConsumer c | c.getResultNode() = source) |
| 11 | + } |
| 12 | + |
| 13 | + predicate isSink(DataFlow::Node sink) { |
| 14 | + exists(ECKeyGenOperation c | c.getAlgorithmArg() = sink.asExpr()) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +private module AlgGetterToAlgConsumerFlow = DataFlow::Global<AlgGetterToAlgConsumerConfig>; |
| 19 | + |
| 20 | +class ECKeyGenOperation extends OpenSSLOperation, Crypto::KeyGenerationOperationInstance { |
| 21 | + ECKeyGenOperation() { |
| 22 | + this.(Call).getTarget().getName() = "EC_KEY_generate_key" and |
| 23 | + isPossibleOpenSSLFunction(this.(Call).getTarget()) |
| 24 | + } |
| 25 | + |
| 26 | + override Expr getOutputArg() { |
| 27 | + result = this.(Call) // return value of call |
| 28 | + } |
| 29 | + |
| 30 | + Expr getAlgorithmArg() { result = this.(Call).getArgument(0) } |
| 31 | + |
| 32 | + override Expr getInputArg() { |
| 33 | + // there is no 'input', in the sense that no data is being manipualted by the operation. |
| 34 | + // There is an input of an algorithm, but that is not the intention of the operation input arg. |
| 35 | + none() |
| 36 | + } |
| 37 | + |
| 38 | + override Crypto::KeyArtifactType getOutputKeyType() { result = Crypto::TAsymmetricKeyType() } |
| 39 | + |
| 40 | + override Crypto::ArtifactOutputDataFlowNode getOutputKeyArtifact() { |
| 41 | + result = this.getOutputNode() |
| 42 | + } |
| 43 | + |
| 44 | + override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() { |
| 45 | + AlgGetterToAlgConsumerFlow::flow(result.(OpenSSLAlgorithmValueConsumer).getResultNode(), |
| 46 | + DataFlow::exprNode(this.getAlgorithmArg())) |
| 47 | + } |
| 48 | + |
| 49 | + override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { |
| 50 | + none() // no explicit key size, inferred from algorithm |
| 51 | + } |
| 52 | + |
| 53 | + override int getKeySizeFixed() { |
| 54 | + none() |
| 55 | + // TODO: marked as none as the operation itself has no key size, it |
| 56 | + // comes from the algorithm source, but note we could grab the |
| 57 | + // algorithm source and get the key size (see below). |
| 58 | + // We may need to reconsider what is the best approach here. |
| 59 | + // result = |
| 60 | + // this.getAnAlgorithmValueConsumer() |
| 61 | + // .getAKnownAlgorithmSource() |
| 62 | + // .(Crypto::EllipticCurveInstance) |
| 63 | + // .getKeySize() |
| 64 | + } |
| 65 | +} |
0 commit comments