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

Move Broker encryption logic to broker code. Some more refactoring. #2016

Merged
merged 10 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
V.NEXT
----------
- [MAJOR] Move Broker encryption logic to broker code. Some more refactoring. (#2016)
- [PATCH] Fix Error Type thrown for NO_ACCOUNT_FOUND (#2006)
- [MINOR] Separate constants for max client and broker protocol versions (#2008)
- [PATCH] Pulling device cert issuer check to beginning of OnReceivedClientCertRequest (#2010)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ public class SharedPreferencesFileManagerTests extends AndroidSecretKeyEnabledHe
private static final String sTEST_SHARED_PREFS_NAME = "com.microsoft.test.preferences";
private static final String sTEST_KEY = "test_key";
private static final String sTEST_VALUE = "test_value";
private static final IKeyAccessor sTEST_ENCRYPTION_MANAGER = new AndroidAuthSdkStorageEncryptionManager(ApplicationProvider.getApplicationContext(), null);
private static final IKeyAccessor sTEST_ENCRYPTION_MANAGER =
new AndroidAuthSdkStorageEncryptionManager(ApplicationProvider.getApplicationContext());

private SharedPreferencesFileManager mSharedPreferencesFileManager;

@Parameterized.Parameters
public static Iterable<SharedPreferencesFileManager> testParams() {
return Arrays.asList(new SharedPreferencesFileManager[]{
SharedPreferencesFileManager.getSharedPreferences(
ApplicationProvider.getApplicationContext(),
sTEST_SHARED_PREFS_NAME,
null),
return Arrays.asList(SharedPreferencesFileManager.getSharedPreferences(
ApplicationProvider.getApplicationContext(),
sTEST_SHARED_PREFS_NAME,
null),
SharedPreferencesFileManager.getSharedPreferences(
ApplicationProvider.getApplicationContext(),
sTEST_SHARED_PREFS_NAME,
sTEST_ENCRYPTION_MANAGER)
});
sTEST_ENCRYPTION_MANAGER));
}

public SharedPreferencesFileManagerTests(final SharedPreferencesFileManager sharedPreferencesFileManager) {
Expand Down Expand Up @@ -122,7 +121,7 @@ public void testGetSharedPreferencesNoKeyAccessor() throws Exception {
IKeyAccessor keyAccessor = mkeyAccessor;
IKeyAccessor newKeyAccessor;
if (keyAccessor == null) {
newKeyAccessor = new AndroidAuthSdkStorageEncryptionManager(ApplicationProvider.getApplicationContext(), null);
newKeyAccessor = new AndroidAuthSdkStorageEncryptionManager(ApplicationProvider.getApplicationContext());
} else {
newKeyAccessor = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class AndroidWrappedKeyLoaderTest {

final Context context = ApplicationProvider.getApplicationContext();
final String MOCK_KEY_ALIAS = "MOCK_KEY_ALIAS";
final String MOCK_KEY_FILE_PATH = "MOCK_KEY_FILE_PATH";
final int TEST_LOOP = 100;

@Before
Expand All @@ -67,7 +68,7 @@ public void setUp() throws Exception {
private File getKeyFile() {
return new File(
context.getDir(context.getPackageName(), Context.MODE_PRIVATE),
AndroidWrappedKeyLoader.KEY_FILE_PATH);
MOCK_KEY_FILE_PATH);
}

@Test
Expand Down Expand Up @@ -109,7 +110,7 @@ private AlgorithmParameterSpec getMockKeyPairGeneratorSpec(final String alias) {

@Test
public void testGenerateKey() throws ClientException {
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);
final SecretKey secretKey = keyLoader.generateRandomKey();

Assert.assertEquals(AES256KeyLoader.AES_ALGORITHM, secretKey.getAlgorithm());
Expand Down Expand Up @@ -139,7 +140,7 @@ public void testLoadKey() throws ClientException {
Assert.assertNull(AndroidKeyStoreUtil.readKey(MOCK_KEY_ALIAS));
Assert.assertNull(FileUtil.readFromFile(getKeyFile(), AndroidWrappedKeyLoader.KEY_FILE_SIZE));

final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);
final SecretKey secretKey = keyLoader.getKey();

final SecretKey key = keyLoader.getKeyCache().getData();
Expand All @@ -152,7 +153,7 @@ public void testLoadKey() throws ClientException {
@Test
public void testLoadKeyFromCorruptedFile_TruncatedExisingKey() throws ClientException {
// Create a new Keystore-wrapped key.
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);
keyLoader.generateRandomKey();

final byte[] wrappedKey = FileUtil.readFromFile(getKeyFile(), AndroidWrappedKeyLoader.KEY_FILE_SIZE);
Expand All @@ -179,7 +180,7 @@ public void testLoadKeyFromCorruptedFile_TruncatedExisingKey() throws ClientExce
@Test
public void testLoadKeyFromCorruptedFile_InjectGarbage() throws ClientException {
// Create a new Keystore-wrapped key.
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);
keyLoader.generateRandomKey();

final byte[] wrappedKey = FileUtil.readFromFile(getKeyFile(), AndroidWrappedKeyLoader.KEY_FILE_SIZE);
Expand All @@ -203,12 +204,11 @@ public void testLoadKeyFromCorruptedFile_InjectGarbage() throws ClientException
Assert.assertNull(keyLoader.readSecretKeyFromStorage());
}


// 1s With Google Pixel XL, OS Version 29 (100 loop)
@Test
@Ignore
public void testPerf_WithCachedKey() throws ClientException {
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);

long timeStartLoop = System.nanoTime();
for (int i = 0; i < TEST_LOOP; i++) {
Expand All @@ -223,7 +223,7 @@ public void testPerf_WithCachedKey() throws ClientException {
@Test
@Ignore
public void testPerf_NoCachedKey() throws ClientException {
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);

long timeStartLoopNotCached = System.nanoTime();
for (int i = 0; i < 100; i++) {
Expand Down Expand Up @@ -261,7 +261,7 @@ public void testLoadDeletedKeyFile() throws ClientException {
}

private AndroidWrappedKeyLoader initKeyLoaderWithKeyEntry() throws ClientException {
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, context, null);
final AndroidWrappedKeyLoader keyLoader = new AndroidWrappedKeyLoader(MOCK_KEY_ALIAS, MOCK_KEY_FILE_PATH, context);
final SecretKey key = keyLoader.getKey();
Assert.assertNotNull(key);
Assert.assertNotNull(keyLoader.getKeyCache().getData());
Expand Down
Loading