@protontech/openpgp 4.10.8 → 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 +41077 -41609
  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 -61152
  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 -40071
  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 -232
  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 -378
  88. package/src/key/index.js +0 -32
  89. package/src/key/key.js +0 -888
  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 -161
  109. package/src/packet/public_subkey.js +0 -44
  110. package/src/packet/secret_key.js +0 -468
  111. package/src/packet/secret_subkey.js +0 -41
  112. package/src/packet/signature.js +0 -786
  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 -84
  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 -3491
  163. package/test/general/keyring.js +0 -336
  164. package/test/general/oid.js +0 -39
  165. package/test/general/openpgp.js +0 -2577
  166. package/test/general/packet.js +0 -950
  167. package/test/general/signature.js +0 -1715
  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,786 +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/packet
21
- * @requires type/keyid
22
- * @requires type/mpi
23
- * @requires crypto
24
- * @requires enums
25
- * @requires util
26
- */
27
-
28
- import stream from 'web-stream-tools';
29
- import packet from './packet';
30
- import type_keyid from '../type/keyid.js';
31
- import type_mpi from '../type/mpi.js';
32
- import crypto from '../crypto';
33
- import enums from '../enums';
34
- import util from '../util';
35
- import config from '../config';
36
-
37
- /**
38
- * Implementation of the Signature Packet (Tag 2)
39
- *
40
- * {@link https://tools.ietf.org/html/rfc4880#section-5.2|RFC4480 5.2}:
41
- * A Signature packet describes a binding between some public key and
42
- * some data. The most common signatures are a signature of a file or a
43
- * block of text, and a signature that is a certification of a User ID.
44
- * @memberof module:packet
45
- * @constructor
46
- * @param {Date} date the creation date of the signature
47
- */
48
- function Signature(date = new Date()) {
49
- this.tag = enums.packet.signature;
50
- this.version = 4; // This is set to 5 below if we sign with a V5 key.
51
- this.signatureType = null;
52
- this.hashAlgorithm = null;
53
- this.publicKeyAlgorithm = null;
54
-
55
- this.signatureData = null;
56
- this.unhashedSubpackets = [];
57
- this.signedHashValue = null;
58
-
59
- this.created = util.normalizeDate(date);
60
- this.signatureExpirationTime = null;
61
- this.signatureNeverExpires = true;
62
- this.exportable = null;
63
- this.trustLevel = null;
64
- this.trustAmount = null;
65
- this.regularExpression = null;
66
- this.revocable = null;
67
- this.keyExpirationTime = null;
68
- this.keyNeverExpires = null;
69
- this.preferredSymmetricAlgorithms = null;
70
- this.revocationKeyClass = null;
71
- this.revocationKeyAlgorithm = null;
72
- this.revocationKeyFingerprint = null;
73
- this.issuerKeyId = new type_keyid();
74
- this.rawNotations = [];
75
- this.notations = {};
76
- this.preferredHashAlgorithms = null;
77
- this.preferredCompressionAlgorithms = null;
78
- this.keyServerPreferences = null;
79
- this.preferredKeyServer = null;
80
- this.isPrimaryUserID = null;
81
- this.policyURI = null;
82
- this.keyFlags = null;
83
- this.signersUserId = null;
84
- this.reasonForRevocationFlag = null;
85
- this.reasonForRevocationString = null;
86
- this.features = null;
87
- this.signatureTargetPublicKeyAlgorithm = null;
88
- this.signatureTargetHashAlgorithm = null;
89
- this.signatureTargetHash = null;
90
- this.embeddedSignature = null;
91
- this.issuerKeyVersion = null;
92
- this.issuerFingerprint = null;
93
- this.preferredAeadAlgorithms = null;
94
-
95
- this.verified = null;
96
- this.revoked = null;
97
- }
98
-
99
- /**
100
- * parsing function for a signature packet (tag 2).
101
- * @param {String} bytes payload of a tag 2 packet
102
- * @param {Integer} position position to start reading from the bytes string
103
- * @param {Integer} len length of the packet or the remaining length of bytes at position
104
- * @returns {module:packet.Signature} object representation
105
- */
106
- Signature.prototype.read = function (bytes) {
107
- let i = 0;
108
- this.version = bytes[i++];
109
-
110
- if (this.version !== 4 && this.version !== 5) {
111
- throw new Error('Version ' + this.version + ' of the signature is unsupported.');
112
- }
113
-
114
- this.signatureType = bytes[i++];
115
- this.publicKeyAlgorithm = bytes[i++];
116
- this.hashAlgorithm = bytes[i++];
117
-
118
- // hashed subpackets
119
- i += this.read_sub_packets(bytes.subarray(i, bytes.length), true);
120
-
121
- // A V4 signature hashes the packet body
122
- // starting from its first field, the version number, through the end
123
- // of the hashed subpacket data. Thus, the fields hashed are the
124
- // signature version, the signature type, the public-key algorithm, the
125
- // hash algorithm, the hashed subpacket length, and the hashed
126
- // subpacket body.
127
- this.signatureData = bytes.subarray(0, i);
128
-
129
- // unhashed subpackets
130
- i += this.read_sub_packets(bytes.subarray(i, bytes.length), false);
131
-
132
- // Two-octet field holding left 16 bits of signed hash value.
133
- this.signedHashValue = bytes.subarray(i, i + 2);
134
- i += 2;
135
-
136
- this.signature = bytes.subarray(i, bytes.length);
137
- };
138
-
139
- Signature.prototype.write = function () {
140
- const arr = [];
141
- arr.push(this.signatureData);
142
- arr.push(this.write_unhashed_sub_packets());
143
- arr.push(this.signedHashValue);
144
- arr.push(stream.clone(this.signature));
145
- return util.concat(arr);
146
- };
147
-
148
- /**
149
- * Signs provided data. This needs to be done prior to serialization.
150
- * @param {module:packet.SecretKey} key private key used to sign the message.
151
- * @param {Object} data Contains packets to be signed.
152
- * @param {Boolean} detached (optional) whether to create a detached signature
153
- * @param {Boolean} streaming (optional) whether to process data as a stream
154
- * @returns {Promise<Boolean>}
155
- * @async
156
- */
157
- Signature.prototype.sign = async function (key, data, detached = false, streaming = false) {
158
- const signatureType = enums.write(enums.signature, this.signatureType);
159
- const publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm);
160
- const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
161
-
162
- if (key.version === 5) {
163
- this.version = 5;
164
- }
165
- const arr = [new Uint8Array([this.version, signatureType, publicKeyAlgorithm, hashAlgorithm])];
166
-
167
- this.issuerKeyVersion = key.version;
168
- this.issuerFingerprint = key.getFingerprintBytes();
169
- this.issuerKeyId = key.getKeyId();
170
-
171
- // Add hashed subpackets
172
- arr.push(this.write_hashed_sub_packets());
173
-
174
- this.signatureData = util.concat(arr);
175
-
176
- const toHash = this.toHash(signatureType, data, detached);
177
- const hash = await this.hash(signatureType, data, toHash, detached);
178
-
179
- this.signedHashValue = stream.slice(stream.clone(hash), 0, 2);
180
- const params = key.params;
181
- const signed = async () => crypto.signature.sign(
182
- publicKeyAlgorithm, hashAlgorithm, params, toHash, await stream.readToEnd(hash)
183
- );
184
- if (streaming) {
185
- this.signature = stream.fromAsync(signed);
186
- } else {
187
- this.signature = await signed();
188
-
189
- // Store the fact that this signature is valid, e.g. for when we call `await
190
- // getLatestValidSignature(this.revocationSignatures, key, data)` later.
191
- // Note that this only holds up if the key and data passed to verify are the
192
- // same as the ones passed to sign.
193
- this.verified = true;
194
- }
195
- return true;
196
- };
197
-
198
- /**
199
- * Creates Uint8Array of bytes of all subpacket data except Issuer and Embedded Signature subpackets
200
- * @returns {Uint8Array} subpacket data
201
- */
202
- Signature.prototype.write_hashed_sub_packets = function () {
203
- const sub = enums.signatureSubpacket;
204
- const arr = [];
205
- let bytes;
206
- if (this.created !== null) {
207
- arr.push(write_sub_packet(sub.signature_creation_time, util.writeDate(this.created)));
208
- }
209
- if (this.signatureExpirationTime !== null) {
210
- arr.push(write_sub_packet(sub.signature_expiration_time, util.writeNumber(this.signatureExpirationTime, 4)));
211
- }
212
- if (this.exportable !== null) {
213
- arr.push(write_sub_packet(sub.exportable_certification, new Uint8Array([this.exportable ? 1 : 0])));
214
- }
215
- if (this.trustLevel !== null) {
216
- bytes = new Uint8Array([this.trustLevel, this.trustAmount]);
217
- arr.push(write_sub_packet(sub.trust_signature, bytes));
218
- }
219
- if (this.regularExpression !== null) {
220
- arr.push(write_sub_packet(sub.regular_expression, this.regularExpression));
221
- }
222
- if (this.revocable !== null) {
223
- arr.push(write_sub_packet(sub.revocable, new Uint8Array([this.revocable ? 1 : 0])));
224
- }
225
- if (this.keyExpirationTime !== null) {
226
- arr.push(write_sub_packet(sub.key_expiration_time, util.writeNumber(this.keyExpirationTime, 4)));
227
- }
228
- if (this.preferredSymmetricAlgorithms !== null) {
229
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.preferredSymmetricAlgorithms));
230
- arr.push(write_sub_packet(sub.preferred_symmetric_algorithms, bytes));
231
- }
232
- if (this.revocationKeyClass !== null) {
233
- bytes = new Uint8Array([this.revocationKeyClass, this.revocationKeyAlgorithm]);
234
- bytes = util.concat([bytes, this.revocationKeyFingerprint]);
235
- arr.push(write_sub_packet(sub.revocation_key, bytes));
236
- }
237
- this.rawNotations.forEach(([{ name, value, humanReadable }]) => {
238
- bytes = [new Uint8Array([humanReadable ? 0x80 : 0, 0, 0, 0])];
239
- // 2 octets of name length
240
- bytes.push(util.writeNumber(name.length, 2));
241
- // 2 octets of value length
242
- bytes.push(util.writeNumber(value.length, 2));
243
- bytes.push(util.str_to_Uint8Array(name));
244
- bytes.push(value);
245
- bytes = util.concat(bytes);
246
- arr.push(write_sub_packet(sub.notation_data, bytes));
247
- });
248
- if (this.preferredHashAlgorithms !== null) {
249
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.preferredHashAlgorithms));
250
- arr.push(write_sub_packet(sub.preferred_hash_algorithms, bytes));
251
- }
252
- if (this.preferredCompressionAlgorithms !== null) {
253
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.preferredCompressionAlgorithms));
254
- arr.push(write_sub_packet(sub.preferred_compression_algorithms, bytes));
255
- }
256
- if (this.keyServerPreferences !== null) {
257
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.keyServerPreferences));
258
- arr.push(write_sub_packet(sub.key_server_preferences, bytes));
259
- }
260
- if (this.preferredKeyServer !== null) {
261
- arr.push(write_sub_packet(sub.preferred_key_server, util.str_to_Uint8Array(this.preferredKeyServer)));
262
- }
263
- if (this.isPrimaryUserID !== null) {
264
- arr.push(write_sub_packet(sub.primary_user_id, new Uint8Array([this.isPrimaryUserID ? 1 : 0])));
265
- }
266
- if (this.policyURI !== null) {
267
- arr.push(write_sub_packet(sub.policy_uri, util.str_to_Uint8Array(this.policyURI)));
268
- }
269
- if (this.keyFlags !== null) {
270
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.keyFlags));
271
- arr.push(write_sub_packet(sub.key_flags, bytes));
272
- }
273
- if (this.signersUserId !== null) {
274
- arr.push(write_sub_packet(sub.signers_user_id, util.str_to_Uint8Array(this.signersUserId)));
275
- }
276
- if (this.reasonForRevocationFlag !== null) {
277
- bytes = util.str_to_Uint8Array(String.fromCharCode(this.reasonForRevocationFlag) + this.reasonForRevocationString);
278
- arr.push(write_sub_packet(sub.reason_for_revocation, bytes));
279
- }
280
- if (this.features !== null) {
281
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.features));
282
- arr.push(write_sub_packet(sub.features, bytes));
283
- }
284
- if (this.signatureTargetPublicKeyAlgorithm !== null) {
285
- bytes = [new Uint8Array([this.signatureTargetPublicKeyAlgorithm, this.signatureTargetHashAlgorithm])];
286
- bytes.push(util.str_to_Uint8Array(this.signatureTargetHash));
287
- bytes = util.concat(bytes);
288
- arr.push(write_sub_packet(sub.signature_target, bytes));
289
- }
290
- if (this.preferredAeadAlgorithms !== null) {
291
- bytes = util.str_to_Uint8Array(util.Uint8Array_to_str(this.preferredAeadAlgorithms));
292
- arr.push(write_sub_packet(sub.preferred_aead_algorithms, bytes));
293
- }
294
-
295
- const result = util.concat(arr);
296
- const length = util.writeNumber(result.length, 2);
297
-
298
- return util.concat([length, result]);
299
- };
300
-
301
- /**
302
- * Creates Uint8Array of bytes of Issuer and Embedded Signature subpackets
303
- * @returns {Uint8Array} subpacket data
304
- */
305
- Signature.prototype.write_unhashed_sub_packets = function() {
306
- const sub = enums.signatureSubpacket;
307
- const arr = [];
308
- let bytes;
309
- if (!this.issuerKeyId.isNull() && this.issuerKeyVersion !== 5) {
310
- // If the version of [the] key is greater than 4, this subpacket
311
- // MUST NOT be included in the signature.
312
- arr.push(write_sub_packet(sub.issuer, this.issuerKeyId.write()));
313
- }
314
- if (this.embeddedSignature !== null) {
315
- arr.push(write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()));
316
- }
317
- if (this.issuerFingerprint !== null) {
318
- bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
319
- bytes = util.concat(bytes);
320
- arr.push(write_sub_packet(sub.issuer_fingerprint, bytes));
321
- }
322
- this.unhashedSubpackets.forEach(data => {
323
- arr.push(packet.writeSimpleLength(data.length));
324
- arr.push(data);
325
- });
326
-
327
- const result = util.concat(arr);
328
- const length = util.writeNumber(result.length, 2);
329
-
330
- return util.concat([length, result]);
331
- };
332
-
333
- /**
334
- * Creates a string representation of a sub signature packet
335
- * @see {@link https://tools.ietf.org/html/rfc4880#section-5.2.3.1|RFC4880 5.2.3.1}
336
- * @see {@link https://tools.ietf.org/html/rfc4880#section-5.2.3.2|RFC4880 5.2.3.2}
337
- * @param {Integer} type subpacket signature type.
338
- * @param {String} data data to be included
339
- * @returns {String} a string-representation of a sub signature packet
340
- * @private
341
- */
342
- function write_sub_packet(type, data) {
343
- const arr = [];
344
- arr.push(packet.writeSimpleLength(data.length + 1));
345
- arr.push(new Uint8Array([type]));
346
- arr.push(data);
347
- return util.concat(arr);
348
- }
349
-
350
- // V4 signature sub packets
351
-
352
- Signature.prototype.read_sub_packet = function (bytes, trusted = true) {
353
- let mypos = 0;
354
-
355
- const read_array = (prop, bytes) => {
356
- this[prop] = [];
357
-
358
- for (let i = 0; i < bytes.length; i++) {
359
- this[prop].push(bytes[i]);
360
- }
361
- };
362
-
363
- // The leftmost bit denotes a "critical" packet
364
- const critical = bytes[mypos] & 0x80;
365
- const type = bytes[mypos] & 0x7F;
366
-
367
- // GPG puts the Issuer and Signature subpackets in the unhashed area.
368
- // Tampering with those invalidates the signature, so we can trust them.
369
- // Ignore all other unhashed subpackets.
370
- if (!trusted && ![
371
- enums.signatureSubpacket.issuer,
372
- enums.signatureSubpacket.issuer_fingerprint,
373
- enums.signatureSubpacket.embedded_signature
374
- ].includes(type)) {
375
- this.unhashedSubpackets.push(bytes.subarray(mypos, bytes.length));
376
- return;
377
- }
378
-
379
- mypos++;
380
-
381
- // subpacket type
382
- switch (type) {
383
- case 2:
384
- // Signature Creation Time
385
- this.created = util.readDate(bytes.subarray(mypos, bytes.length));
386
- break;
387
- case 3: {
388
- // Signature Expiration Time in seconds
389
- const seconds = util.readNumber(bytes.subarray(mypos, bytes.length));
390
-
391
- this.signatureNeverExpires = seconds === 0;
392
- this.signatureExpirationTime = seconds;
393
-
394
- break;
395
- }
396
- case 4:
397
- // Exportable Certification
398
- this.exportable = bytes[mypos++] === 1;
399
- break;
400
- case 5:
401
- // Trust Signature
402
- this.trustLevel = bytes[mypos++];
403
- this.trustAmount = bytes[mypos++];
404
- break;
405
- case 6:
406
- // Regular Expression
407
- this.regularExpression = bytes[mypos];
408
- break;
409
- case 7:
410
- // Revocable
411
- this.revocable = bytes[mypos++] === 1;
412
- break;
413
- case 9: {
414
- // Key Expiration Time in seconds
415
- const seconds = util.readNumber(bytes.subarray(mypos, bytes.length));
416
-
417
- this.keyExpirationTime = seconds;
418
- this.keyNeverExpires = seconds === 0;
419
-
420
- break;
421
- }
422
- case 11:
423
- // Preferred Symmetric Algorithms
424
- read_array('preferredSymmetricAlgorithms', bytes.subarray(mypos, bytes.length));
425
- break;
426
- case 12:
427
- // Revocation Key
428
- // (1 octet of class, 1 octet of public-key algorithm ID, 20
429
- // octets of
430
- // fingerprint)
431
- this.revocationKeyClass = bytes[mypos++];
432
- this.revocationKeyAlgorithm = bytes[mypos++];
433
- this.revocationKeyFingerprint = bytes.subarray(mypos, mypos + 20);
434
- break;
435
-
436
- case 16:
437
- // Issuer
438
- this.issuerKeyId.read(bytes.subarray(mypos, bytes.length));
439
- break;
440
-
441
- case 20: {
442
- // Notation Data
443
- const humanReadable = !!(bytes[mypos] & 0x80);
444
-
445
- // We extract key/value tuple from the byte stream.
446
- mypos += 4;
447
- const m = util.readNumber(bytes.subarray(mypos, mypos + 2));
448
- mypos += 2;
449
- const n = util.readNumber(bytes.subarray(mypos, mypos + 2));
450
- mypos += 2;
451
-
452
- const name = util.Uint8Array_to_str(bytes.subarray(mypos, mypos + m));
453
- const value = bytes.subarray(mypos + m, mypos + m + n);
454
-
455
- this.rawNotations.push({ name, humanReadable, value });
456
-
457
- if (humanReadable) {
458
- this.notations[name] = util.Uint8Array_to_str(value);
459
- }
460
-
461
- if (critical && (config.known_notations.indexOf(name) === -1)) {
462
- throw new Error("Unknown critical notation: " + name);
463
- }
464
- break;
465
- }
466
- case 21:
467
- // Preferred Hash Algorithms
468
- read_array('preferredHashAlgorithms', bytes.subarray(mypos, bytes.length));
469
- break;
470
- case 22:
471
- // Preferred Compression Algorithms
472
- read_array('preferredCompressionAlgorithms', bytes.subarray(mypos, bytes.length));
473
- break;
474
- case 23:
475
- // Key Server Preferences
476
- read_array('keyServerPreferences', bytes.subarray(mypos, bytes.length));
477
- break;
478
- case 24:
479
- // Preferred Key Server
480
- this.preferredKeyServer = util.Uint8Array_to_str(bytes.subarray(mypos, bytes.length));
481
- break;
482
- case 25:
483
- // Primary User ID
484
- this.isPrimaryUserID = bytes[mypos++] !== 0;
485
- break;
486
- case 26:
487
- // Policy URI
488
- this.policyURI = util.Uint8Array_to_str(bytes.subarray(mypos, bytes.length));
489
- break;
490
- case 27:
491
- // Key Flags
492
- read_array('keyFlags', bytes.subarray(mypos, bytes.length));
493
- break;
494
- case 28:
495
- // Signer's User ID
496
- this.signersUserId = util.Uint8Array_to_str(bytes.subarray(mypos, bytes.length));
497
- break;
498
- case 29:
499
- // Reason for Revocation
500
- this.reasonForRevocationFlag = bytes[mypos++];
501
- this.reasonForRevocationString = util.Uint8Array_to_str(bytes.subarray(mypos, bytes.length));
502
- break;
503
- case 30:
504
- // Features
505
- read_array('features', bytes.subarray(mypos, bytes.length));
506
- break;
507
- case 31: {
508
- // Signature Target
509
- // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash)
510
- this.signatureTargetPublicKeyAlgorithm = bytes[mypos++];
511
- this.signatureTargetHashAlgorithm = bytes[mypos++];
512
-
513
- const len = crypto.getHashByteLength(this.signatureTargetHashAlgorithm);
514
-
515
- this.signatureTargetHash = util.Uint8Array_to_str(bytes.subarray(mypos, mypos + len));
516
- break;
517
- }
518
- case 32:
519
- // Embedded Signature
520
- this.embeddedSignature = new Signature();
521
- this.embeddedSignature.read(bytes.subarray(mypos, bytes.length));
522
- break;
523
- case 33:
524
- // Issuer Fingerprint
525
- this.issuerKeyVersion = bytes[mypos++];
526
- this.issuerFingerprint = bytes.subarray(mypos, bytes.length);
527
- if (this.issuerKeyVersion === 5) {
528
- this.issuerKeyId.read(this.issuerFingerprint);
529
- } else {
530
- this.issuerKeyId.read(this.issuerFingerprint.subarray(-8));
531
- }
532
- break;
533
- case 34:
534
- // Preferred AEAD Algorithms
535
- read_array.call(this, 'preferredAeadAlgorithms', bytes.subarray(mypos, bytes.length));
536
- break;
537
- default: {
538
- const err = new Error("Unknown signature subpacket type " + type + " @:" + mypos);
539
- if (critical) {
540
- throw err;
541
- } else {
542
- util.print_debug(err);
543
- }
544
- }
545
- }
546
- };
547
-
548
- Signature.prototype.read_sub_packets = function(bytes, trusted = true) {
549
- // Two-octet scalar octet count for following subpacket data.
550
- const subpacket_length = util.readNumber(bytes.subarray(0, 2));
551
-
552
- let i = 2;
553
-
554
- // subpacket data set (zero or more subpackets)
555
- while (i < 2 + subpacket_length) {
556
- const len = packet.readSimpleLength(bytes.subarray(i, bytes.length));
557
- i += len.offset;
558
-
559
- this.read_sub_packet(bytes.subarray(i, i + len.len), trusted);
560
-
561
- i += len.len;
562
- }
563
-
564
- return i;
565
- };
566
-
567
- // Produces data to produce signature on
568
- Signature.prototype.toSign = function (type, data) {
569
- const t = enums.signature;
570
-
571
- switch (type) {
572
- case t.binary:
573
- if (data.text !== null) {
574
- return util.encode_utf8(data.getText(true));
575
- }
576
- return data.getBytes(true);
577
-
578
- case t.text: {
579
- const bytes = data.getBytes(true);
580
- // normalize EOL to \r\n
581
- return util.canonicalizeEOL(bytes);
582
- }
583
- case t.standalone:
584
- return new Uint8Array(0);
585
-
586
- case t.cert_generic:
587
- case t.cert_persona:
588
- case t.cert_casual:
589
- case t.cert_positive:
590
- case t.cert_revocation: {
591
- let packet;
592
- let tag;
593
-
594
- if (data.userId) {
595
- tag = 0xB4;
596
- packet = data.userId;
597
- } else if (data.userAttribute) {
598
- tag = 0xD1;
599
- packet = data.userAttribute;
600
- } else {
601
- throw new Error('Either a userId or userAttribute packet needs to be ' +
602
- 'supplied for certification.');
603
- }
604
-
605
- const bytes = packet.write();
606
-
607
- return util.concat([this.toSign(t.key, data),
608
- new Uint8Array([tag]),
609
- util.writeNumber(bytes.length, 4),
610
- bytes]);
611
- }
612
- case t.subkey_binding:
613
- case t.subkey_revocation:
614
- case t.key_binding:
615
- return util.concat([this.toSign(t.key, data), this.toSign(t.key, {
616
- key: data.bind
617
- })]);
618
-
619
- case t.key:
620
- if (data.key === undefined) {
621
- throw new Error('Key packet is required for this signature.');
622
- }
623
- return data.key.writeForHash(this.version);
624
-
625
- case t.key_revocation:
626
- return this.toSign(t.key, data);
627
- case t.timestamp:
628
- return new Uint8Array(0);
629
- case t.third_party:
630
- throw new Error('Not implemented');
631
- default:
632
- throw new Error('Unknown signature type.');
633
- }
634
- };
635
-
636
-
637
- Signature.prototype.calculateTrailer = function (data, detached) {
638
- let length = 0;
639
- return stream.transform(stream.clone(this.signatureData), value => {
640
- length += value.length;
641
- }, () => {
642
- const arr = [];
643
- if (this.version === 5 && (this.signatureType === enums.signature.binary || this.signatureType === enums.signature.text)) {
644
- if (detached) {
645
- arr.push(new Uint8Array(6));
646
- } else {
647
- arr.push(data.writeHeader());
648
- }
649
- }
650
- arr.push(new Uint8Array([this.version, 0xFF]));
651
- if (this.version === 5) {
652
- arr.push(new Uint8Array(4));
653
- }
654
- arr.push(util.writeNumber(length, 4));
655
- // For v5, this should really be writeNumber(length, 8) rather than the
656
- // hardcoded 4 zero bytes above
657
- return util.concat(arr);
658
- });
659
- };
660
-
661
-
662
- Signature.prototype.toHash = function(signatureType, data, detached = false) {
663
- const bytes = this.toSign(signatureType, data);
664
-
665
- return util.concat([bytes, this.signatureData, this.calculateTrailer(data, detached)]);
666
- };
667
-
668
- Signature.prototype.hash = async function(signatureType, data, toHash, detached = false, streaming = true) {
669
- const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
670
- if (!toHash) toHash = this.toHash(signatureType, data, detached);
671
- if (!streaming && util.isStream(toHash)) {
672
- return stream.fromAsync(async () => this.hash(signatureType, data, await stream.readToEnd(toHash), detached));
673
- }
674
- return crypto.hash.digest(hashAlgorithm, toHash);
675
- };
676
-
677
-
678
- /**
679
- * verifies the signature packet. Note: not all signature types are implemented
680
- * @param {module:packet.PublicSubkey|module:packet.PublicKey|
681
- * module:packet.SecretSubkey|module:packet.SecretKey} key the public key to verify the signature
682
- * @param {module:enums.signature} signatureType expected signature type
683
- * @param {String|Object} data data which on the signature applies
684
- * @param {Boolean} detached (optional) whether to verify a detached signature
685
- * @returns {Promise<Boolean>} True if message is verified, else false.
686
- * @async
687
- */
688
- Signature.prototype.verify = async function (key, signatureType, data, detached = false, streaming = false) {
689
- const publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm);
690
- const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
691
-
692
- if (publicKeyAlgorithm !== enums.write(enums.publicKey, key.algorithm)) {
693
- throw new Error('Public key algorithm used to sign signature does not match issuer key algorithm.');
694
- }
695
-
696
- let toHash;
697
- let hash;
698
- if (this.hashed) {
699
- hash = await this.hashed;
700
- } else {
701
- toHash = this.toHash(signatureType, data, detached);
702
- if (!streaming) toHash = await stream.readToEnd(toHash);
703
- hash = await this.hash(signatureType, data, toHash);
704
- }
705
- hash = await stream.readToEnd(hash);
706
- if (this.signedHashValue[0] !== hash[0] ||
707
- this.signedHashValue[1] !== hash[1]) {
708
- throw new Error('Message digest did not match');
709
- }
710
-
711
- let mpicount = 0;
712
- // Algorithm-Specific Fields for RSA signatures:
713
- // - multiprecision number (MPI) of RSA signature value m**d mod n.
714
- if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) {
715
- mpicount = 1;
716
-
717
- // Algorithm-Specific Fields for DSA, ECDSA, and EdDSA signatures:
718
- // - MPI of DSA value r.
719
- // - MPI of DSA value s.
720
- } else if (publicKeyAlgorithm === enums.publicKey.dsa ||
721
- publicKeyAlgorithm === enums.publicKey.ecdsa ||
722
- publicKeyAlgorithm === enums.publicKey.eddsa) {
723
- mpicount = 2;
724
- }
725
-
726
- // EdDSA signature parameters are encoded in little-endian format
727
- // https://tools.ietf.org/html/rfc8032#section-5.1.2
728
- const endian = publicKeyAlgorithm === enums.publicKey.eddsa ? 'le' : 'be';
729
- const mpi = [];
730
- let i = 0;
731
- this.signature = await stream.readToEnd(this.signature);
732
- for (let j = 0; j < mpicount; j++) {
733
- mpi[j] = new type_mpi();
734
- i += mpi[j].read(this.signature.subarray(i, this.signature.length), endian);
735
- }
736
- const verified = await crypto.signature.verify(
737
- publicKeyAlgorithm, hashAlgorithm, mpi, key.params,
738
- toHash, hash
739
- );
740
- if (!verified) {
741
- throw new Error('Signature verification failed');
742
- }
743
- if (config.reject_hash_algorithms.has(hashAlgorithm)) {
744
- throw new Error('Insecure hash algorithm: ' + enums.read(enums.hash, hashAlgorithm).toUpperCase());
745
- }
746
- if (config.reject_message_hash_algorithms.has(hashAlgorithm) &&
747
- [enums.signature.binary, enums.signature.text].includes(this.signatureType)) {
748
- throw new Error('Insecure message hash algorithm: ' + enums.read(enums.hash, hashAlgorithm).toUpperCase());
749
- }
750
- if (this.revocationKeyClass !== null) {
751
- throw new Error('This key is intended to be revoked with an authorized key, which OpenPGP.js does not support.');
752
- }
753
- this.verified = true;
754
- return true;
755
- };
756
-
757
- /**
758
- * Verifies signature expiration date
759
- * @param {Date} date (optional) use the given date for verification instead of the current time
760
- * @returns {Boolean} true if expired
761
- */
762
- Signature.prototype.isExpired = function (date = new Date()) {
763
- const normDate = util.normalizeDate(date);
764
- if (normDate !== null) {
765
- const expirationTime = this.getExpirationTime();
766
- return !(this.created <= normDate && normDate <= expirationTime);
767
- }
768
- return false;
769
- };
770
-
771
- /**
772
- * Returns the expiration time of the signature or Infinity if signature does not expire
773
- * @returns {Date} expiration time
774
- */
775
- Signature.prototype.getExpirationTime = function () {
776
- return !this.signatureNeverExpires ? new Date(this.created.getTime() + this.signatureExpirationTime * 1000) : Infinity;
777
- };
778
-
779
- /**
780
- * Fix custom types after cloning
781
- */
782
- Signature.prototype.postCloneTypeFix = function() {
783
- this.issuerKeyId = type_keyid.fromClone(this.issuerKeyId);
784
- };
785
-
786
- export default Signature;