|
| 1 | +import cpp |
| 2 | +import experimental.Quantum.Language |
| 3 | +import EVPCipherConsumers |
| 4 | +import OpenSSLAlgorithmGetter |
| 5 | + |
| 6 | +predicate literalToCipherFamilyType(Literal e, Crypto::TCipherType type) { |
| 7 | + exists(string name, string algType | algType.toLowerCase().matches("%encryption") | |
| 8 | + resolveAlgorithmFromLiteral(e, name, algType) and |
| 9 | + ( |
| 10 | + name.matches("AES%") and type instanceof Crypto::AES |
| 11 | + or |
| 12 | + name.matches("ARIA") and type instanceof Crypto::ARIA |
| 13 | + or |
| 14 | + name.matches("BLOWFISH") and type instanceof Crypto::BLOWFISH |
| 15 | + or |
| 16 | + name.matches("BF") and type instanceof Crypto::BLOWFISH |
| 17 | + or |
| 18 | + name.matches("CAMELLIA%") and type instanceof Crypto::CAMELLIA |
| 19 | + or |
| 20 | + name.matches("CHACHA20") and type instanceof Crypto::CHACHA20 |
| 21 | + or |
| 22 | + name.matches("CAST5") and type instanceof Crypto::CAST5 |
| 23 | + or |
| 24 | + name.matches("2DES") and type instanceof Crypto::DOUBLEDES |
| 25 | + or |
| 26 | + name.matches(["3DES", "TRIPLEDES"]) and type instanceof Crypto::TRIPLEDES |
| 27 | + or |
| 28 | + name.matches("DES") and type instanceof Crypto::DES |
| 29 | + or |
| 30 | + name.matches("DESX") and type instanceof Crypto::DESX |
| 31 | + or |
| 32 | + name.matches("GOST%") and type instanceof Crypto::GOST |
| 33 | + or |
| 34 | + name.matches("IDEA") and type instanceof Crypto::IDEA |
| 35 | + or |
| 36 | + name.matches("KUZNYECHIK") and type instanceof Crypto::KUZNYECHIK |
| 37 | + or |
| 38 | + name.matches("MAGMA") and type instanceof Crypto::MAGMA |
| 39 | + or |
| 40 | + name.matches("RC2") and type instanceof Crypto::RC2 |
| 41 | + or |
| 42 | + name.matches("RC4") and type instanceof Crypto::RC4 |
| 43 | + or |
| 44 | + name.matches("RC5") and type instanceof Crypto::RC5 |
| 45 | + or |
| 46 | + name.matches("RSA") and type instanceof Crypto::RSA |
| 47 | + or |
| 48 | + name.matches("SEED") and type instanceof Crypto::SEED |
| 49 | + or |
| 50 | + name.matches("SM4") and type instanceof Crypto::SM4 |
| 51 | + ) |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +class CipherKnownAlgorithmLiteralAlgorithmInstance extends Crypto::CipherAlgorithmInstance instanceof Literal |
| 56 | +{ |
| 57 | + CipherKnownAlgorithmLiteralAlgorithmInstance() { |
| 58 | + exists(EVPCipherGetterCall c, DataFlow::Node src, DataFlow::Node sink | |
| 59 | + sink = c.getValueArgNode() and |
| 60 | + src.asExpr() = this and |
| 61 | + KnownAlgorithmLiteralToAlgorithmGetterFlow::flow(src, sink) and |
| 62 | + // Not just any known value, but specifically a known cipher operation |
| 63 | + exists(string algType | |
| 64 | + resolveAlgorithmFromLiteral(src.asExpr(), _, algType) and |
| 65 | + algType.toLowerCase().matches("%encryption") |
| 66 | + ) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + Crypto::AlgorithmConsumer getConsumer() { none() } //result = consumer } |
| 71 | + |
| 72 | + override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() { |
| 73 | + none() // TODO: provider defaults |
| 74 | + } |
| 75 | + |
| 76 | + override Crypto::PaddingAlgorithmInstance getPaddingAlgorithm() { none() } |
| 77 | + |
| 78 | + override string getRawAlgorithmName() { result = this.(Literal).getValue().toString() } |
| 79 | + |
| 80 | + override Crypto::TCipherType getCipherFamily() { literalToCipherFamilyType(this, result) } |
| 81 | +} |
| 82 | +// override Crypto::TCipherType getCipherFamily() { |
| 83 | +// if this.cipherNameMappingKnown(_, super.getAlgorithmName()) |
| 84 | +// then this.cipherNameMappingKnown(result, super.getAlgorithmName()) |
| 85 | +// else result instanceof Crypto::OtherCipherType |
| 86 | +// } |
| 87 | +// bindingset[name] |
| 88 | +// private predicate cipherNameMappingKnown(Crypto::TCipherType type, string name) { |
| 89 | +// name = "AES" and |
| 90 | +// type instanceof Crypto::AES |
| 91 | +// or |
| 92 | +// name = "DES" and |
| 93 | +// type instanceof Crypto::DES |
| 94 | +// or |
| 95 | +// name = "TripleDES" and |
| 96 | +// type instanceof Crypto::TripleDES |
| 97 | +// or |
| 98 | +// name = "IDEA" and |
| 99 | +// type instanceof Crypto::IDEA |
| 100 | +// or |
| 101 | +// name = "CAST5" and |
| 102 | +// type instanceof Crypto::CAST5 |
| 103 | +// or |
| 104 | +// name = "ChaCha20" and |
| 105 | +// type instanceof Crypto::ChaCha20 |
| 106 | +// or |
| 107 | +// name = "RC4" and |
| 108 | +// type instanceof Crypto::RC4 |
| 109 | +// or |
| 110 | +// name = "RC5" and |
| 111 | +// type instanceof Crypto::RC5 |
| 112 | +// or |
| 113 | +// name = "RSA" and |
| 114 | +// type instanceof Crypto::RSA |
| 115 | +// } |
| 116 | +// private predicate modeToNameMappingKnown(Crypto::TBlockCipherModeOperationType type, string name) { |
| 117 | +// type instanceof Crypto::ECB and name = "ECB" |
| 118 | +// or |
| 119 | +// type instanceof Crypto::CBC and name = "CBC" |
| 120 | +// or |
| 121 | +// type instanceof Crypto::GCM and name = "GCM" |
| 122 | +// or |
| 123 | +// type instanceof Crypto::CTR and name = "CTR" |
| 124 | +// or |
| 125 | +// type instanceof Crypto::XTS and name = "XTS" |
| 126 | +// or |
| 127 | +// type instanceof Crypto::CCM and name = "CCM" |
| 128 | +// or |
| 129 | +// type instanceof Crypto::SIV and name = "SIV" |
| 130 | +// or |
| 131 | +// type instanceof Crypto::OCB and name = "OCB" |
| 132 | +// } |
| 133 | +// override Crypto::TBlockCipherModeOperationType getModeType() { |
| 134 | +// if this.modeToNameMappingKnown(_, super.getMode()) |
| 135 | +// then this.modeToNameMappingKnown(result, super.getMode()) |
| 136 | +// else result instanceof Crypto::OtherMode |
| 137 | +// } |
| 138 | +// override string getRawModeAlgorithmName() { result = super.getMode() } |
| 139 | +// override string getRawPaddingAlgorithmName() { result = super.getPadding() } |
| 140 | +// bindingset[name] |
| 141 | +// private predicate paddingToNameMappingKnown(Crypto::TPaddingType type, string name) { |
| 142 | +// type instanceof Crypto::NoPadding and name = "NOPADDING" |
| 143 | +// or |
| 144 | +// type instanceof Crypto::PKCS7 and name = ["PKCS5Padding", "PKCS7Padding"] // TODO: misnomer in the JCA? |
| 145 | +// or |
| 146 | +// type instanceof Crypto::OAEP and name.matches("OAEP%") // TODO: handle OAEPWith% |
| 147 | +// } |
| 148 | +// } |
0 commit comments