decrypt.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.flattenedDecrypt = void 0;
  4. const base64url_js_1 = require("../../runtime/base64url.js");
  5. const decrypt_js_1 = require("../../runtime/decrypt.js");
  6. const zlib_js_1 = require("../../runtime/zlib.js");
  7. const errors_js_1 = require("../../util/errors.js");
  8. const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
  9. const is_object_js_1 = require("../../lib/is_object.js");
  10. const decrypt_key_management_js_1 = require("../../lib/decrypt_key_management.js");
  11. const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
  12. const cek_js_1 = require("../../lib/cek.js");
  13. const validate_crit_js_1 = require("../../lib/validate_crit.js");
  14. const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
  15. async function flattenedDecrypt(jwe, key, options) {
  16. var _a;
  17. if (!(0, is_object_js_1.default)(jwe)) {
  18. throw new errors_js_1.JWEInvalid('Flattened JWE must be an object');
  19. }
  20. if (jwe.protected === undefined && jwe.header === undefined && jwe.unprotected === undefined) {
  21. throw new errors_js_1.JWEInvalid('JOSE Header missing');
  22. }
  23. if (typeof jwe.iv !== 'string') {
  24. throw new errors_js_1.JWEInvalid('JWE Initialization Vector missing or incorrect type');
  25. }
  26. if (typeof jwe.ciphertext !== 'string') {
  27. throw new errors_js_1.JWEInvalid('JWE Ciphertext missing or incorrect type');
  28. }
  29. if (typeof jwe.tag !== 'string') {
  30. throw new errors_js_1.JWEInvalid('JWE Authentication Tag missing or incorrect type');
  31. }
  32. if (jwe.protected !== undefined && typeof jwe.protected !== 'string') {
  33. throw new errors_js_1.JWEInvalid('JWE Protected Header incorrect type');
  34. }
  35. if (jwe.encrypted_key !== undefined && typeof jwe.encrypted_key !== 'string') {
  36. throw new errors_js_1.JWEInvalid('JWE Encrypted Key incorrect type');
  37. }
  38. if (jwe.aad !== undefined && typeof jwe.aad !== 'string') {
  39. throw new errors_js_1.JWEInvalid('JWE AAD incorrect type');
  40. }
  41. if (jwe.header !== undefined && !(0, is_object_js_1.default)(jwe.header)) {
  42. throw new errors_js_1.JWEInvalid('JWE Shared Unprotected Header incorrect type');
  43. }
  44. if (jwe.unprotected !== undefined && !(0, is_object_js_1.default)(jwe.unprotected)) {
  45. throw new errors_js_1.JWEInvalid('JWE Per-Recipient Unprotected Header incorrect type');
  46. }
  47. let parsedProt;
  48. if (jwe.protected) {
  49. try {
  50. const protectedHeader = (0, base64url_js_1.decode)(jwe.protected);
  51. parsedProt = JSON.parse(buffer_utils_js_1.decoder.decode(protectedHeader));
  52. }
  53. catch {
  54. throw new errors_js_1.JWEInvalid('JWE Protected Header is invalid');
  55. }
  56. }
  57. if (!(0, is_disjoint_js_1.default)(parsedProt, jwe.header, jwe.unprotected)) {
  58. throw new errors_js_1.JWEInvalid('JWE Protected, JWE Unprotected Header, and JWE Per-Recipient Unprotected Header Parameter names must be disjoint');
  59. }
  60. const joseHeader = {
  61. ...parsedProt,
  62. ...jwe.header,
  63. ...jwe.unprotected,
  64. };
  65. (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
  66. if (joseHeader.zip !== undefined) {
  67. if (!parsedProt || !parsedProt.zip) {
  68. throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
  69. }
  70. if (joseHeader.zip !== 'DEF') {
  71. throw new errors_js_1.JOSENotSupported('Unsupported JWE "zip" (Compression Algorithm) Header Parameter value');
  72. }
  73. }
  74. const { alg, enc } = joseHeader;
  75. if (typeof alg !== 'string' || !alg) {
  76. throw new errors_js_1.JWEInvalid('missing JWE Algorithm (alg) in JWE Header');
  77. }
  78. if (typeof enc !== 'string' || !enc) {
  79. throw new errors_js_1.JWEInvalid('missing JWE Encryption Algorithm (enc) in JWE Header');
  80. }
  81. const keyManagementAlgorithms = options && (0, validate_algorithms_js_1.default)('keyManagementAlgorithms', options.keyManagementAlgorithms);
  82. const contentEncryptionAlgorithms = options &&
  83. (0, validate_algorithms_js_1.default)('contentEncryptionAlgorithms', options.contentEncryptionAlgorithms);
  84. if (keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) {
  85. throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
  86. }
  87. if (contentEncryptionAlgorithms && !contentEncryptionAlgorithms.has(enc)) {
  88. throw new errors_js_1.JOSEAlgNotAllowed('"enc" (Encryption Algorithm) Header Parameter not allowed');
  89. }
  90. let encryptedKey;
  91. if (jwe.encrypted_key !== undefined) {
  92. try {
  93. encryptedKey = (0, base64url_js_1.decode)(jwe.encrypted_key);
  94. }
  95. catch {
  96. throw new errors_js_1.JWEInvalid('Failed to base64url decode the encrypted_key');
  97. }
  98. }
  99. let resolvedKey = false;
  100. if (typeof key === 'function') {
  101. key = await key(parsedProt, jwe);
  102. resolvedKey = true;
  103. }
  104. let cek;
  105. try {
  106. cek = await (0, decrypt_key_management_js_1.default)(alg, key, encryptedKey, joseHeader, options);
  107. }
  108. catch (err) {
  109. if (err instanceof TypeError || err instanceof errors_js_1.JWEInvalid || err instanceof errors_js_1.JOSENotSupported) {
  110. throw err;
  111. }
  112. cek = (0, cek_js_1.default)(enc);
  113. }
  114. let iv;
  115. let tag;
  116. try {
  117. iv = (0, base64url_js_1.decode)(jwe.iv);
  118. }
  119. catch {
  120. throw new errors_js_1.JWEInvalid('Failed to base64url decode the iv');
  121. }
  122. try {
  123. tag = (0, base64url_js_1.decode)(jwe.tag);
  124. }
  125. catch {
  126. throw new errors_js_1.JWEInvalid('Failed to base64url decode the tag');
  127. }
  128. const protectedHeader = buffer_utils_js_1.encoder.encode((_a = jwe.protected) !== null && _a !== void 0 ? _a : '');
  129. let additionalData;
  130. if (jwe.aad !== undefined) {
  131. additionalData = (0, buffer_utils_js_1.concat)(protectedHeader, buffer_utils_js_1.encoder.encode('.'), buffer_utils_js_1.encoder.encode(jwe.aad));
  132. }
  133. else {
  134. additionalData = protectedHeader;
  135. }
  136. let ciphertext;
  137. try {
  138. ciphertext = (0, base64url_js_1.decode)(jwe.ciphertext);
  139. }
  140. catch {
  141. throw new errors_js_1.JWEInvalid('Failed to base64url decode the ciphertext');
  142. }
  143. let plaintext = await (0, decrypt_js_1.default)(enc, cek, ciphertext, iv, tag, additionalData);
  144. if (joseHeader.zip === 'DEF') {
  145. plaintext = await ((options === null || options === void 0 ? void 0 : options.inflateRaw) || zlib_js_1.inflate)(plaintext);
  146. }
  147. const result = { plaintext };
  148. if (jwe.protected !== undefined) {
  149. result.protectedHeader = parsedProt;
  150. }
  151. if (jwe.aad !== undefined) {
  152. try {
  153. result.additionalAuthenticatedData = (0, base64url_js_1.decode)(jwe.aad);
  154. }
  155. catch {
  156. throw new errors_js_1.JWEInvalid('Failed to base64url decode the aad');
  157. }
  158. }
  159. if (jwe.unprotected !== undefined) {
  160. result.sharedUnprotectedHeader = jwe.unprotected;
  161. }
  162. if (jwe.header !== undefined) {
  163. result.unprotectedHeader = jwe.header;
  164. }
  165. if (resolvedKey) {
  166. return { ...result, key };
  167. }
  168. return result;
  169. }
  170. exports.flattenedDecrypt = flattenedDecrypt;