|
1 | 1 | import test from 'ava' |
2 | 2 | import * as crypto from 'crypto' |
3 | 3 |
|
4 | | -import { FlattenedEncrypt, flattenedDecrypt } from '../../src/index.js' |
| 4 | +import { FlattenedEncrypt, base64url, flattenedDecrypt } from '../../src/index.js' |
5 | 5 |
|
6 | 6 | test.before(async (t) => { |
7 | 7 | const encode = TextEncoder.prototype.encode.bind(new TextEncoder()) |
@@ -251,6 +251,27 @@ test('decrypt PBES2 p2c limit', async (t) => { |
251 | 251 | ) |
252 | 252 | }) |
253 | 253 |
|
| 254 | +test('PBES2 p2c must be a positive integer on decrypt', async (t) => { |
| 255 | + const jwe = await new FlattenedEncrypt(new Uint8Array(0)) |
| 256 | + .setProtectedHeader({ alg: 'PBES2-HS256+A128KW', enc: 'A128CBC-HS256' }) |
| 257 | + .setKeyManagementParameters({ p2c: 1 }) |
| 258 | + .encrypt(new Uint8Array(32)) |
| 259 | + |
| 260 | + const protectedHeader = JSON.parse(new TextDecoder().decode(base64url.decode(jwe.protected!))) |
| 261 | + protectedHeader.p2c = 1.5 |
| 262 | + jwe.protected = base64url.encode(JSON.stringify(protectedHeader)) |
| 263 | + |
| 264 | + await t.throwsAsync( |
| 265 | + flattenedDecrypt(jwe, new Uint8Array(32), { |
| 266 | + keyManagementAlgorithms: ['PBES2-HS256+A128KW'], |
| 267 | + }), |
| 268 | + { |
| 269 | + code: 'ERR_JWE_INVALID', |
| 270 | + message: 'PBES2 Count Input must be a positive integer', |
| 271 | + }, |
| 272 | + ) |
| 273 | +}) |
| 274 | + |
254 | 275 | test('decrypt with PBES2 is not allowed by default', async (t) => { |
255 | 276 | const jwe = await new FlattenedEncrypt(new Uint8Array(0)) |
256 | 277 | .setProtectedHeader({ alg: 'PBES2-HS256+A128KW', enc: 'A128CBC-HS256' }) |
|
0 commit comments