michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const nsIFilePicker = Components.interfaces.nsIFilePicker; michael@0: const nsFilePicker = "@mozilla.org/filepicker;1"; michael@0: const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot; michael@0: const nsIPKCS11Module = Components.interfaces.nsIPKCS11Module; michael@0: const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1"; michael@0: const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB; michael@0: const nsIPK11Token = Components.interfaces.nsIPK11Token; michael@0: const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1"; michael@0: const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB; michael@0: const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock; michael@0: const nsDialogParamBlock = "@mozilla.org/embedcomp/dialogparam;1"; michael@0: const nsIPKCS11 = Components.interfaces.nsIPKCS11; michael@0: const nsPKCS11ContractID = "@mozilla.org/security/pkcs11;1"; michael@0: michael@0: var bundle; michael@0: var secmoddb; michael@0: var skip_enable_buttons = false; michael@0: michael@0: /* Do the initial load of all PKCS# modules and list them. */ michael@0: function LoadModules() michael@0: { michael@0: bundle = document.getElementById("pippki_bundle"); michael@0: secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB); michael@0: window.crypto.enableSmartCardEvents = true; michael@0: document.addEventListener("smartcard-insert", onSmartCardChange, false); michael@0: document.addEventListener("smartcard-remove", onSmartCardChange, false); michael@0: michael@0: RefreshDeviceList(); michael@0: } michael@0: michael@0: function getPKCS11() michael@0: { michael@0: return Components.classes[nsPKCS11ContractID].getService(nsIPKCS11); michael@0: } michael@0: michael@0: function getNSSString(name) michael@0: { michael@0: return document.getElementById("pipnss_bundle").getString(name); michael@0: } michael@0: michael@0: function doPrompt(msg) michael@0: { michael@0: let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]. michael@0: getService(Components.interfaces.nsIPromptService); michael@0: prompts.alert(window, null, msg); michael@0: } michael@0: michael@0: function doConfirm(msg) michael@0: { michael@0: let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]. michael@0: getService(Components.interfaces.nsIPromptService); michael@0: return prompts.confirm(window, null, msg); michael@0: } michael@0: michael@0: function RefreshDeviceList() michael@0: { michael@0: var modules = secmoddb.listModules(); michael@0: var done = false; michael@0: michael@0: try { michael@0: modules.isDone(); michael@0: } catch (e) { done = true; } michael@0: while (!done) { michael@0: var module = modules.currentItem().QueryInterface(nsIPKCS11Module); michael@0: if (module) { michael@0: var slotnames = []; michael@0: var slots = module.listSlots(); michael@0: var slots_done = false; michael@0: try { michael@0: slots.isDone(); michael@0: } catch (e) { slots_done = true; } michael@0: while (!slots_done) { michael@0: var slot = null; michael@0: try { michael@0: slot = slots.currentItem().QueryInterface(nsIPKCS11Slot); michael@0: } catch (e) { slot = null; } michael@0: // in the ongoing discussion of whether slot names or token names michael@0: // are to be shown, I've gone with token names because NSS will michael@0: // prefer lookup by token name. However, the token may not be michael@0: // present, so maybe slot names should be listed, while token names michael@0: // are "remembered" for lookup? michael@0: if (slot != null) { michael@0: if (slot.tokenName) michael@0: slotnames[slotnames.length] = slot.tokenName; michael@0: else michael@0: slotnames[slotnames.length] = slot.name; michael@0: } michael@0: try { michael@0: slots.next(); michael@0: } catch (e) { slots_done = true; } michael@0: } michael@0: AddModule(module.name, slotnames); michael@0: } michael@0: try { michael@0: modules.next(); michael@0: } catch (e) { done = true; } michael@0: } michael@0: /* Set the text on the fips button */ michael@0: SetFIPSButton(); michael@0: } michael@0: michael@0: function SetFIPSButton() michael@0: { michael@0: var fipsButton = document.getElementById("fipsbutton"); michael@0: var label; michael@0: if (secmoddb.isFIPSEnabled) { michael@0: label = bundle.getString("disable_fips"); michael@0: } else { michael@0: label = bundle.getString("enable_fips"); michael@0: } michael@0: fipsButton.setAttribute("label", label); michael@0: michael@0: var can_toggle = secmoddb.canToggleFIPS; michael@0: if (can_toggle) { michael@0: fipsButton.removeAttribute("disabled"); michael@0: } else { michael@0: fipsButton.setAttribute("disabled", "true"); michael@0: } michael@0: } michael@0: michael@0: /* Add a module to the tree. slots is the array of slots in the module, michael@0: * to be represented as children. michael@0: */ michael@0: function AddModule(module, slots) michael@0: { michael@0: var tree = document.getElementById("device_list"); michael@0: var item = document.createElement("treeitem"); michael@0: var row = document.createElement("treerow"); michael@0: var cell = document.createElement("treecell"); michael@0: cell.setAttribute("label", module); michael@0: row.appendChild(cell); michael@0: item.appendChild(row); michael@0: var parent = document.createElement("treechildren"); michael@0: for (var i = 0; i