Skip to content

Commit bbbdf89

Browse files
committed
Crypto: OpenSSL ellipitic curve algorithm instances and consumers.
1 parent e7535b3 commit bbbdf89

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cpp
2+
import experimental.quantum.Language
3+
import KnownAlgorithmConstants
4+
import OpenSSLAlgorithmInstanceBase
5+
import AlgToAVCFlow
6+
7+
//ellipticCurveNameToKeySizeAndFamilyMapping(name, size, family)
8+
class KnownOpenSSLEllitpicCurveConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
9+
Crypto::EllipticCurveInstance instanceof KnownOpenSSLEllipticCurveAlgorithmConstant
10+
{
11+
OpenSSLAlgorithmValueConsumer getterCall;
12+
13+
KnownOpenSSLEllitpicCurveConstantAlgorithmInstance() {
14+
// Two possibilities:
15+
// 1) The source is a literal and flows to a getter, then we know we have an instance
16+
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
17+
// Possibility 1:
18+
this instanceof Literal and
19+
exists(DataFlow::Node src, DataFlow::Node sink |
20+
// Sink is an argument to a CipherGetterCall
21+
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
22+
// Source is `this`
23+
src.asExpr() = this and
24+
// This traces to a getter
25+
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
26+
)
27+
or
28+
// Possibility 2:
29+
this instanceof DirectAlgorithmValueConsumer and getterCall = this
30+
}
31+
32+
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
33+
34+
override string getRawEllipticCurveName() { result = this.(Literal).getValue().toString() }
35+
36+
override Crypto::TEllipticCurveType getEllipticCurveType() {
37+
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.(KnownOpenSSLEllipticCurveAlgorithmConstant)
38+
.getNormalizedName(), _, result)
39+
}
40+
41+
override int getKeySize() {
42+
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.(KnownOpenSSLEllipticCurveAlgorithmConstant)
43+
.getNormalizedName(), result, _)
44+
}
45+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class KnownOpenSSLHashAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
6767
}
6868
}
6969

70+
class KnownOpenSSLEllipticCurveAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
71+
string algType;
72+
73+
KnownOpenSSLEllipticCurveAlgorithmConstant() {
74+
resolveAlgorithmFromExpr(this, _, algType) and
75+
algType.toLowerCase().matches("elliptic_curve")
76+
}
77+
}
78+
7079
/**
7180
* Resolves a call to a 'direct algorithm getter', e.g., EVP_MD5()
7281
* This approach to fetching algorithms was used in OpenSSL 1.0.2.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import CipherAlgorithmInstance
33
import PaddingAlgorithmInstance
44
import BlockAlgorithmInstance
55
import HashAlgorithmInstance
6+
import EllipticCurveAlgorithmInstance
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import cpp
2+
import experimental.quantum.Language
3+
import experimental.quantum.OpenSSL.AlgorithmInstances.KnownAlgorithmConstants
4+
import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumerBase
5+
import experimental.quantum.OpenSSL.AlgorithmInstances.OpenSSLAlgorithmInstances
6+
7+
abstract class EllipticCurveValueConsumer extends OpenSSLAlgorithmValueConsumer { }
8+
9+
//https://docs.openssl.org/3.0/man3/EC_KEY_new/#name
10+
class EVPEllipticCurveALgorithmConsumer extends EllipticCurveValueConsumer {
11+
DataFlow::Node valueArgNode;
12+
DataFlow::Node resultNode;
13+
14+
EVPEllipticCurveALgorithmConsumer() {
15+
resultNode.asExpr() = this.(Call) and // in all cases the result is the return
16+
isPossibleOpenSSLFunction(this.(Call).getTarget()) and
17+
(
18+
this.(Call).getTarget().getName() in ["EVP_EC_gen", "EC_KEY_new_by_curve_name"] and
19+
valueArgNode.asExpr() = this.(Call).getArgument(0)
20+
or
21+
this.(Call).getTarget().getName() in [
22+
"EC_KEY_new_by_curve_name_ex", "EVP_PKEY_CTX_set_ec_paramgen_curve_nid"
23+
] and
24+
valueArgNode.asExpr() = this.(Call).getArgument(2)
25+
)
26+
}
27+
28+
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
29+
exists(OpenSSLAlgorithmInstance i | i.getAVC() = this and result = i)
30+
}
31+
32+
override DataFlow::Node getResultNode() { result = resultNode }
33+
34+
override Crypto::ConsumerInputDataFlowNode getInputNode() { result = valueArgNode }
35+
}

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmValueConsumers/OpenSSLAlgorithmValueConsumers.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import CipherAlgorithmValueConsumer
33
import DirectAlgorithmValueConsumer
44
import PaddingAlgorithmValueConsumer
55
import HashAlgorithmValueConsumer
6+
import EllipticCurveAlgorithmValueConsumer

0 commit comments

Comments
 (0)