@blamejs/blamejs-shop 0.5.21 → 0.5.22

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 (70) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +1 -1
  3. package/SECURITY.md +7 -1
  4. package/lib/addresses.js +16 -2
  5. package/lib/affiliates.js +3 -3
  6. package/lib/announcement-bar.js +2 -2
  7. package/lib/api-keys.js +2 -2
  8. package/lib/asset-manifest.json +1 -1
  9. package/lib/blog-articles.js +2 -2
  10. package/lib/carrier-accounts.js +2 -2
  11. package/lib/catalog-drafts.js +1 -1
  12. package/lib/category-navigation.js +6 -3
  13. package/lib/compliance-export.js +8 -1
  14. package/lib/customer-notes.js +1 -1
  15. package/lib/customer-roles.js +1 -1
  16. package/lib/customer-surveys.js +2 -2
  17. package/lib/customers.js +1 -1
  18. package/lib/dispute-resolution.js +3 -3
  19. package/lib/dropship-forwarding.js +1 -1
  20. package/lib/dunning.js +1 -1
  21. package/lib/email-ab-tests.js +1 -1
  22. package/lib/email-campaigns.js +15 -2
  23. package/lib/email-warmup.js +1 -1
  24. package/lib/externaldb-d1.js +0 -8
  25. package/lib/gift-options.js +3 -3
  26. package/lib/knowledge-base.js +3 -3
  27. package/lib/line-gift-wrap.js +6 -2
  28. package/lib/live-chat.js +1 -1
  29. package/lib/notifications.js +1 -1
  30. package/lib/operator-accounts.js +1 -1
  31. package/lib/operator-help-center.js +3 -3
  32. package/lib/operator-inbox.js +2 -2
  33. package/lib/operator-roles.js +1 -1
  34. package/lib/order-notes.js +2 -2
  35. package/lib/order-ratings.js +3 -3
  36. package/lib/payment-methods.js +1 -1
  37. package/lib/payment-retries.js +1 -1
  38. package/lib/pixel-events.js +14 -6
  39. package/lib/plan-changes.js +1 -1
  40. package/lib/product-qa.js +1 -1
  41. package/lib/push-notifications.js +2 -2
  42. package/lib/reviews.js +3 -3
  43. package/lib/seller-signup.js +4 -4
  44. package/lib/sidebar-widgets.js +1 -1
  45. package/lib/sms-dispatcher.js +3 -3
  46. package/lib/stock-receipts.js +1 -1
  47. package/lib/storefront-dashboards.js +1 -1
  48. package/lib/storefront-forms.js +1 -1
  49. package/lib/storefront-pages.js +2 -2
  50. package/lib/subscription-billing.js +1 -1
  51. package/lib/subscription-controls.js +1 -1
  52. package/lib/suggestion-box.js +4 -4
  53. package/lib/support-tickets.js +3 -3
  54. package/lib/tenants.js +1 -1
  55. package/lib/text-guard.js +44 -2
  56. package/lib/theme-assets.js +1 -1
  57. package/lib/vendor/MANIFEST.json +12 -12
  58. package/lib/vendor/blamejs/CHANGELOG.md +32 -0
  59. package/lib/vendor/blamejs/NOTICE +1 -1
  60. package/lib/vendor/blamejs/README.md +1 -1
  61. package/lib/vendor/blamejs/lib/network-tls.js +98 -13
  62. package/lib/vendor/blamejs/lib/vendor/MANIFEST.json +12 -12
  63. package/lib/vendor/blamejs/lib/vendor/blamejs-pki.cjs +568 -304
  64. package/lib/vendor/blamejs/package.json +1 -1
  65. package/lib/vendor/blamejs/sbom.cdx.json +6 -6
  66. package/lib/vendors.js +3 -3
  67. package/lib/webhook-receiver.js +1 -1
  68. package/lib/webhook-subscriptions.js +1 -1
  69. package/lib/wishlist-sharing.js +1 -1
  70. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- // @blamejs/pki v0.4.13 — vendored (Apache-2.0). Zero-dep pure CJS.
1
+ // @blamejs/pki v0.4.14 — vendored (Apache-2.0). Zero-dep pure CJS.
2
2
  // https://github.com/blamejs/pki Exports: x509, crl, pkcs12, key, webcrypto, schema, csr, cms, ...
