Search in sources :

Example 1 with MasterkeyFile

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;
}
Also used : VaultException(ch.cyberduck.core.vault.VaultException) MasterkeyFileAccess(org.cryptomator.cryptolib.common.MasterkeyFileAccess) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Algorithm(com.auth0.jwt.algorithms.Algorithm) DefaultUrlProvider(ch.cyberduck.core.shared.DefaultUrlProvider) Masterkey(org.cryptomator.cryptolib.api.Masterkey) StringReader(java.io.StringReader) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) MasterkeyFile(org.cryptomator.cryptolib.common.MasterkeyFile) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException)

Example 2 with MasterkeyFile

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;
}
Also used : VaultException(ch.cyberduck.core.vault.VaultException) DefaultUrlProvider(ch.cyberduck.core.shared.DefaultUrlProvider) MasterkeyFile(org.cryptomator.cryptolib.common.MasterkeyFile) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Aggregations

DefaultUrlProvider (ch.cyberduck.core.shared.DefaultUrlProvider)2 VaultException (ch.cyberduck.core.vault.VaultException)2 IOException (java.io.IOException)2 MasterkeyFile (org.cryptomator.cryptolib.common.MasterkeyFile)2 LocalAccessDeniedException (ch.cyberduck.core.exception.LocalAccessDeniedException)1 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 JsonParseException (com.google.gson.JsonParseException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringReader (java.io.StringReader)1 Masterkey (org.cryptomator.cryptolib.api.Masterkey)1 MasterkeyFileAccess (org.cryptomator.cryptolib.common.MasterkeyFileAccess)1