Skip to content

Commit 86e51da

Browse files
committed
Improve JCA aes alg model, add test
1 parent 5f355c7 commit 86e51da

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

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

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ predicate cipher_modes(string mode) {mode = ["NONE", "CBC", "CCM", "CFB", "CFBx"
1313
//todo same as above, OAEPWith has asuffix type
1414
predicate cipher_padding(string padding) {padding = ["NoPadding", "ISO10126Padding", "OAEPPadding", "OAEPWith", "PKCS1Padding", "PKCS5Padding", "SSL3Padding"]}
1515

16+
17+
abstract class BlockCiper extends Crypto::Algorithm {
18+
CipherAlgorithmStringLiteral alg;
19+
CipherAlgorithmMode mode;
20+
CipherAlgorithmPadding padding;
21+
22+
23+
CipherAlgorithmStringLiteral getAlg() {result = alg }
24+
CipherAlgorithmMode getMode() {result = mode }
25+
26+
CipherAlgorithmPadding getPadding() {result =padding}
27+
}
1628
/**
1729
* Symmetric algorithms
1830
*/
@@ -45,6 +57,9 @@ class CipherInstance extends Call {
4557
Expr getAlgorithmArg() { result = this.getArgument(0) }
4658
}
4759

60+
/**
61+
* this may be specified either in the ALG/MODE/PADDING or just ALG format
62+
*/
4863
class CipherAlgorithmStringLiteral extends Crypto::NodeBase instanceof StringLiteral {
4964
CipherAlgorithmStringLiteral() { cipher_names(this.getValue().splitAt("/"))}
5065

@@ -53,20 +68,28 @@ class CipherAlgorithmStringLiteral extends Crypto::NodeBase instanceof StringLit
5368
string getValue() { result = this.(StringLiteral).getValue() }
5469
}
5570

56-
class CipherAlgorithmModeStringLiteral extends Crypto::NodeBase instanceof StringLiteral {
57-
CipherAlgorithmModeStringLiteral() { cipher_modes(this.getValue().splitAt("/"))}
71+
abstract class CipherAlgorithmMode extends Crypto::NodeBase {
72+
string getValue() {result = ""}
73+
}
74+
75+
class CipherAlgorithmModeStringLiteral extends CipherAlgorithmMode instanceof StringLiteral {
76+
CipherAlgorithmModeStringLiteral() { cipher_modes(this.(StringLiteral).getValue().splitAt("/"))}
5877

5978
override string toString() { result = this.(StringLiteral).toString() }
6079

61-
string getValue() { result = this.(StringLiteral).getValue() }
80+
override string getValue() { result = this.(StringLiteral).getValue().regexpCapture(".*/(.*)/.*",1) }
6281
}
6382

64-
class CipherAlgorithmPaddingStringLiteral extends Crypto::NodeBase instanceof StringLiteral {
65-
CipherAlgorithmPaddingStringLiteral() { cipher_padding(this.getValue().splitAt("/"))}
83+
abstract class CipherAlgorithmPadding extends Crypto::NodeBase {
84+
string getValue() {result = ""}
85+
}
86+
87+
class CipherAlgorithmPaddingStringLiteral extends CipherAlgorithmPadding instanceof StringLiteral {
88+
CipherAlgorithmPaddingStringLiteral() { cipher_padding(this.(StringLiteral).getValue().splitAt("/"))}
6689

6790
override string toString() { result = this.(StringLiteral).toString() }
6891

69-
string getValue() { result = this.(StringLiteral).getValue() }
92+
override string getValue() { result = this.(StringLiteral).getValue().regexpCapture(".*/.*/(.*)",1) }
7093
}
7194

7295
private module AlgorithmStringToFetchConfig implements DataFlow::ConfigSig {
@@ -79,27 +102,32 @@ class CipherAlgorithmStringLiteral extends Crypto::NodeBase instanceof StringLit
79102

80103
module AlgorithmStringToFetchFlow = DataFlow::Global<AlgorithmStringToFetchConfig>;
81104

82-
predicate algorithmStringToCipherInstanceArgFlow(string name, CipherAlgorithmStringLiteral origin, Expr arg) {
105+
predicate algorithmStringToCipherInstanceArgFlow(string name, CipherAlgorithmStringLiteral origin, CipherAlgorithmModeStringLiteral mode, CipherAlgorithmPaddingStringLiteral padding, Expr arg) {
83106
exists(CipherInstance sinkCall |
84-
origin.getValue().toUpperCase() = name and
107+
origin.getValue().splitAt("/") = name and
108+
origin = mode and
109+
origin = padding and
85110
arg = sinkCall.getAlgorithmArg() and
86111
AlgorithmStringToFetchFlow::flow(DataFlow::exprNode(origin), DataFlow::exprNode(arg))
87112
)
88113
}
89114

90-
class AES extends SymmetricAlgorithm instanceof Expr {
91-
CipherAlgorithmStringLiteral origin;
115+
/**
116+
* A class to represent when AES is used AND it has literal mode and padding provided
117+
* this does not capture the use without
118+
*/
119+
class AESLiteral extends SymmetricAlgorithm, BlockCiper instanceof Expr {
92120

93-
AES() { algorithmStringToCipherInstanceArgFlow("AES", origin, this) }
121+
122+
AESLiteral() { algorithmStringToCipherInstanceArgFlow("AES", alg, mode, padding, this)
123+
}
94124

95125
override Crypto::LocatableElement getOrigin(string name) {
96-
result = origin and name = origin.toString()
126+
result = alg and name = alg.toString()
97127
}
98128

99-
override string getAlgorithmName(){ result = "AES"}
129+
override string getAlgorithmName(){ result = this.getAlgorithmName()}
100130
}
101131

102132

103-
104-
105133
}

java/ql/src/experimental/Quantum/Test.ql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
*/
44

55
import experimental.Quantum.Language
6-
//import java
76

8-
from Crypto::NodeBase node
9-
select node
10-
11-
// from Class t
12-
// where t.hasQualifiedName("javax.crypto", "CipherSpi")
13-
// select t, t.getADescendant*()
7+
from JCAModel::AESLiteral l
8+
select l, l.getAlg(), l.getMode().getValue(), l.getPadding().getValue()

0 commit comments

Comments
 (0)