@protontech/openpgp 4.10.5 → 5.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +311 -239
  2. package/dist/lightweight/bn.interface.min.mjs +3 -0
  3. package/dist/lightweight/bn.interface.min.mjs.map +1 -0
  4. package/dist/lightweight/bn.interface.mjs +340 -0
  5. package/dist/lightweight/bn.min.mjs +3 -0
  6. package/dist/lightweight/bn.min.mjs.map +1 -0
  7. package/dist/lightweight/bn.mjs +3434 -0
  8. package/dist/lightweight/elliptic.min.mjs +3 -0
  9. package/dist/lightweight/elliptic.min.mjs.map +1 -0
  10. package/dist/lightweight/elliptic.mjs +4313 -0
  11. package/dist/lightweight/openpgp.min.mjs +3 -0
  12. package/dist/lightweight/openpgp.min.mjs.map +1 -0
  13. package/dist/lightweight/openpgp.mjs +31375 -0
  14. package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
  15. package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
  16. package/dist/lightweight/ponyfill.es6.mjs +3831 -0
  17. package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
  18. package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
  19. package/dist/lightweight/web-streams-adapter.mjs +561 -0
  20. package/dist/node/openpgp.js +43943 -0
  21. package/dist/node/openpgp.min.js +17 -0
  22. package/dist/node/openpgp.min.js.map +1 -0
  23. package/dist/node/openpgp.min.mjs +17 -0
  24. package/dist/node/openpgp.min.mjs.map +1 -0
  25. package/dist/node/openpgp.mjs +43880 -0
  26. package/dist/openpgp.js +41080 -41565
  27. package/dist/openpgp.min.js +17 -2
  28. package/dist/openpgp.min.js.map +1 -0
  29. package/dist/openpgp.min.mjs +17 -0
  30. package/dist/openpgp.min.mjs.map +1 -0
  31. package/dist/openpgp.mjs +43868 -0
  32. package/lightweight/package.json +5 -0
  33. package/openpgp.d.ts +889 -0
  34. package/package.json +63 -57
  35. package/dist/compat/openpgp.js +0 -61067
  36. package/dist/compat/openpgp.min.js +0 -2
  37. package/dist/compat/openpgp.worker.js +0 -173
  38. package/dist/compat/openpgp.worker.min.js +0 -2
  39. package/dist/lightweight/elliptic.min.js +0 -5
  40. package/dist/lightweight/openpgp.js +0 -40024
  41. package/dist/lightweight/openpgp.min.js +0 -2
  42. package/dist/lightweight/openpgp.worker.js +0 -173
  43. package/dist/lightweight/openpgp.worker.min.js +0 -2
  44. package/dist/openpgp.worker.js +0 -173
  45. package/dist/openpgp.worker.min.js +0 -2
  46. package/src/cleartext.js +0 -220
  47. package/src/config/config.js +0 -224
  48. package/src/config/index.js +0 -7
  49. package/src/config/localStorage.js +0 -35
  50. package/src/crypto/aes_kw.js +0 -153
  51. package/src/crypto/cfb.js +0 -169
  52. package/src/crypto/cipher/aes.js +0 -27
  53. package/src/crypto/cipher/blowfish.js +0 -398
  54. package/src/crypto/cipher/cast5.js +0 -610
  55. package/src/crypto/cipher/des.js +0 -476
  56. package/src/crypto/cipher/index.js +0 -91
  57. package/src/crypto/cipher/twofish.js +0 -346
  58. package/src/crypto/cmac.js +0 -98
  59. package/src/crypto/crypto.js +0 -394
  60. package/src/crypto/eax.js +0 -172
  61. package/src/crypto/gcm.js +0 -141
  62. package/src/crypto/hash/index.js +0 -163
  63. package/src/crypto/hash/md5.js +0 -205
  64. package/src/crypto/index.js +0 -57
  65. package/src/crypto/ocb.js +0 -274
  66. package/src/crypto/pkcs1.js +0 -170
  67. package/src/crypto/pkcs5.js +0 -55
  68. package/src/crypto/public_key/dsa.js +0 -188
  69. package/src/crypto/public_key/elgamal.js +0 -137
  70. package/src/crypto/public_key/elliptic/curves.js +0 -385
  71. package/src/crypto/public_key/elliptic/ecdh.js +0 -414
  72. package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
  73. package/src/crypto/public_key/elliptic/eddsa.js +0 -119
  74. package/src/crypto/public_key/elliptic/index.js +0 -34
  75. package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
  76. package/src/crypto/public_key/index.js +0 -28
  77. package/src/crypto/public_key/prime.js +0 -275
  78. package/src/crypto/public_key/rsa.js +0 -597
  79. package/src/crypto/random.js +0 -145
  80. package/src/crypto/signature.js +0 -137
  81. package/src/encoding/armor.js +0 -433
  82. package/src/encoding/base64.js +0 -96
  83. package/src/enums.js +0 -493
  84. package/src/hkp.js +0 -89
  85. package/src/index.js +0 -161
  86. package/src/key/factory.js +0 -326
  87. package/src/key/helper.js +0 -363
  88. package/src/key/index.js +0 -32
  89. package/src/key/key.js +0 -890
  90. package/src/key/subkey.js +0 -187
  91. package/src/key/user.js +0 -230
  92. package/src/keyring/index.js +0 -12
  93. package/src/keyring/keyring.js +0 -229
  94. package/src/keyring/localstore.js +0 -119
  95. package/src/lightweight_helper.js +0 -26
  96. package/src/message.js +0 -825
  97. package/src/openpgp.js +0 -717
  98. package/src/packet/all_packets.js +0 -116
  99. package/src/packet/clone.js +0 -189
  100. package/src/packet/compressed.js +0 -194
  101. package/src/packet/index.js +0 -20
  102. package/src/packet/literal.js +0 -168
  103. package/src/packet/marker.js +0 -62
  104. package/src/packet/one_pass_signature.js +0 -156
  105. package/src/packet/packet.js +0 -300
  106. package/src/packet/packetlist.js +0 -232
  107. package/src/packet/public_key.js +0 -280
  108. package/src/packet/public_key_encrypted_session_key.js +0 -156
  109. package/src/packet/public_subkey.js +0 -44
  110. package/src/packet/secret_key.js +0 -448
  111. package/src/packet/secret_subkey.js +0 -41
  112. package/src/packet/signature.js +0 -782
  113. package/src/packet/sym_encrypted_aead_protected.js +0 -189
  114. package/src/packet/sym_encrypted_integrity_protected.js +0 -139
  115. package/src/packet/sym_encrypted_session_key.js +0 -204
  116. package/src/packet/symmetrically_encrypted.js +0 -118
  117. package/src/packet/trust.js +0 -35
  118. package/src/packet/user_attribute.js +0 -94
  119. package/src/packet/userid.js +0 -87
  120. package/src/polyfills.js +0 -64
  121. package/src/signature.js +0 -73
  122. package/src/type/ecdh_symkey.js +0 -69
  123. package/src/type/kdf_params.js +0 -114
  124. package/src/type/keyid.js +0 -110
  125. package/src/type/mpi.js +0 -138
  126. package/src/type/oid.js +0 -110
  127. package/src/type/s2k.js +0 -203
  128. package/src/util.js +0 -836
  129. package/src/wkd.js +0 -88
  130. package/src/worker/async_proxy.js +0 -190
  131. package/src/worker/worker.js +0 -167
  132. package/test/crypto/aes_kw.js +0 -57
  133. package/test/crypto/cipher/aes.js +0 -86
  134. package/test/crypto/cipher/blowfish.js +0 -58
  135. package/test/crypto/cipher/cast5.js +0 -25
  136. package/test/crypto/cipher/des.js +0 -143
  137. package/test/crypto/cipher/index.js +0 -7
  138. package/test/crypto/cipher/twofish.js +0 -71
  139. package/test/crypto/crypto.js +0 -383
  140. package/test/crypto/eax.js +0 -150
  141. package/test/crypto/ecdh.js +0 -359
  142. package/test/crypto/elliptic.js +0 -251
  143. package/test/crypto/elliptic_data.js +0 -102
  144. package/test/crypto/hash/index.js +0 -5
  145. package/test/crypto/hash/md5.js +0 -16
  146. package/test/crypto/hash/ripemd.js +0 -14
  147. package/test/crypto/hash/sha.js +0 -20
  148. package/test/crypto/index.js +0 -14
  149. package/test/crypto/ocb.js +0 -183
  150. package/test/crypto/pkcs5.js +0 -39
  151. package/test/crypto/random.js +0 -79
  152. package/test/crypto/rsa.js +0 -180
  153. package/test/crypto/validate.js +0 -387
  154. package/test/general/armor.js +0 -408
  155. package/test/general/brainpool.js +0 -360
  156. package/test/general/decompression.js +0 -60
  157. package/test/general/ecc_nist.js +0 -115
  158. package/test/general/ecc_secp256k1.js +0 -242
  159. package/test/general/forwarding.js +0 -43
  160. package/test/general/hkp.js +0 -165
  161. package/test/general/index.js +0 -20
  162. package/test/general/key.js +0 -3402
  163. package/test/general/keyring.js +0 -336
  164. package/test/general/oid.js +0 -39
  165. package/test/general/openpgp.js +0 -2542
  166. package/test/general/packet.js +0 -937
  167. package/test/general/signature.js +0 -1665
  168. package/test/general/streaming.js +0 -944
  169. package/test/general/testInputs.js +0 -18
  170. package/test/general/util.js +0 -183
  171. package/test/general/wkd.js +0 -48
  172. package/test/general/x25519.js +0 -556
  173. package/test/unittests.js +0 -64
