@guarani/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
82 lines (81 loc) • 3.52 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_json_web_key_exception_1 = require("../../exceptions/invalid-json-web-key.exception");
const invalid_json_web_signature_exception_1 = require("../../exceptions/invalid-json-web-signature.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 the Messages.
*
* @param hash Hash Algorithm used to Sign and Verify the Messages.
* @param algorithm Name of the JSON Web Signature Algorithm.
* @param curve Elliptic Curve used by the JSON Web Signature ECDSA Algorithm.
*/
constructor(hash, algorithm, curve) {
super(hash, algorithm, '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 = Reflect.get(key, 'cryptoKey');
if (cryptoKey.type !== 'private') {
throw new invalid_json_web_key_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 = Reflect.get(key, 'cryptoKey');
const verificationResult = await verifyAsync(this.hash, message, cryptoKey, signature);
if (!verificationResult) {
throw new invalid_json_web_signature_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_json_web_key_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('SHA256', 'ES256', 'P-256');
/**
* ECDSA using P-384 and SHA-384.
*/
exports.ES384 = new EcdsaAlgorithm('SHA384', 'ES384', 'P-384');
/**
* ECDSA using P-521 and SHA-512.
*/
exports.ES512 = new EcdsaAlgorithm('SHA512', 'ES512', 'P-521');