Skip to content

Commit c3ed454

Browse files
committed
Crypto: Changing fixed key size for the key gen operation for EC key gen to be none, and rely implicitly on the connected algorithm length. (+1 squashed commits) (+1 squashed commits)
Squashed commits: [b7cd7ba] Crypto: Modeled EC key gen for openssl. (+1 squashed commits)
1 parent b564724 commit c3ed454

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmInstances/EllipticCurveAlgorithmInstance.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorith
3535
override string getRawEllipticCurveName() { result = this.(Literal).getValue().toString() }
3636

3737
override Crypto::TEllipticCurveType getEllipticCurveType() {
38-
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.(KnownOpenSSLEllipticCurveAlgorithmConstant)
39-
.getNormalizedName(), _, result)
38+
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.getParsedEllipticCurveName(), _, result)
39+
}
40+
41+
override string getParsedEllipticCurveName() {
42+
result = this.(KnownOpenSSLEllipticCurveAlgorithmConstant).getNormalizedName()
4043
}
4144

4245
override int getKeySize() {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import OpenSSLOperationBase
22
import EVPCipherOperation
33
import EVPHashOperation
4+
import ECKeyGenOperation

0 commit comments

Comments
 (0)