Use wrappers for get and set prefs

Fix some syntax issues in hmac.js
This commit is contained in:
Grégory Soutadé 2015-01-22 21:50:07 +01:00
parent e0f8670a58
commit 5cfb47c470
3 changed files with 37 additions and 25 deletions

View File

@ -22,19 +22,21 @@ var {Cc, Ci} = require("chrome");
var hmac = Cc["@mozilla.org/security/hmac;1"] var hmac = Cc["@mozilla.org/security/hmac;1"]
.createInstance(Ci.nsICryptoHMAC); .createInstance(Ci.nsICryptoHMAC);
function hmac_init(key) { exports.hmac = {
var keyObject = Cc["@mozilla.org/security/keyobjectfactory;1"] hmac_init : function (key) {
.getService(Ci.nsIKeyObjectFactory) var keyObject = Cc["@mozilla.org/security/keyobjectfactory;1"]
.keyFromString(Ci.nsIKeyObject.HMAC, key); .getService(Ci.nsIKeyObjectFactory)
hmac.init(hmac.SHA256, keyObject); .keyFromString(Ci.nsIKeyObject.HMAC, key);
} hmac.init(hmac.SHA256, keyObject);
},
function hmac_digest(message) { hmac_digest : function (message) {
var data = new Uint8Array(message.length); var data = new Uint8Array(message.length);
for(i=0; i<message.length; i++) for(i=0; i<message.length; i++)
data[i] = message.charCodeAt(i); data[i] = message.charCodeAt(i);
hmac.update(data, data.length); hmac.update(data, data.length);
res = hmac.finish(false); res = hmac.finish(false);
hmac.reset(); hmac.reset();
return res; return res;
} }
};

View File

@ -25,9 +25,9 @@ var aes = require("jsaes").aes;
var parseURI = require("parseuri").parseURI; var parseURI = require("parseuri").parseURI;
var prefSet = require("sdk/simple-prefs"); var prefSet = require("sdk/simple-prefs");
var DEBUG = false; var DEBUG = false;
var pkdbf2_level = prefSet.prefs.pkdbf2_level; var pkdbf2_level = getPref("pkdbf2_level");
var protocol_version = 3; var protocol_version = 3;
SERVER = { OK : 0, FAILED : 1, RESTART_REQUEST : 2}; SERVER = {OK : 0, FAILED : 1, RESTART_REQUEST : 2};
// http://stackoverflow.com/questions/3745666/how-to-convert-from-hex-to-ascii-in-javascript // http://stackoverflow.com/questions/3745666/how-to-convert-from-hex-to-ascii-in-javascript
function hex2a(hex) { function hex2a(hex) {
@ -63,6 +63,16 @@ function notify(text, data)
}); });
} }
function getPref(key)
{
return prefSet.prefs[key];
}
function setPref(key, value)
{
prefSet.prefs[key] = value;
}
function generate_request(domain, login, mkey) function generate_request(domain, login, mkey)
{ {
var v = "@@" + domain + ";" + login; var v = "@@" + domain + ";" + login;
@ -171,7 +181,7 @@ function ask_server(form, field, logins, domain, wdomain, mkey, salt, submit)
{ {
debug("New pkdbf2 level " + server_pkdbf2_level); debug("New pkdbf2 level " + server_pkdbf2_level);
pkdbf2_level = server_pkdbf2_level; pkdbf2_level = server_pkdbf2_level;
prefSet.prefs.pkdbf2_level = server_pkdbf2_level; setPref("pkdbf2_level", pkdbf2_level);
ret = SERVER.RESTART_REQUEST; ret = SERVER.RESTART_REQUEST;
} }
break; break;
@ -228,8 +238,8 @@ function ask_server(form, field, logins, domain, wdomain, mkey, salt, submit)
notify("Error", notify("Error",
"Error"); "Error");
}, false); }, false);
debug("connect to " + prefSet.prefs.account_url); debug("connect to " + getPref("account_url"));
gPassRequest.open("POST", prefSet.prefs.account_url, true); gPassRequest.open("POST", getPref("account_url"), true);
gPassRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); gPassRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
gPassRequest.send(keys); gPassRequest.send(keys);
@ -268,7 +278,7 @@ function on_sumbit(e)
domain = domain["host"]; domain = domain["host"];
var wdomain = wildcard_domain(domain); var wdomain = wildcard_domain(domain);
var salt = parseURI.parseUri(prefSet.prefs.account_url); var salt = parseURI.parseUri(getPref("account_url"));
salt = salt["host"] + salt["path"]; salt = salt["host"] + salt["path"];
debug("salt " + salt); debug("salt " + salt);

View File

@ -17,7 +17,7 @@
along with gPass. If not, see <http://www.gnu.org/licenses/>. along with gPass. If not, see <http://www.gnu.org/licenses/>.
*/ */
require("hmac.js"); var hmac = require("hmac.js").hmac;
exports.pkdbf2 = { exports.pkdbf2 = {
@ -28,10 +28,10 @@ exports.pkdbf2 = {
var temp_res = ""; var temp_res = "";
var temp_res2 = ""; var temp_res2 = "";
hmac_init(password); hmac.hmac_init(password);
for (i=1; result.length < outlen; i++) for (i=1; result.length < outlen; i++)
{ {
temp = hmac_digest(salt + temp = hmac.hmac_digest(salt +
String.fromCharCode((i & 0xff000000) >> 24) + String.fromCharCode((i & 0xff000000) >> 24) +
String.fromCharCode((i & 0x00ff0000) >> 16) + String.fromCharCode((i & 0x00ff0000) >> 16) +
String.fromCharCode((i & 0x0000ff00) >> 8) + String.fromCharCode((i & 0x0000ff00) >> 8) +
@ -41,7 +41,7 @@ exports.pkdbf2 = {
for(a=1; a<iterations; a++) for(a=1; a<iterations; a++)
{ {
temp2 = hmac_digest(temp); temp2 = hmac.hmac_digest(temp);
temp_res2 = ""; temp_res2 = "";
for(b = 0; b<temp_res.length; b++) for(b = 0; b<temp_res.length; b++)
temp_res2 += String.fromCharCode(temp_res.charCodeAt(b) ^ temp2.charCodeAt(b)); temp_res2 += String.fromCharCode(temp_res.charCodeAt(b) ^ temp2.charCodeAt(b));