|
15 | 15 | import java.nio.file.Files; |
16 | 16 | import java.nio.file.Path; |
17 | 17 | import java.nio.file.Paths; |
| 18 | +import java.security.NoSuchAlgorithmException; |
| 19 | +import java.security.SecureRandom; |
18 | 20 | import java.util.ArrayList; |
19 | 21 | import java.util.Optional; |
20 | 22 | import java.util.Set; |
21 | 23 |
|
| 24 | +import com.google.common.base.Preconditions; |
22 | 25 | import org.apache.commons.cli.ParseException; |
23 | 26 | import org.cryptomator.cryptofs.CryptoFileSystemProperties; |
24 | 27 | import org.cryptomator.cryptofs.CryptoFileSystemProvider; |
| 28 | +import org.cryptomator.cryptolib.common.MasterkeyFileAccess; |
25 | 29 | import org.slf4j.Logger; |
26 | 30 | import org.slf4j.LoggerFactory; |
27 | 31 |
|
28 | 32 | public class CryptomatorCli { |
29 | 33 |
|
30 | 34 | private static final Logger LOG = LoggerFactory.getLogger(CryptomatorCli.class); |
31 | 35 |
|
| 36 | + private static final byte[] PEPPER = new byte[0]; |
| 37 | + private static final String SCHEME = "masterkeyfile"; |
| 38 | + |
32 | 39 | public static void main(String[] rawArgs) throws IOException { |
33 | 40 | try { |
34 | 41 | Args args = Args.parse(rawArgs); |
@@ -71,12 +78,26 @@ private static void startup(Args args) throws IOException { |
71 | 78 | Optional<WebDav> server = initWebDavServer(args); |
72 | 79 | ArrayList<FuseMount> mounts = new ArrayList<>(); |
73 | 80 |
|
| 81 | + SecureRandom secureRandom; |
| 82 | + try { |
| 83 | + secureRandom = SecureRandom.getInstanceStrong(); |
| 84 | + } catch (NoSuchAlgorithmException e) { |
| 85 | + throw new IllegalStateException("A strong algorithm must exist in every Java platform.", e); |
| 86 | + } |
| 87 | + MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(PEPPER, secureRandom); |
| 88 | + |
74 | 89 | for (String vaultName : args.getVaultNames()) { |
75 | 90 | Path vaultPath = Paths.get(args.getVaultPath(vaultName)); |
76 | 91 | LOG.info("Unlocking vault \"{}\" located at {}", vaultName, vaultPath); |
77 | 92 | String vaultPassword = args.getPasswordStrategy(vaultName).password(); |
78 | 93 | CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties() |
79 | | - .withPassphrase(vaultPassword).build(); |
| 94 | + .withKeyLoader(keyId -> { |
| 95 | + Preconditions.checkArgument(SCHEME.equalsIgnoreCase(keyId.getScheme()), "Only supports keys with scheme " + SCHEME); |
| 96 | + Path keyFilePath = vaultPath.resolve(keyId.getSchemeSpecificPart()); |
| 97 | + return masterkeyFileAccess.load(keyFilePath, vaultPassword); |
| 98 | + }) |
| 99 | + .build(); |
| 100 | + |
80 | 101 | Path vaultRoot = CryptoFileSystemProvider.newFileSystem(vaultPath, properties).getPath("/"); |
81 | 102 |
|
82 | 103 | Path fuseMountPoint = args.getFuseMountPoint(vaultName); |
|
0 commit comments