|
@@ -39,6 +39,8 @@ import com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation;
|
|
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
|
|
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
|
|
import com.owncloud.android.lib.resources.status.OCCapability;
|
|
import com.owncloud.android.lib.resources.status.OCCapability;
|
|
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
|
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
|
|
|
+import com.owncloud.android.lib.resources.users.DeletePrivateKeyOperation;
|
|
|
|
+import com.owncloud.android.lib.resources.users.DeletePublicKeyOperation;
|
|
import com.owncloud.android.lib.resources.users.GetPrivateKeyOperation;
|
|
import com.owncloud.android.lib.resources.users.GetPrivateKeyOperation;
|
|
import com.owncloud.android.lib.resources.users.GetPublicKeyOperation;
|
|
import com.owncloud.android.lib.resources.users.GetPublicKeyOperation;
|
|
import com.owncloud.android.lib.resources.users.SendCSROperation;
|
|
import com.owncloud.android.lib.resources.users.SendCSROperation;
|
|
@@ -58,7 +60,10 @@ import org.junit.runner.RunWith;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.math.BigInteger;
|
|
import java.security.KeyPair;
|
|
import java.security.KeyPair;
|
|
|
|
+import java.security.interfaces.RSAPrivateCrtKey;
|
|
|
|
+import java.security.interfaces.RSAPublicKey;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
@@ -474,6 +479,36 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
assertFalse(new File(uploadedFile.getStoragePath()).exists());
|
|
assertFalse(new File(uploadedFile.getStoragePath()).exists());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void testCheckCSR() throws Exception {
|
|
|
|
+ deleteKeys();
|
|
|
|
+
|
|
|
|
+ // Create public/private key pair
|
|
|
|
+ KeyPair keyPair = EncryptionUtils.generateKeyPair();
|
|
|
|
+
|
|
|
|
+ // create CSR
|
|
|
|
+ AccountManager accountManager = AccountManager.get(targetContext);
|
|
|
|
+ String userId = accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID);
|
|
|
|
+ String urlEncoded = CsrHelper.generateCsrPemEncodedString(keyPair, userId);
|
|
|
|
+
|
|
|
|
+ SendCSROperation operation = new SendCSROperation(urlEncoded);
|
|
|
|
+ RemoteOperationResult result = operation.execute(account, targetContext);
|
|
|
|
+
|
|
|
|
+ assertTrue(result.isSuccess());
|
|
|
|
+ String publicKeyString = (String) result.getData().get(0);
|
|
|
|
+
|
|
|
|
+ // check key
|
|
|
|
+ RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyPair.getPrivate();
|
|
|
|
+ RSAPublicKey publicKey = EncryptionUtils.convertPublicKeyFromString(publicKeyString);
|
|
|
|
+
|
|
|
|
+ BigInteger modulusPublic = publicKey.getModulus();
|
|
|
|
+ BigInteger modulusPrivate = privateKey.getModulus();
|
|
|
|
+
|
|
|
|
+ assertEquals(modulusPrivate, modulusPublic);
|
|
|
|
+
|
|
|
|
+ createKeys();
|
|
|
|
+ }
|
|
|
|
+
|
|
private void deleteFile(int i) {
|
|
private void deleteFile(int i) {
|
|
ArrayList<OCFile> files = new ArrayList<>();
|
|
ArrayList<OCFile> files = new ArrayList<>();
|
|
for (OCFile file : getStorageManager().getFolderContent(currentFolder, false)) {
|
|
for (OCFile file : getStorageManager().getFolderContent(currentFolder, false)) {
|
|
@@ -529,11 +564,11 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
private void useExistingKeys() throws Exception {
|
|
private void useExistingKeys() throws Exception {
|
|
// download them from server
|
|
// download them from server
|
|
GetPublicKeyOperation publicKeyOperation = new GetPublicKeyOperation();
|
|
GetPublicKeyOperation publicKeyOperation = new GetPublicKeyOperation();
|
|
- RemoteOperationResult publicKeyResult = publicKeyOperation.execute(account, targetContext);
|
|
|
|
|
|
+ RemoteOperationResult<String> publicKeyResult = publicKeyOperation.execute(account, targetContext);
|
|
|
|
|
|
assertTrue("Result code:" + publicKeyResult.getHttpCode(), publicKeyResult.isSuccess());
|
|
assertTrue("Result code:" + publicKeyResult.getHttpCode(), publicKeyResult.isSuccess());
|
|
|
|
|
|
- String publicKeyFromServer = (String) publicKeyResult.getData().get(0);
|
|
|
|
|
|
+ String publicKeyFromServer = publicKeyResult.getResultData();
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
|
|
EncryptionUtils.PUBLIC_KEY,
|
|
EncryptionUtils.PUBLIC_KEY,
|
|
publicKeyFromServer);
|
|
publicKeyFromServer);
|
|
@@ -559,7 +594,9 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
TODO do not c&p code
|
|
TODO do not c&p code
|
|
*/
|
|
*/
|
|
private static void createKeys() throws Exception {
|
|
private static void createKeys() throws Exception {
|
|
- String publicKey;
|
|
|
|
|
|
+ deleteKeys();
|
|
|
|
+
|
|
|
|
+ String publicKeyString;
|
|
|
|
|
|
// Create public/private key pair
|
|
// Create public/private key pair
|
|
KeyPair keyPair = EncryptionUtils.generateKeyPair();
|
|
KeyPair keyPair = EncryptionUtils.generateKeyPair();
|
|
@@ -573,7 +610,18 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
RemoteOperationResult result = operation.execute(account, targetContext);
|
|
RemoteOperationResult result = operation.execute(account, targetContext);
|
|
|
|
|
|
if (result.isSuccess()) {
|
|
if (result.isSuccess()) {
|
|
- publicKey = (String) result.getData().get(0);
|
|
|
|
|
|
+ publicKeyString = (String) result.getData().get(0);
|
|
|
|
+
|
|
|
|
+ // check key
|
|
|
|
+ RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyPair.getPrivate();
|
|
|
|
+ RSAPublicKey publicKey = EncryptionUtils.convertPublicKeyFromString(publicKeyString);
|
|
|
|
+
|
|
|
|
+ BigInteger modulusPublic = publicKey.getModulus();
|
|
|
|
+ BigInteger modulusPrivate = privateKey.getModulus();
|
|
|
|
+
|
|
|
|
+ if (modulusPrivate.compareTo(modulusPublic) != 0) {
|
|
|
|
+ throw new RuntimeException("Wrong CSR returned");
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
throw new Exception("failed to send CSR", result.getException());
|
|
throw new Exception("failed to send CSR", result.getException());
|
|
}
|
|
}
|
|
@@ -591,7 +639,7 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
if (storePrivateKeyResult.isSuccess()) {
|
|
if (storePrivateKeyResult.isSuccess()) {
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PRIVATE_KEY,
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PRIVATE_KEY,
|
|
privateKeyString);
|
|
privateKeyString);
|
|
- arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PUBLIC_KEY, publicKey);
|
|
|
|
|
|
+ arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PUBLIC_KEY, publicKeyString);
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.MNEMONIC,
|
|
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.MNEMONIC,
|
|
generateMnemonicString());
|
|
generateMnemonicString());
|
|
} else {
|
|
} else {
|
|
@@ -599,6 +647,21 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static void deleteKeys() {
|
|
|
|
+ RemoteOperationResult<PrivateKey> privateKeyRemoteOperationResult = new GetPrivateKeyOperation().execute(client);
|
|
|
|
+ RemoteOperationResult<String> publicKeyRemoteOperationResult = new GetPublicKeyOperation().execute(client);
|
|
|
|
+
|
|
|
|
+ if (privateKeyRemoteOperationResult.isSuccess() || publicKeyRemoteOperationResult.isSuccess()) {
|
|
|
|
+ // delete keys
|
|
|
|
+ assertTrue(new DeletePrivateKeyOperation().execute(client).isSuccess());
|
|
|
|
+ assertTrue(new DeletePublicKeyOperation().execute(client).isSuccess());
|
|
|
|
+
|
|
|
|
+ arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PRIVATE_KEY);
|
|
|
|
+ arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PUBLIC_KEY);
|
|
|
|
+ arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.MNEMONIC);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private static String generateMnemonicString() {
|
|
private static String generateMnemonicString() {
|
|
return "1 2 3 4 5 6";
|
|
return "1 2 3 4 5 6";
|
|
}
|
|
}
|