diff --git a/doc/JXG.Util.html b/doc/JXG.Util.html index c29632cf..906c6076 100644 --- a/doc/JXG.Util.html +++ b/doc/JXG.Util.html @@ -166,7 +166,7 @@ The code is based on the source code for gunzip.c by Pasi Ojala
Source:
@@ -227,7 +227,7 @@ The code is based on the source code for gunzip.c by Pasi Ojala
Source:
@@ -305,7 +305,7 @@ EXAMPLES:
Source:
@@ -491,7 +491,7 @@ EXAMPLES:
Source:
@@ -554,13 +554,13 @@ EXAMPLES:
diff --git a/doc/aes.html b/doc/aes.html index 90875076..f0e7bad3 100644 --- a/doc/aes.html +++ b/doc/aes.html @@ -111,13 +111,13 @@
diff --git a/doc/aes.js.html b/doc/aes.js.html index 92e89619..c8216446 100644 --- a/doc/aes.js.html +++ b/doc/aes.js.html @@ -520,8 +520,8 @@ function makeClass(length) { this.encrypt = function(block) { return AESencrypt(block, this.key); - } - } + }; + }; c.blockSize = c.prototype.blockSize = 16; c.keySize = c.prototype.keySize = length / 8; @@ -529,7 +529,7 @@ function makeClass(length) { return c; } -module.exports = {} +module.exports = {}; var types = [128, 192, 256]; @@ -546,13 +546,13 @@ for (var i in types) {
diff --git a/doc/all_packets.js.html b/doc/all_packets.js.html index a0e6c3f9..d054963e 100644 --- a/doc/all_packets.js.html +++ b/doc/all_packets.js.html @@ -35,43 +35,50 @@ var enums = require('../enums.js'); module.exports = { /** @see module:packet/compressed */ - compressed: require('./compressed.js'), + Compressed: require('./compressed.js'), /** @see module:packet/sym_encrypted_integrity_protected */ - sym_encrypted_integrity_protected: require('./sym_encrypted_integrity_protected.js'), + SymEncryptedIntegrityProtected: require('./sym_encrypted_integrity_protected.js'), /** @see module:packet/public_key_encrypted_session_key */ - public_key_encrypted_session_key: require('./public_key_encrypted_session_key.js'), + PublicKeyEncryptedSessionKey: require('./public_key_encrypted_session_key.js'), /** @see module:packet/sym_encrypted_session_key */ - sym_encrypted_session_key: require('./sym_encrypted_session_key.js'), + SymEncryptedSessionKey: require('./sym_encrypted_session_key.js'), /** @see module:packet/literal */ - literal: require('./literal.js'), + Literal: require('./literal.js'), /** @see module:packet/public_key */ - public_key: require('./public_key.js'), + PublicKey: require('./public_key.js'), /** @see module:packet/symmetrically_encrypted */ - symmetrically_encrypted: require('./symmetrically_encrypted.js'), + SymmetricallyEncrypted: require('./symmetrically_encrypted.js'), /** @see module:packet/marker */ - marker: require('./marker.js'), + Marker: require('./marker.js'), /** @see module:packet/public_subkey */ - public_subkey: require('./public_subkey.js'), + PublicSubkey: require('./public_subkey.js'), /** @see module:packet/user_attribute */ - user_attribute: require('./user_attribute.js'), + UserAttribute: require('./user_attribute.js'), /** @see module:packet/one_pass_signature */ - one_pass_signature: require('./one_pass_signature.js'), + OnePassSignature: require('./one_pass_signature.js'), /** @see module:packet/secret_key */ - secret_key: require('./secret_key.js'), + SecretKey: require('./secret_key.js'), /** @see module:packet/userid */ - userid: require('./userid.js'), + Userid: require('./userid.js'), /** @see module:packet/secret_subkey */ - secret_subkey: require('./secret_subkey.js'), + SecretSubkey: require('./secret_subkey.js'), /** @see module:packet/signature */ - signature: require('./signature.js'), + Signature: require('./signature.js'), /** @see module:packet/trust */ - trust: require('./trust.js') + Trust: require('./trust.js'), + + newPacketFromTag: function (tag) { + return new this[packetClassFromTag(tag)](); + } +}; + +function packetClassFromTag(tag) { + return tag.substr(0, 1).toUpperCase() + tag.substr(1); } - for (var i in enums.packet) { - var packetClass = module.exports[i]; + var packetClass = module.exports[packetClassFromTag(i)]; - if (packetClass != undefined) + if (packetClass !== undefined) packetClass.prototype.tag = enums.packet[i]; } @@ -84,13 +91,13 @@ for (var i in enums.packet) {
diff --git a/doc/armor.html b/doc/armor.html index 7f68f4be..5b5878f2 100644 --- a/doc/armor.html +++ b/doc/armor.html @@ -1358,13 +1358,13 @@ given base64 encoded checksum
diff --git a/doc/armor.js.html b/doc/armor.js.html index 8b6fc8de..e4437f19 100644 --- a/doc/armor.js.html +++ b/doc/armor.js.html @@ -240,7 +240,7 @@ function splitHeaders(text) { var matchResult = reEmptyLine.exec(text); - if (matchResult != null) { + if (matchResult !== null) { headers = text.slice(0, matchResult.index); body = text.slice(matchResult.index + matchResult[0].length); } @@ -262,7 +262,7 @@ function splitChecksum(text) { var matchResult = reChecksumStart.exec(text); - if (matchResult != null) { + if (matchResult !== null) { body = text.slice(0, matchResult.index); checksum = text.slice(matchResult.index + 1); } @@ -295,14 +295,14 @@ function dearmor(text) { // so we know the index of the data we are interested in. var indexBase = 1; - var result, checksum; + var result, checksum, msg; if (text.search(reSplit) != splittext[0].length) { indexBase = 0; } if (type != 2) { - var msg = splitHeaders(splittext[indexBase]); + msg = splitHeaders(splittext[indexBase]); var msg_sum = splitChecksum(msg.body); result = { @@ -313,7 +313,7 @@ function dearmor(text) { checksum = msg_sum.checksum; } else { // Reverse dash-escaping for msg and remove trailing whitespace at end of line - var msg = splitHeaders(splittext[indexBase].replace(/^- /mg, '').replace(/[\t ]+\n/g, "\n")); + msg = splitHeaders(splittext[indexBase].replace(/^- /mg, '').replace(/[\t ]+\n/g, "\n")); var sig = splitHeaders(splittext[indexBase + 1].replace(/^- /mg, '')); var sig_sum = splitChecksum(sig.body); @@ -327,10 +327,10 @@ function dearmor(text) { } if (!verifyCheckSum(result.data, checksum)) { - throw new Error("Ascii armor integrity check on message failed: '" - + checksum - + "' should be '" - + getCheckSum(result) + "'"); + throw new Error("Ascii armor integrity check on message failed: '" + + checksum + + "' should be '" + + getCheckSum(result) + "'"); } else { return result; } @@ -413,13 +413,13 @@ module.exports = {
diff --git a/doc/base64.html b/doc/base64.html index d2cd492d..5f76ecb4 100644 --- a/doc/base64.html +++ b/doc/base64.html @@ -387,13 +387,13 @@
diff --git a/doc/base64.js.html b/doc/base64.js.html index 0442407f..0d7369c8 100644 --- a/doc/base64.js.html +++ b/doc/base64.js.html @@ -59,7 +59,7 @@ function s2r(t) { for (n = 0; n < tl; n++) { c = t.charCodeAt(n); - if (s == 0) { + if (s === 0) { r += b64s.charAt((c >> 2) & 63); a = (c & 3) << 4; } else if (s == 1) { @@ -68,12 +68,12 @@ function s2r(t) { } else if (s == 2) { r += b64s.charAt(a | ((c >> 6) & 3)); l += 1; - if ((l % 60) == 0) + if ((l % 60) === 0) r += "\n"; r += b64s.charAt(c & 63); } l += 1; - if ((l % 60) == 0) + if ((l % 60) === 0) r += "\n"; s += 1; @@ -83,13 +83,13 @@ function s2r(t) { if (s > 0) { r += b64s.charAt(a); l += 1; - if ((l % 60) == 0) + if ((l % 60) === 0) r += "\n"; r += '='; l += 1; } if (s == 1) { - if ((l % 60) == 0) + if ((l % 60) === 0) r += "\n"; r += '='; } @@ -125,7 +125,7 @@ function r2s(t) { module.exports = { encode: s2r, decode: r2s -} +}; @@ -136,13 +136,13 @@ module.exports = {
diff --git a/doc/blowfish.html b/doc/blowfish.html index b311cbff..49fab594 100644 --- a/doc/blowfish.html +++ b/doc/blowfish.html @@ -105,13 +105,13 @@
diff --git a/doc/blowfish.js.html b/doc/blowfish.js.html index 8fb76d56..f5483eda 100644 --- a/doc/blowfish.js.html +++ b/doc/blowfish.js.html @@ -41,7 +41,7 @@ * The constructor doesn't do much of anything. It's just here * so we can start defining properties and methods and such. */ -function Blowfish() {}; +function Blowfish() {} /* * Declare the block size so that protocols know what size @@ -434,7 +434,7 @@ function BF(key) { this.encrypt = function(block) { return this.bf.encrypt_block(block); - } + }; } @@ -451,13 +451,13 @@ module.exports.blockSize = BF.prototype.blockSize = 16;
diff --git a/doc/cast5.html b/doc/cast5.html index 3089499d..3c1cdff0 100644 --- a/doc/cast5.html +++ b/doc/cast5.html @@ -105,13 +105,13 @@
diff --git a/doc/cast5.js.html b/doc/cast5.js.html index 8651ba82..63f41963 100644 --- a/doc/cast5.js.html +++ b/doc/cast5.js.html @@ -279,19 +279,22 @@ function openpgp_symenc_cast5() { var t = new Array(8); var k = new Array(32); - for (var i = 0; i < 4; i++) { - var j = i * 4; + var i, j; + + for (i = 0; i < 4; i++) { + j = i * 4; t[i] = inn[j] << 24 | inn[j + 1] << 16 | inn[j + 2] << 8 | inn[j + 3]; } var x = [6, 7, 4, 5]; var ki = 0; + var w; for (var half = 0; half < 2; half++) { for (var round = 0; round < 4; round++) { - for (var j = 0; j < 4; j++) { + for (j = 0; j < 4; j++) { var a = scheduleA[round][j]; - var w = t[a[1]]; + w = t[a[1]]; w ^= sBox[4][(t[a[2] >>> 2] >>> (24 - 8 * (a[2] & 3))) & 0xff]; w ^= sBox[5][(t[a[3] >>> 2] >>> (24 - 8 * (a[3] & 3))) & 0xff]; @@ -301,9 +304,9 @@ function openpgp_symenc_cast5() { t[a[0]] = w; } - for (var j = 0; j < 4; j++) { + for (j = 0; j < 4; j++) { var b = scheduleB[round][j]; - var w = sBox[4][(t[b[0] >>> 2] >>> (24 - 8 * (b[0] & 3))) & 0xff]; + w = sBox[4][(t[b[0] >>> 2] >>> (24 - 8 * (b[0] & 3))) & 0xff]; w ^= sBox[5][(t[b[1] >>> 2] >>> (24 - 8 * (b[1] & 3))) & 0xff]; w ^= sBox[6][(t[b[2] >>> 2] >>> (24 - 8 * (b[2] & 3))) & 0xff]; @@ -315,7 +318,7 @@ function openpgp_symenc_cast5() { } } - for (var i = 0; i < 16; i++) { + for (i = 0; i < 16; i++) { this.masking[i] = k[i]; this.rotate[i] = k[16 + i] & 0x1f; } @@ -614,8 +617,7 @@ function openpgp_symenc_cast5() { 0xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1, 0xa466bb1e, 0xf8da0a82, 0x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d, 0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e); -}; - +} var util = require('../../util'); function cast5(key) { @@ -624,7 +626,7 @@ function cast5(key) { this.encrypt = function(block) { return this.cast5.encrypt(block); - } + }; } module.exports = cast5; @@ -640,13 +642,13 @@ module.exports.keySize = cast5.prototype.keySize = 16;
diff --git a/doc/cfb.html b/doc/cfb.html index c860d141..bfb6c6de 100644 --- a/doc/cfb.html +++ b/doc/cfb.html @@ -762,13 +762,13 @@ This will be passed to the cipherfn
diff --git a/doc/cfb.js.html b/doc/cfb.js.html index 189c1415..b015f56a 100644 --- a/doc/cfb.js.html +++ b/doc/cfb.js.html @@ -79,8 +79,9 @@ module.exports = { prefixrandom = prefixrandom + prefixrandom.charAt(block_size - 2) + prefixrandom.charAt(block_size - 1); util.print_debug("prefixrandom:" + util.hexstrdump(prefixrandom)); var ciphertext = ""; + var i; // 1. The feedback register (FR) is set to the IV, which is all zeros. - for (var i = 0; i < block_size; i++) FR[i] = 0; + for (i = 0; i < block_size; i++) FR[i] = 0; // 2. FR is encrypted to produce FRE (FR Encrypted). This is the // encryption of an all-zero value. @@ -88,13 +89,13 @@ module.exports = { // 3. FRE is xored with the first BS octets of random data prefixed to // the plaintext to produce C[1] through C[BS], the first BS octets // of ciphertext. - for (var i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ prefixrandom.charCodeAt(i)); + for (i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ prefixrandom.charCodeAt(i)); // 4. FR is loaded with C[1] through C[BS]. - for (var i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); + for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); // 5. FR is encrypted to produce FRE, the encryption of the first BS - // octets of ciphertext. + // octets of ciphertext. FRE = cipherfn.encrypt(FR); // 6. The left two octets of FRE get xored with the next two octets of @@ -105,22 +106,22 @@ module.exports = { if (resync) { // 7. (The resync step) FR is loaded with C3-C10. - for (var i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i + 2); + for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i + 2); } else { - for (var i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); + for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(i); } // 8. FR is encrypted to produce FRE. FRE = cipherfn.encrypt(FR, key); if (resync) { // 9. FRE is xored with the first 8 octets of the given plaintext, now - // that we have finished encrypting the 10 octets of prefixed data. - // This produces C11-C18, the next 8 octets of ciphertext. - for (var i = 0; i < block_size; i++) + // that we have finished encrypting the 10 octets of prefixed data. + // This produces C11-C18, the next 8 octets of ciphertext. + for (i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(i)); for (n = block_size + 2; n < plaintext.length; n += block_size) { // 10. FR is loaded with C11-C18 - for (var i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(n + i); + for (i = 0; i < block_size; i++) FR[i] = ciphertext.charCodeAt(n + i); // 11. FR is encrypted to produce FRE. FRE = cipherfn.encrypt(FR); @@ -128,20 +129,20 @@ module.exports = { // 12. FRE is xored with the next 8 octets of plaintext, to produce the // next 8 octets of ciphertext. These are loaded into FR and the // process is repeated until the plaintext is used up. - for (var i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt((n - 2) + + for (i = 0; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt((n - 2) + i)); } } else { plaintext = " " + plaintext; // 9. FRE is xored with the first 8 octets of the given plaintext, now - // that we have finished encrypting the 10 octets of prefixed data. - // This produces C11-C18, the next 8 octets of ciphertext. - for (var i = 2; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(i)); + // that we have finished encrypting the 10 octets of prefixed data. + // This produces C11-C18, the next 8 octets of ciphertext. + for (i = 2; i < block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(i)); var tempCiphertext = ciphertext.substring(0, 2 * block_size).split(''); var tempCiphertextString = ciphertext.substring(block_size); for (n = block_size; n < plaintext.length; n += block_size) { // 10. FR is loaded with C11-C18 - for (var i = 0; i < block_size; i++) FR[i] = tempCiphertextString.charCodeAt(i); + for (i = 0; i < block_size; i++) FR[i] = tempCiphertextString.charCodeAt(i); tempCiphertextString = ''; // 11. FR is encrypted to produce FRE. @@ -150,13 +151,12 @@ module.exports = { // 12. FRE is xored with the next 8 octets of plaintext, to produce the // next 8 octets of ciphertext. These are loaded into FR and the // process is repeated until the plaintext is used up. - for (var i = 0; i < block_size; i++) { + for (i = 0; i < block_size; i++) { tempCiphertext.push(String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(n + i))); tempCiphertextString += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(n + i)); } } ciphertext = tempCiphertext.join(''); - } ciphertext = ciphertext.substring(0, plaintext.length + 2 + block_size); @@ -266,7 +266,7 @@ module.exports = { } } - var n = resync ? 0 : 2; + n = resync ? 0 : 2; text = text.join(''); @@ -308,14 +308,15 @@ module.exports = { var pos = 0; var plaintext = []; var offset = 0; - if (iv == null) - for (var i = 0; i < block_size; i++) blockp += String.fromCharCode(0); + var i; + if (iv === null) + for (i = 0; i < block_size; i++) blockp += String.fromCharCode(0); else blockp = iv.substring(0, block_size); while (ciphertext.length > (block_size * pos)) { var decblock = cipherfn.encrypt(util.str2bin(blockp)); blockp = ciphertext.substring((pos * (block_size)) + offset, (pos * (block_size)) + (block_size) + offset); - for (var i = 0; i < blockp.length; i++) { + for (i = 0; i < blockp.length; i++) { plaintext.push(String.fromCharCode(blockp.charCodeAt(i) ^ decblock[i])); } pos++; @@ -323,7 +324,7 @@ module.exports = { return plaintext.join(''); } -} +}; @@ -334,13 +335,13 @@ module.exports = {
diff --git a/doc/cipher.html b/doc/cipher.html index 2bfea5bf..7a127c6f 100644 --- a/doc/cipher.html +++ b/doc/cipher.html @@ -406,13 +406,13 @@
diff --git a/doc/cleartext.js.html b/doc/cleartext.js.html index 8af1a8ef..c1d7289b 100644 --- a/doc/cleartext.js.html +++ b/doc/cleartext.js.html @@ -70,7 +70,7 @@ function CleartextMessage(text, packetlist) { } // normalize EOL to canonical form <CR><LF> this.text = text.replace(/\r/g, '').replace(/[\t ]+\n/g, "\n").replace(/\n/g,"\r\n"); - this.packets = packetlist || new packet.list(); + this.packets = packetlist || new packet.List(); } /** @@ -91,11 +91,11 @@ CleartextMessage.prototype.getSigningKeyIds = function() { * @param {Array<module:key~Key>} privateKeys private keys with decrypted secret key data for signing */ CleartextMessage.prototype.sign = function(privateKeys) { - var packetlist = new packet.list(); - var literalDataPacket = new packet.literal(); + var packetlist = new packet.List(); + var literalDataPacket = new packet.Literal(); literalDataPacket.setText(this.text); for (var i = 0; i < privateKeys.length; i++) { - var signaturePacket = new packet.signature(); + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = enums.signature.text; signaturePacket.hashAlgorithm = config.prefer_hash_algorithm; var signingKeyPacket = privateKeys[i].getSigningKeyPacket(); @@ -115,7 +115,7 @@ CleartextMessage.prototype.sign = function(privateKeys) { CleartextMessage.prototype.verify = function(publicKeys) { var result = []; var signatureList = this.packets.filterByTag(enums.packet.signature); - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); // we assume that cleartext signature is generated based on UTF8 cleartext literalDataPacket.setText(this.text); publicKeys.forEach(function(pubKey) { @@ -151,7 +151,7 @@ CleartextMessage.prototype.armor = function() { hash: enums.read(enums.hash, config.prefer_hash_algorithm).toUpperCase(), text: this.text, data: this.packets.write() - } + }; return armor.encode(enums.armor.signed, body); }; @@ -167,7 +167,7 @@ function readArmored(armoredText) { if (input.type !== enums.armor.signed) { throw new Error('No cleartext signed message.'); } - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input.data); var newMessage = new CleartextMessage(input.text, packetlist); return newMessage; @@ -185,13 +185,13 @@ exports.readArmored = readArmored;
diff --git a/doc/compressed-Compressed.html b/doc/compressed-Compressed.html new file mode 100644 index 00000000..3034d7d3 --- /dev/null +++ b/doc/compressed-Compressed.html @@ -0,0 +1,695 @@ + + + + + JSDoc: Class: Compressed + + + + + + + + + + +
+ +

Class: Compressed

+ + + + + +
+ +
+

+ packet/compressed~ + + Compressed +

+ +
+ +
+
+ + + + +
+

new Compressed()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

algorithm :compression

+ + +
+
+ +
+ Compression algorithm +
+ + + +
Type:
+
    +
  • + +compression + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

compressed :String

+ + +
+
+ +
+ Compressed packet data +
+ + + +
Type:
+
    +
  • + +String + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

packets :module:packet/packetlist

+ + +
+
+ +
+ List of packets +
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

compress()

+ + +
+
+ + +
+ Compress the packet data (member decompressedData) +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

decompress()

+ + +
+
+ + +
+ Decompression method for decompressing the compressed data +read by read_packet +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

read(bytes)

+ + +
+
+ + +
+ Parsing function for the packet. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
bytes + + +String + + + + Payload of a tag 8 packet
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ Return the compressed packet. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ binary compressed packet +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/compressed.html b/doc/compressed.html index 9f58e225..c3001e3f 100644 --- a/doc/compressed.html +++ b/doc/compressed.html @@ -35,6 +35,8 @@
+ +
Implementation of the Compressed Data Packet (Tag 8)

@@ -43,74 +45,6 @@ this packet is found as the contents of an encrypted packet, or following a Signature or One-Pass Signature packet, and contains a literal data packet.
-
-

new (require("packet/compressed"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -167,559 +101,19 @@ a Signature or One-Pass Signature packet, and contains a literal data packet.Members +

Classes

- -
-

algorithm :compression

- - -
-
- -
- Compression algorithm -
- - - -
Type:
-
    -
  • - -compression - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

compressed :String

- - -
-
- -
- Compressed packet data -
- - - -
Type:
-
    -
  • - -String - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

packets :module:packet/packetlist

- - -
-
- -
- List of packets -
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
Compressed
+
-

Methods

- -
- -
-

compress()

-
-
-
- Compress the packet data (member decompressedData) -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

decompress()

- - -
-
- - -
- Decompression method for decompressing the compressed data -read by read_packet -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

read(bytes)

- - -
-
- - -
- Parsing function for the packet. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - Payload of a tag 8 packet
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- Return the compressed packet. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- binary compressed packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -734,13 +128,13 @@ read by read_packet

diff --git a/doc/compressed.js.html b/doc/compressed.js.html index b60c43ca..3937d5b7 100644 --- a/doc/compressed.js.html +++ b/doc/compressed.js.html @@ -54,6 +54,8 @@ * @module packet/compressed */ +module.exports = Compressed; + var enums = require('../enums.js'), JXG = require('../compression/jxg.js'), base64 = require('../encoding/base64.js'); @@ -61,7 +63,7 @@ var enums = require('../enums.js'), /** * @constructor */ -module.exports = function compressed() { +function Compressed() { /** * List of packets * @type {module:packet/packetlist} @@ -78,125 +80,118 @@ module.exports = function compressed() { * @type {String} */ this.compressed = null; +} + +/** + * Parsing function for the packet. + * @param {String} bytes Payload of a tag 8 packet + */ +Compressed.prototype.read = function (bytes) { + // One octet that gives the algorithm used to compress the packet. + this.algorithm = enums.read(enums.compression, bytes.charCodeAt(0)); + + // Compressed data, which makes up the remainder of the packet. + this.compressed = bytes.substr(1); + + this.decompress(); +}; - /** - * Parsing function for the packet. - * @param {String} bytes Payload of a tag 8 packet - */ - this.read = function(bytes) { - // One octet that gives the algorithm used to compress the packet. - this.algorithm = enums.read(enums.compression, bytes.charCodeAt(0)); - // Compressed data, which makes up the remainder of the packet. - this.compressed = bytes.substr(1); +/** + * Return the compressed packet. + * @return {String} binary compressed packet + */ +Compressed.prototype.write = function () { + if (this.compressed === null) + this.compress(); - this.decompress(); + return String.fromCharCode(enums.write(enums.compression, this.algorithm)) + this.compressed; +}; + + +/** + * Decompression method for decompressing the compressed data + * read by read_packet + */ +Compressed.prototype.decompress = function () { + var decompressed, compdata, radix; + + switch (this.algorithm) { + case 'uncompressed': + decompressed = this.compressed; + break; + + case 'zip': + compData = this.compressed; + + radix = base64.encode(compData).replace(/\n/g, ""); + // no header in this case, directly call deflate + var jxg_obj = new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(radix)); + + decompressed = unescape(jxg_obj.deflate()[0][0]); + break; + + case 'zlib': + //RFC 1950. Bits 0-3 Compression Method + var compressionMethod = this.compressed.charCodeAt(0) % 0x10; + + //Bits 4-7 RFC 1950 are LZ77 Window. Generally this value is 7 == 32k window size. + // 2nd Byte in RFC 1950 is for "FLAGs" Allows for a Dictionary + // (how is this defined). Basic checksum, and compression level. + + if (compressionMethod == 8) { //CM 8 is for DEFLATE, RFC 1951 + // remove 4 bytes ADLER32 checksum from the end + compData = this.compressed.substring(0, this.compressed.length - 4); + radix = base64.encode(compData).replace(/\n/g, ""); + //TODO check ADLER32 checksum + decompressed = JXG.decompress(radix); + break; + + } else { + throw new Error("Compression algorithm ZLIB only supports " + + "DEFLATE compression method."); + } + break; + + case 'bzip2': + // TODO: need to implement this + throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.'); + + default: + throw new Error("Compression algorithm unknown :" + this.alogrithm); } + this.packets.read(decompressed); +}; +/** + * Compress the packet data (member decompressedData) + */ +Compressed.prototype.compress = function () { + switch (this.algorithm) { - /** - * Return the compressed packet. - * @return {String} binary compressed packet - */ - this.write = function() { - if (this.compressed == null) - this.compress(); + case 'uncompressed': + // - Uncompressed + this.compressed = this.packets.write(); + break; - return String.fromCharCode(enums.write(enums.compression, this.algorithm)) + this.compressed; - } + case 'zip': + // - ZIP [RFC1951] + throw new Error("Compression algorithm ZIP [RFC1951] is not implemented."); + case 'zlib': + // - ZLIB [RFC1950] + // TODO: need to implement this + throw new Error("Compression algorithm ZLIB [RFC1950] is not implemented."); - /** - * Decompression method for decompressing the compressed data - * read by read_packet - */ - this.decompress = function() { - var decompressed; + case 'bzip2': + // - BZip2 [BZ2] + // TODO: need to implement this + throw new Error("Compression algorithm BZip2 [BZ2] is not implemented."); - switch (this.algorithm) { - case 'uncompressed': - decompressed = this.compressed; - break; - - case 'zip': - var compData = this.compressed; - - var radix = base64.encode(compData).replace(/\n/g, ""); - // no header in this case, directly call deflate - var jxg_obj = new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(radix)); - - decompressed = unescape(jxg_obj.deflate()[0][0]); - break; - - case 'zlib': - //RFC 1950. Bits 0-3 Compression Method - var compressionMethod = this.compressed.charCodeAt(0) % 0x10; - - //Bits 4-7 RFC 1950 are LZ77 Window. Generally this value is 7 == 32k window size. - // 2nd Byte in RFC 1950 is for "FLAGs" Allows for a Dictionary - // (how is this defined). Basic checksum, and compression level. - - if (compressionMethod == 8) { //CM 8 is for DEFLATE, RFC 1951 - // remove 4 bytes ADLER32 checksum from the end - var compData = this.compressed.substring(0, this.compressed.length - 4); - var radix = base64.encode(compData).replace(/\n/g, ""); - //TODO check ADLER32 checksum - decompressed = JXG.decompress(radix); - break; - - } else { - throw new Error("Compression algorithm ZLIB only supports " + - "DEFLATE compression method."); - } - break; - - case 'bzip2': - // TODO: need to implement this - throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.'); - break; - - default: - throw new Error("Compression algorithm unknown :" + this.alogrithm); - break; - } - - this.packets.read(decompressed); - } - - /** - * Compress the packet data (member decompressedData) - */ - this.compress = function() { - switch (this.algorithm) { - - case 'uncompressed': - // - Uncompressed - this.compressed = this.packets.write(); - break; - - case 'zip': - // - ZIP [RFC1951] - throw new Error("Compression algorithm ZIP [RFC1951] is not implemented."); - break; - - case 'zlib': - // - ZLIB [RFC1950] - // TODO: need to implement this - throw new Error("Compression algorithm ZLIB [RFC1950] is not implemented."); - break; - - case 'bzip2': - // - BZip2 [BZ2] - // TODO: need to implement this - throw new Error("Compression algorithm BZip2 [BZ2] is not implemented."); - break; - - default: - throw new Error("Compression algorithm unknown :" + this.type); - break; - } + default: + throw new Error("Compression algorithm unknown :" + this.type); } }; @@ -209,13 +204,13 @@ module.exports = function compressed() {
diff --git a/doc/config.html b/doc/config.html index 7469c56d..fc43ecf1 100644 --- a/doc/config.html +++ b/doc/config.html @@ -325,13 +325,13 @@
diff --git a/doc/config.js.html b/doc/config.js.html index b7499701..f23d5772 100644 --- a/doc/config.js.html +++ b/doc/config.js.html @@ -82,13 +82,13 @@ module.exports = {
diff --git a/doc/crypto.html b/doc/crypto.html index 16d37a92..0dd34c94 100644 --- a/doc/crypto.html +++ b/doc/crypto.html @@ -197,7 +197,7 @@
Source:
@@ -336,7 +336,7 @@
Source:
@@ -476,7 +476,7 @@ size of the cipher
Source:
@@ -687,7 +687,7 @@ of the private key used
Source:
@@ -937,13 +937,13 @@ if elgamal encryption an array of two module:type/mpi is returned; otherwise nul
diff --git a/doc/crypto.js.html b/doc/crypto.js.html index 14d2a0d0..8f39f1f5 100644 --- a/doc/crypto.js.html +++ b/doc/crypto.js.html @@ -69,13 +69,14 @@ module.exports = { */ publicKeyEncrypt: function(algo, publicMPIs, data) { var result = (function() { + var m; switch (algo) { case 'rsa_encrypt': case 'rsa_encrypt_sign': var rsa = new publicKey.rsa(); var n = publicMPIs[0].toBigInteger(); var e = publicMPIs[1].toBigInteger(); - var m = data.toBigInteger(); + m = data.toBigInteger(); return [rsa.encrypt(m, e, n)]; case 'elgamal': @@ -83,7 +84,7 @@ module.exports = { var p = publicMPIs[0].toBigInteger(); var g = publicMPIs[1].toBigInteger(); var y = publicMPIs[2].toBigInteger(); - var m = data.toBigInteger(); + m = data.toBigInteger(); return elgamal.encrypt(m, g, p, y); default: @@ -111,6 +112,8 @@ module.exports = { */ publicKeyDecrypt: function(algo, keyIntegers, dataIntegers) { + var p; + var bn = (function() { switch (algo) { case 'rsa_encrypt_sign': @@ -118,7 +121,7 @@ module.exports = { var rsa = new publicKey.rsa(); // 0 and 1 are the public key. var d = keyIntegers[2].toBigInteger(); - var p = keyIntegers[3].toBigInteger(); + p = keyIntegers[3].toBigInteger(); var q = keyIntegers[4].toBigInteger(); var u = keyIntegers[5].toBigInteger(); var m = dataIntegers[0].toBigInteger(); @@ -128,7 +131,7 @@ module.exports = { var x = keyIntegers[3].toBigInteger(); var c1 = dataIntegers[0].toBigInteger(); var c2 = dataIntegers[1].toBigInteger(); - var p = keyIntegers[0].toBigInteger(); + p = keyIntegers[0].toBigInteger(); return elgamal.decrypt(c1, c2, p, x); default: return null; @@ -258,13 +261,13 @@ module.exports = {
diff --git a/doc/des.html b/doc/des.html index 0d832eaf..a8dc29a7 100644 --- a/doc/des.html +++ b/doc/des.html @@ -209,13 +209,13 @@
diff --git a/doc/des.js.html b/doc/des.js.html index 3ace6958..92b0d2a0 100644 --- a/doc/des.js.html +++ b/doc/des.js.html @@ -108,7 +108,7 @@ function des(keys, message, encrypt, mode, iv, padding) { //create the 16 or 48 subkeys we will need var m = 0, i, j, temp, temp2, right1, right2, left, right, looping; - var cbcleft, cbcleft2, cbcright, cbcright2 + var cbcleft, cbcleft2, cbcright, cbcright2; var endloop, loopinc; var len = message.length; var chunk = 0; @@ -405,7 +405,7 @@ function Des(key) { des(des_createKeys(this.key[0]), util.bin2str(block), true, 0, null, null), false, 0, null, null), true, 0, null, null)); - } + }; } Des.keySize = Des.prototype.keySize = 24; @@ -420,12 +420,12 @@ function OriginalDes(key) { this.encrypt = function(block, padding) { var keys = des_createKeys(this.key); return util.str2bin(des(keys, util.bin2str(block), true, 0, null, padding)); - } + }; this.decrypt = function(block, padding) { var keys = des_createKeys(this.key); return util.str2bin(des(keys, util.bin2str(block), false, 0, null, padding)); - } + }; } module.exports = { @@ -433,7 +433,7 @@ module.exports = { des: Des, /** @static */ originalDes: OriginalDes -} +};
@@ -444,13 +444,13 @@ module.exports = {
diff --git a/doc/dsa.html b/doc/dsa.html index 80815f6e..864d5d86 100644 --- a/doc/dsa.html +++ b/doc/dsa.html @@ -117,13 +117,13 @@
diff --git a/doc/dsa.js.html b/doc/dsa.js.html index b0f0fab1..f86d494e 100644 --- a/doc/dsa.js.html +++ b/doc/dsa.js.html @@ -72,7 +72,7 @@ function DSA() { var k = random.getRandomBigIntegerInRange(BigInteger.ONE.add(BigInteger.ONE), q.subtract(BigInteger.ONE)); var s1 = (g.modPow(k, p)).mod(q); var s2 = (k.modInverse(q).multiply(hash.add(x.multiply(s1)))).mod(q); - var result = new Array(); + var result = []; result[0] = s1.toMPI(); result[1] = s2.toMPI(); return result; @@ -135,61 +135,61 @@ function DSA() { * unused code. This can be used as a start to write a key generator * function. - function generateKey(bitcount) { - var qi = new BigInteger(bitcount, primeCenterie); - var pi = generateP(q, 512); - var gi = generateG(p, q, bitcount); - var xi; - do { - xi = new BigInteger(q.bitCount(), rand); - } while (x.compareTo(BigInteger.ZERO) != 1 && x.compareTo(q) != -1); - var yi = g.modPow(x, p); - return {x: xi, q: qi, p: pi, g: gi, y: yi}; - } + function generateKey(bitcount) { + var qi = new BigInteger(bitcount, primeCenterie); + var pi = generateP(q, 512); + var gi = generateG(p, q, bitcount); + var xi; + do { + xi = new BigInteger(q.bitCount(), rand); + } while (x.compareTo(BigInteger.ZERO) != 1 && x.compareTo(q) != -1); + var yi = g.modPow(x, p); + return {x: xi, q: qi, p: pi, g: gi, y: yi}; + } - function generateP(q, bitlength, randomfn) { - if (bitlength % 64 != 0) { - return false; - } - var pTemp; - var pTemp2; - do { - pTemp = randomfn(bitcount, true); - pTemp2 = pTemp.subtract(BigInteger.ONE); - pTemp = pTemp.subtract(pTemp2.remainder(q)); - } while (!pTemp.isProbablePrime(primeCenterie) || pTemp.bitLength() != l); - return pTemp; - } + function generateP(q, bitlength, randomfn) { + if (bitlength % 64 != 0) { + return false; + } + var pTemp; + var pTemp2; + do { + pTemp = randomfn(bitcount, true); + pTemp2 = pTemp.subtract(BigInteger.ONE); + pTemp = pTemp.subtract(pTemp2.remainder(q)); + } while (!pTemp.isProbablePrime(primeCenterie) || pTemp.bitLength() != l); + return pTemp; + } - function generateG(p, q, bitlength, randomfn) { - var aux = p.subtract(BigInteger.ONE); - var pow = aux.divide(q); - var gTemp; - do { - gTemp = randomfn(bitlength); - } while (gTemp.compareTo(aux) != -1 && gTemp.compareTo(BigInteger.ONE) != 1); - return gTemp.modPow(pow, p); - } + function generateG(p, q, bitlength, randomfn) { + var aux = p.subtract(BigInteger.ONE); + var pow = aux.divide(q); + var gTemp; + do { + gTemp = randomfn(bitlength); + } while (gTemp.compareTo(aux) != -1 && gTemp.compareTo(BigInteger.ONE) != 1); + return gTemp.modPow(pow, p); + } - function generateK(q, bitlength, randomfn) { - var tempK; - do { - tempK = randomfn(bitlength, false); - } while (tempK.compareTo(q) != -1 && tempK.compareTo(BigInteger.ZERO) != 1); - return tempK; - } + function generateK(q, bitlength, randomfn) { + var tempK; + do { + tempK = randomfn(bitlength, false); + } while (tempK.compareTo(q) != -1 && tempK.compareTo(BigInteger.ZERO) != 1); + return tempK; + } - function generateR(q,p) { - k = generateK(q); - var r = g.modPow(k, p).mod(q); - return r; - } + function generateR(q,p) { + k = generateK(q); + var r = g.modPow(k, p).mod(q); + return r; + } - function generateS(hashfn,k,r,m,q,x) { - var hash = hashfn(m); - s = (k.modInverse(q).multiply(hash.add(x.multiply(r)))).mod(q); - return s; - } */ + function generateS(hashfn,k,r,m,q,x) { + var hash = hashfn(m); + s = (k.modInverse(q).multiply(hash.add(x.multiply(r)))).mod(q); + return s; + } */ this.sign = sign; this.verify = verify; // this.generate = generateKey; @@ -206,13 +206,13 @@ module.exports = DSA;
diff --git a/doc/elgamal.html b/doc/elgamal.html index 4a80def7..205c25a3 100644 --- a/doc/elgamal.html +++ b/doc/elgamal.html @@ -115,13 +115,13 @@
diff --git a/doc/elgamal.js.html b/doc/elgamal.js.html index bad86554..17564971 100644 --- a/doc/elgamal.js.html +++ b/doc/elgamal.js.html @@ -95,13 +95,13 @@ module.exports = Elgamal;
diff --git a/doc/enums.js.html b/doc/enums.js.html index e6bd811b..85bcaea4 100644 --- a/doc/enums.js.html +++ b/doc/enums.js.html @@ -103,23 +103,23 @@ module.exports = { * @readonly */ packet: { - public_key_encrypted_session_key: 1, + publicKeyEncryptedSessionKey: 1, signature: 2, - sym_encrypted_session_key: 3, - one_pass_signature: 4, - secret_key: 5, - public_key: 6, - secret_subkey: 7, + symEncryptedSessionKey: 3, + onePassSignature: 4, + secretKey: 5, + publicKey: 6, + secretSubkey: 7, compressed: 8, - symmetrically_encrypted: 9, + symmetricallyEncrypted: 9, marker: 10, literal: 11, trust: 12, userid: 13, - public_subkey: 14, - user_attribute: 17, - sym_encrypted_integrity_protected: 18, - modification_detection_code: 19 + publicSubkey: 14, + userAttribute: 17, + symEncryptedIntegrityProtected: 18, + modificationDetectionCode: 19 }, /** Data types in the literal packet @@ -341,7 +341,7 @@ module.exports = { throw new Error('Invalid enum value.'); } -} +}; @@ -352,13 +352,13 @@ module.exports = {
diff --git a/doc/hash.html b/doc/hash.html index da528cab..43db4644 100644 --- a/doc/hash.html +++ b/doc/hash.html @@ -819,13 +819,13 @@
diff --git a/doc/index.html b/doc/index.html index 35a9596f..d4bad455 100644 --- a/doc/index.html +++ b/doc/index.html @@ -41,15 +41,19 @@
-

[Build Status

- -

What is OpenPGP.js?

+

OpenPGP.js [![Build Status](https://secure.travis-ci.org/openpgpjs/openpgpjs.png?branch=master,v0.1.x)](http://travis-ci.org/openpgpjs/openpgpjs)

OpenPGP.js is a Javascript implementation of the OpenPGP protocol. This is defined in RFC 4880.

How do I use it?

-

As a developer, the best place to start is in the resources/ directory. Within this you will find a basic example implementation and the "binary" files for this library. It is likely that you will want to use resources/openpgp.min.js on your site, this is a minified version of our library.

+

To build the library, checkout the HEAD of the master branch or download a stable (old) tagged version from releases.

+ +

Then build the library:

+ +
npm install && grunt
+ +

Then take the use the minified file from resources/openpgp.min.js and use it in your project.

I need some help

@@ -63,7 +67,8 @@

How do I get involved?

-

You want to help, great! Go ahead and fork our repo, make your changes and make a pull request. Please be sure that you run make minify from the root directory to concatenate and minify the library into the resources/ directory.

+

You want to help, great! Go ahead and fork our repo, make your changes +and make a pull request.

It is extra awesome if you write tests for the code you change. Our test coverage is relatively weak, so if you can add cases that is great.

@@ -175,13 +180,13 @@ for extending and developing on top of the base library.
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/index.js.html b/doc/index.js.html index 902d6691..707293a9 100644 --- a/doc/index.js.html +++ b/doc/index.js.html @@ -74,13 +74,13 @@ module.exports.crypto = require('./crypto');
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/index.js_.html b/doc/index.js_.html index 55766af2..46f490ff 100644 --- a/doc/index.js_.html +++ b/doc/index.js_.html @@ -44,8 +44,7 @@ module.exports = { random: require('./random.js'), /** @see module:crypto/pkcs1 */ pkcs1: require('./pkcs1.js') - -} +}; var crypto = require('./crypto.js'); @@ -61,13 +60,13 @@ for (var i in crypto)
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/index.js__.html b/doc/index.js__.html index c302f9d9..0eca47b5 100644 --- a/doc/index.js__.html +++ b/doc/index.js__.html @@ -37,16 +37,16 @@ var desModule = require('./des.js'); module.exports = { /** @see module:crypto/cipher/des.des */ - des: desModule['des'], + des: desModule.des, /** @see module:crypto/cipher/des.originalDes */ - originalDes: desModule['originalDes'], + originalDes: desModule.originalDes, /** @see module:crypto/cipher/cast5 */ cast5: require('./cast5.js'), /** @see module:crypto/cipher/twofish */ twofish: require('./twofish.js'), /** @see module:crypto/cipher/blowfish */ blowfish: require('./blowfish.js') -} +}; var aes = require('./aes.js'); @@ -63,13 +63,13 @@ for (var i in aes) {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/index.js___.html b/doc/index.js___.html index f622a880..628ec272 100644 --- a/doc/index.js___.html +++ b/doc/index.js___.html @@ -101,7 +101,7 @@ module.exports = { return 32; case 9: // - SHA384 [FIPS180] - return 48 + return 48; case 10: // - SHA512 [FIPS180] return 64; @@ -112,7 +112,7 @@ module.exports = { throw new Error('Invalid hash algorithm.'); } } -} +};
@@ -123,13 +123,13 @@ module.exports = {
diff --git a/doc/index.js____.html b/doc/index.js____.html index 05e4110b..8127b5a5 100644 --- a/doc/index.js____.html +++ b/doc/index.js____.html @@ -38,7 +38,7 @@ module.exports = { elgamal: require('./elgamal.js'), /** @see module:crypto/public_key/dsa */ dsa: require('./dsa.js') -} +}; @@ -49,13 +49,13 @@ module.exports = {
diff --git a/doc/jsbn.html b/doc/jsbn.html index b38910fa..ddd108a3 100644 --- a/doc/jsbn.html +++ b/doc/jsbn.html @@ -111,13 +111,13 @@
diff --git a/doc/jsbn.js.html b/doc/jsbn.js.html index 01fd1900..ac948d90 100644 --- a/doc/jsbn.js.html +++ b/doc/jsbn.js.html @@ -1745,13 +1745,13 @@ BigInteger.prototype.square = bnSquare;
diff --git a/doc/jxg.js.html b/doc/jxg.js.html index 54b8047b..506b0840 100644 --- a/doc/jxg.js.html +++ b/doc/jxg.js.html @@ -29,7 +29,7 @@ exists: (function(undefined) { return function(v) { return !(v === undefined || v === null); - } + }; })() }; JXG.decompress = function(str) { @@ -169,24 +169,24 @@ JXG.Util.Unzip = function(barray) { return bA[bytepos++]; } else return -1; - }; + } function byteAlign() { bb = 1; - }; + } function readBit() { var carry; bits++; carry = (bb & 1); bb >>= 1; - if (bb == 0) { + if (bb === 0) { bb = readByte(); carry = (bb & 1); bb = (bb >> 1) | 0x80; } return carry; - }; + } function readBits(a) { var res = 0, @@ -199,12 +199,12 @@ JXG.Util.Unzip = function(barray) { res = bitReverse[res] >> (8 - a); } return res; - }; + } function flushBuffer() { //document.write('FLUSHBUFFER:'+buf32k); bIdx = 0; - }; + } function addBuffer(a) { SIZE++; @@ -216,14 +216,14 @@ JXG.Util.Unzip = function(barray) { //document.write('ADDBUFFER:'+buf32k); bIdx = 0; } - }; + } function HufNode() { this.b0 = 0; this.b1 = 0; this.jump = null; this.jumppos = -1; - }; + } var LITERALS = 288; @@ -250,7 +250,7 @@ JXG.Util.Unzip = function(barray) { return fpos[len]++; fpos[len]++; } - }; + } function Rec() { var curplace = Places[treepos]; @@ -296,7 +296,7 @@ JXG.Util.Unzip = function(barray) { } len--; return 0; - }; + } function CreateTree(currentTree, numval, lengths, show) { var i; @@ -341,7 +341,7 @@ JXG.Util.Unzip = function(barray) { } }*/ return 0; - }; + } function DecodeValue(currentTree) { var len, i, @@ -380,10 +380,10 @@ JXG.Util.Unzip = function(barray) { X = currentTree[xtreepos]; } } - }; + } function DeflateLoop() { - var last, c, type, i, len; + var last, c, type, i, j, len, dist; do { /*if((last = readBit())){ @@ -416,7 +416,7 @@ JXG.Util.Unzip = function(barray) { break; } - if (type == 0) { + if (type === 0) { var blockLen, cSum; // Stored @@ -435,8 +435,6 @@ JXG.Util.Unzip = function(barray) { addBuffer(c); } } else if (type == 1) { - var j; - /* Fixed Huffman tables -- fixed decode routine */ while (1) { /* @@ -481,8 +479,6 @@ JXG.Util.Unzip = function(barray) { /* EOF */ break; } else { - var len, dist; - j -= 256 + 1; /* bytes + EOF */ len = readBits(cplext[j]) + cplens[j]; @@ -497,13 +493,13 @@ JXG.Util.Unzip = function(barray) { /*fprintf(errfp, "@%d (l%02x,d%04x)\n", SIZE, len, dist);*/ for (j = 0; j < len; j++) { - var c = buf32k[(bIdx - dist) & 0x7fff]; + c = buf32k[(bIdx - dist) & 0x7fff]; addBuffer(c); } } } // while } else if (type == 2) { - var j, n, literalCodes, distCodes, lenCodes; + var n, literalCodes, distCodes, lenCodes; var ll = new Array(288 + 32); // "static" just to preserve stack // Dynamic Huffman tables @@ -538,8 +534,8 @@ JXG.Util.Unzip = function(barray) { document.write("<br>" + distanceTree[a].b0 + " " + distanceTree[a].b1 + " " + distanceTree[a].jump + " " + distanceTree[a].jumppos); /*if (distanceTree[a].jumppos!=-1) - document.write(" "+distanceTree[a].jump.b0+" "+distanceTree[a].jump.b1); - */ + document.write(" "+distanceTree[a].jump.b0+" "+distanceTree[a].jump.b1); + */ } } //document.write('<BR>tree created'); @@ -600,7 +596,7 @@ JXG.Util.Unzip = function(barray) { len = literalTree.length; for (i = 0; i < len; i++) distanceTree[i] = new HufNode(); - var ll2 = new Array(); + var ll2 = []; for (i = literalCodes; i < ll.length; i++) { ll2[i - literalCodes] = ll[i]; } @@ -613,9 +609,8 @@ JXG.Util.Unzip = function(barray) { outer: while (1) { j = DecodeValue(literalTree); if (j >= 256) { // In C64: if carry set - var len, dist; j -= 256; - if (j == 0) { + if (j === 0) { // EOF break; } @@ -634,7 +629,7 @@ JXG.Util.Unzip = function(barray) { if (bIdx - dist < 0) { break outer; } - var c = buf32k[(bIdx - dist) & 0x7fff]; + c = buf32k[(bIdx - dist) & 0x7fff]; addBuffer(c); } } else { @@ -647,7 +642,7 @@ JXG.Util.Unzip = function(barray) { byteAlign(); return 0; - }; + } JXG.Util.Unzip.prototype.unzipFile = function(name) { var i; @@ -658,7 +653,6 @@ JXG.Util.Unzip = function(barray) { return unzipped[i][0]; } } - }; JXG.Util.Unzip.prototype.deflate = function() { @@ -673,7 +667,7 @@ JXG.Util.Unzip = function(barray) { unzipped[files][1] = "DEFLATE"; files++; return unzipped; - } + }; JXG.Util.Unzip.prototype.unzip = function() { //convertToByteArray(input); @@ -798,8 +792,9 @@ JXG.Util.Unzip = function(barray) { alert("filelen " + filelen); i = 0; nameBuf = []; + var c; while (filelen--) { - var c = readByte(); + c = readByte(); if (c == "/" | c == ":") { i = 0; } else if (i < NAMEMAX - 1) @@ -821,7 +816,7 @@ JXG.Util.Unzip = function(barray) { CRC = 0xffffffff; SIZE = 0; - if (size == 0 && fileOut.charAt(fileout.length - 1) == "/") { + if (size === 0 && fileOut.charAt(fileout.length - 1) == "/") { //skipdir if (debug) alert("skipdir"); @@ -839,7 +834,7 @@ JXG.Util.Unzip = function(barray) { skipdir(); } } - }; + } function skipdir() { var crc, @@ -952,7 +947,7 @@ JXG.Util.Unzip = function(barray) { if (modeZIP) nextFile(); - }; + } }; @@ -1096,7 +1091,7 @@ JXG.Util.Base64 = { lineno, i, destripped = []; - if (wrap == null) + if (wrap === null) wrap = 76; stripped.replace(/ /g, ""); @@ -1251,7 +1246,7 @@ JXG.Util.utf8Decode = function(utftext) { string.push(String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))); i += 3; } - }; + } return string.join(''); }; @@ -1302,13 +1297,13 @@ module.exports = JXG;
diff --git a/doc/key.js.html b/doc/key.js.html index 300d7691..7c458875 100644 --- a/doc/key.js.html +++ b/doc/key.js.html @@ -86,19 +86,19 @@ Key.prototype.packetlist2structure = function(packetlist) { var user, primaryKeyId, subKey; for (var i = 0; i < packetlist.length; i++) { switch (packetlist[i].tag) { - case enums.packet.public_key: - case enums.packet.secret_key: + case enums.packet.publicKey: + case enums.packet.secretKey: this.primaryKey = packetlist[i]; primaryKeyId = this.primaryKey.getKeyId(); break; case enums.packet.userid: - case enums.packet.user_attribute: + case enums.packet.userAttribute: user = new User(packetlist[i]); if (!this.users) this.users = []; this.users.push(user); break; - case enums.packet.public_subkey: - case enums.packet.secret_subkey: + case enums.packet.publicSubkey: + case enums.packet.secretSubkey: user = null; if (!this.subKeys) this.subKeys = []; subKey = new SubKey(packetlist[i]); @@ -151,15 +151,16 @@ Key.prototype.packetlist2structure = function(packetlist) { * @return {module:packet/packetlist} The packets that form a key */ Key.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.primaryKey); packetlist.push(this.revocationSignature); packetlist.concat(this.directSignatures); - for (var i = 0; i < this.users.length; i++) { + var i; + for (i = 0; i < this.users.length; i++) { packetlist.concat(this.users[i].toPacketlist()); } if (this.subKeys) { - for (var i = 0; i < this.subKeys.length; i++) { + for (i = 0; i < this.subKeys.length; i++) { packetlist.concat(this.subKeys[i].toPacketlist()); } } @@ -227,7 +228,7 @@ function findKey(keys, keyIds) { * @return {(module:packet/public_subkey|module:packet/public_key|null)} */ Key.prototype.getPublicKeyPacket = function(keyIds) { - if (this.primaryKey.tag == enums.packet.public_key) { + if (this.primaryKey.tag == enums.packet.publicKey) { return findKey(this.getAllKeyPackets(), keyIds); } else { return null; @@ -240,7 +241,7 @@ Key.prototype.getPublicKeyPacket = function(keyIds) { * @return {(module:packet/secret_subkey|module:packet/secret_key|null)} */ Key.prototype.getPrivateKeyPacket = function(keyIds) { - if (this.primaryKey.tag == enums.packet.secret_key) { + if (this.primaryKey.tag == enums.packet.secretKey) { return findKey(this.getAllKeyPackets(), keyIds); } else { return null; @@ -266,7 +267,7 @@ Key.prototype.getUserIds = function() { * @return {Boolean} */ Key.prototype.isPublic = function() { - return this.primaryKey.tag == enums.packet.public_key; + return this.primaryKey.tag == enums.packet.publicKey; }; /** @@ -274,7 +275,7 @@ Key.prototype.isPublic = function() { * @return {Boolean} */ Key.prototype.isPrivate = function() { - return this.primaryKey.tag == enums.packet.secret_key; + return this.primaryKey.tag == enums.packet.secretKey; }; /** @@ -282,19 +283,20 @@ Key.prototype.isPrivate = function() { * @return {module:key~Key} new public Key */ Key.prototype.toPublic = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); var keyPackets = this.toPacketlist(); + var bytes; for (var i = 0; i < keyPackets.length; i++) { switch (keyPackets[i].tag) { - case enums.packet.secret_key: - var bytes = keyPackets[i].writePublicKey(); - var pubKeyPacket = new packet.public_key(); + case enums.packet.secretKey: + bytes = keyPackets[i].writePublicKey(); + var pubKeyPacket = new packet.PublicKey(); pubKeyPacket.read(bytes); packetlist.push(pubKeyPacket); break; - case enums.packet.secret_subkey: - var bytes = keyPackets[i].writePublicKey(); - var pubSubkeyPacket = new packet.public_subkey(); + case enums.packet.secretSubkey: + bytes = keyPackets[i].writePublicKey(); + var pubSubkeyPacket = new packet.PublicSubkey(); pubSubkeyPacket.read(bytes); packetlist.push(pubSubkeyPacket); break; @@ -355,7 +357,7 @@ function isValidEncryptionKeyPacket(keyPacket, signature) { ((signature.keyFlags & enums.keyFlags.encrypt_communication) !== 0 || (signature.keyFlags & enums.keyFlags.encrypt_storage) !== 0 || !signature.keyFlags); -}; +} function isValidSigningKeyPacket(keyPacket, signature) { return (keyPacket.algorithm == enums.read(enums.publicKey, enums.publicKey.dsa) || @@ -363,7 +365,7 @@ function isValidSigningKeyPacket(keyPacket, signature) { keyPacket.algorithm == enums.read(enums.publicKey, enums.publicKey.rsa_encrypt_sign)) && ((signature.keyFlags & enums.keyFlags.sign_data) !== 0 || !signature.keyFlags); -}; +} /** * Returns the first valid encryption key packet for this key @@ -496,7 +498,7 @@ Key.prototype.getPrimaryUser = function() { } } return user ? {user: user, selfCertificate: userSelfCert} : null; -} +}; // TODO Key.prototype.revoke = function() { @@ -512,7 +514,7 @@ function User(userPacket) { return new User(userPacket); } this.userId = userPacket.tag == enums.packet.userid ? userPacket : null; - this.userAttribute = userPacket.tag == enums.packet.user_attribute ? userPacket : null + this.userAttribute = userPacket.tag == enums.packet.userAttribute ? userPacket : null; this.selfCertifications = null; this.otherCertifications = null; this.revocationCertifications = null; @@ -523,7 +525,7 @@ function User(userPacket) { * @return {module:packet/packetlist} */ User.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.userId || this.userAttribute); packetlist.concat(this.revocationCertifications); packetlist.concat(this.selfCertifications); @@ -629,7 +631,7 @@ function SubKey(subKeyPacket) { * @return {module:packet/packetlist} */ SubKey.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.subKey); packetlist.push(this.revocationSignature); packetlist.push(this.bindingSignature); @@ -707,11 +709,11 @@ function readArmored(armoredText) { if (!(input.type == enums.armor.public_key || input.type == enums.armor.private_key)) { throw new Error('Armored text not of type key'); } - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input.data); - var keyIndex = packetlist.indexOfTag(enums.packet.public_key, enums.packet.secret_key); - if (keyIndex.length == 0) { - throw new Error('No key packet found in armored text') + var keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey); + if (keyIndex.length === 0) { + throw new Error('No key packet found in armored text'); } for (var i = 0; i < keyIndex.length; i++) { var oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]); @@ -742,20 +744,20 @@ function readArmored(armoredText) { * @static */ function generate(keyType, numBits, userId, passphrase) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); - var secretKeyPacket = new packet.secret_key(); + var secretKeyPacket = new packet.SecretKey(); secretKeyPacket.algorithm = enums.read(enums.publicKey, keyType); secretKeyPacket.generate(numBits); secretKeyPacket.encrypt(passphrase); - var userIdPacket = new packet.userid(); + var userIdPacket = new packet.Userid(); userIdPacket.read(userId); var dataToSign = {}; dataToSign.userid = userIdPacket; dataToSign.key = secretKeyPacket; - var signaturePacket = new packet.signature(); + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = enums.signature.cert_generic; signaturePacket.publicKeyAlgorithm = keyType; //TODO we should load preferred hash from config, or as input to this function @@ -763,7 +765,7 @@ function generate(keyType, numBits, userId, passphrase) { signaturePacket.keyFlags = [enums.keyFlags.certify_keys | enums.keyFlags.sign_data]; signaturePacket.sign(secretKeyPacket, dataToSign); - var secretSubkeyPacket = new packet.secret_subkey(); + var secretSubkeyPacket = new packet.SecretSubkey(); secretSubkeyPacket.algorithm = enums.read(enums.publicKey, keyType); secretSubkeyPacket.generate(numBits); secretSubkeyPacket.encrypt(passphrase); @@ -771,7 +773,7 @@ function generate(keyType, numBits, userId, passphrase) { dataToSign = {}; dataToSign.key = secretKeyPacket; dataToSign.bind = secretSubkeyPacket; - var subkeySignaturePacket = new packet.signature(); + var subkeySignaturePacket = new packet.Signature(); subkeySignaturePacket.signatureType = enums.signature.subkey_binding; subkeySignaturePacket.publicKeyAlgorithm = keyType; //TODO we should load preferred hash from config, or as input to this function @@ -801,13 +803,13 @@ exports.generate = generate;
diff --git a/doc/keyid-Keyid.html b/doc/keyid-Keyid.html new file mode 100644 index 00000000..807fb94c --- /dev/null +++ b/doc/keyid-Keyid.html @@ -0,0 +1,269 @@ + + + + + JSDoc: Class: Keyid + + + + + + + + + + +
+ +

Class: Keyid

+ + + + + +
+ +
+

+ type/keyid~ + + Keyid +

+ +
+ +
+
+ + + + +
+

new Keyid()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

read(input)

+ + +
+
+ + +
+ Parsing method for a key id +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Input to read the key id from
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/keyid.html b/doc/keyid.html index 4f6d1b15..7f979f8a 100644 --- a/doc/keyid.html +++ b/doc/keyid.html @@ -35,6 +35,8 @@
+ +
Implementation of type key id (RFC4880 3.3)

@@ -44,74 +46,6 @@ section "Enhanced Key Formats" below describes how Key IDs are formed.
-
-

new (require("type/keyid"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -164,137 +98,23 @@ formed.
- - - - - - -

Methods

+

Classes

- -
-

read(input)

- - -
-
- - -
- Parsing method for a key id -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Input to read the key id from
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- +
Keyid
+
+ + + + + +
@@ -305,13 +125,13 @@ formed.
diff --git a/doc/keyid.js.html b/doc/keyid.js.html index 8cf723c1..713e2d17 100644 --- a/doc/keyid.js.html +++ b/doc/keyid.js.html @@ -53,44 +53,45 @@ * @module type/keyid */ +module.exports = Keyid; + var util = require('../util'); /** * @constructor */ -module.exports = function keyid() { +function Keyid() { this.bytes = ''; +} - - /** - * Parsing method for a key id - * @param {String} input Input to read the key id from - */ - this.read = function(bytes) { - this.bytes = bytes.substr(0, 8); - }; - - this.write = function() { - return this.bytes; - }; - - this.toHex = function() { - return util.hexstrdump(this.bytes); - }; - - this.equals = function(keyid) { - return this.bytes == keyid.bytes; - }; - - this.isNull = function() { - return this.bytes === ''; - }; +/** + * Parsing method for a key id + * @param {String} input Input to read the key id from + */ +Keyid.prototype.read = function(bytes) { + this.bytes = bytes.substr(0, 8); }; -module.exports.mapToHex = function(keyId) { +Keyid.prototype.write = function() { + return this.bytes; +}; + +Keyid.prototype.toHex = function() { + return util.hexstrdump(this.bytes); +}; + +Keyid.prototype.equals = function(keyid) { + return this.bytes == keyid.bytes; +}; + +Keyid.prototype.isNull = function() { + return this.bytes === ''; +}; + +module.exports.mapToHex = function (keyId) { return keyId.toHex(); -} +}; @@ -101,13 +102,13 @@ module.exports.mapToHex = function(keyId) {
diff --git a/doc/keyring-Keyring.html b/doc/keyring-Keyring.html new file mode 100644 index 00000000..2e54ebf2 --- /dev/null +++ b/doc/keyring-Keyring.html @@ -0,0 +1,1306 @@ + + + + + JSDoc: Class: Keyring + + + + + + + + + + +
+ +

Class: Keyring

+ + + + + +
+ +
+

+ keyring/keyring~ + + Keyring +

+ +
+ +
+
+ + + + +
+

new Keyring(storeHandler)

+ + +
+
+ + +
+ Initialization routine for the keyring. This method reads the +keyring from HTML5 local storage and initializes this instance. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
storeHandler + + +class + + + + + + <optional>
+ + + + + +
class implementing load() and store() methods
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

clear()

+ + +
+
+ + +
+ Clear the keyring - erase all the keys +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

exportKey(index) → {String}

+ + +
+
+ + +
+ returns the armored message representation of the key at key ring index +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
index + + +Integer + + + + the index of the key within the array
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ armored message representing the key object +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

exportPublicKey(index) → {String}

+ + +
+
+ + +
+ returns the armored message representation of the public key portion of the key at key ring index +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
index + + +Integer + + + + the index of the key within the array
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ armored message representing the public key object +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

getKeysForKeyId(keyId) → {Array.<module:key~Key>}

+ + +
+
+ + +
+ Searches the keyring for public keys having the specified key id +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
keyId + + +String + + + + provided as string of hex number (lowercase)
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ public keys found +
+ + + +
+
+ Type +
+
+ +Array.<module:key~Key> + + +
+
+ + + + +
+ + + +
+

getPrivateKeyForAddress(email) → {Array.<module:key~Key>}

+ + +
+
+ + +
+ Searches the keyring for a private key containing the specified email address +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
email + + +String + + + + email address to search for
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ private keys found +
+ + + +
+
+ Type +
+
+ +Array.<module:key~Key> + + +
+
+ + + + +
+ + + +
+

getPublicKeyForAddress(email) → {Array.<module:key~Key>}

+ + +
+
+ + +
+ searches all public keys in the keyring matching the address or address part of the user ids +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
email + + +String + + + + email address to search for
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The public keys associated with provided email address. +
+ + + +
+
+ Type +
+
+ +Array.<module:key~Key> + + +
+
+ + + + +
+ + + +
+

importKey(armored)

+ + +
+
+ + +
+ Imports a key from an ascii armored message +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
armored + + +String + + + + message to read the keys/key from
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

removeKey(index) → {module:key~Key}

+ + +
+
+ + +
+ Removes a public key from the public key keyring at the specified index +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
index + + +Integer + + + + the index of the public key within the publicKeys array
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The public key object which has been removed +
+ + + +
+
+ Type +
+
+ +module:key~Key + + +
+
+ + + + +
+ + + +
+

store()

+ + +
+
+ + +
+ Calls the storeHandler to save the keys +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/keyring.html b/doc/keyring.html index 561bada9..064036f6 100644 --- a/doc/keyring.html +++ b/doc/keyring.html @@ -35,143 +35,12 @@
+ +
The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
-
-

new (require("keyring/keyring"))(storeHandler)

- - -
-
- - -
- Initialization routine for the keyring. This method reads the -keyring from HTML5 local storage and initializes this instance. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
storeHandler - - -class - - - - - - <optional>
- - - - - -
class implementing load() and store() methods
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -224,6 +93,13 @@ keyring from HTML5 local storage and initializes this instance. +

Classes

+ +
+
Keyring
+
+
+ @@ -234,1095 +110,6 @@ keyring from HTML5 local storage and initializes this instance.
-
-

clear()

- - -
-
- - -
- Clear the keyring - erase all the keys -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

exportKey(index) → {String}

- - -
-
- - -
- returns the armored message representation of the key at key ring index -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
index - - -Integer - - - - the index of the key within the array
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- armored message representing the key object -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

exportPublicKey(index) → {String}

- - -
-
- - -
- returns the armored message representation of the public key portion of the key at key ring index -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
index - - -Integer - - - - the index of the key within the array
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- armored message representing the public key object -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getKeysForKeyId(keyId) → {Array.<module:key~Key>}

- - -
-
- - -
- Searches the keyring for public keys having the specified key id -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
keyId - - -String - - - - provided as string of hex number (lowercase)
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- public keys found -
- - - -
-
- Type -
-
- -Array.<module:key~Key> - - -
-
- - - - -
- - - -
-

getPrivateKeyForAddress(email) → {Array.<module:key~Key>}

- - -
-
- - -
- Searches the keyring for a private key containing the specified email address -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
email - - -String - - - - email address to search for
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- private keys found -
- - - -
-
- Type -
-
- -Array.<module:key~Key> - - -
-
- - - - -
- - - -
-

getPublicKeyForAddress(email) → {Array.<module:key~Key>}

- - -
-
- - -
- searches all public keys in the keyring matching the address or address part of the user ids -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
email - - -String - - - - email address to search for
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The public keys associated with provided email address. -
- - - -
-
- Type -
-
- -Array.<module:key~Key> - - -
-
- - - - -
- - - -
-

importKey(armored)

- - -
-
- - -
- Imports a key from an ascii armored message -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
armored - - -String - - - - message to read the keys/key from
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

removeKey(index) → {module:key~Key}

- - -
-
- - -
- Removes a public key from the public key keyring at the specified index -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
index - - -Integer - - - - the index of the public key within the publicKeys array
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The public key object which has been removed -
- - - -
-
- Type -
-
- -module:key~Key - - -
-
- - - - -
- - - -
-

store()

- - -
-
- - -
- Calls the storeHandler to save the keys -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - -

<inner> checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) → {Array.<module:key~Key>}

@@ -2035,13 +822,13 @@ keyring from HTML5 local storage and initializes this instance.

diff --git a/doc/keyring.js.html b/doc/keyring.js.html index a80c80ef..0658d26c 100644 --- a/doc/keyring.js.html +++ b/doc/keyring.js.html @@ -58,162 +58,161 @@ var openpgp = require('openpgp'); * @return {Boolean} True if the input matches the specified key */ -/** +module.exports = Keyring; + + /** * Initialization routine for the keyring. This method reads the * keyring from HTML5 local storage and initializes this instance. * @constructor * @param {class} [storeHandler] class implementing load() and store() methods */ -module.exports = function(storeHandler) { - if (!storeHandler) { - storeHandler = new (require('./localstore.js'))(); - } - this.storeHandler = storeHandler; +function Keyring(storeHandler) { + this.storeHandler = storeHandler || new (require('./localstore.js'))(); this.keys = this.storeHandler.load(); +}; - /** - * Calls the storeHandler to save the keys - */ - this.store = function () { - this.storeHandler.store(this.keys); - }; +/** + * Calls the storeHandler to save the keys + */ +Keyring.prototype.store = function () { + this.storeHandler.store(this.keys); +}; - /** - * Clear the keyring - erase all the keys - */ - this.clear = function() { - this.keys = []; - }; +/** + * Clear the keyring - erase all the keys + */ +Keyring.prototype.clear = function() { + this.keys = []; +}; - /** - * Checks a key to see if it matches the specified email address - * @param {String} email email address to search for - * @param {module:key~Key} key The key to be checked. - * @return {Boolean} True if the email address is defined in the specified key - */ - function emailCheck(email, key) { - email = email.toLowerCase(); - var keyEmails = key.getUserIds(); - for (var i; i < keyEmails.length; i++) { - //we need to get just the email from the userid key - keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase(); - if (keyEmail == email) { - return true; - } +/** + * Checks a key to see if it matches the specified email address + * @param {String} email email address to search for + * @param {module:key~Key} key The key to be checked. + * @return {Boolean} True if the email address is defined in the specified key + */ +function emailCheck(email, key) { + email = email.toLowerCase(); + var keyEmails = key.getUserIds(); + for (var i; i < keyEmails.length; i++) { + //we need to get just the email from the userid key + keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase(); + if (keyEmail == email) { + return true; } - return false; } + return false; +} - /** - * Checks a key to see if it matches the specified keyid - * @param {String} id hex string keyid to search for - * @param {module:key~Key} key the key to be checked. - * @return {Boolean} true if the email address is defined in the specified key - * @inner - */ - 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; - } +/** + * Checks a key to see if it matches the specified keyid + * @param {String} id hex string keyid to search for + * @param {module:key~Key} key the key to be checked. + * @return {Boolean} true if the email address is defined in the specified key + * @inner + */ +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; } + return false; +} - /** - * searches all public keys in the keyring matching the address or address part of the user ids - * @param {Array<module:key~Key>} keys array of keys to search - * @param {module:keyring/keyring.checkCallback} identityFunction callback function which checks for a match - * @param {String} identityInput input to check against - * @param {module:enums.packet} keyType packet types of keys to check - * @return {Array<module:key~Key>} array of keys which match - */ - function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) { - var results = []; - 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)) { - results.push(key); - } - break; - case openpgp.enums.packet.private_key: - if (key.isPrivate() && identityFunction(identityInput, key)) { - results.push(key); - } - break; - } +/** + * searches all public keys in the keyring matching the address or address part of the user ids + * @param {Array<module:key~Key>} keys array of keys to search + * @param {module:keyring/keyring.checkCallback} identityFunction callback function which checks for a match + * @param {String} identityInput input to check against + * @param {module:enums.packet} keyType packet types of keys to check + * @return {Array<module:key~Key>} array of keys which match + */ +function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) { + var results = []; + for (var p = 0; p < keys.length; p++) { + var key = keys[p]; + switch (keyType) { + case openpgp.enums.packet.publicKey: + if (key.isPublic() && identityFunction(identityInput, key)) { + results.push(key); + } + break; + case openpgp.enums.packet.private_key: + if (key.isPrivate() && identityFunction(identityInput, key)) { + results.push(key); + } + break; } - return results; } + return results; +} - /** - * searches all public keys in the keyring matching the address or address part of the user ids - * @param {String} email email address to search for - * @return {Array<module:key~Key>} The public keys associated with provided email address. - */ - this.getPublicKeyForAddress = function (email) { - return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.public_key); - }; +/** + * searches all public keys in the keyring matching the address or address part of the user ids + * @param {String} email email address to search for + * @return {Array<module:key~Key>} The public keys associated with provided email address. + */ +Keyring.prototype.getPublicKeyForAddress = function (email) { + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.publicKey); +}; - /** - * Searches the keyring for a private key containing the specified email address - * @param {String} email email address to search for - * @return {Array<module:key~Key>} private keys found - */ - this.getPrivateKeyForAddress = function (email) { - return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secret_key); - }; +/** + * Searches the keyring for a private key containing the specified email address + * @param {String} email email address to search for + * @return {Array<module:key~Key>} private keys found + */ +Keyring.prototype.getPrivateKeyForAddress = function (email) { + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secretKey); +}; - /** - * Searches the keyring for public keys having the specified key id - * @param {String} keyId provided as string of hex number (lowercase) - * @return {Array<module:key~Key>} public keys found - */ - this.getKeysForKeyId = function (keyId) { - return checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.public_key); - }; +/** + * Searches the keyring for public keys having the specified key id + * @param {String} keyId provided as string of hex number (lowercase) + * @return {Array<module:key~Key>} public keys found + */ +Keyring.prototype.getKeysForKeyId = function (keyId) { + return checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.publicKey); +}; - /** - * Imports a key from an ascii armored message - * @param {String} armored message to read the keys/key from - */ - this.importKey = function (armored) { - this.keys = this.keys.concat(openpgp.key.readArmored(armored).keys); +/** + * Imports a key from an ascii armored message + * @param {String} armored message to read the keys/key from + */ +Keyring.prototype.importKey = function (armored) { + this.keys = this.keys.concat(openpgp.key.readArmored(armored).keys); - return true; - }; + return true; +}; - /** - * returns the armored message representation of the key at key ring index - * @param {Integer} index the index of the key within the array - * @return {String} armored message representing the key object - */ - this.exportKey = function (index) { - return this.keys[index].armor(); - }; +/** + * returns the armored message representation of the key at key ring index + * @param {Integer} index the index of the key within the array + * @return {String} armored message representing the key object + */ +Keyring.prototype.exportKey = function (index) { + return this.keys[index].armor(); +}; - /** - * 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 - * @return {module:key~Key} The public key object which has been removed - */ - this.removeKey = function (index) { - var removed = this.keys.splice(index, 1); +/** + * 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 + * @return {module:key~Key} The public key object which has been removed + */ +Keyring.prototype.removeKey = function (index) { + var removed = this.keys.splice(index, 1); - return removed; - }; + return removed; +}; - /** - * 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 - */ - this.exportPublicKey = function (index) { - return this.keys[index].toPublic().armor(); - }; +/** + * 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 + */ +Keyring.prototype.exportPublicKey = function (index) { + return this.keys[index].toPublic().armor(); };
@@ -225,13 +224,13 @@ module.exports = function(storeHandler) {
diff --git a/doc/literal-Literal.html b/doc/literal-Literal.html new file mode 100644 index 00000000..6ad79fbf --- /dev/null +++ b/doc/literal-Literal.html @@ -0,0 +1,924 @@ + + + + + JSDoc: Class: Literal + + + + + + + + + + +
+ +

Class: Literal

+ + + + + +
+ +
+

+ packet/literal~ + + Literal +

+ +
+ +
+
+ + + + +
+

new Literal()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

getBytes() → {String}

+ + +
+
+ + +
+ Get the byte sequence representing the literal packet data +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ A sequence of bytes +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

getText() → {String}

+ + +
+
+ + +
+ Returns literal data packets as native JavaScript string +with normalized end of line to \n +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ literal data as text +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

read(input, position, len) → {module:packet/literal}

+ + +
+
+ + +
+ Parsing function for a literal data packet (tag 11). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of a tag 11 packet
position + + +Integer + + + + Position to start reading from the input string
len + + +Integer + + + + Length of the packet or the remaining length of + input at position
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ object representation +
+ + + +
+
+ Type +
+
+ +module:packet/literal + + +
+
+ + + + +
+ + + +
+

setBytes(bytes, format)

+ + +
+
+ + +
+ Set the packet data to value represented by the provided string of bytes. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
bytes + + +String + + + + The string of bytes
format + + +utf8 +| + +binary +| + +text + + + + The format of the string of bytes
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

setText(text)

+ + +
+
+ + +
+ Set the packet data to a javascript native string, end of line +will be normalized to \r\n and by default text is converted to UTF8 +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
text + + +String + + + + Any native javascript string
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

write(data) → {String}

+ + +
+
+ + +
+ Creates a string representation of the packet +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
data + + +String + + + + The data to be inserted as body
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ string-representation of the packet +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/literal.html b/doc/literal.html index 70fe457c..a934494e 100644 --- a/doc/literal.html +++ b/doc/literal.html @@ -35,6 +35,8 @@
+ +
Implementation of the Literal Data Packet (Tag 11)

@@ -42,74 +44,6 @@ RFC4880 5.9: A Literal Data packet contains the body of a message; data that is not to be further interpreted.
-
-

new (require("packet/literal"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -164,792 +98,23 @@ is not to be further interpreted.
- - - - - - -

Methods

+

Classes

- -
-

getBytes() → {String}

- - -
-
- - -
- Get the byte sequence representing the literal packet data -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A sequence of bytes -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getText() → {String}

- - -
-
- - -
- Returns literal data packets as native JavaScript string -with normalized end of line to \n -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- literal data as text -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input, position, len) → {module:packet/literal}

- - -
-
- - -
- Parsing function for a literal data packet (tag 11). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of a tag 11 packet
position - - -Integer - - - - Position to start reading from the input string
len - - -Integer - - - - Length of the packet or the remaining length of - input at position
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- object representation -
- - - -
-
- Type -
-
- -module:packet/literal - - -
-
- - - - -
- - - -
-

setBytes(bytes, format)

- - -
-
- - -
- Set the packet data to value represented by the provided string of bytes. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - The string of bytes
format - - -utf8 -| - -binary -| - -text - - - - The format of the string of bytes
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

setText(text)

- - -
-
- - -
- Set the packet data to a javascript native string, end of line -will be normalized to \r\n and by default text is converted to UTF8 -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
text - - -String - - - - Any native javascript string
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

write(data) → {String}

- - -
-
- - -
- Creates a string representation of the packet -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
data - - -String - - - - The data to be inserted as body
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- string-representation of the packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- +
Literal
+
+ + + + + +
@@ -960,13 +125,13 @@ will be normalized to \r\n and by default text is converted to UTF8
diff --git a/doc/literal.js.html b/doc/literal.js.html index 9fd3c59a..27807408 100644 --- a/doc/literal.js.html +++ b/doc/literal.js.html @@ -52,108 +52,109 @@ * @module packet/literal */ +module.exports = Literal; + var util = require('../util'), enums = require('../enums.js'); /** * @constructor */ -module.exports = function literal() { +function Literal() { this.format = 'utf8'; // default format for literal data packets this.data = ''; // literal data representation as native JavaScript string or bytes this.date = new Date(); - - - /** - * Set the packet data to a javascript native string, end of line - * will be normalized to \r\n and by default text is converted to UTF8 - * @param {String} text Any native javascript string - */ - this.setText = function (text) { - // normalize EOL to \r\n - text = text.replace(/\r/g, '').replace(/\n/g, '\r\n'); - // encode UTF8 - this.data = this.format == 'utf8' ? util.encode_utf8(text) : text; - } - - /** - * Returns literal data packets as native JavaScript string - * with normalized end of line to \n - * @return {String} literal data as text - */ - this.getText = function () { - // decode UTF8 - var text = util.decode_utf8(this.data); - // normalize EOL to \n - return text.replace(/\r\n/g, '\n'); - } - - /** - * Set the packet data to value represented by the provided string of bytes. - * @param {String} bytes The string of bytes - * @param {utf8|binary|text} format The format of the string of bytes - */ - this.setBytes = function (bytes, format) { - this.format = format; - this.data = bytes; - } - - - /** - * Get the byte sequence representing the literal packet data - * @returns {String} A sequence of bytes - */ - this.getBytes = function () { - return this.data; - } - - - /** - * Parsing function for a literal data packet (tag 11). - * - * @param {String} input Payload of a tag 11 packet - * @param {Integer} position - * Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/literal} object representation - */ - this.read = function (bytes) { - // - A one-octet field that describes how the data is formatted. - - var format = enums.read(enums.literal, bytes.charCodeAt(0)); - - var filename_len = bytes.charCodeAt(1); - this.filename = util.decode_utf8(bytes.substr(2, filename_len)); - - this.date = util.readDate(bytes.substr(2 + filename_len, 4)); - - var data = bytes.substring(6 + filename_len); - - this.setBytes(data, format); - } - - /** - * Creates a string representation of the packet - * - * @param {String} data The data to be inserted as body - * @return {String} string-representation of the packet - */ - this.write = function () { - var filename = util.encode_utf8("msg.txt"); - - var data = this.getBytes(); - - var result = ''; - result += String.fromCharCode(enums.write(enums.literal, this.format)); - result += String.fromCharCode(filename.length); - result += filename; - result += util.writeDate(this.date); - result += data; - return result; - } } + +/** + * Set the packet data to a javascript native string, end of line + * will be normalized to \r\n and by default text is converted to UTF8 + * @param {String} text Any native javascript string + */ +Literal.prototype.setText = function (text) { + // normalize EOL to \r\n + text = text.replace(/\r/g, '').replace(/\n/g, '\r\n'); + // encode UTF8 + this.data = this.format == 'utf8' ? util.encode_utf8(text) : text; +}; + +/** + * Returns literal data packets as native JavaScript string + * with normalized end of line to \n + * @return {String} literal data as text + */ +Literal.prototype.getText = function () { + // decode UTF8 + var text = util.decode_utf8(this.data); + // normalize EOL to \n + return text.replace(/\r\n/g, '\n'); +}; + +/** + * Set the packet data to value represented by the provided string of bytes. + * @param {String} bytes The string of bytes + * @param {utf8|binary|text} format The format of the string of bytes + */ +Literal.prototype.setBytes = function (bytes, format) { + this.format = format; + this.data = bytes; +}; + + +/** + * Get the byte sequence representing the literal packet data + * @returns {String} A sequence of bytes + */ +Literal.prototype.getBytes = function () { + return this.data; +}; + + +/** + * Parsing function for a literal data packet (tag 11). + * + * @param {String} input Payload of a tag 11 packet + * @param {Integer} position + * Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/literal} object representation + */ +Literal.prototype.read = function (bytes) { + // - A one-octet field that describes how the data is formatted. + + var format = enums.read(enums.literal, bytes.charCodeAt(0)); + + var filename_len = bytes.charCodeAt(1); + this.filename = util.decode_utf8(bytes.substr(2, filename_len)); + + this.date = util.readDate(bytes.substr(2 + filename_len, 4)); + + var data = bytes.substring(6 + filename_len); + + this.setBytes(data, format); +}; + +/** + * Creates a string representation of the packet + * + * @param {String} data The data to be inserted as body + * @return {String} string-representation of the packet + */ +Literal.prototype.write = function () { + var filename = util.encode_utf8("msg.txt"); + + var data = this.getBytes(); + + var result = ''; + result += String.fromCharCode(enums.write(enums.literal, this.format)); + result += String.fromCharCode(filename.length); + result += filename; + result += util.writeDate(this.date); + result += data; + return result; +}; @@ -164,13 +165,13 @@ module.exports = function literal() {
diff --git a/doc/localStorage-LocalStorage.html b/doc/localStorage-LocalStorage.html new file mode 100644 index 00000000..65ad7272 --- /dev/null +++ b/doc/localStorage-LocalStorage.html @@ -0,0 +1,292 @@ + + + + + JSDoc: Class: LocalStorage + + + + + + + + + + +
+ +

Class: LocalStorage

+ + + + + +
+ +
+

+ config/localStorage~ + + LocalStorage +

+ +
+ +
+
+ + + + +
+

new LocalStorage()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

read()

+ + +
+
+ + +
+ Reads the config out of the HTML5 local storage +and initializes the object config. +if config is null the default config will be used +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

write()

+ + +
+
+ + +
+ Writes the config to HTML5 local storage +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/localStorage.html b/doc/localStorage.html index a66d2ac9..f7ff9c56 100644 --- a/doc/localStorage.html +++ b/doc/localStorage.html @@ -35,78 +35,12 @@
+ +
This object storing and retrieving configuration from HTML5 local storage.
-
-

new (require("config/localStorage"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -153,160 +87,23 @@ - - - - - - -

Methods

+

Classes

- -
-

read()

- - -
-
- - -
- Reads the config out of the HTML5 local storage -and initializes the object config. -if config is null the default config will be used -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

write()

- - -
-
- - -
- Writes the config to HTML5 local storage -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- +
LocalStorage
+
+ + + + + +
@@ -317,13 +114,13 @@ if config is null the default config will be used
diff --git a/doc/localStorage.js.html b/doc/localStorage.js.html index b5df0561..9d6be675 100644 --- a/doc/localStorage.js.html +++ b/doc/localStorage.js.html @@ -30,32 +30,34 @@ * @module config/localStorage */ +module.exports = LocalStorage; + /** * @constructor */ -module.exports = function localStorage() { - - /** - * Reads the config out of the HTML5 local storage - * and initializes the object config. - * if config is null the default config will be used - */ - this.read = function () { - var cf = JSON.parse(window.localStorage.getItem("config")); - if (cf === null) { - this.config = this.default_config; - this.write(); - } else - this.config = cf; - } - - /** - * Writes the config to HTML5 local storage - */ - this.write = function () { - window.localStorage.setItem("config", JSON.stringify(this.config)); - } +function LocalStorage() { } + +/** + * Reads the config out of the HTML5 local storage + * and initializes the object config. + * if config is null the default config will be used + */ +LocalStorage.prototype.read = function () { + var cf = JSON.parse(window.localStorage.getItem("config")); + if (cf === null) { + this.config = this.default_config; + this.write(); + } else + this.config = cf; +}; + +/** + * Writes the config to HTML5 local storage + */ +LocalStorage.prototype.write = function () { + window.localStorage.setItem("config", JSON.stringify(this.config)); +}; @@ -66,13 +68,13 @@ module.exports = function localStorage() {
diff --git a/doc/localstore.html b/doc/localstore.html index c41a639e..2d55529e 100644 --- a/doc/localstore.html +++ b/doc/localstore.html @@ -99,219 +99,6 @@ -

Methods

- -
- -
-

<static> load() → {Array.<module:key~Key>}

- - -
-
- - -
- Load the keyring from HTML5 local storage and initializes this instance. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- array of keys retrieved from localstore -
- - - -
-
- Type -
-
- -Array.<module:key~Key> - - -
-
- - - - -
- - - -
-

<static> store(keys)

- - -
-
- - -
- Saves the current state of the keyring to HTML5 local storage. -The privateKeys array and publicKeys array gets Stringified using JSON -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
keys - - -Array.<module:key~Key> - - - - array of keys to save in localstore
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- -
- @@ -326,13 +113,13 @@ The privateKeys array and publicKeys array gets Stringified using JSON
diff --git a/doc/localstore.js.html b/doc/localstore.js.html index e2f1a9be..18c9e837 100644 --- a/doc/localstore.js.html +++ b/doc/localstore.js.html @@ -47,39 +47,41 @@ * @requires openpgp * @module keyring/localstore */ +module.exports = LocalStore; var openpgp = require('openpgp'); -module.exports = function () { - /** - * Load the keyring from HTML5 local storage and initializes this instance. - * @return {Array<module:key~Key>} array of keys retrieved from localstore - */ - this.load = function () { - var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys")); - var keys = []; - if (armoredKeys !== null && armoredKeys.length !== 0) { - var key; - for (var i = 0; i < armoredKeys.length; i++) { - key = openpgp.key.readArmored(armoredKeys[i]); - keys.push(key); - } - } - return keys; - } +function LocalStore() { +} - /** - * Saves the current state of the keyring to HTML5 local storage. - * The privateKeys array and publicKeys array gets Stringified using JSON - * @param {Array<module:key~Key>} keys array of keys to save in localstore - */ - this.store = function (keys) { - var armoredKeys = []; - for (var i = 0; i < keys.length; i++) { - armoredKeys.push(keys[i].armor()); +/** + * Load the keyring from HTML5 local storage and initializes this instance. + * @return {Array<module:key~Key>} array of keys retrieved from localstore + */ +LocalStore.prototype.load = function () { + var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys")); + var keys = []; + if (armoredKeys !== null && armoredKeys.length !== 0) { + var key; + for (var i = 0; i < armoredKeys.length; i++) { + key = openpgp.key.readArmored(armoredKeys[i]); + keys.push(key); } - window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys)); } + return keys; +}; + +/** + * Saves the current state of the keyring to HTML5 local storage. + * The privateKeys array and publicKeys array gets Stringified using JSON + * @param {Array<module:key~Key>} keys array of keys to save in localstore + */ +LocalStore.prototype.store = function (keys) { + var armoredKeys = []; + for (var i = 0; i < keys.length; i++) { + armoredKeys.push(keys[i].armor()); + } + window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys)); }; @@ -91,13 +93,13 @@ module.exports = function () {
diff --git a/doc/marker-Marker.html b/doc/marker-Marker.html new file mode 100644 index 00000000..77a3138b --- /dev/null +++ b/doc/marker-Marker.html @@ -0,0 +1,338 @@ + + + + + JSDoc: Class: Marker + + + + + + + + + + +
+ +

Class: Marker

+ + + + + +
+ +
+

+ packet/marker~ + + Marker +

+ +
+ +
+
+ + + + +
+

new Marker()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

read(input, position, len) → {module:packet/marker}

+ + +
+
+ + +
+ Parsing function for a literal data packet (tag 10). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of a tag 10 packet
position + + +Integer + + + + Position to start reading from the input string
len + + +Integer + + + + Length of the packet or the remaining length of + input at position
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Object representation +
+ + + +
+
+ Type +
+
+ +module:packet/marker + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/marker.html b/doc/marker.html index de21e0d9..2208f53b 100644 --- a/doc/marker.html +++ b/doc/marker.html @@ -35,6 +35,8 @@
+ +
Implementation of the strange "Marker packet" (Tag 10)

@@ -46,74 +48,6 @@ the Marker packet.
Such a packet MUST be ignored when received.
-
-

new (require("packet/marker"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -160,206 +94,23 @@ Such a packet MUST be ignored when received.
- - - - - - -

Methods

+

Classes

- -
-

read(input, position, len) → {module:packet/marker}

- - -
-
- - -
- Parsing function for a literal data packet (tag 10). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of a tag 10 packet
position - - -Integer - - - - Position to start reading from the input string
len - - -Integer - - - - Length of the packet or the remaining length of - input at position
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Object representation -
- - - -
-
- Type -
-
- -module:packet/marker - - -
-
- - - - -
- +
Marker
+
+ + + + + +
@@ -370,13 +121,13 @@ Such a packet MUST be ignored when received.
diff --git a/doc/marker.js.html b/doc/marker.js.html index 0d7d8d83..89d67669 100644 --- a/doc/marker.js.html +++ b/doc/marker.js.html @@ -55,30 +55,33 @@ * @module packet/marker */ +module.exports = Marker; + /** * @constructor */ -module.exports = function marker() { - /** - * Parsing function for a literal data packet (tag 10). - * - * @param {String} input Payload of a tag 10 packet - * @param {Integer} position - * Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/marker} Object representation - */ - this.read = function (bytes) { - if (bytes.charCodeAt(0) == 0x50 && // P - bytes.charCodeAt(1) == 0x47 && // G - bytes.charCodeAt(2) == 0x50) // P - return true; - // marker packet does not contain "PGP" - return false; - } +function Marker() { } + +/** + * Parsing function for a literal data packet (tag 10). + * + * @param {String} input Payload of a tag 10 packet + * @param {Integer} position + * Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/marker} Object representation + */ +Marker.prototype.read = function (bytes) { + if (bytes.charCodeAt(0) == 0x50 && // P + bytes.charCodeAt(1) == 0x47 && // G + bytes.charCodeAt(2) == 0x50) // P + return true; + // marker packet does not contain "PGP" + return false; +}; @@ -89,13 +92,13 @@ module.exports = function marker() {
diff --git a/doc/md5.html b/doc/md5.html index 8c77e0ef..80aa0b38 100644 --- a/doc/md5.html +++ b/doc/md5.html @@ -229,13 +229,13 @@
diff --git a/doc/md5.js.html b/doc/md5.js.html index 596d8779..2fef87d0 100644 --- a/doc/md5.js.html +++ b/doc/md5.js.html @@ -54,7 +54,7 @@ module.exports = function (entree) { var hex = md5(entree); var bin = util.hex2bin(hex); return bin; -} +}; function md5cycle(x, k) { var a = x[0], @@ -252,13 +252,13 @@ if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
diff --git a/doc/message.js.html b/doc/message.js.html index 8dd189e3..d79160d7 100644 --- a/doc/message.js.html +++ b/doc/message.js.html @@ -69,7 +69,7 @@ function Message(packetlist) { if (!(this instanceof Message)) { return new Message(packetlist); } - this.packets = packetlist || new packet.list(); + this.packets = packetlist || new packet.List(); } /** @@ -78,7 +78,7 @@ function Message(packetlist) { */ Message.prototype.getEncryptionKeyIds = function() { var keyIds = []; - var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.public_key_encrypted_session_key); + var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey); pkESKeyPacketlist.forEach(function(packet) { keyIds.push(packet.publicKeyId); }); @@ -93,7 +93,7 @@ Message.prototype.getSigningKeyIds = function() { var keyIds = []; var msg = this.unwrapCompressed(); // search for one pass signatures - var onePassSigList = msg.packets.filterByTag(enums.packet.one_pass_signature); + var onePassSigList = msg.packets.filterByTag(enums.packet.onePassSignature); onePassSigList.forEach(function(packet) { keyIds.push(packet.signingKeyId); }); @@ -120,7 +120,7 @@ Message.prototype.decrypt = function(privateKey) { } var privateKeyPacket = privateKey.getPrivateKeyPacket(encryptionKeyIds); if (!privateKeyPacket.isDecrypted) throw new Error('Private key is not decrypted.'); - var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.public_key_encrypted_session_key); + var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey); var pkESKeyPacket; for (var i = 0; i < pkESKeyPacketlist.length; i++) { if (pkESKeyPacketlist[i].publicKeyId.equals(privateKeyPacket.getKeyId())) { @@ -130,7 +130,7 @@ Message.prototype.decrypt = function(privateKey) { } } if (pkESKeyPacket) { - var symEncryptedPacketlist = this.packets.filterByTag(enums.packet.symmetrically_encrypted, enums.packet.sym_encrypted_integrity_protected); + var symEncryptedPacketlist = this.packets.filterByTag(enums.packet.symmetricallyEncrypted, enums.packet.symEncryptedIntegrityProtected); if (symEncryptedPacketlist.length !== 0) { var symEncryptedPacket = symEncryptedPacketlist[0]; symEncryptedPacket.decrypt(pkESKeyPacket.sessionKeyAlgorithm, pkESKeyPacket.sessionKey); @@ -167,13 +167,13 @@ Message.prototype.getText = function() { * @return {Array<module:message~Message>} new message with encrypted content */ Message.prototype.encrypt = function(keys) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); //TODO get preferred algo from signature var sessionKey = crypto.generateSessionKey(enums.read(enums.symmetric, config.encryption_cipher)); keys.forEach(function(key) { var encryptionKeyPacket = key.getEncryptionKeyPacket(); if (encryptionKeyPacket) { - var pkESKeyPacket = new packet.public_key_encrypted_session_key(); + var pkESKeyPacket = new packet.PublicKeyEncryptedSessionKey(); pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId(); pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm; pkESKeyPacket.sessionKey = sessionKey; @@ -187,9 +187,9 @@ Message.prototype.encrypt = function(keys) { }); var symEncryptedPacket; if (config.integrity_protect) { - symEncryptedPacket = new packet.sym_encrypted_integrity_protected(); + symEncryptedPacket = new packet.SymEncryptedIntegrityProtected(); } else { - symEncryptedPacket = new packet.symmetrically_encrypted(); + symEncryptedPacket = new packet.SymmetricallyEncrypted(); } symEncryptedPacket.packets = this.packets; //TODO get preferred algo from signature @@ -205,17 +205,17 @@ Message.prototype.encrypt = function(keys) { */ Message.prototype.sign = function(privateKeys) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); var literalDataPacket = this.packets.findPacket(enums.packet.literal); if (!literalDataPacket) throw new Error('No literal data packet to sign.'); var literalFormat = enums.write(enums.literal, literalDataPacket.format); - var signatureType = literalFormat == enums.literal.binary - ? enums.signature.binary : enums.signature.text; - - for (var i = 0; i < privateKeys.length; i++) { - var onePassSig = new packet.one_pass_signature(); + var signatureType = literalFormat == enums.literal.binary ? + enums.signature.binary : enums.signature.text; + var i; + for (i = 0; i < privateKeys.length; i++) { + var onePassSig = new packet.OnePassSignature(); onePassSig.type = signatureType; //TODO get preferred hashg algo from key signature onePassSig.hashAlgorithm = config.prefer_hash_algorithm; @@ -230,8 +230,8 @@ Message.prototype.sign = function(privateKeys) { packetlist.push(literalDataPacket); - for (var i = privateKeys.length - 1; i >= 0; i--) { - var signaturePacket = new packet.signature(); + for (i = privateKeys.length - 1; i >= 0; i--) { + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = signatureType; signaturePacket.hashAlgorithm = config.prefer_hash_algorithm; signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm; @@ -300,7 +300,7 @@ function readArmored(armoredText) { //TODO how do we want to handle bad text? Exception throwing //TODO don't accept non-message armored texts var input = armor.decode(armoredText).data; - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input); var newMessage = new Message(packetlist); return newMessage; @@ -313,10 +313,10 @@ function readArmored(armoredText) { * @static */ function fromText(text) { - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); // text will be converted to UTF8 literalDataPacket.setText(text); - var literalDataPacketlist = new packet.list(); + var literalDataPacketlist = new packet.List(); literalDataPacketlist.push(literalDataPacket); var newMessage = new Message(literalDataPacketlist); return newMessage; @@ -329,9 +329,9 @@ function fromText(text) { * @static */ function fromBinary(bytes) { - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); literalDataPacket.setBytes(bytes, enums.read(enums.literal, enums.literal.binary)); - var literalDataPacketlist = new packet.list(); + var literalDataPacketlist = new packet.List(); literalDataPacketlist.push(literalDataPacket); var newMessage = new Message(literalDataPacketlist); return newMessage; @@ -351,13 +351,13 @@ exports.fromBinary = fromBinary;
diff --git a/doc/module-armor.html b/doc/module-armor.html index d5816512..8dedb041 100644 --- a/doc/module-armor.html +++ b/doc/module-armor.html @@ -112,13 +112,13 @@
diff --git a/doc/module-cleartext-CleartextMessage.html b/doc/module-cleartext-CleartextMessage.html index 700e6030..04795f31 100644 --- a/doc/module-cleartext-CleartextMessage.html +++ b/doc/module-cleartext-CleartextMessage.html @@ -741,13 +741,13 @@ See http://tools.ietf.org/html/rfc4880#section-7
diff --git a/doc/module-cleartext.html b/doc/module-cleartext.html index 8f66e4f8..8186a3fd 100644 --- a/doc/module-cleartext.html +++ b/doc/module-cleartext.html @@ -267,13 +267,13 @@
diff --git a/doc/module-config.html b/doc/module-config.html index c2381b1b..0fd7f548 100644 --- a/doc/module-config.html +++ b/doc/module-config.html @@ -112,13 +112,13 @@
diff --git a/doc/module-crypto.html b/doc/module-crypto.html index 39c1b5bf..fae66fe9 100644 --- a/doc/module-crypto.html +++ b/doc/module-crypto.html @@ -515,13 +515,13 @@
diff --git a/doc/module-enums.html b/doc/module-enums.html index 6d847f60..a141cdce 100644 --- a/doc/module-enums.html +++ b/doc/module-enums.html @@ -1643,7 +1643,7 @@ - public_key_encrypted_session_key + publicKeyEncryptedSessionKey @@ -1701,7 +1701,7 @@ - sym_encrypted_session_key + symEncryptedSessionKey @@ -1730,7 +1730,7 @@ - one_pass_signature + onePassSignature @@ -1759,7 +1759,7 @@ - secret_key + secretKey @@ -1788,7 +1788,7 @@ - public_key + publicKey @@ -1817,7 +1817,7 @@ - secret_subkey + secretSubkey @@ -1875,7 +1875,7 @@ - symmetrically_encrypted + symmetricallyEncrypted @@ -2020,7 +2020,7 @@ - public_subkey + publicSubkey @@ -2049,7 +2049,7 @@ - user_attribute + userAttribute @@ -2078,7 +2078,7 @@ - sym_encrypted_integrity_protected + symEncryptedIntegrityProtected @@ -2107,7 +2107,7 @@ - modification_detection_code + modificationDetectionCode @@ -4527,13 +4527,13 @@ document) that cannot include a target subpacket.
diff --git a/doc/module-key-Key.html b/doc/module-key-Key.html index 2a4ec89c..4c49f135 100644 --- a/doc/module-key-Key.html +++ b/doc/module-key-Key.html @@ -216,7 +216,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -355,7 +355,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -517,7 +517,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -608,7 +608,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -695,7 +695,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -798,7 +798,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -885,7 +885,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -978,7 +978,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1067,7 +1067,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1206,7 +1206,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1347,7 +1347,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1440,7 +1440,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1537,7 +1537,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1624,7 +1624,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1715,7 +1715,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -1802,7 +1802,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -2097,7 +2097,7 @@ Can contain additional subkeys, signatures, user ids, user attributes.
Source:
@@ -2189,7 +2189,7 @@ and valid self signature
Source:
@@ -2252,13 +2252,13 @@ and valid self signature
diff --git a/doc/module-key-SubKey.html b/doc/module-key-SubKey.html index ca977352..891957a6 100644 --- a/doc/module-key-SubKey.html +++ b/doc/module-key-SubKey.html @@ -80,7 +80,7 @@
Source:
@@ -218,7 +218,7 @@
Source:
@@ -356,7 +356,7 @@
Source:
@@ -443,7 +443,7 @@
Source:
@@ -531,7 +531,7 @@ and valid binding signature
Source:
@@ -594,13 +594,13 @@ and valid binding signature
diff --git a/doc/module-key-User.html b/doc/module-key-User.html index cb0d7724..9727a170 100644 --- a/doc/module-key-User.html +++ b/doc/module-key-User.html @@ -80,7 +80,7 @@
Source:
@@ -218,7 +218,7 @@
Source:
@@ -383,7 +383,7 @@
Source:
@@ -474,7 +474,7 @@
Source:
@@ -613,7 +613,7 @@ and validity of self signature
Source:
@@ -676,13 +676,13 @@ and validity of self signature
diff --git a/doc/module-key.html b/doc/module-key.html index fde13d78..c0b816ba 100644 --- a/doc/module-key.html +++ b/doc/module-key.html @@ -281,7 +281,7 @@ Primary and subkey will be of same type.
Source:
@@ -416,7 +416,7 @@ Primary and subkey will be of same type.
Source:
@@ -479,13 +479,13 @@ Primary and subkey will be of same type.
diff --git a/doc/module-keyid.html b/doc/module-keyid.html index b9021d4d..01d1baba 100644 --- a/doc/module-keyid.html +++ b/doc/module-keyid.html @@ -112,13 +112,13 @@
diff --git a/doc/module-message-Message.html b/doc/module-message-Message.html index 8dcfb4f7..99367b60 100644 --- a/doc/module-message-Message.html +++ b/doc/module-message-Message.html @@ -1297,13 +1297,13 @@ See http://tools.ietf.org/html/rfc4880#section-11.3
diff --git a/doc/module-message.html b/doc/module-message.html index e44795a4..a8a360cd 100644 --- a/doc/module-message.html +++ b/doc/module-message.html @@ -547,13 +547,13 @@
diff --git a/doc/module-mpi.html b/doc/module-mpi.html index 42165914..f055805d 100644 --- a/doc/module-mpi.html +++ b/doc/module-mpi.html @@ -112,13 +112,13 @@
diff --git a/doc/module-openpgp.html b/doc/module-openpgp.html index 6323cef8..f0ba52c7 100644 --- a/doc/module-openpgp.html +++ b/doc/module-openpgp.html @@ -1358,13 +1358,13 @@ Primary and subkey will be of same type.
diff --git a/doc/module-packet.html b/doc/module-packet.html index 951c331e..a4097997 100644 --- a/doc/module-packet.html +++ b/doc/module-packet.html @@ -100,7 +100,7 @@
-

<static> compressed

+

<static> Compressed

@@ -157,7 +157,7 @@
-

<static> literal

+

<static> Literal

@@ -214,7 +214,7 @@
-

<static> marker

+

<static> Marker

@@ -271,7 +271,7 @@
-

<static> one_pass_signature

+

<static> OnePassSignature

@@ -328,7 +328,7 @@
-

<static> public_key

+

<static> PublicKey

@@ -385,7 +385,7 @@
-

<static> public_key_encrypted_session_key

+

<static> PublicKeyEncryptedSessionKey

@@ -442,7 +442,7 @@
-

<static> public_subkey

+

<static> PublicSubkey

@@ -499,7 +499,7 @@
-

<static> secret_key

+

<static> SecretKey

@@ -556,7 +556,7 @@
-

<static> secret_subkey

+

<static> SecretSubkey

@@ -613,7 +613,7 @@
-

<static> signature

+

<static> Signature

@@ -670,7 +670,7 @@
-

<static> sym_encrypted_integrity_protected

+

<static> SymEncryptedIntegrityProtected

@@ -727,7 +727,7 @@
-

<static> sym_encrypted_session_key

+

<static> SymEncryptedSessionKey

@@ -784,7 +784,7 @@
-

<static> symmetrically_encrypted

+

<static> SymmetricallyEncrypted

@@ -841,7 +841,7 @@
-

<static> trust

+

<static> Trust

@@ -898,7 +898,7 @@
-

<static> user_attribute

+

<static> UserAttribute

@@ -955,7 +955,7 @@
-

<static> userid

+

<static> Userid

@@ -1027,13 +1027,13 @@
diff --git a/doc/module-s2k.html b/doc/module-s2k.html index bc6723c3..e5c44c09 100644 --- a/doc/module-s2k.html +++ b/doc/module-s2k.html @@ -112,13 +112,13 @@
diff --git a/doc/module-util.html b/doc/module-util.html index 4b434820..e017d883 100644 --- a/doc/module-util.html +++ b/doc/module-util.html @@ -112,13 +112,13 @@
diff --git a/doc/mpi-Mpi.html b/doc/mpi-Mpi.html new file mode 100644 index 00000000..4f0c4be1 --- /dev/null +++ b/doc/mpi-Mpi.html @@ -0,0 +1,440 @@ + + + + + JSDoc: Class: Mpi + + + + + + + + + + +
+ +

Class: Mpi

+ + + + + +
+ +
+

+ type/mpi~ + + Mpi +

+ +
+ +
+
+ + + + +
+

new Mpi()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

data

+ + +
+
+ +
+ An implementation dependent integer +
+ + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

read(input) → {Integer}

+ + +
+
+ + +
+ Parsing function for a mpi (RFC 4880 3.2). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of mpi data
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Length of data read +
+ + + +
+
+ Type +
+
+ +Integer + + +
+
+ + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ Converts the mpi object to a string as specified in RFC4880 3.2 +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ mpi Byte representation +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/mpi.html b/doc/mpi.html index 87d75ab4..055b1ba5 100644 --- a/doc/mpi.html +++ b/doc/mpi.html @@ -35,6 +35,8 @@
+ +
Implementation of type MPI (RFC4880 3.2)

@@ -46,74 +48,6 @@ of the MPI in bits followed by a string of octets that contain the actual integer.
-
-

new (require("type/mpi"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -168,304 +102,19 @@ actual integer.
- - - - -

Members

+

Classes

- -
-

data

- - -
-
- -
- An implementation dependent integer -
- - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
Mpi
+
-

Methods

- -
- -
-

read(input) → {Integer}

-
-
-
- Parsing function for a mpi (RFC 4880 3.2). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of mpi data
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Length of data read -
- - - -
-
- Type -
-
- -Integer - - -
-
- - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- Converts the mpi object to a string as specified in RFC4880 3.2 -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- mpi Byte representation -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -480,13 +129,13 @@ actual integer.
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:45 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/mpi.js.html b/doc/mpi.js.html index 757a7fe3..7fee6825 100644 --- a/doc/mpi.js.html +++ b/doc/mpi.js.html @@ -61,70 +61,72 @@ * @module type/mpi */ +module.exports = Mpi; + var BigInteger = require('../crypto/public_key/jsbn.js'), util = require('../util'); /** * @constructor */ -module.exports = function mpi() { +function Mpi() { /** An implementation dependent integer */ this.data = null; - - /** - * Parsing function for a mpi (RFC 4880 3.2). - * @param {String} input Payload of mpi data - * @return {Integer} Length of data read - */ - this.read = function (bytes) { - var bits = (bytes.charCodeAt(0) << 8) | bytes.charCodeAt(1); - - // Additional rules: - // - // The size of an MPI is ((MPI.length + 7) / 8) + 2 octets. - // - // The length field of an MPI describes the length starting from its - // most significant non-zero bit. Thus, the MPI [00 02 01] is not - // formed correctly. It should be [00 01 01]. - - // TODO: Verification of this size method! This size calculation as - // specified above is not applicable in JavaScript - var bytelen = Math.ceil(bits / 8); - - var raw = bytes.substr(2, bytelen); - this.fromBytes(raw); - - return 2 + bytelen; - }; - - this.fromBytes = function (bytes) { - this.data = new BigInteger(util.hexstrdump(bytes), 16); - }; - - this.toBytes = function () { - return this.write().substr(2); - }; - - this.byteLength = function () { - return this.toBytes().length; - }; - - /** - * Converts the mpi object to a string as specified in RFC4880 3.2 - * @return {String} mpi Byte representation - */ - this.write = function () { - return this.data.toMPI(); - }; - - this.toBigInteger = function () { - return this.data.clone(); - }; - - this.fromBigInteger = function (bn) { - this.data = bn.clone(); - }; } + +/** + * Parsing function for a mpi (RFC 4880 3.2). + * @param {String} input Payload of mpi data + * @return {Integer} Length of data read + */ +Mpi.prototype.read = function (bytes) { + var bits = (bytes.charCodeAt(0) << 8) | bytes.charCodeAt(1); + + // Additional rules: + // + // The size of an MPI is ((MPI.length + 7) / 8) + 2 octets. + // + // The length field of an MPI describes the length starting from its + // most significant non-zero bit. Thus, the MPI [00 02 01] is not + // formed correctly. It should be [00 01 01]. + + // TODO: Verification of this size method! This size calculation as + // specified above is not applicable in JavaScript + var bytelen = Math.ceil(bits / 8); + + var raw = bytes.substr(2, bytelen); + this.fromBytes(raw); + + return 2 + bytelen; +}; + +Mpi.prototype.fromBytes = function (bytes) { + this.data = new BigInteger(util.hexstrdump(bytes), 16); +}; + +Mpi.prototype.toBytes = function () { + return this.write().substr(2); +}; + +Mpi.prototype.byteLength = function () { + return this.toBytes().length; +}; + +/** + * Converts the mpi object to a string as specified in RFC4880 3.2 + * @return {String} mpi Byte representation + */ +Mpi.prototype.write = function () { + return this.data.toMPI(); +}; + +Mpi.prototype.toBigInteger = function () { + return this.data.clone(); +}; + +Mpi.prototype.fromBigInteger = function (bn) { + this.data = bn.clone(); +};
@@ -135,13 +137,13 @@ module.exports = function mpi() {
diff --git a/doc/one_pass_signature-OnePassSignature.html b/doc/one_pass_signature-OnePassSignature.html new file mode 100644 index 00000000..b9f6b4ab --- /dev/null +++ b/doc/one_pass_signature-OnePassSignature.html @@ -0,0 +1,382 @@ + + + + + JSDoc: Class: OnePassSignature + + + + + + + + + + +
+ +

Class: OnePassSignature

+ + + + + +
+ +
+

+ packet/one_pass_signature~ + + OnePassSignature +

+ +
+ +
+
+ + + + +
+

new OnePassSignature()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

read(bytes) → {module:packet/one_pass_signature}

+ + +
+
+ + +
+ parsing function for a one-pass signature packet (tag 4). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
bytes + + +String + + + + payload of a tag 4 packet
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ object representation +
+ + + +
+
+ Type +
+
+ +module:packet/one_pass_signature + + +
+
+ + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ creates a string representation of a one-pass signature packet +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ a string representation of a one-pass signature packet +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/one_pass_signature.html b/doc/one_pass_signature.html index 8171b98b..894eb651 100644 --- a/doc/one_pass_signature.html +++ b/doc/one_pass_signature.html @@ -35,6 +35,8 @@
+ +
Implementation of the One-Pass Signature Packets (Tag 4)

@@ -46,74 +48,6 @@ packet to be placed at the end of the message, so that the signer can compute the entire signed message in one pass.
-
-

new (require("packet/one_pass_signature"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -168,250 +102,23 @@ can compute the entire signed message in one pass.
- - - - - - -

Methods

+

Classes

- -
-

read(bytes) → {module:packet/one_pass_signature}

- - -
-
- - -
- parsing function for a one-pass signature packet (tag 4). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - payload of a tag 4 packet
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- object representation -
- - - -
-
- Type -
-
- -module:packet/one_pass_signature - - -
-
- - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- creates a string representation of a one-pass signature packet -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- a string representation of a one-pass signature packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- +
OnePassSignature
+
+ + + + + +
@@ -422,13 +129,13 @@ can compute the entire signed message in one pass.
diff --git a/doc/one_pass_signature.js.html b/doc/one_pass_signature.js.html index df75b80d..cd60f882 100644 --- a/doc/one_pass_signature.js.html +++ b/doc/one_pass_signature.js.html @@ -56,69 +56,71 @@ * @module packet/one_pass_signature */ +module.exports = OnePassSignature; + var enums = require('../enums.js'), type_keyid = require('../type/keyid.js'); /** * @constructor */ -module.exports = function one_pass_signature() { +function OnePassSignature() { this.version = null; // A one-octet version number. The current version is 3. this.type = null; // A one-octet signature type. Signature types are described in RFC4880 Section 5.2.1. this.hashAlgorithm = null; // A one-octet number describing the hash algorithm used. (See RFC4880 9.4) this.publicKeyAlgorithm = null; // A one-octet number describing the public-key algorithm used. (See RFC4880 9.1) this.signingKeyId = null; // An eight-octet number holding the Key ID of the signing key. this.flags = null; // A one-octet number holding a flag showing whether the signature is nested. A zero value indicates that the next packet is another One-Pass Signature packet that describes another signature to be applied to the same message data. +} - /** - * parsing function for a one-pass signature packet (tag 4). - * @param {String} bytes payload of a tag 4 packet - * @return {module:packet/one_pass_signature} object representation - */ - this.read = function (bytes) { - var mypos = 0; - // A one-octet version number. The current version is 3. - this.version = bytes.charCodeAt(mypos++); +/** + * parsing function for a one-pass signature packet (tag 4). + * @param {String} bytes payload of a tag 4 packet + * @return {module:packet/one_pass_signature} object representation + */ +OnePassSignature.prototype.read = function (bytes) { + var mypos = 0; + // A one-octet version number. The current version is 3. + this.version = bytes.charCodeAt(mypos++); - // A one-octet signature type. Signature types are described in - // Section 5.2.1. - this.type = enums.read(enums.signature, bytes.charCodeAt(mypos++)); + // A one-octet signature type. Signature types are described in + // Section 5.2.1. + this.type = enums.read(enums.signature, bytes.charCodeAt(mypos++)); - // A one-octet number describing the hash algorithm used. - this.hashAlgorithm = enums.read(enums.hash, bytes.charCodeAt(mypos++)); + // A one-octet number describing the hash algorithm used. + this.hashAlgorithm = enums.read(enums.hash, bytes.charCodeAt(mypos++)); - // A one-octet number describing the public-key algorithm used. - this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(mypos++)); + // A one-octet number describing the public-key algorithm used. + this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(mypos++)); - // An eight-octet number holding the Key ID of the signing key. - this.signingKeyId = new type_keyid(); - this.signingKeyId.read(bytes.substr(mypos)); - mypos += 8; + // An eight-octet number holding the Key ID of the signing key. + this.signingKeyId = new type_keyid(); + this.signingKeyId.read(bytes.substr(mypos)); + mypos += 8; - // A one-octet number holding a flag showing whether the signature - // is nested. A zero value indicates that the next packet is - // another One-Pass Signature packet that describes another - // signature to be applied to the same message data. - this.flags = bytes.charCodeAt(mypos++); - return this; - } + // A one-octet number holding a flag showing whether the signature + // is nested. A zero value indicates that the next packet is + // another One-Pass Signature packet that describes another + // signature to be applied to the same message data. + this.flags = bytes.charCodeAt(mypos++); + return this; +}; - /** - * creates a string representation of a one-pass signature packet - * @return {String} a string representation of a one-pass signature packet - */ - this.write = function () { - var result = ""; +/** + * creates a string representation of a one-pass signature packet + * @return {String} a string representation of a one-pass signature packet + */ +OnePassSignature.prototype.write = function () { + var result = ""; - result += String.fromCharCode(3); - result += String.fromCharCode(enums.write(enums.signature, this.type)); - result += String.fromCharCode(enums.write(enums.hash, this.hashAlgorithm)); - result += String.fromCharCode(enums.write(enums.publicKey, this.publicKeyAlgorithm)); - result += this.signingKeyId.write(); - result += String.fromCharCode(this.flags); + result += String.fromCharCode(3); + result += String.fromCharCode(enums.write(enums.signature, this.type)); + result += String.fromCharCode(enums.write(enums.hash, this.hashAlgorithm)); + result += String.fromCharCode(enums.write(enums.publicKey, this.publicKeyAlgorithm)); + result += this.signingKeyId.write(); + result += String.fromCharCode(this.flags); - return result; - } + return result; }; @@ -130,13 +132,13 @@ module.exports = function one_pass_signature() {
diff --git a/doc/openpgp.js.html b/doc/openpgp.js.html index 6a6462af..f2449430 100644 --- a/doc/openpgp.js.html +++ b/doc/openpgp.js.html @@ -185,7 +185,7 @@ function generateKeyPair(keyType, numBits, userId, passphrase) { exports.encryptMessage = encryptMessage; exports.signAndEncryptMessage = signAndEncryptMessage; exports.decryptMessage = decryptMessage; -exports.decryptAndVerifyMessage = decryptAndVerifyMessage +exports.decryptAndVerifyMessage = decryptAndVerifyMessage; exports.signClearMessage = signClearMessage; exports.verifyClearSignedMessage = verifyClearSignedMessage; exports.generateKeyPair = generateKeyPair; @@ -199,13 +199,13 @@ exports.generateKeyPair = generateKeyPair;
diff --git a/doc/packet.html b/doc/packet.html index 63fc7f2f..646a90c2 100644 --- a/doc/packet.html +++ b/doc/packet.html @@ -239,7 +239,7 @@
Source:
@@ -402,7 +402,7 @@ string
Source:
@@ -565,7 +565,7 @@ string
Source:
@@ -705,7 +705,7 @@ string
Source:
@@ -768,13 +768,13 @@ string
diff --git a/doc/packet.js.html b/doc/packet.js.html index fbce2eff..6cef4c28 100644 --- a/doc/packet.js.html +++ b/doc/packet.js.html @@ -51,7 +51,6 @@ var enums = require('../enums.js'), util = require('../util'); - module.exports = { readSimpleLength: function(bytes) { var len = 0, @@ -150,8 +149,8 @@ module.exports = { */ read: function(input, position, len) { // some sanity checks - if (input == null || input.length <= position || input.substring(position).length < 2 || (input.charCodeAt(position) & - 0x80) == 0) { + if (input === null || input.length <= position || input.substring(position).length < 2 || (input.charCodeAt(position) & + 0x80) === 0) { throw new Error("Error during parsing. This message / key is probably not containing a valid OpenPGP format."); } var mypos = position; @@ -160,7 +159,7 @@ module.exports = { var packet_length; format = 0; // 0 = old format; 1 = new format - if ((input.charCodeAt(mypos) & 0x40) != 0) { + if ((input.charCodeAt(mypos) & 0x40) !== 0) { format = 1; } @@ -234,27 +233,28 @@ module.exports = { // EEEK, we're reading the full data here... var mypos2 = mypos + packet_length; bodydata = input.substring(mypos, mypos + packet_length); + var tmplen; while (true) { if (input.charCodeAt(mypos2) < 192) { - var tmplen = input.charCodeAt(mypos2++); + tmplen = input.charCodeAt(mypos2++); packet_length += tmplen; bodydata += input.substring(mypos2, mypos2 + tmplen); mypos2 += tmplen; break; } else if (input.charCodeAt(mypos2) >= 192 && input.charCodeAt(mypos2) < 224) { - var tmplen = ((input.charCodeAt(mypos2++) - 192) << 8) + (input.charCodeAt(mypos2++)) + 192; + tmplen = ((input.charCodeAt(mypos2++) - 192) << 8) + (input.charCodeAt(mypos2++)) + 192; packet_length += tmplen; bodydata += input.substring(mypos2, mypos2 + tmplen); mypos2 += tmplen; break; } else if (input.charCodeAt(mypos2) > 223 && input.charCodeAt(mypos2) < 255) { - var tmplen = 1 << (input.charCodeAt(mypos2++) & 0x1F); + tmplen = 1 << (input.charCodeAt(mypos2++) & 0x1F); packet_length += tmplen; bodydata += input.substring(mypos2, mypos2 + tmplen); mypos2 += tmplen; } else { mypos2++; - var tmplen = (input.charCodeAt(mypos2++) << 24) | (input.charCodeAt(mypos2++) << 16) | (input[mypos2++] + tmplen = (input.charCodeAt(mypos2++) << 24) | (input.charCodeAt(mypos2++) << 16) | (input[mypos2++] .charCodeAt() << 8) | input.charCodeAt(mypos2++); bodydata += input.substring(mypos2, mypos2 + tmplen); packet_length += tmplen; @@ -277,7 +277,7 @@ module.exports = { real_packet_length = packet_length; } - if (bodydata == null) { + if (bodydata === null) { bodydata = input.substring(mypos, mypos + real_packet_length); } @@ -287,7 +287,7 @@ module.exports = { offset: mypos + real_packet_length }; } -} +}; @@ -298,13 +298,13 @@ module.exports = {
diff --git a/doc/packetlist-Packetlist.html b/doc/packetlist-Packetlist.html new file mode 100644 index 00000000..66ba0666 --- /dev/null +++ b/doc/packetlist-Packetlist.html @@ -0,0 +1,1051 @@ + + + + + JSDoc: Class: Packetlist + + + + + + + + + + +
+ +

Class: Packetlist

+ + + + + +
+ +
+

+ packet/packetlist~ + + Packetlist +

+ +
+ +
+
+ + + + +
+

new Packetlist()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

<readonly> length :Integer

+ + +
+
+ +
+ The number of packets contained within the list. +
+ + + +
Type:
+
    +
  • + +Integer + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

concat()

+ + +
+
+ + +
+ Concatenates packetlist or array of packets +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

filter()

+ + +
+
+ + +
+ Creates a new PacketList with all packets that pass the test implemented by the provided function. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

filterByTag()

+ + +
+
+ + +
+ Creates a new PacketList with all packets from the given types +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

findPacket(type) → {module:packet/packet|null}

+ + +
+
+ + +
+ Traverses packet tree and returns first matching packet +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +module:enums.packet + + + + The packet type
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +module:packet/packet +| + +null + + +
+
+ + + + +
+ + + +
+

forEach()

+ + +
+
+ + +
+ Executes the provided callback once for each element +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

indexOfTag()

+ + +
+
+ + +
+ Returns array of found indices by tag +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

push()

+ + +
+
+ + +
+ Adds a packet to the list. This is the only supported method of doing so; +writing to packetlist[i] directly will result in an error. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

read(A)

+ + +
+
+ + +
+ Reads a stream of binary data and interprents it as a list of packets. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
A + + +String + + + + binary string of bytes.
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

slice()

+ + +
+
+ + +
+ Returns slice of packetlist +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ Creates a binary representation of openpgp objects contained within the +class instance. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ A binary string of bytes containing valid openpgp packets. +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/packetlist.html b/doc/packetlist.html index 6ba2a3fb..bda441ba 100644 --- a/doc/packetlist.html +++ b/doc/packetlist.html @@ -35,80 +35,14 @@
+ +
This class represents a list of openpgp packets. Take care when iterating over it - the packets themselves are stored as numerical indices.
-
-

new (require("packet/packetlist"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -165,915 +99,19 @@ are stored as numerical indices.
- - - - -

Members

+

Classes

- -
-

<readonly> length :Integer

- - -
-
- -
- The number of packets contained within the list. -
- - - -
Type:
-
    -
  • - -Integer - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
Packetlist
+
-

Methods

- -
- -
-

concat()

-
-
-
- Concatenates packetlist or array of packets -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

filter()

- - -
-
- - -
- Creates a new packetList with all packets that pass the test implemented by the provided function. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

filterByTag()

- - -
-
- - -
- Creates a new packetList with all packets from the given types -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

findPacket(type) → {module:packet/packet|null}

- - -
-
- - -
- Traverses packet tree and returns first matching packet -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
type - - -module:enums.packet - - - - The packet type
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -module:packet/packet -| - -null - - -
-
- - - - -
- - - -
-

forEach()

- - -
-
- - -
- Executes the provided callback once for each element -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

indexOfTag()

- - -
-
- - -
- Returns array of found indices by tag -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

push()

- - -
-
- - -
- Adds a packet to the list. This is the only supported method of doing so; -writing to packetlist[i] directly will result in an error. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

read(A)

- - -
-
- - -
- Reads a stream of binary data and interprents it as a list of packets. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
A - - -String - - - - binary string of bytes.
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

slice()

- - -
-
- - -
- Returns slice of packetlist -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- Creates a binary representation of openpgp objects contained within the -class instance. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A binary string of bytes containing valid openpgp packets. -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -1088,13 +126,13 @@ class instance.
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:43 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/packetlist.js.html b/doc/packetlist.js.html index 55c8aaa0..b2a0c965 100644 --- a/doc/packetlist.js.html +++ b/doc/packetlist.js.html @@ -35,6 +35,8 @@ * @module packet/packetlist */ +module.exports = Packetlist; + var packetParser = require('./packet.js'), packets = require('./all_packets.js'), enums = require('../enums.js'); @@ -42,166 +44,164 @@ var packetParser = require('./packet.js'), /** * @constructor */ -module.exports = function packetlist() { +function Packetlist() { /** The number of packets contained within the list. * @readonly * @type {Integer} */ this.length = 0; - - /** - * Reads a stream of binary data and interprents it as a list of packets. - * @param {String} A binary string of bytes. - */ - this.read = function (bytes) { - var i = 0; - - while (i < bytes.length) { - var parsed = packetParser.read(bytes, i, bytes.length - i); - i = parsed.offset; - - var tag = enums.read(enums.packet, parsed.tag); - var packet = new packets[tag](); - - this.push(packet); - - packet.read(parsed.packet); - } - } - - /** - * Creates a binary representation of openpgp objects contained within the - * class instance. - * @returns {String} A binary string of bytes containing valid openpgp packets. - */ - this.write = function () { - var bytes = ''; - - for (var i = 0; i < this.length; i++) { - var packetbytes = this[i].write(); - bytes += packetParser.writeHeader(this[i].tag, packetbytes.length); - bytes += packetbytes; - } - - return bytes; - } - - /** - * Adds a packet to the list. This is the only supported method of doing so; - * writing to packetlist[i] directly will result in an error. - */ - this.push = function (packet) { - if (!packet) return; - - packet.packets = packet.packets || new packetlist(); - - this[this.length] = packet; - this.length++; - } - - /** - * Creates a new packetList with all packets that pass the test implemented by the provided function. - */ - this.filter = function (callback) { - - var filtered = new packetlist(); - - for (var i = 0; i < this.length; i++) { - if (callback(this[i], i, this)) { - filtered.push(this[i]); - } - } - - return filtered; - } - - /** - * Creates a new packetList with all packets from the given types - */ - this.filterByTag = function () { - var args = Array.prototype.slice.call(arguments); - var filtered = new packetlist(); - var that = this; - - for (var i = 0; i < this.length; i++) { - if (args.some(function(packetType) {return that[i].tag == packetType})) { - filtered.push(this[i]); - } - } - - return filtered; - } - - /** - * Executes the provided callback once for each element - */ - this.forEach = function (callback) { - for (var i = 0; i < this.length; i++) { - callback(this[i]); - } - } - - /** - * Traverses packet tree and returns first matching packet - * @param {module:enums.packet} type The packet type - * @return {module:packet/packet|null} - */ - this.findPacket = function (type) { - var packetlist = this.filterByTag(type); - if (packetlist.length) { - return packetlist[0]; - } else { - var found = null; - for (var i = 0; i < this.length; i++) { - if (this[i].packets.length) { - found = this[i].packets.findPacket(type); - if (found) return found; - } - } - } - return null; - } - - /** - * Returns array of found indices by tag - */ - this.indexOfTag = function () { - var args = Array.prototype.slice.call(arguments); - var tagIndex = []; - var that = this; - for (var i = 0; i < this.length; i++) { - if (args.some(function(packetType) {return that[i].tag == packetType})) { - tagIndex.push(i); - } - } - return tagIndex; - } - - /** - * Returns slice of packetlist - */ - this.slice = function (begin, end) { - if (!end) { - end = this.length - } - var part = new packetlist(); - for (var i = begin; i < end; i++) { - part.push(this[i]); - } - return part; - } - - /** - * Concatenates packetlist or array of packets - */ - this.concat = function (packetlist) { - if (packetlist) { - for (var i = 0; i < packetlist.length; i++) { - this.push(packetlist[i]); - } - } - } - } +/** + * Reads a stream of binary data and interprents it as a list of packets. + * @param {String} A binary string of bytes. + */ +Packetlist.prototype.read = function (bytes) { + var i = 0; + + while (i < bytes.length) { + var parsed = packetParser.read(bytes, i, bytes.length - i); + i = parsed.offset; + + var tag = enums.read(enums.packet, parsed.tag); + var packet = packets.newPacketFromTag(tag); + + this.push(packet); + + packet.read(parsed.packet); + } +}; + +/** + * Creates a binary representation of openpgp objects contained within the + * class instance. + * @returns {String} A binary string of bytes containing valid openpgp packets. + */ +Packetlist.prototype.write = function () { + var bytes = ''; + + for (var i = 0; i < this.length; i++) { + var packetbytes = this[i].write(); + bytes += packetParser.writeHeader(this[i].tag, packetbytes.length); + bytes += packetbytes; + } + + return bytes; +}; + +/** + * Adds a packet to the list. This is the only supported method of doing so; + * writing to packetlist[i] directly will result in an error. + */ +Packetlist.prototype.push = function (packet) { + if (!packet) return; + + packet.packets = packet.packets || new Packetlist(); + + this[this.length] = packet; + this.length++; +}; + +/** +* Creates a new PacketList with all packets that pass the test implemented by the provided function. +*/ +Packetlist.prototype.filter = function (callback) { + + var filtered = new Packetlist(); + + for (var i = 0; i < this.length; i++) { + if (callback(this[i], i, this)) { + filtered.push(this[i]); + } + } + + return filtered; +}; + +/** +* Creates a new PacketList with all packets from the given types +*/ +Packetlist.prototype.filterByTag = function () { + var args = Array.prototype.slice.call(arguments); + var filtered = new Packetlist(); + var that = this; + + for (var i = 0; i < this.length; i++) { + if (args.some(function(packetType) {return that[i].tag == packetType;})) { + filtered.push(this[i]); + } + } + + return filtered; +}; + +/** +* Executes the provided callback once for each element +*/ +Packetlist.prototype.forEach = function (callback) { + for (var i = 0; i < this.length; i++) { + callback(this[i]); + } +}; + +/** + * Traverses packet tree and returns first matching packet + * @param {module:enums.packet} type The packet type + * @return {module:packet/packet|null} + */ +Packetlist.prototype.findPacket = function (type) { + var packetlist = this.filterByTag(type); + if (packetlist.length) { + return packetlist[0]; + } else { + var found = null; + for (var i = 0; i < this.length; i++) { + if (this[i].packets.length) { + found = this[i].packets.findPacket(type); + if (found) return found; + } + } + } + return null; +}; + +/** + * Returns array of found indices by tag + */ +Packetlist.prototype.indexOfTag = function () { + var args = Array.prototype.slice.call(arguments); + var tagIndex = []; + var that = this; + for (var i = 0; i < this.length; i++) { + if (args.some(function(packetType) {return that[i].tag == packetType;})) { + tagIndex.push(i); + } + } + return tagIndex; +}; + +/** + * Returns slice of packetlist + */ +Packetlist.prototype.slice = function (begin, end) { + if (!end) { + end = this.length; + } + var part = new Packetlist(); + for (var i = begin; i < end; i++) { + part.push(this[i]); + } + return part; +}; + +/** + * Concatenates packetlist or array of packets + */ +Packetlist.prototype.concat = function (packetlist) { + if (packetlist) { + for (var i = 0; i < packetlist.length; i++) { + this.push(packetlist[i]); + } + } +};
@@ -212,13 +212,13 @@ module.exports = function packetlist() {
diff --git a/doc/pkcs1.html b/doc/pkcs1.html index 6b427486..289807a4 100644 --- a/doc/pkcs1.html +++ b/doc/pkcs1.html @@ -179,13 +179,13 @@
diff --git a/doc/pkcs1.js.html b/doc/pkcs1.js.html index 8a7734d2..5b35af67 100644 --- a/doc/pkcs1.js.html +++ b/doc/pkcs1.js.html @@ -55,7 +55,7 @@ /** * ASN1 object identifiers for hashes (See RFC4880 5.2.2) */ -hash_headers = new Array(); +hash_headers = []; hash_headers[1] = [0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 ]; @@ -110,10 +110,10 @@ module.exports = { decode: function(message, len) { if (message.length < len) message = String.fromCharCode(0) + message; - if (message.length < 12 || message.charCodeAt(0) != 0 || message.charCodeAt(1) != 2) + if (message.length < 12 || message.charCodeAt(0) !== 0 || message.charCodeAt(1) != 2) return -1; var i = 2; - while (message.charCodeAt(i) != 0 && message.length > i) + while (message.charCodeAt(i) !== 0 && message.length > i) i++; return message.substring(i + 1, message.length); } @@ -132,14 +132,15 @@ module.exports = { var data2 = ""; data2 += String.fromCharCode(0x00); data2 += String.fromCharCode(0x01); - for (var i = 0; i < (keylength - hash_headers[algo].length - 3 - + var i; + for (i = 0; i < (keylength - hash_headers[algo].length - 3 - hash.getHashByteLength(algo)); i++) data2 += String.fromCharCode(0xff); data2 += String.fromCharCode(0x00); - for (var i = 0; i < hash_headers[algo].length; i++) + for (i = 0; i < hash_headers[algo].length; i++) data2 += String.fromCharCode(hash_headers[algo][i]); data2 += hash.digest(algo, data); @@ -153,12 +154,12 @@ module.exports = { */ decode: function(algo, data) { var i = 0; - if (data.charCodeAt(0) == 0) i++; + if (data.charCodeAt(0) === 0) i++; else if (data.charCodeAt(0) != 1) return -1; else i++; while (data.charCodeAt(i) == 0xFF) i++; - if (data.charCodeAt(i++) != 0) return -1; + if (data.charCodeAt(i++) !== 0) return -1; var j = 0; for (j = 0; j < hash_headers[algo].length && j + i < data.length; j++) { if (data.charCodeAt(j + i) != hash_headers[algo][j]) return -1; @@ -168,7 +169,7 @@ module.exports = { return data.substring(i); } } -} +}; @@ -179,13 +180,13 @@ module.exports = {
diff --git a/doc/public_key-PublicKey.html b/doc/public_key-PublicKey.html new file mode 100644 index 00000000..45b26c2d --- /dev/null +++ b/doc/public_key-PublicKey.html @@ -0,0 +1,833 @@ + + + + + JSDoc: Class: PublicKey + + + + + + + + + + +
+ +

Class: PublicKey

+ + + + + +
+ +
+

+ packet/public_key~ + + PublicKey +

+ +
+ +
+
+ + + + +
+

new PublicKey()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

algorithm :module:enums.publicKey

+ + +
+
+ +
+ Public key algorithm +
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

created :Date

+ + +
+
+ +
+ Key creation date. +
+ + + +
Type:
+
    +
  • + +Date + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

mpi :module:type/mpi

+ + +
+
+ +
+ A list of multiprecision integers +
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

getFingerprint() → {String}

+ + +
+
+ + +
+ Calculates the fingerprint of the key +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ A string containing the fingerprint +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

getKeyId() → {String}

+ + +
+
+ + +
+ Calculates the key id of the key +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ A 8 byte key id +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

read(input) → {Object}

+ + +
+
+ + +
+ Internal Parser for public keys as specified in RFC 4880 section +5.5.2 Public-Key Packet Formats +called by read_tag<num> +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Input string to read the packet from
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ This object with attributes set by the parser +
+ + + +
+
+ Type +
+
+ +Object + + +
+
+ + + + +
+ + + +
+

write() → {Object}

+ + +
+
+ + +
+ Same as write_private_key, but has less information because of +public key. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ {body: [string]OpenPGP packet body contents, +header: [string] OpenPGP packet header, string: [string] header+body} +
+ + + +
+
+ Type +
+
+ +Object + + +
+
+ + + + +
+ + + +
+

writeOld()

+ + +
+
+ + +
+ Write an old version packet - it's used by some of the internal routines. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/public_key.html b/doc/public_key.html index 3a2d20cd..0082fb3d 100644 --- a/doc/public_key.html +++ b/doc/public_key.html @@ -290,13 +290,13 @@
diff --git a/doc/public_key.js.html b/doc/public_key.js.html index 7c71f987..e318a777 100644 --- a/doc/public_key.js.html +++ b/doc/public_key.js.html @@ -57,6 +57,8 @@ * @module packet/public_key */ +module.exports = PublicKey; + var util = require('../util'), type_mpi = require('../type/mpi.js'), type_keyid = require('../type/keyid.js'), @@ -66,7 +68,7 @@ var util = require('../util'), /** * @constructor */ -module.exports = function public_key() { +function PublicKey() { this.version = 4; /** Key creation date. * @type {Date} */ @@ -79,138 +81,137 @@ module.exports = function public_key() { this.algorithm = 'rsa_sign'; // time in days (V3 only) this.expirationTimeV3 = 0; +} +/** + * Internal Parser for public keys as specified in RFC 4880 section + * 5.5.2 Public-Key Packet Formats + * called by read_tag<num> + * @param {String} input Input string to read the packet from + * @return {Object} This object with attributes set by the parser + */ +PublicKey.prototype.read = function (bytes) { + var pos = 0; + // A one-octet version number (3 or 4). + this.version = bytes.charCodeAt(pos++); - /** - * Internal Parser for public keys as specified in RFC 4880 section - * 5.5.2 Public-Key Packet Formats - * called by read_tag<num> - * @param {String} input Input string to read the packet from - * @return {Object} This object with attributes set by the parser - */ - this.read = function (bytes) { - var pos = 0; - // A one-octet version number (3 or 4). - this.version = bytes.charCodeAt(pos++); + if (this.version == 3 || this.version == 4) { + // - A four-octet number denoting the time that the key was created. + this.created = util.readDate(bytes.substr(pos, 4)); + pos += 4; - if (this.version == 3 || this.version == 4) { - // - A four-octet number denoting the time that the key was created. - this.created = util.readDate(bytes.substr(pos, 4)); - pos += 4; - - if (this.version == 3) { - // - A two-octet number denoting the time in days that this key is - // valid. If this number is zero, then it does not expire. - this.expirationTimeV3 = util.readNumber(bytes.substr(pos, 2)); - pos += 2; - } - - // - A one-octet number denoting the public-key algorithm of this key. - this.algorithm = enums.read(enums.publicKey, bytes.charCodeAt(pos++)); - - var mpicount = crypto.getPublicMpiCount(this.algorithm); - this.mpi = []; - - var bmpi = bytes.substr(pos); - var p = 0; - - for (var i = 0; i < mpicount && p < bmpi.length; i++) { - - this.mpi[i] = new type_mpi(); - - p += this.mpi[i].read(bmpi.substr(p)) - - if (p > bmpi.length) { - throw new Error('Error reading MPI @:' + p); - } - } - - return p + 6; - } else { - throw new Error('Version ' + version + ' of the key packet is unsupported.'); - } - }; - - /** - * Alias of read() - * @function module:packet/public_key#readPublicKey - * @see module:packet/public_key#read - */ - this.readPublicKey = this.read; - - /** - * Same as write_private_key, but has less information because of - * public key. - * @return {Object} {body: [string]OpenPGP packet body contents, - * header: [string] OpenPGP packet header, string: [string] header+body} - */ - this.write = function () { - // Version - var result = String.fromCharCode(this.version); - result += util.writeDate(this.created); if (this.version == 3) { - result += util.writeNumber(this.expirationTimeV3, 2); + // - A two-octet number denoting the time in days that this key is + // valid. If this number is zero, then it does not expire. + this.expirationTimeV3 = util.readNumber(bytes.substr(pos, 2)); + pos += 2; } - result += String.fromCharCode(enums.write(enums.publicKey, this.algorithm)); + + // - A one-octet number denoting the public-key algorithm of this key. + this.algorithm = enums.read(enums.publicKey, bytes.charCodeAt(pos++)); var mpicount = crypto.getPublicMpiCount(this.algorithm); + this.mpi = []; - for (var i = 0; i < mpicount; i++) { - result += this.mpi[i].write(); - } + var bmpi = bytes.substr(pos); + var p = 0; - return result; - }; + for (var i = 0; i < mpicount && p < bmpi.length; i++) { - /** - * Alias of write() - * @function module:packet/public_key#writePublicKey - * @see module:packet/public_key#write - */ - this.writePublicKey = this.write; + this.mpi[i] = new type_mpi(); - /** - * Write an old version packet - it's used by some of the internal routines. - */ - this.writeOld = function () { - var bytes = this.writePublicKey(); + p += this.mpi[i].read(bmpi.substr(p)); - return String.fromCharCode(0x99) + - util.writeNumber(bytes.length, 2) + - bytes; - }; - - /** - * Calculates the key id of the key - * @return {String} A 8 byte key id - */ - this.getKeyId = function () { - var keyid = new type_keyid(); - if (this.version == 4) { - keyid.read(this.getFingerprint().substr(12, 8)); - } else if (this.version == 3) { - keyid.read(this.mpi[0].write().substr(-8)); - } - return keyid; - }; - - /** - * Calculates the fingerprint of the key - * @return {String} A string containing the fingerprint - */ - this.getFingerprint = function () { - var toHash = ''; - if (this.version == 4) { - toHash = this.writeOld(); - return crypto.hash.sha1(toHash); - } else if (this.version == 3) { - var mpicount = crypto.getPublicMpiCount(this.algorithm); - for (var i = 0; i < mpicount; i++) { - toHash += this.mpi[i].toBytes(); + if (p > bmpi.length) { + throw new Error('Error reading MPI @:' + p); } - return crypto.hash.md5(toHash) } - }; + + return p + 6; + } else { + throw new Error('Version ' + version + ' of the key packet is unsupported.'); + } +}; + +/** + * Alias of read() + * @function module:packet/public_key#readPublicKey + * @see module:packet/public_key#read + */ +PublicKey.prototype.readPublicKey = PublicKey.prototype.read; + +/** + * Same as write_private_key, but has less information because of + * public key. + * @return {Object} {body: [string]OpenPGP packet body contents, + * header: [string] OpenPGP packet header, string: [string] header+body} + */ +PublicKey.prototype.write = function () { + // Version + var result = String.fromCharCode(this.version); + result += util.writeDate(this.created); + if (this.version == 3) { + result += util.writeNumber(this.expirationTimeV3, 2); + } + result += String.fromCharCode(enums.write(enums.publicKey, this.algorithm)); + + var mpicount = crypto.getPublicMpiCount(this.algorithm); + + for (var i = 0; i < mpicount; i++) { + result += this.mpi[i].write(); + } + + return result; +}; + +/** + * Alias of write() + * @function module:packet/public_key#writePublicKey + * @see module:packet/public_key#write + */ +PublicKey.prototype.writePublicKey = PublicKey.prototype.write; + +/** + * Write an old version packet - it's used by some of the internal routines. + */ +PublicKey.prototype.writeOld = function () { + var bytes = this.writePublicKey(); + + return String.fromCharCode(0x99) + + util.writeNumber(bytes.length, 2) + + bytes; +}; + +/** + * Calculates the key id of the key + * @return {String} A 8 byte key id + */ +PublicKey.prototype.getKeyId = function () { + var keyid = new type_keyid(); + if (this.version == 4) { + keyid.read(this.getFingerprint().substr(12, 8)); + } else if (this.version == 3) { + keyid.read(this.mpi[0].write().substr(-8)); + } + return keyid; +}; + +/** + * Calculates the fingerprint of the key + * @return {String} A string containing the fingerprint + */ +PublicKey.prototype.getFingerprint = function () { + var toHash = ''; + if (this.version == 4) { + toHash = this.writeOld(); + return crypto.hash.sha1(toHash); + } else if (this.version == 3) { + var mpicount = crypto.getPublicMpiCount(this.algorithm); + for (var i = 0; i < mpicount; i++) { + toHash += this.mpi[i].toBytes(); + } + return crypto.hash.md5(toHash); + } }; @@ -222,13 +223,13 @@ module.exports = function public_key() {
diff --git a/doc/public_key_.html b/doc/public_key_.html index dd604703..c5b7eaa1 100644 --- a/doc/public_key_.html +++ b/doc/public_key_.html @@ -35,6 +35,8 @@
+ +
Implementation of the Key Material Packet (Tag 5,6,7,14)

@@ -44,74 +46,6 @@ private key. There are four variants of this packet type, and two major versions. Consequently, this section is complex.
-
-

new (require("packet/public_key"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -172,535 +106,23 @@ major versions. Consequently, this section is complex.
- - - - -

Members

+

Classes

- -
-

algorithm :module:enums.publicKey

- - -
-
- -
- Public key algorithm -
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

created :Date

- - -
-
- -
- Key creation date. -
- - - -
Type:
-
    -
  • - -Date - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

mpi :module:type/mpi

- - -
-
- -
- A list of multiprecision integers -
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
PublicKey
+
+ + + +

Methods

-
-

getFingerprint() → {String}

- - -
-
- - -
- Calculates the fingerprint of the key -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string containing the fingerprint -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getKeyId() → {String}

- - -
-
- - -
- Calculates the key id of the key -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A 8 byte key id -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input) → {Object}

- - -
-
- - -
- Internal Parser for public keys as specified in RFC 4880 section -5.5.2 Public-Key Packet Formats -called by read_tag<num> -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Input string to read the packet from
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- This object with attributes set by the parser -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - -

readPublicKey()

@@ -743,7 +165,7 @@ called by read_tag<num>
Source:
@@ -753,7 +175,7 @@ called by read_tag<num>
See:
@@ -773,168 +195,6 @@ called by read_tag<num> - - - - -
-

write() → {Object}

- - -
-
- - -
- Same as write_private_key, but has less information because of -public key. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- {body: [string]OpenPGP packet body contents, -header: [string] OpenPGP packet header, string: [string] header+body} -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - - -
-

writeOld()

- - -
-
- - -
- Write an old version packet - it's used by some of the internal routines. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - -
@@ -981,7 +241,7 @@ header: [string] OpenPGP packet header, string: [string] header+body}
Source:
@@ -991,7 +251,7 @@ header: [string] OpenPGP packet header, string: [string] header+body}
See:
@@ -1029,13 +289,13 @@ header: [string] OpenPGP packet header, string: [string] header+body}
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/public_key_encrypted_session_key-PublicKeyEncryptedSessionKey.html b/doc/public_key_encrypted_session_key-PublicKeyEncryptedSessionKey.html new file mode 100644 index 00000000..b8143456 --- /dev/null +++ b/doc/public_key_encrypted_session_key-PublicKeyEncryptedSessionKey.html @@ -0,0 +1,776 @@ + + + + + JSDoc: Class: PublicKeyEncryptedSessionKey + + + + + + + + + + +
+ +

Class: PublicKeyEncryptedSessionKey

+ + + + + +
+ +
+

+ packet/public_key_encrypted_session_key~ + + PublicKeyEncryptedSessionKey +

+ +
+ +
+
+ + + + +
+

new PublicKeyEncryptedSessionKey()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

encrypted :Array.<module:type/mpi>

+ + +
+
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

decrypt(key) → {String}

+ + +
+
+ + +
+ Decrypts the session key (only for public key encrypted session key +packets (tag 1) +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +module:packet/secret_key + + + + Private key with secMPIs unlocked
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The unencrypted session key +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

read(input, position, len) → {module:packet/public_key_encrypted_session_key}

+ + +
+
+ + +
+ Parsing function for a publickey encrypted session key packet (tag 1). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of a tag 1 packet
position + + +Integer + + + + Position to start reading from the input string
len + + +Integer + + + + Length of the packet or the remaining length of + input at position
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Object representation +
+ + + +
+
+ Type +
+
+ +module:packet/public_key_encrypted_session_key + + +
+
+ + + + +
+ + + +
+

write(publicKeyId, publicMPIs, pubalgo, symmalgo, sessionkey) → {String}

+ + +
+
+ + +
+ Create a string representation of a tag 1 packet +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
publicKeyId + + +String + + + + The public key id corresponding to publicMPIs key as string
publicMPIs + + +Array.<module:type/mpi> + + + + Multiprecision integer objects describing the public key
pubalgo + + +Integer + + + + The corresponding public key algorithm // See RFC4880 9.1
symmalgo + + +Integer + + + + The symmetric cipher algorithm used to encrypt the data + within an encrypteddatapacket or encryptedintegrity- + protecteddatapacket + following this packet //See RFC4880 9.2
sessionkey + + +String + + + + A string of randombytes representing the session key
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The string representation +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST) +
+ + + + + \ No newline at end of file diff --git a/doc/public_key_encrypted_session_key.html b/doc/public_key_encrypted_session_key.html index b5864e74..4acc8d27 100644 --- a/doc/public_key_encrypted_session_key.html +++ b/doc/public_key_encrypted_session_key.html @@ -35,6 +35,8 @@
+ +
Public-Key Encrypted Session Key Packets (Tag 1)

@@ -51,74 +53,6 @@ public key, decrypts the session key, and then uses the session key to decrypt the message.
-
-

new (require("packet/public_key_encrypted_session_key"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -179,640 +113,19 @@ decrypt the message.
- - - - -

Members

+

Classes

- -
-

encrypted :Array.<module:type/mpi>

- - -
-
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
PublicKeyEncryptedSessionKey
+
-

Methods

- -
- -
-

decrypt(key) → {String}

-
-
-
- Decrypts the session key (only for public key encrypted session key -packets (tag 1) -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
key - - -module:packet/secret_key - - - - Private key with secMPIs unlocked
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The unencrypted session key -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input, position, len) → {module:packet/public_key_encrypted_session_key}

- - -
-
- - -
- Parsing function for a publickey encrypted session key packet (tag 1). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of a tag 1 packet
position - - -Integer - - - - Position to start reading from the input string
len - - -Integer - - - - Length of the packet or the remaining length of - input at position
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Object representation -
- - - -
-
- Type -
-
- -module:packet/public_key_encrypted_session_key - - -
-
- - - - -
- - - -
-

write(publicKeyId, publicMPIs, pubalgo, symmalgo, sessionkey) → {String}

- - -
-
- - -
- Create a string representation of a tag 1 packet -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
publicKeyId - - -String - - - - The public key id corresponding to publicMPIs key as string
publicMPIs - - -Array.<module:type/mpi> - - - - Multiprecision integer objects describing the public key
pubalgo - - -Integer - - - - The corresponding public key algorithm // See RFC4880 9.1
symmalgo - - -Integer - - - - The symmetric cipher algorithm used to encrypt the data - within an encrypteddatapacket or encryptedintegrity- - protecteddatapacket - following this packet //See RFC4880 9.2
sessionkey - - -String - - - - A string of randombytes representing the session key
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The string representation -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -827,13 +140,13 @@ packets (tag 1)
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/public_key_encrypted_session_key.js.html b/doc/public_key_encrypted_session_key.js.html index 8f23c2da..68da00c6 100644 --- a/doc/public_key_encrypted_session_key.js.html +++ b/doc/public_key_encrypted_session_key.js.html @@ -64,6 +64,8 @@ * @module packet/public_key_encrypted_session_key */ +module.exports = PublicKeyEncryptedSessionKey; + var type_keyid = require('../type/keyid.js'), util = require('../util'), type_mpi = require('../type/mpi.js'), @@ -73,7 +75,7 @@ var type_keyid = require('../type/keyid.js'), /** * @constructor */ -module.exports = function public_key_encrypted_session_key() { +function PublicKeyEncryptedSessionKey() { this.version = 3; this.publicKeyId = new type_keyid(); @@ -84,128 +86,128 @@ module.exports = function public_key_encrypted_session_key() { /** @type {Array<module:type/mpi>} */ this.encrypted = []; +} - /** - * Parsing function for a publickey encrypted session key packet (tag 1). - * - * @param {String} input Payload of a tag 1 packet - * @param {Integer} position Position to start reading from the input string - * @param {Integer} len Length of the packet or the remaining length of - * input at position - * @return {module:packet/public_key_encrypted_session_key} Object representation - */ - this.read = function (bytes) { +/** + * Parsing function for a publickey encrypted session key packet (tag 1). + * + * @param {String} input Payload of a tag 1 packet + * @param {Integer} position Position to start reading from the input string + * @param {Integer} len Length of the packet or the remaining length of + * input at position + * @return {module:packet/public_key_encrypted_session_key} Object representation + */ +PublicKeyEncryptedSessionKey.prototype.read = function (bytes) { - this.version = bytes.charCodeAt(0); - this.publicKeyId.read(bytes.substr(1)); - this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(9)); + this.version = bytes.charCodeAt(0); + this.publicKeyId.read(bytes.substr(1)); + this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(9)); - var i = 10; + var i = 10; - var integerCount = (function(algo) { - switch (algo) { - case 'rsa_encrypt': - case 'rsa_encrypt_sign': - return 1; + var integerCount = (function(algo) { + switch (algo) { + case 'rsa_encrypt': + case 'rsa_encrypt_sign': + return 1; - case 'elgamal': - return 2; + case 'elgamal': + return 2; - default: - throw new Error("Invalid algorithm."); - } - })(this.publicKeyAlgorithm); - - this.encrypted = []; - - for (var j = 0; j < integerCount; j++) { - var mpi = new type_mpi(); - i += mpi.read(bytes.substr(i)); - this.encrypted.push(mpi); + default: + throw new Error("Invalid algorithm."); } - }; + })(this.publicKeyAlgorithm); - /** - * Create a string representation of a tag 1 packet - * - * @param {String} publicKeyId - * The public key id corresponding to publicMPIs key as string - * @param {Array<module:type/mpi>} publicMPIs - * Multiprecision integer objects describing the public key - * @param {Integer} pubalgo - * The corresponding public key algorithm // See RFC4880 9.1 - * @param {Integer} symmalgo - * The symmetric cipher algorithm used to encrypt the data - * within an encrypteddatapacket or encryptedintegrity- - * protecteddatapacket - * following this packet //See RFC4880 9.2 - * @param {String} sessionkey - * A string of randombytes representing the session key - * @return {String} The string representation - */ - this.write = function () { - - var result = String.fromCharCode(this.version); - result += this.publicKeyId.write(); - result += String.fromCharCode( - enums.write(enums.publicKey, this.publicKeyAlgorithm)); - - for (var i = 0; i < this.encrypted.length; i++) { - result += this.encrypted[i].write() - } - - return result; - }; - - this.encrypt = function (key) { - var data = String.fromCharCode( - enums.write(enums.symmetric, this.sessionKeyAlgorithm)); - - data += this.sessionKey; - var checksum = util.calc_checksum(this.sessionKey); - data += util.writeNumber(checksum, 2); + this.encrypted = []; + for (var j = 0; j < integerCount; j++) { var mpi = new type_mpi(); - mpi.fromBytes(crypto.pkcs1.eme.encode( - data, - key.mpi[0].byteLength())); + i += mpi.read(bytes.substr(i)); + this.encrypted.push(mpi); + } +}; - this.encrypted = crypto.publicKeyEncrypt( - this.publicKeyAlgorithm, - key.mpi, - mpi); - }; +/** + * Create a string representation of a tag 1 packet + * + * @param {String} publicKeyId + * The public key id corresponding to publicMPIs key as string + * @param {Array<module:type/mpi>} publicMPIs + * Multiprecision integer objects describing the public key + * @param {Integer} pubalgo + * The corresponding public key algorithm // See RFC4880 9.1 + * @param {Integer} symmalgo + * The symmetric cipher algorithm used to encrypt the data + * within an encrypteddatapacket or encryptedintegrity- + * protecteddatapacket + * following this packet //See RFC4880 9.2 + * @param {String} sessionkey + * A string of randombytes representing the session key + * @return {String} The string representation + */ +PublicKeyEncryptedSessionKey.prototype.write = function () { - /** - * Decrypts the session key (only for public key encrypted session key - * packets (tag 1) - * - * @param {module:packet/secret_key} key - * Private key with secMPIs unlocked - * @return {String} The unencrypted session key - */ - this.decrypt = function (key) { - var result = crypto.publicKeyDecrypt( - this.publicKeyAlgorithm, - key.mpi, - this.encrypted).toBytes(); + var result = String.fromCharCode(this.version); + result += this.publicKeyId.write(); + result += String.fromCharCode( + enums.write(enums.publicKey, this.publicKeyAlgorithm)); - var checksum = util.readNumber(result.substr(result.length - 2)); + for (var i = 0; i < this.encrypted.length; i++) { + result += this.encrypted[i].write(); + } - var decoded = crypto.pkcs1.eme.decode( - result, - key.mpi[0].byteLength()); + return result; +}; - var key = decoded.substring(1, decoded.length - 2); +PublicKeyEncryptedSessionKey.prototype.encrypt = function (key) { + var data = String.fromCharCode( + enums.write(enums.symmetric, this.sessionKeyAlgorithm)); - if (checksum != util.calc_checksum(key)) { - throw new Error('Checksum mismatch'); - } else { - this.sessionKey = key; - this.sessionKeyAlgorithm = - enums.read(enums.symmetric, decoded.charCodeAt(0)); - } - }; + data += this.sessionKey; + var checksum = util.calc_checksum(this.sessionKey); + data += util.writeNumber(checksum, 2); + + var mpi = new type_mpi(); + mpi.fromBytes(crypto.pkcs1.eme.encode( + data, + key.mpi[0].byteLength())); + + this.encrypted = crypto.publicKeyEncrypt( + this.publicKeyAlgorithm, + key.mpi, + mpi); +}; + +/** + * Decrypts the session key (only for public key encrypted session key + * packets (tag 1) + * + * @param {module:packet/secret_key} key + * Private key with secMPIs unlocked + * @return {String} The unencrypted session key + */ +PublicKeyEncryptedSessionKey.prototype.decrypt = function (key) { + var result = crypto.publicKeyDecrypt( + this.publicKeyAlgorithm, + key.mpi, + this.encrypted).toBytes(); + + var checksum = util.readNumber(result.substr(result.length - 2)); + + var decoded = crypto.pkcs1.eme.decode( + result, + key.mpi[0].byteLength()); + + key = decoded.substring(1, decoded.length - 2); + + if (checksum != util.calc_checksum(key)) { + throw new Error('Checksum mismatch'); + } else { + this.sessionKey = key; + this.sessionKeyAlgorithm = + enums.read(enums.symmetric, decoded.charCodeAt(0)); + } };
@@ -217,13 +219,13 @@ module.exports = function public_key_encrypted_session_key() {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/public_subkey-PublicSubkey.html b/doc/public_subkey-PublicSubkey.html new file mode 100644 index 00000000..56c22403 --- /dev/null +++ b/doc/public_subkey-PublicSubkey.html @@ -0,0 +1,320 @@ + + + + + JSDoc: Class: PublicSubkey + + + + + + + + + + +
+ +

Class: PublicSubkey

+ + + + + +
+ +
+

+ packet/public_subkey~ + + PublicSubkey +

+ +
+ +
+
+ + + + +
+

new PublicSubkey()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + +

Methods

+ +
+ +
+

readPublicKey()

+ + +
+
+ + +
+ Alias of read() +
+ + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+
    +
  • module:packet/public_key#read
  • +
+
+ + + +
+ + + + + + + + + + + + + +
+ + + +
+

writePublicKey()

+ + +
+
+ + +
+ Alias of write() +
+ + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+
    +
  • module:packet/public_key#write
  • +
+
+ + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST) +
+ + + + + \ No newline at end of file diff --git a/doc/public_subkey.html b/doc/public_subkey.html index 47434920..4c9795d7 100644 --- a/doc/public_subkey.html +++ b/doc/public_subkey.html @@ -35,72 +35,6 @@
- - -
-

new (require("packet/public_subkey"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - @@ -157,899 +91,19 @@ - - - - -

Members

+

Classes

- -
-

algorithm :module:enums.publicKey

- - -
-
- -
- Public key algorithm -
- - - -
Type:
- - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

created :Date

- - -
-
- -
- Key creation date. -
- - - -
Type:
-
    -
  • - -Date - - -
  • -
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

mpi :module:type/mpi

- - -
-
- -
- A list of multiprecision integers -
- - - -
Type:
- - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
PublicSubkey
+
-

Methods

- -
- -
-

getFingerprint() → {String}

-
-
-
- Calculates the fingerprint of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string containing the fingerprint -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getKeyId() → {String}

- - -
-
- - -
- Calculates the key id of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A 8 byte key id -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input) → {Object}

- - -
-
- - -
- Internal Parser for public keys as specified in RFC 4880 section -5.5.2 Public-Key Packet Formats -called by read_tag<num> -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Input string to read the packet from
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- This object with attributes set by the parser -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - - -
-

readPublicKey()

- - -
-
- - -
- Alias of read() -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - -
See:
-
- -
- - - -
- - - - - - - - - - - - - -
- - - -
-

write() → {Object}

- - -
-
- - -
- Same as write_private_key, but has less information because of -public key. -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- {body: [string]OpenPGP packet body contents, -header: [string] OpenPGP packet header, string: [string] header+body} -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - - -
-

writeOld()

- - -
-
- - -
- Write an old version packet - it's used by some of the internal routines. -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

writePublicKey()

- - -
-
- - -
- Alias of write() -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - -
See:
-
- -
- - - -
- - - - - - - - - - - - - -
- -
- @@ -1064,13 +118,13 @@ header: [string] OpenPGP packet header, string: [string] header+body}

- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/public_subkey.js.html b/doc/public_subkey.js.html index 95da9223..5722a340 100644 --- a/doc/public_subkey.js.html +++ b/doc/public_subkey.js.html @@ -47,15 +47,20 @@ * @module packet/public_subkey */ +module.exports = PublicSubkey; + var publicKey = require('./public_key.js'); /** * @constructor * @extends module:packet/public_key */ -module.exports = function public_subkey() { +function PublicSubkey() { publicKey.call(this); -} +}; + +PublicSubkey.prototype = new publicKey(); +PublicSubkey.prototype.constructor = PublicSubkey();
@@ -66,13 +71,13 @@ module.exports = function public_subkey() {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/random.html b/doc/random.html index c5543d9c..167737a2 100644 --- a/doc/random.html +++ b/doc/random.html @@ -717,13 +717,13 @@
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:41 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:09 GMT-0800 (PST)
diff --git a/doc/random.js.html b/doc/random.js.html index bbe2c829..a352888a 100644 --- a/doc/random.js.html +++ b/doc/random.js.html @@ -144,13 +144,13 @@ module.exports = {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/ripe-md.html b/doc/ripe-md.html index 40e1b351..fa61d092 100644 --- a/doc/ripe-md.html +++ b/doc/ripe-md.html @@ -105,13 +105,13 @@
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:41 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:08 GMT-0800 (PST)
diff --git a/doc/ripe-md.js.html b/doc/ripe-md.js.html index a9e5d17b..f4e8d93f 100644 --- a/doc/ripe-md.js.html +++ b/doc/ripe-md.js.html @@ -52,7 +52,7 @@ */ var RMDsize = 160; -var X = new Array(); +var X = []; function ROL(x, n) { return new Number((x << n) | (x >>> (32 - n))); @@ -125,7 +125,7 @@ function mixOneRound(a, b, c, d, e, x, s, roundNumber) { d &= 0xffffffff; e &= 0xffffffff; - var retBlock = new Array(); + var retBlock = []; retBlock[0] = a; retBlock[1] = b; retBlock[2] = c; @@ -172,19 +172,21 @@ var indexes = [ ]; function compress(MDbuf, X) { - blockA = new Array(); - blockB = new Array(); + blockA = []; + blockB = []; var retBlock; - for (var i = 0; i < 5; i++) { + var i, j; + + for (i = 0; i < 5; i++) { blockA[i] = new Number(MDbuf[i]); blockB[i] = new Number(MDbuf[i]); } var step = 0; - for (var j = 0; j < 5; j++) { - for (var i = 0; i < 16; i++) { + for (j = 0; j < 5; j++) { + for (i = 0; i < 16; i++) { retBlock = mixOneRound( blockA[(step + 0) % 5], blockA[(step + 1) % 5], @@ -206,8 +208,8 @@ function compress(MDbuf, X) { } step = 0; - for (var j = 5; j < 10; j++) { - for (var i = 0; i < 16; i++) { + for (j = 5; j < 10; j++) { + for (i = 0; i < 16; i++) { retBlock = mixOneRound( blockB[(step + 0) % 5], blockB[(step + 1) % 5], @@ -255,7 +257,7 @@ function MDfinish(MDbuf, strptr, lswlen, mswlen) { if ((lswlen & 63) > 55) { compress(MDbuf, X); - var X = new Array(16); + X = new Array(16); zeroX(X); } @@ -286,9 +288,9 @@ function RMD(message) { var X = new Array(16); zeroX(X); - var j = 0; - for (var nbytes = length; nbytes > 63; nbytes -= 64) { - for (var i = 0; i < 16; i++) { + var i, j = 0; + for (nbytes = length; nbytes > 63; nbytes -= 64) { + for (i = 0; i < 16; i++) { X[i] = BYTES_TO_DWORD(message.substr(j, 4)); j += 4; } @@ -297,7 +299,7 @@ function RMD(message) { MDfinish(MDbuf, message.substr(j), length, 0); - for (var i = 0; i < RMDsize / 8; i += 4) { + for (i = 0; i < RMDsize / 8; i += 4) { hashcode[i] = MDbuf[i >>> 2] & 255; hashcode[i + 1] = (MDbuf[i >>> 2] >>> 8) & 255; hashcode[i + 2] = (MDbuf[i >>> 2] >>> 16) & 255; @@ -330,13 +332,13 @@ module.exports = RMDstring;
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/rsa.html b/doc/rsa.html index e71cdc92..8aa90457 100644 --- a/doc/rsa.html +++ b/doc/rsa.html @@ -115,13 +115,13 @@
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:41 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:09 GMT-0800 (PST)
diff --git a/doc/rsa.js.html b/doc/rsa.js.html index 08b16e34..d0858ff0 100644 --- a/doc/rsa.js.html +++ b/doc/rsa.js.html @@ -85,7 +85,7 @@ function RSA() { util.print_debug("rsa.js decrypt\nxpn:" + util.hexstrdump(xp.toMPI()) + "\nxqn:" + util.hexstrdump(xq.toMPI())); var t = xq.subtract(xp); - if (t[0] == 0) { + if (t[0] === 0) { t = xp.subtract(xq); t = t.multiply(u).mod(q); t = q.subtract(t); @@ -140,12 +140,12 @@ function RSA() { for (;;) { for (;;) { key.p = new BigInteger(B - qs, 1, rng); - if (key.p.subtract(BigInteger.ONE).gcd(key.ee).compareTo(BigInteger.ONE) == 0 && key.p.isProbablePrime(10)) + if (key.p.subtract(BigInteger.ONE).gcd(key.ee).compareTo(BigInteger.ONE) === 0 && key.p.isProbablePrime(10)) break; } for (;;) { key.q = new BigInteger(qs, 1, rng); - if (key.q.subtract(BigInteger.ONE).gcd(key.ee).compareTo(BigInteger.ONE) == 0 && key.q.isProbablePrime(10)) + if (key.q.subtract(BigInteger.ONE).gcd(key.ee).compareTo(BigInteger.ONE) === 0 && key.q.isProbablePrime(10)) break; } if (key.p.compareTo(key.q) <= 0) { @@ -156,7 +156,7 @@ function RSA() { var p1 = key.p.subtract(BigInteger.ONE); var q1 = key.q.subtract(BigInteger.ONE); var phi = p1.multiply(q1); - if (phi.gcd(key.ee).compareTo(BigInteger.ONE) == 0) { + if (phi.gcd(key.ee).compareTo(BigInteger.ONE) === 0) { key.n = key.p.multiply(key.q); key.d = key.ee.modInverse(phi); key.dmp1 = key.d.mod(p1); @@ -187,13 +187,13 @@ module.exports = RSA;
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/s2k-S2k.html b/doc/s2k-S2k.html new file mode 100644 index 00000000..b8c79db5 --- /dev/null +++ b/doc/s2k-S2k.html @@ -0,0 +1,711 @@ + + + + + JSDoc: Class: S2k + + + + + + + + + + +
+ +

Class: S2k

+ + + + + +
+ +
+

+ type/s2k~ + + S2k +

+ +
+ +
+
+ + + + +
+

new S2k()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

algorithm :module:enums.hash

+ + +
+
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

salt :String

+ + +
+
+ +
+ Eight bytes of salt in a binary string. +
+ + + +
Type:
+
    +
  • + +String + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

type :module:enums.s2k

+ + +
+
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

produce_key(passphrase) → {String}

+ + +
+
+ + +
+ Produces a key using the specified passphrase and the defined +hashAlgorithm +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
passphrase + + +String + + + + Passphrase containing user input
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Produced key with a length corresponding to +hashAlgorithm hash length +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

read(input) → {Integer}

+ + +
+
+ + +
+ Parsing function for a string-to-key specifier (RFC 4880 3.7). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of string-to-key specifier
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Actual length of the object +
+ + + +
+
+ Type +
+
+ +Integer + + +
+
+ + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ writes an s2k hash based on the inputs. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Produced key of hashAlgorithm hash length +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST) +
+ + + + + \ No newline at end of file diff --git a/doc/s2k.html b/doc/s2k.html index dc10b2d5..c67351b0 100644 --- a/doc/s2k.html +++ b/doc/s2k.html @@ -35,6 +35,8 @@
+ +
Implementation of the String-to-key specifier (RFC4880 3.7)

@@ -45,74 +47,6 @@ private keyring, and to convert passphrases to encryption keys for symmetrically encrypted messages.
-
-

new (require("type/s2k"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -169,575 +103,19 @@ symmetrically encrypted messages.
- - - - -

Members

+

Classes

- -
-

algorithm :module:enums.hash

- - -
-
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

salt :String

- - -
-
- -
- Eight bytes of salt in a binary string. -
- - - -
Type:
-
    -
  • - -String - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

type :module:enums.s2k

- - -
-
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
S2k
+
-

Methods

- -
- -
-

produce_key(passphrase) → {String}

-
-
-
- Produces a key using the specified passphrase and the defined -hashAlgorithm -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
passphrase - - -String - - - - Passphrase containing user input
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Produced key with a length corresponding to -hashAlgorithm hash length -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input) → {Integer}

- - -
-
- - -
- Parsing function for a string-to-key specifier (RFC 4880 3.7). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of string-to-key specifier
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Actual length of the object -
- - - -
-
- Type -
-
- -Integer - - -
-
- - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- writes an s2k hash based on the inputs. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Produced key of hashAlgorithm hash length -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -752,13 +130,13 @@ hashAlgorithm hash length
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:45 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/s2k.js.html b/doc/s2k.js.html index a3117f95..abc9bb21 100644 --- a/doc/s2k.js.html +++ b/doc/s2k.js.html @@ -56,6 +56,8 @@ * @module type/s2k */ +module.exports = S2k; + var enums = require('../enums.js'), util = require('../util'), crypto = require('../crypto'); @@ -63,7 +65,7 @@ var enums = require('../enums.js'), /** * @constructor */ -module.exports = function s2k() { +function S2k() { /** @type {module:enums.hash} */ this.algorithm = 'sha256'; /** @type {module:enums.s2k} */ @@ -73,137 +75,135 @@ module.exports = function s2k() { * @type {String} */ this.salt = crypto.random.getRandomBytes(8); +} - +S2k.prototype.get_count = function () { // Exponent bias, defined in RFC4880 var expbias = 6; - this.get_count = function () { - return (16 + (this.c & 15)) << ((this.c >> 4) + expbias); - }; + return (16 + (this.c & 15)) << ((this.c >> 4) + expbias); +}; - /** - * Parsing function for a string-to-key specifier (RFC 4880 3.7). - * @param {String} input Payload of string-to-key specifier - * @return {Integer} Actual length of the object - */ - this.read = function (bytes) { - var i = 0; - this.type = enums.read(enums.s2k, bytes.charCodeAt(i++)); - this.algorithm = enums.read(enums.hash, bytes.charCodeAt(i++)); +/** + * Parsing function for a string-to-key specifier (RFC 4880 3.7). + * @param {String} input Payload of string-to-key specifier + * @return {Integer} Actual length of the object + */ +S2k.prototype.read = function (bytes) { + var i = 0; + this.type = enums.read(enums.s2k, bytes.charCodeAt(i++)); + this.algorithm = enums.read(enums.hash, bytes.charCodeAt(i++)); - switch (this.type) { - case 'simple': - break; + switch (this.type) { + case 'simple': + break; - case 'salted': - this.salt = bytes.substr(i, 8); - i += 8; - break; + case 'salted': + this.salt = bytes.substr(i, 8); + i += 8; + break; - case 'iterated': - this.salt = bytes.substr(i, 8); - i += 8; + case 'iterated': + this.salt = bytes.substr(i, 8); + i += 8; - // Octet 10: count, a one-octet, coded value - this.c = bytes.charCodeAt(i++); - break; + // Octet 10: count, a one-octet, coded value + this.c = bytes.charCodeAt(i++); + break; - case 'gnu': - if (bytes.substr(i, 3) == "GNU") { - i += 3; // GNU - var gnuExtType = 1000 + bytes.charCodeAt(i++); - if (gnuExtType == 1001) { - this.type = gnuExtType; - // GnuPG extension mode 1001 -- don't write secret key at all - } else { - throw new Error("Unknown s2k gnu protection mode."); - } + case 'gnu': + if (bytes.substr(i, 3) == "GNU") { + i += 3; // GNU + var gnuExtType = 1000 + bytes.charCodeAt(i++); + if (gnuExtType == 1001) { + this.type = gnuExtType; + // GnuPG extension mode 1001 -- don't write secret key at all } else { - throw new Error("Unknown s2k type."); + throw new Error("Unknown s2k gnu protection mode."); } - break; - - default: + } else { throw new Error("Unknown s2k type."); - break; - } - - return i; - }; - - - /** - * writes an s2k hash based on the inputs. - * @return {String} Produced key of hashAlgorithm hash length - */ - this.write = function () { - var bytes = String.fromCharCode(enums.write(enums.s2k, this.type)); - bytes += String.fromCharCode(enums.write(enums.hash, this.algorithm)); - - switch (this.type) { - case 'simple': - break; - case 'salted': - bytes += this.salt; - break; - case 'iterated': - bytes += this.salt; - bytes += String.fromCharCode(this.c); - break; - } - - return bytes; - }; - - /** - * Produces a key using the specified passphrase and the defined - * hashAlgorithm - * @param {String} passphrase Passphrase containing user input - * @return {String} Produced key with a length corresponding to - * hashAlgorithm hash length - */ - this.produce_key = function (passphrase, numBytes) { - passphrase = util.encode_utf8(passphrase); - - function round(prefix, s2k) { - var algorithm = enums.write(enums.hash, s2k.algorithm); - - switch (s2k.type) { - case 'simple': - return crypto.hash.digest(algorithm, prefix + passphrase); - - case 'salted': - return crypto.hash.digest(algorithm, - prefix + s2k.salt + passphrase); - - case 'iterated': - var isp = [], - count = s2k.get_count(); - data = s2k.salt + passphrase; - - while (isp.length * data.length < count) - isp.push(data); - - isp = isp.join(''); - - if (isp.length > count) - isp = isp.substr(0, count); - - return crypto.hash.digest(algorithm, prefix + isp); } + break; + + default: + throw new Error("Unknown s2k type."); + } + + return i; +}; + + +/** + * writes an s2k hash based on the inputs. + * @return {String} Produced key of hashAlgorithm hash length + */ +S2k.prototype.write = function () { + var bytes = String.fromCharCode(enums.write(enums.s2k, this.type)); + bytes += String.fromCharCode(enums.write(enums.hash, this.algorithm)); + + switch (this.type) { + case 'simple': + break; + case 'salted': + bytes += this.salt; + break; + case 'iterated': + bytes += this.salt; + bytes += String.fromCharCode(this.c); + break; + } + + return bytes; +}; + +/** + * Produces a key using the specified passphrase and the defined + * hashAlgorithm + * @param {String} passphrase Passphrase containing user input + * @return {String} Produced key with a length corresponding to + * hashAlgorithm hash length + */ +S2k.prototype.produce_key = function (passphrase, numBytes) { + passphrase = util.encode_utf8(passphrase); + + function round(prefix, s2k) { + var algorithm = enums.write(enums.hash, s2k.algorithm); + + switch (s2k.type) { + case 'simple': + return crypto.hash.digest(algorithm, prefix + passphrase); + + case 'salted': + return crypto.hash.digest(algorithm, + prefix + s2k.salt + passphrase); + + case 'iterated': + var isp = [], + count = s2k.get_count(); + data = s2k.salt + passphrase; + + while (isp.length * data.length < count) + isp.push(data); + + isp = isp.join(''); + + if (isp.length > count) + isp = isp.substr(0, count); + + return crypto.hash.digest(algorithm, prefix + isp); } + } - var result = '', - prefix = ''; + var result = '', + prefix = ''; - while (result.length <= numBytes) { - result += round(prefix, this); - prefix += String.fromCharCode(0); - } + while (result.length <= numBytes) { + result += round(prefix, this); + prefix += String.fromCharCode(0); + } - return result.substr(0, numBytes); - }; + return result.substr(0, numBytes); };
@@ -215,13 +215,13 @@ module.exports = function s2k() {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/secret_key.html b/doc/secret_key.html index 59857a24..92f28910 100644 --- a/doc/secret_key.html +++ b/doc/secret_key.html @@ -178,722 +178,12 @@ major versions. Consequently, this section is complex. -

Members

- -
- -
-

algorithm :module:enums.publicKey

- - -
-
- -
- Public key algorithm -
- - - -
Type:
- - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

created :Date

- - -
-
- -
- Key creation date. -
- - - -
Type:
-
    -
  • - -Date - - -
  • -
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- -
-

Methods

-
-

decrypt(str_passphrase) → {Boolean}

- - -
-
- - -
- Decrypts the private key MPIs which are needed to use the key. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
str_passphrase - - -String - - - - The passphrase for this private key -as string
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- True if the passphrase was correct or MPI already - decrypted; false if not -
- - - -
-
- Type -
-
- -Boolean - - -
-
- - - - -
- - - -
-

encrypt(passphrase)

- - -
-
- - -
- Encrypt the payload. By default, we use aes256 and iterated, salted string -to key specifier -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
passphrase - - -String - - - -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

getFingerprint() → {String}

- - -
-
- - -
- Calculates the fingerprint of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string containing the fingerprint -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getKeyId() → {String}

- - -
-
- - -
- Calculates the key id of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A 8 byte key id -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(bytes)

- - -
-
- - -
- Internal parser for private keys as specified in RFC 4880 section 5.5.3 -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - Input string to read the packet from
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - -

readPublicKey()

@@ -941,7 +231,7 @@ to key specifier
Source:
@@ -951,7 +241,7 @@ to key specifier
See:
@@ -971,171 +261,6 @@ to key specifier - - - - -
-

write() → {String}

- - -
-
- - -
- Creates an OpenPGP key packet for the given key. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string of bytes containing the secret key OpenPGP packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

writeOld()

- - -
-
- - -
- Write an old version packet - it's used by some of the internal routines. -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - -
@@ -1187,7 +312,7 @@ to key specifier
Source:
@@ -1197,7 +322,7 @@ to key specifier
See:
@@ -1235,13 +360,13 @@ to key specifier
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/secret_key.js.html b/doc/secret_key.js.html index 9dd5a367..0e0e924d 100644 --- a/doc/secret_key.js.html +++ b/doc/secret_key.js.html @@ -69,228 +69,229 @@ var publicKey = require('./public_key.js'), * @constructor * @extends module:packet/public_key */ -module.exports = function secret_key() { +module.exports = SecretKey; + +function SecretKey() { publicKey.call(this); // encrypted secret-key data this.encrypted = null; // indicator if secret-key data is available in decrypted form this.isDecrypted = false; - - - function get_hash_len(hash) { - if (hash == 'sha1') - return 20; - else - return 2; - } - - function get_hash_fn(hash) { - if (hash == 'sha1') - return crypto.hash.sha1; - else - return function(c) { - return util.writeNumber(util.calc_checksum(c), 2); - }; - } - - // Helper function - - function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) { - var hashlen = get_hash_len(hash_algorithm), - hashfn = get_hash_fn(hash_algorithm); - - var hashtext = cleartext.substr(cleartext.length - hashlen); - cleartext = cleartext.substr(0, cleartext.length - hashlen); - - var hash = hashfn(cleartext); - - if (hash != hashtext) - return new Error("Hash mismatch."); - - var mpis = crypto.getPrivateMpiCount(algorithm); - - var j = 0; - var mpi = []; - - for (var i = 0; i < mpis && j < cleartext.length; i++) { - mpi[i] = new type_mpi(); - j += mpi[i].read(cleartext.substr(j)); - } - - return mpi; - } - - function write_cleartext_mpi(hash_algorithm, algorithm, mpi) { - var bytes = ''; - var discard = crypto.getPublicMpiCount(algorithm); - - for (var i = discard; i < mpi.length; i++) { - bytes += mpi[i].write(); - } - - - bytes += get_hash_fn(hash_algorithm)(bytes); - - return bytes; - } - - - // 5.5.3. Secret-Key Packet Formats - - /** - * Internal parser for private keys as specified in RFC 4880 section 5.5.3 - * @param {String} bytes Input string to read the packet from - */ - this.read = function (bytes) { - // - A Public-Key or Public-Subkey packet, as described above. - var len = this.readPublicKey(bytes); - - bytes = bytes.substr(len); - - - // - One octet indicating string-to-key usage conventions. Zero - // indicates that the secret-key data is not encrypted. 255 or 254 - // indicates that a string-to-key specifier is being given. Any - // other value is a symmetric-key encryption algorithm identifier. - var isEncrypted = bytes.charCodeAt(0); - - if (isEncrypted) { - this.encrypted = bytes; - } else { - - // - Plain or encrypted multiprecision integers comprising the secret - // key data. These algorithm-specific fields are as described - // below. - var parsedMPI = parse_cleartext_mpi('mod', bytes.substr(1), this.algorithm); - if (parsedMPI instanceof Error) - throw parsedMPI; - this.mpi = this.mpi.concat(parsedMPI); - this.isDecrypted = true; - } - - }; - - /** Creates an OpenPGP key packet for the given key. - * @return {String} A string of bytes containing the secret key OpenPGP packet - */ - this.write = function () { - var bytes = this.writePublicKey(); - - if (!this.encrypted) { - bytes += String.fromCharCode(0); - - bytes += write_cleartext_mpi('mod', this.algorithm, this.mpi); - } else { - bytes += this.encrypted; - } - - return bytes; - }; - - - - - /** Encrypt the payload. By default, we use aes256 and iterated, salted string - * to key specifier - * @param {String} passphrase - */ - this.encrypt = function (passphrase) { - - var s2k = new type_s2k(), - symmetric = 'aes256', - cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi), - key = produceEncryptionKey(s2k, passphrase, symmetric), - blockLen = crypto.cipher[symmetric].blockSize, - iv = crypto.random.getRandomBytes(blockLen); - - - this.encrypted = ''; - this.encrypted += String.fromCharCode(254); - this.encrypted += String.fromCharCode(enums.write(enums.symmetric, symmetric)); - this.encrypted += s2k.write(); - this.encrypted += iv; - - this.encrypted += crypto.cfb.normalEncrypt(symmetric, key, cleartext, iv); - }; - - function produceEncryptionKey(s2k, passphrase, algorithm) { - return s2k.produce_key(passphrase, - crypto.cipher[algorithm].keySize); - } - - /** - * Decrypts the private key MPIs which are needed to use the key. - * @link module:packet/secret_key.isDecrypted should be - * false otherwise a call to this function is not needed - * - * @param {String} str_passphrase The passphrase for this private key - * as string - * @return {Boolean} True if the passphrase was correct or MPI already - * decrypted; false if not - */ - this.decrypt = function (passphrase) { - if (this.isDecrypted) - return true; - - var i = 0, - symmetric, - key; - - var s2k_usage = this.encrypted.charCodeAt(i++); - - // - [Optional] If string-to-key usage octet was 255 or 254, a one- - // octet symmetric encryption algorithm. - if (s2k_usage == 255 || s2k_usage == 254) { - symmetric = this.encrypted.charCodeAt(i++); - symmetric = enums.read(enums.symmetric, symmetric); - - // - [Optional] If string-to-key usage octet was 255 or 254, a - // string-to-key specifier. The length of the string-to-key - // specifier is implied by its type, as described above. - var s2k = new type_s2k(); - i += s2k.read(this.encrypted.substr(i)); - - key = produceEncryptionKey(s2k, passphrase, symmetric); - } else { - symmetric = s2k_usage; - symmetric = enums.read(enums.symmetric, symmetric); - key = crypto.hash.md5(passphrase); - } - - - // - [Optional] If secret data is encrypted (string-to-key usage octet - // not zero), an Initial Vector (IV) of the same length as the - // cipher's block size. - var iv = this.encrypted.substr(i, - crypto.cipher[symmetric].blockSize); - - i += iv.length; - - var cleartext, - ciphertext = this.encrypted.substr(i); - - cleartext = crypto.cfb.normalDecrypt(symmetric, key, ciphertext, iv); - - var hash = s2k_usage == 254 ? - 'sha1' : - 'mod'; - - var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm); - if (parsedMPI instanceof Error) - return false; - this.mpi = this.mpi.concat(parsedMPI); - this.isDecrypted = true; - return true; - }; - - this.generate = function (bits) { - this.mpi = crypto.generateMpi(this.algorithm, bits); - this.isDecrypted = true; - }; - } -module.exports.prototype = new publicKey(); +SecretKey.prototype = new publicKey(); +SecretKey.prototype.constructor = SecretKey(); + +function get_hash_len(hash) { + if (hash == 'sha1') + return 20; + else + return 2; +} + +function get_hash_fn(hash) { + if (hash == 'sha1') + return crypto.hash.sha1; + else + return function(c) { + return util.writeNumber(util.calc_checksum(c), 2); + }; +} + +// Helper function + +function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) { + var hashlen = get_hash_len(hash_algorithm), + hashfn = get_hash_fn(hash_algorithm); + + var hashtext = cleartext.substr(cleartext.length - hashlen); + cleartext = cleartext.substr(0, cleartext.length - hashlen); + + var hash = hashfn(cleartext); + + if (hash != hashtext) + return new Error("Hash mismatch."); + + var mpis = crypto.getPrivateMpiCount(algorithm); + + var j = 0; + var mpi = []; + + for (var i = 0; i < mpis && j < cleartext.length; i++) { + mpi[i] = new type_mpi(); + j += mpi[i].read(cleartext.substr(j)); + } + + return mpi; +} + +function write_cleartext_mpi(hash_algorithm, algorithm, mpi) { + var bytes = ''; + var discard = crypto.getPublicMpiCount(algorithm); + + for (var i = discard; i < mpi.length; i++) { + bytes += mpi[i].write(); + } + + + bytes += get_hash_fn(hash_algorithm)(bytes); + + return bytes; +} + + +// 5.5.3. Secret-Key Packet Formats + +/** + * Internal parser for private keys as specified in RFC 4880 section 5.5.3 + * @param {String} bytes Input string to read the packet from + */ +SecretKey.prototype.read = function (bytes) { + // - A Public-Key or Public-Subkey packet, as described above. + var len = this.readPublicKey(bytes); + + bytes = bytes.substr(len); + + + // - One octet indicating string-to-key usage conventions. Zero + // indicates that the secret-key data is not encrypted. 255 or 254 + // indicates that a string-to-key specifier is being given. Any + // other value is a symmetric-key encryption algorithm identifier. + var isEncrypted = bytes.charCodeAt(0); + + if (isEncrypted) { + this.encrypted = bytes; + } else { + + // - Plain or encrypted multiprecision integers comprising the secret + // key data. These algorithm-specific fields are as described + // below. + var parsedMPI = parse_cleartext_mpi('mod', bytes.substr(1), this.algorithm); + if (parsedMPI instanceof Error) + throw parsedMPI; + this.mpi = this.mpi.concat(parsedMPI); + this.isDecrypted = true; + } + +}; + +/** Creates an OpenPGP key packet for the given key. + * @return {String} A string of bytes containing the secret key OpenPGP packet + */ +SecretKey.prototype.write = function () { + var bytes = this.writePublicKey(); + + if (!this.encrypted) { + bytes += String.fromCharCode(0); + + bytes += write_cleartext_mpi('mod', this.algorithm, this.mpi); + } else { + bytes += this.encrypted; + } + + return bytes; +}; + + + + +/** Encrypt the payload. By default, we use aes256 and iterated, salted string + * to key specifier + * @param {String} passphrase + */ +SecretKey.prototype.encrypt = function (passphrase) { + + var s2k = new type_s2k(), + symmetric = 'aes256', + cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi), + key = produceEncryptionKey(s2k, passphrase, symmetric), + blockLen = crypto.cipher[symmetric].blockSize, + iv = crypto.random.getRandomBytes(blockLen); + + + this.encrypted = ''; + this.encrypted += String.fromCharCode(254); + this.encrypted += String.fromCharCode(enums.write(enums.symmetric, symmetric)); + this.encrypted += s2k.write(); + this.encrypted += iv; + + this.encrypted += crypto.cfb.normalEncrypt(symmetric, key, cleartext, iv); +}; + +function produceEncryptionKey(s2k, passphrase, algorithm) { + return s2k.produce_key(passphrase, + crypto.cipher[algorithm].keySize); +} + +/** + * Decrypts the private key MPIs which are needed to use the key. + * @link module:packet/secret_key.isDecrypted should be + * false otherwise a call to this function is not needed + * + * @param {String} str_passphrase The passphrase for this private key + * as string + * @return {Boolean} True if the passphrase was correct or MPI already + * decrypted; false if not + */ +SecretKey.prototype.decrypt = function (passphrase) { + if (this.isDecrypted) + return true; + + var i = 0, + symmetric, + key; + + var s2k_usage = this.encrypted.charCodeAt(i++); + + // - [Optional] If string-to-key usage octet was 255 or 254, a one- + // octet symmetric encryption algorithm. + if (s2k_usage == 255 || s2k_usage == 254) { + symmetric = this.encrypted.charCodeAt(i++); + symmetric = enums.read(enums.symmetric, symmetric); + + // - [Optional] If string-to-key usage octet was 255 or 254, a + // string-to-key specifier. The length of the string-to-key + // specifier is implied by its type, as described above. + var s2k = new type_s2k(); + i += s2k.read(this.encrypted.substr(i)); + + key = produceEncryptionKey(s2k, passphrase, symmetric); + } else { + symmetric = s2k_usage; + symmetric = enums.read(enums.symmetric, symmetric); + key = crypto.hash.md5(passphrase); + } + + + // - [Optional] If secret data is encrypted (string-to-key usage octet + // not zero), an Initial Vector (IV) of the same length as the + // cipher's block size. + var iv = this.encrypted.substr(i, + crypto.cipher[symmetric].blockSize); + + i += iv.length; + + var cleartext, + ciphertext = this.encrypted.substr(i); + + cleartext = crypto.cfb.normalDecrypt(symmetric, key, ciphertext, iv); + + var hash = s2k_usage == 254 ? + 'sha1' : + 'mod'; + + var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm); + if (parsedMPI instanceof Error) + return false; + this.mpi = this.mpi.concat(parsedMPI); + this.isDecrypted = true; + return true; +}; + +SecretKey.prototype.generate = function (bits) { + this.mpi = crypto.generateMpi(this.algorithm, bits); + this.isDecrypted = true; +};
@@ -301,13 +302,13 @@ module.exports.prototype = new publicKey();
diff --git a/doc/secret_subkey-SecretSubkey.html b/doc/secret_subkey-SecretSubkey.html new file mode 100644 index 00000000..37342f36 --- /dev/null +++ b/doc/secret_subkey-SecretSubkey.html @@ -0,0 +1,320 @@ + + + + + JSDoc: Class: SecretSubkey + + + + + + + + + + +
+ +

Class: SecretSubkey

+ + + + + +
+ +
+

+ packet/secret_subkey~ + + SecretSubkey +

+ +
+ +
+
+ + + + +
+

new SecretSubkey()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + +

Methods

+ +
+ +
+

readPublicKey()

+ + +
+
+ + +
+ Alias of read() +
+ + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+
    +
  • module:packet/public_key#read
  • +
+
+ + + +
+ + + + + + + + + + + + + +
+ + + +
+

writePublicKey()

+ + +
+
+ + +
+ Alias of write() +
+ + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+
    +
  • module:packet/public_key#write
  • +
+
+ + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/secret_subkey.html b/doc/secret_subkey.html index 4bbb38ec..c6a4c538 100644 --- a/doc/secret_subkey.html +++ b/doc/secret_subkey.html @@ -35,72 +35,6 @@
- - -
-

new (require("packet/secret_subkey"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - @@ -157,1073 +91,19 @@ - - - - -

Members

+

Classes

- -
-

algorithm :module:enums.publicKey

- - -
-
- -
- Public key algorithm -
- - - -
Type:
- - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

created :Date

- - -
-
- -
- Key creation date. -
- - - -
Type:
-
    -
  • - -Date - - -
  • -
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
SecretSubkey
+
-

Methods

- -
- -
-

decrypt(str_passphrase) → {Boolean}

-
-
-
- Decrypts the private key MPIs which are needed to use the key. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
str_passphrase - - -String - - - - The passphrase for this private key -as string
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- True if the passphrase was correct or MPI already - decrypted; false if not -
- - - -
-
- Type -
-
- -Boolean - - -
-
- - - - -
- - - -
-

encrypt(passphrase)

- - -
-
- - -
- Encrypt the payload. By default, we use aes256 and iterated, salted string -to key specifier -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
passphrase - - -String - - - -
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

getFingerprint() → {String}

- - -
-
- - -
- Calculates the fingerprint of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string containing the fingerprint -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

getKeyId() → {String}

- - -
-
- - -
- Calculates the key id of the key -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A 8 byte key id -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(bytes)

- - -
-
- - -
- Internal parser for private keys as specified in RFC 4880 section 5.5.3 -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - Input string to read the packet from
- - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

readPublicKey()

- - -
-
- - -
- Alias of read() -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - -
See:
-
- -
- - - -
- - - - - - - - - - - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- Creates an OpenPGP key packet for the given key. -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- A string of bytes containing the secret key OpenPGP packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

writeOld()

- - -
-
- - -
- Write an old version packet - it's used by some of the internal routines. -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

writePublicKey()

- - -
-
- - -
- Alias of write() -
- - - - - - - - - -
- - - - - - - -
Inherited From:
-
- - - - - - - - - - - - - -
Source:
-
- - - - - -
See:
-
- -
- - - -
- - - - - - - - - - - - - -
- -
- @@ -1238,13 +118,13 @@ to key specifier

- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:11 GMT-0800 (PST)
diff --git a/doc/secret_subkey.js.html b/doc/secret_subkey.js.html index ed2be443..2f9f0146 100644 --- a/doc/secret_subkey.js.html +++ b/doc/secret_subkey.js.html @@ -47,15 +47,20 @@ * @module packet/secret_subkey */ +module.exports = SecretSubkey; + var secretKey = require('./secret_key.js'); /** * @constructor * @extends module:packet/secret_key */ -module.exports = function secret_subkey() { +function SecretSubkey() { secretKey.call(this); } + +SecretSubkey.prototype = new secretKey(); +SecretSubkey.prototype.constructor = SecretSubkey;
@@ -66,13 +71,13 @@ module.exports = function secret_subkey() {
diff --git a/doc/sha.html b/doc/sha.html index 820eddea..d78343ec 100644 --- a/doc/sha.html +++ b/doc/sha.html @@ -454,13 +454,13 @@
diff --git a/doc/sha.js.html b/doc/sha.js.html index 1a78a0d6..da56128f 100644 --- a/doc/sha.js.html +++ b/doc/sha.js.html @@ -126,7 +126,7 @@ var jsSHA = (function() { * * @private * @param {Array} binarray Array of integers to be converted to hexidecimal - * representation + * representation * @return Hexidecimal representation of the parameter in String form */ binb2hex = function(binarray) { @@ -149,7 +149,7 @@ var jsSHA = (function() { * * @private * @param {Array} binarray Array of integers to be converted to base-64 - * representation + * representation * @return Base-64 encoded representation of the parameter in String form */ binb2b64 = function(binarray) { @@ -589,7 +589,7 @@ var jsSHA = (function() { * * @private * @param {Array} message The binary array representation of the string to - * hash + * hash * @param {Number} messageLen The number of bits in the message * @return The array of integers representing the SHA-1 hash of message */ @@ -942,7 +942,7 @@ var jsSHA = (function() { * using the specified parameters * * @param {String} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) + * SHA-256, SHA-384, or SHA-512) * @param {String} format The desired output formatting (B64 or HEX) * @return The string representation of the hash in the format specified */ @@ -1002,9 +1002,9 @@ var jsSHA = (function() { * @param {String} key The key used to calculate the HMAC * @param {String} inputFormat The format of key, ASCII or HEX * @param {String} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) + * SHA-256, SHA-384, or SHA-512) * @param {String} outputFormat The desired output formatting - * (B64 or HEX) + * (B64 or HEX) * @return The string representation of the hash in the format specified */ getHMAC: function(key, inputFormat, variant, outputFormat) { @@ -1149,7 +1149,7 @@ module.exports = { var shaObj = new jsSHA(str, "ASCII"); return shaObj.getHash("SHA-512", "ASCII"); } -} +}; @@ -1160,13 +1160,13 @@ module.exports = {
diff --git a/doc/signature-Signature.html b/doc/signature-Signature.html new file mode 100644 index 00000000..cb609bcb --- /dev/null +++ b/doc/signature-Signature.html @@ -0,0 +1,827 @@ + + + + + JSDoc: Class: Signature + + + + + + + + + + +
+ +

Class: Signature

+ + + + + +
+ +
+

+ packet/signature~ + + Signature +

+ +
+ +
+
+ + + + +
+

new Signature()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

isExpired() → {Boolean}

+ + +
+
+ + +
+ Verifies signature expiration date +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ true if expired +
+ + + +
+
+ Type +
+
+ +Boolean + + +
+
+ + + + +
+ + + +
+

read(bytes, position, len) → {module:packet/signature}

+ + +
+
+ + +
+ parsing function for a signature packet (tag 2). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
bytes + + +String + + + + payload of a tag 2 packet
position + + +Integer + + + + position to start reading from the bytes string
len + + +Integer + + + + length of the packet or the remaining length of bytes at position
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ object representation +
+ + + +
+
+ Type +
+
+ +module:packet/signature + + +
+
+ + + + +
+ + + +
+

sign(key, data)

+ + +
+
+ + +
+ Signs provided data. This needs to be done prior to serialization. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +module:packet/secret_key + + + + private key used to sign the message.
data + + +Object + + + + Contains packets to be signed.
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

verify(data, key) → {boolean}

+ + +
+
+ + +
+ verifys the signature packet. Note: not signature types are implemented +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
data + + +String +| + +Object + + + + data which on the signature applies
key + + +module:packet/public_subkey +| + +module:packet/public_key + + + + the public key to verify the signature
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ True if message is verified, else false. +
+ + + +
+
+ Type +
+
+ +boolean + + +
+
+ + + + +
+ + + +
+

write_all_sub_packets() → {String}

+ + +
+
+ + +
+ Creates string of bytes with all subpacket data +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ a string-representation of a all subpacket data +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/signature.html b/doc/signature.html index fd7e7186..65599a0b 100644 --- a/doc/signature.html +++ b/doc/signature.html @@ -575,13 +575,13 @@ integers which is used to sign the data
diff --git a/doc/signature.js.html b/doc/signature.js.html index d2f11bc7..fe3c4a51 100644 --- a/doc/signature.js.html +++ b/doc/signature.js.html @@ -47,6 +47,7 @@ module.exports = { */ verify: function(algo, hash_algo, msg_MPIs, publickey_MPIs, data) { var calc_hash = hashModule.digest(hash_algo, data); + var dopublic; switch (algo) { case 1: @@ -59,7 +60,7 @@ module.exports = { var n = publickey_MPIs[0].toBigInteger(); var e = publickey_MPIs[1].toBigInteger(); var x = msg_MPIs[0].toBigInteger(); - var dopublic = rsa.verify(x, e, n); + dopublic = rsa.verify(x, e, n); var hash = pkcs1.emsa.decode(hash_algo, dopublic.toMPI().substring(2)); if (hash == -1) { throw new Error('PKCS1 padding in message or key incorrect. Aborting...'); @@ -79,12 +80,11 @@ module.exports = { var g = publickey_MPIs[2].toBigInteger(); var y = publickey_MPIs[3].toBigInteger(); var m = data; - var dopublic = dsa.verify(hash_algo, s1, s2, m, p, q, g, y); - return dopublic.compareTo(s1) == 0; + dopublic = dsa.verify(hash_algo, s1, s2, m, p, q, g, y); + return dopublic.compareTo(s1) === 0; default: throw new Error('Invalid signature algorithm.'); } - }, /** @@ -100,6 +100,8 @@ module.exports = { */ sign: function(hash_algo, algo, keyIntegers, data) { + var m; + switch (algo) { case 1: // RSA (Encrypt or Sign) [HAC] @@ -110,7 +112,7 @@ module.exports = { var rsa = new publicKey.rsa(); var d = keyIntegers[2].toBigInteger(); var n = keyIntegers[0].toBigInteger(); - var m = pkcs1.emsa.encode(hash_algo, + m = pkcs1.emsa.encode(hash_algo, data, keyIntegers[0].byteLength()); return rsa.sign(m, d, n).toMPI(); @@ -124,7 +126,7 @@ module.exports = { var g = keyIntegers[2].toBigInteger(); var y = keyIntegers[3].toBigInteger(); var x = keyIntegers[4].toBigInteger(); - var m = data; + m = data; var result = dsa.sign(hash_algo, m, g, p, q, x); return result[0].toString() + result[1].toString(); @@ -135,7 +137,7 @@ module.exports = { throw new Error('Invalid signature algorithm.'); } } -} +}; @@ -146,13 +148,13 @@ module.exports = {
diff --git a/doc/signature.js_.html b/doc/signature.js_.html index f3a3d0ab..1a575373 100644 --- a/doc/signature.js_.html +++ b/doc/signature.js_.html @@ -58,6 +58,8 @@ * @module packet/signature */ +module.exports = Signature; + var util = require('../util'), packet = require('./packet.js'), enums = require('../enums.js'), @@ -68,7 +70,7 @@ var util = require('../util'), /** * @constructor */ -module.exports = function signature() { +function Signature() { this.version = 4; this.signatureType = null; @@ -112,559 +114,557 @@ module.exports = function signature() { this.embeddedSignature = null; this.verified = false; - - /** - * parsing function for a signature packet (tag 2). - * @param {String} bytes payload of a tag 2 packet - * @param {Integer} position position to start reading from the bytes string - * @param {Integer} len length of the packet or the remaining length of bytes at position - * @return {module:packet/signature} object representation - */ - this.read = function (bytes) { - var i = 0; - - this.version = bytes.charCodeAt(i++); - // switch on version (3 and 4) - switch (this.version) { - case 3: - // One-octet length of following hashed material. MUST be 5. - if (bytes.charCodeAt(i++) != 5) - util.print_debug("packet/signature.js\n" + - 'invalid One-octet length of following hashed material.' + - 'MUST be 5. @:' + (i - 1)); - - var sigpos = i; - // One-octet signature type. - this.signatureType = bytes.charCodeAt(i++); - - // Four-octet creation time. - this.created = util.readDate(bytes.substr(i, 4)); - i += 4; - - // storing data appended to data which gets verified - this.signatureData = bytes.substring(sigpos, i); - - // Eight-octet Key ID of signer. - this.issuerKeyId.read(bytes.substring(i, i + 8)); - i += 8; - - // One-octet public-key algorithm. - this.publicKeyAlgorithm = bytes.charCodeAt(i++); - - // One-octet hash algorithm. - this.hashAlgorithm = bytes.charCodeAt(i++); - break; - case 4: - this.signatureType = bytes.charCodeAt(i++); - this.publicKeyAlgorithm = bytes.charCodeAt(i++); - this.hashAlgorithm = bytes.charCodeAt(i++); - - function subpackets(bytes) { - // Two-octet scalar octet count for following subpacket data. - var subpacket_length = util.readNumber( - bytes.substr(0, 2)); - - var i = 2; - - // subpacket data set (zero or more subpackets) - var subpacked_read = 0; - while (i < 2 + subpacket_length) { - - var len = packet.readSimpleLength(bytes.substr(i)); - i += len.offset; - - this.read_sub_packet(bytes.substr(i, len.len)); - - i += len.len; - } - - return i; - } - - // hashed subpackets - i += subpackets.call(this, bytes.substr(i), true); - - // A V4 signature hashes the packet body - // starting from its first field, the version number, through the end - // of the hashed subpacket data. Thus, the fields hashed are the - // signature version, the signature type, the public-key algorithm, the - // hash algorithm, the hashed subpacket length, and the hashed - // subpacket body. - this.signatureData = bytes.substr(0, i); - - // unhashed subpackets - i += subpackets.call(this, bytes.substr(i), false); - - break; - default: - throw new Error('Version ' + version + ' of the signature is unsupported.'); - break; - } - - // Two-octet field holding left 16 bits of signed hash value. - this.signedHashValue = bytes.substr(i, 2); - i += 2; - - this.signature = bytes.substr(i); - }; - - this.write = function () { - return this.signatureData + - util.writeNumber(0, 2) + // Number of unsigned subpackets. - this.signedHashValue + - this.signature; - }; - - /** - * Signs provided data. This needs to be done prior to serialization. - * @param {module:packet/secret_key} key private key used to sign the message. - * @param {Object} data Contains packets to be signed. - */ - this.sign = function (key, data) { - var signatureType = enums.write(enums.signature, this.signatureType), - publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), - hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); - - var result = String.fromCharCode(4); - result += String.fromCharCode(signatureType); - result += String.fromCharCode(publicKeyAlgorithm); - result += String.fromCharCode(hashAlgorithm); - - this.issuerKeyId = key.getKeyId(); - - // Add hashed subpackets - result += this.write_all_sub_packets(); - - this.signatureData = result; - - var trailer = this.calculateTrailer(); - - var toHash = this.toSign(signatureType, data) + - this.signatureData + trailer; - - var hash = crypto.hash.digest(hashAlgorithm, toHash); - - this.signedHashValue = hash.substr(0, 2); - - this.signature = crypto.signature.sign(hashAlgorithm, - publicKeyAlgorithm, key.mpi, toHash); - }; - - /** - * Creates string of bytes with all subpacket data - * @return {String} a string-representation of a all subpacket data - */ - this.write_all_sub_packets = function () { - var sub = enums.signatureSubpacket; - var result = ''; - var bytes = ''; - if (this.created !== null) { - result += write_sub_packet(sub.signature_creation_time, util.writeDate(this.created)); - } - if (this.signatureExpirationTime !== null) { - result += write_sub_packet(sub.signature_expiration_time, util.writeNumber(this.signatureExpirationTime, 4)); - } - if (this.exportable !== null) { - result += write_sub_packet(sub.exportable_certification, String.fromCharCode(this.exportable ? 1 : 0)); - } - if (this.trustLevel !== null) { - bytes = String.fromCharCode(this.trustLevel) + String.fromCharCode(this.trustAmount); - result += write_sub_packet(sub.trust_signature, bytes); - } - if (this.regularExpression !== null) { - result += write_sub_packet(sub.regular_expression, this.regularExpression); - } - if (this.revocable !== null) { - result += write_sub_packet(sub.revocable, String.fromCharCode(this.revocable ? 1 : 0)); - } - if (this.keyExpirationTime !== null) { - result += write_sub_packet(sub.key_expiration_time, util.writeNumber(this.keyExpirationTime, 4)); - } - if (this.preferredSymmetricAlgorithms !== null) { - bytes = util.bin2str(this.preferredSymmetricAlgorithms); - result += write_sub_packet(sub.preferred_symmetric_algorithms, bytes); - } - if (this.revocationKeyClass !== null) { - bytes = String.fromCharCode(this.revocationKeyClass); - bytes += String.fromCharCode(this.revocationKeyAlgorithm); - bytes += this.revocationKeyFingerprint; - result += write_sub_packet(sub.revocation_key, bytes); - } - if (!this.issuerKeyId.isNull()) { - result += write_sub_packet(sub.issuer, this.issuerKeyId.write()); - } - if (this.notation !== null) { - for (var name in this.notation) { - if (this.notation.hasOwnProperty(name)) { - var value = this.notation[name]; - bytes = String.fromCharCode(0x80); - bytes += String.fromCharCode(0); - bytes += String.fromCharCode(0); - bytes += String.fromCharCode(0); - // 2 octets of name length - bytes += util.writeNumber(name.length, 2); - // 2 octets of value length - bytes += util.writeNumber(value.length, 2); - bytes += name + value; - result += write_sub_packet(sub.notation_data, bytes); - } - } - } - if (this.preferredHashAlgorithms !== null) { - bytes = util.bin2str(this.preferredHashAlgorithms); - result += write_sub_packet(sub.preferred_hash_algorithms, bytes); - } - if (this.preferredCompressionAlgorithms !== null) { - bytes = util.bin2str(this.preferredCompressionAlgorithms); - result += write_sub_packet(sub.preferred_hash_algorithms, bytes); - } - if (this.keyServerPreferences !== null) { - bytes = util.bin2str(this.keyServerPreferences); - result += write_sub_packet(sub.key_server_preferences, bytes); - } - if (this.preferredKeyServer !== null) { - result += write_sub_packet(sub.preferred_key_server, this.preferredKeyServer); - } - if (this.isPrimaryUserID !== null) { - result += write_sub_packet(sub.primary_user_id, String.fromCharCode(this.isPrimaryUserID ? 1 : 0)); - } - if (this.policyURI !== null) { - result += write_sub_packet(sub.policy_uri, this.policyURI); - } - if (this.keyFlags !== null) { - bytes = util.bin2str(this.keyFlags); - result += write_sub_packet(sub.key_flags, bytes); - } - if (this.signersUserId !== null) { - result += write_sub_packet(sub.signers_user_id, this.signersUserId); - } - if (this.reasonForRevocationFlag !== null) { - bytes = String.fromCharCode(this.reasonForRevocationFlag); - bytes += this.reasonForRevocationString; - result += write_sub_packet(sub.reason_for_revocation, bytes); - } - if (this.features !== null) { - bytes = util.bin2str(this.features); - result += write_sub_packet(sub.features, bytes); - } - if (this.signatureTargetPublicKeyAlgorithm !== null) { - bytes = String.fromCharCode(this.signatureTargetPublicKeyAlgorithm); - bytes += String.fromCharCode(this.signatureTargetHashAlgorithm); - bytes += this.signatureTargetHash; - result += write_sub_packet(sub.signature_target, bytes); - } - if (this.embeddedSignature !== null) { - result += write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()); - } - result = util.writeNumber(result.length, 2) + result; - return result; - }; - - /** - * creates a string representation of a sub signature packet (See RFC 4880 5.2.3.1) - * @param {Integer} type subpacket signature type. Signature types as described - * in RFC4880 Section 5.2.3.2 - * @param {String} data data to be included - * @return {String} a string-representation of a sub signature packet (See RFC 4880 5.2.3.1) - */ - function write_sub_packet(type, data) { - var result = ""; - result += packet.writeSimpleLength(data.length + 1); - result += String.fromCharCode(type); - result += data; - return result; - } - - // V4 signature sub packets - - this.read_sub_packet = function (bytes) { - var mypos = 0; - - function read_array(prop, bytes) { - this[prop] = []; - - for (var i = 0; i < bytes.length; i++) { - this[prop].push(bytes.charCodeAt(i)); - } - } - - // The leftwost bit denotes a "critical" packet, but we ignore it. - var type = bytes.charCodeAt(mypos++) & 0x7F; - - // subpacket type - switch (type) { - case 2: - // Signature Creation Time - this.created = util.readDate(bytes.substr(mypos)); - break; - case 3: - // Signature Expiration Time in seconds - var seconds = util.readNumber(bytes.substr(mypos)); - - this.signatureNeverExpires = seconds == 0; - this.signatureExpirationTime = seconds; - - break; - case 4: - // Exportable Certification - this.exportable = bytes.charCodeAt(mypos++) == 1; - break; - case 5: - // Trust Signature - this.trustLevel = bytes.charCodeAt(mypos++); - this.trustAmount = bytes.charCodeAt(mypos++); - break; - case 6: - // Regular Expression - this.regularExpression = bytes.substr(mypos); - break; - case 7: - // Revocable - this.revocable = bytes.charCodeAt(mypos++) == 1; - break; - case 9: - // Key Expiration Time in seconds - var seconds = util.readNumber(bytes.substr(mypos)); - - this.keyExpirationTime = seconds; - this.keyNeverExpires = seconds == 0; - - break; - case 11: - // Preferred Symmetric Algorithms - this.preferredSymmetricAlgorithms = []; - - while (mypos != bytes.length) { - this.preferredSymmetricAlgorithms.push(bytes.charCodeAt(mypos++)); - } - - break; - case 12: - // Revocation Key - // (1 octet of class, 1 octet of public-key algorithm ID, 20 - // octets of - // fingerprint) - this.revocationKeyClass = bytes.charCodeAt(mypos++); - this.revocationKeyAlgorithm = bytes.charCodeAt(mypos++); - this.revocationKeyFingerprint = bytes.substr(mypos, 20); - break; - - case 16: - // Issuer - this.issuerKeyId.read(bytes.substr(mypos)); - break; - - case 20: - // Notation Data - // We don't know how to handle anything but a text flagged data. - if (bytes.charCodeAt(mypos) == 0x80) { - - // We extract key/value tuple from the byte stream. - mypos += 4; - var m = util.readNumber(bytes.substr(mypos, 2)); - mypos += 2 - var n = util.readNumber(bytes.substr(mypos, 2)); - mypos += 2 - - var name = bytes.substr(mypos, m), - value = bytes.substr(mypos + m, n); - - this.notation = this.notation || {}; - this.notation[name] = value; - } else throw new Error("Unsupported notation flag."); - break; - case 21: - // Preferred Hash Algorithms - read_array.call(this, 'preferredHashAlgorithms', bytes.substr(mypos)); - break; - case 22: - // Preferred Compression Algorithms - read_array.call(this, 'preferredCompressionAlgorithms ', bytes.substr(mypos)); - break; - case 23: - // Key Server Preferences - read_array.call(this, 'keyServerPreferencess', bytes.substr(mypos)); - break; - case 24: - // Preferred Key Server - this.preferredKeyServer = bytes.substr(mypos); - break; - case 25: - // Primary User ID - this.isPrimaryUserID = bytes[mypos++] != 0; - break; - case 26: - // Policy URI - this.policyURI = bytes.substr(mypos); - break; - case 27: - // Key Flags - read_array.call(this, 'keyFlags', bytes.substr(mypos)); - break; - case 28: - // Signer's User ID - this.signersUserId += bytes.substr(mypos); - break; - case 29: - // Reason for Revocation - this.reasonForRevocationFlag = bytes.charCodeAt(mypos++); - this.reasonForRevocationString = bytes.substr(mypos); - break; - case 30: - // Features - read_array.call(this, 'features', bytes.substr(mypos)); - break; - case 31: - // Signature Target - // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash) - this.signatureTargetPublicKeyAlgorithm = bytes.charCodeAt(mypos++); - this.signatureTargetHashAlgorithm = bytes.charCodeAt(mypos++); - - var len = crypto.getHashByteLength(this.signatureTargetHashAlgorithm); - - this.signatureTargetHash = bytes.substr(mypos, len); - break; - case 32: - // Embedded Signature - this.embeddedSignature = new signature(); - this.embeddedSignature.read(bytes.substr(mypos)); - break; - default: - throw new Error("Unknown signature subpacket type " + type + " @:" + mypos); - break; - } - }; - - // Produces data to produce signature on - this.toSign = function (type, data) { - var t = enums.signature; - - switch (type) { - case t.binary: - case t.text: - return data.getBytes(); - - case t.standalone: - return ''; - - case t.cert_generic: - case t.cert_persona: - case t.cert_casual: - case t.cert_positive: - case t.cert_revocation: - var packet, tag; - - if (data.userid !== undefined) { - tag = 0xB4; - packet = data.userid; - } else if (data.userattribute !== undefined) { - tag = 0xD1; - packet = data.userattribute; - } else throw new Error('Either a userid or userattribute packet needs to be ' + - 'supplied for certification.'); - - var bytes = packet.write(); - - if (this.version == 4) { - return this.toSign(t.key, data) + - String.fromCharCode(tag) + - util.writeNumber(bytes.length, 4) + - bytes; - } else if (this.version == 3) { - return this.toSign(t.key, data) + - bytes; - } - break; - - case t.subkey_binding: - case t.key_binding: - return this.toSign(t.key, data) + this.toSign(t.key, { - key: data.bind - }); - - case t.key: - if (data.key == undefined) - throw new Error('Key packet is required for this sigtature.'); - - return data.key.writeOld(); - - case t.key_revocation: - case t.subkey_revocation: - return this.toSign(t.key, data); - case t.timestamp: - return ''; - case t.third_party: - throw new Error('Not implemented'); - break; - default: - throw new Error('Unknown signature type.') - } - } - - - this.calculateTrailer = function () { - // calculating the trailer - var trailer = ''; - // V3 signatures don't have a trailer - if (this.version == 3) return trailer; - trailer += String.fromCharCode(4); // Version - trailer += String.fromCharCode(0xFF); - trailer += util.writeNumber(this.signatureData.length, 4); - return trailer - } - - - /** - * verifys the signature packet. Note: not signature types are implemented - * @param {String|Object} data data which on the signature applies - * @param {module:packet/public_subkey|module:packet/public_key} key the public key to verify the signature - * @return {boolean} True if message is verified, else false. - */ - this.verify = function (key, data) { - var signatureType = enums.write(enums.signature, this.signatureType), - publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), - hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); - - var bytes = this.toSign(signatureType, data), - trailer = this.calculateTrailer(); - - - var mpicount = 0; - // Algorithm-Specific Fields for RSA signatures: - // - multiprecision number (MPI) of RSA signature value m**d mod n. - if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) - mpicount = 1; - // Algorithm-Specific Fields for DSA signatures: - // - MPI of DSA value r. - // - MPI of DSA value s. - else if (publicKeyAlgorithm == 17) - mpicount = 2; - - var mpi = [], - i = 0; - for (var j = 0; j < mpicount; j++) { - mpi[j] = new type_mpi(); - i += mpi[j].read(this.signature.substr(i)); - } - - this.verified = crypto.signature.verify(publicKeyAlgorithm, - hashAlgorithm, mpi, key.mpi, - bytes + this.signatureData + trailer); - - return this.verified; - } - - /** - * Verifies signature expiration date - * @return {Boolean} true if expired - */ - this.isExpired = function () { - if (!this.signatureNeverExpires) { - return Date.now() > (this.created.getTime() + this.signatureExpirationTime*1000); - } - return false; - } } + +/** + * parsing function for a signature packet (tag 2). + * @param {String} bytes payload of a tag 2 packet + * @param {Integer} position position to start reading from the bytes string + * @param {Integer} len length of the packet or the remaining length of bytes at position + * @return {module:packet/signature} object representation + */ +Signature.prototype.read = function (bytes) { + var i = 0; + + this.version = bytes.charCodeAt(i++); + // switch on version (3 and 4) + switch (this.version) { + case 3: + // One-octet length of following hashed material. MUST be 5. + if (bytes.charCodeAt(i++) != 5) + util.print_debug("packet/signature.js\n" + + 'invalid One-octet length of following hashed material.' + + 'MUST be 5. @:' + (i - 1)); + + var sigpos = i; + // One-octet signature type. + this.signatureType = bytes.charCodeAt(i++); + + // Four-octet creation time. + this.created = util.readDate(bytes.substr(i, 4)); + i += 4; + + // storing data appended to data which gets verified + this.signatureData = bytes.substring(sigpos, i); + + // Eight-octet Key ID of signer. + this.issuerKeyId.read(bytes.substring(i, i + 8)); + i += 8; + + // One-octet public-key algorithm. + this.publicKeyAlgorithm = bytes.charCodeAt(i++); + + // One-octet hash algorithm. + this.hashAlgorithm = bytes.charCodeAt(i++); + break; + case 4: + this.signatureType = bytes.charCodeAt(i++); + this.publicKeyAlgorithm = bytes.charCodeAt(i++); + this.hashAlgorithm = bytes.charCodeAt(i++); + + function subpackets(bytes) { + // Two-octet scalar octet count for following subpacket data. + var subpacket_length = util.readNumber( + bytes.substr(0, 2)); + + var i = 2; + + // subpacket data set (zero or more subpackets) + var subpacked_read = 0; + while (i < 2 + subpacket_length) { + + var len = packet.readSimpleLength(bytes.substr(i)); + i += len.offset; + + this.read_sub_packet(bytes.substr(i, len.len)); + + i += len.len; + } + + return i; + } + + // hashed subpackets + i += subpackets.call(this, bytes.substr(i), true); + + // A V4 signature hashes the packet body + // starting from its first field, the version number, through the end + // of the hashed subpacket data. Thus, the fields hashed are the + // signature version, the signature type, the public-key algorithm, the + // hash algorithm, the hashed subpacket length, and the hashed + // subpacket body. + this.signatureData = bytes.substr(0, i); + + // unhashed subpackets + i += subpackets.call(this, bytes.substr(i), false); + + break; + default: + throw new Error('Version ' + version + ' of the signature is unsupported.'); + } + + // Two-octet field holding left 16 bits of signed hash value. + this.signedHashValue = bytes.substr(i, 2); + i += 2; + + this.signature = bytes.substr(i); +}; + +Signature.prototype.write = function () { + return this.signatureData + + util.writeNumber(0, 2) + // Number of unsigned subpackets. + this.signedHashValue + + this.signature; +}; + +/** + * Signs provided data. This needs to be done prior to serialization. + * @param {module:packet/secret_key} key private key used to sign the message. + * @param {Object} data Contains packets to be signed. + */ +Signature.prototype.sign = function (key, data) { + var signatureType = enums.write(enums.signature, this.signatureType), + publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), + hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); + + var result = String.fromCharCode(4); + result += String.fromCharCode(signatureType); + result += String.fromCharCode(publicKeyAlgorithm); + result += String.fromCharCode(hashAlgorithm); + + this.issuerKeyId = key.getKeyId(); + + // Add hashed subpackets + result += this.write_all_sub_packets(); + + this.signatureData = result; + + var trailer = this.calculateTrailer(); + + var toHash = this.toSign(signatureType, data) + + this.signatureData + trailer; + + var hash = crypto.hash.digest(hashAlgorithm, toHash); + + this.signedHashValue = hash.substr(0, 2); + + this.signature = crypto.signature.sign(hashAlgorithm, + publicKeyAlgorithm, key.mpi, toHash); +}; + +/** + * Creates string of bytes with all subpacket data + * @return {String} a string-representation of a all subpacket data + */ +Signature.prototype.write_all_sub_packets = function () { + var sub = enums.signatureSubpacket; + var result = ''; + var bytes = ''; + if (this.created !== null) { + result += write_sub_packet(sub.signature_creation_time, util.writeDate(this.created)); + } + if (this.signatureExpirationTime !== null) { + result += write_sub_packet(sub.signature_expiration_time, util.writeNumber(this.signatureExpirationTime, 4)); + } + if (this.exportable !== null) { + result += write_sub_packet(sub.exportable_certification, String.fromCharCode(this.exportable ? 1 : 0)); + } + if (this.trustLevel !== null) { + bytes = String.fromCharCode(this.trustLevel) + String.fromCharCode(this.trustAmount); + result += write_sub_packet(sub.trust_signature, bytes); + } + if (this.regularExpression !== null) { + result += write_sub_packet(sub.regular_expression, this.regularExpression); + } + if (this.revocable !== null) { + result += write_sub_packet(sub.revocable, String.fromCharCode(this.revocable ? 1 : 0)); + } + if (this.keyExpirationTime !== null) { + result += write_sub_packet(sub.key_expiration_time, util.writeNumber(this.keyExpirationTime, 4)); + } + if (this.preferredSymmetricAlgorithms !== null) { + bytes = util.bin2str(this.preferredSymmetricAlgorithms); + result += write_sub_packet(sub.preferred_symmetric_algorithms, bytes); + } + if (this.revocationKeyClass !== null) { + bytes = String.fromCharCode(this.revocationKeyClass); + bytes += String.fromCharCode(this.revocationKeyAlgorithm); + bytes += this.revocationKeyFingerprint; + result += write_sub_packet(sub.revocation_key, bytes); + } + if (!this.issuerKeyId.isNull()) { + result += write_sub_packet(sub.issuer, this.issuerKeyId.write()); + } + if (this.notation !== null) { + for (var name in this.notation) { + if (this.notation.hasOwnProperty(name)) { + var value = this.notation[name]; + bytes = String.fromCharCode(0x80); + bytes += String.fromCharCode(0); + bytes += String.fromCharCode(0); + bytes += String.fromCharCode(0); + // 2 octets of name length + bytes += util.writeNumber(name.length, 2); + // 2 octets of value length + bytes += util.writeNumber(value.length, 2); + bytes += name + value; + result += write_sub_packet(sub.notation_data, bytes); + } + } + } + if (this.preferredHashAlgorithms !== null) { + bytes = util.bin2str(this.preferredHashAlgorithms); + result += write_sub_packet(sub.preferred_hash_algorithms, bytes); + } + if (this.preferredCompressionAlgorithms !== null) { + bytes = util.bin2str(this.preferredCompressionAlgorithms); + result += write_sub_packet(sub.preferred_hash_algorithms, bytes); + } + if (this.keyServerPreferences !== null) { + bytes = util.bin2str(this.keyServerPreferences); + result += write_sub_packet(sub.key_server_preferences, bytes); + } + if (this.preferredKeyServer !== null) { + result += write_sub_packet(sub.preferred_key_server, this.preferredKeyServer); + } + if (this.isPrimaryUserID !== null) { + result += write_sub_packet(sub.primary_user_id, String.fromCharCode(this.isPrimaryUserID ? 1 : 0)); + } + if (this.policyURI !== null) { + result += write_sub_packet(sub.policy_uri, this.policyURI); + } + if (this.keyFlags !== null) { + bytes = util.bin2str(this.keyFlags); + result += write_sub_packet(sub.key_flags, bytes); + } + if (this.signersUserId !== null) { + result += write_sub_packet(sub.signers_user_id, this.signersUserId); + } + if (this.reasonForRevocationFlag !== null) { + bytes = String.fromCharCode(this.reasonForRevocationFlag); + bytes += this.reasonForRevocationString; + result += write_sub_packet(sub.reason_for_revocation, bytes); + } + if (this.features !== null) { + bytes = util.bin2str(this.features); + result += write_sub_packet(sub.features, bytes); + } + if (this.signatureTargetPublicKeyAlgorithm !== null) { + bytes = String.fromCharCode(this.signatureTargetPublicKeyAlgorithm); + bytes += String.fromCharCode(this.signatureTargetHashAlgorithm); + bytes += this.signatureTargetHash; + result += write_sub_packet(sub.signature_target, bytes); + } + if (this.embeddedSignature !== null) { + result += write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()); + } + result = util.writeNumber(result.length, 2) + result; + return result; +}; + +/** + * creates a string representation of a sub signature packet (See RFC 4880 5.2.3.1) + * @param {Integer} type subpacket signature type. Signature types as described + * in RFC4880 Section 5.2.3.2 + * @param {String} data data to be included + * @return {String} a string-representation of a sub signature packet (See RFC 4880 5.2.3.1) + */ +function write_sub_packet(type, data) { + var result = ""; + result += packet.writeSimpleLength(data.length + 1); + result += String.fromCharCode(type); + result += data; + return result; +} + +// V4 signature sub packets + +Signature.prototype.read_sub_packet = function (bytes) { + var mypos = 0; + + function read_array(prop, bytes) { + this[prop] = []; + + for (var i = 0; i < bytes.length; i++) { + this[prop].push(bytes.charCodeAt(i)); + } + } + + // The leftwost bit denotes a "critical" packet, but we ignore it. + var type = bytes.charCodeAt(mypos++) & 0x7F; + var seconds; + + // subpacket type + switch (type) { + case 2: + // Signature Creation Time + this.created = util.readDate(bytes.substr(mypos)); + break; + case 3: + // Signature Expiration Time in seconds + seconds = util.readNumber(bytes.substr(mypos)); + + this.signatureNeverExpires = seconds === 0; + this.signatureExpirationTime = seconds; + + break; + case 4: + // Exportable Certification + this.exportable = bytes.charCodeAt(mypos++) == 1; + break; + case 5: + // Trust Signature + this.trustLevel = bytes.charCodeAt(mypos++); + this.trustAmount = bytes.charCodeAt(mypos++); + break; + case 6: + // Regular Expression + this.regularExpression = bytes.substr(mypos); + break; + case 7: + // Revocable + this.revocable = bytes.charCodeAt(mypos++) == 1; + break; + case 9: + // Key Expiration Time in seconds + seconds = util.readNumber(bytes.substr(mypos)); + + this.keyExpirationTime = seconds; + this.keyNeverExpires = seconds === 0; + + break; + case 11: + // Preferred Symmetric Algorithms + this.preferredSymmetricAlgorithms = []; + + while (mypos != bytes.length) { + this.preferredSymmetricAlgorithms.push(bytes.charCodeAt(mypos++)); + } + + break; + case 12: + // Revocation Key + // (1 octet of class, 1 octet of public-key algorithm ID, 20 + // octets of + // fingerprint) + this.revocationKeyClass = bytes.charCodeAt(mypos++); + this.revocationKeyAlgorithm = bytes.charCodeAt(mypos++); + this.revocationKeyFingerprint = bytes.substr(mypos, 20); + break; + + case 16: + // Issuer + this.issuerKeyId.read(bytes.substr(mypos)); + break; + + case 20: + // Notation Data + // We don't know how to handle anything but a text flagged data. + if (bytes.charCodeAt(mypos) == 0x80) { + + // We extract key/value tuple from the byte stream. + mypos += 4; + var m = util.readNumber(bytes.substr(mypos, 2)); + mypos += 2; + var n = util.readNumber(bytes.substr(mypos, 2)); + mypos += 2; + + var name = bytes.substr(mypos, m), + value = bytes.substr(mypos + m, n); + + this.notation = this.notation || {}; + this.notation[name] = value; + } else throw new Error("Unsupported notation flag."); + break; + case 21: + // Preferred Hash Algorithms + read_array.call(this, 'preferredHashAlgorithms', bytes.substr(mypos)); + break; + case 22: + // Preferred Compression Algorithms + read_array.call(this, 'preferredCompressionAlgorithms ', bytes.substr(mypos)); + break; + case 23: + // Key Server Preferences + read_array.call(this, 'keyServerPreferencess', bytes.substr(mypos)); + break; + case 24: + // Preferred Key Server + this.preferredKeyServer = bytes.substr(mypos); + break; + case 25: + // Primary User ID + this.isPrimaryUserID = bytes[mypos++] !== 0; + break; + case 26: + // Policy URI + this.policyURI = bytes.substr(mypos); + break; + case 27: + // Key Flags + read_array.call(this, 'keyFlags', bytes.substr(mypos)); + break; + case 28: + // Signer's User ID + this.signersUserId += bytes.substr(mypos); + break; + case 29: + // Reason for Revocation + this.reasonForRevocationFlag = bytes.charCodeAt(mypos++); + this.reasonForRevocationString = bytes.substr(mypos); + break; + case 30: + // Features + read_array.call(this, 'features', bytes.substr(mypos)); + break; + case 31: + // Signature Target + // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash) + this.signatureTargetPublicKeyAlgorithm = bytes.charCodeAt(mypos++); + this.signatureTargetHashAlgorithm = bytes.charCodeAt(mypos++); + + var len = crypto.getHashByteLength(this.signatureTargetHashAlgorithm); + + this.signatureTargetHash = bytes.substr(mypos, len); + break; + case 32: + // Embedded Signature + this.embeddedSignature = new Signature(); + this.embeddedSignature.read(bytes.substr(mypos)); + break; + default: + throw new Error("Unknown signature subpacket type " + type + " @:" + mypos); + } +}; + +// Produces data to produce signature on +Signature.prototype.toSign = function (type, data) { + var t = enums.signature; + + switch (type) { + case t.binary: + case t.text: + return data.getBytes(); + + case t.standalone: + return ''; + + case t.cert_generic: + case t.cert_persona: + case t.cert_casual: + case t.cert_positive: + case t.cert_revocation: + var packet, tag; + + if (data.userid !== undefined) { + tag = 0xB4; + packet = data.userid; + } else if (data.userattribute !== undefined) { + tag = 0xD1; + packet = data.userattribute; + } else throw new Error('Either a userid or userattribute packet needs to be ' + + 'supplied for certification.'); + + var bytes = packet.write(); + + if (this.version == 4) { + return this.toSign(t.key, data) + + String.fromCharCode(tag) + + util.writeNumber(bytes.length, 4) + + bytes; + } else if (this.version == 3) { + return this.toSign(t.key, data) + + bytes; + } + break; + + case t.subkey_binding: + case t.key_binding: + return this.toSign(t.key, data) + this.toSign(t.key, { + key: data.bind + }); + + case t.key: + if (data.key === undefined) + throw new Error('Key packet is required for this sigtature.'); + + return data.key.writeOld(); + + case t.key_revocation: + case t.subkey_revocation: + return this.toSign(t.key, data); + case t.timestamp: + return ''; + case t.third_party: + throw new Error('Not implemented'); + default: + throw new Error('Unknown signature type.'); + } +}; + + +Signature.prototype.calculateTrailer = function () { + // calculating the trailer + var trailer = ''; + // V3 signatures don't have a trailer + if (this.version == 3) return trailer; + trailer += String.fromCharCode(4); // Version + trailer += String.fromCharCode(0xFF); + trailer += util.writeNumber(this.signatureData.length, 4); + return trailer; +}; + + +/** + * verifys the signature packet. Note: not signature types are implemented + * @param {String|Object} data data which on the signature applies + * @param {module:packet/public_subkey|module:packet/public_key} key the public key to verify the signature + * @return {boolean} True if message is verified, else false. + */ +Signature.prototype.verify = function (key, data) { + var signatureType = enums.write(enums.signature, this.signatureType), + publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), + hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); + + var bytes = this.toSign(signatureType, data), + trailer = this.calculateTrailer(); + + + var mpicount = 0; + // Algorithm-Specific Fields for RSA signatures: + // - multiprecision number (MPI) of RSA signature value m**d mod n. + if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) + mpicount = 1; + // Algorithm-Specific Fields for DSA signatures: + // - MPI of DSA value r. + // - MPI of DSA value s. + else if (publicKeyAlgorithm == 17) + mpicount = 2; + + var mpi = [], + i = 0; + for (var j = 0; j < mpicount; j++) { + mpi[j] = new type_mpi(); + i += mpi[j].read(this.signature.substr(i)); + } + + this.verified = crypto.signature.verify(publicKeyAlgorithm, + hashAlgorithm, mpi, key.mpi, + bytes + this.signatureData + trailer); + + return this.verified; +}; + +/** + * Verifies signature expiration date + * @return {Boolean} true if expired + */ +Signature.prototype.isExpired = function () { + if (!this.signatureNeverExpires) { + return Date.now() > (this.created.getTime() + this.signatureExpirationTime*1000); + } + return false; +}; @@ -675,13 +675,13 @@ module.exports = function signature() {
diff --git a/doc/signature_.html b/doc/signature_.html index ddf712fe..56d41e0a 100644 --- a/doc/signature_.html +++ b/doc/signature_.html @@ -35,6 +35,8 @@
+ +
Implementation of the Signature Packet (Tag 2)

@@ -44,74 +46,6 @@ some data. The most common signatures are a signature of a file or a block of text, and a signature that is a certification of a User ID.
-
-

new (require("packet/signature"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -174,6 +108,13 @@ block of text, and a signature that is a certification of a User ID.
+

Classes

+ +
+
Signature
+
+
+ @@ -184,681 +125,6 @@ block of text, and a signature that is a certification of a User ID.
-
-

isExpired() → {Boolean}

- - -
-
- - -
- Verifies signature expiration date -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- true if expired -
- - - -
-
- Type -
-
- -Boolean - - -
-
- - - - -
- - - -
-

read(bytes, position, len) → {module:packet/signature}

- - -
-
- - -
- parsing function for a signature packet (tag 2). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
bytes - - -String - - - - payload of a tag 2 packet
position - - -Integer - - - - position to start reading from the bytes string
len - - -Integer - - - - length of the packet or the remaining length of bytes at position
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- object representation -
- - - -
-
- Type -
-
- -module:packet/signature - - -
-
- - - - -
- - - -
-

sign(key, data)

- - -
-
- - -
- Signs provided data. This needs to be done prior to serialization. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
key - - -module:packet/secret_key - - - - private key used to sign the message.
data - - -Object - - - - Contains packets to be signed.
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

verify(data, key) → {boolean}

- - -
-
- - -
- verifys the signature packet. Note: not signature types are implemented -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
data - - -String -| - -Object - - - - data which on the signature applies
key - - -module:packet/public_subkey -| - -module:packet/public_key - - - - the public key to verify the signature
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- True if message is verified, else false. -
- - - -
-
- Type -
-
- -boolean - - -
-
- - - - -
- - - -
-

write_all_sub_packets() → {String}

- - -
-
- - -
- Creates string of bytes with all subpacket data -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- a string-representation of a all subpacket data -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - -

<inner> write_sub_packet(type, data) → {String}

@@ -918,7 +184,7 @@ block of text, and a signature that is a certification of a User ID. - subpacket signature type. Signature types as described + subpacket signature type. Signature types as described in RFC4880 Section 5.2.3.2 @@ -973,7 +239,7 @@ in RFC4880 Section 5.2.3.2
Source:
@@ -1036,13 +302,13 @@ in RFC4880 Section 5.2.3.2
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/sym_encrypted_integrity_protected-SymEncryptedIntegrityProtected.html b/doc/sym_encrypted_integrity_protected-SymEncryptedIntegrityProtected.html new file mode 100644 index 00000000..4efa61e6 --- /dev/null +++ b/doc/sym_encrypted_integrity_protected-SymEncryptedIntegrityProtected.html @@ -0,0 +1,439 @@ + + + + + JSDoc: Class: SymEncryptedIntegrityProtected + + + + + + + + + + +
+ +

Class: SymEncryptedIntegrityProtected

+ + + + + +
+ +
+

+ packet/sym_encrypted_integrity_protected~ + + SymEncryptedIntegrityProtected +

+ +
+ +
+
+ + + + +
+

new SymEncryptedIntegrityProtected()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

encrypted

+ + +
+
+ +
+ The encrypted payload. +
+ + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ + + +
+

modification :Boolean

+ + +
+
+ +
+ If after decrypting the packet this is set to true, +a modification has been detected and thus the contents +should be discarded. +
+ + + +
Type:
+
    +
  • + +Boolean + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

decrypt(sessionKeyAlgorithm, key) → {String}

+ + +
+
+ + +
+ Decrypts the encrypted data contained in this object read_packet must +have been called before +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
sessionKeyAlgorithm + + +Integer + + + + The selected symmetric encryption algorithm to be used
key + + +String + + + + The key of cipher blocksize length to be used
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The decrypted data of this packet +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST) +
+ + + + + \ No newline at end of file diff --git a/doc/sym_encrypted_integrity_protected.html b/doc/sym_encrypted_integrity_protected.html index 80784815..8a3225d5 100644 --- a/doc/sym_encrypted_integrity_protected.html +++ b/doc/sym_encrypted_integrity_protected.html @@ -35,6 +35,8 @@
+ +
Implementation of the Sym. Encrypted Integrity Protected Data Packet (Tag 18)
@@ -46,74 +48,6 @@ encrypted data. It is used in combination with a Modification Detection Code packet.
-
-

new (require("packet/sym_encrypted_integrity_protected"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -168,304 +102,20 @@ packet.
- - - - -

Members

+

Classes

- -
-

encrypted

- - -
-
- -
- The encrypted payload. -
- - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- - - -
-

modification :Boolean

- - -
-
- -
- If after decrypting the packet this is set to true, -a modification has been detected and thus the contents -should be discarded. -
- - - -
Type:
-
    -
  • - -Boolean - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
SymEncryptedIntegrityProtected
+
-

Methods

- -
- -
-

decrypt(sessionKeyAlgorithm, key) → {String}

- - -
-
- - -
- Decrypts the encrypted data contained in this object read_packet must -have been called before -
- - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
sessionKeyAlgorithm - - -Integer - - - - The selected symmetric encryption algorithm to be used
key - - -String - - - - The key of cipher blocksize length to be used
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The decrypted data of this packet -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- - @@ -479,13 +129,13 @@ have been called before
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/sym_encrypted_integrity_protected.js.html b/doc/sym_encrypted_integrity_protected.js.html index a83f5c41..4852f9e3 100644 --- a/doc/sym_encrypted_integrity_protected.js.html +++ b/doc/sym_encrypted_integrity_protected.js.html @@ -56,13 +56,15 @@ * @module packet/sym_encrypted_integrity_protected */ +module.exports = SymEncryptedIntegrityProtected; + var util = require('../util'), crypto = require('../crypto'); /** * @constructor */ -module.exports = function sym_encrypted_integrity_protected() { +function SymEncryptedIntegrityProtected() { /** The encrypted payload. */ this.encrypted = null; // string /** @@ -73,78 +75,77 @@ module.exports = function sym_encrypted_integrity_protected() { */ this.modification = false; this.packets = null; +} + +SymEncryptedIntegrityProtected.prototype.read = function (bytes) { + // - A one-octet version number. The only currently defined value is 1. + var version = bytes.charCodeAt(0); + + if (version != 1) { + throw new Error('Invalid packet version.'); + } + + // - Encrypted data, the output of the selected symmetric-key cipher + // operating in Cipher Feedback mode with shift amount equal to the + // block size of the cipher (CFB-n where n is the block size). + this.encrypted = bytes.substr(1); +}; + +SymEncryptedIntegrityProtected.prototype.write = function () { + + // 1 = Version + return String.fromCharCode(1) + this.encrypted; +}; + +SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm, key) { + var bytes = this.packets.write(); + + var prefixrandom = crypto.getPrefixRandom(sessionKeyAlgorithm); + var prefix = prefixrandom + prefixrandom.charAt(prefixrandom.length - 2) + prefixrandom.charAt(prefixrandom.length - + 1); + + var tohash = bytes; - this.read = function (bytes) { - // - A one-octet version number. The only currently defined value is 1. - var version = bytes.charCodeAt(0); - - if (version != 1) { - throw new Error('Invalid packet version.'); - } - - // - Encrypted data, the output of the selected symmetric-key cipher - // operating in Cipher Feedback mode with shift amount equal to the - // block size of the cipher (CFB-n where n is the block size). - this.encrypted = bytes.substr(1); - }; - - this.write = function () { - - return String.fromCharCode(1) // Version - + this.encrypted; - }; - - this.encrypt = function (sessionKeyAlgorithm, key) { - var bytes = this.packets.write() - - var prefixrandom = crypto.getPrefixRandom(sessionKeyAlgorithm); - var prefix = prefixrandom + prefixrandom.charAt(prefixrandom.length - 2) + prefixrandom.charAt(prefixrandom.length - - 1) - - var tohash = bytes; + // Modification detection code packet. + tohash += String.fromCharCode(0xD3); + tohash += String.fromCharCode(0x14); - // Modification detection code packet. - tohash += String.fromCharCode(0xD3); - tohash += String.fromCharCode(0x14); + tohash += crypto.hash.sha1(prefix + tohash); - tohash += crypto.hash.sha1(prefix + tohash); + this.encrypted = crypto.cfb.encrypt(prefixrandom, + sessionKeyAlgorithm, tohash, key, false).substring(0, + prefix.length + tohash.length); +}; + +/** + * Decrypts the encrypted data contained in this object read_packet must + * have been called before + * + * @param {Integer} sessionKeyAlgorithm + * The selected symmetric encryption algorithm to be used + * @param {String} key The key of cipher blocksize length to be used + * @return {String} The decrypted data of this packet + */ +SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) { + var decrypted = crypto.cfb.decrypt( + sessionKeyAlgorithm, key, this.encrypted, false); - this.encrypted = crypto.cfb.encrypt(prefixrandom, - sessionKeyAlgorithm, tohash, key, false).substring(0, - prefix.length + tohash.length); - }; - - /** - * Decrypts the encrypted data contained in this object read_packet must - * have been called before - * - * @param {Integer} sessionKeyAlgorithm - * The selected symmetric encryption algorithm to be used - * @param {String} key The key of cipher blocksize length to be used - * @return {String} The decrypted data of this packet - */ - this.decrypt = function (sessionKeyAlgorithm, key) { - var decrypted = crypto.cfb.decrypt( - sessionKeyAlgorithm, key, this.encrypted, false); + // there must be a modification detection code packet as the + // last packet and everything gets hashed except the hash itself + this.hash = crypto.hash.sha1( + crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted) + decrypted.substring(0, decrypted.length - 20)); - // there must be a modification detection code packet as the - // last packet and everything gets hashed except the hash itself - this.hash = crypto.hash.sha1( - crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted) + decrypted.substring(0, decrypted.length - 20)); + var mdc = decrypted.substr(decrypted.length - 20, 20); - - var mdc = decrypted.substr(decrypted.length - 20, 20); - - if (this.hash != mdc) { - throw new Error('Modification detected.'); - } else - this.packets.read(decrypted.substr(0, decrypted.length - 22)); - }; + if (this.hash != mdc) { + throw new Error('Modification detected.'); + } else + this.packets.read(decrypted.substr(0, decrypted.length - 22)); };
@@ -156,13 +157,13 @@ module.exports = function sym_encrypted_integrity_protected() {
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:39 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
diff --git a/doc/sym_encrypted_session_key-SymEncryptedSessionKey.html b/doc/sym_encrypted_session_key-SymEncryptedSessionKey.html new file mode 100644 index 00000000..509873b3 --- /dev/null +++ b/doc/sym_encrypted_session_key-SymEncryptedSessionKey.html @@ -0,0 +1,430 @@ + + + + + JSDoc: Class: SymEncryptedSessionKey + + + + + + + + + + +
+ +

Class: SymEncryptedSessionKey

+ + + + + +
+ +
+

+ packet/sym_encrypted_session_key~ + + SymEncryptedSessionKey +

+ +
+ +
+
+ + + + +
+

new SymEncryptedSessionKey()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

decrypt() → {String}

+ + +
+
+ + +
+ Decrypts the session key (only for public key encrypted session key +packets (tag 1) +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ The unencrypted session key +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ + + +
+

read(input, position, len) → {module:packet/sym_encrypted_session_key}

+ + +
+
+ + +
+ Parsing function for a symmetric encrypted session key packet (tag 3). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + Payload of a tag 1 packet
position + + +Integer + + + + Position to start reading from the input string
len + + +Integer + + + + Length of the packet or the remaining length of + input at position
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ Object representation +
+ + + +
+
+ Type +
+
+ +module:packet/sym_encrypted_session_key + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST) +
+ + + + + \ No newline at end of file diff --git a/doc/sym_encrypted_session_key.html b/doc/sym_encrypted_session_key.html index 82399392..1dda29fd 100644 --- a/doc/sym_encrypted_session_key.html +++ b/doc/sym_encrypted_session_key.html @@ -35,6 +35,8 @@
+ +
Public-Key Encrypted Session Key Packets (Tag 1)

@@ -51,74 +53,6 @@ public key, decrypts the session key, and then uses the session key to decrypt the message.
-
-

new (require("packet/sym_encrypted_session_key"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -175,298 +109,23 @@ decrypt the message.
- - - - - - -

Methods

+

Classes

- -
-

decrypt() → {String}

- - -
-
- - -
- Decrypts the session key (only for public key encrypted session key -packets (tag 1) -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- The unencrypted session key -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- - - -
-

read(input, position, len) → {module:packet/sym_encrypted_session_key}

- - -
-
- - -
- Parsing function for a symmetric encrypted session key packet (tag 3). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - Payload of a tag 1 packet
position - - -Integer - - - - Position to start reading from the input string
len - - -Integer - - - - Length of the packet or the remaining length of - input at position
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- Object representation -
- - - -
-
- Type -
-
- -module:packet/sym_encrypted_session_key - - -
-
- - - - -
- +
SymEncryptedSessionKey
+
+ + + + + +
@@ -477,13 +136,13 @@ packets (tag 1)
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/sym_encrypted_session_key.js.html b/doc/sym_encrypted_session_key.js.html index 5fa2357c..54ab9e19 100644 --- a/doc/sym_encrypted_session_key.js.html +++ b/doc/sym_encrypted_session_key.js.html @@ -66,104 +66,106 @@ var type_s2k = require('../type/s2k.js'), enums = require('../enums.js'), crypto = require('../crypto'); +module.exports = SymEncryptedSessionKey; + /** * @constructor */ -module.exports = function sym_encrypted_session_key() { +function SymEncryptedSessionKey() { this.tag = 3; this.sessionKeyEncryptionAlgorithm = null; this.sessionKeyAlgorithm = 'aes256'; this.encrypted = null; this.s2k = new type_s2k(); +} - /** - * Parsing function for a symmetric encrypted session key packet (tag 3). - * - * @param {String} input Payload of a tag 1 packet - * @param {Integer} position Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/sym_encrypted_session_key} Object representation - */ - this.read = function(bytes) { - // A one-octet version number. The only currently defined version is 4. - this.version = bytes.charCodeAt(0); +/** + * Parsing function for a symmetric encrypted session key packet (tag 3). + * + * @param {String} input Payload of a tag 1 packet + * @param {Integer} position Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/sym_encrypted_session_key} Object representation + */ +SymEncryptedSessionKey.prototype.read = function(bytes) { + // A one-octet version number. The only currently defined version is 4. + this.version = bytes.charCodeAt(0); - // A one-octet number describing the symmetric algorithm used. - var algo = enums.read(enums.symmetric, bytes.charCodeAt(1)); + // A one-octet number describing the symmetric algorithm used. + var algo = enums.read(enums.symmetric, bytes.charCodeAt(1)); - // A string-to-key (S2K) specifier, length as defined above. - var s2klength = this.s2k.read(bytes.substr(2)); + // A string-to-key (S2K) specifier, length as defined above. + var s2klength = this.s2k.read(bytes.substr(2)); - // Optionally, the encrypted session key itself, which is decrypted - // with the string-to-key object. - var done = s2klength + 2; + // Optionally, the encrypted session key itself, which is decrypted + // with the string-to-key object. + var done = s2klength + 2; - if (done < bytes.length) { - this.encrypted = bytes.substr(done); - this.sessionKeyEncryptionAlgorithm = algo - } else - this.sessionKeyAlgorithm = algo; - }; + if (done < bytes.length) { + this.encrypted = bytes.substr(done); + this.sessionKeyEncryptionAlgorithm = algo; + } else + this.sessionKeyAlgorithm = algo; +}; - this.write = function() { - var algo = this.encrypted == null ? - this.sessionKeyAlgorithm : - this.sessionKeyEncryptionAlgorithm; +SymEncryptedSessionKey.prototype.write = function() { + var algo = this.encrypted === null ? + this.sessionKeyAlgorithm : + this.sessionKeyEncryptionAlgorithm; - var bytes = String.fromCharCode(this.version) + - String.fromCharCode(enums.write(enums.symmetric, algo)) + - this.s2k.write(); + var bytes = String.fromCharCode(this.version) + + String.fromCharCode(enums.write(enums.symmetric, algo)) + + this.s2k.write(); - if (this.encrypted != null) - bytes += this.encrypted; - return bytes; - }; + if (this.encrypted !== null) + bytes += this.encrypted; + return bytes; +}; - /** - * Decrypts the session key (only for public key encrypted session key - * packets (tag 1) - * - * @return {String} The unencrypted session key - */ - this.decrypt = function(passphrase) { - var algo = this.sessionKeyEncryptionAlgorithm != null ? - this.sessionKeyEncryptionAlgorithm : - this.sessionKeyAlgorithm; +/** + * Decrypts the session key (only for public key encrypted session key + * packets (tag 1) + * + * @return {String} The unencrypted session key + */ +SymEncryptedSessionKey.prototype.decrypt = function(passphrase) { + var algo = this.sessionKeyEncryptionAlgorithm !== null ? + this.sessionKeyEncryptionAlgorithm : + this.sessionKeyAlgorithm; - var length = crypto.cipher[algo].keySize; - var key = this.s2k.produce_key(passphrase, length); + var length = crypto.cipher[algo].keySize; + var key = this.s2k.produce_key(passphrase, length); - if (this.encrypted == null) { - this.sessionKey = key; + if (this.encrypted === null) { + this.sessionKey = key; - } else { - var decrypted = crypto.cfb.decrypt( - this.sessionKeyEncryptionAlgorithm, key, this.encrypted, true); + } else { + var decrypted = crypto.cfb.decrypt( + this.sessionKeyEncryptionAlgorithm, key, this.encrypted, true); - this.sessionKeyAlgorithm = enums.read(enums.symmetric, - decrypted[0].keyCodeAt()); + this.sessionKeyAlgorithm = enums.read(enums.symmetric, + decrypted[0].keyCodeAt()); - this.sessionKey = decrypted.substr(1); - } - }; + this.sessionKey = decrypted.substr(1); + } +}; - this.encrypt = function(passphrase) { - var length = crypto.getKeyLength(this.sessionKeyEncryptionAlgorithm); - var key = this.s2k.produce_key(passphrase, length); +SymEncryptedSessionKey.prototype.encrypt = function(passphrase) { + var length = crypto.getKeyLength(this.sessionKeyEncryptionAlgorithm); + var key = this.s2k.produce_key(passphrase, length); - var private_key = String.fromCharCode( - enums.write(enums.symmetric, this.sessionKeyAlgorithm)) + + var private_key = String.fromCharCode( + enums.write(enums.symmetric, this.sessionKeyAlgorithm)) + - crypto.getRandomBytes( - crypto.getKeyLength(this.sessionKeyAlgorithm)); + crypto.getRandomBytes( + crypto.getKeyLength(this.sessionKeyAlgorithm)); - this.encrypted = crypto.cfb.encrypt( - crypto.getPrefixRandom(this.sessionKeyEncryptionAlgorithm), - this.sessionKeyEncryptionAlgorithm, key, private_key, true); - }; + this.encrypted = crypto.cfb.encrypt( + crypto.getPrefixRandom(this.sessionKeyEncryptionAlgorithm), + this.sessionKeyEncryptionAlgorithm, key, private_key, true); };
@@ -175,13 +177,13 @@ module.exports = function sym_encrypted_session_key() {
diff --git a/doc/symmetrically_encrypted-SymmetricallyEncrypted.html b/doc/symmetrically_encrypted-SymmetricallyEncrypted.html new file mode 100644 index 00000000..a25a0d7c --- /dev/null +++ b/doc/symmetrically_encrypted-SymmetricallyEncrypted.html @@ -0,0 +1,361 @@ + + + + + JSDoc: Class: SymmetricallyEncrypted + + + + + + + + + + +
+ +

Class: SymmetricallyEncrypted

+ + + + + +
+ +
+

+ packet/symmetrically_encrypted~ + + SymmetricallyEncrypted +

+ +
+ +
+
+ + + + +
+

new SymmetricallyEncrypted()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

packets :module:packet/packetlist

+ + +
+
+ +
+ Decrypted packets contained within. +
+ + + +
Type:
+ + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

decrypt(sessionKeyAlgorithm, key)

+ + +
+
+ + +
+ Symmetrically decrypt the packet data +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
sessionKeyAlgorithm + + +Integer + + + + Symmetric key algorithm to use // See RFC4880 9.2
key + + +String + + + + Key as string with the corresponding length to the + algorithm
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/symmetrically_encrypted.html b/doc/symmetrically_encrypted.html index ad3006af..de061a78 100644 --- a/doc/symmetrically_encrypted.html +++ b/doc/symmetrically_encrypted.html @@ -35,6 +35,8 @@
+ +
Implementation of the Symmetrically Encrypted Data Packet (Tag 9)

@@ -45,74 +47,6 @@ theory other Symmetrically Encrypted Data packets or sequences of packets that form whole OpenPGP messages).
-
-

new (require("packet/symmetrically_encrypted"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -165,226 +99,20 @@ that form whole OpenPGP messages).
- - - - -

Members

+

Classes

- -
-

packets :module:packet/packetlist

- - -
-
- -
- Decrypted packets contained within. -
- - - -
Type:
- - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
SymmetricallyEncrypted
+
-

Methods

- -
- -
-

decrypt(sessionKeyAlgorithm, key)

- - -
-
- - -
- Symmetrically decrypt the packet data -
- - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
sessionKeyAlgorithm - - -Integer - - - - Symmetric key algorithm to use // See RFC4880 9.2
key - - -String - - - - Key as string with the corresponding length to the - algorithm
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- -
- - @@ -398,13 +126,13 @@ that form whole OpenPGP messages).
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/symmetrically_encrypted.js.html b/doc/symmetrically_encrypted.js.html index befbf8e1..b8b218b4 100644 --- a/doc/symmetrically_encrypted.js.html +++ b/doc/symmetrically_encrypted.js.html @@ -54,47 +54,49 @@ * @module packet/symmetrically_encrypted */ +module.exports = SymmetricallyEncrypted; + var crypto = require('../crypto'); /** * @constructor */ -module.exports = function symmetrically_encrypted() { +function SymmetricallyEncrypted() { this.encrypted = null; /** Decrypted packets contained within. * @type {module:packet/packetlist} */ this.packets = null; +} - this.read = function(bytes) { - this.encrypted = bytes; - }; +SymmetricallyEncrypted.prototype.read = function (bytes) { + this.encrypted = bytes; +}; - this.write = function() { - return this.encrypted; - }; +SymmetricallyEncrypted.prototype.write = function () { + return this.encrypted; +}; - /** - * Symmetrically decrypt the packet data - * - * @param {Integer} sessionKeyAlgorithm - * Symmetric key algorithm to use // See RFC4880 9.2 - * @param {String} key - * Key as string with the corresponding length to the - * algorithm - */ - this.decrypt = function(sessionKeyAlgorithm, key) { - var decrypted = crypto.cfb.decrypt( - sessionKeyAlgorithm, key, this.encrypted, true); +/** + * Symmetrically decrypt the packet data + * + * @param {Integer} sessionKeyAlgorithm + * Symmetric key algorithm to use // See RFC4880 9.2 + * @param {String} key + * Key as string with the corresponding length to the + * algorithm + */ +SymmetricallyEncrypted.prototype.decrypt = function (sessionKeyAlgorithm, key) { + var decrypted = crypto.cfb.decrypt( + sessionKeyAlgorithm, key, this.encrypted, true); - this.packets.read(decrypted); - }; + this.packets.read(decrypted); +}; - this.encrypt = function(algo, key) { - var data = this.packets.write(); +SymmetricallyEncrypted.prototype.encrypt = function (algo, key) { + var data = this.packets.write(); - this.encrypted = crypto.cfb.encrypt( - crypto.getPrefixRandom(algo), algo, data, key, true); - }; + this.encrypted = crypto.cfb.encrypt( + crypto.getPrefixRandom(algo), algo, data, key, true); };
@@ -106,13 +108,13 @@ module.exports = function symmetrically_encrypted() {
diff --git a/doc/trust-Trust.html b/doc/trust-Trust.html new file mode 100644 index 00000000..6396a902 --- /dev/null +++ b/doc/trust-Trust.html @@ -0,0 +1,148 @@ + + + + + JSDoc: Class: Trust + + + + + + + + + + +
+ +

Class: Trust

+ + + + + +
+ +
+

+ packet/trust~ + + Trust +

+ +
+ +
+
+ + + + +
+

new Trust()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/trust.html b/doc/trust.html index 4ab78f4c..94d672ac 100644 --- a/doc/trust.html +++ b/doc/trust.html @@ -35,72 +35,6 @@
- - -
-

new (require("packet/trust"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - @@ -151,6 +85,13 @@ +

Classes

+ +
+
Trust
+
+
+ @@ -171,13 +112,13 @@

- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/trust.js.html b/doc/trust.js.html index aa796bc7..29582b84 100644 --- a/doc/trust.js.html +++ b/doc/trust.js.html @@ -29,11 +29,12 @@ * @module packet/trust */ +module.exports = Trust; + /** * @constructor */ -module.exports = function trust() { - +function Trust() { };
@@ -45,13 +46,13 @@ module.exports = function trust() {
diff --git a/doc/twofish.html b/doc/twofish.html index a13b1507..1b496f80 100644 --- a/doc/twofish.html +++ b/doc/twofish.html @@ -105,13 +105,13 @@
diff --git a/doc/twofish.js.html b/doc/twofish.js.html index a4354261..c056e897 100644 --- a/doc/twofish.js.html +++ b/doc/twofish.js.html @@ -400,7 +400,7 @@ function TF(key) { this.encrypt = function(block) { return this.tf.encrypt([].concat(block), 0); - } + }; } @@ -417,13 +417,13 @@ module.exports.blockSize = TF.prototype.blockSize = 16;
diff --git a/doc/user_attribute-UserAttribute.html b/doc/user_attribute-UserAttribute.html new file mode 100644 index 00000000..b00ee713 --- /dev/null +++ b/doc/user_attribute-UserAttribute.html @@ -0,0 +1,269 @@ + + + + + JSDoc: Class: UserAttribute + + + + + + + + + + +
+ +

Class: UserAttribute

+ + + + + +
+ +
+

+ packet/user_attribute~ + + UserAttribute +

+ +
+ +
+
+ + + + +
+

new UserAttribute()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

read(input)

+ + +
+
+ + +
+ parsing function for a user attribute packet (tag 17). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + payload of a tag 17 packet
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/user_attribute.html b/doc/user_attribute.html index 044cd0e1..92f93cc8 100644 --- a/doc/user_attribute.html +++ b/doc/user_attribute.html @@ -35,6 +35,8 @@
+ +
Implementation of the User Attribute Packet (Tag 17)

@@ -54,74 +56,6 @@ an implementation may use any method desired. module packet/user_attribute
-
-

new (require("packet/user_attribute"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -168,137 +102,23 @@ module packet/user_attribute
- - - - - - -

Methods

+

Classes

- -
-

read(input)

- - -
-
- - -
- parsing function for a user attribute packet (tag 17). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - payload of a tag 17 packet
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- +
UserAttribute
+
+ + + + + +
@@ -309,13 +129,13 @@ module packet/user_attribute
diff --git a/doc/user_attribute.js.html b/doc/user_attribute.js.html index 81e0c7cf..e34f9681 100644 --- a/doc/user_attribute.js.html +++ b/doc/user_attribute.js.html @@ -65,27 +65,28 @@ var util = require('../util'), packet = require('./packet.js'); +module.exports = UserAttribute; + /** * @constructor */ -module.exports = function user_attribute() { - this.tag = 17; +function UserAttribute() { this.attributes = []; +} - /** - * parsing function for a user attribute packet (tag 17). - * @param {String} input payload of a tag 17 packet - */ - this.read = function(bytes) { - var i = 0; - while (i < bytes.length) { - var len = packet.readSimpleLength(bytes.substr(i)); - i += len.offset; +/** + * parsing function for a user attribute packet (tag 17). + * @param {String} input payload of a tag 17 packet + */ +UserAttribute.prototype.read = function(bytes) { + var i = 0; + while (i < bytes.length) { + var len = packet.readSimpleLength(bytes.substr(i)); + i += len.offset; - this.attributes.push(bytes.substr(i, len.len)); - i += len.len; - } - }; + this.attributes.push(bytes.substr(i, len.len)); + i += len.len; + } }; @@ -97,13 +98,13 @@ module.exports = function user_attribute() {
diff --git a/doc/userid-Userid.html b/doc/userid-Userid.html new file mode 100644 index 00000000..13869573 --- /dev/null +++ b/doc/userid-Userid.html @@ -0,0 +1,429 @@ + + + + + JSDoc: Class: Userid + + + + + + + + + + +
+ +

Class: Userid

+ + + + + +
+ +
+

+ packet/userid~ + + Userid +

+ +
+ +
+
+ + + + +
+

new Userid()

+ + +
+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + +
+ + + + + + + + + + + + +

Members

+ +
+ +
+

userid :String

+ + +
+
+ +
+ A string containing the user id. Usually in the form +John Doe +
+ + + +
Type:
+
    +
  • + +String + + +
  • +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+

read(input)

+ + +
+
+ + +
+ Parsing function for a user id packet (tag 13). +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
input + + +String + + + + payload of a tag 13 packet
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+

write() → {String}

+ + +
+
+ + +
+ Creates a string representation of the user id packet +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + +
Returns:
+ + +
+ string representation +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/doc/userid.html b/doc/userid.html index 577681ef..2e8ae624 100644 --- a/doc/userid.html +++ b/doc/userid.html @@ -35,6 +35,8 @@
+ +
Implementation of the User ID Packet (Tag 13)

@@ -45,74 +47,6 @@ restrictions on its content. The packet length in the header specifies the length of the User ID.
-
-

new (require("packet/userid"))()

- - -
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - - - -
@@ -165,293 +99,19 @@ specifies the length of the User ID.
- - - - -

Members

+

Classes

- -
-

userid :String

- - -
-
- -
- A string containing the user id. Usually in the form -John Doe -
- - - -
Type:
-
    -
  • - -String - - -
  • -
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - -
- +
Userid
+
-

Methods

- -
- -
-

read(input)

-
-
-
- Parsing function for a user id packet (tag 13). -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
input - - -String - - - - payload of a tag 13 packet
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - -
- - - -
-

write() → {String}

- - -
-
- - -
- Creates a string representation of the user id packet -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - -
Returns:
- - -
- string representation -
- - - -
-
- Type -
-
- -String - - -
-
- - - - -
- -
- @@ -466,13 +126,13 @@ John Doe
- Documentation generated by JSDoc 3.2.0 on Thu Jan 02 2014 13:02:44 GMT-0800 (PST) + Documentation generated by JSDoc 3.2.0 on Sun Jan 05 2014 10:08:12 GMT-0800 (PST)
diff --git a/doc/userid.js.html b/doc/userid.js.html index 25f1adf3..f625c5dc 100644 --- a/doc/userid.js.html +++ b/doc/userid.js.html @@ -54,35 +54,36 @@ * @module packet/userid */ +module.exports = Userid; + var util = require('../util'); /** * @constructor */ -module.exports = function userid() { +function Userid() { /** A string containing the user id. Usually in the form * John Doe <john@example.com> * @type {String} */ this.userid = ''; - - - /** - * Parsing function for a user id packet (tag 13). - * @param {String} input payload of a tag 13 packet - */ - this.read = function (bytes) { - this.userid = util.decode_utf8(bytes); - }; - - /** - * Creates a string representation of the user id packet - * @return {String} string representation - */ - this.write = function () { - return util.encode_utf8(this.userid); - }; } + +/** + * Parsing function for a user id packet (tag 13). + * @param {String} input payload of a tag 13 packet + */ +Userid.prototype.read = function (bytes) { + this.userid = util.decode_utf8(bytes); +}; + +/** + * Creates a string representation of the user id packet + * @return {String} string representation + */ +Userid.prototype.write = function () { + return util.encode_utf8(this.userid); +};
@@ -93,13 +94,13 @@ module.exports = function userid() {
diff --git a/doc/util.html b/doc/util.html index bbbe6e34..db143f10 100644 --- a/doc/util.html +++ b/doc/util.html @@ -2000,13 +2000,13 @@ the same as bin2str.
diff --git a/doc/util.js.html b/doc/util.js.html index dac2954e..152fc6b5 100644 --- a/doc/util.js.html +++ b/doc/util.js.html @@ -97,7 +97,7 @@ module.exports = { while (h.length < 2) h = "0" + h; r.push(" " + h); i++; - if (i % 32 == 0) + if (i % 32 === 0) r.push("\n "); } return r.join(''); @@ -109,7 +109,7 @@ module.exports = { * @return {String} String containing the hexadecimal values */ hexstrdump: function (str) { - if (str == null) + if (str === null) return ""; var r = []; var e = str.length; @@ -277,7 +277,7 @@ module.exports = { getLeftNBits: function (string, bitcount) { var rest = bitcount % 8; - if (rest == 0) + if (rest === 0) return string.substring(0, bitcount / 8); var bytes = (bitcount - rest) / 8 + 1; var result = string.substring(0, bytes); @@ -293,7 +293,7 @@ module.exports = { */ shiftRight: function (value, bitcount) { var temp = util.str2bin(value); - if (bitcount % 8 != 0) { + if (bitcount % 8 !== 0) { for (var i = temp.length - 1; i >= 0; i--) { temp[i] >>= bitcount % 8; if (i > 0) @@ -339,13 +339,13 @@ module.exports = {
diff --git a/example/encryption.html b/example/encryption.html index 6f22d0cf..a756154e 100644 --- a/example/encryption.html +++ b/example/encryption.html @@ -135,13 +135,13 @@ function run(encrypt) { var sesskey = null; // Find the private (sub)key for the session key of the message for (var i = 0; i< msg[0].sessionKeys.length; i++) { - if (priv_key[0].privateKeyPacket.publicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) { + if (priv_key[0].privateKeyPacket.PublicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) { keymat = { key: priv_key[0], keymaterial: priv_key[0].privateKeyPacket}; sesskey = msg[0].sessionKeys[i]; break; } for (var j = 0; j < priv_key[0].subKeys.length; j++) { - if (priv_key[0].subKeys[j].publicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) { + if (priv_key[0].subKeys[j].PublicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) { keymat = { key: priv_key[0], keymaterial: priv_key[0].subKeys[j]}; sesskey = msg[0].sessionKeys[i]; break; diff --git a/src/cleartext.js b/src/cleartext.js index e7d8f260..4d9a4c95 100644 --- a/src/cleartext.js +++ b/src/cleartext.js @@ -43,7 +43,7 @@ function CleartextMessage(text, packetlist) { } // normalize EOL to canonical form this.text = text.replace(/\r/g, '').replace(/[\t ]+\n/g, "\n").replace(/\n/g,"\r\n"); - this.packets = packetlist || new packet.list(); + this.packets = packetlist || new packet.List(); } /** @@ -64,11 +64,11 @@ CleartextMessage.prototype.getSigningKeyIds = function() { * @param {Array} privateKeys private keys with decrypted secret key data for signing */ CleartextMessage.prototype.sign = function(privateKeys) { - var packetlist = new packet.list(); - var literalDataPacket = new packet.literal(); + var packetlist = new packet.List(); + var literalDataPacket = new packet.Literal(); literalDataPacket.setText(this.text); for (var i = 0; i < privateKeys.length; i++) { - var signaturePacket = new packet.signature(); + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = enums.signature.text; signaturePacket.hashAlgorithm = config.prefer_hash_algorithm; var signingKeyPacket = privateKeys[i].getSigningKeyPacket(); @@ -88,7 +88,7 @@ CleartextMessage.prototype.sign = function(privateKeys) { CleartextMessage.prototype.verify = function(publicKeys) { var result = []; var signatureList = this.packets.filterByTag(enums.packet.signature); - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); // we assume that cleartext signature is generated based on UTF8 cleartext literalDataPacket.setText(this.text); publicKeys.forEach(function(pubKey) { @@ -140,7 +140,7 @@ function readArmored(armoredText) { if (input.type !== enums.armor.signed) { throw new Error('No cleartext signed message.'); } - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input.data); var newMessage = new CleartextMessage(input.text, packetlist); return newMessage; diff --git a/src/config/localStorage.js b/src/config/localStorage.js index 7f77e861..fe59fdf3 100644 --- a/src/config/localStorage.js +++ b/src/config/localStorage.js @@ -3,29 +3,31 @@ * @module config/localStorage */ +module.exports = LocalStorage; + /** * @constructor */ -module.exports = function localStorage() { +function LocalStorage() { +} - /** - * Reads the config out of the HTML5 local storage - * and initializes the object config. - * if config is null the default config will be used - */ - this.read = function () { - var cf = JSON.parse(window.localStorage.getItem("config")); - if (cf === null) { - this.config = this.default_config; - this.write(); - } else - this.config = cf; - }; - - /** - * Writes the config to HTML5 local storage - */ - this.write = function () { - window.localStorage.setItem("config", JSON.stringify(this.config)); - }; +/** + * Reads the config out of the HTML5 local storage + * and initializes the object config. + * if config is null the default config will be used + */ +LocalStorage.prototype.read = function () { + var cf = JSON.parse(window.localStorage.getItem("config")); + if (cf === null) { + this.config = this.default_config; + this.write(); + } else + this.config = cf; +}; + +/** + * Writes the config to HTML5 local storage + */ +LocalStorage.prototype.write = function () { + window.localStorage.setItem("config", JSON.stringify(this.config)); }; diff --git a/src/enums.js b/src/enums.js index 97ab8f99..b41ca639 100644 --- a/src/enums.js +++ b/src/enums.js @@ -76,23 +76,23 @@ module.exports = { * @readonly */ packet: { - public_key_encrypted_session_key: 1, + publicKeyEncryptedSessionKey: 1, signature: 2, - sym_encrypted_session_key: 3, - one_pass_signature: 4, - secret_key: 5, - public_key: 6, - secret_subkey: 7, + symEncryptedSessionKey: 3, + onePassSignature: 4, + secretKey: 5, + publicKey: 6, + secretSubkey: 7, compressed: 8, - symmetrically_encrypted: 9, + symmetricallyEncrypted: 9, marker: 10, literal: 11, trust: 12, userid: 13, - public_subkey: 14, - user_attribute: 17, - sym_encrypted_integrity_protected: 18, - modification_detection_code: 19 + publicSubkey: 14, + userAttribute: 17, + symEncryptedIntegrityProtected: 18, + modificationDetectionCode: 19 }, /** Data types in the literal packet diff --git a/src/key.js b/src/key.js index ddea303e..cb9b32f4 100644 --- a/src/key.js +++ b/src/key.js @@ -59,19 +59,19 @@ Key.prototype.packetlist2structure = function(packetlist) { var user, primaryKeyId, subKey; for (var i = 0; i < packetlist.length; i++) { switch (packetlist[i].tag) { - case enums.packet.public_key: - case enums.packet.secret_key: + case enums.packet.publicKey: + case enums.packet.secretKey: this.primaryKey = packetlist[i]; primaryKeyId = this.primaryKey.getKeyId(); break; case enums.packet.userid: - case enums.packet.user_attribute: + case enums.packet.userAttribute: user = new User(packetlist[i]); if (!this.users) this.users = []; this.users.push(user); break; - case enums.packet.public_subkey: - case enums.packet.secret_subkey: + case enums.packet.publicSubkey: + case enums.packet.secretSubkey: user = null; if (!this.subKeys) this.subKeys = []; subKey = new SubKey(packetlist[i]); @@ -124,7 +124,7 @@ Key.prototype.packetlist2structure = function(packetlist) { * @return {module:packet/packetlist} The packets that form a key */ Key.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.primaryKey); packetlist.push(this.revocationSignature); packetlist.concat(this.directSignatures); @@ -201,7 +201,7 @@ function findKey(keys, keyIds) { * @return {(module:packet/public_subkey|module:packet/public_key|null)} */ Key.prototype.getPublicKeyPacket = function(keyIds) { - if (this.primaryKey.tag == enums.packet.public_key) { + if (this.primaryKey.tag == enums.packet.publicKey) { return findKey(this.getAllKeyPackets(), keyIds); } else { return null; @@ -214,7 +214,7 @@ Key.prototype.getPublicKeyPacket = function(keyIds) { * @return {(module:packet/secret_subkey|module:packet/secret_key|null)} */ Key.prototype.getPrivateKeyPacket = function(keyIds) { - if (this.primaryKey.tag == enums.packet.secret_key) { + if (this.primaryKey.tag == enums.packet.secretKey) { return findKey(this.getAllKeyPackets(), keyIds); } else { return null; @@ -240,7 +240,7 @@ Key.prototype.getUserIds = function() { * @return {Boolean} */ Key.prototype.isPublic = function() { - return this.primaryKey.tag == enums.packet.public_key; + return this.primaryKey.tag == enums.packet.publicKey; }; /** @@ -248,7 +248,7 @@ Key.prototype.isPublic = function() { * @return {Boolean} */ Key.prototype.isPrivate = function() { - return this.primaryKey.tag == enums.packet.secret_key; + return this.primaryKey.tag == enums.packet.secretKey; }; /** @@ -256,20 +256,20 @@ Key.prototype.isPrivate = function() { * @return {module:key~Key} new public Key */ Key.prototype.toPublic = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); var keyPackets = this.toPacketlist(); var bytes; for (var i = 0; i < keyPackets.length; i++) { switch (keyPackets[i].tag) { - case enums.packet.secret_key: + case enums.packet.secretKey: bytes = keyPackets[i].writePublicKey(); - var pubKeyPacket = new packet.public_key(); + var pubKeyPacket = new packet.PublicKey(); pubKeyPacket.read(bytes); packetlist.push(pubKeyPacket); break; - case enums.packet.secret_subkey: + case enums.packet.secretSubkey: bytes = keyPackets[i].writePublicKey(); - var pubSubkeyPacket = new packet.public_subkey(); + var pubSubkeyPacket = new packet.PublicSubkey(); pubSubkeyPacket.read(bytes); packetlist.push(pubSubkeyPacket); break; @@ -487,7 +487,7 @@ function User(userPacket) { return new User(userPacket); } this.userId = userPacket.tag == enums.packet.userid ? userPacket : null; - this.userAttribute = userPacket.tag == enums.packet.user_attribute ? userPacket : null; + this.userAttribute = userPacket.tag == enums.packet.userAttribute ? userPacket : null; this.selfCertifications = null; this.otherCertifications = null; this.revocationCertifications = null; @@ -498,7 +498,7 @@ function User(userPacket) { * @return {module:packet/packetlist} */ User.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.userId || this.userAttribute); packetlist.concat(this.revocationCertifications); packetlist.concat(this.selfCertifications); @@ -604,7 +604,7 @@ function SubKey(subKeyPacket) { * @return {module:packet/packetlist} */ SubKey.prototype.toPacketlist = function() { - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.push(this.subKey); packetlist.push(this.revocationSignature); packetlist.push(this.bindingSignature); @@ -682,9 +682,9 @@ function readArmored(armoredText) { if (!(input.type == enums.armor.public_key || input.type == enums.armor.private_key)) { throw new Error('Armored text not of type key'); } - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input.data); - var keyIndex = packetlist.indexOfTag(enums.packet.public_key, enums.packet.secret_key); + var keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey); if (keyIndex.length === 0) { throw new Error('No key packet found in armored text'); } @@ -717,20 +717,20 @@ function readArmored(armoredText) { * @static */ function generate(keyType, numBits, userId, passphrase) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); - var secretKeyPacket = new packet.secret_key(); + var secretKeyPacket = new packet.SecretKey(); secretKeyPacket.algorithm = enums.read(enums.publicKey, keyType); secretKeyPacket.generate(numBits); secretKeyPacket.encrypt(passphrase); - var userIdPacket = new packet.userid(); + var userIdPacket = new packet.Userid(); userIdPacket.read(userId); var dataToSign = {}; dataToSign.userid = userIdPacket; dataToSign.key = secretKeyPacket; - var signaturePacket = new packet.signature(); + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = enums.signature.cert_generic; signaturePacket.publicKeyAlgorithm = keyType; //TODO we should load preferred hash from config, or as input to this function @@ -738,7 +738,7 @@ function generate(keyType, numBits, userId, passphrase) { signaturePacket.keyFlags = [enums.keyFlags.certify_keys | enums.keyFlags.sign_data]; signaturePacket.sign(secretKeyPacket, dataToSign); - var secretSubkeyPacket = new packet.secret_subkey(); + var secretSubkeyPacket = new packet.SecretSubkey(); secretSubkeyPacket.algorithm = enums.read(enums.publicKey, keyType); secretSubkeyPacket.generate(numBits); secretSubkeyPacket.encrypt(passphrase); @@ -746,7 +746,7 @@ function generate(keyType, numBits, userId, passphrase) { dataToSign = {}; dataToSign.key = secretKeyPacket; dataToSign.bind = secretSubkeyPacket; - var subkeySignaturePacket = new packet.signature(); + var subkeySignaturePacket = new packet.Signature(); subkeySignaturePacket.signatureType = enums.signature.subkey_binding; subkeySignaturePacket.publicKeyAlgorithm = keyType; //TODO we should load preferred hash from config, or as input to this function diff --git a/src/keyring/keyring.js b/src/keyring/keyring.js index 22f33db3..8876da6d 100644 --- a/src/keyring/keyring.js +++ b/src/keyring/keyring.js @@ -31,160 +31,159 @@ var openpgp = require('openpgp'); * @return {Boolean} True if the input matches the specified key */ -/** +module.exports = Keyring; + + /** * Initialization routine for the keyring. This method reads the * keyring from HTML5 local storage and initializes this instance. * @constructor * @param {class} [storeHandler] class implementing load() and store() methods */ -module.exports = function(storeHandler) { - if (!storeHandler) { - storeHandler = new (require('./localstore.js'))(); - } - this.storeHandler = storeHandler; +function Keyring(storeHandler) { + this.storeHandler = storeHandler || new (require('./localstore.js'))(); this.keys = this.storeHandler.load(); - - /** - * Calls the storeHandler to save the keys - */ - this.store = function () { - this.storeHandler.store(this.keys); - }; - - /** - * Clear the keyring - erase all the keys - */ - this.clear = function() { - this.keys = []; - }; - - /** - * Checks a key to see if it matches the specified email address - * @param {String} email email address to search for - * @param {module:key~Key} key The key to be checked. - * @return {Boolean} True if the email address is defined in the specified key - */ - function emailCheck(email, key) { - email = email.toLowerCase(); - var keyEmails = key.getUserIds(); - for (var i; i < keyEmails.length; i++) { - //we need to get just the email from the userid key - keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase(); - if (keyEmail == email) { - return true; - } - } - return false; - } - - /** - * Checks a key to see if it matches the specified keyid - * @param {String} id hex string keyid to search for - * @param {module:key~Key} key the key to be checked. - * @return {Boolean} true if the email address is defined in the specified key - * @inner - */ - 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; - } - - /** - * searches all public keys in the keyring matching the address or address part of the user ids - * @param {Array} keys array of keys to search - * @param {module:keyring/keyring.checkCallback} identityFunction callback function which checks for a match - * @param {String} identityInput input to check against - * @param {module:enums.packet} keyType packet types of keys to check - * @return {Array} array of keys which match - */ - function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) { - var results = []; - 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)) { - results.push(key); - } - break; - case openpgp.enums.packet.private_key: - if (key.isPrivate() && identityFunction(identityInput, key)) { - results.push(key); - } - break; - } - } - return results; - } - - /** - * searches all public keys in the keyring matching the address or address part of the user ids - * @param {String} email email address to search for - * @return {Array} The public keys associated with provided email address. - */ - this.getPublicKeyForAddress = function (email) { - return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.public_key); - }; - - /** - * Searches the keyring for a private key containing the specified email address - * @param {String} email email address to search for - * @return {Array} private keys found - */ - this.getPrivateKeyForAddress = function (email) { - return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secret_key); - }; - - /** - * Searches the keyring for public keys having the specified key id - * @param {String} keyId provided as string of hex number (lowercase) - * @return {Array} public keys found - */ - this.getKeysForKeyId = function (keyId) { - return checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.public_key); - }; - - /** - * Imports a key from an ascii armored message - * @param {String} armored message to read the keys/key from - */ - this.importKey = function (armored) { - this.keys = this.keys.concat(openpgp.key.readArmored(armored).keys); - - return true; - }; - - /** - * returns the armored message representation of the key at key ring index - * @param {Integer} index the index of the key within the array - * @return {String} armored message representing the key object - */ - this.exportKey = function (index) { - return this.keys[index].armor(); - }; - - /** - * 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 - * @return {module:key~Key} The public key object which has been removed - */ - this.removeKey = function (index) { - var removed = this.keys.splice(index, 1); - - return removed; - }; - - /** - * 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 - */ - this.exportPublicKey = function (index) { - return this.keys[index].toPublic().armor(); - }; +}; + +/** + * Calls the storeHandler to save the keys + */ +Keyring.prototype.store = function () { + this.storeHandler.store(this.keys); +}; + +/** + * Clear the keyring - erase all the keys + */ +Keyring.prototype.clear = function() { + this.keys = []; +}; + +/** + * Checks a key to see if it matches the specified email address + * @param {String} email email address to search for + * @param {module:key~Key} key The key to be checked. + * @return {Boolean} True if the email address is defined in the specified key + */ +function emailCheck(email, key) { + email = email.toLowerCase(); + var keyEmails = key.getUserIds(); + for (var i; i < keyEmails.length; i++) { + //we need to get just the email from the userid key + keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase(); + if (keyEmail == email) { + return true; + } + } + return false; +} + +/** + * Checks a key to see if it matches the specified keyid + * @param {String} id hex string keyid to search for + * @param {module:key~Key} key the key to be checked. + * @return {Boolean} true if the email address is defined in the specified key + * @inner + */ +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; +} + +/** + * searches all public keys in the keyring matching the address or address part of the user ids + * @param {Array} keys array of keys to search + * @param {module:keyring/keyring.checkCallback} identityFunction callback function which checks for a match + * @param {String} identityInput input to check against + * @param {module:enums.packet} keyType packet types of keys to check + * @return {Array} array of keys which match + */ +function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) { + var results = []; + for (var p = 0; p < keys.length; p++) { + var key = keys[p]; + switch (keyType) { + case openpgp.enums.packet.publicKey: + if (key.isPublic() && identityFunction(identityInput, key)) { + results.push(key); + } + break; + case openpgp.enums.packet.private_key: + if (key.isPrivate() && identityFunction(identityInput, key)) { + results.push(key); + } + break; + } + } + return results; +} + +/** + * searches all public keys in the keyring matching the address or address part of the user ids + * @param {String} email email address to search for + * @return {Array} The public keys associated with provided email address. + */ +Keyring.prototype.getPublicKeyForAddress = function (email) { + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.publicKey); +}; + +/** + * Searches the keyring for a private key containing the specified email address + * @param {String} email email address to search for + * @return {Array} private keys found + */ +Keyring.prototype.getPrivateKeyForAddress = function (email) { + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secretKey); +}; + +/** + * Searches the keyring for public keys having the specified key id + * @param {String} keyId provided as string of hex number (lowercase) + * @return {Array} public keys found + */ +Keyring.prototype.getKeysForKeyId = function (keyId) { + return checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.publicKey); +}; + +/** + * Imports a key from an ascii armored message + * @param {String} armored message to read the keys/key from + */ +Keyring.prototype.importKey = function (armored) { + this.keys = this.keys.concat(openpgp.key.readArmored(armored).keys); + + return true; +}; + +/** + * returns the armored message representation of the key at key ring index + * @param {Integer} index the index of the key within the array + * @return {String} armored message representing the key object + */ +Keyring.prototype.exportKey = function (index) { + return this.keys[index].armor(); +}; + +/** + * 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 + * @return {module:key~Key} The public key object which has been removed + */ +Keyring.prototype.removeKey = function (index) { + var removed = this.keys.splice(index, 1); + + return removed; +}; + +/** + * 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 + */ +Keyring.prototype.exportPublicKey = function (index) { + return this.keys[index].toPublic().armor(); }; diff --git a/src/keyring/localstore.js b/src/keyring/localstore.js index 4fc1fd8b..c9c18411 100644 --- a/src/keyring/localstore.js +++ b/src/keyring/localstore.js @@ -20,37 +20,39 @@ * @requires openpgp * @module keyring/localstore */ +module.exports = LocalStore; var openpgp = require('openpgp'); -module.exports = function () { - /** - * Load the keyring from HTML5 local storage and initializes this instance. - * @return {Array} array of keys retrieved from localstore - */ - this.load = function () { - var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys")); - var keys = []; - if (armoredKeys !== null && armoredKeys.length !== 0) { - var key; - for (var i = 0; i < armoredKeys.length; i++) { - key = openpgp.key.readArmored(armoredKeys[i]); - keys.push(key); - } - } - return keys; - }; +function LocalStore() { +} - /** - * Saves the current state of the keyring to HTML5 local storage. - * The privateKeys array and publicKeys array gets Stringified using JSON - * @param {Array} keys array of keys to save in localstore - */ - this.store = function (keys) { - var armoredKeys = []; - for (var i = 0; i < keys.length; i++) { - armoredKeys.push(keys[i].armor()); +/** + * Load the keyring from HTML5 local storage and initializes this instance. + * @return {Array} array of keys retrieved from localstore + */ +LocalStore.prototype.load = function () { + var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys")); + var keys = []; + if (armoredKeys !== null && armoredKeys.length !== 0) { + var key; + for (var i = 0; i < armoredKeys.length; i++) { + key = openpgp.key.readArmored(armoredKeys[i]); + keys.push(key); } - window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys)); - }; + } + return keys; +}; + +/** + * Saves the current state of the keyring to HTML5 local storage. + * The privateKeys array and publicKeys array gets Stringified using JSON + * @param {Array} keys array of keys to save in localstore + */ +LocalStore.prototype.store = function (keys) { + var armoredKeys = []; + for (var i = 0; i < keys.length; i++) { + armoredKeys.push(keys[i].armor()); + } + window.localStorage.setItem("armoredKeys", JSON.stringify(armoredKeys)); }; diff --git a/src/message.js b/src/message.js index f2c9dfae..111c04a2 100644 --- a/src/message.js +++ b/src/message.js @@ -42,7 +42,7 @@ function Message(packetlist) { if (!(this instanceof Message)) { return new Message(packetlist); } - this.packets = packetlist || new packet.list(); + this.packets = packetlist || new packet.List(); } /** @@ -51,7 +51,7 @@ function Message(packetlist) { */ Message.prototype.getEncryptionKeyIds = function() { var keyIds = []; - var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.public_key_encrypted_session_key); + var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey); pkESKeyPacketlist.forEach(function(packet) { keyIds.push(packet.publicKeyId); }); @@ -66,7 +66,7 @@ Message.prototype.getSigningKeyIds = function() { var keyIds = []; var msg = this.unwrapCompressed(); // search for one pass signatures - var onePassSigList = msg.packets.filterByTag(enums.packet.one_pass_signature); + var onePassSigList = msg.packets.filterByTag(enums.packet.onePassSignature); onePassSigList.forEach(function(packet) { keyIds.push(packet.signingKeyId); }); @@ -93,7 +93,7 @@ Message.prototype.decrypt = function(privateKey) { } var privateKeyPacket = privateKey.getPrivateKeyPacket(encryptionKeyIds); if (!privateKeyPacket.isDecrypted) throw new Error('Private key is not decrypted.'); - var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.public_key_encrypted_session_key); + var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey); var pkESKeyPacket; for (var i = 0; i < pkESKeyPacketlist.length; i++) { if (pkESKeyPacketlist[i].publicKeyId.equals(privateKeyPacket.getKeyId())) { @@ -103,7 +103,7 @@ Message.prototype.decrypt = function(privateKey) { } } if (pkESKeyPacket) { - var symEncryptedPacketlist = this.packets.filterByTag(enums.packet.symmetrically_encrypted, enums.packet.sym_encrypted_integrity_protected); + var symEncryptedPacketlist = this.packets.filterByTag(enums.packet.symmetricallyEncrypted, enums.packet.symEncryptedIntegrityProtected); if (symEncryptedPacketlist.length !== 0) { var symEncryptedPacket = symEncryptedPacketlist[0]; symEncryptedPacket.decrypt(pkESKeyPacket.sessionKeyAlgorithm, pkESKeyPacket.sessionKey); @@ -140,13 +140,13 @@ Message.prototype.getText = function() { * @return {Array} new message with encrypted content */ Message.prototype.encrypt = function(keys) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); //TODO get preferred algo from signature var sessionKey = crypto.generateSessionKey(enums.read(enums.symmetric, config.encryption_cipher)); keys.forEach(function(key) { var encryptionKeyPacket = key.getEncryptionKeyPacket(); if (encryptionKeyPacket) { - var pkESKeyPacket = new packet.public_key_encrypted_session_key(); + var pkESKeyPacket = new packet.PublicKeyEncryptedSessionKey(); pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId(); pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm; pkESKeyPacket.sessionKey = sessionKey; @@ -160,9 +160,9 @@ Message.prototype.encrypt = function(keys) { }); var symEncryptedPacket; if (config.integrity_protect) { - symEncryptedPacket = new packet.sym_encrypted_integrity_protected(); + symEncryptedPacket = new packet.SymEncryptedIntegrityProtected(); } else { - symEncryptedPacket = new packet.symmetrically_encrypted(); + symEncryptedPacket = new packet.SymmetricallyEncrypted(); } symEncryptedPacket.packets = this.packets; //TODO get preferred algo from signature @@ -178,7 +178,7 @@ Message.prototype.encrypt = function(keys) { */ Message.prototype.sign = function(privateKeys) { - var packetlist = new packet.list(); + var packetlist = new packet.List(); var literalDataPacket = this.packets.findPacket(enums.packet.literal); if (!literalDataPacket) throw new Error('No literal data packet to sign.'); @@ -188,7 +188,7 @@ Message.prototype.sign = function(privateKeys) { enums.signature.binary : enums.signature.text; var i; for (i = 0; i < privateKeys.length; i++) { - var onePassSig = new packet.one_pass_signature(); + var onePassSig = new packet.OnePassSignature(); onePassSig.type = signatureType; //TODO get preferred hashg algo from key signature onePassSig.hashAlgorithm = config.prefer_hash_algorithm; @@ -204,7 +204,7 @@ Message.prototype.sign = function(privateKeys) { packetlist.push(literalDataPacket); for (i = privateKeys.length - 1; i >= 0; i--) { - var signaturePacket = new packet.signature(); + var signaturePacket = new packet.Signature(); signaturePacket.signatureType = signatureType; signaturePacket.hashAlgorithm = config.prefer_hash_algorithm; signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm; @@ -273,7 +273,7 @@ function readArmored(armoredText) { //TODO how do we want to handle bad text? Exception throwing //TODO don't accept non-message armored texts var input = armor.decode(armoredText).data; - var packetlist = new packet.list(); + var packetlist = new packet.List(); packetlist.read(input); var newMessage = new Message(packetlist); return newMessage; @@ -286,10 +286,10 @@ function readArmored(armoredText) { * @static */ function fromText(text) { - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); // text will be converted to UTF8 literalDataPacket.setText(text); - var literalDataPacketlist = new packet.list(); + var literalDataPacketlist = new packet.List(); literalDataPacketlist.push(literalDataPacket); var newMessage = new Message(literalDataPacketlist); return newMessage; @@ -302,9 +302,9 @@ function fromText(text) { * @static */ function fromBinary(bytes) { - var literalDataPacket = new packet.literal(); + var literalDataPacket = new packet.Literal(); literalDataPacket.setBytes(bytes, enums.read(enums.literal, enums.literal.binary)); - var literalDataPacketlist = new packet.list(); + var literalDataPacketlist = new packet.List(); literalDataPacketlist.push(literalDataPacket); var newMessage = new Message(literalDataPacketlist); return newMessage; diff --git a/src/packet/all_packets.js b/src/packet/all_packets.js index c85c7851..48ed71f3 100644 --- a/src/packet/all_packets.js +++ b/src/packet/all_packets.js @@ -8,41 +8,48 @@ var enums = require('../enums.js'); module.exports = { /** @see module:packet/compressed */ - compressed: require('./compressed.js'), + Compressed: require('./compressed.js'), /** @see module:packet/sym_encrypted_integrity_protected */ - sym_encrypted_integrity_protected: require('./sym_encrypted_integrity_protected.js'), + SymEncryptedIntegrityProtected: require('./sym_encrypted_integrity_protected.js'), /** @see module:packet/public_key_encrypted_session_key */ - public_key_encrypted_session_key: require('./public_key_encrypted_session_key.js'), + PublicKeyEncryptedSessionKey: require('./public_key_encrypted_session_key.js'), /** @see module:packet/sym_encrypted_session_key */ - sym_encrypted_session_key: require('./sym_encrypted_session_key.js'), + SymEncryptedSessionKey: require('./sym_encrypted_session_key.js'), /** @see module:packet/literal */ - literal: require('./literal.js'), + Literal: require('./literal.js'), /** @see module:packet/public_key */ - public_key: require('./public_key.js'), + PublicKey: require('./public_key.js'), /** @see module:packet/symmetrically_encrypted */ - symmetrically_encrypted: require('./symmetrically_encrypted.js'), + SymmetricallyEncrypted: require('./symmetrically_encrypted.js'), /** @see module:packet/marker */ - marker: require('./marker.js'), + Marker: require('./marker.js'), /** @see module:packet/public_subkey */ - public_subkey: require('./public_subkey.js'), + PublicSubkey: require('./public_subkey.js'), /** @see module:packet/user_attribute */ - user_attribute: require('./user_attribute.js'), + UserAttribute: require('./user_attribute.js'), /** @see module:packet/one_pass_signature */ - one_pass_signature: require('./one_pass_signature.js'), + OnePassSignature: require('./one_pass_signature.js'), /** @see module:packet/secret_key */ - secret_key: require('./secret_key.js'), + SecretKey: require('./secret_key.js'), /** @see module:packet/userid */ - userid: require('./userid.js'), + Userid: require('./userid.js'), /** @see module:packet/secret_subkey */ - secret_subkey: require('./secret_subkey.js'), + SecretSubkey: require('./secret_subkey.js'), /** @see module:packet/signature */ - signature: require('./signature.js'), + Signature: require('./signature.js'), /** @see module:packet/trust */ - trust: require('./trust.js') + Trust: require('./trust.js'), + + newPacketFromTag: function (tag) { + return new this[packetClassFromTag(tag)](); + } }; +function packetClassFromTag(tag) { + return tag.substr(0, 1).toUpperCase() + tag.substr(1); +} for (var i in enums.packet) { - var packetClass = module.exports[i]; + var packetClass = module.exports[packetClassFromTag(i)]; if (packetClass !== undefined) packetClass.prototype.tag = enums.packet[i]; diff --git a/src/packet/compressed.js b/src/packet/compressed.js index 53edb353..f0687ed9 100644 --- a/src/packet/compressed.js +++ b/src/packet/compressed.js @@ -27,6 +27,8 @@ * @module packet/compressed */ +module.exports = Compressed; + var enums = require('../enums.js'), JXG = require('../compression/jxg.js'), base64 = require('../encoding/base64.js'); @@ -34,7 +36,7 @@ var enums = require('../enums.js'), /** * @constructor */ -module.exports = function compressed() { +function Compressed() { /** * List of packets * @type {module:packet/packetlist} @@ -51,118 +53,117 @@ module.exports = function compressed() { * @type {String} */ this.compressed = null; +} +/** + * Parsing function for the packet. + * @param {String} bytes Payload of a tag 8 packet + */ +Compressed.prototype.read = function (bytes) { + // One octet that gives the algorithm used to compress the packet. + this.algorithm = enums.read(enums.compression, bytes.charCodeAt(0)); - /** - * Parsing function for the packet. - * @param {String} bytes Payload of a tag 8 packet - */ - this.read = function(bytes) { - // One octet that gives the algorithm used to compress the packet. - this.algorithm = enums.read(enums.compression, bytes.charCodeAt(0)); + // Compressed data, which makes up the remainder of the packet. + this.compressed = bytes.substr(1); - // Compressed data, which makes up the remainder of the packet. - this.compressed = bytes.substr(1); - - this.decompress(); - }; - - - - /** - * Return the compressed packet. - * @return {String} binary compressed packet - */ - this.write = function() { - if (this.compressed === null) - this.compress(); - - return String.fromCharCode(enums.write(enums.compression, this.algorithm)) + this.compressed; - }; - - - /** - * Decompression method for decompressing the compressed data - * read by read_packet - */ - this.decompress = function() { - var decompressed, compdata, radix; - - switch (this.algorithm) { - case 'uncompressed': - decompressed = this.compressed; - break; - - case 'zip': - compData = this.compressed; - - radix = base64.encode(compData).replace(/\n/g, ""); - // no header in this case, directly call deflate - var jxg_obj = new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(radix)); - - decompressed = unescape(jxg_obj.deflate()[0][0]); - break; - - case 'zlib': - //RFC 1950. Bits 0-3 Compression Method - var compressionMethod = this.compressed.charCodeAt(0) % 0x10; - - //Bits 4-7 RFC 1950 are LZ77 Window. Generally this value is 7 == 32k window size. - // 2nd Byte in RFC 1950 is for "FLAGs" Allows for a Dictionary - // (how is this defined). Basic checksum, and compression level. - - if (compressionMethod == 8) { //CM 8 is for DEFLATE, RFC 1951 - // remove 4 bytes ADLER32 checksum from the end - compData = this.compressed.substring(0, this.compressed.length - 4); - radix = base64.encode(compData).replace(/\n/g, ""); - //TODO check ADLER32 checksum - decompressed = JXG.decompress(radix); - break; - - } else { - throw new Error("Compression algorithm ZLIB only supports " + - "DEFLATE compression method."); - } - break; - - case 'bzip2': - // TODO: need to implement this - throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.'); - - default: - throw new Error("Compression algorithm unknown :" + this.alogrithm); - } - - this.packets.read(decompressed); - }; - - /** - * Compress the packet data (member decompressedData) - */ - this.compress = function() { - switch (this.algorithm) { - - case 'uncompressed': - // - Uncompressed - this.compressed = this.packets.write(); - break; - - case 'zip': - // - ZIP [RFC1951] - throw new Error("Compression algorithm ZIP [RFC1951] is not implemented."); - - case 'zlib': - // - ZLIB [RFC1950] - // TODO: need to implement this - throw new Error("Compression algorithm ZLIB [RFC1950] is not implemented."); - - case 'bzip2': - // - BZip2 [BZ2] - // TODO: need to implement this - throw new Error("Compression algorithm BZip2 [BZ2] is not implemented."); - - default: - throw new Error("Compression algorithm unknown :" + this.type); - } - }; + this.decompress(); +}; + + + +/** + * Return the compressed packet. + * @return {String} binary compressed packet + */ +Compressed.prototype.write = function () { + if (this.compressed === null) + this.compress(); + + return String.fromCharCode(enums.write(enums.compression, this.algorithm)) + this.compressed; +}; + + +/** + * Decompression method for decompressing the compressed data + * read by read_packet + */ +Compressed.prototype.decompress = function () { + var decompressed, compdata, radix; + + switch (this.algorithm) { + case 'uncompressed': + decompressed = this.compressed; + break; + + case 'zip': + compData = this.compressed; + + radix = base64.encode(compData).replace(/\n/g, ""); + // no header in this case, directly call deflate + var jxg_obj = new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(radix)); + + decompressed = unescape(jxg_obj.deflate()[0][0]); + break; + + case 'zlib': + //RFC 1950. Bits 0-3 Compression Method + var compressionMethod = this.compressed.charCodeAt(0) % 0x10; + + //Bits 4-7 RFC 1950 are LZ77 Window. Generally this value is 7 == 32k window size. + // 2nd Byte in RFC 1950 is for "FLAGs" Allows for a Dictionary + // (how is this defined). Basic checksum, and compression level. + + if (compressionMethod == 8) { //CM 8 is for DEFLATE, RFC 1951 + // remove 4 bytes ADLER32 checksum from the end + compData = this.compressed.substring(0, this.compressed.length - 4); + radix = base64.encode(compData).replace(/\n/g, ""); + //TODO check ADLER32 checksum + decompressed = JXG.decompress(radix); + break; + + } else { + throw new Error("Compression algorithm ZLIB only supports " + + "DEFLATE compression method."); + } + break; + + case 'bzip2': + // TODO: need to implement this + throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.'); + + default: + throw new Error("Compression algorithm unknown :" + this.alogrithm); + } + + this.packets.read(decompressed); +}; + +/** + * Compress the packet data (member decompressedData) + */ +Compressed.prototype.compress = function () { + switch (this.algorithm) { + + case 'uncompressed': + // - Uncompressed + this.compressed = this.packets.write(); + break; + + case 'zip': + // - ZIP [RFC1951] + throw new Error("Compression algorithm ZIP [RFC1951] is not implemented."); + + case 'zlib': + // - ZLIB [RFC1950] + // TODO: need to implement this + throw new Error("Compression algorithm ZLIB [RFC1950] is not implemented."); + + case 'bzip2': + // - BZip2 [BZ2] + // TODO: need to implement this + throw new Error("Compression algorithm BZip2 [BZ2] is not implemented."); + + default: + throw new Error("Compression algorithm unknown :" + this.type); + } }; diff --git a/src/packet/index.js b/src/packet/index.js index 4396464c..2e62eba6 100644 --- a/src/packet/index.js +++ b/src/packet/index.js @@ -1,7 +1,7 @@ var enums = require('../enums.js'); module.exports = { - list: require('./packetlist.js') + List: require('./packetlist.js') }; var packets = require('./all_packets.js'); diff --git a/src/packet/literal.js b/src/packet/literal.js index 7b2f5a7a..17e76ca6 100644 --- a/src/packet/literal.js +++ b/src/packet/literal.js @@ -25,105 +25,106 @@ * @module packet/literal */ +module.exports = Literal; + var util = require('../util'), enums = require('../enums.js'); /** * @constructor */ -module.exports = function literal() { +function Literal() { this.format = 'utf8'; // default format for literal data packets this.data = ''; // literal data representation as native JavaScript string or bytes this.date = new Date(); +} - - /** - * Set the packet data to a javascript native string, end of line - * will be normalized to \r\n and by default text is converted to UTF8 - * @param {String} text Any native javascript string - */ - this.setText = function (text) { - // normalize EOL to \r\n - text = text.replace(/\r/g, '').replace(/\n/g, '\r\n'); - // encode UTF8 - this.data = this.format == 'utf8' ? util.encode_utf8(text) : text; - }; - - /** - * Returns literal data packets as native JavaScript string - * with normalized end of line to \n - * @return {String} literal data as text - */ - this.getText = function () { - // decode UTF8 - var text = util.decode_utf8(this.data); - // normalize EOL to \n - return text.replace(/\r\n/g, '\n'); - }; - - /** - * Set the packet data to value represented by the provided string of bytes. - * @param {String} bytes The string of bytes - * @param {utf8|binary|text} format The format of the string of bytes - */ - this.setBytes = function (bytes, format) { - this.format = format; - this.data = bytes; - }; - - - /** - * Get the byte sequence representing the literal packet data - * @returns {String} A sequence of bytes - */ - this.getBytes = function () { - return this.data; - }; - - - /** - * Parsing function for a literal data packet (tag 11). - * - * @param {String} input Payload of a tag 11 packet - * @param {Integer} position - * Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/literal} object representation - */ - this.read = function (bytes) { - // - A one-octet field that describes how the data is formatted. - - var format = enums.read(enums.literal, bytes.charCodeAt(0)); - - var filename_len = bytes.charCodeAt(1); - this.filename = util.decode_utf8(bytes.substr(2, filename_len)); - - this.date = util.readDate(bytes.substr(2 + filename_len, 4)); - - var data = bytes.substring(6 + filename_len); - - this.setBytes(data, format); - }; - - /** - * Creates a string representation of the packet - * - * @param {String} data The data to be inserted as body - * @return {String} string-representation of the packet - */ - this.write = function () { - var filename = util.encode_utf8("msg.txt"); - - var data = this.getBytes(); - - var result = ''; - result += String.fromCharCode(enums.write(enums.literal, this.format)); - result += String.fromCharCode(filename.length); - result += filename; - result += util.writeDate(this.date); - result += data; - return result; - }; +/** + * Set the packet data to a javascript native string, end of line + * will be normalized to \r\n and by default text is converted to UTF8 + * @param {String} text Any native javascript string + */ +Literal.prototype.setText = function (text) { + // normalize EOL to \r\n + text = text.replace(/\r/g, '').replace(/\n/g, '\r\n'); + // encode UTF8 + this.data = this.format == 'utf8' ? util.encode_utf8(text) : text; +}; + +/** + * Returns literal data packets as native JavaScript string + * with normalized end of line to \n + * @return {String} literal data as text + */ +Literal.prototype.getText = function () { + // decode UTF8 + var text = util.decode_utf8(this.data); + // normalize EOL to \n + return text.replace(/\r\n/g, '\n'); +}; + +/** + * Set the packet data to value represented by the provided string of bytes. + * @param {String} bytes The string of bytes + * @param {utf8|binary|text} format The format of the string of bytes + */ +Literal.prototype.setBytes = function (bytes, format) { + this.format = format; + this.data = bytes; +}; + + +/** + * Get the byte sequence representing the literal packet data + * @returns {String} A sequence of bytes + */ +Literal.prototype.getBytes = function () { + return this.data; +}; + + +/** + * Parsing function for a literal data packet (tag 11). + * + * @param {String} input Payload of a tag 11 packet + * @param {Integer} position + * Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/literal} object representation + */ +Literal.prototype.read = function (bytes) { + // - A one-octet field that describes how the data is formatted. + + var format = enums.read(enums.literal, bytes.charCodeAt(0)); + + var filename_len = bytes.charCodeAt(1); + this.filename = util.decode_utf8(bytes.substr(2, filename_len)); + + this.date = util.readDate(bytes.substr(2 + filename_len, 4)); + + var data = bytes.substring(6 + filename_len); + + this.setBytes(data, format); +}; + +/** + * Creates a string representation of the packet + * + * @param {String} data The data to be inserted as body + * @return {String} string-representation of the packet + */ +Literal.prototype.write = function () { + var filename = util.encode_utf8("msg.txt"); + + var data = this.getBytes(); + + var result = ''; + result += String.fromCharCode(enums.write(enums.literal, this.format)); + result += String.fromCharCode(filename.length); + result += filename; + result += util.writeDate(this.date); + result += data; + return result; }; diff --git a/src/packet/marker.js b/src/packet/marker.js index d1770d76..fd38986e 100644 --- a/src/packet/marker.js +++ b/src/packet/marker.js @@ -28,27 +28,30 @@ * @module packet/marker */ +module.exports = Marker; + /** * @constructor */ -module.exports = function marker() { - /** - * Parsing function for a literal data packet (tag 10). - * - * @param {String} input Payload of a tag 10 packet - * @param {Integer} position - * Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/marker} Object representation - */ - this.read = function (bytes) { - if (bytes.charCodeAt(0) == 0x50 && // P - bytes.charCodeAt(1) == 0x47 && // G - bytes.charCodeAt(2) == 0x50) // P - return true; - // marker packet does not contain "PGP" - return false; - }; +function Marker() { +} + +/** + * Parsing function for a literal data packet (tag 10). + * + * @param {String} input Payload of a tag 10 packet + * @param {Integer} position + * Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/marker} Object representation + */ +Marker.prototype.read = function (bytes) { + if (bytes.charCodeAt(0) == 0x50 && // P + bytes.charCodeAt(1) == 0x47 && // G + bytes.charCodeAt(2) == 0x50) // P + return true; + // marker packet does not contain "PGP" + return false; }; diff --git a/src/packet/one_pass_signature.js b/src/packet/one_pass_signature.js index 5e616435..33781f07 100644 --- a/src/packet/one_pass_signature.js +++ b/src/packet/one_pass_signature.js @@ -29,67 +29,69 @@ * @module packet/one_pass_signature */ +module.exports = OnePassSignature; + var enums = require('../enums.js'), type_keyid = require('../type/keyid.js'); /** * @constructor */ -module.exports = function one_pass_signature() { +function OnePassSignature() { this.version = null; // A one-octet version number. The current version is 3. this.type = null; // A one-octet signature type. Signature types are described in RFC4880 Section 5.2.1. this.hashAlgorithm = null; // A one-octet number describing the hash algorithm used. (See RFC4880 9.4) this.publicKeyAlgorithm = null; // A one-octet number describing the public-key algorithm used. (See RFC4880 9.1) this.signingKeyId = null; // An eight-octet number holding the Key ID of the signing key. this.flags = null; // A one-octet number holding a flag showing whether the signature is nested. A zero value indicates that the next packet is another One-Pass Signature packet that describes another signature to be applied to the same message data. +} - /** - * parsing function for a one-pass signature packet (tag 4). - * @param {String} bytes payload of a tag 4 packet - * @return {module:packet/one_pass_signature} object representation - */ - this.read = function (bytes) { - var mypos = 0; - // A one-octet version number. The current version is 3. - this.version = bytes.charCodeAt(mypos++); +/** + * parsing function for a one-pass signature packet (tag 4). + * @param {String} bytes payload of a tag 4 packet + * @return {module:packet/one_pass_signature} object representation + */ +OnePassSignature.prototype.read = function (bytes) { + var mypos = 0; + // A one-octet version number. The current version is 3. + this.version = bytes.charCodeAt(mypos++); - // A one-octet signature type. Signature types are described in - // Section 5.2.1. - this.type = enums.read(enums.signature, bytes.charCodeAt(mypos++)); + // A one-octet signature type. Signature types are described in + // Section 5.2.1. + this.type = enums.read(enums.signature, bytes.charCodeAt(mypos++)); - // A one-octet number describing the hash algorithm used. - this.hashAlgorithm = enums.read(enums.hash, bytes.charCodeAt(mypos++)); + // A one-octet number describing the hash algorithm used. + this.hashAlgorithm = enums.read(enums.hash, bytes.charCodeAt(mypos++)); - // A one-octet number describing the public-key algorithm used. - this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(mypos++)); + // A one-octet number describing the public-key algorithm used. + this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(mypos++)); - // An eight-octet number holding the Key ID of the signing key. - this.signingKeyId = new type_keyid(); - this.signingKeyId.read(bytes.substr(mypos)); - mypos += 8; + // An eight-octet number holding the Key ID of the signing key. + this.signingKeyId = new type_keyid(); + this.signingKeyId.read(bytes.substr(mypos)); + mypos += 8; - // A one-octet number holding a flag showing whether the signature - // is nested. A zero value indicates that the next packet is - // another One-Pass Signature packet that describes another - // signature to be applied to the same message data. - this.flags = bytes.charCodeAt(mypos++); - return this; - }; - - /** - * creates a string representation of a one-pass signature packet - * @return {String} a string representation of a one-pass signature packet - */ - this.write = function () { - var result = ""; - - result += String.fromCharCode(3); - result += String.fromCharCode(enums.write(enums.signature, this.type)); - result += String.fromCharCode(enums.write(enums.hash, this.hashAlgorithm)); - result += String.fromCharCode(enums.write(enums.publicKey, this.publicKeyAlgorithm)); - result += this.signingKeyId.write(); - result += String.fromCharCode(this.flags); - - return result; - }; + // A one-octet number holding a flag showing whether the signature + // is nested. A zero value indicates that the next packet is + // another One-Pass Signature packet that describes another + // signature to be applied to the same message data. + this.flags = bytes.charCodeAt(mypos++); + return this; +}; + +/** + * creates a string representation of a one-pass signature packet + * @return {String} a string representation of a one-pass signature packet + */ +OnePassSignature.prototype.write = function () { + var result = ""; + + result += String.fromCharCode(3); + result += String.fromCharCode(enums.write(enums.signature, this.type)); + result += String.fromCharCode(enums.write(enums.hash, this.hashAlgorithm)); + result += String.fromCharCode(enums.write(enums.publicKey, this.publicKeyAlgorithm)); + result += this.signingKeyId.write(); + result += String.fromCharCode(this.flags); + + return result; }; diff --git a/src/packet/packet.js b/src/packet/packet.js index 317ff0d2..661d5f47 100644 --- a/src/packet/packet.js +++ b/src/packet/packet.js @@ -24,7 +24,6 @@ var enums = require('../enums.js'), util = require('../util'); - module.exports = { readSimpleLength: function(bytes) { var len = 0, diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index 7ed4637f..07284b7f 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -8,6 +8,8 @@ * @module packet/packetlist */ +module.exports = Packetlist; + var packetParser = require('./packet.js'), packets = require('./all_packets.js'), enums = require('../enums.js'); @@ -15,162 +17,161 @@ var packetParser = require('./packet.js'), /** * @constructor */ -module.exports = function packetlist() { +function Packetlist() { /** The number of packets contained within the list. * @readonly * @type {Integer} */ this.length = 0; +} +/** + * Reads a stream of binary data and interprents it as a list of packets. + * @param {String} A binary string of bytes. + */ +Packetlist.prototype.read = function (bytes) { + var i = 0; - /** - * Reads a stream of binary data and interprents it as a list of packets. - * @param {String} A binary string of bytes. - */ - this.read = function (bytes) { - var i = 0; + while (i < bytes.length) { + var parsed = packetParser.read(bytes, i, bytes.length - i); + i = parsed.offset; - while (i < bytes.length) { - var parsed = packetParser.read(bytes, i, bytes.length - i); - i = parsed.offset; + var tag = enums.read(enums.packet, parsed.tag); + var packet = packets.newPacketFromTag(tag); - var tag = enums.read(enums.packet, parsed.tag); - var packet = new packets[tag](); + this.push(packet); - this.push(packet); - - packet.read(parsed.packet); - } - }; - - /** - * Creates a binary representation of openpgp objects contained within the - * class instance. - * @returns {String} A binary string of bytes containing valid openpgp packets. - */ - this.write = function () { - var bytes = ''; - - for (var i = 0; i < this.length; i++) { - var packetbytes = this[i].write(); - bytes += packetParser.writeHeader(this[i].tag, packetbytes.length); - bytes += packetbytes; - } - - return bytes; - }; - - /** - * Adds a packet to the list. This is the only supported method of doing so; - * writing to packetlist[i] directly will result in an error. - */ - this.push = function (packet) { - if (!packet) return; - - packet.packets = packet.packets || new packetlist(); - - this[this.length] = packet; - this.length++; - }; - - /** - * Creates a new packetList with all packets that pass the test implemented by the provided function. - */ - this.filter = function (callback) { - - var filtered = new packetlist(); - - for (var i = 0; i < this.length; i++) { - if (callback(this[i], i, this)) { - filtered.push(this[i]); - } - } - - return filtered; - }; - - /** - * Creates a new packetList with all packets from the given types - */ - this.filterByTag = function () { - var args = Array.prototype.slice.call(arguments); - var filtered = new packetlist(); - var that = this; - - for (var i = 0; i < this.length; i++) { - if (args.some(function(packetType) {return that[i].tag == packetType;})) { - filtered.push(this[i]); - } - } - - return filtered; - }; - - /** - * Executes the provided callback once for each element - */ - this.forEach = function (callback) { - for (var i = 0; i < this.length; i++) { - callback(this[i]); - } - }; - - /** - * Traverses packet tree and returns first matching packet - * @param {module:enums.packet} type The packet type - * @return {module:packet/packet|null} - */ - this.findPacket = function (type) { - var packetlist = this.filterByTag(type); - if (packetlist.length) { - return packetlist[0]; - } else { - var found = null; - for (var i = 0; i < this.length; i++) { - if (this[i].packets.length) { - found = this[i].packets.findPacket(type); - if (found) return found; - } - } - } - return null; - }; - - /** - * Returns array of found indices by tag - */ - this.indexOfTag = function () { - var args = Array.prototype.slice.call(arguments); - var tagIndex = []; - var that = this; - for (var i = 0; i < this.length; i++) { - if (args.some(function(packetType) {return that[i].tag == packetType;})) { - tagIndex.push(i); - } - } - return tagIndex; - }; - - /** - * Returns slice of packetlist - */ - this.slice = function (begin, end) { - if (!end) { - end = this.length; - } - var part = new packetlist(); - for (var i = begin; i < end; i++) { - part.push(this[i]); - } - return part; - }; - - /** - * Concatenates packetlist or array of packets - */ - this.concat = function (packetlist) { - if (packetlist) { - for (var i = 0; i < packetlist.length; i++) { - this.push(packetlist[i]); - } - } - }; + packet.read(parsed.packet); + } +}; + +/** + * Creates a binary representation of openpgp objects contained within the + * class instance. + * @returns {String} A binary string of bytes containing valid openpgp packets. + */ +Packetlist.prototype.write = function () { + var bytes = ''; + + for (var i = 0; i < this.length; i++) { + var packetbytes = this[i].write(); + bytes += packetParser.writeHeader(this[i].tag, packetbytes.length); + bytes += packetbytes; + } + + return bytes; +}; + +/** + * Adds a packet to the list. This is the only supported method of doing so; + * writing to packetlist[i] directly will result in an error. + */ +Packetlist.prototype.push = function (packet) { + if (!packet) return; + + packet.packets = packet.packets || new Packetlist(); + + this[this.length] = packet; + this.length++; +}; + +/** +* Creates a new PacketList with all packets that pass the test implemented by the provided function. +*/ +Packetlist.prototype.filter = function (callback) { + + var filtered = new Packetlist(); + + for (var i = 0; i < this.length; i++) { + if (callback(this[i], i, this)) { + filtered.push(this[i]); + } + } + + return filtered; +}; + +/** +* Creates a new PacketList with all packets from the given types +*/ +Packetlist.prototype.filterByTag = function () { + var args = Array.prototype.slice.call(arguments); + var filtered = new Packetlist(); + var that = this; + + for (var i = 0; i < this.length; i++) { + if (args.some(function(packetType) {return that[i].tag == packetType;})) { + filtered.push(this[i]); + } + } + + return filtered; +}; + +/** +* Executes the provided callback once for each element +*/ +Packetlist.prototype.forEach = function (callback) { + for (var i = 0; i < this.length; i++) { + callback(this[i]); + } +}; + +/** + * Traverses packet tree and returns first matching packet + * @param {module:enums.packet} type The packet type + * @return {module:packet/packet|null} + */ +Packetlist.prototype.findPacket = function (type) { + var packetlist = this.filterByTag(type); + if (packetlist.length) { + return packetlist[0]; + } else { + var found = null; + for (var i = 0; i < this.length; i++) { + if (this[i].packets.length) { + found = this[i].packets.findPacket(type); + if (found) return found; + } + } + } + return null; +}; + +/** + * Returns array of found indices by tag + */ +Packetlist.prototype.indexOfTag = function () { + var args = Array.prototype.slice.call(arguments); + var tagIndex = []; + var that = this; + for (var i = 0; i < this.length; i++) { + if (args.some(function(packetType) {return that[i].tag == packetType;})) { + tagIndex.push(i); + } + } + return tagIndex; +}; + +/** + * Returns slice of packetlist + */ +Packetlist.prototype.slice = function (begin, end) { + if (!end) { + end = this.length; + } + var part = new Packetlist(); + for (var i = begin; i < end; i++) { + part.push(this[i]); + } + return part; +}; + +/** + * Concatenates packetlist or array of packets + */ +Packetlist.prototype.concat = function (packetlist) { + if (packetlist) { + for (var i = 0; i < packetlist.length; i++) { + this.push(packetlist[i]); + } + } }; diff --git a/src/packet/public_key.js b/src/packet/public_key.js index 5813134c..1be16ce2 100644 --- a/src/packet/public_key.js +++ b/src/packet/public_key.js @@ -30,6 +30,8 @@ * @module packet/public_key */ +module.exports = PublicKey; + var util = require('../util'), type_mpi = require('../type/mpi.js'), type_keyid = require('../type/keyid.js'), @@ -39,7 +41,7 @@ var util = require('../util'), /** * @constructor */ -module.exports = function public_key() { +function PublicKey() { this.version = 4; /** Key creation date. * @type {Date} */ @@ -52,136 +54,135 @@ module.exports = function public_key() { this.algorithm = 'rsa_sign'; // time in days (V3 only) this.expirationTimeV3 = 0; +} +/** + * Internal Parser for public keys as specified in RFC 4880 section + * 5.5.2 Public-Key Packet Formats + * called by read_tag<num> + * @param {String} input Input string to read the packet from + * @return {Object} This object with attributes set by the parser + */ +PublicKey.prototype.read = function (bytes) { + var pos = 0; + // A one-octet version number (3 or 4). + this.version = bytes.charCodeAt(pos++); - /** - * Internal Parser for public keys as specified in RFC 4880 section - * 5.5.2 Public-Key Packet Formats - * called by read_tag<num> - * @param {String} input Input string to read the packet from - * @return {Object} This object with attributes set by the parser - */ - this.read = function (bytes) { - var pos = 0; - // A one-octet version number (3 or 4). - this.version = bytes.charCodeAt(pos++); + if (this.version == 3 || this.version == 4) { + // - A four-octet number denoting the time that the key was created. + this.created = util.readDate(bytes.substr(pos, 4)); + pos += 4; - if (this.version == 3 || this.version == 4) { - // - A four-octet number denoting the time that the key was created. - this.created = util.readDate(bytes.substr(pos, 4)); - pos += 4; - - if (this.version == 3) { - // - A two-octet number denoting the time in days that this key is - // valid. If this number is zero, then it does not expire. - this.expirationTimeV3 = util.readNumber(bytes.substr(pos, 2)); - pos += 2; - } - - // - A one-octet number denoting the public-key algorithm of this key. - this.algorithm = enums.read(enums.publicKey, bytes.charCodeAt(pos++)); - - var mpicount = crypto.getPublicMpiCount(this.algorithm); - this.mpi = []; - - var bmpi = bytes.substr(pos); - var p = 0; - - for (var i = 0; i < mpicount && p < bmpi.length; i++) { - - this.mpi[i] = new type_mpi(); - - p += this.mpi[i].read(bmpi.substr(p)); - - if (p > bmpi.length) { - throw new Error('Error reading MPI @:' + p); - } - } - - return p + 6; - } else { - throw new Error('Version ' + version + ' of the key packet is unsupported.'); - } - }; - - /** - * Alias of read() - * @function module:packet/public_key#readPublicKey - * @see module:packet/public_key#read - */ - this.readPublicKey = this.read; - - /** - * Same as write_private_key, but has less information because of - * public key. - * @return {Object} {body: [string]OpenPGP packet body contents, - * header: [string] OpenPGP packet header, string: [string] header+body} - */ - this.write = function () { - // Version - var result = String.fromCharCode(this.version); - result += util.writeDate(this.created); if (this.version == 3) { - result += util.writeNumber(this.expirationTimeV3, 2); + // - A two-octet number denoting the time in days that this key is + // valid. If this number is zero, then it does not expire. + this.expirationTimeV3 = util.readNumber(bytes.substr(pos, 2)); + pos += 2; } - result += String.fromCharCode(enums.write(enums.publicKey, this.algorithm)); + + // - A one-octet number denoting the public-key algorithm of this key. + this.algorithm = enums.read(enums.publicKey, bytes.charCodeAt(pos++)); var mpicount = crypto.getPublicMpiCount(this.algorithm); + this.mpi = []; - for (var i = 0; i < mpicount; i++) { - result += this.mpi[i].write(); - } + var bmpi = bytes.substr(pos); + var p = 0; - return result; - }; + for (var i = 0; i < mpicount && p < bmpi.length; i++) { - /** - * Alias of write() - * @function module:packet/public_key#writePublicKey - * @see module:packet/public_key#write - */ - this.writePublicKey = this.write; + this.mpi[i] = new type_mpi(); - /** - * Write an old version packet - it's used by some of the internal routines. - */ - this.writeOld = function () { - var bytes = this.writePublicKey(); + p += this.mpi[i].read(bmpi.substr(p)); - return String.fromCharCode(0x99) + - util.writeNumber(bytes.length, 2) + - bytes; - }; - - /** - * Calculates the key id of the key - * @return {String} A 8 byte key id - */ - this.getKeyId = function () { - var keyid = new type_keyid(); - if (this.version == 4) { - keyid.read(this.getFingerprint().substr(12, 8)); - } else if (this.version == 3) { - keyid.read(this.mpi[0].write().substr(-8)); - } - return keyid; - }; - - /** - * Calculates the fingerprint of the key - * @return {String} A string containing the fingerprint - */ - this.getFingerprint = function () { - var toHash = ''; - if (this.version == 4) { - toHash = this.writeOld(); - return crypto.hash.sha1(toHash); - } else if (this.version == 3) { - var mpicount = crypto.getPublicMpiCount(this.algorithm); - for (var i = 0; i < mpicount; i++) { - toHash += this.mpi[i].toBytes(); + if (p > bmpi.length) { + throw new Error('Error reading MPI @:' + p); } - return crypto.hash.md5(toHash); } - }; + + return p + 6; + } else { + throw new Error('Version ' + version + ' of the key packet is unsupported.'); + } +}; + +/** + * Alias of read() + * @function module:packet/public_key#readPublicKey + * @see module:packet/public_key#read + */ +PublicKey.prototype.readPublicKey = PublicKey.prototype.read; + +/** + * Same as write_private_key, but has less information because of + * public key. + * @return {Object} {body: [string]OpenPGP packet body contents, + * header: [string] OpenPGP packet header, string: [string] header+body} + */ +PublicKey.prototype.write = function () { + // Version + var result = String.fromCharCode(this.version); + result += util.writeDate(this.created); + if (this.version == 3) { + result += util.writeNumber(this.expirationTimeV3, 2); + } + result += String.fromCharCode(enums.write(enums.publicKey, this.algorithm)); + + var mpicount = crypto.getPublicMpiCount(this.algorithm); + + for (var i = 0; i < mpicount; i++) { + result += this.mpi[i].write(); + } + + return result; +}; + +/** + * Alias of write() + * @function module:packet/public_key#writePublicKey + * @see module:packet/public_key#write + */ +PublicKey.prototype.writePublicKey = PublicKey.prototype.write; + +/** + * Write an old version packet - it's used by some of the internal routines. + */ +PublicKey.prototype.writeOld = function () { + var bytes = this.writePublicKey(); + + return String.fromCharCode(0x99) + + util.writeNumber(bytes.length, 2) + + bytes; +}; + +/** + * Calculates the key id of the key + * @return {String} A 8 byte key id + */ +PublicKey.prototype.getKeyId = function () { + var keyid = new type_keyid(); + if (this.version == 4) { + keyid.read(this.getFingerprint().substr(12, 8)); + } else if (this.version == 3) { + keyid.read(this.mpi[0].write().substr(-8)); + } + return keyid; +}; + +/** + * Calculates the fingerprint of the key + * @return {String} A string containing the fingerprint + */ +PublicKey.prototype.getFingerprint = function () { + var toHash = ''; + if (this.version == 4) { + toHash = this.writeOld(); + return crypto.hash.sha1(toHash); + } else if (this.version == 3) { + var mpicount = crypto.getPublicMpiCount(this.algorithm); + for (var i = 0; i < mpicount; i++) { + toHash += this.mpi[i].toBytes(); + } + return crypto.hash.md5(toHash); + } }; diff --git a/src/packet/public_key_encrypted_session_key.js b/src/packet/public_key_encrypted_session_key.js index 2aa42b72..5fc27097 100644 --- a/src/packet/public_key_encrypted_session_key.js +++ b/src/packet/public_key_encrypted_session_key.js @@ -37,6 +37,8 @@ * @module packet/public_key_encrypted_session_key */ +module.exports = PublicKeyEncryptedSessionKey; + var type_keyid = require('../type/keyid.js'), util = require('../util'), type_mpi = require('../type/mpi.js'), @@ -46,7 +48,7 @@ var type_keyid = require('../type/keyid.js'), /** * @constructor */ -module.exports = function public_key_encrypted_session_key() { +function PublicKeyEncryptedSessionKey() { this.version = 3; this.publicKeyId = new type_keyid(); @@ -57,126 +59,126 @@ module.exports = function public_key_encrypted_session_key() { /** @type {Array} */ this.encrypted = []; +} - /** - * Parsing function for a publickey encrypted session key packet (tag 1). - * - * @param {String} input Payload of a tag 1 packet - * @param {Integer} position Position to start reading from the input string - * @param {Integer} len Length of the packet or the remaining length of - * input at position - * @return {module:packet/public_key_encrypted_session_key} Object representation - */ - this.read = function (bytes) { +/** + * Parsing function for a publickey encrypted session key packet (tag 1). + * + * @param {String} input Payload of a tag 1 packet + * @param {Integer} position Position to start reading from the input string + * @param {Integer} len Length of the packet or the remaining length of + * input at position + * @return {module:packet/public_key_encrypted_session_key} Object representation + */ +PublicKeyEncryptedSessionKey.prototype.read = function (bytes) { - this.version = bytes.charCodeAt(0); - this.publicKeyId.read(bytes.substr(1)); - this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(9)); + this.version = bytes.charCodeAt(0); + this.publicKeyId.read(bytes.substr(1)); + this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes.charCodeAt(9)); - var i = 10; + var i = 10; - var integerCount = (function(algo) { - switch (algo) { - case 'rsa_encrypt': - case 'rsa_encrypt_sign': - return 1; + var integerCount = (function(algo) { + switch (algo) { + case 'rsa_encrypt': + case 'rsa_encrypt_sign': + return 1; - case 'elgamal': - return 2; + case 'elgamal': + return 2; - default: - throw new Error("Invalid algorithm."); - } - })(this.publicKeyAlgorithm); - - this.encrypted = []; - - for (var j = 0; j < integerCount; j++) { - var mpi = new type_mpi(); - i += mpi.read(bytes.substr(i)); - this.encrypted.push(mpi); + default: + throw new Error("Invalid algorithm."); } - }; + })(this.publicKeyAlgorithm); - /** - * Create a string representation of a tag 1 packet - * - * @param {String} publicKeyId - * The public key id corresponding to publicMPIs key as string - * @param {Array} publicMPIs - * Multiprecision integer objects describing the public key - * @param {Integer} pubalgo - * The corresponding public key algorithm // See RFC4880 9.1 - * @param {Integer} symmalgo - * The symmetric cipher algorithm used to encrypt the data - * within an encrypteddatapacket or encryptedintegrity- - * protecteddatapacket - * following this packet //See RFC4880 9.2 - * @param {String} sessionkey - * A string of randombytes representing the session key - * @return {String} The string representation - */ - this.write = function () { - - var result = String.fromCharCode(this.version); - result += this.publicKeyId.write(); - result += String.fromCharCode( - enums.write(enums.publicKey, this.publicKeyAlgorithm)); - - for (var i = 0; i < this.encrypted.length; i++) { - result += this.encrypted[i].write(); - } - - return result; - }; - - this.encrypt = function (key) { - var data = String.fromCharCode( - enums.write(enums.symmetric, this.sessionKeyAlgorithm)); - - data += this.sessionKey; - var checksum = util.calc_checksum(this.sessionKey); - data += util.writeNumber(checksum, 2); + this.encrypted = []; + for (var j = 0; j < integerCount; j++) { var mpi = new type_mpi(); - mpi.fromBytes(crypto.pkcs1.eme.encode( - data, - key.mpi[0].byteLength())); - - this.encrypted = crypto.publicKeyEncrypt( - this.publicKeyAlgorithm, - key.mpi, - mpi); - }; - - /** - * Decrypts the session key (only for public key encrypted session key - * packets (tag 1) - * - * @param {module:packet/secret_key} key - * Private key with secMPIs unlocked - * @return {String} The unencrypted session key - */ - this.decrypt = function (key) { - var result = crypto.publicKeyDecrypt( - this.publicKeyAlgorithm, - key.mpi, - this.encrypted).toBytes(); - - var checksum = util.readNumber(result.substr(result.length - 2)); - - var decoded = crypto.pkcs1.eme.decode( - result, - key.mpi[0].byteLength()); - - key = decoded.substring(1, decoded.length - 2); - - if (checksum != util.calc_checksum(key)) { - throw new Error('Checksum mismatch'); - } else { - this.sessionKey = key; - this.sessionKeyAlgorithm = - enums.read(enums.symmetric, decoded.charCodeAt(0)); - } - }; + i += mpi.read(bytes.substr(i)); + this.encrypted.push(mpi); + } +}; + +/** + * Create a string representation of a tag 1 packet + * + * @param {String} publicKeyId + * The public key id corresponding to publicMPIs key as string + * @param {Array} publicMPIs + * Multiprecision integer objects describing the public key + * @param {Integer} pubalgo + * The corresponding public key algorithm // See RFC4880 9.1 + * @param {Integer} symmalgo + * The symmetric cipher algorithm used to encrypt the data + * within an encrypteddatapacket or encryptedintegrity- + * protecteddatapacket + * following this packet //See RFC4880 9.2 + * @param {String} sessionkey + * A string of randombytes representing the session key + * @return {String} The string representation + */ +PublicKeyEncryptedSessionKey.prototype.write = function () { + + var result = String.fromCharCode(this.version); + result += this.publicKeyId.write(); + result += String.fromCharCode( + enums.write(enums.publicKey, this.publicKeyAlgorithm)); + + for (var i = 0; i < this.encrypted.length; i++) { + result += this.encrypted[i].write(); + } + + return result; +}; + +PublicKeyEncryptedSessionKey.prototype.encrypt = function (key) { + var data = String.fromCharCode( + enums.write(enums.symmetric, this.sessionKeyAlgorithm)); + + data += this.sessionKey; + var checksum = util.calc_checksum(this.sessionKey); + data += util.writeNumber(checksum, 2); + + var mpi = new type_mpi(); + mpi.fromBytes(crypto.pkcs1.eme.encode( + data, + key.mpi[0].byteLength())); + + this.encrypted = crypto.publicKeyEncrypt( + this.publicKeyAlgorithm, + key.mpi, + mpi); +}; + +/** + * Decrypts the session key (only for public key encrypted session key + * packets (tag 1) + * + * @param {module:packet/secret_key} key + * Private key with secMPIs unlocked + * @return {String} The unencrypted session key + */ +PublicKeyEncryptedSessionKey.prototype.decrypt = function (key) { + var result = crypto.publicKeyDecrypt( + this.publicKeyAlgorithm, + key.mpi, + this.encrypted).toBytes(); + + var checksum = util.readNumber(result.substr(result.length - 2)); + + var decoded = crypto.pkcs1.eme.decode( + result, + key.mpi[0].byteLength()); + + key = decoded.substring(1, decoded.length - 2); + + if (checksum != util.calc_checksum(key)) { + throw new Error('Checksum mismatch'); + } else { + this.sessionKey = key; + this.sessionKeyAlgorithm = + enums.read(enums.symmetric, decoded.charCodeAt(0)); + } }; diff --git a/src/packet/public_subkey.js b/src/packet/public_subkey.js index db36cad7..8ef4e213 100644 --- a/src/packet/public_subkey.js +++ b/src/packet/public_subkey.js @@ -20,12 +20,17 @@ * @module packet/public_subkey */ +module.exports = PublicSubkey; + var publicKey = require('./public_key.js'); /** * @constructor * @extends module:packet/public_key */ -module.exports = function public_subkey() { +function PublicSubkey() { publicKey.call(this); }; + +PublicSubkey.prototype = new publicKey(); +PublicSubkey.prototype.constructor = PublicSubkey(); diff --git a/src/packet/secret_key.js b/src/packet/secret_key.js index 7e477835..243e9baa 100644 --- a/src/packet/secret_key.js +++ b/src/packet/secret_key.js @@ -42,225 +42,226 @@ var publicKey = require('./public_key.js'), * @constructor * @extends module:packet/public_key */ -module.exports = function secret_key() { +module.exports = SecretKey; + +function SecretKey() { publicKey.call(this); // encrypted secret-key data this.encrypted = null; // indicator if secret-key data is available in decrypted form this.isDecrypted = false; +} +SecretKey.prototype = new publicKey(); +SecretKey.prototype.constructor = SecretKey(); - function get_hash_len(hash) { - if (hash == 'sha1') - return 20; - else - return 2; - } - - function get_hash_fn(hash) { - if (hash == 'sha1') - return crypto.hash.sha1; - else - return function(c) { - return util.writeNumber(util.calc_checksum(c), 2); - }; - } - - // Helper function - - function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) { - var hashlen = get_hash_len(hash_algorithm), - hashfn = get_hash_fn(hash_algorithm); - - var hashtext = cleartext.substr(cleartext.length - hashlen); - cleartext = cleartext.substr(0, cleartext.length - hashlen); - - var hash = hashfn(cleartext); - - if (hash != hashtext) - return new Error("Hash mismatch."); - - var mpis = crypto.getPrivateMpiCount(algorithm); - - var j = 0; - var mpi = []; - - for (var i = 0; i < mpis && j < cleartext.length; i++) { - mpi[i] = new type_mpi(); - j += mpi[i].read(cleartext.substr(j)); - } - - return mpi; - } - - function write_cleartext_mpi(hash_algorithm, algorithm, mpi) { - var bytes = ''; - var discard = crypto.getPublicMpiCount(algorithm); - - for (var i = discard; i < mpi.length; i++) { - bytes += mpi[i].write(); - } - - - bytes += get_hash_fn(hash_algorithm)(bytes); - - return bytes; - } - - - // 5.5.3. Secret-Key Packet Formats - - /** - * Internal parser for private keys as specified in RFC 4880 section 5.5.3 - * @param {String} bytes Input string to read the packet from - */ - this.read = function (bytes) { - // - A Public-Key or Public-Subkey packet, as described above. - var len = this.readPublicKey(bytes); - - bytes = bytes.substr(len); - - - // - One octet indicating string-to-key usage conventions. Zero - // indicates that the secret-key data is not encrypted. 255 or 254 - // indicates that a string-to-key specifier is being given. Any - // other value is a symmetric-key encryption algorithm identifier. - var isEncrypted = bytes.charCodeAt(0); - - if (isEncrypted) { - this.encrypted = bytes; - } else { - - // - Plain or encrypted multiprecision integers comprising the secret - // key data. These algorithm-specific fields are as described - // below. - var parsedMPI = parse_cleartext_mpi('mod', bytes.substr(1), this.algorithm); - if (parsedMPI instanceof Error) - throw parsedMPI; - this.mpi = this.mpi.concat(parsedMPI); - this.isDecrypted = true; - } +function get_hash_len(hash) { + if (hash == 'sha1') + return 20; + else + return 2; +} +function get_hash_fn(hash) { + if (hash == 'sha1') + return crypto.hash.sha1; + else + return function(c) { + return util.writeNumber(util.calc_checksum(c), 2); }; +} - /** Creates an OpenPGP key packet for the given key. - * @return {String} A string of bytes containing the secret key OpenPGP packet - */ - this.write = function () { - var bytes = this.writePublicKey(); +// Helper function - if (!this.encrypted) { - bytes += String.fromCharCode(0); +function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) { + var hashlen = get_hash_len(hash_algorithm), + hashfn = get_hash_fn(hash_algorithm); - bytes += write_cleartext_mpi('mod', this.algorithm, this.mpi); - } else { - bytes += this.encrypted; - } + var hashtext = cleartext.substr(cleartext.length - hashlen); + cleartext = cleartext.substr(0, cleartext.length - hashlen); - return bytes; - }; + var hash = hashfn(cleartext); + if (hash != hashtext) + return new Error("Hash mismatch."); + var mpis = crypto.getPrivateMpiCount(algorithm); + var j = 0; + var mpi = []; - /** Encrypt the payload. By default, we use aes256 and iterated, salted string - * to key specifier - * @param {String} passphrase - */ - this.encrypt = function (passphrase) { - - var s2k = new type_s2k(), - symmetric = 'aes256', - cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi), - key = produceEncryptionKey(s2k, passphrase, symmetric), - blockLen = crypto.cipher[symmetric].blockSize, - iv = crypto.random.getRandomBytes(blockLen); - - - this.encrypted = ''; - this.encrypted += String.fromCharCode(254); - this.encrypted += String.fromCharCode(enums.write(enums.symmetric, symmetric)); - this.encrypted += s2k.write(); - this.encrypted += iv; - - this.encrypted += crypto.cfb.normalEncrypt(symmetric, key, cleartext, iv); - }; - - function produceEncryptionKey(s2k, passphrase, algorithm) { - return s2k.produce_key(passphrase, - crypto.cipher[algorithm].keySize); + for (var i = 0; i < mpis && j < cleartext.length; i++) { + mpi[i] = new type_mpi(); + j += mpi[i].read(cleartext.substr(j)); } - /** - * Decrypts the private key MPIs which are needed to use the key. - * @link module:packet/secret_key.isDecrypted should be - * false otherwise a call to this function is not needed - * - * @param {String} str_passphrase The passphrase for this private key - * as string - * @return {Boolean} True if the passphrase was correct or MPI already - * decrypted; false if not - */ - this.decrypt = function (passphrase) { - if (this.isDecrypted) - return true; + return mpi; +} - var i = 0, - symmetric, - key; +function write_cleartext_mpi(hash_algorithm, algorithm, mpi) { + var bytes = ''; + var discard = crypto.getPublicMpiCount(algorithm); - var s2k_usage = this.encrypted.charCodeAt(i++); - - // - [Optional] If string-to-key usage octet was 255 or 254, a one- - // octet symmetric encryption algorithm. - if (s2k_usage == 255 || s2k_usage == 254) { - symmetric = this.encrypted.charCodeAt(i++); - symmetric = enums.read(enums.symmetric, symmetric); - - // - [Optional] If string-to-key usage octet was 255 or 254, a - // string-to-key specifier. The length of the string-to-key - // specifier is implied by its type, as described above. - var s2k = new type_s2k(); - i += s2k.read(this.encrypted.substr(i)); - - key = produceEncryptionKey(s2k, passphrase, symmetric); - } else { - symmetric = s2k_usage; - symmetric = enums.read(enums.symmetric, symmetric); - key = crypto.hash.md5(passphrase); - } + for (var i = discard; i < mpi.length; i++) { + bytes += mpi[i].write(); + } - // - [Optional] If secret data is encrypted (string-to-key usage octet - // not zero), an Initial Vector (IV) of the same length as the - // cipher's block size. - var iv = this.encrypted.substr(i, - crypto.cipher[symmetric].blockSize); + bytes += get_hash_fn(hash_algorithm)(bytes); - i += iv.length; + return bytes; +} - var cleartext, - ciphertext = this.encrypted.substr(i); - cleartext = crypto.cfb.normalDecrypt(symmetric, key, ciphertext, iv); +// 5.5.3. Secret-Key Packet Formats - var hash = s2k_usage == 254 ? - 'sha1' : - 'mod'; +/** + * Internal parser for private keys as specified in RFC 4880 section 5.5.3 + * @param {String} bytes Input string to read the packet from + */ +SecretKey.prototype.read = function (bytes) { + // - A Public-Key or Public-Subkey packet, as described above. + var len = this.readPublicKey(bytes); - var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm); + bytes = bytes.substr(len); + + + // - One octet indicating string-to-key usage conventions. Zero + // indicates that the secret-key data is not encrypted. 255 or 254 + // indicates that a string-to-key specifier is being given. Any + // other value is a symmetric-key encryption algorithm identifier. + var isEncrypted = bytes.charCodeAt(0); + + if (isEncrypted) { + this.encrypted = bytes; + } else { + + // - Plain or encrypted multiprecision integers comprising the secret + // key data. These algorithm-specific fields are as described + // below. + var parsedMPI = parse_cleartext_mpi('mod', bytes.substr(1), this.algorithm); if (parsedMPI instanceof Error) - return false; + throw parsedMPI; this.mpi = this.mpi.concat(parsedMPI); this.isDecrypted = true; - return true; - }; - - this.generate = function (bits) { - this.mpi = crypto.generateMpi(this.algorithm, bits); - this.isDecrypted = true; - }; + } }; -module.exports.prototype = new publicKey(); +/** Creates an OpenPGP key packet for the given key. + * @return {String} A string of bytes containing the secret key OpenPGP packet + */ +SecretKey.prototype.write = function () { + var bytes = this.writePublicKey(); + + if (!this.encrypted) { + bytes += String.fromCharCode(0); + + bytes += write_cleartext_mpi('mod', this.algorithm, this.mpi); + } else { + bytes += this.encrypted; + } + + return bytes; +}; + + + + +/** Encrypt the payload. By default, we use aes256 and iterated, salted string + * to key specifier + * @param {String} passphrase + */ +SecretKey.prototype.encrypt = function (passphrase) { + + var s2k = new type_s2k(), + symmetric = 'aes256', + cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi), + key = produceEncryptionKey(s2k, passphrase, symmetric), + blockLen = crypto.cipher[symmetric].blockSize, + iv = crypto.random.getRandomBytes(blockLen); + + + this.encrypted = ''; + this.encrypted += String.fromCharCode(254); + this.encrypted += String.fromCharCode(enums.write(enums.symmetric, symmetric)); + this.encrypted += s2k.write(); + this.encrypted += iv; + + this.encrypted += crypto.cfb.normalEncrypt(symmetric, key, cleartext, iv); +}; + +function produceEncryptionKey(s2k, passphrase, algorithm) { + return s2k.produce_key(passphrase, + crypto.cipher[algorithm].keySize); +} + +/** + * Decrypts the private key MPIs which are needed to use the key. + * @link module:packet/secret_key.isDecrypted should be + * false otherwise a call to this function is not needed + * + * @param {String} str_passphrase The passphrase for this private key + * as string + * @return {Boolean} True if the passphrase was correct or MPI already + * decrypted; false if not + */ +SecretKey.prototype.decrypt = function (passphrase) { + if (this.isDecrypted) + return true; + + var i = 0, + symmetric, + key; + + var s2k_usage = this.encrypted.charCodeAt(i++); + + // - [Optional] If string-to-key usage octet was 255 or 254, a one- + // octet symmetric encryption algorithm. + if (s2k_usage == 255 || s2k_usage == 254) { + symmetric = this.encrypted.charCodeAt(i++); + symmetric = enums.read(enums.symmetric, symmetric); + + // - [Optional] If string-to-key usage octet was 255 or 254, a + // string-to-key specifier. The length of the string-to-key + // specifier is implied by its type, as described above. + var s2k = new type_s2k(); + i += s2k.read(this.encrypted.substr(i)); + + key = produceEncryptionKey(s2k, passphrase, symmetric); + } else { + symmetric = s2k_usage; + symmetric = enums.read(enums.symmetric, symmetric); + key = crypto.hash.md5(passphrase); + } + + + // - [Optional] If secret data is encrypted (string-to-key usage octet + // not zero), an Initial Vector (IV) of the same length as the + // cipher's block size. + var iv = this.encrypted.substr(i, + crypto.cipher[symmetric].blockSize); + + i += iv.length; + + var cleartext, + ciphertext = this.encrypted.substr(i); + + cleartext = crypto.cfb.normalDecrypt(symmetric, key, ciphertext, iv); + + var hash = s2k_usage == 254 ? + 'sha1' : + 'mod'; + + var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm); + if (parsedMPI instanceof Error) + return false; + this.mpi = this.mpi.concat(parsedMPI); + this.isDecrypted = true; + return true; +}; + +SecretKey.prototype.generate = function (bits) { + this.mpi = crypto.generateMpi(this.algorithm, bits); + this.isDecrypted = true; +}; diff --git a/src/packet/secret_subkey.js b/src/packet/secret_subkey.js index 84d72306..614c6288 100644 --- a/src/packet/secret_subkey.js +++ b/src/packet/secret_subkey.js @@ -20,12 +20,17 @@ * @module packet/secret_subkey */ +module.exports = SecretSubkey; + var secretKey = require('./secret_key.js'); /** * @constructor * @extends module:packet/secret_key */ -module.exports = function secret_subkey() { +function SecretSubkey() { secretKey.call(this); -}; +} + +SecretSubkey.prototype = new secretKey(); +SecretSubkey.prototype.constructor = SecretSubkey; diff --git a/src/packet/signature.js b/src/packet/signature.js index 8c35c48b..c6f15df3 100644 --- a/src/packet/signature.js +++ b/src/packet/signature.js @@ -31,6 +31,8 @@ * @module packet/signature */ +module.exports = Signature; + var util = require('../util'), packet = require('./packet.js'), enums = require('../enums.js'), @@ -41,7 +43,7 @@ var util = require('../util'), /** * @constructor */ -module.exports = function signature() { +function Signature() { this.version = 4; this.signatureType = null; @@ -85,555 +87,554 @@ module.exports = function signature() { this.embeddedSignature = null; this.verified = false; +} - /** - * parsing function for a signature packet (tag 2). - * @param {String} bytes payload of a tag 2 packet - * @param {Integer} position position to start reading from the bytes string - * @param {Integer} len length of the packet or the remaining length of bytes at position - * @return {module:packet/signature} object representation - */ - this.read = function (bytes) { - var i = 0; +/** + * parsing function for a signature packet (tag 2). + * @param {String} bytes payload of a tag 2 packet + * @param {Integer} position position to start reading from the bytes string + * @param {Integer} len length of the packet or the remaining length of bytes at position + * @return {module:packet/signature} object representation + */ +Signature.prototype.read = function (bytes) { + var i = 0; - this.version = bytes.charCodeAt(i++); - // switch on version (3 and 4) - switch (this.version) { - case 3: - // One-octet length of following hashed material. MUST be 5. - if (bytes.charCodeAt(i++) != 5) - util.print_debug("packet/signature.js\n" + - 'invalid One-octet length of following hashed material.' + - 'MUST be 5. @:' + (i - 1)); + this.version = bytes.charCodeAt(i++); + // switch on version (3 and 4) + switch (this.version) { + case 3: + // One-octet length of following hashed material. MUST be 5. + if (bytes.charCodeAt(i++) != 5) + util.print_debug("packet/signature.js\n" + + 'invalid One-octet length of following hashed material.' + + 'MUST be 5. @:' + (i - 1)); - var sigpos = i; - // One-octet signature type. - this.signatureType = bytes.charCodeAt(i++); + var sigpos = i; + // One-octet signature type. + this.signatureType = bytes.charCodeAt(i++); - // Four-octet creation time. - this.created = util.readDate(bytes.substr(i, 4)); - i += 4; + // Four-octet creation time. + this.created = util.readDate(bytes.substr(i, 4)); + i += 4; - // storing data appended to data which gets verified - this.signatureData = bytes.substring(sigpos, i); + // storing data appended to data which gets verified + this.signatureData = bytes.substring(sigpos, i); - // Eight-octet Key ID of signer. - this.issuerKeyId.read(bytes.substring(i, i + 8)); - i += 8; + // Eight-octet Key ID of signer. + this.issuerKeyId.read(bytes.substring(i, i + 8)); + i += 8; - // One-octet public-key algorithm. - this.publicKeyAlgorithm = bytes.charCodeAt(i++); + // One-octet public-key algorithm. + this.publicKeyAlgorithm = bytes.charCodeAt(i++); - // One-octet hash algorithm. - this.hashAlgorithm = bytes.charCodeAt(i++); - break; - case 4: - this.signatureType = bytes.charCodeAt(i++); - this.publicKeyAlgorithm = bytes.charCodeAt(i++); - this.hashAlgorithm = bytes.charCodeAt(i++); + // One-octet hash algorithm. + this.hashAlgorithm = bytes.charCodeAt(i++); + break; + case 4: + this.signatureType = bytes.charCodeAt(i++); + this.publicKeyAlgorithm = bytes.charCodeAt(i++); + this.hashAlgorithm = bytes.charCodeAt(i++); - function subpackets(bytes) { - // Two-octet scalar octet count for following subpacket data. - var subpacket_length = util.readNumber( - bytes.substr(0, 2)); + function subpackets(bytes) { + // Two-octet scalar octet count for following subpacket data. + var subpacket_length = util.readNumber( + bytes.substr(0, 2)); - var i = 2; + var i = 2; - // subpacket data set (zero or more subpackets) - var subpacked_read = 0; - while (i < 2 + subpacket_length) { + // subpacket data set (zero or more subpackets) + var subpacked_read = 0; + while (i < 2 + subpacket_length) { - var len = packet.readSimpleLength(bytes.substr(i)); - i += len.offset; + var len = packet.readSimpleLength(bytes.substr(i)); + i += len.offset; - this.read_sub_packet(bytes.substr(i, len.len)); + this.read_sub_packet(bytes.substr(i, len.len)); - i += len.len; - } - - return i; + i += len.len; } - // hashed subpackets - i += subpackets.call(this, bytes.substr(i), true); - - // A V4 signature hashes the packet body - // starting from its first field, the version number, through the end - // of the hashed subpacket data. Thus, the fields hashed are the - // signature version, the signature type, the public-key algorithm, the - // hash algorithm, the hashed subpacket length, and the hashed - // subpacket body. - this.signatureData = bytes.substr(0, i); - - // unhashed subpackets - i += subpackets.call(this, bytes.substr(i), false); - - break; - default: - throw new Error('Version ' + version + ' of the signature is unsupported.'); - } - - // Two-octet field holding left 16 bits of signed hash value. - this.signedHashValue = bytes.substr(i, 2); - i += 2; - - this.signature = bytes.substr(i); - }; - - this.write = function () { - return this.signatureData + - util.writeNumber(0, 2) + // Number of unsigned subpackets. - this.signedHashValue + - this.signature; - }; - - /** - * Signs provided data. This needs to be done prior to serialization. - * @param {module:packet/secret_key} key private key used to sign the message. - * @param {Object} data Contains packets to be signed. - */ - this.sign = function (key, data) { - var signatureType = enums.write(enums.signature, this.signatureType), - publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), - hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); - - var result = String.fromCharCode(4); - result += String.fromCharCode(signatureType); - result += String.fromCharCode(publicKeyAlgorithm); - result += String.fromCharCode(hashAlgorithm); - - this.issuerKeyId = key.getKeyId(); - - // Add hashed subpackets - result += this.write_all_sub_packets(); - - this.signatureData = result; - - var trailer = this.calculateTrailer(); - - var toHash = this.toSign(signatureType, data) + - this.signatureData + trailer; - - var hash = crypto.hash.digest(hashAlgorithm, toHash); - - this.signedHashValue = hash.substr(0, 2); - - this.signature = crypto.signature.sign(hashAlgorithm, - publicKeyAlgorithm, key.mpi, toHash); - }; - - /** - * Creates string of bytes with all subpacket data - * @return {String} a string-representation of a all subpacket data - */ - this.write_all_sub_packets = function () { - var sub = enums.signatureSubpacket; - var result = ''; - var bytes = ''; - if (this.created !== null) { - result += write_sub_packet(sub.signature_creation_time, util.writeDate(this.created)); - } - if (this.signatureExpirationTime !== null) { - result += write_sub_packet(sub.signature_expiration_time, util.writeNumber(this.signatureExpirationTime, 4)); - } - if (this.exportable !== null) { - result += write_sub_packet(sub.exportable_certification, String.fromCharCode(this.exportable ? 1 : 0)); - } - if (this.trustLevel !== null) { - bytes = String.fromCharCode(this.trustLevel) + String.fromCharCode(this.trustAmount); - result += write_sub_packet(sub.trust_signature, bytes); - } - if (this.regularExpression !== null) { - result += write_sub_packet(sub.regular_expression, this.regularExpression); - } - if (this.revocable !== null) { - result += write_sub_packet(sub.revocable, String.fromCharCode(this.revocable ? 1 : 0)); - } - if (this.keyExpirationTime !== null) { - result += write_sub_packet(sub.key_expiration_time, util.writeNumber(this.keyExpirationTime, 4)); - } - if (this.preferredSymmetricAlgorithms !== null) { - bytes = util.bin2str(this.preferredSymmetricAlgorithms); - result += write_sub_packet(sub.preferred_symmetric_algorithms, bytes); - } - if (this.revocationKeyClass !== null) { - bytes = String.fromCharCode(this.revocationKeyClass); - bytes += String.fromCharCode(this.revocationKeyAlgorithm); - bytes += this.revocationKeyFingerprint; - result += write_sub_packet(sub.revocation_key, bytes); - } - if (!this.issuerKeyId.isNull()) { - result += write_sub_packet(sub.issuer, this.issuerKeyId.write()); - } - if (this.notation !== null) { - for (var name in this.notation) { - if (this.notation.hasOwnProperty(name)) { - var value = this.notation[name]; - bytes = String.fromCharCode(0x80); - bytes += String.fromCharCode(0); - bytes += String.fromCharCode(0); - bytes += String.fromCharCode(0); - // 2 octets of name length - bytes += util.writeNumber(name.length, 2); - // 2 octets of value length - bytes += util.writeNumber(value.length, 2); - bytes += name + value; - result += write_sub_packet(sub.notation_data, bytes); - } + return i; } - } - if (this.preferredHashAlgorithms !== null) { - bytes = util.bin2str(this.preferredHashAlgorithms); - result += write_sub_packet(sub.preferred_hash_algorithms, bytes); - } - if (this.preferredCompressionAlgorithms !== null) { - bytes = util.bin2str(this.preferredCompressionAlgorithms); - result += write_sub_packet(sub.preferred_hash_algorithms, bytes); - } - if (this.keyServerPreferences !== null) { - bytes = util.bin2str(this.keyServerPreferences); - result += write_sub_packet(sub.key_server_preferences, bytes); - } - if (this.preferredKeyServer !== null) { - result += write_sub_packet(sub.preferred_key_server, this.preferredKeyServer); - } - if (this.isPrimaryUserID !== null) { - result += write_sub_packet(sub.primary_user_id, String.fromCharCode(this.isPrimaryUserID ? 1 : 0)); - } - if (this.policyURI !== null) { - result += write_sub_packet(sub.policy_uri, this.policyURI); - } - if (this.keyFlags !== null) { - bytes = util.bin2str(this.keyFlags); - result += write_sub_packet(sub.key_flags, bytes); - } - if (this.signersUserId !== null) { - result += write_sub_packet(sub.signers_user_id, this.signersUserId); - } - if (this.reasonForRevocationFlag !== null) { - bytes = String.fromCharCode(this.reasonForRevocationFlag); - bytes += this.reasonForRevocationString; - result += write_sub_packet(sub.reason_for_revocation, bytes); - } - if (this.features !== null) { - bytes = util.bin2str(this.features); - result += write_sub_packet(sub.features, bytes); - } - if (this.signatureTargetPublicKeyAlgorithm !== null) { - bytes = String.fromCharCode(this.signatureTargetPublicKeyAlgorithm); - bytes += String.fromCharCode(this.signatureTargetHashAlgorithm); - bytes += this.signatureTargetHash; - result += write_sub_packet(sub.signature_target, bytes); - } - if (this.embeddedSignature !== null) { - result += write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()); - } - result = util.writeNumber(result.length, 2) + result; - return result; - }; - /** - * creates a string representation of a sub signature packet (See RFC 4880 5.2.3.1) - * @param {Integer} type subpacket signature type. Signature types as described - * in RFC4880 Section 5.2.3.2 - * @param {String} data data to be included - * @return {String} a string-representation of a sub signature packet (See RFC 4880 5.2.3.1) - */ - function write_sub_packet(type, data) { - var result = ""; - result += packet.writeSimpleLength(data.length + 1); - result += String.fromCharCode(type); - result += data; - return result; + // hashed subpackets + i += subpackets.call(this, bytes.substr(i), true); + + // A V4 signature hashes the packet body + // starting from its first field, the version number, through the end + // of the hashed subpacket data. Thus, the fields hashed are the + // signature version, the signature type, the public-key algorithm, the + // hash algorithm, the hashed subpacket length, and the hashed + // subpacket body. + this.signatureData = bytes.substr(0, i); + + // unhashed subpackets + i += subpackets.call(this, bytes.substr(i), false); + + break; + default: + throw new Error('Version ' + version + ' of the signature is unsupported.'); } - // V4 signature sub packets + // Two-octet field holding left 16 bits of signed hash value. + this.signedHashValue = bytes.substr(i, 2); + i += 2; - this.read_sub_packet = function (bytes) { - var mypos = 0; - - function read_array(prop, bytes) { - this[prop] = []; - - for (var i = 0; i < bytes.length; i++) { - this[prop].push(bytes.charCodeAt(i)); - } - } - - // The leftwost bit denotes a "critical" packet, but we ignore it. - var type = bytes.charCodeAt(mypos++) & 0x7F; - var seconds; - - // subpacket type - switch (type) { - case 2: - // Signature Creation Time - this.created = util.readDate(bytes.substr(mypos)); - break; - case 3: - // Signature Expiration Time in seconds - seconds = util.readNumber(bytes.substr(mypos)); - - this.signatureNeverExpires = seconds === 0; - this.signatureExpirationTime = seconds; - - break; - case 4: - // Exportable Certification - this.exportable = bytes.charCodeAt(mypos++) == 1; - break; - case 5: - // Trust Signature - this.trustLevel = bytes.charCodeAt(mypos++); - this.trustAmount = bytes.charCodeAt(mypos++); - break; - case 6: - // Regular Expression - this.regularExpression = bytes.substr(mypos); - break; - case 7: - // Revocable - this.revocable = bytes.charCodeAt(mypos++) == 1; - break; - case 9: - // Key Expiration Time in seconds - seconds = util.readNumber(bytes.substr(mypos)); - - this.keyExpirationTime = seconds; - this.keyNeverExpires = seconds === 0; - - break; - case 11: - // Preferred Symmetric Algorithms - this.preferredSymmetricAlgorithms = []; - - while (mypos != bytes.length) { - this.preferredSymmetricAlgorithms.push(bytes.charCodeAt(mypos++)); - } - - break; - case 12: - // Revocation Key - // (1 octet of class, 1 octet of public-key algorithm ID, 20 - // octets of - // fingerprint) - this.revocationKeyClass = bytes.charCodeAt(mypos++); - this.revocationKeyAlgorithm = bytes.charCodeAt(mypos++); - this.revocationKeyFingerprint = bytes.substr(mypos, 20); - break; - - case 16: - // Issuer - this.issuerKeyId.read(bytes.substr(mypos)); - break; - - case 20: - // Notation Data - // We don't know how to handle anything but a text flagged data. - if (bytes.charCodeAt(mypos) == 0x80) { - - // We extract key/value tuple from the byte stream. - mypos += 4; - var m = util.readNumber(bytes.substr(mypos, 2)); - mypos += 2; - var n = util.readNumber(bytes.substr(mypos, 2)); - mypos += 2; - - var name = bytes.substr(mypos, m), - value = bytes.substr(mypos + m, n); - - this.notation = this.notation || {}; - this.notation[name] = value; - } else throw new Error("Unsupported notation flag."); - break; - case 21: - // Preferred Hash Algorithms - read_array.call(this, 'preferredHashAlgorithms', bytes.substr(mypos)); - break; - case 22: - // Preferred Compression Algorithms - read_array.call(this, 'preferredCompressionAlgorithms ', bytes.substr(mypos)); - break; - case 23: - // Key Server Preferences - read_array.call(this, 'keyServerPreferencess', bytes.substr(mypos)); - break; - case 24: - // Preferred Key Server - this.preferredKeyServer = bytes.substr(mypos); - break; - case 25: - // Primary User ID - this.isPrimaryUserID = bytes[mypos++] !== 0; - break; - case 26: - // Policy URI - this.policyURI = bytes.substr(mypos); - break; - case 27: - // Key Flags - read_array.call(this, 'keyFlags', bytes.substr(mypos)); - break; - case 28: - // Signer's User ID - this.signersUserId += bytes.substr(mypos); - break; - case 29: - // Reason for Revocation - this.reasonForRevocationFlag = bytes.charCodeAt(mypos++); - this.reasonForRevocationString = bytes.substr(mypos); - break; - case 30: - // Features - read_array.call(this, 'features', bytes.substr(mypos)); - break; - case 31: - // Signature Target - // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash) - this.signatureTargetPublicKeyAlgorithm = bytes.charCodeAt(mypos++); - this.signatureTargetHashAlgorithm = bytes.charCodeAt(mypos++); - - var len = crypto.getHashByteLength(this.signatureTargetHashAlgorithm); - - this.signatureTargetHash = bytes.substr(mypos, len); - break; - case 32: - // Embedded Signature - this.embeddedSignature = new signature(); - this.embeddedSignature.read(bytes.substr(mypos)); - break; - default: - throw new Error("Unknown signature subpacket type " + type + " @:" + mypos); - } - }; - - // Produces data to produce signature on - this.toSign = function (type, data) { - var t = enums.signature; - - switch (type) { - case t.binary: - case t.text: - return data.getBytes(); - - case t.standalone: - return ''; - - case t.cert_generic: - case t.cert_persona: - case t.cert_casual: - case t.cert_positive: - case t.cert_revocation: - var packet, tag; - - if (data.userid !== undefined) { - tag = 0xB4; - packet = data.userid; - } else if (data.userattribute !== undefined) { - tag = 0xD1; - packet = data.userattribute; - } else throw new Error('Either a userid or userattribute packet needs to be ' + - 'supplied for certification.'); - - var bytes = packet.write(); - - if (this.version == 4) { - return this.toSign(t.key, data) + - String.fromCharCode(tag) + - util.writeNumber(bytes.length, 4) + - bytes; - } else if (this.version == 3) { - return this.toSign(t.key, data) + - bytes; - } - break; - - case t.subkey_binding: - case t.key_binding: - return this.toSign(t.key, data) + this.toSign(t.key, { - key: data.bind - }); - - case t.key: - if (data.key === undefined) - throw new Error('Key packet is required for this sigtature.'); - - return data.key.writeOld(); - - case t.key_revocation: - case t.subkey_revocation: - return this.toSign(t.key, data); - case t.timestamp: - return ''; - case t.third_party: - throw new Error('Not implemented'); - default: - throw new Error('Unknown signature type.'); - } - }; - - - this.calculateTrailer = function () { - // calculating the trailer - var trailer = ''; - // V3 signatures don't have a trailer - if (this.version == 3) return trailer; - trailer += String.fromCharCode(4); // Version - trailer += String.fromCharCode(0xFF); - trailer += util.writeNumber(this.signatureData.length, 4); - return trailer; - }; - - - /** - * verifys the signature packet. Note: not signature types are implemented - * @param {String|Object} data data which on the signature applies - * @param {module:packet/public_subkey|module:packet/public_key} key the public key to verify the signature - * @return {boolean} True if message is verified, else false. - */ - this.verify = function (key, data) { - var signatureType = enums.write(enums.signature, this.signatureType), - publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), - hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); - - var bytes = this.toSign(signatureType, data), - trailer = this.calculateTrailer(); - - - var mpicount = 0; - // Algorithm-Specific Fields for RSA signatures: - // - multiprecision number (MPI) of RSA signature value m**d mod n. - if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) - mpicount = 1; - // Algorithm-Specific Fields for DSA signatures: - // - MPI of DSA value r. - // - MPI of DSA value s. - else if (publicKeyAlgorithm == 17) - mpicount = 2; - - var mpi = [], - i = 0; - for (var j = 0; j < mpicount; j++) { - mpi[j] = new type_mpi(); - i += mpi[j].read(this.signature.substr(i)); - } - - this.verified = crypto.signature.verify(publicKeyAlgorithm, - hashAlgorithm, mpi, key.mpi, - bytes + this.signatureData + trailer); - - return this.verified; - }; - - /** - * Verifies signature expiration date - * @return {Boolean} true if expired - */ - this.isExpired = function () { - if (!this.signatureNeverExpires) { - return Date.now() > (this.created.getTime() + this.signatureExpirationTime*1000); - } - return false; - }; + this.signature = bytes.substr(i); }; +Signature.prototype.write = function () { + return this.signatureData + + util.writeNumber(0, 2) + // Number of unsigned subpackets. + this.signedHashValue + + this.signature; +}; + +/** + * Signs provided data. This needs to be done prior to serialization. + * @param {module:packet/secret_key} key private key used to sign the message. + * @param {Object} data Contains packets to be signed. + */ +Signature.prototype.sign = function (key, data) { + var signatureType = enums.write(enums.signature, this.signatureType), + publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), + hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); + + var result = String.fromCharCode(4); + result += String.fromCharCode(signatureType); + result += String.fromCharCode(publicKeyAlgorithm); + result += String.fromCharCode(hashAlgorithm); + + this.issuerKeyId = key.getKeyId(); + + // Add hashed subpackets + result += this.write_all_sub_packets(); + + this.signatureData = result; + + var trailer = this.calculateTrailer(); + + var toHash = this.toSign(signatureType, data) + + this.signatureData + trailer; + + var hash = crypto.hash.digest(hashAlgorithm, toHash); + + this.signedHashValue = hash.substr(0, 2); + + this.signature = crypto.signature.sign(hashAlgorithm, + publicKeyAlgorithm, key.mpi, toHash); +}; + +/** + * Creates string of bytes with all subpacket data + * @return {String} a string-representation of a all subpacket data + */ +Signature.prototype.write_all_sub_packets = function () { + var sub = enums.signatureSubpacket; + var result = ''; + var bytes = ''; + if (this.created !== null) { + result += write_sub_packet(sub.signature_creation_time, util.writeDate(this.created)); + } + if (this.signatureExpirationTime !== null) { + result += write_sub_packet(sub.signature_expiration_time, util.writeNumber(this.signatureExpirationTime, 4)); + } + if (this.exportable !== null) { + result += write_sub_packet(sub.exportable_certification, String.fromCharCode(this.exportable ? 1 : 0)); + } + if (this.trustLevel !== null) { + bytes = String.fromCharCode(this.trustLevel) + String.fromCharCode(this.trustAmount); + result += write_sub_packet(sub.trust_signature, bytes); + } + if (this.regularExpression !== null) { + result += write_sub_packet(sub.regular_expression, this.regularExpression); + } + if (this.revocable !== null) { + result += write_sub_packet(sub.revocable, String.fromCharCode(this.revocable ? 1 : 0)); + } + if (this.keyExpirationTime !== null) { + result += write_sub_packet(sub.key_expiration_time, util.writeNumber(this.keyExpirationTime, 4)); + } + if (this.preferredSymmetricAlgorithms !== null) { + bytes = util.bin2str(this.preferredSymmetricAlgorithms); + result += write_sub_packet(sub.preferred_symmetric_algorithms, bytes); + } + if (this.revocationKeyClass !== null) { + bytes = String.fromCharCode(this.revocationKeyClass); + bytes += String.fromCharCode(this.revocationKeyAlgorithm); + bytes += this.revocationKeyFingerprint; + result += write_sub_packet(sub.revocation_key, bytes); + } + if (!this.issuerKeyId.isNull()) { + result += write_sub_packet(sub.issuer, this.issuerKeyId.write()); + } + if (this.notation !== null) { + for (var name in this.notation) { + if (this.notation.hasOwnProperty(name)) { + var value = this.notation[name]; + bytes = String.fromCharCode(0x80); + bytes += String.fromCharCode(0); + bytes += String.fromCharCode(0); + bytes += String.fromCharCode(0); + // 2 octets of name length + bytes += util.writeNumber(name.length, 2); + // 2 octets of value length + bytes += util.writeNumber(value.length, 2); + bytes += name + value; + result += write_sub_packet(sub.notation_data, bytes); + } + } + } + if (this.preferredHashAlgorithms !== null) { + bytes = util.bin2str(this.preferredHashAlgorithms); + result += write_sub_packet(sub.preferred_hash_algorithms, bytes); + } + if (this.preferredCompressionAlgorithms !== null) { + bytes = util.bin2str(this.preferredCompressionAlgorithms); + result += write_sub_packet(sub.preferred_hash_algorithms, bytes); + } + if (this.keyServerPreferences !== null) { + bytes = util.bin2str(this.keyServerPreferences); + result += write_sub_packet(sub.key_server_preferences, bytes); + } + if (this.preferredKeyServer !== null) { + result += write_sub_packet(sub.preferred_key_server, this.preferredKeyServer); + } + if (this.isPrimaryUserID !== null) { + result += write_sub_packet(sub.primary_user_id, String.fromCharCode(this.isPrimaryUserID ? 1 : 0)); + } + if (this.policyURI !== null) { + result += write_sub_packet(sub.policy_uri, this.policyURI); + } + if (this.keyFlags !== null) { + bytes = util.bin2str(this.keyFlags); + result += write_sub_packet(sub.key_flags, bytes); + } + if (this.signersUserId !== null) { + result += write_sub_packet(sub.signers_user_id, this.signersUserId); + } + if (this.reasonForRevocationFlag !== null) { + bytes = String.fromCharCode(this.reasonForRevocationFlag); + bytes += this.reasonForRevocationString; + result += write_sub_packet(sub.reason_for_revocation, bytes); + } + if (this.features !== null) { + bytes = util.bin2str(this.features); + result += write_sub_packet(sub.features, bytes); + } + if (this.signatureTargetPublicKeyAlgorithm !== null) { + bytes = String.fromCharCode(this.signatureTargetPublicKeyAlgorithm); + bytes += String.fromCharCode(this.signatureTargetHashAlgorithm); + bytes += this.signatureTargetHash; + result += write_sub_packet(sub.signature_target, bytes); + } + if (this.embeddedSignature !== null) { + result += write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()); + } + result = util.writeNumber(result.length, 2) + result; + return result; +}; + +/** + * creates a string representation of a sub signature packet (See RFC 4880 5.2.3.1) + * @param {Integer} type subpacket signature type. Signature types as described + * in RFC4880 Section 5.2.3.2 + * @param {String} data data to be included + * @return {String} a string-representation of a sub signature packet (See RFC 4880 5.2.3.1) + */ +function write_sub_packet(type, data) { + var result = ""; + result += packet.writeSimpleLength(data.length + 1); + result += String.fromCharCode(type); + result += data; + return result; +} + +// V4 signature sub packets + +Signature.prototype.read_sub_packet = function (bytes) { + var mypos = 0; + + function read_array(prop, bytes) { + this[prop] = []; + + for (var i = 0; i < bytes.length; i++) { + this[prop].push(bytes.charCodeAt(i)); + } + } + + // The leftwost bit denotes a "critical" packet, but we ignore it. + var type = bytes.charCodeAt(mypos++) & 0x7F; + var seconds; + + // subpacket type + switch (type) { + case 2: + // Signature Creation Time + this.created = util.readDate(bytes.substr(mypos)); + break; + case 3: + // Signature Expiration Time in seconds + seconds = util.readNumber(bytes.substr(mypos)); + + this.signatureNeverExpires = seconds === 0; + this.signatureExpirationTime = seconds; + + break; + case 4: + // Exportable Certification + this.exportable = bytes.charCodeAt(mypos++) == 1; + break; + case 5: + // Trust Signature + this.trustLevel = bytes.charCodeAt(mypos++); + this.trustAmount = bytes.charCodeAt(mypos++); + break; + case 6: + // Regular Expression + this.regularExpression = bytes.substr(mypos); + break; + case 7: + // Revocable + this.revocable = bytes.charCodeAt(mypos++) == 1; + break; + case 9: + // Key Expiration Time in seconds + seconds = util.readNumber(bytes.substr(mypos)); + + this.keyExpirationTime = seconds; + this.keyNeverExpires = seconds === 0; + + break; + case 11: + // Preferred Symmetric Algorithms + this.preferredSymmetricAlgorithms = []; + + while (mypos != bytes.length) { + this.preferredSymmetricAlgorithms.push(bytes.charCodeAt(mypos++)); + } + + break; + case 12: + // Revocation Key + // (1 octet of class, 1 octet of public-key algorithm ID, 20 + // octets of + // fingerprint) + this.revocationKeyClass = bytes.charCodeAt(mypos++); + this.revocationKeyAlgorithm = bytes.charCodeAt(mypos++); + this.revocationKeyFingerprint = bytes.substr(mypos, 20); + break; + + case 16: + // Issuer + this.issuerKeyId.read(bytes.substr(mypos)); + break; + + case 20: + // Notation Data + // We don't know how to handle anything but a text flagged data. + if (bytes.charCodeAt(mypos) == 0x80) { + + // We extract key/value tuple from the byte stream. + mypos += 4; + var m = util.readNumber(bytes.substr(mypos, 2)); + mypos += 2; + var n = util.readNumber(bytes.substr(mypos, 2)); + mypos += 2; + + var name = bytes.substr(mypos, m), + value = bytes.substr(mypos + m, n); + + this.notation = this.notation || {}; + this.notation[name] = value; + } else throw new Error("Unsupported notation flag."); + break; + case 21: + // Preferred Hash Algorithms + read_array.call(this, 'preferredHashAlgorithms', bytes.substr(mypos)); + break; + case 22: + // Preferred Compression Algorithms + read_array.call(this, 'preferredCompressionAlgorithms ', bytes.substr(mypos)); + break; + case 23: + // Key Server Preferences + read_array.call(this, 'keyServerPreferencess', bytes.substr(mypos)); + break; + case 24: + // Preferred Key Server + this.preferredKeyServer = bytes.substr(mypos); + break; + case 25: + // Primary User ID + this.isPrimaryUserID = bytes[mypos++] !== 0; + break; + case 26: + // Policy URI + this.policyURI = bytes.substr(mypos); + break; + case 27: + // Key Flags + read_array.call(this, 'keyFlags', bytes.substr(mypos)); + break; + case 28: + // Signer's User ID + this.signersUserId += bytes.substr(mypos); + break; + case 29: + // Reason for Revocation + this.reasonForRevocationFlag = bytes.charCodeAt(mypos++); + this.reasonForRevocationString = bytes.substr(mypos); + break; + case 30: + // Features + read_array.call(this, 'features', bytes.substr(mypos)); + break; + case 31: + // Signature Target + // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash) + this.signatureTargetPublicKeyAlgorithm = bytes.charCodeAt(mypos++); + this.signatureTargetHashAlgorithm = bytes.charCodeAt(mypos++); + + var len = crypto.getHashByteLength(this.signatureTargetHashAlgorithm); + + this.signatureTargetHash = bytes.substr(mypos, len); + break; + case 32: + // Embedded Signature + this.embeddedSignature = new Signature(); + this.embeddedSignature.read(bytes.substr(mypos)); + break; + default: + throw new Error("Unknown signature subpacket type " + type + " @:" + mypos); + } +}; + +// Produces data to produce signature on +Signature.prototype.toSign = function (type, data) { + var t = enums.signature; + + switch (type) { + case t.binary: + case t.text: + return data.getBytes(); + + case t.standalone: + return ''; + + case t.cert_generic: + case t.cert_persona: + case t.cert_casual: + case t.cert_positive: + case t.cert_revocation: + var packet, tag; + + if (data.userid !== undefined) { + tag = 0xB4; + packet = data.userid; + } else if (data.userattribute !== undefined) { + tag = 0xD1; + packet = data.userattribute; + } else throw new Error('Either a userid or userattribute packet needs to be ' + + 'supplied for certification.'); + + var bytes = packet.write(); + + if (this.version == 4) { + return this.toSign(t.key, data) + + String.fromCharCode(tag) + + util.writeNumber(bytes.length, 4) + + bytes; + } else if (this.version == 3) { + return this.toSign(t.key, data) + + bytes; + } + break; + + case t.subkey_binding: + case t.key_binding: + return this.toSign(t.key, data) + this.toSign(t.key, { + key: data.bind + }); + + case t.key: + if (data.key === undefined) + throw new Error('Key packet is required for this sigtature.'); + + return data.key.writeOld(); + + case t.key_revocation: + case t.subkey_revocation: + return this.toSign(t.key, data); + case t.timestamp: + return ''; + case t.third_party: + throw new Error('Not implemented'); + default: + throw new Error('Unknown signature type.'); + } +}; + + +Signature.prototype.calculateTrailer = function () { + // calculating the trailer + var trailer = ''; + // V3 signatures don't have a trailer + if (this.version == 3) return trailer; + trailer += String.fromCharCode(4); // Version + trailer += String.fromCharCode(0xFF); + trailer += util.writeNumber(this.signatureData.length, 4); + return trailer; +}; + + +/** + * verifys the signature packet. Note: not signature types are implemented + * @param {String|Object} data data which on the signature applies + * @param {module:packet/public_subkey|module:packet/public_key} key the public key to verify the signature + * @return {boolean} True if message is verified, else false. + */ +Signature.prototype.verify = function (key, data) { + var signatureType = enums.write(enums.signature, this.signatureType), + publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm), + hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm); + + var bytes = this.toSign(signatureType, data), + trailer = this.calculateTrailer(); + + + var mpicount = 0; + // Algorithm-Specific Fields for RSA signatures: + // - multiprecision number (MPI) of RSA signature value m**d mod n. + if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) + mpicount = 1; + // Algorithm-Specific Fields for DSA signatures: + // - MPI of DSA value r. + // - MPI of DSA value s. + else if (publicKeyAlgorithm == 17) + mpicount = 2; + + var mpi = [], + i = 0; + for (var j = 0; j < mpicount; j++) { + mpi[j] = new type_mpi(); + i += mpi[j].read(this.signature.substr(i)); + } + + this.verified = crypto.signature.verify(publicKeyAlgorithm, + hashAlgorithm, mpi, key.mpi, + bytes + this.signatureData + trailer); + + return this.verified; +}; + +/** + * Verifies signature expiration date + * @return {Boolean} true if expired + */ +Signature.prototype.isExpired = function () { + if (!this.signatureNeverExpires) { + return Date.now() > (this.created.getTime() + this.signatureExpirationTime*1000); + } + return false; +}; diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index dec28bb5..8ca0aa2b 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -29,13 +29,15 @@ * @module packet/sym_encrypted_integrity_protected */ +module.exports = SymEncryptedIntegrityProtected; + var util = require('../util'), crypto = require('../crypto'); /** * @constructor */ -module.exports = function sym_encrypted_integrity_protected() { +function SymEncryptedIntegrityProtected() { /** The encrypted payload. */ this.encrypted = null; // string /** @@ -46,76 +48,75 @@ module.exports = function sym_encrypted_integrity_protected() { */ this.modification = false; this.packets = null; +} +SymEncryptedIntegrityProtected.prototype.read = function (bytes) { + // - A one-octet version number. The only currently defined value is 1. + var version = bytes.charCodeAt(0); - this.read = function (bytes) { - // - A one-octet version number. The only currently defined value is 1. - var version = bytes.charCodeAt(0); + if (version != 1) { + throw new Error('Invalid packet version.'); + } - if (version != 1) { - throw new Error('Invalid packet version.'); - } - - // - Encrypted data, the output of the selected symmetric-key cipher - // operating in Cipher Feedback mode with shift amount equal to the - // block size of the cipher (CFB-n where n is the block size). - this.encrypted = bytes.substr(1); - }; - - this.write = function () { - - // 1 = Version - return String.fromCharCode(1) + this.encrypted; - }; - - this.encrypt = function (sessionKeyAlgorithm, key) { - var bytes = this.packets.write(); - - var prefixrandom = crypto.getPrefixRandom(sessionKeyAlgorithm); - var prefix = prefixrandom + prefixrandom.charAt(prefixrandom.length - 2) + prefixrandom.charAt(prefixrandom.length - - 1); - - var tohash = bytes; - - - // Modification detection code packet. - tohash += String.fromCharCode(0xD3); - tohash += String.fromCharCode(0x14); - - - tohash += crypto.hash.sha1(prefix + tohash); - - - this.encrypted = crypto.cfb.encrypt(prefixrandom, - sessionKeyAlgorithm, tohash, key, false).substring(0, - prefix.length + tohash.length); - }; - - /** - * Decrypts the encrypted data contained in this object read_packet must - * have been called before - * - * @param {Integer} sessionKeyAlgorithm - * The selected symmetric encryption algorithm to be used - * @param {String} key The key of cipher blocksize length to be used - * @return {String} The decrypted data of this packet - */ - this.decrypt = function (sessionKeyAlgorithm, key) { - var decrypted = crypto.cfb.decrypt( - sessionKeyAlgorithm, key, this.encrypted, false); - - - // there must be a modification detection code packet as the - // last packet and everything gets hashed except the hash itself - this.hash = crypto.hash.sha1( - crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted) + decrypted.substring(0, decrypted.length - 20)); - - - var mdc = decrypted.substr(decrypted.length - 20, 20); - - if (this.hash != mdc) { - throw new Error('Modification detected.'); - } else - this.packets.read(decrypted.substr(0, decrypted.length - 22)); - }; + // - Encrypted data, the output of the selected symmetric-key cipher + // operating in Cipher Feedback mode with shift amount equal to the + // block size of the cipher (CFB-n where n is the block size). + this.encrypted = bytes.substr(1); +}; + +SymEncryptedIntegrityProtected.prototype.write = function () { + + // 1 = Version + return String.fromCharCode(1) + this.encrypted; +}; + +SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm, key) { + var bytes = this.packets.write(); + + var prefixrandom = crypto.getPrefixRandom(sessionKeyAlgorithm); + var prefix = prefixrandom + prefixrandom.charAt(prefixrandom.length - 2) + prefixrandom.charAt(prefixrandom.length - + 1); + + var tohash = bytes; + + + // Modification detection code packet. + tohash += String.fromCharCode(0xD3); + tohash += String.fromCharCode(0x14); + + + tohash += crypto.hash.sha1(prefix + tohash); + + + this.encrypted = crypto.cfb.encrypt(prefixrandom, + sessionKeyAlgorithm, tohash, key, false).substring(0, + prefix.length + tohash.length); +}; + +/** + * Decrypts the encrypted data contained in this object read_packet must + * have been called before + * + * @param {Integer} sessionKeyAlgorithm + * The selected symmetric encryption algorithm to be used + * @param {String} key The key of cipher blocksize length to be used + * @return {String} The decrypted data of this packet + */ +SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) { + var decrypted = crypto.cfb.decrypt( + sessionKeyAlgorithm, key, this.encrypted, false); + + + // there must be a modification detection code packet as the + // last packet and everything gets hashed except the hash itself + this.hash = crypto.hash.sha1( + crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted) + decrypted.substring(0, decrypted.length - 20)); + + + var mdc = decrypted.substr(decrypted.length - 20, 20); + + if (this.hash != mdc) { + throw new Error('Modification detected.'); + } else + this.packets.read(decrypted.substr(0, decrypted.length - 22)); }; diff --git a/src/packet/sym_encrypted_session_key.js b/src/packet/sym_encrypted_session_key.js index 68d7936e..bc3686bd 100644 --- a/src/packet/sym_encrypted_session_key.js +++ b/src/packet/sym_encrypted_session_key.js @@ -39,102 +39,104 @@ var type_s2k = require('../type/s2k.js'), enums = require('../enums.js'), crypto = require('../crypto'); +module.exports = SymEncryptedSessionKey; + /** * @constructor */ -module.exports = function sym_encrypted_session_key() { +function SymEncryptedSessionKey() { this.tag = 3; this.sessionKeyEncryptionAlgorithm = null; this.sessionKeyAlgorithm = 'aes256'; this.encrypted = null; this.s2k = new type_s2k(); +} - /** - * Parsing function for a symmetric encrypted session key packet (tag 3). - * - * @param {String} input Payload of a tag 1 packet - * @param {Integer} position Position to start reading from the input string - * @param {Integer} len - * Length of the packet or the remaining length of - * input at position - * @return {module:packet/sym_encrypted_session_key} Object representation - */ - this.read = function(bytes) { - // A one-octet version number. The only currently defined version is 4. - this.version = bytes.charCodeAt(0); +/** + * Parsing function for a symmetric encrypted session key packet (tag 3). + * + * @param {String} input Payload of a tag 1 packet + * @param {Integer} position Position to start reading from the input string + * @param {Integer} len + * Length of the packet or the remaining length of + * input at position + * @return {module:packet/sym_encrypted_session_key} Object representation + */ +SymEncryptedSessionKey.prototype.read = function(bytes) { + // A one-octet version number. The only currently defined version is 4. + this.version = bytes.charCodeAt(0); - // A one-octet number describing the symmetric algorithm used. - var algo = enums.read(enums.symmetric, bytes.charCodeAt(1)); + // A one-octet number describing the symmetric algorithm used. + var algo = enums.read(enums.symmetric, bytes.charCodeAt(1)); - // A string-to-key (S2K) specifier, length as defined above. - var s2klength = this.s2k.read(bytes.substr(2)); + // A string-to-key (S2K) specifier, length as defined above. + var s2klength = this.s2k.read(bytes.substr(2)); - // Optionally, the encrypted session key itself, which is decrypted - // with the string-to-key object. - var done = s2klength + 2; + // Optionally, the encrypted session key itself, which is decrypted + // with the string-to-key object. + var done = s2klength + 2; - if (done < bytes.length) { - this.encrypted = bytes.substr(done); - this.sessionKeyEncryptionAlgorithm = algo; - } else - this.sessionKeyAlgorithm = algo; - }; - - this.write = function() { - var algo = this.encrypted === null ? - this.sessionKeyAlgorithm : - this.sessionKeyEncryptionAlgorithm; - - var bytes = String.fromCharCode(this.version) + - String.fromCharCode(enums.write(enums.symmetric, algo)) + - this.s2k.write(); - - if (this.encrypted !== null) - bytes += this.encrypted; - return bytes; - }; - - /** - * Decrypts the session key (only for public key encrypted session key - * packets (tag 1) - * - * @return {String} The unencrypted session key - */ - this.decrypt = function(passphrase) { - var algo = this.sessionKeyEncryptionAlgorithm !== null ? - this.sessionKeyEncryptionAlgorithm : - this.sessionKeyAlgorithm; - - - var length = crypto.cipher[algo].keySize; - var key = this.s2k.produce_key(passphrase, length); - - if (this.encrypted === null) { - this.sessionKey = key; - - } else { - var decrypted = crypto.cfb.decrypt( - this.sessionKeyEncryptionAlgorithm, key, this.encrypted, true); - - this.sessionKeyAlgorithm = enums.read(enums.symmetric, - decrypted[0].keyCodeAt()); - - this.sessionKey = decrypted.substr(1); - } - }; - - this.encrypt = function(passphrase) { - var length = crypto.getKeyLength(this.sessionKeyEncryptionAlgorithm); - var key = this.s2k.produce_key(passphrase, length); - - var private_key = String.fromCharCode( - enums.write(enums.symmetric, this.sessionKeyAlgorithm)) + - - crypto.getRandomBytes( - crypto.getKeyLength(this.sessionKeyAlgorithm)); - - this.encrypted = crypto.cfb.encrypt( - crypto.getPrefixRandom(this.sessionKeyEncryptionAlgorithm), - this.sessionKeyEncryptionAlgorithm, key, private_key, true); - }; + if (done < bytes.length) { + this.encrypted = bytes.substr(done); + this.sessionKeyEncryptionAlgorithm = algo; + } else + this.sessionKeyAlgorithm = algo; +}; + +SymEncryptedSessionKey.prototype.write = function() { + var algo = this.encrypted === null ? + this.sessionKeyAlgorithm : + this.sessionKeyEncryptionAlgorithm; + + var bytes = String.fromCharCode(this.version) + + String.fromCharCode(enums.write(enums.symmetric, algo)) + + this.s2k.write(); + + if (this.encrypted !== null) + bytes += this.encrypted; + return bytes; +}; + +/** + * Decrypts the session key (only for public key encrypted session key + * packets (tag 1) + * + * @return {String} The unencrypted session key + */ +SymEncryptedSessionKey.prototype.decrypt = function(passphrase) { + var algo = this.sessionKeyEncryptionAlgorithm !== null ? + this.sessionKeyEncryptionAlgorithm : + this.sessionKeyAlgorithm; + + + var length = crypto.cipher[algo].keySize; + var key = this.s2k.produce_key(passphrase, length); + + if (this.encrypted === null) { + this.sessionKey = key; + + } else { + var decrypted = crypto.cfb.decrypt( + this.sessionKeyEncryptionAlgorithm, key, this.encrypted, true); + + this.sessionKeyAlgorithm = enums.read(enums.symmetric, + decrypted[0].keyCodeAt()); + + this.sessionKey = decrypted.substr(1); + } +}; + +SymEncryptedSessionKey.prototype.encrypt = function(passphrase) { + var length = crypto.getKeyLength(this.sessionKeyEncryptionAlgorithm); + var key = this.s2k.produce_key(passphrase, length); + + var private_key = String.fromCharCode( + enums.write(enums.symmetric, this.sessionKeyAlgorithm)) + + + crypto.getRandomBytes( + crypto.getKeyLength(this.sessionKeyAlgorithm)); + + this.encrypted = crypto.cfb.encrypt( + crypto.getPrefixRandom(this.sessionKeyEncryptionAlgorithm), + this.sessionKeyEncryptionAlgorithm, key, private_key, true); }; diff --git a/src/packet/symmetrically_encrypted.js b/src/packet/symmetrically_encrypted.js index cc949d93..873cdd5f 100644 --- a/src/packet/symmetrically_encrypted.js +++ b/src/packet/symmetrically_encrypted.js @@ -27,45 +27,47 @@ * @module packet/symmetrically_encrypted */ +module.exports = SymmetricallyEncrypted; + var crypto = require('../crypto'); /** * @constructor */ -module.exports = function symmetrically_encrypted() { +function SymmetricallyEncrypted() { this.encrypted = null; /** Decrypted packets contained within. * @type {module:packet/packetlist} */ this.packets = null; +} - this.read = function(bytes) { - this.encrypted = bytes; - }; - - this.write = function() { - return this.encrypted; - }; - - /** - * Symmetrically decrypt the packet data - * - * @param {Integer} sessionKeyAlgorithm - * Symmetric key algorithm to use // See RFC4880 9.2 - * @param {String} key - * Key as string with the corresponding length to the - * algorithm - */ - this.decrypt = function(sessionKeyAlgorithm, key) { - var decrypted = crypto.cfb.decrypt( - sessionKeyAlgorithm, key, this.encrypted, true); - - this.packets.read(decrypted); - }; - - this.encrypt = function(algo, key) { - var data = this.packets.write(); - - this.encrypted = crypto.cfb.encrypt( - crypto.getPrefixRandom(algo), algo, data, key, true); - }; +SymmetricallyEncrypted.prototype.read = function (bytes) { + this.encrypted = bytes; +}; + +SymmetricallyEncrypted.prototype.write = function () { + return this.encrypted; +}; + +/** + * Symmetrically decrypt the packet data + * + * @param {Integer} sessionKeyAlgorithm + * Symmetric key algorithm to use // See RFC4880 9.2 + * @param {String} key + * Key as string with the corresponding length to the + * algorithm + */ +SymmetricallyEncrypted.prototype.decrypt = function (sessionKeyAlgorithm, key) { + var decrypted = crypto.cfb.decrypt( + sessionKeyAlgorithm, key, this.encrypted, true); + + this.packets.read(decrypted); +}; + +SymmetricallyEncrypted.prototype.encrypt = function (algo, key) { + var data = this.packets.write(); + + this.encrypted = crypto.cfb.encrypt( + crypto.getPrefixRandom(algo), algo, data, key, true); }; diff --git a/src/packet/trust.js b/src/packet/trust.js index f35ce337..e96b3c33 100644 --- a/src/packet/trust.js +++ b/src/packet/trust.js @@ -2,9 +2,10 @@ * @module packet/trust */ +module.exports = Trust; + /** * @constructor */ -module.exports = function trust() { - +function Trust() { }; diff --git a/src/packet/user_attribute.js b/src/packet/user_attribute.js index fc344d21..787739dd 100644 --- a/src/packet/user_attribute.js +++ b/src/packet/user_attribute.js @@ -38,25 +38,26 @@ var util = require('../util'), packet = require('./packet.js'); +module.exports = UserAttribute; + /** * @constructor */ -module.exports = function user_attribute() { - this.tag = 17; +function UserAttribute() { this.attributes = []; +} - /** - * parsing function for a user attribute packet (tag 17). - * @param {String} input payload of a tag 17 packet - */ - this.read = function(bytes) { - var i = 0; - while (i < bytes.length) { - var len = packet.readSimpleLength(bytes.substr(i)); - i += len.offset; +/** + * parsing function for a user attribute packet (tag 17). + * @param {String} input payload of a tag 17 packet + */ +UserAttribute.prototype.read = function(bytes) { + var i = 0; + while (i < bytes.length) { + var len = packet.readSimpleLength(bytes.substr(i)); + i += len.offset; - this.attributes.push(bytes.substr(i, len.len)); - i += len.len; - } - }; + this.attributes.push(bytes.substr(i, len.len)); + i += len.len; + } }; diff --git a/src/packet/userid.js b/src/packet/userid.js index fb983daa..bc0ad6a8 100644 --- a/src/packet/userid.js +++ b/src/packet/userid.js @@ -27,32 +27,33 @@ * @module packet/userid */ +module.exports = Userid; + var util = require('../util'); /** * @constructor */ -module.exports = function userid() { +function Userid() { /** A string containing the user id. Usually in the form * John Doe * @type {String} */ this.userid = ''; +} - - /** - * Parsing function for a user id packet (tag 13). - * @param {String} input payload of a tag 13 packet - */ - this.read = function (bytes) { - this.userid = util.decode_utf8(bytes); - }; - - /** - * Creates a string representation of the user id packet - * @return {String} string representation - */ - this.write = function () { - return util.encode_utf8(this.userid); - }; +/** + * Parsing function for a user id packet (tag 13). + * @param {String} input payload of a tag 13 packet + */ +Userid.prototype.read = function (bytes) { + this.userid = util.decode_utf8(bytes); +}; + +/** + * Creates a string representation of the user id packet + * @return {String} string representation + */ +Userid.prototype.write = function () { + return util.encode_utf8(this.userid); }; diff --git a/src/type/keyid.js b/src/type/keyid.js index 5a632f3d..40cb8d9e 100644 --- a/src/type/keyid.js +++ b/src/type/keyid.js @@ -26,41 +26,42 @@ * @module type/keyid */ +module.exports = Keyid; + var util = require('../util'); /** * @constructor */ -module.exports = function keyid() { +function Keyid() { this.bytes = ''; +} - - /** - * Parsing method for a key id - * @param {String} input Input to read the key id from - */ - this.read = function(bytes) { - this.bytes = bytes.substr(0, 8); - }; - - this.write = function() { - return this.bytes; - }; - - this.toHex = function() { - return util.hexstrdump(this.bytes); - }; - - this.equals = function(keyid) { - return this.bytes == keyid.bytes; - }; - - this.isNull = function() { - return this.bytes === ''; - }; +/** + * Parsing method for a key id + * @param {String} input Input to read the key id from + */ +Keyid.prototype.read = function(bytes) { + this.bytes = bytes.substr(0, 8); }; -module.exports.mapToHex = function(keyId) { +Keyid.prototype.write = function() { + return this.bytes; +}; + +Keyid.prototype.toHex = function() { + return util.hexstrdump(this.bytes); +}; + +Keyid.prototype.equals = function(keyid) { + return this.bytes == keyid.bytes; +}; + +Keyid.prototype.isNull = function() { + return this.bytes === ''; +}; + +module.exports.mapToHex = function (keyId) { return keyId.toHex(); }; diff --git a/src/type/mpi.js b/src/type/mpi.js index a094cee1..43ddb0f5 100644 --- a/src/type/mpi.js +++ b/src/type/mpi.js @@ -34,67 +34,69 @@ * @module type/mpi */ +module.exports = Mpi; + var BigInteger = require('../crypto/public_key/jsbn.js'), util = require('../util'); /** * @constructor */ -module.exports = function mpi() { +function Mpi() { /** An implementation dependent integer */ this.data = null; +} - /** - * Parsing function for a mpi (RFC 4880 3.2). - * @param {String} input Payload of mpi data - * @return {Integer} Length of data read - */ - this.read = function (bytes) { - var bits = (bytes.charCodeAt(0) << 8) | bytes.charCodeAt(1); +/** + * Parsing function for a mpi (RFC 4880 3.2). + * @param {String} input Payload of mpi data + * @return {Integer} Length of data read + */ +Mpi.prototype.read = function (bytes) { + var bits = (bytes.charCodeAt(0) << 8) | bytes.charCodeAt(1); - // Additional rules: - // - // The size of an MPI is ((MPI.length + 7) / 8) + 2 octets. - // - // The length field of an MPI describes the length starting from its - // most significant non-zero bit. Thus, the MPI [00 02 01] is not - // formed correctly. It should be [00 01 01]. + // Additional rules: + // + // The size of an MPI is ((MPI.length + 7) / 8) + 2 octets. + // + // The length field of an MPI describes the length starting from its + // most significant non-zero bit. Thus, the MPI [00 02 01] is not + // formed correctly. It should be [00 01 01]. - // TODO: Verification of this size method! This size calculation as - // specified above is not applicable in JavaScript - var bytelen = Math.ceil(bits / 8); + // TODO: Verification of this size method! This size calculation as + // specified above is not applicable in JavaScript + var bytelen = Math.ceil(bits / 8); - var raw = bytes.substr(2, bytelen); - this.fromBytes(raw); + var raw = bytes.substr(2, bytelen); + this.fromBytes(raw); - return 2 + bytelen; - }; - - this.fromBytes = function (bytes) { - this.data = new BigInteger(util.hexstrdump(bytes), 16); - }; - - this.toBytes = function () { - return this.write().substr(2); - }; - - this.byteLength = function () { - return this.toBytes().length; - }; - - /** - * Converts the mpi object to a string as specified in RFC4880 3.2 - * @return {String} mpi Byte representation - */ - this.write = function () { - return this.data.toMPI(); - }; - - this.toBigInteger = function () { - return this.data.clone(); - }; - - this.fromBigInteger = function (bn) { - this.data = bn.clone(); - }; + return 2 + bytelen; +}; + +Mpi.prototype.fromBytes = function (bytes) { + this.data = new BigInteger(util.hexstrdump(bytes), 16); +}; + +Mpi.prototype.toBytes = function () { + return this.write().substr(2); +}; + +Mpi.prototype.byteLength = function () { + return this.toBytes().length; +}; + +/** + * Converts the mpi object to a string as specified in RFC4880 3.2 + * @return {String} mpi Byte representation + */ +Mpi.prototype.write = function () { + return this.data.toMPI(); +}; + +Mpi.prototype.toBigInteger = function () { + return this.data.clone(); +}; + +Mpi.prototype.fromBigInteger = function (bn) { + this.data = bn.clone(); }; diff --git a/src/type/s2k.js b/src/type/s2k.js index 48e680de..aa5f8379 100644 --- a/src/type/s2k.js +++ b/src/type/s2k.js @@ -29,6 +29,8 @@ * @module type/s2k */ +module.exports = S2k; + var enums = require('../enums.js'), util = require('../util'), crypto = require('../crypto'); @@ -36,7 +38,7 @@ var enums = require('../enums.js'), /** * @constructor */ -module.exports = function s2k() { +function S2k() { /** @type {module:enums.hash} */ this.algorithm = 'sha256'; /** @type {module:enums.s2k} */ @@ -46,134 +48,133 @@ module.exports = function s2k() { * @type {String} */ this.salt = crypto.random.getRandomBytes(8); +} - +S2k.prototype.get_count = function () { // Exponent bias, defined in RFC4880 var expbias = 6; - this.get_count = function () { - return (16 + (this.c & 15)) << ((this.c >> 4) + expbias); - }; - - /** - * Parsing function for a string-to-key specifier (RFC 4880 3.7). - * @param {String} input Payload of string-to-key specifier - * @return {Integer} Actual length of the object - */ - this.read = function (bytes) { - var i = 0; - this.type = enums.read(enums.s2k, bytes.charCodeAt(i++)); - this.algorithm = enums.read(enums.hash, bytes.charCodeAt(i++)); - - switch (this.type) { - case 'simple': - break; - - case 'salted': - this.salt = bytes.substr(i, 8); - i += 8; - break; - - case 'iterated': - this.salt = bytes.substr(i, 8); - i += 8; - - // Octet 10: count, a one-octet, coded value - this.c = bytes.charCodeAt(i++); - break; - - case 'gnu': - if (bytes.substr(i, 3) == "GNU") { - i += 3; // GNU - var gnuExtType = 1000 + bytes.charCodeAt(i++); - if (gnuExtType == 1001) { - this.type = gnuExtType; - // GnuPG extension mode 1001 -- don't write secret key at all - } else { - throw new Error("Unknown s2k gnu protection mode."); - } - } else { - throw new Error("Unknown s2k type."); - } - break; - - default: - throw new Error("Unknown s2k type."); - } - - return i; - }; - - - /** - * writes an s2k hash based on the inputs. - * @return {String} Produced key of hashAlgorithm hash length - */ - this.write = function () { - var bytes = String.fromCharCode(enums.write(enums.s2k, this.type)); - bytes += String.fromCharCode(enums.write(enums.hash, this.algorithm)); - - switch (this.type) { - case 'simple': - break; - case 'salted': - bytes += this.salt; - break; - case 'iterated': - bytes += this.salt; - bytes += String.fromCharCode(this.c); - break; - } - - return bytes; - }; - - /** - * Produces a key using the specified passphrase and the defined - * hashAlgorithm - * @param {String} passphrase Passphrase containing user input - * @return {String} Produced key with a length corresponding to - * hashAlgorithm hash length - */ - this.produce_key = function (passphrase, numBytes) { - passphrase = util.encode_utf8(passphrase); - - function round(prefix, s2k) { - var algorithm = enums.write(enums.hash, s2k.algorithm); - - switch (s2k.type) { - case 'simple': - return crypto.hash.digest(algorithm, prefix + passphrase); - - case 'salted': - return crypto.hash.digest(algorithm, - prefix + s2k.salt + passphrase); - - case 'iterated': - var isp = [], - count = s2k.get_count(); - data = s2k.salt + passphrase; - - while (isp.length * data.length < count) - isp.push(data); - - isp = isp.join(''); - - if (isp.length > count) - isp = isp.substr(0, count); - - return crypto.hash.digest(algorithm, prefix + isp); - } - } - - var result = '', - prefix = ''; - - while (result.length <= numBytes) { - result += round(prefix, this); - prefix += String.fromCharCode(0); - } - - return result.substr(0, numBytes); - }; + return (16 + (this.c & 15)) << ((this.c >> 4) + expbias); +}; + +/** + * Parsing function for a string-to-key specifier (RFC 4880 3.7). + * @param {String} input Payload of string-to-key specifier + * @return {Integer} Actual length of the object + */ +S2k.prototype.read = function (bytes) { + var i = 0; + this.type = enums.read(enums.s2k, bytes.charCodeAt(i++)); + this.algorithm = enums.read(enums.hash, bytes.charCodeAt(i++)); + + switch (this.type) { + case 'simple': + break; + + case 'salted': + this.salt = bytes.substr(i, 8); + i += 8; + break; + + case 'iterated': + this.salt = bytes.substr(i, 8); + i += 8; + + // Octet 10: count, a one-octet, coded value + this.c = bytes.charCodeAt(i++); + break; + + case 'gnu': + if (bytes.substr(i, 3) == "GNU") { + i += 3; // GNU + var gnuExtType = 1000 + bytes.charCodeAt(i++); + if (gnuExtType == 1001) { + this.type = gnuExtType; + // GnuPG extension mode 1001 -- don't write secret key at all + } else { + throw new Error("Unknown s2k gnu protection mode."); + } + } else { + throw new Error("Unknown s2k type."); + } + break; + + default: + throw new Error("Unknown s2k type."); + } + + return i; +}; + + +/** + * writes an s2k hash based on the inputs. + * @return {String} Produced key of hashAlgorithm hash length + */ +S2k.prototype.write = function () { + var bytes = String.fromCharCode(enums.write(enums.s2k, this.type)); + bytes += String.fromCharCode(enums.write(enums.hash, this.algorithm)); + + switch (this.type) { + case 'simple': + break; + case 'salted': + bytes += this.salt; + break; + case 'iterated': + bytes += this.salt; + bytes += String.fromCharCode(this.c); + break; + } + + return bytes; +}; + +/** + * Produces a key using the specified passphrase and the defined + * hashAlgorithm + * @param {String} passphrase Passphrase containing user input + * @return {String} Produced key with a length corresponding to + * hashAlgorithm hash length + */ +S2k.prototype.produce_key = function (passphrase, numBytes) { + passphrase = util.encode_utf8(passphrase); + + function round(prefix, s2k) { + var algorithm = enums.write(enums.hash, s2k.algorithm); + + switch (s2k.type) { + case 'simple': + return crypto.hash.digest(algorithm, prefix + passphrase); + + case 'salted': + return crypto.hash.digest(algorithm, + prefix + s2k.salt + passphrase); + + case 'iterated': + var isp = [], + count = s2k.get_count(); + data = s2k.salt + passphrase; + + while (isp.length * data.length < count) + isp.push(data); + + isp = isp.join(''); + + if (isp.length > count) + isp = isp.substr(0, count); + + return crypto.hash.digest(algorithm, prefix + isp); + } + } + + var result = '', + prefix = ''; + + while (result.length <= numBytes) { + result += round(prefix, this); + prefix += String.fromCharCode(0); + } + + return result.substr(0, numBytes); }; diff --git a/test/general/key.js b/test/general/key.js index 1cf127c1..ce7e8d36 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -265,7 +265,7 @@ describe('Key', function() { var pubKey = pubKeys.keys[0]; expect(pubKey).to.exist; - var packetlist = new openpgp.packet.list(); + var packetlist = new openpgp.packet.List(); packetlist.read(openpgp.armor.decode(pub_sig_test).data); diff --git a/test/general/keyring.js b/test/general/keyring.js index dd37d8d6..271c0df8 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -1,7 +1,7 @@ 'use strict'; var openpgp = require('openpgp'), - keyring = new (new require('keyring'))(), + keyring = new (require('keyring'))(), expect = chai.expect; describe("Keyring", function() { diff --git a/test/general/packet.js b/test/general/packet.js index 8b2dba28..ab84d779 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -43,12 +43,12 @@ describe("Packet", function() { '-----END PGP PRIVATE KEY BLOCK-----'; it('Symmetrically encrypted packet', function(done) { - var message = new openpgp.packet.list(); + var message = new openpgp.packet.List(); - var literal = new openpgp.packet.literal(); + var literal = new openpgp.packet.Literal(); literal.setText('Hello world'); - var enc = new openpgp.packet.symmetrically_encrypted(); + var enc = new openpgp.packet.SymmetricallyEncrypted(); message.push(enc); enc.packets.push(literal); @@ -57,7 +57,7 @@ describe("Packet", function() { enc.encrypt(algo, key); - var msg2 = new openpgp.packet.list(); + var msg2 = new openpgp.packet.List(); msg2.read(message.write()); msg2[0].decrypt(algo, key); @@ -70,16 +70,16 @@ describe("Packet", function() { var key = '12345678901234567890123456789012', algo = 'aes256'; - var literal = new openpgp.packet.literal(), - enc = new openpgp.packet.sym_encrypted_integrity_protected(), - msg = new openpgp.packet.list(); + var literal = new openpgp.packet.Literal(), + enc = new openpgp.packet.SymEncryptedIntegrityProtected(), + msg = new openpgp.packet.List(); msg.push(enc); literal.setText('Hello world!'); enc.packets.push(literal); enc.encrypt(algo, key); - var msg2 = new openpgp.packet.list(); + var msg2 = new openpgp.packet.List(); msg2.read(msg.write()); msg2[0].decrypt(algo, key); @@ -100,7 +100,7 @@ describe("Packet", function() { var msgbytes = openpgp.armor.decode(msg).data; - var parsed = new openpgp.packet.list(); + var parsed = new openpgp.packet.List(); parsed.read(msgbytes); parsed[0].decrypt('test'); @@ -127,9 +127,9 @@ describe("Packet", function() { return mpi; }); - var enc = new openpgp.packet.public_key_encrypted_session_key(), - msg = new openpgp.packet.list(), - msg2 = new openpgp.packet.list(); + var enc = new openpgp.packet.PublicKeyEncryptedSessionKey(), + msg = new openpgp.packet.List(), + msg2 = new openpgp.packet.List(); enc.sessionKey = '12345678901234567890123456789012'; enc.publicKeyAlgorithm = 'rsa_encrypt'; @@ -171,11 +171,11 @@ describe("Packet", function() { '=lKiS\n' + '-----END PGP PRIVATE KEY BLOCK-----'; - var key = new openpgp.packet.list(); + var key = new openpgp.packet.List(); key.read(openpgp.armor.decode(armored_key).data); key = key[0]; - var enc = new openpgp.packet.public_key_encrypted_session_key(), + var enc = new openpgp.packet.PublicKeyEncryptedSessionKey(), secret = '12345678901234567890123456789012'; enc.sessionKey = secret; @@ -240,11 +240,11 @@ describe("Packet", function() { '-----END PGP MESSAGE-----'; - var key = new openpgp.packet.list(); + var key = new openpgp.packet.List(); key.read(openpgp.armor.decode(armored_key).data); key = key[3]; - var msg = new openpgp.packet.list(); + var msg = new openpgp.packet.List(); msg.read(openpgp.armor.decode(armored_msg).data); msg[0].decrypt(key); @@ -260,10 +260,10 @@ describe("Packet", function() { var passphrase = 'hello', algo = 'aes256'; - var literal = new openpgp.packet.literal(), - key_enc = new openpgp.packet.sym_encrypted_session_key(), - enc = new openpgp.packet.sym_encrypted_integrity_protected(), - msg = new openpgp.packet.list(); + var literal = new openpgp.packet.Literal(), + key_enc = new openpgp.packet.SymEncryptedSessionKey(), + enc = new openpgp.packet.SymEncryptedIntegrityProtected(), + msg = new openpgp.packet.List(); msg.push(key_enc); msg.push(enc); @@ -278,7 +278,7 @@ describe("Packet", function() { enc.encrypt(algo, key); - var msg2 = new openpgp.packet.list(); + var msg2 = new openpgp.packet.List(); msg2.read(msg.write()); msg2[0].decrypt(passphrase); @@ -302,12 +302,12 @@ describe("Packet", function() { '=pR+C\n' + '-----END PGP MESSAGE-----'; - var key = new openpgp.packet.list(); + var key = new openpgp.packet.List(); key.read(openpgp.armor.decode(armored_key).data); key = key[3]; key.decrypt('test'); - var msg = new openpgp.packet.list(); + var msg = new openpgp.packet.List(); msg.read(openpgp.armor.decode(armored_msg).data); msg[0].decrypt(key); @@ -320,7 +320,7 @@ describe("Packet", function() { }); it('Secret key reading with signature verification.', function(done) { - var key = new openpgp.packet.list(); + var key = new openpgp.packet.List(); key.read(openpgp.armor.decode(armored_key).data); @@ -357,11 +357,11 @@ describe("Packet", function() { '=htrB\n' + '-----END PGP MESSAGE-----' - var key = new openpgp.packet.list(); + var key = new openpgp.packet.List(); key.read(openpgp.armor.decode(armored_key).data); key[3].decrypt('test') - var msg = new openpgp.packet.list(); + var msg = new openpgp.packet.List(); msg.read(openpgp.armor.decode(armored_msg).data); msg[0].decrypt(key[3]); @@ -376,8 +376,8 @@ describe("Packet", function() { }); it('Writing and encryption of a secret key packet.', function(done) { - var key = new openpgp.packet.list(); - key.push(new openpgp.packet.secret_key); + var key = new openpgp.packet.List(); + key.push(new openpgp.packet.SecretKey); var rsa = new openpgp.crypto.publicKey.rsa(), mpi = rsa.generate(512, "10001") @@ -397,7 +397,7 @@ describe("Packet", function() { var raw = key.write(); - var key2 = new openpgp.packet.list(); + var key2 = new openpgp.packet.List(); key2.read(raw); key2[0].decrypt('hello'); @@ -406,7 +406,7 @@ describe("Packet", function() { }); it('Writing and verification of a signature packet.', function(done) { - var key = new openpgp.packet.secret_key(); + var key = new openpgp.packet.SecretKey(); var rsa = new openpgp.crypto.publicKey.rsa, mpi = rsa.generate(512, "10001") @@ -421,9 +421,9 @@ describe("Packet", function() { key.mpi = mpi; - var signed = new openpgp.packet.list(), - literal = new openpgp.packet.literal(), - signature = new openpgp.packet.signature(); + var signed = new openpgp.packet.List(), + literal = new openpgp.packet.Literal(), + signature = new openpgp.packet.Signature(); literal.setText('Hello world'); @@ -438,7 +438,7 @@ describe("Packet", function() { var raw = signed.write(); - var signed2 = new openpgp.packet.list(); + var signed2 = new openpgp.packet.List(); signed2.read(raw); var verified = signed2[1].verify(key, signed2[0]);