Skip to content

Conversation

@utkrishtsahu
Copy link
Contributor

@utkrishtsahu utkrishtsahu commented May 19, 2025

RSA encryption padding change from PKCS1Padding to OAEPWithSHA-256And

Changes

Updated padding for RSA encryption from PKCS1Padding to OAEPWithSHA-256And also checked for migration for the same.

References

Testing

Please describe how this can be tested by reviewers. Be specific about anything not tested and reasons why. Since this library has unit testing, tests should be added for new functionality and existing tests should complete without errors.

  • [X ] This change adds unit test coverage

  • [X ] This change adds integration test coverage

  • [ X] This change has been tested on the latest version of the platform/language or why not

Checklist

@utkrishtsahu utkrishtsahu requested a review from a team as a code owner May 19, 2025 05:13
"("+ OLD_PKCS1_RSA_TRANSFORMATION +") for migration due to " +
"InvalidKeyException with OAEP.");
KeyStore.PrivateKeyEntry rsaKeyEntry = getRSAKeyEntry();
Cipher rsaPkcs1Cipher = Cipher.getInstance(OLD_PKCS1_RSA_TRANSFORMATION);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semgrep identified an issue in your code:
Weak cryptographic algorithm detected. The DES, 3DES, RC2, RC4, MD4, MD5, SHA1, BLOWFISH, Dual_EC_DRBG, and SHA1PRNG algorithms are considered insecure and should not be used. Using weak cryptographic algorithms can compromise the confidentiality and integrity of sensitive data. It is recommended to adopt only cryptographic features and algorithms offered by the Android platform that are internationally recognized as strong. It is also fundamental to ensure that the encryption parameters (crypto key, IV, etc.) are generated randomly using a cryptographically strong PRNG function. In addition, if it is needed to store an encryption parameter on the device, a secure storage mechanism like the Android KeyStore must be used.

To resolve this comment:

🔧 No guidance has been designated for this issue. Fix according to your organization's approved methods.

💬 Ignore this finding

Reply with Semgrep commands to ignore this finding.

  • /fp <comment> for false positive
  • /ar <comment> for acceptable risk
  • /other <comment> for all other reasons

Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by android_weak_cryptographic_algorithm.

You can view more details about this finding in the Semgrep AppSec Platform.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary digest used for the RSA-OAEP padding scheme is SHA-256, which is secure.

SHA1 is included only as a supported digest for the Mask Generation Function (MGF1), an internal component of OAEP.

This is a standard practice to ensure maximum compatibility across different Android OS versions and hardware security modules, as some implementations require SHA1 to be available for MGF1.

Log.d(TAG, "Attempting to decrypt AES key with PKCS1Padding ("+
OLD_PKCS1_RSA_TRANSFORMATION +") for migration.");
KeyStore.PrivateKeyEntry rsaKeyEntry = getRSAKeyEntry();
Cipher rsaPkcs1Cipher = Cipher.getInstance(OLD_PKCS1_RSA_TRANSFORMATION);
Copy link

@semgrepcode-auth0 semgrepcode-auth0 bot May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weak cryptographic algorithm detected. The DES, 3DES, RC2, RC4, MD4, MD5, SHA1, BLOWFISH, Dual_EC_DRBG, and SHA1PRNG algorithms are considered insecure and should not be used. Using weak cryptographic algorithms can compromise the confidentiality and integrity of sensitive data. It is recommended to adopt only cryptographic features and algorithms offered by the Android platform that are internationally recognized as strong. It is also fundamental to ensure that the encryption parameters (crypto key, IV, etc.) are generated randomly using a cryptographically strong PRNG function. In addition, if it is needed to store an encryption parameter on the device, a secure storage mechanism like the Android KeyStore must be used.

🧼 Fixed in commit 715bcc8 🧼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary digest used for the RSA-OAEP padding scheme is SHA-256, which is secure.

SHA1 is included only as a supported digest for the Mask Generation Function (MGF1), an internal component of OAEP.

This is a standard practice to ensure maximum compatibility across different Android OS versions and hardware security modules, as some implementations require SHA1 to be available for MGF1.

