|
| 1 | +import cpp |
| 2 | +private import experimental.quantum.Language |
| 3 | +private import KnownAlgorithmConstants |
| 4 | +private import Crypto::KeyOpAlg as KeyOpAlg |
| 5 | +private import OpenSSLAlgorithmInstanceBase |
| 6 | +private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumerBase |
| 7 | +private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.DirectAlgorithmValueConsumer |
| 8 | +private import AlgToAVCFlow |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * Gets the signature algorithm type based on the normalized algorithm name. |
| 13 | + */ |
| 14 | +private predicate knownOpenSSLConstantToSignatureFamilyType( |
| 15 | + KnownOpenSSLSignatureAlgorithmConstant e, Crypto::KeyOpAlg::TAlgorithm type |
| 16 | +) { |
| 17 | + exists(string name | |
| 18 | + name = e.getNormalizedName() and |
| 19 | + ( |
| 20 | + name.matches("rsa%") and type = KeyOpAlg::TAsymmetricCipher(KeyOpAlg::RSA()) |
| 21 | + or |
| 22 | + name.matches("dsa%") and type = KeyOpAlg::TSignature(KeyOpAlg::DSA()) |
| 23 | + or |
| 24 | + name.matches("ecdsa%") and type = KeyOpAlg::TSignature(KeyOpAlg::ECDSA()) |
| 25 | + or |
| 26 | + name.matches("ed25519%") and type = KeyOpAlg::TSignature(KeyOpAlg::Ed25519()) |
| 27 | + or |
| 28 | + name.matches("ed448%") and type = KeyOpAlg::TSignature(KeyOpAlg::Ed448()) |
| 29 | + // or |
| 30 | + // name.matches("sm2%") and type = KeyOpAlg::TSignature(KeyOpAlg::SM2()) |
| 31 | + // or |
| 32 | + // name.matches("ml-dsa%") and type = KeyOpAlg::TSignature(KeyOpAlg::MLDSA()) |
| 33 | + // or |
| 34 | + // name.matches("slh-dsa%") and type = KeyOpAlg::TSignature(KeyOpAlg::SLHDSA()) |
| 35 | + ) |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A signature algorithm instance derived from an OpenSSL constant. |
| 41 | + */ |
| 42 | +class KnownOpenSSLSignatureConstantAlgorithmInstance extends OpenSSLAlgorithmInstance, |
| 43 | + Crypto::KeyOperationAlgorithmInstance instanceof KnownOpenSSLSignatureAlgorithmConstant |
| 44 | +{ |
| 45 | + OpenSSLAlgorithmValueConsumer getterCall; |
| 46 | + |
| 47 | + KnownOpenSSLSignatureConstantAlgorithmInstance() { |
| 48 | + // Two possibilities: |
| 49 | + // 1) The source is a literal and flows to a getter, then we know we have an instance |
| 50 | + // 2) The source is a KnownOpenSSLAlgorithm call, and we know we have an instance immediately from that |
| 51 | + |
| 52 | + // Possibility 1: |
| 53 | + this instanceof Literal and |
| 54 | + exists(DataFlow::Node src, DataFlow::Node sink | |
| 55 | + // Sink is an argument to a signature getter call |
| 56 | + sink = getterCall.getInputNode() and |
| 57 | + // Source is `this` |
| 58 | + src.asExpr() = this and |
| 59 | + // This traces to a getter |
| 60 | + KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink) |
| 61 | + ) |
| 62 | + or |
| 63 | + // Possibility 2: |
| 64 | + this instanceof DirectAlgorithmValueConsumer and getterCall = this |
| 65 | + } |
| 66 | + |
| 67 | + override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() { |
| 68 | + none() |
| 69 | + } |
| 70 | + |
| 71 | + override Crypto::PaddingAlgorithmInstance getPaddingAlgorithm() { |
| 72 | + none() |
| 73 | + } |
| 74 | + |
| 75 | + override string getRawAlgorithmName() { result = this.(Literal).getValue().toString() } |
| 76 | + |
| 77 | + override int getKeySizeFixed() { |
| 78 | + // this.(KnownOpenSSLSignatureAlgorithmConstant).getExplicitKeySize() = result |
| 79 | + none() |
| 80 | + } |
| 81 | + |
| 82 | + override KeyOpAlg::Algorithm getAlgorithmType() { |
| 83 | + knownOpenSSLConstantToSignatureFamilyType(this, result) |
| 84 | + or |
| 85 | + not knownOpenSSLConstantToSignatureFamilyType(this, _) and |
| 86 | + result = KeyOpAlg::TSignature(KeyOpAlg::OtherSignatureAlgorithmType()) |
| 87 | + } |
| 88 | + |
| 89 | + override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall } |
| 90 | + |
| 91 | + override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { |
| 92 | + // TODO: trace to any key size initializer, symmetric and asymmetric |
| 93 | + none() |
| 94 | + } |
| 95 | + |
| 96 | + override predicate shouldHaveModeOfOperation() { |
| 97 | + none() |
| 98 | + } |
| 99 | + |
| 100 | + override predicate shouldHavePaddingScheme() { |
| 101 | + none() |
| 102 | + } |
| 103 | +} |
0 commit comments