security/manager/pki/resources/content/device_manager.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 6 const nsFilePicker = "@mozilla.org/filepicker;1";
michael@0 7 const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
michael@0 8 const nsIPKCS11Module = Components.interfaces.nsIPKCS11Module;
michael@0 9 const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
michael@0 10 const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
michael@0 11 const nsIPK11Token = Components.interfaces.nsIPK11Token;
michael@0 12 const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
michael@0 13 const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
michael@0 14 const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
michael@0 15 const nsDialogParamBlock = "@mozilla.org/embedcomp/dialogparam;1";
michael@0 16 const nsIPKCS11 = Components.interfaces.nsIPKCS11;
michael@0 17 const nsPKCS11ContractID = "@mozilla.org/security/pkcs11;1";
michael@0 18
michael@0 19 var bundle;
michael@0 20 var secmoddb;
michael@0 21 var skip_enable_buttons = false;
michael@0 22
michael@0 23 /* Do the initial load of all PKCS# modules and list them. */
michael@0 24 function LoadModules()
michael@0 25 {
michael@0 26 bundle = document.getElementById("pippki_bundle");
michael@0 27 secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
michael@0 28 window.crypto.enableSmartCardEvents = true;
michael@0 29 document.addEventListener("smartcard-insert", onSmartCardChange, false);
michael@0 30 document.addEventListener("smartcard-remove", onSmartCardChange, false);
michael@0 31
michael@0 32 RefreshDeviceList();
michael@0 33 }
michael@0 34
michael@0 35 function getPKCS11()
michael@0 36 {
michael@0 37 return Components.classes[nsPKCS11ContractID].getService(nsIPKCS11);
michael@0 38 }
michael@0 39
michael@0 40 function getNSSString(name)
michael@0 41 {
michael@0 42 return document.getElementById("pipnss_bundle").getString(name);
michael@0 43 }
michael@0 44
michael@0 45 function doPrompt(msg)
michael@0 46 {
michael@0 47 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
michael@0 48 getService(Components.interfaces.nsIPromptService);
michael@0 49 prompts.alert(window, null, msg);
michael@0 50 }
michael@0 51
michael@0 52 function doConfirm(msg)
michael@0 53 {
michael@0 54 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
michael@0 55 getService(Components.interfaces.nsIPromptService);
michael@0 56 return prompts.confirm(window, null, msg);
michael@0 57 }
michael@0 58
michael@0 59 function RefreshDeviceList()
michael@0 60 {
michael@0 61 var modules = secmoddb.listModules();
michael@0 62 var done = false;
michael@0 63
michael@0 64 try {
michael@0 65 modules.isDone();
michael@0 66 } catch (e) { done = true; }
michael@0 67 while (!done) {
michael@0 68 var module = modules.currentItem().QueryInterface(nsIPKCS11Module);
michael@0 69 if (module) {
michael@0 70 var slotnames = [];
michael@0 71 var slots = module.listSlots();
michael@0 72 var slots_done = false;
michael@0 73 try {
michael@0 74 slots.isDone();
michael@0 75 } catch (e) { slots_done = true; }
michael@0 76 while (!slots_done) {
michael@0 77 var slot = null;
michael@0 78 try {
michael@0 79 slot = slots.currentItem().QueryInterface(nsIPKCS11Slot);
michael@0 80 } catch (e) { slot = null; }
michael@0 81 // in the ongoing discussion of whether slot names or token names
michael@0 82 // are to be shown, I've gone with token names because NSS will
michael@0 83 // prefer lookup by token name. However, the token may not be
michael@0 84 // present, so maybe slot names should be listed, while token names
michael@0 85 // are "remembered" for lookup?
michael@0 86 if (slot != null) {
michael@0 87 if (slot.tokenName)
michael@0 88 slotnames[slotnames.length] = slot.tokenName;
michael@0 89 else
michael@0 90 slotnames[slotnames.length] = slot.name;
michael@0 91 }
michael@0 92 try {
michael@0 93 slots.next();
michael@0 94 } catch (e) { slots_done = true; }
michael@0 95 }
michael@0 96 AddModule(module.name, slotnames);
michael@0 97 }
michael@0 98 try {
michael@0 99 modules.next();
michael@0 100 } catch (e) { done = true; }
michael@0 101 }
michael@0 102 /* Set the text on the fips button */
michael@0 103 SetFIPSButton();
michael@0 104 }
michael@0 105
michael@0 106 function SetFIPSButton()
michael@0 107 {
michael@0 108 var fipsButton = document.getElementById("fipsbutton");
michael@0 109 var label;
michael@0 110 if (secmoddb.isFIPSEnabled) {
michael@0 111 label = bundle.getString("disable_fips");
michael@0 112 } else {
michael@0 113 label = bundle.getString("enable_fips");
michael@0 114 }
michael@0 115 fipsButton.setAttribute("label", label);
michael@0 116
michael@0 117 var can_toggle = secmoddb.canToggleFIPS;
michael@0 118 if (can_toggle) {
michael@0 119 fipsButton.removeAttribute("disabled");
michael@0 120 } else {
michael@0 121 fipsButton.setAttribute("disabled", "true");
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 /* Add a module to the tree. slots is the array of slots in the module,
michael@0 126 * to be represented as children.
michael@0 127 */
michael@0 128 function AddModule(module, slots)
michael@0 129 {
michael@0 130 var tree = document.getElementById("device_list");
michael@0 131 var item = document.createElement("treeitem");
michael@0 132 var row = document.createElement("treerow");
michael@0 133 var cell = document.createElement("treecell");
michael@0 134 cell.setAttribute("label", module);
michael@0 135 row.appendChild(cell);
michael@0 136 item.appendChild(row);
michael@0 137 var parent = document.createElement("treechildren");
michael@0 138 for (var i = 0; i<slots.length; i++) {
michael@0 139 var child_item = document.createElement("treeitem");
michael@0 140 var child_row = document.createElement("treerow");
michael@0 141 var child_cell = document.createElement("treecell");
michael@0 142 child_cell.setAttribute("label", slots[i]);
michael@0 143 child_row.appendChild(child_cell);
michael@0 144 child_item.appendChild(child_row);
michael@0 145 child_item.setAttribute("pk11kind", "slot");
michael@0 146 parent.appendChild(child_item);
michael@0 147 }
michael@0 148 item.appendChild(parent);
michael@0 149 item.setAttribute("pk11kind", "module");
michael@0 150 item.setAttribute("open", "true");
michael@0 151 item.setAttribute("container", "true");
michael@0 152 tree.appendChild(item);
michael@0 153 }
michael@0 154
michael@0 155 var selected_slot;
michael@0 156 var selected_module;
michael@0 157
michael@0 158 /* get the slot selected by the user (can only be one-at-a-time) */
michael@0 159 function getSelectedItem()
michael@0 160 {
michael@0 161 var tree = document.getElementById('device_tree');
michael@0 162 if (tree.currentIndex < 0) return;
michael@0 163 var item = tree.contentView.getItemAtIndex(tree.currentIndex);
michael@0 164 selected_slot = null;
michael@0 165 selected_module = null;
michael@0 166 if (item) {
michael@0 167 var kind = item.getAttribute("pk11kind");
michael@0 168 var module_name;
michael@0 169 if (kind == "slot") {
michael@0 170 // get the module cell for this slot cell
michael@0 171 var cell = item.parentNode.parentNode.firstChild.firstChild;
michael@0 172 module_name = cell.getAttribute("label");
michael@0 173 var module = secmoddb.findModuleByName(module_name);
michael@0 174 // get the cell for the selected row (the slot to display)
michael@0 175 cell = item.firstChild.firstChild;
michael@0 176 var slot_name = cell.getAttribute("label");
michael@0 177 selected_slot = module.findSlotByName(slot_name);
michael@0 178 } else { // (kind == "module")
michael@0 179 // get the cell for the selected row (the module to display)
michael@0 180 cell = item.firstChild.firstChild;
michael@0 181 module_name = cell.getAttribute("label");
michael@0 182 selected_module = secmoddb.findModuleByName(module_name);
michael@0 183 }
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187 function enableButtons()
michael@0 188 {
michael@0 189 if (skip_enable_buttons)
michael@0 190 return;
michael@0 191
michael@0 192 var login_toggle = "true";
michael@0 193 var logout_toggle = "true";
michael@0 194 var pw_toggle = "true";
michael@0 195 var unload_toggle = "true";
michael@0 196 getSelectedItem();
michael@0 197 if (selected_module) {
michael@0 198 unload_toggle = "false";
michael@0 199 showModuleInfo();
michael@0 200 } else if (selected_slot) {
michael@0 201 // here's the workaround - login functions are all with token,
michael@0 202 // so grab the token type
michael@0 203 var selected_token = selected_slot.getToken();
michael@0 204 if (selected_token != null) {
michael@0 205 if (selected_token.needsLogin() || !(selected_token.needsUserInit)) {
michael@0 206 pw_toggle = "false";
michael@0 207 if(selected_token.needsLogin()) {
michael@0 208 if (selected_token.isLoggedIn()) {
michael@0 209 logout_toggle = "false";
michael@0 210 } else {
michael@0 211 login_toggle = "false";
michael@0 212 }
michael@0 213 }
michael@0 214 }
michael@0 215 }
michael@0 216 showSlotInfo();
michael@0 217 }
michael@0 218 var thebutton = document.getElementById('login_button');
michael@0 219 thebutton.setAttribute("disabled", login_toggle);
michael@0 220 thebutton = document.getElementById('logout_button');
michael@0 221 thebutton.setAttribute("disabled", logout_toggle);
michael@0 222 thebutton = document.getElementById('change_pw_button');
michael@0 223 thebutton.setAttribute("disabled", pw_toggle);
michael@0 224 thebutton = document.getElementById('unload_button');
michael@0 225 thebutton.setAttribute("disabled", unload_toggle);
michael@0 226 // not implemented
michael@0 227 //thebutton = document.getElementById('change_slotname_button');
michael@0 228 //thebutton.setAttribute("disabled", toggle);
michael@0 229 }
michael@0 230
michael@0 231 // clear the display of information for the slot
michael@0 232 function ClearInfoList()
michael@0 233 {
michael@0 234 var info_list = document.getElementById("info_list");
michael@0 235 while (info_list.firstChild)
michael@0 236 info_list.removeChild(info_list.firstChild);
michael@0 237 }
michael@0 238
michael@0 239 function ClearDeviceList()
michael@0 240 {
michael@0 241 ClearInfoList();
michael@0 242
michael@0 243 skip_enable_buttons = true;
michael@0 244 var tree = document.getElementById('device_tree');
michael@0 245 tree.view.selection.clearSelection();
michael@0 246 skip_enable_buttons = false;
michael@0 247
michael@0 248 // Remove the existing listed modules so that refresh doesn't
michael@0 249 // display the module that just changed.
michael@0 250 var device_list = document.getElementById("device_list");
michael@0 251 while (device_list.hasChildNodes())
michael@0 252 device_list.removeChild(device_list.firstChild);
michael@0 253 }
michael@0 254
michael@0 255
michael@0 256 // show a list of info about a slot
michael@0 257 function showSlotInfo()
michael@0 258 {
michael@0 259 var present = true;
michael@0 260 ClearInfoList();
michael@0 261 switch (selected_slot.status) {
michael@0 262 case nsIPKCS11Slot.SLOT_DISABLED:
michael@0 263 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 264 bundle.getString("devinfo_stat_disabled"),
michael@0 265 "tok_status");
michael@0 266 present = false;
michael@0 267 break;
michael@0 268 case nsIPKCS11Slot.SLOT_NOT_PRESENT:
michael@0 269 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 270 bundle.getString("devinfo_stat_notpresent"),
michael@0 271 "tok_status");
michael@0 272 present = false;
michael@0 273 break;
michael@0 274 case nsIPKCS11Slot.SLOT_UNINITIALIZED:
michael@0 275 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 276 bundle.getString("devinfo_stat_uninitialized"),
michael@0 277 "tok_status");
michael@0 278 break;
michael@0 279 case nsIPKCS11Slot.SLOT_NOT_LOGGED_IN:
michael@0 280 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 281 bundle.getString("devinfo_stat_notloggedin"),
michael@0 282 "tok_status");
michael@0 283 break;
michael@0 284 case nsIPKCS11Slot.SLOT_LOGGED_IN:
michael@0 285 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 286 bundle.getString("devinfo_stat_loggedin"),
michael@0 287 "tok_status");
michael@0 288 break;
michael@0 289 case nsIPKCS11Slot.SLOT_READY:
michael@0 290 AddInfoRow(bundle.getString("devinfo_status"),
michael@0 291 bundle.getString("devinfo_stat_ready"),
michael@0 292 "tok_status");
michael@0 293 break;
michael@0 294 }
michael@0 295 AddInfoRow(bundle.getString("devinfo_desc"),
michael@0 296 selected_slot.desc, "slot_desc");
michael@0 297 AddInfoRow(bundle.getString("devinfo_manID"),
michael@0 298 selected_slot.manID, "slot_manID");
michael@0 299 AddInfoRow(bundle.getString("devinfo_hwversion"),
michael@0 300 selected_slot.HWVersion, "slot_hwv");
michael@0 301 AddInfoRow(bundle.getString("devinfo_fwversion"),
michael@0 302 selected_slot.FWVersion, "slot_fwv");
michael@0 303 if (present) {
michael@0 304 showTokenInfo();
michael@0 305 }
michael@0 306 }
michael@0 307
michael@0 308 function showModuleInfo()
michael@0 309 {
michael@0 310 ClearInfoList();
michael@0 311 AddInfoRow(bundle.getString("devinfo_modname"),
michael@0 312 selected_module.name, "module_name");
michael@0 313 AddInfoRow(bundle.getString("devinfo_modpath"),
michael@0 314 selected_module.libName, "module_path");
michael@0 315 }
michael@0 316
michael@0 317 // add a row to the info list, as [col1 col2] (ex.: ["status" "logged in"])
michael@0 318 function AddInfoRow(col1, col2, cell_id)
michael@0 319 {
michael@0 320 var tree = document.getElementById("info_list");
michael@0 321 var item = document.createElement("treeitem");
michael@0 322 var row = document.createElement("treerow");
michael@0 323 var cell1 = document.createElement("treecell");
michael@0 324 cell1.setAttribute("label", col1);
michael@0 325 cell1.setAttribute("crop", "never");
michael@0 326 row.appendChild(cell1);
michael@0 327 var cell2 = document.createElement("treecell");
michael@0 328 cell2.setAttribute("label", col2);
michael@0 329 cell2.setAttribute("crop", "never");
michael@0 330 cell2.setAttribute("id", cell_id);
michael@0 331 row.appendChild(cell2);
michael@0 332 item.appendChild(row);
michael@0 333 tree.appendChild(item);
michael@0 334 }
michael@0 335
michael@0 336 // log in to a slot
michael@0 337 function doLogin()
michael@0 338 {
michael@0 339 getSelectedItem();
michael@0 340 // here's the workaround - login functions are with token
michael@0 341 var selected_token = selected_slot.getToken();
michael@0 342 try {
michael@0 343 selected_token.login(false);
michael@0 344 var tok_status = document.getElementById("tok_status");
michael@0 345 if (selected_token.isLoggedIn()) {
michael@0 346 tok_status.setAttribute("label",
michael@0 347 bundle.getString("devinfo_stat_loggedin"));
michael@0 348 } else {
michael@0 349 tok_status.setAttribute("label",
michael@0 350 bundle.getString("devinfo_stat_notloggedin"));
michael@0 351 }
michael@0 352 } catch (e) {
michael@0 353 doPrompt(bundle.getString("login_failed"));
michael@0 354 }
michael@0 355 enableButtons();
michael@0 356 }
michael@0 357
michael@0 358 // log out of a slot
michael@0 359 function doLogout()
michael@0 360 {
michael@0 361 getSelectedItem();
michael@0 362 // here's the workaround - login functions are with token
michael@0 363 var selected_token = selected_slot.getToken();
michael@0 364 try {
michael@0 365 selected_token.logoutAndDropAuthenticatedResources();
michael@0 366 var tok_status = document.getElementById("tok_status");
michael@0 367 if (selected_token.isLoggedIn()) {
michael@0 368 tok_status.setAttribute("label",
michael@0 369 bundle.getString("devinfo_stat_loggedin"));
michael@0 370 } else {
michael@0 371 tok_status.setAttribute("label",
michael@0 372 bundle.getString("devinfo_stat_notloggedin"));
michael@0 373 }
michael@0 374 } catch (e) {
michael@0 375 }
michael@0 376 enableButtons();
michael@0 377 }
michael@0 378
michael@0 379 // load a new device
michael@0 380 function doLoad()
michael@0 381 {
michael@0 382 window.open("load_device.xul", "loaddevice",
michael@0 383 "chrome,centerscreen,modal");
michael@0 384 ClearDeviceList();
michael@0 385 RefreshDeviceList();
michael@0 386 }
michael@0 387
michael@0 388 function deleteSelected()
michael@0 389 {
michael@0 390 getSelectedItem();
michael@0 391 if (selected_module &&
michael@0 392 doConfirm(getNSSString("DelModuleWarning"))) {
michael@0 393 try {
michael@0 394 getPKCS11().deleteModule(selected_module.name);
michael@0 395 }
michael@0 396 catch (e) {
michael@0 397 doPrompt(getNSSString("DelModuleError"));
michael@0 398 return false;
michael@0 399 }
michael@0 400 selected_module = null;
michael@0 401 return true;
michael@0 402 }
michael@0 403 return false;
michael@0 404 }
michael@0 405
michael@0 406 function doUnload()
michael@0 407 {
michael@0 408 if (deleteSelected()) {
michael@0 409 ClearDeviceList();
michael@0 410 RefreshDeviceList();
michael@0 411 }
michael@0 412 }
michael@0 413
michael@0 414 // handle card insertion and removal
michael@0 415 function onSmartCardChange()
michael@0 416 {
michael@0 417 var tree = document.getElementById('device_tree');
michael@0 418 var index = tree.currentIndex;
michael@0 419 tree.currentIndex = 0;
michael@0 420 ClearDeviceList();
michael@0 421 RefreshDeviceList();
michael@0 422 tree.currentIndex = index;
michael@0 423 enableButtons();
michael@0 424 }
michael@0 425
michael@0 426 function changePassword()
michael@0 427 {
michael@0 428 getSelectedItem();
michael@0 429 var params = Components.classes[nsDialogParamBlock].createInstance(nsIDialogParamBlock);
michael@0 430 params.SetString(1,selected_slot.tokenName);
michael@0 431 window.openDialog("changepassword.xul",
michael@0 432 "",
michael@0 433 "chrome,centerscreen,modal", params);
michael@0 434 showSlotInfo();
michael@0 435 enableButtons();
michael@0 436 }
michael@0 437
michael@0 438 // browse fs for PKCS#11 device
michael@0 439 function doBrowseFiles()
michael@0 440 {
michael@0 441 var srbundle = document.getElementById("pippki_bundle");
michael@0 442 var fp = Components.classes[nsFilePicker].createInstance(nsIFilePicker);
michael@0 443 fp.init(window,
michael@0 444 srbundle.getString("loadPK11TokenDialog"),
michael@0 445 nsIFilePicker.modeOpen);
michael@0 446 fp.appendFilters(nsIFilePicker.filterAll);
michael@0 447 if (fp.show() == nsIFilePicker.returnOK) {
michael@0 448 var pathbox = document.getElementById("device_path");
michael@0 449 pathbox.setAttribute("value", fp.file.path);
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453 function doLoadDevice()
michael@0 454 {
michael@0 455 var name_box = document.getElementById("device_name");
michael@0 456 var path_box = document.getElementById("device_path");
michael@0 457 try {
michael@0 458 getPKCS11().addModule(name_box.value, path_box.value, 0,0);
michael@0 459 }
michael@0 460 catch (e) {
michael@0 461 if (e.result == Components.results.NS_ERROR_ILLEGAL_VALUE)
michael@0 462 doPrompt(getNSSString("AddModuleDup"));
michael@0 463 else
michael@0 464 doPrompt(getNSSString("AddModuleFailure"));
michael@0 465
michael@0 466 return false;
michael@0 467 }
michael@0 468 return true;
michael@0 469 }
michael@0 470
michael@0 471 // ------------------------------------- Old code
michael@0 472
michael@0 473 function showTokenInfo()
michael@0 474 {
michael@0 475 //ClearInfoList();
michael@0 476 var selected_token = selected_slot.getToken();
michael@0 477 AddInfoRow(bundle.getString("devinfo_label"),
michael@0 478 selected_token.tokenLabel, "tok_label");
michael@0 479 AddInfoRow(bundle.getString("devinfo_manID"),
michael@0 480 selected_token.tokenManID, "tok_manID");
michael@0 481 AddInfoRow(bundle.getString("devinfo_serialnum"),
michael@0 482 selected_token.tokenSerialNumber, "tok_sNum");
michael@0 483 AddInfoRow(bundle.getString("devinfo_hwversion"),
michael@0 484 selected_token.tokenHWVersion, "tok_hwv");
michael@0 485 AddInfoRow(bundle.getString("devinfo_fwversion"),
michael@0 486 selected_token.tokenFWVersion, "tok_fwv");
michael@0 487 }
michael@0 488
michael@0 489 function toggleFIPS()
michael@0 490 {
michael@0 491 if (!secmoddb.isFIPSEnabled) {
michael@0 492 // A restriction of FIPS mode is, the password must be set
michael@0 493 // In FIPS mode the password must be non-empty.
michael@0 494 // This is different from what we allow in NON-Fips mode.
michael@0 495
michael@0 496 var tokendb = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
michael@0 497 var internal_token = tokendb.getInternalKeyToken(); // nsIPK11Token
michael@0 498 var slot = secmoddb.findSlotByName(internal_token.tokenName);
michael@0 499 switch (slot.status) {
michael@0 500 case nsIPKCS11Slot.SLOT_UNINITIALIZED:
michael@0 501 case nsIPKCS11Slot.SLOT_READY:
michael@0 502 // Token has either no or an empty password.
michael@0 503 doPrompt(bundle.getString("fips_nonempty_password_required"));
michael@0 504 return;
michael@0 505 }
michael@0 506 }
michael@0 507
michael@0 508 try {
michael@0 509 secmoddb.toggleFIPSMode();
michael@0 510 }
michael@0 511 catch (e) {
michael@0 512 doPrompt(bundle.getString("unable_to_toggle_FIPS"));
michael@0 513 return;
michael@0 514 }
michael@0 515
michael@0 516 //Remove the existing listed modules so that re-fresh doesn't
michael@0 517 //display the module that just changed.
michael@0 518 ClearDeviceList();
michael@0 519
michael@0 520 RefreshDeviceList();
michael@0 521 }

mercurial