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>
|
2008-06-23 16:23:57 -07:00
|
|
|
* Justin Dolske <dolske@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 ***** */
|
|
|
|
|
2008-03-25 15:14:00 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Crypto'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
2008-03-07 01:56:36 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
Function.prototype.async = Async.sugar;
|
2007-12-26 17:40:46 -08:00
|
|
|
|
2008-03-25 15:14:00 -07:00
|
|
|
Utils.lazy(this, 'Crypto', CryptoSvc);
|
|
|
|
|
|
|
|
function CryptoSvc() {
|
2007-12-10 21:38:53 -08:00
|
|
|
this._init();
|
|
|
|
}
|
2008-03-25 15:14:00 -07:00
|
|
|
CryptoSvc.prototype = {
|
2007-12-10 21:38:53 -08:00
|
|
|
_logName: "Crypto",
|
|
|
|
|
|
|
|
__os: null,
|
|
|
|
get _os() {
|
|
|
|
if (!this.__os)
|
|
|
|
this.__os = Cc["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Ci.nsIObserverService);
|
|
|
|
return this.__os;
|
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
__crypto: null,
|
|
|
|
get _crypto() {
|
|
|
|
if (!this.__crypto)
|
|
|
|
this.__crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
|
|
|
createInstance(Ci.IWeaveCrypto);
|
|
|
|
return this.__crypto;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
get defaultAlgorithm() {
|
2008-06-18 18:04:01 -07:00
|
|
|
return Utils.prefs.getCharPref("encryption");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
set defaultAlgorithm(value) {
|
2008-06-18 18:04:01 -07:00
|
|
|
if (value != Utils.prefs.getCharPref("encryption"))
|
|
|
|
Utils.prefs.setCharPref("encryption", value);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_init: function Crypto__init() {
|
2008-11-03 15:00:38 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Service." + this._logName);
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.service.crypto")];
|
2007-12-10 21:38:53 -08:00
|
|
|
let branch = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefBranch2);
|
2007-12-20 14:46:12 -08:00
|
|
|
branch.addObserver("extensions.weave.encryption", this, false);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function Sync_observe(subject, topic, data) {
|
|
|
|
switch (topic) {
|
2008-01-07 16:28:57 -08:00
|
|
|
case "extensions.weave.encryption": {
|
2008-07-17 20:39:55 -07:00
|
|
|
if (Utils.pref.getCharPref("encryption") == data)
|
2008-06-18 13:25:58 -07:00
|
|
|
return;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
switch (data) {
|
2008-06-18 13:25:58 -07:00
|
|
|
case "none":
|
|
|
|
this._log.info("Encryption disabled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this._log.warn("Unknown encryption algorithm, resetting");
|
|
|
|
branch.clearUserPref("extensions.weave.encryption");
|
|
|
|
return; // otherwise we'll send the alg changed event twice
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
// FIXME: listen to this bad boy somewhere
|
|
|
|
this._os.notifyObservers(null, "weave:encryption:algorithm-changed", "");
|
2008-01-07 16:28:57 -08:00
|
|
|
} break;
|
2007-12-10 21:38:53 -08:00
|
|
|
default:
|
|
|
|
this._log.warn("Unknown encryption preference changed - ignoring");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-07-17 20:39:55 -07:00
|
|
|
checkModule: function Crypto_checkModule() {
|
|
|
|
let ok = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let svc = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
|
|
|
createInstance(Ci.IWeaveCrypto);
|
|
|
|
let iv = svc.generateRandomIV();
|
|
|
|
if (iv.length == 24)
|
|
|
|
ok = true;
|
|
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// Crypto
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
encryptData: function Crypto_encryptData(data, identity) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-26 17:40:46 -08:00
|
|
|
let ret;
|
2007-12-26 16:10:23 -08:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
this._log.trace("encrypt called. [id=" + identity.realm + "]");
|
2008-02-05 14:15:53 -08:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
if ("none" == this.defaultAlgorithm) {
|
|
|
|
this._log.debug("NOT encrypting data");
|
2008-03-07 01:56:36 -08:00
|
|
|
ret = data;
|
2008-06-23 16:23:57 -07:00
|
|
|
} else {
|
|
|
|
let symkey = identity.bulkKey;
|
|
|
|
let iv = identity.bulkIV;
|
|
|
|
ret = this._crypto.encrypt(data, symkey, iv);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-07 01:56:36 -08:00
|
|
|
|
|
|
|
self.done(ret);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
decryptData: function Crypto_decryptData(data, identity) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-26 17:40:46 -08:00
|
|
|
let ret;
|
2007-12-26 16:10:23 -08:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
this._log.trace("decrypt called. [id=" + identity.realm + "]");
|
2008-02-05 14:15:53 -08:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
if ("none" == this.defaultAlgorithm) {
|
|
|
|
this._log.debug("NOT decrypting data");
|
2008-03-07 01:56:36 -08:00
|
|
|
ret = data;
|
2008-06-23 16:23:57 -07:00
|
|
|
} else {
|
|
|
|
let symkey = identity.bulkKey;
|
|
|
|
let iv = identity.bulkIV;
|
|
|
|
ret = this._crypto.decrypt(data, symkey, iv);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-07 01:56:36 -08:00
|
|
|
|
|
|
|
self.done(ret);
|
2008-02-19 11:39:39 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
/*
|
|
|
|
* randomKeyGen
|
|
|
|
*
|
|
|
|
* Generates a random symmetric key and IV, and puts them in the specified
|
|
|
|
* identity.
|
|
|
|
*/
|
|
|
|
randomKeyGen: function Crypto_randomKeyGen(identity) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
this._log.trace("randomKeyGen called. [id=" + identity.realm + "]");
|
|
|
|
|
|
|
|
let symkey = this._crypto.generateRandomKey();
|
|
|
|
let iv = this._crypto.generateRandomIV();
|
|
|
|
|
|
|
|
identity.bulkKey = symkey;
|
|
|
|
identity.bulkIV = iv;
|
2008-02-19 11:39:39 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
/*
|
|
|
|
* RSAkeygen
|
|
|
|
*
|
|
|
|
* Generates a new RSA keypair, as well as the salt/IV used for protecting
|
|
|
|
* the private key, and puts them in the specified identity.
|
|
|
|
*/
|
2008-03-19 15:17:04 -07:00
|
|
|
RSAkeygen: function Crypto_RSAkeygen(identity) {
|
|
|
|
let self = yield;
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
this._log.trace("RSAkeygen called. [id=" + identity.realm + "]");
|
|
|
|
let privOut = {};
|
|
|
|
let pubOut = {};
|
|
|
|
|
|
|
|
// Generate a blob of random data for salting the passphrase used to
|
|
|
|
// encrypt the private key.
|
|
|
|
let salt = this._crypto.generateRandomBytes(32);
|
|
|
|
let iv = this._crypto.generateRandomIV();
|
|
|
|
|
|
|
|
this._crypto.generateKeypair(identity.password,
|
|
|
|
salt, iv,
|
|
|
|
pubOut, privOut);
|
|
|
|
|
|
|
|
identity.keypairAlg = "RSA";
|
|
|
|
identity.pubkey = pubOut.value;
|
|
|
|
identity.privkey = privOut.value;
|
|
|
|
identity.passphraseSalt = salt;
|
|
|
|
identity.privkeyWrapIV = iv;
|
2008-02-19 11:39:39 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
wrapKey : function Crypto_wrapKey(data, identity) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
this._log.trace("wrapKey called. [id=" + identity.realm + "]");
|
|
|
|
let ret = this._crypto.wrapSymmetricKey(data, identity.pubkey);
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
self.done(ret);
|
2008-02-19 11:39:39 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
unwrapKey: function Crypto_unwrapKey(data, identity) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
this._log.trace("upwrapKey called. [id=" + identity.realm + "]");
|
|
|
|
let ret = this._crypto.unwrapSymmetricKey(data,
|
|
|
|
identity.privkey,
|
|
|
|
identity.password,
|
|
|
|
identity.passphraseSalt,
|
|
|
|
identity.privkeyWrapIV);
|
2008-03-07 01:56:36 -08:00
|
|
|
self.done(ret);
|
2008-06-23 16:23:57 -07:00
|
|
|
},
|
2008-06-30 14:40:11 -07:00
|
|
|
|
|
|
|
// This function tests to see if the passphrase which encrypts
|
|
|
|
// the private key for the given identity is valid.
|
|
|
|
isPassphraseValid: function Crypto_isPassphraseValid(identity) {
|
|
|
|
var self = yield;
|
|
|
|
|
|
|
|
// We do this in a somewhat roundabout way; an alternative is
|
|
|
|
// to use a hard-coded symmetric key, but even that is still
|
|
|
|
// roundabout--ideally, the IWeaveCrypto interface should
|
|
|
|
// expose some sort of functionality to make this easier,
|
|
|
|
// or perhaps it should just offer this very function. -AV
|
|
|
|
|
|
|
|
// Generate a temporary fake identity.
|
|
|
|
var idTemp = {realm: "Temporary passphrase validation"};
|
|
|
|
|
|
|
|
// Generate a random symmetric key.
|
|
|
|
this.randomKeyGen.async(Crypto, self.cb, idTemp);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
// Encrypt the symmetric key with the user's public key.
|
|
|
|
this.wrapKey.async(Crypto, self.cb, idTemp.bulkKey, identity);
|
|
|
|
let wrappedKey = yield;
|
|
|
|
let unwrappedKey;
|
|
|
|
|
|
|
|
// Decrypt the symmetric key with the user's private key.
|
|
|
|
try {
|
|
|
|
this.unwrapKey.async(Crypto, self.cb, wrappedKey, identity);
|
|
|
|
unwrappedKey = yield;
|
|
|
|
} catch (e) {
|
|
|
|
self.done(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the original symmetric key is identical to
|
|
|
|
// the decrypted version.
|
|
|
|
if (unwrappedKey != idTemp.bulkKey)
|
|
|
|
throw new Error("Unwrapped key is not identical to original key.");
|
|
|
|
|
|
|
|
self.done(true);
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
};
|