Convert keyring to use key interface rather than the packetlist.

This commit is contained in:
Robert Nelson 2013-12-08 19:35:01 -08:00
parent bfba0eca0c
commit f00d3210aa
2 changed files with 56 additions and 22 deletions

View File

@ -56,7 +56,7 @@ var keyring = function() {
}
this.store = store;
function emailKeyCheck(key, email) {
function emailCheck(email, key) {
email = email.toLowerCase();
var keyEmails = key.getUserIds();
for (var i; i < keyEmails.length; i++) {
@ -69,18 +69,20 @@ var keyring = function() {
return false;
}
function idKeyCheck(key, id) {
var keyid = key.getKeyId();
if (keyid && keyid.write() == id) {
function idCheck(id, key) {
var keyids = key.getKeyIds();
for (var i = 0; i < keyids.length; i++) {
if (openpgp.util.hexstrdump(keyids[i].write()) == id) {
return true;
}
return false;
}
}
function checkForIdentityAndPacketMatch(identityFunction, identityInput, keyType) {
function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) {
var results = [];
for (var p = 0; p < this.keys.length; p++) {
var key = this.keys[p];
for (var p = 0; p < keys.length; p++) {
var key = keys[p];
switch (keyType) {
case openpgp.enums.packet.public_key:
if (key.isPublic() && identityFunction(identityInput, key)) {
@ -96,7 +98,7 @@ var keyring = function() {
}
return results;
}
this.checkForIdentityAndPacketMatch = checkForIdentityAndPacketMatch;
this.checkForIdentityAndKeyTypeMatch = checkForIdentityAndKeyTypeMatch;
/**
* searches all public keys in the keyring matching the address or address part of the user ids
@ -104,7 +106,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} The public keys associated with provided email address.
*/
function getPublicKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.public_key);
return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.public_key);
}
this.getPublicKeyForAddress = getPublicKeyForAddress;
@ -114,7 +116,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} private keys found
*/
function getPrivateKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.secret_key);
return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secret_key);
}
this.getPrivateKeyForAddress = getPrivateKeyForAddress;
@ -124,7 +126,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} public keys found
*/
function getKeysForKeyId(keyId) {
return this.checkForIdentityAndPacketMatch(idPacketCheck, keyId);
return this.checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.public_key);
}
this.getKeysForKeyId = getKeysForKeyId;

View File

@ -61,17 +61,49 @@ describe('Openpgp integration tests', function() {
});
});
describe('Keyring', function() {
describe('Import key pair', function() {
it('should work', function(done) {
// clear any keypair already in the keychain
keyring.init();
// import private key
keyring.importKey(privkey);
// import public key
keyring.importKey(pubkey);
done();
});
});
describe('Retrieve keys', function() {
it('getPublicKeyForAddress() - unknown address', function(done) {
var key = keyring.getPublicKeyForAddress('nobody@example.com');
expect(key).to.be.empty;
done();
});
it('getPrivateKeyForAddress() - unknown address', function(done) {
var key = keyring.getPrivateKeyForAddress('nobody@example.com');
expect(key).to.be.empty;
done();
});
it('getPublicKeyForAddress() - valid address', function(done) {
var key = keyring.getPublicKeyForAddress(user);
expect(key).to.exist;
done();
});
it('getPrivateKeyForAddress() - valid address', function(done) {
var key = keyring.getPrivateKeyForAddress(user);
expect(key).to.exist;
done();
});
it('getKeysForKeyId() - unknown id', function(done) {
var keys = keyring.getKeysForKeyId('000102030405060708');
expect(keys).to.be.empty;
done();
});
it('getKeysForKeyId() - valid id', function(done) {
var keys = keyring.getKeysForKeyId(keyId.toLowerCase());
expect(keys).to.exist.and.have.length(1);
done();
});
});
});
describe('Encryption', function() {
var message = 'asdfs\n\nThursday, Nov 21, 2013 7:38 PM asdf@example.com wrote:\n' +