@pmathew92 pmathew92 requested a review from Copilot August 19, 2025 06:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the RSA encryption padding from PKCS1Padding to OAEPWithSHA-256AndMGF1Padding for improved security. The change includes backward compatibility handling to migrate existing PKCS1-encrypted AES keys to the new OAEP format.

  • Changes RSA transformation from PKCS1Padding to OAEPWithSHA-256AndMGF1Padding
  • Increases RSA key size from 2048 to 4096 bits
  • Implements migration logic for existing PKCS1-encrypted AES keys to maintain compatibility

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
CryptoUtil.java Updates RSA transformation, key size, and implements migration logic for PKCS1 to OAEP transition
CryptoUtilTest.java Refactors tests to support new RSA transformation and adds migration test coverage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

storage.store(KEY_ALIAS, newEncodedEncryptedAES);
storage.remove(OLD_KEY_ALIAS);
return decryptedAESKey;
} catch (Exception e) {
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching generic Exception is too broad and may hide specific error conditions. Consider catching specific exceptions like BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, etc., to handle different failure scenarios appropriately.

Suggested change
} catch (Exception e) {
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | KeyStoreException | UnrecoverableEntryException e) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made new changes on 17 dec will check on copilot review on it.

Log.e(TAG, "Error while creating the AES key.", e);
Log.e(TAG, "Error while creating the new AES key.", e);
throw new IncompatibleDeviceException(e);
} catch (Exception e) {
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching generic Exception is too broad. This catch block should handle specific exceptions that can occur during RSA encryption or storage operations, such as CryptoException or IncompatibleDeviceException.

Suggested change
} catch (Exception e) {
} catch (InvalidKeyException
| NoSuchPaddingException
| IllegalBlockSizeException
| BadPaddingException
| KeyStoreException
| UnrecoverableEntryException
| CertificateException
| IOException
| ProviderException e) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made new changes on 17 dec will check on copilot review on it.

// https://developer.android.com/reference/javax/crypto/Cipher.html
@SuppressWarnings("SpellCheckingInspection")
private static final String AES_TRANSFORMATION = "AES/GCM/NOPADDING";
private static final String OLD_PKCS1_RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to state why OLD_PKCS1_RSA_TRANSFORMATION is being used

.setKeySize(RSA_KEY_SIZE)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this setDigest addition was also reviewed by security ?

@pmathew92
Copy link
Contributor

@utkrishtsahu The current implementation doesn't handle migration for existing users token . Please handle that and ensure it doesn't break

@utkrishtsahu
Copy link
Contributor Author

@claude

@pmathew92 pmathew92 requested a review from Copilot December 19, 2025 08:53
* been migrated, support for this constant and any code paths that use it should be
* removed.
*/
private static final String LEGACY_PKCS1_RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding";

Check failure

Code scanning / CodeQL

Use of RSA algorithm without OAEP High

This specification is used to
initialize an RSA cipher
without OAEP padding.

Copilot Autofix

AI 3 days ago

In general, the fix is to ensure RSA is used with OAEP padding for all new encryption, and that any unavoidable PKCS#1 v1.5 usage is strictly limited to decrypting pre-existing legacy data, clearly labeled and not used for general-purpose operations. Since this class already uses RSA_TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" for modern operations, the main work is to isolate and clearly mark the PKCS#1 transformation as a legacy-only decryption path to satisfy the intent of the rule while preserving functionality.

The single best fix here without changing behavior is:

  • Keep LEGACY_PKCS1_RSA_TRANSFORMATION but strengthen its documentation, explicitly stating it MUST NOT be used for encryption and is only for decrypting old data during migration.
  • Confine PKCS#1 usage to the dedicated RSADecryptLegacyPKCS1 method (which is already the case) and add Javadoc that explains it is for one-way migration from legacy PKCS#1 to OAEP.
  • Ensure that all normal RSA decryption uses RSA_TRANSFORMATION (OAEP); based on the snippet, RSADecryptLegacyPKCS1 is already a separate helper, so we only clarify its role.

Concretely in auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java:

  • Adjust the block comment above LEGACY_PKCS1_RSA_TRANSFORMATION to highlight that it is legacy-decrypt-only and not to be used for encrypting or for arbitrary attacker-supplied ciphertext whenever possible.
  • Add a similar explanatory Javadoc above RSADecryptLegacyPKCS1 making it clear it is the only place PKCS#1 is used, and that callers should migrate data and then avoid using it.

No new methods, imports, or algorithm changes are required; we are tightening and documenting the legacy usage so the intent is explicit and future maintainers do not mistakenly expand PKCS#1 use.

