mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-24 18:55:30 +00:00
add base record types for the new weave server api
This commit is contained in:
parent
432eb0750b
commit
542bee77cd
148
services/sync/modules/base_records/crypto.js
Normal file
148
services/sync/modules/base_records/crypto.js
Normal file
@ -0,0 +1,148 @@
|
||||
/* ***** 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 = ['CryptoWrapper'];
|
||||
|
||||
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");
|
||||
Cu.import("resource://weave/base_records/keys.js");
|
||||
|
||||
Function.prototype.async = Async.sugar;
|
||||
|
||||
function CryptoWrapper(uri, authenticator) {
|
||||
this._CryptoWrap_init(uri, authenticator);
|
||||
}
|
||||
CryptoWrapper.prototype = {
|
||||
__proto__: WBORecord.prototype,
|
||||
_logName: "Record.CryptoWrapper",
|
||||
|
||||
_CryptoWrap_init: function CryptoWrap_init(uri, authenticator) {
|
||||
this._WBORec_init(uri, authenticator);
|
||||
},
|
||||
|
||||
_encrypt: function CryptoWrap__encrypt(passphrase) {
|
||||
let self = yield;
|
||||
|
||||
let pubkey = yield PubKeys.getDefaultKey(self.cb);
|
||||
let privkey = yield PrivKeys.get(self.cb, pubkey.privateKeyUri);
|
||||
|
||||
let meta = new CryptoMeta(this.data.encryption); // FIXME: cache!
|
||||
yield meta.get(self.cb);
|
||||
let symkey = meta.getKey(pubkey, privkey, passphrase);
|
||||
|
||||
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
||||
createInstance(Ci.IWeaveCrypto);
|
||||
this.data.payload = crypto.encrypt(this.cleartext, symkey, meta.iv);
|
||||
|
||||
self.done();
|
||||
},
|
||||
encrypt: function CryptoWrap_encrypt(onComplete, passphrase) {
|
||||
this._encrypt.async(this, onComplete, passphrase);
|
||||
},
|
||||
|
||||
_decrypt: function CryptoWrap__decrypt(passphrase) {
|
||||
let self = yield;
|
||||
|
||||
let pubkey = yield PubKeys.getDefaultKey(self.cb);
|
||||
let privkey = yield PrivKeys.get(self.cb, pubkey.privateKeyUri);
|
||||
|
||||
let meta = new CryptoMeta(this.data.encryption); // FIXME: cache!
|
||||
yield meta.get(self.cb);
|
||||
let symkey = meta.getKey(pubkey, privkey, passphrase);
|
||||
|
||||
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
||||
createInstance(Ci.IWeaveCrypto);
|
||||
this.cleartext = crypto.decrypt(this.data.payload, symkey, meta.iv);
|
||||
|
||||
self.done(this.cleartext);
|
||||
},
|
||||
decrypt: function CryptoWrap_decrypt(onComplete, passphrase) {
|
||||
this._decrypt.async(this, onComplete, passphrase);
|
||||
}
|
||||
};
|
||||
|
||||
function CryptoMeta(uri, authenticator) {
|
||||
this._CryptoMeta_init(uri, authenticator);
|
||||
}
|
||||
CryptoMeta.prototype = {
|
||||
__proto__: WBORecord.prototype,
|
||||
_logName: "Record.CryptoMeta",
|
||||
|
||||
_CryptoMeta_init: function CryptoMeta_init(uri, authenticator) {
|
||||
this._WBORec_init(uri, authenticator);
|
||||
},
|
||||
|
||||
get salt() this.data.payload.salt,
|
||||
set salt(value) { this.data.payload.salt = value; },
|
||||
|
||||
get iv() this.data.payload.iv,
|
||||
set iv(value) { this.data.payload.iv = value; },
|
||||
|
||||
getKey: function getKey(pubKey, privKey, passphrase) {
|
||||
let wrappedKey;
|
||||
|
||||
// get the full uri to our public key
|
||||
let pubKeyUri;
|
||||
if (typeof pubKey == 'string')
|
||||
pubKeyUri = this.uri.resolve(pubKey);
|
||||
else
|
||||
pubKeyUri = pubKey.spec;
|
||||
|
||||
// each hash key is a relative uri, resolve those and match against ours
|
||||
for (let relUri in this.data.payload.keyring) {
|
||||
if (pubKeyUri == this.uri.resolve(relUri))
|
||||
wrappedKey = this.data.payload.keyring[relUri];
|
||||
}
|
||||
if (!wrappedKey)
|
||||
throw "keyring doesn't contain a key for " + pubKeyUrl;
|
||||
|
||||
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
||||
createInstance(Ci.IWeaveCrypto);
|
||||
return crypto.unwrapSymmetricKey(wrappedKey, privKey.keyData,
|
||||
passphrase, this.salt, this.iv);
|
||||
}
|
||||
};
|
241
services/sync/modules/base_records/keys.js
Normal file
241
services/sync/modules/base_records/keys.js
Normal file
@ -0,0 +1,241 @@
|
||||
/* ***** 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,
|
||||
|
||||
_PubKey_init: function PubKey_init(uri, authenticator) {
|
||||
this._WBORec_init(uri, authenticator);
|
||||
},
|
||||
|
||||
get keyData() {
|
||||
if (!this.data)
|
||||
return null;
|
||||
return this.data.payload.key_data;
|
||||
},
|
||||
|
||||
get privateKeyUri() {
|
||||
if (!this.data)
|
||||
return null;
|
||||
// resolve
|
||||
let uri = this.uri.resolve(this.data.payload.private_key);
|
||||
if (uri)
|
||||
return Utils.makeURI(uri);
|
||||
// does not resolve, return raw (this uri type might not be able to resolve)
|
||||
return Utils.makeURI(this.data.payload.private_key);
|
||||
}
|
||||
};
|
||||
|
||||
function PrivKey(uri, authenticator) {
|
||||
this._PrivKey_init(uri, authenticator);
|
||||
}
|
||||
PrivKey.prototype = {
|
||||
__proto__: WBORecord.prototype,
|
||||
|
||||
_PrivKey_init: function PrivKey_init(uri, authenticator) {
|
||||
this._WBORec_init(uri, authenticator);
|
||||
},
|
||||
|
||||
get keyData() {
|
||||
if (!this.data)
|
||||
return null;
|
||||
return this.data.payload.key_data;
|
||||
},
|
||||
|
||||
get privateKeyUri() {
|
||||
throw "attempted to get private key url from a private key!";
|
||||
},
|
||||
|
||||
get publicKeyUri() {
|
||||
if (!this.data)
|
||||
return null;
|
||||
// resolve
|
||||
let uri = this.uri.resolve(this.data.payload.public_key);
|
||||
if (uri)
|
||||
return Utils.makeURI(uri);
|
||||
// does not resolve, return raw (this uri type might not be able to resolve)
|
||||
return Utils.makeURI(this.data.payload.public_key);
|
||||
}
|
||||
};
|
||||
|
||||
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",
|
||||
|
||||
get defaultKeyUrl() this._defaultKeyUrl,
|
||||
set defaultKeyUrl(value) { this._defaultKeyUrl = value; },
|
||||
|
||||
_getDefaultKey: function KeyMgr__getDefaultKey() {
|
||||
let self = yield;
|
||||
let ret = yield this.get(self.cb, this.defaultKeyUrl);
|
||||
self.done(ret);
|
||||
},
|
||||
getDefaultKey: function KeyMgr_getDefaultKey(onComplete) {
|
||||
return this._getDefaultKey.async(this, onComplete);
|
||||
},
|
||||
|
||||
_init: function KeyMgr__init() {
|
||||
this._log = Log4Moz.repository.getLogger(this._logName);
|
||||
this._keys = {};
|
||||
this._aliases = {};
|
||||
},
|
||||
|
||||
_import: function KeyMgr__import(url) {
|
||||
let self = yield;
|
||||
let ret = null;
|
||||
|
||||
this._log.trace("Importing key: " + (url.spec? url.spec : url));
|
||||
|
||||
try {
|
||||
let key = new this._keyType(url);
|
||||
let foo = yield key.get(self.cb);
|
||||
this.set(url, key);
|
||||
ret = key;
|
||||
} catch (e) {
|
||||
this._log.debug("Failed to import key: " + e);
|
||||
// don't do anything else, we'll just return null
|
||||
}
|
||||
|
||||
self.done(ret);
|
||||
},
|
||||
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"
|
||||
};
|
61
services/sync/modules/base_records/wbo.js
Normal file
61
services/sync/modules/base_records/wbo.js
Normal file
@ -0,0 +1,61 @@
|
||||
/* ***** 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 = ['WBORecord'];
|
||||
|
||||
const Cc = Components.classes;
|
||||
const Ci = Components.interfaces;
|
||||
const Cr = Components.results;
|
||||
const Cu = Components.utils;
|
||||
|
||||
Cu.import("resource://weave/log4moz.js");
|
||||
Cu.import("resource://weave/resource.js");
|
||||
Cu.import("resource://weave/async.js");
|
||||
|
||||
Function.prototype.async = Async.sugar;
|
||||
|
||||
function WBORecord(uri, authenticator) {
|
||||
this._WBORec_init(uri, authenticator);
|
||||
}
|
||||
WBORecord.prototype = {
|
||||
__proto__: Resource.prototype,
|
||||
_logName: "Record.WBO",
|
||||
|
||||
_WBORec_init: function WBORec_init(uri, authenticator) {
|
||||
this._init(uri, authenticator);
|
||||
this.pushFilter(new JsonFilter());
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user