Skip to content

Commit 6005437

Browse files
committed
Update JCA model with flow to call as AESuse and format JCA model
1 parent 60d931a commit 6005437

File tree

1 file changed

+82
-50
lines changed
  • java/ql/lib/experimental/Quantum

1 file changed

+82
-50
lines changed

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

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,80 @@ module JCAModel {
77
abstract class EncryptionOperation extends Crypto::EncryptionOperation { }
88

99
//TODO PBEWith can have suffixes. how to do? enumerate? or match a pattern?
10-
predicate cipher_names(string algo) { algo = ["AES", "AESWrap", "AESWrapPad", "ARCFOUR", "Blowfish", "ChaCha20", "ChaCha20-Poly1305", "DES", "DESede", "DESedeWrap", "ECIES", "PBEWith", "RC2", "RC4", "RC5", "RSA"] }
11-
//TODO solve the fact that x is an int of various values. same as above... enumerate?
12-
predicate cipher_modes(string mode) {mode = ["NONE", "CBC", "CCM", "CFB", "CFBx", "CTR", "CTS", "ECB", "GCM", "KW", "KWP", "OFB", "OFBx", "PCBC"]}
13-
//todo same as above, OAEPWith has asuffix type
14-
predicate cipher_padding(string padding) {padding = ["NoPadding", "ISO10126Padding", "OAEPPadding", "OAEPWith", "PKCS1Padding", "PKCS5Padding", "SSL3Padding"]}
10+
predicate cipher_names(string algo) {
11+
algo =
12+
[
13+
"AES", "AESWrap", "AESWrapPad", "ARCFOUR", "Blowfish", "ChaCha20", "ChaCha20-Poly1305",
14+
"DES", "DESede", "DESedeWrap", "ECIES", "PBEWith", "RC2", "RC4", "RC5", "RSA"
15+
]
16+
}
1517

18+
//TODO solve the fact that x is an int of various values. same as above... enumerate?
19+
predicate cipher_modes(string mode) {
20+
mode =
21+
[
22+
"NONE", "CBC", "CCM", "CFB", "CFBx", "CTR", "CTS", "ECB", "GCM", "KW", "KWP", "OFB", "OFBx",
23+
"PCBC"
24+
]
25+
}
1626

17-
////cipher specifics ----------------------------------------
27+
//todo same as above, OAEPWith has asuffix type
28+
predicate cipher_padding(string padding) {
29+
padding =
30+
[
31+
"NoPadding", "ISO10126Padding", "OAEPPadding", "OAEPWith", "PKCS1Padding", "PKCS5Padding",
32+
"SSL3Padding"
33+
]
34+
}
1835

19-
class CipherInstance extends Call {
20-
CipherInstance() { this.getCallee().hasQualifiedName("javax.crypto","Cipher", "getInstance") }
36+
////cipher specifics ----------------------------------------
37+
class CipherInstance extends Call {
38+
CipherInstance() { this.getCallee().hasQualifiedName("javax.crypto", "Cipher", "getInstance") }
2139

2240
Expr getAlgorithmArg() { result = this.getArgument(0) }
2341
}
2442

2543
/**
2644
* this may be specified either in the ALG/MODE/PADDING or just ALG format
2745
*/
28-
class CipherAlgorithmStringLiteral extends StringLiteral {
29-
CipherAlgorithmStringLiteral() { cipher_names(this.getValue().splitAt("/"))}
46+
class CipherAlgorithmStringLiteral extends StringLiteral {
47+
CipherAlgorithmStringLiteral() { cipher_names(this.getValue().splitAt("/")) }
3048
}
3149

32-
3350
class ModeOfOperationStringLiteral extends Crypto::ModeOfOperation instanceof StringLiteral {
34-
ModeOfOperationStringLiteral() { cipher_modes(this.(StringLiteral).getValue().splitAt("/"))}
51+
ModeOfOperationStringLiteral() { cipher_modes(this.(StringLiteral).getValue().splitAt("/")) }
3552

36-
override string getRawAlgorithmName() { result = this.(StringLiteral).getValue().regexpCapture(".*/(.*)/.*",1) }
53+
override string getRawAlgorithmName() {
54+
result = this.(StringLiteral).getValue().regexpCapture(".*/(.*)/.*", 1)
55+
}
3756

38-
override string getValue() { result = this.(StringLiteral).getValue().regexpCapture(".*/(.*)/.*",1) }
57+
override string getValue() {
58+
result = this.(StringLiteral).getValue().regexpCapture(".*/(.*)/.*", 1)
59+
}
3960

61+
predicate modeToNameMapping(Crypto::TModeOperation type, string name) {
62+
name = "ECB" and type instanceof Crypto::ECB
63+
}
4064

41-
predicate modeToNameMapping(Crypto::TModeOperation type, string name) {
42-
name = "ECB" and type instanceof Crypto::ECB
43-
}
44-
45-
override Crypto::TModeOperation getModeType(){
65+
override Crypto::TModeOperation getModeType() {
4666
modeToNameMapping(result, this.getRawAlgorithmName())
4767
}
4868
}
4969

5070
abstract class CipherAlgorithmPadding extends Crypto::NodeBase {
51-
string getValue() {result = ""}
71+
string getValue() { result = "" }
5272
}
5373

5474
class CipherAlgorithmPaddingStringLiteral extends CipherAlgorithmPadding instanceof StringLiteral {
55-
CipherAlgorithmPaddingStringLiteral() { cipher_padding(this.(StringLiteral).getValue().splitAt("/"))}
75+
CipherAlgorithmPaddingStringLiteral() {
76+
cipher_padding(this.(StringLiteral).getValue().splitAt("/"))
77+
}
5678

5779
override string toString() { result = this.(StringLiteral).toString() }
5880

59-
override string getValue() { result = this.(StringLiteral).getValue().regexpCapture(".*/.*/(.*)",1) }
81+
override string getValue() {
82+
result = this.(StringLiteral).getValue().regexpCapture(".*/.*/(.*)", 1)
83+
}
6084
}
6185

6286
private module AlgorithmStringToFetchConfig implements DataFlow::ConfigSig {
@@ -69,51 +93,59 @@ class CipherAlgorithmStringLiteral extends StringLiteral {
6993

7094
module AlgorithmStringToFetchFlow = DataFlow::Global<AlgorithmStringToFetchConfig>;
7195

72-
predicate algorithmStringToCipherInstanceArgFlow(string name, CipherAlgorithmStringLiteral origin, Expr arg) {
96+
predicate algorithmStringToCipherInstanceArgFlow(
97+
string name, CipherAlgorithmStringLiteral origin, Expr arg
98+
) {
7399
exists(CipherInstance sinkCall |
74100
origin.getValue().splitAt("/") = name and
75-
arg = sinkCall.getAlgorithmArg() and
76-
AlgorithmStringToFetchFlow::flow(DataFlow::exprNode(origin), DataFlow::exprNode(arg))
101+
arg = sinkCall and
102+
AlgorithmStringToFetchFlow::flow(DataFlow::exprNode(origin),
103+
DataFlow::exprNode(sinkCall.getAlgorithmArg()))
77104
)
78105
}
79106

80-
81-
predicate modeStringToCipherInstanceArgFlow(string name, ModeOfOperationStringLiteral mode, Expr arg) {
107+
predicate modeStringToCipherInstanceArgFlow(
108+
string name, ModeOfOperationStringLiteral mode, Expr arg
109+
) {
82110
exists(CipherInstance sinkCall |
83111
mode.getRawAlgorithmName() = name and
84-
arg = sinkCall.getAlgorithmArg() and
85-
AlgorithmStringToFetchFlow::flow(DataFlow::exprNode(mode), DataFlow::exprNode(arg))
112+
arg = sinkCall and
113+
AlgorithmStringToFetchFlow::flow(DataFlow::exprNode(mode),
114+
DataFlow::exprNode(sinkCall.getAlgorithmArg()))
86115
)
87116
}
88117

89118
/**
90-
* A class to represent when AES is used AND it has literal mode and padding provided
91-
* this does not capture the use without
119+
* A class to represent when AES is used
120+
* AND currently it has literal mode and padding provided
121+
*
122+
* this currently does not capture the use without a literal
123+
* though should be extended to
92124
*/
93-
// class AESLiteral extends Crypto::SymmetricAlgorithm instanceof Expr {
94-
// CipherAlgorithmStringLiteral alg;
95-
// AESLiteral() { algorithmStringToCipherInstanceArgFlow("AES", alg, this)
96-
// }
125+
class AESAlgo extends Crypto::SymmetricAlgorithm instanceof Expr {
126+
CipherAlgorithmStringLiteral alg;
97127

98-
// override Crypto::ModeOfOperation getModeOfOperation(){ modeStringToCipherInstanceArgFlow(result.getAlgorithmName(), result, this)}
128+
AESAlgo() { algorithmStringToCipherInstanceArgFlow("AES", alg, this) }
99129

100-
// override Crypto::LocatableElement getOrigin(string name) {
101-
// result = alg and name = alg.toString()
102-
// }
130+
override Crypto::ModeOfOperation getModeOfOperation() {
131+
modeStringToCipherInstanceArgFlow(result.getAlgorithmName(), result, this)
132+
}
103133

104-
// override string getAlgorithmName(){ result = "AES" }
134+
override Crypto::LocatableElement getOrigin(string name) {
135+
result = alg and name = alg.toString()
136+
}
105137

106-
// override string getRawAlgorithmName(){ result = alg.getValue()}
138+
override string getAlgorithmName() { result = "AES" }
107139

108-
// override Crypto::TSymmetricCipherFamilyType getSymmetricCipherFamilyType() { result instanceof Crypto::AES}
140+
override string getRawAlgorithmName() { result = alg.getValue() }
109141

110-
// //temp hacks for testing
111-
// override string getKeySize(Location location){
112-
// result = ""
113-
// }
142+
override Crypto::TSymmetricCipherFamilyType getSymmetricCipherFamilyType() {
143+
result instanceof Crypto::AES
144+
}
114145

115-
// override Crypto::TCipherStructure getCipherType(){ none()}
116-
// }
146+
//temp hacks for testing
147+
override string getKeySize(Location location) { result = "" }
117148

118-
119-
}
149+
override Crypto::TCipherStructure getCipherType() { none() }
150+
}
151+
}

0 commit comments

Comments
 (0)