Suggested changeset 1
auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.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/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java b/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
--- a/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
+++ b/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
@@ -56,15 +56,18 @@
     private static final String RSA_TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
     /**
      * !!! WARNING !!!
-     * "RSA/ECB/PKCS1Padding" is cryptographically deprecated due to vulnerabilities
-     * (e.g. Bleichenbacher padding oracle attacks) and MUST NOT be used for encrypting
-     * new data or for any general-purpose RSA operations.
-     * 
+     * "RSA/ECB/PKCS1Padding" (PKCS#1 v1.5 padding) is cryptographically deprecated due to
+     * vulnerabilities such as Bleichenbacher-style padding oracle attacks and MUST NOT be
+     * used for encrypting new data or for any general-purpose RSA operations.
+     *
      * This transformation exists solely to DECRYPT pre-existing legacy data that was
      * originally encrypted with PKCS#1 v1.5 padding, so that it can be re-encrypted
      * using the secure OAEP-based {@link #RSA_TRANSFORMATION}. Once all legacy data has
      * been migrated, support for this constant and any code paths that use it should be
      * removed.
+     *
+     * Do NOT use this constant for any new encryption, and do NOT introduce new call sites
+     * other than the dedicated legacy decryption helper in this class.
      */
     private static final String LEGACY_PKCS1_RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding";
     // https://developer.android.com/reference/javax/crypto/Cipher.html
@@ -119,6 +117,17 @@
      * @throws BadPaddingException If the encrypted data has invalid padding
      * @throws IllegalBlockSizeException If the encrypted data size is invalid
      */
+    /**
+     * Decrypt data that was encrypted with the legacy PKCS#1 v1.5 RSA padding.
+     * <p>
+     * This method is provided solely to support migration of data that was encrypted
+     * before OAEP was adopted in {@link #RSA_TRANSFORMATION}. Callers should:
+     * <ul>
+     *     <li>Use this method only for existing legacy ciphertexts, and</li>
+     *     <li>Immediately re-encrypt the resulting plaintext using OAEP.</li>
+     * </ul>
+     * It MUST NOT be used for encrypting or for handling newly-created ciphertexts.
+     */
     @NonNull
     private static byte[] RSADecryptLegacyPKCS1(@NonNull byte[] encryptedData,
                                                  @NonNull PrivateKey privateKey)
EOF
@@ -56,15 +56,18 @@
private static final String RSA_TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
/**
* !!! WARNING !!!
* "RSA/ECB/PKCS1Padding" is cryptographically deprecated due to vulnerabilities
* (e.g. Bleichenbacher padding oracle attacks) and MUST NOT be used for encrypting
* new data or for any general-purpose RSA operations.
*
* "RSA/ECB/PKCS1Padding" (PKCS#1 v1.5 padding) is cryptographically deprecated due to
* vulnerabilities such as Bleichenbacher-style padding oracle attacks and MUST NOT be
* used for encrypting new data or for any general-purpose RSA operations.
*
* This transformation exists solely to DECRYPT pre-existing legacy data that was
* originally encrypted with PKCS#1 v1.5 padding, so that it can be re-encrypted
* using the secure OAEP-based {@link #RSA_TRANSFORMATION}. Once all legacy data has
* been migrated, support for this constant and any code paths that use it should be
* removed.
*
* Do NOT use this constant for any new encryption, and do NOT introduce new call sites
* other than the dedicated legacy decryption helper in this class.
*/
private static final String LEGACY_PKCS1_RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding";
// https://developer.android.com/reference/javax/crypto/Cipher.html
@@ -119,6 +117,17 @@
* @throws BadPaddingException If the encrypted data has invalid padding
* @throws IllegalBlockSizeException If the encrypted data size is invalid
*/
/**
* Decrypt data that was encrypted with the legacy PKCS#1 v1.5 RSA padding.
* <p>
* This method is provided solely to support migration of data that was encrypted
* before OAEP was adopted in {@link #RSA_TRANSFORMATION}. Callers should:
* <ul>
* <li>Use this method only for existing legacy ciphertexts, and</li>
* <li>Immediately re-encrypt the resulting plaintext using OAEP.</li>
* </ul>
* It MUST NOT be used for encrypting or for handling newly-created ciphertexts.
*/
@NonNull
private static byte[] RSADecryptLegacyPKCS1(@NonNull byte[] encryptedData,
@NonNull PrivateKey privateKey)
Copilot is powered by AI and may make mistakes. Always verify output.
@NonNull PrivateKey privateKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher rsaPkcs1Cipher = Cipher.getInstance(LEGACY_PKCS1_RSA_TRANSFORMATION);

Check failure

Code scanning / CodeQL

Use of RSA algorithm without OAEP High

