From 3cc3c36b0b3c0913112f3fc5866bb3411372c8c5 Mon Sep 17 00:00:00 2001 From: Dan Mills Date: Tue, 29 Jul 2008 12:04:41 -0700 Subject: [PATCH] get crypto working with FileEngine --- services/sync/modules/engines.js | 10 +++--- services/sync/modules/remote.js | 57 +++++++++++++++++--------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/services/sync/modules/engines.js b/services/sync/modules/engines.js index b871370a8cca..e6f32da2857f 100644 --- a/services/sync/modules/engines.js +++ b/services/sync/modules/engines.js @@ -509,10 +509,10 @@ FileEngine.prototype = { _initialUpload: function FileEngine__initialUpload() { let self = yield; + yield this._keys.initialize(self.cb, this.engineId); this._file.data = {}; yield this._merge.async(this, self.cb); yield this._file.put(self.cb, this._file.data); - // put keychain }, // NOTE: Assumes this._file has latest server data @@ -536,16 +536,16 @@ FileEngine.prototype = { if (!(yield DAV.MKCOL(this.serverPrefix, self.cb))) throw "Could not create remote folder"; - if ("none" != Utils.prefs.getCharPref("encryption")) - yield this._keys.getKeyAndIV(self.cb, this.engineId); - try { + if ("none" != Utils.prefs.getCharPref("encryption")) + yield this._keys.getKeyAndIV(self.cb, this.engineId); yield this._file.get(self.cb); yield this._merge.async(this, self.cb); yield this._file.put(self.cb, this._file.data); + } catch (e if e.status == 404) { - this._initialUpload.async(this, self.cb); this._log.info("Initial upload to server"); + yield this._initialUpload.async(this, self.cb); } this._log.info("Sync complete"); diff --git a/services/sync/modules/remote.js b/services/sync/modules/remote.js index 37532e0a1806..b720930450a0 100644 --- a/services/sync/modules/remote.js +++ b/services/sync/modules/remote.js @@ -350,8 +350,7 @@ CryptoFilter.prototype = { let self = yield; this._log.debug("Encrypting data"); this._os.notifyObservers(null, "weave:service:sync:status", "status.encrypting"); - Crypto.encryptData.async(Crypto, self.cb, data, this._identity); - let ret = yield; + let ret = yield Crypto.encryptData.async(Crypto, self.cb, data, this._identity); self.done(ret); }, @@ -359,8 +358,7 @@ CryptoFilter.prototype = { let self = yield; this._log.debug("Decrypting data"); this._os.notifyObservers(null, "weave:service:sync:status", "status.decrypting"); - Crypto.decryptData.async(Crypto, self.cb, data, this.identity); - let ret = yield; + let ret = yield Crypto.decryptData.async(Crypto, self.cb, data, this._identity); self.done(ret); } }; @@ -374,12 +372,37 @@ Keychain.prototype = { this.__proto__.__proto__._init.call(this, prefix + "keys.json"); this.pushFilter(new JsonFilter()); }, + _initialize: function Keychain__initialize(identity) { + let self = yield; + let wrappedSymkey; + + if ("none" != Utils.prefs.getCharPref("encryption")) { + this._os.notifyObservers(null, "weave:service:sync:status", "status.generating-random-key"); + + yield Crypto.randomKeyGen.async(Crypto, self.cb, identity); + + // Wrap (encrypt) this key with the user's public key. + let idRSA = ID.get('WeaveCryptoID'); + this._os.notifyObservers(null, "weave:service:sync:status", "status.encrypting-key"); + wrappedSymkey = yield Crypto.wrapKey.async(Crypto, self.cb, + identity.bulkKey, idRSA); + } + + let keys = {ring: {}, bulkIV: identity.bulkIV}; + this._os.notifyObservers(null, "weave:service:sync:status", "status.uploading-key"); + keys.ring[identity.username] = wrappedSymkey; + yield this.put(self.cb, keys); + }, + initialize: function Keychain_initialize(onComplete, identity) { + this._initialize.async(this, onComplete, identity); + }, _getKeyAndIV: function Keychain__getKeyAndIV(identity) { let self = yield; this._os.notifyObservers(null, "weave:service:sync:status", "status.downloading-keyring"); - this.get(self.cb); - yield; + + yield this.get(self.cb); + if (!this.data || !this.data.ring || !this.data.ring[identity.username]) throw "Keyring does not contain a key for this user"; @@ -415,7 +438,7 @@ RemoteStore.prototype = { }, get keys() { - let keys = new Keychain(this.serverPrefix); + let keys = new Keychain(this.serverPrefix, this.engineId); this.__defineGetter__("keys", function() keys); return keys; }, @@ -481,26 +504,8 @@ RemoteStore.prototype = { // FIXME: add 'metadata' arg here like appendDelta's _initialize: function RStore__initialize(snapshot) { let self = yield; - let wrappedSymkey; - - if ("none" != Utils.prefs.getCharPref("encryption")) { - this._os.notifyObservers(null, "weave:service:sync:status", "status.generating-random-key"); - - Crypto.randomKeyGen.async(Crypto, self.cb, this.engineId); - yield; - - // Wrap (encrypt) this key with the user's public key. - let idRSA = ID.get('WeaveCryptoID'); - this._os.notifyObservers(null, "weave:service:sync:status", "status.encrypting-key"); - wrappedSymkey = yield Crypto.wrapKey.async(Crypto, self.cb, - this.engineId.bulkKey, idRSA); - } - - let keys = {ring: {}, bulkIV: this.engineId.bulkIV}; - this._os.notifyObservers(null, "weave:service:sync:status", "status.uploading-key"); - keys.ring[this.engineId.username] = wrappedSymkey; - yield this.keys.put(self.cb, keys); + yield this.keys.initialize(self.cb, this.engineId); this._os.notifyObservers(null, "weave:service:sync:status", "status.uploading-snapshot"); yield this._snapshot.put(self.cb, snapshot.data); //yield this._deltas.put(self.cb, []);