Changed unitttests and keyring to use bundled openpgp and updated keyring to use openpgp.key instead of packetlists.

Added getUserIds() to key.

Reenabled keyring ci tests.
This commit is contained in:
Robert Nelson 2013-12-08 15:41:31 -08:00
parent 30332003f8
commit bfba0eca0c
22 changed files with 144 additions and 15701 deletions

View File

@ -23,11 +23,12 @@ module.exports = function(grunt) {
}, },
unittests: { unittests: {
files: { files: {
'test/test-bundle.js': [] 'test/lib/test-bundle.js': []
}, },
options: { options: {
debug: true, debug: true,
alias: './test/test-all.js:test-bundle.js' alias: './test/test-all.js:unittests',
external: [ 'openpgp' ]
} }
}, },
ci_tests: { ci_tests: {

View File

@ -35,7 +35,7 @@ bundle:
@browserify -d -r ./src/:openpgp > ./resources/openpgp.debug.js @browserify -d -r ./src/:openpgp > ./resources/openpgp.debug.js
bundle-test: bundle-test:
@browserify -d -r ./test/test-all.js:test-bundle.js > ./test/test-bundle.js @browserify -d -r ./test/test-all.js:unittests > ./test/lib/test-bundle.js
bundle-ci-test: bundle-ci-test:
@browserify -d -x openpgp -r ./test/ci-tests-all.js:ci-tests > ./test/lib/ci-tests-bundle.js @browserify -d -x openpgp -r ./test/ci-tests-all.js:ci-tests > ./test/lib/ci-tests-bundle.js

File diff suppressed because one or more lines are too long

View File

@ -119,6 +119,19 @@ Key.prototype.getPrivateKeyPacket = function(keyIds) {
return findKey(keys, keyIds); return findKey(keys, keyIds);
}; };
/**
* Returns userids
* @return {string} userid[]
*/
Key.prototype.getUserIds = function() {
var userids = [];
var useridPackets = this.packets.filterByTag(enums.packet.userid);
for (var i = 0; i < useridPackets.length; i++) {
userids.push(useridPackets[i].write());
}
return userids;
};
/** /**
* Returns true if this is a public key * Returns true if this is a public key
* @return {Boolean} * @return {Boolean}

View File

@ -15,17 +15,14 @@
// License along with this library; if not, write to the Free Software // License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var packet = require('./packet'); var openpgp = require('openpgp');
var enums = require('./enums.js');
var armor = require('./encoding/armor.js');
/** /**
* @class * @class
* @classdesc The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage. * @classdesc The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
*/ */
var keyring = function() { var keyring = function() {
this.armoredPacketlists = []; this.keys = [];
this.parsedPacketlists = [];
/** /**
* Initialization routine for the keyring. This method reads the * Initialization routine for the keyring. This method reads the
@ -33,17 +30,15 @@ var keyring = function() {
* This method is called by openpgp.init(). * This method is called by openpgp.init().
*/ */
function init() { function init() {
var armoredPacketlists = JSON.parse(window.localStorage.getItem("armoredPacketlists")); var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys"));
if (armoredPacketlists === null || armoredPacketlists.length === 0) { if (armoredKeys !== null && armoredKeys.length === 0) {
armoredPacketlists = []; var key;
} for (var i = 0; i < armoredKeys.length; i++) {
this.armoredPacketlists = armoredPacketlists; key = openpgp.key.readArmored(armoredKeys[i]);
this.keys.push(key);
var packetlist; }
for (var i = 0; i < armoredPacketlists.length; i++) { } else {
packetlist = new packet.list(); this.keys = [];
packetlist.read(armoredPacketlists[i]);
this.parsedPacketlists.push(packetlist);
} }
} }
this.init = init; this.init = init;
@ -53,60 +48,50 @@ var keyring = function() {
* The privateKeys array and publicKeys array gets Stringified using JSON * The privateKeys array and publicKeys array gets Stringified using JSON
*/ */
function store() { function store() {
window.localStorage.setItem("armoredPacketlists", JSON.stringify(this.armoredPacketlists)); var armoredKeys = [];
for (var i = 0; i < this.keys.length; i++) {
armoredKeys.push(this.keys[i].armor());
}
window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys));
} }
this.store = store; this.store = store;
function emailPacketCheck(packet, email) { function emailKeyCheck(key, email) {
var emailMatch = false;
var packetEmail;
email = email.toLowerCase(); email = email.toLowerCase();
if (packet.tag == enums.packet.userid) { var keyEmails = key.getUserIds();
packetEmail = packet.userid; for (var i; i < keyEmails.length; i++) {
//we need to get just the email from the userid packet //we need to get just the email from the userid key
packetEmail = packetEmail.split('<')[1].split('<')[0].trim.toLowerCase(); keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase();
if (packetEmail == email) { if (keyEmail == email) {
emailMatch = true;
}
}
return emailMatch;
}
function idPacketCheck(packet, id) {
if (packet.getKeyId && packet.getKeyId().write() == id) {
return true;
}
return false;
}
function helperCheckIdentityAndPacketMatch(identityFunction, identityInput, packetType, packetlist) {
var packet;
for (var l = 0; l < packetlist.length; l++) {
packet = packetlist[l];
identityMatch = identityFunction(packet, identityInput);
if (!packetType) {
packetMatch = true;
} else if (packet.tag == packetType) {
packetMatch = true;
}
if (packetMatch && identityMatch) {
return true; return true;
} }
} }
return false; return false;
} }
function checkForIdentityAndPacketMatch(identityFunction, identityInput, packetType) { function idKeyCheck(key, id) {
var keyid = key.getKeyId();
if (keyid && keyid.write() == id) {
return true;
}
return false;
}
function checkForIdentityAndPacketMatch(identityFunction, identityInput, keyType) {
var results = []; var results = [];
var packetlist; for (var p = 0; p < this.keys.length; p++) {
var identityMatch; var key = this.keys[p];
var packetMatch; switch (keyType) {
for (var p = 0; p < this.parsedPacketlists.length; p++) { case openpgp.enums.packet.public_key:
identityMatch = false; if (key.isPublic() && identityFunction(identityInput, key)) {
packetMatch = false; results.push(key);
packetlist = this.parsedPacketlists[p]; }
if (helperCheckIdentityAndPacketMatch(identityFunction, identityInput, packetType, packetlist)) { break;
results.push(packetlist); case openpgp.enums.packet.private_key:
if (key.isPrivate() && identityFunction(identityInput, key)) {
results.push(key);
}
break;
} }
} }
return results; return results;
@ -115,74 +100,76 @@ var keyring = function() {
/** /**
* searches all public keys in the keyring matching the address or address part of the user ids * searches all public keys in the keyring matching the address or address part of the user ids
* @param {String} email_address * @param {String} email email address to search for
* @return {openpgp_msg_publickey[]} The public keys associated with provided email address. * @return {openpgp.key.Key[]} The public keys associated with provided email address.
*/ */
function getPublicKeyForAddress(email) { function getPublicKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, enums.packet.public_key); return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.public_key);
} }
this.getPublicKeyForAddress = getPublicKeyForAddress; this.getPublicKeyForAddress = getPublicKeyForAddress;
/** /**
* Searches the keyring for a private key containing the specified email address * Searches the keyring for a private key containing the specified email address
* @param {String} email_address email address to search for * @param {String} email email address to search for
* @return {openpgp_msg_privatekey[]} private keys found * @return {openpgp.key.Key[]} private keys found
*/ */
function getPrivateKeyForAddress(email_address) { function getPrivateKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, enums.packet.secret_key); return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.secret_key);
} }
this.getPrivateKeyForAddress = getPrivateKeyForAddress; this.getPrivateKeyForAddress = getPrivateKeyForAddress;
/** /**
* Searches the keyring for public keys having the specified key id * Searches the keyring for public keys having the specified key id
* @param {String} keyId provided as string of hex number (lowercase) * @param {String} keyId provided as string of hex number (lowercase)
* @return {openpgp_msg_privatekey[]} public keys found * @return {openpgp.key.Key[]} public keys found
*/ */
function getPacketlistsForKeyId(keyId) { function getKeysForKeyId(keyId) {
return this.checkForIdentityAndPacketMatch(idPacketCheck, keyId); return this.checkForIdentityAndPacketMatch(idPacketCheck, keyId);
} }
this.getPacketlistsForKeyId = getPacketlistsForKeyId; this.getKeysForKeyId = getKeysForKeyId;
/** /**
* Imports a packet list (public or private key block) from an ascii armored message * Imports a key from an ascii armored message
* @param {String} armored message to read the packets/key from * @param {String} armored message to read the keys/key from
*/ */
function importPacketlist(armored) { function importKey(armored) {
this.armoredPacketlists.push(armored); this.keys.push(openpgp.key.readArmored(armored));
var dearmored = armor.decode(armored.replace(/\r/g, '')).data;
packetlist = new packet.list();
packetlist.read(dearmored);
this.parsedPacketlists.push(packetlist);
return true; return true;
} }
this.importPacketlist = importPacketlist; this.importKey = importKey;
/** /**
* TODO * returns the armored message representation of the key at key ring index
* returns the openpgp_msg_privatekey representation of the public key at public key ring index * @param {Integer} index the index of the key within the array
* @param {Integer} index the index of the public key within the publicKeys array * @return {String} armored message representing the key object
* @return {openpgp_msg_privatekey} the public key object
*/ */
function exportPublicKey(index) { function exportKey(index) {
return this.publicKey[index]; return this.keys[index].armor();
} }
this.exportPublicKey = exportPublicKey; this.exportKey = exportKey;
/** /**
* TODO
* Removes a public key from the public key keyring at the specified index * Removes a public key from the public key keyring at the specified index
* @param {Integer} index the index of the public key within the publicKeys array * @param {Integer} index the index of the public key within the publicKeys array
* @return {openpgp_msg_privatekey} The public key object which has been removed * @return {openpgp.key.Key} The public key object which has been removed
*/ */
function removePublicKey(index) { function removeKey(index) {
var removed = this.publicKeys.splice(index, 1); var removed = this.keys.splice(index, 1);
this.store();
return removed; return removed;
} }
this.removePublicKey = removePublicKey; this.removeKey = removeKey;
/**
* returns the armored message representation of the public key portion of the key at key ring index
* @param {Integer} index the index of the key within the array
* @return {String} armored message representing the public key object
*/
function exportPublicKey(index) {
return this.keys[index].toPublic().armor();
}
this.exportPublicKey = exportPublicKey;
}; };

