Skip to content

Commit

Permalink
Completed #5 (close #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdrop committed Sep 23, 2016
1 parent 3244797 commit dbeaedb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/main/java/me/xdrop/passlock/core/PasswordManagerAES.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ public void rename(String reference, String newReference) throws RefNotFoundExce
@Override
public void updatePassword(String reference, char[] masterKey, char[] newPassword) throws RefNotFoundException {

if(reference.equalsIgnoreCase("master")) {

LOG.debug("Updating master password");

try {
updateMasterPassword(masterKey, newPassword);
} catch (InvalidKeyException e) {
LOG.error("Master key was invalid", e);
}

return;
}


LOG.debug("Updating password for " + reference);

AESEncryptionData encryptionData = encryptionModel.encrypt(ByteUtils.getBytes(newPassword), masterKey);
Expand Down Expand Up @@ -167,17 +181,10 @@ public void deletePassword(String reference) throws RefNotFoundException {

}

public void updateMasterPassword(char[] oldMasterPassword, char[] newMasterPassword) throws InvalidKeyException {


if (!unlocksMaster(oldMasterPassword)) throw new InvalidKeyException();

public void updateMasterPassword(char[] oldMasterKey, char[] newMasterPassword) throws InvalidKeyException {

LOG.info("Generating AES secret...");

/* Remember the old master key */
char[] oldKey = getMasterKey(oldMasterPassword);

/* Store the master key */
SecretKey secretKey = encryptionModel.generateSecret(newMasterPassword);

Expand All @@ -188,7 +195,7 @@ public void updateMasterPassword(char[] oldMasterPassword, char[] newMasterPassw
addPassword("The master key", secretKey.getEncoded(), newMasterPassword, "master");

LOG.info("Re-encrypting entries...");
updateMasterKey(oldKey, ByteUtils.getChars(secretKey.getEncoded()));
updateMasterKey(oldMasterKey, ByteUtils.getChars(secretKey.getEncoded()));

} catch (AlreadyExistsException | RefNotFoundException ignored) { /* can't happen */ }

Expand Down Expand Up @@ -227,6 +234,7 @@ public void process() throws Exception {
try {
oldPayload = encryptionModel.decrypt(entry.getEncryptionData(), oldMasterKey);
} catch (InvalidKeyException e) {
/* not all passwords were encoded with the same key, so just skip.. */
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class PasswordManagerAESIT extends LogGroovyTestCase {

def newC = "newmaster".toCharArray()

pwman.updateMasterPassword(masterPassC, newC)
pwman.updateMasterPassword(pwman.getMasterKey(masterPassC), newC)

assert pwman.getPassword("ab", pwman.getMasterKey(newC)) == sampleStoredBytes

Expand Down

0 comments on commit dbeaedb

Please sign in to comment.