88import ch .cyberduck .core .AbstractPath ;
99import ch .cyberduck .core .DisabledListProgressListener ;
1010import ch .cyberduck .core .ListService ;
11- import ch .cyberduck .core .LoginOptions ;
12- import ch .cyberduck .core .PasswordCallback ;
1311import ch .cyberduck .core .Path ;
1412import ch .cyberduck .core .Session ;
1513import ch .cyberduck .core .cryptomator .ContentWriter ;
16- import ch .cyberduck .core .cryptomator .CryptoVault ;
14+ import ch .cyberduck .core .cryptomator .UVFVault ;
1715import ch .cyberduck .core .exception .BackgroundException ;
1816import ch .cyberduck .core .features .Directory ;
1917import ch .cyberduck .core .preferences .PreferencesFactory ;
2220
2321import org .apache .logging .log4j .LogManager ;
2422import org .apache .logging .log4j .Logger ;
25- import org .cryptomator .cryptolib .api .CryptorProvider ;
26- import org .cryptomator .cryptolib .api .Masterkey ;
23+ import org .cryptomator .cryptolib .api .UVFMasterkey ;
2724
2825import java .nio .charset .StandardCharsets ;
29- import java .util .Base64 ;
3026import java .util .EnumSet ;
3127
3228/**
3329 * Cryptomator vault implementation for Cipherduck (without masterkey file).
3430 */
35- public class HubCryptoVault extends CryptoVault {
31+ public class HubCryptoVault extends UVFVault {
3632 private static final Logger log = LogManager .getLogger (HubCryptoVault .class );
33+ private final String decryptedPayload ;
3734
38- // See https://github.com/cryptomator/hub/blob/develop/frontend/src/common/vaultconfig.ts
39- //const jwtPayload: VaultConfigPayload = {
40- // jti: vaultId,
41- // format: 8,
42- // cipherCombo: 'SIV_GCM',
43- // shorteningThreshold: 220
44- //};
45- //const header = JSON.stringify({
46- // kid: kid,
47- // typ: 'jwt',
48- // alg: 'HS256',
49- // hub: hubConfig
50- //});
51- private static final VaultConfig VAULT_CONFIG = new VaultConfig (8 , 220 , CryptorProvider .Scheme .SIV_GCM , "HS256" , null );
5235
5336 public HubCryptoVault (final Path home ) {
54- super (home );
37+ this (home , null , null , null ); // TODO cleanup
5538 }
5639
57- public HubCryptoVault (final Path home , final String masterkey , final String config , final byte [] pepper ) {
58- super (home );
40+ public HubCryptoVault (final Path home , final String decryptedPayload , final String config , final byte [] pepper ) {
41+ super (home , decryptedPayload , config , pepper );
42+ this .decryptedPayload = decryptedPayload ;
5943 }
6044
45+ public Path encrypt (Session <?> session , Path file , byte [] directoryId , boolean metadata ) throws BackgroundException {
46+ log .debug ("HubCryptoVault.encrypt. Use directory ID '{}' for folder {}" , directoryId , file );
47+ return super .encrypt (session , file , directoryId , metadata );
48+ }
49+
50+
51+ @ Override
52+ public Path getHome () {
53+ final Path home = super .getHome ();
54+ final UVFMasterkey masterKey = UVFMasterkey .fromDecryptedPayload (this .decryptedPayload );
55+ byte [] directoryId = masterKey .rootDirId ();
56+ assert directoryId != null ;
57+ home .attributes ().setDirectoryId (directoryId );
58+ return home ;
59+ }
60+
61+
6162 /**
6263 * Upload vault template into existing bucket (permanent credentials)
6364 */
6465 // TODO https://github.com/shift7-ch/cipherduck-hub/issues/19 review @dko check method signature?
65- public synchronized Path create (final Session <?> session , final String region , final VaultCredentials credentials , final int version , final String metadata , final String rootDirHash ) throws BackgroundException {
66+ public synchronized Path create (final Session <?> session , final String region , final VaultCredentials credentials , final int version , final String metadata , final String hashedRootDirId ) throws BackgroundException {
6667 final Path home = new Path (session .getHost ().getDefaultPath (), EnumSet .of (AbstractPath .Type .directory ));
6768 log .debug ("Uploading vault template {} in {} " , home , session .getHost ());
6869
@@ -75,11 +76,11 @@ public synchronized Path create(final Session<?> session, final String region, f
7576 // zip.file('vault.cryptomator', this.vaultConfigToken);
7677 // zip.folder('d')?.folder(this.rootDirHash.substring(0, 2))?.folder(this.rootDirHash.substring(2));
7778 (new ContentWriter (session )).write (new Path (home , PreferencesFactory .get ().getProperty ("cryptomator.vault.config.filename" ), EnumSet .of (AbstractPath .Type .file , AbstractPath .Type .vault )), metadata .getBytes (StandardCharsets .US_ASCII ));
78- Directory <?> directory = (Directory ) session ._getFeature (Directory .class );
79+ Directory <?> directory = (Directory <?> ) session ._getFeature (Directory .class );
7980
8081 // TODO https://github.com/shift7-ch/cipherduck-hub/issues/19 implement CryptoDirectory for uvf
8182 // Path secondLevel = this.directoryProvider.toEncrypted(session, this.home.attributes().getDirectoryId(), this.home);
82- final Path secondLevel = new Path (String .format ("/%s/d/%s/%s/" , session .getHost ().getDefaultPath (), rootDirHash .substring (0 , 2 ), rootDirHash .substring (2 )), EnumSet .of (AbstractPath .Type .directory ));
83+ final Path secondLevel = new Path (String .format ("/%s/d/%s/%s/" , session .getHost ().getDefaultPath (), hashedRootDirId .substring (0 , 2 ), hashedRootDirId .substring (2 )), EnumSet .of (AbstractPath .Type .directory ));
8384 final Path firstLevel = secondLevel .getParent ();
8485 final Path dataDir = firstLevel .getParent ();
8586 log .debug ("Create vault root directory at {}" , secondLevel );
@@ -91,19 +92,6 @@ public synchronized Path create(final Session<?> session, final String region, f
9192 return home ;
9293 }
9394
94- @ Override
95- public HubCryptoVault load (final Session <?> session , final PasswordCallback prompt ) throws BackgroundException {
96- // no-interactive prompt in Cipherduck
97- final String masterkey = prompt .prompt (session .getHost (), "" , "" , new LoginOptions ()).getPassword ();
98- try {
99- this .open (VAULT_CONFIG , new Masterkey (Base64 .getDecoder ().decode (masterkey )));
100- }
101- catch (IllegalArgumentException e ) {
102- throw new BackgroundException (e );
103- }
104- return this ;
105- }
106-
10795 public Path getMasterkey () {
10896 // No master key in vault
10997 return null ;
0 commit comments