UNPKG

parse-cosekey

Version:

Parse COSE(CBOR Object Signing and Encryption) to JWK(JSON Web Key) or PEM.

392 lines 20.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JSONWebKeyOperation = exports.JSONWebKeyUse = exports.JSONWebKeyParameter = exports.JSONWebKeyEllipticCurve = exports.JSONWebSignatureAndEncryptionAlgorithm = exports.JSONWebKeyType = void 0; /** * JSON Web Key (JWK) Types class. * "kty" (Key Type) parameter. * * RFC: {@link https://datatracker.ietf.org/doc/html/rfc7517#section-4.1} * * Registry: {@link https://www.iana.org/assignments/jose/jose.xhtml#web-key-types} */ class JSONWebKeyType { /** * Private constructor. * * @param _value key type.key type. * @param _description Key type description */ constructor(_value, _description) { this._value = _value; this._description = _description; JSONWebKeyType._values.push(this); } /** * Returns key type. * * @returns Key type */ get value() { return this._value; } /** * Returns key type description. * * @returns Key type description */ get description() { return this._description; } /** * Returns all JWK key type class. * * @returns All JWK key type class */ static values() { return JSONWebKeyType._values; } /** * Returns JWK key type class which has same key type to function's parameter. * * @param value Target key type * @returns JWK key type class, if not found returns null */ static fromValue(value) { const found = JSONWebKeyType.values().find((j) => { return j.value === value; }); return found || null; } } exports.JSONWebKeyType = JSONWebKeyType; JSONWebKeyType._values = []; JSONWebKeyType.EC = new JSONWebKeyType('EC', 'Elliptic Curve'); JSONWebKeyType.RSA = new JSONWebKeyType('RSA', 'RSA'); JSONWebKeyType.OCT = new JSONWebKeyType('oct', 'Octet sequence'); JSONWebKeyType.OKP = new JSONWebKeyType('OKP', 'Octet string key pairs'); /** * JSON Web Signature and Encryption Algorithms class. * "alg" (Algorithm) parameter. * * RFC: {@link https://datatracker.ietf.org/doc/html/rfc7517#section-4.4} * * Registry: {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms} */ class JSONWebSignatureAndEncryptionAlgorithm { /** * Private constructor. * * @param _name Algorithm name * @param _description Algorithm description * @param _nodeCryptoHashAlg Node.js crypto module's algorithm name */ constructor(_name, _description, _nodeCryptoHashAlg) { this._name = _name; this._description = _description; this._nodeCryptoHashAlg = _nodeCryptoHashAlg; JSONWebSignatureAndEncryptionAlgorithm._values.push(this); } /** * Returns algorithm name. * * @returns Algorithm name */ get name() { return this._name; } /** * Returns algorithm description. * * @returns Algorithm description */ get description() { return this._description; } /** * Returns Node.js crypto module's algorithm name. * * @returns Node.js crypto module's algorithm name */ get nodeCryptoHashAlg() { return this._nodeCryptoHashAlg; } /** * Returns all JSON Web Signature and Encryption Algorithms class. * * @returns All JSON Web Signature and Encryption Algorithms class */ static values() { return JSONWebSignatureAndEncryptionAlgorithm._values; } /** * Returns JSON Web Signature and Encryption Algorithms class which has same algorithm name to function's parameter. * * @param name Target algorithm name * @returns JSON Web Signature and Encryption Algorithms class, if not found returns null */ static fromName(name) { const found = JSONWebSignatureAndEncryptionAlgorithm.values().find((j) => { return j.name === name; }); return found || null; } } exports.JSONWebSignatureAndEncryptionAlgorithm = JSONWebSignatureAndEncryptionAlgorithm; JSONWebSignatureAndEncryptionAlgorithm._values = []; JSONWebSignatureAndEncryptionAlgorithm.HS256 = new JSONWebSignatureAndEncryptionAlgorithm('HS256', 'HMAC using SHA-256', 'sha256'); JSONWebSignatureAndEncryptionAlgorithm.HS384 = new JSONWebSignatureAndEncryptionAlgorithm('HS384', 'HMAC using SHA-384', 'sha384'); JSONWebSignatureAndEncryptionAlgorithm.HS512 = new JSONWebSignatureAndEncryptionAlgorithm('HS512', 'HMAC using SHA-512', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.RS256 = new JSONWebSignatureAndEncryptionAlgorithm('RS256', 'RSASSA-PKCS1-v1_5 using SHA-256', 'sha256'); JSONWebSignatureAndEncryptionAlgorithm.RS384 = new JSONWebSignatureAndEncryptionAlgorithm('RS384', 'RSASSA-PKCS1-v1_5 using SHA-384', 'sha384'); JSONWebSignatureAndEncryptionAlgorithm.RS512 = new JSONWebSignatureAndEncryptionAlgorithm('RS512', 'RSASSA-PKCS1-v1_5 using SHA-512', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.ES256 = new JSONWebSignatureAndEncryptionAlgorithm('ES256', 'ECDSA using P-256 and SHA-256', 'sha256'); JSONWebSignatureAndEncryptionAlgorithm.ES384 = new JSONWebSignatureAndEncryptionAlgorithm('ES384', 'ECDSA using P-384 and SHA-384', 'sha384'); JSONWebSignatureAndEncryptionAlgorithm.ES512 = new JSONWebSignatureAndEncryptionAlgorithm('ES512', 'ECDSA using P-512 and SHA-512', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.PS256 = new JSONWebSignatureAndEncryptionAlgorithm('PS256', 'RSASSA-PSS using SHA-256 and MGF1 with SHA-256', 'sha256'); JSONWebSignatureAndEncryptionAlgorithm.PS384 = new JSONWebSignatureAndEncryptionAlgorithm('PS384', 'RSASSA-PSS using SHA-384 and MGF1 with SHA-384', 'sha384'); JSONWebSignatureAndEncryptionAlgorithm.PS512 = new JSONWebSignatureAndEncryptionAlgorithm('PS512', 'RSASSA-PSS using SHA-512 and MGF1 with SHA-512', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.NONE = new JSONWebSignatureAndEncryptionAlgorithm('none', 'No digital signature or MAC performed', null); JSONWebSignatureAndEncryptionAlgorithm.RSA1_5 = new JSONWebSignatureAndEncryptionAlgorithm('RSA1_5', 'RSAES-PKCS1-v1_5', null); JSONWebSignatureAndEncryptionAlgorithm.RSA_OAEP = new JSONWebSignatureAndEncryptionAlgorithm('RSA-OAEP', 'RSAES OAEP using default parameters', null); JSONWebSignatureAndEncryptionAlgorithm.RSA_OAEP_256 = new JSONWebSignatureAndEncryptionAlgorithm('RSA-OAEP-256', 'RSAES OAEP using SHA-256 and MGF1 with SHA-256', 'sha256'); JSONWebSignatureAndEncryptionAlgorithm.A128KW = new JSONWebSignatureAndEncryptionAlgorithm('A128KW', 'AES Key Wrap using 128-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A192KW = new JSONWebSignatureAndEncryptionAlgorithm('A192KW', 'AES Key Wrap using 192-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A256KW = new JSONWebSignatureAndEncryptionAlgorithm('A256KW', 'AES Key Wrap using 256-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.DIR = new JSONWebSignatureAndEncryptionAlgorithm('dir', 'Direct use of a shared symmetric key', null); JSONWebSignatureAndEncryptionAlgorithm.ECDH_ES = new JSONWebSignatureAndEncryptionAlgorithm('ECDH-ES', 'ECDH-ES using Concat KDF', null); JSONWebSignatureAndEncryptionAlgorithm.ECDH_ES_A128KW = new JSONWebSignatureAndEncryptionAlgorithm('ECDH-ES+A128KW', 'ECDH-ES using Concat KDF and "A128KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.ECDH_ES_A192KW = new JSONWebSignatureAndEncryptionAlgorithm('ECDH-ES+A192KW', 'ECDH-ES using Concat KDF and "A192KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.ECDH_ES_A256KW = new JSONWebSignatureAndEncryptionAlgorithm('ECDH-ES+A256KW', 'ECDH-ES using Concat KDF and "A256KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.A128GCMKW = new JSONWebSignatureAndEncryptionAlgorithm('A128GCMKW', 'Key wrapping with AES GCM using 128-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A192GCMKW = new JSONWebSignatureAndEncryptionAlgorithm('A192GCMKW', 'Key wrapping with AES GCM using 192-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A256GCMKW = new JSONWebSignatureAndEncryptionAlgorithm('A256GCMKW', 'Key wrapping with AES GCM using 256-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.PBES2_HS256_A128KW = new JSONWebSignatureAndEncryptionAlgorithm('PBES2-HS256+A128KW', 'PBES2 with HMAC SHA-256 and "A128KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.PBES2_HS384_A192KW = new JSONWebSignatureAndEncryptionAlgorithm('PBES2-HS384+A192KW', 'PBES2 with HMAC SHA-384 and "A192KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.PBES2_HS512_A256KW = new JSONWebSignatureAndEncryptionAlgorithm('PBES2-HS512+A256KW', 'PBES2 with HMAC SHA-512 and "A256KW" wrapping', null); JSONWebSignatureAndEncryptionAlgorithm.A128CBC_HS256 = new JSONWebSignatureAndEncryptionAlgorithm('A128CBC-HS256', 'AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm', null); JSONWebSignatureAndEncryptionAlgorithm.A192CBC_HS384 = new JSONWebSignatureAndEncryptionAlgorithm('A192CBC-HS384', 'AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm', null); JSONWebSignatureAndEncryptionAlgorithm.A256CBC_HS512 = new JSONWebSignatureAndEncryptionAlgorithm('A256CBC-HS512', 'AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm', null); JSONWebSignatureAndEncryptionAlgorithm.A128GCM = new JSONWebSignatureAndEncryptionAlgorithm('A128GCM', 'AES GCM using 128-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A192GCM = new JSONWebSignatureAndEncryptionAlgorithm('A192GCM', 'AES GCM using 192-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A256GCM = new JSONWebSignatureAndEncryptionAlgorithm('A256GCM', 'AES GCM using 256-bit key', null); JSONWebSignatureAndEncryptionAlgorithm.EdDSA = new JSONWebSignatureAndEncryptionAlgorithm('EdDSA', 'EdDSA signature algorithms', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.RS1 = new JSONWebSignatureAndEncryptionAlgorithm('RS1', 'RSASSA-PKCS1-v1_5 with SHA-1', 'sha1'); JSONWebSignatureAndEncryptionAlgorithm.RSA_OAEP_384 = new JSONWebSignatureAndEncryptionAlgorithm('RSA-OAEP-384', 'RSA-OAEP using SHA-384 and MGF1 with SHA-384', 'sha384'); JSONWebSignatureAndEncryptionAlgorithm.RSA_OAEP_512 = new JSONWebSignatureAndEncryptionAlgorithm('RSA-OAEP-512', 'RSA-OAEP using SHA-512 and MGF1 with SHA-512', 'sha512'); JSONWebSignatureAndEncryptionAlgorithm.A128CBC = new JSONWebSignatureAndEncryptionAlgorithm('A128CBC', 'AES CBC using 128 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A192CBC = new JSONWebSignatureAndEncryptionAlgorithm('A192CBC', 'AES CBC using 192 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A256CBC = new JSONWebSignatureAndEncryptionAlgorithm('A256CBC', 'AES CBC using 256 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A128CTR = new JSONWebSignatureAndEncryptionAlgorithm('A128CTR', 'AES CTR using 128 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A192CTR = new JSONWebSignatureAndEncryptionAlgorithm('A192CTR', 'AES CTR using 192 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.A256CTR = new JSONWebSignatureAndEncryptionAlgorithm('A256CTR', 'AES CTR using 256 bit key', null); JSONWebSignatureAndEncryptionAlgorithm.HS1 = new JSONWebSignatureAndEncryptionAlgorithm('HS1', 'HMAC using SHA-1', 'sha1'); JSONWebSignatureAndEncryptionAlgorithm.ES256K = new JSONWebSignatureAndEncryptionAlgorithm('ES256K', 'ECDSA using secp256k1 curve and SHA-256', 'sha256'); /** * JSON Web Key Elliptic Curve class. * * RFC: {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2.1.1} * * Registry: {@link https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve} */ class JSONWebKeyEllipticCurve { /** * Private constructor. * * @param _name Curve name * @param _description Curve description */ constructor(_name, _description) { this._name = _name; this._description = _description; JSONWebKeyEllipticCurve._values.push(this); } get name() { return this._name; } get description() { return this._description; } static values() { return JSONWebKeyEllipticCurve._values; } static fromName(name) { const found = JSONWebKeyEllipticCurve.values().find((j) => { return j.name === name; }); return found || null; } } exports.JSONWebKeyEllipticCurve = JSONWebKeyEllipticCurve; JSONWebKeyEllipticCurve._values = []; JSONWebKeyEllipticCurve.P_256 = new JSONWebKeyEllipticCurve('P-256', 'P-256 Curve'); JSONWebKeyEllipticCurve.P_384 = new JSONWebKeyEllipticCurve('P-384', 'P-384 Curve'); JSONWebKeyEllipticCurve.P_521 = new JSONWebKeyEllipticCurve('P-521', 'P-521 Curve'); JSONWebKeyEllipticCurve.ED25519 = new JSONWebKeyEllipticCurve('Ed25519', 'Ed25519 signature algorithm key pairs'); JSONWebKeyEllipticCurve.ED448 = new JSONWebKeyEllipticCurve('Ed448', 'Ed448 signature algorithm key pairs'); JSONWebKeyEllipticCurve.X25519 = new JSONWebKeyEllipticCurve('X25519', 'X25519 function key pairs'); JSONWebKeyEllipticCurve.X448 = new JSONWebKeyEllipticCurve('X448', 'X448 function key pairs'); JSONWebKeyEllipticCurve.SECP256K1 = new JSONWebKeyEllipticCurve('secp256k1', 'SECG secp256k1 curve'); class JSONWebKeyParameter { constructor(_name, _description, _usedWith) { this._name = _name; this._description = _description; this._usedWith = _usedWith; JSONWebKeyParameter._values.push(this); } get name() { return this._name; } get description() { return this._description; } get usedWith() { return this._usedWith; } static values() { return JSONWebKeyParameter._values; } static fromName(usedWith, name) { const found = JSONWebKeyParameter.values().find((j) => { return j.usedWith.includes(usedWith) && j.name === name; }); return found || null; } } exports.JSONWebKeyParameter = JSONWebKeyParameter; JSONWebKeyParameter._values = []; JSONWebKeyParameter.KTY = new JSONWebKeyParameter('kty', 'Key Type', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.USE = new JSONWebKeyParameter('use', 'Public Key Use', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.KEY_OPS = new JSONWebKeyParameter('key_ops', 'Key Operations', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.ALG = new JSONWebKeyParameter('alg', 'Algorithm', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.KID = new JSONWebKeyParameter('kid', 'Key ID', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.X5U = new JSONWebKeyParameter('x5u', 'X.509 URL', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.X5C = new JSONWebKeyParameter('x5c', 'X.509 Certificate Chain', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.X5T = new JSONWebKeyParameter('x5t', 'X.509 Certificate SHA-1 Thumbprint', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.X5T_SHARP_S256 = new JSONWebKeyParameter('x5t#S256', 'X.509 Certificate SHA-256 Thumbprint', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); JSONWebKeyParameter.EC_CRV = new JSONWebKeyParameter('crv', 'Curve', [JSONWebKeyType.EC]); JSONWebKeyParameter.EC_X = new JSONWebKeyParameter('x', 'X Coordinate', [JSONWebKeyType.EC]); JSONWebKeyParameter.EC_Y = new JSONWebKeyParameter('y', 'Y Coordinate', [JSONWebKeyType.EC]); JSONWebKeyParameter.EC_D = new JSONWebKeyParameter('d', 'ECC Private Key', [JSONWebKeyType.EC]); JSONWebKeyParameter.N = new JSONWebKeyParameter('n', 'Modulus', [JSONWebKeyType.RSA]); JSONWebKeyParameter.E = new JSONWebKeyParameter('e', 'Exponent', [JSONWebKeyType.RSA]); JSONWebKeyParameter.RSA_D = new JSONWebKeyParameter('d', 'Private Exponent', [JSONWebKeyType.RSA]); JSONWebKeyParameter.P = new JSONWebKeyParameter('p', 'First Prime Factor', [JSONWebKeyType.RSA]); JSONWebKeyParameter.Q = new JSONWebKeyParameter('q', 'Second Prime Factor', [JSONWebKeyType.RSA]); JSONWebKeyParameter.DP = new JSONWebKeyParameter('dp', 'First Factor CRT Exponent', [JSONWebKeyType.RSA]); JSONWebKeyParameter.DQ = new JSONWebKeyParameter('dq', 'Second Factor CRT Exponent', [JSONWebKeyType.RSA]); JSONWebKeyParameter.QI = new JSONWebKeyParameter('qi', 'First CRT Coefficient', [JSONWebKeyType.RSA]); JSONWebKeyParameter.OTH = new JSONWebKeyParameter('oth', 'Other Primes Info', [JSONWebKeyType.RSA]); JSONWebKeyParameter.K = new JSONWebKeyParameter('k', 'Key Value', [JSONWebKeyType.OCT]); JSONWebKeyParameter.OKP_CRV = new JSONWebKeyParameter('crv', 'The subtype of key pair', [JSONWebKeyType.OKP]); JSONWebKeyParameter.OKP_D = new JSONWebKeyParameter('d', 'The private key', [JSONWebKeyType.OKP]); JSONWebKeyParameter.OKP_X = new JSONWebKeyParameter('x', 'The public key', [JSONWebKeyType.OKP]); JSONWebKeyParameter.EXT = new JSONWebKeyParameter('ext', 'Extractable', [ JSONWebKeyType.EC, JSONWebKeyType.RSA, JSONWebKeyType.OCT, JSONWebKeyType.OKP, ]); class JSONWebKeyUse { constructor(_value, _description) { this._value = _value; this._description = _description; JSONWebKeyUse._values.push(this); } get value() { return this._value; } get description() { return this._description; } static values() { return JSONWebKeyUse._values; } static fromValue(value) { const found = JSONWebKeyUse.values().find((j) => { return j.value === value; }); return found || null; } } exports.JSONWebKeyUse = JSONWebKeyUse; JSONWebKeyUse._values = []; JSONWebKeyUse.SIG = new JSONWebKeyUse('sig', 'Digital Signature or MAC'); JSONWebKeyUse.ENC = new JSONWebKeyUse('enc', 'Encryption'); class JSONWebKeyOperation { constructor(_value, _description) { this._value = _value; this._description = _description; JSONWebKeyOperation._values.push(this); } get value() { return this._value; } get description() { return this._description; } static values() { return JSONWebKeyOperation._values; } static fromValue(value) { const found = JSONWebKeyOperation.values().find((j) => { return j.value === value; }); return found || null; } } exports.JSONWebKeyOperation = JSONWebKeyOperation; JSONWebKeyOperation._values = []; JSONWebKeyOperation.SIGN = new JSONWebKeyOperation('sign', 'Compute digital signature or MAC'); JSONWebKeyOperation.VERIFY = new JSONWebKeyOperation('verify', 'Verify digital signature or MAC'); JSONWebKeyOperation.ENCRYPT = new JSONWebKeyOperation('encrypt', 'Encrypt content'); JSONWebKeyOperation.DECRYPT = new JSONWebKeyOperation('decrypt', 'Decrypt content and validate decryption, if applicable'); JSONWebKeyOperation.WRAP_KEY = new JSONWebKeyOperation('wrapKey', 'Encrypt key'); JSONWebKeyOperation.UNWRAP_KEY = new JSONWebKeyOperation('unwrapKey', 'Decrypt key and validate decryption, if applicable'); JSONWebKeyOperation.DERIVE_KEY = new JSONWebKeyOperation('deriveKey', 'Derive key'); JSONWebKeyOperation.DERIVE_BITS = new JSONWebKeyOperation('deriveBits', 'Derive bits not to be used as a key'); //# sourceMappingURL=joseKey.js.map