From 43c5d1b30ddd538fef1c6e192b2146ea24ea9585 Mon Sep 17 00:00:00 2001 From: Michal Kolodziej Date: Tue, 23 Apr 2013 10:19:01 +0200 Subject: [PATCH] Changed up the interface. Instead of a dedicated list, all packets are directly on the packetlist object. --- resources/openpgp.js | 35 ++++++++++--------- resources/openpgp.min.js | 14 ++++---- src/packet/compressed.js | 6 ++-- src/packet/packetlist.js | 15 ++++---- .../sym_encrypted_integrity_protected.js | 8 ++--- src/packet/symmetrically_encrypted.js | 6 ++-- test/general/packet.js | 28 +++++++-------- 7 files changed, 57 insertions(+), 55 deletions(-) diff --git a/resources/openpgp.js b/resources/openpgp.js index fc62bb08..7bb32d5f 100644 --- a/resources/openpgp.js +++ b/resources/openpgp.js @@ -9311,7 +9311,7 @@ function openpgp_msg_publickey() { */ function openpgp_packet_compressed() { this.tag = 8; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); this.algorithm = openpgp.compression.uncompressed; this.compressed = null; @@ -9402,7 +9402,7 @@ function openpgp_packet_compressed() { util.print_debug("decompressed:"+util.hexstrdump(decompressed)); - this.data.read(decompressed); + this.packets.read(decompressed); } /** @@ -9415,7 +9415,7 @@ function openpgp_packet_compressed() { switch (this.type) { case openpgp.compression.uncompressed: // - Uncompressed - this.compressed = this.data.write(); + this.compressed = this.packets.write(); break; case openpgp.compression.zip: // - ZIP [RFC1951] @@ -12387,11 +12387,11 @@ var openpgp_packet = new _openpgp_packet(); /** * @class * @classdesc This class represents a list of openpgp packets. + * Take care when iterating over it - the packets themselves + * are stored as numerical indices. */ function openpgp_packetlist() { - - /** @type {openpgp_packet_[]} A list of packets */ - this.packets = [] + this.length = 0; @@ -12419,9 +12419,9 @@ function openpgp_packetlist() { this.write = function() { var bytes = ''; - for(var i in this.packets) { - var packetbytes = this.packets[i].write(); - bytes += openpgp_packet.write_packet_header(this.packets[i].tag, packetbytes.length); + for(var i = 0; i < this.length; i++) { + var packetbytes = this[i].write(); + bytes += openpgp_packet.write_packet_header(this[i].tag, packetbytes.length); bytes += packetbytes; } @@ -12429,7 +12429,8 @@ function openpgp_packetlist() { } this.push = function(packet) { - this.packets.push(packet); + this[this.length] = packet; + this.length++; } } @@ -12473,7 +12474,7 @@ function openpgp_packet_sym_encrypted_integrity_protected() { * should be discarded. */ this.modification = false; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); /** @type {openpgp.symmetric} */ this.algorithm = openpgp.symmetric.plaintext; @@ -12502,7 +12503,7 @@ function openpgp_packet_sym_encrypted_integrity_protected() { } this.encrypt = function(symmetric_algorithm, key) { - var bytes = this.data.write() + var bytes = this.packets.write() var prefixrandom = openpgp_crypto_getPrefixRandom(symmetric_algorithm); var prefix = prefixrandom @@ -12553,12 +12554,12 @@ function openpgp_packet_sym_encrypted_integrity_protected() { util.print_debug_hexstr_dump("calc hash = ", this.hash); - this.data.read(decrypted.substr(0, decrypted.length - 22)); + this.packets.read(decrypted.substr(0, decrypted.length - 22)); var mdc = decrypted.substr(decrypted.length - 20, 20); if(this.hash != mdc) { - this.data = null; + this.packets = null; util.print_error("Decryption stopped: discovered a " + "modification of encrypted data."); return; @@ -12720,7 +12721,7 @@ function openpgp_packet_sym_encrypted_session_key() { function openpgp_packet_symmetrically_encrypted() { this.tag = 9; this.encrypted = null; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); @@ -12746,11 +12747,11 @@ function openpgp_packet_symmetrically_encrypted() { var decrypted = openpgp_crypto_symmetricDecrypt( symmetric_algorithm_type, key, this.encrypted, true); - this.data.read(decrypted); + this.packets.read(decrypted); } this.encrypt = function(algo, key) { - var data = this.data.write(); + var data = this.packets.write(); this.encrypted = openpgp_crypto_symmetricEncrypt( openpgp_crypto_getPrefixRandom(algo), algo, key, data, true); diff --git a/resources/openpgp.min.js b/resources/openpgp.min.js index ea52ec38..7d3181e8 100644 --- a/resources/openpgp.min.js +++ b/resources/openpgp.min.js @@ -352,9 +352,9 @@ function(){for(var b=" OPENPGP Public Key\n length: "+this.len+"\n",b=b+" return b};this.validate=function(){for(var b=0;b>24&255);c+=String.fromCharCode(Math.round(this.date.getTime()/1E3)>>16&255);c+=String.fromCharCode(Math.round(this.date.getTime()/1E3)>>8&255);c+=String.fromCharCode(Math.round(this.date.getTime()/ @@ -442,14 +442,14 @@ null;var e=-1,f=-1,f=0;0!=(a[b].charCodeAt()&64)&&(f=1);var g;f?e=a[b].charCodeA d);g=b+d;for(h=a.substring(b,b+d);;)if(192>a[g].charCodeAt()){j=a[g++].charCodeAt();d+=j;h+=a.substring(g,g+j);g+=j;break}else if(192<=a[g].charCodeAt()&&224>a[g].charCodeAt()){j=(a[g++].charCodeAt()-192<<8)+a[g++].charCodeAt()+192;d+=j;h+=a.substring(g,g+j);g+=j;break}else if(223a[g].charCodeAt())j=1<<(a[g++].charCodeAt()&31),d+=j,h+=a.substring(g,g+j),g+=j;else{g++;j=a[g++].charCodeAt()<<24|a[g++].charCodeAt()<<16|a[g++].charCodeAt()<<8|a[g++].charCodeAt();h+=a.substring(g, g+j);d+=j;g+=j;break}j=g}else b++,d=a[b++].charCodeAt()<<24|a[b++].charCodeAt()<<16|a[b++].charCodeAt()<<8|a[b++].charCodeAt();else switch(g){case 0:d=a[b++].charCodeAt();break;case 1:d=a[b++].charCodeAt()<<8|a[b++].charCodeAt();break;case 2:d=a[b++].charCodeAt()<<24|a[b++].charCodeAt()<<16|a[b++].charCodeAt()<<8|a[b++].charCodeAt();break}-1==j&&(j=d);null==h&&(h=a.substring(b,b+j));var a={},k;for(k in this.type)a[this.type[k]]=k;k="openpgp_packet_"+a[e];a=window[k];if(void 0==a)throw k;k=new a;k.read(h); return{packet:k,offset:b+d}};this.type={reserved:0,public_key_encrypted_session_key:1,signature:2,sym_encrypted_session_key:3,one_pass_signature:4,secret_key:5,public_key:6,secret_subkey:7,compressed:8,symmetrically_encrypted:9,marker:10,literal:11,trust:12,userid:13,public_subkey:14,user_attribute:17,sym_encrypted_integrity_protected:18,modification_detection_code:19}}var openpgp_packet=new _openpgp_packet; -function openpgp_packetlist(){this.packets=[];this.read=function(b){this.packets=[];for(var a=0;ae;e++)if(0==d>>e){c=e;break a}this.mpiBitLength=a+c;this.mpiByteLength=b.length;return this};this.toBin=function(){var b=String.fromCharCode(this.mpiBitLength>>8&255),b=b+String.fromCharCode(this.mpiBitLength&255);return b+=this.MPI};this.getByteLength=function(){return this.mpiByteLength}} diff --git a/src/packet/compressed.js b/src/packet/compressed.js index 5e026fc9..8b2c1b40 100644 --- a/src/packet/compressed.js +++ b/src/packet/compressed.js @@ -27,7 +27,7 @@ */ function openpgp_packet_compressed() { this.tag = 8; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); this.algorithm = openpgp.compression.uncompressed; this.compressed = null; @@ -118,7 +118,7 @@ function openpgp_packet_compressed() { util.print_debug("decompressed:"+util.hexstrdump(decompressed)); - this.data.read(decompressed); + this.packets.read(decompressed); } /** @@ -131,7 +131,7 @@ function openpgp_packet_compressed() { switch (this.type) { case openpgp.compression.uncompressed: // - Uncompressed - this.compressed = this.data.write(); + this.compressed = this.packets.write(); break; case openpgp.compression.zip: // - ZIP [RFC1951] diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index cd24a9cc..df3c1ca7 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -3,11 +3,11 @@ /** * @class * @classdesc This class represents a list of openpgp packets. + * Take care when iterating over it - the packets themselves + * are stored as numerical indices. */ function openpgp_packetlist() { - - /** @type {openpgp_packet_[]} A list of packets */ - this.packets = [] + this.length = 0; @@ -35,9 +35,9 @@ function openpgp_packetlist() { this.write = function() { var bytes = ''; - for(var i in this.packets) { - var packetbytes = this.packets[i].write(); - bytes += openpgp_packet.write_packet_header(this.packets[i].tag, packetbytes.length); + for(var i = 0; i < this.length; i++) { + var packetbytes = this[i].write(); + bytes += openpgp_packet.write_packet_header(this[i].tag, packetbytes.length); bytes += packetbytes; } @@ -45,7 +45,8 @@ function openpgp_packetlist() { } this.push = function(packet) { - this.packets.push(packet); + this[this.length] = packet; + this.length++; } } diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index c4a9098a..6bfc90f9 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -38,7 +38,7 @@ function openpgp_packet_sym_encrypted_integrity_protected() { * should be discarded. */ this.modification = false; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); /** @type {openpgp.symmetric} */ this.algorithm = openpgp.symmetric.plaintext; @@ -67,7 +67,7 @@ function openpgp_packet_sym_encrypted_integrity_protected() { } this.encrypt = function(symmetric_algorithm, key) { - var bytes = this.data.write() + var bytes = this.packets.write() var prefixrandom = openpgp_crypto_getPrefixRandom(symmetric_algorithm); var prefix = prefixrandom @@ -118,12 +118,12 @@ function openpgp_packet_sym_encrypted_integrity_protected() { util.print_debug_hexstr_dump("calc hash = ", this.hash); - this.data.read(decrypted.substr(0, decrypted.length - 22)); + this.packets.read(decrypted.substr(0, decrypted.length - 22)); var mdc = decrypted.substr(decrypted.length - 20, 20); if(this.hash != mdc) { - this.data = null; + this.packets = null; util.print_error("Decryption stopped: discovered a " + "modification of encrypted data."); return; diff --git a/src/packet/symmetrically_encrypted.js b/src/packet/symmetrically_encrypted.js index 490ef9f8..2e1902c6 100644 --- a/src/packet/symmetrically_encrypted.js +++ b/src/packet/symmetrically_encrypted.js @@ -29,7 +29,7 @@ function openpgp_packet_symmetrically_encrypted() { this.tag = 9; this.encrypted = null; - this.data = new openpgp_packetlist(); + this.packets = new openpgp_packetlist(); @@ -55,11 +55,11 @@ function openpgp_packet_symmetrically_encrypted() { var decrypted = openpgp_crypto_symmetricDecrypt( symmetric_algorithm_type, key, this.encrypted, true); - this.data.read(decrypted); + this.packets.read(decrypted); } this.encrypt = function(algo, key) { - var data = this.data.write(); + var data = this.packets.write(); this.encrypted = openpgp_crypto_symmetricEncrypt( openpgp_crypto_getPrefixRandom(algo), algo, key, data, true); diff --git a/test/general/packet.js b/test/general/packet.js index 7a27da2c..c6f1e4bd 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -7,7 +7,7 @@ unittests.register("Packet testing", function() { literal.set_data('Hello world', openpgp_packet_literal.format.utf8); var enc = new openpgp_packet_symmetrically_encrypted(); - enc.data.push(literal); + enc.packets.push(literal); var key = '12345678901234567890123456789012', algo = openpgp.symmetric.aes256; @@ -21,10 +21,11 @@ unittests.register("Packet testing", function() { var msg2 = new openpgp_packetlist(); msg2.read(message.write()); - msg2.packets[0].decrypt(algo, key); + msg2[0].decrypt(algo, key); + + return new test_result('Symmetrically encrypted packet', + msg2[0].packets[0].data == literal.data); - return new test_result('Symmetrically encrypted data packet', - msg2.packets[0].data.packets[0].data == literal.data); }, function() { var key = '12345678901234567890123456789012', algo = openpgp.symmetric.aes256; @@ -34,17 +35,17 @@ unittests.register("Packet testing", function() { msg = new openpgp_packetlist(); literal.set_data('Hello world!', openpgp_packet_literal.format.utf8); - enc.data.push(literal); + enc.packets.push(literal); enc.encrypt(algo, key); msg.push(enc); var msg2 = new openpgp_packetlist(); msg2.read(msg.write()); - msg2.packets[0].decrypt(algo, key); + msg2[0].decrypt(algo, key); - return new test_result('Sym. encrypted integrity protected data packet', - msg2.packets[0].data.packets[0].data == literal.data); + return new test_result('Sym. encrypted integrity protected packet', + msg2[0].packets[0].data == literal.data); }, function() { @@ -64,14 +65,13 @@ unittests.register("Packet testing", function() { var parsed = new openpgp_packetlist(); parsed.read(msgbytes); - parsed.packets[0].decrypt('test'); + parsed[0].decrypt('test'); - var key = parsed.packets[0].key; - parsed.packets[1].decrypt(parsed.packets[0].algorithm, key); - var compressed = parsed.packets[1].data.packets[0]; - compressed.decompress(); + var key = parsed[0].key; + parsed[1].decrypt(parsed[0].algorithm, key); + var compressed = parsed[1].packets[0]; - var result = compressed.data.packets[0].data; + var result = compressed.packets[0].data; return new test_result('Sym encrypted session key with a compressed packet', result == 'Hello world!\n');