Skip to content

Commit 41bdd6d

Browse files
Add RSA without OEAP query and qhelp
1 parent 44e1ecd commit 41bdd6d

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** Definitions for the RSE without OAEP query */
2+
3+
import java
4+
import semmle.code.java.dataflow.DataFlow
5+
6+
/** Holds if `ma` is a call to `Cipher.getInstance` which initialises an RSA cipher without using OAEP padding. */
7+
predicate rsaWithoutOaepCall(MethodAccess ma) {
8+
ma.getMethod().hasQualifiedName("javax.crypto", "Cipher", "getInstance") and
9+
exists(CompileTimeConstantExpr specExpr, string spec |
10+
specExpr.getStringValue() = spec and
11+
DataFlow::localExprFlow(specExpr, ma.getArgument(0)) and
12+
spec.matches("RSA/%") and
13+
not spec.matches("%OAEP%")
14+
)
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// BAD: No padding scheme is used
2+
Cipher rsa = Cipher.getInstance("RSA/ECB/NoPadding")
3+
...
4+
5+
//GOOD: OAEP padding is used
6+
Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding")
7+
...
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
4+
<overview>
5+
<p>Cryptographic algorithms often use padding schemes to make the plaintext less predictable. The OAEP scheme (Optimal Asymmetric Encryption Padding) should used with RSA encryption.
6+
Using no padding or an outdated padding scheme such as PKCS1 can weaken the encryption by making it vulnerable to a padding oracle attack.
7+
</p>
8+
</overview>
9+
10+
<recommendation>
11+
<p>Use the OAEP scheme when using RSA encryption.</p>
12+
</recommendation>
13+
14+
<example>
15+
<p>In the following example, the BAD case shows no padding being used, whereas the GOOD case shows an OAEP scheme being used.</p>
16+
<sample src="RsaWithoutOaep.java" />
17+
</example>
18+
19+
<references>
20+
<li>
21+
<a href="https://github.com/MobSF/owasp-mstg/blob/master/Document/0x04g-Testing-Cryptography.md#padding-oracle-attacks-due-to-weaker-padding-or-block-operation-implementations">Mobile Security Testing Guide</a>.
22+
</li>
23+
<li>
24+
<a href="https://robertheaton.com/2013/07/29/padding-oracle-attack/">The Padding Oracle Attack</a>.
25+
</li>
26+
</references>
27+
</qhelp>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Use of RSA algorithm without OAEP
3+
* @description Using RSA encryption without OAEP padding can lead to a padding oracle attack, weakening the encryption.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @precision high
7+
* @id java/rsa-without-oaep
8+
* @tags security
9+
* external/cwe/cwe-780
10+
*/
11+
12+
import java
13+
import semmle.code.java.security.RsaWithoutOaepQuery
14+
15+
from MethodAccess ma
16+
where rsaWithoutOaepCall(ma)
17+
select ma, "This instance of RSA does not use OAEP padding."

0 commit comments

Comments
 (0)