password-manager/frontend/delta/js/Clipperz/PM/PIN.js

161 lines
4.8 KiB
JavaScript
Raw Normal View History

2013-08-30 17:56:53 +02:00
/*
2015-03-09 15:45:35 +01:00
Copyright 2008-2015 Clipperz Srl
2013-08-30 17:56:53 +02:00
This file is part of Clipperz, the online password manager.
For further information about its features and functionalities please
refer to http://www.clipperz.com.
* Clipperz is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
* Clipperz is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public
License along with Clipperz. If not, see http://www.gnu.org/licenses/.
*/
if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
if (typeof(Clipperz.PM.PIN) == 'undefined') { Clipperz.PM.PIN = {}; }
MochiKit.Base.update(Clipperz.PM.PIN, {
//-------------------------------------------------------------------------
'__repr__': function () {
return "[" + this.NAME + " " + this.VERSION + "]";
},
//-------------------------------------------------------------------------
'toString': function () {
return this.__repr__();
},
2015-09-30 20:09:58 +02:00
'ENCRYPTED_PASSPHRASE_LENGTH': 1024,
'DEFAULT_PIN_LENGTH': 5,
2013-08-30 17:56:53 +02:00
'ALLOWED_RETRY': 3,
2015-09-30 20:09:58 +02:00
'LS_USERNAME': 'clipperz.pin.username',
'LS_PASSPHRASE': 'clipperz.pin.passphrase',
'LS_FAILURE_COUNT': 'clipperz.pin.failureCount',
2013-08-30 17:56:53 +02:00
//-------------------------------------------------------------------------
'isSet': function () {
2015-09-30 20:09:58 +02:00
return (localStorage[this.LS_USERNAME] && localStorage[this.LS_PASSPHRASE]);
2013-08-30 17:56:53 +02:00
},
//-------------------------------------------------------------------------
'recordFailedAttempt': function () {
var failureCount;
var result;
2015-09-30 20:09:58 +02:00
failureCount = localStorage[this.LS_FAILURE_COUNT];
2013-08-30 17:56:53 +02:00
if (failureCount == null) {
failureCount = 0
}
failureCount ++;
if (failureCount < this.ALLOWED_RETRY) {
2015-09-30 20:09:58 +02:00
localStorage[this.LS_FAILURE_COUNT] = failureCount;
2013-08-30 17:56:53 +02:00
result = failureCount;
} else {
2015-09-30 20:09:58 +02:00
this.disablePin();
2013-08-30 17:56:53 +02:00
result = -1;
}
return result;
},
'resetFailedAttemptCount': function () {
2015-09-30 20:09:58 +02:00
localStorage.removeItem(this.LS_FAILURE_COUNT);
2013-08-30 17:56:53 +02:00
},
'failureCount': function () {
2015-09-30 20:09:58 +02:00
return localStorage[this.LS_FAILURE_COUNT];
2013-08-30 17:56:53 +02:00
},
//-------------------------------------------------------------------------
'deriveKeyFromPin': function (aPIN) {
return Clipperz.Crypto.SHA.sha256(new Clipperz.ByteArray(aPIN));
},
2015-09-30 20:09:58 +02:00
'credentialsWithPIN': function(aPIN) {
return {
'username': localStorage[this.LS_USERNAME],
'passphrase': this.decryptPassphraseWithPin(aPIN, localStorage[this.LS_PASSPHRASE]),
2013-08-30 17:56:53 +02:00
}
2015-09-30 20:09:58 +02:00
},
2013-08-30 17:56:53 +02:00
2015-09-30 20:09:58 +02:00
'encryptPassphraseWithPin': function(aPIN, aPassphrase) {
var byteArrayPassphrase = new Clipperz.ByteArray(aPassphrase);
var randomBytesLength = this.ENCRYPTED_PASSPHRASE_LENGTH-byteArrayPassphrase.length()-1;
var randomBytes = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(randomBytesLength);
var derivedKey = this.deriveKeyFromPin(aPIN);
byteArrayPassphrase.appendByte(0);
byteArrayPassphrase.appendBytes(randomBytes.arrayValues());
return Clipperz.Crypto.AES_2.encrypt(derivedKey, byteArrayPassphrase).toBase64String();
2013-08-30 17:56:53 +02:00
},
2015-09-30 20:09:58 +02:00
'decryptPassphraseWithPin': function(aPIN, anEncryptedPassphrase) {
var byteArrayEncryptedPassphrase = (new Clipperz.ByteArray()).appendBase64String(anEncryptedPassphrase);
var derivedKey = this.deriveKeyFromPin(aPIN);
var byteArrayPassphrase = Clipperz.Crypto.AES_2.decrypt(derivedKey, byteArrayEncryptedPassphrase);
2015-09-30 20:09:58 +02:00
var arrayPassphrase = byteArrayPassphrase.arrayValues();
var slicedArrayPassphrase = arrayPassphrase.slice(0, arrayPassphrase.indexOf(0));
2013-08-30 17:56:53 +02:00
2015-09-30 20:09:58 +02:00
return new Clipperz.ByteArray(slicedArrayPassphrase).asString();
},
2013-08-30 17:56:53 +02:00
2015-09-30 20:09:58 +02:00
'updatePin': function(aUser, aPIN) {
return Clipperz.Async.callbacks("Clipperz.PM.PIN", [
MochiKit.Base.method(aUser, 'username'),
MochiKit.Base.method(localStorage, 'setItem', this.LS_USERNAME),
MochiKit.Base.method(aUser, 'getPassphrase'),
MochiKit.Base.method(this, 'encryptPassphraseWithPin', aPIN),
MochiKit.Base.method(localStorage, 'setItem', this.LS_PASSPHRASE),
MochiKit.Base.method(localStorage, 'setItem', this.LS_FAILURE_COUNT, 0),
], {trace:false});
2013-08-30 17:56:53 +02:00
},
2015-09-30 20:09:58 +02:00
'disablePin': function () {
localStorage.removeItem(this.LS_USERNAME);
localStorage.removeItem(this.LS_PASSPHRASE);
localStorage.removeItem(this.LS_FAILURE_COUNT);
2013-08-30 17:56:53 +02:00
},
'isLocalStorageSupported': function() {
var result;
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
result = true;
} catch(e) {
result = false;
}
return result;
},
2013-08-30 17:56:53 +02:00
//-------------------------------------------------------------------------
__syntaxFix__: "syntax fix"
});