encrypt.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { encode as base64url } from '../../runtime/base64url.js';
  2. import encrypt from '../../runtime/encrypt.js';
  3. import { deflate } from '../../runtime/zlib.js';
  4. import generateIv from '../../lib/iv.js';
  5. import encryptKeyManagement from '../../lib/encrypt_key_management.js';
  6. import { JOSENotSupported, JWEInvalid } from '../../util/errors.js';
  7. import isDisjoint from '../../lib/is_disjoint.js';
  8. import { encoder, decoder, concat } from '../../lib/buffer_utils.js';
  9. import validateCrit from '../../lib/validate_crit.js';
  10. export const unprotected = Symbol();
  11. export class FlattenedEncrypt {
  12. constructor(plaintext) {
  13. if (!(plaintext instanceof Uint8Array)) {
  14. throw new TypeError('plaintext must be an instance of Uint8Array');
  15. }
  16. this._plaintext = plaintext;
  17. }
  18. setKeyManagementParameters(parameters) {
  19. if (this._keyManagementParameters) {
  20. throw new TypeError('setKeyManagementParameters can only be called once');
  21. }
  22. this._keyManagementParameters = parameters;
  23. return this;
  24. }
  25. setProtectedHeader(protectedHeader) {
  26. if (this._protectedHeader) {
  27. throw new TypeError('setProtectedHeader can only be called once');
  28. }
  29. this._protectedHeader = protectedHeader;
  30. return this;
  31. }
  32. setSharedUnprotectedHeader(sharedUnprotectedHeader) {
  33. if (this._sharedUnprotectedHeader) {
  34. throw new TypeError('setSharedUnprotectedHeader can only be called once');
  35. }
  36. this._sharedUnprotectedHeader = sharedUnprotectedHeader;
  37. return this;
  38. }
  39. setUnprotectedHeader(unprotectedHeader) {
  40. if (this._unprotectedHeader) {
  41. throw new TypeError('setUnprotectedHeader can only be called once');
  42. }
  43. this._unprotectedHeader = unprotectedHeader;
  44. return this;
  45. }
  46. setAdditionalAuthenticatedData(aad) {
  47. this._aad = aad;
  48. return this;
  49. }
  50. setContentEncryptionKey(cek) {
  51. if (this._cek) {
  52. throw new TypeError('setContentEncryptionKey can only be called once');
  53. }
  54. this._cek = cek;
  55. return this;
  56. }
  57. setInitializationVector(iv) {
  58. if (this._iv) {
  59. throw new TypeError('setInitializationVector can only be called once');
  60. }
  61. this._iv = iv;
  62. return this;
  63. }
  64. async encrypt(key, options) {
  65. if (!this._protectedHeader && !this._unprotectedHeader && !this._sharedUnprotectedHeader) {
  66. throw new JWEInvalid('either setProtectedHeader, setUnprotectedHeader, or sharedUnprotectedHeader must be called before #encrypt()');
  67. }
  68. if (!isDisjoint(this._protectedHeader, this._unprotectedHeader, this._sharedUnprotectedHeader)) {
  69. throw new JWEInvalid('JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
  70. }
  71. const joseHeader = {
  72. ...this._protectedHeader,
  73. ...this._unprotectedHeader,
  74. ...this._sharedUnprotectedHeader,
  75. };
  76. validateCrit(JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
  77. if (joseHeader.zip !== undefined) {
  78. if (!this._protectedHeader || !this._protectedHeader.zip) {
  79. throw new JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
  80. }
  81. if (joseHeader.zip !== 'DEF') {
  82. throw new JOSENotSupported('Unsupported JWE "zip" (Compression Algorithm) Header Parameter value');
  83. }
  84. }
  85. const { alg, enc } = joseHeader;
  86. if (typeof alg !== 'string' || !alg) {
  87. throw new JWEInvalid('JWE "alg" (Algorithm) Header Parameter missing or invalid');
  88. }
  89. if (typeof enc !== 'string' || !enc) {
  90. throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter missing or invalid');
  91. }
  92. let encryptedKey;
  93. if (alg === 'dir') {
  94. if (this._cek) {
  95. throw new TypeError('setContentEncryptionKey cannot be called when using Direct Encryption');
  96. }
  97. }
  98. else if (alg === 'ECDH-ES') {
  99. if (this._cek) {
  100. throw new TypeError('setContentEncryptionKey cannot be called when using Direct Key Agreement');
  101. }
  102. }
  103. let cek;
  104. {
  105. let parameters;
  106. ({ cek, encryptedKey, parameters } = await encryptKeyManagement(alg, enc, key, this._cek, this._keyManagementParameters));
  107. if (parameters) {
  108. if (options && unprotected in options) {
  109. if (!this._unprotectedHeader) {
  110. this.setUnprotectedHeader(parameters);
  111. }
  112. else {
  113. this._unprotectedHeader = { ...this._unprotectedHeader, ...parameters };
  114. }
  115. }
  116. else {
  117. if (!this._protectedHeader) {
  118. this.setProtectedHeader(parameters);
  119. }
  120. else {
  121. this._protectedHeader = { ...this._protectedHeader, ...parameters };
  122. }
  123. }
  124. }
  125. }
  126. this._iv || (this._iv = generateIv(enc));
  127. let additionalData;
  128. let protectedHeader;
  129. let aadMember;
  130. if (this._protectedHeader) {
  131. protectedHeader = encoder.encode(base64url(JSON.stringify(this._protectedHeader)));
  132. }
  133. else {
  134. protectedHeader = encoder.encode('');
  135. }
  136. if (this._aad) {
  137. aadMember = base64url(this._aad);
  138. additionalData = concat(protectedHeader, encoder.encode('.'), encoder.encode(aadMember));
  139. }
  140. else {
  141. additionalData = protectedHeader;
  142. }
  143. let ciphertext;
  144. let tag;
  145. if (joseHeader.zip === 'DEF') {
  146. const deflated = await ((options === null || options === void 0 ? void 0 : options.deflateRaw) || deflate)(this._plaintext);
  147. ({ ciphertext, tag } = await encrypt(enc, deflated, cek, this._iv, additionalData));
  148. }
  149. else {
  150. ;
  151. ({ ciphertext, tag } = await encrypt(enc, this._plaintext, cek, this._iv, additionalData));
  152. }
  153. const jwe = {
  154. ciphertext: base64url(ciphertext),
  155. iv: base64url(this._iv),
  156. tag: base64url(tag),
  157. };
  158. if (encryptedKey) {
  159. jwe.encrypted_key = base64url(encryptedKey);
  160. }
  161. if (aadMember) {
  162. jwe.aad = aadMember;
  163. }
  164. if (this._protectedHeader) {
  165. jwe.protected = decoder.decode(protectedHeader);
  166. }
  167. if (this._sharedUnprotectedHeader) {
  168. jwe.unprotected = this._sharedUnprotectedHeader;
  169. }
  170. if (this._unprotectedHeader) {
  171. jwe.header = this._unprotectedHeader;
  172. }
  173. return jwe;
  174. }
  175. }