3
3
  // Backs lib/mtls-engine-default.js (PQC-capable CA + PKCS#12 engine).
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -139,7 +139,7 @@ var require_package = __commonJS({
139
139
  "node_modules/@blamejs/pki/package.json"(exports2, module2) {
140
140
  module2.exports = {
141
141
  name: "@blamejs/pki",
142
- version: "0.4.13",
142
+ version: "0.4.14",
143
143
  description: "Pure-JavaScript PKI toolkit that owns its stack \u2014 X.509, ASN.1/DER, CMS, PQC-first.",
144
144
  license: "Apache-2.0",
145
145
  author: "blamejs contributors",
@@ -3716,25 +3716,31 @@ var require_webcrypto = __commonJS({
3716
3716
  var iv = _toBuf(alg.iv, "AES-GCM iv");
3717
3717
  var aad = alg.additionalData ? _toBuf(alg.additionalData, "AES-GCM aad") : null;
3718
3718
  return _toArrayBuffer(_runCipher(function() {
3719
- var cipher = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-gcm", _secretBytes(key), iv, { authTagLength: (alg.tagLength || 128) / 8 });
3720
- if (aad) cipher.setAAD(aad);
3721
- var ct = Buffer.concat([cipher.update(buf), cipher.final()]);
3722
- return Buffer.concat([ct, cipher.getAuthTag()]);
3719
+ return _withSecretBytes(key, function(kb) {
3720
+ var cipher = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-gcm", kb, iv, { authTagLength: (alg.tagLength || 128) / 8 });
3721
+ if (aad) cipher.setAAD(aad);
3722
+ var ct = Buffer.concat([cipher.update(buf), cipher.final()]);
3723
+ return Buffer.concat([ct, cipher.getAuthTag()]);
3724
+ });
3723
3725
  }, "encrypt"));
3724
3726
  }
3725
3727
  if (name === "AES-CBC") {
3726
3728
  var cbcIv = _toBuf(alg.iv, "AES-CBC iv");
3727
3729
  return _toArrayBuffer(_runCipher(function() {
3728
- var c2 = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-cbc", _secretBytes(key), cbcIv);
3729
- return Buffer.concat([c2.update(buf), c2.final()]);
3730
+ return _withSecretBytes(key, function(kb) {
3731
+ var c2 = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-cbc", kb, cbcIv);
3732
+ return Buffer.concat([c2.update(buf), c2.final()]);
3733
+ });
3730
3734
  }, "encrypt"));
3731
3735
  }
3732
3736
  if (name === "AES-CTR") {
3733
3737
  _requireCtrLength128(alg);
3734
3738
  var ctrCounter = _toBuf(alg.counter, "AES-CTR counter");
3735
3739
  return _toArrayBuffer(_runCipher(function() {
3736
- var c3 = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-ctr", _secretBytes(key), ctrCounter);
3737
- return Buffer.concat([c3.update(buf), c3.final()]);
3740
+ return _withSecretBytes(key, function(kb) {
3741
+ var c3 = nodeCrypto.createCipheriv("aes-" + key.algorithm.length + "-ctr", kb, ctrCounter);
3742
+ return Buffer.concat([c3.update(buf), c3.final()]);
3743
+ });
3738
3744
  }, "encrypt"));
3739
3745
  }
3740
3746
  throw new WebCryptoError("webcrypto/not-supported", "encrypt: unsupported algorithm " + JSON.stringify(name));
@@ -3747,12 +3753,17 @@ var require_webcrypto = __commonJS({
3747
3753
  if (!ENCRYPT_DECRYPT_NAMES[name]) throw new WebCryptoError("webcrypto/not-supported", "decrypt: unsupported algorithm " + JSON.stringify(name));
3748
3754
  _requireAlgMatch(alg, key, "decrypt");
3749
3755
  if (name === "RSA-OAEP") {
3750
- return _toArrayBuffer(nodeCrypto.privateDecrypt({
3756
+ var oaepOut = nodeCrypto.privateDecrypt({
3751
3757
  key: key._handle,
3752
3758
  padding: nodeCrypto.constants.RSA_PKCS1_OAEP_PADDING,
3753
3759
  oaepHash: _hashNode(key.algorithm.hash, "decrypt"),
3754
3760
  oaepLabel: alg.label ? _toBuf(alg.label, "decrypt label") : void 0
3755
- }, buf));
3761
+ }, buf);
3762
+ try {
3763
+ return _toArrayBuffer(oaepOut);
3764
+ } finally {
3765
+ guard.secret.zeroize(oaepOut, WebCryptoError, "webcrypto/operation", "the RSA-OAEP decryption output");
3766
+ }
3756
3767
  }
3757
3768
  if (name === "AES-GCM") {
3758
3769
  var tagLen = (alg.tagLength || 128) / 8;
@@ -3762,31 +3773,57 @@ var require_webcrypto = __commonJS({
3762
3773
  var tag = buf.subarray(buf.length - tagLen);
3763
3774
  var gcmAad = alg.additionalData ? _toBuf(alg.additionalData, "AES-GCM aad") : null;
3764
3775
  return _toArrayBuffer(_runCipher(function() {
3765
- var d = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-gcm", _secretBytes(key), iv, { authTagLength: tagLen });
3766
- if (gcmAad) d.setAAD(gcmAad);
3767
- d.setAuthTag(tag);
3768
- return Buffer.concat([d.update(ct), d.final()]);
3776
+ return _withSecretBytes(key, function(kb) {
3777
+ var d = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-gcm", kb, iv, { authTagLength: tagLen });
3778
+ if (gcmAad) d.setAAD(gcmAad);
3779
+ d.setAuthTag(tag);
3780
+ return Buffer.concat([d.update(ct), d.final()]);
3781
+ });
3769
3782
  }, "decrypt"));
3770
3783
  }
3771
3784
  if (name === "AES-CBC") {
3772
3785
  var cbcIv2 = _toBuf(alg.iv, "AES-CBC iv");
3773
3786
  return _toArrayBuffer(_runCipher(function() {
3774
- var d2 = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-cbc", _secretBytes(key), cbcIv2);
3775
- return Buffer.concat([d2.update(buf), d2.final()]);
3787
+ return _withSecretBytes(key, function(kb) {
3788
+ var d2 = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-cbc", kb, cbcIv2);
3789
+ return Buffer.concat([d2.update(buf), d2.final()]);
3790
+ });
3776
3791
  }, "decrypt"));
3777
3792
  }
3778
3793
  if (name === "AES-CTR") {
3779
3794
  _requireCtrLength128(alg);
3780
3795
  var ctrCounter2 = _toBuf(alg.counter, "AES-CTR counter");
3781
3796
  return _toArrayBuffer(_runCipher(function() {
3782
- var d3 = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-ctr", _secretBytes(key), ctrCounter2);
3783
- return Buffer.concat([d3.update(buf), d3.final()]);
3797
+ return _withSecretBytes(key, function(kb) {
3798
+ var d3 = nodeCrypto.createDecipheriv("aes-" + key.algorithm.length + "-ctr", kb, ctrCounter2);
3799
+ return Buffer.concat([d3.update(buf), d3.final()]);
3800
+ });
3784
3801
  }, "decrypt"));
3785
3802
  }
3786
3803
  throw new WebCryptoError("webcrypto/not-supported", "decrypt: unsupported algorithm " + JSON.stringify(name));
3787
3804
  };
3788
- function _secretBytes(key) {
3789
- return key._handle.export();
3805
+ function _withSecretBytes(key, fn) {
3806
+ var kb = key._handle.export();
3807
+ var deferred = false;
3808
+ try {
3809
+ var out = fn(kb);
3810
+ if (out && typeof out.then === "function") {
3811
+ deferred = true;
3812
+ return out.then(function(v) {
3813
+ _wipeExport(kb);
3814
+ return v;
3815
+ }, function(e) {
3816
+ _wipeExport(kb);
3817
+ throw e;
3818
+ });
3819
+ }
3820
+ return out;
3821
+ } finally {
3822
+ if (!deferred) guard.secret.zeroize(kb, WebCryptoError, "webcrypto/operation", "the exported key material");
3823
+ }
3824
+ }
3825
+ function _wipeExport(kb) {
3826
+ guard.secret.zeroize(kb, WebCryptoError, "webcrypto/operation", "the exported key material");
3790
3827
  }
3791
3828
  function _requireCtrLength128(alg) {
3792
3829
  if (alg.length !== 128) {
@@ -3828,31 +3865,46 @@ var require_webcrypto = __commonJS({
3828
3865
  _requireAlgMatch(alg, alg.public, name + " public key");
3829
3866
  _requireOwnKey(alg.public, "deriveBits public key");
3830
3867
  var secret = nodeCrypto.diffieHellman({ privateKey: key._handle, publicKey: alg.public._handle });
3831
- if (length == null) return _toArrayBuffer(secret);
3832
- _requireDeriveLength(length, name);
3833
- if (length / 8 > secret.length) {
3834
- throw new WebCryptoError("webcrypto/operation", name + ": requested " + length + " bits but the shared secret has " + secret.length * 8);
3868
+ try {
3869
+ if (length == null) return _toArrayBuffer(secret);
3870
+ _requireDeriveLength(length, name);
3871
+ if (length / 8 > secret.length) {
3872
+ throw new WebCryptoError("webcrypto/operation", name + ": requested " + length + " bits but the shared secret has " + secret.length * 8);
3873
+ }
3874
+ return _toArrayBuffer(secret.subarray(0, length / 8));
3875
+ } finally {
3876
+ guard.secret.zeroize(secret, WebCryptoError, "webcrypto/operation", "the raw key-agreement shared secret");
3835
3877
  }
3836
- return _toArrayBuffer(secret.subarray(0, length / 8));
3837
3878
  }
3838
3879
  if (name === "HKDF") {
3839
3880
  _requireDeriveLength(length, "HKDF");
3840
- var ikm = _secretBytes(key);
3841
- try {
3881
+ return _withSecretBytes(key, function(ikm) {
3842
3882
  var derived = nodeCrypto.hkdfSync(_hashNode(alg.hash, "HKDF"), ikm, _toBuf(alg.salt, "HKDF salt"), _toBuf(alg.info || Buffer.alloc(0), "HKDF info"), length / 8);
3843
3883
  return derived instanceof ArrayBuffer ? derived : _toArrayBuffer(Buffer.from(derived));
3844
- } finally {
3845
- guard.secret.zeroize(ikm, WebCryptoError, "webcrypto/operation", "the HKDF input key material");
3846
- }
3884
+ });
3847
3885
  }
3848
3886
  if (name === "PBKDF2") {
3849
3887
  _requireDeriveLength(length, "PBKDF2");
3850
- var out = await _pbkdf2Async(_secretBytes(key), _toBuf(alg.salt, "PBKDF2 salt"), alg.iterations, length / 8, _hashNode(alg.hash, "PBKDF2"));
3851
- return _toArrayBuffer(out);
3888
+ return _withSecretBytes(key, function(pw) {
3889
+ return _pbkdf2Async(pw, _toBuf(alg.salt, "PBKDF2 salt"), alg.iterations, length / 8, _hashNode(alg.hash, "PBKDF2")).then(function(out) {
3890
+ try {
3891
+ return _toArrayBuffer(out);
3892
+ } finally {
3893
+ guard.secret.zeroize(out, WebCryptoError, "webcrypto/operation", "the PBKDF2 output");
3894
+ }
3895
+ });
3896
+ });
3852
3897
  }
3853
3898
  if (name === "X963KDF") {
3854
3899
  _requireDeriveLength(length, "X963KDF");
3855
- return _toArrayBuffer(_x963Kdf(_hashNode(alg.hash, "X963KDF"), _secretBytes(key), _toBuf(alg.info || Buffer.alloc(0), "X963KDF SharedInfo"), length / 8));
3900
+ return _withSecretBytes(key, function(z) {
3901
+ var derived = _x963Kdf(_hashNode(alg.hash, "X963KDF"), z, _toBuf(alg.info || Buffer.alloc(0), "X963KDF SharedInfo"), length / 8);
3902
+ try {
3903
+ return _toArrayBuffer(derived);
3904
+ } finally {
3905
+ guard.secret.zeroize(derived, WebCryptoError, "webcrypto/operation", "the X9.63 KDF output");
3906
+ }
3907
+ });
3856
3908
  }
3857
3909
  throw new WebCryptoError("webcrypto/not-supported", "deriveBits: unsupported algorithm " + JSON.stringify(name));
3858
3910
  }
@@ -3867,7 +3919,12 @@ var require_webcrypto = __commonJS({
3867
3919
  got += h.length;
3868
3920
  counter += 1;
3869
3921
  }
3870
- return Buffer.concat(blocks).subarray(0, lenBytes);
3922
+ var joined = Buffer.concat(blocks);
3923
+ var exact = Buffer.alloc(lenBytes);
3924
+ joined.copy(exact, 0, 0, lenBytes);
3925
+ guard.secret.zeroize(joined, WebCryptoError, "webcrypto/operation", "the KDF accumulator");
3926
+ for (var bi = 0; bi < blocks.length; bi++) guard.secret.zeroize(blocks[bi], WebCryptoError, "webcrypto/operation", "a KDF block");
3927
+ return exact;
3871
3928
  }
3872
3929
  SubtleCrypto.prototype.deriveBits = async function deriveBits(algorithm, key, length) {
3873
3930
  var alg = _normalizeAlg(algorithm, "deriveBits");
@@ -3894,7 +3951,11 @@ var require_webcrypto = __commonJS({
3894
3951
  bits = dk.length != null ? dk.length : null;
3895
3952
  }
3896
3953
  var raw = await _deriveBitsRaw(alg, baseKey, bits);
3897
- return this.importKey("raw", raw, dk, extractable, keyUsages);
3954
+ try {
3955
+ return await this.importKey("raw", raw, dk, extractable, keyUsages);
3956
+ } finally {
3957
+ guard.secret.zeroize(new Uint8Array(raw), WebCryptoError, "webcrypto/operation", "the derived key material");
3958
+ }
3898
3959
  };
3899
3960
  SubtleCrypto.prototype.encapsulateBits = async function encapsulateBits(algorithm, encapsulationKey) {
3900
3961
  var alg = _normalizeAlg(algorithm, "encapsulateBits");
@@ -3943,27 +4004,29 @@ var require_webcrypto = __commonJS({
3943
4004
  SubtleCrypto.prototype.wrapKey = async function wrapKey(format, key, wrappingKey, wrapAlgorithm) {
3944
4005
  var exported = await this.exportKey(format, key);
3945
4006
  var bytes = format === "jwk" ? Buffer.from(JSON.stringify(exported)) : Buffer.from(exported);
3946
- var alg = _normalizeAlg(wrapAlgorithm, "wrapKey");
3947
- _requireUsage(wrappingKey, "wrapKey");
3948
- if (alg.name !== "AES-KW" && !ENCRYPT_DECRYPT_NAMES[alg.name]) throw new WebCryptoError("webcrypto/not-supported", "wrapKey: unsupported algorithm " + JSON.stringify(alg.name));
3949
- _requireAlgMatch(alg, wrappingKey, "wrapKey");
3950
- if (alg.name === "AES-KW") {
3951
- if (bytes.length < 16 || bytes.length % 8 !== 0) {
3952
- throw new WebCryptoError("webcrypto/operation", "wrapKey: AES-KW requires the serialized key be a multiple of 8 bytes (>= 16); got " + bytes.length + " -- format " + JSON.stringify(format) + " is not AES-KW-wrappable");
3953
- }
3954
- var wkBytes = null;
3955
- try {
3956
- wkBytes = _secretBytes(wrappingKey);
3957
- var c = nodeCrypto.createCipheriv("aes" + wrappingKey.algorithm.length + "-wrap", wkBytes, Buffer.from("A6A6A6A6A6A6A6A6", "hex"));
3958
- return _toArrayBuffer(Buffer.concat([c.update(bytes), c.final()]));
3959
- } catch (e) {
3960
- throw new WebCryptoError("webcrypto/operation", "wrapKey: AES-KW key wrap failed", e);
3961
- } finally {
3962
- guard.secret.zeroize(wkBytes, WebCryptoError, "webcrypto/operation", "the AES-KW wrapping key");
4007
+ try {
4008
+ var alg = _normalizeAlg(wrapAlgorithm, "wrapKey");
4009
+ _requireUsage(wrappingKey, "wrapKey");
4010
+ if (alg.name !== "AES-KW" && !ENCRYPT_DECRYPT_NAMES[alg.name]) throw new WebCryptoError("webcrypto/not-supported", "wrapKey: unsupported algorithm " + JSON.stringify(alg.name));
4011
+ _requireAlgMatch(alg, wrappingKey, "wrapKey");
4012
+ if (alg.name === "AES-KW") {
4013
+ if (bytes.length < 16 || bytes.length % 8 !== 0) {
4014
+ throw new WebCryptoError("webcrypto/operation", "wrapKey: AES-KW requires the serialized key be a multiple of 8 bytes (>= 16); got " + bytes.length + " -- format " + JSON.stringify(format) + " is not AES-KW-wrappable");
4015
+ }
4016
+ try {
4017
+ return _withSecretBytes(wrappingKey, function(wkBytes) {
4018
+ var c = nodeCrypto.createCipheriv("aes" + wrappingKey.algorithm.length + "-wrap", wkBytes, Buffer.from("A6A6A6A6A6A6A6A6", "hex"));
4019
+ return _toArrayBuffer(Buffer.concat([c.update(bytes), c.final()]));
4020
+ });
4021
+ } catch (e) {
4022
+ throw new WebCryptoError("webcrypto/operation", "wrapKey: AES-KW key wrap failed", e);
4023
+ }
3963
4024
  }
4025
+ var wrapKeyClone = _cloneWithUsage(wrappingKey, "encrypt");
4026
+ return await this.encrypt(wrapAlgorithm, wrapKeyClone, bytes);
4027
+ } finally {
4028
+ guard.secret.zeroize(bytes, WebCryptoError, "webcrypto/operation", "the exported key being wrapped");
3964
4029
  }
3965
- var wrapKeyClone = _cloneWithUsage(wrappingKey, "encrypt");
3966
- return this.encrypt(wrapAlgorithm, wrapKeyClone, bytes);
3967
4030
  };
3968
4031
  SubtleCrypto.prototype.unwrapKey = async function unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages) {
3969
4032
  var alg = _normalizeAlg(unwrapAlgorithm, "unwrapKey");
@@ -3976,15 +4039,13 @@ var require_webcrypto = __commonJS({
3976
4039
  if (wrapped.length < 24 || wrapped.length % 8 !== 0) {
3977
4040
  throw new WebCryptoError("webcrypto/operation", "unwrapKey: AES-KW wrapped key must be a multiple of 8 bytes (>= 24); got " + wrapped.length);
3978
4041
  }
3979
- var kwBytes = null;
3980
4042
  try {
3981
- kwBytes = _secretBytes(unwrappingKey);
3982
- var d = nodeCrypto.createDecipheriv("aes" + unwrappingKey.algorithm.length + "-wrap", kwBytes, Buffer.from("A6A6A6A6A6A6A6A6", "hex"));
3983
- bytes = Buffer.concat([d.update(wrapped), d.final()]);
4043
+ bytes = _withSecretBytes(unwrappingKey, function(kwBytes) {
4044
+ var d = nodeCrypto.createDecipheriv("aes" + unwrappingKey.algorithm.length + "-wrap", kwBytes, Buffer.from("A6A6A6A6A6A6A6A6", "hex"));
4045
+ return Buffer.concat([d.update(wrapped), d.final()]);
4046
+ });
3984
4047
  } catch (e) {
3985
4048
  throw new WebCryptoError("webcrypto/operation", "unwrapKey: AES-KW key unwrap failed (integrity or length)", e);
3986
- } finally {
3987
- guard.secret.zeroize(kwBytes, WebCryptoError, "webcrypto/operation", "the AES-KW unwrapping key");
3988
4049
  }
3989
4050
  } else {
3990
4051
  var unwrapKeyClone = _cloneWithUsage(unwrappingKey, "decrypt");
@@ -4199,13 +4260,10 @@ var require_webcrypto = __commonJS({
4199
4260
  if (!key.extractable) throw new WebCryptoError("webcrypto/invalid-access", "key is not extractable");
4200
4261
  if (format === "jwk") return key._handle.export({ format: "jwk" });
4201
4262
  if (key.type === "secret") {
4202
- var raw = key._handle.export();
4203
- try {
4263
+ return _withSecretBytes(key, function(raw) {
4204
4264
  if (format === "raw") return _toArrayBuffer(raw);
4205
4265
  throw new WebCryptoError("webcrypto/not-supported", "exportKey: secret keys support 'raw' / 'jwk' only");
4206
- } finally {
4207
- guard.secret.zeroize(raw, WebCryptoError, "webcrypto/operation", "the exported secret key");
4208
- }
4266
+ });
4209
4267
  }
4210
4268
  if (format === "spki") return _toArrayBuffer(key._handle.export({ format: "der", type: "spki" }));
4211
4269
  if (format === "pkcs8") return _toArrayBuffer(key._handle.export({ format: "der", type: "pkcs8" }));
@@ -16326,9 +16384,12 @@ var require_pbes2 = __commonJS({
16326
16384
  CONTENT_MODE[O(r[0])] = r[2];
16327
16385
  });
16328
16386
  function passwordBytes(p, E, prefix) {
16329
- if (Buffer.isBuffer(p)) return p;
16330
- if (p instanceof Uint8Array) return Buffer.from(p);
16331
- if (typeof p === "string") return Buffer.from(p, "utf8");
16387
+ return passwordBytesOwned(p, E, prefix).bytes;
16388
+ }
16389
+ function passwordBytesOwned(p, E, prefix) {
16390
+ if (Buffer.isBuffer(p)) return { bytes: p, owned: false };
16391
+ if (p instanceof Uint8Array) return { bytes: Buffer.from(p), owned: true };
16392
+ if (typeof p === "string") return { bytes: Buffer.from(p, "utf8"), owned: true };
16332
16393
  throw E(prefix + "/bad-input", "a password must be a string, Buffer, or Uint8Array");
16333
16394
  }
16334
16395
  function assertIterations(n, E, prefix) {
@@ -16410,7 +16471,11 @@ var require_pbes2 = __commonJS({
16410
16471
  var salt = opts.salt != null ? assertSalt(opts.salt, E, prefix) : nodeCrypto.randomBytes(16);
16411
16472
  var iv = opts.iv != null ? opts.iv : nodeCrypto.randomBytes(16);
16412
16473
  var key = nodeCrypto.pbkdf2Sync(pwBytes, salt, iterations, keyBits / 8, prfNode);
16413
- return { algId: pbes2AlgId(salt, iterations, prf, cipherName, iv), ct: cbcEncrypt(key, iv, plaintext, keyBits) };
16474
+ try {
16475
+ return { algId: pbes2AlgId(salt, iterations, prf, cipherName, iv), ct: cbcEncrypt(key, iv, plaintext, keyBits) };
16476
+ } finally {
16477
+ guard.secret.zeroize(key, E, prefix + "/bad-input", "the password-derived encryption key");
16478
+ }
16414
16479
  }
16415
16480
  function pbes2Decrypt(pwBytes, params, ciphertext, opts, E, prefix) {
16416
16481
  var keyBits, iv, pb;
@@ -16437,6 +16502,8 @@ var require_pbes2 = __commonJS({
16437
16502
  return cbcDecrypt(dk, iv, ciphertext, keyBits);
16438
16503
  } catch (_e) {
16439
16504
  throw E(prefix + "/decrypt-failed", "decryption failed");
16505
+ } finally {
16506
+ guard.secret.zeroize(dk, E, prefix + "/bad-input", "the password-derived decryption key");
16440
16507
  }
16441
16508
  }
16442
16509
  function _hmacAlgId(name) {
@@ -16453,8 +16520,11 @@ var require_pbes2 = __commonJS({
16453
16520
  return subtle.importKey("raw", pwBytes, { name: "PBKDF2" }, false, ["deriveBits"]).then(function(baseKey) {
16454
16521
  return subtle.deriveBits({ name: "PBKDF2", salt, iterations: iterationCount, hash: prfHash }, baseKey, keyLength * 8);
16455
16522
  }).then(function(bits) {
16456
- return subtle.importKey("raw", Buffer.from(bits), { name: "HMAC", hash: macHash }, false, ["sign"]).then(function(hmacKey) {
16523
+ var mk = Buffer.from(bits);
16524
+ return subtle.importKey("raw", mk, { name: "HMAC", hash: macHash }, false, ["sign"]).then(function(hmacKey) {
16457
16525
  return subtle.sign({ name: "HMAC" }, hmacKey, message);
16526
+ }).finally(function() {
16527
+ guard.secret.zeroize(mk, webcrypto.WebCryptoError, "webcrypto/operation", "the PBMAC1 key");
16458
16528
  });
16459
16529
  }).then(function(sig) {
16460
16530
  return Buffer.from(sig);
@@ -16462,6 +16532,7 @@ var require_pbes2 = __commonJS({
16462
16532
  }
16463
16533
  module2.exports = {
16464
16534
  passwordBytes,
16535
+ passwordBytesOwned,
16465
16536
  assertIterations,
16466
16537
  assertSalt,
16467
16538
  prfNodeByName,
@@ -21047,52 +21118,56 @@ var require_cms_encrypt = __commonJS({
21047
21118
  var keyAlg = cert.subjectPublicKeyInfo.algorithm;
21048
21119
  var wrapName = _wrapOidForKek(cek.length);
21049
21120
  var ukm = opts.ukm ? guard.bytes.view(opts.ukm, CmsError, "cms/bad-input", "ukm") : null;
21050
- var ecdhPub, origKeyAlgId, kek;
21051
- if (keyAlg.oid === O("ecPublicKey")) {
21052
- var curveOid = asn1.read.oid(asn1.decode(keyAlg.parameters));
21053
- var ka = EC_KA[curveOid];
21054
- if (!ka) throw _err("cms/unsupported-algorithm", "unsupported recipient EC curve for kari");
21055
- var recipPub = await subtle.importKey("spki", cert.subjectPublicKeyInfo.bytes, { name: "ECDH", namedCurve: ka.curve }, false, []);
21056
- var eph = await subtle.generateKey({ name: "ECDH", namedCurve: ka.curve }, true, ["deriveBits"]);
21057
- origKeyAlgId = { spki: Buffer.from(await subtle.exportKey("spki", eph.publicKey)), scheme: ka.scheme };
21058
- var z = Buffer.from(await subtle.deriveBits({ name: "ECDH", public: recipPub }, eph.privateKey, null));
21059
- var zKey = await subtle.importKey("raw", z, { name: "X963KDF" }, false, ["deriveBits"]);
21060
- var sharedInfo = _eccSharedInfo(wrapName, ukm, cek.length);
21061
- kek = Buffer.from(await subtle.deriveBits({ name: "X963KDF", hash: ka.hash, info: sharedInfo }, zKey, cek.length * 8));
21062
- void ecdhPub;
21063
- } else if (MONT_KA[keyAlg.oid]) {
21064
- var mka = MONT_KA[keyAlg.oid];
21065
- var rPub = await subtle.importKey("spki", cert.subjectPublicKeyInfo.bytes, { name: mka.name }, false, []);
21066
- var meph = await subtle.generateKey({ name: mka.name }, true, ["deriveBits"]);
21067
- origKeyAlgId = { spki: Buffer.from(await subtle.exportKey("spki", meph.publicKey)), scheme: mka.scheme };
21068
- var mz = Buffer.from(await subtle.deriveBits({ name: mka.name, public: rPub }, meph.privateKey, null));
21069
- if (mz.every(function(x) {
21070
- return x === 0;
21071
- })) throw _err("cms/bad-input", "the X25519/X448 shared secret is all-zero (low-order point)");
21072
- var mzKey = await subtle.importKey("raw", mz, { name: "HKDF" }, false, ["deriveBits"]);
21073
- kek = Buffer.from(await subtle.deriveBits({ name: "HKDF", hash: mka.hkdf, salt: ukm || Buffer.alloc(0), info: _eccSharedInfo(wrapName, ukm, cek.length) }, mzKey, cek.length * 8));
21074
- } else {
21075
- throw _err("cms/unsupported-algorithm", "unsupported recipient key algorithm for kari");
21076
- }
21077
- var encryptedKey = await _aesKwWrap(kek, cek);
21078
- var origSpki = asn1.decode(origKeyAlgId.spki);
21079
- var origPubBits = origSpki.children[1];
21080
- var originatorKey = b.contextConstructed(1, Buffer.concat([origSpki.children[0].bytes, origPubBits.bytes]));
21081
- var ridNode;
21082
- _assertKeyIdentifier(opts.keyIdentifier);
21083
- if (opts.keyIdentifier === "subjectKeyIdentifier") {
21084
- var ski = _skiOf(cert);
21085
- if (!ski) throw _err("cms/bad-input", 'keyIdentifier: "subjectKeyIdentifier" requires the recipient certificate to carry a subjectKeyIdentifier extension');
21086
- ridNode = b.contextConstructed(0, b.octetString(ski));
21087
- } else {
21088
- ridNode = b.sequence([b.raw(cert.issuer.bytes), b.integer(cert.serialNumber)]);
21121
+ var ecdhPub, origKeyAlgId, kek, z, mz;
21122
+ try {
21123
+ if (keyAlg.oid === O("ecPublicKey")) {
21124
+ var curveOid = asn1.read.oid(asn1.decode(keyAlg.parameters));
21125
+ var ka = EC_KA[curveOid];
21126
+ if (!ka) throw _err("cms/unsupported-algorithm", "unsupported recipient EC curve for kari");
21127
+ var recipPub = await subtle.importKey("spki", cert.subjectPublicKeyInfo.bytes, { name: "ECDH", namedCurve: ka.curve }, false, []);
21128
+ var eph = await subtle.generateKey({ name: "ECDH", namedCurve: ka.curve }, true, ["deriveBits"]);
21129
+ origKeyAlgId = { spki: Buffer.from(await subtle.exportKey("spki", eph.publicKey)), scheme: ka.scheme };
21130
+ z = Buffer.from(await subtle.deriveBits({ name: "ECDH", public: recipPub }, eph.privateKey, null));
21131
+ var zKey = await subtle.importKey("raw", z, { name: "X963KDF" }, false, ["deriveBits"]);
21132
+ var sharedInfo = _eccSharedInfo(wrapName, ukm, cek.length);
21133
+ kek = Buffer.from(await subtle.deriveBits({ name: "X963KDF", hash: ka.hash, info: sharedInfo }, zKey, cek.length * 8));
21134
+ void ecdhPub;
21135
+ } else if (MONT_KA[keyAlg.oid]) {
21136
+ var mka = MONT_KA[keyAlg.oid];
21137
+ var rPub = await subtle.importKey("spki", cert.subjectPublicKeyInfo.bytes, { name: mka.name }, false, []);
21138
+ var meph = await subtle.generateKey({ name: mka.name }, true, ["deriveBits"]);
21139
+ origKeyAlgId = { spki: Buffer.from(await subtle.exportKey("spki", meph.publicKey)), scheme: mka.scheme };
21140
+ mz = Buffer.from(await subtle.deriveBits({ name: mka.name, public: rPub }, meph.privateKey, null));
21141
+ if (mz.every(function(x) {
21142
+ return x === 0;
21143
+ })) throw _err("cms/bad-input", "the X25519/X448 shared secret is all-zero (low-order point)");
21144
+ var mzKey = await subtle.importKey("raw", mz, { name: "HKDF" }, false, ["deriveBits"]);
21145
+ kek = Buffer.from(await subtle.deriveBits({ name: "HKDF", hash: mka.hkdf, salt: ukm || Buffer.alloc(0), info: _eccSharedInfo(wrapName, ukm, cek.length) }, mzKey, cek.length * 8));
21146
+ } else {
21147
+ throw _err("cms/unsupported-algorithm", "unsupported recipient key algorithm for kari");
21148
+ }
21149
+ var encryptedKey = await _aesKwWrap(kek, cek);
21150
+ var origSpki = asn1.decode(origKeyAlgId.spki);
21151
+ var origPubBits = origSpki.children[1];
21152
+ var originatorKey = b.contextConstructed(1, Buffer.concat([origSpki.children[0].bytes, origPubBits.bytes]));
21153
+ var ridNode;
21154
+ _assertKeyIdentifier(opts.keyIdentifier);
21155
+ if (opts.keyIdentifier === "subjectKeyIdentifier") {
21156
+ var ski = _skiOf(cert);
21157
+ if (!ski) throw _err("cms/bad-input", 'keyIdentifier: "subjectKeyIdentifier" requires the recipient certificate to carry a subjectKeyIdentifier extension');
21158
+ ridNode = b.contextConstructed(0, b.octetString(ski));
21159
+ } else {
21160
+ ridNode = b.sequence([b.raw(cert.issuer.bytes), b.integer(cert.serialNumber)]);
21161
+ }
21162
+ var rek = b.sequence([b.sequence([ridNode, b.octetString(encryptedKey)])]);
21163
+ var kekAlg = b.sequence([b.oid(O(origKeyAlgId.scheme)), _algId(wrapName, "absent")]);
21164
+ var kariKids = [b.integer(3n), b.explicit(0, originatorKey)];
21165
+ if (ukm) kariKids.push(b.explicit(1, b.octetString(ukm)));
21166
+ kariKids.push(kekAlg, rek);
21167
+ return { tag: 1, node: b.sequence(kariKids) };
21168
+ } finally {
21169
+ guard.secret.zeroizeAll([z, mz, kek], CmsError, "cms/bad-input", "the key-agreement shared secret");
21089
21170
  }
21090
- var rek = b.sequence([b.sequence([ridNode, b.octetString(encryptedKey)])]);
21091
- var kekAlg = b.sequence([b.oid(O(origKeyAlgId.scheme)), _algId(wrapName, "absent")]);
21092
- var kariKids = [b.integer(3n), b.explicit(0, originatorKey)];
21093
- if (ukm) kariKids.push(b.explicit(1, b.octetString(ukm)));
21094
- kariKids.push(kekAlg, rek);
21095
- return { tag: 1, node: b.sequence(kariKids) };
21096
21171
  }
21097
21172
  async function _buildKekri(cek, desc) {
21098
21173
  var kek = guard.bytes.view(desc.kek, CmsError, "cms/bad-input", "kek");
@@ -21103,19 +21178,26 @@ var require_cms_encrypt = __commonJS({
21103
21178
  return { tag: 2, node: b.sequence([b.integer(4n), kekid, _algId(wrapName, "absent"), b.octetString(encryptedKey)]) };
21104
21179
  }
21105
21180
  async function _buildPwri(cek, desc) {
21106
- var password = pbes2.passwordBytes(desc.password, _err, "cms");
21107
21181
  var iterations = pbes2.assertIterations(desc.iterations == null ? 6e5 : desc.iterations, _err, "cms");
21108
21182
  var salt = desc.salt ? pbes2.assertSalt(guard.bytes.view(desc.salt, CmsError, "cms/bad-input", "salt"), _err, "cms") : nodeCrypto.randomBytes(16);
21109
21183
  var prf = desc.prf || "hmacWithSHA256";
21184
+ _prfHash(prf);
21110
21185
  var innerKeyBytes = 32;
21186
+ var pwOwn = pbes2.passwordBytesOwned(desc.password, _err, "cms");
21187
+ var password = pwOwn.bytes;
21111
21188
  var kekKey = await subtle.importKey("raw", password, { name: "PBKDF2" }, false, ["deriveBits"]);
21112
21189
  var kek = Buffer.from(await subtle.deriveBits({ name: "PBKDF2", hash: _prfHash(prf), salt, iterations }, kekKey, innerKeyBytes * 8));
21113
- var iv = nodeCrypto.randomBytes(16);
21114
- var encryptedKey = _pwriWrapIv(kek, cek, iv);
21115
- var kdfParams = pbes2.pbkdf2ParamsSeq(salt, iterations, prf);
21116
- var kdfAlg = b.contextConstructed(0, Buffer.concat([b.oid(O("pbkdf2")), kdfParams]));
21117
- var keyEncAlg = b.sequence([b.oid(O("id-alg-PWRI-KEK")), b.sequence([b.oid(O("aes256-CBC")), b.octetString(iv)])]);
21118
- return { tag: 3, node: b.sequence([b.integer(0n), kdfAlg, keyEncAlg, b.octetString(encryptedKey)]) };
21190
+ if (pwOwn.owned) guard.secret.zeroize(password, CmsError, "cms/bad-input", "the password encoding");
21191
+ try {
21192
+ var iv = nodeCrypto.randomBytes(16);
21193
+ var encryptedKey = _pwriWrapIv(kek, cek, iv);
21194
+ var kdfParams = pbes2.pbkdf2ParamsSeq(salt, iterations, prf);
21195
+ var kdfAlg = b.contextConstructed(0, Buffer.concat([b.oid(O("pbkdf2")), kdfParams]));
21196
+ var keyEncAlg = b.sequence([b.oid(O("id-alg-PWRI-KEK")), b.sequence([b.oid(O("aes256-CBC")), b.octetString(iv)])]);
21197
+ return { tag: 3, node: b.sequence([b.integer(0n), kdfAlg, keyEncAlg, b.octetString(encryptedKey)]) };
21198
+ } finally {
21199
+ guard.secret.zeroize(kek, CmsError, "cms/bad-input", "the password-derived key-encryption key");
21200
+ }
21119
21201
  }
21120
21202
  var KEM_WRAP = {};
21121
21203
  KEM_WRAP[O("id-ml-kem-512")] = "aes128-wrap";
@@ -21175,17 +21257,23 @@ var require_cms_encrypt = __commonJS({
21175
21257
  var blk = 16;
21176
21258
  var padLen = body.length % blk === 0 ? 0 : blk - body.length % blk;
21177
21259
  if (body.length + padLen < 2 * blk) padLen += 2 * blk - (body.length + padLen);
21178
- return Buffer.concat([body, nodeCrypto.randomBytes(padLen)]);
21260
+ var wrapped = Buffer.concat([body, nodeCrypto.randomBytes(padLen)]);
21261
+ guard.secret.zeroizeAll([check, body], CmsError, "cms/bad-input", "a PWRI formatting intermediate");
21262
+ return wrapped;
21179
21263
  }
21180
21264
  function _pwriWrapIv(kek, cek, iv) {
21181
21265
  var wk = _pwriFormat(cek);
21182
- var c1 = nodeCrypto.createCipheriv("aes-256-cbc", kek, iv);
21183
- c1.setAutoPadding(false);
21184
- var pass1 = Buffer.concat([c1.update(wk), c1.final()]);
21185
- var iv2 = pass1.subarray(pass1.length - 16);
21186
- var c2 = nodeCrypto.createCipheriv("aes-256-cbc", kek, iv2);
21187
- c2.setAutoPadding(false);
21188
- return Buffer.concat([c2.update(pass1), c2.final()]);
21266
+ try {
21267
+ var c1 = nodeCrypto.createCipheriv("aes-256-cbc", kek, iv);
21268
+ c1.setAutoPadding(false);
21269
+ var pass1 = Buffer.concat([c1.update(wk), c1.final()]);
21270
+ var iv2 = pass1.subarray(pass1.length - 16);
21271
+ var c2 = nodeCrypto.createCipheriv("aes-256-cbc", kek, iv2);
21272
+ c2.setAutoPadding(false);
21273
+ return Buffer.concat([c2.update(pass1), c2.final()]);
21274
+ } finally {
21275
+ guard.secret.zeroize(wk, CmsError, "cms/bad-input", "the PWRI plaintext block");
21276
+ }
21189
21277
  }
21190
21278
  var PRF_HASH = { hmacWithSHA1: "SHA-1", hmacWithSHA256: "SHA-256", hmacWithSHA384: "SHA-384", hmacWithSHA512: "SHA-512" };
21191
21279
  function _prfHash(prf) {
@@ -21231,13 +21319,17 @@ var require_cms_encrypt = __commonJS({
21231
21319
  if (!ca) throw _err("cms/bad-input", "unsupported contentEncryptionAlgorithm " + JSON.stringify(algName));
21232
21320
  var contentType = opts.contentType || "data";
21233
21321
  var cek = nodeCrypto.randomBytes(ca.keyBits / 8);
21234
- if (!Array.isArray(recipients)) return _encryptedData(contentBytes, recipients, ca, contentType, opts, cek);
21235
- if (!recipients.length) throw _err("cms/bad-input", "at least one recipient is required (RFC 5652 sec. 6.1)");
21236
- var recips = [];
21237
- for (var i = 0; i < recipients.length; i++) recips.push(await _buildRecipient(cek, recipients[i], opts));
21238
- var riNodes = recips.map(_taggedRecipient);
21239
- if (ca.aead) return _emit(_authEnvelopedData(contentBytes, cek, ca, contentType, opts, riNodes, recips), "authEnvelopedData", opts);
21240
- return _emit(_envelopedData(contentBytes, cek, ca, contentType, riNodes, recips), "envelopedData", opts);
21322
+ try {
21323
+ if (!Array.isArray(recipients)) return _encryptedData(contentBytes, recipients, ca, contentType, opts, cek);
21324
+ if (!recipients.length) throw _err("cms/bad-input", "at least one recipient is required (RFC 5652 sec. 6.1)");
21325
+ var recips = [];
21326
+ for (var i = 0; i < recipients.length; i++) recips.push(await _buildRecipient(cek, recipients[i], opts));
21327
+ var riNodes = recips.map(_taggedRecipient);
21328
+ if (ca.aead) return _emit(_authEnvelopedData(contentBytes, cek, ca, contentType, opts, riNodes, recips), "authEnvelopedData", opts);
21329
+ return _emit(_envelopedData(contentBytes, cek, ca, contentType, riNodes, recips), "envelopedData", opts);
21330
+ } finally {
21331
+ guard.secret.zeroize(cek, CmsError, "cms/bad-input", "the content-encryption key");
21332
+ }
21241
21333
  }
21242
21334
  function _emit(inner, ctName, opts) {
21243
21335
  var ci = b.sequence([b.oid(O(ctName)), b.explicit(0, inner)]);
@@ -21287,16 +21379,22 @@ var require_cms_encrypt = __commonJS({
21287
21379
  return _emit(inner, "encryptedData", opts);
21288
21380
  }
21289
21381
  function _encryptedDataPbes2(contentBytes, desc, ca, contentType, iv, opts) {
21290
- var password = pbes2.passwordBytes(desc.password, _err, "cms");
21382
+ var pwOwn2 = pbes2.passwordBytesOwned(desc.password, _err, "cms");
21383
+ var password = pwOwn2.bytes;
21291
21384
  var iterations = pbes2.assertIterations(desc.iterations == null ? 6e5 : desc.iterations, _err, "cms");
21292
21385
  var salt = desc.salt ? pbes2.assertSalt(guard.bytes.view(desc.salt, CmsError, "cms/bad-input", "salt"), _err, "cms") : nodeCrypto.randomBytes(16);
21293
21386
  var prf = desc.prf || "hmacWithSHA256";
21294
21387
  var key = nodeCrypto.pbkdf2Sync(password, salt, iterations, ca.keyBits / 8, pbes2.prfNodeByName(prf, _err, "cms"));
21295
- var enc = pbes2.cbcEncrypt(key, iv, contentBytes, ca.keyBits);
21296
- var contentAlg = pbes2.pbes2AlgId(salt, iterations, prf, ca.oid, iv);
21297
- var eci = b.sequence([b.oid(O(contentType)), contentAlg, b.contextPrimitive(0, enc)]);
21298
- var inner = b.sequence([b.integer(0n), eci]);
21299
- return _emit(inner, "encryptedData", { pem: opts && opts.pem != null ? opts.pem : desc.pem });
21388
+ if (pwOwn2.owned) guard.secret.zeroize(password, CmsError, "cms/bad-input", "the password encoding");
21389
+ try {
21390
+ var enc = pbes2.cbcEncrypt(key, iv, contentBytes, ca.keyBits);
21391
+ var contentAlg = pbes2.pbes2AlgId(salt, iterations, prf, ca.oid, iv);
21392
+ var eci = b.sequence([b.oid(O(contentType)), contentAlg, b.contextPrimitive(0, enc)]);
21393
+ var inner = b.sequence([b.integer(0n), eci]);
21394
+ return _emit(inner, "encryptedData", { pem: opts && opts.pem != null ? opts.pem : desc.pem });
21395
+ } finally {
21396
+ guard.secret.zeroize(key, CmsError, "cms/bad-input", "the password-derived content-encryption key");
21397
+ }
21300
21398
  }
21301
21399
  function _gcmEncrypt(key, nonce, plaintext, aad, keyBits, tagLen) {
21302
21400
  var c = nodeCrypto.createCipheriv("aes-" + keyBits + "-gcm", key, nonce, { authTagLength: tagLen });
@@ -21327,61 +21425,65 @@ var require_cms_encrypt = __commonJS({
21327
21425
  var withAttrs = opts.authenticatedAttributes !== false;
21328
21426
  if (contentType !== "data" && !withAttrs) throw _err("cms/bad-input", "AuthenticatedData with a non-data contentType requires authenticated attributes (RFC 5652 sec. 9.1)");
21329
21427
  var macKey = nodeCrypto.randomBytes(MAC_KEY_OCTETS);
21330
- var recips = [];
21331
- for (var i = 0; i < recipients.length; i++) recips.push(await _buildRecipient(macKey, recipients[i], opts));
21332
- var riNodes = recips.map(_taggedRecipient);
21333
- var digestName = opts.digestAlgorithm || mac.node;
21334
- var digestAlgTagged = null, authAttrsDer = null, preimage;
21335
- if (withAttrs) {
21336
- if (!SUPPORTED_DIGEST[digestName]) throw _err("cms/bad-input", "unsupported digestAlgorithm " + JSON.stringify(digestName) + " (sha256/384/512)");
21337
- var mdDigest = nodeCrypto.createHash(digestName).update(contentBytes).digest();
21338
- var pairs = [
21339
- b.sequence([b.oid(O("contentType")), b.setOf([b.oid(O(contentType))])]),
21340
- b.sequence([b.oid(O("messageDigest")), b.setOf([b.octetString(mdDigest)])])
21341
- ];
21342
- if (opts.authAttrs && opts.authAttrs.length) pairs = pairs.concat(opts.authAttrs);
21343
- var seenTypes = {};
21344
- pairs.forEach(function(p) {
21345
- var node;
21346
- try {
21347
- node = asn1.decode(p);
21348
- } catch (e) {
21349
- throw _err("cms/bad-input", "an authenticated attribute is not well-formed DER", e);
21350
- }
21351
- if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length !== 2 || node.children[1].tagClass !== "universal" || node.children[1].tagNumber !== asn1.TAGS.SET || !node.children[1].children || node.children[1].children.length < 1) {
21352
- throw _err("cms/bad-input", "an authenticated attribute must be an Attribute SEQUENCE { type, non-empty SET OF value } (RFC 5652)");
21353
- }
21354
- var t;
21355
- try {
21356
- t = asn1.read.oid(node.children[0]);
21357
- } catch (e) {
21358
- throw _err("cms/bad-input", "an authenticated attribute type is not an OBJECT IDENTIFIER", e);
21359
- }
21360
- if (seenTypes[t]) throw _err("cms/bad-input", "authenticated attributes must not repeat an attribute type (RFC 5652): " + t);
21361
- seenTypes[t] = 1;
21362
- });
21363
- var setOf = b.setOf(pairs);
21364
- preimage = setOf;
21365
- authAttrsDer = b.contextConstructed(2, setOf.subarray(_tlvHeaderLen(setOf)));
21366
- digestAlgTagged = b.contextConstructed(1, b.oid(O(digestName)));
21367
- } else {
21368
- preimage = contentBytes;
21369
- }
21370
- var hmacKey = await subtle.importKey("raw", macKey, { name: "HMAC", hash: mac.wc }, false, ["sign"]);
21371
- var macValue = Buffer.from(await subtle.sign({ name: "HMAC" }, hmacKey, preimage));
21372
- var eci = b.sequence([b.oid(O(contentType)), b.explicit(0, b.octetString(contentBytes))]);
21373
- var kids = [b.integer(0n), b.setOf(riNodes), _algId(mac.oid)];
21374
- if (digestAlgTagged) kids.push(digestAlgTagged);
21375
- kids.push(eci);
21376
- if (authAttrsDer) kids.push(authAttrsDer);
21377
- kids.push(b.octetString(macValue));
21378
- var ci = b.sequence([b.oid(O("authData")), b.explicit(0, b.sequence(kids))]);
21379
21428
  try {
21380
- schemaCms.parse(ci);
21381
- } catch (e) {
21382
- throw e instanceof CmsError ? e : _err("cms/bad-input", "the supplied authenticated attributes produced an invalid AuthenticatedData", e);
21429
+ var recips = [];
21430
+ for (var i = 0; i < recipients.length; i++) recips.push(await _buildRecipient(macKey, recipients[i], opts));
21431
+ var riNodes = recips.map(_taggedRecipient);
21432
+ var digestName = opts.digestAlgorithm || mac.node;
21433
+ var digestAlgTagged = null, authAttrsDer = null, preimage;
21434
+ if (withAttrs) {
21435
+ if (!SUPPORTED_DIGEST[digestName]) throw _err("cms/bad-input", "unsupported digestAlgorithm " + JSON.stringify(digestName) + " (sha256/384/512)");
21436
+ var mdDigest = nodeCrypto.createHash(digestName).update(contentBytes).digest();
21437
+ var pairs = [
21438
+ b.sequence([b.oid(O("contentType")), b.setOf([b.oid(O(contentType))])]),
21439
+ b.sequence([b.oid(O("messageDigest")), b.setOf([b.octetString(mdDigest)])])
21440
+ ];
21441
+ if (opts.authAttrs && opts.authAttrs.length) pairs = pairs.concat(opts.authAttrs);
21442
+ var seenTypes = {};
21443
+ pairs.forEach(function(p) {
21444
+ var node;
21445
+ try {
21446
+ node = asn1.decode(p);
21447
+ } catch (e) {
21448
+ throw _err("cms/bad-input", "an authenticated attribute is not well-formed DER", e);
21449
+ }
21450
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length !== 2 || node.children[1].tagClass !== "universal" || node.children[1].tagNumber !== asn1.TAGS.SET || !node.children[1].children || node.children[1].children.length < 1) {
21451
+ throw _err("cms/bad-input", "an authenticated attribute must be an Attribute SEQUENCE { type, non-empty SET OF value } (RFC 5652)");
21452
+ }
21453
+ var t;
21454
+ try {
21455
+ t = asn1.read.oid(node.children[0]);
21456
+ } catch (e) {
21457
+ throw _err("cms/bad-input", "an authenticated attribute type is not an OBJECT IDENTIFIER", e);
21458
+ }
21459
+ if (seenTypes[t]) throw _err("cms/bad-input", "authenticated attributes must not repeat an attribute type (RFC 5652): " + t);
21460
+ seenTypes[t] = 1;
21461
+ });
21462
+ var setOf = b.setOf(pairs);
21463
+ preimage = setOf;
21464
+ authAttrsDer = b.contextConstructed(2, setOf.subarray(_tlvHeaderLen(setOf)));
21465
+ digestAlgTagged = b.contextConstructed(1, b.oid(O(digestName)));
21466
+ } else {
21467
+ preimage = contentBytes;
21468
+ }
21469
+ var hmacKey = await subtle.importKey("raw", macKey, { name: "HMAC", hash: mac.wc }, false, ["sign"]);
21470
+ var macValue = Buffer.from(await subtle.sign({ name: "HMAC" }, hmacKey, preimage));
21471
+ var eci = b.sequence([b.oid(O(contentType)), b.explicit(0, b.octetString(contentBytes))]);
21472
+ var kids = [b.integer(0n), b.setOf(riNodes), _algId(mac.oid)];
21473
+ if (digestAlgTagged) kids.push(digestAlgTagged);
21474
+ kids.push(eci);
21475
+ if (authAttrsDer) kids.push(authAttrsDer);
21476
+ kids.push(b.octetString(macValue));
21477
+ var ci = b.sequence([b.oid(O("authData")), b.explicit(0, b.sequence(kids))]);
21478
+ try {
21479
+ schemaCms.parse(ci);
21480
+ } catch (e) {
21481
+ throw e instanceof CmsError ? e : _err("cms/bad-input", "the supplied authenticated attributes produced an invalid AuthenticatedData", e);
21482
+ }
21483
+ return opts.pem ? schemaCms.pemEncode(ci, "CMS") : ci;
21484
+ } finally {
21485
+ guard.secret.zeroize(macKey, CmsError, "cms/bad-input", "the message-authentication key");
21383
21486
  }
21384
- return opts.pem ? schemaCms.pemEncode(ci, "CMS") : ci;
21385
21487
  }
21386
21488
  module2.exports = { encrypt, authenticate };
21387
21489
  }
@@ -21439,16 +21541,20 @@ var require_cms_decrypt = __commonJS({
21439
21541
  try {
21440
21542
  _assertSupported(candidates[ci].ri, keyMaterial);
21441
21543
  var cek = await _acquireCek(candidates[ci].ri, keyMaterial, opts);
21442
- var content = await _openContent(parsed, eci, cek, ct);
21443
- return {
21444
- content,
21445
- contentType: eci.contentType,
21446
- contentTypeName: oid.name(eci.contentType) || eci.contentType,
21447
- recipientType: candidates[ci].ri.type,
21448
- recipientIndex: candidates[ci].index,
21449
- contentEncryptionAlgorithm: eci.contentEncryptionAlgorithm.name || eci.contentEncryptionAlgorithm.oid,
21450
- authenticated: ct === "authEnvelopedData"
21451
- };
21544
+ try {
21545
+ var content = await _openContent(parsed, eci, cek, ct);
21546
+ return {
21547
+ content,
21548
+ contentType: eci.contentType,
21549
+ contentTypeName: oid.name(eci.contentType) || eci.contentType,
21550
+ recipientType: candidates[ci].ri.type,
21551
+ recipientIndex: candidates[ci].index,
21552
+ contentEncryptionAlgorithm: eci.contentEncryptionAlgorithm.name || eci.contentEncryptionAlgorithm.oid,
21553
+ authenticated: ct === "authEnvelopedData"
21554
+ };
21555
+ } finally {
21556
+ guard.secret.zeroize(cek, CmsError, "cms/bad-input", "the recovered content-encryption key");
21557
+ }
21452
21558
  } catch (e) {
21453
21559
  if (candidates.length === 1) throw e;
21454
21560
  }
@@ -21590,31 +21696,35 @@ var require_cms_decrypt = __commonJS({
21590
21696
  var kekBytes = WRAP_KEK_LENGTHS[wrapAlg.oid];
21591
21697
  if (!kekBytes) throw _err("cms/unsupported-algorithm", "unsupported kari key-wrap");
21592
21698
  var ukm = ri.ukm || null;
21593
- var kek;
21594
- if (_isMont(origSpki)) {
21595
- var mont = _montName(origSpki);
21596
- var recipPriv = await subtle.importKey("pkcs8", keyDer, { name: mont.name }, false, ["deriveBits"]);
21597
- var origPub = await subtle.importKey("spki", origSpki, { name: mont.name }, false, []);
21598
- var mz = Buffer.from(await subtle.deriveBits({ name: mont.name, public: origPub }, recipPriv, null));
21599
- if (mz.every(function(x) {
21600
- return x === 0;
21601
- })) throw _fail();
21602
- var mzKey = await subtle.importKey("raw", mz, { name: "HKDF" }, false, ["deriveBits"]);
21603
- kek = Buffer.from(await subtle.deriveBits({ name: "HKDF", hash: mont.hkdf, salt: ukm || Buffer.alloc(0), info: _eccSharedInfo(wrapAlg.name, ukm, kekBytes) }, mzKey, kekBytes * 8));
21604
- } else {
21605
- var origAlg = asn1.decode(origSpki).children[0];
21606
- var origHasParams = origAlg.children.length > 1;
21607
- var curveOid = km.cert != null && _ecCurveFromCert(km.cert) || (origHasParams ? asn1.read.oid(origAlg.children[1]) : null);
21608
- var curve = curveOid ? CURVE[curveOid] : null;
21609
- if (!curve) throw _err("cms/unsupported-algorithm", "unsupported or missing originator EC curve");
21610
- var origSpkiFull = origHasParams ? origSpki : _withEcCurveParams(origSpki, curveOid);
21611
- var recipEc = await subtle.importKey("pkcs8", keyDer, { name: "ECDH", namedCurve: curve.curve }, false, ["deriveBits"]);
21612
- var origEc = await subtle.importKey("spki", origSpkiFull, { name: "ECDH", namedCurve: curve.curve }, false, []);
21613
- var z = Buffer.from(await subtle.deriveBits({ name: "ECDH", public: origEc }, recipEc, null));
21614
- var zKey = await subtle.importKey("raw", z, { name: "X963KDF" }, false, ["deriveBits"]);
21615
- kek = Buffer.from(await subtle.deriveBits({ name: "X963KDF", hash: _x963Hash(scheme), info: _eccSharedInfo(wrapAlg.name, ukm, kekBytes) }, zKey, kekBytes * 8));
21616
- }
21617
- return await _aesKwUnwrap(kek, rek.encryptedKey);
21699
+ var kek, z, mz;
21700
+ try {
21701
+ if (_isMont(origSpki)) {
21702
+ var mont = _montName(origSpki);
21703
+ var recipPriv = await subtle.importKey("pkcs8", keyDer, { name: mont.name }, false, ["deriveBits"]);
21704
+ var origPub = await subtle.importKey("spki", origSpki, { name: mont.name }, false, []);
21705
+ mz = Buffer.from(await subtle.deriveBits({ name: mont.name, public: origPub }, recipPriv, null));
21706
+ if (mz.every(function(x) {
21707
+ return x === 0;
21708
+ })) throw _fail();
21709
+ var mzKey = await subtle.importKey("raw", mz, { name: "HKDF" }, false, ["deriveBits"]);
21710
+ kek = Buffer.from(await subtle.deriveBits({ name: "HKDF", hash: mont.hkdf, salt: ukm || Buffer.alloc(0), info: _eccSharedInfo(wrapAlg.name, ukm, kekBytes) }, mzKey, kekBytes * 8));
21711
+ } else {
21712
+ var origAlg = asn1.decode(origSpki).children[0];
21713
+ var origHasParams = origAlg.children.length > 1;
21714
+ var curveOid = km.cert != null && _ecCurveFromCert(km.cert) || (origHasParams ? asn1.read.oid(origAlg.children[1]) : null);
21715
+ var curve = curveOid ? CURVE[curveOid] : null;
21716
+ if (!curve) throw _err("cms/unsupported-algorithm", "unsupported or missing originator EC curve");
21717
+ var origSpkiFull = origHasParams ? origSpki : _withEcCurveParams(origSpki, curveOid);
21718
+ var recipEc = await subtle.importKey("pkcs8", keyDer, { name: "ECDH", namedCurve: curve.curve }, false, ["deriveBits"]);
21719
+ var origEc = await subtle.importKey("spki", origSpkiFull, { name: "ECDH", namedCurve: curve.curve }, false, []);
21720
+ z = Buffer.from(await subtle.deriveBits({ name: "ECDH", public: origEc }, recipEc, null));
21721
+ var zKey = await subtle.importKey("raw", z, { name: "X963KDF" }, false, ["deriveBits"]);
21722
+ kek = Buffer.from(await subtle.deriveBits({ name: "X963KDF", hash: _x963Hash(scheme), info: _eccSharedInfo(wrapAlg.name, ukm, kekBytes) }, zKey, kekBytes * 8));
21723
+ }
21724
+ return await _aesKwUnwrap(kek, rek.encryptedKey);
21725
+ } finally {
21726
+ guard.secret.zeroizeAll([z, mz, kek], CmsError, "cms/bad-input", "the key-agreement shared secret");
21727
+ }
21618
21728
  }
21619
21729
  async function _kekriCek(ri, km) {
21620
21730
  var kek = guard.bytes.view(km.kek, CmsError, "cms/bad-input", "kek");
@@ -21632,8 +21742,18 @@ var require_cms_decrypt = __commonJS({
21632
21742
  var innerBits = CONTENT_KEYBITS[innerOid];
21633
21743
  if (!innerBits || CONTENT_MODE[innerOid] !== "cbc") throw _err("cms/unsupported-algorithm", "unsupported pwri inner cipher");
21634
21744
  var iv = asn1.read.octetString(inner.children[1]);
21635
- var kek = nodeCrypto.pbkdf2Sync(pbes2.passwordBytes(km.password, _err, "cms"), pb.salt, pb.iterations, innerBits / 8, pb.prfNode);
21636
- return _pwriUnwrap(kek, ri.encryptedKey, iv, innerBits);
21745
+ var pw = pbes2.passwordBytesOwned(km.password, _err, "cms");
21746
+ var kek;
21747
+ try {
21748
+ kek = nodeCrypto.pbkdf2Sync(pw.bytes, pb.salt, pb.iterations, innerBits / 8, pb.prfNode);
21749
+ } finally {
21750
+ if (pw.owned) guard.secret.zeroize(pw.bytes, CmsError, "cms/bad-input", "the password encoding");
21751
+ }
21752
+ try {
21753
+ return _pwriUnwrap(kek, ri.encryptedKey, iv, innerBits);
21754
+ } finally {
21755
+ guard.secret.zeroize(kek, CmsError, "cms/bad-input", "the password-derived key-encryption key");
21756
+ }
21637
21757
  }
21638
21758
  async function _kemriCek(ri, km) {
21639
21759
  var k = ri.kemri;
@@ -21677,7 +21797,11 @@ var require_cms_decrypt = __commonJS({
21677
21797
  var keyBits = CONTENT_KEYBITS[alg.oid];
21678
21798
  if (!keyBits) throw _err("cms/unsupported-algorithm", "unsupported contentEncryptionAlgorithm " + alg.oid);
21679
21799
  if (eci.encryptedContent == null) throw _err("cms/no-encrypted-content", "the message has no encryptedContent (detached; supply it out of band)");
21680
- if (cek == null || cek.length !== keyBits / 8) cek = nodeCrypto.randomBytes(keyBits / 8);
21800
+ var substitute = null;
21801
+ if (cek == null || cek.length !== keyBits / 8) {
21802
+ substitute = nodeCrypto.randomBytes(keyBits / 8);
21803
+ cek = substitute;
21804
+ }
21681
21805
  try {
21682
21806
  if (ct === "authEnvelopedData") {
21683
21807
  var aad = parsed.authAttrsBytes != null ? _explicitSetOf(parsed.authAttrsBytes) : Buffer.alloc(0);
@@ -21689,6 +21813,8 @@ var require_cms_decrypt = __commonJS({
21689
21813
  } catch (e) {
21690
21814
  if (e instanceof CmsError && e.code !== "cms/decrypt-failed") throw e;
21691
21815
  throw _fail();
21816
+ } finally {
21817
+ guard.secret.zeroize(substitute, CmsError, "cms/bad-input", "the implicit-rejection substitute content key");
21692
21818
  }
21693
21819
  }
21694
21820
  function _gcmOpen(cek, nonce, ct, tag, aad, keyBits, icvLen) {
@@ -21732,11 +21858,19 @@ var require_cms_decrypt = __commonJS({
21732
21858
  }
21733
21859
  var keyBits = CONTENT_KEYBITS[encOid];
21734
21860
  if (!keyBits) throw _err("cms/unsupported-algorithm", "unsupported PBES2 content cipher " + encOid);
21735
- var key = nodeCrypto.pbkdf2Sync(pbes2.passwordBytes(km.password, _err, "cms"), pb.salt, pb.iterations, keyBits / 8, pb.prfNode);
21861
+ var pwE = pbes2.passwordBytesOwned(km.password, _err, "cms");
21862
+ var key;
21863
+ try {
21864
+ key = nodeCrypto.pbkdf2Sync(pwE.bytes, pb.salt, pb.iterations, keyBits / 8, pb.prfNode);
21865
+ } finally {
21866
+ if (pwE.owned) guard.secret.zeroize(pwE.bytes, CmsError, "cms/bad-input", "the password encoding");
21867
+ }
21736
21868
  try {
21737
21869
  return { content: pbes2.cbcDecrypt(key, iv, eci.encryptedContent, keyBits), contentType: eci.contentType, contentTypeName: oid.name(eci.contentType) || eci.contentType, recipientType: "password", recipientIndex: -1, contentEncryptionAlgorithm: oid.name(encOid) || encOid, authenticated: false };
21738
21870
  } catch (_e) {
21739
21871
  throw _fail();
21872
+ } finally {
21873
+ guard.secret.zeroize(key, CmsError, "cms/bad-input", "the password-derived content-encryption key");
21740
21874
  }
21741
21875
  }
21742
21876
  async function _aesKwUnwrap(kek, wrapped) {
@@ -21780,13 +21914,17 @@ var require_cms_decrypt = __commonJS({
21780
21914
  var d2 = nodeCrypto.createDecipheriv(alg, kek, iv);
21781
21915
  d2.setAutoPadding(false);
21782
21916
  var body = Buffer.concat([d2.update(pass1), d2.final()]);
21783
- var count = body[0];
21784
- if (count < 1 || count + 4 > body.length) throw _fail();
21785
- var cek = body.subarray(4, 4 + count);
21786
- var bad = 0;
21787
- for (var j = 0; j < 3; j++) bad |= body[1 + j] ^ 255 ^ cek[j];
21788
- if (bad !== 0) throw _fail();
21789
- return Buffer.from(cek);
21917
+ try {
21918
+ var count = body[0];
21919
+ if (count < 1 || count + 4 > body.length) throw _fail();
21920
+ var cek = body.subarray(4, 4 + count);
21921
+ var bad = 0;
21922
+ for (var j = 0; j < 3; j++) bad |= body[1 + j] ^ 255 ^ cek[j];
21923
+ if (bad !== 0) throw _fail();
21924
+ return Buffer.from(cek);
21925
+ } finally {
21926
+ guard.secret.zeroizeAll([body, pass1], CmsError, "cms/bad-input", "the PWRI plaintext block");
21927
+ }
21790
21928
  }
21791
21929
  function _oaepHashFromParams(paramsBytes) {
21792
21930
  if (paramsBytes == null) return "SHA-1";
@@ -21914,23 +22052,28 @@ var require_cms_decrypt = __commonJS({
21914
22052
  try {
21915
22053
  _assertSupported(candidates[ci].ri, km);
21916
22054
  var macKey = await _acquireCek(candidates[ci].ri, km, opts);
21917
- if (macKey == null || macKey.length < MAC_KEY_MIN_OCTETS) macKey = nodeCrypto.randomBytes(MAC_KEY_MIN_OCTETS);
21918
- var key = await subtle.importKey("raw", macKey, { name: "HMAC", hash }, false, ["verify"]);
21919
- if (!await subtle.verify({ name: "HMAC" }, key, Buffer.from(parsed.mac), preimage)) throw _fail();
21920
- if (mdCheck) {
21921
- var actual = Buffer.from(await subtle.digest(mdCheck.hash, content));
21922
- if (!actual.equals(mdCheck.declared)) throw _fail();
22055
+ var macSubstitute = null;
22056
+ if (macKey == null || macKey.length < MAC_KEY_MIN_OCTETS) macSubstitute = nodeCrypto.randomBytes(MAC_KEY_MIN_OCTETS);
22057
+ try {
22058
+ var key = await subtle.importKey("raw", macSubstitute || macKey, { name: "HMAC", hash }, false, ["verify"]);
22059
+ if (!await subtle.verify({ name: "HMAC" }, key, Buffer.from(parsed.mac), preimage)) throw _fail();
22060
+ if (mdCheck) {
22061
+ var actual = Buffer.from(await subtle.digest(mdCheck.hash, content));
22062
+ if (!actual.equals(mdCheck.declared)) throw _fail();
22063
+ }
22064
+ return {
22065
+ content: Buffer.from(content),
22066
+ contentType: parsed.encapContentInfo.eContentType,
22067
+ contentTypeName: oid.name(parsed.encapContentInfo.eContentType) || parsed.encapContentInfo.eContentType,
22068
+ recipientType: candidates[ci].ri.type,
22069
+ recipientIndex: candidates[ci].index,
22070
+ macAlgorithm: macAlg.name || macAlg.oid,
22071
+ digestAlgorithm: parsed.digestAlgorithm ? parsed.digestAlgorithm.name || parsed.digestAlgorithm.oid : null,
22072
+ authenticated: true
22073
+ };
22074
+ } finally {
22075
+ guard.secret.zeroizeAll([macKey, macSubstitute], CmsError, "cms/bad-input", "the message-authentication key");
21923
22076
  }
21924
- return {
21925
- content: Buffer.from(content),
21926
- contentType: parsed.encapContentInfo.eContentType,
21927
- contentTypeName: oid.name(parsed.encapContentInfo.eContentType) || parsed.encapContentInfo.eContentType,
21928
- recipientType: candidates[ci].ri.type,
21929
- recipientIndex: candidates[ci].index,
21930
- macAlgorithm: macAlg.name || macAlg.oid,
21931
- digestAlgorithm: parsed.digestAlgorithm ? parsed.digestAlgorithm.name || parsed.digestAlgorithm.oid : null,
21932
- authenticated: true
21933
- };
21934
22077
  } catch (e) {
21935
22078
  if (candidates.length === 1) throw e;
21936
22079
  }
@@ -25304,11 +25447,21 @@ var require_key = __commonJS({
25304
25447
  var iterations = pbes2.assertIterations(opts.iterations == null ? 6e5 : opts.iterations, _err, "key");
25305
25448
  var salt = opts.salt != null ? pbes2.assertSalt(guard.bytes.view(opts.salt, KeyError, "key/bad-input", "salt"), _err, "key") : nodeCrypto.randomBytes(16);
25306
25449
  var iv = nodeCrypto.randomBytes(16);
25307
- var dk = nodeCrypto.pbkdf2Sync(pbes2.passwordBytes(password, _err, "key"), salt, iterations, keyBits / 8, prfNode);
25308
- var ciphertext = pbes2.cbcEncrypt(dk, iv, der, keyBits);
25309
- var epki = b.sequence([pbes2.pbes2AlgId(salt, iterations, prf, cipherName, iv), b.octetString(ciphertext)]);
25310
- pkcs8.parseEncrypted(epki);
25311
- return opts.pem ? pkcs8.pemEncode(epki, "ENCRYPTED PRIVATE KEY") : epki;
25450
+ var pwK = pbes2.passwordBytesOwned(password, _err, "key");
25451
+ var dk;
25452
+ try {
25453
+ dk = nodeCrypto.pbkdf2Sync(pwK.bytes, salt, iterations, keyBits / 8, prfNode);
25454
+ } finally {
25455
+ if (pwK.owned) guard.secret.zeroize(pwK.bytes, KeyError, "key/bad-input", "the password encoding");
25456
+ }
25457
+ try {
25458
+ var ciphertext = pbes2.cbcEncrypt(dk, iv, der, keyBits);
25459
+ var epki = b.sequence([pbes2.pbes2AlgId(salt, iterations, prf, cipherName, iv), b.octetString(ciphertext)]);
25460
+ pkcs8.parseEncrypted(epki);
25461
+ return opts.pem ? pkcs8.pemEncode(epki, "ENCRYPTED PRIVATE KEY") : epki;
25462
+ } finally {
25463
+ guard.secret.zeroize(dk, KeyError, "key/bad-input", "the password-derived encryption key");
25464
+ }
25312
25465
  }
25313
25466
  async function decrypt(encrypted, password, opts) {
25314
25467
  opts = opts || {};
@@ -25328,7 +25481,13 @@ var require_key = __commonJS({
25328
25481
  return opts.pem ? pkcs8.pemEncode(plaintext, "PRIVATE KEY") : plaintext;
25329
25482
  }
25330
25483
  function _decryptPbes2(encAlg, ciphertext, password, opts) {
25331
- var plaintext = pbes2.pbes2Decrypt(pbes2.passwordBytes(password, _err, "key"), encAlg.parameters, ciphertext, opts, _err, "key");
25484
+ var pwD = pbes2.passwordBytesOwned(password, _err, "key");
25485
+ var plaintext;
25486
+ try {
25487
+ plaintext = pbes2.pbes2Decrypt(pwD.bytes, encAlg.parameters, ciphertext, opts, _err, "key");
25488
+ } finally {
25489
+ if (pwD.owned) guard.secret.zeroize(pwD.bytes, KeyError, "key/bad-input", "the password encoding");
25490
+ }
25332
25491
  try {
25333
25492
  pkcs8.parse(plaintext);
25334
25493
  } catch (_e) {
@@ -25860,7 +26019,11 @@ var require_pkcs12_build = __commonJS({
25860
26019
  var PRF_WC = { hmacWithSHA1: "SHA-1", hmacWithSHA256: "SHA-256", hmacWithSHA384: "SHA-384", hmacWithSHA512: "SHA-512" };
25861
26020
  var CIPHER_NAME = { "aes-128-cbc": "aes128-CBC", "aes-192-cbc": "aes192-CBC", "aes-256-cbc": "aes256-CBC" };
25862
26021
  var DIGEST_NAME = { sha1: "sha1", sha256: "sha256", sha384: "sha384", sha512: "sha512" };
25863
- function _p12Password(pw) {
26022
+ function _p12PasswordOwned(pw) {
26023
+ if (Buffer.isBuffer(pw)) return { bytes: pw, owned: false };
26024
+ return { bytes: _p12Encode(pw), owned: true };
26025
+ }
26026
+ function _p12Encode(pw) {
25864
26027
  if (pw == null) pw = "";
25865
26028
  if (Buffer.isBuffer(pw)) return pw;
25866
26029
  if (pw instanceof Uint8Array) return Buffer.from(pw);
@@ -25891,16 +26054,31 @@ var require_pkcs12_build = __commonJS({
25891
26054
  function _p12Kdf(hashName, id, pwBytes, salt, iterations, nBytes) {
25892
26055
  var uv = P12_KDF_UV[hashName], u = uv.u, v = uv.v;
25893
26056
  var D = Buffer.alloc(v, id);
25894
- var I = Buffer.concat([_blockFill(salt, v), _blockFill(pwBytes, v)]);
26057
+ var sFill = _blockFill(salt, v), pFill = _blockFill(pwBytes, v);
26058
+ var I = Buffer.concat([sFill, pFill]);
26059
+ guard.secret.zeroizeAll([sFill, pFill], Pkcs12Error, "pkcs12/bad-input", "a key-derivation fill");
25895
26060
  var c = Math.ceil(nBytes / u);
25896
- var out = Buffer.alloc(0);
26061
+ var out = Buffer.alloc(c * u);
25897
26062
  for (var i = 0; i < c; i++) {
25898
26063
  var A = Buffer.concat([D, I]);
25899
- for (var r = 0; r < iterations; r++) A = nodeCrypto.createHash(hashName).update(A).digest();
25900
- out = Buffer.concat([out, A]);
25901
- if (i < c - 1) _kdfStepC(I, _blockFill(A, v), v);
26064
+ for (var r = 0; r < iterations; r++) {
26065
+ var prev = A;
26066
+ A = nodeCrypto.createHash(hashName).update(A).digest();
26067
+ guard.secret.zeroize(prev, Pkcs12Error, "pkcs12/bad-input", "a key-derivation intermediate");
26068
+ }
26069
+ A.copy(out, i * u);
26070
+ if (i < c - 1) {
26071
+ var B = _blockFill(A, v);
26072
+ _kdfStepC(I, B, v);
26073
+ guard.secret.zeroize(B, Pkcs12Error, "pkcs12/bad-input", "a key-derivation intermediate");
26074
+ }
26075
+ guard.secret.zeroize(A, Pkcs12Error, "pkcs12/bad-input", "a key-derivation block");
25902
26076
  }
25903
- return out.subarray(0, nBytes);
26077
+ guard.secret.zeroize(I, Pkcs12Error, "pkcs12/bad-input", "the key-derivation input block");
26078
+ var exact = Buffer.alloc(nBytes);
26079
+ out.copy(exact, 0, 0, nBytes);
26080
+ guard.secret.zeroize(out, Pkcs12Error, "pkcs12/bad-input", "the key-derivation accumulator");
26081
+ return exact;
25904
26082
  }
25905
26083
  function _kdfStepC(I, B, v) {
25906
26084
  for (var j = 0; j < I.length; j += v) {
@@ -26067,10 +26245,16 @@ var require_pkcs12_build = __commonJS({
26067
26245
  var node = DIGEST_NAME[hash];
26068
26246
  if (!node || !P12_KDF_UV[node]) throw _err("pkcs12/unsupported-algorithm", "unsupported classic MAC hash " + JSON.stringify(hash) + " (sha1 / sha256 / sha384 / sha512)");
26069
26247
  var iter = _assertMacIter(macOpts.iterations == null ? DEFAULT_MAC_ITER : macOpts.iterations, CLASSIC_MAC_MAX_ITERATIONS);
26070
- var macKey = _p12Kdf(node, 3, _p12Password(password), salt, iter, P12_KDF_UV[node].u);
26071
- var digest = nodeCrypto.createHmac(node, macKey).update(authSafeDer).digest();
26072
- var digestInfo = b.sequence([b.sequence([b.oid(O(node)), b.nullValue()]), b.octetString(digest)]);
26073
- return b.sequence([digestInfo, b.octetString(salt), b.integer(BigInt(iter))]);
26248
+ var macPw = _p12PasswordOwned(password);
26249
+ var macKey = _p12Kdf(node, 3, macPw.bytes, salt, iter, P12_KDF_UV[node].u);
26250
+ if (macPw.owned) guard.secret.zeroize(macPw.bytes, Pkcs12Error, "pkcs12/bad-input", "the password encoding");
26251
+ try {
26252
+ var digest = nodeCrypto.createHmac(node, macKey).update(authSafeDer).digest();
26253
+ var digestInfo = b.sequence([b.sequence([b.oid(O(node)), b.nullValue()]), b.octetString(digest)]);
26254
+ return b.sequence([digestInfo, b.octetString(salt), b.integer(BigInt(iter))]);
26255
+ } finally {
26256
+ guard.secret.zeroize(macKey, Pkcs12Error, "pkcs12/bad-input", "the password-derived MAC key");
26257
+ }
26074
26258
  }
26075
26259
  if (algorithm === "pbmac1") {
26076
26260
  var prf = PBMAC1_PRF[hash];
@@ -26150,8 +26334,14 @@ var require_pkcs12_build = __commonJS({
26150
26334
  var node = (m.mac.hashName || "").toLowerCase();
26151
26335
  if (!P12_KDF_UV[node]) throw _err("pkcs12/unsupported-algorithm", "unsupported classic MAC hash " + m.mac.hashName);
26152
26336
  _capWork(m.mac.iterations, m.mac.macSalt, opts, void 0, CLASSIC_MAC_MAX_ITERATIONS);
26153
- var macKey = _p12Kdf(node, 3, _p12Password(password), m.mac.macSalt, m.mac.iterations, P12_KDF_UV[node].u);
26154
- computed = nodeCrypto.createHmac(node, macKey).update(m.macedBytes).digest();
26337
+ var vMacPw = _p12PasswordOwned(password);
26338
+ var macKey = _p12Kdf(node, 3, vMacPw.bytes, m.mac.macSalt, m.mac.iterations, P12_KDF_UV[node].u);
26339
+ if (vMacPw.owned) guard.secret.zeroize(vMacPw.bytes, Pkcs12Error, "pkcs12/bad-input", "the password encoding");
26340
+ try {
26341
+ computed = nodeCrypto.createHmac(node, macKey).update(m.macedBytes).digest();
26342
+ } finally {
26343
+ guard.secret.zeroize(macKey, Pkcs12Error, "pkcs12/bad-input", "the password-derived MAC key");
26344
+ }
26155
26345
  } else {
26156
26346
  var kdf = m.mac.pbmac1.kdf;
26157
26347
  var prfWc = PRF_WC[kdf.prfName];
@@ -26259,15 +26449,18 @@ var require_pkcs12_build = __commonJS({
26259
26449
  var u = P12_KDF_UV[scheme.hash].u;
26260
26450
  budget.rounds -= (Math.ceil(scheme.keyLen / u) + Math.ceil(scheme.ivLen / u)) * iterations;
26261
26451
  if (budget.rounds < 0) throw _err("pkcs12/iteration-limit", "the store's aggregate legacy PBE key-derivation work exceeds the budget (a hostile many-bag store)");
26262
- var p12pw = _p12Password(password);
26263
- var keyM = _p12Kdf(scheme.hash, 1, p12pw, salt, iterations, scheme.keyLen);
26264
- var iv = _p12Kdf(scheme.hash, 2, p12pw, salt, iterations, scheme.ivLen);
26452
+ var p12pw = _p12PasswordOwned(password);
26453
+ var keyM = _p12Kdf(scheme.hash, 1, p12pw.bytes, salt, iterations, scheme.keyLen);
26454
+ var iv = _p12Kdf(scheme.hash, 2, p12pw.bytes, salt, iterations, scheme.ivLen);
26455
+ if (p12pw.owned) guard.secret.zeroize(p12pw.bytes, Pkcs12Error, "pkcs12/bad-input", "the password encoding");
26265
26456
  try {
26266
26457
  if (scheme.rc2) return rc2.cbcDecrypt(keyM, scheme.rc2, iv, ct, _err, "pkcs12/decrypt-failed");
26267
26458
  var d = nodeCrypto.createDecipheriv(scheme.cipher, keyM, iv);
26268
26459
  return Buffer.concat([d.update(ct), d.final()]);
26269
26460
  } catch (_e) {
26270
26461
  throw _err("pkcs12/decrypt-failed", "decryption failed");
26462
+ } finally {
26463
+ guard.secret.zeroize(keyM, Pkcs12Error, "pkcs12/bad-input", "the password-derived decryption key");
26271
26464
  }
26272
26465
  }
26273
26466
  function _decryptBag(ea, ct, password, opts, budget) {
@@ -26780,13 +26973,25 @@ var require_hpke = __commonJS({
26780
26973
  if (len > 255 * Nh) throw _err("hpke/export-length", "requested length " + len + " exceeds 255*Nh");
26781
26974
  var out = [], t = Buffer.alloc(0), n = Math.ceil(len / Nh);
26782
26975
  for (var i = 1; i <= n; i++) {
26783
- t = nodeCrypto.createHmac(hash, prk).update(concat([t, info, Buffer.from([i])])).digest();
26976
+ var fed = concat([t, info, Buffer.from([i])]);
26977
+ t = nodeCrypto.createHmac(hash, prk).update(fed).digest();
26978
+ guard.secret.zeroize(fed, HpkeError, "hpke/bad-input", "an expand feedback input");
26784
26979
  out.push(t);
26785
26980
  }
26786
- return concat(out).subarray(0, len);
26981
+ var joined = concat(out);
26982
+ var exact = Buffer.alloc(len);
26983
+ joined.copy(exact, 0, 0, len);
26984
+ guard.secret.zeroize(joined, HpkeError, "hpke/bad-input", "the KDF accumulator");
26985
+ for (var oi = 0; oi < out.length; oi++) guard.secret.zeroize(out[oi], HpkeError, "hpke/bad-input", "a KDF block");
26986
+ return exact;
26787
26987
  }
26788
26988
  function _labeledExtract(kdf, suiteId, salt, label, ikm) {
26789
- return _extract(kdf.hash, kdf.Nh, salt, concat([HPKE_V1, suiteId, L(label), ikm]));
26989
+ var labeled = concat([HPKE_V1, suiteId, L(label), ikm]);
26990
+ try {
26991
+ return _extract(kdf.hash, kdf.Nh, salt, labeled);
26992
+ } finally {
26993
+ guard.secret.zeroize(labeled, HpkeError, "hpke/bad-input", "the labeled extract input");
26994
+ }
26790
26995
  }
26791
26996
  function _labeledExpand(kdf, suiteId, prk, label, info, len) {
26792
26997
  return _expand(kdf.hash, kdf.Nh, prk, concat([i2osp(len, 2), HPKE_V1, suiteId, L(label), info]), len);
@@ -26850,7 +27055,11 @@ var require_hpke = __commonJS({
26850
27055
  function _extractAndExpand(kem, dh, kemContext) {
26851
27056
  var kdf = _kdf(kem.kdf), sid = _kemSuiteId(_kemId(kem));
26852
27057
  var eaePrk = _labeledExtract(kdf, sid, Buffer.alloc(0), "eae_prk", dh);
26853
- return _labeledExpand(kdf, sid, eaePrk, "shared_secret", kemContext, kem.Nsecret);
27058
+ try {
27059
+ return _labeledExpand(kdf, sid, eaePrk, "shared_secret", kemContext, kem.Nsecret);
27060
+ } finally {
27061
+ guard.secret.zeroize(eaePrk, HpkeError, "hpke/bad-input", "the KEM extract PRK");
27062
+ }
26854
27063
  }
26855
27064
  function _kemId(kem) {
26856
27065
  for (var k in KEMS) if (KEMS[k] === kem) return Number(k);
@@ -26861,25 +27070,56 @@ var require_hpke = __commonJS({
26861
27070
  var kp = _generate(kem);
26862
27071
  return { skE: kp.privateKey, enc: _exportPublic(kem, kp.publicKey) };
26863
27072
  }
27073
+ function _wipeDh(dh) {
27074
+ guard.secret.zeroize(dh, HpkeError, "hpke/bad-input", "the KEM shared secret");
27075
+ }
26864
27076
  function _encap(kem, pkR, pkRm, eph) {
26865
27077
  var e = _ephemeral(kem, eph);
26866
- var dh = _dh(e.skE, pkR);
26867
- return { sharedSecret: _extractAndExpand(kem, dh, concat([e.enc, pkRm])), enc: e.enc };
27078
+ var dh = null;
27079
+ try {
27080
+ dh = _dh(e.skE, pkR);
27081
+ return { sharedSecret: _extractAndExpand(kem, dh, concat([e.enc, pkRm])), enc: e.enc };
27082
+ } finally {
27083
+ _wipeDh(dh);
27084
+ }
26868
27085
  }
26869
27086
  function _decap(kem, enc, skR, pkRm) {
26870
27087
  var pkE = _importPublic(kem, enc);
26871
- var dh = _dh(skR, pkE);
26872
- return _extractAndExpand(kem, dh, concat([enc, pkRm]));
27088
+ var dh = null;
27089
+ try {
27090
+ dh = _dh(skR, pkE);
27091
+ return _extractAndExpand(kem, dh, concat([enc, pkRm]));
27092
+ } finally {
27093
+ _wipeDh(dh);
27094
+ }
26873
27095
  }
26874
27096
  function _authEncap(kem, pkR, pkRm, skS, pkSm, eph) {
26875
27097
  var e = _ephemeral(kem, eph);
26876
- var dh = concat([_dh(e.skE, pkR), _dh(skS, pkR)]);
26877
- return { sharedSecret: _extractAndExpand(kem, dh, concat([e.enc, pkRm, pkSm])), enc: e.enc };
27098
+ var dh1 = null, dh2 = null, dh = null;
27099
+ try {
27100
+ dh1 = _dh(e.skE, pkR);
27101
+ dh2 = _dh(skS, pkR);
27102
+ dh = concat([dh1, dh2]);
27103
+ return { sharedSecret: _extractAndExpand(kem, dh, concat([e.enc, pkRm, pkSm])), enc: e.enc };
27104
+ } finally {
27105
+ _wipeDh(dh1);
27106
+ _wipeDh(dh2);
27107
+ _wipeDh(dh);
27108
+ }
26878
27109
  }
26879
27110
  function _authDecap(kem, enc, skR, pkRm, pkS, pkSm) {
26880
27111
  var pkE = _importPublic(kem, enc);
26881
- var dh = concat([_dh(skR, pkE), _dh(skR, pkS)]);
26882
- return _extractAndExpand(kem, dh, concat([enc, pkRm, pkSm]));
27112
+ var dh1 = null, dh2 = null, dh = null;
27113
+ try {
27114
+ dh1 = _dh(skR, pkE);
27115
+ dh2 = _dh(skR, pkS);
27116
+ dh = concat([dh1, dh2]);
27117
+ return _extractAndExpand(kem, dh, concat([enc, pkRm, pkSm]));
27118
+ } finally {
27119
+ _wipeDh(dh1);
27120
+ _wipeDh(dh2);
27121
+ _wipeDh(dh);
27122
+ }
26883
27123
  }
26884
27124
  function _hpkeSuiteId(kemId, kdfId, aeadId) {
26885
27125
  return concat([L("HPKE"), i2osp(kemId, 2), i2osp(kdfId, 2), i2osp(aeadId, 2)]);
@@ -26897,13 +27137,17 @@ var require_hpke = __commonJS({
26897
27137
  var infoHash = _labeledExtract(kdf, sid, Buffer.alloc(0), "info_hash", info);
26898
27138
  var ksc = concat([Buffer.from([mode]), pskIdHash, infoHash]);
26899
27139
  var secret = _labeledExtract(kdf, sid, sharedSecret, "secret", psk);
26900
- var exporterSecret = _labeledExpand(kdf, sid, secret, "exp", ksc, kdf.Nh);
26901
- var key = null, baseNonce = null;
26902
- if (!aead.exportOnly) {
26903
- key = _labeledExpand(kdf, sid, secret, "key", ksc, aead.Nk);
26904
- baseNonce = _labeledExpand(kdf, sid, secret, "base_nonce", ksc, aead.Nn);
27140
+ try {
27141
+ var exporterSecret = _labeledExpand(kdf, sid, secret, "exp", ksc, kdf.Nh);
27142
+ var key = null, baseNonce = null;
27143
+ if (!aead.exportOnly) {
27144
+ key = _labeledExpand(kdf, sid, secret, "key", ksc, aead.Nk);
27145
+ baseNonce = _labeledExpand(kdf, sid, secret, "base_nonce", ksc, aead.Nn);
27146
+ }
27147
+ return new Context(suite, key, baseNonce, exporterSecret, role);
27148
+ } finally {
27149
+ guard.secret.zeroize(secret, HpkeError, "hpke/bad-input", "the key-schedule PRK");
26905
27150
  }
26906
- return new Context(suite, key, baseNonce, exporterSecret, role);
26907
27151
  }
26908
27152
  function Context(suite, key, baseNonce, exporterSecret, role) {
26909
27153
  this._suite = suite;
@@ -26913,6 +27157,9 @@ var require_hpke = __commonJS({
26913
27157
  this._seq = 0n;
26914
27158
  this._role = role;
26915
27159
  }
27160
+ Context.prototype._dispose = function() {
27161
+ guard.secret.zeroizeAll([this._key, this._baseNonce, this._exporterSecret], HpkeError, "hpke/bad-input", "the HPKE context key material");
27162
+ };
26916
27163
  Context.prototype._nonce = function() {
26917
27164
  var aead = this._suite.aead;
26918
27165
  var seqBytes = i2osp(this._seq, aead.Nn);
@@ -27031,14 +27278,31 @@ var require_hpke = __commonJS({
27031
27278
  } else {
27032
27279
  ss = _decap(kem, _buf(enc), r.key, r.pkm);
27033
27280
  }
27034
- return _keySchedule(suite, mode, ss, _buf(opts.info), _buf(opts.psk), _buf(opts.pskId), "R");
27281
+ try {
27282
+ return _keySchedule(suite, mode, ss, _buf(opts.info), _buf(opts.psk), _buf(opts.pskId), "R");
27283
+ } finally {
27284
+ guard.secret.zeroize(ss, HpkeError, "hpke/bad-input", "the KEM shared secret");
27285
+ }
27035
27286
  }
27036
27287
  function seal(ids, pkR, opts, aad, pt) {
27037
- var s = _setupS(ids, pkR, opts);
27038
- return { enc: s.enc, ct: s.context.seal(_buf(aad), _buf(pt)) };
27288
+ var s = null;
27289
+ try {
27290
+ s = _setupS(ids, pkR, opts);
27291
+ return { enc: s.enc, ct: s.context.seal(_buf(aad), _buf(pt)) };
27292
+ } finally {
27293
+ if (s) {
27294
+ s.context._dispose();
27295
+ guard.secret.zeroize(s.sharedSecret, HpkeError, "hpke/bad-input", "the KEM shared secret");
27296
+ }
27297
+ }
27039
27298
  }
27040
27299
  function open(ids, enc, skR, opts, aad, ct) {
27041
- return _setupR(ids, enc, skR, opts).open(_buf(aad), _buf(ct));
27300
+ var ctx = _setupR(ids, enc, skR, opts);
27301
+ try {
27302
+ return ctx.open(_buf(aad), _buf(ct));
27303
+ } finally {
27304
+ ctx._dispose();
27305
+ }
27042
27306
  }
27043
27307
  module2.exports = {
27044
27308
  suites,