314 lines
15 KiB
HTML
314 lines
15 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: packet/packet.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: packet/packet.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source"><code>// GPG4Browsers - An OpenPGP implementation in javascript
|
|
// Copyright (C) 2011 Recurity Labs GmbH
|
|
//
|
|
// This library is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
// License as published by the Free Software Foundation; either
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
/**
|
|
* @requires enums
|
|
* @requires util
|
|
* @module packet/packet
|
|
*/
|
|
|
|
var enums = require('../enums.js'),
|
|
util = require('../util');
|
|
|
|
module.exports = {
|
|
readSimpleLength: function(bytes) {
|
|
var len = 0,
|
|
offset,
|
|
type = bytes.charCodeAt(0);
|
|
|
|
|
|
if (type < 192) {
|
|
len = bytes.charCodeAt(0);
|
|
offset = 1;
|
|
} else if (type < 255) {
|
|
len = ((bytes.charCodeAt(0) - 192) << 8) + (bytes.charCodeAt(1)) + 192;
|
|
offset = 2;
|
|
} else if (type == 255) {
|
|
len = util.readNumber(bytes.substr(1, 4));
|
|
offset = 5;
|
|
}
|
|
|
|
return {
|
|
len: len,
|
|
offset: offset
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Encodes a given integer of length to the openpgp length specifier to a
|
|
* string
|
|
*
|
|
* @param {Integer} length The length to encode
|
|
* @return {String} String with openpgp length representation
|
|
*/
|
|
writeSimpleLength: function(length) {
|
|
var result = "";
|
|
if (length < 192) {
|
|
result += String.fromCharCode(length);
|
|
} else if (length > 191 && length < 8384) {
|
|
/*
|
|
* let a = (total data packet length) - 192 let bc = two octet
|
|
* representation of a let d = b + 192
|
|
*/
|
|
result += String.fromCharCode(((length - 192) >> 8) + 192);
|
|
result += String.fromCharCode((length - 192) & 0xFF);
|
|
} else {
|
|
result += String.fromCharCode(255);
|
|
result += util.writeNumber(length, 4);
|
|
}
|
|
return result;
|
|
},
|
|
|
|
/**
|
|
* Writes a packet header version 4 with the given tag_type and length to a
|
|
* string
|
|
*
|
|
* @param {Integer} tag_type Tag type
|
|
* @param {Integer} length Length of the payload
|
|
* @return {String} String of the header
|
|
*/
|
|
writeHeader: function(tag_type, length) {
|
|
/* we're only generating v4 packet headers here */
|
|
var result = "";
|
|
result += String.fromCharCode(0xC0 | tag_type);
|
|
result += this.writeSimpleLength(length);
|
|
return result;
|
|
},
|
|
|
|
/**
|
|
* Writes a packet header Version 3 with the given tag_type and length to a
|
|
* string
|
|
*
|
|
* @param {Integer} tag_type Tag type
|
|
* @param {Integer} length Length of the payload
|
|
* @return {String} String of the header
|
|
*/
|
|
writeOldHeader: function(tag_type, length) {
|
|
var result = "";
|
|
if (length < 256) {
|
|
result += String.fromCharCode(0x80 | (tag_type << 2));
|
|
result += String.fromCharCode(length);
|
|
} else if (length < 65536) {
|
|
result += String.fromCharCode(0x80 | (tag_type << 2) | 1);
|
|
result += util.writeNumber(length, 2);
|
|
} else {
|
|
result += String.fromCharCode(0x80 | (tag_type << 2) | 2);
|
|
result += util.writeNumber(length, 4);
|
|
}
|
|
return result;
|
|
},
|
|
|
|
/**
|
|
* Generic static Packet Parser function
|
|
*
|
|
* @param {String} input Input stream as string
|
|
* @param {integer} position Position to start parsing
|
|
* @param {integer} len Length of the input from position on
|
|
* @return {Object} Returns a parsed module:packet/packet
|
|
*/
|
|
read: function(input, position, len) {
|
|
// some sanity checks
|
|
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;
|
|
var tag = -1;
|
|
var format = -1;
|
|
var packet_length;
|
|
|
|
format = 0; // 0 = old format; 1 = new format
|
|
if ((input.charCodeAt(mypos) & 0x40) !== 0) {
|
|
format = 1;
|
|
}
|
|
|
|
var packet_length_type;
|
|
if (format) {
|
|
// new format header
|
|
tag = input.charCodeAt(mypos) & 0x3F; // bit 5-0
|
|
} else {
|
|
// old format header
|
|
tag = (input.charCodeAt(mypos) & 0x3F) >> 2; // bit 5-2
|
|
packet_length_type = input.charCodeAt(mypos) & 0x03; // bit 1-0
|
|
}
|
|
|
|
// header octet parsing done
|
|
mypos++;
|
|
|
|
// parsed length from length field
|
|
var bodydata = null;
|
|
|
|
// used for partial body lengths
|
|
var real_packet_length = -1;
|
|
if (!format) {
|
|
// 4.2.1. Old Format Packet Lengths
|
|
switch (packet_length_type) {
|
|
case 0:
|
|
// The packet has a one-octet length. The header is 2 octets
|
|
// long.
|
|
packet_length = input.charCodeAt(mypos++);
|
|
break;
|
|
case 1:
|
|
// The packet has a two-octet length. The header is 3 octets
|
|
// long.
|
|
packet_length = (input.charCodeAt(mypos++) << 8) | input.charCodeAt(mypos++);
|
|
break;
|
|
case 2:
|
|
// The packet has a four-octet length. The header is 5
|
|
// octets long.
|
|
packet_length = (input.charCodeAt(mypos++) << 24) | (input.charCodeAt(mypos++) << 16) | (input.charCodeAt(mypos++) <<
|
|
8) | input.charCodeAt(mypos++);
|
|
break;
|
|
default:
|
|
// 3 - The packet is of indeterminate length. The header is 1
|
|
// octet long, and the implementation must determine how long
|
|
// the packet is. If the packet is in a file, this means that
|
|
// the packet extends until the end of the file. In general,
|
|
// an implementation SHOULD NOT use indeterminate-length
|
|
// packets except where the end of the data will be clear
|
|
// from the context, and even then it is better to use a
|
|
// definite length, or a new format header. The new format
|
|
// headers described below have a mechanism for precisely
|
|
// encoding data of indeterminate length.
|
|
packet_length = len;
|
|
break;
|
|
}
|
|
|
|
} else // 4.2.2. New Format Packet Lengths
|
|
{
|
|
|
|
// 4.2.2.1. One-Octet Lengths
|
|
if (input.charCodeAt(mypos) < 192) {
|
|
packet_length = input.charCodeAt(mypos++);
|
|
util.print_debug("1 byte length:" + packet_length);
|
|
// 4.2.2.2. Two-Octet Lengths
|
|
} else if (input.charCodeAt(mypos) >= 192 && input.charCodeAt(mypos) < 224) {
|
|
packet_length = ((input.charCodeAt(mypos++) - 192) << 8) + (input.charCodeAt(mypos++)) + 192;
|
|
util.print_debug("2 byte length:" + packet_length);
|
|
// 4.2.2.4. Partial Body Lengths
|
|
} else if (input.charCodeAt(mypos) > 223 && input.charCodeAt(mypos) < 255) {
|
|
packet_length = 1 << (input.charCodeAt(mypos++) & 0x1F);
|
|
util.print_debug("4 byte length:" + packet_length);
|
|
// 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) {
|
|
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) {
|
|
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) {
|
|
tmplen = 1 << (input.charCodeAt(mypos2++) & 0x1F);
|
|
packet_length += tmplen;
|
|
bodydata += input.substring(mypos2, mypos2 + tmplen);
|
|
mypos2 += tmplen;
|
|
} else {
|
|
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;
|
|
mypos2 += tmplen;
|
|
break;
|
|
}
|
|
}
|
|
real_packet_length = mypos2;
|
|
// 4.2.2.3. Five-Octet Lengths
|
|
} else {
|
|
mypos++;
|
|
packet_length = (input.charCodeAt(mypos++) << 24) | (input.charCodeAt(mypos++) << 16) | (input.charCodeAt(mypos++) <<
|
|
8) | input.charCodeAt(mypos++);
|
|
}
|
|
}
|
|
|
|
// if there was'nt a partial body length: use the specified
|
|
// packet_length
|
|
if (real_packet_length == -1) {
|
|
real_packet_length = packet_length;
|
|
}
|
|
|
|
if (bodydata === null) {
|
|
bodydata = input.substring(mypos, mypos + real_packet_length);
|
|
}
|
|
|
|
return {
|
|
tag: tag,
|
|
packet: bodydata,
|
|
offset: mypos + real_packet_length
|
|
};
|
|
}
|
|
};
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-armor.html">armor</a></li><li><a href="module-cleartext.html">cleartext</a></li><li><a href="module-config.html">config</a></li><li><a href="config.html">config/config</a></li><li><a href="localStorage.html">config/localStorage</a></li><li><a href="module-crypto.html">crypto</a></li><li><a href="cfb.html">crypto/cfb</a></li><li><a href="cipher.html">crypto/cipher</a></li><li><a href="aes.html">crypto/cipher/aes</a></li><li><a href="blowfish.html">crypto/cipher/blowfish</a></li><li><a href="cast5.html">crypto/cipher/cast5</a></li><li><a href="des.html">crypto/cipher/des</a></li><li><a href="twofish.html">crypto/cipher/twofish</a></li><li><a href="crypto.html">crypto/crypto</a></li><li><a href="hash.html">crypto/hash</a></li><li><a href="md5.html">crypto/hash/md5</a></li><li><a href="ripe-md.html">crypto/hash/ripe-md</a></li><li><a href="sha.html">crypto/hash/sha</a></li><li><a href="pkcs1.html">crypto/pkcs1</a></li><li><a href="public_key.html">crypto/public_key</a></li><li><a href="dsa.html">crypto/public_key/dsa</a></li><li><a href="elgamal.html">crypto/public_key/elgamal</a></li><li><a href="jsbn.html">crypto/public_key/jsbn</a></li><li><a href="rsa.html">crypto/public_key/rsa</a></li><li><a href="random.html">crypto/random</a></li><li><a href="signature.html">crypto/signature</a></li><li><a href="armor.html">encoding/armor</a></li><li><a href="base64.html">encoding/base64</a></li><li><a href="module-enums.html">enums</a></li><li><a href="module-key.html">key</a></li><li><a href="module-keyid.html">keyid</a></li><li><a href="keyring.html">keyring/keyring</a></li><li><a href="localstore.html">keyring/localstore</a></li><li><a href="module-message.html">message</a></li><li><a href="module-mpi.html">mpi</a></li><li><a href="module-openpgp.html">openpgp</a></li><li><a href="module-packet.html">packet</a></li><li><a href="compressed.html">packet/compressed</a></li><li><a href="literal.html">packet/literal</a></li><li><a href="marker.html">packet/marker</a></li><li><a href="one_pass_signature.html">packet/one_pass_signature</a></li><li><a href="packet.html">packet/packet</a></li><li><a href="packetlist.html">packet/packetlist</a></li><li><a href="public_key_.html">packet/public_key</a></li><li><a href="public_key_encrypted_session_key.html">packet/public_key_encrypted_session_key</a></li><li><a href="public_subkey.html">packet/public_subkey</a></li><li><a href="secret_key.html">packet/secret_key</a></li><li><a href="secret_subkey.html">packet/secret_subkey</a></li><li><a href="signature_.html">packet/signature</a></li><li><a href="sym_encrypted_integrity_protected.html">packet/sym_encrypted_integrity_protected</a></li><li><a href="sym_encrypted_session_key.html">packet/sym_encrypted_session_key</a></li><li><a href="symmetrically_encrypted.html">packet/symmetrically_encrypted</a></li><li><a href="trust.html">packet/trust</a></li><li><a href="user_attribute.html">packet/user_attribute</a></li><li><a href="userid.html">packet/userid</a></li><li><a href="module-s2k.html">s2k</a></li><li><a href="keyid.html">type/keyid</a></li><li><a href="mpi.html">type/mpi</a></li><li><a href="s2k.html">type/s2k</a></li><li><a href="module-util.html">util</a></li><li><a href="util.html">util/util</a></li></ul><h3>Classes</h3><ul><li><a href="JXG.Util.html">Util</a></li><li><a href="module-cleartext-CleartextMessage.html">CleartextMessage</a></li><li><a href="localStorage-LocalStorage.html">LocalStorage</a></li><li><a href="keyring-Keyring.html">Keyring</a></li><li><a href="module-key-Key.html">Key</a></li><li><a href="module-key-SubKey.html">SubKey</a></li><li><a href="module-key-User.html">User</a></li><li><a href="module-message-Message.html">Message</a></li><li><a href="compressed-Compressed.html">Compressed</a></li><li><a href="literal-Literal.html">Literal</a></li><li><a href="marker-Marker.html">Marker</a></li><li><a href="one_pass_signature-OnePassSignature.html">OnePassSignature</a></li><li><a href="packetlist-Packetlist.html">Packetlist</a></li><li><a href="public_key_encrypted_session_key-PublicKeyEncryptedSessionKey.html">PublicKeyEncryptedSessionKey</a></li><li><a href="public_key-PublicKey.html">PublicKey</a></li><li><a href="public_subkey-PublicSubkey.html">PublicSubkey</a></li><li><a href="secret_subkey-SecretSubkey.html">SecretSubkey</a></li><li><a href="signature-Signature.html">Signature</a></li><li><a href="sym_encrypted_integrity_protected-SymEncryptedIntegrityProtected.html">SymEncryptedIntegrityProtected</a></li><li><a href="sym_encrypted_session_key-SymEncryptedSessionKey.html">SymEncryptedSessionKey</a></li><li><a href="symmetrically_encrypted-SymmetricallyEncrypted.html">SymmetricallyEncrypted</a></li><li><a href="trust-Trust.html">Trust</a></li><li><a href="user_attribute-UserAttribute.html">UserAttribute</a></li><li><a href="userid-Userid.html">Userid</a></li><li><a href="keyid-Keyid.html">Keyid</a></li><li><a href="mpi-Mpi.html">Mpi</a></li><li><a href="s2k-S2k.html">S2k</a></li></ul>
|
|
</nav>
|
|
|
|
<br clear="both">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.0</a> on Sun Jan 05 2014 10:08:07 GMT-0800 (PST)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|