зеркало из https://github.com/mozilla/pjs.git
add openssl support
This commit is contained in:
Родитель
962a1a30a6
Коммит
58454da1d0
|
@ -38,7 +38,7 @@ const EXPORTED_SYMBOLS = ['WEAVE_VERSION', 'STORAGE_FORMAT_VERSION',
|
|||
'PREFS_PRANCH',
|
||||
'MODE_RDONLY', 'MODE_WRONLY',
|
||||
'MODE_CREATE', 'MODE_APPEND', 'MODE_TRUNCATE',
|
||||
'PERMS_FILE', 'PERMS_DIRECTORY',
|
||||
'PERMS_FILE', 'PERMS_PASSFILE', 'PERMS_DIRECTORY',
|
||||
'ONE_BYTE', 'ONE_KILOBYTE', 'ONE_MEGABYTE'];
|
||||
|
||||
const WEAVE_VERSION = "0.1.16";
|
||||
|
@ -53,6 +53,7 @@ const MODE_APPEND = 0x10;
|
|||
const MODE_TRUNCATE = 0x20;
|
||||
|
||||
const PERMS_FILE = 0644;
|
||||
const PERMS_PASSFILE = 0600;
|
||||
const PERMS_DIRECTORY = 0755;
|
||||
|
||||
const ONE_BYTE = 1;
|
||||
|
|
|
@ -139,12 +139,16 @@ WeaveCrypto.prototype = {
|
|||
if (!algorithm)
|
||||
algorithm = this.defaultAlgorithm;
|
||||
|
||||
if (algorithm != "none")
|
||||
this._log.debug("Encrypting data");
|
||||
|
||||
switch (algorithm) {
|
||||
case "none":
|
||||
ret = data;
|
||||
break;
|
||||
|
||||
case "XXXTEA": // Weave 0.1.12.10 and below had this typo
|
||||
case "XXTEA": {
|
||||
this._log.debug("Encrypting data");
|
||||
let gen = this._xxtea.encrypt(data, identity.password);
|
||||
ret = gen.next();
|
||||
while (typeof(ret) == "object") {
|
||||
|
@ -153,12 +157,23 @@ WeaveCrypto.prototype = {
|
|||
ret = gen.next();
|
||||
}
|
||||
gen.close();
|
||||
this._log.debug("Done encrypting data");
|
||||
} break;
|
||||
|
||||
case "aes-128-cbc":
|
||||
case "aes-192-cbc":
|
||||
case "aes-256-cbc":
|
||||
case "bf-cbc":
|
||||
case "des-ede3-cbc":
|
||||
ret = openssl("-e", algorithm, data, identity.password);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw "Unknown encryption algorithm: " + algorithm;
|
||||
}
|
||||
|
||||
if (algorithm != "none")
|
||||
this._log.debug("Done encrypting data");
|
||||
|
||||
} catch (e) {
|
||||
this._log.error("Exception caught: " + (e.message? e.message : e));
|
||||
|
||||
|
@ -180,13 +195,16 @@ WeaveCrypto.prototype = {
|
|||
if (!algorithm)
|
||||
algorithm = this.defaultAlgorithm;
|
||||
|
||||
if (algorithm != "none")
|
||||
this._log.debug("Decrypting data");
|
||||
|
||||
switch (algorithm) {
|
||||
case "none":
|
||||
ret = data;
|
||||
break;
|
||||
|
||||
case "XXXTEA": // Weave 0.1.12.10 and below had this typo
|
||||
case "XXTEA": {
|
||||
this._log.debug("Decrypting data");
|
||||
let gen = this._xxtea.decrypt(data, identity.password);
|
||||
ret = gen.next();
|
||||
while (typeof(ret) == "object") {
|
||||
|
@ -195,12 +213,23 @@ WeaveCrypto.prototype = {
|
|||
ret = gen.next();
|
||||
}
|
||||
gen.close();
|
||||
this._log.debug("Done decrypting data");
|
||||
} break;
|
||||
|
||||
case "aes-128-cbc":
|
||||
case "aes-192-cbc":
|
||||
case "aes-256-cbc":
|
||||
case "bf-cbc":
|
||||
case "des-ede3-cbc":
|
||||
ret = openssl("-d", algorithm, data, identity.password);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw "Unknown encryption algorithm: " + algorithm;
|
||||
}
|
||||
|
||||
if (algorithm != "none")
|
||||
this._log.debug("Done decrypting data");
|
||||
|
||||
} catch (e) {
|
||||
this._log.error("Exception caught: " + (e.message? e.message : e));
|
||||
|
||||
|
@ -212,3 +241,62 @@ WeaveCrypto.prototype = {
|
|||
this._log.warn("generator not properly closed");
|
||||
}
|
||||
};
|
||||
|
||||
function openssl(op, algorithm, input, password) {
|
||||
let extMgr = Components.classes["@mozilla.org/extensions/manager;1"]
|
||||
.getService(Components.interfaces.nsIExtensionManager);
|
||||
let loc = extMgr.getInstallLocation("{340c2bbc-ce74-4362-90b5-7c26312808ef}");
|
||||
|
||||
let wrap = loc.getItemLocation("{340c2bbc-ce74-4362-90b5-7c26312808ef}");
|
||||
wrap.append("openssl");
|
||||
let bin;
|
||||
|
||||
let os = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
|
||||
switch(os) {
|
||||
case "WINNT":
|
||||
wrap.append("win32");
|
||||
wrap.append("exec.bat");
|
||||
bin = wrap.parent.path + "\openssl.exe";
|
||||
dump("FIXME TESTING path: " + bin + "\n");
|
||||
break;
|
||||
case "Linux":
|
||||
case "Darwin":
|
||||
wrap.append("unix");
|
||||
wrap.append("exec.sh");
|
||||
bin = "openssl";
|
||||
break;
|
||||
default:
|
||||
throw "encryption not supported on this platform: " + os;
|
||||
}
|
||||
|
||||
let inputFile = getTmp("input");
|
||||
let [inputFOS] = open(inputFile, ">");
|
||||
inputFOS.write(input, input.length);
|
||||
inputFOS.close();
|
||||
|
||||
let outputFile = getTmp("output");
|
||||
if (outputFile.exists())
|
||||
outputFile.remove(false);
|
||||
|
||||
let passFile = getTmp("pass");
|
||||
let [passFOS] = open(passFile, ">", PERMS_PASSFILE);
|
||||
passFOS.write(password, password.length);
|
||||
passFOS.close();
|
||||
|
||||
try {
|
||||
runCmd(wrap, getTmp().path, bin, algorithm, op, "-a", "-salt",
|
||||
"-in", "input", "-out", "output", "-pass", "file:pass");
|
||||
} catch (e) {
|
||||
throw e;
|
||||
} finally {
|
||||
passFile.remove(false);
|
||||
inputFile.remove(false);
|
||||
}
|
||||
|
||||
let [outputFIS] = open(outputFile, "<");
|
||||
let ret = readStream(outputFIS);
|
||||
outputFIS.close();
|
||||
outputFile.remove(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -247,15 +247,10 @@ WeaveSyncService.prototype = {
|
|||
dapp.level = Log4Moz.Level.All;
|
||||
root.addAppender(dapp);
|
||||
|
||||
let logFile = this._dirSvc.get("ProfD", Ci.nsIFile);
|
||||
|
||||
let brief = this._dirSvc.get("ProfD", Ci.nsIFile);
|
||||
brief.QueryInterface(Ci.nsILocalFile);
|
||||
|
||||
brief.append("weave");
|
||||
if (!brief.exists())
|
||||
brief.create(brief.DIRECTORY_TYPE, PERMS_DIRECTORY);
|
||||
|
||||
brief.append("logs");
|
||||
if (!brief.exists())
|
||||
brief.create(brief.DIRECTORY_TYPE, PERMS_DIRECTORY);
|
||||
|
|
|
@ -35,7 +35,9 @@
|
|||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
const EXPORTED_SYMBOLS = ['deepEquals', 'makeFile', 'makeURI', 'xpath',
|
||||
'bind2', 'generatorAsync', 'generatorDone', 'EventListener'];
|
||||
'bind2', 'generatorAsync', 'generatorDone',
|
||||
'EventListener',
|
||||
'runCmd', 'getTmp', 'open', 'readStream'];
|
||||
|
||||
const Cc = Components.classes;
|
||||
const Ci = Components.interfaces;
|
||||
|
@ -190,3 +192,95 @@ EventListener.prototype = {
|
|||
this._handler(timer);
|
||||
}
|
||||
};
|
||||
|
||||
function runCmd() {
|
||||
var binary;
|
||||
var args = [];
|
||||
|
||||
for (let i = 0; i < arguments.length; ++i) {
|
||||
args.push(arguments[i]);
|
||||
}
|
||||
|
||||
if (args[0] instanceof Ci.nsIFile) {
|
||||
binary = args.shift();
|
||||
} else {
|
||||
binary = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
||||
binary.initWithPath(args.shift());
|
||||
}
|
||||
|
||||
var p = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
|
||||
p.init(binary);
|
||||
|
||||
p.run(true, args, args.length);
|
||||
return p.exitValue;
|
||||
}
|
||||
|
||||
function getTmp(name) {
|
||||
let ds = Cc["@mozilla.org/file/directory_service;1"].
|
||||
getService(Ci.nsIProperties);
|
||||
|
||||
let tmp = ds.get("ProfD", Ci.nsIFile);
|
||||
tmp.QueryInterface(Ci.nsILocalFile);
|
||||
|
||||
tmp.append("weave");
|
||||
tmp.append("tmp");
|
||||
if (!tmp.exists())
|
||||
tmp.create(tmp.DIRECTORY_TYPE, PERMS_DIRECTORY);
|
||||
|
||||
if (name)
|
||||
tmp.append(name);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
function open(pathOrFile, mode, perms) {
|
||||
let stream, file;
|
||||
|
||||
if (pathOrFile instanceof Ci.nsIFile) {
|
||||
file = pathOrFile;
|
||||
} else {
|
||||
file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
||||
file.initWithPath(pathOrFile);
|
||||
}
|
||||
|
||||
if (!perms)
|
||||
perms = PERMS_FILE;
|
||||
|
||||
switch(mode) {
|
||||
case "<": {
|
||||
if (!file.exists())
|
||||
throw "Cannot open file for reading, file does not exist";
|
||||
stream = Cc["@mozilla.org/network/file-input-stream;1"].
|
||||
createInstance(Ci.nsIFileInputStream);
|
||||
stream.init(file, MODE_RDONLY, perms, 0);
|
||||
stream.QueryInterface(Ci.nsILineInputStream);
|
||||
} break;
|
||||
|
||||
case ">": {
|
||||
stream = Cc["@mozilla.org/network/file-output-stream;1"].
|
||||
createInstance(Ci.nsIFileOutputStream);
|
||||
stream.init(file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, perms, 0);
|
||||
} break;
|
||||
|
||||
case ">>": {
|
||||
stream = Cc["@mozilla.org/network/file-output-stream;1"].
|
||||
createInstance(Ci.nsIFileOutputStream);
|
||||
stream.init(file, MODE_WRONLY | MODE_CREATE | MODE_APPEND, perms, 0);
|
||||
} break;
|
||||
|
||||
default:
|
||||
throw "Illegal mode to open(): " + mode;
|
||||
}
|
||||
|
||||
return [stream, file];
|
||||
}
|
||||
|
||||
function readStream(fis) {
|
||||
let data = "";
|
||||
while (fis.available()) {
|
||||
let ret = {};
|
||||
fis.readLine(ret);
|
||||
data += ret.value;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче