Provide SecureRandom as parameter for better testing and code dedup

This commit is contained in:
Julian Raufelder 2021-04-08 21:57:16 +02:00
parent 9c0067b7e2
commit cf5bf19c98
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D
2 changed files with 12 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import org.cryptomator.domain.usecases.vault.UnlockToken;
import org.cryptomator.util.Optional; import org.cryptomator.util.Optional;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.security.SecureRandom;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -27,6 +28,7 @@ public class CryptoCloudFactory {
private final CloudContentRepository cloudContentRepository; private final CloudContentRepository cloudContentRepository;
private final CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory; private final CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory;
private final SecureRandom secureRandom = new SecureRandom();
@Inject @Inject
public CryptoCloudFactory(CloudContentRepository cloudContentRepository, // public CryptoCloudFactory(CloudContentRepository cloudContentRepository, //
@ -84,12 +86,12 @@ public class CryptoCloudFactory {
if (unverifiedVaultConfigOptional.isPresent()) { if (unverifiedVaultConfigOptional.isPresent()) {
switch (unverifiedVaultConfigOptional.get().getKeyId().getScheme()) { switch (unverifiedVaultConfigOptional.get().getKeyId().getScheme()) {
case MASTERKEY_SCHEME: { case MASTERKEY_SCHEME: {
return new MasterkeyCryptoCloudProvider(cloudContentRepository, cryptoCloudContentRepositoryFactory); return new MasterkeyCryptoCloudProvider(cloudContentRepository, cryptoCloudContentRepositoryFactory, secureRandom);
} }
default: throw new IllegalStateException(String.format("Provider with scheme %s not supported", unverifiedVaultConfigOptional.get().getKeyId().getScheme())); default: throw new IllegalStateException(String.format("Provider with scheme %s not supported", unverifiedVaultConfigOptional.get().getKeyId().getScheme()));
} }
} else { } else {
return new MasterkeyCryptoCloudProvider(cloudContentRepository, cryptoCloudContentRepositoryFactory); return new MasterkeyCryptoCloudProvider(cloudContentRepository, cryptoCloudContentRepositoryFactory, secureRandom);
} }
} }
} }

View File

@ -49,19 +49,22 @@ public class MasterkeyCryptoCloudProvider implements CryptoCloudProvider {
private final CloudContentRepository cloudContentRepository; private final CloudContentRepository cloudContentRepository;
private final CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory; private final CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory;
private final SecureRandom secureRandom;
public MasterkeyCryptoCloudProvider(CloudContentRepository cloudContentRepository, // public MasterkeyCryptoCloudProvider(CloudContentRepository cloudContentRepository, //
CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory) { CryptoCloudContentRepositoryFactory cryptoCloudContentRepositoryFactory,
SecureRandom secureRandom) {
this.cloudContentRepository = cloudContentRepository; this.cloudContentRepository = cloudContentRepository;
this.cryptoCloudContentRepositoryFactory = cryptoCloudContentRepositoryFactory; this.cryptoCloudContentRepositoryFactory = cryptoCloudContentRepositoryFactory;
this.secureRandom = secureRandom;
} }
@Override @Override
public void create(CloudFolder location, CharSequence password) throws BackendException { public void create(CloudFolder location, CharSequence password) throws BackendException {
// 1. write masterkey: // 1. write masterkey:
Masterkey masterkey = Masterkey.generate(new SecureRandom()); Masterkey masterkey = Masterkey.generate(secureRandom);
try (ByteArrayOutputStream data = new ByteArrayOutputStream()) { try (ByteArrayOutputStream data = new ByteArrayOutputStream()) {
new MasterkeyFileAccess(PEPPER, new SecureRandom()).persist(masterkey, data, password, DEFAULT_MASTERKEY_FILE_VERSION); new MasterkeyFileAccess(PEPPER, secureRandom).persist(masterkey, data, password, DEFAULT_MASTERKEY_FILE_VERSION);
cloudContentRepository.write(legacyMasterkeyFile(location), ByteArrayDataSource.from(data.toByteArray()), NO_OP_PROGRESS_AWARE, false, data.size()); cloudContentRepository.write(legacyMasterkeyFile(location), ByteArrayDataSource.from(data.toByteArray()), NO_OP_PROGRESS_AWARE, false, data.size());
} catch (IOException e) { } catch (IOException e) {
throw new FatalBackendException("Failed to write masterkey", e); throw new FatalBackendException("Failed to write masterkey", e);
@ -174,7 +177,7 @@ public class MasterkeyCryptoCloudProvider implements CryptoCloudProvider {
} }
private Cryptor cryptorFor(Masterkey keyFile, VaultCipherCombo vaultCipherCombo) { private Cryptor cryptorFor(Masterkey keyFile, VaultCipherCombo vaultCipherCombo) {
return vaultCipherCombo.getCryptorProvider(new SecureRandom()).withKey(keyFile); return vaultCipherCombo.getCryptorProvider(secureRandom).withKey(keyFile);
} }
@Override @Override
@ -269,7 +272,7 @@ public class MasterkeyCryptoCloudProvider implements CryptoCloudProvider {
private void createNewMasterKeyFile(byte[] data, int vaultVersion, String oldPassword, String newPassword, CloudFile masterkeyFile) throws BackendException { private void createNewMasterKeyFile(byte[] data, int vaultVersion, String oldPassword, String newPassword, CloudFile masterkeyFile) throws BackendException {
try { try {
byte[] newMasterKeyFile = new MasterkeyFileAccess(PEPPER, new SecureRandom()) // byte[] newMasterKeyFile = new MasterkeyFileAccess(PEPPER, secureRandom) //
.changePassphrase(data, normalizePassword(oldPassword, vaultVersion), normalizePassword(newPassword, vaultVersion)); .changePassphrase(data, normalizePassword(oldPassword, vaultVersion), normalizePassword(newPassword, vaultVersion));
cloudContentRepository.write(masterkeyFile, // cloudContentRepository.write(masterkeyFile, //
ByteArrayDataSource.from(newMasterKeyFile), // ByteArrayDataSource.from(newMasterKeyFile), //