Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Java 21 #60

Merged
merged 4 commits into from
Jan 9, 2025
Merged

Switch to Java 21 #60

merged 4 commits into from
Jan 9, 2025

Conversation

arusevm
Copy link
Contributor

@arusevm arusevm commented Jan 9, 2025

No description provided.

@arusevm arusevm merged commit 9e517f4 into main Jan 9, 2025
3 checks passed
final SecretKeySpec key,
final byte[] response)
throws GeneralSecurityException, IOException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

Check failure

Code scanning / CodeQL

Use of a broken or risky cryptographic algorithm High

Cryptographic algorithm
AES/CBC/PKCS5Padding
is weak and should not be used.

Copilot Autofix AI about 13 hours ago

To fix the problem, we should replace the use of the AES/CBC/PKCS5Padding mode with a more secure mode, such as AES/GCM/NoPadding. This mode provides authenticated encryption, ensuring both confidentiality and integrity of the data.

  1. Update the Cipher.getInstance calls to use "AES/GCM/NoPadding".
  2. Modify the encryption and decryption methods to handle the GCM-specific parameters, such as the GCM initialization vector (IV) and authentication tag.
  3. Ensure that the IV is generated securely and included in the encrypted output, and that it is extracted correctly during decryption.
Suggested changeset 1
misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java b/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
--- a/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
+++ b/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
@@ -32,11 +32,13 @@
                     throws GeneralSecurityException, IOException {
-        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-        cipher.init(Cipher.ENCRYPT_MODE, key);
+        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
+        byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes
+        SecureRandom random = new SecureRandom();
+        random.nextBytes(iv);
+        GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); // 128-bit authentication tag length
+        cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
         byte[] encrypted = cipher.doFinal(response);
-        AlgorithmParameters p = cipher.getParameters();
-        byte[] params = cipher.getParameters().getEncoded();
         ByteBuffer resp = ByteBuffer.allocate(
-                Integer.BYTES + params.length + encrypted.length);
-        resp.putInt(params.length);
-        resp.put(params);
+                Integer.BYTES + iv.length + encrypted.length);
+        resp.putInt(iv.length);
+        resp.put(iv);
         resp.put(encrypted);
@@ -63,6 +65,5 @@
 
-        AlgorithmParameters algP = AlgorithmParameters.getInstance("AES");
-        algP.init(params);
-        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-        cipher.init(Cipher.DECRYPT_MODE, key, algP);
+        GCMParameterSpec gcmSpec = new GCMParameterSpec(128, params);
+        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
+        cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
 
EOF
@@ -32,11 +32,13 @@
throws GeneralSecurityException, IOException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); // 128-bit authentication tag length
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
byte[] encrypted = cipher.doFinal(response);
AlgorithmParameters p = cipher.getParameters();
byte[] params = cipher.getParameters().getEncoded();
ByteBuffer resp = ByteBuffer.allocate(
Integer.BYTES + params.length + encrypted.length);
resp.putInt(params.length);
resp.put(params);
Integer.BYTES + iv.length + encrypted.length);
resp.putInt(iv.length);
resp.put(iv);
resp.put(encrypted);
@@ -63,6 +65,5 @@

AlgorithmParameters algP = AlgorithmParameters.getInstance("AES");
algP.init(params);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, algP);
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, params);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

AlgorithmParameters algP = AlgorithmParameters.getInstance("AES");
algP.init(params);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

Check failure

Code scanning / CodeQL

Use of a broken or risky cryptographic algorithm High

Cryptographic algorithm
AES/CBC/PKCS5Padding
is weak and should not be used.

Copilot Autofix AI about 13 hours ago

To fix the problem, we should replace the use of the AES/CBC/PKCS5Padding mode with a more secure mode, such as AES/GCM/NoPadding. This change will ensure that the encryption provides both confidentiality and integrity, reducing the risk of attacks.

  1. Replace the Cipher.getInstance("AES/CBC/PKCS5Padding") calls with Cipher.getInstance("AES/GCM/NoPadding").
  2. Update the encryption and decryption logic to handle the GCM mode, which includes managing the GCM parameters (nonce and authentication tag).
Suggested changeset 1
misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java b/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
--- a/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
+++ b/misc/openid/src/main/java/io/mishmash/stacks/oidc/sasl/DHUtils.java
@@ -32,11 +32,13 @@
                     throws GeneralSecurityException, IOException {
-        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-        cipher.init(Cipher.ENCRYPT_MODE, key);
+        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
+        byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes
+        SecureRandom random = new SecureRandom();
+        random.nextBytes(iv);
+        GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
+        cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
         byte[] encrypted = cipher.doFinal(response);
-        AlgorithmParameters p = cipher.getParameters();
-        byte[] params = cipher.getParameters().getEncoded();
         ByteBuffer resp = ByteBuffer.allocate(
-                Integer.BYTES + params.length + encrypted.length);
-        resp.putInt(params.length);
-        resp.put(params);
+                Integer.BYTES + iv.length + encrypted.length);
+        resp.putInt(iv.length);
+        resp.put(iv);
         resp.put(encrypted);
@@ -63,6 +65,7 @@
 
-        AlgorithmParameters algP = AlgorithmParameters.getInstance("AES");
-        algP.init(params);
-        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-        cipher.init(Cipher.DECRYPT_MODE, key, algP);
+        byte[] iv = new byte[paramsLen];
+        challenge.get(iv);
+        GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
+        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
+        cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
 
EOF
@@ -32,11 +32,13 @@
throws GeneralSecurityException, IOException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
byte[] encrypted = cipher.doFinal(response);
AlgorithmParameters p = cipher.getParameters();
byte[] params = cipher.getParameters().getEncoded();
ByteBuffer resp = ByteBuffer.allocate(
Integer.BYTES + params.length + encrypted.length);
resp.putInt(params.length);
resp.put(params);
Integer.BYTES + iv.length + encrypted.length);
resp.putInt(iv.length);
resp.put(iv);
resp.put(encrypted);
@@ -63,6 +65,7 @@

AlgorithmParameters algP = AlgorithmParameters.getInstance("AES");
algP.init(params);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, algP);
byte[] iv = new byte[paramsLen];
challenge.get(iv);
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants