|
| 1 | +package org.cryptomator.cli; |
| 2 | + |
| 3 | +import org.cryptomator.cryptofs.CryptoFileSystemProperties; |
| 4 | +import org.cryptomator.cryptofs.CryptoFileSystemProvider; |
| 5 | +import org.cryptomator.cryptolib.api.CryptoException; |
| 6 | +import org.cryptomator.cryptolib.api.CryptorProvider; |
| 7 | +import org.cryptomator.cryptolib.api.Masterkey; |
| 8 | +import org.cryptomator.cryptolib.common.MasterkeyFileAccess; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import picocli.CommandLine.ArgGroup; |
| 13 | +import picocli.CommandLine.Command; |
| 14 | +import picocli.CommandLine.Mixin; |
| 15 | +import picocli.CommandLine.Model; |
| 16 | +import picocli.CommandLine.Parameters; |
| 17 | +import picocli.CommandLine.Spec; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URI; |
| 21 | +import java.nio.CharBuffer; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.security.SecureRandom; |
| 25 | +import java.util.concurrent.Callable; |
| 26 | + |
| 27 | +@Command( |
| 28 | + name = "create", |
| 29 | + header = "Creates a vault", |
| 30 | + description = "Creates a new cryptomator vault at the specified path.", |
| 31 | + parameterListHeading = "%nParameters:%n", |
| 32 | + headerHeading = "Usage:%n%n", |
| 33 | + synopsisHeading = "%n", |
| 34 | + descriptionHeading = "%nDescription:%n%n", |
| 35 | + optionListHeading = "%nOptions:%n", |
| 36 | + mixinStandardHelpOptions = true) |
| 37 | +public class Create implements Callable<Integer> { |
| 38 | + |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(Create.class); |
| 40 | + private static final byte[] PEPPER = new byte[0]; |
| 41 | + private static final String MASTERKEY_FILE_NAME = "masterkey.cryptomator"; |
| 42 | + private static final URI DEFAULT_KEY_ID = URI.create("masterkeyfile:" + MASTERKEY_FILE_NAME); |
| 43 | + |
| 44 | + @Spec Model.CommandSpec spec; |
| 45 | + @Mixin LoggingMixin loggingMixin; |
| 46 | + |
| 47 | + @Parameters( |
| 48 | + index = "0", |
| 49 | + paramLabel = "/path/to/vaultDirectory", |
| 50 | + description = "Path to the vault directory") |
| 51 | + Path pathToVault; |
| 52 | + |
| 53 | + @ArgGroup(multiplicity = "1") |
| 54 | + PasswordSource passwordSource; |
| 55 | + |
| 56 | + private SecureRandom csprng = null; |
| 57 | + |
| 58 | + @Override |
| 59 | + public Integer call() throws Exception { |
| 60 | + csprng = SecureRandom.getInstanceStrong(); |
| 61 | + |
| 62 | + try (var passphraseContainer = passwordSource.readPassphrase()) { |
| 63 | + passwordSource.confirmPassphrase(); |
| 64 | + |
| 65 | + // Throw exception if there's something already there. |
| 66 | + Files.createDirectory(pathToVault); |
| 67 | + |
| 68 | + try (var masterkey = Masterkey.generate(csprng)) { |
| 69 | + persistMasterkey(pathToVault, masterkey, passphraseContainer.content()); |
| 70 | + initializeVault(pathToVault, masterkey); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + LOG.info("Vault created successfully in {}", pathToVault); |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + private void persistMasterkey(Path path, Masterkey masterkey, char[] passphrase) |
| 79 | + throws IOException { |
| 80 | + Path masterkeyFilePath = path.resolve(MASTERKEY_FILE_NAME); |
| 81 | + MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(PEPPER, csprng); |
| 82 | + masterkeyFileAccess.persist(masterkey, masterkeyFilePath, CharBuffer.wrap(passphrase)); |
| 83 | + } |
| 84 | + |
| 85 | + private void initializeVault(Path path, Masterkey masterkey) throws IOException { |
| 86 | + CryptoFileSystemProperties fsProps = |
| 87 | + CryptoFileSystemProperties.cryptoFileSystemProperties() |
| 88 | + .withCipherCombo(CryptorProvider.Scheme.SIV_GCM) |
| 89 | + .withKeyLoader(ignored -> masterkey.copy()) |
| 90 | + .build(); |
| 91 | + try { |
| 92 | + CryptoFileSystemProvider.initialize(path, fsProps, DEFAULT_KEY_ID); |
| 93 | + } catch (CryptoException e) { |
| 94 | + throw new IOException("Vault initialization failed", e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments