2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2012-09-14 23:02:32 +00:00
|
|
|
"WBORecord",
|
|
|
|
"RecordManager",
|
|
|
|
"CryptoWrapper",
|
2012-09-14 23:02:33 +00:00
|
|
|
"CollectionKeyManager",
|
2012-09-14 23:02:32 +00:00
|
|
|
"Collection",
|
|
|
|
];
|
2008-11-03 22:37:51 +00:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
const CRYPTO_COLLECTION = "crypto";
|
|
|
|
const KEYS_WBO = "keys";
|
|
|
|
|
2012-09-14 23:02:32 +00:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2010-11-30 00:41:17 +00:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2012-03-22 22:49:50 +00:00
|
|
|
Cu.import("resource://services-sync/keys.js");
|
2011-01-19 00:23:30 +00:00
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.WBORecord = function WBORecord(collection, id) {
|
2011-01-19 00:23:30 +00:00
|
|
|
this.data = {};
|
|
|
|
this.payload = {};
|
|
|
|
this.collection = collection; // Optional.
|
|
|
|
this.id = id; // Optional.
|
|
|
|
}
|
|
|
|
WBORecord.prototype = {
|
2011-06-13 18:42:18 +00:00
|
|
|
_logName: "Sync.Record.WBO",
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
get sortindex() {
|
|
|
|
if (this.data.sortindex)
|
|
|
|
return this.data.sortindex;
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get thyself from your URI, then deserialize.
|
|
|
|
// Set thine 'response' field.
|
2012-09-14 23:02:32 +00:00
|
|
|
fetch: function fetch(resource) {
|
|
|
|
if (!resource instanceof Resource) {
|
|
|
|
throw new Error("First argument must be a Resource instance.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let r = resource.get();
|
2011-01-19 00:23:30 +00:00
|
|
|
if (r.success) {
|
|
|
|
this.deserialize(r); // Warning! Muffles exceptions!
|
|
|
|
}
|
|
|
|
this.response = r;
|
|
|
|
return this;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2012-09-14 23:02:32 +00:00
|
|
|
upload: function upload(resource) {
|
|
|
|
if (!resource instanceof Resource) {
|
|
|
|
throw new Error("First argument must be a Resource instance.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.put(this);
|
2011-01-19 00:23:30 +00:00
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-01-19 00:23:30 +00:00
|
|
|
// Take a base URI string, with trailing slash, and return the URI of this
|
|
|
|
// WBO based on collection and ID.
|
|
|
|
uri: function(base) {
|
2012-03-12 18:12:47 +00:00
|
|
|
if (this.collection && this.id) {
|
|
|
|
let url = Utils.makeURI(base + this.collection + "/" + this.id);
|
|
|
|
url.QueryInterface(Ci.nsIURL);
|
|
|
|
return url;
|
|
|
|
}
|
2011-01-19 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-01-19 00:23:30 +00:00
|
|
|
deserialize: function deserialize(json) {
|
|
|
|
this.data = json.constructor.toString() == String ? JSON.parse(json) : json;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// The payload is likely to be JSON, but if not, keep it as a string
|
|
|
|
this.payload = JSON.parse(this.payload);
|
|
|
|
} catch(ex) {}
|
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function toJSON() {
|
|
|
|
// Copy fields from data to be stringified, making sure payload is a string
|
|
|
|
let obj = {};
|
|
|
|
for (let [key, val] in Iterator(this.data))
|
|
|
|
obj[key] = key == "payload" ? JSON.stringify(val) : val;
|
|
|
|
if (this.ttl)
|
|
|
|
obj.ttl = this.ttl;
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
2011-12-13 22:54:17 +00:00
|
|
|
toString: function toString() {
|
|
|
|
return "{ " +
|
|
|
|
"id: " + this.id + " " +
|
|
|
|
"index: " + this.sortindex + " " +
|
|
|
|
"modified: " + this.modified + " " +
|
|
|
|
"ttl: " + this.ttl + " " +
|
|
|
|
"payload: " + JSON.stringify(this.payload) +
|
|
|
|
" }";
|
|
|
|
}
|
2011-01-19 00:23:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(WBORecord, "data", ["id", "modified", "sortindex", "payload"]);
|
|
|
|
|
2012-09-14 23:02:32 +00:00
|
|
|
/**
|
|
|
|
* An interface and caching layer for records.
|
|
|
|
*/
|
2012-10-31 16:13:28 +00:00
|
|
|
this.RecordManager = function RecordManager(service) {
|
2012-09-14 23:02:32 +00:00
|
|
|
this.service = service;
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
this._log = Log4Moz.repository.getLogger(this._logName);
|
|
|
|
this._records = {};
|
|
|
|
}
|
|
|
|
RecordManager.prototype = {
|
|
|
|
_recordType: WBORecord,
|
2011-06-13 18:42:18 +00:00
|
|
|
_logName: "Sync.RecordManager",
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
import: function RecordMgr_import(url) {
|
|
|
|
this._log.trace("Importing record: " + (url.spec ? url.spec : url));
|
|
|
|
try {
|
|
|
|
// Clear out the last response with empty object if GET fails
|
|
|
|
this.response = {};
|
2012-09-14 23:02:32 +00:00
|
|
|
this.response = this.service.resource(url).get();
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
// Don't parse and save the record on failure
|
|
|
|
if (!this.response.success)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let record = new this._recordType(url);
|
|
|
|
record.deserialize(this.response);
|
|
|
|
|
|
|
|
return this.set(url, record);
|
|
|
|
} catch(ex) {
|
|
|
|
this._log.debug("Failed to import record: " + Utils.exceptionStr(ex));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function RecordMgr_get(url) {
|
|
|
|
// Use a url string as the key to the hash
|
|
|
|
let spec = url.spec ? url.spec : url;
|
|
|
|
if (spec in this._records)
|
|
|
|
return this._records[spec];
|
|
|
|
return this.import(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function RecordMgr_set(url, record) {
|
|
|
|
let spec = url.spec ? url.spec : url;
|
|
|
|
return this._records[spec] = record;
|
|
|
|
},
|
|
|
|
|
|
|
|
contains: function RecordMgr_contains(url) {
|
|
|
|
if ((url.spec || url) in this._records)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
clearCache: function recordMgr_clearCache() {
|
|
|
|
this._records = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
del: function RecordMgr_del(url) {
|
|
|
|
delete this._records[url];
|
|
|
|
}
|
|
|
|
};
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.CryptoWrapper = function CryptoWrapper(collection, id) {
|
2010-03-16 23:31:56 +00:00
|
|
|
this.cleartext = {};
|
2010-11-30 00:41:17 +00:00
|
|
|
WBORecord.call(this, collection, id);
|
2010-02-11 23:25:31 +00:00
|
|
|
this.ciphertext = null;
|
2010-11-30 00:41:17 +00:00
|
|
|
this.id = id;
|
2008-11-03 22:37:51 +00:00
|
|
|
}
|
|
|
|
CryptoWrapper.prototype = {
|
|
|
|
__proto__: WBORecord.prototype,
|
2011-06-13 18:42:18 +00:00
|
|
|
_logName: "Sync.Record.CryptoWrapper",
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
ciphertextHMAC: function ciphertextHMAC(keyBundle) {
|
2011-03-01 22:29:41 +00:00
|
|
|
let hasher = keyBundle.sha256HMACHasher;
|
2012-03-22 22:49:50 +00:00
|
|
|
if (!hasher) {
|
2011-03-01 22:29:41 +00:00
|
|
|
throw "Cannot compute HMAC without an HMAC key.";
|
2012-03-22 22:49:50 +00:00
|
|
|
}
|
2011-03-01 22:29:41 +00:00
|
|
|
|
|
|
|
return Utils.bytesAsHex(Utils.digestUTF8(this.ciphertext, hasher));
|
2010-08-12 20:19:39 +00:00
|
|
|
},
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/*
|
|
|
|
* Don't directly use the sync key. Instead, grab a key for this
|
|
|
|
* collection, which is decrypted with the sync key.
|
|
|
|
*
|
|
|
|
* Cache those keys; invalidate the cache if the time on the keys collection
|
|
|
|
* changes, or other auth events occur.
|
|
|
|
*
|
|
|
|
* Optional key bundle overrides the collection key lookup.
|
|
|
|
*/
|
|
|
|
encrypt: function encrypt(keyBundle) {
|
2012-03-22 22:49:50 +00:00
|
|
|
if (!keyBundle) {
|
2012-09-14 23:02:33 +00:00
|
|
|
throw new Error("A key bundle must be supplied to encrypt.");
|
2012-03-22 22:49:50 +00:00
|
|
|
}
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2010-03-16 23:31:56 +00:00
|
|
|
this.IV = Svc.Crypto.generateRandomIV();
|
2010-03-16 23:31:56 +00:00
|
|
|
this.ciphertext = Svc.Crypto.encrypt(JSON.stringify(this.cleartext),
|
2012-03-22 22:49:50 +00:00
|
|
|
keyBundle.encryptionKeyB64, this.IV);
|
2010-11-30 00:41:17 +00:00
|
|
|
this.hmac = this.ciphertextHMAC(keyBundle);
|
2008-12-02 02:08:59 +00:00
|
|
|
this.cleartext = null;
|
2008-11-03 22:37:51 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Optional key bundle.
|
|
|
|
decrypt: function decrypt(keyBundle) {
|
|
|
|
if (!this.ciphertext) {
|
|
|
|
throw "No ciphertext: nothing to decrypt?";
|
|
|
|
}
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2012-03-22 22:49:50 +00:00
|
|
|
if (!keyBundle) {
|
2012-09-14 23:02:33 +00:00
|
|
|
throw new Error("A key bundle must be supplied to decrypt.");
|
2012-03-22 22:49:50 +00:00
|
|
|
}
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2010-03-16 23:31:56 +00:00
|
|
|
// Authenticate the encrypted blob with the expected HMAC
|
2010-11-30 00:41:17 +00:00
|
|
|
let computedHMAC = this.ciphertextHMAC(keyBundle);
|
2010-03-16 23:31:56 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (computedHMAC != this.hmac) {
|
2010-12-09 18:32:03 +00:00
|
|
|
Utils.throwHMACMismatch(this.hmac, computedHMAC);
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle invalid data here. Elsewhere we assume that cleartext is an object.
|
2011-03-01 22:29:41 +00:00
|
|
|
let cleartext = Svc.Crypto.decrypt(this.ciphertext,
|
2012-03-22 22:49:50 +00:00
|
|
|
keyBundle.encryptionKeyB64, this.IV);
|
2011-03-01 22:29:41 +00:00
|
|
|
let json_result = JSON.parse(cleartext);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
if (json_result && (json_result instanceof Object)) {
|
|
|
|
this.cleartext = json_result;
|
2011-03-01 22:29:41 +00:00
|
|
|
this.ciphertext = null;
|
|
|
|
} else {
|
2010-11-30 00:41:17 +00:00
|
|
|
throw "Decryption failed: result is <" + json_result + ">, not an object.";
|
|
|
|
}
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Verify that the encrypted id matches the requested record's id.
|
2010-03-16 23:31:56 +00:00
|
|
|
if (this.cleartext.id != this.id)
|
2011-04-18 20:25:19 +00:00
|
|
|
throw "Record id mismatch: " + this.cleartext.id + " != " + this.id;
|
2010-03-16 23:31:56 +00:00
|
|
|
|
2009-06-05 02:06:57 +00:00
|
|
|
return this.cleartext;
|
2009-01-23 23:08:12 +00:00
|
|
|
},
|
|
|
|
|
2011-12-13 22:54:17 +00:00
|
|
|
toString: function toString() {
|
|
|
|
let payload = this.deleted ? "DELETED" : JSON.stringify(this.cleartext);
|
|
|
|
|
|
|
|
return "{ " +
|
|
|
|
"id: " + this.id + " " +
|
|
|
|
"index: " + this.sortindex + " " +
|
|
|
|
"modified: " + this.modified + " " +
|
|
|
|
"ttl: " + this.ttl + " " +
|
|
|
|
"payload: " + payload + " " +
|
|
|
|
"collection: " + (this.collection || "undefined") +
|
|
|
|
" }";
|
|
|
|
},
|
2010-03-16 23:31:56 +00:00
|
|
|
|
|
|
|
// The custom setter below masks the parent's getter, so explicitly call it :(
|
|
|
|
get id() WBORecord.prototype.__lookupGetter__("id").call(this),
|
|
|
|
|
|
|
|
// Keep both plaintext and encrypted versions of the id to verify integrity
|
|
|
|
set id(val) {
|
|
|
|
WBORecord.prototype.__lookupSetter__("id").call(this, val);
|
|
|
|
return this.cleartext.id = val;
|
|
|
|
},
|
2008-11-03 22:37:51 +00:00
|
|
|
};
|
|
|
|
|
2010-08-12 20:19:39 +00:00
|
|
|
Utils.deferGetSet(CryptoWrapper, "payload", ["ciphertext", "IV", "hmac"]);
|
2010-03-16 23:31:56 +00:00
|
|
|
Utils.deferGetSet(CryptoWrapper, "cleartext", "deleted");
|
2009-04-13 21:39:29 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
|
|
|
|
/**
|
2012-03-22 22:49:50 +00:00
|
|
|
* Keeps track of mappings between collection names ('tabs') and KeyBundles.
|
2010-11-30 00:41:17 +00:00
|
|
|
*
|
|
|
|
* You can update this thing simply by giving it /info/collections. It'll
|
|
|
|
* use the last modified time to bring itself up to date.
|
|
|
|
*/
|
2012-10-31 16:13:28 +00:00
|
|
|
this.CollectionKeyManager = function CollectionKeyManager() {
|
2011-04-19 19:35:04 +00:00
|
|
|
this.lastModified = 0;
|
2010-11-30 00:41:17 +00:00
|
|
|
this._collections = {};
|
|
|
|
this._default = null;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2012-09-14 23:02:33 +00:00
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.CollectionKeyManager");
|
2008-11-03 22:37:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// TODO: persist this locally as an Identity. Bug 610913.
|
|
|
|
// Note that the last modified time needs to be preserved.
|
|
|
|
CollectionKeyManager.prototype = {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Return information about old vs new keys:
|
|
|
|
// * same: true if two collections are equal
|
|
|
|
// * changed: an array of collection names that changed.
|
|
|
|
_compareKeyBundleCollections: function _compareKeyBundleCollections(m1, m2) {
|
|
|
|
let changed = [];
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
function process(m1, m2) {
|
|
|
|
for (let k1 in m1) {
|
|
|
|
let v1 = m1[k1];
|
|
|
|
let v2 = m2[k1];
|
|
|
|
if (!(v1 && v2 && v1.equals(v2)))
|
|
|
|
changed.push(k1);
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Diffs both ways.
|
|
|
|
process(m1, m2);
|
|
|
|
process(m2, m1);
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Return a sorted, unique array.
|
|
|
|
changed.sort();
|
|
|
|
let last;
|
|
|
|
changed = [x for each (x in changed) if ((x != last) && (last = x))];
|
|
|
|
return {same: changed.length == 0,
|
|
|
|
changed: changed};
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
get isClear() {
|
|
|
|
return !this._default;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-08 00:30:06 +00:00
|
|
|
clear: function clear() {
|
2012-09-14 23:02:33 +00:00
|
|
|
this._log.info("Clearing collection keys...");
|
2011-04-19 19:35:04 +00:00
|
|
|
this.lastModified = 0;
|
2010-12-08 00:30:06 +00:00
|
|
|
this._collections = {};
|
|
|
|
this._default = null;
|
|
|
|
},
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
keyForCollection: function(collection) {
|
|
|
|
if (collection && this._collections[collection])
|
|
|
|
return this._collections[collection];
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
return this._default;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If `collections` (an array of strings) is provided, iterate
|
|
|
|
* over it and generate random keys for each collection.
|
2011-04-19 19:35:04 +00:00
|
|
|
* Create a WBO for the given data.
|
|
|
|
*/
|
|
|
|
_makeWBO: function(collections, defaultBundle) {
|
|
|
|
let wbo = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
|
|
|
let c = {};
|
|
|
|
for (let k in collections) {
|
2012-03-22 22:49:50 +00:00
|
|
|
c[k] = collections[k].keyPairB64;
|
2011-04-19 19:35:04 +00:00
|
|
|
}
|
|
|
|
wbo.cleartext = {
|
2012-03-22 22:49:50 +00:00
|
|
|
"default": defaultBundle ? defaultBundle.keyPairB64 : null,
|
2011-04-19 19:35:04 +00:00
|
|
|
"collections": c,
|
|
|
|
"collection": CRYPTO_COLLECTION,
|
|
|
|
"id": KEYS_WBO
|
|
|
|
};
|
|
|
|
return wbo;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a WBO for the current keys.
|
|
|
|
*/
|
|
|
|
asWBO: function(collection, id)
|
|
|
|
this._makeWBO(this._collections, this._default),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute a new default key, and new keys for any specified collections.
|
2010-11-30 00:41:17 +00:00
|
|
|
*/
|
2011-04-19 19:35:04 +00:00
|
|
|
newKeys: function(collections) {
|
2012-03-22 22:49:50 +00:00
|
|
|
let newDefaultKey = new BulkKeyBundle(DEFAULT_KEYBUNDLE_NAME);
|
2010-11-30 00:41:17 +00:00
|
|
|
newDefaultKey.generateRandom();
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let newColls = {};
|
|
|
|
if (collections) {
|
|
|
|
collections.forEach(function (c) {
|
2012-03-22 22:49:50 +00:00
|
|
|
let b = new BulkKeyBundle(c);
|
2010-11-30 00:41:17 +00:00
|
|
|
b.generateRandom();
|
|
|
|
newColls[c] = b;
|
|
|
|
});
|
|
|
|
}
|
2011-04-19 19:35:04 +00:00
|
|
|
return [newDefaultKey, newColls];
|
2010-11-30 00:41:17 +00:00
|
|
|
},
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
/**
|
|
|
|
* Generates new keys, but does not replace our local copy. Use this to
|
|
|
|
* verify an upload before storing.
|
|
|
|
*/
|
|
|
|
generateNewKeysWBO: function(collections) {
|
|
|
|
let newDefaultKey, newColls;
|
|
|
|
[newDefaultKey, newColls] = this.newKeys(collections);
|
|
|
|
|
|
|
|
return this._makeWBO(newColls, newDefaultKey);
|
2010-07-03 18:13:40 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Take the fetched info/collections WBO, checking the change
|
|
|
|
// time of the crypto collection.
|
|
|
|
updateNeeded: function(info_collections) {
|
2008-11-08 10:00:33 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
this._log.info("Testing for updateNeeded. Last modified: " + this.lastModified);
|
2010-03-26 02:23:44 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// No local record of modification time? Need an update.
|
2011-04-19 19:35:04 +00:00
|
|
|
if (!this.lastModified)
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
2010-03-16 23:31:56 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// No keys on the server? We need an update, though our
|
|
|
|
// update handling will be a little more drastic...
|
2011-04-19 19:35:04 +00:00
|
|
|
if (!(CRYPTO_COLLECTION in info_collections))
|
2010-11-30 00:41:17 +00:00
|
|
|
return true;
|
2010-03-16 23:31:56 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// Otherwise, we need an update if our modification time is stale.
|
2011-04-19 19:35:04 +00:00
|
|
|
return (info_collections[CRYPTO_COLLECTION] > this.lastModified);
|
2008-11-08 10:00:33 +00:00
|
|
|
},
|
|
|
|
|
2011-04-19 19:33:47 +00:00
|
|
|
//
|
2010-12-09 18:32:03 +00:00
|
|
|
// Set our keys and modified time to the values fetched from the server.
|
|
|
|
// Returns one of three values:
|
2011-04-19 19:33:47 +00:00
|
|
|
//
|
2010-12-09 18:32:03 +00:00
|
|
|
// * If the default key was modified, return true.
|
|
|
|
// * If the default key was not modified, but per-collection keys were,
|
|
|
|
// return an array of such.
|
|
|
|
// * Otherwise, return false -- we were up-to-date.
|
2011-04-19 19:33:47 +00:00
|
|
|
//
|
2010-11-30 00:41:17 +00:00
|
|
|
setContents: function setContents(payload, modified) {
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
if (!modified)
|
|
|
|
throw "No modified time provided to setContents.";
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
let self = this;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2012-09-14 23:02:33 +00:00
|
|
|
this._log.info("Setting collection keys contents. Our last modified: " +
|
2011-04-19 19:35:04 +00:00
|
|
|
this.lastModified + ", input modified: " + modified + ".");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
if (!payload)
|
2012-09-14 23:02:33 +00:00
|
|
|
throw "No payload in CollectionKeyManager.setContents().";
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
if (!payload.default) {
|
|
|
|
this._log.warn("No downloaded default key: this should not occur.");
|
|
|
|
this._log.warn("Not clearing local keys.");
|
2012-09-14 23:02:33 +00:00
|
|
|
throw "No default key in CollectionKeyManager.setContents(). Cannot proceed.";
|
2010-12-09 18:32:03 +00:00
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Process the incoming default key.
|
2012-03-22 22:49:50 +00:00
|
|
|
let b = new BulkKeyBundle(DEFAULT_KEYBUNDLE_NAME);
|
|
|
|
b.keyPairB64 = payload.default;
|
2010-12-09 18:32:03 +00:00
|
|
|
let newDefault = b;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Process the incoming collections.
|
|
|
|
let newCollections = {};
|
2010-11-30 00:41:17 +00:00
|
|
|
if ("collections" in payload) {
|
2010-12-09 18:32:03 +00:00
|
|
|
this._log.info("Processing downloaded per-collection keys.");
|
|
|
|
let colls = payload.collections;
|
2010-11-30 00:41:17 +00:00
|
|
|
for (let k in colls) {
|
|
|
|
let v = colls[k];
|
|
|
|
if (v) {
|
2012-03-22 22:49:50 +00:00
|
|
|
let keyObj = new BulkKeyBundle(k);
|
|
|
|
keyObj.keyPairB64 = v;
|
2010-11-30 00:41:17 +00:00
|
|
|
if (keyObj) {
|
2010-12-09 18:32:03 +00:00
|
|
|
newCollections[k] = keyObj;
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Check to see if these are already our keys.
|
|
|
|
let sameDefault = (this._default && this._default.equals(newDefault));
|
|
|
|
let collComparison = this._compareKeyBundleCollections(newCollections, this._collections);
|
|
|
|
let sameColls = collComparison.same;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
if (sameDefault && sameColls) {
|
2011-04-19 19:35:04 +00:00
|
|
|
self._log.info("New keys are the same as our old keys! Bumped local modified time.");
|
|
|
|
self.lastModified = modified;
|
2010-12-09 18:32:03 +00:00
|
|
|
return false;
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Make sure things are nice and tidy before we set.
|
|
|
|
this.clear();
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
this._log.info("Saving downloaded keys.");
|
|
|
|
this._default = newDefault;
|
|
|
|
this._collections = newCollections;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2011-04-19 19:35:04 +00:00
|
|
|
// Always trust the server.
|
|
|
|
self._log.info("Bumping last modified to " + modified);
|
|
|
|
self.lastModified = modified;
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
return sameDefault ? collComparison.changed : true;
|
2008-11-08 10:00:33 +00:00
|
|
|
},
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
updateContents: function updateContents(syncKeyBundle, storage_keys) {
|
|
|
|
let log = this._log;
|
|
|
|
log.info("Updating collection keys...");
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
// storage_keys is a WBO, fetched from storage/crypto/keys.
|
|
|
|
// Its payload is the default key, and a map of collections to keys.
|
|
|
|
// We lazily compute the key objects from the strings we're given.
|
2011-04-19 19:33:47 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let payload;
|
|
|
|
try {
|
|
|
|
payload = storage_keys.decrypt(syncKeyBundle);
|
|
|
|
} catch (ex) {
|
|
|
|
log.warn("Got exception \"" + ex + "\" decrypting storage keys with sync key.");
|
|
|
|
log.info("Aborting updateContents. Rethrowing.");
|
|
|
|
throw ex;
|
|
|
|
}
|
2008-11-08 10:00:33 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
let r = this.setContents(payload, storage_keys.modified);
|
|
|
|
log.info("Collection keys updated.");
|
|
|
|
return r;
|
2010-12-09 18:32:03 +00:00
|
|
|
}
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
2008-11-03 22:37:51 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.Collection = function Collection(uri, recordObj, service) {
|
2012-09-14 23:02:32 +00:00
|
|
|
if (!service) {
|
|
|
|
throw new Error("Collection constructor requires a service.");
|
|
|
|
}
|
|
|
|
|
2011-01-19 00:23:30 +00:00
|
|
|
Resource.call(this, uri);
|
2012-09-14 23:02:32 +00:00
|
|
|
|
|
|
|
// This is a bit hacky, but gets the job done.
|
|
|
|
let res = service.resource(uri);
|
|
|
|
this.authenticator = res.authenticator;
|
|
|
|
|
2011-01-19 00:23:30 +00:00
|
|
|
this._recordObj = recordObj;
|
2012-09-14 23:02:32 +00:00
|
|
|
this._service = service;
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
this._full = false;
|
|
|
|
this._ids = null;
|
|
|
|
this._limit = 0;
|
|
|
|
this._older = 0;
|
|
|
|
this._newer = 0;
|
|
|
|
this._data = [];
|
|
|
|
}
|
|
|
|
Collection.prototype = {
|
|
|
|
__proto__: Resource.prototype,
|
2011-06-13 18:42:18 +00:00
|
|
|
_logName: "Sync.Collection",
|
2011-01-19 00:23:30 +00:00
|
|
|
|
|
|
|
_rebuildURL: function Coll__rebuildURL() {
|
|
|
|
// XXX should consider what happens if it's not a URL...
|
|
|
|
this.uri.QueryInterface(Ci.nsIURL);
|
|
|
|
|
|
|
|
let args = [];
|
|
|
|
if (this.older)
|
|
|
|
args.push('older=' + this.older);
|
|
|
|
else if (this.newer) {
|
|
|
|
args.push('newer=' + this.newer);
|
|
|
|
}
|
|
|
|
if (this.full)
|
|
|
|
args.push('full=1');
|
|
|
|
if (this.sort)
|
|
|
|
args.push('sort=' + this.sort);
|
|
|
|
if (this.ids != null)
|
|
|
|
args.push("ids=" + this.ids);
|
|
|
|
if (this.limit > 0 && this.limit != Infinity)
|
|
|
|
args.push("limit=" + this.limit);
|
|
|
|
|
|
|
|
this.uri.query = (args.length > 0)? '?' + args.join('&') : '';
|
|
|
|
},
|
|
|
|
|
|
|
|
// get full items
|
|
|
|
get full() { return this._full; },
|
|
|
|
set full(value) {
|
|
|
|
this._full = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Apply the action to a certain set of ids
|
|
|
|
get ids() this._ids,
|
|
|
|
set ids(value) {
|
|
|
|
this._ids = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Limit how many records to get
|
|
|
|
get limit() this._limit,
|
|
|
|
set limit(value) {
|
|
|
|
this._limit = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
// get only items modified before some date
|
|
|
|
get older() { return this._older; },
|
|
|
|
set older(value) {
|
|
|
|
this._older = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
// get only items modified since some date
|
|
|
|
get newer() { return this._newer; },
|
|
|
|
set newer(value) {
|
|
|
|
this._newer = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
// get items sorted by some criteria. valid values:
|
|
|
|
// oldest (oldest first)
|
|
|
|
// newest (newest first)
|
|
|
|
// index
|
|
|
|
get sort() { return this._sort; },
|
|
|
|
set sort(value) {
|
|
|
|
this._sort = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
|
|
|
pushData: function Coll_pushData(data) {
|
|
|
|
this._data.push(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
clearRecords: function Coll_clearRecords() {
|
|
|
|
this._data = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
set recordHandler(onRecord) {
|
|
|
|
// Save this because onProgress is called with this as the ChannelListener
|
|
|
|
let coll = this;
|
|
|
|
|
|
|
|
// Switch to newline separated records for incremental parsing
|
|
|
|
coll.setHeader("Accept", "application/newlines");
|
|
|
|
|
|
|
|
this._onProgress = function() {
|
|
|
|
let newline;
|
|
|
|
while ((newline = this._data.indexOf("\n")) > 0) {
|
|
|
|
// Split the json record from the rest of the data
|
|
|
|
let json = this._data.slice(0, newline);
|
|
|
|
this._data = this._data.slice(newline + 1);
|
|
|
|
|
|
|
|
// Deserialize a record from json and give it to the callback
|
|
|
|
let record = new coll._recordObj();
|
|
|
|
record.deserialize(json);
|
|
|
|
onRecord(record);
|
|
|
|
}
|
|
|
|
};
|
2012-09-14 23:02:32 +00:00
|
|
|
},
|
2011-01-19 00:23:30 +00:00
|
|
|
};
|