UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

67 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RsaKeyAlgorithm = void 0; const crypto_1 = require("crypto"); const invalid_jsonwebkey_exception_1 = require("../../exceptions/invalid-jsonwebkey.exception"); /** * Implementation of the **RSA** JSON Web Key Algorithm. * * @see https://www.rfc-editor.org/rfc/rfc7518.html#section-6.3 */ class RsaKeyAlgorithm { constructor() { /** * Private Parameters of the JSON Web Key RSA Algorithm. */ this.privateParameters = ['d', 'p', 'q', 'dp', 'dq', 'qi']; } /** * Validates the provided JSON Web Key Parameters. * * @param parameters Parameters of the JSON Web Key. */ validateParameters(parameters) { if (typeof parameters.n !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "n".'); } if (Buffer.byteLength(parameters.n, 'base64url') < 256) { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('The modulus MUST have AT LEAST 2048 bits.'); } if (typeof parameters.e !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "e".'); } // TODO: Validate the following values based on the previous ones. if (this.privateParameters.some((parameter) => parameters[parameter] !== undefined)) { if (typeof parameters.d !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "d".'); } if (typeof parameters.p !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "p".'); } if (typeof parameters.q !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "q".'); } if (typeof parameters.dp !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "dp".'); } if (typeof parameters.dq !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "dq".'); } if (typeof parameters.qi !== 'string') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid key parameter "qi".'); } } } /** * 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.RsaKeyAlgorithm = RsaKeyAlgorithm; //# sourceMappingURL=rsakey.algorithm.js.map