use of org.cryptomator.cryptolib.common.MasterkeyFile in project cyberduck by iterate-ch.
the class CryptoVault method create.
public synchronized Path create(final Session<?> session, final VaultCredentials credentials, final PasswordStore keychain, final int version) throws BackgroundException {
final Host bookmark = session.getHost();
if (credentials.isSaved()) {
try {
keychain.addPassword(String.format("Cryptomator Passphrase (%s)", bookmark.getCredentials().getUsername()), new DefaultUrlProvider(bookmark).toUrl(masterkey).find(DescriptiveUrl.Type.provider).getUrl(), credentials.getPassword());
} catch (LocalAccessDeniedException e) {
log.error(String.format("Failure %s saving credentials for %s in password store", e, bookmark));
}
}
final String passphrase = credentials.getPassword();
final ByteArrayOutputStream mkArray = new ByteArrayOutputStream();
final Masterkey mk = Masterkey.generate(FastSecureRandomProvider.get().provide());
final MasterkeyFileAccess access = new MasterkeyFileAccess(pepper, FastSecureRandomProvider.get().provide());
final MasterkeyFile masterkeyFile;
try {
access.persist(mk, mkArray, passphrase, version);
masterkeyFile = MasterkeyFile.read(new StringReader(new String(mkArray.toByteArray(), StandardCharsets.UTF_8)));
} catch (IOException e) {
throw new VaultException("Failure creating master key", e);
}
if (log.isDebugEnabled()) {
log.debug(String.format("Write master key to %s", masterkey));
}
// Obtain non encrypted directory writer
final Directory directory = session._getFeature(Directory.class);
final TransferStatus status = new TransferStatus();
final Encryption encryption = session.getFeature(Encryption.class);
if (encryption != null) {
status.setEncryption(encryption.getDefault(home));
}
final Path vault = directory.mkdir(home, status);
new ContentWriter(session).write(masterkey, mkArray.toByteArray());
if (VAULT_VERSION == version) {
// Create vaultconfig.cryptomator
final Algorithm algorithm = Algorithm.HMAC256(mk.getEncoded());
final String conf = JWT.create().withJWTId(new UUIDRandomStringService().random()).withKeyId(String.format("masterkeyfile:%s", masterkey.getName())).withClaim("format", version).withClaim("cipherCombo", CryptorProvider.Scheme.SIV_CTRMAC.toString()).withClaim("shorteningThreshold", CryptoFilenameV7Provider.NAME_SHORTENING_THRESHOLD).sign(algorithm);
new ContentWriter(session).write(config, conf.getBytes(StandardCharsets.US_ASCII));
}
this.open(masterkeyFile, passphrase);
final Path secondLevel = directoryProvider.toEncrypted(session, home.attributes().getDirectoryId(), home);
final Path firstLevel = secondLevel.getParent();
final Path dataDir = firstLevel.getParent();
if (log.isDebugEnabled()) {
log.debug(String.format("Create vault root directory at %s", secondLevel));
}
directory.mkdir(dataDir, status);
directory.mkdir(firstLevel, status);
directory.mkdir(secondLevel, status);
return vault;
}
use of org.cryptomator.cryptolib.common.MasterkeyFile in project cyberduck by iterate-ch.
the class CryptoVault method load.
@Override
public synchronized CryptoVault load(final Session<?> session, final PasswordCallback prompt, final PasswordStore keychain) throws BackgroundException {
if (this.isUnlocked()) {
log.warn(String.format("Skip unlock of open vault %s", this));
return this;
}
if (log.isDebugEnabled()) {
log.debug(String.format("Attempt to read master key from %s", masterkey));
}
if (log.isDebugEnabled()) {
log.debug(String.format("Read master key %s", masterkey));
}
final Host bookmark = session.getHost();
String passphrase = keychain.getPassword(String.format("Cryptomator Passphrase (%s)", bookmark.getCredentials().getUsername()), new DefaultUrlProvider(bookmark).toUrl(masterkey).find(DescriptiveUrl.Type.provider).getUrl());
if (null == passphrase) {
// Legacy
passphrase = keychain.getPassword(String.format("Cryptomator Passphrase %s", bookmark.getHostname()), new DefaultUrlProvider(bookmark).toUrl(masterkey).find(DescriptiveUrl.Type.provider).getUrl());
}
final MasterkeyFile mkFile;
try {
mkFile = MasterkeyFile.read(new ContentReader(session).getReader(masterkey));
} catch (JsonParseException | IllegalArgumentException | IllegalStateException | IOException e) {
throw new VaultException(String.format("Failure reading vault master key file %s", masterkey.getName()), e);
}
this.unlock(session, mkFile, passphrase, bookmark, prompt, MessageFormat.format(LocaleFactory.localizedString("Provide your passphrase to unlock the Cryptomator Vault {0}", "Cryptomator"), home.getName()), keychain);
return this;
}
Aggregations