jose/test/cookbook/5_10.including_additional_authentication_data.test.js
Filip Skokan 5b53cb0155 fix: limit calculation of missing RSA private components
- this deprecates the use of `JWK.importKey` in favor of
`JWK.asKey`
- this deprecates the use of `JWKS.KeyStore.fromJWKS` in favor of
`JWKS.asKeyStore`

Both `JWK.importKey` and `JWKS.KeyStore.fromJWKS` could have resulted
in the process getting blocked when large bitsize RSA private keys
were missing their components and could also result in an endless
calculation loop when the private key's private exponent was outright
invalid or tampered with.

The new methods still allow to import private RSA keys with these
optimization key parameters missing but its disabled by default and one
should choose to enable it when working with keys from trusted sources

It is recommended not to use @panva/jose versions with this feature in
its original on-by-default form - v1.1.0 and v1.2.0 These will
2019-06-20 23:32:13 +02:00

72 lines
2.9 KiB
JavaScript

const test = require('ava')
const recipe = require('./recipes').get('5.10')
const { enc: verifiers } = require('./verifiers')
const { JWE, JWK: { asKey, generateSync }, JWKS: { KeyStore }, errors } = require('../..')
const {
input: { plaintext, key: jwk, aad },
encrypting_content: { protected: prot }
} = recipe
const key = asKey(jwk)
const keystoreEmpty = new KeyStore()
const keystoreMatchOne = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use }), key)
const keystoreMatchMore = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use, kid: key.kid }), key, asKey(key))
const keystoreMatchNone = new KeyStore(generateSync(key.kty), generateSync(key.kty))
test(`${recipe.title} - flattened encrypt`, t => {
const res = JWE.encrypt.flattened(plaintext, key, prot, undefined, aad)
verifiers.flattened(t, res, recipe.output.json_flat)
t.deepEqual(JWE.decrypt(res, key), Buffer.from(plaintext))
})
test(`${recipe.title} - general encrypt`, t => {
const res = JWE.encrypt.general(plaintext, key, prot, undefined, aad)
verifiers.general(t, res, recipe.output.json)
t.deepEqual(JWE.decrypt(res, key), Buffer.from(plaintext))
})
test(`${recipe.title} - flattened decrypt`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json_flat, key), Buffer.from(plaintext))
})
test(`${recipe.title} - general decrypt`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json, key), Buffer.from(plaintext))
})
;[keystoreMatchOne, keystoreMatchMore].forEach((keystore, i) => {
test(`${recipe.title} - flattened decrypt (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json_flat, keystore), Buffer.from(plaintext))
})
test(`${recipe.title} - general decrypt (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json, keystore), Buffer.from(plaintext))
})
})
test(`${recipe.title} - flattened verify (failing)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json_flat, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - general verify (failing)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - flattened verify (using empty keystore)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json_flat, keystoreEmpty)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - general verify (using empty keystore)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json, keystoreEmpty)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})