use of com.dracoon.sdk.crypto.error.CryptoException in project cyberduck by iterate-ch.
the class SDSSession method unlockTripleCryptKeyPair.
protected void unlockTripleCryptKeyPair(final LoginCallback prompt, final UserAccountWrapper user, final UserKeyPair.Version requiredKeyPairVersion) throws BackgroundException {
try {
Credentials deprecatedCredentials = null;
if (this.isNewCryptoAvailable()) {
final List<UserKeyPairContainer> pairs = new UserApi(client).requestUserKeyPairs(StringUtils.EMPTY, null);
if (pairs.size() == 0) {
if (log.isDebugEnabled()) {
log.debug(String.format("No keypair found for user %s", user));
}
return;
}
boolean migrated = false;
for (UserKeyPairContainer pair : pairs) {
if (requiredKeyPairVersion == TripleCryptConverter.toCryptoUserKeyPair(pair).getUserPublicKey().getVersion()) {
migrated = true;
break;
}
}
if (migrated && pairs.size() == 2) {
final UserKeyPairContainer deprecated = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, UserKeyPair.Version.RSA2048.getValue(), null);
final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(deprecated);
if (log.isDebugEnabled()) {
log.debug(String.format("Attempt to unlock deprecated private key %s", keypair.getUserPrivateKey()));
}
deprecatedCredentials = new TripleCryptKeyPair().unlock(prompt, host, keypair);
keyPairDeprecated.set(deprecated);
}
if (!migrated) {
final UserKeyPairContainer deprecated = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, UserKeyPair.Version.RSA2048.getValue(), null);
final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(deprecated);
if (log.isDebugEnabled()) {
log.debug(String.format("Attempt to unlock and migrate deprecated private key %s", keypair.getUserPrivateKey()));
}
deprecatedCredentials = new TripleCryptKeyPair().unlock(prompt, host, keypair);
final UserKeyPair newPair = Crypto.generateUserKeyPair(requiredKeyPairVersion, deprecatedCredentials.getPassword());
final CreateKeyPairRequest request = new CreateKeyPairRequest();
request.setPreviousPrivateKey(deprecated.getPrivateKeyContainer());
final UserKeyPairContainer userKeyPairContainer = TripleCryptConverter.toSwaggerUserKeyPairContainer(newPair);
request.setPrivateKeyContainer(userKeyPairContainer.getPrivateKeyContainer());
request.setPublicKeyContainer(userKeyPairContainer.getPublicKeyContainer());
if (log.isDebugEnabled()) {
log.debug("Create new key pair");
}
new UserApi(client).createAndPreserveUserKeyPair(request, null);
keyPairDeprecated.set(deprecated);
}
}
final UserKeyPairContainer container = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, requiredKeyPairVersion.getValue(), null);
keyPair.set(container);
final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(keyPair.get());
if (deprecatedCredentials != null) {
if (log.isDebugEnabled()) {
log.debug(String.format("Attempt to unlock private key with passphrase from deprecated private key %s", keypair.getUserPrivateKey()));
}
if (Crypto.checkUserKeyPair(keypair, deprecatedCredentials.getPassword())) {
new TripleCryptKeyPair().unlock(prompt, host, keypair, deprecatedCredentials.getPassword());
} else {
new TripleCryptKeyPair().unlock(prompt, host, keypair);
}
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("Attempt to unlock private key %s", keypair.getUserPrivateKey()));
}
new TripleCryptKeyPair().unlock(prompt, host, keypair);
}
} catch (CryptoException e) {
throw new TripleCryptExceptionMappingService().map(e);
} catch (ApiException e) {
log.warn(String.format("Ignore failure unlocking user key pair. %s", new SDSExceptionMappingService(nodeid).map(e)));
} catch (LoginCanceledException e) {
log.warn("Ignore cancel unlocking triple crypt private key pair");
}
}
Aggregations