jose/test/jwe/general.decrypt.test.mjs
2021-08-20 12:01:17 +02:00

96 lines
3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'ava';
const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto';
Promise.all([
import(`${root}/jwe/flattened/encrypt`),
import(`${root}/jwe/general/decrypt`),
import(`${root}/util/random`),
]).then(
([{ default: FlattenedEncrypt }, { default: generalDecrypt }, { default: random }]) => {
test.before(async (t) => {
const encode = TextEncoder.prototype.encode.bind(new TextEncoder());
t.context.plaintext = encode('Its a dangerous business, Frodo, going out your door.');
t.context.additionalAuthenticatedData = encode('The Fellowship of the Ring');
t.context.initializationVector = random(new Uint8Array(12));
t.context.secret = random(new Uint8Array(16));
});
test('JWS format validation', async (t) => {
const flattenedJwe = await new FlattenedEncrypt(t.context.plaintext)
.setProtectedHeader({ bar: 'baz' })
.setUnprotectedHeader({ foo: 'bar' })
.setSharedUnprotectedHeader({ alg: 'A128GCMKW', enc: 'A128GCM' })
.setAdditionalAuthenticatedData(t.context.additionalAuthenticatedData)
.encrypt(t.context.secret);
const generalJwe = {
aad: flattenedJwe.aad,
ciphertext: flattenedJwe.ciphertext,
iv: flattenedJwe.iv,
protected: flattenedJwe.protected,
tag: flattenedJwe.tag,
unprotected: flattenedJwe.unprotected,
recipients: [
{
encrypted_key: flattenedJwe.encrypted_key,
header: flattenedJwe.header,
},
],
};
{
await t.throwsAsync(generalDecrypt(null, t.context.secret), {
message: 'General JWE must be an object',
code: 'ERR_JWE_INVALID',
});
}
{
await t.throwsAsync(generalDecrypt({ recipients: null }, t.context.secret), {
message: 'JWE Recipients missing or incorrect type',
code: 'ERR_JWE_INVALID',
});
}
{
await t.throwsAsync(generalDecrypt({ recipients: [null] }, t.context.secret), {
message: 'JWE Recipients missing or incorrect type',
code: 'ERR_JWE_INVALID',
});
}
{
const jwe = { ...generalJwe, recipients: [] };
await t.throwsAsync(generalDecrypt(jwe, t.context.secret), {
message: 'decryption operation failed',
code: 'ERR_JWE_DECRYPTION_FAILED',
});
}
{
const jwe = { ...generalJwe, recipients: [generalJwe.recipients[0]] };
await t.notThrowsAsync(generalDecrypt(jwe, t.context.secret));
}
{
const jwe = { ...generalJwe, recipients: [generalJwe.recipients[0], {}] };
await t.notThrowsAsync(generalDecrypt(jwe, t.context.secret));
}
{
const jwe = { ...generalJwe, recipients: [{}, generalJwe.recipients[0]] };
await t.notThrowsAsync(generalDecrypt(jwe, t.context.secret));
}
});
},
(err) => {
test('failed to import', (t) => {
console.error(err);
t.fail();
});
},
);