move openssl() into crypto object so it can use the logger there. up version
authorthunder@h-132.office.mozilla.org
Mon, 18 Feb 2008 11:18:04 -0800
changeset 44356 938ff0803ee94c4a27a5cbd489e6992a1d40cefa
parent 44355 3263d7c57204816af845d7c4e04294f19b3b3de1
child 44357 37f9015f4be4c7c9d825ec0e89f7945317f5e3d5
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
move openssl() into crypto object so it can use the logger there. up version
services/sync/modules/crypto.js
--- a/services/sync/modules/crypto.js
+++ b/services/sync/modules/crypto.js
@@ -87,16 +87,78 @@ WeaveCrypto.prototype = {
 
   _init: function Crypto__init() {
     this._log = Log4Moz.Service.getLogger("Service." + this._logName);
     let branch = Cc["@mozilla.org/preferences-service;1"]
       .getService(Ci.nsIPrefBranch2);
     branch.addObserver("extensions.weave.encryption", this, false);
   },
 
+  _openssl: function Crypto__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";
+      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 = Utils.getTmp("input");
+    let [inputFOS] = Utils.open(inputFile, ">");
+    inputFOS.write(input, input.length);
+    inputFOS.close();
+
+    let outputFile = Utils.getTmp("output");
+    if (outputFile.exists())
+      outputFile.remove(false);
+
+    // nsIProcess doesn't support stdin, so we write a file instead
+    let passFile = Utils.getTmp("pass");
+    let [passFOS] = Utils.open(passFile, ">", PERMS_PASSFILE);
+    passFOS.write(password, password.length);
+    passFOS.close();
+
+    try {
+      this._log.debug("Running command: " + wrap.path + " " +
+                      Utils.getTmp().path + " " + bin + " " + algorithm + " " +
+                      op + "-a -salt -in input -out output -pass file:pass");
+      Utils.runCmd(wrap, Utils.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] = Utils.open(outputFile, "<");
+    let ret = Utils.readStream(outputFIS);
+    outputFIS.close();
+    //outputFile.remove(false);
+
+    return ret;
+  },
+
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
 
   // nsIObserver
 
   observe: function Sync_observe(subject, topic, data) {
     switch (topic) {
     case "extensions.weave.encryption": {
       let branch = Cc["@mozilla.org/preferences-service;1"]
@@ -159,17 +221,17 @@ WeaveCrypto.prototype = {
         gen.close();
       } 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);
+        ret = this._openssl("-e", algorithm, data, identity.password);
         break;
 
       default:
         throw "Unknown encryption algorithm: " + algorithm;
       }
 
       if (algorithm != "none")
         this._log.debug("Done encrypting data");
@@ -215,17 +277,17 @@ WeaveCrypto.prototype = {
         gen.close();
       } 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);
+        ret = this._openssl("-d", algorithm, data, identity.password);
         break;
 
       default:
         throw "Unknown encryption algorithm: " + algorithm;
       }
 
       if (algorithm != "none")
         this._log.debug("Done decrypting data");
@@ -236,67 +298,8 @@ WeaveCrypto.prototype = {
     } finally {
       timer = null;
       Utils.generatorDone(this, self, onComplete, ret);
       yield; // onComplete is responsible for closing the generator
     }
     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";
-    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 = Utils.getTmp("input");
-  let [inputFOS] = Utils.open(inputFile, ">");
-  inputFOS.write(input, input.length);
-  inputFOS.close();
-
-  let outputFile = Utils.getTmp("output");
-  if (outputFile.exists())
-    outputFile.remove(false);
-
-  // nsIProcess doesn't support stdin, so we write a file instead
-  let passFile = Utils.getTmp("pass");
-  let [passFOS] = Utils.open(passFile, ">", PERMS_PASSFILE);
-  passFOS.write(password, password.length);
-  passFOS.close();
-
-  try {
-    Utils.runCmd(wrap, Utils.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] = Utils.open(outputFile, "<");
-  let ret = Utils.readStream(outputFIS);
-  outputFIS.close();
-  outputFile.remove(false);
-
-  return ret;
-}