From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- comm/mailnews/test/resources/smimeUtils.jsm | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 comm/mailnews/test/resources/smimeUtils.jsm (limited to 'comm/mailnews/test/resources/smimeUtils.jsm') diff --git a/comm/mailnews/test/resources/smimeUtils.jsm b/comm/mailnews/test/resources/smimeUtils.jsm new file mode 100644 index 0000000000..db7cf2e5c9 --- /dev/null +++ b/comm/mailnews/test/resources/smimeUtils.jsm @@ -0,0 +1,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); + }, +}; -- cgit v1.2.3