@@ -1,62 +0,0 @@
1
- // GPG4Browsers - An OpenPGP implementation in javascript
2
- // Copyright (C) 2011 Recurity Labs GmbH
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /**
19
- * @requires enums
20
- */
21
-
22
- import enums from '../enums';
23
-
24
- /**
25
- * Implementation of the strange "Marker packet" (Tag 10)
26
- *
27
- * {@link https://tools.ietf.org/html/rfc4880#section-5.8|RFC4880 5.8}:
28
- * An experimental version of PGP used this packet as the Literal
29
- * packet, but no released version of PGP generated Literal packets with this
30
- * tag. With PGP 5.x, this packet has been reassigned and is reserved for use as
31
- * the Marker packet.
32
- *
33
- * Such a packet MUST be ignored when received.
34
- * @memberof module:packet
35
- * @constructor
36
- */
37
- function Marker() {
38
- this.tag = enums.packet.marker;
39
- }
40
-
41
- /**
42
- * Parsing function for a literal data packet (tag 10).
43
- *
44
- * @param {String} input Payload of a tag 10 packet
45
- * @param {Integer} position
46
- * Position to start reading from the input string
47
- * @param {Integer} len
48
- * Length of the packet or the remaining length of
49
- * input at position
50
- * @returns {module:packet.Marker} Object representation
51
- */
52
- Marker.prototype.read = function (bytes) {
53
- if (bytes[0] === 0x50 && // P
54
- bytes[1] === 0x47 && // G
55
- bytes[2] === 0x50) { // P
56
- return true;
57
- }
58
- // marker packet does not contain "PGP"
59
- return false;
60
- };
61
-
62
- export default Marker;
@@ -1,156 +0,0 @@
1
- // GPG4Browsers - An OpenPGP implementation in javascript
2
- // Copyright (C) 2011 Recurity Labs GmbH
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /**
19
- * @requires web-stream-tools
20
- * @requires packet/signature
21
- * @requires type/keyid
22
- * @requires enums
23
- * @requires util
24
- */
25
-
26
- import stream from 'web-stream-tools';
27
- import Signature from './signature';
28
- import type_keyid from '../type/keyid';
29
- import enums from '../enums';
30
- import util from '../util';
31
-
32
- /**
33
- * Implementation of the One-Pass Signature Packets (Tag 4)
34
- *
35
- * {@link https://tools.ietf.org/html/rfc4880#section-5.4|RFC4880 5.4}:
36
- * The One-Pass Signature packet precedes the signed data and contains
37
- * enough information to allow the receiver to begin calculating any
38
- * hashes needed to verify the signature. It allows the Signature
39
- * packet to be placed at the end of the message, so that the signer
40
- * can compute the entire signed message in one pass.
41
- * @memberof module:packet
42
- * @constructor
43
- */
44
- function OnePassSignature() {
45
- /**
46
- * Packet type
47
- * @type {module:enums.packet}
48
- */
49
- this.tag = enums.packet.onePassSignature;
50
- /** A one-octet version number. The current version is 3. */
51
- this.version = null;
52
- /**
53
- * A one-octet signature type.
54
- * Signature types are described in
55
- * {@link https://tools.ietf.org/html/rfc4880#section-5.2.1|RFC4880 Section 5.2.1}.
56
- */
57
- this.signatureType = null;
58
- /**
59
- * A one-octet number describing the hash algorithm used.
60
- * @see {@link https://tools.ietf.org/html/rfc4880#section-9.4|RFC4880 9.4}
61
- */
62
- this.hashAlgorithm = null;
63
- /**
64
- * A one-octet number describing the public-key algorithm used.
65
- * @see {@link https://tools.ietf.org/html/rfc4880#section-9.1|RFC4880 9.1}
66
- */
67
- this.publicKeyAlgorithm = null;
68
- /** An eight-octet number holding the Key ID of the signing key. */
69
- this.issuerKeyId = null;
70
- /**
71
- * A one-octet number holding a flag showing whether the signature is nested.
72
- * A zero value indicates that the next packet is another One-Pass Signature packet
73
- * that describes another signature to be applied to the same message data.
74
- */
75
- this.flags = null;
76
- }
77
-
78
- /**
79
- * parsing function for a one-pass signature packet (tag 4).
80
- * @param {Uint8Array} bytes payload of a tag 4 packet
81
- * @returns {module:packet.OnePassSignature} object representation
82
- */
83
- OnePassSignature.prototype.read = function (bytes) {
84
- let mypos = 0;
85
- // A one-octet version number. The current version is 3.
86
- this.version = bytes[mypos++];
87
-
88
- // A one-octet signature type. Signature types are described in
89
- // Section 5.2.1.
90
- this.signatureType = bytes[mypos++];
91
-
92
- // A one-octet number describing the hash algorithm used.
93
- this.hashAlgorithm = bytes[mypos++];
94
-
95
- // A one-octet number describing the public-key algorithm used.
96
- this.publicKeyAlgorithm = bytes[mypos++];
97
-
98
- // An eight-octet number holding the Key ID of the signing key.
99
- this.issuerKeyId = new type_keyid();
100
- this.issuerKeyId.read(bytes.subarray(mypos, mypos + 8));
101
- mypos += 8;
102
-
103
- // A one-octet number holding a flag showing whether the signature
104
- // is nested. A zero value indicates that the next packet is
105
- // another One-Pass Signature packet that describes another
106
- // signature to be applied to the same message data.
107
- this.flags = bytes[mypos++];
108
- return this;
109
- };
110
-
111
- /**
112
- * creates a string representation of a one-pass signature packet
113
- * @returns {Uint8Array} a Uint8Array representation of a one-pass signature packet
114
- */
115
- OnePassSignature.prototype.write = function () {
116
- const start = new Uint8Array([3, enums.write(enums.signature, this.signatureType),
117
- enums.write(enums.hash, this.hashAlgorithm),
118
- enums.write(enums.publicKey, this.publicKeyAlgorithm)]);
119
-
120
- const end = new Uint8Array([this.flags]);
121
-
122
- return util.concatUint8Array([start, this.issuerKeyId.write(), end]);
123
- };
124
-
125
- /**
126
- * Fix custom types after cloning
127
- */
128
- OnePassSignature.prototype.postCloneTypeFix = function() {
129
- this.issuerKeyId = type_keyid.fromClone(this.issuerKeyId);
130
- };
131
-
132
- OnePassSignature.prototype.hash = Signature.prototype.hash;
133
- OnePassSignature.prototype.toHash = Signature.prototype.toHash;
134
- OnePassSignature.prototype.toSign = Signature.prototype.toSign;
135
- OnePassSignature.prototype.calculateTrailer = function(...args) {
136
- return stream.fromAsync(async () => Signature.prototype.calculateTrailer.apply(await this.correspondingSig, args));
137
- };
138
-
139
- OnePassSignature.prototype.verify = async function() {
140
- const correspondingSig = await this.correspondingSig;
141
- if (!correspondingSig || correspondingSig.tag !== enums.packet.signature) {
142
- throw new Error('Corresponding signature packet missing');
143
- }
144
- if (
145
- correspondingSig.signatureType !== this.signatureType ||
146
- correspondingSig.hashAlgorithm !== this.hashAlgorithm ||
147
- correspondingSig.publicKeyAlgorithm !== this.publicKeyAlgorithm ||
148
- !correspondingSig.issuerKeyId.equals(this.issuerKeyId)
149
- ) {
150
- throw new Error('Corresponding signature packet does not match one-pass signature packet');
151
- }
152
- correspondingSig.hashed = this.hashed;
153
- return correspondingSig.verify.apply(correspondingSig, arguments);
154
- };
155
-
156
- export default OnePassSignature;
@@ -1,300 +0,0 @@
1
- // GPG4Browsers - An OpenPGP implementation in javascript
2
- // Copyright (C) 2011 Recurity Labs GmbH
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /* eslint-disable callback-return */
19
-
20
- /**
21
- * @fileoverview Functions for reading and writing packets
22
- * @requires web-stream-tools
23
- * @requires enums
24
- * @requires util
25
- * @module packet/packet
26
- */
27
-
28
- import stream from 'web-stream-tools';
29
- import enums from '../enums';
30
- import util from '../util';
31
-
32
- export default {
33
- readSimpleLength: function(bytes) {
34
- let len = 0;
35
- let offset;
36
- const type = bytes[0];
37
-
38
-
39
- if (type < 192) {
40
- [len] = bytes;
41
- offset = 1;
42
- } else if (type < 255) {
43
- len = ((bytes[0] - 192) << 8) + (bytes[1]) + 192;
44
- offset = 2;
45
- } else if (type === 255) {
46
- len = util.readNumber(bytes.subarray(1, 1 + 4));
47
- offset = 5;
48
- }
49
-
50
- return {
51
- len: len,
52
- offset: offset
53
- };
54
- },
55
-
56
- /**
57
- * Encodes a given integer of length to the openpgp length specifier to a
58
- * string
59
- *
60
- * @param {Integer} length The length to encode
61
- * @returns {Uint8Array} String with openpgp length representation
62
- */
63
- writeSimpleLength: function(length) {
64
- if (length < 192) {
65
- return new Uint8Array([length]);
66
- } else if (length > 191 && length < 8384) {
67
- /*
68
- * let a = (total data packet length) - 192 let bc = two octet
69
- * representation of a let d = b + 192
70
- */
71
- return new Uint8Array([((length - 192) >> 8) + 192, (length - 192) & 0xFF]);
72
- }
73
- return util.concatUint8Array([new Uint8Array([255]), util.writeNumber(length, 4)]);
74
- },
75
-
76
- writePartialLength: function(power) {
77
- if (power < 0 || power > 30) {
78
- throw new Error('Partial Length power must be between 1 and 30');
79
- }
80
- return new Uint8Array([224 + power]);
81
- },
82
-
83
- writeTag: function(tag_type) {
84
- /* we're only generating v4 packet headers here */
85
- return new Uint8Array([0xC0 | tag_type]);
86
- },
87
-
88
- /**
89
- * Writes a packet header version 4 with the given tag_type and length to a
90
- * string
91
- *
92
- * @param {Integer} tag_type Tag type
93
- * @param {Integer} length Length of the payload
94
- * @returns {String} String of the header
95
- */
96
- writeHeader: function(tag_type, length) {
97
- /* we're only generating v4 packet headers here */
98
- return util.concatUint8Array([this.writeTag(tag_type), this.writeSimpleLength(length)]);
99
- },
100
-
101
- /**
102
- * Whether the packet type supports partial lengths per RFC4880
103
- * @param {Integer} tag_type Tag type
104
- * @returns {Boolean} String of the header
105
- */
106
- supportsStreaming: function(tag_type) {
107
- return [
108
- enums.packet.literal,
109
- enums.packet.compressed,
110
- enums.packet.symmetricallyEncrypted,
111
- enums.packet.symEncryptedIntegrityProtected,
112
- enums.packet.symEncryptedAEADProtected
113
- ].includes(tag_type);
114
- },
115
-
116
- /**
117
- * Generic static Packet Parser function
118
- *
119
- * @param {Uint8Array | ReadableStream<Uint8Array>} input Input stream as string
120
- * @param {Function} callback Function to call with the parsed packet
121
- * @returns {Boolean} Returns false if the stream was empty and parsing is done, and true otherwise.
122
- */
123
- read: async function(input, streaming, callback) {
124
- const reader = stream.getReader(input);
125
- let writer;
126
- let callbackReturned;
127
- try {
128
- const peekedBytes = await reader.peekBytes(2);
129
- // some sanity checks
130
- if (!peekedBytes || peekedBytes.length < 2 || (peekedBytes[0] & 0x80) === 0) {
131
- throw new Error("Error during parsing. This message / key probably does not conform to a valid OpenPGP format.");
132
- }
133
- const headerByte = await reader.readByte();
134
- let tag = -1;
135
- let format = -1;
136
- let packet_length;
137
-
138
- format = 0; // 0 = old format; 1 = new format
139
- if ((headerByte & 0x40) !== 0) {
140
- format = 1;
141
- }
142
-
143
- let packet_length_type;
144
- if (format) {
145
- // new format header
146
- tag = headerByte & 0x3F; // bit 5-0
147
- } else {
148
- // old format header
149
- tag = (headerByte & 0x3F) >> 2; // bit 5-2
150
- packet_length_type = headerByte & 0x03; // bit 1-0
151
- }
152
-
153
- const supportsStreaming = this.supportsStreaming(tag);
154
- let packet = null;
155
- if (streaming && supportsStreaming) {
156
- const transform = new TransformStream();
157
- writer = stream.getWriter(transform.writable);
158
- packet = transform.readable;
159
- callbackReturned = callback({ tag, packet });
160
- } else {
161
- packet = [];
162
- }
163
-
164
- let wasPartialLength;
165
- do {
166
- if (!format) {
167
- // 4.2.1. Old Format Packet Lengths
168
- switch (packet_length_type) {
169
- case 0:
170
- // The packet has a one-octet length. The header is 2 octets
171
- // long.
172
- packet_length = await reader.readByte();
173
- break;
174
- case 1:
175
- // The packet has a two-octet length. The header is 3 octets
176
- // long.
177
- packet_length = (await reader.readByte() << 8) | await reader.readByte();
178
- break;
179
- case 2:
180
- // The packet has a four-octet length. The header is 5
181
- // octets long.
182
- packet_length = (await reader.readByte() << 24) | (await reader.readByte() << 16) | (await reader.readByte() <<
183
- 8) | await reader.readByte();
184
- break;
185
- default:
186
- // 3 - The packet is of indeterminate length. The header is 1
187
- // octet long, and the implementation must determine how long
188
- // the packet is. If the packet is in a file, this means that
189
- // the packet extends until the end of the file. In general,
190
- // an implementation SHOULD NOT use indeterminate-length
191
- // packets except where the end of the data will be clear
192
- // from the context, and even then it is better to use a
193
- // definite length, or a new format header. The new format
194
- // headers described below have a mechanism for precisely
195
- // encoding data of indeterminate length.
196
- packet_length = Infinity;
197
- break;
198
- }
199
- } else { // 4.2.2. New Format Packet Lengths
200
- // 4.2.2.1. One-Octet Lengths
201
- const lengthByte = await reader.readByte();
202
- wasPartialLength = false;
203
- if (lengthByte < 192) {
204
- packet_length = lengthByte;
205
- // 4.2.2.2. Two-Octet Lengths
206
- } else if (lengthByte >= 192 && lengthByte < 224) {
207
- packet_length = ((lengthByte - 192) << 8) + (await reader.readByte()) + 192;
208
- // 4.2.2.4. Partial Body Lengths
209
- } else if (lengthByte > 223 && lengthByte < 255) {
210
- packet_length = 1 << (lengthByte & 0x1F);
211
- wasPartialLength = true;
212
- if (!supportsStreaming) {
213
- throw new TypeError('This packet type does not support partial lengths.');
214
- }
215
- // 4.2.2.3. Five-Octet Lengths
216
- } else {
217
- packet_length = (await reader.readByte() << 24) | (await reader.readByte() << 16) | (await reader.readByte() <<
218
- 8) | await reader.readByte();
219
- }
220
- }
221
- if (packet_length > 0) {
222
- let bytesRead = 0;
223
- while (true) {
224
- if (writer) await writer.ready;
225
- const { done, value } = await reader.read();
226
- if (done) {
227
- if (packet_length === Infinity) break;
228
- throw new Error('Unexpected end of packet');
229
- }
230
- const chunk = packet_length === Infinity ? value : value.subarray(0, packet_length - bytesRead);
231
- if (writer) await writer.write(chunk);
232
- else packet.push(chunk);
233
- bytesRead += value.length;
234
- if (bytesRead >= packet_length) {
235
- reader.unshift(value.subarray(packet_length - bytesRead + value.length));
236
- break;
237
- }
238
- }
239
- }
240
- } while (wasPartialLength);
241
-
242
- // If this was not a packet that "supports streaming", we peek to check
243
- // whether it is the last packet in the message. We peek 2 bytes instead
244
- // of 1 because the beginning of this function also peeks 2 bytes, and we
245
- // want to cut a `subarray` of the correct length into `web-stream-tools`'
246
- // `externalBuffer` as a tiny optimization here.
247
- //
248
- // If it *was* a streaming packet (i.e. the data packets), we peek at the
249
- // entire remainder of the stream, in order to forward errors in the
250
- // remainder of the stream to the packet data. (Note that this means we
251
- // read/peek at all signature packets before closing the literal data
252
- // packet, for example.) This forwards armor checksum errors to the
253
- // encrypted data stream, for example, so that they don't get lost /
254
- // forgotten on encryptedMessage.packets.stream, which we never look at.
255
- //
256
- // Note that subsequent packet parsing errors could still end up there if
257
- // `config.tolerant` is set to false, or on malformed messages with
258
- // multiple data packets, but usually it shouldn't happen.
259
- //
260
- // An example of what we do when stream-parsing a message containing
261
- // [ one-pass signature packet, literal data packet, signature packet ]:
262
- // 1. Read the one-pass signature packet
263
- // 2. Peek 2 bytes of the literal data packet
264
- // 3. Parse the one-pass signature packet
265
- //
266
- // 4. Read the literal data packet, simultaneously stream-parsing it
267
- // 5. Peek until the end of the message
268
- // 6. Finish parsing the literal data packet
269
- //
270
- // 7. Read the signature packet again (we already peeked at it in step 5)
271
- // 8. Peek at the end of the stream again (`peekBytes` returns undefined)
272
- // 9. Parse the signature packet
273
- //
274
- // Note that this means that if there's an error in the very end of the
275
- // stream, such as an MDC error, we throw in step 5 instead of in step 8
276
- // (or never), which is the point of this exercise.
277
- const nextPacket = await reader.peekBytes(supportsStreaming ? Infinity : 2);
278
- if (writer) {
279
- await writer.ready;
280
- await writer.close();
281
- } else {
282
- packet = util.concatUint8Array(packet);
283
- await callback({ tag, packet });
284
- }
285
- return !nextPacket || !nextPacket.length;
286
- } catch (e) {
287
- if (writer) {
288
- await writer.abort(e);
289
- return true;
290
- } else {
291
- throw e;
292
- }
293
- } finally {
294
- if (writer) {
295
- await callbackReturned;
296
- }
297
- reader.releaseLock();
298
- }
299
- }
300
- };