decrypt.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generalDecrypt = void 0;
  4. const decrypt_js_1 = require("../flattened/decrypt.js");
  5. const errors_js_1 = require("../../util/errors.js");
  6. const is_object_js_1 = require("../../lib/is_object.js");
  7. async function generalDecrypt(jwe, key, options) {
  8. if (!(0, is_object_js_1.default)(jwe)) {
  9. throw new errors_js_1.JWEInvalid('General JWE must be an object');
  10. }
  11. if (!Array.isArray(jwe.recipients) || !jwe.recipients.every(is_object_js_1.default)) {
  12. throw new errors_js_1.JWEInvalid('JWE Recipients missing or incorrect type');
  13. }
  14. if (!jwe.recipients.length) {
  15. throw new errors_js_1.JWEInvalid('JWE Recipients has no members');
  16. }
  17. for (const recipient of jwe.recipients) {
  18. try {
  19. return await (0, decrypt_js_1.flattenedDecrypt)({
  20. aad: jwe.aad,
  21. ciphertext: jwe.ciphertext,
  22. encrypted_key: recipient.encrypted_key,
  23. header: recipient.header,
  24. iv: jwe.iv,
  25. protected: jwe.protected,
  26. tag: jwe.tag,
  27. unprotected: jwe.unprotected,
  28. }, key, options);
  29. }
  30. catch {
  31. }
  32. }
  33. throw new errors_js_1.JWEDecryptionFailed();
  34. }
  35. exports.generalDecrypt = generalDecrypt;