2008-11-03 14:37:51 -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 Weave.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ['PubKey', 'PrivKey',
|
|
|
|
'PubKeys', 'PrivKeys'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://weave/Observers.js");
|
|
|
|
Cu.import("resource://weave/Preferences.js");
|
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Cu.import("resource://weave/crypto.js");
|
|
|
|
Cu.import("resource://weave/base_records/wbo.js");
|
|
|
|
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
|
|
|
Utils.lazy(this, 'PubKeys', PubKeyManager);
|
|
|
|
Utils.lazy(this, 'PrivKeys', PrivKeyManager);
|
|
|
|
|
|
|
|
function PubKey(uri, authenticator) {
|
|
|
|
this._PubKey_init(uri, authenticator);
|
|
|
|
}
|
|
|
|
PubKey.prototype = {
|
|
|
|
__proto__: WBORecord.prototype,
|
2008-11-06 23:23:35 -08:00
|
|
|
_logName: "Record.PubKey",
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
_PubKey_init: function PubKey_init(uri, authenticator) {
|
|
|
|
this._WBORec_init(uri, authenticator);
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload = {
|
2008-11-06 23:23:35 -08:00
|
|
|
type: "pubkey",
|
|
|
|
key_data: null,
|
|
|
|
private_key: null
|
|
|
|
};
|
2008-11-03 14:37:51 -08:00
|
|
|
},
|
|
|
|
|
2008-12-02 14:26:18 -08:00
|
|
|
get keyData() this.payload.key_data,
|
2008-11-06 23:23:35 -08:00
|
|
|
set keyData(value) {
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload.key_data = value;
|
2008-11-06 23:23:35 -08:00
|
|
|
},
|
2008-12-02 14:26:18 -08:00
|
|
|
get _privateKeyUri() this.payload.private_key,
|
2008-11-03 14:37:51 -08:00
|
|
|
get privateKeyUri() {
|
|
|
|
if (!this.data)
|
|
|
|
return null;
|
|
|
|
// resolve
|
2008-11-06 23:23:35 -08:00
|
|
|
let uri = this.uri.resolve(this._privateKeyUri);
|
2008-11-03 14:37:51 -08:00
|
|
|
if (uri)
|
|
|
|
return Utils.makeURI(uri);
|
|
|
|
// does not resolve, return raw (this uri type might not be able to resolve)
|
2008-11-06 23:23:35 -08:00
|
|
|
return Utils.makeURI(this._privateKeyUri);
|
2008-11-08 02:00:33 -08:00
|
|
|
},
|
|
|
|
set privateKeyUri(value) {
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload.private_key = value;
|
2008-11-08 02:00:33 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get publicKeyUri() {
|
|
|
|
throw "attempted to get public key url from a public key!";
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PrivKey(uri, authenticator) {
|
|
|
|
this._PrivKey_init(uri, authenticator);
|
|
|
|
}
|
|
|
|
PrivKey.prototype = {
|
|
|
|
__proto__: WBORecord.prototype,
|
2008-11-06 23:23:35 -08:00
|
|
|
_logName: "Record.PrivKey",
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
_PrivKey_init: function PrivKey_init(uri, authenticator) {
|
|
|
|
this._WBORec_init(uri, authenticator);
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload = {
|
2008-11-06 23:23:35 -08:00
|
|
|
type: "privkey",
|
2008-11-08 02:00:33 -08:00
|
|
|
salt: null,
|
|
|
|
iv: null,
|
2008-11-06 23:23:35 -08:00
|
|
|
key_data: null,
|
2008-11-08 02:00:33 -08:00
|
|
|
public_key: null
|
2008-11-06 23:23:35 -08:00
|
|
|
};
|
2008-11-03 14:37:51 -08:00
|
|
|
},
|
|
|
|
|
2008-12-02 14:26:18 -08:00
|
|
|
get salt() this.payload.salt,
|
2008-11-08 02:00:33 -08:00
|
|
|
set salt(value) {
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload.salt = value;
|
2008-11-08 02:00:33 -08:00
|
|
|
},
|
2008-12-02 14:26:18 -08:00
|
|
|
get iv() this.payload.iv,
|
2008-11-08 02:00:33 -08:00
|
|
|
set iv(value) {
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload.iv = value;
|
2008-11-08 02:00:33 -08:00
|
|
|
},
|
2008-12-02 14:26:18 -08:00
|
|
|
get keyData() this.payload.key_data,
|
2008-11-06 23:23:35 -08:00
|
|
|
set keyData(value) {
|
2008-12-02 14:26:18 -08:00
|
|
|
this.payload.key_data = value;
|
2008-11-06 23:23:35 -08:00
|
|
|
},
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
get publicKeyUri() {
|
|
|
|
if (!this.data)
|
|
|
|
return null;
|
|
|
|
// resolve
|
2008-11-08 02:00:33 -08:00
|
|
|
let uri = this.uri.resolve(this.payload.public_key);
|
2008-11-03 14:37:51 -08:00
|
|
|
if (uri)
|
|
|
|
return Utils.makeURI(uri);
|
|
|
|
// does not resolve, return raw (this uri type might not be able to resolve)
|
2008-11-08 02:00:33 -08:00
|
|
|
return Utils.makeURI(this.payload.public_key);
|
|
|
|
},
|
|
|
|
set publicKeyUri(value) {
|
|
|
|
this.payload.public_key = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get privateKeyUri() {
|
|
|
|
throw "attempted to get private key url from a private key!";
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-06 23:23:35 -08:00
|
|
|
// XXX unused/unfinished
|
2008-11-03 14:37:51 -08:00
|
|
|
function SymKey(keyData, wrapped) {
|
|
|
|
this._init(keyData, wrapped);
|
|
|
|
}
|
|
|
|
SymKey.prototype = {
|
|
|
|
get wrapped() {
|
|
|
|
return this._wrapped;
|
|
|
|
},
|
|
|
|
|
|
|
|
_init: function SymKey__init(keyData, wrapped) {
|
|
|
|
this._data = keyData;
|
|
|
|
this._wrapped = wrapped;
|
|
|
|
},
|
|
|
|
|
|
|
|
unwrap: function SymKey_unwrap(privkey, passphrase, meta_record) {
|
|
|
|
let svc = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
|
|
|
createInstance(Ci.IWeaveCrypto);
|
|
|
|
this._data = svc.unwrapSymmetricKey(this._data,
|
|
|
|
privkey.keyData,
|
|
|
|
passphrase,
|
|
|
|
identity.passphraseSalt,
|
|
|
|
identity.privkeyWrapIV);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PubKeyManager() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
PubKeyManager.prototype = {
|
|
|
|
_keyType: PubKey,
|
|
|
|
_logName: "PubKeyManager",
|
|
|
|
|
2008-11-08 02:00:33 -08:00
|
|
|
_init: function KeyMgr__init() {
|
|
|
|
this._log = Log4Moz.repository.getLogger(this._logName);
|
|
|
|
this._keys = {};
|
|
|
|
this._aliases = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
get _crypto() {
|
|
|
|
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
|
|
|
getService(Ci.IWeaveCrypto);
|
|
|
|
this.__defineGetter__("_crypto", function() crypto);
|
|
|
|
return crypto;
|
|
|
|
},
|
|
|
|
|
2008-11-06 23:23:35 -08:00
|
|
|
get defaultKeyUri() this._defaultKeyUri,
|
2008-11-06 17:36:19 -08:00
|
|
|
set defaultKeyUri(value) { this._defaultKeyUri = value; },
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
_getDefaultKey: function KeyMgr__getDefaultKey() {
|
|
|
|
let self = yield;
|
2008-11-06 17:36:19 -08:00
|
|
|
let ret = yield this.get(self.cb, this.defaultKeyUri);
|
2008-11-03 14:37:51 -08:00
|
|
|
self.done(ret);
|
|
|
|
},
|
|
|
|
getDefaultKey: function KeyMgr_getDefaultKey(onComplete) {
|
|
|
|
return this._getDefaultKey.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2008-11-08 02:00:33 -08:00
|
|
|
createKeypair: function KeyMgr_createKeypair(passphrase, pubkeyUri, privkeyUri) {
|
2008-11-19 16:15:16 -08:00
|
|
|
this._log.debug("Generating RSA keypair");
|
2008-11-06 23:23:35 -08:00
|
|
|
let pubkey = new PubKey();
|
|
|
|
let privkey = new PrivKey();
|
2008-11-08 02:00:33 -08:00
|
|
|
privkey.salt = this._crypto.generateRandomBytes(16);
|
|
|
|
privkey.iv = this._crypto.generateRandomIV();
|
2008-11-06 23:23:35 -08:00
|
|
|
|
2008-11-08 02:00:33 -08:00
|
|
|
let pub = {}, priv = {};
|
|
|
|
this._crypto.generateKeypair(passphrase, privkey.salt, privkey.iv, pub, priv);
|
|
|
|
[pubkey.keyData, privkey.keyData] = [pub.value, priv.value];
|
|
|
|
|
|
|
|
if (pubkeyUri) {
|
|
|
|
pubkey.uri = pubkeyUri;
|
|
|
|
privkey.publicKeyUri = pubkeyUri;
|
|
|
|
}
|
|
|
|
if (privkeyUri) {
|
|
|
|
privkey.uri = privkeyUri;
|
|
|
|
pubkey.privateKeyUri = privkeyUri;
|
|
|
|
}
|
|
|
|
|
2008-11-19 16:15:16 -08:00
|
|
|
this._log.debug("Generating RSA keypair... done");
|
2008-11-08 02:00:33 -08:00
|
|
|
return {pubkey: pubkey, privkey: privkey};
|
2008-11-03 14:37:51 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_import: function KeyMgr__import(url) {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
this._log.trace("Importing key: " + (url.spec? url.spec : url));
|
|
|
|
|
|
|
|
try {
|
|
|
|
let key = new this._keyType(url);
|
2008-11-06 23:23:35 -08:00
|
|
|
yield key.get(self.cb);
|
2008-11-03 14:37:51 -08:00
|
|
|
this.set(url, key);
|
2008-11-06 23:23:35 -08:00
|
|
|
self.done(key);
|
2008-11-03 14:37:51 -08:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Failed to import key: " + e);
|
2008-11-06 23:23:35 -08:00
|
|
|
self.done(null);
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
import: function KeyMgr_import(onComplete, url) {
|
|
|
|
this._import.async(this, onComplete, url);
|
|
|
|
},
|
|
|
|
|
|
|
|
_get: function KeyMgr__get(url) {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
let key = null;
|
|
|
|
if (url in this._aliases)
|
|
|
|
url = this._aliases[url];
|
|
|
|
if (url in this._keys)
|
|
|
|
key = this._keys[url];
|
|
|
|
|
|
|
|
if (!key)
|
|
|
|
key = yield this.import(self.cb, url);
|
|
|
|
|
|
|
|
self.done(key);
|
|
|
|
},
|
|
|
|
get: function KeyMgr_get(onComplete, url) {
|
|
|
|
this._get.async(this, onComplete, url);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function KeyMgr_set(url, key) {
|
|
|
|
this._keys[url] = key;
|
|
|
|
return key;
|
|
|
|
},
|
|
|
|
|
|
|
|
contains: function KeyMgr_contains(url) {
|
|
|
|
let key = null;
|
|
|
|
if (url in this._aliases)
|
|
|
|
url = this._aliases[url];
|
|
|
|
if (url in this._keys)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
del: function KeyMgr_del(url) {
|
|
|
|
delete this._keys[url];
|
|
|
|
},
|
|
|
|
getAlias: function KeyMgr_getAlias(alias) {
|
|
|
|
return this._aliases[alias];
|
|
|
|
},
|
|
|
|
setAlias: function KeyMgr_setAlias(url, alias) {
|
|
|
|
this._aliases[alias] = url;
|
|
|
|
},
|
|
|
|
delAlias: function KeyMgr_delAlias(alias) {
|
|
|
|
delete this._aliases[alias];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PrivKeyManager() { this._init(); }
|
|
|
|
PrivKeyManager.prototype = {
|
|
|
|
__proto__: PubKeyManager.prototype,
|
|
|
|
_keyType: PrivKey,
|
|
|
|
_logName: "PrivKeyManager"
|
|
|
|
};
|