diff --git a/resources/openpgp.js b/resources/openpgp.js index 85a98c58..1f676c70 100644 --- a/resources/openpgp.js +++ b/resources/openpgp.js @@ -3329,14 +3329,11 @@ function openpgp_packet_keymaterial() { * @param key [RSA.keyObject] * @return {body: [string]OpenPGP packet body contents, header: [string] OpenPGP packet header, string: [string] header+body} */ - function write_private_key(keyType, key, password, s2kHash, symmetricEncryptionAlgorithm){ + function write_private_key(keyType, key, password, s2kHash, symmetricEncryptionAlgorithm, timePacket){ this.symmetricEncryptionAlgorithm = symmetricEncryptionAlgorithm; var tag = 5; var body = String.fromCharCode(4); - //TODO make the date into a util function - var d = new Date(); - d = d.getTime()/1000; - body += String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); + body += timePacket; switch(keyType){ case 1: body += String.fromCharCode(keyType);//public key algo @@ -3409,13 +3406,10 @@ function openpgp_packet_keymaterial() { * @param key [RSA.keyObject] * @return {body: [string]OpenPGP packet body contents, header: [string] OpenPGP packet header, string: [string] header+body} */ - function write_public_key(keyType, key){ + function write_public_key(keyType, key, timePacket){ var tag = 6; var body = String.fromCharCode(4); - //TODO make the date into a util function - var d = new Date(); - d = d.getTime()/1000; - body += String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); + body += timePacket; switch(keyType){ case 1: body += String.fromCharCode(1);//public key algo @@ -9663,12 +9657,15 @@ function openpgp_crypto_testRSA(key){ function openpgp_crypto_generateKeyPair(keyType, numBits, passphrase, s2kHash, symmetricEncryptionAlgorithm){ var privKeyPacket; var publicKeyPacket; + var d = new Date(); + d = d.getTime()/1000; + var timePacket = String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); switch(keyType){ case 1: var rsa = new RSA(); var key = rsa.generate(numBits,"10001"); - privKeyPacket = new openpgp_packet_keymaterial().write_private_key(keyType, key, passphrase, s2kHash, symmetricEncryptionAlgorithm); - publicKeyPacket = new openpgp_packet_keymaterial().write_public_key(keyType, key); + privKeyPacket = new openpgp_packet_keymaterial().write_private_key(keyType, key, passphrase, s2kHash, symmetricEncryptionAlgorithm, timePacket); + publicKeyPacket = new openpgp_packet_keymaterial().write_public_key(keyType, key, timePacket); break; default: util.print_error("Unknown keytype "+keyType) diff --git a/resources/openpgp.min.js b/resources/openpgp.min.js index 5da05702..76eb175c 100644 --- a/resources/openpgp.min.js +++ b/resources/openpgp.min.js @@ -98,12 +98,11 @@ e.toString());default:return this.data=b,this.position=c-this.parentNode.packetL e);a+=e.packetLength+e.headerLength;break;default:return this.data=b,this.position=c-this.parentNode.packetLength,this.len=a-c}this.data=b;this.position=c-this.parentNode.packetLength;return this.len=a-c}util.print_error("openpgp.packet.keymaterial.js\nunknown parent node for a key material packet "+a.tagType)};this.verifyKey=function(){if(14==this.tagType){if(null==this.subKeySignature)return 0;if(4==this.subKeySignature.version&&null!=this.subKeySignature.keyNeverExpires&&!this.subKeySignature.keyNeverExpires&& new Date(1E3*this.subKeySignature.keyExpirationTime+this.creationTime.getTime())this.publicKeyAlgorithm){var a=this.MPIs[0].substring(this.MPIs[0].mpiByteLength-8);util.print_debug("openpgp.msg.publickey read_nodes:\nV3 key ID: "+a);return a}};this.getFingerprint=function(){if(4==this.version)return tohash=String.fromCharCode(153)+ -String.fromCharCode(this.packetdata.length>>8&255)+String.fromCharCode(this.packetdata.length&255)+this.packetdata,util.print_debug("openpgp.msg.publickey creating subkey fingerprint by hashing:"+util.hexstrdump(tohash)+"\npublickeyalgorithm: "+this.publicKeyAlgorithm),str_sha1(tohash,tohash.length);if(3==this.version&&0this.publicKeyAlgorithm)return MD5(this.MPIs[0].MPI)};this.write_private_key=function(a,b,c,d,e){this.symmetricEncryptionAlgorithm=e;var e=String.fromCharCode(4), -f=new Date,f=f.getTime()/1E3,e=e+(String.fromCharCode(Math.floor(f/16777216%256))+String.fromCharCode(Math.floor(f/65536%256))+String.fromCharCode(Math.floor(f/256%256))+String.fromCharCode(Math.floor(f%256)));switch(a){case 1:e+=String.fromCharCode(a);e+=b.n.toMPI();e+=b.ee.toMPI();if(c){e+=String.fromCharCode(254);e+=String.fromCharCode(this.symmetricEncryptionAlgorithm);e+=String.fromCharCode(3);e+=String.fromCharCode(d);a=b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI();b=str_sha1(a);util.print_debug_hexstr_dump("write_private_key sha1: ", -b);f=openpgp_crypto_getRandomBytes(8);util.print_debug_hexstr_dump("write_private_key Salt: ",f);var e=e+f,g=openpgp_crypto_getSecureRandomOctet(),e=e+String.fromCharCode(g);util.print_debug("write_private_key c: "+g);c=(new openpgp_type_s2k).write(3,d,c,f,g);switch(this.symmetricEncryptionAlgorithm){case 3:this.IVLength=8;this.IV=openpgp_crypto_getRandomBytes(this.IVLength);ciphertextMPIs=normal_cfb_encrypt(function(a,b){var c=new openpgp_symenc_cast5;c.setKey(b);return c.encrypt(util.str2bin(a))}, -this.IVLength,util.str2bin(c.substring(0,16)),a+b,this.IV);e+=this.IV+ciphertextMPIs;break;case 7:case 8:case 9:this.IVLength=16,this.IV=openpgp_crypto_getRandomBytes(this.IVLength),ciphertextMPIs=normal_cfb_encrypt(AESencrypt,this.IVLength,c,a+b,this.IV),e+=this.IV+ciphertextMPIs}}else e+=String.fromCharCode(0),e+=b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI(),c=util.calc_checksum(b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI()),e+=String.fromCharCode(c/256)+String.fromCharCode(c%256),util.print_debug_hexstr_dump("write_private_key basic checksum: "+ -c);break;default:e="",util.print_error("openpgp.packet.keymaterial.js\nerror writing private key, unknown type :"+a)}c=openpgp_packet.write_packet_header(5,e.length);return{string:c+e,header:c,body:e}};this.write_public_key=function(a,b){var c=String.fromCharCode(4),d=new Date,d=d.getTime()/1E3,c=c+(String.fromCharCode(Math.floor(d/16777216%256))+String.fromCharCode(Math.floor(d/65536%256))+String.fromCharCode(Math.floor(d/256%256))+String.fromCharCode(Math.floor(d%256)));switch(a){case 1:c+=String.fromCharCode(1); -c+=b.n.toMPI();c+=b.ee.toMPI();break;default:util.print_error("openpgp.packet.keymaterial.js\nerror writing private key, unknown type :"+a)}d=openpgp_packet.write_packet_header(6,c.length);return{string:d+c,header:d,body:c}}} +String.fromCharCode(this.packetdata.length>>8&255)+String.fromCharCode(this.packetdata.length&255)+this.packetdata,util.print_debug("openpgp.msg.publickey creating subkey fingerprint by hashing:"+util.hexstrdump(tohash)+"\npublickeyalgorithm: "+this.publicKeyAlgorithm),str_sha1(tohash,tohash.length);if(3==this.version&&0this.publicKeyAlgorithm)return MD5(this.MPIs[0].MPI)};this.write_private_key=function(a,b,c,d,e,f){this.symmetricEncryptionAlgorithm=e;e=String.fromCharCode(4); +e+=f;switch(a){case 1:e+=String.fromCharCode(a);e+=b.n.toMPI();e+=b.ee.toMPI();if(c){e+=String.fromCharCode(254);e+=String.fromCharCode(this.symmetricEncryptionAlgorithm);e+=String.fromCharCode(3);e+=String.fromCharCode(d);a=b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI();b=str_sha1(a);util.print_debug_hexstr_dump("write_private_key sha1: ",b);f=openpgp_crypto_getRandomBytes(8);util.print_debug_hexstr_dump("write_private_key Salt: ",f);var e=e+f,g=openpgp_crypto_getSecureRandomOctet(),e=e+String.fromCharCode(g); +util.print_debug("write_private_key c: "+g);c=(new openpgp_type_s2k).write(3,d,c,f,g);switch(this.symmetricEncryptionAlgorithm){case 3:this.IVLength=8;this.IV=openpgp_crypto_getRandomBytes(this.IVLength);ciphertextMPIs=normal_cfb_encrypt(function(a,b){var c=new openpgp_symenc_cast5;c.setKey(b);return c.encrypt(util.str2bin(a))},this.IVLength,util.str2bin(c.substring(0,16)),a+b,this.IV);e+=this.IV+ciphertextMPIs;break;case 7:case 8:case 9:this.IVLength=16,this.IV=openpgp_crypto_getRandomBytes(this.IVLength), +ciphertextMPIs=normal_cfb_encrypt(AESencrypt,this.IVLength,c,a+b,this.IV),e+=this.IV+ciphertextMPIs}}else e+=String.fromCharCode(0),e+=b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI(),c=util.calc_checksum(b.d.toMPI()+b.p.toMPI()+b.q.toMPI()+b.u.toMPI()),e+=String.fromCharCode(c/256)+String.fromCharCode(c%256),util.print_debug_hexstr_dump("write_private_key basic checksum: "+c);break;default:e="",util.print_error("openpgp.packet.keymaterial.js\nerror writing private key, unknown type :"+a)}c=openpgp_packet.write_packet_header(5, +e.length);return{string:c+e,header:c,body:e}};this.write_public_key=function(a,b,c){var d=String.fromCharCode(4),d=d+c;switch(a){case 1:d+=String.fromCharCode(1);d+=b.n.toMPI();d+=b.ee.toMPI();break;default:util.print_error("openpgp.packet.keymaterial.js\nerror writing private key, unknown type :"+a)}a=openpgp_packet.write_packet_header(6,d.length);return{string:a+d,header:a,body:d}}} function MD5(a){function b(a){for(i=0;i>b)+(1073741824>>b-1):a>>b}function e(a,b){for(var a=c(a),b=c(b),d=0;dy[0]||(y[1]++,y[0]-=4294967296);y[0]+=8;z[b]=f(a,255);if(63<=b){var a=z,c=b=0,g=0,h=0,k=v;b=x[0];c=x[1];g=x[2];h=x[3];for(i=0;16>i;i++){k[i]=f(a[4*i+0],255);for(j=1;4>j;j++)k[i]+=e(f(a[4*i+j+0],255),8*j)}b=m(b,c,g,h,k[0],N,3614090360);h=m(h,b,c,g,k[1],M,3905402710);g=m(g,h,b,c,k[2],D,606105819);c=m(c,g,h,b,k[3],K,3250441966);b=m(b,c,g,h,k[4],N,4118548399);h=m(h,b,c,g,k[5],M,1200080426); @@ -367,7 +366,8 @@ function openpgp_crypto_getHashByteLength(a){switch(a){case 1:return 16;case 2:c function openpgp_crypto_getSecureRandom(a,b){var c=new Uint32Array(1);window.crypto.getRandomValues(c);for(var d=(b-a).toString(2).length;(c[0]&Math.pow(2,d)-1)>b-a;)window.crypto.getRandomValues(c);return a+Math.abs(c[0]&Math.pow(2,d)-1)}function openpgp_crypto_getSecureRandomOctet(){var a=new Uint32Array(1);window.crypto.getRandomValues(a);return a[0]&255} function openpgp_crypto_getRandomBigInteger(a){if(0>a)return null;var b=openpgp_crypto_getRandomBytes(Math.floor((a+7)/8));0=b.compareTo(a))){for(var c=b.subtract(a),d=openpgp_crypto_getRandomBigInteger(c.bitLength());d>c;)d=openpgp_crypto_getRandomBigInteger(c.bitLength());return a.add(d)}} function openpgp_crypto_testRSA(a){debugger;var b=new RSA,c=new openpgp_type_mpi;c.create(openpgp_encoding_eme_pkcs1_encode("ABABABAB",128));c=b.encrypt(c.toBigInteger(),a.ee,a.n);b.decrypt(c,a.d,a.p,a.q,a.u)} -function openpgp_crypto_generateKeyPair(a,b,c,d,e){var f,g;switch(a){case 1:b=(new RSA).generate(b,"10001");f=(new openpgp_packet_keymaterial).write_private_key(a,b,c,d,e);g=(new openpgp_packet_keymaterial).write_public_key(a,b);break;default:util.print_error("Unknown keytype "+a)}return{privateKey:f,publicKey:g}} +function openpgp_crypto_generateKeyPair(a,b,c,d,e){var f,g,h=new Date,h=h.getTime()/1E3,h=String.fromCharCode(Math.floor(h/16777216%256))+String.fromCharCode(Math.floor(h/65536%256))+String.fromCharCode(Math.floor(h/256%256))+String.fromCharCode(Math.floor(h%256));switch(a){case 1:b=(new RSA).generate(b,"10001");f=(new openpgp_packet_keymaterial).write_private_key(a,b,c,d,e,h);g=(new openpgp_packet_keymaterial).write_public_key(a,b,h);break;default:util.print_error("Unknown keytype "+a)}return{privateKey:f, +publicKey:g}} function _openpgp(){this.tostring="";this.generate_key_pair=function(a,b,c,d){var e=(new openpgp_packet_userid).write_packet(c),b=openpgp_crypto_generateKeyPair(a,b,d,openpgp.config.config.prefer_hash_algorithm,3),a=b.privateKey,f=(new openpgp_packet_keymaterial).read_priv_key(a.string,3,a.string.length);f.decryptSecretMPIs(d)||util.print_error("Issue creating key. Unable to read resulting private key");d=new openpgp_msg_privatekey;d.privateKeyPacket=f;d.getPreferredSignatureHashAlgorithm=function(){return openpgp.config.config.prefer_hash_algorithm}; f=d.privateKeyPacket.publicKey.data;f=String.fromCharCode(153)+String.fromCharCode(f.length>>8&255)+String.fromCharCode(f.length&255)+f+String.fromCharCode(180)+String.fromCharCode(c.length>>24)+String.fromCharCode(c.length>>16&255)+String.fromCharCode(c.length>>8&255)+String.fromCharCode(c.length&255)+c;c=new openpgp_packet_signature;c=c.write_message_signature(16,f,d);b=openpgp_encoding_armor(4,b.publicKey.string+e+c.openpgp);e=openpgp_encoding_armor(5,a.string+e+c.openpgp);return{privateKey:d, privateKeyArmored:e,publicKeyArmored:b}};this.write_signed_message=function(a,b){var c=(new openpgp_packet_signature).write_message_signature(1,b.replace(/\r\n/g,"\n").replace(/\n/,"\r\n"),a),c={text:b.replace(/\r\n/g,"\n").replace(/\n/,"\r\n"),openpgp:c.openpgp,hash:c.hash};return openpgp_encoding_armor(2,c,null,null)};this.write_signed_and_encrypted_message=function(a,b,c){var d="",e=(new openpgp_packet_literaldata).write_packet(c.replace(/\r\n/g,"\n").replace(/\n/g,"\r\n"));util.print_debug_hexstr_dump("literal_packet: |"+ diff --git a/src/ciphers/openpgp.crypto.js b/src/ciphers/openpgp.crypto.js index c57286ef..8f0a9ca7 100644 --- a/src/ciphers/openpgp.crypto.js +++ b/src/ciphers/openpgp.crypto.js @@ -476,12 +476,15 @@ function openpgp_crypto_testRSA(key){ function openpgp_crypto_generateKeyPair(keyType, numBits, passphrase, s2kHash, symmetricEncryptionAlgorithm){ var privKeyPacket; var publicKeyPacket; + var d = new Date(); + d = d.getTime()/1000; + var timePacket = String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); switch(keyType){ case 1: var rsa = new RSA(); var key = rsa.generate(numBits,"10001"); - privKeyPacket = new openpgp_packet_keymaterial().write_private_key(keyType, key, passphrase, s2kHash, symmetricEncryptionAlgorithm); - publicKeyPacket = new openpgp_packet_keymaterial().write_public_key(keyType, key); + privKeyPacket = new openpgp_packet_keymaterial().write_private_key(keyType, key, passphrase, s2kHash, symmetricEncryptionAlgorithm, timePacket); + publicKeyPacket = new openpgp_packet_keymaterial().write_public_key(keyType, key, timePacket); break; default: util.print_error("Unknown keytype "+keyType) diff --git a/src/packet/openpgp.packet.keymaterial.js b/src/packet/openpgp.packet.keymaterial.js index b4045192..5069335e 100644 --- a/src/packet/openpgp.packet.keymaterial.js +++ b/src/packet/openpgp.packet.keymaterial.js @@ -679,14 +679,11 @@ function openpgp_packet_keymaterial() { * @param key [RSA.keyObject] * @return {body: [string]OpenPGP packet body contents, header: [string] OpenPGP packet header, string: [string] header+body} */ - function write_private_key(keyType, key, password, s2kHash, symmetricEncryptionAlgorithm){ + function write_private_key(keyType, key, password, s2kHash, symmetricEncryptionAlgorithm, timePacket){ this.symmetricEncryptionAlgorithm = symmetricEncryptionAlgorithm; var tag = 5; var body = String.fromCharCode(4); - //TODO make the date into a util function - var d = new Date(); - d = d.getTime()/1000; - body += String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); + body += timePacket; switch(keyType){ case 1: body += String.fromCharCode(keyType);//public key algo @@ -759,13 +756,10 @@ function openpgp_packet_keymaterial() { * @param key [RSA.keyObject] * @return {body: [string]OpenPGP packet body contents, header: [string] OpenPGP packet header, string: [string] header+body} */ - function write_public_key(keyType, key){ + function write_public_key(keyType, key, timePacket){ var tag = 6; var body = String.fromCharCode(4); - //TODO make the date into a util function - var d = new Date(); - d = d.getTime()/1000; - body += String.fromCharCode(Math.floor(d/0x1000000%0x100)) + String.fromCharCode(Math.floor(d/0x10000%0x100)) + String.fromCharCode(Math.floor(d/0x100%0x100)) + String.fromCharCode(Math.floor(d%0x100)); + body += timePacket; switch(keyType){ case 1: body += String.fromCharCode(1);//public key algo