Skip to content

Commit b1b42dd

Browse files
committed
Confirm passphrase if the user has typed it
1 parent 978438a commit b1b42dd

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

src/main/java/org/cryptomator/cli/Create.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,20 @@ public class Create implements Callable<Integer> {
5959
public Integer call() throws Exception {
6060
csprng = SecureRandom.getInstanceStrong();
6161

62-
createVault(pathToVault);
62+
try (var passphraseContainer = passwordSource.readPassphrase()) {
63+
passwordSource.confirmPassphrase();
6364

64-
LOG.info("Vault created successfully in {}", pathToVault);
65-
return 0;
66-
}
67-
68-
private void createVault(Path path) throws IOException {
69-
// Throw exception if there's something already there.
70-
Files.createDirectory(path);
65+
// Throw exception if there's something already there.
66+
Files.createDirectory(pathToVault);
7167

72-
try (var passphraseContainer = passwordSource.readPassphrase();
73-
var masterkey = Masterkey.generate(csprng)) {
74-
75-
persistMasterkey(path, masterkey, passphraseContainer.content());
76-
initializeVault(path, masterkey);
68+
try (var masterkey = Masterkey.generate(csprng)) {
69+
persistMasterkey(pathToVault, masterkey, passphraseContainer.content());
70+
initializeVault(pathToVault, masterkey);
71+
}
7772
}
73+
74+
LOG.info("Vault created successfully in {}", pathToVault);
75+
return 0;
7876
}
7977

8078
private void persistMasterkey(Path path, Masterkey masterkey, char[] passphrase)

src/main/java/org/cryptomator/cli/PasswordSource.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ Passphrase readPassphrase() throws IOException {
3838
throw new IllegalStateException("Passphrase source not specified, but required.");
3939
}
4040

41+
void confirmPassphrase() throws IOException {
42+
// Don't confirm the passphrase if stdin is piped.
43+
if (passphraseStdin == null || System.console() == null) {
44+
return;
45+
}
46+
char[] confirmationInput = System.console().readPassword("Confirm passphrase: ");
47+
if (confirmationInput == null) {
48+
throw new PassphraseNotConfirmedException("Passphrase confirmation failed (EOF reached).");
49+
}
50+
try (Passphrase confirmationPassphrase = new Passphrase(confirmationInput)) {
51+
System.out.println("\n");
52+
if (!Arrays.equals(passphraseStdin, confirmationPassphrase.content)) {
53+
throw new PassphraseNotConfirmedException("Passphrase does not match. Please try again.");
54+
}
55+
}
56+
}
57+
4158
private Passphrase readPassphraseFromEnvironment() {
4259
LOG.debug("Reading passphrase from env variable '{}'", passphraseEnvironmentVariable);
4360
var tmp = System.getenv(passphraseEnvironmentVariable);
@@ -106,6 +123,12 @@ static class ReadingEnvironmentVariableFailedException extends PasswordSourceExc
106123
}
107124
}
108125

126+
static class PassphraseNotConfirmedException extends PasswordSourceException {
127+
PassphraseNotConfirmedException(String msg) {
128+
super(msg);
129+
}
130+
}
131+
109132
record Passphrase(char[] content) implements AutoCloseable {
110133

111134
@Override

0 commit comments

Comments
 (0)