@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
82 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ES512 = exports.ES384 = exports.ES256 = void 0;
const crypto_1 = require("crypto");
const util_1 = require("util");
const invalid_jsonwebkey_exception_1 = require("../../exceptions/invalid-jsonwebkey.exception");
const invalid_jsonwebsignature_exception_1 = require("../../exceptions/invalid-jsonwebsignature.exception");
const jsonwebsignature_algorithm_1 = require("./jsonwebsignature.algorithm");
const signAsync = (0, util_1.promisify)(crypto_1.sign);
const verifyAsync = (0, util_1.promisify)(crypto_1.verify);
/**
* Implementation of the JSON Web Signature ECDSA Algorithm.
*/
class EcdsaAlgorithm extends jsonwebsignature_algorithm_1.JsonWebSignatureAlgorithm {
/**
* Instantiates a new JSON Web Signature ECDSA Algorithm to Sign and Verify Messages.
*
* @param algorithm Name of the JSON Web Signature Algorithm.
* @param hash Hash Algorithm used to Sign and Verify Messages.
* @param curve Elliptic Curve used by the JSON Web Signature ECDSA Algorithm.
*/
constructor(algorithm, hash, curve) {
super(algorithm, hash, 'EC');
this.curve = curve;
}
/**
* Signs a Message with the provided JSON Web Key.
*
* @param message Message to be Signed.
* @param key JSON Web Key used to Sign the provided Message.
* @returns Resulting Signature of the provided Message.
*/
async sign(message, key) {
this.validateJsonWebKey(key);
const cryptoKey = key['cryptoKey'];
if (cryptoKey.type !== 'private') {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('A Private Key is needed to Sign a JSON Web Signature Message.');
}
const signature = await signAsync(this.hash, message, cryptoKey);
return signature;
}
/**
* Checks if the provided Signature matches the provided Message based on the provide JSON Web Key.
*
* @param signature Signature to be matched against the provided Message.
* @param message Message to be matched against the provided Signature.
* @param key JSON Web Key used to verify the Signature and Message.
*/
async verify(signature, message, key) {
this.validateJsonWebKey(key);
const cryptoKey = key['cryptoKey'];
const result = await verifyAsync(this.hash, message, cryptoKey, signature);
if (!result) {
throw new invalid_jsonwebsignature_exception_1.InvalidJsonWebSignatureException();
}
}
/**
* Checks if the provided JSON Web Key can be used by the JSON Web Signature ECDSA Algorithm.
*
* @param key JSON Web Key to be checked.
* @throws {InvalidJsonWebKeyException} The provided JSON Web Key is invalid.
*/
validateJsonWebKey(key) {
super.validateJsonWebKey(key);
if (key.crv !== this.curve) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException(`The JSON Web Signature ECDSA Algorithm "${this.algorithm}" only accepts the Elliptic Curve "${this.curve}".`);
}
}
}
/**
* ECDSA using P-256 and SHA-256.
*/
exports.ES256 = new EcdsaAlgorithm('ES256', 'SHA256', 'P-256');
/**
* ECDSA using P-384 and SHA-384.
*/
exports.ES384 = new EcdsaAlgorithm('ES384', 'SHA384', 'P-384');
/**
* ECDSA using P-521 and SHA-512.
*/
exports.ES512 = new EcdsaAlgorithm('ES512', 'SHA512', 'P-521');
//# sourceMappingURL=ecdsa.algorithm.js.map