tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

head.js (2369B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 var gCertDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
      6  Ci.nsIX509CertDB
      7 );
      8 
      9 /**
     10 * List of certs imported via readCertificate(). Certs in this list are
     11 * automatically deleted from the cert DB when a test including this head file
     12 * finishes.
     13 *
     14 * @type {nsIX509Cert[]}
     15 */
     16 var gImportedCerts = [];
     17 
     18 registerCleanupFunction(() => {
     19  for (let cert of gImportedCerts) {
     20    gCertDB.deleteCertificate(cert);
     21  }
     22 });
     23 
     24 // This function serves the same purpose as the one defined in head_psm.js.
     25 function pemToBase64(pem) {
     26  return pem
     27    .replace(/-----BEGIN CERTIFICATE-----/, "")
     28    .replace(/-----END CERTIFICATE-----/, "")
     29    .replace(/[\r\n]/g, "");
     30 }
     31 
     32 /**
     33 * Given the filename of a certificate, returns a promise that will resolve with
     34 * a handle to the certificate when that certificate has been read and imported
     35 * with the given trust settings.
     36 *
     37 * Certs imported via this function will automatically be deleted from the cert
     38 * DB once the calling test finishes.
     39 *
     40 * @param {string} filename
     41 *        The filename of the certificate (assumed to be in the same directory).
     42 * @param {string} trustString
     43 *        A string describing how the certificate should be trusted (see
     44 *        `certutil -A --help`).
     45 * @returns {Promise}
     46 *         A promise that will resolve with a handle to the certificate.
     47 */
     48 function readCertificate(filename, trustString) {
     49  return IOUtils.readUTF8(getTestFilePath(filename)).then(
     50    pem => {
     51      let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     52        Ci.nsIX509CertDB
     53      );
     54      let base64 = pemToBase64(pem);
     55      certdb.addCertFromBase64(base64, trustString);
     56      let cert = certdb.constructX509FromBase64(base64);
     57      gImportedCerts.push(cert);
     58      return cert;
     59    },
     60    error => {
     61      throw error;
     62    }
     63  );
     64 }
     65 
     66 /**
     67 * Asynchronously opens the certificate manager.
     68 *
     69 * @returns {Window} a handle on the opened certificate manager window
     70 */
     71 async function openCertManager() {
     72  let win = window.openDialog("chrome://pippki/content/certManager.xhtml");
     73  return new Promise(resolve => {
     74    win.addEventListener(
     75      "load",
     76      function () {
     77        executeSoon(() => resolve(win));
     78      },
     79      { once: true }
     80    );
     81  });
     82 }