This specification is used to
initialize an RSA cipher
without OAEP padding.

Copilot Autofix

AI 3 days ago

In general, the way to fix RSA‑without‑OAEP findings is to use an OAEP transformation string with Cipher.getInstance (e.g. "RSA/ECB/OAEPWithSHA-256AndMGF1Padding") and ensure that all new RSA encryption and decryption uses that scheme. However, in this snippet the method is specifically for decrypting legacy PKCS1‑padded ciphertext; switching this to OAEP would make it impossible to decrypt existing data. The best fix that preserves functionality is therefore:

  • Keep PKCS1 use confined to this one migration helper.
  • Make the legacy nature explicit in code by:
    • Renaming or reinforcing the constant name to show it is legacy (if accessible) and
    • Adding strong documentation and, if possible, annotations to discourage any reuse.
  • Ensure that all non‑legacy RSA operations in this file (not shown in the snippet) use OAEP, and that this helper is not reused for anything else.

Within the given snippet, we can strengthen the guardrails on this method by adding a prominent warning comment and, if available, an annotation signaling that it is for legacy/migration only. We must not change Cipher.getInstance(LEGACY_PKCS1_RSA_TRANSFORMATION) to an OAEP transformation because the method’s contract is to decrypt PKCS1 ciphertext.

Concretely in auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java:

  • Above RSADecryptLegacyPKCS1, add or enhance the documentation comment to clearly state that this method must never be used for new encryption schemes and that all new RSA usage must use OAEP. The snippet already contains such wording; we can tighten it further.
  • Optionally add an internal @VisibleForTesting or similar annotation if available, but we should not introduce new dependencies. Since we already import androidx.annotation.VisibleForTesting, we can reuse that as appropriate, but adding it to this helper would misrepresent its purpose (it’s not only for testing), so we will instead only strengthen the comment.

No imports, methods, or external libs are required beyond updating the comment to make the legacy nature explicit for static analysis reviewers and developers. The functional code (lines 127–129) remains unchanged to preserve decryption compatibility.

