Skip to content

RSA encryption padding change from PKCS1Padding to OAEPWithSHA-256And… #834

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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);

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.

// 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";

Check failure

Code scanning / CodeQL

Use of RSA algorithm without OAEP High

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

Copilot Autofix

AI 29 days ago

Copilot could not generate an autofix suggestion

Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was the PR which we are fixing to use OAEP padding.

"("+ OLD_PKCS1_RSA_TRANSFORMATION +") for migration due to " +
"InvalidKeyException with OAEP.");
KeyStore.PrivateKeyEntry rsaKeyEntry = getRSAKeyEntry();
Cipher rsaPkcs1Cipher = Cipher.getInstance(OLD_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.
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);

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 29 days ago

To address the issue, the fallback decryption logic using OLD_PKCS1_RSA_TRANSFORMATION should be replaced with a secure alternative. Since the fallback is for legacy data, the best approach is to migrate the data securely to use OAEP padding. This can be achieved by decrypting the data with PKCS1 (if necessary) and immediately re-encrypting it with OAEP. This ensures that future operations do not rely on the insecure PKCS1 padding.

The specific changes involve:

  1. Replacing the use of OLD_PKCS1_RSA_TRANSFORMATION with a secure OAEP-based transformation.
  2. Adding logic to re-encrypt the decrypted data with OAEP after successful decryption with PKCS1.
  3. Logging the migration process to ensure traceability.

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
@@ -422,5 +422,16 @@
                         rsaPkcs1Cipher.init(Cipher.DECRYPT_MODE, rsaKeyEntry.getPrivateKey());
-                        decryptedAESKey = rsaPkcs1Cipher.doFinal(encryptedAESBytes);
+                        byte[] legacyDecryptedAESKey = rsaPkcs1Cipher.doFinal(encryptedAESBytes);
                         Log.i(TAG, "Successfully decrypted legacy AES key using PKCS1Padding."
-                                + " Migration is needed.");
+                                + " Re-encrypting with OAEP for migration.");
+                        
+                        // Re-encrypt the key with OAEP
+                        Cipher rsaOaepCipher = Cipher.getInstance(RSA_TRANSFORMATION);
+                        rsaOaepCipher.init(Cipher.ENCRYPT_MODE, rsaKeyEntry.getCertificate().getPublicKey());
+                        decryptedAESKey = rsaOaepCipher.doFinal(legacyDecryptedAESKey);
+                        
+                        // Update storage with the re-encrypted key
+                        String reEncryptedAESKey = Base64.encodeToString(decryptedAESKey, Base64.DEFAULT);
+                        storage.storeString(KEY_ALIAS, reEncryptedAESKey);
+                        
+                        Log.i(TAG, "Successfully migrated AES key to OAEP.");
                         migrationNeeded = true;
EOF
@@ -422,5 +422,16 @@
rsaPkcs1Cipher.init(Cipher.DECRYPT_MODE, rsaKeyEntry.getPrivateKey());
decryptedAESKey = rsaPkcs1Cipher.doFinal(encryptedAESBytes);
byte[] legacyDecryptedAESKey = rsaPkcs1Cipher.doFinal(encryptedAESBytes);
Log.i(TAG, "Successfully decrypted legacy AES key using PKCS1Padding."
+ " Migration is needed.");
+ " Re-encrypting with OAEP for migration.");

// Re-encrypt the key with OAEP
Cipher rsaOaepCipher = Cipher.getInstance(RSA_TRANSFORMATION);
rsaOaepCipher.init(Cipher.ENCRYPT_MODE, rsaKeyEntry.getCertificate().getPublicKey());
decryptedAESKey = rsaOaepCipher.doFinal(legacyDecryptedAESKey);

// Update storage with the re-encrypted key
String reEncryptedAESKey = Base64.encodeToString(decryptedAESKey, Base64.DEFAULT);
storage.storeString(KEY_ALIAS, reEncryptedAESKey);

Log.i(TAG, "Successfully migrated AES key to OAEP.");
migrationNeeded = true;
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was the PR which we are fixing to use OAEP padding.

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.

1 participant