mobile/android/chrome/content/MasterPassword.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/MasterPassword.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var MasterPassword = {
    1.10 +  pref: "privacy.masterpassword.enabled",
    1.11 +  _tokenName: "",
    1.12 +
    1.13 +  get _secModuleDB() {
    1.14 +    delete this._secModuleDB;
    1.15 +    return this._secModuleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService(Ci.nsIPKCS11ModuleDB);
    1.16 +  },
    1.17 +
    1.18 +  get _pk11DB() {
    1.19 +    delete this._pk11DB;
    1.20 +    return this._pk11DB = Cc["@mozilla.org/security/pk11tokendb;1"].getService(Ci.nsIPK11TokenDB);
    1.21 +  },
    1.22 +
    1.23 +  get enabled() {
    1.24 +    let slot = this._secModuleDB.findSlotByName(this._tokenName);
    1.25 +    if (slot) {
    1.26 +      let status = slot.status;
    1.27 +      return status != Ci.nsIPKCS11Slot.SLOT_UNINITIALIZED && status != Ci.nsIPKCS11Slot.SLOT_READY;
    1.28 +    }
    1.29 +    return false;
    1.30 +  },
    1.31 +
    1.32 +  setPassword: function setPassword(aPassword) {
    1.33 +    try {
    1.34 +      let status;
    1.35 +      let slot = this._secModuleDB.findSlotByName(this._tokenName);
    1.36 +      if (slot)
    1.37 +        status = slot.status;
    1.38 +      else
    1.39 +        return false;
    1.40 +
    1.41 +      let token = this._pk11DB.findTokenByName(this._tokenName);
    1.42 +
    1.43 +      if (status == Ci.nsIPKCS11Slot.SLOT_UNINITIALIZED)
    1.44 +        token.initPassword(aPassword);
    1.45 +      else if (status == Ci.nsIPKCS11Slot.SLOT_READY)
    1.46 +        token.changePassword("", aPassword);
    1.47 +
    1.48 +      BrowserApp.notifyPrefObservers(this.pref);
    1.49 +      return true;
    1.50 +    } catch(e) {
    1.51 +      dump("MasterPassword.setPassword: " + e);
    1.52 +    }
    1.53 +    return false;
    1.54 +  },
    1.55 +
    1.56 +  removePassword: function removePassword(aOldPassword) {
    1.57 +    try {
    1.58 +      let token = this._pk11DB.getInternalKeyToken();
    1.59 +      if (token.checkPassword(aOldPassword)) {
    1.60 +        token.changePassword(aOldPassword, "");
    1.61 +        BrowserApp.notifyPrefObservers(this.pref);
    1.62 +        return true;
    1.63 +      }
    1.64 +    } catch(e) {
    1.65 +      dump("MasterPassword.removePassword: " + e + "\n");
    1.66 +    }
    1.67 +    NativeWindow.toast.show(Strings.browser.GetStringFromName("masterPassword.incorrect"), "short");
    1.68 +    return false;
    1.69 +  }
    1.70 +};

mercurial