2007-12-10 21:38:53 -08:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
2010-11-16 11:50:18 -08:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
2007-12-10 21:38:53 -08:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
const EXPORTED_SYMBOLS = ["XPCOMUtils", "Services", "NetUtil", "PlacesUtils",
|
2011-08-26 10:25:29 -07:00
|
|
|
"FileUtils", "Utils", "Async", "Svc", "Str"];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2012-04-16 12:19:01 -07:00
|
|
|
Cu.import("resource://services-common/observers.js");
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/preferences.js");
|
|
|
|
Cu.import("resource://services-common/stringbundle.js");
|
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
Cu.import("resource://services-common/async.js");
|
2012-04-16 12:19:01 -07:00
|
|
|
Cu.import("resource://services-crypto/utils.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2011-05-19 18:08:35 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2011-05-19 18:08:07 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2011-05-19 18:08:22 -07:00
|
|
|
Cu.import("resource://gre/modules/PlacesUtils.jsm");
|
2011-04-07 15:31:11 -07:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
2011-05-19 18:08:51 -07:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2011-01-26 21:43:33 -08:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
/*
|
|
|
|
* Utility functions
|
|
|
|
*/
|
|
|
|
|
2008-02-13 14:30:44 -08:00
|
|
|
let Utils = {
|
2012-04-05 23:26:06 -07:00
|
|
|
// Alias in functions from CommonUtils. These previously were defined here.
|
|
|
|
// In the ideal world, references to these would be removed.
|
|
|
|
nextTick: CommonUtils.nextTick,
|
|
|
|
namedTimer: CommonUtils.namedTimer,
|
|
|
|
exceptionStr: CommonUtils.exceptionStr,
|
|
|
|
stackTrace: CommonUtils.stackTrace,
|
|
|
|
makeURI: CommonUtils.makeURI,
|
2012-04-09 15:39:59 -07:00
|
|
|
encodeUTF8: CommonUtils.encodeUTF8,
|
|
|
|
decodeUTF8: CommonUtils.decodeUTF8,
|
|
|
|
safeAtoB: CommonUtils.safeAtoB,
|
2012-04-16 12:19:01 -07:00
|
|
|
byteArrayToString: CommonUtils.byteArrayToString,
|
|
|
|
bytesAsHex: CommonUtils.bytesAsHex,
|
|
|
|
encodeBase32: CommonUtils.encodeBase32,
|
|
|
|
decodeBase32: CommonUtils.decodeBase32,
|
|
|
|
|
|
|
|
// Aliases from CryptoUtils.
|
|
|
|
generateRandomBytes: CryptoUtils.generateRandomBytes,
|
|
|
|
computeHTTPMACSHA1: CryptoUtils.computeHTTPMACSHA1,
|
|
|
|
digestUTF8: CryptoUtils.digestUTF8,
|
|
|
|
digestBytes: CryptoUtils.digestBytes,
|
|
|
|
sha1: CryptoUtils.sha1,
|
|
|
|
sha1Base32: CryptoUtils.sha1Base32,
|
|
|
|
makeHMACKey: CryptoUtils.makeHMACKey,
|
|
|
|
makeHMACHasher: CryptoUtils.makeHMACHasher,
|
|
|
|
hkdfExpand: CryptoUtils.hkdfExpand,
|
|
|
|
pbkdf2Generate: CryptoUtils.pbkdf2Generate,
|
|
|
|
deriveKeyFromPassphrase: CryptoUtils.deriveKeyFromPassphrase,
|
|
|
|
getHTTPMACSHA1Header: CryptoUtils.getHTTPMACSHA1Header,
|
2012-04-05 23:26:06 -07:00
|
|
|
|
2009-06-05 22:21:14 -07:00
|
|
|
/**
|
|
|
|
* Wrap a function to catch all exceptions and log them
|
|
|
|
*
|
|
|
|
* @usage MyObj._catch = Utils.catch;
|
|
|
|
* MyObj.foo = function() { this._catch(func)(); }
|
2011-01-10 21:51:29 -08:00
|
|
|
*
|
|
|
|
* Optionally pass a function which will be called if an
|
|
|
|
* exception occurs.
|
2009-06-05 22:21:14 -07:00
|
|
|
*/
|
2011-01-10 21:51:29 -08:00
|
|
|
catch: function Utils_catch(func, exceptionCallback) {
|
2009-06-05 22:21:14 -07:00
|
|
|
let thisArg = this;
|
|
|
|
return function WrappedCatch() {
|
|
|
|
try {
|
|
|
|
return func.call(thisArg);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
2009-07-24 15:28:42 -07:00
|
|
|
thisArg._log.debug("Exception: " + Utils.exceptionStr(ex));
|
2011-01-10 21:51:29 -08:00
|
|
|
if (exceptionCallback) {
|
|
|
|
return exceptionCallback.call(thisArg, ex);
|
|
|
|
}
|
|
|
|
return null;
|
2009-06-05 22:21:14 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2009-06-06 11:52:49 -07:00
|
|
|
/**
|
|
|
|
* Wrap a function to call lock before calling the function then unlock.
|
|
|
|
*
|
|
|
|
* @usage MyObj._lock = Utils.lock;
|
|
|
|
* MyObj.foo = function() { this._lock(func)(); }
|
|
|
|
*/
|
2010-11-29 16:41:17 -08:00
|
|
|
lock: function lock(label, func) {
|
2009-06-06 11:52:49 -07:00
|
|
|
let thisArg = this;
|
|
|
|
return function WrappedLock() {
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!thisArg.lock()) {
|
|
|
|
throw "Could not acquire lock. Label: \"" + label + "\".";
|
|
|
|
}
|
2009-06-06 11:52:49 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
return func.call(thisArg);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
thisArg.unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2010-12-16 12:00:57 -08:00
|
|
|
|
|
|
|
isLockException: function isLockException(ex) {
|
2011-01-10 21:51:29 -08:00
|
|
|
return ex && ex.indexOf && ex.indexOf("Could not acquire lock.") == 0;
|
2010-12-16 12:00:57 -08:00
|
|
|
},
|
2009-06-06 11:52:49 -07:00
|
|
|
|
2009-06-05 15:34:23 -07:00
|
|
|
/**
|
2011-07-29 18:47:39 -07:00
|
|
|
* Wrap functions to notify when it starts and finishes executing or if it
|
|
|
|
* threw an error.
|
|
|
|
*
|
|
|
|
* The message is a combination of a provided prefix, the local name, and
|
|
|
|
* the event. Possible events are: "start", "finish", "error". The subject
|
|
|
|
* is the function's return value on "finish" or the caught exception on
|
|
|
|
* "error". The data argument is the predefined data value.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* @usage function MyObj(name) {
|
|
|
|
* this.name = name;
|
|
|
|
* this._notify = Utils.notify("obj:");
|
|
|
|
* }
|
|
|
|
* MyObj.prototype = {
|
|
|
|
* foo: function() this._notify("func", "data-arg", function () {
|
|
|
|
* //...
|
|
|
|
* }(),
|
|
|
|
* };
|
2009-06-05 15:34:23 -07:00
|
|
|
*/
|
|
|
|
notify: function Utils_notify(prefix) {
|
2011-07-29 18:47:39 -07:00
|
|
|
return function NotifyMaker(name, data, func) {
|
2009-06-05 15:34:23 -07:00
|
|
|
let thisArg = this;
|
2011-07-29 18:47:39 -07:00
|
|
|
let notify = function(state, subject) {
|
2009-06-05 15:34:23 -07:00
|
|
|
let mesg = prefix + name + ":" + state;
|
2009-12-17 18:51:55 -08:00
|
|
|
thisArg._log.trace("Event: " + mesg);
|
2011-07-29 18:47:39 -07:00
|
|
|
Observers.notify(mesg, subject, data);
|
2009-06-05 15:34:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return function WrappedNotify() {
|
|
|
|
try {
|
2011-07-29 18:47:39 -07:00
|
|
|
notify("start", null);
|
2009-06-05 15:34:23 -07:00
|
|
|
let ret = func.call(thisArg);
|
2011-07-29 18:47:39 -07:00
|
|
|
notify("finish", ret);
|
2009-06-05 15:34:23 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(ex) {
|
2011-07-29 18:47:39 -07:00
|
|
|
notify("error", ex);
|
2009-06-05 15:34:23 -07:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
2009-08-31 16:28:00 -07:00
|
|
|
|
2011-02-03 10:23:38 -08:00
|
|
|
runInTransaction: function(db, callback, thisObj) {
|
|
|
|
let hasTransaction = false;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
hasTransaction = true;
|
|
|
|
} catch(e) { /* om nom nom exceptions */ }
|
|
|
|
|
|
|
|
try {
|
|
|
|
return callback.call(thisObj);
|
|
|
|
} finally {
|
|
|
|
if (hasTransaction) {
|
|
|
|
db.commitTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-11-30 10:26:13 -08:00
|
|
|
/**
|
|
|
|
* GUIDs are 9 random bytes encoded with base64url (RFC 4648).
|
|
|
|
* That makes them 12 characters long with 72 bits of entropy.
|
|
|
|
*/
|
|
|
|
makeGUID: function makeGUID() {
|
2012-04-17 15:42:33 -07:00
|
|
|
return CommonUtils.encodeBase64URL(Utils.generateRandomBytes(9));
|
2008-06-20 12:34:29 -07:00
|
|
|
},
|
|
|
|
|
2011-01-28 09:39:28 -08:00
|
|
|
_base64url_regex: /^[-abcdefghijklmnopqrstuvwxyz0123456789_]{12}$/i,
|
|
|
|
checkGUID: function checkGUID(guid) {
|
|
|
|
return !!guid && this._base64url_regex.test(guid);
|
|
|
|
},
|
|
|
|
|
2009-04-13 14:39:29 -07:00
|
|
|
/**
|
|
|
|
* Add a simple getter/setter to an object that defers access of a property
|
|
|
|
* to an inner property.
|
|
|
|
*
|
|
|
|
* @param obj
|
|
|
|
* Object to add properties to defer in its prototype
|
|
|
|
* @param defer
|
2011-02-25 14:43:10 -08:00
|
|
|
* Property of obj to defer to
|
2009-04-13 14:39:29 -07:00
|
|
|
* @param prop
|
|
|
|
* Property name to defer (or an array of property names)
|
|
|
|
*/
|
|
|
|
deferGetSet: function Utils_deferGetSet(obj, defer, prop) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prop))
|
2009-04-13 14:39:29 -07:00
|
|
|
return prop.map(function(prop) Utils.deferGetSet(obj, defer, prop));
|
|
|
|
|
|
|
|
let prot = obj.prototype;
|
|
|
|
|
|
|
|
// Create a getter if it doesn't exist yet
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!prot.__lookupGetter__(prop)) {
|
2011-02-25 14:43:10 -08:00
|
|
|
prot.__defineGetter__(prop, function () {
|
|
|
|
return this[defer][prop];
|
|
|
|
});
|
2010-11-29 16:41:17 -08:00
|
|
|
}
|
2009-04-13 14:39:29 -07:00
|
|
|
|
|
|
|
// Create a setter if it doesn't exist yet
|
2011-02-25 14:43:10 -08:00
|
|
|
if (!prot.__lookupSetter__(prop)) {
|
|
|
|
prot.__defineSetter__(prop, function (val) {
|
|
|
|
this[defer][prop] = val;
|
|
|
|
});
|
|
|
|
}
|
2009-04-13 14:39:29 -07:00
|
|
|
},
|
|
|
|
|
2009-08-18 20:03:11 -07:00
|
|
|
lazyStrings: function Weave_lazyStrings(name) {
|
2010-06-09 17:03:31 -07:00
|
|
|
let bundle = "chrome://weave/locale/services/" + name + ".properties";
|
2009-08-18 20:03:11 -07:00
|
|
|
return function() new StringBundle(bundle);
|
|
|
|
},
|
|
|
|
|
2009-05-19 08:53:30 -07:00
|
|
|
deepEquals: function eq(a, b) {
|
|
|
|
// If they're triple equals, then it must be equals!
|
|
|
|
if (a === b)
|
2008-02-13 14:30:44 -08:00
|
|
|
return true;
|
2008-03-05 00:00:56 -08:00
|
|
|
|
2009-07-22 23:49:15 -07:00
|
|
|
// If they weren't equal, they must be objects to be different
|
|
|
|
if (typeof a != "object" || typeof b != "object")
|
2008-02-13 14:30:44 -08:00
|
|
|
return false;
|
|
|
|
|
2009-07-22 23:49:15 -07:00
|
|
|
// But null objects won't have properties to compare
|
|
|
|
if (a === null || b === null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure all of a's keys have a matching value in b
|
|
|
|
for (let k in a)
|
|
|
|
if (!eq(a[k], b[k]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do the same for b's keys but skip those that we already checked
|
|
|
|
for (let k in b)
|
|
|
|
if (!(k in a) && !eq(a[k], b[k]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2008-02-13 14:30:44 -08:00
|
|
|
},
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2010-12-09 10:32:03 -08:00
|
|
|
// Generator and discriminator for HMAC exceptions.
|
2012-04-05 23:26:06 -07:00
|
|
|
// Split these out in case we want to make them richer in future, and to
|
2010-12-09 10:32:03 -08:00
|
|
|
// avoid inevitable confusion if the message changes.
|
|
|
|
throwHMACMismatch: function throwHMACMismatch(shouldBe, is) {
|
|
|
|
throw "Record SHA256 HMAC mismatch: should be " + shouldBe + ", is " + is;
|
|
|
|
},
|
2012-04-05 23:26:06 -07:00
|
|
|
|
2010-12-09 10:32:03 -08:00
|
|
|
isHMACMismatch: function isHMACMismatch(ex) {
|
|
|
|
const hmacFail = "Record SHA256 HMAC mismatch: ";
|
|
|
|
return ex && ex.indexOf && (ex.indexOf(hmacFail) == 0);
|
|
|
|
},
|
2012-04-05 23:26:06 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/**
|
|
|
|
* Turn RFC 4648 base32 into our own user-friendly version.
|
|
|
|
* ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
|
|
|
|
* becomes
|
|
|
|
* abcdefghijk8mn9pqrstuvwxyz234567
|
|
|
|
*/
|
|
|
|
base32ToFriendly: function base32ToFriendly(input) {
|
|
|
|
return input.toLowerCase()
|
|
|
|
.replace("l", '8', "g")
|
|
|
|
.replace("o", '9', "g");
|
|
|
|
},
|
|
|
|
|
|
|
|
base32FromFriendly: function base32FromFriendly(input) {
|
|
|
|
return input.toUpperCase()
|
|
|
|
.replace("8", 'L', "g")
|
|
|
|
.replace("9", 'O', "g");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key manipulation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Return an octet string in friendly base32 *with no trailing =*.
|
|
|
|
encodeKeyBase32: function encodeKeyBase32(keyData) {
|
|
|
|
return Utils.base32ToFriendly(
|
|
|
|
Utils.encodeBase32(keyData))
|
|
|
|
.slice(0, SYNC_KEY_ENCODED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
decodeKeyBase32: function decodeKeyBase32(encoded) {
|
|
|
|
return Utils.decodeBase32(
|
|
|
|
Utils.base32FromFriendly(
|
|
|
|
Utils.normalizePassphrase(encoded)))
|
|
|
|
.slice(0, SYNC_KEY_DECODED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
base64Key: function base64Key(keyData) {
|
|
|
|
return btoa(keyData);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* N.B., salt should be base64 encoded, even though we have to decode
|
|
|
|
* it later!
|
|
|
|
*/
|
|
|
|
derivePresentableKeyFromPassphrase : function derivePresentableKeyFromPassphrase(passphrase, salt, keyLength, forceJS) {
|
2012-04-16 12:19:01 -07:00
|
|
|
let k = CryptoUtils.deriveKeyFromPassphrase(passphrase, salt, keyLength,
|
|
|
|
forceJS);
|
2010-11-29 16:41:17 -08:00
|
|
|
return Utils.encodeKeyBase32(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* N.B., salt should be base64 encoded, even though we have to decode
|
|
|
|
* it later!
|
|
|
|
*/
|
|
|
|
deriveEncodedKeyFromPassphrase : function deriveEncodedKeyFromPassphrase(passphrase, salt, keyLength, forceJS) {
|
2012-04-16 12:19:01 -07:00
|
|
|
let k = CryptoUtils.deriveKeyFromPassphrase(passphrase, salt, keyLength,
|
|
|
|
forceJS);
|
2010-11-29 16:41:17 -08:00
|
|
|
return Utils.base64Key(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a base64-encoded 128-bit AES key, returning it as five groups of five
|
|
|
|
* uppercase alphanumeric characters, separated by hyphens.
|
|
|
|
* A.K.A. base64-to-base32 encoding.
|
|
|
|
*/
|
|
|
|
presentEncodedKeyAsSyncKey : function presentEncodedKeyAsSyncKey(encodedKey) {
|
|
|
|
return Utils.encodeKeyBase32(atob(encodedKey));
|
|
|
|
},
|
|
|
|
|
2012-05-10 12:21:21 -07:00
|
|
|
jsonLoad: function jsonLoad(path, that, callback) {
|
|
|
|
CommonUtils.jsonLoad("weave/" + path, that, callback);
|
2009-03-31 17:52:16 -07:00
|
|
|
},
|
|
|
|
|
2012-05-10 12:21:21 -07:00
|
|
|
jsonSave: function jsonSave(path, that, obj, callback) {
|
|
|
|
CommonUtils.jsonSave("weave/" + path, that, obj, callback);
|
2009-03-31 17:52:16 -07:00
|
|
|
},
|
|
|
|
|
2009-11-25 16:59:02 -08:00
|
|
|
getIcon: function(iconUri, defaultIcon) {
|
|
|
|
try {
|
|
|
|
let iconURI = Utils.makeURI(iconUri);
|
2011-05-19 18:08:22 -07:00
|
|
|
return PlacesUtils.favicons.getFaviconLinkForIcon(iconURI).spec;
|
2009-11-25 16:59:02 -08:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
|
|
|
|
// Just give the provided default icon or the system's default
|
2011-05-19 18:08:22 -07:00
|
|
|
return defaultIcon || PlacesUtils.favicons.defaultFavicon.spec;
|
2009-11-25 16:59:02 -08:00
|
|
|
},
|
|
|
|
|
2009-08-24 21:47:35 -07:00
|
|
|
getErrorString: function Utils_getErrorString(error, args) {
|
2009-09-15 18:38:52 -07:00
|
|
|
try {
|
2009-09-16 16:52:43 -07:00
|
|
|
return Str.errors.get(error, args || null);
|
2009-09-15 18:38:52 -07:00
|
|
|
} catch (e) {}
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
// basically returns "Unknown Error"
|
2009-09-16 16:52:43 -07:00
|
|
|
return Str.errors.get("error.reason.unknown");
|
2009-08-24 21:47:35 -07:00
|
|
|
},
|
|
|
|
|
2010-09-17 08:48:06 -07:00
|
|
|
/**
|
2010-11-29 16:41:17 -08:00
|
|
|
* Generate 26 characters.
|
2010-09-17 08:48:06 -07:00
|
|
|
*/
|
2010-11-29 16:41:17 -08:00
|
|
|
generatePassphrase: function generatePassphrase() {
|
|
|
|
// Note that this is a different base32 alphabet to the one we use for
|
|
|
|
// other tasks. It's lowercase, uses different letters, and needs to be
|
|
|
|
// decoded with decodeKeyBase32, not just decodeBase32.
|
2012-04-16 12:19:01 -07:00
|
|
|
return Utils.encodeKeyBase32(CryptoUtils.generateRandomBytes(16));
|
2010-09-17 08:48:06 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-11-29 16:41:17 -08:00
|
|
|
* The following are the methods supported for UI use:
|
|
|
|
*
|
|
|
|
* * isPassphrase:
|
|
|
|
* determines whether a string is either a normalized or presentable
|
|
|
|
* passphrase.
|
|
|
|
* * hyphenatePassphrase:
|
|
|
|
* present a normalized passphrase for display. This might actually
|
|
|
|
* perform work beyond just hyphenation; sorry.
|
|
|
|
* * hyphenatePartialPassphrase:
|
|
|
|
* present a fragment of a normalized passphrase for display.
|
|
|
|
* * normalizePassphrase:
|
|
|
|
* take a presentable passphrase and reduce it to a normalized
|
|
|
|
* representation for storage. normalizePassphrase can safely be called
|
|
|
|
* on normalized input.
|
2010-12-21 15:32:58 -08:00
|
|
|
* * normalizeAccount:
|
|
|
|
* take user input for account/username, cleaning up appropriately.
|
2010-09-17 08:48:06 -07:00
|
|
|
*/
|
2010-11-29 16:41:17 -08:00
|
|
|
|
|
|
|
isPassphrase: function(s) {
|
|
|
|
if (s) {
|
|
|
|
return /^[abcdefghijkmnpqrstuvwxyz23456789]{26}$/.test(Utils.normalizePassphrase(s));
|
|
|
|
}
|
|
|
|
return false;
|
2010-09-17 08:48:06 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-11-29 16:41:17 -08:00
|
|
|
* Hyphenate a passphrase (26 characters) into groups.
|
|
|
|
* abbbbccccddddeeeeffffggggh
|
|
|
|
* =>
|
|
|
|
* a-bbbbc-cccdd-ddeee-effff-ggggh
|
2010-09-17 08:48:06 -07:00
|
|
|
*/
|
2010-11-29 16:41:17 -08:00
|
|
|
hyphenatePassphrase: function hyphenatePassphrase(passphrase) {
|
|
|
|
// For now, these are the same.
|
|
|
|
return Utils.hyphenatePartialPassphrase(passphrase, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
hyphenatePartialPassphrase: function hyphenatePartialPassphrase(passphrase, omitTrailingDash) {
|
|
|
|
if (!passphrase)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Get the raw data input. Just base32.
|
|
|
|
let data = passphrase.toLowerCase().replace(/[^abcdefghijkmnpqrstuvwxyz23456789]/g, "");
|
|
|
|
|
|
|
|
// This is the neatest way to do this.
|
|
|
|
if ((data.length == 1) && !omitTrailingDash)
|
|
|
|
return data + "-";
|
|
|
|
|
|
|
|
// Hyphenate it.
|
|
|
|
let y = data.substr(0,1);
|
|
|
|
let z = data.substr(1).replace(/(.{1,5})/g, "-$1");
|
|
|
|
|
|
|
|
// Correct length? We're done.
|
|
|
|
if ((z.length == 30) || omitTrailingDash)
|
|
|
|
return y + z;
|
|
|
|
|
|
|
|
// Add a trailing dash if appropriate.
|
|
|
|
return (y + z.replace(/([^-]{5})$/, "$1-")).substr(0, SYNC_KEY_HYPHENATED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
normalizePassphrase: function normalizePassphrase(pp) {
|
|
|
|
// Short var name... have you seen the lines below?!
|
2010-12-20 14:46:16 -08:00
|
|
|
// Allow leading and trailing whitespace.
|
2010-12-28 10:33:23 -08:00
|
|
|
pp = pp.trim().toLowerCase();
|
2010-12-20 10:10:37 -08:00
|
|
|
|
|
|
|
// 20-char sync key.
|
|
|
|
if (pp.length == 23 &&
|
|
|
|
[5, 11, 17].every(function(i) pp[i] == '-')) {
|
|
|
|
|
|
|
|
return pp.slice(0, 5) + pp.slice(6, 11)
|
|
|
|
+ pp.slice(12, 17) + pp.slice(18, 23);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "Modern" 26-char key.
|
|
|
|
if (pp.length == 31 &&
|
|
|
|
[1, 7, 13, 19, 25].every(function(i) pp[i] == '-')) {
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
return pp.slice(0, 1) + pp.slice(2, 7)
|
|
|
|
+ pp.slice(8, 13) + pp.slice(14, 19)
|
|
|
|
+ pp.slice(20, 25) + pp.slice(26, 31);
|
2010-12-20 10:10:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Something else -- just return.
|
2010-09-17 08:48:06 -07:00
|
|
|
return pp;
|
|
|
|
},
|
2010-12-21 15:32:58 -08:00
|
|
|
|
|
|
|
normalizeAccount: function normalizeAccount(acc) {
|
2010-12-28 10:33:23 -08:00
|
|
|
return acc.trim();
|
2010-12-21 15:32:58 -08:00
|
|
|
},
|
2010-09-17 08:48:06 -07:00
|
|
|
|
2009-09-10 23:11:33 -07:00
|
|
|
/**
|
2011-05-30 15:57:33 -07:00
|
|
|
* Create an array like the first but without elements of the second. Reuse
|
|
|
|
* arrays if possible.
|
2009-09-10 23:11:33 -07:00
|
|
|
*/
|
|
|
|
arraySub: function arraySub(minuend, subtrahend) {
|
2011-05-30 15:57:33 -07:00
|
|
|
if (!minuend.length || !subtrahend.length)
|
|
|
|
return minuend;
|
2009-09-10 23:11:33 -07:00
|
|
|
return minuend.filter(function(i) subtrahend.indexOf(i) == -1);
|
|
|
|
},
|
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
/**
|
2011-05-30 15:57:33 -07:00
|
|
|
* Build the union of two arrays. Reuse arrays if possible.
|
2011-01-28 09:39:12 -08:00
|
|
|
*/
|
|
|
|
arrayUnion: function arrayUnion(foo, bar) {
|
2011-05-30 15:57:33 -07:00
|
|
|
if (!foo.length)
|
|
|
|
return bar;
|
|
|
|
if (!bar.length)
|
|
|
|
return foo;
|
2011-01-28 09:39:12 -08:00
|
|
|
return foo.concat(Utils.arraySub(bar, foo));
|
|
|
|
},
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
bind2: function Async_bind2(object, method) {
|
2008-05-23 11:05:42 -07:00
|
|
|
return function innerBind() { return method.apply(object, arguments); };
|
2008-03-07 01:56:36 -08:00
|
|
|
},
|
|
|
|
|
2010-04-08 14:03:10 -07:00
|
|
|
mpLocked: function mpLocked() {
|
2011-02-22 16:09:43 -08:00
|
|
|
let modules = Cc["@mozilla.org/security/pkcs11moduledb;1"]
|
|
|
|
.getService(Ci.nsIPKCS11ModuleDB);
|
2010-04-08 14:03:10 -07:00
|
|
|
let sdrSlot = modules.findSlotByName("");
|
|
|
|
let status = sdrSlot.status;
|
|
|
|
let slots = Ci.nsIPKCS11Slot;
|
|
|
|
|
2010-08-31 08:58:35 -07:00
|
|
|
if (status == slots.SLOT_READY || status == slots.SLOT_LOGGED_IN
|
|
|
|
|| status == slots.SLOT_UNINITIALIZED)
|
2010-04-08 14:03:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (status == slots.SLOT_NOT_LOGGED_IN)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// something wacky happened, pretend MP is locked
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2011-01-13 16:56:10 -08:00
|
|
|
// If Master Password is enabled and locked, present a dialog to unlock it.
|
|
|
|
// Return whether the system is unlocked.
|
|
|
|
ensureMPUnlocked: function ensureMPUnlocked() {
|
2011-10-26 13:46:40 -07:00
|
|
|
if (!Utils.mpLocked()) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-22 16:09:43 -08:00
|
|
|
let sdr = Cc["@mozilla.org/security/sdr;1"]
|
|
|
|
.getService(Ci.nsISecretDecoderRing);
|
2011-01-13 16:56:10 -08:00
|
|
|
try {
|
|
|
|
sdr.encryptString("bacon");
|
2011-02-22 16:09:43 -08:00
|
|
|
return true;
|
2011-01-13 16:56:10 -08:00
|
|
|
} catch(e) {}
|
2011-02-22 16:09:43 -08:00
|
|
|
return false;
|
2011-01-13 16:56:10 -08:00
|
|
|
},
|
|
|
|
|
2011-06-27 06:22:49 -07:00
|
|
|
/**
|
|
|
|
* Return a value for a backoff interval. Maximum is eight hours, unless
|
|
|
|
* Status.backoffInterval is higher.
|
|
|
|
*
|
|
|
|
*/
|
2012-03-22 15:49:50 -07:00
|
|
|
calculateBackoff: function calculateBackoff(attempts, baseInterval,
|
|
|
|
statusInterval) {
|
2011-06-27 06:22:49 -07:00
|
|
|
let backoffInterval = attempts *
|
2012-03-22 15:49:50 -07:00
|
|
|
(Math.floor(Math.random() * baseInterval) +
|
|
|
|
baseInterval);
|
|
|
|
return Math.max(Math.min(backoffInterval, MAXIMUM_BACKOFF_INTERVAL),
|
|
|
|
statusInterval);
|
|
|
|
},
|
2009-01-06 13:54:18 -08:00
|
|
|
};
|
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(Utils, "_utf8Converter", function() {
|
2011-03-01 14:29:41 -08:00
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
return converter;
|
|
|
|
});
|
2010-03-02 11:07:30 -08:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
/*
|
|
|
|
* Commonly-used services
|
|
|
|
*/
|
|
|
|
let Svc = {};
|
2009-02-10 00:52:05 -08:00
|
|
|
Svc.Prefs = new Preferences(PREFS_BRANCH);
|
2010-06-16 15:11:40 -07:00
|
|
|
Svc.DefaultPrefs = new Preferences({branch: PREFS_BRANCH, defaultBranch: true});
|
2009-12-15 14:21:12 -08:00
|
|
|
Svc.Obs = Observers;
|
2010-08-17 06:44:29 -07:00
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
let _sessionCID = Services.appinfo.ID == SEAMONKEY_ID ?
|
|
|
|
"@mozilla.org/suite/sessionstore;1" :
|
|
|
|
"@mozilla.org/browser/sessionstore;1";
|
|
|
|
|
|
|
|
[["Form", "@mozilla.org/satchel/form-history;1", "nsIFormHistory2"],
|
2009-07-17 16:55:15 -07:00
|
|
|
["Idle", "@mozilla.org/widget/idleservice;1", "nsIIdleService"],
|
2011-05-19 18:08:35 -07:00
|
|
|
["Session", _sessionCID, "nsISessionStore"]
|
|
|
|
].forEach(function([name, contract, iface]) {
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(Svc, name, contract, iface);
|
|
|
|
});
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2011-05-25 16:40:09 -07:00
|
|
|
// nsIPrivateBrowsingService is not implemented in mobile Firefox.
|
|
|
|
// Svc.Private should just return undefined in this case instead of throwing.
|
|
|
|
XPCOMUtils.defineLazyGetter(Svc, "Private", function() {
|
|
|
|
try {
|
|
|
|
return Cc["@mozilla.org/privatebrowsing;1"].getService(Ci["nsIPrivateBrowsingService"]);
|
|
|
|
} catch (e) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-10-14 12:17:35 -07:00
|
|
|
Svc.__defineGetter__("Crypto", function() {
|
|
|
|
let cryptoSvc;
|
2011-04-07 15:31:11 -07:00
|
|
|
let ns = {};
|
|
|
|
Cu.import("resource://services-crypto/WeaveCrypto.js", ns);
|
|
|
|
cryptoSvc = new ns.WeaveCrypto();
|
2010-10-14 12:17:35 -07:00
|
|
|
delete Svc.Crypto;
|
|
|
|
return Svc.Crypto = cryptoSvc;
|
|
|
|
});
|
|
|
|
|
2009-08-18 20:03:11 -07:00
|
|
|
let Str = {};
|
2011-05-19 18:08:35 -07:00
|
|
|
["errors", "sync"].forEach(function(lazy) {
|
|
|
|
XPCOMUtils.defineLazyGetter(Str, lazy, Utils.lazyStrings(lazy));
|
|
|
|
});
|
2010-08-10 16:54:46 -07:00
|
|
|
|
|
|
|
Svc.Obs.add("xpcom-shutdown", function () {
|
|
|
|
for (let name in Svc)
|
|
|
|
delete Svc[name];
|
|
|
|
});
|