|
| 1 | +import cpp |
| 2 | +import experimental.Quantum.Language |
| 3 | +import KnownAlgorithmConstants |
| 4 | +import experimental.Quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers |
| 5 | +import AlgToAVCFlow |
| 6 | + |
| 7 | +predicate knownOpenSSLConstantToHashFamilyType( |
| 8 | + KnownOpenSSLHashAlgorithmConstant e, Crypto::THashType type |
| 9 | +) { |
| 10 | + exists(string name | |
| 11 | + name = e.getNormalizedName() and |
| 12 | + ( |
| 13 | + name.matches("BLAKE2B") and type instanceof Crypto::BLAKE2B |
| 14 | + or |
| 15 | + name.matches("BLAKE2S") and type instanceof Crypto::BLAKE2S |
| 16 | + or |
| 17 | + name.matches("GOST%") and type instanceof Crypto::GOSTHash |
| 18 | + or |
| 19 | + name.matches("MD2") and type instanceof Crypto::MD2 |
| 20 | + or |
| 21 | + name.matches("MD4") and type instanceof Crypto::MD4 |
| 22 | + or |
| 23 | + name.matches("MD5") and type instanceof Crypto::MD5 |
| 24 | + or |
| 25 | + name.matches("MDC2") and type instanceof Crypto::MDC2 |
| 26 | + or |
| 27 | + name.matches("POLY1305") and type instanceof Crypto::POLY1305 |
| 28 | + or |
| 29 | + name.matches(["SHA", "SHA1"]) and type instanceof Crypto::SHA1 |
| 30 | + or |
| 31 | + name.matches("SHA+%") and not name.matches(["SHA1", "SHA3-"]) and type instanceof Crypto::SHA2 |
| 32 | + or |
| 33 | + name.matches("SHA3-%") and type instanceof Crypto::SHA3 |
| 34 | + or |
| 35 | + name.matches(["SHAKE"]) and type instanceof Crypto::SHAKE |
| 36 | + or |
| 37 | + name.matches("SM3") and type instanceof Crypto::SM3 |
| 38 | + or |
| 39 | + name.matches("RIPEMD160") and type instanceof Crypto::RIPEMD160 |
| 40 | + or |
| 41 | + name.matches("WHIRLPOOL") and type instanceof Crypto::WHIRLPOOL |
| 42 | + ) |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance, |
| 47 | + Crypto::HashAlgorithmInstance instanceof KnownOpenSSLHashAlgorithmConstant |
| 48 | +{ |
| 49 | + OpenSSLAlgorithmValueConsumer getterCall; |
| 50 | + |
| 51 | + KnownOpenSSLHashConstantAlgorithmInstance() { |
| 52 | + // Two possibilities: |
| 53 | + // 1) The source is a literal and flows to a getter, then we know we have an instance |
| 54 | + // 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that |
| 55 | + // Possibility 1: |
| 56 | + this instanceof Literal and |
| 57 | + exists(DataFlow::Node src, DataFlow::Node sink | |
| 58 | + // Sink is an argument to a CipherGetterCall |
| 59 | + sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and |
| 60 | + // Source is `this` |
| 61 | + src.asExpr() = this and |
| 62 | + // This traces to a getter |
| 63 | + KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink) |
| 64 | + ) |
| 65 | + or |
| 66 | + // Possibility 2: |
| 67 | + this instanceof DirectAlgorithmValueConsumer and getterCall = this |
| 68 | + } |
| 69 | + |
| 70 | + override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall } |
| 71 | + |
| 72 | + override Crypto::THashType getHashFamily() { |
| 73 | + knownOpenSSLConstantToHashFamilyType(this, result) |
| 74 | + or |
| 75 | + not knownOpenSSLConstantToHashFamilyType(this, _) and result = Crypto::OtherHashType() |
| 76 | + } |
| 77 | + |
| 78 | + override string getRawHashAlgorithmName() { result = this.(Literal).getValue().toString() } |
| 79 | + |
| 80 | + override int getFixedDigestLength() { |
| 81 | + this.(KnownOpenSSLHashAlgorithmConstant).getExplicitDigestLength() = result |
| 82 | + } |
| 83 | +} |
0 commit comments