rspec/rules/S3329/java/rule.adoc

57 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
include::../description.adoc[]
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:39 +02:00
----
public void encrypt(String key, String plainText) throws GeneralSecurityException {
byte[] bytesIV = "7cVgr5cbdCZVw5WY".getBytes(StandardCharsets.UTF_8); // secondary
2020-06-30 12:48:39 +02:00
GCMParameterSpec iv = new GCMParameterSpec(128,bytesIV); // secondary
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
2020-06-30 12:48:39 +02:00
2022-12-02 14:53:09 +01:00
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); // Noncompliant
}
2020-06-30 12:48:39 +02:00
----
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:39 +02:00
----
public void encrypt(String key, String plainText) throws GeneralSecurityException {
2022-12-02 14:53:09 +01:00
SecureRandom random = new SecureRandom();
2020-06-30 12:48:39 +02:00
byte[] bytesIV = new byte[16];
2022-12-02 14:53:09 +01:00
random.nextBytes(bytesIV); // Random initialization vector
2020-06-30 12:48:39 +02:00
GCMParameterSpec iv = new GCMParameterSpec(128, bytesIV);
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
2020-06-30 12:48:39 +02:00
2022-12-02 14:53:09 +01:00
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
}
2020-06-30 12:48:39 +02:00
----
2022-12-02 14:53:09 +01:00
include::../see.adoc[]
2020-06-30 12:48:39 +02:00
* Derived from FindSecBugs rule https://find-sec-bugs.github.io/bugs.htm#STATIC_IV[STATIC_IV]
2022-12-02 14:53:09 +01:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]