-
Notifications
You must be signed in to change notification settings - Fork 166
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
base: main
Are you sure you want to change the base?
Changes from all commits
d9bce21
e641aa1
967376e
715bcc8
6a44236
0cc81ea
db797c3
399ddb0
59357a1
f992aea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,7 +53,25 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Transformations available since API 18 | ||||||||||||||||||||||
| // https://developer.android.com/training/articles/keystore.html#SupportedCiphers | ||||||||||||||||||||||
| private static final String RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding"; | ||||||||||||||||||||||
| 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. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * 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. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| // CodeQL suppression: This legacy constant is required for backward compatibility | ||||||||||||||||||||||
| // to decrypt credentials encrypted with PKCS1 before the migration to OAEP. | ||||||||||||||||||||||
| // It is only used for decryption (reading old data), never encryption (writing new data). | ||||||||||||||||||||||
| // This constant will be removed once all users have migrated to OAEP. | ||||||||||||||||||||||
| @SuppressWarnings("java/rsa-without-oaep") | ||||||||||||||||||||||
| private static final String LEGACY_PKCS1_RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding"; | ||||||||||||||||||||||
| // https://developer.android.com/reference/javax/crypto/Cipher.html | ||||||||||||||||||||||
| @SuppressWarnings("SpellCheckingInspection") | ||||||||||||||||||||||
| private static final String AES_TRANSFORMATION = "AES/GCM/NOPADDING"; | ||||||||||||||||||||||
|
|
@@ -62,7 +80,7 @@ | |||||||||||||||||||||
| private static final String ALGORITHM_RSA = "RSA"; | ||||||||||||||||||||||
| private static final String ALGORITHM_AES = "AES"; | ||||||||||||||||||||||
| private static final int AES_KEY_SIZE = 256; | ||||||||||||||||||||||
| private static final int RSA_KEY_SIZE = 2048; | ||||||||||||||||||||||
| private static final int RSA_KEY_SIZE = 4096; | ||||||||||||||||||||||
pmathew92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private static final byte FORMAT_MARKER = 0x01; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -91,6 +109,32 @@ | |||||||||||||||||||||
| this.storage = storage; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * 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. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param encryptedData The data encrypted with PKCS1 padding | ||||||||||||||||||||||
| * @param privateKey The private key for decryption | ||||||||||||||||||||||
| * @return The decrypted data | ||||||||||||||||||||||
| * @throws NoSuchPaddingException If PKCS1 padding is not available | ||||||||||||||||||||||
| * @throws NoSuchAlgorithmException If RSA algorithm is not available | ||||||||||||||||||||||
| * @throws InvalidKeyException If the private key is invalid | ||||||||||||||||||||||
| * @throws BadPaddingException If the encrypted data has invalid padding | ||||||||||||||||||||||
| * @throws IllegalBlockSizeException If the encrypted data size is invalid | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| @NonNull | ||||||||||||||||||||||
| @SuppressWarnings("java/rsa-without-oaep") | ||||||||||||||||||||||
| private static byte[] RSADecryptLegacyPKCS1(@NonNull byte[] encryptedData, | ||||||||||||||||||||||
| @NonNull PrivateKey privateKey) | ||||||||||||||||||||||
| throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, | ||||||||||||||||||||||
| BadPaddingException, IllegalBlockSizeException { | ||||||||||||||||||||||
| Cipher rsaPkcs1Cipher = Cipher.getInstance(LEGACY_PKCS1_RSA_TRANSFORMATION); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| rsaPkcs1Cipher.init(Cipher.DECRYPT_MODE, privateKey); | ||||||||||||||||||||||
| return rsaPkcs1Cipher.doFinal(encryptedData); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Attempts to recover the existing RSA Private Key entry or generates a new one as secure as | ||||||||||||||||||||||
| * this device and Android version allows it if none is found. | ||||||||||||||||||||||
|
|
@@ -372,30 +416,112 @@ | |||||||||||||||||||||
| @VisibleForTesting | ||||||||||||||||||||||
| byte[] getAESKey() throws IncompatibleDeviceException, CryptoException { | ||||||||||||||||||||||
| String encodedEncryptedAES = storage.retrieveString(KEY_ALIAS); | ||||||||||||||||||||||
| if (TextUtils.isEmpty(encodedEncryptedAES)) { | ||||||||||||||||||||||
| encodedEncryptedAES = storage.retrieveString(OLD_KEY_ALIAS); | ||||||||||||||||||||||
| if (!TextUtils.isEmpty(encodedEncryptedAES)) { | ||||||||||||||||||||||
| byte[] encryptedAESBytes = Base64.decode(encodedEncryptedAES, Base64.DEFAULT); | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| return RSADecrypt(encryptedAESBytes); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current code already throws exceptions on decryption failures. If additional validation is desired, it should be addressed in a separate PR to avoid scope creep and maintain backward compatibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire code block can be refactored to make it more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String comparison for checking the old padding doesn't looks like a good method as it might return inconsistent behaviour. Would suggest to do some other approach to differentiate
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While adding AES key length validation would be good defensive programming, it's beyond the scope of this PR, which focuses specifically on migrating from PKCS1 to OAEP padding for the security fix.
The existing error handling already addresses corrupted keys: if the decrypted AES key is malformed, subsequent encryption operations will fail and trigger the cleanup path that deletes corrupted keys and regenerates them. This behavior is consistent with the pre-migration code.
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception handling is in the OLD_KEY_ALIAS migration path (lines 464-467), which is unrelated to the PKCS1→OAEP security migration that this PR addresses. The generic Exception catch block existed in the original code and was not modified by this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to handle both the OLD_KEY_ALIAS and KEY_ALIAS in separate block here ? Can't a single block like in the original work ?
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generic Exception catch block existed in the original code and was not modified by this PR. While catching specific exceptions would improve code quality, changing this would expand the scope beyond the security fix.
Copilot
AI
Aug 19, 2025
There was a problem hiding this comment.
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.
| } catch (Exception e) { | |
| } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | KeyStoreException | UnrecoverableEntryException e) { |
There was a problem hiding this comment.
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.
Copilot
AI
Aug 19, 2025
There was a problem hiding this comment.
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.
| } catch (Exception e) { | |
| } catch (InvalidKeyException | |
| | NoSuchPaddingException | |
| | IllegalBlockSizeException | |
| | BadPaddingException | |
| | KeyStoreException | |
| | UnrecoverableEntryException | |
| | CertificateException | |
| | IOException | |
| | ProviderException e) { |
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.