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

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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 const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
michael@0 5 const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
michael@0 6 const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
michael@0 7 const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
michael@0 8 const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
michael@0 9 const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
michael@0 10 const nsIPK11Token = Components.interfaces.nsIPK11Token;
michael@0 11
michael@0 12 var params;
michael@0 13 var tokenName="";
michael@0 14 var pw1;
michael@0 15
michael@0 16 function doPrompt(msg)
michael@0 17 {
michael@0 18 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
michael@0 19 getService(Components.interfaces.nsIPromptService);
michael@0 20 prompts.alert(window, null, msg);
michael@0 21 }
michael@0 22
michael@0 23 function onLoad()
michael@0 24 {
michael@0 25 document.documentElement.getButton("accept").disabled = true;
michael@0 26
michael@0 27 pw1 = document.getElementById("pw1");
michael@0 28 try {
michael@0 29 params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
michael@0 30 tokenName = params.GetString(1);
michael@0 31 } catch(exception) {
michael@0 32 // this should not happen.
michael@0 33 // previously we had self.name, but self.name was a bad idea
michael@0 34 // as window name must be a subset of ascii, and the code was
michael@0 35 // previously trying to assign unicode to the window's name.
michael@0 36 // I checked all the places where we get a password prompt and
michael@0 37 // all of them pass an argument as part of this patch.
michael@0 38 tokenName="";
michael@0 39 }
michael@0 40
michael@0 41
michael@0 42 if(tokenName=="") {
michael@0 43 var sectokdb = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
michael@0 44 var tokenList = sectokdb.listTokens();
michael@0 45 var enumElement;
michael@0 46 var i=0;
michael@0 47 var menu = document.getElementById("tokenMenu");
michael@0 48 try {
michael@0 49 for ( ; !tokenList.isDone(); tokenList.next()) {
michael@0 50 enumElement = tokenList.currentItem();
michael@0 51 var token = enumElement.QueryInterface(nsIPK11Token);
michael@0 52 if(token.needsLogin() || !(token.needsUserInit)) {
michael@0 53 var menuItemNode = document.createElement("menuitem");
michael@0 54 menuItemNode.setAttribute("value", token.tokenName);
michael@0 55 menuItemNode.setAttribute("label", token.tokenName);
michael@0 56 menu.firstChild.appendChild(menuItemNode);
michael@0 57 if (i == 0) {
michael@0 58 menu.selectedItem = menuItemNode;
michael@0 59 tokenName = token.tokenName;
michael@0 60 }
michael@0 61 i++;
michael@0 62 }
michael@0 63 }
michael@0 64 }catch(exception){}
michael@0 65 } else {
michael@0 66 var sel = document.getElementById("tokenMenu");
michael@0 67 sel.setAttribute("hidden", "true");
michael@0 68 var tag = document.getElementById("tokenName");
michael@0 69 tag.setAttribute("value",tokenName);
michael@0 70 }
michael@0 71
michael@0 72 process();
michael@0 73 }
michael@0 74
michael@0 75 function onMenuChange()
michael@0 76 {
michael@0 77 //get the selected token
michael@0 78 var list = document.getElementById("tokenMenu");
michael@0 79 tokenName = list.value;
michael@0 80
michael@0 81 process();
michael@0 82 }
michael@0 83
michael@0 84
michael@0 85 function process()
michael@0 86 {
michael@0 87 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
michael@0 88 var bundle = document.getElementById("pippki_bundle");
michael@0 89
michael@0 90 // If the token is unitialized, don't use the old password box.
michael@0 91 // Otherwise, do.
michael@0 92
michael@0 93 var slot = secmoddb.findSlotByName(tokenName);
michael@0 94 if (slot) {
michael@0 95 var oldpwbox = document.getElementById("oldpw");
michael@0 96 var msgBox = document.getElementById("message");
michael@0 97 var status = slot.status;
michael@0 98 if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
michael@0 99 || status == nsIPKCS11Slot.SLOT_READY) {
michael@0 100
michael@0 101 oldpwbox.setAttribute("hidden", "true");
michael@0 102 msgBox.setAttribute("value", bundle.getString("password_not_set"));
michael@0 103 msgBox.setAttribute("hidden", "false");
michael@0 104
michael@0 105 if (status == nsIPKCS11Slot.SLOT_READY) {
michael@0 106 oldpwbox.setAttribute("inited", "empty");
michael@0 107 } else {
michael@0 108 oldpwbox.setAttribute("inited", "true");
michael@0 109 }
michael@0 110
michael@0 111 // Select first password field
michael@0 112 document.getElementById('pw1').focus();
michael@0 113
michael@0 114 } else {
michael@0 115 // Select old password field
michael@0 116 oldpwbox.setAttribute("hidden", "false");
michael@0 117 msgBox.setAttribute("hidden", "true");
michael@0 118 oldpwbox.setAttribute("inited", "false");
michael@0 119 oldpwbox.focus();
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 if (params) {
michael@0 124 // Return value 0 means "canceled"
michael@0 125 params.SetInt(1, 0);
michael@0 126 }
michael@0 127
michael@0 128 checkPasswords();
michael@0 129 }
michael@0 130
michael@0 131 function onP12Load(disableOkButton)
michael@0 132 {
michael@0 133 document.documentElement.getButton("accept").disabled = disableOkButton;
michael@0 134 pw1 = document.getElementById("pw1");
michael@0 135 params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
michael@0 136 // Select first password field
michael@0 137 document.getElementById('pw1').focus();
michael@0 138 }
michael@0 139
michael@0 140 function setPassword()
michael@0 141 {
michael@0 142 var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
michael@0 143 var token = pk11db.findTokenByName(tokenName);
michael@0 144
michael@0 145 var oldpwbox = document.getElementById("oldpw");
michael@0 146 var initpw = oldpwbox.getAttribute("inited");
michael@0 147 var bundle = document.getElementById("pippki_bundle");
michael@0 148
michael@0 149 var success = false;
michael@0 150
michael@0 151 if (initpw == "false" || initpw == "empty") {
michael@0 152 try {
michael@0 153 var oldpw = "";
michael@0 154 var passok = 0;
michael@0 155
michael@0 156 if (initpw == "empty") {
michael@0 157 passok = 1;
michael@0 158 } else {
michael@0 159 oldpw = oldpwbox.value;
michael@0 160 passok = token.checkPassword(oldpw);
michael@0 161 }
michael@0 162
michael@0 163 if (passok) {
michael@0 164 if (initpw == "empty" && pw1.value == "") {
michael@0 165 // This makes no sense that we arrive here,
michael@0 166 // we reached a case that should have been prevented by checkPasswords.
michael@0 167 } else {
michael@0 168 if (pw1.value == "") {
michael@0 169 var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
michael@0 170 if (secmoddb.isFIPSEnabled) {
michael@0 171 // empty passwords are not allowed in FIPS mode
michael@0 172 doPrompt(bundle.getString("pw_change2empty_in_fips_mode"));
michael@0 173 passok = 0;
michael@0 174 }
michael@0 175 }
michael@0 176 if (passok) {
michael@0 177 token.changePassword(oldpw, pw1.value);
michael@0 178 if (pw1.value == "") {
michael@0 179 doPrompt(bundle.getString("pw_erased_ok")
michael@0 180 + " "
michael@0 181 + bundle.getString("pw_empty_warning"));
michael@0 182 } else {
michael@0 183 doPrompt(bundle.getString("pw_change_ok"));
michael@0 184 }
michael@0 185 success = true;
michael@0 186 }
michael@0 187 }
michael@0 188 } else {
michael@0 189 oldpwbox.focus();
michael@0 190 oldpwbox.setAttribute("value", "");
michael@0 191 doPrompt(bundle.getString("incorrect_pw"));
michael@0 192 }
michael@0 193 } catch (e) {
michael@0 194 doPrompt(bundle.getString("failed_pw_change"));
michael@0 195 }
michael@0 196 } else {
michael@0 197 token.initPassword(pw1.value);
michael@0 198 if (pw1.value == "") {
michael@0 199 doPrompt(bundle.getString("pw_not_wanted")
michael@0 200 + " "
michael@0 201 + bundle.getString("pw_empty_warning"));
michael@0 202 }
michael@0 203 success = true;
michael@0 204 }
michael@0 205
michael@0 206 if (success && params)
michael@0 207 // Return value 1 means "successfully executed ok"
michael@0 208 params.SetInt(1, 1);
michael@0 209
michael@0 210 // Terminate dialog
michael@0 211 return success;
michael@0 212 }
michael@0 213
michael@0 214 function getPassword()
michael@0 215 {
michael@0 216 // grab what was entered
michael@0 217 params.SetString(2, pw1.value);
michael@0 218 // Return value
michael@0 219 params.SetInt(1, 1);
michael@0 220 // Terminate dialog
michael@0 221 return true;
michael@0 222 }
michael@0 223
michael@0 224 function setP12Password()
michael@0 225 {
michael@0 226 // grab what was entered
michael@0 227 params.SetString(2, pw1.value);
michael@0 228 // Return value
michael@0 229 params.SetInt(1, 1);
michael@0 230 // Terminate dialog
michael@0 231 return true;
michael@0 232 }
michael@0 233
michael@0 234 function setPasswordStrength()
michael@0 235 {
michael@0 236 // Here is how we weigh the quality of the password
michael@0 237 // number of characters
michael@0 238 // numbers
michael@0 239 // non-alpha-numeric chars
michael@0 240 // upper and lower case characters
michael@0 241
michael@0 242 var pw=document.getElementById('pw1').value;
michael@0 243 // doPrompt("password='" + pw +"'");
michael@0 244
michael@0 245 //length of the password
michael@0 246 var pwlength=(pw.length);
michael@0 247 if (pwlength>5)
michael@0 248 pwlength=5;
michael@0 249
michael@0 250
michael@0 251 //use of numbers in the password
michael@0 252 var numnumeric = pw.replace (/[0-9]/g, "");
michael@0 253 var numeric=(pw.length - numnumeric.length);
michael@0 254 if (numeric>3)
michael@0 255 numeric=3;
michael@0 256
michael@0 257 //use of symbols in the password
michael@0 258 var symbols = pw.replace (/\W/g, "");
michael@0 259 var numsymbols=(pw.length - symbols.length);
michael@0 260 if (numsymbols>3)
michael@0 261 numsymbols=3;
michael@0 262
michael@0 263 //use of uppercase in the password
michael@0 264 var numupper = pw.replace (/[A-Z]/g, "");
michael@0 265 var upper=(pw.length - numupper.length);
michael@0 266 if (upper>3)
michael@0 267 upper=3;
michael@0 268
michael@0 269
michael@0 270 var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
michael@0 271
michael@0 272 // make sure we're give a value between 0 and 100
michael@0 273 if ( pwstrength < 0 ) {
michael@0 274 pwstrength = 0;
michael@0 275 }
michael@0 276
michael@0 277 if ( pwstrength > 100 ) {
michael@0 278 pwstrength = 100;
michael@0 279 }
michael@0 280
michael@0 281 var mymeter=document.getElementById('pwmeter');
michael@0 282 mymeter.setAttribute("value",pwstrength);
michael@0 283
michael@0 284 return;
michael@0 285 }
michael@0 286
michael@0 287 function checkPasswords()
michael@0 288 {
michael@0 289 var pw1=document.getElementById('pw1').value;
michael@0 290 var pw2=document.getElementById('pw2').value;
michael@0 291
michael@0 292 var oldpwbox = document.getElementById("oldpw");
michael@0 293 if (oldpwbox) {
michael@0 294 var initpw = oldpwbox.getAttribute("inited");
michael@0 295
michael@0 296 if (initpw == "empty" && pw1 == "") {
michael@0 297 // The token has already been initialized, therefore this dialog
michael@0 298 // was called with the intention to change the password.
michael@0 299 // The token currently uses an empty password.
michael@0 300 // We will not allow changing the password from empty to empty.
michael@0 301 document.documentElement.getButton("accept").disabled = true;
michael@0 302 return;
michael@0 303 }
michael@0 304 }
michael@0 305
michael@0 306 document.documentElement.getButton("accept").disabled = (pw1 != pw2);
michael@0 307 }

mercurial