View File

@ -1,5 +1,5 @@
var openpgp = require('openpgp'); var openpgp = require('openpgp'),
//keyring = require('../../src/keyring.js'); keyring = require('../src/keyring.js');
'use strict'; 'use strict';
@ -61,17 +61,17 @@ describe('Openpgp integration tests', function() {
}); });
}); });
// describe('Import key pair', function() { describe('Import key pair', function() {
// it('should work', function(done) { it('should work', function(done) {
// // clear any keypair already in the keychain // clear any keypair already in the keychain
// keyring.init(); keyring.init();
// // import private key // import private key
// keyring.importPacketlist(privkey); keyring.importKey(privkey);
// // import public key // import public key
// keyring.importPacketlist(pubkey); keyring.importKey(pubkey);
// done(); done();
// }); });
// }); });
describe('Encryption', function() { describe('Encryption', function() {
var message = 'asdfs\n\nThursday, Nov 21, 2013 7:38 PM asdf@example.com wrote:\n' + var message = 'asdfs\n\nThursday, Nov 21, 2013 7:38 PM asdf@example.com wrote:\n' +

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt", function() { unit.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt", function() {
var openpgp = require('../../../'); var openpgp = require('openpgp');
var util = openpgp.util; var util = openpgp.util;
var result = new Array(); var result = new Array();

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt", function() { unit.register("Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
BFencrypt = openpgp.crypto.cipher.blowfish; BFencrypt = openpgp.crypto.cipher.blowfish;

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("CAST-128 cipher test with test vectors from RFC2144", function() { unit.register("CAST-128 cipher test with test vectors from RFC2144", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util; util = openpgp.util;
var result = []; var result = [];

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf", function() { unit.register("TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util; util = openpgp.util;
var result = []; var result = [];
@ -99,7 +99,7 @@ unit.register("TripleDES (EDE) cipher test with test vectors from http://csrc.ni
unit.register("DES encrypt/decrypt padding tests", function () { unit.register("DES encrypt/decrypt padding tests", function () {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util; util = openpgp.util;
var result = []; var result = [];

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("Twofish test with test vectors from http://www.schneier.com/code/ecb_ival.txt", function() { unit.register("Twofish test with test vectors from http://www.schneier.com/code/ecb_ival.txt", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util; util = openpgp.util;
function TFencrypt(block, key) { function TFencrypt(block, key) {

View File

@ -1,7 +1,7 @@
var unit = require('../unit.js'); var unit = require('../unit.js');
unit.register("Functional testing of openpgp.crypto.* methods", function() { unit.register("Functional testing of openpgp.crypto.* methods", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var util = openpgp.util; var util = openpgp.util;
var result = []; var result = [];
var RSApubMPIstrs = [ var RSApubMPIstrs = [

Binary file not shown.

View File

@ -1,7 +1,7 @@
var unit = require('../../unit.js'); var unit = require('../../unit.js');
unit.register("MD5 test with test vectors from RFC 1321", function() { unit.register("MD5 test with test vectors from RFC 1321", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
MD5 = openpgp.crypto.hash.md5; MD5 = openpgp.crypto.hash.md5;

View File

@ -2,7 +2,7 @@ var unit = require('../../unit.js');
unit.register("RIPE-MD 160 bits test with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function() { unit.register("RIPE-MD 160 bits test with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
RMDstring = openpgp.crypto.hash.ripemd; RMDstring = openpgp.crypto.hash.ripemd;

View File

@ -2,7 +2,7 @@ var unit = require('../../unit.js');
unit.register("SHA* test with test vectors from NIST FIPS 180-2", function() { unit.register("SHA* test with test vectors from NIST FIPS 180-2", function() {
var openpgp = require('../../../'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
hash = openpgp.crypto.hash; hash = openpgp.crypto.hash;

View File

@ -1,7 +1,7 @@
var unit = require('../unit.js'); var unit = require('../unit.js');
unit.register("Key generation/encryption/decryption", function() { unit.register("Key generation/encryption/decryption", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var result = []; var result = [];
var testHelper = function(passphrase, userid, message) { var testHelper = function(passphrase, userid, message) {
var key = openpgp.generateKeyPair(openpgp.enums.publicKey.rsa_encrypt_sign, 512, var key = openpgp.generateKeyPair(openpgp.enums.publicKey.rsa_encrypt_sign, 512,
@ -37,7 +37,7 @@ unit.register("Key generation/encryption/decryption", function() {
}); });
unit.register("Message encryption/decryption", function() { unit.register("Message encryption/decryption", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var result = []; var result = [];

View File

@ -1,7 +1,7 @@
var unit = require('../unit.js'); var unit = require('../unit.js');
unit.register("Keyring testing", function() { unit.register("Keyring testing", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var keyring = require('../../src/keyring.js'); var keyring = require('../../src/keyring.js');
var result = []; var result = [];

View File

@ -2,7 +2,7 @@ var unit = require('../unit.js');
unit.register("Packet testing", function() { unit.register("Packet testing", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var armored_key = var armored_key =
'-----BEGIN PGP PRIVATE KEY BLOCK-----\n' + '-----BEGIN PGP PRIVATE KEY BLOCK-----\n' +

View File

@ -1,7 +1,7 @@
var unit = require('../unit.js'); var unit = require('../unit.js');
unit.register("Signature testing", function() { unit.register("Signature testing", function() {
var openpgp = require('../../'); var openpgp = require('openpgp');
var priv_key_arm1 = var priv_key_arm1 =
[ '-----BEGIN PGP PRIVATE KEY BLOCK-----', [ '-----BEGIN PGP PRIVATE KEY BLOCK-----',

View File

@ -56,10 +56,11 @@
</style> </style>
<script src="lib/openpgp.debug.js"></script>
<script src="lib/test-bundle.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var test = require('unittests');
var test = require('test-bundle.js');
function unit_tests() { function unit_tests() {
var table = document.getElementById('unittests'); var table = document.getElementById('unittests');

File diff suppressed because one or more lines are too long