Skip to content

Commit

Permalink
Merge pull request #29 from neXenio/bugfix/028-unable-to-create-key-p…
Browse files Browse the repository at this point in the history
…air-when-using-arabic

Bugfix/028 unable to create key pair when using arabic
  • Loading branch information
marvinmirtschin authored Jun 10, 2021
2 parents da0211c + 96197d6 commit a086049
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 24 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nexenio.rxkeystore.provider.cipher.asymmetric.rsa;
package com.nexenio.rxkeystore;

import org.junit.Test;

Expand Down Expand Up @@ -28,7 +28,7 @@ public void generateSecretKey_matchingKeyPairs_sameSecretKey() throws Exception

private byte[] generateSecret(PrivateKey privateKey, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException {
KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
keyAgreement.init(privateKey);
keyAgreement.init(privateKey); // TODO: 03.06.21 DHPublicKey not for this KeyAgreement! on sdk 21 and 22
keyAgreement.doPhase(publicKey, true);
return keyAgreement.generateSecret();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.nexenio.rxkeystore.provider.cipher.symmetric.aes.AesCipherProvider;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -41,34 +43,60 @@ public class RxKeyStoreTest {
private static final String KEY_STORE_FILE_NAME = "keys.ks";
private static final String KEY_STORE_PASSWORD = "password";

protected static Provider originalProvider;
protected static int originalPosition;

private Context context;
private RxKeyStore keyStore;
private RxAsymmetricCipherProvider asymmetricCryptoProvider;

private KeyPair defaultKeyPair;

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@Before
public void setUp() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
keyStore = new RxKeyStore(RxKeyStore.TYPE_BKS, RxKeyStore.PROVIDER_BOUNCY_CASTLE);
asymmetricCryptoProvider = new RsaCipherProvider(keyStore);

setupSecurityProviders();

resetKeyStore().andThen(generateDefaultKeyPair())
.test()
.assertComplete();
}

protected void setupSecurityProviders() {
final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
if (!(provider instanceof BouncyCastleProvider)) {
protected static void setupSecurityProviders() {
Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) {
Provider provider = providers[i];
if (BouncyCastleProvider.PROVIDER_NAME.equals(provider.getName())) {
originalProvider = provider;
originalPosition = i;
}
}
// originalProvider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
if (!(originalProvider instanceof BouncyCastleProvider)) {
// Android registers its own BC provider. As it might be outdated and might not include
// all needed ciphers, we substitute it with a known BC bundled in the app.
// Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
// of that it's possible to have another BC implementation loaded in VM.
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.insertProviderAt(new BouncyCastleProvider(), 1);
Security.insertProviderAt(new BouncyCastleProvider(), originalPosition + 1);
}
}

protected static void cleanUpSecurityProviders() {
if (originalProvider != null) {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.insertProviderAt(originalProvider, originalPosition + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,43 @@ public abstract class BaseCryptoProviderTest {

protected static final String LOREM_IPSUM_LONG = "Cras pharetra pulvinar interdum. Integer bibendum neque dolor, quis sodales dui bibendum non. Curabitur eleifend massa eros, sollicitudin porta lacus convallis eget. Mauris molestie vulputate mi. Pellentesque sit amet tortor a justo tincidunt viverra faucibus a erat. Praesent nec ante eget leo placerat congue eget id elit. Vivamus maximus consectetur malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed at bibendum lorem, ut bibendum nunc. Quisque ut justo non augue egestas rhoncus quis vitae neque. Integer feugiat diam sapien, eu tincidunt ipsum rutrum vitae. Cras pharetra pulvinar interdum. Integer bibendum neque dolor, quis sodales dui bibendum non. Curabitur eleifend massa eros, sollicitudin porta lacus convallis eget. Mauris molestie vulputate mi. Pellentesque sit amet tortor a justo tincidunt viverra faucibus a erat. Praesent nec ante eget leo placerat congue eget id elit. Vivamus maximus consectetur malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed at bibendum lorem, ut bibendum nunc. Quisque ut justo non augue egestas rhoncus quis vitae neque. Integer feugiat diam sapien, eu tincidunt ipsum rutrum vitae. Cras pharetra pulvinar interdum. Integer bibendum neque dolor, quis sodales dui bibendum non. Curabitur eleifend massa eros, sollicitudin porta lacus convallis eget. Mauris molestie vulputate mi. Pellentesque sit amet tortor a justo tincidunt viverra faucibus a erat. Praesent nec ante eget leo placerat congue eget id elit. Vivamus maximus consectetur malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed at bibendum lorem, ut bibendum nunc. Quisque ut justo non augue egestas rhoncus quis vitae neque. Integer feugiat diam sapien, eu tincidunt ipsum rutrum.";

protected static Provider originalProvider;
protected static int originalPosition;

protected Context context;
protected RxKeyStore keyStore;
protected RxCryptoProvider cryptoProvider;

@CallSuper
protected void setUpBeforeEachTest() {
setupSecurityProviders();

context = InstrumentationRegistry.getInstrumentation().getTargetContext();
keyStore = createKeyStore();
cryptoProvider = createCryptoProvider(keyStore);
}

protected void setupSecurityProviders() {
final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
if (!(provider instanceof BouncyCastleProvider)) {
public static void setupSecurityProviders() {
Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) {
Provider provider = providers[i];
if (BouncyCastleProvider.PROVIDER_NAME.equals(provider.getName())) {
originalProvider = provider;
originalPosition = i;
}
}
if (!(originalProvider instanceof BouncyCastleProvider)) {
// Android registers its own BC provider. As it might be outdated and might not include
// all needed ciphers, we substitute it with a known BC bundled in the app.
// Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
// of that it's possible to have another BC implementation loaded in VM.
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.insertProviderAt(new BouncyCastleProvider(), 1);
Security.insertProviderAt(new BouncyCastleProvider(), originalPosition + 1);
}
}

public static void cleanUpSecurityProviders() {
if (originalProvider != null) {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.insertProviderAt(originalProvider, originalPosition + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import android.util.Base64;

import com.nexenio.rxkeystore.RxKeyStore;
import com.nexenio.rxkeystore.provider.RxCryptoProvider;
import com.nexenio.rxkeystore.provider.cipher.BaseCipherProviderTest;
import com.nexenio.rxkeystore.provider.cipher.RxDecryptionException;
import com.nexenio.rxkeystore.provider.RxCryptoProvider;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.security.Key;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -120,4 +122,21 @@ public void generateKeyPair_subsequentCalls_emitsDistinctKeyPairs() {
.assertValue(false);
}

@Test
public void generateKeyPair_arabicAsLocale_emitsKeyPair() {
Locale actualLocale = Locale.getDefault();
Locale expectedLocale = new Locale("ar");
Locale.setDefault(expectedLocale);

try {
asymmetricCryptoProvider.generateKeyPair("alias", context)
.test()
.assertNoErrors();
Assert.assertEquals(Locale.getDefault(), expectedLocale);
} finally {
// clean up
Locale.setDefault(actualLocale);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
import com.nexenio.rxkeystore.provider.cipher.asymmetric.BaseAsymmetricCipherProviderTest;
import com.nexenio.rxkeystore.provider.cipher.asymmetric.RxAsymmetricCipherProvider;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

public class EcCipherProviderTest extends BaseAsymmetricCipherProviderTest {

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@CallSuper
@Before
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.nexenio.rxkeystore.provider.cipher.asymmetric.rsa;

import com.nexenio.rxkeystore.RxKeyStore;
import com.nexenio.rxkeystore.provider.BaseCryptoProviderTest;
import com.nexenio.rxkeystore.provider.RxCryptoProvider;
import com.nexenio.rxkeystore.provider.cipher.asymmetric.RxAsymmetricCipherProvider;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Locale;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

public class RsaCipherProvider2Test extends BaseCryptoProviderTest {

protected RxAsymmetricCipherProvider asymmetricCryptoProvider;

@Before
@Override
@CallSuper
public void setUpBeforeEachTest() {
super.setUpBeforeEachTest();
}

@Override
protected RxKeyStore createKeyStore() {
return new RxKeyStore(RxKeyStore.TYPE_ANDROID, RxKeyStore.PROVIDER_ANDROID_KEY_STORE);
}

@Override
protected RxCryptoProvider createCryptoProvider(@NonNull RxKeyStore keyStore) {
this.asymmetricCryptoProvider = createAsymmetricCryptoProvider(keyStore);
return asymmetricCryptoProvider;
}

protected RxAsymmetricCipherProvider createAsymmetricCryptoProvider(@NonNull RxKeyStore keyStore) {
return new RsaCipherProvider(keyStore);
}

@Test
public void generateKeyPair_arabicAsLocale_emitsKeyPair() {
Locale originalLocale = Locale.getDefault();
Locale expectedLocale = new Locale("ar");
Locale.setDefault(expectedLocale);

try {
asymmetricCryptoProvider.generateKeyPair("alias", context)
.test()
.assertNoErrors();
Assert.assertEquals(Locale.getDefault(), expectedLocale);
} finally {
// clean up
Locale.setDefault(originalLocale);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.nexenio.rxkeystore.provider.cipher.asymmetric.BaseAsymmetricCipherProviderTest;
import com.nexenio.rxkeystore.provider.cipher.asymmetric.RxAsymmetricCipherProvider;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -16,6 +18,16 @@

public class RsaCipherProviderTest extends BaseAsymmetricCipherProviderTest {

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@CallSuper
@Before
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
import com.nexenio.rxkeystore.provider.cipher.symmetric.BaseSymmetricCryptoProviderTest;
import com.nexenio.rxkeystore.provider.cipher.symmetric.RxSymmetricCipherProvider;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

public class RxAESCryptoProviderTest extends BaseSymmetricCryptoProviderTest {

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@CallSuper
@Before
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

import com.nexenio.rxkeystore.RxKeyStore;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

public class Md5HashProviderTest extends BaseHashProviderTest {

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@CallSuper
@Before
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

import com.nexenio.rxkeystore.RxKeyStore;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

public class Sha256HashProviderTest extends BaseHashProviderTest {

@BeforeClass
public static void setUpBeforeClass() {
setupSecurityProviders();
}

@AfterClass
public static void cleanUpAfterClass() {
cleanUpSecurityProviders();
}

@CallSuper
@Before
@Override
Expand Down
Loading

0 comments on commit a086049

Please sign in to comment.