@protontech/openpgp 5.3.1 → 5.5.0

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.
@@ -1,4 +1,4 @@
1
- /*! OpenPGP.js v5.3.1 - 2022-07-12 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
1
+ /*! OpenPGP.js v5.5.0 - 2022-10-31 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
2
2
  'use strict';
3
3
 
4
4
  const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -1790,7 +1790,7 @@ const util = {
1790
1790
  */
1791
1791
  printDebug: function (str) {
1792
1792
  if (debugMode) {
1793
- console.log(str);
1793
+ console.log('[OpenPGP.js debug]', str);
1794
1794
  }
1795
1795
  },
1796
1796
 
@@ -1801,7 +1801,7 @@ const util = {
1801
1801
  */
1802
1802
  printDebugError: function (error) {
1803
1803
  if (debugMode) {
1804
- console.error(error);
1804
+ console.error('[OpenPGP.js debug]', error);
1805
1805
  }
1806
1806
  },
1807
1807
 
@@ -2021,12 +2021,12 @@ const util = {
2021
2021
  },
2022
2022
 
2023
2023
  /**
2024
- * Remove trailing spaces and tabs from each line
2024
+ * Remove trailing spaces, carriage returns and tabs from each line
2025
2025
  */
2026
2026
  removeTrailingSpaces: function(text) {
2027
2027
  return text.split('\n').map(line => {
2028
2028
  let i = line.length - 1;
2029
- for (; i >= 0 && (line[i] === ' ' || line[i] === '\t'); i--);
2029
+ for (; i >= 0 && (line[i] === ' ' || line[i] === '\t' || line[i] === '\r'); i--);
2030
2030
  return line.substr(0, i + 1);
2031
2031
  }).join('\n');
2032
2032
  },
@@ -2904,7 +2904,7 @@ var defaultConfig = {
2904
2904
  * @memberof module:config
2905
2905
  * @property {String} versionString A version string to be included in armored messages
2906
2906
  */
2907
- versionString: 'OpenPGP.js 5.3.1',
2907
+ versionString: 'OpenPGP.js 5.5.0',
2908
2908
  /**
2909
2909
  * @memberof module:config
2910
2910
  * @property {String} commentString A comment string to be included in armored messages
@@ -3121,15 +3121,17 @@ function createcrc24(input) {
3121
3121
  }
3122
3122
 
3123
3123
  /**
3124
- * Verify armored headers. RFC4880, section 6.3: "OpenPGP should consider improperly formatted
3125
- * Armor Headers to be corruption of the ASCII Armor."
3124
+ * Verify armored headers. crypto-refresh-06, section 6.2:
3125
+ * "An OpenPGP implementation may consider improperly formatted Armor
3126
+ * Headers to be corruption of the ASCII Armor, but SHOULD make an
3127
+ * effort to recover."
3126
3128
  * @private
3127
3129
  * @param {Array<String>} headers - Armor headers
3128
3130
  */
3129
3131
  function verifyHeaders(headers) {
3130
3132
  for (let i = 0; i < headers.length; i++) {
3131
3133
  if (!/^([^\s:]|[^\s:][^:]*[^\s:]): .+$/.test(headers[i])) {
3132
- throw new Error('Improperly formatted armor header: ' + headers[i]);
3134
+ util.printDebugError(new Error('Improperly formatted armor header: ' + headers[i]));
3133
3135
  }
3134
3136
  if (!/^(Version|Comment|MessageID|Hash|Charset): .+$/.test(headers[i])) {
3135
3137
  util.printDebugError(new Error('Unknown header: ' + headers[i]));
@@ -3323,7 +3325,7 @@ function armor(messageType, body, partIndex, partTotal, customComment, config =
3323
3325
  result.push('-----END PGP MESSAGE, PART ' + partIndex + '-----\n');
3324
3326
  break;
3325
3327
  case enums.armor.signed:
3326
- result.push('\n-----BEGIN PGP SIGNED MESSAGE-----\n');
3328
+ result.push('-----BEGIN PGP SIGNED MESSAGE-----\n');
3327
3329
  result.push('Hash: ' + hash + '\n\n');
3328
3330
  result.push(text.replace(/^-/mg, '- -'));
3329
3331
  result.push('\n-----BEGIN PGP SIGNATURE-----\n');
@@ -14314,16 +14316,17 @@ function parsePrivateKeyParams(algo, bytes, publicParams) {
14314
14316
  }
14315
14317
  case enums.publicKey.hmac: {
14316
14318
  const { cipher: algo } = publicParams;
14317
- const keySize = hash.getHashByteLength(algo);
14319
+ const keySize = hash.getHashByteLength(algo.getValue());
14318
14320
  const hashSeed = bytes.subarray(read, read + 32); read += 32;
14319
- const key = bytes.subarray(read, read + keySize); read += keySize;
14320
- return { read, privateParams: { key, hashSeed } };
14321
+ const keyMaterial = bytes.subarray(read, read + keySize); read += keySize;
14322
+ return { read, privateParams: { hashSeed, keyMaterial } };
14321
14323
  }
14322
14324
  case enums.publicKey.aead: {
14323
14325
  const { cipher: algo } = publicParams;
14326
+ const hashSeed = bytes.subarray(read, read + 32); read += 32;
14324
14327
  const { keySize } = getCipher(algo.getValue());
14325
14328
  const keyMaterial = bytes.subarray(read, read + keySize); read += keySize;
14326
- return { read, privateParams: { keyMaterial } };
14329
+ return { read, privateParams: { hashSeed, keyMaterial } };
14327
14330
  }
14328
14331
  default:
14329
14332
  throw new UnsupportedError('Unknown public key encryption algorithm.');
@@ -14369,13 +14372,12 @@ function parseEncSessionKeyParams(algo, bytes) {
14369
14372
  // - An authentication tag generated by the AEAD mode.
14370
14373
  case enums.publicKey.aead: {
14371
14374
  const aeadMode = new AEADEnum(); read += aeadMode.read(bytes.subarray(read));
14372
- const { tagLength, ivLength } = getAEADMode(aeadMode.getValue());
14375
+ const { ivLength } = getAEADMode(aeadMode.getValue());
14373
14376
 
14374
14377
  const iv = bytes.subarray(read, read + ivLength); read += ivLength;
14375
14378
  const c = new ShortByteString(); read += c.read(bytes.subarray(read));
14376
- const t = bytes.subarray(read, read + tagLength);
14377
14379
 
14378
- return { aeadMode, iv, c, t };
14380
+ return { aeadMode, iv, c };
14379
14381
  }
14380
14382
  default:
14381
14383
  throw new UnsupportedError('Unknown public key encryption algorithm.');
@@ -14469,8 +14471,8 @@ async function createSymmetricParams(key, algo) {
14469
14471
  const bindingHash = await hash.sha256(seed);
14470
14472
  return {
14471
14473
  privateParams: {
14472
- keyMaterial: key,
14473
- hashSeed: seed
14474
+ hashSeed: seed,
14475
+ keyMaterial: key
14474
14476
  },
14475
14477
  publicParams: {
14476
14478
  cipher: algo,
@@ -14523,14 +14525,14 @@ async function validateParams$6(algo, publicParams, privateParams) {
14523
14525
  }
14524
14526
  case enums.publicKey.hmac: {
14525
14527
  const { cipher: algo, digest } = publicParams;
14526
- const { keyMaterial, hashSeed } = privateParams;
14527
- const keySize = hash.getHashByteLength(algo);
14528
+ const { hashSeed, keyMaterial } = privateParams;
14529
+ const keySize = hash.getHashByteLength(algo.getValue());
14528
14530
  return keySize === keyMaterial.length &&
14529
14531
  util.equalsUint8Array(digest, await hash.sha256(hashSeed));
14530
14532
  }
14531
14533
  case enums.publicKey.aead: {
14532
14534
  const { cipher: algo, digest } = publicParams;
14533
- const { keyMaterial, hashSeed } = privateParams;
14535
+ const { hashSeed, keyMaterial } = privateParams;
14534
14536
  const { keySize } = getCipher(algo.getValue());
14535
14537
  return keySize === keyMaterial.length &&
14536
14538
  util.equalsUint8Array(digest, await hash.sha256(hashSeed));
@@ -23280,6 +23282,11 @@ class SignaturePacket {
23280
23282
  // Add hashed subpackets
23281
23283
  arr.push(this.writeHashedSubPackets());
23282
23284
 
23285
+ // Remove unhashed subpackets, in case some allowed unhashed
23286
+ // subpackets existed, in order not to duplicate them (in both
23287
+ // the hashed and unhashed subpackets) when re-signing.
23288
+ this.unhashedSubpackets = [];
23289
+
23283
23290
  this.signatureData = util.concat(arr);
23284
23291
 
23285
23292
  const toHash = this.toHash(this.signatureType, data, detached);
@@ -23342,6 +23349,11 @@ class SignaturePacket {
23342
23349
  bytes = util.concat([bytes, this.revocationKeyFingerprint]);
23343
23350
  arr.push(writeSubPacket(sub.revocationKey, bytes));
23344
23351
  }
23352
+ if (!this.issuerKeyID.isNull() && this.issuerKeyVersion !== 5) {
23353
+ // If the version of [the] key is greater than 4, this subpacket
23354
+ // MUST NOT be included in the signature.
23355
+ arr.push(writeSubPacket(sub.issuer, this.issuerKeyID.write()));
23356
+ }
23345
23357
  this.rawNotations.forEach(([{ name, value, humanReadable }]) => {
23346
23358
  bytes = [new Uint8Array([humanReadable ? 0x80 : 0, 0, 0, 0])];
23347
23359
  // 2 octets of name length
@@ -23395,6 +23407,14 @@ class SignaturePacket {
23395
23407
  bytes = util.concat(bytes);
23396
23408
  arr.push(writeSubPacket(sub.signatureTarget, bytes));
23397
23409
  }
23410
+ if (this.embeddedSignature !== null) {
23411
+ arr.push(writeSubPacket(sub.embeddedSignature, this.embeddedSignature.write()));
23412
+ }
23413
+ if (this.issuerFingerprint !== null) {
23414
+ bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
23415
+ bytes = util.concat(bytes);
23416
+ arr.push(writeSubPacket(sub.issuerFingerprint, bytes));
23417
+ }
23398
23418
  if (this.preferredAEADAlgorithms !== null) {
23399
23419
  bytes = util.stringToUint8Array(util.uint8ArrayToString(this.preferredAEADAlgorithms));
23400
23420
  arr.push(writeSubPacket(sub.preferredAEADAlgorithms, bytes));
@@ -23407,26 +23427,11 @@ class SignaturePacket {
23407
23427
  }
23408
23428
 
23409
23429
  /**
23410
- * Creates Uint8Array of bytes of Issuer and Embedded Signature subpackets
23430
+ * Creates an Uint8Array containing the unhashed subpackets
23411
23431
  * @returns {Uint8Array} Subpacket data.
23412
23432
  */
23413
23433
  writeUnhashedSubPackets() {
23414
- const sub = enums.signatureSubpacket;
23415
23434
  const arr = [];
23416
- let bytes;
23417
- if (!this.issuerKeyID.isNull() && this.issuerKeyVersion !== 5) {
23418
- // If the version of [the] key is greater than 4, this subpacket
23419
- // MUST NOT be included in the signature.
23420
- arr.push(writeSubPacket(sub.issuer, this.issuerKeyID.write()));
23421
- }
23422
- if (this.embeddedSignature !== null) {
23423
- arr.push(writeSubPacket(sub.embeddedSignature, this.embeddedSignature.write()));
23424
- }
23425
- if (this.issuerFingerprint !== null) {
23426
- bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
23427
- bytes = util.concat(bytes);
23428
- arr.push(writeSubPacket(sub.issuerFingerprint, bytes));
23429
- }
23430
23435
  this.unhashedSubpackets.forEach(data => {
23431
23436
  arr.push(writeSimpleLength(data.length));
23432
23437
  arr.push(data);
@@ -23446,9 +23451,11 @@ class SignaturePacket {
23446
23451
  const critical = bytes[mypos] & 0x80;
23447
23452
  const type = bytes[mypos] & 0x7F;
23448
23453
 
23449
- if (!hashed && !allowedUnhashedSubpackets.has(type)) {
23454
+ if (!hashed) {
23450
23455
  this.unhashedSubpackets.push(bytes.subarray(mypos, bytes.length));
23451
- return;
23456
+ if (!allowedUnhashedSubpackets.has(type)) {
23457
+ return;
23458
+ }
23452
23459
  }
23453
23460
 
23454
23461
  mypos++;
@@ -30514,7 +30521,7 @@ class CleartextMessage {
30514
30521
  * @param {Signature} signature - The detached signature or an empty signature for unsigned messages
30515
30522
  */
30516
30523
  constructor(text, signature) {
30517
- // normalize EOL to canonical form <CR><LF>
30524
+ // remove trailing whitespace and normalize EOL to canonical form <CR><LF>
30518
30525
  this.text = util.removeTrailingSpaces(text).replace(/\r?\n/g, '\r\n');
30519
30526
  if (signature && !(signature instanceof Signature)) {
30520
30527
  throw new Error('Invalid signature input');
@@ -30915,7 +30922,7 @@ async function encryptKey({ privateKey, passphrase, config, ...rest }) {
30915
30922
 
30916
30923
 
30917
30924
  /**
30918
- * Encrypts a message using public keys, passwords or both at once. At least one of `encryptionKeys` or `passwords`
30925
+ * Encrypts a message using public keys, passwords or both at once. At least one of `encryptionKeys`, `passwords` or `sessionKeys`
30919
30926
  * must be specified. If signing keys are specified, those will be used to sign the message.
30920
30927
  * @param {Object} options
30921
30928
  * @param {Message} options.message - Message to be encrypted as created by {@link createMessage}
@@ -31230,6 +31237,10 @@ async function encryptSessionKey({ data, algorithm, aeadAlgorithm, encryptionKey
31230
31237
  if (rest.publicKeys) throw new Error('The `publicKeys` option has been removed from openpgp.encryptSessionKey, pass `encryptionKeys` instead');
31231
31238
  const unknownOptions = Object.keys(rest); if (unknownOptions.length > 0) throw new Error(`Unknown option: ${unknownOptions.join(', ')}`);
31232
31239
 
31240
+ if ((!encryptionKeys || encryptionKeys.length === 0) && (!passwords || passwords.length === 0)) {
31241
+ throw new Error('No encryption keys or passwords provided.');
31242
+ }
31243
+
31233
31244
  try {
31234
31245
  const message = await Message.encryptSessionKey(data, algorithm, aeadAlgorithm, encryptionKeys, passwords, wildcard, encryptionKeyIDs, date, encryptionUserIDs, config);
31235
31246
  return formatObject(message, format, config);