summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/test/resources/smimeUtils.jsm
blob: db7cf2e5c913c06994433aa3ec03b6364e7bfaa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This file provides some utilities for helping run S/MIME tests.
 */

var EXPORTED_SYMBOLS = ["SmimeUtils"];

var { MockRegistrar } = ChromeUtils.importESModule(
  "resource://testing-common/MockRegistrar.sys.mjs"
);

const gCertDialogs = {
  confirmDownloadCACert: (ctx, cert, trust) => {
    dump("Requesting certificate download\n");
    trust.value = Ci.nsIX509CertDB.TRUSTED_EMAIL;
    return true;
  },
  setPKCS12FilePassword: (ctx, password) => {
    throw new Error("Not implemented");
  },
  getPKCS12FilePassword: (ctx, password) => {
    password.value = "";
    return true;
  },
  viewCert: (ctx, cert) => {
    throw new Error("Not implemented");
  },
  QueryInterface: ChromeUtils.generateQI(["nsICertificateDialogs"]),
};

var SmimeUtils = {
  ensureNSS() {
    // Ensure NSS is initialized.
    Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);

    // Set up the internal key token so that subsequent code doesn't fail. If
    // this isn't done, we'll fail to work if the NSS databases didn't already
    // exist.
    let keydb = Cc["@mozilla.org/security/pk11tokendb;1"].getService(
      Ci.nsIPK11TokenDB
    );
    try {
      keydb.getInternalKeyToken().initPassword("");
    } catch (e) {
      // In this scenario, the key token already had its password initialized.
      // Therefore, we don't need to do anything (assuming its password is
      // empty).
    }

    MockRegistrar.register("@mozilla.org/nsCertificateDialogs;1", gCertDialogs);
  },

  loadPEMCertificate(file, certType, loadKey = false) {
    dump("Loading certificate from " + file.path + "\n");
    let certDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
      Ci.nsIX509CertDB
    );
    certDB.importCertsFromFile(file, certType);
  },

  loadCertificateAndKey(file, pw) {
    dump("Loading key from " + file.path + "\n");
    let certDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
      Ci.nsIX509CertDB
    );
    certDB.importPKCS12File(file, pw);
  },
};