Suggested changeset 1
auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.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/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java b/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
--- a/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
+++ b/auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java
@@ -107,9 +107,11 @@
     /**
      * Decrypts data that was encrypted using legacy RSA/PKCS1 padding.
      * <p>
-     * WARNING: This must only be used for decrypting legacy data during migration.
-     * New code must always use OAEP padding for RSA encryption/decryption.
-     * 
+     * WARNING: This method exists solely to support decryption of data that was
+     * previously encrypted with RSA/PKCS1 padding in older versions of this SDK.
+     * It MUST NOT be used for encrypting or decrypting any new data. All new RSA
+     * encryption/decryption must use an OAEP-based transformation instead.
+     *
      * @param encryptedData The data encrypted with PKCS1 padding
      * @param privateKey The private key for decryption
      * @return The decrypted data
EOF
@@ -107,9 +107,11 @@
/**
* Decrypts data that was encrypted using legacy RSA/PKCS1 padding.
* <p>
* WARNING: This must only be used for decrypting legacy data during migration.
* New code must always use OAEP padding for RSA encryption/decryption.
*
* WARNING: This method exists solely to support decryption of data that was
* previously encrypted with RSA/PKCS1 padding in older versions of this SDK.
* It MUST NOT be used for encrypting or decrypting any new data. All new RSA
* encryption/decryption must use an OAEP-based transformation instead.
*
* @param encryptedData The data encrypted with PKCS1 padding
* @param privateKey The private key for decryption
* @return The decrypted data
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

Comments suppressed due to low confidence (1)

auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java:391

  • String concatenation using += in a loop can be inefficient. While this particular loop is expected to be short (traversing the exception chain), it's better practice to use a StringBuilder for string concatenation. This improves performance and is more maintainable if the loop grows longer in the future.
            throw new IncompatibleDeviceException(e);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            /*
             * They really should not be thrown at all since padding is requested in the transformation.
             * Delete the AES keys since those originated the input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (!TextUtils.isEmpty(encodedEncryptedAES)) {
byte[] encryptedAESBytes = Base64.decode(encodedEncryptedAES, Base64.DEFAULT);
try {
return RSADecrypt(encryptedAESBytes);
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing validation of the decrypted AES key length after PKCS1 decryption. The original getAESKey implementation validated that the AES key has the expected length (32 bytes for 256-bit AES) before returning it. Without this validation, a corrupted or malformed legacy key could be migrated and cause issues later. Consider validating that decryptedAESKey.length equals AES_KEY_SIZE / 8 before proceeding with re-encryption.

Suggested change
return RSADecrypt(encryptedAESBytes);
byte[] decryptedAESKey = RSADecrypt(encryptedAESBytes);
// Validate that the decrypted AES key has the expected length (e.g. 32 bytes for 256-bit AES)
if (decryptedAESKey == null || decryptedAESKey.length != AES_KEY_SIZE / 8) {
// Treat this as corrupted key material: clean up and signal an error
deleteRSAKeys();
deleteAESKeys();
throw new CryptoException("The RSA decrypted AES key has an unexpected length.");
}
return decryptedAESKey;

Copilot uses AI. Check for mistakes.
Comment on lines +464 to +467
NoSuchPaddingException | InvalidKeyException |
IllegalBlockSizeException | BadPaddingException ex) {
Log.e(TAG, "Could not migrate. A new key will be generated.", ex);
deleteRSAKeys();
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching a generic Exception here is overly broad and could hide unexpected issues. The catch block swallows all exceptions (including potential runtime exceptions) and simply generates a new key, which could mask serious bugs. Consider catching only the specific checked exceptions that can be thrown by the operations inside the try block (NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, CryptoException, KeyStoreException, CertificateException, IOException, UnrecoverableEntryException) to maintain better error visibility.

Copilot uses AI. Check for mistakes.
Comment on lines +490 to +493
rsaKeyEntry.getPrivateKey()
);

byte[] encryptedAESWithOAEP = RSAEncrypt(decryptedAESKey);
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching a generic Exception here is overly broad and could mask programming errors or unexpected runtime exceptions. The outer catch for the key generation logic should only catch the specific exceptions that can be thrown. Since this is a new key generation path, catching Exception and wrapping it in CryptoException reduces visibility into actual failures. Consider catching only IncompatibleDeviceException and CryptoException that might propagate from RSAEncrypt.

Copilot uses AI. Check for mistakes.
when(keyStore.getEntry(eq(KEY_ALIAS), nullable(KeyStore.ProtectionParameter.class)))
.thenReturn(mockKeyEntry);

byte[] aesKeyBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses a 16-byte AES key, but AES-256 requires a 32-byte key (AES_KEY_SIZE = 256 bits = 32 bytes). The implementation should validate the decrypted AES key length to ensure it matches the expected 32 bytes. Using a 16-byte key in the test doesn't properly validate that the migration logic would reject incorrectly-sized keys. Consider using a properly-sized 32-byte key in the test to match the actual AES-256 specification.

Copilot uses AI. Check for mistakes.
String newEncodedEncryptedAES = new String(
Base64.encode(encryptedAESWithOAEP, Base64.DEFAULT),
StandardCharsets.UTF_8
);
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing validation of the decrypted AES key length after PKCS1 decryption from OLD_KEY_ALIAS. The original getAESKey implementation validated that the AES key has the expected length (32 bytes for 256-bit AES) before returning it. Without this validation, a corrupted or malformed legacy key could be migrated. Consider validating that decryptedAESKey.length equals AES_KEY_SIZE / 8 before proceeding with re-encryption.

Copilot uses AI. Check for mistakes.
public void shouldDetectAndMigratePKCS1KeyToOAEP() throws Exception {
CryptoUtil cryptoUtil = newCryptoUtilSpy();

byte[] aesKeyBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses a 16-byte AES key (line 1701), but AES-256 requires a 32-byte key (AES_KEY_SIZE = 256 bits = 32 bytes). The implementation should validate the decrypted AES key length to ensure it matches the expected 32 bytes. Using a 16-byte key in the test doesn't properly validate that the migration logic would reject incorrectly-sized keys. Consider using a properly-sized 32-byte key in the test to match the actual AES-256 specification.

Copilot uses AI. Check for mistakes.
public void shouldUseOAEPDirectlyForNewUsers() throws Exception {
CryptoUtil cryptoUtil = newCryptoUtilSpy();

byte[] aesKeyBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses a 16-byte AES key, but AES-256 requires a 32-byte key (AES_KEY_SIZE = 256 bits = 32 bytes). The implementation should validate the decrypted AES key length to ensure it matches the expected 32 bytes. Using a 16-byte key in the test doesn't properly validate that the migration logic would reject incorrectly-sized keys. Consider using a properly-sized 32-byte key in the test to match the actual AES-256 specification.

Copilot uses AI. Check for mistakes.
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.

3 participants