verify.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { decode as b64u } from '../../util/base64url.js';
  2. import { verify } from '../../lib/signing.js';
  3. import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
  4. import { concat, encoder, decoder, encode } from '../../lib/buffer_utils.js';
  5. import { decodeBase64url } from '../../lib/helpers.js';
  6. import { isDisjoint } from '../../lib/type_checks.js';
  7. import { isObject } from '../../lib/type_checks.js';
  8. import { checkKeyType } from '../../lib/check_key_type.js';
  9. import { validateCrit } from '../../lib/validate_crit.js';
  10. import { validateAlgorithms } from '../../lib/validate_algorithms.js';
  11. import { normalizeKey } from '../../lib/normalize_key.js';
  12. export async function flattenedVerify(jws, key, options) {
  13. if (!isObject(jws)) {
  14. throw new JWSInvalid('Flattened JWS must be an object');
  15. }
  16. if (jws.protected === undefined && jws.header === undefined) {
  17. throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
  18. }
  19. if (jws.protected !== undefined && typeof jws.protected !== 'string') {
  20. throw new JWSInvalid('JWS Protected Header incorrect type');
  21. }
  22. if (jws.payload === undefined) {
  23. throw new JWSInvalid('JWS Payload missing');
  24. }
  25. if (typeof jws.signature !== 'string') {
  26. throw new JWSInvalid('JWS Signature missing or incorrect type');
  27. }
  28. if (jws.header !== undefined && !isObject(jws.header)) {
  29. throw new JWSInvalid('JWS Unprotected Header incorrect type');
  30. }
  31. let parsedProt = {};
  32. if (jws.protected) {
  33. try {
  34. const protectedHeader = b64u(jws.protected);
  35. parsedProt = JSON.parse(decoder.decode(protectedHeader));
  36. }
  37. catch {
  38. throw new JWSInvalid('JWS Protected Header is invalid');
  39. }
  40. }
  41. if (!isDisjoint(parsedProt, jws.header)) {
  42. throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
  43. }
  44. const joseHeader = {
  45. ...parsedProt,
  46. ...jws.header,
  47. };
  48. const extensions = validateCrit(JWSInvalid, new Map([['b64', true]]), options?.crit, parsedProt, joseHeader);
  49. let b64 = true;
  50. if (extensions.has('b64')) {
  51. b64 = parsedProt.b64;
  52. if (typeof b64 !== 'boolean') {
  53. throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
  54. }
  55. }
  56. const { alg } = joseHeader;
  57. if (typeof alg !== 'string' || !alg) {
  58. throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
  59. }
  60. const algorithms = options && validateAlgorithms('algorithms', options.algorithms);
  61. if (algorithms && !algorithms.has(alg)) {
  62. throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
  63. }
  64. if (b64) {
  65. if (typeof jws.payload !== 'string') {
  66. throw new JWSInvalid('JWS Payload must be a string');
  67. }
  68. }
  69. else if (typeof jws.payload !== 'string' && !(jws.payload instanceof Uint8Array)) {
  70. throw new JWSInvalid('JWS Payload must be a string or an Uint8Array instance');
  71. }
  72. let resolvedKey = false;
  73. if (typeof key === 'function') {
  74. key = await key(parsedProt, jws);
  75. resolvedKey = true;
  76. }
  77. checkKeyType(alg, key, 'verify');
  78. const data = concat(jws.protected !== undefined ? encode(jws.protected) : new Uint8Array(), encode('.'), typeof jws.payload === 'string'
  79. ? b64
  80. ? encode(jws.payload)
  81. : encoder.encode(jws.payload)
  82. : jws.payload);
  83. const signature = decodeBase64url(jws.signature, 'signature', JWSInvalid);
  84. const k = await normalizeKey(key, alg);
  85. const verified = await verify(alg, k, signature, data);
  86. if (!verified) {
  87. throw new JWSSignatureVerificationFailed();
  88. }
  89. let payload;
  90. if (b64) {
  91. payload = decodeBase64url(jws.payload, 'payload', JWSInvalid);
  92. }
  93. else if (typeof jws.payload === 'string') {
  94. payload = encoder.encode(jws.payload);
  95. }
  96. else {
  97. payload = jws.payload;
  98. }
  99. const result = { payload };
  100. if (jws.protected !== undefined) {
  101. result.protectedHeader = parsedProt;
  102. }
  103. if (jws.header !== undefined) {
  104. result.unprotectedHeader = jws.header;
  105. }
  106. if (resolvedKey) {
  107. return { ...result, key: k };
  108. }
  109. return result;
  110. }