- java.lang.Object
-
- org.cryptomator.cryptolib.common.MasterkeyFileAccess
-
public class MasterkeyFileAccess extends Object
Allow loading and persisting ofmasterkeysfrom and to encrypted json files.Requires a passphrase for derivation of a KEK.
MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(pepper, csprng); try (Masterkey masterkey = masterkeyFileAccess.load(path, passphrase) { // use masterkey }
-
-
Constructor Summary
Constructors Constructor Description MasterkeyFileAccess(byte[] pepper, SecureRandom csprng)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description byte[]changePassphrase(byte[] masterkey, CharSequence oldPassphrase, CharSequence newPassphrase)Reencrypts a masterkey with a new passphrase.voidchangePassphrase(InputStream oldIn, OutputStream newOut, CharSequence oldPassphrase, CharSequence newPassphrase)Masterkeyload(InputStream in, CharSequence passphrase)Masterkeyload(Path filePath, CharSequence passphrase)Loads the JSON contents from the given file and derives a KEK from the given passphrase to unwrap the contained keys.voidpersist(Masterkey masterkey, OutputStream out, CharSequence passphrase, int vaultVersion)voidpersist(Masterkey masterkey, Path filePath, CharSequence passphrase)Derives a KEK from the given passphrase and wraps the key material frommasterkey.voidpersist(Masterkey masterkey, Path filePath, CharSequence passphrase, int vaultVersion)static intreadAllegedVaultVersion(byte[] masterkey)Deprecated.Starting with vault format 8, the vault version is no longer stored inside the masterkey file.
-
-
-
Constructor Detail
-
MasterkeyFileAccess
public MasterkeyFileAccess(byte[] pepper, SecureRandom csprng)
-
-
Method Detail
-
readAllegedVaultVersion
@Deprecated public static int readAllegedVaultVersion(byte[] masterkey) throws IOException
Deprecated.Starting with vault format 8, the vault version is no longer stored inside the masterkey file.Parses the given masterkey file contents and returns the alleged vault version without verifying the version MAC.- Parameters:
masterkey- The file contents of a masterkey file.- Returns:
- The (unverified) vault version
- Throws:
IOException- In case of errors, such as unparseable JSON.
-
changePassphrase
public byte[] changePassphrase(byte[] masterkey, CharSequence oldPassphrase, CharSequence newPassphrase) throws IOException, InvalidPassphraseExceptionReencrypts a masterkey with a new passphrase.- Parameters:
masterkey- The original JSON representation of the masterkeyoldPassphrase- The old passphrasenewPassphrase- The new passphrase- Returns:
- A JSON representation of the masterkey, now encrypted with
newPassphrase - Throws:
IOException- If failing to read, parse or write JSONInvalidPassphraseException- If the wrongoldPassphrasehas been supplied for themasterkey
-
changePassphrase
public void changePassphrase(InputStream oldIn, OutputStream newOut, CharSequence oldPassphrase, CharSequence newPassphrase) throws IOException, InvalidPassphraseException
-
load
public Masterkey load(Path filePath, CharSequence passphrase) throws MasterkeyLoadingFailedException
Loads the JSON contents from the given file and derives a KEK from the given passphrase to unwrap the contained keys.- Parameters:
filePath- Which file to loadpassphrase- The passphrase used during key derivation- Returns:
- A new masterkey. Should be used in a try-with-resource statement.
- Throws:
InvalidPassphraseException- If the provided passphrase can not be used to unwrap the stored keys.MasterkeyLoadingFailedException- If reading the masterkey file fails
-
load
public Masterkey load(InputStream in, CharSequence passphrase) throws IOException
- Throws:
IOException
-
persist
public void persist(Masterkey masterkey, Path filePath, CharSequence passphrase) throws IOException
Derives a KEK from the given passphrase and wraps the key material frommasterkey. Then serializes the encrypted keys as well as used key derivation parameters into a JSON representation that will be stored at the given filePath.- Parameters:
masterkey- The key to protectfilePath- Where to store the file (gets overwritten, parent dir must exist)passphrase- The passphrase used during key derivation- Throws:
IOException- When unable to write to the given file
-
persist
public void persist(Masterkey masterkey, Path filePath, CharSequence passphrase, @Deprecated int vaultVersion) throws IOException
- Throws:
IOException
-
persist
public void persist(Masterkey masterkey, OutputStream out, CharSequence passphrase, @Deprecated int vaultVersion) throws IOException
- Throws:
IOException
-
-