jose/lib/jwa/index.js
2019-01-26 14:24:12 +01:00

85 lines
2.3 KiB
JavaScript

const { TODO } = require('../errors')
const JWA = {
sign: new Map(),
verify: new Map(),
wrapKey: new Map(),
unwrapKey: new Map(),
encrypt: new Map(),
decrypt: new Map()
}
// sign, verify
require('./hmac')(JWA)
require('./ecdsa')(JWA)
require('./rsassa_pss')(JWA)
require('./rsassa_pkcs1')(JWA)
// wrapKey, unwrapKey
require('./rsaes')(JWA)
// encrypt, decrypt
require('./aes_cbc_hmac_sha2')(JWA)
module.exports = {
sign: (alg, key, payload) => {
if (!JWA.sign.has(alg)) {
throw new TODO(`sign alg ${alg} not implemented`)
}
if (!key.algorithms('sign').has(alg)) {
throw new TODO(`the key does not support ${alg} sign algorithm`)
}
return JWA.sign.get(alg)(key, payload)
},
verify: (alg, key, payload, signature) => {
if (!JWA.verify.has(alg)) {
throw new TODO(`verify alg ${alg} not implemented`)
}
if (!key.algorithms('verify').has(alg)) {
throw new TODO(`the key does not support ${alg} verify algorithm`)
}
return JWA.verify.get(alg)(key, payload, signature)
},
wrapKey: (alg, key, payload) => {
if (!JWA.wrapKey.has(alg)) {
throw new TODO(`wrapKey alg ${alg} not implemented`)
}
if (!key.algorithms('wrapKey').has(alg)) {
throw new TODO(`the key does not support ${alg} wrapKey algorithm`)
}
return JWA.wrapKey.get(alg)(key, payload)
},
unwrapKey: (alg, key, payload) => {
if (!JWA.unwrapKey.has(alg)) {
throw new TODO(`unwrapKey alg ${alg} not implemented`)
}
if (!key.algorithms('unwrapKey').has(alg)) {
throw new TODO(`the key does not support ${alg} unwrapKey algorithm`)
}
return JWA.unwrapKey.get(alg)(key, payload)
},
encrypt: (alg, key, cleartext, opts) => {
if (!JWA.encrypt.has(alg)) {
throw new TODO(`encrypt alg ${alg} not implemented`)
}
if (!key.algorithms('encrypt').has(alg)) {
throw new TODO(`the key does not support ${alg} encrypt algorithm`)
}
return JWA.encrypt.get(alg)(key, cleartext, opts)
},
decrypt: (alg, key, ciphertext, opts) => {
if (!JWA.decrypt.has(alg)) {
throw new TODO(`decrypt alg ${alg} not implemented`)
}
if (!key.algorithms('decrypt').has(alg)) {
throw new TODO(`the key does not support ${alg} decrypt algorithm`)
}
return JWA.decrypt.get(alg)(key, ciphertext, opts)
}
}