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;
|
|
|
|
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/base_records/wbo.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-03 14:37:51 -08:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function PubKey(uri) {
|
2010-02-11 15:25:31 -08:00
|
|
|
WBORecord.call(this, uri);
|
|
|
|
this.type = "pubkey";
|
|
|
|
this.keyData = null;
|
|
|
|
this.privateKeyUri = null;
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
PubKey.prototype = {
|
|
|
|
__proto__: WBORecord.prototype,
|
2008-11-06 23:23:35 -08:00
|
|
|
_logName: "Record.PubKey",
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
get privateKeyUri() {
|
|
|
|
if (!this.data)
|
|
|
|
return null;
|
2009-04-13 14:39:29 -07:00
|
|
|
|
|
|
|
// Use the uri if it resolves, otherwise return raw (uri type unresolvable)
|
|
|
|
let key = this.payload.privateKeyUri;
|
|
|
|
return Utils.makeURI(this.uri.resolve(key) || key);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
Utils.deferGetSet(PubKey, "payload", ["keyData", "privateKeyUri", "type"]);
|
2009-04-13 14:39:29 -07:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function PrivKey(uri) {
|
2010-02-11 15:25:31 -08:00
|
|
|
WBORecord.call(this, uri);
|
|
|
|
this.type = "privkey";
|
|
|
|
this.salt = null;
|
|
|
|
this.iv = null;
|
|
|
|
this.keyData = null;
|
|
|
|
this.publicKeyUri = null;
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
PrivKey.prototype = {
|
|
|
|
__proto__: WBORecord.prototype,
|
2008-11-06 23:23:35 -08:00
|
|
|
_logName: "Record.PrivKey",
|
2008-11-03 14:37:51 -08:00
|
|
|
|
|
|
|
get publicKeyUri() {
|
|
|
|
if (!this.data)
|
|
|
|
return null;
|
2009-04-13 14:39:29 -07:00
|
|
|
|
|
|
|
// Use the uri if it resolves, otherwise return raw (uri type unresolvable)
|
|
|
|
let key = this.payload.publicKeyUri;
|
|
|
|
return Utils.makeURI(this.uri.resolve(key) || key);
|
2008-11-08 02:00:33 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get privateKeyUri() {
|
|
|
|
throw "attempted to get private key url from a private key!";
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
Utils.deferGetSet(PrivKey, "payload", ["salt", "iv", "keyData", "publicKeyUri", "type"]);
|
2009-04-13 14:39:29 -07:00
|
|
|
|
2008-11-06 23:23:35 -08:00
|
|
|
// XXX unused/unfinished
|
2008-11-03 14:37:51 -08:00
|
|
|
function SymKey(keyData, wrapped) {
|
2010-02-11 15:25:31 -08:00
|
|
|
this._data = keyData;
|
|
|
|
this._wrapped = wrapped;
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
SymKey.prototype = {
|
|
|
|
get wrapped() {
|
|
|
|
return this._wrapped;
|
|
|
|
},
|
|
|
|
|
|
|
|
unwrap: function SymKey_unwrap(privkey, passphrase, meta_record) {
|
2009-06-02 09:29:58 -07:00
|
|
|
this._data =
|
|
|
|
Svc.Crypto.unwrapSymmetricKey(this._data, privkey.keyData, passphrase,
|
|
|
|
privkey.salt, privkey.iv);
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
Utils.lazy(this, 'PubKeys', PubKeyManager);
|
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
function PubKeyManager() {
|
|
|
|
RecordManager.call(this);
|
|
|
|
}
|
2008-11-03 14:37:51 -08:00
|
|
|
PubKeyManager.prototype = {
|
2009-01-27 13:35:10 -08:00
|
|
|
__proto__: RecordManager.prototype,
|
|
|
|
_recordType: PubKey,
|
2008-11-03 14:37:51 -08:00
|
|
|
_logName: "PubKeyManager",
|
|
|
|
|
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
|
|
|
|
2009-06-04 17:04:51 -07:00
|
|
|
getDefaultKey: function PubKeyManager_getDefaultKey() {
|
|
|
|
return this.get(this.defaultKeyUri);
|
2008-11-03 14:37:51 -08:00
|
|
|
},
|
|
|
|
|
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();
|
2009-01-27 13:35:10 -08:00
|
|
|
privkey.salt = Svc.Crypto.generateRandomBytes(16);
|
|
|
|
privkey.iv = Svc.Crypto.generateRandomIV();
|
2008-11-06 23:23:35 -08:00
|
|
|
|
2008-11-08 02:00:33 -08:00
|
|
|
let pub = {}, priv = {};
|
2010-07-03 11:13:40 -07:00
|
|
|
Svc.Crypto.generateKeypair(passphrase.passwordUTF8, privkey.salt,
|
|
|
|
privkey.iv, pub, priv);
|
2008-11-08 02:00:33 -08:00
|
|
|
[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
|
|
|
},
|
|
|
|
|
2009-06-04 17:09:32 -07:00
|
|
|
uploadKeypair: function PubKeyManager_uploadKeypair(keys) {
|
|
|
|
for each (let key in keys)
|
2009-09-10 11:05:13 -07:00
|
|
|
new Resource(key.uri).put(key);
|
2008-11-03 14:37:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
Utils.lazy(this, 'PrivKeys', PrivKeyManager);
|
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
function PrivKeyManager() {
|
|
|
|
PubKeyManager.call(this);
|
|
|
|
}
|
2008-11-03 14:37:51 -08:00
|
|
|
PrivKeyManager.prototype = {
|
|
|
|
__proto__: PubKeyManager.prototype,
|
2009-01-27 13:35:10 -08:00
|
|
|
_recordType: PrivKey,
|
2008-11-03 14:37:51 -08:00
|
|
|
_logName: "PrivKeyManager"
|
|
|
|
};
|