decrypt.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { flattenedDecrypt } from '../flattened/decrypt.js';
  2. import { JWEDecryptionFailed, JWEInvalid } from '../../util/errors.js';
  3. import isObject from '../../lib/is_object.js';
  4. export async function generalDecrypt(jwe, key, options) {
  5. if (!isObject(jwe)) {
  6. throw new JWEInvalid('General JWE must be an object');
  7. }
  8. if (!Array.isArray(jwe.recipients) || !jwe.recipients.every(isObject)) {
  9. throw new JWEInvalid('JWE Recipients missing or incorrect type');
  10. }
  11. if (!jwe.recipients.length) {
  12. throw new JWEInvalid('JWE Recipients has no members');
  13. }
  14. for (const recipient of jwe.recipients) {
  15. try {
  16. return await flattenedDecrypt({
  17. aad: jwe.aad,
  18. ciphertext: jwe.ciphertext,
  19. encrypted_key: recipient.encrypted_key,
  20. header: recipient.header,
  21. iv: jwe.iv,
  22. protected: jwe.protected,
  23. tag: jwe.tag,
  24. unprotected: jwe.unprotected,
  25. }, key, options);
  26. }
  27. catch {
  28. }
  29. }
  30. throw new JWEDecryptionFailed();
  31. }