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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var MasterPassword = { michael@0: pref: "privacy.masterpassword.enabled", michael@0: _tokenName: "", michael@0: michael@0: get _secModuleDB() { michael@0: delete this._secModuleDB; michael@0: return this._secModuleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService(Ci.nsIPKCS11ModuleDB); michael@0: }, michael@0: michael@0: get _pk11DB() { michael@0: delete this._pk11DB; michael@0: return this._pk11DB = Cc["@mozilla.org/security/pk11tokendb;1"].getService(Ci.nsIPK11TokenDB); michael@0: }, michael@0: michael@0: get enabled() { michael@0: let slot = this._secModuleDB.findSlotByName(this._tokenName); michael@0: if (slot) { michael@0: let status = slot.status; michael@0: return status != Ci.nsIPKCS11Slot.SLOT_UNINITIALIZED && status != Ci.nsIPKCS11Slot.SLOT_READY; michael@0: } michael@0: return false; michael@0: }, michael@0: michael@0: setPassword: function setPassword(aPassword) { michael@0: try { michael@0: let status; michael@0: let slot = this._secModuleDB.findSlotByName(this._tokenName); michael@0: if (slot) michael@0: status = slot.status; michael@0: else michael@0: return false; michael@0: michael@0: let token = this._pk11DB.findTokenByName(this._tokenName); michael@0: michael@0: if (status == Ci.nsIPKCS11Slot.SLOT_UNINITIALIZED) michael@0: token.initPassword(aPassword); michael@0: else if (status == Ci.nsIPKCS11Slot.SLOT_READY) michael@0: token.changePassword("", aPassword); michael@0: michael@0: BrowserApp.notifyPrefObservers(this.pref); michael@0: return true; michael@0: } catch(e) { michael@0: dump("MasterPassword.setPassword: " + e); michael@0: } michael@0: return false; michael@0: }, michael@0: michael@0: removePassword: function removePassword(aOldPassword) { michael@0: try { michael@0: let token = this._pk11DB.getInternalKeyToken(); michael@0: if (token.checkPassword(aOldPassword)) { michael@0: token.changePassword(aOldPassword, ""); michael@0: BrowserApp.notifyPrefObservers(this.pref); michael@0: return true; michael@0: } michael@0: } catch(e) { michael@0: dump("MasterPassword.removePassword: " + e + "\n"); michael@0: } michael@0: NativeWindow.toast.show(Strings.browser.GetStringFromName("masterPassword.incorrect"), "short"); michael@0: return false; michael@0: } michael@0: };