UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

64 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EcKeyAlgorithm = void 0; const crypto_1 = require("crypto"); const invalid_jsonwebkey_exception_1 = require("../../exceptions/invalid-jsonwebkey.exception"); const unsupported_elliptic_curve_exception_1 = require("../../exceptions/unsupported-elliptic-curve.exception"); /** * Elliptic Curves Registry. */ const ELLIPTIC_CURVES_REGISTRY = [ { id: 'P-256', name: 'prime256v1', oid: '1.2.840.10045.3.1.7', length: 32 }, { id: 'P-384', name: 'secp384r1', oid: '1.3.132.0.34', length: 48 }, { id: 'P-521', name: 'secp521r1', oid: '1.3.132.0.35', length: 66 }, ]; /** * Implementation of the **Elliptic Curve** JSON Web Key Algorithm. * * @see https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 */ class EcKeyAlgorithm { constructor() { /** * Private Parameters of the JSON Web Key Elliptic Curve Algorithm. */ this.privateParameters = ['d']; } /** * Validates the provided JSON Web Key Parameters. * * @param parameters Parameters of the JSON Web Key. */ validateParameters(parameters) { if (typeof parameters.crv !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid parameter "crv".'); } const curve = ELLIPTIC_CURVES_REGISTRY.find((ellipticCurve) => ellipticCurve.id === parameters.crv); if (curve === undefined) { throw new unsupported_elliptic_curve_exception_1.UnsupportedEllipticCurveException(`Unsupported Elliptic Curve "${parameters.crv}".`); } if (typeof parameters.x !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "x".'); } if (typeof parameters.y !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "y".'); } if (parameters.d !== undefined) { if (typeof parameters.d !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "d".'); } } } /** * Loads the provided JSON Web Key into a NodeJS Crypto Key. * * @param parameters Parameters of the JSON Web Key. * @returns NodeJS Crypto Key. */ loadCryptoKey(parameters) { const input = { format: 'jwk', key: parameters }; return parameters.d === undefined ? (0, crypto_1.createPublicKey)(input) : (0, crypto_1.createPrivateKey)(input); } } exports.EcKeyAlgorithm = EcKeyAlgorithm; //# sourceMappingURL=eckey.algorithm.js.map