Skip to content

Commit 4901cdf

Browse files
committed
Crypto: Refactor and change casts to super
1 parent 9673b81 commit 4901cdf

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

cpp/ql/lib/experimental/quantum/Language.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ module GenericDataSourceFlow = TaintTracking::Global<GenericDataSourceFlowConfig
9494
private class ConstantDataSource extends Crypto::GenericConstantSourceInstance instanceof OpenSslGenericSourceCandidateLiteral
9595
{
9696
override DataFlow::Node getOutputNode() {
97-
// A literal can be a string or an int, so handling both indirect and direct cases
97+
// OpenSSL algorithms may be referenced either by string name or by numeric ID:
98+
// String names (e.g. "AES-256-CBC") appear in the AST as character pointer
99+
// literals. For these we must use `asIndirectExpr`. Numeric IDs (e.g. NID_aes_256_cbc)
100+
// appear as integer literals. For these, we must use `asExpr` to get the "value" node.
98101
[result.asIndirectExpr(), result.asExpr()] = this
99102
}
100103

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ abstract class HashAlgorithmValueConsumer extends OpenSslAlgorithmValueConsumer
99
/**
1010
* An EVP_Q_Digest directly consumes algorithm constant values
1111
*/
12-
class Evp_Q_Digest_Algorithm_Consumer extends HashAlgorithmValueConsumer {
13-
Evp_Q_Digest_Algorithm_Consumer() { this.(Call).getTarget().getName() = "EVP_Q_digest" }
12+
class Evp_Q_Digest_Algorithm_Consumer extends HashAlgorithmValueConsumer instanceof Call {
13+
Evp_Q_Digest_Algorithm_Consumer() { super.getTarget().getName() = "EVP_Q_digest" }
1414

1515
override Crypto::ConsumerInputDataFlowNode getInputNode() {
16-
result.asIndirectExpr() = this.(Call).getArgument(1)
16+
result.asIndirectExpr() = super.getArgument(1)
1717
}
1818

1919
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {

java/ql/lib/experimental/quantum/JCA.qll

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -651,27 +651,19 @@ module JCAModel {
651651

652652
class IvParameterSpecInstance extends NonceParameterInstantiation {
653653
IvParameterSpecInstance() {
654-
this.(ClassInstanceExpr)
655-
.getConstructedType()
656-
.hasQualifiedName("javax.crypto.spec", "IvParameterSpec")
654+
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "IvParameterSpec")
657655
}
658656

659-
override DataFlow::Node getInputNode() {
660-
result.asExpr() = this.(ClassInstanceExpr).getArgument(0)
661-
}
657+
override DataFlow::Node getInputNode() { result.asExpr() = super.getArgument(0) }
662658
}
663659

664660
// TODO: this also specifies the tag length for GCM
665661
class GCMParameterSpecInstance extends NonceParameterInstantiation {
666662
GCMParameterSpecInstance() {
667-
this.(ClassInstanceExpr)
668-
.getConstructedType()
669-
.hasQualifiedName("javax.crypto.spec", "GCMParameterSpec")
663+
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "GCMParameterSpec")
670664
}
671665

672-
override DataFlow::Node getInputNode() {
673-
result.asExpr() = this.(ClassInstanceExpr).getArgument(1)
674-
}
666+
override DataFlow::Node getInputNode() { result.asExpr() = super.getArgument(1) }
675667
}
676668

677669
class IvParameterSpecGetIvCall extends MethodCall {
@@ -811,14 +803,14 @@ module JCAModel {
811803
HashAlgorithmValueConsumer consumer;
812804

813805
KnownHashAlgorithm() {
814-
hash_names(this.getValue()) and
806+
hash_names(super.getValue()) and
815807
KnownHashAlgorithmLiteralToMessageDigestFlow::flow(DataFlow::exprNode(this),
816808
consumer.getInputNode())
817809
}
818810

819811
HashAlgorithmValueConsumer getConsumer() { result = consumer }
820812

821-
override string getRawHashAlgorithmName() { result = this.(StringLiteral).getValue() }
813+
override string getRawHashAlgorithmName() { result = super.getValue() }
822814

823815
override Crypto::THashType getHashFamily() {
824816
result = hash_name_to_type_known(this.getRawHashAlgorithmName(), _)
@@ -917,9 +909,7 @@ module JCAModel {
917909

918910
class DHGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
919911
DHGenParameterSpecInstance() {
920-
this.(ClassInstanceExpr)
921-
.getConstructedType()
922-
.hasQualifiedName("javax.crypto.spec", "DHGenParameterSpec")
912+
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "DHGenParameterSpec")
923913
}
924914

925915
Expr getPrimeSizeArg() { result = this.getArgument(0) }
@@ -929,9 +919,7 @@ module JCAModel {
929919

930920
class DSAParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
931921
DSAParameterSpecInstance() {
932-
this.(ClassInstanceExpr)
933-
.getConstructedType()
934-
.hasQualifiedName("java.security.spec", "DSAParameterSpec")
922+
super.getConstructedType().hasQualifiedName("java.security.spec", "DSAParameterSpec")
935923
}
936924

937925
Expr getPArg() { result = this.getArgument(0) }
@@ -943,9 +931,7 @@ module JCAModel {
943931

944932
class ECGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
945933
ECGenParameterSpecInstance() {
946-
this.(ClassInstanceExpr)
947-
.getConstructedType()
948-
.hasQualifiedName("java.security.spec", "ECGenParameterSpec")
934+
super.getConstructedType().hasQualifiedName("java.security.spec", "ECGenParameterSpec")
949935
}
950936

951937
Expr getCurveNameArg() { result = this.getArgument(0) }
@@ -955,9 +941,7 @@ module JCAModel {
955941

956942
class RSAGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
957943
RSAGenParameterSpecInstance() {
958-
this.(ClassInstanceExpr)
959-
.getConstructedType()
960-
.hasQualifiedName("java.security.spec", "RSAGenParameterSpec")
944+
super.getConstructedType().hasQualifiedName("java.security.spec", "RSAGenParameterSpec")
961945
}
962946

963947
Expr getKeySizeArg() { result = this.getArgument(0) }
@@ -981,9 +965,7 @@ module JCAModel {
981965

982966
class ECGenParameterSpecClassInstanceExpr extends KeyGeneratorParameterSpecClassInstanceExpr {
983967
ECGenParameterSpecClassInstanceExpr() {
984-
this.(ClassInstanceExpr)
985-
.getConstructedType()
986-
.hasQualifiedName("java.security.spec", "ECGenParameterSpec")
968+
super.getConstructedType().hasQualifiedName("java.security.spec", "ECGenParameterSpec")
987969
}
988970

989971
Expr getAlgorithmArg() { result = this.getArgument(0) }

0 commit comments

Comments
 (0)