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

Using OAEP cipher mode to encrypt unique id #2656

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,33 @@ private synchronized static String generateEncryptionKey(String name) {
}

private synchronized static String generateUniqueIdIfNoneStored(String name, int length) {
final String id = readFromSharedPrefs(ID_PREFIX + name);
String uniqueId = null;
boolean storeUniqueId = false;

// Checks if we have a unique identifier stored.
if (id != null) {
final String encryptedUniqueId = readFromSharedPrefs(ID_PREFIX + name);
if (encryptedUniqueId != null) {
final PrivateKey privateKey = KeyStoreWrapper.getInstance().getRSAPrivateKey(KEYSTORE_ALIAS);
uniqueId = Encryptor.decryptWithRSA(privateKey, id, Encryptor.CipherMode.RSA_PKCS1);
uniqueId = Encryptor.decryptWithRSA(privateKey, encryptedUniqueId, Encryptor.CipherMode.RSA_OAEP_SHA256);
if (uniqueId == null) {
SalesforceSDKLogger.e(TAG, "Unable to decrpyt unique identifier stored");
uniqueId = Encryptor.decryptWithRSA(privateKey, encryptedUniqueId, Encryptor.CipherMode.RSA_PKCS1);
storeUniqueId = true;
}
}

// Other create a new one and store it
// Otherwise create a new one and store it
if (uniqueId == null) {
uniqueId = createUniqueId(length);
storeUniqueId = true;
}

// Encrypt and store unique id if it was just created, or if it had to be decrypted with old cipher mode
if (storeUniqueId) {
final PublicKey publicKey = KeyStoreWrapper.getInstance().getRSAPublicKey(KEYSTORE_ALIAS);
final String encryptedKey = Encryptor.encryptWithRSA(publicKey, uniqueId, Encryptor.CipherMode.RSA_PKCS1);
final String encryptedKey = Encryptor.encryptWithRSA(publicKey, uniqueId, Encryptor.CipherMode.RSA_OAEP_SHA256);
storeInSharedPrefs(ID_PREFIX + name, encryptedKey);
}

return uniqueId;
}

Expand Down