check e2e keys

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2021-05-19 09:56:43 +02:00
parent f6d14856a4
commit df2714ffad
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
3 changed files with 85 additions and 9 deletions

View file

@ -52,6 +52,8 @@ import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import javax.crypto.BadPaddingException;
import androidx.test.runner.AndroidJUnit4;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
@ -144,6 +146,33 @@ public class EncryptionTestIT {
assertTrue(Arrays.equals(key1, key2));
}
@Test
public void encryptStringAsymmetricCorrectPublicKey() throws Exception {
KeyPair keyPair = EncryptionUtils.generateKeyPair();
byte[] key1 = generateKey();
String base64encodedKey = encodeBytesToBase64String(key1);
String encryptedString = EncryptionUtils.encryptStringAsymmetric(base64encodedKey, keyPair.getPublic());
String decryptedString = decryptStringAsymmetric(encryptedString, keyPair.getPrivate());
byte[] key2 = decodeStringToBase64Bytes(decryptedString);
assertTrue(Arrays.equals(key1, key2));
}
@Test(expected = BadPaddingException.class)
public void encryptStringAsymmetricWrongPublicKey() throws Exception {
KeyPair keyPair1 = EncryptionUtils.generateKeyPair();
KeyPair keyPair2 = EncryptionUtils.generateKeyPair();
byte[] key1 = generateKey();
String base64encodedKey = encodeBytesToBase64String(key1);
String encryptedString = EncryptionUtils.encryptStringAsymmetric(base64encodedKey, keyPair1.getPublic());
decryptStringAsymmetric(encryptedString, keyPair2.getPrivate());
}
@Test
public void encryptStringSymmetricRandom() throws Exception {
int max = 500;

View file

@ -52,6 +52,7 @@ import com.owncloud.android.utils.theme.ThemeColorUtils;
import java.io.IOException;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@ -61,6 +62,11 @@ import androidx.appcompat.app.AlertDialog;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;
import static com.owncloud.android.utils.EncryptionUtils.decodeStringToBase64Bytes;
import static com.owncloud.android.utils.EncryptionUtils.decryptStringAsymmetric;
import static com.owncloud.android.utils.EncryptionUtils.encodeBytesToBase64String;
import static com.owncloud.android.utils.EncryptionUtils.generateKey;
/*
* Dialog to setup encryption
*/
@ -197,9 +203,28 @@ public class SetupEncryptionDialogFragment extends DialogFragment {
dialog.dismiss();
Log_OC.d(TAG, "Private key successfully decrypted and stored");
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), EncryptionUtils.MNEMONIC,
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(),
EncryptionUtils.MNEMONIC,
mnemonicUnchanged);
// check if private key and public key match
String publicKey = arbitraryDataProvider.getValue(user.getAccountName(),
EncryptionUtils.PUBLIC_KEY);
byte[] key1 = generateKey();
String base64encodedKey = encodeBytesToBase64String(key1);
String encryptedString = EncryptionUtils.encryptStringAsymmetric(base64encodedKey,
publicKey);
String decryptedString = decryptStringAsymmetric(encryptedString,
decryptedPrivateKey);
byte[] key2 = decodeStringToBase64Bytes(decryptedString);
if (!Arrays.equals(key1, key2)) {
throw new Exception("Keys do not match");
}
Intent intentExisting = new Intent();
intentExisting.putExtra(SUCCESS, true);
intentExisting.putExtra(ARG_POSITION, getArguments().getInt(ARG_POSITION));
@ -257,7 +282,8 @@ public class SetupEncryptionDialogFragment extends DialogFragment {
Log_OC.d(TAG, "public key successful downloaded for " + user.getAccountName());
String publicKeyFromServer = (String) publicKeyResult.getData().get(0);
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY,
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(),
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer);
} else {
return null;

View file

@ -401,6 +401,17 @@ public final class EncryptionUtils {
return encodeBytesToBase64String(cryptedBytes);
}
public static String encryptStringAsymmetric(String string, PublicKey publicKey) throws NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(RSA_CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = encodeStringToBase64Bytes(string);
byte[] cryptedBytes = cipher.doFinal(bytes);
return encodeBytesToBase64String(cryptedBytes);
}
/**
* Decrypt string with RSA algorithm, ECB mode, OAEPWithSHA-256AndMGF1 padding
@ -431,6 +442,16 @@ public final class EncryptionUtils {
return decodeBase64BytesToString(encodedBytes);
}
public static String decryptStringAsymmetric(String string, PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(RSA_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = decodeStringToBase64Bytes(string);
byte[] encodedBytes = cipher.doFinal(bytes);
return decodeBase64BytesToString(encodedBytes);
}
/**
* Encrypt string with RSA algorithm, ECB mode, OAEPWithSHA-256AndMGF1 padding Asymmetric encryption, with private
* and public key