Crypto: Refactor and change casts to super

This commit is contained in:
Nicolas Will
2025-10-02 18:43:38 +02:00
parent 9673b81677
commit 4901cdf929
3 changed files with 18 additions and 33 deletions

View File

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

View File

@@ -9,11 +9,11 @@ abstract class HashAlgorithmValueConsumer extends OpenSslAlgorithmValueConsumer
/**
* An EVP_Q_Digest directly consumes algorithm constant values
*/
class Evp_Q_Digest_Algorithm_Consumer extends HashAlgorithmValueConsumer {
Evp_Q_Digest_Algorithm_Consumer() { this.(Call).getTarget().getName() = "EVP_Q_digest" }
class Evp_Q_Digest_Algorithm_Consumer extends HashAlgorithmValueConsumer instanceof Call {
Evp_Q_Digest_Algorithm_Consumer() { super.getTarget().getName() = "EVP_Q_digest" }
override Crypto::ConsumerInputDataFlowNode getInputNode() {
result.asIndirectExpr() = this.(Call).getArgument(1)
result.asIndirectExpr() = super.getArgument(1)
}
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {

View File

@@ -651,27 +651,19 @@ module JCAModel {
class IvParameterSpecInstance extends NonceParameterInstantiation {
IvParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("javax.crypto.spec", "IvParameterSpec")
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "IvParameterSpec")
}
override DataFlow::Node getInputNode() {
result.asExpr() = this.(ClassInstanceExpr).getArgument(0)
}
override DataFlow::Node getInputNode() { result.asExpr() = super.getArgument(0) }
}
// TODO: this also specifies the tag length for GCM
class GCMParameterSpecInstance extends NonceParameterInstantiation {
GCMParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("javax.crypto.spec", "GCMParameterSpec")
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "GCMParameterSpec")
}
override DataFlow::Node getInputNode() {
result.asExpr() = this.(ClassInstanceExpr).getArgument(1)
}
override DataFlow::Node getInputNode() { result.asExpr() = super.getArgument(1) }
}
class IvParameterSpecGetIvCall extends MethodCall {
@@ -811,14 +803,14 @@ module JCAModel {
HashAlgorithmValueConsumer consumer;
KnownHashAlgorithm() {
hash_names(this.getValue()) and
hash_names(super.getValue()) and
KnownHashAlgorithmLiteralToMessageDigestFlow::flow(DataFlow::exprNode(this),
consumer.getInputNode())
}
HashAlgorithmValueConsumer getConsumer() { result = consumer }
override string getRawHashAlgorithmName() { result = this.(StringLiteral).getValue() }
override string getRawHashAlgorithmName() { result = super.getValue() }
override Crypto::THashType getHashFamily() {
result = hash_name_to_type_known(this.getRawHashAlgorithmName(), _)
@@ -917,9 +909,7 @@ module JCAModel {
class DHGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
DHGenParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("javax.crypto.spec", "DHGenParameterSpec")
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "DHGenParameterSpec")
}
Expr getPrimeSizeArg() { result = this.getArgument(0) }
@@ -929,9 +919,7 @@ module JCAModel {
class DSAParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
DSAParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("java.security.spec", "DSAParameterSpec")
super.getConstructedType().hasQualifiedName("java.security.spec", "DSAParameterSpec")
}
Expr getPArg() { result = this.getArgument(0) }
@@ -943,9 +931,7 @@ module JCAModel {
class ECGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
ECGenParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("java.security.spec", "ECGenParameterSpec")
super.getConstructedType().hasQualifiedName("java.security.spec", "ECGenParameterSpec")
}
Expr getCurveNameArg() { result = this.getArgument(0) }
@@ -955,9 +941,7 @@ module JCAModel {
class RSAGenParameterSpecInstance extends KeyGeneratorParameterSpecClassInstanceExpr {
RSAGenParameterSpecInstance() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("java.security.spec", "RSAGenParameterSpec")
super.getConstructedType().hasQualifiedName("java.security.spec", "RSAGenParameterSpec")
}
Expr getKeySizeArg() { result = this.getArgument(0) }
@@ -981,9 +965,7 @@ module JCAModel {
class ECGenParameterSpecClassInstanceExpr extends KeyGeneratorParameterSpecClassInstanceExpr {
ECGenParameterSpecClassInstanceExpr() {
this.(ClassInstanceExpr)
.getConstructedType()
.hasQualifiedName("java.security.spec", "ECGenParameterSpec")
super.getConstructedType().hasQualifiedName("java.security.spec", "ECGenParameterSpec")
}
Expr getAlgorithmArg() { result = this.getArgument(0) }